8. Metaphysics
• from Ancient Greece
• Physics – “nature” in Greek
• Exploring the fundamental nature of reality
• Ultimately, what is there?
• What is it like?
13. From program to execution
• Pre-processor
• processes its input data to produce output that
is used as input to another program
• Compiler
• transforms source code written in a
programming language into another computer
language
• Assembler
• Translates instructions into machine code
• From Wikipedia
pre-processor
compiler
assembler
linker
memory
loader
14. Runtime
Compile time
From program to execution
• Linker
• takes one or more object files generated
by a compiler and combines them into a
single executable file, library file, or
another object file
• Loader
• part of an operating system that is
responsible for loading programs and
libraries
• From Wikipedia
pre-processor
compiler
assembler
linker
memory
loader
15. Compile time
From program to execution
pre-processor
compiler
assembler
linker
Macro processing, augmentation,
file inclusion, language extension
Generate target assembly code
Generate relocatable machine code
(object file)
Links object files and generate
executable machine code (binary file)
16. Runtime
From program to execution
memory
loader
Loader (part of OS) loads
programs and libraries
Execution
18. 各種metaprogramming
• Macro in C, Julia
• Template in C++
• Reflection in Java……
• Introspection
• Generated function only in Julia
19. Compiler
Compiler in brief
Lexical analysis
Syntax analysis
Semantic analysis
scannerIdentify tokens
parser
Identify the grammar and
establish the parsing tree
checkerType checking, declaration
20. Compiler in brief
Compiler
Intermediate code
generation
Optimization
Code generation
Generate machine independent code for
common optimization
Optimization! Some magics……
Generate assembly code
Interpreter
21. Julia’s compiler
Source code
Tokens
AST
Julia IR
Typed Julia IR
LLVM IR
LLVM IR
Instructions
lexing
parsing
lowering
type inference
LLVM codegen
optimize
native codegen
LLVM IRLLVM IRMacro
expansion
Generated
function
JIT
22. While execution…
sum(12, 3.0)
Function table
-----------
sum
add
reduce
length
sum
-----------
(Number, Number)
(Integer, Integer)
(Float64, Float64)
AST
(Int64, Float64)
sum
Multiple dispatch
Parsing
Function call
Compile
Execute
Cached?
32. AST julia> dump(ex)
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: Symbol a
3: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol *
2: Symbol b
3: Symbol c
4: Int64 1
julia> ex = :(a + b * c + 1)
:(a + b * c + 1)
33. AST
julia> dump(ex)
Expr
head: Symbol call
args: Array{Any}((4,))
1: Symbol +
2: Symbol a
3: Expr
head: Symbol call
args: Array{Any}((3,))
1: Symbol *
2: Symbol b
3: Symbol c
4: Int64 1
a
+
1
b
*
c
43. Generated function
• Generate specialized function depending on the types of their
arguments
• Macro?
• Act on parsing time, and not accept type of arguments
• Once generated function knows type of arguments, it expand
and remain un-compiled
47. Generated function
• How to code a generated function?
1. Add `@generated`
2. In the function body, only type is accessible, not value
3. Return quoted expression, instead of calculation result or actions
48. Generated function
• Purpose: To specialize for performance
julia> @generated function bar(x)
if x <: Integer
return :(x^2)
else
return :(x)
end
end
bar (generic function with 1 method)
julia> bar(4)
16
julia> bar("baz")
"baz"
49. How Julia’s compiler work?
Source code
Tokens
AST
Julia IR
Typed Julia IR
LLVM IR
LLVM IR
Instructions
lexing
parsing
lowering
type inference
LLVM codegen
optimize
native codegen
LLVM IRLLVM IR
Expr
Symbol
@code_lowered
@code_typed
@code_llvm
@code_native
@code_warntype
50. JIT compiler in Julia
AST
Julia IR
Typed Julia IR
lowering
type inference
LLVM IR
LLVM codegen
Function call
Multiple dispatch
Execution
specialize
specialize
optimize
codegen
https://www.youtube.com/watch?v=XWIZ_dCO6X8
51. 為什麼Julia可以這麼快?
• 不提供龐雜的語言 API,增加最佳化可能性
• 型別系統與多重分派的設計提供多樣而可特化的語言
• 在定義語言的時候大量使用macro跟generated function
• 將多數動態語言在runtime執行的事情搬到compile time
• 盡力提供fully-typed LLVM IR,交由LLVM做最佳化