Jim Laskey | 4556ce5 | 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 | 98a6979 | 2006-03-24 10:00:56 +0000 | [diff] [blame] | 5 | // This file was developed by James M. Laskey and is distributed under |
Jim Laskey | 4556ce5 | 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 | 98a6979 | 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 | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 27 | |
| 28 | #include "llvm/IntrinsicInst.h" |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 29 | #include "llvm/Constants.h" |
| 30 | #include "llvm/GlobalVariable.h" |
Jim Laskey | ca0dc56 | 2006-06-19 12:54:15 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineDebugInfo.h" |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics |
| 36 | /// |
| 37 | |
| 38 | static Value *CastOperand(Value *C) { |
| 39 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame^] | 40 | if (CE->isCast()) |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 41 | return CE->getOperand(0); |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | Value *DbgInfoIntrinsic::StripCast(Value *C) { |
| 46 | if (Value *CO = CastOperand(C)) { |
Jim Laskey | fbcf23c | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 47 | C = StripCast(CO); |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 48 | } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
| 49 | if (GV->hasInitializer()) |
| 50 | if (Value *CO = CastOperand(GV->getInitializer())) |
Jim Laskey | fbcf23c | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 51 | C = StripCast(CO); |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 52 | } |
Jim Laskey | fbcf23c | 2006-03-26 22:46:27 +0000 | [diff] [blame] | 53 | return dyn_cast<GlobalVariable>(C); |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction. |
| 58 | /// |
| 59 | |
| 60 | std::string DbgStopPointInst::getFileName() const { |
Jim Laskey | ca0dc56 | 2006-06-19 12:54:15 +0000 | [diff] [blame] | 61 | // Once the operand indices are verified, update this assert |
Jim Laskey | e2a78f2 | 2006-07-11 15:58:09 +0000 | [diff] [blame] | 62 | assert(LLVMDebugVersion == (5 << 16) && "Verify operand indices"); |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 63 | GlobalVariable *GV = cast<GlobalVariable>(getContext()); |
Chris Lattner | df20b96 | 2006-10-04 23:06:26 +0000 | [diff] [blame] | 64 | if (!GV->hasInitializer()) return ""; |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 65 | ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer()); |
Jim Laskey | f06ef2c | 2006-06-16 23:36:12 +0000 | [diff] [blame] | 66 | return CS->getOperand(3)->getStringValue(); |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | std::string DbgStopPointInst::getDirectory() const { |
Jim Laskey | ca0dc56 | 2006-06-19 12:54:15 +0000 | [diff] [blame] | 70 | // Once the operand indices are verified, update this assert |
Jim Laskey | e2a78f2 | 2006-07-11 15:58:09 +0000 | [diff] [blame] | 71 | assert(LLVMDebugVersion == (5 << 16) && "Verify operand indices"); |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 72 | GlobalVariable *GV = cast<GlobalVariable>(getContext()); |
Chris Lattner | df20b96 | 2006-10-04 23:06:26 +0000 | [diff] [blame] | 73 | if (!GV->hasInitializer()) return ""; |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 74 | ConstantStruct *CS = cast<ConstantStruct>(GV->getInitializer()); |
Jim Laskey | f06ef2c | 2006-06-16 23:36:12 +0000 | [diff] [blame] | 75 | return CS->getOperand(4)->getStringValue(); |
Jim Laskey | 4556ce5 | 2006-03-23 18:05:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
Reid Spencer | 4f1bd9e | 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) |