| 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | // This file contains two helper functions MakeLocationSummaryFromEmitter |
| 6 | // and InvokeEmitter which simplify the definition of MakeLocationSummary |
| 7 | // and EmitNativeCode methods for instructions. |
| 8 | // |
| 9 | // Canonical way to define instruction backend would be to override: |
| 10 | // |
| 11 | // A) MakeLocationSummary method that creates and fills LocationSummary object |
| 12 | // with location constraints for register allocator; |
| 13 | // |
| 14 | // B) EmitNativeCode method that unpacks results of register allocation from |
| 15 | // LocationSummary and uses them to generate native code. |
| 16 | // |
| 17 | // Helpers contained in this file allow to "autogenerate" both of these methods |
| 18 | // from a single *emitter* function that has the following signature: |
| 19 | // |
| 20 | // void Emitter(FlowGraphCompiler*, |
| 21 | // Instr* instr, |
| 22 | // OutType out, |
| 23 | // InputType1 v1, ...) |
| 24 | // |
| 25 | // Here Instr is the type of the instruction, OutType is a type of an output |
| 26 | // register and InputType1, InputType2, etc are register types for inputs or |
| 27 | // temps. |
| 28 | // |
| 29 | // To create LocationSummary from emitter's signature invoke |
| 30 | // |
| 31 | // MakeLocationSummaryFromEmitter(zone, instr, &Emitter); |
| 32 | // |
| 33 | // To unpack allocation results from LocationSummary and call emitter write |
| 34 | // |
| 35 | // InvokeEmitter(zone, instr, &Emitter) |
| 36 | // |
| 37 | // See DEFINE_BACKEND macro below that can be used to do that. |
| 38 | // |
| 39 | // In addition to supporting Register and FpuRegister types several markers can |
| 40 | // be used to denote various register constraints, e.g. SameAsFirstInput, Fixed |
| 41 | // and Temp. See below. |
| 42 | // |
| 43 | #ifndef RUNTIME_VM_COMPILER_BACKEND_LOCATIONS_HELPERS_H_ |
| 44 | #define RUNTIME_VM_COMPILER_BACKEND_LOCATIONS_HELPERS_H_ |
| 45 | |
| 46 | #if defined(DART_PRECOMPILED_RUNTIME) |
| 47 | #error "AOT runtime should not use compiler sources (including header files)" |
| 48 | #endif // defined(DART_PRECOMPILED_RUNTIME) |
| 49 | |
| 50 | #include "vm/compiler/backend/locations.h" |
| 51 | |
| 52 | namespace dart { |
| 53 | |
| 54 | // Forward declarations. |
| 55 | class FlowGraphCompiler; |
| 56 | |
| 57 | #define DEFINE_BACKEND(Name, Args) \ |
| 58 | static void EmitterFor##Name(FlowGraphCompiler* compiler, \ |
| 59 | Name##Instr* instr, PP_APPLY(PP_UNPACK, Args)); \ |
| 60 | LocationSummary* Name##Instr::MakeLocationSummary(Zone* zone, bool opt) \ |
| 61 | const { \ |
| 62 | return MakeLocationSummaryFromEmitter(zone, this, &EmitterFor##Name); \ |
| 63 | } \ |
| 64 | void Name##Instr::EmitNativeCode(FlowGraphCompiler* compiler) { \ |
| 65 | InvokeEmitter(compiler, this, &EmitterFor##Name); \ |
| 66 | } \ |
| 67 | static void EmitterFor##Name(FlowGraphCompiler* compiler, \ |
| 68 | Name##Instr* instr, PP_APPLY(PP_UNPACK, Args)) |
| 69 | |
| 70 | #define PP_UNPACK(...) __VA_ARGS__ |
| 71 | #define PP_APPLY(a, b) a b |
| 72 | |
| 73 | // Trait that specifies how different types of locations (e.g. Register, |
| 74 | // FpuRegister) can be extracted from Location objects and how register |
| 75 | // constraints can be created for different location types and markers like |
| 76 | // Temp, Fixed and SameAsFirstInput. |
| 77 | template <typename T> |
| 78 | struct LocationTrait; |
| 79 | |
| 80 | // Marker type used to signal that output has SameAsFirstInput register |
| 81 | // constraint, which means that the first input needs to be in a writable |
| 82 | // register and the instruction will produce output in the same register. |
| 83 | struct SameAsFirstInput {}; |
| 84 | |
| 85 | // Marker type used to signal that output has NoLocation register |
| 86 | // constraint. |
| 87 | struct NoLocation {}; |
| 88 | |
| 89 | // Marker type used to signal that this input, output or temp needs to |
| 90 | // be in a fixed register `reg` of type `R` (either Register or FpuRegister). |
| 91 | template <typename R, R reg> |
| 92 | struct Fixed { |
| 93 | // Allow implicit coercion of Fixed<R, ...> to R. |
| 94 | operator R() { return reg; } |
| 95 | }; |
| 96 | |
| 97 | // Marker type to signal that emitter needs a temporary register of type R. |
| 98 | template <typename R> |
| 99 | class Temp { |
| 100 | private: |
| 101 | typedef typename LocationTrait<R>::RegisterType RegisterType; |
| 102 | |
| 103 | public: |
| 104 | explicit Temp(R reg) : reg_(reg) {} |
| 105 | |
| 106 | operator RegisterType() { return reg_; } |
| 107 | |
| 108 | private: |
| 109 | R reg_; |
| 110 | }; |
| 111 | |
| 112 | // Implementation of MakeLocationSummaryFromEmitter and InvokeEmitter. |
| 113 | |
| 114 | template <> |
| 115 | struct LocationTrait<Register> { |
| 116 | typedef Register RegisterType; |
| 117 | |
| 118 | static const bool kIsTemp = false; // This is not a temporary. |
| 119 | |
| 120 | static Register Unwrap(const Location& loc) { return loc.reg(); } |
| 121 | |
| 122 | template <intptr_t arity, intptr_t index> |
| 123 | static Register UnwrapInput(LocationSummary* locs) { |
| 124 | return Unwrap(locs->in(index)); |
| 125 | } |
| 126 | |
| 127 | template <intptr_t arity, intptr_t index> |
| 128 | static void SetInputConstraint(LocationSummary* locs) { |
| 129 | locs->set_in(index, ToConstraint()); |
| 130 | } |
| 131 | |
| 132 | static Location ToConstraint() { return Location::RequiresRegister(); } |
| 133 | static Location ToFixedConstraint(Register reg) { |
| 134 | return Location::RegisterLocation(reg); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | template <> |
| 139 | struct LocationTrait<FpuRegister> { |
| 140 | typedef FpuRegister RegisterType; |
| 141 | |
| 142 | static const bool kIsTemp = false; // This is not a temporary. |
| 143 | |
| 144 | static FpuRegister Unwrap(const Location& loc) { return loc.fpu_reg(); } |
| 145 | |
| 146 | template <intptr_t arity, intptr_t index> |
| 147 | static FpuRegister UnwrapInput(LocationSummary* locs) { |
| 148 | return Unwrap(locs->in(index)); |
| 149 | } |
| 150 | |
| 151 | template <intptr_t arity, intptr_t index> |
| 152 | static void SetInputConstraint(LocationSummary* locs) { |
| 153 | locs->set_in(index, ToConstraint()); |
| 154 | } |
| 155 | |
| 156 | static Location ToConstraint() { return Location::RequiresFpuRegister(); } |
| 157 | static Location ToFixedConstraint(FpuRegister reg) { |
| 158 | return Location::FpuRegisterLocation(reg); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | template <typename R, R reg> |
| 163 | struct LocationTrait<Fixed<R, reg> > { |
| 164 | typedef R RegisterType; |
| 165 | |
| 166 | static const bool kIsTemp = false; // This is not a temporary. |
| 167 | |
| 168 | static Fixed<R, reg> Unwrap(const Location& loc) { |
| 169 | ASSERT(LocationTrait<R>::Unwrap(loc) == reg); |
| 170 | return Fixed<R, reg>(); |
| 171 | } |
| 172 | |
| 173 | template <intptr_t arity, intptr_t index> |
| 174 | static Fixed<R, reg> UnwrapInput(LocationSummary* locs) { |
| 175 | return Unwrap(locs->in(index)); |
| 176 | } |
| 177 | |
| 178 | template <intptr_t arity, intptr_t index> |
| 179 | static void SetInputConstraint(LocationSummary* locs) { |
| 180 | locs->set_in(index, ToConstraint()); |
| 181 | } |
| 182 | |
| 183 | static Location ToConstraint() { |
| 184 | return LocationTrait<R>::ToFixedConstraint(reg); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | template <typename RegisterType> |
| 189 | struct LocationTrait<Temp<RegisterType> > { |
| 190 | static const bool kIsTemp = true; // This is a temporary. |
| 191 | |
| 192 | static Temp<RegisterType> Unwrap(const Location& loc) { |
| 193 | return Temp<RegisterType>(LocationTrait<RegisterType>::Unwrap(loc)); |
| 194 | } |
| 195 | |
| 196 | template <intptr_t arity, intptr_t index> |
| 197 | static Temp<RegisterType> UnwrapInput(LocationSummary* locs) { |
| 198 | return Unwrap(locs->temp(index - arity)); |
| 199 | } |
| 200 | |
| 201 | template <intptr_t arity, intptr_t index> |
| 202 | static void SetInputConstraint(LocationSummary* locs) { |
| 203 | locs->set_temp(index - arity, ToConstraint()); |
| 204 | } |
| 205 | |
| 206 | static Location ToConstraint() { |
| 207 | return LocationTrait<RegisterType>::ToConstraint(); |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | template <> |
| 212 | struct LocationTrait<SameAsFirstInput> { |
| 213 | static const bool kIsTemp = false; // This is not a temporary. |
| 214 | |
| 215 | static SameAsFirstInput Unwrap(const Location& loc) { |
| 216 | return SameAsFirstInput(); |
| 217 | } |
| 218 | |
| 219 | static Location ToConstraint() { return Location::SameAsFirstInput(); } |
| 220 | }; |
| 221 | |
| 222 | template <> |
| 223 | struct LocationTrait<NoLocation> { |
| 224 | static const bool kIsTemp = false; // This is not a temporary. |
| 225 | |
| 226 | static NoLocation Unwrap(const Location& loc) { return NoLocation(); } |
| 227 | |
| 228 | static Location ToConstraint() { return Location::NoLocation(); } |
| 229 | }; |
| 230 | |
| 231 | // Auxiliary types and macro helpers to construct lists of types. |
| 232 | // TODO(vegorov) rewrite this using variadic templates when we enable C++11 |
| 233 | |
| 234 | struct Nil; |
| 235 | |
| 236 | template <typename T, typename U> |
| 237 | struct Cons {}; |
| 238 | |
| 239 | #define TYPE_LIST_0() Nil |
| 240 | #define TYPE_LIST_1(T0) Cons<T0, TYPE_LIST_0()> |
| 241 | #define TYPE_LIST_2(T0, T1) Cons<T0, TYPE_LIST_1(T1)> |
| 242 | #define TYPE_LIST_3(T0, T1, T2) Cons<T0, TYPE_LIST_2(T1, T2)> |
| 243 | #define TYPE_LIST_4(T0, T1, T2, T3) Cons<T0, TYPE_LIST_3(T1, T2, T3)> |
| 244 | #define TYPE_LIST_5(T0, T1, T2, T3, T4) Cons<T0, TYPE_LIST_4(T1, T2, T3, T4)> |
| 245 | |
| 246 | // SignatureTrait is a recursively defined type that calculates InputCount and |
| 247 | // TempCount for a signature and can be used to invoke SetInputConstraint for |
| 248 | // each type in a signature to populate location summary with correct |
| 249 | // constraints. |
| 250 | #define SIGNATURE_TRAIT(Arity, Args) \ |
| 251 | SignatureTrait<PP_APPLY(TYPE_LIST_##Arity, Args)> |
| 252 | |
| 253 | template <typename T> |
| 254 | struct SignatureTrait; |
| 255 | |
| 256 | template <> |
| 257 | struct SignatureTrait<Nil> { |
| 258 | enum { kArity = 0, kTempCount = 0, kInputCount = kArity - kTempCount }; |
| 259 | |
| 260 | template <intptr_t kArity, intptr_t kOffset> |
| 261 | static void SetConstraints(LocationSummary* locs) {} |
| 262 | }; |
| 263 | |
| 264 | template <typename T0, typename Tx> |
| 265 | struct SignatureTrait<Cons<T0, Tx> > { |
| 266 | typedef SignatureTrait<Tx> Tail; |
| 267 | |
| 268 | enum { |
| 269 | kArity = 1 + Tail::kArity, |
| 270 | kTempCount = (LocationTrait<T0>::kIsTemp ? 1 : 0) + Tail::kTempCount, |
| 271 | kInputCount = kArity - kTempCount |
| 272 | }; |
| 273 | |
| 274 | template <intptr_t kArity, intptr_t kOffset> |
| 275 | static void SetConstraints(LocationSummary* locs) { |
| 276 | LocationTrait<T0>::template SetInputConstraint<kArity, kOffset>(locs); |
| 277 | Tail::template SetConstraints<kArity, kOffset + 1>(locs); |
| 278 | } |
| 279 | }; |
| 280 | |
| 281 | // MakeLocationSummaryFromEmitter overloadings below. |
| 282 | |
| 283 | template <typename Instr, typename Out> |
| 284 | LocationSummary* MakeLocationSummaryFromEmitter(Zone* zone, |
| 285 | const Instr* instr, |
| 286 | void (*Emit)(FlowGraphCompiler*, |
| 287 | Instr*, |
| 288 | Out)) { |
| 289 | typedef SIGNATURE_TRAIT(0, ()) S; |
| 290 | ASSERT(instr->InputCount() == S::kInputCount); |
| 291 | LocationSummary* summary = new (zone) LocationSummary( |
| 292 | zone, S::kInputCount, S::kTempCount, LocationSummary::kNoCall); |
| 293 | summary->set_out(0, LocationTrait<Out>::ToConstraint()); |
| 294 | return summary; |
| 295 | } |
| 296 | |
| 297 | #define DEFINE_MAKE_LOCATION_SUMMARY_SPECIALIZATION(Arity, Types) \ |
| 298 | LocationSummary* MakeLocationSummaryFromEmitter( \ |
| 299 | Zone* zone, const Instr* instr, \ |
| 300 | void (*Emit)(FlowGraphCompiler*, Instr*, Out, \ |
| 301 | PP_APPLY(PP_UNPACK, Types))) { \ |
| 302 | typedef SIGNATURE_TRAIT(Arity, Types) S; \ |
| 303 | ASSERT(instr->InputCount() == S::kInputCount); \ |
| 304 | LocationSummary* summary = new (zone) LocationSummary( \ |
| 305 | zone, S::kInputCount, S::kTempCount, LocationSummary::kNoCall); \ |
| 306 | S::template SetConstraints<S::kInputCount, 0>(summary); \ |
| 307 | summary->set_out(0, LocationTrait<Out>::ToConstraint()); \ |
| 308 | return summary; \ |
| 309 | } |
| 310 | |
| 311 | template <typename Instr, typename Out, typename T0> |
| 312 | DEFINE_MAKE_LOCATION_SUMMARY_SPECIALIZATION(1, (T0)); |
| 313 | |
| 314 | template <typename Instr, typename Out, typename T0, typename T1> |
| 315 | DEFINE_MAKE_LOCATION_SUMMARY_SPECIALIZATION(2, (T0, T1)); |
| 316 | |
| 317 | template <typename Instr, typename Out, typename T0, typename T1, typename T2> |
| 318 | DEFINE_MAKE_LOCATION_SUMMARY_SPECIALIZATION(3, (T0, T1, T2)); |
| 319 | |
| 320 | template <typename Instr, |
| 321 | typename Out, |
| 322 | typename T0, |
| 323 | typename T1, |
| 324 | typename T2, |
| 325 | typename T3> |
| 326 | DEFINE_MAKE_LOCATION_SUMMARY_SPECIALIZATION(4, (T0, T1, T2, T3)); |
| 327 | |
| 328 | template <typename Instr, |
| 329 | typename Out, |
| 330 | typename T0, |
| 331 | typename T1, |
| 332 | typename T2, |
| 333 | typename T3, |
| 334 | typename T4> |
| 335 | DEFINE_MAKE_LOCATION_SUMMARY_SPECIALIZATION(5, (T0, T1, T2, T3, T4)); |
| 336 | |
| 337 | // InvokeEmitter overloadings below. |
| 338 | |
| 339 | template <typename Instr, typename Out> |
| 340 | void InvokeEmitter(FlowGraphCompiler* compiler, |
| 341 | Instr* instr, |
| 342 | void (*Emit)(FlowGraphCompiler*, Instr*, Out)) { |
| 343 | typedef SIGNATURE_TRAIT(0, ()) S; |
| 344 | ASSERT(instr->InputCount() == S::kInputCount); |
| 345 | LocationSummary* locs = instr->locs(); |
| 346 | Emit(compiler, instr, LocationTrait<Out>::Unwrap(locs->out(0))); |
| 347 | } |
| 348 | |
| 349 | template <typename Instr, typename Out, typename T0> |
| 350 | void InvokeEmitter(FlowGraphCompiler* compiler, |
| 351 | Instr* instr, |
| 352 | void (*Emit)(FlowGraphCompiler*, Instr*, Out, T0)) { |
| 353 | typedef SIGNATURE_TRAIT(1, (T0)) S; |
| 354 | ASSERT(instr->InputCount() == S::kInputCount); |
| 355 | LocationSummary* locs = instr->locs(); |
| 356 | Emit(compiler, instr, LocationTrait<Out>::Unwrap(locs->out(0)), |
| 357 | LocationTrait<T0>::template UnwrapInput<S::kInputCount, 0>(locs)); |
| 358 | } |
| 359 | |
| 360 | template <typename Instr, typename Out, typename T0, typename T1> |
| 361 | void InvokeEmitter(FlowGraphCompiler* compiler, |
| 362 | Instr* instr, |
| 363 | void (*Emit)(FlowGraphCompiler*, Instr*, Out, T0, T1)) { |
| 364 | typedef SIGNATURE_TRAIT(2, (T0, T1)) S; |
| 365 | ASSERT(instr->InputCount() == S::kInputCount); |
| 366 | LocationSummary* locs = instr->locs(); |
| 367 | Emit(compiler, instr, LocationTrait<Out>::Unwrap(locs->out(0)), |
| 368 | LocationTrait<T0>::template UnwrapInput<S::kInputCount, 0>(locs), |
| 369 | LocationTrait<T1>::template UnwrapInput<S::kInputCount, 1>(locs)); |
| 370 | } |
| 371 | |
| 372 | template <typename Instr, typename Out, typename T0, typename T1, typename T2> |
| 373 | void InvokeEmitter(FlowGraphCompiler* compiler, |
| 374 | Instr* instr, |
| 375 | void (*Emit)(FlowGraphCompiler*, Instr*, Out, T0, T1, T2)) { |
| 376 | typedef SIGNATURE_TRAIT(3, (T0, T1, T2)) S; |
| 377 | ASSERT(instr->InputCount() == S::kInputCount); |
| 378 | LocationSummary* locs = instr->locs(); |
| 379 | Emit(compiler, instr, LocationTrait<Out>::Unwrap(locs->out(0)), |
| 380 | LocationTrait<T0>::template UnwrapInput<S::kInputCount, 0>(locs), |
| 381 | LocationTrait<T1>::template UnwrapInput<S::kInputCount, 1>(locs), |
| 382 | LocationTrait<T2>::template UnwrapInput<S::kInputCount, 2>(locs)); |
| 383 | } |
| 384 | |
| 385 | template <typename Instr, |
| 386 | typename Out, |
| 387 | typename T0, |
| 388 | typename T1, |
| 389 | typename T2, |
| 390 | typename T3> |
| 391 | void InvokeEmitter( |
| 392 | FlowGraphCompiler* compiler, |
| 393 | Instr* instr, |
| 394 | void (*Emit)(FlowGraphCompiler*, Instr*, Out, T0, T1, T2, T3)) { |
| 395 | typedef SIGNATURE_TRAIT(4, (T0, T1, T2, T3)) S; |
| 396 | ASSERT(instr->InputCount() == S::kInputCount); |
| 397 | LocationSummary* locs = instr->locs(); |
| 398 | Emit(compiler, instr, LocationTrait<Out>::Unwrap(locs->out(0)), |
| 399 | LocationTrait<T0>::template UnwrapInput<S::kInputCount, 0>(locs), |
| 400 | LocationTrait<T1>::template UnwrapInput<S::kInputCount, 1>(locs), |
| 401 | LocationTrait<T2>::template UnwrapInput<S::kInputCount, 2>(locs), |
| 402 | LocationTrait<T3>::template UnwrapInput<S::kInputCount, 3>(locs)); |
| 403 | } |
| 404 | |
| 405 | template <typename Instr, |
| 406 | typename Out, |
| 407 | typename T0, |
| 408 | typename T1, |
| 409 | typename T2, |
| 410 | typename T3, |
| 411 | typename T4> |
| 412 | void InvokeEmitter( |
| 413 | FlowGraphCompiler* compiler, |
| 414 | Instr* instr, |
| 415 | void (*Emit)(FlowGraphCompiler*, Instr*, Out, T0, T1, T2, T3, T4)) { |
| 416 | typedef SIGNATURE_TRAIT(5, (T0, T1, T2, T3, T4)) S; |
| 417 | ASSERT(instr->InputCount() == S::kInputCount); |
| 418 | LocationSummary* locs = instr->locs(); |
| 419 | Emit(compiler, instr, LocationTrait<Out>::Unwrap(locs->out(0)), |
| 420 | LocationTrait<T0>::template UnwrapInput<S::kInputCount, 0>(locs), |
| 421 | LocationTrait<T1>::template UnwrapInput<S::kInputCount, 1>(locs), |
| 422 | LocationTrait<T2>::template UnwrapInput<S::kInputCount, 2>(locs), |
| 423 | LocationTrait<T3>::template UnwrapInput<S::kInputCount, 3>(locs), |
| 424 | LocationTrait<T4>::template UnwrapInput<S::kInputCount, 4>(locs)); |
| 425 | } |
| 426 | |
| 427 | } // namespace dart |
| 428 | |
| 429 | #if defined(TARGET_ARCH_IA32) |
| 430 | |
| 431 | #elif defined(TARGET_ARCH_X64) |
| 432 | |
| 433 | #elif defined(TARGET_ARCH_ARM) |
| 434 | #include "vm/compiler/backend/locations_helpers_arm.h" |
| 435 | #elif defined(TARGET_ARCH_ARM64) |
| 436 | |
| 437 | #else |
| 438 | #error Unknown architecture. |
| 439 | #endif |
| 440 | |
| 441 | #endif // RUNTIME_VM_COMPILER_BACKEND_LOCATIONS_HELPERS_H_ |
| 442 | |