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
1af3567d
Commit
1af3567d
authored
Dec 27, 2011
by
Jayke Meijer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added optimization of bne.
parent
13966882
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
11 deletions
+55
-11
src/optimize.py
src/optimize.py
+16
-1
src/parser.py
src/parser.py
+1
-1
src/writer.py
src/writer.py
+8
-6
tests/test_optimize.py
tests/test_optimize.py
+30
-3
No files found.
src/optimize.py
View file @
1af3567d
...
...
@@ -81,6 +81,20 @@ def optimize_global(statements):
s
.
name
=
'bne'
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
# bne ..., $Lx -> beq ..., $Ly
# j $Ly $Lx:
# $Lx:
if
s
.
is_command
(
'bne'
):
following
=
statements
.
peek
(
2
)
if
len
(
following
)
==
2
:
j
,
label
=
following
if
j
.
is_command
(
'j'
)
and
label
.
is_label
(
s
[
2
]):
s
.
name
=
'beq'
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
def
optimize_blocks
(
blocks
):
...
...
@@ -127,7 +141,8 @@ def optimize(statements, verbose=0):
# Optimize basic blocks
basic_blocks
=
find_basic_blocks
(
statements
)
blocks
=
optimize_blocks
(
basic_blocks
)
# blocks = optimize_blocks(basic_blocks)
blocks
=
basic_blocks
block_statements
=
map
(
lambda
b
:
b
.
statements
,
blocks
)
opt_blocks
=
reduce
(
lambda
a
,
b
:
a
+
b
,
block_statements
)
b
=
len
(
opt_blocks
)
...
...
src/parser.py
View file @
1af3567d
...
...
@@ -32,7 +32,7 @@ def t_DIRECTIVE(t):
return t
def t_hex_word(t):
r'
0
x
[
0
-
9
a
-
fA
-
F
]{
8
}
'
r'
0
x
([
0
-
9
a
-
fA
-
F
]{
8
}
|
[
0
-
9
a
-
fA
-
F
]{
4
})
'
t.type = '
WORD
'
return t
...
...
src/writer.py
View file @
1af3567d
...
...
@@ -12,13 +12,15 @@ def write_statements(statements):
if
s
.
is_label
():
line
=
s
.
name
+
':'
indent_level
=
1
elif
s
.
is_inline_comment
():
line
=
'#'
+
s
.
name
l
=
len
(
prevline
.
expandtabs
(
4
))
tabs
=
int
(
ceil
((
24
-
l
)
/
4.
))
+
1
newline
=
'
\
t
'
*
tabs
elif
s
.
is_comment
():
line
=
'
\
t
'
*
indent_level
+
line
line
=
'#'
+
s
.
name
if
s
.
is_inline_comment
():
l
=
len
(
prevline
.
expandtabs
(
4
))
tabs
=
int
(
ceil
((
24
-
l
)
/
4.
))
+
1
newline
=
'
\
t
'
*
tabs
else
:
line
=
'
\
t
'
*
indent_level
+
line
elif
s
.
is_directive
():
line
=
'
\
t
'
+
s
.
name
elif
s
.
is_command
():
...
...
tests/test_optimize.py
View file @
1af3567d
...
...
@@ -184,9 +184,6 @@ class TestOptimize(unittest.TestCase):
self
.
assertEquals
(
block2
.
statements
,
arguments2
)
self
.
assertEquals
(
block3
.
statements
,
arguments3
)
# beq ..., $Lx -> bne ..., $Ly
# j $Ly $Lx:
# $Lx:
def
test_optimize_global_beq_j_true
(
self
):
foo
=
S
(
'command'
,
'foo'
)
bar
=
S
(
'command'
,
'bar'
)
...
...
@@ -216,3 +213,33 @@ class TestOptimize(unittest.TestCase):
optimize_global
(
block
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
def
test_optimize_global_bne_j_true
(
self
):
foo
=
S
(
'command'
,
'foo'
)
bar
=
S
(
'command'
,
'bar'
)
block
=
B
([
foo
,
S
(
'command'
,
'bne'
,
'$regA'
,
'$regB'
,
'$Lx'
),
S
(
'command'
,
'j'
,
'$Ly'
),
S
(
'label'
,
'$Lx'
),
bar
])
optimize_global
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
S
(
'command'
,
'beq'
,
'$regA'
,
'$regB'
,
'$Ly'
),
S
(
'label'
,
'$Lx'
),
bar
])
def
test_optimize_global_bne_j_false
(
self
):
foo
=
S
(
'command'
,
'foo'
)
bar
=
S
(
'command'
,
'bar'
)
arguments
=
[
foo
,
\
S
(
'command'
,
'bne'
,
'$regA'
,
'$regB'
,
'$Lz'
),
\
S
(
'command'
,
'j'
,
'$Ly'
),
\
S
(
'label'
,
'$Lx'
),
\
bar
]
block
=
B
(
arguments
)
optimize_global
(
block
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
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