cgutils.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. tsubsetregister = record
  55. subsetreg : tregister;
  56. startbit, bitlen: byte;
  57. subsetregsize: tcgsize;
  58. end;
  59. tsubsetreference = record
  60. ref: treference;
  61. bitindexreg: tregister;
  62. startbit, bitlen: byte;
  63. end;
  64. tlocation = record
  65. loc : TCGLoc;
  66. size : TCGSize;
  67. case TCGLoc of
  68. LOC_FLAGS : (resflags : tresflags);
  69. LOC_CONSTANT : (
  70. case longint of
  71. {$ifdef FPC_BIG_ENDIAN}
  72. 1 : (_valuedummy,value : aint);
  73. {$else FPC_BIG_ENDIAN}
  74. 1 : (value : aint);
  75. {$endif FPC_BIG_ENDIAN}
  76. 2 : (value64 : Int64);
  77. );
  78. LOC_CREFERENCE,
  79. LOC_REFERENCE : (reference : treference);
  80. { segment in reference at the same place as in loc_register }
  81. LOC_REGISTER,
  82. LOC_CREGISTER : (
  83. case longint of
  84. 1 : (register : tregister;
  85. {$ifdef m68k}
  86. { some m68k OSes require that the result is returned in d0 and a0
  87. the second location must be stored here }
  88. registeralias : tregister;
  89. {$endif m68k}
  90. );
  91. {$ifndef cpu64bit}
  92. { overlay a 64 Bit register type }
  93. 2 : (register64 : tregister64);
  94. {$endif cpu64bit}
  95. );
  96. LOC_SUBSETREG,
  97. LOC_CSUBSETREG : (
  98. sreg: tsubsetregister;
  99. );
  100. LOC_SUBSETREF : (
  101. sref: tsubsetreference;
  102. )
  103. end;
  104. { trerefence handling }
  105. {# Clear to zero a treference }
  106. procedure reference_reset(var ref : treference);
  107. {# Clear to zero a treference, and set is base address
  108. to base register.
  109. }
  110. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  111. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  112. { This routine verifies if two references are the same, and
  113. if so, returns TRUE, otherwise returns false.
  114. }
  115. function references_equal(const sref,dref : treference) : boolean;
  116. { tlocation handling }
  117. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  118. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  119. procedure location_swap(var destloc,sourceloc : tlocation);
  120. { allocate room for parameters on the stack in the entry code? }
  121. function use_fixed_stack: boolean;
  122. implementation
  123. uses
  124. systems;
  125. {****************************************************************************
  126. TReference
  127. ****************************************************************************}
  128. procedure reference_reset(var ref : treference);
  129. begin
  130. FillChar(ref,sizeof(treference),0);
  131. {$ifdef arm}
  132. ref.signindex:=1;
  133. {$endif arm}
  134. end;
  135. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  136. begin
  137. reference_reset(ref);
  138. ref.base:=base;
  139. ref.offset:=offset;
  140. end;
  141. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  142. begin
  143. reference_reset(ref);
  144. ref.symbol:=sym;
  145. ref.offset:=offset;
  146. end;
  147. function references_equal(const sref,dref : treference):boolean;
  148. begin
  149. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  150. end;
  151. {****************************************************************************
  152. TLocation
  153. ****************************************************************************}
  154. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  155. begin
  156. FillChar(l,sizeof(tlocation),0);
  157. l.loc:=lt;
  158. l.size:=lsize;
  159. {$ifdef arm}
  160. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  161. l.reference.signindex:=1;
  162. {$endif arm}
  163. end;
  164. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  165. begin
  166. destloc:=sourceloc;
  167. end;
  168. procedure location_swap(var destloc,sourceloc : tlocation);
  169. var
  170. swapl : tlocation;
  171. begin
  172. swapl := destloc;
  173. destloc := sourceloc;
  174. sourceloc := swapl;
  175. end;
  176. function use_fixed_stack: boolean;
  177. begin
  178. {$ifdef i386}
  179. result := (target_info.system in [system_i386_darwin,system_x86_64_darwin]);
  180. {$else i386}
  181. {$ifdef cputargethasfixedstack}
  182. result := true;
  183. {$else cputargethasfixedstack}
  184. result := false;
  185. {$endif cputargethasfixedstack}
  186. {$endif i386}
  187. end;
  188. end.