cgutils.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. end;
  51. tlocation = record
  52. loc : TCGLoc;
  53. size : TCGSize;
  54. case TCGLoc of
  55. LOC_FLAGS : (resflags : tresflags);
  56. LOC_CONSTANT : (
  57. case longint of
  58. {$ifdef FPC_BIG_ENDIAN}
  59. 1 : (_valuedummy,value : aint);
  60. {$else FPC_BIG_ENDIAN}
  61. 1 : (value : aint);
  62. {$endif FPC_BIG_ENDIAN}
  63. 2 : (value64 : Int64);
  64. );
  65. LOC_CREFERENCE,
  66. LOC_REFERENCE : (reference : treference);
  67. { segment in reference at the same place as in loc_register }
  68. LOC_REGISTER,
  69. LOC_CREGISTER : (
  70. case longint of
  71. 1 : (register : tregister;
  72. {$ifdef m68k}
  73. { some m68k OSes require that the result is returned in d0 and a0
  74. the second location must be stored here }
  75. registeralias : tregister;
  76. {$endif m68k}
  77. );
  78. {$ifndef cpu64bit}
  79. { overlay a 64 Bit register type }
  80. 2 : (register64 : tregister64);
  81. {$endif cpu64bit}
  82. );
  83. end;
  84. { trerefence handling }
  85. {# Clear to zero a treference }
  86. procedure reference_reset(var ref : treference);
  87. {# Clear to zero a treference, and set is base address
  88. to base register.
  89. }
  90. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  91. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  92. { This routine verifies if two references are the same, and
  93. if so, returns TRUE, otherwise returns false.
  94. }
  95. function references_equal(const sref,dref : treference) : boolean;
  96. { tlocation handling }
  97. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  98. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  99. procedure location_swap(var destloc,sourceloc : tlocation);
  100. implementation
  101. {****************************************************************************
  102. TReference
  103. ****************************************************************************}
  104. procedure reference_reset(var ref : treference);
  105. begin
  106. FillChar(ref,sizeof(treference),0);
  107. {$ifdef arm}
  108. ref.signindex:=1;
  109. {$endif arm}
  110. end;
  111. procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
  112. begin
  113. reference_reset(ref);
  114. ref.base:=base;
  115. ref.offset:=offset;
  116. end;
  117. procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
  118. begin
  119. reference_reset(ref);
  120. ref.symbol:=sym;
  121. ref.offset:=offset;
  122. end;
  123. function references_equal(const sref,dref : treference):boolean;
  124. begin
  125. references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
  126. end;
  127. {****************************************************************************
  128. TLocation
  129. ****************************************************************************}
  130. procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
  131. begin
  132. FillChar(l,sizeof(tlocation),0);
  133. l.loc:=lt;
  134. l.size:=lsize;
  135. {$ifdef arm}
  136. if l.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  137. l.reference.signindex:=1;
  138. {$endif arm}
  139. end;
  140. procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
  141. begin
  142. destloc:=sourceloc;
  143. end;
  144. procedure location_swap(var destloc,sourceloc : tlocation);
  145. var
  146. swapl : tlocation;
  147. begin
  148. swapl := destloc;
  149. destloc := sourceloc;
  150. sourceloc := swapl;
  151. end;
  152. end.