SymTabESP sym_insert_tmp(Type_t tmp_type) { SymBucketSP p; SymTabESP e; e = NULL; char *name = genTmp(); int h = hash(name); if (TOP == NULL) fprintf(tiplist, "SYMTAB BUG:363\n"); for (p = *(TOP->sbp + h); p != NULL; p = p->next) { if ((p->ep != NULL) && (!strcmp(name, p->ep->name)) ) break; } if (p == NULL) { ENTRY(SymTabES, e); e->name = copyString(name); e->label = Nappend(name); e->val = -1; e->lineno = -1; e->level = LEVEL; e->posi = TOP->posi_tmp++; e->obj = Tmp_Obj_t; e->type = tmp_type; e->stp = TOP; ENTRY(SymBucketS, p); p->ep = e; p->next = NULL; sym_insert(p, TOP); } else { fprintf(tiplist, "SYMTAB BUG:393"); } return e; }
SymTabESP sym_insert_var(IdentSP idp) { SymBucketSP p; SymTabESP e; e = NULL; int h = hash(idp->name); if (TOP == NULL) fprintf(tiplist, "SYMTAB BUG:121\n"); for (p = *(TOP->sbp + h); p != NULL; p = p->next) { if ((p->ep != NULL) && (!strcmp(idp->name, p->ep->name)) ) break; } if (p == NULL) { ENTRY(SymTabES, e); e->name = copyString(idp->name); e->label = Nappend(idp->name); e->lineno = idp->line; e->level = LEVEL; e->posi = TOP->posi_var; switch (idp->type) { case Int_Var_Ident_t: e->type = Int_Type_t; e->obj = Var_Obj_t; e->val = -1; TOP->posi_var++; break; case Char_Var_Ident_t: e->type = Char_Type_t; e->obj = Var_Obj_t; e->val = -1; TOP->posi_var++; break; case IntArr_Var_Ident_t: e->type = Int_Type_t; e->obj = Array_Obj_t; e->val = idp->length; TOP->posi_var += e->val; break; case CharArr_Var_Ident_t: e->type = Char_Type_t; e->obj = Array_Obj_t; e->val = idp->length; TOP->posi_var += e->val; break; default: fprintf(tiplist, "SYMTAB BUG: 143\n"); } e->stp = TOP; ENTRY(SymBucketS, p); p->ep = e; p->next = NULL; sym_insert(p, TOP); } else { --runlevel; semanticError(DUPSYM, idp->line, FALSE, idp->name); } return e; }
SymTabESP sym_insert_para(IdentSP idp) { SymBucketSP p; SymTabESP e; e = NULL; int h = hash(idp->name); if (TOP == NULL) fprintf(tiplist, "SYMTAB BUG:263\n"); for (p = *(TOP->sbp + h); p != NULL; p = p->next) { if ((p->ep != NULL) && (!strcmp(idp->name, p->ep->name)) ) break; } if (p == NULL) { ENTRY(SymTabES, e); e->name = copyString(idp->name); e->label = Nappend(idp->name); e->val = -1; e->lineno = idp->line; e->level = LEVEL; e->posi = TOP->posi_para++; switch (idp->type) { case Int_Para_Val_Ident_t: e->obj = Para_Val_Obj_t; e->type = Int_Type_t; break; case Int_Para_Ref_Ident_t: e->obj = Para_Ref_Obj_t; e->type = Int_Type_t; break; case Char_Para_Val_Ident_t: e->obj = Para_Val_Obj_t; e->type = Char_Type_t; break; case Char_Para_Ref_Ident_t: e->obj = Para_Ref_Obj_t; e->type = Char_Type_t; break; default: fprintf(tiplist, "SYMTAB BUG: 291\n"); } e->stp = TOP; recParaInfo(e); ENTRY(SymBucketS, p); p->ep = e; p->next = NULL; sym_insert(p, TOP); } else { --runlevel; semanticError(DUPSYM, idp->line, FALSE, idp->name); } return e; }
SymTabESP sym_insert_fun(IdentSP idp, ParaListSP plp) { char *funlabel; char *funsign; SymBucketSP p; SymTabESP e; int h = hash(idp->name); funsign = signature(idp, plp); funlabel = Nappend(funsign); if (TOP == NULL) fprintf(tiplist, "SYMTAB BUG:121\n"); for (p = *(TOP->sbp + h); p != NULL; p = p->next) { if ((p->ep != NULL) && (!strcmp(idp->name, p->ep->name)) ) break; } if (p == NULL) { ENTRY(SymTabES, e); e->name = copyString(idp->name); e->label = funlabel; Npush(funsign); e->val = -1; e->lineno = idp->line; e->level = LEVEL; e->posi = -1; e->obj = Fun_Obj_t; switch (idp->type) { case Int_Fun_Ident_t: e->type = Int_Type_t; break; case Char_Fun_Ident_t: e->type = Char_Type_t; break; default: fprintf(tiplist, "SYMTAB BUG: 201\n"); } e->stp = TOP; ENTRY(SymBucketS, p); p->ep = e; p->next = NULL; sym_insert(p, TOP); } else { --runlevel; semanticError(DUPSYM, idp->line, FALSE, idp->name); } return e; }
SymTabESP sym_insert_proc(IdentSP idp, ParaListSP plp) { char *proclabel; char *procsign; SymBucketSP p; SymTabESP e; e = NULL; int h = hash(idp->name); procsign = signature(idp, plp); proclabel = Nappend(procsign); if (TOP == NULL) fprintf(tiplist, "SYMTAB BUG:121\n"); for (p = *(TOP->sbp + h); p != NULL; p = p->next) { if ((p->ep != NULL) && (!strcmp(idp->name, p->ep->name)) ) break; } if (p == NULL) { ENTRY(SymTabES, e); e->name = copyString(idp->name); e->label = proclabel; Npush(procsign); e->val = -1; e->lineno = idp->line; e->level = LEVEL; e->posi = -1; e->obj = Proc_Obj_t; e->type = Nop_Type_t; e->stp = TOP; ENTRY(SymBucketS, p); p->ep = e; p->next = NULL; sym_insert(p, TOP); } else { --runlevel; semanticError(DUPSYM, idp->line, FALSE, idp->name); } return e; }
/* Build the machine code. */ static int build_code(BuildCtx *ctx) { int status; int i; /* Initialize DynASM structures. */ ctx->nglob = GLOB__MAX; ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *)); memset(ctx->glob, 0, ctx->nglob*sizeof(void *)); ctx->nreloc = 0; ctx->globnames = globnames; ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *)); ctx->nrelocsym = 0; for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1; ctx->dasm_ident = DASM_IDENT; ctx->dasm_arch = DASM_ARCH; dasm_init(Dst, DASM_MAXSECTION); dasm_setupglobal(Dst, ctx->glob, ctx->nglob); dasm_setup(Dst, build_actionlist); /* Call arch-specific backend to emit the code. */ ctx->npc = build_backend(ctx); /* Finalize the code. */ (void)dasm_checkstep(Dst, -1); if ((status = dasm_link(Dst, &ctx->codesz))) return status; ctx->code = (uint8_t *)malloc(ctx->codesz); if ((status = dasm_encode(Dst, (void *)ctx->code))) return status; /* Allocate symbol table and bytecode offsets. */ ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin"); ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym)); ctx->nsym = 0; ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t)); /* Collect the opcodes (PC labels). */ for (i = 0; i < ctx->npc; i++) { int32_t ofs = dasm_getpclabel(Dst, i); if (ofs < 0) return 0x22000000|i; ctx->bc_ofs[i] = ofs; if ((LJ_HASJIT || !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP || i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) && (LJ_HASFFI || i != BC_KCDATA)) sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]); } /* Collect the globals (named labels). */ for (i = 0; i < ctx->nglob; i++) { const char *gl = globnames[i]; int len = (int)strlen(gl); if (!ctx->glob[i]) { fprintf(stderr, "Error: undefined global %s\n", gl); exit(2); } /* Skip the _Z symbols. */ if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z')) sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code), LABEL_PREFIX, globnames[i]); } /* Close the address range. */ sym_insert(ctx, (int32_t)ctx->codesz, "", ""); ctx->nsym--; dasm_free(Dst); return 0; }
Token lex_scan(lex *l) { int t; for (;;) { t = lex_getc(l); if (t == ' ' || t == '\t') { ; /* strip whitespace */ } else if (t == '\n') { l->lineno++; } else if (isdigit(t)) { int v; v = 0; do { v = 10 * v + (t - '0'); t = lex_getc(l); } while (isdigit(t)); if (t != EOF) lex_ungetc(l, t); l->val = v; return NUMBER; } else if (isalpha(t)) { char *end; char *p; int sym; end = l->lexbuf + l->lexbufsz; p = l->lexbuf; do { *p++ = t; t = lex_getc(l); } while (p < end && isalnum(t)); *p++ = '\0'; if (t != EOF) lex_ungetc(l, t); sym = sym_lookup(l->sym, l->lexbuf); if (!sym) sym = sym_insert(l->sym, l->lexbuf, IDENT); l->val = sym; return IDENT; } else if (t == EOF) { return END; } else { l->val = NONE; return t; } } }