1. tuck
  2. pymite

Commits

Dean Hall  committed 663deb7

Fixes issue 207. Refactor globals and initialization

  • Participants
  • Parent commits 34a2530
  • Branches v10

Comments (0)

Files changed (7)

File src/platform/posix/main.c

View file
  • Ignore whitespace
     uint8_t heap[HEAP_SIZE];
     PmReturn_t retval;
 
+    retval = plat_init();
+    PM_RETURN_IF_ERROR(retval);
+
     retval = pm_init(heap, HEAP_SIZE);
     PM_RETURN_IF_ERROR(retval);
 

File src/tools/pmCoCreator.py

View file
  • Ignore whitespace
 
 
 def process_globals():
-    """Adds VM globals and useful constants to the constant pool"""
+    """Adds VM globals and useful constants to the constant pool.
+    """
     cobjs = (None, -1,0,1,2,3,4,5,6,7,8,9,True,False,
              "__bi","__md","code","__init__","next",(),"",
-             "Generator", "Exception", "bytearray")
+             "Generator", "Exception", "bytearray",
+             "None", "False", "True")
     cnames = map(lambda x: GLOBAL_PREFIX + x,
                  ("none", "negone", "zero", "one", "two", "three", "four",
                   "five", "six", "seven", "eight", "nine", "true", "false",
                   "string_bi", "string_md", "string_code", "string_init",
                   "string_next", "empty_tuple", "empty_string",
-                  "string_generator", "string_exception", "string_bytearray"))
+                  "string_generator", "string_exception", "string_bytearray",
+                  "string_none", "string_false", "string_true",))
     map(obj_to_cvar, cobjs, cnames)
 
 

File src/vm/fileid.txt

View file
  • Ignore whitespace
 0x02  dict.c
 0x03  frame.c
 0x04  func.c
-0x05  global.c
+0x05  UNUSED (used to be global.c)
 0x06  heap.c
 0x07  img.c
 0x08  int.c

File src/vm/global.c

  • Ignore whitespace
