cgutils.pas 7.6 KB

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