x86regalloc_p.h 22.1 KB
Newer Older
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// [AsmJit]
// Complete x86/x64 JIT and Remote Assembler for C++.
//
// [License]
// Zlib - See LICENSE.md file in the package.

// [Guard]
#ifndef _ASMJIT_X86_X86REGALLOC_P_H
#define _ASMJIT_X86_X86REGALLOC_P_H

#include "../asmjit_build.h"
#if !defined(ASMJIT_DISABLE_COMPILER)

// [Dependencies]
#include "../base/codecompiler.h"
#include "../base/regalloc_p.h"
#include "../base/utils.h"
#include "../x86/x86assembler.h"
#include "../x86/x86compiler.h"
#include "../x86/x86misc.h"

// [Api-Begin]
#include "../asmjit_apibegin.h"

namespace asmjit {

//! \addtogroup asmjit_x86
//! \{

// ============================================================================
// [asmjit::X86RAData]
// ============================================================================

struct X86RAData : public RAData {
  ASMJIT_INLINE X86RAData(uint32_t tiedTotal) noexcept : RAData(tiedTotal) {
    inRegs.reset();
    outRegs.reset();
    clobberedRegs.reset();
    tiedIndex.reset();
    tiedCount.reset();
  }

  // --------------------------------------------------------------------------
  // [Accessors]
  // --------------------------------------------------------------------------

  //! Get TiedReg array.
  ASMJIT_INLINE TiedReg* getTiedArray() const noexcept {
    return const_cast<TiedReg*>(tiedArray);
  }

  //! Get TiedReg array for a given register `kind`.
  ASMJIT_INLINE TiedReg* getTiedArrayByKind(uint32_t kind) const noexcept {
    return const_cast<TiedReg*>(tiedArray) + tiedIndex.get(kind);
  }

  //! Get TiedReg index for a given register `kind`.
  ASMJIT_INLINE uint32_t getTiedStart(uint32_t kind) const noexcept {
    return tiedIndex.get(kind);
  }

  //! Get TiedReg count for a given register `kind`.
  ASMJIT_INLINE uint32_t getTiedCountByKind(uint32_t kind) const noexcept {
    return tiedCount.get(kind);
  }

  //! Get TiedReg at the specified `index`.
  ASMJIT_INLINE TiedReg* getTiedAt(uint32_t index) const noexcept {
    ASMJIT_ASSERT(index < tiedTotal);
    return getTiedArray() + index;
  }

  //! Get TiedReg at the specified index for a given register `kind`.
  ASMJIT_INLINE TiedReg* getTiedAtByKind(uint32_t kind, uint32_t index) const noexcept {
    ASMJIT_ASSERT(index < tiedCount._regs[kind]);
    return getTiedArrayByKind(kind) + index;
  }

  ASMJIT_INLINE void setTiedAt(uint32_t index, TiedReg& tied) noexcept {
    ASMJIT_ASSERT(index < tiedTotal);
    tiedArray[index] = tied;
  }

  // --------------------------------------------------------------------------
  // [Utils]
  // --------------------------------------------------------------------------

  //! Find TiedReg.
  ASMJIT_INLINE TiedReg* findTied(VirtReg* vreg) const noexcept {
    TiedReg* tiedArray = getTiedArray();
    uint32_t tiedCount = tiedTotal;

    for (uint32_t i = 0; i < tiedCount; i++)
      if (tiedArray[i].vreg == vreg)
        return &tiedArray[i];

    return nullptr;
  }

  //! Find TiedReg (by class).
  ASMJIT_INLINE TiedReg* findTiedByKind(uint32_t kind, VirtReg* vreg) const noexcept {
    TiedReg* tiedArray = getTiedArrayByKind(kind);
    uint32_t tiedCount = getTiedCountByKind(kind);

    for (uint32_t i = 0; i < tiedCount; i++)
      if (tiedArray[i].vreg == vreg)
        return &tiedArray[i];

    return nullptr;
  }

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

