fpc/compiler/compiler.pas
Jonas Maebe 060d81b8fa Merged revisions 11878,11881-11882,11889,11891-11893,11895,11899-11902,11935,11938,12212,12304,12308-12310,12316,12330-12332,12334,12339-12340 via svnmerge from
svn+ssh://jonas@svn.freepascal.org/FPC/svn/fpc/branches/wpo

........
r11878 | jonas | 2008-10-11 02:25:18 +0200 (Sat, 11 Oct 2008) | 19 lines

  + initial implementation of whole-program optimisation framework
  + implementation of whole-program devirtualisation
  o use:
     a) generate whole-program optimisation information (no need
        to completely compile the program and all of its units
        with -OW/-FW, only the main program is sufficient)
      fpc -OWdevirtcalls -FWmyprog.wpo myprog
     b) use it to optimise the program
      fpc -B -Owdevirtcalls -Fwmyprog.wpo myprog
     (the -B is not required, but only sources recompiled during
      the second pass will actually be optimised -- if you want,
      you can even rebuild the rtl devirtualised for a particular 
      program; and these options can obviously also be used         
      together with regular optimisation switches)
  o warning:
    - there are no checks yet to ensure that you do not use 
      units optimised for a particular program with another
      program (or with a changed version of the same program)

........
r11881 | jonas | 2008-10-11 19:35:52 +0200 (Sat, 11 Oct 2008) | 13 lines

  * extracted code to detect constructed class/object types from
    tcallnode.gen_vmt_tree into its own method to avoid clutter
  * detect x.classtype.create constructs (with classtype = the
    system.tobject.classtype method), and treat them as if a
    "class of x" has been instantiated rather than a
    "class of tobject". this required storing the instantiated
    classrefs in their own array though, because at such a
    point we don't have a "class of x" tdef available (so
    now "x", and all other defs instantiated via a classref,
    are now stored as tobjectdefs in a separate array)
  + support for devirtualising class methods (including
    constructors)

........
r11882 | jonas | 2008-10-11 20:44:02 +0200 (Sat, 11 Oct 2008) | 7 lines

  + -Owoptvmts whole program optimisation which replaces vmt entries
    with method names of child classes in case the current class'
    method can never be called (e.g., because this class is never
    instantiated). As a result, such methods can then be removed
    by dead code removal/smart linking (not much effect for either
    the compiler, lazarus or a trivial lazarus app though).

........
r11889 | jonas | 2008-10-12 14:29:54 +0200 (Sun, 12 Oct 2008) | 2 lines

  * some comment fixes

........
r11891 | jonas | 2008-10-12 18:49:13 +0200 (Sun, 12 Oct 2008) | 4 lines

  * fixed twpofilereader.getnextnoncommentline() when reusing a previously
    read line
  * fixed skipping of unnecessary wpo feedback file sections

........
r11892 | jonas | 2008-10-12 23:42:43 +0200 (Sun, 12 Oct 2008) | 31 lines

  + symbol liveness wpo information extracted from smartlinked programs
    (-OW/-Owsymbolliveness)
  + use symbol liveness information to improve devirtualisation (don't
    consider classes created in code that has been dead code stripped).
    This requires at least two passes of using wpo (first uses dead code
    info to locate classes that are constructed only in dead code,
    second pass uses this info to potentially further devirtualise).
    I.e.:
     1) generate initial liveness and devirtualisation feedback
       fpc -FWtt.wpo -OWall tt.pp -Xs- -CX -XX
     2) use previously generated feedback, and regenerate new feedback
        based on this (i.e., disregard classes created in dead code)
       fpc -FWtt-1.wpo -OWall -Fwtt.wo -Owall tt.pp -Xs- -CX -XX
     3) use the newly generated feedback (in theory, it is possible
        that even more opportunities pop up afterwards; you can
        continue until the program does not get smaller anymore)
       fpc -Fwtt-1.wpo -Owall tt.pp -CX -XX
  * changed all message() to cgmessage() calls so the set codegenerror
  * changed static fsectionhandlers field to a regular field called
    fwpocomponents
  * changed registration of wpocomponents: no longer happens in the
    initialization section of their unit, but in the InitWpo routine
    (which has been moved from the woinfo to the wpo unit). This way
    you can register different classes based on the target/parameters.
  + added static method to twpocomponentbase for checking whether
    the command line parameters don't conflict with the requested
    optimisations (e.g. generating liveness info requires that
    smartlinking is turned on)
  + added static method to twpocomponentbase to request the
    section name

