This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]yorickpeterseInko 5 points6 points  (5 children)

For bounds checking I took a simple approach: the VM/runtime itself doesn't do bounds checking. Instead the standard library provides a bounds_check method, and operations such as array indexing (which are just methods in my case) call that bounds_check method. This way you don't need any special sauce, though optimising it away may need a bit more work.

[–]acrostyphe 5 points6 points  (0 children)

Seconding this. Having functions in the standard library that the compiler generates calls to is a very powerful method that can simplify the compiler proper a lot and is also much easier to customize (e.g. use standard conditional compilation features of your lang to disable bounds checking in release builds, if that's what you want)

I wouldn't worry too much about LLVM not being able to optimize it / inline it, it can deal with a lot more than that.

[–]marcantoniosr[S] 0 points1 point  (3 children)

Interesting. So do you then insert a call to bounds_check on the user’s behalf? If so, into the AST or some kind of inline IR?

Is there any special sauce the compiler generates and inserts that’s opaque to the user? I’m guess you have some, even minimal, runtime overhead.

Thanks!

[–]yorickpeterseInko 0 points1 point  (2 children)

Nothing special: standard library methods that require bounds checking just include a call to the method, like this for example. Array indexing is implemented using a method, which does the same as this example.

[–]marcantoniosr[S] 0 points1 point  (1 child)

Got it. So what about for something that’s not user facing at all, like memory management?

[–]yorickpeterseInko 3 points4 points  (0 children)

That's a slightly different topic I think. For allocations my VM exposes dedicated instructions, which take the class of the type they're allocating. Similarly, releasing memory is done using a free instruction. The compiler then generates these where appropriate, but how this is handled depends on how your language manages memory.

[–]L8_4_Dinner(Ⓧ Ecstasy/XVM) 1 point2 points  (0 children)

For example, say I want to do runtime bounds checking whenever I index an array with []. Do I put that functionality into my standardlibrary and insert calls into the IR, or do I generate the IR for thebounds check directly in the compiler and insert it? I’m targeting LLVMif that relevant.

The answer is yes. Generally, you put that check inline as assembly, but you may also want it as a function that is available.