Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: lib/Transforms/NaCl/ExceptionInfoWriter.cpp

Issue 939073008: Rebased PNaCl localmods in LLVM to 223109 (Closed)
Patch Set: undo localmod Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //===- ExceptionInfoWriter.cpp - Generate C++ exception info for PNaCl-----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The ExceptionInfoWriter class converts the clauses of a
11 // "landingpad" instruction into data tables stored in global
12 // variables. These tables are interpreted by PNaCl's C++ runtime
13 // library (either libsupc++ or libcxxabi), which is linked into a
14 // pexe.
15 //
16 // This is similar to the lowering that the LLVM backend does to
17 // convert landingpad clauses into ".gcc_except_table" sections. The
18 // difference is that ExceptionInfoWriter is an IR-to-IR
19 // transformation that runs on the PNaCl user toolchain side. The
20 // format it produces is not part of PNaCl's stable ABI; the PNaCl
21 // translator and LLVM backend do not know about this format.
22 //
23 // Encoding:
24 //
25 // A landingpad instruction contains a list of clauses.
26 // ExceptionInfoWriter encodes each clause as a 32-bit "clause ID". A
27 // clause is one of the following forms:
28 //
29 // 1) "catch i8* @ExcType"
30 // * This clause means that the landingpad should be entered if
31 // the C++ exception being thrown has type @ExcType (or a
32 // subtype of @ExcType). @ExcType is a pointer to the
33 // std::type_info object (an RTTI object) for the C++ exception
34 // type.
35 // * Clang generates this for a "catch" block in the C++ source.
36 // * @ExcType is NULL for "catch (...)" (catch-all) blocks.
37 // * This is encoded as the "type ID" for @ExcType, defined below,
38 // which is a positive integer.
39 //
40 // 2) "filter [i8* @ExcType1, ..., i8* @ExcTypeN]"
41 // * This clause means that the landingpad should be entered if
42 // the C++ exception being thrown *doesn't* match any of the
43 // types in the list (which are again specified as
44 // std::type_info pointers).
45 // * Clang uses this to implement C++ exception specifications, e.g.
46 // void foo() throw(ExcType1, ..., ExcTypeN) { ... }
47 // * This is encoded as the filter ID, X, where X < 0, and
48 // &__pnacl_eh_filter_table[-X-1] points to a 0-terminated
49 // array of integer "type IDs".
50 //
51 // 3) "cleanup"
52 // * This means that the landingpad should always be entered.
53 // * Clang uses this for calling objects' destructors.
54 // * This is encoded as 0.
55 // * The runtime may treat "cleanup" differently from "catch i8*
56 // null" (a catch-all). In C++, if an unhandled exception
57 // occurs, the language runtime may abort execution without
58 // running any destructors. The runtime may implement this by
59 // searching for a matching non-"cleanup" clause, and aborting
60 // if it does not find one, before entering any landingpad
61 // blocks.
62 //
63 // The "type ID" for a type @ExcType is a 1-based index into the array
64 // __pnacl_eh_type_table[]. That is, the type ID is a value X such
65 // that __pnacl_eh_type_table[X-1] == @ExcType, and X >= 1.
66 //
67 // ExceptionInfoWriter generates the following data structures:
68 //
69 // struct action_table_entry {
70 // int32_t clause_id;
71 // uint32_t next_clause_list_id;
72 // };
73 //
74 // // Represents singly linked lists of clauses.
75 // extern const struct action_table_entry __pnacl_eh_action_table[];
76 //
77 // // Allows std::type_infos to be represented using small integer IDs.
78 // extern std::type_info *const __pnacl_eh_type_table[];
79 //
80 // // Used to represent type arrays for "filter" clauses.
81 // extern const uint32_t __pnacl_eh_filter_table[];
82 //
83 // A "clause list ID" is either:
84 // * 0, representing the empty list; or
85 // * an index into __pnacl_eh_action_table[] with 1 added, which
86 // specifies a node in the clause list.
87 //
88 // Example:
89 //
90 // std::type_info *const __pnacl_eh_type_table[] = {
91 // // defines type ID 1 == ExcA and clause ID 1 == "catch ExcA"
92 // &typeinfo(ExcA),
93 // // defines type ID 2 == ExcB and clause ID 2 == "catch ExcB"
94 // &typeinfo(ExcB),
95 // // defines type ID 3 == ExcC and clause ID 3 == "catch ExcC"
96 // &typeinfo(ExcC),
97 // };
98 //
99 // const uint32_t __pnacl_eh_filter_table[] = {
100 // 1, // refers to ExcA; defines clause ID -1 as "filter [ExcA, ExcB]"
101 // 2, // refers to ExcB; defines clause ID -2 as "filter [ExcB]"
102 // 0, // list terminator; defines clause ID -3 as "filter []"
103 // 3, // refers to ExcC; defines clause ID -4 as "filter [ExcC]"
104 // 0, // list terminator; defines clause ID -5 as "filter []"
105 // };
106 //
107 // const struct action_table_entry __pnacl_eh_action_table[] = {
108 // // defines clause list ID 1:
109 // {
110 // -4, // "filter [ExcC]"
111 // 0, // end of list (no more actions)
112 // },
113 // // defines clause list ID 2:
114 // {
115 // -1, // "filter [ExcA, ExcB]"
116 // 1, // else go to clause list ID 1
117 // },
118 // // defines clause list ID 3:
119 // {
120 // 2, // "catch ExcB"
121 // 2, // else go to clause list ID 2
122 // },
123 // // defines clause list ID 4:
124 // {
125 // 1, // "catch ExcA"
126 // 3, // else go to clause list ID 3
127 // },
128 // };
129 //
130 // So if a landingpad contains the clause list:
131 // [catch ExcA,
132 // catch ExcB,
133 // filter [ExcA, ExcB],
134 // filter [ExcC]]
135 // then this can be represented as clause list ID 4 using the tables above.
136 //
137 // The C++ runtime library checks the clauses in order to decide
138 // whether to enter the landingpad. If a clause matches, the
139 // landingpad BasicBlock is passed the clause ID. The landingpad code
140 // can use the clause ID to decide which C++ catch() block (if any) to
141 // execute.
142 //
143 // The purpose of these exception tables is to keep code sizes
144 // relatively small. The landingpad code only needs to check a small
145 // integer clause ID, rather than having to call a function to check
146 // whether the C++ exception matches a type.
147 //
148 // ExceptionInfoWriter's encoding corresponds loosely to the format of
149 // GCC's .gcc_except_table sections. One difference is that
150 // ExceptionInfoWriter writes fixed-width 32-bit integers, whereas
151 // .gcc_except_table uses variable-length LEB128 encodings. We could
152 // switch to LEB128 to save space in the future.
153 //
154 //===----------------------------------------------------------------------===//
155
156 #include "ExceptionInfoWriter.h"
157 #include "llvm/IR/Constants.h"
158 #include "llvm/Support/raw_ostream.h"
159
160 using namespace llvm;
161
162 ExceptionInfoWriter::ExceptionInfoWriter(LLVMContext *Context):
163 Context(Context) {
164 Type *I32 = Type::getInt32Ty(*Context);
165 Type *Fields[] = { I32, I32 };
166 ActionTableEntryTy = StructType::create(Fields, "action_table_entry");
167 }
168
169 unsigned ExceptionInfoWriter::getIDForExceptionType(Value *ExcTy) {
170 Constant *ExcTyConst = dyn_cast<Constant>(ExcTy);
171 if (!ExcTyConst)
172 report_fatal_error("Exception type not a constant");
173
174 // Reuse existing ID if one has already been assigned.
175 TypeTableIDMapType::iterator Iter = TypeTableIDMap.find(ExcTyConst);
176 if (Iter != TypeTableIDMap.end())
177 return Iter->second;
178
179 unsigned Index = TypeTableData.size() + 1;
180 TypeTableIDMap[ExcTyConst] = Index;
181 TypeTableData.push_back(ExcTyConst);
182 return Index;
183 }
184
185 unsigned ExceptionInfoWriter::getIDForClauseListNode(
186 unsigned ClauseID, unsigned NextClauseListID) {
187 // Reuse existing ID if one has already been assigned.
188 ActionTableEntry Key(ClauseID, NextClauseListID);
189 ActionTableIDMapType::iterator Iter = ActionTableIDMap.find(Key);
190 if (Iter != ActionTableIDMap.end())
191 return Iter->second;
192
193 Type *I32 = Type::getInt32Ty(*Context);
194 Constant *Fields[] = { ConstantInt::get(I32, ClauseID),
195 ConstantInt::get(I32, NextClauseListID) };
196 Constant *Entry = ConstantStruct::get(ActionTableEntryTy, Fields);
197
198 // Add 1 so that the empty list can be represented as 0.
199 unsigned ClauseListID = ActionTableData.size() + 1;
200 ActionTableIDMap[Key] = ClauseListID;
201 ActionTableData.push_back(Entry);
202 return ClauseListID;
203 }
204
205 unsigned ExceptionInfoWriter::getIDForFilterClause(Value *Filter) {
206 unsigned FilterClauseID = -(FilterTableData.size() + 1);
207 Type *I32 = Type::getInt32Ty(*Context);
208 ArrayType *ArrayTy = dyn_cast<ArrayType>(Filter->getType());
209 if (!ArrayTy)
210 report_fatal_error("Landingpad filter clause is not of array type");
211 unsigned FilterLength = ArrayTy->getNumElements();
212 // Don't try the dyn_cast if the FilterLength is zero, because Array
213 // could be a zeroinitializer.
214 if (FilterLength > 0) {
215 ConstantArray *Array = dyn_cast<ConstantArray>(Filter);
216 if (!Array)
217 report_fatal_error("Landingpad filter clause is not a ConstantArray");
218 for (unsigned I = 0; I < FilterLength; ++I) {
219 unsigned TypeID = getIDForExceptionType(Array->getOperand(I));
220 assert(TypeID > 0);
221 FilterTableData.push_back(ConstantInt::get(I32, TypeID));
222 }
223 }
224 // Add array terminator.
225 FilterTableData.push_back(ConstantInt::get(I32, 0));
226 return FilterClauseID;
227 }
228
229 unsigned ExceptionInfoWriter::getIDForLandingPadClauseList(LandingPadInst *LP) {
230 unsigned NextClauseListID = 0; // ID for empty list.
231
232 if (LP->isCleanup()) {
233 // Add cleanup clause at the end of the list.
234 NextClauseListID = getIDForClauseListNode(0, NextClauseListID);
235 }
236
237 for (int I = (int) LP->getNumClauses() - 1; I >= 0; --I) {
238 unsigned ClauseID;
239 if (LP->isCatch(I)) {
240 ClauseID = getIDForExceptionType(LP->getClause(I));
241 } else if (LP->isFilter(I)) {
242 ClauseID = getIDForFilterClause(LP->getClause(I));
243 } else {
244 report_fatal_error("Unknown kind of landingpad clause");
245 }
246 assert(ClauseID > 0);
247 NextClauseListID = getIDForClauseListNode(ClauseID, NextClauseListID);
248 }
249
250 return NextClauseListID;
251 }
252
253 static void defineArray(Module *M, const char *Name,
254 const SmallVectorImpl<Constant *> &Elements,
255 Type *ElementType) {
256 ArrayType *ArrayTy = ArrayType::get(ElementType, Elements.size());
257 Constant *ArrayData = ConstantArray::get(ArrayTy, Elements);
258 GlobalVariable *OldGlobal = M->getGlobalVariable(Name);
259 if (OldGlobal) {
260 if (OldGlobal->hasInitializer()) {
261 report_fatal_error(std::string("Variable ") + Name +
262 " already has an initializer");
263 }
264 Constant *NewGlobal = new GlobalVariable(
265 *M, ArrayTy, /* isConstant= */ true,
266 GlobalValue::InternalLinkage, ArrayData);
267 NewGlobal->takeName(OldGlobal);
268 OldGlobal->replaceAllUsesWith(ConstantExpr::getBitCast(
269 NewGlobal, OldGlobal->getType()));
270 OldGlobal->eraseFromParent();
271 } else {
272 if (Elements.size() > 0) {
273 // This warning could happen for a program that does not link
274 // against the C++ runtime libraries. Such a program might
275 // contain "invoke" instructions but never throw any C++
276 // exceptions.
277 errs() << "Warning: Variable " << Name << " not referenced\n";
278 }
279 }
280 }
281
282 void ExceptionInfoWriter::defineGlobalVariables(Module *M) {
283 defineArray(M, "__pnacl_eh_type_table", TypeTableData,
284 Type::getInt8PtrTy(M->getContext()));
285
286 defineArray(M, "__pnacl_eh_action_table", ActionTableData,
287 ActionTableEntryTy);
288
289 defineArray(M, "__pnacl_eh_filter_table", FilterTableData,
290 Type::getInt32Ty(M->getContext()));
291 }
OLDNEW
« no previous file with comments | « lib/Transforms/NaCl/ExceptionInfoWriter.h ('k') | lib/Transforms/NaCl/ExpandArithWithOverflow.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698