linkage-impl.h 12.2 KB
Newer Older
1 2 3 4 5 6 7
// 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_

8
#include "src/code-stubs.h"
9
#include "src/compiler/osr.h"
10

11 12 13 14
namespace v8 {
namespace internal {
namespace compiler {

15 16
// TODO(titzer): replace uses of int with size_t in LinkageHelper.
template <typename LinkageTraits>
17 18
class LinkageHelper {
 public:
19
  static const RegList kNoCalleeSaved = 0;
20

21 22 23 24 25 26 27 28
  static void AddReturnLocations(LocationSignature::Builder* locations) {
    DCHECK(locations->return_count_ <= 2);
    if (locations->return_count_ > 0) {
      locations->AddReturn(regloc(LinkageTraits::ReturnValueReg()));
    }
    if (locations->return_count_ > 1) {
      locations->AddReturn(regloc(LinkageTraits::ReturnValue2Reg()));
    }
29 30 31
  }

  // TODO(turbofan): cache call descriptors for JSFunction calls.
32 33
  static CallDescriptor* GetJSCallDescriptor(Zone* zone, bool is_osr,
                                             int js_parameter_count,
34
                                             CallDescriptor::Flags flags) {
35 36 37 38 39 40 41 42 43 44 45
    const size_t return_count = 1;
    const size_t context_count = 1;
    const size_t parameter_count = js_parameter_count + context_count;

    LocationSignature::Builder locations(zone, return_count, parameter_count);
    MachineSignature::Builder types(zone, return_count, parameter_count);

    // Add returns.
    AddReturnLocations(&locations);
    for (size_t i = 0; i < return_count; i++) {
      types.AddReturn(kMachAnyTagged);
46 47
    }

48 49 50 51 52 53 54 55 56 57 58 59
    // All parameters to JS calls go on the stack.
    for (int i = 0; i < js_parameter_count; i++) {
      int spill_slot_index = i - js_parameter_count;
      locations.AddParam(stackloc(spill_slot_index));
      types.AddParam(kMachAnyTagged);
    }
    // Add context.
    locations.AddParam(regloc(LinkageTraits::ContextReg()));
    types.AddParam(kMachAnyTagged);

    // The target for JS function calls is the JSFunction object.
    MachineType target_type = kMachAnyTagged;
60 61 62 63
    // TODO(titzer): When entering into an OSR function from unoptimized code,
    // the JSFunction is not in a register, but it is on the stack in an
    // unaddressable spill slot. We hack this in the OSR prologue. Fix.
    LinkageLocation target_loc = regloc(LinkageTraits::JSCallFunctionReg());
64 65 66 67 68 69 70 71 72
    return new (zone) CallDescriptor(     // --
        CallDescriptor::kCallJSFunction,  // kind
        target_type,                      // target MachineType
        target_loc,                       // target location
        types.Build(),                    // machine_sig
        locations.Build(),                // location_sig
        js_parameter_count,               // js_parameter_count
        Operator::kNoProperties,          // properties
        kNoCalleeSaved,                   // callee-saved
73
        kNoCalleeSaved,                   // callee-saved fp
74 75
        flags,                            // flags
        "js-call");
76 77 78 79 80
  }


