aasmcpu.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. {
  2. $Id$
  3. Copyright (c) 1999-2002 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 aasmcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,aasmtai,
  23. aasmbase,globals,verbose,
  24. cpubase,cpuinfo,cgbase;
  25. const
  26. { "mov reg,reg" source operand number }
  27. O_MOV_SOURCE = 1;
  28. { "mov reg,reg" source operand number }
  29. O_MOV_DEST = 0;
  30. type
  31. taicpu = class(taicpu_abstract)
  32. constructor op_none(op : tasmop);
  33. constructor op_reg(op : tasmop;_op1 : tregister);
  34. constructor op_const(op : tasmop;_op1 : longint);
  35. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  36. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  37. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: longint);
  38. constructor op_const_reg(op:tasmop; _op1: longint; _op2: tregister);
  39. constructor op_const_const(op : tasmop;_op1,_op2 : longint);
  40. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  41. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: Longint);
  42. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  43. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  44. constructor op_const_reg_reg(op : tasmop;_op1 : longint;_op2, _op3 : tregister);
  45. constructor op_const_reg_const(op : tasmop;_op1 : longint;_op2 : tregister;_op3 : longint);
  46. constructor op_const_const_const(op : tasmop;_op1 : longint;_op2 : longint;_op3 : longint);
  47. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  48. constructor op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  49. constructor op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: longint);
  50. constructor op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : Longint);
  51. constructor op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : Longint);
  52. { this is for Jmp instructions }
  53. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  54. constructor op_const_const_sym(op : tasmop;_op1,_op2 : longint;_op3: tasmsymbol);
  55. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  56. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  57. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  58. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  59. procedure loadbool(opidx:longint;_b:boolean);
  60. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  61. { register spilling code }
  62. function spilling_get_operation_type(opnr: longint): topertype;override;
  63. function spilling_create_load(const ref:treference;r:tregister): tai;override;
  64. function spilling_create_store(r:tregister; const ref:treference): tai;override;
  65. end;
  66. tai_align = class(tai_align_abstract)
  67. { nothing to add }
  68. end;
  69. procedure InitAsm;
  70. procedure DoneAsm;
  71. implementation
  72. uses cutils,rgobj;
  73. {*****************************************************************************
  74. taicpu Constructors
  75. *****************************************************************************}
  76. procedure taicpu.loadbool(opidx:longint;_b:boolean);
  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. dispose(ref);
  84. b:=_b;
  85. typ:=top_bool;
  86. end;
  87. end;
  88. constructor taicpu.op_none(op : tasmop);
  89. begin
  90. inherited create(op);
  91. end;
  92. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  93. begin
  94. inherited create(op);
  95. ops:=1;
  96. loadreg(0,_op1);
  97. end;
  98. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  99. begin
  100. inherited create(op);
  101. ops:=1;
  102. loadconst(0,aword(_op1));
  103. end;
  104. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  105. begin
  106. inherited create(op);
  107. ops:=2;
  108. loadreg(0,_op1);
  109. loadreg(1,_op2);
  110. end;
  111. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: longint);
  112. begin
  113. inherited create(op);
  114. ops:=2;
  115. loadreg(0,_op1);
  116. loadconst(1,aword(_op2));
  117. end;
  118. constructor taicpu.op_const_reg(op:tasmop; _op1: longint; _op2: tregister);
  119. begin
  120. inherited create(op);
  121. ops:=2;
  122. loadconst(0,aword(_op1));
  123. loadreg(1,_op2);
  124. end;
  125. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  126. begin
  127. inherited create(op);
  128. ops:=2;
  129. loadreg(0,_op1);
  130. loadref(1,_op2);
  131. end;
  132. constructor taicpu.op_const_const(op : tasmop;_op1,_op2 : longint);
  133. begin
  134. inherited create(op);
  135. ops:=2;
  136. loadconst(0,aword(_op1));
  137. loadconst(1,aword(_op2));
  138. end;
  139. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  140. begin
  141. inherited create(op);
  142. ops:=3;
  143. loadreg(0,_op1);
  144. loadreg(1,_op2);
  145. loadreg(2,_op3);
  146. end;
  147. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: Longint);
  148. begin
  149. inherited create(op);
  150. ops:=3;
  151. loadreg(0,_op1);
  152. loadreg(1,_op2);
  153. loadconst(2,aword(_op3));
  154. end;
  155. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  156. begin
  157. inherited create(op);
  158. ops:=3;
  159. loadreg(0,_op1);
  160. loadreg(1,_op2);
  161. loadsymbol(0,_op3,_op3ofs);
  162. end;
  163. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  164. begin
  165. inherited create(op);
  166. ops:=3;
  167. loadreg(0,_op1);
  168. loadreg(1,_op2);
  169. loadref(2,_op3);
  170. end;
  171. constructor taicpu.op_const_reg_reg(op : tasmop;_op1 : longint;_op2, _op3 : tregister);
  172. begin
  173. inherited create(op);
  174. ops:=3;
  175. loadconst(0,aword(_op1));
  176. loadreg(1,_op2);
  177. loadreg(2,_op3);
  178. end;
  179. constructor taicpu.op_const_reg_const(op : tasmop;_op1 : longint;_op2 : tregister;_op3 : longint);
  180. begin
  181. inherited create(op);
  182. ops:=3;
  183. loadconst(0,aword(_op1));
  184. loadreg(1,_op2);
  185. loadconst(2,aword(_op3));
  186. end;
  187. constructor taicpu.op_const_const_const(op : tasmop;_op1 : longint;_op2 : longint;_op3 : longint);
  188. begin
  189. inherited create(op);
  190. ops:=3;
  191. loadconst(0,aword(_op1));
  192. loadconst(1,aword(_op2));
  193. loadconst(2,aword(_op3));
  194. end;
  195. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  196. begin
  197. inherited create(op);
  198. ops:=4;
  199. loadreg(0,_op1);
  200. loadreg(1,_op2);
  201. loadreg(2,_op3);
  202. loadreg(3,_op4);
  203. end;
  204. constructor taicpu.op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  205. begin
  206. inherited create(op);
  207. ops:=4;
  208. loadreg(0,_op1);
  209. loadbool(1,_op2);
  210. loadreg(2,_op3);
  211. loadreg(3,_op4);
  212. end;
  213. constructor taicpu.op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: longint);
  214. begin
  215. inherited create(op);
  216. ops:=4;
  217. loadreg(0,_op1);
  218. loadbool(0,_op2);
  219. loadreg(0,_op3);
  220. loadconst(0,cardinal(_op4));
  221. end;
  222. constructor taicpu.op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : Longint);
  223. begin
  224. inherited create(op);
  225. ops:=5;
  226. loadreg(0,_op1);
  227. loadreg(1,_op2);
  228. loadreg(2,_op3);
  229. loadconst(3,cardinal(_op4));
  230. loadconst(4,cardinal(_op5));
  231. end;
  232. constructor taicpu.op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : Longint);
  233. begin
  234. inherited create(op);
  235. ops:=5;
  236. loadreg(0,_op1);
  237. loadreg(1,_op2);
  238. loadconst(2,aword(_op3));
  239. loadconst(3,cardinal(_op4));
  240. loadconst(4,cardinal(_op5));
  241. end;
  242. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  243. begin
  244. inherited create(op);
  245. condition:=cond;
  246. ops:=1;
  247. loadsymbol(0,_op1,0);
  248. end;
  249. constructor taicpu.op_const_const_sym(op : tasmop;_op1,_op2 : longint; _op3: tasmsymbol);
  250. begin
  251. inherited create(op);
  252. ops:=3;
  253. loadconst(0,aword(_op1));
  254. loadconst(1,aword(_op2));
  255. loadsymbol(2,_op3,0);
  256. end;
  257. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  258. begin
  259. inherited create(op);
  260. ops:=1;
  261. loadsymbol(0,_op1,0);
  262. end;
  263. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  264. begin
  265. inherited create(op);
  266. ops:=1;
  267. loadsymbol(0,_op1,_op1ofs);
  268. end;
  269. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  270. begin
  271. inherited create(op);
  272. ops:=2;
  273. loadreg(0,_op1);
  274. loadsymbol(1,_op2,_op2ofs);
  275. end;
  276. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  277. begin
  278. inherited create(op);
  279. ops:=2;
  280. loadsymbol(0,_op1,_op1ofs);
  281. loadref(1,_op2);
  282. end;
  283. { ****************************** newra stuff *************************** }
  284. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  285. begin
  286. result :=
  287. (((opcode=A_MR) and
  288. (regtype = R_INTREGISTER)) or
  289. ((opcode = A_FMR) and
  290. (regtype = R_FPUREGISTER))) and
  291. { these opcodes can only have registers as operands }
  292. (oper[0]^.reg=oper[1]^.reg);
  293. end;
  294. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  295. begin
  296. result := operand_read;
  297. case opcode of
  298. A_STB,A_STBX,A_STH,A_STHX,A_STW,A_STWX,A_STBU,A_STBUX,A_STHU,A_STHUX,A_STWU,A_STWUX:
  299. begin
  300. if opnr = 1 then
  301. result := operand_write;
  302. end;
  303. else
  304. if opnr = 0 then
  305. result := operand_write;
  306. end;
  307. end;
  308. function taicpu.spilling_create_load(const ref:treference;r:tregister): tai;
  309. begin
  310. result:=taicpu.op_reg_ref(A_LWZ,r,ref);
  311. end;
  312. function taicpu.spilling_create_store(r:tregister; const ref:treference): tai;
  313. begin
  314. result:=taicpu.op_reg_ref(A_STW,r,ref);
  315. end;
  316. procedure InitAsm;
  317. begin
  318. end;
  319. procedure DoneAsm;
  320. begin
  321. end;
  322. end.
  323. {
  324. $Log$
  325. Revision 1.25 2004-02-08 23:10:21 jonas
  326. * taicpu.is_same_reg_move() now gets a regtype parameter so it only
  327. removes moves of that particular register type. This is necessary so
  328. we don't remove the live_start instruction of a register before it
  329. has been processed
  330. Revision 1.24 2004/02/08 20:15:42 jonas
  331. - removed taicpu.is_reg_move because it's not used anymore
  332. + support tracking fpu register moves by rgobj for the ppc
  333. Revision 1.23 2003/12/28 22:09:12 florian
  334. + setting of bit 6 of cr for c var args on ppc implemented
  335. Revision 1.22 2003/12/26 14:02:30 peter
  336. * sparc updates
  337. * use registertype in spill_register
  338. Revision 1.21 2003/12/16 21:49:47 florian
  339. * fixed ppc compilation
  340. Revision 1.20 2003/12/06 22:16:13 jonas
  341. * completely overhauled and fixed generic spilling code. New method:
  342. spilling_get_operation_type(operand_number): returns the operation
  343. performed by the instruction on the operand: read/write/read+write.
  344. See powerpc/aasmcpu.pas for an example
  345. Revision 1.19 2003/10/25 10:37:26 florian
  346. * fixed compilation of ppc compiler
  347. Revision 1.18 2003/10/01 20:34:49 peter
  348. * procinfo unit contains tprocinfo
  349. * cginfo renamed to cgbase
  350. * moved cgmessage to verbose
  351. * fixed ppc and sparc compiles
  352. Revision 1.17 2003/09/03 19:35:24 peter
  353. * powerpc compiles again
  354. Revision 1.16 2003/09/03 15:55:01 peter
  355. * NEWRA branch merged
  356. Revision 1.15.2.1 2003/08/31 21:08:16 peter
  357. * first batch of sparc fixes
  358. Revision 1.15 2003/08/18 21:27:00 jonas
  359. * some newra optimizations (eliminate lots of moves between registers)
  360. Revision 1.14 2003/08/17 16:53:19 jonas
  361. * fixed compilation of ppc compiler with -dnewra
  362. Revision 1.13 2003/08/11 21:18:20 peter
  363. * start of sparc support for newra
  364. Revision 1.12 2002/09/30 23:16:49 jonas
  365. * is_nop() now identifies "mr rA,rA" instructions for removal
  366. Revision 1.11 2003/07/23 10:58:06 jonas
  367. - disabled some debugging code
  368. Revision 1.10 2003/07/06 21:26:06 jonas
  369. * committed wrong file previously :(
  370. Revision 1.8 2003/06/14 22:32:43 jonas
  371. * ppc compiles with -dnewra, haven't tried to compile anything with it
  372. yet though
  373. Revision 1.7 2003/06/14 14:53:50 jonas
  374. * fixed newra cycle for x86
  375. * added constants for indicating source and destination operands of the
  376. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  377. Revision 1.6 2003/05/11 11:08:25 jonas
  378. + op_reg_reg_reg_const_const (for rlwnm)
  379. Revision 1.5 2003/03/12 22:43:38 jonas
  380. * more powerpc and generic fixes related to the new register allocator
  381. Revision 1.4 2002/12/14 15:02:03 carl
  382. * maxoperands -> max_operands (for portability in rautils.pas)
  383. * fix some range-check errors with loadconst
  384. + add ncgadd unit to m68k
  385. * some bugfix of a_param_reg with LOC_CREFERENCE
  386. Revision 1.3 2002/09/17 18:26:02 jonas
  387. - removed taicpu.destroy, its job is already handled by
  388. taicpu_abstract.destroy() and this caused heap corruption
  389. Revision 1.2 2002/07/26 11:19:57 jonas
  390. * fixed range errors
  391. Revision 1.1 2002/07/07 09:44:31 florian
  392. * powerpc target fixed, very simple units can be compiled
  393. Revision 1.8 2002/05/18 13:34:26 peter
  394. * readded missing revisions
  395. Revision 1.7 2002/05/16 19:46:53 carl
  396. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  397. + try to fix temp allocation (still in ifdef)
  398. + generic constructor calls
  399. + start of tassembler / tmodulebase class cleanup
  400. Revision 1.4 2002/05/13 19:52:46 peter
  401. * a ppcppc can be build again
  402. }