narmmat.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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,compinnr,
  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_64bit(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_64bit(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_64bit(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_SHR,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 if CPUARM_HAS_UMULL in cpu_capabilities[current_settings.cputype] then
  162. {Everything else is handled the generic code}
  163. cg.g_div_const_reg_reg(current_asmdata.CurrAsmList,def_cgsize(resultdef),
  164. tordconstnode(right).value.svalue,numerator,resultreg)
  165. else
  166. internalerror(2019012601);
  167. end;
  168. {
  169. procedure genOrdConstNodeMod;
  170. var
  171. modreg, maskreg, tempreg : tregister;
  172. begin
  173. if (tordconstnode(right).value = 0) then begin
  174. internalerror(2005061702);
  175. end
  176. else if (abs(tordconstnode(right).value.svalue) = 1) then
  177. begin
  178. // x mod +/-1 is always zero
  179. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, 0, resultreg);
  180. end
  181. else if (ispowerof2(tordconstnode(right).value, power)) then
  182. begin
  183. if (is_signed(right.resultdef)) then begin
  184. tempreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  185. maskreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  186. modreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  187. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, abs(tordconstnode(right).value.svalue)-1, modreg);
  188. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, 31, numerator, maskreg);
  189. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, numerator, modreg, tempreg);
  190. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ANDC, maskreg, maskreg, modreg));
  191. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC, modreg, tempreg, 0));
  192. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE, modreg, modreg, modreg));
  193. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, modreg, maskreg, maskreg);
  194. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_OR, OS_INT, maskreg, tempreg, resultreg);
  195. end else begin
  196. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_AND, OS_INT, tordconstnode(right).value.svalue-1, numerator, resultreg);
  197. end;
  198. end else begin
  199. genOrdConstNodeDiv();
  200. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_MUL, OS_INT, tordconstnode(right).value.svalue, resultreg, resultreg);
  201. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, resultreg, numerator, resultreg);
  202. end;
  203. end;
  204. }
  205. begin
  206. secondpass(left);
  207. secondpass(right);
  208. if ((GenerateThumbCode or GenerateThumb2Code) and (CPUARM_HAS_THUMB_IDIV in cpu_capabilities[current_settings.cputype])) and
  209. (nodetype=divn) and
  210. not(is_64bitint(resultdef)) then
  211. begin
  212. size:=def_cgsize(left.resultdef);
  213. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  214. location_copy(location,left.location);
  215. location.loc := LOC_REGISTER;
  216. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  217. resultreg:=location.register;
  218. if (right.nodetype=ordconstn) and
  219. ((tordconstnode(right).value=1) or
  220. (tordconstnode(right).value=int64(-1)) or
  221. (tordconstnode(right).value=0) or
  222. ispowerof2(tordconstnode(right).value,power)) then
  223. begin
  224. numerator:=left.location.register;
  225. genOrdConstNodeDiv;
  226. end
  227. else
  228. begin
  229. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,left.resultdef,true);
  230. if is_signed(left.resultdef) or
  231. is_signed(right.resultdef) then
  232. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_IDIV,OS_INT,right.location.register,left.location.register,location.register)
  233. else
  234. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_DIV,OS_INT,right.location.register,left.location.register,location.register);
  235. end;
  236. end
  237. else
  238. begin
  239. location_copy(location,left.location);
  240. { put numerator in register }
  241. size:=def_cgsize(left.resultdef);
  242. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,
  243. left.resultdef,left.resultdef,true);
  244. location_copy(location,left.location);
  245. numerator:=location.register;
  246. resultreg:=location.register;
  247. if location.loc=LOC_CREGISTER then
  248. begin
  249. location.loc := LOC_REGISTER;
  250. location.register := cg.getintregister(current_asmdata.CurrAsmList,size);
  251. resultreg:=location.register;
  252. end
  253. else if (nodetype=modn) or (right.nodetype=ordconstn) then
  254. begin
  255. // for a modulus op, and for const nodes we need the result register
  256. // to be an extra register
  257. resultreg:=cg.getintregister(current_asmdata.CurrAsmList,size);
  258. end;
  259. if (right.nodetype=ordconstn) then
  260. begin
  261. if nodetype=divn then
  262. genOrdConstNodeDiv
  263. else
  264. // genOrdConstNodeMod;
  265. end;
  266. location.register:=resultreg;
  267. end;
  268. { unsigned division/module can only overflow in case of division by zero }
  269. { (but checking this overflow flag is more convoluted than performing a }
  270. { simple comparison with 0) }
  271. if is_signed(right.resultdef) then
  272. cg.g_overflowcheck(current_asmdata.CurrAsmList,location,resultdef);
  273. end;
  274. {*****************************************************************************
  275. TARMNOTNODE
  276. *****************************************************************************}
  277. procedure tarmnotnode.second_boolean;
  278. var
  279. tmpreg : TRegister;
  280. begin
  281. { if the location is LOC_JUMP, we do the secondpass after the
  282. labels are allocated
  283. }
  284. if not handle_locjump then
  285. begin
  286. secondpass(left);
  287. case left.location.loc of
  288. LOC_FLAGS :
  289. begin
  290. location_copy(location,left.location);
  291. inverse_flags(location.resflags);
  292. end;
  293. LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE,
  294. LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF :
  295. begin
  296. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  297. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  298. if is_64bit(resultdef) then
  299. begin
  300. tmpreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_INT);
  301. { OR low and high parts together }
  302. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ORR,tmpreg,left.location.register64.reglo,left.location.register64.reghi),PF_S));
  303. end
  304. else
  305. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,0));
  306. location_reset(location,LOC_FLAGS,OS_NO);
  307. location.resflags:=F_EQ;
  308. end;
  309. else
  310. internalerror(2003042401);
  311. end;
  312. end;
  313. end;
  314. {*****************************************************************************
  315. TARMUNARYMINUSNODE
  316. *****************************************************************************}
  317. function tarmunaryminusnode.pass_1: tnode;
  318. var
  319. procname: string[31];
  320. fdef : tdef;
  321. begin
  322. if (FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[current_settings.fputype]) or
  323. is_single(resultdef) then
  324. exit(inherited pass_1);
  325. result:=nil;
  326. firstpass(left);
  327. if codegenerror then
  328. exit;
  329. if (left.resultdef.typ=floatdef) and (current_settings.fputype=fpu_soft) then
  330. begin
  331. case tfloatdef(resultdef).floattype of
  332. s64real:
  333. begin
  334. procname:='float64_sub';
  335. fdef:=search_system_type('FLOAT64').typedef;
  336. end;
  337. else
  338. internalerror(2005082801);
  339. end;
  340. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  341. ctypeconvnode.create_internal(left,fDef),
  342. ccallparanode.create(ctypeconvnode.create_internal(crealconstnode.create(0,resultdef),fdef),nil))),resultdef);
  343. left:=nil;
  344. end
  345. else
  346. begin
  347. if (left.resultdef.typ=floatdef) then
  348. expectloc:=LOC_FPUREGISTER
  349. else if (left.resultdef.typ=orddef) then
  350. expectloc:=LOC_REGISTER;
  351. end;
  352. end;
  353. procedure tarmunaryminusnode.second_float;
  354. var
  355. pf: TOpPostfix;
  356. begin
  357. secondpass(left);
  358. case current_settings.fputype of
  359. fpu_fpa,
  360. fpu_fpa10,
  361. fpu_fpa11:
  362. begin
  363. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  364. location:=left.location;
  365. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSF,
  366. location.register,left.location.register,0),
  367. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  368. end;
  369. fpu_soft:
  370. begin
  371. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  372. location:=left.location;
  373. case location.size of
  374. OS_32:
  375. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.register);
  376. OS_64:
  377. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,tcgint($80000000),location.registerhi);
  378. else
  379. internalerror(2014033101);
  380. end;
  381. end
  382. else if FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[init_settings.fputype] then
  383. begin
  384. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  385. location:=left.location;
  386. if (left.location.loc=LOC_CMMREGISTER) then
  387. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  388. if (tfloatdef(left.resultdef).floattype=s32real) then
  389. pf:=PF_F32
  390. else
  391. pf:=PF_F64;
  392. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_VNEG,
  393. location.register,left.location.register), pf));
  394. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  395. end
  396. else if FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[init_settings.fputype] then
  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. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  405. end
  406. else
  407. internalerror(2009112602);
  408. end;
  409. end;
  410. function tarmshlshrnode.first_shlshr64bitint: tnode;
  411. begin
  412. if GenerateThumbCode or GenerateThumb2Code then
  413. result:=inherited
  414. else
  415. result := nil;
  416. end;
  417. procedure tarmshlshrnode.second_64bit;
  418. var
  419. v : TConstExprInt;
  420. so: tshifterop;
  421. lreg, resreg: TRegister64;
  422. procedure emit_instr(p: tai);
  423. begin
  424. current_asmdata.CurrAsmList.concat(p);
  425. end;
  426. {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}
  427. procedure shift_less_than_32(srchi, srclo, dsthi, dstlo: TRegister; shiftval: Byte; sm: TShiftMode);
  428. begin
  429. shifterop_reset(so);
  430. so.shiftimm:=shiftval;
  431. so.shiftmode:=sm;
  432. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srclo, so));
  433. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dsthi, srchi, so));
  434. if sm = SM_LSR then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  435. so.shiftimm:=32-shiftval;
  436. emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, dstlo, dstlo, srchi, so));
  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. This will generate
  440. mov shiftval1, shiftval
  441. cmp shiftval1, #64
  442. movcs shiftval1, #64
  443. rsb shiftval2, shiftval1, #32
  444. mov dstlo, srclo, lsr shiftval1
  445. mov dsthi, srchi, lsr shiftval1
  446. orr dstlo, srchi, lsl shiftval2
  447. subs shiftval2, shiftval1, #32
  448. movpl dstlo, srchi, lsr shiftval2
  449. }
  450. procedure shift_by_variable(srchi, srclo, dsthi, dstlo, shiftval: TRegister; sm: TShiftMode);
  451. var
  452. shiftval1,shiftval2:TRegister;
  453. begin
  454. shifterop_reset(so);
  455. shiftval1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  456. shiftval2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  457. cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, shiftval, shiftval1);
  458. {The ARM barrel shifter only considers the lower 8 bits of a register for the shift}
  459. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  460. emit_instr(taicpu.op_reg_const(A_CMP, shiftval1, 64));
  461. emit_instr(setcondition(taicpu.op_reg_const(A_MOV, shiftval1, 64), C_CS));
  462. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  463. {Calculate how much the upper register needs to be shifted left}
  464. emit_instr(taicpu.op_reg_reg_const(A_RSB, shiftval2, shiftval1, 32));
  465. so.shiftmode:=sm;
  466. so.rs:=shiftval1;
  467. {Shift and zerofill the hi+lo register}
  468. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srclo, so));
  469. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, dsthi, srchi, so));
  470. {Fold in the lower 32-shiftval bits}
  471. if sm = SM_LSR then so.shiftmode:=SM_LSL else so.shiftmode:=SM_LSR;
  472. so.rs:=shiftval2;
  473. emit_instr(taicpu.op_reg_reg_reg_shifterop(A_ORR, dstlo, dstlo, srchi, so));
  474. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  475. emit_instr(setoppostfix(taicpu.op_reg_reg_const(A_SUB, shiftval2, shiftval1, 32), PF_S));
  476. so.shiftmode:=sm;
  477. emit_instr(setcondition(taicpu.op_reg_reg_shifterop(A_MOV, dstlo, srchi, so), C_PL));
  478. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  479. end;
  480. begin
  481. if GenerateThumbCode or GenerateThumb2Code then
  482. begin
  483. inherited;
  484. exit;
  485. end;
  486. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  487. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  488. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  489. { load left operator in a register }
  490. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  491. (left.location.size<>OS_64) then
  492. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  493. lreg := left.location.register64;
  494. resreg := location.register64;
  495. shifterop_reset(so);
  496. { shifting by a constant directly coded: }
  497. if (right.nodetype=ordconstn) then
  498. begin
  499. v:=Tordconstnode(right).value and 63;
  500. {Single bit shift}
  501. if v = 1 then
  502. if nodetype=shln then
  503. begin
  504. {Shift left by one by 2 simple 32bit additions}
  505. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  506. emit_instr(setoppostfix(taicpu.op_reg_reg_reg(A_ADD, resreg.reglo, lreg.reglo, lreg.reglo), PF_S));
  507. emit_instr(taicpu.op_reg_reg_reg(A_ADC, resreg.reghi, lreg.reghi, lreg.reghi));
  508. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  509. end
  510. else
  511. begin
  512. {Shift right by first shifting hi by one and then using RRX (rotate right extended), which rotates through the carry}
  513. shifterop_reset(so); so.shiftmode:=SM_LSR; so.shiftimm:=1;
  514. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  515. emit_instr(setoppostfix(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reghi, lreg.reghi, so), PF_S));
  516. so.shiftmode:=SM_RRX; so.shiftimm:=0; {RRX does NOT have a shift amount}
  517. emit_instr(taicpu.op_reg_reg_shifterop(A_MOV, resreg.reglo, lreg.reglo, so));
  518. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  519. end
  520. {Clear one register and use the cg to generate a normal 32-bit shift}
  521. else if v >= 32 then
  522. if nodetype=shln then
  523. begin
  524. emit_instr(taicpu.op_reg_const(A_MOV, resreg.reglo, 0));
  525. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_32,v.uvalue-32,lreg.reglo,resreg.reghi);
  526. end
  527. else
  528. begin
  529. emit_instr(taicpu.op_reg_const(A_MOV, resreg.reghi, 0));
  530. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_32,v.uvalue-32,lreg.reghi,resreg.reglo);
  531. end
  532. {Shift LESS than 32, thats the tricky one}
  533. else if (v < 32) and (v > 1) then
  534. if nodetype=shln then
  535. shift_less_than_32(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, v.uvalue, SM_LSL)
  536. else
  537. shift_less_than_32(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, v.uvalue, SM_LSR);
  538. end
  539. else
  540. begin
  541. { force right operator into a register }
  542. if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  543. (right.location.size<>OS_32) then
  544. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  545. if nodetype = shln then
  546. shift_by_variable(lreg.reglo, lreg.reghi, resreg.reglo, resreg.reghi, right.location.register, SM_LSL)
  547. else
  548. shift_by_variable(lreg.reghi, lreg.reglo, resreg.reghi, resreg.reglo, right.location.register, SM_LSR);
  549. end;
  550. end;
  551. begin
  552. cmoddivnode:=tarmmoddivnode;
  553. cnotnode:=tarmnotnode;
  554. cunaryminusnode:=tarmunaryminusnode;
  555. cshlshrnode:=tarmshlshrnode;
  556. end.