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/frame.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__ 0x03 |
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 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 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 25 | frame_new(pPmObj_t pfunc, pPmObj_t *r_pobj) |
va009039 | 0:65f1469d6bfb | 26 | { |
va009039 | 0:65f1469d6bfb | 27 | PmReturn_t retval = PM_RET_OK; |
va009039 | 0:65f1469d6bfb | 28 | int16_t fsize = 0; |
va009039 | 0:65f1469d6bfb | 29 | pPmCo_t pco = C_NULL; |
va009039 | 0:65f1469d6bfb | 30 | pPmFrame_t pframe = C_NULL; |
va009039 | 0:65f1469d6bfb | 31 | uint8_t *pchunk; |
va009039 | 0:65f1469d6bfb | 32 | |
va009039 | 0:65f1469d6bfb | 33 | /* Get fxn's code obj */ |
va009039 | 0:65f1469d6bfb | 34 | pco = ((pPmFunc_t)pfunc)->f_co; |
va009039 | 0:65f1469d6bfb | 35 | |
va009039 | 0:65f1469d6bfb | 36 | /* TypeError if passed func's CO is not a true COB */ |
va009039 | 0:65f1469d6bfb | 37 | if (OBJ_GET_TYPE(pco) != OBJ_TYPE_COB) |
va009039 | 0:65f1469d6bfb | 38 | { |
va009039 | 0:65f1469d6bfb | 39 | PM_RAISE(retval, PM_RET_EX_TYPE); |
va009039 | 0:65f1469d6bfb | 40 | return retval; |
va009039 | 0:65f1469d6bfb | 41 | } |
va009039 | 0:65f1469d6bfb | 42 | |
va009039 | 0:65f1469d6bfb | 43 | #ifdef HAVE_GENERATORS |
va009039 | 0:65f1469d6bfb | 44 | /* #207: Initializing a Generator using CALL_FUNC needs extra stack slot */ |
va009039 | 0:65f1469d6bfb | 45 | fsize = sizeof(PmFrame_t) + (pco->co_stacksize + pco->co_nlocals + 2) * sizeof(pPmObj_t); |
va009039 | 0:65f1469d6bfb | 46 | #elif defined(HAVE_CLASSES) |
va009039 | 0:65f1469d6bfb | 47 | /* #230: Calling a class's __init__() takes two extra spaces on the stack */ |
va009039 | 0:65f1469d6bfb | 48 | fsize = sizeof(PmFrame_t) + (pco->co_stacksize + pco->co_nlocals + 1) * sizeof(pPmObj_t); |
va009039 | 0:65f1469d6bfb | 49 | #else |
va009039 | 0:65f1469d6bfb | 50 | fsize = sizeof(PmFrame_t) + (pco->co_stacksize + pco->co_nlocals - 1) * sizeof(pPmObj_t); |
va009039 | 0:65f1469d6bfb | 51 | #endif /* HAVE_CLASSES */ |
va009039 | 0:65f1469d6bfb | 52 | |
va009039 | 0:65f1469d6bfb | 53 | #ifdef HAVE_CLOSURES |
va009039 | 0:65f1469d6bfb | 54 | /* #256: Add support for closures */ |
va009039 | 0:65f1469d6bfb | 55 | fsize = fsize + pco->co_nfreevars |
va009039 | 0:65f1469d6bfb | 56 | + ((pco->co_cellvars == C_NULL) ? 0 : pco->co_cellvars->length); |
va009039 | 0:65f1469d6bfb | 57 | #endif /* HAVE_CLOSURES */ |
va009039 | 0:65f1469d6bfb | 58 | |
va009039 | 0:65f1469d6bfb | 59 | /* Allocate a frame */ |
va009039 | 0:65f1469d6bfb | 60 | retval = heap_getChunk(fsize, &pchunk); |
va009039 | 0:65f1469d6bfb | 61 | PM_RETURN_IF_ERROR(retval); |
va009039 | 0:65f1469d6bfb | 62 | pframe = (pPmFrame_t)pchunk; |
va009039 | 0:65f1469d6bfb | 63 | |
va009039 | 0:65f1469d6bfb | 64 | /* Set frame fields */ |
va009039 | 0:65f1469d6bfb | 65 | OBJ_SET_TYPE(pframe, OBJ_TYPE_FRM); |
va009039 | 0:65f1469d6bfb | 66 | pframe->fo_back = C_NULL; |
va009039 | 0:65f1469d6bfb | 67 | pframe->fo_func = (pPmFunc_t)pfunc; |
va009039 | 0:65f1469d6bfb | 68 | pframe->fo_memspace = pco->co_memspace; |
va009039 | 0:65f1469d6bfb | 69 | |
va009039 | 0:65f1469d6bfb | 70 | /* Init instruction pointer and block stack */ |
va009039 | 0:65f1469d6bfb | 71 | pframe->fo_ip = pco->co_codeaddr; |
va009039 | 0:65f1469d6bfb | 72 | pframe->fo_blockstack = C_NULL; |
va009039 | 0:65f1469d6bfb | 73 | |
va009039 | 0:65f1469d6bfb | 74 | /* Get globals and attrs from the function object */ |
va009039 | 0:65f1469d6bfb | 75 | pframe->fo_globals = ((pPmFunc_t)pfunc)->f_globals; |
va009039 | 0:65f1469d6bfb | 76 | pframe->fo_attrs = ((pPmFunc_t)pfunc)->f_attrs; |
va009039 | 0:65f1469d6bfb | 77 | |
va009039 | 0:65f1469d6bfb | 78 | #ifndef HAVE_CLOSURES |
va009039 | 0:65f1469d6bfb | 79 | /* Empty stack points to one past locals */ |
va009039 | 0:65f1469d6bfb | 80 | pframe->fo_sp = &(pframe->fo_locals[pco->co_nlocals]); |
va009039 | 0:65f1469d6bfb | 81 | #else |
va009039 | 0:65f1469d6bfb | 82 | /* #256: Add support for closures */ |
va009039 | 0:65f1469d6bfb | 83 | pframe->fo_sp = &(pframe->fo_locals[pco->co_nlocals + pco->co_nfreevars |
va009039 | 0:65f1469d6bfb | 84 | + ((pco->co_cellvars == C_NULL) ? 0 : pco->co_cellvars->length)]); |
va009039 | 0:65f1469d6bfb | 85 | #endif /* HAVE_CLOSURES */ |
va009039 | 0:65f1469d6bfb | 86 | |
va009039 | 0:65f1469d6bfb | 87 | /* By default, this is a normal frame, not an import or __init__ one */ |
va009039 | 0:65f1469d6bfb | 88 | pframe->fo_isImport = 0; |
va009039 | 0:65f1469d6bfb | 89 | #ifdef HAVE_CLASSES |
va009039 | 0:65f1469d6bfb | 90 | pframe->fo_isInit = 0; |
va009039 | 0:65f1469d6bfb | 91 | #endif |
va009039 | 0:65f1469d6bfb | 92 | |
va009039 | 0:65f1469d6bfb | 93 | /* Clear the stack */ |
va009039 | 0:65f1469d6bfb | 94 | sli_memset((unsigned char *)&(pframe->fo_locals), (char const)0, |
va009039 | 0:65f1469d6bfb | 95 | (unsigned int)fsize - sizeof(PmFrame_t)); |
va009039 | 0:65f1469d6bfb | 96 | |
va009039 | 0:65f1469d6bfb | 97 | /* Return ptr to frame */ |
va009039 | 0:65f1469d6bfb | 98 | *r_pobj = (pPmObj_t)pframe; |
va009039 | 0:65f1469d6bfb | 99 | return retval; |
va009039 | 0:65f1469d6bfb | 100 | } |