Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 1 | //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the AsmPrinter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/AsmPrinter.h" |
| 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/Instruction.h" |
| 17 | #include "llvm/Support/Mangler.h" |
| 18 | #include "llvm/Target/TargetMachine.h" |
| 19 | using namespace llvm; |
| 20 | |
| 21 | bool AsmPrinter::doInitialization(Module &M) { |
Chris Lattner | af2bf0a | 2004-08-17 06:06:19 +0000 | [diff] [blame] | 22 | Mang = new Mangler(M, GlobalPrefix); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 23 | return false; |
| 24 | } |
| 25 | |
| 26 | bool AsmPrinter::doFinalization(Module &M) { |
| 27 | delete Mang; Mang = 0; |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | void AsmPrinter::setupMachineFunction(MachineFunction &MF) { |
| 32 | // What's my mangled name? |
| 33 | CurrentFnName = Mang->getValueName((Value*)MF.getFunction()); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Chris Lattner | bfddc20 | 2004-08-17 19:14:29 +0000 | [diff] [blame^] | 36 | // emitAlignment - Emit an alignment directive to the specified power of two. |
| 37 | void AsmPrinter::emitAlignment(unsigned NumBits) const { |
| 38 | if (AlignmentIsInBytes) NumBits = 1 << NumBits; |
| 39 | O << AlignDirective << NumBits << "\n"; |
| 40 | } |
| 41 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 42 | // Print out the specified constant, without a storage class. Only the |
| 43 | // constants valid in constant expressions can occur here. |
| 44 | void AsmPrinter::emitConstantValueOnly(const Constant *CV) { |
| 45 | if (CV->isNullValue()) |
| 46 | O << "0"; |
| 47 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 48 | assert(CB == ConstantBool::True); |
| 49 | O << "1"; |
| 50 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 51 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 52 | O << CI->getValue(); |
| 53 | else |
| 54 | O << (unsigned long long)CI->getValue(); |
| 55 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 56 | O << CI->getValue(); |
| 57 | else if (isa<GlobalValue>((Value*)CV)) |
| 58 | // This is a constant address for a global variable or function. Use the |
| 59 | // name of the variable or function as the address value. |
| 60 | O << Mang->getValueName(CV); |
| 61 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 62 | const TargetData &TD = TM.getTargetData(); |
| 63 | switch(CE->getOpcode()) { |
| 64 | case Instruction::GetElementPtr: { |
| 65 | // generate a symbolic expression for the byte address |
| 66 | const Constant *ptrVal = CE->getOperand(0); |
| 67 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 68 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 69 | O << "("; |
| 70 | emitConstantValueOnly(ptrVal); |
| 71 | O << ") + " << Offset; |
| 72 | } else { |
| 73 | emitConstantValueOnly(ptrVal); |
| 74 | } |
| 75 | break; |
| 76 | } |
| 77 | case Instruction::Cast: { |
| 78 | // Support only non-converting or widening casts for now, that is, ones |
| 79 | // that do not involve a change in value. This assertion is really gross, |
| 80 | // and may not even be a complete check. |
| 81 | Constant *Op = CE->getOperand(0); |
| 82 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 83 | |
| 84 | // Remember, kids, pointers can be losslessly converted back and forth |
| 85 | // into 32-bit or wider integers, regardless of signedness. :-P |
| 86 | assert(((isa<PointerType>(OpTy) |
| 87 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 88 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 89 | || (isa<PointerType>(Ty) |
| 90 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 91 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 92 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 93 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 94 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 95 | O << "("; |
| 96 | emitConstantValueOnly(Op); |
| 97 | O << ")"; |
| 98 | break; |
| 99 | } |
| 100 | case Instruction::Add: |
| 101 | O << "("; |
| 102 | emitConstantValueOnly(CE->getOperand(0)); |
| 103 | O << ") + ("; |
| 104 | emitConstantValueOnly(CE->getOperand(1)); |
| 105 | O << ")"; |
| 106 | break; |
| 107 | default: |
| 108 | assert(0 && "Unsupported operator!"); |
| 109 | } |
| 110 | } else { |
| 111 | assert(0 && "Unknown constant value!"); |
| 112 | } |
| 113 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 114 | |
| 115 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 116 | /// |
| 117 | static inline char toOctal(int X) { |
| 118 | return (X&7)+'0'; |
| 119 | } |
| 120 | |
| 121 | /// getAsCString - Return the specified array as a C compatible string, only if |
| 122 | /// the predicate isString is true. |
| 123 | /// |
| 124 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 125 | assert(CVA->isString() && "Array is not string compatible!"); |
| 126 | |
| 127 | O << "\""; |
| 128 | for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { |
| 129 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 130 | |
| 131 | if (C == '"') { |
| 132 | O << "\\\""; |
| 133 | } else if (C == '\\') { |
| 134 | O << "\\\\"; |
| 135 | } else if (isprint(C)) { |
| 136 | O << C; |
| 137 | } else { |
| 138 | switch(C) { |
| 139 | case '\b': O << "\\b"; break; |
| 140 | case '\f': O << "\\f"; break; |
| 141 | case '\n': O << "\\n"; break; |
| 142 | case '\r': O << "\\r"; break; |
| 143 | case '\t': O << "\\t"; break; |
| 144 | default: |
| 145 | O << '\\'; |
| 146 | O << toOctal(C >> 6); |
| 147 | O << toOctal(C >> 3); |
| 148 | O << toOctal(C >> 0); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | O << "\""; |
| 154 | } |
| 155 | |
| 156 | /// emitGlobalConstant - Print a general LLVM constant to the .s file. |
| 157 | /// |
| 158 | void AsmPrinter::emitGlobalConstant(const Constant *CV) { |
| 159 | const TargetData &TD = TM.getTargetData(); |
| 160 | |
| 161 | if (CV->isNullValue()) { |
| 162 | O << ZeroDirective << TD.getTypeSize(CV->getType()) << "\n"; |
| 163 | return; |
| 164 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 165 | if (CVA->isString()) { |
| 166 | O << AsciiDirective; |
| 167 | printAsCString(O, CVA); |
| 168 | O << "\n"; |
| 169 | } else { // Not a string. Print the values in successive locations |
| 170 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
| 171 | emitGlobalConstant(CVA->getOperand(i)); |
| 172 | } |
| 173 | return; |
| 174 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 175 | // Print the fields in successive locations. Pad to align if needed! |
| 176 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
| 177 | unsigned sizeSoFar = 0; |
| 178 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 179 | const Constant* field = CVS->getOperand(i); |
| 180 | |
| 181 | // Check if padding is needed and insert one or more 0s. |
| 182 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
| 183 | unsigned padSize = ((i == e-1? cvsLayout->StructSize |
| 184 | : cvsLayout->MemberOffsets[i+1]) |
| 185 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 186 | sizeSoFar += fieldSize + padSize; |
| 187 | |
| 188 | // Now print the actual field value |
| 189 | emitGlobalConstant(field); |
| 190 | |
| 191 | // Insert the field padding unless it's zero bytes... |
| 192 | if (padSize) |
| 193 | O << ZeroDirective << padSize << "\n"; |
| 194 | } |
| 195 | assert(sizeSoFar == cvsLayout->StructSize && |
| 196 | "Layout of constant struct may be incorrect!"); |
| 197 | return; |
| 198 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 199 | // FP Constants are printed as integer constants to avoid losing |
| 200 | // precision... |
| 201 | double Val = CFP->getValue(); |
| 202 | if (CFP->getType() == Type::DoubleTy) { |
| 203 | union DU { // Abide by C TBAA rules |
| 204 | double FVal; |
| 205 | uint64_t UVal; |
| 206 | } U; |
| 207 | U.FVal = Val; |
| 208 | |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 209 | if (Data64bitsDirective) |
| 210 | O << Data64bitsDirective << U.UVal << "\n"; |
| 211 | else if (TD.isBigEndian()) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 212 | O << Data32bitsDirective << unsigned(U.UVal >> 32) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 213 | << "\t" << CommentChar << " double most significant word " |
| 214 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 215 | O << Data32bitsDirective << unsigned(U.UVal) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 216 | << "\t" << CommentChar << " double least significant word " |
| 217 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 218 | } else { |
| 219 | O << Data32bitsDirective << unsigned(U.UVal) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 220 | << "\t" << CommentChar << " double least significant word " << Val |
| 221 | << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 222 | O << Data32bitsDirective << unsigned(U.UVal >> 32) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 223 | << "\t" << CommentChar << " double most significant word " << Val |
| 224 | << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 225 | } |
| 226 | return; |
| 227 | } else { |
| 228 | union FU { // Abide by C TBAA rules |
| 229 | float FVal; |
| 230 | int32_t UVal; |
| 231 | } U; |
| 232 | U.FVal = Val; |
| 233 | |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 234 | O << Data32bitsDirective << U.UVal << "\t" << CommentChar |
| 235 | << " float " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) { |
| 239 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 240 | uint64_t Val = CI->getRawValue(); |
| 241 | |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 242 | if (Data64bitsDirective) |
| 243 | O << Data64bitsDirective << Val << "\n"; |
| 244 | else if (TD.isBigEndian()) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 245 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 246 | << "\t" << CommentChar << " Double-word most significant word " |
| 247 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 248 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 249 | << "\t" << CommentChar << " Double-word least significant word " |
| 250 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 251 | } else { |
| 252 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 253 | << "\t" << CommentChar << " Double-word least significant word " |
| 254 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 255 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 256 | << "\t" << CommentChar << " Double-word most significant word " |
| 257 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 258 | } |
| 259 | return; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | const Type *type = CV->getType(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 264 | switch (type->getTypeID()) { |
| 265 | case Type::UByteTyID: case Type::SByteTyID: |
| 266 | O << Data8bitsDirective; |
| 267 | break; |
| 268 | case Type::UShortTyID: case Type::ShortTyID: |
| 269 | O << Data16bitsDirective; |
| 270 | break; |
| 271 | case Type::BoolTyID: |
| 272 | case Type::PointerTyID: |
| 273 | case Type::UIntTyID: case Type::IntTyID: |
| 274 | O << Data32bitsDirective; |
| 275 | break; |
| 276 | case Type::ULongTyID: case Type::LongTyID: |
| 277 | assert (0 && "Should have already output double-word constant."); |
| 278 | case Type::FloatTyID: case Type::DoubleTyID: |
| 279 | assert (0 && "Should have already output floating point constant."); |
| 280 | default: |
| 281 | assert (0 && "Can't handle printing this type of thing"); |
| 282 | break; |
| 283 | } |
| 284 | emitConstantValueOnly(CV); |
| 285 | O << "\n"; |
| 286 | } |