-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_block.py
39 lines (32 loc) · 1.02 KB
/
basic_block.py
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
36
37
38
39
"""
bril2json < *.bril | python basic_block.py
"""
import json
import sys
TERMINATORS = ['jmp', 'ret', 'br']
def form_basic_blocks(instrs):
"""
A generator that generates basic blocks from an input function region.
"""
curr_block = []
for instr in instrs:
if 'op' in instr: # instr is a an actual instruction
curr_block.append(instr)
if instr['op'] in TERMINATORS:
yield curr_block
curr_block = []
else: # instr is a label, label is the entry of basic block
# the current basic block ends naturally
yield curr_block
# We also want to include the label on the top of next block
curr_block = [instr]
# implicit return: no ret at the end of function
yield curr_block
def main():
prog = json.load(sys.stdin)
for func in prog['functions']:
blocks = form_basic_blocks(func['instrs'])
for block in blocks:
print(block)
if __name__ == "__main__":
main()