[llvm-commits] [llvm] r146592 - in /llvm/trunk: lib/MC/MCParser/AsmParser.cpp test/MC/AsmParser/directive_incbin.s test/MC/AsmParser/incbin_abcd

Kevin Enderby enderby at apple.com
Wed Dec 14 13:47:48 PST 2011


Author: enderby
Date: Wed Dec 14 15:47:48 2011
New Revision: 146592

URL: http://llvm.org/viewvc/llvm-project?rev=146592&view=rev
Log:
Add the .incbin directive which takes the binary data from a file and emits
it to the streamer.  rdar://10383898

Added:
    llvm/trunk/test/MC/AsmParser/directive_incbin.s
    llvm/trunk/test/MC/AsmParser/incbin_abcd
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=146592&r1=146591&r2=146592&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Wed Dec 14 15:47:48 2011
@@ -181,6 +181,9 @@
 
   /// EnterIncludeFile - Enter the specified file. This returns true on failure.
   bool EnterIncludeFile(const std::string &Filename);
+  /// ProcessIncbinFile - Process the specified file for the .incbin directive.
+  /// This returns true on failure.
+  bool ProcessIncbinFile(const std::string &Filename);
 
   /// \brief Reset the current lexer position to that given by \arg Loc. The
   /// current token is not set; clients should ensure Lex() is called
@@ -227,6 +230,7 @@
 
   bool ParseDirectiveAbort(); // ".abort"
   bool ParseDirectiveInclude(); // ".include"
+  bool ParseDirectiveIncbin(); // ".incbin"
 
   bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if"
   // ".ifdef" or ".ifndef", depending on expect_defined
@@ -429,6 +433,24 @@
   return false;
 }
 
+/// Process the specified .incbin file by seaching for it in the include paths
+/// then just emiting the byte contents of the file to the streamer. This 
+/// returns true on failure.
+bool AsmParser::ProcessIncbinFile(const std::string &Filename) {
+  std::string IncludedFile;
+  int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
+  if (NewBuf == -1)
+    return true;
+
+  // Loop picking the bytes from the file and emitting them.
+  const char *BufferStart = SrcMgr.getMemoryBuffer(NewBuf)->getBufferStart();
+  const char *BufferEnd = SrcMgr.getMemoryBuffer(NewBuf)->getBufferEnd();
+  for(const char *p = BufferStart; p < BufferEnd; p++)
+    getStreamer().EmitIntValue(*p, 1, DEFAULT_ADDRSPACE);
+
+  return false;
+}
+
 void AsmParser::JumpToLoc(SMLoc Loc) {
   CurBuffer = SrcMgr.FindBufferContainingLoc(Loc);
   Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer());
@@ -1183,6 +1205,8 @@
       return ParseDirectiveAbort();
     if (IDVal == ".include")
       return ParseDirectiveInclude();
+    if (IDVal == ".incbin")
+      return ParseDirectiveIncbin();
 
     if (IDVal == ".code16")
       return TokError(Twine(IDVal) + " not supported yet");
@@ -2206,6 +2230,31 @@
   return false;
 }
 
+/// ParseDirectiveIncbin
+///  ::= .incbin "filename"
+bool AsmParser::ParseDirectiveIncbin() {
+  if (getLexer().isNot(AsmToken::String))
+    return TokError("expected string in '.incbin' directive");
+
+  std::string Filename = getTok().getString();
+  SMLoc IncbinLoc = getLexer().getLoc();
+  Lex();
+
+  if (getLexer().isNot(AsmToken::EndOfStatement))
+    return TokError("unexpected token in '.incbin' directive");
+
+  // Strip the quotes.
+  Filename = Filename.substr(1, Filename.size()-2);
+
+  // Attempt to process the included file.
+  if (ProcessIncbinFile(Filename)) {
+    Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
+    return true;
+  }
+
+  return false;
+}
+
 /// ParseDirectiveIf
 /// ::= .if expression
 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {

Added: llvm/trunk/test/MC/AsmParser/directive_incbin.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/directive_incbin.s?rev=146592&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/directive_incbin.s (added)
+++ llvm/trunk/test/MC/AsmParser/directive_incbin.s Wed Dec 14 15:47:48 2011
@@ -0,0 +1,9 @@
+# RUN: llvm-mc -triple i386-unknown-unknown %s -I %p | FileCheck %s
+
+.data
+.incbin "incbin_abcd"
+
+# CHECK: .byte	97
+# CHECK: .byte	98
+# CHECK: .byte	99
+# CHECK: .byte	100

Added: llvm/trunk/test/MC/AsmParser/incbin_abcd
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/incbin_abcd?rev=146592&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/incbin_abcd (added)
+++ llvm/trunk/test/MC/AsmParser/incbin_abcd Wed Dec 14 15:47:48 2011
@@ -0,0 +1 @@
+abcd





More information about the llvm-commits mailing list