[llvm] r325139 - Store defined macros in MCContext.
Hans Wennborg via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 16 02:13:28 PST 2018
Merged in r325330.
On Wed, Feb 14, 2018 at 5:34 PM, Rafael Espindola via llvm-commits
<llvm-commits at lists.llvm.org> wrote:
> Author: rafael
> Date: Wed Feb 14 08:34:27 2018
> New Revision: 325139
>
> URL: http://llvm.org/viewvc/llvm-project?rev=325139&view=rev
> Log:
> Store defined macros in MCContext.
>
> So that macros defined in inline assembly blocks are available to the
> whole file.
>
> This provides a consistent behavior with other assembly directives,
> since equations for example are already preserved between inline
> assembly blocks.
>
> PR: 36110
>
> Patch by Roger!
>
> Added:
> llvm/trunk/include/llvm/MC/MCAsmMacro.h
> llvm/trunk/test/MC/AsmParser/inline_macro_duplication.ll
> Modified:
> llvm/trunk/include/llvm/MC/MCContext.h
> llvm/trunk/lib/MC/MCParser/AsmParser.cpp
>
> Added: llvm/trunk/include/llvm/MC/MCAsmMacro.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCAsmMacro.h?rev=325139&view=auto
> ==============================================================================
> --- llvm/trunk/include/llvm/MC/MCAsmMacro.h (added)
> +++ llvm/trunk/include/llvm/MC/MCAsmMacro.h Wed Feb 14 08:34:27 2018
> @@ -0,0 +1,38 @@
> +//===- MCAsmMacro.h - Assembly Macros ---------------------------*- C++ -*-===//
> +//
> +// The LLVM Compiler Infrastructure
> +//
> +// This file is distributed under the University of Illinois Open Source
> +// License. See LICENSE.TXT for details.
> +//
> +//===----------------------------------------------------------------------===//
> +
> +#ifndef LLVM_MC_MCASMMACRO_H
> +#define LLVM_MC_MCASMMACRO_H
> +
> +#include "llvm/MC/MCParser/MCAsmLexer.h"
> +
> +namespace llvm {
> +
> +struct MCAsmMacroParameter {
> + StringRef Name;
> + std::vector<AsmToken> Value;
> + bool Required = false;
> + bool Vararg = false;
> +
> + MCAsmMacroParameter() = default;
> +};
> +
> +typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
> +struct MCAsmMacro {
> + StringRef Name;
> + StringRef Body;
> + MCAsmMacroParameters Parameters;
> +
> +public:
> + MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
> + : Name(N), Body(B), Parameters(std::move(P)) {}
> +};
> +}; // namespace llvm
> +
> +#endif
>
> Modified: llvm/trunk/include/llvm/MC/MCContext.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCContext.h?rev=325139&r1=325138&r2=325139&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/MC/MCContext.h (original)
> +++ llvm/trunk/include/llvm/MC/MCContext.h Wed Feb 14 08:34:27 2018
> @@ -18,6 +18,7 @@
> #include "llvm/ADT/StringRef.h"
> #include "llvm/ADT/Twine.h"
> #include "llvm/BinaryFormat/Dwarf.h"
> +#include "llvm/MC/MCAsmMacro.h"
> #include "llvm/MC/MCDwarf.h"
> #include "llvm/MC/MCSubtargetInfo.h"
> #include "llvm/MC/SectionKind.h"
> @@ -269,6 +270,9 @@ namespace llvm {
> unsigned UniqueID,
> const MCSymbolELF *Associated);
>
> + /// \brief Map of currently defined macros.
> + StringMap<MCAsmMacro> MacroMap;
> +
> public:
> explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI,
> const MCObjectFileInfo *MOFI,
> @@ -620,6 +624,17 @@ namespace llvm {
> // FIXME: We should really do something about that.
> LLVM_ATTRIBUTE_NORETURN void reportFatalError(SMLoc L,
> const Twine &Msg);
> +
> + const MCAsmMacro *lookupMacro(StringRef Name) {
> + StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
> + return (I == MacroMap.end()) ? nullptr : &I->getValue();
> + }
> +
> + void defineMacro(StringRef Name, MCAsmMacro Macro) {
> + MacroMap.insert(std::make_pair(Name, std::move(Macro)));
> + }
> +
> + void undefineMacro(StringRef Name) { MacroMap.erase(Name); }
> };
>
> } // end namespace llvm
>
> Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=325139&r1=325138&r2=325139&view=diff
> ==============================================================================
> --- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
> +++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Wed Feb 14 08:34:27 2018
> @@ -84,27 +84,6 @@ namespace {
> typedef std::vector<AsmToken> MCAsmMacroArgument;
> typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
>
> -struct MCAsmMacroParameter {
> - StringRef Name;
> - MCAsmMacroArgument Value;
> - bool Required = false;
> - bool Vararg = false;
> -
> - MCAsmMacroParameter() = default;
> -};
> -
> -typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters;
> -
> -struct MCAsmMacro {
> - StringRef Name;
> - StringRef Body;
> - MCAsmMacroParameters Parameters;
> -
> -public:
> - MCAsmMacro(StringRef N, StringRef B, MCAsmMacroParameters P)
> - : Name(N), Body(B), Parameters(std::move(P)) {}
> -};
> -
> /// \brief Helper class for storing information about an active macro
> /// instantiation.
> struct MacroInstantiation {
> @@ -165,9 +144,6 @@ private:
> /// addDirectiveHandler.
> StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
>
> - /// \brief Map of currently defined macros.
> - StringMap<MCAsmMacro> MacroMap;
> -
> /// \brief Stack of active macro instantiations.
> std::vector<MacroInstantiation*> ActiveMacros;
>
> @@ -309,17 +285,6 @@ private:
> /// \brief Control a flag in the parser that enables or disables macros.
> void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;}
>
> - /// \brief Lookup a previously defined macro.
> - /// \param Name Macro name.
> - /// \returns Pointer to macro. NULL if no such macro was defined.
> - const MCAsmMacro* lookupMacro(StringRef Name);
> -
> - /// \brief Define a new macro with the given name and information.
> - void defineMacro(StringRef Name, MCAsmMacro Macro);
> -
> - /// \brief Undefine a macro. If no such macro was defined, it's a no-op.
> - void undefineMacro(StringRef Name);
> -
> /// \brief Are we inside a macro instantiation?
> bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
>
> @@ -1842,7 +1807,7 @@ bool AsmParser::parseStatement(ParseStat
>
> // If macros are enabled, check to see if this is a macro instantiation.
> if (areMacrosEnabled())
> - if (const MCAsmMacro *M = lookupMacro(IDVal)) {
> + if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) {
> return handleMacroEntry(M, IDLoc);
> }
>
> @@ -2721,17 +2686,6 @@ bool AsmParser::parseMacroArguments(cons
> return TokError("too many positional arguments");
> }
>
> -const MCAsmMacro *AsmParser::lookupMacro(StringRef Name) {
> - StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
> - return (I == MacroMap.end()) ? nullptr : &I->getValue();
> -}
> -
> -void AsmParser::defineMacro(StringRef Name, MCAsmMacro Macro) {
> - MacroMap.insert(std::make_pair(Name, std::move(Macro)));
> -}
> -
> -void AsmParser::undefineMacro(StringRef Name) { MacroMap.erase(Name); }
> -
> bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) {
> // Arbitrarily limit macro nesting depth (default matches 'as'). We can
> // eliminate this, although we should protect against infinite loops.
> @@ -4274,7 +4228,7 @@ bool AsmParser::parseDirectiveMacro(SMLo
> eatToEndOfStatement();
> }
>
> - if (lookupMacro(Name)) {
> + if (getContext().lookupMacro(Name)) {
> return Error(DirectiveLoc, "macro '" + Name + "' is already defined");
> }
>
> @@ -4282,7 +4236,7 @@ bool AsmParser::parseDirectiveMacro(SMLo
> const char *BodyEnd = EndToken.getLoc().getPointer();
> StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
> checkForBadMacro(DirectiveLoc, Name, Body, Parameters);
> - defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
> + getContext().defineMacro(Name, MCAsmMacro(Name, Body, std::move(Parameters)));
> return false;
> }
>
> @@ -4441,10 +4395,10 @@ bool AsmParser::parseDirectivePurgeMacro
> "unexpected token in '.purgem' directive"))
> return true;
>
> - if (!lookupMacro(Name))
> + if (!getContext().lookupMacro(Name))
> return Error(DirectiveLoc, "macro '" + Name + "' is not defined");
>
> - undefineMacro(Name);
> + getContext().undefineMacro(Name);
> return false;
> }
>
>
> Added: llvm/trunk/test/MC/AsmParser/inline_macro_duplication.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/inline_macro_duplication.ll?rev=325139&view=auto
> ==============================================================================
> --- llvm/trunk/test/MC/AsmParser/inline_macro_duplication.ll (added)
> +++ llvm/trunk/test/MC/AsmParser/inline_macro_duplication.ll Wed Feb 14 08:34:27 2018
> @@ -0,0 +1,8 @@
> +; RUN: not llc < %s 2>&1 | FileCheck %s
> +
> +define void @test() {
> + call void asm sideeffect ".macro FOO\0A.endm", "~{dirflag},~{fpsr},~{flags}"() #1
> + call void asm sideeffect ".macro FOO\0A.endm", "~{dirflag},~{fpsr},~{flags}"() #1
> +; CHECK: error: macro 'FOO' is already defined
> + ret void
> +}
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list