From 941df930b0b519cf4b82f5e46ffe49ca75e68619 Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Mon, 7 Feb 2022 15:29:04 -0500 Subject: [cup] Remove unnecessary function from codegen, add info to README --- compiler/README.md | 15 ++++++++++++++- compiler/codegen.cup | 13 ++----------- 2 files changed, 16 insertions(+), 12 deletions(-) (limited to 'compiler') diff --git a/compiler/README.md b/compiler/README.md index 0e58d43..54ab576 100644 --- a/compiler/README.md +++ b/compiler/README.md @@ -1,3 +1,16 @@ # CUP Compiler in CUP -This is the beginnings of a CUP compiler written in itself. \ No newline at end of file +This is an implementation of the CUP compiler in itself! As of this writing, it can generate identical assembly code to the compiler written in C. + +```bash +$ make +gcc -Wall -Wextra -Werror -ggdb3 -o build/cupcc <...> +$ ./build/cupcc compiler/main.cup -o 1.nasm +$ make 1.out +nasm -felf64 1.nasm -o 1.o +ld 1.o -o 1.out +$ ./1.out compiler/main.cup -o 2.nasm +$ diff 1.nasm 2.nasm +$ # no output, same generated code, yay! +``` + diff --git a/compiler/codegen.cup b/compiler/codegen.cup index c18500f..67a401c 100644 --- a/compiler/codegen.cup +++ b/compiler/codegen.cup @@ -29,12 +29,6 @@ fn emit_asm(msg: char*) { fwrite(gen_out_file, msg, strlen(msg)); } -// This is a hack because we don't support manual escaping, and -// we need to be able to include '`' in string literal definitions. -fn emit_asm_char(c: char) { - fwrite(gen_out_file, &c, 1); -} - fn emit_num(num: int) { // FIXME: Just support printing negatives directly. if (num < 0) { @@ -486,10 +480,7 @@ fn generate_program(ast: Node*, file: File*) { // cause invalid output and break everything. Maybe just output the // byte values. for (let i = 0; i < gen_string_literals.size; ++i) { - emit_asm(" global_string_"); emit_num(i); emit_asm(": db "); - emit_asm_char('`'); - emit_asm(gen_string_literals.data[i]); - emit_asm_char('`'); - emit_asm(", 0\n"); + emit_asm(" global_string_"); emit_num(i); + emit_asm(": db \`"); emit_asm(gen_string_literals.data[i]); emit_asm("\`, 0\n"); } } \ No newline at end of file -- cgit v1.2.3