www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Porting makro and asm statement

reply Frank Benoit <frank_DELETE_ _DELETE_drealtime.com> writes:
I want to port rtai to D. But coming to user-space interrupts there is this
peace of code:

#define RTAI_SYS_VECTOR        0xf6

#define
#define __rtai_stringize(_s_)  __rtai_stringize0(_s_)
#define __rtai_trap_call(_t_)  _t_
#define __rtai_do_trap0(_t_)   __rtai_stringize(int $ _t_)
#define __rtai_do_trap(_t_)    __rtai_do_trap0(__rtai_trap_call(_t_))

#define RTAI_DO_TRAP(v, r, a1, a2)  do { __asm__ __volatile__
( __rtai_do_trap(v): : "a" (a1), "c" (a2), "d" (&r)); } while (0)

How can I write the RTAI_DO_TRAP in D?

Frank


-- 
D goes real-time: http://www.drealtime.com
Jan 12 2006
next sibling parent "Charles" <noone nowhere.com> writes:
Thats a nasty looking macro , it might be beneficial to make a simple test
program that calls RTAI_DO_TRAP , and run it through just the preprocesor
see whats actually getting called.  For gcc ( -E ) , for dmc ( -e ) .

And D supports inline asm also , http://www.digitalmars.com/d/iasm.html .

Great work on the wiki , I really hope you get far with this :D.

Charlie


"Frank Benoit" <frank_DELETE_ _DELETE_drealtime.com> wrote in message
news:dq5v0r$pdf$1 digitaldaemon.com...
 I want to port rtai to D. But coming to user-space interrupts there is
this
 peace of code:

 #define RTAI_SYS_VECTOR        0xf6

 #define
 #define __rtai_stringize(_s_)  __rtai_stringize0(_s_)
 #define __rtai_trap_call(_t_)  _t_
 #define __rtai_do_trap0(_t_)   __rtai_stringize(int $ _t_)
 #define __rtai_do_trap(_t_)    __rtai_do_trap0(__rtai_trap_call(_t_))

 #define RTAI_DO_TRAP(v, r, a1, a2)  do { __asm__ __volatile__
 ( __rtai_do_trap(v): : "a" (a1), "c" (a2), "d" (&r)); } while (0)

 How can I write the RTAI_DO_TRAP in D?

 Frank


 --
 D goes real-time: http://www.drealtime.com
Jan 12 2006
prev sibling next sibling parent Sean Kelly <sean f4.ca> writes:
Frank Benoit wrote:
 I want to port rtai to D. But coming to user-space interrupts there is this
 peace of code:
 
 #define RTAI_SYS_VECTOR        0xf6
 
 #define
 #define __rtai_stringize(_s_)  __rtai_stringize0(_s_)
 #define __rtai_trap_call(_t_)  _t_
 #define __rtai_do_trap0(_t_)   __rtai_stringize(int $ _t_)
 #define __rtai_do_trap(_t_)    __rtai_do_trap0(__rtai_trap_call(_t_))
 
 #define RTAI_DO_TRAP(v, r, a1, a2)  do { __asm__ __volatile__
 ( __rtai_do_trap(v): : "a" (a1), "c" (a2), "d" (&r)); } while (0)
 
 How can I write the RTAI_DO_TRAP in D?
Not easily. RTAI_DO_TRAP is itself a macro, so v, r, a1, and a2 may have different types in different portions of the code. The easiest thing may be to get your hands on a C preprocessor and run it on the full source, then translate each __asm__ block by hand. Sean
Jan 12 2006
prev sibling next sibling parent reply "Walter Bright" <newshound digitalmars.com> writes:
"Frank Benoit" <frank_DELETE_ _DELETE_drealtime.com> wrote in message 
news:dq5v0r$pdf$1 digitaldaemon.com...
I want to port rtai to D. But coming to user-space interrupts there is this
 peace of code:

 #define RTAI_SYS_VECTOR        0xf6

 #define
 #define __rtai_stringize(_s_)  __rtai_stringize0(_s_)
 #define __rtai_trap_call(_t_)  _t_
 #define __rtai_do_trap0(_t_)   __rtai_stringize(int $ _t_)
 #define __rtai_do_trap(_t_)    __rtai_do_trap0(__rtai_trap_call(_t_))

 #define RTAI_DO_TRAP(v, r, a1, a2)  do { __asm__ __volatile__
 ( __rtai_do_trap(v): : "a" (a1), "c" (a2), "d" (&r)); } while (0)

 How can I write the RTAI_DO_TRAP in D?
It's a bit hard to see what that macro is doing. But I suspect it can be done with a template: template RTAI_DO_TRAP(int v) { RTAI_DO_TRAP() { asm { int v; } } } void main() { RTAI_DO_TRAP!(23)(); }
Jan 12 2006
parent reply Frank Benoit <frank_DELETE_ _DELETE_drealtime.com> writes:
Oh, please; can you explain what is going on there. 
Why don't you need the other three parameters?
What sense makes a asm statement with a "int 23;"?
Why do I need a template?

