Skip to content

Commit

Permalink
Fix global variables traversal
Browse files Browse the repository at this point in the history
To emit extern-ed variable in local scope into linking section.
  • Loading branch information
tyfkda committed Feb 19, 2024
1 parent ac6d092 commit aad7af9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/wcc/emit_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,15 @@ static Vector *construct_data_segment(void) {
uint32_t address = 0;
#endif
for (int k = 0; k < 2; ++k) { // 0=data, 1=bss
for (int i = 0, len = global_scope->vars->len; i < len; ++i) {
VarInfo *varinfo = global_scope->vars->data[i];
const Name *name;
GVarInfo *info;
for (int it = 0; (it = table_iterate(&gvar_info_table, it, &name, (void**)&info)) != -1; ) {
const VarInfo *varinfo = info->varinfo;
if (varinfo->storage & (VS_EXTERN | VS_ENUM_MEMBER) || varinfo->type->kind == TY_FUNC)
continue;
if ((k == 0) == (varinfo->global.init == NULL) ||
!is_global_datsec_var(varinfo, global_scope))
continue;
GVarInfo *info = get_gvar_info_from_name(varinfo->name);
if (info == NULL)
continue;

#ifndef NDEBUG
uint32_t adr = info->non_prim.address;
Expand Down Expand Up @@ -417,13 +416,12 @@ static void emit_global_section(EmitWasm *ew) {
data_open_chunk(&globals_section);
uint32_t globals_count = 0;
for (int k = 0; k < 2; ++k) { // 0=resolved(data), 1=resolved(bss)
for (int i = 0, len = global_scope->vars->len; i < len; ++i) {
VarInfo *varinfo = global_scope->vars->data[i];
const Name *name;
GVarInfo *info;
for (int it = 0; (it = table_iterate(&gvar_info_table, it, &name, (void**)&info)) != -1; ) {
const VarInfo *varinfo = info->varinfo;
if (varinfo->storage & VS_ENUM_MEMBER || varinfo->type->kind == TY_FUNC)
continue;
GVarInfo *info = get_gvar_info_from_name(varinfo->name);
if (info == NULL)
continue;

if ((info->flag & GVF_UNRESOLVED) || is_global_datsec_var(varinfo, global_scope) ||
(varinfo->global.init == NULL) == (k == 0))
Expand Down Expand Up @@ -678,8 +676,10 @@ static void emit_linking_section(EmitWasm *ew) {
}
// Globals
for (int k = 0; k < 3; ++k) { // 0=unresolved, 1=resolved(data), 2=resolved(bss)
for (int i = 0, len = global_scope->vars->len; i < len; ++i) {
VarInfo *varinfo = global_scope->vars->data[i];
const Name *name;
GVarInfo *info;
for (int it = 0; (it = table_iterate(&gvar_info_table, it, &name, (void**)&info)) != -1; ) {
const VarInfo *varinfo = info->varinfo;
if (varinfo->storage & VS_ENUM_MEMBER || varinfo->type->kind == TY_FUNC)
continue;
GVarInfo *info = get_gvar_info_from_name(varinfo->name);
Expand Down
24 changes: 14 additions & 10 deletions src/wcc/traverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ static void traverse_vardecl(Stmt *stmt) {
if (varinfo->type->kind == TY_FUNC) {
// Local extern function declaration.
register_func_info(decl->ident, NULL, varinfo, 0);
} else if (varinfo->storage & VS_EXTERN) {
assert(!is_global_scope(curscope));
if (out_type < OutExecutable) {
// Register into global to output linking information.
GVarInfo *info = register_gvar_info(decl->ident, varinfo); // TODO: Confirm.
info->flag |= GVF_UNRESOLVED;
}
}
if (!(varinfo->storage & (VS_EXTERN | VS_STATIC)))
traverse_initializer(varinfo->local.init);
Expand Down Expand Up @@ -783,13 +790,11 @@ uint32_t traverse_ast(Vector *decls, Vector *exports, uint32_t stack_size) {
for (int k = 0; k < 3; ++k) { // 0=unresolved, 1=resolved(data), 2=resolved(bss)
static const char *kTitle[] = {"import", "data", "bss"};
VERBOSE("### Globals(%s)\n", kTitle[k]);
for (int i = 0, len = global_scope->vars->len; i < len; ++i) {
VarInfo *varinfo = global_scope->vars->data[i];
GVarInfo *info;
for (int it = 0; (it = table_iterate(&gvar_info_table, it, &name, (void**)&info)) != -1; ) {
const VarInfo *varinfo = info->varinfo;
if (varinfo->storage & VS_ENUM_MEMBER || varinfo->type->kind == TY_FUNC)
continue;
GVarInfo *info = get_gvar_info_from_name(varinfo->name);
if (info == NULL)
continue;
if ((k == 0 && !(info->flag & GVF_UNRESOLVED)) ||
(k != 0 && ((info->flag & GVF_UNRESOLVED) || (varinfo->global.init == NULL) == (k == 1))))
continue;
Expand Down Expand Up @@ -841,15 +846,14 @@ uint32_t traverse_ast(Vector *decls, Vector *exports, uint32_t stack_size) {
for (int k = 0; k < 2; ++k) { // 0: data, 1: bss
if (k == 1)
VERBOSE("---- BSS 0x%x\n", address);
for (int i = 0, len = global_scope->vars->len; i < len; ++i) {
VarInfo *varinfo = global_scope->vars->data[i];
const Name *name;
GVarInfo *info;
for (int it = 0; (it = table_iterate(&gvar_info_table, it, &name, (void**)&info)) != -1; ) {
const VarInfo *varinfo = info->varinfo;
if (varinfo->storage & (VS_EXTERN | VS_ENUM_MEMBER) || varinfo->type->kind == TY_FUNC)
continue;
if ((varinfo->global.init == NULL) == (k == 0) || !is_global_datsec_var(varinfo, global_scope))
continue;
GVarInfo *info = get_gvar_info_from_name(varinfo->name);
if (info == NULL)
continue;

// Mapped to memory
address = ALIGN(address, align_size(varinfo->type));
Expand Down

0 comments on commit aad7af9

Please sign in to comment.