cgutils.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. segment,
  34. base,
  35. index : tregister;
  36. refaddr : trefaddr;
  37. scalefactor : byte;
  38. {$ifdef arm}
  39. symboldata : tlinkedlistitem;
  40. signindex : shortint;
  41. shiftimm : byte;
  42. addressmode : taddressmode;
  43. shiftmode : tshiftmode;
  44. {$endif arm}
  45. {$ifdef m68k}
  46. { indexed increment and decrement mode }
  47. { (An)+ and -(An) }
  48. direction : tdirection;
  49. {$endif m68k}
  50. {$ifdef SUPPORT_UNALIGNED}
  51. alignment : byte;
  52. {$endif SUPPORT_UNALIGNED}
  53. end;
  54. tlocation = record
  55. loc : TCGLoc;
  56. size : TCGSize;
  57. case TCGLoc of
  58. LOC_FLAGS : (resflags : tresflags);
  59. LOC_CONSTANT : (
  60. case longint of
  61. {$ifdef FPC_BIG_ENDIAN}
  62. 1 : (_valuedummy,value : aint);
  63. {$else FPC_BIG_ENDIAN}
  64. 1 : (value : aint);
  65. {$endif FPC_BIG_ENDIAN}
  66. 2 : (value64 : Int64);
  67. );
  68. LOC_CREFERENCE,
  69. LOC_REFERENCE : (reference : treference);
  70. { segment in reference at the same place as in loc_register }
  71. LOC_REGISTER,
  72. LOC_CREGISTER : (
  73. case longint of
  74. 1 : (register : tregister;
  75. {$ifdef m68k}
  76. { some m68k OSes require that the result is returned in d0 and a0
  77. the second location must be stored here }
  78. registeralias : tregister;
  79. {$endif m68k}
  80. );
  81. {$ifndef cpu64bit}
  82. { overlay a 64 Bit register type }
  83. 2 : (register64 : tregister64);
  84. {$endif cpu64bit}
  85. );
  86. end;
  87. { trerefence handling }
  88. {# Clear to zero a treference }
  89. procedure reference_reset(var ref : treference);
  90. {# Clear to zero a treference, and set is base address
  91. to base register.
  92. }
  93. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  94. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  95. { This routine verifies if two references are the same, and
  96. if so, returns TRUE, otherwise returns false.
  97. }
  98. function references_equal(const sref,dref : treference) : boolean;
  99. { tlocation handling }
  100. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  101. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  102. procedure location_swap(var destloc,sourceloc : tlocation);
  103. { allocate room for parameters on the stack in the entry code? }
  104. function use_fixed_stack: boolean;
  105. implementation
  106. uses
  107. systems;
  108. {****************************************************************************
  109. TReference
  110. ****************************************************************************}
  111. procedure reference_reset(var ref : treference);
  112. begin
  113. FillChar(ref,sizeof(treference),0);
  114. {$ifdef arm}
  115. ref.signindex:=1;
  116. {$endif arm}
  117. end;
  118. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  119. begin
  120. reference_reset(ref);
  121. ref.base:=base;
  122. ref.offset:=offset;
  123. end;
  124. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  125. begin
  126. reference_reset(ref);
  127. ref.symbol:=sym;
  128. ref.offset:=offset;
  129. end;
  130. function references_equal(const sref,dref : treference):boolean;
  131. begin
  132. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  133. end;
  134. {****************************************************************************
  135. TLocation
  136. ****************************************************************************}
  137. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  138. begin
  139. FillChar(l,sizeof(tlocation),0);
  140. l.loc:=lt;
  141. l.size:=lsize;
  142. {$ifdef arm}
  143. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  144. l.reference.signindex:=1;
  145. {$endif arm}
  146. end;
  147. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  148. begin
  149. destloc:=sourceloc;
  150. end;
  151. procedure location_swap(var destloc,sourceloc : tlocation);
  152. var
  153. swapl : tlocation;
  154. begin
  155. swapl := destloc;
  156. destloc := sourceloc;
  157. sourceloc := swapl;
  158. end;
  159. function use_fixed_stack: boolean;
  160. begin
  161. {$ifdef i386}
  162. result := (target_info.system = system_i386_darwin);
  163. {$else i386}
  164. {$ifdef cputargethasfixedstack}
  165. result := true;
  166. {$else cputargethasfixedstack}
  167. result := false;
  168. {$endif cputargethasfixedstack}
  169. {$endif i386}
  170. end;
  171. end.