blob: dd653c174016c9d7c2346b2abba1db8209856a82 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
3// found in the LICENSE file.
4
5#ifndef V8_WASM_MACRO_GEN_H_
6#define V8_WASM_MACRO_GEN_H_
7
8#include "src/wasm/wasm-opcodes.h"
9
10// Convenience macros for building Wasm bytecode directly into a byte array.
11
12//------------------------------------------------------------------------------
13// Control.
14//------------------------------------------------------------------------------
15#define WASM_NOP kExprNop
16
17#define WASM_BLOCK(count, ...) kExprBlock, static_cast<byte>(count), __VA_ARGS__
18#define WASM_INFINITE_LOOP kExprLoop, 1, kExprBr, 0, kExprNop
19#define WASM_LOOP(count, ...) kExprLoop, static_cast<byte>(count), __VA_ARGS__
20#define WASM_IF(cond, tstmt) kExprIf, cond, tstmt
21#define WASM_IF_ELSE(cond, tstmt, fstmt) kExprIfElse, cond, tstmt, fstmt
22#define WASM_SELECT(cond, tval, fval) kExprSelect, cond, tval, fval
23#define WASM_BR(depth) kExprBr, static_cast<byte>(depth), kExprNop
24#define WASM_BR_IF(depth, cond) \
Ben Murdoch097c5b22016-05-18 11:27:45 +010025 kExprBrIf, static_cast<byte>(depth), kExprNop, cond
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026#define WASM_BRV(depth, val) kExprBr, static_cast<byte>(depth), val
Ben Murdoch097c5b22016-05-18 11:27:45 +010027#define WASM_BRV_IF(depth, val, cond) \
28 kExprBrIf, static_cast<byte>(depth), val, cond
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029#define WASM_BREAK(depth) kExprBr, static_cast<byte>(depth + 1), kExprNop
30#define WASM_CONTINUE(depth) kExprBr, static_cast<byte>(depth), kExprNop
31#define WASM_BREAKV(depth, val) kExprBr, static_cast<byte>(depth + 1), val
32#define WASM_RETURN0 kExprReturn
33#define WASM_RETURN(...) kExprReturn, __VA_ARGS__
34#define WASM_UNREACHABLE kExprUnreachable
35
36#define WASM_TABLESWITCH_OP(case_count, table_count, ...) \
37 kExprTableSwitch, static_cast<byte>(case_count), \
38 static_cast<byte>(case_count >> 8), static_cast<byte>(table_count), \
39 static_cast<byte>(table_count >> 8), __VA_ARGS__
40
41#define WASM_TABLESWITCH_BODY0(key) key
42
43#define WASM_TABLESWITCH_BODY(key, ...) key, __VA_ARGS__
44
45#define WASM_CASE(x) static_cast<byte>(x), static_cast<byte>(x >> 8)
46#define WASM_CASE_BR(x) static_cast<byte>(x), static_cast<byte>(0x80 | (x) >> 8)
47
48//------------------------------------------------------------------------------
49// Misc expressions.
50//------------------------------------------------------------------------------
51#define WASM_ID(...) __VA_ARGS__
52#define WASM_ZERO kExprI8Const, 0
53#define WASM_ONE kExprI8Const, 1
54#define WASM_I8(val) kExprI8Const, static_cast<byte>(val)
55#define WASM_I32(val) \
56 kExprI32Const, static_cast<byte>(val), static_cast<byte>(val >> 8), \
57 static_cast<byte>(val >> 16), static_cast<byte>(val >> 24)
58#define WASM_I64(val) \
59 kExprI64Const, static_cast<byte>(static_cast<uint64_t>(val)), \
60 static_cast<byte>(static_cast<uint64_t>(val) >> 8), \
61 static_cast<byte>(static_cast<uint64_t>(val) >> 16), \
62 static_cast<byte>(static_cast<uint64_t>(val) >> 24), \
63 static_cast<byte>(static_cast<uint64_t>(val) >> 32), \
64 static_cast<byte>(static_cast<uint64_t>(val) >> 40), \
65 static_cast<byte>(static_cast<uint64_t>(val) >> 48), \
66 static_cast<byte>(static_cast<uint64_t>(val) >> 56)
67#define WASM_F32(val) \
68 kExprF32Const, \
69 static_cast<byte>(bit_cast<int32_t>(static_cast<float>(val))), \
70 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 8), \
71 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 16), \
72 static_cast<byte>(bit_cast<uint32_t>(static_cast<float>(val)) >> 24)
73#define WASM_F64(val) \
74 kExprF64Const, static_cast<byte>(bit_cast<uint64_t>(val)), \
75 static_cast<byte>(bit_cast<uint64_t>(val) >> 8), \
76 static_cast<byte>(bit_cast<uint64_t>(val) >> 16), \
77 static_cast<byte>(bit_cast<uint64_t>(val) >> 24), \
78 static_cast<byte>(bit_cast<uint64_t>(val) >> 32), \
79 static_cast<byte>(bit_cast<uint64_t>(val) >> 40), \
80 static_cast<byte>(bit_cast<uint64_t>(val) >> 48), \
81 static_cast<byte>(bit_cast<uint64_t>(val) >> 56)
82#define WASM_GET_LOCAL(index) kExprGetLocal, static_cast<byte>(index)
83#define WASM_SET_LOCAL(index, val) kExprSetLocal, static_cast<byte>(index), val
84#define WASM_LOAD_GLOBAL(index) kExprLoadGlobal, static_cast<byte>(index)
85#define WASM_STORE_GLOBAL(index, val) \
86 kExprStoreGlobal, static_cast<byte>(index), val
87#define WASM_LOAD_MEM(type, index) \
88 static_cast<byte>( \
89 v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
90 v8::internal::wasm::WasmOpcodes::LoadStoreAccessOf(false), index
91#define WASM_STORE_MEM(type, index, val) \
92 static_cast<byte>( \
93 v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
94 v8::internal::wasm::WasmOpcodes::LoadStoreAccessOf(false), index, val
95#define WASM_LOAD_MEM_OFFSET(type, offset, index) \
96 static_cast<byte>( \
97 v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, false)), \
98 v8::internal::wasm::WasmOpcodes::LoadStoreAccessOf(true), \
99 static_cast<byte>(offset), index
100#define WASM_STORE_MEM_OFFSET(type, offset, index, val) \
101 static_cast<byte>( \
102 v8::internal::wasm::WasmOpcodes::LoadStoreOpcodeOf(type, true)), \
103 v8::internal::wasm::WasmOpcodes::LoadStoreAccessOf(true), \
104 static_cast<byte>(offset), index, val
105#define WASM_CALL_FUNCTION(index, ...) \
106 kExprCallFunction, static_cast<byte>(index), __VA_ARGS__
Ben Murdoch097c5b22016-05-18 11:27:45 +0100107#define WASM_CALL_IMPORT(index, ...) \
108 kExprCallImport, static_cast<byte>(index), __VA_ARGS__
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109#define WASM_CALL_INDIRECT(index, func, ...) \
110 kExprCallIndirect, static_cast<byte>(index), func, __VA_ARGS__
111#define WASM_CALL_FUNCTION0(index) kExprCallFunction, static_cast<byte>(index)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100112#define WASM_CALL_IMPORT0(index) kExprCallImport, static_cast<byte>(index)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113#define WASM_CALL_INDIRECT0(index, func) \
114 kExprCallIndirect, static_cast<byte>(index), func
115#define WASM_NOT(x) kExprBoolNot, x
116
117//------------------------------------------------------------------------------
118// Constructs that are composed of multiple bytecodes.
119//------------------------------------------------------------------------------
120#define WASM_WHILE(x, y) kExprLoop, 1, kExprIf, x, kExprBr, 0, y
121#define WASM_INC_LOCAL(index) \
122 kExprSetLocal, static_cast<byte>(index), kExprI32Add, kExprGetLocal, \
123 static_cast<byte>(index), kExprI8Const, 1
124#define WASM_INC_LOCAL_BY(index, count) \
125 kExprSetLocal, static_cast<byte>(index), kExprI32Add, kExprGetLocal, \
126 static_cast<byte>(index), kExprI8Const, static_cast<int8_t>(count)
127
128#define WASM_UNOP(opcode, x) static_cast<byte>(opcode), x
129#define WASM_BINOP(opcode, x, y) static_cast<byte>(opcode), x, y
130
131//------------------------------------------------------------------------------
132// Int32 operations
133//------------------------------------------------------------------------------
134#define WASM_I32_ADD(x, y) kExprI32Add, x, y
135#define WASM_I32_SUB(x, y) kExprI32Sub, x, y
136#define WASM_I32_MUL(x, y) kExprI32Mul, x, y
137#define WASM_I32_DIVS(x, y) kExprI32DivS, x, y
138#define WASM_I32_DIVU(x, y) kExprI32DivU, x, y
139#define WASM_I32_REMS(x, y) kExprI32RemS, x, y
140#define WASM_I32_REMU(x, y) kExprI32RemU, x, y
141#define WASM_I32_AND(x, y) kExprI32And, x, y
142#define WASM_I32_IOR(x, y) kExprI32Ior, x, y
143#define WASM_I32_XOR(x, y) kExprI32Xor, x, y
144#define WASM_I32_SHL(x, y) kExprI32Shl, x, y
145#define WASM_I32_SHR(x, y) kExprI32ShrU, x, y
146#define WASM_I32_SAR(x, y) kExprI32ShrS, x, y
147#define WASM_I32_EQ(x, y) kExprI32Eq, x, y
148#define WASM_I32_NE(x, y) kExprI32Ne, x, y
149#define WASM_I32_LTS(x, y) kExprI32LtS, x, y
150#define WASM_I32_LES(x, y) kExprI32LeS, x, y
151#define WASM_I32_LTU(x, y) kExprI32LtU, x, y
152#define WASM_I32_LEU(x, y) kExprI32LeU, x, y
153#define WASM_I32_GTS(x, y) kExprI32GtS, x, y
154#define WASM_I32_GES(x, y) kExprI32GeS, x, y
155#define WASM_I32_GTU(x, y) kExprI32GtU, x, y
156#define WASM_I32_GEU(x, y) kExprI32GeU, x, y
157#define WASM_I32_CLZ(x) kExprI32Clz, x
158#define WASM_I32_CTZ(x) kExprI32Ctz, x
159#define WASM_I32_POPCNT(x) kExprI32Popcnt, x
160
161//------------------------------------------------------------------------------
162// Int64 operations
163//------------------------------------------------------------------------------
164#define WASM_I64_ADD(x, y) kExprI64Add, x, y
165#define WASM_I64_SUB(x, y) kExprI64Sub, x, y
166#define WASM_I64_MUL(x, y) kExprI64Mul, x, y
167#define WASM_I64_DIVS(x, y) kExprI64DivS, x, y
168#define WASM_I64_DIVU(x, y) kExprI64DivU, x, y
169#define WASM_I64_REMS(x, y) kExprI64RemS, x, y
170#define WASM_I64_REMU(x, y) kExprI64RemU, x, y
171#define WASM_I64_AND(x, y) kExprI64And, x, y
172#define WASM_I64_IOR(x, y) kExprI64Ior, x, y
173#define WASM_I64_XOR(x, y) kExprI64Xor, x, y
174#define WASM_I64_SHL(x, y) kExprI64Shl, x, y
175#define WASM_I64_SHR(x, y) kExprI64ShrU, x, y
176#define WASM_I64_SAR(x, y) kExprI64ShrS, x, y
177#define WASM_I64_EQ(x, y) kExprI64Eq, x, y
178#define WASM_I64_NE(x, y) kExprI64Ne, x, y
179#define WASM_I64_LTS(x, y) kExprI64LtS, x, y
180#define WASM_I64_LES(x, y) kExprI64LeS, x, y
181#define WASM_I64_LTU(x, y) kExprI64LtU, x, y
182#define WASM_I64_LEU(x, y) kExprI64LeU, x, y
183#define WASM_I64_GTS(x, y) kExprI64GtS, x, y
184#define WASM_I64_GES(x, y) kExprI64GeS, x, y
185#define WASM_I64_GTU(x, y) kExprI64GtU, x, y
186#define WASM_I64_GEU(x, y) kExprI64GeU, x, y
187#define WASM_I64_CLZ(x) kExprI64Clz, x
188#define WASM_I64_CTZ(x) kExprI64Ctz, x
189#define WASM_I64_POPCNT(x) kExprI64Popcnt, x
190
191//------------------------------------------------------------------------------
192// Float32 operations
193//------------------------------------------------------------------------------
194#define WASM_F32_ADD(x, y) kExprF32Add, x, y
195#define WASM_F32_SUB(x, y) kExprF32Sub, x, y
196#define WASM_F32_MUL(x, y) kExprF32Mul, x, y
197#define WASM_F32_DIV(x, y) kExprF32Div, x, y
198#define WASM_F32_MIN(x, y) kExprF32Min, x, y
199#define WASM_F32_MAX(x, y) kExprF32Max, x, y
200#define WASM_F32_ABS(x) kExprF32Abs, x
201#define WASM_F32_NEG(x) kExprF32Neg, x
202#define WASM_F32_COPYSIGN(x, y) kExprF32CopySign, x, y
203#define WASM_F32_CEIL(x) kExprF32Ceil, x
204#define WASM_F32_FLOOR(x) kExprF32Floor, x
205#define WASM_F32_TRUNC(x) kExprF32Trunc, x
206#define WASM_F32_NEARESTINT(x) kExprF32NearestInt, x
207#define WASM_F32_SQRT(x) kExprF32Sqrt, x
208#define WASM_F32_EQ(x, y) kExprF32Eq, x, y
209#define WASM_F32_NE(x, y) kExprF32Ne, x, y
210#define WASM_F32_LT(x, y) kExprF32Lt, x, y
211#define WASM_F32_LE(x, y) kExprF32Le, x, y
212#define WASM_F32_GT(x, y) kExprF32Gt, x, y
213#define WASM_F32_GE(x, y) kExprF32Ge, x, y
214
215//------------------------------------------------------------------------------
216// Float64 operations
217//------------------------------------------------------------------------------
218#define WASM_F64_ADD(x, y) kExprF64Add, x, y
219#define WASM_F64_SUB(x, y) kExprF64Sub, x, y
220#define WASM_F64_MUL(x, y) kExprF64Mul, x, y
221#define WASM_F64_DIV(x, y) kExprF64Div, x, y
222#define WASM_F64_MIN(x, y) kExprF64Min, x, y
223#define WASM_F64_MAX(x, y) kExprF64Max, x, y
224#define WASM_F64_ABS(x) kExprF64Abs, x
225#define WASM_F64_NEG(x) kExprF64Neg, x
226#define WASM_F64_COPYSIGN(x, y) kExprF64CopySign, x, y
227#define WASM_F64_CEIL(x) kExprF64Ceil, x
228#define WASM_F64_FLOOR(x) kExprF64Floor, x
229#define WASM_F64_TRUNC(x) kExprF64Trunc, x
230#define WASM_F64_NEARESTINT(x) kExprF64NearestInt, x
231#define WASM_F64_SQRT(x) kExprF64Sqrt, x
232#define WASM_F64_EQ(x, y) kExprF64Eq, x, y
233#define WASM_F64_NE(x, y) kExprF64Ne, x, y
234#define WASM_F64_LT(x, y) kExprF64Lt, x, y
235#define WASM_F64_LE(x, y) kExprF64Le, x, y
236#define WASM_F64_GT(x, y) kExprF64Gt, x, y
237#define WASM_F64_GE(x, y) kExprF64Ge, x, y
238
239//------------------------------------------------------------------------------
240// Type conversions.
241//------------------------------------------------------------------------------
242#define WASM_I32_SCONVERT_F32(x) kExprI32SConvertF32, x
243#define WASM_I32_SCONVERT_F64(x) kExprI32SConvertF64, x
244#define WASM_I32_UCONVERT_F32(x) kExprI32UConvertF32, x
245#define WASM_I32_UCONVERT_F64(x) kExprI32UConvertF64, x
246#define WASM_I32_CONVERT_I64(x) kExprI32ConvertI64, x
247#define WASM_I64_SCONVERT_F32(x) kExprI64SConvertF32, x
248#define WASM_I64_SCONVERT_F64(x) kExprI64SConvertF64, x
249#define WASM_I64_UCONVERT_F32(x) kExprI64UConvertF32, x
250#define WASM_I64_UCONVERT_F64(x) kExprI64UConvertF64, x
251#define WASM_I64_SCONVERT_I32(x) kExprI64SConvertI32, x
252#define WASM_I64_UCONVERT_I32(x) kExprI64UConvertI32, x
253#define WASM_F32_SCONVERT_I32(x) kExprF32SConvertI32, x
254#define WASM_F32_UCONVERT_I32(x) kExprF32UConvertI32, x
255#define WASM_F32_SCONVERT_I64(x) kExprF32SConvertI64, x
256#define WASM_F32_UCONVERT_I64(x) kExprF32UConvertI64, x
257#define WASM_F32_CONVERT_F64(x) kExprF32ConvertF64, x
258#define WASM_F32_REINTERPRET_I32(x) kExprF32ReinterpretI32, x
259#define WASM_F64_SCONVERT_I32(x) kExprF64SConvertI32, x
260#define WASM_F64_UCONVERT_I32(x) kExprF64UConvertI32, x
261#define WASM_F64_SCONVERT_I64(x) kExprF64SConvertI64, x
262#define WASM_F64_UCONVERT_I64(x) kExprF64UConvertI64, x
263#define WASM_F64_CONVERT_F32(x) kExprF64ConvertF32, x
264#define WASM_F64_REINTERPRET_I64(x) kExprF64ReinterpretI64, x
265#define WASM_I32_REINTERPRET_F32(x) kExprI32ReinterpretF32, x
266#define WASM_I64_REINTERPRET_F64(x) kExprI64ReinterpretF64, x
267
268#endif // V8_WASM_MACRO_GEN_H_