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

observe calling convention (for x86-64) #117

Merged
merged 9 commits into from
Jan 12, 2025
Merged

Conversation

pointbazaar
Copy link
Owner

No description provided.

x86-64 has a calling convention of passing arguments
in registers.

Use this convention for compatibility with C code.
So that we can call C code an be called from C code.

References:
  https://www.agner.org/optimize/calling_conventions.pdf
  https://en.wikipedia.org/wiki/X86_calling_conventions#References
If there are no local variables, there is no need
to make space on the stack to accomodate them.

This reduces the size of the emitted code.
In x86-64, function arguments are passed in registers, we cannot
calculate the address here.

Maybe this change should better be done in the derefll code, but the
understanding is lacking about how it even works :(
Copy link

netlify bot commented Dec 29, 2024

Deploy Preview for espl1000 ready!

Name Link
🔨 Latest commit db2777b
🔍 Latest deploy log https://app.netlify.com/sites/espl1000/deploys/6782e0e09ec3460008844229
😎 Deploy Preview https://deploy-preview-117--espl1000.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Add some tests to cover the stackframe size and stackframe indices for
local variables and arguments.

Also fix some cases in which that was wrong.
Also extend rat/ to be able to name partial registers
for correctly emitting load/store (mov) of different width.

So for local variables the compiler now respects the width of them when
load/store.

Also needs to be done for the function arguments.
When creating the stackframe, store the function parameters there. So
they do not have to be saved/restored around function calls.

It's just easier when they have a fixed place, relative to the current
stackframe.
In x86-64 there are some callee-saved registers.
Write some function to determine which registers
the callee must save, depending on which registers it is
touching (inspect struct RAT).

To err on the side of caution, we can always save and restore more
registers than actually needed. It will just make the code bigger.

Stack frame layout:

-----
return address
-----
...
callee saved registers <- rbp
-----
local variables
-----
...
saved args <- rsp
-----
...
...      0x0

The registers are saved as part of TAC_SETUP_STACKFRAME and restored as
part of TAC_RETURN. This is not optimal since TAC_RETURN can appear
multiple times and the code to restore the registers will then be
duplicated. It would be great to have some kind of IR statement to
designate the function epilogue to restore the callers registers and
finally 'ret'. The TAC_RETURN could then just 'jmp' there.

References:
  https://www.agner.org/optimize/calling_conventions.pdf
Add assert_examples.sh

That script loops through all ${filename}.dg files in examples/
and if ${filename}.exitcode exists,
it compiles that example program and compares the exit code with whats
in ${filename}.exitcode.

If the exit code of the compiled example program matches, that test is
PASS, otherwise it is FAIL.

This makes for an easy way to write tests for various compiler features.
Some code was still not aware that the function args are saved into
the stackframe and thus have an address.

So there is no limitation and their address is always available inside
the function.
@pointbazaar pointbazaar merged commit 4193f8f into master Jan 12, 2025
13 checks passed
@pointbazaar pointbazaar deleted the calling-convention branch January 12, 2025 11:26
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.

1 participant