2
0

cgutils.pas 7.7 KB

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