SlideShare une entreprise Scribd logo
1  sur  115
Télécharger pour lire hors ligne
A Deeper Deep dive into Swift Literals
freddi from LINE Fukuoka
at try! Swift NYC 2019 (9th.Sep)
About Me
freddi (Yuki Aki) from !
Developing LINE Creators Studio
at LINE Fukuoka
Organizer of HAKATA.swift
- What is a Literal in Computer Programming?
- Why I want you to deep dive into Swift Literals?
- Literals in Swift
- Object of Literals
- Architecture of Type from Literals
- Conclusion
Agenda
High-Level
Low-Level
What is a Literal
in Computer Programming?
What is a Literal in Computer Programming?
What is a Literal in Computer Programming?
Notations for representing a fixed value in source code.
Values we write in a conventional form whose is obvious
// Integer Literal
let assigningIntegerLiteral = 42
// String Literal
let assigningStringLiteral = "try! Swift"
What is a Literal in Computer Programming?
Literal is a minimal way to
represent the actual data for Programmer
What is a Literal in Computer Programming?
Why I want you to
deep-dive into Swift Literals?
Why I want you to deep-dive into Swift Literals?
We have more chances to touch low level Swift
compared to Other Languages
Why I want you to deep-dive into Swift Literals?
But,it is not easy to learn 😩
Why I want you to deep-dive into Swift Literals?
Compiler Code looks too difficult and is too long for reading
Why I want you to deep-dive into Swift Literals?
Why I talk about Literals?
Why I want you to deep-dive into Swift Literals?
Why I talk about Literals?
I learned Swift Compiler things
through understanding deep side of Swift Literals
Why I want you to deep-dive into Swift Literals?
Literals in Swift
Literals in Swift (Basic Literals)
// Integer Literal
let assigningIntegerLiteral = 42
// Float Literal
let assigningFloatLiteral = 10.0
// String Literal
let assigningStringLiteral = "try! Swift"



