OLD | NEW |
(Empty) | |
| 1 /* Copyright 2014 The Native Client Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can |
| 3 * be found in the LICENSE file. |
| 4 */ |
| 5 |
| 6 #include "IceCfg.h" |
| 7 #include "IceCfgNode.h" |
| 8 #include "IceDefs.h" |
| 9 #include "IceInst.h" |
| 10 #include "IceOperand.h" |
| 11 |
| 12 class IceConstantPool { |
| 13 public: |
| 14 IceConstantPool(IceCfg *Cfg) : Cfg(Cfg) {} |
| 15 IceConstantRelocatable *getOrAddRelocatable(IceType Type, const void *Handle, |
| 16 int64_t Offset, |
| 17 const IceString &Name) { |
| 18 uint32_t Index = NameToIndex.translate(KeyType(Name, Type)); |
| 19 if (Index >= RelocatablePool.size()) { |
| 20 RelocatablePool.resize(Index + 1); |
| 21 void *Handle = NULL; |
| 22 RelocatablePool[Index] = IceConstantRelocatable::create( |
| 23 Cfg, Index, Type, Handle, Offset, Name); |
| 24 } |
| 25 IceConstantRelocatable *Constant = RelocatablePool[Index]; |
| 26 assert(Constant); |
| 27 return Constant; |
| 28 } |
| 29 uint32_t getSize(void) const { return RelocatablePool.size(); } |
| 30 IceConstantRelocatable *getEntry(uint32_t Index) const { |
| 31 assert(Index < RelocatablePool.size()); |
| 32 return RelocatablePool[Index]; |
| 33 } |
| 34 |
| 35 private: |
| 36 typedef std::pair<IceString, IceType> KeyType; |
| 37 // TODO: Cfg is being captured primarily for arena allocation for |
| 38 // new IceConstants. If IceConstants live beyond a function/Cfg, |
| 39 // they need to be allocated from a global arena and there needs to |
| 40 // be appropriate locking. |
| 41 IceCfg *Cfg; |
| 42 // Use IceValueTranslation<> to map (Name,Type) pairs to an index. |
| 43 IceValueTranslation<KeyType> NameToIndex; |
| 44 std::vector<IceConstantRelocatable *> RelocatablePool; |
| 45 }; |
| 46 |
| 47 IceCfg::IceCfg(void) |
| 48 : Str(this), Name(""), TestPrefix(""), Type(IceType_void), |
| 49 IsInternal(false), HasError(false), ErrorMessage(""), Entry(NULL), |
| 50 NextInstNumber(1) { |
| 51 ConstantPool = new IceConstantPool(this); |
| 52 } |
| 53 |
| 54 IceCfg::~IceCfg() { delete ConstantPool; } |
| 55 |
| 56 void IceCfg::setError(const IceString &Message) { |
| 57 HasError = true; |
| 58 ErrorMessage = Message; |
| 59 Str << "ICE translation error: " << ErrorMessage << "\n"; |
| 60 } |
| 61 |
| 62 IceCfgNode *IceCfg::makeNode(const IceString &Name) { |
| 63 uint32_t LabelIndex = Nodes.size(); |
| 64 IceCfgNode *Node = IceCfgNode::create(this, LabelIndex, Name); |
| 65 Nodes.push_back(Node); |
| 66 return Node; |
| 67 } |
| 68 |
| 69 // Create a new IceVariable with a particular type and an optional |
| 70 // name. The Node argument is the node where the variable is defined. |
| 71 IceVariable *IceCfg::makeVariable(IceType Type, const IceCfgNode *Node, |
| 72 const IceString &Name) { |
| 73 uint32_t Index = Variables.size(); |
| 74 Variables.push_back(IceVariable::create(this, Type, Node, Index, Name)); |
| 75 return Variables[Index]; |
| 76 } |
| 77 |
| 78 void IceCfg::addArg(IceVariable *Arg) { |
| 79 Arg->setIsArg(this); |
| 80 Args.push_back(Arg); |
| 81 } |
| 82 |
| 83 IceConstant *IceCfg::getConstantInt(IceType Type, uint64_t ConstantInt64) { |
| 84 return IceConstantInteger::create(this, Type, ConstantInt64); |
| 85 } |
| 86 |
| 87 // TODO: Add float and double constants to the global constant pool, |
| 88 // instead of creating a new instance each time. |
| 89 IceConstant *IceCfg::getConstantFloat(float ConstantFloat) { |
| 90 return IceConstantFloat::create(this, IceType_f32, ConstantFloat); |
| 91 } |
| 92 |
| 93 IceConstant *IceCfg::getConstantDouble(double ConstantDouble) { |
| 94 return IceConstantDouble::create(this, IceType_f64, ConstantDouble); |
| 95 } |
| 96 |
| 97 IceConstant *IceCfg::getConstantSym(IceType Type, const void *Handle, |
| 98 int64_t Offset, const IceString &Name, |
| 99 bool SuppressMangling) { |
| 100 IceConstantRelocatable *Const = |
| 101 ConstantPool->getOrAddRelocatable(Type, Handle, Offset, Name); |
| 102 Const->setSuppressMangling(SuppressMangling); |
| 103 return Const; |
| 104 } |
| 105 |
| 106 void IceCfg::registerEdges(void) { |
| 107 for (IceNodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
| 108 (*I)->registerEdges(); |
| 109 } |
| 110 } |
| 111 |
| 112 // ======================== Dump routines ======================== // |
| 113 |
| 114 void IceCfg::dump(void) const { |
| 115 Str.setCurrentNode(getEntryNode()); |
| 116 // Print function name+args |
| 117 if (Str.isVerbose(IceV_Instructions)) { |
| 118 Str << "define "; |
| 119 if (getInternal()) |
| 120 Str << "internal "; |
| 121 Str << Type << " @" << Name << "("; |
| 122 for (uint32_t i = 0; i < Args.size(); ++i) { |
| 123 if (i > 0) |
| 124 Str << ", "; |
| 125 Str << Args[i]->getType() << " " << Args[i]; |
| 126 } |
| 127 Str << ") {\n"; |
| 128 } |
| 129 Str.setCurrentNode(NULL); |
| 130 // Print each basic block |
| 131 for (IceNodeList::const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; |
| 132 ++I) { |
| 133 (*I)->dump(Str); |
| 134 } |
| 135 if (Str.isVerbose(IceV_Instructions)) { |
| 136 Str << "}\n"; |
| 137 } |
| 138 } |
OLD | NEW |