n68kadd.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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,left.location.size);
  398. if right.location.loc=LOC_CONSTANT then
  399. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  400. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,left.location.register,right.location.register,tmpreg);
  401. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,right.location.register));
  402. location.resflags:=F_E;
  403. end;
  404. else
  405. internalerror(2013092701);
  406. end;
  407. end;
  408. {*****************************************************************************
  409. Ordinals
  410. *****************************************************************************}
  411. procedure t68kaddnode.second_cmpordinal;
  412. var
  413. unsigned : boolean;
  414. useconst : boolean;
  415. tmpreg : tregister;
  416. opsize : topsize;
  417. cmpsize : tcgsize;
  418. begin
  419. pass_left_right;
  420. { set result location }
  421. location_reset(location,LOC_JUMP,OS_NO);
  422. { ToDo : set "allowconstants" to True, but this seems to upset Coldfire
  423. a bit for the CMP instruction => check manual and implement
  424. exception accordingly below }
  425. { load values into registers (except constants) }
  426. force_reg_left_right(true, false);
  427. { determine if the comparison will be unsigned }
  428. unsigned:=not(is_signed(left.resultdef)) or
  429. not(is_signed(right.resultdef));
  430. // get the constant on the right if there is one
  431. if (left.location.loc = LOC_CONSTANT) then
  432. swapleftright;
  433. // can we use an immediate, or do we have to load the
  434. // constant in a register first?
  435. if (right.location.loc = LOC_CONSTANT) then
  436. begin
  437. {$ifdef extdebug}
  438. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.value64)<>0) and ((hi(right.location.value64)<>-1) or unsigned) then
  439. internalerror(2002080301);
  440. {$endif extdebug}
  441. if (nodetype in [equaln,unequaln]) then
  442. if (unsigned and
  443. (right.location.value > high(word))) or
  444. (not unsigned and
  445. (longint(right.location.value) < low(smallint)) or
  446. (longint(right.location.value) > high(smallint))) then
  447. { we can then maybe use a constant in the 'othersigned' case
  448. (the sign doesn't matter for // equal/unequal)}
  449. unsigned := not unsigned;
  450. if (unsigned and
  451. ((right.location.value) <= high(word))) or
  452. (not(unsigned) and
  453. (longint(right.location.value) >= low(smallint)) and
  454. (longint(right.location.value) <= high(smallint))) then
  455. useconst := true
  456. else
  457. begin
  458. useconst := false;
  459. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  460. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  461. aword(right.location.value),tmpreg);
  462. end
  463. end
  464. else
  465. useconst := false;
  466. location.loc := LOC_FLAGS;
  467. location.resflags := getresflags(unsigned);
  468. if tcgsize2size[right.location.size]=tcgsize2size[left.location.size] then
  469. cmpsize:=left.location.size
  470. else
  471. { ToDo : zero/sign extend??? }
  472. if tcgsize2size[right.location.size]<tcgsize2size[left.location.size] then
  473. cmpsize:=left.location.size
  474. else
  475. cmpsize:=right.location.size;
  476. opsize:=tcgsize2opsize[cmpsize];
  477. if opsize=S_NO then
  478. internalerror(2013090301);
  479. { Attention: The RIGHT(!) operand is substracted from and must be a
  480. register! }
  481. if (right.location.loc = LOC_CONSTANT) then
  482. if useconst then
  483. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  484. longint(right.location.value),left.location.register))
  485. else
  486. begin
  487. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  488. tmpreg,left.location.register));
  489. end
  490. else
  491. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  492. right.location.register,left.location.register));
  493. end;
  494. function t68kaddnode.pass_1:tnode;
  495. var
  496. ld,rd : tdef;
  497. begin
  498. result:=inherited pass_1;
  499. { for 64 bit operations we return the resulting value in a register }
  500. if not assigned(result) then
  501. begin
  502. rd:=right.resultdef;
  503. ld:=left.resultdef;
  504. if (nodetype in [ltn,lten,gtn,gten,equaln,unequaln]) and
  505. (
  506. ((ld.typ=orddef) and (torddef(ld).ordtype in [u64bit,s64bit,scurrency])) or
  507. ((rd.typ=orddef) and (torddef(rd).ordtype in [u64bit,s64bit,scurrency]))
  508. ) then
  509. expectloc:=LOC_REGISTER;
  510. end;
  511. end;
  512. {*****************************************************************************
  513. 64-bit
  514. *****************************************************************************}
  515. procedure t68kaddnode.second_cmp64bit;
  516. var
  517. unsigned : boolean;
  518. tmp_left_reg : tregister;
  519. begin
  520. pass_left_right;
  521. force_reg_left_right(false,false);
  522. unsigned:=not(is_signed(left.resultdef)) or
  523. not(is_signed(right.resultdef));
  524. location_reset(location,LOC_REGISTER,OS_INT);
  525. location.register:=getres64_register(unsigned,left.location.register64,right.location.register64);
  526. { keep the below code for now, as we could optimize the =/<> code later
  527. on based on it }
  528. // writeln('second_cmp64bit');
  529. // pass_left_right;
  530. // load_left_right(true,false);
  531. (*
  532. case nodetype of
  533. ltn,lten,
  534. gtn,gten:
  535. begin
  536. emit_cmp64_hi;
  537. firstjmp64bitcmp;
  538. emit_cmp64_lo;
  539. secondjmp64bitcmp;
  540. end;
  541. equaln,unequaln:
  542. begin
  543. // instead of doing a complicated compare, do
  544. // (left.hi xor right.hi) or (left.lo xor right.lo)
  545. // (somewhate optimized so that no superfluous 'mr's are
  546. // generated)
  547. if (left.location.loc = LOC_CONSTANT) then
  548. swapleftright;
  549. if (right.location.loc = LOC_CONSTANT) then
  550. begin
  551. if left.location.loc = LOC_REGISTER then
  552. begin
  553. tempreg64.reglo := left.location.register64.reglo;
  554. tempreg64.reghi := left.location.register64.reghi;
  555. end
  556. else
  557. begin
  558. if (aword(right.location.valueqword) <> 0) then
  559. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList)
  560. else
  561. tempreg64.reglo := left.location.register64.reglo;
  562. if ((right.location.valueqword shr 32) <> 0) then
  563. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList)
  564. else
  565. tempreg64.reghi := left.location.register64.reghi;
  566. end;
  567. if (aword(right.location.valueqword) <> 0) then
  568. { negative values can be handled using SUB, }
  569. { positive values < 65535 using XOR. }
  570. if (longint(right.location.valueqword) >= -32767) and
  571. (longint(right.location.valueqword) < 0) then
  572. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  573. aword(right.location.valueqword),
  574. left.location.register64.reglo,tempreg64.reglo)
  575. else
  576. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  577. aword(right.location.valueqword),
  578. left.location.register64.reglo,tempreg64.reglo);
  579. if ((right.location.valueqword shr 32) <> 0) then
  580. if (longint(right.location.valueqword shr 32) >= -32767) and
  581. (longint(right.location.valueqword shr 32) < 0) then
  582. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  583. aword(right.location.valueqword shr 32),
  584. left.location.register64.reghi,tempreg64.reghi)
  585. else
  586. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  587. aword(right.location.valueqword shr 32),
  588. left.location.register64.reghi,tempreg64.reghi);
  589. end
  590. else
  591. begin
  592. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList);
  593. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList);
  594. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,
  595. left.location.register64,right.location.register64,
  596. tempreg64);
  597. end;
  598. cg.a_reg_alloc(current_asmdata.CurrAsmList,R_0);
  599. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  600. tempreg64.reglo,tempreg64.reghi));
  601. cg.a_reg_dealloc(current_asmdata.CurrAsmList,R_0);
  602. if (tempreg64.reglo <> left.location.register64.reglo) then
  603. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reglo);
  604. if (tempreg64.reghi <> left.location.register64.reghi) then
  605. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reghi);
  606. location_reset(location,LOC_FLAGS,OS_NO);
  607. location.resflags := getresflags;
  608. end;
  609. else
  610. internalerror(2002072803);
  611. end;
  612. { set result location }
  613. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  614. { real location only now) (JM) }
  615. if cmpop and
  616. not(nodetype in [equaln,unequaln]) then
  617. location_reset(location,LOC_JUMP,OS_NO);
  618. *)
  619. // location_reset(location,LOC_JUMP,OS_NO);
  620. // writeln('second_cmp64_exit');
  621. end;
  622. begin
  623. caddnode:=t68kaddnode;
  624. end.