-/*
-# This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
-#
-# This file is part of the PyMite VM.
-# The PyMite VM is free software: you can redistribute it and/or modify
-# it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
-#
-# The PyMite VM is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-# A copy of the GNU GENERAL PUBLIC LICENSE Version 2
-# is seen in the file COPYING in this directory.
-*/
-
-
-#undef __FILE_ID__
-#define __FILE_ID__ 0x05
-
-
-/**
- * \file
- * \brief VM Globals
- *
- * VM globals operations.
- * PyMite's global struct def and initial values.
- */
-
-
-#include "pm.h"
-
-
-extern unsigned char const *stdlib_img;
-
-static uint8_t const *bistr = (uint8_t const *)"__bi";
-
-
-/** Most PyMite globals all in one convenient place */
-volatile PmVmGlobal_t gVmGlobal;
-
-
-PmReturn_t
-global_init(void)
-{
-    PmReturn_t retval;
-    pPmObj_t pobj;
-
-    /* Clear the global struct */
-    sli_memset((uint8_t *)&gVmGlobal, '\0', sizeof(PmVmGlobal_t));
-
-    /* Set the PyMite release num (for debug and post mortem) */
-    gVmGlobal.errVmRelease = PM_RELEASE;
-
-    /* Init empty builtins */
-    gVmGlobal.builtins = C_NULL;
-
-    /* Init native frame */
-    OBJ_SET_SIZE(&gVmGlobal.nativeframe, sizeof(PmNativeFrame_t));
-    OBJ_SET_TYPE(&gVmGlobal.nativeframe, OBJ_TYPE_NFM);
-    gVmGlobal.nativeframe.nf_func = C_NULL;
-    gVmGlobal.nativeframe.nf_stack = C_NULL;
-    gVmGlobal.nativeframe.nf_active = C_FALSE;
-    gVmGlobal.nativeframe.nf_numlocals = 0;
-
-    /* Create empty threadList */
-    retval = list_new(&pobj);
-    gVmGlobal.threadList = (pPmList_t)pobj;
-    gVmGlobal.needSoftSpace = C_FALSE;
-    gVmGlobal.somethingPrinted = C_FALSE;
-
-    return retval;
-}
-
-
-PmReturn_t
-global_setBuiltins(pPmFunc_t pmod)
-{
-    PmReturn_t retval = PM_RET_OK;
-    pPmObj_t pkey = C_NULL;
-    uint8_t const *pbistr = bistr;
-    uint8_t objid;
-
-    if (PM_PBUILTINS == C_NULL)
-    {
-        /* Need to load builtins first */
-        global_loadBuiltins();
-    }
-
-    /* Put builtins module in the module's attrs dict */
-    retval = string_new(&pbistr, &pkey);
-    PM_RETURN_IF_ERROR(retval);
-
-    heap_gcPushTempRoot(pkey, &objid);
-    retval = dict_setItem((pPmObj_t)pmod->f_attrs, pkey, PM_PBUILTINS);
-    heap_gcPopTempRoot(objid);
-
-    return retval;
-}
-
-
-PmReturn_t
-global_loadBuiltins(void)
-{
-    PmReturn_t retval = PM_RET_OK;
-    pPmObj_t pkey = C_NULL;
-    uint8_t const *nonestr = (uint8_t const *)"None";
-    uint8_t const *falsestr = (uint8_t const *)"False";
-    uint8_t const *truestr = (uint8_t const *)"True";
-    pPmObj_t pstr = C_NULL;
-    pPmObj_t pbimod;
-    uint8_t const *pbistr = bistr;
-
-    /* Import the builtins */
-    retval = string_new(&pbistr, &pstr);
-    PM_RETURN_IF_ERROR(retval);
-    retval = mod_import(PM_BI_STR, &pbimod);
-    PM_RETURN_IF_ERROR(retval);
-
-    /* Must interpret builtins' root code to set the attrs */
-    C_ASSERT(gVmGlobal.threadList->length == 0);
-    interp_addThread((pPmFunc_t)pbimod);
-    retval = interpret(INTERP_RETURN_ON_NO_THREADS);
-    PM_RETURN_IF_ERROR(retval);
-
-    /* Builtins points to the builtins module's attrs dict */
-    gVmGlobal.builtins = ((pPmFunc_t)pbimod)->f_attrs;
-
-    /* Set None manually */
-    retval = string_new(&nonestr, &pkey);
-    PM_RETURN_IF_ERROR(retval);
-    retval = dict_setItem(PM_PBUILTINS, pkey, PM_NONE);
-    PM_RETURN_IF_ERROR(retval);
-
-    /* Set False manually */
-    retval = string_new(&falsestr, &pkey);
-    PM_RETURN_IF_ERROR(retval);
-    retval = dict_setItem(PM_PBUILTINS, pkey, PM_FALSE);
-    PM_RETURN_IF_ERROR(retval);
-
-    /* Set True manually */
-    retval = string_new(&truestr, &pkey);
-    PM_RETURN_IF_ERROR(retval);
-    retval = dict_setItem(PM_PBUILTINS, pkey, PM_TRUE);
-    PM_RETURN_IF_ERROR(retval);
-
-    /* Deallocate builtins module */
-    retval = heap_freeChunk((pPmObj_t)pbimod);
-
-    return retval;
-}

File src/vm/global.h

  • Ignore whitespace
