Packed record

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Packed record

Carsten Bager
I am using the compiler on an embedded Arm7 platform (no
operating system). So fare things are working ok, but now I have
discovered a problem when packing records (Vi have to maintain
bindery compatibility with older systems)
If I have a packed record like this

Test_typ= packed record
     B:byte;
     I:LongInt;
end;

Var
  Test:Test_typ;

Begin
  Test.i:=1;
  Write(test.i);
End.


The output is wrong. If I remove "Packed" the output is OK.

Regards

Carsten

----------------------------------
Compiler version
Free Pascal Compiler version 2.0.0 [2005/05/15] for arm
Copyright (c) 1993-2005 by Florian Klaempfl
Target OS: Linux for ARM

--
Carsten Bager
Bøvlingbjerg El-automatik
Brørupvænget 10
7650 Bøvlingbjerg
Tlf. +4597885222 Fax +4597885434
http://www.beas.dk

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Packed record

Florian Klämpfl
Carsten Bager wrote:

> I am using the compiler on an embedded Arm7 platform (no  
> operating system). So fare things are working ok, but now I have  
> discovered a problem when packing records (Vi have to maintain  
> bindery compatibility with older systems)  
> If I have a packed record like this
>
> Test_typ= packed record
>      B:byte;
>      I:LongInt;
> end;  
>
> Var
>   Test:Test_typ;
>
> Begin
>   Test.i:=1;
>   Write(test.i);
> End.
>  
>
> The output is wrong.

Usual ARM cores can't handle unaligned memory accesses and on e.g. arm-linux you
would get a sigbus exception on your code.

> If I remove "Packed" the output is OK.

What can do is:
- modify your pascal code:

l:longint;
move(l,Test.i,sizeof(Test.i));  // move can handle unaligned access and that how
C code usually solves it

- add an exception handler to your OS to catch unaligned access and fix things
but emulating the memory access



>
> Regards
>
> Carsten
>
> ----------------------------------
> Compiler version
> Free Pascal Compiler version 2.0.0 [2005/05/15] for arm
> Copyright (c) 1993-2005 by Florian Klaempfl
> Target OS: Linux for ARM
>

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Packed record

Adriaan van Os-2
Florian Klaempfl wrote:

> Carsten Bager wrote:
>
>> I am using the compiler on an embedded Arm7 platform (no
>> operating system). So fare things are working ok, but now I have
>> discovered a problem when packing records (Vi have to maintain
>> bindery compatibility with older systems)
>> If I have a packed record like this
>>
>> Test_typ= packed record
>>      B:byte;
>>      I:LongInt;
>> end;
>>
>> Var
>>   Test:Test_typ;
>>
>> Begin
>>   Test.i:=1;
>>   Write(test.i);
>> End.
>>
>>
>> The output is wrong.
>
> Usual ARM cores can't handle unaligned memory accesses and on e.g.
> arm-linux you
> would get a sigbus exception on your code.
>
>> If I remove "Packed" the output is OK.
>
> What can do is:
> - modify your pascal code:
>
> l:longint;
> move(l,Test.i,sizeof(Test.i));  // move can handle unaligned access
> and that how
> C code usually solves it
>
> - add an exception handler to your OS to catch unaligned access and
> fix things
> but emulating the memory access

- or fix the compiler to ignore "packed" for ARM. This could be a
compiler switch, on by default for ARM (gpc has --ignore-packed).

Regards,

Adriaan van Os

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Packed record

Florian Klämpfl
Adriaan van Os wrote:

> Florian Klaempfl wrote:
>
>> Carsten Bager wrote:
>>
>>> I am using the compiler on an embedded Arm7 platform (no
>>> operating system). So fare things are working ok, but now I have
>>> discovered a problem when packing records (Vi have to maintain
>>> bindery compatibility with older systems)
>>> If I have a packed record like this
>>>
>>> Test_typ= packed record
>>>      B:byte;
>>>      I:LongInt;
>>> end;
>>>
>>> Var
>>>   Test:Test_typ;
>>>
>>> Begin
>>>   Test.i:=1;
>>>   Write(test.i);
>>> End.
>>>
>>>
>>> The output is wrong.
>>
>>
>> Usual ARM cores can't handle unaligned memory accesses and on e.g.
>> arm-linux you
>> would get a sigbus exception on your code.
>>
>>> If I remove "Packed" the output is OK.
>>
>>
>> What can do is:
>> - modify your pascal code:
>>
>> l:longint;
>> move(l,Test.i,sizeof(Test.i));  // move can handle unaligned access
>> and that how
>> C code usually solves it
>>
>> - add an exception handler to your OS to catch unaligned access and
>> fix things
>> but emulating the memory access
>
>
> - or fix the compiler to ignore "packed" for ARM. This could be a
> compiler switch, on by default for ARM (gpc has --ignore-packed).
>

