tainst.pas 5.7 KB

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