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
# 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
(
line
s
):
def
find_leaders
(
statement
s
):
'''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
(
line
s
[
1
:]):
for
i
,
statement
in
enumerate
(
statement
s
[
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
(
line
s
[
1
:]):
for
i
,
statement
in
enumerate
(
statement
s
[
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,
line
s[i + 1]
#print 'target:', i + 1,
statement
s[i + 1]
return
leaders
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
'''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
(
line
s
)
leaders
=
find_leaders
(
statement
s
)
blocks
=
[]
blocks
=
[]
for
i
in
range
(
len
(
leaders
)
-
1
):
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
return
blocks
src/optimize.py
View file @
9a45fd87
...
@@ -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
)
line
s
=
parse_file
(
argv
[
1
])
statement
s
=
parse_file
(
argv
[
1
])
blocks
=
find_basic_blocks
(
line
s
)
blocks
=
find_basic_blocks
(
statement
s
)
#for i,
line in enumerate(line
s):
#for i,
statement in enumerate(statement
s):
# print i,
line
# print i,
statement
for
i
,
block
in
enumerate
(
blocks
):
for
i
,
block
in
enumerate
(
blocks
):
print
'
\
n
basic block %d:'
%
i
print
'
\
n
basic block %d:'
%
i
for
line
in
block
:
for
statement
in
block
:
print
line
print
statement
src/parser.py
View file @
9a45fd87
import
ply.lex
as
lex
import
ply.lex
as
lex
import
ply.yacc
as
yacc
import
ply.yacc
as
yacc
# Global
line
s administration
# Global
statement
s administration
line
s
=
[]
statement
s
=
[]
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
'
line
s.append(('
comment
', p[1], {'
inline
': False}))
statement
s.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
'
line
s.append(('
comment
', p[2], {'
inline
': True}))
statement
s.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
'
line
s.append(('
directive
', p[1], {}))
statement
s.append(('
directive
', p[1], {}))
def p_instruction_label(p):
def p_instruction_label(p):
'
instruction
:
WORD
COLON
'
'
instruction
:
WORD
COLON
'
line
s.append(('
label
', p[1], {}))
statement
s.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'''
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):
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
line
s
global
statement
s
line
s = []
statement
s = []
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
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