IRBindings.cpp raw

   1  //===- IRBindings.cpp - Additional bindings for ir ------------------------===//
   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 defines additional C bindings for the ir component.
  10  //
  11  //===----------------------------------------------------------------------===//
  12  
  13  #include "IRBindings.h"
  14  #include "llvm/IR/Attributes.h"
  15  #include "llvm/IR/DebugLoc.h"
  16  #include "llvm/IR/DebugInfoMetadata.h"
  17  #include "llvm/IR/Function.h"
  18  #include "llvm/IR/IRBuilder.h"
  19  #include "llvm/IR/LLVMContext.h"
  20  #include "llvm/IR/Module.h"
  21  
  22  using namespace llvm;
  23  
  24  LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef C) {
  25    return wrap(ConstantAsMetadata::get(unwrap<Constant>(C)));
  26  }
  27  
  28  LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen) {
  29    return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
  30  }
  31  
  32  LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
  33                              unsigned Count) {
  34    return wrap(
  35        MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
  36  }
  37  
  38  void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
  39                                    LLVMMetadataRef Val) {
  40    NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
  41    if (!N)
  42      return;
  43    if (!Val)
  44      return;
  45    N->addOperand(unwrap<MDNode>(Val));
  46  }
  47  
  48  void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
  49    MDNode *N = MD ? unwrap<MDNode>(MD) : nullptr;
  50    unwrap<Instruction>(Inst)->setMetadata(KindID, N);
  51  }
  52  
  53  void LLVMGoSetCurrentDebugLocation(LLVMBuilderRef Bref, unsigned Line,
  54                                    unsigned Col, LLVMMetadataRef Scope,
  55                                    LLVMMetadataRef InlinedAt) {
  56    if (!Scope)
  57      unwrap(Bref)->SetCurrentDebugLocation(DebugLoc());
  58    else
  59      unwrap(Bref)->SetCurrentDebugLocation(DILocation::get(
  60          unwrap<MDNode>(Scope)->getContext(), Line, Col, unwrap<MDNode>(Scope),
  61          InlinedAt ? unwrap<MDNode>(InlinedAt) : nullptr));
  62  }
  63  
  64  LLVMDebugLocMetadata LLVMGoGetCurrentDebugLocation(LLVMBuilderRef Bref) {
  65    const auto& Loc = unwrap(Bref)->getCurrentDebugLocation();
  66    const auto* InlinedAt = Loc.getInlinedAt();
  67    const LLVMDebugLocMetadata md{
  68      Loc.getLine(),
  69      Loc.getCol(),
  70      wrap(Loc.getScope()),
  71      InlinedAt == nullptr ? nullptr : wrap(InlinedAt->getRawInlinedAt()),
  72    };
  73    return md;
  74  }
  75  
  76  LLVMValueRef LLVMGoGetInlineAsm(LLVMTypeRef Ty, char *AsmString,
  77                                  size_t AsmStringSize, char *Constraints,
  78                                  size_t ConstraintsSize, LLVMBool HasSideEffects,
  79                                  LLVMBool IsAlignStack,
  80                                  LLVMInlineAsmDialect Dialect, LLVMBool CanThrow)
  81  {
  82    return LLVMGetInlineAsm(Ty, AsmString,
  83                            AsmStringSize, Constraints,
  84                            ConstraintsSize, HasSideEffects,
  85                            IsAlignStack,
  86                            Dialect, CanThrow);
  87  }
  88