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/frame.h@12:d27ad05214e3, 2014-08-28 (annotated)
- Committer:
- kausdev
- Date:
- Thu Aug 28 19:35:16 2014 +0000
- Revision:
- 12:d27ad05214e3
- Parent:
- 0:65f1469d6bfb
pymte
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 __FRAME_H__ |
| va009039 | 0:65f1469d6bfb | 10 | #define __FRAME_H__ |
| va009039 | 0:65f1469d6bfb | 11 | |
| va009039 | 0:65f1469d6bfb | 12 | |
| va009039 | 0:65f1469d6bfb | 13 | /** |
| va009039 | 0:65f1469d6bfb | 14 | * \file |
| va009039 | 0:65f1469d6bfb | 15 | * \brief VM Frame |
| va009039 | 0:65f1469d6bfb | 16 | * |
| va009039 | 0:65f1469d6bfb | 17 | * VM frame header. |
| va009039 | 0:65f1469d6bfb | 18 | */ |
| va009039 | 0:65f1469d6bfb | 19 | |
| va009039 | 0:65f1469d6bfb | 20 | |
| va009039 | 0:65f1469d6bfb | 21 | /** |
| va009039 | 0:65f1469d6bfb | 22 | * The maximum number of local variables a native function can have. |
| va009039 | 0:65f1469d6bfb | 23 | * This defines the length of the locals array in the native frame struct. |
| va009039 | 0:65f1469d6bfb | 24 | */ |
| va009039 | 0:65f1469d6bfb | 25 | #define NATIVE_MAX_NUM_LOCALS 8 |
| va009039 | 0:65f1469d6bfb | 26 | |
| va009039 | 0:65f1469d6bfb | 27 | |
| va009039 | 0:65f1469d6bfb | 28 | /** |
| va009039 | 0:65f1469d6bfb | 29 | * Block Type |
| va009039 | 0:65f1469d6bfb | 30 | * |
| va009039 | 0:65f1469d6bfb | 31 | * Numerical values to put in the 'b_type' field of the tPmBlockType struct. |
| va009039 | 0:65f1469d6bfb | 32 | */ |
| va009039 | 0:65f1469d6bfb | 33 | typedef enum PmBlockType_e |
| va009039 | 0:65f1469d6bfb | 34 | { |
| va009039 | 0:65f1469d6bfb | 35 | /** Invalid block type */ |
| va009039 | 0:65f1469d6bfb | 36 | B_INVALID = 0, |
| va009039 | 0:65f1469d6bfb | 37 | |
| va009039 | 0:65f1469d6bfb | 38 | /** Loop type */ |
| va009039 | 0:65f1469d6bfb | 39 | B_LOOP, |
| va009039 | 0:65f1469d6bfb | 40 | |
| va009039 | 0:65f1469d6bfb | 41 | /** Try type */ |
| va009039 | 0:65f1469d6bfb | 42 | B_TRY |
| va009039 | 0:65f1469d6bfb | 43 | } PmBlockType_t, *pPmBlockType_t; |
| va009039 | 0:65f1469d6bfb | 44 | |
| va009039 | 0:65f1469d6bfb | 45 | |
| va009039 | 0:65f1469d6bfb | 46 | /** |
| va009039 | 0:65f1469d6bfb | 47 | * Block |
| va009039 | 0:65f1469d6bfb | 48 | * |
| va009039 | 0:65f1469d6bfb | 49 | * Extra info for loops and trys (others?) |
| va009039 | 0:65f1469d6bfb | 50 | * Frames use linked list of blocks to handle |
| va009039 | 0:65f1469d6bfb | 51 | * nested loops and try-catch blocks. |
| va009039 | 0:65f1469d6bfb | 52 | */ |
| va009039 | 0:65f1469d6bfb | 53 | typedef struct PmBlock_s |
| va009039 | 0:65f1469d6bfb | 54 | { |
| va009039 | 0:65f1469d6bfb | 55 | /** Obligatory obj descriptor */ |
| va009039 | 0:65f1469d6bfb | 56 | PmObjDesc_t od; |
| va009039 | 0:65f1469d6bfb | 57 | |
| va009039 | 0:65f1469d6bfb | 58 | /** Ptr to backup stack ptr */ |
| va009039 | 0:65f1469d6bfb | 59 | pPmObj_t *b_sp; |
| va009039 | 0:65f1469d6bfb | 60 | |
| va009039 | 0:65f1469d6bfb | 61 | /** Handler fxn obj */ |
| va009039 | 0:65f1469d6bfb | 62 | uint8_t const *b_handler; |
| va009039 | 0:65f1469d6bfb | 63 | |
| va009039 | 0:65f1469d6bfb | 64 | /** Block type */ |
| va009039 | 0:65f1469d6bfb | 65 | PmBlockType_t b_type:8; |
| va009039 | 0:65f1469d6bfb | 66 | |
| va009039 | 0:65f1469d6bfb | 67 | /** Next block in stack */ |
| va009039 | 0:65f1469d6bfb | 68 | struct PmBlock_s *next; |
| va009039 | 0:65f1469d6bfb | 69 | } PmBlock_t, |
| va009039 | 0:65f1469d6bfb | 70 | *pPmBlock_t; |
| va009039 | 0:65f1469d6bfb | 71 | |
| va009039 | 0:65f1469d6bfb | 72 | |
| va009039 | 0:65f1469d6bfb | 73 | /** |
| va009039 | 0:65f1469d6bfb | 74 | * Frame |
| va009039 | 0:65f1469d6bfb | 75 | * |
| va009039 | 0:65f1469d6bfb | 76 | * A struct that holds the execution frame of a function, including the stack, |
| va009039 | 0:65f1469d6bfb | 77 | * local vars and pointer to the code object. |
| va009039 | 0:65f1469d6bfb | 78 | * |
| va009039 | 0:65f1469d6bfb | 79 | * This struct doesn't declare the stack. |
| va009039 | 0:65f1469d6bfb | 80 | * frame_new() is responsible for allocating the extra memory |
| va009039 | 0:65f1469d6bfb | 81 | * at the tail of fo_locals[] to hold both the locals and stack. |
| va009039 | 0:65f1469d6bfb | 82 | */ |
| va009039 | 0:65f1469d6bfb | 83 | typedef struct PmFrame_s |
| va009039 | 0:65f1469d6bfb | 84 | { |
| va009039 | 0:65f1469d6bfb | 85 | /** Obligatory obj descriptor */ |
| va009039 | 0:65f1469d6bfb | 86 | PmObjDesc_t od; |
| va009039 | 0:65f1469d6bfb | 87 | |
| va009039 | 0:65f1469d6bfb | 88 | /** Ptr to previous frame obj */ |
| va009039 | 0:65f1469d6bfb | 89 | struct PmFrame_s *fo_back; |
| va009039 | 0:65f1469d6bfb | 90 | |
| va009039 | 0:65f1469d6bfb | 91 | /** Ptr to fxn obj */ |
| va009039 | 0:65f1469d6bfb | 92 | pPmFunc_t fo_func; |
| va009039 | 0:65f1469d6bfb | 93 | |
| va009039 | 0:65f1469d6bfb | 94 | /** Mem space where func's CO comes from */ |
| va009039 | 0:65f1469d6bfb | 95 | PmMemSpace_t fo_memspace:8; |
| va009039 | 0:65f1469d6bfb | 96 | |
| va009039 | 0:65f1469d6bfb | 97 | /** Instrxn ptr (pts into memspace) */ |
| va009039 | 0:65f1469d6bfb | 98 | uint8_t const *fo_ip; |
| va009039 | 0:65f1469d6bfb | 99 | |
| va009039 | 0:65f1469d6bfb | 100 | /** Linked list of blocks */ |
| va009039 | 0:65f1469d6bfb | 101 | pPmBlock_t fo_blockstack; |
| va009039 | 0:65f1469d6bfb | 102 | |
| va009039 | 0:65f1469d6bfb | 103 | /** Local attributes dict (non-fast locals) */ |
| va009039 | 0:65f1469d6bfb | 104 | pPmDict_t fo_attrs; |
| va009039 | 0:65f1469d6bfb | 105 | |
| va009039 | 0:65f1469d6bfb | 106 | /** Global attributes dict (pts to root frame's globals */ |
| va009039 | 0:65f1469d6bfb | 107 | pPmDict_t fo_globals; |
| va009039 | 0:65f1469d6bfb | 108 | |
| va009039 | 0:65f1469d6bfb | 109 | /** Points to next empty slot in fo_locals (1 past TOS) */ |
| va009039 | 0:65f1469d6bfb | 110 | pPmObj_t *fo_sp; |
| va009039 | 0:65f1469d6bfb | 111 | |
| va009039 | 0:65f1469d6bfb | 112 | /** Frame can be an import-frame that handles RETURN differently */ |
| va009039 | 0:65f1469d6bfb | 113 | uint8_t fo_isImport:1; |
| va009039 | 0:65f1469d6bfb | 114 | |
| va009039 | 0:65f1469d6bfb | 115 | #ifdef HAVE_CLASSES |
| va009039 | 0:65f1469d6bfb | 116 | /** Flag to indicate class initailzer frame; handle RETURN differently */ |
| va009039 | 0:65f1469d6bfb | 117 | uint8_t fo_isInit:1; |
| va009039 | 0:65f1469d6bfb | 118 | #endif /* HAVE_CLASSES */ |
| va009039 | 0:65f1469d6bfb | 119 | |
| va009039 | 0:65f1469d6bfb | 120 | /** Array of local vars and stack (space appended at alloc) */ |
| va009039 | 0:65f1469d6bfb | 121 | pPmObj_t fo_locals[1]; |
| va009039 | 0:65f1469d6bfb | 122 | /* WARNING: Do not put new fields below fo_locals */ |
| va009039 | 0:65f1469d6bfb | 123 | } PmFrame_t, |
| va009039 | 0:65f1469d6bfb | 124 | *pPmFrame_t; |
| va009039 | 0:65f1469d6bfb | 125 | |
| va009039 | 0:65f1469d6bfb | 126 | |
| va009039 | 0:65f1469d6bfb | 127 | /** |
| va009039 | 0:65f1469d6bfb | 128 | * Native Frame |
| va009039 | 0:65f1469d6bfb | 129 | * |
| va009039 | 0:65f1469d6bfb | 130 | * A struct that holds the execution frame of a native function, |
| va009039 | 0:65f1469d6bfb | 131 | * including the args and single stack slot, and pointer to the code object. |
| va009039 | 0:65f1469d6bfb | 132 | * |
| va009039 | 0:65f1469d6bfb | 133 | * This struct doesn't need an OD because it is only used statically in the |
| va009039 | 0:65f1469d6bfb | 134 | * globals struct. There's only one native frame, the global one. |
| va009039 | 0:65f1469d6bfb | 135 | * This happens because a native function is a leaf node |
| va009039 | 0:65f1469d6bfb | 136 | * in the call tree (a native func can't call python funcs). |
| va009039 | 0:65f1469d6bfb | 137 | */ |
| va009039 | 0:65f1469d6bfb | 138 | typedef struct PmNativeFrame_s |
| va009039 | 0:65f1469d6bfb | 139 | { |
| va009039 | 0:65f1469d6bfb | 140 | /** Object descriptor */ |
| va009039 | 0:65f1469d6bfb | 141 | PmObjDesc_t od; |
| va009039 | 0:65f1469d6bfb | 142 | |
| va009039 | 0:65f1469d6bfb | 143 | /** Ptr to previous frame obj */ |
| va009039 | 0:65f1469d6bfb | 144 | struct PmFrame_s *nf_back; |
| va009039 | 0:65f1469d6bfb | 145 | |
| va009039 | 0:65f1469d6bfb | 146 | /** Ptr to fxn obj */ |
| va009039 | 0:65f1469d6bfb | 147 | pPmFunc_t nf_func; |
| va009039 | 0:65f1469d6bfb | 148 | |
| va009039 | 0:65f1469d6bfb | 149 | /** Single stack slot */ |
| va009039 | 0:65f1469d6bfb | 150 | pPmObj_t nf_stack; |
| va009039 | 0:65f1469d6bfb | 151 | |
| va009039 | 0:65f1469d6bfb | 152 | /** Boolean to indicate if the native frame is active */ |
| va009039 | 0:65f1469d6bfb | 153 | uint8_t nf_active; |
| va009039 | 0:65f1469d6bfb | 154 | |
| va009039 | 0:65f1469d6bfb | 155 | /** Number of args passed to the native function */ |
| va009039 | 0:65f1469d6bfb | 156 | uint8_t nf_numlocals; |
| va009039 | 0:65f1469d6bfb | 157 | |
| va009039 | 0:65f1469d6bfb | 158 | /** Local vars */ |
| va009039 | 0:65f1469d6bfb | 159 | pPmObj_t nf_locals[NATIVE_MAX_NUM_LOCALS]; |
| va009039 | 0:65f1469d6bfb | 160 | } PmNativeFrame_t, |
| va009039 | 0:65f1469d6bfb | 161 | *pPmNativeFrame_t; |
| va009039 | 0:65f1469d6bfb | 162 | |
| va009039 | 0:65f1469d6bfb | 163 | |
| va009039 | 0:65f1469d6bfb | 164 | /** |
| va009039 | 0:65f1469d6bfb | 165 | * Allocate space for a new frame, fill its fields |
| va009039 | 0:65f1469d6bfb | 166 | * with respect to the given function object. |
| va009039 | 0:65f1469d6bfb | 167 | * Return pointer to the new frame. |
| va009039 | 0:65f1469d6bfb | 168 | * |
| va009039 | 0:65f1469d6bfb | 169 | * @param pfunc ptr to Function object. |
| va009039 | 0:65f1469d6bfb | 170 | * @param r_pobj Return value; the new frame. |
| va009039 | 0:65f1469d6bfb | 171 | * @return Return status. |
| va009039 | 0:65f1469d6bfb | 172 | */ |
| va009039 | 0:65f1469d6bfb | 173 | PmReturn_t frame_new(pPmObj_t pfunc, pPmObj_t *r_pobj); |
| va009039 | 0:65f1469d6bfb | 174 | |
| va009039 | 0:65f1469d6bfb | 175 | #endif /* __FRAME_H__ */ |
