Flutter Engine
The Flutter Engine
locations_helpers_test.cc
Go to the documentation of this file.
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
6#include "vm/dart_api_impl.h"
7#include "vm/dart_entry.h"
8#include "vm/unit_test.h"
9
10namespace dart {
11
12#define Reg(index) (static_cast<Register>(index))
13#define Fpu(index) (static_cast<FpuRegister>(index))
14
15#define ReqReg Location::RequiresRegister()
16#define ReqFpu Location::RequiresFpuRegister()
17#define RegLoc(index) Location::RegisterLocation(Reg(index))
18#define FpuLoc(index) Location::FpuRegisterLocation(Fpu(index))
19
21
23 LocationArray* arr = new LocationArray();
24 return arr;
25}
26
28 LocationArray* arr = new LocationArray();
29 arr->Add(loc0);
30 return arr;
31}
32
34 LocationArray* arr = new LocationArray();
35 arr->Add(loc0);
36 arr->Add(loc1);
37 return arr;
38}
39
42 const LocationArray* expected_inputs,
43 const LocationArray* expected_temps) {
45 EXPECT_EQ(expected_inputs->length(), locs->input_count());
46 for (intptr_t i = 0; i < expected_inputs->length(); i++) {
47 EXPECT(locs->in(i).Equals(expected_inputs->At(i)));
48 }
49 EXPECT_EQ(expected_temps->length(), locs->temp_count());
50 for (intptr_t i = 0; i < expected_temps->length(); i++) {
51 EXPECT(locs->temp(i).Equals(expected_temps->At(i)));
52 }
53}
54
55static void FillSummary(LocationSummary* locs,
57 const LocationArray* expected_inputs,
58 const LocationArray* expected_temps) {
59 locs->set_out(0, expected_output);
60 for (intptr_t i = 0; i < expected_inputs->length(); i++) {
61 locs->set_in(i, expected_inputs->At(i));
62 }
63 for (intptr_t i = 0; i < expected_temps->length(); i++) {
64 locs->set_temp(i, expected_temps->At(i));
65 }
66}
67
69 public:
70 virtual ~MockInstruction() {}
71
73 if (locs_ == nullptr) {
74 locs_ = MakeLocationSummary(Thread::Current()->zone(), false);
75 }
76 return locs_;
77 }
78
79 virtual LocationSummary* MakeLocationSummary(Zone* zone, bool opt) const = 0;
81
82 private:
83 LocationSummary* locs_;
84};
85
86#define INSTRUCTION_TEST(Name, Arity, Signature, ExpectedOut, ExpectedIn, \
87 ExpectedTemp, AllocatedOut, AllocatedIn, \
88 AllocatedTemp) \
89 class Name##Instr : public MockInstruction { \
90 public: \
91 LocationSummary* MakeLocationSummary(Zone* zone, bool opt) const; \
92 void EmitNativeCode(FlowGraphCompiler* compiler); \
93 virtual intptr_t InputCount() const { \
94 return Arity; \
95 } \
96 }; \
97 TEST_CASE(LocationsHelpers_##Name) { \
98 const Location expected_out = ExpectedOut; \
99 const LocationArray* expected_inputs = MakeLocationArray ExpectedIn; \
100 const LocationArray* expected_temps = MakeLocationArray ExpectedTemp; \
101 \
102 const Location allocated_out = AllocatedOut; \
103 const LocationArray* allocated_inputs = MakeLocationArray AllocatedIn; \
104 const LocationArray* allocated_temps = MakeLocationArray AllocatedTemp; \
105 \
106 Name##Instr* instr = new Name##Instr(); \
107 LocationSummary* locs = instr->locs(); \
108 \
109 ValidateSummary(locs, expected_out, expected_inputs, expected_temps); \
110 FillSummary(locs, allocated_out, allocated_inputs, allocated_temps); \
111 \
112 instr->EmitNativeCode(nullptr); \
113 } \
114 DEFINE_BACKEND(Name, Signature)
115
116// Reg -> Reg
118 1,
119 (Register out, Register in),
120 ReqReg,
121 (ReqReg),
122 (),
123 RegLoc(0),
124 (RegLoc(1)),
125 ()) {
126 EXPECT_EQ(Reg(0), out);
127 EXPECT_EQ(Reg(1), in);
128}
129
130// (Reg, Fpu) -> Reg
132 2,
133 (Register out, Register in0, FpuRegister in1),
134 ReqReg,
136 (),
137 RegLoc(0),
138 (RegLoc(1), FpuLoc(2)),
139 ()) {
140 EXPECT_EQ(Reg(0), out);
141 EXPECT_EQ(Reg(1), in0);
142 EXPECT_EQ(Fpu(2), in1);
143}
144
145// (Fpu, Reg) -> Reg
147 2,
148 (Register out, FpuRegister in0, Register in1),
149 ReqReg,
150 (ReqFpu, ReqReg),
151 (),
152 RegLoc(0),
153 (FpuLoc(1), RegLoc(2)),
154 ()) {
155 EXPECT_EQ(Reg(0), out);
156 EXPECT_EQ(Fpu(1), in0);
157 EXPECT_EQ(Reg(2), in1);
158}
159
160// -> Reg(3)
162 0,
163 (Fixed<Register, Reg(3)> out),
164 RegLoc(3),
165 (),
166 (),
167 RegLoc(3),
168 (),
169 ()) {
170 EXPECT_EQ(Reg(3), Reg(out));
171}
172
173// Fpu(3) -> Fpu
175 1,
177 ReqFpu,
178 (FpuLoc(3)),
179 (),
180 FpuLoc(0),
181 (FpuLoc(3)),
182 ()) {
183 EXPECT_EQ(Fpu(0), out);
184 EXPECT_EQ(Fpu(3), Fpu(in));
185}
186
187// Reg -> SameAsFirstInput
189 2,
192 (ReqReg, ReqReg),
193 (),
194 RegLoc(0),
195 (RegLoc(0), RegLoc(1)),
196 ()) {
197 EXPECT_EQ(Reg(0), in0);
198 EXPECT_EQ(Reg(1), in1);
199}
200
201// {Temps: Fpu, Reg} (Reg, Fpu) -> Reg
203 2,
204 (Register out,
205 Register in0,
206 FpuRegister in1,
207 Temp<FpuRegister> temp0,
208 Temp<Register> temp1),
209 ReqReg,
210 (ReqReg, ReqFpu),
211 (ReqFpu, ReqReg),
212 RegLoc(0),
213 (RegLoc(1), FpuLoc(2)),
214 (FpuLoc(3), RegLoc(4))) {
215 EXPECT_EQ(Reg(0), out);
216 EXPECT_EQ(Reg(1), in0);
217 EXPECT_EQ(Fpu(2), in1);
218 EXPECT_EQ(Fpu(3), Fpu(temp0));
219 EXPECT_EQ(Reg(4), Reg(temp1));
220}
221
222// {Temps: Fpu(3)} -> Fpu
224 0,
225 (FpuRegister out, Temp<Fixed<FpuRegister, Fpu(3)> > temp),
226 ReqFpu,
227 (),
228 (FpuLoc(3)),
229 FpuLoc(4),
230 (),
231 (FpuLoc(3))) {
232 EXPECT_EQ(Fpu(4), out);
233 EXPECT_EQ(Fpu(3), Fpu(temp));
234}
235
236} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
void Add(const T &value)
const T & At(intptr_t index) const
intptr_t length() const
Location temp(intptr_t index) const
Definition: locations.h:882
Location out(intptr_t index) const
Definition: locations.h:903
intptr_t input_count() const
Definition: locations.h:864
void set_temp(intptr_t index, Location loc)
Definition: locations.h:894
intptr_t temp_count() const
Definition: locations.h:880
void set_out(intptr_t index, Location loc)
Definition: locations.cc:232
Location in(intptr_t index) const
Definition: locations.h:866
void set_in(intptr_t index, Location loc)
Definition: locations.cc:205
static Location SameAsFirstInput()
Definition: locations.h:382
bool Equals(Location other) const
Definition: locations.h:519
static Location RequiresFpuRegister()
Definition: locations.h:369
virtual void EmitNativeCode(FlowGraphCompiler *compiler)=0
virtual LocationSummary * MakeLocationSummary(Zone *zone, bool opt) const =0
static Thread * Current()
Definition: thread.h:362
#define FpuLoc(index)
#define ReqFpu
#define Reg(index)
#define ReqReg
#define RegLoc(index)
#define Fpu(index)
Definition: dart_vm.cc:33
static void ValidateSummary(LocationSummary *locs, Location expected_output, const LocationArray *expected_inputs, const LocationArray *expected_temps)
static void FillSummary(LocationSummary *locs, Location expected_output, const LocationArray *expected_inputs, const LocationArray *expected_temps)
ZoneGrowableArray< Location > LocationArray
INSTRUCTION_TEST(Unary, 1,(Register out, Register in), ReqReg,(ReqReg),(), RegLoc(0),(RegLoc(1)),())
static LocationArray * MakeLocationArray()