translation-array.cc 10.4 KB
Newer Older
1 2 3 4 5 6
// Copyright 2021 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.

#include "src/deoptimizer/translation-array.h"

7
#include "src/base/vlq.h"
8
#include "src/deoptimizer/translated-state.h"
9
#include "src/objects/fixed-array-inl.h"
10
#include "third_party/zlib/google/compression_utils_portable.h"
11 12 13 14

namespace v8 {
namespace internal {

15 16 17 18 19 20 21 22 23 24 25 26
namespace {

// Constants describing compressed TranslationArray layout. Only relevant if
// --turbo-compress-translation-arrays is enabled.
constexpr int kUncompressedSizeOffset = 0;
constexpr int kUncompressedSizeSize = kInt32Size;
constexpr int kCompressedDataOffset =
    kUncompressedSizeOffset + kUncompressedSizeSize;
constexpr int kTranslationArrayElementSize = kInt32Size;

}  // namespace

27 28 29
TranslationArrayIterator::TranslationArrayIterator(TranslationArray buffer,
                                                   int index)
    : buffer_(buffer), index_(index) {
30 31 32 33 34 35
  if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
    const int size = buffer_.get_int(kUncompressedSizeOffset);
    uncompressed_contents_.insert(uncompressed_contents_.begin(), size, 0);

    uLongf uncompressed_size = size * kTranslationArrayElementSize;

36 37 38 39 40 41 42
    CHECK_EQ(zlib_internal::UncompressHelper(
                 zlib_internal::ZRAW,
                 base::bit_cast<Bytef*>(uncompressed_contents_.data()),
                 &uncompressed_size,
                 buffer_.GetDataStartAddress() + kCompressedDataOffset,
                 buffer_.DataSize()),
             Z_OK);
43 44 45 46
    DCHECK(index >= 0 && index < size);
  } else {
    DCHECK(index >= 0 && index < buffer.length());
  }
47 48 49
}

int32_t TranslationArrayIterator::Next() {
50 51 52
  if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
    return uncompressed_contents_[index_++];
  } else {
53 54 55
    int32_t value = base::VLQDecode(buffer_.GetDataStartAddress(), &index_);
    DCHECK_LE(index_, buffer_.length());
    return value;
56 57 58 59
  }
}

bool TranslationArrayIterator::HasNext() const {
60 61 62 63 64
  if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
    return index_ < static_cast<int>(uncompressed_contents_.size());
  } else {
    return index_ < buffer_.length();
  }
65 66
}

67
void TranslationArrayBuilder::Add(int32_t value) {
68 69 70
  if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
    contents_for_compression_.push_back(value);
  } else {
71
    base::VLQEncode(&contents_, value);
72
  }
73 74 75 76
}

Handle<TranslationArray> TranslationArrayBuilder::ToTranslationArray(
    Factory* factory) {
77 78 79 80 81 82 83 84 85
  if (V8_UNLIKELY(FLAG_turbo_compress_translation_arrays)) {
    const int input_size = SizeInBytes();
    uLongf compressed_data_size = compressBound(input_size);

    ZoneVector<byte> compressed_data(compressed_data_size, zone());

    CHECK_EQ(
        zlib_internal::CompressHelper(
            zlib_internal::ZRAW, compressed_data.data(), &compressed_data_size,
86
            base::bit_cast<const Bytef*>(contents_for_compression_.data()),
87 88
            input_size, Z_DEFAULT_COMPRESSION, nullptr, nullptr),
        Z_OK);
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    const int translation_array_size =
        static_cast<int>(compressed_data_size) + kUncompressedSizeSize;
    Handle<TranslationArray> result =
        factory->NewByteArray(translation_array_size, AllocationType::kOld);

    result->set_int(kUncompressedSizeOffset, Size());
    std::memcpy(result->GetDataStartAddress() + kCompressedDataOffset,
                compressed_data.data(), compressed_data_size);

    return result;
  } else {
    Handle<TranslationArray> result =
        factory->NewByteArray(SizeInBytes(), AllocationType::kOld);
    memcpy(result->GetDataStartAddress(), contents_.data(),
           contents_.size() * sizeof(uint8_t));
    return result;
  }
107 108 109 110 111 112 113 114 115 116 117 118
}

void TranslationArrayBuilder::BeginBuiltinContinuationFrame(
    BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
  auto opcode = TranslationOpcode::BUILTIN_CONTINUATION_FRAME;
  Add(opcode);
  Add(bytecode_offset.ToInt());
  Add(literal_id);
  Add(height);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}

119
#if V8_ENABLE_WEBASSEMBLY
120 121
void TranslationArrayBuilder::BeginJSToWasmBuiltinContinuationFrame(
    BytecodeOffset bytecode_offset, int literal_id, unsigned height,
122
    base::Optional<wasm::ValueKind> return_kind) {
123 124 125 126 127
  auto opcode = TranslationOpcode::JS_TO_WASM_BUILTIN_CONTINUATION_FRAME;
  Add(opcode);
  Add(bytecode_offset.ToInt());
  Add(literal_id);
  Add(height);
128
  Add(return_kind ? static_cast<int>(return_kind.value()) : kNoWasmReturnKind);
129 130
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 4);
}
131
#endif  // V8_ENABLE_WEBASSEMBLY
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
void TranslationArrayBuilder::BeginJavaScriptBuiltinContinuationFrame(
    BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
  auto opcode = TranslationOpcode::JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME;
  Add(opcode);
  Add(bytecode_offset.ToInt());
  Add(literal_id);
  Add(height);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}

