@@ -1187,3 +1187,64 @@ declare i32 @setjmp(ptr) #0
11871187attributes #0 = { returns_twice }
11881188` )
11891189}
1190+
1191+ func TestAbiTables (t * testing.T ) {
1192+ prog := NewProgram (nil )
1193+ prog .sizes = types .SizesFor ("gc" , runtime .GOARCH )
1194+ prog .SetRuntime (func () * types.Package {
1195+ pkg , err := importer .For ("source" , nil ).Import (PkgRuntime )
1196+ if err != nil {
1197+ t .Fatal (err )
1198+ }
1199+ return pkg
1200+ })
1201+ pkg := prog .NewPackage ("bar" , "foo/bar" )
1202+
1203+ emptyIface := types .NewInterfaceType (nil , nil )
1204+ emptyIface .Complete ()
1205+ emptyType := prog .Type (emptyIface , InGo )
1206+
1207+ makeFn := func (name string , x Expr ) {
1208+ sig := types .NewSignatureType (nil , nil , nil , nil , types .NewTuple (types .NewVar (0 , nil , "" , emptyIface )), false )
1209+ fn := pkg .NewFunc (name , sig , InGo )
1210+ b := fn .MakeBody (1 )
1211+ iface := b .MakeInterface (emptyType , x )
1212+ b .Return (iface )
1213+ }
1214+
1215+ makeFn ("intIface" , prog .Val (1 ))
1216+ makeFn ("ptrIface" , prog .Nil (prog .VoidPtr ()))
1217+ makeFn ("floatIface" , prog .FloatVal (3.5 , prog .Float32 ()))
1218+
1219+ st := types .NewStruct ([]* types.Var {
1220+ types .NewVar (0 , nil , "a" , types .Typ [types .Int ]),
1221+ types .NewVar (0 , nil , "b" , types .Typ [types .Int ]),
1222+ }, nil )
1223+ makeFn ("structIface" , prog .Zero (prog .Type (st , InGo )))
1224+
1225+ single := types .NewStruct ([]* types.Var {
1226+ types .NewVar (0 , nil , "v" , types .Typ [types .Int ]),
1227+ }, nil )
1228+ makeFn ("singleFieldIface" , prog .Zero (prog .Type (single , InGo )))
1229+
1230+ pkgTypes := types .NewPackage ("foo/bar" , "bar" )
1231+ rawSig := types .NewSignatureType (nil , nil , nil , nil , nil , false )
1232+ rawMeth := types .NewFunc (0 , pkgTypes , "M" , rawSig )
1233+ nonEmpty := types .NewInterfaceType ([]* types.Func {rawMeth }, nil )
1234+ nonEmpty .Complete ()
1235+ nonEmptyType := prog .Type (nonEmpty , InGo )
1236+ sigNE := types .NewSignatureType (nil , nil , nil , nil , types .NewTuple (types .NewVar (0 , nil , "" , nonEmpty )), false )
1237+ fnNE := pkg .NewFunc ("nonEmptyIface" , sigNE , InGo )
1238+ bNE := fnNE .MakeBody (1 )
1239+ bNE .Return (bNE .MakeInterface (nonEmptyType , prog .Val (7 )))
1240+
1241+ fn := pkg .InitAbiTypes (pkg .Path () + ".init$abitables" )
1242+ s := fn .impl .String ()
1243+ if ! strings .Contains (s , `define void @"foo/bar.init$abitables"() {
1244+ _llgo_0:
1245+ call void @"github.com/goplus/llgo/runtime/internal/runtime.initTypes"(ptr @"foo/bar.init$abitables$slice")
1246+ ret void
1247+ }` ) {
1248+ t .Fatal ("error abi tables" , s )
1249+ }
1250+ }
0 commit comments