n68kadd.pas 35 KB

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