cpuasm.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. {
  2. $Id$
  3. Copyright (c) 1999-2001 by Jonas Maebe
  4. Contains the assembler object for the PowerPC
  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 cpuasm;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cclasses,tainst,
  23. aasm,globals,verbose,
  24. cpubase;
  25. type
  26. taicpu = class(tainstruction)
  27. constructor op_none(op : tasmop);
  28. constructor op_reg(op : tasmop;_op1 : tregister);
  29. constructor op_const(op : tasmop;_op1 : longint);
  30. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  31. constructor op_reg_ref(op : tasmop;_op1 : tregister;_op2 : preference);
  32. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: longint);
  33. constructor op_const_reg(op:tasmop; _op1: longint; _op2: tregister);
  34. constructor op_const_const(op : tasmop;_op1,_op2 : longint);
  35. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  36. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: Longint);
  37. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  38. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; _op3: preference);
  39. constructor op_const_reg_reg(op : tasmop;_op1 : longint;_op2, _op3 : tregister);
  40. constructor op_const_reg_const(op : tasmop;_op1 : longint;_op2 : tregister;_op3 : longint);
  41. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  42. constructor op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  43. constructor op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: longint);
  44. constructor op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : Longint);
  45. { this is for Jmp instructions }
  46. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  47. constructor op_const_const_sym(op : tasmop;_op1,_op2 : longint;_op3: tasmsymbol);
  48. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  49. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  50. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  51. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;_op2 : preference);
  52. procedure loadbool(opidx:longint;_b:boolean);
  53. procedure loadconst(opidx:longint;l:longint);
  54. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  55. procedure loadref(opidx:longint;p:preference);
  56. procedure loadreg(opidx:longint;r:tregister);
  57. procedure loadoper(opidx:longint;o:toper);
  58. destructor destroy;override;
  59. end;
  60. implementation
  61. {*****************************************************************************
  62. taicpu Constructors
  63. *****************************************************************************}
  64. procedure taicpu.loadconst(opidx:longint;l:longint);
  65. begin
  66. if opidx>=ops then
  67. ops:=opidx+1;
  68. with oper[opidx] do
  69. begin
  70. if typ=top_ref then
  71. disposereference(ref);
  72. val:=l;
  73. typ:=top_const;
  74. end;
  75. end;
  76. procedure taicpu.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  77. begin
  78. if opidx>=ops then
  79. ops:=opidx+1;
  80. with oper[opidx] do
  81. begin
  82. if typ=top_ref then
  83. disposereference(ref);
  84. sym:=s;
  85. symofs:=sofs;
  86. typ:=top_symbol;
  87. end;
  88. { Mark the symbol as used }
  89. if assigned(s) then
  90. inc(s.refs);
  91. end;
  92. procedure taicpu.loadref(opidx:longint;p:preference);
  93. begin
  94. if opidx>=ops then
  95. ops:=opidx+1;
  96. with oper[opidx] do
  97. begin
  98. if typ=top_ref then
  99. disposereference(ref);
  100. if p^.is_immediate then
  101. begin
  102. {$ifdef REF_IMMEDIATE_WARN}
  103. Comment(V_Warning,'Reference immediate');
  104. {$endif}
  105. val:=p^.offset;
  106. disposereference(p);
  107. typ:=top_const;
  108. end
  109. else
  110. begin
  111. ref:=p;
  112. typ:=top_ref;
  113. { mark symbol as used }
  114. if assigned(ref^.symbol) then
  115. inc(ref^.symbol.refs);
  116. end;
  117. end;
  118. end;
  119. procedure taicpu.loadreg(opidx:longint;r:tregister);
  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. reg:=r;
  128. typ:=top_reg;
  129. end;
  130. end;
  131. procedure taicpu.loadoper(opidx:longint;o:toper);
  132. begin
  133. if opidx>=ops then
  134. ops:=opidx+1;
  135. if oper[opidx].typ=top_ref then
  136. disposereference(oper[opidx].ref);
  137. oper[opidx]:=o;
  138. { copy also the reference }
  139. if oper[opidx].typ=top_ref then
  140. oper[opidx].ref:=newreference(o.ref^);
  141. end;
  142. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  143. begin
  144. if opidx>=ops then
  145. ops:=opidx+1;
  146. with oper[opidx] do
  147. begin
  148. if typ=top_ref then
  149. disposereference(ref);
  150. b:=_b;
  151. typ:=top_bool;
  152. end;
  153. end;
  154. constructor taicpu.op_none(op : tasmop);
  155. begin
  156. inherited create(op);
  157. end;
  158. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  159. begin
  160. inherited create(op);
  161. ops:=1;
  162. loadreg(0,_op1);
  163. end;
  164. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  165. begin
  166. inherited create(op);
  167. ops:=1;
  168. loadconst(0,_op1);
  169. end;
  170. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  171. begin
  172. inherited create(op);
  173. ops:=2;
  174. loadreg(0,_op1);
  175. loadreg(1,_op2);
  176. end;
  177. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: longint);
  178. begin
  179. inherited create(op);
  180. ops:=2;
  181. loadreg(0,_op1);
  182. loadconst(1,_op2);
  183. end;
  184. constructor taicpu.op_const_reg(op:tasmop; _op1: longint; _op2: tregister);
  185. begin
  186. inherited create(op);
  187. ops:=2;
  188. loadconst(0,_op1);
  189. loadreg(1,_op2);
  190. end;
  191. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;_op2 : preference);
  192. begin
  193. inherited create(op);
  194. ops:=2;
  195. loadreg(0,_op1);
  196. loadref(1,_op2);
  197. end;
  198. constructor taicpu.op_const_const(op : tasmop;_op1,_op2 : longint);
  199. begin
  200. inherited create(op);
  201. ops:=2;
  202. loadconst(0,_op1);
  203. loadconst(1,_op2);
  204. end;
  205. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  206. begin
  207. inherited create(op);
  208. ops:=3;
  209. loadreg(0,_op1);
  210. loadreg(1,_op2);
  211. loadreg(2,_op3);
  212. end;
  213. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: Longint);
  214. begin
  215. inherited create(op);
  216. ops:=3;
  217. loadreg(0,_op1);
  218. loadreg(1,_op2);
  219. loadconst(2,_op3);
  220. end;
  221. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  222. begin
  223. inherited create(op);
  224. ops:=3;
  225. loadreg(0,_op1);
  226. loadreg(1,_op2);
  227. loadsymbol(0,_op3,_op3ofs);
  228. end;
  229. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; _op3: preference);
  230. begin
  231. inherited create(op);
  232. ops:=3;
  233. loadreg(0,_op1);
  234. loadreg(1,_op2);
  235. loadref(2,_op3);
  236. end;
  237. constructor taicpu.op_const_reg_reg(op : tasmop;_op1 : longint;_op2, _op3 : tregister);
  238. begin
  239. inherited create(op);
  240. ops:=3;
  241. loadconst(0,_op1);
  242. loadreg(1,_op2);
  243. loadreg(2,_op3);
  244. end;
  245. constructor taicpu.op_const_reg_const(op : tasmop;_op1 : longint;_op2 : tregister;_op3 : longint);
  246. begin
  247. inherited create(op);
  248. ops:=3;
  249. loadconst(0,_op1);
  250. loadreg(1,_op2);
  251. loadconst(2,_op3);
  252. end;
  253. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  254. begin
  255. inherited create(op);
  256. ops:=4;
  257. loadreg(0,_op1);
  258. loadreg(1,_op2);
  259. loadreg(2,_op3);
  260. loadreg(3,_op4);
  261. end;
  262. constructor taicpu.op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  263. begin
  264. inherited create(op);
  265. ops:=4;
  266. loadreg(0,_op1);
  267. loadbool(1,_op2);
  268. loadreg(2,_op3);
  269. loadreg(3,_op4);
  270. end;
  271. constructor taicpu.op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: longint);
  272. begin
  273. inherited create(op);
  274. ops:=4;
  275. loadreg(0,_op1);
  276. loadbool(0,_op2);
  277. loadreg(0,_op3);
  278. loadconst(0,_op4);
  279. end;
  280. constructor taicpu.op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : Longint);
  281. begin
  282. inherited create(op);
  283. ops:=5;
  284. loadreg(0,_op1);
  285. loadreg(1,_op2);
  286. loadconst(2,_op3);
  287. loadconst(3,_op4);
  288. loadconst(4,_op5);
  289. end;
  290. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  291. begin
  292. inherited create(op);
  293. condition:=cond;
  294. ops:=1;
  295. loadsymbol(0,_op1,0);
  296. end;
  297. constructor taicpu.op_const_const_sym(op : tasmop;_op1,_op2 : longint; _op3: tasmsymbol);
  298. begin
  299. inherited create(op);
  300. ops:=3;
  301. loadconst(0,_op1);
  302. loadconst(1,_op2);
  303. loadsymbol(2,_op3,0);
  304. end;
  305. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  306. begin
  307. inherited create(op);
  308. ops:=1;
  309. loadsymbol(0,_op1,0);
  310. end;
  311. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  312. begin
  313. inherited create(op);
  314. ops:=1;
  315. loadsymbol(0,_op1,_op1ofs);
  316. end;
  317. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  318. begin
  319. inherited create(op);
  320. ops:=2;
  321. loadreg(0,_op1);
  322. loadsymbol(1,_op2,_op2ofs);
  323. end;
  324. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;_op2 : preference);
  325. begin
  326. inherited create(op);
  327. ops:=2;
  328. loadsymbol(0,_op1,_op1ofs);
  329. loadref(1,_op2);
  330. end;
  331. destructor taicpu.destroy;
  332. var
  333. i : longint;
  334. begin
  335. for i:=ops-1 downto 0 do
  336. if (oper[i].typ=top_ref) then
  337. dispose(oper[i].ref);
  338. inherited destroy;
  339. end;
  340. end.
  341. {
  342. $Log$
  343. Revision 1.3 2001-12-29 15:28:58 jonas
  344. * powerpc/cgcpu.pas compiles :)
  345. * several powerpc-related fixes
  346. * cpuasm unit is now based on common tainst unit
  347. + nppcmat unit for powerpc (almost complete)
  348. Revision 1.2 2001/08/26 13:31:04 florian
  349. * some cg reorganisation
  350. * some PPC updates
  351. Revision 1.2 2001/08/26 13:29:34 florian
  352. * some cg reorganisation
  353. * some PPC updates
  354. Revision 1.1 2000/07/13 06:30:12 michael
  355. + Initial import
  356. Revision 1.5 2000/01/07 01:14:58 peter
  357. * updated copyright to 2000
  358. Revision 1.4 1999/08/25 12:00:24 jonas
  359. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  360. Revision 1.3 1999/08/06 16:41:11 jonas
  361. * PowerPC compiles again, several routines implemented in cgcpu.pas
  362. * added constant to cpubase of alpha and powerpc for maximum
  363. number of operands
  364. Revision 1.2 1999/08/04 12:59:24 jonas
  365. * all tokes now start with an underscore
  366. * PowerPC compiles!!
  367. Revision 1.1 1999/08/03 23:37:53 jonas
  368. + initial implementation for PowerPC based on the Alpha stuff
  369. }