cgutils.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. {
  2. $Id$
  3. Copyright (c) 1998-2004 by Florian Klaempfl
  4. Some basic types and constants for the code generation
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit exports some helper routines which are used across the code generator }
  19. unit cgutils;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. cclasses,
  25. aasmbase,
  26. cpubase,cgbase;
  27. type
  28. { reference record, reordered for best alignment }
  29. preference = ^treference;
  30. treference = record
  31. offset : aint;
  32. symbol,
  33. relsymbol : tasmsymbol;
  34. segment,
  35. base,
  36. index : tregister;
  37. refaddr : trefaddr;
  38. scalefactor : byte;
  39. {$ifdef arm}
  40. symboldata : tlinkedlistitem;
  41. signindex : shortint;
  42. shiftimm : byte;
  43. addressmode : taddressmode;
  44. shiftmode : tshiftmode;
  45. {$endif arm}
  46. {$ifdef m68k}
  47. { indexed increment and decrement mode }
  48. { (An)+ and -(An) }
  49. direction : tdirection;
  50. {$endif m68k}
  51. end;
  52. tlocation = record
  53. loc : TCGLoc;
  54. size : TCGSize;
  55. case TCGLoc of
  56. LOC_FLAGS : (resflags : tresflags);
  57. LOC_CONSTANT : (
  58. case longint of
  59. {$ifdef FPC_BIG_ENDIAN}
  60. 1 : (_valuedummy,value : aint);
  61. {$else FPC_BIG_ENDIAN}
  62. 1 : (value : aint);
  63. {$endif FPC_BIG_ENDIAN}
  64. 2 : (value64 : Int64);
  65. );
  66. LOC_CREFERENCE,
  67. LOC_REFERENCE : (reference : treference);
  68. { segment in reference at the same place as in loc_register }
  69. LOC_REGISTER,
  70. LOC_CREGISTER : (
  71. case longint of
  72. 1 : (register : tregister;
  73. {$ifdef m68k}
  74. { some m68k OSes require that the result is returned in d0 and a0
  75. the second location must be stored here }
  76. registeralias : tregister;
  77. {$endif m68k}
  78. );
  79. {$ifndef cpu64bit}
  80. { overlay a 64 Bit register type }
  81. 2 : (register64 : tregister64);
  82. {$endif cpu64bit}
  83. );
  84. end;
  85. { trerefence handling }
  86. {# Clear to zero a treference }
  87. procedure reference_reset(var ref : treference);
  88. {# Clear to zero a treference, and set is base address
  89. to base register.
  90. }
  91. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  92. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  93. { This routine verifies if two references are the same, and
  94. if so, returns TRUE, otherwise returns false.
  95. }
  96. function references_equal(sref : treference;dref : treference) : boolean;
  97. { tlocation handling }
  98. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  99. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  100. procedure location_swap(var destloc,sourceloc : tlocation);
  101. implementation
  102. {****************************************************************************
  103. TReference
  104. ****************************************************************************}
  105. procedure reference_reset(var ref : treference);
  106. begin
  107. FillChar(ref,sizeof(treference),0);
  108. {$ifdef arm}
  109. ref.signindex:=1;
  110. {$endif arm}
  111. end;
  112. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  113. begin
  114. reference_reset(ref);
  115. ref.base:=base;
  116. ref.offset:=offset;
  117. end;
  118. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  119. begin
  120. reference_reset(ref);
  121. ref.symbol:=sym;
  122. ref.offset:=offset;
  123. end;
  124. function references_equal(sref : treference;dref : treference):boolean;
  125. begin
  126. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  127. end;
  128. {****************************************************************************
  129. TLocation
  130. ****************************************************************************}
  131. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  132. begin
  133. FillChar(l,sizeof(tlocation),0);
  134. l.loc:=lt;
  135. l.size:=lsize;
  136. {$ifdef arm}
  137. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  138. l.reference.signindex:=1;
  139. {$endif arm}
  140. end;
  141. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  142. begin
  143. destloc:=sourceloc;
  144. end;
  145. procedure location_swap(var destloc,sourceloc : tlocation);
  146. var
  147. swapl : tlocation;
  148. begin
  149. swapl := destloc;
  150. destloc := sourceloc;
  151. sourceloc := swapl;
  152. end;
  153. end.
  154. {
  155. $Log$
  156. Revision 1.6 2005-02-14 17:13:06 peter
  157. * truncate log
  158. }