aasmcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. {
  2. Copyright (c) 1999-2009 by Mazen Neifer and David Zhang
  3. Contains the assembler object for the MIPSEL
  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. cclasses,
  22. globtype, globals, verbose,
  23. aasmbase, aasmdata, aasmsym, aasmtai,
  24. cgbase, cgutils, cpubase, cpuinfo;
  25. const
  26. { "mov reg,reg" source operand number }
  27. O_MOV_SOURCE = 0;
  28. { "mov reg,reg" source operand number }
  29. O_MOV_DEST = 1;
  30. type
  31. { taicpu }
  32. taicpu = class(tai_cpu_abstract_sym)
  33. delayslot_annulled: boolean; { conditinal opcode with ,a }
  34. constructor op_none(op: tasmop);
  35. constructor op_reg(op: tasmop; _op1: tregister);
  36. constructor op_const(op: tasmop; _op1: longint);
  37. constructor op_ref(op: tasmop; const _op1: treference);
  38. constructor op_reg_reg(op: tasmop; _op1, _op2: tregister);
  39. constructor op_reg_ref(op: tasmop; _op1: tregister; const _op2: treference);
  40. constructor op_reg_const(op: tasmop; _op1: tregister; _op2: longint);
  41. constructor op_const_const(op: tasmop; _op1: aint; _op2: aint);
  42. constructor op_reg_reg_reg(op: tasmop; _op1, _op2, _op3: tregister);
  43. constructor op_reg_reg_ref(op: tasmop; _op1, _op2: tregister; const _op3: treference);
  44. constructor op_reg_reg_const(op: tasmop; _op1, _op2: tregister; _op3: aint);
  45. constructor op_reg_const_reg(op: tasmop; _op1: tregister; _op2: aint; _op3: tregister);
  46. { this is for Jmp instructions }
  47. constructor op_sym(op: tasmop; _op1: tasmsymbol);
  48. constructor op_reg_reg_sym(op: tasmop; _op1, _op2: tregister; _op3: tasmsymbol);
  49. constructor op_reg_sym(op: tasmop; _op1: tregister; _op2: tasmsymbol);
  50. constructor op_sym_ofs(op: tasmop; _op1: tasmsymbol; _op1ofs: longint);
  51. { register allocation }
  52. function is_same_reg_move(regtype: Tregistertype): boolean; override;
  53. { register spilling code }
  54. function spilling_get_operation_type(opnr: longint): topertype; override;
  55. end;
  56. tai_align = class(tai_align_abstract)
  57. { nothing to add }
  58. end;
  59. procedure InitAsm;
  60. procedure DoneAsm;
  61. procedure fixup_jmps(list: TAsmList);
  62. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  63. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  64. implementation
  65. uses
  66. cutils;
  67. {*****************************************************************************
  68. taicpu Constructors
  69. *****************************************************************************}
  70. constructor taicpu.op_none(op: tasmop);
  71. begin
  72. inherited Create(op);
  73. end;
  74. constructor taicpu.op_reg(op: tasmop; _op1: tregister);
  75. begin
  76. inherited Create(op);
  77. ops := 1;
  78. loadreg(0, _op1);
  79. end;
  80. constructor taicpu.op_ref(op: tasmop; const _op1: treference);
  81. begin
  82. inherited Create(op);
  83. ops := 1;
  84. loadref(0, _op1);
  85. end;
  86. constructor taicpu.op_const(op: tasmop; _op1: longint);
  87. begin
  88. inherited Create(op);
  89. ops := 1;
  90. loadconst(0, _op1);
  91. end;
  92. constructor taicpu.op_reg_reg(op: tasmop; _op1, _op2: tregister);
  93. begin
  94. inherited Create(op);
  95. ops := 2;
  96. loadreg(0, _op1);
  97. loadreg(1, _op2);
  98. end;
  99. constructor taicpu.op_reg_const(op: tasmop; _op1: tregister; _op2: longint);
  100. begin
  101. inherited Create(op);
  102. ops := 2;
  103. loadreg(0, _op1);
  104. loadconst(1, _op2);
  105. end;
  106. constructor taicpu.op_const_const(op: tasmop; _op1: aint; _op2: aint);
  107. begin
  108. inherited Create(op);
  109. ops := 2;
  110. loadconst(0, _op1);
  111. loadconst(1, _op2);
  112. end;
  113. constructor taicpu.op_reg_ref(op: tasmop; _op1: tregister; const _op2: treference);
  114. begin
  115. inherited Create(op);
  116. ops := 2;
  117. loadreg(0, _op1);
  118. loadref(1, _op2);
  119. end;
  120. constructor taicpu.op_reg_reg_reg(op: tasmop; _op1, _op2, _op3: tregister);
  121. begin
  122. inherited Create(op);
  123. ops := 3;
  124. loadreg(0, _op1);
  125. loadreg(1, _op2);
  126. loadreg(2, _op3);
  127. end;
  128. constructor taicpu.op_reg_reg_ref(op: tasmop; _op1, _op2: tregister; const _op3: treference);
  129. begin
  130. inherited create(op);
  131. ops := 3;
  132. loadreg(0, _op1);
  133. loadreg(1, _op2);
  134. loadref(2, _op3);
  135. end;
  136. constructor taicpu.op_reg_reg_const(op: tasmop; _op1, _op2: tregister; _op3: aint);
  137. begin
  138. inherited create(op);
  139. ops := 3;
  140. loadreg(0, _op1);
  141. loadreg(1, _op2);
  142. loadconst(2, _op3);
  143. end;
  144. constructor taicpu.op_reg_const_reg(op: tasmop; _op1: tregister; _op2: aint;
  145. _op3: tregister);
  146. begin
  147. inherited create(op);
  148. ops := 3;
  149. loadreg(0, _op1);
  150. loadconst(1, _op2);
  151. loadreg(2, _op3);
  152. end;
  153. constructor taicpu.op_sym(op: tasmop; _op1: tasmsymbol);
  154. begin
  155. inherited Create(op);
  156. is_jmp := op in [A_BC, A_BA];
  157. ops := 1;
  158. loadsymbol(0, _op1, 0);
  159. end;
  160. constructor taicpu.op_reg_reg_sym(op: tasmop; _op1, _op2: tregister; _op3: tasmsymbol);
  161. begin
  162. inherited create(op);
  163. is_jmp := op in [A_BC, A_BA];
  164. ops := 3;
  165. loadreg(0, _op1);
  166. loadreg(1, _op2);
  167. loadsymbol(2, _op3, 0);
  168. end;
  169. constructor taicpu.op_reg_sym(op: tasmop; _op1: tregister; _op2: tasmsymbol);
  170. begin
  171. inherited create(op);
  172. is_jmp := op in [A_BC, A_BA];
  173. ops := 2;
  174. loadreg(0, _op1);
  175. loadsymbol(1, _op2, 0);
  176. end;
  177. constructor taicpu.op_sym_ofs(op: tasmop; _op1: tasmsymbol; _op1ofs: longint);
  178. begin
  179. inherited Create(op);
  180. ops := 1;
  181. loadsymbol(0, _op1, _op1ofs);
  182. end;
  183. function taicpu.is_same_reg_move(regtype: Tregistertype): boolean;
  184. begin
  185. Result := (
  186. ((opcode = A_MOVE) and (regtype = R_INTREGISTER)) or
  187. ((regtype = R_FPUREGISTER) and (opcode in [A_MOV_S, A_MOV_D]))
  188. ) and
  189. (oper[0]^.reg = oper[1]^.reg);
  190. end;
  191. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  192. type
  193. op_write_set_type = set of TAsmOp;
  194. const
  195. op_write_set: op_write_set_type =
  196. [A_NEG,
  197. A_NEGU,
  198. A_LI,
  199. A_DLI,
  200. A_LA,
  201. A_MOVE,
  202. A_LB,
  203. A_LBU,
  204. A_LH,
  205. A_LHU,
  206. A_LW,
  207. A_LWU,
  208. A_LWL,
  209. A_LWR,
  210. A_LD,
  211. A_LDL,
  212. A_LDR,
  213. A_LL,
  214. A_LLD,
  215. A_ADDI,
  216. A_DADDI,
  217. A_ADDIU,
  218. A_DADDIU,
  219. A_SLTI,
  220. A_SLTIU,
  221. A_ANDI,
  222. A_ORI,
  223. A_XORI,
  224. A_LUI,
  225. A_DNEG,
  226. A_DNEGU,
  227. A_ADD,
  228. A_DADD,
  229. A_ADDU,
  230. A_DADDU,
  231. A_SUB,
  232. A_DSUB,
  233. A_SUBU,
  234. A_DSUBU,
  235. A_SLT,
  236. A_SLTU,
  237. A_AND,
  238. A_OR,
  239. A_XOR,
  240. A_NOR,
  241. { We can get into trouble if an instruction can be interpreted as
  242. macros with different operands. The following commented out ones
  243. refer to elementary instructions: DIV[U], MULT[U] do not modify
  244. first operand. Rest are subject to check. }
  245. A_MUL,
  246. A_MULO,
  247. A_MULOU,
  248. A_DMUL,
  249. A_DMULO,
  250. A_DMULOU,
  251. // A_DIV,
  252. // A_DIVU,
  253. A_DDIV,
  254. A_DDIVU,
  255. A_REM,
  256. A_REMU,
  257. A_DREM,
  258. A_DREMU,
  259. // A_MULT,
  260. A_DMULT,
  261. // A_MULTU,
  262. A_DMULTU,
  263. A_MFHI,
  264. A_MFLO,
  265. A_MULTG,
  266. A_DMULTG,
  267. A_MULTUG,
  268. A_DMULTUG,
  269. A_DIVG,
  270. A_DDIVG,
  271. A_DIVUG,
  272. A_DDIVUG,
  273. A_MODG,
  274. A_DMODG,
  275. A_MODUG,
  276. A_DMODUG,
  277. A_SLL,
  278. A_SRL,
  279. A_SRA,
  280. A_SLLV,
  281. A_SRLV,
  282. A_SRAV,
  283. A_DSLL,
  284. A_DSRL,
  285. A_DSRA,
  286. A_DSLLV,
  287. A_DSRLV,
  288. A_DSRAV,
  289. A_DSLL32,
  290. A_DSRL32,
  291. A_DSRA32,
  292. A_LWC1,
  293. A_LDC1,
  294. A_ADD_S,
  295. A_ADD_D,
  296. A_SUB_S,
  297. A_SUB_D,
  298. A_MUL_S,
  299. A_MUL_D,
  300. A_DIV_S,
  301. A_DIV_D,
  302. A_ABS_S,
  303. A_ABS_D,
  304. A_NEG_S,
  305. A_NEG_D,
  306. A_SQRT_S,
  307. A_SQRT_D,
  308. A_MOV_S,
  309. A_MOV_D,
  310. A_CVT_S_D,
  311. A_CVT_S_W,
  312. A_CVT_S_L,
  313. A_CVT_D_S,
  314. A_CVT_D_W,
  315. A_CVT_D_L,
  316. A_CVT_W_S,
  317. A_CVT_W_D,
  318. A_CVT_L_S,
  319. A_CVT_L_D,
  320. A_ROUND_W_S,
  321. A_ROUND_W_D,
  322. A_ROUND_L_S,
  323. A_ROUND_L_D,
  324. A_TRUNC_W_S,
  325. A_TRUNC_W_D,
  326. A_TRUNC_L_S,
  327. A_TRUNC_L_D,
  328. A_CEIL_W_S,
  329. A_CEIL_W_D,
  330. A_CEIL_L_S,
  331. A_CEIL_L_D,
  332. A_FLOOR_W_S,
  333. A_FLOOR_W_D,
  334. A_FLOOR_L_S,
  335. A_FLOOR_L_D,
  336. A_SEQ,
  337. A_SGE,
  338. A_SGEU,
  339. A_SGT,
  340. A_SGTU,
  341. A_SLE,
  342. A_SLEU,
  343. A_SNE];
  344. begin
  345. result := operand_read;
  346. if opcode in op_write_set then
  347. if opnr = 0 then
  348. result := operand_write;
  349. end;
  350. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  351. begin
  352. case getregtype(r) of
  353. R_INTREGISTER :
  354. result:=taicpu.op_reg_ref(A_LW,r,ref);
  355. R_FPUREGISTER :
  356. begin
  357. case getsubreg(r) of
  358. R_SUBFS :
  359. result:=taicpu.op_reg_ref(A_LWC1,r,ref);
  360. R_SUBFD :
  361. result:=taicpu.op_reg_ref(A_LDC1,r,ref);
  362. else
  363. internalerror(200401042);
  364. end;
  365. end
  366. else
  367. internalerror(200401041);
  368. end;
  369. end;
  370. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  371. begin
  372. case getregtype(r) of
  373. R_INTREGISTER :
  374. result:=taicpu.op_reg_ref(A_SW,r,ref);
  375. R_FPUREGISTER :
  376. begin
  377. case getsubreg(r) of
  378. R_SUBFS :
  379. result:=taicpu.op_reg_ref(A_SWC1,r,ref);
  380. R_SUBFD :
  381. result:=taicpu.op_reg_ref(A_SDC1,r,ref);
  382. else
  383. internalerror(200401042);
  384. end;
  385. end
  386. else
  387. internalerror(200401041);
  388. end;
  389. end;
  390. procedure InitAsm;
  391. begin
  392. end;
  393. procedure DoneAsm;
  394. begin
  395. end;
  396. procedure fixup_jmps(list: TAsmList);
  397. var
  398. p,pdelayslot: tai;
  399. newcomment: tai_comment;
  400. newjmp,newnoop: taicpu;
  401. labelpositions: TFPList;
  402. instrpos: ptrint;
  403. l: tasmlabel;
  404. inserted_something: boolean;
  405. begin
  406. // if certainly not enough instructions to cause an overflow, dont bother
  407. if (list.count <= (high(smallint) div 4)) then
  408. exit;
  409. labelpositions := TFPList.create;
  410. p := tai(list.first);
  411. instrpos := 1;
  412. // record label positions
  413. while assigned(p) do
  414. begin
  415. if p.typ = ait_label then
  416. begin
  417. if (tai_label(p).labsym.labelnr >= labelpositions.count) then
  418. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  419. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  420. end;
  421. { ait_const is for jump tables }
  422. case p.typ of
  423. ait_instruction:
  424. { probleim here: pseudo-instructions can translate into
  425. several CPU instructions, possibly depending on assembler options,
  426. to obe on safe side, let's assume a mean of two. }
  427. inc(instrpos,2);
  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. { If the number of instructions is below limit, we can't overflow either }
  438. if (instrpos <= (high(smallint) div 4)) then
  439. exit;
  440. // check and fix distances
  441. repeat
  442. inserted_something := false;
  443. p := tai(list.first);
  444. instrpos := 1;
  445. while assigned(p) do
  446. begin
  447. case p.typ of
  448. ait_label:
  449. // update labelposition in case it changed due to insertion
  450. // of jumps
  451. begin
  452. // can happen because of newly inserted labels
  453. if (tai_label(p).labsym.labelnr > labelpositions.count) then
  454. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  455. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  456. end;
  457. ait_instruction:
  458. begin
  459. inc(instrpos,2);
  460. case taicpu(p).opcode of
  461. A_BA:
  462. if (taicpu(p).oper[0]^.typ = top_ref) and
  463. assigned(taicpu(p).oper[0]^.ref^.symbol) and
  464. (taicpu(p).oper[0]^.ref^.symbol is tasmlabel) and
  465. (labelpositions[tasmlabel(taicpu(p).oper[0]^.ref^.symbol).labelnr] <> NIL) and
  466. {$push}
  467. {$q-}
  468. (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
  469. {$pop}
  470. begin
  471. { This is not PIC safe }
  472. taicpu(p).opcode:=A_J;
  473. newcomment:=tai_comment.create(strpnew('fixup_jmps, A_BA changed into A_J'));
  474. list.insertbefore(newcomment,p);
  475. end;
  476. A_BC:
  477. if (taicpu(p).ops=3) and (taicpu(p).oper[2]^.typ = top_ref) and
  478. assigned(taicpu(p).oper[2]^.ref^.symbol) and
  479. (taicpu(p).oper[2]^.ref^.symbol is tasmlabel) and
  480. (labelpositions[tasmlabel(taicpu(p).oper[2]^.ref^.symbol).labelnr] <> NIL) and
  481. {$push}
  482. {$q-}
  483. (ptruint(abs(ptrint(labelpositions[tasmlabel(taicpu(p).oper[2]^.ref^.symbol).labelnr]-instrpos)) - (low(smallint) div 4)) > ptruint((high(smallint) - low(smallint)) div 4)) then
  484. {$pop}
  485. begin
  486. // add a new label after this jump
  487. current_asmdata.getjumplabel(l);
  488. { new label -> may have to increase array size }
  489. if (l.labelnr >= labelpositions.count) then
  490. labelpositions.count := l.labelnr + 10;
  491. { newjmp will be inserted before the label, and it's inserted after }
  492. { plus delay slot }
  493. { the current jump -> instrpos+3 }
  494. labelpositions[l.labelnr] := pointer(instrpos+2*3);
  495. pdelayslot:=tai(p.next);
  496. { We need to insert the new instruction after the delay slot instruction ! }
  497. while assigned(pdelayslot) and (pdelayslot.typ<>ait_instruction) do
  498. pdelayslot:=tai(pdelayslot.next);
  499. list.insertafter(tai_label.create(l),pdelayslot);
  500. // add a new unconditional jump between this jump and the label
  501. newcomment:=tai_comment.create(strpnew('fixup_jmps, A_BXX changed into A_BNOTXX label;A_J;label:'));
  502. list.insertbefore(newcomment,p);
  503. newjmp := taicpu.op_sym(A_J,taicpu(p).oper[2]^.ref^.symbol);
  504. newjmp.is_jmp := true;
  505. newjmp.fileinfo := taicpu(p).fileinfo;
  506. list.insertafter(newjmp,pdelayslot);
  507. inc(instrpos,2);
  508. { Add a delay slot for new A_J instruction }
  509. newnoop:=taicpu.op_none(A_NOP);
  510. newnoop.fileinfo := taicpu(p).fileinfo;
  511. list.insertafter(newnoop,newjmp);
  512. inc(instrpos,2);
  513. // change the conditional jump to point to the newly inserted label
  514. tasmlabel(taicpu(p).oper[2]^.ref^.symbol).decrefs;
  515. taicpu(p).oper[2]^.ref^.symbol := l;
  516. l.increfs;
  517. // and invert its condition code
  518. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  519. // we inserted an instruction, so will have to check everything again
  520. inserted_something := true;
  521. end;
  522. end;
  523. end;
  524. ait_const:
  525. inc(instrpos);
  526. end;
  527. p := tai(p.next);
  528. end;
  529. until not inserted_something;
  530. labelpositions.free;
  531. end;
  532. begin
  533. cai_cpu := taicpu;
  534. cai_align := tai_align;
  535. end.