cg386mat.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 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 secondumminus(var p : ptree);
  25. procedure secondnot(var p : ptree);
  26. implementation
  27. uses
  28. globtype,systems,
  29. cobjects,verbose,globals,
  30. symtable,aasm,types,
  31. hcodegen,temp_gen,pass_2,
  32. {$ifndef OLDASM}
  33. i386base,i386asm,
  34. {$else}
  35. i386,
  36. {$endif}
  37. cgai386,tgeni386;
  38. {*****************************************************************************
  39. SecondModDiv
  40. *****************************************************************************}
  41. procedure secondmoddiv(var p : ptree);
  42. var
  43. hreg1 : tregister;
  44. shrdiv, andmod, pushed,popeax,popedx : boolean;
  45. power : longint;
  46. hl : plabel;
  47. begin
  48. shrdiv := false;
  49. andmod := false;
  50. secondpass(p^.left);
  51. set_location(p^.location,p^.left^.location);
  52. pushed:=maybe_push(p^.right^.registers32,p);
  53. secondpass(p^.right);
  54. if pushed then restore(p);
  55. { put numerator in register }
  56. if p^.left^.location.loc<>LOC_REGISTER then
  57. begin
  58. if p^.left^.location.loc=LOC_CREGISTER then
  59. begin
  60. hreg1:=getregister32;
  61. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hreg1);
  62. end
  63. else
  64. begin
  65. del_reference(p^.left^.location.reference);
  66. hreg1:=getregister32;
  67. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  68. hreg1)));
  69. end;
  70. clear_location(p^.left^.location);
  71. p^.left^.location.loc:=LOC_REGISTER;
  72. p^.left^.location.register:=hreg1;
  73. end
  74. else hreg1:=p^.left^.location.register;
  75. if (p^.treetype=divn) and (p^.right^.treetype=ordconstn) and
  76. ispowerof2(p^.right^.value,power) then
  77. Begin
  78. shrdiv := true;
  79. {for signed numbers, the numerator must be adjusted before the
  80. shift instruction, but not wih unsigned numbers! Otherwise,
  81. "Cardinal($ffffffff) div 16" overflows! (JM)}
  82. If is_signed(p^.left^.resulttype) Then
  83. Begin
  84. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,hreg1,hreg1)));
  85. getlabel(hl);
  86. emitjmp(C_NS,hl);
  87. if power=1 then
  88. exprasmlist^.concat(new(pai386,op_reg(A_INC,S_L,hreg1)))
  89. else
  90. exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,p^.right^.value-1,hreg1)));
  91. emitlab(hl);
  92. exprasmlist^.concat(new(pai386,op_const_reg(A_SAR,S_L,power,hreg1)));
  93. End
  94. Else
  95. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,power,hreg1)));
  96. End
  97. else
  98. if (p^.treetype=modn) and (p^.right^.treetype=ordconstn) and
  99. ispowerof2(p^.right^.value,power) and Not(is_signed(p^.left^.resulttype)) Then
  100. {is there a similar trick for MOD'ing signed numbers? (JM)}
  101. Begin
  102. exprasmlist^.concat(new(pai386,op_const_reg(A_AND,S_L,p^.right^.value-1,hreg1)));
  103. andmod := true;
  104. End
  105. else
  106. begin
  107. { bring denominator to EDI }
  108. { EDI is always free, it's }
  109. { only used for temporary }
  110. { purposes }
  111. if (p^.right^.location.loc<>LOC_REGISTER) and
  112. (p^.right^.location.loc<>LOC_CREGISTER) then
  113. begin
  114. del_reference(p^.right^.location.reference);
  115. p^.left^.location.loc:=LOC_REGISTER;
  116. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),R_EDI)));
  117. end
  118. else
  119. begin
  120. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,R_EDI);
  121. ungetregister32(p^.right^.location.register);
  122. end;
  123. popedx:=false;
  124. popeax:=false;
  125. if hreg1=R_EDX then
  126. begin
  127. if not(R_EAX in unused) then
  128. begin
  129. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  130. popeax:=true;
  131. end;
  132. emit_reg_reg(A_MOV,S_L,R_EDX,R_EAX);
  133. end
  134. else
  135. begin
  136. if not(R_EDX in unused) then
  137. begin
  138. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDX)));
  139. popedx:=true;
  140. end;
  141. if hreg1<>R_EAX then
  142. begin
  143. if not(R_EAX in unused) then
  144. begin
  145. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  146. popeax:=true;
  147. end;
  148. emit_reg_reg(A_MOV,S_L,hreg1,R_EAX);
  149. end;
  150. end;
  151. { sign extension depends on the left type }
  152. if porddef(p^.left^.resulttype)^.typ=u32bit then
  153. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EDX,R_EDX)))
  154. else
  155. exprasmlist^.concat(new(pai386,op_none(A_CDQ,S_NO)));
  156. { division depends on the right type }
  157. if porddef(p^.right^.resulttype)^.typ=u32bit then
  158. exprasmlist^.concat(new(pai386,op_reg(A_DIV,S_L,R_EDI)))
  159. else
  160. exprasmlist^.concat(new(pai386,op_reg(A_IDIV,S_L,R_EDI)));
  161. if p^.treetype=divn then
  162. begin
  163. { if result register is busy then copy }
  164. if popeax then
  165. begin
  166. if hreg1=R_EAX then
  167. internalerror(112);
  168. emit_reg_reg(A_MOV,S_L,R_EAX,hreg1)
  169. end
  170. else
  171. if hreg1<>R_EAX then
  172. Begin
  173. ungetregister32(hreg1);
  174. hreg1 := getexplicitregister32(R_EAX);
  175. { I don't think it's possible that now hreg1 <> R_EAX
  176. since popeax is false, but for all certainty I do
  177. support that situation (JM)}
  178. if hreg1 <> R_EAX then
  179. emit_reg_reg(A_MOV,S_L,R_EAX,hreg1);
  180. end;
  181. end
  182. else
  183. {if we did the mod by an "and", the result is in hreg1 and
  184. EDX certainly hasn't been pushed (JM)}
  185. if not(andmod) Then
  186. if popedx then
  187. {the mod was done by an (i)div (so the result is now in
  188. edx), but edx was occupied prior to the division, so
  189. move the result into a safe place (JM)}
  190. emit_reg_reg(A_MOV,S_L,R_EDX,hreg1)
  191. else
  192. Begin
  193. {Get rid of the unnecessary hreg1 if possible (same as with
  194. EAX in divn) (JM)}
  195. ungetregister32(hreg1);
  196. hreg1 := getexplicitregister32(R_EDX);
  197. if hreg1 <> R_EDX then
  198. emit_reg_reg(A_MOV,S_L,R_EDX,hreg1);;
  199. End;
  200. if popeax then
  201. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  202. if popedx then
  203. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDX)));
  204. end;
  205. If not(andmod or shrdiv) then
  206. {andmod and shrdiv only use hreg1 (which is already in usedinproc,
  207. since it was acquired with getregister), the others also use both
  208. EAX and EDX (JM)}
  209. Begin
  210. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  211. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  212. End;
  213. clear_location(p^.location);
  214. p^.location.loc:=LOC_REGISTER;
  215. p^.location.register:=hreg1;
  216. end;
  217. {*****************************************************************************
  218. SecondShlShr
  219. *****************************************************************************}
  220. procedure secondshlshr(var p : ptree);
  221. var
  222. hregister1,hregister2,hregister3,
  223. hregisterhigh,hregisterlow : tregister;
  224. pushed,popecx : boolean;
  225. op : tasmop;
  226. hr : preference;
  227. begin
  228. popecx:=false;
  229. secondpass(p^.left);
  230. pushed:=maybe_push(p^.right^.registers32,p);
  231. secondpass(p^.right);
  232. if pushed then
  233. restore(p);
  234. if is_64bitint(p^.left^.resulttype) then
  235. begin
  236. { load left operators in a register }
  237. if p^.left^.location.loc<>LOC_REGISTER then
  238. begin
  239. if p^.left^.location.loc=LOC_CREGISTER then
  240. begin
  241. hregisterlow:=getregister32;
  242. hregisterhigh:=getregister32;
  243. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,
  244. hregisterlow);
  245. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,
  246. hregisterlow);
  247. end
  248. else
  249. begin
  250. del_reference(p^.left^.location.reference);
  251. hregisterlow:=getregister32;
  252. hregisterhigh:=getregister32;
  253. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  254. hregisterlow)));
  255. hr:=newreference(p^.left^.location.reference);
  256. inc(hr^.offset,4);
  257. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,hr,
  258. hregisterhigh)));
  259. end;
  260. end
  261. else
  262. begin
  263. hregisterlow:=p^.left^.location.registerlow;
  264. hregisterhigh:=p^.left^.location.registerhigh;
  265. end;
  266. { shifting by a constant directly coded: }
  267. if (p^.right^.treetype=ordconstn) then
  268. begin
  269. if p^.treetype=shln then
  270. begin
  271. exprasmlist^.concat(new(pai386,op_const_reg_reg(A_SHLD,S_L,p^.right^.location.reference.offset and 31,
  272. hregisterlow,hregisterhigh)));
  273. exprasmlist^.concat(new(pai386,op_const_reg(A_SHL,S_L,p^.right^.location.reference.offset and 31,
  274. hregisterlow)));
  275. end
  276. else
  277. begin
  278. exprasmlist^.concat(new(pai386,op_const_reg_reg(A_SHRD,S_L,p^.right^.location.reference.offset and 31,
  279. hregisterhigh,hregisterlow)));
  280. exprasmlist^.concat(new(pai386,op_const_reg(A_SHR,S_L,p^.right^.location.reference.offset and 31,
  281. hregisterhigh)));
  282. end;
  283. p^.location.loc:=LOC_REGISTER;
  284. p^.location.registerlow:=hregisterlow;
  285. p^.location.registerhigh:=hregisterhigh;
  286. end
  287. else
  288. begin
  289. { load right operators in a register }
  290. if p^.right^.location.loc<>LOC_REGISTER then
  291. begin
  292. if p^.right^.location.loc=LOC_CREGISTER then
  293. begin
  294. hregister2:=getexplicitregister32(R_ECX);
  295. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,
  296. hregister2);
  297. end
  298. else
  299. begin
  300. del_reference(p^.right^.location.reference);
  301. hregister2:=getexplicitregister32(R_ECX);
  302. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),
  303. hregister2)));
  304. end;
  305. end
  306. else
  307. hregister2:=p^.right^.location.register;
  308. { left operator is already in a register }
  309. { hence are both in a register }
  310. { is it in the case ECX ? }
  311. if (hregisterlow=R_ECX) then
  312. begin
  313. { then only swap }
  314. emit_reg_reg(A_XCHG,S_L,hregisterlow,hregister2);
  315. hregister3:=hregisterlow;
  316. hregisterlow:=hregister2;
  317. hregister2:=hregister3;
  318. end
  319. else if (hregisterhigh=R_ECX) then
  320. begin
  321. { then only swap }
  322. emit_reg_reg(A_XCHG,S_L,hregisterhigh,hregister2);
  323. hregister3:=hregisterhigh;
  324. hregisterhigh:=hregister2;
  325. hregister2:=hregister3;
  326. end
  327. { if second operator not in ECX ? }
  328. else if (hregister2<>R_ECX) then
  329. begin
  330. { ECX occupied then push it }
  331. if not (R_ECX in unused) then
  332. begin
  333. popecx:=true;
  334. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  335. end;
  336. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  337. end;
  338. ungetregister32(hregister2);
  339. if p^.treetype=shln then
  340. begin
  341. exprasmlist^.concat(new(pai386,op_reg_reg_reg(A_SHLD,S_L,R_CL,
  342. hregisterlow,hregisterhigh)));
  343. exprasmlist^.concat(new(pai386,op_reg_reg(A_SHL,S_L,R_CL,
  344. hregisterlow)));
  345. end
  346. else
  347. begin
  348. exprasmlist^.concat(new(pai386,op_reg_reg_reg(A_SHRD,S_L,R_CL,
  349. hregisterhigh,hregisterlow)));
  350. exprasmlist^.concat(new(pai386,op_reg_reg(A_SHR,S_L,R_CL,
  351. hregisterhigh)));
  352. end;
  353. { maybe put ECX back }
  354. if popecx then
  355. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  356. p^.location.registerlow:=hregisterlow;
  357. p^.location.registerhigh:=hregisterhigh;
  358. end;
  359. end
  360. else
  361. begin
  362. { load left operators in a register }
  363. if p^.left^.location.loc<>LOC_REGISTER then
  364. begin
  365. if p^.left^.location.loc=LOC_CREGISTER then
  366. begin
  367. hregister1:=getregister32;
  368. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  369. hregister1);
  370. end
  371. else
  372. begin
  373. del_reference(p^.left^.location.reference);
  374. hregister1:=getregister32;
  375. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  376. hregister1)));
  377. end;
  378. end
  379. else
  380. hregister1:=p^.left^.location.register;
  381. { determine operator }
  382. if p^.treetype=shln then
  383. op:=A_SHL
  384. else
  385. op:=A_SHR;
  386. { shifting by a constant directly coded: }
  387. if (p^.right^.treetype=ordconstn) then
  388. begin
  389. exprasmlist^.concat(new(pai386,op_const_reg(op,S_L,p^.right^.location.reference.offset and 31,
  390. hregister1)));
  391. p^.location.loc:=LOC_REGISTER;
  392. p^.location.register:=hregister1;
  393. end
  394. else
  395. begin
  396. { load right operators in a register }
  397. if p^.right^.location.loc<>LOC_REGISTER then
  398. begin
  399. if p^.right^.location.loc=LOC_CREGISTER then
  400. begin
  401. hregister2:=getexplicitregister32(R_ECX);
  402. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,
  403. hregister2);
  404. end
  405. else
  406. begin
  407. del_reference(p^.right^.location.reference);
  408. hregister2:=getexplicitregister32(R_ECX);
  409. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),
  410. hregister2)));
  411. end;
  412. end
  413. else
  414. hregister2:=p^.right^.location.register;
  415. { left operator is already in a register }
  416. { hence are both in a register }
  417. { is it in the case ECX ? }
  418. if (hregister1=R_ECX) then
  419. begin
  420. { then only swap }
  421. emit_reg_reg(A_XCHG,S_L,hregister1,hregister2);
  422. hregister3:=hregister1;
  423. hregister1:=hregister2;
  424. hregister2:=hregister3;
  425. end
  426. { if second operator not in ECX ? }
  427. else if (hregister2<>R_ECX) then
  428. begin
  429. { ECX occupied then push it }
  430. if not (R_ECX in unused) then
  431. begin
  432. popecx:=true;
  433. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  434. end;
  435. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  436. end;
  437. ungetregister32(hregister2);
  438. { right operand is in ECX }
  439. emit_reg_reg(op,S_L,R_CL,hregister1);
  440. { maybe ECX back }
  441. if popecx then
  442. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  443. p^.location.register:=hregister1;
  444. end;
  445. end;
  446. end;
  447. {*****************************************************************************
  448. SecondUmMinus
  449. *****************************************************************************}
  450. procedure secondumminus(var p : ptree);
  451. {$ifdef SUPPORT_MMX}
  452. procedure do_mmx_neg;
  453. var
  454. op : tasmop;
  455. begin
  456. p^.location.loc:=LOC_MMXREGISTER;
  457. if cs_mmx_saturation in aktlocalswitches then
  458. case mmx_type(p^.resulttype) of
  459. mmxs8bit:
  460. op:=A_PSUBSB;
  461. mmxu8bit:
  462. op:=A_PSUBUSB;
  463. mmxs16bit,mmxfixed16:
  464. op:=A_PSUBSW;
  465. mmxu16bit:
  466. op:=A_PSUBUSW;
  467. end
  468. else
  469. case mmx_type(p^.resulttype) of
  470. mmxs8bit,mmxu8bit:
  471. op:=A_PSUBB;
  472. mmxs16bit,mmxu16bit,mmxfixed16:
  473. op:=A_PSUBW;
  474. mmxs32bit,mmxu32bit:
  475. op:=A_PSUBD;
  476. end;
  477. emit_reg_reg(op,S_NO,p^.location.register,R_MM7);
  478. emit_reg_reg(A_MOVQ,S_NO,R_MM7,p^.location.register);
  479. end;
  480. {$endif}
  481. var
  482. hr : preference;
  483. begin
  484. if is_64bitint(p^.left^.resulttype) then
  485. begin
  486. secondpass(p^.left);
  487. clear_location(p^.location);
  488. p^.location.loc:=LOC_REGISTER;
  489. case p^.left^.location.loc of
  490. LOC_REGISTER :
  491. begin
  492. p^.location.registerlow:=p^.left^.location.registerlow;
  493. p^.location.registerhigh:=p^.left^.location.registerhigh;
  494. end;
  495. LOC_CREGISTER :
  496. begin
  497. p^.location.registerlow:=getregister32;
  498. p^.location.registerhigh:=getregister32;
  499. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,p^.location.registerlow);
  500. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,p^.location.registerhigh);
  501. end;
  502. LOC_REFERENCE,LOC_MEM :
  503. begin
  504. del_reference(p^.left^.location.reference);
  505. p^.location.registerlow:=getregister32;
  506. p^.location.registerhigh:=getregister32;
  507. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  508. newreference(p^.left^.location.reference),p^.location.registerlow)));
  509. hr:=newreference(p^.left^.location.reference);
  510. inc(hr^.offset,4);
  511. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  512. hr,p^.location.registerhigh)));
  513. end;
  514. end;
  515. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.registerlow)));
  516. exprasmlist^.concat(new(pai386,op_const_reg(A_ADC,S_L,0,p^.location.registerhigh)));
  517. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.registerhigh)));
  518. end
  519. else
  520. begin
  521. secondpass(p^.left);
  522. p^.location.loc:=LOC_REGISTER;
  523. case p^.left^.location.loc of
  524. LOC_REGISTER:
  525. begin
  526. p^.location.register:=p^.left^.location.register;
  527. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.register)));
  528. end;
  529. LOC_CREGISTER:
  530. begin
  531. p^.location.register:=getregister32;
  532. emit_reg_reg(A_MOV,S_L,p^.location.register,
  533. p^.location.register);
  534. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.register)));
  535. end;
  536. {$ifdef SUPPORT_MMX}
  537. LOC_MMXREGISTER:
  538. begin
  539. set_location(p^.location,p^.left^.location);
  540. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  541. do_mmx_neg;
  542. end;
  543. LOC_CMMXREGISTER:
  544. begin
  545. p^.location.register:=getregistermmx;
  546. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  547. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,
  548. p^.location.register);
  549. do_mmx_neg;
  550. end;
  551. {$endif SUPPORT_MMX}
  552. LOC_REFERENCE,LOC_MEM:
  553. begin
  554. del_reference(p^.left^.location.reference);
  555. if (p^.left^.resulttype^.deftype=floatdef) and
  556. (pfloatdef(p^.left^.resulttype)^.typ<>f32bit) then
  557. begin
  558. p^.location.loc:=LOC_FPU;
  559. floatload(pfloatdef(p^.left^.resulttype)^.typ,
  560. p^.left^.location.reference);
  561. exprasmlist^.concat(new(pai386,op_none(A_FCHS,S_NO)));
  562. end
  563. {$ifdef SUPPORT_MMX}
  564. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(p^.left^.resulttype) then
  565. begin
  566. p^.location.register:=getregistermmx;
  567. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  568. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVQ,S_NO,
  569. newreference(p^.left^.location.reference),
  570. p^.location.register)));
  571. do_mmx_neg;
  572. end
  573. {$endif SUPPORT_MMX}
  574. else
  575. begin
  576. p^.location.register:=getregister32;
  577. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  578. newreference(p^.left^.location.reference),
  579. p^.location.register)));
  580. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.register)));
  581. end;
  582. end;
  583. LOC_FPU:
  584. begin
  585. p^.location.loc:=LOC_FPU;
  586. exprasmlist^.concat(new(pai386,op_none(A_FCHS,S_NO)));
  587. end;
  588. end;
  589. end;
  590. { Here was a problem... }
  591. { Operand to be negated always }
  592. { seems to be converted to signed }
  593. { 32-bit before doing neg!! }
  594. { So this is useless... }
  595. { emitoverflowcheck(p);}
  596. end;
  597. {*****************************************************************************
  598. SecondNot
  599. *****************************************************************************}
  600. procedure secondnot(var p : ptree);
  601. const
  602. flagsinvers : array[F_E..F_BE] of tresflags =
  603. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  604. F_A,F_AE,F_B,F_BE);
  605. var
  606. hl : plabel;
  607. opsize : topsize;
  608. hr : preference;
  609. begin
  610. if is_boolean(p^.resulttype) then
  611. begin
  612. opsize:=def_opsize(p^.resulttype);
  613. case p^.left^.location.loc of
  614. LOC_JUMP :
  615. begin
  616. hl:=truelabel;
  617. truelabel:=falselabel;
  618. falselabel:=hl;
  619. secondpass(p^.left);
  620. maketojumpbool(p^.left);
  621. hl:=truelabel;
  622. truelabel:=falselabel;
  623. falselabel:=hl;
  624. end;
  625. LOC_FLAGS :
  626. begin
  627. secondpass(p^.left);
  628. p^.location.resflags:=flagsinvers[p^.left^.location.resflags];
  629. end;
  630. LOC_REGISTER :
  631. begin
  632. secondpass(p^.left);
  633. {p^.location.register:=p^.left^.location.register;
  634. exprasmlist^.concat(new(pai386,op_const_reg(A_XOR,opsize,1,p^.location.register)));}
  635. p^.location.loc:=LOC_FLAGS;
  636. p^.location.resflags:=F_E;
  637. exprasmlist^.concat(new(pai386,op_reg_reg(A_TEST,opsize,
  638. p^.left^.location.register,p^.left^.location.register)));
  639. ungetregister(p^.left^.location.register);
  640. end;
  641. LOC_CREGISTER :
  642. begin
  643. secondpass(p^.left);
  644. clear_location(p^.location);
  645. p^.location.loc:=LOC_REGISTER;
  646. p^.location.register:=def_getreg(p^.resulttype);
  647. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,p^.location.register);
  648. exprasmlist^.concat(new(pai386,op_reg_reg(A_TEST,opsize,p^.location.register,p^.location.register)));
  649. ungetregister(p^.location.register);
  650. p^.location.loc:=LOC_FLAGS;
  651. p^.location.resflags:=F_E;
  652. end;
  653. LOC_REFERENCE,
  654. LOC_MEM :
  655. begin
  656. secondpass(p^.left);
  657. clear_location(p^.location);
  658. p^.location.loc:=LOC_REGISTER;
  659. del_reference(p^.left^.location.reference);
  660. { this was placed before del_ref => internaalerror(10) }
  661. p^.location.register:=def_getreg(p^.resulttype);
  662. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  663. newreference(p^.left^.location.reference),p^.location.register)));
  664. exprasmlist^.concat(new(pai386,op_reg_reg(A_TEST,opsize,p^.location.register,p^.location.register)));
  665. ungetregister(p^.location.register);
  666. p^.location.loc:=LOC_FLAGS;
  667. p^.location.resflags:=F_E;
  668. end;
  669. end;
  670. end
  671. {$ifdef SUPPORT_MMX}
  672. else
  673. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(p^.left^.resulttype) then
  674. begin
  675. secondpass(p^.left);
  676. p^.location.loc:=LOC_MMXREGISTER;
  677. { prepare EDI }
  678. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,$ffffffff,R_EDI)));
  679. { load operand }
  680. case p^.left^.location.loc of
  681. LOC_MMXREGISTER:
  682. set_location(p^.location,p^.left^.location);
  683. LOC_CMMXREGISTER:
  684. begin
  685. p^.location.register:=getregistermmx;
  686. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,p^.location.register);
  687. end;
  688. LOC_REFERENCE,LOC_MEM:
  689. begin
  690. del_reference(p^.left^.location.reference);
  691. p^.location.register:=getregistermmx;
  692. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVQ,S_NO,
  693. newreference(p^.left^.location.reference),p^.location.register)));
  694. end;
  695. end;
  696. { load mask }
  697. emit_reg_reg(A_MOV,S_D,R_EDI,R_MM7);
  698. { lower 32 bit }
  699. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  700. { shift mask }
  701. exprasmlist^.concat(new(pai386,op_const_reg(A_PSLLQ,S_NO,32,R_MM7)));
  702. { higher 32 bit }
  703. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  704. end
  705. {$endif SUPPORT_MMX}
  706. else if is_64bitint(p^.left^.resulttype) then
  707. begin
  708. secondpass(p^.left);
  709. clear_location(p^.location);
  710. p^.location.loc:=LOC_REGISTER;
  711. case p^.left^.location.loc of
  712. LOC_REGISTER :
  713. begin
  714. p^.location.registerlow:=p^.left^.location.registerlow;
  715. p^.location.registerhigh:=p^.left^.location.registerhigh;
  716. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.registerlow)));
  717. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.registerhigh)));
  718. end;
  719. LOC_CREGISTER :
  720. begin
  721. p^.location.registerlow:=getregister32;
  722. p^.location.registerhigh:=getregister32;
  723. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerlow,p^.location.registerlow);
  724. emit_reg_reg(A_MOV,S_L,p^.left^.location.registerhigh,p^.location.registerhigh);
  725. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.registerlow)));
  726. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.registerhigh)));
  727. end;
  728. LOC_REFERENCE,LOC_MEM :
  729. begin
  730. del_reference(p^.left^.location.reference);
  731. p^.location.registerlow:=getregister32;
  732. p^.location.registerhigh:=getregister32;
  733. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  734. newreference(p^.left^.location.reference),p^.location.registerlow)));
  735. hr:=newreference(p^.left^.location.reference);
  736. inc(hr^.offset,4);
  737. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  738. hr,p^.location.registerhigh)));
  739. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.registerlow)));
  740. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.registerhigh)));
  741. end;
  742. end;
  743. end
  744. else
  745. begin
  746. secondpass(p^.left);
  747. clear_location(p^.location);
  748. p^.location.loc:=LOC_REGISTER;
  749. case p^.left^.location.loc of
  750. LOC_REGISTER :
  751. begin
  752. p^.location.register:=p^.left^.location.register;
  753. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  754. end;
  755. LOC_CREGISTER :
  756. begin
  757. p^.location.register:=getregister32;
  758. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,p^.location.register);
  759. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  760. end;
  761. LOC_REFERENCE,LOC_MEM :
  762. begin
  763. del_reference(p^.left^.location.reference);
  764. p^.location.register:=getregister32;
  765. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  766. newreference(p^.left^.location.reference),p^.location.register)));
  767. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  768. end;
  769. end;
  770. end;
  771. end;
  772. end.
  773. {
  774. $Log$
  775. Revision 1.23 1999-05-08 20:41:08 jonas
  776. + positive number MOD power of 2 now done with AND instruction
  777. * fix to division of positive numbers by power of 2
  778. * the result of a MOD is left in EDX if possible
  779. Revision 1.22 1999/05/01 13:24:11 peter
  780. * merged nasm compiler
  781. * old asm moved to oldasm/
  782. Revision 1.21 1999/04/16 13:42:27 jonas
  783. * more regalloc fixes (still not complete)
  784. Revision 1.20 1999/02/22 02:15:13 peter
  785. * updates for ag386bin
  786. Revision 1.19 1999/02/04 10:49:40 florian
  787. + range checking for ansi- and widestrings
  788. * made it compilable with TP
  789. Revision 1.18 1999/02/03 10:11:12 pierre
  790. * fix for bug0211 for i386
  791. Revision 1.17 1999/01/21 22:10:41 peter
  792. * fixed array of const
  793. * generic platform independent high() support
  794. Revision 1.16 1999/01/19 10:51:32 pierre
  795. * fix to bug0183 in secondnot
  796. Revision 1.15 1998/12/11 16:50:22 florian
  797. + typed const int64 and qword
  798. + unary minus-operator q1:=-q2;
  799. + not-operator
  800. Revision 1.14 1998/12/11 16:10:07 florian
  801. + shifting for 64 bit ints added
  802. * bug in getexplicitregister32 fixed: usableregs wasn't decremented !!
  803. Revision 1.13 1998/12/11 00:02:52 peter
  804. + globtype,tokens,version unit splitted from globals
  805. Revision 1.12 1998/11/26 21:45:29 jonas
  806. - removed A_CLTD opcode (use A_CDQ instead)
  807. * changed cbw, cwde and cwd to cbtw, cwtl and cwtd in att_op2str array
  808. * in daopt386: adapted AsmInstr array to reflect changes + fixed line too long
  809. Revision 1.11 1998/11/05 14:26:02 peter
  810. * fixed shlshr which would push ecx when not needed
  811. Revision 1.10 1998/10/20 13:12:38 peter
  812. * fixed 'not not boolean', the location was not set to register
  813. Revision 1.9 1998/10/20 08:06:42 pierre
  814. * several memory corruptions due to double freemem solved
  815. => never use p^.loc.location:=p^.left^.loc.location;
  816. + finally I added now by default
  817. that ra386dir translates global and unit symbols
  818. + added a first field in tsymtable and
  819. a nextsym field in tsym
  820. (this allows to obtain ordered type info for
  821. records and objects in gdb !)
  822. Revision 1.8 1998/10/09 08:56:24 pierre
  823. * several memory leaks fixed
  824. Revision 1.7 1998/09/17 09:42:17 peter
  825. + pass_2 for cg386
  826. * Message() -> CGMessage() for pass_1/pass_2
  827. Revision 1.6 1998/09/09 14:37:37 florian
  828. * mod/div for cardinal type fixed
  829. Revision 1.5 1998/08/23 16:07:20 florian
  830. * internalerror with mod/div fixed
  831. Revision 1.4 1998/08/18 09:24:38 pierre
  832. * small warning position bug fixed
  833. * support_mmx switches splitting was missing
  834. * rhide error and warning output corrected
  835. Revision 1.3 1998/06/05 17:44:12 peter
  836. * splitted cgi386
  837. Revision 1.2 1998/06/02 17:02:59 pierre
  838. * with node corrected for objects
  839. * small bugs for SUPPORT_MMX fixed
  840. Revision 1.1 1998/06/01 16:50:18 peter
  841. + boolean -> ord conversion
  842. * fixed ord -> boolean conversion
  843. }