SlideShare a Scribd company logo
Poor Man's
Undergraduate Compilers
suhorng
‘‘ "
This slide: https://github.com/suhorng/ss/tree/master/ft11
1 / 20
Poor Man's Undergraduate Compilers
What compiler?
: https://github.com/suhorng/ss/
A minimal functional language compiler
:
https://github.com/suhorng/compiler13hw/
Compiler homework: Compiling C-- to MIPS,
written in Haskell
How poor?
It is slow
It generates slow codes
‘‘ "
ss
‘‘ "
compiler13hw
2 / 20
originally stands for small Scheme
Written in Scheme, compiling a minimal functional
language to x86-32 assembly
No data types, no optimizations, no need for parsers
Only 102 commits!
suhorng@SHHY-ASPIRE2920 /d/code/test/ss (master)
$ wc *.ss *.s
78 363 3645 closure.ss
192 762 9137 code-gen.ss
86 465 3588 cps.ss
11 24 172 issac.ss
168 811 5584 match-case-simple.ss
30 115 991 prelude.ss
313 1495 14334 reg-alloc.ss
130 477 4942 seq-ir-gen.ss
104 304 3090 ss.ss
166 863 8500 type-infer.ss
85 222 2095 sscrt.s
1363 5901 56078 total
ss
ss
3 / 20
: A Language
Simply typed λ-calculus with constants and a fixed-point
operator. Strict evaluation.
Terms
Types
ss
e :: = c | x | (+ ) | (× )e1 e2 e1 e2
| (lambda ( …) e)x1 x2
| (ifz con th el)
| (fix e)
| ( …)e1 e2
t :: = N | ()
| →t1 t2
| (× …)t1 t2
4 / 20
Passes
type inference
⇓
CPS transformation
⇓
closure conversion
⇓
transform into low-level IR
⇓
register allocation
⇓
machine code generation
5 / 20
Type Inference
Interpreter,
[()
`(() ,(prim-type 'Unit))]
[,x (guard (var? x))
`(,x ,(assq x mono-cxt))]
[(fix ,e)
(let [(a (fresh-var))
(built-e (build-type! e mono-cxt))]
(unify! (expr->type built-e) (fun-type a a))
`((fix ,built-e) ,a))]
[(lambda (,[xs ..]) ,e)
(let* [(obj-as (map (lambda (_) (fresh-var)) xs))
(built-e (build-type! e (append (map cons xs obj-as) mono-cxt)))
(obj-b (expr->type built-e))
(obj-xs (map list xs obj-as))]
`((lambda (,@obj-xs) ,built-e) ,(fun-type (tuple-type obj-as) obj-b)))]
[(,e1 ,[es ..])
(let* [(built-e1 (build-type! e1 mono-cxt))
(built-es (map (lambda (e) (build-type! e mono-cxt)) es))
(obj-a2b (expr->type built-e1))
(obj-as (map expr->type built-es))
(obj-b (fresh-var))]
(unify! obj-a2b (fun-type (tuple-type obj-as) obj-b))
`((,built-e1 ,@built-es) ,obj-b))]
6 / 20
CPS Transformation
interpreter,
(define cpsk ;; cpsk :: eT -> {(eT -> eC) | kC} -> eC
(lambda (expr k)
(match expr
[(,c ,t) (guard (prim-const? c))
(apply-cont k `(,c ,t))]
[(,x ,t) (guard (var? x))
(apply-cont k `(,x ,t))]
[((lambda (,[xs ..]) ,e) ,t)
(let [(k0 (fresh-var "&"))]
(apply-cont k `((lambda ,xs ,k0 ,(cpsk e k0)) ,t)))]
[((fix ,e) ,t)
(cpsk e (lambda (v)
`((fix ,(mark-type v e) ,(place-cont k)) ,t)))]
...
(define apply-cont
(lambda (k x)
(cond [(procedure? k) (k x)]
[else `(cont-ap ,k ,x)])))
(define place-cont
(lambda (k)
(cond [(procedure? k)
(let [(t (fresh-var "%"))]
`(lambda (,t) ,(k t)))]
[else k])))
7 / 20
Closure Conversion
interpreter...
Compute free variables
(match expr
[(,c ,t) (guard (prim-const? c))
'( () )]
[(,x ,t) (guard (var? x))
`( ((,x ,t)) )]
[((lambda (,[xs ..]) ,k ,e) ,t) ; lambda abstraction
(let [(var/e (uncover-free-vars e))]
`(,(remove-assoc* (map car xs) (car var/e)) ,var/e))]
Closure conversion
[((,x ,t) __) (guard (var? x))
(cond [(memq x bound-vars) `(,x ,t)]
[else `((this-ref ,x) ,t)])]
[(((lambda (,[xs ..]) ,k ,e) ,t) (,free-vars ,var/e));lambda abstraction
(let [(fv-ref (map (lambda (x) (closure-convert x `((x)) bound-vars))
free-vars))]
`((closure ,fv-ref
((lambda ,xs ,k ,(closure-convert e var/e (map car xs)))
,t))
,t))]
8 / 20
Now the code looks like
((lambda (a)
((lambda (b) a)
5))
(+ 1 2))
((closure ()
((lambda ((argv Unit)) &1
((+ (1 Int)
(2 Int)
(lambda (%2)
((((closure ()
((lambda ((a Int)) &3
((((closure ((a Int))
((lambda ((b Int)) &4
(cont-ap &4 ((this-ref a) Int)))
(Int -> Int)))
(Int -> Int))
(5 Int))
&3
Int))
(Int -> Int)))
(Int -> Int))
(%2 Int))
&1
Int)))
Int))
(Unit -> Int)))
(Unit -> Int))
9 / 20
Now the code looks like
((lambda (a)
((lambda (b) a)
5))
(+ 1 2))
((closure ()
((lambda ((argv Unit)) &1
((+ (1 Int)
(2 Int)
(lambda (%2)
((((closure ()
| ((lambda ((a Int)) &3
| ((((closure ((a Int))
| ((lambda ((b Int)) &4
| (cont-ap &4 ((this-ref a) Int))))))
| (5 Int))
| &3)))))
(%2 Int))
&1))))))))
10 / 20
Low-level IR
Flatten the continuations, lift functions to top level
(((closure ::fn1 ()) (Unit -> Int))
((lambda ::fn1
(Unit -> Int) ()
((argv Unit))
((%2 : Int <- (1 + 2))
(tail-call (function ::fn2) %2)))
(lambda ::fn2
(Int -> Int) ()
((a Int))
((%f1 : (Int -> Int) <- (closure ::fn3 (a)))
(tail-call %f1 5)))
(lambda ::fn3
(Int -> Int) ((a Int))
((b Int))
((ret (this-ref a))))))
Here comes a machine!
‘‘ "
11 / 20
Register Allocation
A MESS :-D
12 / 20
Machine Code Generation
From pseudo-assembly...
(lambda ::fn1 (Unit -> Int) () ((argv Unit))
((argv Unit)) 1 () ()
((make-call-stack 1)
(eax <- (const 3))
((arg 0) <- eax)
(edi <- (function ::fn2))
(tail-call 1 (function ::fn2))))
(lambda ::fn2 (Int -> Int) () ((a Int))
((a Int) (%f1 Int -> Int)) 2 (ebx) ()
((make-call-stack 2)
(eax <- (function ::fn3))
((arg 0) <- eax)
(eax <- (const 1))
((arg 1) <- eax)
(call-prim 2 make_closure)
(edx <- (formal a))
((closure eax 0) <- edx)
(make-call-stack 1)
(ebx <- eax)
(eax <- (const 5))
((arg 0) <- eax)
(edi <- ebx)
(tail-call 1 ebx)))
(lambda ::fn3 (Int -> Int) ((a Int)) ((b Int))
((a Int) (b Int)) 0 () ()
((eax <- (this-ref a))
return)))
13 / 20
Machine Code Generation
To concrete machine code
(lambda _ss_function_fn1 (Unit -> Int) () ((argv Unit))
((argv Unit)) ()
((sub esp 4) (mov eax 3) (mov (* (esp + 0)) eax)
(mov edi _ss_function_fn2)
(lea edx (* (esp + 4)))
(mov ecx (* (esp + 4))) (mov eax (* (esp + 0)))
(mov (* (edx + 4)) eax) (mov (* (edx)) ecx) (mov esp edx)
(jmp _ss_function_fn2_code)))
(lambda _ss_function_fn2 (Int -> Int) () ((a Int))
((a Int) (%f1 Int -> Int)) ()
((push ebp) (mov ebp esp) (sub esp 4) (mov (* (ebp - 4)) ebx)
(sub esp 8) (mov eax _ss_function_fn3)
(mov (* (esp + 0)) eax) (mov eax 1) (mov (* (esp + 4)) eax)
(call _ss_prim_make_closure) (mov edx (* (ebp + 8)))
(mov (* (eax + 4)) edx) (sub esp 4) (mov ebx eax)
(mov eax 5) (mov (* (esp + 0)) eax) (mov edi ebx)
(mov ebx (* (ebp - 4))) (mov edx (* (ebp)))
(mov ecx (* (ebp + 4))) (lea ebp (* (esp + 12)))
(mov eax (* (esp + 0))) (mov (* (ebp + 4)) eax)
(mov (* (ebp)) ecx) (mov esp ebp) (mov ebp edx)
(jmp (* (edi)))))
(lambda _ss_function_fn3 (Int -> Int) ((a Int)) ((b Int))
((a Int) (b Int)) ()
((mov eax (* (edi + 4)))
(ret 4))))
14 / 20
Tail Calls
Loops
((fix
(lambda (loop)
(lambda (n sum)
(ifz n
sum
(loop (+ n -1) (+ sum n))))))
5 0)
Compare:
int sum = ???;
for (int i = n; i != 0; i = i-1)
sum = sum + n;
Naively implementing tail calls:
Place function call arguments as usual
Move arguments
Adjust frame pointer; jump.
15 / 20
Tail Calls
;#######################################
; _ss_function_fn3: ((* Int Int) -> Int)
; parameters:
; ((n Int) (sum Int))
; free variables:
; ((loop ((* Int Int) -> Int)))
;#######################################
_ss_function_fn3_code: ; Note: this function doesn't have a frame
cmp dword [esp + 4], 0
jne .L1
mov eax, [esp + 8]
ret 8 ; terminating loop
.L1:
mov eax, [esp + 8] ; eax := sum
add eax, [esp + 4] ; eax (sum') += n
mov edx, [esp + 4]
add edx, -1 ; edx (n') := n - 1
sub esp, 8 ; place arguments as usual
mov [esp], edx ; | sum' | esp+4
mov [esp + 4], eax ; | n' | esp
mov edi, [edi + 4] ; load closure pointer
lea edx, [esp + 8]
mov ecx, [esp + 8] ; move new arguments up
mov eax, [esp + 4]
mov [edx + 8], eax ; sum' overrides sum,
mov eax, [esp]
mov [edx + 4], eax ; so does n'!
mov [edx], ecx
mov esp, edx
jmp [edi]
16 / 20
A compiler for a small subset of C, implemented in
Haskell/suhorng & kevin4314
(nothing special)
Parsing is done using Happy
cf. Happy MonadFix, Easy -pass compiler/CindyLinz
compiler13hw
n
17 / 20
Yet the register allocation is still a mess :-D
do
{- read input & parsing -}
let Right parsedAST = Parser.parse input
{- semantic check -}
let compareCompileError ce1 ce2 = compare (errLine ce1) (errLine ce2)
(ast, ces) <- runWriterT $ censor (sortBy compareCompileError) $ do
foldedAST <- Const.constFolding parsedAST
typeInlinedAST <- Desugar.tyDesugar foldedAST
let decayedAST = Desugar.fnArrDesugar typeInlinedAST
symbolAST <- SymTable.buildSymTable decayedAST
typedAST <- TypeCheck.typeCheck symbolAST
return $ NormalizeAST.normalize typedAST
when (not $ null ces) $ mapM_ (putStrLn . show) ces >> exit1
{- code generation -}
let adjustedAST = SethiUllman.seull ast
llir = LLIRTrans.llirTrans adjustedAST
inlinedLLIR = EmptyBlockElim.elim llir
llirFuncs = LLIR.progFuncs inlinedLLIR
llirGlobl = LLIR.progVars inlinedLLIR
llirRegs = LLIR.progRegs inlinedLLIR
let mips = MIPSTrans.transProg $ inlinedLLIR
simpMips = BlockOrder.jumpElim . BlockOrder.blockOrder $ mips
print simpMips
compiler13hw
18 / 20
-- all are interpreters
alphaConvAST s@(S.Block _ _) =
runLocal $ alphaConvBlock' s
alphaConvAST (S.Expr ty line rand rators) =
S.Expr ty line rand <$> mapM alphaConvAST rators
alphaConvAST (S.ImplicitCast ty' ty e) =
S.ImplicitCast ty' ty <$> alphaConvAST e
buildMStmt (P.While line whcond whcode) = do
whcond' <- buildMStmts whcond
whcode' <- runLocal (buildMStmt whcode)
return $ S.While line whcond' whcode'
buildMStmt (P.Identifier line name) = do
currScope <- get
upperScope <- ask
let ty = fmap S.varType $ lookup name currScope <|> lookup name upperScope
case ty of
Just S.TTypeSyn -> tell [errorAt line $ "Unexpected type synonym '" ++ name ++ "'"]
Nothing -> tell [errorAt line $ "Undeclared identifier '" ++ name ++ "'"]
otherwise -> return ()
return $ S.Identifier (error "buildMStmt:Identifier") line name
tyCheckAST (S.Expr _ line rator [rand1, rand2]) | rator `elem` logicOps = do
rand1' <- tyCheckAST rand1
rand2' <- tyCheckAST rand2
let (t1, t2) = (S.getType rand1', S.getType rand2')
when ((not $ tyIsScalarType t1) || (not $ tyIsScalarType t2)) $
tell [errorAt line $ "'" ++ show rator ++ "' is applied to operands of non-scalar typ
return $ S.Expr S.TInt line rator [rand1', rand2']
compiler13hw
19 / 20
Writing Compiler Using Functional
Languages
Interpreters love you!
Simple AST (Pointers? new, delete? Visitor pattern?
Wat?)
I don't know how to deal with similar ASTs
Don't know how to express constraints on ASTs (e.g.
only allow a subset of operators)
Haven't thought of good ways to implement register
allocation
20 / 20

More Related Content

What's hot

Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfacesJava8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Dirk Detering
 
An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROP
Saumil Shah
 
clang-intro
clang-introclang-intro
clang-intro
Hajime Morrita
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
 
Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。
Mr. Vengineer
 
Javascript Uncommon Programming
Javascript Uncommon ProgrammingJavascript Uncommon Programming
Javascript Uncommon Programmingjeffz
 
Lambda выражения и Java 8
Lambda выражения и Java 8Lambda выражения и Java 8
Lambda выражения и Java 8
Alex Tumanoff
 
Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations DVClub
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
Chris Ohk
 
C++11
C++11C++11
C++11
ppd1961
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
Kamil Witecki
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)jeffz
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Sergey Platonov
 
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
GangSeok Lee
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
Ismar Silveira
 
Network lab manual
Network lab manualNetwork lab manual
Network lab manualPrabhu D
 
Rcpp11
Rcpp11Rcpp11
Promise of an API
Promise of an APIPromise of an API
Promise of an API
Maxim Zaks
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semesterDOSONKA Group
 

What's hot (20)

Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfacesJava8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
Java8 Neue Sprachfeatures - Lambda/Streams/default Methods/FunctionalInterfaces
 
An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROP
 
clang-intro
clang-introclang-intro
clang-intro
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。Tiramisu をちょっと、味見してみました。
Tiramisu をちょっと、味見してみました。
 
Javascript Uncommon Programming
Javascript Uncommon ProgrammingJavascript Uncommon Programming
Javascript Uncommon Programming
 
Lambda выражения и Java 8
Lambda выражения и Java 8Lambda выражения и Java 8
Lambda выражения и Java 8
 
Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations Architecture for Massively Parallel HDL Simulations
Architecture for Massively Parallel HDL Simulations
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C++11
C++11C++11
C++11
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against itEvgeniy Muralev, Mark Vince, Working with the compiler, not against it
Evgeniy Muralev, Mark Vince, Working with the compiler, not against it
 
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
Network lab manual
Network lab manualNetwork lab manual
Network lab manual
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
Promise of an API
Promise of an APIPromise of an API
Promise of an API
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 

Viewers also liked

Slide obseravasi pendidikan
Slide obseravasi pendidikanSlide obseravasi pendidikan
Slide obseravasi pendidikankikiregar
 
Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#
IndyMobileNetDev
 
Vaibhav
VaibhavVaibhav
Student managed fund final
Student managed fund finalStudent managed fund final
Student managed fund final
Maria Sanchez
 
It outsourcing 2005
It outsourcing 2005It outsourcing 2005
It outsourcing 2005eaiti
 
PROMIS Tempus Project
PROMIS Tempus ProjectPROMIS Tempus Project
PROMIS Tempus Project
PROMISproject
 
ERAMIS Tempus Project
ERAMIS Tempus ProjectERAMIS Tempus Project
ERAMIS Tempus Project
PROMISproject
 
Evaluation Part 1
Evaluation Part 1Evaluation Part 1
Evaluation Part 1SophieB23
 
Push to pull
Push to pullPush to pull
Push to pulleaiti
 
Cc1 cancer derma
Cc1 cancer dermaCc1 cancer derma
Cc1 cancer derma
Ahmed Amer
 
observasi psikologi pendidikan MAN 2 Model Medan
observasi psikologi pendidikan MAN 2 Model Medanobservasi psikologi pendidikan MAN 2 Model Medan
observasi psikologi pendidikan MAN 2 Model Medan251304
 
Social apps 3_1_2008
Social apps 3_1_2008Social apps 3_1_2008
Social apps 3_1_2008eaiti
 
презентация на ресепшин
презентация на ресепшинпрезентация на ресепшин
презентация на ресепшинTatyana Dubrova
 
Cloud mz cto_roundtable
Cloud mz cto_roundtableCloud mz cto_roundtable
Cloud mz cto_roundtableeaiti
 
10 basics of human genetics
10 basics of human genetics10 basics of human genetics
10 basics of human geneticsAhmed Amer
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...
Functional Thursday
 

Viewers also liked (20)

Slide obseravasi pendidikan
Slide obseravasi pendidikanSlide obseravasi pendidikan
Slide obseravasi pendidikan
 
Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#
 
Vaibhav
VaibhavVaibhav
Vaibhav
 
Skyeluxuria
SkyeluxuriaSkyeluxuria
Skyeluxuria
 
Student managed fund final
Student managed fund finalStudent managed fund final
Student managed fund final
 
It outsourcing 2005
It outsourcing 2005It outsourcing 2005
It outsourcing 2005
 
PROMIS Tempus Project
PROMIS Tempus ProjectPROMIS Tempus Project
PROMIS Tempus Project
 
ERAMIS Tempus Project
ERAMIS Tempus ProjectERAMIS Tempus Project
ERAMIS Tempus Project
 
Evaluation Part 1
Evaluation Part 1Evaluation Part 1
Evaluation Part 1
 
Push to pull
Push to pullPush to pull
Push to pull
 
Nagaraj
NagarajNagaraj
Nagaraj
 
Cc1 cancer derma
Cc1 cancer dermaCc1 cancer derma
Cc1 cancer derma
 
observasi psikologi pendidikan MAN 2 Model Medan
observasi psikologi pendidikan MAN 2 Model Medanobservasi psikologi pendidikan MAN 2 Model Medan
observasi psikologi pendidikan MAN 2 Model Medan
 
Social apps 3_1_2008
Social apps 3_1_2008Social apps 3_1_2008
Social apps 3_1_2008
 
презентация на ресепшин
презентация на ресепшинпрезентация на ресепшин
презентация на ресепшин
 
Prashant Kumar
Prashant KumarPrashant Kumar
Prashant Kumar
 
Ford
FordFord
Ford
 
Cloud mz cto_roundtable
Cloud mz cto_roundtableCloud mz cto_roundtable
Cloud mz cto_roundtable
 
10 basics of human genetics
10 basics of human genetics10 basics of human genetics
10 basics of human genetics
 
[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...[FT-7][snowmantw] How to make a new functional language and make the world be...
[FT-7][snowmantw] How to make a new functional language and make the world be...
 

Similar to [FT-11][suhorng] “Poor Man's” Undergraduate Compilers

Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOAD
Dharmalingam Ganesan
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
RootedCON
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
kyleburton
 
Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1
Tom Paulus
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#bleis tift
 
Lisp как универсальная обертка
Lisp как универсальная оберткаLisp как универсальная обертка
Lisp как универсальная оберткаVsevolod Dyomkin
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
Leonardo Borges
 
Pragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler OptimizationsPragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Marina Kolpakova
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)ujihisa
 
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogrammingdudarev
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
C++ Homework Help
 
Introduction to Assembly Language
Introduction to Assembly LanguageIntroduction to Assembly Language
Introduction to Assembly Language
Motaz Saad
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
Michiel Borkent
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
Eelco Visser
 
Eta
EtaEta
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
jonbodner
 
lex.ppt
lex.pptlex.ppt

Similar to [FT-11][suhorng] “Poor Man's” Undergraduate Compilers (20)

Load-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOADLoad-time Hacking using LD_PRELOAD
Load-time Hacking using LD_PRELOAD
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 
Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1Getting Started with Raspberry Pi - DCC 2013.1
Getting Started with Raspberry Pi - DCC 2013.1
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#
 
Lisp как универсальная обертка
Lisp как универсальная оберткаLisp как универсальная обертка
Lisp как универсальная обертка
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
 
Pragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler OptimizationsPragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
Pragmatic Optimization in Modern Programming - Mastering Compiler Optimizations
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogramming
 
Best C++ Programming Homework Help
Best C++ Programming Homework HelpBest C++ Programming Homework Help
Best C++ Programming Homework Help
 
Introduction to Assembly Language
Introduction to Assembly LanguageIntroduction to Assembly Language
Introduction to Assembly Language
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
Eta
EtaEta
Eta
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
 
lex.ppt
lex.pptlex.ppt
lex.ppt
 

Recently uploaded

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

[FT-11][suhorng] “Poor Man's” Undergraduate Compilers

  • 1. Poor Man's Undergraduate Compilers suhorng ‘‘ " This slide: https://github.com/suhorng/ss/tree/master/ft11 1 / 20
  • 2. Poor Man's Undergraduate Compilers What compiler? : https://github.com/suhorng/ss/ A minimal functional language compiler : https://github.com/suhorng/compiler13hw/ Compiler homework: Compiling C-- to MIPS, written in Haskell How poor? It is slow It generates slow codes ‘‘ " ss ‘‘ " compiler13hw 2 / 20
  • 3. originally stands for small Scheme Written in Scheme, compiling a minimal functional language to x86-32 assembly No data types, no optimizations, no need for parsers Only 102 commits! suhorng@SHHY-ASPIRE2920 /d/code/test/ss (master) $ wc *.ss *.s 78 363 3645 closure.ss 192 762 9137 code-gen.ss 86 465 3588 cps.ss 11 24 172 issac.ss 168 811 5584 match-case-simple.ss 30 115 991 prelude.ss 313 1495 14334 reg-alloc.ss 130 477 4942 seq-ir-gen.ss 104 304 3090 ss.ss 166 863 8500 type-infer.ss 85 222 2095 sscrt.s 1363 5901 56078 total ss ss 3 / 20
  • 4. : A Language Simply typed λ-calculus with constants and a fixed-point operator. Strict evaluation. Terms Types ss e :: = c | x | (+ ) | (× )e1 e2 e1 e2 | (lambda ( …) e)x1 x2 | (ifz con th el) | (fix e) | ( …)e1 e2 t :: = N | () | →t1 t2 | (× …)t1 t2 4 / 20
  • 5. Passes type inference ⇓ CPS transformation ⇓ closure conversion ⇓ transform into low-level IR ⇓ register allocation ⇓ machine code generation 5 / 20
  • 6. Type Inference Interpreter, [() `(() ,(prim-type 'Unit))] [,x (guard (var? x)) `(,x ,(assq x mono-cxt))] [(fix ,e) (let [(a (fresh-var)) (built-e (build-type! e mono-cxt))] (unify! (expr->type built-e) (fun-type a a)) `((fix ,built-e) ,a))] [(lambda (,[xs ..]) ,e) (let* [(obj-as (map (lambda (_) (fresh-var)) xs)) (built-e (build-type! e (append (map cons xs obj-as) mono-cxt))) (obj-b (expr->type built-e)) (obj-xs (map list xs obj-as))] `((lambda (,@obj-xs) ,built-e) ,(fun-type (tuple-type obj-as) obj-b)))] [(,e1 ,[es ..]) (let* [(built-e1 (build-type! e1 mono-cxt)) (built-es (map (lambda (e) (build-type! e mono-cxt)) es)) (obj-a2b (expr->type built-e1)) (obj-as (map expr->type built-es)) (obj-b (fresh-var))] (unify! obj-a2b (fun-type (tuple-type obj-as) obj-b)) `((,built-e1 ,@built-es) ,obj-b))] 6 / 20
  • 7. CPS Transformation interpreter, (define cpsk ;; cpsk :: eT -> {(eT -> eC) | kC} -> eC (lambda (expr k) (match expr [(,c ,t) (guard (prim-const? c)) (apply-cont k `(,c ,t))] [(,x ,t) (guard (var? x)) (apply-cont k `(,x ,t))] [((lambda (,[xs ..]) ,e) ,t) (let [(k0 (fresh-var "&"))] (apply-cont k `((lambda ,xs ,k0 ,(cpsk e k0)) ,t)))] [((fix ,e) ,t) (cpsk e (lambda (v) `((fix ,(mark-type v e) ,(place-cont k)) ,t)))] ... (define apply-cont (lambda (k x) (cond [(procedure? k) (k x)] [else `(cont-ap ,k ,x)]))) (define place-cont (lambda (k) (cond [(procedure? k) (let [(t (fresh-var "%"))] `(lambda (,t) ,(k t)))] [else k]))) 7 / 20
  • 8. Closure Conversion interpreter... Compute free variables (match expr [(,c ,t) (guard (prim-const? c)) '( () )] [(,x ,t) (guard (var? x)) `( ((,x ,t)) )] [((lambda (,[xs ..]) ,k ,e) ,t) ; lambda abstraction (let [(var/e (uncover-free-vars e))] `(,(remove-assoc* (map car xs) (car var/e)) ,var/e))] Closure conversion [((,x ,t) __) (guard (var? x)) (cond [(memq x bound-vars) `(,x ,t)] [else `((this-ref ,x) ,t)])] [(((lambda (,[xs ..]) ,k ,e) ,t) (,free-vars ,var/e));lambda abstraction (let [(fv-ref (map (lambda (x) (closure-convert x `((x)) bound-vars)) free-vars))] `((closure ,fv-ref ((lambda ,xs ,k ,(closure-convert e var/e (map car xs))) ,t)) ,t))] 8 / 20
  • 9. Now the code looks like ((lambda (a) ((lambda (b) a) 5)) (+ 1 2)) ((closure () ((lambda ((argv Unit)) &1 ((+ (1 Int) (2 Int) (lambda (%2) ((((closure () ((lambda ((a Int)) &3 ((((closure ((a Int)) ((lambda ((b Int)) &4 (cont-ap &4 ((this-ref a) Int))) (Int -> Int))) (Int -> Int)) (5 Int)) &3 Int)) (Int -> Int))) (Int -> Int)) (%2 Int)) &1 Int))) Int)) (Unit -> Int))) (Unit -> Int)) 9 / 20
  • 10. Now the code looks like ((lambda (a) ((lambda (b) a) 5)) (+ 1 2)) ((closure () ((lambda ((argv Unit)) &1 ((+ (1 Int) (2 Int) (lambda (%2) ((((closure () | ((lambda ((a Int)) &3 | ((((closure ((a Int)) | ((lambda ((b Int)) &4 | (cont-ap &4 ((this-ref a) Int)))))) | (5 Int)) | &3))))) (%2 Int)) &1)))))))) 10 / 20
  • 11. Low-level IR Flatten the continuations, lift functions to top level (((closure ::fn1 ()) (Unit -> Int)) ((lambda ::fn1 (Unit -> Int) () ((argv Unit)) ((%2 : Int <- (1 + 2)) (tail-call (function ::fn2) %2))) (lambda ::fn2 (Int -> Int) () ((a Int)) ((%f1 : (Int -> Int) <- (closure ::fn3 (a))) (tail-call %f1 5))) (lambda ::fn3 (Int -> Int) ((a Int)) ((b Int)) ((ret (this-ref a)))))) Here comes a machine! ‘‘ " 11 / 20
  • 13. Machine Code Generation From pseudo-assembly... (lambda ::fn1 (Unit -> Int) () ((argv Unit)) ((argv Unit)) 1 () () ((make-call-stack 1) (eax <- (const 3)) ((arg 0) <- eax) (edi <- (function ::fn2)) (tail-call 1 (function ::fn2)))) (lambda ::fn2 (Int -> Int) () ((a Int)) ((a Int) (%f1 Int -> Int)) 2 (ebx) () ((make-call-stack 2) (eax <- (function ::fn3)) ((arg 0) <- eax) (eax <- (const 1)) ((arg 1) <- eax) (call-prim 2 make_closure) (edx <- (formal a)) ((closure eax 0) <- edx) (make-call-stack 1) (ebx <- eax) (eax <- (const 5)) ((arg 0) <- eax) (edi <- ebx) (tail-call 1 ebx))) (lambda ::fn3 (Int -> Int) ((a Int)) ((b Int)) ((a Int) (b Int)) 0 () () ((eax <- (this-ref a)) return))) 13 / 20
  • 14. Machine Code Generation To concrete machine code (lambda _ss_function_fn1 (Unit -> Int) () ((argv Unit)) ((argv Unit)) () ((sub esp 4) (mov eax 3) (mov (* (esp + 0)) eax) (mov edi _ss_function_fn2) (lea edx (* (esp + 4))) (mov ecx (* (esp + 4))) (mov eax (* (esp + 0))) (mov (* (edx + 4)) eax) (mov (* (edx)) ecx) (mov esp edx) (jmp _ss_function_fn2_code))) (lambda _ss_function_fn2 (Int -> Int) () ((a Int)) ((a Int) (%f1 Int -> Int)) () ((push ebp) (mov ebp esp) (sub esp 4) (mov (* (ebp - 4)) ebx) (sub esp 8) (mov eax _ss_function_fn3) (mov (* (esp + 0)) eax) (mov eax 1) (mov (* (esp + 4)) eax) (call _ss_prim_make_closure) (mov edx (* (ebp + 8))) (mov (* (eax + 4)) edx) (sub esp 4) (mov ebx eax) (mov eax 5) (mov (* (esp + 0)) eax) (mov edi ebx) (mov ebx (* (ebp - 4))) (mov edx (* (ebp))) (mov ecx (* (ebp + 4))) (lea ebp (* (esp + 12))) (mov eax (* (esp + 0))) (mov (* (ebp + 4)) eax) (mov (* (ebp)) ecx) (mov esp ebp) (mov ebp edx) (jmp (* (edi))))) (lambda _ss_function_fn3 (Int -> Int) ((a Int)) ((b Int)) ((a Int) (b Int)) () ((mov eax (* (edi + 4))) (ret 4)))) 14 / 20
  • 15. Tail Calls Loops ((fix (lambda (loop) (lambda (n sum) (ifz n sum (loop (+ n -1) (+ sum n)))))) 5 0) Compare: int sum = ???; for (int i = n; i != 0; i = i-1) sum = sum + n; Naively implementing tail calls: Place function call arguments as usual Move arguments Adjust frame pointer; jump. 15 / 20
  • 16. Tail Calls ;####################################### ; _ss_function_fn3: ((* Int Int) -> Int) ; parameters: ; ((n Int) (sum Int)) ; free variables: ; ((loop ((* Int Int) -> Int))) ;####################################### _ss_function_fn3_code: ; Note: this function doesn't have a frame cmp dword [esp + 4], 0 jne .L1 mov eax, [esp + 8] ret 8 ; terminating loop .L1: mov eax, [esp + 8] ; eax := sum add eax, [esp + 4] ; eax (sum') += n mov edx, [esp + 4] add edx, -1 ; edx (n') := n - 1 sub esp, 8 ; place arguments as usual mov [esp], edx ; | sum' | esp+4 mov [esp + 4], eax ; | n' | esp mov edi, [edi + 4] ; load closure pointer lea edx, [esp + 8] mov ecx, [esp + 8] ; move new arguments up mov eax, [esp + 4] mov [edx + 8], eax ; sum' overrides sum, mov eax, [esp] mov [edx + 4], eax ; so does n'! mov [edx], ecx mov esp, edx jmp [edi] 16 / 20
  • 17. A compiler for a small subset of C, implemented in Haskell/suhorng & kevin4314 (nothing special) Parsing is done using Happy cf. Happy MonadFix, Easy -pass compiler/CindyLinz compiler13hw n 17 / 20
  • 18. Yet the register allocation is still a mess :-D do {- read input & parsing -} let Right parsedAST = Parser.parse input {- semantic check -} let compareCompileError ce1 ce2 = compare (errLine ce1) (errLine ce2) (ast, ces) <- runWriterT $ censor (sortBy compareCompileError) $ do foldedAST <- Const.constFolding parsedAST typeInlinedAST <- Desugar.tyDesugar foldedAST let decayedAST = Desugar.fnArrDesugar typeInlinedAST symbolAST <- SymTable.buildSymTable decayedAST typedAST <- TypeCheck.typeCheck symbolAST return $ NormalizeAST.normalize typedAST when (not $ null ces) $ mapM_ (putStrLn . show) ces >> exit1 {- code generation -} let adjustedAST = SethiUllman.seull ast llir = LLIRTrans.llirTrans adjustedAST inlinedLLIR = EmptyBlockElim.elim llir llirFuncs = LLIR.progFuncs inlinedLLIR llirGlobl = LLIR.progVars inlinedLLIR llirRegs = LLIR.progRegs inlinedLLIR let mips = MIPSTrans.transProg $ inlinedLLIR simpMips = BlockOrder.jumpElim . BlockOrder.blockOrder $ mips print simpMips compiler13hw 18 / 20
  • 19. -- all are interpreters alphaConvAST s@(S.Block _ _) = runLocal $ alphaConvBlock' s alphaConvAST (S.Expr ty line rand rators) = S.Expr ty line rand <$> mapM alphaConvAST rators alphaConvAST (S.ImplicitCast ty' ty e) = S.ImplicitCast ty' ty <$> alphaConvAST e buildMStmt (P.While line whcond whcode) = do whcond' <- buildMStmts whcond whcode' <- runLocal (buildMStmt whcode) return $ S.While line whcond' whcode' buildMStmt (P.Identifier line name) = do currScope <- get upperScope <- ask let ty = fmap S.varType $ lookup name currScope <|> lookup name upperScope case ty of Just S.TTypeSyn -> tell [errorAt line $ "Unexpected type synonym '" ++ name ++ "'"] Nothing -> tell [errorAt line $ "Undeclared identifier '" ++ name ++ "'"] otherwise -> return () return $ S.Identifier (error "buildMStmt:Identifier") line name tyCheckAST (S.Expr _ line rator [rand1, rand2]) | rator `elem` logicOps = do rand1' <- tyCheckAST rand1 rand2' <- tyCheckAST rand2 let (t1, t2) = (S.getType rand1', S.getType rand2') when ((not $ tyIsScalarType t1) || (not $ tyIsScalarType t2)) $ tell [errorAt line $ "'" ++ show rator ++ "' is applied to operands of non-scalar typ return $ S.Expr S.TInt line rator [rand1', rand2'] compiler13hw 19 / 20
  • 20. Writing Compiler Using Functional Languages Interpreters love you! Simple AST (Pointers? new, delete? Visitor pattern? Wat?) I don't know how to deal with similar ASTs Don't know how to express constraints on ASTs (e.g. only allow a subset of operators) Haven't thought of good ways to implement register allocation 20 / 20