narmmat.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. function pass_1: tnode; override;
  32. procedure second_float;override;
  33. end;
  34. tarmshlshrnode = class(tcgshlshrnode)
  35. procedure second_64bit;override;
  36. function first_shlshr64bitint: tnode; override;
  37. end;
  38. implementation
  39. uses
  40. globtype,
  41. cutils,verbose,globals,constexp,
  42. aasmbase,aasmcpu,aasmtai,aasmdata,
  43. defutil,
  44. symtype,symconst,symtable,
  45. cgbase,cgobj,hlcgobj,cgutils,
  46. pass_2,procinfo,
  47. ncon,ncnv,ncal,ninl,
  48. cpubase,cpuinfo,
  49. ncgutil,
  50. nadd,pass_1,symdef;
  51. {*****************************************************************************
  52. TARMMODDIVNODE
  53. *****************************************************************************}
  54. function tarmmoddivnode.first_moddivint: tnode;
  55. var
  56. power : longint;
  57. begin
  58. {We can handle all cases of constant division}
  59. if not(cs_check_overflow in current_settings.localswitches) and
  60. (right.nodetype=ordconstn) and
  61. (nodetype=divn) and
  62. not(is_64bitint(resultdef)) and
  63. {Only the ARM and thumb2-isa support umull and smull, which are required for arbitary division by const optimization}
  64. (GenerateArmCode or
  65. GenerateThumb2Code or
  66. (ispowerof2(tordconstnode(right).value,power) or
  67. (tordconstnode(right).value=1) or
  68. (tordconstnode(right).value=int64(-1))
  69. )
  70. ) then
  71. result:=nil
  72. else if ((GenerateThumbCode or GenerateThumb2Code) and (CPUARM_HAS_THUMB_IDIV in cpu_capabilities[current_settings.cputype])) and
  73. (nodetype=divn) and
  74. not(is_64bitint(resultdef)) then
  75. result:=nil
  76. else if ((GenerateThumbCode or GenerateThumb2Code) and (CPUARM_HAS_THUMB_IDIV in cpu_capabilities[current_settings.cputype])) and
  77. (nodetype=modn) and
  78. not(is_64bitint(resultdef)) then
  79. begin
  80. if (right.nodetype=ordconstn) and
  81. ispowerof2(tordconstnode(right).value,power) and
  82. (tordconstnode(right).value<=256) and
  83. (tordconstnode(right).value>0) then
  84. result:=caddnode.create_internal(andn,left,cordconstnode.create(tordconstnode(right).value-1,sinttype,false))
  85. else
  86. begin
  87. result:=caddnode.create_internal(subn,left,caddnode.create_internal(muln,right,cmoddivnode.Create(divn,left.getcopy,right.getcopy)));
  88. right:=nil;
  89. end;
  90. left:=nil;
  91. firstpass(result);
  92. end
  93. else if (nodetype=modn) and
  94. (is_signed(left.resultdef)) and
  95. (right.nodetype=ordconstn) and
  96. (tordconstnode(right).value=2) then
  97. begin
  98. // result:=(0-(left and 1)) and (1+(sarlongint(left,31) shl 1))
  99. result:=caddnode.create_internal(andn,caddnode.create_internal(subn,cordconstnode.create(0,sinttype,false),caddnode.create_internal(andn,left,cordconstnode.create(1,sinttype,false))),
  100. caddnode.create_internal(addn,cordconstnode.create(1,sinttype,false),
  101. cshlshrnode.create(shln,cinlinenode.create(in_sar_x_y,false,ccallparanode.create(cordconstnode.create(31,sinttype,false),ccallparanode.Create(left.getcopy,nil))),cordconstnode.create(1,sinttype,false))));
  102. left:=nil;
  103. firstpass(result);
  104. end
  105. else
  106. result:=inherited first_moddivint;
  107. { we may not change the result type here }
  108. if assigned(result) and (torddef(result.resultdef).ordtype<>torddef(resultdef).ordtype) then
  109. inserttypeconv(result,resultdef);
  110. end;
  111. procedure tarmmoddivnode.pass_generate_code;
  112. var
  113. power : longint;
  114. numerator,
  115. helper1,
  116. helper2,
  117. resultreg : tregister;
  118. size : Tcgsize;
  119. so : tshifterop;
  120. procedure genOrdConstNodeDiv;
  121. begin
  122. if tordconstnode(right).value=0 then
  123. internalerror(2005061701)
  124. else if tordconstnode(right).value=1 then
  125. cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, numerator, resultreg)
  126. else if (tordconstnode(right).value = int64(-1)) then
  127. begin
  128. // note: only in the signed case possible..., may overflow
  129. if cs_check_overflow in current_settings.localswitches then
  130. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  131. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_MVN,
  132. resultreg,numerator),toppostfix(ord(cs_check_overflow in current_settings.localswitches)*ord(PF_S))));
  133. end
  134. else if ispowerof2(tordconstnode(right).value,power) then
  135. begin
  136. if (is_signed(right.resultdef)) then
  137. begin
  138. helper1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  139. helper2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  140. if power = 1 then
  141. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,numerator,helper1)
  142. else
  143. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,31,numerator,helper1);
  144. if GenerateThumbCode then
  145. begin
  146. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,32-power,helper1);
  147. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,helper2,numerator,helper1));
  148. end
  149. else
  150. begin
  151. shifterop_reset(so);
  152. so.shiftmode:=SM_LSR;
  153. so.shiftimm:=32-power;
  154. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,helper2,numerator,helper1,so));
  155. end;
  156. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,OS_INT,power,helper2,resultreg);
  157. end
  158. else
  159. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_INT,power,numerator,resultreg)
  160. end
  161. else {Everything else is handled the generic code}
  162. cg.g_div_const_reg_reg(current_asmdata.CurrAsmList,def_cgsize(resultdef),
  163. tordconstnode(right).value.svalue,numerator,resultreg);
  164. end;
  165. {
  166. procedure genOrdConstNodeMod;
  167. var
  168. modreg, maskreg, tempreg : tregister;
  169. begin
  170. if (tordconstnode(right).value = 0) then begin
  171. internalerror(2005061702);
  172. end
  173. else if (abs(tordconstnode(right).value.svalue) = 1) then
  174. begin
  175. // x mod +/-1 is always zero
  176. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 0, resultreg);
  177. end
  178. else if (ispowerof2(tordconstnode(right).value, power)) then
  179. begin
  180. if (is_signed(right.resultdef)) then begin
  181. tempreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  182. maskreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  183. modreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  184. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, abs(tordconstnode(right).value.svalue)-1, modreg);
  185. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, 31, numerator, maskreg);
  186. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, numerator, modreg, tempreg);
  187. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC, maskreg, maskreg, modreg));
  188. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC, modreg, tempreg, 0));
  189. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE, modreg, modreg, modreg));
  190. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, modreg, maskreg, maskreg);
  191. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_INT, maskreg, tempreg, resultreg);
  192. end else begin
  193. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, tordconstnode(right).value.svalue-1, numerator, resultreg);
  194. end;
  195. end else begin
  196. genOrdConstNodeDiv();
  197. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_MUL, OS_INT, tordconstnode(right).value.svalue, resultreg, resultreg);
  198. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, resultreg, numerator, resultreg);
  199. end;
  200. end;
  201. }
  202. begin
  203. secondpass(left);
  204. secondpass(right);
  205. if ((GenerateThumbCode or GenerateThumb2Code) and (CPUARM_HAS_THUMB_IDIV in cpu_capabilities[current_settings.cputype])) and
  206. (nodetype=divn) and
  207. not(is_64bitint(resultdef)) then
  208. begin
  209. size:=def_cgsize(left.resultdef);
  210. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  211. location_copy(location,left.location);
  212. location.loc := LOC_REGISTER;
  213. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  214. resultreg:=location.register;
  215. if (right.nodetype=ordconstn) and
  216. ((tordconstnode(right).value=1) or
  217. (tordconstnode(right).value=int64(-1)) or
  218. (tordconstnode(right).value=0) or
  219. ispowerof2(tordconstnode(right).value,power)) then
  220. begin
  221. numerator:=left.location.register;
  222. genOrdConstNodeDiv;
  223. end
  224. else
  225. begin
  226. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,left.resultdef,true);
  227. if is_signed(left.resultdef) or
  228. is_signed(right.resultdef) then
  229. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_IDIV,OS_INT,right.location.register,left.location.register,location.register)
  230. else
  231. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_DIV,OS_INT,right.location.register,left.location.register,location.register);
  232. end;
  233. end
  234. else
  235. begin
  236. location_copy(location,left.location);
  237. { put numerator in register }
  238. size:=def_cgsize(left.resultdef);
  239. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  240. left.resultdef,left.resultdef,true);
  241. location_copy(location,left.location);
  242. numerator:=location.register;
  243. resultreg:=location.register;
  244. if location.loc=LOC_CREGISTER then
  245. begin
  246. location.loc := LOC_REGISTER;
  247. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  248. resultreg:=location.register;
  249. end
  250. else if (nodetype=modn) or (right.nodetype=ordconstn) then
  251. begin
  252. // for a modulus op, and for const nodes we need the result register
  253. // to be an extra register
  254. resultreg:=cg.getintregister(current_asmdata.CurrAsmList,size);
  255. end;
  256. if right.nodetype=ordconstn then
  257. begin
  258. if nodetype=divn then
  259. genOrdConstNodeDiv
  260. else
  261. // genOrdConstNodeMod;
  262. end;
  263. location.register:=resultreg;
  264. end;
  265. { unsigned division/module can only overflow in case of division by zero }
  266. { (but checking this overflow flag is more convoluted than performing a }
  267. { simple comparison with 0) }
  268. if is_signed(right.resultdef) then
  269. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  270. end;
  271. {*****************************************************************************
  272. TARMNOTNODE
  273. *****************************************************************************}
  274. procedure tarmnotnode.second_boolean;
  275. var
  276. hl : tasmlabel;
  277. begin
  278. { if the location is LOC_JUMP, we do the secondpass after the
  279. labels are allocated
  280. }
  281. if left.expectloc=LOC_JUMP then
  282. begin
  283. hl:=current_procinfo.CurrTrueLabel;
  284. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  285. current_procinfo.CurrFalseLabel:=hl;
  286. secondpass(left);
  287. if left.location.loc<>LOC_JUMP then
  288. internalerror(2012081305);
  289. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  290. hl:=current_procinfo.CurrTrueLabel;
  291. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  292. current_procinfo.CurrFalseLabel:=hl;
  293. location.loc:=LOC_JUMP;
  294. end
  295. else
  296. begin
  297. secondpass(left);
  298. case left.location.loc of
  299. LOC_FLAGS :
  300. begin
  301. location_copy(location,left.location);
  302. inverse_flags(location.resflags);
  303. end;
  304. LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  305. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  306. begin
  307. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  308. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  309. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,0));
  310. location_reset(location,LOC_FLAGS,OS_NO);
  311. location.resflags:=F_EQ;
  312. end;
  313. else
  314. internalerror(2003042401);
  315. end;
  316. end;
  317. end;
  318. {*****************************************************************************
  319. TARMUNARYMINUSNODE
  320. *****************************************************************************}
  321. function tarmunaryminusnode.pass_1: tnode;
  322. var
  323. procname: string[31];
  324. fdef : tdef;
  325. begin
  326. if (current_settings.fputype=fpu_soft) and
  327. (left.resultdef.typ=floatdef) then
  328. begin
  329. result:=nil;
  330. firstpass(left);
  331. expectloc:=LOC_REGISTER;
  332. exit;
  333. end;
  334. if (current_settings.fputype<>fpu_fpv4_s16) or
  335. (tfloatdef(resultdef).floattype=s32real) then
  336. exit(inherited pass_1);
  337. result:=nil;
  338. firstpass(left);
  339. if codegenerror then
  340. exit;
  341. if (left.resultdef.typ=floatdef) then
  342. begin
  343. case tfloatdef(resultdef).floattype of
  344. s64real:
  345. begin
  346. procname:='float64_sub';
  347. fdef:=search_system_type('FLOAT64').typedef;
  348. end;
  349. else
  350. internalerror(2005082801);
  351. end;
  352. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  353. ctypeconvnode.create_internal(left,fDef),
  354. ccallparanode.create(ctypeconvnode.create_internal(crealconstnode.create(0,resultdef),fdef),nil))),resultdef);
  355. left:=nil;
  356. end
  357. else
  358. begin
  359. if (left.resultdef.typ=floatdef) then
  360. expectloc:=LOC_FPUREGISTER
  361. else if (left.resultdef.typ=orddef) then
  362. expectloc:=LOC_REGISTER;
  363. end;
  364. end;
  365. procedure tarmunaryminusnode.second_float;
  366. var
  367. op: tasmop;
  368. begin
  369. secondpass(left);
  370. case current_settings.fputype of
  371. fpu_fpa,
  372. fpu_fpa10,
  373. fpu_fpa11:
  374. begin
  375. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  376. location:=left.location;
  377. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSF,
  378. location.register,left.location.register,0),
  379. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  380. end;
  381. fpu_vfpv2,
  382. fpu_vfpv3,
  383. fpu_vfpv3_d16:
  384. begin
  385. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  386. location:=left.location;
  387. if (left.location.loc=LOC_CMMREGISTER) then
  388. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  389. if (location.size=OS_F32) then
  390. op:=A_FNEGS
  391. else
  392. op:=A_FNEGD;
  393. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  394. location.register,left.location.register));
  395. end;
  396. fpu_fpv4_s16:
  397. begin
  398. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  399. location:=left.location;
  400. if (left.location.loc=LOC_CMMREGISTER) then
  401. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  402. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VNEG,
  403. location.register,left.location.register), PF_F32));
  404. end;
  405. fpu_soft:
  406. begin
  407. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  408. location:=left.location;
  409. case location.size of
  410. OS_32:
  411. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.register);
  412. OS_64:
  413. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.registerhi);
  414. else
  415. internalerror(2014033101);
  416. end;
  417. end
  418. else
  419. internalerror(2009112602);
  420. end;
  421. end;
  422. function tarmshlshrnode.first_shlshr64bitint: tnode;
  423. begin
  424. if GenerateThumbCode or GenerateThumb2Code then
  425. result:=inherited
  426. else
  427. result := nil;
  428. end;
  429. procedure tarmshlshrnode.second_64bit;
  430. var
  431. v : TConstExprInt;
  432. so: tshifterop;
  433. lreg, resreg: TRegister64;
  434. procedure emit_instr(p: tai);
  435. begin
  436. current_asmdata.CurrAsmList.concat(p);
  437. end;
  438. {This code is build like it gets called with sm=SM_LSR all the time, for SM_LSL dst* and src* have to be reversed}
  439. procedure shift_less_than_32(srchi, srclo, dsthi, dstlo: TRegister; shiftval: Byte; sm: TShiftMode);
  440. begin
  441. shifterop_reset(so);
  442. so.shiftimm:=shiftval;
  443. so.shiftmode:=sm;
  444. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srclo, so));
  445. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dsthi, srchi, so));
  446. if sm = SM_LSR then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  447. so.shiftimm:=32-shiftval;
  448. emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, dstlo, dstlo, srchi, so));
  449. end;
  450. {This code is build like it gets called with sm=SM_LSR all the time, for SM_LSL dst* and src* have to be reversed
  451. This will generate
  452. mov shiftval1, shiftval
  453. cmp shiftval1, #64
  454. movcs shiftval1, #64
  455. rsb shiftval2, shiftval1, #32
  456. mov dstlo, srclo, lsr shiftval1
  457. mov dsthi, srchi, lsr shiftval1
  458. orr dstlo, srchi, lsl shiftval2
  459. subs shiftval2, shiftval1, #32
  460. movpl dstlo, srchi, lsr shiftval2
  461. }
  462. procedure shift_by_variable(srchi, srclo, dsthi, dstlo, shiftval: TRegister; sm: TShiftMode);
  463. var
  464. shiftval1,shiftval2:TRegister;
  465. begin
  466. shifterop_reset(so);
  467. shiftval1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  468. shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  469. cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, shiftval, shiftval1);
  470. {The ARM barrel shifter only considers the lower 8 bits of a register for the shift}
  471. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  472. emit_instr(taicpu.op_reg_const(A_CMP, shiftval1, 64));
  473. emit_instr(setcondition(taicpu.op_reg_const(A_MOV, shiftval1, 64), C_CS));
  474. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  475. {Calculate how much the upper register needs to be shifted left}
  476. emit_instr(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval1, 32));
  477. so.shiftmode:=sm;
  478. so.rs:=shiftval1;
  479. {Shift and zerofill the hi+lo register}
  480. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srclo, so));
  481. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dsthi, srchi, so));
  482. {Fold in the lower 32-shiftval bits}
  483. if sm = SM_LSR then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  484. so.rs:=shiftval2;
  485. emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, dstlo, dstlo, srchi, so));
  486. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  487. emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval1, 32), PF_S));
  488. so.shiftmode:=sm;
  489. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srchi, so), C_PL));
  490. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  491. end;
  492. begin
  493. if GenerateThumbCode or GenerateThumb2Code then
  494. begin
  495. inherited;
  496. exit;
  497. end;
  498. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  499. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  500. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  501. { load left operator in a register }
  502. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  503. (left.location.size<>OS_64) then
  504. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  505. lreg := left.location.register64;
  506. resreg := location.register64;
  507. shifterop_reset(so);
  508. { shifting by a constant directly coded: }
  509. if (right.nodetype=ordconstn) then
  510. begin
  511. v:=Tordconstnode(right).value and 63;
  512. {Single bit shift}
  513. if v = 1 then
  514. if nodetype=shln then
  515. begin
  516. {Shift left by one by 2 simple 32bit additions}
  517. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  518. emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, resreg.reglo, lreg.reglo, lreg.reglo), PF_S));
  519. emit_instr(taicpu.op_reg_reg_reg(A_ADC, resreg.reghi, lreg.reghi, lreg.reghi));
  520. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  521. end
  522. else
  523. begin
  524. {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  525. shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  526. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  527. emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reghi, lreg.reghi, so), PF_S));
  528. so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  529. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reglo, lreg.reglo, so));
  530. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  531. end
  532. {Clear one register and use the cg to generate a normal 32-bit shift}
  533. else if v >= 32 then
  534. if nodetype=shln then
  535. begin
  536. emit_instr(taicpu.op_reg_const(A_MOV, resreg.reglo, 0));
  537. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,v.uvalue-32,lreg.reglo,resreg.reghi);
  538. end
  539. else
  540. begin
  541. emit_instr(taicpu.op_reg_const(A_MOV, resreg.reghi, 0));
  542. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,v.uvalue-32,lreg.reghi,resreg.reglo);
  543. end
  544. {Shift LESS than 32, thats the tricky one}
  545. else if (v < 32) and (v > 1) then
  546. if nodetype=shln then
  547. shift_less_than_32(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, v.uvalue, SM_LSL)
  548. else
  549. shift_less_than_32(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, v.uvalue, SM_LSR);
  550. end
  551. else
  552. begin
  553. { force right operator into a register }
  554. if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  555. (right.location.size<>OS_32) then
  556. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  557. if nodetype = shln then
  558. shift_by_variable(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, right.location.register, SM_LSL)
  559. else
  560. shift_by_variable(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, right.location.register, SM_LSR);
  561. end;
  562. end;
  563. begin
  564. cmoddivnode:=tarmmoddivnode;
  565. cnotnode:=tarmnotnode;
  566. cunaryminusnode:=tarmunaryminusnode;
  567. cshlshrnode:=tarmshlshrnode;
  568. end.