[llvm-branch-commits] [llvm] c64037b - [ms] [llvm-ml] Support command-line defines

Eric Astor via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 1 15:11:04 PST 2020


Author: Eric Astor
Date: 2020-12-01T18:06:05-05:00
New Revision: c64037b784aeae63b1863ee1abd5601c2c6a8102

URL: https://github.com/llvm/llvm-project/commit/c64037b784aeae63b1863ee1abd5601c2c6a8102
DIFF: https://github.com/llvm/llvm-project/commit/c64037b784aeae63b1863ee1abd5601c2c6a8102.diff

LOG: [ms] [llvm-ml] Support command-line defines

Enable command-line defines as textmacros

Reviewed By: thakis

Differential Revision: https://reviews.llvm.org/D90059

Added: 
    llvm/test/tools/llvm-ml/command_line_defines.asm

Modified: 
    llvm/include/llvm/MC/MCParser/MCAsmParser.h
    llvm/lib/MC/MCParser/MasmParser.cpp
    llvm/tools/llvm-ml/Opts.td
    llvm/tools/llvm-ml/llvm-ml.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCParser/MCAsmParser.h b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
index 2040810eac14..391a6b0b575e 100644
--- a/llvm/include/llvm/MC/MCParser/MCAsmParser.h
+++ b/llvm/include/llvm/MC/MCParser/MCAsmParser.h
@@ -184,6 +184,8 @@ class MCAsmParser {
 
   virtual bool isParsingMasm() const { return false; }
 
+  virtual bool defineMacro(StringRef Name, StringRef Value) { return true; }
+
   virtual bool lookUpField(StringRef Name, AsmFieldInfo &Info) const {
     return true;
   }

diff  --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index 709f1ea1173d..63aebbc5a7ba 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -501,6 +501,8 @@ class MasmParser : public MCAsmParser {
 
   bool isParsingMasm() const override { return true; }
 
+  bool defineMacro(StringRef Name, StringRef Value) override;
+
   bool lookUpField(StringRef Name, AsmFieldInfo &Info) const override;
   bool lookUpField(StringRef Base, StringRef Member,
                    AsmFieldInfo &Info) const override;
@@ -6905,6 +6907,19 @@ static int rewritesSort(const AsmRewrite *AsmRewriteA,
   llvm_unreachable("Unstable rewrite sort.");
 }
 
+bool MasmParser::defineMacro(StringRef Name, StringRef Value) {
+  Variable &Var = Variables[Name.lower()];
+  if (Var.Name.empty()) {
+    Var.Name = Name;
+  } else if (!Var.Redefinable) {
+    return TokError("invalid variable redefinition");
+  }
+  Var.Redefinable = true;
+  Var.IsText = true;
+  Var.TextValue = Value.str();
+  return false;
+}
+
 bool MasmParser::lookUpField(StringRef Name, AsmFieldInfo &Info) const {
   const std::pair<StringRef, StringRef> BaseMember = Name.split('.');
   const StringRef Base = BaseMember.first, Member = BaseMember.second;

diff  --git a/llvm/test/tools/llvm-ml/command_line_defines.asm b/llvm/test/tools/llvm-ml/command_line_defines.asm
new file mode 100644
index 000000000000..51b02e6ebb4a
--- /dev/null
+++ b/llvm/test/tools/llvm-ml/command_line_defines.asm
@@ -0,0 +1,38 @@
+; RUN: llvm-ml -filetype=s %s /Fo - /DT1=test1 /D T2=test2 | FileCheck %s
+
+.code
+
+t1:
+  ret
+; CHECK-NOT: t1:
+; CHECK-LABEL: test1:
+; CHECK-NOT: t1:
+
+t2:
+  ret
+; CHECK-NOT: t2:
+; CHECK-LABEL: test2:
+; CHECK-NOT: t2:
+
+t3:
+ifdef t1
+  xor eax, eax
+endif
+  ret
+; CHECK-LABEL: t3:
+; CHECK: xor eax, eax
+; CHECK: ret
+
+t4:
+ifdef undefined
+  xor eax, eax
+elseifdef t2
+  xor ebx, ebx
+endif
+  ret
+; CHECK-LABEL: t4:
+; CHECK-NOT: xor eax, eax
+; CHECK: xor ebx, ebx
+; CHECK: ret
+
+end

diff  --git a/llvm/tools/llvm-ml/Opts.td b/llvm/tools/llvm-ml/Opts.td
index ed52bb4771ba..4c2757b05722 100644
--- a/llvm/tools/llvm-ml/Opts.td
+++ b/llvm/tools/llvm-ml/Opts.td
@@ -31,6 +31,9 @@ def help : MLFlag<"?">,
            HelpText<"Display available options">;
 def help_long : MLFlag<"help">, Alias<help>;
 def assemble_only : MLFlag<"c">, HelpText<"Assemble only; do not link">;
+def define : MLJoinedOrSeparate<"D">, MetaVarName<"<macro>=<value>">,
+             HelpText<"Define <macro> to <value> (or blank if <value> "
+                      "omitted)">;
 def output_file : MLJoinedOrSeparate<"Fo">, HelpText<"Names the output file">;
 def include_path : MLJoinedOrSeparate<"I">,
                    HelpText<"Sets path for include files">;
@@ -72,7 +75,6 @@ def coff_object_file : UnsupportedFlag<"coff">, HelpText<"">;
 def preserve_identifier_case : UnsupportedFlag<"Cp">, HelpText<"">;
 def uppercase_identifiers : UnsupportedFlag<"Cu">, HelpText<"">;
 def preserve_extern_case : UnsupportedFlag<"Cx">, HelpText<"">;
-def define : UnsupportedJoinedOrSeparate<"D">, HelpText<"">;
 def output_preprocessed : UnsupportedFlag<"EP">, HelpText<"">;
 def errorreport : UnsupportedJoined<"ERRORREPORT">, HelpText<"">;
 def stacksize : UnsupportedSeparate<"F">, HelpText<"">;

diff  --git a/llvm/tools/llvm-ml/llvm-ml.cpp b/llvm/tools/llvm-ml/llvm-ml.cpp
index ac03ef826c58..1733dcd47281 100644
--- a/llvm/tools/llvm-ml/llvm-ml.cpp
+++ b/llvm/tools/llvm-ml/llvm-ml.cpp
@@ -147,6 +147,17 @@ static int AssembleInput(StringRef ProgName, const Target *TheTarget,
   Parser->getLexer().setLexMasmHexFloats(true);
   Parser->getLexer().setLexMasmStrings(true);
 
+  auto Defines = InputArgs.getAllArgValues(OPT_define);
+  for (StringRef Define : Defines) {
+    const auto NameValue = Define.split('=');
+    StringRef Name = NameValue.first, Value = NameValue.second;
+    if (Parser->defineMacro(Name, Value)) {
+      WithColor::error(errs(), ProgName)
+          << "can't define macro '" << Name << "' = '" << Value << "'\n";
+      return 1;
+    }
+  }
+
   int Res = Parser->Run(/*NoInitialTextSection=*/true);
 
   return Res;


        


More information about the llvm-branch-commits mailing list