cgutils.pas 6.1 KB

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