n68kadd.pas 22 KB

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