Skip to contentSkip to conversation

In line ASM magic? What is this?

DISCUSSION George Anzinger 2 participants 3 messages Nov 15, 2000

lore
Share & tools
lore
#0 lore raw

I am trying to understand what is going on in the following code. The reference for %2, i.e. "m"(*__xg(ptr)) seems like magic (from .../include/i386/system.h). At the same time, the code "m" (*mem) from the second __asm__ below (my code) seems to generate the required asm code. Before I go with the simple version, could someone tell me why? Inquiring minds want to know.

struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x) ((struct __xchg_dummy *)(x))

		__asm__ __volatile__(LOCK_PREFIX "cmpxchgl %b1,%2"
				     : "=a"(prev)
				     : "q"(new), "m"(*__xg(ptr)), "0"(old)
				     : "memory");


	__asm__ __volatile__(
                             LOCK "cmpxchgl %1,%2\n\t"
                             :"=a" (result)
                             :"r" (new),
                              "m" (*mem),
                              "a0" (test)
                             : "memory");

George
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/

#1 lore raw

** Reply to message from George Anzinger <[email protected]> on Wed, 15 Nov 2000 12:55:46 -0800

Quoted 10 lines

I am trying to understand what is going on in the following code. The reference for %2, i.e. "m"(*__xg(ptr)) seems like magic (from .../include/i386/system.h). At the same time, the code "m" (*mem) from the second __asm__ below (my code) seems to generate the required asm code. Before I go with the simple version, could someone tell me why? Inquiring minds want to know.

struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x) ((struct __xchg_dummy *)(x))

Quoted 13 lines
		__asm__ __volatile__(LOCK_PREFIX "cmpxchgl %b1,%2"
				     : "=a"(prev)
				     : "q"(new), "m"(*__xg(ptr)), "0"(old)
				     : "memory");


	__asm__ __volatile__(
                             LOCK "cmpxchgl %1,%2\n\t"
                             :"=a" (result)
                             :"r" (new),
                              "m" (*mem),
                              "a0" (test)
                             : "memory");

I've been a lot of gcc inline asm recently, and I still consider it a black art. There are times when I just throw in what I think makes sense, and then look at the code the compiler generated. If it's wrong, I try something else.

Both versions look correct to me. The "m" simply tells the compiler that __xg(ptr) is a memory location, and the contents of that memory location should NOT be copied to a register. The confusion occurs because its unintuitive that the "*" is required. Otherwise, it would have been "r", which basically tells the compiler to copy the contents to a register first.

Signature · 8 lines
Timur Tabi - [email protected]
Interactive Silicon - http://www.interactivesi.com

When replying to a mailing-list message, please direct the reply to the mailing list only.  Don't send another copy to me.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/
#2 lore raw
Quoted 32 lines · Timur Tabi

** Reply to message from George Anzinger <[email protected]> on Wed, 15 Nov 2000 12:55:46 -0800

Quoted 10 lines

I am trying to understand what is going on in the following code. The reference for %2, i.e. "m"(*__xg(ptr)) seems like magic (from .../include/i386/system.h). At the same time, the code "m" (*mem) from the second __asm__ below (my code) seems to generate the required asm code. Before I go with the simple version, could someone tell me why? Inquiring minds want to know.

struct __xchg_dummy { unsigned long a[100]; };
#define __xg(x) ((struct __xchg_dummy *)(x))

Quoted 13 lines
              __asm__ __volatile__(LOCK_PREFIX "cmpxchgl %b1,%2"
                                   : "=a"(prev)
                                   : "q"(new), "m"(*__xg(ptr)), "0"(old)
                                   : "memory");


      __asm__ __volatile__(
                             LOCK "cmpxchgl %1,%2\n\t"
                             :"=a" (result)
                             :"r" (new),
                              "m" (*mem),
                              "a0" (test)
                             : "memory");

I've been a lot of gcc inline asm recently, and I still consider it a black art. There are times when I just throw in what I think makes sense, and then look at the code the compiler generated. If it's wrong, I try something else.

Quoted 6 lines

Both versions look correct to me. The "m" simply tells the compiler that __xg(ptr) is a memory location, and the contents of that memory location should NOT be copied to a register. The confusion occurs because its unintuitive that the "*" is required. Otherwise, it would have been "r", which basically tells the compiler to copy the contents to a register first.

I know the feeling. I am currently strugling with "inconsistant constraints". Still, I must assume that form 1 was used instead of 2 for some reason....

George
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
Please read the FAQ at http://www.tux.org/lkml/