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]. |
16 | OPCODE(CONSTANT, 1) |
17 | |
18 | // Push null onto the stack. |
19 | OPCODE(NULL, 1) |
20 | |
21 | // Push false onto the stack. |
22 | OPCODE(FALSE, 1) |
23 | |
24 | // Push true onto the stack. |
25 | OPCODE(TRUE, 1) |
26 | |
27 | // Pushes the value in the given local slot. |
28 | OPCODE(LOAD_LOCAL_0, 1) |
29 | OPCODE(LOAD_LOCAL_1, 1) |
30 | OPCODE(LOAD_LOCAL_2, 1) |
31 | OPCODE(LOAD_LOCAL_3, 1) |
32 | OPCODE(LOAD_LOCAL_4, 1) |
33 | OPCODE(LOAD_LOCAL_5, 1) |
34 | OPCODE(LOAD_LOCAL_6, 1) |
35 | OPCODE(LOAD_LOCAL_7, 1) |
36 | OPCODE(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]. |
42 | OPCODE(LOAD_LOCAL, 1) |
43 | |
44 | // Stores the top of stack in local slot [arg]. Does not pop it. |
45 | OPCODE(STORE_LOCAL, 0) |
46 | |
47 | // Pushes the value in upvalue [arg]. |
48 | OPCODE(LOAD_UPVALUE, 1) |
49 | |
50 | // Stores the top of stack in upvalue [arg]. Does not pop it. |
51 | OPCODE(STORE_UPVALUE, 0) |
52 | |
53 | // Pushes the value of the top-level variable in slot [arg]. |
54 | OPCODE(LOAD_MODULE_VAR, 1) |
55 | |
56 | // Stores the top of stack in top-level variable slot [arg]. Does not pop it. |
57 | OPCODE(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. |
63 | OPCODE(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. |
68 | OPCODE(STORE_FIELD_THIS, 0) |
69 | |
70 | // Pops an instance and pushes the value of the field in slot [arg] of it. |
71 | OPCODE(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. |
75 | OPCODE(STORE_FIELD, -1) |
76 | |
77 | // Pop and discard the top of stack. |
78 | OPCODE(POP, -1) |
79 | |
80 | // Invoke the method with symbol [arg]. The number indicates the number of |
81 | // arguments (not including the receiver). |
82 | OPCODE(CALL_0, 0) |
83 | OPCODE(CALL_1, -1) |
84 | OPCODE(CALL_2, -2) |
85 | OPCODE(CALL_3, -3) |
86 | OPCODE(CALL_4, -4) |
87 | OPCODE(CALL_5, -5) |
88 | OPCODE(CALL_6, -6) |
89 | OPCODE(CALL_7, -7) |
90 | OPCODE(CALL_8, -8) |
91 | OPCODE(CALL_9, -9) |
92 | OPCODE(CALL_10, -10) |
93 | OPCODE(CALL_11, -11) |
94 | OPCODE(CALL_12, -12) |
95 | OPCODE(CALL_13, -13) |
96 | OPCODE(CALL_14, -14) |
97 | OPCODE(CALL_15, -15) |
98 | OPCODE(CALL_16, -16) |
99 | |
100 | // Invoke a superclass method with symbol [arg]. The number indicates the |
101 | // number of arguments (not including the receiver). |
102 | OPCODE(SUPER_0, 0) |
103 | OPCODE(SUPER_1, -1) |
104 | OPCODE(SUPER_2, -2) |
105 | OPCODE(SUPER_3, -3) |
106 | OPCODE(SUPER_4, -4) |
107 | OPCODE(SUPER_5, -5) |
108 | OPCODE(SUPER_6, -6) |
109 | OPCODE(SUPER_7, -7) |
110 | OPCODE(SUPER_8, -8) |
111 | OPCODE(SUPER_9, -9) |
112 | OPCODE(SUPER_10, -10) |
113 | OPCODE(SUPER_11, -11) |
114 | OPCODE(SUPER_12, -12) |
115 | OPCODE(SUPER_13, -13) |
116 | OPCODE(SUPER_14, -14) |
117 | OPCODE(SUPER_15, -15) |
118 | OPCODE(SUPER_16, -16) |
119 | |
120 | // Jump the instruction pointer [arg] forward. |
121 | OPCODE(JUMP, 0) |
122 | |
123 | // Jump the instruction pointer [arg] backward. |
124 | OPCODE(LOOP, 0) |
125 | |
126 | // Pop and if not truthy then jump the instruction pointer [arg] forward. |
127 | OPCODE(JUMP_IF, -1) |
128 | |
129 | // If the top of the stack is false, jump [arg] forward. Otherwise, pop and |
130 | // continue. |
131 | OPCODE(AND, -1) |
132 | |
133 | // If the top of the stack is non-false, jump [arg] forward. Otherwise, pop |
134 | // and continue. |
135 | OPCODE(OR, -1) |
136 | |
137 | // Close the upvalue for the local on the top of the stack, then pop it. |
138 | OPCODE(CLOSE_UPVALUE, -1) |
139 | |
140 | // Exit from the current function and return the value on the top of the |
141 | // stack. |
142 | OPCODE(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. |
152 | OPCODE(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. |
159 | OPCODE(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. |
166 | OPCODE(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. |
170 | OPCODE(CLASS, -1) |
171 | |
172 | // Ends a class. |
173 | // Atm the stack contains the class and the ClassAttributes (or null). |
174 | OPCODE(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. |
178 | OPCODE(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. |
186 | OPCODE(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. |
195 | OPCODE(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. |
200 | OPCODE(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.) |
208 | OPCODE(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. |
213 | OPCODE(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. |
217 | OPCODE(END, 0) |
218 | |