aasmcpu.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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 = 1;
  28. { "mov reg,reg" source operand number }
  29. O_MOV_DEST = 0;
  30. type
  31. { taicpu }
  32. taicpu = class(tai_cpu_abstract_sym)
  33. constructor op_none(op: tasmop);
  34. constructor op_reg(op: tasmop; _op1: tregister);
  35. constructor op_const(op: tasmop; _op1: longint);
  36. constructor op_ref(op: tasmop; const _op1: treference);
  37. constructor op_reg_reg(op: tasmop; _op1, _op2: tregister);
  38. constructor op_reg_ref(op: tasmop; _op1: tregister; const _op2: treference);
  39. constructor op_reg_const(op: tasmop; _op1: tregister; _op2: longint);
  40. constructor op_const_const(op: tasmop; _op1: aint; _op2: aint);
  41. constructor op_reg_reg_reg(op: tasmop; _op1, _op2, _op3: tregister);
  42. constructor op_reg_reg_ref(op: tasmop; _op1, _op2: tregister; const _op3: treference);
  43. constructor op_reg_reg_const(op: tasmop; _op1, _op2: tregister; _op3: aint);
  44. { INS and EXT }
  45. constructor op_reg_reg_const_const(op: tasmop; _op1,_op2: tregister; _op3,_op4: aint);
  46. constructor op_reg_const_reg(op: tasmop; _op1: tregister; _op2: aint; _op3: tregister);
  47. { this is for Jmp instructions }
  48. constructor op_sym(op: tasmop; _op1: tasmsymbol);
  49. constructor op_reg_reg_sym(op: tasmop; _op1, _op2: tregister; _op3: tasmsymbol);
  50. constructor op_reg_sym(op: tasmop; _op1: tregister; _op2: tasmsymbol);
  51. constructor op_sym_ofs(op: tasmop; _op1: tasmsymbol; _op1ofs: longint);
  52. { register allocation }
  53. function is_same_reg_move(regtype: Tregistertype): boolean; override;
  54. { register spilling code }
  55. function spilling_get_operation_type(opnr: longint): topertype; override;
  56. function is_macro: boolean;
  57. end;
  58. tai_align = class(tai_align_abstract)
  59. { nothing to add }
  60. end;
  61. procedure InitAsm;
  62. procedure DoneAsm;
  63. procedure fixup_jmps(list: TAsmList);
  64. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  65. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  66. procedure resolveReadAfterWrite(list: TAsmList);
  67. implementation
  68. uses
  69. cutils;
  70. {*****************************************************************************
  71. taicpu Constructors
  72. *****************************************************************************}
  73. constructor taicpu.op_none(op: tasmop);
  74. begin
  75. inherited Create(op);
  76. end;
  77. constructor taicpu.op_reg(op: tasmop; _op1: tregister);
  78. begin
  79. inherited Create(op);
  80. ops := 1;
  81. loadreg(0, _op1);
  82. end;
  83. constructor taicpu.op_ref(op: tasmop; const _op1: treference);
  84. begin
  85. inherited Create(op);
  86. ops := 1;
  87. loadref(0, _op1);
  88. end;
  89. constructor taicpu.op_const(op: tasmop; _op1: longint);
  90. begin
  91. inherited Create(op);
  92. ops := 1;
  93. loadconst(0, _op1);
  94. end;
  95. constructor taicpu.op_reg_reg(op: tasmop; _op1, _op2: tregister);
  96. begin
  97. inherited Create(op);
  98. ops := 2;
  99. loadreg(0, _op1);
  100. loadreg(1, _op2);
  101. end;
  102. constructor taicpu.op_reg_const(op: tasmop; _op1: tregister; _op2: longint);
  103. begin
  104. inherited Create(op);
  105. ops := 2;
  106. loadreg(0, _op1);
  107. loadconst(1, _op2);
  108. end;
  109. constructor taicpu.op_const_const(op: tasmop; _op1: aint; _op2: aint);
  110. begin
  111. inherited Create(op);
  112. ops := 2;
  113. loadconst(0, _op1);
  114. loadconst(1, _op2);
  115. end;
  116. constructor taicpu.op_reg_ref(op: tasmop; _op1: tregister; const _op2: treference);
  117. begin
  118. inherited Create(op);
  119. ops := 2;
  120. loadreg(0, _op1);
  121. loadref(1, _op2);
  122. end;
  123. constructor taicpu.op_reg_reg_reg(op: tasmop; _op1, _op2, _op3: tregister);
  124. begin
  125. inherited Create(op);
  126. ops := 3;
  127. loadreg(0, _op1);
  128. loadreg(1, _op2);
  129. loadreg(2, _op3);
  130. end;
  131. constructor taicpu.op_reg_reg_ref(op: tasmop; _op1, _op2: tregister; const _op3: treference);
  132. begin
  133. inherited create(op);
  134. ops := 3;
  135. loadreg(0, _op1);
  136. loadreg(1, _op2);
  137. loadref(2, _op3);
  138. end;
  139. constructor taicpu.op_reg_reg_const(op: tasmop; _op1, _op2: tregister; _op3: aint);
  140. begin
  141. inherited create(op);
  142. ops := 3;
  143. loadreg(0, _op1);
  144. loadreg(1, _op2);
  145. loadconst(2, _op3);
  146. end;
  147. constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  148. begin
  149. inherited create(op);
  150. ops := 4;
  151. loadreg(0, _op1);
  152. loadreg(1, _op2);
  153. loadconst(2, _op3);
  154. loadconst(3, _op4);
  155. end;
  156. constructor taicpu.op_reg_const_reg(op: tasmop; _op1: tregister; _op2: aint;
  157. _op3: tregister);
  158. begin
  159. inherited create(op);
  160. ops := 3;
  161. loadreg(0, _op1);
  162. loadconst(1, _op2);
  163. loadreg(2, _op3);
  164. end;
  165. constructor taicpu.op_sym(op: tasmop; _op1: tasmsymbol);
  166. begin
  167. inherited Create(op);
  168. is_jmp := op in [A_BC, A_BA];
  169. ops := 1;
  170. loadsymbol(0, _op1, 0);
  171. end;
  172. constructor taicpu.op_reg_reg_sym(op: tasmop; _op1, _op2: tregister; _op3: tasmsymbol);
  173. begin
  174. inherited create(op);
  175. is_jmp := op in [A_BC, A_BA];
  176. ops := 3;
  177. loadreg(0, _op1);
  178. loadreg(1, _op2);
  179. loadsymbol(2, _op3, 0);
  180. end;
  181. constructor taicpu.op_reg_sym(op: tasmop; _op1: tregister; _op2: tasmsymbol);
  182. begin
  183. inherited create(op);
  184. is_jmp := op in [A_BC, A_BA];
  185. ops := 2;
  186. loadreg(0, _op1);
  187. loadsymbol(1, _op2, 0);
  188. end;
  189. constructor taicpu.op_sym_ofs(op: tasmop; _op1: tasmsymbol; _op1ofs: longint);
  190. begin
  191. inherited Create(op);
  192. ops := 1;
  193. loadsymbol(0, _op1, _op1ofs);
  194. end;
  195. function taicpu.is_same_reg_move(regtype: Tregistertype): boolean;
  196. begin
  197. Result := (
  198. ((opcode = A_MOVE) and (regtype = R_INTREGISTER)) or
  199. ((regtype = R_FPUREGISTER) and (opcode in [A_MOV_S, A_MOV_D]))
  200. ) and
  201. (oper[0]^.reg = oper[1]^.reg);
  202. end;
  203. function taicpu.is_macro: boolean;
  204. begin
  205. result :=
  206. { 'seq', 'sge', 'sgeu', 'sgt', 'sgtu', 'sle', 'sleu', 'sne', }
  207. (opcode=A_SEQ) or (opcode=A_SGE) or (opcode=A_SGEU) or (opcode=A_SGT) or
  208. (opcode=A_SGTU) or (opcode=A_SLE) or (opcode=A_SLEU) or (opcode=A_SNE)
  209. { JAL is a macro in pic code mode }
  210. or ((opcode=A_JAL) and (cs_create_pic in current_settings.moduleswitches))
  211. or (opcode=A_LA) or ((opcode=A_BC) and
  212. not (condition in [C_EQ,C_NE,C_GTZ,C_GEZ,C_LTZ,C_LEZ,C_COP1TRUE,C_COP1FALSE]))
  213. or (opcode=A_REM) or (opcode=A_REMU)
  214. { DIV and DIVU are normally macros, but use $zero as first arg to generate a CPU instruction. }
  215. or (((opcode=A_DIV) or (opcode=A_DIVU)) and
  216. ((ops<>3) or (oper[0]^.typ<>top_reg) or (oper[0]^.reg<>NR_R0)))
  217. or (opcode=A_MULO) or (opcode=A_MULOU)
  218. { A_LI is only a macro if the immediate is not in thez 16-bit range }
  219. or (opcode=A_LI);
  220. end;
  221. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  222. type
  223. op_write_set_type = set of TAsmOp;
  224. const
  225. op_write_set: op_write_set_type =
  226. [A_NEG,
  227. A_NEGU,
  228. A_LI,
  229. A_DLI,
  230. A_LA,
  231. A_MOVE,
  232. A_LB,
  233. A_LBU,
  234. A_LH,
  235. A_LHU,
  236. A_LW,
  237. A_LWU,
  238. A_LWL,
  239. A_LWR,
  240. A_LD,
  241. A_LDL,
  242. A_LDR,
  243. A_LL,
  244. A_LLD,
  245. A_ADDI,
  246. A_DADDI,
  247. A_ADDIU,
  248. A_DADDIU,
  249. A_SLTI,
  250. A_SLTIU,
  251. A_ANDI,
  252. A_ORI,
  253. A_XORI,
  254. A_LUI,
  255. A_DNEG,
  256. A_DNEGU,
  257. A_ADD,
  258. A_DADD,
  259. A_ADDU,
  260. A_DADDU,
  261. A_SUB,
  262. A_DSUB,
  263. A_SUBU,
  264. A_DSUBU,
  265. A_SLT,
  266. A_SLTU,
  267. A_AND,
  268. A_OR,
  269. A_XOR,
  270. A_NOR,
  271. { We can get into trouble if an instruction can be interpreted as
  272. macros with different operands. The following commented out ones
  273. refer to elementary instructions: DIV[U], MULT[U] do not modify
  274. first operand. Rest are subject to check. }
  275. A_MUL,
  276. A_MULO,
  277. A_MULOU,
  278. A_DMUL,
  279. A_DMULO,
  280. A_DMULOU,
  281. // A_DIV,
  282. // A_DIVU,
  283. A_DDIV,
  284. A_DDIVU,
  285. A_REM,
  286. A_REMU,
  287. A_DREM,
  288. A_DREMU,
  289. // A_MULT,
  290. A_DMULT,
  291. // A_MULTU,
  292. A_DMULTU,
  293. A_MFHI,
  294. A_MFLO,
  295. A_SLL,
  296. A_SRL,
  297. A_SRA,
  298. A_SLLV,
  299. A_SRLV,
  300. A_SRAV,
  301. A_DSLL,
  302. A_DSRL,
  303. A_DSRA,
  304. A_DSLLV,
  305. A_DSRLV,
  306. A_DSRAV,
  307. A_DSLL32,
  308. A_DSRL32,
  309. A_DSRA32,
  310. A_LWC1,
  311. A_LDC1,
  312. A_ADD_S,
  313. A_ADD_D,
  314. A_SUB_S,
  315. A_SUB_D,
  316. A_MUL_S,
  317. A_MUL_D,
  318. A_DIV_S,
  319. A_DIV_D,
  320. A_ABS_S,
  321. A_ABS_D,
  322. A_NEG_S,
  323. A_NEG_D,
  324. A_SQRT_S,
  325. A_SQRT_D,
  326. A_MOV_S,
  327. A_MOV_D,
  328. A_CVT_S_D,
  329. A_CVT_S_W,
  330. A_CVT_S_L,
  331. A_CVT_D_S,
  332. A_CVT_D_W,
  333. A_CVT_D_L,
  334. A_CVT_W_S,
  335. A_CVT_W_D,
  336. A_CVT_L_S,
  337. A_CVT_L_D,
  338. A_ROUND_W_S,
  339. A_ROUND_W_D,
  340. A_ROUND_L_S,
  341. A_ROUND_L_D,
  342. A_TRUNC_W_S,
  343. A_TRUNC_W_D,
  344. A_TRUNC_L_S,
  345. A_TRUNC_L_D,
  346. A_CEIL_W_S,
  347. A_CEIL_W_D,
  348. A_CEIL_L_S,
  349. A_CEIL_L_D,
  350. A_FLOOR_W_S,
  351. A_FLOOR_W_D,
  352. A_FLOOR_L_S,
  353. A_FLOOR_L_D,
  354. A_SEQ,
  355. A_SGE,
  356. A_SGEU,
  357. A_SGT,
  358. A_SGTU,
  359. A_SLE,
  360. A_SLEU,
  361. A_SNE,
  362. A_EXT,
  363. A_INS,
  364. A_MFC0,
  365. A_SEB,
  366. A_SEH];
  367. begin
  368. result := operand_read;
  369. case opcode of
  370. A_DIV, { these have 3 operands if used as macros }
  371. A_DIVU:
  372. if (ops=3) and (opnr=0) then
  373. result:=operand_write;
  374. else
  375. if opcode in op_write_set then
  376. if opnr = 0 then
  377. result := operand_write;
  378. end;
  379. end;
  380. function spilling_create_load(const ref: treference; r: tregister): taicpu;
  381. begin
  382. case getregtype(r) of
  383. R_INTREGISTER :
  384. result:=taicpu.op_reg_ref(A_LW,r,ref);
  385. R_FPUREGISTER :
  386. begin
  387. case getsubreg(r) of
  388. R_SUBFS :
  389. result:=taicpu.op_reg_ref(A_LWC1,r,ref);
  390. R_SUBFD :
  391. result:=taicpu.op_reg_ref(A_LDC1,r,ref);
  392. else
  393. internalerror(2004010418);
  394. end;
  395. end
  396. else
  397. internalerror(2004010408);
  398. end;
  399. end;
  400. function spilling_create_store(r: tregister; const ref: treference): taicpu;
  401. begin
  402. case getregtype(r) of
  403. R_INTREGISTER :
  404. result:=taicpu.op_reg_ref(A_SW,r,ref);
  405. R_FPUREGISTER :
  406. begin
  407. case getsubreg(r) of
  408. R_SUBFS :
  409. result:=taicpu.op_reg_ref(A_SWC1,r,ref);
  410. R_SUBFD :
  411. result:=taicpu.op_reg_ref(A_SDC1,r,ref);
  412. else
  413. internalerror(2004010419);
  414. end;
  415. end
  416. else
  417. internalerror(2004010409);
  418. end;
  419. end;
  420. procedure InitAsm;
  421. begin
  422. end;
  423. procedure DoneAsm;
  424. begin
  425. end;
  426. procedure fixup_jmps(list: TAsmList);
  427. var
  428. p,pdelayslot: tai;
  429. newjmp,newnoop: taicpu;
  430. labelpositions: TFPList;
  431. instrpos: ptrint;
  432. l: tasmlabel;
  433. again: boolean;
  434. insai: tai;
  435. procedure create_pic_load(ai: taicpu; insloc: tai);
  436. var
  437. href: treference;
  438. newins: taicpu;
  439. begin
  440. { validity of operand has been checked by caller }
  441. href:=ai.oper[ai.ops-1]^.ref^;
  442. href.refaddr:=addr_pic;
  443. href.base:=NR_GP;
  444. newins:=taicpu.op_reg_ref(A_LW,NR_PIC_FUNC,href);
  445. newins.fileinfo:=ai.fileinfo;
  446. list.insertbefore(newins,insloc);
  447. inc(instrpos,2);
  448. if (href.symbol.bind=AB_LOCAL) then
  449. begin
  450. href.refaddr:=addr_low;
  451. href.base:=NR_NO;
  452. newins:=taicpu.op_reg_reg_ref(A_ADDIU,NR_PIC_FUNC,NR_PIC_FUNC,href);
  453. newins.fileinfo:=ai.fileinfo;
  454. list.insertbefore(newins,insloc);
  455. inc(instrpos,2);
  456. end;
  457. end;
  458. begin
  459. // MIPS relative branch range is +-32K instructions, i.e +-128 kBytes
  460. // if certainly not enough instructions to cause an overflow, dont bother
  461. if (list.count<high(smallint)) then
  462. exit;
  463. labelpositions := TFPList.create;
  464. p := tai(list.first);
  465. instrpos := 1;
  466. // record label positions
  467. while assigned(p) do
  468. begin
  469. if p.typ = ait_label then
  470. begin
  471. if (tai_label(p).labsym.labelnr >= labelpositions.count) then
  472. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  473. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  474. end;
  475. { ait_const is for jump tables }
  476. case p.typ of
  477. ait_instruction:
  478. { probleim here: pseudo-instructions can translate into
  479. several CPU instructions, possibly depending on assembler options,
  480. to obe on safe side, let's assume a mean of two. }
  481. inc(instrpos,2);
  482. ait_const:
  483. begin
  484. if (tai_const(p).consttype<>aitconst_32bit) then
  485. internalerror(2008052101);
  486. inc(instrpos);
  487. end;
  488. else
  489. ;
  490. end;
  491. p := tai(p.next);
  492. end;
  493. { If the number of instructions is below limit, we can't overflow either }
  494. if (instrpos<high(smallint)) then
  495. exit;
  496. // check and fix distances
  497. repeat
  498. again := false;
  499. p := tai(list.first);
  500. instrpos := 1;
  501. while assigned(p) do
  502. begin
  503. case p.typ of
  504. ait_label:
  505. // update labelposition in case it changed due to insertion
  506. // of jumps
  507. begin
  508. // can happen because of newly inserted labels
  509. if (tai_label(p).labsym.labelnr > labelpositions.count) then
  510. labelpositions.count := tai_label(p).labsym.labelnr * 2;
  511. labelpositions[tai_label(p).labsym.labelnr] := pointer(instrpos);
  512. end;
  513. ait_instruction:
  514. begin
  515. inc(instrpos,2);
  516. case taicpu(p).opcode of
  517. A_BA,A_BC:
  518. if (taicpu(p).ops>0) and (taicpu(p).oper[taicpu(p).ops-1]^.typ=top_ref) and
  519. assigned(taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol) and
  520. (taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol is tasmlabel) and
  521. (labelpositions[tasmlabel(taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol).labelnr] <> NIL) and
  522. {$push}
  523. {$q-}
  524. (ptruint(abs(ptrint(labelpositions[tasmlabel(taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol).labelnr]-instrpos)) - low(smallint)) > ptruint((high(smallint) - low(smallint)))) then
  525. {$pop}
  526. begin
  527. if (taicpu(p).opcode=A_BC) then
  528. begin
  529. { we're adding a new label together with the only branch to it;
  530. providing exact label position is not necessary }
  531. current_asmdata.getjumplabel(l);
  532. pdelayslot:=tai(p.next);
  533. { We need to insert the new instruction after the delay slot instruction ! }
  534. while assigned(pdelayslot) and (pdelayslot.typ<>ait_instruction) do
  535. pdelayslot:=tai(pdelayslot.next);
  536. insai:=tai_label.create(l);
  537. list.insertafter(insai,pdelayslot);
  538. // add a new unconditional jump between this jump and the label
  539. list.insertbefore(tai_comment.create(strpnew('fixup_jmps, A_BXX changed into A_BNOTXX label;A_J;label:')),p);
  540. if (cs_create_pic in current_settings.moduleswitches) then
  541. begin
  542. create_pic_load(taicpu(p),insai);
  543. newjmp:=taicpu.op_reg(A_JR,NR_PIC_FUNC);
  544. end
  545. else
  546. begin
  547. newjmp:=taicpu.op_sym(A_J,taicpu(p).oper[2]^.ref^.symbol);
  548. newjmp.is_jmp := true;
  549. end;
  550. newjmp.fileinfo:=taicpu(p).fileinfo;
  551. list.insertbefore(newjmp,insai);
  552. inc(instrpos,2);
  553. { Add a delay slot for new A_J instruction }
  554. newnoop:=taicpu.op_none(A_NOP);
  555. newnoop.fileinfo := taicpu(p).fileinfo;
  556. list.insertbefore(newnoop,insai);
  557. inc(instrpos,2);
  558. // change the conditional jump to point to the newly inserted label
  559. tasmlabel(taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol).decrefs;
  560. taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol := l;
  561. l.increfs;
  562. // and invert its condition code
  563. taicpu(p).condition := inverse_cond(taicpu(p).condition);
  564. { skip inserted stuff and continue processing from 'pdelayslot' }
  565. p:=pdelayslot;
  566. again:=true;
  567. end
  568. else // opcode=A_BA
  569. begin
  570. if (cs_create_pic in current_settings.moduleswitches) then
  571. begin
  572. list.insertbefore(tai_comment.create(strpnew('fixup_jmps, A_BA changed into PIC sequence')),p);
  573. create_pic_load(taicpu(p),p);
  574. taicpu(p).opcode:=A_JR;
  575. taicpu(p).loadreg(0,NR_PIC_FUNC);
  576. again:=true;
  577. { inserted stuff before 'p', continue processing from 'p' on }
  578. end
  579. else
  580. begin
  581. list.insertbefore(tai_comment.create(strpnew('fixup_jmps, A_BA changed into A_J')),p);
  582. taicpu(p).opcode:=A_J;
  583. end;
  584. end;
  585. end;
  586. else
  587. ;
  588. end;
  589. end;
  590. ait_const:
  591. inc(instrpos);
  592. else
  593. ;
  594. end;
  595. p := tai(p.next);
  596. end;
  597. until not again;
  598. labelpositions.free;
  599. end;
  600. procedure resolveReadAfterWrite(list: TAsmList);
  601. label skip;
  602. var
  603. p, pp : tai;
  604. l, x : TLinkedListItem;
  605. firstReg : tregister;
  606. begin
  607. l:= list.first;
  608. while assigned(l) do begin
  609. p:= tai(l);
  610. if p.typ = ait_instruction then begin
  611. if taicpu(p).opcode in [A_LB, A_LBU, A_LH, A_LHU, A_LW, A_LWU, A_LWL, A_LWR, A_MFC0 {MFC2, LWC2}] then begin
  612. firstReg:= taicpu(p).oper[0]^.reg;
  613. x:= l.next;
  614. if not assigned(x) then goto skip;
  615. pp:= tai(x);
  616. while pp.typ <> ait_instruction do begin
  617. x:= x.next;
  618. if not assigned(x) then goto skip;
  619. pp:= tai(x);
  620. end;
  621. if pp.typ = ait_instruction then begin
  622. if (taicpu(p).opcode = A_LWL) and (taicpu(pp).opcode = A_LWR) then goto skip;
  623. if (taicpu(p).opcode = A_LWR) and (taicpu(pp).opcode = A_LWL) then goto skip;
  624. if taicpu(pp).ops > 0 then begin
  625. case taicpu(pp).ops of
  626. 0 : {nothing to do};
  627. 1 :
  628. if (taicpu(pp).oper[0]^.typ = top_reg) and (firstReg = taicpu(pp).oper[0]^.reg) then
  629. list.insertAfter(taicpu.op_none(A_NOP), l);
  630. 2 :
  631. if ((taicpu(pp).oper[0]^.typ = top_reg) and (firstReg = taicpu(pp).oper[0]^.reg)) or
  632. ((taicpu(pp).oper[1]^.typ = top_reg) and (firstReg = taicpu(pp).oper[1]^.reg)) or
  633. ((taicpu(pp).oper[1]^.typ = top_ref) and (firstReg = taicpu(pp).oper[1]^.ref^.base)) then
  634. list.insertAfter(taicpu.op_none(A_NOP), l);
  635. 3 :
  636. if ((taicpu(pp).oper[0]^.typ = top_reg) and (firstReg = taicpu(pp).oper[0]^.reg)) or
  637. ((taicpu(pp).oper[1]^.typ = top_reg) and (firstReg = taicpu(pp).oper[1]^.reg)) or
  638. ((taicpu(pp).oper[2]^.typ = top_reg) and (firstReg = taicpu(pp).oper[2]^.reg)) then
  639. list.insertAfter(taicpu.op_none(A_NOP), l);
  640. else
  641. internalerror(2024092501);
  642. end;
  643. end;
  644. end;
  645. end;
  646. end;
  647. skip:
  648. l:= l.next;
  649. end;
  650. end;
  651. begin
  652. cai_cpu := taicpu;
  653. cai_align := tai_align;
  654. end.