Skip to content
Permalink
Browse files

Global variables are added.

  • Loading branch information...
moizumi99 committed Dec 8, 2018
1 parent a270279 commit 1b336b69b8596e9ab5a52f32fd6b080bfe7668a5
Showing with 59 additions and 18 deletions.
  1. +13 −3 codegen.c
  2. +30 −6 main.c
  3. +1 −1 note.md
  4. +9 −7 parse.c
  5. +6 −1 test.sh
@@ -18,6 +18,12 @@ void gen_block(Vector *block_code) {

void gen_lval(Node *node) {
if (node->ty == ND_IDENT) {
if (get_global_symbol_address(node->name) != NULL) {
// global address.
printf(" lea rax, %s[rip]\n", node->name);
printf(" push rax\n");
return;
}
printf(" mov rax, rbp\n");
void *address = get_local_symbol_address(node->name);
// If new variable, create a room.
@@ -65,9 +71,13 @@ void gen(Node *node) {
}

if (node->ty == ND_IDENT) {
gen_lval(node);
printf(" pop rax\n");
printf(" mov rax, [rax]\n");
if (get_global_symbol_address(node->name) != NULL) {
printf(" mov rax, QWORD PTR %s[rip]\n", node->name);
} else {
gen_lval(node);
printf(" pop rax\n");
printf(" mov rax, [rax]\n");
}
printf(" push rax\n");
return;
}
36 main.c
@@ -65,14 +65,34 @@ int main(int argc, char **argv) {
}
*(sp-1) = '\0';
fclose(srcfile);

// Tokenize
tokenize(src);
// Parse
program(program_code);

printf(".intel_syntax noprefix\n");
printf(" .intel_syntax noprefix\n");
printf(" .text\n");

// Global variables.
for (int i = 0; i < global_symbols->keys->len; i++) {
Symbol *s = global_symbols->vals->data[i];
char *name = (char *) global_symbols->keys->data[i];
if (s->type == ID_VAR) {
printf(" .globl %s\n", name);
printf(" .bss\n");
printf(" .align 8\n");
printf(" .type %s, @object\n", name);
printf(" .size %s, 4\n", name);
printf("%s:\n", name);
printf(" .zero 8\n");
}
}

// Main function.
printf(" .text\n");
printf(".global main\n");
printf(".type main, @function\n");
printf("main:\n");
printf(" call func_main\n");
printf(" ret\n");
@@ -81,12 +101,16 @@ int main(int argc, char **argv) {
current_local_symbols = (Map *)local_symbols->data[j];
//dump_symbols(current_local_symbols);
// functions
Node *function = get_function_p(j);
if (function->ty != ND_FUNCDEF) {
Node *identifier = get_function_p(j);
if (identifier->ty == ND_IDENT) {
// TODO: add initialization.
continue;
}
if (identifier->ty != ND_FUNCDEF) {
fprintf(stderr, "The first line of the function isn't function definition");
exit(1);
}
Node *func_ident = function->lhs;
Node *func_ident = identifier->lhs;
if (strcmp(func_ident->name, "main") == 0) {
printf("func_main:\n");
} else {
@@ -114,7 +138,7 @@ int main(int argc, char **argv) {
}
}
// Generate codes from the top line to bottom
Vector *block = function->block;
Vector *block = identifier->block;
gen_block(block);

// The evaluated value is at the top of stack.
@@ -22,7 +22,7 @@ Nov 21.
So far, p to the middle of 3 is done.
Let's implement variable using map and vector.

Nov 26, Function declaration without arguments is done.


# Note

16 parse.c
@@ -193,7 +193,7 @@ Node *term() {
return node;
} else {
// If not followed by (, it's a variable.
if (get_local_symbol(id->name) == NULL) {
if (get_global_symbol(id->name) == NULL && get_local_symbol(id->name) == NULL) {
add_local_symbol(id->name, ID_VAR);
}
return id;
@@ -371,16 +371,18 @@ void code_block(Vector *code) {
void identifier_node(Vector *code) {
Node *id = new_node_ident(GET_TOKEN(pos).val, GET_TOKEN(pos).input,
GET_TOKEN(pos).len);
if (get_global_symbol(id->name) == NULL) {
add_global_symbol(id->name, ID_FUNC);
} else {
error("Function name conflict. \"%s\"", id->name);
if (get_global_symbol(id->name) != NULL) {
error("Global name conflict. \"%s\"", id->name);
}
pos++;
if (GET_TOKEN(pos++).ty != '(') {
error("Left parenthesis '(' missing (function): \"%s\"",
GET_TOKEN(pos - 1).input);
// global variable.
vec_push(code, id);
add_global_symbol(id->name, ID_VAR);
// TODO: add initialization.
return;
}
add_global_symbol(id->name, ID_FUNC);
Node *arg = NULL;
if (GET_TOKEN(pos).ty != ')') {
arg = argument();
@@ -85,8 +85,13 @@ try 55 'main(){b=0; a=1; while (a<11) {b = b + a; a = a + 1;} b;}'
try 11 'main(){for(a=1; a<11; a=a+1) {b=0;} a;}'
try 55 'main(){b=0; for(a=1; a<11; a = a+1) {b = b + a;} b;}'

# putchar test
try 2 'main(){putchar(97); putchar(13); putchar(10); 2;}'

run 0 program/hello.c

# global function.
try 10 'a; main(){a=10;}'
try 10 'a; f(){a;} main(){a=10;f();}'


echo OK

0 comments on commit 1b336b6

Please sign in to comment.
You can’t perform that action at this time.