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

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

Issue 24777002: Add PNaClSjLjEH pass to implement C++ exception handling using setjmp()+longjmp() (Closed) Base URL: http://git.chromium.org/native_client/pnacl-llvm.git@master
Patch Set: Retry upload Created 7 years, 2 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
« no previous file with comments | « lib/Transforms/NaCl/ExceptionInfoWriter.h ('k') | lib/Transforms/NaCl/PNaClSjLjEH.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 integer "type ID" @ExcType, X,
38 // such that: __pnacl_eh_type_table[X] == @ExcType, and X >= 0.
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 -1-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 // * ExceptionInfoWriter encodes this the same as "catch i8* null"
55 // (which is a catch-all).
56 //
57 // ExceptionInfoWriter generates the following data structures:
58 //
59 // struct action_table_entry {
60 // int32_t clause_id;
61 // uint32_t next_clause_list_id;
62 // };
63 //
64 // // Represents singly linked lists of clauses.
65 // extern const struct action_table_entry __pnacl_eh_action_table[];
66 //
67 // // Allows std::type_infos to be represented using small integer IDs.
68 // extern std::type_info *const __pnacl_eh_type_table[];
69 //
70 // // Used to represent type arrays for "filter" clauses.
71 // extern const int32_t __pnacl_eh_filter_table[];
72 //
73 // A "clause list ID" is either:
74 // * 0, representing the empty list; or
75 // * an index into __pnacl_eh_action_table[] with 1 added, which
76 // specifies a node in the clause list.
77 //
78 // Example:
79 //
80 // std::type_info *const __pnacl_eh_type_table[] = {
81 // // defines type ID 0 == ExcA and clause ID 0 == "catch ExcA"
82 // &typeinfo(ExcA),
83 // // defines type ID 1 == ExcB and clause ID 1 == "catch ExcB"
84 // &typeinfo(ExcB),
85 // // defines type ID 2 == ExcC and clause ID 2 == "catch ExcC"
86 // &typeinfo(ExcC),
87 // };
88 //
89 // const int32_t __pnacl_eh_filter_table[] = {
90 // 0, // refers to ExcA; defines clause ID -1 as "filter [ExcA, ExcB]"
91 // 1, // refers to ExcB; defines clause ID -2 as "filter [ExcB]"
92 // -1, // list terminator; defines clause ID -3 as "filter []"
93 // 2, // refers to ExcC; defines clause ID -4 as "filter [ExcC]"
94 // -1, // list terminator; defines clause ID -5 as "filter []"
95 // };
96 //
97 // const struct action_table_entry __pnacl_eh_action_table[] = {
98 // // defines clause list ID 1:
99 // {
100 // -4, // "filter [ExcC]"
101 // 0, // end of list (no more actions)
102 // },
103 // // defines clause list ID 2:
104 // {
105 // -1, // "filter [ExcA, ExcB]"
106 // 1, // else go to clause list ID 1
107 // },
108 // // defines clause list ID 3:
109 // {
110 // 1, // "catch ExcB"
111 // 2, // else go to clause list ID 2
112 // },
113 // // defines clause list ID 4:
114 // {
115 // 0, // "catch ExcA"
116 // 3, // else go to clause list ID 3
117 // },
118 // };
119 //
120 // So if a landingpad contains the clause list:
121 // [catch ExcA,
122 // catch ExcB,
123 // filter [ExcA, ExcB],
124 // filter [ExcC]]
125 // then this can be represented as clause list ID 4 using the tables above.
126 //
127 // The C++ runtime library checks the clauses in order to decide
128 // whether to enter the landingpad. If a clause matches, the
129 // landingpad BasicBlock is passed the clause ID. The landingpad code
130 // can use the clause ID to decide which C++ catch() block (if any) to
131 // execute.
132 //
133 // The purpose of these exception tables is to keep code sizes
134 // relatively small. The landingpad code only needs to check a small
135 // integer clause ID, rather than having to call a function to check
136 // whether the C++ exception matches a type.
137 //
138 // ExceptionInfoWriter's encoding corresponds loosely to the format of
139 // GCC's .gcc_except_table sections. One difference is that
140 // ExceptionInfoWriter writes fixed-width 32-bit integers, whereas
141 // .gcc_except_table uses variable-length LEB128 encodings. We could
142 // switch to LEB128 to save space in the future.
143 //
144 //===----------------------------------------------------------------------===//
145
146 #include "ExceptionInfoWriter.h"
147 #include "llvm/Support/raw_ostream.h"
148
149 using namespace llvm;
150
151 ExceptionInfoWriter::ExceptionInfoWriter(LLVMContext *Context):
152 Context(Context) {
153 Type *I32 = Type::getInt32Ty(*Context);
154 Type *Fields[] = { I32, I32 };
155 ActionTableEntryTy = StructType::create(Fields, "action_table_entry");
156 }
157
158 unsigned ExceptionInfoWriter::getIDForExceptionType(Value *ExcTy) {
159 Constant *ExcTyConst = dyn_cast<Constant>(ExcTy);
160 if (!ExcTyConst)
161 report_fatal_error("Exception type not a constant");
162
163 // Reuse existing ID if one has already been assigned.
164 TypeTableIDMapType::iterator Iter = TypeTableIDMap.find(ExcTyConst);
165 if (Iter != TypeTableIDMap.end())
166 return Iter->second;
167
168 unsigned Index = TypeTableData.size();
169 TypeTableIDMap[ExcTyConst] = Index;
170 TypeTableData.push_back(ExcTyConst);
171 return Index;
172 }
173
174 unsigned ExceptionInfoWriter::getIDForClauseListNode(
175 unsigned ClauseID, unsigned NextClauseListID) {
176 // Reuse existing ID if one has already been assigned.
177 ActionTableEntry Key(ClauseID, NextClauseListID);
178 ActionTableIDMapType::iterator Iter = ActionTableIDMap.find(Key);
179 if (Iter != ActionTableIDMap.end())
180 return Iter->second;
181
182 Type *I32 = Type::getInt32Ty(*Context);
183 Constant *Fields[] = { ConstantInt::get(I32, ClauseID),
184 ConstantInt::get(I32, NextClauseListID) };
185 Constant *Entry = ConstantStruct::get(ActionTableEntryTy, Fields);
186
187 // Add 1 so that the empty list can be represented as 0.
188 unsigned ClauseListID = ActionTableData.size() + 1;
189 ActionTableIDMap[Key] = ClauseListID;
190 ActionTableData.push_back(Entry);
191 return ClauseListID;
192 }
193
194 unsigned ExceptionInfoWriter::getIDForFilterClause(Value *Filter) {
195 unsigned FilterClauseID = -(FilterTableData.size() + 1);
196 Type *I32 = Type::getInt32Ty(*Context);
197 ArrayType *ArrayTy = dyn_cast<ArrayType>(Filter->getType());
198 if (!ArrayTy)
199 report_fatal_error("Landingpad filter clause is not of array type");
200 unsigned FilterLength = ArrayTy->getNumElements();
201 // Don't try the dyn_cast if the FilterLength is zero, because Array
202 // could be a zeroinitializer.
203 if (FilterLength > 0) {
204 ConstantArray *Array = dyn_cast<ConstantArray>(Filter);
205 if (!Array)
206 report_fatal_error("Landingpad filter clause is not a ConstantArray");
207 for (unsigned I = 0; I < FilterLength; ++I) {
208 unsigned TypeID = getIDForExceptionType(Array->getOperand(I));
209 FilterTableData.push_back(ConstantInt::get(I32, TypeID));
210 }
211 }
212 // Add array terminator.
213 FilterTableData.push_back(ConstantInt::get(I32, -1));
214 return FilterClauseID;
215 }
216
217 unsigned ExceptionInfoWriter::getIDForLandingPadClauseList(LandingPadInst *LP) {
218 unsigned NextClauseListID = 0; // ID for empty list.
219
220 if (LP->isCleanup()) {
221 // Add catch-all entry. There doesn't appear to be any need to
222 // treat "cleanup" differently from a catch-all.
223 unsigned TypeID = getIDForExceptionType(
224 ConstantPointerNull::get(Type::getInt8PtrTy(*Context)));
225 NextClauseListID = getIDForClauseListNode(TypeID, NextClauseListID);
226 }
227
228 for (int I = (int) LP->getNumClauses() - 1; I >= 0; --I) {
229 unsigned ClauseID;
230 if (LP->isCatch(I)) {
231 ClauseID = getIDForExceptionType(LP->getClause(I));
232 } else if (LP->isFilter(I)) {
233 ClauseID = getIDForFilterClause(LP->getClause(I));
234 } else {
235 report_fatal_error("Unknown kind of landingpad clause");
236 }
237 NextClauseListID = getIDForClauseListNode(ClauseID, NextClauseListID);
238 }
239
240 return NextClauseListID;
241 }
242
243 static void defineArray(Module *M, const char *Name,
244 const SmallVectorImpl<Constant *> &Elements,
245 Type *ElementType) {
246 ArrayType *ArrayTy = ArrayType::get(ElementType, Elements.size());
247 Constant *ArrayData = ConstantArray::get(ArrayTy, Elements);
248 GlobalVariable *OldGlobal = M->getGlobalVariable(Name);
249 if (OldGlobal) {
250 if (OldGlobal->hasInitializer()) {
251 report_fatal_error(std::string("Variable ") + Name +
252 " already has an initializer");
253 }
254 Constant *NewGlobal = new GlobalVariable(
255 *M, ArrayTy, /* isConstant= */ true,
256 GlobalValue::InternalLinkage, ArrayData);
257 NewGlobal->takeName(OldGlobal);
258 OldGlobal->replaceAllUsesWith(ConstantExpr::getBitCast(
259 NewGlobal, OldGlobal->getType()));
260 OldGlobal->eraseFromParent();
261 } else {
262 if (Elements.size() > 0) {
263 // This warning could happen for a program that does not link
264 // against the C++ runtime libraries. Such a program might
265 // contain "invoke" instructions but never throw any C++
266 // exceptions.
267 errs() << "Warning: Variable " << Name << " not referenced\n";
268 }
269 }
270 }
271
272 void ExceptionInfoWriter::defineGlobalVariables(Module *M) {
273 defineArray(M, "__pnacl_eh_type_table", TypeTableData,
274 Type::getInt8PtrTy(M->getContext()));
275
276 defineArray(M, "__pnacl_eh_action_table", ActionTableData,
277 ActionTableEntryTy);
278
279 defineArray(M, "__pnacl_eh_filter_table", FilterTableData,
280 Type::getInt32Ty(M->getContext()));
281 }
OLDNEW
« no previous file with comments | « lib/Transforms/NaCl/ExceptionInfoWriter.h ('k') | lib/Transforms/NaCl/PNaClSjLjEH.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698