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

Chris Lattner sabre at nondot.org
Fri Jul 10 15:20:30 PDT 2009


Author: lattner
Date: Fri Jul 10 17:20:30 2009
New Revision: 75301

URL: http://llvm.org/viewvc/llvm-project?rev=75301&view=rev
Log:
add support for .zerofill, patch by Kevin Enderby!

Added:
    llvm/trunk/test/MC/AsmParser/directive_zerofill.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=75301&r1=75300&r2=75301&view=diff

==============================================================================
--- llvm/trunk/include/llvm/MC/MCStreamer.h (original)
+++ llvm/trunk/include/llvm/MC/MCStreamer.h Fri Jul 10 17:20:30 2009
@@ -125,6 +125,17 @@
     virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
                                   unsigned Pow2Alignment, bool IsLocal) = 0;
 
+    /// EmitZerofill - Emit a the zerofill section and possiblity a symbol, if
+    /// @param Symbol is non-NULL, for @param Size and with the @param
+    /// Pow2Alignment if non-zero.
+    ///
+    /// @param Section - The zerofill section to create and or to put the symbol
+    /// @param Symbol - The zerofill symbol to emit, if non-NULL.
+    /// @param Size - The size of the zerofill symbol.
+    /// @param Pow2Alignment - The alignment of the zerofill symbol if non-zero.
+    virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = 0,
+                              unsigned Size = 0,unsigned Pow2Alignment = 0) = 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=75301&r1=75300&r2=75301&view=diff

==============================================================================
--- llvm/trunk/lib/MC/MCAsmStreamer.cpp (original)
+++ llvm/trunk/lib/MC/MCAsmStreamer.cpp Fri Jul 10 17:20:30 2009
@@ -44,6 +44,9 @@
     virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
                                   unsigned Pow2Alignment, bool IsLocal);
 
+    virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
+                              unsigned Size = 0, unsigned Pow2Alignment = 0);
+
     virtual void EmitBytes(const char *Data, unsigned Length);
 
     virtual void EmitValue(const MCValue &Value, unsigned Size);
@@ -157,6 +160,21 @@
   OS << '\n';
 }
 
+void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
+                                 unsigned Size, unsigned Pow2Alignment) {
+  // Note: a .zerofill directive does not switch sections
+  // FIXME: Really we would like the segment and section names as well as the
+  // section type to be separate values instead of embedded in the name. Not
+  // all assemblers understand all this stuff though.
+  OS << ".zerofill " << Section->getName();
+  if (Symbol != NULL) {
+    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_zerofill.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/directive_zerofill.s?rev=75301&view=auto

==============================================================================
--- llvm/trunk/test/MC/AsmParser/directive_zerofill.s (added)
+++ llvm/trunk/test/MC/AsmParser/directive_zerofill.s Fri Jul 10 17:20:30 2009
@@ -0,0 +1,10 @@
+# RUN: llvm-mc %s | FileCheck %s
+
+# CHECK: TEST0:
+# CHECK: .zerofill __FOO,__bar,x,1
+# CHECK: .zerofill __FOO,__bar,y,8,2
+# CHECK: .zerofill __EMPTY,__NoSymbol
+TEST0:  
+	.zerofill __FOO, __bar, x, 2-1
+	.zerofill __FOO,   __bar, y ,  8 , 1+1
+	.zerofill __EMPTY,__NoSymbol

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

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.cpp (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.cpp Fri Jul 10 17:20:30 2009
@@ -524,6 +524,8 @@
       return ParseDirectiveComm(/*IsLocal=*/false);
     if (!strcmp(IDVal, ".lcomm"))
       return ParseDirectiveComm(/*IsLocal=*/true);
+    if (!strcmp(IDVal, ".zerofill"))
+      return ParseDirectiveDarwinZerofill();
 
     Warning(IDLoc, "ignoring directive for now");
     EatToEndOfStatement();
@@ -958,3 +960,95 @@
 
   return false;
 }
+
+/// ParseDirectiveDarwinZerofill
+///  ::= .zerofill segname , sectname [, identifier , size_expression [
+///      , align_expression ]]
+bool AsmParser::ParseDirectiveDarwinZerofill() {
+  if (Lexer.isNot(asmtok::Identifier))
+    return TokError("expected segment name after '.zerofill' directive");
+  std::string Section = Lexer.getCurStrVal();
+  Lexer.Lex();
+
+  if (Lexer.isNot(asmtok::Comma))
+    return TokError("unexpected token in directive");
+  Section += ',';
+  Lexer.Lex();
+ 
+  if (Lexer.isNot(asmtok::Identifier))
+    return TokError("expected section name after comma in '.zerofill' "
+                    "directive");
+  Section += Lexer.getCurStrVal();
+  Lexer.Lex();
+
+  // FIXME: we will need to tell GetSection() that this is to be created with or
+  // must have the Mach-O section type of S_ZEROFILL.  Something like the code
+  // below could be done but for now it is not as EmitZerofill() does not know
+  // how to deal with a section type in the section name like
+  // ParseDirectiveDarwinSection() allows.
+  // Section += ',';
+  // Section += "zerofill";
+
+  // If this is the end of the line all that was wanted was to create the
+  // the section but with no symbol.
+  if (Lexer.is(asmtok::EndOfStatement)) {
+    // Create the zerofill section but no symbol
+    Out.EmitZerofill(Ctx.GetSection(Section.c_str()));
+    return false;
+  }
+
+  if (Lexer.isNot(asmtok::Comma))
+    return TokError("unexpected token in directive");
+  Lexer.Lex();
+
+  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 '.zerofill' directive");
+  
+  Lexer.Lex();
+
+  if (Size < 0)
+    return Error(SizeLoc, "invalid '.zerofill' directive 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 '.zerofill' directive 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");
+
+  // Create the zerofill Symbol with Size and Pow2Alignment
+  Out.EmitZerofill(Ctx.GetSection(Section.c_str()), 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=75301&r1=75300&r2=75301&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.h (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.h Fri Jul 10 17:20:30 2009
@@ -111,7 +111,7 @@
   bool ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr);
 
   bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
-  
+  bool ParseDirectiveDarwinZerofill(); // Darwin specific ".zerofill"
 };
 
 } // end namespace llvm





More information about the llvm-commits mailing list