Operand.h revision 533eae20118036f425f27bf0536ef0ccbb090b65
1//===- Operand.h ----------------------------------------------------------===// 2// 3// The MCLinker Project 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9#ifndef MCLD_SCRIPT_OPERAND_H 10#define MCLD_SCRIPT_OPERAND_H 11 12#include <mcld/Script/ExprToken.h> 13#include <mcld/Object/SectionMap.h> 14#include <mcld/Support/Allocators.h> 15#include <mcld/Config/Config.h> 16#include <llvm/Support/DataTypes.h> 17#include <string> 18#include <cassert> 19 20namespace mcld 21{ 22 23/** \class Operand 24 * \brief This class defines the interfaces to an operand token. 25 */ 26 27class Operand : public ExprToken 28{ 29public: 30 enum Type { 31 SYMBOL, 32 INTEGER, 33 SECTION, 34 SECTION_DESC, 35 FRAGMENT 36 }; 37 38protected: 39 Operand(Type pType); 40 virtual ~Operand(); 41 42public: 43 Type type() const { return m_Type; } 44 45 virtual bool isDot() const { return false; } 46 47 virtual uint64_t value() const = 0; 48 49 static bool classof(const ExprToken* pToken) 50 { 51 return pToken->kind() == ExprToken::OPERAND; 52 } 53 54private: 55 Type m_Type; 56}; 57 58/** \class SymOperand 59 * \brief This class defines the interfaces to a symbol operand. 60 */ 61 62class SymOperand : public Operand 63{ 64private: 65 friend class Chunk<SymOperand, MCLD_SYMBOLS_PER_INPUT>; 66 SymOperand(); 67 SymOperand(const std::string& pName); 68 69public: 70 void dump() const; 71 72 const std::string& name() const { return m_Name; } 73 74 bool isDot() const; 75 76 uint64_t value() const { return m_Value; } 77 78 void setValue(uint64_t pValue) { m_Value = pValue; } 79 80 static bool classof(const Operand* pOperand) 81 { 82 return pOperand->type() == Operand::SYMBOL; 83 } 84 85 /* factory method */ 86 static SymOperand* create(const std::string& pName); 87 static void destroy(SymOperand*& pOperand); 88 static void clear(); 89 90private: 91 std::string m_Name; 92 uint64_t m_Value; 93}; 94 95/** \class IntOperand 96 * \brief This class defines the interfaces to an integer operand. 97 */ 98 99class IntOperand : public Operand 100{ 101private: 102 friend class Chunk<IntOperand, MCLD_SYMBOLS_PER_INPUT>; 103 IntOperand(); 104 IntOperand(uint64_t pValue); 105 106public: 107 void dump() const; 108 109 uint64_t value() const { return m_Value; } 110 111 void setValue(uint64_t pValue) { m_Value = pValue; } 112 113 static bool classof(const Operand* pOperand) 114 { 115 return pOperand->type() == Operand::INTEGER; 116 } 117 118 /* factory method */ 119 static IntOperand* create(uint64_t pValue); 120 static void destroy(IntOperand*& pOperand); 121 static void clear(); 122 123private: 124 uint64_t m_Value; 125}; 126 127/** \class SectOperand 128 * \brief This class defines the interfaces to an section name operand. 129 */ 130class LDSection; 131 132class SectOperand : public Operand 133{ 134private: 135 friend class Chunk<SectOperand, MCLD_SECTIONS_PER_INPUT>; 136 SectOperand(); 137 SectOperand(const std::string& pName); 138 139public: 140 void dump() const; 141 142 const std::string& name() const { return m_Name; } 143 144 uint64_t value() const 145 { 146 assert(0); 147 return 0; 148 } 149 150 static bool classof(const Operand* pOperand) 151 { 152 return pOperand->type() == Operand::SECTION; 153 } 154 155 /* factory method */ 156 static SectOperand* create(const std::string& pName); 157 static void destroy(SectOperand*& pOperand); 158 static void clear(); 159 160private: 161 std::string m_Name; 162}; 163 164/** \class SectDescOperand 165 * \brief This class defines the interfaces to an section name operand. 166 */ 167 168class SectDescOperand : public Operand 169{ 170private: 171 friend class Chunk<SectDescOperand, MCLD_SECTIONS_PER_INPUT>; 172 SectDescOperand(); 173 SectDescOperand(const SectionMap::Output* pOutputDesc); 174 175public: 176 void dump() const; 177 178 const SectionMap::Output* outputDesc() const { return m_pOutputDesc; } 179 180 uint64_t value() const 181 { 182 assert(0); 183 return 0; 184 } 185 186 static bool classof(const Operand* pOperand) 187 { 188 return pOperand->type() == Operand::SECTION_DESC; 189 } 190 191 /* factory method */ 192 static SectDescOperand* create(const SectionMap::Output* pOutputDesc); 193 static void destroy(SectDescOperand*& pOperand); 194 static void clear(); 195 196private: 197 const SectionMap::Output* m_pOutputDesc; 198}; 199 200/** \class FragOperand 201 * \brief This class defines the interfaces to a fragment operand. 202 */ 203 204class Fragment; 205 206class FragOperand : public Operand 207{ 208private: 209 friend class Chunk<FragOperand, MCLD_SYMBOLS_PER_INPUT>; 210 FragOperand(); 211 FragOperand(Fragment& pFragment); 212 213public: 214 void dump() const; 215 216 const Fragment* frag() const { return m_pFragment; } 217 Fragment* frag() { return m_pFragment; } 218 219 uint64_t value() const; 220 221 static bool classof(const Operand* pOperand) 222 { 223 return pOperand->type() == Operand::FRAGMENT; 224 } 225 226 /* factory method */ 227 static FragOperand* create(Fragment& pFragment); 228 static void destroy(FragOperand*& pOperand); 229 static void clear(); 230 231private: 232 Fragment* m_pFragment; 233}; 234 235} // namespace of mcld 236 237#endif 238 239