  //! Special registers on input.
  //!
  //! Special register(s) restricted to one or more physical register. If there
  //! is more than one special register it means that we have to duplicate the
  //! variable content to all of them (it means that the same varible was used
  //! by two or more operands). We forget about duplicates after the register
  //! allocation finishes and marks all duplicates as non-assigned.
  X86RegMask inRegs;

  //! Special registers on output.
  //!
  //! Special register(s) used on output. Each variable can have only one
  //! special register on the output, 'X86RAData' contains all registers from
  //! all 'TiedReg's.
  X86RegMask outRegs;

  //! Clobbered registers (by a function call).
  X86RegMask clobberedRegs;

  //! Start indexes of `TiedReg`s per register kind.
  X86RegCount tiedIndex;
  //! Count of variables per register kind.
  X86RegCount tiedCount;

  //! Linked registers.
  TiedReg tiedArray[1];
};

// ============================================================================
// [asmjit::X86StateCell]
// ============================================================================

//! X86/X64 state-cell.
union X86StateCell {
  // --------------------------------------------------------------------------
  // [Accessors]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE uint32_t getState() const noexcept { return _state; }
  ASMJIT_INLINE void setState(uint32_t state) noexcept { _state = static_cast<uint8_t>(state); }

  // --------------------------------------------------------------------------
  // [Reset]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE void reset() noexcept { _packed = 0; }

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

  uint8_t _packed;

  struct {
    uint8_t _state : 2;
    uint8_t _unused : 6;
  };
};

// ============================================================================
// [asmjit::X86RAState]
// ============================================================================

//! X86/X64 state.
struct X86RAState : RAState {
  enum {
    //! Base index of GP registers.
    kGpIndex = 0,
    //! Count of GP registers.
    kGpCount = 16,

    //! Base index of MMX registers.
    kMmIndex = kGpIndex + kGpCount,
    //! Count of Mm registers.
    kMmCount = 8,

    //! Base index of XMM registers.
    kXmmIndex = kMmIndex + kMmCount,
    //! Count of XMM registers.
    kXmmCount = 16,

    //! Count of all registers in `X86RAState`.
    kAllCount = kXmmIndex + kXmmCount
  };

  // --------------------------------------------------------------------------
  // [Accessors]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE VirtReg** getList() {
    return _list;
  }

  ASMJIT_INLINE VirtReg** getListByKind(uint32_t kind) {
    switch (kind) {
      case X86Reg::kKindGp : return _listGp;
      case X86Reg::kKindMm : return _listMm;
      case X86Reg::kKindVec: return _listXmm;

      default:
        return nullptr;
    }
  }

  // --------------------------------------------------------------------------
  // [Clear]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE void reset(size_t numCells) {
    ::memset(this, 0, kAllCount * sizeof(VirtReg*) +
                      2         * sizeof(X86RegMask) +
                      numCells  * sizeof(X86StateCell));
  }

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

  union {
    //! List of all allocated variables in one array.
    VirtReg* _list[kAllCount];

    struct {
      //! Allocated GP registers.
      VirtReg* _listGp[kGpCount];
      //! Allocated MMX registers.
      VirtReg* _listMm[kMmCount];
      //! Allocated XMM registers.
      VirtReg* _listXmm[kXmmCount];
    };
  };

  //! Occupied registers (mask).
  X86RegMask _occupied;
  //! Modified registers (mask).
  X86RegMask _modified;

  //! Variables data, the length is stored in `X86RAPass`.
  X86StateCell _cells[1];
};

// ============================================================================
// [asmjit::X86RAPass]
// ============================================================================

#if defined(ASMJIT_DEBUG)
# define ASMJIT_X86_CHECK_STATE _checkState();
#else
# define ASMJIT_X86_CHECK_STATE
#endif // ASMJIT_DEBUG

//! \internal
//!
//! X86 register allocator pipeline.
//!
//! Takes care of generating function prologs and epilogs, and also performs
//! register allocation.
class X86RAPass : public RAPass {
public:
  ASMJIT_NONCOPYABLE(X86RAPass)
  typedef RAPass Base;

