[llvm-commits] [llvm] r74944 - in /llvm/trunk: include/llvm/MC/MCStreamer.h lib/MC/MCAsmStreamer.cpp test/MC/AsmParser/directive_comm.s tools/llvm-mc/AsmParser.cpp tools/llvm-mc/AsmParser.h

Chris Lattner sabre at nondot.org
Tue Jul 7 13:30:48 PDT 2009


Author: lattner
Date: Tue Jul  7 15:30:46 2009
New Revision: 74944

URL: http://llvm.org/viewvc/llvm-project?rev=74944&view=rev
Log:
Implement parsing support for the .comm directive.  Patch by
Kevin Enderby!

Added:
    llvm/trunk/test/MC/AsmParser/directive_comm.s
Modified:
    llvm/trunk/include/llvm/MC/MCStreamer.h
    llvm/trunk/lib/MC/MCAsmStreamer.cpp
    llvm/trunk/tools/llvm-mc/AsmParser.cpp
    llvm/trunk/tools/llvm-mc/AsmParser.h

Modified: llvm/trunk/include/llvm/MC/MCStreamer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCStreamer.h?rev=74944&r1=74943&r2=74944&view=diff

==============================================================================
--- llvm/trunk/include/llvm/MC/MCStreamer.h (original)
+++ llvm/trunk/include/llvm/MC/MCStreamer.h Tue Jul  7 15:30:46 2009
@@ -115,6 +115,15 @@
     virtual void EmitSymbolAttribute(MCSymbol *Symbol,
                                      SymbolAttr Attribute) = 0;
 
+    /// EmitCommonSymbol - Emit a common symbol of @param Size with the @param
+    /// Pow2Alignment if non-zero.
+    ///
+    /// @param Symbol - The common symbol to emit.
+    /// @param Size - The size of the common symbol.
+    /// @param Pow2Alignment - The alignment of the common symbol if non-zero.
+    virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
+                                  unsigned Pow2Alignment) = 0;
+
     /// @}
     /// @name Generating Data
     /// @{

Modified: llvm/trunk/lib/MC/MCAsmStreamer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCAsmStreamer.cpp?rev=74944&r1=74943&r2=74944&view=diff

==============================================================================
--- llvm/trunk/lib/MC/MCAsmStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmStreamer.cpp Tue Jul  7 15:30:46 2009
@@ -41,6 +41,9 @@
 
     virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
 
+    virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
+                                  unsigned Pow2Alignment);
+
     virtual void EmitBytes(const char *Data, unsigned Length);
 
     virtual void EmitValue(const MCValue &Value, unsigned Size);
@@ -142,6 +145,15 @@
   OS << ' ' << Symbol->getName() << '\n';
 }
 
+void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
+                                     unsigned Pow2Alignment) {
+  OS << ".comm";
+  OS << ' ' << Symbol->getName() << ',' << Size;
+  if (Pow2Alignment != 0)
+    OS << ',' << Pow2Alignment;
+  OS << '\n';
+}
+
 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
   assert(CurSection && "Cannot emit contents before setting section!");
   for (unsigned i = 0; i != Length; ++i)

Added: llvm/trunk/test/MC/AsmParser/directive_comm.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/directive_comm.s?rev=74944&view=auto

==============================================================================
--- llvm/trunk/test/MC/AsmParser/directive_comm.s (added)
+++ llvm/trunk/test/MC/AsmParser/directive_comm.s Tue Jul  7 15:30:46 2009
@@ -0,0 +1,8 @@
+# RUN: llvm-mc %s > %t
+
+# RUN: grep -A 3 TEST0 %t > %t2
+# RUN: grep ".comm a,6,2" %t2 | count 1
+# RUN: grep ".comm b,8" %t2 | count 1
+TEST0:  
+        .comm a, 4+2, 2
+        .comm b,8

Modified: llvm/trunk/tools/llvm-mc/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.cpp?rev=74944&r1=74943&r2=74944&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.cpp (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.cpp Tue Jul  7 15:30:46 2009
@@ -520,6 +520,9 @@
     if (!strcmp(IDVal, ".weak_reference"))
       return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
 
+    if (!strcmp(IDVal, ".comm"))
+      return ParseDirectiveComm();
+
     Warning(IDLoc, "ignoring directive for now");
     EatToEndOfStatement();
     return false;
@@ -896,3 +899,59 @@
   Lexer.Lex();
   return false;  
 }
+
+/// ParseDirectiveComm
+///  ::= .comm identifier , size_expression [ , align_expression ]
+bool AsmParser::ParseDirectiveComm() {
+  if (Lexer.isNot(asmtok::Identifier))
+    return TokError("expected identifier in directive");
+  
+  // handle the identifier as the key symbol.
+  SMLoc IDLoc = Lexer.getLoc();
+  MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
+  Lexer.Lex();
+
+  if (Lexer.isNot(asmtok::Comma))
+    return TokError("unexpected token in directive");
+  Lexer.Lex();
+
+  int64_t Size;
+  SMLoc SizeLoc = Lexer.getLoc();
+  if (ParseAbsoluteExpression(Size))
+    return true;
+
+  int64_t Pow2Alignment = 0;
+  SMLoc Pow2AlignmentLoc;
+  if (Lexer.is(asmtok::Comma)) {
+    Lexer.Lex();
+    Pow2AlignmentLoc = Lexer.getLoc();
+    if (ParseAbsoluteExpression(Pow2Alignment))
+      return true;
+  }
+  
+  if (Lexer.isNot(asmtok::EndOfStatement))
+    return TokError("unexpected token in '.comm' directive");
+  
+  Lexer.Lex();
+
+  // NOTE: a size of zero should create a undefined symbol
+  if (Size < 0)
+    return Error(SizeLoc, "invalid '.comm' size, can't be less than zero");
+
+  // NOTE: The alignment in the directive is a power of 2 value, the assember
+  // may internally end up wanting an alignment in bytes.
+  // FIXME: Diagnose overflow.
+  if (Pow2Alignment < 0)
+    return Error(Pow2AlignmentLoc, "invalid '.comm' alignment, can't be less "
+                 "than zero");
+
+  // TODO: Symbol must be undefined or it is a error to re-defined the symbol
+  if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
+    return Error(IDLoc, "invalid symbol redefinition");
+
+  // TODO: Symbol to be made into a common with this Size and Pow2Alignment
+
+  Out.EmitCommonSymbol(Sym, Size, Pow2Alignment);
+
+  return false;
+}

Modified: llvm/trunk/tools/llvm-mc/AsmParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.h?rev=74944&r1=74943&r2=74944&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.h (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.h Tue Jul  7 15:30:46 2009
@@ -109,6 +109,8 @@
   /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which
   /// accepts a single symbol (which should be a label or an external).
   bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
+
+  bool ParseDirectiveComm(); // ".comm"
   
 };
 





More information about the llvm-commits mailing list