1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  *
4  * Copyright 2016 Mozilla Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 // This is an INTERNAL header for Wasm baseline compiler: inline BaseCompiler
20 // methods that don't fit in any other group in particular.
21 
22 #ifndef wasm_wasm_baseline_reg_defs_inl_h
23 #define wasm_wasm_baseline_reg_defs_inl_h
24 
25 namespace js {
26 namespace wasm {
27 // TODO / OPTIMIZE (Bug 1316802): Do not sync everything on allocation
28 // failure, only as much as we need.
29 
needI32()30 RegI32 BaseRegAlloc::needI32() {
31   if (!hasGPR()) {
32     bc->sync();
33   }
34   return RegI32(allocGPR());
35 }
36 
needI32(RegI32 specific)37 void BaseRegAlloc::needI32(RegI32 specific) {
38   if (!isAvailableI32(specific)) {
39     bc->sync();
40   }
41   allocGPR(specific);
42 }
43 
needI64()44 RegI64 BaseRegAlloc::needI64() {
45   if (!hasGPR64()) {
46     bc->sync();
47   }
48   return RegI64(allocInt64());
49 }
50 
needI64(RegI64 specific)51 void BaseRegAlloc::needI64(RegI64 specific) {
52   if (!isAvailableI64(specific)) {
53     bc->sync();
54   }
55   allocInt64(specific);
56 }
57 
needRef()58 RegRef BaseRegAlloc::needRef() {
59   if (!hasGPR()) {
60     bc->sync();
61   }
62   return RegRef(allocGPR());
63 }
64 
needRef(RegRef specific)65 void BaseRegAlloc::needRef(RegRef specific) {
66   if (!isAvailableRef(specific)) {
67     bc->sync();
68   }
69   allocGPR(specific);
70 }
71 
needPtr()72 RegPtr BaseRegAlloc::needPtr() {
73   if (!hasGPR()) {
74     bc->sync();
75   }
76   return RegPtr(allocGPR());
77 }
78 
needPtr(RegPtr specific)79 void BaseRegAlloc::needPtr(RegPtr specific) {
80   if (!isAvailablePtr(specific)) {
81     bc->sync();
82   }
83   allocGPR(specific);
84 }
85 
needTempPtr(RegPtr fallback,bool * saved)86 RegPtr BaseRegAlloc::needTempPtr(RegPtr fallback, bool* saved) {
87   if (hasGPR()) {
88     *saved = false;
89     return RegPtr(allocGPR());
90   }
91   *saved = true;
92   bc->saveTempPtr(fallback);
93   MOZ_ASSERT(isAvailablePtr(fallback));
94   allocGPR(fallback);
95   return RegPtr(fallback);
96 }
97 
needF32()98 RegF32 BaseRegAlloc::needF32() {
99   if (!hasFPU<MIRType::Float32>()) {
100     bc->sync();
101   }
102   return RegF32(allocFPU<MIRType::Float32>());
103 }
104 
needF32(RegF32 specific)105 void BaseRegAlloc::needF32(RegF32 specific) {
106   if (!isAvailableF32(specific)) {
107     bc->sync();
108   }
109   allocFPU(specific);
110 }
111 
needF64()112 RegF64 BaseRegAlloc::needF64() {
113   if (!hasFPU<MIRType::Double>()) {
114     bc->sync();
115   }
116   return RegF64(allocFPU<MIRType::Double>());
117 }
118 
needF64(RegF64 specific)119 void BaseRegAlloc::needF64(RegF64 specific) {
120   if (!isAvailableF64(specific)) {
121     bc->sync();
122   }
123   allocFPU(specific);
124 }
125 
126 #ifdef ENABLE_WASM_SIMD
needV128()127 RegV128 BaseRegAlloc::needV128() {
128   if (!hasFPU<MIRType::Simd128>()) {
129     bc->sync();
130   }
131   return RegV128(allocFPU<MIRType::Simd128>());
132 }
133 
needV128(RegV128 specific)134 void BaseRegAlloc::needV128(RegV128 specific) {
135   if (!isAvailableV128(specific)) {
136     bc->sync();
137   }
138   allocFPU(specific);
139 }
140 #endif
141 
freeI32(RegI32 r)142 void BaseRegAlloc::freeI32(RegI32 r) { freeGPR(r); }
143 
freeI64(RegI64 r)144 void BaseRegAlloc::freeI64(RegI64 r) { freeInt64(r); }
145 
freeRef(RegRef r)146 void BaseRegAlloc::freeRef(RegRef r) { freeGPR(r); }
147 
freePtr(RegPtr r)148 void BaseRegAlloc::freePtr(RegPtr r) { freeGPR(r); }
149 
freeF64(RegF64 r)150 void BaseRegAlloc::freeF64(RegF64 r) { freeFPU(r); }
151 
freeF32(RegF32 r)152 void BaseRegAlloc::freeF32(RegF32 r) { freeFPU(r); }
153 
154 #ifdef ENABLE_WASM_SIMD
freeV128(RegV128 r)155 void BaseRegAlloc::freeV128(RegV128 r) { freeFPU(r); }
156 #endif
157 
freeTempPtr(RegPtr r,bool saved)158 void BaseRegAlloc::freeTempPtr(RegPtr r, bool saved) {
159   freePtr(r);
160   if (saved) {
161     bc->restoreTempPtr(r);
162     MOZ_ASSERT(!isAvailablePtr(r));
163   }
164 }
165 
166 #ifdef JS_CODEGEN_ARM
needI64Pair()167 RegI64 BaseRegAlloc::needI64Pair() {
168   if (!hasGPRPair()) {
169     bc->sync();
170   }
171   Register low, high;
172   allocGPRPair(&low, &high);
173   return RegI64(Register64(high, low));
174 }
175 #endif
176 
177 }  // namespace wasm
178 }  // namespace js
179 
180 #endif  // wasm_wasm_baseline_reg_defs_inl_h
181