tw8513.pp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. type
  2. TMyType = cardinal;
  3. tr = record
  4. a,b,c,d: byte;
  5. end;
  6. procedure t(var l: cardinal);
  7. begin
  8. if (l <> $cafebabe) then
  9. halt(4);
  10. l := $c001d00d;
  11. end;
  12. var
  13. Item: TMyType;
  14. ItemAsByte: byte absolute Item;
  15. r: tr;
  16. b: byte absolute r.b;
  17. l: cardinal;
  18. labs: cardinal absolute l;
  19. begin
  20. { Of course I understand fully that this code is bad
  21. (unless you really want to read the 1st byte of 4-byte LongInt
  22. type, messing with endianess problems).
  23. In real code, I accessed ItemAsByte only when
  24. SizeOf(TMyType) = 1 (the code is
  25. used like a simple template, so it must work with any
  26. TMyType, and the case when SizeOf(TMyType) = 1 uses some
  27. specially optimized versions (e.g. FillChar(..., ItemAsByte)
  28. can be used in this case to fill the array of TMyType). }
  29. {$ifdef FPC_BIG_ENDIAN}
  30. item:=$deadbeef;
  31. {$else}
  32. item:=$efbeadde;
  33. {$endif}
  34. if (itemasbyte <> $de) then
  35. halt(1);
  36. r.a := $de;
  37. r.b := $ad;
  38. r.c := $be;
  39. r.d := $ef;
  40. if (b <> $ad) then
  41. halt(2);
  42. l := $cafebabe;
  43. t(labs);
  44. if (l <> $c001d00d) then
  45. halt(6);
  46. end.