 Chromium Code Reviews
 Chromium Code Reviews Issue 205613002:
  Initial skeleton of Subzero.  (Closed) 
  Base URL: https://gerrit.chromium.org/gerrit/p/native_client/pnacl-subzero.git@master
    
  
    Issue 205613002:
  Initial skeleton of Subzero.  (Closed) 
  Base URL: https://gerrit.chromium.org/gerrit/p/native_client/pnacl-subzero.git@master| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 //===- subzero/src/IceGlobalContext.cpp - Global context defs ---*- C++ -*-===// | |
| 2 // | |
| 3 // The Subzero Code Generator | |
| 4 // | |
| 5 // This file is distributed under the University of Illinois Open Source | |
| 6 // License. See LICENSE.TXT for details. | |
| 7 // | |
| 8 //===----------------------------------------------------------------------===// | |
| 9 // | |
| 10 // This file defines aspects of the compilation that persist across | |
| 11 // multiple functions. | |
| 12 // | |
| 13 //===----------------------------------------------------------------------===// | |
| 14 | |
| 15 #include "IceDefs.h" | |
| 16 #include "IceTypes.h" | |
| 17 #include "IceCfg.h" | |
| 18 #include "IceGlobalContext.h" | |
| 19 #include "IceOperand.h" | |
| 20 | |
| 21 class IceConstantPool { | |
| 22 // TODO: Try to refactor the getOrAdd*() methods. | |
| 
JF
2014/04/16 01:27:32
Yes please!
 
Jim Stichnoth
2014/04/21 20:26:40
Done.
 | |
| 23 public: | |
| 24 IceConstantPool(IceGlobalContext *Ctx) : Ctx(Ctx) {} | |
| 25 IceConstantRelocatable *getOrAddRelocatable(IceType Type, const void *Handle, | |
| 26 int64_t Offset, | |
| 27 const IceString &Name) { | |
| 28 uint32_t Index = NameToIndexReloc.translate( | |
| 29 KeyTypeReloc(Type, std::pair<IceString, int64_t>(Name, Offset))); | |
| 
JF
2014/04/16 01:27:32
std::make_pair
 
Jim Stichnoth
2014/04/21 20:26:40
Done.
 | |
| 30 if (Index >= RelocatablePool.size()) { | |
| 31 RelocatablePool.resize(Index + 1); | |
| 32 void *Handle = NULL; | |
| 33 RelocatablePool[Index] = IceConstantRelocatable::create( | |
| 34 Ctx, Index, Type, Handle, Offset, Name); | |
| 35 } | |
| 36 IceConstantRelocatable *Constant = RelocatablePool[Index]; | |
| 37 assert(Constant); | |
| 38 return Constant; | |
| 39 } | |
| 40 uint32_t getRelocatableSize() const { return RelocatablePool.size(); } | |
| 41 IceConstantRelocatable *getEntry(uint32_t Index) const { | |
| 42 assert(Index < RelocatablePool.size()); | |
| 43 return RelocatablePool[Index]; | |
| 44 } | |
| 45 IceConstantInteger *getOrAddInteger(IceType Type, uint64_t Value) { | |
| 46 uint32_t Index = NameToIndexInteger.translate(KeyTypeInteger(Type, Value)); | |
| 47 if (Index >= IntegerPool.size()) { | |
| 48 IntegerPool.resize(Index + 1); | |
| 49 IntegerPool[Index] = IceConstantInteger::create(Ctx, Type, Value); | |
| 50 } | |
| 51 IceConstantInteger *Constant = IntegerPool[Index]; | |
| 52 assert(Constant); | |
| 53 return Constant; | |
| 54 } | |
| 55 IceConstantFloat *getOrAddFloat(float Value) { | |
| 56 uint32_t Index = NameToIndexFloat.translate(Value); | |
| 57 if (Index >= FloatPool.size()) { | |
| 58 FloatPool.resize(Index + 1); | |
| 59 FloatPool[Index] = IceConstantFloat::create(Ctx, IceType_f32, Value); | |
| 60 } | |
| 61 IceConstantFloat *Constant = FloatPool[Index]; | |
| 62 assert(Constant); | |
| 63 return Constant; | |
| 64 } | |
| 65 IceConstantDouble *getOrAddDouble(double Value) { | |
| 66 uint32_t Index = NameToIndexDouble.translate(Value); | |
| 67 if (Index >= DoublePool.size()) { | |
| 68 DoublePool.resize(Index + 1); | |
| 69 DoublePool[Index] = IceConstantDouble::create(Ctx, IceType_f64, Value); | |
| 70 } | |
| 71 IceConstantDouble *Constant = DoublePool[Index]; | |
| 72 assert(Constant); | |
| 73 return Constant; | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 // KeyTypeReloc is a triple of {Type, Name, Offset}. | |
| 
JF
2014/04/16 01:27:32
TODO: use tuple when we move to C++11.
 
Jim Stichnoth
2014/04/21 20:26:40
Done.
 | |
| 78 typedef std::pair<IceType, std::pair<IceString, int64_t> > KeyTypeReloc; | |
| 79 typedef std::pair<IceType, int64_t> KeyTypeInteger; | |
| 80 IceGlobalContext *Ctx; | |
| 81 // Use IceValueTranslation<> to map (Name,Type) pairs to an index. | |
| 82 IceValueTranslation<KeyTypeReloc> NameToIndexReloc; | |
| 83 IceValueTranslation<KeyTypeInteger> NameToIndexInteger; | |
| 84 IceValueTranslation<float> NameToIndexFloat; | |
| 85 IceValueTranslation<double> NameToIndexDouble; | |
| 86 std::vector<IceConstantRelocatable *> RelocatablePool; | |
| 87 std::vector<IceConstantInteger *> IntegerPool; | |
| 88 std::vector<IceConstantFloat *> FloatPool; | |
| 89 std::vector<IceConstantDouble *> DoublePool; | |
| 
JF
2014/04/16 01:27:32
I find the map/vector duality really weird: the ma
 
Jim Stichnoth
2014/04/21 20:26:40
The latest patchset makes this map/vector duality
 
JF
2014/04/23 03:51:28
Could you make sure the tests have NaN values in t
 
JF
2014/04/23 03:51:28
The whole duality still seems weird. I really thin
 
Jim Stichnoth
2014/04/26 15:02:11
Done - see fpconst.pnacl.ll, which includes duplic
 
Jim Stichnoth
2014/04/26 15:02:11
You are right - it's good enough to keep a simple
 | |
| 90 }; | |
| 91 | |
| 92 IceGlobalContext::IceGlobalContext(llvm::raw_ostream *OsDump, | |
| 93 llvm::raw_ostream *OsEmit, | |
| 94 IceVerboseMask VerboseMask, | |
| 95 IceString TestPrefix) | |
| 96 : StrDump(OsDump), StrEmit(OsEmit), VerboseMask(VerboseMask), | |
| 97 ConstantPool(new IceConstantPool(this)), TestPrefix(TestPrefix) {} | |
| 98 | |
| 99 // In this context, name mangling means to rewrite a symbol using a | |
| 100 // given prefix. For a C++ symbol, we'd like to demangle it, prepend | |
| 101 // the prefix to the original symbol, and remangle it for C++. For | |
| 102 // other symbols, just prepend the prefix. | |
| 103 IceString IceGlobalContext::mangleName(const IceString &Name) const { | |
| 104 // TODO: This handles only non-nested C++ symbols, and not ones that | |
| 105 // begin with "_ZN". For the latter, we need to rewrite only the | |
| 106 // last name component. | |
| 107 if (getTestPrefix() == "") | |
| 108 return Name; | |
| 109 IceString Default = getTestPrefix() + Name; | |
| 110 uint32_t BaseLength = 0; | |
| 111 char Buffer[1 + Name.length()]; | |
| 112 int ItemsParsed = sscanf(Name.c_str(), "_Z%u%s", &BaseLength, Buffer); | |
| 113 if (ItemsParsed != 2) | |
| 114 return Default; | |
| 115 if (strlen(Buffer) < BaseLength) | |
| 
JF
2014/04/16 01:27:32
Could you explain what this case is?
 
Jim Stichnoth
2014/04/21 20:26:40
Done.  I actually rolled in a better version of ma
 | |
| 116 return Default; | |
| 117 | |
| 118 BaseLength += getTestPrefix().length(); | |
| 119 char NewNumber[30 + Name.length() + getTestPrefix().length()]; | |
| 120 sprintf(NewNumber, "_Z%u%s%s", BaseLength, getTestPrefix().c_str(), Buffer); | |
| 
JF
2014/04/16 01:27:32
snprintf, and check for errors.
 
Jim Stichnoth
2014/04/21 20:26:40
Done.  I don't think error (or snprintf return val
 | |
| 121 return NewNumber; | |
| 122 } | |
| 123 | |
| 124 IceGlobalContext::~IceGlobalContext() {} | |
| 125 | |
| 126 IceConstant *IceGlobalContext::getConstantInt(IceType Type, | |
| 127 uint64_t ConstantInt64) { | |
| 128 return ConstantPool->getOrAddInteger(Type, ConstantInt64); | |
| 129 } | |
| 130 | |
| 131 IceConstant *IceGlobalContext::getConstantFloat(float ConstantFloat) { | |
| 132 return ConstantPool->getOrAddFloat(ConstantFloat); | |
| 133 } | |
| 134 | |
| 135 IceConstant *IceGlobalContext::getConstantDouble(double ConstantDouble) { | |
| 136 return ConstantPool->getOrAddDouble(ConstantDouble); | |
| 137 } | |
| 138 | |
| 139 IceConstant *IceGlobalContext::getConstantSym(IceType Type, const void *Handle, | |
| 140 int64_t Offset, | |
| 141 const IceString &Name, | |
| 142 bool SuppressMangling) { | |
| 143 IceConstantRelocatable *Const = | |
| 144 ConstantPool->getOrAddRelocatable(Type, Handle, Offset, Name); | |
| 145 Const->setSuppressMangling(SuppressMangling); | |
| 
JF
2014/04/16 01:27:32
Doesn't this change the manging if e.g. the symbol
 
Jim Stichnoth
2014/04/21 20:26:40
True.  The code has been changed to add SuppressMa
 | |
| 146 return Const; | |
| 147 } | |
| 148 | |
| 149 void IceTimer::printElapsedUs(IceGlobalContext *Ctx, | |
| 150 const IceString &Tag) const { | |
| 151 if (Ctx->isVerbose(IceV_Timing)) { | |
| 152 // Prefixing with '#' allows timing strings to be included | |
| 153 // without error in textual assembly output. | |
| 154 Ctx->StrDump << "# " << getElapsedUs() << " usec " << Tag << "\n"; | |
| 155 } | |
| 156 } | |
| OLD | NEW |