00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __TREE_H
00025 #define __TREE_H
00026
00027 #include "compiler.h"
00028 #include "../common/consts.h"
00029 #include <vector>
00030 #include <string>
00031 #include <ostream>
00032
00033
00034
00035 namespace Aseba
00036 {
00039
00041 std::string binaryOperatorToString(AsebaBinaryOperator op);
00042
00044 std::string unaryOperatorToString(AsebaUnaryOperator op);
00045
00047 struct Node
00048 {
00050 enum ReturnType
00051 {
00052 TYPE_UNIT = 0,
00053 TYPE_BOOL,
00054 TYPE_INT
00055 };
00056
00058 Node(const SourcePos& sourcePos) : sourcePos(sourcePos) { }
00059
00060 virtual ~Node();
00061
00063 virtual ReturnType typeCheck() const;
00065 virtual Node* optimize(std::ostream* dump) = 0;
00067 virtual unsigned getStackDepth() const;
00069 virtual void emit(PreLinkBytecode& bytecodes) const = 0;
00070
00072 virtual std::string toString() const = 0;
00074 virtual std::string toNodeName() const = 0;
00076 virtual void dump(std::ostream& dest, unsigned& indent) const;
00077
00079 std::string typeName(const Node::ReturnType& type) const;
00081 void expectType(const Node::ReturnType& expected, const Node::ReturnType& type) const;
00082
00084 typedef std::vector<Node *> NodesVector;
00085 NodesVector children;
00086 SourcePos sourcePos;
00087 };
00088
00090 struct BlockNode : Node
00091 {
00093 BlockNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00094
00095 virtual Node* optimize(std::ostream* dump);
00096 virtual void emit(PreLinkBytecode& bytecodes) const;
00097 virtual std::string toString() const { return "Block"; }
00098 virtual std::string toNodeName() const { return "block"; }
00099 };
00100
00102 struct ProgramNode: BlockNode
00103 {
00105 ProgramNode(const SourcePos& sourcePos) : BlockNode(sourcePos) { }
00106
00107 virtual void emit(PreLinkBytecode& bytecodes) const;
00108 virtual std::string toString() const { return "ProgramBlock"; }
00109 virtual std::string toNodeName() const { return "program block"; }
00110 };
00111
00115 struct AssignmentNode : Node
00116 {
00118 AssignmentNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00119
00120 virtual Node* optimize(std::ostream* dump);
00121 virtual void emit(PreLinkBytecode& bytecodes) const;
00122 virtual std::string toString() const { return "Assign"; }
00123 virtual std::string toNodeName() const { return "assignment"; }
00124 };
00125
00130 struct IfWhenNode : Node
00131 {
00132 bool edgeSensitive;
00133 unsigned endLine;
00134
00136 IfWhenNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00137
00138 virtual ReturnType typeCheck() const;
00139 virtual Node* optimize(std::ostream* dump);
00140 virtual void emit(PreLinkBytecode& bytecodes) const;
00141 virtual std::string toString() const;
00142 virtual std::string toNodeName() const { return "if/when"; }
00143 };
00144
00150 struct FoldedIfWhenNode : Node
00151 {
00152 AsebaBinaryOperator op;
00153 bool edgeSensitive;
00154 unsigned endLine;
00155
00157 FoldedIfWhenNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00158
00159 virtual Node* optimize(std::ostream* dump);
00160 virtual unsigned getStackDepth() const;
00161 virtual void emit(PreLinkBytecode& bytecodes) const;
00162 virtual std::string toString() const;
00163 virtual std::string toNodeName() const { return "folded if/when"; }
00164 };
00165
00169 struct WhileNode : Node
00170 {
00172 WhileNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00173
00174 virtual ReturnType typeCheck() const;
00175 virtual Node* optimize(std::ostream* dump);
00176 virtual void emit(PreLinkBytecode& bytecodes) const;
00177 virtual std::string toString() const;
00178 virtual std::string toNodeName() const { return "while"; }
00179 };
00180
00185 struct FoldedWhileNode : Node
00186 {
00187 AsebaBinaryOperator op;
00188
00190 FoldedWhileNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00191
00192 virtual Node* optimize(std::ostream* dump);
00193 virtual unsigned getStackDepth() const;
00194 virtual void emit(PreLinkBytecode& bytecodes) const;
00195 virtual std::string toString() const;
00196 virtual std::string toNodeName() const { return "folded while"; }
00197 };
00198
00201 struct EventDeclNode : Node
00202 {
00203 unsigned eventId;
00204
00205 EventDeclNode(const SourcePos& sourcePos, unsigned eventId = 0);
00206
00207 virtual Node* optimize(std::ostream* dump);
00208 virtual void emit(PreLinkBytecode& bytecodes) const;
00209 virtual std::string toString() const;
00210 virtual std::string toNodeName() const { return "event declaration"; }
00211 };
00212
00215 struct EmitNode : Node
00216 {
00217 unsigned eventId;
00218 unsigned arrayAddr;
00219 unsigned arraySize;
00220
00222 EmitNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00223
00224 virtual Node* optimize(std::ostream* dump);
00225 virtual void emit(PreLinkBytecode& bytecodes) const;
00226 virtual std::string toString() const;
00227 virtual std::string toNodeName() const { return "emit"; }
00228 };
00229
00232 struct SubDeclNode : Node
00233 {
00234 unsigned subroutineId;
00235
00236 SubDeclNode(const SourcePos& sourcePos, unsigned subroutineId);
00237
00238 virtual Node* optimize(std::ostream* dump);
00239 virtual void emit(PreLinkBytecode& bytecodes) const;
00240 virtual std::string toString() const;
00241 virtual std::string toNodeName() const { return "subroutine declaration"; }
00242 };
00243
00246 struct CallSubNode : Node
00247 {
00248 unsigned subroutineId;
00249
00250 CallSubNode(const SourcePos& sourcePos, unsigned subroutineId);
00251
00252 virtual Node* optimize(std::ostream* dump);
00253 virtual void emit(PreLinkBytecode& bytecodes) const;
00254 virtual std::string toString() const;
00255 virtual std::string toNodeName() const { return "subroutine call"; }
00256 };
00257
00261 struct BinaryArithmeticNode : Node
00262 {
00263 AsebaBinaryOperator op;
00264
00265 BinaryArithmeticNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00266 BinaryArithmeticNode(const SourcePos& sourcePos, AsebaBinaryOperator op, Node *left, Node *right);
00267
00268 void deMorganNotRemoval();
00269
00270 virtual ReturnType typeCheck() const;
00271 virtual Node* optimize(std::ostream* dump);
00272 virtual unsigned getStackDepth() const;
00273 virtual void emit(PreLinkBytecode& bytecodes) const;
00274 virtual std::string toString() const;
00275 virtual std::string toNodeName() const { return "binary function"; }
00276
00277 static BinaryArithmeticNode *fromComparison(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
00278 static BinaryArithmeticNode *fromShiftExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
00279 static BinaryArithmeticNode *fromAddExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
00280 static BinaryArithmeticNode *fromMultExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
00281 };
00282
00285 struct UnaryArithmeticNode : Node
00286 {
00287 AsebaUnaryOperator op;
00288
00290 UnaryArithmeticNode(const SourcePos& sourcePos) : Node(sourcePos) { }
00291 UnaryArithmeticNode(const SourcePos& sourcePos, AsebaUnaryOperator op, Node *child);
00292
00293 virtual ReturnType typeCheck() const;
00294 virtual Node* optimize(std::ostream* dump);
00295 virtual void emit(PreLinkBytecode& bytecodes) const;
00296 virtual std::string toString() const;
00297 virtual std::string toNodeName() const { return "unary function"; }
00298 };
00299
00304 struct ImmediateNode : Node
00305 {
00306 int value;
00307
00309 ImmediateNode(const SourcePos& sourcePos, int value) : Node(sourcePos), value(value) { }
00310
00311 virtual ReturnType typeCheck() const { return TYPE_INT; }
00312 virtual Node* optimize(std::ostream* dump);
00313 virtual unsigned getStackDepth() const;
00314 virtual void emit(PreLinkBytecode& bytecodes) const;
00315 virtual std::string toString() const;
00316 virtual std::string toNodeName() const { return "constant"; }
00317 };
00318
00321 struct LoadNode : Node
00322 {
00323 unsigned varAddr;
00324
00326 LoadNode(const SourcePos& sourcePos, unsigned varAddr) : Node(sourcePos), varAddr(varAddr) { }
00327
00328 virtual ReturnType typeCheck() const { return TYPE_INT; }
00329 virtual Node* optimize(std::ostream* dump);
00330 virtual unsigned getStackDepth() const;
00331 virtual void emit(PreLinkBytecode& bytecodes) const;
00332 virtual std::string toString() const;
00333 virtual std::string toNodeName() const { return "variable access (read)"; }
00334 };
00335
00338 struct StoreNode : Node
00339 {
00340 unsigned varAddr;
00341
00343 StoreNode(const SourcePos& sourcePos, unsigned varAddr) : Node(sourcePos), varAddr(varAddr) { }
00344
00345 virtual Node* optimize(std::ostream* dump);
00346 virtual void emit(PreLinkBytecode& bytecodes) const;
00347 virtual std::string toString() const;
00348 virtual std::string toNodeName() const { return "variable access (write)"; }
00349 };
00350
00353 struct ArrayReadNode : Node
00354 {
00355 unsigned arrayAddr;
00356 unsigned arraySize;
00357 std::string arrayName;
00358
00359 ArrayReadNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::string &arrayName);
00360
00361 virtual ReturnType typeCheck() const { return TYPE_INT; }
00362 virtual Node* optimize(std::ostream* dump);
00363 virtual void emit(PreLinkBytecode& bytecodes) const;
00364 virtual std::string toString() const;
00365 virtual std::string toNodeName() const { return "array access (read)"; }
00366 };
00367
00370 struct ArrayWriteNode : Node
00371 {
00372 unsigned arrayAddr;
00373 unsigned arraySize;
00374 std::string arrayName;
00375
00376 ArrayWriteNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::string &arrayName);
00377
00378 virtual Node* optimize(std::ostream* dump);
00379 virtual void emit(PreLinkBytecode& bytecodes) const;
00380 virtual std::string toString() const;
00381 virtual std::string toNodeName() const { return "array access (write)"; }
00382 };
00383
00386 struct CallNode : Node
00387 {
00388 unsigned funcId;
00389 std::vector<unsigned> argumentsAddr;
00390
00391 CallNode(const SourcePos& sourcePos, unsigned funcId);
00392
00393 virtual ReturnType typeCheck() const { return TYPE_UNIT; }
00394 virtual Node* optimize(std::ostream* dump);
00395 virtual unsigned getStackDepth() const;
00396 virtual void emit(PreLinkBytecode& bytecodes) const;
00397 virtual std::string toString() const;
00398 virtual std::string toNodeName() const { return "native function call"; }
00399 };
00400
00403 };
00404
00405 #endif