summaryrefslogtreecommitdiffstats
path: root/js/src/jit/KnownClass.cpp
blob: 46a59912eb8e1cac49ad8be2c4a8993191071b67 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/KnownClass.h"

#include "jit/MIR.h"
#include "vm/ArrayObject.h"
#include "vm/Iteration.h"
#include "vm/JSFunction.h"
#include "vm/PlainObject.h"  // js::PlainObject
#include "vm/RegExpObject.h"

using namespace js;
using namespace js::jit;

KnownClass jit::GetObjectKnownClass(const MDefinition* def) {
  MOZ_ASSERT(def->type() == MIRType::Object);

  switch (def->op()) {
    case MDefinition::Opcode::NewArray:
    case MDefinition::Opcode::NewArrayDynamicLength:
      return KnownClass::Array;

    case MDefinition::Opcode::NewObject:
    case MDefinition::Opcode::CreateThis:
    case MDefinition::Opcode::CreateThisWithTemplate:
      return KnownClass::PlainObject;

    case MDefinition::Opcode::Lambda:
    case MDefinition::Opcode::LambdaArrow:
    case MDefinition::Opcode::FunctionWithProto:
      return KnownClass::Function;

    case MDefinition::Opcode::RegExp:
      return KnownClass::RegExp;

    case MDefinition::Opcode::NewIterator:
      switch (def->toNewIterator()->type()) {
        case MNewIterator::ArrayIterator:
          return KnownClass::ArrayIterator;
        case MNewIterator::StringIterator:
          return KnownClass::StringIterator;
        case MNewIterator::RegExpStringIterator:
          return KnownClass::RegExpStringIterator;
      }
      MOZ_CRASH("unreachable");

    case MDefinition::Opcode::Phi: {
      if (def->numOperands() == 0) {
        return KnownClass::None;
      }

      MDefinition* op = def->getOperand(0);
      // Check for Phis to avoid recursion for now.
      if (op->isPhi()) {
        return KnownClass::None;
      }

      KnownClass known = GetObjectKnownClass(op);
      if (known == KnownClass::None) {
        return KnownClass::None;
      }

      for (size_t i = 1; i < def->numOperands(); i++) {
        op = def->getOperand(i);
        if (op->isPhi() || GetObjectKnownClass(op) != known) {
          return KnownClass::None;
        }
      }

      return known;
    }

    default:
      break;
  }

  return KnownClass::None;
}

const JSClass* jit::GetObjectKnownJSClass(const MDefinition* def) {
  switch (GetObjectKnownClass(def)) {
    case KnownClass::PlainObject:
      return &PlainObject::class_;
    case KnownClass::Array:
      return &ArrayObject::class_;
    case KnownClass::Function:
      return &JSFunction::class_;
    case KnownClass::RegExp:
      return &RegExpObject::class_;
    case KnownClass::ArrayIterator:
      return &ArrayIteratorObject::class_;
    case KnownClass::StringIterator:
      return &StringIteratorObject::class_;
    case KnownClass::RegExpStringIterator:
      return &RegExpStringIteratorObject::class_;
    case KnownClass::None:
      break;
  }

  return nullptr;
}