hashc
hashc copied to clipboard
tir+lower: emit function names for monomorphised functions
Currently, the semantic pass doesn't generate names for monomorphised names, so for code like:
main := () => {
buf : [u8; 9] = [1u8; 9]
mut sum := 0u32
mut count := 0usize
while count < 9 {
sum += cast<_, u32>(buf[count])
count += 1usize
}
if sum == 9 {
println("sum is 9")
} else {
println("sum is not 9")
}
}
The cast
function which is generic over T
and U
is generated as the following IR:
main := () -> () {
mut _0: ();
_1: [u8; 9]; // parameter `buf`
_2: u32; // parameter `sum`
_3: usize; // parameter `count`
mut _4: bool;
mut _5: u32;
_6: usize;
_7: (u32, bool);
_8: (usize, bool);
mut _9: bool;
bb0: {
_1 = [const 1_u8; 9];
_2 = const 0_u32;
_3 = const 0_u64;
goto -> bb1;
}
bb1: {
_4 = Lt(_3, const 9_u64);
switch(_4) [false -> bb2, otherwise -> bb3];
}
bb2: {
_9 = Eq(_2, const 9_u32);
switch(_9) [false -> bb7, otherwise -> bb8];
}
bb3: {
_6 = _3;
_5 = _(_1[_6]) -> bb4; // <-- the `cast` function is the `_`
}
...
}
In order to generate names, we would have to change the mono pass to create/inherit names from the original polymorphic function. It is still unclear whether the function naming scheme should be, some possible variants:
-
cast_u8_u32
- we should append the concrete type arguments after the initial function name. -
cast<u8, u32>
- we use a similar syntax to the type arguments as in the language. The symbol mangling in the code generation would need to be adjusted to support this (but it shouldn't be too much of a problem).