summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/backend/icode/Opcodes.scala
blob: bb2b0c39ebd89cf79fa5885b94b76af93a6274d2 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* NSC -- new scala compiler
 * Copyright 2005 LAMP/EPFL
 * @author  Martin Odersky
 */

// $Id$


package scala.tools.nsc.backend.icode;

import scala.tools.nsc.ast._;
import scala.tools.nsc.util.Position;

/*
  A pattern match

  case THIS(clasz) =>
  case CONSTANT(const) =>
  case LOAD_ARRAY_ITEM(kind) =>
  case LOAD_LOCAL(local, isArg) =>
  case LOAD_FIELD(field, isStatic) =>
  case LOAD_MODULE(module) =>
  case STORE_ARRAY_ITEM(kind) =>
  case STORE_LOCAL(local, isArg) =>
  case STORE_FIELD(field, isStatic) =>
  case CALL_PRIMITIVE(primitive) =>
  case CALL_METHOD(method, style) =>
  case NEW(kind) =>
  case CREATE_ARRAY(elem) =>
  case IS_INSTANCE(tpe) =>
  case CHECK_CAST(tpe) =>
  case SWITCH(tags, labels) =>
  case JUMP(where) =>
  case CJUMP(success, failure, cond, kind) =>
  case CZJUMP(success, failure, cond, kind) =>
  case RETURN(kind) =>
  case THROW() =>
  case DROP(kind) =>
  case DUP(kind) =>
  case MONITOR_ENTER() =>
  case MONITOR_EXIT() =>
*/


/**
 * The ICode intermediate representation. It is a stack-based
 * representation, very close to the JVM and .NET. It uses the
 * erased types of Scala and references Symbols to refer named entities
 * in the source files.
 */