No. He needs to maintain binary compatibility with an older system so this is no
option, see initial post.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Packed record

Tomas Hajny
Florian Klaempfl napsal(a):

> Adriaan van Os wrote:
>> Florian Klaempfl wrote:
>>> Carsten Bager wrote:
>>>
>>>> I am using the compiler on an embedded Arm7 platform (no
>>>> operating system). So fare things are working ok, but now I have
>>>> discovered a problem when packing records (Vi have to maintain
>>>> bindery compatibility with older systems)
>>>> If I have a packed record like this
>>>>
>>>> Test_typ= packed record
>>>>      B:byte;
>>>>      I:LongInt;
>>>> end;
>>>>
>>>> Var
>>>>   Test:Test_typ;
>>>>
>>>> Begin
>>>>   Test.i:=1;
>>>>   Write(test.i);
>>>> End.
>>>>
>>>>
>>>> The output is wrong.
>>>
>>>
>>> Usual ARM cores can't handle unaligned memory accesses and on e.g.
>>> arm-linux you
>>> would get a sigbus exception on your code.
>>>
>>>> If I remove "Packed" the output is OK.
>>>
>>>
>>> What can do is:
>>> - modify your pascal code:
>>>
>>> l:longint;
>>> move(l,Test.i,sizeof(Test.i));  // move can handle unaligned access
>>> and that how
>>> C code usually solves it
>>>
>>> - add an exception handler to your OS to catch unaligned access and
>>> fix things
>>> but emulating the memory access
>>
>>
>> - or fix the compiler to ignore "packed" for ARM. This could be a
>> compiler switch, on by default for ARM (gpc has --ignore-packed).
>>
>
> No. He needs to maintain binary compatibility with an older system so this
> is no
> option, see initial post.

What exactly happens there now (I don't have an ARM, just curious ;-) )?
Is it that the compiler generates the code as it is instructed to do (i.e.
regardless the fact that it might not work under ARM), and that the
particular instruction is just ignored by the CPU (without exception), or
what?

Tomas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Packed record

Florian Klämpfl
Tomas Hajny wrote:
>
>
> What exactly happens there now (I don't have an ARM, just curious ;-) )?
> Is it that the compiler generates the code as it is instructed to do (i.e.
> regardless the fact that it might not work under ARM),

Yes. That's common behaviour.

> and that the
> particular instruction is just ignored by the CPU (without exception), or
> what?

That depends on the OS, I've no clue either. Under Linux you get an exception.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Packed record

Carsten Bager
> Tomas Hajny wrote:
> >
> >
> > What exactly happens there now (I don't have an ARM, just curious ;-) )?
> > Is it that the compiler generates the code as it is instructed to do (i.e.
> > regardless the fact that it might not work under ARM),
>
> Yes. That's common behaviour.
>
> > and that the
> > particular instruction is just ignored by the CPU (without exception), or
> > what?
>
> That depends on the OS, I've no clue either. Under Linux you get an exception.

The CPU keeps running, In fact if I analyze the Input/Output
(convert it to hex) it is possible to se that part of the number
is written and that what should be LSB has moved.

I have solved the problem with "move" as suggested by
Florian Klaempfl.

>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal

--
Carsten Bager
Bøvlingbjerg El-automatik
Brørupvænget 10
7650 Bøvlingbjerg
Tlf. +4597885222 Fax +4597885434
http://www.beas.dk

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Packed record

Marco van de Voort
In reply to this post by Florian Klämpfl
> > particular instruction is just ignored by the CPU (without exception), or
> > what?
>
> That depends on the OS, I've no clue either. Under Linux you get an
> exception.

On PPC we saw that NetBSD doesn't do this (at least for floats, for which
older PPCs (603) have alignment requirements.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal