ncpuadd.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. {******************************************************************************
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Code generation for add nodes on the i386
  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. unit ncpuadd;
  18. {$INCLUDE fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,cpubase,cginfo;
  22. type
  23. TSparcAddNode=class(TAddNode)
  24. procedure pass_2;override;
  25. private
  26. procedure second_addboolean;
  27. procedure second_add64bit;
  28. procedure second_addfloat;
  29. function GetResFlags(unsigned:Boolean):TResFlags;
  30. procedure left_must_be_reg(OpSize:TOpSize;NoSwap:Boolean);
  31. procedure emit_generic_code(op:TAsmOp;OpSize:TOpSize;unsigned,extra_not,mboverflow:Boolean);
  32. procedure emit_op_right_left(op:TAsmOp);
  33. procedure pass_left_and_right;
  34. procedure set_result_location(cmpOp,unsigned:Boolean);
  35. end;
  36. implementation
  37. uses
  38. globtype,systems,
  39. cutils,verbose,globals,
  40. symconst,symdef,SymType,paramgr,
  41. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  42. cgbase,pass_2,regvars,
  43. cpupara,
  44. ncon,nset,
  45. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  46. const
  47. opsize_2_cgSize:array[S_B..S_L]of TCgSize=(OS_8,OS_16,OS_32);
  48. procedure TSparcAddNode.second_addboolean;
  49. var
  50. cgop:TOpCg;
  51. cgsize:TCgSize;
  52. cmpop,isjump:boolean;
  53. otl,ofl:tasmlabel;
  54. pushedregs:TMaybeSave;
  55. begin
  56. { calculate the operator which is more difficult }
  57. firstcomplex(self);
  58. cmpop:=false;
  59. if (torddef(left.resulttype.def).typ=bool8bit) or
  60. (torddef(right.resulttype.def).typ=bool8bit)
  61. then
  62. cgsize:=OS_8
  63. else if (torddef(left.resulttype.def).typ=bool16bit) or
  64. (torddef(right.resulttype.def).typ=bool16bit)
  65. then
  66. cgsize:=OS_16
  67. else
  68. cgsize:=OS_32;
  69. if (cs_full_boolean_eval in aktlocalswitches) or
  70. (nodetype in [unequaln,ltn,lten,gtn,gten,equaln,xorn])
  71. then
  72. begin
  73. if left.nodetype in [ordconstn,realconstn]
  74. then
  75. swapleftright;
  76. isjump:=(left.location.loc=LOC_JUMP);
  77. if isjump
  78. then
  79. begin
  80. otl:=truelabel;
  81. objectlibrary.getlabel(truelabel);
  82. ofl:=falselabel;
  83. objectlibrary.getlabel(falselabel);
  84. end;
  85. secondpass(left);
  86. if left.location.loc in [LOC_FLAGS,LOC_JUMP]
  87. then
  88. location_force_reg(exprasmlist,left.location,cgsize,false);
  89. if isjump
  90. then
  91. begin
  92. truelabel:=otl;
  93. falselabel:=ofl;
  94. end;
  95. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  96. isjump:=(right.location.loc=LOC_JUMP);
  97. if isjump
  98. then
  99. begin
  100. otl:=truelabel;
  101. objectlibrary.getlabel(truelabel);
  102. ofl:=falselabel;
  103. objectlibrary.getlabel(falselabel);
  104. end;
  105. secondpass(right);
  106. maybe_restore(exprasmlist,left.location,pushedregs);
  107. if right.location.loc in [LOC_FLAGS,LOC_JUMP]
  108. then
  109. location_force_reg(exprasmlist,right.location,cgsize,false);
  110. if isjump
  111. then
  112. begin
  113. truelabel:=otl;
  114. falselabel:=ofl;
  115. end;
  116. cmpop := nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  117. { set result location }
  118. if not cmpop
  119. then
  120. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def))
  121. else
  122. location_reset(location,LOC_FLAGS,OS_NO);
  123. //load_left_right(cmpop,false);
  124. if (left.location.loc = LOC_CONSTANT)
  125. then
  126. swapleftright;
  127. { compare the }
  128. case nodetype of
  129. ltn,lten,gtn,gten,
  130. equaln,unequaln :
  131. begin
  132. if (right.location.loc <> LOC_CONSTANT)
  133. then
  134. exprasmlist.concat(taicpu.op_reg_reg(A_JMPL,left.location.register,right.location.register))
  135. else
  136. exprasmlist.concat(taicpu.op_reg_const(A_JMPL,left.location.register,longint(right.location.value)));
  137. location.resflags := GetResFlags(true);
  138. end;
  139. else
  140. begin
  141. case nodetype of
  142. xorn :
  143. cgop:=OP_XOR;
  144. orn :
  145. cgop:=OP_OR;
  146. andn :
  147. cgop:=OP_AND;
  148. else
  149. internalerror(200203247);
  150. end;
  151. if right.location.loc <> LOC_CONSTANT
  152. then
  153. cg.a_op_reg_reg_reg(exprasmlist,cgop,OS_INT,left.location.register,right.location.register,location.register)
  154. else
  155. cg.a_op_const_reg_reg(exprasmlist,cgop,OS_INT,right.location.value,left.location.register,location.register);
  156. end;
  157. end;
  158. end
  159. else
  160. begin
  161. // just to make sure we free the right registers
  162. cmpop := true;
  163. case nodetype of
  164. andn,
  165. orn :
  166. begin
  167. location_reset(location,LOC_JUMP,OS_NO);
  168. case nodetype of
  169. andn :
  170. begin
  171. otl:=truelabel;
  172. objectlibrary.getlabel(truelabel);
  173. secondpass(left);
  174. maketojumpbool(exprasmlist,left,lr_load_regvars);
  175. cg.a_label(exprasmlist,truelabel);
  176. truelabel:=otl;
  177. end;
  178. orn :
  179. begin
  180. ofl:=falselabel;
  181. objectlibrary.getlabel(falselabel);
  182. secondpass(left);
  183. maketojumpbool(exprasmlist,left,lr_load_regvars);
  184. cg.a_label(exprasmlist,falselabel);
  185. falselabel:=ofl;
  186. end;
  187. else
  188. CGMessage(type_e_mismatch);
  189. end;
  190. secondpass(right);
  191. maketojumpbool(exprasmlist,right,lr_load_regvars);
  192. end;
  193. end;
  194. end;
  195. // clear_left_right(CmpOp);
  196. end;
  197. function TSparcAddNode.GetResFlags(unsigned:Boolean):TResFlags;
  198. begin
  199. case NodeType of
  200. equaln:
  201. GetResFlags:=F_E;
  202. unequaln:
  203. GetResFlags:=F_NE;
  204. else
  205. if not(unsigned)
  206. then
  207. if nf_swaped IN flags
  208. then
  209. case NodeType of
  210. ltn:
  211. GetResFlags:=F_G;
  212. lten:
  213. GetResFlags:=F_GE;
  214. gtn:
  215. GetResFlags:=F_L;
  216. gten:
  217. GetResFlags:=F_LE;
  218. end
  219. else
  220. case NodeType of
  221. ltn:
  222. GetResFlags:=F_L;
  223. lten:
  224. GetResFlags:=F_LE;
  225. gtn:
  226. GetResFlags:=F_G;
  227. gten:
  228. GetResFlags:=F_GE;
  229. end
  230. else
  231. if nf_swaped IN Flags
  232. then
  233. case NodeType of
  234. ltn:
  235. GetResFlags:=F_A;
  236. lten:
  237. GetResFlags:=F_AE;
  238. gtn:
  239. GetResFlags:=F_B;
  240. gten:
  241. GetResFlags:=F_BE;
  242. end
  243. else
  244. case NodeType of
  245. ltn:
  246. GetResFlags:=F_B;
  247. lten:
  248. GetResFlags:=F_BE;
  249. gtn:
  250. GetResFlags:=F_A;
  251. gten:
  252. GetResFlags:=F_AE;
  253. end;
  254. end;
  255. end;
  256. procedure TSparcAddNode.left_must_be_reg(OpSize:TOpSize;NoSwap:Boolean);
  257. begin
  258. if(left.location.loc=LOC_REGISTER)
  259. then
  260. exit;
  261. {left location is not a register}
  262. if(not NoSwap)and(right.location.loc=LOC_REGISTER)
  263. then{right is register so we can swap the locations}
  264. begin
  265. location_swap(left.location,right.location);
  266. toggleflag(nf_swaped);
  267. end
  268. else
  269. begin
  270. {maybe we can reuse a constant register when the operation is a comparison that
  271. doesn't change the value of the register}
  272. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  273. end;
  274. end;
  275. procedure TSparcAddNode.emit_generic_code(op:TAsmOp;OpSize:TOpSize;unsigned,extra_not,mboverflow:Boolean);
  276. VAR
  277. power:LongInt;
  278. hl4:TAsmLabel;
  279. begin
  280. { at this point, left.location.loc should be LOC_REGISTER }
  281. if right.location.loc=LOC_REGISTER
  282. then
  283. begin
  284. { right.location is a LOC_REGISTER }
  285. { when swapped another result register }
  286. if(nodetype=subn)and(nf_swaped in flags)
  287. then
  288. begin
  289. if extra_not
  290. then
  291. exprasmList.concat(Taicpu.Op_reg(A_NOT,left.location.register));
  292. exprasmList.concat(Taicpu.Op_reg_reg_reg(Op,right.location.register,left.location.register,right.location.register));
  293. { newly swapped also set swapped flag }
  294. location_swap(left.location,right.location);
  295. toggleflag(nf_swaped);
  296. end
  297. else
  298. begin
  299. if extra_not
  300. then
  301. exprasmList.concat(Taicpu.Op_reg(A_NOT,right.location.register));
  302. // emit_reg_reg(op,opsize,right.location.register,left.location.register);
  303. exprasmList.concat(Taicpu.Op_reg_reg_reg(Op,right.location.register,left.location.register,right.location.register));
  304. end;
  305. end
  306. ELSE
  307. begin
  308. { right.location is not a LOC_REGISTER }
  309. IF(nodetype=subn)AND(nf_swaped IN flags)
  310. THEN
  311. begin
  312. IF extra_not
  313. THEN
  314. exprasmList.concat(Taicpu.Op_reg(A_NOT,left.location.register));
  315. // rg.getexplicitregisterint(exprasmlist,R_EDI);
  316. // cg.a_load_loc_reg(exprasmlist,right.location,R_EDI);
  317. // emit_reg_reg(op,opsize,left.location.register,R_EDI);
  318. // emit_reg_reg(A_MOV,opsize,R_EDI,left.location.register);
  319. // rg.ungetregisterint(exprasmlist,R_EDI);
  320. end
  321. ELSE
  322. begin
  323. { Optimizations when right.location is a constant value }
  324. IF(op=A_CMP)AND(nodetype IN [equaln,unequaln])AND(right.location.loc=LOC_CONSTANT)AND(right.location.value=0)
  325. THEN
  326. begin
  327. // emit_reg_reg(A_TEST,opsize,left.location.register,left.location.register);
  328. end
  329. ELSE IF(op=A_ADD)AND(right.location.loc=LOC_CONSTANT)AND(right.location.value=1)AND NOT(cs_check_overflow in aktlocalswitches)
  330. THEN
  331. with ExprAsmList,left.location do
  332. begin
  333. concat(TAiCpu.op_reg_const_reg(A_ADD,register,1,register));
  334. end
  335. ELSE IF(op=A_SUB)AND(right.location.loc=LOC_CONSTANT)AND(right.location.value=1)AND NOT(cs_check_overflow in aktlocalswitches)
  336. THEN
  337. begin
  338. exprasmList.concat(Taicpu.Op_reg(A_DEC,left.location.register));
  339. end
  340. ELSE IF(op=A_SMUL)AND(right.location.loc=LOC_CONSTANT)AND(ispowerof2(right.location.value,power))AND NOT(cs_check_overflow in aktlocalswitches)
  341. THEN
  342. begin
  343. exprasmList.concat(Taicpu.Op_const_reg(A_SLL,power,left.location.register));
  344. end
  345. ELSE
  346. begin
  347. IF extra_not
  348. THEN
  349. begin
  350. // rg.getexplicitregisterint(exprasmlist,R_EDI);
  351. // cg.a_load_loc_reg(exprasmlist,right.location,R_EDI);
  352. // emit_reg(A_NOT,S_L,R_EDI);
  353. // emit_reg_reg(A_AND,S_L,R_EDI,left.location.register);
  354. // rg.ungetregisterint(exprasmlist,R_EDI);
  355. end
  356. ELSE
  357. begin
  358. emit_op_right_left(op);
  359. end;
  360. end;
  361. end;
  362. end;
  363. { only in case of overflow operations }
  364. { produce overflow code }
  365. { we must put it here directly, because sign of operation }
  366. { is in unsigned VAR!! }
  367. IF mboverflow
  368. THEN
  369. begin
  370. IF cs_check_overflow IN aktlocalswitches
  371. THEN
  372. begin
  373. // getlabel(hl4);
  374. IF unsigned
  375. THEN
  376. exprasmList.concat(Taicpu.Op_sym(A_JMPL,S_NO,hl4))
  377. ELSE
  378. exprasmList.concat(Taicpu.Op_sym(A_JMPL,S_NO,hl4));
  379. cg.a_call_name(exprasmlist,'FPC_OVERFLOW');
  380. cg.a_label(exprasmlist,hl4);
  381. end;
  382. end;
  383. end;
  384. procedure TSparcAddNode.emit_op_right_left(op:TAsmOp);
  385. begin
  386. {left must be a register}
  387. with left,location,exprasmlist do
  388. case Right.Location.Loc of
  389. LOC_REGISTER,LOC_CREGISTER:
  390. concat(taicpu.op_reg_reg_reg(op,Register,Right.Location.register,register));
  391. LOC_REFERENCE,LOC_CREFERENCE :
  392. begin
  393. location_force_reg(exprasmlist,Right.Location,OS_32,(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  394. concat(taicpu.op_reg_reg_reg(op,register,Right.Location.register,register));
  395. end;
  396. LOC_CONSTANT:
  397. concat(taicpu.op_reg_const_reg(op,register,Right.Location.value,register));
  398. else
  399. InternalError(200203232);
  400. end;
  401. end;
  402. procedure TSparcAddNode.second_add64bit;
  403. var
  404. op : TOpCG;
  405. op1,op2 : TAsmOp;
  406. hl4 : tasmlabel;
  407. cmpop,
  408. unsigned : boolean;
  409. r : Tregister;
  410. procedure emit_cmp64_hi;
  411. var
  412. oldleft, oldright: tlocation;
  413. begin
  414. // put the high part of the location in the low part
  415. location_copy(oldleft,left.location);
  416. location_copy(oldright,right.location);
  417. if left.location.loc = LOC_CONSTANT
  418. then
  419. left.location.valueqword := left.location.valueqword shr 32
  420. else
  421. left.location.registerlow := left.location.registerhigh;
  422. if right.location.loc = LOC_CONSTANT
  423. then
  424. right.location.valueqword := right.location.valueqword shr 32
  425. else
  426. right.location.registerlow := right.location.registerhigh;
  427. // and call the normal emit_compare
  428. //emit_compare(unsigned);
  429. location_copy(left.location,oldleft);
  430. location_copy(right.location,oldright);
  431. end;
  432. procedure emit_cmp64_lo;
  433. begin
  434. // emit_compare(true);
  435. end;
  436. procedure firstjmp64bitcmp;
  437. var
  438. oldnodetype: tnodetype;
  439. begin
  440. load_all_regvars(exprasmlist);
  441. { the jump the sequence is a little bit hairy }
  442. case nodetype of
  443. ltn,gtn:
  444. begin
  445. cg.a_jmp_flags(exprasmlist,getresflags(false),truelabel);
  446. { cheat a little bit for the negative test }
  447. toggleflag(nf_swaped);
  448. cg.a_jmp_flags(exprasmlist,getresflags(false),falselabel);
  449. toggleflag(nf_swaped);
  450. end;
  451. lten,gten:
  452. begin
  453. oldnodetype:=nodetype;
  454. if nodetype=lten then
  455. nodetype:=ltn
  456. else
  457. nodetype:=gtn;
  458. cg.a_jmp_flags(exprasmlist,getresflags(false),truelabel);
  459. { cheat for the negative test }
  460. if nodetype=ltn then
  461. nodetype:=gtn
  462. else
  463. nodetype:=ltn;
  464. cg.a_jmp_flags(exprasmlist,getresflags(false),falselabel);
  465. nodetype:=oldnodetype;
  466. end;
  467. equaln:
  468. begin
  469. nodetype := unequaln;
  470. cg.a_jmp_flags(exprasmlist,getresflags(true),falselabel);
  471. nodetype := equaln;
  472. end;
  473. unequaln:
  474. begin
  475. cg.a_jmp_flags(exprasmlist,getresflags(true),truelabel);
  476. end;
  477. end;
  478. end;
  479. procedure secondjmp64bitcmp;
  480. begin
  481. { the jump the sequence is a little bit hairy }
  482. case nodetype of
  483. ltn,gtn,lten,gten:
  484. begin
  485. { the comparison of the low dword always has }
  486. { to be always unsigned! }
  487. cg.a_jmp_flags(exprasmlist,getresflags(false),truelabel);
  488. cg.a_jmp_always(exprasmlist,falselabel);
  489. end;
  490. equaln:
  491. begin
  492. nodetype := unequaln;
  493. cg.a_jmp_flags(exprasmlist,getresflags(true),falselabel);
  494. cg.a_jmp_always(exprasmlist,truelabel);
  495. nodetype := equaln;
  496. end;
  497. unequaln:
  498. begin
  499. cg.a_jmp_flags(exprasmlist,getresflags(true),truelabel);
  500. cg.a_jmp_always(exprasmlist,falselabel);
  501. end;
  502. end;
  503. end;
  504. var
  505. tempreg64: tregister64;
  506. begin
  507. firstcomplex(self);
  508. pass_left_and_right;
  509. cmpop:=false;
  510. unsigned:=((left.resulttype.def.deftype=orddef) and
  511. (torddef(left.resulttype.def).typ=u64bit)) or
  512. ((right.resulttype.def.deftype=orddef) and
  513. (torddef(right.resulttype.def).typ=u64bit));
  514. case nodetype of
  515. addn :
  516. begin
  517. op:=OP_ADD;
  518. end;
  519. subn :
  520. begin
  521. op:=OP_SUB;
  522. end;
  523. ltn,lten,
  524. gtn,gten,
  525. equaln,unequaln:
  526. begin
  527. op:=OP_NONE;
  528. cmpop:=true;
  529. end;
  530. xorn:
  531. op:=OP_XOR;
  532. orn:
  533. op:=OP_OR;
  534. andn:
  535. op:=OP_AND;
  536. muln:
  537. begin
  538. { should be handled in pass_1 (JM) }
  539. internalerror(200109051);
  540. end;
  541. else
  542. internalerror(2002072705);
  543. end;
  544. if not cmpop then
  545. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def));
  546. //load_left_right(cmpop,(cs_check_overflow in aktlocalswitches) and (nodetype in [addn,subn]));
  547. if not(cs_check_overflow in aktlocalswitches) or
  548. not(nodetype in [addn,subn]) then
  549. begin
  550. case nodetype of
  551. ltn,lten,
  552. gtn,gten:
  553. begin
  554. emit_cmp64_hi;
  555. firstjmp64bitcmp;
  556. emit_cmp64_lo;
  557. secondjmp64bitcmp;
  558. end;
  559. equaln,unequaln:
  560. begin
  561. // instead of doing a complicated compare, do
  562. // (left.hi xor right.hi) or (left.lo xor right.lo)
  563. // (somewhate optimized so that no superfluous 'mr's are
  564. // generated)
  565. if (left.location.loc = LOC_CONSTANT) then
  566. swapleftright;
  567. if (right.location.loc = LOC_CONSTANT) then
  568. begin
  569. if left.location.loc = LOC_REGISTER then
  570. begin
  571. tempreg64.reglo := left.location.registerlow;
  572. tempreg64.reghi := left.location.registerhigh;
  573. end
  574. else
  575. begin
  576. if (right.location.valueqword <> 0)
  577. then
  578. tempreg64.reglo := cg.get_scratch_reg_int(exprasmlist)
  579. else
  580. tempreg64.reglo := left.location.registerlow;
  581. if ((right.location.valueqword shr 32) <> 0) then
  582. tempreg64.reghi := cg.get_scratch_reg_int(exprasmlist)
  583. else
  584. tempreg64.reghi := left.location.registerhigh;
  585. end;
  586. if (right.location.valueqword <> 0) then
  587. { negative values can be handled using SUB, }
  588. { positive values < 65535 using XOR. }
  589. if (longint(right.location.valueqword) >= -32767) and
  590. (longint(right.location.valueqword) < 0) then
  591. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  592. right.location.valueqword,
  593. left.location.registerlow,tempreg64.reglo)
  594. else
  595. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  596. right.location.valueqword,
  597. left.location.registerlow,tempreg64.reglo);
  598. if ((right.location.valueqword shr 32) <> 0) then
  599. if (longint(right.location.valueqword shr 32) >= -32767) and
  600. (longint(right.location.valueqword shr 32) < 0) then
  601. cg.a_op_const_reg_reg(exprasmlist,OP_SUB,OS_INT,
  602. right.location.valueqword shr 32,
  603. left.location.registerhigh,tempreg64.reghi)
  604. else
  605. cg.a_op_const_reg_reg(exprasmlist,OP_XOR,OS_INT,
  606. right.location.valueqword shr 32,
  607. left.location.registerhigh,tempreg64.reghi);
  608. end
  609. else
  610. begin
  611. tempreg64.reglo := cg.get_scratch_reg_int(exprasmlist);
  612. tempreg64.reghi := cg.get_scratch_reg_int(exprasmlist);
  613. cg64.a_op64_reg_reg_reg(exprasmlist,OP_XOR,
  614. left.location.register64,right.location.register64,
  615. tempreg64);
  616. end;
  617. r.enum:=R_G0;
  618. cg.a_reg_alloc(exprasmlist,r);
  619. exprasmlist.concat(taicpu.op_reg_reg_reg(A_OR,r,
  620. tempreg64.reglo,tempreg64.reghi));
  621. cg.a_reg_dealloc(exprasmlist,r);
  622. if (tempreg64.reglo.enum <> left.location.registerlow.enum) then
  623. cg.free_scratch_reg(exprasmlist,tempreg64.reglo);
  624. if (tempreg64.reghi.enum <> left.location.registerhigh.enum) then
  625. cg.free_scratch_reg(exprasmlist,tempreg64.reghi);
  626. location_reset(location,LOC_FLAGS,OS_NO);
  627. location.resflags := getresflags(true);
  628. end;
  629. xorn,orn,andn,addn:
  630. begin
  631. if (location.registerlow.enum = R_NO) then
  632. begin
  633. location.registerlow := rg.getregisterint(exprasmlist);
  634. location.registerhigh := rg.getregisterint(exprasmlist);
  635. end;
  636. if (left.location.loc = LOC_CONSTANT) then
  637. swapleftright;
  638. if (right.location.loc = LOC_CONSTANT) then
  639. cg64.a_op64_const_reg_reg(exprasmlist,op,right.location.valueqword,
  640. left.location.register64,location.register64)
  641. else
  642. cg64.a_op64_reg_reg_reg(exprasmlist,op,right.location.register64,
  643. left.location.register64,location.register64);
  644. end;
  645. subn:
  646. begin
  647. if (nf_swaped in flags) then
  648. swapleftright;
  649. if left.location.loc <> LOC_CONSTANT then
  650. begin
  651. if (location.registerlow.enum = R_NO) then
  652. begin
  653. location.registerlow := rg.getregisterint(exprasmlist);
  654. location.registerhigh := rg.getregisterint(exprasmlist);
  655. end;
  656. if right.location.loc <> LOC_CONSTANT then
  657. // reg64 - reg64
  658. cg64.a_op64_reg_reg_reg(exprasmlist,OP_SUB,
  659. right.location.register64,left.location.register64,
  660. location.register64)
  661. else
  662. // reg64 - const64
  663. cg64.a_op64_const_reg_reg(exprasmlist,OP_SUB,
  664. right.location.valueqword,left.location.register64,
  665. location.register64)
  666. end
  667. else if ((left.location.valueqword shr 32) = 0) then
  668. begin
  669. if (location.registerlow.enum = R_NO) then
  670. begin
  671. location.registerlow := rg.getregisterint(exprasmlist);
  672. location.registerhigh := rg.getregisterint(exprasmlist);
  673. end;
  674. if (int64(left.location.valueqword) >= low(smallint)) and
  675. (int64(left.location.valueqword) <= high(smallint))
  676. then
  677. begin
  678. // consts16 - reg64
  679. exprasmlist.concat(taicpu.op_reg_const_Reg(A_SUBcc,location.registerlow,left.location.value,right.location.registerlow));
  680. end
  681. else
  682. begin
  683. // const32 - reg64
  684. cg.a_load_const_reg(exprasmlist,OS_32,
  685. left.location.valueqword,location.registerlow);
  686. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUBcc,
  687. location.registerlow,location.registerlow,
  688. right.location.registerlow));
  689. end;
  690. exprasmlist.concat(taicpu.op_reg_reg(A_SUBcc,
  691. location.registerhigh,right.location.registerhigh));
  692. end
  693. else if (left.location.valueqword = 0) then
  694. begin
  695. // (const32 shl 32) - reg64
  696. if (location.registerlow.enum = R_NO) then
  697. begin
  698. location.registerlow := rg.getregisterint(exprasmlist);
  699. location.registerhigh := rg.getregisterint(exprasmlist);
  700. end;
  701. exprasmlist.concat(taicpu.op_reg_Const_reg(A_SUBcc,location.registerlow,0,right.location.registerlow));
  702. cg.a_load_const_reg(exprasmlist,OS_INT,
  703. left.location.valueqword shr 32,location.registerhigh);
  704. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUBcc,
  705. location.registerhigh,right.location.registerhigh,
  706. location.registerhigh));
  707. end
  708. else
  709. begin
  710. // const64 - reg64
  711. location_force_reg(exprasmlist,left.location,
  712. def_cgsize(left.resulttype.def),true);
  713. if (left.location.loc = LOC_REGISTER) then
  714. location.register64 := left.location.register64
  715. else if (location.registerlow.enum = R_NO) then
  716. begin
  717. location.registerlow := rg.getregisterint(exprasmlist);
  718. location.registerhigh := rg.getregisterint(exprasmlist);
  719. end;
  720. cg64.a_op64_reg_reg_reg(exprasmlist,OP_SUB,
  721. right.location.register64,left.location.register64,
  722. location.register64);
  723. end;
  724. end;
  725. else
  726. internalerror(2002072803);
  727. end;
  728. end
  729. else
  730. begin
  731. case nodetype of
  732. addn:
  733. begin
  734. op1 := A_ADDcc;
  735. op2 := A_ADDcc;
  736. end;
  737. subn:
  738. begin
  739. op1 := A_SUBcc;
  740. op2 := A_SUBcc;
  741. end;
  742. else
  743. internalerror(2002072806);
  744. end;
  745. exprasmlist.concat(taicpu.op_reg_reg_reg(op1,location.registerlow,
  746. left.location.registerlow,right.location.registerlow));
  747. exprasmlist.concat(taicpu.op_reg_reg_reg(op2,location.registerhigh,
  748. right.location.registerhigh,left.location.registerhigh));
  749. cg.g_overflowcheck(exprasmlist,self);
  750. end;
  751. { set result location }
  752. { (emit_compare sets it to LOC_FLAGS for compares, so set the }
  753. { real location only now) (JM) }
  754. if cmpop and
  755. not(nodetype in [equaln,unequaln]) then
  756. location_reset(location,LOC_JUMP,OS_NO);
  757. // clear_left_right(cmpop);
  758. end;
  759. procedure TSparcAddNode.second_addfloat;
  760. var
  761. reg : tregister;
  762. op : TAsmOp;
  763. cmpop : boolean;
  764. r : Tregister;
  765. procedure location_force_fpureg(var l: tlocation);
  766. begin
  767. if not(l.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  768. begin
  769. reg := rg.getregisterfpu(exprasmlist);
  770. cg.a_loadfpu_loc_reg(exprasmlist,l,reg);
  771. location_freetemp(exprasmlist,l);
  772. location_release(exprasmlist,l);
  773. location_reset(l,LOC_FPUREGISTER,l.size);
  774. l.register := reg;
  775. end;
  776. end;
  777. begin
  778. pass_left_and_right;
  779. cmpop:=false;
  780. case nodetype of
  781. addn :
  782. op:=A_FADDs;
  783. muln :
  784. op:=A_FMULs;
  785. subn :
  786. op:=A_FSUBs;
  787. slashn :
  788. op:=A_FDIVs;
  789. ltn,lten,gtn,gten,
  790. equaln,unequaln :
  791. begin
  792. op:=A_FCMPs;
  793. cmpop:=true;
  794. end;
  795. else
  796. CGMessage(type_e_mismatch);
  797. end;
  798. // get the operands in the correct order, there are no special cases
  799. // here, everything is register-based
  800. if nf_swaped in flags then
  801. swapleftright;
  802. // put both operands in a register
  803. location_force_fpureg(right.location);
  804. location_force_fpureg(left.location);
  805. // initialize de result
  806. if not cmpop then
  807. begin
  808. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  809. if left.location.loc = LOC_FPUREGISTER then
  810. location.register := left.location.register
  811. else if right.location.loc = LOC_FPUREGISTER then
  812. location.register := right.location.register
  813. else
  814. location.register := rg.getregisterfpu(exprasmlist);
  815. end
  816. else
  817. begin
  818. location_reset(location,LOC_FLAGS,OS_NO);
  819. location.resflags := getresflags(true);
  820. end;
  821. // emit the actual operation
  822. if not cmpop then
  823. begin
  824. exprasmlist.concat(taicpu.op_reg_reg_reg(op,
  825. location.register,left.location.register,
  826. right.location.register))
  827. end
  828. else
  829. begin
  830. r.enum:=R_PSR;
  831. exprasmlist.concat(taicpu.op_reg_reg_reg(op,
  832. r,left.location.register,right.location.register))
  833. end;
  834. // clear_left_right(cmpop);
  835. end;
  836. procedure TSparcAddNode.set_result_location(cmpOp,unsigned:Boolean);
  837. begin
  838. IF cmpOp
  839. THEN
  840. begin
  841. location_reset(location,LOC_FLAGS,OS_NO);
  842. location.resflags:=GetResFlags(unsigned);
  843. end
  844. ELSE
  845. location_copy(location,left.location);
  846. end;
  847. function def_opsize(p1:tdef):topsize;
  848. begin
  849. case p1.size of
  850. 1:def_opsize:=S_B;
  851. 2:def_opsize:=S_W;
  852. 4:def_opsize:=S_L;
  853. 8:def_opsize:=S_L;
  854. else
  855. InternalError(130820001);
  856. end;
  857. end;
  858. procedure TSparcAddNode.pass_2;
  859. {is also being used for "xor", and "mul", "sub", or and comparative operators}
  860. var
  861. popeax,popedx,pushedfpu,mboverflow,cmpop:Boolean;
  862. op:TAsmOp;
  863. power:LongInt;
  864. OpSize:TOpSize;
  865. unsigned:Boolean;{true, if unsigned types are compared}
  866. extra_not:Boolean;
  867. cgop:TOpCg;
  868. begin
  869. {to make it more readable, string and set (not smallset!) have their own
  870. procedures }
  871. case left.resulttype.def.deftype of
  872. orddef:
  873. if is_boolean(left.resulttype.def)and is_boolean(right.resulttype.def)
  874. then{handling boolean expressions}
  875. begin
  876. second_addboolean;
  877. exit;
  878. end
  879. else if is_64bitint(left.resulttype.def)
  880. then{64bit operations}
  881. begin
  882. second_add64bit;
  883. exit;
  884. end;
  885. stringdef:
  886. InternalError(20020726);//second_addstring;
  887. setdef:
  888. {normalsets are already handled in pass1}
  889. if(tsetdef(left.resulttype.def).settype<>smallset)
  890. then
  891. internalerror(200109041)
  892. else
  893. InternalError(20020726);//second_addsmallset;
  894. arraydef :
  895. InternalError(2002110600);
  896. floatdef :
  897. begin
  898. second_addfloat;
  899. exit;
  900. end;
  901. end;
  902. {defaults}
  903. extra_not:=false;
  904. mboverflow:=false;
  905. cmpop:=nodetype in [ltn,lten,gtn,gten,equaln,unequaln];
  906. unsigned:=not(is_signed(left.resulttype.def))or
  907. not(is_signed(right.resulttype.def));
  908. opsize:=def_opsize(left.resulttype.def);
  909. pass_left_and_right;
  910. { set result location }
  911. if not cmpop
  912. then
  913. location_reset(location,LOC_REGISTER,def_cgsize(resulttype.def))
  914. else
  915. location_reset(location,LOC_FLAGS,OS_NO);
  916. //load_left_right(cmpop, (cs_check_overflow in aktlocalswitches) and
  917. //(nodetype in [addn,subn,muln]));
  918. if(location.register.enum = R_NO)and not(cmpop)
  919. then
  920. location.register := rg.getregisterint(exprasmlist);
  921. if not(cs_check_overflow in aktlocalswitches)or cmpop or (nodetype in [orn,andn,xorn])
  922. then
  923. begin
  924. case NodeType of
  925. addn:
  926. begin
  927. op:=A_ADD;
  928. mboverflow:=true;
  929. end;
  930. muln:
  931. begin
  932. IF unsigned
  933. THEN
  934. op:=A_UMUL
  935. ELSE
  936. op:=A_SMUL;
  937. mboverflow:=true;
  938. end;
  939. subn:
  940. begin
  941. op:=A_SUB;
  942. mboverflow:=true;
  943. end;
  944. ltn,lten,
  945. gtn,gten,
  946. equaln,unequaln:
  947. begin
  948. op:=A_CMP;
  949. cmpop:=true;
  950. end;
  951. xorn:
  952. op:=A_XOR;
  953. orn:
  954. op:=A_OR;
  955. andn:
  956. op:=A_AND;
  957. else
  958. CGMessage(type_e_mismatch);
  959. end;
  960. { Convert flags to register first }
  961. if(left.location.loc=LOC_FLAGS)
  962. then
  963. location_force_reg(exprasmlist,left.location,opsize_2_cgsize[opsize],false);
  964. if (right.location.loc=LOC_FLAGS)
  965. then
  966. location_force_reg(exprasmlist,right.location,opsize_2_cgsize[opsize],false);
  967. left_must_be_reg(OpSize,false);
  968. emit_generic_code(op,opsize,unsigned,extra_not,mboverflow);
  969. location_freetemp(exprasmlist,right.location);
  970. location_release(exprasmlist,right.location);
  971. if cmpop and(left.location.loc<>LOC_CREGISTER)
  972. then
  973. begin
  974. location_freetemp(exprasmlist,left.location);
  975. location_release(exprasmlist,left.location);
  976. end;
  977. end;
  978. //clear_left_right(cmpop);
  979. end;
  980. procedure TSparcAddNode.pass_left_and_right;
  981. var
  982. pushedregs:tmaybesave;
  983. tmpreg:tregister;
  984. pushedfpu:boolean;
  985. begin
  986. { calculate the operator which is more difficult }
  987. firstcomplex(self);
  988. { in case of constant put it to the left }
  989. if (left.nodetype=ordconstn)
  990. then
  991. swapleftright;
  992. secondpass(left);
  993. { are too few registers free? }
  994. maybe_save(exprasmlist,right.registers32,left.location,pushedregs);
  995. if location.loc=LOC_FPUREGISTER
  996. then
  997. pushedfpu:=maybe_pushfpu(exprasmlist,right.registersfpu,left.location)
  998. else
  999. pushedfpu:=false;
  1000. secondpass(right);
  1001. maybe_restore(exprasmlist,left.location,pushedregs);
  1002. if pushedfpu
  1003. then
  1004. begin
  1005. tmpreg := rg.getregisterfpu(exprasmlist);
  1006. cg.a_loadfpu_loc_reg(exprasmlist,left.location,tmpreg);
  1007. location_reset(left.location,LOC_FPUREGISTER,left.location.size);
  1008. left.location.register := tmpreg;
  1009. end;
  1010. end;
  1011. begin
  1012. cAddNode:=TSparcAddNode;
  1013. end.
  1014. {
  1015. $Log$
  1016. Revision 1.8 2003-01-22 20:45:15 mazen
  1017. * making math code in RTL compiling.
  1018. *NB : This does NOT mean necessary that it will generate correct code!
  1019. Revision 1.7 2003/01/20 22:21:36 mazen
  1020. * many stuff related to RTL fixed
  1021. Revision 1.6 2003/01/08 18:43:58 daniel
  1022. * Tregister changed into a record
  1023. Revision 1.5 2003/01/07 22:03:40 mazen
  1024. * adding unequaln node support to sparc compiler
  1025. Revision 1.4 2002/12/30 21:17:22 mazen
  1026. - unit cga no more used in sparc compiler.
  1027. Revision 1.3 2002/12/25 20:59:49 mazen
  1028. - many emitXXX removed from cga.pas in order to remove that file.
  1029. Revision 1.2 2002/12/22 19:26:32 mazen
  1030. * many internal errors related to unimplemented nodes are fixed
  1031. Revision 1.1 2002/12/21 23:21:47 mazen
  1032. + added support for the shift nodes
  1033. + added debug output on screen with -an command line option
  1034. Revision 1.10 2002/11/25 17:43:28 peter
  1035. * splitted defbase in defutil,symutil,defcmp
  1036. * merged isconvertable and is_equal into compare_defs(_ext)
  1037. * made operator search faster by walking the list only once
  1038. Revision 1.9 2002/11/10 19:07:46 mazen
  1039. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  1040. Revision 1.8 2002/11/06 15:34:00 mazen
  1041. *** empty log message ***
  1042. Revision 1.7 2002/11/06 11:31:24 mazen
  1043. * op_reg_reg_reg don't need any more a TOpSize parameter
  1044. Revision 1.6 2002/11/05 16:15:00 mazen
  1045. *** empty log message ***
  1046. Revision 1.5 2002/10/22 13:43:01 mazen
  1047. - cga.pas redueced to an empty unit
  1048. Revision 1.4 2002/10/10 20:23:57 mazen
  1049. * tabs replaces by spaces
  1050. }