-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOpCode.cs
102 lines (89 loc) · 5.18 KB
/
OpCode.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
namespace MoonSharp.Interpreter.Execution.VM
{
internal enum OpCode
{
// Meta-opcodes
Nop, // Does not perform any operation.
Debug, // Does not perform any operation. Used to help debugging.
// Stack ops and assignment
Pop, // Discards the topmost n elements from the v-stack.
Copy, // Copies the n-th value of the stack on the top
CopyValue, // Copies the n-th value of the stack on the top, fetching tupleidx from NumVal2
Swap, // Swaps two entries relative to the v-stack
PushNil, // Pushes nil to the v-stack
PushTrue, // Pushes true to the v-stack
PushFalse, // Pushes false to the v-stack
PushNumber, // Pushes a number to the v-stack
PushString, // Pushes a string to the v-stack
Closure, // Creates a closure on the top of the v-stack, using the symbols for upvalues and num-val for entry point of the function.
NewTable, // Creates a new empty table on the stack
TblInitN, // Initializes a table named entry
TblInitI, // Initializes a table positional entry
StoreLcl, Local,
StoreUpv, Upvalue,
IndexSet, Index,
IndexSetN, IndexN,
IndexSetL, IndexL,
// Stack-frame ops and calls
Clean, // Cleansup locals setting them as null
CloseUp, // Close a specific upvalue
Meta, // Injects function metadata used for reflection things (dumping, debugging)
BeginFn, // Adjusts for start of function, taking in parameters and allocating locals
Args, // Takes the arguments passed to a function and sets the appropriate symbols in the local scope
Call, // Calls the function specified on the specified element from the top of the v-stack. If the function is a MoonSharp function, it pushes its numeric value on the v-stack, then pushes the current PC onto the x-stack, enters the function closure and jumps to the function first instruction. If the function is a CLR function, it pops the function value from the v-stack, then invokes the function synchronously and finally pushes the result on the v-stack.
ThisCall, // Same as call, but the call is a ':' method invocation
Ret, // Pops the top n values of the v-stack. Then pops an X value from the v-stack. Then pops X values from the v-stack. Afterwards, it pushes the top n values popped in the first step, pops the top of the x-stack and jumps to that location.
// Jumps
Jump, // Jumps to the specified PC
Jf, // Pops the top of the v-stack and jumps to the specified location if it's false
Jt, // Pops the top of the v-stack and jumps to the specified location if it's true
JNil, // Jumps if the top of the stack is nil (pops stack)
JNilChk, // Jumps if the top of the stack is nil (does not pop stack)
JFor, // Peeks at the top, top-1 and top-2 values of the v-stack which it assumes to be numbers. Then if top-1 is less than zero, checks if top is <= top-2, otherwise it checks that top is >= top-2. Then if the condition is false, it jumps.
JtOrPop, // Peeks at the topmost value of the v-stack as a boolean. If true, it performs a jump, otherwise it removes the topmost value from the v-stack.
JfOrPop, // Peeks at the topmost value of the v-stack as a boolean. If false, it performs a jump, otherwise it removes the topmost value from the v-stack.
//
StrFormat, // Format using string.Format
// Operators
Concat, // Concatenation of the two topmost operands on the v-stack
LessEq, // Compare <= of the two topmost operands on the v-stack
Less, // Compare < of the two topmost operands on the v-stack
Eq, // Compare == of the two topmost operands on the v-stack
Add, // Addition of the two topmost operands on the v-stack
AddStr, // Addition of the two topmost operands on the v-stack, will concat strings
Sub, // Subtraction of the two topmost operands on the v-stack
Mul, // Multiplication of the two topmost operands on the v-stack
Div, // Division of the two topmost operands on the v-stack
Mod, // Modulus of the two topmost operands on the v-stack
Not, // Logical inversion of the topmost operand on the v-stack
Len, // Size operator of the topmost operand on the v-stack
Neg, // Negation (unary minus) operator of the topmost operand on the v-stack
Power, // Power of the two topmost operands on the v-stack
CNot, // Conditional NOT - takes second operand from the v-stack (must be bool), if true execs a NOT otherwise execs a TOBOOL
//Bit Operators
BAnd,
BOr,
BXor,
BLShift,
BRShiftA,
BRShiftL,
BNot,
// Type conversions and manipulations
MkTuple, // Creates a tuple from the topmost n values
Scalar, // Converts the topmost tuple to a scalar
Incr, // Performs an add operation, without extracting the operands from the v-stack and assuming the operands are numbers.
ToNum, // Converts the top of the stack to a number
ToBool, // Converts the top of the stack to a boolean
ExpTuple, // Expands a tuple on the stack
// Iterators
IterPrep, // Prepares an iterator for execution
IterUpd, // Updates the var part of an iterator
// Nil coalescing
NilCoalescing,
NilCoalescingInverse,
JLclInit, // Inits a param value if a default one is specified and not provided at callsite.
// Meta
Invalid, // Crashes the executor with an unrecoverable NotImplementedException. This MUST always be the last opcode in enum
}
}