Skip to content

Commit 4cff23d

Browse files
authored
Support case-insensitive DMD-style inline assembly. (#4629)
1 parent b29edc8 commit 4cff23d

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

gen/asm-x86.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "dmd/mangle.h"
2626
#include "gen/llvmhelpers.h" // printLabelName
2727
#include <cctype>
28+
#include "llvm/ADT/StringRef.h"
2829

2930
#ifndef ASM_X86_64
3031
namespace AsmParserx8632 {
@@ -3703,7 +3704,15 @@ struct AsmProcessor {
37033704
// check for reg first then dotexp is an error?
37043705
if (e->op == EXP::identifier) {
37053706
for (int i = 0; i < N_Regs; i++) {
3706-
if (ident == regInfo[i].ident) {
3707+
const auto reg = regInfo[i].ident;
3708+
const auto matchesRegister = stmt->caseSensitive ?
3709+
ident == reg :
3710+
#if LDC_LLVM_VER >= 1300
3711+
reg && llvm::StringRef(ident->toChars()).equals_insensitive(reg->toChars());
3712+
#else
3713+
reg && llvm::StringRef(ident->toChars()).equals_lower(reg->toChars());
3714+
#endif
3715+
if (matchesRegister) {
37073716
if (static_cast<Reg>(i) == Reg_ST &&
37083717
token->value == TOK::leftParenthesis) {
37093718
nextToken();

gen/asmstmt.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ Statement *asmSemantic(AsmStatement *s, Scope *sc) {
114114
// this is DMD-style asm
115115
sc->func->hasReturnExp |= 32;
116116

117+
const auto caseSensitive = s->caseSensitive;
118+
117119
auto ias = createInlineAsmStatement(s->loc, s->tokens);
118120
s = ias;
121+
s->caseSensitive = caseSensitive;
119122

120123
bool err = false;
121124
llvm::Triple const &t = *global.params.targetTriple;

tests/compilable/c_masm.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// DMD supports case-insensitive register operands for x86 assembly in C files, when targeting Windows.
2+
// Additionally, a new-line can be used to terminate an instruction.
3+
4+
// REQUIRES: Windows && target_X86
5+
// RUN: %ldc -mtriple=i686-pc-windows-msvc -c %s
6+
7+
unsigned int subtract(unsigned int a, unsigned int b) {
8+
__asm {
9+
mov eax, dword ptr [a]
10+
mov eDX, dword ptr [b]
11+
sub Eax, edx
12+
}
13+
}

0 commit comments

Comments
 (0)