Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Instruction.cpp - Implement the Instruction class --------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the Instruction class for the VMCore library. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 7 | #include "llvm/Function.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 8 | #include "llvm/SymbolTable.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 9 | #include "llvm/Type.h" |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 10 | #include "Support/LeakDetector.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 12 | Instruction::Instruction(const Type *ty, unsigned it, const std::string &Name) |
Chris Lattner | 71947fd | 2002-02-03 07:52:58 +0000 | [diff] [blame] | 13 | : User(ty, Value::InstructionVal, Name) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | Parent = 0; |
| 15 | iType = it; |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 16 | |
| 17 | // Make sure that we get added to a basicblock |
| 18 | LeakDetector::addGarbageObject(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 21 | void Instruction::setParent(BasicBlock *P) { |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 22 | if (getParent()) |
| 23 | LeakDetector::addGarbageObject(this); |
| 24 | |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 25 | Parent = P; |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 26 | |
| 27 | if (getParent()) |
| 28 | LeakDetector::removeGarbageObject(this); |
Chris Lattner | bded132 | 2002-09-06 21:33:15 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 31 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 32 | void Instruction::setName(const std::string &name, SymbolTable *ST) { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 33 | BasicBlock *P = 0; Function *PP = 0; |
Chris Lattner | e05bf2f | 2001-09-07 16:47:03 +0000 | [diff] [blame] | 34 | assert((ST == 0 || !getParent() || !getParent()->getParent() || |
| 35 | ST == getParent()->getParent()->getSymbolTable()) && |
| 36 | "Invalid symtab argument!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | if ((P = getParent()) && (PP = P->getParent()) && hasName()) |
| 38 | PP->getSymbolTable()->remove(this); |
| 39 | Value::setName(name); |
| 40 | if (PP && hasName()) PP->getSymbolTableSure()->insert(this); |
| 41 | } |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | const char *Instruction::getOpcodeName(unsigned OpCode) { |
| 45 | switch (OpCode) { |
| 46 | // Terminators |
Chris Lattner | 0513e9f | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 47 | case Ret: return "ret"; |
| 48 | case Br: return "br"; |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 49 | case Switch: return "switch"; |
| 50 | case Invoke: return "invoke"; |
| 51 | |
Vikram S. Adve | c105645 | 2002-07-14 23:09:40 +0000 | [diff] [blame] | 52 | // Standard binary operators... |
| 53 | case Add: return "add"; |
| 54 | case Sub: return "sub"; |
| 55 | case Mul: return "mul"; |
| 56 | case Div: return "div"; |
| 57 | case Rem: return "rem"; |
| 58 | |
| 59 | // Logical operators... |
| 60 | case And: return "and"; |
| 61 | case Or : return "or"; |
| 62 | case Xor: return "xor"; |
| 63 | |
| 64 | // SetCC operators... |
| 65 | case SetLE: return "setle"; |
| 66 | case SetGE: return "setge"; |
| 67 | case SetLT: return "setlt"; |
| 68 | case SetGT: return "setgt"; |
| 69 | case SetEQ: return "seteq"; |
| 70 | case SetNE: return "setne"; |
| 71 | |
| 72 | // Memory instructions... |
| 73 | case Malloc: return "malloc"; |
| 74 | case Free: return "free"; |
| 75 | case Alloca: return "alloca"; |
| 76 | case Load: return "load"; |
| 77 | case Store: return "store"; |
| 78 | case GetElementPtr: return "getelementptr"; |
| 79 | |
| 80 | // Other instructions... |
| 81 | case PHINode: return "phi"; |
| 82 | case Cast: return "cast"; |
| 83 | case Call: return "call"; |
| 84 | case Shl: return "shl"; |
| 85 | case Shr: return "shr"; |
| 86 | |
| 87 | default: return "<Invalid operator> "; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |