I am advancing on a toy compiler (first time) and trying to figure out how to distribute / build an LLVM structure type. The Kaleidoscope tutorial does not include or mention this, and I don’t know what I am looking for in the LLVM source / tests to find possible examples.
So, I just wrote an example in C ++, dropped IR with clang, trying to figure out what it produces, but honestly, I am not following all this. Things that are obvious to me are function definitions / declarations and some function calls and a call memset, so I get chunks, but that's not all for me. (PS My interpretation of the allocationa instruction documents is that everything created from this is freed upon return, so I can’t use this right, essentially, only for local variables?)
What I've done:
alloc.cpp
struct Alloc {
int age;
};
Alloc *allocPtr() {
return new Alloc();
}
int main() {
Alloc *ptr = allocPtr();
return 0;
}
Then run clang -S -emit-llvm alloc.cppto createalloc.ll
; ModuleID = 'alloc.cpp'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.11.0"
%struct.Alloc = type { i32 }
; Function Attrs: ssp uwtable
define %struct.Alloc* @_Z8allocPtrv()
entry:
%call = call noalias i8* @_Znwm(i64 4)
%0 = bitcast i8* %call to %struct.Alloc*
%1 = bitcast %struct.Alloc* %0 to i8*
call void @llvm.memset.p0i8.i64(i8* %1, i8 0, i64 4, i32 4, i1 false)
ret %struct.Alloc* %0
}
; Function Attrs: nobuiltin
declare noalias i8* @_Znwm(i64)
; Function Attrs: nounwind
declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1)
; Function Attrs: ssp uwtable
define i32 @main()
entry:
%retval = alloca i32, align 4
%ptr = alloca %struct.Alloc*, align 8
store i32 0, i32* %retval
%call = call %struct.Alloc* @_Z8allocPtrv()
store %struct.Alloc* %call, %struct.Alloc** %ptr, align 8
ret i32 0
}
attributes
attributes
attributes
attributes
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"PIC Level", i32 2}
!1 = !{!"clang version 3.7.0 (tags/RELEASE_370/final)"}
- , IR ++? , / LLVM StructType, , ( , ).
, , , , , IR ...