-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathByteCode.cs
executable file
·365 lines (301 loc) · 10.3 KB
/
ByteCode.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
#define EMIT_DEBUG_OPS
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using MoonSharp.Interpreter.Debugging;
namespace MoonSharp.Interpreter.Execution.VM
{
internal class ByteCode : RefIdObject
{
public List<Instruction> Code = new List<Instruction>();
public List<SourceRef> SourceRefs = new List<SourceRef>();
public Script Script { get; private set; }
private List<SourceRef> m_SourceRefStack = new List<SourceRef>();
private SourceRef m_CurrentSourceRef = null;
internal LoopTracker LoopTracker = new LoopTracker();
public Stack<int> NilChainTargets = new Stack<int>();
public ByteCode(Script script)
{
Script = script;
}
public IDisposable EnterSource(SourceRef sref)
{
return new SourceCodeStackGuard(sref, this);
}
private class SourceCodeStackGuard : IDisposable
{
ByteCode m_Bc;
public SourceCodeStackGuard(SourceRef sref, ByteCode bc)
{
m_Bc = bc;
m_Bc.PushSourceRef(sref);
}
public void Dispose()
{
m_Bc.PopSourceRef();
}
}
public void PushSourceRef(SourceRef sref)
{
m_SourceRefStack.Add(sref);
m_CurrentSourceRef = sref;
}
public void PopSourceRef()
{
m_SourceRefStack.RemoveAt(m_SourceRefStack.Count - 1);
m_CurrentSourceRef = (m_SourceRefStack.Count > 0) ? m_SourceRefStack[m_SourceRefStack.Count - 1] : null;
}
public string Dump()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Code.Count; i++)
{
if (Code[i].OpCode == OpCode.Debug)
sb.AppendFormat(" {0}\n", Code[i]);
else
sb.AppendFormat("{0:X8} {1}\n", i, Code[i]);
}
return sb.ToString();
}
public int GetJumpPointForNextInstruction()
{
return Code.Count;
}
public int GetJumpPointForLastInstruction()
{
return Code.Count - 1;
}
private int AppendInstruction(Instruction c)
{
Code.Add(c);
SourceRefs.Add(m_CurrentSourceRef);
return Code.Count - 1;
}
public void SetNumVal(int instruction, int val)
{
var ins = Code[instruction];
ins.NumVal = val;
Code[instruction] = ins;
}
public int Emit_Nop(string comment)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Nop, String = comment });
}
public int Emit_Invalid(string type)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Invalid, String = type });
}
public int Emit_Pop(int num = 1)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Pop, NumVal = num });
}
public void Emit_Call(int argCount, string debugName)
{
AppendInstruction(new Instruction() { OpCode = OpCode.Call, NumVal = argCount, String = debugName });
}
public void Emit_ThisCall(int argCount, string debugName)
{
AppendInstruction(new Instruction() { OpCode = OpCode.ThisCall, NumVal = argCount, String = debugName });
}
public int Emit_Literal(DynValue value)
{
switch (value.Type)
{
case DataType.Nil:
return AppendInstruction(new Instruction() {OpCode = OpCode.PushNil});
case DataType.Boolean:
if (value.Boolean)
return AppendInstruction(new Instruction() {OpCode = OpCode.PushTrue});
else
return AppendInstruction(new Instruction() {OpCode = OpCode.PushFalse});
case DataType.Number:
return AppendInstruction(new Instruction() {OpCode = OpCode.PushNumber, Number = value.Number});
case DataType.String:
return AppendInstruction(new Instruction() {OpCode = OpCode.PushString, String = value.String});
}
throw new InvalidOperationException(value.Type.ToString());
}
public int Emit_StrFormat(int argCount)
{
return AppendInstruction(new Instruction() {OpCode = OpCode.StrFormat, NumVal = argCount});
}
public int Emit_Jump(OpCode jumpOpCode, int idx, int optPar = 0)
{
return AppendInstruction(new Instruction() { OpCode = jumpOpCode, NumVal = idx, NumVal2 = optPar });
}
public int Emit_MkTuple(int cnt)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.MkTuple, NumVal = cnt });
}
public int Emit_Operator(OpCode opcode)
{
var i = AppendInstruction(new Instruction() { OpCode = opcode });
if (opcode == OpCode.LessEq)
AppendInstruction(new Instruction() { OpCode = OpCode.CNot });
if (opcode == OpCode.Eq || opcode == OpCode.Less)
AppendInstruction(new Instruction() { OpCode = OpCode.ToBool });
return i;
}
[Conditional("EMIT_DEBUG_OPS")]
public void Emit_Debug(string str)
{
AppendInstruction(new Instruction() { OpCode = OpCode.Debug, String = str.Substring(0, Math.Min(32, str.Length)) });
}
public int Emit_Enter(RuntimeScopeBlock runtimeScopeBlock)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Clean, NumVal = runtimeScopeBlock.From, NumVal2 = runtimeScopeBlock.ToInclusive });
}
public int Emit_Leave(RuntimeScopeBlock runtimeScopeBlock)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Clean, NumVal = runtimeScopeBlock.From, NumVal2 = runtimeScopeBlock.To });
}
public int Emit_Exit(RuntimeScopeBlock runtimeScopeBlock)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Clean, NumVal = runtimeScopeBlock.From, NumVal2 = runtimeScopeBlock.ToInclusive });
}
public int Emit_Clean(RuntimeScopeBlock runtimeScopeBlock)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Clean, NumVal = runtimeScopeBlock.To + 1, NumVal2 = runtimeScopeBlock.ToInclusive });
}
public int Emit_CloseUp(SymbolRef sym)
{
if (sym.Type != SymbolRefType.Local)
throw new InternalErrorException("Can only emit CloseUp for locals");
return AppendInstruction(new Instruction() { OpCode = OpCode.CloseUp, NumVal = sym.i_Index });
}
public int Emit_Closure(SymbolRef[] symbols, int jmpnum)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Closure, SymbolList = symbols, NumVal = jmpnum });
}
public int Emit_Args(params SymbolRef[] symbols)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Args, SymbolList = symbols });
}
public int Emit_Ret(int retvals)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Ret, NumVal = retvals });
}
public int Emit_Incr(int i)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Incr, NumVal = i });
}
public int Emit_NewTable(bool shared)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.NewTable, NumVal = shared ? 1 : 0 });
}
public int Emit_IterPrep()
{
return AppendInstruction(new Instruction() { OpCode = OpCode.IterPrep });
}
public int Emit_ExpTuple(int stackOffset)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.ExpTuple, NumVal = stackOffset });
}
public int Emit_IterUpd()
{
return AppendInstruction(new Instruction() { OpCode = OpCode.IterUpd });
}
public int Emit_Meta(string funcName, OpCodeMetadataType metaType)
{
return AppendInstruction(new Instruction()
{
OpCode = OpCode.Meta,
String = funcName,
NumVal2 = (int)metaType
});
}
public int Emit_BeginFn(RuntimeScopeFrame stackFrame)
{
return AppendInstruction(new Instruction()
{
OpCode = OpCode.BeginFn,
SymbolList = stackFrame.DebugSymbols.ToArray(),
NumVal = stackFrame.Count,
NumVal2 = stackFrame.ToFirstBlock,
});
}
public int Emit_Scalar()
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Scalar });
}
public int Emit_Load(SymbolRef sym)
{
switch (sym.Type)
{
case SymbolRefType.Global:
Emit_Load(sym.i_Env);
AppendInstruction(new Instruction() { OpCode = OpCode.Index, String = sym.i_Name });
return 2;
case SymbolRefType.Local:
AppendInstruction(new Instruction() { OpCode = OpCode.Local, NumVal = sym.i_Index });
return 1;
case SymbolRefType.Upvalue:
AppendInstruction(new Instruction() { OpCode = OpCode.Upvalue, NumVal = sym.i_Index });
return 1;
default:
throw new InternalErrorException("Unexpected symbol type : {0}", sym);
}
}
public int Emit_Store(SymbolRef sym, int stackofs, int tupleidx)
{
switch (sym.Type)
{
case SymbolRefType.Global:
Emit_Load(sym.i_Env);
AppendInstruction(new Instruction() { OpCode = OpCode.IndexSet, NumVal = stackofs, NumVal2 = tupleidx, String = sym.i_Name });
return 2;
case SymbolRefType.Local:
AppendInstruction(new Instruction() { OpCode = OpCode.StoreLcl, Symbol = sym, NumVal = stackofs, NumVal2 = tupleidx });
return 1;
case SymbolRefType.Upvalue:
AppendInstruction(new Instruction() { OpCode = OpCode.StoreUpv, Symbol = sym, NumVal = stackofs, NumVal2 = tupleidx });
return 1;
default:
throw new InternalErrorException("Unexpected symbol type : {0}", sym);
}
}
public int Emit_TblInitN()
{
return AppendInstruction(new Instruction() { OpCode = OpCode.TblInitN });
}
public int Emit_TblInitI(bool lastpos)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.TblInitI, NumVal = lastpos ? 1 : 0 });
}
public int Emit_Index(string index = null, bool isNameIndex = false, bool isExpList = false)
{
OpCode o;
if (isNameIndex) o = OpCode.IndexN;
else if (isExpList) o = OpCode.IndexL;
else o = OpCode.Index;
return AppendInstruction(new Instruction() { OpCode = o, String = index });
}
public int Emit_IndexSet(int stackofs, int tupleidx, string index = null, bool isNameIndex = false, bool isExpList = false)
{
OpCode o;
if (isNameIndex) o = OpCode.IndexSetN;
else if (isExpList) o = OpCode.IndexSetL;
else o = OpCode.IndexSet;
return AppendInstruction(new Instruction() { OpCode = o, NumVal = stackofs, NumVal2 = tupleidx, String = index });
}
public int Emit_Copy(int numval)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Copy, NumVal = numval });
}
public int Emit_CopyValue(int numval, int tupleidx)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Copy, NumVal = numval, NumVal2 = tupleidx });
}
public int Emit_Swap(int p1, int p2)
{
return AppendInstruction(new Instruction() { OpCode = OpCode.Swap, NumVal = p1, NumVal2 = p2 });
}
public int Emit_JLclInit(SymbolRef sym, int target)
{
if(sym.Type != SymbolRefType.Local) throw new InternalErrorException("Unexpected symbol type : {0}", sym);
return AppendInstruction(new Instruction() { OpCode = OpCode.JLclInit, NumVal = target, NumVal2 = sym.Index });
}
}
}