Embed:
(wiki syntax)
Show/hide line numbers
interp.h
Go to the documentation of this file.
00001 /* 00002 # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall. 00003 # 00004 # This file is part of the PyMite VM. 00005 # The PyMite VM is free software: you can redistribute it and/or modify 00006 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2. 00007 # 00008 # The PyMite VM is distributed in the hope that it will be useful, 00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00011 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2 00012 # is seen in the file COPYING in this directory. 00013 */ 00014 00015 00016 #ifndef __INTERP_H__ 00017 #define __INTERP_H__ 00018 00019 00020 /** 00021 * \file 00022 * \brief VM Interpreter 00023 * 00024 * VM interpreter header. 00025 */ 00026 00027 00028 #include "thread.h" 00029 00030 00031 #define INTERP_LOOP_FOREVER 0 00032 #define INTERP_RETURN_ON_NO_THREADS 1 00033 00034 00035 /** frame pointer ; currently for single thread */ 00036 #define FP (gVmGlobal.pthread->pframe) 00037 /** main module pointer (referred to by root frame) */ 00038 #define MP (gVmGlobal.pmod) 00039 /** instruction pointer */ 00040 #define IP (FP->fo_ip) 00041 /** argument stack pointer */ 00042 #undef SP 00043 #define SP (FP->fo_sp) 00044 /** memspace where the frame's func's CO came from */ 00045 #define MS (FP->fo_memspace) 00046 00047 /** top of stack */ 00048 #define TOS (*(SP - 1)) 00049 /** one under TOS */ 00050 #define TOS1 (*(SP - 2)) 00051 /** two under TOS */ 00052 #define TOS2 (*(SP - 3)) 00053 /** three under TOS */ 00054 #define TOS3 (*(SP - 4)) 00055 /** index into stack; 0 is top, 1 is next */ 00056 #define STACK(n) (*(SP - ((n) + 1))) 00057 /** pops an obj from the stack */ 00058 #define PM_POP() (*(--SP)) 00059 /** pushes an obj on the stack */ 00060 #define PM_PUSH(pobj) (*(SP++) = (pobj)) 00061 /** gets the argument (S16) from the instruction stream */ 00062 #define GET_ARG() mem_getWord(MS, &IP) 00063 00064 /** pushes an obj in the only stack slot of the native frame */ 00065 #define NATIVE_SET_TOS(pobj) (gVmGlobal.nativeframe.nf_stack = \ 00066 (pobj)) 00067 /** gets the nth local var from the native frame locals */ 00068 #define NATIVE_GET_LOCAL(n) (gVmGlobal.nativeframe.nf_locals[n]) 00069 /** gets a pointer to the frame that called this native fxn */ 00070 #define NATIVE_GET_PFRAME() (*ppframe) 00071 /** gets the number of args passed to the native fxn */ 00072 #define NATIVE_GET_NUM_ARGS() (gVmGlobal.nativeframe.nf_numlocals) 00073 00074 00075 /** 00076 * COMPARE_OP enum. 00077 * Used by the COMPARE_OP bytecode to determine 00078 * which type of compare to perform. 00079 * Must match those defined in Python. 00080 */ 00081 typedef enum PmCompare_e 00082 { 00083 COMP_LT = 0, /**< less than */ 00084 COMP_LE, /**< less than or equal */ 00085 COMP_EQ, /**< equal */ 00086 COMP_NE, /**< not equal */ 00087 COMP_GT, /**< greater than */ 00088 COMP_GE, /**< greater than or equal */ 00089 COMP_IN, /**< is in */ 00090 COMP_NOT_IN, /**< is not in */ 00091 COMP_IS, /**< is */ 00092 COMP_IS_NOT, /**< is not */ 00093 COMP_EXN_MATCH /**< do exceptions match */ 00094 } PmCompare_t, *pPmCompare_t; 00095 00096 /** 00097 * Byte code enumeration 00098 */ 00099 typedef enum PmBcode_e 00100 { 00101 /* 00102 * Python source to create this list: 00103 * import dis 00104 * o = dis.opname 00105 * for i in range(256): 00106 * if o[i][0] != '<': 00107 * print "\t%s," % o[i] 00108 * else: 00109 * print "\tUNUSED_%02X," % i 00110 */ 00111 STOP_CODE = 0, /* 0x00 */ 00112 POP_TOP, 00113 ROT_TWO, 00114 ROT_THREE, 00115 DUP_TOP, 00116 ROT_FOUR, 00117 UNUSED_06, 00118 UNUSED_07, 00119 UNUSED_08, 00120 NOP, 00121 UNARY_POSITIVE, /* d010 */ 00122 UNARY_NEGATIVE, 00123 UNARY_NOT, 00124 UNARY_CONVERT, 00125 UNUSED_0E, 00126 UNARY_INVERT, 00127 UNUSED_10, /* 0x10 */ 00128 UNUSED_11, 00129 LIST_APPEND, 00130 BINARY_POWER, 00131 BINARY_MULTIPLY, /* d020 */ 00132 BINARY_DIVIDE, 00133 BINARY_MODULO, 00134 BINARY_ADD, 00135 BINARY_SUBTRACT, 00136 BINARY_SUBSCR, 00137 BINARY_FLOOR_DIVIDE, 00138 BINARY_TRUE_DIVIDE, 00139 INPLACE_FLOOR_DIVIDE, 00140 INPLACE_TRUE_DIVIDE, 00141 SLICE_0, /* d030 */ 00142 SLICE_1, 00143 SLICE_2, /* 0x20 */ 00144 SLICE_3, 00145 UNUSED_22, 00146 UNUSED_23, 00147 UNUSED_24, 00148 UNUSED_25, 00149 UNUSED_26, 00150 UNUSED_27, 00151 STORE_SLICE_0, /* d040 */ 00152 STORE_SLICE_1, 00153 STORE_SLICE_2, 00154 STORE_SLICE_3, 00155 UNUSED_2C, 00156 UNUSED_2D, 00157 UNUSED_2E, 00158 UNUSED_2F, 00159 UNUSED_30, /* 0x30 */ 00160 UNUSED_31, 00161 DELETE_SLICE_0, /* d050 */ 00162 DELETE_SLICE_1, 00163 DELETE_SLICE_2, 00164 DELETE_SLICE_3, 00165 STORE_MAP, 00166 INPLACE_ADD, 00167 INPLACE_SUBTRACT, 00168 INPLACE_MULTIPLY, 00169 INPLACE_DIVIDE, 00170 INPLACE_MODULO, 00171 STORE_SUBSCR, /* d060 */ 00172 DELETE_SUBSCR, 00173 BINARY_LSHIFT, 00174 BINARY_RSHIFT, 00175 BINARY_AND, /* 0x40 */ 00176 BINARY_XOR, 00177 BINARY_OR, 00178 INPLACE_POWER, 00179 GET_ITER, 00180 UNUSED_45, 00181 PRINT_EXPR, /* d070 */ 00182 PRINT_ITEM, 00183 PRINT_NEWLINE, 00184 PRINT_ITEM_TO, 00185 PRINT_NEWLINE_TO, 00186 INPLACE_LSHIFT, 00187 INPLACE_RSHIFT, 00188 INPLACE_AND, 00189 INPLACE_XOR, 00190 INPLACE_OR, 00191 BREAK_LOOP, /* 0x50 *//* d080 */ 00192 WITH_CLEANUP, 00193 LOAD_LOCALS, 00194 RETURN_VALUE, 00195 IMPORT_STAR, 00196 EXEC_STMT, 00197 YIELD_VALUE, 00198 POP_BLOCK, 00199 END_FINALLY, 00200 BUILD_CLASS, 00201 00202 /* Opcodes from here have an argument */ 00203 HAVE_ARGUMENT = 90, /* d090 */ 00204 STORE_NAME = 90, 00205 DELETE_NAME, 00206 UNPACK_SEQUENCE, 00207 FOR_ITER, 00208 UNUSED_5E, 00209 STORE_ATTR, 00210 DELETE_ATTR, /* 0x60 */ 00211 STORE_GLOBAL, 00212 DELETE_GLOBAL, 00213 DUP_TOPX, 00214 LOAD_CONST, /* d100 */ 00215 LOAD_NAME, 00216 BUILD_TUPLE, 00217 BUILD_LIST, 00218 BUILD_MAP, 00219 LOAD_ATTR, 00220 COMPARE_OP, 00221 IMPORT_NAME, 00222 IMPORT_FROM, 00223 UNUSED_6D, 00224 JUMP_FORWARD, /* d110 */ 00225 JUMP_IF_FALSE, 00226 JUMP_IF_TRUE, /* 0x70 */ 00227 JUMP_ABSOLUTE, 00228 UNUSED_72, 00229 UNUSED_73, 00230 LOAD_GLOBAL, 00231 UNUSED_75, 00232 UNUSED_76, 00233 CONTINUE_LOOP, 00234 SETUP_LOOP, /* d120 */ 00235 SETUP_EXCEPT, 00236 SETUP_FINALLY, 00237 UNUSED_7B, 00238 LOAD_FAST, 00239 STORE_FAST, 00240 DELETE_FAST, 00241 UNUSED_79, 00242 UNUSED_80, /* 0x80 */ 00243 UNUSED_81, 00244 RAISE_VARARGS, /* d130 */ 00245 CALL_FUNCTION, 00246 MAKE_FUNCTION, 00247 BUILD_SLICE, 00248 MAKE_CLOSURE, 00249 LOAD_CLOSURE, 00250 LOAD_DEREF, 00251 STORE_DEREF, 00252 UNUSED_8A, 00253 UNUSED_8B, 00254 CALL_FUNCTION_VAR, /* d140 */ 00255 CALL_FUNCTION_KW, 00256 CALL_FUNCTION_VAR_KW, 00257 EXTENDED_ARG, 00258 00259 UNUSED_90, UNUSED_91, UNUSED_92, UNUSED_93, 00260 UNUSED_94, UNUSED_95, UNUSED_96, UNUSED_97, 00261 UNUSED_98, UNUSED_99, UNUSED_9A, UNUSED_9B, 00262 UNUSED_9C, UNUSED_9D, UNUSED_9E, UNUSED_9F, 00263 UNUSED_A0, UNUSED_A1, UNUSED_A2, UNUSED_A3, 00264 UNUSED_A4, UNUSED_A5, UNUSED_A6, UNUSED_A7, 00265 UNUSED_A8, UNUSED_A9, UNUSED_AA, UNUSED_AB, 00266 UNUSED_AC, UNUSED_AD, UNUSED_AE, UNUSED_AF, 00267 UNUSED_B0, UNUSED_B1, UNUSED_B2, UNUSED_B3, 00268 UNUSED_B4, UNUSED_B5, UNUSED_B6, UNUSED_B7, 00269 UNUSED_B8, UNUSED_B9, UNUSED_BA, UNUSED_BB, 00270 UNUSED_BC, UNUSED_BD, UNUSED_BE, UNUSED_BF, 00271 UNUSED_C0, UNUSED_C1, UNUSED_C2, UNUSED_C3, 00272 UNUSED_C4, UNUSED_C5, UNUSED_C6, UNUSED_C7, 00273 UNUSED_C8, UNUSED_C9, UNUSED_CA, UNUSED_CB, 00274 UNUSED_CC, UNUSED_CD, UNUSED_CE, UNUSED_CF, 00275 UNUSED_D0, UNUSED_D1, UNUSED_D2, UNUSED_D3, 00276 UNUSED_D4, UNUSED_D5, UNUSED_D6, UNUSED_D7, 00277 UNUSED_D8, UNUSED_D9, UNUSED_DA, UNUSED_DB, 00278 UNUSED_DC, UNUSED_DD, UNUSED_DE, UNUSED_DF, 00279 UNUSED_E0, UNUSED_E1, UNUSED_E2, UNUSED_E3, 00280 UNUSED_E4, UNUSED_E5, UNUSED_E6, UNUSED_E7, 00281 UNUSED_E8, UNUSED_E9, UNUSED_EA, UNUSED_EB, 00282 UNUSED_EC, UNUSED_ED, UNUSED_EE, UNUSED_EF, 00283 UNUSED_F0, UNUSED_F1, UNUSED_F2, UNUSED_F3, 00284 UNUSED_F4, UNUSED_F5, UNUSED_F6, UNUSED_F7, 00285 UNUSED_F8, UNUSED_F9, UNUSED_FA, UNUSED_FB, 00286 UNUSED_FC, UNUSED_FD, UNUSED_FE, UNUSED_FF 00287 } PmBcode_t, *pPmBcode_t; 00288 00289 00290 /** 00291 * Interprets the available threads. Does not return. 00292 * 00293 * @param returnOnNoThreads Loop forever if 0, exit with status if no more 00294 * threads left. 00295 * @return Return status if called with returnOnNoThreads != 0, 00296 * will not return otherwise. 00297 */ 00298 PmReturn_t interpret(const uint8_t returnOnNoThreads); 00299 00300 /** 00301 * Selects a thread to run and changes the VM internal variables to 00302 * let the switch-loop execute the chosen one in the next iteration. 00303 * For the moment the algorithm is primitive and will change the 00304 * thread each time it is called in a round-robin fashion. 00305 */ 00306 PmReturn_t interp_reschedule(void); 00307 00308 /** 00309 * Creates a thread object and adds it to the queue of threads to be 00310 * executed while interpret() is running. 00311 * 00312 * The given obj may be a function, module, or class. 00313 * Creates a frame for the given function. 00314 * 00315 * @param pfunc Ptr to function to be executed as a thread. 00316 * @return Return status 00317 */ 00318 PmReturn_t interp_addThread(pPmFunc_t pfunc); 00319 00320 /** 00321 * Sets the reschedule flag. 00322 * 00323 * @param boolean Reschedule on next occasion if boolean is true; clear 00324 * the flag otherwise. 00325 */ 00326 void interp_setRescheduleFlag(uint8_t boolean); 00327 00328 #endif /* __INTERP_H__ */
Generated on Tue Jul 12 2022 17:07:01 by
1.7.2