From: Michael D. Lowis Date: Tue, 8 Mar 2022 21:18:31 +0000 (-0500) Subject: fixed up loads and stores X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=7247081ab312843ad163ee961d3552cec94d83ea;p=proto%2Fobnc.git fixed up loads and stores --- diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index 539942c..6da63e2 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -240,6 +240,19 @@ char* binop_name(SsaNode* node) return name; } +void print_operand(Parser* p, SsaNode* expr, int is_const, SsaValue* value) +{ + if (is_const) + { + print_const(expr->arg_type, value); + } + else + { + printf(" "); + print_ident(p, &(value->var)); + } +} + void print_op(Parser* p, SsaNode* expr) { switch(expr->mode) @@ -261,8 +274,7 @@ void print_op(Parser* p, SsaNode* expr) { printf(" store "); emit_type(expr->arg_type); - printf(" "); - print_ident(p, &(expr->left.var)); + print_operand(p, expr, expr->lconst, &(expr->left)); printf(", "); emit_type(expr->ret_type); printf("* "); @@ -301,40 +313,15 @@ void print_op(Parser* p, SsaNode* expr) printf(" = %s ", binop_name(expr)); emit_type(expr->arg_type); printf(" "); - print_ident(p, &(expr->left.var)); - printf(", "); - print_ident(p, &(expr->right.var)); - printf("\n"); - break; - - case MODE_BINOP_LC: - printf(" "); - print_ident(p, &(expr->dest)); - printf(" = %s ", binop_name(expr)); - emit_type(expr->arg_type); - printf(" "); - print_const(expr->arg_type, &(expr->left)); - printf(", "); - print_ident(p, &(expr->right.var)); - printf("\n"); - break; - - case MODE_BINOP_RC: - printf(" "); - print_ident(p, &(expr->dest)); - printf(" = %s ", binop_name(expr)); - emit_type(expr->arg_type); - printf(" "); - print_ident(p, &(expr->left.var)); + print_operand(p, expr, expr->lconst, &(expr->left)); printf(", "); - print_const(expr->arg_type, &(expr->right)); + print_operand(p, expr, expr->rconst, &(expr->right)); printf("\n"); break; case MODE_VAR: case MODE_CONST: default: - printf("OP ????\n"); assert(!"not implemented"); break; } diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 0bc3180..be90539 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -127,9 +127,11 @@ typedef union { typedef struct SsaNode { struct SsaNode* next; - int code : 27; - int mode : 4; - int loaded : 1; + unsigned int code : 25; + unsigned int mode : 4; + unsigned int loaded : 1; + unsigned int lconst : 1; + unsigned int rconst : 1; Type* ret_type; Type* arg_type; SsaVar dest; @@ -166,9 +168,7 @@ enum { MODE_CONTROL = 2, MODE_UNOP = 3, MODE_BINOP = 4, - MODE_BINOP_LC = 5, - MODE_BINOP_RC = 6, - MODE_MEMORY = 7, + MODE_MEMORY = 5, }; typedef struct Symbol { diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index 23aa3f1..bef2c36 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -191,7 +191,17 @@ SsaNode* ssa_store(Parser* p, SsaNode* dest, SsaNode* value) node->ret_type = dest->ret_type; node->arg_type = dest->ret_type; node->dest = dest->left.var; - node->left.var = value->dest; + + if (value->mode == MODE_CONST) + { + node->lconst = 1; + node->left = value->left; + } + else + { + node->left.var = value->dest; + } + node->dest.symver = phi_add(p, node->dest); ssa_block_add(p->curr_block, node); node->loaded = 1; @@ -281,22 +291,22 @@ static SsaNode* binop(Parser* p, int op, SsaNode* left, SsaNode* right) { left = load(p, left); right = load(p, right); + node = ssa_node(op, MODE_BINOP); if (left->mode == MODE_CONST) { - node = ssa_node(op, MODE_BINOP_LC); + node->lconst = 1; node->left = left->left; node->right.var = right->dest; } else if (right->mode == MODE_CONST) { - node = ssa_node(op, MODE_BINOP_RC); + node->rconst = 1; node->left.var = left->dest; node->right = right->left; } else { - node = ssa_node(op, MODE_BINOP); node->left.var = left->dest; node->right.var = right->dest; } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 67a6d14..8086199 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -73,7 +73,7 @@ var begin # Global variables i = 42; -# b = i; + b = i; # # Arithmetic ops # c = b + 1;