Browse Source

bugfixes for local vars

rlbr-dev
Larry Xue 5 years ago
parent
commit
a6786bc41c
  1. 5
      README.md
  2. 6
      c2logic/compiler.py
  3. 15
      examples/factorial.c
  4. 2
      examples/funcs2.c
  5. 14
      examples/nested_loops.c

5
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

6
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))

examples/func_calls.c → examples/factorial.c

2
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);
}

14
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);
}
Loading…
Cancel
Save