n68kadd.pas 20 KB

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