n68kadd.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. begin
  103. pass_left_right;
  104. case nodetype of
  105. addn :
  106. op:=A_FADD;
  107. muln :
  108. op:=A_FMUL;
  109. subn :
  110. op:=A_FSUB;
  111. slashn :
  112. op:=A_FDIV;
  113. else
  114. internalerror(200403182);
  115. end;
  116. // get the operands in the correct order, there are no special cases
  117. // here, everything is register-based
  118. if nf_swapped in flags then
  119. swapleftright;
  120. case current_settings.fputype of
  121. fpu_68881:
  122. begin
  123. // put both operands in a register
  124. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  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. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FMOVE,S_FX,left.location.register,location.register));
  131. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_FX,right.location.register,location.register));
  132. end;
  133. else
  134. // softfpu should be handled in pass1, others are not yet supported...
  135. internalerror(2015010201);
  136. end;
  137. end;
  138. procedure t68kaddnode.second_cmpfloat;
  139. var
  140. tmpreg : tregister;
  141. ai: taicpu;
  142. begin
  143. pass_left_right;
  144. if (nf_swapped in flags) then
  145. swapleftright;
  146. case current_settings.fputype of
  147. fpu_68881:
  148. begin
  149. location_reset(location,LOC_FLAGS,OS_NO);
  150. { force fpureg as location, left right doesn't matter
  151. as both will be in a fpureg }
  152. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  153. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  154. // emit compare
  155. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCMP,S_FX,right.location.register,left.location.register));
  156. location.resflags:=getresflags(false);
  157. // temporary(?) hack, move condition result back to the CPU from the FPU.
  158. // 6888x has its own FBcc branch instructions and FScc flags->reg instruction,
  159. // which we don't support yet in the rest of the cg. (KB)
  160. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  161. ai:=taicpu.op_reg(A_FSxx,S_B,tmpreg);
  162. ai.SetCondition(flags_to_cond(location.resflags));
  163. current_asmdata.CurrAsmList.concat(ai);
  164. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_B,tmpreg));
  165. location.resflags:=F_E;
  166. end;
  167. else
  168. // softfpu should be handled in pass1, others are not yet supported...
  169. internalerror(2015010201);
  170. end;
  171. end;
  172. {*****************************************************************************
  173. Smallsets
  174. *****************************************************************************}
  175. procedure t68kaddnode.second_cmpsmallset;
  176. var
  177. tmpreg : tregister;
  178. begin
  179. pass_left_right;
  180. location_reset(location,LOC_FLAGS,OS_NO);
  181. if (not(nf_swapped in flags) and
  182. (nodetype = lten)) or
  183. ((nf_swapped in flags) and
  184. (nodetype = gten)) then
  185. swapleftright;
  186. { Try to keep right as a constant }
  187. if right.location.loc<>LOC_CONSTANT then
  188. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  189. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  190. case nodetype of
  191. equaln,
  192. unequaln:
  193. begin
  194. if right.location.loc=LOC_CONSTANT then
  195. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,right.location.value,left.location.register))
  196. else
  197. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register,left.location.register));
  198. if nodetype=equaln then
  199. location.resflags:=F_E
  200. else
  201. location.resflags:=F_NE;
  202. end;
  203. lten,
  204. gten:
  205. begin
  206. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  207. if right.location.loc=LOC_CONSTANT then
  208. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,false);
  209. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_32,left.location.register,right.location.register,tmpreg);
  210. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,tmpreg,right.location.register));
  211. location.resflags:=F_E;
  212. end;
  213. else
  214. internalerror(2013092701);
  215. end;
  216. end;
  217. {*****************************************************************************
  218. Ordinals
  219. *****************************************************************************}
  220. procedure t68kaddnode.second_cmpordinal;
  221. var
  222. unsigned : boolean;
  223. tmpreg : tregister;
  224. opsize : topsize;
  225. cmpsize : tcgsize;
  226. href: treference;
  227. begin
  228. { determine if the comparison will be unsigned }
  229. unsigned:=not(is_signed(left.resultdef)) or
  230. not(is_signed(right.resultdef));
  231. { this puts constant operand (if any) to the right }
  232. pass_left_right;
  233. { tentatively assume left size (correct for possible TST, will fix later) }
  234. cmpsize:=def_cgsize(left.resultdef);
  235. opsize:=tcgsize2opsize[cmpsize];
  236. { set result location }
  237. location_reset(location,LOC_FLAGS,OS_NO);
  238. { see if we can optimize into TST }
  239. if (right.location.loc=LOC_CONSTANT) and (right.location.value=0) then
  240. begin
  241. { Unsigned <0 or >=0 should not reach pass2, most likely }
  242. case left.location.loc of
  243. LOC_REFERENCE,
  244. LOC_CREFERENCE:
  245. begin
  246. href:=left.location.reference;
  247. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  248. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,opsize,href));
  249. location_freetemp(current_asmdata.CurrAsmList,left.location);
  250. end;
  251. else
  252. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  253. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,opsize,left.location.register));
  254. end;
  255. location.resflags := getresflags(unsigned);
  256. exit;
  257. end;
  258. { Coldfire supports byte/word compares only starting with ISA_B,
  259. !!see remark about Qemu weirdness in tcg68k.a_cmp_const_reg_label }
  260. if (opsize<>S_L) and (current_settings.cputype in cpu_coldfire{-[cpu_isa_b,cpu_isa_c]}) then
  261. begin
  262. { 1) Extension is needed for LOC_REFERENCE, but what about LOC_REGISTER ? Perhaps after fixing cg we can assume
  263. that high bits of registers are correct.
  264. 2) Assuming that extension depends only on source signedness --> destination OS_32 is acceptable. }
  265. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(OS_32),false);
  266. if (right.location.loc<>LOC_CONSTANT) then
  267. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,cgsize_orddef(OS_32),false);
  268. opsize:=S_L;
  269. end
  270. else if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  271. begin
  272. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  273. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  274. else
  275. begin
  276. location_swap(left.location,right.location);
  277. toggleflag(nf_swapped);
  278. end;
  279. end;
  280. { left is now in register }
  281. case right.location.loc of
  282. LOC_CONSTANT:
  283. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  284. longint(right.location.value),left.location.register));
  285. LOC_REFERENCE,
  286. LOC_CREFERENCE:
  287. begin
  288. href:=right.location.reference;
  289. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  290. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,opsize,href,
  291. left.location.register));
  292. end;
  293. LOC_REGISTER,
  294. LOC_CREGISTER:
  295. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  296. right.location.register,left.location.register));
  297. else
  298. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  299. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  300. right.location.register,left.location.register));
  301. end;
  302. { update location because sides could have been swapped }
  303. location.resflags:=getresflags(unsigned);
  304. end;
  305. {*****************************************************************************
  306. 64-bit
  307. *****************************************************************************}
  308. procedure t68kaddnode.second_cmp64bit;
  309. var
  310. hlab: tasmlabel;
  311. unsigned : boolean;
  312. href: treference;
  313. procedure firstjmp64bitcmp;
  314. var
  315. oldnodetype : tnodetype;
  316. begin
  317. case nodetype of
  318. ltn,gtn:
  319. begin
  320. if (hlab<>current_procinfo.CurrTrueLabel) then
  321. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  322. { cheat a little bit for the negative test }
  323. toggleflag(nf_swapped);
  324. if (hlab<>current_procinfo.CurrFalseLabel) then
  325. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  326. toggleflag(nf_swapped);
  327. end;
  328. lten,gten:
  329. begin
  330. oldnodetype:=nodetype;
  331. if nodetype=lten then
  332. nodetype:=ltn
  333. else
  334. nodetype:=gtn;
  335. if (hlab<>current_procinfo.CurrTrueLabel) then
  336. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  337. { cheat for the negative test }
  338. if nodetype=ltn then
  339. nodetype:=gtn
  340. else
  341. nodetype:=ltn;
  342. if (hlab<>current_procinfo.CurrFalseLabel) then
  343. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  344. nodetype:=oldnodetype;
  345. end;
  346. equaln:
  347. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  348. unequaln:
  349. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  350. end;
  351. end;
  352. procedure secondjmp64bitcmp;
  353. begin
  354. case nodetype of
  355. ltn,gtn,lten,gten:
  356. begin
  357. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  358. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  359. end;
  360. equaln:
  361. begin
  362. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  363. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  364. end;
  365. unequaln:
  366. begin
  367. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  368. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  369. end;
  370. end;
  371. end;
  372. begin
  373. { This puts constant operand (if any) to the right }
  374. pass_left_right;
  375. unsigned:=not(is_signed(left.resultdef)) or
  376. not(is_signed(right.resultdef));
  377. location_reset(location,LOC_JUMP,OS_NO);
  378. { Relational compares against constants having low dword=0 can omit the
  379. second compare based on the fact that any unsigned value is >=0 }
  380. hlab:=nil;
  381. if (right.location.loc=LOC_CONSTANT) and
  382. (lo(right.location.value64)=0) then
  383. begin
  384. case getresflags(true) of
  385. F_AE: hlab:=current_procinfo.CurrTrueLabel;
  386. F_B: hlab:=current_procinfo.CurrFalseLabel;
  387. end;
  388. end;
  389. if (right.location.loc=LOC_CONSTANT) and (right.location.value64=0) and
  390. (nodetype in [equaln,unequaln]) then
  391. begin
  392. case left.location.loc of
  393. LOC_REFERENCE,
  394. LOC_CREFERENCE:
  395. begin
  396. href:=left.location.reference;
  397. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  398. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  399. firstjmp64bitcmp;
  400. inc(href.offset,4);
  401. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  402. secondjmp64bitcmp;
  403. location_freetemp(current_asmdata.CurrAsmList,left.location);
  404. end;
  405. else
  406. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  407. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reglo));
  408. firstjmp64bitcmp;
  409. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reghi));
  410. secondjmp64bitcmp;
  411. end;
  412. exit;
  413. end;
  414. { left and right no register? }
  415. { then one must be demanded }
  416. if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  417. begin
  418. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  419. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  420. else
  421. begin
  422. location_swap(left.location,right.location);
  423. toggleflag(nf_swapped);
  424. end;
  425. end;
  426. { left is now in register }
  427. case right.location.loc of
  428. LOC_REGISTER,LOC_CREGISTER:
  429. begin
  430. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reghi,left.location.register64.reghi));
  431. firstjmp64bitcmp;
  432. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reglo,left.location.register64.reglo));
  433. secondjmp64bitcmp;
  434. end;
  435. LOC_REFERENCE,LOC_CREFERENCE:
  436. begin
  437. href:=right.location.reference;
  438. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  439. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reghi));
  440. firstjmp64bitcmp;
  441. inc(href.offset,4);
  442. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reglo));
  443. secondjmp64bitcmp;
  444. location_freetemp(current_asmdata.CurrAsmList,right.location);
  445. end;
  446. LOC_CONSTANT:
  447. begin
  448. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(hi(right.location.value64)),left.location.register64.reghi));
  449. firstjmp64bitcmp;
  450. if assigned(hlab) then
  451. cg.a_jmp_always(current_asmdata.CurrAsmList,hlab)
  452. else
  453. begin
  454. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(lo(right.location.value64)),left.location.register64.reglo));
  455. secondjmp64bitcmp;
  456. end;
  457. end;
  458. else
  459. InternalError(2014072501);
  460. end;
  461. end;
  462. begin
  463. caddnode:=t68kaddnode;
  464. end.