mirror of
				https://gitlab.com/freepascal.org/fpc/source.git
				synced 2025-10-31 13:31:40 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			489 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			ObjectPascal
		
	
	
	
	
	
			
		
		
	
	
			489 lines
		
	
	
		
			17 KiB
		
	
	
	
		
			ObjectPascal
		
	
	
	
	
	
| {
 | |
|     $Id$
 | |
|     Copyright (c) 1998-2002 by Florian Klaempfl
 | |
| 
 | |
|     This unit handles the codegeneration pass
 | |
| 
 | |
|     This program is free software; you can redistribute it and/or modify
 | |
|     it under the terms of the GNU General Public License as published by
 | |
|     the Free Software Foundation; either version 2 of the License, or
 | |
|     (at your option) any later version.
 | |
| 
 | |
|     This program is distributed in the hope that it will be useful,
 | |
|     but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
|     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | |
|     GNU General Public License for more details.
 | |
| 
 | |
|     You should have received a copy of the GNU General Public License
 | |
|     along with this program; if not, write to the Free Software
 | |
|     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 | |
| 
 | |
|  ****************************************************************************
 | |
| }
 | |
| unit pass_2;
 | |
| 
 | |
| {$i fpcdefs.inc}
 | |
| 
 | |
| interface
 | |
| 
 | |
| uses
 | |
|    node;
 | |
| 
 | |
|     type
 | |
|        tenumflowcontrol = (fc_exit,fc_break,fc_continue);
 | |
|        tflowcontrol = set of tenumflowcontrol;
 | |
| 
 | |
|     var
 | |
|        allow_multi_pass2 : boolean;
 | |
|        flowcontrol : tflowcontrol;
 | |
| 
 | |
| { produces the actual code }
 | |
| function do_secondpass(var p : tnode) : boolean;
 | |
| procedure secondpass(var p : tnode);
 | |
| 
 | |
| 
 | |
| implementation
 | |
| 
 | |
|    uses
 | |
| {$ifdef EXTDEBUG}
 | |
|      cutils,
 | |
| {$endif}
 | |
|      globtype,systems,verbose,
 | |
|      globals,
 | |
|      paramgr,
 | |
|      aasmtai,
 | |
|      cgbase,
 | |
|      nflw,cgobj;
 | |
| 
 | |
| {*****************************************************************************
 | |
|                               SecondPass
 | |
| *****************************************************************************}
 | |
| 
 | |
| {$ifdef EXTDEBUG}
 | |
|      procedure logsecond(ht:tnodetype; entry: boolean);
 | |
|        const
 | |
|          secondnames: array[tnodetype] of string[13] =
 | |
