summaryrefslogtreecommitdiff
path: root/sources/scalac/PhaseDescriptor.java
blob: 4bdb89ae924eaca8bbb1924cf270442213396225 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import scalac.util.Debug;

/**
 * Information about a compiler phase.
 */
public final class PhaseDescriptor {

    //########################################################################
    // Public Constants

    public static final int STOP  = 0x0001;
    public static final int SKIP  = 0x0002;
    public static final int PRINT = 0x0004;
    public static final int GRAPH = 0x0008;
    public static final int CHECK = 0x0010;
    public static final int LOG   = 0x0020;

    //########################################################################
    // Public Functions

    /** Freezes the given phases (patches flags and assigns ids). */
    public static void freeze(PhaseDescriptor[] phases) {
        // there are at least two phases: INITIAL and TERMINAL
        assert phases.length >= 2;
        if (phases.length == 0) return;
        // propagate STOP and SKIP
        for (int i = 0; i < phases.length - 1; i++) {
            phases[i].flags |= (phases[i + 1].flags >>> 16) & (STOP | SKIP);
            phases[i+1].flags &= ~((STOP | SKIP) << 16);
        }
        // transform STOP flags into SKIP flags
        boolean stop = false;
        for (int i = 0; i < phases.length; i++) {
            if (stop) phases[i].flags |= SKIP;
            stop |= phases[i].hasStopFlag();
            if (stop) phases[i].flags &= ~STOP;
        }
        // remove SKIP flag on INITIAL and TERMINAL phases
        phases[0].flags &= ~SKIP;
        phases[phases.length - 1].flags &= ~SKIP;
        // propagate other flags and freeze remaining phases
        PhaseDescriptor last = null;
        for (int i = 0; i < phases.length; i++) {
            phases[i].id = i;
            if (phases[i].hasSkipFlag()) continue;
            if (last != null) last.flags |= phases[i].flags >>> 16;
            phases[i].flags &= 0x0000FFFF;
            last = phases[i];
        }
    }

    //########################################################################
    // Private Fields

    /** The phase name */
    private final String name;
    /** The phase description */
    private final String description;
    /** The phase task description */
    private final String task;
    /** The phase implementation class */
    private final Class clasz;

    /** The flags of this phase */
    private int flags;
    /** The phase instance */
    private Phase phase;
    /** The phase identifier */
    private int id = -1;

    //########################################################################
    // Public Constructors

    /** Initializes this instance. */
    public PhaseDescriptor(String name, String description, String task,
        Class clasz)
    {
        this.name = name;
        this.description = description;
        this.task = task;
        this.clasz = clasz;
    }

    //########################################################################
    // Public Methods

    /** Returns the one-word name of this phase. */
    public String name() {
        return name;
    }

    /** Returns a one-line description of this phase. */
    public String description() {
        return description;
    }

    /** Returns a one-line task description of this phase. */
    public String taskDescription() {
        return task;
    }

    /** Creates the object implementing this phase. */
    public Phase create(Global global) {
        assert id >= 0 : "phase " + name + " not yet frozen";
        assert phase == null : "phase " + name + " already created";
        try {
            Class[] params = { Global.class, PhaseDescriptor.class };
            Object[] args = { global, this };
            Constructor constructor = clasz.getConstructor(params);
            return phase = (Phase)constructor.newInstance(args);
        } catch (NoSuchMethodException exception) {
            throw Debug.abort(exception);
        } catch (IllegalAccessException exception) {
            throw Debug.abort(exception);
        } catch (InstantiationException exception) {
            throw Debug.abort(exception);
        } catch (InvocationTargetException exception) {
            throw Debug.abort(exception);
        }
    }

    /** Returns the object implementing this phase. */
    public Phase phase() {
        assert phase != null : "phase " + name + " not yet created";
        return phase;
    }

    /** Returns the identifier of this phase. */
    public int id() {
        assert id >= 0 : "phase " + name + " not yet frozen";
        return id;
    }

    /** Adds the given flag (to previous phase if prev is true). */
    public void addFlag(int flag, boolean prev) {
        assert id < 0 : "phase " + name + " already frozen";
        flags |= flag << (prev ? 16 : 0);
    }

    /** Adds the SKIP flag. */
    public void addSkipFlag() {
        flags |= SKIP;
    }

    /** Adds the CHECK flag. */
    public void addCheckFlag() {
        flags |= CHECK;
    }

    /** Adds the PRINT flag. */
    public void addPrintFlag() {
        flags |= PRINT;
    }

    /** Adds the GRAPH flag. */
    public void addGraphFlag() {
        flags |= GRAPH;
    }

    /** Adds the STOP flag. */
    public void addStopFlag() {
        flags |= STOP;
    }

    /** Adds the LOG flag. */
    public void addLogFlag() {
        flags |= LOG;
    }

    /** Has this phase the SKIP flag? */
    public boolean hasSkipFlag() {
        return (flags & SKIP) != 0;
    }

    /** Has this phase the CHECK flag? */
    public boolean hasCheckFlag() {
        return (flags & CHECK) != 0;
    }

    /** Has this phase the PRINT flag? */
    public boolean hasPrintFlag() {
        return (flags & PRINT) != 0;
    }

    /** Has this phase the GRAPH flag? */
    public boolean hasGraphFlag() {
        return (flags & GRAPH) != 0;
    }

    /** Has this phase the STOP flag? */
    public boolean hasStopFlag() {
        return (flags & STOP) != 0;
    }

    /** Has this phase the LOG flag? */
    public boolean hasLogFlag() {
        return (flags & LOG) != 0;
    }

    /** Returns the name of this phase. */
    public String toString() {
        return name();
    }

    //########################################################################
}