Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pointer data types for sizeof operator #171

Merged
merged 1 commit into from
Dec 15, 2024

Conversation

ChAoSUnItY
Copy link
Collaborator

Currently sizeof is only capable of parsing basic data types like int, char, or user-defined data types like struct, enum etc. But not able to parsing pointer type, thus, in this PR, by introducing a temporary workaround parsing 0 or more asterisk tokens, we now partially support pointer type for sizeof operator (not including function pointer type yet).

Additionally, since the result given by HOST_PTR_SIZE in stage 0 and stage 1/2 are different, so I make another test driver named driver-stage2.sh for testing stage 2 shecc and adding corresponding sizeof test to verify the implementation.

@jserv jserv requested a review from vacantron December 6, 2024 12:47
Makefile Outdated Show resolved Hide resolved
tests/driver-stage2.sh Outdated Show resolved Hide resolved
tests/driver-stage2.sh Outdated Show resolved Hide resolved
@ChAoSUnItY
Copy link
Collaborator Author

I'm unable to reproduce broken pipe failure on my machine, and the snapshots I checked which are generated by tests/update-snapshots.sh are same as the original one.

@DrXiao
Copy link
Collaborator

DrXiao commented Dec 6, 2024

Because this pull request involves size of pointers, I would like to ask a question.

In the header file (def.h), we can notice that there are two macros called PTR_SIZE (Line 47) and HOST_PTR_SIZE (Line 59 ~ 64). Obviously, both of them mean the size of a pointer, but I cannot understand their use and difference all the time, especially the later one (HOST_PTR_SIZE).

Thus, I hope to obtain an explanation about them through this opportunity.

@ChAoSUnItY
Copy link
Collaborator Author

In the header file (def.h), we can notice that there are two macros called PTR_SIZE (Line 47) and HOST_PTR_SIZE (Line 59 ~ 64). Obviously, both of them mean the size of a pointer, but I cannot understand their use and difference all the time, especially the later one (HOST_PTR_SIZE).

The HOST_PTR_SIZE is determined by the running host, while PTR_SIZE is supposed to be 4, which is capable of handling the arch we're targeting to. The only usages of HOST_PTR_SIZE currently are memcpy in src/ssa.c.

Which I later realized value of sizeof(type*) should not be HOST_PTR_SIZE, instead, it should be PTR_SIZE.

@DrXiao
Copy link
Collaborator

DrXiao commented Dec 7, 2024

@ChAoSUnItY , after fetching your latest changes, I try to build shecc and check the snapshots:

$ make distclean && make ARCH=arm && tests/check-snapshots.sh 
...
  SHECC	out/shecc-stage1.elf
  SHECC	out/shecc-stage2.elf
$ make distclean && make ARCH=riscv && tests/check-snapshots.sh 
...
  SHECC	out/shecc-stage1.elf
  SHECC	out/shecc-stage2.elf
Files /dev/fd/63 and /dev/fd/62 differ
Files /dev/fd/63 and /dev/fd/62 differ

It seems that the snapshots differs when shecc uses RISC-V backend. Hope this information helps you.

@ChAoSUnItY ChAoSUnItY force-pushed the feat/sizeof branch 2 times, most recently from 5a9aa0c to f7db9ba Compare December 7, 2024 06:38
@ChAoSUnItY
Copy link
Collaborator Author

ChAoSUnItY commented Dec 7, 2024

Ok, I found the cause, apparently previously shecc's CI won't check snapshot on rv32 but only on arm32:

shecc/Makefile

Lines 55 to 59 in 18e005d

check: $(TESTBINS) tests/driver.sh
tests/driver.sh
check-snapshots: $(OUT)/$(STAGE0) $(SNAPSHOTS) tests/check-snapshots.sh
tests/check-snapshots.sh

host-x86:
runs-on: ubuntu-22.04
strategy:
matrix:
compiler: [gcc-12, clang]
steps:
- name: checkout code
uses: actions/checkout@v4
- name: build artifact
env:
CC: ${{ matrix.compiler }}
run: |
sudo apt-get update -q -y
sudo apt-get install -q -y graphviz jq
sudo apt-get install -q -y qemu-user
sudo apt-get install -q -y build-essential
make clean config
make check-snapshots || exit 1
make distclean config ARCH=arm
make check || exit 1
make distclean config ARCH=riscv
make check || exit 1

So currently the workaround is just not to run check-snapshot on rv32.

Should we later address this issue in another PR? @jserv

@jserv
Copy link
Collaborator

jserv commented Dec 7, 2024

Ok, I found the cause, apparently previously shecc's CI won't check snapshot on rv32 but only on arm32:

That was a fault. Let's fix it in another pull request.

@ChAoSUnItY
Copy link
Collaborator Author

ChAoSUnItY commented Dec 11, 2024

I will wrap up some additional changes introduced besides sizeof operator implementation enhancement:

Previously, while using stage 2 compiler to compile division-related test suites in tests/driver.sh, the arm32 suffers bus error, and the rv32 suffers infinite loop.

The former one was caused by improper stack alignment, while the later one was caused by missing edge case equality check, which is INT_MIN. Thus, I have added stack_align function in reg-alloc.c to ensure every stack increment is properly aligned, and I have added a branching instruction for the edge case.

The change of total stack size of shecc itself has increased from 9225 to 9228.

src/arm-codegen.c Outdated Show resolved Hide resolved
- Refactor test driver to make it capable of running different stage
  based on supplied stage number.
- Refactor make rule "check" for checking stage 0 and stage 2.
- Replace constant value 4 with PTR_SIZE to give appropriate
  corresponding target arch's pointer size.
- Add sizeof test.
- Introduce __SIZE_OF_PTR__ macro in lib/c.c.
- Fix arm32 div/mod, this was caused by inproper stack alignment
  previously calculated in reg_alloc.c.
- Fix rv32 div/mod, this was caused by missing approximating value
  equality checking.
@jserv
Copy link
Collaborator

jserv commented Dec 15, 2024

I defer to @vacantron for confirmation.

Copy link
Collaborator

@vacantron vacantron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jserv jserv merged commit 5b47efd into sysprog21:master Dec 15, 2024
4 checks passed
@jserv
Copy link
Collaborator

jserv commented Dec 15, 2024

Thank @ChAoSUnItY for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants