forked from microsoft/llvm-mctoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MCInstOrData.cpp
76 lines (67 loc) · 1.77 KB
/
MCInstOrData.cpp
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
//===-- MCInstOrData.cpp ----------------------------------------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "MCInstOrData.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#define DEBUG_TYPE "mctoll"
MCInstOrData::MCInstOrData(const MCInstOrData &V) {
Type = V.Type;
switch (Type) {
case Tag::DATA:
Data = V.Data;
break;
case Tag::INSTRUCTION:
new (&Inst) MCInst(V.Inst);
break;
}
}
MCInstOrData::MCInstOrData(const MCInst &V) {
Type = Tag::INSTRUCTION;
new (&Inst) MCInst(V); // placement new: explicitly construct MCInst
}
MCInstOrData::MCInstOrData(const uint32_t V) {
Type = Tag::DATA;
Data = V;
}
// This is needed because of user-defined variant MCInst being part of MCInst
MCInstOrData &MCInstOrData::operator=(const MCInstOrData &E) {
if (Type == Tag::INSTRUCTION) {
if (E.Type == Tag::INSTRUCTION) {
// Usual MCInst assignment
Inst = E.Inst;
return *this;
}
// Explicit destroy
Inst.~MCInst();
}
switch (E.Type) {
case Tag::DATA:
Data = E.Data;
break;
case Tag::INSTRUCTION:
new (&Inst) MCInst(E.Inst);
Type = E.Type;
break;
}
return *this;
}
void MCInstOrData::dump() const {
switch (Type) {
case Tag::DATA:
outs() << "0x" << format("%04" PRIx16, Data) << "\n";
break;
case Tag::INSTRUCTION:
LLVM_DEBUG(Inst.dump());
break;
}
}
MCInstOrData::~MCInstOrData() {
if (Type == Tag::INSTRUCTION)
Inst.~MCInst(); // explicit destroy
}