PmReturn_t co_loadFromImg(PmMemSpace_t memspace, uint8_t const **paddr, pPmObj_t *r_pco) { PmReturn_t retval = PM_RET_OK; pPmObj_t pobj; pPmCo_t pco = C_NULL; uint8_t *pchunk; /* Store ptr to top of code img (less type byte) */ uint8_t const *pci = *paddr - 1; /* Get size of code img */ uint16_t size = mem_getWord(memspace, paddr); /* Allocate a code obj */ retval = heap_getChunk(sizeof(PmCo_t), &pchunk); PM_RETURN_IF_ERROR(retval); pco = (pPmCo_t)pchunk; /* Fill in the CO struct */ OBJ_SET_TYPE(pco, OBJ_TYPE_COB); pco->co_memspace = memspace; pco->co_codeimgaddr = pci; /* Load names (tuple obj) */ *paddr = pci + CI_NAMES_FIELD; retval = obj_loadFromImg(memspace, paddr, &pobj); PM_RETURN_IF_ERROR(retval); pco->co_names = (pPmTuple_t)pobj; /* Load consts (tuple obj) assume it follows names */ retval = obj_loadFromImg(memspace, paddr, &pobj); PM_RETURN_IF_ERROR(retval); pco->co_consts = (pPmTuple_t)pobj; /* Start of bcode always follows consts */ pco->co_codeaddr = *paddr; /* Set addr to point one past end of img */ *paddr = pci + size; *r_pco = (pPmObj_t)pco; return PM_RET_OK; }
PmReturn_t obj_loadFromImgObj(pPmObj_t pimg, pPmObj_t *r_pobj) { uint8_t const *imgaddr; PmReturn_t retval; C_ASSERT(OBJ_GET_TYPE(pimg) == OBJ_TYPE_CIO); imgaddr = (uint8_t const *)&(((pPmCodeImgObj_t)pimg)->val); retval = obj_loadFromImg(MEMSPACE_RAM, &imgaddr, r_pobj); C_ASSERT(OBJ_GET_TYPE(*r_pobj) == OBJ_TYPE_COB); /* All COs must reference the top of the code img obj * so the image is marked and prevented from being reclaimed */ co_rSetCodeImgAddr((pPmCo_t)*r_pobj, (uint8_t const *)pimg); 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); }
/** * Tests interpret(): * retval is OK */ void ut_interp_interpret_000(CuTest *tc) { PmReturn_t retval; uint8_t const *pimg = test_code_image0; pPmObj_t pcodeobject; pPmObj_t pmodule; pm_init(MEMSPACE_RAM, C_NULL); /* Check the return value of the load function */ retval = obj_loadFromImg(MEMSPACE_PROG, &pimg, &pcodeobject); CuAssertTrue(tc, retval == PM_RET_OK); retval = mod_new(pcodeobject, &pmodule); CuAssertTrue(tc, retval == PM_RET_OK); retval = interp_addThread((pPmFunc_t)pmodule); CuAssertTrue(tc, retval == PM_RET_OK); /* Check that the module is interpreted and its return value is ok */ retval = interpret(C_TRUE); CuAssertTrue(tc, retval == PM_RET_OK); }