n386mat.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Generate i386 assembler for math nodes
  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 n386mat;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,nmat;
  23. type
  24. ti386moddivnode = class(tmoddivnode)
  25. procedure pass_2;override;
  26. end;
  27. ti386shlshrnode = class(tshlshrnode)
  28. procedure pass_2;override;
  29. end;
  30. ti386unaryminusnode = class(tunaryminusnode)
  31. procedure pass_2;override;
  32. end;
  33. ti386notnode = class(tnotnode)
  34. procedure pass_2;override;
  35. end;
  36. implementation
  37. uses
  38. globtype,systems,
  39. cutils,cobjects,verbose,globals,
  40. symconst,symdef,aasm,types,
  41. hcodegen,temp_gen,pass_2,
  42. ncon,
  43. cpubase,cpuasm,
  44. cgai386,tgeni386,n386util;
  45. {*****************************************************************************
  46. TI386MODDIVNODE
  47. *****************************************************************************}
  48. procedure ti386moddivnode.pass_2;
  49. var
  50. hreg1 : tregister;
  51. hreg2 : tregister;
  52. shrdiv, andmod, pushed,popeax,popedx : boolean;
  53. power : longint;
  54. hl : pasmlabel;
  55. hloc : tlocation;
  56. pushedreg : tpushed;
  57. typename,opname : string[6];
  58. begin
  59. shrdiv := false;
  60. andmod := false;
  61. secondpass(left);
  62. pushed:=maybe_push(right.registers32,left,is_64bitint(left.resulttype));
  63. secondpass(right);
  64. if pushed then
  65. restore(left,is_64bitint(left.resulttype));
  66. set_location(location,left.location);
  67. if is_64bitint(resulttype) then
  68. begin
  69. { save lcoation, because we change it now }
  70. set_location(hloc,location);
  71. release_qword_loc(location);
  72. release_qword_loc(right.location);
  73. location.registerlow:=getexplicitregister32(R_EAX);
  74. location.registerhigh:=getexplicitregister32(R_EDX);
  75. pushusedregisters(pushedreg,$ff
  76. and not($80 shr byte(location.registerlow))
  77. and not($80 shr byte(location.registerhigh)));
  78. { the left operand is in hloc, because the
  79. location of left is location but location
  80. is already destroyed
  81. }
  82. emit_pushq_loc(hloc);
  83. clear_location(hloc);
  84. emit_pushq_loc(right.location);
  85. if porddef(resulttype)^.typ=u64bit then
  86. typename:='QWORD'
  87. else
  88. typename:='INT64';
  89. if nodetype=divn then
  90. opname:='DIV_'
  91. else
  92. opname:='MOD_';
  93. emitcall('FPC_'+opname+typename);
  94. emit_reg_reg(A_MOV,S_L,R_EAX,location.registerlow);
  95. emit_reg_reg(A_MOV,S_L,R_EDX,location.registerhigh);
  96. popusedregisters(pushedreg);
  97. location.loc:=LOC_REGISTER;
  98. end
  99. else
  100. begin
  101. { put numerator in register }
  102. if left.location.loc<>LOC_REGISTER then
  103. begin
  104. if left.location.loc=LOC_CREGISTER then
  105. begin
  106. hreg1:=getregister32;
  107. emit_reg_reg(A_MOV,S_L,left.location.register,hreg1);
  108. end
  109. else
  110. begin
  111. del_reference(left.location.reference);
  112. hreg1:=getregister32;
  113. emit_ref_reg(A_MOV,S_L,newreference(left.location.reference),
  114. hreg1);
  115. end;
  116. clear_location(left.location);
  117. left.location.loc:=LOC_REGISTER;
  118. left.location.register:=hreg1;
  119. end
  120. else hreg1:=left.location.register;
  121. if (nodetype=divn) and (right.nodetype=ordconstn) and
  122. ispowerof2(tordconstnode(right).value,power) then
  123. Begin
  124. shrdiv := true;
  125. {for signed numbers, the numerator must be adjusted before the
  126. shift instruction, but not wih unsigned numbers! Otherwise,
  127. "Cardinal($ffffffff) div 16" overflows! (JM)}
  128. If is_signed(left.resulttype) Then
  129. Begin
  130. If (aktOptProcessor <> class386) and
  131. not(CS_LittleSize in aktglobalswitches) then
  132. { use a sequence without jumps, saw this in
  133. comp.compilers (JM) }
  134. begin
  135. { no jumps, but more operations }
  136. if (hreg1 = R_EAX) and
  137. (R_EDX in unused) then
  138. begin
  139. hreg2 := getexplicitregister32(R_EDX);
  140. emit_none(A_CDQ,S_NO);
  141. end
  142. else
  143. begin
  144. getexplicitregister32(R_EDI);
  145. hreg2 := R_EDI;
  146. emit_reg_reg(A_MOV,S_L,hreg1,R_EDI);
  147. { if the left value is signed, R_EDI := $ffffffff,
  148. otherwise 0 }
  149. emit_const_reg(A_SAR,S_L,31,R_EDI);
  150. { if signed, R_EDI := right value-1, otherwise 0 }
  151. end;
  152. emit_const_reg(A_AND,S_L,tordconstnode(right).value-1,hreg2);
  153. { add to the left value }
  154. emit_reg_reg(A_ADD,S_L,hreg2,hreg1);
  155. { release EDX if we used it }
  156. { also releas EDI }
  157. ungetregister32(hreg2);
  158. { do the shift }
  159. emit_const_reg(A_SAR,S_L,power,hreg1);
  160. end
  161. else
  162. begin
  163. { a jump, but less operations }
  164. emit_reg_reg(A_TEST,S_L,hreg1,hreg1);
  165. getlabel(hl);
  166. emitjmp(C_NS,hl);
  167. if power=1 then
  168. emit_reg(A_INC,S_L,hreg1)
  169. else
  170. emit_const_reg(A_ADD,S_L,tordconstnode(right).value-1,hreg1);
  171. emitlab(hl);
  172. emit_const_reg(A_SAR,S_L,power,hreg1);
  173. end
  174. End
  175. Else
  176. emit_const_reg(A_SHR,S_L,power,hreg1);
  177. End
  178. else
  179. if (nodetype=modn) and (right.nodetype=ordconstn) and
  180. ispowerof2(tordconstnode(right).value,power) and Not(is_signed(left.resulttype)) Then
  181. {is there a similar trick for MOD'ing signed numbers? (JM)}
  182. Begin
  183. emit_const_reg(A_AND,S_L,tordconstnode(right).value-1,hreg1);
  184. andmod := true;
  185. End
  186. else
  187. begin
  188. { bring denominator to EDI }
  189. { EDI is always free, it's }
  190. { only used for temporary }
  191. { purposes }
  192. getexplicitregister32(R_EDI);
  193. if (right.location.loc<>LOC_REGISTER) and
  194. (right.location.loc<>LOC_CREGISTER) then
  195. begin
  196. del_reference(right.location.reference);
  197. left.location.loc:=LOC_REGISTER;
  198. emit_ref_reg(A_MOV,S_L,newreference(right.location.reference),R_EDI);
  199. end
  200. else
  201. begin
  202. emit_reg_reg(A_MOV,S_L,right.location.register,R_EDI);
  203. ungetregister32(right.location.register);
  204. end;
  205. popedx:=false;
  206. popeax:=false;
  207. if hreg1=R_EDX then
  208. begin
  209. if not(R_EAX in unused) then
  210. begin
  211. emit_reg(A_PUSH,S_L,R_EAX);
  212. popeax:=true;
  213. end;
  214. emit_reg_reg(A_MOV,S_L,R_EDX,R_EAX);
  215. end
  216. else
  217. begin
  218. if not(R_EDX in unused) then
  219. begin
  220. emit_reg(A_PUSH,S_L,R_EDX);
  221. popedx:=true;
  222. end
  223. else
  224. getexplicitregister32(R_EDX);
  225. if hreg1<>R_EAX then
  226. begin
  227. if not(R_EAX in unused) then
  228. begin
  229. emit_reg(A_PUSH,S_L,R_EAX);
  230. popeax:=true;
  231. end
  232. else
  233. getexplicitregister32(R_EAX);
  234. emit_reg_reg(A_MOV,S_L,hreg1,R_EAX);
  235. end;
  236. end;
  237. { sign extension depends on the left type }
  238. if porddef(left.resulttype)^.typ=u32bit then
  239. emit_reg_reg(A_XOR,S_L,R_EDX,R_EDX)
  240. else
  241. emit_none(A_CDQ,S_NO);
  242. { division depends on the right type }
  243. if porddef(right.resulttype)^.typ=u32bit then
  244. emit_reg(A_DIV,S_L,R_EDI)
  245. else
  246. emit_reg(A_IDIV,S_L,R_EDI);
  247. ungetregister32(R_EDI);
  248. if nodetype=divn then
  249. begin
  250. { if result register is busy then copy }
  251. if popeax then
  252. begin
  253. if hreg1=R_EAX then
  254. internalerror(112);
  255. emit_reg_reg(A_MOV,S_L,R_EAX,hreg1)
  256. end
  257. else
  258. if hreg1<>R_EAX then
  259. Begin
  260. ungetregister32(hreg1);
  261. { no need to allocate eax, that's already done before }
  262. { the div (JM) }
  263. hreg1 := R_EAX;
  264. end;
  265. if not popedx then
  266. ungetregister(R_EDX);
  267. end
  268. else
  269. {if we did the mod by an "and", the result is in hreg1 and
  270. EDX certainly hasn't been pushed (JM)}
  271. if not(andmod) Then
  272. begin
  273. if popedx then
  274. {the mod was done by an (i)div (so the result is now in
  275. edx), but edx was occupied prior to the division, so
  276. move the result into a safe place (JM)}
  277. emit_reg_reg(A_MOV,S_L,R_EDX,hreg1)
  278. else
  279. Begin
  280. ungetregister32(hreg1);
  281. hreg1 := R_EDX
  282. End;
  283. if not popeax then
  284. ungetregister(R_EAX);
  285. end;
  286. if popeax then
  287. emit_reg(A_POP,S_L,R_EAX);
  288. if popedx then
  289. emit_reg(A_POP,S_L,R_EDX);
  290. end;
  291. If not(andmod or shrdiv) then
  292. {andmod and shrdiv only use hreg1 (which is already in usedinproc,
  293. since it was acquired with getregister), the others also use both
  294. EAX and EDX (JM)}
  295. Begin
  296. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  297. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  298. End;
  299. clear_location(location);
  300. location.loc:=LOC_REGISTER;
  301. location.register:=hreg1;
  302. end;
  303. end;
  304. {*****************************************************************************
  305. TI386SHLRSHRNODE
  306. *****************************************************************************}
  307. procedure ti386shlshrnode.pass_2;
  308. var
  309. hregister1,hregister2,hregister3,
  310. hregisterhigh,hregisterlow : tregister;
  311. pushed,popecx : boolean;
  312. op : tasmop;
  313. l1,l2,l3 : pasmlabel;
  314. begin
  315. popecx:=false;
  316. secondpass(left);
  317. pushed:=maybe_push(right.registers32,left,is_64bitint(left.resulttype));
  318. secondpass(right);
  319. if pushed then
  320. restore(left,is_64bitint(left.resulttype));
  321. if is_64bitint(left.resulttype) then
  322. begin
  323. { load left operator in a register }
  324. if left.location.loc<>LOC_REGISTER then
  325. begin
  326. if left.location.loc=LOC_CREGISTER then
  327. begin
  328. hregisterlow:=getregister32;
  329. hregisterhigh:=getregister32;
  330. emit_reg_reg(A_MOV,S_L,left.location.registerlow,
  331. hregisterlow);
  332. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,
  333. hregisterlow);
  334. end
  335. else
  336. begin
  337. del_reference(left.location.reference);
  338. hregisterlow:=getregister32;
  339. hregisterhigh:=getregister32;
  340. emit_mov_ref_reg64(left.location.reference,
  341. hregisterlow,
  342. hregisterhigh);
  343. end;
  344. end
  345. else
  346. begin
  347. hregisterlow:=left.location.registerlow;
  348. hregisterhigh:=left.location.registerhigh;
  349. end;
  350. { shifting by a constant directly coded: }
  351. if (right.nodetype=ordconstn) then
  352. begin
  353. { shrd/shl works only for values <=31 !! }
  354. if tordconstnode(right).value>31 then
  355. begin
  356. if nodetype=shln then
  357. begin
  358. emit_reg_reg(A_XOR,S_L,hregisterhigh,
  359. hregisterhigh);
  360. emit_const_reg(A_SHL,S_L,tordconstnode(right).value and 31,
  361. hregisterlow);
  362. end
  363. else
  364. begin
  365. emit_reg_reg(A_XOR,S_L,hregisterlow,
  366. hregisterlow);
  367. emit_const_reg(A_SHR,S_L,tordconstnode(right).value and 31,
  368. hregisterhigh);
  369. end;
  370. location.registerhigh:=hregisterlow;
  371. location.registerlow:=hregisterhigh;
  372. end
  373. else
  374. begin
  375. if nodetype=shln then
  376. begin
  377. emit_const_reg_reg(A_SHLD,S_L,tordconstnode(right).value and 31,
  378. hregisterlow,hregisterhigh);
  379. emit_const_reg(A_SHL,S_L,tordconstnode(right).value and 31,
  380. hregisterlow);
  381. end
  382. else
  383. begin
  384. emit_const_reg_reg(A_SHRD,S_L,tordconstnode(right).value and 31,
  385. hregisterhigh,hregisterlow);
  386. emit_const_reg(A_SHR,S_L,tordconstnode(right).value and 31,
  387. hregisterhigh);
  388. end;
  389. location.registerlow:=hregisterlow;
  390. location.registerhigh:=hregisterhigh;
  391. end;
  392. location.loc:=LOC_REGISTER;
  393. end
  394. else
  395. begin
  396. { load right operators in a register }
  397. if right.location.loc<>LOC_REGISTER then
  398. begin
  399. if right.location.loc=LOC_CREGISTER then
  400. begin
  401. hregister2:=getexplicitregister32(R_ECX);
  402. emit_reg_reg(A_MOV,S_L,right.location.register,
  403. hregister2);
  404. end
  405. else
  406. begin
  407. del_reference(right.location.reference);
  408. hregister2:=getexplicitregister32(R_ECX);
  409. emit_ref_reg(A_MOV,S_L,newreference(right.location.reference),
  410. hregister2);
  411. end;
  412. end
  413. else
  414. hregister2:=right.location.register;
  415. { left operator is already in a register }
  416. { hence are both in a register }
  417. { is it in the case ECX ? }
  418. if (hregisterlow=R_ECX) then
  419. begin
  420. { then only swap }
  421. emit_reg_reg(A_XCHG,S_L,hregisterlow,hregister2);
  422. hregister3:=hregisterlow;
  423. hregisterlow:=hregister2;
  424. hregister2:=hregister3;
  425. end
  426. else if (hregisterhigh=R_ECX) then
  427. begin
  428. { then only swap }
  429. emit_reg_reg(A_XCHG,S_L,hregisterhigh,hregister2);
  430. hregister3:=hregisterhigh;
  431. hregisterhigh:=hregister2;
  432. hregister2:=hregister3;
  433. end
  434. { if second operator not in ECX ? }
  435. else if (hregister2<>R_ECX) then
  436. begin
  437. { ECX occupied then push it }
  438. if not (R_ECX in unused) then
  439. begin
  440. popecx:=true;
  441. emit_reg(A_PUSH,S_L,R_ECX);
  442. end;
  443. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  444. end;
  445. if hregister2 <> R_ECX then
  446. ungetregister32(hregister2);
  447. { the damned shift instructions work only til a count of 32 }
  448. { so we've to do some tricks here }
  449. if nodetype=shln then
  450. begin
  451. getlabel(l1);
  452. getlabel(l2);
  453. getlabel(l3);
  454. emit_const_reg(A_CMP,S_L,64,R_ECX);
  455. emitjmp(C_L,l1);
  456. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  457. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  458. emitjmp(C_None,l3);
  459. emitlab(l1);
  460. emit_const_reg(A_CMP,S_L,32,R_ECX);
  461. emitjmp(C_L,l2);
  462. emit_const_reg(A_SUB,S_L,32,R_ECX);
  463. emit_reg_reg(A_SHL,S_L,R_CL,
  464. hregisterlow);
  465. emit_reg_reg(A_MOV,S_L,hregisterlow,hregisterhigh);
  466. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  467. emitjmp(C_None,l3);
  468. emitlab(l2);
  469. emit_reg_reg_reg(A_SHLD,S_L,R_CL,
  470. hregisterlow,hregisterhigh);
  471. emit_reg_reg(A_SHL,S_L,R_CL,
  472. hregisterlow);
  473. emitlab(l3);
  474. end
  475. else
  476. begin
  477. getlabel(l1);
  478. getlabel(l2);
  479. getlabel(l3);
  480. emit_const_reg(A_CMP,S_L,64,R_ECX);
  481. emitjmp(C_L,l1);
  482. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  483. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  484. emitjmp(C_None,l3);
  485. emitlab(l1);
  486. emit_const_reg(A_CMP,S_L,32,R_ECX);
  487. emitjmp(C_L,l2);
  488. emit_const_reg(A_SUB,S_L,32,R_ECX);
  489. emit_reg_reg(A_SHR,S_L,R_CL,
  490. hregisterhigh);
  491. emit_reg_reg(A_MOV,S_L,hregisterhigh,hregisterlow);
  492. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  493. emitjmp(C_None,l3);
  494. emitlab(l2);
  495. emit_reg_reg_reg(A_SHRD,S_L,R_CL,
  496. hregisterhigh,hregisterlow);
  497. emit_reg_reg(A_SHR,S_L,R_CL,
  498. hregisterhigh);
  499. emitlab(l3);
  500. end;
  501. { maybe put ECX back }
  502. if popecx then
  503. emit_reg(A_POP,S_L,R_ECX)
  504. else ungetregister32(R_ECX);
  505. location.registerlow:=hregisterlow;
  506. location.registerhigh:=hregisterhigh;
  507. end;
  508. end
  509. else
  510. begin
  511. { load left operators in a register }
  512. if left.location.loc<>LOC_REGISTER then
  513. begin
  514. if left.location.loc=LOC_CREGISTER then
  515. begin
  516. hregister1:=getregister32;
  517. emit_reg_reg(A_MOV,S_L,left.location.register,
  518. hregister1);
  519. end
  520. else
  521. begin
  522. del_reference(left.location.reference);
  523. hregister1:=getregister32;
  524. emit_ref_reg(A_MOV,S_L,newreference(left.location.reference),
  525. hregister1);
  526. end;
  527. end
  528. else
  529. hregister1:=left.location.register;
  530. { determine operator }
  531. if nodetype=shln then
  532. op:=A_SHL
  533. else
  534. op:=A_SHR;
  535. { shifting by a constant directly coded: }
  536. if (right.nodetype=ordconstn) then
  537. begin
  538. { l shl 32 should 0 imho, but neither TP nor Delphi do it in this way (FK)
  539. if right.value<=31 then
  540. }
  541. emit_const_reg(op,S_L,tordconstnode(right).value and 31,
  542. hregister1);
  543. {
  544. else
  545. emit_reg_reg(A_XOR,S_L,hregister1,
  546. hregister1);
  547. }
  548. location.loc:=LOC_REGISTER;
  549. location.register:=hregister1;
  550. end
  551. else
  552. begin
  553. { load right operators in a register }
  554. if right.location.loc<>LOC_REGISTER then
  555. begin
  556. if right.location.loc=LOC_CREGISTER then
  557. begin
  558. hregister2:=getexplicitregister32(R_ECX);
  559. emit_reg_reg(A_MOV,S_L,right.location.register,
  560. hregister2);
  561. end
  562. else
  563. begin
  564. del_reference(right.location.reference);
  565. hregister2:=getexplicitregister32(R_ECX);
  566. emit_ref_reg(A_MOV,S_L,newreference(right.location.reference),
  567. hregister2);
  568. end;
  569. end
  570. else
  571. hregister2:=right.location.register;
  572. { left operator is already in a register }
  573. { hence are both in a register }
  574. { is it in the case ECX ? }
  575. if (hregister1=R_ECX) then
  576. begin
  577. { then only swap }
  578. emit_reg_reg(A_XCHG,S_L,hregister1,hregister2);
  579. hregister3:=hregister1;
  580. hregister1:=hregister2;
  581. hregister2:=hregister3;
  582. end
  583. { if second operator not in ECX ? }
  584. else if (hregister2<>R_ECX) then
  585. begin
  586. { ECX occupied then push it }
  587. if not (R_ECX in unused) then
  588. begin
  589. popecx:=true;
  590. emit_reg(A_PUSH,S_L,R_ECX);
  591. end
  592. else
  593. getexplicitregister32(R_ECX);
  594. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  595. end;
  596. ungetregister32(hregister2);
  597. { right operand is in ECX }
  598. emit_reg_reg(op,S_L,R_CL,hregister1);
  599. { maybe ECX back }
  600. if popecx then
  601. emit_reg(A_POP,S_L,R_ECX)
  602. else
  603. ungetregister32(R_ECX);
  604. location.register:=hregister1;
  605. end;
  606. end;
  607. end;
  608. {*****************************************************************************
  609. Ti386UNARYMINUSNODE
  610. *****************************************************************************}
  611. procedure ti386unaryminusnode.pass_2;
  612. {$ifdef SUPPORT_MMX}
  613. procedure do_mmx_neg;
  614. var
  615. op : tasmop;
  616. begin
  617. location.loc:=LOC_MMXREGISTER;
  618. if cs_mmx_saturation in aktlocalswitches then
  619. case mmx_type(resulttype) of
  620. mmxs8bit:
  621. op:=A_PSUBSB;
  622. mmxu8bit:
  623. op:=A_PSUBUSB;
  624. mmxs16bit,mmxfixed16:
  625. op:=A_PSUBSW;
  626. mmxu16bit:
  627. op:=A_PSUBUSW;
  628. end
  629. else
  630. case mmx_type(resulttype) of
  631. mmxs8bit,mmxu8bit:
  632. op:=A_PSUBB;
  633. mmxs16bit,mmxu16bit,mmxfixed16:
  634. op:=A_PSUBW;
  635. mmxs32bit,mmxu32bit:
  636. op:=A_PSUBD;
  637. end;
  638. emit_reg_reg(op,S_NO,location.register,R_MM7);
  639. emit_reg_reg(A_MOVQ,S_NO,R_MM7,location.register);
  640. end;
  641. {$endif}
  642. begin
  643. if is_64bitint(left.resulttype) then
  644. begin
  645. secondpass(left);
  646. clear_location(location);
  647. location.loc:=LOC_REGISTER;
  648. case left.location.loc of
  649. LOC_REGISTER :
  650. begin
  651. location.registerlow:=left.location.registerlow;
  652. location.registerhigh:=left.location.registerhigh;
  653. end;
  654. LOC_CREGISTER :
  655. begin
  656. location.registerlow:=getregister32;
  657. location.registerhigh:=getregister32;
  658. emit_reg_reg(A_MOV,S_L,left.location.registerlow,location.registerlow);
  659. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,location.registerhigh);
  660. end;
  661. LOC_REFERENCE,LOC_MEM :
  662. begin
  663. del_reference(left.location.reference);
  664. location.registerlow:=getregister32;
  665. location.registerhigh:=getregister32;
  666. emit_mov_ref_reg64(left.location.reference,
  667. location.registerlow,
  668. location.registerhigh);
  669. end;
  670. end;
  671. {
  672. emit_reg(A_NEG,S_L,location.registerlow);
  673. emit_const_reg(A_ADC,S_L,0,location.registerhigh);
  674. emit_reg(A_NEG,S_L,location.registerhigh);
  675. }
  676. emit_reg(A_NOT,S_L,location.registerhigh);
  677. emit_reg(A_NEG,S_L,location.registerlow);
  678. emit_const_reg(A_SBB,S_L,-1,location.registerhigh);
  679. end
  680. else
  681. begin
  682. secondpass(left);
  683. location.loc:=LOC_REGISTER;
  684. case left.location.loc of
  685. LOC_REGISTER:
  686. begin
  687. location.register:=left.location.register;
  688. emit_reg(A_NEG,S_L,location.register);
  689. end;
  690. LOC_CREGISTER:
  691. begin
  692. location.register:=getregister32;
  693. emit_reg_reg(A_MOV,S_L,location.register,
  694. location.register);
  695. emit_reg(A_NEG,S_L,location.register);
  696. end;
  697. {$ifdef SUPPORT_MMX}
  698. LOC_MMXREGISTER:
  699. begin
  700. set_location(location,left.location);
  701. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  702. do_mmx_neg;
  703. end;
  704. LOC_CMMXREGISTER:
  705. begin
  706. location.register:=getregistermmx;
  707. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  708. emit_reg_reg(A_MOVQ,S_NO,left.location.register,
  709. location.register);
  710. do_mmx_neg;
  711. end;
  712. {$endif SUPPORT_MMX}
  713. LOC_REFERENCE,LOC_MEM:
  714. begin
  715. del_reference(left.location.reference);
  716. if (left.resulttype^.deftype=floatdef) and
  717. (pfloatdef(left.resulttype)^.typ<>f32bit) then
  718. begin
  719. location.loc:=LOC_FPU;
  720. floatload(pfloatdef(left.resulttype)^.typ,
  721. left.location.reference);
  722. emit_none(A_FCHS,S_NO);
  723. end
  724. {$ifdef SUPPORT_MMX}
  725. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(left.resulttype) then
  726. begin
  727. location.register:=getregistermmx;
  728. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  729. emit_ref_reg(A_MOVQ,S_NO,
  730. newreference(left.location.reference),
  731. location.register);
  732. do_mmx_neg;
  733. end
  734. {$endif SUPPORT_MMX}
  735. else
  736. begin
  737. location.register:=getregister32;
  738. emit_ref_reg(A_MOV,S_L,
  739. newreference(left.location.reference),
  740. location.register);
  741. emit_reg(A_NEG,S_L,location.register);
  742. end;
  743. end;
  744. LOC_FPU:
  745. begin
  746. location.loc:=LOC_FPU;
  747. emit_none(A_FCHS,S_NO);
  748. end;
  749. LOC_CFPUREGISTER:
  750. begin
  751. emit_reg(A_FLD,S_NO,
  752. correct_fpuregister(left.location.register,fpuvaroffset));
  753. inc(fpuvaroffset);
  754. location.loc:=LOC_FPU;
  755. emit_none(A_FCHS,S_NO);
  756. end;
  757. end;
  758. end;
  759. { Here was a problem... }
  760. { Operand to be negated always }
  761. { seems to be converted to signed }
  762. { 32-bit before doing neg!! }
  763. { So this is useless... }
  764. { that's not true: -2^31 gives an overflow error if it is negaded (FK) }
  765. { emitoverflowcheck(p);}
  766. end;
  767. {*****************************************************************************
  768. TI386NOTNODE
  769. *****************************************************************************}
  770. procedure ti386notnode.pass_2;
  771. const
  772. flagsinvers : array[F_E..F_BE] of tresflags =
  773. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  774. F_BE,F_B,F_AE,F_A);
  775. var
  776. hl : pasmlabel;
  777. opsize : topsize;
  778. begin
  779. if is_boolean(resulttype) then
  780. begin
  781. opsize:=def_opsize(resulttype);
  782. { the second pass could change the location of left }
  783. { if it is a register variable, so we've to do }
  784. { this before the case statement }
  785. if left.location.loc in [LOC_REFERENCE,LOC_MEM,
  786. LOC_FLAGS,LOC_REGISTER,LOC_CREGISTER] then
  787. secondpass(left);
  788. case left.location.loc of
  789. LOC_JUMP :
  790. begin
  791. hl:=truelabel;
  792. truelabel:=falselabel;
  793. falselabel:=hl;
  794. secondpass(left);
  795. maketojumpbool(left);
  796. hl:=truelabel;
  797. truelabel:=falselabel;
  798. falselabel:=hl;
  799. end;
  800. LOC_FLAGS :
  801. location.resflags:=flagsinvers[left.location.resflags];
  802. LOC_REGISTER :
  803. begin
  804. {location.register:=left.location.register;
  805. emit_const_reg(A_XOR,opsize,1,location.register);}
  806. location.loc:=LOC_FLAGS;
  807. location.resflags:=F_E;
  808. emit_reg_reg(A_TEST,opsize,
  809. left.location.register,left.location.register);
  810. ungetregister(left.location.register);
  811. end;
  812. LOC_CREGISTER :
  813. begin
  814. clear_location(location);
  815. location.loc:=LOC_REGISTER;
  816. location.register:=def_getreg(resulttype);
  817. emit_reg_reg(A_MOV,opsize,left.location.register,location.register);
  818. emit_reg_reg(A_TEST,opsize,location.register,location.register);
  819. ungetregister(location.register);
  820. location.loc:=LOC_FLAGS;
  821. location.resflags:=F_E;
  822. end;
  823. LOC_REFERENCE,
  824. LOC_MEM :
  825. begin
  826. clear_location(location);
  827. location.loc:=LOC_REGISTER;
  828. del_reference(left.location.reference);
  829. { this was placed before del_ref => internaalerror(10) }
  830. location.register:=def_getreg(resulttype);
  831. emit_ref_reg(A_MOV,opsize,
  832. newreference(left.location.reference),location.register);
  833. emit_reg_reg(A_TEST,opsize,location.register,location.register);
  834. ungetregister(location.register);
  835. location.loc:=LOC_FLAGS;
  836. location.resflags:=F_E;
  837. end;
  838. end;
  839. end
  840. {$ifdef SUPPORT_MMX}
  841. else
  842. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(left.resulttype) then
  843. begin
  844. secondpass(left);
  845. location.loc:=LOC_MMXREGISTER;
  846. { prepare EDI }
  847. getexplicitregister32(R_EDI);
  848. emit_const_reg(A_MOV,S_L,$ffffffff,R_EDI);
  849. { load operand }
  850. case left.location.loc of
  851. LOC_MMXREGISTER:
  852. set_location(location,left.location);
  853. LOC_CMMXREGISTER:
  854. begin
  855. location.register:=getregistermmx;
  856. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  857. end;
  858. LOC_REFERENCE,LOC_MEM:
  859. begin
  860. del_reference(left.location.reference);
  861. location.register:=getregistermmx;
  862. emit_ref_reg(A_MOVQ,S_NO,
  863. newreference(left.location.reference),location.register);
  864. end;
  865. end;
  866. { load mask }
  867. emit_reg_reg(A_MOVD,S_NO,R_EDI,R_MM7);
  868. ungetregister32(R_EDI);
  869. { lower 32 bit }
  870. emit_reg_reg(A_PXOR,S_D,R_MM7,location.register);
  871. { shift mask }
  872. emit_const_reg(A_PSLLQ,S_NO,32,R_MM7);
  873. { higher 32 bit }
  874. emit_reg_reg(A_PXOR,S_D,R_MM7,location.register);
  875. end
  876. {$endif SUPPORT_MMX}
  877. else if is_64bitint(left.resulttype) then
  878. begin
  879. secondpass(left);
  880. clear_location(location);
  881. location.loc:=LOC_REGISTER;
  882. case left.location.loc of
  883. LOC_REGISTER :
  884. begin
  885. location.registerlow:=left.location.registerlow;
  886. location.registerhigh:=left.location.registerhigh;
  887. emit_reg(A_NOT,S_L,location.registerlow);
  888. emit_reg(A_NOT,S_L,location.registerhigh);
  889. end;
  890. LOC_CREGISTER :
  891. begin
  892. location.registerlow:=getregister32;
  893. location.registerhigh:=getregister32;
  894. emit_reg_reg(A_MOV,S_L,left.location.registerlow,location.registerlow);
  895. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,location.registerhigh);
  896. emit_reg(A_NOT,S_L,location.registerlow);
  897. emit_reg(A_NOT,S_L,location.registerhigh);
  898. end;
  899. LOC_REFERENCE,LOC_MEM :
  900. begin
  901. del_reference(left.location.reference);
  902. location.registerlow:=getregister32;
  903. location.registerhigh:=getregister32;
  904. emit_mov_ref_reg64(left.location.reference,
  905. location.registerlow,
  906. location.registerhigh);
  907. emit_reg(A_NOT,S_L,location.registerlow);
  908. emit_reg(A_NOT,S_L,location.registerhigh);
  909. end;
  910. end;
  911. end
  912. else
  913. begin
  914. secondpass(left);
  915. clear_location(location);
  916. location.loc:=LOC_REGISTER;
  917. case left.location.loc of
  918. LOC_REGISTER :
  919. begin
  920. location.register:=left.location.register;
  921. emit_reg(A_NOT,S_L,location.register);
  922. end;
  923. LOC_CREGISTER :
  924. begin
  925. location.register:=getregister32;
  926. emit_reg_reg(A_MOV,S_L,left.location.register,location.register);
  927. emit_reg(A_NOT,S_L,location.register);
  928. end;
  929. LOC_REFERENCE,LOC_MEM :
  930. begin
  931. del_reference(left.location.reference);
  932. location.register:=getregister32;
  933. emit_ref_reg(A_MOV,S_L,
  934. newreference(left.location.reference),location.register);
  935. emit_reg(A_NOT,S_L,location.register);
  936. end;
  937. end;
  938. end;
  939. end;
  940. begin
  941. cmoddivnode:=ti386moddivnode;
  942. cshlshrnode:=ti386shlshrnode;
  943. cunaryminusnode:=ti386unaryminusnode;
  944. cnotnode:=ti386notnode;
  945. end.
  946. {
  947. $Log$
  948. Revision 1.5 2000-10-31 22:02:56 peter
  949. * symtable splitted, no real code changes
  950. Revision 1.4 2000/10/19 16:26:52 jonas
  951. * fixed wrong regalloc info for secondmoddiv ("merged", also small
  952. correction made afterwards in fixes branch)
  953. Revision 1.3 2000/10/17 15:41:48 jonas
  954. * fixed stupid error in previous commit :/
  955. Revision 1.1 2000/10/15 09:33:32 peter
  956. * moved n386*.pas to i386/ cpu_target dir
  957. Revision 1.4 2000/10/14 10:14:49 peter
  958. * moehrendorf oct 2000 rewrite
  959. Revision 1.3 2000/09/30 16:08:45 peter
  960. * more cg11 updates
  961. Revision 1.2 2000/09/24 15:06:18 peter
  962. * use defines.inc
  963. Revision 1.1 2000/09/22 22:24:37 florian
  964. * initial revision
  965. }