n68kadd.pas 19 KB

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