ncpuadd.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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,cginfo;
  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.get_scratch_reg_int(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.free_scratch_reg(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. load_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.get_scratch_reg_int(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.free_scratch_reg(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. pushedregs : tmaybesave;
  218. begin
  219. if (torddef(left.resulttype.def).typ=bool8bit) or
  220. (torddef(right.resulttype.def).typ=bool8bit) then
  221. cgsize:=OS_8
  222. else
  223. if (torddef(left.resulttype.def).typ=bool16bit) or
  224. (torddef(right.resulttype.def).typ=bool16bit) then
  225. cgsize:=OS_16
  226. else
  227. cgsize:=OS_32;
  228. if (cs_full_boolean_eval in aktlocalswitches) or
  229. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  230. begin
  231. if left.nodetype in [ordconstn,realconstn] then
  232. swapleftright;
  233. isjump:=(left.location.loc=LOC_JUMP);
  234. if isjump then
  235. begin
  236. otl:=truelabel;
  237. objectlibrary.getlabel(truelabel);
  238. ofl:=falselabel;
  239. objectlibrary.getlabel(falselabel);
  240. end;
  241. secondpass(left);
  242. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then
  243. location_force_reg(exprasmlist,left.location,cgsize,false);
  244. if isjump then
  245. begin
  246. truelabel:=otl;
  247. falselabel:=ofl;
  248. end;
  249. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  250. isjump:=(right.location.loc=LOC_JUMP);
  251. if isjump then
  252. begin
  253. otl:=truelabel;
  254. objectlibrary.getlabel(truelabel);
  255. ofl:=falselabel;
  256. objectlibrary.getlabel(falselabel);
  257. end;
  258. secondpass(right);
  259. maybe_restore(exprasmlist,left.location,pushedregs);
  260. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  261. location_force_reg(exprasmlist,right.location,cgsize,false);
  262. if isjump then
  263. begin
  264. truelabel:=otl;
  265. falselabel:=ofl;
  266. end;
  267. location_reset(location,LOC_FLAGS,OS_NO);
  268. load_left_right(true,false);
  269. if (left.location.loc = LOC_CONSTANT) then
  270. swapleftright;
  271. if (right.location.loc <> LOC_CONSTANT) then
  272. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,S_L,
  273. left.location.register,right.location.register))
  274. else
  275. exprasmlist.concat(taicpu.op_const_reg(A_CMP,S_L,
  276. longint(right.location.value),left.location.register));
  277. location.resflags := getresflags(true);
  278. end;
  279. clear_left_right(true);
  280. end;
  281. {*****************************************************************************
  282. 64-bit
  283. *****************************************************************************}
  284. procedure t68kaddnode.second_cmp64bit;
  285. begin
  286. (* load_left_right(true,false);
  287. case nodetype of
  288. ltn,lten,
  289. gtn,gten:
  290. begin
  291. emit_cmp64_hi;
  292. firstjmp64bitcmp;
  293. emit_cmp64_lo;
  294. secondjmp64bitcmp;
  295. end;
  296. equaln,unequaln:
  297. begin
  298. // instead of doing a complicated compare, do
  299. // (left.hi xor right.hi) or (left.lo xor right.lo)
  300. // (somewhate optimized so that no superfluous 'mr's are
  301. // generated)
  302. if (left.location.loc = LOC_CONSTANT) then
  303. swapleftright;
  304. if (right.location.loc = LOC_CONSTANT) then
  305. begin
  306. if left.location.loc = LOC_REGISTER then
  307. begin
  308. tempreg64.reglo := left.location.registerlow;
  309. tempreg64.reghi := left.location.registerhigh;
  310. end
  311. else
  312. begin
  313. if (aword(right.location.valueqword) <> 0) then
  314. tempreg64.reglo := cg.get_scratch_reg_int(exprasmlist)
  315. else
  316. tempreg64.reglo := left.location.registerlow;
  317. if ((right.location.valueqword shr 32) <> 0) then
  318. tempreg64.reghi := cg.get_scratch_reg_int(exprasmlist)
  319. else
  320. tempreg64.reghi := left.location.registerhigh;
  321. end;
  322. if (aword(right.location.valueqword) <> 0) then
  323. { negative values can be handled using SUB, }
  324. { positive values < 65535 using XOR. }
  325. if (longint(right.location.valueqword) >= -32767) and
  326. (longint(right.location.valueqword) < 0) then
  327. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  328. aword(right.location.valueqword),
  329. left.location.registerlow,tempreg64.reglo)
  330. else
  331. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  332. aword(right.location.valueqword),
  333. left.location.registerlow,tempreg64.reglo);
  334. if ((right.location.valueqword shr 32) <> 0) then
  335. if (longint(right.location.valueqword shr 32) >= -32767) and
  336. (longint(right.location.valueqword shr 32) < 0) then
  337. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  338. aword(right.location.valueqword shr 32),
  339. left.location.registerhigh,tempreg64.reghi)
  340. else
  341. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  342. aword(right.location.valueqword shr 32),
  343. left.location.registerhigh,tempreg64.reghi);
  344. end
  345. else
  346. begin
  347. tempreg64.reglo := cg.get_scratch_reg_int(exprasmlist);
  348. tempreg64.reghi := cg.get_scratch_reg_int(exprasmlist);
  349. cg64.a_op64_reg_reg_reg(exprasmlist,OP_XOR,
  350. left.location.register64,right.location.register64,
  351. tempreg64);
  352. end;
  353. cg.a_reg_alloc(exprasmlist,R_0);
  354. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  355. tempreg64.reglo,tempreg64.reghi));
  356. cg.a_reg_dealloc(exprasmlist,R_0);
  357. if (tempreg64.reglo <> left.location.registerlow) then
  358. cg.free_scratch_reg(exprasmlist,tempreg64.reglo);
  359. if (tempreg64.reghi <> left.location.registerhigh) then
  360. cg.free_scratch_reg(exprasmlist,tempreg64.reghi);
  361. location_reset(location,LOC_FLAGS,OS_NO);
  362. location.resflags := getresflags;
  363. end;
  364. else
  365. internalerror(2002072803);
  366. end;
  367. { set result location }
  368. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  369. { real location only now) (JM) }
  370. if cmpop and
  371. not(nodetype in [equaln,unequaln]) then
  372. location_reset(location,LOC_JUMP,OS_NO);
  373. *)
  374. location_reset(location,LOC_JUMP,OS_NO);
  375. end;
  376. begin
  377. caddnode:=t68kaddnode;
  378. end.
  379. {
  380. $Log$
  381. Revision 1.2 2003-02-19 22:00:16 daniel
  382. * Code generator converted to new register notation
  383. - Horribily outdated todo.txt removed
  384. Revision 1.1 2003/02/02 19:25:54 carl
  385. * Several bugfixes for m68k target (register alloc., opcode emission)
  386. + VIS target
  387. + Generic add more complete (still not verified)
  388. }