🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

mingw + asOBJ_IS_COMPLEX: portability issues

Started by
4 comments, last by WitchLord 19 years, 11 months ago
Branching from my other thread on the offsetof() macro: http://www.gamedev.net/community/forums/topic.asp?topic_id=260145 I have the following class:

class CComplexClass
{
public:
	CComplexClass() { a = 0xDEADC0DE; }
	
	unsigned long a;
};

Since the class has a constructor MSVC will return objects of this type by reference by having the caller allocate memory for it and pass the pointer to the memory in a hidden address. MSVC will do this for all classes that have either a constructor, a destructor or an assignment operator. My problem is that this is not true for MinGW. For this example it seems that MinGW returns the object normally in the register EAX. I write in AngelScript's manual that you should use the flag asOBJ_IS_COMPLEX when registering a class that has a constructor, destructor, or assignment operator. AngelScript is supposed to use this flag to identify the correct method that the host application uses to return objects with that type. I need help in defining how MinGW returns objects on the x86 processor. If you don't know exactly how it is done already, then perhaps you can tell me how I can debug the MinGW code or disassemble it.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
I did not ever need to deal with things like that, but I believe this document: GNU Compiler Collection Internals will help you. It describes the behaviour of all GCC compilers, so MinGW falls under this category too. Chapters 10.10.8 and 10.10.9 will be the most relevant, probably.

Hope that helps.

[EDIT]

Hm, just found that there are flags in GCC to take care of returning structs and classes. They -fpcc-struct-return and -freg-struct-return. The second should objects that fit in registers throguh the registers and the big ones through memory. The first one forces all struct returns to be done through memory. The default setting for MinGW is -freg, for Cygwin: -fpcc, I don't know about the others GCC versions. You can just say the user that if he has set the -fpcc flag, he should register all objects as asOBJECT_IS_COMPLEX.

Constructors, destructors and assignment operators have no part in that: it's just the size of the object that matters.

That is the result of some research done through Google, you should probably check if it's all 100% right...

[Edited by - krajzega on July 28, 2004 3:11:45 PM]
That document didn't really tell me much. It is more about what options there are in configuring the compiler, it doesn't tell me how it works on x86.

I found another document here: http://www.ictp.trieste.it/texi/gpp/gpp_53.html

This one says how it is supposed to work. Though I'm not so sure it really does work like that. I'll have to do some more tests.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Quote:
Hm, just found that there are flags in GCC to take care of returning structs and classes. They -fpcc-struct-return and -freg-struct-return. The second should objects that fit in registers throguh the registers and the big ones through memory. The first one forces all struct returns to be done through memory. The default setting for MinGW is -freg, for Cygwin: -fpcc, I don't know about the others GCC versions. You can just say the user that if he has set the -fpcc flag, he should register all objects as asOBJECT_IS_COMPLEX.

Constructors, destructors and assignment operators have no part in that: it's just the size of the object that matters.


I'll do some more investigations.

I think you are correct in what you said. My tests seems to support that. Though it also seems that MinGW doesn't use EAX:EDX like MSVC6 does. When I tried returning an 8 bytes object I only received half of it. When trying to return a 12 bytes object the program crashed with a memory access violation, which would suggest that MinGW doesn't work the same way as MSVC6 when returning objects in memory either. Perhaps it passes the hidden address as the last parameter, or in a register.

I need to somehow discover these things so that I can make AngelScript portable.

The programmer shouldn't have to change the way he writes his application depending on how the compiler is configured. All this should be hidden inside the library code, where the as_config.h file would have to be set up correctly to take care of the calling convention differences.

Note, AngelScript is not quite there yet, but I'm slowly moving all compiler differences into the as_config.h file.

Does anyone know of a visual debugger with disassembler that works with MinGW? In MSVC6 I can easily verify how it compiles function calls, but I'm not sure how I should go about verifying this with MinGW.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

About MinGW debuggers/disassemblers, I know of two. One, GDB, is the standard gnu debugger and is shipped with your MinGW in the bin directory. There are tutorials on the net on how to use GDB, it can be a pain if you want to do something more than a simple run and a backtrace... And you certainly do. It has disassemble features and everything a debugger needs, but the command line interface needs a lot of getting-used-to.

There is also "Dr. MinGW", this is a JIT (Just-In-Time) Windows debugger. I haven't had many experiences with it, but I have heard only good about it. It has visual interface, but I'm not sure if it does everything you need. Just Google for it and check if it has the needed features on its website.
Thanks for the help, krajzega. GDB was good enough for me. With the manual open it was quite easy to use.

Firstly there was a bug that made the AngelScript library misshandle objects with 2 DWORDs in size. To fix this the line 963 in as_scriptengine.cpp has to be changed to:

{  systemFunctions[n]->hostReturnMethod = asCALL_RETURNBYVAL;  systemFunctions[n]->hostReturnSize = systemFunctions[n]->returnType.GetSizeOnStackDWords();}


The second problem is when the object is larger than 2 DWORDs and returned in memory. MinGW and MSVC treats these situations slightly differently. Both pass a hidden pointer as the first parameter, but with MinGW the callee pops this pointer before returning while MSVC leaves it for the caller.

The third problem is that MinGW doesn't treat classes that have constructors or destructors differently than any other. I was actually prepared for this one already, although I didn't know what the difference was.

The first problem have been fixed with the above code change. The other two I'll fix in the next few days (I hope).

I'm pretty sure that MinGW work the same way as any other g++ based compiler so these changes will probably work for g++ on Linux and DJGPP on Windows as well.

I think I'll write an article on differences in calling conventions. It would be interesting if I could collect information about all types of calling conventions in one place.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement