1
votes

I have written my lexer and parser in flex and bison. My project is based on C++ and I would love to stick to it. Currently my Interpreter is written in C++ but I want to achieve faster execution time by converting to bytecode (some form of a VM-level bytecode) when my interpreter works. I know this can be achieved through LLVM. I had problems using it from a x64 OS and developing on a Visual Studio 2012 (32-bit). Some of which can be found @ LLVM linker errors on VS. The other tool I came across is ANTLR and if I understand correctly then the latest release does not easily integrate into C++ yet. Many references were found for the same but a quick one can be @ ANTLR integration with C++ issue. Also I do not want to dispose off my lexer and parser written in flex and bison. What are my options if I want to generate bytecode from my AST?

EDIT: My aim is to generate bytecode from my AST (for the target architecture) so the code can be executed at a Virtual Machine level. Currently I have an Interpretor which interpretes (executes the AST) based on C++ library and generates bytecode. I want to generate Bytecode straight from my AST and execute the AST in its bytecode.

Would be appreciated.

1
@jasal Thanks for the feedback. I have updated my question to indicate what I am looking to achieve.Segmented

1 Answers

1
votes

Generating native bytecode directly from your AST is not possible (well actually it is, but that would be extremely difficult). You need some kind of intermediary step like emitting LLVM bytecode or code in some programming language of your choice. Please note that LLVM bytecode is not the same as native target machine bytecode. The LLVM bytecode has to be compiled to native binaries for target machines which is done by the respective frontend. So you could as well just generate C++ code from your AST using a handwritten code emitter which traverses your syntax tree. Then you use a C++ compiler for the target platform to compile it to the desired native binary.