aasmcpu.pas 18 KB

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