Help with alignment of types in S12XCPU and XGATE

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with alignment of types in S12XCPU and XGATE

3,141 Views
admin
Specialist II
Hello, I can not understand as to solve the following problem: has created own type which I use both in S12X_CPU and in XGate, but at compilation has appeared the warning "L1827: Symbol ControlData has different size in xgate.cxgate.o (10 bytes) and main.cpp.o (9 bytes)". The type looks like the following: typedef struct _Measuring_Device_data { tHL2Byte addres; byte number; byte length; byte sign:1; t4Long value; } Measuring_Device_data; Used user's types: typedef union u4Long { long dWord; struct { tHL2Byte msw; tHL2Byte lsw; } _word; } t4Long; typedef union uHL2Byte { word _word; struct { byte msb; byte lsb; }_byte; } tHL2Byte; Prompt as to make type alignment that they had the identical size and S12X_CPU and in XGate?
Labels (1)
0 Kudos
10 Replies

558 Views
admin
Specialist II
Has made so that pragma it was visible only S12X, the warning has disappeared, types are aligned..... Here only how to be if a variable of such user's type it is necessary to use as well as in S12X, and in XGATE? I have made here and so:
#ifndef _TYPE_H_
#define _TYPE_H_
#ifdef __XGATE__
typedef struct _TYPE
{ char _ch;
char _ch2;
char _ch3;
int _int;
float _float;
}TYPE;
#else
#pragma align on
typedef struct _TYPE
{
char _ch;
char _ch2;
char _ch3;
int _int;
float _float;
}TYPE;
#pragma align off
#endif
#endif
The Given heading file I connect both in XGATE unit, and in S12X. It is correct?
0 Kudos

558 Views
kef
Specialist I
Hm, i think there's one more problem. Are bitfields portable, bit order and memory needed to hold bit variable? I think they aren't. But since both compilers (S12X and XGATE) are from the same provider and for the same chip, they should work the same. Unfortunately they don't. It seems that CW4.6 S12X compiler is adding aligning character after your struct, instead of
 
#pragma align on
 
struuct
{
   tHL2Byte addres;
   byte number;
   byte length; 
   int sign:1;  << bitfield size is single byte
   t4Long value;
     << extra byte allocated here
}
while XGATE compiler is aligning .value member.
 
If you bitfield in your structure with char, then both compilers are aligning struct members the same.
 
 
0 Kudos

558 Views
admin
Specialist II
Has much understood explanations....... It turns out if to add additional empty byte that the compiler will make alignment struct? Or simply it is not necessary to use bitfields?
0 Kudos

558 Views
kef
Specialist I
Sorry maybe odd reply. What I wanted to say is that using CW4.6 I was unable to share your struct with bitfields (   int sign:1;  or char sign:1; in the struct). But if you replace bitfield with plain int or char variable, then all it starts working, provided you are using #pragma align on in S12X part of code. I think it's a toolset bug. Hope CW4.7 has fixed that.
 
0 Kudos

558 Views
admin
Specialist II
Thanks for the help, and answers:-). In my program the compiler does alignment even if I use in structure with bitfields, a unique problem which could not solve meanwhile it how to make visible this data type both S12X and XGATE. If to connect a heading file with type declaration both to S12X and to XGATE that at compilation, I believe it does compiler XGATE, there is a warning "C4201: pragma align was not handled"
0 Kudos

558 Views
kef
Specialist I
I thought you figured how to make it compileable without warning, using __XGATE__ define. Something like that.
 
#ifndef __XGATE__
   #pragma align on
#endif
typedef union uHL2Byte {
   word _word;
   struct { byte msb; byte lsb; }_byte;
} tHL2Byte;
typedef union u4Long {
   long dWord;
   struct { tHL2Byte msw; tHL2Byte lsw; } _word;
} t4Long;
typedef struct _Measuring_Device_data
{
   tHL2Byte addres;
   byte number;
   byte length;
   char sign:1;
   t4Long value;
} Measuring_Device_data;
 
// and probably declare some extern data
extern Measuring_Device_data data;
 
#ifndef __XGATE__
   #pragma align off
#endif
Then in one of source files you include the header and define (allocate) you data:
Measuring_Device_data data;
 
 
I again was not clear. I meant that this code doesn't work in CW4.6. Having sign member defined as    char sign:1; , or as    int sign:1; , value member offset is +5 in S12X part, and +6 in XGATE! Try filling structure from S12X, then reading it from XGATE.
And with sign member defined as char sign, or as int sign, both structures are aligned absolutely the same, and sharing between XGATE and S12X works properly.


Message Edited by kef on 2008-10-23 03:29 PM
0 Kudos

558 Views
admin
Specialist II
Thanks for councils :smileywink: Kef
0 Kudos

558 Views
admin
Specialist II
Try using..... The compiler has produced the warning "C4201: pragma aling was not handled"
0 Kudos

558 Views
kef
Specialist I
Only S12X compiler should see this pragma. XGATE compiler doesn't know about it.
 
0 Kudos

558 Views
kef
Specialist I

Try using

#pragma align on

in your S12X code.

0 Kudos