Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
peephole
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
peephole
Commits
fd60f13d
Commit
fd60f13d
authored
Dec 30, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed infinite loop in optimization phase.
parent
e0b4e598
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
32 additions
and
20 deletions
+32
-20
main.py
main.py
+1
-1
src/optimize/__init__.py
src/optimize/__init__.py
+12
-1
src/optimize/advanced.py
src/optimize/advanced.py
+3
-3
src/optimize/redundancies.py
src/optimize/redundancies.py
+9
-7
src/program.py
src/program.py
+3
-4
src/statement.py
src/statement.py
+4
-4
No files found.
main.py
View file @
fd60f13d
...
@@ -13,7 +13,7 @@ if __name__ == '__main__':
...
@@ -13,7 +13,7 @@ if __name__ == '__main__':
# Parse file
# Parse file
program
=
parse_file
(
argv
[
1
])
program
=
parse_file
(
argv
[
1
])
program
.
debug
=
True
program
.
verbose
=
1
if
len
(
argv
)
>
3
:
if
len
(
argv
)
>
3
:
# Save input assembly in new file for easy comparison
# Save input assembly in new file for easy comparison
...
...
src/optimize/__init__.py
View file @
fd60f13d
...
@@ -12,13 +12,21 @@ def optimize(program, verbose=0):
...
@@ -12,13 +12,21 @@ def optimize(program, verbose=0):
o
=
program
.
count_instructions
()
o
=
program
.
count_instructions
()
changed
=
True
changed
=
True
iterations
=
0
while
changed
:
while
changed
:
print
'main iteration'
iterations
+=
1
if
verbose
>
1
:
print
'main iteration %d'
,
iterations
changed
=
False
changed
=
False
# Optimize on a global level
# Optimize on a global level
if
program
.
optimize_global
():
if
program
.
optimize_global
():
if
verbose
>
1
:
print
'changed on global level'
changed
=
True
changed
=
True
# Perform dataflow analysis on new blocks
# Perform dataflow analysis on new blocks
...
@@ -26,6 +34,9 @@ def optimize(program, verbose=0):
...
@@ -26,6 +34,9 @@ def optimize(program, verbose=0):
# Optimize basic blocks
# Optimize basic blocks
if
program
.
optimize_blocks
():
if
program
.
optimize_blocks
():
if
verbose
>
1
:
print
'changed on block level'
changed
=
True
changed
=
True
# Count number of instructions after optimization
# Count number of instructions after optimization
...
...
src/optimize/advanced.py
View file @
fd60f13d
...
@@ -274,7 +274,7 @@ def fold_constants(block):
...
@@ -274,7 +274,7 @@ def fold_constants(block):
del
register
[
reg
]
del
register
[
reg
]
known
.
append
((
reg
,
'unknown'
))
known
.
append
((
reg
,
'unknown'
))
if
block
.
debug
and
len
(
known
):
if
block
.
verbose
and
len
(
known
):
s
.
set_inline_comment
(
','
.
join
([
' %s = %s'
%
k
for
k
in
known
]))
s
.
set_inline_comment
(
','
.
join
([
' %s = %s'
%
k
for
k
in
known
]))
return
changed
return
changed
...
@@ -401,7 +401,7 @@ def eliminate_dead_code(block):
...
@@ -401,7 +401,7 @@ def eliminate_dead_code(block):
for
reg
in
s
.
get_def
():
for
reg
in
s
.
get_def
():
if
is_reg_dead_after
(
reg
,
block
,
n
):
if
is_reg_dead_after
(
reg
,
block
,
n
):
# Statement is redefined later, so this statement is useless
# Statement is redefined later, so this statement is useless
if
block
.
debug
:
if
block
.
verbose
:
s
.
stype
=
'comment'
s
.
stype
=
'comment'
s
.
options
[
'block'
]
=
False
s
.
options
[
'block'
]
=
False
s
.
set_inline_comment
(
' dead register %s'
%
reg
)
s
.
set_inline_comment
(
' dead register %s'
%
reg
)
...
@@ -411,7 +411,7 @@ def eliminate_dead_code(block):
...
@@ -411,7 +411,7 @@ def eliminate_dead_code(block):
changed
=
True
changed
=
True
if
not
block
.
debug
:
if
not
block
.
verbose
:
block
.
apply_filter
(
lambda
s
:
not
hasattr
(
s
,
'remove'
))
block
.
apply_filter
(
lambda
s
:
not
hasattr
(
s
,
'remove'
))
return
changed
return
changed
src/optimize/redundancies.py
View file @
fd60f13d
...
@@ -3,7 +3,7 @@ import re
...
@@ -3,7 +3,7 @@ import re
def
remove_redundancies
(
block
):
def
remove_redundancies
(
block
):
"""Execute all functions that remove redundant statements."""
"""Execute all functions that remove redundant statements."""
callbacks
=
[
move_aa
,
move_inst
,
instr_move_jal
,
move_move
,
sw_ld
,
shift
,
callbacks
=
[
move_aa
,
move_inst
,
instr_move_jal
,
move_move
,
sw_ld
,
shift
,
add_lw
]
add_lw
]
old_len
=
-
1
old_len
=
-
1
changed
=
False
changed
=
False
...
@@ -126,8 +126,9 @@ def add_lw(add, statements):
...
@@ -126,8 +126,9 @@ def add_lw(add, statements):
def
remove_redundant_jumps
(
statements
):
def
remove_redundant_jumps
(
statements
):
"""Remove jump if label follows immediately."""
"""Remove jump if label follows immediately."""
changed
=
False
changed
=
False
statements
.
reset
()
statements
.
reset
()
while
not
statements
.
end
():
while
not
statements
.
end
():
s
=
statements
.
read
()
s
=
statements
.
read
()
...
@@ -135,18 +136,19 @@ def remove_redundant_jumps(statements):
...
@@ -135,18 +136,19 @@ def remove_redundant_jumps(statements):
# $Lx:
# $Lx:
if
s
.
is_command
(
'j'
):
if
s
.
is_command
(
'j'
):
following
=
statements
.
peek
()
following
=
statements
.
peek
()
if
following
.
is_label
(
s
[
0
]):
if
following
.
is_label
(
s
[
0
]):
statements
.
replace
(
1
,
[])
statements
.
replace
(
1
,
[])
changed
=
True
changed
=
True
return
True
return
changed
def
remove_redundant_branch_jumps
(
statements
):
def
remove_redundant_branch_jumps
(
statements
):
"""Optimize statement sequences on a global level."""
"""Optimize statement sequences on a global level."""
changed
=
False
changed
=
False
statements
.
reset
()
statements
.
reset
()
while
not
statements
.
end
():
while
not
statements
.
end
():
s
=
statements
.
read
()
s
=
statements
.
read
()
...
@@ -164,5 +166,5 @@ def remove_redundant_branch_jumps(statements):
...
@@ -164,5 +166,5 @@ def remove_redundant_branch_jumps(statements):
s
[
2
]
=
j
[
0
]
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
statements
.
replace
(
3
,
[
s
,
label
])
changed
=
True
changed
=
True
return
changed
return
changed
src/program.py
View file @
fd60f13d
...
@@ -21,8 +21,8 @@ class Program(Block):
...
@@ -21,8 +21,8 @@ class Program(Block):
if
hasattr
(
self
,
'statements'
):
if
hasattr
(
self
,
'statements'
):
return
self
.
statements
return
self
.
statements
# Only add block start and end comments when in
debug
mode
# Only add block start and end comments when in
verbose
mode
if
add_block_comments
and
self
.
debug
:
if
add_block_comments
and
self
.
verbose
:
get_id
=
lambda
b
:
b
.
bid
get_id
=
lambda
b
:
b
.
bid
statements
=
[]
statements
=
[]
...
@@ -75,7 +75,6 @@ class Program(Block):
...
@@ -75,7 +75,6 @@ class Program(Block):
changed
=
False
changed
=
False
for
block
in
self
.
blocks
:
for
block
in
self
.
blocks
:
print
'block iteration'
if
remove_redundancies
(
block
)
\
if
remove_redundancies
(
block
)
\
|
eliminate_common_subexpressions
(
block
)
\
|
eliminate_common_subexpressions
(
block
)
\
|
fold_constants
(
block
)
\
|
fold_constants
(
block
)
\
...
@@ -90,7 +89,7 @@ class Program(Block):
...
@@ -90,7 +89,7 @@ class Program(Block):
self
.
blocks
=
find_basic_blocks
(
self
.
statements
)
self
.
blocks
=
find_basic_blocks
(
self
.
statements
)
for
b
in
self
.
blocks
:
for
b
in
self
.
blocks
:
b
.
debug
=
self
.
debug
b
.
verbose
=
self
.
verbose
# Remove the old statement list, since it will probably change
# Remove the old statement list, since it will probably change
del
self
.
statements
del
self
.
statements
...
...
src/statement.py
View file @
fd60f13d
...
@@ -231,7 +231,7 @@ class Statement:
...
@@ -231,7 +231,7 @@ class Statement:
class Block:
class Block:
bid = 1
bid = 1
def __init__(self, statements=[],
debug
=False):
def __init__(self, statements=[],
verbose
=False):
self.statements = statements
self.statements = statements
self.pointer = 0
self.pointer = 0
...
@@ -239,7 +239,7 @@ class Block:
...
@@ -239,7 +239,7 @@ class Block:
self.bid = Block.bid
self.bid = Block.bid
Block.bid += 1
Block.bid += 1
self.
debug = debug
self.
verbose = verbose
def __str__(self):
def __str__(self):
return '
<
Block
bid
=%
d
statements
=%
d
>
' % (self.bid, len(self))
return '
<
Block
bid
=%
d
statements
=%
d
>
' % (self.bid, len(self))
...
@@ -288,7 +288,7 @@ class Block:
...
@@ -288,7 +288,7 @@ class Block:
start = self.pointer - 1
start = self.pointer - 1
# Add a message in inline comments
# Add a message in inline comments
if self.
debug
:
if self.
verbose
:
if len(message):
if len(message):
message = '
' + message
message = '
' + message
...
@@ -313,7 +313,7 @@ class Block:
...
@@ -313,7 +313,7 @@ class Block:
if index == None:
if index == None:
index = self.pointer
index = self.pointer
if self.
debug
and len(message):
if self.
verbose
and len(message):
statement.set_inline_comment('
' + message)
statement.set_inline_comment('
' + message)
self.statements.insert(index, statement)
self.statements.insert(index, statement)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment