"Fossies" - the Fresh Open Source Software Archive

Member "ponyc-0.33.2/src/libponyc/codegen/gendebug.cc" (3 Feb 2020, 10529 Bytes) of package /linux/misc/ponyc-0.33.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gendebug.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.33.1_vs_0.33.2.

    1 #include "codegen.h"
    2 #include "gendebug.h"
    3 
    4 #include "llvm_config_begin.h"
    5 
    6 #include <llvm/IR/DIBuilder.h>
    7 #include <llvm/IR/IRBuilder.h>
    8 #include <llvm/IR/Module.h>
    9 #include <llvm/Support/Path.h>
   10 
   11 #include "llvm_config_end.h"
   12 
   13 #define DW_TAG_auto_variable 0x100
   14 #define DW_TAG_arg_variable 0x101
   15 
   16 namespace llvm
   17 {
   18 #if PONY_LLVM < 500
   19   DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef)
   20 
   21   inline Metadata** unwrap(LLVMMetadataRef* md)
   22   {
   23     return reinterpret_cast<Metadata**>(md);
   24   }
   25   #endif
   26 }
   27 
   28 using namespace llvm;
   29 
   30 #if PONY_LLVM < 500
   31 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DIBuilder, LLVMDIBuilderRef);
   32 #endif
   33 
   34 #if PONY_LLVM < 700
   35 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef md_old,
   36   LLVMMetadataRef md_new)
   37 {
   38   MDNode* node = unwrap<MDNode>(md_old);
   39   node->replaceAllUsesWith(unwrap<Metadata>(md_new));
   40   MDNode::deleteTemporary(node);
   41 }
   42 #endif
   43 
   44 LLVMDIBuilderRef LLVMNewDIBuilder(LLVMModuleRef m)
   45 {
   46   Module* pm = unwrap(m);
   47 
   48 #ifdef _MSC_VER
   49   pm->addModuleFlag(Module::Warning, "CodeView", 1);
   50 #else
   51   unsigned dwarf = dwarf::DWARF_VERSION;
   52   unsigned debug_info = DEBUG_METADATA_VERSION;
   53 
   54   pm->addModuleFlag(Module::Warning, "Dwarf Version", dwarf);
   55   pm->addModuleFlag(Module::Warning, "Debug Info Version", debug_info);
   56 #endif
   57 
   58   return wrap(new DIBuilder(*pm));
   59 }
   60 
   61 void LLVMDIBuilderDestroy(LLVMDIBuilderRef d)
   62 {
   63   DIBuilder* pd = unwrap(d);
   64   delete pd;
   65 }
   66 
   67 #if PONY_LLVM < 600
   68 void LLVMDIBuilderFinalize(LLVMDIBuilderRef d)
   69 {
   70   unwrap(d)->finalize();
   71 }
   72 
   73 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(LLVMDIBuilderRef d,
   74   unsigned lang, const char* file, const char* dir, const char* producer,
   75   int optimized)
   76 {
   77   DIBuilder* pd = unwrap(d);
   78   const StringRef flags = "";
   79   const unsigned runtimever = 0;
   80 
   81 #if PONY_LLVM >= 400
   82   DIFile* difile = pd->createFile(file, dir);
   83   return wrap(pd->createCompileUnit(lang, difile, producer,
   84     optimized ? true : false, flags, runtimever));
   85 #else
   86   return wrap(pd->createCompileUnit(lang, file, dir, producer, optimized ? true : false,
   87     flags, runtimever, StringRef())); // use the defaults
   88 #endif
   89 }
   90 
   91 LLVMMetadataRef LLVMDIBuilderCreateFile(LLVMDIBuilderRef d, const char* file)
   92 {
   93   DIBuilder* pd = unwrap(d);
   94 
   95   StringRef filename = sys::path::filename(file);
   96   StringRef dir = sys::path::parent_path(file);
   97 
   98   return wrap(pd->createFile(filename, dir));
   99 }
  100 #endif
  101 
  102 LLVMMetadataRef LLVMDIBuilderCreateNamespace(LLVMDIBuilderRef d,
  103   LLVMMetadataRef scope, const char* name, LLVMMetadataRef file, unsigned line)
  104 {
  105   DIBuilder* pd = unwrap(d);
  106 #if PONY_LLVM >= 400
  107 #  if PONY_LLVM >= 500
  108   (void)file;
  109   (void)line;
  110   return wrap(pd->createNameSpace(unwrap<DIScope>(scope), name, false));
  111 #  else
  112   return wrap(pd->createNameSpace(unwrap<DIScope>(scope), name,
  113     unwrap<DIFile>(file), line, false));
  114 #  endif
  115 #else
  116   return wrap(pd->createNameSpace(unwrap<DIScope>(scope), name,
  117     unwrap<DIFile>(file), line));
  118 #endif
  119 }
  120 
  121 #if PONY_LLVM < 700
  122 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(LLVMDIBuilderRef d,
  123   LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line, unsigned col)
  124 {
  125   DIBuilder* pd = unwrap(d);
  126 
  127   return wrap(pd->createLexicalBlock(
  128     unwrap<DILocalScope>(scope), unwrap<DIFile>(file), line, col));
  129 }
  130 #endif
  131 
  132 LLVMMetadataRef LLVMDIBuilderCreateMethod(LLVMDIBuilderRef d,
  133   LLVMMetadataRef scope, const char* name, const char* linkage,
  134   LLVMMetadataRef file, unsigned line, LLVMMetadataRef type, LLVMValueRef func,
  135   int optimized)
  136 {
  137   DIBuilder* pd = unwrap(d);
  138   Function* f = unwrap<Function>(func);
  139 
  140 #if PONY_LLVM >= 800
  141   DISubprogram::DISPFlags sp_flags = DISubprogram::toSPFlags(false, true,
  142     optimized ? true : false);
  143 
  144   DISubprogram* di_method = pd->createMethod(unwrap<DIScope>(scope),
  145     name, linkage, unwrap<DIFile>(file), line, unwrap<DISubroutineType>(type),
  146     0, 0, nullptr, DINode::FlagZero, sp_flags, nullptr, nullptr);
  147 #elif PONY_LLVM >= 400
  148   DISubprogram* di_method = pd->createMethod(unwrap<DIScope>(scope),
  149     name, linkage, unwrap<DIFile>(file), line, unwrap<DISubroutineType>(type),
  150     false, true, 0, 0, 0, nullptr, DINode::FlagZero, optimized ? true : false);
  151 #else
  152   DISubprogram* di_method = pd->createMethod(unwrap<DIScope>(scope),
  153     name, linkage, unwrap<DIFile>(file), line, unwrap<DISubroutineType>(type),
  154     false, true, 0, 0, 0, 0, optimized);
  155 #endif
  156 
  157   f->setSubprogram(di_method);
  158   return wrap(di_method);
  159 }
  160 
  161 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(LLVMDIBuilderRef d,
  162   LLVMMetadataRef scope, const char* name, LLVMMetadataRef file,
  163   unsigned line, LLVMMetadataRef type)
  164 {
  165   DIBuilder* pd = unwrap(d);
  166 
  167 #if PONY_LLVM >= 400
  168   return wrap(pd->createAutoVariable(unwrap<DIScope>(scope), name,
  169     unwrap<DIFile>(file), line, unwrap<DIType>(type), true, DINode::FlagZero));
  170 #else
  171   return wrap(pd->createAutoVariable(unwrap<DIScope>(scope), name,
  172     unwrap<DIFile>(file), line, unwrap<DIType>(type), true, 0));
  173 #endif
  174 }
  175 
  176 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(LLVMDIBuilderRef d,
  177   LLVMMetadataRef scope, const char* name, unsigned arg,
  178   LLVMMetadataRef file, unsigned line, LLVMMetadataRef type)
  179 {
  180   DIBuilder* pd = unwrap(d);
  181 
  182 #if PONY_LLVM >= 400
  183   return wrap(pd->createParameterVariable(
  184     unwrap<DIScope>(scope), name, arg, unwrap<DIFile>(file), line,
  185     unwrap<DIType>(type), true, DINode::FlagZero));
  186 #else
  187   return wrap(pd->createParameterVariable(
  188     unwrap<DIScope>(scope), name, arg, unwrap<DIFile>(file), line,
  189     unwrap<DIType>(type), true, 0));
  190 #endif
  191 }
  192 
  193 LLVMMetadataRef LLVMDIBuilderCreateArtificialVariable(LLVMDIBuilderRef d,
  194   LLVMMetadataRef scope, const char* name, unsigned arg,
  195   LLVMMetadataRef file, unsigned line, LLVMMetadataRef type)
  196 {
  197   DIBuilder* pd = unwrap(d);
  198 
  199   return wrap(pd->createParameterVariable(
  200     unwrap<DIScope>(scope), name, arg, unwrap<DIFile>(file), line,
  201     pd->createArtificialType(unwrap<DIType>(type)),
  202     true, DINode::FlagArtificial));
  203 }
  204 
  205 #if PONY_LLVM < 700
  206 LLVMMetadataRef LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef d,
  207   const char* name, uint64_t size_bits, uint64_t align_bits,
  208   unsigned encoding)
  209 {
  210   DIBuilder* pd = unwrap(d);
  211 
  212 #if PONY_LLVM >= 400
  213   (void)(align_bits);
  214   return wrap(pd->createBasicType(name, size_bits, encoding));
  215 #else
  216   return wrap(pd->createBasicType(name, size_bits, align_bits, encoding));
  217 #endif
  218 }
  219 #endif
  220 
  221 #if PONY_LLVM < 700
  222 LLVMMetadataRef LLVMDIBuilderCreatePointerType(LLVMDIBuilderRef d,
  223   LLVMMetadataRef elem_type, uint64_t size_bits, uint64_t align_bits)
  224 {
  225   DIBuilder* pd = unwrap(d);
  226 
  227   return wrap(pd->createPointerType(unwrap<DIType>(elem_type), size_bits,
  228     static_cast<uint32_t>(align_bits)));
  229 }
  230 #endif
  231 
  232 #if PONY_LLVM < 700
  233 LLVMMetadataRef LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef d,
  234   LLVMMetadataRef file, LLVMMetadataRef param_types)
  235 {
  236   DIBuilder* pd = unwrap(d);
  237 
  238   (void)file;
  239   return wrap(pd->createSubroutineType(
  240     DITypeRefArray(unwrap<MDTuple>(param_types))));
  241 }
  242 
  243 LLVMMetadataRef LLVMDIBuilderCreateStructType(LLVMDIBuilderRef d,
  244   LLVMMetadataRef scope, const char* name, LLVMMetadataRef file,
  245   unsigned line, uint64_t size_bits, uint64_t align_bits,
  246   LLVMMetadataRef elem_types)
  247 {
  248   DIBuilder* pd = unwrap(d);
  249 
  250 #if PONY_LLVM >= 400
  251   return wrap(pd->createStructType(unwrap<DIScope>(scope), name,
  252     unwrap<DIFile>(file), line, size_bits,
  253     static_cast<uint32_t>(align_bits), DINode::FlagZero, nullptr,
  254     elem_types ? DINodeArray(unwrap<MDTuple>(elem_types)) : nullptr));
  255 #else
  256   return wrap(pd->createStructType(unwrap<DIScope>(scope), name,
  257     unwrap<DIFile>(file), line, size_bits, align_bits, 0, nullptr,
  258     elem_types ? DINodeArray(unwrap<MDTuple>(elem_types)) : nullptr));
  259 #endif
  260 }
  261 #endif
  262 
  263 LLVMMetadataRef LLVMDIBuilderCreateReplaceableStruct(LLVMDIBuilderRef d,
  264   const char* name, LLVMMetadataRef scope, LLVMMetadataRef file, unsigned line)
  265 {
  266   DIBuilder* pd = unwrap(d);
  267 
  268   return wrap(pd->createReplaceableCompositeType(dwarf::DW_TAG_structure_type,
  269     name, unwrap<DIScope>(scope), unwrap<DIFile>(file), line));
  270 }
  271 
  272 LLVMMetadataRef LLVMDIBuilderCreateMemberType(LLVMDIBuilderRef d,
  273   LLVMMetadataRef scope, const char* name, LLVMMetadataRef file,
  274   unsigned line, uint64_t size_bits, uint64_t align_bits,
  275   uint64_t offset_bits, unsigned flags, LLVMMetadataRef type)
  276 {
  277   DIBuilder* pd = unwrap(d);
  278 
  279 #if PONY_LLVM >= 400
  280   return wrap(pd->createMemberType(unwrap<DIScope>(scope), name,
  281     unwrap<DIFile>(file), line, size_bits, static_cast<uint32_t>(align_bits),
  282     offset_bits, flags ? DINode::FlagPrivate : DINode::FlagZero,
  283     unwrap<DIType>(type)));
  284 #else
  285   return wrap(pd->createMemberType(unwrap<DIScope>(scope), name,
  286     unwrap<DIFile>(file), line, size_bits, align_bits,
  287     offset_bits, flags, unwrap<DIType>(type)));
  288 #endif
  289 }
  290 
  291 LLVMMetadataRef LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef d,
  292   uint64_t size_bits, uint64_t align_bits,
  293   LLVMMetadataRef elem_type, LLVMMetadataRef subscripts)
  294 {
  295   DIBuilder* pd = unwrap(d);
  296 
  297   return wrap(pd->createArrayType(size_bits, static_cast<uint32_t>(align_bits),
  298     unwrap<DIType>(elem_type), DINodeArray(unwrap<MDTuple>(subscripts))));
  299 }
  300 
  301 #if PONY_LLVM < 700
  302 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef d,
  303   LLVMMetadataRef* data, size_t length)
  304 {
  305   DIBuilder* pd = unwrap(d);
  306   Metadata** md = unwrap(data);
  307   ArrayRef<Metadata*> elems(md, length);
  308 
  309   DINodeArray a = pd->getOrCreateArray(elems);
  310   return wrap(a.get());
  311 }
  312 
  313 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef d,
  314   LLVMMetadataRef* data, size_t length)
  315 {
  316   DIBuilder* pd = unwrap(d);
  317   Metadata** md = unwrap(data);
  318   ArrayRef<Metadata*> elems(md, length);
  319 
  320   DITypeRefArray a = pd->getOrCreateTypeArray(elems);
  321   return wrap(a.get());
  322 }
  323 
  324 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef d,
  325   int64_t* addr, size_t length)
  326 {
  327   DIBuilder* pd = unwrap(d);
  328   return wrap(pd->createExpression(ArrayRef<int64_t>(addr, length)));
  329 }
  330 #endif
  331 
  332 LLVMValueRef LLVMDIBuilderInsertDeclare(LLVMDIBuilderRef d,
  333   LLVMValueRef value, LLVMMetadataRef info, LLVMMetadataRef expr,
  334   unsigned line, unsigned col, LLVMMetadataRef scope, LLVMBasicBlockRef block)
  335 {
  336   DIBuilder* pd = unwrap(d);
  337 
  338   return wrap(pd->insertDeclare(unwrap(value),
  339     unwrap<DILocalVariable>(info), unwrap<DIExpression>(expr),
  340     DebugLoc::get(line, col, scope ? unwrap<MDNode>(scope) : nullptr, nullptr),
  341     unwrap(block)));
  342 }
  343 
  344 void LLVMSetCurrentDebugLocation2(LLVMBuilderRef b,
  345   unsigned line, unsigned col, LLVMMetadataRef scope)
  346 {
  347   unwrap(b)->SetCurrentDebugLocation(
  348     DebugLoc::get(line, col,
  349       scope ? unwrap<MDNode>(scope) : nullptr, nullptr));
  350 }