| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
| 6 | 6 |
| 7 #include "src/v8.h" | 7 #include "src/v8.h" |
| 8 | 8 |
| 9 #include "src/wasm/ast-decoder.h" | 9 #include "src/wasm/ast-decoder.h" |
| 10 #include "src/wasm/encoder.h" | 10 #include "src/wasm/encoder.h" |
| 11 | 11 |
| 12 #include "test/cctest/wasm/test-signatures.h" |
| 13 |
| 12 namespace v8 { | 14 namespace v8 { |
| 13 namespace internal { | 15 namespace internal { |
| 14 namespace wasm { | 16 namespace wasm { |
| 15 | 17 |
| 16 class EncoderTest : public TestWithZone { | 18 class EncoderTest : public TestWithZone { |
| 17 protected: | 19 protected: |
| 18 void AddLocal(WasmFunctionBuilder* f, LocalType type) { | 20 void AddLocal(WasmFunctionBuilder* f, LocalType type) { |
| 19 uint16_t index = f->AddLocal(type); | 21 uint16_t index = f->AddLocal(type); |
| 20 f->EmitGetLocal(index); | 22 f->EmitGetLocal(index); |
| 21 } | 23 } |
| 22 }; | 24 }; |
| 23 | 25 |
| 24 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { | |
| 25 base::AccountingAllocator allocator; | |
| 26 Zone zone(&allocator); | |
| 27 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 28 uint16_t f_index = builder->AddFunction(); | |
| 29 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | |
| 30 uint16_t local_f32 = function->AddLocal(kAstF32); | |
| 31 uint16_t param_float32 = function->AddParam(kAstF32); | |
| 32 uint16_t local_i32 = function->AddLocal(kAstI32); | |
| 33 uint16_t local_f64 = function->AddLocal(kAstF64); | |
| 34 uint16_t local_i64 = function->AddLocal(kAstI64); | |
| 35 uint16_t param_int32 = function->AddParam(kAstI32); | |
| 36 uint16_t local_i32_2 = function->AddLocal(kAstI32); | |
| 37 | |
| 38 byte code[] = {kExprGetLocal, static_cast<uint8_t>(param_float32)}; | |
| 39 uint32_t local_indices[] = {1}; | |
| 40 function->EmitCode(code, sizeof(code), local_indices, 1); | |
| 41 code[1] = static_cast<uint8_t>(param_int32); | |
| 42 function->EmitCode(code, sizeof(code), local_indices, 1); | |
| 43 code[1] = static_cast<uint8_t>(local_i32); | |
| 44 function->EmitCode(code, sizeof(code), local_indices, 1); | |
| 45 code[1] = static_cast<uint8_t>(local_i32_2); | |
| 46 function->EmitCode(code, sizeof(code), local_indices, 1); | |
| 47 code[1] = static_cast<uint8_t>(local_i64); | |
| 48 function->EmitCode(code, sizeof(code), local_indices, 1); | |
| 49 code[1] = static_cast<uint8_t>(local_f32); | |
| 50 function->EmitCode(code, sizeof(code), local_indices, 1); | |
| 51 code[1] = static_cast<uint8_t>(local_f64); | |
| 52 function->EmitCode(code, sizeof(code), local_indices, 1); | |
| 53 | |
| 54 WasmFunctionEncoder* f = function->Build(&zone, builder); | |
| 55 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); | |
| 56 byte* buffer = &buffer_vector[0]; | |
| 57 byte* header = buffer; | |
| 58 byte* body = buffer + f->HeaderSize(); | |
| 59 f->Serialize(buffer, &header, &body); | |
| 60 } | |
| 61 | |
| 62 TEST_F(EncoderTest, Function_Builder_Indexing_Variable_Width) { | |
| 63 base::AccountingAllocator allocator; | |
| 64 Zone zone(&allocator); | |
| 65 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 66 uint16_t f_index = builder->AddFunction(); | |
| 67 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | |
| 68 for (size_t i = 0; i < 128; i++) { | |
| 69 AddLocal(function, kAstF32); | |
| 70 } | |
| 71 AddLocal(function, kAstI32); | |
| 72 | |
| 73 WasmFunctionEncoder* f = function->Build(&zone, builder); | |
| 74 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); | |
| 75 byte* buffer = &buffer_vector[0]; | |
| 76 byte* header = buffer; | |
| 77 byte* body = buffer + f->HeaderSize(); | |
| 78 f->Serialize(buffer, &header, &body); | |
| 79 body = buffer + f->HeaderSize(); | |
| 80 } | |
| 81 | |
| 82 TEST_F(EncoderTest, Function_Builder_Block_Variable_Width) { | |
| 83 base::AccountingAllocator allocator; | |
| 84 Zone zone(&allocator); | |
| 85 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 86 uint16_t f_index = builder->AddFunction(); | |
| 87 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | |
| 88 function->EmitWithVarInt(kExprBlock, 200); | |
| 89 for (int i = 0; i < 200; ++i) { | |
| 90 function->Emit(kExprNop); | |
| 91 } | |
| 92 | |
| 93 WasmFunctionEncoder* f = function->Build(&zone, builder); | |
| 94 CHECK_EQ(f->BodySize(), 204); | |
| 95 } | |
| 96 | |
| 97 TEST_F(EncoderTest, Function_Builder_EmitEditableVarIntImmediate) { | |
| 98 base::AccountingAllocator allocator; | |
| 99 Zone zone(&allocator); | |
| 100 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 101 uint16_t f_index = builder->AddFunction(); | |
| 102 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | |
| 103 function->Emit(kExprLoop); | |
| 104 uint32_t offset = function->EmitEditableVarIntImmediate(); | |
| 105 for (int i = 0; i < 200; ++i) { | |
| 106 function->Emit(kExprNop); | |
| 107 } | |
| 108 function->EditVarIntImmediate(offset, 200); | |
| 109 | |
| 110 WasmFunctionEncoder* f = function->Build(&zone, builder); | |
| 111 CHECK_EQ(f->BodySize(), 204); | |
| 112 } | |
| 113 | |
| 114 TEST_F(EncoderTest, Function_Builder_EmitEditableVarIntImmediate_Locals) { | |
| 115 base::AccountingAllocator allocator; | |
| 116 Zone zone(&allocator); | |
| 117 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | |
| 118 uint16_t f_index = builder->AddFunction(); | |
| 119 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | |
| 120 function->Emit(kExprBlock); | |
| 121 uint32_t offset = function->EmitEditableVarIntImmediate(); | |
| 122 for (int i = 0; i < 200; ++i) { | |
| 123 AddLocal(function, kAstI32); | |
| 124 } | |
| 125 function->EditVarIntImmediate(offset, 200); | |
| 126 | |
| 127 WasmFunctionEncoder* f = function->Build(&zone, builder); | |
| 128 ZoneVector<uint8_t> buffer_vector(f->HeaderSize() + f->BodySize(), &zone); | |
| 129 byte* buffer = &buffer_vector[0]; | |
| 130 byte* header = buffer; | |
| 131 byte* body = buffer + f->HeaderSize(); | |
| 132 f->Serialize(buffer, &header, &body); | |
| 133 body = buffer + f->HeaderSize(); | |
| 134 | |
| 135 CHECK_EQ(f->BodySize(), 479); | |
| 136 const uint8_t varint200_low = (200 & 0x7f) | 0x80; | |
| 137 const uint8_t varint200_high = (200 >> 7) & 0x7f; | |
| 138 offset = 0; | |
| 139 CHECK_EQ(body[offset++], 1); // Local decl count. | |
| 140 CHECK_EQ(body[offset++], varint200_low); | |
| 141 CHECK_EQ(body[offset++], varint200_high); | |
| 142 CHECK_EQ(body[offset++], kLocalI32); | |
| 143 CHECK_EQ(body[offset++], kExprBlock); | |
| 144 CHECK_EQ(body[offset++], varint200_low); | |
| 145 CHECK_EQ(body[offset++], varint200_high); | |
| 146 // GetLocal with one-byte indices. | |
| 147 for (int i = 0; i <= 127; ++i) { | |
| 148 CHECK_EQ(body[offset++], kExprGetLocal); | |
| 149 CHECK_EQ(body[offset++], i); | |
| 150 } | |
| 151 // GetLocal with two-byte indices. | |
| 152 for (int i = 128; i < 200; ++i) { | |
| 153 CHECK_EQ(body[offset++], kExprGetLocal); | |
| 154 CHECK_EQ(body[offset++], (i & 0x7f) | 0x80); | |
| 155 CHECK_EQ(body[offset++], (i >> 7) & 0x7f); | |
| 156 } | |
| 157 CHECK_EQ(offset, 479); | |
| 158 } | |
| 159 } // namespace wasm | 26 } // namespace wasm |
| 160 } // namespace internal | 27 } // namespace internal |
| 161 } // namespace v8 | 28 } // namespace v8 |
| OLD | NEW |