Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of pymite by
global.c
00001 /* 00002 # This file is Copyright 2002 Dean Hall. 00003 # This file is part of the PyMite VM. 00004 # This file is licensed under the MIT License. 00005 # See the LICENSE file for details. 00006 */ 00007 00008 00009 #undef __FILE_ID__ 00010 #define __FILE_ID__ 0x05 00011 00012 00013 /** 00014 * \file 00015 * \brief VM Globals 00016 * 00017 * VM globals operations. 00018 * PyMite's global struct def and initial values. 00019 */ 00020 00021 00022 #include "pm.h" 00023 00024 00025 extern unsigned char const *stdlib_img; 00026 00027 static uint8_t const *bistr = (uint8_t const *)"__bi"; 00028 00029 00030 /** Most PyMite globals all in one convenient place */ 00031 volatile PmVmGlobal_t gVmGlobal; 00032 00033 00034 PmReturn_t 00035 global_init(void) 00036 { 00037 PmReturn_t retval; 00038 uint8_t *codestr = (uint8_t *)"code"; 00039 uint8_t *pchunk; 00040 pPmObj_t pobj; 00041 #ifdef HAVE_CLASSES 00042 uint8_t const *initstr = (uint8_t const *)"__init__"; 00043 #endif /* HAVE_CLASSES */ 00044 #ifdef HAVE_GENERATORS 00045 uint8_t const *genstr = (uint8_t const *)"Generator"; 00046 uint8_t const *nextstr = (uint8_t const *)"next"; 00047 #endif /* HAVE_GENERATORS */ 00048 #ifdef HAVE_ASSERT 00049 uint8_t const *exnstr = (uint8_t const *)"Exception"; 00050 #endif /* HAVE_ASSERT */ 00051 #ifdef HAVE_BYTEARRAY 00052 uint8_t const *pbastr = (uint8_t const *)"bytearray"; 00053 #endif /* HAVE_BYTEARRAY */ 00054 uint8_t const *pmdstr = (uint8_t const *)"__md"; 00055 00056 /* Clear the global struct */ 00057 sli_memset((uint8_t *)&gVmGlobal, '\0', sizeof(PmVmGlobal_t)); 00058 00059 /* Set the PyMite release num (for debug and post mortem) */ 00060 gVmGlobal.errVmRelease = PM_RELEASE; 00061 00062 /* Init zero */ 00063 retval = heap_getChunk(sizeof(PmInt_t), &pchunk); 00064 PM_RETURN_IF_ERROR(retval); 00065 pobj = (pPmObj_t)pchunk; 00066 OBJ_SET_TYPE(pobj, OBJ_TYPE_INT); 00067 ((pPmInt_t)pobj)->val = (int32_t)0; 00068 gVmGlobal.pzero = (pPmInt_t)pobj; 00069 00070 /* Init one */ 00071 retval = heap_getChunk(sizeof(PmInt_t), &pchunk); 00072 PM_RETURN_IF_ERROR(retval); 00073 pobj = (pPmObj_t)pchunk; 00074 OBJ_SET_TYPE(pobj, OBJ_TYPE_INT); 00075 ((pPmInt_t)pobj)->val = (int32_t)1; 00076 gVmGlobal.pone = (pPmInt_t)pobj; 00077 00078 /* Init negone */ 00079 retval = heap_getChunk(sizeof(PmInt_t), &pchunk); 00080 PM_RETURN_IF_ERROR(retval); 00081 pobj = (pPmObj_t)pchunk; 00082 OBJ_SET_TYPE(pobj, OBJ_TYPE_INT); 00083 ((pPmInt_t)pobj)->val = (int32_t)-1; 00084 gVmGlobal.pnegone = (pPmInt_t)pobj; 00085 00086 /* Init False */ 00087 retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk); 00088 PM_RETURN_IF_ERROR(retval); 00089 pobj = (pPmObj_t)pchunk; 00090 OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL); 00091 ((pPmBoolean_t) pobj)->val = (int32_t)C_FALSE; 00092 gVmGlobal.pfalse = (pPmInt_t)pobj; 00093 00094 /* Init True */ 00095 retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk); 00096 PM_RETURN_IF_ERROR(retval); 00097 pobj = (pPmObj_t)pchunk; 00098 OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL); 00099 ((pPmBoolean_t) pobj)->val = (int32_t)C_TRUE; 00100 gVmGlobal.ptrue = (pPmInt_t)pobj; 00101 00102 /* Init None */ 00103 retval = heap_getChunk(sizeof(PmObj_t), &pchunk); 00104 PM_RETURN_IF_ERROR(retval); 00105 pobj = (pPmObj_t)pchunk; 00106 OBJ_SET_TYPE(pobj, OBJ_TYPE_NON); 00107 gVmGlobal.pnone = pobj; 00108 00109 /* Init "code" string obj */ 00110 retval = string_new((uint8_t const **)&codestr, &pobj); 00111 PM_RETURN_IF_ERROR(retval); 00112 gVmGlobal.pcodeStr = (pPmString_t)pobj; 00113 00114 #ifdef HAVE_CLASSES 00115 /* Init "__init__" string obj */ 00116 retval = string_new((uint8_t const **)&initstr, &pobj); 00117 PM_RETURN_IF_ERROR(retval); 00118 gVmGlobal.pinitStr = (pPmString_t)pobj; 00119 #endif /* HAVE_CLASSES */ 00120 00121 #ifdef HAVE_GENERATORS 00122 /* Init "Generator" string obj */ 00123 retval = string_new((uint8_t const **)&genstr, &pobj); 00124 PM_RETURN_IF_ERROR(retval); 00125 gVmGlobal.pgenStr = (pPmString_t)pobj; 00126 00127 /* Init "next" string obj */ 00128 retval = string_new((uint8_t const **)&nextstr, &pobj); 00129 PM_RETURN_IF_ERROR(retval); 00130 gVmGlobal.pnextStr = (pPmString_t)pobj; 00131 #endif /* HAVE_GENERATORS */ 00132 00133 #ifdef HAVE_ASSERT 00134 /* Init "Exception" string obj */ 00135 retval = string_new((uint8_t const **)&exnstr, &pobj); 00136 PM_RETURN_IF_ERROR(retval); 00137 gVmGlobal.pexnStr = (pPmString_t)pobj; 00138 #endif /* HAVE_ASSERT */ 00139 00140 #ifdef HAVE_BYTEARRAY 00141 /* Init "bytearray" string obj */ 00142 retval = string_new((uint8_t const **)&pbastr, &pobj); 00143 PM_RETURN_IF_ERROR(retval); 00144 gVmGlobal.pbaStr = (pPmString_t)pobj; 00145 #endif /* HAVE_BYTEARRAY */ 00146 00147 /* Init "__md" string obj */ 00148 retval = string_new((uint8_t const **)&pmdstr, &pobj); 00149 PM_RETURN_IF_ERROR(retval); 00150 gVmGlobal.pmdStr = (pPmString_t)pobj; 00151 00152 /* Init empty builtins */ 00153 gVmGlobal.builtins = C_NULL; 00154 00155 /* Init native frame */ 00156 gVmGlobal.nativeframe.od = sizeof(PmNativeFrame_t); 00157 OBJ_SET_TYPE(&gVmGlobal.nativeframe, OBJ_TYPE_NFM); 00158 gVmGlobal.nativeframe.nf_func = C_NULL; 00159 gVmGlobal.nativeframe.nf_stack = C_NULL; 00160 gVmGlobal.nativeframe.nf_active = C_FALSE; 00161 gVmGlobal.nativeframe.nf_numlocals = 0; 00162 00163 /* Create empty threadList */ 00164 retval = list_new(&pobj); 00165 gVmGlobal.threadList = (pPmList_t)pobj; 00166 00167 /* Init the PmImgPaths with std image info */ 00168 gVmGlobal.imgPaths.memspace[0] = MEMSPACE_PROG; 00169 gVmGlobal.imgPaths.pimg[0] = (uint8_t *)&stdlib_img; 00170 gVmGlobal.imgPaths.pathcount = 1; 00171 00172 #ifdef HAVE_PRINT 00173 gVmGlobal.needSoftSpace = C_FALSE; 00174 gVmGlobal.somethingPrinted = C_FALSE; 00175 #endif /* HAVE_PRINT */ 00176 00177 return retval; 00178 } 00179 00180 00181 PmReturn_t 00182 global_setBuiltins(pPmFunc_t pmod) 00183 { 00184 PmReturn_t retval = PM_RET_OK; 00185 pPmObj_t pkey = C_NULL; 00186 uint8_t const *pbistr = bistr; 00187 uint8_t objid; 00188 00189 if (PM_PBUILTINS == C_NULL) 00190 { 00191 /* Need to load builtins first */ 00192 global_loadBuiltins(); 00193 } 00194 00195 /* Put builtins module in the module's attrs dict */ 00196 retval = string_new(&pbistr, &pkey); 00197 PM_RETURN_IF_ERROR(retval); 00198 00199 heap_gcPushTempRoot(pkey, &objid); 00200 retval = dict_setItem((pPmObj_t)pmod->f_attrs, pkey, PM_PBUILTINS); 00201 heap_gcPopTempRoot(objid); 00202 00203 return retval; 00204 } 00205 00206 00207 PmReturn_t 00208 global_loadBuiltins(void) 00209 { 00210 PmReturn_t retval = PM_RET_OK; 00211 pPmObj_t pkey = C_NULL; 00212 uint8_t const *nonestr = (uint8_t const *)"None"; 00213 uint8_t const *falsestr = (uint8_t const *)"False"; 00214 uint8_t const *truestr = (uint8_t const *)"True"; 00215 pPmObj_t pstr = C_NULL; 00216 pPmObj_t pbimod; 00217 uint8_t const *pbistr = bistr; 00218 00219 /* Import the builtins */ 00220 retval = string_new(&pbistr, &pstr); 00221 PM_RETURN_IF_ERROR(retval); 00222 retval = mod_import(pstr, &pbimod); 00223 PM_RETURN_IF_ERROR(retval); 00224 00225 /* Must interpret builtins' root code to set the attrs */ 00226 C_ASSERT(gVmGlobal.threadList->length == 0); 00227 interp_addThread((pPmFunc_t)pbimod); 00228 retval = interpret(INTERP_RETURN_ON_NO_THREADS); 00229 PM_RETURN_IF_ERROR(retval); 00230 00231 /* Builtins points to the builtins module's attrs dict */ 00232 gVmGlobal.builtins = ((pPmFunc_t)pbimod)->f_attrs; 00233 00234 /* Set None manually */ 00235 retval = string_new(&nonestr, &pkey); 00236 PM_RETURN_IF_ERROR(retval); 00237 retval = dict_setItem(PM_PBUILTINS, pkey, PM_NONE); 00238 PM_RETURN_IF_ERROR(retval); 00239 00240 /* Set False manually */ 00241 retval = string_new(&falsestr, &pkey); 00242 PM_RETURN_IF_ERROR(retval); 00243 retval = dict_setItem(PM_PBUILTINS, pkey, PM_FALSE); 00244 PM_RETURN_IF_ERROR(retval); 00245 00246 /* Set True manually */ 00247 retval = string_new(&truestr, &pkey); 00248 PM_RETURN_IF_ERROR(retval); 00249 retval = dict_setItem(PM_PBUILTINS, pkey, PM_TRUE); 00250 PM_RETURN_IF_ERROR(retval); 00251 00252 /* Deallocate builtins module */ 00253 retval = heap_freeChunk((pPmObj_t)pbimod); 00254 00255 return retval; 00256 }
Generated on Tue Jul 12 2022 21:25:46 by
1.7.2
