Skip to content

Commit

Permalink
Avoid calling scope_find in emit-wasm
Browse files Browse the repository at this point in the history
Keep `VarInfo`.
  • Loading branch information
tyfkda committed Jan 29, 2024
1 parent 72cf524 commit 1d94871
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
27 changes: 6 additions & 21 deletions src/wcc/emit_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,7 @@ static void emit_import_section(EmitWasm *ew) {
for (int it = 0; (it = table_iterate(&func_info_table, it, &name, (void**)&info)) != -1; ) {
if (info->flag == 0 || info->func != NULL)
continue;
const Type *type = info->type;
assert(type != NULL && type->kind == TY_FUNC);

VarInfo *varinfo = scope_find(global_scope, name, NULL);
if (varinfo == NULL) {
error("Import: `%.*s' not found", NAMES(name));
}
if (varinfo->type->kind != TY_FUNC) {
error("Import: `%.*s' is not function", NAMES(name));
}
VarInfo *varinfo = info->varinfo;
if (varinfo->storage & VS_STATIC) {
error("Import: `%.*s' is not public", NAMES(name));
}
Expand Down Expand Up @@ -335,25 +326,19 @@ static void emit_export_section(EmitWasm *ew, Vector *exports) {
int num_exports = 0;
for (int i = 0; i < exports->len; ++i) {
const Name *name = exports->data[i];
VarInfo *varinfo = scope_find(global_scope, name, NULL);
if (varinfo == NULL) {
FuncInfo *info = table_get(&func_info_table, name);
if (info == NULL) {
error("Export: `%.*s' not found", NAMES(name));
}
if (varinfo->type->kind != TY_FUNC) {
error("Export: `%.*s' is not function", NAMES(name));
}
assert(info->func != NULL);
VarInfo *varinfo = info->varinfo;
if (varinfo->storage & VS_STATIC) {
error("Export: `%.*s' is not public", NAMES(name));
}

FuncInfo *info = table_get(&func_info_table, name);
assert(info != NULL && info->func != NULL);

uint32_t func_index = info->index;

data_string(&exports_section, name->chars, name->bytes); // export name
data_uleb128(&exports_section, -1, IMPORT_FUNC); // export kind
data_uleb128(&exports_section, -1, func_index); // export func index
data_uleb128(&exports_section, -1, info->index); // export func index
++num_exports;
}
// Export globals.
Expand Down
31 changes: 17 additions & 14 deletions src/wcc/traverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,39 +72,42 @@ int getsert_func_type_index(const Type *type, bool reg) {

extern int get_func_type_index(const Type *type);

static FuncInfo *register_func_info(const Name *funcname, Function *func, const Type *type,
int flag) {
static FuncInfo *register_func_info(const Name *funcname, Function *func, int flag) {
assert(func == NULL || func->type->kind == TY_FUNC);
FuncInfo *info;
if (!table_try_get(&func_info_table, funcname, (void**)&info)) {
info = calloc_or_die(sizeof(*info));
table_put(&func_info_table, funcname, info);
info->type_index = (uint32_t)-1;

VarInfo *varinfo = scope_find(global_scope, funcname, NULL);
assert(varinfo != NULL);
assert(varinfo->type->kind == TY_FUNC);
assert(func == NULL || same_type(varinfo->type, func->type));
info->varinfo = varinfo;
}
if (func != NULL)
info->func = func;
if (type != NULL) {
info->type = type;
if (info->type_index == (uint32_t)-1)
info->type_index = getsert_func_type_index(type, true);
}
if (info->type_index == (uint32_t)-1)
info->type_index = getsert_func_type_index(info->varinfo->type, true);
info->flag |= flag;
return info;
}

// static void register_func_info_if_not_exist(const Name *funcname, Type *(*callback)(void)) {
// if (table_get(&func_info_table, funcname) == NULL) {
// Type *functype = (*callback)();
// register_func_info(funcname, NULL, functype, FF_REFERED);
// scope_add(global_scope, funcname, functype, 0);
// register_func_info(funcname, NULL, FF_REFERED);
// }
// }

static uint32_t register_indirect_function(const Name *name, const Type *type) {
static uint32_t register_indirect_function(const Name *name) {
FuncInfo *info;
if (table_try_get(&indirect_function_table, name, (void**)&info))
return info->indirect_index;

info = register_func_info(name, NULL, type, FF_INDIRECT);
info = register_func_info(name, NULL, FF_INDIRECT);
uint32_t index = indirect_function_table.count + 1;
info->indirect_index = index;
table_put(&indirect_function_table, name, info);
Expand Down Expand Up @@ -177,7 +180,7 @@ static void traverse_func_expr(Expr **pexpr) {
}
if (global && type->kind == TY_FUNC) {
if (!table_try_get(&builtin_function_table, expr->var.name, NULL))
register_func_info(expr->var.name, NULL, type, FF_REFERED);
register_func_info(expr->var.name, NULL, FF_REFERED);
} else {
assert(type->kind == TY_PTR && type->pa.ptrof->kind == TY_FUNC);
getsert_func_type_index(type->pa.ptrof, true);
Expand Down Expand Up @@ -248,7 +251,7 @@ static void te_var(Expr **pexpr, bool needval) {
Expr *expr = *pexpr;
UNUSED(needval);
if (expr->type->kind == TY_FUNC) {
register_indirect_function(expr->var.name, expr->type);
register_indirect_function(expr->var.name);
traverse_func_expr(pexpr);
}
}
Expand Down Expand Up @@ -625,7 +628,7 @@ static void traverse_defun(Function *func) {
scope_add(func->scopes->data[0], name, tyvalist, 0);
}

register_func_info(func->name, func, func->type, 0);
register_func_info(func->name, func, 0);
curfunc = func;
traverse_stmt(func->body_block);
if (compile_error_count == 0) {
Expand Down Expand Up @@ -697,7 +700,7 @@ uint32_t traverse_ast(Vector *decls, Vector *exports, uint32_t stack_size) {
FuncInfo *info = table_get(&func_info_table, name);
if (info == NULL)
error("`%.*s' not found", NAMES(name));
register_func_info(name, NULL, info->type, FF_REFERED);
register_func_info(name, NULL, FF_REFERED);
}

{
Expand Down
2 changes: 1 addition & 1 deletion src/wcc/wcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern uint32_t data_end_address;

typedef struct {
Function *func;
const Type *type;
VarInfo *varinfo;
const Name *bpname;
uint32_t index;
int flag;
Expand Down

0 comments on commit 1d94871

Please sign in to comment.