tgeni386.pas 20 KB

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