cgutils.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. const
  27. { implementation of max function using only functionality that can be
  28. evaluated as a constant expression by the compiler -- this is
  29. basically maxcpureg = max(max(first_int_imreg,first_fpu_imreg),first_mm_imreg)-1 }
  30. tmpmaxcpufpuintreg = first_int_imreg + ((first_fpu_imreg - first_int_imreg) * ord(first_int_imreg < first_fpu_imreg));
  31. maxcpuregister = (tmpmaxcpufpuintreg + ((first_mm_imreg - tmpmaxcpufpuintreg) * ord(tmpmaxcpufpuintreg < first_mm_imreg)))-1;
  32. type
  33. { Set type definition for cpuregisters }
  34. tcpuregisterset = set of 0..maxcpuregister;
  35. {$ifdef jvm}
  36. tarrayreftype = (art_none,art_indexreg,art_indexref,art_indexconst);
  37. {$endif jvm}
  38. { reference record, reordered for best alignment }
  39. preference = ^treference;
  40. treference = record
  41. offset : asizeint;
  42. symbol,
  43. relsymbol : tasmsymbol;
  44. {$if defined(x86) or defined(m68k)}
  45. segment,
  46. {$endif defined(x86) or defined(m68k)}
  47. base,
  48. index : tregister;
  49. refaddr : trefaddr;
  50. scalefactor : byte;
  51. {$ifdef arm}
  52. symboldata : tlinkedlistitem;
  53. signindex : shortint;
  54. shiftimm : byte;
  55. addressmode : taddressmode;
  56. shiftmode : tshiftmode;
  57. {$endif arm}
  58. {$ifdef avr}
  59. addressmode : taddressmode;
  60. {$endif avr}
  61. {$ifdef m68k}
  62. { indexed increment and decrement mode }
  63. { (An)+ and -(An) }
  64. direction : tdirection;
  65. {$endif m68k}
  66. {$ifdef jvm}
  67. arrayreftype: tarrayreftype;
  68. indexbase: tregister;
  69. indexsymbol: tasmsymbol;
  70. indexoffset: aint;
  71. checkcast: boolean;
  72. {$endif jvm}
  73. alignment : byte;
  74. end;
  75. tsubsetregister = record
  76. subsetreg : tregister;
  77. startbit, bitlen: byte;
  78. subsetregsize: tcgsize;
  79. end;
  80. tsubsetreference = record
  81. ref: treference;
  82. bitindexreg: tregister;
  83. startbit, bitlen: byte;
  84. end;
  85. tlocation = record
  86. loc : TCGLoc;
  87. size : TCGSize;
  88. case TCGLoc of
  89. {$ifdef cpuflags}
  90. LOC_FLAGS : (resflags : tresflags);
  91. {$endif cpuflags}
  92. LOC_CONSTANT : (
  93. case longint of
  94. {$ifdef FPC_BIG_ENDIAN}
  95. 1 : (_valuedummy,value : aint);
  96. {$else FPC_BIG_ENDIAN}
  97. 1 : (value : aint);
  98. {$endif FPC_BIG_ENDIAN}
  99. 2 : (value64 : Int64);
  100. );
  101. LOC_CREFERENCE,
  102. LOC_REFERENCE : (reference : treference);
  103. { segment in reference at the same place as in loc_register }
  104. LOC_REGISTER,
  105. LOC_CREGISTER : (
  106. case longint of
  107. 1 : (register : tregister;
  108. { some x86_64 targets require two function result registers }
  109. registerhi : tregister;
  110. {$ifdef m68k}
  111. { some m68k OSes require that the result is returned in d0 and a0
  112. the second location must be stored here }
  113. registeralias : tregister;
  114. {$endif m68k}
  115. );
  116. {$ifdef cpu64bitalu}
  117. { overlay a 128 Bit register type }
  118. 2 : (register128 : tregister128);
  119. {$else cpu64bitalu}
  120. { overlay a 64 Bit register type }
  121. 2 : (register64 : tregister64);
  122. {$endif cpu64bitalu}
  123. {$ifdef avr}
  124. 3 : (registers : array[0..3] of tregister);
  125. {$endif avr}
  126. );
  127. LOC_SUBSETREG,
  128. LOC_CSUBSETREG : (
  129. sreg: tsubsetregister;
  130. );
  131. LOC_SUBSETREF : (
  132. sref: tsubsetreference;
  133. )
  134. end;
  135. { trerefence handling }
  136. {# Clear to zero a treference }
  137. procedure reference_reset(var ref : treference; alignment: longint);
  138. {# Clear to zero a treference, and set is base address
  139. to base register.
  140. }
  141. procedure reference_reset_base(var ref : treference;base : tregister;offset, alignment : longint);
  142. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset, alignment : longint);
  143. { This routine verifies if two references are the same, and
  144. if so, returns TRUE, otherwise returns false.
  145. }
  146. function references_equal(const sref,dref : treference) : boolean;
  147. { tlocation handling }
  148. { cannot be used for loc_(c)reference, because that one requires an alignment }
  149. procedure location_reset(var l : tlocation;lt:TCGNonRefLoc;lsize:TCGSize);
  150. { for loc_(c)reference }
  151. procedure location_reset_ref(var l : tlocation;lt:TCGRefLoc;lsize:TCGSize; alignment: longint);
  152. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  153. procedure location_swap(var destloc,sourceloc : tlocation);
  154. { returns r with the given alignment }
  155. function setalignment(const r : treference;b : byte) : treference;
  156. implementation
  157. uses
  158. systems,
  159. verbose;
  160. {****************************************************************************
  161. TReference
  162. ****************************************************************************}
  163. procedure reference_reset(var ref : treference; alignment: longint);
  164. begin
  165. FillChar(ref,sizeof(treference),0);
  166. {$ifdef arm}
  167. ref.signindex:=1;
  168. {$endif arm}
  169. ref.alignment:=alignment;
  170. end;
  171. procedure reference_reset_base(var ref : treference;base : tregister;offset, alignment : longint);
  172. begin
  173. reference_reset(ref,alignment);
  174. ref.base:=base;
  175. ref.offset:=offset;
  176. end;
  177. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset, alignment : longint);
  178. begin
  179. reference_reset(ref,alignment);
  180. ref.symbol:=sym;
  181. ref.offset:=offset;
  182. end;
  183. function references_equal(const sref,dref : treference):boolean;
  184. begin
  185. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  186. end;
  187. { returns r with the given alignment }
  188. function setalignment(const r : treference;b : byte) : treference;
  189. begin
  190. result:=r;
  191. result.alignment:=b;
  192. end;
  193. {****************************************************************************
  194. TLocation
  195. ****************************************************************************}
  196. procedure location_reset(var l : tlocation;lt:TCGNonRefLoc;lsize:TCGSize);
  197. begin
  198. FillChar(l,sizeof(tlocation),0);
  199. l.loc:=lt;
  200. l.size:=lsize;
  201. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  202. { call location_reset_ref instead }
  203. internalerror(2009020705);
  204. end;
  205. procedure location_reset_ref(var l: tlocation; lt: tcgrefloc; lsize: tcgsize;
  206. alignment: longint);
  207. begin
  208. FillChar(l,sizeof(tlocation),0);
  209. l.loc:=lt;
  210. l.size:=lsize;
  211. {$ifdef arm}
  212. l.reference.signindex:=1;
  213. {$endif arm}
  214. l.reference.alignment:=alignment;
  215. end;
  216. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  217. begin
  218. destloc:=sourceloc;
  219. end;
  220. procedure location_swap(var destloc,sourceloc : tlocation);
  221. var
  222. swapl : tlocation;
  223. begin
  224. swapl := destloc;
  225. destloc := sourceloc;
  226. sourceloc := swapl;
  227. end;
  228. end.