  enum RegOp {
    kRegOpMove,
    kRegOpLoad,
    kRegOpSave
  };

  // --------------------------------------------------------------------------
  // [Construction / Destruction]
  // --------------------------------------------------------------------------

  X86RAPass() noexcept;
  virtual ~X86RAPass() noexcept;

  // --------------------------------------------------------------------------
  // [Interface]
  // --------------------------------------------------------------------------

  virtual Error process(Zone* zone) noexcept override;
  virtual Error prepare(CCFunc* func) noexcept override;

  // --------------------------------------------------------------------------
  // [ArchInfo]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE uint32_t getGpSize() const noexcept { return _zsp.getSize(); }

  // --------------------------------------------------------------------------
  // [Accessors]
  // --------------------------------------------------------------------------

  //! Get compiler as `X86Compiler`.
  ASMJIT_INLINE X86Compiler* cc() const noexcept { return static_cast<X86Compiler*>(_cb); }
  //! Get clobbered registers (global).
  ASMJIT_INLINE uint32_t getClobberedRegs(uint32_t kind) noexcept { return _clobberedRegs.get(kind); }

  // --------------------------------------------------------------------------
  // [Helpers]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE X86RAData* newRAData(uint32_t tiedTotal) noexcept {
    return new(_zone->alloc(sizeof(X86RAData) + tiedTotal * sizeof(TiedReg))) X86RAData(tiedTotal);
  }

  // --------------------------------------------------------------------------
  // [Emit]
  // --------------------------------------------------------------------------

  // Tiny wrappers that call `X86Internal::emit...()`.
  Error emitMove(VirtReg* vreg, uint32_t dstId, uint32_t srcId, const char* reason);
  Error emitLoad(VirtReg* vreg, uint32_t id, const char* reason);
  Error emitSave(VirtReg* vreg, uint32_t id, const char* reason);
  Error emitSwapGp(VirtReg* aVReg, VirtReg* bVReg, uint32_t aId, uint32_t bId, const char* reason) noexcept;

  Error emitImmToReg(uint32_t dstTypeId, uint32_t dstPhysId, const Imm* src) noexcept;
  Error emitImmToStack(uint32_t dstTypeId, const X86Mem* dst, const Imm* src) noexcept;
  Error emitRegToStack(uint32_t dstTypeId, const X86Mem* dst, uint32_t srcTypeId, uint32_t srcPhysId) noexcept;

  // --------------------------------------------------------------------------
  // [Register Management]
  // --------------------------------------------------------------------------

  void _checkState();

  // --------------------------------------------------------------------------
  // [Attach / Detach]
  // --------------------------------------------------------------------------

  //! Attach.
  //!
  //! Attach a register to the 'VirtReg', changing 'VirtReg' members to show
  //! that the variable is currently alive and linking variable with the
  //! current 'X86RAState'.
  template<int C>
  ASMJIT_INLINE void attach(VirtReg* vreg, uint32_t physId, bool modified) {
    ASMJIT_ASSERT(vreg->getKind() == C);
    ASMJIT_ASSERT(physId != Globals::kInvalidRegId);

    // Prevent Esp allocation if C==Gp.
    ASMJIT_ASSERT(C != X86Reg::kKindGp || physId != X86Gp::kIdSp);

    uint32_t regMask = Utils::mask(physId);

    vreg->setState(VirtReg::kStateReg);
    vreg->setModified(modified);
    vreg->setPhysId(physId);
    vreg->addHomeId(physId);

    _x86State.getListByKind(C)[physId] = vreg;
    _x86State._occupied.or_(C, regMask);
    _x86State._modified.or_(C, static_cast<uint32_t>(modified) << physId);

    ASMJIT_X86_CHECK_STATE
  }

  //! Detach.
  //!
  //! The opposite of 'Attach'. Detach resets the members in 'VirtReg'
  //! (physId, state and changed flags) and unlinks the variable with the
  //! current 'X86RAState'.
  template<int C>
  ASMJIT_INLINE void detach(VirtReg* vreg, uint32_t physId, uint32_t vState) {
    ASMJIT_ASSERT(vreg->getKind() == C);
    ASMJIT_ASSERT(vreg->getPhysId() == physId);
    ASMJIT_ASSERT(vState != VirtReg::kStateReg);

    uint32_t regMask = Utils::mask(physId);

    vreg->setState(vState);
    vreg->resetPhysId();
    vreg->setModified(false);

    _x86State.getListByKind(C)[physId] = nullptr;
    _x86State._occupied.andNot(C, regMask);
    _x86State._modified.andNot(C, regMask);

    ASMJIT_X86_CHECK_STATE
  }

  // --------------------------------------------------------------------------
  // [Rebase]
  // --------------------------------------------------------------------------

  //! Rebase.
  //!
  //! Change the register of the 'VirtReg' changing also the current 'X86RAState'.
  //! Rebase is nearly identical to 'Detach' and 'Attach' sequence, but doesn't
  //! change the `VirtReg`s modified flag.
  template<int C>
  ASMJIT_INLINE void rebase(VirtReg* vreg, uint32_t newPhysId, uint32_t oldPhysId) {
    ASMJIT_ASSERT(vreg->getKind() == C);

    uint32_t newRegMask = Utils::mask(newPhysId);
    uint32_t oldRegMask = Utils::mask(oldPhysId);
    uint32_t bothRegMask = newRegMask ^ oldRegMask;

    vreg->setPhysId(newPhysId);

    _x86State.getListByKind(C)[oldPhysId] = nullptr;
    _x86State.getListByKind(C)[newPhysId] = vreg;

    _x86State._occupied.xor_(C, bothRegMask);
    _x86State._modified.xor_(C, bothRegMask & -static_cast<int32_t>(vreg->isModified()));

    ASMJIT_X86_CHECK_STATE
  }

  // --------------------------------------------------------------------------
  // [Load / Save]
  // --------------------------------------------------------------------------

  //! Load.
  //!
  //! Load variable from its memory slot to a register, emitting 'Load'
  //! instruction and changing the variable state to allocated.
  template<int C>
  ASMJIT_INLINE void load(VirtReg* vreg, uint32_t physId) {
    // Can be only called if variable is not allocated.
    ASMJIT_ASSERT(vreg->getKind() == C);
    ASMJIT_ASSERT(vreg->getState() != VirtReg::kStateReg);
    ASMJIT_ASSERT(vreg->getPhysId() == Globals::kInvalidRegId);

    emitLoad(vreg, physId, "Load");
    attach<C>(vreg, physId, false);

    ASMJIT_X86_CHECK_STATE
  }

  //! Save.
  //!
  //! Save the variable into its home location, but keep it as allocated.
  template<int C>
  ASMJIT_INLINE void save(VirtReg* vreg) {
    ASMJIT_ASSERT(vreg->getKind() == C);
    ASMJIT_ASSERT(vreg->getState() == VirtReg::kStateReg);
    ASMJIT_ASSERT(vreg->getPhysId() != Globals::kInvalidRegId);

    uint32_t physId = vreg->getPhysId();
    uint32_t regMask = Utils::mask(physId);

    emitSave(vreg, physId, "Save");
    vreg->setModified(false);
    _x86State._modified.andNot(C, regMask);

    ASMJIT_X86_CHECK_STATE
  }

  // --------------------------------------------------------------------------
  // [Move / Swap]
  // --------------------------------------------------------------------------

  //! Move a register.
  //!
  //! Move register from one index to another, emitting 'Move' if needed. This
  //! function does nothing if register is already at the given index.
  template<int C>
  ASMJIT_INLINE void move(VirtReg* vreg, uint32_t newPhysId) {
    ASMJIT_ASSERT(vreg->getKind() == C);
    ASMJIT_ASSERT(vreg->getState() == VirtReg::kStateReg);
    ASMJIT_ASSERT(vreg->getPhysId() != Globals::kInvalidRegId);

    uint32_t oldPhysId = vreg->getPhysId();
    if (newPhysId != oldPhysId) {
      emitMove(vreg, newPhysId, oldPhysId, "Move");
      rebase<C>(vreg, newPhysId, oldPhysId);
    }

    ASMJIT_X86_CHECK_STATE
  }

  //! Swap two registers
  //!
  //! It's only possible to swap Gp registers.
  ASMJIT_INLINE void swapGp(VirtReg* aVReg, VirtReg* bVReg) {
    ASMJIT_ASSERT(aVReg != bVReg);

    ASMJIT_ASSERT(aVReg->getKind() == X86Reg::kKindGp);
    ASMJIT_ASSERT(aVReg->getState() == VirtReg::kStateReg);
    ASMJIT_ASSERT(aVReg->getPhysId() != Globals::kInvalidRegId);

    ASMJIT_ASSERT(bVReg->getKind() == X86Reg::kKindGp);
    ASMJIT_ASSERT(bVReg->getState() == VirtReg::kStateReg);
    ASMJIT_ASSERT(bVReg->getPhysId() != Globals::kInvalidRegId);

    uint32_t aIndex = aVReg->getPhysId();
    uint32_t bIndex = bVReg->getPhysId();

    emitSwapGp(aVReg, bVReg, aIndex, bIndex, "Swap");

    aVReg->setPhysId(bIndex);
    bVReg->setPhysId(aIndex);

    _x86State.getListByKind(X86Reg::kKindGp)[aIndex] = bVReg;
    _x86State.getListByKind(X86Reg::kKindGp)[bIndex] = aVReg;

    uint32_t m = aVReg->isModified() ^ bVReg->isModified();
    _x86State._modified.xor_(X86Reg::kKindGp, (m << aIndex) | (m << bIndex));

    ASMJIT_X86_CHECK_STATE
  }

  // --------------------------------------------------------------------------
  // [Alloc / Spill]
  // --------------------------------------------------------------------------

  //! Alloc.
  template<int C>
  ASMJIT_INLINE void alloc(VirtReg* vreg, uint32_t physId) {
    ASMJIT_ASSERT(vreg->getKind() == C);
    ASMJIT_ASSERT(physId != Globals::kInvalidRegId);

    uint32_t oldPhysId = vreg->getPhysId();
    uint32_t oldState = vreg->getState();
    uint32_t regMask = Utils::mask(physId);

    ASMJIT_ASSERT(_x86State.getListByKind(C)[physId] == nullptr || physId == oldPhysId);

    if (oldState != VirtReg::kStateReg) {
      if (oldState == VirtReg::kStateMem)
        emitLoad(vreg, physId, "Alloc");
      vreg->setModified(false);
    }
    else if (oldPhysId != physId) {
      emitMove(vreg, physId, oldPhysId, "Alloc");

      _x86State.getListByKind(C)[oldPhysId] = nullptr;
      regMask ^= Utils::mask(oldPhysId);
    }
    else {
      ASMJIT_X86_CHECK_STATE
      return;
    }

    vreg->setState(VirtReg::kStateReg);
    vreg->setPhysId(physId);
    vreg->addHomeId(physId);

    _x86State.getListByKind(C)[physId] = vreg;
    _x86State._occupied.xor_(C, regMask);
    _x86State._modified.xor_(C, regMask & -static_cast<int32_t>(vreg->isModified()));

    ASMJIT_X86_CHECK_STATE
  }

  //! Spill.
  //!
  //! Spill variable/register, saves the content to the memory-home if modified.
  template<int C>
  ASMJIT_INLINE void spill(VirtReg* vreg) {
    ASMJIT_ASSERT(vreg->getKind() == C);

    if (vreg->getState() != VirtReg::kStateReg) {
      ASMJIT_X86_CHECK_STATE
      return;
    }

    uint32_t physId = vreg->getPhysId();
    ASMJIT_ASSERT(physId != Globals::kInvalidRegId);
    ASMJIT_ASSERT(_x86State.getListByKind(C)[physId] == vreg);

    if (vreg->isModified())
      emitSave(vreg, physId, "Spill");
    detach<C>(vreg, physId, VirtReg::kStateMem);

    ASMJIT_X86_CHECK_STATE
  }

  // --------------------------------------------------------------------------
  // [Modify]
  // --------------------------------------------------------------------------

  template<int C>
  ASMJIT_INLINE void modify(VirtReg* vreg) {
    ASMJIT_ASSERT(vreg->getKind() == C);

    uint32_t physId = vreg->getPhysId();
    uint32_t regMask = Utils::mask(physId);

    vreg->setModified(true);
    _x86State._modified.or_(C, regMask);

    ASMJIT_X86_CHECK_STATE
  }

  // --------------------------------------------------------------------------
  // [Unuse]
  // --------------------------------------------------------------------------

  //! Unuse.
  //!
  //! Unuse variable, it will be detached it if it's allocated then its state
  //! will be changed to VirtReg::kStateNone.
  template<int C>
  ASMJIT_INLINE void unuse(VirtReg* vreg, uint32_t vState = VirtReg::kStateNone) {
    ASMJIT_ASSERT(vreg->getKind() == C);
    ASMJIT_ASSERT(vState != VirtReg::kStateReg);

    uint32_t physId = vreg->getPhysId();
    if (physId != Globals::kInvalidRegId)
      detach<C>(vreg, physId, vState);
    else
      vreg->setState(vState);

    ASMJIT_X86_CHECK_STATE
  }

  // --------------------------------------------------------------------------
  // [State]
  // --------------------------------------------------------------------------

  //! Get state as `X86RAState`.
  ASMJIT_INLINE X86RAState* getState() const { return const_cast<X86RAState*>(&_x86State); }

  virtual void loadState(RAState* src) override;
  virtual RAState* saveState() override;

  virtual void switchState(RAState* src) override;
  virtual void intersectStates(RAState* a, RAState* b) override;

  // --------------------------------------------------------------------------
  // [Memory]
  // --------------------------------------------------------------------------

  ASMJIT_INLINE X86Mem getVarMem(VirtReg* vreg) {
    (void)getVarCell(vreg);
    return X86Mem(Init,
      cc()->_nativeGpReg.getType(), vreg->getId(),
      Reg::kRegNone, kInvalidValue,
      0, 0, Mem::kSignatureMemRegHomeFlag);
  }

  // --------------------------------------------------------------------------
  // [Fetch]
  // --------------------------------------------------------------------------

  virtual Error fetch() override;

  // --------------------------------------------------------------------------
  // [Annotate]
  // --------------------------------------------------------------------------

  virtual Error annotate() override;

  // --------------------------------------------------------------------------
  // [Translate]
  // --------------------------------------------------------------------------

  virtual Error translate() override;

  // --------------------------------------------------------------------------
  // [Members]
  // --------------------------------------------------------------------------

  //! Count of X86/X64 registers.
  X86RegCount _regCount;
  //! X86/X64 stack-pointer (esp or rsp).
  X86Gp _zsp;
  //! X86/X64 frame-pointer (ebp or rbp).
  X86Gp _zbp;

  //! X86/X64 specific compiler state, linked to `_state`.
  X86RAState _x86State;
  //! Clobbered registers (for the whole function).
  X86RegMask _clobberedRegs;

  //! Global allocable registers mask.
  uint32_t _gaRegs[Globals::kMaxVRegKinds];

  bool _avxEnabled;

  //! Function variables base pointer (register).
  uint8_t _varBaseRegId;
  //! Function variables base offset.
  int32_t _varBaseOffset;

  //! Temporary string builder used for logging.
  StringBuilderTmp<256> _stringBuilder;
};

//! \}

} // asmjit namespace

// [Api-End]
#include "../asmjit_apiend.h"

// [Guard]
#endif // !ASMJIT_DISABLE_COMPILER
#endif // _ASMJIT_X86_X86REGALLOC_P_H