commit 2343dad778541f4119e9277ddf1f1a7271ba56d2
parent 2215c34db5349131d5855bbd4bc69dcf4d6b64cc
Author: Brian Swetland <swetland@frotz.net>
Date: Wed, 8 Dec 2021 08:08:59 -0800
compiler2: cleanup
- no more AST_LABEL, just generate an assignment if needed
- start sketching some generalized assignment handling
Diffstat:
2 files changed, 42 insertions(+), 25 deletions(-)
diff --git a/src/codegen-risc5-simple.c b/src/codegen-risc5-simple.c
@@ -282,11 +282,37 @@ u32 gen_assign_expr(Ast expr, Symbol sym) {
}
}
+#if 0
+u32 gen_assign_loc(Ast lhs) {
+ if (lhs->kind == AST_NAME) {
+ u32 base;
+ i32 offset;
+ sym_get_loc(lhs->sym, &base, &offset);
+ r = get_reg_tmp();
+ if (lhs->sym->flags & SYM_IS_REFERNCE) {
+ gen_opi(ADD, r, base, offset);
+
+ return r;
+ } else if (lhs->kind == AST_INDEX) {
+ u32 ar = gen_assign_loc(lhs->c0);
+ u32 r = gen_expr(lhs->c1);
+
+ u32 lbase;
+ i32 loffset;
+ u32 kind = gen_assign_loc(lhs->c0, &lbase, &loffset);
+
+ } else if (lhs->kind == AST_DEREF) {
+ } else {
+ err_ast = lhs;
+ error("invalid lhs expr %s", ast_kind[lhs->kind]);
+ }
+}
+#endif
+
u32 gen_assign(Ast lhs, Ast expr) {
gen_trace("gen_assign()");
- if ((lhs->kind == AST_LOCAL) ||
- (lhs->kind == AST_NAME)) {
+ if (lhs->kind == AST_NAME) {
u32 base;
i32 offset;
sym_get_loc(lhs->sym, &base, &offset);
@@ -296,6 +322,8 @@ u32 gen_assign(Ast lhs, Ast expr) {
return r;
} else if (lhs->kind == AST_INDEX) {
error("wip");
+ } else if (lhs->kind == AST_DEREF) {
+
} else {
err_ast = lhs;
error("illegal on lhs (%s)", ast_kind[lhs->kind]);
@@ -503,9 +531,6 @@ u32 gen_expr(Ast node) {
} else if (node->kind == AST_BINOP) {
u32 op = node->ival;
if (op == tASSIGN) {
- if (node->c0->kind != AST_NAME) {
- error("unhandled complex assignment");
- }
return gen_assign(node->c0, node->c1);
} else if ((op & tcMASK) == tcRELOP) {
return gen_relop(node, rel_op_to_cc_tab[op - tEQ]);
@@ -637,11 +662,6 @@ void gen_stmt(Ast node) {
if (kind == AST_EXPR) {
u32 r = gen_expr(node->c0);
put_reg(r);
- } else if (kind == AST_LOCAL) {
- if (node->c0) {
- u32 r = gen_assign(node, node->c0);
- put_reg(r);
- }
} else if (kind == AST_IF) {
gen_if_else(node);
} else if (kind == AST_WHILE) {
diff --git a/src/compiler2.c b/src/compiler2.c
@@ -88,7 +88,6 @@ enum {
AST_ENUMDEF, // c2=FIELD*
AST_FUNC, // c0=BLOCK
AST_GLOBAL, // c0=EXPR
- AST_LOCAL, // c0=NAME c1=EXPR?
AST_FIELD,
};
@@ -97,7 +96,7 @@ str ast_kind[AST_FIELD + 1] = {
"BLOCK", "EXPR", "CALL", "WHILE", "IF",
"RETURN", "BREAK", "CONTINUE", "IFELSE",
"PROGRAM", "TYPEDEF", "ENUMDEF", "FUNCDEF",
- "GLOBAL", "LOCAL", "FIELD",
+ "GLOBAL", "FIELD",
};
struct AstRec {
@@ -323,8 +322,8 @@ Ast ast_make_const(ast_t kind, u32 x, Type type) {
return ast_make(kind, x, nil, nil, type);
}
-Ast ast_make_name(String name) {
- return ast_make(AST_NAME, 0, name, nil, nil);
+Ast ast_make_name(String name, Symbol sym) {
+ return ast_make(AST_NAME, 0, name, sym, sym->type);
}
// ================================================================
@@ -1127,9 +1126,7 @@ Ast parse_operand() {
if (sym == nil) {
error("undefined identifier '%s'", ctx.ident->text);
}
- node = ast_make_name(ctx.ident);
- node->sym = sym;
- node->type = sym->type;
+ node = ast_make_name(ctx.ident, sym);
} else {
error("invalid expression");
}
@@ -1510,14 +1507,11 @@ Ast parse_local_var() {
ctx.local_stack = ctx.alloc_stack;
}
- Ast node = ast_make_simple(AST_LOCAL, 0);
- node->name = name;
- node->type = type;
- node->sym = sym;
-
+ Ast node = nil;
if (ctx.tok == tASSIGN) {
next();
- node->c0 = parse_expr();
+ node = ast_make_simple(AST_EXPR, 0);
+ node->c0 = ast_make_binop(tASSIGN, ast_make_name(name, sym), parse_expr());
}
require(tSEMI);
@@ -1644,8 +1638,11 @@ Ast parse_block() {
}
// append to block's list of statements
- last->c2 = node;
- last = node;
+ // some of these don't always return a node (like local var)
+ if (node != nil) {
+ last->c2 = node;
+ last = node;
+ }
}
return block;
}