n68kadd.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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;
  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. procedure second_cmpboolean;override;
  33. end;
  34. implementation
  35. uses
  36. globtype,systems,
  37. cutils,verbose,globals,
  38. symconst,symdef,paramgr,
  39. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  40. cgbase,cpuinfo,pass_1,pass_2,regvars,
  41. cpupara,cgutils,procinfo,
  42. ncon,nset,
  43. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  44. {*****************************************************************************
  45. Helpers
  46. *****************************************************************************}
  47. function t68kaddnode.getresflags(unsigned : boolean) : tresflags;
  48. begin
  49. case nodetype of
  50. equaln : getresflags:=F_E;
  51. unequaln : getresflags:=F_NE;
  52. else
  53. if not(unsigned) then
  54. begin
  55. if nf_swapped in flags then
  56. case nodetype of
  57. ltn : getresflags:=F_G;
  58. lten : getresflags:=F_GE;
  59. gtn : getresflags:=F_L;
  60. gten : getresflags:=F_LE;
  61. end
  62. else
  63. case nodetype of
  64. ltn : getresflags:=F_L;
  65. lten : getresflags:=F_LE;
  66. gtn : getresflags:=F_G;
  67. gten : getresflags:=F_GE;
  68. end;
  69. end
  70. else
  71. begin
  72. if nf_swapped in flags then
  73. case nodetype of
  74. ltn : getresflags:=F_A;
  75. lten : getresflags:=F_AE;
  76. gtn : getresflags:=F_B;
  77. gten : getresflags:=F_BE;
  78. end
  79. else
  80. case nodetype of
  81. ltn : getresflags:=F_B;
  82. lten : getresflags:=F_BE;
  83. gtn : getresflags:=F_A;
  84. gten : getresflags:=F_AE;
  85. end;
  86. end;
  87. end;
  88. end;
  89. {*****************************************************************************
  90. AddFloat
  91. *****************************************************************************}
  92. procedure t68kaddnode.second_addfloat;
  93. var
  94. op : TAsmOp;
  95. cmpop : boolean;
  96. begin
  97. pass_left_right;
  98. cmpop:=false;
  99. case nodetype of
  100. addn :
  101. op:=A_FADD;
  102. muln :
  103. op:=A_FMUL;
  104. subn :
  105. op:=A_FSUB;
  106. slashn :
  107. op:=A_FDIV;
  108. ltn,lten,gtn,gten,
  109. equaln,unequaln :
  110. begin
  111. // op:=A_FCMPO;
  112. cmpop:=true;
  113. end;
  114. else
  115. internalerror(200403182);
  116. end;
  117. // get the operands in the correct order, there are no special cases
  118. // here, everything is register-based
  119. if nf_swapped in flags then
  120. swapleftright;
  121. // put both operands in a register
  122. location_force_fpureg(current_asmdata.CurrAsmList,right.location,true);
  123. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  124. // initialize de result
  125. if not cmpop then
  126. begin
  127. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  128. if left.location.loc = LOC_FPUREGISTER then
  129. location.register := left.location.register
  130. else if right.location.loc = LOC_FPUREGISTER then
  131. location.register := right.location.register
  132. else
  133. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  134. end
  135. else
  136. begin
  137. location_reset(location,LOC_FLAGS,OS_NO);
  138. // FIX ME!
  139. // location.resflags := getresflags;
  140. end;
  141. // emit the actual operation
  142. if not cmpop then
  143. begin
  144. {
  145. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  146. location.register,left.location.register,
  147. right.location.register))
  148. }
  149. end
  150. else
  151. begin
  152. { current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  153. newreg(R_SPECIALREGISTER,location.resflags.cr,R_SUBNONE),left.location.register,right.location.register))}
  154. end;
  155. end;
  156. procedure t68kaddnode.second_cmpfloat;
  157. begin
  158. pass_left_right;
  159. {
  160. if (nf_swapped in flags) then
  161. swapleftright;
  162. }
  163. { force fpureg as location, left right doesn't matter
  164. as both will be in a fpureg }
  165. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  166. location_force_fpureg(current_asmdata.CurrAsmList,right.location,true);
  167. location_reset(location,LOC_FLAGS,OS_NO);
  168. location.resflags:=getresflags(true);
  169. {
  170. if nodetype in [equaln,unequaln] then
  171. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMF,
  172. left.location.register,right.location.register),
  173. cgsize2fpuoppostfix[def_cgsize(resultdef)]))
  174. else
  175. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMFE,
  176. left.location.register,right.location.register),
  177. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  178. location_reset(location,LOC_FLAGS,OS_NO);
  179. location.resflags:=getresflags(false);
  180. }
  181. end;
  182. {*****************************************************************************
  183. Smallsets
  184. *****************************************************************************}
  185. procedure t68kaddnode.second_cmpsmallset;
  186. var
  187. tmpreg : tregister;
  188. begin
  189. location_reset(location,LOC_FLAGS,OS_NO);
  190. case nodetype of
  191. equaln,
  192. unequaln :
  193. begin
  194. {emit_compare(true);}
  195. end;
  196. lten,gten:
  197. begin
  198. If (not(nf_swapped in flags) and
  199. (nodetype = lten)) or
  200. ((nf_swapped in flags) and
  201. (nodetype = gten)) then
  202. swapleftright;
  203. // now we have to check whether left >= right
  204. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  205. if left.location.loc = LOC_CONSTANT then
  206. begin
  207. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,
  208. not(left.location.value),right.location.register,tmpreg);
  209. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,tmpreg));
  210. // the two instructions above should be folded together by
  211. // the peepholeoptimizer
  212. end
  213. else
  214. begin
  215. if right.location.loc = LOC_CONSTANT then
  216. begin
  217. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  218. aword(right.location.value),tmpreg);
  219. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,
  220. tmpreg,left.location.register));
  221. end
  222. else
  223. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,
  224. right.location.register,left.location.register));
  225. end;
  226. // cg.ungetcpuregister(current_asmdata.CurrAsmList,tmpreg);
  227. location.resflags := getresflags(true);
  228. end;
  229. else
  230. internalerror(2002072701);
  231. end;
  232. end;
  233. {*****************************************************************************
  234. Ordinals
  235. *****************************************************************************}
  236. procedure t68kaddnode.second_cmpordinal;
  237. var
  238. unsigned : boolean;
  239. useconst : boolean;
  240. tmpreg : tregister;
  241. op : tasmop;
  242. begin
  243. // writeln('second_cmpordinal');
  244. pass_left_right;
  245. { set result location }
  246. location_reset(location,LOC_JUMP,OS_NO);
  247. { load values into registers (except constants) }
  248. force_reg_left_right(true, false);
  249. { determine if the comparison will be unsigned }
  250. unsigned:=not(is_signed(left.resultdef)) or
  251. not(is_signed(right.resultdef));
  252. // get the constant on the right if there is one
  253. if (left.location.loc = LOC_CONSTANT) then
  254. swapleftright;
  255. // can we use an immediate, or do we have to load the
  256. // constant in a register first?
  257. if (right.location.loc = LOC_CONSTANT) then
  258. begin
  259. {$ifdef extdebug}
  260. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.value64)<>0) and ((hi(right.location.value64)<>-1) or unsigned) then
  261. internalerror(2002080301);
  262. {$endif extdebug}
  263. if (nodetype in [equaln,unequaln]) then
  264. if (unsigned and
  265. (right.location.value > high(word))) or
  266. (not unsigned and
  267. (longint(right.location.value) < low(smallint)) or
  268. (longint(right.location.value) > high(smallint))) then
  269. { we can then maybe use a constant in the 'othersigned' case
  270. (the sign doesn't matter for // equal/unequal)}
  271. unsigned := not unsigned;
  272. if (unsigned and
  273. ((right.location.value) <= high(word))) or
  274. (not(unsigned) and
  275. (longint(right.location.value) >= low(smallint)) and
  276. (longint(right.location.value) <= high(smallint))) then
  277. useconst := true
  278. else
  279. begin
  280. useconst := false;
  281. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  282. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  283. aword(right.location.value),tmpreg);
  284. end
  285. end
  286. else
  287. useconst := false;
  288. location.loc := LOC_FLAGS;
  289. location.resflags := getresflags(unsigned);
  290. op := A_CMP;
  291. if (right.location.loc = LOC_CONSTANT) then
  292. if useconst then
  293. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(op,S_L,
  294. left.location.register,longint(right.location.value)))
  295. else
  296. begin
  297. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_L,
  298. left.location.register,tmpreg));
  299. // cg.ungetcpuregister(current_asmdata.CurrAsmList,tmpreg);
  300. end
  301. else
  302. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_L,
  303. left.location.register,right.location.register));
  304. end;
  305. {*****************************************************************************
  306. Boolean
  307. *****************************************************************************}
  308. procedure t68kaddnode.second_cmpboolean;
  309. var
  310. cgop : TOpCg;
  311. cgsize : TCgSize;
  312. isjump : boolean;
  313. otl,ofl : tasmlabel;
  314. begin
  315. // writeln('second_cmpboolean');
  316. if (torddef(left.resultdef).ordtype=bool8bit) or
  317. (torddef(right.resultdef).ordtype=bool8bit) then
  318. cgsize:=OS_8
  319. else
  320. if (torddef(left.resultdef).ordtype=bool16bit) or
  321. (torddef(right.resultdef).ordtype=bool16bit) then
  322. cgsize:=OS_16
  323. else
  324. cgsize:=OS_32;
  325. if (cs_full_boolean_eval in current_settings.localswitches) or
  326. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  327. begin
  328. if left.nodetype in [ordconstn,realconstn] then
  329. swapleftright;
  330. isjump:=(left.location.loc=LOC_JUMP);
  331. if isjump then
  332. begin
  333. otl:=current_procinfo.CurrTrueLabel;
  334. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  335. ofl:=current_procinfo.CurrFalseLabel;
  336. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  337. end;
  338. secondpass(left);
  339. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then begin
  340. // writeln('ajjaj');
  341. location_force_reg(current_asmdata.CurrAsmList,left.location,cgsize,false);
  342. // writeln('reccs?');
  343. end;
  344. if isjump then
  345. begin
  346. current_procinfo.CurrTrueLabel:=otl;
  347. current_procinfo.CurrFalseLabel:=ofl;
  348. end;
  349. isjump:=(right.location.loc=LOC_JUMP);
  350. if isjump then
  351. begin
  352. otl:=current_procinfo.CurrTrueLabel;
  353. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  354. ofl:=current_procinfo.CurrFalseLabel;
  355. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  356. end;
  357. secondpass(right);
  358. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  359. location_force_reg(current_asmdata.CurrAsmList,right.location,cgsize,false);
  360. if isjump then
  361. begin
  362. current_procinfo.CurrTrueLabel:=otl;
  363. current_procinfo.CurrFalseLabel:=ofl;
  364. end;
  365. location_reset(location,LOC_FLAGS,OS_NO);
  366. force_reg_left_right(true,false);
  367. if (left.location.loc = LOC_CONSTANT) then
  368. swapleftright;
  369. if (right.location.loc <> LOC_CONSTANT) then
  370. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,
  371. left.location.register,right.location.register))
  372. else
  373. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,
  374. longint(right.location.value),left.location.register));
  375. location.resflags := getresflags(true);
  376. end;
  377. //release_reg_left_right;
  378. end;
  379. {*****************************************************************************
  380. 64-bit
  381. *****************************************************************************}
  382. procedure t68kaddnode.second_cmp64bit;
  383. begin
  384. // writeln('second_cmp64bit');
  385. pass_left_right;
  386. // load_left_right(true,false);
  387. (*
  388. case nodetype of
  389. ltn,lten,
  390. gtn,gten:
  391. begin
  392. emit_cmp64_hi;
  393. firstjmp64bitcmp;
  394. emit_cmp64_lo;
  395. secondjmp64bitcmp;
  396. end;
  397. equaln,unequaln:
  398. begin
  399. // instead of doing a complicated compare, do
  400. // (left.hi xor right.hi) or (left.lo xor right.lo)
  401. // (somewhate optimized so that no superfluous 'mr's are
  402. // generated)
  403. if (left.location.loc = LOC_CONSTANT) then
  404. swapleftright;
  405. if (right.location.loc = LOC_CONSTANT) then
  406. begin
  407. if left.location.loc = LOC_REGISTER then
  408. begin
  409. tempreg64.reglo := left.location.register64.reglo;
  410. tempreg64.reghi := left.location.register64.reghi;
  411. end
  412. else
  413. begin
  414. if (aword(right.location.valueqword) <> 0) then
  415. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList)
  416. else
  417. tempreg64.reglo := left.location.register64.reglo;
  418. if ((right.location.valueqword shr 32) <> 0) then
  419. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList)
  420. else
  421. tempreg64.reghi := left.location.register64.reghi;
  422. end;
  423. if (aword(right.location.valueqword) <> 0) then
  424. { negative values can be handled using SUB, }
  425. { positive values < 65535 using XOR. }
  426. if (longint(right.location.valueqword) >= -32767) and
  427. (longint(right.location.valueqword) < 0) then
  428. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  429. aword(right.location.valueqword),
  430. left.location.register64.reglo,tempreg64.reglo)
  431. else
  432. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  433. aword(right.location.valueqword),
  434. left.location.register64.reglo,tempreg64.reglo);
  435. if ((right.location.valueqword shr 32) <> 0) then
  436. if (longint(right.location.valueqword shr 32) >= -32767) and
  437. (longint(right.location.valueqword shr 32) < 0) then
  438. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  439. aword(right.location.valueqword shr 32),
  440. left.location.register64.reghi,tempreg64.reghi)
  441. else
  442. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  443. aword(right.location.valueqword shr 32),
  444. left.location.register64.reghi,tempreg64.reghi);
  445. end
  446. else
  447. begin
  448. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList);
  449. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList);
  450. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,
  451. left.location.register64,right.location.register64,
  452. tempreg64);
  453. end;
  454. cg.a_reg_alloc(current_asmdata.CurrAsmList,R_0);
  455. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  456. tempreg64.reglo,tempreg64.reghi));
  457. cg.a_reg_dealloc(current_asmdata.CurrAsmList,R_0);
  458. if (tempreg64.reglo <> left.location.register64.reglo) then
  459. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reglo);
  460. if (tempreg64.reghi <> left.location.register64.reghi) then
  461. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reghi);
  462. location_reset(location,LOC_FLAGS,OS_NO);
  463. location.resflags := getresflags;
  464. end;
  465. else
  466. internalerror(2002072803);
  467. end;
  468. { set result location }
  469. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  470. { real location only now) (JM) }
  471. if cmpop and
  472. not(nodetype in [equaln,unequaln]) then
  473. location_reset(location,LOC_JUMP,OS_NO);
  474. *)
  475. location_reset(location,LOC_JUMP,OS_NO);
  476. // writeln('second_cmp64_exit');
  477. end;
  478. begin
  479. caddnode:=t68kaddnode;
  480. end.