n68kadd.pas 36 KB

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