Skip to content

Commit

Permalink
Implement va block arg without result:
Browse files Browse the repository at this point in the history
  Process NULL res in va_block_arg_builtin for all targets.
  Process null desirable_dest for va_arg in gen.
  Add new test issue361.c.
  • Loading branch information
vnmakarov committed Aug 8, 2023
1 parent e652044 commit 9bd782b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
17 changes: 17 additions & 0 deletions c-tests/new/issue361.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdarg.h>
struct car {
char a;
long d;
};

o (int n, ...) {
struct car c0, c;
va_list args;
va_arg (args, struct car);
int n0;
c = va_arg (args, struct car);
int n2 = (args);
return 0;
}

int main (void) { return 0; }
12 changes: 9 additions & 3 deletions c2mir/c2mir.c
Original file line number Diff line number Diff line change
Expand Up @@ -12809,14 +12809,20 @@ static op_t gen (c2m_ctx_t c2m_ctx, node_t r, MIR_label_t true_label, MIR_label_
op2 = mem_to_address (c2m_ctx, op2, FALSE);
}
if (type->mode == TM_STRUCT || type->mode == TM_UNION) {
assert (desirable_dest != NULL && desirable_dest->mir_op.mode == MIR_OP_MEM);
res = mem_to_address (c2m_ctx, *desirable_dest, TRUE);
if (desirable_dest == NULL) {
res = get_new_temp (c2m_ctx, MIR_T_I64);
MIR_append_insn (ctx, curr_func,
MIR_new_insn (ctx, MIR_MOV, res.mir_op, MIR_new_int_op (ctx, 0)));
} else {
assert (desirable_dest->mir_op.mode == MIR_OP_MEM);
res = mem_to_address (c2m_ctx, *desirable_dest, TRUE);
}
MIR_append_insn (ctx, curr_func,
MIR_new_insn (ctx, MIR_VA_BLOCK_ARG, res.mir_op, op2.mir_op,
MIR_new_int_op (ctx, type_size (c2m_ctx, type)),
MIR_new_int_op (ctx, target_get_blk_type (c2m_ctx, type)
- MIR_T_BLK)));
res = *desirable_dest;
if (desirable_dest != NULL) res = *desirable_dest;
} else {
MIR_append_insn (ctx, curr_func,
MIR_new_insn (ctx, MIR_VA_ARG, op1.mir_op, op2.mir_op,
Expand Down
6 changes: 3 additions & 3 deletions mir-aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase MIR_UNUS
a = *(void **) a;
va->arg_area++;
}
memcpy (res, a, s);
if (res != NULL) memcpy (res, a, s);
#else
void *a;
long size = (s + 7) / 8 * 8;
Expand All @@ -119,7 +119,7 @@ void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase MIR_UNUS
a = va->__stack;
va->__stack = (char *) va->__stack + size;
va->__gr_offs += size;
memcpy (res, a, s);
if (res != NULL) memcpy (res, a, s);
return;
}
if (size > 2 * 8) size = 8;
Expand All @@ -131,7 +131,7 @@ void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase MIR_UNUS
va->__stack = (char *) va->__stack + size;
}
if (s > 2 * 8) a = *(void **) a; /* address */
memcpy (res, a, s);
if (res != NULL) memcpy (res, a, s);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion mir-ppc64.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void *va_arg_builtin (void *p, uint64_t t) {
void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase MIR_UNUSED) {
struct ppc64_va_list *va = p;
void *a = va->arg_area;
memcpy (res, a, s);
if (res != NULL) memcpy (res, a, s);
va->arg_area += (s + sizeof (uint64_t) - 1) / sizeof (uint64_t);
}

Expand Down
2 changes: 1 addition & 1 deletion mir-riscv64.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase) {
a = *(void **) a;
va->arg_area++;
}
memcpy (res, a, s);
if (res != NULL) memcpy (res, a, s);
}

void va_start_interp_builtin (MIR_context_t ctx MIR_UNUSED, void *p, void *a) {
Expand Down
3 changes: 2 additions & 1 deletion mir-s390x.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ void *va_arg_builtin (void *p, uint64_t t) {
}

void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase MIR_UNUSED) {
memcpy (res, *(void **) va_arg_builtin (p, MIR_T_I64), s);
void *a = *(void **) va_arg_builtin (p, MIR_T_I64);
if (res == NULL) memcpy (res, a, s);
}

void va_start_interp_builtin (MIR_context_t ctx MIR_UNUSED, void *p, void *a) {
Expand Down
10 changes: 5 additions & 5 deletions mir-x86_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase) {
u[1].i = *(uint64_t *) ((char *) va->reg_save_area + va->gp_offset);
va->gp_offset += 8;
}
memcpy (res, &u, s);
if (res != NULL) memcpy (res, &u, s);
return;
case 2:
u[0].d = *(double *) ((char *) va->reg_save_area + va->fp_offset);
Expand All @@ -90,7 +90,7 @@ void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase) {
u[1].d = *(double *) ((char *) va->reg_save_area + va->fp_offset);
va->fp_offset += 16;
}
memcpy (res, &u, s);
if (res != NULL) memcpy (res, &u, s);
return;
case 3:
case 4:
Expand All @@ -104,11 +104,11 @@ void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase) {
}
va->fp_offset += 8;
va->gp_offset += 8;
memcpy (res, &u, s);
if (res != NULL) memcpy (res, &u, s);
return;
default: break;
}
memcpy (res, a, s);
if (res != NULL) memcpy (res, a, s);
va->overflow_arg_area += size / 8;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ void *va_arg_builtin (void *p, uint64_t t) {
void va_block_arg_builtin (void *res, void *p, size_t s, uint64_t ncase) {
struct x86_64_va_list *va = p;
void *a = s <= 8 ? va->arg_area : *(void **) va->arg_area; /* pass by pointer */
memcpy (res, a, s);
if (res != NULL) memcpy (res, a, s);
va->arg_area++;
}

Expand Down

0 comments on commit 9bd782b

Please sign in to comment.