# Marmalade: Angelscript runs fine on x86, crashes on ARM

This topic is 1857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 [url="http://www.gamedev.net/topic/619511-compiling-with-marmalade/"]this thread[/url] 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:
[CODE]
#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
[/CODE]

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:

[CODE]
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>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(130) : undefined reference to armFunc'
1>
1>
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>
1>
1> d:/sebastian/games/marmalade/angelscript_new3/angelscript_marmalade/angelscript/source/as_callfunc_arm.cpp(155) : undefined reference to armFuncR0R1'
1>
1>
1>
1>
[/CODE]

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

##### Share on other sites
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.

##### Share on other sites
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:

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

##### Share on other sites
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

##### Share on other sites
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?

##### Share on other sites
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:
[code]
#ifdef AS_MAX_PORTABILITY
if( callConv != asCALL_GENERIC )
#endif
[/code]
The code I have written that makes this happen is essentially this:
[code]
// 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 );
[/code]

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)?

##### Share on other sites
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
_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.

##### Share on other sites
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 [i]#include "../../angelscript/source/as_config.h" [/i]made Intellisense happy, it still wouldn't compile for some reason. I get errors like this:

[b]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[/b]

It also seems like I can't define [i]_MSC_VER[/i]. 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:

[b]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[/b]

If need be I can ask for help in the Marmalade forums, but I need to know what to ask.

##### Share on other sites

_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:

[code]
Line382: #if defined(_MSC_VER) && !defined(__MWERKS__)
Line452: #ifdef _ARM_
[/code]

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.

##### Share on other sites
Dear Andreas:
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.

##### Share on other sites
Here is the attached file

##### Share on other sites
Thanks for sharing this loboWu.

I'm very pleased to know it works properly.

I didn't know Marmalade used gcc to build for ARM. Are you building the library from Windows? Do you have to make a separate download of gcc or does it come with the Marmalade SDK?

##### Share on other sites
The arm gcc compiler is just come with marmalade SDK.
It use arm-none-eabi with GCC 4.4.1

##### Share on other sites
Hi IoboWu,

Thank you for sharing your project files. Could you explain a little bit about how you use them? I downloaded them and, judging from the file paths inside them, assumed I'm supposed to put them in angelscript/projects/marmalade. When I run the *.mkb I get a slew of errors about unrecognized opcodes, concluding with the error:

error: Executing 'd: ools\game\marmalade\6.0\s3e\bin\scons.bat -Q compiler=gcc' failed. (return code 2)

I tried including the path of the *.mkf from my current Marmalade project; when I do this, the project loads, but upon compiling I get:

Error 4 error MSB6006: "link.exe" exited with code 1.

I also tried copying various parts of your *.mkb into the *.mkf I have been using to include Angelscript. Copying most of the stuff over caused no change (the program still fails when I try to register a CDECL function on ARM), but copying the part about the *.S files makes the project fail to load up. In this scenario the command line seems to be actually compiling the code, which is a bit strange... it fails when trying to use an add-on, RegisterStdString, claiming it "was not declared in this scope."

Sorry I'm having so much difficulty figuring this stuff out... I don't have much experience writing multiplatform code or with Marmalade but I've been trying my best. If somewhere there was a complete downloadable Marmalade project that demonstrates compiling and running the latest Angelscript as well as registering and using CDECL functions without failing on device, that would be a dream come true.

##### Share on other sites
I have to say also that angelscript can work properly with marmalade.. with some finishing touches.
Secondly, it needs to use scons for Android or IOS builds (see http://www.madewithmarmalade.com/devnet/forum/angel-script for more details).
Edited by Papirosnik

##### Share on other sites
Thanks for reminding me of the comments in the .S file, and for clarifying the need to use scons to build for Android and iOS.

I don't want to remove the comments from the .S file. But perhaps there is another comment syntax that is acceptable when compiling with both Marmalade and gcc? Do you know what can be used instead of the C++ comments?

##### Share on other sites
You can use C comments - /* ... */
I'm not sure about Marmelade, but GNU assembler also accepts # as delimiter for comments - the rest of line after # symbol will be viewed as comment

##### Share on other sites
Thanks. I think I'll try the # style then.

It would be good if someone using Marmalade can confirm that it actually works before I check in the changes.

##### Share on other sites
[quote name='Andreas Jonsson' timestamp='1352988835' post='5001224']
Thanks. I think I'll try the # style then.
It would be good if someone using Marmalade can confirm that it actually works before I check in the changes.
[/quote]

I just checked and can confirm that '//' comments are not allowed; using '#' instead '//' is allowed but only at the beginning of the line... arm assembler uses '#' as a part of operands.

'/*.. */' is allowed everywhere and probably would be the best solution. Edited by Papirosnik

##### Share on other sites
Thanks for confirming this. I'll make the necessary changes.

Regards,
Andreas

##### Share on other sites
Hi Andreas,

Thanks very much for all your help, as well as IobuWu, Papirosnik, and Martins Mozeiko. However, even after trying everything, I am still unable to get anything but the generic calling convention working on an ARM processor. I've asked a [url="https://devnet.madewithmarmalade.com/questions/1934/preprocessor-definitions-angelscript-with-marmalad.html"]question[/url] to Marmalade like you suggested.

I would love to help solve this as it'd make my life a whole lot easier, having chosen AngelScript as the scripting language for the game project I'm working on. The generic calling conventions are also a problem when using things like the "ref" type extension, which I need to let my game objects communicate, but I'll have to modify to use generic calling conventions if I can't solve the cdecl problem.

It appears that some of the suggestions in this thread may have been coded into the newest version of AngelScript (2.25.2) which I am now using. I still have made sure that MARMALADE and _ARM_ are getting defined by putting them into the project manually. Defining AS_NO_THREADS doesn't seem to be necessary anymore (though I have tried with and without). I tried using the *.S files instead of *.asm with scons as suggested (had to delete the comments as I couldn't figure out the correct syntax to change it to), unfortunately I still get the same runtime error when running the scons builds.