-/*
-# This file is Copyright 2003, 2006, 2007, 2009, 2010 Dean Hall.
-#
-# This file is part of the PyMite VM.
-# The PyMite VM is free software: you can redistribute it and/or modify
-# it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
-#
-# The PyMite VM is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-# A copy of the GNU GENERAL PUBLIC LICENSE Version 2
-# is seen in the file COPYING in this directory.
-*/
-
-
-#ifndef __GLOBAL_H__
-#define __GLOBAL_H__
-
-
-/**
- * \file
- * \brief VM Globals
- *
- * VM globals header.
- */
-
-
-/** The global root PmGlobals Dict object */
-#define PM_PBUILTINS    (pPmObj_t)(gVmGlobal.builtins)
-
-/** The global None object */
-#define PM_NONE         (pPmObj_t)&pm_global_none
-
-/** The global False object */
-#define PM_FALSE        (pPmObj_t)&pm_global_false
-
-/** The global True object */
-#define PM_TRUE         (pPmObj_t)&pm_global_true
-
-/** The global integer 0 object */
-#define PM_ZERO         (pPmObj_t)&pm_global_zero
-
-/** The global integer 1 object */
-#define PM_ONE          (pPmObj_t)&pm_global_one
-
-/** The global integer -1 object */
-#define PM_NEGONE       (pPmObj_t)&pm_global_negone
-
-/** The global string "code" */
-#define PM_CODE_STR     (pPmObj_t)&pm_global_string_code
-
-/** The global string "__init__" */
-#define PM_INIT_STR     (pPmObj_t)&pm_global_string_init
-
-/** The global string "Generator" */
-#define PM_GENERATOR_STR (pPmObj_t)&pm_global_string_generator
-/** The global string "next" */
-#define PM_NEXT_STR (pPmObj_t)&pm_global_string_next
-
-/** The global string "Exception" */
-#define PM_EXCEPTION_STR (pPmObj_t)&pm_global_string_exception
-
-/** The global string "bytearray" */
-#define PM_BYTEARRAY_STR (pPmObj_t)&pm_global_string_bytearray
-
-/** The global string "__md" */
-#define PM_MD_STR (pPmObj_t)&pm_global_string_md
-
-/** The global string "__bi" */
-#define PM_BI_STR (pPmObj_t)&pm_global_string_bi
-
-
-/**
- * This struct contains ALL of PyMite's globals
- */
-typedef struct PmVmGlobal_s
-{
-    /** Dict for builtins */
-    pPmDict_t builtins;
-
-    /** The single native frame.  Static alloc so it won't be GC'd */
-    PmNativeFrame_t nativeframe;
-
-    /** PyMite release value for when an error occurs */
-    uint8_t errVmRelease;
-
-    /** PyMite source file ID number for when an error occurs */
-    uint8_t errFileId;
-
-    /** Line number for when an error occurs */
-    uint16_t errLineNum;
-
-    /** Thread list */
-    pPmList_t threadList;
-
-    /** Ptr to current thread */
-    pPmThread_t pthread;
-
-    /** Remembers when a space is needed before printing the next object */
-    uint8_t needSoftSpace;
-
-    /** Remembers when something has printed since the last newline */
-    uint8_t somethingPrinted;
-
-    /** Flag to trigger rescheduling */
-    uint8_t reschedule;
-} PmVmGlobal_t,
- *pPmVmGlobal_t;
-
-
-extern volatile PmVmGlobal_t gVmGlobal;
-
-
-/**
- * Initializes the global struct
- *
- * @return Return status
- */
-PmReturn_t global_init(void);
-
-/**
- * Sets the builtins dict into the given module's attrs.
- *
- * If not yet done, loads the "__bt" module via global_loadBuiltins().
- * Restrictions described in that functions documentation apply.
- *
- * @param pmod Module whose attrs receive builtins
- * @return Return status
- */
-PmReturn_t global_setBuiltins(pPmFunc_t pmod);
-
-/**
- * Loads the "__bt" module and sets the builtins dict (PM_PBUILTINS)
- * to point to __bt's attributes dict.
- * Creates "None" = None entry in builtins.
- *
- * When run, there should not be any other threads in the interpreter
- * thread list yet.
- *
- * @return  Return status
- */
-PmReturn_t global_loadBuiltins(void);
-
-#endif /* __GLOBAL_H__ */

File src/vm/pm.c

View file
  • Ignore whitespace
 /** Stores tick timestamp of last scheduler run */
 volatile uint32_t pm_lastRescheduleTimestamp = 0;
 
