r/ProgrammingLanguages 14d ago

Help How to make a compiler backend?

Hell everyone, i have an question. Im for long trying to make a cool, powerful "kinda" low level language similar to zig and rust, but im struggling to choice llvm as backend, sure i can generate C, but its makes compiler dependent on gcc or clang or other c compiler. LLVM seems hard to me, sure project like QBE exist, but QBE doesnt have C/C++ api like llvm's IRbuilder. So are there other ways? I tried thinking about using GCC infrastructure but GCC has poor api and not very documented api. Maybe just stick to generating C?

19 Upvotes

35 comments sorted by

View all comments

8

u/koflerdavid 14d ago edited 14d ago

For a production grade compiler it's probably best to use LLVM or GCC as a backend. But writing a backend targeting assembly [edit:] is very educational. Best target a simpler architecture like RISC-V instead of x86, unless you are already familiar with x86 assembly.

Targeting C is also fine. There are C compilers for just about any architecture. You can also target Tiny C Compiler, which can be embedded into your compiler.

2

u/naharashu 14d ago

I wanted to embed libtcc into compiler but tcc is targeting C99, i need some of C11 features like _Thread_local

1

u/koflerdavid 14d ago

Ah, too bad. It's not a total roadblock though; you'd simply have to call native APIs then. That's surely more performant and less buggy than implementing your own thread locals.

1

u/naharashu 14d ago

What about libgccjit

1

u/koflerdavid 13d ago

That could be a solid option as well. But I know only that GNU Emacs uses it for JIT compilation.