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.
17 #define __FILE_ID__ 0x05
24 * VM globals operations.
25 * PyMite's global struct def and initial values.
32 extern unsigned char const *stdlib_img
;
34 static uint8_t const *bistr
= (uint8_t const *)"__bi";
37 /** Most PyMite globals all in one convenient place */
38 volatile PmVmGlobal_t gVmGlobal
;
45 uint8_t *codestr
= (uint8_t *)"code";
49 uint8_t const *initstr
= (uint8_t const *)"__init__";
50 #endif /* HAVE_CLASSES */
51 #ifdef HAVE_GENERATORS
52 uint8_t const *genstr
= (uint8_t const *)"Generator";
53 uint8_t const *nextstr
= (uint8_t const *)"next";
54 #endif /* HAVE_GENERATORS */
56 uint8_t const *exnstr
= (uint8_t const *)"Exception";
57 #endif /* HAVE_ASSERT */
59 uint8_t const *pbastr
= (uint8_t const *)"bytearray";
60 #endif /* HAVE_BYTEARRAY */
62 /* Clear the global struct */
63 sli_memset((uint8_t *)&gVmGlobal
, '\0', sizeof(PmVmGlobal_t
));
65 /* Set the PyMite release num (for debug and post mortem) */
66 gVmGlobal
.errVmRelease
= PM_RELEASE
;
69 retval
= heap_getChunk(sizeof(PmInt_t
), &pchunk
);
70 PM_RETURN_IF_ERROR(retval
);
71 pobj
= (pPmObj_t
)pchunk
;
72 OBJ_SET_TYPE(pobj
, OBJ_TYPE_INT
);
73 ((pPmInt_t
)pobj
)->val
= (int32_t)0;
74 gVmGlobal
.pzero
= (pPmInt_t
)pobj
;
77 retval
= heap_getChunk(sizeof(PmInt_t
), &pchunk
);
78 PM_RETURN_IF_ERROR(retval
);
79 pobj
= (pPmObj_t
)pchunk
;
80 OBJ_SET_TYPE(pobj
, OBJ_TYPE_INT
);
81 ((pPmInt_t
)pobj
)->val
= (int32_t)1;
82 gVmGlobal
.pone
= (pPmInt_t
)pobj
;
85 retval
= heap_getChunk(sizeof(PmInt_t
), &pchunk
);
86 PM_RETURN_IF_ERROR(retval
);
87 pobj
= (pPmObj_t
)pchunk
;
88 OBJ_SET_TYPE(pobj
, OBJ_TYPE_INT
);
89 ((pPmInt_t
)pobj
)->val
= (int32_t)-1;
90 gVmGlobal
.pnegone
= (pPmInt_t
)pobj
;
93 retval
= heap_getChunk(sizeof(PmBoolean_t
), &pchunk
);
94 PM_RETURN_IF_ERROR(retval
);
95 pobj
= (pPmObj_t
)pchunk
;
96 OBJ_SET_TYPE(pobj
, OBJ_TYPE_BOOL
);
97 ((pPmBoolean_t
) pobj
)->val
= (int32_t)C_FALSE
;
98 gVmGlobal
.pfalse
= (pPmInt_t
)pobj
;
101 retval
= heap_getChunk(sizeof(PmBoolean_t
), &pchunk
);
102 PM_RETURN_IF_ERROR(retval
);
103 pobj
= (pPmObj_t
)pchunk
;
104 OBJ_SET_TYPE(pobj
, OBJ_TYPE_BOOL
);
105 ((pPmBoolean_t
) pobj
)->val
= (int32_t)C_TRUE
;
106 gVmGlobal
.ptrue
= (pPmInt_t
)pobj
;
109 retval
= heap_getChunk(sizeof(PmObj_t
), &pchunk
);
110 PM_RETURN_IF_ERROR(retval
);
111 pobj
= (pPmObj_t
)pchunk
;
112 OBJ_SET_TYPE(pobj
, OBJ_TYPE_NON
);
113 gVmGlobal
.pnone
= pobj
;
115 /* Init "code" string obj */
116 retval
= string_new((uint8_t const **)&codestr
, &pobj
);
117 PM_RETURN_IF_ERROR(retval
);
118 gVmGlobal
.pcodeStr
= (pPmString_t
)pobj
;
121 /* Init "__init__" string obj */
122 retval
= string_new((uint8_t const **)&initstr
, &pobj
);
123 PM_RETURN_IF_ERROR(retval
);
124 gVmGlobal
.pinitStr
= (pPmString_t
)pobj
;
125 #endif /* HAVE_CLASSES */
127 #ifdef HAVE_GENERATORS
128 /* Init "Generator" string obj */
129 retval
= string_new((uint8_t const **)&genstr
, &pobj
);
130 PM_RETURN_IF_ERROR(retval
);
131 gVmGlobal
.pgenStr
= (pPmString_t
)pobj
;
133 /* Init "next" string obj */
134 retval
= string_new((uint8_t const **)&nextstr
, &pobj
);
135 PM_RETURN_IF_ERROR(retval
);
136 gVmGlobal
.pnextStr
= (pPmString_t
)pobj
;
137 #endif /* HAVE_GENERATORS */
140 /* Init "Exception" string obj */
141 retval
= string_new((uint8_t const **)&exnstr
, &pobj
);
142 PM_RETURN_IF_ERROR(retval
);
143 gVmGlobal
.pexnStr
= (pPmString_t
)pobj
;
144 #endif /* HAVE_ASSERT */
146 #ifdef HAVE_BYTEARRAY
147 /* Init "bytearray" string obj */
148 retval
= string_new((uint8_t const **)&pbastr
, &pobj
);
149 PM_RETURN_IF_ERROR(retval
);
150 gVmGlobal
.pbaStr
= (pPmString_t
)pobj
;
151 #endif /* HAVE_BYTEARRAY */
153 /* Init empty builtins */
154 gVmGlobal
.builtins
= C_NULL
;
156 /* Init native frame */
157 OBJ_SET_SIZE(&gVmGlobal
.nativeframe
, sizeof(PmNativeFrame_t
));
158 OBJ_SET_TYPE(&gVmGlobal
.nativeframe
, OBJ_TYPE_NFM
);
159 gVmGlobal
.nativeframe
.nf_func
= C_NULL
;
160 gVmGlobal
.nativeframe
.nf_stack
= C_NULL
;
161 gVmGlobal
.nativeframe
.nf_active
= C_FALSE
;
162 gVmGlobal
.nativeframe
.nf_numlocals
= 0;
164 /* Create empty threadList */
165 retval
= list_new(&pobj
);
166 gVmGlobal
.threadList
= (pPmList_t
)pobj
;
168 /* Init the PmImgPaths with std image info */
169 gVmGlobal
.imgPaths
.memspace
[0] = MEMSPACE_PROG
;
170 gVmGlobal
.imgPaths
.pimg
[0] = (uint8_t *)&stdlib_img
;
171 gVmGlobal
.imgPaths
.pathcount
= 1;
174 gVmGlobal
.needSoftSpace
= C_FALSE
;
175 gVmGlobal
.somethingPrinted
= C_FALSE
;
176 #endif /* HAVE_PRINT */
183 global_setBuiltins(pPmFunc_t pmod
)
185 PmReturn_t retval
= PM_RET_OK
;
186 pPmObj_t pkey
= C_NULL
;
187 uint8_t const *pbistr
= bistr
;
190 if (PM_PBUILTINS
== C_NULL
)
192 /* Need to load builtins first */
193 global_loadBuiltins();
196 /* Put builtins module in the module's attrs dict */
197 retval
= string_new(&pbistr
, &pkey
);
198 PM_RETURN_IF_ERROR(retval
);
200 heap_gcPushTempRoot(pkey
, &objid
);
201 retval
= dict_setItem((pPmObj_t
)pmod
->f_attrs
, pkey
, PM_PBUILTINS
);
202 heap_gcPopTempRoot(objid
);
209 global_loadBuiltins(void)
211 PmReturn_t retval
= PM_RET_OK
;
212 pPmObj_t pkey
= C_NULL
;
213 uint8_t const *nonestr
= (uint8_t const *)"None";
214 uint8_t const *falsestr
= (uint8_t const *)"False";
215 uint8_t const *truestr
= (uint8_t const *)"True";
216 pPmObj_t pstr
= C_NULL
;
218 uint8_t const *pbistr
= bistr
;
220 /* Import the builtins */
221 retval
= string_new(&pbistr
, &pstr
);
222 PM_RETURN_IF_ERROR(retval
);
223 retval
= mod_import(pstr
, &pbimod
);
224 PM_RETURN_IF_ERROR(retval
);
226 /* Must interpret builtins' root code to set the attrs */
227 C_ASSERT(gVmGlobal
.threadList
->length
== 0);
228 interp_addThread((pPmFunc_t
)pbimod
);
229 retval
= interpret(INTERP_RETURN_ON_NO_THREADS
);
230 PM_RETURN_IF_ERROR(retval
);
232 /* Builtins points to the builtins module's attrs dict */
233 gVmGlobal
.builtins
= ((pPmFunc_t
)pbimod
)->f_attrs
;
235 /* Set None manually */
236 retval
= string_new(&nonestr
, &pkey
);
237 PM_RETURN_IF_ERROR(retval
);
238 retval
= dict_setItem(PM_PBUILTINS
, pkey
, PM_NONE
);
239 PM_RETURN_IF_ERROR(retval
);
241 /* Set False manually */
242 retval
= string_new(&falsestr
, &pkey
);
243 PM_RETURN_IF_ERROR(retval
);
244 retval
= dict_setItem(PM_PBUILTINS
, pkey
, PM_FALSE
);
245 PM_RETURN_IF_ERROR(retval
);
247 /* Set True manually */
248 retval
= string_new(&truestr
, &pkey
);
249 PM_RETURN_IF_ERROR(retval
);
250 retval
= dict_setItem(PM_PBUILTINS
, pkey
, PM_TRUE
);
251 PM_RETURN_IF_ERROR(retval
);
253 /* Deallocate builtins module */
254 retval
= heap_freeChunk((pPmObj_t
)pbimod
);