>From f97535755751db471232360f403d6aed185b9e9d Mon Sep 17 00:00:00 2001 From: Mart Gerrits Date: Sun, 15 Apr 2018 19:50:17 +0200 Subject: [PATCH 1/2] x86-64: expand bool/char/short return values to int --- x86_64-gen.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/x86_64-gen.c b/x86_64-gen.c index e33a38a..199a74f 100644 --- a/x86_64-gen.c +++ b/x86_64-gen.c @@ -1237,7 +1237,7 @@ void gfunc_call(int nb_args) { X86_64_Mode mode; CType type; - int size, align, r, args_size, stack_adjust, i, reg_count; + int size, align, r, args_size, stack_adjust, i, reg_count, bt; int nb_reg_args = 0; int nb_sse_args = 0; int sse_reg, gen_reg; @@ -1420,6 +1420,20 @@ void gfunc_call(int nb_args) gcall_or_jmp(0); if (args_size) gadd_sp(args_size); + + /* other compilers don't clear the upper bits when returning bool/char/short */ + bt = vtop->type.ref->type.t & (VT_BTYPE | VT_UNSIGNED); + if (bt == (VT_BYTE | VT_UNSIGNED)) + o(0xc0b60f); /* movzbl %al, %eax */ + else if(bt == VT_BOOL) + o(0xc0b60f); /* movzbl %al, %eax */ + else if (bt == VT_BYTE) + o(0xc0be0f); /* movsbl %al, %eax */ + else if (bt == VT_SHORT) + o(0x98); /* cwtl */ + else if (bt == (VT_SHORT | VT_UNSIGNED)) + o(0xc0b70f); /* movzbl %al, %eax */ + vtop--; } -- 2.16.3