aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/TargetFrameLowering.h
blob: 1712616d8d917827ac5b0a08f2616782c5aca498 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_TARGETFRAMELOWERING_H
#define RCC_CODEGEN_TARGETFRAMELOWERING_H

#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/CodeGen/Register.h"
#include "rcc/Support/Error.h"

#include <cstddef>
#include <cstdint>

namespace rcc {

class MachineBasicBlock;
class MachineFunction;
class MachineInstr;
class RegisterScavenger;
class TargetDescription;

struct FrameIndexReference final {
  PhysicalRegister BaseRegister;
  std::int64_t Offset = 0;
};

class TargetFrameLowering {
public:
  virtual ~TargetFrameLowering() = default;

  [[nodiscard]] virtual Expected<void> prepareFrame(MachineFunction &Function,
                                                    const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<void>
  insertCalleeSavedSpillsAndRestores(MachineFunction &Function,
                                     const TargetDescription &Target) const = 0;
  [[nodiscard]] virtual Expected<void>
  processFunctionBeforeFrameFinalized(MachineFunction &, const TargetDescription &) const {
    return {};
  }
  [[nodiscard]] virtual Expected<void> finalizeFrame(MachineFunction &Function,
                                                     const TargetDescription &Target) const = 0;

  [[nodiscard]] virtual bool isCallFramePseudo(const MachineInstr &Instruction) const noexcept = 0;
  [[nodiscard]] virtual Expected<void>
  eliminateCallFramePseudo(MachineInstr &Instruction, MachineFunction &Function,
                           const TargetDescription &Target, RegisterScavenger &Scavenger) const = 0;

  [[nodiscard]] virtual Expected<void> emitPrologue(MachineFunction &Function,
                                                    const TargetDescription &Target,
                                                    RegisterScavenger &Scavenger) const = 0;
  [[nodiscard]] virtual bool
  isEpilogueTerminator(const MachineInstr &Instruction,
                       const TargetDescription &Target) const noexcept = 0;
  [[nodiscard]] virtual Expected<void>
  emitEpilogue(MachineBasicBlock &Block, MachineInstr &Terminator, MachineFunction &Function,
               const TargetDescription &Target, RegisterScavenger &Scavenger) const = 0;
  [[nodiscard]] virtual Expected<void>
  processFunctionBeforeFrameIndexElimination(MachineFunction &, const TargetDescription &) const {
    return {};
  }

  [[nodiscard]] virtual Expected<FrameIndexReference>
  getFrameIndexReference(const MachineFunction &Function, const MachineInstr &Instruction,
                         FrameIndex Index, std::int64_t OperandOffset) const = 0;
  [[nodiscard]] virtual Expected<void> eliminateFrameIndex(MachineInstr &Instruction,
                                                           std::size_t OperandIndex,
                                                           MachineFunction &Function,
                                                           const TargetDescription &Target,
                                                           RegisterScavenger &Scavenger) const = 0;
};

} // namespace rcc

#endif // RCC_CODEGEN_TARGETFRAMELOWERING_H