cg386mat.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. i386,cgai386,tgeni386;
  33. {*****************************************************************************
  34. SecondModDiv
  35. *****************************************************************************}
  36. procedure secondmoddiv(var p : ptree);
  37. var
  38. hreg1 : tregister;
  39. pushed,popeax,popedx : boolean;
  40. power : longint;
  41. hl : plabel;
  42. begin
  43. secondpass(p^.left);
  44. set_location(p^.location,p^.left^.location);
  45. pushed:=maybe_push(p^.right^.registers32,p);
  46. secondpass(p^.right);
  47. if pushed then restore(p);
  48. { put numerator in register }
  49. if p^.left^.location.loc<>LOC_REGISTER then
  50. begin
  51. if p^.left^.location.loc=LOC_CREGISTER then
  52. begin
  53. hreg1:=getregister32;
  54. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,hreg1);
  55. end
  56. else
  57. begin
  58. del_reference(p^.left^.location.reference);
  59. hreg1:=getregister32;
  60. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  61. hreg1)));
  62. end;
  63. clear_location(p^.left^.location);
  64. p^.left^.location.loc:=LOC_REGISTER;
  65. p^.left^.location.register:=hreg1;
  66. end
  67. else hreg1:=p^.left^.location.register;
  68. if (p^.treetype=divn) and (p^.right^.treetype=ordconstn) and
  69. ispowerof2(p^.right^.value,power) then
  70. begin
  71. exprasmlist^.concat(new(pai386,op_reg_reg(A_OR,S_L,hreg1,hreg1)));
  72. getlabel(hl);
  73. emitl(A_JNS,hl);
  74. if power=1 then
  75. exprasmlist^.concat(new(pai386,op_reg(A_INC,S_L,hreg1)))
  76. else exprasmlist^.concat(new(pai386,op_const_reg(A_ADD,S_L,p^.right^.value-1,hreg1)));
  77. emitl(A_LABEL,hl);
  78. exprasmlist^.concat(new(pai386,op_const_reg(A_SAR,S_L,power,hreg1)));
  79. end
  80. else
  81. begin
  82. { bring denominator to EDI }
  83. { EDI is always free, it's }
  84. { only used for temporary }
  85. { purposes }
  86. if (p^.right^.location.loc<>LOC_REGISTER) and
  87. (p^.right^.location.loc<>LOC_CREGISTER) then
  88. begin
  89. del_reference(p^.right^.location.reference);
  90. p^.left^.location.loc:=LOC_REGISTER;
  91. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),R_EDI)));
  92. end
  93. else
  94. begin
  95. ungetregister32(p^.right^.location.register);
  96. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,R_EDI);
  97. end;
  98. popedx:=false;
  99. popeax:=false;
  100. if hreg1=R_EDX then
  101. begin
  102. if not(R_EAX in unused) then
  103. begin
  104. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  105. popeax:=true;
  106. end;
  107. emit_reg_reg(A_MOV,S_L,R_EDX,R_EAX);
  108. end
  109. else
  110. begin
  111. if not(R_EDX in unused) then
  112. begin
  113. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EDX)));
  114. popedx:=true;
  115. end;
  116. if hreg1<>R_EAX then
  117. begin
  118. if not(R_EAX in unused) then
  119. begin
  120. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_EAX)));
  121. popeax:=true;
  122. end;
  123. emit_reg_reg(A_MOV,S_L,hreg1,R_EAX);
  124. end;
  125. end;
  126. { sign extension depends on the left type }
  127. if porddef(p^.left^.resulttype)^.typ=u32bit then
  128. exprasmlist^.concat(new(pai386,op_reg_reg(A_XOR,S_L,R_EDX,R_EDX)))
  129. else
  130. exprasmlist^.concat(new(pai386,op_none(A_CDQ,S_NO)));
  131. { division depends on the right type }
  132. if porddef(p^.right^.resulttype)^.typ=u32bit then
  133. exprasmlist^.concat(new(pai386,op_reg(A_DIV,S_L,R_EDI)))
  134. else
  135. exprasmlist^.concat(new(pai386,op_reg(A_IDIV,S_L,R_EDI)));
  136. if p^.treetype=divn then
  137. begin
  138. { if result register is busy then copy }
  139. if popeax then
  140. begin
  141. if hreg1=R_EAX then
  142. internalerror(112);
  143. emit_reg_reg(A_MOV,S_L,R_EAX,hreg1)
  144. end
  145. else
  146. if hreg1<>R_EAX then
  147. emit_reg_reg(A_MOV,S_L,R_EAX,hreg1);
  148. end
  149. else
  150. emit_reg_reg(A_MOV,S_L,R_EDX,hreg1);
  151. if popeax then
  152. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EAX)));
  153. if popedx then
  154. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_EDX)));
  155. end;
  156. { this registers are always used when div/mod are present }
  157. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  158. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  159. clear_location(p^.location);
  160. p^.location.loc:=LOC_REGISTER;
  161. p^.location.register:=hreg1;
  162. end;
  163. {*****************************************************************************
  164. SecondShlShr
  165. *****************************************************************************}
  166. procedure secondshlshr(var p : ptree);
  167. var
  168. hregister1,hregister2,hregister3 : tregister;
  169. pushed,popecx : boolean;
  170. op : tasmop;
  171. begin
  172. popecx:=false;
  173. secondpass(p^.left);
  174. pushed:=maybe_push(p^.right^.registers32,p);
  175. secondpass(p^.right);
  176. if pushed then
  177. restore(p);
  178. { load left operators in a register }
  179. if p^.left^.location.loc<>LOC_REGISTER then
  180. begin
  181. if p^.left^.location.loc=LOC_CREGISTER then
  182. begin
  183. hregister1:=getregister32;
  184. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,
  185. hregister1);
  186. end
  187. else
  188. begin
  189. del_reference(p^.left^.location.reference);
  190. hregister1:=getregister32;
  191. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.left^.location.reference),
  192. hregister1)));
  193. end;
  194. end
  195. else
  196. hregister1:=p^.left^.location.register;
  197. { determine operator }
  198. if p^.treetype=shln then
  199. op:=A_SHL
  200. else
  201. op:=A_SHR;
  202. { shifting by a constant directly decode: }
  203. if (p^.right^.treetype=ordconstn) then
  204. begin
  205. exprasmlist^.concat(new(pai386,op_const_reg(op,S_L,p^.right^.location.reference.offset and 31,
  206. hregister1)));
  207. p^.location.loc:=LOC_REGISTER;
  208. p^.location.register:=hregister1;
  209. end
  210. else
  211. begin
  212. { load right operators in a register }
  213. if p^.right^.location.loc<>LOC_REGISTER then
  214. begin
  215. if p^.right^.location.loc=LOC_CREGISTER then
  216. begin
  217. hregister2:=getregister32;
  218. emit_reg_reg(A_MOV,S_L,p^.right^.location.register,
  219. hregister2);
  220. end
  221. else
  222. begin
  223. del_reference(p^.right^.location.reference);
  224. hregister2:=getregister32;
  225. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,newreference(p^.right^.location.reference),
  226. hregister2)));
  227. end;
  228. end
  229. else
  230. hregister2:=p^.right^.location.register;
  231. { left operator is already in a register }
  232. { hence are both in a register }
  233. { is it in the case ECX ? }
  234. if (hregister1=R_ECX) then
  235. begin
  236. { then only swap }
  237. emit_reg_reg(A_XCHG,S_L,hregister1,hregister2);
  238. hregister3:=hregister1;
  239. hregister1:=hregister2;
  240. hregister2:=hregister3;
  241. end
  242. { if second operator not in ECX ? }
  243. else if (hregister2<>R_ECX) then
  244. begin
  245. { ECX occupied then push it }
  246. if not (R_ECX in unused) then
  247. begin
  248. popecx:=true;
  249. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,R_ECX)));
  250. end;
  251. emit_reg_reg(A_MOV,S_L,hregister2,R_ECX);
  252. ungetregister32(hregister2);
  253. end;
  254. { right operand is in ECX }
  255. emit_reg_reg(op,S_L,R_CL,hregister1);
  256. { maybe ECX back }
  257. if popecx then
  258. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,R_ECX)));
  259. p^.location.register:=hregister1;
  260. end;
  261. end;
  262. {*****************************************************************************
  263. SecondUmMinus
  264. *****************************************************************************}
  265. procedure secondumminus(var p : ptree);
  266. {$ifdef SUPPORT_MMX}
  267. procedure do_mmx_neg;
  268. var
  269. op : tasmop;
  270. begin
  271. p^.location.loc:=LOC_MMXREGISTER;
  272. if cs_mmx_saturation in aktlocalswitches then
  273. case mmx_type(p^.resulttype) of
  274. mmxs8bit:
  275. op:=A_PSUBSB;
  276. mmxu8bit:
  277. op:=A_PSUBUSB;
  278. mmxs16bit,mmxfixed16:
  279. op:=A_PSUBSW;
  280. mmxu16bit:
  281. op:=A_PSUBUSW;
  282. end
  283. else
  284. case mmx_type(p^.resulttype) of
  285. mmxs8bit,mmxu8bit:
  286. op:=A_PSUBB;
  287. mmxs16bit,mmxu16bit,mmxfixed16:
  288. op:=A_PSUBW;
  289. mmxs32bit,mmxu32bit:
  290. op:=A_PSUBD;
  291. end;
  292. emit_reg_reg(op,S_NO,p^.location.register,R_MM7);
  293. emit_reg_reg(A_MOVQ,S_NO,R_MM7,p^.location.register);
  294. end;
  295. {$endif}
  296. begin
  297. secondpass(p^.left);
  298. p^.location.loc:=LOC_REGISTER;
  299. case p^.left^.location.loc of
  300. LOC_REGISTER:
  301. begin
  302. p^.location.register:=p^.left^.location.register;
  303. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.register)));
  304. end;
  305. LOC_CREGISTER:
  306. begin
  307. p^.location.register:=getregister32;
  308. emit_reg_reg(A_MOV,S_L,p^.location.register,
  309. p^.location.register);
  310. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.register)));
  311. end;
  312. {$ifdef SUPPORT_MMX}
  313. LOC_MMXREGISTER:
  314. begin
  315. set_location(p^.location,p^.left^.location);
  316. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  317. do_mmx_neg;
  318. end;
  319. LOC_CMMXREGISTER:
  320. begin
  321. p^.location.register:=getregistermmx;
  322. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  323. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,
  324. p^.location.register);
  325. do_mmx_neg;
  326. end;
  327. {$endif SUPPORT_MMX}
  328. LOC_REFERENCE,LOC_MEM:
  329. begin
  330. del_reference(p^.left^.location.reference);
  331. if (p^.left^.resulttype^.deftype=floatdef) and
  332. (pfloatdef(p^.left^.resulttype)^.typ<>f32bit) then
  333. begin
  334. p^.location.loc:=LOC_FPU;
  335. floatload(pfloatdef(p^.left^.resulttype)^.typ,
  336. p^.left^.location.reference);
  337. exprasmlist^.concat(new(pai386,op_none(A_FCHS,S_NO)));
  338. end
  339. {$ifdef SUPPORT_MMX}
  340. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(p^.left^.resulttype) then
  341. begin
  342. p^.location.register:=getregistermmx;
  343. emit_reg_reg(A_PXOR,S_NO,R_MM7,R_MM7);
  344. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVQ,S_NO,
  345. newreference(p^.left^.location.reference),
  346. p^.location.register)));
  347. do_mmx_neg;
  348. end
  349. {$endif SUPPORT_MMX}
  350. else
  351. begin
  352. p^.location.register:=getregister32;
  353. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  354. newreference(p^.left^.location.reference),
  355. p^.location.register)));
  356. exprasmlist^.concat(new(pai386,op_reg(A_NEG,S_L,p^.location.register)));
  357. end;
  358. end;
  359. LOC_FPU:
  360. begin
  361. p^.location.loc:=LOC_FPU;
  362. exprasmlist^.concat(new(pai386,op_none(A_FCHS,S_NO)));
  363. end;
  364. end;
  365. { Here was a problem... }
  366. { Operand to be negated always }
  367. { seems to be converted to signed }
  368. { 32-bit before doing neg!! }
  369. { So this is useless... }
  370. { emitoverflowcheck(p);}
  371. end;
  372. {*****************************************************************************
  373. SecondNot
  374. *****************************************************************************}
  375. procedure secondnot(var p : ptree);
  376. const
  377. flagsinvers : array[F_E..F_BE] of tresflags =
  378. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  379. F_A,F_AE,F_B,F_BE);
  380. var
  381. hl : plabel;
  382. opsize : topsize;
  383. begin
  384. if is_boolean(p^.resulttype) then
  385. begin
  386. opsize:=def_opsize(p^.resulttype);
  387. case p^.left^.location.loc of
  388. LOC_JUMP :
  389. begin
  390. hl:=truelabel;
  391. truelabel:=falselabel;
  392. falselabel:=hl;
  393. secondpass(p^.left);
  394. maketojumpbool(p^.left);
  395. hl:=truelabel;
  396. truelabel:=falselabel;
  397. falselabel:=hl;
  398. end;
  399. LOC_FLAGS :
  400. begin
  401. secondpass(p^.left);
  402. p^.location.resflags:=flagsinvers[p^.left^.location.resflags];
  403. end;
  404. LOC_REGISTER :
  405. begin
  406. secondpass(p^.left);
  407. p^.location.register:=p^.left^.location.register;
  408. exprasmlist^.concat(new(pai386,op_const_reg(A_XOR,opsize,1,p^.location.register)));
  409. end;
  410. LOC_CREGISTER :
  411. begin
  412. secondpass(p^.left);
  413. clear_location(p^.location);
  414. p^.location.loc:=LOC_REGISTER;
  415. p^.location.register:=def_getreg(p^.resulttype);
  416. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,p^.location.register);
  417. exprasmlist^.concat(new(pai386,op_const_reg(A_XOR,opsize,1,p^.location.register)));
  418. end;
  419. LOC_REFERENCE,
  420. LOC_MEM :
  421. begin
  422. secondpass(p^.left);
  423. clear_location(p^.location);
  424. p^.location.loc:=LOC_REGISTER;
  425. p^.location.register:=def_getreg(p^.resulttype);
  426. del_reference(p^.left^.location.reference);
  427. if p^.left^.location.loc=LOC_CREGISTER then
  428. emit_reg_reg(A_MOV,opsize,p^.left^.location.register,p^.location.register)
  429. else
  430. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,opsize,
  431. newreference(p^.left^.location.reference),p^.location.register)));
  432. exprasmlist^.concat(new(pai386,op_const_reg(A_XOR,opsize,1,p^.location.register)));
  433. end;
  434. end;
  435. end
  436. {$ifdef SUPPORT_MMX}
  437. else
  438. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(p^.left^.resulttype) then
  439. begin
  440. secondpass(p^.left);
  441. p^.location.loc:=LOC_MMXREGISTER;
  442. { prepare EDI }
  443. exprasmlist^.concat(new(pai386,op_const_reg(A_MOV,S_L,$ffffffff,R_EDI)));
  444. { load operand }
  445. case p^.left^.location.loc of
  446. LOC_MMXREGISTER:
  447. set_location(p^.location,p^.left^.location);
  448. LOC_CMMXREGISTER:
  449. begin
  450. p^.location.register:=getregistermmx;
  451. emit_reg_reg(A_MOVQ,S_NO,p^.left^.location.register,p^.location.register);
  452. end;
  453. LOC_REFERENCE,LOC_MEM:
  454. begin
  455. del_reference(p^.left^.location.reference);
  456. p^.location.register:=getregistermmx;
  457. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOVQ,S_NO,
  458. newreference(p^.left^.location.reference),p^.location.register)));
  459. end;
  460. end;
  461. { load mask }
  462. emit_reg_reg(A_MOV,S_D,R_EDI,R_MM7);
  463. { lower 32 bit }
  464. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  465. { shift mask }
  466. exprasmlist^.concat(new(pai386,op_const_reg(A_PSLLQ,S_NO,32,R_MM7)));
  467. { higher 32 bit }
  468. emit_reg_reg(A_PXOR,S_D,R_MM7,p^.location.register);
  469. end
  470. {$endif SUPPORT_MMX}
  471. else
  472. begin
  473. secondpass(p^.left);
  474. clear_location(p^.location);
  475. p^.location.loc:=LOC_REGISTER;
  476. case p^.left^.location.loc of
  477. LOC_REGISTER :
  478. begin
  479. p^.location.register:=p^.left^.location.register;
  480. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  481. end;
  482. LOC_CREGISTER :
  483. begin
  484. p^.location.register:=getregister32;
  485. emit_reg_reg(A_MOV,S_L,p^.left^.location.register,p^.location.register);
  486. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  487. end;
  488. LOC_REFERENCE,LOC_MEM :
  489. begin
  490. del_reference(p^.left^.location.reference);
  491. p^.location.register:=getregister32;
  492. exprasmlist^.concat(new(pai386,op_ref_reg(A_MOV,S_L,
  493. newreference(p^.left^.location.reference),p^.location.register)));
  494. exprasmlist^.concat(new(pai386,op_reg(A_NOT,S_L,p^.location.register)));
  495. end;
  496. end;
  497. end;
  498. end;
  499. end.
  500. {
  501. $Log$
  502. Revision 1.13 1998-12-11 00:02:52 peter
  503. + globtype,tokens,version unit splitted from globals
  504. Revision 1.12 1998/11/26 21:45:29 jonas
  505. - removed A_CLTD opcode (use A_CDQ instead)
  506. * changed cbw, cwde and cwd to cbtw, cwtl and cwtd in att_op2str array
  507. * in daopt386: adapted AsmInstr array to reflect changes + fixed line too long
  508. Revision 1.11 1998/11/05 14:26:02 peter
  509. * fixed shlshr which would push ecx when not needed
  510. Revision 1.10 1998/10/20 13:12:38 peter
  511. * fixed 'not not boolean', the location was not set to register
  512. Revision 1.9 1998/10/20 08:06:42 pierre
  513. * several memory corruptions due to double freemem solved
  514. => never use p^.loc.location:=p^.left^.loc.location;
  515. + finally I added now by default
  516. that ra386dir translates global and unit symbols
  517. + added a first field in tsymtable and
  518. a nextsym field in tsym
  519. (this allows to obtain ordered type info for
  520. records and objects in gdb !)
  521. Revision 1.8 1998/10/09 08:56:24 pierre
  522. * several memory leaks fixed
  523. Revision 1.7 1998/09/17 09:42:17 peter
  524. + pass_2 for cg386
  525. * Message() -> CGMessage() for pass_1/pass_2
  526. Revision 1.6 1998/09/09 14:37:37 florian
  527. * mod/div for cardinal type fixed
  528. Revision 1.5 1998/08/23 16:07:20 florian
  529. * internalerror with mod/div fixed
  530. Revision 1.4 1998/08/18 09:24:38 pierre
  531. * small warning position bug fixed
  532. * support_mmx switches splitting was missing
  533. * rhide error and warning output corrected
  534. Revision 1.3 1998/06/05 17:44:12 peter
  535. * splitted cgi386
  536. Revision 1.2 1998/06/02 17:02:59 pierre
  537. * with node corrected for objects
  538. * small bugs for SUPPORT_MMX fixed
  539. Revision 1.1 1998/06/01 16:50:18 peter
  540. + boolean -> ord conversion
  541. * fixed ord -> boolean conversion
  542. }