summaryrefslogtreecommitdiffstats
path: root/js/src/jit/mips32/Architecture-mips32.h
blob: 8e186d2c9c0aec49ad712c24e5975c098802df68 (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef jit_mips32_Architecture_mips32_h
#define jit_mips32_Architecture_mips32_h

#include "mozilla/EndianUtils.h"
#include "mozilla/MathAlgorithms.h"

#include <limits.h>
#include <stdint.h>

#include "jit/mips-shared/Architecture-mips-shared.h"

#include "js/Utility.h"

namespace js {
namespace jit {

static const uint32_t ShadowStackSpace = 4 * sizeof(uintptr_t);

// These offsets are specific to nunboxing, and capture offsets into the
// components of a js::Value.
// Size of MIPS32 general purpose registers is 32 bits.
#if MOZ_LITTLE_ENDIAN()
static const int32_t NUNBOX32_TYPE_OFFSET = 4;
static const int32_t NUNBOX32_PAYLOAD_OFFSET = 0;
#else
static const int32_t NUNBOX32_TYPE_OFFSET = 0;
static const int32_t NUNBOX32_PAYLOAD_OFFSET = 4;
#endif

// MIPS32 can have two types of floating-point coprocessors modes:
// - FR=0 mode/ 32-bit FPRs - Historical default, there are 32 single
// precision registers and pairs of even and odd float registers are used as
// double precision registers. Example: f0 (double) is composed of
// f0 and f1 (single). Loongson3A FPU running in this mode doesn't allow
// use of odd registers for single precision arithmetic.
// - FR=1 mode/ 64-bit FPRs - In this case, there are 32 double precision
// register which can also be used as single precision registers. More info
// https://dmz-portal.imgtec.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking

// Currently we enable 16 even single precision registers which can be also can
// be used as double precision registers. It enables jit code to run even on
// Loongson3A. It does not support FR=1 mode because MacroAssembler threats odd
// single precision registers as high parts of even double precision registers.
#ifdef __mips_fpr
static_assert(__mips_fpr == 32, "MIPS32 jit only supports FR=0 fpu mode.");
#endif

class FloatRegisters : public FloatRegistersMIPSShared {
 public:
  static const char* GetName(uint32_t i) {
    MOZ_ASSERT(i < RegisterIdLimit);
    return FloatRegistersMIPSShared::GetName(Encoding(i % 32));
  }

  static Encoding FromName(const char* name);

  static const uint32_t Total = 32;
  static const uint32_t TotalDouble = 16;
  static const uint32_t TotalSingle = 16;

  static const uint32_t Allocatable = 30;
  static const SetType AllSingleMask = (1ULL << TotalSingle) - 1;

  static const SetType AllDoubleMask = ((1ULL << TotalDouble) - 1)
                                       << TotalSingle;
  static const SetType AllMask = AllDoubleMask | AllSingleMask;

  // When saving all registers we only need to do is save double registers.
  static const uint32_t TotalPhys = 16;
  static const uint32_t RegisterIdLimit = 32;

  static_assert(sizeof(SetType) * 8 >= Total,
                "SetType should be large enough to enumerate all registers.");

  static const SetType NonVolatileMask =
      ((SetType(1) << (FloatRegisters::f20 >> 1)) |
       (SetType(1) << (FloatRegisters::f22 >> 1)) |
       (SetType(1) << (FloatRegisters::f24 >> 1)) |
       (SetType(1) << (FloatRegisters::f26 >> 1)) |
       (SetType(1) << (FloatRegisters::f28 >> 1)) |
       (SetType(1) << (FloatRegisters::f30 >> 1))) *
      ((1 << TotalSingle) + 1);

  static const SetType VolatileMask = AllMask & ~NonVolatileMask;

  static const SetType WrapperMask = VolatileMask;

  static const SetType NonAllocatableMask =
      (SetType(1) << (FloatRegisters::f18 >> 1)) * ((1 << TotalSingle) + 1);

  static const SetType AllocatableMask = AllMask & ~NonAllocatableMask;
};

class FloatRegister : public FloatRegisterMIPSShared {
 public:
  enum RegType {
    Single = 0x0,
    Double = 0x1,
  };

  typedef FloatRegisters Codes;
  typedef Codes::Code Code;
  typedef Codes::Encoding Encoding;

  Encoding code_ : 6;

 protected:
  RegType kind_ : 1;

 public:
  constexpr FloatRegister(uint32_t code, RegType kind = Double)
      : code_(Encoding(code)), kind_(kind) {}
  constexpr FloatRegister()
      : code_(FloatRegisters::invalid_freg), kind_(Double) {}

  bool operator==(const FloatRegister& other) const {
    MOZ_ASSERT(!isInvalid());
    MOZ_ASSERT(!other.isInvalid());
    return kind_ == other.kind_ && code_ == other.code_;
  }
  bool equiv(const FloatRegister& other) const { return other.kind_ == kind_; }
  size_t size() const { return (kind_ == Double) ? 8 : 4; }
  size_t pushSize() const { return size(); }

  bool isNotOdd() const { return !isInvalid() && ((code_ & 1) == 0); }

  bool isSingle() const { return kind_ == Single; }
  bool isDouble() const { return kind_ == Double; }
  bool isInvalid() const { return code_ == FloatRegisters::invalid_freg; }
  bool isSimd128() const { return false; }

  FloatRegister doubleOverlay() const;
  FloatRegister singleOverlay() const;

  FloatRegister asSingle() const { return singleOverlay(); }
  FloatRegister asDouble() const { return doubleOverlay(); }
  FloatRegister asSimd128() const { MOZ_CRASH("NYI"); }

  Code code() const {
    MOZ_ASSERT(isNotOdd());
    return Code((code_ >> 1) | (kind_ << 4));
  }
  Encoding encoding() const {
    MOZ_ASSERT(!isInvalid());
    return code_;
  }
  uint32_t id() const {
    MOZ_ASSERT(!isInvalid());
    return code_;
  }
  static FloatRegister FromCode(uint32_t i) {
    uint32_t code = i & 15;
    uint32_t kind = i >> 4;
    return FloatRegister(Encoding(code << 1), RegType(kind));
  }

  static FloatRegister FromIndex(uint32_t index, RegType kind) {
    MOZ_ASSERT(index < 16);
    return FloatRegister(Encoding(index << 1), kind);
  }

  bool volatile_() const {
    return !!((SetType(1) << code()) & FloatRegisters::VolatileMask);
  }
  const char* name() const { return FloatRegisters::GetName(code_); }
  bool operator!=(const FloatRegister& other) const {
    return other.kind_ != kind_ || code_ != other.code_;
  }
  bool aliases(const FloatRegister& other) {
    MOZ_ASSERT(isNotOdd());
    return code_ == other.code_;
  }
  uint32_t numAliased() const {
    MOZ_ASSERT(isNotOdd());
    return 2;
  }
  FloatRegister aliased(uint32_t aliasIdx) {
    MOZ_ASSERT(isNotOdd());

    if (aliasIdx == 0) {
      return *this;
    }
    MOZ_ASSERT(aliasIdx == 1);
    if (isDouble()) {
      return singleOverlay();
    }
    return doubleOverlay();
  }
  uint32_t numAlignedAliased() const {
    MOZ_ASSERT(isNotOdd());
    return 2;
  }
  FloatRegister alignedAliased(uint32_t aliasIdx) {
    MOZ_ASSERT(isNotOdd());

    if (aliasIdx == 0) {
      return *this;
    }
    MOZ_ASSERT(aliasIdx == 1);
    if (isDouble()) {
      return singleOverlay();
    }
    return doubleOverlay();
  }

  SetType alignedOrDominatedAliasedSet() const {
    MOZ_ASSERT(isNotOdd());
    return (SetType(1) << (code_ >> 1)) *
           ((1 << FloatRegisters::TotalSingle) + 1);
  }

  static constexpr RegTypeName DefaultType = RegTypeName::Float64;

  template <RegTypeName = DefaultType>
  static SetType LiveAsIndexableSet(SetType s) {
    return SetType(0);
  }

  template <RegTypeName Name = DefaultType>
  static SetType AllocatableAsIndexableSet(SetType s) {
    static_assert(Name != RegTypeName::Any, "Allocatable set are not iterable");
    return LiveAsIndexableSet<Name>(s);
  }

  static Code FromName(const char* name) {
    return FloatRegisters::FromName(name);
  }
  static TypedRegisterSet<FloatRegister> ReduceSetForPush(
      const TypedRegisterSet<FloatRegister>& s);
  static uint32_t GetPushSizeInBytes(const TypedRegisterSet<FloatRegister>& s);
  uint32_t getRegisterDumpOffsetInBytes();
};

template <>
inline FloatRegister::SetType
FloatRegister::LiveAsIndexableSet<RegTypeName::Float32>(SetType set) {
  return set & FloatRegisters::AllSingleMask;
}

template <>
inline FloatRegister::SetType
FloatRegister::LiveAsIndexableSet<RegTypeName::Float64>(SetType set) {
  return set & FloatRegisters::AllDoubleMask;
}

template <>
inline FloatRegister::SetType
FloatRegister::LiveAsIndexableSet<RegTypeName::Any>(SetType set) {
  return set;
}

template <>
inline FloatRegister::SetType
FloatRegister::AllocatableAsIndexableSet<RegTypeName::Float32>(SetType set) {
  // Single registers are not dominating any smaller registers, thus masking
  // is enough to convert an allocatable set into a set of register list all
  // single register available.
  return set & FloatRegisters::AllSingleMask;
}

template <>
inline FloatRegister::SetType
FloatRegister::AllocatableAsIndexableSet<RegTypeName::Float64>(SetType set) {
  return set & FloatRegisters::AllDoubleMask;
}

// In order to handle functions such as int(*)(int, double) where the first
// argument is a general purpose register, and the second argument is a floating
// point register, we have to store the double content into 2 general purpose
// registers, namely a2 and a3.
#define JS_CODEGEN_REGISTER_PAIR 1

}  // namespace jit
}  // namespace js

#endif /* jit_mips32_Architecture_mips32_h */