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
.statements
))
57 simplifications_
.clear();
59 BOOST_FOREACH(Node
* snode
, simplifications_
)
60 newList
->push_back(snode
);
63 //delete block.instrs;
65 block
.statements
= newList
;
68 #define SIMPLIFY_BINARY_EXP(Type) \
72 * - Create a new id. \
73 * - Create a new assign exp to id. \
74 * - Create a IdExp and tell replace the parent's child with it. \
77 SimplifyVisitor::visit(Type & node) \
80 node.left->accept(*this); \
81 node.left = replacement_; \
83 node.right->accept(*this); \
84 node.right = replacement_; \
86 std::string name = nextId(); \
88 TmpResultExp * tmpRes = new TmpResultExp(); \
89 tmpRes->name = name; \
90 tmpRes->type = node.type; \
91 assert(tmpRes->type); \
92 replacement_ = tmpRes; \
94 AssignExp * assign = new AssignExp(); \
95 assign->dest = new Symbol(); \
96 assign->dest->name = name; \
97 assign->dest->type = node.type; \
98 assign->value = &node; \
99 assign->type = node.type; \
100 simplifications_.push_back(assign); \
103 SIMPLIFY_BINARY_EXP(EqExp
)
104 SIMPLIFY_BINARY_EXP(NeqExp
)
105 SIMPLIFY_BINARY_EXP(LtExp
)
106 SIMPLIFY_BINARY_EXP(LtEqExp
)
107 SIMPLIFY_BINARY_EXP(GtExp
)
108 SIMPLIFY_BINARY_EXP(GtEqExp
)
110 SIMPLIFY_BINARY_EXP(AddExp
)
111 SIMPLIFY_BINARY_EXP(SubExp
)
112 SIMPLIFY_BINARY_EXP(MulExp
)
113 SIMPLIFY_BINARY_EXP(DivExp
)
114 SIMPLIFY_BINARY_EXP(ModExp
)
116 SIMPLIFY_BINARY_EXP(AndExp
)
117 SIMPLIFY_BINARY_EXP(OrExp
)
118 SIMPLIFY_BINARY_EXP(XorExp
)
120 SIMPLIFY_BINARY_EXP(AndAndExp
)
121 SIMPLIFY_BINARY_EXP(OrOrExp
)
123 SIMPLIFY_BINARY_EXP(ShlExp
)
124 SIMPLIFY_BINARY_EXP(AShrExp
)
125 SIMPLIFY_BINARY_EXP(LShrExp
)
128 SimplifyVisitor::visit(AssignExp
& node
)
131 node
.value
->accept(*this);
132 node
.value
= replacement_
;
134 StoreVar
* sv
= new StoreVar();
135 sv
->name
= node
.dest
->name
;
136 sv
->value
= replacement_
;
137 simplifications_
.push_back(sv
);
143 SimplifyVisitor::visit(NumberExp
& node
)
145 replacement_
= &node
;
149 SimplifyVisitor::visit(IdExp
& node
)
151 LoadVar
* lv
= new LoadVar();
152 lv
->name
= node
.name
;
154 lv
->type
= node
.type
;
155 simplifications_
.push_back(lv
);
157 TmpResultExp
* tre
= new TmpResultExp();
159 tre
->type
= node
.type
;
166 * develop the cast, and simplify it
169 SimplifyVisitor::visit(CastExp
& node
)
171 node
.exp
->accept(*this);
172 node
.exp
= replacement_
;
174 AssignExp
* assign
= new AssignExp();
175 assign
->value
= &node
;
176 assign
->dest
= new Symbol();
177 assign
->dest
->name
= nextId();
178 assign
->dest
->type
= node
.type
;
179 assign
->type
= node
.type
;
180 simplifications_
.push_back(assign
);
182 TmpResultExp
* tre
= new TmpResultExp();
183 tre
->name
= assign
->dest
->name
;
184 tre
->type
= node
.type
;
189 SimplifyVisitor::visit(Label
& node
)
191 simplifications_
.push_back(&node
);
195 SimplifyVisitor::visit(Goto
& node
)
197 simplifications_
.push_back(&node
);