aasmcpu.pas 15 KB

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