transform: split interface and reflect lowering

These two passes are related, but can definitely work independently.
Which is what this change does: it splits the two passes. This should
make it easier to change these two new passes in the future.

This change now also enables slightly better testing by testing these
two passes independently. In particular, the reflect lowering pass got
some actual tests: it was barely unit-tested before.

I have verified that this doesn't really change code size, at least not
on the microbit target. Two tests do change, but in a very minor way
(and in opposite direction).
This commit is contained in:
Ayke van Laethem
2021-04-26 13:53:05 +02:00
committed by Ron Evans
parent 52d8655eec
commit cd517a30af
10 changed files with 395 additions and 280 deletions
+2 -2
View File
@@ -4,10 +4,10 @@ target triple = "armv7m-none-eabi"
%runtime.typecodeID = type { %runtime.typecodeID*, i32, %runtime.interfaceMethodInfo* }
%runtime.interfaceMethodInfo = type { i8*, i32 }
@"reflect/types.type:basic:uint8" = external constant %runtime.typecodeID
@"reflect/types.type:basic:uint8" = private constant %runtime.typecodeID zeroinitializer
@"reflect/types.typeid:basic:uint8" = external constant i8
@"reflect/types.typeid:basic:int16" = external constant i8
@"reflect/types.type:basic:int" = external constant %runtime.typecodeID
@"reflect/types.type:basic:int" = private constant %runtime.typecodeID zeroinitializer
@"func NeverImplementedMethod()" = external constant i8
@"Unmatched$interface" = private constant [1 x i8*] [i8* @"func NeverImplementedMethod()"]
@"func Double() int" = external constant i8
+17 -33
View File
@@ -4,19 +4,9 @@ target triple = "armv7m-none-eabi"
%runtime.typecodeID = type { %runtime.typecodeID*, i32, %runtime.interfaceMethodInfo* }
%runtime.interfaceMethodInfo = type { i8*, i32 }
@"reflect/types.type:basic:uint8" = external constant %runtime.typecodeID
@"reflect/types.typeid:basic:uint8" = external constant i8
@"reflect/types.typeid:basic:int16" = external constant i8
@"reflect/types.type:basic:int" = external constant %runtime.typecodeID
@"func NeverImplementedMethod()" = external constant i8
@"func Double() int" = external constant i8
@"reflect/types.type:named:Number" = private constant %runtime.typecodeID zeroinitializer
declare i1 @runtime.interfaceImplements(i32, i8**)
declare i1 @runtime.typeAssert(i32, i8*)
declare i32 @runtime.interfaceMethod(i32, i8**, i8*)
@"reflect/types.type:basic:uint8" = private constant %runtime.typecodeID zeroinitializer
@"reflect/types.type:basic:int" = private constant %runtime.typecodeID zeroinitializer
@"reflect/types.type:named:Number" = private constant %runtime.typecodeID { %runtime.typecodeID* @"reflect/types.type:basic:int", i32 0, %runtime.interfaceMethodInfo* null }
declare void @runtime.printuint8(i8)
@@ -31,9 +21,9 @@ declare void @runtime.printnl()
declare void @runtime.nilPanic(i8*, i8*)
define void @printInterfaces() {
call void @printInterface(i32 4, i8* inttoptr (i32 5 to i8*))
call void @printInterface(i32 16, i8* inttoptr (i8 120 to i8*))
call void @printInterface(i32 68, i8* inttoptr (i32 3 to i8*))
call void @printInterface(i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:basic:int" to i32), i8* inttoptr (i32 5 to i8*))
call void @printInterface(i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:basic:uint8" to i32), i8* inttoptr (i8 120 to i8*))
call void @printInterface(i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:named:Number" to i32), i8* inttoptr (i32 3 to i8*))
ret void
}
@@ -57,7 +47,7 @@ typeswitch.Doubler: ; preds = %typeswitch.notUnmat
ret void
typeswitch.notDoubler: ; preds = %typeswitch.notUnmatched
%typeassert.ok2 = icmp eq i32 16, %typecode
%typeassert.ok2 = icmp eq i32 ptrtoint (%runtime.typecodeID* @"reflect/types.type:basic:uint8" to i32), %typecode
br i1 %typeassert.ok2, label %typeswitch.byte, label %typeswitch.notByte
typeswitch.byte: ; preds = %typeswitch.notDoubler
@@ -92,40 +82,34 @@ define i32 @"(Number).Double$invoke"(i8* %receiverPtr, i8* %parentHandle) {
define internal i32 @"(Doubler).Double"(i8* %0, i8* %1, i32 %actualType, i8* %parentHandle) unnamed_addr {
entry:
switch i32 %actualType, label %default [
i32 68, label %"named:Number"
]
default: ; preds = %entry
call void @runtime.nilPanic(i8* undef, i8* undef)
unreachable
%"named:Number.icmp" = icmp eq i32 %actualType, ptrtoint (%runtime.typecodeID* @"reflect/types.type:named:Number" to i32)
br i1 %"named:Number.icmp", label %"named:Number", label %"named:Number.next"
"named:Number": ; preds = %entry
%2 = call i32 @"(Number).Double$invoke"(i8* %0, i8* %1)
ret i32 %2
"named:Number.next": ; preds = %entry
call void @runtime.nilPanic(i8* undef, i8* undef)
unreachable
}
define internal i1 @"Doubler$typeassert"(i32 %actualType) unnamed_addr {
entry:
switch i32 %actualType, label %else [
i32 68, label %then
]
%"named:Number.icmp" = icmp eq i32 %actualType, ptrtoint (%runtime.typecodeID* @"reflect/types.type:named:Number" to i32)
br i1 %"named:Number.icmp", label %then, label %"named:Number.next"
then: ; preds = %entry
ret i1 true
else: ; preds = %entry
"named:Number.next": ; preds = %entry
ret i1 false
}
define internal i1 @"Unmatched$typeassert"(i32 %actualType) unnamed_addr {
entry:
switch i32 %actualType, label %else [
]
ret i1 false
then: ; No predecessors!
ret i1 true
else: ; preds = %entry
ret i1 false
}
+56
View File
@@ -0,0 +1,56 @@
package main
// This file tests the type codes assigned by the reflect lowering pass.
// This test is not complete, most importantly, sidetables are not currently
// being tested.
import (
"reflect"
"unsafe"
)
const (
// See the top of src/reflect/type.go
prefixChan = 0b0001
prefixInterface = 0b0011
prefixPtr = 0b0101
prefixSlice = 0b0111
prefixArray = 0b1001
prefixFunc = 0b1011
prefixMap = 0b1101
prefixStruct = 0b1111
)
func main() {
// Check for some basic types.
assertType(3, uintptr(reflect.Int)<<1)
assertType(uint8(3), uintptr(reflect.Uint8)<<1)
assertType(byte(3), uintptr(reflect.Uint8)<<1)
assertType(int64(3), uintptr(reflect.Int64)<<1)
assertType("", uintptr(reflect.String)<<1)
assertType(3.5, uintptr(reflect.Float64)<<1)
assertType(unsafe.Pointer(nil), uintptr(reflect.UnsafePointer)<<1)
// Check for named types: they are given names in order.
// They are sorted in reverse, for no good reason.
const intNum = uintptr(reflect.Int) << 1
assertType(namedInt1(0), (3<<6)|intNum)
assertType(namedInt2(0), (2<<6)|intNum)
assertType(namedInt3(0), (1<<6)|intNum)
// Check for some "prefix-style" types.
assertType(make(chan int), (intNum<<5)|prefixChan)
assertType(new(int), (intNum<<5)|prefixPtr)
assertType([]int{}, (intNum<<5)|prefixSlice)
}
type (
namedInt1 int
namedInt2 int
namedInt3 int
)
// Pseudo call that is being checked by the code in reflect_test.go.
// After reflect lowering, the type code as part of the interface should match
// the asserted type code.
func assertType(itf interface{}, assertedTypeCode uintptr)