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.
Fork of pymite by
vm/codeobj.c@0:65f1469d6bfb, 2013-03-02 (annotated)
- Committer:
- va009039
- Date:
- Sat Mar 02 11:54:20 2013 +0000
- Revision:
- 0:65f1469d6bfb
first commit
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__ 0x01 |
| 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 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 image format is defined by co_to_str() in src/tools/pmImgCreator.py */ |
| va009039 | 0:65f1469d6bfb | 25 | PmReturn_t |
| va009039 | 0:65f1469d6bfb | 26 | co_loadFromImg(PmMemSpace_t memspace, uint8_t const **paddr, pPmObj_t *r_pco) |
| va009039 | 0:65f1469d6bfb | 27 | { |
| va009039 | 0:65f1469d6bfb | 28 | PmReturn_t retval = PM_RET_OK; |
| va009039 | 0:65f1469d6bfb | 29 | pPmObj_t pobj; |
| va009039 | 0:65f1469d6bfb | 30 | pPmCo_t pco = C_NULL; |
| va009039 | 0:65f1469d6bfb | 31 | uint8_t *pchunk; |
| va009039 | 0:65f1469d6bfb | 32 | uint8_t objid; |
| va009039 | 0:65f1469d6bfb | 33 | #ifdef HAVE_DEBUG_INFO |
| va009039 | 0:65f1469d6bfb | 34 | uint8_t objtype; |
| va009039 | 0:65f1469d6bfb | 35 | uint16_t len_str; |
| va009039 | 0:65f1469d6bfb | 36 | #endif /* HAVE_DEBUG_INFO */ |
| va009039 | 0:65f1469d6bfb | 37 | |
| va009039 | 0:65f1469d6bfb | 38 | /* Store ptr to top of code img (less type byte) */ |
| va009039 | 0:65f1469d6bfb | 39 | uint8_t const *pci = *paddr - 1; |
| va009039 | 0:65f1469d6bfb | 40 | |
| va009039 | 0:65f1469d6bfb | 41 | /* Get size of code img */ |
| va009039 | 0:65f1469d6bfb | 42 | uint16_t size = mem_getWord(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 43 | |
| va009039 | 0:65f1469d6bfb | 44 | /* Allocate a code obj */ |
| va009039 | 0:65f1469d6bfb | 45 | retval = heap_getChunk(sizeof(PmCo_t), &pchunk); |
| va009039 | 0:65f1469d6bfb | 46 | PM_RETURN_IF_ERROR(retval); |
| va009039 | 0:65f1469d6bfb | 47 | pco = (pPmCo_t)pchunk; |
| va009039 | 0:65f1469d6bfb | 48 | |
| va009039 | 0:65f1469d6bfb | 49 | /* Fill in the CO struct */ |
| va009039 | 0:65f1469d6bfb | 50 | OBJ_SET_TYPE(pco, OBJ_TYPE_COB); |
| va009039 | 0:65f1469d6bfb | 51 | pco->co_memspace = memspace; |
| va009039 | 0:65f1469d6bfb | 52 | pco->co_argcount = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 53 | pco->co_flags = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 54 | pco->co_stacksize = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 55 | pco->co_nlocals = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 56 | |
| va009039 | 0:65f1469d6bfb | 57 | /* Do not set code image address if image is in RAM. |
| va009039 | 0:65f1469d6bfb | 58 | * CIs in RAM have their image address set in obj_loadFromImgObj() */ |
| va009039 | 0:65f1469d6bfb | 59 | pco->co_codeimgaddr = (memspace == MEMSPACE_RAM) ? C_NULL : pci; |
| va009039 | 0:65f1469d6bfb | 60 | |
| va009039 | 0:65f1469d6bfb | 61 | /* Set these to null in case a GC occurs before their objects are alloc'd */ |
| va009039 | 0:65f1469d6bfb | 62 | pco->co_names = C_NULL; |
| va009039 | 0:65f1469d6bfb | 63 | pco->co_consts = C_NULL; |
| va009039 | 0:65f1469d6bfb | 64 | pco->co_codeaddr = C_NULL; |
| va009039 | 0:65f1469d6bfb | 65 | |
| va009039 | 0:65f1469d6bfb | 66 | #ifdef HAVE_CLOSURES |
| va009039 | 0:65f1469d6bfb | 67 | pco->co_nfreevars = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 68 | pco->co_cellvars = C_NULL; |
| va009039 | 0:65f1469d6bfb | 69 | #endif /* HAVE_CLOSURES */ |
| va009039 | 0:65f1469d6bfb | 70 | |
| va009039 | 0:65f1469d6bfb | 71 | #ifdef HAVE_DEBUG_INFO |
| va009039 | 0:65f1469d6bfb | 72 | pco->co_firstlineno = mem_getWord(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 73 | pco->co_lnotab = C_NULL; |
| va009039 | 0:65f1469d6bfb | 74 | pco->co_filename = C_NULL; |
| va009039 | 0:65f1469d6bfb | 75 | #endif /* HAVE_DEBUG_INFO */ |
| va009039 | 0:65f1469d6bfb | 76 | |
| va009039 | 0:65f1469d6bfb | 77 | /* Load names (tuple obj) */ |
| va009039 | 0:65f1469d6bfb | 78 | heap_gcPushTempRoot((pPmObj_t)pco, &objid); |
| va009039 | 0:65f1469d6bfb | 79 | retval = obj_loadFromImg(memspace, paddr, &pobj); |
| va009039 | 0:65f1469d6bfb | 80 | heap_gcPopTempRoot(objid); |
| va009039 | 0:65f1469d6bfb | 81 | PM_RETURN_IF_ERROR(retval); |
| va009039 | 0:65f1469d6bfb | 82 | pco->co_names = (pPmTuple_t)pobj; |
| va009039 | 0:65f1469d6bfb | 83 | |
| va009039 | 0:65f1469d6bfb | 84 | #ifdef HAVE_DEBUG_INFO |
| va009039 | 0:65f1469d6bfb | 85 | /* Get address in memspace of line number table (including length) */ |
| va009039 | 0:65f1469d6bfb | 86 | objtype = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 87 | C_ASSERT(objtype == OBJ_TYPE_STR); |
| va009039 | 0:65f1469d6bfb | 88 | pco->co_lnotab = *paddr; |
| va009039 | 0:65f1469d6bfb | 89 | len_str = mem_getWord(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 90 | *paddr = *paddr + len_str; |
| va009039 | 0:65f1469d6bfb | 91 | |
| va009039 | 0:65f1469d6bfb | 92 | /* Get address in memspace of CO's filename (excluding length) */ |
| va009039 | 0:65f1469d6bfb | 93 | objtype = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 94 | C_ASSERT(objtype == OBJ_TYPE_STR); |
| va009039 | 0:65f1469d6bfb | 95 | len_str = mem_getWord(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 96 | pco->co_filename = *paddr; |
| va009039 | 0:65f1469d6bfb | 97 | *paddr = *paddr + len_str; |
| va009039 | 0:65f1469d6bfb | 98 | #endif /* HAVE_DEBUG_INFO */ |
| va009039 | 0:65f1469d6bfb | 99 | |
| va009039 | 0:65f1469d6bfb | 100 | /* Load consts (tuple obj) assume it follows names */ |
| va009039 | 0:65f1469d6bfb | 101 | heap_gcPushTempRoot((pPmObj_t)pco, &objid); |
| va009039 | 0:65f1469d6bfb | 102 | retval = obj_loadFromImg(memspace, paddr, &pobj); |
| va009039 | 0:65f1469d6bfb | 103 | heap_gcPopTempRoot(objid); |
| va009039 | 0:65f1469d6bfb | 104 | PM_RETURN_IF_ERROR(retval); |
| va009039 | 0:65f1469d6bfb | 105 | pco->co_consts = (pPmTuple_t)pobj; |
| va009039 | 0:65f1469d6bfb | 106 | |
| va009039 | 0:65f1469d6bfb | 107 | #ifdef HAVE_CLOSURES |
| va009039 | 0:65f1469d6bfb | 108 | heap_gcPushTempRoot((pPmObj_t)pco, &objid); |
| va009039 | 0:65f1469d6bfb | 109 | retval = obj_loadFromImg(memspace, paddr, &pobj); |
| va009039 | 0:65f1469d6bfb | 110 | heap_gcPopTempRoot(objid); |
| va009039 | 0:65f1469d6bfb | 111 | PM_RETURN_IF_ERROR(retval); |
| va009039 | 0:65f1469d6bfb | 112 | |
| va009039 | 0:65f1469d6bfb | 113 | /* Save RAM, don't keep empty tuple */ |
| va009039 | 0:65f1469d6bfb | 114 | if (((pPmTuple_t)pobj)->length == 0) |
| va009039 | 0:65f1469d6bfb | 115 | { |
| va009039 | 0:65f1469d6bfb | 116 | heap_freeChunk(pobj); |
| va009039 | 0:65f1469d6bfb | 117 | } |
| va009039 | 0:65f1469d6bfb | 118 | else |
| va009039 | 0:65f1469d6bfb | 119 | { |
| va009039 | 0:65f1469d6bfb | 120 | pco->co_cellvars = (pPmTuple_t)pobj; |
| va009039 | 0:65f1469d6bfb | 121 | } |
| va009039 | 0:65f1469d6bfb | 122 | #endif /* HAVE_CLOSURES */ |
| va009039 | 0:65f1469d6bfb | 123 | |
| va009039 | 0:65f1469d6bfb | 124 | /* Start of bcode always follows consts */ |
| va009039 | 0:65f1469d6bfb | 125 | pco->co_codeaddr = *paddr; |
| va009039 | 0:65f1469d6bfb | 126 | |
| va009039 | 0:65f1469d6bfb | 127 | /* Set addr to point one past end of img */ |
| va009039 | 0:65f1469d6bfb | 128 | *paddr = pci + size; |
| va009039 | 0:65f1469d6bfb | 129 | |
| va009039 | 0:65f1469d6bfb | 130 | *r_pco = (pPmObj_t)pco; |
| va009039 | 0:65f1469d6bfb | 131 | return PM_RET_OK; |
| va009039 | 0:65f1469d6bfb | 132 | } |
| va009039 | 0:65f1469d6bfb | 133 | |
| va009039 | 0:65f1469d6bfb | 134 | |
| va009039 | 0:65f1469d6bfb | 135 | void |
| va009039 | 0:65f1469d6bfb | 136 | co_rSetCodeImgAddr(pPmCo_t pco, uint8_t const *pimg) |
| va009039 | 0:65f1469d6bfb | 137 | { |
| va009039 | 0:65f1469d6bfb | 138 | uint8_t i; |
| va009039 | 0:65f1469d6bfb | 139 | |
| va009039 | 0:65f1469d6bfb | 140 | pco->co_codeimgaddr = pimg; |
| va009039 | 0:65f1469d6bfb | 141 | |
| va009039 | 0:65f1469d6bfb | 142 | /* Set the image address for any COs in the constant pool */ |
| va009039 | 0:65f1469d6bfb | 143 | for (i = 0; i < pco->co_consts->length; i++) |
| va009039 | 0:65f1469d6bfb | 144 | { |
| va009039 | 0:65f1469d6bfb | 145 | if (OBJ_GET_TYPE(pco->co_consts->val[i]) == OBJ_TYPE_COB) |
| va009039 | 0:65f1469d6bfb | 146 | { |
| va009039 | 0:65f1469d6bfb | 147 | co_rSetCodeImgAddr((pPmCo_t)pco->co_consts->val[i], pimg); |
| va009039 | 0:65f1469d6bfb | 148 | } |
| va009039 | 0:65f1469d6bfb | 149 | } |
| va009039 | 0:65f1469d6bfb | 150 | |
| va009039 | 0:65f1469d6bfb | 151 | return; |
| va009039 | 0:65f1469d6bfb | 152 | } |
| va009039 | 0:65f1469d6bfb | 153 | |
| va009039 | 0:65f1469d6bfb | 154 | |
| va009039 | 0:65f1469d6bfb | 155 | PmReturn_t |
| va009039 | 0:65f1469d6bfb | 156 | no_loadFromImg(PmMemSpace_t memspace, uint8_t const **paddr, pPmObj_t *r_pno) |
| va009039 | 0:65f1469d6bfb | 157 | { |
| va009039 | 0:65f1469d6bfb | 158 | PmReturn_t retval = PM_RET_OK; |
| va009039 | 0:65f1469d6bfb | 159 | pPmNo_t pno = C_NULL; |
| va009039 | 0:65f1469d6bfb | 160 | uint8_t *pchunk; |
| va009039 | 0:65f1469d6bfb | 161 | |
| va009039 | 0:65f1469d6bfb | 162 | /* Allocate a code obj */ |
| va009039 | 0:65f1469d6bfb | 163 | retval = heap_getChunk(sizeof(PmNo_t), &pchunk); |
| va009039 | 0:65f1469d6bfb | 164 | PM_RETURN_IF_ERROR(retval); |
| va009039 | 0:65f1469d6bfb | 165 | pno = (pPmNo_t)pchunk; |
| va009039 | 0:65f1469d6bfb | 166 | |
| va009039 | 0:65f1469d6bfb | 167 | /* Fill in the NO struct */ |
| va009039 | 0:65f1469d6bfb | 168 | OBJ_SET_TYPE(pno, OBJ_TYPE_NOB); |
| va009039 | 0:65f1469d6bfb | 169 | pno->no_argcount = mem_getByte(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 170 | |
| va009039 | 0:65f1469d6bfb | 171 | /* Get index into native fxn table */ |
| va009039 | 0:65f1469d6bfb | 172 | pno->no_funcindx = (int16_t)mem_getWord(memspace, paddr); |
| va009039 | 0:65f1469d6bfb | 173 | |
| va009039 | 0:65f1469d6bfb | 174 | *r_pno = (pPmObj_t)pno; |
| va009039 | 0:65f1469d6bfb | 175 | return PM_RET_OK; |
| va009039 | 0:65f1469d6bfb | 176 | } |
