n68kadd.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl and Jonas Maebe
  3. Code generation for add nodes on the Motorola 680x0 family
  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 n68kadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,ncgadd,cpubase,cgbase;
  22. type
  23. t68kaddnode = class(tcgaddnode)
  24. private
  25. function getresflags(unsigned: boolean) : tresflags;
  26. function getfloatresflags: tresflags;
  27. function inlineable_realconstnode(const n: tnode): boolean;
  28. procedure second_mul64bit;
  29. protected
  30. function use_generic_mul64bit: boolean; override;
  31. function use_generic_mul32to64: boolean; override;
  32. function use_mul_helper: boolean; override;
  33. procedure second_addfloat;override;
  34. procedure second_cmpfloat;override;
  35. procedure second_addordinal;override;
  36. procedure second_cmpordinal;override;
  37. procedure second_cmpsmallset;override;
  38. procedure second_add64bit;override;
  39. procedure second_cmp64bit;override;
  40. end;
  41. implementation
  42. uses
  43. globtype,systems,
  44. cutils,verbose,globals,
  45. symconst,symdef,paramgr,symtype,
  46. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  47. cpuinfo,pass_1,pass_2,
  48. cpupara,cgutils,procinfo,
  49. ncon,nset,
  50. ncgutil,tgobj,rgobj,rgcpu,cgobj,cgcpu,hlcgobj,cg64f32;
  51. {*****************************************************************************
  52. Helpers
  53. *****************************************************************************}
  54. function t68kaddnode.getresflags(unsigned : boolean) : tresflags;
  55. begin
  56. case nodetype of
  57. equaln : getresflags:=F_E;
  58. unequaln : getresflags:=F_NE;
  59. else
  60. if not(unsigned) then
  61. begin
  62. if nf_swapped in flags then
  63. case nodetype of
  64. ltn : getresflags:=F_G;
  65. lten : getresflags:=F_GE;
  66. gtn : getresflags:=F_L;
  67. gten : getresflags:=F_LE;
  68. else
  69. internalerror(2014082030);
  70. end
  71. else
  72. case nodetype of
  73. ltn : getresflags:=F_L;
  74. lten : getresflags:=F_LE;
  75. gtn : getresflags:=F_G;
  76. gten : getresflags:=F_GE;
  77. else
  78. internalerror(2014082031);
  79. end;
  80. end
  81. else
  82. begin
  83. if nf_swapped in flags then
  84. case nodetype of
  85. ltn : getresflags:=F_A;
  86. lten : getresflags:=F_AE;
  87. gtn : getresflags:=F_B;
  88. gten : getresflags:=F_BE;
  89. else
  90. internalerror(2014082032);
  91. end
  92. else
  93. case nodetype of
  94. ltn : getresflags:=F_B;
  95. lten : getresflags:=F_BE;
  96. gtn : getresflags:=F_A;
  97. gten : getresflags:=F_AE;
  98. else
  99. internalerror(2014082033);
  100. end;
  101. end;
  102. end;
  103. end;
  104. function t68kaddnode.getfloatresflags : tresflags;
  105. begin
  106. case nodetype of
  107. equaln : getfloatresflags:=F_FE;
  108. unequaln : getfloatresflags:=F_FNE;
  109. else
  110. if nf_swapped in flags then
  111. case nodetype of
  112. ltn : getfloatresflags:=F_FG;
  113. lten : getfloatresflags:=F_FGE;
  114. gtn : getfloatresflags:=F_FL;
  115. gten : getfloatresflags:=F_FLE;
  116. else
  117. internalerror(201604260);
  118. end
  119. else
  120. case nodetype of
  121. ltn : getfloatresflags:=F_FL;
  122. lten : getfloatresflags:=F_FLE;
  123. gtn : getfloatresflags:=F_FG;
  124. gten : getfloatresflags:=F_FGE;
  125. else
  126. internalerror(201604261);
  127. end;
  128. end;
  129. end;
  130. function t68kaddnode.inlineable_realconstnode(const n: tnode): boolean;
  131. begin
  132. result:=(n.nodetype = realconstn) and
  133. not ((trealconstnode(n).value_real=MathInf.Value) or
  134. (trealconstnode(n).value_real=MathNegInf.Value) or
  135. (trealconstnode(n).value_real=MathQNaN.value));
  136. end;
  137. {*****************************************************************************
  138. AddFloat
  139. *****************************************************************************}
  140. procedure t68kaddnode.second_addfloat;
  141. var
  142. op : TAsmOp;
  143. href : TReference;
  144. begin
  145. pass_left_right;
  146. case nodetype of
  147. addn :
  148. op:=A_FADD;
  149. muln :
  150. op:=A_FMUL;
  151. subn :
  152. op:=A_FSUB;
  153. slashn :
  154. op:=A_FDIV;
  155. else
  156. internalerror(200403182);
  157. end;
  158. // get the operands in the correct order, there are no special cases
  159. // here, everything is register-based
  160. if nf_swapped in flags then
  161. swapleftright;
  162. case current_settings.fputype of
  163. fpu_68881,fpu_coldfire:
  164. begin
  165. { initialize the result }
  166. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  167. { have left in the register, right can be a memory location }
  168. if not (current_settings.fputype = fpu_coldfire) and
  169. inlineable_realconstnode(left) then
  170. begin
  171. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  172. current_asmdata.CurrAsmList.concat(taicpu.op_realconst_reg(A_FMOVE,tcgsize2opsize[left.location.size],trealconstnode(left).value_real,location.register))
  173. end
  174. else
  175. begin
  176. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  177. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  178. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmlist,OS_NO,OS_NO,left.location.register,location.register);
  179. end;
  180. { emit the actual operation }
  181. case right.location.loc of
  182. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  183. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,fpuregopsize,right.location.register,location.register));
  184. LOC_REFERENCE,LOC_CREFERENCE:
  185. begin
  186. if not (current_settings.fputype = fpu_coldfire) and
  187. inlineable_realconstnode(right) then
  188. current_asmdata.CurrAsmList.concat(taicpu.op_realconst_reg(op,tcgsize2opsize[right.location.size],trealconstnode(right).value_real,location.register))
  189. else
  190. begin
  191. href:=right.location.reference;
  192. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,current_settings.fputype = fpu_coldfire);
  193. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,tcgsize2opsize[right.location.size],href,location.register));
  194. end;
  195. end
  196. else
  197. internalerror(2015021501);
  198. end;
  199. end;
  200. else
  201. // softfpu should be handled in pass1, others are not yet supported...
  202. internalerror(2015010201);
  203. end;
  204. end;
  205. procedure t68kaddnode.second_cmpfloat;
  206. var
  207. tmpreg : tregister;
  208. ai: taicpu;
  209. href : TReference;
  210. begin
  211. pass_left_right;
  212. if (nf_swapped in flags) then
  213. swapleftright;
  214. case current_settings.fputype of
  215. fpu_68881,fpu_coldfire:
  216. begin
  217. location_reset(location,LOC_FLAGS,OS_NO);
  218. location.resflags:=getfloatresflags;
  219. { emit compare }
  220. case right.location.loc of
  221. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  222. begin
  223. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('second_cmpfloat right reg!')));
  224. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  225. begin
  226. href:=left.location.reference;
  227. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,current_settings.fputype = fpu_coldfire);
  228. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FCMP,tcgsize2opsize[left.location.size],href,right.location.register));
  229. toggleflag(nf_swapped);
  230. location.resflags:=getfloatresflags;
  231. end
  232. else
  233. begin
  234. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  235. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCMP,fpuregopsize,right.location.register,left.location.register));
  236. end;
  237. end;
  238. LOC_REFERENCE,LOC_CREFERENCE:
  239. begin
  240. { use FTST, if realconst is 0.0, it would be hard to do this in the
  241. optimizer, because we would need to investigate the referenced value... }
  242. if (right.nodetype = realconstn) and
  243. (trealconstnode(right).value_real = 0.0) then
  244. begin
  245. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  246. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_FTST,fpuregopsize,left.location.register))
  247. else
  248. if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  249. begin
  250. href:=left.location.reference;
  251. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  252. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FTST,tcgsize2opsize[left.location.size],href))
  253. end
  254. else
  255. internalerror(2016051001);
  256. end
  257. else
  258. begin
  259. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  260. if not (current_settings.fputype = fpu_coldfire) and
  261. inlineable_realconstnode(right) then
  262. current_asmdata.CurrAsmList.concat(taicpu.op_realconst_reg(A_FCMP,tcgsize2opsize[right.location.size],trealconstnode(right).value_real,left.location.register))
  263. else
  264. begin
  265. href:=right.location.reference;
  266. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,current_settings.fputype = fpu_coldfire);
  267. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FCMP,tcgsize2opsize[right.location.size],href,left.location.register));
  268. end;
  269. end;
  270. end
  271. else
  272. internalerror(2015021502);
  273. end;
  274. end;
  275. else
  276. // softfpu should be handled in pass1, others are not yet supported...
  277. internalerror(2015010201);
  278. end;
  279. end;
  280. {*****************************************************************************
  281. Smallsets
  282. *****************************************************************************}
  283. procedure t68kaddnode.second_cmpsmallset;
  284. var
  285. tmpreg : tregister;
  286. begin
  287. pass_left_right;
  288. location_reset(location,LOC_FLAGS,OS_NO);
  289. if (not(nf_swapped in flags) and
  290. (nodetype = lten)) or
  291. ((nf_swapped in flags) and
  292. (nodetype = gten)) then
  293. swapleftright;
  294. { Try to keep right as a constant }
  295. if right.location.loc<>LOC_CONSTANT then
  296. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  297. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  298. case nodetype of
  299. equaln,
  300. unequaln:
  301. begin
  302. if right.location.loc=LOC_CONSTANT then
  303. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,right.location.value,left.location.register))
  304. else
  305. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register,left.location.register));
  306. if nodetype=equaln then
  307. location.resflags:=F_E
  308. else
  309. location.resflags:=F_NE;
  310. end;
  311. lten,
  312. gten:
  313. begin
  314. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  315. if right.location.loc=LOC_CONSTANT then
  316. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  317. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,left.location.register,right.location.register,tmpreg);
  318. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,right.location.register));
  319. location.resflags:=F_E;
  320. end;
  321. else
  322. internalerror(2013092701);
  323. end;
  324. end;
  325. {*****************************************************************************
  326. Ordinals
  327. *****************************************************************************}
  328. function t68kaddnode.use_mul_helper: boolean;
  329. begin
  330. result:=(nodetype=muln) and not (CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype]);
  331. end;
  332. procedure t68kaddnode.second_addordinal;
  333. const
  334. mul_op_signed: array[boolean] of tasmop = ( A_MULU, A_MULS );
  335. var
  336. cgop : topcg;
  337. asmop : tasmop;
  338. list : tasmlist;
  339. href : treference;
  340. begin
  341. { if we need to handle overflow checking, fall back to the generic cg }
  342. if (nodetype in [addn,subn,muln]) and
  343. (left.resultdef.typ<>pointerdef) and
  344. (right.resultdef.typ<>pointerdef) and
  345. (cs_check_overflow in current_settings.localswitches) then
  346. begin
  347. inherited;
  348. exit;
  349. end;
  350. list:=current_asmdata.CurrAsmList;
  351. case nodetype of
  352. addn: cgop:=OP_ADD;
  353. xorn: cgop:=OP_XOR;
  354. orn : cgop:=OP_OR;
  355. andn: cgop:=OP_AND;
  356. subn: cgop:=OP_SUB;
  357. muln:
  358. begin
  359. if not(is_signed(left.resultdef)) or
  360. not(is_signed(right.resultdef)) then
  361. cgop:=OP_MUL
  362. else
  363. cgop:=OP_IMUL;
  364. end;
  365. else
  366. internalerror(2013120104);
  367. end;
  368. pass_left_right;
  369. if (nodetype=subn) and (nf_swapped in flags) then
  370. swapleftright;
  371. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  372. { initialize the result }
  373. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  374. { this is only true, if the CPU supports 32x32 -> 64 bit MUL, see the relevant method }
  375. if (nodetype=muln) and is_64bit(resultdef) then
  376. begin
  377. list.concat(tai_comment.create(strpnew('second_addordinal: mul32to64bit')));
  378. asmop:=mul_op_signed[cgop = OP_IMUL];
  379. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  380. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  381. cg.a_load_reg_reg(list,left.location.size,OS_INT,left.location.register,location.register64.reglo);
  382. if not (right.location.size in [OS_S32, OS_32]) or
  383. not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_CONSTANT,LOC_REFERENCE,LOC_CREFERENCE]) or
  384. ((right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,def_cgsize(resultdef))) then
  385. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  386. case right.location.loc of
  387. LOC_REGISTER,
  388. LOC_CREGISTER:
  389. list.concat(taicpu.op_reg_reg_reg(asmop,S_L,right.location.register,location.register64.reghi,location.register64.reglo));
  390. LOC_CONSTANT:
  391. list.concat(taicpu.op_const_reg_reg(asmop,S_L,right.location.value,location.register64.reghi,location.register64.reglo));
  392. LOC_REFERENCE,
  393. LOC_CREFERENCE:
  394. begin
  395. href:=right.location.reference;
  396. tcg68k(cg).fixref(list,href,false);
  397. list.concat(taicpu.op_ref_reg_reg(asmop,S_L,href,location.register64.reghi,location.register64.reglo));
  398. end;
  399. else
  400. internalerror(2017052601);
  401. end;
  402. exit;
  403. end;
  404. location.register := cg.getintregister(current_asmdata.CurrAsmList,location.size);
  405. cg.a_load_reg_reg(current_asmdata.CurrAsmlist,left.location.size,location.size,left.location.register,location.register);
  406. if (location.size <> right.location.size) or
  407. not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_CONSTANT,LOC_REFERENCE,LOC_CREFERENCE]) or
  408. (not(CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype]) and (nodetype = muln)) or
  409. ((right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,def_cgsize(resultdef))) then
  410. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  411. case right.location.loc of
  412. LOC_REGISTER,
  413. LOC_CREGISTER:
  414. cg.a_op_reg_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),right.location.register,location.register);
  415. LOC_CONSTANT:
  416. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),right.location.value,location.register);
  417. LOC_REFERENCE,
  418. LOC_CREFERENCE:
  419. cg.a_op_ref_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),right.location.reference,location.register);
  420. else
  421. internalerror(2016052101);
  422. end;
  423. end;
  424. procedure t68kaddnode.second_cmpordinal;
  425. var
  426. unsigned : boolean;
  427. tmpreg : tregister;
  428. opsize : topsize;
  429. cmpsize : tcgsize;
  430. href: treference;
  431. begin
  432. { determine if the comparison will be unsigned }
  433. unsigned:=not(is_signed(left.resultdef)) or
  434. not(is_signed(right.resultdef));
  435. { this puts constant operand (if any) to the right }
  436. pass_left_right;
  437. { tentatively assume left size (correct for possible TST, will fix later) }
  438. cmpsize:=def_cgsize(left.resultdef);
  439. opsize:=tcgsize2opsize[cmpsize];
  440. { set result location }
  441. location_reset(location,LOC_FLAGS,OS_NO);
  442. { see if we can optimize into TST }
  443. if (right.location.loc=LOC_CONSTANT) and (right.location.value=0) then
  444. begin
  445. { Unsigned <0 or >=0 should not reach pass2, most likely }
  446. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and not needs_unaligned(left.location.reference.alignment,cmpsize) then
  447. begin
  448. href:=left.location.reference;
  449. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  450. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,opsize,href));
  451. location_freetemp(current_asmdata.CurrAsmList,left.location);
  452. end
  453. else
  454. begin
  455. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  456. if (current_settings.cputype = cpu_mc68000) and isaddressregister(left.location.register) then
  457. begin
  458. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,cmpsize);
  459. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,cmpsize,left.location.register,tmpreg);
  460. end
  461. else
  462. tmpreg:=left.location.register;
  463. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,opsize,tmpreg));
  464. end;
  465. location.resflags := getresflags(unsigned);
  466. exit;
  467. end;
  468. { Coldfire supports byte/word compares only starting with ISA_B,
  469. !!see remark about Qemu weirdness in tcg68k.a_cmp_const_reg_label }
  470. if (opsize<>S_L) and (current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c,cfv4e]}) then
  471. begin
  472. { 1) Extension is needed for LOC_REFERENCE, but what about LOC_REGISTER ? Perhaps after fixing cg we can assume
  473. that high bits of registers are correct.
  474. 2) Assuming that extension depends only on source signedness --> destination OS_32 is acceptable. }
  475. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(OS_32),false);
  476. if (right.location.loc<>LOC_CONSTANT) then
  477. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(OS_32),false);
  478. opsize:=S_L;
  479. end
  480. else if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  481. begin
  482. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  483. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  484. else
  485. begin
  486. location_swap(left.location,right.location);
  487. toggleflag(nf_swapped);
  488. end;
  489. end;
  490. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,cmpsize) then
  491. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  492. { left is now in register }
  493. case right.location.loc of
  494. LOC_CONSTANT:
  495. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  496. longint(right.location.value),left.location.register));
  497. LOC_REFERENCE,
  498. LOC_CREFERENCE:
  499. begin
  500. href:=right.location.reference;
  501. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  502. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,opsize,href,
  503. left.location.register));
  504. end;
  505. LOC_REGISTER,
  506. LOC_CREGISTER:
  507. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  508. right.location.register,left.location.register));
  509. else
  510. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  511. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  512. right.location.register,left.location.register));
  513. end;
  514. { update location because sides could have been swapped }
  515. location.resflags:=getresflags(unsigned);
  516. end;
  517. {*****************************************************************************
  518. 64-bit
  519. *****************************************************************************}
  520. function t68kaddnode.use_generic_mul32to64: boolean;
  521. begin
  522. result:=not (CPUM68K_HAS_64BITMUL in cpu_capabilities[current_settings.cputype]);
  523. end;
  524. function t68kaddnode.use_generic_mul64bit: boolean;
  525. begin
  526. result:=(cs_check_overflow in current_settings.localswitches) or
  527. (cs_opt_size in current_settings.optimizerswitches) or
  528. not (CPUM68K_HAS_64BITMUL in cpu_capabilities[current_settings.cputype]);
  529. end;
  530. procedure t68kaddnode.second_add64bit;
  531. begin
  532. if (nodetype=muln) then
  533. second_mul64bit
  534. else
  535. inherited second_add64bit;
  536. end;
  537. procedure t68kaddnode.second_mul64bit;
  538. var
  539. list: TAsmList;
  540. hreg1,hreg2,tmpreg: TRegister;
  541. begin
  542. list:=current_asmdata.CurrAsmList;
  543. pass_left_right;
  544. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  545. hlcg.location_force_reg(list,left.location,left.resultdef,left.resultdef,true);
  546. { calculate 32-bit terms lo(right)*hi(left) and hi(left)*lo(right) }
  547. hreg1:=NR_NO;
  548. hreg2:=NR_NO;
  549. tmpreg:=NR_NO;
  550. if (right.location.loc=LOC_CONSTANT) then
  551. begin
  552. //list.concat(tai_comment.create(strpnew('second_mul64bit: with const')));
  553. { Omit zero terms, if any }
  554. if hi(right.location.value64)<>0 then
  555. begin
  556. hreg2:=cg.getintregister(list,OS_INT);
  557. cg.a_load_const_reg(list,OS_INT,longint(hi(right.location.value64)),hreg2);
  558. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reglo,hreg2));
  559. end;
  560. if lo(right.location.value64)<>0 then
  561. begin
  562. hreg1:=cg.getintregister(list,OS_INT);
  563. tmpreg:=cg.getintregister(list,OS_INT);
  564. cg.a_load_const_reg(list,OS_INT,longint(lo(right.location.value64)),hreg1);
  565. cg.a_load_reg_reg(list,OS_INT,OS_INT,hreg1,tmpreg);
  566. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reghi,hreg1));
  567. end;
  568. end
  569. else
  570. begin
  571. //list.concat(tai_comment.create(strpnew('second_mul64bit: no const')));
  572. hlcg.location_force_reg(list,right.location,right.resultdef,right.resultdef,true);
  573. tmpreg:=right.location.register64.reglo;
  574. hreg1:=cg.getintregister(list,OS_INT);
  575. hreg2:=cg.getintregister(list,OS_INT);
  576. cg.a_load_reg_reg(list,OS_INT,OS_INT,right.location.register64.reglo,hreg1);
  577. cg.a_load_reg_reg(list,OS_INT,OS_INT,right.location.register64.reghi,hreg2);
  578. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reghi,hreg1));
  579. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reglo,hreg2));
  580. end;
  581. { At this point, tmpreg is either lo(right) or NR_NO if lo(left)*lo(right) is zero }
  582. if (tmpreg=NR_NO) then
  583. begin
  584. if (hreg2<>NR_NO) then
  585. begin
  586. location.register64.reghi:=hreg2;
  587. if (hreg1<>NR_NO) then
  588. list.concat(taicpu.op_reg_reg(A_ADD,S_L,hreg1,location.register64.reghi));
  589. end
  590. else if (hreg1<>NR_NO) then
  591. location.register64.reghi:=hreg1
  592. else
  593. internalerror(2017052501);
  594. location.register64.reglo:=cg.getintregister(list,OS_INT);
  595. cg.a_load_const_reg(list,OS_INT,0,location.register64.reglo);
  596. end
  597. else
  598. begin
  599. location.register64.reghi:=cg.getintregister(list,OS_INT);
  600. location.register64.reglo:=cg.getintregister(list,OS_INT);
  601. cg.a_load_reg_reg(list,OS_INT,OS_INT,left.location.register64.reglo,location.register64.reglo);
  602. list.concat(taicpu.op_reg_reg_reg(A_MULU,S_L,tmpreg,location.register64.reghi,location.register64.reglo));
  603. if (hreg2<>NR_NO) then
  604. list.concat(taicpu.op_reg_reg(A_ADD,S_L,hreg2,location.register64.reghi));
  605. if (hreg1<>NR_NO) then
  606. list.concat(taicpu.op_reg_reg(A_ADD,S_L,hreg1,location.register64.reghi));
  607. end;
  608. end;
  609. procedure t68kaddnode.second_cmp64bit;
  610. var
  611. truelabel,
  612. falselabel: tasmlabel;
  613. hlab: tasmlabel;
  614. unsigned : boolean;
  615. href: treference;
  616. procedure firstjmp64bitcmp;
  617. var
  618. oldnodetype : tnodetype;
  619. begin
  620. case nodetype of
  621. ltn,gtn:
  622. begin
  623. if (hlab<>location.truelabel) then
  624. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  625. { cheat a little bit for the negative test }
  626. toggleflag(nf_swapped);
  627. if (hlab<>location.falselabel) then
  628. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  629. toggleflag(nf_swapped);
  630. end;
  631. lten,gten:
  632. begin
  633. oldnodetype:=nodetype;
  634. if nodetype=lten then
  635. nodetype:=ltn
  636. else
  637. nodetype:=gtn;
  638. if (hlab<>location.truelabel) then
  639. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  640. { cheat for the negative test }
  641. if nodetype=ltn then
  642. nodetype:=gtn
  643. else
  644. nodetype:=ltn;
  645. if (hlab<>location.falselabel) then
  646. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  647. nodetype:=oldnodetype;
  648. end;
  649. equaln:
  650. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  651. unequaln:
  652. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  653. end;
  654. end;
  655. procedure secondjmp64bitcmp;
  656. begin
  657. case nodetype of
  658. ltn,gtn,lten,gten:
  659. begin
  660. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  661. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  662. end;
  663. equaln:
  664. begin
  665. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  666. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  667. end;
  668. unequaln:
  669. begin
  670. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  671. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  672. end;
  673. end;
  674. end;
  675. begin
  676. truelabel:=nil;
  677. falselabel:=nil;
  678. { This puts constant operand (if any) to the right }
  679. pass_left_right;
  680. unsigned:=not(is_signed(left.resultdef)) or
  681. not(is_signed(right.resultdef));
  682. current_asmdata.getjumplabel(truelabel);
  683. current_asmdata.getjumplabel(falselabel);
  684. location_reset_jump(location,truelabel,falselabel);
  685. { Relational compares against constants having low dword=0 can omit the
  686. second compare based on the fact that any unsigned value is >=0 }
  687. hlab:=nil;
  688. if (right.location.loc=LOC_CONSTANT) and
  689. (lo(right.location.value64)=0) then
  690. begin
  691. case getresflags(true) of
  692. F_AE: hlab:=location.truelabel;
  693. F_B: hlab:=location.falselabel;
  694. end;
  695. end;
  696. if (right.location.loc=LOC_CONSTANT) and (right.location.value64=0) and
  697. (nodetype in [equaln,unequaln]) then
  698. begin
  699. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and not needs_unaligned(left.location.reference.alignment,OS_INT) then
  700. begin
  701. href:=left.location.reference;
  702. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  703. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  704. firstjmp64bitcmp;
  705. inc(href.offset,4);
  706. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  707. secondjmp64bitcmp;
  708. location_freetemp(current_asmdata.CurrAsmList,left.location);
  709. end
  710. else
  711. begin
  712. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  713. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reglo));
  714. firstjmp64bitcmp;
  715. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reghi));
  716. secondjmp64bitcmp;
  717. end;
  718. exit;
  719. end;
  720. { left and right no register? }
  721. { then one must be demanded }
  722. if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  723. begin
  724. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  725. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  726. else
  727. begin
  728. location_swap(left.location,right.location);
  729. toggleflag(nf_swapped);
  730. end;
  731. end;
  732. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,OS_INT) then
  733. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  734. { left is now in register }
  735. case right.location.loc of
  736. LOC_REGISTER,LOC_CREGISTER:
  737. begin
  738. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reghi,left.location.register64.reghi));
  739. firstjmp64bitcmp;
  740. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reglo,left.location.register64.reglo));
  741. secondjmp64bitcmp;
  742. end;
  743. LOC_REFERENCE,LOC_CREFERENCE:
  744. begin
  745. href:=right.location.reference;
  746. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  747. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reghi));
  748. firstjmp64bitcmp;
  749. inc(href.offset,4);
  750. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reglo));
  751. secondjmp64bitcmp;
  752. location_freetemp(current_asmdata.CurrAsmList,right.location);
  753. end;
  754. LOC_CONSTANT:
  755. begin
  756. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(hi(right.location.value64)),left.location.register64.reghi));
  757. firstjmp64bitcmp;
  758. if assigned(hlab) then
  759. cg.a_jmp_always(current_asmdata.CurrAsmList,hlab)
  760. else
  761. begin
  762. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(lo(right.location.value64)),left.location.register64.reglo));
  763. secondjmp64bitcmp;
  764. end;
  765. end;
  766. else
  767. InternalError(2014072501);
  768. end;
  769. end;
  770. begin
  771. caddnode:=t68kaddnode;
  772. end.