|             ('<emptynode>',
 | |
|              'add-addn',  {addn}
 | |
|              'add-muln',  {muln}
 | |
|              'add-subn',  {subn}
 | |
|              'moddiv-divn',      {divn}
 | |
|              'add-symdifn',      {symdifn}
 | |
|              'moddiv-modn',      {modn}
 | |
|              'assignment',  {assignn}
 | |
|              'load',        {loadn}
 | |
|              'nothing-range',     {range}
 | |
|              'add-ltn',  {ltn}
 | |
|              'add-lten',  {lten}
 | |
|              'add-gtn',  {gtn}
 | |
|              'add-gten',  {gten}
 | |
|              'add-equaln',  {equaln}
 | |
|              'add-unequaln',  {unequaln}
 | |
|              'in',    {inn}
 | |
|              'add-orn',  {orn}
 | |
|              'add-xorn',  {xorn}
 | |
|              'shlshr-shrn',      {shrn}
 | |
|              'shlshr-shln',      {shln}
 | |
|              'add-slashn',  {slashn}
 | |
|              'add-andn',  {andn}
 | |
|              'subscriptn',  {subscriptn}
 | |
|              'deref',       {derefn}
 | |
|              'addr',        {addrn}
 | |
|              'ordconst',    {ordconstn}
 | |
|              'typeconv',    {typeconvn}
 | |
|              'calln',       {calln}
 | |
|              'noth-callpar',{callparan}
 | |
|              'realconst',   {realconstn}
 | |
|              'unaryminus',  {unaryminusn}
 | |
|              'asm',         {asmn}
 | |
|              'vecn',        {vecn}
 | |
|              'pointerconst',{pointerconstn}
 | |
|              'stringconst', {stringconstn}
 | |
|              'not',         {notn}
 | |
|              'inline',      {inlinen}
 | |
|              'niln',        {niln}
 | |
|              'error',       {errorn}
 | |
|              'nothing-typen',     {typen}
 | |
|              'setelement',  {setelementn}
 | |
|              'setconst',    {setconstn}
 | |
|              'blockn',      {blockn}
 | |
|              'statement',   {statementn}
 | |
|              'ifn',         {ifn}
 | |
|              'breakn',      {breakn}
 | |
|              'continuen',   {continuen}
 | |
|              'while_repeat', {whilerepeatn}
 | |
|              'for',         {forn}
 | |
|              'exitn',       {exitn}
 | |
|              'with',        {withn}
 | |
|              'case',        {casen}
 | |
|              'label',       {labeln}
 | |
|              'goto',        {goton}
 | |
|              'tryexcept',   {tryexceptn}
 | |
|              'raise',       {raisen}
 | |
|              'tryfinally',  {tryfinallyn}
 | |
|              'on',    {onn}
 | |
|              'is',    {isn}
 | |
|              'as',    {asn}
 | |
|              'error-caret',       {caretn}
 | |
|              'add-starstar',  {starstarn}
 | |
|              'arrayconstruc', {arrayconstructn}
 | |
|              'noth-arrcnstr',     {arrayconstructrangen}
 | |
|              'tempcreaten',
 | |
|              'temprefn',
 | |
|              'tempdeleten',
 | |
|              'addoptn',
 | |
|              'nothing-nothg',     {nothingn}
 | |
|              'loadvmt',      {loadvmtn}
 | |
|              'guidconstn',
 | |
|              'rttin',
 | |
|              'loadparentfpn'
 | |
|              );
 | |
|       var
 | |
|         p: pchar;
 | |
|       begin
 | |
|         if entry then
 | |
|           p := strpnew('second '+secondnames[ht]+' (entry)')
 | |
|         else
 | |
|           p := strpnew('second '+secondnames[ht]+' (exit)');
 | |
|         exprasmlist.concat(tai_comment.create(p));
 | |
|       end;
 | |
| {$endif EXTDEBUG}
 | |
| 
 | |
|      procedure secondpass(var p : tnode);
 | |
|       var
 | |
|          oldcodegenerror  : boolean;
 | |
|          oldlocalswitches : tlocalswitches;
 | |
|          oldpos    : tfileposinfo;
 | |
|       begin
 | |
|          if not assigned(p) then
 | |
|           internalerror(200208221);
 | |
|          if not(nf_error in p.flags) then
 | |
|           begin
 | |
|             oldcodegenerror:=codegenerror;
 | |
|             oldlocalswitches:=aktlocalswitches;
 | |
|             oldpos:=aktfilepos;
 | |
|             if not inlining_procedure then
 | |
|               aktfilepos:=p.fileinfo;
 | |
|             aktlocalswitches:=p.localswitches;
 | |
|             codegenerror:=false;
 | |
| {$ifdef EXTDEBUG}
 | |
|             if (p.expectloc=LOC_INVALID) then
 | |
|               Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
 | |
|             if (not allow_multi_pass2) and
 | |
|                (p.location.loc<>LOC_INVALID) then
 | |
|               Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
 | |
|             if (cs_asm_nodes in aktglobalswitches) then
 | |
|               logsecond(p.nodetype,true);
 | |
