about summary refs log tree commit diff
path: root/src/rustllvm/PassWrapper.cpp
blob: 53b291ff470d196e67866277fbc5937d2adc6602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#include "rustllvm.h"

using namespace llvm;

// Pass conversion fns
typedef struct LLVMOpaquePass *LLVMPassRef;

inline Pass *unwrap(LLVMPassRef P) {
    return reinterpret_cast<Pass*>(P);
}

inline LLVMPassRef wrap(const Pass *P) {
    return reinterpret_cast<LLVMPassRef>(const_cast<Pass*>(P));
}

template<typename T>
inline T *unwrap(LLVMPassRef P) {
    T *Q = (T*)unwrap(P);
    assert(Q && "Invalid cast!");
    return Q;
}

extern "C" void LLVMInitializePasses() {
  PassRegistry &Registry = *PassRegistry::getPassRegistry();
  initializeCore(Registry);
  initializeCodeGen(Registry);
  initializeScalarOpts(Registry);
  initializeVectorization(Registry);
  initializeIPO(Registry);
  initializeAnalysis(Registry);
  initializeIPA(Registry);
  initializeTransformUtils(Registry);
  initializeInstCombine(Registry);
  initializeInstrumentation(Registry);
  initializeTarget(Registry);
}

extern "C" void LLVMAddPass(LLVMPassManagerRef PM, LLVMPassRef P) {
    PassManagerBase * pm = unwrap(PM);
    Pass * p = unwrap(P);

    pm->add(p);
}

extern "C" LLVMPassRef LLVMCreatePass(const char * PassName) {
    StringRef SR(PassName);
    PassRegistry * PR = PassRegistry::getPassRegistry();

    const PassInfo * PI = PR->getPassInfo(SR);
    if (PI) {
        return wrap(PI->createPass());
    } else {
        return (LLVMPassRef)0;
    }
}