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 | |
Evan Cheng | 932f022 | 2006-03-03 02:04:29 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/AsmPrinter.h" |
Evan Cheng | 246ae0d | 2006-03-01 22:18:09 +0000 | [diff] [blame] | 15 | #include "llvm/Assembly/Writer.h" |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Mangler.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" |
Duraid Madina | 2e096c1 | 2005-12-28 06:29:02 +0000 | [diff] [blame] | 25 | #include <iostream> |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 26 | #include <cerrno> |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 29 | AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm) |
| 30 | : FunctionNumber(0), O(o), TM(tm), |
| 31 | CommentString("#"), |
| 32 | GlobalPrefix(""), |
| 33 | PrivateGlobalPrefix("."), |
| 34 | GlobalVarAddrPrefix(""), |
| 35 | GlobalVarAddrSuffix(""), |
| 36 | FunctionAddrPrefix(""), |
| 37 | FunctionAddrSuffix(""), |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 38 | InlineAsmStart("#APP"), |
| 39 | InlineAsmEnd("#NO_APP"), |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 40 | ZeroDirective("\t.zero\t"), |
Jeff Cohen | c6a057b | 2006-05-02 03:46:13 +0000 | [diff] [blame] | 41 | ZeroDirectiveSuffix(0), |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 42 | AsciiDirective("\t.ascii\t"), |
| 43 | AscizDirective("\t.asciz\t"), |
| 44 | Data8bitsDirective("\t.byte\t"), |
| 45 | Data16bitsDirective("\t.short\t"), |
| 46 | Data32bitsDirective("\t.long\t"), |
| 47 | Data64bitsDirective("\t.quad\t"), |
| 48 | AlignDirective("\t.align\t"), |
| 49 | AlignmentIsInBytes(true), |
| 50 | SwitchToSectionDirective("\t.section\t"), |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 51 | TextSectionStartSuffix(""), |
| 52 | DataSectionStartSuffix(""), |
| 53 | SectionEndDirectiveSuffix(0), |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 54 | ConstantPoolSection("\t.section .rodata\n"), |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 55 | JumpTableSection("\t.section .rodata\n"), |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 56 | StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"), |
| 57 | StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"), |
| 58 | LCOMMDirective(0), |
| 59 | COMMDirective("\t.comm\t"), |
| 60 | COMMDirectiveTakesAlignment(true), |
| 61 | HasDotTypeDotSizeDirective(true) { |
| 62 | } |
| 63 | |
| 64 | |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 65 | /// SwitchToTextSection - Switch to the specified text section of the executable |
| 66 | /// if we are not already in it! |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 67 | /// |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 68 | void AsmPrinter::SwitchToTextSection(const char *NewSection, |
| 69 | const GlobalValue *GV) { |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 70 | std::string NS; |
Chris Lattner | a7090ae | 2006-05-09 05:24:50 +0000 | [diff] [blame] | 71 | if (GV && GV->hasSection()) |
Chris Lattner | c8d37c6 | 2006-05-09 16:41:59 +0000 | [diff] [blame] | 72 | NS = SwitchToSectionDirective + GV->getSection(); |
Chris Lattner | a7090ae | 2006-05-09 05:24:50 +0000 | [diff] [blame] | 73 | else |
| 74 | NS = NewSection; |
| 75 | |
| 76 | // If we're already in this section, we're done. |
| 77 | if (CurrentSection == NS) return; |
Jeff Cohen | 51b776d | 2006-05-02 03:58:45 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 79 | // Close the current section, if applicable. |
| 80 | if (SectionEndDirectiveSuffix && !CurrentSection.empty()) |
| 81 | O << CurrentSection << SectionEndDirectiveSuffix << "\n"; |
Jeff Cohen | 51b776d | 2006-05-02 03:58:45 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 83 | CurrentSection = NS; |
| 84 | |
| 85 | if (!CurrentSection.empty()) |
| 86 | O << CurrentSection << TextSectionStartSuffix << '\n'; |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 89 | /// SwitchToTextSection - Switch to the specified text section of the executable |
| 90 | /// if we are not already in it! |
| 91 | /// |
| 92 | void AsmPrinter::SwitchToDataSection(const char *NewSection, |
| 93 | const GlobalValue *GV) { |
| 94 | std::string NS; |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 95 | if (GV && GV->hasSection()) |
| 96 | NS = SwitchToSectionDirective + GV->getSection(); |
| 97 | else |
| 98 | NS = NewSection; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 99 | |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 100 | // If we're already in this section, we're done. |
| 101 | if (CurrentSection == NS) return; |
| 102 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 103 | // Close the current section, if applicable. |
| 104 | if (SectionEndDirectiveSuffix && !CurrentSection.empty()) |
| 105 | O << CurrentSection << SectionEndDirectiveSuffix << "\n"; |
| 106 | |
| 107 | CurrentSection = NS; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 109 | if (!CurrentSection.empty()) |
| 110 | O << CurrentSection << DataSectionStartSuffix << '\n'; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 114 | bool AsmPrinter::doInitialization(Module &M) { |
Chris Lattner | af2bf0a | 2004-08-17 06:06:19 +0000 | [diff] [blame] | 115 | Mang = new Mangler(M, GlobalPrefix); |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 117 | if (!M.getModuleInlineAsm().empty()) |
| 118 | O << CommentString << " Start of file scope inline assembly\n" |
| 119 | << M.getModuleInlineAsm() |
| 120 | << "\n" << CommentString << " End of file scope inline assembly\n"; |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 122 | SwitchToDataSection("", 0); // Reset back to no section. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 123 | |
| 124 | if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) { |
| 125 | DebugInfo->AnalyzeModule(M); |
| 126 | } |
| 127 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
| 131 | bool AsmPrinter::doFinalization(Module &M) { |
| 132 | delete Mang; Mang = 0; |
| 133 | return false; |
| 134 | } |
| 135 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 136 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 137 | // What's my mangled name? |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 138 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
Chris Lattner | 77bc228 | 2005-11-21 08:13:27 +0000 | [diff] [blame] | 139 | IncrementFunctionNumber(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 142 | /// EmitConstantPool - Print to the current output stream assembly |
| 143 | /// representations of the constants in the constant pool MCP. This is |
| 144 | /// used to print out constants which have been "spilled to memory" by |
| 145 | /// the code generator. |
| 146 | /// |
| 147 | void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 148 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 149 | if (CP.empty()) return; |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 151 | SwitchToDataSection(ConstantPoolSection, 0); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 152 | EmitAlignment(MCP->getConstantPoolAlignment()); |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 153 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 154 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i |
Evan Cheng | 246ae0d | 2006-03-01 22:18:09 +0000 | [diff] [blame] | 155 | << ":\t\t\t\t\t" << CommentString << " "; |
| 156 | WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n'; |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 157 | EmitGlobalConstant(CP[i].Val); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 158 | if (i != e-1) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 159 | unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType()); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 160 | unsigned ValEnd = CP[i].Offset + EntSize; |
| 161 | // Emit inter-object padding for alignment. |
| 162 | EmitZeros(CP[i+1].Offset-ValEnd); |
| 163 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 167 | /// EmitJumpTableInfo - Print assembly representations of the jump tables used |
| 168 | /// by the current function to the current output stream. |
| 169 | /// |
| 170 | void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) { |
| 171 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 172 | if (JT.empty()) return; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 173 | const TargetData *TD = TM.getTargetData(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 174 | |
| 175 | // FIXME: someday we need to handle PIC jump tables |
| 176 | assert((TM.getRelocationModel() == Reloc::Static || |
| 177 | TM.getRelocationModel() == Reloc::DynamicNoPIC) && |
| 178 | "Unhandled relocation model emitting jump table information!"); |
| 179 | |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 180 | SwitchToDataSection(JumpTableSection, 0); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 181 | EmitAlignment(Log2_32(TD->getPointerAlignment())); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 182 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 183 | O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i |
| 184 | << ":\n"; |
| 185 | const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; |
| 186 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { |
| 187 | O << Data32bitsDirective << ' '; |
| 188 | printBasicBlockLabel(JTBBs[ii]); |
| 189 | O << '\n'; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 194 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a |
| 195 | /// special global used by LLVM. If so, emit it and return true, otherwise |
| 196 | /// do nothing and return false. |
| 197 | bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { |
Jim Laskey | 7809811 | 2006-03-07 22:00:35 +0000 | [diff] [blame] | 198 | // Ignore debug and non-emitted data. |
| 199 | if (GV->getSection() == "llvm.metadata") return true; |
| 200 | |
| 201 | if (!GV->hasAppendingLinkage()) return false; |
| 202 | |
| 203 | assert(GV->hasInitializer() && "Not a special LLVM global!"); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 204 | |
| 205 | if (GV->getName() == "llvm.used") |
| 206 | return true; // No need to emit this at all. |
| 207 | |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 208 | if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) { |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 209 | SwitchToDataSection(StaticCtorsSection, 0); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 210 | EmitAlignment(2, 0); |
| 211 | EmitXXStructorList(GV->getInitializer()); |
| 212 | return true; |
| 213 | } |
| 214 | |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 215 | if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) { |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 216 | SwitchToDataSection(StaticDtorsSection, 0); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 217 | EmitAlignment(2, 0); |
| 218 | EmitXXStructorList(GV->getInitializer()); |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the |
| 226 | /// function pointers, ignoring the init priority. |
| 227 | void AsmPrinter::EmitXXStructorList(Constant *List) { |
| 228 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 229 | // init priority, which we ignore. |
| 230 | if (!isa<ConstantArray>(List)) return; |
| 231 | ConstantArray *InitList = cast<ConstantArray>(List); |
| 232 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 233 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 234 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
Chris Lattner | 8de324b | 2005-12-21 01:17:37 +0000 | [diff] [blame] | 235 | |
| 236 | if (CS->getOperand(1)->isNullValue()) |
| 237 | return; // Found a null terminator, exit printing. |
| 238 | // Emit the function pointer. |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 239 | EmitGlobalConstant(CS->getOperand(1)); |
| 240 | } |
| 241 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 4d57e0c | 2006-02-05 01:29:18 +0000 | [diff] [blame] | 243 | /// getPreferredAlignmentLog - Return the preferred alignment of the |
| 244 | /// specified global, returned in log form. This includes an explicitly |
| 245 | /// requested alignment (if the global has one). |
| 246 | unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 247 | unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType()); |
Chris Lattner | 4d57e0c | 2006-02-05 01:29:18 +0000 | [diff] [blame] | 248 | if (GV->getAlignment() > (1U << Alignment)) |
| 249 | Alignment = Log2_32(GV->getAlignment()); |
| 250 | |
Chris Lattner | 519ea2a | 2006-02-05 01:46:49 +0000 | [diff] [blame] | 251 | if (GV->hasInitializer()) { |
| 252 | // Always round up alignment of global doubles to 8 bytes. |
| 253 | if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3) |
| 254 | Alignment = 3; |
| 255 | if (Alignment < 4) { |
| 256 | // If the global is not external, see if it is large. If so, give it a |
| 257 | // larger alignment. |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 258 | if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128) |
Chris Lattner | 519ea2a | 2006-02-05 01:46:49 +0000 | [diff] [blame] | 259 | Alignment = 4; // 16-byte alignment. |
| 260 | } |
Chris Lattner | 4d57e0c | 2006-02-05 01:29:18 +0000 | [diff] [blame] | 261 | } |
| 262 | return Alignment; |
| 263 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 265 | // EmitAlignment - Emit an alignment directive to the specified power of two. |
| 266 | void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const { |
Chris Lattner | a1ab72d | 2005-11-14 19:00:06 +0000 | [diff] [blame] | 267 | if (GV && GV->getAlignment()) |
| 268 | NumBits = Log2_32(GV->getAlignment()); |
Chris Lattner | 2a21c6e | 2005-11-10 18:09:27 +0000 | [diff] [blame] | 269 | if (NumBits == 0) return; // No need to emit alignment. |
Chris Lattner | bfddc20 | 2004-08-17 19:14:29 +0000 | [diff] [blame] | 270 | if (AlignmentIsInBytes) NumBits = 1 << NumBits; |
| 271 | O << AlignDirective << NumBits << "\n"; |
| 272 | } |
| 273 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 274 | /// EmitZeros - Emit a block of zeros. |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 275 | /// |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 276 | void AsmPrinter::EmitZeros(uint64_t NumZeros) const { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 277 | if (NumZeros) { |
Jeff Cohen | c6a057b | 2006-05-02 03:46:13 +0000 | [diff] [blame] | 278 | if (ZeroDirective) { |
| 279 | O << ZeroDirective << NumZeros; |
| 280 | if (ZeroDirectiveSuffix) |
| 281 | O << ZeroDirectiveSuffix; |
| 282 | O << "\n"; |
| 283 | } else { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 284 | for (; NumZeros; --NumZeros) |
| 285 | O << Data8bitsDirective << "0\n"; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 290 | // Print out the specified constant, without a storage class. Only the |
| 291 | // constants valid in constant expressions can occur here. |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 292 | void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 293 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 294 | O << "0"; |
| 295 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 296 | assert(CB == ConstantBool::True); |
| 297 | O << "1"; |
| 298 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 299 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 300 | O << CI->getValue(); |
| 301 | else |
Duraid Madina | 4560657 | 2005-05-15 13:05:48 +0000 | [diff] [blame] | 302 | O << (uint64_t)CI->getValue(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 303 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 304 | O << CI->getValue(); |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 305 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 306 | // This is a constant address for a global variable or function. Use the |
| 307 | // name of the variable or function as the address value, possibly |
| 308 | // decorating it with GlobalVarAddrPrefix/Suffix or |
| 309 | // FunctionAddrPrefix/Suffix (these all default to "" ) |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 310 | if (isa<Function>(GV)) |
| 311 | O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix; |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 312 | else |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 313 | O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix; |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 314 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 315 | const TargetData *TD = TM.getTargetData(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 316 | switch(CE->getOpcode()) { |
| 317 | case Instruction::GetElementPtr: { |
| 318 | // generate a symbolic expression for the byte address |
| 319 | const Constant *ptrVal = CE->getOperand(0); |
| 320 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 321 | if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) { |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 322 | if (Offset) |
| 323 | O << "("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 324 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 325 | if (Offset > 0) |
| 326 | O << ") + " << Offset; |
| 327 | else if (Offset < 0) |
| 328 | O << ") - " << -Offset; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 329 | } else { |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 330 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 331 | } |
| 332 | break; |
| 333 | } |
| 334 | case Instruction::Cast: { |
| 335 | // Support only non-converting or widening casts for now, that is, ones |
| 336 | // that do not involve a change in value. This assertion is really gross, |
| 337 | // and may not even be a complete check. |
| 338 | Constant *Op = CE->getOperand(0); |
| 339 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 340 | |
| 341 | // Remember, kids, pointers can be losslessly converted back and forth |
| 342 | // into 32-bit or wider integers, regardless of signedness. :-P |
| 343 | assert(((isa<PointerType>(OpTy) |
| 344 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 345 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 346 | || (isa<PointerType>(Ty) |
| 347 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 348 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 349 | || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy)) |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 350 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 351 | && "FIXME: Don't yet support this kind of constant cast expr"); |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 352 | EmitConstantValueOnly(Op); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | case Instruction::Add: |
| 356 | O << "("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 357 | EmitConstantValueOnly(CE->getOperand(0)); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 358 | O << ") + ("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 359 | EmitConstantValueOnly(CE->getOperand(1)); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 360 | O << ")"; |
| 361 | break; |
| 362 | default: |
| 363 | assert(0 && "Unsupported operator!"); |
| 364 | } |
| 365 | } else { |
| 366 | assert(0 && "Unknown constant value!"); |
| 367 | } |
| 368 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 369 | |
| 370 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 371 | /// |
| 372 | static inline char toOctal(int X) { |
| 373 | return (X&7)+'0'; |
| 374 | } |
| 375 | |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 376 | /// printAsCString - Print the specified array as a C compatible string, only if |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 377 | /// the predicate isString is true. |
| 378 | /// |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 379 | static void printAsCString(std::ostream &O, const ConstantArray *CVA, |
| 380 | unsigned LastElt) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 381 | assert(CVA->isString() && "Array is not string compatible!"); |
| 382 | |
| 383 | O << "\""; |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 384 | for (unsigned i = 0; i != LastElt; ++i) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 385 | unsigned char C = |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 386 | (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 387 | |
| 388 | if (C == '"') { |
| 389 | O << "\\\""; |
| 390 | } else if (C == '\\') { |
| 391 | O << "\\\\"; |
| 392 | } else if (isprint(C)) { |
| 393 | O << C; |
| 394 | } else { |
| 395 | switch(C) { |
| 396 | case '\b': O << "\\b"; break; |
| 397 | case '\f': O << "\\f"; break; |
| 398 | case '\n': O << "\\n"; break; |
| 399 | case '\r': O << "\\r"; break; |
| 400 | case '\t': O << "\\t"; break; |
| 401 | default: |
| 402 | O << '\\'; |
| 403 | O << toOctal(C >> 6); |
| 404 | O << toOctal(C >> 3); |
| 405 | O << toOctal(C >> 0); |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | O << "\""; |
| 411 | } |
| 412 | |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 413 | /// EmitString - Emit a zero-byte-terminated string constant. |
| 414 | /// |
| 415 | void AsmPrinter::EmitString(const ConstantArray *CVA) const { |
| 416 | unsigned NumElts = CVA->getNumOperands(); |
| 417 | if (AscizDirective && NumElts && |
| 418 | cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) { |
| 419 | O << AscizDirective; |
| 420 | printAsCString(O, CVA, NumElts-1); |
| 421 | } else { |
| 422 | O << AsciiDirective; |
| 423 | printAsCString(O, CVA, NumElts); |
| 424 | } |
| 425 | O << "\n"; |
| 426 | } |
| 427 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 428 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 429 | /// |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 430 | void AsmPrinter::EmitGlobalConstant(const Constant *CV) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 431 | const TargetData *TD = TM.getTargetData(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 432 | |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 433 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 434 | EmitZeros(TD->getTypeSize(CV->getType())); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 435 | return; |
| 436 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 437 | if (CVA->isString()) { |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 438 | EmitString(CVA); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 439 | } else { // Not a string. Print the values in successive locations |
| 440 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 441 | EmitGlobalConstant(CVA->getOperand(i)); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 442 | } |
| 443 | return; |
| 444 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 445 | // Print the fields in successive locations. Pad to align if needed! |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 446 | const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 447 | uint64_t sizeSoFar = 0; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 448 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 449 | const Constant* field = CVS->getOperand(i); |
| 450 | |
| 451 | // Check if padding is needed and insert one or more 0s. |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 452 | uint64_t fieldSize = TD->getTypeSize(field->getType()); |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 453 | uint64_t padSize = ((i == e-1? cvsLayout->StructSize |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 454 | : cvsLayout->MemberOffsets[i+1]) |
| 455 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 456 | sizeSoFar += fieldSize + padSize; |
| 457 | |
| 458 | // Now print the actual field value |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 459 | EmitGlobalConstant(field); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 460 | |
| 461 | // Insert the field padding unless it's zero bytes... |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 462 | EmitZeros(padSize); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 463 | } |
| 464 | assert(sizeSoFar == cvsLayout->StructSize && |
| 465 | "Layout of constant struct may be incorrect!"); |
| 466 | return; |
| 467 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 468 | // FP Constants are printed as integer constants to avoid losing |
| 469 | // precision... |
| 470 | double Val = CFP->getValue(); |
| 471 | if (CFP->getType() == Type::DoubleTy) { |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 472 | if (Data64bitsDirective) |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 473 | O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 474 | << " double value: " << Val << "\n"; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 475 | else if (TD->isBigEndian()) { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 476 | O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 477 | << "\t" << CommentString << " double most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 478 | << Val << "\n"; |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 479 | O << Data32bitsDirective << unsigned(DoubleToBits(Val)) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 480 | << "\t" << CommentString << " double least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 481 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 482 | } else { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 483 | O << Data32bitsDirective << unsigned(DoubleToBits(Val)) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 484 | << "\t" << CommentString << " double least significant word " << Val |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 485 | << "\n"; |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 486 | O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 487 | << "\t" << CommentString << " double most significant word " << Val |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 488 | << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 489 | } |
| 490 | return; |
| 491 | } else { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 492 | O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 493 | << " float " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) { |
| 497 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
| 498 | uint64_t Val = CI->getRawValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 499 | |
Chris Lattner | e85a5a9 | 2004-08-17 06:48:16 +0000 | [diff] [blame] | 500 | if (Data64bitsDirective) |
| 501 | O << Data64bitsDirective << Val << "\n"; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 502 | else if (TD->isBigEndian()) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 503 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 504 | << "\t" << CommentString << " Double-word most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 505 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 506 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 507 | << "\t" << CommentString << " Double-word least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 508 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 509 | } else { |
| 510 | O << Data32bitsDirective << unsigned(Val) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 511 | << "\t" << CommentString << " Double-word least significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 512 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 513 | O << Data32bitsDirective << unsigned(Val >> 32) |
Chris Lattner | f746a7d | 2004-08-18 02:22:55 +0000 | [diff] [blame] | 514 | << "\t" << CommentString << " Double-word most significant word " |
Chris Lattner | 0554fb6 | 2004-08-17 16:27:05 +0000 | [diff] [blame] | 515 | << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 516 | } |
| 517 | return; |
| 518 | } |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 519 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) { |
| 520 | const PackedType *PTy = CP->getType(); |
| 521 | |
| 522 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
| 523 | EmitGlobalConstant(CP->getOperand(I)); |
| 524 | |
| 525 | return; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | const Type *type = CV->getType(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 529 | switch (type->getTypeID()) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 530 | case Type::BoolTyID: |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 531 | case Type::UByteTyID: case Type::SByteTyID: |
| 532 | O << Data8bitsDirective; |
| 533 | break; |
| 534 | case Type::UShortTyID: case Type::ShortTyID: |
| 535 | O << Data16bitsDirective; |
| 536 | break; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 537 | case Type::PointerTyID: |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 538 | if (TD->getPointerSize() == 8) { |
Evan Cheng | a9767f6 | 2006-06-15 08:10:56 +0000 | [diff] [blame^] | 539 | assert(Data64bitsDirective && |
| 540 | "Target cannot handle 64-bit pointer exprs!"); |
Andrew Lenharth | 571c9c3 | 2005-02-04 13:47:16 +0000 | [diff] [blame] | 541 | O << Data64bitsDirective; |
| 542 | break; |
| 543 | } |
| 544 | //Fall through for pointer size == int size |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 545 | case Type::UIntTyID: case Type::IntTyID: |
| 546 | O << Data32bitsDirective; |
| 547 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 548 | case Type::ULongTyID: case Type::LongTyID: |
Chris Lattner | 660538c | 2005-08-08 04:26:32 +0000 | [diff] [blame] | 549 | assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!"); |
| 550 | O << Data64bitsDirective; |
| 551 | break; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 552 | case Type::FloatTyID: case Type::DoubleTyID: |
| 553 | assert (0 && "Should have already output floating point constant."); |
| 554 | default: |
| 555 | assert (0 && "Can't handle printing this type of thing"); |
| 556 | break; |
| 557 | } |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 558 | EmitConstantValueOnly(CV); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 559 | O << "\n"; |
| 560 | } |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 561 | |
| 562 | /// printInlineAsm - This method formats and prints the specified machine |
| 563 | /// instruction that is an inline asm. |
| 564 | void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 565 | O << InlineAsmStart << "\n\t"; |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 566 | unsigned NumOperands = MI->getNumOperands(); |
| 567 | |
| 568 | // Count the number of register definitions. |
| 569 | unsigned NumDefs = 0; |
| 570 | for (; MI->getOperand(NumDefs).isDef(); ++NumDefs) |
| 571 | assert(NumDefs != NumOperands-1 && "No asm string?"); |
| 572 | |
| 573 | assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 574 | |
| 575 | // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 576 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 577 | |
| 578 | // The variant of the current asmprinter: FIXME: change. |
| 579 | int AsmPrinterVariant = 0; |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 581 | int CurVariant = -1; // The number of the {.|.|.} region we are in. |
| 582 | const char *LastEmitted = AsmStr; // One past the last character emitted. |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 584 | while (*LastEmitted) { |
| 585 | switch (*LastEmitted) { |
| 586 | default: { |
| 587 | // Not a special case, emit the string section literally. |
| 588 | const char *LiteralEnd = LastEmitted+1; |
| 589 | while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 590 | *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 591 | ++LiteralEnd; |
| 592 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 593 | O.write(LastEmitted, LiteralEnd-LastEmitted); |
| 594 | LastEmitted = LiteralEnd; |
| 595 | break; |
| 596 | } |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 597 | case '\n': |
| 598 | ++LastEmitted; // Consume newline character. |
| 599 | O << "\n\t"; // Indent code with newline. |
| 600 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 601 | case '$': { |
| 602 | ++LastEmitted; // Consume '$' character. |
| 603 | if (*LastEmitted == '$') { // $$ -> $ |
| 604 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 605 | O << '$'; |
| 606 | ++LastEmitted; // Consume second '$' character. |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | bool HasCurlyBraces = false; |
| 611 | if (*LastEmitted == '{') { // ${variable} |
| 612 | ++LastEmitted; // Consume '{' character. |
| 613 | HasCurlyBraces = true; |
| 614 | } |
| 615 | |
| 616 | const char *IDStart = LastEmitted; |
| 617 | char *IDEnd; |
| 618 | long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. |
| 619 | if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { |
| 620 | std::cerr << "Bad $ operand number in inline asm string: '" |
| 621 | << AsmStr << "'\n"; |
| 622 | exit(1); |
| 623 | } |
| 624 | LastEmitted = IDEnd; |
| 625 | |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 626 | char Modifier[2] = { 0, 0 }; |
| 627 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 628 | if (HasCurlyBraces) { |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 629 | // If we have curly braces, check for a modifier character. This |
| 630 | // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. |
| 631 | if (*LastEmitted == ':') { |
| 632 | ++LastEmitted; // Consume ':' character. |
| 633 | if (*LastEmitted == 0) { |
| 634 | std::cerr << "Bad ${:} expression in inline asm string: '" |
| 635 | << AsmStr << "'\n"; |
| 636 | exit(1); |
| 637 | } |
| 638 | |
| 639 | Modifier[0] = *LastEmitted; |
| 640 | ++LastEmitted; // Consume modifier character. |
| 641 | } |
| 642 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 643 | if (*LastEmitted != '}') { |
| 644 | std::cerr << "Bad ${} expression in inline asm string: '" |
| 645 | << AsmStr << "'\n"; |
| 646 | exit(1); |
| 647 | } |
| 648 | ++LastEmitted; // Consume '}' character. |
| 649 | } |
| 650 | |
| 651 | if ((unsigned)Val >= NumOperands-1) { |
| 652 | std::cerr << "Invalid $ operand number in inline asm string: '" |
| 653 | << AsmStr << "'\n"; |
| 654 | exit(1); |
| 655 | } |
| 656 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 657 | // Okay, we finally have a value number. Ask the target to print this |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 658 | // operand! |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 659 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { |
| 660 | unsigned OpNo = 1; |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 661 | |
| 662 | bool Error = false; |
| 663 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 664 | // Scan to find the machine operand number for the operand. |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 665 | for (; Val; --Val) { |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 666 | if (OpNo >= MI->getNumOperands()) break; |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 667 | unsigned OpFlags = MI->getOperand(OpNo).getImmedValue(); |
| 668 | OpNo += (OpFlags >> 3) + 1; |
| 669 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 670 | |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 671 | if (OpNo >= MI->getNumOperands()) { |
| 672 | Error = true; |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 673 | } else { |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 674 | unsigned OpFlags = MI->getOperand(OpNo).getImmedValue(); |
| 675 | ++OpNo; // Skip over the ID number. |
| 676 | |
| 677 | AsmPrinter *AP = const_cast<AsmPrinter*>(this); |
| 678 | if ((OpFlags & 7) == 4 /*ADDR MODE*/) { |
| 679 | Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, |
| 680 | Modifier[0] ? Modifier : 0); |
| 681 | } else { |
| 682 | Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, |
| 683 | Modifier[0] ? Modifier : 0); |
| 684 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 685 | } |
| 686 | if (Error) { |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 687 | std::cerr << "Invalid operand found in inline asm: '" |
| 688 | << AsmStr << "'\n"; |
| 689 | MI->dump(); |
| 690 | exit(1); |
| 691 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 692 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 693 | break; |
| 694 | } |
| 695 | case '{': |
| 696 | ++LastEmitted; // Consume '{' character. |
| 697 | if (CurVariant != -1) { |
| 698 | std::cerr << "Nested variants found in inline asm string: '" |
| 699 | << AsmStr << "'\n"; |
| 700 | exit(1); |
| 701 | } |
| 702 | CurVariant = 0; // We're in the first variant now. |
| 703 | break; |
| 704 | case '|': |
| 705 | ++LastEmitted; // consume '|' character. |
| 706 | if (CurVariant == -1) { |
| 707 | std::cerr << "Found '|' character outside of variant in inline asm " |
| 708 | << "string: '" << AsmStr << "'\n"; |
| 709 | exit(1); |
| 710 | } |
| 711 | ++CurVariant; // We're in the next variant. |
| 712 | break; |
| 713 | case '}': |
| 714 | ++LastEmitted; // consume '}' character. |
| 715 | if (CurVariant == -1) { |
| 716 | std::cerr << "Found '}' character outside of variant in inline asm " |
| 717 | << "string: '" << AsmStr << "'\n"; |
| 718 | exit(1); |
| 719 | } |
| 720 | CurVariant = -1; |
| 721 | break; |
| 722 | } |
| 723 | } |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 724 | O << "\n\t" << InlineAsmEnd << "\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM |
| 728 | /// instruction, using the specified assembler variant. Targets should |
| 729 | /// overried this to format as appropriate. |
| 730 | bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 731 | unsigned AsmVariant, const char *ExtraCode) { |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 732 | // Target doesn't support this yet! |
| 733 | return true; |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 734 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 735 | |
| 736 | bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 737 | unsigned AsmVariant, |
| 738 | const char *ExtraCode) { |
| 739 | // Target doesn't support this yet! |
| 740 | return true; |
| 741 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 742 | |
| 743 | /// printBasicBlockLabel - This method prints the label for the specified |
| 744 | /// MachineBasicBlock |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 745 | void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, |
| 746 | bool printColon, |
| 747 | bool printComment) const { |
Nate Begeman | 9d51eeb | 2006-05-02 17:36:46 +0000 | [diff] [blame] | 748 | O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_" |
| 749 | << MBB->getNumber(); |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 750 | if (printColon) |
| 751 | O << ':'; |
| 752 | if (printComment) |
| 753 | O << '\t' << CommentString << MBB->getBasicBlock()->getName(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 754 | } |