diff options
author | Kevin Enderby <enderby@apple.com> | 2011-12-14 21:47:48 +0000 |
---|---|---|
committer | Kevin Enderby <enderby@apple.com> | 2011-12-14 21:47:48 +0000 |
commit | 109f25c966ac1648a5fe60f3ba76c438e24f8f9c (patch) | |
tree | f980ab08b6ecd12a63ce4969b00095b0b588ad66 | |
parent | 37a9102b0bcc4b7b6dd5ce25c85441ab21b3e764 (diff) | |
download | bcm5719-llvm-109f25c966ac1648a5fe60f3ba76c438e24f8f9c.tar.gz bcm5719-llvm-109f25c966ac1648a5fe60f3ba76c438e24f8f9c.zip |
Add the .incbin directive which takes the binary data from a file and emits
it to the streamer. rdar://10383898
llvm-svn: 146592
-rw-r--r-- | llvm/lib/MC/MCParser/AsmParser.cpp | 49 | ||||
-rw-r--r-- | llvm/test/MC/AsmParser/directive_incbin.s | 9 | ||||
-rw-r--r-- | llvm/test/MC/AsmParser/incbin_abcd | 1 |
3 files changed, 59 insertions, 0 deletions
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp index 943f270a0a7..93492377ff0 100644 --- a/llvm/lib/MC/MCParser/AsmParser.cpp +++ b/llvm/lib/MC/MCParser/AsmParser.cpp @@ -181,6 +181,9 @@ private: /// 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 @@ private: 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 @@ bool AsmParser::EnterIncludeFile(const std::string &Filename) { 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 @@ bool AsmParser::ParseStatement() { 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 @@ bool AsmParser::ParseDirectiveInclude() { 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) { diff --git a/llvm/test/MC/AsmParser/directive_incbin.s b/llvm/test/MC/AsmParser/directive_incbin.s new file mode 100644 index 00000000000..230573c6488 --- /dev/null +++ b/llvm/test/MC/AsmParser/directive_incbin.s @@ -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 diff --git a/llvm/test/MC/AsmParser/incbin_abcd b/llvm/test/MC/AsmParser/incbin_abcd new file mode 100644 index 00000000000..acbe86c7c89 --- /dev/null +++ b/llvm/test/MC/AsmParser/incbin_abcd @@ -0,0 +1 @@ +abcd |