Slide 1

Slide 1 text

4XJGUதؒݴޠΛಡΉ !NBUVZVKJ  ୈճ$PDPBษڧձؔ੢

Slide 2

Slide 2 text

!NBUVZVKJ TBGYEFWCMPHTQPUKQ

Slide 3

Slide 3 text

4XJGUதؒݴޠ 4XJGU*OUFSNFEJBUF-BOHVBHF

Slide 4

Slide 4 text

6OEPDVNFOUFE w ͳΜͱͳ͘ͰಡΉ w งғؾͰ಺༰Λ௫Ή

Slide 5

Slide 5 text

TXJGUD

Slide 6

Slide 6 text

TXJGUDIFMQ $ swiftc --help OVERVIEW: Swift compiler USAGE: swiftc [options] MODES: -dump-ast Parse and type-check input file(s) and dump AST(s) -dump-parse Parse input file(s) and dump AST(s) -dump-type-refinement-contexts Type-check input file(s) and dump type refinement contexts(s) -emit-assembly Emit assembly file(s) (-S) -emit-bc Emit LLVM BC file(s) -emit-executable Emit a linked executable -emit-ir Emit LLVM IR file(s) -emit-library Emit a linked library -emit-object Emit object file(s) (-c) -emit-sibgen Emit serialized AST + raw SIL file(s) -emit-sib Emit serialized AST + canonical SIL file(s) -emit-silgen Emit raw SIL file(s) -emit-sil Emit canonical SIL file(s) -parse Parse input file(s) -print-ast Parse and type-check input file(s) and pretty print AST(s)

Slide 7

Slide 7 text

TXJGUDFNJUTJMHFO $ echo 'print("hello world!")' > hello.swift $ swiftc -emit-silgen hello.swift > hello.sil $ swiftc -parse-sil hello.sil $ ./hello hello world!

Slide 8

Slide 8 text

$ grep -A 7 -B 4 hello hello.sil %11 = init_existential_addr %10 : $*protocol<>, $String // user: %18 // function_ref Swift.String.init (Swift.String.Type)(_builtinStringLitera %12 = function_ref @_TFSSCfMSSFT21_builtinStringLiteralBp8byteSizeBw7isASC %13 = metatype $@thin String.Type // user: %17 %14 = string_literal utf8 "hello world!" // user: %17 %15 = integer_literal $Builtin.Word, 12 // user: %17 %16 = integer_literal $Builtin.Int1, -1 // user: %17 %17 = apply %12(%14, %15, %16, %13) : $@convention(thin) (Builtin.RawPoint store %17 to %11 : $*String // id: %18 // function_ref Swift.(print (Swift.Array>, separator : Swift.S %19 = function_ref @_TIFSs5printFTGSaP__9separatorSS10terminatorSS_T_A0_ : %20 = apply %19() : $@convention(thin) () -> @owned String // user: %23 // pseudo code let p14 = "hello world!" let p15 = 12 let p16 = -1 let p17 = String.init(_builtinStringLiteral: p14, byteSize: p15, isASCII: p16) let p20 = print(p17)

Slide 9

Slide 9 text

TXJGUD 4*- 4XJGU "45 --7. *3 TXJGU --7. #$ /BUJWF BTN TJM FNJUTJMHFO QBSTFTJM /BUJWF CJOBSZ

Slide 10

Slide 10 text

--7. 'SPOUFOE 0QUJNJ[FS #BDLFOE 4*- 4XJGU "45 --7. *3 TXJGU --7. #$ /BUJWF BTN /BUJWF CJOBSZ

Slide 11

Slide 11 text

