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

#ifndef RCC_CODEGEN_MACHINEMCCONTEXT_H
#define RCC_CODEGEN_MACHINEMCCONTEXT_H

#include "rcc/CodeGen/MachineIRTypes.h"
#include "rcc/MC/MCContext.h"
#include "rcc/MC/MCSymbol.h"
#include "rcc/Support/Error.h"

#include <string>
#include <string_view>
#include <unordered_map>

namespace rcc {

class CompilationUnit;
struct GlobalObjectDesc;
class MachineBasicBlock;
class MachineFunction;

class MachineMCContext final {
public:
  explicit MachineMCContext(mc::MCContext &Context) noexcept : Context(&Context) {}

  [[nodiscard]] Expected<void> initialize(const CompilationUnit &Unit);
  [[nodiscard]] Expected<void> initializeFunction(const MachineFunction &Function);

  [[nodiscard]] mc::MCSymbolRef functionSymbol(const MachineFunction &Function) const noexcept;
  [[nodiscard]] mc::MCSymbolRef globalSymbol(const GlobalObjectDesc &Object) const noexcept;
  [[nodiscard]] mc::MCSymbolRef blockSymbol(const MachineBasicBlock &Block) const noexcept;
  [[nodiscard]] mc::MCSymbolRef constantPoolSymbol(const MachineFunction &Function,
                                                   ConstantPoolIndex Index) const noexcept;
  [[nodiscard]] mc::MCSymbolRef jumpTableSymbol(const MachineFunction &Function,
                                                JumpTableIndex Index) const noexcept;

  [[nodiscard]] Expected<mc::MCSymbolRef> symbol(std::string Name,
                                                 mc::MCSymbolKind Kind = mc::MCSymbolKind::Generic);
  [[nodiscard]] Expected<mc::MCSymbolRef> blockAddressSymbol(std::string_view FunctionName,
                                                             std::string_view BlockName) const;

  [[nodiscard]] mc::MCContext &context() const noexcept {
    return *Context;
  }

private:
  struct FunctionIndexKey final {
    const MachineFunction *Function = nullptr;
    std::uint32_t Index = 0;

    friend bool operator==(FunctionIndexKey Left, FunctionIndexKey Right) noexcept = default;
  };

  struct FunctionIndexHash final {
    [[nodiscard]] std::size_t operator()(FunctionIndexKey Key) const noexcept;
  };

  mc::MCContext *Context;
  std::unordered_map<const MachineFunction *, mc::MCSymbolRef> FunctionSymbols;
  std::unordered_map<const GlobalObjectDesc *, mc::MCSymbolRef> GlobalSymbols;
  std::unordered_map<const MachineBasicBlock *, mc::MCSymbolRef> BlockSymbols;
  std::unordered_map<FunctionIndexKey, mc::MCSymbolRef, FunctionIndexHash> ConstantPoolSymbols;
  std::unordered_map<FunctionIndexKey, mc::MCSymbolRef, FunctionIndexHash> JumpTableSymbols;
};

} // namespace rcc

#endif // RCC_CODEGEN_MACHINEMCCONTEXT_H