string-constants.cc 5.53 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/codegen/string-constants.h"
6 7

#include "src/base/functional.h"
8
#include "src/numbers/dtoa.h"
9
#include "src/objects/objects.h"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include "src/objects/string-inl.h"

namespace v8 {
namespace internal {

Handle<String> StringConstantBase::AllocateStringConstant(
    Isolate* isolate) const {
  if (!flattened_.is_null()) {
    return flattened_;
  }

  Handle<String> result;
  switch (kind()) {
    case StringConstantKind::kStringLiteral: {
      result = static_cast<const StringLiteral*>(this)->str();
25
      CHECK(!result.is_null());
26 27 28 29 30 31 32
      break;
    }
    case StringConstantKind::kNumberToStringConstant: {
      auto num_constant = static_cast<const NumberToStringConstant*>(this);
      Handle<Object> num_obj =
          isolate->factory()->NewNumber(num_constant->num());
      result = isolate->factory()->NumberToString(num_obj);
33
      CHECK(!result.is_null());
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
      break;
    }
    case StringConstantKind::kStringCons: {
      Handle<String> lhs =
          static_cast<const StringCons*>(this)->lhs()->AllocateStringConstant(
              isolate);
      Handle<String> rhs =
          static_cast<const StringCons*>(this)->rhs()->AllocateStringConstant(
              isolate);
      result = isolate->factory()->NewConsString(lhs, rhs).ToHandleChecked();
      break;
    }
  }

  // TODO(mslekova): Normally we'd want to flatten the string here
  // but that results in OOM for too long strings.
  Memoize(result);
  return flattened_;
}

bool StringConstantBase::operator==(const StringConstantBase& other) const {
  if (kind() != other.kind()) return false;

  switch (kind()) {
    case StringConstantKind::kStringLiteral: {
      return static_cast<const StringLiteral*>(this) ==
             static_cast<const StringLiteral*>(&other);
    }
    case StringConstantKind::kNumberToStringConstant: {
      return static_cast<const NumberToStringConstant*>(this) ==
             static_cast<const NumberToStringConstant*>(&other);
    }
    case StringConstantKind::kStringCons: {
      return static_cast<const StringCons*>(this) ==
             static_cast<const StringCons*>(&other);
    }
  }
  UNREACHABLE();
}

size_t hash_value(StringConstantBase const& base) {
  switch (base.kind()) {
    case StringConstantKind::kStringLiteral: {
      return hash_value(*static_cast<const StringLiteral*>(&base));
    }
    case StringConstantKind::kNumberToStringConstant: {
      return hash_value(*static_cast<const NumberToStringConstant*>(&base));
    }
    case StringConstantKind::kStringCons: {
      return hash_value(*static_cast<const StringCons*>(&base));
    }
  }
  UNREACHABLE();
}

bool operator==(StringLiteral const& lhs, StringLiteral const& rhs) {
  return lhs.str().address() == rhs.str().address();
}

bool operator!=(StringLiteral const& lhs, StringLiteral const& rhs) {
  return !(lhs == rhs);
}

size_t hash_value(StringLiteral const& p) {
  return base::hash_combine(p.str().address());
}

std::ostream& operator<<(std::ostream& os, StringLiteral const& p) {
  return os << Brief(*p.str());
}

bool operator==(NumberToStringConstant const& lhs,
                NumberToStringConstant const& rhs) {
  return lhs.num() == rhs.num();
}

bool operator!=(NumberToStringConstant const& lhs,
                NumberToStringConstant const& rhs) {
  return !(lhs == rhs);
}

size_t hash_value(NumberToStringConstant const& p) {
  return base::hash_combine(p.num());
}

std::ostream& operator<<(std::ostream& os, NumberToStringConstant const& p) {
  return os << p.num();
}

bool operator==(StringCons const& lhs, StringCons const& rhs) {
  // TODO(mslekova): Think if we can express this in a more readable manner
  return *(lhs.lhs()) == *(rhs.lhs()) && *(lhs.rhs()) == *(rhs.rhs());
}

bool operator!=(StringCons const& lhs, StringCons const& rhs) {
  return !(lhs == rhs);
}

size_t hash_value(StringCons const& p) {
  return base::hash_combine(*(p.lhs()), *(p.rhs()));
}

std::ostream& operator<<(std::ostream& os, const StringConstantBase* base) {
  os << "DelayedStringConstant: ";
  switch (base->kind()) {
    case StringConstantKind::kStringLiteral: {
      os << *static_cast<const StringLiteral*>(base);
      break;
    }
    case StringConstantKind::kNumberToStringConstant: {
      os << *static_cast<const NumberToStringConstant*>(base);
      break;
    }
    case StringConstantKind::kStringCons: {
      os << *static_cast<const StringCons*>(base);
      break;
    }
  }
  return os;
}

std::ostream& operator<<(std::ostream& os, StringCons const& p) {
  return os << p.lhs() << ", " << p.rhs();
}

size_t StringConstantBase::GetMaxStringConstantLength() const {
  switch (kind()) {
    case StringConstantKind::kStringLiteral: {
      return static_cast<const StringLiteral*>(this)
          ->GetMaxStringConstantLength();
    }
    case StringConstantKind::kNumberToStringConstant: {
      return static_cast<const NumberToStringConstant*>(this)
          ->GetMaxStringConstantLength();
    }
    case StringConstantKind::kStringCons: {
      return static_cast<const StringCons*>(this)->GetMaxStringConstantLength();
    }
  }
  UNREACHABLE();
}

176
size_t StringLiteral::GetMaxStringConstantLength() const { return length_; }
177 178 179 180 181 182 183 184 185 186 187 188

size_t NumberToStringConstant::GetMaxStringConstantLength() const {
  return kBase10MaximalLength + 1;
}

size_t StringCons::GetMaxStringConstantLength() const {
  return lhs()->GetMaxStringConstantLength() +
         rhs()->GetMaxStringConstantLength();
}

}  // namespace internal
}  // namespace v8