tgeni386.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. {
  2. $Id$
  3. Copyright (C) 1993-98 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,files,aasm
  22. {$ifdef i386}
  23. ,i386
  24. {$endif}
  25. ;
  26. type
  27. tregisterset = set of tregister;
  28. tpushed = array[R_EAX..R_MM6] of boolean;
  29. const
  30. usablereg32 : byte = 4;
  31. {$ifdef SUPPORT_MMX}
  32. usableregmmx : byte = 8;
  33. {$endif SUPPORT_MMX}
  34. function getregister32 : tregister;
  35. procedure ungetregister32(r : tregister);
  36. {$ifdef SUPPORT_MMX}
  37. function getregistermmx : tregister;
  38. procedure ungetregistermmx(r : tregister);
  39. {$endif SUPPORT_MMX}
  40. procedure ungetregister(r : tregister);
  41. procedure cleartempgen;
  42. { generates temporary variables }
  43. procedure resettempgen;
  44. procedure setfirsttemp(l : longint);
  45. function gettempsize : longint;
  46. function gettempofsize(size : longint) : longint;
  47. procedure ungettemp(pos : longint;size : longint);
  48. procedure gettempofsizereference(l : longint;var ref : treference);
  49. function istemp(const ref : treference) : boolean;
  50. procedure ungetiftemp(const ref : treference);
  51. procedure del_reference(const ref : treference);
  52. procedure del_locref(const location : tlocation);
  53. { pushs and restores registers }
  54. procedure pushusedregisters(var pushed : tpushed;b : byte);
  55. procedure popusedregisters(const pushed : tpushed);
  56. var
  57. unused,usableregs : tregisterset;
  58. c_usableregs : longint;
  59. { uses only 1 byte while a set uses in FPC 32 bytes }
  60. usedinproc : byte;
  61. { count, how much a register must be pushed if it is used as register }
  62. { variable }
  63. {$ifdef SUPPORT_MMX}
  64. reg_pushes : array[R_EAX..R_MM6] of longint;
  65. is_reg_var : array[R_EAX..R_MM6] of boolean;
  66. {$else SUPPORT_MMX}
  67. reg_pushes : array[R_EAX..R_EDI] of longint;
  68. is_reg_var : array[R_EAX..R_EDI] of boolean;
  69. {$endif SUPPORT_MMX}
  70. implementation
  71. procedure pushusedregisters(var pushed : tpushed;b : byte);
  72. var
  73. r : tregister;
  74. hr : preference;
  75. begin
  76. usedinproc:=usedinproc or b;
  77. for r:=R_EAX to R_EBX do
  78. begin
  79. pushed[r]:=false;
  80. { if the register is used by the calling subroutine }
  81. if ((b and ($80 shr byte(r)))<>0) then
  82. begin
  83. { and is present in use }
  84. if not(r in unused) then
  85. begin
  86. { then save it }
  87. exprasmlist^.concat(new(pai386,op_reg(A_PUSH,S_L,r)));
  88. { here was a big problem !!!!!}
  89. { you cannot do that for a register that is
  90. globally assigned to a var
  91. this also means that you must push it much more
  92. often, but there must be a better way
  93. maybe by putting the value back to the stack !! }
  94. if not(is_reg_var[r]) then
  95. unused:=unused+[r];
  96. pushed[r]:=true;
  97. end;
  98. end;
  99. end;
  100. {$ifdef SUPPORT_MMX}
  101. for r:=R_MM0 to R_MM6 do
  102. begin
  103. pushed[r]:=false;
  104. { if the mmx register is in use, save it }
  105. if not(r in unused) then
  106. begin
  107. exprasmlist^.concat(new(pai386,op_const_reg(
  108. A_SUB,S_L,8,R_ESP)));
  109. new(hr);
  110. reset_reference(hr^);
  111. hr^.base:=R_ESP;
  112. exprasmlist^.concat(new(pai386,op_reg_ref(
  113. A_MOVQ,S_NO,r,hr)));
  114. if not(is_reg_var[r]) then
  115. unused:=unused+[r];
  116. pushed[r]:=true;
  117. end;
  118. end;
  119. {$endif SUPPORT_MMX}
  120. end;
  121. procedure popusedregisters(const pushed : tpushed);
  122. var
  123. r : tregister;
  124. hr : preference;
  125. begin
  126. { restore in reverse order: }
  127. {$ifdef SUPPORT_MMX}
  128. for r:=R_MM6 downto R_MM0 do
  129. begin
  130. if pushed[r] then
  131. begin
  132. new(hr);
  133. reset_reference(hr^);
  134. hr^.base:=R_ESP;
  135. exprasmlist^.concat(new(pai386,op_ref_reg(
  136. A_MOVQ,S_NO,hr,r)));
  137. exprasmlist^.concat(new(pai386,op_const_reg(
  138. A_ADD,S_L,8,R_ESP)));
  139. unused:=unused-[r];
  140. end;
  141. end;
  142. {$endif SUPPORT_MMX}
  143. for r:=R_EBX downto R_EAX do
  144. if pushed[r] then
  145. begin
  146. exprasmlist^.concat(new(pai386,op_reg(A_POP,S_L,r)));
  147. unused:=unused-[r];
  148. end;
  149. end;
  150. procedure ungetregister(r : tregister);
  151. begin
  152. if r in [R_EAX,R_ECX,R_EDX,R_EBX,R_ESP,R_EBP,R_ESI,R_EDI] then
  153. ungetregister32(r)
  154. else if r in [R_AX,R_CX,R_DX,R_BX,R_SP,R_BP,R_SI,R_DI] then
  155. ungetregister32(reg16toreg32(r))
  156. else if r in [R_AL,R_BL,R_CL,R_DL] then
  157. ungetregister32(reg8toreg32(r))
  158. {$ifdef SUPPORT_MMX}
  159. else if r in [R_MM0..R_MM6] then
  160. ungetregistermmx(r)
  161. {$endif SUPPORT_MMX}
  162. else internalerror(18);
  163. end;
  164. procedure ungetregister32(r : tregister);
  165. begin
  166. if cs_maxoptimieren in aktswitches then
  167. begin
  168. { takes much time }
  169. if not(r in usableregs) then
  170. exit;
  171. unused:=unused+[r];
  172. inc(usablereg32);
  173. end
  174. else
  175. begin
  176. if not(r in [R_EAX,R_EBX,R_ECX,R_EDX]) then
  177. exit;
  178. unused:=unused+[r];
  179. inc(usablereg32);
  180. end;
  181. {$ifdef REGALLOC}
  182. exprasmlist^.concat(new(pairegdealloc,init(r)));
  183. {$endif REGALLOC}
  184. end;
  185. {$ifdef SUPPORT_MMX}
  186. function getregistermmx : tregister;
  187. var
  188. r : tregister;
  189. begin
  190. dec(usableregmmx);
  191. for r:=R_MM0 to R_MM6 do
  192. if r in unused then
  193. begin
  194. unused:=unused-[r];
  195. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  196. getregistermmx:=r;
  197. exit;
  198. end;
  199. internalerror(10);
  200. end;
  201. procedure ungetregistermmx(r : tregister);
  202. begin
  203. if cs_maxoptimieren in aktswitches then
  204. begin
  205. { takes much time }
  206. if not(r in usableregs) then
  207. exit;
  208. unused:=unused+[r];
  209. inc(usableregmmx);
  210. end
  211. else
  212. begin
  213. unused:=unused+[r];
  214. inc(usableregmmx);
  215. end;
  216. end;
  217. {$endif SUPPORT_MMX}
  218. procedure del_reference(const ref : treference);
  219. begin
  220. if ref.isintvalue then
  221. exit;
  222. ungetregister32(ref.base);
  223. ungetregister32(ref.index);
  224. { ref.segment:=R_DEFAULT_SEG; }
  225. end;
  226. procedure del_locref(const location : tlocation);
  227. begin
  228. if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  229. exit;
  230. if location.reference.isintvalue then
  231. exit;
  232. ungetregister32(location.reference.base);
  233. ungetregister32(location.reference.index);
  234. { ref.segment:=R_DEFAULT_SEG; }
  235. end;
  236. function getregister32 : tregister;
  237. begin
  238. dec(usablereg32);
  239. if R_EAX in unused then
  240. begin
  241. unused:=unused-[R_EAX];
  242. usedinproc:=usedinproc or ($80 shr byte(R_EAX));
  243. getregister32:=R_EAX;
  244. end
  245. else if R_EDX in unused then
  246. begin
  247. unused:=unused-[R_EDX];
  248. usedinproc:=usedinproc or ($80 shr byte(R_EDX));
  249. getregister32:=R_EDX;
  250. end
  251. else if R_EBX in unused then
  252. begin
  253. unused:=unused-[R_EBX];
  254. usedinproc:=usedinproc or ($80 shr byte(R_EBX));
  255. getregister32:=R_EBX;
  256. end
  257. else if R_ECX in unused then
  258. begin
  259. unused:=unused-[R_ECX];
  260. usedinproc:=usedinproc or ($80 shr byte(R_ECX));
  261. getregister32:=R_ECX;
  262. end
  263. else internalerror(10);
  264. end;
  265. procedure cleartempgen;
  266. begin
  267. unused:=usableregs;
  268. usablereg32:=c_usableregs;
  269. end;
  270. type
  271. pfreerecord = ^tfreerecord;
  272. tfreerecord = record
  273. next : pfreerecord;
  274. pos : longint;
  275. size : longint;
  276. {$ifdef EXTDEBUG}
  277. line : longint;
  278. {$endif}
  279. end;
  280. var
  281. tmpfreelist : pfreerecord;
  282. templist : pfreerecord;
  283. lastoccupied : longint;
  284. firsttemp, maxtemp : longint;
  285. procedure resettempgen;
  286. var
  287. hp : pfreerecord;
  288. begin
  289. while assigned(tmpfreelist) do
  290. begin
  291. hp:=tmpfreelist;
  292. tmpfreelist:=hp^.next;
  293. dispose(hp);
  294. end;
  295. while assigned(templist) do
  296. begin
  297. {$ifdef EXTDEBUG}
  298. Comment(V_Warning,'temporary assignment of size '
  299. +tostr(templist^.size)+' from '+tostr(templist^.line)+
  300. +' at pos '+tostr(templist^.pos)+
  301. ' not freed at the end of the procedure');
  302. {$endif}
  303. hp:=templist;
  304. templist:=hp^.next;
  305. {$ifndef EXTDEBUG}
  306. dispose(hp);
  307. {$endif not EXTDEBUG}
  308. end;
  309. templist:=nil;
  310. tmpfreelist:=nil;
  311. firsttemp:=0;
  312. maxtemp:=0;
  313. lastoccupied:=0;
  314. end;
  315. procedure setfirsttemp(l : longint);
  316. begin
  317. { generates problems
  318. if (l mod 4 <> 0) then dec(l,l mod 4);}
  319. firsttemp:=l;
  320. maxtemp := l;
  321. lastoccupied:=l;
  322. end;
  323. function gettempofsize(size : longint) : longint;
  324. var
  325. last,hp : pfreerecord;
  326. begin
  327. { this code comes from the heap management of FPC ... }
  328. if (size mod 4)<>0 then
  329. size:=size+(4-(size mod 4));
  330. if assigned(tmpfreelist) then
  331. begin
  332. last:=nil;
  333. hp:=tmpfreelist;
  334. while assigned(hp) do
  335. begin
  336. { first fit }
  337. if hp^.size>=size then
  338. begin
  339. gettempofsize:=hp^.pos;
  340. if hp^.pos-size < maxtemp then
  341. maxtemp := hp^.size-size;
  342. { the whole block is needed ? }
  343. if hp^.size>size then
  344. begin
  345. hp^.size:=hp^.size-size;
  346. hp^.pos:=hp^.pos-size;
  347. end
  348. else
  349. begin
  350. if assigned(last) then
  351. last^.next:=hp^.next
  352. else
  353. tmpfreelist:=nil;
  354. dispose(hp);
  355. end;
  356. exit;
  357. end;
  358. last:=hp;
  359. hp:=hp^.next;
  360. end;
  361. end;
  362. { nothing free is big enough : expand temp }
  363. gettempofsize:=lastoccupied-size;
  364. lastoccupied:=lastoccupied-size;
  365. if lastoccupied < maxtemp then
  366. maxtemp := lastoccupied;
  367. end;
  368. function gettempsize : longint;
  369. begin
  370. { align local data to dwords }
  371. if (maxtemp mod 4)<>0 then
  372. dec(maxtemp,4+(maxtemp mod 4));
  373. gettempsize:=-maxtemp;
  374. end;
  375. procedure gettempofsizereference(l : longint;var ref : treference);
  376. var
  377. tl : pfreerecord;
  378. begin
  379. { do a reset, because the reference isn't used }
  380. reset_reference(ref);
  381. ref.offset:=gettempofsize(l);
  382. ref.base:=procinfo.framepointer;
  383. new(tl);
  384. tl^.pos:=ref.offset;
  385. tl^.size:=l;
  386. tl^.next:=templist;
  387. templist:=tl;
  388. {$ifdef EXTDEBUG}
  389. tl^.line:=current_module^.current_inputfile^.line_no;
  390. {$endif}
  391. end;
  392. function istemp(const ref : treference) : boolean;
  393. begin
  394. istemp:=((ref.base=procinfo.framepointer) and
  395. (ref.offset<firsttemp));
  396. end;
  397. procedure ungettemp(pos : longint;size : longint);
  398. var
  399. hp,newhp : pfreerecord;
  400. begin
  401. if (size mod 4)<>0 then
  402. size:=size+(4-(size mod 4));
  403. if size = 0 then
  404. exit;
  405. if pos<=lastoccupied then
  406. if pos=lastoccupied then
  407. begin
  408. lastoccupied:=pos+size;
  409. hp:=tmpfreelist;
  410. newhp:=nil;
  411. while assigned(hp) do
  412. begin
  413. { conneting a free block }
  414. if hp^.pos=lastoccupied then
  415. begin
  416. if assigned(newhp) then newhp^.next:=nil
  417. else tmpfreelist:=nil;
  418. lastoccupied:=lastoccupied+hp^.size;
  419. dispose(hp);
  420. break;
  421. end;
  422. newhp:=hp;
  423. hp:=hp^.next;
  424. end;
  425. end
  426. else
  427. begin
  428. {$ifdef EXTDEBUG}
  429. Comment(V_Warning,'temp managment problem : ungettemp() pos < lastoccupied !');
  430. {$endif}
  431. end
  432. else
  433. begin
  434. new(newhp);
  435. { size can be allways set }
  436. newhp^.size:=size;
  437. newhp^.pos := pos;
  438. { if there is no free list }
  439. if not assigned(tmpfreelist) then
  440. begin
  441. { then generate one }
  442. tmpfreelist:=newhp;
  443. newhp^.next:=nil;
  444. exit;
  445. end;
  446. { search the position to insert }
  447. hp:=tmpfreelist;
  448. while assigned(hp) do
  449. begin
  450. { conneting two blocks ? }
  451. if hp^.pos+hp^.size=pos then
  452. begin
  453. inc(hp^.size,size);
  454. dispose(newhp);
  455. break;
  456. end
  457. { if the end is reached, then concat }
  458. else if hp^.next=nil then
  459. begin
  460. hp^.next:=newhp;
  461. newhp^.next:=nil;
  462. break;
  463. end
  464. { falls der n„chste Zeiger gr”áer ist, dann }
  465. { Einh„ngen }
  466. else if hp^.next^.pos<=pos+size then
  467. begin
  468. { concat two blocks ? }
  469. if pos+size=hp^.next^.pos then
  470. begin
  471. newhp^.next:=hp^.next^.next;
  472. inc(newhp^.size,hp^.next^.size);
  473. dispose(hp^.next);
  474. hp^.next:=newhp;
  475. end
  476. else
  477. begin
  478. newhp^.next:=hp^.next;
  479. hp^.next:=newhp;
  480. end;
  481. break;
  482. end;
  483. hp:=hp^.next;
  484. end;
  485. end;
  486. end;
  487. procedure ungetiftemp(const ref : treference);
  488. var
  489. tl,prev : pfreerecord;
  490. begin
  491. if istemp(ref) then
  492. begin
  493. prev:=nil;
  494. tl:=templist;
  495. while assigned(tl) do
  496. begin
  497. if ref.offset=tl^.pos then
  498. begin
  499. ungettemp(ref.offset,tl^.size);
  500. if assigned(prev) then
  501. prev^.next:=tl^.next
  502. else
  503. templist:=tl^.next;
  504. dispose(tl);
  505. exit;
  506. end
  507. else
  508. begin
  509. prev:=tl;
  510. tl:=tl^.next;
  511. end;
  512. end;
  513. {$ifdef EXTDEBUG}
  514. Comment(V_Warning,'Internal: temp managment problem : '+
  515. 'temp not found for release at offset '+tostr(ref.offset));
  516. {$endIf}
  517. end;
  518. end;
  519. begin
  520. usableregs:=[R_EAX,R_EBX,R_ECX,R_EDX];
  521. {$ifdef SUPPORT_MMX}
  522. usableregs:=usableregs+[R_MM0..R_MM6];
  523. {$endif SUPPORT_MMX}
  524. c_usableregs:=4;
  525. tmpfreelist:=nil;
  526. templist:=nil;
  527. end.
  528. {
  529. $Log$
  530. Revision 1.4 1998-04-29 10:34:08 pierre
  531. + added some code for ansistring (not complete nor working yet)
  532. * corrected operator overloading
  533. * corrected nasm output
  534. + started inline procedures
  535. + added starstarn : use ** for exponentiation (^ gave problems)
  536. + started UseTokenInfo cond to get accurate positions
  537. Revision 1.3 1998/04/09 22:16:36 florian
  538. * problem with previous REGALLOC solved
  539. * improved property support
  540. Revision 1.2 1998/04/09 15:46:39 florian
  541. + register allocation tracing stuff added
  542. Revision 1.1.1.1 1998/03/25 11:18:15 root
  543. * Restored version
  544. Revision 1.9 2036/02/07 09:26:57 florian
  545. * more fixes to get -Ox work
  546. Revision 1.8 1998/03/10 01:17:30 peter
  547. * all files have the same header
  548. * messages are fully implemented, EXTDEBUG uses Comment()
  549. + AG... files for the Assembler generation
  550. Revision 1.7 1998/03/02 01:49:36 peter
  551. * renamed target_DOS to target_GO32V1
  552. + new verbose system, merged old errors and verbose units into one new
  553. verbose.pas, so errors.pas is obsolete
  554. Revision 1.6 1998/02/13 10:35:52 daniel
  555. * Made Motorola version compilable.
  556. * Fixed optimizer
  557. Revision 1.5 1998/02/12 17:19:32 florian
  558. * fixed to get remake3 work, but needs additional fixes (output, I don't like
  559. also that aktswitches isn't a pointer)
  560. Revision 1.4 1998/02/12 11:50:50 daniel
  561. Yes! Finally! After three retries, my patch!
  562. Changes:
  563. Complete rewrite of psub.pas.
  564. Added support for DLL's.
  565. Compiler requires less memory.
  566. Platform units for each platform.
  567. Revision 1.3 1998/02/04 22:02:46 florian
  568. + complete handling of MMX registers
  569. Revision 1.2 1998/01/07 00:13:44 michael
  570. Restored released version (plus fixes) as current
  571. Revision 1.1.1.1 1997/11/27 08:33:03 michael
  572. FPC Compiler CVS start
  573. Pre-CVS log:
  574. FK Florian Klaempfl
  575. PM Pierre Muller
  576. + feature added
  577. - removed
  578. * bug fixed or changed
  579. History (started with version 0.9.0):
  580. 7th december 1996:
  581. * some code from Pierre Muller inserted
  582. makes the use of the stack more efficient
  583. 20th november 1997:
  584. * tempsize is multiple of 4 for alignment (PM), buggy commented (PM)
  585. }