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/codeobj.h@15:94ca5c8003e5, 2016-04-14 (annotated)
- Committer:
- va009039
- Date:
- Thu Apr 14 22:32:57 2016 +0000
- Revision:
- 15:94ca5c8003e5
- Parent:
- 0:65f1469d6bfb
update Nucleo-F401RE.
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 | #ifndef __CODEOBJ_H__ |
va009039 | 0:65f1469d6bfb | 10 | #define __CODEOBJ_H__ |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief CodeObj Type |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * CodeObj type header. |
va009039 | 0:65f1469d6bfb | 18 | */ |
va009039 | 0:65f1469d6bfb | 19 | |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | /** Code image field offset consts */ |
va009039 | 0:65f1469d6bfb | 22 | #define CI_TYPE_FIELD 0 |
va009039 | 0:65f1469d6bfb | 23 | #define CI_SIZE_FIELD 1 |
va009039 | 0:65f1469d6bfb | 24 | #define CI_ARGCOUNT_FIELD 3 |
va009039 | 0:65f1469d6bfb | 25 | #define CI_FLAGS_FIELD 4 |
va009039 | 0:65f1469d6bfb | 26 | #define CI_STACKSIZE_FIELD 5 |
va009039 | 0:65f1469d6bfb | 27 | #define CI_NLOCALS_FIELD 6 |
va009039 | 0:65f1469d6bfb | 28 | |
va009039 | 0:65f1469d6bfb | 29 | #ifdef HAVE_CLOSURES |
va009039 | 0:65f1469d6bfb | 30 | # define CI_FREEVARS_FIELD 7 |
va009039 | 0:65f1469d6bfb | 31 | # ifdef HAVE_DEBUG_INFO |
va009039 | 0:65f1469d6bfb | 32 | # define CI_FIRST_LINE_NO 8 |
va009039 | 0:65f1469d6bfb | 33 | # define CI_NAMES_FIELD 10 |
va009039 | 0:65f1469d6bfb | 34 | # else |
va009039 | 0:65f1469d6bfb | 35 | # define CI_NAMES_FIELD 8 |
va009039 | 0:65f1469d6bfb | 36 | # endif /* HAVE_DEBUG_INFO */ |
va009039 | 0:65f1469d6bfb | 37 | #else |
va009039 | 0:65f1469d6bfb | 38 | # ifdef HAVE_DEBUG_INFO |
va009039 | 0:65f1469d6bfb | 39 | # define CI_FIRST_LINE_NO 7 |
va009039 | 0:65f1469d6bfb | 40 | # define CI_NAMES_FIELD 9 |
va009039 | 0:65f1469d6bfb | 41 | # else |
va009039 | 0:65f1469d6bfb | 42 | # define CI_NAMES_FIELD 7 |
va009039 | 0:65f1469d6bfb | 43 | # endif /* HAVE_DEBUG_INFO */ |
va009039 | 0:65f1469d6bfb | 44 | #endif /* HAVE_CLOSURES */ |
va009039 | 0:65f1469d6bfb | 45 | |
va009039 | 0:65f1469d6bfb | 46 | |
va009039 | 0:65f1469d6bfb | 47 | /** Native code image size */ |
va009039 | 0:65f1469d6bfb | 48 | #define NATIVE_IMAGE_SIZE 4 |
va009039 | 0:65f1469d6bfb | 49 | |
va009039 | 0:65f1469d6bfb | 50 | /* Masks for co_flags (from Python's code.h) */ |
va009039 | 0:65f1469d6bfb | 51 | #define CO_OPTIMIZED 0x01 |
va009039 | 0:65f1469d6bfb | 52 | #define CO_NEWLOCALS 0x02 |
va009039 | 0:65f1469d6bfb | 53 | #define CO_VARARGS 0x04 |
va009039 | 0:65f1469d6bfb | 54 | #define CO_VARKEYWORDS 0x08 |
va009039 | 0:65f1469d6bfb | 55 | #define CO_NESTED 0x10 |
va009039 | 0:65f1469d6bfb | 56 | #define CO_GENERATOR 0x20 |
va009039 | 0:65f1469d6bfb | 57 | #define CO_NOFREE 0x40 |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | /** |
va009039 | 0:65f1469d6bfb | 60 | * Code Object |
va009039 | 0:65f1469d6bfb | 61 | * |
va009039 | 0:65f1469d6bfb | 62 | * An extended object that holds only the most frequently used parts |
va009039 | 0:65f1469d6bfb | 63 | * of the static code image. Other parts can be obtained by |
va009039 | 0:65f1469d6bfb | 64 | * inspecting the code image itself. |
va009039 | 0:65f1469d6bfb | 65 | */ |
va009039 | 0:65f1469d6bfb | 66 | typedef struct PmCo_s |
va009039 | 0:65f1469d6bfb | 67 | { |
va009039 | 0:65f1469d6bfb | 68 | /** Object descriptor */ |
va009039 | 0:65f1469d6bfb | 69 | PmObjDesc_t od; |
va009039 | 0:65f1469d6bfb | 70 | /** Address in progmem of the code image, or of code img obj in heap */ |
va009039 | 0:65f1469d6bfb | 71 | uint8_t const *co_codeimgaddr; |
va009039 | 0:65f1469d6bfb | 72 | /** Address in RAM of names tuple */ |
va009039 | 0:65f1469d6bfb | 73 | pPmTuple_t co_names; |
va009039 | 0:65f1469d6bfb | 74 | /** Address in RAM of constants tuple */ |
va009039 | 0:65f1469d6bfb | 75 | pPmTuple_t co_consts; |
va009039 | 0:65f1469d6bfb | 76 | /** Address in memspace of bytecode (or native function) */ |
va009039 | 0:65f1469d6bfb | 77 | uint8_t const *co_codeaddr; |
va009039 | 0:65f1469d6bfb | 78 | |
va009039 | 0:65f1469d6bfb | 79 | #ifdef HAVE_DEBUG_INFO |
va009039 | 0:65f1469d6bfb | 80 | /** Address in memspace of the line number table */ |
va009039 | 0:65f1469d6bfb | 81 | uint8_t const *co_lnotab; |
va009039 | 0:65f1469d6bfb | 82 | /** Address in memspace of the filename */ |
va009039 | 0:65f1469d6bfb | 83 | uint8_t const *co_filename; |
va009039 | 0:65f1469d6bfb | 84 | /** Line number of first source line of lnotab */ |
va009039 | 0:65f1469d6bfb | 85 | uint16_t co_firstlineno; |
va009039 | 0:65f1469d6bfb | 86 | #endif /* HAVE_DEBUG_INFO */ |
va009039 | 0:65f1469d6bfb | 87 | |
va009039 | 0:65f1469d6bfb | 88 | #ifdef HAVE_CLOSURES |
va009039 | 0:65f1469d6bfb | 89 | /** Address in RAM of cellvars tuple */ |
va009039 | 0:65f1469d6bfb | 90 | pPmTuple_t co_cellvars; |
va009039 | 0:65f1469d6bfb | 91 | /** Number of freevars */ |
va009039 | 0:65f1469d6bfb | 92 | uint8_t co_nfreevars; |
va009039 | 0:65f1469d6bfb | 93 | #endif /* HAVE_CLOSURES */ |
va009039 | 0:65f1469d6bfb | 94 | |
va009039 | 0:65f1469d6bfb | 95 | /** Memory space selector */ |
va009039 | 0:65f1469d6bfb | 96 | PmMemSpace_t co_memspace:8; |
va009039 | 0:65f1469d6bfb | 97 | /** Number of positional arguments the function expects */ |
va009039 | 0:65f1469d6bfb | 98 | uint8_t co_argcount; |
va009039 | 0:65f1469d6bfb | 99 | /** Compiler flags */ |
va009039 | 0:65f1469d6bfb | 100 | uint8_t co_flags; |
va009039 | 0:65f1469d6bfb | 101 | /** Stack size */ |
va009039 | 0:65f1469d6bfb | 102 | uint8_t co_stacksize; |
va009039 | 0:65f1469d6bfb | 103 | /** Number of local variables */ |
va009039 | 0:65f1469d6bfb | 104 | uint8_t co_nlocals; |
va009039 | 0:65f1469d6bfb | 105 | } PmCo_t, |
va009039 | 0:65f1469d6bfb | 106 | *pPmCo_t; |
va009039 | 0:65f1469d6bfb | 107 | |
va009039 | 0:65f1469d6bfb | 108 | /** |
va009039 | 0:65f1469d6bfb | 109 | * Native Code Object |
va009039 | 0:65f1469d6bfb | 110 | * |
va009039 | 0:65f1469d6bfb | 111 | * An extended object that holds only the most frequently used parts |
va009039 | 0:65f1469d6bfb | 112 | * of the static native image. Other parts can be obtained by |
va009039 | 0:65f1469d6bfb | 113 | * inspecting the native image itself. |
va009039 | 0:65f1469d6bfb | 114 | */ |
va009039 | 0:65f1469d6bfb | 115 | typedef struct PmNo_s |
va009039 | 0:65f1469d6bfb | 116 | { |
va009039 | 0:65f1469d6bfb | 117 | /** object descriptor */ |
va009039 | 0:65f1469d6bfb | 118 | PmObjDesc_t od; |
va009039 | 0:65f1469d6bfb | 119 | /** expected num args to the func */ |
va009039 | 0:65f1469d6bfb | 120 | int8_t no_argcount; |
va009039 | 0:65f1469d6bfb | 121 | /** index into native function table */ |
va009039 | 0:65f1469d6bfb | 122 | int16_t no_funcindx; |
va009039 | 0:65f1469d6bfb | 123 | } PmNo_t, |
va009039 | 0:65f1469d6bfb | 124 | *pPmNo_t; |
va009039 | 0:65f1469d6bfb | 125 | |
va009039 | 0:65f1469d6bfb | 126 | |
va009039 | 0:65f1469d6bfb | 127 | /** |
va009039 | 0:65f1469d6bfb | 128 | * Creates a CodeObj by loading info from a code image in memory. |
va009039 | 0:65f1469d6bfb | 129 | * |
va009039 | 0:65f1469d6bfb | 130 | * An image is a static representation of a Python object. |
va009039 | 0:65f1469d6bfb | 131 | * The process of converting an object to and from an image |
va009039 | 0:65f1469d6bfb | 132 | * is also called marshalling. |
va009039 | 0:65f1469d6bfb | 133 | * In PyMite, code images are the equivalent of .pyc files. |
va009039 | 0:65f1469d6bfb | 134 | * Code images can only contain a select subset of object types |
va009039 | 0:65f1469d6bfb | 135 | * (None, Int, Float, String, Slice?, Tuple, and CodeImg). |
va009039 | 0:65f1469d6bfb | 136 | * All other types (Lists, Dicts, CodeObjs, Modules, Classes, |
va009039 | 0:65f1469d6bfb | 137 | * Functions, ClassInstances) are built at runtime. |
va009039 | 0:65f1469d6bfb | 138 | * |
va009039 | 0:65f1469d6bfb | 139 | * All multibyte values are in Little Endian order |
va009039 | 0:65f1469d6bfb | 140 | * (least significant byte comes first in the byte stream). |
va009039 | 0:65f1469d6bfb | 141 | * |
va009039 | 0:65f1469d6bfb | 142 | * memspace and *paddr determine the start of the code image. |
va009039 | 0:65f1469d6bfb | 143 | * Load the code object with values from the code image, |
va009039 | 0:65f1469d6bfb | 144 | * including the names and consts tuples. |
va009039 | 0:65f1469d6bfb | 145 | * Leave contents of paddr pointing one byte past end of |
va009039 | 0:65f1469d6bfb | 146 | * code img. |
va009039 | 0:65f1469d6bfb | 147 | * |
va009039 | 0:65f1469d6bfb | 148 | * The code image has the following structure: |
va009039 | 0:65f1469d6bfb | 149 | * -type: 8b - OBJ_TYPE_CIM |
va009039 | 0:65f1469d6bfb | 150 | * -size: 16b - number of bytes |
va009039 | 0:65f1469d6bfb | 151 | * the code image occupies. |
va009039 | 0:65f1469d6bfb | 152 | * -argcount: 8b - number of arguments to this code obj. |
va009039 | 0:65f1469d6bfb | 153 | * -stacksz: 8b - the maximum arg-stack size needed. |
va009039 | 0:65f1469d6bfb | 154 | * -nlocals: 8b - number of local vars in the code obj. |
va009039 | 0:65f1469d6bfb | 155 | * -names: Tuple - tuple of string objs. |
va009039 | 0:65f1469d6bfb | 156 | * -consts: Tuple - tuple of objs. |
va009039 | 0:65f1469d6bfb | 157 | * -code: 8b[] - bytecode array. |
va009039 | 0:65f1469d6bfb | 158 | * |
va009039 | 0:65f1469d6bfb | 159 | * @param memspace memory space containing image |
va009039 | 0:65f1469d6bfb | 160 | * @param paddr ptr to ptr to code img in memspace |
va009039 | 0:65f1469d6bfb | 161 | * return by reference: paddr points one byte |
va009039 | 0:65f1469d6bfb | 162 | * past end of code img |
va009039 | 0:65f1469d6bfb | 163 | * @param r_pco Return arg. New code object with fields |
va009039 | 0:65f1469d6bfb | 164 | * filled in. |
va009039 | 0:65f1469d6bfb | 165 | * @return Return status |
va009039 | 0:65f1469d6bfb | 166 | */ |
va009039 | 0:65f1469d6bfb | 167 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 168 | co_loadFromImg(PmMemSpace_t memspace, uint8_t const **paddr, pPmObj_t *r_pco); |
va009039 | 0:65f1469d6bfb | 169 | |
va009039 | 0:65f1469d6bfb | 170 | /** |
va009039 | 0:65f1469d6bfb | 171 | * Recursively sets image address of the CO and all its nested COs |
va009039 | 0:65f1469d6bfb | 172 | * in its constant pool. This is done so that an image that was |
va009039 | 0:65f1469d6bfb | 173 | * received during an interactive session will persist as long as any |
va009039 | 0:65f1469d6bfb | 174 | * of its COs/funcs/objects is still alive. |
va009039 | 0:65f1469d6bfb | 175 | * |
va009039 | 0:65f1469d6bfb | 176 | * @param pco Pointer to root code object whose images are set |
va009039 | 0:65f1469d6bfb | 177 | * @param pimg Pointer to very top of code image (PmodeImgObj) |
va009039 | 0:65f1469d6bfb | 178 | */ |
va009039 | 0:65f1469d6bfb | 179 | void co_rSetCodeImgAddr(pPmCo_t pco, uint8_t const *pimg); |
va009039 | 0:65f1469d6bfb | 180 | |
va009039 | 0:65f1469d6bfb | 181 | /** |
va009039 | 0:65f1469d6bfb | 182 | * Creates a Native code object by loading a native image. |
va009039 | 0:65f1469d6bfb | 183 | * |
va009039 | 0:65f1469d6bfb | 184 | * An image is a static representation of a Python object. |
va009039 | 0:65f1469d6bfb | 185 | * A native image is much smaller than a regular image |
va009039 | 0:65f1469d6bfb | 186 | * because only two items of data are needed after the type: |
va009039 | 0:65f1469d6bfb | 187 | * the number of args the func expects and the index into |
va009039 | 0:65f1469d6bfb | 188 | * the native function table. |
va009039 | 0:65f1469d6bfb | 189 | * A reference to the image is not needed since it is |
va009039 | 0:65f1469d6bfb | 190 | * just as efficient to store the info in RAM as it is to |
va009039 | 0:65f1469d6bfb | 191 | * store a pointer and memspace value. |
va009039 | 0:65f1469d6bfb | 192 | * |
va009039 | 0:65f1469d6bfb | 193 | * memspace and *paddr determine the start of the native image. |
va009039 | 0:65f1469d6bfb | 194 | * Loads the argcount and the func index from the native object. |
va009039 | 0:65f1469d6bfb | 195 | * Leaves contents of paddr pointing one byte past end of |
va009039 | 0:65f1469d6bfb | 196 | * code img. |
va009039 | 0:65f1469d6bfb | 197 | * |
va009039 | 0:65f1469d6bfb | 198 | * The native image has the following structure: |
va009039 | 0:65f1469d6bfb | 199 | * -type: 8b - OBJ_TYPE_CIM |
va009039 | 0:65f1469d6bfb | 200 | * -argcount: 8b - number of arguments to this code obj. |
va009039 | 0:65f1469d6bfb | 201 | * -code: 16b - index into native function table. |
va009039 | 0:65f1469d6bfb | 202 | * |
va009039 | 0:65f1469d6bfb | 203 | * @param memspace memory space containing image |
va009039 | 0:65f1469d6bfb | 204 | * @param paddr ptr to ptr to code img in memspace (return) |
va009039 | 0:65f1469d6bfb | 205 | * @param r_pno Return by reference, new code object |
va009039 | 0:65f1469d6bfb | 206 | * @return Return status |
va009039 | 0:65f1469d6bfb | 207 | */ |
va009039 | 0:65f1469d6bfb | 208 | PmReturn_t no_loadFromImg(PmMemSpace_t memspace, |
va009039 | 0:65f1469d6bfb | 209 | uint8_t const **paddr, pPmObj_t *r_pno); |
va009039 | 0:65f1469d6bfb | 210 | |
va009039 | 0:65f1469d6bfb | 211 | #endif /* __CODEOBJ_H__ */ |