aasmcpu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. else
  363. ;
  364. end;
  365. end;
  366. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  367. begin
  368. case getregtype(r) of
  369. R_INTREGISTER:
  370. {$ifdef cpu64bitalu}
  371. result:=taicpu.op_reg_ref(A_LD,r,ref);
  372. {$else cpu64bitalu}
  373. result:=taicpu.op_reg_ref(A_LWZ,r,ref);
  374. {$endif cpu64bitalu}
  375. R_FPUREGISTER:
  376. result:=taicpu.op_reg_ref(A_LFD,r,ref);
  377. else
  378. internalerror(2005123101);
  379. end;
  380. end;
  381. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  382. begin
  383. case getregtype(r) of
  384. R_INTREGISTER:
  385. {$ifdef cpu64bitalu}
  386. result:=taicpu.op_reg_ref(A_STD,r,ref);
  387. {$else cpu64bitalu}
  388. result:=taicpu.op_reg_ref(A_STW,r,ref);
  389. {$endif cpu64bitalu}
  390. R_FPUREGISTER:
  391. result:=taicpu.op_reg_ref(A_STFD,r,ref);
  392. else
  393. internalerror(2005123102);
  394. end;
  395. end;
  396. procedure InitAsm;
  397. begin
  398. end;
  399. procedure DoneAsm;
  400. begin
  401. end;
  402. procedure fixup_jmps(list: TAsmList);
  403. var
  404. p: tai;
  405. newjmp: taicpu;
  406. labelpositions: TFPList;
  407. instrpos: ptrint;
  408. l: tasmlabel;
  409. inserted_something: boolean;
  410. begin
  411. // if certainly not enough instructions to cause an overflow, don't bother
  412. if (list.count <= (high(smallint) div 4)) then
  413. exit;
  414. labelpositions := TFPList.create;
  415. p := tai(list.first);
  416. instrpos := 1;
  417. // record label positions
  418. while assigned(p) do
  419. begin
  420. if p.typ = ait_label then
  421. begin
  422. if (tai_label(p).labsym.labelnr >= labelpositions.count) then
  423. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  424. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  425. end;
  426. { ait_const is for jump tables }
  427. case p.typ of
  428. ait_instruction:
  429. inc(instrpos);
  430. ait_const:
  431. begin
  432. if (tai_const(p).consttype<>aitconst_32bit) then
  433. internalerror(2008052101);
  434. inc(instrpos);
  435. end;
  436. else
  437. ;
  438. end;
  439. p := tai(p.next);
  440. end;
  441. // check and fix distances
  442. repeat
  443. inserted_something := false;
  444. p := tai(list.first);
  445. instrpos := 1;
  446. while assigned(p) do
  447. begin
  448. case p.typ of
  449. ait_label:
  450. // update labelposition in case it changed due to insertion
  451. // of jumps
  452. begin
  453. // can happen because of newly inserted labels
  454. if (tai_label(p).labsym.labelnr > labelpositions.count) then
  455. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  456. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  457. end;
  458. ait_instruction:
  459. begin
  460. inc(instrpos);
  461. case taicpu(p).opcode of
  462. A_BC:
  463. if (taicpu(p).oper[0]^.typ = top_ref) and
  464. assigned(taicpu(p).oper[0]^.ref^.symbol) and
  465. (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) and
  466. (labelpositions[tasmlabel(taicpu(p).oper[0]^.ref^.symbol).labelnr] <> NIL) and
  467. {$push}
  468. {$q-}
  469. (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
  470. {$pop}
  471. begin
  472. // add a new label after this jump
  473. current_asmdata.getjumplabel(l);
  474. { new label -> may have to increase array size }
  475. if (l.labelnr >= labelpositions.count) then
  476. labelpositions.count := l.labelnr + 10;
  477. { newjmp will be inserted before the label, and it's inserted after }
  478. { the current jump -> instrpos+2 }
  479. labelpositions[l.labelnr] := pointer(instrpos+2);
  480. list.insertafter(tai_label.create(l),p);
  481. // add a new unconditional jump between this jump and the label
  482. newjmp := taicpu.op_sym(A_B,taicpu(p).oper[0]^.ref^.symbol);
  483. newjmp.is_jmp := true;
  484. newjmp.fileinfo := taicpu(p).fileinfo;
  485. list.insertafter(newjmp,p);
  486. inc(instrpos);
  487. // change the conditional jump to point to the newly inserted label
  488. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  489. taicpu(p).oper[0]^.ref^.symbol := l;
  490. l.increfs;
  491. // and invert its condition code
  492. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  493. // we inserted an instruction, so will have to check everything again
  494. inserted_something := true;
  495. end;
  496. else
  497. ;
  498. end;
  499. end;
  500. ait_const:
  501. inc(instrpos);
  502. else
  503. ;
  504. end;
  505. p := tai(p.next);
  506. end;
  507. until not inserted_something;
  508. labelpositions.free;
  509. end;
  510. begin
  511. cai_align:=tai_align;
  512. cai_cpu:=taicpu;
  513. end.