titzer / virgil

A fast and lightweight native programming language
1.13k stars 40 forks source link

Variant Unboxing #209

Open btwj opened 8 months ago

btwj commented 8 months ago

This PR enables (with the -unbox-variants flag) the ability to unbox ADTs, both at the parent level and at a per-case level. For now, these ADTs are normalized by greedily packing fields into the fewest scalars possible, and then attaching an extra scalar representing the tag.

Todo:

btwj commented 7 months ago

@titzer I've gone ahead and added #unboxed to type Operand here, it bootstraps now!

Also fixed a bug in the existing compiler that I discovered while writing extra tests: equality for boxed variants in tuples wasn't normalized correctly, e.g. if U was a boxed variant, the instruction OverloadedEq<(U, U)> ended up becoming normalized wrongly as VariantEq<U> when it should've become CallVariantSelector<U>[==]. See test/variants/eq00.v3 for an example that crashes the old compiler.

Separately, the equality code for U wasn't deemed reachable from OverloadedEq<(U, U)> because getEquality doesn't recurse into the nested types if the type was a tuple, I've added that as well.