Skip to content
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
5 changes: 4 additions & 1 deletion gen/asm-gcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ std::string translateTemplate(GccAsmStatement *stmt) {
result += "$$"; // escape for LLVM: $ => $$
break;
case '%':
if (i < N - 1 && insn[i + 1] == '%') { // unescape for LLVM: %% => %
if (i < N - 1 && insn[i + 1] == '=') { // unique ID: %= => ${:uid}
result += "${:uid}";
++i;
} else if (i < N - 1 && insn[i + 1] == '%') { // unescape for LLVM: %% => %
result += '%';
++i;
} else {
Expand Down
35 changes: 35 additions & 0 deletions tests/codegen/asm_gcc_unique_id.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Tests that GCC-style asm %= (unique ID) is correctly translated to LLVM ${:uid}
// which generates unique labels for each asm statement.
// See: https://github.com/ldc-developers/ldc/issues/4294

// REQUIRES: target_X86

// RUN: %ldc -mtriple=x86_64-linux-gnu -output-s -of=%t.s %s
// RUN: FileCheck %s < %t.s

// Two functions with identical asm that use %= should get different label numbers.
// The ${:uid} mechanism in LLVM generates sequential numbers (0, 1, 2, ...).

int func1() {
int result;
// CHECK-LABEL: func1
// CHECK: loop_[[ID1:[0-9]+]]:
// CHECK: jmp loop_[[ID1]]
asm {
"xorl %0, %0\n\tloop_%=:\n\tincl %0\n\tjmp loop_%="
: "=r" (result);
}
return result;
}

int func2() {
int result;
// CHECK-LABEL: func2
// CHECK: loop_[[ID2:[0-9]+]]:
// CHECK: jmp loop_[[ID2]]
asm {
"xorl %0, %0\n\tloop_%=:\n\tincl %0\n\tjmp loop_%="
: "=r" (result);
}
return result;
}