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
9a45fd87
Commit
9a45fd87
authored
Nov 30, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed 'lines' variables to 'statements'.
parent
25aec454
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
30 deletions
+30
-30
src/basic_block.py
src/basic_block.py
+14
-14
src/optimize.py
src/optimize.py
+6
-6
src/parser.py
src/parser.py
+10
-10
No files found.
src/basic_block.py
View file @
9a45fd87
# 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
(
line
s
):
def
find_leaders
(
statement
s
):
'''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
(
line
s
[
1
:]):
if
is_jump
(
line
):
for
i
,
statement
in
enumerate
(
statement
s
[
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
(
line
s
[
1
:]):
for
i
,
statement
in
enumerate
(
statement
s
[
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,
line
s[i + 1]
#print 'target:', i + 1,
statement
s[i + 1]
return
leaders
def
find_basic_blocks
(
line
s
):
def
find_basic_blocks
(
statement
s
):
'''Divide a statement list into basic blocks. Returns a list of basic
blocks, which are also statement lists.'''
leaders
=
find_leaders
(
line
s
)
leaders
=
find_leaders
(
statement
s
)
blocks
=
[]
for
i
in
range
(
len
(
leaders
)
-
1
):
blocks
.
append
(
line
s
[
leaders
[
i
]:
leaders
[
i
+
1
]])
blocks
.
append
(
statement
s
[
leaders
[
i
]:
leaders
[
i
+
1
]])
return
blocks
src/optimize.py
View file @
9a45fd87
...
...
@@ -9,14 +9,14 @@ if __name__ == '__main__':
print
'Usage: python %s FILE'
%
argv
[
0
]
exit
(
1
)
line
s
=
parse_file
(
argv
[
1
])
blocks
=
find_basic_blocks
(
line
s
)
statement
s
=
parse_file
(
argv
[
1
])
blocks
=
find_basic_blocks
(
statement
s
)
#for i,
line in enumerate(line
s):
# print i,
line
#for i,
statement in enumerate(statement
s):
# print i,
statement
for
i
,
block
in
enumerate
(
blocks
):
print
'
\
n
basic block %d:'
%
i
for
line
in
block
:
print
line
for
statement
in
block
:
print
statement
src/parser.py
View file @
9a45fd87
import
ply.lex
as
lex
import
ply.yacc
as
yacc
# Global
line
s administration
line
s
=
[]
# Global
statement
s administration
statement
s
=
[]
tokens
=
(
'NEWLINE'
,
'WORD'
,
'COMMENT'
,
'DIRECTIVE'
,
'COMMA'
,
'COLON'
)
...
...
@@ -61,11 +61,11 @@ def p_line_instruction(p):
def p_line_comment(p):
'
line
:
COMMENT
NEWLINE
'
line
s.append(('
comment
', p[1], {'
inline
': False}))
statement
s.append(('
comment
', p[1], {'
inline
': False}))
def p_line_inline_comment(p):
'
line
:
instruction
COMMENT
NEWLINE
'
line
s.append(('
comment
', p[2], {'
inline
': True}))
statement
s.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
'
line
s.append(('
directive
', p[1], {}))
statement
s.append(('
directive
', p[1], {}))
def p_instruction_label(p):
'
instruction
:
WORD
COLON
'
line
s.append(('
label
', p[1], {}))
statement
s.append(('
label
', p[1], {}))
def p_command(p):
'''command : WORD WORD COMMA WORD COMMA WORD
| WORD WORD COMMA WORD
| WORD WORD'''
line
s.append(('
command
', p[1], {'
args
': list(p)[2::2]}))
statement
s.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
line
s
global
statement
s
line
s = []
statement
s = []
try:
content = open(filename).read()
...
...
@@ -101,4 +101,4 @@ def parse_file(filename):
except IOError:
print '
File
"%s"
could
not
be
opened
' % filename
return
line
s
return
statement
s
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