Commit 25aec454 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added basic block recognizer.

parent 58e3df8c
# TODO: Get all jump commands
JUMP_COMMANDS = ['j', 'jal']
def is_jump(line):
'''Check if a statement is a jump command.'''
return line[0] == 'command' and line[1] in JUMP_COMMANDS
def find_leaders(lines):
'''Determine the leaders, which are:
1. The first statement.
2. Any statement that is the target of a jump.
3. Any statement that follows directly follows a jump.'''
leaders = [0]
jump_target_labels = []
# Append statements following jumps and save jump target labels
for i, line in enumerate(lines[1:]):
if is_jump(line):
leaders.append(i + 2)
jump_target_labels.append(line[2]['args'][0])
#print 'found jump:', i, line
#print 'target labels:', jump_target_labels
# Append jump targets
for i, line in enumerate(lines[1:]):
if i + 1 not in leaders \
and line[0] == 'label' \
and line[1] in jump_target_labels:
leaders.append(i + 1)
#print 'target:', i + 1, lines[i + 1]
return leaders
def find_basic_blocks(lines):
'''Divide a statement list into basic blocks. Returns a list of basic
blocks, which are also statement lists.'''
leaders = find_leaders(lines)
blocks = []
for i in range(len(leaders) - 1):
blocks.append(lines[leaders[i]:leaders[i + 1]])
return blocks
#!/usr/bin/python
from parser import parse_file
from basic_block import find_basic_blocks
if __name__ == '__main__':
from sys import argv, exit
if len(argv) < 2:
print 'Usage: python %s FILE' % argv[0]
exit(1)
lines = parse_file(argv[1])
blocks = find_basic_blocks(lines)
#for i, line in enumerate(lines):
# print i, line
for i, block in enumerate(blocks):
print '\nbasic block %d:' % i
for line in block:
print line
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment