summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/linkage-impl.h
blob: e7aafc3885d8415554a170c8c53144260b49fa9d (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
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_LINKAGE_IMPL_H_
#define V8_COMPILER_LINKAGE_IMPL_H_

namespace v8 {
namespace internal {
namespace compiler {

class LinkageHelper {
 public:
  static LinkageLocation TaggedStackSlot(int index) {
    DCHECK(index < 0);
    return LinkageLocation(kMachineTagged, index);
  }

  static LinkageLocation TaggedRegisterLocation(Register reg) {
    return LinkageLocation(kMachineTagged, Register::ToAllocationIndex(reg));
  }

  static inline LinkageLocation WordRegisterLocation(Register reg) {
    return LinkageLocation(MachineOperatorBuilder::pointer_rep(),
                           Register::ToAllocationIndex(reg));
  }

  static LinkageLocation UnconstrainedRegister(MachineType rep) {
    return LinkageLocation(rep, LinkageLocation::ANY_REGISTER);
  }

  static const RegList kNoCalleeSaved = 0;

  // TODO(turbofan): cache call descriptors for JSFunction calls.
  template <typename LinkageTraits>
  static CallDescriptor* GetJSCallDescriptor(Zone* zone, int parameter_count) {
    const int jsfunction_count = 1;
    const int context_count = 1;
    int input_count = jsfunction_count + parameter_count + context_count;

    const int return_count = 1;
    LinkageLocation* locations =
        zone->NewArray<LinkageLocation>(return_count + input_count);

    int index = 0;
    locations[index++] =
        TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
    locations[index++] =
        TaggedRegisterLocation(LinkageTraits::JSCallFunctionReg());

    for (int i = 0; i < parameter_count; i++) {
      // All parameters to JS calls go on the stack.
      int spill_slot_index = i - parameter_count;
      locations[index++] = TaggedStackSlot(spill_slot_index);
    }
    locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg());

    // TODO(titzer): refactor TurboFan graph to consider context a value input.
    return new (zone)
        CallDescriptor(CallDescriptor::kCallJSFunction,  // kind
                       return_count,                     // return_count
                       parameter_count,                  // parameter_count
                       input_count - context_count,      // input_count
                       locations,                        // locations
                       Operator::kNoProperties,          // properties
                       kNoCalleeSaved,  // callee-saved registers
                       CallDescriptor::kCanDeoptimize);  // deoptimization
  }


  // TODO(turbofan): cache call descriptors for runtime calls.
  template <typename LinkageTraits>
  static CallDescriptor* GetRuntimeCallDescriptor(
      Zone* zone, Runtime::FunctionId function_id, int parameter_count,
      Operator::Property properties,
      CallDescriptor::DeoptimizationSupport can_deoptimize) {
    const int code_count = 1;
    const int function_count = 1;
    const int num_args_count = 1;
    const int context_count = 1;
    const int input_count = code_count + parameter_count + function_count +
                            num_args_count + context_count;

    const Runtime::Function* function = Runtime::FunctionForId(function_id);
    const int return_count = function->result_size;
    LinkageLocation* locations =
        zone->NewArray<LinkageLocation>(return_count + input_count);

    int index = 0;
    if (return_count > 0) {
      locations[index++] =
          TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
    }
    if (return_count > 1) {
      locations[index++] =
          TaggedRegisterLocation(LinkageTraits::ReturnValue2Reg());
    }

    DCHECK_LE(return_count, 2);

    locations[index++] = UnconstrainedRegister(kMachineTagged);  // CEntryStub

    for (int i = 0; i < parameter_count; i++) {
      // All parameters to runtime calls go on the stack.
      int spill_slot_index = i - parameter_count;
      locations[index++] = TaggedStackSlot(spill_slot_index);
    }
    locations[index++] =
        TaggedRegisterLocation(LinkageTraits::RuntimeCallFunctionReg());
    locations[index++] =
        WordRegisterLocation(LinkageTraits::RuntimeCallArgCountReg());
    locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg());

    // TODO(titzer): refactor TurboFan graph to consider context a value input.
    return new (zone) CallDescriptor(CallDescriptor::kCallCodeObject,  // kind
                                     return_count,     // return_count
                                     parameter_count,  // parameter_count
                                     input_count,      // input_count
                                     locations,        // locations
                                     properties,       // properties
                                     kNoCalleeSaved,   // callee-saved registers
                                     can_deoptimize,   // deoptimization
                                     function->name);
  }


  // TODO(turbofan): cache call descriptors for code stub calls.
  template <typename LinkageTraits>
  static CallDescriptor* GetStubCallDescriptor(
      Zone* zone, CodeStubInterfaceDescriptor* descriptor,
      int stack_parameter_count,
      CallDescriptor::DeoptimizationSupport can_deoptimize) {
    int register_parameter_count = descriptor->GetEnvironmentParameterCount();
    int parameter_count = register_parameter_count + stack_parameter_count;
    const int code_count = 1;
    const int context_count = 1;
    int input_count = code_count + parameter_count + context_count;

    const int return_count = 1;
    LinkageLocation* locations =
        zone->NewArray<LinkageLocation>(return_count + input_count);

    int index = 0;
    locations[index++] =
        TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
    locations[index++] = UnconstrainedRegister(kMachineTagged);  // code
    for (int i = 0; i < parameter_count; i++) {
      if (i < register_parameter_count) {
        // The first parameters to code stub calls go in registers.
        Register reg = descriptor->GetEnvironmentParameterRegister(i);
        locations[index++] = TaggedRegisterLocation(reg);
      } else {
        // The rest of the parameters go on the stack.
        int stack_slot = i - register_parameter_count - stack_parameter_count;
        locations[index++] = TaggedStackSlot(stack_slot);
      }
    }
    locations[index++] = TaggedRegisterLocation(LinkageTraits::ContextReg());

    // TODO(titzer): refactor TurboFan graph to consider context a value input.
    return new (zone)
        CallDescriptor(CallDescriptor::kCallCodeObject,  // kind
                       return_count,                     // return_count
                       parameter_count,                  // parameter_count
                       input_count,                      // input_count
                       locations,                        // locations
                       Operator::kNoProperties,          // properties
                       kNoCalleeSaved,  // callee-saved registers
                       can_deoptimize,  // deoptimization
                       CodeStub::MajorName(descriptor->MajorKey(), false));
  }


  template <typename LinkageTraits>
  static CallDescriptor* GetSimplifiedCDescriptor(
      Zone* zone, int num_params, MachineType return_type,
      const MachineType* param_types) {
    LinkageLocation* locations =
        zone->NewArray<LinkageLocation>(num_params + 2);
    int index = 0;
    locations[index++] =
        TaggedRegisterLocation(LinkageTraits::ReturnValueReg());
    locations[index++] = LinkageHelper::UnconstrainedRegister(
        MachineOperatorBuilder::pointer_rep());
    // TODO(dcarney): test with lots of parameters.
    int i = 0;
    for (; i < LinkageTraits::CRegisterParametersLength() && i < num_params;
         i++) {
      locations[index++] = LinkageLocation(
          param_types[i],
          Register::ToAllocationIndex(LinkageTraits::CRegisterParameter(i)));
    }
    for (; i < num_params; i++) {
      locations[index++] = LinkageLocation(param_types[i], -1 - i);
    }
    return new (zone) CallDescriptor(
        CallDescriptor::kCallAddress, 1, num_params, num_params + 1, locations,
        Operator::kNoProperties, LinkageTraits::CCalleeSaveRegisters(),
        CallDescriptor::kCannotDeoptimize);  // TODO(jarin) should deoptimize!
  }
};
}
}
}  // namespace v8::internal::compiler

#endif  // V8_COMPILER_LINKAGE_IMPL_H_