ncpuadd.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 ncpuadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,ncgadd,cpubase;
  22. type
  23. t68kaddnode = class(tcgaddnode)
  24. procedure second_cmpordinal;override;
  25. procedure second_cmpsmallset;override;
  26. procedure second_cmp64bit;override;
  27. procedure second_cmpboolean;override;
  28. private
  29. function getresflags(unsigned: boolean) : tresflags;
  30. end;
  31. implementation
  32. uses
  33. globtype,systems,
  34. cutils,verbose,globals,
  35. symconst,symdef,paramgr,
  36. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  37. cgbase,cpuinfo,pass_1,pass_2,regvars,
  38. cpupara,cgutils,
  39. ncon,nset,
  40. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  41. {*****************************************************************************
  42. Helpers
  43. *****************************************************************************}
  44. function t68kaddnode.getresflags(unsigned : boolean) : tresflags;
  45. begin
  46. case nodetype of
  47. equaln : getresflags:=F_E;
  48. unequaln : getresflags:=F_NE;
  49. else
  50. if not(unsigned) then
  51. begin
  52. if nf_swaped in flags then
  53. case nodetype of
  54. ltn : getresflags:=F_G;
  55. lten : getresflags:=F_GE;
  56. gtn : getresflags:=F_L;
  57. gten : getresflags:=F_LE;
  58. end
  59. else
  60. case nodetype of
  61. ltn : getresflags:=F_L;
  62. lten : getresflags:=F_LE;
  63. gtn : getresflags:=F_G;
  64. gten : getresflags:=F_GE;
  65. end;
  66. end
  67. else
  68. begin
  69. if nf_swaped in flags then
  70. case nodetype of
  71. ltn : getresflags:=F_A;
  72. lten : getresflags:=F_AE;
  73. gtn : getresflags:=F_B;
  74. gten : getresflags:=F_BE;
  75. end
  76. else
  77. case nodetype of
  78. ltn : getresflags:=F_B;
  79. lten : getresflags:=F_BE;
  80. gtn : getresflags:=F_A;
  81. gten : getresflags:=F_AE;
  82. end;
  83. end;
  84. end;
  85. end;
  86. {*****************************************************************************
  87. Smallsets
  88. *****************************************************************************}
  89. procedure t68kaddnode.second_cmpsmallset;
  90. var
  91. tmpreg : tregister;
  92. begin
  93. location_reset(location,LOC_FLAGS,OS_NO);
  94. case nodetype of
  95. equaln,
  96. unequaln :
  97. begin
  98. {emit_compare(true);}
  99. end;
  100. lten,gten:
  101. begin
  102. If (not(nf_swaped in flags) and
  103. (nodetype = lten)) or
  104. ((nf_swaped in flags) and
  105. (nodetype = gten)) then
  106. swapleftright;
  107. // now we have to check whether left >= right
  108. tmpreg := cg.getintregister(exprasmlist,OS_INT);
  109. if left.location.loc = LOC_CONSTANT then
  110. begin
  111. cg.a_op_const_reg_reg(exprasmlist,OP_AND,OS_INT,
  112. not(left.location.value),right.location.register,tmpreg);
  113. exprasmlist.concat(taicpu.op_reg(A_TST,S_L,tmpreg));
  114. // the two instructions above should be folded together by
  115. // the peepholeoptimizer
  116. end
  117. else
  118. begin
  119. if right.location.loc = LOC_CONSTANT then
  120. begin
  121. cg.a_load_const_reg(exprasmlist,OS_INT,
  122. aword(right.location.value),tmpreg);
  123. exprasmlist.concat(taicpu.op_reg_reg(A_AND,S_L,
  124. tmpreg,left.location.register));
  125. end
  126. else
  127. exprasmlist.concat(taicpu.op_reg_reg(A_AND,S_L,
  128. right.location.register,left.location.register));
  129. end;
  130. cg.ungetcpuregister(exprasmlist,tmpreg);
  131. location.resflags := getresflags(true);
  132. end;
  133. else
  134. internalerror(2002072701);
  135. end;
  136. end;
  137. {*****************************************************************************
  138. Ordinals
  139. *****************************************************************************}
  140. procedure t68kaddnode.second_cmpordinal;
  141. var
  142. unsigned : boolean;
  143. useconst : boolean;
  144. tmpreg : tregister;
  145. op : tasmop;
  146. begin
  147. writeln('second_cmpordinal');
  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.value64)<>0) and ((hi(right.location.value64)<>-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.ungetcpuregister(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.getjumplabel(truelabel);
  237. ofl:=falselabel;
  238. objectlibrary.getjumplabel(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.getjumplabel(truelabel);
  253. ofl:=falselabel;
  254. objectlibrary.getjumplabel(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. writeln('second_cmp64bit');
  284. (* load_left_right(true,false);
  285. case nodetype of
  286. ltn,lten,
  287. gtn,gten:
  288. begin
  289. emit_cmp64_hi;
  290. firstjmp64bitcmp;
  291. emit_cmp64_lo;
  292. secondjmp64bitcmp;
  293. end;
  294. equaln,unequaln:
  295. begin
  296. // instead of doing a complicated compare, do
  297. // (left.hi xor right.hi) or (left.lo xor right.lo)
  298. // (somewhate optimized so that no superfluous 'mr's are
  299. // generated)
  300. if (left.location.loc = LOC_CONSTANT) then
  301. swapleftright;
  302. if (right.location.loc = LOC_CONSTANT) then
  303. begin
  304. if left.location.loc = LOC_REGISTER then
  305. begin
  306. tempreg64.reglo := left.location.register64.reglo;
  307. tempreg64.reghi := left.location.register64.reghi;
  308. end
  309. else
  310. begin
  311. if (aword(right.location.valueqword) <> 0) then
  312. tempreg64.reglo := cg.getintregister(exprasmlist)
  313. else
  314. tempreg64.reglo := left.location.register64.reglo;
  315. if ((right.location.valueqword shr 32) <> 0) then
  316. tempreg64.reghi := cg.getintregister(exprasmlist)
  317. else
  318. tempreg64.reghi := left.location.register64.reghi;
  319. end;
  320. if (aword(right.location.valueqword) <> 0) then
  321. { negative values can be handled using SUB, }
  322. { positive values < 65535 using XOR. }
  323. if (longint(right.location.valueqword) >= -32767) and
  324. (longint(right.location.valueqword) < 0) then
  325. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  326. aword(right.location.valueqword),
  327. left.location.register64.reglo,tempreg64.reglo)
  328. else
  329. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  330. aword(right.location.valueqword),
  331. left.location.register64.reglo,tempreg64.reglo);
  332. if ((right.location.valueqword shr 32) <> 0) then
  333. if (longint(right.location.valueqword shr 32) >= -32767) and
  334. (longint(right.location.valueqword shr 32) < 0) then
  335. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  336. aword(right.location.valueqword shr 32),
  337. left.location.register64.reghi,tempreg64.reghi)
  338. else
  339. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  340. aword(right.location.valueqword shr 32),
  341. left.location.register64.reghi,tempreg64.reghi);
  342. end
  343. else
  344. begin
  345. tempreg64.reglo := cg.getintregister(exprasmlist);
  346. tempreg64.reghi := cg.getintregister(exprasmlist);
  347. cg64.a_op64_reg_reg_reg(exprasmlist,OP_XOR,
  348. left.location.register64,right.location.register64,
  349. tempreg64);
  350. end;
  351. cg.a_reg_alloc(exprasmlist,R_0);
  352. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  353. tempreg64.reglo,tempreg64.reghi));
  354. cg.a_reg_dealloc(exprasmlist,R_0);
  355. if (tempreg64.reglo <> left.location.register64.reglo) then
  356. cg.ungetregister(exprasmlist,tempreg64.reglo);
  357. if (tempreg64.reghi <> left.location.register64.reghi) then
  358. cg.ungetregister(exprasmlist,tempreg64.reghi);
  359. location_reset(location,LOC_FLAGS,OS_NO);
  360. location.resflags := getresflags;
  361. end;
  362. else
  363. internalerror(2002072803);
  364. end;
  365. { set result location }
  366. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  367. { real location only now) (JM) }
  368. if cmpop and
  369. not(nodetype in [equaln,unequaln]) then
  370. location_reset(location,LOC_JUMP,OS_NO);
  371. *)
  372. location_reset(location,LOC_JUMP,OS_NO);
  373. end;
  374. begin
  375. caddnode:=t68kaddnode;
  376. end.