OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_LINKAGE_IMPL_H_ | 5 #ifndef V8_COMPILER_LINKAGE_IMPL_H_ |
6 #define V8_COMPILER_LINKAGE_IMPL_H_ | 6 #define V8_COMPILER_LINKAGE_IMPL_H_ |
7 | 7 |
8 namespace v8 { | 8 namespace v8 { |
9 namespace internal { | 9 namespace internal { |
10 namespace compiler { | 10 namespace compiler { |
11 | 11 |
| 12 // TODO(titzer): replace uses of int with size_t in LinkageHelper. |
| 13 template <typename LinkageTraits> |
12 class LinkageHelper { | 14 class LinkageHelper { |
13 public: | 15 public: |
14 static LinkageLocation TaggedStackSlot(int index) { | |
15 DCHECK(index < 0); | |
16 return LinkageLocation(kMachAnyTagged, index); | |
17 } | |
18 | |
19 static LinkageLocation TaggedRegisterLocation(Register reg) { | |
20 return LinkageLocation(kMachAnyTagged, Register::ToAllocationIndex(reg)); | |
21 } | |
22 | |
23 static inline LinkageLocation WordRegisterLocation(Register reg) { | |
24 return LinkageLocation(kMachPtr, Register::ToAllocationIndex(reg)); | |
25 } | |
26 | |
27 static LinkageLocation UnconstrainedRegister(MachineType rep) { | |
28 return LinkageLocation(rep, LinkageLocation::ANY_REGISTER); | |
29 } | |
30 | |
31 static const RegList kNoCalleeSaved = 0; | 16 static const RegList kNoCalleeSaved = 0; |
32 | 17 |
| 18 static void AddReturnLocations(LocationSignature::Builder* locations) { |
| 19 DCHECK(locations->return_count_ <= 2); |
| 20 if (locations->return_count_ > 0) { |
| 21 locations->AddReturn(regloc(LinkageTraits::ReturnValueReg())); |
| 22 } |
| 23 if (locations->return_count_ > 1) { |
| 24 locations->AddReturn(regloc(LinkageTraits::ReturnValue2Reg())); |
| 25 } |
| 26 } |
| 27 |
33 // TODO(turbofan): cache call descriptors for JSFunction calls. | 28 // TODO(turbofan): cache call descriptors for JSFunction calls. |
34 template <typename LinkageTraits> | 29 static CallDescriptor* GetJSCallDescriptor(Zone* zone, |
35 static CallDescriptor* GetJSCallDescriptor(Zone* zone, int parameter_count) { | 30 int js_parameter_count) { |
36 const int jsfunction_count = 1; | 31 const size_t return_count = 1; |
37 const int context_count = 1; | 32 const size_t context_count = 1; |
38 int input_count = jsfunction_count + parameter_count + context_count; | 33 const size_t parameter_count = js_parameter_count + context_count; |
39 | 34 |
40 const int return_count = 1; | 35 LocationSignature::Builder locations(zone, return_count, parameter_count); |
41 LinkageLocation* locations = | 36 MachineSignature::Builder types(zone, return_count, parameter_count); |
42 zone->NewArray<LinkageLocation>(return_count + input_count); | 37 |
43 | 38 // Add returns. |
44 int index = 0; | 39 AddReturnLocations(&locations); |
45 locations[index++] = | 40 for (size_t i = 0; i < return_count; i++) { |
46 TaggedRegisterLocation(LinkageTraits::ReturnValueReg()); | 41 types.AddReturn(kMachAnyTagged); |
47 locations[index++] = | 42 } |
48 TaggedRegisterLocation(LinkageTraits::JSCallFunctionReg()); | 43 |
49 | 44 // All parameters to JS calls go on the stack. |
50 for (int i = 0; i < parameter_count; i++) { | 45 for (int i = 0; i < js_parameter_count; i++) { |
51 // All parameters to JS calls go on the stack. | 46 int spill_slot_index = i - js_parameter_count; |
52 int spill_slot_index = i - parameter_count; | 47 locations.AddParam(stackloc(spill_slot_index)); |
53 locations[index++] = TaggedStackSlot(spill_slot_index); | 48 types.AddParam(kMachAnyTagged); |
54 } | 49 } |
55 locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg()); | 50 // Add context. |
56 | 51 locations.AddParam(regloc(LinkageTraits::ContextReg())); |
57 // TODO(titzer): refactor TurboFan graph to consider context a value input. | 52 types.AddParam(kMachAnyTagged); |
58 return new (zone) | 53 |
59 CallDescriptor(CallDescriptor::kCallJSFunction, // kind | 54 // The target for JS function calls is the JSFunction object. |
60 return_count, // return_count | 55 MachineType target_type = kMachAnyTagged; |
61 parameter_count, // parameter_count | 56 LinkageLocation target_loc = regloc(LinkageTraits::JSCallFunctionReg()); |
62 input_count - context_count, // input_count | 57 return new (zone) CallDescriptor(CallDescriptor::kCallJSFunction, // kind |
63 locations, // locations | 58 target_type, // target MachineType |
64 Operator::kNoProperties, // properties | 59 target_loc, // target location |
65 kNoCalleeSaved, // callee-saved registers | 60 types.Build(), // machine_sig |
66 CallDescriptor::kNeedsFrameState); // flags | 61 locations.Build(), // location_sig |
| 62 js_parameter_count, // js_parameter_count |
| 63 Operator::kNoProperties, // properties |
| 64 kNoCalleeSaved, // callee-saved |
| 65 CallDescriptor::kNeedsFrameState, // flags |
| 66 "js-call"); |
67 } | 67 } |
68 | 68 |
69 | 69 |
70 // TODO(turbofan): cache call descriptors for runtime calls. | 70 // TODO(turbofan): cache call descriptors for runtime calls. |
71 template <typename LinkageTraits> | |
72 static CallDescriptor* GetRuntimeCallDescriptor( | 71 static CallDescriptor* GetRuntimeCallDescriptor( |
73 Zone* zone, Runtime::FunctionId function_id, int parameter_count, | 72 Zone* zone, Runtime::FunctionId function_id, int js_parameter_count, |
74 Operator::Properties properties) { | 73 Operator::Properties properties) { |
75 const int code_count = 1; | 74 const size_t function_count = 1; |
76 const int function_count = 1; | 75 const size_t num_args_count = 1; |
77 const int num_args_count = 1; | 76 const size_t context_count = 1; |
78 const int context_count = 1; | 77 const size_t parameter_count = function_count + |
79 const int input_count = code_count + parameter_count + function_count + | 78 static_cast<size_t>(js_parameter_count) + |
80 num_args_count + context_count; | 79 num_args_count + context_count; |
81 | 80 |
82 const Runtime::Function* function = Runtime::FunctionForId(function_id); | 81 const Runtime::Function* function = Runtime::FunctionForId(function_id); |
83 const int return_count = function->result_size; | 82 const size_t return_count = static_cast<size_t>(function->result_size); |
84 LinkageLocation* locations = | 83 |
85 zone->NewArray<LinkageLocation>(return_count + input_count); | 84 LocationSignature::Builder locations(zone, return_count, parameter_count); |
86 | 85 MachineSignature::Builder types(zone, return_count, parameter_count); |
87 int index = 0; | 86 |
88 if (return_count > 0) { | 87 // Add returns. |
89 locations[index++] = | 88 AddReturnLocations(&locations); |
90 TaggedRegisterLocation(LinkageTraits::ReturnValueReg()); | 89 for (size_t i = 0; i < return_count; i++) { |
91 } | 90 types.AddReturn(kMachAnyTagged); |
92 if (return_count > 1) { | 91 } |
93 locations[index++] = | 92 |
94 TaggedRegisterLocation(LinkageTraits::ReturnValue2Reg()); | 93 // All parameters to the runtime call go on the stack. |
95 } | 94 for (int i = 0; i < js_parameter_count; i++) { |
96 | 95 locations.AddParam(stackloc(i - js_parameter_count)); |
97 DCHECK_LE(return_count, 2); | 96 types.AddParam(kMachAnyTagged); |
98 | 97 } |
99 locations[index++] = UnconstrainedRegister(kMachAnyTagged); // CEntryStub | 98 // Add runtime function itself. |
100 | 99 locations.AddParam(regloc(LinkageTraits::RuntimeCallFunctionReg())); |
101 for (int i = 0; i < parameter_count; i++) { | 100 types.AddParam(kMachAnyTagged); |
102 // All parameters to runtime calls go on the stack. | 101 |
103 int spill_slot_index = i - parameter_count; | 102 // Add runtime call argument count. |
104 locations[index++] = TaggedStackSlot(spill_slot_index); | 103 locations.AddParam(regloc(LinkageTraits::RuntimeCallArgCountReg())); |
105 } | 104 types.AddParam(kMachPtr); |
106 locations[index++] = | 105 |
107 TaggedRegisterLocation(LinkageTraits::RuntimeCallFunctionReg()); | 106 // Add context. |
108 locations[index++] = | 107 locations.AddParam(regloc(LinkageTraits::ContextReg())); |
109 WordRegisterLocation(LinkageTraits::RuntimeCallArgCountReg()); | 108 types.AddParam(kMachAnyTagged); |
110 locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg()); | |
111 | 109 |
112 CallDescriptor::Flags flags = Linkage::NeedsFrameState(function_id) | 110 CallDescriptor::Flags flags = Linkage::NeedsFrameState(function_id) |
113 ? CallDescriptor::kNeedsFrameState | 111 ? CallDescriptor::kNeedsFrameState |
114 : CallDescriptor::kNoFlags; | 112 : CallDescriptor::kNoFlags; |
115 | 113 |
116 // TODO(titzer): refactor TurboFan graph to consider context a value input. | 114 // The target for runtime calls is a code object. |
| 115 MachineType target_type = kMachAnyTagged; |
| 116 LinkageLocation target_loc = LinkageLocation::AnyRegister(); |
117 return new (zone) CallDescriptor(CallDescriptor::kCallCodeObject, // kind | 117 return new (zone) CallDescriptor(CallDescriptor::kCallCodeObject, // kind |
118 return_count, // return_count | 118 target_type, // target MachineType |
119 parameter_count, // parameter_count | 119 target_loc, // target location |
120 input_count, // input_count | 120 types.Build(), // machine_sig |
121 locations, // locations | 121 locations.Build(), // location_sig |
122 properties, // properties | 122 js_parameter_count, // js_parameter_count |
123 kNoCalleeSaved, // callee-saved registers | 123 properties, // properties |
124 flags, // flags | 124 kNoCalleeSaved, // callee-saved |
125 function->name); | 125 flags, // flags |
| 126 function->name); // debug name |
126 } | 127 } |
127 | 128 |
128 | 129 |
129 // TODO(turbofan): cache call descriptors for code stub calls. | 130 // TODO(turbofan): cache call descriptors for code stub calls. |
130 template <typename LinkageTraits> | |
131 static CallDescriptor* GetStubCallDescriptor( | 131 static CallDescriptor* GetStubCallDescriptor( |
132 Zone* zone, CodeStubInterfaceDescriptor* descriptor, | 132 Zone* zone, CodeStubInterfaceDescriptor* descriptor, |
133 int stack_parameter_count, CallDescriptor::Flags flags) { | 133 int stack_parameter_count, CallDescriptor::Flags flags) { |
134 int register_parameter_count = descriptor->GetEnvironmentParameterCount(); | 134 const int register_parameter_count = |
135 int parameter_count = register_parameter_count + stack_parameter_count; | 135 descriptor->GetEnvironmentParameterCount(); |
136 const int code_count = 1; | 136 const int js_parameter_count = |
| 137 register_parameter_count + stack_parameter_count; |
137 const int context_count = 1; | 138 const int context_count = 1; |
138 int input_count = code_count + parameter_count + context_count; | 139 const size_t return_count = 1; |
139 | 140 const size_t parameter_count = |
140 const int return_count = 1; | 141 static_cast<size_t>(js_parameter_count + context_count); |
141 LinkageLocation* locations = | 142 |
142 zone->NewArray<LinkageLocation>(return_count + input_count); | 143 LocationSignature::Builder locations(zone, return_count, parameter_count); |
143 | 144 MachineSignature::Builder types(zone, return_count, parameter_count); |
144 int index = 0; | 145 |
145 locations[index++] = | 146 // Add return location. |
146 TaggedRegisterLocation(LinkageTraits::ReturnValueReg()); | 147 AddReturnLocations(&locations); |
147 locations[index++] = UnconstrainedRegister(kMachAnyTagged); // code | 148 types.AddReturn(kMachAnyTagged); |
148 for (int i = 0; i < parameter_count; i++) { | 149 |
| 150 // Add parameters in registers and on the stack. |
| 151 for (int i = 0; i < js_parameter_count; i++) { |
149 if (i < register_parameter_count) { | 152 if (i < register_parameter_count) { |
150 // The first parameters to code stub calls go in registers. | 153 // The first parameters go in registers. |
151 Register reg = descriptor->GetEnvironmentParameterRegister(i); | 154 Register reg = descriptor->GetEnvironmentParameterRegister(i); |
152 locations[index++] = TaggedRegisterLocation(reg); | 155 locations.AddParam(regloc(reg)); |
153 } else { | 156 } else { |
154 // The rest of the parameters go on the stack. | 157 // The rest of the parameters go on the stack. |
155 int stack_slot = i - register_parameter_count - stack_parameter_count; | 158 int stack_slot = i - register_parameter_count - stack_parameter_count; |
156 locations[index++] = TaggedStackSlot(stack_slot); | 159 locations.AddParam(stackloc(stack_slot)); |
157 } | 160 } |
158 } | 161 types.AddParam(kMachAnyTagged); |
159 locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg()); | 162 } |
160 | 163 // Add context. |
161 // TODO(titzer): refactor TurboFan graph to consider context a value input. | 164 locations.AddParam(regloc(LinkageTraits::ContextReg())); |
| 165 types.AddParam(kMachAnyTagged); |
| 166 |
| 167 // The target for stub calls is a code object. |
| 168 MachineType target_type = kMachAnyTagged; |
| 169 LinkageLocation target_loc = LinkageLocation::AnyRegister(); |
162 return new (zone) | 170 return new (zone) |
163 CallDescriptor(CallDescriptor::kCallCodeObject, // kind | 171 CallDescriptor(CallDescriptor::kCallCodeObject, // kind |
164 return_count, // return_count | 172 target_type, // target MachineType |
165 parameter_count, // parameter_count | 173 target_loc, // target location |
166 input_count, // input_count | 174 types.Build(), // machine_sig |
167 locations, // locations | 175 locations.Build(), // location_sig |
| 176 js_parameter_count, // js_parameter_count |
168 Operator::kNoProperties, // properties | 177 Operator::kNoProperties, // properties |
169 kNoCalleeSaved, // callee-saved registers | 178 kNoCalleeSaved, // callee-saved registers |
170 flags, // flags | 179 flags, // flags |
171 CodeStub::MajorName(descriptor->MajorKey(), false)); | 180 CodeStub::MajorName(descriptor->MajorKey(), false)); |
172 } | 181 } |
173 | 182 |
174 | 183 static CallDescriptor* GetSimplifiedCDescriptor(Zone* zone, |
175 template <typename LinkageTraits> | 184 MachineSignature* msig) { |
176 static CallDescriptor* GetSimplifiedCDescriptor( | 185 LocationSignature::Builder locations(zone, msig->return_count(), |
177 Zone* zone, int num_params, MachineType return_type, | 186 msig->parameter_count()); |
178 const MachineType* param_types) { | 187 // Add return location(s). |
179 LinkageLocation* locations = | 188 AddReturnLocations(&locations); |
180 zone->NewArray<LinkageLocation>(num_params + 2); | 189 |
181 int index = 0; | 190 // Add register and/or stack parameter(s). |
182 locations[index++] = | 191 const int parameter_count = static_cast<int>(msig->parameter_count()); |
183 TaggedRegisterLocation(LinkageTraits::ReturnValueReg()); | 192 for (int i = 0; i < parameter_count; i++) { |
184 locations[index++] = LinkageHelper::UnconstrainedRegister(kMachPtr); | 193 if (i < LinkageTraits::CRegisterParametersLength()) { |
185 // TODO(dcarney): test with lots of parameters. | 194 locations.AddParam(regloc(LinkageTraits::CRegisterParameter(i))); |
186 int i = 0; | 195 } else { |
187 for (; i < LinkageTraits::CRegisterParametersLength() && i < num_params; | 196 locations.AddParam(stackloc(-1 - i)); |
188 i++) { | 197 } |
189 locations[index++] = LinkageLocation( | 198 } |
190 param_types[i], | 199 |
191 Register::ToAllocationIndex(LinkageTraits::CRegisterParameter(i))); | 200 // The target for C calls is always an address (i.e. machine pointer). |
192 } | 201 MachineType target_type = kMachPtr; |
193 for (; i < num_params; i++) { | 202 LinkageLocation target_loc = LinkageLocation::AnyRegister(); |
194 locations[index++] = LinkageLocation(param_types[i], -1 - i); | 203 return new (zone) CallDescriptor(CallDescriptor::kCallAddress, // kind |
195 } | 204 target_type, // target MachineType |
196 return new (zone) CallDescriptor( | 205 target_loc, // target location |
197 CallDescriptor::kCallAddress, 1, num_params, num_params + 1, locations, | 206 msig, // machine_sig |
198 Operator::kNoProperties, LinkageTraits::CCalleeSaveRegisters(), | 207 locations.Build(), // location_sig |
199 CallDescriptor::kNoFlags); // TODO(jarin) should deoptimize! | 208 0, // js_parameter_count |
| 209 Operator::kNoProperties, // properties |
| 210 LinkageTraits::CCalleeSaveRegisters(), |
| 211 CallDescriptor::kNoFlags, "c-call"); |
| 212 } |
| 213 |
| 214 static LinkageLocation regloc(Register reg) { |
| 215 return LinkageLocation(Register::ToAllocationIndex(reg)); |
| 216 } |
| 217 |
| 218 static LinkageLocation stackloc(int i) { |
| 219 DCHECK_LT(i, 0); |
| 220 return LinkageLocation(i); |
200 } | 221 } |
201 }; | 222 }; |
202 } // namespace compiler | 223 } // namespace compiler |
203 } // namespace internal | 224 } // namespace internal |
204 } // namespace v8 | 225 } // namespace v8 |
205 | 226 |
206 #endif // V8_COMPILER_LINKAGE_IMPL_H_ | 227 #endif // V8_COMPILER_LINKAGE_IMPL_H_ |
OLD | NEW |