blob: 04c12e5d89efc3c70715faf4c4458159bc2c7e5d [file] [log] [blame]
Ben Claytonac07ed82019-03-26 14:17:41 +00001// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "LLVMReactorDebugInfo.hpp"
16
17#ifdef ENABLE_RR_DEBUG_INFO
18
Ben Clayton713b8d32019-12-17 20:37:56 +000019# include "LLVMReactor.hpp"
20# include "Reactor.hpp"
Antonio Maiorano415d1812020-02-11 16:22:55 -050021# include "Print.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000022
Ben Clayton713b8d32019-12-17 20:37:56 +000023# include "boost/stacktrace.hpp"
Ben Claytonac07ed82019-03-26 14:17:41 +000024
Nicolas Capens41a73022020-01-30 00:30:14 -050025// TODO(b/143539525): Eliminate when warning has been fixed.
26# ifdef _MSC_VER
27__pragma(warning(push))
28 __pragma(warning(disable : 4146)) // unary minus operator applied to unsigned type, result still unsigned
29# endif
30
Ben Clayton713b8d32019-12-17 20:37:56 +000031# include "llvm/Demangle/Demangle.h"
32# include "llvm/ExecutionEngine/JITEventListener.h"
33# include "llvm/IR/DIBuilder.h"
34# include "llvm/IR/IRBuilder.h"
35# include "llvm/IR/Intrinsics.h"
Ben Claytonac07ed82019-03-26 14:17:41 +000036
Nicolas Capens41a73022020-01-30 00:30:14 -050037# ifdef _MSC_VER
38 __pragma(warning(pop))
39# endif
40
Ben Clayton713b8d32019-12-17 20:37:56 +000041# include <cctype>
42# include <fstream>
43# include <mutex>
44# include <regex>
45# include <sstream>
46# include <string>
Ben Claytonac07ed82019-03-26 14:17:41 +000047
Ben Clayton713b8d32019-12-17 20:37:56 +000048# if 0
49# define LOG(msg, ...) printf(msg "\n", ##__VA_ARGS__)
50# else
51# define LOG(msg, ...)
52# endif
Ben Claytonac07ed82019-03-26 14:17:41 +000053
Nicolas Capens41a73022020-01-30 00:30:14 -050054 namespace
Ben Claytonac07ed82019-03-26 14:17:41 +000055{
Ben Clayton90cb2602019-05-23 14:42:32 +010056
Nicolas Capens41a73022020-01-30 00:30:14 -050057 std::pair<llvm::StringRef, llvm::StringRef> splitPath(const char *path)
58 {
59 return llvm::StringRef(path).rsplit('/');
60 }
61
62 // Note: createGDBRegistrationListener() returns a pointer to a singleton.
63 // Nothing is actually created.
64 auto jitEventListener = llvm::JITEventListener::createGDBRegistrationListener(); // guarded by jitEventListenerMutex
65 std::mutex jitEventListenerMutex;
Ben Clayton90cb2602019-05-23 14:42:32 +010066
Ben Clayton713b8d32019-12-17 20:37:56 +000067} // namespace
Ben Claytonac07ed82019-03-26 14:17:41 +000068
Nicolas Capens157ba262019-12-10 17:49:14 -050069namespace rr {
70
71DebugInfo::DebugInfo(
Ben Clayton713b8d32019-12-17 20:37:56 +000072 llvm::IRBuilder<> *builder,
73 llvm::LLVMContext *context,
74 llvm::Module *module,
75 llvm::Function *function)
76 : builder(builder)
77 , context(context)
78 , module(module)
79 , function(function)
Ben Claytonac07ed82019-03-26 14:17:41 +000080{
Nicolas Capens157ba262019-12-10 17:49:14 -050081 using namespace ::llvm;
Ben Claytonac07ed82019-03-26 14:17:41 +000082
Nicolas Capens157ba262019-12-10 17:49:14 -050083 auto location = getCallerLocation();
Ben Claytonac07ed82019-03-26 14:17:41 +000084
Nicolas Capens157ba262019-12-10 17:49:14 -050085 auto fileAndDir = splitPath(location.function.file.c_str());
86 diBuilder.reset(new llvm::DIBuilder(*module));
87 diCU = diBuilder->createCompileUnit(
Ben Clayton713b8d32019-12-17 20:37:56 +000088 llvm::dwarf::DW_LANG_C,
89 diBuilder->createFile(fileAndDir.first, fileAndDir.second),
90 "Reactor",
91 0, "", 0);
Ben Claytonac07ed82019-03-26 14:17:41 +000092
Nicolas Capens157ba262019-12-10 17:49:14 -050093 registerBasicTypes();
Ben Claytonac07ed82019-03-26 14:17:41 +000094
Nicolas Capens157ba262019-12-10 17:49:14 -050095 SmallVector<Metadata *, 8> EltTys;
96 auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray(EltTys));
Ben Claytonac07ed82019-03-26 14:17:41 +000097
Nicolas Capens157ba262019-12-10 17:49:14 -050098 auto file = getOrCreateFile(location.function.file.c_str());
99 auto sp = diBuilder->createFunction(
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400100 file, // scope
101 "ReactorFunction", // function name
102 "ReactorFunction", // linkage
103 file, // file
104 location.line, // line
105 funcTy, // type
106 location.line, // scope line
107 DINode::FlagPrototyped, // flags
108 DISubprogram::SPFlagDefinition // subprogram flags
Nicolas Capens157ba262019-12-10 17:49:14 -0500109 );
110 diSubprogram = sp;
111 function->setSubprogram(sp);
112 diRootLocation = DILocation::get(*context, location.line, 0, sp);
113 builder->SetCurrentDebugLocation(diRootLocation);
114}
Ben Claytonac07ed82019-03-26 14:17:41 +0000115
Nicolas Capens157ba262019-12-10 17:49:14 -0500116DebugInfo::~DebugInfo() = default;
Ben Clayton90cb2602019-05-23 14:42:32 +0100117
Nicolas Capens157ba262019-12-10 17:49:14 -0500118void DebugInfo::Finalize()
119{
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500120 while(diScope.size() > 0)
Ben Claytonac07ed82019-03-26 14:17:41 +0000121 {
Ben Clayton90cb2602019-05-23 14:42:32 +0100122 emitPending(diScope.back(), builder);
Nicolas Capens157ba262019-12-10 17:49:14 -0500123 diScope.pop_back();
124 }
125 diBuilder->finalize();
126}
127
128void DebugInfo::EmitLocation()
129{
Ben Clayton713b8d32019-12-17 20:37:56 +0000130 auto const &backtrace = getCallerBacktrace();
Nicolas Capens157ba262019-12-10 17:49:14 -0500131 syncScope(backtrace);
132 builder->SetCurrentDebugLocation(getLocation(backtrace, backtrace.size() - 1));
Antonio Maioranoaae33732020-02-14 14:52:34 -0500133 emitPrintLocation(backtrace);
Nicolas Capens157ba262019-12-10 17:49:14 -0500134}
135
136void DebugInfo::Flush()
137{
138 emitPending(diScope.back(), builder);
139}
140
Ben Clayton713b8d32019-12-17 20:37:56 +0000141void DebugInfo::syncScope(Backtrace const &backtrace)
Nicolas Capens157ba262019-12-10 17:49:14 -0500142{
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400143 using namespace ::llvm;
144
Ben Clayton713b8d32019-12-17 20:37:56 +0000145 auto shrink = [this](size_t newsize) {
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500146 while(diScope.size() > newsize)
Nicolas Capens157ba262019-12-10 17:49:14 -0500147 {
148 auto &scope = diScope.back();
149 LOG("- STACK(%d): di: %p, location: %s:%d",
Ben Clayton713b8d32019-12-17 20:37:56 +0000150 int(diScope.size() - 1), scope.di,
151 scope.location.function.file.c_str(),
152 int(scope.location.line));
Nicolas Capens157ba262019-12-10 17:49:14 -0500153 emitPending(scope, builder);
154 diScope.pop_back();
155 }
156 };
157
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500158 if(backtrace.size() < diScope.size())
Nicolas Capens157ba262019-12-10 17:49:14 -0500159 {
160 shrink(backtrace.size());
Ben Claytonac07ed82019-03-26 14:17:41 +0000161 }
162
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500163 for(size_t i = 0; i < diScope.size(); i++)
Ben Claytonac07ed82019-03-26 14:17:41 +0000164 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500165 auto &scope = diScope[i];
166 auto const &oldLocation = scope.location;
167 auto const &newLocation = backtrace[i];
Ben Claytonac07ed82019-03-26 14:17:41 +0000168
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500169 if(oldLocation.function != newLocation.function)
Ben Claytonac07ed82019-03-26 14:17:41 +0000170 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500171 LOG(" STACK(%d): Changed function %s -> %s", int(i),
Ben Clayton713b8d32019-12-17 20:37:56 +0000172 oldLocation.function.name.c_str(), newLocation.function.name.c_str());
Nicolas Capens157ba262019-12-10 17:49:14 -0500173 shrink(i);
174 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000175 }
176
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500177 if(oldLocation.line > newLocation.line)
Ben Claytonac07ed82019-03-26 14:17:41 +0000178 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500179 // Create a new di block to shadow all the variables in the loop.
180 auto file = getOrCreateFile(newLocation.function.file.c_str());
181 auto di = diBuilder->createLexicalBlock(scope.di, file, newLocation.line, 0);
182 LOG(" STACK(%d): Jumped backwards %d -> %d. di: %p -> %p", int(i),
Ben Clayton713b8d32019-12-17 20:37:56 +0000183 oldLocation.line, newLocation.line, scope.di, di);
Nicolas Capens157ba262019-12-10 17:49:14 -0500184 emitPending(scope, builder);
Ben Clayton713b8d32019-12-17 20:37:56 +0000185 scope = { newLocation, di };
186 shrink(i + 1);
Nicolas Capens157ba262019-12-10 17:49:14 -0500187 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000188 }
189
Nicolas Capens157ba262019-12-10 17:49:14 -0500190 scope.location = newLocation;
Ben Claytonac07ed82019-03-26 14:17:41 +0000191 }
192
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500193 while(backtrace.size() > diScope.size())
Ben Claytonac07ed82019-03-26 14:17:41 +0000194 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500195 auto i = diScope.size();
196 auto location = backtrace[i];
197 auto file = getOrCreateFile(location.function.file.c_str());
198 auto funcTy = diBuilder->createSubroutineType(diBuilder->getOrCreateTypeArray({}));
199
200 char buf[1024];
201 size_t size = sizeof(buf);
202 int status = 0;
203 llvm::itaniumDemangle(location.function.name.c_str(), buf, &size, &status);
204 auto name = "jit!" + (status == 0 ? std::string(buf) : location.function.name);
205
206 auto func = diBuilder->createFunction(
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400207 file, // scope
208 name, // function name
209 "", // linkage
210 file, // file
211 location.line, // line
212 funcTy, // type
213 location.line, // scope line
214 DINode::FlagPrototyped, // flags
215 DISubprogram::SPFlagDefinition // subprogram flags
Ben Claytonac07ed82019-03-26 14:17:41 +0000216 );
Ben Clayton713b8d32019-12-17 20:37:56 +0000217 diScope.push_back({ location, func });
Nicolas Capens157ba262019-12-10 17:49:14 -0500218 LOG("+ STACK(%d): di: %p, location: %s:%d", int(i), di,
Ben Clayton713b8d32019-12-17 20:37:56 +0000219 location.function.file.c_str(), int(location.line));
Ben Claytonac07ed82019-03-26 14:17:41 +0000220 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500221}
Ben Claytonac07ed82019-03-26 14:17:41 +0000222
Ben Clayton713b8d32019-12-17 20:37:56 +0000223llvm::DILocation *DebugInfo::getLocation(const Backtrace &backtrace, size_t i)
Nicolas Capens157ba262019-12-10 17:49:14 -0500224{
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500225 if(backtrace.size() == 0) { return nullptr; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500226 assert(backtrace.size() == diScope.size());
227 return llvm::DILocation::get(
Ben Clayton713b8d32019-12-17 20:37:56 +0000228 *context,
229 backtrace[i].line,
230 0,
231 diScope[i].di,
232 i > 0 ? getLocation(backtrace, i - 1) : diRootLocation);
Nicolas Capens157ba262019-12-10 17:49:14 -0500233}
234
235void DebugInfo::EmitVariable(Value *variable)
236{
Ben Clayton713b8d32019-12-17 20:37:56 +0000237 auto const &backtrace = getCallerBacktrace();
Nicolas Capens157ba262019-12-10 17:49:14 -0500238 syncScope(backtrace);
239
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500240 for(int i = backtrace.size() - 1; i >= 0; i--)
Ben Claytonac07ed82019-03-26 14:17:41 +0000241 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500242 auto const &location = backtrace[i];
243 auto tokens = getOrParseFileTokens(location.function.file.c_str());
244 auto tokIt = tokens->find(location.line);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500245 if(tokIt == tokens->end())
Ben Claytonac07ed82019-03-26 14:17:41 +0000246 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500247 break;
Ben Claytonac07ed82019-03-26 14:17:41 +0000248 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500249 auto token = tokIt->second;
250 auto name = token.identifier;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500251 if(token.kind == Token::Return)
Ben Claytonac07ed82019-03-26 14:17:41 +0000252 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500253 // This is a:
Ben Claytonac07ed82019-03-26 14:17:41 +0000254 //
Nicolas Capens157ba262019-12-10 17:49:14 -0500255 // return <expr>;
Ben Claytonac07ed82019-03-26 14:17:41 +0000256 //
Nicolas Capens157ba262019-12-10 17:49:14 -0500257 // Emit this expression as two variables -
258 // Once as a synthetic 'return_value' variable at this scope.
259 // Again by bubbling the expression value up the callstack as
260 // Return Value Optimizations (RVOs) are likely to carry across
261 // the value to a local without calling a constructor in
262 // statements like:
263 //
264 // auto val = foo();
265 //
266 name = "return_value";
Ben Claytonac07ed82019-03-26 14:17:41 +0000267 }
268
Nicolas Capens157ba262019-12-10 17:49:14 -0500269 auto &scope = diScope[i];
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500270 if(scope.pending.location != location)
Ben Claytonac07ed82019-03-26 14:17:41 +0000271 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500272 emitPending(scope, builder);
273 }
274
275 auto value = V(variable);
276 auto block = builder->GetInsertBlock();
277
278 auto insertAfter = block->size() > 0 ? &block->back() : nullptr;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500279 while(insertAfter != nullptr && insertAfter->isTerminator())
Nicolas Capens157ba262019-12-10 17:49:14 -0500280 {
281 insertAfter = insertAfter->getPrevNode();
Ben Claytonac07ed82019-03-26 14:17:41 +0000282 }
283
284 scope.pending = Pending{};
Nicolas Capens157ba262019-12-10 17:49:14 -0500285 scope.pending.name = name;
286 scope.pending.location = location;
287 scope.pending.diLocation = getLocation(backtrace, i);
288 scope.pending.value = value;
289 scope.pending.block = block;
290 scope.pending.insertAfter = insertAfter;
291 scope.pending.scope = scope.di;
Ben Claytonac07ed82019-03-26 14:17:41 +0000292
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500293 if(token.kind == Token::Return)
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400294 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500295 // Insert a noop instruction so the debugger can inspect the
296 // return value before the function scope closes.
297 scope.pending.addNopOnNextLine = true;
298 }
299 else
300 {
301 break;
302 }
303 }
304}
Ben Claytonac07ed82019-03-26 14:17:41 +0000305
Nicolas Capens157ba262019-12-10 17:49:14 -0500306void DebugInfo::emitPending(Scope &scope, IRBuilder *builder)
307{
308 auto const &pending = scope.pending;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500309 if(pending.value == nullptr)
Nicolas Capens157ba262019-12-10 17:49:14 -0500310 {
311 return;
312 }
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400313
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500314 if(!scope.symbols.emplace(pending.name).second)
Nicolas Capens157ba262019-12-10 17:49:14 -0500315 {
316 return;
317 }
318
319 bool isAlloca = llvm::isa<llvm::AllocaInst>(pending.value);
320
321 LOG(" EMIT(%s): di: %p, location: %s:%d, isAlloca: %s", pending.name.c_str(), scope.di,
Ben Clayton713b8d32019-12-17 20:37:56 +0000322 pending.location.function.file.c_str(), pending.location.line, isAlloca ? "true" : "false");
Nicolas Capens157ba262019-12-10 17:49:14 -0500323
324 auto value = pending.value;
325
326 IRBuilder::InsertPointGuard guard(*builder);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500327 if(pending.insertAfter != nullptr)
Nicolas Capens157ba262019-12-10 17:49:14 -0500328 {
329 builder->SetInsertPoint(pending.block, ++pending.insertAfter->getIterator());
330 }
331 else
332 {
333 builder->SetInsertPoint(pending.block);
334 }
335 builder->SetCurrentDebugLocation(pending.diLocation);
336
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500337 if(!isAlloca)
Nicolas Capens157ba262019-12-10 17:49:14 -0500338 {
339 // While insertDbgValueIntrinsic should be enough to declare a
340 // variable with no storage, variables of RValues can share the same
341 // llvm::Value, and only one can be named. Take for example:
342 //
343 // Int a = 42;
344 // RValue<Int> b = a;
345 // RValue<Int> c = b;
346 //
347 // To handle this, always promote named RValues to an alloca.
348
349 llvm::BasicBlock &entryBlock = function->getEntryBlock();
350 auto alloca = new llvm::AllocaInst(value->getType(), 0, pending.name);
351 entryBlock.getInstList().push_front(alloca);
352 builder->CreateStore(value, alloca);
353 value = alloca;
354 }
355
356 value->setName(pending.name);
357
358 auto diFile = getOrCreateFile(pending.location.function.file.c_str());
359 auto diType = getOrCreateType(value->getType()->getPointerElementType());
360 auto diVar = diBuilder->createAutoVariable(scope.di, pending.name, diFile, pending.location.line, diType);
361
362 auto di = diBuilder->insertDeclare(value, diVar, diBuilder->createExpression(), pending.diLocation, pending.block);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500363 if(pending.insertAfter != nullptr) { di->moveAfter(pending.insertAfter); }
Nicolas Capens157ba262019-12-10 17:49:14 -0500364
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500365 if(pending.addNopOnNextLine)
Nicolas Capens157ba262019-12-10 17:49:14 -0500366 {
367 builder->SetCurrentDebugLocation(llvm::DILocation::get(
Ben Clayton713b8d32019-12-17 20:37:56 +0000368 *context,
369 pending.diLocation->getLine() + 1,
370 0,
371 pending.diLocation->getScope(),
372 pending.diLocation->getInlinedAt()));
Nicolas Capens157ba262019-12-10 17:49:14 -0500373 Nop();
374 }
375
376 scope.pending = Pending{};
377}
378
379void DebugInfo::NotifyObjectEmitted(const llvm::object::ObjectFile &Obj, const llvm::LoadedObjectInfo &L)
380{
381 std::unique_lock<std::mutex> lock(jitEventListenerMutex);
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400382 auto key = reinterpret_cast<llvm::JITEventListener::ObjectKey>(&Obj);
383 jitEventListener->notifyObjectLoaded(key, Obj, static_cast<const llvm::RuntimeDyld::LoadedObjectInfo &>(L));
Nicolas Capens157ba262019-12-10 17:49:14 -0500384}
385
386void DebugInfo::NotifyFreeingObject(const llvm::object::ObjectFile &Obj)
387{
388 std::unique_lock<std::mutex> lock(jitEventListenerMutex);
Antonio Maiorano7d529ff2020-07-20 15:27:02 -0400389 auto key = reinterpret_cast<llvm::JITEventListener::ObjectKey>(&Obj);
390 jitEventListener->notifyFreeingObject(key);
Nicolas Capens157ba262019-12-10 17:49:14 -0500391}
392
393void DebugInfo::registerBasicTypes()
394{
395 using namespace rr;
396 using namespace llvm;
397
398 auto vec4 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 4));
399 auto vec8 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 8));
400 auto vec16 = diBuilder->getOrCreateArray(diBuilder->getOrCreateSubrange(0, 16));
401
Antonio Maiorano4b777772020-06-22 14:55:37 -0400402 diTypes.emplace(T(Bool::type()), diBuilder->createBasicType("Bool", sizeof(bool), dwarf::DW_ATE_boolean));
403 diTypes.emplace(T(Byte::type()), diBuilder->createBasicType("Byte", 8, dwarf::DW_ATE_unsigned_char));
404 diTypes.emplace(T(SByte::type()), diBuilder->createBasicType("SByte", 8, dwarf::DW_ATE_signed_char));
405 diTypes.emplace(T(Short::type()), diBuilder->createBasicType("Short", 16, dwarf::DW_ATE_signed));
406 diTypes.emplace(T(UShort::type()), diBuilder->createBasicType("UShort", 16, dwarf::DW_ATE_unsigned));
407 diTypes.emplace(T(Int::type()), diBuilder->createBasicType("Int", 32, dwarf::DW_ATE_signed));
408 diTypes.emplace(T(UInt::type()), diBuilder->createBasicType("UInt", 32, dwarf::DW_ATE_unsigned));
409 diTypes.emplace(T(Long::type()), diBuilder->createBasicType("Long", 64, dwarf::DW_ATE_signed));
410 diTypes.emplace(T(Half::type()), diBuilder->createBasicType("Half", 16, dwarf::DW_ATE_float));
411 diTypes.emplace(T(Float::type()), diBuilder->createBasicType("Float", 32, dwarf::DW_ATE_float));
Nicolas Capens157ba262019-12-10 17:49:14 -0500412
Antonio Maiorano4b777772020-06-22 14:55:37 -0400413 diTypes.emplace(T(Byte4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 }));
414 diTypes.emplace(T(SByte4::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 }));
415 diTypes.emplace(T(Byte8::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 }));
416 diTypes.emplace(T(SByte8::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 }));
417 diTypes.emplace(T(Byte16::type()), diBuilder->createVectorType(128, 128, diTypes[T(Byte::type())], { vec16 }));
418 diTypes.emplace(T(SByte16::type()), diBuilder->createVectorType(128, 128, diTypes[T(SByte::type())], { vec16 }));
419 diTypes.emplace(T(Short2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 }));
420 diTypes.emplace(T(UShort2::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 }));
421 diTypes.emplace(T(Short4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 }));
422 diTypes.emplace(T(UShort4::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 }));
423 diTypes.emplace(T(Short8::type()), diBuilder->createVectorType(128, 128, diTypes[T(Short::type())], { vec8 }));
424 diTypes.emplace(T(UShort8::type()), diBuilder->createVectorType(128, 128, diTypes[T(UShort::type())], { vec8 }));
425 diTypes.emplace(T(Int2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Int::type())], { vec4 }));
426 diTypes.emplace(T(UInt2::type()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::type())], { vec4 }));
427 diTypes.emplace(T(Int4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Int::type())], { vec4 }));
428 diTypes.emplace(T(UInt4::type()), diBuilder->createVectorType(128, 128, diTypes[T(UInt::type())], { vec4 }));
429 diTypes.emplace(T(Float2::type()), diBuilder->createVectorType(128, 128, diTypes[T(Float::type())], { vec4 }));
430 diTypes.emplace(T(Float4::type()), diBuilder->createVectorType(128, 128, diTypes[T(Float::type())], { vec4 }));
Nicolas Capens157ba262019-12-10 17:49:14 -0500431}
432
Antonio Maioranoaae33732020-02-14 14:52:34 -0500433Location DebugInfo::getCallerLocation() const
Nicolas Capens157ba262019-12-10 17:49:14 -0500434{
435 return getCallerBacktrace(1)[0];
436}
437
Antonio Maioranoaae33732020-02-14 14:52:34 -0500438Backtrace DebugInfo::getCallerBacktrace(size_t limit /* = 0 */) const
Nicolas Capens157ba262019-12-10 17:49:14 -0500439{
Antonio Maioranoaae33732020-02-14 14:52:34 -0500440 return rr::getCallerBacktrace(limit);
Nicolas Capens157ba262019-12-10 17:49:14 -0500441}
442
Ben Clayton713b8d32019-12-17 20:37:56 +0000443llvm::DIType *DebugInfo::getOrCreateType(llvm::Type *type)
Nicolas Capens157ba262019-12-10 17:49:14 -0500444{
445 auto it = diTypes.find(type);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500446 if(it != diTypes.end()) { return it->second; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500447
448 if(type->isPointerTy())
Ben Claytonac07ed82019-03-26 14:17:41 +0000449 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500450 auto dbgTy = diBuilder->createPointerType(
Ben Clayton713b8d32019-12-17 20:37:56 +0000451 getOrCreateType(type->getPointerElementType()),
452 sizeof(void *) * 8, alignof(void *) * 8);
Nicolas Capens157ba262019-12-10 17:49:14 -0500453 diTypes.emplace(type, dbgTy);
454 return dbgTy;
455 }
456 llvm::errs() << "Unimplemented debug type: " << type << "\n";
457 assert(false);
458 return nullptr;
459}
460
Ben Clayton713b8d32019-12-17 20:37:56 +0000461llvm::DIFile *DebugInfo::getOrCreateFile(const char *path)
Nicolas Capens157ba262019-12-10 17:49:14 -0500462{
463 auto it = diFiles.find(path);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500464 if(it != diFiles.end()) { return it->second; }
Nicolas Capens157ba262019-12-10 17:49:14 -0500465 auto dirAndName = splitPath(path);
466 auto file = diBuilder->createFile(dirAndName.second, dirAndName.first);
467 diFiles.emplace(path, file);
468 return file;
469}
470
Ben Clayton713b8d32019-12-17 20:37:56 +0000471DebugInfo::LineTokens const *DebugInfo::getOrParseFileTokens(const char *path)
Nicolas Capens157ba262019-12-10 17:49:14 -0500472{
473 static std::regex reLocalDecl(
Ben Clayton713b8d32019-12-17 20:37:56 +0000474 "^" // line start
475 "\\s*" // initial whitespace
476 "(?:For\\s*\\(\\s*)?" // optional 'For('
477 "((?:\\w+(?:<[^>]+>)?)(?:::\\w+(?:<[^>]+>)?)*)" // type (match group 1)
478 "\\s+" // whitespace between type and name
479 "(\\w+)" // identifier (match group 2)
480 "\\s*" // whitespace after identifier
481 "(\\[.*\\])?"); // optional array suffix (match group 3)
Nicolas Capens157ba262019-12-10 17:49:14 -0500482
483 auto it = fileTokens.find(path);
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500484 if(it != fileTokens.end())
Nicolas Capens157ba262019-12-10 17:49:14 -0500485 {
486 return it->second.get();
Ben Claytonac07ed82019-03-26 14:17:41 +0000487 }
488
Ben Clayton368d39c2020-01-08 23:10:47 +0000489 auto tokens = std::make_unique<LineTokens>();
Nicolas Capens157ba262019-12-10 17:49:14 -0500490
491 std::ifstream file(path);
492 std::string line;
493 int lineCount = 0;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500494 while(std::getline(file, line))
Ben Claytonac07ed82019-03-26 14:17:41 +0000495 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500496 lineCount++;
497 std::smatch match;
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500498 if(std::regex_search(line, match, reLocalDecl) && match.size() > 3)
Ben Claytonac07ed82019-03-26 14:17:41 +0000499 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500500 bool isArray = match.str(3) != "";
Ben Clayton713b8d32019-12-17 20:37:56 +0000501 if(!isArray) // Cannot deal with C-arrays of values.
Ben Claytonac07ed82019-03-26 14:17:41 +0000502 {
Nicolas Capens81bc9d92019-12-16 15:05:57 -0500503 if(match.str(1) == "return")
Ben Claytonac07ed82019-03-26 14:17:41 +0000504 {
Ben Clayton713b8d32019-12-17 20:37:56 +0000505 (*tokens)[lineCount] = Token{ Token::Return };
Nicolas Capens157ba262019-12-10 17:49:14 -0500506 }
507 else
508 {
Ben Clayton713b8d32019-12-17 20:37:56 +0000509 (*tokens)[lineCount] = Token{ Token::Identifier, match.str(2) };
Ben Claytonac07ed82019-03-26 14:17:41 +0000510 }
511 }
512 }
Ben Claytonac07ed82019-03-26 14:17:41 +0000513 }
514
Nicolas Capens157ba262019-12-10 17:49:14 -0500515 auto out = tokens.get();
516 fileTokens.emplace(path, std::move(tokens));
517 return out;
518}
519
520} // namespace rr
Ben Claytonac07ed82019-03-26 14:17:41 +0000521
Ben Clayton713b8d32019-12-17 20:37:56 +0000522#endif // ENABLE_RR_DEBUG_INFO