This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
luabytecode.pir
512 lines (456 loc) · 9.58 KB
/
luabytecode.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# Copyright (C) 2008-2009, Parrot Foundation.
# $Id$
=head1 Lua bytecode translation Library
=head2 Description
=cut
.namespace ['Lua'; 'Bytecode']
.sub '__onload' :anon :load :init
$P0 = newclass ['Lua'; 'Bytecode']
addattribute $P0, 'version'
addattribute $P0, 'format'
addattribute $P0, 'endian'
addattribute $P0, 'sizeof_int'
addattribute $P0, 'sizeof_size_t'
addattribute $P0, 'sizeof_opcode'
addattribute $P0, 'sizeof_number'
addattribute $P0, 'integral'
addattribute $P0, 'top'
$P0 = newclass ['Lua'; 'Function']
addattribute $P0, 'source'
addattribute $P0, 'linedefined'
addattribute $P0, 'lastlinedefined'
addattribute $P0, 'nups'
addattribute $P0, 'numparams'
addattribute $P0, 'is_vararg'
addattribute $P0, 'maxstacksize'
addattribute $P0, 'code'
addattribute $P0, 'k'
addattribute $P0, 'p'
addattribute $P0, 'lineinfo'
addattribute $P0, 'locvars'
addattribute $P0, 'upvalues'
$P0 = subclass 'FixedIntegerArray', ['Lua'; 'InstructionList']
$P0 = subclass 'FixedPMCArray', ['Lua'; 'ConstantList']
$P0 = subclass 'Undef', ['Lua'; 'Nil']
$P0 = subclass 'Boolean', ['Lua'; 'Boolean']
$P0 = subclass 'Float', ['Lua'; 'Number']
$P0 = subclass 'String', ['Lua'; 'String']
$P0 = subclass 'FixedPMCArray', ['Lua'; 'PrototypeList']
$P0 = subclass 'FixedIntegerArray', ['Lua'; 'LineList']
$P0 = subclass 'FixedPMCArray', ['Lua'; 'LocalList']
$P0 = subclass 'String', ['Lua'; 'Local']
addattribute $P0, 'startpc'
addattribute $P0, 'endpc'
$P0 = subclass 'FixedPMCArray', ['Lua'; 'UpvalueList']
$P0 = subclass 'String', ['Lua'; 'Upvalue']
.end
.sub 'brief' :method
.param string filename
print "; source chunk: "
print filename
print "\n"
$P0 = getattribute self, 'top'
$P0.'brief'(0, 1)
.end
.sub 'translate' :method
.local string pir
pir = <<'PIRCODE'
.include 'interpinfo.pasm'
.HLL 'lua'
.loadlib 'lua_group'
.namespace []
.sub '&start' :anon :main
.param pmc args :optional
# print "start\n"
load_bytecode 'languages/lua/lua.pbc'
lua_openlibs()
.local pmc env
env = get_hll_global '_G'
.local pmc vararg
vararg = argstolua(env, args)
.const 'Sub' main = '&function_0'
main.'setfenv'(env)
($I0, $P0) = docall(main, vararg :flat)
unless $I0 goto L1
printerr 'luac2pir: '
printerr $P0
L1:
.end
PIRCODE
$P0 = getattribute self, 'top'
$S0 = $P0.'translate'('&function_0', '&start')
pir .= $S0
.return (pir)
.end
.namespace ['Lua'; 'Function']
.sub 'brief' :method
.param int i
.param int level
.local int nups
$P0 = getattribute self, 'nups'
nups = $P0
.local int numparams
$P0 = getattribute self, 'numparams'
numparams = $P0
.local int is_vararg
$P0 = getattribute self, 'is_vararg'
is_vararg = $P0
.local int maxstacksize
$P0 = getattribute self, 'maxstacksize'
maxstacksize = $P0
print "\n; function ["
print i
print "] definition (level "
print level
print ")\n"
print "; "
print nups
print " upvalues, "
print numparams
print " params, "
print maxstacksize
print " stacks\n"
print ".function "
print nups
print " "
print numparams
print " "
print is_vararg
print " "
print maxstacksize
print "\n"
$P0 = getattribute self, 'locvars'
$P0.'brief'()
$P0 = getattribute self, 'upvalues'
$P0.'brief'()
$P0 = getattribute self, 'k'
$P0.'brief'()
$P0 = getattribute self, 'p'
inc level
$P0.'brief'(level)
$P0 = getattribute self, 'code'
$P0.'brief'()
print "; end of function\n\n"
.end
.sub 'translate' :method
.param string funcname
.param string outer
.local string pir
pir = ".sub '" . funcname
pir .= "' :anon :lex :outer('"
pir .= outer
pir .= "')\n"
.local int numparams
$P0 = getattribute self, 'numparams'
numparams = $P0
.local int i
i = 0
L1:
unless i < numparams goto L2
$S0 = i
pir .= " .param pmc loc_"
pir .= $S0
pir .= " :optional\n .param int has_loc_"
pir .= $S0
pir .= " :opt_flag\n"
inc i
goto L1
L2:
.local int is_vararg
$P0 = getattribute self, 'is_vararg'
is_vararg = $P0
unless is_vararg goto L3
pir.= " .param pmc vararg :slurpy\n"
goto L4
L3:
pir .= " .param pmc extra :slurpy\n"
L4:
i = 0
L5:
unless i < numparams goto L6
$S0 = i
pir .= " if has_loc_"
pir .= $S0
pir .= ", vivify_"
pir .= $S0
pir .= "\n new loc_"
pir .= $S0
pir .= ", 'LuaNil'\n vivify_"
pir .= $S0
pir .= ":\n"
inc i
goto L5
L6:
$P0 = getattribute self, 'locvars'
$S0 = $P0.'translate'(numparams, is_vararg)
pir .= $S0
i = $P0
.local int maxstacksize
$P0 = getattribute self, 'maxstacksize'
maxstacksize = $P0
L7:
unless i < maxstacksize goto L8
pir .= " .local pmc loc_"
$S0 = i
pir .= $S0
pir .= "\n new loc_"
pir .= $S0
pir .= ", 'LuaNil'\n"
inc i
goto L7
L8:
$P0 = getattribute self, 'k'
$S0= $P0.'translate'()
pir .= $S0
pir .= " .local pmc subr, env\n"
pir .= " subr = interpinfo .INTERPINFO_CURRENT_SUB\n"
$P0 = getattribute self, 'code'
$S0 = $P0.'translate'(self, funcname)
pir .= $S0
pir .= ".end\n\n"
$P0 = getattribute self, 'p'
$S0 = $P0.'translate'(funcname)
pir .= $S0
.return (pir)
.end
.namespace ['Lua'; 'ConstantList']
.sub 'brief' :method
.local int i, n
n = self
i = 0
L1:
unless i < n goto L2
$P0 = self[i]
$P0.'brief'(i)
inc i
goto L1
L2:
.end
.sub 'translate' :method
.local string pir
.local int i, n
pir = ''
n = self
i = 0
L1:
unless i < n goto L2
$P0 = self[i]
$S0 = $P0.'translate'(i)
pir .= $S0
inc i
goto L1
L2:
.return (pir)
.end
.namespace ['Lua'; 'Nil']
.sub 'brief' :method
.param int i
print ".const nil ; "
print i
print "\n"
.end
.sub 'translate' :method
.param int i
.local string pir
pir = " .local pmc k_"
$S0 = i
pir .= $S0
pir .= "\n new k_"
pir .= $S0
pir .= ", 'LuaNil'\n"
.return (pir)
.end
.namespace ['Lua'; 'Boolean']
.sub 'brief' :method
.param int i
print ".const "
print self
print " ; "
print i
print "\n"
.end
.sub 'translate' :method
.param int i
.local string pir
pir = " .local pmc k_"
$S0 = i
pir .= $S0
pir .= "\n new k_"
pir .= $S0
pir .= ", 'LuaBoolean'\n set k_"
pir .= $S0
pir .= ", "
$S0 = self
pir .= $S0
pir .= "\n"
.return (pir)
.end
.namespace ['Lua'; 'Number']
.sub 'brief' :method
.param int i
print ".const "
print self
print " ; "
print i
print "\n"
.end
.sub 'translate' :method
.param int i
.local string pir
pir = " .local pmc k_"
$S0 = i
pir .= $S0
pir .= "\n box k_"
pir .= $S0
pir .= ", "
$S0 = self
pir .= $S0
pir .= "\n"
.return (pir)
.end
.namespace ['Lua'; 'String']
.sub 'brief' :method
.param int i
print ".const \""
$S0 = self
$S1 = escape $S0
print $S1
print "\" ; "
print i
print "\n"
.end
.sub 'translate' :method
.param int i
.local string pir
pir = " .local pmc k_"
$S0 = i
pir .= $S0
pir .= "\n box k_"
pir .= $S0
pir .= ", \""
$S0 = self
$S1 = escape $S0
pir .= $S1
pir .= "\"\n"
.return (pir)
.end
.namespace ['Lua'; 'PrototypeList']
.sub 'brief' :method
.param int level
.local int i, n
n = self
i = 0
L1:
unless i < n goto L2
$P0 = self[i]
$P0.'brief'(i, level)
inc i
goto L1
L2:
.end
.sub 'translate' :method
.param string outer
.local string pir
.local int i, n
pir = ''
n = self
i = 0
L1:
unless i < n goto L2
.local string funcname
funcname = outer . '_'
$S0 = i
funcname .= $S0
$P0 = self[i]
$S0 = $P0.'translate'(funcname, outer)
pir .= $S0
inc i
goto L1
L2:
.return (pir)
.end
.namespace ['Lua'; 'LocalList']
.sub 'brief' :method
.local int i, n
n = self
i = 0
L1:
unless i < n goto L2
$P0 = self[i]
$P0.'brief'(i)
inc i
goto L1
L2:
.end
.sub 'translate' :method
.param int numparams
.param int is_vararg
.local string pir
.local int i, n
pir = ''
n = self
i = numparams
$I0 = is_vararg & 1
unless $I0 goto L1
inc i
L1:
unless i < n goto L2
$P0 = self[i]
$S0 = $P0.'translate'(i)
pir .= $S0
inc i
goto L1
L2:
.return (pir)
.end
.namespace ['Lua'; 'Local']
.sub 'brief' :method
.param int i
print ".local \""
print self
print "\" ; "
print i
print "\n"
.end
.sub 'translate' :method
.param int i
.local string pir
pir = " .local pmc loc_"
$S0 = i
pir .= $S0
pir .= " # "
$S1 = self
pir .= $S1
pir .= "\n new loc_"
pir .= $S0
pir .= ", 'LuaNil'\n"
.return (pir)
.end
.namespace ['Lua'; 'UpvalueList']
.sub 'brief' :method
.local int i, n
n = self
i = 0
L1:
unless i < n goto L2
$P0 = self[i]
$P0.'brief'(i)
inc i
goto L1
L2:
.end
.namespace ['Lua'; 'Upvalue']
.sub 'brief' :method
.param int i
print ".upvalue \""
print self
print "\" ; "
print i
print "\n"
.end
.include 'languages/lua/src/lib/luabytecode_gen.pir'
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: