forked from JariKCoding/CoDLuaDecompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaDisassembler.cs
651 lines (623 loc) · 35.5 KB
/
LuaDisassembler.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSLuaDecompiler.LuaFileTypes;
using DSLuaDecompiler.LuaFileTypes.Structures;
using luadec.IR;
using Constant = DSLuaDecompiler.LuaFileTypes.Structures.Constant;
using Function = DSLuaDecompiler.LuaFileTypes.Structures.Function;
namespace luadec
{
class LuaDisassembler
{
public static IR.SymbolTable SymbolTable = new IR.SymbolTable();
private static IR.Expression RKIR(Function fun, uint val)
{
if (val < 250)
{
return new IR.IdentifierReference(SymbolTable.GetRegister(val));
}
else
{
return ToConstantIR(fun.Constants[(int) (val - 250)]);
}
}
private static IR.Expression RKIRHKS(Function fun, uint val, bool szero)
{
if (val >= 0 && !szero)
{
return new IR.IdentifierReference(SymbolTable.GetRegister((uint)val));
}
else if (szero)
{
return ToConstantIR(fun.Constants[(int) val]);
}
else
{
return ToConstantIR(fun.Constants[(int) -val]);
}
}
private static IR.IdentifierReference Register(uint reg)
{
return new IR.IdentifierReference(SymbolTable.GetRegister((uint)reg));
}
private static IR.Constant ToConstantIR(Constant con)
{
if (con.Type == ConstantType.TNumber)
return new IR.Constant(con.NumberValue);
if (con.Type == ConstantType.TString)
return new IR.Constant(con.StringValue);
if (con.Type == ConstantType.TBoolean)
return new IR.Constant(con.BoolValue);
if (con.Type == ConstantType.THash)
return new IR.Constant(con.HashValue);
return new IR.Constant(IR.Constant.ConstantType.ConstNil);
}
private static void CheckLocal(IR.Assignment a, Function fun, int index)
{
a.LocalAssignments = fun.LocalsAt(index + 1);
}
public static void GenerateIRHKS(IR.Function irfun, Function fun)
{
// First register closures for all the children
for (int i = 0; i < fun.ChildFunctions.Count; i++)
{
var cfun = new IR.Function();
// Upval count needs to be set for child functions for analysis to be correct
cfun.UpvalCount = fun.ChildFunctions[i].Upvalues.Count;
irfun.AddClosure(cfun);
}
SymbolTable.BeginScope();
var parameters = new List<IR.Identifier>();
for (uint i = 0; i < fun.ParameterCount; i++)
{
parameters.Add(SymbolTable.GetRegister(i));
}
irfun.SetParameters(parameters);
for (int i = 0; i < fun.Instructions.Count * 4; i += 4)
{
var instruction = fun.Instructions[i / 4];
//uint opcode = instruction & 0x3F;
// Uhhh thanks again hork
var opcode = instruction.OpCode;
var a = instruction.A;
var c = instruction.C;
var b = instruction.B;
var szero = instruction.ExtraCBit;
var bx = instruction.Bx;
var sbx = instruction.SBx;
uint addr;
var pc = i / 4;
List<IR.Expression> args = null;
List<IR.IdentifierReference> rets = null;
List<IR.IInstruction> instructions = new List<IR.IInstruction>();
IR.Assignment assn;
switch (opcode)
{
case LuaOpCode.HKS_OPCODE_MOVE:
assn = new IR.Assignment(SymbolTable.GetRegister(a), Register((uint)b));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_LOADK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), ToConstantIR(fun.Constants[(int) bx]));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_LOADBOOL:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.Constant(b == 1));
assn.NilAssignmentReg = a;
CheckLocal(assn, fun, pc);
instructions.Add(assn);
if (c > 0)
{
instructions.Add(new IR.Jump(irfun.GetLabel((uint)((i / 4) + 2))));
}
break;
case LuaOpCode.HKS_OPCODE_LOADNIL:
var nlist = new List<IR.IdentifierReference>();
for (int arg = (int)a; arg <= b; arg++)
{
nlist.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)arg)));
}
assn = new IR.Assignment(nlist, new IR.Constant(IR.Constant.ConstantType.ConstNil));
assn.NilAssignmentReg = a;
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_GETUPVAL:
if (b >= irfun.UpvalueBindings.Count)
{
//throw new Exception("Reference to unbound upvalue");
}
Identifier up = irfun.UpvalueBindings[(int) b];
up.IsClosureBound = true;
instructions.Add(new IR.Assignment(SymbolTable.GetRegister(a), new IR.IdentifierReference(up)));
break;
case LuaOpCode.HKS_OPCODE_SETUPVAL:
case LuaOpCode.HKS_OPCODE_SETUPVAL_R1:
up = SymbolTable.GetUpvalue((uint)b);
if (fun.Upvalues.Any() && !up.UpvalueResolved)
{
up.Name = fun.Upvalues[(int) b].Name;
up.UpvalueResolved = true;
}
instructions.Add(new IR.Assignment(up, new IR.IdentifierReference(SymbolTable.GetRegister(a))));
break;
case LuaOpCode.HKS_OPCODE_GETGLOBAL_MEM:
case LuaOpCode.HKS_OPCODE_GETGLOBAL:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.IdentifierReference(SymbolTable.GetGlobal(fun.Constants[(int) bx].ToString())));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_GETTABLE_S:
case LuaOpCode.HKS_OPCODE_GETTABLE:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.IdentifierReference(SymbolTable.GetRegister((uint)b), RKIRHKS(fun, c, szero)));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SETGLOBAL:
instructions.Add(new IR.Assignment(SymbolTable.GetGlobal(fun.Constants[(int) bx].ToString()), new IR.IdentifierReference(SymbolTable.GetRegister(a))));
break;
case LuaOpCode.HKS_OPCODE_NEWTABLE:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.InitializerList(new List<IR.Expression>()));
assn.VarargAssignmentReg = a;
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SELF:
instructions.Add(new IR.Assignment(SymbolTable.GetRegister(a + 1), Register((uint)b)));
instructions.Add(new IR.Assignment(SymbolTable.GetRegister(a), new IR.IdentifierReference(SymbolTable.GetRegister((uint)b), RKIRHKS(fun, c, szero))));
break;
case LuaOpCode.HKS_OPCODE_ADD:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpAdd));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_ADD_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpAdd));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SUB:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpSub));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SUB_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpSub));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_MUL:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpMul));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_MUL_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpMul));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_DIV:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpDiv));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_DIV_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpDiv));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_MOD:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpMod));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_MOD_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpMod));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_POW:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpPow));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_POW_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpPow));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_UNM:
assn = new IR.Assignment(SymbolTable.GetRegister(a),
new IR.UnaryOp(new IR.IdentifierReference(SymbolTable.GetRegister((uint)b)), IR.UnaryOp.OperationType.OpNegate));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_NOT:
case LuaOpCode.HKS_OPCODE_NOT_R1:
assn = new IR.Assignment(SymbolTable.GetRegister(a),
new IR.UnaryOp(new IR.IdentifierReference(SymbolTable.GetRegister((uint)b)), IR.UnaryOp.OperationType.OpNot));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_LEN:
assn = new IR.Assignment(SymbolTable.GetRegister(a),
new IR.UnaryOp(new IR.IdentifierReference(SymbolTable.GetRegister((uint)b)), IR.UnaryOp.OperationType.OpLength));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SHIFT_LEFT:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpShiftLeft));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SHIFT_LEFT_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpShiftLeft));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SHIFT_RIGHT:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpShiftRight));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_SHIFT_RIGHT_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpShiftRight));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_BITWISE_AND:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpBAnd));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_BITWISE_AND_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpBAnd));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_BITWISE_OR:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), IR.BinOp.OperationType.OpBOr));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_BITWISE_OR_BK:
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), IR.BinOp.OperationType.OpBOr));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_CONCAT:
args = new List<IR.Expression>();
for (int arg = (int)b; arg <= c; arg++)
{
args.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)arg)));
}
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.Concat(args));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_JMP:
instructions.Add(new IR.Jump(irfun.GetLabel((uint) (pc + 1 + sbx))));
break;
case LuaOpCode.HKS_OPCODE_EQ:
var operation = IR.BinOp.OperationType.OpEqual;
if (a == 1)
operation = IR.BinOp.OperationType.OpNotEqual;
instructions.Add(new IR.Jump(irfun.GetLabel((uint)(pc + 2)), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), operation)));
break;
case LuaOpCode.HKS_OPCODE_EQ_BK:
operation = IR.BinOp.OperationType.OpEqual;
if (a == 1)
operation = IR.BinOp.OperationType.OpNotEqual;
instructions.Add(new IR.Jump(irfun.GetLabel((uint)(pc + 2)), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), operation)));
break;
case LuaOpCode.HKS_OPCODE_LT:
operation = IR.BinOp.OperationType.OpLessThan;
if (a == 1)
operation = IR.BinOp.OperationType.OpGreaterEqual;
instructions.Add(new IR.Jump(irfun.GetLabel((uint)(pc + 2)), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), operation)));
break;
case LuaOpCode.HKS_OPCODE_LT_BK:
operation = IR.BinOp.OperationType.OpLessThan;
if (a == 1)
operation = IR.BinOp.OperationType.OpGreaterEqual;
instructions.Add(new IR.Jump(irfun.GetLabel((uint)(pc + 2)), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), operation)));
break;
case LuaOpCode.HKS_OPCODE_LE:
operation = IR.BinOp.OperationType.OpLessEqual;
if (a == 1)
operation = IR.BinOp.OperationType.OpGreaterThan;
instructions.Add(new IR.Jump(irfun.GetLabel((uint)(pc + 2)), new IR.BinOp(Register((uint)b), RKIRHKS(fun, c, szero), operation)));
break;
case LuaOpCode.HKS_OPCODE_LE_BK:
operation = IR.BinOp.OperationType.OpLessEqual;
if (a == 1)
operation = IR.BinOp.OperationType.OpGreaterThan;
instructions.Add(new IR.Jump(irfun.GetLabel((uint)(pc + 2)), new IR.BinOp(ToConstantIR(fun.Constants[(int) b]), Register((uint)c), operation)));
break;
case LuaOpCode.HKS_OPCODE_TEST:
case LuaOpCode.HKS_OPCODE_TEST_R1:
// This op is weird
if (c == 0)
{
instructions.Add(new IR.Jump(irfun.GetLabel((uint)((i / 4) + 2)), Register((uint)a)));
}
else
{
instructions.Add(new IR.Jump(irfun.GetLabel((uint)((i / 4) + 2)), new IR.UnaryOp(Register((uint)a), IR.UnaryOp.OperationType.OpNot)));
}
break;
case LuaOpCode.HKS_OPCODE_TESTSET:
// This op is weird
if (c == 0)
{
instructions.Add(new IR.Jump(irfun.GetLabel((uint)((i / 4) + 2)), new IR.BinOp(RKIR(fun, b), new IR.Constant(0.0), IR.BinOp.OperationType.OpNotEqual)));
}
else
{
instructions.Add(new IR.Jump(irfun.GetLabel((uint)((i / 4) + 2)), new IR.BinOp(RKIR(fun, b), new IR.Constant(0.0), IR.BinOp.OperationType.OpEqual)));
}
instructions.Add(new IR.Assignment(SymbolTable.GetRegister(a), new IR.IdentifierReference(SymbolTable.GetRegister(b))));
break;
case LuaOpCode.HKS_OPCODE_SETTABLE:
case LuaOpCode.HKS_OPCODE_SETTABLE_S:
instructions.Add(new IR.Assignment(new IR.IdentifierReference(SymbolTable.GetRegister(a), Register(b)), RKIRHKS(fun, c, szero)));
break;
case LuaOpCode.HKS_OPCODE_TAILCALL:
case LuaOpCode.HKS_OPCODE_TAILCALL_I:
case LuaOpCode.HKS_OPCODE_TAILCALL_I_R1:
args = new List<IR.Expression>();
for (int arg = (int)a + 1; arg < a + b; arg++)
{
args.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)arg)));
}
var funCall = new IR.FunctionCall(new IR.IdentifierReference(SymbolTable.GetRegister(a)), args);
funCall.IsIndeterminantArgumentCount = (b == 0);
funCall.BeginArg = a + 1;
instructions.Add(new IR.Return(funCall));
break;
case LuaOpCode.HKS_OPCODE_SETTABLE_S_BK:
instructions.Add(new IR.Assignment(new IR.IdentifierReference(SymbolTable.GetRegister(a), ToConstantIR(fun.Constants[(int) b])), RKIRHKS(fun, c, szero)));
break;
case LuaOpCode.HKS_OPCODE_CALL_I:
case LuaOpCode.HKS_OPCODE_CALL_I_R1:
case LuaOpCode.HKS_OPCODE_CALL:
args = new List<IR.Expression>();
rets = new List<IR.IdentifierReference>();
for (int arg = (int)a + 1; arg < a + b; arg++)
{
args.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)arg)));
}
for (int r = (int)a + 1; r < a + c; r++)
{
rets.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)r - 1)));
}
if (c == 0)
{
rets.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)a)));
}
funCall = new IR.FunctionCall(new IR.IdentifierReference(SymbolTable.GetRegister(a)), args);
funCall.IsIndeterminantArgumentCount = (b == 0);
funCall.IsIndeterminantReturnCount = (c == 0);
funCall.BeginArg = a + 1;
assn = new IR.Assignment(rets, funCall);
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_RETURN:
args = new List<IR.Expression>();
if (b != 0)
{
for (int arg = (int)a; arg < a + b - 1; arg++)
{
args.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)arg)));
}
}
var ret = new IR.Return(args);
if (b == 0)
{
ret.BeginRet = a;
ret.IsIndeterminantReturnCount = true;
}
instructions.Add(ret);
break;
case LuaOpCode.HKS_OPCODE_FORLOOP:
instructions.Add(new IR.Assignment(new IR.IdentifierReference(SymbolTable.GetRegister(a)), new IR.BinOp(new IR.IdentifierReference(SymbolTable.GetRegister(a)),
new IR.IdentifierReference(SymbolTable.GetRegister(a + 2)), IR.BinOp.OperationType.OpAdd)));
var jmp = new IR.Jump(irfun.GetLabel((uint) (pc + 1 + sbx)), new IR.BinOp(new IR.IdentifierReference(SymbolTable.GetRegister(a)),
new IR.IdentifierReference(SymbolTable.GetRegister(a + 1)), IR.BinOp.OperationType.OpLoopCompare));
var pta = new IR.Assignment(SymbolTable.GetRegister(a + 3), Register((uint)a));
pta.PropogateAlways = true;
jmp.PostTakenAssignment = pta;
instructions.Add(jmp);
break;
case LuaOpCode.HKS_OPCODE_TFORLOOP:
args = new List<IR.Expression>();
rets = new List<IR.IdentifierReference>();
args.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)a + 1)));
args.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)a + 2)));
if (c == 0)
{
rets.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)a + 3)));
}
else
{
for (int r = (int)a + 3; r <= a + c + 2; r++)
{
rets.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)r)));
}
}
var fcall = new IR.FunctionCall(new IR.IdentifierReference(SymbolTable.GetRegister(a)), args);
fcall.IsIndeterminantReturnCount = (c == 0);
assn = new IR.Assignment(rets, fcall);
CheckLocal(assn, fun, pc);
instructions.Add(assn);
instructions.Add(new IR.Jump(irfun.GetLabel((uint)((i / 4) + 2)), new IR.BinOp(Register((uint)a + 3), new IR.Constant(IR.Constant.ConstantType.ConstNil), IR.BinOp.OperationType.OpEqual)));
instructions.Add(new IR.Assignment(SymbolTable.GetRegister(a + 2), new IR.IdentifierReference(SymbolTable.GetRegister(a + 3))));
break;
case LuaOpCode.HKS_OPCODE_FORPREP:
// The VM technically does a subtract, but we don't actually emit it since it simplifies things to map better to the high level Lua
//instructions.Add(new IR.Assignment(new IR.IdentifierReference(SymbolTable.GetRegister(a)), new IR.BinOp(new IR.IdentifierReference(SymbolTable.GetRegister(a)),
// new IR.IdentifierReference(SymbolTable.GetRegister(a + 2)), IR.BinOp.OperationType.OpSub)));
instructions.Add(new IR.Jump(irfun.GetLabel((uint) (pc + 1 + sbx))));
break;
case LuaOpCode.HKS_OPCODE_SETLIST:
if (b == 0)
{
if (c == 1)
{
assn = new IR.Assignment(SymbolTable.GetRegister(a), Register(a + 1));
assn.VarargAssignmentReg = a;
assn.IsIndeterminantVararg = true;
CheckLocal(assn, fun, pc);
instructions.Add(assn);
}
}
else
{
for (int j = 1; j <= b; j++)
{
assn = new IR.Assignment(new IR.IdentifierReference(SymbolTable.GetRegister(a), new IR.Constant((double)(c - 1) * 32 + j)),
new IR.IdentifierReference(SymbolTable.GetRegister(a + (uint)j)));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
}
}
break;
case LuaOpCode.HKS_OPCODE_CLOSURE:
instructions.Add(new IR.Assignment(SymbolTable.GetRegister(a), new IR.Closure(irfun.LookupClosure(bx))));
break;
case LuaOpCode.HKS_OPCODE_GETFIELD:
case LuaOpCode.HKS_OPCODE_GETFIELD_R1:
assn = new IR.Assignment(Register((uint)a), new IR.IdentifierReference(SymbolTable.GetRegister((uint)b), new IR.Constant(fun.Constants[(int) c].ToString())));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_DATA:
if (a != 0)
{
IR.Function closureFunc = null;
int index = pc;
while (index >= 0)
{
if (fun.Instructions[index].OpCode == LuaOpCode.HKS_OPCODE_CLOSURE)
{
closureFunc = irfun.LookupClosure(fun.Instructions[index].Bx);
break;
}
index--;
}
if (closureFunc == null)
{
continue;
}
if (a == 1)
{
closureFunc.UpvalueBindings.Add(SymbolTable.GetRegister(c));
}
else if (a == 2)
{
closureFunc.UpvalueBindings.Add(irfun.UpvalueBindings[(int) c]);
}
}
else
{
instructions.Add(new Data());
}
break;
case LuaOpCode.HKS_OPCODE_SETFIELD:
case LuaOpCode.HKS_OPCODE_SETFIELD_R1:
assn = new IR.Assignment(new IR.IdentifierReference(SymbolTable.GetRegister(a), new IR.Constant(fun.Constants[(int) b].ToString())), RKIRHKS(fun, c, szero));
CheckLocal(assn, fun, pc);
instructions.Add(assn);
break;
case LuaOpCode.HKS_OPCODE_VARARG:
var vargs = new List<IR.IdentifierReference>();
for (int arg = (int)a; arg <= a + b - 1; arg++)
{
vargs.Add(new IR.IdentifierReference(SymbolTable.GetRegister((uint)arg)));
}
if (b != 0)
{
assn = new IR.Assignment(vargs, new IR.IdentifierReference(SymbolTable.GetVarargs()));
}
else
{
assn = new IR.Assignment(SymbolTable.GetRegister(a), new IR.IdentifierReference(SymbolTable.GetVarargs()));
assn.IsIndeterminantVararg = true;
assn.VarargAssignmentReg = a;
}
CheckLocal(assn, fun, pc);
instructions.Add(assn);
irfun.IsVarargs = true;
break;
case LuaOpCode.HKS_OPCODE_CLOSE:
// LUA source : close all variables in the stack up to (>=) R(A)
// Let's ignore this for now, doesn't print anything and don't know if it affects SSA
instructions.Add(new Close());
break;
default:
Console.WriteLine($@"Missing op: {opcode} {a} {b} {c}");
instructions.Add(new IR.PlaceholderInstruction(($@"{opcode} {a} {b} {c}")));
break;
}
foreach (var inst in instructions)
{
inst.OpLocation = i / 4;
irfun.AddInstruction(inst);
}
}
irfun.ApplyLabels();
// Simple post-ir and idiom recognition analysis passes
irfun.ResolveVarargListAssignment(SymbolTable);
irfun.MergeMultiBoolAssignment();
irfun.EliminateRedundantAssignments();
irfun.MergeConditionalJumps();
irfun.MergeConditionalAssignments();
//irfun.PeepholeOptimize();
irfun.CheckControlFlowIntegrity();
irfun.RemoveUnusedLabels();
irfun.ClearDataInstructions();
// Control flow graph construction and SSA conversion
irfun.ConstructControlFlowGraph();
irfun.ResolveIndeterminantArguments(SymbolTable);
irfun.CompleteLua51Loops();
// Upval resolution
irfun.RegisterClosureUpvalues53(SymbolTable.GetAllRegistersInScope());
irfun.ConvertToSSA(SymbolTable.GetAllRegistersInScope());
// Data flow passes
irfun.EliminateDeadAssignments(true);
irfun.PerformExpressionPropogation();
irfun.DetectListInitializers();
// CFG passes
irfun.StructureCompoundConditionals();
irfun.DetectLoops();
irfun.DetectLoopConditionals();
irfun.DetectTwoWayConditionals();
irfun.SimplifyIfElseFollowChain();
irfun.EliminateDeadAssignments(true);
irfun.PerformExpressionPropogation();
irfun.VerifyLivenessNoInterference();
// Convert out of SSA and rename variables
irfun.DropSSADropSubscripts();
irfun.AnnotateLocalDeclarations();
irfun.RenameVariables();
irfun.Parenthesize();
irfun.AddEmptyLines();
irfun.SearchInlineClosures();
irfun.RemoveUnnecessaryReturns();
// Convert to AST
irfun.ConvertToAST(true);
// Now generate IR for all the child closures
for (int i = 0; i < fun.ChildFunctions.Count; i++)
{
GenerateIRHKS(irfun.LookupClosure((uint)i), fun.ChildFunctions[i]);
}
SymbolTable.EndScope();
}
}
}