From a6786bc41cf503e46eb2b55b4cdb138caa4f263a Mon Sep 17 00:00:00 2001 From: Larry Xue Date: Mon, 24 Aug 2020 18:28:45 -0400 Subject: [PATCH] bugfixes for local vars --- README.md | 5 ++--- c2logic/compiler.py | 6 +++--- examples/{func_calls.c => factorial.c} | 15 +++++++-------- examples/funcs2.c | 2 +- examples/nested_loops.c | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 15 deletions(-) rename examples/{func_calls.c => factorial.c} (64%) create mode 100644 examples/nested_loops.c diff --git a/README.md b/README.md index 572cb92..b31a626 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Run the command line tool using: where `filename` is a string and `optimization_level` is an optional integer Optimization Level: -0. completely unoptimized +0. completely unoptimized. 1. the default - modify variables without using a temporary 2. turns on some potentially unsafe optimizations @@ -28,7 +28,7 @@ Special Variables: - \_\_rax: similar to x86 rax - \_\_rbx: stores left hand side of binary ops to avoid clobbering by the right side -- \__retaddr_\*: stores return address of func call +- \_\_retaddr\_\*: stores return address of func call When developing your script, you can include `c2logic/builtins.h` located in the python include directory(location depends on system, mine is at `~/.local/include/python3.8/`) @@ -43,7 +43,6 @@ See `include/builtins.h` for API definitions. - drawing - getlink - memory cell read/write -- actual functions - structs - enums diff --git a/c2logic/compiler.py b/c2logic/compiler.py index 8569b14..538b3eb 100644 --- a/c2logic/compiler.py +++ b/c2logic/compiler.py @@ -248,12 +248,12 @@ class Compiler(c_ast.NodeVisitor): def visit_UnaryOp(self, node): if node.op == "p++" or node.op == "p--": #postincrement/decrement - varname = node.expr.name + varname = self.get_varname(node.expr.name) if self.opt_level < 2: self.push(Set("__rax", varname)) self.push(BinaryOp(varname, varname, "1", node.op[1])) elif node.op == "++" or node.op == "--": - varname = node.expr.name + varname = self.get_varname(node.expr.name) self.push(BinaryOp(varname, varname, "1", node.op[0])) if self.opt_level < 2: self.push(Set("__rax", varname)) @@ -418,7 +418,7 @@ class Compiler(c_ast.NodeVisitor): raise ValueError(f"{name} is not a function") for param, arg in zip(func.params, args): self.visit(arg) - self.set_to_rax(f"{param}_{name}") + self.set_to_rax(f"_{param}_{name}") self.push(Set("__retaddr_" + name, self.curr_offset() + 3)) self.push(FunctionCall(name)) diff --git a/examples/func_calls.c b/examples/factorial.c similarity index 64% rename from examples/func_calls.c rename to examples/factorial.c index 1905f74..3fd91b2 100644 --- a/examples/func_calls.c +++ b/examples/factorial.c @@ -1,6 +1,9 @@ #include "c2logic/builtins.h" extern struct MindustryObject message1; double factorial(int x) { + if (x < 2) { + return 1; + } int ret = 1; for (int i = 2; i <= x; i++) { ret *= i; @@ -8,14 +11,10 @@ double factorial(int x) { return ret; } -double a(void) { - return 5; -} void main(void) { - printd(a()); - print("\n"); - printd(factorial(4)); - print("\n"); - printd(factorial(5)); + for (int i = 0; i < 10; i++) { + printd(factorial(i)); + print("\n"); + } printflush(message1); } \ No newline at end of file diff --git a/examples/funcs2.c b/examples/funcs2.c index 12103bd..bc07a4f 100644 --- a/examples/funcs2.c +++ b/examples/funcs2.c @@ -6,6 +6,6 @@ void main(void) { print("\n"); printd(y); print("\n"); - print(max(x, y) < 10); + printd(max(x, y) < 10); printflush(message1); } \ No newline at end of file diff --git a/examples/nested_loops.c b/examples/nested_loops.c new file mode 100644 index 0000000..31cf6df --- /dev/null +++ b/examples/nested_loops.c @@ -0,0 +1,14 @@ +#include "c2logic/builtins.h" +extern struct MindustryObject message1; +void a(int i) { + for (int j = 0; j < 4; j++) { + printd(i + j); + print("\n"); + } +} +void main(void) { + for (int i = 0; i < 4; i++) { + a(i); + } + printflush(message1); +} \ No newline at end of file