tgeni386.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. {
  2. $Id$
  3. Copyright (C) 1998-2000 by Florian Klaempfl
  4. This unit handles the temporary variables stuff for i386
  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 tgeni386;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. cobjects,globals,tree,hcodegen,verbose,aasm,
  23. cpubase,cpuasm
  24. ;
  25. type
  26. tregisterset = set of tregister;
  27. tpushed = array[R_EAX..R_MM6] of boolean;
  28. tsaved = array[R_EAX..R_MM6] of longint;
  29. const
  30. usablereg32 : byte = 4;
  31. { this value is used in tsaved, if the register isn't saved }
  32. reg_not_saved = $7fffffff;
  33. {$ifdef SUPPORT_MMX}
  34. usableregmmx : byte = 8;
  35. {$endif SUPPORT_MMX}
  36. var
  37. { tries to hold the amount of times which the current tree is processed }
  38. t_times : longint;
  39. {$ifdef TEMPREGDEBUG}
  40. procedure testregisters32;
  41. {$endif TEMPREGDEBUG}
  42. function getregister32 : tregister;
  43. procedure ungetregister32(r : tregister);
  44. { tries to allocate the passed register, if possible }
  45. function getexplicitregister32(r : tregister) : tregister;
  46. {$ifdef SUPPORT_MMX}
  47. function getregistermmx : tregister;
  48. procedure ungetregistermmx(r : tregister);
  49. {$endif SUPPORT_MMX}
  50. procedure ungetregister(r : tregister);
  51. procedure cleartempgen;
  52. procedure del_reference(const ref : treference);
  53. procedure del_locref(const location : tlocation);
  54. procedure del_location(const l : tlocation);
  55. { pushs and restores registers }
  56. procedure pushusedregisters(var pushed : tpushed;b : byte);
  57. procedure popusedregisters(const pushed : tpushed);
  58. { saves and restores used registers to temp. values }
  59. procedure saveusedregisters(var saved : tsaved;b : byte);
  60. procedure restoreusedregisters(const saved : tsaved);
  61. { increments the push count of all registers in b}
  62. procedure incrementregisterpushed(b : byte);
  63. procedure clearregistercount;
  64. procedure resetusableregisters;
  65. { corrects the fpu stack register by ofs }
  66. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  67. type
  68. {$ifdef SUPPORT_MMX}
  69. regvar_longintarray = array[R_EAX..R_MM6] of longint;
  70. regvar_booleanarray = array[R_EAX..R_MM6] of boolean;
  71. regvar_ptreearray = array[R_EAX..R_MM6] of ptree;
  72. {$else SUPPORT_MMX}
  73. regvar_longintarray = array[R_EAX..R_EDI] of longint;
  74. regvar_booleanarray = array[R_EAX..R_EDI] of boolean;
  75. regvar_ptreearray = array[R_EAX..R_EDI] of ptree;
  76. {$endif SUPPORT_MMX}
  77. var
  78. unused,usableregs : tregisterset;
  79. c_usableregs : longint;
  80. { uses only 1 byte while a set uses in FPC 32 bytes }
  81. usedinproc : byte;
  82. fpuvaroffset : byte;
  83. { count, how much a register must be pushed if it is used as register }
  84. { variable }
  85. reg_pushes : regvar_longintarray;
  86. is_reg_var : regvar_booleanarray;
  87. {$ifdef TEMPREGDEBUG}
  88. reg_user : regvar_ptreearray;
  89. reg_releaser : regvar_ptreearray;
  90. {$endif TEMPREGDEBUG}
  91. implementation
  92. uses
  93. globtype,temp_gen;
  94. procedure incrementregisterpushed(b : byte);
  95. var
  96. regi : tregister;
  97. begin
  98. for regi:=R_EAX to R_EDI do
  99. begin
  100. if (b and ($80 shr word(regi)))<>0 then
  101. inc(reg_pushes[regi],t_times*2);
  102. end;
  103. end;
  104. procedure pushusedregisters(var pushed : tpushed;b : byte);
  105. var
  106. r : tregister;
  107. {$ifdef SUPPORT_MMX}
  108. hr : preference;
  109. {$endif}
  110. begin
  111. usedinproc:=usedinproc or b;
  112. for r:=R_EAX to R_EBX do
  113. begin
  114. pushed[r]:=false;
  115. { if the register is used by the calling subroutine }
  116. if ((b and ($80 shr byte(r)))<>0) then
  117. begin
  118. { and is present in use }
  119. if not(r in unused) then
  120. begin
  121. { then save it }
  122. exprasmlist^.concat(new(paicpu,op_reg(A_PUSH,S_L,r)));
  123. { here was a big problem !!!!!}
  124. { you cannot do that for a register that is
  125. globally assigned to a var
  126. this also means that you must push it much more
  127. often, but there must be a better way
  128. maybe by putting the value back to the stack !! }
  129. if not(is_reg_var[r]) then
  130. begin
  131. unused:=unused+[r];
  132. {$ifdef TEMPREGDEBUG}
  133. inc(usablereg32);
  134. {$endif TEMPREGDEBUG}
  135. end;
  136. pushed[r]:=true;
  137. end;
  138. end;
  139. end;
  140. {$ifdef SUPPORT_MMX}
  141. for r:=R_MM0 to R_MM6 do
  142. begin
  143. pushed[r]:=false;
  144. { if the mmx register is in use, save it }
  145. if not(r in unused) then
  146. begin
  147. exprasmlist^.concat(new(paicpu,op_const_reg(
  148. A_SUB,S_L,8,R_ESP)));
  149. new(hr);
  150. reset_reference(hr^);
  151. hr^.base:=R_ESP;
  152. exprasmlist^.concat(new(paicpu,op_reg_ref(
  153. A_MOVQ,S_NO,r,hr)));
  154. if not(is_reg_var[r]) then
  155. begin
  156. unused:=unused+[r];
  157. {$ifdef TEMPREGDEBUG}
  158. inc(usableregmmx);
  159. {$endif TEMPREGDEBUG}
  160. end;
  161. pushed[r]:=true;
  162. end;
  163. end;
  164. {$endif SUPPORT_MMX}
  165. {$ifdef TEMPREGDEBUG}
  166. testregisters32;
  167. {$endif TEMPREGDEBUG}
  168. end;
  169. procedure saveusedregisters(var saved : tsaved;b : byte);
  170. var
  171. r : tregister;
  172. hr : treference;
  173. begin
  174. usedinproc:=usedinproc or b;
  175. for r:=R_EAX to R_EBX do
  176. begin
  177. saved[r]:=reg_not_saved;
  178. { if the register is used by the calling subroutine }
  179. if ((b and ($80 shr byte(r)))<>0) then
  180. begin
  181. { and is present in use }
  182. if not(r in unused) then
  183. begin
  184. { then save it }
  185. gettempofsizereference(4,hr);
  186. saved[r]:=hr.offset;
  187. exprasmlist^.concat(new(paicpu,op_reg_ref(A_MOV,S_L,r,newreference(hr))));
  188. { here was a big problem !!!!!}
  189. { you cannot do that for a register that is
  190. globally assigned to a var
  191. this also means that you must push it much more
  192. often, but there must be a better way
  193. maybe by putting the value back to the stack !! }
  194. if not(is_reg_var[r]) then
  195. begin
  196. unused:=unused+[r];
  197. {$ifdef TEMPREGDEBUG}
  198. inc(usablereg32);
  199. {$endif TEMPREGDEBUG}
  200. end;
  201. end;
  202. end;
  203. end;
  204. {$ifdef SUPPORT_MMX}
  205. for r:=R_MM0 to R_MM6 do
  206. begin
  207. saved[r]:=reg_not_saved;
  208. { if the mmx register is in use, save it }
  209. if not(r in unused) then
  210. begin
  211. gettempofsizereference(8,hr);
  212. exprasmlist^.concat(new(paicpu,op_reg_ref(
  213. A_MOVQ,S_NO,r,newreference(hr))));
  214. if not(is_reg_var[r]) then
  215. begin
  216. unused:=unused+[r];
  217. {$ifdef TEMPREGDEBUG}
  218. inc(usableregmmx);
  219. {$endif TEMPREGDEBUG}
  220. end;
  221. saved[r]:=hr.offset;
  222. end;
  223. end;
  224. {$endif SUPPORT_MMX}
  225. {$ifdef TEMPREGDEBUG}
  226. testregisters32;
  227. {$endif TEMPREGDEBUG}
  228. end;
  229. procedure popusedregisters(const pushed : tpushed);
  230. var
  231. r : tregister;
  232. {$ifdef SUPPORT_MMX}
  233. hr : preference;
  234. {$endif SUPPORT_MMX}
  235. begin
  236. { restore in reverse order: }
  237. {$ifdef SUPPORT_MMX}
  238. for r:=R_MM6 downto R_MM0 do
  239. begin
  240. if pushed[r] then
  241. begin
  242. new(hr);
  243. reset_reference(hr^);
  244. hr^.base:=R_ESP;
  245. exprasmlist^.concat(new(paicpu,op_ref_reg(
  246. A_MOVQ,S_NO,hr,r)));
  247. exprasmlist^.concat(new(paicpu,op_const_reg(
  248. A_ADD,S_L,8,R_ESP)));
  249. unused:=unused-[r];
  250. {$ifdef TEMPREGDEBUG}
  251. dec(usableregmmx);
  252. {$endif TEMPREGDEBUG}
  253. end;
  254. end;
  255. {$endif SUPPORT_MMX}
  256. for r:=R_EBX downto R_EAX do
  257. if pushed[r] then
  258. begin
  259. exprasmlist^.concat(new(paicpu,op_reg(A_POP,S_L,r)));
  260. {$ifdef TEMPREGDEBUG}
  261. if not (r in unused) then
  262. { internalerror(10)
  263. in cg386cal we always restore regs
  264. that appear as used
  265. due to a unused tmep storage PM }
  266. else
  267. dec(usablereg32);
  268. {$endif TEMPREGDEBUG}
  269. unused:=unused-[r];
  270. end;
  271. {$ifdef TEMPREGDEBUG}
  272. testregisters32;
  273. {$endif TEMPREGDEBUG}
  274. end;
  275. procedure restoreusedregisters(const saved : tsaved);
  276. var
  277. r : tregister;
  278. hr : treference;
  279. begin
  280. { restore in reverse order: }
  281. {$ifdef SUPPORT_MMX}
  282. for r:=R_MM6 downto R_MM0 do
  283. begin
  284. if saved[r]<>reg_not_saved then
  285. begin
  286. reset_reference(hr);
  287. hr.base:=frame_pointer;
  288. hr.offset:=saved[r];
  289. exprasmlist^.concat(new(paicpu,op_ref_reg(
  290. A_MOVQ,S_NO,newreference(hr),r)));
  291. unused:=unused-[r];
  292. {$ifdef TEMPREGDEBUG}
  293. dec(usableregmmx);
  294. {$endif TEMPREGDEBUG}
  295. ungetiftemp(hr);
  296. end;
  297. end;
  298. {$endif SUPPORT_MMX}
  299. for r:=R_EBX downto R_EAX do
  300. if saved[r]<>reg_not_saved then
  301. begin
  302. reset_reference(hr);
  303. hr.base:=frame_pointer;
  304. hr.offset:=saved[r];
  305. exprasmlist^.concat(new(paicpu,op_ref_reg(A_MOV,S_L,newreference(hr),r)));
  306. {$ifdef TEMPREGDEBUG}
  307. if not (r in unused) then
  308. internalerror(10)
  309. else
  310. dec(usablereg32);
  311. {$endif TEMPREGDEBUG}
  312. unused:=unused-[r];
  313. ungetiftemp(hr);
  314. end;
  315. {$ifdef TEMPREGDEBUG}
  316. testregisters32;
  317. {$endif TEMPREGDEBUG}
  318. end;
  319. procedure ungetregister(r : tregister);
  320. begin
  321. if r in [R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI] then
  322. ungetregister32(r)
  323. else if r in [R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI] then
  324. ungetregister32(reg16toreg32(r))
  325. else if r in [R_AL,R_BL,R_CL,R_DL] then
  326. ungetregister32(reg8toreg32(r))
  327. {$ifdef SUPPORT_MMX}
  328. else if r in [R_MM0..R_MM6] then
  329. ungetregistermmx(r)
  330. {$endif SUPPORT_MMX}
  331. else internalerror(18);
  332. end;
  333. procedure ungetregister32(r : tregister);
  334. begin
  335. if (r = R_EDI) or
  336. ((not assigned(procinfo^._class)) and (r = R_ESI)) then
  337. begin
  338. exprasmlist^.concat(new(pairegalloc,dealloc(r)));
  339. exit;
  340. end;
  341. if cs_regalloc in aktglobalswitches then
  342. begin
  343. { takes much time }
  344. if not(r in usableregs) then
  345. exit;
  346. unused:=unused+[r];
  347. inc(usablereg32);
  348. end
  349. else
  350. begin
  351. if not(r in [R_EAX,R_EBX,R_ECX,R_EDX]) then
  352. exit;
  353. {$ifdef TEMPREGDEBUG}
  354. if (r in unused) then
  355. {$ifdef EXTTEMPREGDEBUG}
  356. begin
  357. Comment(V_Debug,'register freed twice '+reg2str(r));
  358. testregisters32;
  359. exit;
  360. end
  361. {$else EXTTEMPREGDEBUG}
  362. exit
  363. {$endif EXTTEMPREGDEBUG}
  364. else
  365. {$endif TEMPREGDEBUG}
  366. inc(usablereg32);
  367. unused:=unused+[r];
  368. {$ifdef TEMPREGDEBUG}
  369. reg_releaser[r]:=curptree^;
  370. {$endif TEMPREGDEBUG}
  371. end;
  372. exprasmlist^.concat(new(pairegalloc,dealloc(r)));
  373. {$ifdef TEMPREGDEBUG}
  374. testregisters32;
  375. {$endif TEMPREGDEBUG}
  376. end;
  377. {$ifdef SUPPORT_MMX}
  378. function getregistermmx : tregister;
  379. var
  380. r : tregister;
  381. begin
  382. dec(usableregmmx);
  383. for r:=R_MM0 to R_MM6 do
  384. if r in unused then
  385. begin
  386. unused:=unused-[r];
  387. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  388. getregistermmx:=r;
  389. exit;
  390. end;
  391. internalerror(10);
  392. end;
  393. procedure ungetregistermmx(r : tregister);
  394. begin
  395. if cs_regalloc in aktglobalswitches then
  396. begin
  397. { takes much time }
  398. if not(r in usableregs) then
  399. exit;
  400. unused:=unused+[r];
  401. inc(usableregmmx);
  402. end
  403. else
  404. begin
  405. unused:=unused+[r];
  406. inc(usableregmmx);
  407. end;
  408. end;
  409. {$endif SUPPORT_MMX}
  410. procedure del_reference(const ref : treference);
  411. begin
  412. if ref.is_immediate then
  413. exit;
  414. ungetregister32(ref.base);
  415. ungetregister32(ref.index);
  416. end;
  417. procedure del_locref(const location : tlocation);
  418. begin
  419. if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  420. exit;
  421. if location.reference.is_immediate then
  422. exit;
  423. ungetregister32(location.reference.base);
  424. ungetregister32(location.reference.index);
  425. end;
  426. procedure del_location(const l : tlocation);
  427. begin
  428. case l.loc of
  429. LOC_REGISTER :
  430. ungetregister(l.register);
  431. LOC_MEM,LOC_REFERENCE :
  432. del_reference(l.reference);
  433. end;
  434. end;
  435. {$ifdef TEMPREGDEBUG}
  436. procedure testregisters32;
  437. var test : byte;
  438. begin
  439. test:=0;
  440. if R_EAX in unused then
  441. inc(test);
  442. if R_EBX in unused then
  443. inc(test);
  444. if R_ECX in unused then
  445. inc(test);
  446. if R_EDX in unused then
  447. inc(test);
  448. if test<>usablereg32 then
  449. internalerror(10);
  450. end;
  451. {$endif TEMPREGDEBUG}
  452. function getregister32 : tregister;
  453. begin
  454. if usablereg32=0 then
  455. internalerror(10);
  456. dec(usablereg32);
  457. {$ifdef TEMPREGDEBUG}
  458. if curptree^^.usableregs-usablereg32>curptree^^.registers32 then
  459. internalerror(10);
  460. {$endif TEMPREGDEBUG}
  461. {$ifdef EXTTEMPREGDEBUG}
  462. if curptree^^.usableregs-usablereg32>curptree^^.reallyusedregs then
  463. curptree^^.reallyusedregs:=curptree^^.usableregs-usablereg32;
  464. {$endif EXTTEMPREGDEBUG}
  465. if R_EAX in unused then
  466. begin
  467. unused:=unused-[R_EAX];
  468. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  469. getregister32:=R_EAX;
  470. {$ifdef TEMPREGDEBUG}
  471. reg_user[R_EAX]:=curptree^;
  472. {$endif TEMPREGDEBUG}
  473. exprasmlist^.concat(new(pairegalloc,alloc(R_EAX)));
  474. end
  475. else if R_EDX in unused then
  476. begin
  477. unused:=unused-[R_EDX];
  478. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  479. getregister32:=R_EDX;
  480. {$ifdef TEMPREGDEBUG}
  481. reg_user[R_EDX]:=curptree^;
  482. {$endif TEMPREGDEBUG}
  483. exprasmlist^.concat(new(pairegalloc,alloc(R_EDX)));
  484. end
  485. else if R_EBX in unused then
  486. begin
  487. unused:=unused-[R_EBX];
  488. usedinproc:=usedinproc or ($80 shr byte(R_EBX));
  489. getregister32:=R_EBX;
  490. {$ifdef TEMPREGDEBUG}
  491. reg_user[R_EBX]:=curptree^;
  492. {$endif TEMPREGDEBUG}
  493. exprasmlist^.concat(new(pairegalloc,alloc(R_EBX)));
  494. end
  495. else if R_ECX in unused then
  496. begin
  497. unused:=unused-[R_ECX];
  498. usedinproc:=usedinproc or ($80 shr byte(R_ECX));
  499. getregister32:=R_ECX;
  500. {$ifdef TEMPREGDEBUG}
  501. reg_user[R_ECX]:=curptree^;
  502. {$endif TEMPREGDEBUG}
  503. exprasmlist^.concat(new(pairegalloc,alloc(R_ECX)));
  504. end
  505. else internalerror(10);
  506. {$ifdef TEMPREGDEBUG}
  507. testregisters32;
  508. {$endif TEMPREGDEBUG}
  509. end;
  510. function getexplicitregister32(r : tregister) : tregister;
  511. begin
  512. if r in [R_ESI,R_EDI] then
  513. begin
  514. exprasmlist^.concat(new(pairegalloc,alloc(r)));
  515. getexplicitregister32 := r;
  516. exit;
  517. end;
  518. if r in unused then
  519. begin
  520. dec(usablereg32);
  521. {$ifdef TEMPREGDEBUG}
  522. if curptree^^.usableregs-usablereg32>curptree^^.registers32 then
  523. internalerror(10);
  524. reg_user[r]:=curptree^;
  525. {$endif TEMPREGDEBUG}
  526. unused:=unused-[r];
  527. usedinproc:=usedinproc or ($80 shr byte(r));
  528. exprasmlist^.concat(new(pairegalloc,alloc(r)));
  529. getexplicitregister32:=r;
  530. {$ifdef TEMPREGDEBUG}
  531. testregisters32;
  532. {$endif TEMPREGDEBUG}
  533. end
  534. else
  535. getexplicitregister32:=getregister32;
  536. end;
  537. procedure cleartempgen;
  538. begin
  539. unused:=usableregs;
  540. usablereg32:=c_usableregs;
  541. {fpuvaroffset:=0;
  542. this must only be resetted at each procedure
  543. compilation start PM }
  544. end;
  545. procedure clearregistercount;
  546. var
  547. regi : tregister;
  548. begin
  549. {$ifdef SUPPORT_MMX}
  550. for regi:=R_EAX to R_MM6 do
  551. begin
  552. reg_pushes[regi]:=0;
  553. is_reg_var[regi]:=false;
  554. end;
  555. {$else SUPPORT_MMX}
  556. for regi:=R_EAX to R_EDI do
  557. begin
  558. reg_pushes[regi]:=0;
  559. is_reg_var[regi]:=false;
  560. end;
  561. {$endif SUPPORT_MMX}
  562. end;
  563. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  564. begin
  565. correct_fpuregister:=tregister(longint(r)+ofs);
  566. end;
  567. procedure resetusableregisters;
  568. begin
  569. {$ifdef SUPPORT_MMX}
  570. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX,R_MM0..R_MM6];
  571. c_usableregs:=4;
  572. usableregmmx:=8;
  573. {$else}
  574. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  575. c_usableregs:=4;
  576. {$endif SUPPORT_MMX}
  577. fpuvaroffset:=0;
  578. end;
  579. begin
  580. resetusableregisters;
  581. end.
  582. {
  583. $Log$
  584. Revision 1.6 2000-09-24 15:06:32 peter
  585. * use defines.inc
  586. Revision 1.5 2000/08/27 16:11:55 peter
  587. * moved some util functions from globals,cobjects to cutils
  588. * splitted files into finput,fmodule
  589. Revision 1.4 2000/08/05 13:32:39 peter
  590. * fixed build prob without support_mmx
  591. Revision 1.3 2000/08/04 05:09:49 jonas
  592. * forgot to commit :( (part of regvar changes)
  593. Revision 1.2 2000/07/13 11:32:52 michael
  594. + removed logs
  595. }