n68kadd.pas 36 KB

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