2
0

cgutils.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 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. { 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. end.