From 5f4daefd7554f04fd7cd3e416609ed021415a2f7 Mon Sep 17 00:00:00 2001 From: lemon Date: Sun, 4 Jun 2023 15:10:48 +0200 Subject: correctly handle function call returning narrow int --- parse.c | 15 ++++++++------- test2.c | 8 ++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/parse.c b/parse.c index 1081fca..d3167ce 100644 --- a/parse.c +++ b/parse.c @@ -1139,12 +1139,10 @@ narrow(struct function *fn, enum irclass to, enum typetag tt, union ref ref) assert(to == KF4 && tt == TYDOUBLE); ins.op = Ocvtf8f4; } else { - switch (targ_primsizes[tt]) { - case 1: ins.op = issignedt(tt) ? Oexts1 : Oextu1; break; - case 2: ins.op = issignedt(tt) ? Oexts2 : Oextu2; break; - case 4: ins.op = issignedt(tt) ? Oexts4 : Oextu4; break; - default: assert(0); - } + static const enum op ext[5][2] = { + [1] = {Oextu1, Oexts1}, [2] = {Oextu2, Oexts2}, [4] = {Oextu4, Oexts4} + }; + ins.op = ext[targ_primsizes[tt]][issignedt(tt)]; } ins.l = ref; return addinstr(fn, ins); @@ -1564,7 +1562,10 @@ compileexpr(struct function *fn, const struct expr *ex, bool discard) if (discard) return NOREF; return narrow(fn, cls, ex->ty.t, q); case ECALL: - return compilecall(fn, ex); + r = compilecall(fn, ex); + if (isint(ex->ty)) + return narrow(fn, cls, ex->ty.t, r); + return r; case ECOND: if (ex->ty.t == TYVOID) { struct block *tr, *fl, *end; diff --git a/test2.c b/test2.c index 13ed47c..7d39164 100644 --- a/test2.c +++ b/test2.c @@ -13,3 +13,11 @@ v2d add(v2d a, v2d b) addp(&a, &b); return a; } + +short s(int a, int b) { + return a + b; +} + +int i() { + return s(1,2); +} -- cgit v1.2.3