// Boolean Literal
let assigningBooleanLiteral = true
Representing basic data
- Integer Literal → integer numeric
- Float Literal → floating numeric
- String Literal → sentence
- Boolean Literal → true/false
Literals in Swift (Basic Literals)
Literals in Swift (Basic Literals with nested Type)
// Optional from Nil Literal
let assigningNilLiteral: Int? = nil
// Array Literal
let assigningArrayLiteralWithInt: [Int] = [42]
// Dictionary Literal
let assigningDictLiteral: [String: Int] = [:]
Context of Nested Values is necessary
- Nil Literal → null value
- Array Literal → sequencial value
- Dictionary Literal → value with key
Literals in Swift (Basic Literals with nested Type)
Literals in Swift (Basic Literals with nested Type)
// Not declared what type you want to make optional
❌ let assigningNilLiteral = nil
// Not declared what element type
❌ let assigningArrayLiteral = []
Literals in Swift (Special Literals)
// Color Literal
let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
// Image Literal
let image = #imageLiteral(resourceName: “cat”)
// File Reference Literal
let file = #fileLiteral(resourceName: “cat.txt”)
Swift also provides Special Literals for resources
- Color Literal → color
- Image Literal → image
- File Reference Literal → file location
Literals in Swift (Special Literals)
Literals in Swift (Special Literals)
Color/Image/File Reference Literal are
- rendered as Actual Data in Xcode
- File → select from file manager
- Color → adjusting color code
// Color Literal
let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1)
Literals in Swift (Special Literals)
Literals in Swift (Special Literals at Xcode!)
// Color Literal
let color =
// Color Literal
let color =
Literals in Swift (Special Literals at Xcode!)
// Image Literal
let image = #imageLiteral(resourceName: “cat")
Literals in Swift (Special Literals)
// Image Literal
let image =
Literals in Swift (Special Literals at Xcode!)
// Image Literal
let image =
Literals in Swift (Special Literals at Xcode!)
Literals are a minimal way to represent value by programmer
There are many Literals in Swift,not only usual Literal!
- Integer,Array,Nil,Color etc…
Literals in Swift
Object of Literals
Object of Literals
The Ways of determining Type of value are …
- Type Annotation
- Type Inference with Default Type of Literal
// Integer Literal
// assigningIntegerLiteral is Int
let assigningIntegerLiteral: Int = 42
Literals get Type from annotation after colon(:)
Object of Literals(Type Annotation)
// Integer Literal
// assigningIntegerLiteral is Float
let assigningIntegerLiteral: Float = 42
Literals get Type from annotation after colon(:)
Object of Literals(Type Annotation)
Object of Literals
The Ways of determining Type of value are …
- Type Annotation
- Type Inference with Default Type of Literal
Object of Literals (Type Inference)
// Integer Literal
// assigningIntegerLiteral is Int
let assigningIntegerLiteral = 42
Each Literal has Default Type,can be inferenced
Object of Literals (Special Literals)
// Image Literal
let image = #imageLiteral(resourceName: “cat”)
type(of: image) // UIImage at UIKit, NSImage at AppKit
AppKit UIKit
Image Literal NSImage UIImage
Color Literal NSColor UIColor
Object of Literals
Some Literals are treated as Object
which cannot be used Directly
Swift’s Compiling Flow
42 SIL(Swift Intermediate Language)
Abstract Syntax Tree(AST)
AST With Type information
Swift’s Compiling Flow
42 SIL(Swift Intermediate Language)
Abstract Syntax Tree(AST)
AST With Type information
Front-End of Compiler
Back-End of Compiler
42 SIL(Swift Intermediate Language)
Abstract Syntax Tree(AST)
AST With Type information
Swift’s Compiling Flow
SIL (Swift Intermediate Language)
Intermediate Language between Swift and LLVM IR
- Swift code will be converted SIL after AST
- Some of optimization are operated at SIL
Format of SIL
SIL is conforming Static Single Assignment format
%a = some value 1 ⭕
%b = some value 2 ⭕
%a = some value 3 ❌
Value
- Object is assigned to value at each line
- Assigning to one value is allowed only once
SIL (Swift Intermediate Language)
Why do we need SIL when deep dive into Literals?
SIL (Swift Intermediate Language)
Why do we need SIL when deep dive into Literals?
We can find how Swift Code is treated by Compiler
SIL (Swift Intermediate Language)
Why do we need SIL when deep dive into Literals?
We can find how Swift Code is treated by Compiler
More easy to read SIL than reading compiler code
Let’s read SIL
let assigningLiteral: Int = 42
let assigningLiteral: Int = 42
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Let’s read SIL
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
let assigningLiteral: Int = 42
Let’s read SIL
let assigningLiteral: Int = 42
Allocation of memory to %3
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Let’s read SIL
$Builtin.Int64 object is assigned %4
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
let assigningLiteral: Int = 42
Let’s read SIL
At first,Some Literals are …
%4 = integer_literal $Builtin.Int64, 42
Literal Object
- treated as “Literal Object” starts with $Builtin.***
- $Builtin.*** will be used in Actual Type Initializer
%4 = integer_literal $Builtin.Int64, 42
Literal Object
At first,Some Literals are …
- treated as “Literal Object” starts with $Builtin.***
- $Builtin.*** will be used in Actual Type Initializer
Un-official name!;(
42
$Builtin.Int64
4.2
$Builtin.FPIEEE64
“42”
$Builtin.RawPointer
$Builtin.Word
Integer Float String
Literal Object
42
$Builtin.Int64
4.2
$Builtin.FPIEEE64
“42”
$Builtin.RawPointer
$Builtin.Word
Integer Float String
let value: Float = 42
Literal Object
true
$Builtin.Int1
Boolean Digit after $Buildin.Int means bit-size
Integer
42
$Builtin.Int64
Literal Object
true
$Builtin.Int1
Boolean Digit after $Buildin.Int means bit-size
Integer
42
$Builtin.Int64
Literal Object
#imageLiteral(resourceName: “cat”)
UIImage(named: “cat”)
Image Literal(at import UIKit)
No Literal Object!
Literal Object
$Builtin.Int64 object is assigned %4
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
let assigningLiteral: Int = 42
Let’s read SIL
let assigningLiteral: Int = 42
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Let’s read SIL
$Builtin.Int64 object is assigned %4
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object (%4) is passed to Int Initializer
Let’s read SIL let assigningLiteral: Int = 42
Some Type which can be generated from Literal
Let’s read SIL
- has initializer which receives Literal Object
- Initializer of default Type is used,if type not annotated
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object (%4) is passed to Int Initializer
Let’s read SIL let assigningLiteral: Int = 42
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
The generated Int Object is assigned to allocated place
let assigningLiteral: Int = 42Let’s read SIL
"try! Swift"
Let’s read SIL
"try! Swift"
%2 = string_literal utf8 "try! Swift" // user: %7
%3 = integer_literal $Builtin.Word, 10 // user: %7
%4 = integer_literal $Builtin.Int1, -1 // user: %7
%5 = metatype $@thin String.Type // user: %7
// function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
%6 = function_ref ...
%7 = apply %6(%2, %3, %4, %5) ...
Let’s read SIL(from string literal to String)
%2 = string_literal utf8 "try! Swift" // user: %7
%3 = integer_literal $Builtin.Word, 10 // user: %7
%4 = integer_literal $Builtin.Int1, -1 // user: %7
%5 = metatype $@thin String.Type // user: %7
// function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
%6 = function_ref ...
%7 = apply %6(%2, %3, %4, %5) ...
Let’s read SIL(from string literal to String)
Let’s read SIL(from string literal to String)
%2 = string_literal utf8 "try! Swift" // user: %7
%3 = integer_literal $Builtin.Word, 10 // user: %7
%4 = integer_literal $Builtin.Int1, -1 // user: %7
%2: $Builtin.RawPointer // pointer of string
%4: $Builtin.Int1 // flag string is Ascii or not
%3: $Builtin.Word // length of data on memory
%2 = string_literal utf8 "try! Swift" // user: %7
%3 = integer_literal $Builtin.Word, 10 // user: %7
%4 = integer_literal $Builtin.Int1, -1 // user: %7
%5 = metatype $@thin String.Type // user: %7
// function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
%6 = function_ref ...
%7 = apply %6(%2, %3, %4, %5) ...
Let’s read SIL(from string literal to String)
// function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:)
%6 = function_ref ...
%7 = apply %6(%2, %3, %4, %5) ...
Let’s read SIL(from string literal to String)
Swift Literals are treated as Literal Object at first
- Literal Object will be used for Initializers of actual Swift Type
Object of Literals
%5 = struct $Int (%4 : $Builtin.Int64)
Architecture of Type
from Literals
Literal Object
At first,Some Literals are treated as “Literal Object”
42
$Builtin.Int64
4.2
$Builtin.FPIEEE64
“42”
$Builtin.RawPointer
$Builtin.Word
Integer Float String
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object is passed to Int Initializer
Initializer which receives Literal Object
Implementation of this initializer is required by
_ExpressibleByBuiltin***Literal protocol
%5 = struct $Int (%4 : $Builtin.Int64)
Initializer which receives Literal Object
What is _ExpressibleByBuiltin***Literal protocol?
_ExpressibleByBuiltin***Literal
What is _ExpressibleByBuiltin***Literal protocol?
_ExpressibleByBuiltin***Literal
public protocol _ExpressibleByBuiltinIntegerLiteral {
init(_builtinIntegerLiteral value: Builtin.IntLiteral)
}
init(:Builtin.IntLiteral) is called on assigning Literal by =
- Type which conforming with this protocol is convertible by Literal Object
_ExpressibleByBuiltin***Literal
init(:Builtin.IntLiteral) is called on assigning Literal by =
%5 = struct $Int (%4 : $Builtin.Int64)
What is _ExpressibleByBuiltin***Literal protocol?
- Type which conforming with this protocol is convertible by Literal Object
ExpressibleBy***Literal
You may know ExpressibleBy***Literal protocol
- makes Type be convertible by each Literal,using =
- ExpressibleByIntegerLiteral
- ExpressibleByFloatLiteral
- ExpressibleByBooleanLiteral …
struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}


