n68kadd.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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,hlcgobj,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. leftreg,
  188. rightreg : tregister;
  189. begin
  190. pass_left_right;
  191. location_reset(location,LOC_FLAGS,OS_NO);
  192. case nodetype of
  193. equaln,
  194. unequaln :
  195. begin
  196. {emit_compare(true);}
  197. end;
  198. lten,gten:
  199. begin
  200. if (not(nf_swapped in flags) and
  201. (nodetype = lten)) or
  202. ((nf_swapped in flags) and
  203. (nodetype = gten)) then
  204. swapleftright;
  205. // now we have to check whether left >= right
  206. // first load right into a register
  207. case right.location.loc of
  208. LOC_CONSTANT:
  209. begin
  210. rightreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  211. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  212. aword(right.location.value),rightreg);
  213. end;
  214. LOC_REFERENCE:
  215. begin
  216. rightreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  217. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,right.location.reference,rightreg);
  218. end;
  219. LOC_REGISTER:
  220. begin
  221. rightreg:=right.location.register;
  222. end;
  223. else
  224. internalerror(2012102001);
  225. end;
  226. // now process left
  227. case left.location.loc of
  228. LOC_CONSTANT:
  229. begin
  230. leftreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  231. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,
  232. not(left.location.value),rightreg,leftreg);
  233. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_TST,S_L,leftreg));
  234. // the two instructions above should be folded together by
  235. // the peepholeoptimizer
  236. end;
  237. LOC_REFERENCE:
  238. begin
  239. leftreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  240. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,leftreg);
  241. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,
  242. rightreg,leftreg));
  243. end;
  244. LOC_REGISTER:
  245. begin
  246. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_AND,S_L,
  247. rightreg,left.location.register));
  248. end;
  249. else
  250. internalerror(2012102002);
  251. end;
  252. location.resflags := getresflags(true);
  253. end;
  254. else
  255. internalerror(2002072701);
  256. end;
  257. end;
  258. {*****************************************************************************
  259. Ordinals
  260. *****************************************************************************}
  261. procedure t68kaddnode.second_cmpordinal;
  262. var
  263. unsigned : boolean;
  264. useconst : boolean;
  265. tmpreg : tregister;
  266. op : tasmop;
  267. begin
  268. pass_left_right;
  269. { set result location }
  270. location_reset(location,LOC_JUMP,OS_NO);
  271. { load values into registers (except constants) }
  272. force_reg_left_right(true, false);
  273. { determine if the comparison will be unsigned }
  274. unsigned:=not(is_signed(left.resultdef)) or
  275. not(is_signed(right.resultdef));
  276. // get the constant on the right if there is one
  277. if (left.location.loc = LOC_CONSTANT) then
  278. swapleftright;
  279. // can we use an immediate, or do we have to load the
  280. // constant in a register first?
  281. if (right.location.loc = LOC_CONSTANT) then
  282. begin
  283. {$ifdef extdebug}
  284. if (right.location.size in [OS_64,OS_S64]) and (hi(right.location.value64)<>0) and ((hi(right.location.value64)<>-1) or unsigned) then
  285. internalerror(2002080301);
  286. {$endif extdebug}
  287. if (nodetype in [equaln,unequaln]) then
  288. if (unsigned and
  289. (right.location.value > high(word))) or
  290. (not unsigned and
  291. (longint(right.location.value) < low(smallint)) or
  292. (longint(right.location.value) > high(smallint))) then
  293. { we can then maybe use a constant in the 'othersigned' case
  294. (the sign doesn't matter for // equal/unequal)}
  295. unsigned := not unsigned;
  296. if (unsigned and
  297. ((right.location.value) <= high(word))) or
  298. (not(unsigned) and
  299. (longint(right.location.value) >= low(smallint)) and
  300. (longint(right.location.value) <= high(smallint))) then
  301. useconst := true
  302. else
  303. begin
  304. useconst := false;
  305. tmpreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  306. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  307. aword(right.location.value),tmpreg);
  308. end
  309. end
  310. else
  311. useconst := false;
  312. location.loc := LOC_FLAGS;
  313. location.resflags := getresflags(unsigned);
  314. op := A_CMP;
  315. { Attention: The RIGHT(!) operand is substracted from and must be a
  316. register! }
  317. if (right.location.loc = LOC_CONSTANT) then
  318. if useconst then
  319. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(op,S_L,
  320. longint(right.location.value),left.location.register))
  321. else
  322. begin
  323. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_L,
  324. tmpreg,left.location.register));
  325. end
  326. else
  327. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_L,
  328. right.location.register,left.location.register));
  329. end;
  330. {*****************************************************************************
  331. Boolean
  332. *****************************************************************************}
  333. procedure t68kaddnode.second_cmpboolean;
  334. var
  335. cgop : TOpCg;
  336. cgsize : TCgSize;
  337. isjump : boolean;
  338. otl,ofl : tasmlabel;
  339. begin
  340. // writeln('second_cmpboolean');
  341. { ToDo : add support for pasbool64 and bool64bit }
  342. if (torddef(left.resultdef).ordtype in [pasbool8,bool8bit]) or
  343. (torddef(right.resultdef).ordtype in [pasbool8,bool8bit]) then
  344. cgsize:=OS_8
  345. else
  346. if (torddef(left.resultdef).ordtype in [pasbool16,bool16bit]) or
  347. (torddef(right.resultdef).ordtype in [pasbool16,bool16bit]) then
  348. cgsize:=OS_16
  349. else
  350. cgsize:=OS_32;
  351. if (cs_full_boolean_eval in current_settings.localswitches) or
  352. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn]) then
  353. begin
  354. if left.nodetype in [ordconstn,realconstn] then
  355. swapleftright;
  356. isjump:=(left.expectloc=LOC_JUMP);
  357. if isjump then
  358. begin
  359. otl:=current_procinfo.CurrTrueLabel;
  360. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  361. ofl:=current_procinfo.CurrFalseLabel;
  362. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  363. end;
  364. secondpass(left);
  365. if left.location.loc in [LOC_FLAGS,LOC_JUMP] then begin
  366. // writeln('ajjaj');
  367. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,hlcg.tcgsize2orddef(cgsize),false);
  368. // writeln('reccs?');
  369. end;
  370. if isjump then
  371. begin
  372. current_procinfo.CurrTrueLabel:=otl;
  373. current_procinfo.CurrFalseLabel:=ofl;
  374. end;
  375. isjump:=(right.expectloc=LOC_JUMP);
  376. if isjump then
  377. begin
  378. otl:=current_procinfo.CurrTrueLabel;
  379. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  380. ofl:=current_procinfo.CurrFalseLabel;
  381. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  382. end;
  383. secondpass(right);
  384. if right.location.loc in [LOC_FLAGS,LOC_JUMP] then
  385. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,hlcg.tcgsize2orddef(cgsize),false);
  386. if isjump then
  387. begin
  388. current_procinfo.CurrTrueLabel:=otl;
  389. current_procinfo.CurrFalseLabel:=ofl;
  390. end;
  391. location_reset(location,LOC_FLAGS,OS_NO);
  392. force_reg_left_right(true,false);
  393. if (left.location.loc = LOC_CONSTANT) then
  394. swapleftright;
  395. if (right.location.loc <> LOC_CONSTANT) then
  396. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,S_L,
  397. left.location.register,right.location.register))
  398. else
  399. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(A_CMP,S_L,
  400. longint(right.location.value),left.location.register));
  401. location.resflags := getresflags(true);
  402. end;
  403. //release_reg_left_right;
  404. end;
  405. {*****************************************************************************
  406. 64-bit
  407. *****************************************************************************}
  408. procedure t68kaddnode.second_cmp64bit;
  409. begin
  410. // writeln('second_cmp64bit');
  411. pass_left_right;
  412. // load_left_right(true,false);
  413. (*
  414. case nodetype of
  415. ltn,lten,
  416. gtn,gten:
  417. begin
  418. emit_cmp64_hi;
  419. firstjmp64bitcmp;
  420. emit_cmp64_lo;
  421. secondjmp64bitcmp;
  422. end;
  423. equaln,unequaln:
  424. begin
  425. // instead of doing a complicated compare, do
  426. // (left.hi xor right.hi) or (left.lo xor right.lo)
  427. // (somewhate optimized so that no superfluous 'mr's are
  428. // generated)
  429. if (left.location.loc = LOC_CONSTANT) then
  430. swapleftright;
  431. if (right.location.loc = LOC_CONSTANT) then
  432. begin
  433. if left.location.loc = LOC_REGISTER then
  434. begin
  435. tempreg64.reglo := left.location.register64.reglo;
  436. tempreg64.reghi := left.location.register64.reghi;
  437. end
  438. else
  439. begin
  440. if (aword(right.location.valueqword) <> 0) then
  441. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList)
  442. else
  443. tempreg64.reglo := left.location.register64.reglo;
  444. if ((right.location.valueqword shr 32) <> 0) then
  445. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList)
  446. else
  447. tempreg64.reghi := left.location.register64.reghi;
  448. end;
  449. if (aword(right.location.valueqword) <> 0) then
  450. { negative values can be handled using SUB, }
  451. { positive values < 65535 using XOR. }
  452. if (longint(right.location.valueqword) >= -32767) and
  453. (longint(right.location.valueqword) < 0) then
  454. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  455. aword(right.location.valueqword),
  456. left.location.register64.reglo,tempreg64.reglo)
  457. else
  458. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  459. aword(right.location.valueqword),
  460. left.location.register64.reglo,tempreg64.reglo);
  461. if ((right.location.valueqword shr 32) <> 0) then
  462. if (longint(right.location.valueqword shr 32) >= -32767) and
  463. (longint(right.location.valueqword shr 32) < 0) then
  464. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,
  465. aword(right.location.valueqword shr 32),
  466. left.location.register64.reghi,tempreg64.reghi)
  467. else
  468. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_INT,
  469. aword(right.location.valueqword shr 32),
  470. left.location.register64.reghi,tempreg64.reghi);
  471. end
  472. else
  473. begin
  474. tempreg64.reglo := cg.getintregister(current_asmdata.CurrAsmList);
  475. tempreg64.reghi := cg.getintregister(current_asmdata.CurrAsmList);
  476. cg64.a_op64_reg_reg_reg(current_asmdata.CurrAsmList,OP_XOR,
  477. left.location.register64,right.location.register64,
  478. tempreg64);
  479. end;
  480. cg.a_reg_alloc(current_asmdata.CurrAsmList,R_0);
  481. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_OR_,R_0,
  482. tempreg64.reglo,tempreg64.reghi));
  483. cg.a_reg_dealloc(current_asmdata.CurrAsmList,R_0);
  484. if (tempreg64.reglo <> left.location.register64.reglo) then
  485. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reglo);
  486. if (tempreg64.reghi <> left.location.register64.reghi) then
  487. cg.ungetregister(current_asmdata.CurrAsmList,tempreg64.reghi);
  488. location_reset(location,LOC_FLAGS,OS_NO);
  489. location.resflags := getresflags;
  490. end;
  491. else
  492. internalerror(2002072803);
  493. end;
  494. { set result location }
  495. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  496. { real location only now) (JM) }
  497. if cmpop and
  498. not(nodetype in [equaln,unequaln]) then
  499. location_reset(location,LOC_JUMP,OS_NO);
  500. *)
  501. location_reset(location,LOC_JUMP,OS_NO);
  502. // writeln('second_cmp64_exit');
  503. end;
  504. begin
  505. caddnode:=t68kaddnode;
  506. end.