[llvm-commits] [llvm] r158604 - in /llvm/trunk: lib/MC/MCParser/AsmParser.cpp test/MC/AsmParser/macro-irpc.s

Rafael Espindola rafael.espindola at gmail.com
Sat Jun 16 11:03:26 PDT 2012


Author: rafael
Date: Sat Jun 16 13:03:25 2012
New Revision: 158604

URL: http://llvm.org/viewvc/llvm-project?rev=158604&view=rev
Log:
Implement irpc. Extracted from a patch by the PaX team. I just added the test.

Added:
    llvm/trunk/test/MC/AsmParser/macro-irpc.s
Modified:
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=158604&r1=158603&r2=158604&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Sat Jun 16 13:03:25 2012
@@ -277,6 +277,7 @@
                                 raw_svector_ostream &OS);
   bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept"
   bool ParseDirectiveIrp(SMLoc DirectiveLoc);  // ".irp"
+  bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc"
   bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr"
 };
 
@@ -1280,6 +1281,8 @@
       return ParseDirectiveRept(IDLoc);
     if (IDVal == ".irp")
       return ParseDirectiveIrp(IDLoc);
+    if (IDVal == ".irpc")
+      return ParseDirectiveIrpc(IDLoc);
     if (IDVal == ".endr")
       return ParseDirectiveEndr(IDLoc);
 
@@ -3324,6 +3327,60 @@
   return false;
 }
 
+/// ParseDirectiveIrpc
+/// ::= .irpc symbol,values
+bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) {
+  std::vector<StringRef> Parameters;
+  StringRef Parameter;
+
+  if (ParseIdentifier(Parameter))
+    return TokError("expected identifier in '.irpc' directive");
+
+  Parameters.push_back(Parameter);
+
+  if (Lexer.isNot(AsmToken::Comma))
+    return TokError("expected comma in '.irpc' directive");
+
+  Lex();
+
+  std::vector<MacroArgument> A;
+  if (ParseMacroArguments(0, A))
+    return true;
+
+  if (A.size() != 1 || A.front().size() != 1)
+    return TokError("unexpected token in '.irpc' directive");
+
+  // Eat the end of statement.
+  Lex();
+
+  // Lex the irpc definition.
+  Macro *M = ParseMacroLikeBody(DirectiveLoc);
+  if (!M)
+    return true;
+
+  // Macro instantiation is lexical, unfortunately. We construct a new buffer
+  // to hold the macro body with substitutions.
+  SmallString<256> Buf;
+  raw_svector_ostream OS(Buf);
+
+  StringRef Values = A.front().front().getString();
+  std::size_t I, End = Values.size();
+  for (I = 0; I < End; ++I) {
+    MacroArgument Arg;
+    Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1)));
+
+    std::vector<MacroArgument> Args;
+    Args.push_back(Arg);
+
+    if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc()))
+      return true;
+  }
+
+  InstantiateMacroLikeBody(M, DirectiveLoc, OS);
+
+  return false;
+}
+
 bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) {
   if (ActiveMacros.empty())
     return TokError("unexpected '.endr' directive, no current .rept");

Added: llvm/trunk/test/MC/AsmParser/macro-irpc.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/macro-irpc.s?rev=158604&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/macro-irpc.s (added)
+++ llvm/trunk/test/MC/AsmParser/macro-irpc.s Sat Jun 16 13:03:25 2012
@@ -0,0 +1,9 @@
+// RUN: llvm-mc -triple x86_64-unknown-unknown %s | FileCheck %s
+
+.irpc foo,123
+        .long \foo
+.endr
+
+// CHECK: long 1
+// CHECK: long 2
+// CHECK: long 3





More information about the llvm-commits mailing list