n68kadd.pas 33 KB

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