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/tuple.c@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 | #undef __FILE_ID__ |
va009039 | 0:65f1469d6bfb | 10 | #define __FILE_ID__ 0x13 |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief Tuple Object Type |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * Tuple object type operations. |
va009039 | 0:65f1469d6bfb | 18 | */ |
va009039 | 0:65f1469d6bfb | 19 | |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | #include "pm.h" |
va009039 | 0:65f1469d6bfb | 22 | |
va009039 | 0:65f1469d6bfb | 23 | |
va009039 | 0:65f1469d6bfb | 24 | /* The follwing value should match that in pmImgCreator.py */ |
va009039 | 0:65f1469d6bfb | 25 | #define MAX_TUPLE_LEN 253 |
va009039 | 0:65f1469d6bfb | 26 | |
va009039 | 0:65f1469d6bfb | 27 | |
va009039 | 0:65f1469d6bfb | 28 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 29 | tuple_loadFromImg(PmMemSpace_t memspace, |
va009039 | 0:65f1469d6bfb | 30 | uint8_t const **paddr, pPmObj_t *r_ptuple) |
va009039 | 0:65f1469d6bfb | 31 | { |
va009039 | 0:65f1469d6bfb | 32 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 33 | uint8_t i = (uint8_t)0; |
va009039 | 0:65f1469d6bfb | 34 | uint8_t n = (uint8_t)0; |
va009039 | 0:65f1469d6bfb | 35 | uint8_t objid; |
va009039 | 0:65f1469d6bfb | 36 | |
va009039 | 0:65f1469d6bfb | 37 | /* Get num objs in tuple */ |
va009039 | 0:65f1469d6bfb | 38 | n = mem_getByte(memspace, paddr); |
va009039 | 0:65f1469d6bfb | 39 | |
va009039 | 0:65f1469d6bfb | 40 | /* Create empty tuple */ |
va009039 | 0:65f1469d6bfb | 41 | retval = tuple_new(n, r_ptuple); |
va009039 | 0:65f1469d6bfb | 42 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 43 | ((pPmTuple_t)*r_ptuple)->length = 0; |
va009039 | 0:65f1469d6bfb | 44 | |
va009039 | 0:65f1469d6bfb | 45 | /* Load the next n objs into tuple */ |
va009039 | 0:65f1469d6bfb | 46 | heap_gcPushTempRoot((pPmObj_t)*r_ptuple, &objid); |
va009039 | 0:65f1469d6bfb | 47 | for (i = (uint8_t)0; i < n; i++) |
va009039 | 0:65f1469d6bfb | 48 | { |
va009039 | 0:65f1469d6bfb | 49 | retval = obj_loadFromImg(memspace, |
va009039 | 0:65f1469d6bfb | 50 | paddr, |
va009039 | 0:65f1469d6bfb | 51 | (pPmObj_t *)&(((pPmTuple_t)*r_ptuple)-> |
va009039 | 0:65f1469d6bfb | 52 | val[i])); |
va009039 | 0:65f1469d6bfb | 53 | if (retval != PM_RET_OK) |
va009039 | 0:65f1469d6bfb | 54 | { |
va009039 | 0:65f1469d6bfb | 55 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 56 | return retval; |
va009039 | 0:65f1469d6bfb | 57 | } |
va009039 | 0:65f1469d6bfb | 58 | ((pPmTuple_t)*r_ptuple)->length++; |
va009039 | 0:65f1469d6bfb | 59 | } |
va009039 | 0:65f1469d6bfb | 60 | heap_gcPopTempRoot(objid); |
va009039 | 0:65f1469d6bfb | 61 | return PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 62 | } |
va009039 | 0:65f1469d6bfb | 63 | |
va009039 | 0:65f1469d6bfb | 64 | |
va009039 | 0:65f1469d6bfb | 65 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 66 | tuple_new(uint16_t n, pPmObj_t *r_ptuple) |
va009039 | 0:65f1469d6bfb | 67 | { |
va009039 | 0:65f1469d6bfb | 68 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 69 | uint16_t size = 0; |
va009039 | 0:65f1469d6bfb | 70 | |
va009039 | 0:65f1469d6bfb | 71 | /* Raise a SystemError for a Tuple that is too large */ |
va009039 | 0:65f1469d6bfb | 72 | if (n > MAX_TUPLE_LEN) |
va009039 | 0:65f1469d6bfb | 73 | { |
va009039 | 0:65f1469d6bfb | 74 | PM_RAISE(retval, PM_RET_EX_SYS); |
va009039 | 0:65f1469d6bfb | 75 | return retval; |
va009039 | 0:65f1469d6bfb | 76 | } |
va009039 | 0:65f1469d6bfb | 77 | |
va009039 | 0:65f1469d6bfb | 78 | /* Calc size of struct to hold tuple; (n-1) because PmTuple_t has val[1] */ |
va009039 | 0:65f1469d6bfb | 79 | size = sizeof(PmTuple_t) + ((n - 1) * sizeof(pPmObj_t)); |
va009039 | 0:65f1469d6bfb | 80 | |
va009039 | 0:65f1469d6bfb | 81 | /* Allocate a tuple */ |
va009039 | 0:65f1469d6bfb | 82 | retval = heap_getChunk(size, (uint8_t **)r_ptuple); |
va009039 | 0:65f1469d6bfb | 83 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 84 | OBJ_SET_TYPE(*r_ptuple, OBJ_TYPE_TUP); |
va009039 | 0:65f1469d6bfb | 85 | |
va009039 | 0:65f1469d6bfb | 86 | /* Set the number of objs in the tuple */ |
va009039 | 0:65f1469d6bfb | 87 | ((pPmTuple_t)*r_ptuple)->length = n; |
va009039 | 0:65f1469d6bfb | 88 | |
va009039 | 0:65f1469d6bfb | 89 | /* Clear entries in the tuple so the GC doesn't try to mark/sweep them */ |
va009039 | 0:65f1469d6bfb | 90 | if (n > 0) |
va009039 | 0:65f1469d6bfb | 91 | { |
va009039 | 0:65f1469d6bfb | 92 | size = n; |
va009039 | 0:65f1469d6bfb | 93 | while (--size > 0) |
va009039 | 0:65f1469d6bfb | 94 | { |
va009039 | 0:65f1469d6bfb | 95 | ((pPmTuple_t)*r_ptuple)->val[size] = C_NULL; |
va009039 | 0:65f1469d6bfb | 96 | } |
va009039 | 0:65f1469d6bfb | 97 | } |
va009039 | 0:65f1469d6bfb | 98 | |
va009039 | 0:65f1469d6bfb | 99 | /* No need to null the ptrs because they are set by the caller */ |
va009039 | 0:65f1469d6bfb | 100 | return retval; |
va009039 | 0:65f1469d6bfb | 101 | } |
va009039 | 0:65f1469d6bfb | 102 | |
va009039 | 0:65f1469d6bfb | 103 | |
va009039 | 0:65f1469d6bfb | 104 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 105 | tuple_replicate(pPmObj_t ptup, int16_t n, pPmObj_t *r_ptuple) |
va009039 | 0:65f1469d6bfb | 106 | { |
va009039 | 0:65f1469d6bfb | 107 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 108 | int16_t length; |
va009039 | 0:65f1469d6bfb | 109 | int16_t i; |
va009039 | 0:65f1469d6bfb | 110 | int16_t j; |
va009039 | 0:65f1469d6bfb | 111 | |
va009039 | 0:65f1469d6bfb | 112 | /* Raise TypeError if object is not a Tuple */ |
va009039 | 0:65f1469d6bfb | 113 | if (OBJ_GET_TYPE(ptup) != OBJ_TYPE_TUP) |
va009039 | 0:65f1469d6bfb | 114 | { |
va009039 | 0:65f1469d6bfb | 115 | PM_RAISE(retval, PM_RET_EX_SYS); |
va009039 | 0:65f1469d6bfb | 116 | return retval; |
va009039 | 0:65f1469d6bfb | 117 | } |
va009039 | 0:65f1469d6bfb | 118 | |
va009039 | 0:65f1469d6bfb | 119 | C_ASSERT(n >= 0); |
va009039 | 0:65f1469d6bfb | 120 | |
va009039 | 0:65f1469d6bfb | 121 | /* Allocate the new tuple */ |
va009039 | 0:65f1469d6bfb | 122 | length = ((pPmTuple_t)ptup)->length; |
va009039 | 0:65f1469d6bfb | 123 | retval = tuple_new(length * n, r_ptuple); |
va009039 | 0:65f1469d6bfb | 124 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 125 | |
va009039 | 0:65f1469d6bfb | 126 | /* Copy src tuple the designated number of times */ |
va009039 | 0:65f1469d6bfb | 127 | for (i = 0; i < n; i++) |
va009039 | 0:65f1469d6bfb | 128 | { |
va009039 | 0:65f1469d6bfb | 129 | for (j = 0; j < length; j++) |
va009039 | 0:65f1469d6bfb | 130 | { |
va009039 | 0:65f1469d6bfb | 131 | ((pPmTuple_t)*r_ptuple)->val[length * i + j] = |
va009039 | 0:65f1469d6bfb | 132 | ((pPmTuple_t)ptup)->val[j]; |
va009039 | 0:65f1469d6bfb | 133 | } |
va009039 | 0:65f1469d6bfb | 134 | } |
va009039 | 0:65f1469d6bfb | 135 | return retval; |
va009039 | 0:65f1469d6bfb | 136 | } |
va009039 | 0:65f1469d6bfb | 137 | |
va009039 | 0:65f1469d6bfb | 138 | |
va009039 | 0:65f1469d6bfb | 139 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 140 | tuple_getItem(pPmObj_t ptup, int16_t index, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 141 | { |
va009039 | 0:65f1469d6bfb | 142 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 143 | |
va009039 | 0:65f1469d6bfb | 144 | /* Adjust for negative index */ |
va009039 | 0:65f1469d6bfb | 145 | if (index < 0) |
va009039 | 0:65f1469d6bfb | 146 | { |
va009039 | 0:65f1469d6bfb | 147 | index += ((pPmTuple_t)ptup)->length; |
va009039 | 0:65f1469d6bfb | 148 | } |
va009039 | 0:65f1469d6bfb | 149 | |
va009039 | 0:65f1469d6bfb | 150 | /* Raise IndexError if index is out of bounds */ |
va009039 | 0:65f1469d6bfb | 151 | if ((index < 0) || (index > ((pPmTuple_t)ptup)->length)) |
va009039 | 0:65f1469d6bfb | 152 | { |
va009039 | 0:65f1469d6bfb | 153 | PM_RAISE(retval, PM_RET_EX_INDX); |
va009039 | 0:65f1469d6bfb | 154 | } |
va009039 | 0:65f1469d6bfb | 155 | |
va009039 | 0:65f1469d6bfb | 156 | /* Get the tuple item */ |
va009039 | 0:65f1469d6bfb | 157 | *r_pobj = ((pPmTuple_t)ptup)->val[index]; |
va009039 | 0:65f1469d6bfb | 158 | |
va009039 | 0:65f1469d6bfb | 159 | return retval; |
va009039 | 0:65f1469d6bfb | 160 | } |
va009039 | 0:65f1469d6bfb | 161 | |
va009039 | 0:65f1469d6bfb | 162 | |
va009039 | 0:65f1469d6bfb | 163 | #ifdef HAVE_PRINT |
va009039 | 0:65f1469d6bfb | 164 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 165 | tuple_print(pPmObj_t ptup) |
va009039 | 0:65f1469d6bfb | 166 | { |
va009039 | 0:65f1469d6bfb | 167 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 168 | int16_t index; |
va009039 | 0:65f1469d6bfb | 169 | |
va009039 | 0:65f1469d6bfb | 170 | C_ASSERT(ptup != C_NULL); |
va009039 | 0:65f1469d6bfb | 171 | |
va009039 | 0:65f1469d6bfb | 172 | /* If it's not a tuple, raise TypeError */ |
va009039 | 0:65f1469d6bfb | 173 | if (OBJ_GET_TYPE(ptup) != OBJ_TYPE_TUP) |
va009039 | 0:65f1469d6bfb | 174 | { |
va009039 | 0:65f1469d6bfb | 175 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 176 | return retval; |
va009039 | 0:65f1469d6bfb | 177 | } |
va009039 | 0:65f1469d6bfb | 178 | |
va009039 | 0:65f1469d6bfb | 179 | plat_putByte('('); |
va009039 | 0:65f1469d6bfb | 180 | |
va009039 | 0:65f1469d6bfb | 181 | for (index = 0; index < ((pPmTuple_t)ptup)->length; index++) |
va009039 | 0:65f1469d6bfb | 182 | { |
va009039 | 0:65f1469d6bfb | 183 | if (index != 0) |
va009039 | 0:65f1469d6bfb | 184 | { |
va009039 | 0:65f1469d6bfb | 185 | plat_putByte(','); |
va009039 | 0:65f1469d6bfb | 186 | plat_putByte(' '); |
va009039 | 0:65f1469d6bfb | 187 | } |
va009039 | 0:65f1469d6bfb | 188 | retval = obj_print(((pPmTuple_t)ptup)->val[index], C_FALSE, C_TRUE); |
va009039 | 0:65f1469d6bfb | 189 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 190 | } |
va009039 | 0:65f1469d6bfb | 191 | |
va009039 | 0:65f1469d6bfb | 192 | return plat_putByte(')'); |
va009039 | 0:65f1469d6bfb | 193 | } |
va009039 | 0:65f1469d6bfb | 194 | #endif /* HAVE_PRINT */ |