diff options
| author | Mustafa Quraish <[email protected]> | 2022-02-03 21:01:03 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-02-03 21:01:03 -0500 |
| commit | 9f3edcbeea51dd8841b9d89fa7ef98f11682a1c7 (patch) | |
| tree | b2a4b6fa51ad558b36facde2cf952a6af5a25669 /src/generator.c | |
| parent | Add automatic type inference for initialized variable declarations (diff) | |
| download | cup-9f3edcbeea51dd8841b9d89fa7ef98f11682a1c7.tar.xz cup-9f3edcbeea51dd8841b9d89fa7ef98f11682a1c7.zip | |
Add support for basic structs
Structs for now (and probably for the near future) are not allowed
to be passed by value, and instead you just pass a pointer to it.
Nested structs can also be defined, and they can be either anonymous,
or named (in which case only the members can access the type).
Diffstat (limited to 'src/generator.c')
| -rw-r--r-- | src/generator.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/generator.c b/src/generator.c index 6fcccbf..2fb89b7 100644 --- a/src/generator.c +++ b/src/generator.c @@ -50,13 +50,21 @@ void generate_expr_into_rax(Node *expr, FILE *out); void generate_lvalue_into_rax(Node *node, FILE *out) { assert(is_lvalue(node->type)); - i64 offset = node->variable->offset; if (node->type == AST_LOCAL_VAR) { + i64 offset = node->variable->offset; fprintf(out, " mov rax, rbp\n"); fprintf(out, " sub rax, %lld\n", offset); } else if (node->type == AST_GLOBAL_VAR) { + i64 offset = node->variable->offset; fprintf(out, " mov rax, global_vars\n"); fprintf(out, " add rax, %lld\n", offset); + } else if (node->type == OP_MEMBER) { + i64 offset = node->member.offset; + if (node->member.is_ptr) + generate_expr_into_rax(node->member.expr, out); + else + generate_lvalue_into_rax(node->member.expr, out); + fprintf(out, " add rax, %lld\n", offset); } else if (node->type == OP_DEREF) { generate_expr_into_rax(node->unary_expr, out); } else { |