ssa: impl builtin alignof offsetof

This commit is contained in:
visualfc
2025-09-18 21:06:30 +08:00
parent 18e036568d
commit 7323187f90
3 changed files with 274 additions and 0 deletions

View File

@@ -1257,7 +1257,23 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
case "Add":
return b.Advance(args[0], args[1])
case "Sizeof":
// instance of generic function
return b.Prog.Val(int(b.Prog.SizeOf(args[0].Type)))
case "Alignof":
// instance of generic function
return b.Prog.Val(int(b.Prog.td.ABITypeAlignment(args[0].ll)))
case "Offsetof":
// instance of generic function
if load := args[0].impl.IsALoadInst(); !load.IsNil() {
if gep := load.Operand(0).IsAGetElementPtrInst(); !gep.IsNil() {
typ := gep.GEPSourceElementType()
offset := gep.Operand(2).IsAConstantInt()
if typ.TypeKind() == llvm.StructTypeKind && !offset.IsNil() {
return b.Prog.Val(int(b.Prog.td.ElementOffset(typ, int(offset.SExtValue()))))
}
}
}
panic("invalid argument for unsafe.Offsetof: must be a selector expression")
}
panic("todo: " + fn)
}