Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
peephole
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Taddeüs Kroes
peephole
Commits
b279082a
Commit
b279082a
authored
13 years ago
by
Jayke Meijer
Browse files
Options
Downloads
Patches
Plain Diff
Algebraic transformations unittested.
parent
12b1dc5e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/optimize/advanced.py
+10
-5
10 additions, 5 deletions
src/optimize/advanced.py
tests/test_optimize_advanced.py
+75
-0
75 additions, 0 deletions
tests/test_optimize_advanced.py
with
85 additions
and
5 deletions
src/optimize/advanced.py
+
10
−
5
View file @
b279082a
from
src.statement
import
Statement
as
S
from
src.statement
import
Statement
as
S
from
math
import
log
def
create_variable
():
def
create_variable
():
return
'
$15
'
return
'
$15
'
...
@@ -223,6 +223,7 @@ def algebraic_transformations(block):
...
@@ -223,6 +223,7 @@ def algebraic_transformations(block):
- x = x + 0 -> remove
- x = x + 0 -> remove
- x = x - 0 -> remove
- x = x - 0 -> remove
- x = x * 1 -> remove
- x = x * 1 -> remove
- x = x * 0 -> x = 0
- x = x * 2 -> x = x << 1
- x = x * 2 -> x = x << 1
"""
"""
changed
=
False
changed
=
False
...
@@ -236,10 +237,14 @@ def algebraic_transformations(block):
...
@@ -236,10 +237,14 @@ def algebraic_transformations(block):
elif
s
.
is_command
(
'
mult
'
)
and
s
[
2
]
==
1
:
elif
s
.
is_command
(
'
mult
'
)
and
s
[
2
]
==
1
:
block
.
replace
(
1
,
[])
block
.
replace
(
1
,
[])
changed
=
True
changed
=
True
elif
s
.
is_command
(
'
mult
'
)
and
s
[
2
]
==
2
:
elif
s
.
is_command
(
'
mult
'
)
and
s
[
2
]
==
0
:
new_command
=
S
([
'
command
'
,
'
sll
'
,
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
li
'
,
'
$1
'
,
to_hex
(
0
))])
s
[
0
],
s
[
1
],
1
])
block
.
replace
(
1
,
[
new_command
])
changed
=
True
changed
=
True
elif
s
.
is_command
(
'
mult
'
):
shift_amount
=
log
(
s
[
2
],
2
)
if
shift_amount
.
is_integer
():
new_command
=
S
(
'
command
'
,
'
sll
'
,
s
[
0
],
s
[
1
],
shift_amount
)
block
.
replace
(
1
,
[
new_command
])
changed
=
True
return
changed
return
changed
This diff is collapsed.
Click to expand it.
tests/test_optimize_advanced.py
+
75
−
0
View file @
b279082a
...
@@ -79,3 +79,78 @@ class TestOptimizeAdvanced(unittest.TestCase):
...
@@ -79,3 +79,78 @@ class TestOptimizeAdvanced(unittest.TestCase):
self
.
assertTrue
(
algebraic_transformations
(
block
))
self
.
assertTrue
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
self
.
bar
])
self
.
bar
])
def
test_algebraic_transforms_add1
(
self
):
arguments
=
[
self
.
foo
,
S
(
'
command
'
,
'
addu
'
,
'
$1
'
,
'
$2
'
,
1
),
self
.
bar
]
block
=
B
(
arguments
)
self
.
assertFalse
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
arguments
)
def
test_algebraic_transforms_sub0
(
self
):
block
=
B
([
self
.
foo
,
S
(
'
command
'
,
'
subu
'
,
'
$1
'
,
'
$2
'
,
0
),
self
.
bar
])
self
.
assertTrue
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
self
.
bar
])
def
test_algebraic_transforms_sub1
(
self
):
arguments
=
[
self
.
foo
,
S
(
'
command
'
,
'
subu
'
,
'
$1
'
,
'
$2
'
,
1
),
self
.
bar
]
block
=
B
(
arguments
)
self
.
assertFalse
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
arguments
)
def
test_algebraic_transforms_mult0
(
self
):
block
=
B
([
self
.
foo
,
S
(
'
command
'
,
'
mult
'
,
'
$1
'
,
'
$2
'
,
0
),
self
.
bar
])
self
.
assertTrue
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
command
'
,
'
li
'
,
'
$1
'
,
'
0x00000000
'
),
self
.
bar
])
def
test_algebraic_transforms_mult1
(
self
):
block
=
B
([
self
.
foo
,
S
(
'
command
'
,
'
mult
'
,
'
$1
'
,
'
$2
'
,
1
),
self
.
bar
])
self
.
assertTrue
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
self
.
bar
])
def
test_algebraic_transforms_mult2
(
self
):
block
=
B
([
self
.
foo
,
S
(
'
command
'
,
'
mult
'
,
'
$1
'
,
'
$2
'
,
2
),
self
.
bar
])
self
.
assertTrue
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
command
'
,
'
sll
'
,
'
$1
'
,
'
$2
'
,
1
),
self
.
bar
])
def
test_algebraic_transforms_mult16
(
self
):
block
=
B
([
self
.
foo
,
S
(
'
command
'
,
'
mult
'
,
'
$1
'
,
'
$2
'
,
16
),
self
.
bar
])
self
.
assertTrue
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
command
'
,
'
sll
'
,
'
$1
'
,
'
$2
'
,
4
),
self
.
bar
])
def
test_algebraic_transforms_mult3
(
self
):
arguments
=
[
self
.
foo
,
S
(
'
command
'
,
'
mult
'
,
'
$1
'
,
'
$2
'
,
3
),
self
.
bar
]
block
=
B
(
arguments
)
self
.
assertFalse
(
algebraic_transformations
(
block
))
self
.
assertEqual
(
block
.
statements
,
arguments
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment