Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
b8782c99
Commit
b8782c99
authored
Dec 13, 2008
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial constant folding transform: calculate constant values in node.constant_result
parent
fb4ea137
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
190 additions
and
16 deletions
+190
-16
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+140
-15
Cython/Compiler/Main.py
Cython/Compiler/Main.py
+2
-1
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+48
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
b8782c99
This diff is collapsed.
Click to expand it.
Cython/Compiler/Main.py
View file @
b8782c99
...
...
@@ -83,7 +83,7 @@ class Context:
from
ParseTreeTransforms
import
AlignFunctionDefinitions
from
AutoDocTransforms
import
EmbedSignature
from
Optimize
import
FlattenInListTransform
,
SwitchTransform
,
DictIterTransform
from
Optimize
import
FlattenBuiltinTypeCreation
,
FinalOptimizePhase
from
Optimize
import
FlattenBuiltinTypeCreation
,
ConstantFolding
,
FinalOptimizePhase
from
Buffer
import
IntroduceBufferAuxiliaryVars
from
ModuleNode
import
check_c_declarations
...
...
@@ -123,6 +123,7 @@ class Context:
IntroduceBufferAuxiliaryVars
(
self
),
_check_c_declarations
,
AnalyseExpressionsTransform
(
self
),
ConstantFolding
(),
FlattenBuiltinTypeCreation
(),
DictIterTransform
(),
SwitchTransform
(),
...
...
Cython/Compiler/Optimize.py
View file @
b8782c99
...
...
@@ -387,6 +387,54 @@ class FlattenBuiltinTypeCreation(Visitor.VisitorTransform):
return
node
class
ConstantFolding
(
Visitor
.
VisitorTransform
,
SkipDeclarations
):
"""Calculate the result of constant expressions to store it in
``expr_node.constant_result``, and replace trivial cases by their
constant result.
"""
def
_calculate_const
(
self
,
node
):
if
node
.
constant_result
is
not
ExprNodes
.
constant_value_not_set
:
return
# make sure we always set the value
not_a_constant
=
ExprNodes
.
not_a_constant
node
.
constant_result
=
not_a_constant
# check if all children are constant
children
=
self
.
visitchildren
(
node
)
for
child_result
in
children
.
itervalues
():
if
type
(
child_result
)
is
list
:
for
child
in
child_result
:
if
child
.
constant_result
is
not_a_constant
:
return
elif
child_result
.
constant_result
is
not_a_constant
:
return
# now try to calculate the real constant value
try
:
node
.
calculate_constant_result
()
# if node.constant_result is not ExprNodes.not_a_constant:
# print node.__class__.__name__, node.constant_result
except
(
ValueError
,
TypeError
,
IndexError
,
AttributeError
):
# ignore all 'normal' errors here => no constant result
pass
except
Exception
:
# this looks like a real error
import
traceback
,
sys
traceback
.
print_exc
(
file
=
sys
.
stdout
)
def
visit_ExprNode
(
self
,
node
):
self
.
_calculate_const
(
node
)
return
node
# in the future, other nodes can have their own handler method here
# that can replace them with a constant result node
def
visit_Node
(
self
,
node
):
self
.
visitchildren
(
node
)
return
node
class
FinalOptimizePhase
(
Visitor
.
CythonTransform
):
"""
This visitor handles several commuting optimizations, and is run
...
...
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