aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-02 07:20:53 -0500
committerMustafa Quraish <[email protected]>2022-02-02 07:37:39 -0500
commit1a8f96c65f94227faa9747ef876a60f3c313c6f1 (patch)
treed80a396958ff2fc752b620cc5314e27e40b58ecb /src/generator.c
parentUse `type*` instead of `type&` to denote a pointer type (for now) (diff)
downloadcup-1a8f96c65f94227faa9747ef876a60f3c313c6f1.tar.xz
cup-1a8f96c65f94227faa9747ef876a60f3c313c6f1.zip
Type checking of expressions / functions!
This is a bit of a chonky commit, but it adds in the basics of checking the types of expressions / function calls / return types. There's still a lot of work to be done, including: (1) Adding new core types, and casting between allowed types automatically (2) Picking the corrent output type based on input types (for instance float+int == float) (3) We need much better error reporting, the error messages are really vague and unhelpful as-is (4) We also need to work to ensure that a function with a return type actually returns (5) Possible re-factoring to make stuff less hacky when we have more types / structs / arrays / etc.
Diffstat (limited to 'src/generator.c')
-rw-r--r--src/generator.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/src/generator.c b/src/generator.c
index 8663116..f9aa787 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -24,6 +24,16 @@ void make_syscall(i64 syscall_no, FILE *out) {
fprintf(out, " syscall\n");
}
+char *specifier_for_type(Type *type) {
+ switch (size_for_type(type)) {
+ case 1: return "byte";
+ case 2: return "word";
+ case 4: return "dword";
+ case 8: return "qword";
+ default: assert(false && "Unreachable");
+ }
+}
+
void generate_expr_into_rax(Node *expr, FILE *out);
void generate_lvalue_into_rax(Node *node, FILE *out)
@@ -85,7 +95,7 @@ void generate_expr_into_rax(Node *expr, FILE *out)
fprintf(out, " push rax\n");
generate_expr_into_rax(expr->assign.value, out);
fprintf(out, " pop rbx\n");
- fprintf(out, " mov [rbx], rax\n");
+ fprintf(out, " mov %s [rbx], rax\n", specifier_for_type(var->expr_type));
} else if (expr->type == OP_NEG) {
generate_expr_into_rax(expr->unary_expr, out);