pass_2.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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,cgbase,
  45. procinfo,
  46. {$ifdef EXTDEBUG}
  47. cgobj,
  48. {$endif EXTDEBUG}
  49. regvars,nflw,rgobj;
  50. {*****************************************************************************
  51. SecondPass
  52. *****************************************************************************}
  53. {$ifdef EXTDEBUG}
  54. procedure logsecond(ht:tnodetype; entry: boolean);
  55. const
  56. secondnames: array[tnodetype] of string[13] =
  57. ('<emptynode>',
  58. 'add-addn', {addn}
  59. 'add-muln', {muln}
  60. 'add-subn', {subn}
  61. 'moddiv-divn', {divn}
  62. 'add-symdifn', {symdifn}
  63. 'moddiv-modn', {modn}
  64. 'assignment', {assignn}
  65. 'load', {loadn}
  66. 'nothing-range', {range}
  67. 'add-ltn', {ltn}
  68. 'add-lten', {lten}
  69. 'add-gtn', {gtn}
  70. 'add-gten', {gten}
  71. 'add-equaln', {equaln}
  72. 'add-unequaln', {unequaln}
  73. 'in', {inn}
  74. 'add-orn', {orn}
  75. 'add-xorn', {xorn}
  76. 'shlshr-shrn', {shrn}
  77. 'shlshr-shln', {shln}
  78. 'add-slashn', {slashn}
  79. 'add-andn', {andn}
  80. 'subscriptn', {subscriptn}
  81. 'deref', {derefn}
  82. 'addr', {addrn}
  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. 'not', {notn}
  94. 'inline', {inlinen}
  95. 'niln', {niln}
  96. 'error', {errorn}
  97. 'nothing-typen', {typen}
  98. 'setelement', {setelementn}
  99. 'setconst', {setconstn}
  100. 'blockn', {blockn}
  101. 'statement', {statementn}
  102. 'ifn', {ifn}
  103. 'breakn', {breakn}
  104. 'continuen', {continuen}
  105. 'while_repeat', {whilerepeatn}
  106. 'for', {forn}
  107. 'exitn', {exitn}
  108. 'with', {withn}
  109. 'case', {casen}
  110. 'label', {labeln}
  111. 'goto', {goton}
  112. 'tryexcept', {tryexceptn}
  113. 'raise', {raisen}
  114. 'tryfinally', {tryfinallyn}
  115. 'on', {onn}
  116. 'is', {isn}
  117. 'as', {asn}
  118. 'error-caret', {caretn}
  119. 'add-starstar', {starstarn}
  120. 'arrayconstruc', {arrayconstructn}
  121. 'noth-arrcnstr', {arrayconstructrangen}
  122. 'tempcreaten',
  123. 'temprefn',
  124. 'tempdeleten',
  125. 'addoptn',
  126. 'nothing-nothg', {nothingn}
  127. 'loadvmt', {loadvmtn}
  128. 'guidconstn',
  129. 'rttin',
  130. 'loadparentfpn'
  131. );
  132. var
  133. p: pchar;
  134. begin
  135. if entry then
  136. p := strpnew('second '+secondnames[ht]+' (entry)')
  137. else
  138. p := strpnew('second '+secondnames[ht]+' (exit)');
  139. exprasmlist.concat(tai_comment.create(p));
  140. end;
  141. {$endif EXTDEBUG}
  142. procedure secondpass(var p : tnode);
  143. var
  144. oldcodegenerror : boolean;
  145. oldlocalswitches : tlocalswitches;
  146. oldpos : tfileposinfo;
  147. {$ifdef TEMPREGDEBUG}
  148. prevp : pptree;
  149. {$endif TEMPREGDEBUG}
  150. {$ifdef EXTDEBUG}
  151. {$endif EXTDEBUG}
  152. begin
  153. if not assigned(p) then
  154. internalerror(200208221);
  155. if not(nf_error in p.flags) then
  156. begin
  157. oldcodegenerror:=codegenerror;
  158. oldlocalswitches:=aktlocalswitches;
  159. oldpos:=aktfilepos;
  160. {$ifdef TEMPREGDEBUG}
  161. testregisters32;
  162. prevp:=curptree;
  163. curptree:=@p;
  164. p^.usableregs:=usablereg32;
  165. {$endif TEMPREGDEBUG}
  166. aktfilepos:=p.fileinfo;
  167. aktlocalswitches:=p.localswitches;
  168. codegenerror:=false;
  169. {$ifdef EXTDEBUG}
  170. if (p.expectloc=LOC_INVALID) then
  171. Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
  172. if (not allow_multi_pass2) and
  173. (p.location.loc<>LOC_INVALID) then
  174. Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
  175. if (cs_asm_nodes in aktglobalswitches) then
  176. logsecond(p.nodetype,true);
  177. {$endif EXTDEBUG}
  178. p.pass_2;
  179. {$ifdef EXTDEBUG}
  180. if (cs_asm_nodes in aktglobalswitches) then
  181. logsecond(p.nodetype,false);
  182. if (not codegenerror) then
  183. begin
  184. if (p.location.loc=LOC_INVALID) then
  185. Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype])
  186. else if (p.location.loc<>p.expectloc) then
  187. Comment(V_Warning,'Location is different in secondpass: '+nodetype2str[p.nodetype]);
  188. end;
  189. {$ifdef i386}
  190. if rg.unusedregsint*([first_int_supreg..last_int_supreg] - [RS_FRAME_POINTER_REG,RS_STACK_POINTER_REG])<>
  191. ([first_int_supreg..last_int_supreg] - [RS_FRAME_POINTER_REG,RS_STACK_POINTER_REG]) then
  192. internalerror(200306171);
  193. {$endif i386}
  194. {$endif EXTDEBUG}
  195. if codegenerror then
  196. include(p.flags,nf_error);
  197. codegenerror:=codegenerror or oldcodegenerror;
  198. aktlocalswitches:=oldlocalswitches;
  199. aktfilepos:=oldpos;
  200. {$ifdef TEMPREGDEBUG}
  201. curptree:=prevp;
  202. {$endif TEMPREGDEBUG}
  203. {$ifdef EXTTEMPREGDEBUG}
  204. if p.usableregs-usablereg32>p.reallyusedregs then
  205. p.reallyusedregs:=p.usableregs-usablereg32;
  206. if p.reallyusedregs<p.registers32 then
  207. Comment(V_Debug,'registers32 overestimated '+tostr(p^.registers32)+
  208. '>'+tostr(p^.reallyusedregs));
  209. {$endif EXTTEMPREGDEBUG}
  210. end
  211. else
  212. codegenerror:=true;
  213. end;
  214. function do_secondpass(var p : tnode) : boolean;
  215. begin
  216. codegenerror:=false;
  217. if not(nf_error in p.flags) then
  218. secondpass(p);
  219. do_secondpass:=codegenerror;
  220. end;
  221. procedure clearrefs(p : tnamedindexitem;arg:pointer);
  222. begin
  223. if (tsym(p).typ=varsym) then
  224. if tvarsym(p).refs>1 then
  225. tvarsym(p).refs:=1;
  226. end;
  227. procedure generatecode(var p : tnode);
  228. {$ifdef EXTDEBUG}
  229. var
  230. sr : tsuperregister;
  231. {$endif EXTDEBUG}
  232. begin
  233. flowcontrol:=[];
  234. { when size optimization only count occurrence }
  235. if cs_littlesize in aktglobalswitches then
  236. rg.t_times:=1
  237. else
  238. { reference for repetition is 100 }
  239. rg.t_times:=100;
  240. { clear register count }
  241. rg.clearregistercount;
  242. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  243. symtablestack.next.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  244. { firstpass everything }
  245. do_firstpass(p);
  246. { only do secondpass if there are no errors }
  247. if ErrorCount=0 then
  248. begin
  249. { caller paraloc info is also necessary in the stackframe_entry }
  250. { code of the ppc (and possibly other processors) }
  251. if not current_procinfo.procdef.has_paraloc_info then
  252. begin
  253. paramanager.create_paraloc_info(current_procinfo.procdef,callerside);
  254. current_procinfo.procdef.has_paraloc_info:=true;
  255. end;
  256. { process register variable stuff (JM) }
  257. assign_regvars(p);
  258. // load_regvars(current_procinfo.aktentrycode,p);
  259. { for the i386 it must be done in genexitcode because it has }
  260. { to add 'fstp' instructions when using fpu regvars and those }
  261. { must come after the "exitlabel" (JM) }
  262. {$ifndef i386}
  263. // cleanup_regvars(current_procinfo.aktexitcode);
  264. {$endif i386}
  265. current_procinfo.allocate_framepointer_reg;
  266. do_secondpass(p);
  267. {$ifdef EXTDEBUG}
  268. for sr:=first_int_imreg to last_int_imreg do
  269. if not(sr in rg.unusedregsint) then
  270. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,sr,R_SUBNONE))+' not released');
  271. {$endif EXTDEBUG}
  272. if assigned(current_procinfo.procdef) then
  273. current_procinfo.procdef.fpu_used:=p.registersfpu;
  274. end;
  275. current_procinfo.aktproccode.concatlist(exprasmlist);
  276. end;
  277. end.
  278. {
  279. $Log$
  280. Revision 1.67 2003-10-01 20:34:49 peter
  281. * procinfo unit contains tprocinfo
  282. * cginfo renamed to cgbase
  283. * moved cgmessage to verbose
  284. * fixed ppc and sparc compiles
  285. Revision 1.66 2003/09/28 21:45:52 peter
  286. * fix register leak in with debug
  287. Revision 1.65 2003/09/28 17:55:04 peter
  288. * parent framepointer changed to hidden parameter
  289. * tloadparentfpnode added
  290. Revision 1.64 2003/09/03 15:55:01 peter
  291. * NEWRA branch merged
  292. Revision 1.63.2.3 2003/08/31 15:46:26 peter
  293. * more updates for tregister
  294. Revision 1.63.2.2 2003/08/31 13:50:16 daniel
  295. * Remove sorting and use pregenerated indexes
  296. * Some work on making things compile
  297. Revision 1.63.2.1 2003/08/29 17:28:59 peter
  298. * next batch of updates
  299. Revision 1.63 2003/08/11 21:18:20 peter
  300. * start of sparc support for newra
  301. Revision 1.62 2003/08/10 17:25:23 peter
  302. * fixed some reported bugs
  303. Revision 1.61 2003/07/06 17:58:22 peter
  304. * framepointer fixes for sparc
  305. * parent framepointer code more generic
  306. Revision 1.60 2003/07/06 15:31:20 daniel
  307. * Fixed register allocator. *Lots* of fixes.
  308. Revision 1.59 2003/07/06 10:18:47 jonas
  309. * also generate the caller paraloc info of a procedure if it doesn't exist
  310. yet at the start of pass_2
  311. Revision 1.58 2003/07/05 20:13:03 jonas
  312. * create_paraloc_info() is now called separately for the caller and
  313. callee info
  314. * fixed ppc cycle
  315. Revision 1.57 2003/06/13 21:19:30 peter
  316. * current_procdef removed, use current_procinfo.procdef instead
  317. Revision 1.56 2003/06/12 16:43:07 peter
  318. * newra compiles for sparc
  319. Revision 1.55 2003/06/09 12:23:30 peter
  320. * init/final of procedure data splitted from genentrycode
  321. * use asmnode getposition to insert final at the correct position
  322. als for the implicit try...finally
  323. Revision 1.54 2003/06/03 13:01:59 daniel
  324. * Register allocator finished
  325. Revision 1.53 2003/05/26 21:17:17 peter
  326. * procinlinenode removed
  327. * aktexit2label removed, fast exit removed
  328. + tcallnode.inlined_pass_2 added
  329. Revision 1.52 2003/05/15 18:58:53 peter
  330. * removed selfpointer_offset, vmtpointer_offset
  331. * tvarsym.adjusted_address
  332. * address in localsymtable is now in the real direction
  333. * removed some obsolete globals
  334. Revision 1.51 2003/05/13 19:14:41 peter
  335. * failn removed
  336. * inherited result code check moven to pexpr
  337. Revision 1.50 2003/05/09 17:47:02 peter
  338. * self moved to hidden parameter
  339. * removed hdisposen,hnewn,selfn
  340. Revision 1.49 2003/04/27 11:21:33 peter
  341. * aktprocdef renamed to current_procinfo.procdef
  342. * procinfo renamed to current_procinfo
  343. * procinfo will now be stored in current_module so it can be
  344. cleaned up properly
  345. * gen_main_procsym changed to create_main_proc and release_main_proc
  346. to also generate a tprocinfo structure
  347. * fixed unit implicit initfinal
  348. Revision 1.48 2003/04/27 07:29:50 peter
  349. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  350. a new procdef declaration
  351. * aktprocsym removed
  352. * lexlevel removed, use symtable.symtablelevel instead
  353. * implicit init/final code uses the normal genentry/genexit
  354. * funcret state checking updated for new funcret handling
  355. Revision 1.47 2003/04/25 20:59:33 peter
  356. * removed funcretn,funcretsym, function result is now in varsym
  357. and aliases for result and function name are added using absolutesym
  358. * vs_hidden parameter for funcret passed in parameter
  359. * vs_hidden fixes
  360. * writenode changed to printnode and released from extdebug
  361. * -vp option added to generate a tree.log with the nodetree
  362. * nicer printnode for statements, callnode
  363. Revision 1.46 2003/04/23 10:12:14 peter
  364. * allow multi pass2 changed to global boolean instead of node flag
  365. Revision 1.45 2003/04/22 23:50:23 peter
  366. * firstpass uses expectloc
  367. * checks if there are differences between the expectloc and
  368. location.loc from secondpass in EXTDEBUG
  369. Revision 1.44 2003/04/22 12:45:58 florian
  370. * fixed generic in operator code
  371. + added debug code to check if all scratch registers are released
  372. Revision 1.43 2003/01/09 20:40:59 daniel
  373. * Converted some code in cgx86.pas to new register numbering
  374. Revision 1.42 2003/01/09 15:49:56 daniel
  375. * Added register conversion
  376. Revision 1.41 2002/12/22 14:35:39 peter
  377. * removed Writeln
  378. Revision 1.40 2002/12/21 23:21:47 mazen
  379. + added support for the shift nodes
  380. + added debug output on screen with -an command line option
  381. Revision 1.39 2002/08/23 16:14:49 peter
  382. * tempgen cleanup
  383. * tt_noreuse temp type added that will be used in genentrycode
  384. Revision 1.38 2002/08/20 16:55:38 peter
  385. * don't write (stabs)line info when inlining a procedure
  386. Revision 1.37 2002/08/19 19:36:44 peter
  387. * More fixes for cross unit inlining, all tnodes are now implemented
  388. * Moved pocall_internconst to po_internconst because it is not a
  389. calling type at all and it conflicted when inlining of these small
  390. functions was requested
  391. Revision 1.36 2002/08/18 20:06:24 peter
  392. * inlining is now also allowed in interface
  393. * renamed write/load to ppuwrite/ppuload
  394. * tnode storing in ppu
  395. * nld,ncon,nbas are already updated for storing in ppu
  396. Revision 1.35 2002/08/17 09:23:38 florian
  397. * first part of procinfo rewrite
  398. Revision 1.34 2002/08/15 19:10:35 peter
  399. * first things tai,tnode storing in ppu
  400. Revision 1.33 2002/07/30 20:50:44 florian
  401. * the code generator knows now if parameters are in registers
  402. Revision 1.32 2002/07/19 11:41:36 daniel
  403. * State tracker work
  404. * The whilen and repeatn are now completely unified into whilerepeatn. This
  405. allows the state tracker to change while nodes automatically into
  406. repeat nodes.
  407. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  408. 'not(a>b)' is optimized into 'a<=b'.
  409. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  410. by removing the notn and later switchting the true and falselabels. The
  411. same is done with 'repeat until not a'.
  412. Revision 1.31 2002/07/01 18:46:25 peter
  413. * internal linker
  414. * reorganized aasm layer
  415. Revision 1.30 2002/05/18 13:34:11 peter
  416. * readded missing revisions
  417. Revision 1.29 2002/05/16 19:46:42 carl
  418. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  419. + try to fix temp allocation (still in ifdef)
  420. + generic constructor calls
  421. + start of tassembler / tmodulebase class cleanup
  422. Revision 1.27 2002/05/12 16:53:08 peter
  423. * moved entry and exitcode to ncgutil and cgobj
  424. * foreach gets extra argument for passing local data to the
  425. iterator function
  426. * -CR checks also class typecasts at runtime by changing them
  427. into as
  428. * fixed compiler to cycle with the -CR option
  429. * fixed stabs with elf writer, finally the global variables can
  430. be watched
  431. * removed a lot of routines from cga unit and replaced them by
  432. calls to cgobj
  433. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  434. u32bit then the other is typecasted also to u32bit without giving
  435. a rangecheck warning/error.
  436. * fixed pascal calling method with reversing also the high tree in
  437. the parast, detected by tcalcst3 test
  438. Revision 1.26 2002/04/21 19:02:04 peter
  439. * removed newn and disposen nodes, the code is now directly
  440. inlined from pexpr
  441. * -an option that will write the secondpass nodes to the .s file, this
  442. requires EXTDEBUG define to actually write the info
  443. * fixed various internal errors and crashes due recent code changes
  444. Revision 1.25 2002/04/20 21:32:24 carl
  445. + generic FPC_CHECKPOINTER
  446. + first parameter offset in stack now portable
  447. * rename some constants
  448. + move some cpu stuff to other units
  449. - remove unused constents
  450. * fix stacksize for some targets
  451. * fix generic size problems which depend now on EXTEND_SIZE constant
  452. Revision 1.24 2002/04/07 13:30:13 carl
  453. - removed unused variable
  454. Revision 1.23 2002/04/02 17:11:29 peter
  455. * tlocation,treference update
  456. * LOC_CONSTANT added for better constant handling
  457. * secondadd splitted in multiple routines
  458. * location_force_reg added for loading a location to a register
  459. of a specified size
  460. * secondassignment parses now first the right and then the left node
  461. (this is compatible with Kylix). This saves a lot of push/pop especially
  462. with string operations
  463. * adapted some routines to use the new cg methods
  464. Revision 1.22 2002/03/31 20:26:35 jonas
  465. + a_loadfpu_* and a_loadmm_* methods in tcg
  466. * register allocation is now handled by a class and is mostly processor
  467. independent (+rgobj.pas and i386/rgcpu.pas)
  468. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  469. * some small improvements and fixes to the optimizer
  470. * some register allocation fixes
  471. * some fpuvaroffset fixes in the unary minus node
  472. * push/popusedregisters is now called rg.save/restoreusedregisters and
  473. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  474. also better optimizable)
  475. * fixed and optimized register saving/restoring for new/dispose nodes
  476. * LOC_FPU locations now also require their "register" field to be set to
  477. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  478. - list field removed of the tnode class because it's not used currently
  479. and can cause hard-to-find bugs
  480. }