ncpuadd.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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,cgsize_orddef(OS_INT),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. if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  160. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(OS_INT),true);
  161. if nf_swapped in flags then
  162. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,left.location.register,right.location.register,location.truelabel)
  163. else
  164. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,right.location.register,left.location.register,location.truelabel);
  165. end;
  166. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  167. end;
  168. const
  169. cmpops: array[boolean] of TOpCmp = (OC_LT,OC_B);
  170. procedure TCPUAddNode.cmp64_lt(left_reg, right_reg: TRegister64;unsigned: boolean);
  171. begin
  172. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],right_reg.reghi,left_reg.reghi,location.truelabel);
  173. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.falselabel);
  174. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,right_reg.reglo,left_reg.reglo,location.truelabel);
  175. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  176. end;
  177. procedure TCPUAddNode.cmp64_le(left_reg, right_reg: TRegister64;unsigned: boolean);
  178. begin
  179. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cmpops[unsigned],left_reg.reghi,right_reg.reghi,location.falselabel);
  180. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.truelabel);
  181. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_B,left_reg.reglo,right_reg.reglo,location.falselabel);
  182. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  183. end;
  184. procedure TCPUAddNode.second_cmp64bit;
  185. var
  186. truelabel,
  187. falselabel: tasmlabel;
  188. unsigned: boolean;
  189. left_reg,right_reg: TRegister64;
  190. begin
  191. current_asmdata.getjumplabel(truelabel);
  192. current_asmdata.getjumplabel(falselabel);
  193. location_reset_jump(location,truelabel,falselabel);
  194. pass_left_right;
  195. force_reg_left_right(true,true);
  196. unsigned:=not(is_signed(left.resultdef)) or
  197. not(is_signed(right.resultdef));
  198. left_reg:=left.location.register64;
  199. { force_reg_left_right might leave right as LOC_CONSTANT, however, we cannot take advantage of this yet }
  200. if right.location.loc=LOC_CONSTANT then
  201. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  202. right_reg:=right.location.register64;
  203. case NodeType of
  204. equaln:
  205. begin
  206. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.falselabel);
  207. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,location.falselabel);
  208. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  209. end;
  210. unequaln:
  211. begin
  212. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reghi,right_reg.reghi,location.truelabel);
  213. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_NE,left_reg.reglo,right_reg.reglo,location.truelabel);
  214. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  215. end;
  216. else
  217. if nf_swapped in flags then
  218. case NodeType of
  219. ltn:
  220. cmp64_lt(right_reg, left_reg,unsigned);
  221. lten:
  222. cmp64_le(right_reg, left_reg,unsigned);
  223. gtn:
  224. cmp64_lt(left_reg, right_reg,unsigned);
  225. gten:
  226. cmp64_le(left_reg, right_reg,unsigned);
  227. else
  228. internalerror(2020082202);
  229. end
  230. else
  231. case NodeType of
  232. ltn:
  233. cmp64_lt(left_reg, right_reg,unsigned);
  234. lten:
  235. cmp64_le(left_reg, right_reg,unsigned);
  236. gtn:
  237. cmp64_lt(right_reg, left_reg,unsigned);
  238. gten:
  239. cmp64_le(right_reg, left_reg,unsigned);
  240. else
  241. internalerror(2020082203);
  242. end;
  243. end;
  244. end;
  245. function TCPUAddNode.pass_1 : tnode;
  246. begin
  247. result:=inherited pass_1;
  248. if not(assigned(result)) and (nodetype in [equaln,unequaln,ltn,lten,gtn,gten]) and
  249. not((FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) and
  250. is_single(left.resultdef) and (nodetype<>slashn)) then
  251. expectloc:=LOC_JUMP;
  252. {$ifdef dummy}
  253. if not(assigned(result)) then
  254. begin
  255. unsigned:=not(is_signed(left.resultdef)) or
  256. not(is_signed(right.resultdef));
  257. if is_64bit(left.resultdef) and
  258. ((nodetype in [equaln,unequaln]) or
  259. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  260. ) then
  261. expectloc:=LOC_FLAGS;
  262. end;
  263. { handling boolean expressions }
  264. if not(assigned(result)) and
  265. (
  266. not(is_boolean(left.resultdef)) or
  267. not(is_boolean(right.resultdef)) or
  268. is_dynamic_array(left.resultdef)
  269. ) then
  270. expectloc:=LOC_FLAGS;
  271. {$endif dummy}
  272. end;
  273. procedure TCPUAddNode.second_cmpordinal;
  274. begin
  275. second_cmp;
  276. end;
  277. procedure TCPUAddNode.pass_left_and_right;
  278. begin
  279. { calculate the operator which is more difficult }
  280. firstcomplex(self);
  281. { in case of constant put it to the left }
  282. if (left.nodetype=ordconstn) then
  283. swapleftright;
  284. secondpass(left);
  285. secondpass(right);
  286. end;
  287. function TCPUAddNode.first_addfloat: tnode;
  288. begin
  289. result := nil;
  290. if (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) and
  291. (tfloatdef(left.resultdef).floattype=s32real) and (nodetype<>slashn) then
  292. begin
  293. if nodetype in [equaln,unequaln,lten,ltn,gten,gtn] then
  294. expectloc:=LOC_FLAGS
  295. else
  296. expectloc:=LOC_FPUREGISTER;
  297. end
  298. else
  299. result:=first_addfloat_soft;
  300. end;
  301. function TCPUAddNode.use_generic_mul32to64: boolean;
  302. begin
  303. result:=not(CPUXTENSA_HAS_MUL32HIGH in cpu_capabilities[current_settings.cputype]) or needoverflowcheck;
  304. end;
  305. function TCPUAddNode.use_generic_mul64bit: boolean;
  306. begin
  307. result:=needoverflowcheck or
  308. (cs_opt_size in current_settings.optimizerswitches) or
  309. not(CPUXTENSA_HAS_MUL32HIGH in cpu_capabilities[current_settings.cputype]);
  310. end;
  311. procedure TCPUAddNode.second_addfloat;
  312. var
  313. op : TAsmOp;
  314. cmpop,
  315. singleprec , inv: boolean;
  316. ai : taicpu;
  317. begin
  318. pass_left_and_right;
  319. if (nf_swapped in flags) then
  320. swapleftright;
  321. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  322. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  323. cmpop:=false;
  324. inv:=false;
  325. case nodetype of
  326. addn :
  327. op:=A_ADD;
  328. muln :
  329. op:=A_MUL;
  330. subn :
  331. op:=A_SUB;
  332. unequaln:
  333. begin
  334. op:=A_OEQ;
  335. cmpop:=true;
  336. inv:=true;
  337. end;
  338. equaln:
  339. begin
  340. op:=A_OEQ;
  341. cmpop:=true;
  342. end;
  343. ltn:
  344. begin
  345. op:=A_OLT;
  346. cmpop:=true;
  347. end;
  348. lten:
  349. begin
  350. op:=A_OLE;
  351. cmpop:=true;
  352. end;
  353. gtn:
  354. begin
  355. op:=A_OLT;
  356. swapleftright;
  357. cmpop:=true;
  358. end;
  359. gten:
  360. begin
  361. op:=A_OLE;
  362. swapleftright;
  363. cmpop:=true;
  364. end;
  365. else
  366. internalerror(2020032601);
  367. end;
  368. { initialize de result }
  369. if cmpop then
  370. begin
  371. if CPUXTENSA_HAS_BOOLEAN_OPTION in cpu_capabilities[current_settings.cputype] then
  372. begin
  373. location_reset(location,LOC_FLAGS,OS_NO);
  374. location.resflags.register:=NR_B0;
  375. location.resflags.flag:=F_NZ;
  376. end
  377. else
  378. Internalerror(2020070402);
  379. end
  380. else
  381. begin
  382. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  383. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  384. end;
  385. { emit the actual operation }
  386. if cmpop then
  387. begin
  388. cg.getcpuregister(current_asmdata.CurrAsmList,location.resflags.register);
  389. ai:=taicpu.op_reg_reg_reg(op,location.resflags.register,left.location.register,right.location.register);
  390. ai.oppostfix:=PF_S;
  391. current_asmdata.CurrAsmList.concat(ai);
  392. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  393. if inv then
  394. location.resflags.flag:=F_Z;
  395. end
  396. else
  397. begin
  398. ai:=taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register);
  399. ai.oppostfix := PF_S;
  400. current_asmdata.CurrAsmList.concat(ai);
  401. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  402. end;
  403. end;
  404. procedure TCPUAddNode.second_cmpfloat;
  405. begin
  406. second_addfloat;
  407. end;
  408. procedure TCPUAddNode.second_add64bit;
  409. var
  410. unsigned: Boolean;
  411. tmpreg: tregister;
  412. begin
  413. if nodetype=muln then
  414. begin
  415. pass_left_right;
  416. unsigned:=((left.resultdef.typ=orddef) and
  417. (torddef(left.resultdef).ordtype=u64bit)) or
  418. ((right.resultdef.typ=orddef) and
  419. (torddef(right.resultdef).ordtype=u64bit));
  420. force_reg_left_right(true,true);
  421. { force_reg_left_right might leave right as LOC_CONSTANT, however, we cannot take advantage of this yet }
  422. if right.location.loc=LOC_CONSTANT then
  423. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  424. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  425. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  426. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  427. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  428. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULL,location.register64.reglo,left.location.register64.reglo,right.location.register64.reglo));
  429. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULUH,location.register64.reghi,left.location.register64.reglo,right.location.register64.reglo));
  430. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULL,tmpreg,left.location.register64.reglo,right.location.register64.reghi));
  431. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register64.reghi,location.register64.reghi,tmpreg));
  432. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULL,tmpreg,left.location.register64.reghi,right.location.register64.reglo));
  433. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ADD,location.register64.reghi,location.register64.reghi,tmpreg));
  434. end
  435. else
  436. Inherited;
  437. end;
  438. begin
  439. caddnode:=tcpuaddnode;
  440. end.