  // TODO(turbofan): cache call descriptors for runtime calls.
  static CallDescriptor* GetRuntimeCallDescriptor(
81
      Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
82
      Operator::Properties properties) {
83 84 85 86 87 88
    const size_t function_count = 1;
    const size_t num_args_count = 1;
    const size_t context_count = 1;
    const size_t parameter_count = function_count +
                                   static_cast<size_t>(js_parameter_count) +
                                   num_args_count + context_count;
89 90

    const Runtime::Function* function = Runtime::FunctionForId(function_id);
91
    const size_t return_count = static_cast<size_t>(function->result_size);
92

93 94
    LocationSignature::Builder locations(zone, return_count, parameter_count);
    MachineSignature::Builder types(zone, return_count, parameter_count);
95

96 97 98 99 100
    // Add returns.
    AddReturnLocations(&locations);
    for (size_t i = 0; i < return_count; i++) {
      types.AddReturn(kMachAnyTagged);
    }
101

102 103 104 105
    // All parameters to the runtime call go on the stack.
    for (int i = 0; i < js_parameter_count; i++) {
      locations.AddParam(stackloc(i - js_parameter_count));
      types.AddParam(kMachAnyTagged);
106
    }
107 108 109 110 111 112 113 114 115 116 117
    // Add runtime function itself.
    locations.AddParam(regloc(LinkageTraits::RuntimeCallFunctionReg()));
    types.AddParam(kMachAnyTagged);

    // Add runtime call argument count.
    locations.AddParam(regloc(LinkageTraits::RuntimeCallArgCountReg()));
    types.AddParam(kMachPtr);

    // Add context.
    locations.AddParam(regloc(LinkageTraits::ContextReg()));
    types.AddParam(kMachAnyTagged);
118

119
    CallDescriptor::Flags flags = Linkage::FrameStateInputCount(function_id) > 0
120 121 122
                                      ? CallDescriptor::kNeedsFrameState
                                      : CallDescriptor::kNoFlags;

123 124 125
    // The target for runtime calls is a code object.
    MachineType target_type = kMachAnyTagged;
    LinkageLocation target_loc = LinkageLocation::AnyRegister();
126 127 128 129 130 131 132 133 134
    return new (zone) CallDescriptor(     // --
        CallDescriptor::kCallCodeObject,  // kind
        target_type,                      // target MachineType
        target_loc,                       // target location
        types.Build(),                    // machine_sig
        locations.Build(),                // location_sig
        js_parameter_count,               // js_parameter_count
        properties,                       // properties
        kNoCalleeSaved,                   // callee-saved
135
        kNoCalleeSaved,                   // callee-saved fp
136 137
        flags,                            // flags
        function->name);                  // debug name
138 139 140
  }


141 142
  // TODO(all): Add support for return representations/locations to
  // CallInterfaceDescriptor.
143 144
  // TODO(turbofan): cache call descriptors for code stub calls.
  static CallDescriptor* GetStubCallDescriptor(
145
      Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
146
      int stack_parameter_count, CallDescriptor::Flags flags,
147
      Operator::Properties properties, MachineType return_type) {
148
    const int register_parameter_count = descriptor.GetRegisterParameterCount();
149 150
    const int js_parameter_count =
        register_parameter_count + stack_parameter_count;
151
    const int context_count = 1;
152 153 154
    const size_t return_count = 1;
    const size_t parameter_count =
        static_cast<size_t>(js_parameter_count + context_count);
155

156 157
    LocationSignature::Builder locations(zone, return_count, parameter_count);
    MachineSignature::Builder types(zone, return_count, parameter_count);
158

159 160
    // Add return location.
    AddReturnLocations(&locations);
161
    types.AddReturn(return_type);
162 163 164

    // Add parameters in registers and on the stack.
    for (int i = 0; i < js_parameter_count; i++) {
165
      if (i < register_parameter_count) {
166
        // The first parameters go in registers.
167
        Register reg = descriptor.GetRegisterParameter(i);
168
        Representation rep =
169
            RepresentationFromType(descriptor.GetParameterType(i));
170
        locations.AddParam(regloc(reg));
171
        types.AddParam(reptyp(rep));
172 173 174
      } else {
        // The rest of the parameters go on the stack.
        int stack_slot = i - register_parameter_count - stack_parameter_count;
175
        locations.AddParam(stackloc(stack_slot));
176
        types.AddParam(kMachAnyTagged);
177 178
      }
    }
179 180 181
    // Add context.
    locations.AddParam(regloc(LinkageTraits::ContextReg()));
    types.AddParam(kMachAnyTagged);
182

183 184 185
    // The target for stub calls is a code object.
    MachineType target_type = kMachAnyTagged;
    LinkageLocation target_loc = LinkageLocation::AnyRegister();
186 187 188 189 190 191 192
    return new (zone) CallDescriptor(     // --
        CallDescriptor::kCallCodeObject,  // kind
        target_type,                      // target MachineType
        target_loc,                       // target location
        types.Build(),                    // machine_sig
        locations.Build(),                // location_sig
        js_parameter_count,               // js_parameter_count
193
        properties,                       // properties
194
        kNoCalleeSaved,                   // callee-saved registers
195
        kNoCalleeSaved,                   // callee-saved fp
196
        flags,                            // flags
197
        descriptor.DebugName(isolate));
198 199
  }

200 201
  static CallDescriptor* GetSimplifiedCDescriptor(
      Zone* zone, const MachineSignature* msig) {
202 203 204 205
    LocationSignature::Builder locations(zone, msig->return_count(),
                                         msig->parameter_count());
    // Add return location(s).
    AddReturnLocations(&locations);
206

207 208
    // Add register and/or stack parameter(s).
    const int parameter_count = static_cast<int>(msig->parameter_count());
209
    int stack_offset = LinkageTraits::CStackBackingStoreLength();
210 211 212 213
    for (int i = 0; i < parameter_count; i++) {
      if (i < LinkageTraits::CRegisterParametersLength()) {
        locations.AddParam(regloc(LinkageTraits::CRegisterParameter(i)));
      } else {
214 215
        locations.AddParam(stackloc(-1 - stack_offset));
        stack_offset++;
216
      }
217
    }
218 219 220 221

    // The target for C calls is always an address (i.e. machine pointer).
    MachineType target_type = kMachPtr;
    LinkageLocation target_loc = LinkageLocation::AnyRegister();
222 223 224 225 226 227 228 229 230 231 232
    return new (zone) CallDescriptor(             // --
        CallDescriptor::kCallAddress,             // kind
        target_type,                              // target MachineType
        target_loc,                               // target location
        msig,                                     // machine_sig
        locations.Build(),                        // location_sig
        0,                                        // js_parameter_count
        Operator::kNoProperties,                  // properties
        LinkageTraits::CCalleeSaveRegisters(),    // callee-saved registers
        LinkageTraits::CCalleeSaveFPRegisters(),  // callee-saved fp regs
        CallDescriptor::kNoFlags,                 // flags
233
        "c-call");
234 235 236 237 238 239 240 241 242
  }

