Skip to content

Commit 4ca5466

Browse files
committed
MC/AsmParser: Add .base64 directive
1 parent 31180ba commit 4ca5466

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "llvm/MC/MCSymbolMachO.h"
4747
#include "llvm/MC/MCTargetOptions.h"
4848
#include "llvm/MC/MCValue.h"
49+
#include "llvm/Support/Base64.h"
4950
#include "llvm/Support/Casting.h"
5051
#include "llvm/Support/CommandLine.h"
5152
#include "llvm/Support/ErrorHandling.h"
@@ -530,6 +531,7 @@ class AsmParser : public MCAsmParser {
530531
DK_LTO_SET_CONDITIONAL,
531532
DK_CFI_MTE_TAGGED_FRAME,
532533
DK_MEMTAG,
534+
DK_BASE64,
533535
DK_END
534536
};
535537

@@ -552,6 +554,7 @@ class AsmParser : public MCAsmParser {
552554

553555
// ".ascii", ".asciz", ".string"
554556
bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
557+
bool parseDirectiveBase64(); // ".base64"
555558
bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc"
556559
bool parseDirectiveValue(StringRef IDVal,
557560
unsigned Size); // ".byte", ".long", ...
@@ -1953,6 +1956,8 @@ bool AsmParser::parseStatement(ParseStatementInfo &Info,
19531956
case DK_ASCIZ:
19541957
case DK_STRING:
19551958
return parseDirectiveAscii(IDVal, true);
1959+
case DK_BASE64:
1960+
return parseDirectiveBase64();
19561961
case DK_BYTE:
19571962
case DK_DC_B:
19581963
return parseDirectiveValue(IDVal, 1);
@@ -3076,6 +3081,25 @@ bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
30763081
return parseMany(parseOp);
30773082
}
30783083

3084+
/// parseDirectiveBase64:
3085+
// ::= .base64 "string"
3086+
bool AsmParser::parseDirectiveBase64() {
3087+
if (checkForValidSection() ||
3088+
check(getTok().isNot(AsmToken::String), "expected string")) {
3089+
return true;
3090+
}
3091+
3092+
std::vector<char> Decoded;
3093+
std::string const str = getTok().getStringContents().str();
3094+
if (str.empty() || decodeBase64(str, Decoded)) {
3095+
return true;
3096+
}
3097+
3098+
getStreamer().emitBytes(std::string(Decoded.begin(), Decoded.end()));
3099+
Lex();
3100+
return false;
3101+
}
3102+
30793103
/// parseDirectiveReloc
30803104
/// ::= .reloc expression , identifier [ , expression ]
30813105
bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) {
@@ -5345,6 +5369,7 @@ void AsmParser::initializeDirectiveKindMap() {
53455369
DirectiveKindMap[".asciz"] = DK_ASCIZ;
53465370
DirectiveKindMap[".string"] = DK_STRING;
53475371
DirectiveKindMap[".byte"] = DK_BYTE;
5372+
DirectiveKindMap[".base64"] = DK_BASE64;
53485373
DirectiveKindMap[".short"] = DK_SHORT;
53495374
DirectiveKindMap[".value"] = DK_VALUE;
53505375
DirectiveKindMap[".2byte"] = DK_2BYTE;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# RUN: llvm-mc -triple i386-unknown-unknown %s | FileCheck %s
2+
3+
.data
4+
# CHECK: TEST0:
5+
# CHECK: .byte 0
6+
TEST0:
7+
.base64 "AA=="
8+
9+
# CHECK: TEST1:
10+
# CHECK: .ascii "abcxyz"
11+
TEST1:
12+
.base64 "YWJjeHl6"

0 commit comments

Comments
 (0)