let value: OriginalIntegerType = 10
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}


let value: OriginalIntegerType = 10
1.Set typealias to IntegerLiteralType
the type should be converted from Integer Literal
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}


let value: OriginalIntegerType = 10
2.Implement Initializer
which receives IntegerLiteralType value
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}


let value: OriginalIntegerType = 10
You can initialize your type by Literal!
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
ExpressibleBy***Literal
public struct CGFloat {
#if arch(i386) || arch(arm)
public typealias NativeType = Float

#elseif arch(x86_64) || arch(arm64) || …
public typealias NativeType = Double
...

typealias FloatLiteralType = NativeType
CGFloat is using ExpressibleByFloatLiteral
struct OriginalIntegerType: ExpressibleByIntegerLiteral {
typealias IntegerLiteralType = Int
init(integerLiteral value: IntegerLiteralType) {}
}


let value: OriginalIntegerType = 10
ExpressibleBy***Literal
How to make Original Type convertible from Literal?
1.Set typealias to IntegerLiteralType
the type should be converted from Integer Literal
Associated Type in ExpressibleBy***Literal should conform
with _ExpressibleByBuiltin***Literal protocol
public protocol ExpressibleByIntegerLiteral {
associatedtype IntegerLiteralType: _ExpressibleByBuiltinIntegerLiteral
ExpressibleBy***Literal
Flow of Converting Literal
Literal Object ( = Builtin.***)
Swift Type
Literal
Swift Type
Literal Object ( = Builtin.***)
Swift Type: _ExpressibleByBuiltin***
Swift Type: ExpressibleBy***
Literal
Optional →
Flow of Converting Literal
_ExpressibleByBuiltin***Literal
Please note that …
You cannot use _ExpressibleByBuiltin***Literal protocol
in your usual Swift Code 🙃 🙃 🙃
$Builtin.Int64 $Builtin.FPIEEE64
What is “Literal Object” ??? 🤔
$Builtin.RawPointer
$Builtin.Word
Literal Object
Literal Object
Implementation of this initializer is required by
_ExpressibleByBuiltin***Literal protocol
%5 = struct $Int (%4 : $Builtin.Int64)
Where will passed Literal Object go?
%5 = struct $Int (%4 : $Builtin.Int64)
Literal Object
public struct Int : …… {

public var _value: Builtin.Int64
Literal Object will be stored in Some Swift Types from Literal
Literal Object
// static Int.== infix(_:_:)
sil public_external ... (Int, Int, @thin Int.Type) -> Bool {
...
%3 = struct_extract %0 : $Int, #Int._value // user: %5
%4 = struct_extract %1 : $Int, #Int._value // user: %5
%5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ...
%6 = struct $Bool (%5 : $Builtin.Int1) // user: %7
public var _value: Builtin.Int64
== operator on Int
Literal Object
// static Int.== infix(_:_:)
sil public_external ... (Int, Int, @thin Int.Type) -> Bool {
...
%3 = struct_extract %0 : $Int, #Int._value // user: %5
%4 = struct_extract %1 : $Int, #Int._value // user: %5
%5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ...
%6 = struct $Bool (%5 : $Builtin.Int1) // user: %7
public var _value: Builtin.Int64
1.Getting _value from left value and right value
Literal Object
== operator on Int
// static Int.== infix(_:_:)
sil public_external ... (Int, Int, @thin Int.Type) -> Bool {
...
%3 = struct_extract %0 : $Int, #Int._value // user: %5
%4 = struct_extract %1 : $Int, #Int._value // user: %5
%5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ...
%6 = struct $Bool (%5 : $Builtin.Int1) // user: %7
public var _value: Builtin.Int64
2. Passing each _value to Back-End function
Literal Object
== operator on Int
Swift Type from Literal
Swift’s Compiling Flow
Front-End of Compiler
Back-End of Compiler
$Builtin.*** Back-End Object
// static Int.== infix(_:_:)
sil public_external ... (Int, Int, @thin Int.Type) -> Bool {
...
%3 = struct_extract %0 : $Int, #Int._value // user: %5
%4 = struct_extract %1 : $Int, #Int._value // user: %5
%5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ...
%6 = struct $Bool (%5 : $Builtin.Int1) // user: %7
public var _value: Builtin.Int64
3.Return result as Bool in Swift
Literal Object
== operator on Int
Swift Type
Literal Object ( = Builtin.***)
Swift Type: _ExpressibleByBuiltin***
Swift Type: ExpressibleBy***
Literal
Flow of Converting Literal
Literal
Swift Type: ExpressibleBy***
Swift Type: _ExpressibleByBuiltin***
Literal Object ( = Builtin.***)
Architecture of Type from Literals
Int, String …
CGFloat …
Operators(+,-,*,/,== …)
Operators(+,-,*,/,== …)
Architecture of Type from Literals
Back-End Function
ift Type: ExpressibleBy***
ype: _ExpressibleByBuiltin***
eral Object ( = Builtin.***)
Swift Ty
Swift Type:
Literal O
Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
Architecture of Type from Literals
$Builtin.Int64
Int UInt
Architecture of Type from Literals
Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
Architecture of Type from Literals
Some Types generated from Literals are wrappers of Literal Object
Literal Object represents object in Back-End
Architecture of Type from Literals
Swift Type from Literal
$Builtin.*** Back-End Object
Conclusion
Literals in Swift
Literals are a minimal way to represent value by programmer
There are many Literals in Swift,not only usual Literal!
- Integer,Array,Nil,Color etc…
Literal Object
At first,Some Literals are treated as “Literal Object”
42
$Builtin.Int64
4.2
$Builtin.FPIEEE64
“42”
$Builtin.RawPointer
$Builtin.Word
Integer Float String
%3 = global_addr @$s4test16assigningLiteralSivp : $*Int
%4 = integer_literal $Builtin.Int64, 42
%5 = struct $Int (%4 : $Builtin.Int64)
store %5 to %3 : $*Int
Literal Object is passed to Int Initializer
Initializer which receives Literal Object
Architecture of Type from Literals
Literal
Swift Type: ExpressibleBy***
Swift Type: _ExpressibleByBuiltin***
Literal Object ( = Builtin.***)
Int, String …
CGFloat …
For more your understanding
https://github.com/apple/swift
For more your understanding
https://www.youtube.com/watch?v=sT0SNp-Tw-8
For more your understanding
Swiftckaigi
19th November 2019@Japan
For more your understanding
Thank you for listening!

Contenu connexe

Similaire à A Deeper Deep Dive into Swift Literal

Transpilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing HydraTranspilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing HydraJoshua Shinavier
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptEPAM Systems
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless Jared Roesch
 
Compiler2016 by abcdabcd987
Compiler2016 by abcdabcd987Compiler2016 by abcdabcd987
Compiler2016 by abcdabcd987乐群 陈
 
Kotlin what_you_need_to_know-converted event 4 with nigerians
Kotlin  what_you_need_to_know-converted event 4 with nigeriansKotlin  what_you_need_to_know-converted event 4 with nigerians
Kotlin what_you_need_to_know-converted event 4 with nigeriansjunaidhasan17
 
Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Karthik Srini B R
 
SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first timeYusuke Kita
 
2 programming with c# i
2 programming with c# i2 programming with c# i
2 programming with c# isiragezeynu
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)Shoaib Ghachi
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin XPeppers
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending PythonPriyank Kapadia
 
Build a compiler using C#, Irony and RunSharp.
Build a compiler using C#, Irony and RunSharp.Build a compiler using C#, Irony and RunSharp.
Build a compiler using C#, Irony and RunSharp.James Curran
 
掀起 Swift 的面紗
掀起 Swift 的面紗掀起 Swift 的面紗
掀起 Swift 的面紗Pofat Tseng
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, SwiftYandex
 

Similaire à A Deeper Deep Dive into Swift Literal (20)

Transpilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing HydraTranspilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing Hydra
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
 
Demystifying Shapeless
Demystifying Shapeless Demystifying Shapeless
Demystifying Shapeless
 
Compiler2016 by abcdabcd987
Compiler2016 by abcdabcd987Compiler2016 by abcdabcd987
Compiler2016 by abcdabcd987
 
C Course Material0209
C Course Material0209C Course Material0209
C Course Material0209
 
Kotlin what_you_need_to_know-converted event 4 with nigerians
Kotlin  what_you_need_to_know-converted event 4 with nigeriansKotlin  what_you_need_to_know-converted event 4 with nigerians
Kotlin what_you_need_to_know-converted event 4 with nigerians
 
Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1Fundamentals of Computing and C Programming - Part 1
Fundamentals of Computing and C Programming - Part 1
 
SIL for the first time
SIL for the first timeSIL for the first time
SIL for the first time
 
2 programming with c# i
2 programming with c# i2 programming with c# i
2 programming with c# i
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
Let's talk swift - an introduction
Let's talk swift - an introductionLet's talk swift - an introduction
Let's talk swift - an introduction
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
A quick and fast intro to Kotlin
A quick and fast intro to Kotlin A quick and fast intro to Kotlin
A quick and fast intro to Kotlin
 
python-ch2.pptx
python-ch2.pptxpython-ch2.pptx
python-ch2.pptx
 
C Types - Extending Python
C Types - Extending PythonC Types - Extending Python
C Types - Extending Python
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Build a compiler using C#, Irony and RunSharp.
Build a compiler using C#, Irony and RunSharp.Build a compiler using C#, Irony and RunSharp.
Build a compiler using C#, Irony and RunSharp.
 
掀起 Swift 的面紗
掀起 Swift 的面紗掀起 Swift 的面紗
掀起 Swift 的面紗
 
Unit 2 python
Unit 2 pythonUnit 2 python
Unit 2 python
 
Denis Lebedev, Swift
Denis  Lebedev, SwiftDenis  Lebedev, Swift
Denis Lebedev, Swift
 

Plus de 秋 勇紀

For Developing URL Routing of SwiftUI App
For Developing URL Routing of SwiftUI AppFor Developing URL Routing of SwiftUI App
For Developing URL Routing of SwiftUI App秋 勇紀
 
Tutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer PassTutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer Pass秋 勇紀
 
What Swifty is, from Enum
What Swifty is, from EnumWhat Swifty is, from Enum
What Swifty is, from Enum秋 勇紀
 
SILOptimizerのCode Reading入門
SILOptimizerのCode Reading入門SILOptimizerのCode Reading入門
SILOptimizerのCode Reading入門秋 勇紀
 
自作コンパイラのお話
自作コンパイラのお話自作コンパイラのお話
自作コンパイラのお話秋 勇紀
 
ディープラーニングでポプテピピック
ディープラーニングでポプテピピックディープラーニングでポプテピピック
ディープラーニングでポプテピピック秋 勇紀
 

Plus de 秋 勇紀 (6)

For Developing URL Routing of SwiftUI App
For Developing URL Routing of SwiftUI AppFor Developing URL Routing of SwiftUI App
For Developing URL Routing of SwiftUI App
 
Tutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer PassTutorial for developing SILOptimizer Pass
Tutorial for developing SILOptimizer Pass
 
What Swifty is, from Enum
What Swifty is, from EnumWhat Swifty is, from Enum
What Swifty is, from Enum
 
SILOptimizerのCode Reading入門
SILOptimizerのCode Reading入門SILOptimizerのCode Reading入門
SILOptimizerのCode Reading入門
 
自作コンパイラのお話
自作コンパイラのお話自作コンパイラのお話
自作コンパイラのお話
 
ディープラーニングでポプテピピック
ディープラーニングでポプテピピックディープラーニングでポプテピピック
ディープラーニングでポプテピピック
 

Dernier

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 

Dernier (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 

A Deeper Deep Dive into Swift Literal

  • 1. A Deeper Deep dive into Swift Literals freddi from LINE Fukuoka at try! Swift NYC 2019 (9th.Sep)
  • 2. About Me freddi (Yuki Aki) from ! Developing LINE Creators Studio at LINE Fukuoka Organizer of HAKATA.swift
  • 3. - What is a Literal in Computer Programming? - Why I want you to deep dive into Swift Literals? - Literals in Swift - Object of Literals - Architecture of Type from Literals - Conclusion Agenda High-Level Low-Level
  • 4. What is a Literal in Computer Programming?
  • 5. What is a Literal in Computer Programming? What is a Literal in Computer Programming? Notations for representing a fixed value in source code. Values we write in a conventional form whose is obvious
  • 6. // Integer Literal let assigningIntegerLiteral = 42 // String Literal let assigningStringLiteral = "try! Swift" What is a Literal in Computer Programming?
  • 7. Literal is a minimal way to represent the actual data for Programmer What is a Literal in Computer Programming?
  • 8. Why I want you to deep-dive into Swift Literals?
  • 9. Why I want you to deep-dive into Swift Literals? We have more chances to touch low level Swift compared to Other Languages
  • 10. Why I want you to deep-dive into Swift Literals?
  • 11. But,it is not easy to learn 😩 Why I want you to deep-dive into Swift Literals?
  • 12. Compiler Code looks too difficult and is too long for reading Why I want you to deep-dive into Swift Literals?
  • 13. Why I talk about Literals? Why I want you to deep-dive into Swift Literals?
  • 14. Why I talk about Literals? I learned Swift Compiler things through understanding deep side of Swift Literals Why I want you to deep-dive into Swift Literals?
  • 16. Literals in Swift (Basic Literals) // Integer Literal let assigningIntegerLiteral = 42 // Float Literal let assigningFloatLiteral = 10.0 // String Literal let assigningStringLiteral = "try! Swift"
 
 // Boolean Literal let assigningBooleanLiteral = true
  • 17. Representing basic data - Integer Literal → integer numeric - Float Literal → floating numeric - String Literal → sentence - Boolean Literal → true/false Literals in Swift (Basic Literals)
  • 18. Literals in Swift (Basic Literals with nested Type) // Optional from Nil Literal let assigningNilLiteral: Int? = nil // Array Literal let assigningArrayLiteralWithInt: [Int] = [42] // Dictionary Literal let assigningDictLiteral: [String: Int] = [:]
  • 19. Context of Nested Values is necessary - Nil Literal → null value - Array Literal → sequencial value - Dictionary Literal → value with key Literals in Swift (Basic Literals with nested Type)
  • 20. Literals in Swift (Basic Literals with nested Type) // Not declared what type you want to make optional ❌ let assigningNilLiteral = nil // Not declared what element type ❌ let assigningArrayLiteral = []
  • 21. Literals in Swift (Special Literals) // Color Literal let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1) // Image Literal let image = #imageLiteral(resourceName: “cat”) // File Reference Literal let file = #fileLiteral(resourceName: “cat.txt”)
  • 22. Swift also provides Special Literals for resources - Color Literal → color - Image Literal → image - File Reference Literal → file location Literals in Swift (Special Literals)
  • 23. Literals in Swift (Special Literals) Color/Image/File Reference Literal are - rendered as Actual Data in Xcode - File → select from file manager - Color → adjusting color code
  • 24. // Color Literal let color = #colorLiteral(red: 1, green: 0, blue: 0, alpha: 1) Literals in Swift (Special Literals)
  • 25. Literals in Swift (Special Literals at Xcode!) // Color Literal let color =
  • 26. // Color Literal let color = Literals in Swift (Special Literals at Xcode!)
  • 27. // Image Literal let image = #imageLiteral(resourceName: “cat") Literals in Swift (Special Literals)
  • 28. // Image Literal let image = Literals in Swift (Special Literals at Xcode!)
  • 29. // Image Literal let image = Literals in Swift (Special Literals at Xcode!)
  • 30. Literals are a minimal way to represent value by programmer There are many Literals in Swift,not only usual Literal! - Integer,Array,Nil,Color etc… Literals in Swift
  • 32. Object of Literals The Ways of determining Type of value are … - Type Annotation - Type Inference with Default Type of Literal
  • 33. // Integer Literal // assigningIntegerLiteral is Int let assigningIntegerLiteral: Int = 42 Literals get Type from annotation after colon(:) Object of Literals(Type Annotation)
  • 34. // Integer Literal // assigningIntegerLiteral is Float let assigningIntegerLiteral: Float = 42 Literals get Type from annotation after colon(:) Object of Literals(Type Annotation)
  • 35. Object of Literals The Ways of determining Type of value are … - Type Annotation - Type Inference with Default Type of Literal
  • 36. Object of Literals (Type Inference) // Integer Literal // assigningIntegerLiteral is Int let assigningIntegerLiteral = 42 Each Literal has Default Type,can be inferenced
  • 37. Object of Literals (Special Literals) // Image Literal let image = #imageLiteral(resourceName: “cat”) type(of: image) // UIImage at UIKit, NSImage at AppKit AppKit UIKit Image Literal NSImage UIImage Color Literal NSColor UIColor
  • 38. Object of Literals Some Literals are treated as Object which cannot be used Directly
  • 39. Swift’s Compiling Flow 42 SIL(Swift Intermediate Language) Abstract Syntax Tree(AST) AST With Type information
  • 40. Swift’s Compiling Flow 42 SIL(Swift Intermediate Language) Abstract Syntax Tree(AST) AST With Type information Front-End of Compiler Back-End of Compiler
  • 41. 42 SIL(Swift Intermediate Language) Abstract Syntax Tree(AST) AST With Type information Swift’s Compiling Flow
  • 42. SIL (Swift Intermediate Language) Intermediate Language between Swift and LLVM IR - Swift code will be converted SIL after AST - Some of optimization are operated at SIL
  • 43. Format of SIL SIL is conforming Static Single Assignment format %a = some value 1 ⭕ %b = some value 2 ⭕ %a = some value 3 ❌ Value - Object is assigned to value at each line - Assigning to one value is allowed only once
  • 44. SIL (Swift Intermediate Language) Why do we need SIL when deep dive into Literals?
  • 45. SIL (Swift Intermediate Language) Why do we need SIL when deep dive into Literals? We can find how Swift Code is treated by Compiler
  • 46. SIL (Swift Intermediate Language) Why do we need SIL when deep dive into Literals? We can find how Swift Code is treated by Compiler More easy to read SIL than reading compiler code
  • 47. Let’s read SIL let assigningLiteral: Int = 42
  • 48. let assigningLiteral: Int = 42 %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int Let’s read SIL
  • 49. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int let assigningLiteral: Int = 42 Let’s read SIL
  • 50. let assigningLiteral: Int = 42 Allocation of memory to %3 %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int Let’s read SIL
  • 51. $Builtin.Int64 object is assigned %4 %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int let assigningLiteral: Int = 42 Let’s read SIL
  • 52. At first,Some Literals are … %4 = integer_literal $Builtin.Int64, 42 Literal Object - treated as “Literal Object” starts with $Builtin.*** - $Builtin.*** will be used in Actual Type Initializer
  • 53. %4 = integer_literal $Builtin.Int64, 42 Literal Object At first,Some Literals are … - treated as “Literal Object” starts with $Builtin.*** - $Builtin.*** will be used in Actual Type Initializer Un-official name!;(
  • 56. true $Builtin.Int1 Boolean Digit after $Buildin.Int means bit-size Integer 42 $Builtin.Int64 Literal Object
  • 57. true $Builtin.Int1 Boolean Digit after $Buildin.Int means bit-size Integer 42 $Builtin.Int64 Literal Object
  • 58. #imageLiteral(resourceName: “cat”) UIImage(named: “cat”) Image Literal(at import UIKit) No Literal Object! Literal Object
  • 59. $Builtin.Int64 object is assigned %4 %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int let assigningLiteral: Int = 42 Let’s read SIL
  • 60. let assigningLiteral: Int = 42 %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int Let’s read SIL $Builtin.Int64 object is assigned %4
  • 61. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int Literal Object (%4) is passed to Int Initializer Let’s read SIL let assigningLiteral: Int = 42
  • 62. Some Type which can be generated from Literal Let’s read SIL - has initializer which receives Literal Object - Initializer of default Type is used,if type not annotated
  • 63. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int Literal Object (%4) is passed to Int Initializer Let’s read SIL let assigningLiteral: Int = 42
  • 64. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int The generated Int Object is assigned to allocated place let assigningLiteral: Int = 42Let’s read SIL
  • 66. "try! Swift" %2 = string_literal utf8 "try! Swift" // user: %7 %3 = integer_literal $Builtin.Word, 10 // user: %7 %4 = integer_literal $Builtin.Int1, -1 // user: %7 %5 = metatype $@thin String.Type // user: %7 // function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:) %6 = function_ref ... %7 = apply %6(%2, %3, %4, %5) ... Let’s read SIL(from string literal to String)
  • 67. %2 = string_literal utf8 "try! Swift" // user: %7 %3 = integer_literal $Builtin.Word, 10 // user: %7 %4 = integer_literal $Builtin.Int1, -1 // user: %7 %5 = metatype $@thin String.Type // user: %7 // function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:) %6 = function_ref ... %7 = apply %6(%2, %3, %4, %5) ... Let’s read SIL(from string literal to String)
  • 68. Let’s read SIL(from string literal to String) %2 = string_literal utf8 "try! Swift" // user: %7 %3 = integer_literal $Builtin.Word, 10 // user: %7 %4 = integer_literal $Builtin.Int1, -1 // user: %7 %2: $Builtin.RawPointer // pointer of string %4: $Builtin.Int1 // flag string is Ascii or not %3: $Builtin.Word // length of data on memory
  • 69. %2 = string_literal utf8 "try! Swift" // user: %7 %3 = integer_literal $Builtin.Word, 10 // user: %7 %4 = integer_literal $Builtin.Int1, -1 // user: %7 %5 = metatype $@thin String.Type // user: %7 // function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:) %6 = function_ref ... %7 = apply %6(%2, %3, %4, %5) ... Let’s read SIL(from string literal to String)
  • 70. // function_ref String.init(_builtinStringLiteral:utf8CodeUnitCount:isASCII:) %6 = function_ref ... %7 = apply %6(%2, %3, %4, %5) ... Let’s read SIL(from string literal to String)
  • 71. Swift Literals are treated as Literal Object at first - Literal Object will be used for Initializers of actual Swift Type Object of Literals %5 = struct $Int (%4 : $Builtin.Int64)
  • 73. Literal Object At first,Some Literals are treated as “Literal Object” 42 $Builtin.Int64 4.2 $Builtin.FPIEEE64 “42” $Builtin.RawPointer $Builtin.Word Integer Float String
  • 74. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int Literal Object is passed to Int Initializer Initializer which receives Literal Object
  • 75. Implementation of this initializer is required by _ExpressibleByBuiltin***Literal protocol %5 = struct $Int (%4 : $Builtin.Int64) Initializer which receives Literal Object
  • 76. What is _ExpressibleByBuiltin***Literal protocol? _ExpressibleByBuiltin***Literal
  • 77. What is _ExpressibleByBuiltin***Literal protocol? _ExpressibleByBuiltin***Literal public protocol _ExpressibleByBuiltinIntegerLiteral { init(_builtinIntegerLiteral value: Builtin.IntLiteral) } init(:Builtin.IntLiteral) is called on assigning Literal by = - Type which conforming with this protocol is convertible by Literal Object
  • 78. _ExpressibleByBuiltin***Literal init(:Builtin.IntLiteral) is called on assigning Literal by = %5 = struct $Int (%4 : $Builtin.Int64) What is _ExpressibleByBuiltin***Literal protocol? - Type which conforming with this protocol is convertible by Literal Object
  • 79. ExpressibleBy***Literal You may know ExpressibleBy***Literal protocol - makes Type be convertible by each Literal,using = - ExpressibleByIntegerLiteral - ExpressibleByFloatLiteral - ExpressibleByBooleanLiteral …
  • 80. struct OriginalIntegerType: ExpressibleByIntegerLiteral { typealias IntegerLiteralType = Int init(integerLiteral value: IntegerLiteralType) {} } 
 let value: OriginalIntegerType = 10 ExpressibleBy***Literal How to make Original Type convertible from Literal?
  • 81. struct OriginalIntegerType: ExpressibleByIntegerLiteral { typealias IntegerLiteralType = Int init(integerLiteral value: IntegerLiteralType) {} } 
 let value: OriginalIntegerType = 10 1.Set typealias to IntegerLiteralType the type should be converted from Integer Literal ExpressibleBy***Literal How to make Original Type convertible from Literal?
  • 82. struct OriginalIntegerType: ExpressibleByIntegerLiteral { typealias IntegerLiteralType = Int init(integerLiteral value: IntegerLiteralType) {} } 
 let value: OriginalIntegerType = 10 2.Implement Initializer which receives IntegerLiteralType value ExpressibleBy***Literal How to make Original Type convertible from Literal?
  • 83. struct OriginalIntegerType: ExpressibleByIntegerLiteral { typealias IntegerLiteralType = Int init(integerLiteral value: IntegerLiteralType) {} } 
 let value: OriginalIntegerType = 10 You can initialize your type by Literal! ExpressibleBy***Literal How to make Original Type convertible from Literal?
  • 84. ExpressibleBy***Literal public struct CGFloat { #if arch(i386) || arch(arm) public typealias NativeType = Float
 #elseif arch(x86_64) || arch(arm64) || … public typealias NativeType = Double ...
 typealias FloatLiteralType = NativeType CGFloat is using ExpressibleByFloatLiteral
  • 85. struct OriginalIntegerType: ExpressibleByIntegerLiteral { typealias IntegerLiteralType = Int init(integerLiteral value: IntegerLiteralType) {} } 
 let value: OriginalIntegerType = 10 ExpressibleBy***Literal How to make Original Type convertible from Literal? 1.Set typealias to IntegerLiteralType the type should be converted from Integer Literal
  • 86. Associated Type in ExpressibleBy***Literal should conform with _ExpressibleByBuiltin***Literal protocol public protocol ExpressibleByIntegerLiteral { associatedtype IntegerLiteralType: _ExpressibleByBuiltinIntegerLiteral ExpressibleBy***Literal
  • 87. Flow of Converting Literal Literal Object ( = Builtin.***) Swift Type Literal
  • 88. Swift Type Literal Object ( = Builtin.***) Swift Type: _ExpressibleByBuiltin*** Swift Type: ExpressibleBy*** Literal Optional → Flow of Converting Literal
  • 89. _ExpressibleByBuiltin***Literal Please note that … You cannot use _ExpressibleByBuiltin***Literal protocol in your usual Swift Code 🙃 🙃 🙃
  • 90. $Builtin.Int64 $Builtin.FPIEEE64 What is “Literal Object” ??? 🤔 $Builtin.RawPointer $Builtin.Word Literal Object
  • 91. Literal Object Implementation of this initializer is required by _ExpressibleByBuiltin***Literal protocol %5 = struct $Int (%4 : $Builtin.Int64)
  • 92. Where will passed Literal Object go? %5 = struct $Int (%4 : $Builtin.Int64) Literal Object
  • 93. public struct Int : …… {
 public var _value: Builtin.Int64 Literal Object will be stored in Some Swift Types from Literal Literal Object
  • 94. // static Int.== infix(_:_:) sil public_external ... (Int, Int, @thin Int.Type) -> Bool { ... %3 = struct_extract %0 : $Int, #Int._value // user: %5 %4 = struct_extract %1 : $Int, #Int._value // user: %5 %5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ... %6 = struct $Bool (%5 : $Builtin.Int1) // user: %7 public var _value: Builtin.Int64 == operator on Int Literal Object
  • 95. // static Int.== infix(_:_:) sil public_external ... (Int, Int, @thin Int.Type) -> Bool { ... %3 = struct_extract %0 : $Int, #Int._value // user: %5 %4 = struct_extract %1 : $Int, #Int._value // user: %5 %5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ... %6 = struct $Bool (%5 : $Builtin.Int1) // user: %7 public var _value: Builtin.Int64 1.Getting _value from left value and right value Literal Object == operator on Int
  • 96. // static Int.== infix(_:_:) sil public_external ... (Int, Int, @thin Int.Type) -> Bool { ... %3 = struct_extract %0 : $Int, #Int._value // user: %5 %4 = struct_extract %1 : $Int, #Int._value // user: %5 %5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ... %6 = struct $Bool (%5 : $Builtin.Int1) // user: %7 public var _value: Builtin.Int64 2. Passing each _value to Back-End function Literal Object == operator on Int
  • 97. Swift Type from Literal Swift’s Compiling Flow Front-End of Compiler Back-End of Compiler $Builtin.*** Back-End Object
  • 98. // static Int.== infix(_:_:) sil public_external ... (Int, Int, @thin Int.Type) -> Bool { ... %3 = struct_extract %0 : $Int, #Int._value // user: %5 %4 = struct_extract %1 : $Int, #Int._value // user: %5 %5 = builtin "cmp_eq_Int64"(%3 : $Builtin.Int64, %4 : $Builtin.Int64) ... %6 = struct $Bool (%5 : $Builtin.Int1) // user: %7 public var _value: Builtin.Int64 3.Return result as Bool in Swift Literal Object == operator on Int
  • 99. Swift Type Literal Object ( = Builtin.***) Swift Type: _ExpressibleByBuiltin*** Swift Type: ExpressibleBy*** Literal Flow of Converting Literal
  • 100. Literal Swift Type: ExpressibleBy*** Swift Type: _ExpressibleByBuiltin*** Literal Object ( = Builtin.***) Architecture of Type from Literals Int, String … CGFloat …
  • 101. Operators(+,-,*,/,== …) Operators(+,-,*,/,== …) Architecture of Type from Literals Back-End Function ift Type: ExpressibleBy*** ype: _ExpressibleByBuiltin*** eral Object ( = Builtin.***) Swift Ty Swift Type: Literal O
  • 102. Some Types generated from Literals are wrappers of Literal Object Literal Object represents object in Back-End Architecture of Type from Literals
  • 103. $Builtin.Int64 Int UInt Architecture of Type from Literals Some Types generated from Literals are wrappers of Literal Object Literal Object represents object in Back-End
  • 104. Some Types generated from Literals are wrappers of Literal Object Literal Object represents object in Back-End Architecture of Type from Literals
  • 105. Some Types generated from Literals are wrappers of Literal Object Literal Object represents object in Back-End Architecture of Type from Literals Swift Type from Literal $Builtin.*** Back-End Object
  • 107. Literals in Swift Literals are a minimal way to represent value by programmer There are many Literals in Swift,not only usual Literal! - Integer,Array,Nil,Color etc…
  • 108. Literal Object At first,Some Literals are treated as “Literal Object” 42 $Builtin.Int64 4.2 $Builtin.FPIEEE64 “42” $Builtin.RawPointer $Builtin.Word Integer Float String
  • 109. %3 = global_addr @$s4test16assigningLiteralSivp : $*Int %4 = integer_literal $Builtin.Int64, 42 %5 = struct $Int (%4 : $Builtin.Int64) store %5 to %3 : $*Int Literal Object is passed to Int Initializer Initializer which receives Literal Object
  • 110. Architecture of Type from Literals Literal Swift Type: ExpressibleBy*** Swift Type: _ExpressibleByBuiltin*** Literal Object ( = Builtin.***) Int, String … CGFloat …
  • 111. For more your understanding
  • 114. Swiftckaigi 19th November 2019@Japan For more your understanding
  • 115. Thank you for listening!