........
r11893 | jonas | 2008-10-12 23:53:57 +0200 (Sun, 12 Oct 2008) | 3 lines

  * fixed comment error (twpodeadcodeinfo keeps a list of live,
    not dead symbols)

........
r11895 | jonas | 2008-10-13 00:13:59 +0200 (Mon, 13 Oct 2008) | 2 lines

  + documented -OW<x>, -Ow<x>, -FW<x> and -Fw<x> wpo parameters

........
r11899 | jonas | 2008-10-14 22:14:56 +0200 (Tue, 14 Oct 2008) | 2 lines

  * replaced hardcoded string with objdumpsearchstr constant

........
r11900 | jonas | 2008-10-14 22:15:25 +0200 (Tue, 14 Oct 2008) | 2 lines

  * reset wpofeedbackinput and wpofeedbackoutput in wpodone

........
r11901 | jonas | 2008-10-14 22:16:07 +0200 (Tue, 14 Oct 2008) | 2 lines

  * various additional comments and comment fixes

........
r11902 | jonas | 2008-10-15 18:09:42 +0200 (Wed, 15 Oct 2008) | 5 lines

  * store vmt procdefs in the ppu files so we don't have to use a hack to
    regenerate them for whole-program optimisation
  * fixed crash when performing devirtualisation optimisation on programs
    that do not construct any classes/objects with optimisable vmts

........
r11935 | jonas | 2008-10-19 12:24:26 +0200 (Sun, 19 Oct 2008) | 4 lines

  * set the vmt entries of non-class virtual methods of not instantiated
    objects/classes to FPC_ABSTRACTERROR so the code they refer to can
    be thrown away if it is not referred to in any other way either

........
r11938 | jonas | 2008-10-19 20:55:02 +0200 (Sun, 19 Oct 2008) | 7 lines

  * record all classrefdefs/objdefs for which a loadvmtaddrnode is generated,
    and instead of marking all classes that derive from instantiated
    classrefdefs as instantiated, only mark those classes from the above
    collection that derive from instantiated classrefdefs as
    instantiated (since to instantiate a class, you have to load its vmt
    somehow -- this may be broken by using assembler code though)

........
r12212 | jonas | 2008-11-23 12:26:34 +0100 (Sun, 23 Nov 2008) | 3 lines

  * fixed to work with the new vmtentries that are always available and
    removed previously added code to save/load vmtentries to ppu files

........
r12304 | jonas | 2008-12-05 22:23:30 +0100 (Fri, 05 Dec 2008) | 4 lines

  * check whether the correct wpo feedback file is used in the current
    compilation when using units that were compiled using wpo information
    during a previous compilation run

........
r12308 | jonas | 2008-12-06 18:03:39 +0100 (Sat, 06 Dec 2008) | 2 lines

  * abort compilation if an error occurred during wpo initialisation

........
r12309 | jonas | 2008-12-06 18:04:28 +0100 (Sat, 06 Dec 2008) | 3 lines

  * give an error message instead of crashing with an io exception if the
    compiler is unable to create the wpo feedback file specified using -FW

........
r12310 | jonas | 2008-12-06 18:12:43 +0100 (Sat, 06 Dec 2008) | 3 lines

  * don't let the used wpo feedback file influence the interface crc (there's
    a separate check for such changes)

........
r12316 | jonas | 2008-12-08 19:08:25 +0100 (Mon, 08 Dec 2008) | 3 lines

  * document the format of the sections of the wpo feedback file inside the
    feedback file itself

........
r12330 | jonas | 2008-12-10 22:26:47 +0100 (Wed, 10 Dec 2008) | 2 lines

  * use sysutils instead of dos to avoid command line length limits

