Path: blob/main/contrib/llvm-project/llvm/lib/IR/InlineAsm.cpp
35234 views
//===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the InlineAsm class.9//10//===----------------------------------------------------------------------===//1112#include "llvm/IR/InlineAsm.h"13#include "ConstantsContext.h"14#include "LLVMContextImpl.h"15#include "llvm/ADT/StringRef.h"16#include "llvm/IR/DerivedTypes.h"17#include "llvm/IR/LLVMContext.h"18#include "llvm/IR/Value.h"19#include "llvm/Support/Casting.h"20#include "llvm/Support/Compiler.h"21#include "llvm/Support/Errc.h"22#include <algorithm>23#include <cassert>24#include <cctype>25#include <cstdlib>2627using namespace llvm;2829InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,30const std::string &constraints, bool hasSideEffects,31bool isAlignStack, AsmDialect asmDialect, bool canThrow)32: Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),33AsmString(asmString), Constraints(constraints), FTy(FTy),34HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),35Dialect(asmDialect), CanThrow(canThrow) {36#ifndef NDEBUG37// Do various checks on the constraint string and type.38cantFail(verify(getFunctionType(), constraints));39#endif40}4142InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,43StringRef Constraints, bool hasSideEffects,44bool isAlignStack, AsmDialect asmDialect,45bool canThrow) {46InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,47isAlignStack, asmDialect, canThrow);48LLVMContextImpl *pImpl = FTy->getContext().pImpl;49return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);50}5152void InlineAsm::destroyConstant() {53getType()->getContext().pImpl->InlineAsms.remove(this);54delete this;55}5657FunctionType *InlineAsm::getFunctionType() const {58return FTy;59}6061void InlineAsm::collectAsmStrs(SmallVectorImpl<StringRef> &AsmStrs) const {62StringRef AsmStr(AsmString);63AsmStrs.clear();6465// TODO: 1) Unify delimiter for inline asm, we also meet other delimiters66// for example "\0A", ";".67// 2) Enhance StringRef. Some of the special delimiter ("\0") can't be68// split in StringRef. Also empty StringRef can not call split (will stuck).69if (AsmStr.empty())70return;71AsmStr.split(AsmStrs, "\n\t", -1, false);72}7374/// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the75/// fields in this structure. If the constraint string is not understood,76/// return true, otherwise return false.77bool InlineAsm::ConstraintInfo::Parse(StringRef Str,78InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {79StringRef::iterator I = Str.begin(), E = Str.end();80unsigned multipleAlternativeCount = Str.count('|') + 1;81unsigned multipleAlternativeIndex = 0;82ConstraintCodeVector *pCodes = &Codes;8384// Initialize85isMultipleAlternative = multipleAlternativeCount > 1;86if (isMultipleAlternative) {87multipleAlternatives.resize(multipleAlternativeCount);88pCodes = &multipleAlternatives[0].Codes;89}90Type = isInput;91isEarlyClobber = false;92MatchingInput = -1;93isCommutative = false;94isIndirect = false;95currentAlternativeIndex = 0;9697// Parse prefixes.98if (*I == '~') {99Type = isClobber;100++I;101102// '{' must immediately follow '~'.103if (I != E && *I != '{')104return true;105} else if (*I == '=') {106++I;107Type = isOutput;108} else if (*I == '!') {109++I;110Type = isLabel;111}112113if (*I == '*') {114isIndirect = true;115++I;116}117118if (I == E) return true; // Just a prefix, like "==" or "~".119120// Parse the modifiers.121bool DoneWithModifiers = false;122while (!DoneWithModifiers) {123switch (*I) {124default:125DoneWithModifiers = true;126break;127case '&': // Early clobber.128if (Type != isOutput || // Cannot early clobber anything but output.129isEarlyClobber) // Reject &&&&&&130return true;131isEarlyClobber = true;132break;133case '%': // Commutative.134if (Type == isClobber || // Cannot commute clobbers.135isCommutative) // Reject %%%%%136return true;137isCommutative = true;138break;139case '#': // Comment.140case '*': // Register preferencing.141return true; // Not supported.142}143144if (!DoneWithModifiers) {145++I;146if (I == E) return true; // Just prefixes and modifiers!147}148}149150// Parse the various constraints.151while (I != E) {152if (*I == '{') { // Physical register reference.153// Find the end of the register name.154StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');155if (ConstraintEnd == E) return true; // "{foo"156pCodes->push_back(std::string(StringRef(I, ConstraintEnd + 1 - I)));157I = ConstraintEnd+1;158} else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint159// Maximal munch numbers.160StringRef::iterator NumStart = I;161while (I != E && isdigit(static_cast<unsigned char>(*I)))162++I;163pCodes->push_back(std::string(StringRef(NumStart, I - NumStart)));164unsigned N = atoi(pCodes->back().c_str());165// Check that this is a valid matching constraint!166if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||167Type != isInput)168return true; // Invalid constraint number.169170// If Operand N already has a matching input, reject this. An output171// can't be constrained to the same value as multiple inputs.172if (isMultipleAlternative) {173if (multipleAlternativeIndex >=174ConstraintsSoFar[N].multipleAlternatives.size())175return true;176InlineAsm::SubConstraintInfo &scInfo =177ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];178if (scInfo.MatchingInput != -1)179return true;180// Note that operand #n has a matching input.181scInfo.MatchingInput = ConstraintsSoFar.size();182assert(scInfo.MatchingInput >= 0);183} else {184if (ConstraintsSoFar[N].hasMatchingInput() &&185(size_t)ConstraintsSoFar[N].MatchingInput !=186ConstraintsSoFar.size())187return true;188// Note that operand #n has a matching input.189ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();190assert(ConstraintsSoFar[N].MatchingInput >= 0);191}192} else if (*I == '|') {193multipleAlternativeIndex++;194pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;195++I;196} else if (*I == '^') {197// Multi-letter constraint198// FIXME: For now assuming these are 2-character constraints.199pCodes->push_back(std::string(StringRef(I + 1, 2)));200I += 3;201} else if (*I == '@') {202// Multi-letter constraint203++I;204unsigned char C = static_cast<unsigned char>(*I);205assert(isdigit(C) && "Expected a digit!");206int N = C - '0';207assert(N > 0 && "Found a zero letter constraint!");208++I;209pCodes->push_back(std::string(StringRef(I, N)));210I += N;211} else {212// Single letter constraint.213pCodes->push_back(std::string(StringRef(I, 1)));214++I;215}216}217218return false;219}220221/// selectAlternative - Point this constraint to the alternative constraint222/// indicated by the index.223void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {224if (index < multipleAlternatives.size()) {225currentAlternativeIndex = index;226InlineAsm::SubConstraintInfo &scInfo =227multipleAlternatives[currentAlternativeIndex];228MatchingInput = scInfo.MatchingInput;229Codes = scInfo.Codes;230}231}232233InlineAsm::ConstraintInfoVector234InlineAsm::ParseConstraints(StringRef Constraints) {235ConstraintInfoVector Result;236237// Scan the constraints string.238for (StringRef::iterator I = Constraints.begin(),239E = Constraints.end(); I != E; ) {240ConstraintInfo Info;241242// Find the end of this constraint.243StringRef::iterator ConstraintEnd = std::find(I, E, ',');244245if (ConstraintEnd == I || // Empty constraint like ",,"246Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {247Result.clear(); // Erroneous constraint?248break;249}250251Result.push_back(Info);252253// ConstraintEnd may be either the next comma or the end of the string. In254// the former case, we skip the comma.255I = ConstraintEnd;256if (I != E) {257++I;258if (I == E) {259Result.clear();260break;261} // don't allow "xyz,"262}263}264265return Result;266}267268static Error makeStringError(const char *Msg) {269return createStringError(errc::invalid_argument, Msg);270}271272Error InlineAsm::verify(FunctionType *Ty, StringRef ConstStr) {273if (Ty->isVarArg())274return makeStringError("inline asm cannot be variadic");275276ConstraintInfoVector Constraints = ParseConstraints(ConstStr);277278// Error parsing constraints.279if (Constraints.empty() && !ConstStr.empty())280return makeStringError("failed to parse constraints");281282unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;283unsigned NumIndirect = 0, NumLabels = 0;284285for (const ConstraintInfo &Constraint : Constraints) {286switch (Constraint.Type) {287case InlineAsm::isOutput:288if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0 || NumLabels != 0)289return makeStringError("output constraint occurs after input, "290"clobber or label constraint");291292if (!Constraint.isIndirect) {293++NumOutputs;294break;295}296++NumIndirect;297[[fallthrough]]; // We fall through for Indirect Outputs.298case InlineAsm::isInput:299if (NumClobbers)300return makeStringError("input constraint occurs after clobber "301"constraint");302++NumInputs;303break;304case InlineAsm::isClobber:305++NumClobbers;306break;307case InlineAsm::isLabel:308if (NumClobbers)309return makeStringError("label constraint occurs after clobber "310"constraint");311312++NumLabels;313break;314}315}316317switch (NumOutputs) {318case 0:319if (!Ty->getReturnType()->isVoidTy())320return makeStringError("inline asm without outputs must return void");321break;322case 1:323if (Ty->getReturnType()->isStructTy())324return makeStringError("inline asm with one output cannot return struct");325break;326default:327StructType *STy = dyn_cast<StructType>(Ty->getReturnType());328if (!STy || STy->getNumElements() != NumOutputs)329return makeStringError("number of output constraints does not match "330"number of return struct elements");331break;332}333334if (Ty->getNumParams() != NumInputs)335return makeStringError("number of input constraints does not match number "336"of parameters");337338// We don't have access to labels here, NumLabels will be checked separately.339return Error::success();340}341342343