Commit 9a45fd87 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Renamed 'lines' variables to 'statements'.

parent 25aec454
# TODO: Get all jump commands
JUMP_COMMANDS = ['j', 'jal']
def is_jump(line):
def is_jump(statement):
'''Check if a statement is a jump command.'''
return line[0] == 'command' and line[1] in JUMP_COMMANDS
return statement[0] == 'command' and statement[1] in JUMP_COMMANDS
def find_leaders(lines):
def find_leaders(statements):
'''Determine the leaders, which are:
1. The first statement.
2. Any statement that is the target of a jump.
......@@ -14,31 +14,31 @@ def find_leaders(lines):
jump_target_labels = []
# Append statements following jumps and save jump target labels
for i, line in enumerate(lines[1:]):
if is_jump(line):
for i, statement in enumerate(statements[1:]):
if is_jump(statement):
leaders.append(i + 2)
jump_target_labels.append(line[2]['args'][0])
#print 'found jump:', i, line
jump_target_labels.append(statement[2]['args'][0])
#print 'found jump:', i, statement
#print 'target labels:', jump_target_labels
# Append jump targets
for i, line in enumerate(lines[1:]):
for i, statement in enumerate(statements[1:]):
if i + 1 not in leaders \
and line[0] == 'label' \
and line[1] in jump_target_labels:
and statement[0] == 'label' \
and statement[1] in jump_target_labels:
leaders.append(i + 1)
#print 'target:', i + 1, lines[i + 1]
#print 'target:', i + 1, statements[i + 1]
return leaders
def find_basic_blocks(lines):
def find_basic_blocks(statements):
'''Divide a statement list into basic blocks. Returns a list of basic
blocks, which are also statement lists.'''
leaders = find_leaders(lines)
leaders = find_leaders(statements)
blocks = []
for i in range(len(leaders) - 1):
blocks.append(lines[leaders[i]:leaders[i + 1]])
blocks.append(statements[leaders[i]:leaders[i + 1]])
return blocks
......@@ -9,14 +9,14 @@ if __name__ == '__main__':
print 'Usage: python %s FILE' % argv[0]
exit(1)
lines = parse_file(argv[1])
blocks = find_basic_blocks(lines)
statements = parse_file(argv[1])
blocks = find_basic_blocks(statements)
#for i, line in enumerate(lines):
# print i, line
#for i, statement in enumerate(statements):
# print i, statement
for i, block in enumerate(blocks):
print '\nbasic block %d:' % i
for line in block:
print line
for statement in block:
print statement
import ply.lex as lex
import ply.yacc as yacc
# Global lines administration
lines = []
# Global statements administration
statements = []
tokens = ('NEWLINE', 'WORD', 'COMMENT', 'DIRECTIVE', 'COMMA', 'COLON')
......@@ -61,11 +61,11 @@ def p_line_instruction(p):
def p_line_comment(p):
'line : COMMENT NEWLINE'
lines.append(('comment', p[1], {'inline': False}))
statements.append(('comment', p[1], {'inline': False}))
def p_line_inline_comment(p):
'line : instruction COMMENT NEWLINE'
lines.append(('comment', p[2], {'inline': True}))
statements.append(('comment', p[2], {'inline': True}))
def p_instruction_command(p):
'instruction : command'
......@@ -73,17 +73,17 @@ def p_instruction_command(p):
def p_instruction_directive(p):
'instruction : DIRECTIVE'
lines.append(('directive', p[1], {}))
statements.append(('directive', p[1], {}))
def p_instruction_label(p):
'instruction : WORD COLON'
lines.append(('label', p[1], {}))
statements.append(('label', p[1], {}))
def p_command(p):
'''command : WORD WORD COMMA WORD COMMA WORD
| WORD WORD COMMA WORD
| WORD WORD'''
lines.append(('command', p[1], {'args': list(p)[2::2]}))
statements.append(('command', p[1], {'args': list(p)[2::2]}))
def p_error(p):
print 'Syntax error at "%s" on line %d' % (p.value, lexer.lineno)
......@@ -91,9 +91,9 @@ def p_error(p):
yacc.yacc()
def parse_file(filename):
global lines
global statements
lines = []
statements = []
try:
content = open(filename).read()
......@@ -101,4 +101,4 @@ def parse_file(filename):
except IOError:
print 'File "%s" could not be opened' % filename
return lines
return statements
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