LP-89 - Port OP_15.05.01 fixes. Release notes:
[librepilot.git] / flight / libraries / PyMite / vm / interp.h
blob93b8632e7930fcafafda820015ba7d831386eb08
1 /*
2 # This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
4 # This file is part of the PyMite VM.
5 # The PyMite VM is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
8 # The PyMite VM is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
12 # is seen in the file COPYING in this directory.
16 #ifndef __INTERP_H__
17 #define __INTERP_H__
20 /**
21 * \file
22 * \brief VM Interpreter
24 * VM interpreter header.
28 #include "thread.h"
31 #define INTERP_LOOP_FOREVER 0
32 #define INTERP_RETURN_ON_NO_THREADS 1
35 /** Frame pointer ; currently for single thread */
36 #define PM_FP (gVmGlobal.pthread->pframe)
37 /** Instruction pointer */
38 #define PM_IP (PM_FP->fo_ip)
39 /** Argument stack pointer */
40 #define PM_SP (PM_FP->fo_sp)
42 /** top of stack */
43 #define TOS (*(PM_SP - 1))
44 /** one under TOS */
45 #define TOS1 (*(PM_SP - 2))
46 /** two under TOS */
47 #define TOS2 (*(PM_SP - 3))
48 /** three under TOS */
49 #define TOS3 (*(PM_SP - 4))
50 /** index into stack; 0 is top, 1 is next */
51 #define STACK(n) (*(PM_SP - ((n) + 1)))
52 /** pops an obj from the stack */
53 #define PM_POP() (*(--PM_SP))
54 /** pushes an obj on the stack */
55 #define PM_PUSH(pobj) (*(PM_SP++) = (pobj))
56 /** gets the argument (S16) from the instruction stream */
57 #define GET_ARG() mem_getWord(PM_FP->fo_memspace, &PM_IP)
59 /** pushes an obj in the only stack slot of the native frame */
60 #define NATIVE_SET_TOS(pobj) (gVmGlobal.nativeframe.nf_stack = \
61 (pobj))
62 /** gets the nth local var from the native frame locals */
63 #define NATIVE_GET_LOCAL(n) (gVmGlobal.nativeframe.nf_locals[n])
64 /** gets a pointer to the frame that called this native fxn */
65 #define NATIVE_GET_PFRAME() (*ppframe)
66 /** gets the number of args passed to the native fxn */
67 #define NATIVE_GET_NUM_ARGS() (gVmGlobal.nativeframe.nf_numlocals)
70 /**
71 * COMPARE_OP enum.
72 * Used by the COMPARE_OP bytecode to determine
73 * which type of compare to perform.
74 * Must match those defined in Python.
76 typedef enum PmCompare_e
78 COMP_LT = 0, /**< less than */
79 COMP_LE, /**< less than or equal */
80 COMP_EQ, /**< equal */
81 COMP_NE, /**< not equal */
82 COMP_GT, /**< greater than */
83 COMP_GE, /**< greater than or equal */
84 COMP_IN, /**< is in */
85 COMP_NOT_IN, /**< is not in */
86 COMP_IS, /**< is */
87 COMP_IS_NOT, /**< is not */
88 COMP_EXN_MATCH /**< do exceptions match */
89 } PmCompare_t, *pPmCompare_t;
91 /**
92 * Byte code enumeration
94 typedef enum PmBcode_e
97 * Python source to create this list:
98 * import dis
99 * o = dis.opname
100 * for i in range(256):
101 * if o[i][0] != '<':
102 * print "\t%s," % o[i]
103 * else:
104 * print "\tUNUSED_%02X," % i
106 STOP_CODE = 0, /* 0x00 */
107 POP_TOP,
108 ROT_TWO,
109 ROT_THREE,
110 DUP_TOP,
111 ROT_FOUR,
112 UNUSED_06,
113 UNUSED_07,
114 UNUSED_08,
115 NOP,
116 UNARY_POSITIVE, /* d010 */
117 UNARY_NEGATIVE,
118 UNARY_NOT,
119 UNARY_CONVERT,
120 UNUSED_0E,
121 UNARY_INVERT,
122 UNUSED_10, /* 0x10 */
123 UNUSED_11,
124 LIST_APPEND,
125 BINARY_POWER,
126 BINARY_MULTIPLY, /* d020 */
127 BINARY_DIVIDE,
128 BINARY_MODULO,
129 BINARY_ADD,
130 BINARY_SUBTRACT,
131 BINARY_SUBSCR,
132 BINARY_FLOOR_DIVIDE,
133 BINARY_TRUE_DIVIDE,
134 INPLACE_FLOOR_DIVIDE,
135 INPLACE_TRUE_DIVIDE,
136 SLICE_0, /* d030 */
137 SLICE_1,
138 SLICE_2, /* 0x20 */
139 SLICE_3,
140 UNUSED_22,
141 UNUSED_23,
142 UNUSED_24,
143 UNUSED_25,
144 UNUSED_26,
145 UNUSED_27,
146 STORE_SLICE_0, /* d040 */
147 STORE_SLICE_1,
148 STORE_SLICE_2,
149 STORE_SLICE_3,
150 UNUSED_2C,
151 UNUSED_2D,
152 UNUSED_2E,
153 UNUSED_2F,
154 UNUSED_30, /* 0x30 */
155 UNUSED_31,
156 DELETE_SLICE_0, /* d050 */
157 DELETE_SLICE_1,
158 DELETE_SLICE_2,
159 DELETE_SLICE_3,
160 STORE_MAP,
161 INPLACE_ADD,
162 INPLACE_SUBTRACT,
163 INPLACE_MULTIPLY,
164 INPLACE_DIVIDE,
165 INPLACE_MODULO,
166 STORE_SUBSCR, /* d060 */
167 DELETE_SUBSCR,
168 BINARY_LSHIFT,
169 BINARY_RSHIFT,
170 BINARY_AND, /* 0x40 */
171 BINARY_XOR,
172 BINARY_OR,
173 INPLACE_POWER,
174 GET_ITER,
175 UNUSED_45,
176 PRINT_EXPR, /* d070 */
177 PRINT_ITEM,
178 PRINT_NEWLINE,
179 PRINT_ITEM_TO,
180 PRINT_NEWLINE_TO,
181 INPLACE_LSHIFT,
182 INPLACE_RSHIFT,
183 INPLACE_AND,
184 INPLACE_XOR,
185 INPLACE_OR,
186 BREAK_LOOP, /* 0x50 *//* d080 */
187 WITH_CLEANUP,
188 LOAD_LOCALS,
189 RETURN_VALUE,
190 IMPORT_STAR,
191 EXEC_STMT,
192 YIELD_VALUE,
193 POP_BLOCK,
194 END_FINALLY,
195 BUILD_CLASS,
197 /* Opcodes from here have an argument */
198 HAVE_ARGUMENT = 90, /* d090 */
199 STORE_NAME = 90,
200 DELETE_NAME,
201 UNPACK_SEQUENCE,
202 FOR_ITER,
203 UNUSED_5E,
204 STORE_ATTR,
205 DELETE_ATTR, /* 0x60 */
206 STORE_GLOBAL,
207 DELETE_GLOBAL,
208 DUP_TOPX,
209 LOAD_CONST, /* d100 */
210 LOAD_NAME,
211 BUILD_TUPLE,
212 BUILD_LIST,
213 BUILD_MAP,
214 LOAD_ATTR,
215 COMPARE_OP,
216 IMPORT_NAME,
217 IMPORT_FROM,
218 UNUSED_6D,
219 JUMP_FORWARD, /* d110 */
220 JUMP_IF_FALSE,
221 JUMP_IF_TRUE, /* 0x70 */
222 JUMP_ABSOLUTE,
223 UNUSED_72,
224 UNUSED_73,
225 LOAD_GLOBAL,
226 UNUSED_75,
227 UNUSED_76,
228 CONTINUE_LOOP,
229 SETUP_LOOP, /* d120 */
230 SETUP_EXCEPT,
231 SETUP_FINALLY,
232 UNUSED_7B,
233 LOAD_FAST,
234 STORE_FAST,
235 DELETE_FAST,
236 UNUSED_79,
237 UNUSED_80, /* 0x80 */
238 UNUSED_81,
239 RAISE_VARARGS, /* d130 */
240 CALL_FUNCTION,
241 MAKE_FUNCTION,
242 BUILD_SLICE,
243 MAKE_CLOSURE,
244 LOAD_CLOSURE,
245 LOAD_DEREF,
246 STORE_DEREF,
247 UNUSED_8A,
248 UNUSED_8B,
249 CALL_FUNCTION_VAR, /* d140 */
250 CALL_FUNCTION_KW,
251 CALL_FUNCTION_VAR_KW,
252 EXTENDED_ARG,
254 UNUSED_90, UNUSED_91, UNUSED_92, UNUSED_93,
255 UNUSED_94, UNUSED_95, UNUSED_96, UNUSED_97,
256 UNUSED_98, UNUSED_99, UNUSED_9A, UNUSED_9B,
257 UNUSED_9C, UNUSED_9D, UNUSED_9E, UNUSED_9F,
258 UNUSED_A0, UNUSED_A1, UNUSED_A2, UNUSED_A3,
259 UNUSED_A4, UNUSED_A5, UNUSED_A6, UNUSED_A7,
260 UNUSED_A8, UNUSED_A9, UNUSED_AA, UNUSED_AB,
261 UNUSED_AC, UNUSED_AD, UNUSED_AE, UNUSED_AF,
262 UNUSED_B0, UNUSED_B1, UNUSED_B2, UNUSED_B3,
263 UNUSED_B4, UNUSED_B5, UNUSED_B6, UNUSED_B7,
264 UNUSED_B8, UNUSED_B9, UNUSED_BA, UNUSED_BB,
265 UNUSED_BC, UNUSED_BD, UNUSED_BE, UNUSED_BF,
266 UNUSED_C0, UNUSED_C1, UNUSED_C2, UNUSED_C3,
267 UNUSED_C4, UNUSED_C5, UNUSED_C6, UNUSED_C7,
268 UNUSED_C8, UNUSED_C9, UNUSED_CA, UNUSED_CB,
269 UNUSED_CC, UNUSED_CD, UNUSED_CE, UNUSED_CF,
270 UNUSED_D0, UNUSED_D1, UNUSED_D2, UNUSED_D3,
271 UNUSED_D4, UNUSED_D5, UNUSED_D6, UNUSED_D7,
272 UNUSED_D8, UNUSED_D9, UNUSED_DA, UNUSED_DB,
273 UNUSED_DC, UNUSED_DD, UNUSED_DE, UNUSED_DF,
274 UNUSED_E0, UNUSED_E1, UNUSED_E2, UNUSED_E3,
275 UNUSED_E4, UNUSED_E5, UNUSED_E6, UNUSED_E7,
276 UNUSED_E8, UNUSED_E9, UNUSED_EA, UNUSED_EB,
277 UNUSED_EC, UNUSED_ED, UNUSED_EE, UNUSED_EF,
278 UNUSED_F0, UNUSED_F1, UNUSED_F2, UNUSED_F3,
279 UNUSED_F4, UNUSED_F5, UNUSED_F6, UNUSED_F7,
280 UNUSED_F8, UNUSED_F9, UNUSED_FA, UNUSED_FB,
281 UNUSED_FC, UNUSED_FD, UNUSED_FE, UNUSED_FF
282 } PmBcode_t, *pPmBcode_t;
286 * Interprets the available threads. Does not return.
288 * @param returnOnNoThreads Loop forever if 0, exit with status if no more
289 * threads left.
290 * @return Return status if called with returnOnNoThreads != 0,
291 * will not return otherwise.
293 PmReturn_t interpret(const uint8_t returnOnNoThreads);
296 * Selects a thread to run and changes the VM internal variables to
297 * let the switch-loop execute the chosen one in the next iteration.
298 * For the moment the algorithm is primitive and will change the
299 * thread each time it is called in a round-robin fashion.
301 PmReturn_t interp_reschedule(void);
304 * Creates a thread object and adds it to the queue of threads to be
305 * executed while interpret() is running.
307 * The given obj may be a function, module, or class.
308 * Creates a frame for the given function.
310 * @param pfunc Ptr to function to be executed as a thread.
311 * @return Return status
313 PmReturn_t interp_addThread(pPmFunc_t pfunc);
316 * Sets the reschedule flag.
318 * @param boolean Reschedule on next occasion if boolean is true; clear
319 * the flag otherwise.
321 void interp_setRescheduleFlag(uint8_t boolean);
323 #endif /* __INTERP_H__ */