ncpuadd.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl and Jonas Maebe
  4. Code generation for add nodes on the Motorola 680x0 family
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncpuadd;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nadd,ncgadd,cpubase;
  23. type
  24. t68kaddnode = class(tcgaddnode)
  25. procedure second_cmpordinal;override;
  26. procedure second_cmpsmallset;override;
  27. procedure second_cmp64bit;override;
  28. procedure second_cmpboolean;override;
  29. private
  30. function getresflags(unsigned: boolean) : tresflags;
  31. end;
  32. implementation
  33. uses
  34. globtype,systems,
  35. cutils,verbose,globals,
  36. symconst,symdef,paramgr,
  37. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  38. cgbase,cpuinfo,pass_1,pass_2,regvars,
  39. cpupara,
  40. ncon,nset,
  41. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  42. {*****************************************************************************
  43. Helpers
  44. *****************************************************************************}
  45. function t68kaddnode.getresflags(unsigned : boolean) : tresflags;
  46. begin
  47. case nodetype of
  48. equaln : getresflags:=F_E;
  49. unequaln : getresflags:=F_NE;
  50. else
  51. if not(unsigned) then
  52. begin
  53. if nf_swaped in flags then
  54. case nodetype of
  55. ltn : getresflags:=F_G;
  56. lten : getresflags:=F_GE;
  57. gtn : getresflags:=F_L;
  58. gten : getresflags:=F_LE;
  59. end
  60. else
  61. case nodetype of
  62. ltn : getresflags:=F_L;
  63. lten : getresflags:=F_LE;
  64. gtn : getresflags:=F_G;
  65. gten : getresflags:=F_GE;
  66. end;
  67. end
  68. else
  69. begin
  70. if nf_swaped in flags then
  71. case nodetype of
  72. ltn : getresflags:=F_A;
  73. lten : getresflags:=F_AE;
  74. gtn : getresflags:=F_B;
  75. gten : getresflags:=F_BE;
  76. end
  77. else
  78. case nodetype of
  79. ltn : getresflags:=F_B;
  80. lten : getresflags:=F_BE;
  81. gtn : getresflags:=F_A;
  82. gten : getresflags:=F_AE;
  83. end;
  84. end;
  85. end;
  86. end;
  87. {*****************************************************************************
  88. Smallsets
  89. *****************************************************************************}
  90. procedure t68kaddnode.second_cmpsmallset;
  91. var
  92. tmpreg : tregister;
  93. begin
  94. location_reset(location,LOC_FLAGS,OS_NO);
  95. case nodetype of
  96. equaln,
  97. unequaln :
  98. begin
  99. {emit_compare(true);}
  100. end;
  101. lten,gten:
  102. begin
  103. If (not(nf_swaped in flags) and
  104. (nodetype = lten)) or
  105. ((nf_swaped in flags) and
  106. (nodetype = gten)) then
  107. swapleftright;
  108. // now we have to check whether left >= right
  109. tmpreg := cg.getintregister(exprasmlist,OS_INT);
  110. if left.location.loc = LOC_CONSTANT then
  111. begin
  112. cg.a_op_const_reg_reg(exprasmlist,OP_AND,OS_INT,
  113. not(left.location.value),right.location.register,tmpreg);
  114. exprasmlist.concat(taicpu.op_reg(A_TST,S_L,tmpreg));
  115. // the two instructions above should be folded together by
  116. // the peepholeoptimizer
  117. end
  118. else
  119. begin
  120. if right.location.loc = LOC_CONSTANT then
  121. begin
  122. cg.a_load_const_reg(exprasmlist,OS_INT,
  123. aword(right.location.value),tmpreg);
  124. exprasmlist.concat(taicpu.op_reg_reg(A_AND,S_L,
  125. tmpreg,left.location.register));
  126. end
  127. else
  128. exprasmlist.concat(taicpu.op_reg_reg(A_AND,S_L,
  129. right.location.register,left.location.register));
  130. end;
  131. cg.ungetregister(exprasmlist,tmpreg);
  132. location.resflags := getresflags(true);
  133. end;
  134. else
  135. internalerror(2002072701);
  136. end;
  137. end;
  138. {*****************************************************************************
  139. Ordinals
  140. *****************************************************************************}
  141. procedure t68kaddnode.second_cmpordinal;
  142. var
  143. unsigned : boolean;
  144. useconst : boolean;
  145. tmpreg : tregister;
  146. op : tasmop;
  147. begin
  148. { set result location }
  149. location_reset(location,LOC_JUMP,OS_NO);
  150. { load values into registers (except constants) }
  151. force_reg_left_right(true, false);
  152. { determine if the comparison will be unsigned }
  153. unsigned:=not(is_signed(left.resulttype.def)) or
  154. not(is_signed(right.resulttype.def));
  155. // get the constant on the right if there is one
  156. if (left.location.loc = LOC_CONSTANT) then
  157. swapleftright;
  158. // can we use an immediate, or do we have to load the
  159. // constant in a register first?
  160. if (right.location.loc = LOC_CONSTANT) then
  161. begin
  162. {$ifdef extdebug}
  163. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.valueqword)<>0) and ((hi(right.location.valueqword)<>-1) or unsigned) then
  164. internalerror(2002080301);
  165. {$endif extdebug}
  166. if (nodetype in [equaln,unequaln]) then
  167. if (unsigned and
  168. (right.location.value > high(word))) or
  169. (not unsigned and
  170. (longint(right.location.value) < low(smallint)) or
  171. (longint(right.location.value) > high(smallint))) then
  172. { we can then maybe use a constant in the 'othersigned' case
  173. (the sign doesn't matter for // equal/unequal)}
  174. unsigned := not unsigned;
  175. if (unsigned and
  176. ((right.location.value) <= high(word))) or
  177. (not(unsigned) and
  178. (longint(right.location.value) >= low(smallint)) and
  179. (longint(right.location.value) <= high(smallint))) then
  180. useconst := true
  181. else
  182. begin
  183. useconst := false;
  184. tmpreg := cg.getintregister(exprasmlist,OS_INT);
  185. cg.a_load_const_reg(exprasmlist,OS_INT,
  186. aword(right.location.value),tmpreg);
  187. end
  188. end
  189. else
  190. useconst := false;
  191. location.loc := LOC_FLAGS;
  192. location.resflags := getresflags(unsigned);
  193. op := A_CMP;
  194. if (right.location.loc = LOC_CONSTANT) then
  195. if useconst then
  196. exprasmlist.concat(taicpu.op_reg_const(op,S_L,
  197. left.location.register,longint(right.location.value)))
  198. else
  199. begin
  200. exprasmlist.concat(taicpu.op_reg_reg(op,S_L,
  201. left.location.register,tmpreg));
  202. cg.ungetregister(exprasmlist,tmpreg);
  203. end
  204. else
  205. exprasmlist.concat(taicpu.op_reg_reg(op,S_L,
  206. left.location.register,right.location.register));
  207. end;
  208. {*****************************************************************************
  209. Boolean
  210. *****************************************************************************}
  211. procedure t68kaddnode.second_cmpboolean;
  212. var
  213. cgop : TOpCg;
  214. cgsize : TCgSize;
  215. isjump : boolean;
  216. otl,ofl : tasmlabel;
  217. begin
  218. if (torddef(left.resulttype.def).typ=bool8bit) or
  219. (torddef(right.resulttype.def).typ=bool8bit) then
  220. cgsize:=OS_8
  221. else
  222. if (torddef(left.resulttype.def).typ=bool16bit) or
  223. (torddef(right.resulttype.def).typ=bool16bit) then
  224. cgsize:=OS_16
  225. else
  226. cgsize:=OS_32;
  227. if (cs_full_boolean_eval in aktlocalswitches) or
  228. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  229. begin
  230. if left.nodetype in [ordconstn,realconstn] then
  231. swapleftright;
  232. isjump:=(left.location.loc=LOC_JUMP);
  233. if isjump then
  234. begin
  235. otl:=truelabel;
  236. objectlibrary.getlabel(truelabel);
  237. ofl:=falselabel;
  238. objectlibrary.getlabel(falselabel);
  239. end;
  240. secondpass(left);
  241. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  242. location_force_reg(exprasmlist,left.location,cgsize,false);
  243. if isjump then
  244. begin
  245. truelabel:=otl;
  246. falselabel:=ofl;
  247. end;
  248. isjump:=(right.location.loc=LOC_JUMP);
  249. if isjump then
  250. begin
  251. otl:=truelabel;
  252. objectlibrary.getlabel(truelabel);
  253. ofl:=falselabel;
  254. objectlibrary.getlabel(falselabel);
  255. end;
  256. secondpass(right);
  257. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  258. location_force_reg(exprasmlist,right.location,cgsize,false);
  259. if isjump then
  260. begin
  261. truelabel:=otl;
  262. falselabel:=ofl;
  263. end;
  264. location_reset(location,LOC_FLAGS,OS_NO);
  265. force_reg_left_right(true,false);
  266. if (left.location.loc = LOC_CONSTANT) then
  267. swapleftright;
  268. if (right.location.loc <> LOC_CONSTANT) then
  269. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,
  270. left.location.register,right.location.register))
  271. else
  272. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,
  273. longint(right.location.value),left.location.register));
  274. location.resflags := getresflags(true);
  275. end;
  276. release_reg_left_right;
  277. end;
  278. {*****************************************************************************
  279. 64-bit
  280. *****************************************************************************}
  281. procedure t68kaddnode.second_cmp64bit;
  282. begin
  283. (* load_left_right(true,false);
  284. case nodetype of
  285. ltn,lten,
  286. gtn,gten:
  287. begin
  288. emit_cmp64_hi;
  289. firstjmp64bitcmp;
  290. emit_cmp64_lo;
  291. secondjmp64bitcmp;
  292. end;
  293. equaln,unequaln:
  294. begin
  295. // instead of doing a complicated compare, do
  296. // (left.hi xor right.hi) or (left.lo xor right.lo)
  297. // (somewhate optimized so that no superfluous 'mr's are
  298. // generated)
  299. if (left.location.loc = LOC_CONSTANT) then
  300. swapleftright;
  301. if (right.location.loc = LOC_CONSTANT) then
  302. begin
  303. if left.location.loc = LOC_REGISTER then
  304. begin
  305. tempreg64.reglo := left.location.registerlow;
  306. tempreg64.reghi := left.location.registerhigh;
  307. end
  308. else
  309. begin
  310. if (aword(right.location.valueqword) <> 0) then
  311. tempreg64.reglo := cg.getintregister(exprasmlist)
  312. else
  313. tempreg64.reglo := left.location.registerlow;
  314. if ((right.location.valueqword shr 32) <> 0) then
  315. tempreg64.reghi := cg.getintregister(exprasmlist)
  316. else
  317. tempreg64.reghi := left.location.registerhigh;
  318. end;
  319. if (aword(right.location.valueqword) <> 0) then
  320. { negative values can be handled using SUB, }
  321. { positive values < 65535 using XOR. }
  322. if (longint(right.location.valueqword) >= -32767) and
  323. (longint(right.location.valueqword) < 0) then
  324. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  325. aword(right.location.valueqword),
  326. left.location.registerlow,tempreg64.reglo)
  327. else
  328. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  329. aword(right.location.valueqword),
  330. left.location.registerlow,tempreg64.reglo);
  331. if ((right.location.valueqword shr 32) <> 0) then
  332. if (longint(right.location.valueqword shr 32) >= -32767) and
  333. (longint(right.location.valueqword shr 32) < 0) then
  334. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  335. aword(right.location.valueqword shr 32),
  336. left.location.registerhigh,tempreg64.reghi)
  337. else
  338. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  339. aword(right.location.valueqword shr 32),
  340. left.location.registerhigh,tempreg64.reghi);
  341. end
  342. else
  343. begin
  344. tempreg64.reglo := cg.getintregister(exprasmlist);
  345. tempreg64.reghi := cg.getintregister(exprasmlist);
  346. cg64.a_op64_reg_reg_reg(exprasmlist,OP_XOR,
  347. left.location.register64,right.location.register64,
  348. tempreg64);
  349. end;
  350. cg.a_reg_alloc(exprasmlist,R_0);
  351. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  352. tempreg64.reglo,tempreg64.reghi));
  353. cg.a_reg_dealloc(exprasmlist,R_0);
  354. if (tempreg64.reglo <> left.location.registerlow) then
  355. cg.ungetregister(exprasmlist,tempreg64.reglo);
  356. if (tempreg64.reghi <> left.location.registerhigh) then
  357. cg.ungetregister(exprasmlist,tempreg64.reghi);
  358. location_reset(location,LOC_FLAGS,OS_NO);
  359. location.resflags := getresflags;
  360. end;
  361. else
  362. internalerror(2002072803);
  363. end;
  364. { set result location }
  365. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  366. { real location only now) (JM) }
  367. if cmpop and
  368. not(nodetype in [equaln,unequaln]) then
  369. location_reset(location,LOC_JUMP,OS_NO);
  370. *)
  371. location_reset(location,LOC_JUMP,OS_NO);
  372. end;
  373. begin
  374. caddnode:=t68kaddnode;
  375. end.
  376. {
  377. $Log$
  378. Revision 1.4 2004-04-25 21:26:16 florian
  379. * some m68k stuff fixed
  380. Revision 1.3 2004/02/03 22:32:54 peter
  381. * renamed xNNbittype to xNNinttype
  382. * renamed registers32 to registersint
  383. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  384. Revision 1.2 2003/02/19 22:00:16 daniel
  385. * Code generator converted to new register notation
  386. - Horribily outdated todo.txt removed
  387. Revision 1.1 2003/02/02 19:25:54 carl
  388. * Several bugfixes for m68k target (register alloc., opcode emission)
  389. + VIS target
  390. + Generic add more complete (still not verified)
  391. }