简体   繁体   中英

I'm using Antlr4 to create a language which I then want to generate LLVM IR with. Do I need to hand-write LLVM IR in response to my visitor events?

While learning Antlr4, I used Golang as a target language, so a statement in my toy language like:

$myVar = 10
$myVar + 5

Would translate to some Golang code that generates "15" for the result

However, as far as I can see, there isn't an LLVM IR target for ANTLR, so the question is: what are my options?

1) Generate C/C++ and then use it to emit LLVM IR?
2) Try to find a Golang LLVM IR emitter?
3) Keep using the generated Go lexer/parser but hand-write LLVM IR?

I tried to go through the LLVM documentation and watched a few videos on LLVM< but they all seem to generate C/C++ and then communicate with the API that way. Not sure if they do that because that's what they know or if it's because that's the only way.

Thanks in advance for any insights!

While learning Antlr4, I used Golang as a target language, so a statement in my toy language like:

 $myVar = 10 $myVar + 5 

Would translate to some Golang code that generates "15" for the result

That's not accurate. Your grammar is translated into Go code that parses your language. Your own code can then use that generated parser to translate the above into whatever you want.

there isn't an LLVM IR target for ANTLR

Nor would it help you if there were one. All that would do would be to create a parser written in LLVM instead of Go. You'd still have to write the code to translate your language into LLVM yourself (just like you'd have to write your own code into translate your language to Go).


As to whether to use the LLVM-API to generate LLVM or to generate it as strings, either option would work. There are Go bindings for LLVM , but it's also perfectly possible to just write LLVM assembly into an .ll file and then run that through llc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM