n68kadd.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 cmp64_lt(left_reg,right_reg:tregister64):tregister;
  26. function cmp64_le(left_reg,right_reg:tregister64):tregister;
  27. function cmp64_eq(left_reg,right_reg:tregister64):tregister;
  28. function cmp64_ne(left_reg,right_reg:tregister64):tregister;
  29. function cmp64_ltu(left_reg,right_reg:tregister64):tregister;
  30. function cmp64_leu(left_reg,right_reg:tregister64):tregister;
  31. function getresflags(unsigned: boolean) : tresflags;
  32. function getres64_register(unsigned:boolean;left_reg,right_reg:tregister64):tregister;
  33. protected
  34. procedure second_addfloat;override;
  35. procedure second_cmpfloat;override;
  36. procedure second_cmpordinal;override;
  37. procedure second_cmpsmallset;override;
  38. procedure second_cmp64bit;override;
  39. procedure second_cmpboolean;override;
  40. public
  41. function pass_1:tnode;override;
  42. end;
  43. implementation
  44. uses
  45. globtype,systems,
  46. cutils,verbose,globals,
  47. symconst,symdef,paramgr,symtype,
  48. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  49. cpuinfo,pass_1,pass_2,regvars,
  50. cpupara,cgutils,procinfo,
  51. ncon,nset,
  52. ncgutil,tgobj,rgobj,rgcpu,cgobj,hlcgobj,cg64f32;
  53. {*****************************************************************************
  54. Helpers
  55. *****************************************************************************}
  56. function t68kaddnode.cmp64_lt(left_reg,right_reg:tregister64):tregister;
  57. var
  58. labelcmp64_1,labelcmp64_2 : tasmlabel;
  59. tmpreg : tregister;
  60. begin
  61. tmpreg:=cg.getintregister(current_asmdata.currasmlist,OS_INT);
  62. { load the value for "false" }
  63. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,0,tmpreg);
  64. current_asmdata.getjumplabel(labelcmp64_1);
  65. current_asmdata.getjumplabel(labelcmp64_2);
  66. { check whether left_reg.reghi is less than right_reg.reghi }
  67. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,right_reg.reghi,left_reg.reghi));
  68. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_LT,S_NO,labelcmp64_2));
  69. { are left_reg.reghi and right_reg.reghi equal? }
  70. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64_1));
  71. { is left_reg.reglo less than right_reg.reglo? }
  72. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,right_reg.reglo,left_reg.reglo));
  73. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_CS,S_NO,labelcmp64_2));
  74. current_asmdata.currasmlist.concat(Taicpu.op_sym(A_BRA,S_NO,labelcmp64_1));
  75. cg.a_label(current_asmdata.currasmlist,labelcmp64_2);
  76. { load the value for "true" }
  77. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,1,tmpreg);
  78. cg.a_label(current_asmdata.currasmlist,labelcmp64_1);
  79. result:=tmpreg;
  80. end;
  81. function t68kaddnode.cmp64_le(left_reg,right_reg:tregister64):tregister;
  82. var
  83. labelcmp64_1,labelcmp64_2 : tasmlabel;
  84. tmpreg : tregister;
  85. begin
  86. tmpreg:=cg.getintregister(current_asmdata.currasmlist,OS_INT);
  87. { load the value for "false" }
  88. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,0,tmpreg);
  89. current_asmdata.getjumplabel(labelcmp64_1);
  90. current_asmdata.getjumplabel(labelcmp64_2);
  91. { check whether right_reg.reghi is less than left_reg.reghi }
  92. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reghi,right_reg.reghi));
  93. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_LT,S_NO,labelcmp64_1));
  94. { are left_reg.reghi and right_reg.reghi equal? }
  95. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64_2));
  96. { is right_reg.reglo less than left_reg.reglo? }
  97. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reglo,right_reg.reglo));
  98. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_CS,S_NO,labelcmp64_1));
  99. cg.a_label(current_asmdata.currasmlist,labelcmp64_2);
  100. { load the value for "true" }
  101. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,1,tmpreg);
  102. cg.a_label(current_asmdata.currasmlist,labelcmp64_1);
  103. result:=tmpreg;
  104. end;
  105. function t68kaddnode.cmp64_eq(left_reg,right_reg:tregister64):tregister;
  106. var
  107. labelcmp64 : tasmlabel;
  108. tmpreg : tregister;
  109. begin
  110. tmpreg:=cg.getintregister(current_asmdata.currasmlist,OS_INT);
  111. current_asmdata.getjumplabel(labelcmp64);
  112. { load the value for "false" }
  113. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,0,tmpreg);
  114. { is the high order longword equal? }
  115. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reghi,right_reg.reghi));
  116. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64));
  117. { is the low order longword equal? }
  118. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reglo,right_reg.reglo));
  119. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64));
  120. { load the value for "true" }
  121. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,1,tmpreg);
  122. cg.a_label(current_asmdata.currasmlist,labelcmp64);
  123. result:=tmpreg;
  124. end;
  125. function t68kaddnode.cmp64_ne(left_reg,right_reg:tregister64):tregister;
  126. var
  127. labelcmp64 : tasmlabel;
  128. tmpreg : tregister;
  129. begin
  130. tmpreg:=cg.getintregister(current_asmdata.currasmlist,OS_INT);
  131. current_asmdata.getjumplabel(labelcmp64);
  132. { load the value for "true" }
  133. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,1,tmpreg);
  134. { is the high order longword equal? }
  135. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reghi,right_reg.reghi));
  136. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64));
  137. { is the low order longword equal? }
  138. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reglo,right_reg.reglo));
  139. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64));
  140. { load the value for "false" }
  141. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,0,tmpreg);
  142. cg.a_label(current_asmdata.currasmlist,labelcmp64);
  143. result:=tmpreg;
  144. end;
  145. function t68kaddnode.cmp64_ltu(left_reg,right_reg:tregister64):tregister;
  146. var
  147. labelcmp64_1,labelcmp64_2 : tasmlabel;
  148. tmpreg : tregister;
  149. begin
  150. tmpreg:=cg.getintregister(current_asmdata.currasmlist,OS_INT);
  151. { load the value for "false" }
  152. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,0,tmpreg);
  153. current_asmdata.getjumplabel(labelcmp64_1);
  154. current_asmdata.getjumplabel(labelcmp64_2);
  155. { check whether left_reg.reghi is less than right_reg.reghi }
  156. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,right_reg.reghi,left_reg.reghi));
  157. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_CS,S_NO,labelcmp64_2));
  158. { are left_reg.reghi and right_reg.reghi equal? }
  159. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64_1));
  160. { is left_reg.reglo less than right_reg.reglo? }
  161. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,right_reg.reglo,left_reg.reglo));
  162. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_CS,S_NO,labelcmp64_2));
  163. current_asmdata.currasmlist.concat(Taicpu.op_sym(A_BRA,S_NO,labelcmp64_1));
  164. cg.a_label(current_asmdata.currasmlist,labelcmp64_2);
  165. { load the value for "true" }
  166. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,1,tmpreg);
  167. cg.a_label(current_asmdata.currasmlist,labelcmp64_1);
  168. result:=tmpreg;
  169. end;
  170. function t68kaddnode.cmp64_leu(left_reg,right_reg:tregister64):tregister;
  171. var
  172. labelcmp64_1,labelcmp64_2 : tasmlabel;
  173. tmpreg : tregister;
  174. begin
  175. tmpreg:=cg.getintregister(current_asmdata.currasmlist,OS_INT);
  176. { load the value for "false" }
  177. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,0,tmpreg);
  178. current_asmdata.getjumplabel(labelcmp64_1);
  179. current_asmdata.getjumplabel(labelcmp64_2);
  180. { check whether right_reg.reghi is less than left_reg.reghi }
  181. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reghi,right_reg.reghi));
  182. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_CS,S_NO,labelcmp64_1));
  183. { are left_reg.reghi and right_reg.reghi equal? }
  184. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_NE,S_NO,labelcmp64_2));
  185. { is right_reg.reglo less than left_reg.reglo? }
  186. current_asmdata.currasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,left_reg.reglo,right_reg.reglo));
  187. current_asmdata.currasmlist.concat(taicpu.op_cond_sym(A_BXX,C_CS,S_NO,labelcmp64_1));
  188. cg.a_label(current_asmdata.currasmlist,labelcmp64_2);
  189. { load the value for "true" }
  190. cg.a_load_const_reg(current_asmdata.currasmlist,OS_INT,1,tmpreg);
  191. cg.a_label(current_asmdata.currasmlist,labelcmp64_1);
  192. result:=tmpreg;
  193. end;
  194. function t68kaddnode.getresflags(unsigned : boolean) : tresflags;
  195. begin
  196. case nodetype of
  197. equaln : getresflags:=F_E;
  198. unequaln : getresflags:=F_NE;
  199. else
  200. if not(unsigned) then
  201. begin
  202. if nf_swapped in flags then
  203. case nodetype of
  204. ltn : getresflags:=F_G;
  205. lten : getresflags:=F_GE;
  206. gtn : getresflags:=F_L;
  207. gten : getresflags:=F_LE;
  208. end
  209. else
  210. case nodetype of
  211. ltn : getresflags:=F_L;
  212. lten : getresflags:=F_LE;
  213. gtn : getresflags:=F_G;
  214. gten : getresflags:=F_GE;
  215. end;
  216. end
  217. else
  218. begin
  219. if nf_swapped in flags then
  220. case nodetype of
  221. ltn : getresflags:=F_A;
  222. lten : getresflags:=F_AE;
  223. gtn : getresflags:=F_B;
  224. gten : getresflags:=F_BE;
  225. end
  226. else
  227. case nodetype of
  228. ltn : getresflags:=F_B;
  229. lten : getresflags:=F_BE;
  230. gtn : getresflags:=F_A;
  231. gten : getresflags:=F_AE;
  232. end;
  233. end;
  234. end;
  235. end;
  236. function t68kaddnode.getres64_register(unsigned:boolean;left_reg,right_reg:tregister64):tregister;
  237. begin
  238. case nodetype of
  239. equaln:
  240. result:=cmp64_eq(left_reg,right_reg);
  241. unequaln:
  242. result:=cmp64_ne(left_reg,right_reg);
  243. else
  244. if not unsigned then
  245. begin
  246. if nf_swapped in flags then
  247. case nodetype of
  248. ltn:
  249. result:=cmp64_lt(right_reg,left_reg);
  250. lten:
  251. result:=cmp64_le(right_reg,left_reg);
  252. gtn:
  253. result:=cmp64_lt(left_reg,right_reg);
  254. gten:
  255. result:=cmp64_le(left_reg,right_reg);
  256. end
  257. else
  258. case nodetype of
  259. ltn:
  260. result:=cmp64_lt(left_reg,right_reg);
  261. lten:
  262. result:=cmp64_le(left_reg,right_reg);
  263. gtn:
  264. result:=cmp64_lt(right_reg,left_reg);
  265. gten:
  266. result:=cmp64_le(right_reg,left_reg);
  267. end;
  268. end
  269. else
  270. begin
  271. if nf_swapped in Flags then
  272. case nodetype of
  273. ltn:
  274. result:=cmp64_ltu(right_reg,left_reg);
  275. lten:
  276. result:=cmp64_leu(right_reg,left_reg);
  277. gtn:
  278. result:=cmp64_ltu(left_reg,right_reg);
  279. gten:
  280. result:=cmp64_leu(left_reg,right_reg);
  281. end
  282. else
  283. case nodetype of
  284. ltn:
  285. result:=cmp64_ltu(left_reg,right_reg);
  286. lten:
  287. result:=cmp64_leu(left_reg,right_reg);
  288. gtn:
  289. result:=cmp64_ltu(right_reg,left_reg);
  290. gten:
  291. result:=cmp64_leu(right_reg,left_reg);
  292. end;
  293. end;
  294. end;
  295. end;
  296. {*****************************************************************************
  297. AddFloat
  298. *****************************************************************************}
  299. procedure t68kaddnode.second_addfloat;
  300. var
  301. op : TAsmOp;
  302. cmpop : boolean;
  303. begin
  304. pass_left_right;
  305. cmpop:=false;
  306. case nodetype of
  307. addn :
  308. op:=A_FADD;
  309. muln :
  310. op:=A_FMUL;
  311. subn :
  312. op:=A_FSUB;
  313. slashn :
  314. op:=A_FDIV;
  315. ltn,lten,gtn,gten,
  316. equaln,unequaln :
  317. begin
  318. // op:=A_FCMPO;
  319. cmpop:=true;
  320. end;
  321. else
  322. internalerror(200403182);
  323. end;
  324. // get the operands in the correct order, there are no special cases
  325. // here, everything is register-based
  326. if nf_swapped in flags then
  327. swapleftright;
  328. // put both operands in a register
  329. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  330. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  331. // initialize de result
  332. if not cmpop then
  333. begin
  334. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  335. if left.location.loc = LOC_FPUREGISTER then
  336. location.register := left.location.register
  337. else if right.location.loc = LOC_FPUREGISTER then
  338. location.register := right.location.register
  339. else
  340. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  341. end
  342. else
  343. begin
  344. location_reset(location,LOC_FLAGS,OS_NO);
  345. // FIX ME!
  346. // location.resflags := getresflags;
  347. end;
  348. // emit the actual operation
  349. if not cmpop then
  350. begin
  351. {
  352. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  353. location.register,left.location.register,
  354. right.location.register))
  355. }
  356. end
  357. else
  358. begin
  359. { current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  360. newreg(R_SPECIALREGISTER,location.resflags.cr,R_SUBNONE),left.location.register,right.location.register))}
  361. end;
  362. end;
  363. procedure t68kaddnode.second_cmpfloat;
  364. begin
  365. pass_left_right;
  366. {
  367. if (nf_swapped in flags) then
  368. swapleftright;
  369. }
  370. { force fpureg as location, left right doesn't matter
  371. as both will be in a fpureg }
  372. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  373. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  374. location_reset(location,LOC_FLAGS,OS_NO);
  375. location.resflags:=getresflags(true);
  376. {
  377. if nodetype in [equaln,unequaln] then
  378. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMF,
  379. left.location.register,right.location.register),
  380. cgsize2fpuoppostfix[def_cgsize(resultdef)]))
  381. else
  382. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMFE,
  383. left.location.register,right.location.register),
  384. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  385. location_reset(location,LOC_FLAGS,OS_NO);
  386. location.resflags:=getresflags(false);
  387. }
  388. end;
  389. {*****************************************************************************
  390. Smallsets
  391. *****************************************************************************}
  392. procedure t68kaddnode.second_cmpsmallset;
  393. var
  394. tmpreg : tregister;
  395. begin
  396. pass_left_right;
  397. location_reset(location,LOC_FLAGS,OS_NO);
  398. if (not(nf_swapped in flags) and
  399. (nodetype = lten)) or
  400. ((nf_swapped in flags) and
  401. (nodetype = gten)) then
  402. swapleftright;
  403. { Try to keep right as a constant }
  404. if right.location.loc<>LOC_CONSTANT then
  405. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  406. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  407. case nodetype of
  408. equaln,
  409. unequaln:
  410. begin
  411. if right.location.loc=LOC_CONSTANT then
  412. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,right.location.value,left.location.register))
  413. else
  414. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register,left.location.register));
  415. if nodetype=equaln then
  416. location.resflags:=F_E
  417. else
  418. location.resflags:=F_NE;
  419. end;
  420. lten,
  421. gten:
  422. begin
  423. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  424. if right.location.loc=LOC_CONSTANT then
  425. begin
  426. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_MOVE,S_L,right.location.value,tmpreg));
  427. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,tmpreg,left.location.register));
  428. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,left.location.register));
  429. end
  430. else
  431. begin
  432. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,right.location.register,tmpreg));
  433. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,tmpreg,left.location.register));
  434. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,left.location.register));
  435. end;
  436. location.resflags:=F_E;
  437. end;
  438. else
  439. internalerror(2013092701);
  440. end;
  441. end;
  442. {*****************************************************************************
  443. Ordinals
  444. *****************************************************************************}
  445. procedure t68kaddnode.second_cmpordinal;
  446. var
  447. unsigned : boolean;
  448. useconst : boolean;
  449. tmpreg : tregister;
  450. opsize : topsize;
  451. cmpsize : tcgsize;
  452. begin
  453. pass_left_right;
  454. { set result location }
  455. location_reset(location,LOC_JUMP,OS_NO);
  456. { ToDo : set "allowconstants" to True, but this seems to upset Coldfire
  457. a bit for the CMP instruction => check manual and implement
  458. exception accordingly below }
  459. { load values into registers (except constants) }
  460. force_reg_left_right(true, false);
  461. { determine if the comparison will be unsigned }
  462. unsigned:=not(is_signed(left.resultdef)) or
  463. not(is_signed(right.resultdef));
  464. // get the constant on the right if there is one
  465. if (left.location.loc = LOC_CONSTANT) then
  466. swapleftright;
  467. // can we use an immediate, or do we have to load the
  468. // constant in a register first?
  469. if (right.location.loc = LOC_CONSTANT) then
  470. begin
  471. {$ifdef extdebug}
  472. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.value64)<>0) and ((hi(right.location.value64)<>-1) or unsigned) then
  473. internalerror(2002080301);
  474. {$endif extdebug}
  475. if (nodetype in [equaln,unequaln]) then
  476. if (unsigned and
  477. (right.location.value > high(word))) or
  478. (not unsigned and
  479. (longint(right.location.value) < low(smallint)) or
  480. (longint(right.location.value) > high(smallint))) then
  481. { we can then maybe use a constant in the 'othersigned' case
  482. (the sign doesn't matter for // equal/unequal)}
  483. unsigned := not unsigned;
  484. if (unsigned and
  485. ((right.location.value) <= high(word))) or
  486. (not(unsigned) and
  487. (longint(right.location.value) >= low(smallint)) and
  488. (longint(right.location.value) <= high(smallint))) then
  489. useconst := true
  490. else
  491. begin
  492. useconst := false;
  493. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  494. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  495. aword(right.location.value),tmpreg);
  496. end
  497. end
  498. else
  499. useconst := false;
  500. location.loc := LOC_FLAGS;
  501. location.resflags := getresflags(unsigned);
  502. if tcgsize2size[right.location.size]=tcgsize2size[left.location.size] then
  503. cmpsize:=left.location.size
  504. else
  505. { ToDo : zero/sign extend??? }
  506. if tcgsize2size[right.location.size]<tcgsize2size[left.location.size] then
  507. cmpsize:=left.location.size
  508. else
  509. cmpsize:=right.location.size;
  510. opsize:=tcgsize2opsize[cmpsize];
  511. if opsize=S_NO then
  512. internalerror(2013090301);
  513. { Attention: The RIGHT(!) operand is substracted from and must be a
  514. register! }
  515. if (right.location.loc = LOC_CONSTANT) then
  516. if useconst then
  517. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  518. longint(right.location.value),left.location.register))
  519. else
  520. begin
  521. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  522. tmpreg,left.location.register));
  523. end
  524. else
  525. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  526. right.location.register,left.location.register));
  527. end;
  528. {*****************************************************************************
  529. Boolean
  530. *****************************************************************************}
  531. procedure t68kaddnode.second_cmpboolean;
  532. var
  533. cgop : TOpCg;
  534. cgsize : TCgSize;
  535. isjump : boolean;
  536. otl,ofl : tasmlabel;
  537. begin
  538. // writeln('second_cmpboolean');
  539. { ToDo : add support for pasbool64 and bool64bit }
  540. if (torddef(left.resultdef).ordtype in [pasbool8,bool8bit]) or
  541. (torddef(right.resultdef).ordtype in [pasbool8,bool8bit]) then
  542. cgsize:=OS_8
  543. else
  544. if (torddef(left.resultdef).ordtype in [pasbool16,bool16bit]) or
  545. (torddef(right.resultdef).ordtype in [pasbool16,bool16bit]) then
  546. cgsize:=OS_16
  547. else
  548. cgsize:=OS_32;
  549. if (cs_full_boolean_eval in current_settings.localswitches) or
  550. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  551. begin
  552. if left.nodetype in [ordconstn,realconstn] then
  553. swapleftright;
  554. isjump:=(left.expectloc=LOC_JUMP);
  555. if isjump then
  556. begin
  557. otl:=current_procinfo.CurrTrueLabel;
  558. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  559. ofl:=current_procinfo.CurrFalseLabel;
  560. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  561. end;
  562. secondpass(left);
  563. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then begin
  564. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(cgsize),false);
  565. end;
  566. if isjump then
  567. begin
  568. current_procinfo.CurrTrueLabel:=otl;
  569. current_procinfo.CurrFalseLabel:=ofl;
  570. end;
  571. isjump:=(right.expectloc=LOC_JUMP);
  572. if isjump then
  573. begin
  574. otl:=current_procinfo.CurrTrueLabel;
  575. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  576. ofl:=current_procinfo.CurrFalseLabel;
  577. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  578. end;
  579. secondpass(right);
  580. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  581. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(cgsize),false);
  582. if isjump then
  583. begin
  584. current_procinfo.CurrTrueLabel:=otl;
  585. current_procinfo.CurrFalseLabel:=ofl;
  586. end;
  587. location_reset(location,LOC_FLAGS,OS_NO);
  588. force_reg_left_right(true,false);
  589. if (left.location.loc = LOC_CONSTANT) then
  590. swapleftright;
  591. if (right.location.loc <> LOC_CONSTANT) then
  592. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,
  593. left.location.register,right.location.register))
  594. else
  595. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,
  596. longint(right.location.value),left.location.register));
  597. location.resflags := getresflags(true);
  598. end;
  599. //release_reg_left_right;
  600. end;
  601. function t68kaddnode.pass_1:tnode;
  602. var
  603. ld,rd : tdef;
  604. begin
  605. result:=inherited pass_1;
  606. { for 64 bit operations we return the resulting value in a register }
  607. if not assigned(result) then
  608. begin
  609. rd:=right.resultdef;
  610. ld:=left.resultdef;
  611. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) and
  612. (
  613. ((ld.typ=orddef) and (torddef(ld).ordtype in [u64bit,s64bit,scurrency])) or
  614. ((rd.typ=orddef) and (torddef(rd).ordtype in [u64bit,s64bit,scurrency]))
  615. ) then
  616. expectloc:=LOC_REGISTER;
  617. end;
  618. end;
  619. {*****************************************************************************
  620. 64-bit
  621. *****************************************************************************}
  622. procedure t68kaddnode.second_cmp64bit;
  623. var
  624. unsigned : boolean;
  625. tmp_left_reg : tregister;
  626. begin
  627. pass_left_right;
  628. force_reg_left_right(false,false);
  629. unsigned:=not(is_signed(left.resultdef)) or
  630. not(is_signed(right.resultdef));
  631. location_reset(location,LOC_REGISTER,OS_INT);
  632. location.register:=getres64_register(unsigned,left.location.register64,right.location.register64);
  633. { keep the below code for now, as we could optimize the =/<> code later
  634. on based on it }
  635. // writeln('second_cmp64bit');
  636. // pass_left_right;
  637. // load_left_right(true,false);
  638. (*
  639. case nodetype of
  640. ltn,lten,
  641. gtn,gten:
  642. begin
  643. emit_cmp64_hi;
  644. firstjmp64bitcmp;
  645. emit_cmp64_lo;
  646. secondjmp64bitcmp;
  647. end;
  648. equaln,unequaln:
  649. begin
  650. // instead of doing a complicated compare, do
  651. // (left.hi xor right.hi) or (left.lo xor right.lo)
  652. // (somewhate optimized so that no superfluous 'mr's are
  653. // generated)
  654. if (left.location.loc = LOC_CONSTANT) then
  655. swapleftright;
  656. if (right.location.loc = LOC_CONSTANT) then
  657. begin
  658. if left.location.loc = LOC_REGISTER then
  659. begin
  660. tempreg64.reglo := left.location.register64.reglo;
  661. tempreg64.reghi := left.location.register64.reghi;
  662. end
  663. else
  664. begin
  665. if (aword(right.location.valueqword) <> 0) then
  666. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList)
  667. else
  668. tempreg64.reglo := left.location.register64.reglo;
  669. if ((right.location.valueqword shr 32) <> 0) then
  670. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList)
  671. else
  672. tempreg64.reghi := left.location.register64.reghi;
  673. end;
  674. if (aword(right.location.valueqword) <> 0) then
  675. { negative values can be handled using SUB, }
  676. { positive values < 65535 using XOR. }
  677. if (longint(right.location.valueqword) >= -32767) and
  678. (longint(right.location.valueqword) < 0) then
  679. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  680. aword(right.location.valueqword),
  681. left.location.register64.reglo,tempreg64.reglo)
  682. else
  683. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  684. aword(right.location.valueqword),
  685. left.location.register64.reglo,tempreg64.reglo);
  686. if ((right.location.valueqword shr 32) <> 0) then
  687. if (longint(right.location.valueqword shr 32) >= -32767) and
  688. (longint(right.location.valueqword shr 32) < 0) then
  689. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  690. aword(right.location.valueqword shr 32),
  691. left.location.register64.reghi,tempreg64.reghi)
  692. else
  693. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  694. aword(right.location.valueqword shr 32),
  695. left.location.register64.reghi,tempreg64.reghi);
  696. end
  697. else
  698. begin
  699. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList);
  700. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList);
  701. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,
  702. left.location.register64,right.location.register64,
  703. tempreg64);
  704. end;
  705. cg.a_reg_alloc(current_asmdata.CurrAsmList,R_0);
  706. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  707. tempreg64.reglo,tempreg64.reghi));
  708. cg.a_reg_dealloc(current_asmdata.CurrAsmList,R_0);
  709. if (tempreg64.reglo <> left.location.register64.reglo) then
  710. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reglo);
  711. if (tempreg64.reghi <> left.location.register64.reghi) then
  712. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reghi);
  713. location_reset(location,LOC_FLAGS,OS_NO);
  714. location.resflags := getresflags;
  715. end;
  716. else
  717. internalerror(2002072803);
  718. end;
  719. { set result location }
  720. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  721. { real location only now) (JM) }
  722. if cmpop and
  723. not(nodetype in [equaln,unequaln]) then
  724. location_reset(location,LOC_JUMP,OS_NO);
  725. *)
  726. // location_reset(location,LOC_JUMP,OS_NO);
  727. // writeln('second_cmp64_exit');
  728. end;
  729. begin
  730. caddnode:=t68kaddnode;
  731. end.