Advertisement

Marmalade: Angelscript runs fine on x86, crashes on ARM

Started by September 24, 2012 04:45 PM
34 comments, last by WitchLord 11 years, 11 months ago
Hello,

I am making a mobile game engine that runs on TinyXML and Angelscript. I'm using MSVC 2010 Professional. I used the Marmalade Angelscript project found in this thread and it seems to be working. So far, I've made it so I can define hierarchical game objects and their properties in XML, and optionally reference Angelscript scripts that specialize the functionality even further. Angelscript looks very promising, and while there've been a few hiccups I couldn't find reference to in the documentation, the library seems to be designed sensibly enough that with enough persistence I can usually figure out what's wrong.

Now, however, I'm completely stuck. I suspect the issue may lie with Marmalade as much as Angelscript. The engine runs flawlessly when I compile to x86 and run it in the Marmalade simulator. However, when I want to compile to ARM so I can run on an iOS device, something bad happens and it crashes. I stepped through in the ARM simulator and found the game was blowing up whenever I tried to register a function for Angelscript. Upon further inspection, I saw the AS_MAX_PORTABILITY #define was getting set, making it so I can only use the "generic" calling convention. I haven't written a lot of cross-platform code so I'm a newbie to thinking about different calling conventions, but from searching it looks to me as if ARM is supposed to be able to use CDECL function calls just fine.

Using #pragma messages I figured out where AS_MAX_PORTABILITY was getting defined. It's here in as_config.h:

#if (!defined(AS_X86) && !defined(AS_SH4) && !defined(AS_MIPS) && !defined(AS_PPC) && !defined(AS_PPC_64) && !defined(AS_XENON) && !defined(AS_X64_GCC) && !defined(AS_X64_MSVC) && !defined(AS_ARM) && !defined(AS_X64_MINGW))
#ifndef AS_MAX_PORTABILITY
#define AS_MAX_PORTABILITY
#endif
#endif


AS_MARMALADE is defined, but I realized that AS_ARM should probably also be defined, so I tried setting that in the project properties. Unfortunately when I do that, along with some probably-harmless warnings about ignoring stdcall, I get compiler errors:


1> Debug_AngelScript_new_vc10_gcc_arm/as_callfunc_arm.obj: In function `CallSystemFunctionNative(asCContext*, asCScriptFunction*, void*, unsigned long*, void*, unsigned long long&)':
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(126) : undefined reference to `armFuncR0'
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(130) : undefined reference to `armFunc'
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(134) : undefined reference to `armFuncR0'
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(142) : undefined reference to `armFuncR0R1'
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(147) : undefined reference to `armFuncR0'
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(155) : undefined reference to `armFuncR0R1'
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(159) : undefined reference to `armFuncObjLast'
1>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(162) : undefined reference to `armFuncR0ObjLast'


This doesn't make any sense, since the extern function definitions appear to be right there at the top of the document. I get that they reference assembler routines, but if anything went wrong there I would expect a linker error not a compiler error. I've fiddled around with as_config.h a little more but can't find the solution.

I feel like I'm stuck, since going any further would require fiddling around with the Angelscript source, in a place where I don't really fully understand how it works. Are there any ideas of what I can do to get Angelscript to run correctly when built for ARM? I am doing this game as part of a small sponsorship deal so I am on a really tight schedule. Worst case scenario, I can kludge this milestone by "pretending" Angelscript is being used; however it looks as though Andreas Jönsson peruses these forums somewhat frequently so I thought posting would be a good idea. I feel a little guilty asking for quick help from the creator of a library that's already very graciously offered for free... You have my word that, if the game makes any money, I'll be sure to make a nice donation on the AngelCode site :]

Thank you very much,
Sebastian
From our conversation via e-mail I know you have a work around already with the use of generic calling conventions and the auto wrapper add-on. However, now that I'm back from my vacation I'll start looking into this.

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
The biggest problem with Marmalade is that it appears to remove all predefines that are normally available. This makes the code in as_config.h fail to identify the target platform and thus doesn't properly set up the defines that the rest of the code need to compile properly.

When compiling for the ARM platform, you need to make sure the macro _ARM_ is defined globally. This should make as_config.h set up the proper macros for ARM, including AS_ARM define.

You will also have to include the as_callfunc_arm_msvc.asm in the project, so the armFuncXXX functions get compiled too. I'm not sure how Marmalade will handle the assembler code. However, as Marmalade can use MSVC10, it can probably also use custom build commands. For the assembler file you can probably use the following custom build command:

armasm -g $(InputPath)


Add to this the recommendations from our e-mail conversations:

1) Defined AS_NO_THREADS to avoid the problem with pthread_rwlock_t
2) Change angelscript.h to use typedef size_t asPWORD; instead of typedef uintptr_t asPWORD;


I'd very much like to see Marmalade working with native calling conventions too. Let me know if the above changes work, and I'll make them part of the official code.


Thanks,
Andreas

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

Hi Andreas,

Thank you very much for the help! With your suggestions I managed to get version 2.25.0 compiling. However, when I run on ARM, the crash still persists. The crash seems to be happening when the program returns from asFunctionPtr. At this point Marmalade gives the following error:

IwAssert failure:
Channel: S3E
File: iwcrt_common.cpp
Line: 836
Expression: false
Message: application aborted

Any ideas?

Thanks,
Sebastian
I'm not sure. asFunctionPtr is a template function to copy different function pointers into a common structure that AngelScript can use to determine how to call the application function. The implementation of asFunctionPtr is extremely simple, I don't see how it could fail inside this function.

Can you show the callstack when the assert happens?

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

Unfortunately, with Marmalade I can only debug ARM builds with a somewhat limited debugger, which for some reason can't show me the callstack once the assert has happened. However, I figured it out by more carefully stepping through the code.


It actually wasn't failing inside of asFunctionPtr (the weird debugger was just making it look that way). The cause of the failure appears to be inside of RegisterBehaviourToObjectType, seemingly because AS_MAX_PORTABILITY is getting defined, making it fail when I try to use CDECL. The problem seems to be happening here:

#ifdef AS_MAX_PORTABILITY
if( callConv != asCALL_GENERIC )
return ConfigError(asNOT_SUPPORTED, "RegisterObjectBehaviour", objectType->name.AddressOf(), decl);
#endif

The code I have written that makes this happen is essentially this:

// Create the script script_engine
script_engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
// Set the message callback to receive information on errors in human readable form.
int r = script_engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL); assert( r >= 0 );
//Register the Vector2 primitive
r = engine->RegisterObjectType("Vector2", sizeof(Vector2),
asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS | asOBJ_APP_CLASS_CONSTRUCTOR | asOBJ_APP_CLASS_ASSIGNMENT | asOBJ_APP_CLASS_COPY_CONSTRUCTOR); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("Vector2", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(trVector2Factory1), asCALL_CDECL_OBJFIRST); assert( r >= 0 );


Once RegisterObjectBehaviour returns an error, my assertion fails (would've been nice if Marmalade told me that instead of a cryptic message about iwcrt_common.cpp).

This works fine on x86 as long as I don't have _ARM_ defined. Maybe the problem is that, by defining _ARM_, I've somehow got AS_MAX_PORTABILITY getting defined as well(when it shouldn't be if I want to use CDECL)?
Advertisement
Apparently the as_config.h is not detecting that you're compiling for an ARM processor and automatically turn on AS_MAX_PORTABILITY.

As you're compiling with MSVC the following defines must be given in order for as_config.h to properly detect the target CPU.

_MSC_VER
MARMALADE
_ARM_

Try forcing those defines in the project settings when compiling the AngelScript library.


It would help a lot if there is a way to see what defines are predefined when compiling with Marmalade. This would require a lot less guesswork then.

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

I ran into a couple issues using these defines.

First, thus far I've had to use AS_MARMALADE rather than simply MARMALADE. The add-ons won't compile with just MARMALADE. I thought it might be because they don't include as_config.h, but while adding #include "../../angelscript/source/as_config.h" made Intellisense happy, it still wouldn't compile for some reason. I get errors like this:

error : no matching function for call to 'operator new [](unsigned int, const _STL::nothrow_t&)' d:\sebastian\games\marmalade\elasticocean\angelscripttest2\angelscript_marmalade\add_on\scriptarray\scriptarray.cpp 413 1 AngelScript_new_vc10

It also seems like I can't define _MSC_VER. Doing so causes a ton of compiler errors within Marmalade (too many to list). I dunno if it's helpful but the first error looks like this:

error : there are no arguments to 'IwDebugExit' that depend on a template parameter, so a declaration of 'IwDebugExit' must be available d: ools\game\marmalade\6.0\modules\iwutil\h\IwDebug.h 591 1 AngelScript_new_vc10

If need be I can ask for help in the Marmalade forums, but I need to know what to ask.
You can use AS_MARMALADE instead of MARMALADE.

_MSC_VER really should be defined internally by the MSVC compiler, so defining it manually could definitely cause some troubles. I just asked you to define it because it seemed that it wasn't defined already.

I would really like to know what defines are given automatically by Marmalade when compiling for ARM with the MSVC compiler. If you can ask on the Marmalade forum if anyone knows how to find that out it would be good.

Without it I really don't know why as_config.h doesn't properly identify that the target platform is Marmalade with ARM.

If you look at the as_config.h it is really quite simple:


Line382: #if defined(_MSC_VER) && !defined(__MWERKS__)
Line404: #if defined(AS_MARMALADE) || defined (MARMALADE)
Line452: #ifdef _ARM_


These three conditions should be evaluated to true in order for it to work, but for some reason it doesn't seem to be the case for you.

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

Dear Andreas:
I had made marmalade project, and run as native ARM api.
It works on Android and iOS.
Just let you known, and I will be glad to share my experience.

The attached Files are my marmalade mkb.
I think it works for 2.25.1 (2.25.0 need some patch)
Besides, you should remove all comments in as_callfunc_arm_gcc.S

And use scons to build marmalade arm library( just right click on the angelscript_lib.mkb,
and choose "Build and Run ARM GCC release")

marmalade use gcc compiler to build the library, so the ABI is the same as gcc.
I use the gcc ARM native ABI to build marmalade library, and it really run on Adnroid and iOS.
I am so happy that angelscript is almost really platform independent, I compile my bytecode on PC,
and run on Android.

This topic is closed to new replies.

Advertisement