tgeni386.pas 20 KB

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