-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompressed.lua
More file actions
5375 lines (4940 loc) · 185 KB
/
Copy pathcompressed.lua
File metadata and controls
5375 lines (4940 loc) · 185 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
--[=[
so as you know this is ur backdoor yes,
but could you maybe make it compressed T^T?
is usefull for me :>
anyways this is the end of ma message
local contacts = {
Name = "raxlov";
Contacts = {
Discord = "raxlov";
};
}
--]=]
function loadsting(str)
local L, LBI, LuaK, LuaP, LuaU, LuaX, LuaY, LuaZ
LBI = function()
local advanced_debug
local lua_opcode_types = {
"ABC", "ABx", "ABC", "ABC",
"ABC", "ABx", "ABC", "ABx",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "AsBx", "ABC",
"ABC", "ABC", "ABC", "ABC",
"ABC", "ABC", "ABC", "AsBx",
"AsBx", "ABC", "ABC", "ABC",
"ABx", "ABC",
}
local lua_opcode_names = {
"MOVE", "LOADK", "LOADBOOL", "LOADNIL",
"GETUPVAL", "GETGLOBAL", "GETTABLE", "SETGLOBAL",
"SETUPVAL", "SETTABLE", "NEWTABLE", "SELF",
"ADD", "SUB", "MUL", "DIV",
"MOD", "POW", "UNM", "NOT",
"LEN", "CONCAT", "JMP", "EQ",
"LT", "LE", "TEST", "TESTSET",
"CALL", "TAILCALL", "RETURN", "FORLOOP",
"FORPREP", "TFORLOOP", "SETLIST", "CLOSE",
"CLOSURE", "VARARG"
};
--[[
local lua_opcode_numbers = {};
for number, name in next, lua_opcode_names do
lua_opcode_numbers[name] = number;
end
--]]
--- Extract bits from an integer
--@author: Stravant
local function get_bits(input, n, n2)
if n2 then
local total = 0
local digitn = 0
for i = n, n2 do
total = total + 2^digitn*get_bits(input, i)
digitn = digitn + 1
end
return total
else
local pn = 2^(n-1)
return (input % (pn + pn) >= pn) and 1 or 0
end
end
local function decode_bytecode(bytecode)
local index = 1
local big_endian = false
local int_size;
local size_t;
-- Actual binary decoding functions. Dependant on the bytecode.
local get_int, get_size_t;
-- Binary decoding helper functions
local get_int8, get_int32, get_int64, get_float64, get_string;
do
function get_int8()
local a = bytecode:byte(index, index);
index = index + 1
return a
end
function get_int32()
local a, b, c, d = bytecode:byte(index, index + 3);
index = index + 4;
return d*16777216 + c*65536 + b*256 + a
end
function get_int64()
local a = get_int32();
local b = get_int32();
return b*4294967296 + a;
end
function get_float64()
local a = get_int32()
local b = get_int32()
return (-2*get_bits(b, 32)+1)*(2^(get_bits(b, 21, 31)-1023))*
((get_bits(b, 1, 20)*(2^32) + a)/(2^52)+1)
end
function get_string(len)
local str;
if len then
str = bytecode:sub(index, index + len - 1);
index = index + len;
else
len = get_size_t();
if len == 0 then return; end
str = bytecode:sub(index, index + len - 1);
index = index + len;
end
return str;
end
end
local function decode_chunk()
local chunk;
local instructions = {};
local constants = {};
local prototypes = {};
local debug = {
lines = {};
};
chunk = {
instructions = instructions;
constants = constants;
prototypes = prototypes;
debug = debug;
};
local num;
chunk.name = get_string();-- Function name
chunk.first_line = get_int(); -- First line
chunk.last_line = get_int(); -- Last line
if chunk.name then chunk.name = chunk.name:sub(1, -2); end
chunk.upvalues = get_int8();
chunk.arguments = get_int8();
chunk.varg = get_int8();
chunk.stack = get_int8();
-- TODO: realign lists to 1
-- Decode instructions
do
num = get_int();
for i = 1, num do
local instruction = {
-- opcode = opcode number;
-- type = [ABC, ABx, AsBx]
-- A, B, C, Bx, or sBx depending on type
};
local data = get_int32();
local opcode = get_bits(data, 1, 6);
local type = lua_opcode_types[opcode + 1];
instruction.opcode = opcode;
instruction.type = type;
instruction.A = get_bits(data, 7, 14);
if type == "ABC" then
instruction.B = get_bits(data, 24, 32);
instruction.C = get_bits(data, 15, 23);
elseif type == "ABx" then
instruction.Bx = get_bits(data, 15, 32);
elseif type == "AsBx" then
instruction.sBx = get_bits(data, 15, 32) - 131071;
end
instructions[i] = instruction;
end
end
-- Decode constants
do
num = get_int();
for i = 1, num do
local constant = {
-- type = constant type;
-- data = constant data;
};
local type = get_int8();
constant.type = type;
if type == 1 then
constant.data = (get_int8() ~= 0);
elseif type == 3 then
constant.data = get_float64();
elseif type == 4 then
constant.data = get_string():sub(1, -2);
end
constants[i-1] = constant;
end
end
-- Decode Prototypes
do
num = get_int();
for i = 1, num do
prototypes[i-1] = decode_chunk();
end
end
-- Decode debug info
-- Not all of which is used yet.
do
-- line numbers
local data = debug.lines
num = get_int();
for i = 1, num do
data[i] = get_int32();
end
-- locals
num = get_int();
for i = 1, num do
get_string():sub(1, -2); -- local name
get_int32(); -- local start PC
get_int32(); -- local end PC
end
-- upvalues
num = get_int();
for i = 1, num do
get_string(); -- upvalue name
end
end
return chunk;
end
-- Verify bytecode header
do
assert(get_string(4) == "\27Lua", "Lua bytecode expected.");
assert(get_int8() == 0x51, "Only Lua 5.1 is supported.");
get_int8(); -- Oficial bytecode
big_endian = (get_int8() == 0);
int_size = get_int8();
size_t = get_int8();
if int_size == 4 then
get_int = get_int32;
elseif int_size == 8 then
get_int = get_int64;
else
-- TODO: refactor errors into table
error("Unsupported bytecode target platform");
end
if size_t == 4 then
get_size_t = get_int32;
elseif size_t == 8 then
get_size_t = get_int64;
else
error("Unsupported bytecode target platform");
end
assert(get_string(3) == "\4\8\0",
"Unsupported bytecode target platform");
end
return decode_chunk();
end
local function handle_return(...)
local c = select("#", ...)
local t = {...}
return c, t
end
local function create_wrapper(cache, upvalues)
local instructions = cache.instructions;
local constants = cache.constants;
local prototypes = cache.prototypes;
local stack, top
local environment
local IP = 1; -- instruction pointer
local vararg, vararg_size
local opcode_funcs = {
[0] = function(instruction) -- MOVE
stack[instruction.A] = stack[instruction.B];
end,
[1] = function(instruction) -- LOADK
stack[instruction.A] = constants[instruction.Bx].data;
end,
[2] = function(instruction) -- LOADBOOL
stack[instruction.A] = instruction.B ~= 0
if instruction.C ~= 0 then
IP = IP + 1
end
end,
[3] = function(instruction) -- LOADNIL
local stack = stack
for i = instruction.A, instruction.B do
stack[i] = nil
end
end,
[4] = function(instruction) -- GETUPVAL
stack[instruction.A] = upvalues[instruction.B]
end,
[5] = function(instruction) -- GETGLOBAL
local key = constants[instruction.Bx].data;
stack[instruction.A] = environment[key];
end,
[6] = function(instruction) -- GETTABLE
local C = instruction.C
local stack = stack
C = C > 255 and constants[C-256].data or stack[C]
stack[instruction.A] = stack[instruction.B][C];
end,
[7] = function(instruction) -- SETGLOBAL
local key = constants[instruction.Bx].data;
environment[key] = stack[instruction.A];
end,
[8] = function (instruction) -- SETUPVAL
upvalues[instruction.B] = stack[instruction.A]
end,
[9] = function (instruction) -- SETTABLE
local B = instruction.B;
local C = instruction.C;
local stack, constants = stack, constants;
B = B > 255 and constants[B-256].data or stack[B];
C = C > 255 and constants[C-256].data or stack[C];
stack[instruction.A][B] = C
end,
[10] = function (instruction) -- NEWTABLE
stack[instruction.A] = {}
end,
[11] = function (instruction) -- SELF
local A = instruction.A
local B = instruction.B
local C = instruction.C
local stack = stack
B = stack[B]
C = C > 255 and constants[C-256].data or stack[C]
stack[A+1] = B
stack[A] = B[C]
end,
[12] = function(instruction) -- ADD
local B = instruction.B;
local C = instruction.C;
local stack, constants = stack, constants;
B = B > 255 and constants[B-256].data or stack[B];
C = C > 255 and constants[C-256].data or stack[C];
stack[instruction.A] = B+C;
end,
[13] = function(instruction) -- SUB
local B = instruction.B;
local C = instruction.C;
local stack, constants = stack, constants;
B = B > 255 and constants[B-256].data or stack[B];
C = C > 255 and constants[C-256].data or stack[C];
stack[instruction.A] = B - C;
end,
[14] = function(instruction) -- MUL
local B = instruction.B;
local C = instruction.C;
local stack, constants = stack, constants;
B = B > 255 and constants[B-256].data or stack[B];
C = C > 255 and constants[C-256].data or stack[C];
stack[instruction.A] = B * C;
end,
[15] = function(instruction) --DIV
local B = instruction.B;
local C = instruction.C;
local stack, constants = stack, constants;
B = B > 255 and constants[B-256].data or stack[B];
C = C > 255 and constants[C-256].data or stack[C];
stack[instruction.A] = B / C;
end,
[16] = function(instruction) -- MOD
local B = instruction.B;
local C = instruction.C;
local stack, constants = stack, constants;
B = B > 255 and constants[B-256].data or stack[B];
C = C > 255 and constants[C-256].data or stack[C];
stack[instruction.A] = B % C;
end,
[17] = function(instruction) -- POW
local B = instruction.B;
local C = instruction.C;
local stack, constants = stack, constants;
B = B > 255 and constants[B-256].data or stack[B];
C = C > 255 and constants[C-256].data or stack[C];
stack[instruction.A] = B ^ C;
end,
[18] = function(instruction) -- UNM
stack[instruction.A] = -stack[instruction.B]
end,
[19] = function(instruction) -- NOT
stack[instruction.A] = not stack[instruction.B]
end,
[20] = function(instruction) -- LEN
stack[instruction.A] = #stack[instruction.B]
end,
[21] = function(instruction) -- CONCAT
local B = instruction.B
local result = stack[B]
for i = B+1, instruction.C do
result = result .. stack[i]
end
stack[instruction.A] = result
end,
[22] = function(instruction) -- JUMP
IP = IP + instruction.sBx
end,
[23] = function(instruction) -- EQ
local A = instruction.A
local B = instruction.B
local C = instruction.C
local stack, constants = stack, constants
A = A ~= 0
B = B > 255 and constants[B-256].data or stack[B]
C = C > 255 and constants[C-256].data or stack[C]
if (B == C) ~= A then
IP = IP + 1
end
end,
[24] = function(instruction) -- LT
local A = instruction.A
local B = instruction.B
local C = instruction.C
local stack, constants = stack, constants
A = A ~= 0
B = B > 255 and constants[B-256].data or stack[B]
C = C > 255 and constants[C-256].data or stack[C]
if (B < C) ~= A then
IP = IP + 1
end
end,
[25] = function(instruction) -- LT
local A = instruction.A
local B = instruction.B
local C = instruction.C
local stack, constants = stack, constants
A = A ~= 0
B = B > 255 and constants[B-256].data or stack[B]
C = C > 255 and constants[C-256].data or stack[C]
if (B <= C) ~= A then
IP = IP + 1
end
end,
[26] = function(instruction) -- TEST
if stack[instruction.A] == (instruction.C ~= 0) then
IP = IP + 1
end
end,
[27] = function(instruction) -- TESTSET
local stack = stack
local B = stack[instruction.B]
if B == (instruction.C ~= 0) then
IP = IP + 1
else
stack[instruction.A] = B
end
end,
[28] = function(instruction) -- CALL
local A = instruction.A;
local B = instruction.B;
local C = instruction.C;
local stack = stack;
local args, results;
local limit, loop
args = {};
if B ~= 1 then
if B ~= 0 then
limit = A+B-1;
else
limit = top
end
loop = 0
for i = A+1, limit do
loop = loop + 1
args[loop] = stack[i];
end
limit, results = handle_return(stack[A](unpack(args, 1, limit-A)))
else
limit, results = handle_return(stack[A]())
end
top = A - 1
if C ~= 1 then
if C ~= 0 then
limit = A+C-2;
else
limit = limit+A
end
loop = 0;
for i = A, limit do
loop = loop + 1;
stack[i] = results[loop];
end
end
end,
[29] = function (instruction) -- TAILCALL
local A = instruction.A;
local B = instruction.B;
local C = instruction.C;
local stack = stack;
local args, results;
local top, limit, loop = top
args = {};
if B ~= 1 then
if B ~= 0 then
limit = A+B-1;
else
limit = top
end
loop = 0
for i = A+1, limit do
loop = loop + 1
args[#args+1] = stack[i];
end
results = {stack[A](unpack(args, 1, limit-A))};
else
results = {stack[A]()};
end
return true, results
end,
[30] = function(instruction) -- RETURN
--TODO: CLOSE
local A = instruction.A;
local B = instruction.B;
local stack = stack;
local limit;
local loop, output;
if B == 1 then
return true;
end
if B == 0 then
limit = top
else
limit = A + B - 2;
end
output = {};
local loop = 0
for i = A, limit do
loop = loop + 1
output[loop] = stack[i];
end
return true, output;
end,
[31] = function(instruction) -- FORLOOP
local A = instruction.A
local stack = stack
local step = stack[A+2]
local index = stack[A] + step
stack[A] = index
if step > 0 then
if index <= stack[A+1] then
IP = IP + instruction.sBx
stack[A+3] = index
end
else
if index >= stack[A+1] then
IP = IP + instruction.sBx
stack[A+3] = index
end
end
end,
[32] = function(instruction) -- FORPREP
local A = instruction.A
local stack = stack
stack[A] = stack[A] - stack[A+2]
IP = IP + instruction.sBx
end,
[33] = function(instruction) -- TFORLOOP
local A = instruction.A
local B = instruction.B
local C = instruction.C
local stack = stack
local offset = A+2
local result = {stack[A](stack[A+1], stack[A+2])}
for i = 1, C do
stack[offset+i] = result[i]
end
if stack[A+3] ~= nil then
stack[A+2] = stack[A+3]
else
IP = IP + 1
end
end,
[34] = function(instruction) -- SETLIST
local A = instruction.A
local B = instruction.B
local C = instruction.C
local stack = stack
if C == 0 then
error("NYI: extended SETLIST")
else
local offset = (C - 1) * 50
local t = stack[A]
if B == 0 then
B = top
end
for i = 1, B do
t[offset+i] = stack[A+i]
end
end
end,
[35] = function(instruction) -- CLOSE
--io.stderr:write("NYI: CLOSE")
--io.stderr:flush()
end,
[36] = function(instruction) -- CLOSURE
local proto = prototypes[instruction.Bx]
local instructions = instructions
local stack = stack
local indices = {}
local new_upvals = setmetatable({},
{
__index = function(t, k)
local upval = indices[k]
return upval.segment[upval.offset]
end,
__newindex = function(t, k, v)
local upval = indices[k]
upval.segment[upval.offset] = v
end
}
)
for i = 1, proto.upvalues do
local movement = instructions[IP]
if movement.opcode == 0 then -- MOVE
indices[i-1] = {segment = stack, offset = movement.B}
elseif instructions[IP].opcode == 4 then -- GETUPVAL
indices[i-1] = {segment = upvalues, offset = movement.B}
end
IP = IP + 1
end
local _, func = create_wrapper(proto, new_upvals)
stack[instruction.A] = func
end,
[37] = function(instruction) -- VARARG
local A = instruction.A
local B = instruction.B
local stack, vararg = stack, vararg
for i = A, A + (B > 0 and B - 1 or vararg_size) do
stack[i] = vararg[i - A]
end
end,
}
local function loop()
local instructions = instructions
local instruction, a, b
while true do
instruction = instructions[IP];
IP = IP + 1
a, b = opcode_funcs[instruction.opcode](instruction);
if a then
return b;
end
end
end
local debugging = {
get_stack = function()
return stack;
end;
get_IP = function()
return IP;
end
};
local function func(...)
local local_stack = {};
local ghost_stack = {};
top = -1
stack = setmetatable(local_stack, {
__index = ghost_stack;
__newindex = function(t, k, v)
if k > top and v then
top = k
end
ghost_stack[k] = v
end;
})
local args = {...};
vararg = {}
vararg_size = select("#", ...) - 1
for i = 0, vararg_size do
local_stack[i] = args[i+1];
vararg[i] = args[i+1]
end
environment = getfenv();
IP = 1;
local thread = coroutine.create(loop)
local a, b = coroutine.resume(thread)
if a then
if b then
return unpack(b);
end
return;
else
if advanced_debug then
--TODO advanced debugging
else
--TODO error converting
local name = cache.name;
local line = cache.debug.lines[IP];
local err = b:gsub("(.-:)", "");
local output = "";
output = output .. (name and name .. ":" or "");
output = output .. (line and line .. ":" or "");
output = output .. b
--[[
output = ("%s (Instruction=%s)"):format(output,
lua_opcode_names[select(2,debug.getlocal(loop,1, 1)).opcode+1])
--]]
error(output, 0);
end
end
end
return debugging, func;
end
return {
load_bytecode = function(bytecode,env)
local cache = decode_bytecode(bytecode);
local _, func = create_wrapper(cache);
return func;
end;
-- Utilities (Debug, Introspection, Testing, etc)
utils = {
decode_bytecode = decode_bytecode;
create_wrapper = create_wrapper;
debug_bytecode = function(bytecode)
local cache = decode_bytecode(bytecode)
return create_wrapper(cache);
end;
};
}
end
LuaP = function()
--[[--------------------------------------------------------------------
lopcodes.lua
Lua 5 virtual machine opcodes in Lua
This file is part of Yueliang.
Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
The COPYRIGHT file describes the conditions
under which this software may be distributed.
See the ChangeLog for more information.
----------------------------------------------------------------------]]
--[[--------------------------------------------------------------------
-- Notes:
-- * an Instruction is a table with OP, A, B, C, Bx elements; this
-- makes the code easy to follow and should allow instruction handling
-- to work with doubles and ints
-- * WARNING luaP:Instruction outputs instructions encoded in little-
-- endian form and field size and positions are hard-coded
--
-- Not implemented:
-- *
--
-- Added:
-- * luaP:CREATE_Inst(c): create an inst from a number (for OP_SETLIST)
-- * luaP:Instruction(i): convert field elements to a 4-char string
-- * luaP:DecodeInst(x): convert 4-char string into field elements
--
-- Changed in 5.1.x:
-- * POS_OP added, instruction field positions changed
-- * some symbol names may have changed, e.g. LUAI_BITSINT
-- * new operators for RK indices: BITRK, ISK(x), INDEXK(r), RKASK(x)
-- * OP_MOD, OP_LEN is new
-- * OP_TEST is now OP_TESTSET, OP_TEST is new
-- * OP_FORLOOP, OP_TFORLOOP adjusted, OP_FORPREP is new
-- * OP_TFORPREP deleted
-- * OP_SETLIST and OP_SETLISTO merged and extended
-- * OP_VARARG is new
-- * many changes to implementation of OpMode data
----------------------------------------------------------------------]]
local luaP = {}
--[[
===========================================================================
We assume that instructions are unsigned numbers.
All instructions have an opcode in the first 6 bits.
Instructions can have the following fields:
'A' : 8 bits
'B' : 9 bits
'C' : 9 bits
'Bx' : 18 bits ('B' and 'C' together)
'sBx' : signed Bx
A signed argument is represented in excess K; that is, the number
value is the unsigned value minus K. K is exactly the maximum value
for that argument (so that -max is represented by 0, and +max is
represented by 2*max), which is half the maximum for the corresponding
unsigned argument.
===========================================================================
--]]
luaP.OpMode = { iABC = 0, iABx = 1, iAsBx = 2 } -- basic instruction format
------------------------------------------------------------------------
-- size and position of opcode arguments.
-- * WARNING size and position is hard-coded elsewhere in this script
------------------------------------------------------------------------
luaP.SIZE_C = 9
luaP.SIZE_B = 9
luaP.SIZE_Bx = luaP.SIZE_C + luaP.SIZE_B
luaP.SIZE_A = 8
luaP.SIZE_OP = 6
luaP.POS_OP = 0
luaP.POS_A = luaP.POS_OP + luaP.SIZE_OP
luaP.POS_C = luaP.POS_A + luaP.SIZE_A
luaP.POS_B = luaP.POS_C + luaP.SIZE_C
luaP.POS_Bx = luaP.POS_C
------------------------------------------------------------------------
-- limits for opcode arguments.
-- we use (signed) int to manipulate most arguments,
-- so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
------------------------------------------------------------------------
-- removed "#if SIZE_Bx < BITS_INT-1" test, assume this script is
-- running on a Lua VM with double or int as LUA_NUMBER
luaP.MAXARG_Bx = math.ldexp(1, luaP.SIZE_Bx) - 1
luaP.MAXARG_sBx = math.floor(luaP.MAXARG_Bx / 2) -- 'sBx' is signed
luaP.MAXARG_A = math.ldexp(1, luaP.SIZE_A) - 1
luaP.MAXARG_B = math.ldexp(1, luaP.SIZE_B) - 1
luaP.MAXARG_C = math.ldexp(1, luaP.SIZE_C) - 1
-- creates a mask with 'n' 1 bits at position 'p'
-- MASK1(n,p) deleted, not required
-- creates a mask with 'n' 0 bits at position 'p'
-- MASK0(n,p) deleted, not required
--[[--------------------------------------------------------------------
Visual representation for reference:
31 | | | 0 bit position
+-----+-----+-----+----------+
| B | C | A | Opcode | iABC format
+-----+-----+-----+----------+
- 9 - 9 - 8 - 6 - field sizes
+-----+-----+-----+----------+
| [s]Bx | A | Opcode | iABx | iAsBx format
+-----+-----+-----+----------+
----------------------------------------------------------------------]]
------------------------------------------------------------------------
-- the following macros help to manipulate instructions
-- * changed to a table object representation, very clean compared to
-- the [nightmare] alternatives of using a number or a string
-- * Bx is a separate element from B and C, since there is never a need
-- to split Bx in the parser or code generator
------------------------------------------------------------------------
-- these accept or return opcodes in the form of string names
function luaP:GET_OPCODE(i) return self.ROpCode[i.OP] end
function luaP:SET_OPCODE(i, o) i.OP = self.OpCode[o] end
function luaP:GETARG_A(i) return i.A end
function luaP:SETARG_A(i, u) i.A = u end
function luaP:GETARG_B(i) return i.B end
function luaP:SETARG_B(i, b) i.B = b end
function luaP:GETARG_C(i) return i.C end
function luaP:SETARG_C(i, b) i.C = b end
function luaP:GETARG_Bx(i) return i.Bx end
function luaP:SETARG_Bx(i, b) i.Bx = b end
function luaP:GETARG_sBx(i) return i.Bx - self.MAXARG_sBx end
function luaP:SETARG_sBx(i, b) i.Bx = b + self.MAXARG_sBx end
function luaP:CREATE_ABC(o,a,b,c)
return {OP = self.OpCode[o], A = a, B = b, C = c}
end
function luaP:CREATE_ABx(o,a,bc)
return {OP = self.OpCode[o], A = a, Bx = bc}
end
------------------------------------------------------------------------
-- create an instruction from a number (for OP_SETLIST)
------------------------------------------------------------------------
function luaP:CREATE_Inst(c)
local o = c % 64
c = (c - o) / 64
local a = c % 256
c = (c - a) / 256
return self:CREATE_ABx(o, a, c)
end
------------------------------------------------------------------------
-- returns a 4-char string little-endian encoded form of an instruction
------------------------------------------------------------------------
function luaP:Instruction(i)
if i.Bx then
-- change to OP/A/B/C format
i.C = i.Bx % 512
i.B = (i.Bx - i.C) / 512
end
local I = i.A * 64 + i.OP
local c0 = I % 256
I = i.C * 64 + (I - c0) / 256 -- 6 bits of A left
local c1 = I % 256
I = i.B * 128 + (I - c1) / 256 -- 7 bits of C left
local c2 = I % 256
local c3 = (I - c2) / 256
return string.char(c0, c1, c2, c3)
end
------------------------------------------------------------------------
-- decodes a 4-char little-endian string into an instruction struct
------------------------------------------------------------------------
function luaP:DecodeInst(x)
local byte = string.byte
local i = {}
local I = byte(x, 1)
local op = I % 64
i.OP = op
I = byte(x, 2) * 4 + (I - op) / 64 -- 2 bits of c0 left
local a = I % 256
i.A = a
I = byte(x, 3) * 4 + (I - a) / 256 -- 2 bits of c1 left
local c = I % 512
i.C = c
i.B = byte(x, 4) * 2 + (I - c) / 512 -- 1 bits of c2 left
local opmode = self.OpMode[tonumber(string.sub(self.opmodes[op + 1], 7, 7))]
if opmode ~= "iABC" then
i.Bx = i.B * 512 + i.C
end
return i
end
------------------------------------------------------------------------
-- Macros to operate RK indices