cgutils.pas 7.6 KB

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