Provide CFG example

This commit is contained in:
Alex Hirsch 2019-01-30 11:25:52 +01:00
parent 6e0e1507de
commit 6e654bdc61
3 changed files with 44 additions and 0 deletions

19
images/cfg.dot Normal file
View File

@ -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"];
}

BIN
images/cfg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -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. 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 ### Assembly Code Generation
The mC compiler targets x86 and uses GCC as back-end compiler. The mC compiler targets x86 and uses GCC as back-end compiler.