IfNode.h

Go to the documentation of this file.
00001 /*
00002  *      IfNode.h -
00003  *
00004  *  Copyright (C) 2002-2003 The Pentagram Team
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #ifndef IFNODE_H
00022 #define IFNODE_H
00023 
00024 #include "GenericNodes.h"
00025 #include <deque>
00026 #include "CallNodes.h"
00027 
00028 class EndNode : public Node
00029 {
00030         public:
00031                 EndNode(const uint32 opcode, const uint32 offset, const uint32 newTargetOffset)
00032                         : Node(opcode, offset, Type(Type::T_INVALID)), targetOffset(newTargetOffset)
00033                         {
00034                                 assert(acceptOp(opcode, 0x52));
00035                                 switch(opcode)
00036                                 {
00037                                         case 0x52: etype = JMP; break;
00038                                         default: assert(false);
00039                                 }
00040                         };
00041                 ~EndNode() {};
00042                 
00043                 void print_unk(Console &o, const uint32 isize) const;
00044                 void print_asm(Console &o) const;
00045                 void print_bin(ODequeDataSource &o) const;
00046                 bool fold(DCUnit *unit, std::deque<Node *> &nodes);
00047                 
00048                 inline uint32 TargetOffset() const { return targetOffset; };
00049                 
00050         protected:
00051                 enum endtype { JMP } etype;
00052 
00053         private:
00054                 uint32 targetOffset; // jmp
00055 };
00056 
00057 class IfNode;
00058 
00059 class IfNode : public UniNode
00060 {
00061         public:
00062                 enum iftype { I_IF=0, I_IF_ELSE, I_IF_ELSE_IF, I_ELSE_IF, I_ELSE_IF_ELSE, I_ELSE };
00063                 
00064                 IfNode(const iftype newItype, const uint32 newTargetOffset)
00065                         : UniNode(0x51, 0x0000, Type(Type::T_INVALID)), itype(newItype),
00066                         targetOffset(newTargetOffset), jmpnode(0), elsenode(0)
00067                         {};
00068                 IfNode(const uint32 opcode, const uint32 offset, const uint32 newTargetOffset)
00069                         : UniNode(opcode, offset, Type(Type::T_INVALID)),
00070                         targetOffset(newTargetOffset), jmpnode(0), elsenode(0)
00071                         {
00072                                 assert(acceptOp(opcode, 0x51));
00073                                 switch(opcode)
00074                                 {
00075                                         case 0x51: itype = I_IF; break;
00076                                         default: assert(false);
00077                                 }
00078                         };
00079                 ~IfNode() {};
00080                 
00081                 void print_unk(Console &o, const uint32 isize) const;
00082                 void print_asm(Console &o) const;
00083                 void print_bin(ODequeDataSource &o) const;
00084                 bool fold(DCUnit *unit, std::deque<Node *> &nodes);
00085                 bool fold_else(DCUnit *unit, std::deque<Node *> &nodes);
00086                 
00087                 inline uint32 TargetOffset() const
00088                 {
00089                         if(jmpnode!=0)
00090                                 return jmpnode->TargetOffset();
00091                         return targetOffset;
00092                 };
00093                 
00094                 void AddJmp(EndNode *jmp) { assert(jmpnode==0); jmpnode=jmp; };
00095                 
00096                 std::deque<Node *> &nodes() { return ifnodes; };
00097                 IfNode *elsen() { return elsenode; };
00098 
00099                 /* IF, IF_ELSE, and IF_ELSE_IF are all technically the same type,
00100                         what they represent is the state the function is in.
00101 
00102                         * If we're at IF, then we've either not parsed a 'cmp' to terminate
00103                         our basic block yet, or we're 'just' a plain if(){} statement.
00104                         * If we're at IF_ELSE, then we've found our associated 'cmp' opcode,
00105                         just before our targetOffset, and we're either an if(){}else{} statment
00106                         or we've not tripped over the next 'if' statement in an if/else if/else
00107                         series.
00108                         * If we're at IF_ELSE_IF, and IF node has been parsed coming after us,
00109                         and we were already an IF_ELSE flagged node.
00110                 */
00111                 iftype itype;
00112 
00113                 // 'special' functions
00114                 void setAddSP(DCCallPostfixNode *newAddSP) { addSP = newAddSP; };
00115 
00116         protected:
00117                 std::deque<Node *> ifnodes;
00118 
00119         private:
00120                 uint32 targetOffset; // jne
00121                 EndNode *jmpnode; // this is for when we're handling the pessimised if(true){code}->if(false){}else{code}
00122                 DCCallPostfixNode *addSP;
00123         public:
00124                 IfNode  *elsenode;
00125 };
00126 
00127 #endif

Generated on Fri Jul 27 22:27:19 2007 for pentagram by  doxygen 1.4.7