Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
- http://pymbed.appspot.com/
- https://code.google.com/p/python-on-a-chip/
- http://www.youtube.com/watch?v=Oyqc2bFRW9I
- https://bitbucket.org/va009039/pymbed/
more info: python-on-a-chip
vm/global.c@0:65f1469d6bfb, 2013-03-02 (annotated)
- Committer:
- va009039
- Date:
- Sat Mar 02 11:54:20 2013 +0000
- Revision:
- 0:65f1469d6bfb
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:65f1469d6bfb | 1 | /* |
va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2002 Dean Hall. |
va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
va009039 | 0:65f1469d6bfb | 6 | */ |
va009039 | 0:65f1469d6bfb | 7 | |
va009039 | 0:65f1469d6bfb | 8 | |
va009039 | 0:65f1469d6bfb | 9 | #undef __FILE_ID__ |
va009039 | 0:65f1469d6bfb | 10 | #define __FILE_ID__ 0x05 |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief VM Globals |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * VM globals operations. |
va009039 | 0:65f1469d6bfb | 18 | * PyMite's global struct def and initial values. |
va009039 | 0:65f1469d6bfb | 19 | */ |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | |
va009039 | 0:65f1469d6bfb | 22 | #include "pm.h" |
va009039 | 0:65f1469d6bfb | 23 | |
va009039 | 0:65f1469d6bfb | 24 | |
va009039 | 0:65f1469d6bfb | 25 | extern unsigned char const *stdlib_img; |
va009039 | 0:65f1469d6bfb | 26 | |
va009039 | 0:65f1469d6bfb | 27 | static uint8_t const *bistr = (uint8_t const *)"__bi"; |
va009039 | 0:65f1469d6bfb | 28 | |
va009039 | 0:65f1469d6bfb | 29 | |
va009039 | 0:65f1469d6bfb | 30 | /** Most PyMite globals all in one convenient place */ |
va009039 | 0:65f1469d6bfb | 31 | volatile PmVmGlobal_t gVmGlobal; |
va009039 | 0:65f1469d6bfb | 32 | |
va009039 | 0:65f1469d6bfb | 33 | |
va009039 | 0:65f1469d6bfb | 34 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 35 | global_init(void) |
va009039 | 0:65f1469d6bfb | 36 | { |
va009039 | 0:65f1469d6bfb | 37 | PmReturn_t retval; |
va009039 | 0:65f1469d6bfb | 38 | uint8_t *codestr = (uint8_t *)"code"; |
va009039 | 0:65f1469d6bfb | 39 | uint8_t *pchunk; |
va009039 | 0:65f1469d6bfb | 40 | pPmObj_t pobj; |
va009039 | 0:65f1469d6bfb | 41 | #ifdef HAVE_CLASSES |
va009039 | 0:65f1469d6bfb | 42 | uint8_t const *initstr = (uint8_t const *)"__init__"; |
va009039 | 0:65f1469d6bfb | 43 | #endif /* HAVE_CLASSES */ |
va009039 | 0:65f1469d6bfb | 44 | #ifdef HAVE_GENERATORS |
va009039 | 0:65f1469d6bfb | 45 | uint8_t const *genstr = (uint8_t const *)"Generator"; |
va009039 | 0:65f1469d6bfb | 46 | uint8_t const *nextstr = (uint8_t const *)"next"; |
va009039 | 0:65f1469d6bfb | 47 | #endif /* HAVE_GENERATORS */ |
va009039 | 0:65f1469d6bfb | 48 | #ifdef HAVE_ASSERT |
va009039 | 0:65f1469d6bfb | 49 | uint8_t const *exnstr = (uint8_t const *)"Exception"; |
va009039 | 0:65f1469d6bfb | 50 | #endif /* HAVE_ASSERT */ |
va009039 | 0:65f1469d6bfb | 51 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 52 | uint8_t const *pbastr = (uint8_t const *)"bytearray"; |
va009039 | 0:65f1469d6bfb | 53 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 54 | uint8_t const *pmdstr = (uint8_t const *)"__md"; |
va009039 | 0:65f1469d6bfb | 55 | |
va009039 | 0:65f1469d6bfb | 56 | /* Clear the global struct */ |
va009039 | 0:65f1469d6bfb | 57 | sli_memset((uint8_t *)&gVmGlobal, '\0', sizeof(PmVmGlobal_t)); |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | /* Set the PyMite release num (for debug and post mortem) */ |
va009039 | 0:65f1469d6bfb | 60 | gVmGlobal.errVmRelease = PM_RELEASE; |
va009039 | 0:65f1469d6bfb | 61 | |
va009039 | 0:65f1469d6bfb | 62 | /* Init zero */ |
va009039 | 0:65f1469d6bfb | 63 | retval = heap_getChunk(sizeof(PmInt_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 64 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 65 | pobj = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 66 | OBJ_SET_TYPE(pobj, OBJ_TYPE_INT); |
va009039 | 0:65f1469d6bfb | 67 | ((pPmInt_t)pobj)->val = (int32_t)0; |
va009039 | 0:65f1469d6bfb | 68 | gVmGlobal.pzero = (pPmInt_t)pobj; |
va009039 | 0:65f1469d6bfb | 69 | |
va009039 | 0:65f1469d6bfb | 70 | /* Init one */ |
va009039 | 0:65f1469d6bfb | 71 | retval = heap_getChunk(sizeof(PmInt_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 72 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 73 | pobj = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 74 | OBJ_SET_TYPE(pobj, OBJ_TYPE_INT); |
va009039 | 0:65f1469d6bfb | 75 | ((pPmInt_t)pobj)->val = (int32_t)1; |
va009039 | 0:65f1469d6bfb | 76 | gVmGlobal.pone = (pPmInt_t)pobj; |
va009039 | 0:65f1469d6bfb | 77 | |
va009039 | 0:65f1469d6bfb | 78 | /* Init negone */ |
va009039 | 0:65f1469d6bfb | 79 | retval = heap_getChunk(sizeof(PmInt_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 80 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 81 | pobj = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 82 | OBJ_SET_TYPE(pobj, OBJ_TYPE_INT); |
va009039 | 0:65f1469d6bfb | 83 | ((pPmInt_t)pobj)->val = (int32_t)-1; |
va009039 | 0:65f1469d6bfb | 84 | gVmGlobal.pnegone = (pPmInt_t)pobj; |
va009039 | 0:65f1469d6bfb | 85 | |
va009039 | 0:65f1469d6bfb | 86 | /* Init False */ |
va009039 | 0:65f1469d6bfb | 87 | retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 88 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 89 | pobj = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 90 | OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL); |
va009039 | 0:65f1469d6bfb | 91 | ((pPmBoolean_t) pobj)->val = (int32_t)C_FALSE; |
va009039 | 0:65f1469d6bfb | 92 | gVmGlobal.pfalse = (pPmInt_t)pobj; |
va009039 | 0:65f1469d6bfb | 93 | |
va009039 | 0:65f1469d6bfb | 94 | /* Init True */ |
va009039 | 0:65f1469d6bfb | 95 | retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 96 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 97 | pobj = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 98 | OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL); |
va009039 | 0:65f1469d6bfb | 99 | ((pPmBoolean_t) pobj)->val = (int32_t)C_TRUE; |
va009039 | 0:65f1469d6bfb | 100 | gVmGlobal.ptrue = (pPmInt_t)pobj; |
va009039 | 0:65f1469d6bfb | 101 | |
va009039 | 0:65f1469d6bfb | 102 | /* Init None */ |
va009039 | 0:65f1469d6bfb | 103 | retval = heap_getChunk(sizeof(PmObj_t), &pchunk); |
va009039 | 0:65f1469d6bfb | 104 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 105 | pobj = (pPmObj_t)pchunk; |
va009039 | 0:65f1469d6bfb | 106 | OBJ_SET_TYPE(pobj, OBJ_TYPE_NON); |
va009039 | 0:65f1469d6bfb | 107 | gVmGlobal.pnone = pobj; |
va009039 | 0:65f1469d6bfb | 108 | |
va009039 | 0:65f1469d6bfb | 109 | /* Init "code" string obj */ |
va009039 | 0:65f1469d6bfb | 110 | retval = string_new((uint8_t const **)&codestr, &pobj); |
va009039 | 0:65f1469d6bfb | 111 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 112 | gVmGlobal.pcodeStr = (pPmString_t)pobj; |
va009039 | 0:65f1469d6bfb | 113 | |
va009039 | 0:65f1469d6bfb | 114 | #ifdef HAVE_CLASSES |
va009039 | 0:65f1469d6bfb | 115 | /* Init "__init__" string obj */ |
va009039 | 0:65f1469d6bfb | 116 | retval = string_new((uint8_t const **)&initstr, &pobj); |
va009039 | 0:65f1469d6bfb | 117 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 118 | gVmGlobal.pinitStr = (pPmString_t)pobj; |
va009039 | 0:65f1469d6bfb | 119 | #endif /* HAVE_CLASSES */ |
va009039 | 0:65f1469d6bfb | 120 | |
va009039 | 0:65f1469d6bfb | 121 | #ifdef HAVE_GENERATORS |
va009039 | 0:65f1469d6bfb | 122 | /* Init "Generator" string obj */ |
va009039 | 0:65f1469d6bfb | 123 | retval = string_new((uint8_t const **)&genstr, &pobj); |
va009039 | 0:65f1469d6bfb | 124 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 125 | gVmGlobal.pgenStr = (pPmString_t)pobj; |
va009039 | 0:65f1469d6bfb | 126 | |
va009039 | 0:65f1469d6bfb | 127 | /* Init "next" string obj */ |
va009039 | 0:65f1469d6bfb | 128 | retval = string_new((uint8_t const **)&nextstr, &pobj); |
va009039 | 0:65f1469d6bfb | 129 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 130 | gVmGlobal.pnextStr = (pPmString_t)pobj; |
va009039 | 0:65f1469d6bfb | 131 | #endif /* HAVE_GENERATORS */ |
va009039 | 0:65f1469d6bfb | 132 | |
va009039 | 0:65f1469d6bfb | 133 | #ifdef HAVE_ASSERT |
va009039 | 0:65f1469d6bfb | 134 | /* Init "Exception" string obj */ |
va009039 | 0:65f1469d6bfb | 135 | retval = string_new((uint8_t const **)&exnstr, &pobj); |
va009039 | 0:65f1469d6bfb | 136 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 137 | gVmGlobal.pexnStr = (pPmString_t)pobj; |
va009039 | 0:65f1469d6bfb | 138 | #endif /* HAVE_ASSERT */ |
va009039 | 0:65f1469d6bfb | 139 | |
va009039 | 0:65f1469d6bfb | 140 | #ifdef HAVE_BYTEARRAY |
va009039 | 0:65f1469d6bfb | 141 | /* Init "bytearray" string obj */ |
va009039 | 0:65f1469d6bfb | 142 | retval = string_new((uint8_t const **)&pbastr, &pobj); |
va009039 | 0:65f1469d6bfb | 143 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 144 | gVmGlobal.pbaStr = (pPmString_t)pobj; |
va009039 | 0:65f1469d6bfb | 145 | #endif /* HAVE_BYTEARRAY */ |
va009039 | 0:65f1469d6bfb | 146 | |
va009039 | 0:65f1469d6bfb | 147 | /* Init "__md" string obj */ |
va009039 | 0:65f1469d6bfb | 148 | retval = string_new((uint8_t const **)&pmdstr, &pobj); |
va009039 | 0:65f1469d6bfb | 149 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 150 | gVmGlobal.pmdStr = (pPmString_t)pobj; |
va009039 | 0:65f1469d6bfb | 151 | |
va009039 | 0:65f1469d6bfb | 152 | /* Init empty builtins */ |
va009039 | 0:65f1469d6bfb | 153 | gVmGlobal.builtins = C_NULL; |
va009039 | 0:65f1469d6bfb | 154 | |
va009039 | 0:65f1469d6bfb | 155 | /* Init native frame */ |
va009039 | 0:65f1469d6bfb | 156 | gVmGlobal.nativeframe.od = sizeof(PmNativeFrame_t); |
va009039 | 0:65f1469d6bfb | 157 | OBJ_SET_TYPE(&gVmGlobal.nativeframe, OBJ_TYPE_NFM); |
va009039 | 0:65f1469d6bfb | 158 | gVmGlobal.nativeframe.nf_func = C_NULL; |
va009039 | 0:65f1469d6bfb | 159 | gVmGlobal.nativeframe.nf_stack = C_NULL; |
va009039 | 0:65f1469d6bfb | 160 | gVmGlobal.nativeframe.nf_active = C_FALSE; |
va009039 | 0:65f1469d6bfb | 161 | gVmGlobal.nativeframe.nf_numlocals = 0; |
va009039 | 0:65f1469d6bfb | 162 | |
va009039 | 0:65f1469d6bfb | 163 | /* Create empty threadList */ |
va009039 | 0:65f1469d6bfb | 164 | retval = list_new(&pobj); |
va009039 | 0:65f1469d6bfb | 165 | gVmGlobal.threadList = (pPmList_t)pobj; |
va009039 | 0:65f1469d6bfb | 166 | |
va009039 | 0:65f1469d6bfb | 167 | /* Init the PmImgPaths with std image info */ |
va009039 | 0:65f1469d6bfb | 168 | gVmGlobal.imgPaths.memspace[0] = MEMSPACE_PROG; |
va009039 | 0:65f1469d6bfb | 169 | gVmGlobal.imgPaths.pimg[0] = (uint8_t *)&stdlib_img; |
va009039 | 0:65f1469d6bfb | 170 | gVmGlobal.imgPaths.pathcount = 1; |
va009039 | 0:65f1469d6bfb | 171 | |
va009039 | 0:65f1469d6bfb | 172 | #ifdef HAVE_PRINT |
va009039 | 0:65f1469d6bfb | 173 | gVmGlobal.needSoftSpace = C_FALSE; |
va009039 | 0:65f1469d6bfb | 174 | gVmGlobal.somethingPrinted = C_FALSE; |
va009039 | 0:65f1469d6bfb | 175 | #endif /* HAVE_PRINT */ |
va009039 | 0:65f1469d6bfb | 176 | |
va009039 | 0:65f1469d6bfb | 177 | return retval; |
va009039 | 0:65f1469d6bfb | 178 | } |
va009039 | 0:65f1469d6bfb | 179 | |
va009039 | 0:65f1469d6bfb | 180 | |
va009039 | 0:65f1469d6bfb | 181 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 182 | global_setBuiltins(pPmFunc_t pmod) |
va009039 | 0:65f1469d6bfb | 183 | { |
va009039 | 0:65f1469d6bfb | 184 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 185 | pPmObj_t pkey = C_NULL; |
va009039 | 0:65f1469d6bfb | 186 | uint8_t const *pbistr = bistr; |
va009039 | 0:65f1469d6bfb | 187 | uint8_t objid; |
va009039 | 0:65f1469d6bfb | 188 | |
va009039 | 0:65f1469d6bfb | 189 | if (PM_PBUILTINS == C_NULL) |
va009039 | 0:65f1469d6bfb | 190 | { |
va009039 | 0:65f1469d6bfb | 191 | /* Need to load builtins first */ |
va009039 | 0:65f1469d6bfb | 192 | global_loadBuiltins(); |
va009039 | 0:65f1469d6bfb | 193 | } |
va009039 | 0:65f1469d6bfb | 194 | |
va009039 | 0:65f1469d6bfb | 195 | /* Put builtins module in the module's attrs dict */ |
va009039 | 0:65f1469d6bfb | 196 | retval = string_new(&pbistr, &pkey); |
va009039 | 0:65f1469d6bfb | 197 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 198 | |
va009039 | 0:65f1469d6bfb | 199 | heap_gcPushTempRoot(pkey, &objid); |
va009039 | 0:65f1469d6bfb | 200 | retval = dict_setItem((pPmObj_t)pmod->f_attrs, pkey, PM_PBUILTINS); |
va009039 | 0:65f1469d6bfb | 201 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 202 | |
va009039 | 0:65f1469d6bfb | 203 | return retval; |
va009039 | 0:65f1469d6bfb | 204 | } |
va009039 | 0:65f1469d6bfb | 205 | |
va009039 | 0:65f1469d6bfb | 206 | |
va009039 | 0:65f1469d6bfb | 207 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 208 | global_loadBuiltins(void) |
va009039 | 0:65f1469d6bfb | 209 | { |
va009039 | 0:65f1469d6bfb | 210 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 211 | pPmObj_t pkey = C_NULL; |
va009039 | 0:65f1469d6bfb | 212 | uint8_t const *nonestr = (uint8_t const *)"None"; |
va009039 | 0:65f1469d6bfb | 213 | uint8_t const *falsestr = (uint8_t const *)"False"; |
va009039 | 0:65f1469d6bfb | 214 | uint8_t const *truestr = (uint8_t const *)"True"; |
va009039 | 0:65f1469d6bfb | 215 | pPmObj_t pstr = C_NULL; |
va009039 | 0:65f1469d6bfb | 216 | pPmObj_t pbimod; |
va009039 | 0:65f1469d6bfb | 217 | uint8_t const *pbistr = bistr; |
va009039 | 0:65f1469d6bfb | 218 | |
va009039 | 0:65f1469d6bfb | 219 | /* Import the builtins */ |
va009039 | 0:65f1469d6bfb | 220 | retval = string_new(&pbistr, &pstr); |
va009039 | 0:65f1469d6bfb | 221 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 222 | retval = mod_import(pstr, &pbimod); |
va009039 | 0:65f1469d6bfb | 223 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 224 | |
va009039 | 0:65f1469d6bfb | 225 | /* Must interpret builtins' root code to set the attrs */ |
va009039 | 0:65f1469d6bfb | 226 | C_ASSERT(gVmGlobal.threadList->length == 0); |
va009039 | 0:65f1469d6bfb | 227 | interp_addThread((pPmFunc_t)pbimod); |
va009039 | 0:65f1469d6bfb | 228 | retval = interpret(INTERP_RETURN_ON_NO_THREADS); |
va009039 | 0:65f1469d6bfb | 229 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 230 | |
va009039 | 0:65f1469d6bfb | 231 | /* Builtins points to the builtins module's attrs dict */ |
va009039 | 0:65f1469d6bfb | 232 | gVmGlobal.builtins = ((pPmFunc_t)pbimod)->f_attrs; |
va009039 | 0:65f1469d6bfb | 233 | |
va009039 | 0:65f1469d6bfb | 234 | /* Set None manually */ |
va009039 | 0:65f1469d6bfb | 235 | retval = string_new(&nonestr, &pkey); |
va009039 | 0:65f1469d6bfb | 236 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 237 | retval = dict_setItem(PM_PBUILTINS, pkey, PM_NONE); |
va009039 | 0:65f1469d6bfb | 238 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 239 | |
va009039 | 0:65f1469d6bfb | 240 | /* Set False manually */ |
va009039 | 0:65f1469d6bfb | 241 | retval = string_new(&falsestr, &pkey); |
va009039 | 0:65f1469d6bfb | 242 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 243 | retval = dict_setItem(PM_PBUILTINS, pkey, PM_FALSE); |
va009039 | 0:65f1469d6bfb | 244 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 245 | |
va009039 | 0:65f1469d6bfb | 246 | /* Set True manually */ |
va009039 | 0:65f1469d6bfb | 247 | retval = string_new(&truestr, &pkey); |
va009039 | 0:65f1469d6bfb | 248 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 249 | retval = dict_setItem(PM_PBUILTINS, pkey, PM_TRUE); |
va009039 | 0:65f1469d6bfb | 250 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 251 | |
va009039 | 0:65f1469d6bfb | 252 | /* Deallocate builtins module */ |
va009039 | 0:65f1469d6bfb | 253 | retval = heap_freeChunk((pPmObj_t)pbimod); |
va009039 | 0:65f1469d6bfb | 254 | |
va009039 | 0:65f1469d6bfb | 255 | return retval; |
va009039 | 0:65f1469d6bfb | 256 | } |