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