diff --git a/images/cfg.dot b/images/cfg.dot new file mode 100644 index 0000000..16e05db --- /dev/null +++ b/images/cfg.dot @@ -0,0 +1,19 @@ +digraph "CFG" { + nodesep=0.6 + node [shape=box, fontname="Roboto Mono", fontsize=11] + + B1 [xlabel="B1", label="s = 0\li = 0\ln = 10\l"]; + B2 [xlabel="B2", label="t1 = a - b\lifz t1 goto B4\l"]; + B3 [xlabel="B3", label="t2 = i * 4\ls = s + t2\l"]; + B4 [xlabel="B4", label="s = s + i"]; + B5 [xlabel="B5", label="i = i + 1\lt3 = n - i\lifnz t3 goto B2\l"]; + B6 [xlabel="B6", label="t4 = a - b"]; + + B1 -> B2; + B2 -> B3 [label="F"]; + B2 -> B4 [label="T"]; + B3 -> B5; + B4 -> B5; + B5 -> B6 [label="F"]; + B5 -> B2 [label="T"]; +} diff --git a/images/cfg.png b/images/cfg.png new file mode 100644 index 0000000..fcb5170 Binary files /dev/null and b/images/cfg.png differ diff --git a/specification.md b/specification.md index 8cb278f..51565cd 100644 --- a/specification.md +++ b/specification.md @@ -370,6 +370,31 @@ This graph is commonly used by analyses for extracting structural information cr It is recommended to also provide a visitor mechanism for this graph. +Like the AST, it can be visualised. +The example below is taken from [Marc Moreno Maza](http://www.csd.uwo.ca/~moreno/CS447/Lectures/CodeOptimization.html/node6.html). + +Given this example IR: + +``` + s = 0 + i = 0 + n = 10 +L1: t1 = a - b + ifz t1 goto L2 + t2 = i * 4 + s = s + t2 + goto L3 +L2: s = s + i +L3: i = i + 1 + t3 = n - i + ifnz t3 goto L1 + t4 = a - b +``` + +The visualisation of the corresponding CFG could look like this: + +![CFG Example](images/cfg.png) + ### Assembly Code Generation The mC compiler targets x86 and uses GCC as back-end compiler.