  static LinkageLocation regloc(Register reg) {
    return LinkageLocation(Register::ToAllocationIndex(reg));
  }

  static LinkageLocation stackloc(int i) {
    DCHECK_LT(i, 0);
    return LinkageLocation(i);
243
  }
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

  static MachineType reptyp(Representation representation) {
    switch (representation.kind()) {
      case Representation::kInteger8:
        return kMachInt8;
      case Representation::kUInteger8:
        return kMachUint8;
      case Representation::kInteger16:
        return kMachInt16;
      case Representation::kUInteger16:
        return kMachUint16;
      case Representation::kInteger32:
        return kMachInt32;
      case Representation::kSmi:
      case Representation::kTagged:
      case Representation::kHeapObject:
        return kMachAnyTagged;
      case Representation::kDouble:
        return kMachFloat64;
      case Representation::kExternal:
        return kMachPtr;
      case Representation::kNone:
      case Representation::kNumRepresentations:
        break;
    }
    UNREACHABLE();
    return kMachNone;
  }
272
};
273 274 275 276 277 278 279


LinkageLocation Linkage::GetOsrValueLocation(int index) const {
  CHECK(incoming_->IsJSFunctionCall());
  int parameter_count = static_cast<int>(incoming_->JSParameterCount() - 1);
  int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);

280 281 282 283 284 285
  if (index == kOsrContextSpillSlotIndex) {
    // Context. Use the parameter location of the context spill slot.
    // Parameter (arity + 1) is special for the context of the function frame.
    int context_index = 1 + 1 + parameter_count;  // target + receiver + params
    return incoming_->GetInputLocation(context_index);
  } else if (index >= first_stack_slot) {
286 287 288 289 290 291 292 293 294 295 296 297 298
    // Local variable stored in this (callee) stack.
    int spill_index =
        LinkageLocation::ANY_REGISTER + 1 + index - first_stack_slot;
    // TODO(titzer): bailout instead of crashing here.
    CHECK(spill_index <= LinkageLocation::MAX_STACK_SLOT);
    return LinkageLocation(spill_index);
  } else {
    // Parameter. Use the assigned location from the incoming call descriptor.
    int parameter_index = 1 + index;  // skip index 0, which is the target.
    return incoming_->GetInputLocation(parameter_index);
  }
}

299 300 301
}  // namespace compiler
}  // namespace internal
}  // namespace v8
302 303

#endif  // V8_COMPILER_LINKAGE_IMPL_H_