From 15784cc3d256ee342f7ac52d91cb6fcebd453d28 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 28 Apr 2021 14:50:06 -0400 Subject: [PATCH] added handling of most operators --- .gitignore | 1 + cerise/backend/c99/codegen.c | 43 +++++++++++++++++++++++-------- cerise/src/parser.c | 50 ------------------------------------ cerise/tests/Module.m | 4 +-- 4 files changed, 36 insertions(+), 62 deletions(-) diff --git a/.gitignore b/.gitignore index 3cb8f46..327418d 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ y.tab.* cerisec cerisec-test +cerisec-x86_64 diff --git a/cerise/backend/c99/codegen.c b/cerise/backend/c99/codegen.c index 1d8da23..bb14f9a 100644 --- a/cerise/backend/c99/codegen.c +++ b/cerise/backend/c99/codegen.c @@ -29,12 +29,22 @@ static char* TypeNames[] = { [FORM_STRING] = "char*" }; -static char* BinaryOps[] = { - ['+'] = "+", - ['-'] = "-", - ['*'] = "*", - ['/'] = "/", - ['%'] = "%", +static char* Operators[] = { + ['+'] = "+", + ['-'] = "-", + ['*'] = "*", + ['/'] = "/", + ['%'] = "%", + [AND] = "&&", + [OR] = "||", + [NOT] = "!", + [EQ] = "==", + [NEQ] = "!=", + ['<'] = "<", + [LTEQ] = "<=", + ['>'] = ">", + [GTEQ] = ">=", +// [IS] = ??? }; static void load_var(Parser* p, Item* item) @@ -66,18 +76,30 @@ static void load_var(Parser* p, Item* item) } } -/* Operators +/* Operator Handling *****************************************************************************/ -void binary_op(Parser* p, int op, Item* a, Item* b) +static void binary_op(Parser* p, int op, Item* a, Item* b) { char* type = TypeNames[a->type->form]; - char* oper = BinaryOps[op]; + char* oper = Operators[op]; + assert(type && oper); printf(" const %s _T%d = _T%d %s _T%d;\n", type, p->curr_reg, a->reg, oper, b->reg); a->reg = p->curr_reg; p->curr_reg++; } +static void unary_op(Parser* p, int op, Item* a) +{ + char* type = TypeNames[a->type->form]; + char* oper = Operators[op]; + assert(type && oper); + printf(" const %s _T%d = %s _T%d;\n", + type, p->curr_reg, oper, a->reg); + a->reg = p->curr_reg; + p->curr_reg++; +} + /* Public Interface *****************************************************************************/ void codegen_setint(Item* item, Type* type, long long val) @@ -150,7 +172,8 @@ void codegen_unop(Parser* p, int op, Item* a) } else { - assert(!"not supported"); + load_var(p, a); + unary_op(p, op, a); } } diff --git a/cerise/src/parser.c b/cerise/src/parser.c index 0c6f746..508cccb 100644 --- a/cerise/src/parser.c +++ b/cerise/src/parser.c @@ -533,56 +533,6 @@ RULE(const_decl) } } -/* Code generation for -find(obj); -OSS.Get(sym); -IF obj.class = OSG.SProc THEN - StandProc(obj.val) -ELSE - OSG.MakeItem(x, obj, level); - selector(x); - - IF sym = OSS.becomes THEN (*assignment*) - - OSS.Get(sym); - expression(y); - IF (x.type.form IN {OSG.Boolean, OSG.Integer}) & (x.type.form = y.type.form) THEN - OSG.Store(x, y) - ELSE - OSS.Mark("incompatible assignment") - END - -// ELSIF sym = OSS.eql THEN -// OSS.Mark("should be :="); -// OSS.Get(sym); -// expression(y) -// ELSIF sym = OSS.lparen THEN (*procedure call*) -// OSS.Get(sym); -// IF (obj.class = OSG.Proc) & (obj.type = NIL) THEN -// ParamList(obj); -// OSG.Call(obj); -// ELSE -// OSS.Mark("not a procedure") -// END -// ELSIF obj.class = OSG.Proc THEN (*procedure call without parameters*) -// IF obj.nofpar > 0 THEN -// OSS.Mark("missing parameters") -// END ; -// IF obj.type = NIL THEN -// OSG.Call(obj) ELSE -// OSS.Mark("not a procedure") -// END -// ELSIF (obj.class = OSG.SProc) & (obj.val = 3) THEN -// OSG.WriteLn -// ELSIF obj.class = OSG.Typ THEN -// OSS.Mark("illegal assignment") -// ELSE -// OSS.Mark("not a procedure") -// END -END - -*/ - RULE(statement_seq) { while (1) diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 960e45c..c78051e 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -20,8 +20,8 @@ begin b = B; # Unary ops -# b = +b; -# b = -b; + b = +b; + b = -b; # Immediate ops c = b + 1; -- 2.49.0