aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-03 03:42:41 -0500
committerMustafa Quraish <[email protected]>2022-02-03 03:42:41 -0500
commit3a7588e5c5b7718012917b608e2346dc066cecc2 (patch)
treeaa88655cf4210d5256377ab44ec1915a0bcebcfc /src/generator.c
parentMove builtins to a separate file (diff)
downloadcup-3a7588e5c5b7718012917b608e2346dc066cecc2.tar.xz
cup-3a7588e5c5b7718012917b608e2346dc066cecc2.zip
Remove `putc` intrinsic and replace with `write(fd, buf, size)`
`putc`, `puts`, and `putnum` in `std/common.cup` are now implemented in terms of the `write()` syscall.
Diffstat (limited to 'src/generator.c')
-rw-r--r--src/generator.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/generator.c b/src/generator.c
index f422657..aace3bc 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -170,6 +170,15 @@ void generate_expr_into_rax(Node *expr, FILE *out)
fprintf(out, " cqo\n");
fprintf(out, " idiv rbx\n");
+ } else if (expr->type == OP_MOD) {
+ generate_expr_into_rax(expr->binary.right, out);
+ fprintf(out, " push rax\n");
+ generate_expr_into_rax(expr->binary.left, out);
+ fprintf(out, " pop rbx\n");
+ fprintf(out, " cqo\n");
+ fprintf(out, " idiv rbx\n");
+ fprintf(out, " mov rax, rdx\n");
+
} else if (expr->type == OP_MUL) {
generate_expr_into_rax(expr->binary.right, out);
fprintf(out, " push rax\n");
@@ -507,13 +516,12 @@ void generate_builtins(FILE *out)
/////////////////////////////////////////////////////////////////
- // Print out a single character
+ // Write syscall
fprintf(out,
- "func_putc:\n"
- " mov rdi, 1\n" // stdout
- " mov rsi, rsp\n"
- " add rsi, 8\n"
- " mov rdx, 1\n" // 1 byte
+ "func_write:\n"
+ " mov rdi, [rsp+8]\n" // stdout
+ " mov rsi, [rsp+16]\n"
+ " mov rdx, [rsp+24]\n" // 1 byte
);
make_syscall(SYS_write, out);
fprintf(out, " ret\n");