cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit 1a38a5fc4844a0de8729be694a62ba0afce3ff52
parent 7d5d63cd2f7c49adaf2c97ccc0ba39063cf084a0
Author: Michael Forney <mforney@mforney.org>
Date:   Thu, 11 Jul 2019 13:56:26 -0700

expr: Fix cast of same type

Diffstat:
Mexpr.c | 40+++++++++++++++++++++-------------------
Atest/cast-same-type.c | 4++++
Atest/cast-same-type.qbe | 12++++++++++++
3 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/expr.c b/expr.c @@ -866,38 +866,40 @@ unaryexpr(struct scope *s) static struct expr * castexpr(struct scope *s) { - struct type *t; + struct type *t, *prev = NULL; enum typequal tq; - struct expr *r, *e, **end; + struct expr *result, *base = NULL, **end = &result; - end = &r; while (consume(TLPAREN)) { tq = QUALNONE; t = typename(s, &tq); if (!t) { - e = expr(s); + base = expr(s); expect(TRPAREN, "after expression to match '('"); - *end = postfixexpr(s, e); - return r; + base = postfixexpr(s, base); + break; } expect(TRPAREN, "after type name"); if (tok.kind == TLBRACE) { - e = mkexpr(EXPRCOMPOUND, t); - e->qual = tq; - e->lvalue = true; - e->compound.init = parseinit(s, t); - e = decay(e); - *end = postfixexpr(s, e); - return r; + base = mkexpr(EXPRCOMPOUND, t); + base->qual = tq; + base->lvalue = true; + base->compound.init = parseinit(s, t); + base = postfixexpr(s, decay(base)); + break; } - e = mkexpr(EXPRCAST, t); - // XXX check types 6.5.4 - *end = e; - end = &e->base; + if (prev && !typecompatible(prev, t)) { + *end = mkexpr(EXPRCAST, prev); + // XXX check types 6.5.4 + end = &(*end)->base; + } + prev = t; } - *end = unaryexpr(s); + if (!base) + base = unaryexpr(s); + *end = prev ? exprconvert(base, prev) : base; - return r; + return result; } static int diff --git a/test/cast-same-type.c b/test/cast-same-type.c @@ -0,0 +1,4 @@ +struct S {int x;} s; +int main(void) { + return ((struct S)s).x; +} diff --git a/test/cast-same-type.qbe b/test/cast-same-type.qbe @@ -0,0 +1,12 @@ +export +function w $main() { +@start.1 +@body.2 + %.1 =l copy $s + %.2 =l mul 0, 1 + %.3 =l add %.1, %.2 + %.4 =l copy %.3 + %.5 =w loadsw %.4 + ret %.5 +} +export data $s = align 4 { z 4 }