pass_2.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. unit pass_2;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. tenumflowcontrol = (fc_exit,fc_break,fc_continue);
  25. tflowcontrol = set of tenumflowcontrol;
  26. var
  27. allow_multi_pass2 : boolean;
  28. flowcontrol : tflowcontrol;
  29. { produces assembler for the expression in variable p }
  30. { and produces an assembler node at the end }
  31. procedure generatecode(var p : tnode);
  32. { produces the actual code }
  33. function do_secondpass(var p : tnode) : boolean;
  34. procedure secondpass(var p : tnode);
  35. implementation
  36. uses
  37. {$ifdef EXTDEBUG}
  38. cutils,
  39. {$endif}
  40. globtype,systems,verbose,
  41. cclasses,globals,
  42. symconst,symbase,symtype,symsym,paramgr,
  43. aasmbase,aasmtai,
  44. pass_1,cpubase,cginfo,cgbase,
  45. {$ifdef EXTDEBUG}
  46. cgobj,
  47. {$endif EXTDEBUG}
  48. regvars,nflw,rgobj;
  49. {*****************************************************************************
  50. SecondPass
  51. *****************************************************************************}
  52. {$ifdef EXTDEBUG}
  53. procedure logsecond(ht:tnodetype; entry: boolean);
  54. const
  55. secondnames: array[tnodetype] of string[13] =
  56. ('<emptynode>',
  57. 'add-addn', {addn}
  58. 'add-muln', {muln}
  59. 'add-subn', {subn}
  60. 'moddiv-divn', {divn}
  61. 'add-symdifn', {symdifn}
  62. 'moddiv-modn', {modn}
  63. 'assignment', {assignn}
  64. 'load', {loadn}
  65. 'nothing-range', {range}
  66. 'add-ltn', {ltn}
  67. 'add-lten', {lten}
  68. 'add-gtn', {gtn}
  69. 'add-gten', {gten}
  70. 'add-equaln', {equaln}
  71. 'add-unequaln', {unequaln}
  72. 'in', {inn}
  73. 'add-orn', {orn}
  74. 'add-xorn', {xorn}
  75. 'shlshr-shrn', {shrn}
  76. 'shlshr-shln', {shln}
  77. 'add-slashn', {slashn}
  78. 'add-andn', {andn}
  79. 'subscriptn', {subscriptn}
  80. 'dderef', {derefn}
  81. 'addr', {addrn}
  82. 'doubleaddr', {doubleaddrn}
  83. 'ordconst', {ordconstn}
  84. 'typeconv', {typeconvn}
  85. 'calln', {calln}
  86. 'noth-callpar',{callparan}
  87. 'realconst', {realconstn}
  88. 'unaryminus', {unaryminusn}
  89. 'asm', {asmn}
  90. 'vecn', {vecn}
  91. 'pointerconst',{pointerconstn}
  92. 'stringconst', {stringconstn}
  93. 'funcret', {funcretn}
  94. 'selfn', {selfn}
  95. 'not', {notn}
  96. 'inline', {inlinen}
  97. 'niln', {niln}
  98. 'error', {errorn}
  99. 'nothing-typen', {typen}
  100. 'hnewn', {hnewn}
  101. 'hdisposen', {hdisposen}
  102. 'setelement', {setelementn}
  103. 'setconst', {setconstn}
  104. 'blockn', {blockn}
  105. 'statement', {statementn}
  106. 'ifn', {ifn}
  107. 'breakn', {breakn}
  108. 'continuen', {continuen}
  109. 'while_repeat', {whilerepeatn}
  110. 'for', {forn}
  111. 'exitn', {exitn}
  112. 'with', {withn}
  113. 'case', {casen}
  114. 'label', {labeln}
  115. 'goto', {goton}
  116. 'tryexcept', {tryexceptn}
  117. 'raise', {raisen}
  118. 'tryfinally', {tryfinallyn}
  119. 'on', {onn}
  120. 'is', {isn}
  121. 'as', {asn}
  122. 'error-caret', {caretn}
  123. 'fail', {failn}
  124. 'add-starstar', {starstarn}
  125. 'procinline', {procinlinen}
  126. 'arrayconstruc', {arrayconstructn}
  127. 'noth-arrcnstr', {arrayconstructrangen}
  128. 'tempcreaten',
  129. 'temprefn',
  130. 'tempdeleten',
  131. 'addoptn',
  132. 'nothing-nothg', {nothingn}
  133. 'loadvmt', {loadvmtn}
  134. 'guidconstn',
  135. 'rttin'
  136. );
  137. var
  138. p: pchar;
  139. begin
  140. if entry then
  141. p := strpnew('second '+secondnames[ht]+' (entry)')
  142. else
  143. p := strpnew('second '+secondnames[ht]+' (exit)');
  144. exprasmlist.concat(tai_comment.create(p));
  145. end;
  146. {$endif EXTDEBUG}
  147. procedure secondpass(var p : tnode);
  148. var
  149. oldcodegenerror : boolean;
  150. oldlocalswitches : tlocalswitches;
  151. oldpos : tfileposinfo;
  152. {$ifdef TEMPREGDEBUG}
  153. prevp : pptree;
  154. {$endif TEMPREGDEBUG}
  155. {$ifdef EXTDEBUG}
  156. i : longint;
  157. {$endif EXTDEBUG}
  158. begin
  159. if not assigned(p) then
  160. internalerror(200208221);
  161. if not(nf_error in p.flags) then
  162. begin
  163. oldcodegenerror:=codegenerror;
  164. oldlocalswitches:=aktlocalswitches;
  165. oldpos:=aktfilepos;
  166. {$ifdef TEMPREGDEBUG}
  167. testregisters32;
  168. prevp:=curptree;
  169. curptree:=@p;
  170. p^.usableregs:=usablereg32;
  171. {$endif TEMPREGDEBUG}
  172. aktfilepos:=p.fileinfo;
  173. aktlocalswitches:=p.localswitches;
  174. codegenerror:=false;
  175. {$ifdef EXTDEBUG}
  176. if (p.expectloc=LOC_INVALID) then
  177. Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
  178. if (not allow_multi_pass2) and
  179. (p.location.loc<>LOC_INVALID) then
  180. Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
  181. if (cs_asm_nodes in aktglobalswitches) then
  182. logsecond(p.nodetype,true);
  183. {$endif EXTDEBUG}
  184. p.pass_2;
  185. {$ifdef EXTDEBUG}
  186. if (cs_asm_nodes in aktglobalswitches) then
  187. logsecond(p.nodetype,false);
  188. if (not codegenerror) then
  189. begin
  190. if (p.location.loc=LOC_INVALID) then
  191. Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype])
  192. else if (p.location.loc<>p.expectloc) then
  193. Comment(V_Warning,'Location is different in secondpass: '+nodetype2str[p.nodetype]);
  194. end;
  195. { check if all scratch registers are freed }
  196. for i:=1 to max_scratch_regs do
  197. if not(scratch_regs[i] in cg.unusedscratchregisters) then
  198. begin
  199. writenode(p);
  200. internalerror(2003042201);
  201. end;
  202. {$endif EXTDEBUG}
  203. if codegenerror then
  204. include(p.flags,nf_error);
  205. codegenerror:=codegenerror or oldcodegenerror;
  206. aktlocalswitches:=oldlocalswitches;
  207. aktfilepos:=oldpos;
  208. {$ifdef TEMPREGDEBUG}
  209. curptree:=prevp;
  210. {$endif TEMPREGDEBUG}
  211. {$ifdef EXTTEMPREGDEBUG}
  212. if p.usableregs-usablereg32>p.reallyusedregs then
  213. p.reallyusedregs:=p.usableregs-usablereg32;
  214. if p.reallyusedregs<p.registers32 then
  215. Comment(V_Debug,'registers32 overestimated '+tostr(p^.registers32)+
  216. '>'+tostr(p^.reallyusedregs));
  217. {$endif EXTTEMPREGDEBUG}
  218. end
  219. else
  220. codegenerror:=true;
  221. end;
  222. function do_secondpass(var p : tnode) : boolean;
  223. begin
  224. codegenerror:=false;
  225. if not(nf_error in p.flags) then
  226. secondpass(p);
  227. do_secondpass:=codegenerror;
  228. end;
  229. procedure clearrefs(p : tnamedindexitem;arg:pointer);
  230. begin
  231. if (tsym(p).typ=varsym) then
  232. if tvarsym(p).refs>1 then
  233. tvarsym(p).refs:=1;
  234. end;
  235. procedure generatecode(var p : tnode);
  236. begin
  237. rg.cleartempgen;
  238. flowcontrol:=[];
  239. { when size optimization only count occurrence }
  240. if cs_littlesize in aktglobalswitches then
  241. rg.t_times:=1
  242. else
  243. { reference for repetition is 100 }
  244. rg.t_times:=100;
  245. { clear register count }
  246. rg.clearregistercount;
  247. use_esp_stackframe:=false;
  248. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  249. symtablestack.next.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  250. { firstpass everything }
  251. do_firstpass(p);
  252. { only do secondpass if there are no errors }
  253. if ErrorCount=0 then
  254. begin
  255. {$ifdef OMITSTACKFRAME}
  256. if (cs_regalloc in aktglobalswitches) and
  257. ((procinfo^.flags and (pi_uses_asm or pi_uses_exceptions))=0) then
  258. begin
  259. { can we omit the stack frame ? }
  260. { conditions:
  261. 1. procedure (not main block)
  262. 2. no constructor or destructor
  263. 3. no call to other procedures
  264. 4. no interrupt handler
  265. }
  266. {!!!!!! this doesn work yet, because of problems with
  267. with linux and windows
  268. }
  269. (*
  270. if assigned(aktprocsym) then
  271. begin
  272. if not(assigned(procinfo^._class)) and
  273. not(aktprocdef.proctypeoption in [potype_constructor,potype_destructor]) and
  274. not(po_interrupt in aktprocdef.procoptions) and
  275. ((procinfo^.flags and pi_do_call)=0) and
  276. (lexlevel>=normal_function_level) then
  277. begin
  278. { use ESP as frame pointer }
  279. procinfo^.framepointer:=STACK_POINTER_REG;
  280. use_esp_stackframe:=true;
  281. { calc parameter distance new }
  282. dec(procinfo^.framepointer_offset,4);
  283. dec(procinfo^.selfpointer_offset,4);
  284. { is this correct ???}
  285. { retoffset can be negativ for results in eax !! }
  286. { the value should be decreased only if positive }
  287. if procinfo.retoffset>=0 then
  288. dec(procinfo.retoffset,4);
  289. dec(procinfo.para_offset,4);
  290. aktprocdef.parast.address_fixup:=procinfo.para_offset;
  291. end;
  292. end;
  293. *)
  294. end;
  295. {$endif OMITSTACKFRAME}
  296. { assign parameter locations }
  297. {$ifndef i386}
  298. setparalocs(procinfo.procdef);
  299. {$endif i386}
  300. procinfo.after_pass1;
  301. { process register variable stuff (JM) }
  302. assign_regvars(p);
  303. load_regvars(procinfo.aktentrycode,p);
  304. { for the i386 it must be done in genexitcode because it has }
  305. { to add 'fstp' instructions when using fpu regvars and those }
  306. { must come after the "exitlabel" (JM) }
  307. {$ifndef i386}
  308. cleanup_regvars(procinfo.aktexitcode);
  309. {$endif i386}
  310. do_secondpass(p);
  311. if assigned(procinfo.procdef) then
  312. procinfo.procdef.fpu_used:=p.registersfpu;
  313. end;
  314. procinfo.aktproccode.concatlist(exprasmlist);
  315. end;
  316. end.
  317. {
  318. $Log$
  319. Revision 1.46 2003-04-23 10:12:14 peter
  320. * allow multi pass2 changed to global boolean instead of node flag
  321. Revision 1.45 2003/04/22 23:50:23 peter
  322. * firstpass uses expectloc
  323. * checks if there are differences between the expectloc and
  324. location.loc from secondpass in EXTDEBUG
  325. Revision 1.44 2003/04/22 12:45:58 florian
  326. * fixed generic in operator code
  327. + added debug code to check if all scratch registers are released
  328. Revision 1.43 2003/01/09 20:40:59 daniel
  329. * Converted some code in cgx86.pas to new register numbering
  330. Revision 1.42 2003/01/09 15:49:56 daniel
  331. * Added register conversion
  332. Revision 1.41 2002/12/22 14:35:39 peter
  333. * removed Writeln
  334. Revision 1.40 2002/12/21 23:21:47 mazen
  335. + added support for the shift nodes
  336. + added debug output on screen with -an command line option
  337. Revision 1.39 2002/08/23 16:14:49 peter
  338. * tempgen cleanup
  339. * tt_noreuse temp type added that will be used in genentrycode
  340. Revision 1.38 2002/08/20 16:55:38 peter
  341. * don't write (stabs)line info when inlining a procedure
  342. Revision 1.37 2002/08/19 19:36:44 peter
  343. * More fixes for cross unit inlining, all tnodes are now implemented
  344. * Moved pocall_internconst to po_internconst because it is not a
  345. calling type at all and it conflicted when inlining of these small
  346. functions was requested
  347. Revision 1.36 2002/08/18 20:06:24 peter
  348. * inlining is now also allowed in interface
  349. * renamed write/load to ppuwrite/ppuload
  350. * tnode storing in ppu
  351. * nld,ncon,nbas are already updated for storing in ppu
  352. Revision 1.35 2002/08/17 09:23:38 florian
  353. * first part of procinfo rewrite
  354. Revision 1.34 2002/08/15 19:10:35 peter
  355. * first things tai,tnode storing in ppu
  356. Revision 1.33 2002/07/30 20:50:44 florian
  357. * the code generator knows now if parameters are in registers
  358. Revision 1.32 2002/07/19 11:41:36 daniel
  359. * State tracker work
  360. * The whilen and repeatn are now completely unified into whilerepeatn. This
  361. allows the state tracker to change while nodes automatically into
  362. repeat nodes.
  363. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  364. 'not(a>b)' is optimized into 'a<=b'.
  365. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  366. by removing the notn and later switchting the true and falselabels. The
  367. same is done with 'repeat until not a'.
  368. Revision 1.31 2002/07/01 18:46:25 peter
  369. * internal linker
  370. * reorganized aasm layer
  371. Revision 1.30 2002/05/18 13:34:11 peter
  372. * readded missing revisions
  373. Revision 1.29 2002/05/16 19:46:42 carl
  374. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  375. + try to fix temp allocation (still in ifdef)
  376. + generic constructor calls
  377. + start of tassembler / tmodulebase class cleanup
  378. Revision 1.27 2002/05/12 16:53:08 peter
  379. * moved entry and exitcode to ncgutil and cgobj
  380. * foreach gets extra argument for passing local data to the
  381. iterator function
  382. * -CR checks also class typecasts at runtime by changing them
  383. into as
  384. * fixed compiler to cycle with the -CR option
  385. * fixed stabs with elf writer, finally the global variables can
  386. be watched
  387. * removed a lot of routines from cga unit and replaced them by
  388. calls to cgobj
  389. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  390. u32bit then the other is typecasted also to u32bit without giving
  391. a rangecheck warning/error.
  392. * fixed pascal calling method with reversing also the high tree in
  393. the parast, detected by tcalcst3 test
  394. Revision 1.26 2002/04/21 19:02:04 peter
  395. * removed newn and disposen nodes, the code is now directly
  396. inlined from pexpr
  397. * -an option that will write the secondpass nodes to the .s file, this
  398. requires EXTDEBUG define to actually write the info
  399. * fixed various internal errors and crashes due recent code changes
  400. Revision 1.25 2002/04/20 21:32:24 carl
  401. + generic FPC_CHECKPOINTER
  402. + first parameter offset in stack now portable
  403. * rename some constants
  404. + move some cpu stuff to other units
  405. - remove unused constents
  406. * fix stacksize for some targets
  407. * fix generic size problems which depend now on EXTEND_SIZE constant
  408. Revision 1.24 2002/04/07 13:30:13 carl
  409. - removed unused variable
  410. Revision 1.23 2002/04/02 17:11:29 peter
  411. * tlocation,treference update
  412. * LOC_CONSTANT added for better constant handling
  413. * secondadd splitted in multiple routines
  414. * location_force_reg added for loading a location to a register
  415. of a specified size
  416. * secondassignment parses now first the right and then the left node
  417. (this is compatible with Kylix). This saves a lot of push/pop especially
  418. with string operations
  419. * adapted some routines to use the new cg methods
  420. Revision 1.22 2002/03/31 20:26:35 jonas
  421. + a_loadfpu_* and a_loadmm_* methods in tcg
  422. * register allocation is now handled by a class and is mostly processor
  423. independent (+rgobj.pas and i386/rgcpu.pas)
  424. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  425. * some small improvements and fixes to the optimizer
  426. * some register allocation fixes
  427. * some fpuvaroffset fixes in the unary minus node
  428. * push/popusedregisters is now called rg.save/restoreusedregisters and
  429. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  430. also better optimizable)
  431. * fixed and optimized register saving/restoring for new/dispose nodes
  432. * LOC_FPU locations now also require their "register" field to be set to
  433. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  434. - list field removed of the tnode class because it's not used currently
  435. and can cause hard-to-find bugs
  436. }