2012-08-18 05:27:25 +08:00
|
|
|
//===--- SemaStmtAsm.cpp - Semantic Analysis for Asm Statements -----------===//
|
2012-08-18 05:19:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-08-18 05:19:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for inline asm statements.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-02-04 06:35:58 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2012-10-26 05:49:22 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2012-08-18 05:19:40 +08:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2014-09-22 10:21:54 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/Initialization.h"
|
|
|
|
#include "clang/Sema/Lookup.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
|
|
|
#include "clang/Sema/ScopeInfo.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
2012-08-18 05:19:40 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2016-12-26 20:23:42 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2014-06-08 13:11:37 +08:00
|
|
|
#include "llvm/MC/MCParser/MCAsmParser.h"
|
2012-08-18 05:19:40 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace sema;
|
|
|
|
|
2018-10-21 06:49:23 +08:00
|
|
|
/// Remove the upper-level LValueToRValue cast from an expression.
|
|
|
|
static void removeLValueToRValueCast(Expr *E) {
|
|
|
|
Expr *Parent = E;
|
|
|
|
Expr *ExprUnderCast = nullptr;
|
|
|
|
SmallVector<Expr *, 8> ParentsToUpdate;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
ParentsToUpdate.push_back(Parent);
|
|
|
|
if (auto *ParenE = dyn_cast<ParenExpr>(Parent)) {
|
|
|
|
Parent = ParenE->getSubExpr();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr *Child = nullptr;
|
|
|
|
CastExpr *ParentCast = dyn_cast<CastExpr>(Parent);
|
|
|
|
if (ParentCast)
|
|
|
|
Child = ParentCast->getSubExpr();
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto *CastE = dyn_cast<CastExpr>(Child))
|
|
|
|
if (CastE->getCastKind() == CK_LValueToRValue) {
|
|
|
|
ExprUnderCast = CastE->getSubExpr();
|
|
|
|
// LValueToRValue cast inside GCCAsmStmt requires an explicit cast.
|
|
|
|
ParentCast->setSubExpr(ExprUnderCast);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Parent = Child;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update parent expressions to have same ValueType as the underlying.
|
|
|
|
assert(ExprUnderCast &&
|
|
|
|
"Should be reachable only if LValueToRValue cast was found!");
|
|
|
|
auto ValueKind = ExprUnderCast->getValueKind();
|
|
|
|
for (Expr *E : ParentsToUpdate)
|
|
|
|
E->setValueKind(ValueKind);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a warning about usage of "noop"-like casts for lvalues (GNU extension)
|
|
|
|
/// and fix the argument with removing LValueToRValue cast from the expression.
|
|
|
|
static void emitAndFixInvalidAsmCastLValue(const Expr *LVal, Expr *BadArgument,
|
|
|
|
Sema &S) {
|
|
|
|
if (!S.getLangOpts().HeinousExtensions) {
|
|
|
|
S.Diag(LVal->getBeginLoc(), diag::err_invalid_asm_cast_lvalue)
|
|
|
|
<< BadArgument->getSourceRange();
|
|
|
|
} else {
|
|
|
|
S.Diag(LVal->getBeginLoc(), diag::warn_invalid_asm_cast_lvalue)
|
|
|
|
<< BadArgument->getSourceRange();
|
|
|
|
}
|
|
|
|
removeLValueToRValueCast(BadArgument);
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:19:40 +08:00
|
|
|
/// CheckAsmLValue - GNU C has an extremely ugly extension whereby they silently
|
|
|
|
/// ignore "noop" casts in places where an lvalue is required by an inline asm.
|
|
|
|
/// We emulate this behavior when -fheinous-gnu-extensions is specified, but
|
|
|
|
/// provide a strong guidance to not use it.
|
|
|
|
///
|
|
|
|
/// This method checks to see if the argument is an acceptable l-value and
|
|
|
|
/// returns false if it is a case we can handle.
|
2018-10-21 06:49:23 +08:00
|
|
|
static bool CheckAsmLValue(Expr *E, Sema &S) {
|
2012-08-18 05:19:40 +08:00
|
|
|
// Type dependent expressions will be checked during instantiation.
|
|
|
|
if (E->isTypeDependent())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (E->isLValue())
|
|
|
|
return false; // Cool, this is an lvalue.
|
|
|
|
|
|
|
|
// Okay, this is not an lvalue, but perhaps it is the result of a cast that we
|
|
|
|
// are supposed to allow.
|
|
|
|
const Expr *E2 = E->IgnoreParenNoopCasts(S.Context);
|
|
|
|
if (E != E2 && E2->isLValue()) {
|
2018-10-21 06:49:23 +08:00
|
|
|
emitAndFixInvalidAsmCastLValue(E2, E, S);
|
2012-08-18 05:19:40 +08:00
|
|
|
// Accept, even if we emitted an error diagnostic.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of the above, just randomly invalid non-lvalue.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isOperandMentioned - Return true if the specified operand # is mentioned
|
|
|
|
/// anywhere in the decomposed asm string.
|
2017-09-10 20:39:21 +08:00
|
|
|
static bool
|
|
|
|
isOperandMentioned(unsigned OpNo,
|
|
|
|
ArrayRef<GCCAsmStmt::AsmStringPiece> AsmStrPieces) {
|
2012-08-18 05:19:40 +08:00
|
|
|
for (unsigned p = 0, e = AsmStrPieces.size(); p != e; ++p) {
|
2012-08-25 08:11:56 +08:00
|
|
|
const GCCAsmStmt::AsmStringPiece &Piece = AsmStrPieces[p];
|
2017-09-10 20:39:21 +08:00
|
|
|
if (!Piece.isOperand())
|
|
|
|
continue;
|
2012-08-18 05:19:40 +08:00
|
|
|
|
|
|
|
// If this is a reference to the input and if the input was the smaller
|
|
|
|
// one, then we have to reject this asm.
|
|
|
|
if (Piece.getOperandNo() == OpNo)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-08 09:58:02 +08:00
|
|
|
static bool CheckNakedParmReference(Expr *E, Sema &S) {
|
|
|
|
FunctionDecl *Func = dyn_cast<FunctionDecl>(S.CurContext);
|
|
|
|
if (!Func)
|
|
|
|
return false;
|
|
|
|
if (!Func->hasAttr<NakedAttr>())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallVector<Expr*, 4> WorkList;
|
|
|
|
WorkList.push_back(E);
|
|
|
|
while (WorkList.size()) {
|
|
|
|
Expr *E = WorkList.pop_back_val();
|
2015-02-04 06:35:58 +08:00
|
|
|
if (isa<CXXThisExpr>(E)) {
|
2018-08-10 05:08:08 +08:00
|
|
|
S.Diag(E->getBeginLoc(), diag::err_asm_naked_this_ref);
|
2015-02-04 06:35:58 +08:00
|
|
|
S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute);
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-08 09:58:02 +08:00
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
|
|
|
|
if (isa<ParmVarDecl>(DRE->getDecl())) {
|
2018-08-10 05:08:08 +08:00
|
|
|
S.Diag(DRE->getBeginLoc(), diag::err_asm_naked_parm_ref);
|
2014-10-08 09:58:02 +08:00
|
|
|
S.Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Stmt *Child : E->children()) {
|
|
|
|
if (Expr *E = dyn_cast_or_null<Expr>(Child))
|
|
|
|
WorkList.push_back(E);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns true if given expression is not compatible with inline
|
2015-08-03 18:38:10 +08:00
|
|
|
/// assembly's memory constraint; false otherwise.
|
|
|
|
static bool checkExprMemoryConstraintCompat(Sema &S, Expr *E,
|
|
|
|
TargetInfo::ConstraintInfo &Info,
|
|
|
|
bool is_input_expr) {
|
|
|
|
enum {
|
|
|
|
ExprBitfield = 0,
|
|
|
|
ExprVectorElt,
|
|
|
|
ExprGlobalRegVar,
|
|
|
|
ExprSafeType
|
|
|
|
} EType = ExprSafeType;
|
|
|
|
|
|
|
|
// Bitfields, vector elements and global register variables are not
|
|
|
|
// compatible.
|
|
|
|
if (E->refersToBitField())
|
|
|
|
EType = ExprBitfield;
|
|
|
|
else if (E->refersToVectorElement())
|
|
|
|
EType = ExprVectorElt;
|
|
|
|
else if (E->refersToGlobalRegisterVar())
|
|
|
|
EType = ExprGlobalRegVar;
|
|
|
|
|
|
|
|
if (EType != ExprSafeType) {
|
2018-08-10 05:08:08 +08:00
|
|
|
S.Diag(E->getBeginLoc(), diag::err_asm_non_addr_value_in_memory_constraint)
|
2015-08-03 18:38:10 +08:00
|
|
|
<< EType << is_input_expr << Info.getConstraintStr()
|
|
|
|
<< E->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-26 20:23:42 +08:00
|
|
|
// Extracting the register name from the Expression value,
|
|
|
|
// if there is no register name to extract, returns ""
|
|
|
|
static StringRef extractRegisterName(const Expr *Expression,
|
|
|
|
const TargetInfo &Target) {
|
|
|
|
Expression = Expression->IgnoreImpCasts();
|
|
|
|
if (const DeclRefExpr *AsmDeclRef = dyn_cast<DeclRefExpr>(Expression)) {
|
|
|
|
// Handle cases where the expression is a variable
|
|
|
|
const VarDecl *Variable = dyn_cast<VarDecl>(AsmDeclRef->getDecl());
|
|
|
|
if (Variable && Variable->getStorageClass() == SC_Register) {
|
|
|
|
if (AsmLabelAttr *Attr = Variable->getAttr<AsmLabelAttr>())
|
|
|
|
if (Target.isValidGCCRegisterName(Attr->getLabel()))
|
|
|
|
return Target.getNormalizedGCCRegisterName(Attr->getLabel(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks if there is a conflict between the input and output lists with the
|
|
|
|
// clobbers list. If there's a conflict, returns the location of the
|
|
|
|
// conflicted clobber, else returns nullptr
|
|
|
|
static SourceLocation
|
|
|
|
getClobberConflictLocation(MultiExprArg Exprs, StringLiteral **Constraints,
|
|
|
|
StringLiteral **Clobbers, int NumClobbers,
|
2019-06-03 23:57:25 +08:00
|
|
|
unsigned NumLabels,
|
2016-12-26 20:23:42 +08:00
|
|
|
const TargetInfo &Target, ASTContext &Cont) {
|
|
|
|
llvm::StringSet<> InOutVars;
|
|
|
|
// Collect all the input and output registers from the extended asm
|
2016-12-26 21:16:40 +08:00
|
|
|
// statement in order to check for conflicts with the clobber list
|
2019-06-03 23:57:25 +08:00
|
|
|
for (unsigned int i = 0; i < Exprs.size() - NumLabels; ++i) {
|
2016-12-26 20:23:42 +08:00
|
|
|
StringRef Constraint = Constraints[i]->getString();
|
|
|
|
StringRef InOutReg = Target.getConstraintRegister(
|
|
|
|
Constraint, extractRegisterName(Exprs[i], Target));
|
|
|
|
if (InOutReg != "")
|
|
|
|
InOutVars.insert(InOutReg);
|
|
|
|
}
|
|
|
|
// Check for each item in the clobber list if it conflicts with the input
|
|
|
|
// or output
|
|
|
|
for (int i = 0; i < NumClobbers; ++i) {
|
|
|
|
StringRef Clobber = Clobbers[i]->getString();
|
|
|
|
// We only check registers, therefore we don't check cc and memory
|
|
|
|
// clobbers
|
|
|
|
if (Clobber == "cc" || Clobber == "memory")
|
|
|
|
continue;
|
|
|
|
Clobber = Target.getNormalizedGCCRegisterName(Clobber, true);
|
|
|
|
// Go over the output's registers we collected
|
|
|
|
if (InOutVars.count(Clobber))
|
2018-08-10 05:08:08 +08:00
|
|
|
return Clobbers[i]->getBeginLoc();
|
2016-12-26 20:23:42 +08:00
|
|
|
}
|
|
|
|
return SourceLocation();
|
|
|
|
}
|
|
|
|
|
2012-08-25 08:11:56 +08:00
|
|
|
StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
|
|
|
|
bool IsVolatile, unsigned NumOutputs,
|
|
|
|
unsigned NumInputs, IdentifierInfo **Names,
|
2013-05-10 09:14:26 +08:00
|
|
|
MultiExprArg constraints, MultiExprArg Exprs,
|
2012-08-25 08:11:56 +08:00
|
|
|
Expr *asmString, MultiExprArg clobbers,
|
2019-06-03 23:57:25 +08:00
|
|
|
unsigned NumLabels,
|
2012-08-25 08:11:56 +08:00
|
|
|
SourceLocation RParenLoc) {
|
2012-08-18 05:19:40 +08:00
|
|
|
unsigned NumClobbers = clobbers.size();
|
|
|
|
StringLiteral **Constraints =
|
2012-08-24 07:38:35 +08:00
|
|
|
reinterpret_cast<StringLiteral**>(constraints.data());
|
2012-08-18 05:19:40 +08:00
|
|
|
StringLiteral *AsmString = cast<StringLiteral>(asmString);
|
2012-08-24 07:38:35 +08:00
|
|
|
StringLiteral **Clobbers = reinterpret_cast<StringLiteral**>(clobbers.data());
|
2012-08-18 05:19:40 +08:00
|
|
|
|
|
|
|
SmallVector<TargetInfo::ConstraintInfo, 4> OutputConstraintInfos;
|
|
|
|
|
|
|
|
// The parser verifies that there is a string literal here.
|
2014-12-11 09:00:48 +08:00
|
|
|
assert(AsmString->isAscii());
|
2012-08-18 05:19:40 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumOutputs; i++) {
|
|
|
|
StringLiteral *Literal = Constraints[i];
|
2014-12-11 09:00:48 +08:00
|
|
|
assert(Literal->isAscii());
|
2012-08-18 05:19:40 +08:00
|
|
|
|
|
|
|
StringRef OutputName;
|
|
|
|
if (Names[i])
|
|
|
|
OutputName = Names[i]->getName();
|
|
|
|
|
|
|
|
TargetInfo::ConstraintInfo Info(Literal->getString(), OutputName);
|
2019-02-27 05:51:16 +08:00
|
|
|
if (!Context.getTargetInfo().validateOutputConstraint(Info)) {
|
|
|
|
targetDiag(Literal->getBeginLoc(),
|
|
|
|
diag::err_asm_invalid_output_constraint)
|
|
|
|
<< Info.getConstraintStr();
|
|
|
|
return new (Context)
|
|
|
|
GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
|
|
|
|
NumInputs, Names, Constraints, Exprs.data(), AsmString,
|
2019-06-03 23:57:25 +08:00
|
|
|
NumClobbers, Clobbers, NumLabels, RParenLoc);
|
2019-02-27 05:51:16 +08:00
|
|
|
}
|
2012-08-18 05:19:40 +08:00
|
|
|
|
2014-12-29 17:30:33 +08:00
|
|
|
ExprResult ER = CheckPlaceholderExpr(Exprs[i]);
|
|
|
|
if (ER.isInvalid())
|
|
|
|
return StmtError();
|
|
|
|
Exprs[i] = ER.get();
|
|
|
|
|
2012-08-18 05:19:40 +08:00
|
|
|
// Check that the output exprs are valid lvalues.
|
|
|
|
Expr *OutputExpr = Exprs[i];
|
2013-03-26 05:09:49 +08:00
|
|
|
|
2014-10-08 09:58:02 +08:00
|
|
|
// Referring to parameters is not allowed in naked functions.
|
|
|
|
if (CheckNakedParmReference(OutputExpr, *this))
|
|
|
|
return StmtError();
|
|
|
|
|
2015-08-03 18:38:10 +08:00
|
|
|
// Check that the output expression is compatible with memory constraint.
|
|
|
|
if (Info.allowsMemory() &&
|
|
|
|
checkExprMemoryConstraintCompat(*this, OutputExpr, Info, false))
|
|
|
|
return StmtError();
|
2015-06-05 21:40:59 +08:00
|
|
|
|
2012-08-18 05:19:40 +08:00
|
|
|
OutputConstraintInfos.push_back(Info);
|
2014-09-19 02:17:18 +08:00
|
|
|
|
2014-12-29 17:30:33 +08:00
|
|
|
// If this is dependent, just continue.
|
|
|
|
if (OutputExpr->isTypeDependent())
|
2014-09-19 02:17:18 +08:00
|
|
|
continue;
|
|
|
|
|
2014-12-29 17:30:33 +08:00
|
|
|
Expr::isModifiableLvalueResult IsLV =
|
|
|
|
OutputExpr->isModifiableLvalue(Context, /*Loc=*/nullptr);
|
|
|
|
switch (IsLV) {
|
|
|
|
case Expr::MLV_Valid:
|
|
|
|
// Cool, this is an lvalue.
|
|
|
|
break;
|
2014-12-29 18:29:53 +08:00
|
|
|
case Expr::MLV_ArrayType:
|
|
|
|
// This is OK too.
|
|
|
|
break;
|
2014-12-29 17:30:33 +08:00
|
|
|
case Expr::MLV_LValueCast: {
|
|
|
|
const Expr *LVal = OutputExpr->IgnoreParenNoopCasts(Context);
|
2018-10-21 06:49:23 +08:00
|
|
|
emitAndFixInvalidAsmCastLValue(LVal, OutputExpr, *this);
|
2014-12-29 17:30:33 +08:00
|
|
|
// Accept, even if we emitted an error diagnostic.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Expr::MLV_IncompleteType:
|
|
|
|
case Expr::MLV_IncompleteVoidType:
|
2018-08-10 05:08:08 +08:00
|
|
|
if (RequireCompleteType(OutputExpr->getBeginLoc(), Exprs[i]->getType(),
|
2014-12-29 17:30:33 +08:00
|
|
|
diag::err_dereference_incomplete_type))
|
|
|
|
return StmtError();
|
2017-06-03 14:35:06 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2014-12-29 17:30:33 +08:00
|
|
|
default:
|
2018-08-10 05:08:08 +08:00
|
|
|
return StmtError(Diag(OutputExpr->getBeginLoc(),
|
2014-12-29 17:30:33 +08:00
|
|
|
diag::err_asm_invalid_lvalue_in_output)
|
|
|
|
<< OutputExpr->getSourceRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Size = Context.getTypeSize(OutputExpr->getType());
|
2019-02-27 05:51:16 +08:00
|
|
|
if (!Context.getTargetInfo().validateOutputSize(Literal->getString(),
|
|
|
|
Size)) {
|
|
|
|
targetDiag(OutputExpr->getBeginLoc(), diag::err_asm_invalid_output_size)
|
|
|
|
<< Info.getConstraintStr();
|
|
|
|
return new (Context)
|
|
|
|
GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
|
|
|
|
NumInputs, Names, Constraints, Exprs.data(), AsmString,
|
2019-06-03 23:57:25 +08:00
|
|
|
NumClobbers, Clobbers, NumLabels, RParenLoc);
|
2019-02-27 05:51:16 +08:00
|
|
|
}
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<TargetInfo::ConstraintInfo, 4> InputConstraintInfos;
|
|
|
|
|
|
|
|
for (unsigned i = NumOutputs, e = NumOutputs + NumInputs; i != e; i++) {
|
|
|
|
StringLiteral *Literal = Constraints[i];
|
2014-12-11 09:00:48 +08:00
|
|
|
assert(Literal->isAscii());
|
2012-08-18 05:19:40 +08:00
|
|
|
|
|
|
|
StringRef InputName;
|
|
|
|
if (Names[i])
|
|
|
|
InputName = Names[i]->getName();
|
|
|
|
|
|
|
|
TargetInfo::ConstraintInfo Info(Literal->getString(), InputName);
|
2015-10-21 10:34:10 +08:00
|
|
|
if (!Context.getTargetInfo().validateInputConstraint(OutputConstraintInfos,
|
|
|
|
Info)) {
|
2019-02-27 05:51:16 +08:00
|
|
|
targetDiag(Literal->getBeginLoc(), diag::err_asm_invalid_input_constraint)
|
|
|
|
<< Info.getConstraintStr();
|
|
|
|
return new (Context)
|
|
|
|
GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
|
|
|
|
NumInputs, Names, Constraints, Exprs.data(), AsmString,
|
2019-06-03 23:57:25 +08:00
|
|
|
NumClobbers, Clobbers, NumLabels, RParenLoc);
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
|
2014-12-29 17:30:33 +08:00
|
|
|
ExprResult ER = CheckPlaceholderExpr(Exprs[i]);
|
|
|
|
if (ER.isInvalid())
|
|
|
|
return StmtError();
|
|
|
|
Exprs[i] = ER.get();
|
|
|
|
|
2012-08-18 05:19:40 +08:00
|
|
|
Expr *InputExpr = Exprs[i];
|
|
|
|
|
2014-10-08 09:58:02 +08:00
|
|
|
// Referring to parameters is not allowed in naked functions.
|
|
|
|
if (CheckNakedParmReference(InputExpr, *this))
|
|
|
|
return StmtError();
|
|
|
|
|
2015-08-03 18:38:10 +08:00
|
|
|
// Check that the input expression is compatible with memory constraint.
|
|
|
|
if (Info.allowsMemory() &&
|
|
|
|
checkExprMemoryConstraintCompat(*this, InputExpr, Info, true))
|
|
|
|
return StmtError();
|
2015-06-05 21:40:59 +08:00
|
|
|
|
2012-08-18 05:19:40 +08:00
|
|
|
// Only allow void types for memory constraints.
|
|
|
|
if (Info.allowsMemory() && !Info.allowsRegister()) {
|
|
|
|
if (CheckAsmLValue(InputExpr, *this))
|
2018-08-10 05:08:08 +08:00
|
|
|
return StmtError(Diag(InputExpr->getBeginLoc(),
|
2012-08-18 05:19:40 +08:00
|
|
|
diag::err_asm_invalid_lvalue_in_input)
|
|
|
|
<< Info.getConstraintStr()
|
|
|
|
<< InputExpr->getSourceRange());
|
2015-01-06 12:26:34 +08:00
|
|
|
} else if (Info.requiresImmediateConstant() && !Info.allowsRegister()) {
|
2015-07-15 02:08:50 +08:00
|
|
|
if (!InputExpr->isValueDependent()) {
|
2018-12-19 12:36:42 +08:00
|
|
|
Expr::EvalResult EVResult;
|
Delay diagnosing asm constraints that require immediates until after inlining
Summary:
An inline asm call may result in an immediate input value after inlining.
Therefore, don't emit a diagnostic here if the input isn't an immediate.
Reviewers: joerg, eli.friedman, rsmith
Subscribers: asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, s.egerton, krytarowski, mgorny, riccibruno, eraman, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D60943
llvm-svn: 368104
2019-08-07 06:41:22 +08:00
|
|
|
if (InputExpr->EvaluateAsRValue(EVResult, Context, true)) {
|
|
|
|
// For compatibility with GCC, we also allow pointers that would be
|
|
|
|
// integral constant expressions if they were cast to int.
|
|
|
|
llvm::APSInt IntResult;
|
|
|
|
if (EVResult.Val.toIntegralConstant(IntResult, InputExpr->getType(),
|
|
|
|
Context))
|
|
|
|
if (!Info.isValidAsmImmediate(IntResult))
|
|
|
|
return StmtError(Diag(InputExpr->getBeginLoc(),
|
|
|
|
diag::err_invalid_asm_value_for_constraint)
|
|
|
|
<< IntResult.toString(10)
|
|
|
|
<< Info.getConstraintStr()
|
|
|
|
<< InputExpr->getSourceRange());
|
|
|
|
}
|
2015-07-15 02:08:50 +08:00
|
|
|
}
|
2015-01-06 12:26:34 +08:00
|
|
|
|
2014-07-15 00:27:53 +08:00
|
|
|
} else {
|
|
|
|
ExprResult Result = DefaultFunctionArrayLvalueConversion(Exprs[i]);
|
|
|
|
if (Result.isInvalid())
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
Exprs[i] = Result.get();
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Info.allowsRegister()) {
|
|
|
|
if (InputExpr->getType()->isVoidType()) {
|
2018-08-10 05:08:08 +08:00
|
|
|
return StmtError(
|
|
|
|
Diag(InputExpr->getBeginLoc(), diag::err_asm_invalid_type_in_input)
|
|
|
|
<< InputExpr->getType() << Info.getConstraintStr()
|
|
|
|
<< InputExpr->getSourceRange());
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InputConstraintInfos.push_back(Info);
|
2012-11-12 14:42:51 +08:00
|
|
|
|
|
|
|
const Type *Ty = Exprs[i]->getType().getTypePtr();
|
2013-03-26 05:09:49 +08:00
|
|
|
if (Ty->isDependentType())
|
2012-11-13 07:13:34 +08:00
|
|
|
continue;
|
|
|
|
|
2013-03-26 05:09:49 +08:00
|
|
|
if (!Ty->isVoidType() || !Info.allowsMemory())
|
2018-08-10 05:08:08 +08:00
|
|
|
if (RequireCompleteType(InputExpr->getBeginLoc(), Exprs[i]->getType(),
|
2013-03-27 14:06:26 +08:00
|
|
|
diag::err_dereference_incomplete_type))
|
|
|
|
return StmtError();
|
2013-03-26 05:09:49 +08:00
|
|
|
|
2012-11-12 14:42:51 +08:00
|
|
|
unsigned Size = Context.getTypeSize(Ty);
|
|
|
|
if (!Context.getTargetInfo().validateInputSize(Literal->getString(),
|
|
|
|
Size))
|
2019-02-21 01:42:57 +08:00
|
|
|
return StmtResult(
|
|
|
|
targetDiag(InputExpr->getBeginLoc(), diag::err_asm_invalid_input_size)
|
2018-08-10 05:08:08 +08:00
|
|
|
<< Info.getConstraintStr());
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the clobbers are valid.
|
|
|
|
for (unsigned i = 0; i != NumClobbers; i++) {
|
|
|
|
StringLiteral *Literal = Clobbers[i];
|
2014-12-11 09:00:48 +08:00
|
|
|
assert(Literal->isAscii());
|
2012-08-18 05:19:40 +08:00
|
|
|
|
|
|
|
StringRef Clobber = Literal->getString();
|
|
|
|
|
2019-02-27 05:51:16 +08:00
|
|
|
if (!Context.getTargetInfo().isValidClobber(Clobber)) {
|
|
|
|
targetDiag(Literal->getBeginLoc(), diag::err_asm_unknown_register_name)
|
|
|
|
<< Clobber;
|
|
|
|
return new (Context)
|
|
|
|
GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
|
|
|
|
NumInputs, Names, Constraints, Exprs.data(), AsmString,
|
2019-06-03 23:57:25 +08:00
|
|
|
NumClobbers, Clobbers, NumLabels, RParenLoc);
|
2019-02-27 05:51:16 +08:00
|
|
|
}
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
|
2012-08-25 08:11:56 +08:00
|
|
|
GCCAsmStmt *NS =
|
|
|
|
new (Context) GCCAsmStmt(Context, AsmLoc, IsSimple, IsVolatile, NumOutputs,
|
2013-05-10 09:14:26 +08:00
|
|
|
NumInputs, Names, Constraints, Exprs.data(),
|
2019-06-03 23:57:25 +08:00
|
|
|
AsmString, NumClobbers, Clobbers, NumLabels,
|
|
|
|
RParenLoc);
|
2012-08-18 05:19:40 +08:00
|
|
|
// Validate the asm string, ensuring it makes sense given the operands we
|
|
|
|
// have.
|
2012-08-25 08:11:56 +08:00
|
|
|
SmallVector<GCCAsmStmt::AsmStringPiece, 8> Pieces;
|
2012-08-18 05:19:40 +08:00
|
|
|
unsigned DiagOffs;
|
2019-02-27 05:51:16 +08:00
|
|
|
if (unsigned DiagID = NS->AnalyzeAsmString(Pieces, Context, DiagOffs)) {
|
|
|
|
targetDiag(getLocationOfStringLiteralByte(AsmString, DiagOffs), DiagID)
|
|
|
|
<< AsmString->getSourceRange();
|
|
|
|
return NS;
|
|
|
|
}
|
2012-08-18 05:19:40 +08:00
|
|
|
|
2012-10-26 07:28:48 +08:00
|
|
|
// Validate constraints and modifiers.
|
|
|
|
for (unsigned i = 0, e = Pieces.size(); i != e; ++i) {
|
|
|
|
GCCAsmStmt::AsmStringPiece &Piece = Pieces[i];
|
|
|
|
if (!Piece.isOperand()) continue;
|
|
|
|
|
|
|
|
// Look for the correct constraint index.
|
2015-02-04 08:27:13 +08:00
|
|
|
unsigned ConstraintIdx = Piece.getOperandNo();
|
2019-06-03 23:57:25 +08:00
|
|
|
// Labels are the last in the Exprs list.
|
|
|
|
if (NS->isAsmGoto() && ConstraintIdx >= NS->getNumInputs())
|
|
|
|
continue;
|
2015-02-04 08:27:13 +08:00
|
|
|
unsigned NumOperands = NS->getNumOutputs() + NS->getNumInputs();
|
|
|
|
// Look for the (ConstraintIdx - NumOperands + 1)th constraint with
|
|
|
|
// modifier '+'.
|
|
|
|
if (ConstraintIdx >= NumOperands) {
|
|
|
|
unsigned I = 0, E = NS->getNumOutputs();
|
2012-10-26 07:28:48 +08:00
|
|
|
|
2015-02-04 08:27:13 +08:00
|
|
|
for (unsigned Cnt = ConstraintIdx - NumOperands; I != E; ++I)
|
|
|
|
if (OutputConstraintInfos[I].isReadWrite() && Cnt-- == 0) {
|
|
|
|
ConstraintIdx = I;
|
2012-10-26 07:28:48 +08:00
|
|
|
break;
|
2015-02-04 08:27:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(I != E && "Invalid operand number should have been caught in "
|
|
|
|
" AnalyzeAsmString");
|
2012-10-26 07:28:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have the right indexes go ahead and check.
|
|
|
|
StringLiteral *Literal = Constraints[ConstraintIdx];
|
|
|
|
const Type *Ty = Exprs[ConstraintIdx]->getType().getTypePtr();
|
|
|
|
if (Ty->isDependentType() || Ty->isIncompleteType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned Size = Context.getTypeSize(Ty);
|
2014-08-22 14:05:21 +08:00
|
|
|
std::string SuggestedModifier;
|
|
|
|
if (!Context.getTargetInfo().validateConstraintModifier(
|
|
|
|
Literal->getString(), Piece.getModifier(), Size,
|
|
|
|
SuggestedModifier)) {
|
2019-02-21 01:42:57 +08:00
|
|
|
targetDiag(Exprs[ConstraintIdx]->getBeginLoc(),
|
|
|
|
diag::warn_asm_mismatched_size_modifier);
|
2014-08-22 14:05:21 +08:00
|
|
|
|
|
|
|
if (!SuggestedModifier.empty()) {
|
2019-02-21 01:42:57 +08:00
|
|
|
auto B = targetDiag(Piece.getRange().getBegin(),
|
|
|
|
diag::note_asm_missing_constraint_modifier)
|
2014-08-22 14:05:21 +08:00
|
|
|
<< SuggestedModifier;
|
|
|
|
SuggestedModifier = "%" + SuggestedModifier + Piece.getString();
|
2019-02-21 01:42:57 +08:00
|
|
|
B << FixItHint::CreateReplacement(Piece.getRange(), SuggestedModifier);
|
2014-08-22 14:05:21 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-26 07:28:48 +08:00
|
|
|
}
|
|
|
|
|
2012-08-18 05:19:40 +08:00
|
|
|
// Validate tied input operands for type mismatches.
|
2014-12-29 12:09:59 +08:00
|
|
|
unsigned NumAlternatives = ~0U;
|
|
|
|
for (unsigned i = 0, e = OutputConstraintInfos.size(); i != e; ++i) {
|
|
|
|
TargetInfo::ConstraintInfo &Info = OutputConstraintInfos[i];
|
|
|
|
StringRef ConstraintStr = Info.getConstraintStr();
|
|
|
|
unsigned AltCount = ConstraintStr.count(',') + 1;
|
2019-02-27 05:51:16 +08:00
|
|
|
if (NumAlternatives == ~0U) {
|
2014-12-29 12:09:59 +08:00
|
|
|
NumAlternatives = AltCount;
|
2019-02-27 05:51:16 +08:00
|
|
|
} else if (NumAlternatives != AltCount) {
|
|
|
|
targetDiag(NS->getOutputExpr(i)->getBeginLoc(),
|
|
|
|
diag::err_asm_unexpected_constraint_alternatives)
|
|
|
|
<< NumAlternatives << AltCount;
|
|
|
|
return NS;
|
|
|
|
}
|
2014-12-29 12:09:59 +08:00
|
|
|
}
|
2015-09-21 22:41:00 +08:00
|
|
|
SmallVector<size_t, 4> InputMatchedToOutput(OutputConstraintInfos.size(),
|
|
|
|
~0U);
|
2012-08-18 05:19:40 +08:00
|
|
|
for (unsigned i = 0, e = InputConstraintInfos.size(); i != e; ++i) {
|
|
|
|
TargetInfo::ConstraintInfo &Info = InputConstraintInfos[i];
|
2014-12-29 12:09:59 +08:00
|
|
|
StringRef ConstraintStr = Info.getConstraintStr();
|
|
|
|
unsigned AltCount = ConstraintStr.count(',') + 1;
|
2019-02-27 05:51:16 +08:00
|
|
|
if (NumAlternatives == ~0U) {
|
2014-12-29 12:09:59 +08:00
|
|
|
NumAlternatives = AltCount;
|
2019-02-27 05:51:16 +08:00
|
|
|
} else if (NumAlternatives != AltCount) {
|
|
|
|
targetDiag(NS->getInputExpr(i)->getBeginLoc(),
|
|
|
|
diag::err_asm_unexpected_constraint_alternatives)
|
|
|
|
<< NumAlternatives << AltCount;
|
|
|
|
return NS;
|
|
|
|
}
|
2012-08-18 05:19:40 +08:00
|
|
|
|
|
|
|
// If this is a tied constraint, verify that the output and input have
|
|
|
|
// either exactly the same type, or that they are int/ptr operands with the
|
|
|
|
// same size (int/long, int*/long, are ok etc).
|
|
|
|
if (!Info.hasTiedOperand()) continue;
|
|
|
|
|
|
|
|
unsigned TiedTo = Info.getTiedOperand();
|
|
|
|
unsigned InputOpNo = i+NumOutputs;
|
|
|
|
Expr *OutputExpr = Exprs[TiedTo];
|
|
|
|
Expr *InputExpr = Exprs[InputOpNo];
|
|
|
|
|
2015-09-21 22:41:00 +08:00
|
|
|
// Make sure no more than one input constraint matches each output.
|
|
|
|
assert(TiedTo < InputMatchedToOutput.size() && "TiedTo value out of range");
|
|
|
|
if (InputMatchedToOutput[TiedTo] != ~0U) {
|
2019-02-21 01:42:57 +08:00
|
|
|
targetDiag(NS->getInputExpr(i)->getBeginLoc(),
|
|
|
|
diag::err_asm_input_duplicate_match)
|
2015-09-21 22:41:00 +08:00
|
|
|
<< TiedTo;
|
2019-02-27 05:51:16 +08:00
|
|
|
targetDiag(NS->getInputExpr(InputMatchedToOutput[TiedTo])->getBeginLoc(),
|
|
|
|
diag::note_asm_input_duplicate_first)
|
|
|
|
<< TiedTo;
|
|
|
|
return NS;
|
2015-09-21 22:41:00 +08:00
|
|
|
}
|
|
|
|
InputMatchedToOutput[TiedTo] = i;
|
|
|
|
|
2012-08-18 05:19:40 +08:00
|
|
|
if (OutputExpr->isTypeDependent() || InputExpr->isTypeDependent())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QualType InTy = InputExpr->getType();
|
|
|
|
QualType OutTy = OutputExpr->getType();
|
|
|
|
if (Context.hasSameType(InTy, OutTy))
|
|
|
|
continue; // All types can be tied to themselves.
|
|
|
|
|
|
|
|
// Decide if the input and output are in the same domain (integer/ptr or
|
|
|
|
// floating point.
|
|
|
|
enum AsmDomain {
|
|
|
|
AD_Int, AD_FP, AD_Other
|
|
|
|
} InputDomain, OutputDomain;
|
|
|
|
|
|
|
|
if (InTy->isIntegerType() || InTy->isPointerType())
|
|
|
|
InputDomain = AD_Int;
|
|
|
|
else if (InTy->isRealFloatingType())
|
|
|
|
InputDomain = AD_FP;
|
|
|
|
else
|
|
|
|
InputDomain = AD_Other;
|
|
|
|
|
|
|
|
if (OutTy->isIntegerType() || OutTy->isPointerType())
|
|
|
|
OutputDomain = AD_Int;
|
|
|
|
else if (OutTy->isRealFloatingType())
|
|
|
|
OutputDomain = AD_FP;
|
|
|
|
else
|
|
|
|
OutputDomain = AD_Other;
|
|
|
|
|
|
|
|
// They are ok if they are the same size and in the same domain. This
|
|
|
|
// allows tying things like:
|
|
|
|
// void* to int*
|
|
|
|
// void* to int if they are the same size.
|
|
|
|
// double to long double if they are the same size.
|
|
|
|
//
|
|
|
|
uint64_t OutSize = Context.getTypeSize(OutTy);
|
|
|
|
uint64_t InSize = Context.getTypeSize(InTy);
|
|
|
|
if (OutSize == InSize && InputDomain == OutputDomain &&
|
|
|
|
InputDomain != AD_Other)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the smaller input/output operand is not mentioned in the asm string,
|
|
|
|
// then we can promote the smaller one to a larger input and the asm string
|
|
|
|
// won't notice.
|
|
|
|
bool SmallerValueMentioned = false;
|
|
|
|
|
|
|
|
// If this is a reference to the input and if the input was the smaller
|
|
|
|
// one, then we have to reject this asm.
|
|
|
|
if (isOperandMentioned(InputOpNo, Pieces)) {
|
|
|
|
// This is a use in the asm string of the smaller operand. Since we
|
|
|
|
// codegen this by promoting to a wider value, the asm will get printed
|
|
|
|
// "wrong".
|
|
|
|
SmallerValueMentioned |= InSize < OutSize;
|
|
|
|
}
|
|
|
|
if (isOperandMentioned(TiedTo, Pieces)) {
|
|
|
|
// If this is a reference to the output, and if the output is the larger
|
|
|
|
// value, then it's ok because we'll promote the input to the larger type.
|
|
|
|
SmallerValueMentioned |= OutSize < InSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the smaller value wasn't mentioned in the asm string, and if the
|
|
|
|
// output was a register, just extend the shorter one to the size of the
|
|
|
|
// larger one.
|
|
|
|
if (!SmallerValueMentioned && InputDomain != AD_Other &&
|
|
|
|
OutputConstraintInfos[TiedTo].allowsRegister())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Either both of the operands were mentioned or the smaller one was
|
|
|
|
// mentioned. One more special case that we'll allow: if the tied input is
|
|
|
|
// integer, unmentioned, and is a constant, then we'll allow truncating it
|
|
|
|
// down to the size of the destination.
|
|
|
|
if (InputDomain == AD_Int && OutputDomain == AD_Int &&
|
|
|
|
!isOperandMentioned(InputOpNo, Pieces) &&
|
|
|
|
InputExpr->isEvaluatable(Context)) {
|
|
|
|
CastKind castKind =
|
|
|
|
(OutTy->isBooleanType() ? CK_IntegralToBoolean : CK_IntegralCast);
|
2014-05-29 18:55:11 +08:00
|
|
|
InputExpr = ImpCastExprToType(InputExpr, OutTy, castKind).get();
|
2012-08-18 05:19:40 +08:00
|
|
|
Exprs[InputOpNo] = InputExpr;
|
|
|
|
NS->setInputExpr(i, InputExpr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-27 05:51:16 +08:00
|
|
|
targetDiag(InputExpr->getBeginLoc(), diag::err_asm_tying_incompatible_types)
|
|
|
|
<< InTy << OutTy << OutputExpr->getSourceRange()
|
|
|
|
<< InputExpr->getSourceRange();
|
|
|
|
return NS;
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
|
2016-12-26 20:23:42 +08:00
|
|
|
// Check for conflicts between clobber list and input or output lists
|
|
|
|
SourceLocation ConstraintLoc =
|
|
|
|
getClobberConflictLocation(Exprs, Constraints, Clobbers, NumClobbers,
|
2019-06-03 23:57:25 +08:00
|
|
|
NumLabels,
|
2016-12-26 20:23:42 +08:00
|
|
|
Context.getTargetInfo(), Context);
|
|
|
|
if (ConstraintLoc.isValid())
|
2019-02-27 05:51:16 +08:00
|
|
|
targetDiag(ConstraintLoc, diag::error_inoutput_conflict_with_clobber);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2019-06-03 23:57:25 +08:00
|
|
|
// Check for duplicate asm operand name between input, output and label lists.
|
|
|
|
typedef std::pair<StringRef , Expr *> NamedOperand;
|
|
|
|
SmallVector<NamedOperand, 4> NamedOperandList;
|
|
|
|
for (unsigned i = 0, e = NumOutputs + NumInputs + NumLabels; i != e; ++i)
|
|
|
|
if (Names[i])
|
|
|
|
NamedOperandList.emplace_back(
|
|
|
|
std::make_pair(Names[i]->getName(), Exprs[i]));
|
|
|
|
// Sort NamedOperandList.
|
|
|
|
std::stable_sort(NamedOperandList.begin(), NamedOperandList.end(),
|
|
|
|
[](const NamedOperand &LHS, const NamedOperand &RHS) {
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
});
|
|
|
|
// Find adjacent duplicate operand.
|
|
|
|
SmallVector<NamedOperand, 4>::iterator Found =
|
|
|
|
std::adjacent_find(begin(NamedOperandList), end(NamedOperandList),
|
|
|
|
[](const NamedOperand &LHS, const NamedOperand &RHS) {
|
|
|
|
return LHS.first == RHS.first;
|
|
|
|
});
|
|
|
|
if (Found != NamedOperandList.end()) {
|
|
|
|
Diag((Found + 1)->second->getBeginLoc(),
|
|
|
|
diag::error_duplicate_asm_operand_name)
|
|
|
|
<< (Found + 1)->first;
|
|
|
|
Diag(Found->second->getBeginLoc(), diag::note_duplicate_asm_operand_name)
|
|
|
|
<< Found->first;
|
|
|
|
return StmtError();
|
|
|
|
}
|
|
|
|
if (NS->isAsmGoto())
|
|
|
|
setFunctionHasBranchIntoScope();
|
2014-05-29 22:05:12 +08:00
|
|
|
return NS;
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-29 15:02:49 +08:00
|
|
|
void Sema::FillInlineAsmIdentifierInfo(Expr *Res,
|
|
|
|
llvm::InlineAsmIdentifierInfo &Info) {
|
|
|
|
QualType T = Res->getType();
|
|
|
|
Expr::EvalResult Eval;
|
|
|
|
if (T->isFunctionType() || T->isDependentType())
|
|
|
|
return Info.setLabel(Res);
|
|
|
|
if (Res->isRValue()) {
|
|
|
|
if (isa<clang::EnumType>(T) && Res->EvaluateAsRValue(Eval, Context))
|
|
|
|
return Info.setEnum(Eval.Val.getInt().getSExtValue());
|
|
|
|
return Info.setLabel(Res);
|
2015-08-27 05:57:20 +08:00
|
|
|
}
|
2017-09-29 15:02:49 +08:00
|
|
|
unsigned Size = Context.getTypeSizeInChars(T).getQuantity();
|
|
|
|
unsigned Type = Size;
|
|
|
|
if (const auto *ATy = Context.getAsArrayType(T))
|
|
|
|
Type = Context.getTypeSizeInChars(ATy->getElementType()).getQuantity();
|
|
|
|
bool IsGlobalLV = false;
|
|
|
|
if (Res->EvaluateAsLValue(Eval, Context))
|
|
|
|
IsGlobalLV = Eval.isGlobalLValue();
|
|
|
|
Info.setVar(Res, IsGlobalLV, Size, Type);
|
2015-08-27 05:57:20 +08:00
|
|
|
}
|
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS,
|
|
|
|
SourceLocation TemplateKWLoc,
|
|
|
|
UnqualifiedId &Id,
|
|
|
|
bool IsUnevaluatedContext) {
|
2012-08-18 05:19:40 +08:00
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
if (IsUnevaluatedContext)
|
2017-04-02 05:30:49 +08:00
|
|
|
PushExpressionEvaluationContext(
|
|
|
|
ExpressionEvaluationContext::UnevaluatedAbstract,
|
|
|
|
ReuseLambdaContextDecl);
|
2012-10-16 03:56:10 +08:00
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
ExprResult Result = ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Id,
|
|
|
|
/*trailing lparen*/ false,
|
2013-05-25 02:32:55 +08:00
|
|
|
/*is & operand*/ false,
|
2014-05-26 14:22:03 +08:00
|
|
|
/*CorrectionCandidateCallback=*/nullptr,
|
2013-05-25 02:32:55 +08:00
|
|
|
/*IsInlineAsmIdentifier=*/ true);
|
2012-08-18 05:19:40 +08:00
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
if (IsUnevaluatedContext)
|
|
|
|
PopExpressionEvaluationContext();
|
2012-08-18 05:19:40 +08:00
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
if (!Result.isUsable()) return Result;
|
2012-08-18 05:19:40 +08:00
|
|
|
|
2014-05-29 18:55:11 +08:00
|
|
|
Result = CheckPlaceholderExpr(Result.get());
|
2013-05-03 08:10:13 +08:00
|
|
|
if (!Result.isUsable()) return Result;
|
2012-10-23 10:43:30 +08:00
|
|
|
|
2014-09-05 06:16:48 +08:00
|
|
|
// Referring to parameters is not allowed in naked functions.
|
2014-10-08 09:58:02 +08:00
|
|
|
if (CheckNakedParmReference(Result.get(), *this))
|
|
|
|
return ExprError();
|
2017-07-26 03:17:32 +08:00
|
|
|
|
|
|
|
QualType T = Result.get()->getType();
|
2012-10-26 05:49:22 +08:00
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
if (T->isDependentType()) {
|
2016-01-05 07:51:15 +08:00
|
|
|
return Result;
|
2012-10-23 10:43:30 +08:00
|
|
|
}
|
2012-10-16 03:56:10 +08:00
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
// Any sort of function type is fine.
|
|
|
|
if (T->isFunctionType()) {
|
|
|
|
return Result;
|
2013-04-20 04:37:49 +08:00
|
|
|
}
|
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
// Otherwise, it needs to be a complete type.
|
2017-07-26 03:17:32 +08:00
|
|
|
if (RequireCompleteExprType(Result.get(), diag::err_asm_incomplete_type)) {
|
2013-05-03 08:10:13 +08:00
|
|
|
return ExprError();
|
2012-10-18 23:49:40 +08:00
|
|
|
}
|
|
|
|
|
2013-05-03 08:10:13 +08:00
|
|
|
return Result;
|
2012-10-16 03:56:10 +08:00
|
|
|
}
|
2012-08-23 03:18:30 +08:00
|
|
|
|
2012-10-26 05:49:22 +08:00
|
|
|
bool Sema::LookupInlineAsmField(StringRef Base, StringRef Member,
|
|
|
|
unsigned &Offset, SourceLocation AsmLoc) {
|
|
|
|
Offset = 0;
|
2015-12-17 20:51:51 +08:00
|
|
|
SmallVector<StringRef, 2> Members;
|
|
|
|
Member.split(Members, ".");
|
|
|
|
|
2017-08-09 21:31:41 +08:00
|
|
|
NamedDecl *FoundDecl = nullptr;
|
|
|
|
|
|
|
|
// MS InlineAsm uses 'this' as a base
|
|
|
|
if (getLangOpts().CPlusPlus && Base.equals("this")) {
|
|
|
|
if (const Type *PT = getCurrentThisType().getTypePtrOrNull())
|
|
|
|
FoundDecl = PT->getPointeeType()->getAsTagDecl();
|
|
|
|
} else {
|
|
|
|
LookupResult BaseResult(*this, &Context.Idents.get(Base), SourceLocation(),
|
|
|
|
LookupOrdinaryName);
|
|
|
|
if (LookupName(BaseResult, getCurScope()) && BaseResult.isSingleResult())
|
|
|
|
FoundDecl = BaseResult.getFoundDecl();
|
|
|
|
}
|
2012-10-26 05:49:22 +08:00
|
|
|
|
2017-08-09 21:31:41 +08:00
|
|
|
if (!FoundDecl)
|
2016-03-16 17:56:58 +08:00
|
|
|
return true;
|
2017-08-09 21:31:41 +08:00
|
|
|
|
2015-12-17 20:51:51 +08:00
|
|
|
for (StringRef NextMember : Members) {
|
|
|
|
const RecordType *RT = nullptr;
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(FoundDecl))
|
|
|
|
RT = VD->getType()->getAs<RecordType>();
|
|
|
|
else if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(FoundDecl)) {
|
|
|
|
MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
|
2017-08-09 21:31:41 +08:00
|
|
|
// MS InlineAsm often uses struct pointer aliases as a base
|
|
|
|
QualType QT = TD->getUnderlyingType();
|
|
|
|
if (const auto *PT = QT->getAs<PointerType>())
|
|
|
|
QT = PT->getPointeeType();
|
|
|
|
RT = QT->getAs<RecordType>();
|
2015-12-17 20:51:51 +08:00
|
|
|
} else if (TypeDecl *TD = dyn_cast<TypeDecl>(FoundDecl))
|
|
|
|
RT = TD->getTypeForDecl()->getAs<RecordType>();
|
|
|
|
else if (FieldDecl *TD = dyn_cast<FieldDecl>(FoundDecl))
|
|
|
|
RT = TD->getType()->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return true;
|
2012-10-26 05:49:22 +08:00
|
|
|
|
2015-12-19 06:40:25 +08:00
|
|
|
if (RequireCompleteType(AsmLoc, QualType(RT, 0),
|
|
|
|
diag::err_asm_incomplete_type))
|
2015-12-17 20:51:51 +08:00
|
|
|
return true;
|
2012-10-26 05:49:22 +08:00
|
|
|
|
2015-12-17 20:51:51 +08:00
|
|
|
LookupResult FieldResult(*this, &Context.Idents.get(NextMember),
|
|
|
|
SourceLocation(), LookupMemberName);
|
|
|
|
|
|
|
|
if (!LookupQualifiedName(FieldResult, RT->getDecl()))
|
|
|
|
return true;
|
2012-10-26 05:49:22 +08:00
|
|
|
|
2016-03-16 17:56:58 +08:00
|
|
|
if (!FieldResult.isSingleResult())
|
|
|
|
return true;
|
|
|
|
FoundDecl = FieldResult.getFoundDecl();
|
|
|
|
|
2015-12-17 20:51:51 +08:00
|
|
|
// FIXME: Handle IndirectFieldDecl?
|
2016-03-16 17:56:58 +08:00
|
|
|
FieldDecl *FD = dyn_cast<FieldDecl>(FoundDecl);
|
2015-12-17 20:51:51 +08:00
|
|
|
if (!FD)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const ASTRecordLayout &RL = Context.getASTRecordLayout(RT->getDecl());
|
|
|
|
unsigned i = FD->getFieldIndex();
|
|
|
|
CharUnits Result = Context.toCharUnitsFromBits(RL.getFieldOffset(i));
|
|
|
|
Offset += (unsigned)Result.getQuantity();
|
|
|
|
}
|
2012-10-26 05:49:22 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-27 05:57:20 +08:00
|
|
|
ExprResult
|
2016-01-05 08:08:41 +08:00
|
|
|
Sema::LookupInlineAsmVarDeclField(Expr *E, StringRef Member,
|
2015-08-27 05:57:20 +08:00
|
|
|
SourceLocation AsmLoc) {
|
|
|
|
|
2016-01-05 07:51:15 +08:00
|
|
|
QualType T = E->getType();
|
|
|
|
if (T->isDependentType()) {
|
|
|
|
DeclarationNameInfo NameInfo;
|
|
|
|
NameInfo.setLoc(AsmLoc);
|
|
|
|
NameInfo.setName(&Context.Idents.get(Member));
|
|
|
|
return CXXDependentScopeMemberExpr::Create(
|
|
|
|
Context, E, T, /*IsArrow=*/false, AsmLoc, NestedNameSpecifierLoc(),
|
|
|
|
SourceLocation(),
|
2019-07-16 12:46:31 +08:00
|
|
|
/*FirstQualifierFoundInScope=*/nullptr, NameInfo, /*TemplateArgs=*/nullptr);
|
2016-01-05 07:51:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const RecordType *RT = T->getAs<RecordType>();
|
2015-08-27 05:57:20 +08:00
|
|
|
// FIXME: Diagnose this as field access into a scalar type.
|
|
|
|
if (!RT)
|
|
|
|
return ExprResult();
|
|
|
|
|
|
|
|
LookupResult FieldResult(*this, &Context.Idents.get(Member), AsmLoc,
|
|
|
|
LookupMemberName);
|
|
|
|
|
|
|
|
if (!LookupQualifiedName(FieldResult, RT->getDecl()))
|
|
|
|
return ExprResult();
|
|
|
|
|
|
|
|
// Only normal and indirect field results will work.
|
|
|
|
ValueDecl *FD = dyn_cast<FieldDecl>(FieldResult.getFoundDecl());
|
|
|
|
if (!FD)
|
|
|
|
FD = dyn_cast<IndirectFieldDecl>(FieldResult.getFoundDecl());
|
|
|
|
if (!FD)
|
|
|
|
return ExprResult();
|
|
|
|
|
|
|
|
// Make an Expr to thread through OpDecl.
|
|
|
|
ExprResult Result = BuildMemberReferenceExpr(
|
|
|
|
E, E->getType(), AsmLoc, /*IsArrow=*/false, CXXScopeSpec(),
|
2015-09-01 22:49:24 +08:00
|
|
|
SourceLocation(), nullptr, FieldResult, nullptr, nullptr);
|
2015-08-27 05:57:20 +08:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-09-13 08:06:55 +08:00
|
|
|
StmtResult Sema::ActOnMSAsmStmt(SourceLocation AsmLoc, SourceLocation LBraceLoc,
|
2013-05-03 08:10:13 +08:00
|
|
|
ArrayRef<Token> AsmToks,
|
|
|
|
StringRef AsmString,
|
|
|
|
unsigned NumOutputs, unsigned NumInputs,
|
|
|
|
ArrayRef<StringRef> Constraints,
|
|
|
|
ArrayRef<StringRef> Clobbers,
|
|
|
|
ArrayRef<Expr*> Exprs,
|
|
|
|
SourceLocation EndLoc) {
|
|
|
|
bool IsSimple = (NumOutputs != 0 || NumInputs != 0);
|
2018-03-13 05:43:02 +08:00
|
|
|
setFunctionHasBranchProtectedScope();
|
2012-08-18 05:19:40 +08:00
|
|
|
MSAsmStmt *NS =
|
2012-10-18 23:49:40 +08:00
|
|
|
new (Context) MSAsmStmt(Context, AsmLoc, LBraceLoc, IsSimple,
|
|
|
|
/*IsVolatile*/ true, AsmToks, NumOutputs, NumInputs,
|
2013-05-03 08:10:13 +08:00
|
|
|
Constraints, Exprs, AsmString,
|
|
|
|
Clobbers, EndLoc);
|
2014-05-29 22:05:12 +08:00
|
|
|
return NS;
|
2012-08-18 05:19:40 +08:00
|
|
|
}
|
2014-09-22 10:21:54 +08:00
|
|
|
|
|
|
|
LabelDecl *Sema::GetOrCreateMSAsmLabel(StringRef ExternalLabelName,
|
|
|
|
SourceLocation Location,
|
|
|
|
bool AlwaysCreate) {
|
|
|
|
LabelDecl* Label = LookupOrCreateLabel(PP.getIdentifierInfo(ExternalLabelName),
|
|
|
|
Location);
|
|
|
|
|
2014-10-09 01:28:34 +08:00
|
|
|
if (Label->isMSAsmLabel()) {
|
|
|
|
// If we have previously created this label implicitly, mark it as used.
|
|
|
|
Label->markUsed(Context);
|
|
|
|
} else {
|
2014-09-22 10:21:54 +08:00
|
|
|
// Otherwise, insert it, but only resolve it if we have seen the label itself.
|
|
|
|
std::string InternalName;
|
|
|
|
llvm::raw_string_ostream OS(InternalName);
|
2016-12-07 08:17:18 +08:00
|
|
|
// Create an internal name for the label. The name should not be a valid
|
|
|
|
// mangled name, and should be unique. We use a dot to make the name an
|
|
|
|
// invalid mangled name. We use LLVM's inline asm ${:uid} escape so that a
|
|
|
|
// unique label is generated each time this blob is emitted, even after
|
|
|
|
// inlining or LTO.
|
2016-11-29 08:39:37 +08:00
|
|
|
OS << "__MSASMLABEL_.${:uid}__";
|
2016-11-29 04:52:19 +08:00
|
|
|
for (char C : ExternalLabelName) {
|
|
|
|
OS << C;
|
|
|
|
// We escape '$' in asm strings by replacing it with "$$"
|
|
|
|
if (C == '$')
|
2015-12-29 16:49:34 +08:00
|
|
|
OS << '$';
|
|
|
|
}
|
2014-09-22 10:21:54 +08:00
|
|
|
Label->setMSAsmLabel(OS.str());
|
|
|
|
}
|
|
|
|
if (AlwaysCreate) {
|
|
|
|
// The label might have been created implicitly from a previously encountered
|
|
|
|
// goto statement. So, for both newly created and looked up labels, we mark
|
|
|
|
// them as resolved.
|
|
|
|
Label->setMSAsmLabelResolved();
|
|
|
|
}
|
|
|
|
// Adjust their location for being able to generate accurate diagnostics.
|
|
|
|
Label->setLocation(Location);
|
|
|
|
|
|
|
|
return Label;
|
|
|
|
}
|