global.c

Go to the documentation of this file.
00001 /*
00002 # This file is Copyright 2003, 2006, 2007, 2009, 2010 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 #undef __FILE_ID__
00017 #define __FILE_ID__ 0x05
00018 
00019 
00029 #include "pm.h"
00030 
00031 
00032 extern unsigned char const *stdlib_img;
00033 
00034 static uint8_t const *bistr = (uint8_t const *)"__bi";
00035 
00036 
00038 volatile PmVmGlobal_t gVmGlobal;
00039 
00040 
00041 PmReturn_t
00042 global_init(void)
00043 {
00044     PmReturn_t retval;
00045     uint8_t *codestr = (uint8_t *)"code";
00046     uint8_t *pchunk;
00047     pPmObj_t pobj;
00048 #ifdef HAVE_CLASSES
00049     uint8_t const *initstr = (uint8_t const *)"__init__"; 
00050 #endif /* HAVE_CLASSES */
00051 #ifdef HAVE_GENERATORS
00052     uint8_t const *genstr = (uint8_t const *)"Generator";
00053     uint8_t const *nextstr = (uint8_t const *)"next";
00054 #endif /* HAVE_GENERATORS */
00055 #ifdef HAVE_ASSERT
00056     uint8_t const *exnstr = (uint8_t const *)"Exception";
00057 #endif /* HAVE_ASSERT */
00058 #ifdef HAVE_BYTEARRAY
00059     uint8_t const *pbastr = (uint8_t const *)"bytearray";
00060 #endif /* HAVE_BYTEARRAY */
00061 
00062     /* Clear the global struct */
00063     sli_memset((uint8_t *)&gVmGlobal, '\0', sizeof(PmVmGlobal_t));
00064 
00065     /* Set the PyMite release num (for debug and post mortem) */
00066     gVmGlobal.errVmRelease = PM_RELEASE;
00067 
00068     /* Init zero */
00069     retval = heap_getChunk(sizeof(PmInt_t), &pchunk);
00070     PM_RETURN_IF_ERROR(retval);
00071     pobj = (pPmObj_t)pchunk;
00072     OBJ_SET_TYPE(pobj, OBJ_TYPE_INT);
00073     ((pPmInt_t)pobj)->val = (int32_t)0;
00074     gVmGlobal.pzero = (pPmInt_t)pobj;
00075 
00076     /* Init one */
00077     retval = heap_getChunk(sizeof(PmInt_t), &pchunk);
00078     PM_RETURN_IF_ERROR(retval);
00079     pobj = (pPmObj_t)pchunk;
00080     OBJ_SET_TYPE(pobj, OBJ_TYPE_INT);
00081     ((pPmInt_t)pobj)->val = (int32_t)1;
00082     gVmGlobal.pone = (pPmInt_t)pobj;
00083 
00084     /* Init negone */
00085     retval = heap_getChunk(sizeof(PmInt_t), &pchunk);
00086     PM_RETURN_IF_ERROR(retval);
00087     pobj = (pPmObj_t)pchunk;
00088     OBJ_SET_TYPE(pobj, OBJ_TYPE_INT);
00089     ((pPmInt_t)pobj)->val = (int32_t)-1;
00090     gVmGlobal.pnegone = (pPmInt_t)pobj;
00091 
00092     /* Init False */
00093     retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk);
00094     PM_RETURN_IF_ERROR(retval);
00095     pobj = (pPmObj_t)pchunk;
00096     OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL);
00097     ((pPmBoolean_t) pobj)->val = (int32_t)C_FALSE;
00098     gVmGlobal.pfalse = (pPmInt_t)pobj;
00099 
00100     /* Init True */
00101     retval = heap_getChunk(sizeof(PmBoolean_t), &pchunk);
00102     PM_RETURN_IF_ERROR(retval);
00103     pobj = (pPmObj_t)pchunk;
00104     OBJ_SET_TYPE(pobj, OBJ_TYPE_BOOL);
00105     ((pPmBoolean_t) pobj)->val = (int32_t)C_TRUE;
00106     gVmGlobal.ptrue = (pPmInt_t)pobj;
00107 
00108     /* Init None */
00109     retval = heap_getChunk(sizeof(PmObj_t), &pchunk);
00110     PM_RETURN_IF_ERROR(retval);
00111     pobj = (pPmObj_t)pchunk;
00112     OBJ_SET_TYPE(pobj, OBJ_TYPE_NON);
00113     gVmGlobal.pnone = pobj;
00114 
00115     /* Init "code" string obj */
00116     retval = string_new((uint8_t const **)&codestr, &pobj);
00117     PM_RETURN_IF_ERROR(retval);
00118     gVmGlobal.pcodeStr = (pPmString_t)pobj;
00119 
00120 #ifdef HAVE_CLASSES
00121     /* Init "__init__" string obj */
00122     retval = string_new((uint8_t const **)&initstr, &pobj);
00123     PM_RETURN_IF_ERROR(retval);
00124     gVmGlobal.pinitStr = (pPmString_t)pobj;
00125 #endif /* HAVE_CLASSES */
00126 
00127 #ifdef HAVE_GENERATORS
00128     /* Init "Generator" string obj */
00129     retval = string_new((uint8_t const **)&genstr, &pobj);
00130     PM_RETURN_IF_ERROR(retval);
00131     gVmGlobal.pgenStr = (pPmString_t)pobj;
00132     
00133     /* Init "next" string obj */
00134     retval = string_new((uint8_t const **)&nextstr, &pobj);
00135     PM_RETURN_IF_ERROR(retval);
00136     gVmGlobal.pnextStr = (pPmString_t)pobj;
00137 #endif /* HAVE_GENERATORS */
00138 
00139 #ifdef HAVE_ASSERT
00140     /* Init "Exception" string obj */
00141     retval = string_new((uint8_t const **)&exnstr, &pobj);
00142     PM_RETURN_IF_ERROR(retval);
00143     gVmGlobal.pexnStr = (pPmString_t)pobj;
00144 #endif /* HAVE_ASSERT */
00145 
00146 #ifdef HAVE_BYTEARRAY
00147     /* Init "bytearray" string obj */
00148     retval = string_new((uint8_t const **)&pbastr, &pobj);
00149     PM_RETURN_IF_ERROR(retval);
00150     gVmGlobal.pbaStr = (pPmString_t)pobj;
00151 #endif /* HAVE_BYTEARRAY */
00152 
00153     /* Init empty builtins */
00154     gVmGlobal.builtins = C_NULL;
00155 
00156     /* Init native frame */
00157     OBJ_SET_SIZE(&gVmGlobal.nativeframe, sizeof(PmNativeFrame_t));
00158     OBJ_SET_TYPE(&gVmGlobal.nativeframe, OBJ_TYPE_NFM);
00159     gVmGlobal.nativeframe.nf_func = C_NULL;
00160     gVmGlobal.nativeframe.nf_stack = C_NULL;
00161     gVmGlobal.nativeframe.nf_active = C_FALSE;
00162     gVmGlobal.nativeframe.nf_numlocals = 0;
00163 
00164     /* Create empty threadList */
00165     retval = list_new(&pobj);
00166     gVmGlobal.threadList = (pPmList_t)pobj;
00167 
00168     /* Init the PmImgPaths with std image info */
00169     gVmGlobal.imgPaths.memspace[0] = MEMSPACE_PROG;
00170     gVmGlobal.imgPaths.pimg[0] = (uint8_t *)&stdlib_img;
00171     gVmGlobal.imgPaths.pathcount = 1;
00172 
00173 #ifdef HAVE_PRINT
00174     gVmGlobal.needSoftSpace = C_FALSE;
00175     gVmGlobal.somethingPrinted = C_FALSE;
00176 #endif /* HAVE_PRINT */
00177 
00178     return retval;
00179 }
00180 
00181 
00182 PmReturn_t
00183 global_setBuiltins(pPmFunc_t pmod)
00184 {
00185     PmReturn_t retval = PM_RET_OK;
00186     pPmObj_t pkey = C_NULL;
00187     uint8_t const *pbistr = bistr;
00188     uint8_t objid;
00189 
00190     if (PM_PBUILTINS == C_NULL)
00191     {
00192         /* Need to load builtins first */
00193         global_loadBuiltins();
00194     }
00195 
00196     /* Put builtins module in the module's attrs dict */
00197     retval = string_new(&pbistr, &pkey);
00198     PM_RETURN_IF_ERROR(retval);
00199 
00200     heap_gcPushTempRoot(pkey, &objid);
00201     retval = dict_setItem((pPmObj_t)pmod->f_attrs, pkey, PM_PBUILTINS);
00202     heap_gcPopTempRoot(objid);
00203 
00204     return retval;
00205 }
00206 
00207 
00208 PmReturn_t
00209 global_loadBuiltins(void)
00210 {
00211     PmReturn_t retval = PM_RET_OK;
00212     pPmObj_t pkey = C_NULL;
00213     uint8_t const *nonestr = (uint8_t const *)"None";
00214     uint8_t const *falsestr = (uint8_t const *)"False";
00215     uint8_t const *truestr = (uint8_t const *)"True";
00216     pPmObj_t pstr = C_NULL;
00217     pPmObj_t pbimod;
00218     uint8_t const *pbistr = bistr;
00219 
00220     /* Import the builtins */
00221     retval = string_new(&pbistr, &pstr);
00222     PM_RETURN_IF_ERROR(retval);
00223     retval = mod_import(pstr, &pbimod);
00224     PM_RETURN_IF_ERROR(retval);
00225 
00226     /* Must interpret builtins' root code to set the attrs */
00227     C_ASSERT(gVmGlobal.threadList->length == 0);
00228     interp_addThread((pPmFunc_t)pbimod);
00229     retval = interpret(INTERP_RETURN_ON_NO_THREADS);
00230     PM_RETURN_IF_ERROR(retval);
00231 
00232     /* Builtins points to the builtins module's attrs dict */
00233     gVmGlobal.builtins = ((pPmFunc_t)pbimod)->f_attrs;
00234 
00235     /* Set None manually */
00236     retval = string_new(&nonestr, &pkey);
00237     PM_RETURN_IF_ERROR(retval);
00238     retval = dict_setItem(PM_PBUILTINS, pkey, PM_NONE);
00239     PM_RETURN_IF_ERROR(retval);
00240 
00241     /* Set False manually */
00242     retval = string_new(&falsestr, &pkey);
00243     PM_RETURN_IF_ERROR(retval);
00244     retval = dict_setItem(PM_PBUILTINS, pkey, PM_FALSE);
00245     PM_RETURN_IF_ERROR(retval);
00246 
00247     /* Set True manually */
00248     retval = string_new(&truestr, &pkey);
00249     PM_RETURN_IF_ERROR(retval);
00250     retval = dict_setItem(PM_PBUILTINS, pkey, PM_TRUE);
00251     PM_RETURN_IF_ERROR(retval);
00252 
00253     /* Deallocate builtins module */
00254     retval = heap_freeChunk((pPmObj_t)pbimod);
00255 
00256     return retval;
00257 }

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9