- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbytecode.lua
More file actions
Latest commit
1308 lines (1247 loc) · 36.3 KB
/
Copy pathbytecode.lua
File metadata and controls
1308 lines (1247 loc) · 36.3 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[=[
dump = header proto+ 0U
header = ESC 'L' 'J' versionB flagsU [namelenU nameB*]
proto = lengthU pdata
pdata = phead bcinsW* uvdataH* kgc* knum* [debugB*]
phead = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU
[debuglenU [firstlineU numlineU]]
kgc = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* }
knum = intU0 | (loU1 hiU)
ktab = narrayU nhashU karray* khash*
karray = ktabk
khash = ktabk ktabk
ktabk = ktabtypeU { intU | (loU hiU) | strB* }
uvdata = register index with high bit set if local to outer
debug = lninfoV* uvals vars '\0'
uvals = nameB* '\0'
vars = nameB* '\0' startU endU
B = 8 bit,
H = 16 bit,
W = 32 bit,
V = B, H or W,
U = ULEB128 of W, U0/U1 = ULEB128 of W+1,
]=]
localbit=require'bit'
localffi=require'ffi'
localjit=require'jit'
localband, bor, shl, shr, bnot=bit.band, bit.bor, bit.lshift, bit.rshift, bit.bnot
localjit_v21=jit.version_num>=20100
localtypeof=getmetatable
localfunctionenum(t)
fori=0,#tdot[t[i]] =iend
returnt
end
-- forward declarations
localBuf, Ins, Proto, Dump, KNum, KObj
localMAX_REG=200
localMAX_UVS=60
localfunctionenum_mode(t)
localre, rm= {}, {}
localnskip=0
fori=1, #tdo
localv=t[i]
ifv[3] ==falsethen
nskip=nskip+1
else
localidx=i-nskip-1
rm[idx ] =v[2]
re[v[1]] =idx
end
end
returnre, rm
end
localBC_ABC=0
localBC_AD=1
localBC_AJ=2
localBC, BC_MODE=enum_mode {
{ 'ISLT' , BC_AD },
{ 'ISGE' , BC_AD },
{ 'ISLE' , BC_AD },
{ 'ISGT' , BC_AD },
{ 'ISEQV' , BC_AD },
{ 'ISNEV' , BC_AD },
{ 'ISEQS' , BC_AD },
{ 'ISNES' , BC_AD },
{ 'ISEQN' , BC_AD },
{ 'ISNEN' , BC_AD },
{ 'ISEQP' , BC_AD },
{ 'ISNEP' , BC_AD },
{ 'ISTC' , BC_AD },
{ 'ISFC' , BC_AD },
{ 'IST' , BC_AD },
{ 'ISF' , BC_AD },
{ 'ISTYPE', BC_AD, jit_v21 },
{ 'ISNUM' , BC_AD, jit_v21 },
{ 'MOV' , BC_AD },
{ 'NOT' , BC_AD },
{ 'UNM' , BC_AD },
{ 'LEN' , BC_AD },
{ 'ADDVN' , BC_ABC },
{ 'SUBVN' , BC_ABC },
{ 'MULVN' , BC_ABC },
{ 'DIVVN' , BC_ABC },
{ 'MODVN' , BC_ABC },
{ 'ADDNV' , BC_ABC },
{ 'SUBNV' , BC_ABC },
{ 'MULNV' , BC_ABC },
{ 'DIVNV' , BC_ABC },
{ 'MODNV' , BC_ABC },
{ 'ADDVV' , BC_ABC },
{ 'SUBVV' , BC_ABC },
{ 'MULVV' , BC_ABC },
{ 'DIVVV' , BC_ABC },
{ 'MODVV' , BC_ABC },
{ 'POW' , BC_ABC },
{ 'CAT' , BC_ABC },
{ 'KSTR' , BC_AD },
{ 'KCDATA', BC_AD },
{ 'KSHORT', BC_AD },
{ 'KNUM' , BC_AD },
{ 'KPRI' , BC_AD },
{ 'KNIL' , BC_AD },
{ 'UGET' , BC_AD },
{ 'USETV' , BC_AD },
{ 'USETS' , BC_AD },
{ 'USETN' , BC_AD },
{ 'USETP' , BC_AD },
{ 'UCLO' , BC_AJ },
{ 'FNEW' , BC_AD },
{ 'TNEW' , BC_AD },
{ 'TDUP' , BC_AD },
{ 'GGET' , BC_AD },
{ 'GSET' , BC_AD },
{ 'TGETV' , BC_ABC },
{ 'TGETS' , BC_ABC },
{ 'TGETB' , BC_ABC },
{ 'TGETR' , BC_ABC, jit_v21 },
{ 'TSETV' , BC_ABC },
{ 'TSETS' , BC_ABC },
{ 'TSETB' , BC_ABC },
{ 'TSETM' , BC_AD },
{ 'TSETR' , BC_ABC, jit_v21 },
{ 'CALLM' , BC_ABC },
{ 'CALL' , BC_ABC },
{ 'CALLMT', BC_AD },
{ 'CALLT' , BC_AD },
{ 'ITERC' , BC_ABC },
{ 'ITERN' , BC_ABC },
{ 'VARG' , BC_ABC },
{ 'ISNEXT', BC_AJ },
{ 'RETM' , BC_AD },
{ 'RET' , BC_AD },
{ 'RET0' , BC_AD },
{ 'RET1' , BC_AD },
{ 'FORI' , BC_AJ },
{ 'JFORI' , BC_AJ },
{ 'FORL' , BC_AJ },
{ 'IFORL' , BC_AJ },
{ 'JFORL' , BC_AD },
{ 'ITERL' , BC_AJ },
{ 'IITERL', BC_AJ },
{ 'JITERL', BC_AD },
{ 'LOOP' , BC_AJ },
{ 'ILOOP' , BC_AJ },
{ 'JLOOP' , BC_AD },
{ 'JMP' , BC_AJ },
{ 'FUNCF' , BC_AD },
{ 'IFUNCF', BC_AD },
{ 'JFUNCF', BC_AD },
{ 'FUNCV' , BC_AD },
{ 'IFUNCV', BC_AD },
{ 'JFUNCV', BC_AD },
{ 'FUNCC' , BC_AD },
{ 'FUNCCW', BC_AD },
}
localVKNIL=0
localVKFALSE=1
localVKTRUE=2
localNO_JMP=bnot(0)
-- Type codes for the GC constants of a prototype. Plus length for strings.
localKOBJ=enum {
[0] ="CHILD", "TAB", "I64", "U64", "COMPLEX", "STR",
}
-- Type codes for the keys/values of a constant table.
localKTAB=enum {
[0] ="NIL", "FALSE", "TRUE", "INT", "NUM", "STR",
}
localFOR_IDX="(for index)";
localFOR_STOP="(for limit)";
localFOR_STEP="(for step)";
localFOR_GEN="(for generator)";
localFOR_STATE="(for state)";
localFOR_CTL="(for control)";
localfunctionnum_is_int32(x)
returnx%1==0andx>=-2^31andx<2^31
end
Buf= {}
Buf.new=function(size)
size=sizeor2048
localself= {
data=ffi.new('char[?]', size),
size=size,
offs=0,
}
returnsetmetatable(self, Buf)
end
Buf.__index= {}
Buf.__index.need=function(self, size)
localneed_size=self.offs+size
ifself.size<=need_sizethen
localprev_size=self.size
whileself.size<=need_sizedo
self.size=self.size*2
end
localnew_data=ffi.new('char[?]', self.size)
ffi.copy(new_data, self.data, prev_size)
self.data=new_data
end
end
Buf.__index.put=function(self, v)
self:need(1)
localoffs=self.offs
self.data[offs] =v
self.offs=offs+1
returnoffs
end
Buf.__index.put_uint8=Buf.__index.put
Buf.__index.put_uint16=function(self, v)
self:need(2)
localoffs=self.offs
localdptr=self.data+offs
dptr[0] =v
v=shr(v, 8)
dptr[1] =v
self.offs=offs+2
returnoffs
end
Buf.__index.put_uint32=function(self, v)
self:need(4)
localoffs=self.offs
localdptr=self.data+offs
dptr[0] =v
v=shr(v, 8)
dptr[1] =v
v=shr(v, 8)
dptr[2] =v
v=shr(v, 8)
dptr[3] =v
self.offs=offs+4
returnoffs
end
Buf.__index.put_uleb128=function(self, v)
localoffs=self.offs
localb=band(v, 0x7f)
v=shr(v, 7)
ifv~=0thenb=bor(b, 0x80) end
self:put(b)
whilev>0do
b=band(v, 0x7f)
v=shr(v, 7)
ifv~=0thenb=bor(b, 0x80) end
self:put(b)
end
returnoffs
end
-- Write a 32 bit unsigned integer + 1 bit given by "numbit".
-- This last argument should be either 0 or 1.
Buf.__index.put_uleb128_33=function(self, v, numbit)
localoffs=self.offs
localb=bor(shl(band(v, 0x3f), 1), numbit)
v=shr(v, 6)
ifv~=0thenb=bor(b, 0x80) end
self:put(b)
whilev>0do
b=band(v, 0x7f)
v=shr(v, 7)
ifv~=0thenb=bor(b, 0x80) end
self:put(b)
end
returnoffs
end
Buf.__index.put_bytes=function(self, v)
localoffs=self.offs
self:need(#v)
ffi.copy(self.data+offs, v)
self.offs=offs+#v
returnoffs
end
Buf.__index.pack=function(self)
returnffi.string(self.data, self.offs)
end
localdouble_new=ffi.typeof('double[1]')
localuint32_new=ffi.typeof('uint32_t[1]')
localint64_new=ffi.typeof('int64_t[1]')
localuint64_new=ffi.typeof('uint64_t[1]')
localfunctiondword_get_u32(cdata_new, v)
localp=cdata_new(v)
localchar=ffi.cast('uint8_t*', p)
locallo, hi=uint32_new(0), uint32_new(0)
ffi.copy(lo, char, 4)
ffi.copy(hi, char+4, 4)
returnlo[0], hi[0]
end
Buf.__index.put_number=function(self, v)
localoffs=self.offs
ifnum_is_int32(v) then
self:put_uleb128_33(v, 0)
else
locallo, hi=dword_get_u32(double_new, v)
self:put_uleb128_33(lo, 1)
self:put_uleb128(hi)
end
returnoffs
end
Ins= {}
Ins.__index= {}
functionIns.new(op, a, b, c)
returnsetmetatable({
op;
aor0;
bor0;
cor0;
}, Ins)
end
functionIns.__index:rewrite(op, a, b, c)
self[1], self[2], self[3], self[4] =op, aor0, bor0, cor0
end
functionIns.__index:write(buf)
localop, a=self[1], self[2]
buf:put(op)
buf:put(a)
localmode=BC_MODE[op]
ifmode==BC_ABCthen
localb, c=self[3], self[4]
buf:put(c)
buf:put(b)
elseifmode==BC_ADthen
locald=self[3]
buf:put_uint16(d)
elseifmode==BC_AJthen
localj=self[3]
buf:put_uint16(j+0x8000)
else
error("bad instruction ["..tostring(op).."] (op mode unknown)")
end
end
localfunctionhsize2hbits(s)
ifs<=0then
return0
elseifs==1then
return1
end
s=s-1
localc=0
whiles>0do
s=shr(s, 1)
c=c+1
end
returnc
end
localfunctiontabsize(narr, nrec)
returnbor(narr, shl(hsize2hbits(nrec), 11))
end
functionIns.__index.tnewsize(narry, nhash)
ifnarrythen
ifnarry<3then
narry=3
elseifnarry>0x7ffthen
narry=0x7ff
end
else
narry=0
end
returntabsize(narry, nhashor0)
end
KObj= {}
KObj.__index= {}
functionKObj.new(v)
returnsetmetatable({ v }, KObj)
end
functionKObj.__index:write(buf)
localt, v=type(self[1]), self[1]
ift=="string" then
self:write_string(buf, v)
elseift=='table' then
iftypeof(v) ==Protothen
self:write_proto(buf, v)
else
self:write_table(buf, v)
end
elseift=='cdata' then
self:write_kcdata(buf, v)
end
end
functionKObj.__index:write_string(buf, v)
buf:put_uleb128(KOBJ.STR+#v)
buf:put_bytes(v)
end
functionKObj.__index:write_kcdata(buf, v)
ifffi.istype('double complex', v) then
buf:put_uleb128(KOBJ.COMPLEX)
locallo, hi=dword_get_u32(double_new, v[0])
buf:put_uleb128(lo)
buf:put_uleb128(hi)
lo, hi=dword_get_u32(double_new, v[1])
buf:put_uleb128(lo)
buf:put_uleb128(hi)
elseifffi.istype('uint64_t', v) then
buf:put_uleb128(KOBJ.U64)
locallo, hi=dword_get_u32(uint64_new, v)
buf:put_uleb128(lo)
buf:put_uleb128(hi)
elseifffi.istype('int64_t', v) then
buf:put_uleb128(KOBJ.I64)
locallo, hi=dword_get_u32(int64_new, v)
buf:put_uleb128(lo)
buf:put_uleb128(hi)
else
assert(false, 'Unknown KCDATA : ' ..tostring(v))
end
end
localfunctionwrite_ktabk(buf, val, narrow)
localtp=type(val)
iftp=="string" then
buf:put_uleb128(KTAB.STR+#val)
buf:put_bytes(val)
elseiftp=="number" then
ifnarrowandnum_is_int32(val) then
buf:put(KTAB.INT)
buf:put_uleb128(val)
return
end
locallo, hi=dword_get_u32(double_new, val)
buf:put(KTAB.NUM)
buf:put_uleb128(lo)
buf:put_uleb128(hi)
elseiftp=="boolean" then
buf:put(valandKTAB.TRUEorKTAB.FALSE)
elseiftp=="nil" then
buf:put(KTAB.NIL)
else
assert(false, "error with constant in ktabk")
end
end
functionKObj.__index:write_table(buf, v)
buf:put_uleb128(KOBJ.TAB)
buf:put_uleb128(v.narray)
buf:put_uleb128(v.nhash)
fori=0, v.narray-1do
write_ktabk(buf, v.array[i], true)
end
fori=1, v.nhashdo
write_ktabk(buf, v.hash_keys[i], false)
write_ktabk(buf, v.hash_values[i], true)
end
end
KNum= {}
KNum.__index= {}
functionKNum.new(v)
returnsetmetatable({ v }, KNum)
end
functionKNum.__index:write(buf)
buf:put_number(self[1])
end
-- Determine if a backward jump up to "label_name" needs an UCLO instruction.
localfunctionlabel_need_uclo(scope, label_name)
locallabel_list=scope.goto_labels
localneed_uclo=false
fori=#label_list, 1, -1do
locallabel=label_list[i]
need_uclo=need_ucloorlabel.need_uclo
iflabel.name==label_namethenbreakend
end
returnneed_uclo
end
localfunctiongoto_label_resolve(self, label_name)
localscope=self.scope
whilescopedo
localls=scope.goto_labels
fori=1, #lsdo
ifls[i].name==label_namethen
returnls[i]
end
end
scope=scope.outer
end
end
localfunctiongoto_label_append(self, label_name)
localscope=self.scope
locallabel= { name=label_name, scope=scope, basereg=self.freereg, need_uclo=false, pc=#self.code }
scope.goto_labels[#scope.goto_labels+1] =label
returnlabel
end
localfunctiongoto_fixup_append(self, label_name, source_line, pc)
localscope=self.scope
localfixup= { name=label_name, basereg=self.freereg, need_uclo=false, pc=pc, source_line=source_line }
scope.goto_fixups[#scope.goto_fixups+1] =fixup
end
-- Follow the scope from the bottom (src_scope) to upper (dest_scope) to
-- tell if an UCLO instruction is needed to perform such a backward jump.
localfunctiongoto_uclo_backward(self, label, src_scope, dest_scope)
localscope=src_scope
localneed_uclo=false
whilescopeandscope~=dest_scopedo
need_uclo=need_ucloorscope.need_uclo
scope=scope.outer
end
ifscopethen
need_uclo=need_ucloorlabel_need_uclo(scope, label.name)
end
returnneed_uclo
end
-- Bind dangling goto instructions ("fixups") to a label.
-- When a dangling goto is bound to a label it is removed from the
-- fixup list.
localfunctiongoto_label_bind(self, label_name)
localfixup_list=self.scope.goto_fixups
locali=1
whilefixup_list[i] do
localfixup=fixup_list[i]
iffixup.name==label_namethen
ifself.freereg>fixup.baseregthenreturnfixup, fixup.baseregend
self:fix_goto(fixup.pc, self.freereg, fixup.need_uclo, #self.code-fixup.pc)
table.remove(fixup_list, i)
else
i=i+1
end
end
end
Proto= {
CHILD=0x01; -- Has child prototypes.
VARARG=0x02; -- Vararg function.
FFI=0x04; -- Uses BC_KCDATA for FFI datatypes.
NOJIT=0x08; -- JIT disabled for this function.
ILOOP=0x10; -- Patched bytecode with ILOOP etc.
}
functionProto.new(flags, firstline, lastline, outer)
localproto=setmetatable({
flags=flagsor0;
outer=outer;
params= {};
upvals= {};
code= {};
kobj= {};
knum= {};
debug= {};
lninfo= {};
labels= {};
tohere= {};
kcache= {};
varinfo= {};
freereg=0;
firstline=firstline;
lastline=lastline;
currline=firstline;
numlines=lastline-firstline;
framesize=1;
explret=false;
}, Proto)
proto:enter()
returnproto
end
Proto.__index= {}
functionProto.__index:nextreg(num)
num=numor1
localreg=self.freereg
self.freereg=self.freereg+num
ifself.freereg>=self.framesizethen
self.framesize=self.freereg
end
returnreg
end
functionProto.__index:setreg(reg)
self.freereg=reg
ifself.freereg>=self.framesizethen
self.framesize=self.freereg
end
end
functionProto.__index:maxframe(reg)
ifreg>self.framesizethen
self.framesize=reg
end
end
functionProto.__index:enter()
localouter=self.scope
self.scope= {
actvars= {};
basereg=self.freereg;
goto_labels= {};
goto_fixups= {};
outer=outer;
}
goto_label_append(self, nil) -- Add a label without name.
returnself.scope
end
functionProto.__index:is_root_scope()
return (self.scope.outer==nil)
end
functionProto.__index:leave()
fori=1, #self.scope.actvarsdo
self.scope.actvars[i].endpc=#self.code
end
localfreereg=self.scope.basereg
self.scope=self.scope.outer
self.freereg=freereg
end
functionProto.__index:child(firstline, lastline)
self.flags=bor(self.flags, Proto.CHILD)
localchild=Proto.new(0, firstline, lastline, self)
child.idx=#self.kobj
self.kobj[child] =#self.kobj
self.kobj[#self.kobj+1] =child
returnchild
end
functionProto.__index:parent()
localparent=self.outer
parent.flags=bor(parent.flags, band(self.flags, Proto.FFI))
returnparent
end
functionProto.__index:kpri(val)
ifval==nilthenreturnVKNIL
elseifval==truethenreturnVKTRUE
elseifval==falsethenreturnVKFALSE
elseerror('Invalid primitive value: '..val) end
end
functionProto.__index:const(val)
iftype(val) =='string' then
ifnotself.kcache[val] then
localitem=KObj.new(val)
item.idx=#self.kobj
self.kcache[val] =item
self.kobj[#self.kobj+1] =item
end
elseiftype(val) =='number' then
ifnotself.kcache[val] then
localitem=KNum.new(val)
item.idx=#self.knum
self.kcache[val] =item
self.knum[#self.knum+1] =item
end
elseiftype(val) =='cdata' then
localitem=KObj.new(val)
item.idx=#self.kobj
self.kobj[#self.kobj+1] =item
-- Set the FFI flag for the proto.
self.flags=bor(self.flags, Proto.FFI)
returnitem.idx
else
error("not a const: "..tostring(val))
end
returnself.kcache[val].idx
end
functionProto.__index:new_table_template()
localt= { array= {}, hash_keys= {}, hash_values= {} }
localitem=KObj.new(t)
item.idx=#self.kobj
self.kobj[#self.kobj+1] =item
returnitem.idx, t
end
functionProto.__index:line(ln)
self.currline=ln
end
-- Set line number for the last generated instruction.
functionProto.__index:setpcline(ln)
self.lninfo[#self.lninfo] =ln
end
functionProto.__index:emit(op, a, b, c)
localins=Ins.new(op, a, b, c)
self.code[#self.code+1] =ins
self.lninfo[#self.lninfo+1] =self.currline
returnins
end
functionProto.__index:write(buf)
localhas_child
ifband(self.flags, Proto.CHILD) ~=0then
has_child=true
fori=1, #self.kobjdo
localo=self.kobj[i]
iftypeof(o) ==Protothen
o:write(buf)
end
end
end
localbody=Buf.new()
self:write_body(body)
localoffs=body.offs
self:write_debug(body)
localhead=Buf.new()
self:write_head(head, body.offs-offs)
buf:put_uleb128(head.offs+body.offs) -- length of the proto
localhead_pack=ffi.string(head.data, head.offs)
localbody_pack=ffi.string(body.data, body.offs)
buf:put_bytes(head_pack)
buf:put_bytes(body_pack)
end
functionProto.__index:write_head(buf, size_debug)
buf:put(self.flags)
buf:put(#self.params)
buf:put(self.framesize)
buf:put(#self.upvals)
buf:put_uleb128(#self.kobj)
buf:put_uleb128(#self.knum)
buf:put_uleb128(#self.code)
buf:put_uleb128(size_debugor0)
buf:put_uleb128(self.firstline)
buf:put_uleb128(self.numlines)
end
functionProto.__index:write_body(buf)
fori=1, #self.codedo
self.code[i]:write(buf)
end
fori=1, #self.upvalsdo
localuval=self.upvals[i]
ifuval.outer_idxthen
-- the upvalue refer to a local of the enclosing function
localbtag=uval.vinfo.mutableand0x8000or0xc000
localuv=bor(uval.outer_idx, btag)
buf:put_uint16(uv)
else
-- the upvalue refer to an upvalue of the enclosing function
localuv=uval.outer_uv
buf:put_uint16(uv)
end
end
fori=#self.kobj, 1, -1do
localo=self.kobj[i]
iftypeof(o) ==Protothen
buf:put_uleb128(KOBJ.CHILD)
else
self.kobj[i]:write(buf)
end
end
fori=1, #self.knumdo
self.knum[i]:write(buf)
end
end
functionProto.__index:write_debug(buf)
localfirst=self.firstline
ifself.numlines<256then
fori=1, #self.lninfodo
localdelta=self.lninfo[i] -first
buf:put_uint8(delta)
end
elseifself.numlines<65536then
fori=1, #self.lninfodo
localdelta=self.lninfo[i] -first
buf:put_uint16(delta)
end
else
fori=1, #self.lninfodo
localdelta=self.lninfo[i] -first
buf:put_uint32(delta)
end
end
fori=1, #self.upvalsdo
localuval=self.upvals[i]
buf:put_bytes(uval.vinfo.name.."\0")
end
locallastpc=0
fori=1, #self.varinfodo
localvar=self.varinfo[i]
localstartpc, endpc= (var.startpcor0), (var.endpcor0) +1
iftype(var.name) =="number" then
forn=var.name, var.name+2do
buf:put(n)
buf:put_uleb128(startpc-lastpc)
buf:put_uleb128(endpc-startpc)
lastpc=startpc
end
else
buf:put_bytes(var.name.."\0")
buf:put_uleb128(startpc-lastpc)
buf:put_uleb128(endpc-startpc)
end
lastpc=startpc
end
buf:put(0)
end
functionProto.__index:newvar(name, dest)
dest=destorself:nextreg()
localvinfo= {
idx=dest,
startpc=#self.code+1,
endpc=#self.code+1,
name=name,
mutable=false,
}
-- scoped variable info
self.scope.actvars[name] =vinfo
self.scope.actvars[#self.scope.actvars+1] =vinfo
-- for the debug segment only
self.varinfo[#self.varinfo+1] =vinfo
returnvinfo
end
-- Add debug variables informations for implicit for variables.
-- The code should be 1 for simple "for" statements and 4 for
-- "for in" statements.
functionProto.__index:forivars(code)
localvinfo= { name=code, startpc=#self.code+1 }
self.varinfo[#self.varinfo+1] =vinfo
returnvinfo
end
localfunctionscope_var_lookup(scope, name)
whilescopedo
localvar=scope.actvars[name]
ifvarthenreturnvar, scopeend
scope=scope.outer
end
end
functionProto.__index:lookup(name)
localvar=scope_var_lookup(self.scope, name)
ifvarthen
returnvar, false
elseifself.outerthen
localv=self.outer:lookup(name)
ifvthenreturnv, trueend
end
-- Global variable.
returnnil, false
end
functionProto.__index:var_inverse_lookup(var)
fori=1, #self.scope.actvarsdo
localxvar=self.scope.actvars[i]
ifxvar.idx==varthenreturnxvarend
end
end
functionProto.__index:param(...)
localvar=self:newvar(...)
var.startpc=0
self.params[#self.params+1] =var
returnvar.idx
end
-- Find into the scope the label that immediately precedes where the given
-- variable is declared and return the label.
functionProto.__index:label_var_lookup(scope, var)
locallabel_list=scope.goto_labels
locallabel=label_list[1]
fori=2, #label_listdo
localnext_label=label_list[i]
ifnext_label.basereg>var.idxthenbreakend
label=next_label
end
returnlabel
end
functionProto.__index:upval(name)
ifnotself.upvals[name] then
localproto, upval=self.outer, {}
localvar, scope
whileprotodo
var, scope=scope_var_lookup(proto.scope, name)
ifvarthen
break
end
proto=proto.outer
end
upval= { vinfo=var; proto=proto; }
-- for each upval we set either outer_idx or outer_uv
ifproto==self.outerthen
-- The variable is in the enclosing function's scope.
-- We store just its register index.
upval.outer_idx=var.idx
locallabel=self:label_var_lookup(scope, var)
label.need_uclo=true
scope.need_uclo=true
else
-- The variable is in the outer scope of the enclosing
-- function. We register this variable as an upvalue for
-- the enclosing function. Then we store the upvale index.
upval.outer_uv=self.outer:upval(name)
end
self.upvals[name] =upval
upval.idx=#self.upvals
self.upvals[#self.upvals+1] =upval
end
returnself.upvals[name].idx
end
functionProto.__index:here(name)
ifself.tohere[name] then
-- forward jump
localback=self.tohere[name]
fori=1, #backdo
localoffs=back[i]
self.code[offs][3] =#self.code-offs
end
self.tohere[name] =nil
else
-- backward jump
self.labels[name] =#self.code-1
end
returnname
end
functionProto.__index:enable_jump(name)
iftype(name) =='number' then
error("bad label")
end
localhere=self.tohere[name]
ifnotherethen
here= {}
self.tohere[name] =here
end
here[#here+1] =#self.code+1
end
localfunctionlocate_jump(self, name)
ifself.labels[name] then
-- backward jump
returnself.labels[name] -#self.code
else
-- forward jump
self:enable_jump(name)
returnNO_JMP
end
end
functionProto.__index:scope_jump(name, basereg, need_uclo)
localjump=locate_jump(self, name)
returnself:emit(need_ucloandBC.UCLOorBC.JMP, basereg, jump)
end
functionProto.__index:jump(name, basereg)
localjump=locate_jump(self, name)
returnself:emit(BC.JMP, basereg, jump)
end
-- When closing a scope this function is used to transfer all the dangling
-- gotos (fixups) in the enclosing scope.
-- Before being copied the basereg and need_uclo information is updated.
-- The original list is not modified since it will be dropped with the scope
-- itself.
functionProto.__index:fscope_end()
localscope=self.scope
localfixup_list=scope.goto_fixups
localouter_list=scope.outer.goto_fixups
fori=1, #fixup_listdo
localfixup=fixup_list[i]
fixup.need_uclo=fixup.need_ucloorscope.need_uclo
fixup.basereg=scope.basereg
outer_list[#outer_list+1] =fixup
end
end
functionProto.__index:fix_goto(pc, basereg, need_uclo, jump)
localins=self.code[pc]
localop=need_ucloandBC.UCLOorBC.JMP
ins:rewrite(op, basereg, jump)
end
functionProto.__index:goto_jump(label_name, source_line)
locallabel=goto_label_resolve(self, label_name)
iflabelthen-- backward jump
localdest_scope, basereg=label.scope, label.basereg
localneed_uclo=goto_uclo_backward(self, label, self.scope, dest_scope)
localop=need_ucloandBC.UCLOorBC.JMP
self:emit(op, basereg, label.pc-#self.code-1)
else-- forward jump
self:emit(BC.JMP, 0, NO_JMP)
goto_fixup_append(self, label_name, source_line, #self.code)
end
end
functionProto.__index:goto_label(label_name)
ifgoto_label_resolve(self, label_name) then
returnfalse, string.format("duplicate label '%s'", label_name)
end
locallabel=goto_label_append(self, label_name)
localgoto_err, var_err=goto_label_bind(self, label_name)
ifgoto_errthen
localvar=self:var_inverse_lookup(var_err)
returnfalse, string.format("<goto %s> jumps into the scope of local '%s'", label_name, var.name)
else
returntrue, label
end
end
functionProto.__index:loop_register(exit, exit_reg, cont)