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
cbd8597b
Commit
cbd8597b
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Started implementing Constant Folding.
parent
ad32fbbc
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/__init__.py
+1
-1
1 addition, 1 deletion
src/optimize/__init__.py
src/optimize/advanced.py
+37
-0
37 additions, 0 deletions
src/optimize/advanced.py
with
38 additions
and
1 deletion
src/optimize/__init__.py
+
1
−
1
View file @
cbd8597b
...
@@ -61,7 +61,7 @@ def optimize_block(block):
...
@@ -61,7 +61,7 @@ def optimize_block(block):
pass
pass
def
optimize
(
statements
,
verbose
=
0
):
def
optimize
(
statements
,
verbose
=
0
):
"""
o
ptimization wrapper function, calls global and basic-block level
"""
O
ptimization wrapper function, calls global and basic-block level
optimization functions.
"""
optimization functions.
"""
# Optimize on a global level
# Optimize on a global level
o
=
len
(
statements
)
o
=
len
(
statements
)
...
...
This diff is collapsed.
Click to expand it.
src/optimize/advanced.py
+
37
−
0
View file @
cbd8597b
from
src.statement
import
Statement
as
S
from
src.statement
import
Statement
as
S
def
create_variable
():
return
'
$15
'
def
eliminate_common_subexpressions
(
block
):
def
eliminate_common_subexpressions
(
block
):
"""
"""
Common subexpression elimination:
Common subexpression elimination:
...
@@ -21,6 +25,7 @@ def eliminate_common_subexpressions(block):
...
@@ -21,6 +25,7 @@ def eliminate_common_subexpressions(block):
if
s
.
is_arith
():
if
s
.
is_arith
():
pointer
=
block
.
pointer
pointer
=
block
.
pointer
last
=
False
last
=
False
new_reg
=
False
args
=
s
[
1
:]
args
=
s
[
1
:]
# Collect similar statements
# Collect similar statements
...
@@ -33,6 +38,9 @@ def eliminate_common_subexpressions(block):
...
@@ -33,6 +38,9 @@ def eliminate_common_subexpressions(block):
# Replace a similar expression by a move instruction
# Replace a similar expression by a move instruction
if
s2
.
name
==
s
.
name
and
s2
[
1
:]
==
args
:
if
s2
.
name
==
s
.
name
and
s2
[
1
:]
==
args
:
if
not
new_reg
:
new_reg
=
create_variable
()
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
move
'
,
s2
[
0
],
new_reg
)])
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
move
'
,
s2
[
0
],
new_reg
)])
last
=
block
.
pointer
last
=
block
.
pointer
...
@@ -49,8 +57,37 @@ def eliminate_common_subexpressions(block):
...
@@ -49,8 +57,37 @@ def eliminate_common_subexpressions(block):
return
found
return
found
def
to_hex
(
value
):
"""
Create the hexadecimal string of an integer.
"""
return
'
0x%08x
'
%
value
def
fold_constants
(
block
):
def
fold_constants
(
block
):
"""
"""
Constant folding:
Constant folding:
"""
"""
constants
=
{}
while
not
block
.
end
():
s
=
block
.
read
()
if
s
.
is_load
():
constants
[
s
[
0
]]
=
s
[
1
]
elif
s
.
is_command
()
and
len
(
s
)
==
3
:
d
,
s
,
t
=
s
if
s
in
constants
and
t
in
constants
:
if
s
.
name
==
'
addu
'
:
result
=
s
+
t
elif
s
.
name
==
'
subu
'
:
result
=
s
-
t
elif
s
.
name
==
'
mult
'
:
result
=
s
*
t
elif
s
.
name
==
'
div
'
:
result
=
s
/
t
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
li
'
,
to_hex
(
result
))])
constants
[
d
]
=
result
#else:
return
False
return
False
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