6. LLVM
Low-Level Virtual Machine
An Infrastructure for Multi-stage Optimization
− by Chris Arthur Lattner @2002
Design and Implementation of a compiler infrastructure
− support a unique multi-stage optimization system
− support inter-procedural and profile-driven optimizations
LLVM virtual instruction (IR)
− with high-level type information
Sponsored by APPLE
12. Objective-C Type System
Static Type + Duck Type System
− C-style static type
− Smalltalk-style duck type
Duck Type History
− a message to comp.lang.python Newsgroup (2000)
Don't check whether it IS-a duck: check whether it QUACKS-like-a
duck, WALKS-like-a duck, etc, etc, depending on exactly what subset
of duck-like behaviour you need to play your language-games with.
“
”
13. Swift Type System
Bi-directional type inference
Constraint-based type checker
− Based classical Hindley-Milner type system
− Include polymorphic types & function overloading
− Step : Generate ➔ Solve ➔ Solution Application
func foo(x: Double) -> Int { … }
var doubleValue : Double = 3.141592
var unknown = foo(doubleValue)
func bar<T>(x: T) -> T { return x }
var floatValue: Float = -bar(1.414)
14. “The Principal Type-Scheme of an Object in
Combinatory Logic” Hindley, J. Roger (1969)
− Describe what types an expression can have
− Present an algorithm actually computing a type.
Deductive System
First implemented as part of the system of ML.
Hindley-Milner Type System
A, A ➔ B
B
(rule)
15. Hindley-Milner Type System
Expression
e = x
| e1 e2
| 𝞴x . e
| let x=e1 in e2
Types
mono 𝞃 = 𝝰
| D𝞃…𝞃
poly 𝞂 = 𝞃
| ∀𝝰.𝞂
variable
application
abstraction
variable
application
quantifier
Syntax
Context 𝛤 = 𝛜
| 𝛤, x : 𝝰
Typing = 𝛤 ⊢e : 𝞂
17. Type Checker - Constraints
0PT HS x
TAS OD r
NMUD RHNM r q DPT HS RTAS OD
NMRS T SHNM s r q
DLAD c% h
NMEN L M D p
GD DC RS R q
-OO H A D ETM SHNM d
UD N C AHMCHMF y
RR
NMITMSHNM /HRITM SHNM a i p i
18. Constraint Solving
Take a given set of constraints
Determine the most specific type binding for each of the type
variables
The Design of the solver
− Simplification : Relational, Member constraints
− Strategies : solver scope, overload selection
− Comparing solutions : choose solution with smaller scores
“more specific” type
the super-type of overload binds
23. enumeration type
_ p r q
− _l u q p
RG A D O NSN N
0PT S A D O NSN N
public protocol Hashable : Equatable {
var hashValue: Int { get }
}
public protocol Equatable {
@warn_unused_result
func == (lhs: Self, rhs: Self) -> Bool
}
24. enum type example
enum PenModels {
case BallPen
case NamePen
}
// enum.PenModels.hashValue.getter : Swift.Int
@_TFO4enum9PenModelsg9hashValueSi : $@convention(method) (PenModels) -> Int {
%2 = alloc_box($Int, var, name "index")
%3 = mark_uninitialized([var] %2#1 : $*Int)
switch_enum %0 : $PenModels,
case #PenModels.BallPen!enumelt: bb1,
case #PenModels.NamePen!enumelt: bb2
bb1:
%6 = metatype($@thin Int.Type)
%7 = integer_literal($Builtin.Int2048, 0)
%8 = Swift.Int.init(%7, %6) // (Builtin.Int2048, @thin Int.Type) -> Int
assign(%8 to %3)
br bb3
bb2:
%12 = metatype($@thin Int.Type)
%13 = integer_literal($Builtin.Int2048, 1)
%14 = Swift.Int.init(%13, %12) // (Builtin.Int2048, @thin Int.Type) -> Int
assign(%14 to %3)
br bb3
bb3:
%17 = load(%3 : $*Int)
%19 = Swift.Int.hashValue.getter(%17) // $@convention(method) (Int) -> Int
strong_release(%2#0) // $@box Int
return %19
}
getter
32. 파일 구분 관련 타입들 설명
AffineTransform OTA H RS T S -EEHMD< MREN L -EEHMD< MREN L v p f
CharacterSet
HMSD M EHM RR
VHES G SD DS
G SD DS d TS A D G SD DS p n
Data HMSD M EHM RR VHES / S / S TS A D/ S p n
Date OTA H RS T S / SD <HLD MSD U SHLD m b p
DateComponents OTA H RS T S / SD NLONMDMSR / SD NLONMDMSR p m
DateInterval OTA H RS T S / SD MSD U / SD MSD U p m i o
Decimal D SDMRHNM /D HL
t % e % /D HL TLAD
a p
FileManager OTA H RR H D M FD H D M FD
IndexPath OTA H RS T S MCD 9 SG MCD 9 SG v p o f
IndexSet OTA H RS T S MCD DS MCD DS v p o f
Measurement
OTA H RS T S D RT DLDMS+ MHS< OD
MHS,
D RT DLDMS v p o f
Notification OTA H RS T S NSHEH SHNM NSHEH SHNM v p o f
NSError D SDMRHNM 0 N 0 N 9 NSN N 0 N o u
URL OTA H RS T S : : p f
URLComponents OTA H RS T S : NLONMDMSR : NLONMDMSR p f
URLRequest OTA H RS T S : :DPTDRS TS A D : :DPTDRS p f
33. Summary
Swift Compiler
− LLVM + Chris Lattner, Talyer swift
Type System
− HM Type System + W Algorithm
Type Internals
− class, enumeration, struct, protocol Types
Swift Foundation
− IndexPath, IndexSet, Measurement, Notification