Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceGlobalInits.cpp - Global initializers ---------------===// | 1 //===- subzero/src/IceGlobalInits.cpp - Global declarations ---------------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the notion of global addresses and | 10 // This file implements the notion of function declarations, global |
| 11 // initializers in Subzero. | 11 // variable declarations, and the corresponding variable initializers |
| 12 // in Subzero. | |
| 12 // | 13 // |
| 13 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
| 14 | 15 |
| 15 #include "llvm/ADT/STLExtras.h" | 16 #include "llvm/ADT/STLExtras.h" |
| 16 #include "llvm/IR/Function.h" | 17 #include "llvm/IR/Function.h" |
| 17 #include "llvm/IR/Value.h" | 18 #include "llvm/IR/Value.h" |
| 18 | 19 |
| 19 #include "IceDefs.h" | 20 #include "IceDefs.h" |
| 21 #include "IceGlobalContext.h" | |
| 20 #include "IceGlobalInits.h" | 22 #include "IceGlobalInits.h" |
| 21 #include "IceTypes.h" | 23 #include "IceTypes.h" |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 char hexdigit(unsigned X) { return X < 10 ? '0' + X : 'A' + X - 10; } | 26 char hexdigit(unsigned X) { return X < 10 ? '0' + X : 'A' + X - 10; } |
| 27 | |
| 28 void dumpLinkage(Ice::Ostream &Stream, | |
| 29 llvm::GlobalValue::LinkageTypes Linkage) { | |
| 30 switch (Linkage) { | |
| 31 case llvm::GlobalValue::ExternalLinkage: | |
| 32 Stream << "external"; | |
| 33 return; | |
| 34 case llvm::GlobalValue::InternalLinkage: | |
| 35 Stream << "internal"; | |
| 36 return; | |
| 37 default: | |
| 38 break; | |
| 39 } | |
| 40 std::string Buffer; | |
| 41 llvm::raw_string_ostream StrBuf(Buffer); | |
| 42 StrBuf << "Unknown linkage value: " << Linkage; | |
| 43 llvm::report_fatal_error(StrBuf.str()); | |
| 25 } | 44 } |
| 26 | 45 |
| 46 void dumpCallingConv(Ice::Ostream &, llvm::CallingConv::ID CallingConv) { | |
| 47 if (CallingConv == llvm::CallingConv::C) | |
| 48 return; | |
| 49 std::string Buffer; | |
| 50 llvm::raw_string_ostream StrBuf(Buffer); | |
| 51 StrBuf << "Unknown calling convention: " << CallingConv; | |
| 52 llvm::report_fatal_error(StrBuf.str()); | |
| 53 } | |
| 54 | |
| 55 } // end of anonymous namespace | |
| 56 | |
| 27 namespace Ice { | 57 namespace Ice { |
| 28 | 58 |
| 29 GlobalAddress::~GlobalAddress() { llvm::DeleteContainerPointers(Initializers); } | 59 IceString GlobalDeclaration::mangleName(GlobalContext *Ctx) const { |
| 60 return (getIsExternal() && !hasInitializer()) ? Name : Ctx->mangleName(Name); | |
|
jvoung (off chromium)
2014/10/10 23:01:16
Is it possible that IsExternal can hasInitializer(
Karl
2014/10/13 17:43:02
It can if you are assuming the block definition as
| |
| 61 } | |
| 30 | 62 |
| 31 void GlobalAddress::dumpType(Ostream &Stream) const { | 63 FunctionDeclaration * |
| 64 FunctionDeclaration::create(GlobalContext *Ctx, const FuncSigType &Signature, | |
| 65 llvm::CallingConv::ID CallingConv, | |
| 66 llvm::GlobalValue::LinkageTypes Linkage, | |
| 67 bool IsProto) { | |
| 68 return Ctx->newFunctionDeclaration(&Signature, CallingConv, Linkage, IsProto); | |
| 69 } | |
| 70 | |
| 71 void FunctionDeclaration::dumpType(Ostream &Stream) const { | |
| 72 Stream << Signature; | |
| 73 } | |
| 74 | |
| 75 void FunctionDeclaration::dump(Ostream &Stream) const { | |
| 76 if (IsProto) | |
| 77 Stream << "declare "; | |
| 78 ::dumpLinkage(Stream, Linkage); | |
| 79 ::dumpCallingConv(Stream, CallingConv); | |
| 80 Stream << Signature.getReturnType() << " @" << Name << "("; | |
| 81 bool IsFirst = true; | |
| 82 for (Type ArgTy : Signature.getArgList()) { | |
| 83 if (IsFirst) | |
| 84 IsFirst = false; | |
| 85 else | |
| 86 Stream << ", "; | |
| 87 Stream << ArgTy; | |
| 88 } | |
| 89 Stream << ")"; | |
| 90 } | |
| 91 | |
| 92 VariableDeclaration *VariableDeclaration::create(GlobalContext *Ctx) { | |
| 93 return Ctx->newVariableDeclaration(); | |
| 94 } | |
| 95 | |
| 96 VariableDeclaration::~VariableDeclaration() { | |
| 97 llvm::DeleteContainerPointers(Initializers); | |
| 98 } | |
| 99 | |
| 100 void VariableDeclaration::dumpType(Ostream &Stream) const { | |
| 32 if (Initializers.size() == 1) { | 101 if (Initializers.size() == 1) { |
| 33 Initializers.front()->dumpType(Stream); | 102 Initializers.front()->dumpType(Stream); |
| 34 } else { | 103 } else { |
| 35 Stream << "<{ "; | 104 Stream << "<{ "; |
| 36 bool IsFirst = true; | 105 bool IsFirst = true; |
| 37 for (Initializer *Init : Initializers) { | 106 for (Initializer *Init : Initializers) { |
| 38 if (IsFirst) { | 107 if (IsFirst) { |
| 39 IsFirst = false; | 108 IsFirst = false; |
| 40 } else { | 109 } else { |
| 41 Stream << ", "; | 110 Stream << ", "; |
| 42 } | 111 } |
| 43 Init->dumpType(Stream); | 112 Init->dumpType(Stream); |
| 44 } | 113 } |
| 45 Stream << " }>"; | 114 Stream << " }>"; |
| 46 } | 115 } |
| 47 } | 116 } |
| 48 | 117 |
| 49 void GlobalAddress::dump(Ostream &Stream) const { | 118 void VariableDeclaration::dump(Ostream &Stream) const { |
| 50 Stream << "@" << getName() << " = internal " | 119 Stream << "@" << Name << " = internal " |
| 51 << (IsConstant ? "constant" : "global") << " "; | 120 << (IsConstant ? "constant" : "global") << " "; |
| 52 | 121 |
| 53 // Add initializer. | 122 // Add initializer. |
| 54 if (Initializers.size() == 1) { | 123 if (Initializers.size() == 1) { |
| 55 Initializers.front()->dump(Stream); | 124 Initializers.front()->dump(Stream); |
| 56 } else { | 125 } else { |
| 57 dumpType(Stream); | 126 dumpType(Stream); |
| 58 Stream << " <{ "; | 127 Stream << " <{ "; |
| 59 bool IsFirst = true; | 128 bool IsFirst = true; |
| 60 for (Initializer *Init : Initializers) { | 129 for (Initializer *Init : Initializers) { |
| 61 if (IsFirst) { | 130 if (IsFirst) { |
| 62 IsFirst = false; | 131 IsFirst = false; |
| 63 } else { | 132 } else { |
| 64 Stream << ", "; | 133 Stream << ", "; |
| 65 } | 134 } |
| 66 Init->dump(Stream); | 135 Init->dump(Stream); |
| 67 } | 136 } |
| 68 Stream << " }>"; | 137 Stream << " }>"; |
| 69 } | 138 } |
| 70 | 139 |
| 71 // Add alignment. | 140 // Add alignment. |
| 72 if (Alignment > 0) | 141 if (Alignment > 0) |
| 73 Stream << ", align " << Alignment; | 142 Stream << ", align " << Alignment; |
| 74 Stream << "\n"; | 143 Stream << "\n"; |
| 75 } | 144 } |
| 76 | 145 |
| 77 void GlobalAddress::Initializer::dumpType(Ostream &Stream) const { | 146 void VariableDeclaration::Initializer::dumpType(Ostream &Stream) const { |
| 78 Stream << "[" << getNumBytes() << " x " << Ice::IceType_i8 << "]"; | 147 Stream << "[" << getNumBytes() << " x " << Ice::IceType_i8 << "]"; |
| 79 } | 148 } |
| 80 | 149 |
| 81 void GlobalAddress::DataInitializer::dump(Ostream &Stream) const { | 150 void VariableDeclaration::DataInitializer::dump(Ostream &Stream) const { |
| 82 dumpType(Stream); | 151 dumpType(Stream); |
| 83 Stream << " c\""; | 152 Stream << " c\""; |
| 84 // Code taken from PrintEscapedString() in AsmWriter.cpp. Keep | 153 // Code taken from PrintEscapedString() in AsmWriter.cpp. Keep |
| 85 // the strings in the same format as the .ll file for practical | 154 // the strings in the same format as the .ll file for practical |
| 86 // diffing. | 155 // diffing. |
| 87 for (uint8_t C : Contents) { | 156 for (uint8_t C : Contents) { |
| 88 if (isprint(C) && C != '\\' && C != '"') | 157 if (isprint(C) && C != '\\' && C != '"') |
| 89 Stream << C; | 158 Stream << C; |
| 90 else | 159 else |
| 91 Stream << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); | 160 Stream << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); |
| 92 } | 161 } |
| 93 Stream << "\""; | 162 Stream << "\""; |
| 94 } | 163 } |
| 95 | 164 |
| 96 void GlobalAddress::ZeroInitializer::dump(Ostream &Stream) const { | 165 void VariableDeclaration::ZeroInitializer::dump(Ostream &Stream) const { |
| 97 dumpType(Stream); | 166 dumpType(Stream); |
| 98 Stream << " zeroinitializer"; | 167 Stream << " zeroinitializer"; |
| 99 } | 168 } |
| 100 | 169 |
| 101 IceString GlobalAddress::RelocInitializer::getName() const { | 170 void VariableDeclaration::RelocInitializer::dumpType(Ostream &Stream) const { |
| 102 switch (Address.getKind()) { | |
| 103 case FunctionRelocation: | |
| 104 return Address.getFunction()->getName(); | |
| 105 case GlobalAddressRelocation: | |
| 106 return Address.getGlobalAddr()->getName(); | |
| 107 default: | |
| 108 llvm::report_fatal_error("Malformed relocation address!"); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void GlobalAddress::RelocInitializer::dumpType(Ostream &Stream) const { | |
| 113 Stream << Ice::IceType_i32; | 171 Stream << Ice::IceType_i32; |
| 114 } | 172 } |
| 115 | 173 |
| 116 void GlobalAddress::RelocInitializer::dump(Ostream &Stream) const { | 174 void VariableDeclaration::RelocInitializer::dump(Ostream &Stream) const { |
| 117 if (Offset != 0) { | 175 if (Offset != 0) { |
| 118 dumpType(Stream); | 176 dumpType(Stream); |
| 119 Stream << " add ("; | 177 Stream << " add ("; |
| 120 } | 178 } |
| 121 dumpType(Stream); | 179 dumpType(Stream); |
| 122 Stream << " ptrtoint ("; | 180 Stream << " ptrtoint ("; |
| 123 if (Address.getKind() == FunctionRelocation) { | 181 Declaration->dumpType(Stream); |
| 124 Stream << *Address.getFunction()->getType() << " @" | 182 Stream << "* @" << Declaration->getName() << " to "; |
| 125 << Address.getFunction()->getName(); | |
| 126 } else { | |
| 127 Address.getGlobalAddr()->dumpType(Stream); | |
| 128 Stream << "* @" << Address.getGlobalAddr()->getName(); | |
| 129 } | |
| 130 Stream << " to "; | |
| 131 dumpType(Stream); | 183 dumpType(Stream); |
| 132 Stream << ")"; | 184 Stream << ")"; |
| 133 if (Offset != 0) { | 185 if (Offset != 0) { |
| 134 Stream << ", "; | 186 Stream << ", "; |
| 135 dumpType(Stream); | 187 dumpType(Stream); |
| 136 Stream << " " << Offset << ")"; | 188 Stream << " " << Offset << ")"; |
| 137 } | 189 } |
| 138 } | 190 } |
| 139 } | 191 |
| 192 } // end of namespace Ice | |
| OLD | NEW |