tainst.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Michael Van Canneyt
  4. Contains a generic assembler instruction object;
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit tainst;
  19. interface
  20. Uses aasm,cpubase,cpuinfo,cclasses;
  21. Type
  22. tairegalloc = class(tai)
  23. allocation : boolean;
  24. reg : tregister;
  25. constructor alloc(r : tregister);
  26. constructor dealloc(r : tregister);
  27. end;
  28. tainstruction = class(tai)
  29. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  30. opcode : tasmop;
  31. condition : TAsmCond;
  32. ops : longint;
  33. oper : array[0..max_operands-1] of toper;
  34. Constructor Create(op : tasmop);
  35. Destructor Destroy;override;
  36. function getcopy:tlinkedlistitem;virtual;
  37. procedure loadconst(opidx:longint;l:longint);
  38. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  39. procedure loadref(opidx:longint;p:preference);
  40. procedure loadreg(opidx:longint;r:tregister);
  41. procedure loadoper(opidx:longint;o:toper);
  42. procedure SetCondition(c:TAsmCond);
  43. end;
  44. implementation
  45. {*****************************************************************************
  46. TaiRegAlloc
  47. *****************************************************************************}
  48. constructor tairegalloc.alloc(r : tregister);
  49. begin
  50. inherited create;
  51. typ:=ait_regalloc;
  52. allocation:=true;
  53. reg:=r;
  54. end;
  55. constructor tairegalloc.dealloc(r : tregister);
  56. begin
  57. inherited create;
  58. typ:=ait_regalloc;
  59. allocation:=false;
  60. reg:=r;
  61. end;
  62. { ---------------------------------------------------------------------
  63. TaInstruction Constructor/Destructor
  64. ---------------------------------------------------------------------}
  65. Constructor tainstruction.Create(op : tasmop);
  66. begin
  67. inherited create;
  68. typ:=ait_instruction;
  69. is_jmp:=false;
  70. opcode:=op;
  71. ops:=0;
  72. fillchar(condition,sizeof(condition),0);
  73. fillchar(oper,sizeof(oper),0);
  74. end;
  75. Destructor Tainstruction.Destroy;
  76. Var i : longint;
  77. begin
  78. for i:=1 to ops do
  79. if (oper[i-1].typ=top_ref) then
  80. dispose(oper[i-1].ref);
  81. inherited destroy;
  82. end;
  83. { ---------------------------------------------------------------------
  84. Loading of operands.
  85. ---------------------------------------------------------------------}
  86. procedure tainstruction.loadconst(opidx:longint;l:longint);
  87. begin
  88. if opidx>=ops then
  89. ops:=opidx+1;
  90. with oper[opidx] do
  91. begin
  92. if typ=top_ref then
  93. disposereference(ref);
  94. val:=l;
  95. typ:=top_const;
  96. end;
  97. end;
  98. procedure tainstruction.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  99. begin
  100. if opidx>=ops then
  101. ops:=opidx+1;
  102. with oper[opidx] do
  103. begin
  104. if typ=top_ref then
  105. disposereference(ref);
  106. sym:=s;
  107. symofs:=sofs;
  108. typ:=top_symbol;
  109. end;
  110. { Mark the symbol as used }
  111. if assigned(s) then
  112. inc(s.refs);
  113. end;
  114. procedure tainstruction.loadref(opidx:longint;p:preference);
  115. begin
  116. if opidx>=ops then
  117. ops:=opidx+1;
  118. with oper[opidx] do
  119. begin
  120. if typ=top_ref then
  121. disposereference(ref);
  122. if p^.is_immediate then
  123. begin
  124. val:=p^.offset;
  125. disposereference(p);
  126. typ:=top_const;
  127. end
  128. else
  129. begin
  130. ref:=p;
  131. { We allow this exception for i386, since overloading this would be
  132. too much of a a speed penalty}
  133. {$ifdef i386}
  134. if not(ref^.segment in [R_DS,R_NO]) then
  135. segprefix:=ref^.segment;
  136. {$endif}
  137. typ:=top_ref;
  138. { mark symbol as used }
  139. if assigned(ref^.symbol) then
  140. inc(ref^.symbol.refs);
  141. end;
  142. end;
  143. end;
  144. procedure tainstruction.loadreg(opidx:longint;r:tregister);
  145. begin
  146. if opidx>=ops then
  147. ops:=opidx+1;
  148. with oper[opidx] do
  149. begin
  150. if typ=top_ref then
  151. disposereference(ref);
  152. reg:=r;
  153. typ:=top_reg;
  154. end;
  155. end;
  156. procedure tainstruction.loadoper(opidx:longint;o:toper);
  157. begin
  158. if opidx>=ops then
  159. ops:=opidx+1;
  160. if oper[opidx].typ=top_ref then
  161. disposereference(oper[opidx].ref);
  162. oper[opidx]:=o;
  163. { copy also the reference }
  164. if oper[opidx].typ=top_ref then
  165. oper[opidx].ref:=newreference(o.ref^);
  166. end;
  167. { ---------------------------------------------------------------------
  168. Miscellaneous methods.
  169. ---------------------------------------------------------------------}
  170. procedure tainstruction.SetCondition(c:TAsmCond);
  171. begin
  172. condition:=c;
  173. end;
  174. Function tainstruction.getcopy:tlinkedlistitem;
  175. var
  176. i : longint;
  177. p : tlinkedlistitem;
  178. begin
  179. p:=inherited getcopy;
  180. { make a copy of the references }
  181. for i:=1 to ops do
  182. if (tainstruction(p).oper[i-1].typ=top_ref) then
  183. begin
  184. new(tainstruction(p).oper[i-1].ref);
  185. tainstruction(p).oper[i-1].ref^:=oper[i-1].ref^;
  186. end;
  187. getcopy:=p;
  188. end;
  189. end.
  190. {
  191. $Log$
  192. Revision 1.1 2001-08-26 13:36:52 florian
  193. * some cg reorganisation
  194. * some PPC updates
  195. Revision 1.1 2000/07/13 06:30:08 michael
  196. + Initial import
  197. Revision 1.6 2000/01/07 01:14:54 peter
  198. * updated copyright to 2000
  199. Revision 1.5 1999/09/10 18:48:11 florian
  200. * some bug fixes (e.g. must_be_valid and procinfo.funcret_is_valid)
  201. * most things for stored properties fixed
  202. Revision 1.4 1999/09/03 13:10:11 jonas
  203. * condition is now zeroed using fillchar\n because on powerpc it's a record now
  204. Revision 1.3 1999/08/26 14:52:59 jonas
  205. * added segprefix field for i386 in tainstruction object
  206. Revision 1.2 1999/08/06 16:38:37 jonas
  207. * declared getcopy virtual, since it's already declared as such
  208. in cobjects.pas (FPC doesn't error on that, TP does)
  209. Revision 1.1 1999/08/06 16:04:05 michael
  210. + introduced tainstruction
  211. }