mirror of
https://github.com/Noratrieb/uwucc.git
synced 2026-01-15 00:55:05 +01:00
more codegen
This commit is contained in:
parent
ee0b311261
commit
db219d3d74
7 changed files with 214 additions and 15 deletions
69
notes.md
Normal file
69
notes.md
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
# lowering
|
||||
|
||||
This C program
|
||||
|
||||
```c
|
||||
long main() {
|
||||
long a = 0;
|
||||
int b = 0;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
should lower into the following unoptimized IR
|
||||
|
||||
```llvmir
|
||||
def main() {
|
||||
bb0:
|
||||
%a = alloca, size=8, align=8
|
||||
store %a, 0, size=8, align=8
|
||||
%b = alloca, size=4, align=4
|
||||
store %b, 0, size=4, align=4
|
||||
ret 0
|
||||
}
|
||||
```
|
||||
|
||||
this IR can then be lowered to very sane machine code ignoring stack alignment which also like matters
|
||||
|
||||
```x86asm
|
||||
sub rbp, 8 ; a
|
||||
mov [rbp], 0
|
||||
sub rbp, 4 ; b
|
||||
xor rax, rax
|
||||
ret
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```c
|
||||
int main()
|
||||
{
|
||||
int a = 1 + 3 * 4;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
```llvmir
|
||||
def main() {
|
||||
bb0:
|
||||
%a = alloca, size=4, align=4
|
||||
%1 = mul 3, 4
|
||||
%2 = add 1, %1
|
||||
store %a, %2, size=4, align=4
|
||||
ret 0
|
||||
}
|
||||
```
|
||||
|
||||
```x86asm
|
||||
sub rbp, 4 ; a
|
||||
sub rbp, 4 ; %1
|
||||
mov rbx, 3
|
||||
mul rbx, 4
|
||||
mov dword ptr [rbp], rbx
|
||||
sub rbp, 4 ; %2
|
||||
mov rbx, 1
|
||||
add rbx, dword ptr [rbp + 4]
|
||||
mov dword ptr [rbp + 8], dword ptr [rbp]
|
||||
xor rax, rax
|
||||
ret
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue