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 | |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 14 | #include "llvm/DerivedTypes.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/AsmPrinter.h" |
| 16 | #include "llvm/Constants.h" |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Mangler.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
Duraid Madina | 2e096c1 | 2005-12-28 06:29:02 +0000 | [diff] [blame] | 22 | #include <iostream> |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 25 | AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm) |
| 26 | : FunctionNumber(0), O(o), TM(tm), |
| 27 | CommentString("#"), |
| 28 | GlobalPrefix(""), |
| 29 | PrivateGlobalPrefix("."), |
| 30 | GlobalVarAddrPrefix(""), |
| 31 | GlobalVarAddrSuffix(""), |
| 32 | FunctionAddrPrefix(""), |
| 33 | FunctionAddrSuffix(""), |
| 34 | ZeroDirective("\t.zero\t"), |
| 35 | AsciiDirective("\t.ascii\t"), |
| 36 | AscizDirective("\t.asciz\t"), |
| 37 | Data8bitsDirective("\t.byte\t"), |
| 38 | Data16bitsDirective("\t.short\t"), |
| 39 | Data32bitsDirective("\t.long\t"), |
| 40 | Data64bitsDirective("\t.quad\t"), |
| 41 | AlignDirective("\t.align\t"), |
| 42 | AlignmentIsInBytes(true), |
| 43 | SwitchToSectionDirective("\t.section\t"), |
| 44 | ConstantPoolSection("\t.section .rodata\n"), |
| 45 | StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"), |
| 46 | StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"), |
| 47 | LCOMMDirective(0), |
| 48 | COMMDirective("\t.comm\t"), |
| 49 | COMMDirectiveTakesAlignment(true), |
| 50 | HasDotTypeDotSizeDirective(true) { |
| 51 | } |
| 52 | |
| 53 | |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 54 | /// SwitchSection - Switch to the specified section of the executable if we |
| 55 | /// are not already in it! |
| 56 | /// |
| 57 | void AsmPrinter::SwitchSection(const char *NewSection, const GlobalValue *GV) { |
| 58 | std::string NS; |
| 59 | |
| 60 | if (GV && GV->hasSection()) |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 61 | NS = SwitchToSectionDirective + GV->getSection(); |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 62 | else |
Chris Lattner | 42a80fe | 2005-12-09 19:28:49 +0000 | [diff] [blame] | 63 | NS = std::string("\t")+NewSection; |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 64 | |
| 65 | if (CurrentSection != NS) { |
| 66 | CurrentSection = NS; |
| 67 | if (!CurrentSection.empty()) |
Chris Lattner | 42a80fe | 2005-12-09 19:28:49 +0000 | [diff] [blame] | 68 | O << CurrentSection << '\n'; |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 72 | bool AsmPrinter::doInitialization(Module &M) { |
Chris Lattner | af2bf0a | 2004-08-17 06:06:19 +0000 | [diff] [blame] | 73 | Mang = new Mangler(M, GlobalPrefix); |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame^] | 74 | |
| 75 | if (!M.getInlineAsm().empty()) |
| 76 | O << CommentString << " Start File Scope Asm Blocks:\n" << M.getInlineAsm() |
| 77 | << "\n" << CommentString << " End File Scope Asm Blocks\n"; |
| 78 | |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 79 | SwitchSection("", 0); // Reset back to no section. |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | bool AsmPrinter::doFinalization(Module &M) { |
| 84 | delete Mang; Mang = 0; |
| 85 | return false; |
| 86 | } |
| 87 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 88 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 89 | // What's my mangled name? |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 90 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
Chris Lattner | 77bc228 | 2005-11-21 08:13:27 +0000 | [diff] [blame] | 91 | IncrementFunctionNumber(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 94 | /// EmitConstantPool - Print to the current output stream assembly |
| 95 | /// representations of the constants in the constant pool MCP. This is |
| 96 | /// used to print out constants which have been "spilled to memory" by |
| 97 | /// the code generator. |
| 98 | /// |
| 99 | void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { |
| 100 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 101 | if (CP.empty()) return; |
| 102 | const TargetData &TD = TM.getTargetData(); |
| 103 | |
| 104 | SwitchSection(ConstantPoolSection, 0); |
| 105 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 106 | // FIXME: force doubles to be naturally aligned. We should handle this |
| 107 | // more correctly in the future. |
| 108 | unsigned Alignment = TD.getTypeAlignmentShift(CP[i]->getType()); |
| 109 | if (CP[i]->getType() == Type::DoubleTy && Alignment < 3) Alignment = 3; |
| 110 | |
| 111 | EmitAlignment(Alignment); |
| 112 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i |
| 113 | << ":\t\t\t\t\t" << CommentString << *CP[i] << '\n'; |
| 114 | EmitGlobalConstant(CP[i]); |
| 115 | } |
| 116 | } |
| 117 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 118 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a |
| 119 | /// special global used by LLVM. If so, emit it and return true, otherwise |
| 120 | /// do nothing and return false. |
| 121 | bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { |
| 122 | assert(GV->hasInitializer() && GV->hasAppendingLinkage() && |
| 123 | "Not a special LLVM global!"); |
| 124 | |
| 125 | if (GV->getName() == "llvm.used") |
| 126 | return true; // No need to emit this at all. |
| 127 | |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 128 | if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) { |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 129 | SwitchSection(StaticCtorsSection, 0); |
| 130 | EmitAlignment(2, 0); |
| 131 | EmitXXStructorList(GV->getInitializer()); |
| 132 | return true; |
| 133 | } |
| 134 | |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 135 | if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) { |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 136 | SwitchSection(StaticDtorsSection, 0); |
| 137 | EmitAlignment(2, 0); |
| 138 | EmitXXStructorList(GV->getInitializer()); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the |
| 146 | /// function pointers, ignoring the init priority. |
| 147 | void AsmPrinter::EmitXXStructorList(Constant *List) { |
| 148 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 149 | // init priority, which we ignore. |
| 150 | if (!isa<ConstantArray>(List)) return; |
| 151 | ConstantArray *InitList = cast<ConstantArray>(List); |
| 152 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 153 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 154 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
Chris Lattner | 8de324b | 2005-12-21 01:17:37 +0000 | [diff] [blame] | 155 | |
| 156 | if (CS->getOperand(1)->isNullValue()) |
| 157 | return; // Found a null terminator, exit printing. |
| 158 | // Emit the function pointer. |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 159 | EmitGlobalConstant(CS->getOperand(1)); |
| 160 | } |
| 161 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 162 | |
| 163 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 164 | // EmitAlignment - Emit an alignment directive to the specified power of two. |
| 165 | void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const { |
Chris Lattner | a1ab72d | 2005-11-14 19:00:06 +0000 | [diff] [blame] | 166 | if (GV && GV->getAlignment()) |
| 167 | NumBits = Log2_32(GV->getAlignment()); |
Chris Lattner | 2a21c6e | 2005-11-10 18:09:27 +0000 | [diff] [blame] | 168 | if (NumBits == 0) return; // No need to emit alignment. |
Chris Lattner | bfddc20 | 2004-08-17 19:14:29 +0000 | [diff] [blame] | 169 | if (AlignmentIsInBytes) NumBits = 1 << NumBits; |
| 170 | O << AlignDirective << NumBits << "\n"; |
| 171 | } |
| 172 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 173 | /// EmitZeros - Emit a block of zeros. |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 174 | /// |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 175 | void AsmPrinter::EmitZeros(uint64_t NumZeros) const { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 176 | if (NumZeros) { |
| 177 | if (ZeroDirective) |
| 178 | O << ZeroDirective << NumZeros << "\n"; |
| 179 | else { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 180 | for (; NumZeros; --NumZeros) |
| 181 | O << Data8bitsDirective << "0\n"; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 186 | // Print out the specified constant, without a storage class. Only the |
| 187 | // constants valid in constant expressions can occur here. |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 188 | void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 189 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 190 | O << "0"; |
| 191 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 192 | assert(CB == ConstantBool::True); |
| 193 | O << "1"; |
| 194 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 195 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 196 | O << CI->getValue(); |
| 197 | else |
Duraid Madina | 4560657 | 2005-05-15 13:05:48 +0000 | [diff] [blame] | 198 | O << (uint64_t)CI->getValue(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 199 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 200 | O << CI->getValue(); |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 201 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 202 | // This is a constant address for a global variable or function. Use the |
| 203 | // name of the variable or function as the address value, possibly |
| 204 | // decorating it with GlobalVarAddrPrefix/Suffix or |
| 205 | // FunctionAddrPrefix/Suffix (these all default to "" ) |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 206 | if (isa<Function>(GV)) |
| 207 | O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix; |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 208 | else |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 209 | O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix; |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 210 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 211 | const TargetData &TD = TM.getTargetData(); |
| 212 | switch(CE->getOpcode()) { |
| 213 | case Instruction::GetElementPtr: { |
| 214 | // generate a symbolic expression for the byte address |
| 215 | const Constant *ptrVal = CE->getOperand(0); |
| 216 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 217 | if (int64_t Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 218 | if (Offset) |
| 219 | O << "("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 220 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 221 | if (Offset > 0) |
| 222 | O << ") + " << Offset; |
| 223 | else if (Offset < 0) |
| 224 | O << ") - " << -Offset; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 225 | } else { |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 226 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 227 | } |
| 228 | break; |
| 229 | } |
| 230 | case Instruction::Cast: { |
| 231 | // Support only non-converting or widening casts for now, that is, ones |
| 232 | // that do not involve a change in value. This assertion is really gross, |
| 233 | // and may not even be a complete check. |
| 234 | Constant *Op = CE->getOperand(0); |
| 235 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 236 | |
| 237 | // Remember, kids, pointers can be losslessly converted back and forth |
| 238 | // into 32-bit or wider integers, regardless of signedness. :-P |
| 239 | assert(((isa<PointerType>(OpTy) |
| 240 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 241 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 242 | || (isa<PointerType>(Ty) |
| 243 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 244 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 245 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 246 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 247 | && "FIXME: Don't yet support this kind of constant cast expr"); |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 248 | EmitConstantValueOnly(Op); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | case Instruction::Add: |
| 252 | O << "("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 253 | EmitConstantValueOnly(CE->getOperand(0)); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 254 | O << ") + ("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 255 | EmitConstantValueOnly(CE->getOperand(1)); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 256 | O << ")"; |
| 257 | break; |
| 258 | default: |
| 259 | assert(0 && "Unsupported operator!"); |
| 260 | } |
| 261 | } else { |
| 262 | assert(0 && "Unknown constant value!"); |
| 263 | } |
| 264 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 265 | |
| 266 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 267 | /// |
| 268 | static inline char toOctal(int X) { |
| 269 | return (X&7)+'0'; |
| 270 | } |
| 271 | |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 272 | /// printAsCString - Print the specified array as a C compatible string, only if |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 273 | /// the predicate isString is true. |
| 274 | /// |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 275 | static void printAsCString(std::ostream &O, const ConstantArray *CVA, |
| 276 | unsigned LastElt) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 277 | assert(CVA->isString() && "Array is not string compatible!"); |
| 278 | |
| 279 | O << "\""; |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 280 | for (unsigned i = 0; i != LastElt; ++i) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 281 | unsigned char C = |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 282 | (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 283 | |
| 284 | if (C == '"') { |
| 285 | O << "\\\""; |
| 286 | } else if (C == '\\') { |
| 287 | O << "\\\\"; |
| 288 | } else if (isprint(C)) { |
| 289 | O << C; |
| 290 | } else { |
| 291 | switch(C) { |
| 292 | case '\b': O << "\\b"; break; |
| 293 | case '\f': O << "\\f"; break; |
| 294 | case '\n': O << "\\n"; break; |
| 295 | case '\r': O << "\\r"; break; |
| 296 | case '\t': O << "\\t"; break; |
| 297 | default: |
| 298 | O << '\\'; |
| 299 | O << toOctal(C >> 6); |
| 300 | O << toOctal(C >> 3); |
| 301 | O << toOctal(C >> 0); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | O << "\""; |
| 307 | } |
| 308 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 309 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 310 | /// |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 311 | void AsmPrinter::EmitGlobalConstant(const Constant *CV) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 312 | const TargetData &TD = TM.getTargetData(); |
| 313 | |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 314 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 315 | EmitZeros(TD.getTypeSize(CV->getType())); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 316 | return; |
| 317 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 318 | if (CVA->isString()) { |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 319 | unsigned NumElts = CVA->getNumOperands(); |
| 320 | if (AscizDirective && NumElts && |
| 321 | cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) { |
| 322 | O << AscizDirective; |
| 323 | printAsCString(O, CVA, NumElts-1); |
| 324 | } else { |
| 325 | O << AsciiDirective; |
| 326 | printAsCString(O, CVA, NumElts); |
| 327 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 328 | O << "\n"; |
| 329 | } else { // Not a string. Print the values in successive locations |
| 330 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 331 | EmitGlobalConstant(CVA->getOperand(i)); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 332 | } |
| 333 | return; |
| 334 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 335 | // Print the fields in successive locations. Pad to align if needed! |
| 336 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 337 | uint64_t sizeSoFar = 0; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 338 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 339 | const Constant* field = CVS->getOperand(i); |
| 340 | |
| 341 | // Check if padding is needed and insert one or more 0s. |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 342 | uint64_t fieldSize = TD.getTypeSize(field->getType()); |
| 343 | uint64_t padSize = ((i == e-1? cvsLayout->StructSize |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 344 | : cvsLayout->MemberOffsets[i+1]) |
| 345 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 346 | sizeSoFar += fieldSize + padSize; |
| 347 | |
| 348 | // Now print the actual field value |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 349 | EmitGlobalConstant(field); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 350 | |
| 351 | // Insert the field padding unless it's zero bytes... |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 352 | EmitZeros(padSize); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 353 | } |
| 354 | assert(sizeSoFar == cvsLayout->StructSize && |
| 355 | "Layout of constant struct may be incorrect!"); |
| 356 | return; |
| 357 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 358 | // FP Constants are printed as integer constants to avoid losing |
| 359 | // precision... |
| 360 | double Val = CFP->getValue(); |
| 361 | if (CFP->getType() == Type::DoubleTy) { |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 362 | if (Data64bitsDirective) |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 363 | O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 364 | << " double value: " << Val << "\n"; |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 365 | else if (TD.isBigEndian()) { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 366 | O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 367 | << "\t" << CommentString << " double most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 368 | << Val << "\n"; |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 369 | O << Data32bitsDirective << unsigned(DoubleToBits(Val)) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 370 | << "\t" << CommentString << " double least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 371 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 372 | } else { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 373 | O << Data32bitsDirective << unsigned(DoubleToBits(Val)) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 374 | << "\t" << CommentString << " double least significant word " << Val |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 375 | << "\n"; |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 376 | O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 377 | << "\t" << CommentString << " double most significant word " << Val |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 378 | << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 379 | } |
| 380 | return; |
| 381 | } else { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 382 | O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 383 | << " float " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 384 | return; |
| 385 | } |
| 386 | } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) { |
| 387 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 388 | uint64_t Val = CI->getRawValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 389 | |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 390 | if (Data64bitsDirective) |
| 391 | O << Data64bitsDirective << Val << "\n"; |
| 392 | else if (TD.isBigEndian()) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 393 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 394 | << "\t" << CommentString << " Double-word most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 395 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 396 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 397 | << "\t" << CommentString << " Double-word least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 398 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 399 | } else { |
| 400 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 401 | << "\t" << CommentString << " Double-word least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 402 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 403 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 404 | << "\t" << CommentString << " Double-word most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 405 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 406 | } |
| 407 | return; |
| 408 | } |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 409 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) { |
| 410 | const PackedType *PTy = CP->getType(); |
| 411 | |
| 412 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
| 413 | EmitGlobalConstant(CP->getOperand(I)); |
| 414 | |
| 415 | return; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | const Type *type = CV->getType(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 419 | switch (type->getTypeID()) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 420 | case Type::BoolTyID: |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 421 | case Type::UByteTyID: case Type::SByteTyID: |
| 422 | O << Data8bitsDirective; |
| 423 | break; |
| 424 | case Type::UShortTyID: case Type::ShortTyID: |
| 425 | O << Data16bitsDirective; |
| 426 | break; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 427 | case Type::PointerTyID: |
Andrew Lenharth | 571c9c3 | 2005-02-04 13:47:16 +0000 | [diff] [blame] | 428 | if (TD.getPointerSize() == 8) { |
| 429 | O << Data64bitsDirective; |
| 430 | break; |
| 431 | } |
| 432 | //Fall through for pointer size == int size |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 433 | case Type::UIntTyID: case Type::IntTyID: |
| 434 | O << Data32bitsDirective; |
| 435 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 436 | case Type::ULongTyID: case Type::LongTyID: |
Chris Lattner | 660538c | 2005-08-08 04:26:32 +0000 | [diff] [blame] | 437 | assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!"); |
| 438 | O << Data64bitsDirective; |
| 439 | break; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 440 | case Type::FloatTyID: case Type::DoubleTyID: |
| 441 | assert (0 && "Should have already output floating point constant."); |
| 442 | default: |
| 443 | assert (0 && "Can't handle printing this type of thing"); |
| 444 | break; |
| 445 | } |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 446 | EmitConstantValueOnly(CV); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 447 | O << "\n"; |
| 448 | } |