n68kadd.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. useconst : boolean;
  208. tmpreg : tregister;
  209. opsize : topsize;
  210. cmpsize : tcgsize;
  211. href: treference;
  212. begin
  213. { determine if the comparison will be unsigned }
  214. unsigned:=not(is_signed(left.resultdef)) or
  215. not(is_signed(right.resultdef));
  216. { this puts constant operand (if any) to the right }
  217. pass_left_right;
  218. { tentatively assume left size (correct for possible TST/CMPI, will fix later) }
  219. cmpsize:=def_cgsize(left.resultdef);
  220. opsize:=tcgsize2opsize[cmpsize];
  221. { set result location }
  222. location_reset(location,LOC_FLAGS,OS_NO);
  223. location.resflags := getresflags(unsigned);
  224. { see if we can optimize into TST }
  225. if (right.location.loc=LOC_CONSTANT) and (right.location.value=0) and
  226. ((nodetype in [equaln,unequaln]) or (not unsigned)) then
  227. begin
  228. case left.location.loc of
  229. LOC_REFERENCE,
  230. LOC_CREFERENCE:
  231. begin
  232. href:=left.location.reference;
  233. tcg68k(cg).fixref(current_asmdata.CurrAsmList,href);
  234. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_TST,opsize,href));
  235. location_freetemp(current_asmdata.CurrAsmList,left.location);
  236. end;
  237. else
  238. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  239. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,opsize,left.location.register));
  240. end;
  241. exit;
  242. end;
  243. { ToDo : set "allowconstants" to True, but this seems to upset Coldfire
  244. a bit for the CMP instruction => check manual and implement
  245. exception accordingly below }
  246. { load values into registers (except constants) }
  247. force_reg_left_right(true, false);
  248. useconst := false;
  249. if tcgsize2size[right.location.size]=tcgsize2size[left.location.size] then
  250. cmpsize:=left.location.size
  251. else
  252. { ToDo : zero/sign extend??? }
  253. if tcgsize2size[right.location.size]<tcgsize2size[left.location.size] then
  254. cmpsize:=left.location.size
  255. else
  256. cmpsize:=right.location.size;
  257. opsize:=tcgsize2opsize[cmpsize];
  258. if opsize=S_NO then
  259. internalerror(2013090301);
  260. { Attention: The RIGHT(!) operand is substracted from and must be a
  261. register! }
  262. {if (right.location.loc = LOC_CONSTANT) then
  263. if useconst then
  264. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,opsize,
  265. longint(right.location.value),left.location.register))
  266. else
  267. begin
  268. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  269. tmpreg,left.location.register));
  270. end
  271. else}
  272. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,opsize,
  273. right.location.register,left.location.register));
  274. end;
  275. {*****************************************************************************
  276. 64-bit
  277. *****************************************************************************}
  278. procedure t68kaddnode.second_cmp64bit;
  279. var
  280. unsigned : boolean;
  281. procedure firstjmp64bitcmp;
  282. var
  283. oldnodetype : tnodetype;
  284. begin
  285. case nodetype of
  286. ltn,gtn:
  287. begin
  288. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  289. { cheat a little bit for the negative test }
  290. toggleflag(nf_swapped);
  291. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  292. toggleflag(nf_swapped);
  293. end;
  294. lten,gten:
  295. begin
  296. oldnodetype:=nodetype;
  297. if nodetype=lten then
  298. nodetype:=ltn
  299. else
  300. nodetype:=gtn;
  301. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  302. { cheat for the negative test }
  303. if nodetype=ltn then
  304. nodetype:=gtn
  305. else
  306. nodetype:=ltn;
  307. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  308. nodetype:=oldnodetype;
  309. end;
  310. equaln:
  311. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  312. unequaln:
  313. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  314. end;
  315. end;
  316. procedure secondjmp64bitcmp;
  317. begin
  318. case nodetype of
  319. ltn,gtn,lten,gten:
  320. begin
  321. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  322. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  323. end;
  324. equaln:
  325. begin
  326. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  327. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  328. end;
  329. unequaln:
  330. begin
  331. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  332. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  333. end;
  334. end;
  335. end;
  336. begin
  337. pass_left_right;
  338. force_reg_left_right(false,false);
  339. unsigned:=not(is_signed(left.resultdef)) or
  340. not(is_signed(right.resultdef));
  341. location_reset(location,LOC_JUMP,OS_NO);
  342. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reghi,left.location.register64.reghi));
  343. firstjmp64bitcmp;
  344. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,right.location.register64.reglo,left.location.register64.reglo));
  345. secondjmp64bitcmp;
  346. end;
  347. begin
  348. caddnode:=t68kaddnode;
  349. end.