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.
src/vm/codeobj.h@0:14e5e829dffe, 2010-07-21 (annotated)
- Committer:
- dadaista
- Date:
- Wed Jul 21 12:50:41 2010 +0000
- Revision:
- 0:14e5e829dffe
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| dadaista | 0:14e5e829dffe | 1 | /* |
| dadaista | 0:14e5e829dffe | 2 | # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall. |
| dadaista | 0:14e5e829dffe | 3 | # |
| dadaista | 0:14e5e829dffe | 4 | # This file is part of the PyMite VM. |
| dadaista | 0:14e5e829dffe | 5 | # The PyMite VM is free software: you can redistribute it and/or modify |
| dadaista | 0:14e5e829dffe | 6 | # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2. |
| dadaista | 0:14e5e829dffe | 7 | # |
| dadaista | 0:14e5e829dffe | 8 | # The PyMite VM is distributed in the hope that it will be useful, |
| dadaista | 0:14e5e829dffe | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| dadaista | 0:14e5e829dffe | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| dadaista | 0:14e5e829dffe | 11 | # A copy of the GNU GENERAL PUBLIC LICENSE Version 2 |
| dadaista | 0:14e5e829dffe | 12 | # is seen in the file COPYING in this directory. |
| dadaista | 0:14e5e829dffe | 13 | */ |
| dadaista | 0:14e5e829dffe | 14 | |
| dadaista | 0:14e5e829dffe | 15 | |
| dadaista | 0:14e5e829dffe | 16 | #ifndef __CODEOBJ_H__ |
| dadaista | 0:14e5e829dffe | 17 | #define __CODEOBJ_H__ |
| dadaista | 0:14e5e829dffe | 18 | |
| dadaista | 0:14e5e829dffe | 19 | |
| dadaista | 0:14e5e829dffe | 20 | /** |
| dadaista | 0:14e5e829dffe | 21 | * \file |
| dadaista | 0:14e5e829dffe | 22 | * \brief CodeObj Type |
| dadaista | 0:14e5e829dffe | 23 | * |
| dadaista | 0:14e5e829dffe | 24 | * CodeObj type header. |
| dadaista | 0:14e5e829dffe | 25 | */ |
| dadaista | 0:14e5e829dffe | 26 | |
| dadaista | 0:14e5e829dffe | 27 | |
| dadaista | 0:14e5e829dffe | 28 | /** Code image field offset consts */ |
| dadaista | 0:14e5e829dffe | 29 | #define CI_TYPE_FIELD 0 |
| dadaista | 0:14e5e829dffe | 30 | #define CI_SIZE_FIELD 1 |
| dadaista | 0:14e5e829dffe | 31 | #define CI_ARGCOUNT_FIELD 3 |
| dadaista | 0:14e5e829dffe | 32 | #define CI_FLAGS_FIELD 4 |
| dadaista | 0:14e5e829dffe | 33 | #define CI_STACKSIZE_FIELD 5 |
| dadaista | 0:14e5e829dffe | 34 | #define CI_NLOCALS_FIELD 6 |
| dadaista | 0:14e5e829dffe | 35 | #ifdef HAVE_CLOSURES |
| dadaista | 0:14e5e829dffe | 36 | #define CI_FREEVARS_FIELD 7 |
| dadaista | 0:14e5e829dffe | 37 | #define CI_NAMES_FIELD 8 |
| dadaista | 0:14e5e829dffe | 38 | #else |
| dadaista | 0:14e5e829dffe | 39 | #define CI_NAMES_FIELD 7 |
| dadaista | 0:14e5e829dffe | 40 | #endif /* HAVE_CLOSURES */ |
| dadaista | 0:14e5e829dffe | 41 | |
| dadaista | 0:14e5e829dffe | 42 | /** Native code image size */ |
| dadaista | 0:14e5e829dffe | 43 | #define NATIVE_IMAGE_SIZE 4 |
| dadaista | 0:14e5e829dffe | 44 | |
| dadaista | 0:14e5e829dffe | 45 | /* Masks for co_flags (from Python's code.h) */ |
| dadaista | 0:14e5e829dffe | 46 | #define CO_OPTIMIZED 0x01 |
| dadaista | 0:14e5e829dffe | 47 | #define CO_NEWLOCALS 0x02 |
| dadaista | 0:14e5e829dffe | 48 | #define CO_VARARGS 0x04 |
| dadaista | 0:14e5e829dffe | 49 | #define CO_VARKEYWORDS 0x08 |
| dadaista | 0:14e5e829dffe | 50 | #define CO_NESTED 0x10 |
| dadaista | 0:14e5e829dffe | 51 | #define CO_GENERATOR 0x20 |
| dadaista | 0:14e5e829dffe | 52 | #define CO_NOFREE 0x40 |
| dadaista | 0:14e5e829dffe | 53 | |
| dadaista | 0:14e5e829dffe | 54 | /** |
| dadaista | 0:14e5e829dffe | 55 | * Code Object |
| dadaista | 0:14e5e829dffe | 56 | * |
| dadaista | 0:14e5e829dffe | 57 | * An extended object that holds only the most frequently used parts |
| dadaista | 0:14e5e829dffe | 58 | * of the static code image. Other parts can be obtained by |
| dadaista | 0:14e5e829dffe | 59 | * inspecting the code image itself. |
| dadaista | 0:14e5e829dffe | 60 | */ |
| dadaista | 0:14e5e829dffe | 61 | typedef struct PmCo_s |
| dadaista | 0:14e5e829dffe | 62 | { |
| dadaista | 0:14e5e829dffe | 63 | /** object descriptor */ |
| dadaista | 0:14e5e829dffe | 64 | PmObjDesc_t od; |
| dadaista | 0:14e5e829dffe | 65 | /** memory space selector */ |
| dadaista | 0:14e5e829dffe | 66 | PmMemSpace_t co_memspace:8; |
| dadaista | 0:14e5e829dffe | 67 | /** address in progmem of the code image, or of code img obj in heap */ |
| dadaista | 0:14e5e829dffe | 68 | uint8_t const *co_codeimgaddr; |
| dadaista | 0:14e5e829dffe | 69 | /** address in RAM of names tuple */ |
| dadaista | 0:14e5e829dffe | 70 | pPmTuple_t co_names; |
| dadaista | 0:14e5e829dffe | 71 | /** address in RAM of constants tuple */ |
| dadaista | 0:14e5e829dffe | 72 | pPmTuple_t co_consts; |
| dadaista | 0:14e5e829dffe | 73 | #ifdef HAVE_CLOSURES |
| dadaista | 0:14e5e829dffe | 74 | /** Number of freevars */ |
| dadaista | 0:14e5e829dffe | 75 | uint8_t co_nfreevars; |
| dadaista | 0:14e5e829dffe | 76 | /** address in RAM of cellvars tuple */ |
| dadaista | 0:14e5e829dffe | 77 | pPmTuple_t co_cellvars; |
| dadaista | 0:14e5e829dffe | 78 | /** Number of local variables */ |
| dadaista | 0:14e5e829dffe | 79 | uint16_t co_nlocals; |
| dadaista | 0:14e5e829dffe | 80 | #endif /* HAVE_CLOSURES */ |
| dadaista | 0:14e5e829dffe | 81 | /** address in memspace of bytecode (or native function) */ |
| dadaista | 0:14e5e829dffe | 82 | uint8_t const *co_codeaddr; |
| dadaista | 0:14e5e829dffe | 83 | /** number of positional arguments the function expects */ |
| dadaista | 0:14e5e829dffe | 84 | uint8_t co_argcount; |
| dadaista | 0:14e5e829dffe | 85 | /** compiler flags */ |
| dadaista | 0:14e5e829dffe | 86 | uint8_t co_flags; |
| dadaista | 0:14e5e829dffe | 87 | } PmCo_t, |
| dadaista | 0:14e5e829dffe | 88 | *pPmCo_t; |
| dadaista | 0:14e5e829dffe | 89 | |
| dadaista | 0:14e5e829dffe | 90 | /** |
| dadaista | 0:14e5e829dffe | 91 | * Native Code Object |
| dadaista | 0:14e5e829dffe | 92 | * |
| dadaista | 0:14e5e829dffe | 93 | * An extended object that holds only the most frequently used parts |
| dadaista | 0:14e5e829dffe | 94 | * of the static native image. Other parts can be obtained by |
| dadaista | 0:14e5e829dffe | 95 | * inspecting the native image itself. |
| dadaista | 0:14e5e829dffe | 96 | */ |
| dadaista | 0:14e5e829dffe | 97 | typedef struct PmNo_s |
| dadaista | 0:14e5e829dffe | 98 | { |
| dadaista | 0:14e5e829dffe | 99 | /** object descriptor */ |
| dadaista | 0:14e5e829dffe | 100 | PmObjDesc_t od; |
| dadaista | 0:14e5e829dffe | 101 | /** expected num args to the func */ |
| dadaista | 0:14e5e829dffe | 102 | int8_t no_argcount; |
| dadaista | 0:14e5e829dffe | 103 | /** index into native function table */ |
| dadaista | 0:14e5e829dffe | 104 | int16_t no_funcindx; |
| dadaista | 0:14e5e829dffe | 105 | } PmNo_t, |
| dadaista | 0:14e5e829dffe | 106 | *pPmNo_t; |
| dadaista | 0:14e5e829dffe | 107 | |
| dadaista | 0:14e5e829dffe | 108 | |
| dadaista | 0:14e5e829dffe | 109 | /** |
| dadaista | 0:14e5e829dffe | 110 | * Creates a CodeObj by loading info from a code image in memory. |
| dadaista | 0:14e5e829dffe | 111 | * |
| dadaista | 0:14e5e829dffe | 112 | * An image is a static representation of a Python object. |
| dadaista | 0:14e5e829dffe | 113 | * The process of converting an object to and from an image |
| dadaista | 0:14e5e829dffe | 114 | * is also called marshalling. |
| dadaista | 0:14e5e829dffe | 115 | * In PyMite, code images are the equivalent of .pyc files. |
| dadaista | 0:14e5e829dffe | 116 | * Code images can only contain a select subset of object types |
| dadaista | 0:14e5e829dffe | 117 | * (None, Int, Float, String, Slice?, Tuple, and CodeImg). |
| dadaista | 0:14e5e829dffe | 118 | * All other types (Lists, Dicts, CodeObjs, Modules, Classes, |
| dadaista | 0:14e5e829dffe | 119 | * Functions, ClassInstances) are built at runtime. |
| dadaista | 0:14e5e829dffe | 120 | * |
| dadaista | 0:14e5e829dffe | 121 | * All multibyte values are in Little Endian order |
| dadaista | 0:14e5e829dffe | 122 | * (least significant byte comes first in the byte stream). |
| dadaista | 0:14e5e829dffe | 123 | * |
| dadaista | 0:14e5e829dffe | 124 | * memspace and *paddr determine the start of the code image. |
| dadaista | 0:14e5e829dffe | 125 | * Load the code object with values from the code image, |
| dadaista | 0:14e5e829dffe | 126 | * including the names and consts tuples. |
| dadaista | 0:14e5e829dffe | 127 | * Leave contents of paddr pointing one byte past end of |
| dadaista | 0:14e5e829dffe | 128 | * code img. |
| dadaista | 0:14e5e829dffe | 129 | * |
| dadaista | 0:14e5e829dffe | 130 | * The code image has the following structure: |
| dadaista | 0:14e5e829dffe | 131 | * -type: 8b - OBJ_TYPE_CIM |
| dadaista | 0:14e5e829dffe | 132 | * -size: 16b - number of bytes |
| dadaista | 0:14e5e829dffe | 133 | * the code image occupies. |
| dadaista | 0:14e5e829dffe | 134 | * -argcount: 8b - number of arguments to this code obj. |
| dadaista | 0:14e5e829dffe | 135 | * -stacksz: 8b - the maximum arg-stack size needed. |
| dadaista | 0:14e5e829dffe | 136 | * -nlocals: 8b - number of local vars in the code obj. |
| dadaista | 0:14e5e829dffe | 137 | * -names: Tuple - tuple of string objs. |
| dadaista | 0:14e5e829dffe | 138 | * -consts: Tuple - tuple of objs. |
| dadaista | 0:14e5e829dffe | 139 | * -code: 8b[] - bytecode array. |
| dadaista | 0:14e5e829dffe | 140 | * |
| dadaista | 0:14e5e829dffe | 141 | * @param memspace memory space containing image |
| dadaista | 0:14e5e829dffe | 142 | * @param paddr ptr to ptr to code img in memspace |
| dadaista | 0:14e5e829dffe | 143 | * return by reference: paddr points one byte |
| dadaista | 0:14e5e829dffe | 144 | * past end of code img |
| dadaista | 0:14e5e829dffe | 145 | * @param r_pco Return arg. New code object with fields |
| dadaista | 0:14e5e829dffe | 146 | * filled in. |
| dadaista | 0:14e5e829dffe | 147 | * @return Return status |
| dadaista | 0:14e5e829dffe | 148 | */ |
| dadaista | 0:14e5e829dffe | 149 | PmReturn_t |
| dadaista | 0:14e5e829dffe | 150 | co_loadFromImg(PmMemSpace_t memspace, uint8_t const **paddr, pPmObj_t *r_pco); |
| dadaista | 0:14e5e829dffe | 151 | |
| dadaista | 0:14e5e829dffe | 152 | /** |
| dadaista | 0:14e5e829dffe | 153 | * Creates a Native code object by loading a native image. |
| dadaista | 0:14e5e829dffe | 154 | * |
| dadaista | 0:14e5e829dffe | 155 | * An image is a static representation of a Python object. |
| dadaista | 0:14e5e829dffe | 156 | * A native image is much smaller than a regular image |
| dadaista | 0:14e5e829dffe | 157 | * because only two items of data are needed after the type: |
| dadaista | 0:14e5e829dffe | 158 | * the number of args the func expects and the index into |
| dadaista | 0:14e5e829dffe | 159 | * the native function table. |
| dadaista | 0:14e5e829dffe | 160 | * A reference to the image is not needed since it is |
| dadaista | 0:14e5e829dffe | 161 | * just as efficient to store the info in RAM as it is to |
| dadaista | 0:14e5e829dffe | 162 | * store a pointer and memspace value. |
| dadaista | 0:14e5e829dffe | 163 | * |
| dadaista | 0:14e5e829dffe | 164 | * memspace and *paddr determine the start of the native image. |
| dadaista | 0:14e5e829dffe | 165 | * Loads the argcount and the func index from the native object. |
| dadaista | 0:14e5e829dffe | 166 | * Leaves contents of paddr pointing one byte past end of |
| dadaista | 0:14e5e829dffe | 167 | * code img. |
| dadaista | 0:14e5e829dffe | 168 | * |
| dadaista | 0:14e5e829dffe | 169 | * The native image has the following structure: |
| dadaista | 0:14e5e829dffe | 170 | * -type: 8b - OBJ_TYPE_CIM |
| dadaista | 0:14e5e829dffe | 171 | * -argcount: 8b - number of arguments to this code obj. |
| dadaista | 0:14e5e829dffe | 172 | * -code: 16b - index into native function table. |
| dadaista | 0:14e5e829dffe | 173 | * |
| dadaista | 0:14e5e829dffe | 174 | * @param memspace memory space containing image |
| dadaista | 0:14e5e829dffe | 175 | * @param paddr ptr to ptr to code img in memspace (return) |
| dadaista | 0:14e5e829dffe | 176 | * @param r_pno Return by reference, new code object |
| dadaista | 0:14e5e829dffe | 177 | * @return Return status |
| dadaista | 0:14e5e829dffe | 178 | */ |
| dadaista | 0:14e5e829dffe | 179 | PmReturn_t no_loadFromImg(PmMemSpace_t memspace, |
| dadaista | 0:14e5e829dffe | 180 | uint8_t const **paddr, pPmObj_t *r_pno); |
| dadaista | 0:14e5e829dffe | 181 | |
| dadaista | 0:14e5e829dffe | 182 | #endif /* __CODEOBJ_H__ */ |