[_trait_] abstract class Opcodes: ICodes {
  import global.{Symbol, NoSymbol, Type, Name, Constant};

  /** This class represents an instruction of the intermediate code.
   *  Each case subclass will represent a specific operation.
   */
  abstract class Instruction {

    /** This abstract method returns the number of used elements on the stack */
    def consumed : Int = 0;

    /** This abstract method returns the number of produced elements on the stack */
    def produced : Int = 0;

    /** This method returns the difference of size of the stack when the instruction is used */
    def difference = produced-consumed;

    /** The corresponding position in the source file */
    var pos: Int = Position.NOPOS;
  }

  object opcodes {

    /** Loads the "this" references on top of the stack.
     * Stack: ...
     *    ->: ...:ref
     */
    case class THIS(clasz: Symbol) extends Instruction {
      /** Returns a string representation of this constant */
      override def toString(): String = "THIS";

      override def consumed = 0;
      override def produced = 1;
    }

    /** Loads a constant on the stack.
     * Stack: ...
     *    ->: ...:constant
     */
    case class CONSTANT(constant: Constant) extends Instruction{
      /** Returns a string representation of this constant */
      override def toString(): String = "CONSTANT ("+constant.toString()+")";

      override def consumed = 0;
      override def produced = 1;
    }

    /** Loads an element of an array. The array and the index should
     * be on top of the stack.
     * Stack: ...:array[a](Ref):index(Int)
     *    ->: ...:element(a)
     */
    case class LOAD_ARRAY_ITEM(kind: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String = "LOAD_ARRAY_ITEM (" + kind + ")";

      override def consumed = 2;
      override def produced = 1;
    }

    /** Load a local variable on the stack. It can be a method argument.
     * Stack: ...
     *    ->: ...:value
     */
    case class LOAD_LOCAL(local: Local, isArgument: boolean) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String = "LOAD_LOCAL "+local.toString(); //+isArgument?" (argument)":"";

      override def consumed = 0;
      override def produced = 1;
    }

    /** Load a field on the stack. The object to which it refers should be
     * on the stack.
     * Stack: ...:ref
     *    ->: ...:value
     */
    case class LOAD_FIELD(field: Symbol, isStatic: boolean) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String =
        "LOAD_FIELD " + (if (isStatic) field.fullNameString else field.toString());

      override def consumed = 1;
      override def produced = 1;
    }

    case class LOAD_MODULE(module: Symbol) extends Instruction {
      assert(module != NoSymbol,
             "Invalid module symbol");
      /** Returns a string representation of this instruction */
      override def toString(): String =
        "LOAD_MODULE " + module.toString();

      override def consumed = 0;
      override def produced = 1;
    }

    /** Store a value into an array at a specified index.
     * Stack: ...:array[a](Ref):index(Int):value(a)
     *    ->: ...
     */
    case class STORE_ARRAY_ITEM(kind: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String = "STORE_ARRAY_ITEM (" + kind + ")";

      override def consumed = 3;
      override def produced = 0;
    }

    /** Store a value into a local variable. It can be an argument.
     * Stack: ...:value
     *    ->: ...
     */
    case class STORE_LOCAL(local: Local, isArgument: boolean) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String = "STORE_LOCAL "+local.toString(); //+isArgument?" (argument)":"";

      override def consumed = 1;
      override def produced = 0;
    }

    /** Store a value into a field.
     * Stack: ...:ref:value
     *    ->: ...
     */
    case class STORE_FIELD(field: Symbol, isStatic: boolean) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String = "STORE_FIELD "+field.toString(); //+isStatic?" (static)":"";

      override def consumed = 2;
      override def produced = 0;
    }

    /** Call a primitive function.
     * Stack: ...:arg1:arg2:...:argn
     *    ->: ...:result
     */
    case class CALL_PRIMITIVE(primitive: Primitive) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="CALL_PRIMITIVE "+primitive.toString();

      override def consumed = primitive match {
        case Negation(_) => 1;
        case Test(_,_,true) => 1;
        case Test(_,_,false) => 2;
        case Comparison(_,_) => 2;
        case Arithmetic(_,_) => 2;
        case Logical(_,_) => 2;
        case Shift(_,_) => 2;
        case Conversion(_,_) => 1;
        case ArrayLength(_) => 1;
        case StringConcat(_) => 2;
        case StartConcat => 0;
        case EndConcat => 1;
      }
      override def produced = 1;
    }

    /** This class represents a CALL_METHOD instruction
     * STYLE: dynamic / static(StaticInstance)
     * Stack: ...:ref:arg1:arg2:...:argn
     *    ->: ...:result
     *
     * STYLE: static(StaticClass)
     * Stack: ...:arg1:arg2:...:argn
     *    ->: ...:result
     *
     */
    case class CALL_METHOD(method: Symbol, style: InvokeStyle) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String =
        "CALL_METHOD " + method.fullNameString +" ("+style.toString()+")";

      override def consumed = {
        var result = method.tpe.paramTypes.length;
        result = result + (style match {
          case Dynamic => 1
          case Static(true) => 1
          case _ => 0
        });

        result;
      }
      override def produced = 1;
    }

    /** Create a new instance of a class through the specified constructor
     * Stack: ...:arg1:arg2:...:argn
     *    ->: ...:ref
     */
    case class NEW(kind: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String = "NEW "+ kind;

      override def consumed = 0;
      override def produced = 1;
    }


    /** This class represents a CREATE_ARRAY instruction
     * Stack: ...:size(int)
     *    ->: ...:arrayref
     */
    case class CREATE_ARRAY(element: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="CREATE_ARRAY "+element.toString();

      override def consumed = 1;
      override def produced = 1;
    }

    /** This class represents a IS_INSTANCE instruction
     * Stack: ...:ref
     *    ->: ...:result(boolean)
     */
    case class IS_INSTANCE(typ: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="IS_INSTANCE "+typ.toString();

      override def consumed = 1;
      override def produced = 1;
    }

    /** This class represents a CHECK_CAST instruction
     * Stack: ...:ref(oldtype)
     *    ->: ...:ref(typ <=: oldtype)
     */
    case class CHECK_CAST(typ: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="CHECK_CAST "+typ.toString();

      override def consumed = 1;
      override def produced = 1;
    }

    /** This class represents a SWITCH instruction
     * Stack: ...:index(int)
     *    ->: ...:
     *
     * The tags array contains one entry per label, each entry consisting of
     * an array of ints, any of which will trigger the jump to the corresponding label.
     * labels should contain an extra label, which is the 'default' jump.
     */
    case class SWITCH(tags: List[List[Int]], labels: List[BasicBlock]) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="SWITCH ...";

      override def consumed = 1;
      override def produced = 0;
    }

    /** This class represents a JUMP instruction
     * Stack: ...
     *    ->: ...
     */
    case class JUMP(where: BasicBlock) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="JUMP "+where.label;

      override def consumed = 0;
      override def produced = 0;
    }

    /** This class represents a CJUMP instruction
     * It compares the two values on the stack with the 'cond' test operator
     * Stack: ...:value1:value2
     *    ->: ...
     */
    case class CJUMP(successBlock: BasicBlock,
		     failureBlock: BasicBlock,
		     cond: TestOp,
                     kind: TypeKind) extends Instruction
    {

      /** Returns a string representation of this instruction */
      override def toString(): String = (
        "CJUMP (" + kind + ")" +
        cond.toString()+" ? "+successBlock.label+" : "+failureBlock.label
      );

      override def consumed = 2;
      override def produced = 0;
    }

    /** This class represents a CZJUMP instruction
     * It compares the one value on the stack and zero with the 'cond' test operator
     * Stack: ...:value:
     *    ->: ...
     */
    case class CZJUMP(successBlock: BasicBlock,
                      failureBlock: BasicBlock,
                      cond: TestOp,
                      kind: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String = (
        "CZJUMP (" + kind + ")" +
        cond.toString()+" ? "+successBlock.label+" : "+failureBlock.label
      );

      override def consumed = 1;
      override def produced = 0;
    }


    /** This class represents a RETURN instruction
     * Stack: ...
     *    ->: ...
     */
    case class RETURN(kind: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="RETURN (" + kind + ")";

      override def consumed = 0;
      override def produced = 0;
    }

    /** This class represents a THROW instruction
     * Stack: ...:Throwable(Ref)
     *    ->: ...:
     */
    case class THROW() extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="THROW";

      override def consumed = 1;
      override def produced = 0;
    }

    /** This class represents a DROP instruction
     * Stack: ...:something
     *    ->: ...
     */
    case class DROP (typ: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="DROP "+typ.toString();

      override def consumed = 1;
      override def produced = 0;
    }

    /** This class represents a DUP instruction
     * Stack: ...:something
     *    ->: ...:something:something
     */
    case class DUP (typ: TypeKind) extends Instruction {
      /** Returns a string representation of this instruction */
      override def toString(): String ="DUP";

      override def consumed = 1;
      override def produced = 2;
    }

    /** This class represents a MONITOR_ENTER instruction
     * Stack: ...:object(ref)
     *    ->: ...:
     */
    case class MONITOR_ENTER() extends Instruction {

      /** Returns a string representation of this instruction */
      override def toString(): String ="MONITOR_ENTER";

      override def consumed = 1;
      override def produced = 0;
    }

    /** This class represents a MONITOR_EXIT instruction
     * Stack: ...:object(ref)
     *    ->: ...:
     */
    case class MONITOR_EXIT() extends Instruction {

      /** Returns a string representation of this instruction */
      override def toString(): String ="MONITOR_EXIT";

      override def consumed = 1;
      override def produced = 0;
    }

    /** This class represents a method invocation style. */
    trait InvokeStyle {

      /** Is this a dynamic method call? */
      def isDynamic: Boolean = this match {
        case Dynamic =>  true;
        case _       => false;
      }

      /** Is this a static method call? */
      def isStatic: Boolean = this match {
        case Static(_) => true;
        case _ =>  false;
      }

      /** Is this an instance method call? */
      def hasInstance: Boolean = this match {
        case Dynamic => true;
        case Static(onInstance) => onInstance;
        case SuperCall(_) => true;
        case _ => false;
      }

      /** Returns a string representation of this style. */
      override def toString(): String = this match {
        case Dynamic =>  "dynamic";
        case Static(false) => "static-class";
        case Static(true) =>  "static-instance";
        case SuperCall(mix) => "super(" + mix + ")";
      }
    }

    case object Dynamic extends InvokeStyle;

    /**
     * Special invoke. Static(true) is used for calls to private
     * members.
     */
    case class Static(onInstance: Boolean) extends InvokeStyle;

    /** Call through super[mix]. */
    case class SuperCall(mix: Name) extends InvokeStyle;

  }
}