Skip to content

Instantly share code, notes, and snippets.

@MrSmith33
Created April 17, 2018 12:10
Show Gist options
  • Save MrSmith33/a175edeb0676304c9a88e0ab4901968d to your computer and use it in GitHub Desktop.
Save MrSmith33/a175edeb0676304c9a88e0ab4901968d to your computer and use it in GitHub Desktop.
Code generated by indexing pointer
// Source
i32 test(i32* array, i32 index) {
return array[index]; // read from array
}
// IR
function i32 $test () {
| @start:0 instr[0..6]
1| %0 = ptr o_param uses [ ptr %3]
2| %1 = i32 o_param uses [ i64 %2]
3| %2 = i64 o_mul i64 %1, i32 4 uses [ ptr %3]
4| %3 = ptr o_add ptr %0, i64 %2 uses [ i32 %4]
5| %4 = i32 o_load ptr %3 uses [ instr.6]
6| return i32 %4
}
// Amd64 code
48 6B C2 04 48 89 CA 48 01 C2 8B 02 C3
0: 48 6b c2 04 imul rax,rdx,0x4
4: 48 89 ca mov rdx,rcx
7: 48 01 c2 add rdx,rax
a: 8b 02 mov eax,DWORD PTR [rdx]
c: c3 ret
test([42, 56].ptr, 1) -> 56
------------------------------
// Source
void test(i32* array, i32 index, i32 value) {
array[index] = value; // write to array
}
// IR
function i32 $test () {
| @start:0 instr[0..7]
1| %0 = ptr o_param uses [ ptr %4]
2| %1 = i32 o_param uses [ i64 %3]
3| %2 = i32 o_param uses [ instr.6]
4| %3 = i64 o_mul i64 %1, i32 4 uses [ ptr %4]
5| %4 = ptr o_add ptr %0, i64 %3 uses [ instr.6]
6| o_store ptr %4, i32 %2
7| return
}
// Amd64 code
48 6B C2 04 48 89 CA 48 01 C2 44 89 02 C3
0: 48 6b c2 04 imul rax,rdx,0x4
4: 48 89 ca mov rdx,rcx
7: 48 01 c2 add rdx,rax
a: 44 89 02 mov DWORD PTR [rdx],r8d
d: c3 ret
test([42, 56].ptr, 1, 20) -> [42, 20]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment