aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/MC/MCInstPrinter.cpp
blob: 1af6e4e121538c322cfbb5096276ac576328b822 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/MC/MCInstPrinter.h"

#include "rcc/MC/MCExpr.h"

#include <cctype>
#include <cstddef>
#include <cstdint>
#include <string>
#include <string_view>

namespace rcc::mc {
namespace {

[[nodiscard]] bool isBareSymbolName(std::string_view Name) noexcept {
  if (Name.empty())
    return false;
  const auto Initial = [](unsigned char Character) {
    return std::isalpha(Character) != 0 || Character == '_' || Character == '.' || Character == '$';
  };
  const auto Following = [](unsigned char Character) {
    return std::isalnum(Character) != 0 || Character == '_' || Character == '.' || Character == '$';
  };
  if (!Initial(static_cast<unsigned char>(Name.front())))
    return false;
  for (const char Character : Name.substr(1)) {
    if (!Following(static_cast<unsigned char>(Character)))
      return false;
  }
  return true;
}

[[nodiscard]] std::string_view unarySpelling(MCUnaryOperator Operator) noexcept {
  switch (Operator) {
  case MCUnaryOperator::Plus:
    return "+";
  case MCUnaryOperator::Minus:
    return "-";
  case MCUnaryOperator::BitwiseNot:
    return "~";
  case MCUnaryOperator::LogicalNot:
    return "!";
  }
  return {};
}

[[nodiscard]] std::string_view binarySpelling(MCBinaryOperator Operator) noexcept {
  switch (Operator) {
  case MCBinaryOperator::Add:
    return "+";
  case MCBinaryOperator::Subtract:
    return "-";
  case MCBinaryOperator::Multiply:
    return "*";
  case MCBinaryOperator::Divide:
    return "/";
  case MCBinaryOperator::Modulo:
    return "%";
  case MCBinaryOperator::ShiftLeft:
    return "<<";
  case MCBinaryOperator::ShiftRight:
    return ">>";
  case MCBinaryOperator::BitwiseAnd:
    return "&";
  case MCBinaryOperator::BitwiseOr:
    return "|";
  case MCBinaryOperator::BitwiseXor:
    return "^";
  case MCBinaryOperator::LogicalAnd:
    return "&&";
  case MCBinaryOperator::LogicalOr:
    return "||";
  case MCBinaryOperator::Equal:
    return "==";
  case MCBinaryOperator::NotEqual:
    return "!=";
  case MCBinaryOperator::Less:
    return "<";
  case MCBinaryOperator::LessEqual:
    return "<=";
  case MCBinaryOperator::Greater:
    return ">";
  case MCBinaryOperator::GreaterEqual:
    return ">=";
  }
  return {};
}

[[nodiscard]] Expected<std::string>
printExpressionImpl(const MCExpr &Expression, const MCInstPrinter &Printer, std::size_t Depth) {
  if (Depth > 256)
    return makeUnexpected(ErrorCode::AssemblyLoweringFailed,
                          "MC expression exceeds the assembly printing depth limit");

  switch (Expression.kind()) {
  case MCExpr::Kind::Constant:
    return std::to_string(*Expression.getIf<std::int64_t>());
  case MCExpr::Kind::SymbolRef: {
    const auto *Reference = Expression.getIf<MCSymbolRefExpr>();
    if (Reference == nullptr || !Reference->Symbol)
      return makeUnexpected(ErrorCode::InternalError, "MC symbol expression storage is invalid");
    return printMCSymbolName(Reference->Symbol->name());
  }
  case MCExpr::Kind::Unary: {
    const auto *Unary = Expression.getIf<MCUnaryExpr>();
    if (Unary == nullptr || !Unary->Operand)
      return makeUnexpected(ErrorCode::InternalError, "MC unary expression storage is invalid");
    auto Operand = printExpressionImpl(*Unary->Operand, Printer, Depth + 1);
    if (!Operand)
      return std::unexpected(Operand.error());
    return std::string(unarySpelling(Unary->Operator)) + "(" + *Operand + ")";
  }
  case MCExpr::Kind::Binary: {
    const auto *Binary = Expression.getIf<MCBinaryExpr>();
    if (Binary == nullptr || !Binary->Left || !Binary->Right)
      return makeUnexpected(ErrorCode::InternalError, "MC binary expression storage is invalid");
    auto Left = printExpressionImpl(*Binary->Left, Printer, Depth + 1);
    if (!Left)
      return std::unexpected(Left.error());
    auto Right = printExpressionImpl(*Binary->Right, Printer, Depth + 1);
    if (!Right)
      return std::unexpected(Right.error());
    return "(" + *Left + std::string(binarySpelling(Binary->Operator)) + *Right + ")";
  }
  case MCExpr::Kind::Target: {
    const auto *Target = Expression.getIf<MCTargetExpr>();
    if (Target == nullptr || !Target->Operand)
      return makeUnexpected(ErrorCode::InternalError, "target MC expression storage is invalid");
    auto Operand = printExpressionImpl(*Target->Operand, Printer, Depth + 1);
    if (!Operand)
      return std::unexpected(Operand.error());
    return Printer.printTargetExpression(Target->Kind, *Operand);
  }
  }
  return makeUnexpected(ErrorCode::InternalError, "MC expression kind is invalid");
}

} // namespace

Expected<std::string> printMCSymbolName(std::string_view Name) {
  if (Name.empty())
    return makeUnexpected(ErrorCode::AssemblyLoweringFailed, "MC symbol name cannot be empty");
  if (isBareSymbolName(Name))
    return std::string(Name);

  std::string Result;
  Result.push_back('"');
  for (const char RawCharacter : Name) {
    const auto Character = static_cast<unsigned char>(RawCharacter);
    if (Character == 0 || Character == '\n' || Character == '\r' || Character == 0x7fU) {
      return makeUnexpected(ErrorCode::AssemblyLoweringFailed,
                            "MC symbol name contains a non-printable character");
    }
    if (Character == '"' || Character == '\\')
      Result.push_back('\\');
    Result.push_back(static_cast<char>(Character));
  }
  Result.push_back('"');
  return Result;
}

Expected<std::string> printMCExpression(const MCExpr &Expression, const MCInstPrinter &Printer) {
  auto Verification = Expression.verify();
  if (!Verification)
    return std::unexpected(Verification.error());
  return printExpressionImpl(Expression, Printer, 1);
}

} // namespace rcc::mc