1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
*
* Copyright 2016 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This is an INTERNAL header for Wasm baseline compiler: inline methods in the
// compiler for basic code generation.
#ifndef wasm_wasm_baseline_codegen_inl_h
#define wasm_wasm_baseline_codegen_inl_h
// The templates for register management must be defined by the time we use the
// templated emitters, below.
#include "wasm/WasmBCRegMgmt-inl.h"
namespace js {
namespace wasm {
//////////////////////////////////////////////////////////////////////////////
//
// Register-to-register moves.
void BaseCompiler::moveI32(RegI32 src, RegI32 dest) {
if (src != dest) {
masm.move32(src, dest);
}
}
void BaseCompiler::moveI64(RegI64 src, RegI64 dest) {
if (src != dest) {
masm.move64(src, dest);
}
}
void BaseCompiler::moveRef(RegRef src, RegRef dest) {
if (src != dest) {
masm.movePtr(src, dest);
}
}
void BaseCompiler::movePtr(RegPtr src, RegPtr dest) {
if (src != dest) {
masm.movePtr(src, dest);
}
}
void BaseCompiler::moveF64(RegF64 src, RegF64 dest) {
if (src != dest) {
masm.moveDouble(src, dest);
}
}
void BaseCompiler::moveF32(RegF32 src, RegF32 dest) {
if (src != dest) {
masm.moveFloat32(src, dest);
}
}
#ifdef ENABLE_WASM_SIMD
void BaseCompiler::moveV128(RegV128 src, RegV128 dest) {
if (src != dest) {
masm.moveSimd128(src, dest);
}
}
#endif
template <>
inline void BaseCompiler::move<RegI32>(RegI32 src, RegI32 dest) {
moveI32(src, dest);
}
template <>
inline void BaseCompiler::move<RegI64>(RegI64 src, RegI64 dest) {
moveI64(src, dest);
}
template <>
inline void BaseCompiler::move<RegF32>(RegF32 src, RegF32 dest) {
moveF32(src, dest);
}
template <>
inline void BaseCompiler::move<RegF64>(RegF64 src, RegF64 dest) {
moveF64(src, dest);
}
template <>
inline void BaseCompiler::move<RegRef>(RegRef src, RegRef dest) {
moveRef(src, dest);
}
template <>
inline void BaseCompiler::move<RegPtr>(RegPtr src, RegPtr dest) {
movePtr(src, dest);
}
#ifdef ENABLE_WASM_SIMD
template <>
inline void BaseCompiler::move<RegV128>(RegV128 src, RegV128 dest) {
moveV128(src, dest);
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Constant loads.
void BaseCompiler::moveImm32(int32_t v, RegI32 dest) {
masm.move32(Imm32(v), dest);
}
void BaseCompiler::moveImm64(int64_t v, RegI64 dest) {
masm.move64(Imm64(v), dest);
}
void BaseCompiler::moveImmRef(intptr_t v, RegRef dest) {
masm.movePtr(ImmWord(v), dest);
}
//////////////////////////////////////////////////////////////////////////////
//
// Calls.
RegI32 BaseCompiler::captureReturnedI32() {
RegI32 r = RegI32(ReturnReg);
MOZ_ASSERT(isAvailableI32(r));
needI32(r);
#if defined(JS_64BIT)
masm.widenInt32(r);
#endif
return r;
}
RegI64 BaseCompiler::captureReturnedI64() {
RegI64 r = RegI64(ReturnReg64);
MOZ_ASSERT(isAvailableI64(r));
needI64(r);
return r;
}
RegF32 BaseCompiler::captureReturnedF32(const FunctionCall& call) {
RegF32 r = RegF32(ReturnFloat32Reg);
MOZ_ASSERT(isAvailableF32(r));
needF32(r);
#if defined(JS_CODEGEN_ARM)
if (call.usesSystemAbi && !call.hardFP) {
masm.ma_vxfer(ReturnReg, r);
}
#endif
return r;
}
RegF64 BaseCompiler::captureReturnedF64(const FunctionCall& call) {
RegF64 r = RegF64(ReturnDoubleReg);
MOZ_ASSERT(isAvailableF64(r));
needF64(r);
#if defined(JS_CODEGEN_ARM)
if (call.usesSystemAbi && !call.hardFP) {
masm.ma_vxfer(ReturnReg64.low, ReturnReg64.high, r);
}
#endif
return r;
}
#ifdef ENABLE_WASM_SIMD
RegV128 BaseCompiler::captureReturnedV128(const FunctionCall& call) {
RegV128 r = RegV128(ReturnSimd128Reg);
MOZ_ASSERT(isAvailableV128(r));
needV128(r);
return r;
}
#endif
RegRef BaseCompiler::captureReturnedRef() {
RegRef r = RegRef(ReturnReg);
MOZ_ASSERT(isAvailableRef(r));
needRef(r);
return r;
}
//////////////////////////////////////////////////////////////////////////////
//
// Miscellaneous.
void BaseCompiler::trap(Trap t) const { masm.wasmTrap(t, bytecodeOffset()); }
void BaseCompiler::cmp64Set(Assembler::Condition cond, RegI64 lhs, RegI64 rhs,
RegI32 dest) {
#if defined(JS_PUNBOX64)
masm.cmpPtrSet(cond, lhs.reg, rhs.reg, dest);
#else
// TODO / OPTIMIZE (Bug 1316822): This is pretty branchy, we should be
// able to do better.
Label done, condTrue;
masm.branch64(cond, lhs, rhs, &condTrue);
moveImm32(0, dest);
masm.jump(&done);
masm.bind(&condTrue);
moveImm32(1, dest);
masm.bind(&done);
#endif
}
[[nodiscard]] bool BaseCompiler::supportsRoundInstruction(RoundingMode mode) {
return Assembler::HasRoundInstruction(mode);
}
void BaseCompiler::roundF32(RoundingMode roundingMode, RegF32 f0) {
masm.nearbyIntFloat32(roundingMode, f0, f0);
}
void BaseCompiler::roundF64(RoundingMode roundingMode, RegF64 f0) {
masm.nearbyIntDouble(roundingMode, f0, f0);
}
void BaseCompiler::branchTo(Assembler::DoubleCondition c, RegF64 lhs,
RegF64 rhs, Label* l) {
masm.branchDouble(c, lhs, rhs, l);
}
void BaseCompiler::branchTo(Assembler::DoubleCondition c, RegF32 lhs,
RegF32 rhs, Label* l) {
masm.branchFloat(c, lhs, rhs, l);
}
void BaseCompiler::branchTo(Assembler::Condition c, RegI32 lhs, RegI32 rhs,
Label* l) {
masm.branch32(c, lhs, rhs, l);
}
void BaseCompiler::branchTo(Assembler::Condition c, RegI32 lhs, Imm32 rhs,
Label* l) {
masm.branch32(c, lhs, rhs, l);
}
void BaseCompiler::branchTo(Assembler::Condition c, RegI64 lhs, RegI64 rhs,
Label* l) {
masm.branch64(c, lhs, rhs, l);
}
void BaseCompiler::branchTo(Assembler::Condition c, RegI64 lhs, Imm64 rhs,
Label* l) {
masm.branch64(c, lhs, rhs, l);
}
void BaseCompiler::branchTo(Assembler::Condition c, RegRef lhs, ImmWord rhs,
Label* l) {
masm.branchPtr(c, lhs, rhs, l);
}
//////////////////////////////////////////////////////////////////////////////
//
// Templated emitters
template <>
inline BaseCompiler& BaseCompiler::selectCompiler<BaseCompiler>() {
return *this;
}
template <>
inline MacroAssembler& BaseCompiler::selectCompiler<MacroAssembler>() {
return masm;
}
template <typename SourceType, typename DestType>
void BaseCompiler::emitUnop(void (*op)(MacroAssembler& masm, SourceType rs,
DestType rd)) {
SourceType rs = pop<SourceType>();
DestType rd = need<DestType>();
op(masm, rs, rd);
free(rs);
push(rd);
}
// Specialize narrowing reuse. Consumers may assume that rs.reg==rd on 64-bit
// platforms, or rs.low==rd on 32-bit platforms.
template <>
inline void BaseCompiler::emitUnop(void (*op)(MacroAssembler& masm, RegI64 rs,
RegI32 rd)) {
RegI64 rs = pop<RegI64>();
RegI32 rd = fromI64(rs);
op(masm, rs, rd);
freeI64Except(rs, rd);
push(rd);
}
template <typename CompilerType, typename RegType>
void BaseCompiler::emitUnop(void (*op)(CompilerType& compiler, RegType rsd)) {
RegType rsd = pop<RegType>();
op(selectCompiler<CompilerType>(), rsd);
push(rsd);
}
template <typename RegType, typename TempType>
void BaseCompiler::emitUnop(void (*op)(BaseCompiler& bc, RegType rsd,
TempType rt),
TempType (*getSpecializedTemp)(BaseCompiler& bc)) {
RegType rsd = pop<RegType>();
TempType temp = getSpecializedTemp(*this);
op(*this, rsd, temp);
maybeFree(temp);
push(rsd);
}
template <typename SourceType, typename DestType, typename TempType>
void BaseCompiler::emitUnop(void (*op)(MacroAssembler& masm, SourceType rs,
DestType rd, TempType temp)) {
SourceType rs = pop<SourceType>();
DestType rd = need<DestType>();
TempType temp = need<TempType>();
op(masm, rs, rd, temp);
free(rs);
free(temp);
push(rd);
}
template <typename SourceType, typename DestType, typename ImmType>
void BaseCompiler::emitUnop(ImmType immediate,
void (*op)(MacroAssembler&, ImmType, SourceType,
DestType)) {
SourceType rs = pop<SourceType>();
DestType rd = need<DestType>();
op(masm, immediate, rs, rd);
free(rs);
push(rd);
}
template <typename CompilerType, typename RhsType, typename LhsDestType>
void BaseCompiler::emitBinop(void (*op)(CompilerType& masm, RhsType src,
LhsDestType srcDest)) {
RhsType rs = pop<RhsType>();
LhsDestType rsd = pop<LhsDestType>();
op(selectCompiler<CompilerType>(), rs, rsd);
free(rs);
push(rsd);
}
template <typename CompilerType, typename ValType>
void BaseCompiler::emitTernary(void (*op)(CompilerType&, ValType src0,
ValType src1, ValType srcDest)) {
ValType src2 = pop<ValType>();
ValType src1 = pop<ValType>();
ValType srcDest = pop<ValType>();
op(selectCompiler<CompilerType>(), src1, src2, srcDest);
free(src2);
free(src1);
push(srcDest);
}
template <typename CompilerType, typename ValType>
void BaseCompiler::emitTernary(void (*op)(CompilerType&, ValType src0,
ValType src1, ValType srcDest,
ValType temp)) {
ValType src2 = pop<ValType>();
ValType src1 = pop<ValType>();
ValType srcDest = pop<ValType>();
ValType temp = need<ValType>();
op(selectCompiler<CompilerType>(), src1, src2, srcDest, temp);
free(temp);
free(src2);
free(src1);
push(srcDest);
}
template <typename CompilerType, typename ValType>
void BaseCompiler::emitTernaryResultLast(void (*op)(CompilerType&, ValType src0,
ValType src1,
ValType srcDest)) {
ValType srcDest = pop<ValType>();
ValType src2 = pop<ValType>();
ValType src1 = pop<ValType>();
op(selectCompiler<CompilerType>(), src1, src2, srcDest);
free(src2);
free(src1);
push(srcDest);
}
template <typename RhsDestType, typename LhsType>
void BaseCompiler::emitBinop(void (*op)(MacroAssembler& masm, RhsDestType src,
LhsType srcDest, RhsDestOp)) {
RhsDestType rsd = pop<RhsDestType>();
LhsType rs = pop<LhsType>();
op(masm, rsd, rs, RhsDestOp::True);
free(rs);
push(rsd);
}
template <typename RhsType, typename LhsDestType, typename TempType>
void BaseCompiler::emitBinop(void (*op)(MacroAssembler& masm, RhsType rs,
LhsDestType rsd, TempType temp)) {
RhsType rs = pop<RhsType>();
LhsDestType rsd = pop<LhsDestType>();
TempType temp = need<TempType>();
op(masm, rs, rsd, temp);
free(rs);
free(temp);
push(rsd);
}
template <typename RhsType, typename LhsDestType, typename TempType1,
typename TempType2>
void BaseCompiler::emitBinop(void (*op)(MacroAssembler& masm, RhsType rs,
LhsDestType rsd, TempType1 temp1,
TempType2 temp2)) {
RhsType rs = pop<RhsType>();
LhsDestType rsd = pop<LhsDestType>();
TempType1 temp1 = need<TempType1>();
TempType2 temp2 = need<TempType2>();
op(masm, rs, rsd, temp1, temp2);
free(rs);
free(temp1);
free(temp2);
push(rsd);
}
template <typename RhsType, typename LhsDestType, typename ImmType>
void BaseCompiler::emitBinop(ImmType immediate,
void (*op)(MacroAssembler&, ImmType, RhsType,
LhsDestType)) {
RhsType rs = pop<RhsType>();
LhsDestType rsd = pop<LhsDestType>();
op(masm, immediate, rs, rsd);
free(rs);
push(rsd);
}
template <typename RhsType, typename LhsDestType, typename ImmType,
typename TempType1, typename TempType2>
void BaseCompiler::emitBinop(ImmType immediate,
void (*op)(MacroAssembler&, ImmType, RhsType,
LhsDestType, TempType1 temp1,
TempType2 temp2)) {
RhsType rs = pop<RhsType>();
LhsDestType rsd = pop<LhsDestType>();
TempType1 temp1 = need<TempType1>();
TempType2 temp2 = need<TempType2>();
op(masm, immediate, rs, rsd, temp1, temp2);
free(rs);
free(temp1);
free(temp2);
push(rsd);
}
template <typename CompilerType1, typename CompilerType2, typename RegType,
typename ImmType>
void BaseCompiler::emitBinop(void (*op)(CompilerType1& compiler, RegType rs,
RegType rsd),
void (*opConst)(CompilerType2& compiler, ImmType c,
RegType rsd),
RegType (BaseCompiler::*rhsPopper)()) {
ImmType c;
if (popConst(&c)) {
RegType rsd = pop<RegType>();
opConst(selectCompiler<CompilerType2>(), c, rsd);
push(rsd);
} else {
RegType rs = rhsPopper ? (this->*rhsPopper)() : pop<RegType>();
RegType rsd = pop<RegType>();
op(selectCompiler<CompilerType1>(), rs, rsd);
free(rs);
push(rsd);
}
}
template <typename R>
bool BaseCompiler::emitInstanceCallOp(const SymbolicAddressSignature& fn,
R reader) {
if (!reader()) {
return false;
}
if (deadCode_) {
return true;
}
return emitInstanceCall(fn);
}
template <typename A1, typename R>
bool BaseCompiler::emitInstanceCallOp(const SymbolicAddressSignature& fn,
R reader) {
A1 arg = 0;
if (!reader(&arg)) {
return false;
}
if (deadCode_) {
return true;
}
push(arg);
return emitInstanceCall(fn);
}
template <typename A1, typename A2, typename R>
bool BaseCompiler::emitInstanceCallOp(const SymbolicAddressSignature& fn,
R reader) {
A1 arg1 = 0;
A2 arg2 = 0;
if (!reader(&arg1, &arg2)) {
return false;
}
if (deadCode_) {
return true;
}
// Note order of arguments must be the same as for the reader.
push(arg1);
push(arg2);
return emitInstanceCall(fn);
}
} // namespace wasm
} // namespace js
#endif // wasm_wasm_baseline_codegen_inl_h
|