Followers 0

# Test case fails on armel

## 13 posts in this topic

I'm trying to get native calling conventions working on armel linux (not android). The test case fails in test_scriptstring,cpp at the follwing:

[source]
printOutput = "";
ExecuteString(engine, "print(\"a\" + 1.2)");
if( printOutput != "a1.2") TEST_FAILED;

printOutput = "";
ExecuteString(engine, "print(1.2 + \"a\")");
if( printOutput != "1.2a") TEST_FAILED;
[/source]

The actual contents of printOutput is:
[source]
a1.05345e-307
5.30084e-315a
[/source]

If I skip those tests it then fails in test_conversion.cpp on the following lines:
[source]
ExecuteString(engine, "ui = -12.3f;"); if( ui != asUINT(-12) ) TEST_FAILED;
ExecuteString(engine, "ui8 = -12.3f;"); if( ui8 != asBYTE(-12) ) TEST_FAILED; // asBYTE(-12.3f) doesn't seem to produce the same result on MSVC and GNUC
ExecuteString(engine, "ui16 = -12.3f;"); if( ui16 != asWORD(-12) ) TEST_FAILED; // asWORD(-12.3f) doesn't seem to produce the same result on MSVC and GNUC
[/source]

Any idea why its failing when handling floats?

Thanks,
Jeremy
0

##### Share on other sites
Unfortunately not. It doesn't seem to be solely related with the native calling conventions though, as the test_conversion isn't calling any functions.

Maybe it is a problem when the compiler parses the number in the script. Or maybe the problem is in the VM when converting the float to an integer.

Would it be possible for you to debug it?
0

##### Share on other sites
Seems like the value is parsed correctly, still trying to figure out where the problem actually is. Here is the debug output for the first test that fails:

[source]
void ExecuteString()

Temps: 1, 3, 4

Variables:
001: string {noname}
004: string {noname}

0 0 * PUSH 4
- 1,1 -
1 4 * SUSPEND
2 4 * PshC8 0x3ff3333333333333 (i:4608083138725491507, f:1.2)
5 6 * STR 0 (l:1 s:"a")
6 8 * CALLSYS 65 (string@ _string_factory_(const int, const uint8&))
8 6 * STOREOBJ v1
9 6 * PshV4 v1
10 7 * CALLSYS 75 (string@ string::opAdd(double) const)
12 4 * STOREOBJ v4
13 5 * FREE v1, 2566488
15 4 * ChkNullV v4
16 4 * VAR v4
17 5 * GETOBJREF 0
18 5 * CALLSYS 113 (void print(const string&in))
20 4 * FREE v4, 2566488
- 2,3 -
22 4 * SUSPEND
23 4 * 0:
23 4 * RET 0
[/source]
0

##### Share on other sites
It has to be in the string conversion, as the following works:
[source]
double test = 1.2;
if(test == 1.2)
print("Yes");

float test = 0.31f;
if(test == 0.31f)
print("Yes");
[/source]

Also if I register a print function to accept a double, it will print correctly.
0

##### Share on other sites
It's probably two different problems. Have you checked the actual value that is assigned to the global variable in the test_conversion.cpp? Also, for case when passing a float/double to a function, maybe it is exclusive to class methods. Can you test writing the print function as a class method instead of a global function?
0

##### Share on other sites
You are correct, if I register a class method to print the double it's not printed correctly.
[source]
class TestOut {
public:
void Print(double t) { printf("%g\n", t); };
};

engine->RegisterObjectType("TestOut", 0, asOBJ_REF | asOBJ_NOHANDLE);
engine->RegisterObjectMethod("TestOut", "void Print(double)", asMETHOD(TestOut, Print), asCALL_THISCALL);
engine->RegisterGlobalProperty("TestOut Out", &Out);

ExecuteString(engine, "double test = 1.2; Out.Print(test)");
[/source]

Outputs:
[source]
2.87027
[/source]

