aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis/NaCl/PNaClABITypeChecker.cpp
blob: 8749abcaa6f5d2c2c9256ff062ead93275c166d2 (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
//===- PNaClABITypeChecker.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.
//
//===----------------------------------------------------------------------===//
//
// Common type-checking code for module and function-level passes
//
//
//===----------------------------------------------------------------------===//

#include "PNaClABITypeChecker.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Metadata.h"

using namespace llvm;

bool PNaClABITypeChecker::isValidParamType(const Type *Ty) {
  if (!isValidScalarType(Ty))
    return false;
  if (const IntegerType *IntTy = dyn_cast<IntegerType>(Ty)) {
    // PNaCl requires function arguments and return values to be 32
    // bits or larger.  This avoids exposing architecture
    // ABI-dependent differences about whether arguments or return
    // values are zero-extended when calling a function with the wrong
    // prototype.
    if (IntTy->getBitWidth() < 32)
      return false;
  }
  return true;
}

bool PNaClABITypeChecker::isValidFunctionType(const FunctionType *FTy) {
  if (FTy->isVarArg())
    return false;
  if (!isValidParamType(FTy->getReturnType()))
    return false;
  for (unsigned I = 0, E = FTy->getNumParams(); I < E; ++I) {
    if (!isValidParamType(FTy->getParamType(I)))
      return false;
  }
  return true;
}

bool PNaClABITypeChecker::isValidScalarType(const Type *Ty) {
  switch (Ty->getTypeID()) {
    case Type::IntegerTyID: {
      unsigned Width = cast<const IntegerType>(Ty)->getBitWidth();
      return Width == 1 || Width == 8 || Width == 16 ||
             Width == 32 || Width == 64;
    }
    case Type::VoidTyID:
    case Type::FloatTyID:
    case Type::DoubleTyID:
      return true;
    default:
      return false;
  }
}