| {$endif EXTDEBUG}
 | |
|             p.pass_2;
 | |
| {$ifdef EXTDEBUG}
 | |
|             if (cs_asm_nodes in aktglobalswitches) then
 | |
|               logsecond(p.nodetype,false);
 | |
|             if (not codegenerror) then
 | |
|              begin
 | |
|                if (p.location.loc=LOC_INVALID) then
 | |
|                  Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype])
 | |
|                else if (p.location.loc<>p.expectloc) then
 | |
|                  Comment(V_Warning,'Location is different in secondpass: '+nodetype2str[p.nodetype]);
 | |
|              end;
 | |
| {$endif EXTDEBUG}
 | |
|             if codegenerror then
 | |
|               include(p.flags,nf_error);
 | |
|             codegenerror:=codegenerror or oldcodegenerror;
 | |
|             aktlocalswitches:=oldlocalswitches;
 | |
|             aktfilepos:=oldpos;
 | |
|           end
 | |
|          else
 | |
|            codegenerror:=true;
 | |
|       end;
 | |
| 
 | |
| 
 | |
|     function do_secondpass(var p : tnode) : boolean;
 | |
|       begin
 | |
|          codegenerror:=false;
 | |
|          if not(nf_error in p.flags) then
 | |
|            secondpass(p);
 | |
|          do_secondpass:=codegenerror;
 | |
|       end;
 | |
| 
 | |
| 
 | |
| end.
 | |
