mini-llvm 0.1.0
Loading...
Searching...
No Matches
MemoryOperand.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <format>
6#include <memory>
7#include <string>
8#include <utility>
9
17
18namespace mini_llvm::mir {
19
21public:
22 explicit MemoryOperand(std::shared_ptr<Register> baseReg,
23 std::unique_ptr<Immediate> offset = std::make_unique<IntegerImmediate>(0))
24 : baseReg_(RegisterClass::kGPR, std::move(baseReg)), offset_(std::move(offset)) {}
25
26 template <typename Self>
27 auto &baseReg(this Self &&self) {
28 return self.baseReg_;
29 }
30
31 template <typename Self>
32 auto &offset(this Self &&self) {
33 return self.offset_;
34 }
35
36 std::string format() const {
37 return std::format("{}({})", *offset(), *baseReg());
38 }
39
41 return MemoryOperand(share(*baseReg()), offset()->clone());
42 }
43
44private:
45 RegisterOperand baseReg_;
46 ImmediateOperand offset_;
47};
48
49} // namespace mini_llvm::mir
50
51template <>
52struct std::formatter<mini_llvm::mir::MemoryOperand> {
53 constexpr auto parse(std::format_parse_context &ctx) {
54 return ctx.begin();
55 }
56
57 template <typename FormatContext>
58 auto format(const mini_llvm::mir::MemoryOperand &op, FormatContext &ctx) const {
59 return std::format_to(ctx.out(), "{}", op.format());
60 }
61};
Definition ImmediateOperand.h:12
Definition MemoryOperand.h:20
auto & offset(this Self &&self)
Definition MemoryOperand.h:32
auto & baseReg(this Self &&self)
Definition MemoryOperand.h:27
std::string format() const
Definition MemoryOperand.h:36
MemoryOperand(std::shared_ptr< Register > baseReg, std::unique_ptr< Immediate > offset=std::make_unique< IntegerImmediate >(0))
Definition MemoryOperand.h:22
MemoryOperand clone() const
Definition MemoryOperand.h:40
Definition RegisterOperand.h:13
Definition BasicBlock.h:22
RegisterClass
Definition RegisterClass.h:7
@ kGPR
Definition RegisterClass.h:8
Definition GraphColoringAllocator.h:13
std::shared_ptr< T > share(T &value)
Definition Memory.h:25
constexpr auto parse(std::format_parse_context &ctx)
Definition MemoryOperand.h:53
auto format(const mini_llvm::mir::MemoryOperand &op, FormatContext &ctx) const
Definition MemoryOperand.h:58