Skip to content

Instantly share code, notes, and snippets.

@nachivpn
Created May 10, 2017 18:26
Show Gist options
  • Save nachivpn/d1c5bcfe721fd6e6e92febb32e56badc to your computer and use it in GitHub Desktop.
Save nachivpn/d1c5bcfe721fd6e6e92febb32e56badc to your computer and use it in GitHub Desktop.
Iplementing Arrays in LLVM
; In JL: int[] a = new int[2];
; In C: int *a = (int*) malloc(2 * sizeof(int));
; In LLVM:
%a = alloca i32*, align 8
; type of %a = i32 **
; %a is the location on stack which (later) holds the address of the array on the heap
%call = call i8* @malloc(i64 8) ; notice the size = 8 i.e, (2 * 4) as int is 4 bytes long
%0 = bitcast i8* %call to i32*
store i32* %0, i32** %a, align 8
; In JL: int[][] b = new int[5][2];
; In C:
; int **b = (int**) malloc(5 * sizeof(int*));
; for (int i = 0; i < 5; i++) {
; b[i] = malloc(2 * sizeof(int));
; }
; In LLVM:
%b = alloca i32**, align 8
%call1 = call i8* @malloc(i64 40) ; notice the size = 40 i.e, (5 * 8) because an int* is 8 bytes long
%1 = bitcast i8* %call1 to i32**
store i32** %1, i32*** %b, align 8
; LLVM code for the for loop (not included here)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment