PatLang control-flow graphs
For new readers
A "control-flow graph" is a diagram of every possible path execution can take through a function — each box is a straight-line chunk of code, and each arrow is a jump (a red curve for a conditional branch, blue for an unconditional one). These are drawn directly from PatLang's own compiler internals, for a handful of real example programs, as a way to see what a program's shape looks like once it's been analyzed rather than just read as text. See the flowgraph reference page for how this is built.
Basic blocks and jump edges recovered from the self-hosted compiler's own IR, one section per program. Red curves are conditional (JumpIfFalse), blue are unconditional.
self_hosting/examples/fib_bench.patlang · self_hosting/examples/contracts_demo.patlang · self_hosting/lib/pos.patlang+self_hosting/examples/pos_demo.patlang · self_hosting/examples/feature_demo.patlang
self_hosting/examples/fib_bench.patlang
self_hosting/examples/fib_bench.patlang — 3 function(s)
fib
B@0
Load n
Const num 2
Bin <
JumpIfFalse 7
B@4
Load n
Return
Jump 17
B@7
Load n
Const num 1
Bin -
Call fib 1
Load n
Const num 2
Bin -
Call fib 1
Bin +
Return
B@17
Return
sum_to
B@0
Const num 0
Store total
Const num 1
Store i
B@4
Load i
Load n
Bin <=
JumpIfFalse 17
B@8
Load total
Load i
Bin +
Store total
Load i
Const num 1
Bin +
Store i
Jump 4
B@17
Load total
Return
Return
main
B@0
CallHost now_ms 0
Store t0
Const num 27
Call fib 1
Store f
CallHost now_ms 0
Store t1
Const str fib(27) =
Load f
Bin +
Const str [
Bin +
Load t1
Load t0
Bin -
Bin +
Const str ms]
Bin +
CallHost print 1
CallHost now_ms 0
Store t2
Const num 2000000
Call sum_to 1
Store s
CallHost now_ms 0
Store t3
Const str sum 1..2000000 =
Load s
Bin +
Const str [
Bin +
Load t3
Load t2
Bin -
Bin +
Const str ms]
Bin +
CallHost print 1
CallHost now_ms 0
Store t4
CallHost vec_new 0
Store xs
Const num 0
Store k
B@44
Load k
Const num 200000
Bin <
JumpIfFalse 58
B@48
Load xs
Load k
Const num 2
Bin *
CallHost vec_push 2
Load k
Const num 1
Bin +
Store k
Jump 44
B@58
CallHost now_ms 0
Store t5
Const str built vector of
Load xs
CallHost vec_len 1
Bin +
Const str [
Bin +
Load t5
Load t4
Bin -
Bin +
Const str ms]
Bin +
CallHost print 1
Const str total:
Load t5
Load t0
Bin -
Bin +
Const str ms
Bin +
CallHost print 1
Return
self_hosting/examples/contracts_demo.patlang
self_hosting/examples/contracts_demo.patlang — 4 function(s)
safe_divide
B@0
Const str safe_divide
Const str require
Const str (Var(b) != Num(0))
Load b
Const num 0
Bin !=
CallHost contract_check 4
Load a
Load b
Bin /
Store r
Const str safe_divide
Const str ensure
Const str ((Var(r) * Var(b)) == Var(a))
Load r
Load b
Bin *
Load a
Bin ==
CallHost contract_check 4
Load r
Return
Return
clamp
B@0
Const str clamp
Const str require
Const str (Var(lo) <= Var(hi))
Load lo
Load hi
Bin <=
CallHost contract_check 4
Load x
Load lo
Bin <
JumpIfFalse 14
B@11
Load lo
Store r
Jump 23
B@14
Load x
Load hi
Bin >
JumpIfFalse 21
B@18
Load hi
Store r
Jump 23
B@21
Load x
Store r
B@23
Const str clamp
Const str ensure
Const str ((Var(r) >= Var(lo)) and (Var(r) <= Var(hi)))
Load r
Load lo
Bin >=
Un not
JumpIfFalse 33
B@31
Const bool false
Jump 38
B@33
Load r
Load hi
Bin <=
Un not
Un not
B@38
CallHost contract_check 4
Load r
Return
Return
factorial
B@0
Const str factorial
Const str require
Const str (Var(n) >= Num(0))
Load n
Const num 0
Bin >=
CallHost contract_check 4
Load n
Const num 2
Bin <
JumpIfFalse 14
B@11
Const num 1
Store r
Jump 21
B@14
Load n
Load n
Const num 1
Bin -
Call factorial 1
Bin *
Store r
B@21
Const str factorial
Const str ensure
Const str (Var(r) >= Num(1))
Load r
Const num 1
Bin >=
CallHost contract_check 4
Load r
Return
Return
main
B@0
Const str safe_divide(10, 2) =
Const num 10
Const num 2
Call safe_divide 2
Bin +
CallHost print 1
Const str clamp(15, 0, 10) =
Const num 15
Const num 0
Const num 10
Call clamp 3
Bin +
CallHost print 1
Const str clamp(-5, 0, 10) =
Const num 5
Un -
Const num 0
Const num 10
Call clamp 3
Bin +
CallHost print 1
Const str factorial(6) =
Const num 6
Call factorial 1
Bin +
CallHost print 1
Const str main
Const str assert
Const str ((Num(1) + Num(1)) == Num(2))
Const num 1
Const num 1
Bin +
Const num 2
Bin ==
CallHost contract_check 4
Const str standalone assert passed
CallHost print 1
Const str --- now deliberately violating a precondition ---
CallHost print 1
Const str safe_divide(1, 0) =
Const num 1
Const num 0
Call safe_divide 2
Bin +
CallHost print 1
Const str (unreachable: the line above aborts the program)
CallHost print 1
Return
self_hosting/lib/pos.patlang+self_hosting/examples/pos_demo.patlang
self_hosting/lib/pos.patlang+self_hosting/examples/pos_demo.patlang — 7 function(s)
pos_setup
B@0
Const str Product
Const str apple
CallHost new 2
Const str apple
Const str set
Const str price
Const num 30
CallHost send 4
Const str Product
Const str banana
CallHost new 2
Const str banana
Const str set
Const str price
Const num 25
CallHost send 4
Const str Product
Const str milk
CallHost new 2
Const str milk
Const str set
Const str price
Const num 120
CallHost send 4
Const str dairy
Const str milk
Const str yes
CallHost fact 3
Const str dairy
Const str cheese
Const str yes
CallHost fact 3
Const str total
Const num 0
CallHost set_var 2
Const str items
Const num 0
CallHost set_var 2
Const str receipt
Const str
CallHost set_var 2
Const str serve_customer
Const str till-1
CallHost goal 2
Const bool true
Return
Return
pos_scan
B@0
Load item
Const str price
CallHost get 2
Store price
Load price
Un not
JumpIfFalse 14
B@7
Const str unknown item:
Load item
Bin +
CallHost print 1
Const num 0
Return
Jump 14
B@14
Const str dairy
Load item
Const num 0
CallHost query 3
Const num 0
Bin >
JumpIfFalse 28
B@21
Load price
Const num 9
Bin *
Const num 10
Bin /
Store price
Jump 28
B@28
Const str total
Const str __vars
Const str total
CallHost get 2
Load price
Bin +
CallHost set_var 2
Const str items
Const str __vars
Const str items
CallHost get 2
Const num 1
Bin +
CallHost set_var 2
Const str receipt
Const str __vars
Const str receipt
CallHost get 2
Const str
Bin +
Load item
Bin +
Const str
Bin +
Load price
Bin +
Const str p
Bin +
CallHost set_var 2
Load price
Return
Return
pos_total
B@0
Const str __vars
Const str total
CallHost get 2
Return
Return
pos_pay
B@0
Const str __vars
Const str total
CallHost get 2
Store total
Load tendered
Load total
Bin -
Store change
Const str --- RECEIPT ---
CallHost print 1
Const str __vars
Const str receipt
CallHost get 2
Const str items:
Bin +
Const str __vars
Const str items
CallHost get 2
Bin +
CallHost print 1
Const str total:
Load total
Bin +
Const str p, tendered:
Bin +
Load tendered
Bin +
Const str p, change:
Bin +
Load change
Bin +
Const str p
Bin +
CallHost print 1
Load change
Return
Return
__when_scan_1
B@0
Load event_data
Call pos_scan 1
Return
__when_pay_2
B@0
Load event_data
Call pos_pay 1
Return
main
B@0
Call pos_setup 0
Const str scanning...
CallHost print 1
Const str scan
Const str apple
CallHost emit 2
Const str scan
Const str apple
CallHost emit 2
Const str scan
Const str banana
CallHost emit 2
Const str scan
Const str milk
CallHost emit 2
Const str scan
Const str unobtainium
CallHost emit 2
Const str pay
Const num 500
CallHost emit 2
Const str till session complete
CallHost print 1
Return
self_hosting/examples/feature_demo.patlang
self_hosting/examples/feature_demo.patlang — 8 function(s)
fib
B@0
Load n
Const num 2
Bin <
JumpIfFalse 7
B@4
Load n
Return
Jump 17
B@7
Load n
Const num 1
Bin -
Call fib 1
Load n
Const num 2
Bin -
Call fib 1
Bin +
Return
B@17
Return
sum_list
B@0
Const num 0
Store total
Const num 0
Store i
B@4
Load i
Load xs
CallHost len 1
Bin <
JumpIfFalse 20
B@9
Load total
Load xs
Load i
CallHost list_get 2
Bin +
Store total
Load i
Const num 1
Bin +
Store i
Jump 4
B@20
Load total
Return
Return
__when_greeting_1
B@0
Const str event received:
Load event_data
Bin +
CallHost print 1
Return
double
B@0
Load x
Const num 2
Bin *
Return
Return
is_even
B@0
Load x
Const num 2
Bin %
Const num 0
Bin ==
Return
Return
map_list
B@0
BuildList 0
Store out
Const num 0
Store i
B@4
Load i
Load xs
CallHost len 1
Bin <
JumpIfFalse 22
B@9
Load out
Load fname
Load xs
Load i
CallHost list_get 2
CallHost apply 2
CallHost list_push 2
Store out
Load i
Const num 1
Bin +
Store i
Jump 4
B@22
Load out
Return
Return
filter_list
B@0
BuildList 0
Store out
Const num 0
Store i
B@4
Load i
Load xs
CallHost len 1
Bin <
JumpIfFalse 27
B@9
Load fname
Load xs
Load i
CallHost list_get 2
CallHost apply 2
JumpIfFalse 22
B@15
Load out
Load xs
Load i
CallHost list_get 2
CallHost list_push 2
Store out
Jump 22
B@22
Load i
Const num 1
Bin +
Store i
Jump 4
B@27
Load out
Return
Return
main
B@0
Const num 1
Const num 2
Const num 3
Const num 4
BuildList 4
Store nums
Const str fib(10) =
Const num 10
Call fib 1
Bin +
CallHost print 1
Const str sum =
Load nums
Call sum_list 1
Bin +
CallHost print 1
Load nums
Call sum_list 1
Const num 10
Bin ==
JumpIfFalse 24
B@21
Const str sum ok
CallHost print 1
Jump 26
B@24
Const str sum wrong
CallHost print 1
B@26
Const str greeting
Const str hello events
CallHost emit 2
Const str parent
Const str alice
Const str bob
CallHost fact 3
Const str parent
Const str alice
Const str carol
CallHost fact 3
Const str alice children:
Const str parent
Const str alice
Const num 0
CallHost query 3
Bin +
CallHost print 1
Const str Person
Const str kim
CallHost new 2
Const str kim
Const str set
Const str age
Const num 42
CallHost send 4
Const str kim age:
Const str kim
Const str age
CallHost get 2
Bin +
CallHost print 1
Const str doubled:
Load nums
Const str double
Call map_list 2
Bin +
CallHost print 1
Const str evens:
Load nums
Const str is_even
Call filter_list 2
Bin +
CallHost print 1
Return