llir / llvm

Library for interacting with LLVM IR in pure Go.
https://llir.github.io/document/
BSD Zero Clause License
1.19k stars 78 forks source link

Help: String concatenation? #180

Closed Nv7-GitHub closed 3 years ago

Nv7-GitHub commented 3 years ago

So far I've been learning LLVM by looking at the output of C code. For functions like printf, puts, and strlen, you just define the function signature and then call it. However, how do I use strcat? When looking at the C output, it generates things like:

; Function Attrs: nounwind
declare i8* @__strcat_chk(i8*, i8*, i64) #2

; Function Attrs: nounwind readnone speculatable willreturn
declare i64 @llvm.objectsize.i64.p0i8(i8*, i1 immarg, i1 immarg, i1 immarg) #3

And to use it it calls:

%21 = call i64 @llvm.objectsize.i64.p0i8(i8* %20, i1 false, i1 true, i1 false)
%22 = call i8* @__strcat_chk(i8* %18, i8* %19, i64 %21) #4

How does this work? How would I generate code to concatenate two strings with this library?

Nv7-GitHub commented 3 years ago

Never mind, it seems like you can just call strcat too (https://github.com/llir/llvm/issues/22)

dannypsnl commented 3 years ago

I think those functions are handled by cr0 or something like that, the linker will load them when needed.