-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-cfg.rkt
35 lines (29 loc) · 939 Bytes
/
build-cfg.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#lang racket
(require graph
"utilities.rkt")
(provide build-cfg)
(define (build-cfg p)
(match p
[(ProgramDefs info def*)
(ProgramDefs info (map bc-def def*))]))
(define (bc-def d)
(match d
[(Def name param* rty info label-block*)
(let* ([labels (dict-keys label-block*)]
[g (directed-graph '())]
[_ (for ([v labels]) (add-vertex! g v))]
[_ (map (add-edges! g) label-block*)])
(printf "CFG : ~a\n" (graphviz g))
(Def name param* rty (dict-set info 'cfg g) label-block*))]))
(define ((add-edges! g) label-block)
(match (cdr label-block)
[(Block info instrs)
(map (add-edges-instr! g (car label-block)) instrs)]
[else (error "add-edges! unhandled case " label-block)]))
(define ((add-edges-instr! g c) instr)
(match instr
[(Jmp l)
(add-directed-edge! g c l)]
[(JmpIf _ l)
(add-directed-edge! g c l)]
[else (void)]))