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

Renamed 'lines' variables to 'statements'.

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