Add support for casting operators
[oota-llvm.git] / lib / VMCore / iOperators.cpp
1 //===-- iOperators.cpp - Implement the Binary & Unary Operators --*- C++ -*--=//
2 //
3 // This file implements the nontrivial binary & unary operator instructions.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iOperators.h"
8 #include "llvm/Type.h"
9
10 //===----------------------------------------------------------------------===//
11 //                              UnaryOperator Class
12 //===----------------------------------------------------------------------===//
13
14 UnaryOperator *UnaryOperator::create(UnaryOps Op, Value *Source) {
15   switch (Op) {
16   case Not:  return new GenericUnaryInst(Op, Source);
17   default:
18     cerr << "Don't know how to Create UnaryOperator " << Op << endl;
19     return 0;
20   }
21 }
22
23
24 //===----------------------------------------------------------------------===//
25 //                           GenericUnaryOperator Class
26 //===----------------------------------------------------------------------===//
27
28 const char *GenericUnaryInst::getOpcodeName() const {
29   switch (getOpcode()) {
30   case Not: return "not";
31   case Cast: return "cast";
32   default:
33     cerr << "Invalid unary operator type!" << getOpcode() << endl;
34     abort();
35   }
36 }
37
38
39 //===----------------------------------------------------------------------===//
40 //                             BinaryOperator Class
41 //===----------------------------------------------------------------------===//
42
43 BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
44                                        const string &Name) {
45   switch (Op) {
46   // Binary comparison operators...
47   case SetLT: case SetGT: case SetLE:
48   case SetGE: case SetEQ: case SetNE:
49     return new SetCondInst(Op, S1, S2, Name);
50
51   default:
52     return new GenericBinaryInst(Op, S1, S2, Name);
53   }
54 }
55
56
57 //===----------------------------------------------------------------------===//
58 //                            GenericBinaryInst Class
59 //===----------------------------------------------------------------------===//
60
61 const char *GenericBinaryInst::getOpcodeName() const {
62   switch (getOpcode()) {
63   // Standard binary operators...
64   case Add: return "add";
65   case Sub: return "sub";
66   case Mul: return "mul";
67   case Div: return "div";
68   case Rem: return "rem";
69
70   // Logical operators...
71   case And: return "and";
72   case Or : return "or";
73   case Xor: return "xor";
74   default:
75     cerr << "Invalid binary operator type!" << getOpcode() << endl;
76     abort();
77   }
78 }
79
80
81 //===----------------------------------------------------------------------===//
82 //                             SetCondInst Class
83 //===----------------------------------------------------------------------===//
84
85 SetCondInst::SetCondInst(BinaryOps opType, Value *S1, Value *S2, 
86                          const string &Name) 
87   : BinaryOperator(opType, S1, S2, Name) {
88
89   OpType = opType;
90   setType(Type::BoolTy);   // setcc instructions always return bool type.
91
92   // Make sure it's a valid type...
93   assert(getOpcodeName() != 0);
94 }
95
96 const char *SetCondInst::getOpcodeName() const {
97   switch (OpType) {
98   case SetLE:  return "setle";
99   case SetGE:  return "setge";
100   case SetLT:  return "setlt";
101   case SetGT:  return "setgt";
102   case SetEQ:  return "seteq";
103   case SetNE:  return "setne";
104   default:
105     assert(0 && "Invalid opcode type to SetCondInst class!");
106     return "invalid opcode type to SetCondInst";
107   }
108 }