n386mat.pas 42 KB

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