Skip to content

Support case-insensitive DMD-style inline assembly. #4629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion gen/asm-x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "dmd/mangle.h"
#include "gen/llvmhelpers.h" // printLabelName
#include <cctype>
#include "llvm/ADT/StringRef.h"

#ifndef ASM_X86_64
namespace AsmParserx8632 {
Expand Down Expand Up @@ -3703,7 +3704,15 @@ struct AsmProcessor {
// check for reg first then dotexp is an error?
if (e->op == EXP::identifier) {
for (int i = 0; i < N_Regs; i++) {
if (ident == regInfo[i].ident) {
const auto reg = regInfo[i].ident;
const auto matchesRegister = stmt->caseSensitive ?
ident == reg :
#if LDC_LLVM_VER >= 1300
reg && llvm::StringRef(ident->toChars()).equals_insensitive(reg->toChars());
#else
reg && llvm::StringRef(ident->toChars()).equals_lower(reg->toChars());
#endif
if (matchesRegister) {
if (static_cast<Reg>(i) == Reg_ST &&
token->value == TOK::leftParenthesis) {
nextToken();
Expand Down
3 changes: 3 additions & 0 deletions gen/asmstmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ Statement *asmSemantic(AsmStatement *s, Scope *sc) {
// this is DMD-style asm
sc->func->hasReturnExp |= 32;

const auto caseSensitive = s->caseSensitive;

auto ias = createInlineAsmStatement(s->loc, s->tokens);
s = ias;
s->caseSensitive = caseSensitive;

bool err = false;
llvm::Triple const &t = *global.params.targetTriple;
Expand Down
13 changes: 13 additions & 0 deletions tests/compilable/c_masm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// DMD supports case-insensitive register operands for x86 assembly in C files, when targeting Windows.
// Additionally, a new-line can be used to terminate an instruction.

// REQUIRES: Windows && target_X86
// RUN: %ldc -mtriple=i686-pc-windows-msvc -c %s

unsigned int subtract(unsigned int a, unsigned int b) {
__asm {
mov eax, dword ptr [a]
mov eDX, dword ptr [b]
sub Eax, edx
}
}
Loading