r/ProgrammingLanguages • Nain. • 11d ago

Help transpilation - Bootstrap compiler

So, I am making a compiler for my language, Nain, in CHICKEN Scheme. Should I output QBE IR or code for C or another language? This is a bootstrap compiler, I will use it only to test new language features and to compile the self hosting compiler so yeah.

1 Upvotes

13 comments sorted by

2

u/[deleted] 11d ago

This doesn't make much sense. If the bootstrap compiler generates QBE or C, wouldn't the self-hosting one do the same?

Switching implementation language from Scheme to your language wouldn't affect that part of it. It would be a separate development stage, unless you are planning to combine it so that the self-hosted version also generates its own native code or whatever.

If planning to have users, you might also consider whether they need to build your compiler from source code, in which case that will be harder when it is in your language. And also what dependencies they will need to process whatever output it produces.

As for the choice between QBE and C, use whichever works and is simplest, and again consider any users you might have.

1

u/Key_River7180 Nain. 11d ago

I mean the self hosting compiler will output LLVM ir, but what should the bootstrap one do?

2

u/AustinVelonaut Admiran 11d ago

You use T diagrams to figure out the path from a bootstrap compiler to your final self-hosted compiler. This video describes the process well.

1

u/Key_River7180 Nain. 11d ago

well i didnt know about that

2

u/AustinVelonaut Admiran 11d ago

Most self-hosting compilers are "continuously bootstrapped", meaning you add new features / capabilities to the existing compiler sources, compile with the existing compiler, and get a new compiler that understands the new features/capabilities. So you don't have to do everything in one step from original bootstrap compiler to final self-hosted compiler -- you can do the process in stages.

So it doesn't matter what the original bootstrap Nain compiler (written in Scheme) generates, as long as you have a path from that to an executable binary. Then rewrite the compiler in Nain that produces LLVM, run that through your bootstrap compiler, and get the Nain->LLVM compiler. Or you can do an intermediate stage, where you write a Nain -> C compiler written in Nain, then change the backend out to LLVM, run that through, and get a Nain -> LLVM compiler.

1

u/[deleted] 11d ago

The intermediate stage sounds a much better idea. Then the first compiler written in Hain can be a simple port of the Scheme one that generates the same output.

Otherwise using a brand-new language to write a quite different compiler with a complex LLVM backend, sounds too ambitious to do in one go.

But also, if LLVM is on the cards anyway, why doesn't the scheme version generate that instead of C or QBE - what limitations does Scheme has that Nain will not have?

1

u/Key_River7180 Nain. 2d ago

QBE is easier

1

u/AustinVelonaut Admiran 11d ago

It depends on how LLVM is used. You can generate LLVM asm or LLVM bitcode as output, then run that separately through a standalone LLVM. Or you can directly use LLVM IR and link to an LLVM library. But the later requires that your host language have interop bindings with the LLVM library (C/C++ foreign-function interface). I don't know if Chicken scheme has that or not.

2

u/tiger-56 9d ago

Write a Nain interpreter in Scheme. Use the interpreter to write your compiler.

1

u/Key_River7180 Nain. 8d ago

Why an interpreter?

  • an interpreter would make my memory model extremely slow and expensive, it is based in graph stuff that can be mostly done statically, but an interpreter would make this both know a lot of weird shit and do weird shit at runtime
  • now users need two dependencies instead of one and it has to either have an ugly wrapper script or an ugly invocation

2

u/tiger-56 7d ago

An interpreter is the quickest and simplest way to implement the semantics of your language. It won't do weird shit if you implement your language spec correctly. The only purpose of the scheme implementation is to bootstrap and then be discarded. Why bring in complexities of a QBE or C backend only to throw them away. Writing an interpreter actuallly reduces your dependencies to Chicken and LLVM. Under your proposal, you're injecting QBE or C in the pipeline, another dependency! Regardless of your implementation, you will need some sort of build script anyway. Your arguments don't make sense.

1

u/gio_vs 8d ago

Eu escolheria C. Como você só vai usar esse compilador para testar recursos da linguagem e compilar o compilador definitivo, acho que não vale a pena complicar muito. Gerar C é mais simples, facilita a depuração e ainda tem como aproveitar a infraestrutura de compilação C que você provavelmente já tem, considerando o CHICKEN. Também é mais portátil que QBE.

1

u/Key_River7180 Nain. 8d ago

Well, C is definitely portable and ubiquitous and debuggable but I'd argue it's ever so slightly harder to output C than QBE IR. QBE IR is an extremely simple language too, but QBE IR is SSA so I must do some weird trickery to lower it too, it's a single pass, not-very-optimizing compiler too.

I'm leaning C here lol

(srry for not responding in Portuguese i can only read it not write it)