2 #include <boost/foreach.hpp>
4 #include <core/assert.hh>
5 #include "simplify-visitor.hh"
11 * @class SimplifyVisitor
14 * <b>Algorithm</b><br>
16 * - for every instruction
17 * - split the instruction in 3 address instruction
20 SimplifyVisitor::SimplifyVisitor()
28 SimplifyVisitor::~SimplifyVisitor()
33 SimplifyVisitor::nextId()
36 ss
<< "r" << ++nextId_
;
41 SimplifyVisitor::currentId() const
49 SimplifyVisitor::visit(Block
& block
)
51 /// @todo use a garbage collector
52 std::vector
<Node
*> * newList
= new std::vector
<Node
*>();
54 BOOST_FOREACH(Node
* node
, (*block
.instrs
))
57 simplifications_
.clear();
59 BOOST_FOREACH(Node
* snode
, simplifications_
)
60 newList
->push_back(snode
);
63 //delete block.instrs;
64 block
.instrs
= newList
;
67 #define SIMPLIFY_BINARY_EXP(Type) \
71 * - Create a new id. \
72 * - Create a new assign exp to id. \
73 * - Create a IdExp and tell replace the parent's child with it. \
76 SimplifyVisitor::visit(Type & node) \
79 node.left->accept(*this); \
80 node.left = replacement_; \
82 node.right->accept(*this); \
83 node.right = replacement_; \
85 std::string name = nextId(); \
87 TmpResultExp * tmpRes = new TmpResultExp(); \
88 tmpRes->name = name; \
89 replacement_ = tmpRes; \
91 AssignExp * assign = new AssignExp(); \
92 assign->name = name; \
93 assign->value = &node; \
94 simplifications_.push_back(assign); \
97 SIMPLIFY_BINARY_EXP(EqExp
)
98 SIMPLIFY_BINARY_EXP(NeqExp
)
99 SIMPLIFY_BINARY_EXP(LtExp
)
100 SIMPLIFY_BINARY_EXP(LtEqExp
)
101 SIMPLIFY_BINARY_EXP(GtExp
)
102 SIMPLIFY_BINARY_EXP(GtEqExp
)
104 SIMPLIFY_BINARY_EXP(AddExp
)
105 SIMPLIFY_BINARY_EXP(SubExp
)
106 SIMPLIFY_BINARY_EXP(MulExp
)
107 SIMPLIFY_BINARY_EXP(DivExp
)
108 SIMPLIFY_BINARY_EXP(ModExp
)
110 SIMPLIFY_BINARY_EXP(AndExp
)
111 SIMPLIFY_BINARY_EXP(OrExp
)
112 SIMPLIFY_BINARY_EXP(XorExp
)
114 SIMPLIFY_BINARY_EXP(AndAndExp
)
115 SIMPLIFY_BINARY_EXP(OrOrExp
)
117 SIMPLIFY_BINARY_EXP(ShlExp
)
118 SIMPLIFY_BINARY_EXP(AShrExp
)
119 SIMPLIFY_BINARY_EXP(LShrExp
)
122 SimplifyVisitor::visit(AssignExp
& node
)
125 node
.value
->accept(*this);
126 node
.value
= replacement_
;
128 StoreVar
* sv
= new StoreVar();
129 sv
->name
= node
.name
;
130 sv
->value
= replacement_
;
131 simplifications_
.push_back(sv
);
137 SimplifyVisitor::visit(NumberExp
& node
)
139 replacement_
= &node
;
143 SimplifyVisitor::visit(IdExp
& node
)
145 LoadVar
* lv
= new LoadVar();
146 lv
->name
= node
.name
;
148 simplifications_
.push_back(lv
);
150 TmpResultExp
* tre
= new TmpResultExp();
157 SimplifyVisitor::visit(Label
& node
)
159 simplifications_
.push_back(&node
);
163 SimplifyVisitor::visit(Goto
& node
)
165 simplifications_
.push_back(&node
);