n68kadd.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. location.resflags := getresflags(unsigned);
  223. { see if we can optimize into TST }
  224. if (right.location.loc=LOC_CONSTANT) and (right.location.value=0) and
  225. ((nodetype in [equaln,unequaln]) or (not unsigned)) then
  226. begin
  227. case left.location.loc of
  228. LOC_REFERENCE,
  229. LOC_CREFERENCE:
  230. begin
  231. href:=left.location.reference;
  232. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  233. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,opsize,href));
  234. location_freetemp(current_asmdata.CurrAsmList,left.location);
  235. end;
  236. else
  237. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  238. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,opsize,left.location.register));
  239. end;
  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
  255. { TODO: support LOC_REFERENCE }
  256. force_reg_left_right(true, true);
  257. if (right.location.loc = LOC_CONSTANT) then
  258. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  259. longint(right.location.value),left.location.register))
  260. else
  261. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  262. right.location.register,left.location.register));
  263. end;
  264. {*****************************************************************************
  265. 64-bit
  266. *****************************************************************************}
  267. procedure t68kaddnode.second_cmp64bit;
  268. var
  269. unsigned : boolean;
  270. href: treference;
  271. procedure firstjmp64bitcmp;
  272. var
  273. oldnodetype : tnodetype;
  274. begin
  275. case nodetype of
  276. ltn,gtn:
  277. begin
  278. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  279. { cheat a little bit for the negative test }
  280. toggleflag(nf_swapped);
  281. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  282. toggleflag(nf_swapped);
  283. end;
  284. lten,gten:
  285. begin
  286. oldnodetype:=nodetype;
  287. if nodetype=lten then
  288. nodetype:=ltn
  289. else
  290. nodetype:=gtn;
  291. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  292. { cheat for the negative test }
  293. if nodetype=ltn then
  294. nodetype:=gtn
  295. else
  296. nodetype:=ltn;
  297. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  298. nodetype:=oldnodetype;
  299. end;
  300. equaln:
  301. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  302. unequaln:
  303. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  304. end;
  305. end;
  306. procedure secondjmp64bitcmp;
  307. begin
  308. case nodetype of
  309. ltn,gtn,lten,gten:
  310. begin
  311. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  312. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  313. end;
  314. equaln:
  315. begin
  316. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  317. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  318. end;
  319. unequaln:
  320. begin
  321. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  322. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  323. end;
  324. end;
  325. end;
  326. begin
  327. { This puts constant operand (if any) to the right }
  328. pass_left_right;
  329. unsigned:=not(is_signed(left.resultdef)) or
  330. not(is_signed(right.resultdef));
  331. location_reset(location,LOC_JUMP,OS_NO);
  332. if (right.location.loc=LOC_CONSTANT) and (right.location.value64=0) and
  333. (nodetype in [equaln,unequaln]) then
  334. begin
  335. case left.location.loc of
  336. LOC_REFERENCE,
  337. LOC_CREFERENCE:
  338. begin
  339. href:=left.location.reference;
  340. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  341. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  342. firstjmp64bitcmp;
  343. inc(href.offset,4);
  344. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,S_L,href));
  345. secondjmp64bitcmp;
  346. location_freetemp(current_asmdata.CurrAsmList,left.location);
  347. end;
  348. else
  349. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  350. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reglo));
  351. firstjmp64bitcmp;
  352. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,left.location.register64.reghi));
  353. secondjmp64bitcmp;
  354. end;
  355. exit;
  356. end;
  357. { left and right no register? }
  358. { then one must be demanded }
  359. if not (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  360. begin
  361. if not (right.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  362. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true)
  363. else
  364. begin
  365. location_swap(left.location,right.location);
  366. toggleflag(nf_swapped);
  367. end;
  368. end;
  369. { left is now in register }
  370. case right.location.loc of
  371. LOC_REGISTER,LOC_CREGISTER:
  372. begin
  373. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reghi,left.location.register64.reghi));
  374. firstjmp64bitcmp;
  375. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reglo,left.location.register64.reglo));
  376. secondjmp64bitcmp;
  377. end;
  378. LOC_REFERENCE,LOC_CREFERENCE:
  379. begin
  380. href:=right.location.reference;
  381. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  382. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reghi));
  383. firstjmp64bitcmp;
  384. inc(href.offset,4);
  385. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_CMP,S_L,href,left.location.register64.reglo));
  386. secondjmp64bitcmp;
  387. location_freetemp(current_asmdata.CurrAsmList,right.location);
  388. end;
  389. LOC_CONSTANT:
  390. begin
  391. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(hi(right.location.value64)),left.location.register64.reghi));
  392. firstjmp64bitcmp;
  393. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,aint(lo(right.location.value64)),left.location.register64.reglo));
  394. secondjmp64bitcmp;
  395. end;
  396. else
  397. InternalError(2014072501);
  398. end;
  399. end;
  400. begin
  401. caddnode:=t68kaddnode;
  402. end.