n68kadd.pas 33 KB

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