IRBindings.cpp raw

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