commit 4d8d2185f886a1f5d3829e0e441a837d5fa4b8fe
parent 08eb7986edfd27887906a7e75ee81cee3ca75c39
Author: Drew DeVault <sir@cmpwn.com>
Date: Wed, 4 Aug 2021 09:20:21 +0200
gen: implement index expressions
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
1 file changed, 23 insertions(+), 1 deletion(-)
diff --git a/src/gen.c b/src/gen.c
@@ -197,6 +197,27 @@ gen_access_field(struct gen_context *ctx, const struct expression *expr)
}
static struct gen_value
+gen_access_index(struct gen_context *ctx, const struct expression *expr)
+{
+ struct gen_value glval = gen_expr(ctx, expr->access.array);
+ glval = gen_autoderef(ctx, glval);
+ struct qbe_value qlval = mkqval(ctx, &glval);
+ struct qbe_value qival = mkqtmp(ctx, ctx->arch.ptr, ".%d");
+
+ struct gen_value index = gen_expr(ctx, expr->access.index);
+ struct qbe_value qindex = mkqval(ctx, &index);
+ struct qbe_value itemsz = constl(expr->result->size);
+ pushi(ctx->current, &qival, Q_MUL, &qindex, &itemsz, NULL);
+ pushi(ctx->current, &qival, Q_ADD, &qlval, &qival, NULL);
+
+ return (struct gen_value){
+ .kind = GV_TEMP,
+ .type = expr->result,
+ .name = qival.name,
+ };
+}
+
+static struct gen_value
gen_expr_access_addr(struct gen_context *ctx, const struct expression *expr)
{
struct gen_value addr;
@@ -205,7 +226,8 @@ gen_expr_access_addr(struct gen_context *ctx, const struct expression *expr)
addr = gen_access_ident(ctx, expr);
break;
case ACCESS_INDEX:
- assert(0); // TODO
+ addr = gen_access_index(ctx, expr);
+ break;
case ACCESS_FIELD:
addr = gen_access_field(ctx, expr);
break;