This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Postioning of items within a class
- To: gcc at gcc dot gnu dot org
- Subject: Postioning of items within a class
- From: Andrew Henderson <prometheus at lokigames dot com>
- Date: Wed, 12 Jul 2000 12:05:39 -0400
- CC: prometheus at lokigames dot com
I've run into a snag while porting some MASM-based inline assembly over
to inlined GAS assembly and I was hoping for some helpful suggestions.
Say you have the following struct:
struct dummy {
int item1;
int item2;
int item3;
};
A pointer to a struct of the "dummy" type would point at item1. Now,
assuming a 4-byte int and 4-byte memory alignment, the (address of the
pointer + 0x4) would give you item2. Likewise, the (address of the
pointer + 0x8) would give you item3.
MASM will replace "dummy.item1" with 0x0, "dummy.item2" with 0x4 and
"dummy.item3" with 0x8 if you use those references within your assembly.
So, you can easily set up offsets to get at items within a data
structure if you only have a pointer to the struct. Here is an example
of moving the 32-bit value held in EAX into my_pointer->item2:
MASM: mov dword ptr [my_pointer + dummy.item2], eax
GAS: movl %eax, 4(my_pointer)
Dealing with structures like this is a pain, but is not that much of a
problem. I just have to go through the paces of calculating the offsets
of each item within the structure. The snag that I am hitting is that
the MASM assembly is using classes in the same manner. So, a class like:
class DummyClass
{
public:
void method1(void);
void method2(void);
int item1;
int item2;
int item3;
};
... would be used in MASM assembly like so (with my_pointer being a
pointer to a DummyClass object) when referencing "item3" of the class:
MASM: mov dword ptr [my_pointer + DummyClass.item3], eax
Without a lot of trial and error, I don't think I'll be able to
calculate the needed offsets for the members of the class. What I need
to know is:
1. Is there a method by which GCC/GAS can do a similar type of
substitution for offsets of items within classes (like MASM does in the
example above)?
2. Failing that, is there a GCC way of querying the class and getting
the offsets of members within the class (perhaps an __attribute__ of
some sort)? I'd hate to have to rework the asm whenever a header gets
changed.
3. Finally, is there a particular structured order for how members and
methods within a class are stored in memory? For instance, are all
members held in memory, followed by methods? Or are all items within the
class simply placed in memory in the same order that they are defined in
the class prototype?
Thanks to everyone for any help received on this puzzler.
--
Andrew Henderson
Programmer
Loki Entertainment Software