n68kadd.pas 36 KB

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