cg386mat.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 cg386mat;
  19. interface
  20. uses
  21. tree;
  22. procedure secondmoddiv(var p : ptree);
  23. procedure secondshlshr(var p : ptree);
  24. procedure secondunaryminus(var p : ptree);
  25. procedure secondnot(var p : ptree);
  26. implementation
  27. uses
  28. globtype,systems,
  29. cobjects,verbose,globals,
  30. symconst,symtable,aasm,types,
  31. hcodegen,temp_gen,pass_2,
  32. cpubase,cpuasm,
  33. {$ifdef dummy}
  34. end { this overcomes the annoying highlighting problem in my TP IDE,
  35. the IDE assumes i386asm start a asm block (FK) }
  36. {$endif}
  37. cgai386,tgeni386;
  38. {*****************************************************************************
  39. SecondModDiv
  40. *****************************************************************************}
  41. procedure secondmoddiv(var p : ptree);
  42. var
  43. hreg1, hreg2 : tregister;
  44. shrdiv, andmod, pushed,popeax,popedx : boolean;
  45. power : longint;
  46. hl : pasmlabel;
  47. hloc : tlocation;
  48. pushedreg : tpushed;
  49. typename,opname : string[6];
  50. begin
  51. shrdiv := false;
  52. andmod := false;
  53. secondpass(p^.left);
  54. pushed:=maybe_push(p^.right^.registers32,p^.left,is_64bitint(p^.left^.resulttype));
  55. secondpass(p^.right);
  56. if pushed then
  57. restore(p^.left,is_64bitint(p^.left^.resulttype));
  58. set_location(p^.location,p^.left^.location);
  59. if is_64bitint(p^.resulttype) then
  60. begin
  61. { save p^.lcoation, because we change it now }
  62. set_location(hloc,p^.location);
  63. release_qword_loc(p^.location);
  64. release_qword_loc(p^.right^.location);
  65. p^.location.registerlow:=getexplicitregister32(R_EAX);
  66. p^.location.registerhigh:=getexplicitregister32(R_EDX);
  67. pushusedregisters(pushedreg,$ff
  68. and not($80 shr byte(p^.location.registerlow))
  69. and not($80 shr byte(p^.location.registerhigh)));
  70. { the left operand is in hloc, because the
  71. location of left is p^.location but p^.location
  72. is already destroyed
  73. }
  74. emit_pushq_loc(hloc);
  75. clear_location(hloc);
  76. emit_pushq_loc(p^.right^.location);
  77. if porddef(p^.resulttype)^.typ=u64bit then
  78. typename:='QWORD'
  79. else
  80. typename:='INT64';
  81. if p^.treetype=divn then
  82. opname:='DIV_'
  83. else
  84. opname:='MOD_';
  85. emitcall('FPC_'+opname+typename);
  86. emit_reg_reg(A_MOV,S_L,R_EAX,p^.location.registerlow);
  87. emit_reg_reg(A_MOV,S_L,R_EDX,p^.location.registerhigh);
  88. popusedregisters(pushedreg);
  89. p^.location.loc:=LOC_REGISTER;
  90. end
  91. else
  92. begin
  93. { put numerator in register }
  94. if p^.left^.location.loc<>LOC_REGISTER then
  95. begin
  96. if p^.left^.location.loc=LOC_CREGISTER then
  97. begin
  98. hreg1:=getregister32;
  99. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hreg1);
  100. end
  101. else
  102. begin
  103. del_reference(p^.left^.location.reference);
  104. hreg1:=getregister32;
  105. emit_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  106. hreg1);
  107. end;
  108. clear_location(p^.left^.location);
  109. p^.left^.location.loc:=LOC_REGISTER;
  110. p^.left^.location.register:=hreg1;
  111. end
  112. else hreg1:=p^.left^.location.register;
  113. if (p^.treetype=divn) and (p^.right^.treetype=ordconstn) and
  114. ispowerof2(p^.right^.value,power) then
  115. Begin
  116. shrdiv := true;
  117. {for signed numbers, the numerator must be adjusted before the
  118. shift instruction, but not wih unsigned numbers! Otherwise,
  119. "Cardinal($ffffffff) div 16" overflows! (JM)}
  120. If is_signed(p^.left^.resulttype) Then
  121. Begin
  122. {$ifdef newOptimizations}
  123. If (aktOptProcessor <> class386) and
  124. not(CS_LittleSize in aktglobalswitches) then
  125. { use a sequence without jumps, saw this in
  126. comp.compilers (JM) }
  127. begin
  128. { no jumps, but more operations }
  129. if (hreg1 = R_EAX) and
  130. (R_EDX in unused) then
  131. begin
  132. hreg2 := getexplicitregister32(R_EDX);
  133. emit_none(A_CDQ,S_NO);
  134. end
  135. else
  136. begin
  137. {$ifndef noAllocEdi}
  138. getexplicitregister32(R_EDI);
  139. {$endif noAllocEdi}
  140. hreg2 := R_EDI;
  141. emit_reg_reg(A_MOV,S_L,hreg1,R_EDI);
  142. { if the left value is signed, R_EDI := $ffffffff,
  143. otherwise 0 }
  144. emit_const_reg(A_SAR,S_L,31,R_EDI);
  145. { if signed, R_EDI := right value-1, otherwise 0 }
  146. end;
  147. emit_const_reg(A_AND,S_L,p^.right^.value-1,hreg2);
  148. { add to the left value }
  149. emit_reg_reg(A_ADD,S_L,hreg2,hreg1);
  150. { release EDX if we used it }
  151. {$ifndef noAllocEdi}
  152. { also releas EDI }
  153. ungetregister32(hreg2);
  154. {$else noAllocEdi}
  155. if (hreg2 = R_EDX) then
  156. ungetregister32(hreg2);
  157. {$endif noAllocEdi}
  158. { do the shift }
  159. emit_const_reg(A_SAR,S_L,power,hreg1);
  160. end
  161. else
  162. {$endif newOptimizations}
  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,p^.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 (p^.treetype=modn) and (p^.right^.treetype=ordconstn) and
  181. ispowerof2(p^.right^.value,power) and Not(is_signed(p^.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,p^.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. {$ifndef noAllocEdi}
  194. getexplicitregister32(R_EDI);
  195. {$endif noAllocEdi}
  196. if (p^.right^.location.loc<>LOC_REGISTER) and
  197. (p^.right^.location.loc<>LOC_CREGISTER) then
  198. begin
  199. del_reference(p^.right^.location.reference);
  200. p^.left^.location.loc:=LOC_REGISTER;
  201. emit_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),R_EDI);
  202. end
  203. else
  204. begin
  205. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,R_EDI);
  206. ungetregister32(p^.right^.location.register);
  207. end;
  208. popedx:=false;
  209. popeax:=false;
  210. if hreg1=R_EDX then
  211. begin
  212. if not(R_EAX in unused) then
  213. begin
  214. emit_reg(A_PUSH,S_L,R_EAX);
  215. popeax:=true;
  216. end;
  217. emit_reg_reg(A_MOV,S_L,R_EDX,R_EAX);
  218. end
  219. else
  220. begin
  221. if not(R_EDX in unused) then
  222. begin
  223. emit_reg(A_PUSH,S_L,R_EDX);
  224. popedx:=true;
  225. end;
  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. emit_reg_reg(A_MOV,S_L,hreg1,R_EAX);
  234. end;
  235. end;
  236. { sign extension depends on the left type }
  237. if porddef(p^.left^.resulttype)^.typ=u32bit then
  238. emit_reg_reg(A_XOR,S_L,R_EDX,R_EDX)
  239. else
  240. emit_none(A_CDQ,S_NO);
  241. { division depends on the right type }
  242. if porddef(p^.right^.resulttype)^.typ=u32bit then
  243. emit_reg(A_DIV,S_L,R_EDI)
  244. else
  245. emit_reg(A_IDIV,S_L,R_EDI);
  246. {$ifndef noAllocEdi}
  247. ungetregister32(R_EDI);
  248. {$endif noAllocEdi}
  249. if p^.treetype=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. hreg1 := getexplicitregister32(R_EAX);
  263. { I don't think it's possible that now hreg1 <> R_EAX
  264. since popeax is false, but for all certainty I do
  265. support that situation (JM)}
  266. if hreg1 <> R_EAX then
  267. emit_reg_reg(A_MOV,S_L,R_EAX,hreg1);
  268. end;
  269. end
  270. else
  271. {if we did the mod by an "and", the result is in hreg1 and
  272. EDX certainly hasn't been pushed (JM)}
  273. if not(andmod) Then
  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. {Get rid of the unnecessary hreg1 if possible (same as with
  282. EAX in divn) (JM)}
  283. ungetregister32(hreg1);
  284. hreg1 := getexplicitregister32(R_EDX);
  285. if hreg1 <> R_EDX then
  286. emit_reg_reg(A_MOV,S_L,R_EDX,hreg1);;
  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(p^.location);
  302. p^.location.loc:=LOC_REGISTER;
  303. p^.location.register:=hreg1;
  304. end;
  305. end;
  306. {*****************************************************************************
  307. SecondShlShr
  308. *****************************************************************************}
  309. procedure secondshlshr(var p : ptree);
  310. var
  311. hregister1,hregister2,hregister3,
  312. hregisterhigh,hregisterlow : tregister;
  313. pushed,popecx : boolean;
  314. op : tasmop;
  315. hr : preference;
  316. l1,l2,l3 : pasmlabel;
  317. begin
  318. popecx:=false;
  319. secondpass(p^.left);
  320. pushed:=maybe_push(p^.right^.registers32,p^.left,is_64bitint(p^.left^.resulttype));
  321. secondpass(p^.right);
  322. if pushed then
  323. restore(p^.left,is_64bitint(p^.left^.resulttype));
  324. if is_64bitint(p^.left^.resulttype) then
  325. begin
  326. { load left operator in a register }
  327. if p^.left^.location.loc<>LOC_REGISTER then
  328. begin
  329. if p^.left^.location.loc=LOC_CREGISTER then
  330. begin
  331. hregisterlow:=getregister32;
  332. hregisterhigh:=getregister32;
  333. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  334. hregisterlow);
  335. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,
  336. hregisterlow);
  337. end
  338. else
  339. begin
  340. del_reference(p^.left^.location.reference);
  341. hregisterlow:=getregister32;
  342. hregisterhigh:=getregister32;
  343. emit_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  344. hregisterlow);
  345. hr:=newreference(p^.left^.location.reference);
  346. inc(hr^.offset,4);
  347. emit_ref_reg(A_MOV,S_L,hr,
  348. hregisterhigh);
  349. end;
  350. end
  351. else
  352. begin
  353. hregisterlow:=p^.left^.location.registerlow;
  354. hregisterhigh:=p^.left^.location.registerhigh;
  355. end;
  356. { shifting by a constant directly coded: }
  357. if (p^.right^.treetype=ordconstn) then
  358. begin
  359. { shrd/shl works only for values <=31 !! }
  360. if p^.right^.value>31 then
  361. begin
  362. if p^.treetype=shln then
  363. begin
  364. emit_reg_reg(A_XOR,S_L,hregisterhigh,
  365. hregisterhigh);
  366. emit_const_reg(A_SHL,S_L,p^.right^.value and 31,
  367. hregisterlow);
  368. end
  369. else
  370. begin
  371. emit_reg_reg(A_XOR,S_L,hregisterlow,
  372. hregisterlow);
  373. emit_const_reg(A_SHR,S_L,p^.right^.value and 31,
  374. hregisterhigh);
  375. end;
  376. p^.location.registerhigh:=hregisterlow;
  377. p^.location.registerlow:=hregisterhigh;
  378. end
  379. else
  380. begin
  381. if p^.treetype=shln then
  382. begin
  383. emit_const_reg_reg(A_SHLD,S_L,p^.right^.value and 31,
  384. hregisterlow,hregisterhigh);
  385. emit_const_reg(A_SHL,S_L,p^.right^.value and 31,
  386. hregisterlow);
  387. end
  388. else
  389. begin
  390. emit_const_reg_reg(A_SHRD,S_L,p^.right^.value and 31,
  391. hregisterhigh,hregisterlow);
  392. emit_const_reg(A_SHR,S_L,p^.right^.value and 31,
  393. hregisterhigh);
  394. end;
  395. p^.location.registerlow:=hregisterlow;
  396. p^.location.registerhigh:=hregisterhigh;
  397. end;
  398. p^.location.loc:=LOC_REGISTER;
  399. end
  400. else
  401. begin
  402. { load right operators in a register }
  403. if p^.right^.location.loc<>LOC_REGISTER then
  404. begin
  405. if p^.right^.location.loc=LOC_CREGISTER then
  406. begin
  407. hregister2:=getexplicitregister32(R_ECX);
  408. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,
  409. hregister2);
  410. end
  411. else
  412. begin
  413. del_reference(p^.right^.location.reference);
  414. hregister2:=getexplicitregister32(R_ECX);
  415. emit_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),
  416. hregister2);
  417. end;
  418. end
  419. else
  420. hregister2:=p^.right^.location.register;
  421. { left operator is already in a register }
  422. { hence are both in a register }
  423. { is it in the case ECX ? }
  424. if (hregisterlow=R_ECX) then
  425. begin
  426. { then only swap }
  427. emit_reg_reg(A_XCHG,S_L,hregisterlow,hregister2);
  428. hregister3:=hregisterlow;
  429. hregisterlow:=hregister2;
  430. hregister2:=hregister3;
  431. end
  432. else if (hregisterhigh=R_ECX) then
  433. begin
  434. { then only swap }
  435. emit_reg_reg(A_XCHG,S_L,hregisterhigh,hregister2);
  436. hregister3:=hregisterhigh;
  437. hregisterhigh:=hregister2;
  438. hregister2:=hregister3;
  439. end
  440. { if second operator not in ECX ? }
  441. else if (hregister2<>R_ECX) then
  442. begin
  443. { ECX occupied then push it }
  444. if not (R_ECX in unused) then
  445. begin
  446. popecx:=true;
  447. emit_reg(A_PUSH,S_L,R_ECX);
  448. end;
  449. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  450. end;
  451. ungetregister32(hregister2);
  452. { the damned shift instructions work only til a count of 32 }
  453. { so we've to do some tricks here }
  454. if p^.treetype=shln then
  455. begin
  456. getlabel(l1);
  457. getlabel(l2);
  458. getlabel(l3);
  459. emit_const_reg(A_CMP,S_L,64,R_ECX);
  460. emitjmp(C_L,l1);
  461. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  462. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  463. emitjmp(C_None,l3);
  464. emitlab(l1);
  465. emit_const_reg(A_CMP,S_L,32,R_ECX);
  466. emitjmp(C_L,l2);
  467. emit_const_reg(A_SUB,S_L,32,R_ECX);
  468. emit_reg_reg(A_SHL,S_L,R_CL,
  469. hregisterlow);
  470. emit_reg_reg(A_MOV,S_L,hregisterlow,hregisterhigh);
  471. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  472. emitjmp(C_None,l3);
  473. emitlab(l2);
  474. emit_reg_reg_reg(A_SHLD,S_L,R_CL,
  475. hregisterlow,hregisterhigh);
  476. emit_reg_reg(A_SHL,S_L,R_CL,
  477. hregisterlow);
  478. emitlab(l3);
  479. end
  480. else
  481. begin
  482. getlabel(l1);
  483. getlabel(l2);
  484. getlabel(l3);
  485. emit_const_reg(A_CMP,S_L,64,R_ECX);
  486. emitjmp(C_L,l1);
  487. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  488. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  489. emitjmp(C_None,l3);
  490. emitlab(l1);
  491. emit_const_reg(A_CMP,S_L,32,R_ECX);
  492. emitjmp(C_L,l2);
  493. emit_const_reg(A_SUB,S_L,32,R_ECX);
  494. emit_reg_reg(A_SHR,S_L,R_CL,
  495. hregisterhigh);
  496. emit_reg_reg(A_MOV,S_L,hregisterhigh,hregisterlow);
  497. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  498. emitjmp(C_None,l3);
  499. emitlab(l2);
  500. emit_reg_reg_reg(A_SHRD,S_L,R_CL,
  501. hregisterhigh,hregisterlow);
  502. emit_reg_reg(A_SHR,S_L,R_CL,
  503. hregisterhigh);
  504. emitlab(l3);
  505. end;
  506. { maybe put ECX back }
  507. if popecx then
  508. emit_reg(A_POP,S_L,R_ECX);
  509. p^.location.registerlow:=hregisterlow;
  510. p^.location.registerhigh:=hregisterhigh;
  511. end;
  512. end
  513. else
  514. begin
  515. { load left operators in a register }
  516. if p^.left^.location.loc<>LOC_REGISTER then
  517. begin
  518. if p^.left^.location.loc=LOC_CREGISTER then
  519. begin
  520. hregister1:=getregister32;
  521. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  522. hregister1);
  523. end
  524. else
  525. begin
  526. del_reference(p^.left^.location.reference);
  527. hregister1:=getregister32;
  528. emit_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  529. hregister1);
  530. end;
  531. end
  532. else
  533. hregister1:=p^.left^.location.register;
  534. { determine operator }
  535. if p^.treetype=shln then
  536. op:=A_SHL
  537. else
  538. op:=A_SHR;
  539. { shifting by a constant directly coded: }
  540. if (p^.right^.treetype=ordconstn) then
  541. begin
  542. { l shl 32 should 0 imho, but neither TP nor Delphi do it in this way (FK)
  543. if p^.right^.value<=31 then
  544. }
  545. emit_const_reg(op,S_L,p^.right^.value and 31,
  546. hregister1);
  547. {
  548. else
  549. emit_reg_reg(A_XOR,S_L,hregister1,
  550. hregister1);
  551. }
  552. p^.location.loc:=LOC_REGISTER;
  553. p^.location.register:=hregister1;
  554. end
  555. else
  556. begin
  557. { load right operators in a register }
  558. if p^.right^.location.loc<>LOC_REGISTER then
  559. begin
  560. if p^.right^.location.loc=LOC_CREGISTER then
  561. begin
  562. hregister2:=getexplicitregister32(R_ECX);
  563. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,
  564. hregister2);
  565. end
  566. else
  567. begin
  568. del_reference(p^.right^.location.reference);
  569. hregister2:=getexplicitregister32(R_ECX);
  570. emit_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),
  571. hregister2);
  572. end;
  573. end
  574. else
  575. hregister2:=p^.right^.location.register;
  576. { left operator is already in a register }
  577. { hence are both in a register }
  578. { is it in the case ECX ? }
  579. if (hregister1=R_ECX) then
  580. begin
  581. { then only swap }
  582. emit_reg_reg(A_XCHG,S_L,hregister1,hregister2);
  583. hregister3:=hregister1;
  584. hregister1:=hregister2;
  585. hregister2:=hregister3;
  586. end
  587. { if second operator not in ECX ? }
  588. else if (hregister2<>R_ECX) then
  589. begin
  590. { ECX occupied then push it }
  591. if not (R_ECX in unused) then
  592. begin
  593. popecx:=true;
  594. emit_reg(A_PUSH,S_L,R_ECX);
  595. end;
  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. p^.location.register:=hregister1;
  605. end;
  606. end;
  607. end;
  608. {*****************************************************************************
  609. SecondUnaryMinus
  610. *****************************************************************************}
  611. procedure secondunaryminus(var p : ptree);
  612. {$ifdef SUPPORT_MMX}
  613. procedure do_mmx_neg;
  614. var
  615. op : tasmop;
  616. begin
  617. p^.location.loc:=LOC_MMXREGISTER;
  618. if cs_mmx_saturation in aktlocalswitches then
  619. case mmx_type(p^.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(p^.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,p^.location.register,R_MM7);
  639. emit_reg_reg(A_MOVQ,S_NO,R_MM7,p^.location.register);
  640. end;
  641. {$endif}
  642. var
  643. hr : preference;
  644. begin
  645. if is_64bitint(p^.left^.resulttype) then
  646. begin
  647. secondpass(p^.left);
  648. clear_location(p^.location);
  649. p^.location.loc:=LOC_REGISTER;
  650. case p^.left^.location.loc of
  651. LOC_REGISTER :
  652. begin
  653. p^.location.registerlow:=p^.left^.location.registerlow;
  654. p^.location.registerhigh:=p^.left^.location.registerhigh;
  655. end;
  656. LOC_CREGISTER :
  657. begin
  658. p^.location.registerlow:=getregister32;
  659. p^.location.registerhigh:=getregister32;
  660. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,p^.location.registerlow);
  661. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,p^.location.registerhigh);
  662. end;
  663. LOC_REFERENCE,LOC_MEM :
  664. begin
  665. del_reference(p^.left^.location.reference);
  666. p^.location.registerlow:=getregister32;
  667. p^.location.registerhigh:=getregister32;
  668. emit_ref_reg(A_MOV,S_L,
  669. newreference(p^.left^.location.reference),p^.location.registerlow);
  670. hr:=newreference(p^.left^.location.reference);
  671. inc(hr^.offset,4);
  672. emit_ref_reg(A_MOV,S_L,
  673. hr,p^.location.registerhigh);
  674. end;
  675. end;
  676. {
  677. emit_reg(A_NEG,S_L,p^.location.registerlow);
  678. emit_const_reg(A_ADC,S_L,0,p^.location.registerhigh);
  679. emit_reg(A_NEG,S_L,p^.location.registerhigh);
  680. }
  681. emit_reg(A_NOT,S_L,p^.location.registerhigh);
  682. emit_reg(A_NEG,S_L,p^.location.registerlow);
  683. emit_const_reg(A_SBB,S_L,-1,p^.location.registerhigh);
  684. end
  685. else
  686. begin
  687. secondpass(p^.left);
  688. p^.location.loc:=LOC_REGISTER;
  689. case p^.left^.location.loc of
  690. LOC_REGISTER:
  691. begin
  692. p^.location.register:=p^.left^.location.register;
  693. emit_reg(A_NEG,S_L,p^.location.register);
  694. end;
  695. LOC_CREGISTER:
  696. begin
  697. p^.location.register:=getregister32;
  698. emit_reg_reg(A_MOV,S_L,p^.location.register,
  699. p^.location.register);
  700. emit_reg(A_NEG,S_L,p^.location.register);
  701. end;
  702. {$ifdef SUPPORT_MMX}
  703. LOC_MMXREGISTER:
  704. begin
  705. set_location(p^.location,p^.left^.location);
  706. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  707. do_mmx_neg;
  708. end;
  709. LOC_CMMXREGISTER:
  710. begin
  711. p^.location.register:=getregistermmx;
  712. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  713. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,
  714. p^.location.register);
  715. do_mmx_neg;
  716. end;
  717. {$endif SUPPORT_MMX}
  718. LOC_REFERENCE,LOC_MEM:
  719. begin
  720. del_reference(p^.left^.location.reference);
  721. if (p^.left^.resulttype^.deftype=floatdef) and
  722. (pfloatdef(p^.left^.resulttype)^.typ<>f32bit) then
  723. begin
  724. p^.location.loc:=LOC_FPU;
  725. floatload(pfloatdef(p^.left^.resulttype)^.typ,
  726. p^.left^.location.reference);
  727. emit_none(A_FCHS,S_NO);
  728. end
  729. {$ifdef SUPPORT_MMX}
  730. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(p^.left^.resulttype) then
  731. begin
  732. p^.location.register:=getregistermmx;
  733. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  734. emit_ref_reg(A_MOVQ,S_NO,
  735. newreference(p^.left^.location.reference),
  736. p^.location.register);
  737. do_mmx_neg;
  738. end
  739. {$endif SUPPORT_MMX}
  740. else
  741. begin
  742. p^.location.register:=getregister32;
  743. emit_ref_reg(A_MOV,S_L,
  744. newreference(p^.left^.location.reference),
  745. p^.location.register);
  746. emit_reg(A_NEG,S_L,p^.location.register);
  747. end;
  748. end;
  749. LOC_FPU:
  750. begin
  751. p^.location.loc:=LOC_FPU;
  752. emit_none(A_FCHS,S_NO);
  753. end;
  754. LOC_CFPUREGISTER:
  755. begin
  756. emit_reg(A_FLD,S_NO,
  757. correct_fpuregister(p^.left^.location.register,fpuvaroffset));
  758. inc(fpuvaroffset);
  759. p^.location.loc:=LOC_FPU;
  760. emit_none(A_FCHS,S_NO);
  761. end;
  762. end;
  763. end;
  764. { Here was a problem... }
  765. { Operand to be negated always }
  766. { seems to be converted to signed }
  767. { 32-bit before doing neg!! }
  768. { So this is useless... }
  769. { that's not true: -2^31 gives an overflow error if it is negaded (FK) }
  770. { emitoverflowcheck(p);}
  771. end;
  772. {*****************************************************************************
  773. SecondNot
  774. *****************************************************************************}
  775. procedure secondnot(var p : ptree);
  776. const
  777. flagsinvers : array[F_E..F_BE] of tresflags =
  778. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  779. F_A,F_AE,F_B,F_BE);
  780. var
  781. hl : pasmlabel;
  782. opsize : topsize;
  783. hr : preference;
  784. begin
  785. if is_boolean(p^.resulttype) then
  786. begin
  787. opsize:=def_opsize(p^.resulttype);
  788. case p^.left^.location.loc of
  789. LOC_JUMP :
  790. begin
  791. hl:=truelabel;
  792. truelabel:=falselabel;
  793. falselabel:=hl;
  794. secondpass(p^.left);
  795. maketojumpbool(p^.left);
  796. hl:=truelabel;
  797. truelabel:=falselabel;
  798. falselabel:=hl;
  799. end;
  800. LOC_FLAGS :
  801. begin
  802. secondpass(p^.left);
  803. p^.location.resflags:=flagsinvers[p^.left^.location.resflags];
  804. end;
  805. LOC_REGISTER :
  806. begin
  807. secondpass(p^.left);
  808. {p^.location.register:=p^.left^.location.register;
  809. emit_const_reg(A_XOR,opsize,1,p^.location.register);}
  810. p^.location.loc:=LOC_FLAGS;
  811. p^.location.resflags:=F_E;
  812. emit_reg_reg(A_TEST,opsize,
  813. p^.left^.location.register,p^.left^.location.register);
  814. ungetregister(p^.left^.location.register);
  815. end;
  816. LOC_CREGISTER :
  817. begin
  818. secondpass(p^.left);
  819. clear_location(p^.location);
  820. p^.location.loc:=LOC_REGISTER;
  821. p^.location.register:=def_getreg(p^.resulttype);
  822. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,p^.location.register);
  823. emit_reg_reg(A_TEST,opsize,p^.location.register,p^.location.register);
  824. ungetregister(p^.location.register);
  825. p^.location.loc:=LOC_FLAGS;
  826. p^.location.resflags:=F_E;
  827. end;
  828. LOC_REFERENCE,
  829. LOC_MEM :
  830. begin
  831. secondpass(p^.left);
  832. clear_location(p^.location);
  833. p^.location.loc:=LOC_REGISTER;
  834. del_reference(p^.left^.location.reference);
  835. { this was placed before del_ref => internaalerror(10) }
  836. p^.location.register:=def_getreg(p^.resulttype);
  837. emit_ref_reg(A_MOV,opsize,
  838. newreference(p^.left^.location.reference),p^.location.register);
  839. emit_reg_reg(A_TEST,opsize,p^.location.register,p^.location.register);
  840. ungetregister(p^.location.register);
  841. p^.location.loc:=LOC_FLAGS;
  842. p^.location.resflags:=F_E;
  843. end;
  844. end;
  845. end
  846. {$ifdef SUPPORT_MMX}
  847. else
  848. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(p^.left^.resulttype) then
  849. begin
  850. secondpass(p^.left);
  851. p^.location.loc:=LOC_MMXREGISTER;
  852. { prepare EDI }
  853. {$ifndef noAllocEdi}
  854. getexplicitregister32(R_EDI);
  855. {$endif noAllocEdi}
  856. emit_const_reg(A_MOV,S_L,$ffffffff,R_EDI);
  857. { load operand }
  858. case p^.left^.location.loc of
  859. LOC_MMXREGISTER:
  860. set_location(p^.location,p^.left^.location);
  861. LOC_CMMXREGISTER:
  862. begin
  863. p^.location.register:=getregistermmx;
  864. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,p^.location.register);
  865. end;
  866. LOC_REFERENCE,LOC_MEM:
  867. begin
  868. del_reference(p^.left^.location.reference);
  869. p^.location.register:=getregistermmx;
  870. emit_ref_reg(A_MOVQ,S_NO,
  871. newreference(p^.left^.location.reference),p^.location.register);
  872. end;
  873. end;
  874. { load mask }
  875. emit_reg_reg(A_MOVD,S_NO,R_EDI,R_MM7);
  876. {$ifndef noAllocEdi}
  877. ungetregister32(R_EDI);
  878. {$endif noAllocEdi}
  879. { lower 32 bit }
  880. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  881. { shift mask }
  882. emit_const_reg(A_PSLLQ,S_NO,32,R_MM7);
  883. { higher 32 bit }
  884. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  885. end
  886. {$endif SUPPORT_MMX}
  887. else if is_64bitint(p^.left^.resulttype) then
  888. begin
  889. secondpass(p^.left);
  890. clear_location(p^.location);
  891. p^.location.loc:=LOC_REGISTER;
  892. case p^.left^.location.loc of
  893. LOC_REGISTER :
  894. begin
  895. p^.location.registerlow:=p^.left^.location.registerlow;
  896. p^.location.registerhigh:=p^.left^.location.registerhigh;
  897. emit_reg(A_NOT,S_L,p^.location.registerlow);
  898. emit_reg(A_NOT,S_L,p^.location.registerhigh);
  899. end;
  900. LOC_CREGISTER :
  901. begin
  902. p^.location.registerlow:=getregister32;
  903. p^.location.registerhigh:=getregister32;
  904. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,p^.location.registerlow);
  905. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,p^.location.registerhigh);
  906. emit_reg(A_NOT,S_L,p^.location.registerlow);
  907. emit_reg(A_NOT,S_L,p^.location.registerhigh);
  908. end;
  909. LOC_REFERENCE,LOC_MEM :
  910. begin
  911. del_reference(p^.left^.location.reference);
  912. p^.location.registerlow:=getregister32;
  913. p^.location.registerhigh:=getregister32;
  914. emit_ref_reg(A_MOV,S_L,
  915. newreference(p^.left^.location.reference),p^.location.registerlow);
  916. hr:=newreference(p^.left^.location.reference);
  917. inc(hr^.offset,4);
  918. emit_ref_reg(A_MOV,S_L,
  919. hr,p^.location.registerhigh);
  920. emit_reg(A_NOT,S_L,p^.location.registerlow);
  921. emit_reg(A_NOT,S_L,p^.location.registerhigh);
  922. end;
  923. end;
  924. end
  925. else
  926. begin
  927. secondpass(p^.left);
  928. clear_location(p^.location);
  929. p^.location.loc:=LOC_REGISTER;
  930. case p^.left^.location.loc of
  931. LOC_REGISTER :
  932. begin
  933. p^.location.register:=p^.left^.location.register;
  934. emit_reg(A_NOT,S_L,p^.location.register);
  935. end;
  936. LOC_CREGISTER :
  937. begin
  938. p^.location.register:=getregister32;
  939. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,p^.location.register);
  940. emit_reg(A_NOT,S_L,p^.location.register);
  941. end;
  942. LOC_REFERENCE,LOC_MEM :
  943. begin
  944. del_reference(p^.left^.location.reference);
  945. p^.location.register:=getregister32;
  946. emit_ref_reg(A_MOV,S_L,
  947. newreference(p^.left^.location.reference),p^.location.register);
  948. emit_reg(A_NOT,S_L,p^.location.register);
  949. end;
  950. end;
  951. end;
  952. end;
  953. end.
  954. {
  955. $Log$
  956. Revision 1.41 2000-01-27 15:46:00 florian
  957. * slighly improved code for -<qword> and -<int64>
  958. Revision 1.40 2000/01/09 12:35:01 jonas
  959. * changed edi allocation to use getexplicitregister32/ungetregister
  960. (adapted tgeni386 a bit for this) and enabled it by default
  961. * fixed very big and stupid bug of mine in cg386mat that broke the
  962. include() code (and make cycle :( ) if you compiled without
  963. -dnewoptimizations
  964. Revision 1.39 2000/01/09 01:44:20 jonas
  965. + (de)allocation info for EDI to fix reported bug on mailinglist.
  966. Also some (de)allocation info for ESI added. Between -dallocEDI
  967. because at this time of the night bugs could easily slip in ;)
  968. Revision 1.38 2000/01/07 01:14:21 peter
  969. * updated copyright to 2000
  970. Revision 1.37 2000/01/07 00:12:10 peter
  971. * fixed movd isntruction to be A_MOVD instead of A_MOV S_D
  972. Revision 1.36 1999/11/18 15:34:44 pierre
  973. * Notes/Hints for local syms changed to
  974. Set_varstate function
  975. Revision 1.35 1999/11/06 14:34:18 peter
  976. * truncated log to 20 revs
  977. Revision 1.34 1999/09/28 19:43:47 florian
  978. * the maybe_push fix of Pierre wasn't 100%, the tree parameter
  979. must contain a valid location (which is saved if necessary)
  980. Revision 1.33 1999/09/27 23:37:26 peter
  981. * fixed push/restore bug in div/mod
  982. Revision 1.32 1999/09/02 17:07:38 florian
  983. * problems with -Or fixed: tdef.isfpuregable was wrong!
  984. Revision 1.31 1999/08/19 13:08:50 pierre
  985. * emit_??? used
  986. Revision 1.30 1999/08/04 13:45:23 florian
  987. + floating point register variables !!
  988. * pairegalloc is now generated for register variables
  989. Revision 1.29 1999/08/04 00:22:51 florian
  990. * renamed i386asm and i386base to cpuasm and cpubase
  991. Revision 1.28 1999/08/03 22:02:45 peter
  992. * moved bitmask constants to sets
  993. * some other type/const renamings
  994. Revision 1.27 1999/06/28 22:29:14 florian
  995. * qword division fixed
  996. + code for qword/int64 type casting added:
  997. range checking isn't implemented yet
  998. Revision 1.26 1999/06/02 10:11:44 florian
  999. * make cycle fixed i.e. compilation with 0.99.10
  1000. * some fixes for qword
  1001. * start of register calling conventions
  1002. Revision 1.25 1999/05/27 19:44:16 peter
  1003. * removed oldasm
  1004. * plabel -> pasmlabel
  1005. * -a switches to source writing automaticly
  1006. * assembler readers OOPed
  1007. * asmsymbol automaticly external
  1008. * jumptables and other label fixes for asm readers
  1009. Revision 1.24 1999/05/25 20:36:13 florian
  1010. * some bugs in the qword code generation fixed
  1011. Revision 1.23 1999/05/08 20:41:08 jonas
  1012. + positive number MOD power of 2 now done with AND instruction
  1013. * fix to division of positive numbers by power of 2
  1014. * the result of a MOD is left in EDX if possible
  1015. Revision 1.22 1999/05/01 13:24:11 peter
  1016. * merged nasm compiler
  1017. * old asm moved to oldasm/
  1018. Revision 1.21 1999/04/16 13:42:27 jonas
  1019. * more regalloc fixes (still not complete)
  1020. Revision 1.20 1999/02/22 02:15:13 peter
  1021. * updates for ag386bin
  1022. Revision 1.19 1999/02/04 10:49:40 florian
  1023. + range checking for ansi- and widestrings
  1024. * made it compilable with TP
  1025. Revision 1.18 1999/02/03 10:11:12 pierre
  1026. * fix for bug0211 for i386
  1027. Revision 1.17 1999/01/21 22:10:41 peter
  1028. * fixed array of const
  1029. * generic platform independent high() support
  1030. Revision 1.16 1999/01/19 10:51:32 pierre
  1031. * fix to bug0183 in secondnot
  1032. Revision 1.15 1998/12/11 16:50:22 florian
  1033. + typed const int64 and qword
  1034. + unary minus-operator q1:=-q2;
  1035. + not-operator
  1036. }