void potion_test_allocated(CuTest *T) { struct PNMemory *M = P->mem; void *prev = NULL; void *scanptr = (void *)((char *)M->birth_lo + PN_ALIGN(sizeof(struct PNMemory), 8)); while ((PN)scanptr < (PN)M->birth_cur) { if (((struct PNFwd *)scanptr)->fwd != POTION_FWD && ((struct PNFwd *)scanptr)->fwd != POTION_COPIED) { if (((struct PNObject *)scanptr)->vt > PN_TUSER) { vPN(Object) o = (struct PNObject *)scanptr; fprintf(stderr, "error: scanning heap from %p to %p\n", M->birth_lo, M->birth_cur); fprintf(stderr, "%p in %s region\n", scanptr, IS_GC_PROTECTED(scanptr) ? "protected" : IN_BIRTH_REGION(scanptr) ? "birth" : IN_OLDER_REGION(scanptr) ? "older" : "gc"); fprintf(stderr, "%p { uniq:0x%08x vt:0x%08x ivars[0]:0x%08lx type:0x%x}\n", scanptr, o->uniq, o->vt, o->ivars[0], potion_type((PN)scanptr)); fprintf(stderr, "prev %p: size=%d, type:0x%x (%s)\n", prev, potion_type_size(P, prev), potion_type((PN)prev), AS_STR(PN_VTABLE(PN_TYPE((PN)prev)))); #ifdef DEBUG //potion_dump_stack(P); #endif } CuAssert(T, "wrong type for allocated object", ((struct PNObject *)scanptr)->vt <= PN_TUSER); } prev = scanptr; scanptr = (void *)((char *)scanptr + potion_type_size(P, scanptr)); CuAssert(T, "allocated object goes beyond GC pointer", (PN)scanptr <= (PN)M->birth_cur); } }
void *potion_gc_copy(Potion *P, struct PNObject *ptr) { void *dst = (void *)P->mem->old_cur; PN_SIZE sz = potion_type_size(P, (const struct PNObject *)ptr); memcpy(dst, ptr, sz); P->mem->old_cur = (char *)dst + sz; ((struct PNFwd *)ptr)->fwd = POTION_COPIED; ((struct PNFwd *)ptr)->siz = sz; ((struct PNFwd *)ptr)->ptr = (PN)dst; return dst; }
void *potion_mark_major(Potion *P, const struct PNObject *ptr) { struct PNMemory *M = P->mem; PN_SIZE i; PN_SIZE sz = 16; switch (((struct PNFwd *)ptr)->fwd) { case POTION_COPIED: case POTION_FWD: GC_MAJOR_UPDATE(((struct PNFwd *)ptr)->ptr); goto done; } if (ptr->vt > PN_TUSER) { GC_MAJOR_UPDATE(PN_VTABLE(ptr->vt)); int ivars = ((struct PNVtable *)PN_VTABLE(ptr->vt))->ivlen; for (i = 0; i < ivars; i++) GC_MAJOR_UPDATE(((struct PNObject *)ptr)->ivars[i]); goto done; } switch (ptr->vt) { case PN_TWEAK: GC_MAJOR_UPDATE(((struct PNWeakRef *)ptr)->data); break; case PN_TCLOSURE: GC_MAJOR_UPDATE(((struct PNClosure *)ptr)->sig); for (i = 0; i < ((struct PNClosure *)ptr)->extra; i++) GC_MAJOR_UPDATE(((struct PNClosure *)ptr)->data[i]); break; case PN_TTUPLE: { struct PNTuple * volatile t = (struct PNTuple *)potion_fwd((PN)ptr); for (i = 0; i < t->len; i++) GC_MAJOR_UPDATE(t->set[i]); } break; case PN_TSTATE: GC_MAJOR_UPDATE(((Potion *)ptr)->strings); GC_MAJOR_UPDATE(((Potion *)ptr)->lobby); GC_MAJOR_UPDATE(((Potion *)ptr)->vts); GC_MAJOR_UPDATE(((Potion *)ptr)->source); GC_MAJOR_UPDATE(((Potion *)ptr)->input); GC_MAJOR_UPDATE(((Potion *)ptr)->pbuf); GC_MAJOR_UPDATE(((Potion *)ptr)->unclosed); GC_MAJOR_UPDATE(((Potion *)ptr)->call); GC_MAJOR_UPDATE(((Potion *)ptr)->callset); break; case PN_TFILE: GC_MAJOR_UPDATE(((struct PNFile *)ptr)->path); break; case PN_TVTABLE: GC_MAJOR_UPDATE(((struct PNVtable *)ptr)->parent); GC_MAJOR_UPDATE(((struct PNVtable *)ptr)->ivars); GC_MAJOR_UPDATE(((struct PNVtable *)ptr)->methods); GC_MAJOR_UPDATE(((struct PNVtable *)ptr)->ctor); GC_MAJOR_UPDATE(((struct PNVtable *)ptr)->call); GC_MAJOR_UPDATE(((struct PNVtable *)ptr)->callset); break; case PN_TSOURCE: GC_MAJOR_UPDATE(((struct PNSource *)ptr)->a[0]); GC_MAJOR_UPDATE(((struct PNSource *)ptr)->a[1]); GC_MAJOR_UPDATE(((struct PNSource *)ptr)->a[2]); break; case PN_TPROTO: GC_MAJOR_UPDATE(((struct PNProto *)ptr)->source); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->sig); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->stack); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->paths); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->locals); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->upvals); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->values); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->protos); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->tree); GC_MAJOR_UPDATE(((struct PNProto *)ptr)->asmb); break; case PN_TTABLE: GC_MAJOR_UPDATE_TABLE(PN, (struct PNTable *)potion_fwd((PN)ptr), 1); break; case PN_TFLEX: for (i = 0; i < PN_FLEX_SIZE(ptr); i++) GC_MAJOR_UPDATE(PN_FLEX_AT(ptr, i)); break; case PN_TCONT: GC_KEEP(ptr); pngc_mark_array(P, (_PN *)((struct PNCont *)ptr)->stack + 3, ((struct PNCont *)ptr)->len - 3, 2); break; } done: sz = potion_type_size(P, ptr); return (void *)((char *)ptr + sz); }
static void potion_cmd_compile ( char *filename, int exec, int verbose, void *sp ) { PN buf, code; int fd = -1; struct stat stats; Potion *P = potion_create(sp); if (stat(filename, &stats) == -1) { potion_warn("** %s does not exist.\n", filename); goto done; } fd = open(filename, O_RDONLY | O_BINARY); if (fd == -1) { potion_warn("** could not open %s. check permissions.\n", filename); goto done; } buf = potion_bytes(P, stats.st_size); if (read(fd, PN_STR_PTR(buf), stats.st_size) == stats.st_size) { PN_STR_PTR(buf)[stats.st_size] = '\0'; code = potion_source_load(P, PN_NIL, buf); if (PN_IS_PROTO(code)) { if (verbose > 1) printf("\n\n-- loaded --\n"); } else { code = potion_parse(P, buf); if (PN_TYPE(code) == PN_TERROR) { potion_send(potion_send(code, PN_string), PN_print); goto done; } if (verbose > 1) { printf("\n-- parsed --\n"); potion_send(potion_send(code, PN_string), PN_print); printf("\n"); } code = potion_send(code, PN_compile, potion_str(P, filename), PN_NIL); if (verbose > 1) printf("\n-- compiled --\n"); } if (verbose > 1) { potion_send(potion_send(code, PN_string), PN_print); printf("\n"); } if (exec == 1) { code = potion_vm(P, code, P->lobby, PN_NIL, 0, NULL ); if (verbose > 1) printf( "\n-- vm returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", (void *) code, PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)), PN_INT(potion_gc_reserved(P, 0, 0))); if (verbose) { potion_send(potion_send(code, PN_string), PN_print); printf("\n"); } } else if (exec == 2) { #if POTION_JIT == 1 PN val; PN cl = potion_closure_new(P, (PN_F)potion_jit_proto(P, code, POTION_JIT_TARGET), PN_NIL, 1); PN_CLOSURE(cl)->data[0] = code; val = PN_PROTO(code)->jit(P, cl, P->lobby); if (verbose > 1) printf("\n-- jit returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", PN_PROTO(code)->jit, PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)), PN_INT(potion_gc_reserved(P, 0, 0))); if (verbose) { potion_send(potion_send(val, PN_string), PN_print); printf("\n"); } #else potion_warn("** potion built without JIT support\n"); #endif } else { char pnbpath[255]; FILE *pnb; sprintf(pnbpath, "%sb", filename); pnb = fopen(pnbpath, "wb"); if (!pnb) { potion_warn("** could not open %s for writing. check permissions.\n",pnbpath); goto done; } code = potion_source_dump(P, PN_NIL, code); if (fwrite(PN_STR_PTR(code), 1, PN_STR_LEN(code), pnb) == PN_STR_LEN(code)) { potion_notice("** compiled code saved to %s\n", pnbpath); potion_notice("** run it with: potion %s\n", pnbpath); fclose(pnb); } else { potion_warn("** could not write all bytecode.\n"); } } #if 0 void *scanptr = (void *)((char *)P->mem->old_lo + (sizeof(PN) * 2)); while ((PN)scanptr < (PN)P->mem->old_cur) { printf("%p.vt = %lx (%u)\n", scanptr, ((struct PNObject *)scanptr)->vt, potion_type_size(P, scanptr)); if (((struct PNFwd *)scanptr)->fwd != POTION_FWD && ((struct PNFwd *)scanptr)->fwd != POTION_COPIED) { if (((struct PNObject *)scanptr)->vt < 0 || ((struct PNObject *)scanptr)->vt > PN_TUSER) { printf("wrong type for allocated object: %p.vt = %lx\n", scanptr, ((struct PNObject *)scanptr)->vt); break; } } scanptr = (void *)((char *)scanptr + potion_type_size(P, scanptr)); if ((PN)scanptr > (PN)P->mem->old_cur) { printf("allocated object goes beyond GC pointer\n"); break; } } #endif } else { potion_warn("** could not read entire file."); } done: if (fd != -1) close(fd); if (P != NULL ) potion_destroy(P); }