tainst.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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,cobjects;
  21. Type
  22. pairegalloc = ^tairegalloc;
  23. tairegalloc = object(tai)
  24. allocation : boolean;
  25. reg : tregister;
  26. constructor alloc(r : tregister);
  27. constructor dealloc(r : tregister);
  28. end;
  29. painstruction = ^tainstruction;
  30. tainstruction = object(tai)
  31. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  32. opcode : tasmop;
  33. condition : TAsmCond;
  34. ops : longint;
  35. oper : array[0..max_operands-1] of toper;
  36. {$ifdef i386}
  37. segprefix : tregister;
  38. {$endif}
  39. Constructor init(op : tasmop);
  40. Destructor Done;virtual;
  41. function getcopy:plinkedlist_item;virtual;
  42. procedure loadconst(opidx:longint;l:longint);
  43. procedure loadsymbol(opidx:longint;s:pasmsymbol;sofs:longint);
  44. procedure loadref(opidx:longint;p:preference);
  45. procedure loadreg(opidx:longint;r:tregister);
  46. procedure loadoper(opidx:longint;o:toper);
  47. procedure SetCondition(c:TAsmCond);
  48. end;
  49. implementation
  50. {*****************************************************************************
  51. TaiRegAlloc
  52. *****************************************************************************}
  53. constructor tairegalloc.alloc(r : tregister);
  54. begin
  55. inherited init;
  56. typ:=ait_regalloc;
  57. allocation:=true;
  58. reg:=r;
  59. end;
  60. constructor tairegalloc.dealloc(r : tregister);
  61. begin
  62. inherited init;
  63. typ:=ait_regalloc;
  64. allocation:=false;
  65. reg:=r;
  66. end;
  67. { ---------------------------------------------------------------------
  68. TaInstruction Constructor/Destructor
  69. ---------------------------------------------------------------------}
  70. Constructor tainstruction.init(op : tasmop);
  71. begin
  72. inherited init;
  73. typ:=ait_instruction;
  74. is_jmp:=false;
  75. opcode:=op;
  76. ops:=0;
  77. fillchar(condition,sizeof(condition),0);
  78. fillchar(oper,sizeof(oper),0);
  79. end;
  80. Destructor Tainstruction.Done;
  81. Var i : longint;
  82. begin
  83. for i:=1 to ops do
  84. if (oper[i-1].typ=top_ref) then
  85. dispose(oper[i-1].ref);
  86. inherited done;
  87. end;
  88. { ---------------------------------------------------------------------
  89. Loading of operands.
  90. ---------------------------------------------------------------------}
  91. procedure tainstruction.loadconst(opidx:longint;l:longint);
  92. begin
  93. if opidx>=ops then
  94. ops:=opidx+1;
  95. with oper[opidx] do
  96. begin
  97. if typ=top_ref then
  98. disposereference(ref);
  99. val:=l;
  100. typ:=top_const;
  101. end;
  102. end;
  103. procedure tainstruction.loadsymbol(opidx:longint;s:pasmsymbol;sofs:longint);
  104. begin
  105. if opidx>=ops then
  106. ops:=opidx+1;
  107. with oper[opidx] do
  108. begin
  109. if typ=top_ref then
  110. disposereference(ref);
  111. sym:=s;
  112. symofs:=sofs;
  113. typ:=top_symbol;
  114. end;
  115. { Mark the symbol as used }
  116. if assigned(s) then
  117. inc(s^.refs);
  118. end;
  119. procedure tainstruction.loadref(opidx:longint;p:preference);
  120. begin
  121. if opidx>=ops then
  122. ops:=opidx+1;
  123. with oper[opidx] do
  124. begin
  125. if typ=top_ref then
  126. disposereference(ref);
  127. if p^.is_immediate then
  128. begin
  129. val:=p^.offset;
  130. disposereference(p);
  131. typ:=top_const;
  132. end
  133. else
  134. begin
  135. ref:=p;
  136. { We allow this exception for i386, since overloading this would be
  137. too much of a a speed penalty}
  138. {$ifdef i386}
  139. if not(ref^.segment in [R_DS,R_NO]) then
  140. segprefix:=ref^.segment;
  141. {$endif}
  142. typ:=top_ref;
  143. { mark symbol as used }
  144. if assigned(ref^.symbol) then
  145. inc(ref^.symbol^.refs);
  146. end;
  147. end;
  148. end;
  149. procedure tainstruction.loadreg(opidx:longint;r:tregister);
  150. begin
  151. if opidx>=ops then
  152. ops:=opidx+1;
  153. with oper[opidx] do
  154. begin
  155. if typ=top_ref then
  156. disposereference(ref);
  157. reg:=r;
  158. typ:=top_reg;
  159. end;
  160. end;
  161. procedure tainstruction.loadoper(opidx:longint;o:toper);
  162. begin
  163. if opidx>=ops then
  164. ops:=opidx+1;
  165. if oper[opidx].typ=top_ref then
  166. disposereference(oper[opidx].ref);
  167. oper[opidx]:=o;
  168. { copy also the reference }
  169. if oper[opidx].typ=top_ref then
  170. oper[opidx].ref:=newreference(o.ref^);
  171. end;
  172. { ---------------------------------------------------------------------
  173. Miscellaneous methods.
  174. ---------------------------------------------------------------------}
  175. procedure tainstruction.SetCondition(c:TAsmCond);
  176. begin
  177. condition:=c;
  178. end;
  179. Function tainstruction.getcopy:plinkedlist_item;
  180. var
  181. i : longint;
  182. p : plinkedlist_item;
  183. begin
  184. p:=inherited getcopy;
  185. { make a copy of the references }
  186. for i:=1 to ops do
  187. if (painstruction(p)^.oper[i-1].typ=top_ref) then
  188. begin
  189. new(painstruction(p)^.oper[i-1].ref);
  190. painstruction(p)^.oper[i-1].ref^:=oper[i-1].ref^;
  191. end;
  192. getcopy:=p;
  193. end;
  194. end.
  195. {
  196. $Log$
  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. }