/*----------------------------------------------------------------------------*/ PmReturn_t tres_pm_get_state(pPmFrame_t *ppframe) { PmReturn_t retv = PM_RET_OK; pPmObj_t pobj; pPmObj_t pobj_new; pPmInstance_t pcli; pPmDict_t pdict; int16_t index; float fval; int32_t ival; if(NATIVE_GET_NUM_ARGS() != 1) { PM_RAISE(retv, PM_RET_EX_TYPE); return retv; } pobj = NATIVE_GET_LOCAL(0); if(OBJ_GET_TYPE(pobj) != OBJ_TYPE_CLI) { PM_RAISE(retv, PM_RET_EX_TYPE); return retv; } pcli = (pPmInstance_t)pobj; pdict = pcli->cli_attrs; if(*tres_pm_io.state_len > 0) { // restore each attribute of the object for(index = pdict->length - 1; index >= 0; index--) { seglist_getItem(pdict->d_keys, index, &pobj); retv = seglist_getItem(pdict->d_vals, index, &pobj); PM_RETURN_IF_ERROR(retv); switch (OBJ_GET_TYPE(pobj)) { case OBJ_TYPE_INT: //pop int pop_int(&ival); retv = int_new(ival, &pobj_new); break; case OBJ_TYPE_FLT: //pop float pop_float(&fval); retv = float_new(fval, &pobj_new); break; default: /* Raise TypeError */ PM_RAISE(retv, PM_RET_EX_TYPE); } if (retv == PM_RET_OK) { seglist_setItem(pdict->d_vals, pobj_new, index); } } } NATIVE_SET_TOS((pPmObj_t)pcli); return retv; }
PmReturn_t obj_repr(pPmObj_t pobj, pPmObj_t *r_pstr) { uint8_t tBuffer[32]; PmReturn_t retval = PM_RET_OK; uint8_t const *pcstr = (uint8_t *)tBuffer;; C_ASSERT(pobj != C_NULL); switch (OBJ_GET_TYPE(pobj)) { case OBJ_TYPE_INT: retval = sli_ltoa10(((pPmInt_t)pobj)->val, tBuffer, sizeof(tBuffer)); PM_RETURN_IF_ERROR(retval); retval = string_new(&pcstr, r_pstr); break; #ifdef HAVE_FLOAT case OBJ_TYPE_FLT: /* #212: Use homebrew float formatter */ retval = sli_ftoa(((pPmFloat_t)pobj)->val, tBuffer, sizeof(tBuffer)); sli_strlen((char *)tBuffer); retval = string_new(&pcstr, r_pstr); break; #endif /* HAVE_FLOAT */ default: /* Otherwise raise a TypeError */ PM_RAISE(retval, PM_RET_EX_TYPE); break; } return retval; }
PmReturn_t float_print(pPmObj_t pf) { uint8_t tBuffer[32]; uint8_t bytesWritten; uint8_t i; PmReturn_t retval = PM_RET_OK; C_ASSERT(pf != C_NULL); /* Raise TypeError if obj is not an float */ if (OBJ_GET_TYPE(pf) != OBJ_TYPE_FLT) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* #196: Changed to use snprintf */ bytesWritten = snprintf((char *)&tBuffer, 32, "%f", ((pPmFloat_t) pf)->val); /* Sanity check */ C_ASSERT(bytesWritten != 0); C_ASSERT(bytesWritten < sizeof(tBuffer)); for (i = (uint8_t)0; i < bytesWritten; i++) { retval = plat_putByte(tBuffer[i]); PM_RETURN_IF_ERROR(retval); } return PM_RET_OK; }
PmReturn_t thread_new(pPmObj_t pframe, pPmObj_t *r_pobj) { PmReturn_t retval = PM_RET_OK; pPmThread_t pthread = C_NULL; C_ASSERT(pframe != C_NULL); /* If it's not a frame, raise TypeError */ if (OBJ_GET_TYPE(pframe) != OBJ_TYPE_FRM) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* Allocate a thread */ retval = heap_getChunk(sizeof(PmThread_t), (uint8_t **)r_pobj); PM_RETURN_IF_ERROR(retval); /* Set type, frame and initialize status */ pthread = (pPmThread_t)*r_pobj; OBJ_SET_TYPE(pthread, OBJ_TYPE_THR); pthread->pframe = (pPmFrame_t)pframe; pthread->interpctrl = INTERP_CTRL_CONT; return retval; }
PmReturn_t mod_new(pPmObj_t pco, pPmObj_t *pmod) { PmReturn_t retval; uint8_t *pchunk; pPmObj_t pobj; /* If it's not a code obj, raise TypeError */ if (OBJ_GET_TYPE(pco) != OBJ_TYPE_COB) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* Alloc and init func obj */ retval = heap_getChunk(sizeof(PmFunc_t), &pchunk); PM_RETURN_IF_ERROR(retval); *pmod = (pPmObj_t)pchunk; OBJ_SET_TYPE(*pmod, OBJ_TYPE_MOD); ((pPmFunc_t)*pmod)->f_co = (pPmCo_t)pco; /* Alloc and init attrs dict */ retval = dict_new(&pobj); ((pPmFunc_t)*pmod)->f_attrs = (pPmDict_t)pobj; /* A module's globals is the same as its attrs */ ((pPmFunc_t)*pmod)->f_globals = (pPmDict_t)pobj; return retval; }
PmReturn_t dict_clear(pPmObj_t pdict) { PmReturn_t retval = PM_RET_OK; C_ASSERT(pdict != C_NULL); /* Raise TypeError if arg is not a dict */ if (OBJ_GET_TYPE(pdict) != OBJ_TYPE_DIC) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* clear length */ ((pPmDict_t)pdict)->length = 0; /* Free the keys and values seglists if needed */ if (((pPmDict_t)pdict)->d_keys != C_NULL) { PM_RETURN_IF_ERROR(seglist_clear(((pPmDict_t)pdict)->d_keys)); PM_RETURN_IF_ERROR(heap_freeChunk((pPmObj_t) ((pPmDict_t)pdict)->d_keys)); ((pPmDict_t)pdict)->d_keys = C_NULL; } if (((pPmDict_t)pdict)->d_vals != C_NULL) { PM_RETURN_IF_ERROR(seglist_clear(((pPmDict_t)pdict)->d_vals)); retval = heap_freeChunk((pPmObj_t)((pPmDict_t)pdict)->d_vals); ((pPmDict_t)pdict)->d_vals = C_NULL; } return retval; }
PmReturn_t seqiter_getNext(pPmObj_t pobj, pPmObj_t *r_pitem) { PmReturn_t retval; int16_t length; C_ASSERT(pobj != C_NULL); C_ASSERT(*r_pitem != C_NULL); C_ASSERT(OBJ_GET_TYPE(pobj) == OBJ_TYPE_SQI); /* * Raise TypeError if sequence iterator's object is not a sequence * otherwise, the get sequence's length */ retval = seq_getLength(((pPmSeqIter_t)pobj)->si_sequence, &length); PM_RETURN_IF_ERROR(retval); /* Raise StopIteration if at the end of the sequence */ if (((pPmSeqIter_t)pobj)->si_index == length) { /* Make null the pointer to the sequence */ ((pPmSeqIter_t)pobj)->si_sequence = C_NULL; PM_RAISE(retval, PM_RET_EX_STOP); return retval; } /* Get the item at the current index */ retval = seq_getSubscript(((pPmSeqIter_t)pobj)->si_sequence, ((pPmSeqIter_t)pobj)->si_index, r_pitem); /* Increment the index */ ((pPmSeqIter_t)pobj)->si_index++; return retval; }
PmReturn_t seqiter_new(pPmObj_t pobj, pPmObj_t *r_pobj) { PmReturn_t retval; uint8_t *pchunk; pPmSeqIter_t psi; C_ASSERT(pobj != C_NULL); C_ASSERT(*r_pobj != C_NULL); /* Raise a TypeError if pobj is not a sequence */ if ((OBJ_GET_TYPE(pobj) != OBJ_TYPE_STR) && (OBJ_GET_TYPE(pobj) != OBJ_TYPE_TUP) && (OBJ_GET_TYPE(pobj) != OBJ_TYPE_LST)) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* Alloc a chunk for the sequence iterator obj */ retval = heap_getChunk(sizeof(PmSeqIter_t), &pchunk); PM_RETURN_IF_ERROR(retval); /* Set the sequence iterator's fields */ psi = (pPmSeqIter_t)pchunk; OBJ_SET_TYPE(psi, OBJ_TYPE_SQI); psi->si_sequence = pobj; psi->si_index = 0; *r_pobj = (pPmObj_t)psi; return retval; }
/* Returns the length of the sequence */ PmReturn_t seq_getLength(pPmObj_t pobj, int16_t *r_index) { PmReturn_t retval = PM_RET_OK; switch (OBJ_GET_TYPE(pobj)) { case OBJ_TYPE_STR: *r_index = ((pPmString_t)pobj)->length; break; case OBJ_TYPE_TUP: *r_index = ((pPmTuple_t)pobj)->length; break; case OBJ_TYPE_LST: *r_index = ((pPmList_t)pobj)->length; break; default: /* Raise TypeError, non-sequence object */ PM_RAISE(retval, PM_RET_EX_TYPE); break; } return retval; }
PmReturn_t class_new(pPmObj_t pattrs, pPmObj_t pbases, pPmObj_t pname, pPmObj_t *r_pclass) { PmReturn_t retval = PM_RET_OK; uint8_t *pchunk; pPmObj_t pobj; /* Ensure types */ if ((OBJ_GET_TYPE(pattrs) != OBJ_TYPE_DIC) || (OBJ_GET_TYPE(pbases) != OBJ_TYPE_TUP) || (OBJ_GET_TYPE(pname) != OBJ_TYPE_STR)) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* Allocate a class obj */ retval = heap_getChunk(sizeof(PmClass_t), &pchunk); PM_RETURN_IF_ERROR(retval); pobj = (pPmObj_t)pchunk; OBJ_SET_TYPE(pobj, OBJ_TYPE_CLO); /* Class has no access to its CO */ ((pPmClass_t)pobj)->cl_attrs = (pPmDict_t)pattrs; ((pPmClass_t)pobj)->cl_bases = (pPmTuple_t)pbases; *r_pclass = pobj; return retval; }
PmReturn_t dict_delItem(pPmObj_t pdict, pPmObj_t pkey) { PmReturn_t retval = PM_RET_OK; int16_t indx = 0; C_ASSERT(pdict != C_NULL); /* Check for matching key */ retval = seglist_findEqual(((pPmDict_t)pdict)->d_keys, pkey, &indx); /* Raise KeyError if key is not found */ if (retval == PM_RET_NO) { PM_RAISE(retval, PM_RET_EX_KEY); } /* Return any other error */ PM_RETURN_IF_ERROR(retval); /* Remove the key and value */ retval = seglist_removeItem(((pPmDict_t)pdict)->d_keys, indx); PM_RETURN_IF_ERROR(retval); retval = seglist_removeItem(((pPmDict_t)pdict)->d_vals, indx); /* Reduce the item count */ ((pPmDict_t)pdict)->length--; return retval; }
/* Returns the object sequence[index] */ PmReturn_t seq_getSubscript(pPmObj_t pobj, int16_t index, pPmObj_t *r_pobj) { PmReturn_t retval; uint8_t c; switch (OBJ_GET_TYPE(pobj)) { case OBJ_TYPE_STR: /* Adjust for negative index */ if (index < 0) { index += ((pPmString_t)pobj)->length; } /* Raise IndexError if index is out of bounds */ if ((index < 0) || (index > ((pPmString_t)pobj)->length)) { PM_RAISE(retval, PM_RET_EX_INDX); break; } /* Get the character from the string */ c = ((pPmString_t)pobj)->val[index]; /* Create a new string from the character */ retval = string_newFromChar(c, r_pobj); break; case OBJ_TYPE_TUP: /* Get the tuple item */ retval = tuple_getItem(pobj, index, r_pobj); break; case OBJ_TYPE_LST: /* Get the list item */ retval = list_getItem(pobj, index, r_pobj); break; default: /* Raise TypeError, unsubscriptable object */ PM_RAISE(retval, PM_RET_EX_TYPE); break; } return retval; }
PmReturn_t dict_getItem(pPmObj_t pdict, pPmObj_t pkey, pPmObj_t *r_pobj) { PmReturn_t retval = PM_RET_OK; int16_t indx = 0; /* C_ASSERT(pdict != C_NULL);*/ /* if it's not a dict, raise TypeError */ if (OBJ_GET_TYPE(pdict) != OBJ_TYPE_DIC) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* if dict is empty, raise KeyError */ if (((pPmDict_t)pdict)->length <= 0) { PM_RAISE(retval, PM_RET_EX_KEY); return retval; } /* #147: Change boolean keys to integers */ if (pkey == PM_TRUE) { pkey = PM_ONE; } else if (pkey == PM_FALSE) { pkey = PM_ZERO; } /* check for matching key */ retval = seglist_findEqual(((pPmDict_t)pdict)->d_keys, pkey, &indx); /* if key not found, raise KeyError */ if (retval == PM_RET_NO) { PM_RAISE(retval, PM_RET_EX_KEY); } /* return any other error */ PM_RETURN_IF_ERROR(retval); /* key was found, get obj from vals */ retval = seglist_getItem(((pPmDict_t)pdict)->d_vals, indx, r_pobj); return retval; }
PmReturn_t dict_update(pPmObj_t pdestdict, pPmObj_t psourcedict, uint8_t omit_underscored) { PmReturn_t retval = PM_RET_OK; int16_t i; pPmObj_t pkey; pPmObj_t pval; C_ASSERT(pdestdict != C_NULL); C_ASSERT(psourcedict != C_NULL); /* If it's not a dict, raise TypeError */ if (OBJ_GET_TYPE(pdestdict) != OBJ_TYPE_DIC) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* If it's not a dict, raise TypeError */ if (OBJ_GET_TYPE(psourcedict) != OBJ_TYPE_DIC) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } /* Iterate over the add-on dict */ for (i = 0; i < ((pPmDict_t)psourcedict)->length; i++) { /* Get the key,val from the add-on dict */ retval = seglist_getItem(((pPmDict_t)psourcedict)->d_keys, i, &pkey); PM_RETURN_IF_ERROR(retval); retval = seglist_getItem(((pPmDict_t)psourcedict)->d_vals, i, &pval); PM_RETURN_IF_ERROR(retval); if (!(omit_underscored && (OBJ_GET_TYPE(pkey) == OBJ_TYPE_STR) && ((pPmString_t)pkey)->val[0] == '_')) { /* Set the key,val to the destination dict */ retval = dict_setItem(pdestdict, pkey, pval); PM_RETURN_IF_ERROR(retval); } } return retval; }
PmReturn_t obj_loadFromImg(PmMemSpace_t memspace, uint8_t const **paddr, pPmObj_t *r_pobj) { PmReturn_t retval = PM_RET_OK; PmObj_t obj; /* Get the object descriptor */ obj.od = (PmObjDesc_t)0x0000; OBJ_SET_TYPE(&obj, mem_getByte(memspace, paddr)); switch (OBJ_GET_TYPE(&obj)) { case OBJ_TYPE_NON: /* If it's the None object, return global None */ *r_pobj = PM_NONE; break; case OBJ_TYPE_INT: /* Read an integer and create an integer object with the value */ retval = int_new(mem_getInt(memspace, paddr), r_pobj); break; #ifdef HAVE_FLOAT case OBJ_TYPE_FLT: /* Read a float and create an float object with the value */ retval = float_new(mem_getFloat(memspace, paddr), r_pobj); break; #endif /* HAVE_FLOAT */ case OBJ_TYPE_STR: retval = string_loadFromImg(memspace, paddr, r_pobj); break; case OBJ_TYPE_TUP: retval = tuple_loadFromImg(memspace, paddr, r_pobj); break; case OBJ_TYPE_NIM: /* If it's a native code img, load into a code obj */ retval = no_loadFromImg(memspace, paddr, r_pobj); break; case OBJ_TYPE_CIM: /* If it's a code img, load into a code obj */ retval = co_loadFromImg(memspace, paddr, r_pobj); break; default: /* All other types should not be in an img obj */ PM_RAISE(retval, PM_RET_EX_SYS); break; } return retval; }
PmReturn_t mod_import(pPmObj_t pstr, pPmObj_t *pmod) { pPmImgInfo_t pii = C_NULL; uint8_t const *imgaddr = C_NULL; pPmCo_t pco = C_NULL; PmReturn_t retval = PM_RET_OK; pPmObj_t pobj; /* If it's not a string obj, raise SyntaxError */ if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR) { PM_RAISE(retval, PM_RET_EX_SYNTAX); return retval; } /* Iterate through the global img list */ pii = gVmGlobal.pimglist; /* Scan until end of list or string matches */ while ((pii != C_NULL) && (string_compare((pPmString_t)pstr, pii->ii_name) == C_DIFFER)) { pii = pii->next; } /* If img was not found, raise ImportError */ if (pii == C_NULL) { PM_RAISE(retval, PM_RET_EX_IMPRT); return retval; } /* Make copy of addr so image list pointer isn't modified */ imgaddr = pii->ii_addr; /* Load img into code obj */ retval = obj_loadFromImg(pii->ii_memspace, &imgaddr, &pobj); PM_RETURN_IF_ERROR(retval); pco = (pPmCo_t)pobj; return mod_new((pPmObj_t)pco, pmod); }
/*----------------------------------------------------------------------------*/ PmReturn_t tres_pm_state_pop(pPmFrame_t *ppframe) { PmReturn_t retv = PM_RET_OK; pPmObj_t r_pflt; pPmObj_t pa; float fval; int32_t ival; int pop_retv; /* Raise TypeError if wrong number of args */ pa = NATIVE_GET_LOCAL(0); if(NATIVE_GET_NUM_ARGS() != 1) { PM_RAISE(retv, PM_RET_EX_TYPE); return retv; } switch (OBJ_GET_TYPE(pa)) { //case OBJ_TYPE_STR: // ptr = (char const *)&(((pPmString_t)pa)->val); // // TODO: unimplemented // break; case OBJ_TYPE_INT: pop_retv = pop_int(&ival); if(pop_retv != TRES_ERR_NONE) { ival = ((pPmInt_t) pa)->val; } retv = int_new(ival, &r_pflt); break; case OBJ_TYPE_FLT: pop_retv = pop_float(&fval); if(pop_retv != TRES_ERR_NONE) { fval = ((pPmFloat_t) pa)->val; } retv = float_new(fval, &r_pflt); break; default: /* Raise TypeError */ PM_RAISE(retv, PM_RET_EX_TYPE); } NATIVE_SET_TOS(r_pflt); return retv; }
/*----------------------------------------------------------------------------*/ PmReturn_t tres_pm_set_output(pPmFrame_t *ppframe) { const char *ptr; int32_t ival; float fval; PmReturn_t retv = PM_RET_OK; pPmObj_t pa; /* Raise TypeError if wrong number of args */ if(NATIVE_GET_NUM_ARGS() != 1) { PM_RAISE(retv, PM_RET_EX_TYPE); return retv; } pa = NATIVE_GET_LOCAL(0); switch (OBJ_GET_TYPE(pa)) { case OBJ_TYPE_STR: ptr = (char const *)&(((pPmString_t) pa)->val); snprintf(tres_pm_io.out, REST_MAX_CHUNK_SIZE, "%s", ptr); break; case OBJ_TYPE_INT: ival = ((pPmInt_t) pa)->val; snprintf(tres_pm_io.out, REST_MAX_CHUNK_SIZE, "%" PRId32, ival); break; case OBJ_TYPE_FLT: fval = ((pPmFloat_t) pa)->val; // we cannot use snprintf: float are not supported //snprintf(tres_pm_io.out, REST_MAX_CHUNK_SIZE, "%f", fval); //therefore we use pymite sli_ftoa, which, however requires a buffer => 15 if(REST_MAX_CHUNK_SIZE >= 15) { sli_ftoa(fval, (uint8_t *)tres_pm_io.out, REST_MAX_CHUNK_SIZE); } break; default: /* Raise TypeError */ PM_RAISE(retv, PM_RET_EX_TYPE); } tres_pm_io.output_set = 1; NATIVE_SET_TOS(PM_NONE); return retv; }
/*----------------------------------------------------------------------------*/ PmReturn_t tres_pm_save_state(pPmFrame_t *ppframe) { PmReturn_t retv = PM_RET_OK; pPmObj_t pobj; pPmInstance_t pcli; pPmDict_t pdict; uint16_t index; if(NATIVE_GET_NUM_ARGS() != 1) { PM_RAISE(retv, PM_RET_EX_TYPE); return retv; } pobj = NATIVE_GET_LOCAL(0); if(OBJ_GET_TYPE(pobj) != OBJ_TYPE_CLI) { PM_RAISE(retv, PM_RET_EX_TYPE); return retv; } pcli = (pPmInstance_t)pobj; pdict = pcli->cli_attrs; // store each attribute of the object for(index = 0; index < pdict->length; index++) { seglist_getItem(pdict->d_keys, index, &pobj); retv = seglist_getItem(pdict->d_vals, index, &pobj); PM_RETURN_IF_ERROR(retv); switch (OBJ_GET_TYPE(pobj)) { case OBJ_TYPE_INT: push_int(((pPmInt_t) pobj)->val); break; case OBJ_TYPE_FLT: push_float(((pPmFloat_t) pobj)->val); break; } } NATIVE_SET_TOS(PM_NONE); return retv; }
/* Desktop target shall use stdio for I/O routines */ PmReturn_t plat_putByte(uint8_t b) { int i; PmReturn_t retval = PM_RET_OK; i = putchar(b); fflush(stdout); if ((i != b) || (i == EOF)) { PM_RAISE(retval, PM_RET_EX_IO); } return retval; }
/* * UART send char routine MUST send exactly and only the given char; * it should not translate \n to \r\n. * This is because the interactive interface uses binary transfers. */ PmReturn_t plat_putByte(uint8_t b) { PmReturn_t retval = PM_RET_OK; if(usb_serial_putchar(b) == 0) { return retval; } else { PM_RAISE(retval, PM_RET_EX_IO); return retval; } }
PmReturn_t nat_placeholder_func(pPmFrame_t *ppframe) { /* * Use placeholder because an index * value of zero denotes the stdlib. * This function should not be called. */ PmReturn_t retval; PM_RAISE(retval, PM_RET_EX_SYS); return retval; }
/* * UART receive char routine MUST return exactly and only the received char; * it should not translate \n to \r\n. * This is because the interactive interface uses binary transfers. */ PmReturn_t plat_getByte(uint8_t *b) { PmReturn_t retval = PM_RET_OK; int c = usb_serial_getchar(); if(c == -1) { PM_RAISE(retval, PM_RET_EX_IO); return retval; } *b = c; return retval; }
/*----------------------------------------------------------------------------*/ PmReturn_t tres_pm_state_push(pPmFrame_t *ppframe) { //const char *ptr; int32_t ival; float fval; PmReturn_t retv = PM_RET_OK; pPmObj_t pa; /* Raise TypeError if wrong number of args */ pa = NATIVE_GET_LOCAL(0); if(NATIVE_GET_NUM_ARGS() != 1) { PM_RAISE(retv, PM_RET_EX_TYPE); return retv; } switch (OBJ_GET_TYPE(pa)) { //case OBJ_TYPE_STR: // ptr = (char const *)&(((pPmString_t)pa)->val); // // TODO: unimplemented // break; case OBJ_TYPE_INT: ival = ((pPmInt_t) pa)->val; push_int(ival); break; case OBJ_TYPE_FLT: fval = ((pPmFloat_t) pa)->val; push_float(fval); break; default: /* Raise TypeError */ PM_RAISE(retv, PM_RET_EX_TYPE); } NATIVE_SET_TOS(PM_NONE); return retv; }
PmReturn_t plat_getByte(uint8_t *b) { int c; PmReturn_t retval = PM_RET_OK; c = getchar(); *b = c & 0xFF; if (c == EOF) { PM_RAISE(retval, PM_RET_EX_IO); } return retval; }
/* * Allocates chunk of memory. * Filters out invalid sizes. * Rounds the size up to the next multiple of 4. * Obtains a chunk of at least the desired size. */ PmReturn_t heap_getChunk(uint16_t requestedsize, uint8_t **r_pchunk) { PmReturn_t retval; uint16_t adjustedsize; /* Ensure size request is valid */ if (requestedsize > HEAP_MAX_LIVE_CHUNK_SIZE) { PM_RAISE(retval, PM_RET_EX_MEM); return retval; } else if (requestedsize < HEAP_MIN_CHUNK_SIZE) { requestedsize = HEAP_MIN_CHUNK_SIZE; } /* * Round up the size to a multiple of 4 bytes. * This maintains alignment on 32-bit platforms (required). */ adjustedsize = ((requestedsize + 3) & ~3); /* Attempt to get a chunk */ retval = heap_getChunkImpl(adjustedsize, r_pchunk); #ifdef HAVE_GC /* Perform GC if out of memory, gc is enabled and not in native session */ if ((retval == PM_RET_EX_MEM) && (pmHeap.auto_gc == C_TRUE) && (gVmGlobal.nativeframe.nf_active == C_FALSE)) { retval = heap_gcRun(); PM_RETURN_IF_ERROR(retval); /* Attempt to get a chunk */ retval = heap_getChunkImpl(adjustedsize, r_pchunk); } #endif /* HAVE_GC */ /* Ensure that the pointer is 4-byte aligned */ if (retval == PM_RET_OK) { C_ASSERT(((intptr_t)*r_pchunk & 3) == 0); } return retval; }
/* * Allocates chunk of memory. * Filters out invalid sizes. * Rounds the size up to the next multiple of 4. * Obtains a chunk of at least the desired size. */ PmReturn_t heap_getChunk(uint16_t requestedsize, uint8_t **r_pchunk) { PmReturn_t retval; uint16_t adjustedsize; /* Ensure size request is valid */ if (requestedsize > HEAP_MAX_CHUNK_SIZE) { PM_RAISE(retval, PM_RET_EX_MEM); return retval; } if (requestedsize < HEAP_MIN_CHUNK_SIZE) { requestedsize = HEAP_MIN_CHUNK_SIZE; } /* * Round up the size to a multiple of 4 bytes. * This maintains alignment on 32-bit platforms (required). */ adjustedsize = ((requestedsize + 3) & ~3); /* Attempt to get a chunk */ retval = heap_getChunkImpl(adjustedsize, r_pchunk); /* Perform GC if out of memory and auto-gc is enabled */ if ((retval == PM_RET_EX_MEM) && (pmHeap.auto_gc == C_TRUE)) { retval = heap_gcRun(); PM_RETURN_IF_ERROR(retval); /* Attempt to get a chunk */ retval = heap_getChunkImpl(adjustedsize, r_pchunk); } /* Ensure that the pointer is 4-byte aligned */ if (retval == PM_RET_OK) { C_ASSERT(((int)*r_pchunk & 3) == 0); } return retval; }
PmReturn_t dict_print(pPmObj_t pdict) { PmReturn_t retval = PM_RET_OK; int16_t index; pSeglist_t keys, vals; pPmObj_t pobj1; C_ASSERT(pdict != C_NULL); /* if it's not a dict, raise TypeError */ if (OBJ_GET_TYPE(pdict) != OBJ_TYPE_DIC) { PM_RAISE(retval, PM_RET_EX_TYPE); return retval; } plat_putByte('{'); keys = ((pPmDict_t)pdict)->d_keys; vals = ((pPmDict_t)pdict)->d_vals; /* if dict is empty, raise KeyError */ for (index = 0; index < ((pPmDict_t)pdict)->length; index++) { if (index != 0) { plat_putByte(','); plat_putByte(' '); } retval = seglist_getItem(keys, index, &pobj1); PM_RETURN_IF_ERROR(retval); retval = obj_print(pobj1, C_FALSE, C_TRUE); PM_RETURN_IF_ERROR(retval); plat_putByte(':'); retval = seglist_getItem(vals, index, &pobj1); PM_RETURN_IF_ERROR(retval); retval = obj_print(pobj1, C_FALSE, C_TRUE); PM_RETURN_IF_ERROR(retval); } return plat_putByte('}'); }
/* * UART receive char routine MUST return exactly and only the received char; * it should not translate \n to \r\n. * This is because the interactive interface uses binary transfers. */ PmReturn_t plat_getByte(uint8_t *b) { PmReturn_t retval = PM_RET_OK; /* Loop until serial receive is complete */ loop_until_bit_is_set(UCSR0A, RXC0); /* If a framing error or data overrun occur, raise an IOException */ if (UCSR0A & (_BV(FE0) | _BV(DOR0))) { PM_RAISE(retval, PM_RET_EX_IO); return retval; } *b = UDR0; return retval; }
PmReturn_t plat_getByte(uint8_t *b) { int c; PmReturn_t retval = PM_RET_OK; while(!(U0LSR && 0x01)); //waits till the register to contain valid data c = U0RBR; *b = c & 0xFF; if (c > 0xFF) { PM_RAISE(retval, PM_RET_EX_IO); } return retval; }