void TranslationArrayBuilder::BeginJavaScriptBuiltinContinuationWithCatchFrame(
    BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
  auto opcode =
      TranslationOpcode::JAVA_SCRIPT_BUILTIN_CONTINUATION_WITH_CATCH_FRAME;
  Add(opcode);
  Add(bytecode_offset.ToInt());
  Add(literal_id);
  Add(height);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}

void TranslationArrayBuilder::BeginConstructStubFrame(
    BytecodeOffset bytecode_offset, int literal_id, unsigned height) {
  auto opcode = TranslationOpcode::CONSTRUCT_STUB_FRAME;
  Add(opcode);
  Add(bytecode_offset.ToInt());
  Add(literal_id);
  Add(height);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 3);
}

164
void TranslationArrayBuilder::BeginInlinedExtraArguments(int literal_id,
165
                                                         unsigned height) {
166
  auto opcode = TranslationOpcode::INLINED_EXTRA_ARGUMENTS;
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
  Add(opcode);
  Add(literal_id);
  Add(height);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 2);
}

void TranslationArrayBuilder::BeginInterpretedFrame(
    BytecodeOffset bytecode_offset, int literal_id, unsigned height,
    int return_value_offset, int return_value_count) {
  auto opcode = TranslationOpcode::INTERPRETED_FRAME;
  Add(opcode);
  Add(bytecode_offset.ToInt());
  Add(literal_id);
  Add(height);
  Add(return_value_offset);
  Add(return_value_count);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 5);
}

void TranslationArrayBuilder::ArgumentsElements(CreateArgumentsType type) {
  auto opcode = TranslationOpcode::ARGUMENTS_ELEMENTS;
  Add(opcode);
  Add(static_cast<uint8_t>(type));
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::ArgumentsLength() {
  auto opcode = TranslationOpcode::ARGUMENTS_LENGTH;
  Add(opcode);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 0);
}

void TranslationArrayBuilder::BeginCapturedObject(int length) {
  auto opcode = TranslationOpcode::CAPTURED_OBJECT;
  Add(opcode);
  Add(length);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::DuplicateObject(int object_index) {
  auto opcode = TranslationOpcode::DUPLICATED_OBJECT;
  Add(opcode);
  Add(object_index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreRegister(Register reg) {
  auto opcode = TranslationOpcode::REGISTER;
  Add(opcode);
  Add(reg.code());
}

void TranslationArrayBuilder::StoreInt32Register(Register reg) {
  auto opcode = TranslationOpcode::INT32_REGISTER;
  Add(opcode);
  Add(reg.code());
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreInt64Register(Register reg) {
  auto opcode = TranslationOpcode::INT64_REGISTER;
  Add(opcode);
  Add(reg.code());
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreUint32Register(Register reg) {
  auto opcode = TranslationOpcode::UINT32_REGISTER;
  Add(opcode);
  Add(reg.code());
}

void TranslationArrayBuilder::StoreBoolRegister(Register reg) {
  auto opcode = TranslationOpcode::BOOL_REGISTER;
  Add(opcode);
  Add(reg.code());
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreFloatRegister(FloatRegister reg) {
  auto opcode = TranslationOpcode::FLOAT_REGISTER;
  Add(opcode);
  Add(reg.code());
}

void TranslationArrayBuilder::StoreDoubleRegister(DoubleRegister reg) {
  auto opcode = TranslationOpcode::DOUBLE_REGISTER;
  Add(opcode);
  Add(reg.code());
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreStackSlot(int index) {
  auto opcode = TranslationOpcode::STACK_SLOT;
  Add(opcode);
  Add(index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreInt32StackSlot(int index) {
  auto opcode = TranslationOpcode::INT32_STACK_SLOT;
  Add(opcode);
  Add(index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreInt64StackSlot(int index) {
  auto opcode = TranslationOpcode::INT64_STACK_SLOT;
  Add(opcode);
  Add(index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreUint32StackSlot(int index) {
  auto opcode = TranslationOpcode::UINT32_STACK_SLOT;
  Add(opcode);
  Add(index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreBoolStackSlot(int index) {
  auto opcode = TranslationOpcode::BOOL_STACK_SLOT;
  Add(opcode);
  Add(index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreFloatStackSlot(int index) {
  auto opcode = TranslationOpcode::FLOAT_STACK_SLOT;
  Add(opcode);
  Add(index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreDoubleStackSlot(int index) {
  auto opcode = TranslationOpcode::DOUBLE_STACK_SLOT;
  Add(opcode);
  Add(index);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::StoreLiteral(int literal_id) {
  auto opcode = TranslationOpcode::LITERAL;
  Add(opcode);
  Add(literal_id);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 1);
}

void TranslationArrayBuilder::AddUpdateFeedback(int vector_literal, int slot) {
  auto opcode = TranslationOpcode::UPDATE_FEEDBACK;
  Add(opcode);
  Add(vector_literal);
  Add(slot);
  DCHECK_EQ(TranslationOpcodeOperandCount(opcode), 2);
}

void TranslationArrayBuilder::StoreJSFrameFunction() {
  StoreStackSlot((StandardFrameConstants::kCallerPCOffset -
                  StandardFrameConstants::kFunctionOffset) /
                 kSystemPointerSize);
}

329 330
}  // namespace internal
}  // namespace v8