From 7a1a986e28b8377dec674d02778f4f8152a9975b Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 18 Nov 2024 21:53:27 +1300 Subject: [PATCH] last commit for now --- makefile | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/codegen.c | 27 +++++++-------- src/codegen.h | 2 +- src/main.c | 10 +++--- test.c | 7 ++++ test.ssa | 4 ++- 6 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 makefile create mode 100644 test.c diff --git a/makefile b/makefile new file mode 100644 index 0000000..9928b85 --- /dev/null +++ b/makefile @@ -0,0 +1,94 @@ +# this is the build file for project +# it is autogenerated by the xmake build system. +# do not edit by hand. + +ifneq ($(VERBOSE),1) +VV=@ +endif + +CXX=/usr/bin/gcc +CU=/usr/bin/clang +NC=/home/sam/Documents/Projects/Nim/bin/nim +MXX=/usr/bin/gcc +AS=/usr/bin/gcc +CC=/usr/bin/gcc +RC=/home/sam/.cargo/bin/rustc +ZC=/snap/bin/zig +GC=/usr/bin/go +MM=/usr/bin/gcc + +AR=/usr/bin/ar +RCAR=/home/sam/.cargo/bin/rustc +ZCAR=/snap/bin/zig +NCAR=/home/sam/Documents/Projects/Nim/bin/nim +GCAR=/usr/bin/go +SH=/usr/bin/g++ +RCSH=/home/sam/.cargo/bin/rustc +ZCSH=/snap/bin/zig +NCSH=/home/sam/Documents/Projects/Nim/bin/nim +LD=/usr/bin/g++ +RCLD=/home/sam/.cargo/bin/rustc +ZCLD=/snap/bin/zig +NCLD=/home/sam/Documents/Projects/Nim/bin/nim +GCLD=/usr/bin/go + +compiler-c_LD=/usr/bin/g++ +compiler-c_CC=/usr/bin/gcc + +compiler-c_CCFLAGS=-g -O0 -isystem /home/sam/.xmake/packages/s/stc/v4.2/bfec6d3335d54b48969cc50946a9b5ac/include +compiler-c_LDFLAGS= + +default: compiler-c + +all: compiler-c + +.PHONY: default all compiler-c + +compiler-c: build/linux/arm64/debug/compiler-c +build/linux/arm64/debug/compiler-c: build/.objs/compiler-c/linux/arm64/debug/src/codegen.c.o build/.objs/compiler-c/linux/arm64/debug/src/helpers.c.o build/.objs/compiler-c/linux/arm64/debug/src/parser.c.o build/.objs/compiler-c/linux/arm64/debug/src/lexer.c.o build/.objs/compiler-c/linux/arm64/debug/src/compiler.c.o build/.objs/compiler-c/linux/arm64/debug/src/main.c.o + @echo linking.debug compiler-c + @mkdir -p build/linux/arm64/debug + $(VV)$(compiler-c_LDFLAGS)$(compiler-c_LD) -o build/linux/arm64/debug/compiler-c build/.objs/compiler-c/linux/arm64/debug/src/codegen.c.o build/.objs/compiler-c/linux/arm64/debug/src/helpers.c.o build/.objs/compiler-c/linux/arm64/debug/src/parser.c.o build/.objs/compiler-c/linux/arm64/debug/src/lexer.c.o build/.objs/compiler-c/linux/arm64/debug/src/compiler.c.o build/.objs/compiler-c/linux/arm64/debug/src/main.c.o + +build/.objs/compiler-c/linux/arm64/debug/src/codegen.c.o: src/codegen.c + @echo compiling.debug src/codegen.c + @mkdir -p build/.objs/compiler-c/linux/arm64/debug/src + $(VV)$(compiler-c_CC) -c $(compiler-c_CCFLAGS) -o build/.objs/compiler-c/linux/arm64/debug/src/codegen.c.o src/codegen.c + +build/.objs/compiler-c/linux/arm64/debug/src/helpers.c.o: src/helpers.c + @echo compiling.debug src/helpers.c + @mkdir -p build/.objs/compiler-c/linux/arm64/debug/src + $(VV)$(compiler-c_CC) -c $(compiler-c_CCFLAGS) -o build/.objs/compiler-c/linux/arm64/debug/src/helpers.c.o src/helpers.c + +build/.objs/compiler-c/linux/arm64/debug/src/parser.c.o: src/parser.c + @echo compiling.debug src/parser.c + @mkdir -p build/.objs/compiler-c/linux/arm64/debug/src + $(VV)$(compiler-c_CC) -c $(compiler-c_CCFLAGS) -o build/.objs/compiler-c/linux/arm64/debug/src/parser.c.o src/parser.c + +build/.objs/compiler-c/linux/arm64/debug/src/lexer.c.o: src/lexer.c + @echo compiling.debug src/lexer.c + @mkdir -p build/.objs/compiler-c/linux/arm64/debug/src + $(VV)$(compiler-c_CC) -c $(compiler-c_CCFLAGS) -o build/.objs/compiler-c/linux/arm64/debug/src/lexer.c.o src/lexer.c + +build/.objs/compiler-c/linux/arm64/debug/src/compiler.c.o: src/compiler.c + @echo compiling.debug src/compiler.c + @mkdir -p build/.objs/compiler-c/linux/arm64/debug/src + $(VV)$(compiler-c_CC) -c $(compiler-c_CCFLAGS) -o build/.objs/compiler-c/linux/arm64/debug/src/compiler.c.o src/compiler.c + +build/.objs/compiler-c/linux/arm64/debug/src/main.c.o: src/main.c + @echo compiling.debug src/main.c + @mkdir -p build/.objs/compiler-c/linux/arm64/debug/src + $(VV)$(compiler-c_CC) -c $(compiler-c_CCFLAGS) -o build/.objs/compiler-c/linux/arm64/debug/src/main.c.o src/main.c + +clean: clean_compiler-c + +clean_compiler-c: + @rm -rf build/linux/arm64/debug/compiler-c + @rm -rf build/linux/arm64/debug/compiler-c.sym + @rm -rf build/.objs/compiler-c/linux/arm64/debug/src/codegen.c.o + @rm -rf build/.objs/compiler-c/linux/arm64/debug/src/helpers.c.o + @rm -rf build/.objs/compiler-c/linux/arm64/debug/src/parser.c.o + @rm -rf build/.objs/compiler-c/linux/arm64/debug/src/lexer.c.o + @rm -rf build/.objs/compiler-c/linux/arm64/debug/src/compiler.c.o + @rm -rf build/.objs/compiler-c/linux/arm64/debug/src/main.c.o + diff --git a/src/codegen.c b/src/codegen.c index e720c70..3acfa70 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -17,7 +17,7 @@ void codegen(Node node, bool emit_type) { codegen_arg_decl(node.arg_decl, emit_type); break; case NODE_REFERENCE: - codegen_reference(node.reference, emit_type); + codegen_reference(node.reference); break; case NODE_NUMBER: codegen_number(node.number, emit_type); @@ -31,7 +31,7 @@ void codegen(Node node, bool emit_type) { } void codegen_function_call(FunctionCall node) { - printf("call $%s(", node.name); + printf("%s(", node.name); // printf("FunctionCall(%s", node.function_call.name); /*if(Nodes_size(&node.function_call.args) > 0) printf(", ");*/ @@ -43,7 +43,7 @@ void codegen_function_call(FunctionCall node) { printf(", "); } } - printf(")\n"); + printf(");\n"); } void codegen_function_decl(FunctionDecl node) { @@ -60,7 +60,7 @@ void codegen_function_decl(FunctionDecl node) { } void codegen_function_impl(FunctionImpl node) { - printf("export function %s $%s(", node.type, node.name); + printf("%s %s(", node.type, node.name); for(size_t i = 0; i < Nodes_size(&node.args); i++) { const Node* arg = Nodes_at(&node.args, i); codegen(*arg, true); @@ -69,7 +69,7 @@ void codegen_function_impl(FunctionImpl node) { printf(", "); } } - printf(") {\n@start\n\t"); + printf(") {\n\t"); for(size_t i = 0; i < Nodes_size(&node.body); i++) { const Node* n = Nodes_at(&node.body, i); @@ -88,28 +88,25 @@ void codegen_arg_decl(ArgDecl node, bool emit_type) { if(emit_type) { printf("%s ", node.type); } - printf("%%%s", node.name); + printf("%s", node.name); } -void codegen_reference(Reference node, bool emit_type) { +void codegen_reference(Reference node) { // printf("Reference(%s)", node.reference.name); - if(emit_type) { - printf("%s ", node.type); - } - printf("%%%s", node.name); + printf("%s", node.name); } void codegen_number(Number node, bool emit_type) { // printf("Number(%llu)", node.number.value); - if(emit_type) { - printf("w "); - } printf("%d", node.value); } void codegen_return(Return node) { - printf("ret "); + printf("return"); + if(node.value != NULL) { + printf(" "); codegen(*node.value, false); } + printf(";"); } diff --git a/src/codegen.h b/src/codegen.h index 9b2f2c7..451b3f8 100644 --- a/src/codegen.h +++ b/src/codegen.h @@ -8,7 +8,7 @@ void codegen_function_call(FunctionCall node); void codegen_function_decl(FunctionDecl node); void codegen_arg_decl(ArgDecl node, bool emit_type); void codegen_function_impl(FunctionImpl node); -void codegen_reference(Reference node, bool emit_type); +void codegen_reference(Reference node); void codegen_number(Number node, bool emit_type); void codegen_return(Return node); diff --git a/src/main.c b/src/main.c index 3180763..a10be7a 100644 --- a/src/main.c +++ b/src/main.c @@ -31,11 +31,11 @@ int main(int argc, char** argv) { .lexer = &lexer, .types = c_make(cmap_str, { - { "i8", "b" }, - { "i16", "h" }, - { "i32", "w" }, - { "i64", "l" }, - { "void", "" }, + { "i8", "char" }, + { "i16", "short" }, + { "i32", "int" }, + { "i64", "long long int" }, + { "void", "void" }, }), .symbols = { 0 }, }; diff --git a/test.c b/test.c new file mode 100644 index 0000000..98619e4 --- /dev/null +++ b/test.c @@ -0,0 +1,7 @@ +generic T; +int add(T value) { + printf("%v\n", value); + return 0; +} + +int result = add("bob"); diff --git a/test.ssa b/test.ssa index b7373cc..4ec8bf5 100644 --- a/test.ssa +++ b/test.ssa @@ -3,9 +3,11 @@ export function w $sayhi(w %char) { call $putchar(w 72) call $putchar(w 105) call $putchar(w %char) - ret 5 + %tempval =w load 5 + ret %tempval } + export function $main() { @start call $sayhi(w 74)