n68kadd.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. begin
  303. pass_left_right;
  304. case nodetype of
  305. addn :
  306. op:=A_FADD;
  307. muln :
  308. op:=A_FMUL;
  309. subn :
  310. op:=A_FSUB;
  311. slashn :
  312. op:=A_FDIV;
  313. else
  314. internalerror(200403182);
  315. end;
  316. // get the operands in the correct order, there are no special cases
  317. // here, everything is register-based
  318. if nf_swapped in flags then
  319. swapleftright;
  320. // put both operands in a register
  321. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  322. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  323. // initialize de result
  324. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  325. if left.location.loc = LOC_FPUREGISTER then
  326. location.register := left.location.register
  327. else if right.location.loc = LOC_FPUREGISTER then
  328. location.register := right.location.register
  329. else
  330. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  331. // emit the actual operation
  332. {
  333. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  334. location.register,left.location.register,
  335. right.location.register))
  336. }
  337. end;
  338. procedure t68kaddnode.second_cmpfloat;
  339. begin
  340. pass_left_right;
  341. {
  342. if (nf_swapped in flags) then
  343. swapleftright;
  344. }
  345. { force fpureg as location, left right doesn't matter
  346. as both will be in a fpureg }
  347. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  348. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  349. location_reset(location,LOC_FLAGS,OS_NO);
  350. location.resflags:=getresflags(true);
  351. {
  352. if nodetype in [equaln,unequaln] then
  353. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMF,
  354. left.location.register,right.location.register),
  355. cgsize2fpuoppostfix[def_cgsize(resultdef)]))
  356. else
  357. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMFE,
  358. left.location.register,right.location.register),
  359. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  360. location_reset(location,LOC_FLAGS,OS_NO);
  361. location.resflags:=getresflags(false);
  362. }
  363. end;
  364. {*****************************************************************************
  365. Smallsets
  366. *****************************************************************************}
  367. procedure t68kaddnode.second_cmpsmallset;
  368. var
  369. tmpreg : tregister;
  370. begin
  371. pass_left_right;
  372. location_reset(location,LOC_FLAGS,OS_NO);
  373. if (not(nf_swapped in flags) and
  374. (nodetype = lten)) or
  375. ((nf_swapped in flags) and
  376. (nodetype = gten)) then
  377. swapleftright;
  378. { Try to keep right as a constant }
  379. if right.location.loc<>LOC_CONSTANT then
  380. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  381. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  382. case nodetype of
  383. equaln,
  384. unequaln:
  385. begin
  386. if right.location.loc=LOC_CONSTANT then
  387. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,right.location.value,left.location.register))
  388. else
  389. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register,left.location.register));
  390. if nodetype=equaln then
  391. location.resflags:=F_E
  392. else
  393. location.resflags:=F_NE;
  394. end;
  395. lten,
  396. gten:
  397. begin
  398. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  399. if right.location.loc=LOC_CONSTANT then
  400. begin
  401. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_MOVE,S_L,right.location.value,tmpreg));
  402. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,tmpreg,left.location.register));
  403. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,left.location.register));
  404. end
  405. else
  406. begin
  407. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MOVE,S_L,right.location.register,tmpreg));
  408. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,tmpreg,left.location.register));
  409. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,left.location.register));
  410. end;
  411. location.resflags:=F_E;
  412. end;
  413. else
  414. internalerror(2013092701);
  415. end;
  416. end;
  417. {*****************************************************************************
  418. Ordinals
  419. *****************************************************************************}
  420. procedure t68kaddnode.second_cmpordinal;
  421. var
  422. unsigned : boolean;
  423. useconst : boolean;
  424. tmpreg : tregister;
  425. opsize : topsize;
  426. cmpsize : tcgsize;
  427. begin
  428. pass_left_right;
  429. { set result location }
  430. location_reset(location,LOC_JUMP,OS_NO);
  431. { ToDo : set "allowconstants" to True, but this seems to upset Coldfire
  432. a bit for the CMP instruction => check manual and implement
  433. exception accordingly below }
  434. { load values into registers (except constants) }
  435. force_reg_left_right(true, false);
  436. { determine if the comparison will be unsigned }
  437. unsigned:=not(is_signed(left.resultdef)) or
  438. not(is_signed(right.resultdef));
  439. // get the constant on the right if there is one
  440. if (left.location.loc = LOC_CONSTANT) then
  441. swapleftright;
  442. // can we use an immediate, or do we have to load the
  443. // constant in a register first?
  444. if (right.location.loc = LOC_CONSTANT) then
  445. begin
  446. {$ifdef extdebug}
  447. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.value64)<>0) and ((hi(right.location.value64)<>-1) or unsigned) then
  448. internalerror(2002080301);
  449. {$endif extdebug}
  450. if (nodetype in [equaln,unequaln]) then
  451. if (unsigned and
  452. (right.location.value > high(word))) or
  453. (not unsigned and
  454. (longint(right.location.value) < low(smallint)) or
  455. (longint(right.location.value) > high(smallint))) then
  456. { we can then maybe use a constant in the 'othersigned' case
  457. (the sign doesn't matter for // equal/unequal)}
  458. unsigned := not unsigned;
  459. if (unsigned and
  460. ((right.location.value) <= high(word))) or
  461. (not(unsigned) and
  462. (longint(right.location.value) >= low(smallint)) and
  463. (longint(right.location.value) <= high(smallint))) then
  464. useconst := true
  465. else
  466. begin
  467. useconst := false;
  468. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  469. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  470. aword(right.location.value),tmpreg);
  471. end
  472. end
  473. else
  474. useconst := false;
  475. location.loc := LOC_FLAGS;
  476. location.resflags := getresflags(unsigned);
  477. if tcgsize2size[right.location.size]=tcgsize2size[left.location.size] then
  478. cmpsize:=left.location.size
  479. else
  480. { ToDo : zero/sign extend??? }
  481. if tcgsize2size[right.location.size]<tcgsize2size[left.location.size] then
  482. cmpsize:=left.location.size
  483. else
  484. cmpsize:=right.location.size;
  485. opsize:=tcgsize2opsize[cmpsize];
  486. if opsize=S_NO then
  487. internalerror(2013090301);
  488. { Attention: The RIGHT(!) operand is substracted from and must be a
  489. register! }
  490. if (right.location.loc = LOC_CONSTANT) then
  491. if useconst then
  492. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  493. longint(right.location.value),left.location.register))
  494. else
  495. begin
  496. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  497. tmpreg,left.location.register));
  498. end
  499. else
  500. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  501. right.location.register,left.location.register));
  502. end;
  503. {*****************************************************************************
  504. Boolean
  505. *****************************************************************************}
  506. procedure t68kaddnode.second_cmpboolean;
  507. var
  508. cgop : TOpCg;
  509. cgsize : TCgSize;
  510. isjump : boolean;
  511. otl,ofl : tasmlabel;
  512. begin
  513. // writeln('second_cmpboolean');
  514. { ToDo : add support for pasbool64 and bool64bit }
  515. if (torddef(left.resultdef).ordtype in [pasbool8,bool8bit]) or
  516. (torddef(right.resultdef).ordtype in [pasbool8,bool8bit]) then
  517. cgsize:=OS_8
  518. else
  519. if (torddef(left.resultdef).ordtype in [pasbool16,bool16bit]) or
  520. (torddef(right.resultdef).ordtype in [pasbool16,bool16bit]) then
  521. cgsize:=OS_16
  522. else
  523. cgsize:=OS_32;
  524. if (cs_full_boolean_eval in current_settings.localswitches) or
  525. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  526. begin
  527. if left.nodetype in [ordconstn,realconstn] then
  528. swapleftright;
  529. isjump:=(left.expectloc=LOC_JUMP);
  530. if isjump then
  531. begin
  532. otl:=current_procinfo.CurrTrueLabel;
  533. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  534. ofl:=current_procinfo.CurrFalseLabel;
  535. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  536. end;
  537. secondpass(left);
  538. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then begin
  539. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(cgsize),false);
  540. end;
  541. if isjump then
  542. begin
  543. current_procinfo.CurrTrueLabel:=otl;
  544. current_procinfo.CurrFalseLabel:=ofl;
  545. end;
  546. isjump:=(right.expectloc=LOC_JUMP);
  547. if isjump then
  548. begin
  549. otl:=current_procinfo.CurrTrueLabel;
  550. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  551. ofl:=current_procinfo.CurrFalseLabel;
  552. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  553. end;
  554. secondpass(right);
  555. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  556. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(cgsize),false);
  557. if isjump then
  558. begin
  559. current_procinfo.CurrTrueLabel:=otl;
  560. current_procinfo.CurrFalseLabel:=ofl;
  561. end;
  562. location_reset(location,LOC_FLAGS,OS_NO);
  563. force_reg_left_right(true,false);
  564. if (left.location.loc = LOC_CONSTANT) then
  565. swapleftright;
  566. if (right.location.loc <> LOC_CONSTANT) then
  567. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,
  568. left.location.register,right.location.register))
  569. else
  570. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,
  571. longint(right.location.value),left.location.register));
  572. location.resflags := getresflags(true);
  573. end;
  574. //release_reg_left_right;
  575. end;
  576. function t68kaddnode.pass_1:tnode;
  577. var
  578. ld,rd : tdef;
  579. begin
  580. result:=inherited pass_1;
  581. { for 64 bit operations we return the resulting value in a register }
  582. if not assigned(result) then
  583. begin
  584. rd:=right.resultdef;
  585. ld:=left.resultdef;
  586. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) and
  587. (
  588. ((ld.typ=orddef) and (torddef(ld).ordtype in [u64bit,s64bit,scurrency])) or
  589. ((rd.typ=orddef) and (torddef(rd).ordtype in [u64bit,s64bit,scurrency]))
  590. ) then
  591. expectloc:=LOC_REGISTER;
  592. end;
  593. end;
  594. {*****************************************************************************
  595. 64-bit
  596. *****************************************************************************}
  597. procedure t68kaddnode.second_cmp64bit;
  598. var
  599. unsigned : boolean;
  600. tmp_left_reg : tregister;
  601. begin
  602. pass_left_right;
  603. force_reg_left_right(false,false);
  604. unsigned:=not(is_signed(left.resultdef)) or
  605. not(is_signed(right.resultdef));
  606. location_reset(location,LOC_REGISTER,OS_INT);
  607. location.register:=getres64_register(unsigned,left.location.register64,right.location.register64);
  608. { keep the below code for now, as we could optimize the =/<> code later
  609. on based on it }
  610. // writeln('second_cmp64bit');
  611. // pass_left_right;
  612. // load_left_right(true,false);
  613. (*
  614. case nodetype of
  615. ltn,lten,
  616. gtn,gten:
  617. begin
  618. emit_cmp64_hi;
  619. firstjmp64bitcmp;
  620. emit_cmp64_lo;
  621. secondjmp64bitcmp;
  622. end;
  623. equaln,unequaln:
  624. begin
  625. // instead of doing a complicated compare, do
  626. // (left.hi xor right.hi) or (left.lo xor right.lo)
  627. // (somewhate optimized so that no superfluous 'mr's are
  628. // generated)
  629. if (left.location.loc = LOC_CONSTANT) then
  630. swapleftright;
  631. if (right.location.loc = LOC_CONSTANT) then
  632. begin
  633. if left.location.loc = LOC_REGISTER then
  634. begin
  635. tempreg64.reglo := left.location.register64.reglo;
  636. tempreg64.reghi := left.location.register64.reghi;
  637. end
  638. else
  639. begin
  640. if (aword(right.location.valueqword) <> 0) then
  641. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList)
  642. else
  643. tempreg64.reglo := left.location.register64.reglo;
  644. if ((right.location.valueqword shr 32) <> 0) then
  645. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList)
  646. else
  647. tempreg64.reghi := left.location.register64.reghi;
  648. end;
  649. if (aword(right.location.valueqword) <> 0) then
  650. { negative values can be handled using SUB, }
  651. { positive values < 65535 using XOR. }
  652. if (longint(right.location.valueqword) >= -32767) and
  653. (longint(right.location.valueqword) < 0) then
  654. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  655. aword(right.location.valueqword),
  656. left.location.register64.reglo,tempreg64.reglo)
  657. else
  658. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  659. aword(right.location.valueqword),
  660. left.location.register64.reglo,tempreg64.reglo);
  661. if ((right.location.valueqword shr 32) <> 0) then
  662. if (longint(right.location.valueqword shr 32) >= -32767) and
  663. (longint(right.location.valueqword shr 32) < 0) then
  664. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  665. aword(right.location.valueqword shr 32),
  666. left.location.register64.reghi,tempreg64.reghi)
  667. else
  668. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  669. aword(right.location.valueqword shr 32),
  670. left.location.register64.reghi,tempreg64.reghi);
  671. end
  672. else
  673. begin
  674. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList);
  675. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList);
  676. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,
  677. left.location.register64,right.location.register64,
  678. tempreg64);
  679. end;
  680. cg.a_reg_alloc(current_asmdata.CurrAsmList,R_0);
  681. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  682. tempreg64.reglo,tempreg64.reghi));
  683. cg.a_reg_dealloc(current_asmdata.CurrAsmList,R_0);
  684. if (tempreg64.reglo <> left.location.register64.reglo) then
  685. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reglo);
  686. if (tempreg64.reghi <> left.location.register64.reghi) then
  687. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reghi);
  688. location_reset(location,LOC_FLAGS,OS_NO);
  689. location.resflags := getresflags;
  690. end;
  691. else
  692. internalerror(2002072803);
  693. end;
  694. { set result location }
  695. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  696. { real location only now) (JM) }
  697. if cmpop and
  698. not(nodetype in [equaln,unequaln]) then
  699. location_reset(location,LOC_JUMP,OS_NO);
  700. *)
  701. // location_reset(location,LOC_JUMP,OS_NO);
  702. // writeln('second_cmp64_exit');
  703. end;
  704. begin
  705. caddnode:=t68kaddnode;
  706. end.