pass_2.pas 35 KB

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