/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaStmtAsm.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SemaStmtAsm.cpp - Semantic Analysis for Asm Statements -----------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements semantic analysis for inline asm statements. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/ExprCXX.h" |
14 | | #include "clang/AST/GlobalDecl.h" |
15 | | #include "clang/AST/RecordLayout.h" |
16 | | #include "clang/AST/TypeLoc.h" |
17 | | #include "clang/Basic/TargetInfo.h" |
18 | | #include "clang/Lex/Preprocessor.h" |
19 | | #include "clang/Sema/Initialization.h" |
20 | | #include "clang/Sema/Lookup.h" |
21 | | #include "clang/Sema/Scope.h" |
22 | | #include "clang/Sema/ScopeInfo.h" |
23 | | #include "clang/Sema/SemaInternal.h" |
24 | | #include "llvm/ADT/ArrayRef.h" |
25 | | #include "llvm/ADT/StringSet.h" |
26 | | #include "llvm/MC/MCParser/MCAsmParser.h" |
27 | | using namespace clang; |
28 | | using namespace sema; |
29 | | |
30 | | /// Remove the upper-level LValueToRValue cast from an expression. |
31 | 6 | static void removeLValueToRValueCast(Expr *E) { |
32 | 6 | Expr *Parent = E; |
33 | 6 | Expr *ExprUnderCast = nullptr; |
34 | 6 | SmallVector<Expr *, 8> ParentsToUpdate; |
35 | | |
36 | 7 | while (true) { |
37 | 7 | ParentsToUpdate.push_back(Parent); |
38 | 7 | if (auto *ParenE = dyn_cast<ParenExpr>(Parent)) { |
39 | 1 | Parent = ParenE->getSubExpr(); |
40 | 1 | continue; |
41 | 1 | } |
42 | | |
43 | 6 | Expr *Child = nullptr; |
44 | 6 | CastExpr *ParentCast = dyn_cast<CastExpr>(Parent); |
45 | 6 | if (ParentCast) |
46 | 6 | Child = ParentCast->getSubExpr(); |
47 | 0 | else |
48 | 0 | return; |
49 | | |
50 | 6 | if (auto *CastE = dyn_cast<CastExpr>(Child)) |
51 | 6 | if (CastE->getCastKind() == CK_LValueToRValue) { |
52 | 6 | ExprUnderCast = CastE->getSubExpr(); |
53 | | // LValueToRValue cast inside GCCAsmStmt requires an explicit cast. |
54 | 6 | ParentCast->setSubExpr(ExprUnderCast); |
55 | 6 | break; |
56 | 6 | } |
57 | 0 | Parent = Child; |
58 | 0 | } |
59 | | |
60 | | // Update parent expressions to have same ValueType as the underlying. |
61 | 6 | assert(ExprUnderCast && |
62 | 6 | "Should be reachable only if LValueToRValue cast was found!"); |
63 | 0 | auto ValueKind = ExprUnderCast->getValueKind(); |
64 | 6 | for (Expr *E : ParentsToUpdate) |
65 | 7 | E->setValueKind(ValueKind); |
66 | 6 | } |
67 | | |
68 | | /// Emit a warning about usage of "noop"-like casts for lvalues (GNU extension) |
69 | | /// and fix the argument with removing LValueToRValue cast from the expression. |
70 | | static void emitAndFixInvalidAsmCastLValue(const Expr *LVal, Expr *BadArgument, |
71 | 6 | Sema &S) { |
72 | 6 | if (!S.getLangOpts().HeinousExtensions) { |
73 | 2 | S.Diag(LVal->getBeginLoc(), diag::err_invalid_asm_cast_lvalue) |
74 | 2 | << BadArgument->getSourceRange(); |
75 | 4 | } else { |
76 | 4 | S.Diag(LVal->getBeginLoc(), diag::warn_invalid_asm_cast_lvalue) |
77 | 4 | << BadArgument->getSourceRange(); |
78 | 4 | } |
79 | 6 | removeLValueToRValueCast(BadArgument); |
80 | 6 | } |
81 | | |
82 | | /// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently |
83 | | /// ignore "noop" casts in places where an lvalue is required by an inline asm. |
84 | | /// We emulate this behavior when -fheinous-gnu-extensions is specified, but |
85 | | /// provide a strong guidance to not use it. |
86 | | /// |
87 | | /// This method checks to see if the argument is an acceptable l-value and |
88 | | /// returns false if it is a case we can handle. |
89 | 95 | static bool CheckAsmLValue(Expr *E, Sema &S) { |
90 | | // Type dependent expressions will be checked during instantiation. |
91 | 95 | if (E->isTypeDependent()) |
92 | 0 | return false; |
93 | | |
94 | 95 | if (E->isLValue()) |
95 | 91 | return false; // Cool, this is an lvalue. |
96 | | |
97 | | // Okay, this is not an lvalue, but perhaps it is the result of a cast that we |
98 | | // are supposed to allow. |
99 | 4 | const Expr *E2 = E->IgnoreParenNoopCasts(S.Context); |
100 | 4 | if (E != E2 && E2->isLValue()2 ) { |
101 | 2 | emitAndFixInvalidAsmCastLValue(E2, E, S); |
102 | | // Accept, even if we emitted an error diagnostic. |
103 | 2 | return false; |
104 | 2 | } |
105 | | |
106 | | // None of the above, just randomly invalid non-lvalue. |
107 | 2 | return true; |
108 | 4 | } |
109 | | |
110 | | /// isOperandMentioned - Return true if the specified operand # is mentioned |
111 | | /// anywhere in the decomposed asm string. |
112 | | static bool |
113 | | isOperandMentioned(unsigned OpNo, |
114 | 43 | ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) { |
115 | 84 | for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p41 ) { |
116 | 48 | const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p]; |
117 | 48 | if (!Piece.isOperand()) |
118 | 30 | continue; |
119 | | |
120 | | // If this is a reference to the input and if the input was the smaller |
121 | | // one, then we have to reject this asm. |
122 | 18 | if (Piece.getOperandNo() == OpNo) |
123 | 7 | return true; |
124 | 18 | } |
125 | 36 | return false; |
126 | 43 | } |
127 | | |
128 | 16.6k | static bool CheckNakedParmReference(Expr *E, Sema &S) { |
129 | 16.6k | FunctionDecl *Func = dyn_cast<FunctionDecl>(S.CurContext); |
130 | 16.6k | if (!Func) |
131 | 0 | return false; |
132 | 16.6k | if (!Func->hasAttr<NakedAttr>()) |
133 | 16.6k | return false; |
134 | | |
135 | 13 | SmallVector<Expr*, 4> WorkList; |
136 | 13 | WorkList.push_back(E); |
137 | 23 | while (WorkList.size()) { |
138 | 15 | Expr *E = WorkList.pop_back_val(); |
139 | 15 | if (isa<CXXThisExpr>(E)) { |
140 | 2 | S.Diag(E->getBeginLoc(), diag::err_asm_naked_this_ref); |
141 | 2 | S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute); |
142 | 2 | return true; |
143 | 2 | } |
144 | 13 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
145 | 11 | if (isa<ParmVarDecl>(DRE->getDecl())) { |
146 | 3 | S.Diag(DRE->getBeginLoc(), diag::err_asm_naked_parm_ref); |
147 | 3 | S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute); |
148 | 3 | return true; |
149 | 3 | } |
150 | 11 | } |
151 | 10 | for (Stmt *Child : E->children()) { |
152 | 2 | if (Expr *E = dyn_cast_or_null<Expr>(Child)) |
153 | 2 | WorkList.push_back(E); |
154 | 2 | } |
155 | 10 | } |
156 | 8 | return false; |
157 | 13 | } |
158 | | |
159 | | /// Returns true if given expression is not compatible with inline |
160 | | /// assembly's memory constraint; false otherwise. |
161 | | static bool checkExprMemoryConstraintCompat(Sema &S, Expr *E, |
162 | | TargetInfo::ConstraintInfo &Info, |
163 | 919 | bool is_input_expr) { |
164 | 919 | enum { |
165 | 919 | ExprBitfield = 0, |
166 | 919 | ExprVectorElt, |
167 | 919 | ExprGlobalRegVar, |
168 | 919 | ExprSafeType |
169 | 919 | } EType = ExprSafeType; |
170 | | |
171 | | // Bitfields, vector elements and global register variables are not |
172 | | // compatible. |
173 | 919 | if (E->refersToBitField()) |
174 | 2 | EType = ExprBitfield; |
175 | 917 | else if (E->refersToVectorElement()) |
176 | 2 | EType = ExprVectorElt; |
177 | 915 | else if (E->refersToGlobalRegisterVar()) |
178 | 2 | EType = ExprGlobalRegVar; |
179 | | |
180 | 919 | if (EType != ExprSafeType) { |
181 | 6 | S.Diag(E->getBeginLoc(), diag::err_asm_non_addr_value_in_memory_constraint) |
182 | 6 | << EType << is_input_expr << Info.getConstraintStr() |
183 | 6 | << E->getSourceRange(); |
184 | 6 | return true; |
185 | 6 | } |
186 | | |
187 | 913 | return false; |
188 | 919 | } |
189 | | |
190 | | // Extracting the register name from the Expression value, |
191 | | // if there is no register name to extract, returns "" |
192 | | static StringRef extractRegisterName(const Expr *Expression, |
193 | 16.0k | const TargetInfo &Target) { |
194 | 16.0k | Expression = Expression->IgnoreImpCasts(); |
195 | 16.0k | if (const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(Expression)) { |
196 | | // Handle cases where the expression is a variable |
197 | 7.38k | const VarDecl *Variable = dyn_cast<VarDecl>(AsmDeclRef->getDecl()); |
198 | 7.38k | if (Variable && Variable->getStorageClass() == SC_Register7.37k ) { |
199 | 662 | if (AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>()) |
200 | 87 | if (Target.isValidGCCRegisterName(Attr->getLabel())) |
201 | 87 | return Target.getNormalizedGCCRegisterName(Attr->getLabel(), true); |
202 | 662 | } |
203 | 7.38k | } |
204 | 15.9k | return ""; |
205 | 16.0k | } |
206 | | |
207 | | // Checks if there is a conflict between the input and output lists with the |
208 | | // clobbers list. If there's a conflict, returns the location of the |
209 | | // conflicted clobber, else returns nullptr |
210 | | static SourceLocation |
211 | | getClobberConflictLocation(MultiExprArg Exprs, StringLiteral **Constraints, |
212 | | StringLiteral **Clobbers, int NumClobbers, |
213 | | unsigned NumLabels, |
214 | 5.06k | const TargetInfo &Target, ASTContext &Cont) { |
215 | 5.06k | llvm::StringSet<> InOutVars; |
216 | | // Collect all the input and output registers from the extended asm |
217 | | // statement in order to check for conflicts with the clobber list |
218 | 21.0k | for (unsigned int i = 0; i < Exprs.size() - NumLabels; ++i16.0k ) { |
219 | 16.0k | StringRef Constraint = Constraints[i]->getString(); |
220 | 16.0k | StringRef InOutReg = Target.getConstraintRegister( |
221 | 16.0k | Constraint, extractRegisterName(Exprs[i], Target)); |
222 | 16.0k | if (InOutReg != "") |
223 | 10.3k | InOutVars.insert(InOutReg); |
224 | 16.0k | } |
225 | | // Check for each item in the clobber list if it conflicts with the input |
226 | | // or output |
227 | 8.43k | for (int i = 0; i < NumClobbers; ++i3.37k ) { |
228 | 3.39k | StringRef Clobber = Clobbers[i]->getString(); |
229 | | // We only check registers, therefore we don't check cc and memory |
230 | | // clobbers |
231 | 3.39k | if (Clobber == "cc" || Clobber == "memory"2.18k || Clobber == "unwind"1.24k ) |
232 | 2.15k | continue; |
233 | 1.24k | Clobber = Target.getNormalizedGCCRegisterName(Clobber, true); |
234 | | // Go over the output's registers we collected |
235 | 1.24k | if (InOutVars.count(Clobber)) |
236 | 20 | return Clobbers[i]->getBeginLoc(); |
237 | 1.24k | } |
238 | 5.04k | return SourceLocation(); |
239 | 5.06k | } |
240 | | |
241 | | StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, |
242 | | bool IsVolatile, unsigned NumOutputs, |
243 | | unsigned NumInputs, IdentifierInfo **Names, |
244 | | MultiExprArg constraints, MultiExprArg Exprs, |
245 | | Expr *asmString, MultiExprArg clobbers, |
246 | | unsigned NumLabels, |
247 | 5.36k | SourceLocation RParenLoc) { |
248 | 5.36k | unsigned NumClobbers = clobbers.size(); |
249 | 5.36k | StringLiteral **Constraints = |
250 | 5.36k | reinterpret_cast<StringLiteral**>(constraints.data()); |
251 | 5.36k | StringLiteral *AsmString = cast<StringLiteral>(asmString); |
252 | 5.36k | StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data()); |
253 | | |
254 | 5.36k | SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos; |
255 | | |
256 | | // The parser verifies that there is a string literal here. |
257 | 5.36k | assert(AsmString->isOrdinary()); |
258 | | |
259 | 0 | FunctionDecl *FD = dyn_cast<FunctionDecl>(getCurLexicalContext()); |
260 | 5.36k | llvm::StringMap<bool> FeatureMap; |
261 | 5.36k | Context.getFunctionFeatureMap(FeatureMap, FD); |
262 | | |
263 | 12.7k | for (unsigned i = 0; i != NumOutputs; i++7.35k ) { |
264 | 7.45k | StringLiteral *Literal = Constraints[i]; |
265 | 7.45k | assert(Literal->isOrdinary()); |
266 | | |
267 | 0 | StringRef OutputName; |
268 | 7.45k | if (Names[i]) |
269 | 267 | OutputName = Names[i]->getName(); |
270 | | |
271 | 7.45k | TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName); |
272 | 7.45k | if (!Context.getTargetInfo().validateOutputConstraint(Info)) { |
273 | 60 | targetDiag(Literal->getBeginLoc(), |
274 | 60 | diag::err_asm_invalid_output_constraint) |
275 | 60 | << Info.getConstraintStr(); |
276 | 60 | return new (Context) |
277 | 60 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
278 | 60 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
279 | 60 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
280 | 60 | } |
281 | | |
282 | 7.39k | ExprResult ER = CheckPlaceholderExpr(Exprs[i]); |
283 | 7.39k | if (ER.isInvalid()) |
284 | 0 | return StmtError(); |
285 | 7.39k | Exprs[i] = ER.get(); |
286 | | |
287 | | // Check that the output exprs are valid lvalues. |
288 | 7.39k | Expr *OutputExpr = Exprs[i]; |
289 | | |
290 | | // Referring to parameters is not allowed in naked functions. |
291 | 7.39k | if (CheckNakedParmReference(OutputExpr, *this)) |
292 | 1 | return StmtError(); |
293 | | |
294 | | // Check that the output expression is compatible with memory constraint. |
295 | 7.39k | if (Info.allowsMemory() && |
296 | 7.39k | checkExprMemoryConstraintCompat(*this, OutputExpr, Info, false)324 ) |
297 | 3 | return StmtError(); |
298 | | |
299 | | // Disallow bit-precise integer types, since the backends tend to have |
300 | | // difficulties with abnormal sizes. |
301 | 7.39k | if (OutputExpr->getType()->isBitIntType()) |
302 | 1 | return StmtError( |
303 | 1 | Diag(OutputExpr->getBeginLoc(), diag::err_asm_invalid_type) |
304 | 1 | << OutputExpr->getType() << 0 /*Input*/ |
305 | 1 | << OutputExpr->getSourceRange()); |
306 | | |
307 | 7.39k | OutputConstraintInfos.push_back(Info); |
308 | | |
309 | | // If this is dependent, just continue. |
310 | 7.39k | if (OutputExpr->isTypeDependent()) |
311 | 2 | continue; |
312 | | |
313 | 7.39k | Expr::isModifiableLvalueResult IsLV = |
314 | 7.39k | OutputExpr->isModifiableLvalue(Context, /*Loc=*/nullptr); |
315 | 7.39k | switch (IsLV) { |
316 | 7.37k | case Expr::MLV_Valid: |
317 | | // Cool, this is an lvalue. |
318 | 7.37k | break; |
319 | 3 | case Expr::MLV_ArrayType: |
320 | | // This is OK too. |
321 | 3 | break; |
322 | 4 | case Expr::MLV_LValueCast: { |
323 | 4 | const Expr *LVal = OutputExpr->IgnoreParenNoopCasts(Context); |
324 | 4 | emitAndFixInvalidAsmCastLValue(LVal, OutputExpr, *this); |
325 | | // Accept, even if we emitted an error diagnostic. |
326 | 4 | break; |
327 | 0 | } |
328 | 1 | case Expr::MLV_IncompleteType: |
329 | 1 | case Expr::MLV_IncompleteVoidType: |
330 | 1 | if (RequireCompleteType(OutputExpr->getBeginLoc(), Exprs[i]->getType(), |
331 | 1 | diag::err_dereference_incomplete_type)) |
332 | 1 | return StmtError(); |
333 | 1 | LLVM_FALLTHROUGH0 ;0 |
334 | 4 | default: |
335 | 4 | return StmtError(Diag(OutputExpr->getBeginLoc(), |
336 | 4 | diag::err_asm_invalid_lvalue_in_output) |
337 | 4 | << OutputExpr->getSourceRange()); |
338 | 7.39k | } |
339 | | |
340 | 7.38k | unsigned Size = Context.getTypeSize(OutputExpr->getType()); |
341 | 7.38k | if (!Context.getTargetInfo().validateOutputSize( |
342 | 7.38k | FeatureMap, Literal->getString(), Size)) { |
343 | 35 | targetDiag(OutputExpr->getBeginLoc(), diag::err_asm_invalid_output_size) |
344 | 35 | << Info.getConstraintStr(); |
345 | 35 | return new (Context) |
346 | 35 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
347 | 35 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
348 | 35 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
349 | 35 | } |
350 | 7.38k | } |
351 | | |
352 | 5.26k | SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos; |
353 | | |
354 | 14.1k | for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++8.88k ) { |
355 | 9.04k | StringLiteral *Literal = Constraints[i]; |
356 | 9.04k | assert(Literal->isOrdinary()); |
357 | | |
358 | 0 | StringRef InputName; |
359 | 9.04k | if (Names[i]) |
360 | 398 | InputName = Names[i]->getName(); |
361 | | |
362 | 9.04k | TargetInfo::ConstraintInfo Info(Literal->getString(), InputName); |
363 | 9.04k | if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos, |
364 | 9.04k | Info)) { |
365 | 41 | targetDiag(Literal->getBeginLoc(), diag::err_asm_invalid_input_constraint) |
366 | 41 | << Info.getConstraintStr(); |
367 | 41 | return new (Context) |
368 | 41 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
369 | 41 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
370 | 41 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
371 | 41 | } |
372 | | |
373 | 9.00k | ExprResult ER = CheckPlaceholderExpr(Exprs[i]); |
374 | 9.00k | if (ER.isInvalid()) |
375 | 0 | return StmtError(); |
376 | 9.00k | Exprs[i] = ER.get(); |
377 | | |
378 | 9.00k | Expr *InputExpr = Exprs[i]; |
379 | | |
380 | | // Referring to parameters is not allowed in naked functions. |
381 | 9.00k | if (CheckNakedParmReference(InputExpr, *this)) |
382 | 3 | return StmtError(); |
383 | | |
384 | | // Check that the input expression is compatible with memory constraint. |
385 | 9.00k | if (Info.allowsMemory() && |
386 | 9.00k | checkExprMemoryConstraintCompat(*this, InputExpr, Info, true)595 ) |
387 | 3 | return StmtError(); |
388 | | |
389 | | // Only allow void types for memory constraints. |
390 | 8.99k | if (Info.allowsMemory() && !Info.allowsRegister()592 ) { |
391 | 95 | if (CheckAsmLValue(InputExpr, *this)) |
392 | 2 | return StmtError(Diag(InputExpr->getBeginLoc(), |
393 | 2 | diag::err_asm_invalid_lvalue_in_input) |
394 | 2 | << Info.getConstraintStr() |
395 | 2 | << InputExpr->getSourceRange()); |
396 | 8.90k | } else { |
397 | 8.90k | ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]); |
398 | 8.90k | if (Result.isInvalid()) |
399 | 0 | return StmtError(); |
400 | | |
401 | 8.90k | InputExpr = Exprs[i] = Result.get(); |
402 | | |
403 | 8.90k | if (Info.requiresImmediateConstant() && !Info.allowsRegister()268 ) { |
404 | 238 | if (!InputExpr->isValueDependent()) { |
405 | 234 | Expr::EvalResult EVResult; |
406 | 234 | if (InputExpr->EvaluateAsRValue(EVResult, Context, true)) { |
407 | | // For compatibility with GCC, we also allow pointers that would be |
408 | | // integral constant expressions if they were cast to int. |
409 | 226 | llvm::APSInt IntResult; |
410 | 226 | if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(), |
411 | 226 | Context)) |
412 | 225 | if (!Info.isValidAsmImmediate(IntResult)) |
413 | 74 | return StmtError( |
414 | 74 | Diag(InputExpr->getBeginLoc(), |
415 | 74 | diag::err_invalid_asm_value_for_constraint) |
416 | 74 | << toString(IntResult, 10) << Info.getConstraintStr() |
417 | 74 | << InputExpr->getSourceRange()); |
418 | 226 | } |
419 | 234 | } |
420 | 238 | } |
421 | 8.90k | } |
422 | | |
423 | 8.92k | if (Info.allowsRegister()) { |
424 | 8.44k | if (InputExpr->getType()->isVoidType()) { |
425 | 3 | return StmtError( |
426 | 3 | Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_type_in_input) |
427 | 3 | << InputExpr->getType() << Info.getConstraintStr() |
428 | 3 | << InputExpr->getSourceRange()); |
429 | 3 | } |
430 | 8.44k | } |
431 | | |
432 | 8.91k | if (InputExpr->getType()->isBitIntType()) |
433 | 1 | return StmtError( |
434 | 1 | Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_type) |
435 | 1 | << InputExpr->getType() << 1 /*Output*/ |
436 | 1 | << InputExpr->getSourceRange()); |
437 | | |
438 | 8.91k | InputConstraintInfos.push_back(Info); |
439 | | |
440 | 8.91k | const Type *Ty = Exprs[i]->getType().getTypePtr(); |
441 | 8.91k | if (Ty->isDependentType()) |
442 | 3 | continue; |
443 | | |
444 | 8.91k | if (!Ty->isVoidType() || !Info.allowsMemory()1 ) |
445 | 8.91k | if (RequireCompleteType(InputExpr->getBeginLoc(), Exprs[i]->getType(), |
446 | 8.91k | diag::err_dereference_incomplete_type)) |
447 | 1 | return StmtError(); |
448 | | |
449 | 8.91k | unsigned Size = Context.getTypeSize(Ty); |
450 | 8.91k | if (!Context.getTargetInfo().validateInputSize(FeatureMap, |
451 | 8.91k | Literal->getString(), Size)) |
452 | 35 | return targetDiag(InputExpr->getBeginLoc(), |
453 | 35 | diag::err_asm_invalid_input_size) |
454 | 35 | << Info.getConstraintStr(); |
455 | 8.91k | } |
456 | | |
457 | 5.09k | Optional<SourceLocation> UnwindClobberLoc; |
458 | | |
459 | | // Check that the clobbers are valid. |
460 | 8.50k | for (unsigned i = 0; i != NumClobbers; i++3.40k ) { |
461 | 3.40k | StringLiteral *Literal = Clobbers[i]; |
462 | 3.40k | assert(Literal->isOrdinary()); |
463 | | |
464 | 0 | StringRef Clobber = Literal->getString(); |
465 | | |
466 | 3.40k | if (!Context.getTargetInfo().isValidClobber(Clobber)) { |
467 | 4 | targetDiag(Literal->getBeginLoc(), diag::err_asm_unknown_register_name) |
468 | 4 | << Clobber; |
469 | 4 | return new (Context) |
470 | 4 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
471 | 4 | NumInputs, Names, Constraints, Exprs.data(), AsmString, |
472 | 4 | NumClobbers, Clobbers, NumLabels, RParenLoc); |
473 | 4 | } |
474 | | |
475 | 3.40k | if (Clobber == "unwind") { |
476 | 1 | UnwindClobberLoc = Literal->getBeginLoc(); |
477 | 1 | } |
478 | 3.40k | } |
479 | | |
480 | | // Using unwind clobber and asm-goto together is not supported right now. |
481 | 5.09k | if (UnwindClobberLoc && NumLabels > 01 ) { |
482 | 0 | targetDiag(*UnwindClobberLoc, diag::err_asm_unwind_and_goto); |
483 | 0 | return new (Context) |
484 | 0 | GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, NumInputs, |
485 | 0 | Names, Constraints, Exprs.data(), AsmString, NumClobbers, |
486 | 0 | Clobbers, NumLabels, RParenLoc); |
487 | 0 | } |
488 | | |
489 | 5.09k | GCCAsmStmt *NS = |
490 | 5.09k | new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs, |
491 | 5.09k | NumInputs, Names, Constraints, Exprs.data(), |
492 | 5.09k | AsmString, NumClobbers, Clobbers, NumLabels, |
493 | 5.09k | RParenLoc); |
494 | | // Validate the asm string, ensuring it makes sense given the operands we |
495 | | // have. |
496 | 5.09k | SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces; |
497 | 5.09k | unsigned DiagOffs; |
498 | 5.09k | if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) { |
499 | 24 | targetDiag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID) |
500 | 24 | << AsmString->getSourceRange(); |
501 | 24 | return NS; |
502 | 24 | } |
503 | | |
504 | | // Validate constraints and modifiers. |
505 | 20.6k | for (unsigned i = 0, e = Pieces.size(); 5.06k i != e; ++i15.5k ) { |
506 | 15.5k | GCCAsmStmt::AsmStringPiece &Piece = Pieces[i]; |
507 | 15.5k | if (!Piece.isOperand()) continue9.52k ; |
508 | | |
509 | | // Look for the correct constraint index. |
510 | 6.01k | unsigned ConstraintIdx = Piece.getOperandNo(); |
511 | 6.01k | unsigned NumOperands = NS->getNumOutputs() + NS->getNumInputs(); |
512 | | // Labels are the last in the Exprs list. |
513 | 6.01k | if (NS->isAsmGoto() && ConstraintIdx >= NumOperands155 ) |
514 | 58 | continue; |
515 | | // Look for the (ConstraintIdx - NumOperands + 1)th constraint with |
516 | | // modifier '+'. |
517 | 5.95k | if (ConstraintIdx >= NumOperands) { |
518 | 5 | unsigned I = 0, E = NS->getNumOutputs(); |
519 | | |
520 | 5 | for (unsigned Cnt = ConstraintIdx - NumOperands; I != E; ++I0 ) |
521 | 5 | if (OutputConstraintInfos[I].isReadWrite() && Cnt-- == 0) { |
522 | 5 | ConstraintIdx = I; |
523 | 5 | break; |
524 | 5 | } |
525 | | |
526 | 5 | assert(I != E && "Invalid operand number should have been caught in " |
527 | 5 | " AnalyzeAsmString"); |
528 | 5 | } |
529 | | |
530 | | // Now that we have the right indexes go ahead and check. |
531 | 0 | StringLiteral *Literal = Constraints[ConstraintIdx]; |
532 | 5.95k | const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr(); |
533 | 5.95k | if (Ty->isDependentType() || Ty->isIncompleteType()5.95k ) |
534 | 1 | continue; |
535 | | |
536 | 5.95k | unsigned Size = Context.getTypeSize(Ty); |
537 | 5.95k | std::string SuggestedModifier; |
538 | 5.95k | if (!Context.getTargetInfo().validateConstraintModifier( |
539 | 5.95k | Literal->getString(), Piece.getModifier(), Size, |
540 | 5.95k | SuggestedModifier)) { |
541 | 29 | targetDiag(Exprs[ConstraintIdx]->getBeginLoc(), |
542 | 29 | diag::warn_asm_mismatched_size_modifier); |
543 | | |
544 | 29 | if (!SuggestedModifier.empty()) { |
545 | 21 | auto B = targetDiag(Piece.getRange().getBegin(), |
546 | 21 | diag::note_asm_missing_constraint_modifier) |
547 | 21 | << SuggestedModifier; |
548 | 21 | SuggestedModifier = "%" + SuggestedModifier + Piece.getString(); |
549 | 21 | B << FixItHint::CreateReplacement(Piece.getRange(), SuggestedModifier); |
550 | 21 | } |
551 | 29 | } |
552 | 5.95k | } |
553 | | |
554 | | // Validate tied input operands for type mismatches. |
555 | 5.06k | unsigned NumAlternatives = ~0U; |
556 | 12.3k | for (unsigned i = 0, e = OutputConstraintInfos.size(); i != e; ++i7.23k ) { |
557 | 7.23k | TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i]; |
558 | 7.23k | StringRef ConstraintStr = Info.getConstraintStr(); |
559 | 7.23k | unsigned AltCount = ConstraintStr.count(',') + 1; |
560 | 7.23k | if (NumAlternatives == ~0U) { |
561 | 3.16k | NumAlternatives = AltCount; |
562 | 4.07k | } else if (NumAlternatives != AltCount) { |
563 | 0 | targetDiag(NS->getOutputExpr(i)->getBeginLoc(), |
564 | 0 | diag::err_asm_unexpected_constraint_alternatives) |
565 | 0 | << NumAlternatives << AltCount; |
566 | 0 | return NS; |
567 | 0 | } |
568 | 7.23k | } |
569 | 5.06k | SmallVector<size_t, 4> InputMatchedToOutput(OutputConstraintInfos.size(), |
570 | 5.06k | ~0U); |
571 | 13.8k | for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i8.80k ) { |
572 | 8.81k | TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i]; |
573 | 8.81k | StringRef ConstraintStr = Info.getConstraintStr(); |
574 | 8.81k | unsigned AltCount = ConstraintStr.count(',') + 1; |
575 | 8.81k | if (NumAlternatives == ~0U) { |
576 | 585 | NumAlternatives = AltCount; |
577 | 8.22k | } else if (NumAlternatives != AltCount) { |
578 | 1 | targetDiag(NS->getInputExpr(i)->getBeginLoc(), |
579 | 1 | diag::err_asm_unexpected_constraint_alternatives) |
580 | 1 | << NumAlternatives << AltCount; |
581 | 1 | return NS; |
582 | 1 | } |
583 | | |
584 | | // If this is a tied constraint, verify that the output and input have |
585 | | // either exactly the same type, or that they are int/ptr operands with the |
586 | | // same size (int/long, int*/long, are ok etc). |
587 | 8.81k | if (!Info.hasTiedOperand()) continue8.66k ; |
588 | | |
589 | 146 | unsigned TiedTo = Info.getTiedOperand(); |
590 | 146 | unsigned InputOpNo = i+NumOutputs; |
591 | 146 | Expr *OutputExpr = Exprs[TiedTo]; |
592 | 146 | Expr *InputExpr = Exprs[InputOpNo]; |
593 | | |
594 | | // Make sure no more than one input constraint matches each output. |
595 | 146 | assert(TiedTo < InputMatchedToOutput.size() && "TiedTo value out of range"); |
596 | 146 | if (InputMatchedToOutput[TiedTo] != ~0U) { |
597 | 1 | targetDiag(NS->getInputExpr(i)->getBeginLoc(), |
598 | 1 | diag::err_asm_input_duplicate_match) |
599 | 1 | << TiedTo; |
600 | 1 | targetDiag(NS->getInputExpr(InputMatchedToOutput[TiedTo])->getBeginLoc(), |
601 | 1 | diag::note_asm_input_duplicate_first) |
602 | 1 | << TiedTo; |
603 | 1 | return NS; |
604 | 1 | } |
605 | 145 | InputMatchedToOutput[TiedTo] = i; |
606 | | |
607 | 145 | if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent()144 ) |
608 | 1 | continue; |
609 | | |
610 | 144 | QualType InTy = InputExpr->getType(); |
611 | 144 | QualType OutTy = OutputExpr->getType(); |
612 | 144 | if (Context.hasSameType(InTy, OutTy)) |
613 | 122 | continue; // All types can be tied to themselves. |
614 | | |
615 | | // Decide if the input and output are in the same domain (integer/ptr or |
616 | | // floating point. |
617 | 22 | enum AsmDomain { |
618 | 22 | AD_Int, AD_FP, AD_Other |
619 | 22 | } InputDomain, OutputDomain; |
620 | | |
621 | 22 | if (InTy->isIntegerType() || InTy->isPointerType()9 ) |
622 | 16 | InputDomain = AD_Int; |
623 | 6 | else if (InTy->isRealFloatingType()) |
624 | 3 | InputDomain = AD_FP; |
625 | 3 | else |
626 | 3 | InputDomain = AD_Other; |
627 | | |
628 | 22 | if (OutTy->isIntegerType() || OutTy->isPointerType()11 ) |
629 | 12 | OutputDomain = AD_Int; |
630 | 10 | else if (OutTy->isRealFloatingType()) |
631 | 3 | OutputDomain = AD_FP; |
632 | 7 | else |
633 | 7 | OutputDomain = AD_Other; |
634 | | |
635 | | // They are ok if they are the same size and in the same domain. This |
636 | | // allows tying things like: |
637 | | // void* to int* |
638 | | // void* to int if they are the same size. |
639 | | // double to long double if they are the same size. |
640 | | // |
641 | 22 | uint64_t OutSize = Context.getTypeSize(OutTy); |
642 | 22 | uint64_t InSize = Context.getTypeSize(InTy); |
643 | 22 | if (OutSize == InSize && InputDomain == OutputDomain4 && |
644 | 22 | InputDomain != AD_Other2 ) |
645 | 2 | continue; |
646 | | |
647 | | // If the smaller input/output operand is not mentioned in the asm string, |
648 | | // then we can promote the smaller one to a larger input and the asm string |
649 | | // won't notice. |
650 | 20 | bool SmallerValueMentioned = false; |
651 | | |
652 | | // If this is a reference to the input and if the input was the smaller |
653 | | // one, then we have to reject this asm. |
654 | 20 | if (isOperandMentioned(InputOpNo, Pieces)) { |
655 | | // This is a use in the asm string of the smaller operand. Since we |
656 | | // codegen this by promoting to a wider value, the asm will get printed |
657 | | // "wrong". |
658 | 2 | SmallerValueMentioned |= InSize < OutSize; |
659 | 2 | } |
660 | 20 | if (isOperandMentioned(TiedTo, Pieces)) { |
661 | | // If this is a reference to the output, and if the output is the larger |
662 | | // value, then it's ok because we'll promote the input to the larger type. |
663 | 4 | SmallerValueMentioned |= OutSize < InSize; |
664 | 4 | } |
665 | | |
666 | | // If the smaller value wasn't mentioned in the asm string, and if the |
667 | | // output was a register, just extend the shorter one to the size of the |
668 | | // larger one. |
669 | 20 | if (!SmallerValueMentioned && InputDomain != AD_Other16 && |
670 | 20 | OutputConstraintInfos[TiedTo].allowsRegister()13 ) { |
671 | | // FIXME: GCC supports the OutSize to be 128 at maximum. Currently codegen |
672 | | // crash when the size larger than the register size. So we limit it here. |
673 | 13 | if (OutTy->isStructureType() && |
674 | 13 | Context.getIntTypeForBitwidth(OutSize, /*Signed*/ false).isNull()5 ) { |
675 | 1 | targetDiag(OutputExpr->getExprLoc(), diag::err_store_value_to_reg); |
676 | 1 | return NS; |
677 | 1 | } |
678 | | |
679 | 12 | continue; |
680 | 13 | } |
681 | | |
682 | | // Either both of the operands were mentioned or the smaller one was |
683 | | // mentioned. One more special case that we'll allow: if the tied input is |
684 | | // integer, unmentioned, and is a constant, then we'll allow truncating it |
685 | | // down to the size of the destination. |
686 | 7 | if (InputDomain == AD_Int && OutputDomain == AD_Int3 && |
687 | 7 | !isOperandMentioned(InputOpNo, Pieces)3 && |
688 | 7 | InputExpr->isEvaluatable(Context)2 ) { |
689 | 2 | CastKind castKind = |
690 | 2 | (OutTy->isBooleanType() ? CK_IntegralToBoolean1 : CK_IntegralCast1 ); |
691 | 2 | InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).get(); |
692 | 2 | Exprs[InputOpNo] = InputExpr; |
693 | 2 | NS->setInputExpr(i, InputExpr); |
694 | 2 | continue; |
695 | 2 | } |
696 | | |
697 | 5 | targetDiag(InputExpr->getBeginLoc(), diag::err_asm_tying_incompatible_types) |
698 | 5 | << InTy << OutTy << OutputExpr->getSourceRange() |
699 | 5 | << InputExpr->getSourceRange(); |
700 | 5 | return NS; |
701 | 7 | } |
702 | | |
703 | | // Check for conflicts between clobber list and input or output lists |
704 | 5.06k | SourceLocation ConstraintLoc = |
705 | 5.06k | getClobberConflictLocation(Exprs, Constraints, Clobbers, NumClobbers, |
706 | 5.06k | NumLabels, |
707 | 5.06k | Context.getTargetInfo(), Context); |
708 | 5.06k | if (ConstraintLoc.isValid()) |
709 | 20 | targetDiag(ConstraintLoc, diag::error_inoutput_conflict_with_clobber); |
710 | | |
711 | | // Check for duplicate asm operand name between input, output and label lists. |
712 | 5.06k | typedef std::pair<StringRef , Expr *> NamedOperand; |
713 | 5.06k | SmallVector<NamedOperand, 4> NamedOperandList; |
714 | 21.2k | for (unsigned i = 0, e = NumOutputs + NumInputs + NumLabels; i != e; ++i16.2k ) |
715 | 16.2k | if (Names[i]) |
716 | 735 | NamedOperandList.emplace_back( |
717 | 735 | std::make_pair(Names[i]->getName(), Exprs[i])); |
718 | | // Sort NamedOperandList. |
719 | 5.06k | llvm::stable_sort(NamedOperandList, llvm::less_first()); |
720 | | // Find adjacent duplicate operand. |
721 | 5.06k | SmallVector<NamedOperand, 4>::iterator Found = |
722 | 5.06k | std::adjacent_find(begin(NamedOperandList), end(NamedOperandList), |
723 | 5.06k | [](const NamedOperand &LHS, const NamedOperand &RHS) { |
724 | 444 | return LHS.first == RHS.first; |
725 | 444 | }); |
726 | 5.06k | if (Found != NamedOperandList.end()) { |
727 | 4 | Diag((Found + 1)->second->getBeginLoc(), |
728 | 4 | diag::error_duplicate_asm_operand_name) |
729 | 4 | << (Found + 1)->first; |
730 | 4 | Diag(Found->second->getBeginLoc(), diag::note_duplicate_asm_operand_name) |
731 | 4 | << Found->first; |
732 | 4 | return StmtError(); |
733 | 4 | } |
734 | 5.05k | if (NS->isAsmGoto()) |
735 | 116 | setFunctionHasBranchIntoScope(); |
736 | | |
737 | 5.05k | CleanupVarDeclMarking(); |
738 | 5.05k | DiscardCleanupsInEvaluationContext(); |
739 | 5.05k | return NS; |
740 | 5.06k | } |
741 | | |
742 | | void Sema::FillInlineAsmIdentifierInfo(Expr *Res, |
743 | 210 | llvm::InlineAsmIdentifierInfo &Info) { |
744 | 210 | QualType T = Res->getType(); |
745 | 210 | Expr::EvalResult Eval; |
746 | 210 | if (T->isFunctionType() || T->isDependentType()199 ) |
747 | 13 | return Info.setLabel(Res); |
748 | 197 | if (Res->isPRValue()) { |
749 | 22 | bool IsEnum = isa<clang::EnumType>(T); |
750 | 22 | if (DeclRefExpr *DRE = dyn_cast<clang::DeclRefExpr>(Res)) |
751 | 19 | if (DRE->getDecl()->getKind() == Decl::EnumConstant) |
752 | 19 | IsEnum = true; |
753 | 22 | if (IsEnum && Res->EvaluateAsRValue(Eval, Context)19 ) |
754 | 19 | return Info.setEnum(Eval.Val.getInt().getSExtValue()); |
755 | | |
756 | 3 | return Info.setLabel(Res); |
757 | 22 | } |
758 | 175 | unsigned Size = Context.getTypeSizeInChars(T).getQuantity(); |
759 | 175 | unsigned Type = Size; |
760 | 175 | if (const auto *ATy = Context.getAsArrayType(T)) |
761 | 44 | Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity(); |
762 | 175 | bool IsGlobalLV = false; |
763 | 175 | if (Res->EvaluateAsLValue(Eval, Context)) |
764 | 38 | IsGlobalLV = Eval.isGlobalLValue(); |
765 | 175 | Info.setVar(Res, IsGlobalLV, Size, Type); |
766 | 175 | } |
767 | | |
768 | | ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, |
769 | | SourceLocation TemplateKWLoc, |
770 | | UnqualifiedId &Id, |
771 | 231 | bool IsUnevaluatedContext) { |
772 | | |
773 | 231 | if (IsUnevaluatedContext) |
774 | 33 | PushExpressionEvaluationContext( |
775 | 33 | ExpressionEvaluationContext::UnevaluatedAbstract, |
776 | 33 | ReuseLambdaContextDecl); |
777 | | |
778 | 231 | ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id, |
779 | 231 | /*trailing lparen*/ false, |
780 | 231 | /*is & operand*/ false, |
781 | 231 | /*CorrectionCandidateCallback=*/nullptr, |
782 | 231 | /*IsInlineAsmIdentifier=*/ true); |
783 | | |
784 | 231 | if (IsUnevaluatedContext) |
785 | 33 | PopExpressionEvaluationContext(); |
786 | | |
787 | 231 | if (!Result.isUsable()) return Result20 ; |
788 | | |
789 | 211 | Result = CheckPlaceholderExpr(Result.get()); |
790 | 211 | if (!Result.isUsable()) return Result0 ; |
791 | | |
792 | | // Referring to parameters is not allowed in naked functions. |
793 | 211 | if (CheckNakedParmReference(Result.get(), *this)) |
794 | 1 | return ExprError(); |
795 | | |
796 | 210 | QualType T = Result.get()->getType(); |
797 | | |
798 | 210 | if (T->isDependentType()) { |
799 | 2 | return Result; |
800 | 2 | } |
801 | | |
802 | | // Any sort of function type is fine. |
803 | 208 | if (T->isFunctionType()) { |
804 | 11 | return Result; |
805 | 11 | } |
806 | | |
807 | | // Otherwise, it needs to be a complete type. |
808 | 197 | if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) { |
809 | 0 | return ExprError(); |
810 | 0 | } |
811 | | |
812 | 197 | return Result; |
813 | 197 | } |
814 | | |
815 | | bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member, |
816 | 25 | unsigned &Offset, SourceLocation AsmLoc) { |
817 | 25 | Offset = 0; |
818 | 25 | SmallVector<StringRef, 2> Members; |
819 | 25 | Member.split(Members, "."); |
820 | | |
821 | 25 | NamedDecl *FoundDecl = nullptr; |
822 | | |
823 | | // MS InlineAsm uses 'this' as a base |
824 | 25 | if (getLangOpts().CPlusPlus && Base.equals("this")7 ) { |
825 | 1 | if (const Type *PT = getCurrentThisType().getTypePtrOrNull()) |
826 | 1 | FoundDecl = PT->getPointeeType()->getAsTagDecl(); |
827 | 24 | } else { |
828 | 24 | LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(), |
829 | 24 | LookupOrdinaryName); |
830 | 24 | if (LookupName(BaseResult, getCurScope()) && BaseResult.isSingleResult()22 ) |
831 | 22 | FoundDecl = BaseResult.getFoundDecl(); |
832 | 24 | } |
833 | | |
834 | 25 | if (!FoundDecl) |
835 | 2 | return true; |
836 | | |
837 | 30 | for (StringRef NextMember : Members)23 { |
838 | 30 | const RecordType *RT = nullptr; |
839 | 30 | if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl)) |
840 | 2 | RT = VD->getType()->getAs<RecordType>(); |
841 | 28 | else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(FoundDecl)) { |
842 | 18 | MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false); |
843 | | // MS InlineAsm often uses struct pointer aliases as a base |
844 | 18 | QualType QT = TD->getUnderlyingType(); |
845 | 18 | if (const auto *PT = QT->getAs<PointerType>()) |
846 | 2 | QT = PT->getPointeeType(); |
847 | 18 | RT = QT->getAs<RecordType>(); |
848 | 18 | } else if (TypeDecl *10 TD10 = dyn_cast<TypeDecl>(FoundDecl)) |
849 | 3 | RT = TD->getTypeForDecl()->getAs<RecordType>(); |
850 | 7 | else if (FieldDecl *TD = dyn_cast<FieldDecl>(FoundDecl)) |
851 | 7 | RT = TD->getType()->getAs<RecordType>(); |
852 | 30 | if (!RT) |
853 | 0 | return true; |
854 | | |
855 | 30 | if (RequireCompleteType(AsmLoc, QualType(RT, 0), |
856 | 30 | diag::err_asm_incomplete_type)) |
857 | 0 | return true; |
858 | | |
859 | 30 | LookupResult FieldResult(*this, &Context.Idents.get(NextMember), |
860 | 30 | SourceLocation(), LookupMemberName); |
861 | | |
862 | 30 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
863 | 0 | return true; |
864 | | |
865 | 30 | if (!FieldResult.isSingleResult()) |
866 | 1 | return true; |
867 | 29 | FoundDecl = FieldResult.getFoundDecl(); |
868 | | |
869 | | // FIXME: Handle IndirectFieldDecl? |
870 | 29 | FieldDecl *FD = dyn_cast<FieldDecl>(FoundDecl); |
871 | 29 | if (!FD) |
872 | 0 | return true; |
873 | | |
874 | 29 | const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl()); |
875 | 29 | unsigned i = FD->getFieldIndex(); |
876 | 29 | CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i)); |
877 | 29 | Offset += (unsigned)Result.getQuantity(); |
878 | 29 | } |
879 | | |
880 | 22 | return false; |
881 | 23 | } |
882 | | |
883 | | ExprResult |
884 | | Sema::LookupInlineAsmVarDeclField(Expr *E, StringRef Member, |
885 | 42 | SourceLocation AsmLoc) { |
886 | | |
887 | 42 | QualType T = E->getType(); |
888 | 42 | if (T->isDependentType()) { |
889 | 2 | DeclarationNameInfo NameInfo; |
890 | 2 | NameInfo.setLoc(AsmLoc); |
891 | 2 | NameInfo.setName(&Context.Idents.get(Member)); |
892 | 2 | return CXXDependentScopeMemberExpr::Create( |
893 | 2 | Context, E, T, /*IsArrow=*/false, AsmLoc, NestedNameSpecifierLoc(), |
894 | 2 | SourceLocation(), |
895 | 2 | /*FirstQualifierFoundInScope=*/nullptr, NameInfo, /*TemplateArgs=*/nullptr); |
896 | 2 | } |
897 | | |
898 | 40 | const RecordType *RT = T->getAs<RecordType>(); |
899 | | // FIXME: Diagnose this as field access into a scalar type. |
900 | 40 | if (!RT) |
901 | 0 | return ExprResult(); |
902 | | |
903 | 40 | LookupResult FieldResult(*this, &Context.Idents.get(Member), AsmLoc, |
904 | 40 | LookupMemberName); |
905 | | |
906 | 40 | if (!LookupQualifiedName(FieldResult, RT->getDecl())) |
907 | 2 | return ExprResult(); |
908 | | |
909 | | // Only normal and indirect field results will work. |
910 | 38 | ValueDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl()); |
911 | 38 | if (!FD) |
912 | 3 | FD = dyn_cast<IndirectFieldDecl>(FieldResult.getFoundDecl()); |
913 | 38 | if (!FD) |
914 | 1 | return ExprResult(); |
915 | | |
916 | | // Make an Expr to thread through OpDecl. |
917 | 37 | ExprResult Result = BuildMemberReferenceExpr( |
918 | 37 | E, E->getType(), AsmLoc, /*IsArrow=*/false, CXXScopeSpec(), |
919 | 37 | SourceLocation(), nullptr, FieldResult, nullptr, nullptr); |
920 | | |
921 | 37 | return Result; |
922 | 38 | } |
923 | | |
924 | | StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc, |
925 | | ArrayRef<Token> AsmToks, |
926 | | StringRef AsmString, |
927 | | unsigned NumOutputs, unsigned NumInputs, |
928 | | ArrayRef<StringRef> Constraints, |
929 | | ArrayRef<StringRef> Clobbers, |
930 | | ArrayRef<Expr*> Exprs, |
931 | 229 | SourceLocation EndLoc) { |
932 | 229 | bool IsSimple = (NumOutputs != 0 || NumInputs != 0210 ); |
933 | 229 | setFunctionHasBranchProtectedScope(); |
934 | | |
935 | 382 | for (uint64_t I = 0; I < NumOutputs + NumInputs; ++I153 ) { |
936 | 155 | if (Exprs[I]->getType()->isBitIntType()) |
937 | 2 | return StmtError( |
938 | 2 | Diag(Exprs[I]->getBeginLoc(), diag::err_asm_invalid_type) |
939 | 2 | << Exprs[I]->getType() << (I < NumOutputs) |
940 | 2 | << Exprs[I]->getSourceRange()); |
941 | 155 | } |
942 | | |
943 | 227 | MSAsmStmt *NS = |
944 | 227 | new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple, |
945 | 227 | /*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs, |
946 | 227 | Constraints, Exprs, AsmString, |
947 | 227 | Clobbers, EndLoc); |
948 | 227 | return NS; |
949 | 229 | } |
950 | | |
951 | | LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName, |
952 | | SourceLocation Location, |
953 | 42 | bool AlwaysCreate) { |
954 | 42 | LabelDecl* Label = LookupOrCreateLabel(PP.getIdentifierInfo(ExternalLabelName), |
955 | 42 | Location); |
956 | | |
957 | 42 | if (Label->isMSAsmLabel()) { |
958 | | // If we have previously created this label implicitly, mark it as used. |
959 | 15 | Label->markUsed(Context); |
960 | 27 | } else { |
961 | | // Otherwise, insert it, but only resolve it if we have seen the label itself. |
962 | 27 | std::string InternalName; |
963 | 27 | llvm::raw_string_ostream OS(InternalName); |
964 | | // Create an internal name for the label. The name should not be a valid |
965 | | // mangled name, and should be unique. We use a dot to make the name an |
966 | | // invalid mangled name. We use LLVM's inline asm ${:uid} escape so that a |
967 | | // unique label is generated each time this blob is emitted, even after |
968 | | // inlining or LTO. |
969 | 27 | OS << "__MSASMLABEL_.${:uid}__"; |
970 | 210 | for (char C : ExternalLabelName) { |
971 | 210 | OS << C; |
972 | | // We escape '$' in asm strings by replacing it with "$$" |
973 | 210 | if (C == '$') |
974 | 1 | OS << '$'; |
975 | 210 | } |
976 | 27 | Label->setMSAsmLabel(OS.str()); |
977 | 27 | } |
978 | 42 | if (AlwaysCreate) { |
979 | | // The label might have been created implicitly from a previously encountered |
980 | | // goto statement. So, for both newly created and looked up labels, we mark |
981 | | // them as resolved. |
982 | 18 | Label->setMSAsmLabelResolved(); |
983 | 18 | } |
984 | | // Adjust their location for being able to generate accurate diagnostics. |
985 | 42 | Label->setLocation(Location); |
986 | | |
987 | 42 | return Label; |
988 | 42 | } |