aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp
blob: a1045107a832f4e3f032c39a50d847c553a7dd7f (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//===-- VEMCExpr.cpp - VE specific MC expression classes ------------------===//
//
// 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 contains the implementation of the assembly expression modifiers
// accepted by the VE architecture (e.g. "%hi", "%lo", ...).
//
//===----------------------------------------------------------------------===//

#include "VEMCExpr.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Casting.h"

using namespace llvm;

#define DEBUG_TYPE "vemcexpr"

const VEMCExpr *VEMCExpr::create(VariantKind Kind, const MCExpr *Expr,
                                 MCContext &Ctx) {
  return new (Ctx) VEMCExpr(Kind, Expr);
}

void VEMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {

  bool closeParen = printVariantKind(OS, Kind);

  const MCExpr *Expr = getSubExpr();
  Expr->print(OS, MAI);

  if (closeParen)
    OS << ')';
  printVariantKindSuffix(OS, Kind);
}

bool VEMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind) {
  switch (Kind) {
  case VK_VE_None:
  case VK_VE_REFLONG:
    return false;

  case VK_VE_HI32:
  case VK_VE_LO32:
  case VK_VE_PC_HI32:
  case VK_VE_PC_LO32:
  case VK_VE_GOT_HI32:
  case VK_VE_GOT_LO32:
  case VK_VE_GOTOFF_HI32:
  case VK_VE_GOTOFF_LO32:
  case VK_VE_PLT_HI32:
  case VK_VE_PLT_LO32:
  case VK_VE_TLS_GD_HI32:
  case VK_VE_TLS_GD_LO32:
  case VK_VE_TPOFF_HI32:
  case VK_VE_TPOFF_LO32:
    // Use suffix for these variant kinds
    return false;
  }
  return true;
}

void VEMCExpr::printVariantKindSuffix(raw_ostream &OS, VariantKind Kind) {
  switch (Kind) {
  case VK_VE_None:
  case VK_VE_REFLONG:
    break;
  case VK_VE_HI32:
    OS << "@hi";
    break;
  case VK_VE_LO32:
    OS << "@lo";
    break;
  case VK_VE_PC_HI32:
    OS << "@pc_hi";
    break;
  case VK_VE_PC_LO32:
    OS << "@pc_lo";
    break;
  case VK_VE_GOT_HI32:
    OS << "@got_hi";
    break;
  case VK_VE_GOT_LO32:
    OS << "@got_lo";
    break;
  case VK_VE_GOTOFF_HI32:
    OS << "@gotoff_hi";
    break;
  case VK_VE_GOTOFF_LO32:
    OS << "@gotoff_lo";
    break;
  case VK_VE_PLT_HI32:
    OS << "@plt_hi";
    break;
  case VK_VE_PLT_LO32:
    OS << "@plt_lo";
    break;
  case VK_VE_TLS_GD_HI32:
    OS << "@tls_gd_hi";
    break;
  case VK_VE_TLS_GD_LO32:
    OS << "@tls_gd_lo";
    break;
  case VK_VE_TPOFF_HI32:
    OS << "@tpoff_hi";
    break;
  case VK_VE_TPOFF_LO32:
    OS << "@tpoff_lo";
    break;
  }
}

VEMCExpr::VariantKind VEMCExpr::parseVariantKind(StringRef name) {
  return StringSwitch<VEMCExpr::VariantKind>(name)
      .Case("hi", VK_VE_HI32)
      .Case("lo", VK_VE_LO32)
      .Case("pc_hi", VK_VE_PC_HI32)
      .Case("pc_lo", VK_VE_PC_LO32)
      .Case("got_hi", VK_VE_GOT_HI32)
      .Case("got_lo", VK_VE_GOT_LO32)
      .Case("gotoff_hi", VK_VE_GOTOFF_HI32)
      .Case("gotoff_lo", VK_VE_GOTOFF_LO32)
      .Case("plt_hi", VK_VE_PLT_HI32)
      .Case("plt_lo", VK_VE_PLT_LO32)
      .Case("tls_gd_hi", VK_VE_TLS_GD_HI32)
      .Case("tls_gd_lo", VK_VE_TLS_GD_LO32)
      .Case("tpoff_hi", VK_VE_TPOFF_HI32)
      .Case("tpoff_lo", VK_VE_TPOFF_LO32)
      .Default(VK_VE_None);
}

VE::Fixups VEMCExpr::getFixupKind(VEMCExpr::VariantKind Kind) {
  switch (Kind) {
  default:
    llvm_unreachable("Unhandled VEMCExpr::VariantKind");
  case VK_VE_REFLONG:
    return VE::fixup_ve_reflong;
  case VK_VE_HI32:
    return VE::fixup_ve_hi32;
  case VK_VE_LO32:
    return VE::fixup_ve_lo32;
  case VK_VE_PC_HI32:
    return VE::fixup_ve_pc_hi32;
  case VK_VE_PC_LO32:
    return VE::fixup_ve_pc_lo32;
  case VK_VE_GOT_HI32:
    return VE::fixup_ve_got_hi32;
  case VK_VE_GOT_LO32:
    return VE::fixup_ve_got_lo32;
  case VK_VE_GOTOFF_HI32:
    return VE::fixup_ve_gotoff_hi32;
  case VK_VE_GOTOFF_LO32:
    return VE::fixup_ve_gotoff_lo32;
  case VK_VE_PLT_HI32:
    return VE::fixup_ve_plt_hi32;
  case VK_VE_PLT_LO32:
    return VE::fixup_ve_plt_lo32;
  case VK_VE_TLS_GD_HI32:
    return VE::fixup_ve_tls_gd_hi32;
  case VK_VE_TLS_GD_LO32:
    return VE::fixup_ve_tls_gd_lo32;
  case VK_VE_TPOFF_HI32:
    return VE::fixup_ve_tpoff_hi32;
  case VK_VE_TPOFF_LO32:
    return VE::fixup_ve_tpoff_lo32;
  }
}

bool VEMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
                                         const MCAsmLayout *Layout,
                                         const MCFixup *Fixup) const {
  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
    return false;

  Res =
      MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());

  return true;
}

static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
  switch (Expr->getKind()) {
  case MCExpr::Target:
    llvm_unreachable("Can't handle nested target expr!");
    break;

  case MCExpr::Constant:
    break;

  case MCExpr::Binary: {
    const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
    fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
    fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
    break;
  }

  case MCExpr::SymbolRef: {
    // We're known to be under a TLS fixup, so any symbol should be
    // modified. There should be only one.
    const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
    cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
    break;
  }

  case MCExpr::Unary:
    fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
    break;
  }
}

void VEMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
  Streamer.visitUsedExpr(*getSubExpr());
}

void VEMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
  switch (getKind()) {
  default:
    return;
  case VK_VE_TLS_GD_HI32:
  case VK_VE_TLS_GD_LO32:
  case VK_VE_TPOFF_HI32:
  case VK_VE_TPOFF_LO32:
    break;
  }
  fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
}