[PATCH] D24714: [MC] Support skip and count for .incbin directive

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 18 18:05:39 PDT 2016


phosek created this revision.
phosek added subscribers: llvm-commits, phosek.
phosek set the repository for this revision to rL LLVM.

These optional arguments are supported by GNU assembler.

Repository:
  rL LLVM

https://reviews.llvm.org/D24714

Files:
  lib/MC/MCParser/AsmParser.cpp
  test/MC/AsmParser/directive_incbin.s

Index: test/MC/AsmParser/directive_incbin.s
===================================================================
--- test/MC/AsmParser/directive_incbin.s
+++ test/MC/AsmParser/directive_incbin.s
@@ -4,3 +4,13 @@
 .incbin "incbin\137abcd"  # "\137" is underscore "_"
 
 # CHECK: .ascii	 "abcd\n"
+
+.data
+.incbin "incbin\137abcd", 1
+
+# CHECK: .ascii	 "bcd\n"
+
+.data
+.incbin "incbin\137abcd", 1, 2
+
+# CHECK: .ascii	 "bc"
Index: lib/MC/MCParser/AsmParser.cpp
===================================================================
--- lib/MC/MCParser/AsmParser.cpp
+++ lib/MC/MCParser/AsmParser.cpp
@@ -342,7 +342,8 @@
 
   /// \brief Process the specified file for the .incbin directive.
   /// This returns true on failure.
-  bool processIncbinFile(const std::string &Filename);
+  bool processIncbinFile(const std::string &Filename, int64_t Skip,
+                         int64_t Count);
 
   /// \brief Reset the current lexer position to that given by \p Loc. The
   /// current token is not set; clients should ensure Lex() is called
@@ -643,15 +644,17 @@
 /// Process the specified .incbin file by searching for it in the include paths
 /// then just emitting the byte contents of the file to the streamer. This
 /// returns true on failure.
-bool AsmParser::processIncbinFile(const std::string &Filename) {
+bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip,
+                                  int64_t Count) {
   std::string IncludedFile;
   unsigned NewBuf =
       SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
   if (!NewBuf)
     return true;
 
   // Pick up the bytes from the file and emit them.
-  getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer());
+  StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer();
+  getStreamer().EmitBytes(Bytes.drop_front(Skip).take_front(Count));
   return false;
 }
 
@@ -4388,20 +4391,38 @@
 }
 
 /// parseDirectiveIncbin
-///  ::= .incbin "filename"
+///  ::= .incbin "filename" [ , skip [ , count ] ]
 bool AsmParser::parseDirectiveIncbin() {
   // Allow the strings to have escaped octal character sequence.
   std::string Filename;
   SMLoc IncbinLoc = getTok().getLoc();
   if (check(getTok().isNot(AsmToken::String),
             "expected string in '.incbin' directive") ||
-      parseEscapedString(Filename) ||
-      parseToken(AsmToken::EndOfStatement,
+      parseEscapedString(Filename))
+    return true;
+
+  int64_t Skip = 0;
+  int64_t Count = -1;
+  if (getLexer().isNot(AsmToken::EndOfStatement)) {
+    if (parseToken(AsmToken::Comma,
+                   "unexpected token in '.incbin' directive") ||
+        parseAbsoluteExpression(Skip))
+      return true;
+ 
+    if (getLexer().isNot(AsmToken::EndOfStatement)) {
+      if (parseToken(AsmToken::Comma,
+                     "unexpected token in '.incbin' directive") ||
+          parseAbsoluteExpression(Count))
+        return true;
+    }
+  }
+
+  if (parseToken(AsmToken::EndOfStatement,
                  "unexpected token in '.incbin' directive"))
     return true;
 
   // Attempt to process the included file.
-  if (processIncbinFile(Filename))
+  if (processIncbinFile(Filename, Skip, Count))
     return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'");
   return false;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24714.71764.patch
Type: text/x-patch
Size: 3299 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160919/60aeb577/attachment.bin>


More information about the llvm-commits mailing list