OLD | NEW |
---|---|
(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 | |
Derek Schuff
2013/10/15 17:39:26
I do like the idea of starting with the top-down d
Mark Seaborn
2013/10/15 21:41:18
Comment added to ExceptionInfoWriter.h.
| |
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 | |
Derek Schuff
2013/10/15 17:39:26
maybe say "represents" instead of "encodes" (the w
Mark Seaborn
2013/10/15 21:41:18
Hmm, I thought "represent" would carry the same im
Derek Schuff
2013/10/15 22:36:20
Yeah, after seeing it the other way, it doesn't re
Mark Seaborn
2013/10/15 23:08:57
OK, I've changed it back to "encode".
| |
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 uint32_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 // The C++ runtime library checks the clauses in order to decide | |
79 // whether to enter the landingpad. If a clause matches, the | |
80 // landingpad BasicBlock is passed the clause ID. The landingpad code | |
81 // can use the clause ID to decide which C++ catch() block (if any) to | |
82 // execute. | |
83 // | |
84 // The purpose of these exception tables is to keep code sizes | |
85 // relatively small. The landingpad code only needs to check a small | |
86 // integer clause ID, rather than having to call a function to check | |
87 // whether the C++ exception matches a type. | |
88 // | |
89 // ExceptionInfoWriter's encoding corresponds loosely to the format of | |
90 // GCC's .gcc_except_table sections. One difference is that | |
91 // ExceptionInfoWriter writes fixed-width 32-bit integers, whereas | |
92 // .gcc_except_table uses variable-length LEB128 encodings. We could | |
93 // switch to LEB128 to save space in the future. | |
94 // | |
95 //===----------------------------------------------------------------------===// | |
96 | |
97 #include "ExceptionInfoWriter.h" | |
98 #include "llvm/Support/raw_ostream.h" | |
99 | |
100 using namespace llvm; | |
101 | |
102 ExceptionInfoWriter::ExceptionInfoWriter(LLVMContext *Context): | |
103 Context(Context) { | |
104 Type *I32 = Type::getInt32Ty(*Context); | |
105 Type *Fields[] = { I32, I32 }; | |
106 ActionTableEntryTy = StructType::create(Fields, "action_table_entry"); | |
107 } | |
108 | |
109 unsigned ExceptionInfoWriter::getIDForExceptionType(Value *ExcTy) { | |
110 Constant *ExcTyConst = dyn_cast<Constant>(ExcTy); | |
111 if (!ExcTyConst) | |
112 report_fatal_error("Exception type not a constant"); | |
Derek Schuff
2013/10/15 17:39:26
We've been using report_fatal_error for things tha
Mark Seaborn
2013/10/15 21:41:18
Not sure I agree. It's inelegant for the module v
| |
113 | |
114 // Reuse existing ID if one has already been assigned. | |
115 TypeTableIDMapType::iterator Iter = TypeTableIDMap.find(ExcTyConst); | |
116 if (Iter != TypeTableIDMap.end()) | |
117 return Iter->second; | |
118 | |
119 unsigned Index = TypeTableData.size(); | |
120 TypeTableIDMap[ExcTyConst] = Index; | |
121 TypeTableData.push_back(ExcTyConst); | |
122 return Index; | |
123 } | |
124 | |
125 unsigned ExceptionInfoWriter::getIDForClauseListNode( | |
126 unsigned ClauseID, unsigned NextClauseListID) { | |
127 // Reuse existing ID if one has already been assigned. | |
128 ActionTableEntry Key(ClauseID, NextClauseListID); | |
129 ActionTableIDMapType::iterator Iter = ActionTableIDMap.find(Key); | |
130 if (Iter != ActionTableIDMap.end()) | |
131 return Iter->second; | |
132 | |
133 Type *I32 = Type::getInt32Ty(*Context); | |
134 Constant *Fields[] = { ConstantInt::get(I32, ClauseID), | |
135 ConstantInt::get(I32, NextClauseListID) }; | |
136 Constant *Entry = ConstantStruct::get(ActionTableEntryTy, Fields); | |
137 | |
138 // Add 1 so that the empty list can be represent as 0. | |
Derek Schuff
2013/10/15 17:39:26
represent->represented
Mark Seaborn
2013/10/15 21:41:18
Done.
| |
139 unsigned ClauseListID = ActionTableData.size() + 1; | |
140 ActionTableIDMap[Key] = ClauseListID; | |
141 ActionTableData.push_back(Entry); | |
142 return ClauseListID; | |
143 } | |
144 | |
145 unsigned ExceptionInfoWriter::getIDForFilterClause(Value *Filter) { | |
146 unsigned FilterClauseID = -(FilterTableData.size() + 1); | |
147 Type *I32 = Type::getInt32Ty(*Context); | |
148 ArrayType *ArrayTy = dyn_cast<ArrayType>(Filter->getType()); | |
149 if (!ArrayTy) | |
150 report_fatal_error("Landingpad filter clause is not of array type"); | |
151 unsigned FilterLength = ArrayTy->getNumElements(); | |
152 // Don't try the dyn_cast if the FilterLength is zero, because Array | |
153 // could be a zeroinitializer. | |
154 if (FilterLength > 0) { | |
155 ConstantArray *Array = dyn_cast<ConstantArray>(Filter); | |
156 if (!Array) | |
157 report_fatal_error("Landingpad filter clause is not a ConstantArray"); | |
158 for (unsigned I = 0; I < FilterLength; ++I) { | |
159 unsigned TypeID = getIDForExceptionType(Array->getOperand(I)); | |
160 FilterTableData.push_back(ConstantInt::get(I32, TypeID)); | |
161 } | |
162 } | |
163 // Add array terminator. | |
164 FilterTableData.push_back(ConstantInt::get(I32, -1)); | |
165 return FilterClauseID; | |
166 } | |
167 | |
168 unsigned ExceptionInfoWriter::getIDForLandingPadClauseList(LandingPadInst *LP) { | |
169 unsigned NextClauseListID = 0; // ID for empty list. | |
170 | |
171 if (LP->isCleanup()) { | |
172 // Add catch-all entry. There doesn't appear to be any need to | |
173 // treat "cleanup" differently from a catch-all. | |
174 unsigned TypeID = getIDForExceptionType( | |
175 ConstantPointerNull::get(Type::getInt8PtrTy(*Context))); | |
176 NextClauseListID = getIDForClauseListNode(TypeID, NextClauseListID); | |
177 } | |
178 | |
179 for (int I = (int) LP->getNumClauses() - 1; I >= 0; --I) { | |
180 unsigned ClauseID; | |
181 if (LP->isCatch(I)) { | |
182 ClauseID = getIDForExceptionType(LP->getClause(I)); | |
183 } else if (LP->isFilter(I)) { | |
184 ClauseID = getIDForFilterClause(LP->getClause(I)); | |
185 } else { | |
186 report_fatal_error("Unknown kind of landingpad clause"); | |
187 } | |
188 NextClauseListID = getIDForClauseListNode(ClauseID, NextClauseListID); | |
189 } | |
190 | |
191 return NextClauseListID; | |
192 } | |
193 | |
194 static void defineArray(Module *M, const char *Name, | |
195 const SmallVectorImpl<Constant *> &Elements, | |
196 Type *ElementType) { | |
197 ArrayType *ArrayTy = ArrayType::get(ElementType, Elements.size()); | |
198 Constant *ArrayData = ConstantArray::get(ArrayTy, Elements); | |
199 GlobalValue *OldGlobal = M->getGlobalVariable(Name); | |
200 if (OldGlobal) { | |
Derek Schuff
2013/10/15 17:39:26
If there does not exist a variable by that name al
Mark Seaborn
2013/10/15 21:41:18
This code is expecting an external reference to __
| |
201 Constant *NewGlobal = new GlobalVariable( | |
202 *M, ArrayTy, /* isConstant= */ true, | |
203 GlobalValue::InternalLinkage, ArrayData); | |
204 NewGlobal->takeName(OldGlobal); | |
205 OldGlobal->replaceAllUsesWith(ConstantExpr::getBitCast( | |
206 NewGlobal, OldGlobal->getType())); | |
207 OldGlobal->eraseFromParent(); | |
208 } else { | |
209 if (Elements.size() > 0) { | |
210 // This warning could happen for a program that does not link | |
211 // against the C++ runtime libraries. Such a program might | |
212 // contain "invoke" instructions but never throw any C++ | |
213 // exceptions. | |
214 errs() << "Warning: Variable " << Name << " not referenced\n"; | |
215 } | |
216 } | |
217 } | |
218 | |
219 void ExceptionInfoWriter::defineGlobalVariables(Module *M) { | |
220 defineArray(M, "__pnacl_eh_type_table", TypeTableData, | |
221 Type::getInt8PtrTy(M->getContext())); | |
222 | |
223 defineArray(M, "__pnacl_eh_action_table", ActionTableData, | |
224 ActionTableEntryTy); | |
225 | |
226 defineArray(M, "__pnacl_eh_filter_table", FilterTableData, | |
227 Type::getInt32Ty(M->getContext())); | |
228 } | |
OLD | NEW |