www.digitalmars.com         C & C++   DMDScript  

D.gnu - How to port this GCC assembly to gdc?

reply drug <drug2004 bk.ru> writes:
Hello!

I have the following assebly:
```C
__asm__ __volatile__ (
	"990: nop\n"                                                 \
	".pushsection .note.stapsdt,\"?\",\"note\"\n"                \
	".balign 4\n"                                                \
	".4byte 992f-991f, 994f-993f, 3\n"                           \
	"991: .asciz \"stapsdt\"\n"                                  \
	"992: .balign 4\n"                                           \
	"993: .8byte 990b\n"                                         \
	".8byte _.stapsdt.base\n"                                    \
	".8byte 0\n"                                                 \
	".asciz \"myapp\"\n"                                         \
	".asciz \"func_call\"\n"                                     \
	".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\"\n" \
	"994: .balign 4\n"                                           \
	".popsection\n"
	:: [_SDT_S1] "n" (4),
	   [_SDT_A1] "nor" ((a)),
	   [_SDT_S2] "n" (4),
	   [_SDT_A2] "nor" ((b))
);
```
this code compiles by gcc but gdc ((Debian 6.3.0-18+deb9u1) 6.3.0 
20170516) fails. It doesn't like __volatile__ keyword and :: syntax

```D
__asm__  (
	"990: nop"
	".pushsection .note.stapsdt,\"?\",\"note\""
	".balign 4"
	".4byte 992f-991f, 994f-993f, 3"
	"991: .asciz \"stapsdt\""
	"992: .balign 4"
	"993: .8byte 990b"
	".8byte _.stapsdt.base"
	".8byte 0"
	".asciz \"myapp\""
	".asciz \"func_call\""
	".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\""
	"994: .balign 4"
	".popsection\n"
	:: [_SDT_S1] "n" (4),
	   [_SDT_A1] "nor" ((a)),
	   [_SDT_S2] "n" (4),
	   [_SDT_A2] "nor" ((b))
);
```
Oct 24 2019
parent reply Iain Buclaw <ibuclaw gdcproject.org> writes:
On Thu, 24 Oct 2019 at 12:05, drug via D.gnu <d.gnu puremagic.com> wrote:
 Hello!

 I have the following assebly:
 ```C
 __asm__ __volatile__ (
         "990: nop\n"                                                 \
         ".pushsection .note.stapsdt,\"?\",\"note\"\n"                \
         ".balign 4\n"                                                \
         ".4byte 992f-991f, 994f-993f, 3\n"                           \
         "991: .asciz \"stapsdt\"\n"                                  \
         "992: .balign 4\n"                                           \
         "993: .8byte 990b\n"                                         \
         ".8byte _.stapsdt.base\n"                                    \
         ".8byte 0\n"                                                 \
         ".asciz \"myapp\"\n"                                         \
         ".asciz \"func_call\"\n"                                     \
         ".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\"\n" \
         "994: .balign 4\n"                                           \
         ".popsection\n"
         :: [_SDT_S1] "n" (4),
            [_SDT_A1] "nor" ((a)),
            [_SDT_S2] "n" (4),
            [_SDT_A2] "nor" ((b))
 );
 ```
 this code compiles by gcc but gdc ((Debian 6.3.0-18+deb9u1) 6.3.0
 20170516) fails. It doesn't like __volatile__ keyword and :: syntax

 ```D
 __asm__  (
         "990: nop"
         ".pushsection .note.stapsdt,\"?\",\"note\""
         ".balign 4"
         ".4byte 992f-991f, 994f-993f, 3"
         "991: .asciz \"stapsdt\""
         "992: .balign 4"
         "993: .8byte 990b"
         ".8byte _.stapsdt.base"
         ".8byte 0"
         ".asciz \"myapp\""
         ".asciz \"func_call\""
         ".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\""
         "994: .balign 4"
         ".popsection\n"
         :: [_SDT_S1] "n" (4),
            [_SDT_A1] "nor" ((a)),
            [_SDT_S2] "n" (4),
            [_SDT_A2] "nor" ((b))
 );
 ```
It looks like this is a top-level inline asslembler declaration? D doesn't support top-level inline assembler, however I think this would be a useful feature to allow. As for the syntax, it's `asm { ... }` -- Iain
Oct 24 2019
parent reply drug <drug2004 bk.ru> writes:
On 10/24/19 5:41 PM, Iain Buclaw wrote:
 
 It looks like this is a top-level inline asslembler declaration?  D
 doesn't support top-level inline assembler, however I think this would
 be a useful feature to allow.
 
 As for the syntax, it's `asm { ... }`
 
This is in fact DTrace probe generated by using ``` DTRACE_PROBE2(myapp, func_call, a, b); ``` I would like to generate this code by myself without C macros by obvious reason. I need ability to insert nop operation (that is trivial) and then add a note to specific section with the description of this probe (the address of this nop operation first of all and some other information) But as I understand it is impossible?
Oct 24 2019
parent reply Iain Buclaw <ibuclaw gdcproject.org> writes:
On Thu, 24 Oct 2019 at 16:50, drug via D.gnu <d.gnu puremagic.com> wrote:
 On 10/24/19 5:41 PM, Iain Buclaw wrote:
 It looks like this is a top-level inline asslembler declaration?  D
 doesn't support top-level inline assembler, however I think this would
 be a useful feature to allow.

 As for the syntax, it's `asm { ... }`
