n68kadd.pas 20 KB

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