It doesn't seem that Marmalade defines _MSC_VER when compiling for ARM from within MSVC. It doesn't seem to define that either when compiling with scons. Perhaps this is the problem, as it gets into the other two just fine when I force that one to true. However, I can't simply do this to make it work as it seems to cause a domino-chain of other compiler errors that I can't fix without intimate knowledge of AngelScript's inner workings.

In an effort to make the problem as clear as possible I've put together a complete "Hello World" Angelscript/Marmalade project that should be runnable on any system with even the free trial of Marmalade installed (and MSVC). It's attached to this message. If this project worked without crashing on ARM it would be like gold to me (I've spent days trying to troubleshoot this). As it is now, here are instructions on how to reproduce the crash:

[u]MSVC way:[/u]
1.) Double-click AngelScript_HelloWorld.mkb to open the project in MSVC.
2.) If you'd like, compile the debug or release version for x86 and run it. This is what's supposed to happen (a "Hello World" dialog)
3.) Compile the debug version using GCC(ARM). This should compile successfully.
4.) Hit F5 and this will start the special ARM simulator and debugger.
5.) To see the exact point of the problem, navigate to as_scriptengine.cpp and put a breakpoint on line 2473.
6.) Click "continue" and the program will hit the breakpoint (which means the error has occurred)

(note that _ARM_ isn't actually defined for the MSVC build as I haven't figured out how to do this for only one build with Marmalade. However, even if you explicitly declare _ARM_ in the project settings, the same error occurs because _MSC_VER isn't defined for the ARM compiler)

[u]Scons way:[/u]
1.) Double-click the scons.bat file that I put in the same directory as the project files, this should compile to ARM files with scons.
2.) Navigate to the build_angelscript_helloworld_scons_scons_arm directory.
3.) Choose either debug or release directory.
4.) Copy the *.s3e file in that directory to the "data" directory under the project's folder. You can double-click it to run it from there (ignore the Marmalade warning message).
5.) You should get the same result as if you ran the ARM simulator from MSVC.
6.) If you want to debug, you should be able to click the *.s3e file and click "debug." Then you can point the ARM simulator to the generated *.elf file in the directory where the build came from.

That is the situation as best as I've been able to understand it. I hope that this leads to a solution. I've been very happy with AngelScript so far, it's just this calling convention problem that's been driving me up a wall.

Thank you very much,
Sebastian

##### Share on other sites
From what we've learned earlier from papirosnik and loboWu, when compiling for ARM scons must be used, so I suggest you concentrate your efforts on that and leave trying to get it to work with MSVC for later.

Do you expect this to run on iOS or Android? I don't think Marmalade defines either when compiling for arm, which will leave the configuration in AngelScript only partly defined.

Try including ANDROID or __APPLE__ and TARGET_OS_IPHONE, i.e.

[code]
defines
{
_ARM_
ANDROID
}
[/code]

or

[code]
defines
{
_ARM_
__APPLE__
TARGET_OS_IPHONE
}
[/code]

In loboWu's makefile I see he defined AS_ARM directly to enable the as_callfunc_arm.cpp. But this would bypass all other configuration in as_config.h so this is not recommended. However, if none of the above works, you might try that as well.

Observe, iOS and Android uses slightly different calling conventions and I have no idea which Marmalade uses (or even if it isn't using a completely different calling convention) so you may need to try both to see which works best.

##### Share on other sites
Hi Andreas, thanks for the response. I would like to be able to make builds for both iOS and Android. I tried both those suggestions and got compiler errors.

With Android define:
[i]as_callfunc.cpp:476: error: exception handling disabled, use -fexceptions to enable[/i]

I figure I am supposed to add that command line flag somehow, but I can't figure it out. Neither:

AngelScript_HelloWorld_scons.mkb --arm --buildenv=scons --compiler=gcc --cppflag=-fexceptions

nor:

AngelScript_HelloWorld_scons.mkb --arm --buildenv=scons --compiler=gcc -fexceptions

seem to work.

With iPhone defines:
[i]as_config.h:583: fatal error: TargetConditionals.h: No such file or directory[/i]

My guess is that this is an apple-specific library that I should probably be able to include as a subproject with Marmalade, but I don't know what subproject to use. Should I ask another question to Marmalade?

The way the Marmalade SDK works, it seems to compile a single ARM binary, and be able to "package" it up for use with either iOS or Android without the need for a recompile. So if the systems are slightly different, you may be right be right that Marmalade has a favorite, though it's not clear yet which one that might be.

##### Share on other sites
From loboWu's makefile I see that exceptions are enabled like this:

[code]
#!/usr/bin/env mkb

options
{
enable-exceptions=1
lib
module_path="../../source"
}
[/code]

Did you try defining AS_ARM directly? Like loboWu does in his makefile? What was the result of that?

##### Share on other sites
I played around with AS_ARM and it didn't seem to help. However, using the following defines/options:

defines
{
_ARM_
ANDROID
}

options
{
enable-exceptions=1
}

It seems that, when compiling with SCons, the ARM versions are finally working properly! Since Marmalade's ARM binaries are theoretically platform independent, I may be able to "package" the binary up for iOS as well.

Thanks a ton for all your help. It seems my troubles may finally be over, though I need to verify everything works on device. Once I can confirm everything's working I'll try and clean up the Hello World program and post it for anyone who might need it in the future. Will post again with results.

---Sebastian