-
Notifications
You must be signed in to change notification settings - Fork 34
/
LuaInstruction.cs
274 lines (238 loc) · 9.09 KB
/
LuaInstruction.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
using LuaToolkit.Models;
using System.Collections.Generic;
namespace LuaToolkit.Core
{
public class LuaInstruction
{
private const int HalfMax18Bit = 2 << 16; // == 2^16 -1 == 131071
public int Data
{
get;
private set;
}
public LuaOpcode OpCode
{
get;
private set;
}
public int A
{
get;
set;
}
public int B
{
get;
set;
}
public int C
{
get;
set;
}
public int Bx
{
get { return ((B << 9) & 0xFFE00 | C) & 0x3FFFF; }
set
{
B = value >> 9; // TODO: verift that this gets rid of the first 9 bits?
C = (value & ~0xFFE00); // TODO: verify that this gets rid of the last 9 bits?
}
}
public int sBx
{
get { return Bx - (HalfMax18Bit-1); }
set { Bx = value + (HalfMax18Bit - 1); }
}
public bool HasBx
{
get;
private set;
}
public bool Signed
{
get;
private set;
}
public List<int> OffsetVariables(int offset)
{
List<int> originals = new List<int>();
// offsets the variables (if any)
bool NoC = false;
bool NoB = false;
bool NoA = false;
// TODO: Complete list
switch (this.OpCode)
{
case LuaOpcode.JMP:
case LuaOpcode.CLOSE:
case LuaOpcode.GETUPVAL:
case LuaOpcode.SETUPVAL:
NoA = true;
break;
}
switch (this.OpCode)
{
case LuaOpcode.JMP:
case LuaOpcode.CLOSE:
case LuaOpcode.GETUPVAL:
case LuaOpcode.SETUPVAL:
case LuaOpcode.CALL:
case LuaOpcode.TAILCALL:
NoB = true;
break;
}
switch (this.OpCode)
{
case LuaOpcode.MOVE:
case LuaOpcode.LOADNIL:
case LuaOpcode.GETUPVAL:
case LuaOpcode.SETUPVAL:
case LuaOpcode.UNM:
case LuaOpcode.NOT:
case LuaOpcode.LEN:
case LuaOpcode.RETURN:
case LuaOpcode.VARARG:
case LuaOpcode.CALL:
case LuaOpcode.TAILCALL:
NoC = true;
break;
}
// TODO: check if value is NOT constant
if (!NoA && this.A < 256)
{
originals.Add(this.A);
this.A += offset;
}
if (!NoB && this.B < 256)
{
this.B += offset;
originals.Add(this.B);
}
if (!NoC && this.C < 256)
{
this.C += offset;
originals.Add(this.C);
}
return originals;
}
public LuaInstruction(int data)
{
Data = data;
OpCode = (LuaOpcode)(data & 0x3F);
A = (data >> 6) & 0xFF;
B = (data >> 23) & 0x1FF;
C = (data >> 14) & 0x1FF;
SetVars();
}
public LuaInstruction(LuaOpcode opcode)
{
this.OpCode = opcode;
SetVars();
}
private void SetVars()
{
switch (this.OpCode)
{
case LuaOpcode.JMP:
case LuaOpcode.FORLOOP:
case LuaOpcode.FORPREP:
Signed = true;
goto case LuaOpcode.LOADK;
case LuaOpcode.LOADK:
case LuaOpcode.GETGLOBAL:
case LuaOpcode.SETGLOBAL:
case LuaOpcode.CLOSE:
HasBx = true;
break;
default:
HasBx = false;
Signed = false;
break;
}
}
public override string ToString()
{
// TODO: cleanup
if (this.OpCode == LuaOpcode.CLOSE)
return $"{this.OpCode} {this.A}";
else if (this.OpCode == LuaOpcode.CLOSURE || this.OpCode == LuaOpcode.GETGLOBAL
|| this.OpCode == LuaOpcode.SETGLOBAL || this.OpCode == LuaOpcode.LOADK)
return $"{this.OpCode} {this.A} {this.Bx}";
if(this.OpCode == LuaOpcode.TFORLOOP || this.OpCode == LuaOpcode.TEST)
return $"{this.OpCode} {this.A} {this.C}";
if (this.OpCode == LuaOpcode.JMP)
return $"{this.OpCode} {this.sBx}";
else if (this.Signed)
return $"{this.OpCode} {this.A} {this.sBx}";
bool NoC = false;
switch(this.OpCode)
{
case LuaOpcode.MOVE:
case LuaOpcode.LOADNIL:
case LuaOpcode.GETUPVAL:
case LuaOpcode.SETUPVAL:
case LuaOpcode.UNM:
case LuaOpcode.NOT:
case LuaOpcode.LEN:
case LuaOpcode.RETURN:
case LuaOpcode.VARARG:
NoC = true;
break;
}
if (this.HasBx)
return $"{this.OpCode} {this.A} {this.Bx}";
else
if(NoC)
return $"{this.OpCode} {this.A} {this.B}";
else
return $"{this.OpCode} {this.A} {this.B} {this.C}";
}
}
// NOTE: We can re-use this to calculate the performance impact when we add
// some kind of weight to each instruction and do the math.
//public static class LuaInstructions
//{
// public static LuaInstruction[] Table =
// {
// new LuaInstruction { Opcode=LuaOpcode.MOVE, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.LOADK, Type=OpcodeType.ABx },
// new LuaInstruction { Opcode=LuaOpcode.LOADBOOL, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.LOADNIL, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.GETUPVAL, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.GETGLOBAL, Type=OpcodeType.ABx },
// new LuaInstruction { Opcode=LuaOpcode.GETTABLE, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.SETGLOBAL, Type=OpcodeType.ABx },
// new LuaInstruction { Opcode=LuaOpcode.SETUPVAL, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.SETTABLE, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.NEWTABLE, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.SELF, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.ADD, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.SUB, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.MUL, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.DIV, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.MOD, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.POW, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.UNM, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.NOT, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.LEN, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.CONCAT, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.JMP, Type=OpcodeType.AsBx },
// new LuaInstruction { Opcode=LuaOpcode.EQ, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.LT, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.LE, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.TEST, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.TESTSET, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.CALL, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.TAILCALL, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.RETURN, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.FORLOOP, Type=OpcodeType.AsBx },
// new LuaInstruction { Opcode=LuaOpcode.FORPREP, Type=OpcodeType.AsBx },
// new LuaInstruction { Opcode=LuaOpcode.TFORLOOP, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.SETLIST, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.CLOSE, Type=OpcodeType.ABC },
// new LuaInstruction { Opcode=LuaOpcode.CLOSURE, Type=OpcodeType.ABx },
// new LuaInstruction { Opcode=LuaOpcode.VARARG, Type=OpcodeType.ABC },
// };
//}
}