Skip to content
Snippets Groups Projects
Commit ebede4bb authored by Aron Strandberg's avatar Aron Strandberg
Browse files

added last contribution, LLVM

parent b552a017
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
- [Void (none)](#void-none) - [Void (none)](#void-none)
- [Your borrrow checker](#your-borrrow-checker) - [Your borrrow checker](#your-borrrow-checker)
- [Your LLVM backend](#your-llvm-backend) - [Your LLVM backend](#your-llvm-backend)
- [Comparison](#comparison)
- [Overal course goals and learning outcomes.](#overal-course-goals-and-learning-outcomes) - [Overal course goals and learning outcomes.](#overal-course-goals-and-learning-outcomes)
## Your repo ## Your repo
...@@ -231,7 +232,7 @@ fn main() { ...@@ -231,7 +232,7 @@ fn main() {
test(); test();
} }
``` ```
The function call moves the current state, $σ$ to the new derived state $σ'$ in the function, test()'s context. A function can evaluate void (none), i32 and bool. The function call moves the current state, σ to the new derived state σ' in the function, test()'s context. A function can evaluate void (none), i32 and bool.
### Command sequence ### Command sequence
```math ```math
...@@ -242,7 +243,7 @@ The function call moves the current state, $σ$ to the new derived state $σ'$ i ...@@ -242,7 +243,7 @@ The function call moves the current state, $σ$ to the new derived state $σ'$ i
let x : i32 = 2; let x : i32 = 2;
test(); test();
``` ```
A sequence is a composition of commands where the first command is first executed then the second command. Otherwise the intermediate step, $σ'$ would be lost. A sequence is a composition of commands where the first command is first executed then the second command. Otherwise the intermediate step, σ' would be lost.
### Operands for arithmetic expressions ### Operands for arithmetic expressions
```math ```math
...@@ -322,7 +323,7 @@ let a : i32 = 1; ...@@ -322,7 +323,7 @@ let a : i32 = 1;
let b : bool = true; let b : bool = true;
let c : i32 = test(); // c will be assigned whaterver test() returns let c : i32 = test(); // c will be assigned whaterver test() returns
``` ```
For let commands, if the variable is already declared the state $σ$ will be changed to the one of the second command, state $σ'$, the variable will be lost. Otherwise the variable will be assigned to the state, $σ$. For let commands, if the variable is already declared the state σ will be changed to the one of the second command, state σ', the variable will be lost. Otherwise the variable will be assigned to the state, σ.
### Assignment ### Assignment
```math ```math
...@@ -570,12 +571,97 @@ The end goal of the borrow checker is to prevent data-racing. Data-racing is whe ...@@ -570,12 +571,97 @@ The end goal of the borrow checker is to prevent data-racing. Data-racing is whe
## Your LLVM backend ## Your LLVM backend
- Let your backend produces LLVM-IR for your example program. - Let your backend produces LLVM-IR for your example program.
```llvm
; ModuleID = 'main'
source_filename = "main"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
define i32 @test_i32(i32, i32) {
entry:
%a = alloca i32
%c = alloca i32
%b = alloca i32
store i32 %0, i32* %b
store i32 %1, i32* %c
store i32 15, i32* %a
store i32 1, i32* %a
%a1 = load i32, i32* %a
%b2 = load i32, i32* %b
%Sum = add i32 %a1, %b2
ret i32 %Sum
}
define i1 @test_bool(i1) {
entry:
%b = alloca i32
store i1 %0, i32* %b
%b1 = load i32, i32* %b
br i32 %b1, label %if, label %cont
if: ; preds = %entry
%a = alloca i32
%test_i32 = call i32 @test_i32(i32 1, i32 999)
store i32 %test_i32, i32* %a
%a2 = load i32, i32* %a
%Lt = icmp slt i32 %a2, 5
br i1 %Lt, label %while, label %cont3
cont: ; preds = %cont3, %entry
%iftmp = phi i32
%b6 = load i32, i32* %b
ret i32 %b6
while: ; preds = %while, %if
%a4 = load i32, i32* %a
%Sum = add i32 %a4, 1
store i32 %Sum, i32* %a
br i1 %Lt, label %while, label %cont3
cont3: ; preds = %while, %if
%whiletmp = phi i32
%a5 = load i32, i32* %a
ret i32 %a5
br label %cont
}
define void @main() {
entry:
%test_bool = call i1 @test_bool(i1 true)
}
```
- Describe where and why you introduced allocations and phi nodes. - Allocation are done to store a variable on the stack, such that LLVM later can access the variable in the memory using given pointer. This is done when storing parameters, arguments, variables etc.
- If you have added optimization passes and/or attributed your code for better optimization (using e.g., `noalias`). - Phi nodes are supposed to be used in if and while statements in my implementation. Because of the way I have built the block functionality it gave me a headech to change at this late of a stage- Therefor I will explain phi nodes. They way I interpreted them!
- Phi nodes are used to handle different blocks in e.g. a if block. There are two blocks and because SSA (Static single assignment) a variable cannot be assigned more than once.
- Compare your solution to the requirements (as stated in the README.md). What are your contributions to the implementation. ```rust
if ... {
a.0 = 1;
} else {
a.1 = 2;
}
...
```
Since LLVM uses the variable with highest index, in the example above a.1 = 2. What if the condition were true? Then we want a.0 = 1! This is the problem with SSA in LLVM and therefor phi nodes are used e.g.
```rust
if ... {
phi.if_block
} else {
phi.else_block
}
phi.cont_block
...
```
### Comparison
- No PassManager has been used or other optimization, except some [#inline] somewere and verify.
- LLVM is a good wrapper to the course as I will talk more about but I have been introduced to
- SSA form, it's hiccups and how we bypass them
- The concept of unique I would like to discuss
- LLVM optimization as the example of PassManager, verify, more to e discovered...
- By using the It's a New Kind of Wrapper for Exposing LLVM (safely) I have been introduced to some LLVM-API during implementation.
## Overal course goals and learning outcomes. ## Overal course goals and learning outcomes.
...@@ -591,7 +677,7 @@ The end goal of the borrow checker is to prevent data-racing. Data-racing is whe ...@@ -591,7 +677,7 @@ The end goal of the borrow checker is to prevent data-racing. Data-racing is whe
- At this point I excpect the interpreter not to fail. - At this point I excpect the interpreter not to fail.
- LLVM - LLVM
- This is the last step of my compiler, now we are generating actual machine code. It has linked this course with microcomputing which I feel has been rewarding. To think more of e.g. how boolean are represented in machine code. To grasp LLVM you could have a seperate course but it wraps up the comiler you have written and produce something useful. - This is the last step of my compiler, now we are generating actual machine code. It has linked this course with microcomputing which I feel has been rewarding. To think more of e.g. how boolean are represented in machine code and the problems surrounding SSA form. To grasp LLVM you could have had a seperate course but it wraps up the compiler you have written and produce during the course to something useful.
Comment on additional things that you have experienced and learned throughout the course. Comment on additional things that you have experienced and learned throughout the course.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment