tgeni386.pas 20 KB

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