www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - importC: __asm volatile parsing failure - issue or not?

reply Denis Feklushkin <feklushkin.denis gmail.com> writes:
In fact, this is my first time using importC, so maybe I messed 
something up

This is real piece of FreeRTOS header. It contains ARM assembler 
instruction and attended to use with GCC:
```
$ cat test.h
__attribute__( ( always_inline ) ) static inline uint8_t 
ucPortCountLeadingZeros( uint32_t ulBitmap )
{
     uint8_t ucReturn;

     __asm volatile ( "clz %0, %1" : "=r" ( ucReturn ) : "r" ( 
ulBitmap ) : "memory" );

     return ucReturn;
}
```
Preprocessing this C header:
```
$ arm-none-eabi-gcc -std=c11 -E test.h -o test_gcc.i
$ cat test_gcc.i




__attribute__( ( always_inline ) ) static inline uint8_t 
ucPortCountLeadingZeros( uint32_t ulBitmap )
{
     uint8_t ucReturn;

     __asm volatile ( "clz %0, %1" : "=r" ( ucReturn ) : "r" ( 
ulBitmap ) : "memory" );

     return ucReturn;
}
```

Trying to use it from D:
```
$ dmd test_gcc.i -Hf=test.di
test.h(5): Error: found `volatile` when expecting `;` following 
statement
test.h(5): Error: found `:` when expecting `)`
test.h(5): Error: found `"=r"` when expecting `;` following 
statement
test.h(5): Error: found `:` when expecting `;` following statement
test.h(5): Error: found `"r"` instead of statement

$ dmd --version
DMD64 D Compiler v2.107.1
Copyright (C) 1999-2024 by The D Language Foundation, All Rights 
Reserved written by Walter Bright
```
```
$ ldc2 --mtriple=thumbv7em-unknown-none-eabi test_gcc.i 
-Hf=test.di
test.h(5): Error: found `volatile` when expecting `;` following 
statement
test.h(5): Error: found `:` when expecting `)`
test.h(5): Error: found `"=r"` when expecting `;` following 
statement
test.h(5): Error: found `:` when expecting `;` following statement
test.h(5): Error: found `"r"` instead of statement

$ ldc2 --version
LDC - the LLVM D compiler (1.37.0):
   based on DMD v2.107.1 and LLVM 17.0.6
   built with LDC - the LLVM D compiler (1.37.0)
```

Something went wrong with "__asm volatile" syntax?
Mar 31
parent reply Denis Feklushkin <feklushkin.denis gmail.com> writes:
On Sunday, 31 March 2024 at 08:42:55 UTC, Denis Feklushkin wrote:
 ```
     __asm volatile ( "clz %0, %1" : "=r" ( ucReturn ) : "r" ( 
 ```
Tried to replace (non-standard?) __asm by \_\_asm\_\_ - nothing changed
Mar 31
parent reply Denis Feklushkin <feklushkin.denis gmail.com> writes:
On Sunday, 31 March 2024 at 09:35:01 UTC, Denis Feklushkin wrote:
 On Sunday, 31 March 2024 at 08:42:55 UTC, Denis Feklushkin 
 wrote:
 ```
     __asm volatile ( "clz %0, %1" : "=r" ( ucReturn ) : "r" ( 
 ```
Tried to replace (non-standard?) __asm by \_\_asm\_\_ - nothing changed
"asm volatile" works as expected, but "asm" isn't C11 keyword
Mar 31
parent reply Walter Bright <newshound2 digitalmars.com> writes:
On 3/31/2024 2:38 AM, Denis Feklushkin wrote:
 "asm volatile" works as expected, but "asm" isn't C11 keyword
"asm" is a C11 keyword, see C11.j.5.10 dmd doesn't support the gcc asm syntax, only the Microsoft & Digital Mars syntaxes. Usually, in header files with asm syntax, it can be disabled with an #ifdef.
Mar 31
parent reply Denis Feklushkin <feklushkin.denis gmail.com> writes:
On Monday, 1 April 2024 at 00:18:15 UTC, Walter Bright wrote:
 On 3/31/2024 2:38 AM, Denis Feklushkin wrote:
 "asm volatile" works as expected, but "asm" isn't C11 keyword
"asm" is a C11 keyword, see C11.j.5.10
Really! I just got confused Thank you!
 dmd doesn't support the gcc asm syntax, only the Microsoft & 
 Digital Mars syntaxes.

 Usually, in header files with asm syntax, it can be disabled 
 with an #ifdef.
This is just the opposite case: FreeRTOS code is full of asm entries which isn't be able to disabled. But, preliminarily, a simple mass replacement to "asm volatile" works for me
Apr 01
parent Denis Feklushkin <feklushkin.denis gmail.com> writes:
On Monday, 1 April 2024 at 18:02:33 UTC, Denis Feklushkin wrote:
 On Monday, 1 April 2024 at 00:18:15 UTC, Walter Bright wrote:
 On 3/31/2024 2:38 AM, Denis Feklushkin wrote:
 "asm volatile" works as expected, but "asm" isn't C11 keyword
"asm" is a C11 keyword, see C11.j.5.10
Really! I just got confused Thank you!
 dmd doesn't support the gcc asm syntax, only the Microsoft & 
 Digital Mars syntaxes.

 Usually, in header files with asm syntax, it can be disabled 
 with an #ifdef.
This is just the opposite case: FreeRTOS code is full of asm entries which isn't be able to disabled. But, preliminarily, a simple mass replacement to "asm volatile" works for me
Fixed in this PR: https://github.com/dlang/dmd/pull/16386 Please review!
Apr 17