This is in fact DTrace probe generated by using ``` DTRACE_PROBE2(myapp, func_call, a, b); ``` I would like to generate this code by myself without C macros by obvious reason. I need ability to insert nop operation (that is trivial) and then add a note to specific section with the description of this probe (the address of this nop operation first of all and some other information) But as I understand it is impossible?
You can do the following: void somefunc() { asm { "990: nop" ~ ".pushsection .note.stapsdt,\"?\",\"note\"" ~ ".balign 4" ~ ".4byte 992f-991f, 994f-993f, 3" ~ "991: .asciz \"stapsdt\"" ~ "992: .balign 4" ~ "993: .8byte 990b" ~ ".8byte _.stapsdt.base" ~ ".8byte 0" ~ ".asciz \"myapp\"" ~ ".asciz \"func_call\"" ~ ".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\"" ~ "994: .balign 4" ~ ".popsection\n" :: [_SDT_S1] "n" (4), [_SDT_A1] "nor" ((a)), [_SDT_S2] "n" (4), [_SDT_A2] "nor" ((b)) } } The instruction string can be generated at CTFE. -- Iain
Oct 24 2019
parent reply drug <drug2004 bk.ru> writes:
On 10/24/19 8:01 PM, Iain Buclaw wrote:
 You can do the following:
 
 void somefunc()
 {
    asm {
          "990: nop" ~
          ".pushsection .note.stapsdt,\"?\",\"note\"" ~
          ".balign 4" ~
          ".4byte 992f-991f, 994f-993f, 3" ~
          "991: .asciz \"stapsdt\"" ~
          "992: .balign 4" ~
          "993: .8byte 990b" ~
          ".8byte _.stapsdt.base" ~
          ".8byte 0" ~
          ".asciz \"myapp\"" ~
          ".asciz \"func_call\"" ~
          ".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\"" ~
          "994: .balign 4" ~
          ".popsection\n"
          :: [_SDT_S1] "n" (4),
             [_SDT_A1] "nor" ((a)),
             [_SDT_S2] "n" (4),
             [_SDT_A2] "nor" ((b))
    } // <= line 26
 }
 
 The instruction string can be generated at CTFE.
 
Unfortunately I doesn't work https://godbolt.org/z/ly2yBT ``` source/app.d:26:3: error: expression expected, not '}' ```
Oct 24 2019
parent reply Iain Buclaw <ibuclaw gdcproject.org> writes:
On Thu, 24 Oct 2019 at 19:20, drug via D.gnu <d.gnu puremagic.com> wrote:
 On 10/24/19 8:01 PM, Iain Buclaw wrote:
 You can do the following:

 void somefunc()
 {
    asm {
          "990: nop" ~
          ".pushsection .note.stapsdt,\"?\",\"note\"" ~
          ".balign 4" ~
          ".4byte 992f-991f, 994f-993f, 3" ~
          "991: .asciz \"stapsdt\"" ~
          "992: .balign 4" ~
          "993: .8byte 990b" ~
          ".8byte _.stapsdt.base" ~
          ".8byte 0" ~
          ".asciz \"myapp\"" ~
          ".asciz \"func_call\"" ~
          ".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\"" ~
          "994: .balign 4" ~
          ".popsection\n"
          :: [_SDT_S1] "n" (4),
             [_SDT_A1] "nor" ((a)),
             [_SDT_S2] "n" (4),
             [_SDT_A2] "nor" ((b))
    } // <= line 26
 }

 The instruction string can be generated at CTFE.
Unfortunately I doesn't work https://godbolt.org/z/ly2yBT ``` source/app.d:26:3: error: expression expected, not '}' ```
That's correct, because I forgot the semicolon (the hint is in the latter message: found 'EOF' when expecting ';').
From what little information I'm going off on that macro, it seems
that you want it to do something like the following: https://explore.dgnu.org/z/TtTcij -- Iain
Oct 24 2019
parent drug <drug2004 bk.ru> writes:
24.10.2019 22:51, Iain Buclaw пишет:
 On Thu, 24 Oct 2019 at 19:20, drug via D.gnu <d.gnu puremagic.com> wrote:
 On 10/24/19 8:01 PM, Iain Buclaw wrote:
 You can do the following:

 void somefunc()
 {
     asm {
           "990: nop" ~
           ".pushsection .note.stapsdt,\"?\",\"note\"" ~
           ".balign 4" ~
           ".4byte 992f-991f, 994f-993f, 3" ~
           "991: .asciz \"stapsdt\"" ~
           "992: .balign 4" ~
           "993: .8byte 990b" ~
           ".8byte _.stapsdt.base" ~
           ".8byte 0" ~
           ".asciz \"myapp\"" ~
           ".asciz \"func_call\"" ~
           ".asciz \"%n[_SDT_S1] %[_SDT_A1] %n[_SDT_S2] %[_SDT_A2]\"" ~
           "994: .balign 4" ~
           ".popsection\n"
           :: [_SDT_S1] "n" (4),
              [_SDT_A1] "nor" ((a)),
              [_SDT_S2] "n" (4),
              [_SDT_A2] "nor" ((b))
     } // <= line 26
 }

 The instruction string can be generated at CTFE.
Unfortunately I doesn't work https://godbolt.org/z/ly2yBT ``` source/app.d:26:3: error: expression expected, not '}' ```
That's correct, because I forgot the semicolon (the hint is in the latter message: found 'EOF' when expecting ';').
From what little information I'm going off on that macro, it seems
that you want it to do something like the following: https://explore.dgnu.org/z/TtTcij
That's it! Thank you very much!
Oct 24 2019