Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-10

Packed record

8 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

Florian Klaempfl
to Carsten Bager
Carsten Bager wrote 21 lines
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

Show quoted text 11 lines
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

Reply by email

Adriaan van Os
to Florian Klaempfl
Florian Klaempfl wrote 41 lines
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

Reply by email

Florian Klaempfl
to Adriaan van Os
Adriaan van Os wrote 50 lines
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.

Reply by email

Tomas Hajny
to Florian Klaempfl

Florian Klaempfl napsal(a):

Show quoted text 54 lines
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

Reply by email

Florian Klaempfl
to Tomas Hajny
Tomas Hajny wrote 5 lines
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.

Reply by email

Carsten Bager
to Florian Klaempfl
Show quoted text 14 lines
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 - 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

Reply by email