Skip to content

Instantly share code, notes, and snippets.

@nuclearace
Last active May 11, 2018 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuclearace/b6cc1241487c3b1dc729edaa60b08da5 to your computer and use it in GitHub Desktop.
Save nuclearace/b6cc1241487c3b1dc729edaa60b08da5 to your computer and use it in GitHub Desktop.
sil_stage canonical
import Builtin
import Swift
import SwiftShims
class MySuper<T> {
func doesThing(_ x: T) -> T
init()
deinit
}
class MyChild : MySuper<Int> {
override func doesThing(_ x: Int) -> Int
override init()
deinit
}
let a: MyChild
let bound: (Int) -> Int
// a
sil_global hidden [let] @$S4main1aAA7MyChildCvp : $MyChild
// bound
sil_global hidden [let] @$S4main5boundyS2icvp : $@callee_guaranteed (Int) -> Int
// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
alloc_global @$S4main1aAA7MyChildCvp // id: %2
%3 = global_addr @$S4main1aAA7MyChildCvp : $*MyChild // users: %10, %7
%4 = metatype $@thick MyChild.Type // user: %6
// function_ref MyChild.__allocating_init()
%5 = function_ref @$S4main7MyChildCACycfC : $@convention(method) (@thick MyChild.Type) -> @owned MyChild // user: %6
%6 = apply %5(%4) : $@convention(method) (@thick MyChild.Type) -> @owned MyChild // user: %7
store %6 to %3 : $*MyChild // id: %7
alloc_global @$S4main5boundyS2icvp // id: %8
%9 = global_addr @$S4main5boundyS2icvp : $*@callee_guaranteed (Int) -> Int // users: %25, %15
%10 = load %3 : $*MyChild // user: %12
// function_ref curry thunk of MyChild.doesThing(_:)
%11 = function_ref @$S4main7MyChildC9doesThingyS2iFTc : $@convention(thin) (@guaranteed MyChild) -> @owned @callee_guaranteed (@in_guaranteed Int) -> @out Int // user: %12
%12 = apply %11(%10) : $@convention(thin) (@guaranteed MyChild) -> @owned @callee_guaranteed (@in_guaranteed Int) -> @out Int // user: %14
// function_ref thunk for @escaping @callee_guaranteed (@in_guaranteed Int) -> (@out Int)
%13 = function_ref @$SS2iIegnr_S2iIegyd_TR : $@convention(thin) (Int, @guaranteed @callee_guaranteed (@in_guaranteed Int) -> @out Int) -> Int // user: %14
%14 = partial_apply [callee_guaranteed] %13(%12) : $@convention(thin) (Int, @guaranteed @callee_guaranteed (@in_guaranteed Int) -> @out Int) -> Int // user: %15
store %14 to %9 : $*@callee_guaranteed (Int) -> Int // id: %15
%16 = integer_literal $Builtin.Word, 1 // user: %18
// function_ref specialized _allocateUninitializedArray<A>(_:)
%17 = function_ref @$Ss27_allocateUninitializedArrayySayxG_BptBwlFyp_Tg5 : $@convention(thin) (Builtin.Word) -> (@owned Array<Any>, Builtin.RawPointer) // user: %18
%18 = apply %17(%16) : $@convention(thin) (Builtin.Word) -> (@owned Array<Any>, Builtin.RawPointer) // users: %21, %19, %22
%19 = tuple_extract %18 : $(Array<Any>, Builtin.RawPointer), 0 // users: %40, %36, %20
retain_value %19 : $Array<Any> // id: %20
%21 = tuple_extract %18 : $(Array<Any>, Builtin.RawPointer), 1 // user: %23
release_value %18 : $(Array<Any>, Builtin.RawPointer) // id: %22
%23 = pointer_to_address %21 : $Builtin.RawPointer to [strict] $*Any // user: %24
%24 = init_existential_addr %23 : $*Any, $Int // user: %30
%25 = load %9 : $*@callee_guaranteed (Int) -> Int // users: %39, %29, %26
strong_retain %25 : $@callee_guaranteed (Int) -> Int // id: %26
%27 = integer_literal $Builtin.Int64, 5 // user: %28
%28 = struct $Int (%27 : $Builtin.Int64) // user: %29
%29 = apply %25(%28) : $@callee_guaranteed (Int) -> Int // user: %30
store %29 to %24 : $*Int // id: %30
// function_ref default argument 1 of print(_:separator:terminator:)
%31 = function_ref @$Ss5print_9separator10terminatoryypd_S2StFfA0_ : $@convention(thin) () -> @owned String // user: %32
%32 = apply %31() : $@convention(thin) () -> @owned String // users: %38, %36
// function_ref default argument 2 of print(_:separator:terminator:)
%33 = function_ref @$Ss5print_9separator10terminatoryypd_S2StFfA1_ : $@convention(thin) () -> @owned String // user: %34
%34 = apply %33() : $@convention(thin) () -> @owned String // users: %37, %36
// function_ref print(_:separator:terminator:)
%35 = function_ref @$Ss5print_9separator10terminatoryypd_S2StF : $@convention(thin) (@guaranteed Array<Any>, @guaranteed String, @guaranteed String) -> () // user: %36
%36 = apply %35(%19, %32, %34) : $@convention(thin) (@guaranteed Array<Any>, @guaranteed String, @guaranteed String) -> ()
release_value %34 : $String // id: %37
release_value %32 : $String // id: %38
strong_release %25 : $@callee_guaranteed (Int) -> Int // id: %39
release_value %19 : $Array<Any> // id: %40
%41 = integer_literal $Builtin.Int32, 0 // user: %42
%42 = struct $Int32 (%41 : $Builtin.Int32) // user: %43
return %42 : $Int32 // id: %43
} // end sil function 'main'
// MySuper.doesThing(_:)
sil hidden @$S4main7MySuperC9doesThingyxxF : $@convention(method) <T> (@in_guaranteed T, @guaranteed MySuper<T>) -> @out T {
// %0 // user: %5
// %1 // users: %5, %3
// %2 // user: %4
bb0(%0 : $*T, %1 : $*T, %2 : $MySuper<T>):
debug_value_addr %1 : $*T, let, name "x", argno 1 // id: %3
debug_value %2 : $MySuper<T>, let, name "self", argno 2 // id: %4
copy_addr %1 to [initialization] %0 : $*T // id: %5
%6 = tuple () // user: %7
return %6 : $() // id: %7
} // end sil function '$S4main7MySuperC9doesThingyxxF'
// MySuper.__allocating_init()
sil hidden @$S4main7MySuperCACyxGycfC : $@convention(method) <T> (@thick MySuper<T>.Type) -> @owned MySuper<T> {
bb0(%0 : $@thick MySuper<T>.Type):
%1 = alloc_ref $MySuper<T> // user: %3
// function_ref MySuper.init()
%2 = function_ref @$S4main7MySuperCACyxGycfc : $@convention(method) <τ_0_0> (@owned MySuper<τ_0_0>) -> @owned MySuper<τ_0_0> // user: %3
%3 = apply %2<T>(%1) : $@convention(method) <τ_0_0> (@owned MySuper<τ_0_0>) -> @owned MySuper<τ_0_0> // user: %4
return %3 : $MySuper<T> // id: %4
} // end sil function '$S4main7MySuperCACyxGycfC'
// MySuper.init()
sil hidden @$S4main7MySuperCACyxGycfc : $@convention(method) <T> (@owned MySuper<T>) -> @owned MySuper<T> {
// %0 // users: %2, %1
bb0(%0 : $MySuper<T>):
debug_value %0 : $MySuper<T>, let, name "self", argno 1 // id: %1
return %0 : $MySuper<T> // id: %2
} // end sil function '$S4main7MySuperCACyxGycfc'
// MySuper.deinit
sil hidden @$S4main7MySuperCfd : $@convention(method) <T> (@guaranteed MySuper<T>) -> @owned Builtin.NativeObject {
// %0 // users: %2, %1
bb0(%0 : $MySuper<T>):
debug_value %0 : $MySuper<T>, let, name "self", argno 1 // id: %1
%2 = unchecked_ref_cast %0 : $MySuper<T> to $Builtin.NativeObject // user: %3
return %2 : $Builtin.NativeObject // id: %3
} // end sil function '$S4main7MySuperCfd'
// MySuper.__deallocating_deinit
sil hidden @$S4main7MySuperCfD : $@convention(method) <T> (@owned MySuper<T>) -> () {
// %0 // users: %3, %1
bb0(%0 : $MySuper<T>):
debug_value %0 : $MySuper<T>, let, name "self", argno 1 // id: %1
// function_ref MySuper.deinit
%2 = function_ref @$S4main7MySuperCfd : $@convention(method) <τ_0_0> (@guaranteed MySuper<τ_0_0>) -> @owned Builtin.NativeObject // user: %3
%3 = apply %2<T>(%0) : $@convention(method) <τ_0_0> (@guaranteed MySuper<τ_0_0>) -> @owned Builtin.NativeObject // user: %4
%4 = unchecked_ref_cast %3 : $Builtin.NativeObject to $MySuper<T> // user: %5
dealloc_ref %4 : $MySuper<T> // id: %5
%6 = tuple () // user: %7
return %6 : $() // id: %7
} // end sil function '$S4main7MySuperCfD'
// MyChild.doesThing(_:)
sil hidden @$S4main7MyChildC9doesThingyS2iF : $@convention(method) (Int, @guaranteed MyChild) -> Int {
// %0 // users: %5, %2
// %1 // user: %3
bb0(%0 : $Int, %1 : $MyChild):
debug_value %0 : $Int, let, name "x", argno 1 // id: %2
debug_value %1 : $MyChild, let, name "self", argno 2 // id: %3
%4 = integer_literal $Builtin.Int64, 5 // user: %7
%5 = struct_extract %0 : $Int, #Int._value // user: %7
%6 = integer_literal $Builtin.Int1, -1 // user: %7
%7 = builtin "sadd_with_overflow_Int64"(%5 : $Builtin.Int64, %4 : $Builtin.Int64, %6 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1) // users: %9, %8
%8 = tuple_extract %7 : $(Builtin.Int64, Builtin.Int1), 0 // user: %11
%9 = tuple_extract %7 : $(Builtin.Int64, Builtin.Int1), 1 // user: %10
cond_fail %9 : $Builtin.Int1 // id: %10
%11 = struct $Int (%8 : $Builtin.Int64) // user: %12
return %11 : $Int // id: %12
} // end sil function '$S4main7MyChildC9doesThingyS2iF'
// Int.init(_builtinIntegerLiteral:)
sil public_external [transparent] [serialized] @$SSi22_builtinIntegerLiteralSiBi2048__tcfC : $@convention(method) (Builtin.Int2048, @thin Int.Type) -> Int {
// %0 // user: %2
bb0(%0 : $Builtin.Int2048, %1 : $@thin Int.Type):
%2 = builtin "s_to_s_checked_trunc_Int2048_Int64"(%0 : $Builtin.Int2048) : $(Builtin.Int64, Builtin.Int1) // user: %3
%3 = tuple_extract %2 : $(Builtin.Int64, Builtin.Int1), 0 // user: %4
%4 = struct $Int (%3 : $Builtin.Int64) // user: %5
return %4 : $Int // id: %5
} // end sil function '$SSi22_builtinIntegerLiteralSiBi2048__tcfC'
// static Int.+ infix(_:_:)
sil public_external [transparent] [serialized] @$SSi1poiyS2i_SitFZ : $@convention(method) (Int, Int, @thin Int.Type) -> Int {
// %0 // user: %3
// %1 // user: %4
bb0(%0 : $Int, %1 : $Int, %2 : $@thin Int.Type):
%3 = struct_extract %0 : $Int, #Int._value // user: %6
%4 = struct_extract %1 : $Int, #Int._value // user: %6
%5 = integer_literal $Builtin.Int1, -1 // user: %6
%6 = builtin "sadd_with_overflow_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64, %5 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1) // users: %8, %7
%7 = tuple_extract %6 : $(Builtin.Int64, Builtin.Int1), 0 // user: %10
%8 = tuple_extract %6 : $(Builtin.Int64, Builtin.Int1), 1 // user: %9
cond_fail %8 : $Builtin.Int1 // id: %9
%10 = struct $Int (%7 : $Builtin.Int64) // user: %11
return %10 : $Int // id: %11
} // end sil function '$SSi1poiyS2i_SitFZ'
// MyChild.__allocating_init()
sil hidden @$S4main7MyChildCACycfC : $@convention(method) (@thick MyChild.Type) -> @owned MyChild {
bb0(%0 : $@thick MyChild.Type):
%1 = alloc_ref $MyChild // user: %3
// function_ref MyChild.init()
%2 = function_ref @$S4main7MyChildCACycfc : $@convention(method) (@owned MyChild) -> @owned MyChild // user: %3
%3 = apply %2(%1) : $@convention(method) (@owned MyChild) -> @owned MyChild // user: %4
return %3 : $MyChild // id: %4
} // end sil function '$S4main7MyChildCACycfC'
// MyChild.init()
sil hidden @$S4main7MyChildCACycfc : $@convention(method) (@owned MyChild) -> @owned MyChild {
// %0 // users: %3, %2
bb0(%0 : $MyChild):
%1 = alloc_stack $MyChild, let, name "self" // users: %7, %2, %9, %10
store %0 to %1 : $*MyChild // id: %2
%3 = upcast %0 : $MyChild to $MySuper<Int> // user: %5
// function_ref MySuper.init()
%4 = function_ref @$S4main7MySuperCACyxGycfc : $@convention(method) <τ_0_0> (@owned MySuper<τ_0_0>) -> @owned MySuper<τ_0_0> // user: %5
%5 = apply %4<Int>(%3) : $@convention(method) <τ_0_0> (@owned MySuper<τ_0_0>) -> @owned MySuper<τ_0_0> // user: %6
%6 = unchecked_ref_cast %5 : $MySuper<Int> to $MyChild // users: %8, %11, %7
store %6 to %1 : $*MyChild // id: %7
strong_retain %6 : $MyChild // id: %8
destroy_addr %1 : $*MyChild // id: %9
dealloc_stack %1 : $*MyChild // id: %10
return %6 : $MyChild // id: %11
} // end sil function '$S4main7MyChildCACycfc'
// MyChild.deinit
sil hidden @$S4main7MyChildCfd : $@convention(method) (@guaranteed MyChild) -> @owned Builtin.NativeObject {
// %0 // users: %2, %1
bb0(%0 : $MyChild):
debug_value %0 : $MyChild, let, name "self", argno 1 // id: %1
%2 = upcast %0 : $MyChild to $MySuper<Int> // user: %4
// function_ref MySuper.deinit
%3 = function_ref @$S4main7MySuperCfd : $@convention(method) <τ_0_0> (@guaranteed MySuper<τ_0_0>) -> @owned Builtin.NativeObject // user: %4
%4 = apply %3<Int>(%2) : $@convention(method) <τ_0_0> (@guaranteed MySuper<τ_0_0>) -> @owned Builtin.NativeObject // users: %5, %6
%5 = unchecked_ref_cast %4 : $Builtin.NativeObject to $MyChild
return %4 : $Builtin.NativeObject // id: %6
} // end sil function '$S4main7MyChildCfd'
// MyChild.__deallocating_deinit
sil hidden @$S4main7MyChildCfD : $@convention(method) (@owned MyChild) -> () {
// %0 // users: %3, %1
bb0(%0 : $MyChild):
debug_value %0 : $MyChild, let, name "self", argno 1 // id: %1
// function_ref MyChild.deinit
%2 = function_ref @$S4main7MyChildCfd : $@convention(method) (@guaranteed MyChild) -> @owned Builtin.NativeObject // user: %3
%3 = apply %2(%0) : $@convention(method) (@guaranteed MyChild) -> @owned Builtin.NativeObject // user: %4
%4 = unchecked_ref_cast %3 : $Builtin.NativeObject to $MyChild // user: %5
dealloc_ref %4 : $MyChild // id: %5
%6 = tuple () // user: %7
return %6 : $() // id: %7
} // end sil function '$S4main7MyChildCfD'
// vtable thunk for MySuper.doesThing(_:) dispatching to MyChild.doesThing(_:)
sil private @$S4main7MyChildC9doesThingyS2iFAA0B5SuperCADyxxFTV : $@convention(method) (@in_guaranteed Int, @guaranteed MyChild) -> @out Int {
// %0 // user: %6
// %1 // user: %3
// %2 // user: %5
bb0(%0 : $*Int, %1 : $*Int, %2 : $MyChild):
%3 = load %1 : $*Int // user: %5
// function_ref MyChild.doesThing(_:)
%4 = function_ref @$S4main7MyChildC9doesThingyS2iF : $@convention(method) (Int, @guaranteed MyChild) -> Int // user: %5
%5 = apply %4(%3, %2) : $@convention(method) (Int, @guaranteed MyChild) -> Int // user: %6
store %5 to %0 : $*Int // id: %6
%7 = tuple () // user: %8
return %7 : $() // id: %8
} // end sil function '$S4main7MyChildC9doesThingyS2iFAA0B5SuperCADyxxFTV'
// curry thunk of MyChild.doesThing(_:)
sil shared [thunk] @$S4main7MyChildC9doesThingyS2iFTc : $@convention(thin) (@guaranteed MyChild) -> @owned @callee_guaranteed (@in_guaranteed Int) -> @out Int {
// %0 // users: %3, %2, %1
bb0(%0 : $MyChild):
%1 = class_method %0 : $MyChild, #MyChild.doesThing!1 : (MyChild) -> (Int) -> Int, $@convention(method) (@in_guaranteed Int, @guaranteed MyChild) -> @out Int // user: %3
strong_retain %0 : $MyChild // id: %2
%3 = partial_apply [callee_guaranteed] %1(%0) : $@convention(method) (@in_guaranteed Int, @guaranteed MyChild) -> @out Int // user: %4
return %3 : $@callee_guaranteed (@in_guaranteed Int) -> @out Int // id: %4
} // end sil function '$S4main7MyChildC9doesThingyS2iFTc'
// thunk for @escaping @callee_guaranteed (@in_guaranteed Int) -> (@out Int)
sil shared [transparent] [serializable] [reabstraction_thunk] @$SS2iIegnr_S2iIegyd_TR : $@convention(thin) (Int, @guaranteed @callee_guaranteed (@in_guaranteed Int) -> @out Int) -> Int {
// %0 // user: %3
// %1 // user: %5
bb0(%0 : $Int, %1 : $@callee_guaranteed (@in_guaranteed Int) -> @out Int):
%2 = alloc_stack $Int // users: %3, %8, %5
store %0 to %2 : $*Int // id: %3
%4 = alloc_stack $Int // users: %6, %7, %5
%5 = apply %1(%4, %2) : $@callee_guaranteed (@in_guaranteed Int) -> @out Int
%6 = load %4 : $*Int // user: %9
dealloc_stack %4 : $*Int // id: %7
dealloc_stack %2 : $*Int // id: %8
return %6 : $Int // id: %9
} // end sil function '$SS2iIegnr_S2iIegyd_TR'
// _allocateUninitializedArray<A>(_:)
sil [serialized] [always_inline] @$Ss27_allocateUninitializedArrayySayxG_BptBwlF : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)
// default argument 1 of print(_:separator:terminator:)
sil [noinline] @$Ss5print_9separator10terminatoryypd_S2StFfA0_ : $@convention(thin) () -> @owned String
// default argument 2 of print(_:separator:terminator:)
sil [noinline] @$Ss5print_9separator10terminatoryypd_S2StFfA1_ : $@convention(thin) () -> @owned String
// print(_:separator:terminator:)
sil [noinline] @$Ss5print_9separator10terminatoryypd_S2StF : $@convention(thin) (@guaranteed Array<Any>, @guaranteed String, @guaranteed String) -> ()
// specialized _allocateUninitializedArray<A>(_:)
sil [always_inline] @$Ss27_allocateUninitializedArrayySayxG_BptBwlFyp_Tg5 : $@convention(thin) (Builtin.Word) -> (@owned Array<Any>, Builtin.RawPointer)
sil_vtable MySuper {
#MySuper.doesThing!1: <T> (MySuper<T>) -> (T) -> T : @$S4main7MySuperC9doesThingyxxF // MySuper.doesThing(_:)
#MySuper.init!initializer.1: <T> (MySuper<T>.Type) -> () -> MySuper<T> : @$S4main7MySuperCACyxGycfc // MySuper.init()
#MySuper.deinit!deallocator: @$S4main7MySuperCfD // MySuper.__deallocating_deinit
}
sil_vtable MyChild {
#MySuper.doesThing!1: <T> (MySuper<T>) -> (T) -> T : hidden @$S4main7MyChildC9doesThingyS2iFAA0B5SuperCADyxxFTV [override] // vtable thunk for MySuper.doesThing(_:) dispatching to MyChild.doesThing(_:)
#MySuper.init!initializer.1: <T> (MySuper<T>.Type) -> () -> MySuper<T> : @$S4main7MyChildCACycfc [override] // MyChild.init()
#MyChild.deinit!deallocator: @$S4main7MyChildCfD // MyChild.__deallocating_deinit
}
class MySuper<T> {
func doesThing(_ x: T) -> T {
return x
}
}
class MyChild : MySuper<Int> {
override func doesThing(_ x: Int) -> Int {
return x + 5
}
}
let a = MyChild()
let bound = a.doesThing
print(bound(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment