Lorenzo Laneve via llvm-dev
2016-Mar-19 12:31 UTC
[llvm-dev] Need help with code generation
I'd like to make my compiler independent, just like Clang. Doesn't Clang call llc and then system's ld by itself? I don't want my compiler to depend by any other program. I guess there will be a class in the llvm library that generates the object files based on the system's triple and data layout, and then call the system's ld?> On Mar 19, 2016, at 11:48 AM, Bruce Hoult <bruce at hoult.org> wrote: > > If you've created a .bc or a .ll file then the simplest thing is to just give it to clang exactly the same as you would for a .c file. Clang will just Do The Right Thing with it. > > If you don't want to link, then pass flags such as -c to clang as usual. > > e.g. > > ---- hello.ll ---- > declare i32 @puts(i8*) > @str = constant [12 x i8] c"Hello World\00" > > define i32 @main() { > %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @str, i64 0, i64 0)) > ret i32 0 > } > ---------------- > > $ clang hello.ll -o hello && ./hello > warning: overriding the module target triple with x86_64-apple-macosx10.10.0 > 1 warning generated. > Hello World > > >> On Sat, Mar 19, 2016 at 3:03 AM, Lorenzo Laneve via llvm-dev <llvm-dev at lists.llvm.org> wrote: >> I wrote my compiler and now it generates LLVM IR modules. Now i’d like to go ahead and make object file and then executable, just like clang does. >> >> What should I have to use to create the object files? and then how do I call the ld? (not llvm-ld, I want my compiler to work like Clang and I read that Clang doesn’t use llvm-ld). >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160319/7aff0d78/attachment.html>
Hi Lorenzo, Clang doesn't call llc; LLVM is compiled into Clang. Clang does call the system linker though. Making your compiler generate *object* code is very simple. Making it fixup that object code and execute it in memory (JIT style) is also simple. Linking it properly and creating a fixed up ELF file is less simple. For that, you need to compile to object (using addPassesToEmitFile() - see llc.cpp) then invoke a linker. Getting that command line right can be quite difficult. Rafael, This would be a good usecase for LLD as a library. I heard that this is is an explicit non-goal, which really surprised me. Is that indeed the case? Cheers, James On Sat, 19 Mar 2016 at 13:32 Lorenzo Laneve via llvm-dev < llvm-dev at lists.llvm.org> wrote:> I'd like to make my compiler independent, just like Clang. Doesn't Clang > call llc and then system's ld by itself? I don't want my compiler to depend > by any other program. > I guess there will be a class in the llvm library that generates the > object files based on the system's triple and data layout, and then call > the system's ld? > > On Mar 19, 2016, at 11:48 AM, Bruce Hoult <bruce at hoult.org> wrote: > > If you've created a .bc or a .ll file then the simplest thing is to just > give it to clang exactly the same as you would for a .c file. Clang will > just Do The Right Thing with it. > > If you don't want to link, then pass flags such as -c to clang as usual. > > e.g. > > ---- hello.ll ---- > declare i32 @puts(i8*) > @str = constant [12 x i8] c"Hello World\00" > > define i32 @main() { > %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @str, i64 0, > i64 0)) > ret i32 0 > } > ---------------- > > $ clang hello.ll -o hello && ./hello > warning: overriding the module target triple with > x86_64-apple-macosx10.10.0 > 1 warning generated. > Hello World > > > On Sat, Mar 19, 2016 at 3:03 AM, Lorenzo Laneve via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> I wrote my compiler and now it generates LLVM IR modules. Now i’d like to >> go ahead and make object file and then executable, just like clang does. >> >> What should I have to use to create the object files? and then how do I >> call the ld? (not llvm-ld, I want my compiler to work like Clang and I read >> that Clang doesn’t use llvm-ld). >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160319/2005bd52/attachment.html>
mats petersson via llvm-dev
2016-Mar-19 20:58 UTC
[llvm-dev] Need help with code generation
If you plan on calling C runtime library functions, you probably want to do what I did: Cheat, and make a libruntime.a (with C functions to do stuff your compiler can't do natively) and then link that using clang or gcc. https://github.com/Leporacanthicus/lacsap/blob/master/binary.cpp#L124 At some point, I plan to replace my runtime library with native Pascal code, at which point I will be able to generate the ELF binary straight from my compiler without the runtime library linking in the C runtime library, but that's not happening anytime real soon. Getting the compiler to compile v5 of Wirth's original Pascal compiler is higher on the list... :) -- Mats On 19 March 2016 at 20:51, James Molloy via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi Lorenzo, > > Clang doesn't call llc; LLVM is compiled into Clang. Clang does call the > system linker though. > > Making your compiler generate *object* code is very simple. Making it > fixup that object code and execute it in memory (JIT style) is also simple. > Linking it properly and creating a fixed up ELF file is less simple. For > that, you need to compile to object (using addPassesToEmitFile() - see > llc.cpp) then invoke a linker. Getting that command line right can be quite > difficult. > > Rafael, This would be a good usecase for LLD as a library. I heard that > this is is an explicit non-goal, which really surprised me. Is that indeed > the case? > > Cheers, > > James > > On Sat, 19 Mar 2016 at 13:32 Lorenzo Laneve via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> I'd like to make my compiler independent, just like Clang. Doesn't Clang >> call llc and then system's ld by itself? I don't want my compiler to depend >> by any other program. >> I guess there will be a class in the llvm library that generates the >> object files based on the system's triple and data layout, and then call >> the system's ld? >> >> On Mar 19, 2016, at 11:48 AM, Bruce Hoult <bruce at hoult.org> wrote: >> >> If you've created a .bc or a .ll file then the simplest thing is to just >> give it to clang exactly the same as you would for a .c file. Clang will >> just Do The Right Thing with it. >> >> If you don't want to link, then pass flags such as -c to clang as usual. >> >> e.g. >> >> ---- hello.ll ---- >> declare i32 @puts(i8*) >> @str = constant [12 x i8] c"Hello World\00" >> >> define i32 @main() { >> %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @str, i64 0, >> i64 0)) >> ret i32 0 >> } >> ---------------- >> >> $ clang hello.ll -o hello && ./hello >> warning: overriding the module target triple with >> x86_64-apple-macosx10.10.0 >> 1 warning generated. >> Hello World >> >> >> On Sat, Mar 19, 2016 at 3:03 AM, Lorenzo Laneve via llvm-dev < >> llvm-dev at lists.llvm.org> wrote: >> >>> I wrote my compiler and now it generates LLVM IR modules. Now i’d like >>> to go ahead and make object file and then executable, just like clang does. >>> >>> What should I have to use to create the object files? and then how do I >>> call the ld? (not llvm-ld, I want my compiler to work like Clang and I read >>> that Clang doesn’t use llvm-ld). >>> _______________________________________________ >>> LLVM Developers mailing list >>> llvm-dev at lists.llvm.org >>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >>> >> >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160319/ba83690a/attachment.html>
On Sat, Mar 19, 2016 at 9:51 PM, James Molloy via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi Lorenzo, > > Clang doesn't call llc; LLVM is compiled into Clang. Clang does call the > system linker though. > > Making your compiler generate *object* code is very simple. Making it > fixup that object code and execute it in memory (JIT style) is also simple. > Linking it properly and creating a fixed up ELF file is less simple. For > that, you need to compile to object (using addPassesToEmitFile() - see > llc.cpp) then invoke a linker. Getting that command line right can be quite > difficult. > > Rafael, This would be a good usecase for LLD as a library. I heard that > this is is an explicit non-goal, which really surprised me. Is that indeed > the case? >You can use LLD as a library. https://github.com/llvm-mirror/lld/blob/master/docs/NewLLD.rst#the-elf-linker-as-a-library> Cheers, > > James > > On Sat, 19 Mar 2016 at 13:32 Lorenzo Laneve via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> I'd like to make my compiler independent, just like Clang. Doesn't Clang >> call llc and then system's ld by itself? I don't want my compiler to depend >> by any other program. >> I guess there will be a class in the llvm library that generates the >> object files based on the system's triple and data layout, and then call >> the system's ld? >> >> On Mar 19, 2016, at 11:48 AM, Bruce Hoult <bruce at hoult.org> wrote: >> >> If you've created a .bc or a .ll file then the simplest thing is to just >> give it to clang exactly the same as you would for a .c file. Clang will >> just Do The Right Thing with it. >> >> If you don't want to link, then pass flags such as -c to clang as usual. >> >> e.g. >> >> ---- hello.ll ---- >> declare i32 @puts(i8*) >> @str = constant [12 x i8] c"Hello World\00" >> >> define i32 @main() { >> %1 = call i32 @puts(i8* getelementptr inbounds ([12 x i8]* @str, i64 0, >> i64 0)) >> ret i32 0 >> } >> ---------------- >> >> $ clang hello.ll -o hello && ./hello >> warning: overriding the module target triple with >> x86_64-apple-macosx10.10.0 >> 1 warning generated. >> Hello World >> >> >> On Sat, Mar 19, 2016 at 3:03 AM, Lorenzo Laneve via llvm-dev < >> llvm-dev at lists.llvm.org> wrote: >> >>> I wrote my compiler and now it generates LLVM IR modules. Now i’d like >>> to go ahead and make object file and then executable, just like clang does. >>> >>> What should I have to use to create the object files? and then how do I >>> call the ld? (not llvm-ld, I want my compiler to work like Clang and I read >>> that Clang doesn’t use llvm-ld). >>> _______________________________________________ >>> LLVM Developers mailing list >>> llvm-dev at lists.llvm.org >>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >>> >> >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160321/fdc19888/attachment.html>