zetah11 / zippy

a smol functional/imperative programming language
MIT License
2 stars 0 forks source link

runtime initialization #4

Closed zetah11 closed 2 years ago

zetah11 commented 2 years ago

Currently, only top-level functions are supported. Several things need to be implemented to support top-level values too:

zetah11 commented 2 years ago

Constant values are more-or-less implemented, though I'm not really happy with how they work currently. For the moment, we just drop the bytes straight into the code and use labels provided by iced-x86, which gives us rip-relative addressing. This is fine, I guess, but I'd prefer to generate the data segment separately (and put it in its own section in emit). That's what the bespoke x64 backend did, but it didn't do RIP-relative addressing.

Basically, the current problem is that iced doesn't really play all that nicely with relocations. In order to get the address of a relocation we need to

As such, I'm adding a "handle relocations properly" task.

zetah11 commented 2 years ago

I think all of this kind of gets put on hold by #8

zetah11 commented 2 years ago

With the C backend, this will presumably be easier ("just create an init func"), though I want to redo the architecture a bit. I'm thinking that top-level values should really be a (Name, SimpleValue) pair, where SimpleValue is just an int, byte string or uninitialized, and when lowering from the MIR to this representation, anything that doesn't fit into that gets an uninitialized SimpleValue with some initialization code in that init func.

There's also the question of mutually recursive values. If not handled carefully, they can let us read uninitialized memory with this setup:

let x: 10 = y
let y: 10 = x
fun main (?: 1) = y

becomes something like

static char x;
static char y;
void init(void) {
  x = y;
  y = x;
}
// etc.

I think if pointers are involved (as in, if one of the values takes the address of the other), then this should be fine.

zetah11 commented 2 years ago

Basically closed by 372df3255d857fc9eea16eec1dce3576ff3d9656 which took the approach described in the previous comment (though named StaticValue instead of SimpleValue. There's still some care needed wrt pointer and non-pointer values, but I think that's a more general issue that may be the focus later.