n68kadd.pas 34 KB

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