mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 19:43:44 +00:00
compiler: lower interfaces in a separate pass
This commit changes many things:
* Most interface-related operations are moved into an optimization
pass for more modularity. IR construction creates pseudo-calls which
are lowered in this pass.
* Type codes are assigned in this interface lowering pass, after DCE.
* Type codes are sorted by usage: types more often used in type
asserts are assigned lower numbers to ease jump table construction
during machine code generation.
* Interface assertions are optimized: they are replaced by constant
false, comparison against a constant, or a typeswitch with only
concrete types in the general case.
* Interface calls are replaced with unreachable, direct calls, or a
concrete type switch with direct calls depending on the number of
implementing types. This hopefully makes some interface patterns
zero-cost.
These changes lead to a ~0.5K reduction in code size on Cortex-M for
testdata/interface.go. It appears that a major cause for this is the
replacement of function pointers with direct calls, which are far more
susceptible to optimization. Also, not having a fixed global array of
function pointers greatly helps dead code elimination.
This change also makes future optimizations easier, like optimizations
on interface value comparisons.
This commit is contained in:
Vendored
+7
@@ -26,6 +26,8 @@ func main() {
|
||||
|
||||
func printItf(val interface{}) {
|
||||
switch val := val.(type) {
|
||||
case Unmatched:
|
||||
panic("matched the unmatchable")
|
||||
case Doubler:
|
||||
println("is Doubler:", val.Double())
|
||||
case Tuple:
|
||||
@@ -127,3 +129,8 @@ func (p SmallPair) Nth(n int) uint32 {
|
||||
func (p SmallPair) Print() {
|
||||
println("SmallPair.Print:", p.a, p.b)
|
||||
}
|
||||
|
||||
// There is no type that matches this method.
|
||||
type Unmatched interface {
|
||||
NeverImplementedMethod()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user