pass_2.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit handles the codegeneration pass
  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. {$ifdef FPC}
  19. {$goto on}
  20. {$endif FPC}
  21. {$ifdef TP}
  22. {$E+,F+,N+}
  23. {$endif}
  24. unit pass_2;
  25. interface
  26. uses
  27. tree;
  28. { produces assembler for the expression in variable p }
  29. { and produces an assembler node at the end }
  30. procedure generatecode(var p : ptree);
  31. { produces the actual code }
  32. function do_secondpass(var p : ptree) : boolean;
  33. procedure secondpass(var p : ptree);
  34. implementation
  35. uses
  36. globtype,systems,
  37. cobjects,comphook,verbose,globals,files,
  38. symconst,symtable,types,aasm,scanner,
  39. pass_1,hcodegen,temp_gen,cpubase,cpuasm
  40. {$ifndef newcg}
  41. ,tcflw
  42. {$endif newcg}
  43. {$ifdef GDB}
  44. ,gdb
  45. {$endif}
  46. {$ifdef i386}
  47. ,tgeni386,cgai386
  48. ,cg386con,cg386mat,cg386cnv,cg386set,cg386add
  49. ,cg386mem,cg386cal,cg386ld,cg386flw,cg386inl
  50. {$endif}
  51. {$ifdef m68k}
  52. ,tgen68k,cga68k
  53. ,cg68kcon,cg68kmat,cg68kcnv,cg68kset,cg68kadd
  54. ,cg68kmem,cg68kcal,cg68kld,cg68kflw,cg68kinl
  55. {$endif}
  56. ;
  57. {*****************************************************************************
  58. SecondPass
  59. *****************************************************************************}
  60. type
  61. secondpassproc = procedure(var p : ptree);
  62. procedure secondnothing(var p : ptree);
  63. begin
  64. end;
  65. procedure seconderror(var p : ptree);
  66. begin
  67. p^.error:=true;
  68. codegenerror:=true;
  69. end;
  70. procedure secondstatement(var p : ptree);
  71. var
  72. hp : ptree;
  73. begin
  74. hp:=p;
  75. while assigned(hp) do
  76. begin
  77. if assigned(hp^.right) then
  78. begin
  79. cleartempgen;
  80. {!!!!!!
  81. oldrl:=temptoremove;
  82. temptoremove:=new(plinkedlist,init);
  83. }
  84. secondpass(hp^.right);
  85. { !!!!!!!
  86. some temporary data which can't be released elsewhere
  87. removetemps(exprasmlist,temptoremove);
  88. dispose(temptoremove,done);
  89. temptoremove:=oldrl;
  90. }
  91. end;
  92. hp:=hp^.left;
  93. end;
  94. end;
  95. procedure secondblockn(var p : ptree);
  96. begin
  97. { do second pass on left node }
  98. if assigned(p^.left) then
  99. secondpass(p^.left);
  100. end;
  101. procedure secondasm(var p : ptree);
  102. procedure ReLabel(var p:pasmsymbol);
  103. begin
  104. if p^.proclocal then
  105. begin
  106. if not assigned(p^.altsymbol) then
  107. p^.GenerateAltSymbol;
  108. p:=p^.altsymbol;
  109. end;
  110. end;
  111. var
  112. hp,hp2 : pai;
  113. localfixup,parafixup,
  114. i : longint;
  115. r : preference;
  116. skipnode : boolean;
  117. begin
  118. if inlining_procedure then
  119. begin
  120. localfixup:=aktprocsym^.definition^.localst^.address_fixup;
  121. parafixup:=aktprocsym^.definition^.parast^.address_fixup;
  122. ResetAsmSymbolListAltSymbol;
  123. hp:=pai(p^.p_asm^.first);
  124. while assigned(hp) do
  125. begin
  126. hp2:=pai(hp^.getcopy);
  127. skipnode:=false;
  128. case hp2^.typ of
  129. ait_label :
  130. begin
  131. { regenerate the labels by setting altsymbol }
  132. ReLabel(pasmsymbol(pai_label(hp2)^.l));
  133. end;
  134. ait_const_rva,
  135. ait_const_symbol :
  136. begin
  137. ReLabel(pai_const_symbol(hp2)^.sym);
  138. end;
  139. ait_instruction :
  140. begin
  141. {$ifdef i386}
  142. { fixup the references }
  143. for i:=1 to paicpu(hp2)^.ops do
  144. case paicpu(hp2)^.oper[i-1].typ of
  145. top_ref :
  146. begin
  147. r:=paicpu(hp2)^.oper[i-1].ref;
  148. case r^.options of
  149. ref_parafixup :
  150. r^.offsetfixup:=parafixup;
  151. ref_localfixup :
  152. r^.offsetfixup:=localfixup;
  153. end;
  154. if assigned(r^.symbol) then
  155. ReLabel(r^.symbol);
  156. end;
  157. top_symbol :
  158. begin
  159. ReLabel(paicpu(hp2)^.oper[i-1].sym);
  160. end;
  161. end;
  162. {$endif i386}
  163. end;
  164. ait_marker :
  165. begin
  166. { it's not an assembler block anymore }
  167. if (pai_marker(hp2)^.kind in [AsmBlockStart, AsmBlockEnd]) then
  168. skipnode:=true;
  169. end;
  170. else
  171. end;
  172. if not skipnode then
  173. exprasmlist^.concat(hp2)
  174. else
  175. dispose(hp2,done);
  176. hp:=pai(hp^.next);
  177. end
  178. end
  179. else
  180. begin
  181. { if the routine is an inline routine, then we must hold a copy
  182. becuase it can be necessary for inlining later }
  183. if (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  184. exprasmlist^.concatlistcopy(p^.p_asm)
  185. else
  186. exprasmlist^.concatlist(p^.p_asm);
  187. end;
  188. if not p^.object_preserved then
  189. begin
  190. {$ifdef i386}
  191. maybe_loadesi;
  192. {$endif}
  193. {$ifdef m68k}
  194. maybe_loada5;
  195. {$endif}
  196. end;
  197. end;
  198. {$ifdef logsecondpass}
  199. procedure logsecond(const s: string; entry: boolean);
  200. var p: pchar;
  201. begin
  202. if entry then
  203. p := strpnew(s+' (entry)')
  204. else p := strpnew(s+' (exit)');
  205. exprasmlist^.concat(new(pai_asm_comment,init(p)));
  206. end;
  207. {$endif logsecondpass}
  208. procedure secondpass(var p : ptree);
  209. const
  210. procedures : array[ttreetyp] of secondpassproc =
  211. (secondadd, {addn}
  212. secondadd, {muln}
  213. secondadd, {subn}
  214. secondmoddiv, {divn}
  215. secondadd, {symdifn}
  216. secondmoddiv, {modn}
  217. secondassignment, {assignn}
  218. secondload, {loadn}
  219. secondnothing, {range}
  220. secondadd, {ltn}
  221. secondadd, {lten}
  222. secondadd, {gtn}
  223. secondadd, {gten}
  224. secondadd, {equaln}
  225. secondadd, {unequaln}
  226. secondin, {inn}
  227. secondadd, {orn}
  228. secondadd, {xorn}
  229. secondshlshr, {shrn}
  230. secondshlshr, {shln}
  231. secondadd, {slashn}
  232. secondadd, {andn}
  233. secondsubscriptn, {subscriptn}
  234. secondderef, {derefn}
  235. secondaddr, {addrn}
  236. seconddoubleaddr, {doubleaddrn}
  237. secondordconst, {ordconstn}
  238. secondtypeconv, {typeconvn}
  239. secondcalln, {calln}
  240. secondnothing, {callparan}
  241. secondrealconst, {realconstn}
  242. secondfixconst, {fixconstn}
  243. secondunaryminus, {unaryminusn}
  244. secondasm, {asmn}
  245. secondvecn, {vecn}
  246. secondpointerconst, {pointerconstn}
  247. secondstringconst, {stringconstn}
  248. secondfuncret, {funcretn}
  249. secondselfn, {selfn}
  250. secondnot, {notn}
  251. secondinline, {inlinen}
  252. secondniln, {niln}
  253. seconderror, {errorn}
  254. secondnothing, {typen}
  255. secondhnewn, {hnewn}
  256. secondhdisposen, {hdisposen}
  257. secondnewn, {newn}
  258. secondsimplenewdispose, {simpledisposen}
  259. secondsetelement, {setelementn}
  260. secondsetconst, {setconstn}
  261. secondblockn, {blockn}
  262. secondstatement, {statementn}
  263. secondnothing, {loopn}
  264. secondifn, {ifn}
  265. secondbreakn, {breakn}
  266. secondcontinuen, {continuen}
  267. second_while_repeatn, {repeatn}
  268. second_while_repeatn, {whilen}
  269. secondfor, {forn}
  270. secondexitn, {exitn}
  271. secondwith, {withn}
  272. secondcase, {casen}
  273. secondlabel, {labeln}
  274. secondgoto, {goton}
  275. secondsimplenewdispose, {simplenewn}
  276. secondtryexcept, {tryexceptn}
  277. secondraise, {raisen}
  278. secondnothing, {switchesn}
  279. secondtryfinally, {tryfinallyn}
  280. secondon, {onn}
  281. secondis, {isn}
  282. secondas, {asn}
  283. seconderror, {caretn}
  284. secondfail, {failn}
  285. secondadd, {starstarn}
  286. secondprocinline, {procinlinen}
  287. secondarrayconstruct, {arrayconstructn}
  288. secondnothing, {arrayconstructrangen}
  289. secondnothing, {nothingn}
  290. secondloadvmt {loadvmtn}
  291. );
  292. {$ifdef logsecondpass}
  293. secondnames: array[ttreetyp] of string[13] =
  294. ('add-addn', {addn}
  295. 'add-muln)', {muln}
  296. 'add-subn', {subn}
  297. 'moddiv-divn', {divn}
  298. 'add-symdifn', {symdifn}
  299. 'moddiv-modn', {modn}
  300. 'assignment', {assignn}
  301. 'load', {loadn}
  302. 'nothing-range', {range}
  303. 'add-ltn', {ltn}
  304. 'add-lten', {lten}
  305. 'add-gtn', {gtn}
  306. 'add-gten', {gten}
  307. 'add-equaln', {equaln}
  308. 'add-unequaln', {unequaln}
  309. 'in', {inn}
  310. 'add-orn', {orn}
  311. 'add-xorn', {xorn}
  312. 'shlshr-shrn', {shrn}
  313. 'shlshr-shln', {shln}
  314. 'add-slashn', {slashn}
  315. 'add-andn', {andn}
  316. 'subscriptn', {subscriptn}
  317. 'dderef', {derefn}
  318. 'addr', {addrn}
  319. 'doubleaddr', {doubleaddrn}
  320. 'ordconst', {ordconstn}
  321. 'typeconv', {typeconvn}
  322. 'calln', {calln}
  323. 'nothing-callp', {callparan}
  324. 'realconst', {realconstn}
  325. 'fixconst', {fixconstn}
  326. 'unaryminus', {unaryminusn}
  327. 'asm', {asmn}
  328. 'vecn', {vecn}
  329. 'pointerconst', {pointerconstn}
  330. 'stringconst', {stringconstn}
  331. 'funcret', {funcretn}
  332. 'selfn', {selfn}
  333. 'not', {notn}
  334. 'inline', {inlinen}
  335. 'niln', {niln}
  336. 'error', {errorn}
  337. 'nothing-typen', {typen}
  338. 'hnewn', {hnewn}
  339. 'hdisposen', {hdisposen}
  340. 'newn', {newn}
  341. 'simplenewDISP', {simpledisposen}
  342. 'setelement', {setelementn}
  343. 'setconst', {setconstn}
  344. 'blockn', {blockn}
  345. 'statement', {statementn}
  346. 'nothing-loopn', {loopn}
  347. 'ifn', {ifn}
  348. 'breakn', {breakn}
  349. 'continuen', {continuen}
  350. '_while_REPEAT', {repeatn}
  351. '_WHILE_repeat', {whilen}
  352. 'for', {forn}
  353. 'exitn', {exitn}
  354. 'with', {withn}
  355. 'case', {casen}
  356. 'label', {labeln}
  357. 'goto', {goton}
  358. 'simpleNEWdisp', {simplenewn}
  359. 'tryexcept', {tryexceptn}
  360. 'raise', {raisen}
  361. 'nothing-swtch', {switchesn}
  362. 'tryfinally', {tryfinallyn}
  363. 'on', {onn}
  364. 'is', {isn}
  365. 'as', {asn}
  366. 'error-caret', {caretn}
  367. 'fail', {failn}
  368. 'add-startstar', {starstarn}
  369. 'procinline', {procinlinen}
  370. 'arrayconstruc', {arrayconstructn}
  371. 'noth-arrcnstr', {arrayconstructrangen}
  372. 'nothing-nothg', {nothingn}
  373. 'loadvmt' {loadvmtn}
  374. );
  375. {$endif logsecondpass}
  376. var
  377. oldcodegenerror : boolean;
  378. oldlocalswitches : tlocalswitches;
  379. oldpos : tfileposinfo;
  380. {$ifdef TEMPREGDEBUG}
  381. prevp : pptree;
  382. {$endif TEMPREGDEBUG}
  383. begin
  384. if not(p^.error) then
  385. begin
  386. oldcodegenerror:=codegenerror;
  387. oldlocalswitches:=aktlocalswitches;
  388. oldpos:=aktfilepos;
  389. {$ifdef TEMPREGDEBUG}
  390. testregisters32;
  391. prevp:=curptree;
  392. curptree:=@p;
  393. p^.usableregs:=usablereg32;
  394. {$endif TEMPREGDEBUG}
  395. aktfilepos:=p^.fileinfo;
  396. aktlocalswitches:=p^.localswitches;
  397. codegenerror:=false;
  398. {$ifdef logsecondpass}
  399. logsecond('second'+secondnames[p^.treetype],true);
  400. {$endif logsecondpass}
  401. procedures[p^.treetype](p);
  402. {$ifdef logsecondpass}
  403. logsecond('second'+secondnames[p^.treetype],false);
  404. {$endif logsecondpass}
  405. p^.error:=codegenerror;
  406. codegenerror:=codegenerror or oldcodegenerror;
  407. aktlocalswitches:=oldlocalswitches;
  408. aktfilepos:=oldpos;
  409. {$ifdef TEMPREGDEBUG}
  410. curptree:=prevp;
  411. {$endif TEMPREGDEBUG}
  412. {$ifdef EXTTEMPREGDEBUG}
  413. if p^.usableregs-usablereg32>p^.reallyusedregs then
  414. p^.reallyusedregs:=p^.usableregs-usablereg32;
  415. if p^.reallyusedregs<p^.registers32 then
  416. Comment(V_Debug,'registers32 overestimated '+tostr(p^.registers32)+
  417. '>'+tostr(p^.reallyusedregs));
  418. {$endif EXTTEMPREGDEBUG}
  419. end
  420. else
  421. codegenerror:=true;
  422. end;
  423. function do_secondpass(var p : ptree) : boolean;
  424. begin
  425. codegenerror:=false;
  426. if not(p^.error) then
  427. secondpass(p);
  428. do_secondpass:=codegenerror;
  429. end;
  430. var
  431. { the array ranges are overestimated !!! }
  432. { max(maxvarregs,maxfpuvarregs) would be }
  433. { enough }
  434. regvars : array[1..maxvarregs+maxfpuvarregs] of pvarsym;
  435. regvars_para : array[1..maxvarregs+maxfpuvarregs] of boolean;
  436. regvars_refs : array[1..maxvarregs+maxfpuvarregs] of longint;
  437. parasym : boolean;
  438. procedure searchregvars(p : pnamedindexobject);
  439. var
  440. i,j,k : longint;
  441. begin
  442. if (psym(p)^.typ=varsym) and (vo_regable in pvarsym(p)^.varoptions) then
  443. begin
  444. j:=pvarsym(p)^.refs;
  445. { parameter get a less value }
  446. if parasym then
  447. begin
  448. if cs_littlesize in aktglobalswitches then
  449. dec(j,1)
  450. else
  451. dec(j,100);
  452. end;
  453. { walk through all momentary register variables }
  454. for i:=1 to maxvarregs do
  455. begin
  456. if ((regvars[i]=nil) or (j>regvars_refs[i])) and (j>0) then
  457. begin
  458. for k:=maxvarregs-1 downto i do
  459. begin
  460. regvars[k+1]:=regvars[k];
  461. regvars_para[k+1]:=regvars_para[k];
  462. regvars_refs[k+1]:=regvars_refs[k];
  463. end;
  464. { calc the new refs
  465. pvarsym(p)^.refs:=j; }
  466. regvars[i]:=pvarsym(p);
  467. regvars_para[i]:=parasym;
  468. regvars_refs[i]:=j;
  469. break;
  470. end;
  471. end;
  472. end;
  473. end;
  474. procedure searchfpuregvars(p : pnamedindexobject);
  475. var
  476. i,j,k : longint;
  477. begin
  478. if (psym(p)^.typ=varsym) and (vo_fpuregable in pvarsym(p)^.varoptions) then
  479. begin
  480. j:=pvarsym(p)^.refs;
  481. { parameter get a less value }
  482. if parasym then
  483. begin
  484. if cs_littlesize in aktglobalswitches then
  485. dec(j,1)
  486. else
  487. dec(j,100);
  488. end;
  489. { walk through all momentary register variables }
  490. for i:=1 to maxfpuvarregs do
  491. begin
  492. if ((regvars[i]=nil) or (j>regvars_refs[i])) and (j>0) then
  493. begin
  494. for k:=maxfpuvarregs-1 downto i do
  495. begin
  496. regvars[k+1]:=regvars[k];
  497. regvars_para[k+1]:=regvars_para[k];
  498. regvars_refs[k+1]:=regvars_refs[k];
  499. end;
  500. { calc the new refs
  501. pvarsym(p)^.refs:=j; }
  502. regvars[i]:=pvarsym(p);
  503. regvars_para[i]:=parasym;
  504. regvars_refs[i]:=j;
  505. break;
  506. end;
  507. end;
  508. end;
  509. end;
  510. procedure clearrefs(p : pnamedindexobject);
  511. begin
  512. if (psym(p)^.typ=varsym) then
  513. pvarsym(p)^.refs:=0;
  514. end;
  515. procedure generatecode(var p : ptree);
  516. var
  517. i : longint;
  518. regsize : topsize;
  519. hr : preference;
  520. label
  521. nextreg;
  522. begin
  523. cleartempgen;
  524. flowcontrol:=[];
  525. { when size optimization only count occurrence }
  526. if cs_littlesize in aktglobalswitches then
  527. t_times:=1
  528. else
  529. { reference for repetition is 100 }
  530. t_times:=100;
  531. { clear register count }
  532. clearregistercount;
  533. use_esp_stackframe:=false;
  534. aktexceptblock:=nil;
  535. symtablestack^.foreach(@clearrefs);
  536. symtablestack^.next^.foreach(@clearrefs);
  537. if not(do_firstpass(p)) then
  538. begin
  539. { max. optimizations }
  540. { only if no asm is used }
  541. { and no try statement }
  542. if (cs_regalloc in aktglobalswitches) and
  543. ((procinfo^.flags and (pi_uses_asm or pi_uses_exceptions))=0) then
  544. begin
  545. { can we omit the stack frame ? }
  546. { conditions:
  547. 1. procedure (not main block)
  548. 2. no constructor or destructor
  549. 3. no call to other procedures
  550. 4. no interrupt handler
  551. }
  552. {!!!!!! this doesn work yet, because of problems with
  553. with linux and windows
  554. }
  555. (*
  556. if assigned(aktprocsym) then
  557. begin
  558. if not(assigned(procinfo^._class)) and
  559. not(aktprocsym^.definition^.proctypeoption in [potype_constructor,potype_destructor]) and
  560. not(po_interrupt in aktprocsym^.definition^.procoptions) and
  561. ((procinfo^.flags and pi_do_call)=0) and
  562. (lexlevel>=normal_function_level) then
  563. begin
  564. { use ESP as frame pointer }
  565. procinfo^.framepointer:=stack_pointer;
  566. use_esp_stackframe:=true;
  567. { calc parameter distance new }
  568. dec(procinfo^.framepointer_offset,4);
  569. dec(procinfo^.selfpointer_offset,4);
  570. { is this correct ???}
  571. { retoffset can be negativ for results in eax !! }
  572. { the value should be decreased only if positive }
  573. if procinfo^.retoffset>=0 then
  574. dec(procinfo^.retoffset,4);
  575. dec(procinfo^.para_offset,4);
  576. aktprocsym^.definition^.parast^.address_fixup:=procinfo^.para_offset;
  577. end;
  578. end;
  579. *)
  580. if (p^.registers32<4) then
  581. begin
  582. for i:=1 to maxvarregs do
  583. regvars[i]:=nil;
  584. parasym:=false;
  585. symtablestack^.foreach({$ifndef TP}@{$endif}searchregvars);
  586. { copy parameter into a register ? }
  587. parasym:=true;
  588. symtablestack^.next^.foreach({$ifndef TP}@{$endif}searchregvars);
  589. { hold needed registers free }
  590. for i:=maxvarregs downto maxvarregs-p^.registers32+1 do
  591. regvars[i]:=nil;
  592. { now assign register }
  593. for i:=1 to maxvarregs-p^.registers32 do
  594. begin
  595. if assigned(regvars[i]) then
  596. begin
  597. { it is nonsens, to copy the variable to }
  598. { a register because we need then much }
  599. { too pushes ? }
  600. if reg_pushes[varregs[i]]>=regvars[i]^.refs then
  601. begin
  602. regvars[i]:=nil;
  603. goto nextreg;
  604. end;
  605. { register is no longer available for }
  606. { expressions }
  607. { search the register which is the most }
  608. { unused }
  609. usableregs:=usableregs-[varregs[i]];
  610. {$ifdef i386}
  611. procinfo^.aktentrycode^.concat(new(pairegalloc,alloc(varregs[i])));
  612. {$endif i386}
  613. is_reg_var[varregs[i]]:=true;
  614. dec(c_usableregs);
  615. { possibly no 32 bit register are needed }
  616. { call by reference/const ? }
  617. if (regvars[i]^.varspez=vs_var) or
  618. ((regvars[i]^.varspez=vs_const) and
  619. push_addr_param(regvars[i]^.vartype.def)) then
  620. begin
  621. regvars[i]^.reg:=varregs[i];
  622. regsize:=S_L;
  623. end
  624. else
  625. if (regvars[i]^.vartype.def^.deftype=orddef) and
  626. (porddef(regvars[i]^.vartype.def)^.size=1) then
  627. begin
  628. {$ifdef i386}
  629. regvars[i]^.reg:=reg32toreg8(varregs[i]);
  630. {$endif}
  631. regsize:=S_B;
  632. end
  633. else
  634. if (regvars[i]^.vartype.def^.deftype=orddef) and
  635. (porddef(regvars[i]^.vartype.def)^.size=2) then
  636. begin
  637. {$ifdef i386}
  638. regvars[i]^.reg:=reg32toreg16(varregs[i]);
  639. {$endif}
  640. regsize:=S_W;
  641. end
  642. else
  643. begin
  644. regvars[i]^.reg:=varregs[i];
  645. regsize:=S_L;
  646. end;
  647. { parameter must be load }
  648. if regvars_para[i] then
  649. begin
  650. { procinfo is there actual, }
  651. { because we can't never be in a }
  652. { nested procedure }
  653. { when loading parameter to reg }
  654. new(hr);
  655. reset_reference(hr^);
  656. hr^.offset:=pvarsym(regvars[i])^.address+procinfo^.para_offset;
  657. hr^.base:=procinfo^.framepointer;
  658. {$ifdef i386}
  659. procinfo^.aktentrycode^.concat(new(paicpu,op_ref_reg(A_MOV,regsize,
  660. hr,regvars[i]^.reg)));
  661. {$endif i386}
  662. {$ifdef m68k}
  663. procinfo^.aktentrycode^.concat(new(paicpu,op_ref_reg(A_MOVE,regsize,
  664. hr,regvars[i]^.reg)));
  665. {$endif m68k}
  666. unused:=unused - [regvars[i]^.reg];
  667. end;
  668. { procedure uses this register }
  669. {$ifdef i386}
  670. usedinproc:=usedinproc or ($80 shr byte(varregs[i]));
  671. {$endif i386}
  672. {$ifdef m68k}
  673. usedinproc:=usedinproc or ($800 shr word(varregs[i]));
  674. {$endif m68k}
  675. end;
  676. nextreg:
  677. { dummy }
  678. regsize:=S_W;
  679. end;
  680. for i:=1 to maxvarregs do
  681. begin
  682. if assigned(regvars[i]) then
  683. begin
  684. if cs_asm_source in aktglobalswitches then
  685. procinfo^.aktentrycode^.insert(new(pai_asm_comment,init(strpnew(regvars[i]^.name+
  686. ' with weight '+tostr(regvars[i]^.refs)+' assigned to register '+
  687. reg2str(regvars[i]^.reg)))));
  688. if (status.verbosity and v_debug)=v_debug then
  689. Message3(cg_d_register_weight,reg2str(regvars[i]^.reg),
  690. tostr(regvars[i]^.refs),regvars[i]^.name);
  691. end;
  692. end;
  693. end;
  694. if ((p^.registersfpu+1)<maxfpuvarregs) then
  695. begin
  696. for i:=1 to maxfpuvarregs do
  697. regvars[i]:=nil;
  698. parasym:=false;
  699. symtablestack^.foreach({$ifndef TP}@{$endif}searchfpuregvars);
  700. {$ifdef dummy}
  701. { copy parameter into a register ? }
  702. parasym:=true;
  703. symtablestack^.next^.foreach({$ifndef TP}@{$endif}searchregvars);
  704. {$endif dummy}
  705. { hold needed registers free }
  706. { in non leaf procedures we must be very careful }
  707. { with assigning registers }
  708. if aktmaxfpuregisters=-1 then
  709. begin
  710. if (procinfo^.flags and pi_do_call)<>0 then
  711. begin
  712. for i:=maxfpuvarregs downto 2 do
  713. regvars[i]:=nil;
  714. end
  715. else
  716. begin
  717. for i:=maxfpuvarregs downto maxfpuvarregs-p^.registersfpu do
  718. regvars[i]:=nil;
  719. end;
  720. end
  721. else
  722. begin
  723. for i:=aktmaxfpuregisters+1 to maxfpuvarregs do
  724. regvars[i]:=nil;
  725. end;
  726. { now assign register }
  727. for i:=1 to maxfpuvarregs do
  728. begin
  729. if assigned(regvars[i]) then
  730. begin
  731. {$ifdef i386}
  732. { reserve place on the FPU stack }
  733. regvars[i]^.reg:=correct_fpuregister(R_ST0,i-1);
  734. procinfo^.aktentrycode^.concat(new(paicpu,op_none(A_FLDZ,S_NO)));
  735. { ... and clean it up }
  736. procinfo^.aktexitcode^.concat(new(paicpu,op_reg(A_FSTP,S_NO,R_ST0)));
  737. {$endif i386}
  738. {$ifdef m68k}
  739. regvars[i]^.reg:=fpuvarregs[i];
  740. {$endif m68k}
  741. {$ifdef dummy}
  742. { parameter must be load }
  743. if regvars_para[i] then
  744. begin
  745. { procinfo is there actual, }
  746. { because we can't never be in a }
  747. { nested procedure }
  748. { when loading parameter to reg }
  749. new(hr);
  750. reset_reference(hr^);
  751. hr^.offset:=pvarsym(regvars[i])^.address+procinfo^.para_offset;
  752. hr^.base:=procinfo^.framepointer;
  753. {$ifdef i386}
  754. procinfo^.aktentrycode^.concat(new(paicpu,op_ref_reg(A_MOV,regsize,
  755. hr,regvars[i]^.reg)));
  756. {$endif i386}
  757. {$ifdef m68k}
  758. procinfo^.aktentrycode^.concat(new(paicpu,op_ref_reg(A_MOVE,regsize,
  759. hr,regvars[i]^.reg)));
  760. {$endif m68k}
  761. end;
  762. {$endif dummy}
  763. end;
  764. end;
  765. if cs_asm_source in aktglobalswitches then
  766. procinfo^.aktentrycode^.insert(new(pai_asm_comment,init(strpnew(tostr(p^.registersfpu)+
  767. ' registers on FPU stack used by temp. expressions'))));
  768. for i:=1 to maxfpuvarregs do
  769. begin
  770. if assigned(regvars[i]) then
  771. begin
  772. if cs_asm_source in aktglobalswitches then
  773. procinfo^.aktentrycode^.insert(new(pai_asm_comment,init(strpnew(regvars[i]^.name+
  774. ' with weight '+tostr(regvars[i]^.refs)+' assigned to register '+
  775. reg2str(regvars[i]^.reg)))));
  776. if (status.verbosity and v_debug)=v_debug then
  777. Message3(cg_d_register_weight,reg2str(regvars[i]^.reg),
  778. tostr(regvars[i]^.refs),regvars[i]^.name);
  779. end;
  780. end;
  781. if cs_asm_source in aktglobalswitches then
  782. procinfo^.aktentrycode^.insert(new(pai_asm_comment,init(strpnew('Register variable assignment:'))));
  783. end;
  784. end;
  785. if assigned(aktprocsym) and
  786. (pocall_inline in aktprocsym^.definition^.proccalloptions) then
  787. make_const_global:=true;
  788. do_secondpass(p);
  789. if assigned(procinfo^.def) then
  790. procinfo^.def^.fpu_used:=p^.registersfpu;
  791. end;
  792. procinfo^.aktproccode^.concatlist(exprasmlist);
  793. make_const_global:=false;
  794. end;
  795. end.
  796. {
  797. $Log$
  798. Revision 1.61 2000-03-26 10:50:04 florian
  799. * improved allocation rules for integer register variables
  800. Revision 1.60 2000/03/19 08:17:36 peter
  801. * tp7 fix
  802. Revision 1.59 2000/03/01 00:01:14 pierre
  803. Use $GOTO ON
  804. Revision 1.58 2000/02/20 20:49:45 florian
  805. * newcg is compiling
  806. * fixed the dup id problem reported by Paul Y.
  807. Revision 1.57 2000/02/10 23:44:43 florian
  808. * big update for exception handling code generation: possible mem holes
  809. fixed, break/continue/exit should work always now as expected
  810. Revision 1.56 2000/02/09 13:22:55 peter
  811. * log truncated
  812. Revision 1.55 2000/02/05 15:57:58 florian
  813. * for some strange reasons my fix regarding register variable
  814. allocation was lost
  815. Revision 1.54 2000/02/04 14:54:17 jonas
  816. * moved call to resetusableregs to compile_proc_body (put it right before the
  817. reset of the temp generator) so the optimizer can know which registers are
  818. regvars
  819. Revision 1.52 2000/01/22 15:58:12 jonas
  820. * forgot to commit a procedure for -dlogsecondpass the last time
  821. Revision 1.51 2000/01/21 12:16:53 jonas
  822. + add info on entry/exit of secondpass procedure in assembler files, between
  823. -dlogsecondpass
  824. Revision 1.50 2000/01/16 22:17:11 peter
  825. * renamed call_offset to para_offset
  826. Revision 1.49 2000/01/07 01:14:28 peter
  827. * updated copyright to 2000
  828. Revision 1.48 2000/01/04 15:15:52 florian
  829. + added compiler switch $maxfpuregisters
  830. + fixed a small problem in secondvecn
  831. Revision 1.47 1999/12/22 01:01:52 peter
  832. - removed freelabel()
  833. * added undefined label detection in internal assembler, this prevents
  834. a lot of ld crashes and wrong .o files
  835. * .o files aren't written anymore if errors have occured
  836. * inlining of assembler labels is now correct
  837. Revision 1.46 1999/12/19 23:37:18 pierre
  838. * fix for web bug735
  839. Revision 1.45 1999/12/14 09:58:42 florian
  840. + compiler checks now if a goto leaves an exception block
  841. Revision 1.44 1999/11/30 10:40:44 peter
  842. + ttype, tsymlist
  843. Revision 1.43 1999/11/18 15:34:47 pierre
  844. * Notes/Hints for local syms changed to
  845. Set_varstate function
  846. Revision 1.42 1999/11/09 23:06:45 peter
  847. * esi_offset -> selfpointer_offset to be newcg compatible
  848. * hcogegen -> cgbase fixes for newcg
  849. Revision 1.41 1999/11/06 14:34:21 peter
  850. * truncated log to 20 revs
  851. Revision 1.40 1999/09/27 23:44:52 peter
  852. * procinfo is now a pointer
  853. * support for result setting in sub procedure
  854. Revision 1.39 1999/09/26 21:30:17 peter
  855. + constant pointer support which can happend with typecasting like
  856. const p=pointer(1)
  857. * better procvar parsing in typed consts
  858. Revision 1.38 1999/09/16 23:05:54 florian
  859. * m68k compiler is again compilable (only gas writer, no assembler reader)
  860. Revision 1.37 1999/09/15 20:35:41 florian
  861. * small fix to operator overloading when in MMX mode
  862. + the compiler uses now fldz and fld1 if possible
  863. + some fixes to floating point registers
  864. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  865. * .... ???
  866. Revision 1.36 1999/09/07 14:12:35 jonas
  867. * framepointer cannot be changed to esp for methods
  868. Revision 1.35 1999/08/27 10:46:26 pierre
  869. + some EXTTEMPREGDEBUG code added
  870. }