Skip to content

Commit

Permalink
Change HuCC sign-extend char or zero-extend unsigned char before a fu…
Browse files Browse the repository at this point in the history
…nction returns.
  • Loading branch information
jbrandwood committed Oct 27, 2024
1 parent 93ea212 commit d88e464
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/hucc/hucc-codegen.asm
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ __enter .macro

; **************
; function epilog
; \1 == 0 if no return value

__leave .macro
__return .macro
.if (\1 != 0)
sta <__hucc_ret
.endif
Expand Down
4 changes: 2 additions & 2 deletions src/hucc/code.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ void gen_code (INS *tmp)
nl();
break;

case I_LEAVE:
ot("__leave\t\t");
case I_RETURN:
ot("__return\t\t");
outdec((int)data);
nl();
break;
Expand Down
2 changes: 1 addition & 1 deletion src/hucc/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum ICODE {
/* i-codes for C functions and the C parameter stack */

I_ENTER,
I_LEAVE,
I_RETURN,
I_GETACC,
I_SAVESP,
I_LOADSP,
Expand Down
9 changes: 8 additions & 1 deletion src/hucc/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,14 @@ void newfunc (const char *sname, int ret_ptr_order, int ret_type, int ret_otag,
gtext();
gnlabel(fexitlab);
modstk(nbarg * INTSIZE);
out_ins(I_LEAVE, T_VALUE, ret_type != CVOID || ret_ptr_order != 0); /* generate the return statement */
if (ret_ptr_order == 0) {
if (ret_type == CCHAR)
out_ins(I_EXT_BR, 0, 0);
else
if (ret_type == CUCHAR)
out_ins(I_EXT_UR, 0, 0);
}
out_ins(I_RETURN, T_VALUE, ret_type != CVOID || ret_ptr_order != 0); /* generate the return statement */
flush_ins(); /* David, optimize.c related */

ol(".endp"); /* David, .endp directive support */
Expand Down
2 changes: 1 addition & 1 deletion src/hucc/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ unsigned char icode_flags[] = {
// i-codes for C functions and the C parameter stack

/* I_ENTER */ 0,
/* I_LEAVE */ 0,
/* I_RETURN */ 0,
/* I_GETACC */ 0,
/* I_SAVESP */ 0,
/* I_LOADSP */ 0,
Expand Down
2 changes: 1 addition & 1 deletion test/tests/huc-funcptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main()
if (k(3,4) != 7)
abort();

if (p(3,4) != -1)
if (p(4,3) != 1)
abort();
if ((*p)(4,3) != 1)
abort();
Expand Down

0 comments on commit d88e464

Please sign in to comment.