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
25aec454
Commit
25aec454
authored
Nov 30, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added basic block recognizer.
parent
58e3df8c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
0 deletions
+66
-0
src/basic_block.py
src/basic_block.py
+44
-0
src/optimize.py
src/optimize.py
+22
-0
No files found.
src/basic_block.py
0 → 100644
View file @
25aec454
# 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
src/optimize.py
0 → 100755
View file @
25aec454
#!/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
'
\
n
basic block %d:'
%
i
for
line
in
block
:
print
line
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