narmmat.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate ARM assembler for math nodes
  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 narmmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat;
  22. type
  23. tarmmoddivnode = class(tmoddivnode)
  24. function first_moddivint: tnode;override;
  25. procedure pass_generate_code;override;
  26. end;
  27. tarmnotnode = class(tcgnotnode)
  28. procedure second_boolean;override;
  29. end;
  30. tarmunaryminusnode = class(tcgunaryminusnode)
  31. procedure second_float;override;
  32. end;
  33. tarmshlshrnode = class(tcgshlshrnode)
  34. procedure second_64bit;override;
  35. function first_shlshr64bitint: tnode; override;
  36. end;
  37. implementation
  38. uses
  39. globtype,systems,
  40. cutils,verbose,globals,constexp,
  41. aasmbase,aasmcpu,aasmtai,aasmdata,
  42. defutil,
  43. cgbase,cgobj,hlcgobj,cgutils,
  44. pass_2,procinfo,
  45. ncon,
  46. cpubase,cpuinfo,
  47. ncgutil,cgcpu,
  48. nadd,pass_1,symdef;
  49. {*****************************************************************************
  50. TARMMODDIVNODE
  51. *****************************************************************************}
  52. function tarmmoddivnode.first_moddivint: tnode;
  53. var
  54. power : longint;
  55. begin
  56. if (right.nodetype=ordconstn) and
  57. (nodetype=divn) and
  58. (ispowerof2(tordconstnode(right).value,power) or
  59. (tordconstnode(right).value=1) or
  60. (tordconstnode(right).value=int64(-1))
  61. ) and
  62. not(is_64bitint(resultdef)) then
  63. result:=nil
  64. else if (current_settings.cputype in [cpu_armv7m]) and
  65. (nodetype=divn) and
  66. not(is_64bitint(resultdef)) then
  67. result:=nil
  68. else if (current_settings.cputype in [cpu_armv7m]) and
  69. (nodetype=modn) and
  70. not(is_64bitint(resultdef)) then
  71. begin
  72. if (right.nodetype=ordconstn) and
  73. ispowerof2(tordconstnode(right).value,power) and
  74. (tordconstnode(right).value<=256) and
  75. (tordconstnode(right).value>0) then
  76. result:=caddnode.create(andn,left,cordconstnode.create(tordconstnode(right).value-1,sinttype,false))
  77. else
  78. begin
  79. result:=caddnode.create(subn,left,caddnode.create(muln,right.getcopy, cmoddivnode.Create(divn,left.getcopy,right.getcopy)));
  80. right:=nil;
  81. end;
  82. left:=nil;
  83. end
  84. else
  85. result:=inherited first_moddivint;
  86. end;
  87. procedure tarmmoddivnode.pass_generate_code;
  88. var
  89. power : longint;
  90. numerator,
  91. helper1,
  92. helper2,
  93. resultreg : tregister;
  94. size : Tcgsize;
  95. so : tshifterop;
  96. procedure genOrdConstNodeDiv;
  97. begin
  98. if tordconstnode(right).value=0 then
  99. internalerror(2005061701)
  100. else if tordconstnode(right).value=1 then
  101. cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, numerator, resultreg)
  102. else if (tordconstnode(right).value = int64(-1)) then
  103. begin
  104. // note: only in the signed case possible..., may overflow
  105. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MVN,
  106. resultreg,numerator),toppostfix(ord(cs_check_overflow in current_settings.localswitches)*ord(PF_S))));
  107. end
  108. else if ispowerof2(tordconstnode(right).value,power) then
  109. begin
  110. if (is_signed(right.resultdef)) then
  111. begin
  112. helper1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  113. helper2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  114. shifterop_reset(so);
  115. so.shiftmode:=SM_ASR;
  116. so.shiftimm:=31;
  117. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_shifterop(A_MOV,helper1,numerator,so));
  118. shifterop_reset(so);
  119. so.shiftmode:=SM_LSR;
  120. so.shiftimm:=32-power;
  121. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,helper2,numerator,helper1,so));
  122. shifterop_reset(so);
  123. so.shiftmode:=SM_ASR;
  124. so.shiftimm:=power;
  125. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_shifterop(A_MOV,resultreg,helper2,so));
  126. end
  127. else
  128. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,power,numerator,resultreg)
  129. end;
  130. end;
  131. {
  132. procedure genOrdConstNodeMod;
  133. var
  134. modreg, maskreg, tempreg : tregister;
  135. begin
  136. if (tordconstnode(right).value = 0) then begin
  137. internalerror(2005061702);
  138. end
  139. else if (abs(tordconstnode(right).value.svalue) = 1) then
  140. begin
  141. // x mod +/-1 is always zero
  142. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 0, resultreg);
  143. end
  144. else if (ispowerof2(tordconstnode(right).value, power)) then
  145. begin
  146. if (is_signed(right.resultdef)) then begin
  147. tempreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  148. maskreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  149. modreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  150. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, abs(tordconstnode(right).value.svalue)-1, modreg);
  151. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, 31, numerator, maskreg);
  152. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, numerator, modreg, tempreg);
  153. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC, maskreg, maskreg, modreg));
  154. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC, modreg, tempreg, 0));
  155. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE, modreg, modreg, modreg));
  156. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, modreg, maskreg, maskreg);
  157. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_INT, maskreg, tempreg, resultreg);
  158. end else begin
  159. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, tordconstnode(right).value.svalue-1, numerator, resultreg);
  160. end;
  161. end else begin
  162. genOrdConstNodeDiv();
  163. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_MUL, OS_INT, tordconstnode(right).value.svalue, resultreg, resultreg);
  164. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, resultreg, numerator, resultreg);
  165. end;
  166. end;
  167. }
  168. begin
  169. secondpass(left);
  170. secondpass(right);
  171. if (current_settings.cputype in [cpu_armv7m]) and
  172. (nodetype=divn) and
  173. not(is_64bitint(resultdef)) then
  174. begin
  175. size:=def_cgsize(left.resultdef);
  176. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  177. location_copy(location,left.location);
  178. location.loc := LOC_REGISTER;
  179. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  180. resultreg:=location.register;
  181. if (right.nodetype=ordconstn) and
  182. ((tordconstnode(right).value=1) or
  183. (tordconstnode(right).value=int64(-1)) or
  184. (tordconstnode(right).value=0) or
  185. ispowerof2(tordconstnode(right).value,power)) then
  186. begin
  187. numerator:=left.location.register;
  188. genOrdConstNodeDiv;
  189. end
  190. else
  191. begin
  192. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,left.resultdef,true);
  193. if is_signed(left.resultdef) or
  194. is_signed(right.resultdef) then
  195. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_IDIV,OS_INT,right.location.register,left.location.register,location.register)
  196. else
  197. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_DIV,OS_INT,right.location.register,left.location.register,location.register);
  198. end;
  199. end
  200. else
  201. begin
  202. location_copy(location,left.location);
  203. { put numerator in register }
  204. size:=def_cgsize(left.resultdef);
  205. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  206. left.resultdef,left.resultdef,true);
  207. location_copy(location,left.location);
  208. numerator:=location.register;
  209. resultreg:=location.register;
  210. if location.loc=LOC_CREGISTER then
  211. begin
  212. location.loc := LOC_REGISTER;
  213. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  214. resultreg:=location.register;
  215. end
  216. else if (nodetype=modn) or (right.nodetype=ordconstn) then
  217. begin
  218. // for a modulus op, and for const nodes we need the result register
  219. // to be an extra register
  220. resultreg:=cg.getintregister(current_asmdata.CurrAsmList,size);
  221. end;
  222. if right.nodetype=ordconstn then
  223. begin
  224. if nodetype=divn then
  225. genOrdConstNodeDiv
  226. else
  227. // genOrdConstNodeMod;
  228. end;
  229. location.register:=resultreg;
  230. end;
  231. { unsigned division/module can only overflow in case of division by zero }
  232. { (but checking this overflow flag is more convoluted than performing a }
  233. { simple comparison with 0) }
  234. if is_signed(right.resultdef) then
  235. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  236. end;
  237. {*****************************************************************************
  238. TARMNOTNODE
  239. *****************************************************************************}
  240. procedure tarmnotnode.second_boolean;
  241. var
  242. hl : tasmlabel;
  243. begin
  244. { if the location is LOC_JUMP, we do the secondpass after the
  245. labels are allocated
  246. }
  247. if left.expectloc=LOC_JUMP then
  248. begin
  249. hl:=current_procinfo.CurrTrueLabel;
  250. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  251. current_procinfo.CurrFalseLabel:=hl;
  252. secondpass(left);
  253. if left.location.loc<>LOC_JUMP then
  254. internalerror(2012081305);
  255. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  256. hl:=current_procinfo.CurrTrueLabel;
  257. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  258. current_procinfo.CurrFalseLabel:=hl;
  259. location.loc:=LOC_JUMP;
  260. end
  261. else
  262. begin
  263. secondpass(left);
  264. case left.location.loc of
  265. LOC_FLAGS :
  266. begin
  267. location_copy(location,left.location);
  268. inverse_flags(location.resflags);
  269. end;
  270. LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  271. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  272. begin
  273. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  274. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,0));
  275. location_reset(location,LOC_FLAGS,OS_NO);
  276. location.resflags:=F_EQ;
  277. end;
  278. else
  279. internalerror(2003042401);
  280. end;
  281. end;
  282. end;
  283. {*****************************************************************************
  284. TARMUNARYMINUSNODE
  285. *****************************************************************************}
  286. procedure tarmunaryminusnode.second_float;
  287. var
  288. op: tasmop;
  289. begin
  290. secondpass(left);
  291. case current_settings.fputype of
  292. fpu_fpa,
  293. fpu_fpa10,
  294. fpu_fpa11:
  295. begin
  296. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  297. location:=left.location;
  298. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSF,
  299. location.register,left.location.register,0),
  300. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  301. end;
  302. fpu_vfpv2,
  303. fpu_vfpv3,
  304. fpu_vfpv3_d16:
  305. begin
  306. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  307. location:=left.location;
  308. if (left.location.loc=LOC_CMMREGISTER) then
  309. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  310. if (location.size=OS_F32) then
  311. op:=A_FNEGS
  312. else
  313. op:=A_FNEGD;
  314. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  315. location.register,left.location.register));
  316. end;
  317. else
  318. internalerror(2009112602);
  319. end;
  320. end;
  321. function tarmshlshrnode.first_shlshr64bitint: tnode;
  322. begin
  323. if (current_settings.cputype in cpu_thumb2) then
  324. result:=inherited
  325. else
  326. result := nil;
  327. end;
  328. procedure tarmshlshrnode.second_64bit;
  329. var
  330. hreg64hi,hreg64lo,shiftreg:Tregister;
  331. v : TConstExprInt;
  332. l1,l2,l3:Tasmlabel;
  333. so: tshifterop;
  334. procedure emit_instr(p: tai);
  335. begin
  336. current_asmdata.CurrAsmList.concat(p);
  337. end;
  338. {Reg1 gets shifted and moved into reg2, and is set to zero afterwards}
  339. procedure shift_more_than_32(reg1, reg2: TRegister; shiftval: Byte ; sm: TShiftMode);
  340. begin
  341. shifterop_reset(so); so.shiftimm:=shiftval - 32; so.shiftmode:=sm;
  342. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg1, so));
  343. emit_instr(taicpu.op_reg_const(A_MOV, reg1, 0));
  344. end;
  345. procedure shift_less_than_32(reg1, reg2: TRegister; shiftval: Byte; shiftright: boolean);
  346. begin
  347. shifterop_reset(so); so.shiftimm:=shiftval;
  348. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  349. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg1, reg1, so));
  350. if shiftright then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  351. so.shiftimm:=32-shiftval;
  352. emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, reg1, reg1, reg2, so));
  353. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  354. so.shiftimm:=shiftval;
  355. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg2, so));
  356. end;
  357. procedure shift_by_variable(reg1, reg2, shiftval: TRegister; shiftright: boolean);
  358. var
  359. shiftval2:TRegister;
  360. begin
  361. shifterop_reset(so);
  362. shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  363. {Do we shift more than 32 bits?}
  364. emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval, 32), PF_S));
  365. {This part cares for 32 bits and more}
  366. emit_instr(setcondition(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval, 32), C_MI));
  367. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  368. so.rs:=shiftval2;
  369. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg1, so), C_MI));
  370. {Less than 32 bits}
  371. so.rs:=shiftval;
  372. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg2, so), C_PL));
  373. if shiftright then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  374. so.rs:=shiftval2;
  375. emit_instr(setcondition(taicpu.op_reg_reg_reg_shifterop(A_ORR, reg2, reg2, reg1, so), C_PL));
  376. {Final adjustments}
  377. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  378. so.rs:=shiftval;
  379. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg1, reg1, so));
  380. end;
  381. begin
  382. if (current_settings.cputype in cpu_thumb2) then
  383. begin
  384. inherited;
  385. exit;
  386. end;
  387. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  388. { load left operator in a register }
  389. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  390. hreg64hi:=left.location.register64.reghi;
  391. hreg64lo:=left.location.register64.reglo;
  392. location.register64.reghi:=hreg64hi;
  393. location.register64.reglo:=hreg64lo;
  394. { shifting by a constant directly coded: }
  395. if (right.nodetype=ordconstn) then
  396. begin
  397. v:=Tordconstnode(right).value and 63;
  398. {Single bit shift}
  399. if v = 1 then
  400. if nodetype=shln then
  401. begin
  402. {Shift left by one by 2 simple 32bit additions}
  403. emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, hreg64lo, hreg64lo, hreg64lo), PF_S));
  404. emit_instr(taicpu.op_reg_reg_reg(A_ADC, hreg64hi, hreg64hi, hreg64hi));
  405. end
  406. else
  407. begin
  408. {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  409. shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  410. emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, hreg64hi, hreg64hi, so), PF_S));
  411. so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  412. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, hreg64lo, hreg64lo, so));
  413. end
  414. {A 32bit shift just replaces a register and clears the other}
  415. else if v = 32 then
  416. begin
  417. if nodetype=shln then
  418. emit_instr(taicpu.op_reg_const(A_MOV, hreg64hi, 0))
  419. else
  420. emit_instr(taicpu.op_reg_const(A_MOV, hreg64lo, 0));
  421. location.register64.reghi:=hreg64lo;
  422. location.register64.reglo:=hreg64hi;
  423. end
  424. {Shift LESS than 32}
  425. else if (v < 32) and (v > 1) then
  426. if nodetype=shln then
  427. shift_less_than_32(hreg64hi, hreg64lo, v.uvalue, false)
  428. else
  429. shift_less_than_32(hreg64lo, hreg64hi, v.uvalue, true)
  430. {More than 32}
  431. else if v > 32 then
  432. if nodetype=shln then
  433. shift_more_than_32(hreg64lo, hreg64hi, v.uvalue, SM_LSL)
  434. else
  435. shift_more_than_32(hreg64hi, hreg64lo, v.uvalue, SM_LSR);
  436. end
  437. else
  438. begin
  439. { force right operators in a register }
  440. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,false);
  441. if nodetype = shln then
  442. shift_by_variable(hreg64lo,hreg64hi,right.location.register, false)
  443. else
  444. shift_by_variable(hreg64hi,hreg64lo,right.location.register, true);
  445. end;
  446. end;
  447. begin
  448. cmoddivnode:=tarmmoddivnode;
  449. cnotnode:=tarmnotnode;
  450. cunaryminusnode:=tarmunaryminusnode;
  451. cshlshrnode:=tarmshlshrnode;
  452. end.