n68kadd.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 getresflags(unsigned: boolean) : tresflags;
  26. protected
  27. procedure second_addfloat;override;
  28. procedure second_cmpfloat;override;
  29. procedure second_cmpordinal;override;
  30. procedure second_cmpsmallset;override;
  31. procedure second_cmp64bit;override;
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,
  36. cutils,verbose,globals,
  37. symconst,symdef,paramgr,symtype,
  38. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  39. cpuinfo,pass_1,pass_2,regvars,
  40. cpupara,cgutils,procinfo,
  41. ncon,nset,
  42. ncgutil,tgobj,rgobj,rgcpu,cgobj,cgcpu,hlcgobj,cg64f32;
  43. {*****************************************************************************
  44. Helpers
  45. *****************************************************************************}
  46. function t68kaddnode.getresflags(unsigned : boolean) : tresflags;
  47. begin
  48. case nodetype of
  49. equaln : getresflags:=F_E;
  50. unequaln : getresflags:=F_NE;
  51. else
  52. if not(unsigned) then
  53. begin
  54. if nf_swapped in flags then
  55. case nodetype of
  56. ltn : getresflags:=F_G;
  57. lten : getresflags:=F_GE;
  58. gtn : getresflags:=F_L;
  59. gten : getresflags:=F_LE;
  60. else
  61. internalerror(2014082030);
  62. end
  63. else
  64. case nodetype of
  65. ltn : getresflags:=F_L;
  66. lten : getresflags:=F_LE;
  67. gtn : getresflags:=F_G;
  68. gten : getresflags:=F_GE;
  69. else
  70. internalerror(2014082031);
  71. end;
  72. end
  73. else
  74. begin
  75. if nf_swapped in flags then
  76. case nodetype of
  77. ltn : getresflags:=F_A;
  78. lten : getresflags:=F_AE;
  79. gtn : getresflags:=F_B;
  80. gten : getresflags:=F_BE;
  81. else
  82. internalerror(2014082032);
  83. end
  84. else
  85. case nodetype of
  86. ltn : getresflags:=F_B;
  87. lten : getresflags:=F_BE;
  88. gtn : getresflags:=F_A;
  89. gten : getresflags:=F_AE;
  90. else
  91. internalerror(2014082033);
  92. end;
  93. end;
  94. end;
  95. end;
  96. {*****************************************************************************
  97. AddFloat
  98. *****************************************************************************}
  99. procedure t68kaddnode.second_addfloat;
  100. var
  101. op : TAsmOp;
  102. href : TReference;
  103. begin
  104. pass_left_right;
  105. case nodetype of
  106. addn :
  107. op:=A_FADD;
  108. muln :
  109. op:=A_FMUL;
  110. subn :
  111. op:=A_FSUB;
  112. slashn :
  113. op:=A_FDIV;
  114. else
  115. internalerror(200403182);
  116. end;
  117. // get the operands in the correct order, there are no special cases
  118. // here, everything is register-based
  119. if nf_swapped in flags then
  120. swapleftright;
  121. case current_settings.fputype of
  122. fpu_68881:
  123. begin
  124. { have left in the register, right can be a memory location }
  125. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  126. { initialize the result }
  127. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  128. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  129. { emit the actual operation }
  130. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmlist,OS_NO,OS_NO,left.location.register,location.register);
  131. case right.location.loc of
  132. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  133. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_FX,right.location.register,location.register));
  134. LOC_REFERENCE,LOC_CREFERENCE:
  135. begin
  136. href:=right.location.reference;
  137. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  138. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,tcgsize2opsize[right.location.size],href,location.register));
  139. end
  140. else
  141. internalerror(2015021501);
  142. end;
  143. end;
  144. else
  145. // softfpu should be handled in pass1, others are not yet supported...
  146. internalerror(2015010201);
  147. end;
  148. end;
  149. procedure t68kaddnode.second_cmpfloat;
  150. var
  151. tmpreg : tregister;
  152. ai: taicpu;
  153. href : TReference;
  154. begin
  155. pass_left_right;
  156. if (nf_swapped in flags) then
  157. swapleftright;
  158. case current_settings.fputype of
  159. fpu_68881:
  160. begin
  161. { force left fpureg as register, right can be reference }
  162. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  163. { emit compare }
  164. case right.location.loc of
  165. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  166. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCMP,S_FX,right.location.register,left.location.register));
  167. LOC_REFERENCE,LOC_CREFERENCE:
  168. begin
  169. href:=right.location.reference;
  170. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  171. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_FCMP,tcgsize2opsize[right.location.size],href,left.location.register));
  172. end
  173. else
  174. internalerror(2015021502);
  175. end;
  176. // temporary(?) hack, move condition result back to the CPU from the FPU.
  177. // 6888x has its own FBcc branch instructions and FScc flags->reg instruction,
  178. // which we don't support yet in the rest of the cg. (KB)
  179. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  180. ai:=taicpu.op_reg(A_FSxx,S_B,tmpreg);
  181. ai.SetCondition(flags_to_cond(getresflags(false)));
  182. current_asmdata.CurrAsmList.concat(ai);
  183. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_NEG,S_B,tmpreg));
  184. location_reset(location,LOC_REGISTER,OS_8);
  185. location.register:=tmpreg;
  186. end;
  187. else
  188. // softfpu should be handled in pass1, others are not yet supported...
  189. internalerror(2015010201);
  190. end;
  191. end;
  192. {*****************************************************************************
  193. Smallsets
  194. *****************************************************************************}
  195. procedure t68kaddnode.second_cmpsmallset;
  196. var
  197. tmpreg : tregister;
  198. begin
  199. pass_left_right;
  200. location_reset(location,LOC_FLAGS,OS_NO);
  201. if (not(nf_swapped in flags) and
  202. (nodetype = lten)) or
  203. ((nf_swapped in flags) and
  204. (nodetype = gten)) then
  205. swapleftright;
  206. { Try to keep right as a constant }
  207. if right.location.loc<>LOC_CONSTANT then
  208. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  209. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  210. case nodetype of
  211. equaln,
  212. unequaln:
  213. begin
  214. if right.location.loc=LOC_CONSTANT then
  215. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,right.location.value,left.location.register))
  216. else
  217. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register,left.location.register));
  218. if nodetype=equaln then
  219. location.resflags:=F_E
  220. else
  221. location.resflags:=F_NE;
  222. end;
  223. lten,
  224. gten:
  225. begin
  226. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  227. if right.location.loc=LOC_CONSTANT then
  228. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  229. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,left.location.register,right.location.register,tmpreg);
  230. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,right.location.register));
  231. location.resflags:=F_E;
  232. end;
  233. else
  234. internalerror(2013092701);
  235. end;
  236. end;
  237. {*****************************************************************************
  238. Ordinals
  239. *****************************************************************************}
  240. procedure t68kaddnode.second_cmpordinal;
  241. var
  242. unsigned : boolean;
  243. tmpreg : tregister;
  244. opsize : topsize;
  245. cmpsize : tcgsize;
  246. href: treference;
  247. begin
  248. { determine if the comparison will be unsigned }
  249. unsigned:=not(is_signed(left.resultdef)) or
  250. not(is_signed(right.resultdef));
  251. { this puts constant operand (if any) to the right }
  252. pass_left_right;
  253. { tentatively assume left size (correct for possible TST, will fix later) }
  254. cmpsize:=def_cgsize(left.resultdef);
  255. opsize:=tcgsize2opsize[cmpsize];
  256. { set result location }
  257. location_reset(location,LOC_FLAGS,OS_NO);
  258. { see if we can optimize into TST }
  259. if (right.location.loc=LOC_CONSTANT) and (right.location.value=0) then
  260. begin
  261. { Unsigned <0 or >=0 should not reach pass2, most likely }
  262. case left.location.loc of
  263. LOC_REFERENCE,
  264. LOC_CREFERENCE:
  265. begin
  266. href:=left.location.reference;
  267. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  268. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,opsize,href));
  269. location_freetemp(current_asmdata.CurrAsmList,left.location);
  270. end;
  271. else
  272. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  273. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,opsize,left.location.register));
  274. end;
  275. location.resflags := getresflags(unsigned);
  276. exit;
  277. end;
  278. { Coldfire supports byte/word compares only starting with ISA_B,
  279. !!see remark about Qemu weirdness in tcg68k.a_cmp_const_reg_label }
  280. if (opsize<>S_L) and (current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]}) then
  281. begin
  282. { 1) Extension is needed for LOC_REFERENCE, but what about LOC_REGISTER ? Perhaps after fixing cg we can assume
  283. that high bits of registers are correct.
  284. 2) Assuming that extension depends only on source signedness --> destination OS_32 is acceptable. }
  285. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(OS_32),false);
  286. if (right.location.loc<>LOC_CONSTANT) then
  287. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(OS_32),false);
  288. opsize:=S_L;
  289. end
  290. else if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  291. begin
  292. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  293. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  294. else
  295. begin
  296. location_swap(left.location,right.location);
  297. toggleflag(nf_swapped);
  298. end;
  299. end;
  300. { left is now in register }
  301. case right.location.loc of
  302. LOC_CONSTANT:
  303. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  304. longint(right.location.value),left.location.register));
  305. LOC_REFERENCE,
  306. LOC_CREFERENCE:
  307. begin
  308. href:=right.location.reference;
  309. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  310. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,opsize,href,
  311. left.location.register));
  312. end;
  313. LOC_REGISTER,
  314. LOC_CREGISTER:
  315. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  316. right.location.register,left.location.register));
  317. else
  318. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  319. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  320. right.location.register,left.location.register));
  321. end;
  322. { update location because sides could have been swapped }
  323. location.resflags:=getresflags(unsigned);
  324. end;
  325. {*****************************************************************************
  326. 64-bit
  327. *****************************************************************************}
  328. procedure t68kaddnode.second_cmp64bit;
  329. var
  330. truelabel,
  331. falselabel: tasmlabel;
  332. hlab: tasmlabel;
  333. unsigned : boolean;
  334. href: treference;
  335. procedure firstjmp64bitcmp;
  336. var
  337. oldnodetype : tnodetype;
  338. begin
  339. case nodetype of
  340. ltn,gtn:
  341. begin
  342. if (hlab<>location.truelabel) then
  343. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  344. { cheat a little bit for the negative test }
  345. toggleflag(nf_swapped);
  346. if (hlab<>location.falselabel) then
  347. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  348. toggleflag(nf_swapped);
  349. end;
  350. lten,gten:
  351. begin
  352. oldnodetype:=nodetype;
  353. if nodetype=lten then
  354. nodetype:=ltn
  355. else
  356. nodetype:=gtn;
  357. if (hlab<>location.truelabel) then
  358. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.truelabel);
  359. { cheat for the negative test }
  360. if nodetype=ltn then
  361. nodetype:=gtn
  362. else
  363. nodetype:=ltn;
  364. if (hlab<>location.falselabel) then
  365. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),location.falselabel);
  366. nodetype:=oldnodetype;
  367. end;
  368. equaln:
  369. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  370. unequaln:
  371. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  372. end;
  373. end;
  374. procedure secondjmp64bitcmp;
  375. begin
  376. case nodetype of
  377. ltn,gtn,lten,gten:
  378. begin
  379. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),location.truelabel);
  380. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  381. end;
  382. equaln:
  383. begin
  384. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.falselabel);
  385. cg.a_jmp_always(current_asmdata.CurrAsmList,location.truelabel);
  386. end;
  387. unequaln:
  388. begin
  389. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,location.truelabel);
  390. cg.a_jmp_always(current_asmdata.CurrAsmList,location.falselabel);
  391. end;
  392. end;
  393. end;
  394. begin
  395. truelabel:=nil;
  396. falselabel:=nil;
  397. { This puts constant operand (if any) to the right }
  398. pass_left_right;
  399. unsigned:=not(is_signed(left.resultdef)) or
  400. not(is_signed(right.resultdef));
  401. current_asmdata.getjumplabel(truelabel);
  402. current_asmdata.getjumplabel(falselabel);
  403. location_reset_jump(location,truelabel,falselabel);
  404. { Relational compares against constants having low dword=0 can omit the
  405. second compare based on the fact that any unsigned value is >=0 }
  406. hlab:=nil;
  407. if (right.location.loc=LOC_CONSTANT) and
  408. (lo(right.location.value64)=0) then
  409. begin
  410. case getresflags(true) of
  411. F_AE: hlab:=location.truelabel;
  412. F_B: hlab:=location.falselabel;
  413. end;
  414. end;
  415. if (right.location.loc=LOC_CONSTANT) and (right.location.value64=0) and
  416. (nodetype in [equaln,unequaln]) then
  417. begin
  418. case left.location.loc of
  419. LOC_REFERENCE,
  420. LOC_CREFERENCE:
  421. begin
  422. href:=left.location.reference;
  423. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  424. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  425. firstjmp64bitcmp;
  426. inc(href.offset,4);
  427. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  428. secondjmp64bitcmp;
  429. location_freetemp(current_asmdata.CurrAsmList,left.location);
  430. end;
  431. else
  432. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  433. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reglo));
  434. firstjmp64bitcmp;
  435. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reghi));
  436. secondjmp64bitcmp;
  437. end;
  438. exit;
  439. end;
  440. { left and right no register? }
  441. { then one must be demanded }
  442. if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  443. begin
  444. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  445. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  446. else
  447. begin
  448. location_swap(left.location,right.location);
  449. toggleflag(nf_swapped);
  450. end;
  451. end;
  452. { left is now in register }
  453. case right.location.loc of
  454. LOC_REGISTER,LOC_CREGISTER:
  455. begin
  456. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reghi,left.location.register64.reghi));
  457. firstjmp64bitcmp;
  458. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reglo,left.location.register64.reglo));
  459. secondjmp64bitcmp;
  460. end;
  461. LOC_REFERENCE,LOC_CREFERENCE:
  462. begin
  463. href:=right.location.reference;
  464. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  465. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reghi));
  466. firstjmp64bitcmp;
  467. inc(href.offset,4);
  468. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reglo));
  469. secondjmp64bitcmp;
  470. location_freetemp(current_asmdata.CurrAsmList,right.location);
  471. end;
  472. LOC_CONSTANT:
  473. begin
  474. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(hi(right.location.value64)),left.location.register64.reghi));
  475. firstjmp64bitcmp;
  476. if assigned(hlab) then
  477. cg.a_jmp_always(current_asmdata.CurrAsmList,hlab)
  478. else
  479. begin
  480. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(lo(right.location.value64)),left.location.register64.reglo));
  481. secondjmp64bitcmp;
  482. end;
  483. end;
  484. else
  485. InternalError(2014072501);
  486. end;
  487. end;
  488. begin
  489. caddnode:=t68kaddnode;
  490. end.