PatLang control-flow graphs

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 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 Store __discard 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 Store __discard CallHost now_ms 0 Store t4 CallHost vec_new 0 Store xs Const num 0 Store k B@46 Load k Const num 200000 Bin < JumpIfFalse 61 B@50 Load xs Load k Const num 2 Bin * CallHost vec_push 2 Store __discard Load k Const num 1 Bin + Store k Jump 46 B@61 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 Store __discard 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 Store __discard 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 Store __discard Load r Return 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 Store __discard Load x Load lo Bin < JumpIfFalse 15 B@12 Load lo Store r Jump 24 B@15 Load x Load hi Bin > JumpIfFalse 22 B@19 Load hi Store r Jump 24 B@22 Load x Store r B@24 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 34 B@32 Const bool false Jump 39 B@34 Load r Load hi Bin <= Un not Un not B@39 CallHost contract_check 4 Store __discard Load r Return 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 Store __discard Load n Const num 2 Bin < JumpIfFalse 15 B@12 Const num 1 Store r Jump 22 B@15 Load n Load n Const num 1 Bin - Call factorial 1 Bin * Store r B@22 Const str factorial Const str ensure Const str (Var(r) >= Num(1)) Load r Const num 1 Bin >= CallHost contract_check 4 Store __discard Load r Return 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 Store __discard Const str clamp(15, 0, 10) = Const num 15 Const num 0 Const num 10 Call clamp 3 Bin + CallHost print 1 Store __discard Const str clamp(-5, 0, 10) = Const num 5 Un - Const num 0 Const num 10 Call clamp 3 Bin + CallHost print 1 Store __discard Const str factorial(6) = Const num 6 Call factorial 1 Bin + CallHost print 1 Store __discard 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 Store __discard Const str standalone assert passed CallHost print 1 Store __discard Const str --- now deliberately violating a precondition --- CallHost print 1 Store __discard Const str safe_divide(1, 0) = Const num 1 Const num 0 Call safe_divide 2 Bin + CallHost print 1 Store __discard 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 Store __discard Const str apple Const str set Const str price Const num 30 CallHost send 4 Store __discard Const str Product Const str banana CallHost new 2 Store __discard Const str banana Const str set Const str price Const num 25 CallHost send 4 Store __discard Const str Product Const str milk CallHost new 2 Store __discard Const str milk Const str set Const str price Const num 120 CallHost send 4 Store __discard Const str dairy Const str milk Const str yes CallHost fact 3 Store __discard Const str dairy Const str cheese Const str yes CallHost fact 3 Store __discard Const str total Const num 0 CallHost set_var 2 Store __discard Const str items Const num 0 CallHost set_var 2 Store __discard Const str receipt Const str CallHost set_var 2 Store __discard Const str serve_customer Const str till-1 CallHost goal 2 Store __discard Const bool true Return Return

pos_scan

B@0 Load item Const str price CallHost get 2 Store price Load price Un not JumpIfFalse 15 B@7 Const str unknown item: Load item Bin + CallHost print 1 Store __discard Const num 0 Return Jump 15 B@15 Const str dairy Load item Const num 0 CallHost query 3 Const num 0 Bin > JumpIfFalse 29 B@22 Load price Const num 9 Bin * Const num 10 Bin / Store price Jump 29 B@29 Const str total Const str __vars Const str total CallHost get 2 Load price Bin + CallHost set_var 2 Store __discard Const str items Const str __vars Const str items CallHost get 2 Const num 1 Bin + CallHost set_var 2 Store __discard 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 Store __discard Load price Return 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 Store __discard 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 Store __discard 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 Store __discard Load change Return Load change Return Return

main

B@0 Const str scan MakeClosure __closure_0 [] CallHost register_event_handler 2 Store __discard Const str pay MakeClosure __closure_1 [] CallHost register_event_handler 2 Store __discard Call pos_setup 0 Store __discard Const str scanning... CallHost print 1 Store __discard Const str scan Const str apple CallHost emit 2 Store __discard Const str scan Const str apple CallHost emit 2 Store __discard Const str scan Const str banana CallHost emit 2 Store __discard Const str scan Const str milk CallHost emit 2 Store __discard Const str scan Const str unobtainium CallHost emit 2 Store __discard Const str pay Const num 500 CallHost emit 2 Store __discard Const str till session complete CallHost print 1 Return

__closure_0

B@0 Load event_data Call pos_scan 1 Return

__closure_1

B@0 Load event_data Call pos_pay 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 Load total Return 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 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 Load out Return Return

main

B@0 Const str greeting MakeClosure __closure_0 [] CallHost register_event_handler 2 Store __discard 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 Store __discard Const str sum = Load nums Call sum_list 1 Bin + CallHost print 1 Store __discard Load nums Call sum_list 1 Const num 10 Bin == JumpIfFalse 31 B@27 Const str sum ok CallHost print 1 Store __discard Jump 34 B@31 Const str sum wrong CallHost print 1 Store __discard B@34 Const str greeting Const str hello events CallHost emit 2 Store __discard Const str parent Const str alice Const str bob CallHost fact 3 Store __discard Const str parent Const str alice Const str carol CallHost fact 3 Store __discard Const str alice children: Const str parent Const str alice Const num 0 CallHost query 3 Bin + CallHost print 1 Store __discard Const str Person Const str kim CallHost new 2 Store __discard Const str kim Const str set Const str age Const num 42 CallHost send 4 Store __discard Const str kim age: Const str kim Const str age CallHost get 2 Bin + CallHost print 1 Store __discard Const str doubled: Load nums Const str double Call map_list 2 Bin + CallHost print 1 Store __discard Const str evens: Load nums Const str is_even Call filter_list 2 Bin + CallHost print 1 Return

__closure_0

B@0 Const str event received: Load event_data Bin + CallHost print 1 Return