pass_2.pas 20 KB

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