Bug 21934 – importC: Support asm labels to specify the assembler name to use for a C symbol

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-05-20T13:09:14Z
Last change time
2021-05-21T01:36:26Z
Keywords
ImportC, pull
Assigned to
No Owner
Creator
Iain Buclaw

Comments

Comment #0 by ibuclaw — 2021-05-20T13:09:14Z
6.47.4 Controlling Names Used in Assembler Code You can specify the name to be used in the assembler code for a C function or variable by writing the asm (or __asm__) keyword after the declarator. It is up to you to make sure that the assembler names you choose do not conflict with any other assembler symbols, or reference registers. Assembler names for data: This sample shows how to specify the assembler name for data: int foo asm ("myfoo") = 2; This specifies that the name to be used for the variable foo in the assembler code should be ‘myfoo’ rather than the usual ‘_foo’. On systems where an underscore is normally prepended to the name of a C variable, this feature allows you to define names for the linker that do not start with an underscore. GCC does not support using this feature with a non-static local variable since such variables do not have assembler names. If you are trying to put the variable in a particular register, see Explicit Register Variables. Assembler names for functions: To specify the assembler name for functions, write a declaration for the function before its definition and put asm there, like this: int func (int x, int y) asm ("MYFUNC"); int func (int x, int y) { /* … */ This specifies that the name to be used for the function func in the assembler code should be MYFUNC.
Comment #1 by ibuclaw — 2021-05-20T13:11:45Z
None of these work: --- int sym1 asm("realsym1") = 1; int sym2 __asm("realsym2") = 2; int sym3 __asm__("realsym3") = 3; int fun1() asm("realfun1"); int fun1() { return 1; } int fun2() __asm("realfun2"); int fun2() { return 2; } int fun3() __asm__("realfun3"); int fun3() { return 3; }
Comment #2 by dlang-bot — 2021-05-20T18:27:00Z
@ibuclaw created dlang/dmd pull request #12546 "fix Issue 21934 - importC: Support asm labels to specify the assembler name to use for a C symbol" fixing this issue: - fix Issue 21934 - importC: Support asm labels to specify the assembler name to use for a C symbol https://github.com/dlang/dmd/pull/12546
Comment #3 by aminyahyaabadi74 — 2021-05-20T19:42:44Z
I wanted to add more examples that don't work with importC. These snippets are generated by gcc (mingw) Error: `=`, `;` or `,` expected ``` extern __inline__ __attribute__((__always_inline__, __gnu_inline__)) void __attribute__((__cdecl__)) __debugbreak(void) { __asm__ __volatile__("int {$}3" :); } ``` Error: `=`, `;` or `,` expected ``` errno_t __attribute__((__cdecl__)) _get_errno(int *_Value); ``` Error: `=`, `;` or `,` expected ``` typedef struct { long long __max_align_ll __attribute__((__aligned__(__alignof__(long long)))); long double __max_align_ld __attribute__((__aligned__(__alignof__(long double)))); # 427 "c:\\lib\\mingw\\tools\\install\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\10.2.0\\include\\stddef.h" 3 4 } max_align_t; # 33 "c:\\lib\\mingw\\tools\\install\\mingw64\\x86_64-w64-mingw32\\include\\stdint.h" 2 3 4 ``` Error: illegal combination of type specifiers ``` __extension__ typedef long long __time64_t; __extension__ typedef long long int64_t; __extension__ typedef unsigned long long uint64_t; __extension__ typedef long long int_least64_t; __extension__ typedef unsigned long long uint_least64_t; __extension__ typedef long long int_fast64_t; __extension__ typedef unsigned long long uint_fast64_t; ```
Comment #4 by ibuclaw — 2021-05-21T00:43:55Z
(In reply to aminya from comment #3) > I wanted to add more examples that don't work with importC. These snippets > are generated by gcc (mingw) > > Error: `=`, `;` or `,` expected > ``` > extern __inline__ __attribute__((__always_inline__, __gnu_inline__)) void > __attribute__((__cdecl__)) __debugbreak(void) { > __asm__ __volatile__("int {$}3" :); > } > ``` > Error: `=`, `;` or `,` expected Please raise a new issue for supporting `__inline__`. > ``` > errno_t __attribute__((__cdecl__)) _get_errno(int *_Value); > ``` > Error: `=`, `;` or `,` expected Dupicate of issue 21937. > ``` > typedef struct { > long long __max_align_ll __attribute__((__aligned__(__alignof__(long > long)))); > long double __max_align_ld > __attribute__((__aligned__(__alignof__(long double)))); > # 427 > "c:\\lib\\mingw\\tools\\install\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\10.2. > 0\\include\\stddef.h" 3 4 > } max_align_t; > # 33 > "c:\\lib\\mingw\\tools\\install\\mingw64\\x86_64-w64- > mingw32\\include\\stdint.h" 2 3 4 > ``` 1. Raise a new bug report for invalid error: --- struct Foo { int var; #1 "foo.c" 3 4 }; --- 2. Raise a new bug report for ICE: --- typedef struct { long var; } Foo; --- > > Error: illegal combination of type specifiers > ``` > __extension__ typedef long long __time64_t; > __extension__ typedef long long int64_t; > __extension__ typedef unsigned long long uint64_t; > __extension__ typedef long long int_least64_t; > __extension__ typedef unsigned long long uint_least64_t; > __extension__ typedef long long int_fast64_t; > __extension__ typedef unsigned long long uint_fast64_t; > ``` Raise a new bug report for supporting `__extension__`.
Comment #5 by dlang-bot — 2021-05-21T01:36:26Z
dlang/dmd pull request #12546 "fix Issue 21934 - importC: Support asm labels to specify the assembler name to use for a C symbol" was merged into master: - a4d8508e0bc3b7d24364bf0528f82b19da24bb89 by Iain Buclaw: fix Issue 21934 - importC: Support asm labels to specify the assembler name to use for a C symbol https://github.com/dlang/dmd/pull/12546