n68kadd.pas 20 KB

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