HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Oreo
|
8.0.0_r4
下载
查看原文件
收藏
根目录
external
v8
src
wasm
wasm-macro-gen.h
// Copyright 2015 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. #ifndef V8_WASM_MACRO_GEN_H_ #define V8_WASM_MACRO_GEN_H_ #include "src/wasm/wasm-opcodes.h" #include "src/zone/zone-containers.h" #define U32_LE(v) \ static_cast
(v), static_cast
((v) >> 8), \ static_cast
((v) >> 16), static_cast
((v) >> 24) #define U16_LE(v) static_cast
(v), static_cast
((v) >> 8) #define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion) #define IMPORT_SIG_INDEX(v) U32V_1(v) #define FUNC_INDEX(v) U32V_1(v) #define TABLE_INDEX(v) U32V_1(v) #define NO_NAME U32V_1(0) #define NAME_LENGTH(v) U32V_1(v) #define ENTRY_COUNT(v) U32V_1(v) #define ZERO_ALIGNMENT 0 #define ZERO_OFFSET 0 #define BR_TARGET(v) U32V_1(v) #define MASK_7 ((1 << 7) - 1) #define MASK_14 ((1 << 14) - 1) #define MASK_21 ((1 << 21) - 1) #define MASK_28 ((1 << 28) - 1) #define U32V_1(x) static_cast
((x)&MASK_7) #define U32V_2(x) \ static_cast
(((x)&MASK_7) | 0x80), static_cast
(((x) >> 7) & MASK_7) #define U32V_3(x) \ static_cast
((((x)) & MASK_7) | 0x80), \ static_cast
((((x) >> 7) & MASK_7) | 0x80), \ static_cast
(((x) >> 14) & MASK_7) #define U32V_4(x) \ static_cast
(((x)&MASK_7) | 0x80), \ static_cast
((((x) >> 7) & MASK_7) | 0x80), \ static_cast
((((x) >> 14) & MASK_7) | 0x80), \ static_cast
(((x) >> 21) & MASK_7) #define U32V_5(x) \ static_cast
(((x)&MASK_7) | 0x80), \ static_cast
((((x) >> 7) & MASK_7) | 0x80), \ static_cast
((((x) >> 14) & MASK_7) | 0x80), \ static_cast
((((x) >> 21) & MASK_7) | 0x80), \ static_cast
((((x) >> 28) & MASK_7)) // Convenience macros for building Wasm bytecode directly into a byte array. //------------------------------------------------------------------------------ // Control. //------------------------------------------------------------------------------ #define WASM_NOP kExprNop #define ARITY_0 0 #define ARITY_1 1 #define ARITY_2 2 #define DEPTH_0 0 #define DEPTH_1 1 #define DEPTH_2 2 #define ARITY_2 2 #define WASM_BLOCK(...) kExprBlock, kLocalVoid, __VA_ARGS__, kExprEnd #define WASM_BLOCK_T(t, ...) \ kExprBlock, static_cast
(WasmOpcodes::LocalTypeCodeFor(t)), \ __VA_ARGS__, kExprEnd #define WASM_BLOCK_TT(t1, t2, ...) \ kExprBlock, kMultivalBlock, 0, \ static_cast
(WasmOpcodes::LocalTypeCodeFor(t1)), \ static_cast
(WasmOpcodes::LocalTypeCodeFor(t2)), __VA_ARGS__, \ kExprEnd #define WASM_BLOCK_I(...) kExprBlock, kLocalI32, __VA_ARGS__, kExprEnd #define WASM_BLOCK_L(...) kExprBlock, kLocalI64, __VA_ARGS__, kExprEnd #define WASM_BLOCK_F(...) kExprBlock, kLocalF32, __VA_ARGS__, kExprEnd #define WASM_BLOCK_D(...) kExprBlock, kLocalF64, __VA_ARGS__, kExprEnd #define WASM_INFINITE_LOOP kExprLoop, kLocalVoid, kExprBr, DEPTH_0, kExprEnd #define WASM_LOOP(...) kExprLoop, kLocalVoid, __VA_ARGS__, kExprEnd #define WASM_LOOP_I(...) kExprLoop, kLocalI32, __VA_ARGS__, kExprEnd #define WASM_LOOP_L(...) kExprLoop, kLocalI64, __VA_ARGS__, kExprEnd #define WASM_LOOP_F(...) kExprLoop, kLocalF32, __VA_ARGS__, kExprEnd #define WASM_LOOP_D(...) kExprLoop, kLocalF64, __VA_ARGS__, kExprEnd #define WASM_IF(cond, tstmt) cond, kExprIf, kLocalVoid, tstmt, kExprEnd #define WASM_IF_ELSE(cond, tstmt, fstmt) \ cond, kExprIf, kLocalVoid, tstmt, kExprElse, fstmt, kExprEnd #define WASM_IF_ELSE_T(t, cond, tstmt, fstmt) \ cond, kExprIf, static_cast
(WasmOpcodes::LocalTypeCodeFor(t)), tstmt, \ kExprElse, fstmt, kExprEnd #define WASM_IF_ELSE_TT(t1, t2, cond, tstmt, fstmt) \ cond, kExprIf, kMultivalBlock, 0, \ static_cast
(WasmOpcodes::LocalTypeCodeFor(t1)), \ static_cast
(WasmOpcodes::LocalTypeCodeFor(t2)), tstmt, kExprElse, \ fstmt, kExprEnd #define WASM_IF_ELSE_I(cond, tstmt, fstmt) \ cond, kExprIf, kLocalI32, tstmt, kExprElse, fstmt, kExprEnd #define WASM_IF_ELSE_L(cond, tstmt, fstmt) \ cond, kExprIf, kLocalI64, tstmt, kExprElse, fstmt, kExprEnd #define WASM_IF_ELSE_F(cond, tstmt, fstmt) \ cond, kExprIf, kLocalF32, tstmt, kExprElse, fstmt, kExprEnd #define WASM_IF_ELSE_D(cond, tstmt, fstmt) \ cond, kExprIf, kLocalF64, tstmt, kExprElse, fstmt, kExprEnd #define WASM_SELECT(tval, fval, cond) tval, fval, cond, kExprSelect #define WASM_RETURN0 kExprReturn #define WASM_RETURN1(val) val, kExprReturn #define WASM_RETURNN(count, ...) __VA_ARGS__, kExprReturn #define WASM_BR(depth) kExprBr, static_cast
(depth) #define WASM_BR_IF(depth, cond) cond, kExprBrIf, static_cast
(depth) #define WASM_BR_IFD(depth, val, cond) \ val, cond, kExprBrIf, static_cast
(depth), kExprDrop #define WASM_CONTINUE(depth) kExprBr, static_cast
(depth) #define WASM_UNREACHABLE kExprUnreachable #define WASM_BR_TABLE(key, count, ...) \ key, kExprBrTable, U32V_1(count), __VA_ARGS__ #define WASM_CASE(x) static_cast
(x), static_cast
(x >> 8) #define WASM_CASE_BR(x) static_cast
(x), static_cast
(0x80 | (x) >> 8) //------------------------------------------------------------------------------ // Misc expressions. //------------------------------------------------------------------------------ #define WASM_ID(...) __VA_ARGS__ #define WASM_ZERO kExprI8Const, 0 #define WASM_ONE kExprI8Const, 1 #define WASM_I8(val) kExprI8Const, static_cast
(val) #define I32V_MIN(length) -(1 << (6 + (7 * ((length) - 1)))) #define I32V_MAX(length) ((1 << (6 + (7 * ((length) - 1)))) - 1) #define I64V_MIN(length) -(1LL << (6 + (7 * ((length) - 1)))) #define I64V_MAX(length) ((1LL << (6 + 7 * ((length) - 1))) - 1) #define I32V_IN_RANGE(value, length) \ ((value) >= I32V_MIN(length) && (value) <= I32V_MAX(length)) #define I64V_IN_RANGE(value, length) \ ((value) >= I64V_MIN(length) && (value) <= I64V_MAX(length)) #define WASM_NO_LOCALS 0 namespace v8 { namespace internal { namespace wasm { inline void CheckI32v(int32_t value, int length) { DCHECK(length >= 1 && length <= 5); DCHECK(length == 5 || I32V_IN_RANGE(value, length)); } inline void CheckI64v(int64_t value, int length) { DCHECK(length >= 1 && length <= 10); DCHECK(length == 10 || I64V_IN_RANGE(value, length)); } // A helper for encoding local declarations prepended to the body of a // function. // TODO(titzer): move this to an appropriate header. class LocalDeclEncoder { public: explicit LocalDeclEncoder(Zone* zone, FunctionSig* s = nullptr) : sig(s), local_decls(zone), total(0) {} // Prepend local declarations by creating a new buffer and copying data // over. The new buffer must be delete[]'d by the caller. void Prepend(Zone* zone, const byte** start, const byte** end) const { size_t size = (*end - *start); byte* buffer = reinterpret_cast
(zone->New(Size() + size)); size_t pos = Emit(buffer); memcpy(buffer + pos, *start, size); pos += size; *start = buffer; *end = buffer + pos; } size_t Emit(byte* buffer) const { size_t pos = 0; pos = WriteUint32v(buffer, pos, static_cast
(local_decls.size())); for (size_t i = 0; i < local_decls.size(); ++i) { pos = WriteUint32v(buffer, pos, local_decls[i].first); buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second); } DCHECK_EQ(Size(), pos); return pos; } // Add locals declarations to this helper. Return the index of the newly added // local(s), with an optional adjustment for the parameters. uint32_t AddLocals(uint32_t count, LocalType type) { uint32_t result = static_cast
(total + (sig ? sig->parameter_count() : 0)); total += count; if (local_decls.size() > 0 && local_decls.back().second == type) { count += local_decls.back().first; local_decls.pop_back(); } local_decls.push_back(std::pair
(count, type)); return result; } size_t Size() const { size_t size = SizeofUint32v(static_cast
(local_decls.size())); for (auto p : local_decls) size += 1 + SizeofUint32v(p.first); return size; } bool has_sig() const { return sig != nullptr; } FunctionSig* get_sig() const { return sig; } void set_sig(FunctionSig* s) { sig = s; } private: FunctionSig* sig; ZoneVector
> local_decls; size_t total; size_t SizeofUint32v(uint32_t val) const { size_t size = 1; while (true) { byte b = val & MASK_7; if (b == val) return size; size++; val = val >> 7; } } // TODO(titzer): lift encoding of u32v to a common place. size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const { while (true) { byte b = val & MASK_7; if (b == val) { buffer[pos++] = b; break; } buffer[pos++] = 0x80 | b; val = val >> 7; } return pos; } }; } // namespace wasm } // namespace internal } // namespace v8 //------------------------------------------------------------------------------ // Int32 Const operations //------------------------------------------------------------------------------ #define WASM_I32V(val) kExprI32Const, U32V_5(val) #define WASM_I32V_1(val) \ static_cast
(CheckI32v((val), 1), kExprI32Const), U32V_1(val) #define WASM_I32V_2(val) \ static_cast
(CheckI32v((val), 2), kExprI32Const), U32V_2(val) #define WASM_I32V_3(val) \ static_cast
(CheckI32v((val), 3), kExprI32Const), U32V_3(val) #define WASM_I32V_4(val) \ static_cast
(CheckI32v((val), 4), kExprI32Const), U32V_4(val) #define WASM_I32V_5(val) \ static_cast
(CheckI32v((val), 5), kExprI32Const), U32V_5(val) //------------------------------------------------------------------------------ // Int64 Const operations //------------------------------------------------------------------------------ #define WASM_I64V(val) \ kExprI64Const, \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 21) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 28) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 35) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 42) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 49) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 56) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 63) & MASK_7) #define WASM_I64V_1(val) \ static_cast
(CheckI64v(static_cast
(val), 1), kExprI64Const), \ static_cast
(static_cast
(val) & MASK_7) #define WASM_I64V_2(val) \ static_cast
(CheckI64v(static_cast
(val), 2), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 7) & MASK_7) #define WASM_I64V_3(val) \ static_cast
(CheckI64v(static_cast
(val), 3), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 14) & MASK_7) #define WASM_I64V_4(val) \ static_cast
(CheckI64v(static_cast
(val), 4), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 21) & MASK_7) #define WASM_I64V_5(val) \ static_cast
(CheckI64v(static_cast
(val), 5), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 21) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 28) & MASK_7) #define WASM_I64V_6(val) \ static_cast
(CheckI64v(static_cast
(val), 6), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 21) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 28) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 35) & MASK_7) #define WASM_I64V_7(val) \ static_cast
(CheckI64v(static_cast
(val), 7), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 21) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 28) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 35) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 42) & MASK_7) #define WASM_I64V_8(val) \ static_cast
(CheckI64v(static_cast
(val), 8), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 21) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 28) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 35) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 42) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 49) & MASK_7) #define WASM_I64V_9(val) \ static_cast
(CheckI64v(static_cast
(val), 9), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 21) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 28) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 35) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 42) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 49) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 56) & MASK_7) #define WASM_I64V_10(val) \ static_cast
(CheckI64v(static_cast
(val), 10), kExprI64Const), \ static_cast
((static_cast
(val) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 7) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 14) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 21) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 28) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 35) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 42) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 49) & MASK_7) | 0x80), \ static_cast
(((static_cast
(val) >> 56) & MASK_7) | 0x80), \ static_cast
((static_cast
(val) >> 63) & MASK_7) #define WASM_F32(val) \ kExprF32Const, \ static_cast
(bit_cast
(static_cast
(val))), \ static_cast
(bit_cast
(static_cast
(val)) >> 8), \ static_cast
(bit_cast
(static_cast
(val)) >> 16), \ static_cast
(bit_cast
(static_cast
(val)) >> 24) #define WASM_F64(val) \ kExprF64Const, static_cast
(bit_cast
(val)), \ static_cast
(bit_cast
(val) >> 8), \ static_cast
(bit_cast
(val) >> 16), \ static_cast
(bit_cast
(val) >> 24), \ static_cast
(bit_cast
(val) >> 32), \ static_cast
(bit_cast
(val) >> 40), \ static_cast
(bit_cast
(val) >> 48), \ static_cast
(bit_cast
(val) >> 56) #define WASM_GET_LOCAL(index) kExprGetLocal, static_cast
(index) #define WASM_SET_LOCAL(index, val) val, kExprSetLocal, static_cast
(index) #define WASM_TEE_LOCAL(index, val) val, kExprTeeLocal, static_cast
(index) #define WASM_DROP kExprDrop #define WASM_GET_GLOBAL(index) kExprGetGlobal, static_cast
(index) #define WASM_SET_GLOBAL(index, val) \ val, kExprSetGlobal, static_cast
(index) #define WASM_LOAD_MEM(type, index) \ index, static_cast
( \ v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \ ZERO_ALIGNMENT, ZERO_OFFSET #define WASM_STORE_MEM(type, index, val) \ index, val, \ static_cast
( \ v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \ ZERO_ALIGNMENT, ZERO_OFFSET #define WASM_LOAD_MEM_OFFSET(type, offset, index) \ index, static_cast
( \ v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \ ZERO_ALIGNMENT, static_cast
(offset) #define WASM_STORE_MEM_OFFSET(type, offset, index, val) \ index, val, \ static_cast
( \ v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \ ZERO_ALIGNMENT, static_cast
(offset) #define WASM_LOAD_MEM_ALIGNMENT(type, index, alignment) \ index, static_cast
( \ v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \ alignment, ZERO_OFFSET #define WASM_STORE_MEM_ALIGNMENT(type, index, alignment, val) \ index, val, \ static_cast
( \ v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \ alignment, ZERO_OFFSET #define WASM_CALL_FUNCTION0(index) kExprCallFunction, static_cast
(index) #define WASM_CALL_FUNCTION(index, ...) \ __VA_ARGS__, kExprCallFunction, static_cast
(index) #define TABLE_ZERO 0 // TODO(titzer): change usages of these macros to put func last. #define WASM_CALL_INDIRECT0(index, func) \ func, kExprCallIndirect, static_cast
(index), TABLE_ZERO #define WASM_CALL_INDIRECT1(index, func, a) \ a, func, kExprCallIndirect, static_cast
(index), TABLE_ZERO #define WASM_CALL_INDIRECT2(index, func, a, b) \ a, b, func, kExprCallIndirect, static_cast
(index), TABLE_ZERO #define WASM_CALL_INDIRECT3(index, func, a, b, c) \ a, b, c, func, kExprCallIndirect, static_cast
(index), TABLE_ZERO #define WASM_CALL_INDIRECT4(index, func, a, b, c, d) \ a, b, c, d, func, kExprCallIndirect, static_cast
(index), TABLE_ZERO #define WASM_CALL_INDIRECT5(index, func, a, b, c, d, e) \ a, b, c, d, e, func, kExprCallIndirect, static_cast
(index), TABLE_ZERO #define WASM_CALL_INDIRECTN(arity, index, func, ...) \ __VA_ARGS__, func, kExprCallIndirect, static_cast
(index), TABLE_ZERO #define WASM_NOT(x) x, kExprI32Eqz #define WASM_SEQ(...) __VA_ARGS__ //------------------------------------------------------------------------------ // Constructs that are composed of multiple bytecodes. //------------------------------------------------------------------------------ #define WASM_WHILE(x, y) \ kExprLoop, kLocalVoid, x, kExprIf, kLocalVoid, y, kExprBr, DEPTH_1, \ kExprEnd, kExprEnd #define WASM_INC_LOCAL(index) \ kExprGetLocal, static_cast
(index), kExprI8Const, 1, kExprI32Add, \ kExprTeeLocal, static_cast
(index) #define WASM_INC_LOCAL_BYV(index, count) \ kExprGetLocal, static_cast
(index), kExprI8Const, \ static_cast
(count), kExprI32Add, kExprTeeLocal, \ static_cast
(index) #define WASM_INC_LOCAL_BY(index, count) \ kExprGetLocal, static_cast
(index), kExprI8Const, \ static_cast