Skip to content

Commit

Permalink
codegen global array variables
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Sep 1, 2024
1 parent 7c648b2 commit 5cfe81e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 20 additions & 0 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,25 @@ void codegen_generate_global_variable_for_union(struct node *node) {
asm_keyword_for_size(variable_size(node), tmp_buf));
}

void codegen_generate_variable_for_array(struct node *node) {
if (node->var.val != NULL) {
compiler_error(current_process, "Arrays with values not supported");
}

char tmp_buf[256];
asm_push("%s: %s 0", node->var.name,
asm_keyword_for_size(variable_size(node), tmp_buf));
}

void codegen_generate_global_variable(struct node *node) {
asm_push("; %s %s", node->var.type.type_str, node->var.name);

if (node->var.type.flags & DATATYPE_FLAG_IS_ARRAY) {
codegen_generate_variable_for_array(node);
codegen_new_scope_entity(node, 0, 0);
return;
}

switch (node->var.type.type) {
case DATA_TYPE_VOID:
case DATA_TYPE_CHAR:
Expand All @@ -568,6 +585,9 @@ void codegen_generate_global_variable(struct node *node) {
compiler_error(current_process, "Unsupported data type");
break;
}

assert(node->type == NODE_TYPE_VARIABLE);
codegen_new_scope_entity(node, 0, 0);
}

void codegen_generate_struct(struct node *node) {
Expand Down
11 changes: 3 additions & 8 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
union abc {
int x;
int y;
};

union abc a;
int array[10];

int main() {
a.x = 20;
return a.y;
array[0] = 1;
array[1] = 2;
}

0 comments on commit 5cfe81e

Please sign in to comment.