commit 97cfc1bcbb9bb4e73d23211e7983e99490da6164
parent 4f167dd8b1209571c1fb7cc06cb9c4eee52d5d9e
Author: Eyal Sawady <ecs@d2evs.net>
Date: Wed, 4 Aug 2021 10:16:53 +0000
gen: implement += et al
Diffstat:
3 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -302,7 +302,6 @@ gen_expr_assign(struct gen_context *ctx, const struct expression *expr)
struct expression *value = expr->assign.value;
assert(object->type == EXPR_ACCESS || expr->assign.indirect); // Invariant
assert(object->type != EXPR_SLICE); // TODO
- assert(expr->assign.op == BIN_LEQUAL); // TODO
struct gen_value obj;
if (expr->assign.indirect) {
@@ -311,7 +310,20 @@ gen_expr_assign(struct gen_context *ctx, const struct expression *expr)
} else {
obj = gen_expr_access_addr(ctx, object);
}
- gen_store(ctx, obj, gen_expr(ctx, value));
+ if (expr->assign.op == BIN_LEQUAL) {
+ gen_store(ctx, obj, gen_expr(ctx, value));
+ } else if (expr->assign.op == BIN_LAND || expr->assign.op == BIN_LOR) {
+ assert(0); // TODO
+ } else {
+ struct gen_value lvalue = gen_load(ctx, obj);
+ struct gen_value rvalue = gen_expr(ctx, value);
+ struct qbe_value qlval = mkqval(ctx, &lvalue);
+ struct qbe_value qrval = mkqval(ctx, &rvalue);
+ enum qbe_instr instr = binarithm_for_op(ctx, expr->assign.op,
+ lvalue.type);
+ pushi(ctx->current, &qlval, instr, &qlval, &qrval, NULL);
+ gen_store(ctx, obj, lvalue);
+ }
return gv_void;
}
diff --git a/tests/905-assign.ha b/tests/905-assign.ha
@@ -0,0 +1,9 @@
+export fn main() int = {
+ let x = 0;
+ x += 1;
+ assert(x == 1);
+ let y = &x;
+ *y += 1;
+ assert(x == 2);
+ return 0;
+};
diff --git a/tests/configure b/tests/configure
@@ -8,7 +8,8 @@ tests() {
901-primitives \
902-arithm \
903-postfix \
- 904-copy
+ 904-copy \
+ 905-assign
do
cat <<EOF
tests/$t: harec tests/$t.ha tests/rt.o