n68kadd.pas 36 KB

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