cgutils.pas 6.9 KB

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