cgutils.pas 7.5 KB

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