Skip to content

Commit

Permalink
Fix sizeof operator behavior
Browse files Browse the repository at this point in the history
sizeof with pointer typee should generate target pointer size instead
of compiler's host pointer size.
  • Loading branch information
ChAoSUnItY committed Dec 7, 2024
1 parent fb69ecc commit 2107890
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)

ph1_ir = add_ph1_ir(OP_load_constant);
vd = require_var(parent);
vd->init_val = ptr_cnt ? HOST_PTR_SIZE : type->size;
vd->init_val = ptr_cnt ? PTR_SIZE : type->size;
strcpy(vd->var_name, gen_name());
ph1_ir->dest = vd;
opstack_push(vd);
Expand Down
26 changes: 10 additions & 16 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ set -u

readonly SHECC="$PWD/out/shecc"

if [ "$(getconf LONG_BIT)" == "64" ]; then
readonly HOST_PTR_SIZE=8
else
readonly HOST_PTR_SIZE=4
fi

# try - test shecc with given code
# Usage:
# - try exit_code input_code
Expand Down Expand Up @@ -416,25 +410,25 @@ expr 1 "sizeof(_Bool)";
expr 1 "sizeof(char)";
expr 4 "sizeof(int)";
# sizeof pointers
expr $HOST_PTR_SIZE "sizeof(void*)";
expr $HOST_PTR_SIZE "sizeof(_Bool*)";
expr $HOST_PTR_SIZE "sizeof(char*)";
expr $HOST_PTR_SIZE "sizeof(int*)";
expr 4 "sizeof(void*)";
expr 4 "sizeof(_Bool*)";
expr 4 "sizeof(char*)";
expr 4 "sizeof(int*)";
# sizeof multi-level pointer
expr $HOST_PTR_SIZE "sizeof(void**)";
expr $HOST_PTR_SIZE "sizeof(_Bool**)";
expr $HOST_PTR_SIZE "sizeof(char**)";
expr $HOST_PTR_SIZE "sizeof(int**)";
expr 4 "sizeof(void**)";
expr 4 "sizeof(_Bool**)";
expr 4 "sizeof(char**)";
expr 4 "sizeof(int**)";
# sizeof struct
try_ $HOST_PTR_SIZE << EOF
try_ 4 << EOF
typedef struct {
int a;
int b;
} struct_t;
int main() { return sizeof(struct_t*); }
EOF
# sizeof enum
try_ $HOST_PTR_SIZE << EOF
try_ 4 << EOF
typedef enum {
A,
B
Expand Down

0 comments on commit 2107890

Please sign in to comment.