6.4.2. Returning integer-like structures

The APCS specifies different rules for returning integer-like structures. An integer-like structure:

The following structures are integer-like:

struct
{ 
	unsigned a:8, b:8, c:8, d:8;
}
union polymorphic_ptr
{ 
	struct A *a;
	struct B *b;
	int      *i;
}

whereas the structure used in Example 6.5 is not:

struct { char ch1, ch2; }

An integer-like structure has its contents returned in a1. This means that a1 is not needed to pass a pointer to a result structure in memory, and is instead used to pass the first argument. Example 6.7 demonstrates this.

Example 6.7. 

typedef struct half_words_struct
{	unsigned field1:16;
	unsigned field2:16;
}half_words;
half_words max(half_words a, half_words b)
{ half_words x;
	x = (a.field1 > b.field1) ? a : b;
	return x;
}

Arguments a and b are passed in registers a1 and a2, and because half_word_struct is integer-like, you would expect a1 to return the result structure directly, rather than a pointer to it.

This code is available in the file examples/candasm/half_str.c, and can be compiled to produce assembly language source using:


armcc -S half_str.c -li

Example 6.8 shows the code armcc produces. The version of armcc supplied with your release may produce output slightly different from that listed here.

Example 6.8. 

max
	MOV		a3,a1,LSL #16
	CMP		a3,a2,LSL #16
	MOVLS		a1,a2
	MOV		pc,lr

From this you can see that the contents of the half_words structure is returned directly in a1 as expected.

Copyright © 1997, 1998 ARM Limited. All rights reserved.ARM DUI 0040D
Non-Confidential