OLD | NEW |
1 //===- subzero/src/IceGlobalInits.cpp - Global declarations ---------------===// | 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 function declarations, global | 10 // This file implements the notion of function declarations, global |
11 // variable declarations, and the corresponding variable initializers | 11 // variable declarations, and the corresponding variable initializers |
12 // in Subzero. | 12 // in Subzero. |
13 // | 13 // |
14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
15 | 15 |
16 #include "llvm/ADT/STLExtras.h" | 16 #include "llvm/ADT/STLExtras.h" |
17 #include "llvm/IR/Function.h" | 17 #include "llvm/IR/Function.h" |
18 #include "llvm/IR/Value.h" | 18 #include "llvm/IR/Value.h" |
19 | 19 |
20 #include "IceDefs.h" | 20 #include "IceDefs.h" |
21 #include "IceGlobalContext.h" | 21 #include "IceGlobalContext.h" |
22 #include "IceGlobalInits.h" | 22 #include "IceGlobalInits.h" |
23 #include "IceTypes.h" | 23 #include "IceTypes.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 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 | 27 |
28 void dumpLinkage(Ice::Ostream &Stream, | 28 void dumpLinkage(Ice::Ostream &Stream, |
29 llvm::GlobalValue::LinkageTypes Linkage) { | 29 llvm::GlobalValue::LinkageTypes Linkage) { |
| 30 if (!ALLOW_DUMP) |
| 31 return; |
30 switch (Linkage) { | 32 switch (Linkage) { |
31 case llvm::GlobalValue::ExternalLinkage: | 33 case llvm::GlobalValue::ExternalLinkage: |
32 Stream << "external"; | 34 Stream << "external"; |
33 return; | 35 return; |
34 case llvm::GlobalValue::InternalLinkage: | 36 case llvm::GlobalValue::InternalLinkage: |
35 Stream << "internal"; | 37 Stream << "internal"; |
36 return; | 38 return; |
37 default: | 39 default: |
38 break; | 40 break; |
39 } | 41 } |
40 std::string Buffer; | 42 std::string Buffer; |
41 llvm::raw_string_ostream StrBuf(Buffer); | 43 llvm::raw_string_ostream StrBuf(Buffer); |
42 StrBuf << "Unknown linkage value: " << Linkage; | 44 StrBuf << "Unknown linkage value: " << Linkage; |
43 llvm::report_fatal_error(StrBuf.str()); | 45 llvm::report_fatal_error(StrBuf.str()); |
44 } | 46 } |
45 | 47 |
46 void dumpCallingConv(Ice::Ostream &, llvm::CallingConv::ID CallingConv) { | 48 void dumpCallingConv(Ice::Ostream &, llvm::CallingConv::ID CallingConv) { |
| 49 if (!ALLOW_DUMP) |
| 50 return; |
47 if (CallingConv == llvm::CallingConv::C) | 51 if (CallingConv == llvm::CallingConv::C) |
48 return; | 52 return; |
49 std::string Buffer; | 53 std::string Buffer; |
50 llvm::raw_string_ostream StrBuf(Buffer); | 54 llvm::raw_string_ostream StrBuf(Buffer); |
51 StrBuf << "Unknown calling convention: " << CallingConv; | 55 StrBuf << "Unknown calling convention: " << CallingConv; |
52 llvm::report_fatal_error(StrBuf.str()); | 56 llvm::report_fatal_error(StrBuf.str()); |
53 } | 57 } |
54 | 58 |
55 } // end of anonymous namespace | 59 } // end of anonymous namespace |
56 | 60 |
57 namespace Ice { | 61 namespace Ice { |
58 | 62 |
59 FunctionDeclaration * | 63 FunctionDeclaration * |
60 FunctionDeclaration::create(GlobalContext *Ctx, const FuncSigType &Signature, | 64 FunctionDeclaration::create(GlobalContext *Ctx, const FuncSigType &Signature, |
61 llvm::CallingConv::ID CallingConv, | 65 llvm::CallingConv::ID CallingConv, |
62 llvm::GlobalValue::LinkageTypes Linkage, | 66 llvm::GlobalValue::LinkageTypes Linkage, |
63 bool IsProto) { | 67 bool IsProto) { |
64 return Ctx->newFunctionDeclaration(&Signature, CallingConv, Linkage, IsProto); | 68 return Ctx->newFunctionDeclaration(&Signature, CallingConv, Linkage, IsProto); |
65 } | 69 } |
66 | 70 |
67 void FunctionDeclaration::dumpType(Ostream &Stream) const { | 71 void FunctionDeclaration::dumpType(Ostream &Stream) const { |
| 72 if (!ALLOW_DUMP) |
| 73 return; |
68 Stream << Signature; | 74 Stream << Signature; |
69 } | 75 } |
70 | 76 |
71 void FunctionDeclaration::dump(GlobalContext *Ctx, Ostream &Stream) const { | 77 void FunctionDeclaration::dump(GlobalContext *Ctx, Ostream &Stream) const { |
| 78 if (!ALLOW_DUMP) |
| 79 return; |
72 if (IsProto) | 80 if (IsProto) |
73 Stream << "declare "; | 81 Stream << "declare "; |
74 ::dumpLinkage(Stream, Linkage); | 82 ::dumpLinkage(Stream, Linkage); |
75 ::dumpCallingConv(Stream, CallingConv); | 83 ::dumpCallingConv(Stream, CallingConv); |
76 Stream << Signature.getReturnType() << " @" | 84 Stream << Signature.getReturnType() << " @" |
77 << (Ctx ? Ctx->mangleName(Name) : Name) << "("; | 85 << (Ctx ? Ctx->mangleName(Name) : Name) << "("; |
78 bool IsFirst = true; | 86 bool IsFirst = true; |
79 for (Type ArgTy : Signature.getArgList()) { | 87 for (Type ArgTy : Signature.getArgList()) { |
80 if (IsFirst) | 88 if (IsFirst) |
81 IsFirst = false; | 89 IsFirst = false; |
82 else | 90 else |
83 Stream << ", "; | 91 Stream << ", "; |
84 Stream << ArgTy; | 92 Stream << ArgTy; |
85 } | 93 } |
86 Stream << ")"; | 94 Stream << ")"; |
87 } | 95 } |
88 | 96 |
89 VariableDeclaration *VariableDeclaration::create(GlobalContext *Ctx) { | 97 VariableDeclaration *VariableDeclaration::create(GlobalContext *Ctx) { |
90 return Ctx->newVariableDeclaration(); | 98 return Ctx->newVariableDeclaration(); |
91 } | 99 } |
92 | 100 |
93 VariableDeclaration::~VariableDeclaration() { | 101 VariableDeclaration::~VariableDeclaration() { |
94 llvm::DeleteContainerPointers(Initializers); | 102 llvm::DeleteContainerPointers(Initializers); |
95 } | 103 } |
96 | 104 |
97 void VariableDeclaration::dumpType(Ostream &Stream) const { | 105 void VariableDeclaration::dumpType(Ostream &Stream) const { |
| 106 if (!ALLOW_DUMP) |
| 107 return; |
98 if (Initializers.size() == 1) { | 108 if (Initializers.size() == 1) { |
99 Initializers.front()->dumpType(Stream); | 109 Initializers.front()->dumpType(Stream); |
100 } else { | 110 } else { |
101 Stream << "<{ "; | 111 Stream << "<{ "; |
102 bool IsFirst = true; | 112 bool IsFirst = true; |
103 for (Initializer *Init : Initializers) { | 113 for (Initializer *Init : Initializers) { |
104 if (IsFirst) { | 114 if (IsFirst) { |
105 IsFirst = false; | 115 IsFirst = false; |
106 } else { | 116 } else { |
107 Stream << ", "; | 117 Stream << ", "; |
108 } | 118 } |
109 Init->dumpType(Stream); | 119 Init->dumpType(Stream); |
110 } | 120 } |
111 Stream << " }>"; | 121 Stream << " }>"; |
112 } | 122 } |
113 } | 123 } |
114 | 124 |
115 void VariableDeclaration::dump(GlobalContext *Ctx, Ostream &Stream) const { | 125 void VariableDeclaration::dump(GlobalContext *Ctx, Ostream &Stream) const { |
| 126 if (!ALLOW_DUMP) |
| 127 return; |
116 Stream << "@" << ((Ctx && !getSuppressMangling()) | 128 Stream << "@" << ((Ctx && !getSuppressMangling()) |
117 ? Ctx->mangleName(Name) : Name) << " = "; | 129 ? Ctx->mangleName(Name) : Name) << " = "; |
118 ::dumpLinkage(Stream, Linkage); | 130 ::dumpLinkage(Stream, Linkage); |
119 Stream << " " << (IsConstant ? "constant" : "global") << " "; | 131 Stream << " " << (IsConstant ? "constant" : "global") << " "; |
120 | 132 |
121 // Add initializer. | 133 // Add initializer. |
122 if (Initializers.size() == 1) { | 134 if (Initializers.size() == 1) { |
123 Initializers.front()->dump(Stream); | 135 Initializers.front()->dump(Stream); |
124 } else { | 136 } else { |
125 dumpType(Stream); | 137 dumpType(Stream); |
(...skipping 10 matching lines...) Expand all Loading... |
136 Stream << " }>"; | 148 Stream << " }>"; |
137 } | 149 } |
138 | 150 |
139 // Add alignment. | 151 // Add alignment. |
140 if (Alignment > 0) | 152 if (Alignment > 0) |
141 Stream << ", align " << Alignment; | 153 Stream << ", align " << Alignment; |
142 Stream << "\n"; | 154 Stream << "\n"; |
143 } | 155 } |
144 | 156 |
145 void VariableDeclaration::Initializer::dumpType(Ostream &Stream) const { | 157 void VariableDeclaration::Initializer::dumpType(Ostream &Stream) const { |
| 158 if (!ALLOW_DUMP) |
| 159 return; |
146 Stream << "[" << getNumBytes() << " x " << Ice::IceType_i8 << "]"; | 160 Stream << "[" << getNumBytes() << " x " << Ice::IceType_i8 << "]"; |
147 } | 161 } |
148 | 162 |
149 void VariableDeclaration::DataInitializer::dump( | 163 void VariableDeclaration::DataInitializer::dump( |
150 GlobalContext *, Ostream &Stream) const { | 164 GlobalContext *, Ostream &Stream) const { |
| 165 if (!ALLOW_DUMP) |
| 166 return; |
151 dumpType(Stream); | 167 dumpType(Stream); |
152 Stream << " c\""; | 168 Stream << " c\""; |
153 // Code taken from PrintEscapedString() in AsmWriter.cpp. Keep | 169 // Code taken from PrintEscapedString() in AsmWriter.cpp. Keep |
154 // the strings in the same format as the .ll file for practical | 170 // the strings in the same format as the .ll file for practical |
155 // diffing. | 171 // diffing. |
156 for (uint8_t C : Contents) { | 172 for (uint8_t C : Contents) { |
157 if (isprint(C) && C != '\\' && C != '"') | 173 if (isprint(C) && C != '\\' && C != '"') |
158 Stream << C; | 174 Stream << C; |
159 else | 175 else |
160 Stream << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); | 176 Stream << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); |
161 } | 177 } |
162 Stream << "\""; | 178 Stream << "\""; |
163 } | 179 } |
164 | 180 |
165 void VariableDeclaration::ZeroInitializer::dump( | 181 void VariableDeclaration::ZeroInitializer::dump( |
166 GlobalContext *, Ostream &Stream) const { | 182 GlobalContext *, Ostream &Stream) const { |
| 183 if (!ALLOW_DUMP) |
| 184 return; |
167 dumpType(Stream); | 185 dumpType(Stream); |
168 Stream << " zeroinitializer"; | 186 Stream << " zeroinitializer"; |
169 } | 187 } |
170 | 188 |
171 void VariableDeclaration::RelocInitializer::dumpType(Ostream &Stream) const { | 189 void VariableDeclaration::RelocInitializer::dumpType(Ostream &Stream) const { |
| 190 if (!ALLOW_DUMP) |
| 191 return; |
172 Stream << Ice::IceType_i32; | 192 Stream << Ice::IceType_i32; |
173 } | 193 } |
174 | 194 |
175 void VariableDeclaration::RelocInitializer::dump( | 195 void VariableDeclaration::RelocInitializer::dump( |
176 GlobalContext *Ctx, Ostream &Stream) const { | 196 GlobalContext *Ctx, Ostream &Stream) const { |
| 197 if (!ALLOW_DUMP) |
| 198 return; |
177 if (Offset != 0) { | 199 if (Offset != 0) { |
178 dumpType(Stream); | 200 dumpType(Stream); |
179 Stream << " add ("; | 201 Stream << " add ("; |
180 } | 202 } |
181 dumpType(Stream); | 203 dumpType(Stream); |
182 Stream << " ptrtoint ("; | 204 Stream << " ptrtoint ("; |
183 Declaration->dumpType(Stream); | 205 Declaration->dumpType(Stream); |
184 Stream << "* @"; | 206 Stream << "* @"; |
185 if (Ctx) | 207 if (Ctx) |
186 Stream << Ctx->mangleName(Declaration->getName()); | 208 Stream << Ctx->mangleName(Declaration->getName()); |
187 else | 209 else |
188 Stream << Declaration->getName(); | 210 Stream << Declaration->getName(); |
189 Stream << " to "; | 211 Stream << " to "; |
190 dumpType(Stream); | 212 dumpType(Stream); |
191 Stream << ")"; | 213 Stream << ")"; |
192 if (Offset != 0) { | 214 if (Offset != 0) { |
193 Stream << ", "; | 215 Stream << ", "; |
194 dumpType(Stream); | 216 dumpType(Stream); |
195 Stream << " " << Offset << ")"; | 217 Stream << " " << Offset << ")"; |
196 } | 218 } |
197 } | 219 } |
198 | 220 |
199 } // end of namespace Ice | 221 } // end of namespace Ice |
OLD | NEW |