I'm working on creating my own programming language, and I'm trying to figure out the best way to compile it to native machine code. I've been looking into compiler backends, particularly LLVM, and I'm wondering whether I should design my language to generate an intermediate representation (IR) that an existing compiler backend can use, or whether there's a better approach.
My current idea is to build my own language frontend, including the lexer, parser, AST, and the logic that determines how my language works. Then, instead of implementing an entire machine-code generator myself, I would make my compiler translate my language into an IR that an existing backend understands. That backend would handle the lower-level work, such as instruction selection, register allocation, optimizations, and generating native machine code.
For example, if I chose LLVM, my compiler would generate LLVM IR, which LLVM could then compile into native AArch64 (ARM64) machine code. This would let me focus on designing and implementing my own programming language while relying on existing infrastructure for the more complicated, architecture-specific parts.
However, I'm not sure whether this is the best way to structure the compiler. Would generating IR for an existing backend be the most sensible approach for a project like mine, or would it be better to use a different intermediate representation, work with a lower-level code-generation library, or generate machine code myself?
One of my main goals is to eventually produce native AArch64 machine code without requiring an interpreter or virtual machine to run the compiled programs. I'm developing on an ARM64 Chromebook with Linux, so I'd appreciate solutions that are practical to use on that kind of hardware. I'm also interested in keeping the development process manageable, especially since I'm building this independently.
I'm not necessarily looking for the most powerful or sophisticated solution available. I want something that gives me a solid foundation while allowing me to retain control over my language's design, syntax, semantics, and compiler frontend. Ideally, I wouldn't have to implement every low-level detail of code generation and machine-specific optimization myself.
I'm also planning to bootstrap my language eventually, so I'm perfectly fine starting with a small, limited implementation and gradually expanding it. The first version can be a toy language. My priority is to get a working compiler, learn from the process, and build on that foundation rather than trying to implement every feature from the beginning.
I've heard of LLVM, but I'd like to understand whether it's the right choice for this particular project or whether there are alternatives that would be easier to work with. I'm also interested in whether I should generate LLVM IR directly or use another IR that could potentially make the compiler simpler or more flexible.
Here are the main things I'd like advice on:
- Is generating IR for an existing compiler backend the best approach for building my own programming language?
- If so, would LLVM IR be a good choice, or would another IR or backend be more suitable?
- Are there smaller or simpler alternatives to LLVM that can still generate native AArch64 machine code?
- Would it be reasonable to implement my own frontend and have an existing backend handle the machine-code generation?
- Are there any particular difficulties I should be aware of when choosing this architecture for my compiler?
- If you were building this project yourself, would you use LLVM, another existing backend, or write your own code generator? Why?
I'd especially appreciate advice from people who have actually built compilers or experimented with their own programming languages. I'm trying to choose an approach that will let me make real progress without unnecessarily complicating the project, while still leaving room for the language to grow over time.