cgutils.pas 7.7 KB

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