........
r12331 | jonas | 2008-12-10 22:31:11 +0100 (Wed, 10 Dec 2008) | 3 lines

  + support for testing whole program optimisation tests (multiple
    compilations using successively generated feedback files)

........
r12332 | jonas | 2008-12-10 22:31:40 +0100 (Wed, 10 Dec 2008) | 2 lines

  + whole program optimisation tests

........
r12334 | jonas | 2008-12-10 22:38:07 +0100 (Wed, 10 Dec 2008) | 2 lines

  - removed unused local variable

........
r12339 | jonas | 2008-12-11 18:06:36 +0100 (Thu, 11 Dec 2008) | 2 lines

  + comments for newly added fields to tobjectdef for devirtualisation

........
r12340 | jonas | 2008-12-11 18:10:01 +0100 (Thu, 11 Dec 2008) | 2 lines

  * increase ppu version (was no longer different from trunk due to merging)

........

git-svn-id: trunk@12341 -
2008-12-11 17:40:18 +00:00

338 lines
8.3 KiB
ObjectPascal

{
This unit is the interface of the compiler which can be used by
external programs to link in the compiler
Copyright (c) 1998-2005 by Florian Klaempfl
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 compiler;
{$i fpcdefs.inc}
interface
uses
{$ifdef GO32V2}
emu387,
{$endif GO32V2}
{$ifdef WATCOM}
emu387,
{$endif WATCOM}
{$IFNDEF USE_FAKE_SYSUTILS}
sysutils,math,
{$ELSE}
fksysutl,
{$ENDIF}
verbose,comphook,systems,
cutils,cfileutl,cclasses,globals,options,fmodule,parser,symtable,
assemble,link,dbgbase,import,export,tokens,pass_1,wpobase,wpo
{ cpu parameter handling }
,cpupara
{ procinfo stuff }
,cpupi
{ cpu codegenerator }
,cgcpu
{$ifndef NOPASS2}
,cpunode
{$endif}
{ cpu targets }
,cputarg
{ system information for source system }
{ the information about the target os }
{ are pulled in by the t_* units }
{$ifdef amiga}
,i_amiga
{$endif amiga}
{$ifdef atari}
,i_atari
{$endif atari}
{$ifdef beos}
,i_beos
{$endif beos}
{$ifdef fbsd}
,i_fbsd
{$endif fbsd}
{$ifdef gba}
,i_gba
{$endif gba}
{$ifdef go32v2}
,i_go32v2
{$endif go32v2}
{$ifdef linux}
,i_linux
{$endif linux}
{$ifdef macos}
,i_macos
{$endif macos}
{$ifdef nds}
,i_nds
{$endif nds}
{$ifdef nwm}
,i_nwm
{$endif nwm}
{$ifdef nwl}
,i_nwl
{$endif nwm}
{$ifdef os2}
{$ifdef emx}
,i_emx
{$else emx}
,i_os2
{$endif emx}
{$endif os2}
{$ifdef palmos}
,i_palmos
{$endif palmos}
{$ifdef solaris}
,i_sunos
{$endif solaris}
{$ifdef wdosx}
,i_wdosx
{$endif wdosx}
{$ifdef win32}
,i_win
{$endif win32}
{$ifdef symbian}
,i_symbian
{$endif symbian}
,globtype;
function Compile(const cmd:string):longint;
implementation
uses
aasmcpu;
{$if defined(EXTDEBUG) or defined(MEMDEBUG)}
{$define SHOWUSEDMEM}
{$endif}
var
CompilerInitedAfterArgs,
CompilerInited : boolean;
{****************************************************************************
Compiler
****************************************************************************}
procedure DoneCompiler;
begin
if not CompilerInited then
exit;
{ Free compiler if args are read }
if CompilerInitedAfterArgs then
begin
CompilerInitedAfterArgs:=false;
DoneParser;
DoneImport;
DoneExport;
DoneLinker;
DoneAsm;
DoneWpo;
end;
{ Free memory for the others }
CompilerInited:=false;
do_doneSymbolInfo;
DoneSymtable;
DoneGlobals;
DoneFileUtils;
donetokens;
end;
procedure InitCompiler(const cmd:string);
begin
if CompilerInited then
DoneCompiler;
{ inits which need to be done before the arguments are parsed }
InitSystems;
{ fileutils depends on source_info so it must be after systems }
InitFileUtils;
{ globals depends on source_info so it must be after systems }
InitGlobals;
{ verbose depends on exe_path and must be after globals }
InitVerbose;
inittokens;
IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
do_initSymbolInfo;
CompilerInited:=true;
{ this is needed here for the IDE
in case of compilation failure
at the previous compile }
set_current_module(nil);
{ read the arguments }
read_arguments(cmd);
{ inits which depend on arguments }
InitParser;
InitImport;
InitExport;
InitLinker;
InitAsm;
InitWpo;
CompilerInitedAfterArgs:=true;
end;
function Compile(const cmd:string):longint;
{$maxfpuregisters 0}
procedure writepathlist(w:longint;l:TSearchPathList);
var
hp : TCmdStrListItem;
begin
hp:=TCmdStrListItem(l.first);
while assigned(hp) do
begin
Message1(w,hp.str);
hp:=TCmdStrListItem(hp.next);
end;
end;
var
timestr : string[20];
linkstr : string[64];
{$ifdef SHOWUSEDMEM}
hstatus : TFPCHeapStatus;
{$endif SHOWUSEDMEM}
ExceptionMask : TFPUExceptionMask;
totaltime : real;
begin
try
try
ExceptionMask:=GetExceptionMask;
SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
exOverflow, exUnderflow, exPrecision]);
starttime:=getrealtime;
{ Initialize the compiler }
InitCompiler(cmd);
{ show some info }
Message1(general_t_compilername,FixFileName(system.paramstr(0)));
Message1(general_d_sourceos,source_info.name);
Message1(general_i_targetos,target_info.name);
Message1(general_t_exepath,exepath);
WritePathList(general_t_unitpath,unitsearchpath);
WritePathList(general_t_includepath,includesearchpath);
WritePathList(general_t_librarypath,librarysearchpath);
WritePathList(general_t_objectpath,objectsearchpath);
{ Compile the program }
{$ifdef PREPROCWRITE}
if parapreprocess then
parser.preprocess(inputfilepath+inputfilename)
else
{$endif PREPROCWRITE}
parser.compile(inputfilepath+inputfilename);
{ Show statistics }
if status.errorcount=0 then
begin
totaltime:=getrealtime-starttime;
if totaltime<0 then
totaltime:=totaltime+3600.0*24.0;
timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
if status.codesize<>aword(-1) then
linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
else
linkstr:='';
Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
if (Status.Verbosity and V_Warning = V_Warning) and
(Status.CountWarnings <> 0) then
Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
if (Status.Verbosity and V_Hint = V_Hint) and
(Status.CountHints <> 0) then
Message1 (general_i_number_of_hints, tostr (Status.CountHints));
if (Status.Verbosity and V_Note = V_Note) and
(Status.CountNotes <> 0) then
Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
end;
finally
{ no message possible after this !! }
DoneCompiler;
SetExceptionMask(ExceptionMask);
end;
DoneVerbose;
except
on EControlCAbort do
begin
try
{ in case of 50 errors, this could cause another exception,
suppress this exception
}
Message(general_f_compilation_aborted);
except
on ECompilerAbort do
;
end;
DoneVerbose;
end;
on ECompilerAbort do
begin
try
{ in case of 50 errors, this could cause another exception,
suppress this exception
}
Message(general_f_compilation_aborted);
except
on ECompilerAbort do
;
end;
DoneVerbose;
end;
on ECompilerAbortSilent do
begin
DoneVerbose;
end;
on Exception do
begin
{ General catchall, normally not used }
try
{ in case of 50 errors, this could cause another exception,
suppress this exception
}
Message(general_f_compilation_aborted);
except
on ECompilerAbort do
;
end;
DoneVerbose;
Raise;
end;
end;
{$ifdef SHOWUSEDMEM}
hstatus:=GetFPCHeapStatus;
Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
{$endif SHOWUSEDMEM}
{ Set the return value if an error has occurred }
if status.errorcount=0 then
result:=0
else
result:=1;
end;
end.