summary refs log tree commit diff stats
path: root/nim/passaux.pas
blob: 7898d827800f13cb0c4596771d043c4fc6ab2a62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
//
//           The Nimrod Compiler
//        (c) Copyright 2008 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit passaux;

// implements some little helper passes
{$include 'config.inc'}

interface

uses
  nsystem, strutils, ast, astalgo, passes, msgs, options;

function verbosePass: TPass;
function cleanupPass: TPass;

implementation

function verboseOpen(s: PSym; const filename: string): PPassContext;
begin
  //MessageOut('compiling ' + s.name.s);
  result := nil; // we don't need a context
  if gVerbosity > 0 then 
    rawMessage(hintProcessing, s.name.s);
end;

function verboseProcess(context: PPassContext; n: PNode): PNode;
begin
  result := n;
  if context <> nil then InternalError('logpass: context is not nil');
  if gVerbosity = 3 then
    liMessage(n.info, hintProcessing, toString(ast.gid));
end;

function verbosePass: TPass;
begin
  initPass(result);
  result.open := verboseOpen;
  result.process := verboseProcess;
end;

function cleanUp(c: PPassContext; n: PNode): PNode;
var
  i: int;
  s: PSym;
begin
  result := n;
  // we cannot clean up if dead code elimination is activated
  if (optDeadCodeElim in gGlobalOptions) then exit;
  case n.kind of
    nkStmtList: begin
      for i := 0 to sonsLen(n)-1 do {@discard} cleanup(c, n.sons[i]);
    end;
    nkProcDef, nkMethodDef: begin
      if (n.sons[namePos].kind = nkSym) then begin
        s := n.sons[namePos].sym;
        if not (sfDeadCodeElim in getModule(s).flags) and
           not astNeeded(s) then s.ast.sons[codePos] := nil; // free the memory
      end
    end
    else begin end;
  end
end;

function cleanupPass: TPass;
begin
  initPass(result);
  result.process := cleanUp;
  result.close := cleanUp;
end;

end.