--7. --7. 0QUJNJ[FS "3. #BDLFOE Y #BDLFOE $MBOH 'SPOUFOE 4XJGU 'SPOUFOE $ $  0CK$ 4XJGU Y CJOBSZ "3. CJOBSZ 'SPOUFOE 0QUJNJ[FS #BDLFOE

Slide 12

Slide 12 text

4*-

Slide 13

Slide 13 text

4*- w 4UBUJD4JOHMF"TTJHONFOU w CSBODIJOH
 br cond_br switch_enum_add w CSBODIXJUIBSHVNFOU
 br bb7(%17 : $Optional)

Slide 14

Slide 14 text

DPOE@CS bb0: : cond_br %9, bb1, bb2 // id: %11 bb1: // Preds: bb0 // function_ref Swift.Bool.init (Swift.Bool.Type)(_builtinBooleanLiteral : Builtin %12 = function_ref @_TFSbCfMSbFT22_builtinBooleanLiteralBi1__Sb : $@convention(thi %13 = metatype $@thin Bool.Type // user: %15 %14 = integer_literal $Builtin.Int1, -1 // user: %15 %15 = apply %12(%14, %13) : $@convention(thin) (Builtin.Int1, @thin Bool.Type) -> br bb3(%15 : $Bool) // id: %16 bb2: // Preds: bb0 // function_ref Swift.Bool.init (Swift.Bool.Type)(_builtinBooleanLiteral : Builtin %17 = function_ref @_TFSbCfMSbFT22_builtinBooleanLiteralBi1__Sb : $@convention(thi %18 = metatype $@thin Bool.Type // user: %20 %19 = integer_literal $Builtin.Int1, 0 // user: %20 %20 = apply %17(%19, %18) : $@convention(thin) (Builtin.Int1, @thin Bool.Type) -> br bb3(%20 : $Bool) // id: %21 bb3(%22 : $Bool): // Preds: bb1 bb2 release_value %0 : $Array // id: %23 return %22 : $Bool // id: %24 if value { return true } else { return false }

Slide 15

Slide 15 text

"SSBZ DPVOU JT&NQUZ pSTU

Slide 16

Slide 16 text

&MFNFOUDIFDL func exa_count(arr: [Int]) -> Bool { if arr.count > 0 { return true } return false } func exa_empty(arr: [Int]) -> Bool { if !arr.isEmpty { return true } return false } func exa_first(arr: [Int]) -> Bool { if let _ = arr.first { return true } return false }

Slide 17

Slide 17 text

/PUPQUJNJ[FEZFU 4*- 4XJGU "45 --7. *3 TXJGU --7. #$ /BUJWF BTN /BUJWF CJOBSZ

Slide 18

Slide 18 text

DPVOU // function_ref static Swift.> infix (Swift.Int, Swift.Int) -> Swift.Bool %3 = function_ref @_TZFSsoi1gFTSiSi_Sb : $@convention(thin) (Int, Int) -> Bool // user: %10 // function_ref Swift.Array.count.getter : Swift.Int %4 = function_ref @_TFSag5countSi : $@convention(method) <ŵЮ0_0> (@guaranteed Array<ŵЮ0_0>) -> Int // user: %5 %5 = apply %4(%0) : $@convention(method) <ŵЮ0_0> (@guaranteed Array<ŵЮ0_0>) -> Int // user: %10 // function_ref Swift.Int.init (Swift.Int.Type) (_builtinIntegerLiteral : Builtin.Int2048) -> Swift.Int %6 = function_ref @_TFSiCfMSiFT22_builtinIntegerLiteralBi2048__Si : $@convention(thin) (Builtin.Int2048, @thin Int.Type) -> Int // user: %9 %7 = metatype $@thin Int.Type // user: %9 %8 = integer_literal $Builtin.Int2048, 0 // user: %9 %9 = apply %6(%8, %7) : $@convention(thin) (Builtin.Int2048, @thin Int.Type) -> Int // user: %10 %10 = apply %3(%5, %9) : $@convention(thin) (Int, Int) -> Bool // user: %11

Slide 19

Slide 19 text

JT&NQUZ %2 = function_ref @_TFSb21_getBuiltinLogicValuefSbFT_Bi1_ : $@convention(method) (Bool) -> Builtin.Int1 // user: %9 // function_ref static Swift.! prefix (Swift.Bool) -> Swift.Bool %3 = function_ref @_TZFSsop1nFSbSb : $@convention(thin) (Bool) -> Bool // user: %8 %4 = alloc_stack $Array // users: %5, %7, %10 store %0 to %4#1 : $*Array // id: %5 // function_ref ext.Swift.Swift.CollectionType.isEmpty.getter : Swift.Bool %6 = function_ref @_TFeRq_Ss14CollectionType_SsS_g7isEmptySb : $@convention(method) <ŵЮ0_0 where ŵЮ0_0 : CollectionType, ŵЮ0_0.Generator : GeneratorType, ŵЮ0_0.Index : ForwardIndexType, ŵЮ0_0.Index.Distance : _SignedIntegerType, ŵЮ0_0.Index.Distance.IntegerLiteralType : _BuiltinIntegerLiteralConvertible, ŵЮ0_0.SubSequence : Indexable, ŵЮ0_0.SubSequence : SequenceType, ŵЮ0_0.SubSequence.Generator : GeneratorType, ŵЮ0_0.SubSequence.Index : ForwardIndexType, ŵЮ0_0.SubSequence.Index.Distance : _SignedIntegerType, ŵЮ0_0.SubSequence.Index.Distance.IntegerLiteralType : _BuiltinIntegerLiteralConvertible> (@in_guaranteed ŵЮ0_0) -> Bool // user: %7 %7 = apply %6<[Int], IndexingGenerator>, Int, Int, Int, Int, Int, ArraySlice, IndexingGenerator>, Int, Int, Int, Int, Int, ArraySlice, Int, Int>(%4#1) : $@convention(method) <ŵЮ0_0 where ŵЮ0_0 : CollectionType, ŵЮ0_0.Generator : GeneratorType, ŵЮ0_0.Index : ForwardIndexType, ŵЮ0_0.Index.Distance : _SignedIntegerType, ŵЮ0_0.Index.Distance.IntegerLiteralType : _BuiltinIntegerLiteralConvertible, ŵЮ0_0.SubSequence : Indexable, ŵЮ0_0.SubSequence : SequenceType, ŵЮ0_0.SubSequence.Generator : GeneratorType, ŵЮ0_0.SubSequence.Index : ForwardIndexType, ŵЮ0_0.SubSequence.Index.Distance : _SignedIntegerType, ŵЮ0_0.SubSequence.Index.Distance.IntegerLiteralType : _BuiltinIntegerLiteralConvertible> (@in_guaranteed ŵЮ0_0) -> Bool // user: %8 %8 = apply %3(%7) : $@convention(thin) (Bool) -> Bool // user: %9 %9 = apply %2(%8) : $@convention(method) (Bool) -> Builtin.Int1 // user: %11

Slide 20

Slide 20 text

MB[Z

Slide 21

Slide 21 text

MB[Z class SomeViewController: UIViewController { lazy var errImg: UIImage = UIImage(named: "largeFile")! }

Slide 22

Slide 22 text

MB[Z let f = Foo() print(f.a) // 0 f.a = nil print(f.a) // 1 let g = Bar() print(g.a) // Optional(0) g.a = nil print(g.a) // nil class Foo { static var x: Int = 0 lazy var a: Int! = { return x++ }() } class Bar { static var x: Int = 0 lazy var a: Int? = { return x++ }() } SFJOJU

Slide 23

Slide 23 text

MB[ZPQUJPOBM w lazyVTFTPQUJPOBMJOUFSOBMMZ class Foo { static var x: Int = 0 var lazy_a: Optional var a: Int! { get { if self.lazy_a == nil { let v = { … }() self.lazy_a = .Some(.Some(v)) } return self.lazy_a } set { … } } }

Slide 24

Slide 24 text

class Foo { static var x: Int = 0 var lazy_a: Optional var a: Int! { set { switch newValue { case .None: self.lazy_a = nil case .Some(let v): self.lazy_a = .Some(.Some(v)) } } get { … } } } class Bar { static var x: Int = 0 var lazy_a: Optional var a: Int? { set { self.lazy_a = .Some(newValue) } get { … } } }

Slide 25

Slide 25 text

// main.Bar.a.setter : Swift.Optional sil hidden [transparent] @_TFC4main3Bars1bGSqSi_ : $@convention(method) (Optional, %1 : $Bar): debug_value %0 : $Optional // let value // id: %2 debug_value %1 : $Bar // let self // id: %3 %4 = enum $Optional>, #Optional.Some!enumelt.1, %0 : $Optional %5 = ref_element_addr %1 : $Bar, #Bar.a.storage // user: %6 store %4 to %5 : $*Optional> // id: %6 %7 = tuple () // user: %8 return %7 : $() // id: %8 }

Slide 26

Slide 26 text

// main.Foo.a.setter : Swift.ImplicitlyUnwrappedOptional sil hidden [transparent] @_TFC4main3Foos1aGSQSi_ : $@convention(method) (ImplicitlyU bb0(%0 : $ImplicitlyUnwrappedOptional, %1 : $Foo): debug_value %0 : $ImplicitlyUnwrappedOptional // let value // id: %2 debug_value %1 : $Foo // let self // id: %3 %4 = integer_literal $Builtin.Int1, -1 // user: %6 %5 = integer_literal $Builtin.Int1, 0 // user: %6 %6 = select_enum %0 : $ImplicitlyUnwrappedOptional, case #ImplicitlyUnwrappedOptional.Some!enumelt.1: %4, default %5 : $Builtin.Int1 // user: %7 cond_br %6, bb1, bb3 // id: %7 bb1: // Preds: bb0 %8 = unchecked_enum_data %0 : $ImplicitlyUnwrappedOptional, #ImplicitlyUnwrap %9 = enum $ImplicitlyUnwrappedOptional, #ImplicitlyUnwrappedOptional.Some!enu %10 = enum $Optional>, #Optional.Some!enumelt.1, br bb2(%10 : $Optional>) // id: %11 bb2(%12 : $Optional>): // Preds: bb1 bb3 %13 = ref_element_addr %1 : $Foo, #Foo.a.storage // user: %14 store %12 to %13 : $*Optional> // id: %14 %15 = tuple () // user: %16 return %15 : $() // id: %16 bb3: // Preds: bb0 %17 = enum $Optional>, #Optional.None!enumelt // br bb2(%17 : $Optional>) // id: %18 }

Slide 27

Slide 27 text

get { // main.Foo.a.getter let val: Int! if self.lazy_a != nil { // bb0 switch self.lazy_a {// bb1 case .Some(let v): // bb2 val = v // bb5 case .None: // bb3, bb4 fatalError("unexpectedly found nil while unwrapping an Optional value") } } else { // bb6 let v = { return Foo.x++ }() val = .Some(v) let tmp: Optional switch val { case .Some: // bb7 tmp = .Some(.Some(val)) case .None: // bb9 tmp = .None } self.lazy_a = tmp // bb8 } return val // bb10 }

Slide 28

Slide 28 text

3FGFSFODFT w l--7.zJO5IF"SDIJUFDUVSFPG0QFO4PVSDF"QQMJDBUJPOT
 IUUQXXXBPTBCPPLPSHFOMMWNIUNM w 5IJOLJOHJO4XJGU 1BSU** r$PNQJMFS
 IUUQTXXXBDDFMFCSBUFDPNCMPHUIJOLJOHTXJGUQBSUJJ w -B[ZTUPSFEQSPQFSUZʹ͍ͭͯൃද͠·ͨ͠
 IUUQZBTIJHBOJIBUFOBCMPHDPNFOUSZ