Issue 22722 - ImportC: parser doesn’t understand `asm volatile` syntax
Summary: ImportC: parser doesn’t understand `asm volatile` syntax
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: ImportC
Depends on:
Blocks:
 
Reported: 2022-01-31 22:27 UTC by dave287091
Modified: 2023-03-22 23:58 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description dave287091 2022-01-31 22:27:31 UTC
<dispatch/dispatch.h> on macOS, which is included by <CoreFoundation/CoreFoundation.h>, has an inline function which has a macro defined:

#define dispatch_compiler_barrier()  __asm__ __volatile__("" ::: "memory”)

Where it is used, it is expanded to:

typedef void (*dispatch_function_t)(void *);
typedef intptr_t dispatch_once_t;

extern void dispatch_once_f(dispatch_once_t *predicate, void * context, dispatch_function_t function);

static inline /* several attributes omitted */
void
_dispatch_once_f(dispatch_once_t *predicate, void * context, dispatch_function_t function)                                                
{
 if (__builtin_expect((*predicate), (~0l)) != ~0l) {
  dispatch_once_f(predicate, context, function);                               
 } else {                                                                      
  asm volatile("" ::: "memory");                                               
 }
 __builtin_assume(*predicate == ~0l);
}

gcc docs on the use of volatile with asm: https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile
Comment 1 Walter Bright 2022-02-02 09:22:47 UTC
It does more than not accept the `volatile`, it also does not accept the `( ... )` construct. I'm not sure what to do about it. There are no plans to implement the gnu assembler syntax.
Comment 2 dave287091 2022-02-02 16:55:47 UTC
(In reply to Walter Bright from comment #1)
> It does more than not accept the `volatile`, it also does not accept the `(
> ... )` construct. I'm not sure what to do about it. There are no plans to
> implement the gnu assembler syntax.

It’s unfortunate as it is not even using the asm, it’s just to inhibit compiler optimizations. If __GNUC__ is not defined it defines it as do { } while(0).
Comment 3 Walter Bright 2022-09-23 08:52:51 UTC
> If __GNUC__ is not defined it defines it as do { } while(0)

__GNUC__ is for compiling with gcc. It's not a good idea for ImportC to pretend to be gcc.
Comment 4 dave287091 2022-09-26 19:45:36 UTC
(In reply to Walter Bright from comment #3)
> > If __GNUC__ is not defined it defines it as do { } while(0)
> 
> __GNUC__ is for compiling with gcc. It's not a good idea for ImportC to
> pretend to be gcc.

clang also defines __GNUC__. There are other ways extensions like gnu asm can make it into headers, like `#if __has_extension(gnu_asm)`
Comment 5 Dlang Bot 2023-02-16 17:45:58 UTC
@ibuclaw created dlang/dmd pull request #14890 "partial fix Issue 22722 - ImportC: parser doesn’t understand 'asm volatile' syntax" mentioning this issue:

- partial fix Issue 22722 - ImportC: parser doesn’t understand 'asm volatile' syntax

https://github.com/dlang/dmd/pull/14890
Comment 6 Dlang Bot 2023-03-20 20:37:21 UTC
dlang/dmd pull request #14890 "partial fix Issue 22722 - ImportC: parser doesn’t understand 'asm volatile' syntax" was merged into master:

- a7af3a05452ac5ad8c70996f1eae18f196228b9c by Iain Buclaw:
  partial fix Issue 22722 - ImportC: parser doesn’t understand 'asm volatile' syntax

https://github.com/dlang/dmd/pull/14890
Comment 7 dave287091 2023-03-22 23:58:28 UTC
This bug was resolved by https://github.com/dlang/dmd/pull/15011