aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp
blob: abc66b0addca49ac34139b9250abcb9f4462b4e4 (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
//===- PNaClABIVerifyFunctions.cpp - Verify PNaCl ABI rules --------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Verify function-level PNaCl ABI requirements.
//
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Twine.h"
#include "llvm/Function.h"
#include "llvm/Instruction.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Analysis/NaCl.h"
using namespace llvm;

namespace {

// Checks that examine anything in the function body should be in
// FunctionPasses to make them streaming-friendly
struct PNaClABIVerifyFunctions : public FunctionPass {
  static char ID;
  PNaClABIVerifyFunctions() : FunctionPass(ID) {}
  bool runOnFunction(Function &F);
  // For now, this print method exists to allow us to run the pass with
  // opt -analyze to avoid dumping the result to stdout, to make testing
  // simpler. In the future we will probably want to make it do something
  // useful.
  virtual void print(llvm::raw_ostream &O, const Module *M) const {};
};
} // and anonymous namespace

bool PNaClABIVerifyFunctions::runOnFunction(Function &F) {
  for (Function::const_iterator FI = F.begin(), FE = F.end();
           FI != FE; ++FI) {
    for (BasicBlock::const_iterator BBI = FI->begin(), BBE = FI->end();
             BBI != BBE; ++BBI) {
      switch (BBI->getOpcode()) {
        // Terminator instructions
        case Instruction::Ret:
        case Instruction::Br:
        case Instruction::Switch:
        case Instruction::Resume:
        case Instruction::Unreachable:
          // indirectbr is not allowed for now.
          // invoke and call are handled separately.
          break;
        default:
          errs() << Twine("Function ") + F.getName() +
              " has disallowed instruction: " +
              BBI->getOpcodeName() + "\n";
      }
    }
  }
  return false;
}

char PNaClABIVerifyFunctions::ID = 0;

static RegisterPass<PNaClABIVerifyFunctions> X("verify-pnaclabi-functions",
    "Verify functions for PNaCl", false, false);

FunctionPass *llvm::createPNaClABIVerifyFunctionsPass() {
  return new PNaClABIVerifyFunctions();
}