aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/NaCl/AddPNaClExternalDecls.cpp
blob: f96db09b2f3ca231c396d6d6ef54eda5f9193c2a (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
66
67
68
69
70
71
//===- AddPNaClExternalDecls.cpp - Add decls for PNaCl external functions -===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass adds function declarations for external functions used by PNaCl.
// These externals are implemented in native libraries and calls to them are
// created as part of the translation process.
//
// Running this pass is a precondition for running ResolvePNaClIntrinsics. They
// are separate because one is a ModulePass and the other is a FunctionPass.
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/NaCl.h"

using namespace llvm;

namespace {
  // This is a module pass because it adds declarations to the module.
  class AddPNaClExternalDecls : public ModulePass {
  public:
    static char ID;
    AddPNaClExternalDecls() : ModulePass(ID) {
      initializeAddPNaClExternalDeclsPass(*PassRegistry::getPassRegistry());
    }

    virtual bool runOnModule(Module &M);
  };
}

bool AddPNaClExternalDecls::runOnModule(Module &M) {
  // Add declarations for a pre-defined set of external functions to the module.
  // The function names must match the functions implemented in native code (in
  // pnacl/support). The function types must match the types of the LLVM
  // intrinsics.
  // We expect these declarations not to exist in the module before this pass
  // runs, but don't assert it; it will be handled by the ABI verifier.
  LLVMContext &C = M.getContext();
  M.getOrInsertFunction("setjmp",
                        // return type
                        Type::getInt32Ty(C),
                        // arguments
                        Type::getInt8Ty(C)->getPointerTo(),
                        NULL);
  M.getOrInsertFunction("longjmp",
                        // return type
                        Type::getVoidTy(C),
                        // arguments
                        Type::getInt8Ty(C)->getPointerTo(),
                        Type::getInt32Ty(C),
                        NULL);
  return true;
}

char AddPNaClExternalDecls::ID = 0;
INITIALIZE_PASS(AddPNaClExternalDecls, "add-pnacl-external-decls",
                "Add declarations of external functions used by PNaCl",
                false, false)

ModulePass *llvm::createAddPNaClExternalDeclsPass() {
  return new AddPNaClExternalDecls();
}