Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 1 | //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Jim Laskey | 864e444 | 2006-03-24 10:00:56 +0000 | [diff] [blame] | 5 | // This file was developed by James M. Laskey and is distributed under |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jim Laskey | 864e444 | 2006-03-24 10:00:56 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements methods that make it really easy to deal with intrinsic |
| 11 | // functions with the isa/dyncast family of functions. In particular, this |
| 12 | // allows you to do things like: |
| 13 | // |
| 14 | // if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(Inst)) |
| 15 | // ... SPI->getFileName() ... SPI->getDirectory() ... |
| 16 | // |
| 17 | // All intrinsic function calls are instances of the call instruction, so these |
| 18 | // are all subclasses of the CallInst class. Note that none of these classes |
| 19 | // has state or virtual methods, which is an important part of this gross/neat |
| 20 | // hack working. |
| 21 | // |
| 22 | // In some cases, arguments to intrinsics need to be generic and are defined as |
| 23 | // type pointer to empty struct { }*. To access the real item of interest the |
| 24 | // cast instruction needs to be stripped away. |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 27 | |
| 28 | #include "llvm/IntrinsicInst.h" |
| 29 | |
| 30 | #include "llvm/Constants.h" |
| 31 | #include "llvm/GlobalVariable.h" |
Jim Laskey | 90cd68a | 2006-06-19 12:54:15 +0000 | [diff] [blame^] | 32 | #include "llvm/CodeGen/MachineDebugInfo.h" |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics |
| 38 | /// |
| 39 | |
| 40 | static Value *CastOperand(Value *C) { |
| 41 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
| 42 | if (CE->getOpcode() == Instruction::Cast) |
| 43 | return CE->getOperand(0); |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | Value *DbgInfoIntrinsic::StripCast(Value *C) { |
| 48 | if (Value *CO = CastOperand(C)) { |
Jim Laskey | 7092888 | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 49 | C = StripCast(CO); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 50 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
| 51 | if (GV->hasInitializer()) |
| 52 | if (Value *CO = CastOperand(GV->getInitializer())) |
Jim Laskey | 7092888 | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 53 | C = StripCast(CO); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 54 | } |
Jim Laskey | 7092888 | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 55 | return dyn_cast<GlobalVariable>(C); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction. |
| 60 | /// |
| 61 | |
| 62 | std::string DbgStopPointInst::getFileName() const { |
Jim Laskey | 90cd68a | 2006-06-19 12:54:15 +0000 | [diff] [blame^] | 63 | // Once the operand indices are verified, update this assert |
| 64 | assert(LLVMDebugVersion == (4 << 16) && "Verify operand indices"); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 65 | GlobalVariable *GV = cast<GlobalVariable>(getContext()); |
| 66 | ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer()); |
Jim Laskey | 8dd2143 | 2006-06-16 23:36:12 +0000 | [diff] [blame] | 67 | return CS->getOperand(3)->getStringValue(); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | std::string DbgStopPointInst::getDirectory() const { |
Jim Laskey | 90cd68a | 2006-06-19 12:54:15 +0000 | [diff] [blame^] | 71 | // Once the operand indices are verified, update this assert |
| 72 | assert(LLVMDebugVersion == (4 << 16) && "Verify operand indices"); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 73 | GlobalVariable *GV = cast<GlobalVariable>(getContext()); |
| 74 | ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer()); |
Jim Laskey | 8dd2143 | 2006-06-16 23:36:12 +0000 | [diff] [blame] | 75 | return CS->getOperand(4)->getStringValue(); |
Jim Laskey | 0cf8ed6 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
Reid Spencer | be53566 | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 79 | /// Ensure that users of IntrinsicInst.h will link with this module. |
| 80 | DEFINING_FILE_FOR(IntrinsicInst) |