ncpuadd.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. {
  2. Copyright (c) 2008 by Florian Klaempfl
  3. Code generation for add nodes on the Xtensa
  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 ncpuadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,node,ncgadd,cpubase;
  22. type
  23. TCPUAddNode = class(tcgaddnode)
  24. private
  25. procedure pass_left_and_right;
  26. procedure cmp64_le(left_reg, right_reg: TRegister64; unsigned: boolean);
  27. procedure cmp64_lt(left_reg, right_reg: TRegister64; unsigned: boolean);
  28. protected
  29. function pass_1 : tnode;override;
  30. function first_addfloat: tnode;override;
  31. function use_generic_mul32to64: boolean;override;
  32. function use_generic_mul64bit: boolean;override;
  33. procedure second_addordinal;override;
  34. procedure second_cmpordinal;override;
  35. procedure second_cmpsmallset;override;
  36. procedure second_cmp64bit;override;
  37. procedure second_add64bit;override;
  38. procedure second_cmpfloat;override;
  39. procedure second_addfloat;override;
  40. procedure second_cmp;
  41. end;
  42. implementation
  43. uses
  44. globtype,systems,
  45. cutils,verbose,globals,
  46. symconst,symdef,paramgr,
  47. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  48. cgutils,cgcpu,
  49. cpuinfo,pass_1,pass_2,procinfo,
  50. cpupara,
  51. ncon,nset,nadd,
  52. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32,
  53. hlcgobj;
  54. {*****************************************************************************
  55. TCPUAddNode
  56. *****************************************************************************}
  57. procedure TCPUAddNode.second_addordinal;
  58. var
  59. ophigh: tasmop;
  60. begin
  61. { this is only true, if the CPU supports 32x32 -> 64 bit MUL, see the relevant method }
  62. if (nodetype=muln) and is_64bit(resultdef) then
  63. begin
  64. if not(is_signed(left.resultdef)) or
  65. not(is_signed(right.resultdef)) then
  66. ophigh:=A_MULUH
  67. else
  68. ophigh:=A_MULSH;
  69. pass_left_right;
  70. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  71. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  72. if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  73. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  74. { initialize the result }
  75. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  76. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  77. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  78. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULL,location.register64.reglo,left.location.register,right.location.register));
  79. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(ophigh,location.register64.reghi,left.location.register,right.location.register));
  80. end
  81. else
  82. Inherited;
  83. end;
  84. procedure TCPUAddNode.second_cmpsmallset;
  85. var
  86. tmpreg : tregister;
  87. truelab, falselab: TAsmLabel;
  88. begin
  89. pass_left_right;
  90. if (not(nf_swapped in flags) and
  91. (nodetype = lten)) or
  92. ((nf_swapped in flags) and
  93. (nodetype = gten)) then
  94. swapleftright;
  95. current_asmdata.getjumplabel(truelab);
  96. current_asmdata.getjumplabel(falselab);
  97. location_reset_jump(location,truelab,falselab);
  98. force_reg_left_right(false,false);
  99. case nodetype of
  100. equaln:
  101. begin
  102. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,left.location.register,right.location.register,location.truelabel);
  103. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  104. end;
  105. unequaln:
  106. begin
  107. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left.location.register,right.location.register,location.truelabel);
  108. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  109. end;
  110. lten,
  111. gten:
  112. begin
  113. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  114. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,left.location.register,right.location.register,tmpreg);
  115. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,tmpreg,right.location.register,location.truelabel);
  116. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  117. end;
  118. else
  119. internalerror(2020082401);
  120. end;
  121. end;
  122. procedure TCPUAddNode.second_cmp;
  123. var
  124. cond: TOpCmp;
  125. instr: taicpu;
  126. truelab, falselab: TAsmLabel;
  127. begin
  128. pass_left_right;
  129. current_asmdata.getjumplabel(truelab);
  130. current_asmdata.getjumplabel(falselab);
  131. location_reset_jump(location,truelab,falselab);
  132. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  133. if is_signed(left.resultdef) then
  134. case nodetype of
  135. equaln: cond:=OC_EQ;
  136. unequaln: cond:=OC_NE;
  137. ltn: cond:=OC_LT;
  138. lten: cond:=OC_LTE;
  139. gtn: cond:=OC_GT;
  140. gten: cond:=OC_GTE;
  141. else
  142. internalerror(2020030801);
  143. end
  144. else
  145. case nodetype of
  146. equaln: cond:=OC_EQ;
  147. unequaln: cond:=OC_NE;
  148. ltn: cond:=OC_B;
  149. lten: cond:=OC_BE;
  150. gtn: cond:=OC_A;
  151. gten: cond:=OC_AE;
  152. else
  153. internalerror(2020030801);
  154. end;
  155. if (right.nodetype=ordconstn) and not(nf_swapped in flags) then
  156. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,right.location.value,left.location.register,location.truelabel)
  157. else
  158. begin
  159. force_reg_left_right(false,false);
  160. if nf_swapped in flags then
  161. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,left.location.register,right.location.register,location.truelabel)
  162. else
  163. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,right.location.register,left.location.register,location.truelabel);
  164. end;
  165. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  166. end;
  167. const
  168. cmpops: array[boolean] of TOpCmp = (OC_LT,OC_B);
  169. procedure TCPUAddNode.cmp64_lt(left_reg, right_reg: TRegister64;unsigned: boolean);
  170. begin
  171. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],right_reg.reghi,left_reg.reghi,location.truelabel);
  172. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.falselabel);
  173. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,right_reg.reglo,left_reg.reglo,location.truelabel);
  174. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  175. end;
  176. procedure TCPUAddNode.cmp64_le(left_reg, right_reg: TRegister64;unsigned: boolean);
  177. begin
  178. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],left_reg.reghi,right_reg.reghi,location.falselabel);
  179. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.truelabel);
  180. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,left_reg.reglo,right_reg.reglo,location.falselabel);
  181. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  182. end;
  183. procedure TCPUAddNode.second_cmp64bit;
  184. var
  185. truelabel,
  186. falselabel: tasmlabel;
  187. unsigned: boolean;
  188. left_reg,right_reg: TRegister64;
  189. begin
  190. current_asmdata.getjumplabel(truelabel);
  191. current_asmdata.getjumplabel(falselabel);
  192. location_reset_jump(location,truelabel,falselabel);
  193. pass_left_right;
  194. force_reg_left_right(true,true);
  195. unsigned:=not(is_signed(left.resultdef)) or
  196. not(is_signed(right.resultdef));
  197. left_reg:=left.location.register64;
  198. { force_reg_left_right might leave right as LOC_CONSTANT, however, we cannot take advantage of this yet }
  199. if right.location.loc=LOC_CONSTANT then
  200. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  201. right_reg:=right.location.register64;
  202. case NodeType of
  203. equaln:
  204. begin
  205. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.falselabel);
  206. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,location.falselabel);
  207. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  208. end;
  209. unequaln:
  210. begin
  211. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.truelabel);
  212. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,location.truelabel);
  213. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  214. end;
  215. else
  216. if nf_swapped in flags then
  217. case NodeType of
  218. ltn:
  219. cmp64_lt(right_reg, left_reg,unsigned);
  220. lten:
  221. cmp64_le(right_reg, left_reg,unsigned);
  222. gtn:
  223. cmp64_lt(left_reg, right_reg,unsigned);
  224. gten:
  225. cmp64_le(left_reg, right_reg,unsigned);
  226. else
  227. internalerror(2020082202);
  228. end
  229. else
  230. case NodeType of
  231. ltn:
  232. cmp64_lt(left_reg, right_reg,unsigned);
  233. lten:
  234. cmp64_le(left_reg, right_reg,unsigned);
  235. gtn:
  236. cmp64_lt(right_reg, left_reg,unsigned);
  237. gten:
  238. cmp64_le(right_reg, left_reg,unsigned);
  239. else
  240. internalerror(2020082203);
  241. end;
  242. end;
  243. end;
  244. function TCPUAddNode.pass_1 : tnode;
  245. begin
  246. result:=inherited pass_1;
  247. if not(assigned(result)) and (nodetype in [equaln,unequaln,ltn,lten,gtn,gten]) and
  248. not((FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) and
  249. is_single(left.resultdef) and (nodetype<>slashn)) then
  250. expectloc:=LOC_JUMP;
  251. {$ifdef dummy}
  252. if not(assigned(result)) then
  253. begin
  254. unsigned:=not(is_signed(left.resultdef)) or
  255. not(is_signed(right.resultdef));
  256. if is_64bit(left.resultdef) and
  257. ((nodetype in [equaln,unequaln]) or
  258. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  259. ) then
  260. expectloc:=LOC_FLAGS;
  261. end;
  262. { handling boolean expressions }
  263. if not(assigned(result)) and
  264. (
  265. not(is_boolean(left.resultdef)) or
  266. not(is_boolean(right.resultdef)) or
  267. is_dynamic_array(left.resultdef)
  268. ) then
  269. expectloc:=LOC_FLAGS;
  270. {$endif dummy}
  271. end;
  272. procedure TCPUAddNode.second_cmpordinal;
  273. begin
  274. second_cmp;
  275. end;
  276. procedure TCPUAddNode.pass_left_and_right;
  277. begin
  278. { calculate the operator which is more difficult }
  279. firstcomplex(self);
  280. { in case of constant put it to the left }
  281. if (left.nodetype=ordconstn) then
  282. swapleftright;
  283. secondpass(left);
  284. secondpass(right);
  285. end;
  286. function TCPUAddNode.first_addfloat: tnode;
  287. begin
  288. result := nil;
  289. if (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) and
  290. (tfloatdef(left.resultdef).floattype=s32real) and (nodetype<>slashn) then
  291. begin
  292. if nodetype in [equaln,unequaln,lten,ltn,gten,gtn] then
  293. expectloc:=LOC_FLAGS
  294. else
  295. expectloc:=LOC_FPUREGISTER;
  296. end
  297. else
  298. result:=first_addfloat_soft;
  299. end;
  300. function TCPUAddNode.use_generic_mul32to64: boolean;
  301. begin
  302. result:=not(CPUXTENSA_HAS_MUL32HIGH in cpu_capabilities[current_settings.cputype]) or needoverflowcheck;
  303. end;
  304. function TCPUAddNode.use_generic_mul64bit: boolean;
  305. begin
  306. result:=needoverflowcheck or
  307. (cs_opt_size in current_settings.optimizerswitches) or
  308. not(CPUXTENSA_HAS_MUL32HIGH in cpu_capabilities[current_settings.cputype]);
  309. end;
  310. procedure TCPUAddNode.second_addfloat;
  311. var
  312. op : TAsmOp;
  313. cmpop,
  314. singleprec , inv: boolean;
  315. ai : taicpu;
  316. begin
  317. pass_left_and_right;
  318. if (nf_swapped in flags) then
  319. swapleftright;
  320. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  321. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  322. cmpop:=false;
  323. inv:=false;
  324. case nodetype of
  325. addn :
  326. op:=A_ADD;
  327. muln :
  328. op:=A_MUL;
  329. subn :
  330. op:=A_SUB;
  331. unequaln,
  332. equaln:
  333. begin
  334. op:=A_OEQ;
  335. cmpop:=true;
  336. end;
  337. ltn:
  338. begin
  339. op:=A_OLT;
  340. cmpop:=true;
  341. end;
  342. lten:
  343. begin
  344. op:=A_OLE;
  345. cmpop:=true;
  346. end;
  347. gtn:
  348. begin
  349. op:=A_OLT;
  350. swapleftright;
  351. cmpop:=true;
  352. end;
  353. gten:
  354. begin
  355. op:=A_OLE;
  356. swapleftright;
  357. cmpop:=true;
  358. end;
  359. else
  360. internalerror(2020032601);
  361. end;
  362. { initialize de result }
  363. if cmpop then
  364. begin
  365. if CPUXTENSA_HAS_BOOLEAN_OPTION in cpu_capabilities[current_settings.cputype] then
  366. begin
  367. location_reset(location,LOC_FLAGS,OS_NO);
  368. location.resflags.register:=NR_B0;
  369. location.resflags.flag:=F_NZ;
  370. end
  371. else
  372. Internalerror(2020070402);
  373. end
  374. else
  375. begin
  376. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  377. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  378. end;
  379. { emit the actual operation }
  380. if cmpop then
  381. begin
  382. cg.getcpuregister(current_asmdata.CurrAsmList,location.resflags.register);
  383. ai:=taicpu.op_reg_reg_reg(op,location.resflags.register,left.location.register,right.location.register);
  384. ai.oppostfix:=PF_S;
  385. current_asmdata.CurrAsmList.concat(ai);
  386. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  387. if inv then
  388. location.resflags.flag:=F_Z;
  389. end
  390. else
  391. begin
  392. ai:=taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register);
  393. ai.oppostfix := PF_S;
  394. current_asmdata.CurrAsmList.concat(ai);
  395. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  396. end;
  397. end;
  398. procedure TCPUAddNode.second_cmpfloat;
  399. begin
  400. second_addfloat;
  401. end;
  402. procedure TCPUAddNode.second_add64bit;
  403. var
  404. unsigned: Boolean;
  405. tmpreg: tregister;
  406. begin
  407. if nodetype=muln then
  408. begin
  409. pass_left_right;
  410. unsigned:=((left.resultdef.typ=orddef) and
  411. (torddef(left.resultdef).ordtype=u64bit)) or
  412. ((right.resultdef.typ=orddef) and
  413. (torddef(right.resultdef).ordtype=u64bit));
  414. force_reg_left_right(true,true);
  415. { force_reg_left_right might leave right as LOC_CONSTANT, however, we cannot take advantage of this yet }
  416. if right.location.loc=LOC_CONSTANT then
  417. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  418. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  419. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  420. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  421. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  422. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULL,location.register64.reglo,left.location.register64.reglo,right.location.register64.reglo));
  423. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULUH,location.register64.reghi,left.location.register64.reglo,right.location.register64.reglo));
  424. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULL,tmpreg,left.location.register64.reglo,right.location.register64.reghi));
  425. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register64.reghi,location.register64.reghi,tmpreg));
  426. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULL,tmpreg,left.location.register64.reghi,right.location.register64.reglo));
  427. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register64.reghi,location.register64.reghi,tmpreg));
  428. end
  429. else
  430. Inherited;
  431. end;
  432. begin
  433. caddnode:=tcpuaddnode;
  434. end.