For the following test (first that fails in test_conversion):
[source]
ExecuteString(engine, "ui = -12.3f;"); if( ui != asUINT(-12) ) TEST_FAILED;
[/source]

The global var ui is actually zero, and here is the debug output:
[source]
void ExecuteString()

Temps: 1

Variables:

0 0 * PUSH 1
- 1,1 -
1 1 * SUSPEND
2 1 * SetG4 1074197428, 0
- 2,3 -
5 1 * SUSPEND
6 1 * 0:
6 1 * RET 0
[/source]

-Jeremy
0

##### Share on other sites
Is there some sort of assumption in the interpreter that all CPUs use an Intel x86 double? If so, it's an incorrect assumption for ARM.
0

##### Share on other sites
No, the code make no assumptions on the layout of floats or doubles, except that floats are 32bit wide and doubles are 64bit wide.

That the global function works and the class method doesn't indicates that there is a problem with the code for native calling convention in as_callfunc_arm.cpp. Currently the 'thiscall' is implemented exactly as asCALL_CDECL_OBJFIRST, i.e. it places the object pointer as the first argument on the stack and then the rest of the arguments under that. It seems that with the Linux Armel target and your compiler (presumably gnuc) this is not the case.

Would it be possible for you to provide disassemblies for a C function calling the global print function and the class method? It would help me figure out what the difference is and maybe point out the changes that needs to be made in as_callfunc_arm.cpp.

In the second case the [size="2"]debug output should look like the following:

[code]

0 0 * PUSH 1
- 1,1 -
1 1 * SUSPEND
2 1 * SetG4 1244640, -12
- 2,3 -
5 1 * SUSPEND
6 1 * 0:
6 1 * RET 0
[/code]
[/size][size="2"]
The problem is most likely in asCCompiler::ImplicitConversionConstant. More specifically the following (line 4849):

[code]
asUINT uic = asUINT(fc);
[/code]

Can you try changing it to the following?:

[code]
asUINT uic = asUINT(int(fc));
[/code]

Some compilers give the value 0, when converting a negative float to an unsigned integer. This looks like what is happening here.

[/size]
0

##### Share on other sites
[quote name='WitchLord' timestamp='1307759513' post='4821950']
[size="2"]The problem is most likely in asCCompiler::ImplicitConversionConstant. More specifically the following (line 4849):

[code]
asUINT uic = asUINT(fc);
[/code]

Can you try changing it to the following?:

[code]
asUINT uic = asUINT(int(fc));
[/code]
[/quote]

This fixes that problem and now TestConversion passes.[/size]

Now I'm not sure if this is what you are looking for, but this is the assembly generated by GCC for calling the Print() function:
[source]
.file "test.cpp"
.section .rodata
.LC0:
.string "%g\n"
.text
.globl Print
.type Print, @function
Print:
pushl %ebp
movl %esp, %ebp
subl $40, %esp movl 8(%ebp), %eax movl %eax, -16(%ebp) movl 12(%ebp), %eax movl %eax, -12(%ebp) movl$.LC0, %eax
fldl -16(%ebp)
fstpl 4(%esp)
movl %eax, (%esp)
call printf
leave
ret
.size Print, .-Print
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp subl$16, %esp
fldl .LC2
fstpl (%esp)
call Print
movl \$0, %eax
leave
ret
.size main, .-main
.section .rodata
.align 8
.LC2:
.long -858993459
.long 1073007820
.ident "GCC: (Debian 4.4.5-8) 4.4.5"
.section .note.GNU-stack,"",@progbits
[/source]

