n68kadd.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. opsize: topsize;
  287. cmpsize : tcgsize;
  288. begin
  289. pass_left_right;
  290. location_reset(location,LOC_FLAGS,OS_NO);
  291. cmpsize:=def_cgsize(left.resultdef);
  292. opsize:=tcgsize2opsize[cmpsize];
  293. { Coldfire supports byte/word compares only starting with ISA_B,
  294. See remark about Qemu weirdness in tcg68k.a_cmp_const_reg_label }
  295. if (opsize<>S_L) and (current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c,cfv4e]}) then
  296. begin
  297. cmpsize:=OS_32;
  298. opsize:=S_L;
  299. end;
  300. if (not(nf_swapped in flags) and
  301. (nodetype = lten)) or
  302. ((nf_swapped in flags) and
  303. (nodetype = gten)) then
  304. swapleftright;
  305. { Try to keep right as a constant }
  306. if right.location.loc<>LOC_CONSTANT then
  307. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(cmpsize),true);
  308. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(cmpsize),true);
  309. case nodetype of
  310. equaln,
  311. unequaln:
  312. begin
  313. if right.location.loc=LOC_CONSTANT then
  314. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,right.location.value,left.location.register))
  315. else
  316. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,right.location.register,left.location.register));
  317. if nodetype=equaln then
  318. location.resflags:=F_E
  319. else
  320. location.resflags:=F_NE;
  321. end;
  322. lten,
  323. gten:
  324. begin
  325. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  326. if right.location.loc=LOC_CONSTANT then
  327. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(cmpsize),false);
  328. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,cmpsize,left.location.register,right.location.register,tmpreg);
  329. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,tmpreg,right.location.register));
  330. location.resflags:=F_E;
  331. end;
  332. else
  333. internalerror(2013092701);
  334. end;
  335. end;
  336. {*****************************************************************************
  337. Ordinals
  338. *****************************************************************************}
  339. function t68kaddnode.use_mul_helper: boolean;
  340. begin
  341. result:=(nodetype=muln) and not (CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype]);
  342. end;
  343. procedure t68kaddnode.second_addordinal;
  344. const
  345. mul_op_signed: array[boolean] of tasmop = ( A_MULU, A_MULS );
  346. var
  347. cgop : topcg;
  348. asmop : tasmop;
  349. list : tasmlist;
  350. href : treference;
  351. begin
  352. { if we need to handle overflow checking, fall back to the generic cg }
  353. if (nodetype in [addn,subn,muln]) and
  354. (left.resultdef.typ<>pointerdef) and
  355. (right.resultdef.typ<>pointerdef) and
  356. (cs_check_overflow in current_settings.localswitches) then
  357. begin
  358. inherited;
  359. exit;
  360. end;
  361. list:=current_asmdata.CurrAsmList;
  362. case nodetype of
  363. addn: cgop:=OP_ADD;
  364. xorn: cgop:=OP_XOR;
  365. orn : cgop:=OP_OR;
  366. andn: cgop:=OP_AND;
  367. subn: cgop:=OP_SUB;
  368. muln:
  369. begin
  370. if not(is_signed(left.resultdef)) or
  371. not(is_signed(right.resultdef)) then
  372. cgop:=OP_MUL
  373. else
  374. cgop:=OP_IMUL;
  375. end;
  376. else
  377. internalerror(2013120104);
  378. end;
  379. pass_left_right;
  380. if (nodetype=subn) and (nf_swapped in flags) then
  381. swapleftright;
  382. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  383. { initialize the result }
  384. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  385. { this is only true, if the CPU supports 32x32 -> 64 bit MUL, see the relevant method }
  386. if (nodetype=muln) and is_64bit(resultdef) then
  387. begin
  388. list.concat(tai_comment.create(strpnew('second_addordinal: mul32to64bit')));
  389. asmop:=mul_op_signed[cgop = OP_IMUL];
  390. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  391. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  392. cg.a_load_reg_reg(list,left.location.size,OS_INT,left.location.register,location.register64.reglo);
  393. if not (right.location.size in [OS_S32, OS_32]) or
  394. not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_CONSTANT,LOC_REFERENCE,LOC_CREFERENCE]) or
  395. ((right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,def_cgsize(resultdef))) then
  396. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  397. case right.location.loc of
  398. LOC_REGISTER,
  399. LOC_CREGISTER:
  400. list.concat(taicpu.op_reg_reg_reg(asmop,S_L,right.location.register,location.register64.reghi,location.register64.reglo));
  401. LOC_CONSTANT:
  402. list.concat(taicpu.op_const_reg_reg(asmop,S_L,right.location.value,location.register64.reghi,location.register64.reglo));
  403. LOC_REFERENCE,
  404. LOC_CREFERENCE:
  405. begin
  406. href:=right.location.reference;
  407. tcg68k(cg).fixref(list,href,false);
  408. list.concat(taicpu.op_ref_reg_reg(asmop,S_L,href,location.register64.reghi,location.register64.reglo));
  409. end;
  410. else
  411. internalerror(2017052601);
  412. end;
  413. exit;
  414. end;
  415. location.register := cg.getintregister(current_asmdata.CurrAsmList,location.size);
  416. cg.a_load_reg_reg(current_asmdata.CurrAsmlist,left.location.size,location.size,left.location.register,location.register);
  417. if (location.size <> right.location.size) or
  418. not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_CONSTANT,LOC_REFERENCE,LOC_CREFERENCE]) or
  419. (not(CPUM68K_HAS_32BITMUL in cpu_capabilities[current_settings.cputype]) and (nodetype = muln)) or
  420. ((right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,def_cgsize(resultdef))) then
  421. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  422. case right.location.loc of
  423. LOC_REGISTER,
  424. LOC_CREGISTER:
  425. cg.a_op_reg_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),right.location.register,location.register);
  426. LOC_CONSTANT:
  427. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),right.location.value,location.register);
  428. LOC_REFERENCE,
  429. LOC_CREFERENCE:
  430. cg.a_op_ref_reg(current_asmdata.CurrAsmList,cgop,def_cgsize(resultdef),right.location.reference,location.register);
  431. else
  432. internalerror(2016052101);
  433. end;
  434. end;
  435. procedure t68kaddnode.second_cmpordinal;
  436. var
  437. unsigned : boolean;
  438. tmpreg : tregister;
  439. opsize : topsize;
  440. cmpsize : tcgsize;
  441. href: treference;
  442. begin
  443. { determine if the comparison will be unsigned }
  444. unsigned:=not(is_signed(left.resultdef)) or
  445. not(is_signed(right.resultdef));
  446. { this puts constant operand (if any) to the right }
  447. pass_left_right;
  448. { tentatively assume left size (correct for possible TST, will fix later) }
  449. cmpsize:=def_cgsize(left.resultdef);
  450. opsize:=tcgsize2opsize[cmpsize];
  451. { set result location }
  452. location_reset(location,LOC_FLAGS,OS_NO);
  453. { see if we can optimize into TST }
  454. if (right.location.loc=LOC_CONSTANT) and (right.location.value=0) then
  455. begin
  456. { Unsigned <0 or >=0 should not reach pass2, most likely }
  457. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and not needs_unaligned(left.location.reference.alignment,cmpsize) then
  458. begin
  459. href:=left.location.reference;
  460. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  461. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,opsize,href));
  462. location_freetemp(current_asmdata.CurrAsmList,left.location);
  463. end
  464. else
  465. begin
  466. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  467. if (current_settings.cputype = cpu_mc68000) and isaddressregister(left.location.register) then
  468. begin
  469. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,cmpsize);
  470. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,cmpsize,left.location.register,tmpreg);
  471. end
  472. else
  473. tmpreg:=left.location.register;
  474. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,opsize,tmpreg));
  475. end;
  476. location.resflags := getresflags(unsigned);
  477. exit;
  478. end;
  479. { Coldfire supports byte/word compares only starting with ISA_B,
  480. !!see remark about Qemu weirdness in tcg68k.a_cmp_const_reg_label }
  481. if (opsize<>S_L) and (current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c,cfv4e]}) then
  482. begin
  483. { 1) Extension is needed for LOC_REFERENCE, but what about LOC_REGISTER ? Perhaps after fixing cg we can assume
  484. that high bits of registers are correct.
  485. 2) Assuming that extension depends only on source signedness --> destination OS_32 is acceptable. }
  486. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(OS_32),false);
  487. if (right.location.loc<>LOC_CONSTANT) then
  488. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(OS_32),false);
  489. opsize:=S_L;
  490. end
  491. else if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  492. begin
  493. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  494. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  495. else
  496. begin
  497. location_swap(left.location,right.location);
  498. toggleflag(nf_swapped);
  499. end;
  500. end;
  501. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,cmpsize) then
  502. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  503. { left is now in register }
  504. case right.location.loc of
  505. LOC_CONSTANT:
  506. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  507. longint(right.location.value),left.location.register));
  508. LOC_REFERENCE,
  509. LOC_CREFERENCE:
  510. begin
  511. href:=right.location.reference;
  512. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  513. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,opsize,href,
  514. left.location.register));
  515. end;
  516. LOC_REGISTER,
  517. LOC_CREGISTER:
  518. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  519. right.location.register,left.location.register));
  520. else
  521. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  522. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  523. right.location.register,left.location.register));
  524. end;
  525. { update location because sides could have been swapped }
  526. location.resflags:=getresflags(unsigned);
  527. end;
  528. {*****************************************************************************
  529. 64-bit
  530. *****************************************************************************}
  531. function t68kaddnode.use_generic_mul32to64: boolean;
  532. begin
  533. result:=not (CPUM68K_HAS_64BITMUL in cpu_capabilities[current_settings.cputype]);
  534. end;
  535. function t68kaddnode.use_generic_mul64bit: boolean;
  536. begin
  537. result:=(cs_check_overflow in current_settings.localswitches) or
  538. (cs_opt_size in current_settings.optimizerswitches) or
  539. not (CPUM68K_HAS_64BITMUL in cpu_capabilities[current_settings.cputype]);
  540. end;
  541. procedure t68kaddnode.second_add64bit;
  542. begin
  543. if (nodetype=muln) then
  544. second_mul64bit
  545. else
  546. inherited second_add64bit;
  547. end;
  548. procedure t68kaddnode.second_mul64bit;
  549. var
  550. list: TAsmList;
  551. hreg1,hreg2,tmpreg: TRegister;
  552. begin
  553. list:=current_asmdata.CurrAsmList;
  554. pass_left_right;
  555. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  556. hlcg.location_force_reg(list,left.location,left.resultdef,left.resultdef,true);
  557. { calculate 32-bit terms lo(right)*hi(left) and hi(left)*lo(right) }
  558. hreg1:=NR_NO;
  559. hreg2:=NR_NO;
  560. tmpreg:=NR_NO;
  561. if (right.location.loc=LOC_CONSTANT) then
  562. begin
  563. //list.concat(tai_comment.create(strpnew('second_mul64bit: with const')));
  564. { Omit zero terms, if any }
  565. if hi(right.location.value64)<>0 then
  566. begin
  567. hreg2:=cg.getintregister(list,OS_INT);
  568. cg.a_load_const_reg(list,OS_INT,longint(hi(right.location.value64)),hreg2);
  569. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reglo,hreg2));
  570. end;
  571. if lo(right.location.value64)<>0 then
  572. begin
  573. hreg1:=cg.getintregister(list,OS_INT);
  574. tmpreg:=cg.getintregister(list,OS_INT);
  575. cg.a_load_const_reg(list,OS_INT,longint(lo(right.location.value64)),hreg1);
  576. cg.a_load_reg_reg(list,OS_INT,OS_INT,hreg1,tmpreg);
  577. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reghi,hreg1));
  578. end;
  579. end
  580. else
  581. begin
  582. //list.concat(tai_comment.create(strpnew('second_mul64bit: no const')));
  583. hlcg.location_force_reg(list,right.location,right.resultdef,right.resultdef,true);
  584. tmpreg:=right.location.register64.reglo;
  585. hreg1:=cg.getintregister(list,OS_INT);
  586. hreg2:=cg.getintregister(list,OS_INT);
  587. cg.a_load_reg_reg(list,OS_INT,OS_INT,right.location.register64.reglo,hreg1);
  588. cg.a_load_reg_reg(list,OS_INT,OS_INT,right.location.register64.reghi,hreg2);
  589. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reghi,hreg1));
  590. list.concat(taicpu.op_reg_reg(A_MULU,S_L,left.location.register64.reglo,hreg2));
  591. end;
  592. { At this point, tmpreg is either lo(right) or NR_NO if lo(left)*lo(right) is zero }
  593. if (tmpreg=NR_NO) then
  594. begin
  595. if (hreg2<>NR_NO) then
  596. begin
  597. location.register64.reghi:=hreg2;
  598. if (hreg1<>NR_NO) then
  599. list.concat(taicpu.op_reg_reg(A_ADD,S_L,hreg1,location.register64.reghi));
  600. end
  601. else if (hreg1<>NR_NO) then
  602. location.register64.reghi:=hreg1
  603. else
  604. internalerror(2017052501);
  605. location.register64.reglo:=cg.getintregister(list,OS_INT);
  606. cg.a_load_const_reg(list,OS_INT,0,location.register64.reglo);
  607. end
  608. else
  609. begin
  610. location.register64.reghi:=cg.getintregister(list,OS_INT);
  611. location.register64.reglo:=cg.getintregister(list,OS_INT);
  612. cg.a_load_reg_reg(list,OS_INT,OS_INT,left.location.register64.reglo,location.register64.reglo);
  613. list.concat(taicpu.op_reg_reg_reg(A_MULU,S_L,tmpreg,location.register64.reghi,location.register64.reglo));
  614. if (hreg2<>NR_NO) then
  615. list.concat(taicpu.op_reg_reg(A_ADD,S_L,hreg2,location.register64.reghi));
  616. if (hreg1<>NR_NO) then
  617. list.concat(taicpu.op_reg_reg(A_ADD,S_L,hreg1,location.register64.reghi));
  618. end;
  619. end;
  620. procedure t68kaddnode.second_cmp64bit;
  621. var
  622. truelabel,
  623. falselabel: tasmlabel;
  624. hlab: tasmlabel;
  625. unsigned : boolean;
  626. href: treference;
  627. procedure firstjmp64bitcmp;
  628. var
  629. oldnodetype : tnodetype;
  630. begin
  631. case nodetype of
  632. ltn,gtn:
  633. begin
  634. if (hlab<>location.truelabel) then
  635. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  636. { cheat a little bit for the negative test }
  637. toggleflag(nf_swapped);
  638. if (hlab<>location.falselabel) then
  639. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  640. toggleflag(nf_swapped);
  641. end;
  642. lten,gten:
  643. begin
  644. oldnodetype:=nodetype;
  645. if nodetype=lten then
  646. nodetype:=ltn
  647. else
  648. nodetype:=gtn;
  649. if (hlab<>location.truelabel) then
  650. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  651. { cheat for the negative test }
  652. if nodetype=ltn then
  653. nodetype:=gtn
  654. else
  655. nodetype:=ltn;
  656. if (hlab<>location.falselabel) then
  657. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  658. nodetype:=oldnodetype;
  659. end;
  660. equaln:
  661. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  662. unequaln:
  663. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  664. end;
  665. end;
  666. procedure secondjmp64bitcmp;
  667. begin
  668. case nodetype of
  669. ltn,gtn,lten,gten:
  670. begin
  671. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  672. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  673. end;
  674. equaln:
  675. begin
  676. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  677. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  678. end;
  679. unequaln:
  680. begin
  681. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  682. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  683. end;
  684. end;
  685. end;
  686. begin
  687. truelabel:=nil;
  688. falselabel:=nil;
  689. { This puts constant operand (if any) to the right }
  690. pass_left_right;
  691. unsigned:=not(is_signed(left.resultdef)) or
  692. not(is_signed(right.resultdef));
  693. current_asmdata.getjumplabel(truelabel);
  694. current_asmdata.getjumplabel(falselabel);
  695. location_reset_jump(location,truelabel,falselabel);
  696. { Relational compares against constants having low dword=0 can omit the
  697. second compare based on the fact that any unsigned value is >=0 }
  698. hlab:=nil;
  699. if (right.location.loc=LOC_CONSTANT) and
  700. (lo(right.location.value64)=0) then
  701. begin
  702. case getresflags(true) of
  703. F_AE: hlab:=location.truelabel;
  704. F_B: hlab:=location.falselabel;
  705. end;
  706. end;
  707. if (right.location.loc=LOC_CONSTANT) and (right.location.value64=0) and
  708. (nodetype in [equaln,unequaln]) then
  709. begin
  710. if (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and not needs_unaligned(left.location.reference.alignment,OS_INT) then
  711. begin
  712. href:=left.location.reference;
  713. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  714. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  715. firstjmp64bitcmp;
  716. inc(href.offset,4);
  717. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  718. secondjmp64bitcmp;
  719. location_freetemp(current_asmdata.CurrAsmList,left.location);
  720. end
  721. else
  722. begin
  723. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  724. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reglo));
  725. firstjmp64bitcmp;
  726. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reghi));
  727. secondjmp64bitcmp;
  728. end;
  729. exit;
  730. end;
  731. { left and right no register? }
  732. { then one must be demanded }
  733. if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  734. begin
  735. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  736. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  737. else
  738. begin
  739. location_swap(left.location,right.location);
  740. toggleflag(nf_swapped);
  741. end;
  742. end;
  743. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and needs_unaligned(right.location.reference.alignment,OS_INT) then
  744. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  745. { left is now in register }
  746. case right.location.loc of
  747. LOC_REGISTER,LOC_CREGISTER:
  748. begin
  749. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reghi,left.location.register64.reghi));
  750. firstjmp64bitcmp;
  751. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reglo,left.location.register64.reglo));
  752. secondjmp64bitcmp;
  753. end;
  754. LOC_REFERENCE,LOC_CREFERENCE:
  755. begin
  756. href:=right.location.reference;
  757. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href,false);
  758. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reghi));
  759. firstjmp64bitcmp;
  760. inc(href.offset,4);
  761. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reglo));
  762. secondjmp64bitcmp;
  763. location_freetemp(current_asmdata.CurrAsmList,right.location);
  764. end;
  765. LOC_CONSTANT:
  766. begin
  767. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(hi(right.location.value64)),left.location.register64.reghi));
  768. firstjmp64bitcmp;
  769. if assigned(hlab) then
  770. cg.a_jmp_always(current_asmdata.CurrAsmList,hlab)
  771. else
  772. begin
  773. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(lo(right.location.value64)),left.location.register64.reglo));
  774. secondjmp64bitcmp;
  775. end;
  776. end;
  777. else
  778. InternalError(2014072501);
  779. end;
  780. end;
  781. begin
  782. caddnode:=t68kaddnode;
  783. end.