tainst.pas 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. {$i defines.inc}
  20. interface
  21. Uses aasm,cpubase,cpuinfo,cclasses;
  22. Type
  23. tairegalloc = class(tai)
  24. allocation : boolean;
  25. reg : tregister;
  26. constructor alloc(r : tregister);
  27. constructor dealloc(r : tregister);
  28. end;
  29. tainstruction = class(tai)
  30. condition : TAsmCond;
  31. ops : longint;
  32. oper : array[0..max_operands-1] of toper;
  33. opcode : tasmop;
  34. {$ifdef i386}
  35. segprefix : tregister;
  36. {$endif i386}
  37. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  38. Constructor Create(op : tasmop);
  39. Destructor Destroy;override;
  40. function getcopy:tlinkedlistitem;override;
  41. procedure loadconst(opidx:longint;l:longint);
  42. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  43. procedure loadref(opidx:longint;p:preference);
  44. procedure loadreg(opidx:longint;r:tregister);
  45. procedure loadoper(opidx:longint;o:toper);
  46. procedure SetCondition(const c:TAsmCond);
  47. end;
  48. implementation
  49. {*****************************************************************************
  50. TaiRegAlloc
  51. *****************************************************************************}
  52. constructor tairegalloc.alloc(r : tregister);
  53. begin
  54. inherited create;
  55. typ:=ait_regalloc;
  56. allocation:=true;
  57. reg:=r;
  58. end;
  59. constructor tairegalloc.dealloc(r : tregister);
  60. begin
  61. inherited create;
  62. typ:=ait_regalloc;
  63. allocation:=false;
  64. reg:=r;
  65. end;
  66. { ---------------------------------------------------------------------
  67. TaInstruction Constructor/Destructor
  68. ---------------------------------------------------------------------}
  69. constructor Tainstruction.Create(op : tasmop);
  70. begin
  71. inherited create;
  72. typ:=ait_instruction;
  73. is_jmp:=false;
  74. opcode:=op;
  75. ops:=0;
  76. fillchar(condition,sizeof(condition),0);
  77. fillchar(oper,sizeof(oper),0);
  78. end;
  79. destructor Tainstruction.Destroy;
  80. var
  81. i : longint;
  82. begin
  83. for i:=0 to ops-1 do
  84. case oper[i].typ of
  85. top_ref:
  86. dispose(oper[i].ref);
  87. top_symbol:
  88. dec(tasmsymbol(oper[i].sym).refs);
  89. end;
  90. inherited destroy;
  91. end;
  92. { ---------------------------------------------------------------------
  93. Loading of operands.
  94. ---------------------------------------------------------------------}
  95. procedure tainstruction.loadconst(opidx:longint;l:longint);
  96. begin
  97. if opidx>=ops then
  98. ops:=opidx+1;
  99. with oper[opidx] do
  100. begin
  101. if typ=top_ref then
  102. disposereference(ref);
  103. val:=l;
  104. typ:=top_const;
  105. end;
  106. end;
  107. procedure tainstruction.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  108. begin
  109. if opidx>=ops then
  110. ops:=opidx+1;
  111. with oper[opidx] do
  112. begin
  113. if typ=top_ref then
  114. disposereference(ref);
  115. sym:=s;
  116. symofs:=sofs;
  117. typ:=top_symbol;
  118. end;
  119. { Mark the symbol as used }
  120. if assigned(s) then
  121. inc(s.refs);
  122. end;
  123. procedure tainstruction.loadref(opidx:longint;p:preference);
  124. begin
  125. if opidx>=ops then
  126. ops:=opidx+1;
  127. with oper[opidx] do
  128. begin
  129. if typ=top_ref then
  130. disposereference(ref);
  131. if p^.is_immediate then
  132. begin
  133. {$ifdef REF_IMMEDIATE_WARN}
  134. Comment(V_Warning,'Reference immediate');
  135. {$endif}
  136. val:=p^.offset;
  137. disposereference(p);
  138. typ:=top_const;
  139. end
  140. else
  141. begin
  142. ref:=p;
  143. { We allow this exception for i386, since overloading this would be
  144. too much of a a speed penalty}
  145. {$ifdef i386}
  146. if not(ref^.segment in [R_DS,R_NO]) then
  147. segprefix:=ref^.segment;
  148. {$endif}
  149. typ:=top_ref;
  150. { mark symbol as used }
  151. if assigned(ref^.symbol) then
  152. inc(ref^.symbol.refs);
  153. end;
  154. end;
  155. end;
  156. procedure tainstruction.loadreg(opidx:longint;r:tregister);
  157. begin
  158. if opidx>=ops then
  159. ops:=opidx+1;
  160. with oper[opidx] do
  161. begin
  162. if typ=top_ref then
  163. disposereference(ref);
  164. reg:=r;
  165. typ:=top_reg;
  166. end;
  167. end;
  168. procedure tainstruction.loadoper(opidx:longint;o:toper);
  169. begin
  170. if opidx>=ops then
  171. ops:=opidx+1;
  172. if oper[opidx].typ=top_ref then
  173. disposereference(oper[opidx].ref);
  174. oper[opidx]:=o;
  175. { copy also the reference }
  176. if oper[opidx].typ=top_ref then
  177. oper[opidx].ref:=newreference(o.ref^);
  178. end;
  179. { ---------------------------------------------------------------------
  180. Miscellaneous methods.
  181. ---------------------------------------------------------------------}
  182. procedure tainstruction.SetCondition(const c:TAsmCond);
  183. begin
  184. condition:=c;
  185. end;
  186. Function tainstruction.getcopy:tlinkedlistitem;
  187. var
  188. i : longint;
  189. p : tlinkedlistitem;
  190. begin
  191. p:=inherited getcopy;
  192. { make a copy of the references }
  193. for i:=1 to ops do
  194. if (tainstruction(p).oper[i-1].typ=top_ref) then
  195. begin
  196. new(tainstruction(p).oper[i-1].ref);
  197. tainstruction(p).oper[i-1].ref^:=oper[i-1].ref^;
  198. end;
  199. getcopy:=p;
  200. end;
  201. end.
  202. {
  203. $Log$
  204. Revision 1.3 2001-12-29 16:29:08 jonas
  205. * fixed stupid copy-paste bug
  206. Revision 1.2 2001/12/29 15:28:57 jonas
  207. * powerpc/cgcpu.pas compiles :)
  208. * several powerpc-related fixes
  209. * cpuasm unit is now based on common tainst unit
  210. + nppcmat unit for powerpc (almost complete)
  211. Revision 1.1 2001/08/26 13:36:52 florian
  212. * some cg reorganisation
  213. * some PPC updates
  214. Revision 1.1 2000/07/13 06:30:08 michael
  215. + Initial import
  216. Revision 1.6 2000/01/07 01:14:54 peter
  217. * updated copyright to 2000
  218. Revision 1.5 1999/09/10 18:48:11 florian
  219. * some bug fixes (e.g. must_be_valid and procinfo.funcret_is_valid)
  220. * most things for stored properties fixed
  221. Revision 1.4 1999/09/03 13:10:11 jonas
  222. * condition is now zeroed using fillchar
  223. because on powerpc it's a record now
  224. Revision 1.3 1999/08/26 14:52:59 jonas
  225. * added segprefix field for i386 in tainstruction object
  226. Revision 1.2 1999/08/06 16:38:37 jonas
  227. * declared getcopy virtual, since it's already declared as such
  228. in cobjects.pas (FPC doesn't error on that, TP does)
  229. Revision 1.1 1999/08/06 16:04:05 michael
  230. + introduced tainstruction
  231. }