1# Tool draw_cfg.py 2## Introduction 3 4`draw_cfg.py` is a tool for drawing IR CFGs, you can find it in `runtime_core/compiler/tools/`. 5 6 7## Dependencies 8 9To install `graphviz` in ubuntu: 10 11```shell 12sudo apt install graphviz 13pip3 install graphviz 14``` 15 16 17## Usage 18 19``` 20usage: draw_cfg.py [-h] [--no-insts] [--out OUT] 21 22A tool for drawing CFGs by reading ir dump from stdin 23 24options: 25 -h, --help show this help message and exit 26 --no-insts drawing without ir instructions 27 --out OUT output directory, default to './out' 28``` 29 30Example: 31 32Suppose you want to draw the IR CFG of following js code: 33 34```js 35function example(cond) { 36 let a = 47; 37 if (cond) { 38 a = a + a; 39 } else { 40 a = a + a; 41 } 42 43 print(a); 44} 45``` 46 47You should: 48 491. get ir dump from optimizer, as follows: 50 51``` 52Method: L_GLOBAL;::example 53 54BB 4 55prop: start, bc: 0x00000000 56 0.any Parameter arg 0 57 -> [u64] 58 1.any Parameter arg 1 59 -> [u64] 60 2.any Parameter arg 2 61 -> [u64] 62 3.any Parameter arg 3 -> (v5) 63 -> [u64] 64 4.i32 Constant 0x2f -> (v12, v12, v10, v10) 65 8.i64 Constant 0x0 -> (v7) 66succs: [bb 0] 67 68BB 0 preds: [bb 4] 69prop: bc: 0x00000000 70 6. SaveState -> (v5) bc: 0x00000011 71 5.any Intrinsic.isfalse v3, v6 -> (v7) bc: 0x00000011 72 7.b Compare NE any v5, v8 -> (v9) bc: 0x00000012 73 9. IfImm NE b v7, 0x0 bc: 0x00000012 74succs: [bb 1, bb 2] 75 76BB 2 preds: [bb 0] 77prop: bc: 0x00000014 78 11. SaveState -> (v10) bc: 0x0000001a 79 10.any Intrinsic.add2 v4, v4, v11 -> (v14p) bc: 0x0000001a 80succs: [bb 3] 81 82BB 1 preds: [bb 0] 83prop: bc: 0x00000021 84 13. SaveState -> (v12) bc: 0x00000027 85 12.any Intrinsic.add2 v4, v4, v13 -> (v14p) bc: 0x00000027 86succs: [bb 3] 87 88BB 3 preds: [bb 2, bb 1] 89prop: bc: 0x0000002c 90 14p.any Phi v10(bb2), v12(bb1) -> (v18) 91 17. SaveState -> (v16) bc: 0x0000002c 92 16.any Intrinsic.tryldglobalbyname v17 -> (v18) bc: 0x0000002c 93 19. SaveState -> (v18) bc: 0x00000038 94 18.any Intrinsic.callarg1 v14p, v16, v19 bc: 0x00000038 95 21. SaveState -> (v20) bc: 0x0000003b 96 20.any Intrinsic.ldundefined v21 bc: 0x0000003b 97 23. SaveState -> (v22) bc: 0x0000003c 98 22.void Intrinsic.returnundefined v23 bc: 0x0000003c 99succs: [bb 5] 100 101BB 5 preds: [bb 3] 102prop: end, bc: 0x0000003d 103``` 104 1052. use `draw_cfg.py` to draw CFG: 106 107```shell 108./draw_cfg.py --out out < your_ir_dump 109``` 110 111Output image is `./out/cfg_L_GLOBAL;::example.png`:  112