n386mat.pas 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. 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 and (hreg1 <> R_EDX) 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. if hreg1 <> R_EDX then
  281. ungetregister32(hreg1);
  282. hreg1 := R_EDX
  283. End;
  284. if not popeax and (hreg1 <> R_EAX)then
  285. ungetregister(R_EAX);
  286. end;
  287. if popeax then
  288. emit_reg(A_POP,S_L,R_EAX);
  289. if popedx then
  290. emit_reg(A_POP,S_L,R_EDX);
  291. end;
  292. If not(andmod or shrdiv) then
  293. {andmod and shrdiv only use hreg1 (which is already in usedinproc,
  294. since it was acquired with getregister), the others also use both
  295. EAX and EDX (JM)}
  296. Begin
  297. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  298. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  299. End;
  300. clear_location(location);
  301. location.loc:=LOC_REGISTER;
  302. location.register:=hreg1;
  303. end;
  304. end;
  305. {*****************************************************************************
  306. TI386SHLRSHRNODE
  307. *****************************************************************************}
  308. procedure ti386shlshrnode.pass_2;
  309. var
  310. hregister1,hregister2,hregister3,
  311. hregisterhigh,hregisterlow : tregister;
  312. pushed,popecx : boolean;
  313. op : tasmop;
  314. l1,l2,l3 : pasmlabel;
  315. begin
  316. popecx:=false;
  317. secondpass(left);
  318. pushed:=maybe_push(right.registers32,left,is_64bitint(left.resulttype));
  319. secondpass(right);
  320. if pushed then
  321. restore(left,is_64bitint(left.resulttype));
  322. if is_64bitint(left.resulttype) then
  323. begin
  324. { load left operator in a register }
  325. if left.location.loc<>LOC_REGISTER then
  326. begin
  327. if left.location.loc=LOC_CREGISTER then
  328. begin
  329. hregisterlow:=getregister32;
  330. hregisterhigh:=getregister32;
  331. emit_reg_reg(A_MOV,S_L,left.location.registerlow,
  332. hregisterlow);
  333. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,
  334. hregisterlow);
  335. end
  336. else
  337. begin
  338. del_reference(left.location.reference);
  339. hregisterlow:=getregister32;
  340. hregisterhigh:=getregister32;
  341. emit_mov_ref_reg64(left.location.reference,
  342. hregisterlow,
  343. hregisterhigh);
  344. end;
  345. end
  346. else
  347. begin
  348. hregisterlow:=left.location.registerlow;
  349. hregisterhigh:=left.location.registerhigh;
  350. end;
  351. { shifting by a constant directly coded: }
  352. if (right.nodetype=ordconstn) then
  353. begin
  354. { shrd/shl works only for values <=31 !! }
  355. if tordconstnode(right).value>31 then
  356. begin
  357. if nodetype=shln then
  358. begin
  359. emit_reg_reg(A_XOR,S_L,hregisterhigh,
  360. hregisterhigh);
  361. emit_const_reg(A_SHL,S_L,tordconstnode(right).value and 31,
  362. hregisterlow);
  363. end
  364. else
  365. begin
  366. emit_reg_reg(A_XOR,S_L,hregisterlow,
  367. hregisterlow);
  368. emit_const_reg(A_SHR,S_L,tordconstnode(right).value and 31,
  369. hregisterhigh);
  370. end;
  371. location.registerhigh:=hregisterlow;
  372. location.registerlow:=hregisterhigh;
  373. end
  374. else
  375. begin
  376. if nodetype=shln then
  377. begin
  378. emit_const_reg_reg(A_SHLD,S_L,tordconstnode(right).value and 31,
  379. hregisterlow,hregisterhigh);
  380. emit_const_reg(A_SHL,S_L,tordconstnode(right).value and 31,
  381. hregisterlow);
  382. end
  383. else
  384. begin
  385. emit_const_reg_reg(A_SHRD,S_L,tordconstnode(right).value and 31,
  386. hregisterhigh,hregisterlow);
  387. emit_const_reg(A_SHR,S_L,tordconstnode(right).value and 31,
  388. hregisterhigh);
  389. end;
  390. location.registerlow:=hregisterlow;
  391. location.registerhigh:=hregisterhigh;
  392. end;
  393. location.loc:=LOC_REGISTER;
  394. end
  395. else
  396. begin
  397. { load right operators in a register }
  398. if right.location.loc<>LOC_REGISTER then
  399. begin
  400. if right.location.loc=LOC_CREGISTER then
  401. begin
  402. hregister2:=getexplicitregister32(R_ECX);
  403. emit_reg_reg(A_MOV,S_L,right.location.register,
  404. hregister2);
  405. end
  406. else
  407. begin
  408. del_reference(right.location.reference);
  409. hregister2:=getexplicitregister32(R_ECX);
  410. emit_ref_reg(A_MOV,S_L,newreference(right.location.reference),
  411. hregister2);
  412. end;
  413. end
  414. else
  415. hregister2:=right.location.register;
  416. { left operator is already in a register }
  417. { hence are both in a register }
  418. { is it in the case ECX ? }
  419. if (hregisterlow=R_ECX) then
  420. begin
  421. { then only swap }
  422. emit_reg_reg(A_XCHG,S_L,hregisterlow,hregister2);
  423. hregister3:=hregisterlow;
  424. hregisterlow:=hregister2;
  425. hregister2:=hregister3;
  426. end
  427. else if (hregisterhigh=R_ECX) then
  428. begin
  429. { then only swap }
  430. emit_reg_reg(A_XCHG,S_L,hregisterhigh,hregister2);
  431. hregister3:=hregisterhigh;
  432. hregisterhigh:=hregister2;
  433. hregister2:=hregister3;
  434. end
  435. { if second operator not in ECX ? }
  436. else if (hregister2<>R_ECX) then
  437. begin
  438. { ECX occupied then push it }
  439. if not (R_ECX in unused) then
  440. begin
  441. popecx:=true;
  442. emit_reg(A_PUSH,S_L,R_ECX);
  443. end;
  444. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  445. end;
  446. if hregister2 <> R_ECX then
  447. ungetregister32(hregister2);
  448. { the damned shift instructions work only til a count of 32 }
  449. { so we've to do some tricks here }
  450. if nodetype=shln then
  451. begin
  452. getlabel(l1);
  453. getlabel(l2);
  454. getlabel(l3);
  455. emit_const_reg(A_CMP,S_L,64,R_ECX);
  456. emitjmp(C_L,l1);
  457. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  458. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  459. emitjmp(C_None,l3);
  460. emitlab(l1);
  461. emit_const_reg(A_CMP,S_L,32,R_ECX);
  462. emitjmp(C_L,l2);
  463. emit_const_reg(A_SUB,S_L,32,R_ECX);
  464. emit_reg_reg(A_SHL,S_L,R_CL,
  465. hregisterlow);
  466. emit_reg_reg(A_MOV,S_L,hregisterlow,hregisterhigh);
  467. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  468. emitjmp(C_None,l3);
  469. emitlab(l2);
  470. emit_reg_reg_reg(A_SHLD,S_L,R_CL,
  471. hregisterlow,hregisterhigh);
  472. emit_reg_reg(A_SHL,S_L,R_CL,
  473. hregisterlow);
  474. emitlab(l3);
  475. end
  476. else
  477. begin
  478. getlabel(l1);
  479. getlabel(l2);
  480. getlabel(l3);
  481. emit_const_reg(A_CMP,S_L,64,R_ECX);
  482. emitjmp(C_L,l1);
  483. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  484. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  485. emitjmp(C_None,l3);
  486. emitlab(l1);
  487. emit_const_reg(A_CMP,S_L,32,R_ECX);
  488. emitjmp(C_L,l2);
  489. emit_const_reg(A_SUB,S_L,32,R_ECX);
  490. emit_reg_reg(A_SHR,S_L,R_CL,
  491. hregisterhigh);
  492. emit_reg_reg(A_MOV,S_L,hregisterhigh,hregisterlow);
  493. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  494. emitjmp(C_None,l3);
  495. emitlab(l2);
  496. emit_reg_reg_reg(A_SHRD,S_L,R_CL,
  497. hregisterhigh,hregisterlow);
  498. emit_reg_reg(A_SHR,S_L,R_CL,
  499. hregisterhigh);
  500. emitlab(l3);
  501. end;
  502. { maybe put ECX back }
  503. if popecx then
  504. emit_reg(A_POP,S_L,R_ECX)
  505. else ungetregister32(R_ECX);
  506. location.registerlow:=hregisterlow;
  507. location.registerhigh:=hregisterhigh;
  508. end;
  509. end
  510. else
  511. begin
  512. { load left operators in a register }
  513. if left.location.loc<>LOC_REGISTER then
  514. begin
  515. if left.location.loc=LOC_CREGISTER then
  516. begin
  517. hregister1:=getregister32;
  518. emit_reg_reg(A_MOV,S_L,left.location.register,
  519. hregister1);
  520. end
  521. else
  522. begin
  523. del_reference(left.location.reference);
  524. hregister1:=getregister32;
  525. emit_ref_reg(A_MOV,S_L,newreference(left.location.reference),
  526. hregister1);
  527. end;
  528. end
  529. else
  530. hregister1:=left.location.register;
  531. { determine operator }
  532. if nodetype=shln then
  533. op:=A_SHL
  534. else
  535. op:=A_SHR;
  536. { shifting by a constant directly coded: }
  537. if (right.nodetype=ordconstn) then
  538. begin
  539. { l shl 32 should 0 imho, but neither TP nor Delphi do it in this way (FK)
  540. if right.value<=31 then
  541. }
  542. emit_const_reg(op,S_L,tordconstnode(right).value and 31,
  543. hregister1);
  544. {
  545. else
  546. emit_reg_reg(A_XOR,S_L,hregister1,
  547. hregister1);
  548. }
  549. location.loc:=LOC_REGISTER;
  550. location.register:=hregister1;
  551. end
  552. else
  553. begin
  554. { load right operators in a register }
  555. if right.location.loc<>LOC_REGISTER then
  556. begin
  557. if right.location.loc=LOC_CREGISTER then
  558. begin
  559. hregister2:=getexplicitregister32(R_ECX);
  560. emit_reg_reg(A_MOV,S_L,right.location.register,
  561. hregister2);
  562. end
  563. else
  564. begin
  565. del_reference(right.location.reference);
  566. hregister2:=getexplicitregister32(R_ECX);
  567. emit_ref_reg(A_MOV,S_L,newreference(right.location.reference),
  568. hregister2);
  569. end;
  570. end
  571. else
  572. hregister2:=right.location.register;
  573. { left operator is already in a register }
  574. { hence are both in a register }
  575. { is it in the case ECX ? }
  576. if (hregister1=R_ECX) then
  577. begin
  578. { then only swap }
  579. emit_reg_reg(A_XCHG,S_L,hregister1,hregister2);
  580. hregister3:=hregister1;
  581. hregister1:=hregister2;
  582. hregister2:=hregister3;
  583. end
  584. { if second operator not in ECX ? }
  585. else if (hregister2<>R_ECX) then
  586. begin
  587. { ECX occupied then push it }
  588. if not (R_ECX in unused) then
  589. begin
  590. popecx:=true;
  591. emit_reg(A_PUSH,S_L,R_ECX);
  592. end
  593. else
  594. getexplicitregister32(R_ECX);
  595. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  596. end;
  597. ungetregister32(hregister2);
  598. { right operand is in ECX }
  599. emit_reg_reg(op,S_L,R_CL,hregister1);
  600. { maybe ECX back }
  601. if popecx then
  602. emit_reg(A_POP,S_L,R_ECX)
  603. else
  604. ungetregister32(R_ECX);
  605. location.register:=hregister1;
  606. end;
  607. end;
  608. end;
  609. {*****************************************************************************
  610. Ti386UNARYMINUSNODE
  611. *****************************************************************************}
  612. procedure ti386unaryminusnode.pass_2;
  613. {$ifdef SUPPORT_MMX}
  614. procedure do_mmx_neg;
  615. var
  616. op : tasmop;
  617. begin
  618. location.loc:=LOC_MMXREGISTER;
  619. if cs_mmx_saturation in aktlocalswitches then
  620. case mmx_type(resulttype) of
  621. mmxs8bit:
  622. op:=A_PSUBSB;
  623. mmxu8bit:
  624. op:=A_PSUBUSB;
  625. mmxs16bit,mmxfixed16:
  626. op:=A_PSUBSW;
  627. mmxu16bit:
  628. op:=A_PSUBUSW;
  629. end
  630. else
  631. case mmx_type(resulttype) of
  632. mmxs8bit,mmxu8bit:
  633. op:=A_PSUBB;
  634. mmxs16bit,mmxu16bit,mmxfixed16:
  635. op:=A_PSUBW;
  636. mmxs32bit,mmxu32bit:
  637. op:=A_PSUBD;
  638. end;
  639. emit_reg_reg(op,S_NO,location.register,R_MM7);
  640. emit_reg_reg(A_MOVQ,S_NO,R_MM7,location.register);
  641. end;
  642. {$endif}
  643. begin
  644. if is_64bitint(left.resulttype) then
  645. begin
  646. secondpass(left);
  647. clear_location(location);
  648. location.loc:=LOC_REGISTER;
  649. case left.location.loc of
  650. LOC_REGISTER :
  651. begin
  652. location.registerlow:=left.location.registerlow;
  653. location.registerhigh:=left.location.registerhigh;
  654. end;
  655. LOC_CREGISTER :
  656. begin
  657. location.registerlow:=getregister32;
  658. location.registerhigh:=getregister32;
  659. emit_reg_reg(A_MOV,S_L,left.location.registerlow,location.registerlow);
  660. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,location.registerhigh);
  661. end;
  662. LOC_REFERENCE,LOC_MEM :
  663. begin
  664. del_reference(left.location.reference);
  665. location.registerlow:=getregister32;
  666. location.registerhigh:=getregister32;
  667. emit_mov_ref_reg64(left.location.reference,
  668. location.registerlow,
  669. location.registerhigh);
  670. end;
  671. end;
  672. {
  673. emit_reg(A_NEG,S_L,location.registerlow);
  674. emit_const_reg(A_ADC,S_L,0,location.registerhigh);
  675. emit_reg(A_NEG,S_L,location.registerhigh);
  676. }
  677. emit_reg(A_NOT,S_L,location.registerhigh);
  678. emit_reg(A_NEG,S_L,location.registerlow);
  679. emit_const_reg(A_SBB,S_L,-1,location.registerhigh);
  680. end
  681. else
  682. begin
  683. secondpass(left);
  684. location.loc:=LOC_REGISTER;
  685. case left.location.loc of
  686. LOC_REGISTER:
  687. begin
  688. location.register:=left.location.register;
  689. emit_reg(A_NEG,S_L,location.register);
  690. end;
  691. LOC_CREGISTER:
  692. begin
  693. location.register:=getregister32;
  694. emit_reg_reg(A_MOV,S_L,location.register,
  695. location.register);
  696. emit_reg(A_NEG,S_L,location.register);
  697. end;
  698. {$ifdef SUPPORT_MMX}
  699. LOC_MMXREGISTER:
  700. begin
  701. set_location(location,left.location);
  702. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  703. do_mmx_neg;
  704. end;
  705. LOC_CMMXREGISTER:
  706. begin
  707. location.register:=getregistermmx;
  708. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  709. emit_reg_reg(A_MOVQ,S_NO,left.location.register,
  710. location.register);
  711. do_mmx_neg;
  712. end;
  713. {$endif SUPPORT_MMX}
  714. LOC_REFERENCE,LOC_MEM:
  715. begin
  716. del_reference(left.location.reference);
  717. if (left.resulttype^.deftype=floatdef) and
  718. (pfloatdef(left.resulttype)^.typ<>f32bit) then
  719. begin
  720. location.loc:=LOC_FPU;
  721. floatload(pfloatdef(left.resulttype)^.typ,
  722. left.location.reference);
  723. emit_none(A_FCHS,S_NO);
  724. end
  725. {$ifdef SUPPORT_MMX}
  726. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(left.resulttype) then
  727. begin
  728. location.register:=getregistermmx;
  729. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  730. emit_ref_reg(A_MOVQ,S_NO,
  731. newreference(left.location.reference),
  732. location.register);
  733. do_mmx_neg;
  734. end
  735. {$endif SUPPORT_MMX}
  736. else
  737. begin
  738. location.register:=getregister32;
  739. emit_ref_reg(A_MOV,S_L,
  740. newreference(left.location.reference),
  741. location.register);
  742. emit_reg(A_NEG,S_L,location.register);
  743. end;
  744. end;
  745. LOC_FPU:
  746. begin
  747. location.loc:=LOC_FPU;
  748. emit_none(A_FCHS,S_NO);
  749. end;
  750. LOC_CFPUREGISTER:
  751. begin
  752. emit_reg(A_FLD,S_NO,
  753. correct_fpuregister(left.location.register,fpuvaroffset));
  754. inc(fpuvaroffset);
  755. location.loc:=LOC_FPU;
  756. emit_none(A_FCHS,S_NO);
  757. end;
  758. end;
  759. end;
  760. { Here was a problem... }
  761. { Operand to be negated always }
  762. { seems to be converted to signed }
  763. { 32-bit before doing neg!! }
  764. { So this is useless... }
  765. { that's not true: -2^31 gives an overflow error if it is negaded (FK) }
  766. { emitoverflowcheck(p);}
  767. end;
  768. {*****************************************************************************
  769. TI386NOTNODE
  770. *****************************************************************************}
  771. procedure ti386notnode.pass_2;
  772. const
  773. flagsinvers : array[F_E..F_BE] of tresflags =
  774. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  775. F_BE,F_B,F_AE,F_A);
  776. var
  777. hl : pasmlabel;
  778. opsize : topsize;
  779. begin
  780. if is_boolean(resulttype) then
  781. begin
  782. opsize:=def_opsize(resulttype);
  783. { the second pass could change the location of left }
  784. { if it is a register variable, so we've to do }
  785. { this before the case statement }
  786. if left.location.loc in [LOC_REFERENCE,LOC_MEM,
  787. LOC_FLAGS,LOC_REGISTER,LOC_CREGISTER] then
  788. secondpass(left);
  789. case left.location.loc of
  790. LOC_JUMP :
  791. begin
  792. hl:=truelabel;
  793. truelabel:=falselabel;
  794. falselabel:=hl;
  795. secondpass(left);
  796. maketojumpbool(left);
  797. hl:=truelabel;
  798. truelabel:=falselabel;
  799. falselabel:=hl;
  800. end;
  801. LOC_FLAGS :
  802. location.resflags:=flagsinvers[left.location.resflags];
  803. LOC_REGISTER :
  804. begin
  805. {location.register:=left.location.register;
  806. emit_const_reg(A_XOR,opsize,1,location.register);}
  807. location.loc:=LOC_FLAGS;
  808. location.resflags:=F_E;
  809. emit_reg_reg(A_TEST,opsize,
  810. left.location.register,left.location.register);
  811. ungetregister(left.location.register);
  812. end;
  813. LOC_CREGISTER :
  814. begin
  815. clear_location(location);
  816. location.loc:=LOC_REGISTER;
  817. location.register:=def_getreg(resulttype);
  818. emit_reg_reg(A_MOV,opsize,left.location.register,location.register);
  819. emit_reg_reg(A_TEST,opsize,location.register,location.register);
  820. ungetregister(location.register);
  821. location.loc:=LOC_FLAGS;
  822. location.resflags:=F_E;
  823. end;
  824. LOC_REFERENCE,
  825. LOC_MEM :
  826. begin
  827. clear_location(location);
  828. location.loc:=LOC_REGISTER;
  829. del_reference(left.location.reference);
  830. { this was placed before del_ref => internaalerror(10) }
  831. location.register:=def_getreg(resulttype);
  832. emit_ref_reg(A_MOV,opsize,
  833. newreference(left.location.reference),location.register);
  834. emit_reg_reg(A_TEST,opsize,location.register,location.register);
  835. ungetregister(location.register);
  836. location.loc:=LOC_FLAGS;
  837. location.resflags:=F_E;
  838. end;
  839. end;
  840. end
  841. {$ifdef SUPPORT_MMX}
  842. else
  843. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(left.resulttype) then
  844. begin
  845. secondpass(left);
  846. location.loc:=LOC_MMXREGISTER;
  847. { prepare EDI }
  848. getexplicitregister32(R_EDI);
  849. emit_const_reg(A_MOV,S_L,$ffffffff,R_EDI);
  850. { load operand }
  851. case left.location.loc of
  852. LOC_MMXREGISTER:
  853. set_location(location,left.location);
  854. LOC_CMMXREGISTER:
  855. begin
  856. location.register:=getregistermmx;
  857. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  858. end;
  859. LOC_REFERENCE,LOC_MEM:
  860. begin
  861. del_reference(left.location.reference);
  862. location.register:=getregistermmx;
  863. emit_ref_reg(A_MOVQ,S_NO,
  864. newreference(left.location.reference),location.register);
  865. end;
  866. end;
  867. { load mask }
  868. emit_reg_reg(A_MOVD,S_NO,R_EDI,R_MM7);
  869. ungetregister32(R_EDI);
  870. { lower 32 bit }
  871. emit_reg_reg(A_PXOR,S_D,R_MM7,location.register);
  872. { shift mask }
  873. emit_const_reg(A_PSLLQ,S_NO,32,R_MM7);
  874. { higher 32 bit }
  875. emit_reg_reg(A_PXOR,S_D,R_MM7,location.register);
  876. end
  877. {$endif SUPPORT_MMX}
  878. else if is_64bitint(left.resulttype) then
  879. begin
  880. secondpass(left);
  881. clear_location(location);
  882. location.loc:=LOC_REGISTER;
  883. case left.location.loc of
  884. LOC_REGISTER :
  885. begin
  886. location.registerlow:=left.location.registerlow;
  887. location.registerhigh:=left.location.registerhigh;
  888. emit_reg(A_NOT,S_L,location.registerlow);
  889. emit_reg(A_NOT,S_L,location.registerhigh);
  890. end;
  891. LOC_CREGISTER :
  892. begin
  893. location.registerlow:=getregister32;
  894. location.registerhigh:=getregister32;
  895. emit_reg_reg(A_MOV,S_L,left.location.registerlow,location.registerlow);
  896. emit_reg_reg(A_MOV,S_L,left.location.registerhigh,location.registerhigh);
  897. emit_reg(A_NOT,S_L,location.registerlow);
  898. emit_reg(A_NOT,S_L,location.registerhigh);
  899. end;
  900. LOC_REFERENCE,LOC_MEM :
  901. begin
  902. del_reference(left.location.reference);
  903. location.registerlow:=getregister32;
  904. location.registerhigh:=getregister32;
  905. emit_mov_ref_reg64(left.location.reference,
  906. location.registerlow,
  907. location.registerhigh);
  908. emit_reg(A_NOT,S_L,location.registerlow);
  909. emit_reg(A_NOT,S_L,location.registerhigh);
  910. end;
  911. end;
  912. end
  913. else
  914. begin
  915. secondpass(left);
  916. clear_location(location);
  917. location.loc:=LOC_REGISTER;
  918. case left.location.loc of
  919. LOC_REGISTER :
  920. begin
  921. location.register:=left.location.register;
  922. emit_reg(A_NOT,S_L,location.register);
  923. end;
  924. LOC_CREGISTER :
  925. begin
  926. location.register:=getregister32;
  927. emit_reg_reg(A_MOV,S_L,left.location.register,location.register);
  928. emit_reg(A_NOT,S_L,location.register);
  929. end;
  930. LOC_REFERENCE,LOC_MEM :
  931. begin
  932. del_reference(left.location.reference);
  933. location.register:=getregister32;
  934. emit_ref_reg(A_MOV,S_L,
  935. newreference(left.location.reference),location.register);
  936. emit_reg(A_NOT,S_L,location.register);
  937. end;
  938. end;
  939. end;
  940. end;
  941. begin
  942. cmoddivnode:=ti386moddivnode;
  943. cshlshrnode:=ti386shlshrnode;
  944. cunaryminusnode:=ti386unaryminusnode;
  945. cnotnode:=ti386notnode;
  946. end.
  947. {
  948. $Log$
  949. Revision 1.7 2000-11-29 00:30:48 florian
  950. * unused units removed from uses clause
  951. * some changes for widestrings
  952. Revision 1.6 2000/11/20 14:05:50 jonas
  953. * fixed bug in my changes to fix the regalloc info for div/mod ("merged")
  954. Revision 1.5 2000/10/31 22:02:56 peter
  955. * symtable splitted, no real code changes
  956. Revision 1.4 2000/10/19 16:26:52 jonas
  957. * fixed wrong regalloc info for secondmoddiv ("merged", also small
  958. correction made afterwards in fixes branch)
  959. Revision 1.3 2000/10/17 15:41:48 jonas
  960. * fixed stupid error in previous commit :/
  961. Revision 1.1 2000/10/15 09:33:32 peter
  962. * moved n386*.pas to i386/ cpu_target dir
  963. Revision 1.4 2000/10/14 10:14:49 peter
  964. * moehrendorf oct 2000 rewrite
  965. Revision 1.3 2000/09/30 16:08:45 peter
  966. * more cg11 updates
  967. Revision 1.2 2000/09/24 15:06:18 peter
  968. * use defines.inc
  969. Revision 1.1 2000/09/22 22:24:37 florian
  970. * initial revision
  971. }