cgutils.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 cpu64bitalu}
  95. 1 : (value : Int64);
  96. {$else cpu64bitalu}
  97. {$ifdef FPC_BIG_ENDIAN}
  98. 1 : (_valuedummy,value : longint);
  99. {$else FPC_BIG_ENDIAN}
  100. 1 : (value : longint);
  101. {$endif FPC_BIG_ENDIAN}
  102. {$endif cpu64bitalu}
  103. 2 : (value64 : Int64);
  104. );
  105. LOC_CREFERENCE,
  106. LOC_REFERENCE : (reference : treference);
  107. { segment in reference at the same place as in loc_register }
  108. LOC_REGISTER,
  109. LOC_CREGISTER : (
  110. case longint of
  111. 1 : (register : tregister;
  112. { some x86_64 targets require two function result registers }
  113. registerhi : tregister;
  114. {$ifdef m68k}
  115. { some m68k OSes require that the result is returned in d0 and a0
  116. the second location must be stored here }
  117. registeralias : tregister;
  118. {$endif m68k}
  119. );
  120. {$ifdef cpu64bitalu}
  121. { overlay a 128 Bit register type }
  122. 2 : (register128 : tregister128);
  123. {$else cpu64bitalu}
  124. { overlay a 64 Bit register type }
  125. 2 : (register64 : tregister64);
  126. {$endif cpu64bitalu}
  127. {$ifdef avr}
  128. 3 : (registers : array[0..3] of tregister);
  129. {$endif avr}
  130. );
  131. LOC_SUBSETREG,
  132. LOC_CSUBSETREG : (
  133. sreg: tsubsetregister;
  134. );
  135. LOC_SUBSETREF : (
  136. sref: tsubsetreference;
  137. )
  138. end;
  139. { trerefence handling }
  140. {# Clear to zero a treference }
  141. procedure reference_reset(var ref : treference; alignment: longint);
  142. {# Clear to zero a treference, and set is base address
  143. to base register.
  144. }
  145. procedure reference_reset_base(var ref : treference;base : tregister;offset, alignment : longint);
  146. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset, alignment : longint);
  147. { This routine verifies if two references are the same, and
  148. if so, returns TRUE, otherwise returns false.
  149. }
  150. function references_equal(const sref,dref : treference) : boolean;
  151. { tlocation handling }
  152. { cannot be used for loc_(c)reference, because that one requires an alignment }
  153. procedure location_reset(var l : tlocation;lt:TCGNonRefLoc;lsize:TCGSize);
  154. { for loc_(c)reference }
  155. procedure location_reset_ref(var l : tlocation;lt:TCGRefLoc;lsize:TCGSize; alignment: longint);
  156. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  157. procedure location_swap(var destloc,sourceloc : tlocation);
  158. { returns r with the given alignment }
  159. function setalignment(const r : treference;b : byte) : treference;
  160. implementation
  161. uses
  162. systems,
  163. verbose;
  164. {****************************************************************************
  165. TReference
  166. ****************************************************************************}
  167. procedure reference_reset(var ref : treference; alignment: longint);
  168. begin
  169. FillChar(ref,sizeof(treference),0);
  170. {$ifdef arm}
  171. ref.signindex:=1;
  172. {$endif arm}
  173. ref.alignment:=alignment;
  174. end;
  175. procedure reference_reset_base(var ref : treference;base : tregister;offset, alignment : longint);
  176. begin
  177. reference_reset(ref,alignment);
  178. ref.base:=base;
  179. ref.offset:=offset;
  180. end;
  181. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset, alignment : longint);
  182. begin
  183. reference_reset(ref,alignment);
  184. ref.symbol:=sym;
  185. ref.offset:=offset;
  186. end;
  187. function references_equal(const sref,dref : treference):boolean;
  188. begin
  189. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  190. end;
  191. { returns r with the given alignment }
  192. function setalignment(const r : treference;b : byte) : treference;
  193. begin
  194. result:=r;
  195. result.alignment:=b;
  196. end;
  197. {****************************************************************************
  198. TLocation
  199. ****************************************************************************}
  200. procedure location_reset(var l : tlocation;lt:TCGNonRefLoc;lsize:TCGSize);
  201. begin
  202. FillChar(l,sizeof(tlocation),0);
  203. l.loc:=lt;
  204. l.size:=lsize;
  205. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  206. { call location_reset_ref instead }
  207. internalerror(2009020705);
  208. end;
  209. procedure location_reset_ref(var l: tlocation; lt: tcgrefloc; lsize: tcgsize;
  210. alignment: longint);
  211. begin
  212. FillChar(l,sizeof(tlocation),0);
  213. l.loc:=lt;
  214. l.size:=lsize;
  215. {$ifdef arm}
  216. l.reference.signindex:=1;
  217. {$endif arm}
  218. l.reference.alignment:=alignment;
  219. end;
  220. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  221. begin
  222. destloc:=sourceloc;
  223. end;
  224. procedure location_swap(var destloc,sourceloc : tlocation);
  225. var
  226. swapl : tlocation;
  227. begin
  228. swapl := destloc;
  229. destloc := sourceloc;
  230. sourceloc := swapl;
  231. end;
  232. end.