//=== W65816MachineFunctionInfo.h - W65816 machine function info -*- C++ -*-==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file declares W65816-specific per-machine-function information.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_W65816_W65816MACHINEFUNCTIONINFO_H
#define LLVM_LIB_TARGET_W65816_W65816MACHINEFUNCTIONINFO_H

#include "llvm/CodeGen/MachineFunction.h"

namespace llvm {

/// W65816MachineFunctionInfo - per-MachineFunction private target-specific
/// information for the W65816.
class W65816MachineFunctionInfo : public MachineFunctionInfo {
  virtual void anchor();

  /// Size of the callee-saved register portion of the stack frame in bytes.
  unsigned CalleeSavedFrameSize = 0;

  /// FrameIndex for the return-address slot.
  int ReturnAddrIndex = 0;

  /// FrameIndex for the start of the varargs area.
  int VarArgsFrameIndex = 0;

  /// Virtual register holding the struct-return pointer for sret returns.
  Register SRetReturnReg;

  /// True iff the function's prologue chose 8-bit M (SEP #$20).  Pure-i8
  /// functions run with M=1; everything else runs with M=0.  AsmPrinter
  /// reads this when expanding pseudos whose width depends on M (e.g.
  /// STA8abs needs an SEP/REP wrap in M=0 to avoid a 2-byte store).
  bool UsesAcc8 = false;

  /// True iff this function reserved DP $F6/$F7 as a frame pointer.
  /// Set when the static frame size exceeds the 8-bit `,S` stack-rel
  /// addressing range (256 bytes); the prologue stores `S` (after
  /// local allocation) into $F6/$F7 (16-bit, bank-0 implicit), and
  /// eliminateFrameIndex routes any FI access whose effective offset
  /// exceeds 0xFF through `(F6),Y` indirect-indexed addressing.
  bool UsesDpFP = false;

  /// FrameIndex of a 2-byte slot reserved (VLA functions only) to
  /// callee-save the caller's DP $F4/$F5 (the VLA entry-SP pointer).
  /// A nested VLA callee overwrites $F4 in its own prologue; without
  /// this save/restore the caller's epilogue SP-restore and $F6
  /// recompute would read a stale $F4.  -1 when unused.
  int F4SaveFrameIndex = -1;


public:
  W65816MachineFunctionInfo() = default;

  W65816MachineFunctionInfo(const Function &F, const TargetSubtargetInfo *STI) {
  }

  MachineFunctionInfo *
  clone(BumpPtrAllocator &Allocator, MachineFunction &DestMF,
        const DenseMap<MachineBasicBlock *, MachineBasicBlock *> &Src2DstMBB)
      const override;

  unsigned getCalleeSavedFrameSize() const { return CalleeSavedFrameSize; }
  void setCalleeSavedFrameSize(unsigned Bytes) { CalleeSavedFrameSize = Bytes; }

  Register getSRetReturnReg() const { return SRetReturnReg; }
  void setSRetReturnReg(Register Reg) { SRetReturnReg = Reg; }

  int getRAIndex() const { return ReturnAddrIndex; }
  void setRAIndex(int Index) { ReturnAddrIndex = Index; }

  int getVarArgsFrameIndex() const { return VarArgsFrameIndex; }
  void setVarArgsFrameIndex(int Index) { VarArgsFrameIndex = Index; }

  bool getUsesAcc8() const { return UsesAcc8; }
  void setUsesAcc8(bool V) { UsesAcc8 = V; }

  bool getUsesDpFP() const { return UsesDpFP; }
  void setUsesDpFP(bool V) { UsesDpFP = V; }

  int getF4SaveFrameIndex() const { return F4SaveFrameIndex; }
  void setF4SaveFrameIndex(int Index) { F4SaveFrameIndex = Index; }
};

} // namespace llvm

#endif