The following is when calling a class print method:
[source]
.cpu arm9tdmi
.fpu softvfp
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 2
.eabi_attribute 30, 6
.eabi_attribute 18, 4
.file "test.cpp"
.section .rodata
.align 2
.LC0:
.ascii "%g\012\000"
.section .text._ZN7OutTest5PrintEd,"axG",%progbits,_ZN7OutTest5PrintEd,comdat
.align 2
.weak _ZN7OutTest5PrintEd
.type _ZN7OutTest5PrintEd, %function
_ZN7OutTest5PrintEd:
.fnstart
.LFB2:
@ Function supports interworking.
@ args = 0, pretend = 0, frame = 16
@ frame_needed = 1, uses_anonymous_args = 0
.movsp ip
mov ip, sp
.LCFI0:
stmfd sp!, {fp, ip, lr, pc}
.save {fp, ip, lr}
.LCFI1:
.setfp fp, ip, #-4
sub fp, ip, #4
.LCFI2:
sub sp, sp, #16
.LCFI3:
str r0, [fp, #-16]
str r2, [fp, #-28]
str r3, [fp, #-24]
ldr r0, .L3
sub r2, fp, #28
ldmia r2, {r2-r3}
bl printf
sub sp, fp, #12
ldmfd sp, {fp, sp, lr}
bx lr
.L4:
.align 2
.L3:
.word .LC0
.LFE2:
.fnend
.size _ZN7OutTest5PrintEd, .-_ZN7OutTest5PrintEd
.text
.align 2
.global main
.type main, %function
main:
.fnstart
.LFB3:
@ Function supports interworking.
@ args = 0, pretend = 0, frame = 8
@ frame_needed = 1, uses_anonymous_args = 0
.movsp ip
mov ip, sp
.LCFI4:
stmfd sp!, {fp, ip, lr, pc}
.save {fp, ip, lr}
.LCFI5:
.setfp fp, ip, #-4
sub fp, ip, #4
.LCFI6:
sub sp, sp, #8
.LCFI7:
sub r3, fp, #13
mov r0, r3
ldmia r2, {r2-r3}
bl _ZN7OutTest5PrintEd
mov r3, #0
mov r0, r3
sub sp, fp, #12
ldmfd sp, {fp, sp, lr}
bx lr
.L8:
.align 3
.L7:
.word -858993459
.word 1073007820
.LFE3:
.fnend
.size main, .-main
.ident "GCC: (Debian 4.3.5-4) 4.3.5"
.section .note.GNU-stack,"",%progbits
[/source]

If this isn't what you are looking for, then I will have to figure out how to disassemble a arm binary.

Thanks,
Jeremy
0

##### Share on other sites
Thanks. This clarifies things.

From the disassembly (by the way, the one for global print is for x86, not arm) I can see that the ABI for your target system loads the object pointer into r0, and the float arg into r2. But the implementation in as_callfunc_arm.gcc.S the float arg is loaded into r1, which explains why you get the error.

It will probably be necessary to write a different version of as_callfunc_arm.gcc.S to adapt it to Linux armel. Any idea where I can find the specification for the ABI for your target system?
0

##### Share on other sites
The following is a PDF on AAPCS (eabi calling conventions)
http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042d/IHI0042D_aapcs.pdf

This describes a little about the differences and the linux port for eabi:
http://wiki.debian.org/ArmEabiPort

Android should use the same calling conventions as it also uses ARM EABI. I haven't been able to test this on android, as I haven't been able to get angelscript to compile. The android NDK is missing a lot of stuff, including a real stdc++ library. I'm guessing who ever ported it used one of the third-party toolchains to build it. But I'm really curious if these tests pass on it or not.

jeremy
0

##### Share on other sites
Android does have a stdc++ library and much better support for C++ in NDK r5. I'm working to get the library and test_feature built and will post the results when I'm done.
0

##### Share on other sites
After a long time rooting my phone (which required me to downgrade then upgrade again), and figuring out all the correct flags, and include paths I got test_feature to compile. The GetSet test actually causes a segfault on my phone, I will try to figure out where it crashes, just need to figure out how to use gdb with my phone. Skipping that test leads to the same failure in test_scriptstring.
0

##### Share on other sites
I believe the original code for arm was written for embedded devices, so it may not follow the ABI used by Linux and Android.

I'll study the specification for the ABI and see if I can determine what exactly needs to be modified. Unfortunately I do not have any ARM devices so I can't really test any of the changes myself.
0

## Create an account

Register a new account