python-on-a-chip online compiler

Dependencies:   mbed TSI

/media/uploads/va009039/p14p-f446re.png

more info: python-on-a-chip

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?

UserRevisionLine numberNew 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 __OBJ_H__
va009039 0:65f1469d6bfb 10 #define __OBJ_H__
va009039 0:65f1469d6bfb 11
va009039 0:65f1469d6bfb 12
va009039 0:65f1469d6bfb 13 /**
va009039 0:65f1469d6bfb 14 * \file
va009039 0:65f1469d6bfb 15 * \brief Object Type
va009039 0:65f1469d6bfb 16 *
va009039 0:65f1469d6bfb 17 * Object type header.
va009039 0:65f1469d6bfb 18 */
va009039 0:65f1469d6bfb 19
va009039 0:65f1469d6bfb 20
va009039 0:65f1469d6bfb 21 /** Object descriptor field constants */
va009039 0:65f1469d6bfb 22 #define OD_MARK_SHIFT (uint8_t)0
va009039 0:65f1469d6bfb 23 #define OD_FREE_SHIFT (uint8_t)1
va009039 0:65f1469d6bfb 24 #define OD_SIZE_SHIFT (uint8_t)0
va009039 0:65f1469d6bfb 25 #define OD_TYPE_SHIFT (uint8_t)11
va009039 0:65f1469d6bfb 26 #define OD_MARK_MASK (uint16_t)(1 << OD_MARK_SHIFT)
va009039 0:65f1469d6bfb 27 #define OD_FREE_MASK (uint16_t)(1 << OD_FREE_SHIFT)
va009039 0:65f1469d6bfb 28 #define OD_SIZE_MASK (uint16_t)(0x07FC)
va009039 0:65f1469d6bfb 29 #define OD_TYPE_MASK (uint16_t)(0xF800)
va009039 0:65f1469d6bfb 30
va009039 0:65f1469d6bfb 31 /** Heap descriptor size mask */
va009039 0:65f1469d6bfb 32 #define HD_SIZE_MASK (uint16_t)(OD_TYPE_MASK | OD_SIZE_MASK)
va009039 0:65f1469d6bfb 33 #define HD_SIZE_SHIFT OD_SIZE_SHIFT
va009039 0:65f1469d6bfb 34
va009039 0:65f1469d6bfb 35 /**
va009039 0:65f1469d6bfb 36 * Gets the free bit of the given object to the given value.
va009039 0:65f1469d6bfb 37 * If the object is marked free, it is not being used by the VM.
va009039 0:65f1469d6bfb 38 */
va009039 0:65f1469d6bfb 39 #define OBJ_GET_FREE(pobj) \
va009039 0:65f1469d6bfb 40 ((((pPmObj_t)pobj)->od & OD_FREE_MASK) >> OD_FREE_SHIFT)
va009039 0:65f1469d6bfb 41
va009039 0:65f1469d6bfb 42 /**
va009039 0:65f1469d6bfb 43 * Sets the free bit of the given object to the given value.
va009039 0:65f1469d6bfb 44 * Setting the free bit means that the object will use the heap descriptor
va009039 0:65f1469d6bfb 45 * structure instead of the object descriptor structure.
va009039 0:65f1469d6bfb 46 */
va009039 0:65f1469d6bfb 47 #define OBJ_SET_FREE(pobj, free) \
va009039 0:65f1469d6bfb 48 do \
va009039 0:65f1469d6bfb 49 { \
va009039 0:65f1469d6bfb 50 ((pPmObj_t)pobj)->od = ((uint8_t)free) \
va009039 0:65f1469d6bfb 51 ? ((pPmObj_t)pobj)->od | OD_FREE_MASK \
va009039 0:65f1469d6bfb 52 : ((pPmObj_t)pobj)->od & ~OD_FREE_MASK;\
va009039 0:65f1469d6bfb 53 } \
va009039 0:65f1469d6bfb 54 while (0)
va009039 0:65f1469d6bfb 55
va009039 0:65f1469d6bfb 56 /*
va009039 0:65f1469d6bfb 57 * #99: od_size bits are shifted because size is a scaled value
va009039 0:65f1469d6bfb 58 * True size is always a multiple of 4, so the lower two bits are ignored
va009039 0:65f1469d6bfb 59 * and two more significant bits are gained.
va009039 0:65f1469d6bfb 60 */
va009039 0:65f1469d6bfb 61 /** Gets the size in bytes of the object. */
va009039 0:65f1469d6bfb 62 #define PM_OBJ_GET_SIZE(pobj) (((pPmObj_t)pobj)->od & OD_SIZE_MASK)
va009039 0:65f1469d6bfb 63
va009039 0:65f1469d6bfb 64 /**
va009039 0:65f1469d6bfb 65 * Gets the type of the object
va009039 0:65f1469d6bfb 66 * This MUST NOT be called on objects that are free.
va009039 0:65f1469d6bfb 67 */
va009039 0:65f1469d6bfb 68 #define OBJ_GET_TYPE(pobj) \
va009039 0:65f1469d6bfb 69 ((((pPmObj_t)pobj)->od) >> OD_TYPE_SHIFT)
va009039 0:65f1469d6bfb 70
va009039 0:65f1469d6bfb 71 /**
va009039 0:65f1469d6bfb 72 * Sets the type of the object
va009039 0:65f1469d6bfb 73 * This MUST NOT be called on objects that are free.
va009039 0:65f1469d6bfb 74 */
va009039 0:65f1469d6bfb 75 #define OBJ_SET_TYPE(pobj, type) \
va009039 0:65f1469d6bfb 76 do \
va009039 0:65f1469d6bfb 77 { \
va009039 0:65f1469d6bfb 78 ((pPmObj_t)pobj)->od &= ~OD_TYPE_MASK; \
va009039 0:65f1469d6bfb 79 ((pPmObj_t)pobj)->od |= (((type) << OD_TYPE_SHIFT) & OD_TYPE_MASK); \
va009039 0:65f1469d6bfb 80 } \
va009039 0:65f1469d6bfb 81 while (0)
va009039 0:65f1469d6bfb 82
va009039 0:65f1469d6bfb 83
va009039 0:65f1469d6bfb 84 /**
va009039 0:65f1469d6bfb 85 * Object type enum
va009039 0:65f1469d6bfb 86 *
va009039 0:65f1469d6bfb 87 * These values go in the od_type fields of the obj descriptor.
va009039 0:65f1469d6bfb 88 * Be sure these values correspond to those in the image creator
va009039 0:65f1469d6bfb 89 * tool.
va009039 0:65f1469d6bfb 90 * The hashable types are grouped together for convenience.
va009039 0:65f1469d6bfb 91 *
va009039 0:65f1469d6bfb 92 * WARNING: od_type must be at most 5 bits! (must be < 0x20)
va009039 0:65f1469d6bfb 93 */
va009039 0:65f1469d6bfb 94 typedef enum PmType_e
va009039 0:65f1469d6bfb 95 {
va009039 0:65f1469d6bfb 96 OBJ_TYPE_HASHABLE_MIN = 0x00,
va009039 0:65f1469d6bfb 97
va009039 0:65f1469d6bfb 98 /** None */
va009039 0:65f1469d6bfb 99 OBJ_TYPE_NON = 0x00,
va009039 0:65f1469d6bfb 100
va009039 0:65f1469d6bfb 101 /** Signed integer */
va009039 0:65f1469d6bfb 102 OBJ_TYPE_INT = 0x01,
va009039 0:65f1469d6bfb 103
va009039 0:65f1469d6bfb 104 /** Floating point 32b */
va009039 0:65f1469d6bfb 105 OBJ_TYPE_FLT = 0x02,
va009039 0:65f1469d6bfb 106
va009039 0:65f1469d6bfb 107 /** String */
va009039 0:65f1469d6bfb 108 OBJ_TYPE_STR = 0x03,
va009039 0:65f1469d6bfb 109
va009039 0:65f1469d6bfb 110 /** Tuple (immutable sequence) */
va009039 0:65f1469d6bfb 111 OBJ_TYPE_TUP = 0x04,
va009039 0:65f1469d6bfb 112
va009039 0:65f1469d6bfb 113 /** Code obj */
va009039 0:65f1469d6bfb 114 OBJ_TYPE_COB = 0x05,
va009039 0:65f1469d6bfb 115
va009039 0:65f1469d6bfb 116 /** Module obj */
va009039 0:65f1469d6bfb 117 OBJ_TYPE_MOD = 0x06,
va009039 0:65f1469d6bfb 118
va009039 0:65f1469d6bfb 119 /** Class obj */
va009039 0:65f1469d6bfb 120 OBJ_TYPE_CLO = 0x07,
va009039 0:65f1469d6bfb 121
va009039 0:65f1469d6bfb 122 /** Function obj (callable) */
va009039 0:65f1469d6bfb 123 OBJ_TYPE_FXN = 0x08,
va009039 0:65f1469d6bfb 124
va009039 0:65f1469d6bfb 125 /** Class instance */
va009039 0:65f1469d6bfb 126 OBJ_TYPE_CLI = 0x09,
va009039 0:65f1469d6bfb 127
va009039 0:65f1469d6bfb 128 /** Code image in static memory */
va009039 0:65f1469d6bfb 129 OBJ_TYPE_CIM = 0x0A,
va009039 0:65f1469d6bfb 130
va009039 0:65f1469d6bfb 131 /** Native function image */
va009039 0:65f1469d6bfb 132 OBJ_TYPE_NIM = 0x0B,
va009039 0:65f1469d6bfb 133
va009039 0:65f1469d6bfb 134 /** Native function object */
va009039 0:65f1469d6bfb 135 OBJ_TYPE_NOB = 0x0C,
va009039 0:65f1469d6bfb 136
va009039 0:65f1469d6bfb 137 /** Thread */
va009039 0:65f1469d6bfb 138 OBJ_TYPE_THR = 0x0D,
va009039 0:65f1469d6bfb 139
va009039 0:65f1469d6bfb 140 /** Boolean object */
va009039 0:65f1469d6bfb 141 OBJ_TYPE_BOOL = 0x0F,
va009039 0:65f1469d6bfb 142
va009039 0:65f1469d6bfb 143 /** Code image object */
va009039 0:65f1469d6bfb 144 OBJ_TYPE_CIO = 0x10,
va009039 0:65f1469d6bfb 145
va009039 0:65f1469d6bfb 146 /** Method object */
va009039 0:65f1469d6bfb 147 OBJ_TYPE_MTH = 0x11,
va009039 0:65f1469d6bfb 148
va009039 0:65f1469d6bfb 149 /* All types after this are not hashable */
va009039 0:65f1469d6bfb 150 OBJ_TYPE_HASHABLE_MAX = 0x11,
va009039 0:65f1469d6bfb 151
va009039 0:65f1469d6bfb 152 /** List (mutable sequence) */
va009039 0:65f1469d6bfb 153 OBJ_TYPE_LST = 0x12,
va009039 0:65f1469d6bfb 154
va009039 0:65f1469d6bfb 155 /** Dictionary (hash table) */
va009039 0:65f1469d6bfb 156 OBJ_TYPE_DIC = 0x13,
va009039 0:65f1469d6bfb 157
va009039 0:65f1469d6bfb 158 #ifdef HAVE_BYTEARRAY
va009039 0:65f1469d6bfb 159 /** Bytearray (mutable) */
va009039 0:65f1469d6bfb 160 OBJ_TYPE_BYA = 0x14,
va009039 0:65f1469d6bfb 161 #endif /* HAVE_BYTEARRAY */
va009039 0:65f1469d6bfb 162
va009039 0:65f1469d6bfb 163 /* All types after this are not accessible to the user */
va009039 0:65f1469d6bfb 164 OBJ_TYPE_ACCESSIBLE_MAX = 0x18,
va009039 0:65f1469d6bfb 165
va009039 0:65f1469d6bfb 166 #ifdef HAVE_BYTEARRAY
va009039 0:65f1469d6bfb 167 /** Bytes (mutable container for Bytearray type) */
va009039 0:65f1469d6bfb 168 OBJ_TYPE_BYS = 0x18,
va009039 0:65f1469d6bfb 169 #endif /* HAVE_BYTEARRAY */
va009039 0:65f1469d6bfb 170
va009039 0:65f1469d6bfb 171 /** Frame type */
va009039 0:65f1469d6bfb 172 OBJ_TYPE_FRM = 0x19,
va009039 0:65f1469d6bfb 173
va009039 0:65f1469d6bfb 174 /** Block type (for,while,try,etc) */
va009039 0:65f1469d6bfb 175 OBJ_TYPE_BLK = 0x1A,
va009039 0:65f1469d6bfb 176
va009039 0:65f1469d6bfb 177 /** Segment (within a seglist) */
va009039 0:65f1469d6bfb 178 OBJ_TYPE_SEG = 0x1B,
va009039 0:65f1469d6bfb 179
va009039 0:65f1469d6bfb 180 /** Seglist */
va009039 0:65f1469d6bfb 181 OBJ_TYPE_SGL = 0x1C,
va009039 0:65f1469d6bfb 182
va009039 0:65f1469d6bfb 183 /** Sequence iterator */
va009039 0:65f1469d6bfb 184 OBJ_TYPE_SQI = 0x1D,
va009039 0:65f1469d6bfb 185
va009039 0:65f1469d6bfb 186 /** Native frame (there is only one) */
va009039 0:65f1469d6bfb 187 OBJ_TYPE_NFM = 0x1E,
va009039 0:65f1469d6bfb 188 } PmType_t, *pPmType_t;
va009039 0:65f1469d6bfb 189
va009039 0:65f1469d6bfb 190
va009039 0:65f1469d6bfb 191 /**
va009039 0:65f1469d6bfb 192 * Object Descriptor
va009039 0:65f1469d6bfb 193 *
va009039 0:65f1469d6bfb 194 * All active PyMite "objects" must have this at the top of their struct.
va009039 0:65f1469d6bfb 195 * The following is a diagram of the object descriptor:
va009039 0:65f1469d6bfb 196 * \verbatim
va009039 0:65f1469d6bfb 197 * MSb LSb
va009039 0:65f1469d6bfb 198 * 7 6 5 4 3 2 1 0
va009039 0:65f1469d6bfb 199 * pchunk-> +-+-+-+-+-+-+-+-+ S := Size of the chunk (2 LSbs dropped)
va009039 0:65f1469d6bfb 200 * | S |F|M| F := Free bit
va009039 0:65f1469d6bfb 201 * +---------+-+-+-+ M := GC Mark bit
va009039 0:65f1469d6bfb 202 * | T | S | T := Object type (PyMite specific)
va009039 0:65f1469d6bfb 203 * +---------+-----+
va009039 0:65f1469d6bfb 204 * | object data |
va009039 0:65f1469d6bfb 205 * ... ...
va009039 0:65f1469d6bfb 206 * | end data |
va009039 0:65f1469d6bfb 207 * +---------------+
va009039 0:65f1469d6bfb 208 * \endverbatim
va009039 0:65f1469d6bfb 209 *
va009039 0:65f1469d6bfb 210 * The theoretical minimum size of an object descriptor is 2 bytes;
va009039 0:65f1469d6bfb 211 * however, the effective minimum size must be at least that of the minimum
va009039 0:65f1469d6bfb 212 * heap descriptor. So on an 8-bit MCU, the minimum size is 8 bytes
va009039 0:65f1469d6bfb 213 * and on an MCU with 32-bit addresses, the size is 12 bytes.
va009039 0:65f1469d6bfb 214 */
va009039 0:65f1469d6bfb 215 typedef uint16_t PmObjDesc_t, *pPmObjDesc_t;
va009039 0:65f1469d6bfb 216
va009039 0:65f1469d6bfb 217 /** The abstract empty object type for PyMite. */
va009039 0:65f1469d6bfb 218 typedef struct PmObj_s
va009039 0:65f1469d6bfb 219 {
va009039 0:65f1469d6bfb 220 /** Object descriptor */
va009039 0:65f1469d6bfb 221 PmObjDesc_t od;
va009039 0:65f1469d6bfb 222 } PmObj_t, *pPmObj_t;
va009039 0:65f1469d6bfb 223
va009039 0:65f1469d6bfb 224 /** Boolean object */
va009039 0:65f1469d6bfb 225 typedef struct PmBoolean_s
va009039 0:65f1469d6bfb 226 {
va009039 0:65f1469d6bfb 227 /** Object descriptor */
va009039 0:65f1469d6bfb 228 PmObjDesc_t od;
va009039 0:65f1469d6bfb 229
va009039 0:65f1469d6bfb 230 /** Boolean value */
va009039 0:65f1469d6bfb 231 int32_t val;
va009039 0:65f1469d6bfb 232 }
va009039 0:65f1469d6bfb 233 PmBoolean_t, *pPmBoolean_t;
va009039 0:65f1469d6bfb 234
va009039 0:65f1469d6bfb 235
va009039 0:65f1469d6bfb 236 /**
va009039 0:65f1469d6bfb 237 * Loads an object from an image in memory.
va009039 0:65f1469d6bfb 238 * Return pointer to object.
va009039 0:65f1469d6bfb 239 * Leave add pointing one byte past end of obj.
va009039 0:65f1469d6bfb 240 *
va009039 0:65f1469d6bfb 241 * The following lists the simple object types
va009039 0:65f1469d6bfb 242 * and their image structures:
va009039 0:65f1469d6bfb 243 * -None:
va009039 0:65f1469d6bfb 244 * -type: int8_t - OBJ_TYPE_NON
va009039 0:65f1469d6bfb 245 *
va009039 0:65f1469d6bfb 246 * -Int:
va009039 0:65f1469d6bfb 247 * -type: int8_t - OBJ_TYPE_INT
va009039 0:65f1469d6bfb 248 * -value: int32_t - signed integer value
va009039 0:65f1469d6bfb 249 *
va009039 0:65f1469d6bfb 250 * -Float:
va009039 0:65f1469d6bfb 251 * -type: int8_t - OBJ_TYPE_FLOAT
va009039 0:65f1469d6bfb 252 * -value: float32_t - 32-bit floating point value
va009039 0:65f1469d6bfb 253 *
va009039 0:65f1469d6bfb 254 * -Slice (is this allowed in img?):
va009039 0:65f1469d6bfb 255 * -type: int8_t - OBJ_TYPE_SLICE
va009039 0:65f1469d6bfb 256 * -index1: int16_t - first index.
va009039 0:65f1469d6bfb 257 * -index2: int16_t - second index.
va009039 0:65f1469d6bfb 258 *
va009039 0:65f1469d6bfb 259 * @param memspace memory space/type
va009039 0:65f1469d6bfb 260 * @param paddr ptr to ptr to obj
va009039 0:65f1469d6bfb 261 * return by reference: paddr pts to
va009039 0:65f1469d6bfb 262 * first byte after obj
va009039 0:65f1469d6bfb 263 * @param r_pobj Return arg, the loaded object.
va009039 0:65f1469d6bfb 264 * @return Return status
va009039 0:65f1469d6bfb 265 */
va009039 0:65f1469d6bfb 266 PmReturn_t obj_loadFromImg(PmMemSpace_t memspace,
va009039 0:65f1469d6bfb 267 uint8_t const **paddr, pPmObj_t *r_pobj);
va009039 0:65f1469d6bfb 268
va009039 0:65f1469d6bfb 269 /**
va009039 0:65f1469d6bfb 270 * Loads a code object from a code image object
va009039 0:65f1469d6bfb 271 *
va009039 0:65f1469d6bfb 272 * @param pimg Ptr to a code image object
va009039 0:65f1469d6bfb 273 * @param r_pobj Return arg, the loaded object
va009039 0:65f1469d6bfb 274 * @return Returns status
va009039 0:65f1469d6bfb 275 */
va009039 0:65f1469d6bfb 276 PmReturn_t obj_loadFromImgObj(pPmObj_t pimg, pPmObj_t *r_pobj);
va009039 0:65f1469d6bfb 277
va009039 0:65f1469d6bfb 278 /**
va009039 0:65f1469d6bfb 279 * Finds the boolean value of the given object.
va009039 0:65f1469d6bfb 280 *
va009039 0:65f1469d6bfb 281 * @param pobj Ptr to object to test.
va009039 0:65f1469d6bfb 282 * @return Nonzero value if object is False.
va009039 0:65f1469d6bfb 283 */
va009039 0:65f1469d6bfb 284 int8_t obj_isFalse(pPmObj_t pobj);
va009039 0:65f1469d6bfb 285
va009039 0:65f1469d6bfb 286 /**
va009039 0:65f1469d6bfb 287 * Returns the boolean true if the item is in the object
va009039 0:65f1469d6bfb 288 *
va009039 0:65f1469d6bfb 289 * @param pobj Ptr to container object
va009039 0:65f1469d6bfb 290 * @param pitem Ptr to item
va009039 0:65f1469d6bfb 291 */
va009039 0:65f1469d6bfb 292 PmReturn_t obj_isIn(pPmObj_t pobj, pPmObj_t pitem);
va009039 0:65f1469d6bfb 293
va009039 0:65f1469d6bfb 294 /**
va009039 0:65f1469d6bfb 295 * Compares two objects for equality.
va009039 0:65f1469d6bfb 296 *
va009039 0:65f1469d6bfb 297 * @param pobj1 Ptr to first object.
va009039 0:65f1469d6bfb 298 * @param pobj2 Ptr to second object.
va009039 0:65f1469d6bfb 299 * @return C_SAME if the items are equivalent, C_DIFFER otherwise.
va009039 0:65f1469d6bfb 300 */
va009039 0:65f1469d6bfb 301 int8_t obj_compare(pPmObj_t pobj1, pPmObj_t pobj2);
va009039 0:65f1469d6bfb 302
va009039 0:65f1469d6bfb 303 /**
va009039 0:65f1469d6bfb 304 * Print an object, thereby using objects helpers.
va009039 0:65f1469d6bfb 305 *
va009039 0:65f1469d6bfb 306 * @param pobj Ptr to object for printing.
va009039 0:65f1469d6bfb 307 * @param is_expr_repr Influences the way None and strings are printed.
va009039 0:65f1469d6bfb 308 * If 0, None is printed, strings are printed.
va009039 0:65f1469d6bfb 309 * If 1, None is not printed and strings are printed
va009039 0:65f1469d6bfb 310 * surrounded with single quotes and unprintable
va009039 0:65f1469d6bfb 311 * characters are escaped.
va009039 0:65f1469d6bfb 312 * @param is_nested Influences the way None and strings are printed.
va009039 0:65f1469d6bfb 313 * If 1, None will be printed and strings will be
va009039 0:65f1469d6bfb 314 * surrounded with single quotes and escaped.
va009039 0:65f1469d6bfb 315 * This argument overrides the is_expr_repr argument.
va009039 0:65f1469d6bfb 316 * @return Return status
va009039 0:65f1469d6bfb 317 */
va009039 0:65f1469d6bfb 318 PmReturn_t obj_print(pPmObj_t pobj, uint8_t is_expr_repr, uint8_t is_nested);
va009039 0:65f1469d6bfb 319
va009039 0:65f1469d6bfb 320 #ifdef HAVE_BACKTICK
va009039 0:65f1469d6bfb 321 /**
va009039 0:65f1469d6bfb 322 * Returns by reference a string object that is the human-readable
va009039 0:65f1469d6bfb 323 * representation of the object. Used by the backtick operation (UNARY_CONVERT).
va009039 0:65f1469d6bfb 324 *
va009039 0:65f1469d6bfb 325 * @param pobj Ptr to object to represent
va009039 0:65f1469d6bfb 326 * @param r_pstr Return arg, the string object
va009039 0:65f1469d6bfb 327 * @return Return status
va009039 0:65f1469d6bfb 328 */
va009039 0:65f1469d6bfb 329 PmReturn_t obj_repr(pPmObj_t pobj, pPmObj_t *r_pstr);
va009039 0:65f1469d6bfb 330 #endif /* HAVE_BACKTICK */
va009039 0:65f1469d6bfb 331
va009039 0:65f1469d6bfb 332 #endif /* __OBJ_H__ */