n68kadd.pas 29 KB

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