Jelajahi Sumber

* fixed compilation for i8086 and 8-bit targets after r49622. The conditional
ifdef checks for the ordinal helpers rewritten to be less dependendant on
defines, such as CPU16/CPU32/CPU64 and instead use sizeof(integer),
sizeof(nativeint) and sizeof(nativeuint)

git-svn-id: trunk@49634 -

nickysn 4 tahun lalu
induk
melakukan
b878d461c8
2 mengubah file dengan 143 tambahan dan 53 penghapusan
  1. 37 14
      rtl/objpas/sysutils/syshelp.inc
  2. 106 39
      rtl/objpas/sysutils/syshelph.inc

+ 37 - 14
rtl/objpas/sysutils/syshelp.inc

@@ -1639,6 +1639,29 @@ end;
 {$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
 {$define TORDINALBYTEINDEX:=TIntegerByteIndex}
 {$define TORDINALWORDINDEX:=TIntegerWordIndex}
+{$if sizeof(Integer)=2}
+  {$define TORDINALOVERLAY:=TWordOverlay}
+  {$define TORDINALTYPESIZE2}
+{$elseif sizeof(Integer)=4}
+  {$define TORDINALOVERLAY:=TDwordOverlay}
+  {$define TORDINALTYPESIZE4}
+{$else}
+  {$fatal Unsupported Integer type size}
+{$endif}
+{$i syshelpo.inc}
+{$undef TORDINALTYPESIZE2}
+{$undef TORDINALTYPESIZE4}
+
+{ ---------------------------------------------------------------------
+  TLongIntHelper
+  ---------------------------------------------------------------------}
+
+{$define TORDINALHELPER:=TLongIntHelper}
+{$define TORDINALTYPE:=LongInt}
+{$define TORDINALBITINDEX:=TLongIntBitIndex}
+{$define TORDINALNIBBLEINDEX:=TLongIntNibbleIndex}
+{$define TORDINALBYTEINDEX:=TLongIntByteIndex}
+{$define TORDINALWORDINDEX:=TLongIntWordIndex}
 {$define TORDINALOVERLAY:=TDwordOverlay}
 {$define TORDINALTYPESIZE4}
 {$i syshelpo.inc}
@@ -1683,26 +1706,26 @@ end;
 {$define TORDINALHELPER:=TNativeIntHelper}
 {$define TORDINALTYPE:=NativeInt}
 {$define TORDINALBITINDEX:=TNativeIntBitIndex}
-{$ifdef cpu16}
+{$if sizeof(NativeInt)=2}
   {$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
   {$define TORDINALBYTEINDEX:=TSmallIntByteIndex}
   {$define TORDINALOVERLAY:=TSmallIntOverlay}
   {$define TORDINALTYPESIZE2}
-{$endif}
-{$ifdef cpu32}
-  {$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
-  {$define TORDINALBYTEINDEX:=TIntegerByteIndex}
-  {$define TORDINALWORDINDEX:=TIntegerWordIndex}
-  {$define TORDINALOVERLAY:=TIntegerOverlay}
+{$elseif sizeof(NativeInt)=4}
+  {$define TORDINALNIBBLEINDEX:=TLongIntNibbleIndex}
+  {$define TORDINALBYTEINDEX:=TLongIntByteIndex}
+  {$define TORDINALWORDINDEX:=TLongIntWordIndex}
+  {$define TORDINALOVERLAY:=TLongIntOverlay}
   {$define TORDINALTYPESIZE4}
-{$endif}
-{$ifdef cpu64}
+{$elseif sizeof(NativeInt)=8}
   {$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
   {$define TORDINALBYTEINDEX:=TInt64ByteIndex}
   {$define TORDINALWORDINDEX:=TInt64WordIndex}
   {$define TORDINALDWORDINDEX:=TInt64DWordIndex}
   {$define TORDINALOVERLAY:=TInt64Overlay}
   {$define TORDINALTYPESIZE8}
+{$else}
+  {$fatal Unsupported NativeInt type size}
 {$endif}
 {$i syshelpo.inc}
 {$undef TORDINALTYPESIZE2}
@@ -1716,26 +1739,26 @@ end;
 {$define TORDINALHELPER:=TNativeUIntHelper}
 {$define TORDINALTYPE:=NativeUInt}
 {$define TORDINALBITINDEX:=TNativeUIntBitIndex}
-{$ifdef cpu16}
+{$if sizeof(NativeUInt)=2}
   {$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
   {$define TORDINALBYTEINDEX:=TWordByteIndex}
   {$define TORDINALOVERLAY:=TWordOverlay}
   {$define TORDINALTYPESIZE2}
-{$endif}
-{$ifdef cpu32}
+{$elseif sizeof(NativeUInt)=4}
   {$define TORDINALNIBBLEINDEX:=TDwordNibbleIndex}
   {$define TORDINALBYTEINDEX:=TDwordByteIndex}
   {$define TORDINALWORDINDEX:=TDwordWordIndex}
   {$define TORDINALOVERLAY:=TDwordOverlay}
   {$define TORDINALTYPESIZE4}
-{$endif}
-{$ifdef cpu64}
+{$elseif sizeof(NativeUInt)=8}
   {$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
   {$define TORDINALBYTEINDEX:=TQwordByteIndex}
   {$define TORDINALWORDINDEX:=TQwordWordIndex}
   {$define TORDINALDWORDINDEX:=TQwordDWordIndex}
   {$define TORDINALOVERLAY:=TQwordOverlay}
   {$define TORDINALTYPESIZE8}
+{$else}
+  {$fatal Unsupported NativeUInt type size}
 {$endif}
 {$i syshelpo.inc}
 {$undef TORDINALTYPESIZE2}

+ 106 - 39
rtl/objpas/sysutils/syshelph.inc

@@ -10,8 +10,14 @@ Type
   TWordBitIndex = 0..15;
   TSmallIntBitIndex = 0..15;
   TCardinalBitIndex = 0..31;
+{$if sizeof(Integer)=2}
+  TIntegerBitIndex = 0..15;
+{$elseif sizeof(Integer)=4}
   TIntegerBitIndex = 0..31;
-  TLongIntBitIndex = TIntegerBitIndex;
+{$else}
+  {$fatal Unsupported Integer type size}
+{$endif}
+  TLongIntBitIndex = 0..31;
   TQwordBitIndex = 0..63;
   TInt64BitIndex = 0..63;
 {$IFDEF cpu16}
@@ -657,13 +663,22 @@ Type
   end;
 
   // TIntegerBitIndex is already defined
+{$if sizeof(Integer)=2}
+  TIntegerNibbleIndex = TWordNibbleIndex;
+  TIntegerByteIndex   = TWordByteIndex;
+
+  TIntegerOverlay = TWordOverlay;
+{$elseif sizeof(Integer)=4}
   TIntegerNibbleIndex = TDwordNibbleIndex;
   TIntegerByteIndex   = TDwordByteIndex;
   TIntegerWordIndex   = TDwordWordIndex;
 
   TIntegerOverlay = TDwordOverlay;
+{$else}
+  {$fatal Unsupported Integer type size}
+{$endif}
 
-  TIntegerHelper = Type Helper for Integer { for LongInt Type too }
+  TIntegerHelper = Type Helper for Integer
   const
     MaxValue  = High(integer);
     MinValue  = Low(integer);
@@ -673,8 +688,10 @@ Type
     MinNibble = Low(TIntegerNibbleIndex);
     MaxByte   = High(TIntegerByteIndex);
     MinByte   = Low(TIntegerByteIndex);
+{$if sizeof(Integer)>=4}
     MaxWord   = High(TIntegerWordIndex);
     MinWord   = Low(TIntegerWordIndex);
+{$endif}
   public
     Class Function Size: Integer; inline; static;
     Class Function ToString(const AValue: Integer): string; overload; inline; static;
@@ -687,8 +704,10 @@ Type
     procedure PutNibble(const aIndex: TIntegerNibbleIndex; const aNewValue: nibble); inline;
     function  GetByte(const aIndex: TIntegerByteIndex): byte;
     procedure PutByte(const aIndex: TIntegerByteIndex; const aNewValue: byte);
+{$if sizeof(Integer)>=4}
     function  GetWord(const aIndex: TIntegerWordIndex): word;
     procedure PutWord(const aIndex: TIntegerWordIndex; const aNewValue: word);
+{$endif}
   public
     Function ToBoolean: Boolean; inline;
     Function ToDouble: Double; inline;
@@ -707,9 +726,67 @@ Type
     function  LowestSetBitPos: cardinal; inline;
     function  SetBitsCount: byte; inline;
     property  Bits   [aIndex: TIntegerBitIndex]: boolean    read GetBit    write PutBit;
-    property  Nibbles[aIndex: TCardinalNibbleIndex]: nibble read GetNibble write PutNibble;
+    property  Nibbles[aIndex: TIntegerNibbleIndex]: nibble  read GetNibble write PutNibble;
     property  Bytes  [aIndex: TIntegerByteIndex]:  byte     read GetByte   write PutByte;
+{$if sizeof(Integer)>=4}
     property  Words  [aIndex: TIntegerWordIndex]:  word     read GetWord   write PutWord;
+{$endif}
+  end;
+
+  // TLongIntBitIndex is already defined
+  TLongIntNibbleIndex = TDwordNibbleIndex;
+  TLongIntByteIndex   = TDwordByteIndex;
+  TLongIntWordIndex   = TDwordWordIndex;
+
+  TLongIntOverlay = TDwordOverlay;
+
+  TLongIntHelper = Type Helper for LongInt
+  const
+    MaxValue  = High(LongInt);
+    MinValue  = Low(LongInt);
+    MaxBit    = High(TLongIntBitIndex);
+    MinBit    = Low(TLongIntBitIndex);
+    MaxNibble = High(TLongIntNibbleIndex);
+    MinNibble = Low(TLongIntNibbleIndex);
+    MaxByte   = High(TLongIntByteIndex);
+    MinByte   = Low(TLongIntByteIndex);
+    MaxWord   = High(TLongIntWordIndex);
+    MinWord   = Low(TLongIntWordIndex);
+  public
+    Class Function Size: Integer; inline; static;
+    Class Function ToString(const AValue: LongInt): string; overload; inline; static;
+    Class Function Parse(const AString: string): LongInt; inline; static;
+    Class Function TryParse(const AString: string; out AValue: LongInt): Boolean; inline; static;
+  protected
+    function  GetBit(const aIndex: TLongIntBitIndex): boolean; inline;
+    procedure PutBit(const aIndex: TLongIntBitIndex; const aNewValue: boolean); inline;
+    function  GetNibble(const aIndex: TLongIntNibbleIndex): nibble; inline;
+    procedure PutNibble(const aIndex: TLongIntNibbleIndex; const aNewValue: nibble); inline;
+    function  GetByte(const aIndex: TLongIntByteIndex): byte;
+    procedure PutByte(const aIndex: TLongIntByteIndex; const aNewValue: byte);
+    function  GetWord(const aIndex: TLongIntWordIndex): word;
+    procedure PutWord(const aIndex: TLongIntWordIndex; const aNewValue: word);
+  public
+    Function ToBoolean: Boolean; inline;
+    Function ToDouble: Double; inline;
+    Function ToExtended: Extended; inline;
+    Function ToBinString:string; inline;
+    Function ToHexString(const AMinDigits: Integer): string; overload; inline;
+    Function ToHexString: string; overload; inline;
+    Function ToSingle: Single; inline;
+    Function ToString: string; overload; inline;
+    Function SetBit(const Index: TLongIntBitIndex) : LongInt; inline;
+    Function ClearBit(const Index: TLongIntBitIndex) : LongInt; inline;
+    Function ToggleBit(const Index: TLongIntBitIndex) : LongInt; inline;
+    Function TestBit(const Index:TLongIntBitIndex):Boolean; inline;   
+    procedure Clear; inline;
+    function  HighestSetBitPos: cardinal; inline;
+    function  LowestSetBitPos: cardinal; inline;
+    function  SetBitsCount: byte; inline;
+    property  Bits   [aIndex: TLongIntBitIndex]: boolean    read GetBit    write PutBit;
+    property  Nibbles[aIndex: TLongIntNibbleIndex]: nibble  read GetNibble write PutNibble;
+    property  Bytes  [aIndex: TLongIntByteIndex]:  byte     read GetByte   write PutByte;
+    property  Words  [aIndex: TLongIntWordIndex]:  word     read GetWord   write PutWord;
   end;
 
   // TQwordBitIndex is already defined
@@ -848,23 +925,23 @@ Type
   end;
 
   // TNativeIntBitIndex is already defined
-  {$ifdef cpu16}
+  {$if sizeof(NativeInt)=2}
     TNativeIntNibbleIndex = TSmallIntNibbleIndex;
     TNativeIntByteIndex   = TSmallIntByteIndex;
     TNativeIntOverlay     = TSmallIntOverlay;
-  {$endif}
-  {$ifdef cpu32}
-    TNativeIntNibbleIndex = TIntegerNibbleIndex;
-    TNativeIntByteIndex   = TIntegerByteIndex;
-    TNativeIntWordIndex   = TIntegerWordIndex;
-    TNativeIntOverlay     = TIntegerOverlay;
-  {$endif}
-  {$ifdef cpu64}
+  {$elseif sizeof(NativeInt)=4}
+    TNativeIntNibbleIndex = TLongIntNibbleIndex;
+    TNativeIntByteIndex   = TLongIntByteIndex;
+    TNativeIntWordIndex   = TLongIntWordIndex;
+    TNativeIntOverlay     = TLongIntOverlay;
+  {$elseif sizeof(NativeInt)=8}
     TNativeIntNibbleIndex = TInt64NibbleIndex;
     TNativeIntByteIndex   = TInt64ByteIndex;
     TNativeIntWordIndex   = TInt64WordIndex;
     TNativeIntDwordIndex  = TInt64DWordIndex;
     TNativeIntOverlay     = TInt64Overlay;
+  {$else}
+    {$fatal Unsupported NativeInt type size}
   {$endif}
 
   TNativeIntHelper = Type Helper for NativeInt
@@ -878,13 +955,11 @@ Type
     // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need MaxByte and MinByte
     MaxByte   = High(TNativeIntByteIndex);
     MinByte   = Low(TNativeIntByteIndex);
-    {$ifdef cpu32}
+    {$if sizeof(NativeInt)>=4}
     MaxWord   = High(TNativeIntWordIndex);
     MinWord   = Low(TNativeIntWordIndex);
     {$endif}
-    {$ifdef cpu64}
-    MaxWord   = High(TNativeIntWordIndex);
-    MinWord   = Low(TNativeIntWordIndex);
+    {$ifdef sizeof(NativeInt)>=8}
     MaxDword  = High(TNativeIntDwordIndex);
     MinDword  = Low(TNativeIntDwordIndex);
     {$endif}
@@ -901,13 +976,11 @@ Type
     // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need GetByte and PutByte
     function  GetByte(const aIndex: TNativeIntByteIndex): byte;
     procedure PutByte(const aIndex: TNativeIntByteIndex; const aNewValue: byte);
-    {$ifdef cpu32}
+    {$if sizeof(NativeInt)>=4}
     function  GetWord(const aIndex: TNativeIntWordIndex): word;
     procedure PutWord(const aIndex: TNativeIntWordIndex; const aNewValue: word);
     {$endif}
-    {$ifdef cpu64}
-    function  GetWord(const aIndex: TNativeIntWordIndex): word;
-    procedure PutWord(const aIndex: TNativeIntWordIndex; const aNewValue: word);
+    {$if sizeof(NativeInt)>=8}
     function  GetDword(const aIndex: TNativeIntDwordIndex): dword;
     procedure PutDword(const aIndex: TNativeIntDwordIndex; const aNewValue: dword);
     {$endif}
@@ -932,33 +1005,32 @@ Type
     property  Nibbles [aIndex: TNativeIntNibbleIndex]: nibble read GetNibble write PutNibble;
     // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need Byte[]
     property  Bytes [aIndex: TNativeIntByteIndex]: byte     read GetByte   write PutByte;
-    {$ifdef cpu32}
+    {$if sizeof(NativeInt)>=4}
     property  Words [aIndex: TNativeIntWordIndex]: word     read GetWord   write PutWord;
     {$endif}
-    {$ifdef cpu64}
-    property  Words [aIndex: TNativeIntWordIndex]: word     read GetWord   write PutWord;
+    {$if sizeof(NativeInt)>=8}
     property  DWords [aIndex: TNativeIntDwordIndex]: dword   read GetDWord  write PutDWord;
     {$endif}
   end;
 
   // TNativeUIntBitIndex is already defined
-  {$ifdef cpu16}
+  {$if sizeof(NativeUInt)=2}
     TNativeUIntNibbleIndex = TWordNibbleIndex;
     TNativeUIntByteIndex   = TWordByteIndex;
     TNativeUIntOverlay     = TWordOverlay;
-  {$endif}
-  {$ifdef cpu32}
+  {$elseif sizeof(NativeUInt)=4}
     TNativeUIntNibbleIndex = TDwordNibbleIndex;
     TNativeUIntByteIndex   = TDwordByteIndex;
     TNativeUIntWordIndex   = TDwordWordIndex;
     TNativeUIntOverlay     = TDwordOverlay;
-  {$endif}
-  {$ifdef cpu64}
+  {$elseif sizeof(NativeUInt)=8}
     TNativeUIntNibbleIndex = TQwordNibbleIndex;
     TNativeUIntByteIndex   = TQwordByteIndex;
     TNativeUIntWordIndex   = TQwordWordIndex;
     TNativeUIntDwordIndex  = TQwordDWordIndex;
     TNativeUIntOverlay     = TQwordOverlay;
+  {$else}
+    {$fatal Unsupported NativeUInt type size}
   {$endif}
 
   TNativeUIntHelper = Type Helper for NativeUInt
@@ -972,13 +1044,11 @@ Type
     // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need MaxByte and MinByte
     MaxByte   = High(TNativeUIntByteIndex);
     MinByte   = Low(TNativeUIntByteIndex);
-    {$ifdef cpu32}
+    {$if sizeof(NativeUInt)>=4}
     MaxWord   = High(TNativeUIntWordIndex);
     MinWord   = Low(TNativeUIntWordIndex);
     {$endif}
-    {$ifdef cpu64}
-    MaxWord   = High(TNativeUIntWordIndex);
-    MinWord   = Low(TNativeUIntWordIndex);
+    {$if sizeof(NativeUInt)>=8}
     MaxDword  = High(TNativeUIntDwordIndex);
     MinDword  = Low(TNativeUIntDwordIndex);
     {$endif}
@@ -995,13 +1065,11 @@ Type
     // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need GetByte and PutByte
     function  GetByte(const aIndex: TNativeUIntByteIndex): byte;
     procedure PutByte(const aIndex: TNativeUIntByteIndex; const aNewValue: byte);
-    {$ifdef cpu32}
+    {$if sizeof(NativeUInt)>=4}
     function  GetWord(const aIndex: TNativeUIntWordIndex): word;
     procedure PutWord(const aIndex: TNativeUIntWordIndex; const aNewValue: word);
     {$endif}
-    {$ifdef cpu64}
-    function  GetWord(const aIndex: TNativeUIntWordIndex): word;
-    procedure PutWord(const aIndex: TNativeUIntWordIndex; const aNewValue: word);
+    {$if sizeof(NativeUInt)>=8}
     function  GetDword(const aIndex: TNativeUIntDwordIndex): dword;
     procedure PutDword(const aIndex: TNativeUIntDwordIndex; const aNewValue: dword);
     {$endif}
@@ -1026,11 +1094,10 @@ Type
     property  Nibbles [aIndex: TNativeUIntNibbleIndex]: nibble read GetNibble write PutNibble;
     // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need Byte[]
     property  Bytes [aIndex: TNativeUIntByteIndex]: byte     read GetByte   write PutByte;
-    {$ifdef cpu32}
+    {$if sizeof(NativeUInt)>=4}
     property  Words [aIndex: TNativeUIntWordIndex]: word     read GetWord   write PutWord;
     {$endif}
-    {$ifdef cpu64}
-    property  Words [aIndex: TNativeUIntWordIndex]: word     read GetWord   write PutWord;
+    {$if sizeof(NativeUInt)>=8}
     property  DWords [aIndex: TNativeUIntDwordIndex]: dword   read GetDWord  write PutDWord;
     {$endif}
   end;