blob: 820fa5cb12dcf74cd854622cc55a61dcc522d155 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
2//
3// This file defines the method verifier interface, that can be used for some
4// sanity checking of input to the system.
5//
6// Note that this does not provide full 'java style' security and verifications,
7// instead it just tries to ensure that code is well formed.
8//
9// . There are no duplicated names in a symbol table... ie there !exist a val
10// with the same name as something in the symbol table, but with a different
11// address as what is in the symbol table...
12// . Both of a binary operator's parameters are the same type
13// . Only PHI nodes can refer to themselves
14// . All of the constants in a switch statement are of the correct type
15// . The code is in valid SSA form
16// . It should be illegal to put a label into any other type (like a structure)
17// or to return one. [except constant arrays!]
18// . Right now 'add bool 0, 0' is valid. This isn't particularly good.
19// . Only phi nodes can be self referential: 'add int 0, 0 ; <int>:0' is bad
20// . All other things that are tested by asserts spread about the code...
21// . All basic blocks should only end with terminator insts, not contain them
22// . All methods must have >= 1 basic block
23// . Verify that none of the Def getType()'s are null.
24// . Method's cannot take a void typed parameter
25// . Verify that a method's argument list agrees with it's declared type.
26// . Verify that arrays and structures have fixed elements: No unsized arrays.
27//
28//===----------------------------------------------------------------------===//
29
30#include "llvm/Analysis/Verifier.h"
31#include "llvm/ConstantPool.h"
32#include "llvm/Method.h"
33#include "llvm/Module.h"
34#include "llvm/BasicBlock.h"
35#include "llvm/Type.h"
36
37// Error - Define a macro to do the common task of pushing a message onto the
38// end of the error list and setting Bad to true.
39//
40#define Error(msg) do { ErrorMsgs.push_back(msg); Bad = true; } while (0)
41
42#define t(x) (1 << (unsigned)Type::x)
43
44#define SignedIntegralTypes (t(SByteTyID) | t(ShortTyID) | \
45 t(IntTyID) | t(LongTyID))
46static long UnsignedIntegralTypes = t(UByteTyID) | t(UShortTyID) |
47 t(UIntTyID) | t(ULongTyID);
48static const long FloatingPointTypes = t(FloatTyID) | t(DoubleTyID);
49
50static const long IntegralTypes = SignedIntegralTypes | UnsignedIntegralTypes;
51
52#if 0
53static long ValidTypes[Type::FirstDerivedTyID] = {
54 [(unsigned)Instruction::UnaryOps::Not] t(BoolTyID),
55 //[Instruction::UnaryOps::Add] = IntegralTypes,
56 // [Instruction::Sub] = IntegralTypes,
57};
58#endif
59
60#undef t
61
62static bool verify(const BasicBlock *BB, vector<string> &ErrorMsgs) {
63 bool Bad = false;
64 if (BB->getTerminator() == 0) Error("Basic Block does not have terminator!");
65
66
67 return Bad;
68}
69
70
71bool verify(const Method *M, vector<string> &ErrorMsgs) {
72 bool Bad = false;
73
74 for (Method::BasicBlocksType::const_iterator BBIt = M->getBasicBlocks().begin();
75 BBIt != M->getBasicBlocks().end(); BBIt++) {
76 Bad |= verify(*BBIt, ErrorMsgs);
77 }
78
79 return Bad;
80}
81
82bool verify(const Module *C, vector<string> &ErrorMsgs) {
83 bool Bad = false;
84 assert(Type::FirstDerivedTyID-1 < sizeof(long)*8 &&
85 "Resize ValidTypes table to handle more than 32 primitive types!");
86
87 for (Module::MethodListType::const_iterator MI = C->getMethodList().begin();
88 MI != C->getMethodList().end(); MI++) {
89 const Method *M = *MI;
90 Bad |= verify(M, ErrorMsgs);
91 }
92
93 return Bad;
94}