1// This defines the bytecode instructions used by the VM. It does so by invoking
2// an OPCODE() macro which is expected to be defined at the point that this is
3// included. (See: http://en.wikipedia.org/wiki/X_Macro for more.)
4//
5// The first argument is the name of the opcode. The second is its "stack
6// effect" -- the amount that the op code changes the size of the stack. A
7// stack effect of 1 means it pushes a value and the stack grows one larger.
8// -2 means it pops two values, etc.
9//
10// Note that the order of instructions here affects the order of the dispatch
11// table in the VM's interpreter loop. That in turn affects caching which
12// affects overall performance. Take care to run benchmarks if you change the
13// order here.
14
15// Load the constant at index [arg].
16OPCODE(CONSTANT, 1)
17
18// Push null onto the stack.
19OPCODE(NULL, 1)
20
21// Push false onto the stack.
22OPCODE(FALSE, 1)
23
24// Push true onto the stack.
25OPCODE(TRUE, 1)
26
27// Pushes the value in the given local slot.
28OPCODE(LOAD_LOCAL_0, 1)
29OPCODE(LOAD_LOCAL_1, 1)
30OPCODE(LOAD_LOCAL_2, 1)
31OPCODE(LOAD_LOCAL_3, 1)
32OPCODE(LOAD_LOCAL_4, 1)
33OPCODE(LOAD_LOCAL_5, 1)
34OPCODE(LOAD_LOCAL_6, 1)
35OPCODE(LOAD_LOCAL_7, 1)
36OPCODE(LOAD_LOCAL_8, 1)
37
38// Note: The compiler assumes the following _STORE instructions always
39// immediately follow their corresponding _LOAD ones.
40
41// Pushes the value in local slot [arg].
42OPCODE(LOAD_LOCAL, 1)
43
44// Stores the top of stack in local slot [arg]. Does not pop it.
45OPCODE(STORE_LOCAL, 0)
46
47// Pushes the value in upvalue [arg].
48OPCODE(LOAD_UPVALUE, 1)
49
50// Stores the top of stack in upvalue [arg]. Does not pop it.
51OPCODE(STORE_UPVALUE, 0)
52
53// Pushes the value of the top-level variable in slot [arg].
54OPCODE(LOAD_MODULE_VAR, 1)
55
56// Stores the top of stack in top-level variable slot [arg]. Does not pop it.
57OPCODE(STORE_MODULE_VAR, 0)
58
59// Pushes the value of the field in slot [arg] of the receiver of the current
60// function. This is used for regular field accesses on "this" directly in
61// methods. This instruction is faster than the more general CODE_LOAD_FIELD
62// instruction.
63OPCODE(LOAD_FIELD_THIS, 1)
64
65// Stores the top of the stack in field slot [arg] in the receiver of the
66// current value. Does not pop the value. This instruction is faster than the
67// more general CODE_LOAD_FIELD instruction.
68OPCODE(STORE_FIELD_THIS, 0)
69
70// Pops an instance and pushes the value of the field in slot [arg] of it.
71OPCODE(LOAD_FIELD, 0)
72
73// Pops an instance and stores the subsequent top of stack in field slot
74// [arg] in it. Does not pop the value.
75OPCODE(STORE_FIELD, -1)
76
77// Pop and discard the top of stack.
78OPCODE(POP, -1)
79
80// Invoke the method with symbol [arg]. The number indicates the number of
81// arguments (not including the receiver).
82OPCODE(CALL_0, 0)
83OPCODE(CALL_1, -1)
84OPCODE(CALL_2, -2)
85OPCODE(CALL_3, -3)
86OPCODE(CALL_4, -4)
87OPCODE(CALL_5, -5)
88OPCODE(CALL_6, -6)
89OPCODE(CALL_7, -7)
90OPCODE(CALL_8, -8)
91OPCODE(CALL_9, -9)
92OPCODE(CALL_10, -10)
93OPCODE(CALL_11, -11)
94OPCODE(CALL_12, -12)
95OPCODE(CALL_13, -13)
96OPCODE(CALL_14, -14)
97OPCODE(CALL_15, -15)
98OPCODE(CALL_16, -16)
99
100// Invoke a superclass method with symbol [arg]. The number indicates the
101// number of arguments (not including the receiver).
102OPCODE(SUPER_0, 0)
103OPCODE(SUPER_1, -1)
104OPCODE(SUPER_2, -2)
105OPCODE(SUPER_3, -3)
106OPCODE(SUPER_4, -4)
107OPCODE(SUPER_5, -5)
108OPCODE(SUPER_6, -6)
109OPCODE(SUPER_7, -7)
110OPCODE(SUPER_8, -8)
111OPCODE(SUPER_9, -9)
112OPCODE(SUPER_10, -10)
113OPCODE(SUPER_11, -11)
114OPCODE(SUPER_12, -12)
115OPCODE(SUPER_13, -13)
116OPCODE(SUPER_14, -14)
117OPCODE(SUPER_15, -15)
118OPCODE(SUPER_16, -16)
119
120// Jump the instruction pointer [arg] forward.
121OPCODE(JUMP, 0)
122
123// Jump the instruction pointer [arg] backward.
124OPCODE(LOOP, 0)
125
126// Pop and if not truthy then jump the instruction pointer [arg] forward.
127OPCODE(JUMP_IF, -1)
128
129// If the top of the stack is false, jump [arg] forward. Otherwise, pop and
130// continue.
131OPCODE(AND, -1)
132
133// If the top of the stack is non-false, jump [arg] forward. Otherwise, pop
134// and continue.
135OPCODE(OR, -1)
136
137// Close the upvalue for the local on the top of the stack, then pop it.
138OPCODE(CLOSE_UPVALUE, -1)
139
140// Exit from the current function and return the value on the top of the
141// stack.
142OPCODE(RETURN, 0)
143
144// Creates a closure for the function stored at [arg] in the constant table.
145//
146// Following the function argument is a number of arguments, two for each
147// upvalue. The first is true if the variable being captured is a local (as
148// opposed to an upvalue), and the second is the index of the local or
149// upvalue being captured.
150//
151// Pushes the created closure.
152OPCODE(CLOSURE, 1)
153
154// Creates a new instance of a class.
155//
156// Assumes the class object is in slot zero, and replaces it with the new
157// uninitialized instance of that class. This opcode is only emitted by the
158// compiler-generated constructor metaclass methods.
159OPCODE(CONSTRUCT, 0)
160
161// Creates a new instance of a foreign class.
162//
163// Assumes the class object is in slot zero, and replaces it with the new
164// uninitialized instance of that class. This opcode is only emitted by the
165// compiler-generated constructor metaclass methods.
166OPCODE(FOREIGN_CONSTRUCT, 0)
167
168// Creates a class. Top of stack is the superclass. Below that is a string for
169// the name of the class. Byte [arg] is the number of fields in the class.
170OPCODE(CLASS, -1)
171
172// Ends a class.
173// Atm the stack contains the class and the ClassAttributes (or null).
174OPCODE(END_CLASS, -2)
175
176// Creates a foreign class. Top of stack is the superclass. Below that is a
177// string for the name of the class.
178OPCODE(FOREIGN_CLASS, -1)
179
180// Define a method for symbol [arg]. The class receiving the method is popped
181// off the stack, then the function defining the body is popped.
182//
183// If a foreign method is being defined, the "function" will be a string
184// identifying the foreign method. Otherwise, it will be a function or
185// closure.
186OPCODE(METHOD_INSTANCE, -2)
187
188// Define a method for symbol [arg]. The class whose metaclass will receive
189// the method is popped off the stack, then the function defining the body is
190// popped.
191//
192// If a foreign method is being defined, the "function" will be a string
193// identifying the foreign method. Otherwise, it will be a function or
194// closure.
195OPCODE(METHOD_STATIC, -2)
196
197// This is executed at the end of the module's body. Pushes NULL onto the stack
198// as the "return value" of the import statement and stores the module as the
199// most recently imported one.
200OPCODE(END_MODULE, 1)
201
202// Import a module whose name is the string stored at [arg] in the constant
203// table.
204//
205// Pushes null onto the stack so that the fiber for the imported module can
206// replace that with a dummy value when it returns. (Fibers always return a
207// value when resuming a caller.)
208OPCODE(IMPORT_MODULE, 1)
209
210// Import a variable from the most recently imported module. The name of the
211// variable to import is at [arg] in the constant table. Pushes the loaded
212// variable's value.
213OPCODE(IMPORT_VARIABLE, 1)
214
215// This pseudo-instruction indicates the end of the bytecode. It should
216// always be preceded by a `CODE_RETURN`, so is never actually executed.
217OPCODE(END, 0)
218