+/** Most PyMite globals all in one convenient place */
+volatile PmVmGlobal_t gVmGlobal;
+
+
+/**
+ * Loads the "__bt" module and sets the builtins dict (PM_PBUILTINS)
+ * to point to __bt's attributes dict.
+ * Creates "None" = None entry in builtins.
+ *
+ * When run, there should not be any other threads in the interpreter
+ * thread list yet.
+ *
+ * @return  Return status
+ */
+static PmReturn_t
+pm_loadBuiltins(void)
+{
+    PmReturn_t retval = PM_RET_OK;
+    pPmObj_t pbimod;
+
+    /* Import the builtins */
+    retval = mod_import(PM_BI_STR, &pbimod);
+    PM_RETURN_IF_ERROR(retval);
+
+    /* Must interpret builtins' root code to set the attrs */
+    C_ASSERT(gVmGlobal.threadList->length == 0);
+    interp_addThread((pPmFunc_t)pbimod);
+    retval = interpret(INTERP_RETURN_ON_NO_THREADS);
+    PM_RETURN_IF_ERROR(retval);
+
+    /* Builtins points to the builtins module's attrs dict */
+    gVmGlobal.pbuiltins = ((pPmFunc_t)pbimod)->f_attrs;
+
+    /* Set None, False and True */
+    retval = dict_setItem(PM_PBUILTINS, (pPmObj_t)&pm_global_string_none, PM_NONE);
+    PM_RETURN_IF_ERROR(retval);
+    retval = dict_setItem(PM_PBUILTINS, (pPmObj_t)&pm_global_string_false, PM_FALSE);
+    PM_RETURN_IF_ERROR(retval);
+    retval = dict_setItem(PM_PBUILTINS, (pPmObj_t)&pm_global_string_true, PM_TRUE);
+    PM_RETURN_IF_ERROR(retval);
+
+    /* Deallocate builtins module */
+    retval = heap_freeChunk((pPmObj_t)pbimod);
+
+    return retval;
+}
+
 
 PmReturn_t
-pm_init(uint8_t *heap_base, uint32_t heap_size
-        /*DWH PmMemSpace_t memspace, uint8_t const * const pusrimg*/)
+pm_init(uint8_t *heap_base, uint32_t heap_size)
 {
     PmReturn_t retval;
-
-    /* Initialize the hardware platform */
-    retval = plat_init();
-    PM_RETURN_IF_ERROR(retval);
+    pPmObj_t pobj;
 
     /* Initialize the heap and the globals */
     retval = heap_init(heap_base, heap_size);
     PM_RETURN_IF_ERROR(retval);
 
-    retval = global_init();
-    PM_RETURN_IF_ERROR(retval);
+    /* Clear the global struct */
+    sli_memset((uint8_t *)&gVmGlobal, '\0', sizeof(PmVmGlobal_t));
 
-    /* Load usr image info if given */
-/*DWH
-    if (pusrimg != C_NULL)
-    {
-        retval = img_appendToPath(memspace, pusrimg);
-    }
-*/
+    /* Set the PyMite release num (for debug and post mortem) */
+    gVmGlobal.errVmRelease = PM_RELEASE;
+
+    /* Init native frame */
+    OBJ_SET_SIZE(&gVmGlobal.nativeframe, sizeof(PmNativeFrame_t));
+    OBJ_SET_TYPE(&gVmGlobal.nativeframe, OBJ_TYPE_NFM);
+    gVmGlobal.nativeframe.nf_func = C_NULL;
+    gVmGlobal.nativeframe.nf_stack = C_NULL;
+    gVmGlobal.nativeframe.nf_active = C_FALSE;
+    gVmGlobal.nativeframe.nf_numlocals = 0;
+
+    /* Create empty threadList */
+    retval = list_new(&pobj);
+    gVmGlobal.threadList = (pPmList_t)pobj;
 
     return retval;
 }
     retval = mod_import(pstring, &pmod);
     PM_RETURN_IF_ERROR(retval);
 
-    /* Load builtins into thread */
-    retval = global_setBuiltins((pPmFunc_t)pmod);
+    /* Load builtins module */
+    retval = pm_loadBuiltins();
+    PM_RETURN_IF_ERROR(retval);
+
+    /* Put builtins module in the module's attrs dict */
+    retval = dict_setItem((pPmObj_t)((pPmFunc_t)pmod)->f_attrs,
+                          (pPmObj_t)&pm_global_string_bi,
+                          PM_PBUILTINS);
     PM_RETURN_IF_ERROR(retval);
 
     /* Interpret the module's bcode */

File src/vm/pm.h

View file
  • Ignore whitespace
 #define INLINE __inline__
 
 
