r/Compilers • • 22d ago

What should compile-time code be allowed to do?

/r/LowLevelLanguages/comments/1xqu7ii/what_should_compiletime_code_be_allowed_to_do/
5 Upvotes

9 comments sorted by

7

u/SwedishFindecanor 22d ago edited 22d ago

In many environments, it is important to have builds be reproducible. That means no I/O with the outside world. A build script/description should of course create files. One aspect of that is having builds be idempotent, i.e. the same intended result if you run the build process again. On the extreme end of reproducible builds, timestamps are faked, but that is going too far, and at the expense of provenance tracking -- which is also becoming more important.

I have sometimes had to write scripts to create source code because the main programming language hadn't been up to the task. Most of these have been complex static data structures created from higher-level descriptions. For embedded systems, you'd place static data structures in read-only memory sections so they don't occupy precious RAM. One extreme example of that we encounter here is compiler-compilers, such as lexer generators, parser generators and machine description language compilers. For example, machine descriptions for my compiler are in C++, but separate programs that produce C.

I'd prefer to have everything in the same language as much as possible, but also to retain a view of the build system at a high level. I don't want any source file to create lots of intermediary files that go as input into other source files: that is something that belongs at the build level.

1

u/chbeyondC 22d ago

I agree with you, generated files belong in the build. Fels can't really express that yet, but tables like that you can already make with a normal function through `#run`, so no second language or extra file.

4

u/Physical_Dare8553 22d ago

I'm leaning more and more on the side of everything including io. Obviously letting programmers program can be good and bad

1

u/chbeyondC 22d ago

Yeah, I get that. Maybe allow everything, but opt in per package?

1

u/mamcx 22d ago

I think you already see the answer: Compile time (in the compiler) should only do code, build.rs is not comp time, is build tooling.

It feels the same because the tooling like cargo run the build steps, but that is not in fact part of the actual compiler.

1

u/og_hylyx 22d ago edited 22d ago

Anything. See https://github.com/kvthweatt/IDEKA for example
Ideka lets you use the entire language at comptime and inspect compiler/arch state to write portable code by accessing the `compiler.` namespace

1

u/EggplantExtra4946 22d ago

I think both unrestricted system access (within the bounds of what the OS user has permissions for) and fully restricted access (no I/O, no file system access, no networking, no launching processes, etc..) are desirable in different situations.

It would also suck to need to use 2 different languages just to have one or the other, which is also true for other things, for example memory safety.

Therefore IMO the solution is to have full safety by default and opt-in the unsafe features with a capability system, e.g. the caller function or the importer module must give the permission for that specific feature (having 2 modes, one fully safe and one fully unsafe is stupid).

1

u/flatfinger 21d ago

Sometimes it may be useful to have a compiler process untrusted code which will be executed in a sandbox, without requiring that the compiler itself be in a sandbox.

2

u/EggplantExtra4946 21d ago

Yes? I already acknowledged that safety is desirable and that it should even be the default behavior.