aasmcpu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. {
  2. Copyright (c) 1999-2002 by Jonas Maebe and Thomas Schatzl
  3. Contains the assembler object for the PowerPC 32 and PowerPC 64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. cpubase,cgbase,cgutils;
  24. const
  25. { "mov reg,reg" source operand number }
  26. O_MOV_SOURCE = 1;
  27. { "mov reg,reg" source operand number }
  28. O_MOV_DEST = 0;
  29. type
  30. taicpu = class(tai_cpu_abstract_sym)
  31. constructor op_none(op : tasmop);
  32. constructor op_reg(op : tasmop;_op1 : tregister);
  33. constructor op_const(op : tasmop;_op1 : aint);
  34. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  35. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  36. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  37. constructor op_const_reg(op:tasmop; _op1: aint; _op2: tregister);
  38. constructor op_const_const(op : tasmop;_op1,_op2 : aint);
  39. constructor op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  40. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  41. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  42. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  43. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  44. constructor op_const_reg_reg(op : tasmop;_op1 : aint;_op2, _op3 : tregister);
  45. constructor op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  46. constructor op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  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: aint);
  50. constructor op_reg_reg_reg_const(op : tasmop; _op1, _op2, _op3 : tregister; _op4 : aint);
  51. constructor op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : aint);
  52. constructor op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : aint);
  53. { this is for Jmp instructions }
  54. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  55. constructor op_const_const_sym(op : tasmop;_op1,_op2 : aint;_op3: tasmsymbol);
  56. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  57. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint);
  58. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : aint);
  59. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint;const _op2 : treference);
  60. procedure loadbool(opidx:aint;_b:boolean);
  61. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  62. { register spilling code }
  63. function spilling_get_operation_type(opnr: longint): topertype;override;
  64. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  65. end;
  66. tai_align = class(tai_align_abstract)
  67. { nothing to add }
  68. end;
  69. procedure InitAsm;
  70. procedure DoneAsm;
  71. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  72. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  73. procedure fixup_jmps(list: TAsmList);
  74. implementation
  75. uses cutils, cclasses;
  76. {*****************************************************************************
  77. taicpu Constructors
  78. *****************************************************************************}
  79. procedure taicpu.loadbool(opidx:aint;_b:boolean);
  80. begin
  81. if opidx>=ops then
  82. ops:=opidx+1;
  83. with oper[opidx]^ do
  84. begin
  85. if typ=top_ref then
  86. dispose(ref);
  87. b:=_b;
  88. typ:=top_bool;
  89. end;
  90. end;
  91. constructor taicpu.op_none(op : tasmop);
  92. begin
  93. inherited create(op);
  94. end;
  95. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  96. begin
  97. inherited create(op);
  98. ops:=1;
  99. loadreg(0,_op1);
  100. end;
  101. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  102. begin
  103. inherited create(op);
  104. ops:=1;
  105. loadconst(0,_op1);
  106. end;
  107. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  108. begin
  109. inherited create(op);
  110. ops:=2;
  111. loadreg(0,_op1);
  112. loadreg(1,_op2);
  113. end;
  114. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  115. begin
  116. inherited create(op);
  117. ops:=2;
  118. loadreg(0,_op1);
  119. loadconst(1,_op2);
  120. end;
  121. constructor taicpu.op_const_reg(op:tasmop; _op1: aint; _op2: tregister);
  122. begin
  123. inherited create(op);
  124. ops:=2;
  125. loadconst(0,_op1);
  126. loadreg(1,_op2);
  127. end;
  128. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  129. begin
  130. inherited create(op);
  131. ops:=2;
  132. loadreg(0,_op1);
  133. loadref(1,_op2);
  134. end;
  135. constructor taicpu.op_const_const(op : tasmop;_op1,_op2 : aint);
  136. begin
  137. inherited create(op);
  138. ops:=2;
  139. loadconst(0,_op1);
  140. loadconst(1,_op2);
  141. end;
  142. constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  143. begin
  144. inherited create(op);
  145. ops := 4;
  146. loadreg(0, _op1);
  147. loadreg(1, _op2);
  148. loadconst(2, _op3);
  149. loadconst(3, _op4);
  150. end;
  151. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  152. begin
  153. inherited create(op);
  154. ops:=3;
  155. loadreg(0,_op1);
  156. loadreg(1,_op2);
  157. loadreg(2,_op3);
  158. end;
  159. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  160. begin
  161. inherited create(op);
  162. ops:=3;
  163. loadreg(0,_op1);
  164. loadreg(1,_op2);
  165. loadconst(2,_op3);
  166. end;
  167. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  168. begin
  169. inherited create(op);
  170. ops:=3;
  171. loadreg(0,_op1);
  172. loadreg(1,_op2);
  173. loadsymbol(0,_op3,_op3ofs);
  174. end;
  175. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  176. begin
  177. inherited create(op);
  178. ops:=3;
  179. loadreg(0,_op1);
  180. loadreg(1,_op2);
  181. loadref(2,_op3);
  182. end;
  183. constructor taicpu.op_const_reg_reg(op : tasmop;_op1 : aint;_op2, _op3 : tregister);
  184. begin
  185. inherited create(op);
  186. ops:=3;
  187. loadconst(0,_op1);
  188. loadreg(1,_op2);
  189. loadreg(2,_op3);
  190. end;
  191. constructor taicpu.op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  192. begin
  193. inherited create(op);
  194. ops:=3;
  195. loadconst(0,_op1);
  196. loadreg(1,_op2);
  197. loadconst(2,_op3);
  198. end;
  199. constructor taicpu.op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  200. begin
  201. inherited create(op);
  202. ops:=3;
  203. loadconst(0,_op1);
  204. loadconst(1,_op2);
  205. loadconst(2,_op3);
  206. end;
  207. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  208. begin
  209. inherited create(op);
  210. ops:=4;
  211. loadreg(0,_op1);
  212. loadreg(1,_op2);
  213. loadreg(2,_op3);
  214. loadreg(3,_op4);
  215. end;
  216. constructor taicpu.op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  217. begin
  218. inherited create(op);
  219. ops:=4;
  220. loadreg(0,_op1);
  221. loadbool(1,_op2);
  222. loadreg(2,_op3);
  223. loadreg(3,_op4);
  224. end;
  225. constructor taicpu.op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: aint);
  226. begin
  227. inherited create(op);
  228. ops:=4;
  229. loadreg(0,_op1);
  230. loadbool(0,_op2);
  231. loadreg(0,_op3);
  232. loadconst(0,cardinal(_op4));
  233. end;
  234. constructor taicpu.op_reg_reg_reg_const(op : tasmop; _op1, _op2, _op3 : tregister; _op4 : aint);
  235. begin
  236. inherited create(op);
  237. ops := 4;
  238. loadreg(0, _op1);
  239. loadreg(1, _op2);
  240. loadreg(2, _op3);
  241. loadconst(3, cardinal(_op4));
  242. end;
  243. constructor taicpu.op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : aint);
  244. begin
  245. inherited create(op);
  246. ops:=5;
  247. loadreg(0,_op1);
  248. loadreg(1,_op2);
  249. loadreg(2,_op3);
  250. loadconst(3,cardinal(_op4));
  251. loadconst(4,cardinal(_op5));
  252. end;
  253. constructor taicpu.op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : aint);
  254. begin
  255. inherited create(op);
  256. ops:=5;
  257. loadreg(0,_op1);
  258. loadreg(1,_op2);
  259. loadconst(2,_op3);
  260. loadconst(3,_op4);
  261. loadconst(4,_op5);
  262. end;
  263. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  264. begin
  265. inherited create(op);
  266. condition:=cond;
  267. ops:=1;
  268. loadsymbol(0,_op1,0);
  269. end;
  270. constructor taicpu.op_const_const_sym(op : tasmop;_op1,_op2 : aint; _op3: tasmsymbol);
  271. begin
  272. inherited create(op);
  273. ops:=3;
  274. loadconst(0,_op1);
  275. loadconst(1,_op2);
  276. loadsymbol(2,_op3,0);
  277. end;
  278. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  279. begin
  280. inherited create(op);
  281. ops:=1;
  282. loadsymbol(0,_op1,0);
  283. end;
  284. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint);
  285. begin
  286. inherited create(op);
  287. ops:=1;
  288. loadsymbol(0,_op1,_op1ofs);
  289. end;
  290. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : aint);
  291. begin
  292. inherited create(op);
  293. ops:=2;
  294. loadreg(0,_op1);
  295. loadsymbol(1,_op2,_op2ofs);
  296. end;
  297. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint;const _op2 : treference);
  298. begin
  299. inherited create(op);
  300. ops:=2;
  301. loadsymbol(0,_op1,_op1ofs);
  302. loadref(1,_op2);
  303. end;
  304. { ****************************** newra stuff *************************** }
  305. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  306. begin
  307. result :=
  308. (((opcode=A_MR) and
  309. (regtype = R_INTREGISTER)) or
  310. ((opcode = A_FMR) and
  311. (regtype = R_FPUREGISTER))) and
  312. { these opcodes can only have registers as operands }
  313. (oper[0]^.reg=oper[1]^.reg);
  314. end;
  315. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  316. begin
  317. result := operand_read;
  318. case opcode of
  319. A_STMW,A_LMW:
  320. internalerror(2005021805);
  321. A_STBU, A_STBUX, A_STHU, A_STHUX,
  322. A_STWU, A_STWUX,
  323. A_STFSU, A_STFSUX, A_STFDU, A_STFDUX,
  324. A_STB, A_STBX, A_STH, A_STHX,
  325. A_STW, A_STWX,
  326. A_STFS, A_STFSX, A_STFD, A_STFDX, A_STFIWX, A_STHBRX, A_STWBRX, A_STWCX_,
  327. A_CMP, A_CMPI, A_CMPL, A_CMPLI,
  328. A_DCBA, A_DCBI, A_DCBST, A_DCBT, A_DCBTST, A_DCBZ, A_DCBF, A_ICBI,
  329. A_ECOWX, A_FCMPO, A_FCMPU, A_MTMSR, A_TLBIE, A_TW, A_TWI,
  330. A_CMPWI, A_CMPW, A_CMPLWI, A_CMPLW, A_MT, A_MTLR, A_MTCTR
  331. {$ifdef cpu64bitalu}
  332. , A_STDU, A_STDUX,
  333. A_STD, A_STDX,
  334. A_STDCX_,
  335. A_CMPD, A_CMPDI, A_CMPLD, A_CMPLDI,
  336. A_MFXER
  337. {$endif cpu64bitalu}
  338. : ;
  339. A_RLWIMI, A_RLWIMI_, A_INSLWI, A_INSLWI_, A_INSRWI, A_INSRWI_
  340. {$ifdef cpu64bitalu}
  341. , A_INSRDI, A_INSRDI_, A_RLDIMI
  342. {$endif not cpu64bitalu}
  343. :
  344. if opnr = 0 then
  345. result := operand_readwrite;
  346. else
  347. if opnr = 0 then
  348. result := operand_write;
  349. end;
  350. end;
  351. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  352. begin
  353. result := operand_read;
  354. case opcode of
  355. A_STBU, A_STBUX, A_STHU, A_STHUX, A_STWU, A_STWUX,
  356. {$ifdef cpu64bitalu}
  357. A_STDU, A_STDUX,
  358. {$endif cpu64bitalu}
  359. A_STFSU, A_STFSUX, A_STFDU, A_STFDUX:
  360. if (oper[opnr]^.ref^.base = reg) then
  361. result := operand_readwrite;
  362. end;
  363. end;
  364. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  365. begin
  366. case getregtype(r) of
  367. R_INTREGISTER:
  368. {$ifdef cpu64bitalu}
  369. result:=taicpu.op_reg_ref(A_LD,r,ref);
  370. {$else cpu64bitalu}
  371. result:=taicpu.op_reg_ref(A_LWZ,r,ref);
  372. {$endif cpu64bitalu}
  373. R_FPUREGISTER:
  374. result:=taicpu.op_reg_ref(A_LFD,r,ref);
  375. else
  376. internalerror(2005123101);
  377. end;
  378. end;
  379. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  380. begin
  381. case getregtype(r) of
  382. R_INTREGISTER:
  383. {$ifdef cpu64bitalu}
  384. result:=taicpu.op_reg_ref(A_STD,r,ref);
  385. {$else cpu64bitalu}
  386. result:=taicpu.op_reg_ref(A_STW,r,ref);
  387. {$endif cpu64bitalu}
  388. R_FPUREGISTER:
  389. result:=taicpu.op_reg_ref(A_STFD,r,ref);
  390. else
  391. internalerror(2005123102);
  392. end;
  393. end;
  394. procedure InitAsm;
  395. begin
  396. end;
  397. procedure DoneAsm;
  398. begin
  399. end;
  400. procedure fixup_jmps(list: TAsmList);
  401. var
  402. p: tai;
  403. newjmp: taicpu;
  404. labelpositions: TFPList;
  405. instrpos: ptrint;
  406. l: tasmlabel;
  407. inserted_something: boolean;
  408. begin
  409. // if certainly not enough instructions to cause an overflow, don't bother
  410. if (list.count <= (high(smallint) div 4)) then
  411. exit;
  412. labelpositions := TFPList.create;
  413. p := tai(list.first);
  414. instrpos := 1;
  415. // record label positions
  416. while assigned(p) do
  417. begin
  418. if p.typ = ait_label then
  419. begin
  420. if (tai_label(p).labsym.labelnr >= labelpositions.count) then
  421. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  422. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  423. end;
  424. { ait_const is for jump tables }
  425. case p.typ of
  426. ait_instruction:
  427. inc(instrpos);
  428. ait_const:
  429. begin
  430. if (tai_const(p).consttype<>aitconst_32bit) then
  431. internalerror(2008052101);
  432. inc(instrpos);
  433. end;
  434. end;
  435. p := tai(p.next);
  436. end;
  437. // check and fix distances
  438. repeat
  439. inserted_something := false;
  440. p := tai(list.first);
  441. instrpos := 1;
  442. while assigned(p) do
  443. begin
  444. case p.typ of
  445. ait_label:
  446. // update labelposition in case it changed due to insertion
  447. // of jumps
  448. begin
  449. // can happen because of newly inserted labels
  450. if (tai_label(p).labsym.labelnr > labelpositions.count) then
  451. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  452. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  453. end;
  454. ait_instruction:
  455. begin
  456. inc(instrpos);
  457. case taicpu(p).opcode of
  458. A_BC:
  459. if (taicpu(p).oper[0]^.typ = top_ref) and
  460. assigned(taicpu(p).oper[0]^.ref^.symbol) and
  461. (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) and
  462. (labelpositions[tasmlabel(taicpu(p).oper[0]^.ref^.symbol).labelnr] <> NIL) and
  463. {$push}
  464. {$q-}
  465. (ptruint(abs(ptrint(labelpositions[tasmlabel(taicpu(p).oper[0]^.ref^.symbol).labelnr]-instrpos)) - (low(smallint) div 4)) > ptruint((high(smallint) - low(smallint)) div 4)) then
  466. {$pop}
  467. begin
  468. // add a new label after this jump
  469. current_asmdata.getjumplabel(l);
  470. { new label -> may have to increase array size }
  471. if (l.labelnr >= labelpositions.count) then
  472. labelpositions.count := l.labelnr + 10;
  473. { newjmp will be inserted before the label, and it's inserted after }
  474. { the current jump -> instrpos+2 }
  475. labelpositions[l.labelnr] := pointer(instrpos+2);
  476. list.insertafter(tai_label.create(l),p);
  477. // add a new unconditional jump between this jump and the label
  478. newjmp := taicpu.op_sym(A_B,taicpu(p).oper[0]^.ref^.symbol);
  479. newjmp.is_jmp := true;
  480. newjmp.fileinfo := taicpu(p).fileinfo;
  481. list.insertafter(newjmp,p);
  482. inc(instrpos);
  483. // change the conditional jump to point to the newly inserted label
  484. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  485. taicpu(p).oper[0]^.ref^.symbol := l;
  486. l.increfs;
  487. // and invert its condition code
  488. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  489. // we inserted an instruction, so will have to check everything again
  490. inserted_something := true;
  491. end;
  492. end;
  493. end;
  494. ait_const:
  495. inc(instrpos);
  496. end;
  497. p := tai(p.next);
  498. end;
  499. until not inserted_something;
  500. labelpositions.free;
  501. end;
  502. begin
  503. cai_align:=tai_align;
  504. cai_cpu:=taicpu;
  505. end.