blob: 007324eb686edd3ad2c9b950aab214fa302fb735 [file] [log] [blame]
Andreas Gampe878d58c2015-01-15 23:24:00 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_
18#define ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_
19
20#include "locations.h"
21#include "nodes.h"
22#include "utils/arm64/assembler_arm64.h"
23#include "a64/disasm-a64.h"
24#include "a64/macro-assembler-a64.h"
25
26namespace art {
27namespace arm64 {
28namespace helpers {
29
Andreas Gampe878d58c2015-01-15 23:24:00 -080030// Convenience helpers to ease conversion to and from VIXL operands.
31static_assert((SP == 31) && (WSP == 31) && (XZR == 32) && (WZR == 32),
32 "Unexpected values for register codes.");
33
34static inline int VIXLRegCodeFromART(int code) {
35 if (code == SP) {
36 return vixl::kSPRegInternalCode;
37 }
38 if (code == XZR) {
39 return vixl::kZeroRegCode;
40 }
41 return code;
42}
43
44static inline int ARTRegCodeFromVIXL(int code) {
45 if (code == vixl::kSPRegInternalCode) {
46 return SP;
47 }
48 if (code == vixl::kZeroRegCode) {
49 return XZR;
50 }
51 return code;
52}
53
54static inline vixl::Register XRegisterFrom(Location location) {
55 DCHECK(location.IsRegister());
56 return vixl::Register::XRegFromCode(VIXLRegCodeFromART(location.reg()));
57}
58
59static inline vixl::Register WRegisterFrom(Location location) {
60 DCHECK(location.IsRegister());
61 return vixl::Register::WRegFromCode(VIXLRegCodeFromART(location.reg()));
62}
63
64static inline vixl::Register RegisterFrom(Location location, Primitive::Type type) {
Alexandre Rames542361f2015-01-29 16:57:31 +000065 DCHECK(type != Primitive::kPrimVoid && !Primitive::IsFloatingPointType(type));
Andreas Gampe878d58c2015-01-15 23:24:00 -080066 return type == Primitive::kPrimLong ? XRegisterFrom(location) : WRegisterFrom(location);
67}
68
69static inline vixl::Register OutputRegister(HInstruction* instr) {
70 return RegisterFrom(instr->GetLocations()->Out(), instr->GetType());
71}
72
73static inline vixl::Register InputRegisterAt(HInstruction* instr, int input_index) {
74 return RegisterFrom(instr->GetLocations()->InAt(input_index),
75 instr->InputAt(input_index)->GetType());
76}
77
78static inline vixl::FPRegister DRegisterFrom(Location location) {
79 DCHECK(location.IsFpuRegister());
80 return vixl::FPRegister::DRegFromCode(location.reg());
81}
82
83static inline vixl::FPRegister SRegisterFrom(Location location) {
84 DCHECK(location.IsFpuRegister());
85 return vixl::FPRegister::SRegFromCode(location.reg());
86}
87
88static inline vixl::FPRegister FPRegisterFrom(Location location, Primitive::Type type) {
Alexandre Rames542361f2015-01-29 16:57:31 +000089 DCHECK(Primitive::IsFloatingPointType(type));
Andreas Gampe878d58c2015-01-15 23:24:00 -080090 return type == Primitive::kPrimDouble ? DRegisterFrom(location) : SRegisterFrom(location);
91}
92
93static inline vixl::FPRegister OutputFPRegister(HInstruction* instr) {
94 return FPRegisterFrom(instr->GetLocations()->Out(), instr->GetType());
95}
96
97static inline vixl::FPRegister InputFPRegisterAt(HInstruction* instr, int input_index) {
98 return FPRegisterFrom(instr->GetLocations()->InAt(input_index),
99 instr->InputAt(input_index)->GetType());
100}
101
102static inline vixl::CPURegister CPURegisterFrom(Location location, Primitive::Type type) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000103 return Primitive::IsFloatingPointType(type) ? vixl::CPURegister(FPRegisterFrom(location, type))
104 : vixl::CPURegister(RegisterFrom(location, type));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800105}
106
107static inline vixl::CPURegister OutputCPURegister(HInstruction* instr) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000108 return Primitive::IsFloatingPointType(instr->GetType())
109 ? static_cast<vixl::CPURegister>(OutputFPRegister(instr))
110 : static_cast<vixl::CPURegister>(OutputRegister(instr));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800111}
112
113static inline vixl::CPURegister InputCPURegisterAt(HInstruction* instr, int index) {
Alexandre Rames542361f2015-01-29 16:57:31 +0000114 return Primitive::IsFloatingPointType(instr->InputAt(index)->GetType())
Andreas Gampe878d58c2015-01-15 23:24:00 -0800115 ? static_cast<vixl::CPURegister>(InputFPRegisterAt(instr, index))
116 : static_cast<vixl::CPURegister>(InputRegisterAt(instr, index));
117}
118
119static inline int64_t Int64ConstantFrom(Location location) {
120 HConstant* instr = location.GetConstant();
121 return instr->IsIntConstant() ? instr->AsIntConstant()->GetValue()
122 : instr->AsLongConstant()->GetValue();
123}
124
125static inline vixl::Operand OperandFrom(Location location, Primitive::Type type) {
126 if (location.IsRegister()) {
127 return vixl::Operand(RegisterFrom(location, type));
128 } else {
129 return vixl::Operand(Int64ConstantFrom(location));
130 }
131}
132
133static inline vixl::Operand InputOperandAt(HInstruction* instr, int input_index) {
134 return OperandFrom(instr->GetLocations()->InAt(input_index),
135 instr->InputAt(input_index)->GetType());
136}
137
138static inline vixl::MemOperand StackOperandFrom(Location location) {
139 return vixl::MemOperand(vixl::sp, location.GetStackIndex());
140}
141
142static inline vixl::MemOperand HeapOperand(const vixl::Register& base, size_t offset = 0) {
143 // A heap reference must be 32bit, so fit in a W register.
144 DCHECK(base.IsW());
145 return vixl::MemOperand(base.X(), offset);
146}
147
148static inline vixl::MemOperand HeapOperand(const vixl::Register& base, Offset offset) {
149 return HeapOperand(base, offset.SizeValue());
150}
151
152static inline vixl::MemOperand HeapOperandFrom(Location location, Offset offset) {
153 return HeapOperand(RegisterFrom(location, Primitive::kPrimNot), offset);
154}
155
156static inline Location LocationFrom(const vixl::Register& reg) {
157 return Location::RegisterLocation(ARTRegCodeFromVIXL(reg.code()));
158}
159
160static inline Location LocationFrom(const vixl::FPRegister& fpreg) {
161 return Location::FpuRegisterLocation(fpreg.code());
162}
163
164static inline vixl::Operand OperandFromMemOperand(const vixl::MemOperand& mem_op) {
165 if (mem_op.IsImmediateOffset()) {
166 return vixl::Operand(mem_op.offset());
167 } else {
168 DCHECK(mem_op.IsRegisterOffset());
169 if (mem_op.extend() != vixl::NO_EXTEND) {
170 return vixl::Operand(mem_op.regoffset(), mem_op.extend(), mem_op.shift_amount());
171 } else if (mem_op.shift() != vixl::NO_SHIFT) {
172 return vixl::Operand(mem_op.regoffset(), mem_op.shift(), mem_op.shift_amount());
173 } else {
174 LOG(FATAL) << "Should not reach here";
175 UNREACHABLE();
176 }
177 }
178}
179
180} // namespace helpers
181} // namespace arm64
182} // namespace art
183
184#endif // ART_COMPILER_OPTIMIZING_COMMON_ARM64_H_