aasmcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. {
  2. Copyright (c) 1999-2002 by Jonas Maebe
  3. Contains the assembler object for the PowerPC
  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,
  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)
  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_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  40. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  41. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  42. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  43. constructor op_const_reg_reg(op : tasmop;_op1 : aint;_op2, _op3 : tregister);
  44. constructor op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  45. constructor op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  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: aint);
  49. constructor op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : aint);
  50. constructor op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : aint);
  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 : aint;_op3: tasmsymbol);
  54. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  55. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint);
  56. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : aint);
  57. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint;const _op2 : treference);
  58. procedure loadbool(opidx:aint;_b:boolean);
  59. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  60. { register spilling code }
  61. function spilling_get_operation_type(opnr: aint): topertype;override;
  62. function spilling_get_operation_type_ref(opnr: aint; reg: tregister): topertype;override;
  63. end;
  64. tai_align = class(tai_align_abstract)
  65. { nothing to add }
  66. end;
  67. procedure InitAsm;
  68. procedure DoneAsm;
  69. function spilling_create_load(const ref:treference;r:tregister): tai;
  70. function spilling_create_store(r:tregister; const ref:treference): tai;
  71. procedure fixup_jmps(list: TAsmList);
  72. implementation
  73. uses cutils, cclasses;
  74. {*****************************************************************************
  75. taicpu Constructors
  76. *****************************************************************************}
  77. procedure taicpu.loadbool(opidx:aint;_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 : aint);
  100. begin
  101. inherited create(op);
  102. ops:=1;
  103. loadconst(0,_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: aint);
  113. begin
  114. inherited create(op);
  115. ops:=2;
  116. loadreg(0,_op1);
  117. loadconst(1,_op2);
  118. end;
  119. constructor taicpu.op_const_reg(op:tasmop; _op1: aint; _op2: tregister);
  120. begin
  121. inherited create(op);
  122. ops:=2;
  123. loadconst(0,_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 : aint);
  134. begin
  135. inherited create(op);
  136. ops:=2;
  137. loadconst(0,_op1);
  138. loadconst(1,_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: aint);
  149. begin
  150. inherited create(op);
  151. ops:=3;
  152. loadreg(0,_op1);
  153. loadreg(1,_op2);
  154. loadconst(2,_op3);
  155. end;
  156. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  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 : aint;_op2, _op3 : tregister);
  173. begin
  174. inherited create(op);
  175. ops:=3;
  176. loadconst(0,_op1);
  177. loadreg(1,_op2);
  178. loadreg(2,_op3);
  179. end;
  180. constructor taicpu.op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  181. begin
  182. inherited create(op);
  183. ops:=3;
  184. loadconst(0,_op1);
  185. loadreg(1,_op2);
  186. loadconst(2,_op3);
  187. end;
  188. constructor taicpu.op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  189. begin
  190. inherited create(op);
  191. ops:=3;
  192. loadconst(0,_op1);
  193. loadconst(1,_op2);
  194. loadconst(2,_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: aint);
  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 : aint);
  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 : aint);
  234. begin
  235. inherited create(op);
  236. ops:=5;
  237. loadreg(0,_op1);
  238. loadreg(1,_op2);
  239. loadconst(2,_op3);
  240. loadconst(3,_op4);
  241. loadconst(4,_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 : aint; _op3: tasmsymbol);
  251. begin
  252. inherited create(op);
  253. ops:=3;
  254. loadconst(0,_op1);
  255. loadconst(1,_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:aint);
  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 : aint);
  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:aint;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(regtype: Tregistertype):boolean;
  286. begin
  287. result :=
  288. (((opcode=A_MR) and
  289. (regtype = R_INTREGISTER)) or
  290. ((opcode = A_FMR) and
  291. (regtype = R_FPUREGISTER))) and
  292. { these opcodes can only have registers as operands }
  293. (oper[0]^.reg=oper[1]^.reg);
  294. end;
  295. function taicpu.spilling_get_operation_type(opnr: aint): topertype;
  296. begin
  297. result := operand_read;
  298. case opcode of
  299. A_STMW,A_LMW:
  300. internalerror(2005021805);
  301. A_STBU, A_STBUX, A_STHU, A_STHUX, A_STWU, A_STWUX, A_STFSU, A_STFSUX, A_STFDU, A_STFDUX, A_STB, A_STBX, A_STH, A_STHX, A_STW, A_STWX, A_STFS, A_STFSX, A_STFD, A_STFDX, A_STFIWX, A_STHBRX, A_STWBRX, A_STWCX_, A_CMP, A_CMPI, A_CMPL, A_CMPLI, A_DCBA, A_DCBI, A_DCBST, A_DCBT, A_DCBTST, A_DCBZ, A_ECOWX, A_FCMPO, A_FCMPU, A_MTMSR, A_TLBIE, A_TW, A_TWI, A_CMPWI, A_CMPW, A_CMPLWI, A_CMPLW, A_MT, A_MTLR, A_MTCTR:;
  302. else
  303. if opnr = 0 then
  304. result := operand_write;
  305. end;
  306. end;
  307. function taicpu.spilling_get_operation_type_ref(opnr: aint; reg: tregister): topertype;
  308. begin
  309. result := operand_read;
  310. case opcode of
  311. A_STBU, A_STBUX, A_STHU, A_STHUX, A_STWU, A_STWUX, A_STFSU, A_STFSUX, A_STFDU, A_STFDUX:
  312. if (oper[opnr]^.ref^.base = reg) then
  313. result := operand_readwrite;
  314. end;
  315. end;
  316. function spilling_create_load(const ref:treference;r:tregister): tai;
  317. begin
  318. case getregtype(r) of
  319. R_INTREGISTER:
  320. result:=taicpu.op_reg_ref(A_LWZ,r,ref);
  321. R_FPUREGISTER:
  322. result:=taicpu.op_reg_ref(A_LFD,r,ref);
  323. else
  324. internalerror(2005123101);
  325. end;
  326. end;
  327. function spilling_create_store(r:tregister; const ref:treference): tai;
  328. begin
  329. case getregtype(r) of
  330. R_INTREGISTER:
  331. result:=taicpu.op_reg_ref(A_STW,r,ref);
  332. R_FPUREGISTER:
  333. result:=taicpu.op_reg_ref(A_STFD,r,ref);
  334. else
  335. internalerror(2005123102);
  336. end;
  337. end;
  338. procedure InitAsm;
  339. begin
  340. end;
  341. procedure DoneAsm;
  342. begin
  343. end;
  344. procedure fixup_jmps(list: TAsmList);
  345. var
  346. p: tai;
  347. newjmp: taicpu;
  348. labelpositions: TFPList;
  349. instrpos: ptrint;
  350. l: tasmlabel;
  351. inserted_something: boolean;
  352. begin
  353. // if certainly not enough instructions to cause an overflow, don't bother
  354. if (list.count <= (high(smallint) div 4)) then
  355. exit;
  356. labelpositions := TFPList.create;
  357. p := tai(list.first);
  358. instrpos := 1;
  359. // record label positions
  360. while assigned(p) do
  361. begin
  362. if p.typ = ait_label then
  363. begin
  364. if (tai_label(p).labsym.labelnr >= labelpositions.count) then
  365. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  366. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  367. end;
  368. if p.typ = ait_instruction then
  369. inc(instrpos);
  370. p := tai(p.next);
  371. end;
  372. // check and fix distances
  373. repeat
  374. inserted_something := false;
  375. p := tai(list.first);
  376. instrpos := 1;
  377. while assigned(p) do
  378. begin
  379. case p.typ of
  380. ait_label:
  381. // update labelposition in case it changed due to insertion
  382. // of jumps
  383. begin
  384. // can happen because of newly inserted labels
  385. if (tai_label(p).labsym.labelnr > labelpositions.count) then
  386. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  387. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  388. end;
  389. ait_instruction:
  390. begin
  391. inc(instrpos);
  392. case taicpu(p).opcode of
  393. A_BC:
  394. if (taicpu(p).oper[0]^.typ = top_ref) and
  395. assigned(taicpu(p).oper[0]^.ref^.symbol) and
  396. (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) and
  397. (labelpositions[tasmlabel(taicpu(p).oper[0]^.ref^.symbol).labelnr] <> NIL) and
  398. (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
  399. begin
  400. // add a new label after this jump
  401. current_asmdata.getjumplabel(l);
  402. list.insertafter(tai_label.create(l),p);
  403. // add a new unconditional jump between this jump and the label
  404. newjmp := taicpu.op_sym(A_B,taicpu(p).oper[0]^.ref^.symbol);
  405. newjmp.is_jmp := true;
  406. newjmp.fileinfo := taicpu(p).fileinfo;
  407. list.insertafter(newjmp,p);
  408. inc(instrpos);
  409. // change the conditional jump to point to the newly inserted label
  410. tasmlabel(taicpu(p).oper[0]^.ref^.symbol).decrefs;
  411. taicpu(p).oper[0]^.ref^.symbol := l;
  412. l.increfs;
  413. // and invert its condition code
  414. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  415. // we inserted an instruction, so will have to check everything again
  416. inserted_something := true;
  417. end;
  418. end;
  419. end;
  420. end;
  421. p := tai(p.next);
  422. end;
  423. until not inserted_something;
  424. labelpositions.free;
  425. end;
  426. begin
  427. cai_align:=tai_align;
  428. cai_cpu:=taicpu;
  429. end.