Frank
Jan 12 2006
next sibling parent Frank Benoit <frank_DELETE_ _DELETE_drealtime.com> writes:
 What sense makes a asm statement with a "int 23;"?
Ok, int not for integer <-> interrupt
Jan 12 2006
prev sibling parent "Walter Bright" <newshound digitalmars.com> writes:
"Frank Benoit" <frank_DELETE_ _DELETE_drealtime.com> wrote in message 
news:dq62ic$sgb$1 digitaldaemon.com...
 Oh, please; can you explain what is going on there.
 Why don't you need the other three parameters?
The compiler figures out for itself which registers are read and written to.
 What sense makes a asm statement with a "int 23;"?
That's what the RTAI macro does with its argument, too.
 Why do I need a template?
The int instruction takes a literal operand, not a variable, so this is the only way to parameterize it.
Jan 12 2006
prev sibling parent reply Frank Benoit <frank_DELETE_ _DELETE_drealtime.com> writes:
I run it through gcc -E. But I don't understand the asm part. Can somebody
explain it to me?

static inline long long rtai_srq(int srq, unsigned long args)
{
 long long retval;
 do { __asm__ __volatile__ ( "int $ 0xf6": : "a" (srq), "c" (args),
"d" (&retval)); } while (0);
 return retval;
}
Jan 12 2006
parent reply Sean Kelly <sean f4.ca> writes:
Frank Benoit wrote:
 I run it through gcc -E. But I don't understand the asm part. Can somebody
 explain it to me?
 
 static inline long long rtai_srq(int srq, unsigned long args)
 {
  long long retval;
  do { __asm__ __volatile__ ( "int $ 0xf6": : "a" (srq), "c" (args),
 "d" (&retval)); } while (0);
  return retval;
 }
GCC uses a compiler-specific asm syntax. I suggest reading the GCC docs to make sense of it. Though perhaps someone else on this list has written inline ASM with GCC. Sean
Jan 12 2006
parent reply Frank Benoit <frank_DELETE_ _DELETE_drealtime.com> writes:
I found this solution. Can somebody correct me, if there is a mistake?
Perhaps there is a better syntax for getting the address of retval?

const uint RTAI_SYS_VECTOR = 0xf6;

long rtai_srq( int srq, uint args )
{       
  long retval;
  long* rptr = &retval;
  asm{
    // load registers with arguments
    mov srq,EAX;
    mov args,ECX;
    mov rptr,EDX;
    // initiate software interrupt
    int RTAI_SYS_VECTOR;
  }
  return retval;
}


Frank


--
D goes real-time: http://www.drealtime.com
Jan 12 2006
next sibling parent "Walter Bright" <newshound digitalmars.com> writes:
"Frank Benoit" <frank_DELETE_ _DELETE_drealtime.com> wrote in message 
news:dq667u$100d$1 digitaldaemon.com...
I found this solution. Can somebody correct me, if there is a mistake?
 Perhaps there is a better syntax for getting the address of retval?

 const uint RTAI_SYS_VECTOR = 0xf6;

 long rtai_srq( int srq, uint args )
 {
  long retval;
Should be int retval, not long. Longs in D are 64 bits.
  long* rptr = &retval;
  asm{
    // load registers with arguments
    mov srq,EAX;
For this and the next two instructions, the operands are reversed, should be: mov EAX, srq ;
    mov args,ECX;
    mov rptr,EDX;
Can do it like: lea EDX, retval[EBP] ;
    // initiate software interrupt
    int RTAI_SYS_VECTOR;
  }
  return retval;
 }
Jan 12 2006
prev sibling parent reply "Kris" <fu bar.com> writes:
There's possibly a number of things wrong here, so I'll try to get you 
started at least:

1) long in D is different than long in C. Did you intend to return a 64 bit 
value?

2) the mov statements are inverted. Intel asm places the destination on the 
left.

3) you can probably use a lea instruction to get the relevant address of 
retval.



"Frank Benoit" <frank_DELETE_ _DELETE_drealtime.com> wrote in message 
news:dq667u$100d$1 digitaldaemon.com...
I found this solution. Can somebody correct me, if there is a mistake?
 Perhaps there is a better syntax for getting the address of retval?

 const uint RTAI_SYS_VECTOR = 0xf6;

 long rtai_srq( int srq, uint args )
 {
  long retval;
  long* rptr = &retval;
  asm{
    // load registers with arguments
    mov srq,EAX;
    mov args,ECX;
    mov rptr,EDX;
    // initiate software interrupt
    int RTAI_SYS_VECTOR;
  }
  return retval;
 }


 Frank


 --
 D goes real-time: http://www.drealtime.com 
Jan 12 2006
parent Frank Benoit <frank_DELETE_ _DELETE_drealtime.com> writes:
Thanks for the replies. 
My "solution" wasn't really the optimum :))

Frank
Jan 12 2006