Skip to content

Commit 091dfad

Browse files
author
git apple-llvm automerger
committed
Merge commit 'a704e66eb049' from apple/main into swift/next
2 parents 64d0661 + a704e66 commit 091dfad

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

llvm/include/llvm/MC/MCParser/MCAsmLexer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class MCAsmLexer {
4949
bool SkipSpace = true;
5050
bool AllowAtInIdentifier;
5151
bool IsAtStartOfStatement = true;
52+
bool LexMasmHexFloats = false;
5253
bool LexMasmIntegers = false;
5354
bool UseMasmDefaultRadix = false;
5455
unsigned DefaultRadix = 10;
@@ -159,6 +160,9 @@ class MCAsmLexer {
159160

160161
unsigned getMasmDefaultRadix() const { return DefaultRadix; }
161162
void setMasmDefaultRadix(unsigned Radix) { DefaultRadix = Radix; }
163+
164+
/// Set whether to lex masm-style hex float literals, such as 3f800000r.
165+
void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; }
162166
};
163167

164168
} // end namespace llvm

llvm/lib/MC/MCParser/AsmLexer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ AsmToken AsmLexer::LexDigit() {
350350
return LexFloatLiteral();
351351
}
352352

353+
if (LexMasmHexFloats && (*CurPtr == 'r' || *CurPtr == 'R')) {
354+
++CurPtr;
355+
return AsmToken(AsmToken::Real, StringRef(TokStart, CurPtr - TokStart));
356+
}
357+
353358
unsigned Radix = 0;
354359
if (*CurPtr == 'h' || *CurPtr == 'H') {
355360
// hexadecimal number

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,10 +3425,13 @@ bool MasmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
34253425
// We don't truly support arithmetic on floating point expressions, so we
34263426
// have to manually parse unary prefixes.
34273427
bool IsNeg = false;
3428+
SMLoc SignLoc;
34283429
if (getLexer().is(AsmToken::Minus)) {
3430+
SignLoc = getLexer().getLoc();
34293431
Lexer.Lex();
34303432
IsNeg = true;
34313433
} else if (getLexer().is(AsmToken::Plus)) {
3434+
SignLoc = getLexer().getLoc();
34323435
Lexer.Lex();
34333436
}
34343437

@@ -3450,6 +3453,20 @@ bool MasmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
34503453
Value = APFloat::getZero(Semantics);
34513454
else
34523455
return TokError("invalid floating point literal");
3456+
} else if (IDVal.consume_back("r") || IDVal.consume_back("R")) {
3457+
// MASM hexadecimal floating-point literal; no APFloat conversion needed.
3458+
// To match ML64.exe, ignore the initial sign.
3459+
unsigned Size = Value.getSizeInBits(Semantics);
3460+
if (Size != (IDVal.size() << 2))
3461+
return TokError("invalid floating point literal");
3462+
3463+
// Consume the numeric token.
3464+
Lex();
3465+
3466+
Res = APInt(Size, IDVal, 16);
3467+
if (SignLoc.isValid())
3468+
return Warning(SignLoc, "MASM-style hex floats ignore explicit sign");
3469+
return false;
34533470
} else if (errorToBool(
34543471
Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
34553472
.takeError())) {

llvm/test/tools/llvm-ml/builtin_types.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ t6_double REAL8 1.3
7272
; CHECK-LABEL: t6_double:
7373
; CHECK-NEXT: .quad 4608533498688228557
7474

75+
t7_single_hex REAL4 3f800000r
76+
t7_double_hex REAL8 3FF0000000000000R
77+
78+
; CHECK-LABEL: t7_single_hex:
79+
; CHECK-NEXT: .long 1065353216
80+
; CHECK-LABEL: t7_double_hex:
81+
; CHECK-NEXT: .quad 4607182418800017408
82+
7583
.code
7684

7785
END

llvm/tools/llvm-mc/llvm-mc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ static cl::opt<bool> LexMasmIntegers(
169169
"masm-integers",
170170
cl::desc("Enable binary and hex masm integers (0b110 and 0ABCh)"));
171171

172+
static cl::opt<bool> LexMasmHexFloats(
173+
"masm-hexfloats",
174+
cl::desc("Enable MASM-style hex float initializers (3F800000r)"));
175+
172176
static cl::opt<bool> NoExecStack("no-exec-stack",
173177
cl::desc("File doesn't need an exec stack"));
174178

@@ -300,6 +304,7 @@ static int AssembleInput(const char *ProgName, const Target *TheTarget,
300304
Parser->setShowParsedOperands(ShowInstOperands);
301305
Parser->setTargetParser(*TAP);
302306
Parser->getLexer().setLexMasmIntegers(LexMasmIntegers);
307+
Parser->getLexer().setLexMasmHexFloats(LexMasmHexFloats);
303308

304309
int Res = Parser->Run(NoInitialTextSection);
305310

llvm/tools/llvm-ml/llvm-ml.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI, raw_ostream &OS) {
177177
Lexer.setBuffer(SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer());
178178
Lexer.setLexMasmIntegers(true);
179179
Lexer.useMasmDefaultRadix(true);
180+
Lexer.setLexMasmHexFloats(true);
180181

181182
bool Error = false;
182183
while (Lexer.Lex().isNot(AsmToken::Eof)) {
@@ -208,6 +209,7 @@ static int AssembleInput(const char *ProgName, const Target *TheTarget,
208209
Parser->setTargetParser(*TAP);
209210
Parser->getLexer().setLexMasmIntegers(true);
210211
Parser->getLexer().useMasmDefaultRadix(true);
212+
Parser->getLexer().setLexMasmHexFloats(true);
211213

212214
int Res = Parser->Run(/*NoInitialTextSection=*/true);
213215

0 commit comments

Comments
 (0)