From 4435db03297a4f45078b590f408857935de9bce0 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 14 May 2023 12:39:11 +0900 Subject: [PATCH] [qfcc] Support pointer arithmetic on arrays That is, `array + offset`. This actually works around the bug highlighted by arraylife.r (because the array is explicitly used), but is not a proper solution, so that test still fails of course. However, with this, it's no longer necessary to use `&array[index]` instead of `array + index`. --- tools/qfcc/source/expr_binary.c | 8 ++++++++ tools/qfcc/source/statements.c | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/tools/qfcc/source/expr_binary.c b/tools/qfcc/source/expr_binary.c index 8396d4149..c55182667 100644 --- a/tools/qfcc/source/expr_binary.c +++ b/tools/qfcc/source/expr_binary.c @@ -1119,6 +1119,14 @@ binary_expr (int op, expr_t *e1, expr_t *e2) t2 = &type_float; e2 = cast_expr (t2, e2); } + if (is_array (t1) && (is_ptr (t2) || is_integral (t2))) { + t1 = pointer_type (t1->t.array.type); + e1 = cast_expr (t1, e1); + } + if (is_array (t2) && (is_ptr (t1) || is_integral (t1))) { + t2 = pointer_type (t2->t.array.type); + e2 = cast_expr (t2, e2); + } et1 = low_level_type (t1); et2 = low_level_type (t2); diff --git a/tools/qfcc/source/statements.c b/tools/qfcc/source/statements.c index 833545c57..018803d94 100644 --- a/tools/qfcc/source/statements.c +++ b/tools/qfcc/source/statements.c @@ -677,6 +677,11 @@ expr_address (sblock_t *sblock, expr_t *e, operand_t **op) lvalue->e.alias.expr, expr_int (offset)); offset = 0; + } else if (offset && is_constant (offset)) { + int o = expr_int (offset); + if (o < 32768 && o >= -32768) { + offset = expr_file_line (new_short_expr (o), offset); + } } s = new_statement (st_address, "lea", e);