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 | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 42 | /// emitZeros - Emit a block of zeros. |
| 43 | /// |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 44 | void AsmPrinter::emitZeros(uint64_t NumZeros) const { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 45 | if (NumZeros) { |
| 46 | if (ZeroDirective) |
| 47 | O << ZeroDirective << NumZeros << "\n"; |
| 48 | else { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 49 | for (; NumZeros; --NumZeros) |
| 50 | O << Data8bitsDirective << "0\n"; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 55 | // Print out the specified constant, without a storage class. Only the |
| 56 | // constants valid in constant expressions can occur here. |
| 57 | void AsmPrinter::emitConstantValueOnly(const Constant *CV) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 58 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 59 | O << "0"; |
| 60 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 61 | assert(CB == ConstantBool::True); |
| 62 | O << "1"; |
| 63 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 64 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 65 | O << CI->getValue(); |
| 66 | else |
| 67 | O << (unsigned long long)CI->getValue(); |
| 68 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 69 | O << CI->getValue(); |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 70 | else if (isa<GlobalValue>((Value*)CV)) { |
| 71 | // This is a constant address for a global variable or function. Use the |
| 72 | // name of the variable or function as the address value, possibly |
| 73 | // decorating it with GlobalVarAddrPrefix/Suffix or |
| 74 | // FunctionAddrPrefix/Suffix (these all default to "" ) |
| 75 | if (isa<Function>((Value*)CV)) |
| 76 | O << FunctionAddrPrefix << Mang->getValueName(CV) << FunctionAddrSuffix; |
| 77 | else |
| 78 | O << GlobalVarAddrPrefix << Mang->getValueName(CV) << GlobalVarAddrSuffix; |
| 79 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 80 | const TargetData &TD = TM.getTargetData(); |
| 81 | switch(CE->getOpcode()) { |
| 82 | case Instruction::GetElementPtr: { |
| 83 | // generate a symbolic expression for the byte address |
| 84 | const Constant *ptrVal = CE->getOperand(0); |
| 85 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 86 | if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 87 | if (Offset) |
| 88 | O << "("; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 89 | emitConstantValueOnly(ptrVal); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 90 | if (Offset > 0) |
| 91 | O << ") + " << Offset; |
| 92 | else if (Offset < 0) |
| 93 | O << ") - " << -Offset; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 94 | } else { |
| 95 | emitConstantValueOnly(ptrVal); |
| 96 | } |
| 97 | break; |
| 98 | } |
| 99 | case Instruction::Cast: { |
| 100 | // Support only non-converting or widening casts for now, that is, ones |
| 101 | // that do not involve a change in value. This assertion is really gross, |
| 102 | // and may not even be a complete check. |
| 103 | Constant *Op = CE->getOperand(0); |
| 104 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 105 | |
| 106 | // Remember, kids, pointers can be losslessly converted back and forth |
| 107 | // into 32-bit or wider integers, regardless of signedness. :-P |
| 108 | assert(((isa<PointerType>(OpTy) |
| 109 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 110 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 111 | || (isa<PointerType>(Ty) |
| 112 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 113 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 114 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 115 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 116 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 117 | O << "("; |
| 118 | emitConstantValueOnly(Op); |
| 119 | O << ")"; |
| 120 | break; |
| 121 | } |
| 122 | case Instruction::Add: |
| 123 | O << "("; |
| 124 | emitConstantValueOnly(CE->getOperand(0)); |
| 125 | O << ") + ("; |
| 126 | emitConstantValueOnly(CE->getOperand(1)); |
| 127 | O << ")"; |
| 128 | break; |
| 129 | default: |
| 130 | assert(0 && "Unsupported operator!"); |
| 131 | } |
| 132 | } else { |
| 133 | assert(0 && "Unknown constant value!"); |
| 134 | } |
| 135 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 136 | |
| 137 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 138 | /// |
| 139 | static inline char toOctal(int X) { |
| 140 | return (X&7)+'0'; |
| 141 | } |
| 142 | |
| 143 | /// getAsCString - Return the specified array as a C compatible string, only if |
| 144 | /// the predicate isString is true. |
| 145 | /// |
| 146 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 147 | assert(CVA->isString() && "Array is not string compatible!"); |
| 148 | |
| 149 | O << "\""; |
| 150 | for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame^] | 151 | unsigned char C = |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 152 | (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 153 | |
| 154 | if (C == '"') { |
| 155 | O << "\\\""; |
| 156 | } else if (C == '\\') { |
| 157 | O << "\\\\"; |
| 158 | } else if (isprint(C)) { |
| 159 | O << C; |
| 160 | } else { |
| 161 | switch(C) { |
| 162 | case '\b': O << "\\b"; break; |
| 163 | case '\f': O << "\\f"; break; |
| 164 | case '\n': O << "\\n"; break; |
| 165 | case '\r': O << "\\r"; break; |
| 166 | case '\t': O << "\\t"; break; |
| 167 | default: |
| 168 | O << '\\'; |
| 169 | O << toOctal(C >> 6); |
| 170 | O << toOctal(C >> 3); |
| 171 | O << toOctal(C >> 0); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | O << "\""; |
| 177 | } |
| 178 | |
| 179 | /// emitGlobalConstant - Print a general LLVM constant to the .s file. |
| 180 | /// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame^] | 181 | void AsmPrinter::emitGlobalConstant(const Constant *CV) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 182 | const TargetData &TD = TM.getTargetData(); |
| 183 | |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 184 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 185 | emitZeros(TD.getTypeSize(CV->getType())); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 186 | return; |
| 187 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 188 | if (CVA->isString()) { |
| 189 | O << AsciiDirective; |
| 190 | printAsCString(O, CVA); |
| 191 | O << "\n"; |
| 192 | } else { // Not a string. Print the values in successive locations |
| 193 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
| 194 | emitGlobalConstant(CVA->getOperand(i)); |
| 195 | } |
| 196 | return; |
| 197 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 198 | // Print the fields in successive locations. Pad to align if needed! |
| 199 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 200 | uint64_t sizeSoFar = 0; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 201 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 202 | const Constant* field = CVS->getOperand(i); |
| 203 | |
| 204 | // Check if padding is needed and insert one or more 0s. |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 205 | uint64_t fieldSize = TD.getTypeSize(field->getType()); |
| 206 | uint64_t padSize = ((i == e-1? cvsLayout->StructSize |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 207 | : cvsLayout->MemberOffsets[i+1]) |
| 208 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 209 | sizeSoFar += fieldSize + padSize; |
| 210 | |
| 211 | // Now print the actual field value |
| 212 | emitGlobalConstant(field); |
| 213 | |
| 214 | // Insert the field padding unless it's zero bytes... |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 215 | emitZeros(padSize); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 216 | } |
| 217 | assert(sizeSoFar == cvsLayout->StructSize && |
| 218 | "Layout of constant struct may be incorrect!"); |
| 219 | return; |
| 220 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 221 | // FP Constants are printed as integer constants to avoid losing |
| 222 | // precision... |
| 223 | double Val = CFP->getValue(); |
| 224 | if (CFP->getType() == Type::DoubleTy) { |
| 225 | union DU { // Abide by C TBAA rules |
| 226 | double FVal; |
| 227 | uint64_t UVal; |
| 228 | } U; |
| 229 | U.FVal = Val; |
| 230 | |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 231 | if (Data64bitsDirective) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 232 | O << Data64bitsDirective << U.UVal << "\t" << CommentString |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 233 | << " double value: " << Val << "\n"; |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 234 | else if (TD.isBigEndian()) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 235 | O << Data32bitsDirective << unsigned(U.UVal >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 236 | << "\t" << CommentString << " double most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 237 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 238 | O << Data32bitsDirective << unsigned(U.UVal) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 239 | << "\t" << CommentString << " double least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 240 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 241 | } else { |
| 242 | O << Data32bitsDirective << unsigned(U.UVal) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 243 | << "\t" << CommentString << " double least significant word " << Val |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 244 | << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 245 | O << Data32bitsDirective << unsigned(U.UVal >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 246 | << "\t" << CommentString << " double most significant word " << Val |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 247 | << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 248 | } |
| 249 | return; |
| 250 | } else { |
| 251 | union FU { // Abide by C TBAA rules |
| 252 | float FVal; |
| 253 | int32_t UVal; |
| 254 | } U; |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 255 | U.FVal = (float)Val; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame^] | 256 | |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 257 | O << Data32bitsDirective << U.UVal << "\t" << CommentString |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 258 | << " float " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) { |
| 262 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 263 | uint64_t Val = CI->getRawValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame^] | 264 | |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 265 | if (Data64bitsDirective) |
| 266 | O << Data64bitsDirective << Val << "\n"; |
| 267 | else if (TD.isBigEndian()) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 268 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 269 | << "\t" << CommentString << " Double-word most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 270 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 271 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 272 | << "\t" << CommentString << " Double-word least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 273 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 274 | } else { |
| 275 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 276 | << "\t" << CommentString << " Double-word least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 277 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 278 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 279 | << "\t" << CommentString << " Double-word most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 280 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 281 | } |
| 282 | return; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | const Type *type = CV->getType(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 287 | switch (type->getTypeID()) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame^] | 288 | case Type::BoolTyID: |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 289 | case Type::UByteTyID: case Type::SByteTyID: |
| 290 | O << Data8bitsDirective; |
| 291 | break; |
| 292 | case Type::UShortTyID: case Type::ShortTyID: |
| 293 | O << Data16bitsDirective; |
| 294 | break; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 295 | case Type::PointerTyID: |
Andrew Lenharth | 571c9c3 | 2005-02-04 13:47:16 +0000 | [diff] [blame] | 296 | if (TD.getPointerSize() == 8) { |
| 297 | O << Data64bitsDirective; |
| 298 | break; |
| 299 | } |
| 300 | //Fall through for pointer size == int size |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 301 | case Type::UIntTyID: case Type::IntTyID: |
| 302 | O << Data32bitsDirective; |
| 303 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame^] | 304 | case Type::ULongTyID: case Type::LongTyID: |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 305 | assert (0 && "Should have already output double-word constant."); |
| 306 | case Type::FloatTyID: case Type::DoubleTyID: |
| 307 | assert (0 && "Should have already output floating point constant."); |
| 308 | default: |
| 309 | assert (0 && "Can't handle printing this type of thing"); |
| 310 | break; |
| 311 | } |
| 312 | emitConstantValueOnly(CV); |
| 313 | O << "\n"; |
| 314 | } |