2
0

aasmcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  47. constructor op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  48. constructor op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: longint);
  49. constructor op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : Longint);
  50. constructor op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : Longint);
  51. { this is for Jmp instructions }
  52. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  53. constructor op_const_const_sym(op : tasmop;_op1,_op2 : longint;_op3: tasmsymbol);
  54. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  55. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  56. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  57. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  58. procedure loadbool(opidx:longint;_b:boolean);
  59. function is_nop: boolean; override;
  60. function is_move:boolean; override;
  61. { register spilling code }
  62. function spilling_decode_loadstore(op: tasmop; var counterpart: tasmop; var wasload: boolean): boolean;override;
  63. function spilling_create_loadstore(op: tasmop; r:tregister; const ref:treference): tai;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_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  189. begin
  190. inherited create(op);
  191. ops:=4;
  192. loadreg(0,_op1);
  193. loadreg(1,_op2);
  194. loadreg(2,_op3);
  195. loadreg(3,_op4);
  196. end;
  197. constructor taicpu.op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  198. begin
  199. inherited create(op);
  200. ops:=4;
  201. loadreg(0,_op1);
  202. loadbool(1,_op2);
  203. loadreg(2,_op3);
  204. loadreg(3,_op4);
  205. end;
  206. constructor taicpu.op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: longint);
  207. begin
  208. inherited create(op);
  209. ops:=4;
  210. loadreg(0,_op1);
  211. loadbool(0,_op2);
  212. loadreg(0,_op3);
  213. loadconst(0,cardinal(_op4));
  214. end;
  215. constructor taicpu.op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : Longint);
  216. begin
  217. inherited create(op);
  218. ops:=5;
  219. loadreg(0,_op1);
  220. loadreg(1,_op2);
  221. loadreg(2,_op3);
  222. loadconst(3,cardinal(_op4));
  223. loadconst(4,cardinal(_op5));
  224. end;
  225. constructor taicpu.op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : Longint);
  226. begin
  227. inherited create(op);
  228. ops:=5;
  229. loadreg(0,_op1);
  230. loadreg(1,_op2);
  231. loadconst(2,aword(_op3));
  232. loadconst(3,cardinal(_op4));
  233. loadconst(4,cardinal(_op5));
  234. end;
  235. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  236. begin
  237. inherited create(op);
  238. condition:=cond;
  239. ops:=1;
  240. loadsymbol(0,_op1,0);
  241. end;
  242. constructor taicpu.op_const_const_sym(op : tasmop;_op1,_op2 : longint; _op3: tasmsymbol);
  243. begin
  244. inherited create(op);
  245. ops:=3;
  246. loadconst(0,aword(_op1));
  247. loadconst(1,aword(_op2));
  248. loadsymbol(2,_op3,0);
  249. end;
  250. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  251. begin
  252. inherited create(op);
  253. ops:=1;
  254. loadsymbol(0,_op1,0);
  255. end;
  256. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  257. begin
  258. inherited create(op);
  259. ops:=1;
  260. loadsymbol(0,_op1,_op1ofs);
  261. end;
  262. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  263. begin
  264. inherited create(op);
  265. ops:=2;
  266. loadreg(0,_op1);
  267. loadsymbol(1,_op2,_op2ofs);
  268. end;
  269. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  270. begin
  271. inherited create(op);
  272. ops:=2;
  273. loadsymbol(0,_op1,_op1ofs);
  274. loadref(1,_op2);
  275. end;
  276. { ****************************** newra stuff *************************** }
  277. function taicpu.is_nop: boolean;
  278. begin
  279. { we don't insert any more nops than necessary }
  280. is_nop :=
  281. ((opcode=A_MR) and (oper[0]^.typ=top_reg) and (oper[1]^.typ=top_reg) and (oper[0]^.reg=oper[1]^.reg));
  282. end;
  283. function taicpu.is_move:boolean;
  284. begin
  285. is_move := (opcode = A_MR) or
  286. (opcode = A_EXTSB) or
  287. (opcode = A_EXTSH) or
  288. ((opcode = A_RLWINM) and
  289. (oper[2]^.val = 0) and
  290. (oper[4]^.val = 31) and
  291. (oper[3]^.val in [31-8+1,31-16+1]));
  292. end;
  293. function taicpu.spilling_decode_loadstore(op: tasmop; var counterpart: tasmop; var wasload: boolean): boolean;
  294. begin
  295. result := true;
  296. wasload := true;
  297. case op of
  298. A_LBZ:
  299. begin
  300. counterpart := A_STB;
  301. end;
  302. A_LBZX:
  303. begin
  304. counterpart := A_STBX;
  305. end;
  306. A_LHZ,A_LHA:
  307. begin
  308. counterpart := A_STH;
  309. end;
  310. A_LHZX,A_LHAX:
  311. begin
  312. counterpart := A_STHX;
  313. end;
  314. A_LWZ:
  315. begin
  316. counterpart := A_STW;
  317. end;
  318. A_LWZX:
  319. begin
  320. counterpart := A_STWX;
  321. end;
  322. A_STB:
  323. begin
  324. counterpart := A_LBZ;
  325. wasload := false;
  326. end;
  327. A_STBX:
  328. begin
  329. counterpart := A_LBZX;
  330. wasload := false;
  331. end;
  332. A_STH:
  333. begin
  334. counterpart := A_LHZ;
  335. wasload := false;
  336. end;
  337. A_STHX:
  338. begin
  339. counterpart := A_LHZX;
  340. wasload := false;
  341. end;
  342. A_STW:
  343. begin
  344. counterpart := A_LWZ;
  345. wasload := false;
  346. end;
  347. A_STWX:
  348. begin
  349. counterpart := A_LWZX;
  350. wasload := false;
  351. end;
  352. A_LBZU,A_LBZUX,A_LHZU,A_LHZUX,A_LHAU,A_LHAUX,
  353. A_LWZU,A_LWZUX,A_STBU,A_STBUX,A_STHU,A_STHUX,
  354. A_STWU,A_STWUX:
  355. internalerror(2003070602);
  356. else
  357. result := false;
  358. end;
  359. end;
  360. function taicpu.spilling_create_loadstore(op: tasmop; r:tregister; const ref:treference): tai;
  361. begin
  362. result:=taicpu.op_reg_ref(opcode,r,ref);
  363. end;
  364. function taicpu.spilling_create_load(const ref:treference;r:tregister): tai;
  365. begin
  366. result:=taicpu.op_reg_ref(A_LWZ,r,ref);
  367. end;
  368. function taicpu.spilling_create_store(r:tregister; const ref:treference): tai;
  369. begin
  370. result:=taicpu.op_reg_ref(A_STW,r,ref);
  371. end;
  372. procedure InitAsm;
  373. begin
  374. end;
  375. procedure DoneAsm;
  376. begin
  377. end;
  378. end.
  379. {
  380. $Log$
  381. Revision 1.19 2003-10-25 10:37:26 florian
  382. * fixed compilation of ppc compiler
  383. Revision 1.18 2003/10/01 20:34:49 peter
  384. * procinfo unit contains tprocinfo
  385. * cginfo renamed to cgbase
  386. * moved cgmessage to verbose
  387. * fixed ppc and sparc compiles
  388. Revision 1.17 2003/09/03 19:35:24 peter
  389. * powerpc compiles again
  390. Revision 1.16 2003/09/03 15:55:01 peter
  391. * NEWRA branch merged
  392. Revision 1.15.2.1 2003/08/31 21:08:16 peter
  393. * first batch of sparc fixes
  394. Revision 1.15 2003/08/18 21:27:00 jonas
  395. * some newra optimizations (eliminate lots of moves between registers)
  396. Revision 1.14 2003/08/17 16:53:19 jonas
  397. * fixed compilation of ppc compiler with -dnewra
  398. Revision 1.13 2003/08/11 21:18:20 peter
  399. * start of sparc support for newra
  400. Revision 1.12 2002/09/30 23:16:49 jonas
  401. * is_nop() now identifies "mr rA,rA" instructions for removal
  402. Revision 1.11 2003/07/23 10:58:06 jonas
  403. - disabled some debugging code
  404. Revision 1.10 2003/07/06 21:26:06 jonas
  405. * committed wrong file previously :(
  406. Revision 1.8 2003/06/14 22:32:43 jonas
  407. * ppc compiles with -dnewra, haven't tried to compile anything with it
  408. yet though
  409. Revision 1.7 2003/06/14 14:53:50 jonas
  410. * fixed newra cycle for x86
  411. * added constants for indicating source and destination operands of the
  412. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  413. Revision 1.6 2003/05/11 11:08:25 jonas
  414. + op_reg_reg_reg_const_const (for rlwnm)
  415. Revision 1.5 2003/03/12 22:43:38 jonas
  416. * more powerpc and generic fixes related to the new register allocator
  417. Revision 1.4 2002/12/14 15:02:03 carl
  418. * maxoperands -> max_operands (for portability in rautils.pas)
  419. * fix some range-check errors with loadconst
  420. + add ncgadd unit to m68k
  421. * some bugfix of a_param_reg with LOC_CREFERENCE
  422. Revision 1.3 2002/09/17 18:26:02 jonas
  423. - removed taicpu.destroy, its job is already handled by
  424. taicpu_abstract.destroy() and this caused heap corruption
  425. Revision 1.2 2002/07/26 11:19:57 jonas
  426. * fixed range errors
  427. Revision 1.1 2002/07/07 09:44:31 florian
  428. * powerpc target fixed, very simple units can be compiled
  429. Revision 1.8 2002/05/18 13:34:26 peter
  430. * readded missing revisions
  431. Revision 1.7 2002/05/16 19:46:53 carl
  432. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  433. + try to fix temp allocation (still in ifdef)
  434. + generic constructor calls
  435. + start of tassembler / tmodulebase class cleanup
  436. Revision 1.4 2002/05/13 19:52:46 peter
  437. * a ppcppc can be build again
  438. }