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
3 // found in the LICENSE file.
5 #ifndef V8_COMPILER_LINKAGE_IMPL_H_
6 #define V8_COMPILER_LINKAGE_IMPL_H_
14 static LinkageLocation TaggedStackSlot(int index) {
16 return LinkageLocation(kMachineTagged, index);
19 static LinkageLocation TaggedRegisterLocation(Register reg) {
20 return LinkageLocation(kMachineTagged, Register::ToAllocationIndex(reg));
23 static inline LinkageLocation WordRegisterLocation(Register reg) {
24 return LinkageLocation(MachineOperatorBuilder::pointer_rep(),
25 Register::ToAllocationIndex(reg));
28 static LinkageLocation UnconstrainedRegister(MachineType rep) {
29 return LinkageLocation(rep, LinkageLocation::ANY_REGISTER);
32 static const RegList kNoCalleeSaved = 0;
34 // TODO(turbofan): cache call descriptors for JSFunction calls.
35 template <typename LinkageTraits>
36 static CallDescriptor* GetJSCallDescriptor(Zone* zone, int parameter_count) {
37 const int jsfunction_count = 1;
38 const int context_count = 1;
39 int input_count = jsfunction_count + parameter_count + context_count;
41 const int return_count = 1;
42 LinkageLocation* locations =
43 zone->NewArray<LinkageLocation>(return_count + input_count);
47 TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
49 TaggedRegisterLocation(LinkageTraits::JSCallFunctionReg());
51 for (int i = 0; i < parameter_count; i++) {
52 // All parameters to JS calls go on the stack.
53 int spill_slot_index = i - parameter_count;
54 locations[index++] = TaggedStackSlot(spill_slot_index);
56 locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg());
58 // TODO(titzer): refactor TurboFan graph to consider context a value input.
60 CallDescriptor(CallDescriptor::kCallJSFunction, // kind
61 return_count, // return_count
62 parameter_count, // parameter_count
63 input_count - context_count, // input_count
64 locations, // locations
65 Operator::kNoProperties, // properties
66 kNoCalleeSaved, // callee-saved registers
67 CallDescriptor::kCanDeoptimize); // deoptimization
71 // TODO(turbofan): cache call descriptors for runtime calls.
72 template <typename LinkageTraits>
73 static CallDescriptor* GetRuntimeCallDescriptor(
74 Zone* zone, Runtime::FunctionId function_id, int parameter_count,
75 Operator::Property properties,
76 CallDescriptor::DeoptimizationSupport can_deoptimize) {
77 const int code_count = 1;
78 const int function_count = 1;
79 const int num_args_count = 1;
80 const int context_count = 1;
81 const int input_count = code_count + parameter_count + function_count +
82 num_args_count + context_count;
84 const Runtime::Function* function = Runtime::FunctionForId(function_id);
85 const int return_count = function->result_size;
86 LinkageLocation* locations =
87 zone->NewArray<LinkageLocation>(return_count + input_count);
90 if (return_count > 0) {
92 TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
94 if (return_count > 1) {
96 TaggedRegisterLocation(LinkageTraits::ReturnValue2Reg());
99 DCHECK_LE(return_count, 2);
101 locations[index++] = UnconstrainedRegister(kMachineTagged); // CEntryStub
103 for (int i = 0; i < parameter_count; i++) {
104 // All parameters to runtime calls go on the stack.
105 int spill_slot_index = i - parameter_count;
106 locations[index++] = TaggedStackSlot(spill_slot_index);
109 TaggedRegisterLocation(LinkageTraits::RuntimeCallFunctionReg());
111 WordRegisterLocation(LinkageTraits::RuntimeCallArgCountReg());
112 locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg());
114 // TODO(titzer): refactor TurboFan graph to consider context a value input.
115 return new (zone) CallDescriptor(CallDescriptor::kCallCodeObject, // kind
116 return_count, // return_count
117 parameter_count, // parameter_count
118 input_count, // input_count
119 locations, // locations
120 properties, // properties
121 kNoCalleeSaved, // callee-saved registers
122 can_deoptimize, // deoptimization
127 // TODO(turbofan): cache call descriptors for code stub calls.
128 template <typename LinkageTraits>
129 static CallDescriptor* GetStubCallDescriptor(
130 Zone* zone, CodeStubInterfaceDescriptor* descriptor,
131 int stack_parameter_count,
132 CallDescriptor::DeoptimizationSupport can_deoptimize) {
133 int register_parameter_count = descriptor->GetEnvironmentParameterCount();
134 int parameter_count = register_parameter_count + stack_parameter_count;
135 const int code_count = 1;
136 const int context_count = 1;
137 int input_count = code_count + parameter_count + context_count;
139 const int return_count = 1;
140 LinkageLocation* locations =
141 zone->NewArray<LinkageLocation>(return_count + input_count);
145 TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
146 locations[index++] = UnconstrainedRegister(kMachineTagged); // code
147 for (int i = 0; i < parameter_count; i++) {
148 if (i < register_parameter_count) {
149 // The first parameters to code stub calls go in registers.
150 Register reg = descriptor->GetEnvironmentParameterRegister(i);
151 locations[index++] = TaggedRegisterLocation(reg);
153 // The rest of the parameters go on the stack.
154 int stack_slot = i - register_parameter_count - stack_parameter_count;
155 locations[index++] = TaggedStackSlot(stack_slot);
158 locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg());
160 // TODO(titzer): refactor TurboFan graph to consider context a value input.
162 CallDescriptor(CallDescriptor::kCallCodeObject, // kind
163 return_count, // return_count
164 parameter_count, // parameter_count
165 input_count, // input_count
166 locations, // locations
167 Operator::kNoProperties, // properties
168 kNoCalleeSaved, // callee-saved registers
169 can_deoptimize, // deoptimization
170 CodeStub::MajorName(descriptor->MajorKey(), false));
174 template <typename LinkageTraits>
175 static CallDescriptor* GetSimplifiedCDescriptor(
176 Zone* zone, int num_params, MachineType return_type,
177 const MachineType* param_types) {
178 LinkageLocation* locations =
179 zone->NewArray<LinkageLocation>(num_params + 2);
182 TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
183 locations[index++] = LinkageHelper::UnconstrainedRegister(
184 MachineOperatorBuilder::pointer_rep());
185 // TODO(dcarney): test with lots of parameters.
187 for (; i < LinkageTraits::CRegisterParametersLength() && i < num_params;
189 locations[index++] = LinkageLocation(
191 Register::ToAllocationIndex(LinkageTraits::CRegisterParameter(i)));
193 for (; i < num_params; i++) {
194 locations[index++] = LinkageLocation(param_types[i], -1 - i);
196 return new (zone) CallDescriptor(
197 CallDescriptor::kCallAddress, 1, num_params, num_params + 1, locations,
198 Operator::kNoProperties, LinkageTraits::CCalleeSaveRegisters(),
199 CallDescriptor::kCannotDeoptimize); // TODO(jarin) should deoptimize!
204 } // namespace v8::internal::compiler
206 #endif // V8_COMPILER_LINKAGE_IMPL_H_