narmmat.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. if cs_check_overflow in current_settings.localswitches then
  106. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  107. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MVN,
  108. resultreg,numerator),toppostfix(ord(cs_check_overflow in current_settings.localswitches)*ord(PF_S))));
  109. end
  110. else if ispowerof2(tordconstnode(right).value,power) then
  111. begin
  112. if (is_signed(right.resultdef)) then
  113. begin
  114. helper1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  115. helper2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  116. shifterop_reset(so);
  117. so.shiftmode:=SM_ASR;
  118. so.shiftimm:=31;
  119. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_shifterop(A_MOV,helper1,numerator,so));
  120. shifterop_reset(so);
  121. so.shiftmode:=SM_LSR;
  122. so.shiftimm:=32-power;
  123. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,helper2,numerator,helper1,so));
  124. shifterop_reset(so);
  125. so.shiftmode:=SM_ASR;
  126. so.shiftimm:=power;
  127. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_shifterop(A_MOV,resultreg,helper2,so));
  128. end
  129. else
  130. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,power,numerator,resultreg)
  131. end;
  132. end;
  133. {
  134. procedure genOrdConstNodeMod;
  135. var
  136. modreg, maskreg, tempreg : tregister;
  137. begin
  138. if (tordconstnode(right).value = 0) then begin
  139. internalerror(2005061702);
  140. end
  141. else if (abs(tordconstnode(right).value.svalue) = 1) then
  142. begin
  143. // x mod +/-1 is always zero
  144. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 0, resultreg);
  145. end
  146. else if (ispowerof2(tordconstnode(right).value, power)) then
  147. begin
  148. if (is_signed(right.resultdef)) then begin
  149. tempreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  150. maskreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  151. modreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  152. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, abs(tordconstnode(right).value.svalue)-1, modreg);
  153. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, 31, numerator, maskreg);
  154. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, numerator, modreg, tempreg);
  155. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC, maskreg, maskreg, modreg));
  156. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC, modreg, tempreg, 0));
  157. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE, modreg, modreg, modreg));
  158. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, modreg, maskreg, maskreg);
  159. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_INT, maskreg, tempreg, resultreg);
  160. end else begin
  161. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, tordconstnode(right).value.svalue-1, numerator, resultreg);
  162. end;
  163. end else begin
  164. genOrdConstNodeDiv();
  165. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_MUL, OS_INT, tordconstnode(right).value.svalue, resultreg, resultreg);
  166. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, resultreg, numerator, resultreg);
  167. end;
  168. end;
  169. }
  170. begin
  171. secondpass(left);
  172. secondpass(right);
  173. if (current_settings.cputype in [cpu_armv7m]) and
  174. (nodetype=divn) and
  175. not(is_64bitint(resultdef)) then
  176. begin
  177. size:=def_cgsize(left.resultdef);
  178. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  179. location_copy(location,left.location);
  180. location.loc := LOC_REGISTER;
  181. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  182. resultreg:=location.register;
  183. if (right.nodetype=ordconstn) and
  184. ((tordconstnode(right).value=1) or
  185. (tordconstnode(right).value=int64(-1)) or
  186. (tordconstnode(right).value=0) or
  187. ispowerof2(tordconstnode(right).value,power)) then
  188. begin
  189. numerator:=left.location.register;
  190. genOrdConstNodeDiv;
  191. end
  192. else
  193. begin
  194. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,left.resultdef,true);
  195. if is_signed(left.resultdef) or
  196. is_signed(right.resultdef) then
  197. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_IDIV,OS_INT,right.location.register,left.location.register,location.register)
  198. else
  199. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_DIV,OS_INT,right.location.register,left.location.register,location.register);
  200. end;
  201. end
  202. else
  203. begin
  204. location_copy(location,left.location);
  205. { put numerator in register }
  206. size:=def_cgsize(left.resultdef);
  207. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  208. left.resultdef,left.resultdef,true);
  209. location_copy(location,left.location);
  210. numerator:=location.register;
  211. resultreg:=location.register;
  212. if location.loc=LOC_CREGISTER then
  213. begin
  214. location.loc := LOC_REGISTER;
  215. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  216. resultreg:=location.register;
  217. end
  218. else if (nodetype=modn) or (right.nodetype=ordconstn) then
  219. begin
  220. // for a modulus op, and for const nodes we need the result register
  221. // to be an extra register
  222. resultreg:=cg.getintregister(current_asmdata.CurrAsmList,size);
  223. end;
  224. if right.nodetype=ordconstn then
  225. begin
  226. if nodetype=divn then
  227. genOrdConstNodeDiv
  228. else
  229. // genOrdConstNodeMod;
  230. end;
  231. location.register:=resultreg;
  232. end;
  233. { unsigned division/module can only overflow in case of division by zero }
  234. { (but checking this overflow flag is more convoluted than performing a }
  235. { simple comparison with 0) }
  236. if is_signed(right.resultdef) then
  237. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  238. end;
  239. {*****************************************************************************
  240. TARMNOTNODE
  241. *****************************************************************************}
  242. procedure tarmnotnode.second_boolean;
  243. var
  244. hl : tasmlabel;
  245. begin
  246. { if the location is LOC_JUMP, we do the secondpass after the
  247. labels are allocated
  248. }
  249. if left.expectloc=LOC_JUMP then
  250. begin
  251. hl:=current_procinfo.CurrTrueLabel;
  252. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  253. current_procinfo.CurrFalseLabel:=hl;
  254. secondpass(left);
  255. if left.location.loc<>LOC_JUMP then
  256. internalerror(2012081305);
  257. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  258. hl:=current_procinfo.CurrTrueLabel;
  259. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  260. current_procinfo.CurrFalseLabel:=hl;
  261. location.loc:=LOC_JUMP;
  262. end
  263. else
  264. begin
  265. secondpass(left);
  266. case left.location.loc of
  267. LOC_FLAGS :
  268. begin
  269. location_copy(location,left.location);
  270. inverse_flags(location.resflags);
  271. end;
  272. LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  273. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  274. begin
  275. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  276. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  277. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,0));
  278. location_reset(location,LOC_FLAGS,OS_NO);
  279. location.resflags:=F_EQ;
  280. end;
  281. else
  282. internalerror(2003042401);
  283. end;
  284. end;
  285. end;
  286. {*****************************************************************************
  287. TARMUNARYMINUSNODE
  288. *****************************************************************************}
  289. procedure tarmunaryminusnode.second_float;
  290. var
  291. op: tasmop;
  292. begin
  293. secondpass(left);
  294. case current_settings.fputype of
  295. fpu_fpa,
  296. fpu_fpa10,
  297. fpu_fpa11:
  298. begin
  299. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  300. location:=left.location;
  301. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSF,
  302. location.register,left.location.register,0),
  303. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  304. end;
  305. fpu_vfpv2,
  306. fpu_vfpv3,
  307. fpu_vfpv3_d16:
  308. begin
  309. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  310. location:=left.location;
  311. if (left.location.loc=LOC_CMMREGISTER) then
  312. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  313. if (location.size=OS_F32) then
  314. op:=A_FNEGS
  315. else
  316. op:=A_FNEGD;
  317. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  318. location.register,left.location.register));
  319. end;
  320. else
  321. internalerror(2009112602);
  322. end;
  323. end;
  324. function tarmshlshrnode.first_shlshr64bitint: tnode;
  325. begin
  326. if (current_settings.cputype in cpu_thumb2) then
  327. result:=inherited
  328. else
  329. result := nil;
  330. end;
  331. procedure tarmshlshrnode.second_64bit;
  332. var
  333. hreg64hi,hreg64lo,shiftreg:Tregister;
  334. v : TConstExprInt;
  335. l1,l2,l3:Tasmlabel;
  336. so: tshifterop;
  337. procedure emit_instr(p: tai);
  338. begin
  339. current_asmdata.CurrAsmList.concat(p);
  340. end;
  341. {Reg1 gets shifted and moved into reg2, and is set to zero afterwards}
  342. procedure shift_more_than_32(reg1, reg2: TRegister; shiftval: Byte ; sm: TShiftMode);
  343. begin
  344. shifterop_reset(so); so.shiftimm:=shiftval - 32; so.shiftmode:=sm;
  345. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg1, so));
  346. emit_instr(taicpu.op_reg_const(A_MOV, reg1, 0));
  347. end;
  348. procedure shift_less_than_32(reg1, reg2: TRegister; shiftval: Byte; shiftright: boolean);
  349. begin
  350. shifterop_reset(so); so.shiftimm:=shiftval;
  351. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  352. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg1, reg1, so));
  353. if shiftright then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  354. so.shiftimm:=32-shiftval;
  355. emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, reg1, reg1, reg2, so));
  356. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  357. so.shiftimm:=shiftval;
  358. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg2, so));
  359. end;
  360. procedure shift_by_variable(reg1, reg2, shiftval: TRegister; shiftright: boolean);
  361. var
  362. shiftval2:TRegister;
  363. begin
  364. shifterop_reset(so);
  365. shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  366. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  367. {Do we shift more than 32 bits?}
  368. emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval, 32), PF_S));
  369. {This part cares for 32 bits and more}
  370. emit_instr(setcondition(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval, 32), C_MI));
  371. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  372. so.rs:=shiftval2;
  373. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg1, so), C_MI));
  374. {Less than 32 bits}
  375. so.rs:=shiftval;
  376. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg2, so), C_PL));
  377. if shiftright then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  378. so.rs:=shiftval2;
  379. emit_instr(setcondition(taicpu.op_reg_reg_reg_shifterop(A_ORR, reg2, reg2, reg1, so), C_PL));
  380. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  381. {Final adjustments}
  382. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  383. so.rs:=shiftval;
  384. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg1, reg1, so));
  385. end;
  386. begin
  387. if (current_settings.cputype in cpu_thumb2) then
  388. begin
  389. inherited;
  390. exit;
  391. end;
  392. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  393. { load left operator in a register }
  394. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  395. hreg64hi:=left.location.register64.reghi;
  396. hreg64lo:=left.location.register64.reglo;
  397. location.register64.reghi:=hreg64hi;
  398. location.register64.reglo:=hreg64lo;
  399. { shifting by a constant directly coded: }
  400. if (right.nodetype=ordconstn) then
  401. begin
  402. v:=Tordconstnode(right).value and 63;
  403. {Single bit shift}
  404. if v = 1 then
  405. if nodetype=shln then
  406. begin
  407. {Shift left by one by 2 simple 32bit additions}
  408. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  409. emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, hreg64lo, hreg64lo, hreg64lo), PF_S));
  410. emit_instr(taicpu.op_reg_reg_reg(A_ADC, hreg64hi, hreg64hi, hreg64hi));
  411. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  412. end
  413. else
  414. begin
  415. {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  416. shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  417. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  418. emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, hreg64hi, hreg64hi, so), PF_S));
  419. so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  420. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, hreg64lo, hreg64lo, so));
  421. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  422. end
  423. {A 32bit shift just replaces a register and clears the other}
  424. else if v = 32 then
  425. begin
  426. if nodetype=shln then
  427. emit_instr(taicpu.op_reg_const(A_MOV, hreg64hi, 0))
  428. else
  429. emit_instr(taicpu.op_reg_const(A_MOV, hreg64lo, 0));
  430. location.register64.reghi:=hreg64lo;
  431. location.register64.reglo:=hreg64hi;
  432. end
  433. {Shift LESS than 32}
  434. else if (v < 32) and (v > 1) then
  435. if nodetype=shln then
  436. shift_less_than_32(hreg64hi, hreg64lo, v.uvalue, false)
  437. else
  438. shift_less_than_32(hreg64lo, hreg64hi, v.uvalue, true)
  439. {More than 32}
  440. else if v > 32 then
  441. if nodetype=shln then
  442. shift_more_than_32(hreg64lo, hreg64hi, v.uvalue, SM_LSL)
  443. else
  444. shift_more_than_32(hreg64hi, hreg64lo, v.uvalue, SM_LSR);
  445. end
  446. else
  447. begin
  448. { force right operators in a register }
  449. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,false);
  450. if nodetype = shln then
  451. shift_by_variable(hreg64lo,hreg64hi,right.location.register, false)
  452. else
  453. shift_by_variable(hreg64hi,hreg64lo,right.location.register, true);
  454. end;
  455. end;
  456. begin
  457. cmoddivnode:=tarmmoddivnode;
  458. cnotnode:=tarmnotnode;
  459. cunaryminusnode:=tarmunaryminusnode;
  460. cshlshrnode:=tarmshlshrnode;
  461. end.