narmmat.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  254. hl:=current_procinfo.CurrTrueLabel;
  255. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  256. current_procinfo.CurrFalseLabel:=hl;
  257. location.loc:=LOC_JUMP;
  258. end
  259. else
  260. begin
  261. secondpass(left);
  262. case left.location.loc of
  263. LOC_FLAGS :
  264. begin
  265. location_copy(location,left.location);
  266. inverse_flags(location.resflags);
  267. end;
  268. LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  269. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  270. begin
  271. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  272. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,0));
  273. location_reset(location,LOC_FLAGS,OS_NO);
  274. location.resflags:=F_EQ;
  275. end;
  276. else
  277. internalerror(2003042401);
  278. end;
  279. end;
  280. end;
  281. {*****************************************************************************
  282. TARMUNARYMINUSNODE
  283. *****************************************************************************}
  284. procedure tarmunaryminusnode.second_float;
  285. var
  286. op: tasmop;
  287. begin
  288. secondpass(left);
  289. case current_settings.fputype of
  290. fpu_fpa,
  291. fpu_fpa10,
  292. fpu_fpa11:
  293. begin
  294. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  295. location:=left.location;
  296. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSF,
  297. location.register,left.location.register,0),
  298. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  299. end;
  300. fpu_vfpv2,
  301. fpu_vfpv3,
  302. fpu_vfpv3_d16:
  303. begin
  304. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  305. location:=left.location;
  306. if (left.location.loc=LOC_CMMREGISTER) then
  307. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  308. if (location.size=OS_F32) then
  309. op:=A_FNEGS
  310. else
  311. op:=A_FNEGD;
  312. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  313. location.register,left.location.register));
  314. end;
  315. else
  316. internalerror(2009112602);
  317. end;
  318. end;
  319. function tarmshlshrnode.first_shlshr64bitint: tnode;
  320. begin
  321. result := nil;
  322. end;
  323. procedure tarmshlshrnode.second_64bit;
  324. var
  325. hreg64hi,hreg64lo,shiftreg:Tregister;
  326. v : TConstExprInt;
  327. l1,l2,l3:Tasmlabel;
  328. so: tshifterop;
  329. procedure emit_instr(p: tai);
  330. begin
  331. current_asmdata.CurrAsmList.concat(p);
  332. end;
  333. {Reg1 gets shifted and moved into reg2, and is set to zero afterwards}
  334. procedure shift_more_than_32(reg1, reg2: TRegister; shiftval: Byte ; sm: TShiftMode);
  335. begin
  336. shifterop_reset(so); so.shiftimm:=shiftval - 32; so.shiftmode:=sm;
  337. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg1, so));
  338. emit_instr(taicpu.op_reg_const(A_MOV, reg1, 0));
  339. end;
  340. procedure shift_less_than_32(reg1, reg2: TRegister; shiftval: Byte; shiftright: boolean);
  341. begin
  342. shifterop_reset(so); so.shiftimm:=shiftval;
  343. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  344. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg1, reg1, so));
  345. if shiftright then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  346. so.shiftimm:=32-shiftval;
  347. emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, reg1, reg1, reg2, so));
  348. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  349. so.shiftimm:=shiftval;
  350. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg2, so));
  351. end;
  352. procedure shift_by_variable(reg1, reg2, shiftval: TRegister; shiftright: boolean);
  353. var
  354. shiftval2:TRegister;
  355. begin
  356. shifterop_reset(so);
  357. shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  358. {Do we shift more than 32 bits?}
  359. emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval, 32), PF_S));
  360. {This part cares for 32 bits and more}
  361. emit_instr(setcondition(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval, 32), C_MI));
  362. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  363. so.rs:=shiftval2;
  364. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg1, so), C_MI));
  365. {Less than 32 bits}
  366. so.rs:=shiftval;
  367. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, reg2, reg2, so), C_PL));
  368. if shiftright then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  369. so.rs:=shiftval2;
  370. emit_instr(setcondition(taicpu.op_reg_reg_reg_shifterop(A_ORR, reg2, reg2, reg1, so), C_PL));
  371. {Final adjustments}
  372. if shiftright then so.shiftmode:=SM_LSR else so.shiftmode:=SM_LSL;
  373. so.rs:=shiftval;
  374. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, reg1, reg1, so));
  375. end;
  376. begin
  377. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  378. { load left operator in a register }
  379. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  380. hreg64hi:=left.location.register64.reghi;
  381. hreg64lo:=left.location.register64.reglo;
  382. location.register64.reghi:=hreg64hi;
  383. location.register64.reglo:=hreg64lo;
  384. { shifting by a constant directly coded: }
  385. if (right.nodetype=ordconstn) then
  386. begin
  387. v:=Tordconstnode(right).value and 63;
  388. {Single bit shift}
  389. if v = 1 then
  390. if nodetype=shln then
  391. begin
  392. {Shift left by one by 2 simple 32bit additions}
  393. emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, hreg64lo, hreg64lo, hreg64lo), PF_S));
  394. emit_instr(taicpu.op_reg_reg_reg(A_ADC, hreg64hi, hreg64hi, hreg64hi));
  395. end
  396. else
  397. begin
  398. {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  399. shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  400. emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, hreg64hi, hreg64hi, so), PF_S));
  401. so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  402. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, hreg64lo, hreg64lo, so));
  403. end
  404. {A 32bit shift just replaces a register and clears the other}
  405. else if v = 32 then
  406. begin
  407. if nodetype=shln then
  408. emit_instr(taicpu.op_reg_const(A_MOV, hreg64hi, 0))
  409. else
  410. emit_instr(taicpu.op_reg_const(A_MOV, hreg64lo, 0));
  411. location.register64.reghi:=hreg64lo;
  412. location.register64.reglo:=hreg64hi;
  413. end
  414. {Shift LESS than 32}
  415. else if (v < 32) and (v > 1) then
  416. if nodetype=shln then
  417. shift_less_than_32(hreg64hi, hreg64lo, v.uvalue, false)
  418. else
  419. shift_less_than_32(hreg64lo, hreg64hi, v.uvalue, true)
  420. {More than 32}
  421. else if v > 32 then
  422. if nodetype=shln then
  423. shift_more_than_32(hreg64lo, hreg64hi, v.uvalue, SM_LSL)
  424. else
  425. shift_more_than_32(hreg64hi, hreg64lo, v.uvalue, SM_LSR);
  426. end
  427. else
  428. begin
  429. { force right operators in a register }
  430. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,resultdef,false);
  431. if nodetype = shln then
  432. shift_by_variable(hreg64lo,hreg64hi,right.location.register, false)
  433. else
  434. shift_by_variable(hreg64hi,hreg64lo,right.location.register, true);
  435. end;
  436. end;
  437. begin
  438. cmoddivnode:=tarmmoddivnode;
  439. cnotnode:=tarmnotnode;
  440. cunaryminusnode:=tarmunaryminusnode;
  441. cshlshrnode:=tarmshlshrnode;
  442. end.