| {
 | |
|   $Log$
 | |
|   Revision 1.74  2003-11-10 22:02:52  peter
 | |
|     * cross unit inlining fixed
 | |
| 
 | |
|   Revision 1.73  2003/10/30 16:22:40  peter
 | |
|     * call firstpass before allocation and codegeneration is started
 | |
|     * move leftover code from pass_2.generatecode() to psub
 | |
| 
 | |
|   Revision 1.72  2003/10/19 01:34:30  florian
 | |
|     * some ppc stuff fixed
 | |
|     * memory leak fixed
 | |
| 
 | |
|   Revision 1.71  2003/10/18 15:41:26  peter
 | |
|     * made worklists dynamic in size
 | |
| 
 | |
|   Revision 1.70  2003/10/17 15:08:34  peter
 | |
|     * commented out more obsolete constants
 | |
| 
 | |
|   Revision 1.69  2003/10/10 17:48:13  peter
 | |
|     * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
 | |
|     * tregisteralloctor renamed to trgobj
 | |
|     * removed rgobj from a lot of units
 | |
|     * moved location_* and reference_* to cgobj
 | |
|     * first things for mmx register allocation
 | |
| 
 | |
|   Revision 1.68  2003/10/09 21:31:37  daniel
 | |
|     * Register allocator splitted, ans abstract now
 | |
| 
 | |
|   Revision 1.67  2003/10/01 20:34:49  peter
 | |
|     * procinfo unit contains tprocinfo
 | |
|     * cginfo renamed to cgbase
 | |
|     * moved cgmessage to verbose
 | |
|     * fixed ppc and sparc compiles
 | |
| 
 | |
|   Revision 1.66  2003/09/28 21:45:52  peter
 | |
|     * fix register leak in with debug
 | |
| 
 | |
|   Revision 1.65  2003/09/28 17:55:04  peter
 | |
|     * parent framepointer changed to hidden parameter
 | |
|     * tloadparentfpnode added
 | |
| 
 | |
|   Revision 1.64  2003/09/03 15:55:01  peter
 | |
|     * NEWRA branch merged
 | |
| 
 | |
|   Revision 1.63.2.3  2003/08/31 15:46:26  peter
 | |
|     * more updates for tregister
 | |
| 
 | |
|   Revision 1.63.2.2  2003/08/31 13:50:16  daniel
 | |
|     * Remove sorting and use pregenerated indexes
 | |
|     * Some work on making things compile
 | |
| 
 | |
|   Revision 1.63.2.1  2003/08/29 17:28:59  peter
 | |
|     * next batch of updates
 | |
| 
 | |
|   Revision 1.63  2003/08/11 21:18:20  peter
 | |
|     * start of sparc support for newra
 | |
| 
 | |
|   Revision 1.62  2003/08/10 17:25:23  peter
 | |
|     * fixed some reported bugs
 | |
| 
 | |
|   Revision 1.61  2003/07/06 17:58:22  peter
 | |
|     * framepointer fixes for sparc
 | |
|     * parent framepointer code more generic
 | |
| 
 | |
|   Revision 1.60  2003/07/06 15:31:20  daniel
 | |
|     * Fixed register allocator. *Lots* of fixes.
 | |
| 
 | |
|   Revision 1.59  2003/07/06 10:18:47  jonas
 | |
|     * also generate the caller paraloc info of a procedure if it doesn't exist
 | |
|       yet at the start of pass_2
 | |
| 
 | |
|   Revision 1.58  2003/07/05 20:13:03  jonas
 | |
|      * create_paraloc_info() is now called separately for the caller and
 | |
|        callee info
 | |
|      * fixed ppc cycle
 | |
| 
 | |
|   Revision 1.57  2003/06/13 21:19:30  peter
 | |
|     * current_procdef removed, use current_procinfo.procdef instead
 | |
| 
 | |
|   Revision 1.56  2003/06/12 16:43:07  peter
 | |
|     * newra compiles for sparc
 | |
| 
 | |
|   Revision 1.55  2003/06/09 12:23:30  peter
 | |
|     * init/final of procedure data splitted from genentrycode
 | |
|     * use asmnode getposition to insert final at the correct position
 | |
|       als for the implicit try...finally
 | |
| 
 | |
|   Revision 1.54  2003/06/03 13:01:59  daniel
 | |
|     * Register allocator finished
 | |
| 
 | |
|   Revision 1.53  2003/05/26 21:17:17  peter
 | |
|     * procinlinenode removed
 | |
|     * aktexit2label removed, fast exit removed
 | |
|     + tcallnode.inlined_pass_2 added
 | |
| 
 | |
|   Revision 1.52  2003/05/15 18:58:53  peter
 | |
|     * removed selfpointer_offset, vmtpointer_offset
 | |
|     * tvarsym.adjusted_address
 | |
|     * address in localsymtable is now in the real direction
 | |
|     * removed some obsolete globals
 | |
| 
 | |
|   Revision 1.51  2003/05/13 19:14:41  peter
 | |
|     * failn removed
 | |
|     * inherited result code check moven to pexpr
 | |
| 
 | |
|   Revision 1.50  2003/05/09 17:47:02  peter
 | |
|     * self moved to hidden parameter
 | |
|     * removed hdisposen,hnewn,selfn
 | |
| 
 | |
|   Revision 1.49  2003/04/27 11:21:33  peter
 | |
|     * aktprocdef renamed to current_procinfo.procdef
 | |
|     * procinfo renamed to current_procinfo
 | |
|     * procinfo will now be stored in current_module so it can be
 | |
|       cleaned up properly
 | |
|     * gen_main_procsym changed to create_main_proc and release_main_proc
 | |
|       to also generate a tprocinfo structure
 | |
|     * fixed unit implicit initfinal
 | |
| 
 | |
|   Revision 1.48  2003/04/27 07:29:50  peter
 | |
|     * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
 | |
|       a new procdef declaration
 | |
|     * aktprocsym removed
 | |
|     * lexlevel removed, use symtable.symtablelevel instead
 | |
|     * implicit init/final code uses the normal genentry/genexit
 | |
|     * funcret state checking updated for new funcret handling
 | |
| 
 | |
|   Revision 1.47  2003/04/25 20:59:33  peter
 | |
|     * removed funcretn,funcretsym, function result is now in varsym
 | |
|       and aliases for result and function name are added using absolutesym
 | |
|     * vs_hidden parameter for funcret passed in parameter
 | |
|     * vs_hidden fixes
 | |
|     * writenode changed to printnode and released from extdebug
 | |
|     * -vp option added to generate a tree.log with the nodetree
 | |
|     * nicer printnode for statements, callnode
 | |
| 
 | |
|   Revision 1.46  2003/04/23 10:12:14  peter
 | |
|     * allow multi pass2 changed to global boolean instead of node flag
 | |
| 
 | |
|   Revision 1.45  2003/04/22 23:50:23  peter
 | |
|     * firstpass uses expectloc
 | |
|     * checks if there are differences between the expectloc and
 | |
|       location.loc from secondpass in EXTDEBUG
 | |
| 
 | |
|   Revision 1.44  2003/04/22 12:45:58  florian
 | |
|     * fixed generic in operator code
 | |
|     + added debug code to check if all scratch registers are released
 | |
| 
 | |
|   Revision 1.43  2003/01/09 20:40:59  daniel
 | |
|     * Converted some code in cgx86.pas to new register numbering
 | |
| 
 | |
|   Revision 1.42  2003/01/09 15:49:56  daniel
 | |
|     * Added register conversion
 | |
| 
 | |
|   Revision 1.41  2002/12/22 14:35:39  peter
 | |
|     * removed Writeln
 | |
| 
 | |
|   Revision 1.40  2002/12/21 23:21:47  mazen
 | |
|   + added support for the shift nodes
 | |
|   + added debug output on screen with -an command line option
 | |
| 
 | |
|   Revision 1.39  2002/08/23 16:14:49  peter
 | |
|     * tempgen cleanup
 | |
|     * tt_noreuse temp type added that will be used in genentrycode
 | |
| 
 | |
|   Revision 1.38  2002/08/20 16:55:38  peter
 | |
|     * don't write (stabs)line info when inlining a procedure
 | |
| 
 | |
|   Revision 1.37  2002/08/19 19:36:44  peter
 | |
|     * More fixes for cross unit inlining, all tnodes are now implemented
 | |
|     * Moved pocall_internconst to po_internconst because it is not a
 | |
|       calling type at all and it conflicted when inlining of these small
 | |
|       functions was requested
 | |
| 
 | |
|   Revision 1.36  2002/08/18 20:06:24  peter
 | |
|     * inlining is now also allowed in interface
 | |
|     * renamed write/load to ppuwrite/ppuload
 | |
|     * tnode storing in ppu
 | |
|     * nld,ncon,nbas are already updated for storing in ppu
 | |
| 
 | |
|   Revision 1.35  2002/08/17 09:23:38  florian
 | |
|     * first part of procinfo rewrite
 | |
| 
 | |
|   Revision 1.34  2002/08/15 19:10:35  peter
 | |
|     * first things tai,tnode storing in ppu
 | |
| 
 | |
|   Revision 1.33  2002/07/30 20:50:44  florian
 | |
|     * the code generator knows now if parameters are in registers
 | |
| 
 | |
|   Revision 1.32  2002/07/19 11:41:36  daniel
 | |
|   * State tracker work
 | |
|   * The whilen and repeatn are now completely unified into whilerepeatn. This
 | |
|     allows the state tracker to change while nodes automatically into
 | |
|     repeat nodes.
 | |
|   * Resulttypepass improvements to the notn. 'not not a' is optimized away and
 | |
|     'not(a>b)' is optimized into 'a<=b'.
 | |
|   * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
 | |
|     by removing the notn and later switchting the true and falselabels. The
 | |
|     same is done with 'repeat until not a'.
 | |
| 
 | |
|   Revision 1.31  2002/07/01 18:46:25  peter
 | |
|     * internal linker
 | |
|     * reorganized aasm layer
 | |
| 
 | |
|   Revision 1.30  2002/05/18 13:34:11  peter
 | |
|     * readded missing revisions
 | |
| 
 | |
|   Revision 1.29  2002/05/16 19:46:42  carl
 | |
|   + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
 | |
|   + try to fix temp allocation (still in ifdef)
 | |
|   + generic constructor calls
 | |
|   + start of tassembler / tmodulebase class cleanup
 | |
| 
 | |
|   Revision 1.27  2002/05/12 16:53:08  peter
 | |
|     * moved entry and exitcode to ncgutil and cgobj
 | |
|     * foreach gets extra argument for passing local data to the
 | |
|       iterator function
 | |
|     * -CR checks also class typecasts at runtime by changing them
 | |
|       into as
 | |
|     * fixed compiler to cycle with the -CR option
 | |
|     * fixed stabs with elf writer, finally the global variables can
 | |
|       be watched
 | |
|     * removed a lot of routines from cga unit and replaced them by
 | |
|       calls to cgobj
 | |
|     * u32bit-s32bit updates for and,or,xor nodes. When one element is
 | |
|       u32bit then the other is typecasted also to u32bit without giving
 | |
|       a rangecheck warning/error.
 | |
|     * fixed pascal calling method with reversing also the high tree in
 | |
|       the parast, detected by tcalcst3 test
 | |
| 
 | |
|   Revision 1.26  2002/04/21 19:02:04  peter
 | |
|     * removed newn and disposen nodes, the code is now directly
 | |
|       inlined from pexpr
 | |
|     * -an option that will write the secondpass nodes to the .s file, this
 | |
|       requires EXTDEBUG define to actually write the info
 | |
|     * fixed various internal errors and crashes due recent code changes
 | |
| 
 | |
|   Revision 1.25  2002/04/20 21:32:24  carl
 | |
|   + generic FPC_CHECKPOINTER
 | |
|   + first parameter offset in stack now portable
 | |
|   * rename some constants
 | |
|   + move some cpu stuff to other units
 | |
|   - remove unused constents
 | |
|   * fix stacksize for some targets
 | |
|   * fix generic size problems which depend now on EXTEND_SIZE constant
 | |
| 
 | |
|   Revision 1.24  2002/04/07 13:30:13  carl
 | |
|   - removed unused variable
 | |
| 
 | |
|   Revision 1.23  2002/04/02 17:11:29  peter
 | |
|     * tlocation,treference update
 | |
|     * LOC_CONSTANT added for better constant handling
 | |
|     * secondadd splitted in multiple routines
 | |
|     * location_force_reg added for loading a location to a register
 | |
|       of a specified size
 | |
|     * secondassignment parses now first the right and then the left node
 | |
|       (this is compatible with Kylix). This saves a lot of push/pop especially
 | |
|       with string operations
 | |
|     * adapted some routines to use the new cg methods
 | |
| 
 | |
|   Revision 1.22  2002/03/31 20:26:35  jonas
 | |
|     + a_loadfpu_* and a_loadmm_* methods in tcg
 | |
|     * register allocation is now handled by a class and is mostly processor
 | |
|       independent (+rgobj.pas and i386/rgcpu.pas)
 | |
|     * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
 | |
|     * some small improvements and fixes to the optimizer
 | |
|     * some register allocation fixes
 | |
|     * some fpuvaroffset fixes in the unary minus node
 | |
|     * push/popusedregisters is now called rg.save/restoreusedregisters and
 | |
|       (for i386) uses temps instead of push/pop's when using -Op3 (that code is
 | |
|       also better optimizable)
 | |
|     * fixed and optimized register saving/restoring for new/dispose nodes
 | |
|     * LOC_FPU locations now also require their "register" field to be set to
 | |
|       R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
 | |
|     - list field removed of the tnode class because it's not used currently
 | |
|       and can cause hard-to-find bugs
 | |
| 
 | |
| }
 | 
