Bug Summary

File:llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp
Warning:line 205, column 34
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SparcMCCodeEmitter.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/build-llvm/lib/Target/Sparc/MCTargetDesc -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/build-llvm/lib/Target/Sparc/MCTargetDesc -I /build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc -I /build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc -I /build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/build-llvm/lib/Target/Sparc -I /build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../x86_64-linux-gnu/include -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/build-llvm/lib/Target/Sparc/MCTargetDesc -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4=. -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-04-05-202135-9119-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp
1//===-- SparcMCCodeEmitter.cpp - Convert Sparc code to machine code -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SparcMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MCTargetDesc/SparcFixupKinds.h"
14#include "SparcMCExpr.h"
15#include "SparcMCTargetDesc.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCFixup.h"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCObjectFileInfo.h"
26#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCSubtargetInfo.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/MC/SubtargetFeature.h"
30#include "llvm/Support/Casting.h"
31#include "llvm/Support/Endian.h"
32#include "llvm/Support/EndianStream.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
35#include <cassert>
36#include <cstdint>
37
38using namespace llvm;
39
40#define DEBUG_TYPE"mccodeemitter" "mccodeemitter"
41
42STATISTIC(MCNumEmitted, "Number of MC instructions emitted")static llvm::Statistic MCNumEmitted = {"mccodeemitter", "MCNumEmitted"
, "Number of MC instructions emitted"}
;
43
44namespace {
45
46class SparcMCCodeEmitter : public MCCodeEmitter {
47 const MCInstrInfo &MCII;
48 MCContext &Ctx;
49
50public:
51 SparcMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
52 : MCII(mcii), Ctx(ctx) {}
53 SparcMCCodeEmitter(const SparcMCCodeEmitter &) = delete;
54 SparcMCCodeEmitter &operator=(const SparcMCCodeEmitter &) = delete;
55 ~SparcMCCodeEmitter() override = default;
56
57 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
58 SmallVectorImpl<MCFixup> &Fixups,
59 const MCSubtargetInfo &STI) const override;
60
61 // getBinaryCodeForInstr - TableGen'erated function for getting the
62 // binary encoding for an instruction.
63 uint64_t getBinaryCodeForInstr(const MCInst &MI,
64 SmallVectorImpl<MCFixup> &Fixups,
65 const MCSubtargetInfo &STI) const;
66
67 /// getMachineOpValue - Return binary encoding of operand. If the machine
68 /// operand requires relocation, record the relocation and return zero.
69 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
70 SmallVectorImpl<MCFixup> &Fixups,
71 const MCSubtargetInfo &STI) const;
72 unsigned getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const;
75 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
78 unsigned getSImm13OpValue(const MCInst &MI, unsigned OpNo,
79 SmallVectorImpl<MCFixup> &Fixups,
80 const MCSubtargetInfo &STI) const;
81 unsigned getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
82 SmallVectorImpl<MCFixup> &Fixups,
83 const MCSubtargetInfo &STI) const;
84 unsigned getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
85 SmallVectorImpl<MCFixup> &Fixups,
86 const MCSubtargetInfo &STI) const;
87
88private:
89 FeatureBitset computeAvailableFeatures(const FeatureBitset &FB) const;
90 void
91 verifyInstructionPredicates(const MCInst &MI,
92 const FeatureBitset &AvailableFeatures) const;
93};
94
95} // end anonymous namespace
96
97void SparcMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
98 SmallVectorImpl<MCFixup> &Fixups,
99 const MCSubtargetInfo &STI) const {
100 verifyInstructionPredicates(MI,
101 computeAvailableFeatures(STI.getFeatureBits()));
102
103 unsigned Bits = getBinaryCodeForInstr(MI, Fixups, STI);
104 support::endian::write(OS, Bits,
105 Ctx.getAsmInfo()->isLittleEndian() ? support::little
106 : support::big);
107 unsigned tlsOpNo = 0;
108 switch (MI.getOpcode()) {
109 default: break;
110 case SP::TLS_CALL: tlsOpNo = 1; break;
111 case SP::TLS_ADDrr:
112 case SP::TLS_ADDXrr:
113 case SP::TLS_LDrr:
114 case SP::TLS_LDXrr: tlsOpNo = 3; break;
115 }
116 if (tlsOpNo != 0) {
117 const MCOperand &MO = MI.getOperand(tlsOpNo);
118 uint64_t op = getMachineOpValue(MI, MO, Fixups, STI);
119 assert(op == 0 && "Unexpected operand value!")((op == 0 && "Unexpected operand value!") ? static_cast
<void> (0) : __assert_fail ("op == 0 && \"Unexpected operand value!\""
, "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 119, __PRETTY_FUNCTION__))
;
120 (void)op; // suppress warning.
121 }
122
123 ++MCNumEmitted; // Keep track of the # of mi's emitted.
124}
125
126unsigned SparcMCCodeEmitter::
127getMachineOpValue(const MCInst &MI, const MCOperand &MO,
128 SmallVectorImpl<MCFixup> &Fixups,
129 const MCSubtargetInfo &STI) const {
130 if (MO.isReg())
131 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
132
133 if (MO.isImm())
134 return MO.getImm();
135
136 assert(MO.isExpr())((MO.isExpr()) ? static_cast<void> (0) : __assert_fail (
"MO.isExpr()", "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 136, __PRETTY_FUNCTION__))
;
137 const MCExpr *Expr = MO.getExpr();
138 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
139 MCFixupKind Kind = (MCFixupKind)SExpr->getFixupKind();
140 Fixups.push_back(MCFixup::create(0, Expr, Kind));
141 return 0;
142 }
143
144 int64_t Res;
145 if (Expr->evaluateAsAbsolute(Res))
146 return Res;
147
148 llvm_unreachable("Unhandled expression!")::llvm::llvm_unreachable_internal("Unhandled expression!", "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 148)
;
149 return 0;
150}
151
152unsigned
153SparcMCCodeEmitter::getSImm13OpValue(const MCInst &MI, unsigned OpNo,
154 SmallVectorImpl<MCFixup> &Fixups,
155 const MCSubtargetInfo &STI) const {
156 const MCOperand &MO = MI.getOperand(OpNo);
157
158 if (MO.isImm())
159 return MO.getImm();
160
161 assert(MO.isExpr() &&((MO.isExpr() && "getSImm13OpValue expects only expressions or an immediate"
) ? static_cast<void> (0) : __assert_fail ("MO.isExpr() && \"getSImm13OpValue expects only expressions or an immediate\""
, "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 162, __PRETTY_FUNCTION__))
162 "getSImm13OpValue expects only expressions or an immediate")((MO.isExpr() && "getSImm13OpValue expects only expressions or an immediate"
) ? static_cast<void> (0) : __assert_fail ("MO.isExpr() && \"getSImm13OpValue expects only expressions or an immediate\""
, "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 162, __PRETTY_FUNCTION__))
;
163
164 const MCExpr *Expr = MO.getExpr();
165
166 // Constant value, no fixup is needed
167 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
168 return CE->getValue();
169
170 MCFixupKind Kind;
171 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr)) {
172 Kind = MCFixupKind(SExpr->getFixupKind());
173 } else {
174 bool IsPic = Ctx.getObjectFileInfo()->isPositionIndependent();
175 Kind = IsPic ? MCFixupKind(Sparc::fixup_sparc_got13)
176 : MCFixupKind(Sparc::fixup_sparc_13);
177 }
178
179 Fixups.push_back(MCFixup::create(0, Expr, Kind));
180 return 0;
181}
182
183unsigned SparcMCCodeEmitter::
184getCallTargetOpValue(const MCInst &MI, unsigned OpNo,
185 SmallVectorImpl<MCFixup> &Fixups,
186 const MCSubtargetInfo &STI) const {
187 const MCOperand &MO = MI.getOperand(OpNo);
188 const MCExpr *Expr = MO.getExpr();
189 const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Expr);
1
Assuming 'Expr' is not a 'SparcMCExpr'
2
'SExpr' initialized to a null pointer value
190
191 if (MI.getOpcode() == SP::TLS_CALL) {
3
Assuming the condition is false
4
Taking false branch
192 // No fixups for __tls_get_addr. Will emit for fixups for tls_symbol in
193 // encodeInstruction.
194#ifndef NDEBUG
195 // Verify that the callee is actually __tls_get_addr.
196 assert(SExpr && SExpr->getSubExpr()->getKind() == MCExpr::SymbolRef &&((SExpr && SExpr->getSubExpr()->getKind() == MCExpr
::SymbolRef && "Unexpected expression in TLS_CALL") ?
static_cast<void> (0) : __assert_fail ("SExpr && SExpr->getSubExpr()->getKind() == MCExpr::SymbolRef && \"Unexpected expression in TLS_CALL\""
, "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 197, __PRETTY_FUNCTION__))
197 "Unexpected expression in TLS_CALL")((SExpr && SExpr->getSubExpr()->getKind() == MCExpr
::SymbolRef && "Unexpected expression in TLS_CALL") ?
static_cast<void> (0) : __assert_fail ("SExpr && SExpr->getSubExpr()->getKind() == MCExpr::SymbolRef && \"Unexpected expression in TLS_CALL\""
, "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 197, __PRETTY_FUNCTION__))
;
198 const MCSymbolRefExpr *SymExpr = cast<MCSymbolRefExpr>(SExpr->getSubExpr());
199 assert(SymExpr->getSymbol().getName() == "__tls_get_addr" &&((SymExpr->getSymbol().getName() == "__tls_get_addr" &&
"Unexpected function for TLS_CALL") ? static_cast<void>
(0) : __assert_fail ("SymExpr->getSymbol().getName() == \"__tls_get_addr\" && \"Unexpected function for TLS_CALL\""
, "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 200, __PRETTY_FUNCTION__))
200 "Unexpected function for TLS_CALL")((SymExpr->getSymbol().getName() == "__tls_get_addr" &&
"Unexpected function for TLS_CALL") ? static_cast<void>
(0) : __assert_fail ("SymExpr->getSymbol().getName() == \"__tls_get_addr\" && \"Unexpected function for TLS_CALL\""
, "/build/llvm-toolchain-snapshot-13~++20210405022414+5f57793c4fe4/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp"
, 200, __PRETTY_FUNCTION__))
;
201#endif
202 return 0;
203 }
204
205 MCFixupKind Kind = MCFixupKind(SExpr->getFixupKind());
5
Called C++ object pointer is null
206 Fixups.push_back(MCFixup::create(0, Expr, Kind));
207 return 0;
208}
209
210unsigned SparcMCCodeEmitter::
211getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
212 SmallVectorImpl<MCFixup> &Fixups,
213 const MCSubtargetInfo &STI) const {
214 const MCOperand &MO = MI.getOperand(OpNo);
215 if (MO.isReg() || MO.isImm())
216 return getMachineOpValue(MI, MO, Fixups, STI);
217
218 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
219 (MCFixupKind)Sparc::fixup_sparc_br22));
220 return 0;
221}
222
223unsigned SparcMCCodeEmitter::
224getBranchPredTargetOpValue(const MCInst &MI, unsigned OpNo,
225 SmallVectorImpl<MCFixup> &Fixups,
226 const MCSubtargetInfo &STI) const {
227 const MCOperand &MO = MI.getOperand(OpNo);
228 if (MO.isReg() || MO.isImm())
229 return getMachineOpValue(MI, MO, Fixups, STI);
230
231 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
232 (MCFixupKind)Sparc::fixup_sparc_br19));
233 return 0;
234}
235
236unsigned SparcMCCodeEmitter::
237getBranchOnRegTargetOpValue(const MCInst &MI, unsigned OpNo,
238 SmallVectorImpl<MCFixup> &Fixups,
239 const MCSubtargetInfo &STI) const {
240 const MCOperand &MO = MI.getOperand(OpNo);
241 if (MO.isReg() || MO.isImm())
242 return getMachineOpValue(MI, MO, Fixups, STI);
243
244 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
245 (MCFixupKind)Sparc::fixup_sparc_br16_2));
246 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
247 (MCFixupKind)Sparc::fixup_sparc_br16_14));
248
249 return 0;
250}
251
252#define ENABLE_INSTR_PREDICATE_VERIFIER
253#include "SparcGenMCCodeEmitter.inc"
254
255MCCodeEmitter *llvm::createSparcMCCodeEmitter(const MCInstrInfo &MCII,
256 const MCRegisterInfo &MRI,
257 MCContext &Ctx) {
258 return new SparcMCCodeEmitter(MCII, Ctx);
259}