+/* Convenience macros for global objects */
+#define PM_PBUILTINS (pPmObj_t)(gVmGlobal.pbuiltins)
+#define PM_NONE (pPmObj_t)&pm_global_none
+#define PM_FALSE (pPmObj_t)&pm_global_false
+#define PM_TRUE (pPmObj_t)&pm_global_true
+#define PM_ZERO (pPmObj_t)&pm_global_zero
+#define PM_ONE (pPmObj_t)&pm_global_one
+#define PM_NEGONE (pPmObj_t)&pm_global_negone
+#define PM_CODE_STR (pPmObj_t)&pm_global_string_code
+#define PM_INIT_STR (pPmObj_t)&pm_global_string_init
+#define PM_GENERATOR_STR (pPmObj_t)&pm_global_string_generator
+#define PM_NEXT_STR (pPmObj_t)&pm_global_string_next
+#define PM_EXCEPTION_STR (pPmObj_t)&pm_global_string_exception
+#define PM_BYTEARRAY_STR (pPmObj_t)&pm_global_string_bytearray
+#define PM_MD_STR (pPmObj_t)&pm_global_string_md
+#define PM_BI_STR (pPmObj_t)&pm_global_string_bi
+
+
 /**
  * Returns an exception error code and stores debug data
  *
 } PmReturn_t;
 
 
-extern volatile uint32_t pm_timerMsTicks;
-
 /* WARNING: The order of the following includes is critical */
 #include "plat.h"
 #include "pmfeatures.h"
 #include "module.h"
 #include "frame.h"
 #include "interp.h"
-#include "global.h"
 #include "class.h"
 #include "thread.h"
 #include "float.h"
 } PmModuleEntry_t, *pPmModuleEntry;
 
 
+/**
+ * This struct contains ALL of PyMite's globals
+ */
+typedef struct PmVmGlobal_s
+{
+    /** Dict for builtins */
+    pPmDict_t pbuiltins;
+
+    /** The single native frame.  Static alloc so it won't be GC'd */
+    PmNativeFrame_t nativeframe;
+
+    /** PyMite release value for when an error occurs */
+    uint8_t errVmRelease;
+
+    /** PyMite source file ID number for when an error occurs */
+    uint8_t errFileId;
+
+    /** Line number for when an error occurs */
+    uint16_t errLineNum;
+
+    /** Thread list */
+    pPmList_t threadList;
+
+    /** Ptr to current thread */
+    pPmThread_t pthread;
+
+    /** Remembers when a space is needed before printing the next object */
+    uint8_t needSoftSpace;
+
+    /** Remembers when something has printed since the last newline */
+    uint8_t somethingPrinted;
+
+    /** Flag to trigger rescheduling */
+    uint8_t reschedule;
+} PmVmGlobal_t,
+ *pPmVmGlobal_t;
+
+
 /** Object descriptor declaration macro (used in output of pmCoCreator.py) */
 #define PM_DECLARE_OD(type, size) \
     ((((type) << OD_TYPE_SHIFT) & OD_TYPE_MASK) \
 extern PmString9_t PM_PLAT_PROGMEM pm_global_string_generator;
 extern PmString9_t PM_PLAT_PROGMEM pm_global_string_exception;
 extern PmString9_t PM_PLAT_PROGMEM pm_global_string_bytearray;
+extern PmString4_t PM_PLAT_PROGMEM pm_global_string_none;
+extern PmString5_t PM_PLAT_PROGMEM pm_global_string_false;
+extern PmString4_t PM_PLAT_PROGMEM pm_global_string_true;
 extern PmInt_t PM_PLAT_PROGMEM * const pm_global_module_table_len_ptr;
 extern PmModuleEntry_t PM_PLAT_PROGMEM pm_global_module_table[];
 
+extern volatile PmVmGlobal_t gVmGlobal;
+extern volatile uint32_t pm_timerMsTicks;
+
 
 /**
  * Initializes the PyMite virtual machine and indexes the user's application
  * @param pusrimg       Address of the user image in the memory space
  * @return Return status
  */
-PmReturn_t pm_init(uint8_t *heap_base, uint32_t heap_size
-                   /*DWHPmMemSpace_t memspace, uint8_t const * const pusrimg*/);
+PmReturn_t pm_init(uint8_t *heap_base, uint32_t heap_size);
 
 /**
  * Executes the named module