1//===- MIRYamlMapping.cpp - Describe mapping between MIR and YAML ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the mapping between various MIR data structures and
10// their corresponding YAML representation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MIRYamlMapping.h"
15#include "llvm/CodeGen/MachineFrameInfo.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/FormatVariadic.h"
18
19using namespace llvm;
20using namespace llvm::yaml;
21
22FrameIndex::FrameIndex(int FI, const llvm::MachineFrameInfo &MFI) {
23  IsFixed = MFI.isFixedObjectIndex(FI);
24  if (IsFixed)
25    FI -= MFI.getObjectIndexBegin();
26  this->FI = FI;
27}
28
29// Returns the value and if the frame index is fixed or not.
30Expected<int> FrameIndex::getFI(const llvm::MachineFrameInfo &MFI) const {
31  int FI = this->FI;
32  if (IsFixed) {
33    if (unsigned(FI) >= MFI.getNumFixedObjects())
34      return make_error<StringError>(
35          formatv("invalid fixed frame index {0}", FI).str(),
36          inconvertibleErrorCode());
37    FI += MFI.getObjectIndexBegin();
38  }
39  if (unsigned(FI + MFI.getNumFixedObjects()) >= MFI.getNumObjects())
40    return make_error<StringError>(formatv("invalid frame index {0}", FI).str(),
41                                   inconvertibleErrorCode());
42  return FI;
43}
44