Commit 2f461f59 authored by Taddeus Kroes's avatar Taddeus Kroes

Added string representation to Block class.

parent caf3ff63
...@@ -10,7 +10,7 @@ class Statement: ...@@ -10,7 +10,7 @@ class Statement:
self.args = list(args) self.args = list(args)
self.options = kwargs self.options = kwargs
# Assign a unique ID to each satement # Assign a unique ID to each statement
self.sid = Statement.sid self.sid = Statement.sid
Statement.sid += 1 Statement.sid += 1
...@@ -65,7 +65,7 @@ class Statement: ...@@ -65,7 +65,7 @@ class Statement:
return self.is_command() \ return self.is_command() \
and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \ and re.match('^beq|bne|blez|bgtz|bltz|bgez|bct|bcf$', \
self.name) self.name)
def is_branch_zero(self): def is_branch_zero(self):
"""Check if statement is a branch that compares with zero.""" """Check if statement is a branch that compares with zero."""
return self.is_command() \ return self.is_command() \
...@@ -79,12 +79,12 @@ class Statement: ...@@ -79,12 +79,12 @@ class Statement:
"""Check if the statement is a load instruction.""" """Check if the statement is a load instruction."""
return self.is_command() and self.name in ['lw', 'li', 'dlw', 'l.s', \ return self.is_command() and self.name in ['lw', 'li', 'dlw', 'l.s', \
'l.d'] 'l.d']
def is_store(self): def is_store(self):
"""Check if the statement is a store instruction.""" """Check if the statement is a store instruction."""
return self.is_command() and self.name in ['sw', 's.d', 'dsw', 's.s', \ return self.is_command() and self.name in ['sw', 's.d', 'dsw', 's.s', \
's.b'] 's.b']
def is_arith(self): def is_arith(self):
"""Check if the statement is an arithmetic operation.""" """Check if the statement is an arithmetic operation."""
return self.is_command() \ return self.is_command() \
...@@ -101,7 +101,7 @@ class Statement: ...@@ -101,7 +101,7 @@ class Statement:
def is_binop(self): def is_binop(self):
"""Check if the statement is an binary operation.""" """Check if the statement is an binary operation."""
return self.is_command() and len(self) == 3 and not self.is_jump() return self.is_command() and len(self) == 3 and not self.is_jump()
def is_load_non_immediate(self): def is_load_non_immediate(self):
"""Check if the statement is a load statement.""" """Check if the statement is a load statement."""
return self.is_command() \ return self.is_command() \
...@@ -110,48 +110,48 @@ class Statement: ...@@ -110,48 +110,48 @@ class Statement:
def is_logical(self): def is_logical(self):
"""Check if the statement is a logical operator.""" """Check if the statement is a logical operator."""
return self.is_command() and re.match('^(xor|or|and)i?$', self.name) return self.is_command() and re.match('^(xor|or|and)i?$', self.name)
def is_double_arithmetic(self): def is_double_arithmetic(self):
"""Check if the statement is a arithmetic .d operator.""" """Check if the statement is a arithmetic .d operator."""
return self.is_command() and \ return self.is_command() and \
re.match('^(add|sub|div|mul)\.d$', self.name) re.match('^(add|sub|div|mul)\.d$', self.name)
def is_double_unary(self): def is_double_unary(self):
"""Check if the statement is a unary .d operator.""" """Check if the statement is a unary .d operator."""
return self.is_command() and \ return self.is_command() and \
re.match('^(abs|neg|mov)\.d$', self.name) re.match('^(abs|neg|mov)\.d$', self.name)
def is_move_from_spec(self): def is_move_from_spec(self):
"""Check if the statement is a move from the result register.""" """Check if the statement is a move from the result register."""
return self.is_command() and self.name in ['mflo', 'mthi'] return self.is_command() and self.name in ['mflo', 'mthi']
def is_set_if_less(self): def is_set_if_less(self):
"""Check if the statement is a shift if less then.""" """Check if the statement is a shift if less then."""
return self.is_command() and self.name in ['slt', 'sltu'] return self.is_command() and self.name in ['slt', 'sltu']
def is_convert(self): def is_convert(self):
"""Check if the statement is a convert operator.""" """Check if the statement is a convert operator."""
return self.is_command() and re.match('^cvt\.[a-z\.]*$', self.name) return self.is_command() and re.match('^cvt\.[a-z\.]*$', self.name)
def is_truncate(self): def is_truncate(self):
"""Check if the statement is a convert operator.""" """Check if the statement is a convert operator."""
return self.is_command() and re.match('^trunc\.[a-z\.]*$', self.name) return self.is_command() and re.match('^trunc\.[a-z\.]*$', self.name)
def is_compare(self): def is_compare(self):
"""Check if the statement is a comparison.""" """Check if the statement is a comparison."""
return self.is_command() and re.match('^c\.[a-z\.]*$', self.name) return self.is_command() and re.match('^c\.[a-z\.]*$', self.name)
def jump_target(self): def jump_target(self):
"""Get the jump target of this statement.""" """Get the jump target of this statement."""
if not self.is_jump(): if not self.is_jump():
raise Exception('Command "%s" has no jump target' % self.name) raise Exception('Command "%s" has no jump target' % self.name)
return self[-1] return self[-1]
def get_def(self): def get_def(self):
"""Get the variable that this statement defines, if any.""" """Get the variable that this statement defines, if any."""
instr = ['move', 'addu', 'subu', 'li', 'mtc1', 'dmfc1', 'mov.d'] instr = ['move', 'addu', 'subu', 'li', 'mtc1', 'dmfc1', 'mov.d']
if self.is_load_non_immediate() or self.is_arith() \ if self.is_load_non_immediate() or self.is_arith() \
or self.is_logical() or self.is_double_arithmetic() \ or self.is_logical() or self.is_double_arithmetic() \
or self.is_move_from_spec() or self.is_double_unary() \ or self.is_move_from_spec() or self.is_double_unary() \
...@@ -184,7 +184,7 @@ class Statement: ...@@ -184,7 +184,7 @@ class Statement:
or self.is_command(*instr): or self.is_command(*instr):
use.append(self[1]) use.append(self[1])
# Case arg1 relative adressing # Case arg1 relative adressing
if self.is_load_non_immediate() or self.is_store(): if self.is_load_non_immediate() or self.is_store():
m = re.match('^\d+\(([^)]+)\)$', self[1]) m = re.match('^\d+\(([^)]+)\)$', self[1])
if m: if m:
use.append(m) use.append(m)
...@@ -208,10 +208,22 @@ class Statement: ...@@ -208,10 +208,22 @@ class Statement:
class Block: class Block:
bid = 1
def __init__(self, statements=[]): def __init__(self, statements=[]):
self.statements = statements self.statements = statements
self.pointer = 0 self.pointer = 0
# Assign a unique ID to each block for printing purposes
self.bid = Block.bid
Block.bid += 1
def __str__(self):
return '<Block bid=%d statements=%d>' % (self.bid, len(self))
def __repr__(self):
return str(self)
def __iter__(self): def __iter__(self):
return iter(self.statements) return iter(self.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