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