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" |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Mangler.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Streams.h" |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetAsmInfo.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 0336fdb | 2006-10-06 22:50:56 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 29 | #include <cerrno> |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 32 | static cl::opt<bool> |
| 33 | AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives.")); |
| 34 | |
Jim Laskey | a0f3d17 | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 35 | AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm, |
| 36 | const TargetAsmInfo *T) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 37 | : FunctionNumber(0), O(o), TM(tm), TAI(T) |
| 38 | {} |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 52f0670 | 2006-10-05 02:42:47 +0000 | [diff] [blame] | 40 | std::string AsmPrinter::getSectionForFunction(const Function &F) const { |
| 41 | return TAI->getTextSection(); |
| 42 | } |
| 43 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 45 | /// SwitchToTextSection - Switch to the specified text section of the executable |
| 46 | /// if we are not already in it! |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 47 | /// |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 48 | void AsmPrinter::SwitchToTextSection(const char *NewSection, |
| 49 | const GlobalValue *GV) { |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 50 | std::string NS; |
Chris Lattner | a7090ae | 2006-05-09 05:24:50 +0000 | [diff] [blame] | 51 | if (GV && GV->hasSection()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 52 | NS = TAI->getSwitchToSectionDirective() + GV->getSection(); |
Chris Lattner | a7090ae | 2006-05-09 05:24:50 +0000 | [diff] [blame] | 53 | else |
| 54 | NS = NewSection; |
| 55 | |
| 56 | // If we're already in this section, we're done. |
| 57 | if (CurrentSection == NS) return; |
Jeff Cohen | 51b776d | 2006-05-02 03:58:45 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 59 | // Close the current section, if applicable. |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 60 | if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) |
| 61 | O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n"; |
Jeff Cohen | 51b776d | 2006-05-02 03:58:45 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 63 | CurrentSection = NS; |
| 64 | |
| 65 | if (!CurrentSection.empty()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 66 | O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n'; |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 69 | /// SwitchToDataSection - Switch to the specified data section of the executable |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 70 | /// if we are not already in it! |
| 71 | /// |
| 72 | void AsmPrinter::SwitchToDataSection(const char *NewSection, |
| 73 | const GlobalValue *GV) { |
| 74 | std::string NS; |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 75 | if (GV && GV->hasSection()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 76 | NS = TAI->getSwitchToSectionDirective() + GV->getSection(); |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 77 | else |
| 78 | NS = NewSection; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 79 | |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 80 | // If we're already in this section, we're done. |
| 81 | if (CurrentSection == NS) return; |
| 82 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 83 | // Close the current section, if applicable. |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 84 | if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) |
| 85 | O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n"; |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 86 | |
| 87 | CurrentSection = NS; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 89 | if (!CurrentSection.empty()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 90 | O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n'; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 94 | bool AsmPrinter::doInitialization(Module &M) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 95 | Mang = new Mangler(M, TAI->getGlobalPrefix()); |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 97 | if (!M.getModuleInlineAsm().empty()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 98 | O << TAI->getCommentString() << " Start of file scope inline assembly\n" |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 99 | << M.getModuleInlineAsm() |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 100 | << "\n" << TAI->getCommentString() |
| 101 | << " End of file scope inline assembly\n"; |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 102 | |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 103 | SwitchToDataSection(""); // Reset back to no section. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 104 | |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 105 | if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) { |
| 106 | MMI->AnalyzeModule(M); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | |
| 112 | bool AsmPrinter::doFinalization(Module &M) { |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 113 | if (TAI->getWeakRefDirective()) { |
| 114 | if (ExtWeakSymbols.begin() != ExtWeakSymbols.end()) |
| 115 | SwitchToDataSection(""); |
| 116 | |
| 117 | for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(), |
| 118 | e = ExtWeakSymbols.end(); i != e; ++i) { |
| 119 | const GlobalValue *GV = *i; |
| 120 | std::string Name = Mang->getValueName(GV); |
| 121 | O << TAI->getWeakRefDirective() << Name << "\n"; |
| 122 | } |
| 123 | } |
| 124 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 125 | delete Mang; Mang = 0; |
| 126 | return false; |
| 127 | } |
| 128 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 129 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 130 | // What's my mangled name? |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 131 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
Chris Lattner | 77bc228 | 2005-11-21 08:13:27 +0000 | [diff] [blame] | 132 | IncrementFunctionNumber(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 135 | /// EmitConstantPool - Print to the current output stream assembly |
| 136 | /// representations of the constants in the constant pool MCP. This is |
| 137 | /// used to print out constants which have been "spilled to memory" by |
| 138 | /// the code generator. |
| 139 | /// |
| 140 | void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 141 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 142 | if (CP.empty()) return; |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 143 | |
| 144 | // Some targets require 4-, 8-, and 16- byte constant literals to be placed |
| 145 | // in special sections. |
| 146 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs; |
| 147 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs; |
| 148 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs; |
| 149 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 150 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs; |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 151 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 152 | MachineConstantPoolEntry CPE = CP[i]; |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 153 | const Type *Ty = CPE.getType(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 154 | if (TAI->getFourByteConstantSection() && |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 155 | TM.getTargetData()->getTypeSize(Ty) == 4) |
| 156 | FourByteCPs.push_back(std::make_pair(CPE, i)); |
Evan Cheng | 61c958e | 2006-09-07 18:50:20 +0000 | [diff] [blame] | 157 | else if (TAI->getEightByteConstantSection() && |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 158 | TM.getTargetData()->getTypeSize(Ty) == 8) |
| 159 | EightByteCPs.push_back(std::make_pair(CPE, i)); |
Evan Cheng | 61c958e | 2006-09-07 18:50:20 +0000 | [diff] [blame] | 160 | else if (TAI->getSixteenByteConstantSection() && |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 161 | TM.getTargetData()->getTypeSize(Ty) == 16) |
| 162 | SixteenByteCPs.push_back(std::make_pair(CPE, i)); |
| 163 | else |
| 164 | OtherCPs.push_back(std::make_pair(CPE, i)); |
| 165 | } |
| 166 | |
| 167 | unsigned Alignment = MCP->getConstantPoolAlignment(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 168 | EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs); |
| 169 | EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs); |
| 170 | EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(), |
| 171 | SixteenByteCPs); |
| 172 | EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section, |
| 176 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) { |
| 177 | if (CP.empty()) return; |
| 178 | |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 179 | SwitchToDataSection(Section); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 180 | EmitAlignment(Alignment); |
| 181 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 182 | O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' |
| 183 | << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " "; |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 184 | WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n'; |
| 185 | if (CP[i].first.isMachineConstantPoolEntry()) |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 186 | EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal); |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 187 | else |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 188 | EmitGlobalConstant(CP[i].first.Val.ConstVal); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 189 | if (i != e-1) { |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 190 | const Type *Ty = CP[i].first.getType(); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 191 | unsigned EntSize = |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 192 | TM.getTargetData()->getTypeSize(Ty); |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 193 | unsigned ValEnd = CP[i].first.getOffset() + EntSize; |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 194 | // Emit inter-object padding for alignment. |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 195 | EmitZeros(CP[i+1].first.getOffset()-ValEnd); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 200 | /// EmitJumpTableInfo - Print assembly representations of the jump tables used |
| 201 | /// by the current function to the current output stream. |
| 202 | /// |
Chris Lattner | 1da31ee | 2006-10-05 03:01:21 +0000 | [diff] [blame] | 203 | void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, |
| 204 | MachineFunction &MF) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 205 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 206 | if (JT.empty()) return; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 207 | bool IsPic = TM.getRelocationModel() == Reloc::PIC_; |
Nate Begeman | 4d9bbdc | 2006-07-27 16:46:58 +0000 | [diff] [blame] | 208 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 209 | // Use JumpTableDirective otherwise honor the entry size from the jump table |
| 210 | // info. |
Andrew Lenharth | beec30e | 2006-09-24 19:45:58 +0000 | [diff] [blame] | 211 | const char *JTEntryDirective = TAI->getJumpTableDirective(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 212 | bool HadJTEntryDirective = JTEntryDirective != NULL; |
| 213 | if (!HadJTEntryDirective) { |
| 214 | JTEntryDirective = MJTI->getEntrySize() == 4 ? |
| 215 | TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); |
| 216 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 217 | |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 218 | // Pick the directive to use to print the jump table entries, and switch to |
| 219 | // the appropriate section. |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 220 | TargetLowering *LoweringInfo = TM.getTargetLowering(); |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 221 | |
| 222 | const char* JumpTableDataSection = TAI->getJumpTableDataSection(); |
| 223 | if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) || |
| 224 | !JumpTableDataSection) { |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 225 | // In PIC mode, we need to emit the jump table to the same section as the |
| 226 | // function body itself, otherwise the label differences won't make sense. |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 227 | // We should also do if the section name is NULL. |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 228 | const Function *F = MF.getFunction(); |
| 229 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 230 | } else { |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 231 | SwitchToDataSection(JumpTableDataSection); |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 232 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 233 | |
| 234 | EmitAlignment(Log2_32(MJTI->getAlignment())); |
Chris Lattner | 0c4e678 | 2006-07-15 01:34:12 +0000 | [diff] [blame] | 235 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 236 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 237 | const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; |
Chris Lattner | 0737188 | 2006-10-28 18:10:06 +0000 | [diff] [blame] | 238 | |
| 239 | // If this jump table was deleted, ignore it. |
| 240 | if (JTBBs.empty()) continue; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 241 | |
| 242 | // For PIC codegen, if possible we want to use the SetDirective to reduce |
| 243 | // the number of relocations the assembler will generate for the jump table. |
| 244 | // Set directives are all printed before the jump table itself. |
| 245 | std::set<MachineBasicBlock*> EmittedSets; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 246 | if (TAI->getSetDirective() && IsPic) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 247 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) |
| 248 | if (EmittedSets.insert(JTBBs[ii]).second) |
| 249 | printSetLabel(i, JTBBs[ii]); |
| 250 | |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 251 | // On some targets (e.g. darwin) we want to emit two consequtive labels |
| 252 | // before each jump table. The first label is never referenced, but tells |
| 253 | // the assembler and linker the extents of the jump table object. The |
| 254 | // second label is actually referenced by the code. |
| 255 | if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix()) |
| 256 | O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n"; |
| 257 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 258 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 259 | << '_' << i << ":\n"; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 260 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 261 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { |
Nate Begeman | 4d9bbdc | 2006-07-27 16:46:58 +0000 | [diff] [blame] | 262 | O << JTEntryDirective << ' '; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 263 | // If we have emitted set directives for the jump table entries, print |
| 264 | // them rather than the entries themselves. If we're emitting PIC, then |
| 265 | // emit the table entries as differences between two text section labels. |
| 266 | // If we're emitting non-PIC code, then emit the entries as direct |
| 267 | // references to the target basic blocks. |
| 268 | if (!EmittedSets.empty()) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 269 | O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() |
| 270 | << '_' << i << "_set_" << JTBBs[ii]->getNumber(); |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 271 | } else if (IsPic) { |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 272 | printBasicBlockLabel(JTBBs[ii], false, false); |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 273 | // If the arch uses custom Jump Table directives, don't calc relative to |
| 274 | // JT |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 275 | if (!HadJTEntryDirective) |
| 276 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" |
| 277 | << getFunctionNumber() << '_' << i; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 278 | } else { |
| 279 | printBasicBlockLabel(JTBBs[ii], false, false); |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 280 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 281 | O << '\n'; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 286 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a |
| 287 | /// special global used by LLVM. If so, emit it and return true, otherwise |
| 288 | /// do nothing and return false. |
| 289 | bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { |
Jim Laskey | 7809811 | 2006-03-07 22:00:35 +0000 | [diff] [blame] | 290 | // Ignore debug and non-emitted data. |
| 291 | if (GV->getSection() == "llvm.metadata") return true; |
| 292 | |
| 293 | if (!GV->hasAppendingLinkage()) return false; |
| 294 | |
| 295 | assert(GV->hasInitializer() && "Not a special LLVM global!"); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 296 | |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 297 | if (GV->getName() == "llvm.used") { |
| 298 | if (TAI->getUsedDirective() != 0) // No need to emit this at all. |
| 299 | EmitLLVMUsedList(GV->getInitializer()); |
| 300 | return true; |
| 301 | } |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 303 | if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) { |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 304 | SwitchToDataSection(TAI->getStaticCtorsSection()); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 305 | EmitAlignment(2, 0); |
| 306 | EmitXXStructorList(GV->getInitializer()); |
| 307 | return true; |
| 308 | } |
| 309 | |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 310 | if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) { |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 311 | SwitchToDataSection(TAI->getStaticDtorsSection()); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 312 | EmitAlignment(2, 0); |
| 313 | EmitXXStructorList(GV->getInitializer()); |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | return false; |
| 318 | } |
| 319 | |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 320 | /// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each |
| 321 | /// global in the specified llvm.used list as being used with this directive. |
| 322 | void AsmPrinter::EmitLLVMUsedList(Constant *List) { |
| 323 | const char *Directive = TAI->getUsedDirective(); |
| 324 | |
| 325 | // Should be an array of 'sbyte*'. |
| 326 | ConstantArray *InitList = dyn_cast<ConstantArray>(List); |
| 327 | if (InitList == 0) return; |
| 328 | |
| 329 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 330 | O << Directive; |
| 331 | EmitConstantValueOnly(InitList->getOperand(i)); |
| 332 | O << "\n"; |
| 333 | } |
| 334 | } |
| 335 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 336 | /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the |
| 337 | /// function pointers, ignoring the init priority. |
| 338 | void AsmPrinter::EmitXXStructorList(Constant *List) { |
| 339 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 340 | // init priority, which we ignore. |
| 341 | if (!isa<ConstantArray>(List)) return; |
| 342 | ConstantArray *InitList = cast<ConstantArray>(List); |
| 343 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 344 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 345 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
Chris Lattner | 8de324b | 2005-12-21 01:17:37 +0000 | [diff] [blame] | 346 | |
| 347 | if (CS->getOperand(1)->isNullValue()) |
| 348 | return; // Found a null terminator, exit printing. |
| 349 | // Emit the function pointer. |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 350 | EmitGlobalConstant(CS->getOperand(1)); |
| 351 | } |
| 352 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 353 | |
Jim Laskey | a1a19f8 | 2006-10-17 13:41:07 +0000 | [diff] [blame] | 354 | /// getGlobalLinkName - Returns the asm/link name of of the specified |
| 355 | /// global variable. Should be overridden by each target asm printer to |
| 356 | /// generate the appropriate value. |
Jim Laskey | 99e41ee | 2006-10-17 17:17:24 +0000 | [diff] [blame] | 357 | const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{ |
| 358 | std::string LinkName; |
Jim Laskey | 0374248 | 2006-11-20 20:29:06 +0000 | [diff] [blame] | 359 | |
| 360 | if (isa<Function>(GV)) { |
| 361 | LinkName += TAI->getFunctionAddrPrefix(); |
| 362 | LinkName += Mang->getValueName(GV); |
| 363 | LinkName += TAI->getFunctionAddrSuffix(); |
| 364 | } else { |
| 365 | LinkName += TAI->getGlobalVarAddrPrefix(); |
| 366 | LinkName += Mang->getValueName(GV); |
| 367 | LinkName += TAI->getGlobalVarAddrSuffix(); |
| 368 | } |
| 369 | |
Jim Laskey | 99e41ee | 2006-10-17 17:17:24 +0000 | [diff] [blame] | 370 | return LinkName; |
Jim Laskey | a1a19f8 | 2006-10-17 13:41:07 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 373 | //===----------------------------------------------------------------------===// |
| 374 | /// LEB 128 number encoding. |
| 375 | |
| 376 | /// PrintULEB128 - Print a series of hexidecimal values (separated by commas) |
| 377 | /// representing an unsigned leb128 value. |
| 378 | void AsmPrinter::PrintULEB128(unsigned Value) const { |
| 379 | do { |
| 380 | unsigned Byte = Value & 0x7f; |
| 381 | Value >>= 7; |
| 382 | if (Value) Byte |= 0x80; |
| 383 | O << "0x" << std::hex << Byte << std::dec; |
| 384 | if (Value) O << ", "; |
| 385 | } while (Value); |
| 386 | } |
| 387 | |
| 388 | /// SizeULEB128 - Compute the number of bytes required for an unsigned leb128 |
| 389 | /// value. |
| 390 | unsigned AsmPrinter::SizeULEB128(unsigned Value) { |
| 391 | unsigned Size = 0; |
| 392 | do { |
| 393 | Value >>= 7; |
| 394 | Size += sizeof(int8_t); |
| 395 | } while (Value); |
| 396 | return Size; |
| 397 | } |
| 398 | |
| 399 | /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) |
| 400 | /// representing a signed leb128 value. |
| 401 | void AsmPrinter::PrintSLEB128(int Value) const { |
| 402 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 403 | bool IsMore; |
| 404 | |
| 405 | do { |
| 406 | unsigned Byte = Value & 0x7f; |
| 407 | Value >>= 7; |
| 408 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 409 | if (IsMore) Byte |= 0x80; |
| 410 | O << "0x" << std::hex << Byte << std::dec; |
| 411 | if (IsMore) O << ", "; |
| 412 | } while (IsMore); |
| 413 | } |
| 414 | |
| 415 | /// SizeSLEB128 - Compute the number of bytes required for a signed leb128 |
| 416 | /// value. |
| 417 | unsigned AsmPrinter::SizeSLEB128(int Value) { |
| 418 | unsigned Size = 0; |
| 419 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 420 | bool IsMore; |
| 421 | |
| 422 | do { |
| 423 | unsigned Byte = Value & 0x7f; |
| 424 | Value >>= 7; |
| 425 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 426 | Size += sizeof(int8_t); |
| 427 | } while (IsMore); |
| 428 | return Size; |
| 429 | } |
| 430 | |
| 431 | //===--------------------------------------------------------------------===// |
| 432 | // Emission and print routines |
| 433 | // |
| 434 | |
| 435 | /// PrintHex - Print a value as a hexidecimal value. |
| 436 | /// |
| 437 | void AsmPrinter::PrintHex(int Value) const { |
| 438 | O << "0x" << std::hex << Value << std::dec; |
| 439 | } |
| 440 | |
| 441 | /// EOL - Print a newline character to asm stream. If a comment is present |
| 442 | /// then it will be printed first. Comments should not contain '\n'. |
| 443 | void AsmPrinter::EOL(const std::string &Comment) const { |
| 444 | if (AsmVerbose && !Comment.empty()) { |
| 445 | O << "\t" |
| 446 | << TAI->getCommentString() |
| 447 | << " " |
| 448 | << Comment; |
| 449 | } |
| 450 | O << "\n"; |
| 451 | } |
| 452 | |
| 453 | /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an |
| 454 | /// unsigned leb128 value. |
| 455 | void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { |
| 456 | if (TAI->hasLEB128()) { |
| 457 | O << "\t.uleb128\t" |
| 458 | << Value; |
| 459 | } else { |
| 460 | O << TAI->getData8bitsDirective(); |
| 461 | PrintULEB128(Value); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /// EmitSLEB128Bytes - print an assembler byte data directive to compose a |
| 466 | /// signed leb128 value. |
| 467 | void AsmPrinter::EmitSLEB128Bytes(int Value) const { |
| 468 | if (TAI->hasLEB128()) { |
| 469 | O << "\t.sleb128\t" |
| 470 | << Value; |
| 471 | } else { |
| 472 | O << TAI->getData8bitsDirective(); |
| 473 | PrintSLEB128(Value); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | /// EmitInt8 - Emit a byte directive and value. |
| 478 | /// |
| 479 | void AsmPrinter::EmitInt8(int Value) const { |
| 480 | O << TAI->getData8bitsDirective(); |
| 481 | PrintHex(Value & 0xFF); |
| 482 | } |
| 483 | |
| 484 | /// EmitInt16 - Emit a short directive and value. |
| 485 | /// |
| 486 | void AsmPrinter::EmitInt16(int Value) const { |
| 487 | O << TAI->getData16bitsDirective(); |
| 488 | PrintHex(Value & 0xFFFF); |
| 489 | } |
| 490 | |
| 491 | /// EmitInt32 - Emit a long directive and value. |
| 492 | /// |
| 493 | void AsmPrinter::EmitInt32(int Value) const { |
| 494 | O << TAI->getData32bitsDirective(); |
| 495 | PrintHex(Value); |
| 496 | } |
| 497 | |
| 498 | /// EmitInt64 - Emit a long long directive and value. |
| 499 | /// |
| 500 | void AsmPrinter::EmitInt64(uint64_t Value) const { |
| 501 | if (TAI->getData64bitsDirective()) { |
| 502 | O << TAI->getData64bitsDirective(); |
| 503 | PrintHex(Value); |
| 504 | } else { |
| 505 | if (TM.getTargetData()->isBigEndian()) { |
| 506 | EmitInt32(unsigned(Value >> 32)); O << "\n"; |
| 507 | EmitInt32(unsigned(Value)); |
| 508 | } else { |
| 509 | EmitInt32(unsigned(Value)); O << "\n"; |
| 510 | EmitInt32(unsigned(Value >> 32)); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 516 | /// |
| 517 | static inline char toOctal(int X) { |
| 518 | return (X&7)+'0'; |
| 519 | } |
| 520 | |
| 521 | /// printStringChar - Print a char, escaped if necessary. |
| 522 | /// |
| 523 | static void printStringChar(std::ostream &O, unsigned char C) { |
| 524 | if (C == '"') { |
| 525 | O << "\\\""; |
| 526 | } else if (C == '\\') { |
| 527 | O << "\\\\"; |
| 528 | } else if (isprint(C)) { |
| 529 | O << C; |
| 530 | } else { |
| 531 | switch(C) { |
| 532 | case '\b': O << "\\b"; break; |
| 533 | case '\f': O << "\\f"; break; |
| 534 | case '\n': O << "\\n"; break; |
| 535 | case '\r': O << "\\r"; break; |
| 536 | case '\t': O << "\\t"; break; |
| 537 | default: |
| 538 | O << '\\'; |
| 539 | O << toOctal(C >> 6); |
| 540 | O << toOctal(C >> 3); |
| 541 | O << toOctal(C >> 0); |
| 542 | break; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /// EmitString - Emit a string with quotes and a null terminator. |
| 548 | /// Special characters are emitted properly. |
| 549 | /// \literal (Eg. '\t') \endliteral |
| 550 | void AsmPrinter::EmitString(const std::string &String) const { |
| 551 | O << TAI->getAsciiDirective() |
| 552 | << "\""; |
| 553 | for (unsigned i = 0, N = String.size(); i < N; ++i) { |
| 554 | unsigned char C = String[i]; |
| 555 | printStringChar(O, C); |
| 556 | } |
| 557 | O << "\\0\""; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | //===----------------------------------------------------------------------===// |
| 562 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 563 | // EmitAlignment - Emit an alignment directive to the specified power of two. |
| 564 | void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const { |
Chris Lattner | a1ab72d | 2005-11-14 19:00:06 +0000 | [diff] [blame] | 565 | if (GV && GV->getAlignment()) |
| 566 | NumBits = Log2_32(GV->getAlignment()); |
Chris Lattner | 2a21c6e | 2005-11-10 18:09:27 +0000 | [diff] [blame] | 567 | if (NumBits == 0) return; // No need to emit alignment. |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 568 | if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits; |
| 569 | O << TAI->getAlignDirective() << NumBits << "\n"; |
Chris Lattner | bfddc20 | 2004-08-17 19:14:29 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 572 | /// EmitZeros - Emit a block of zeros. |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 573 | /// |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 574 | void AsmPrinter::EmitZeros(uint64_t NumZeros) const { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 575 | if (NumZeros) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 576 | if (TAI->getZeroDirective()) { |
| 577 | O << TAI->getZeroDirective() << NumZeros; |
| 578 | if (TAI->getZeroDirectiveSuffix()) |
| 579 | O << TAI->getZeroDirectiveSuffix(); |
Jeff Cohen | c6a057b | 2006-05-02 03:46:13 +0000 | [diff] [blame] | 580 | O << "\n"; |
| 581 | } else { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 582 | for (; NumZeros; --NumZeros) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 583 | O << TAI->getData8bitsDirective() << "0\n"; |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 588 | // Print out the specified constant, without a storage class. Only the |
| 589 | // constants valid in constant expressions can occur here. |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 590 | void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 591 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 592 | O << "0"; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 593 | else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Chris Lattner | a875df3 | 2007-01-12 18:15:09 +0000 | [diff] [blame] | 594 | O << CI->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 595 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 596 | // This is a constant address for a global variable or function. Use the |
| 597 | // name of the variable or function as the address value, possibly |
| 598 | // decorating it with GlobalVarAddrPrefix/Suffix or |
| 599 | // FunctionAddrPrefix/Suffix (these all default to "" ) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 600 | if (isa<Function>(GV)) { |
| 601 | O << TAI->getFunctionAddrPrefix() |
| 602 | << Mang->getValueName(GV) |
| 603 | << TAI->getFunctionAddrSuffix(); |
| 604 | } else { |
| 605 | O << TAI->getGlobalVarAddrPrefix() |
| 606 | << Mang->getValueName(GV) |
| 607 | << TAI->getGlobalVarAddrSuffix(); |
| 608 | } |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 609 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 610 | const TargetData *TD = TM.getTargetData(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 611 | switch(CE->getOpcode()) { |
| 612 | case Instruction::GetElementPtr: { |
| 613 | // generate a symbolic expression for the byte address |
| 614 | const Constant *ptrVal = CE->getOperand(0); |
| 615 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 616 | if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) { |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 617 | if (Offset) |
| 618 | O << "("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 619 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 620 | if (Offset > 0) |
| 621 | O << ") + " << Offset; |
| 622 | else if (Offset < 0) |
| 623 | O << ") - " << -Offset; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 624 | } else { |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 625 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 626 | } |
| 627 | break; |
| 628 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 629 | case Instruction::Trunc: |
| 630 | case Instruction::ZExt: |
| 631 | case Instruction::SExt: |
| 632 | case Instruction::FPTrunc: |
| 633 | case Instruction::FPExt: |
| 634 | case Instruction::UIToFP: |
| 635 | case Instruction::SIToFP: |
| 636 | case Instruction::FPToUI: |
| 637 | case Instruction::FPToSI: |
| 638 | assert(0 && "FIXME: Don't yet support this kind of constant cast expr"); |
| 639 | break; |
Chris Lattner | cb0a681 | 2006-12-12 05:14:13 +0000 | [diff] [blame] | 640 | case Instruction::BitCast: |
| 641 | return EmitConstantValueOnly(CE->getOperand(0)); |
| 642 | |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 643 | case Instruction::IntToPtr: { |
| 644 | // Handle casts to pointers by changing them into casts to the appropriate |
| 645 | // integer type. This promotes constant folding and simplifies this code. |
| 646 | Constant *Op = CE->getOperand(0); |
| 647 | Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/); |
| 648 | return EmitConstantValueOnly(Op); |
| 649 | } |
| 650 | |
| 651 | |
| 652 | case Instruction::PtrToInt: { |
Chris Lattner | 4a6bd33 | 2006-07-29 01:57:19 +0000 | [diff] [blame] | 653 | // Support only foldable casts to/from pointers that can be eliminated by |
| 654 | // changing the pointer to the appropriately sized integer type. |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 655 | Constant *Op = CE->getOperand(0); |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 656 | const Type *Ty = CE->getType(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 657 | |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 658 | // We can emit the pointer value into this slot if the slot is an |
| 659 | // integer slot greater or equal to the size of the pointer. |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 660 | if (Ty->isInteger() && |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 661 | TD->getTypeSize(Ty) >= TD->getTypeSize(Op->getType())) |
Chris Lattner | 4a6bd33 | 2006-07-29 01:57:19 +0000 | [diff] [blame] | 662 | return EmitConstantValueOnly(Op); |
Chris Lattner | 4a6bd33 | 2006-07-29 01:57:19 +0000 | [diff] [blame] | 663 | |
| 664 | assert(0 && "FIXME: Don't yet support this kind of constant cast expr"); |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 665 | EmitConstantValueOnly(Op); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 666 | break; |
| 667 | } |
| 668 | case Instruction::Add: |
| 669 | O << "("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 670 | EmitConstantValueOnly(CE->getOperand(0)); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 671 | O << ") + ("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 672 | EmitConstantValueOnly(CE->getOperand(1)); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 673 | O << ")"; |
| 674 | break; |
| 675 | default: |
| 676 | assert(0 && "Unsupported operator!"); |
| 677 | } |
| 678 | } else { |
| 679 | assert(0 && "Unknown constant value!"); |
| 680 | } |
| 681 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 682 | |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 683 | /// printAsCString - Print the specified array as a C compatible string, only if |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 684 | /// the predicate isString is true. |
| 685 | /// |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 686 | static void printAsCString(std::ostream &O, const ConstantArray *CVA, |
| 687 | unsigned LastElt) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 688 | assert(CVA->isString() && "Array is not string compatible!"); |
| 689 | |
| 690 | O << "\""; |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 691 | for (unsigned i = 0; i != LastElt; ++i) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 692 | unsigned char C = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 693 | (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 694 | printStringChar(O, C); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 695 | } |
| 696 | O << "\""; |
| 697 | } |
| 698 | |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 699 | /// EmitString - Emit a zero-byte-terminated string constant. |
| 700 | /// |
| 701 | void AsmPrinter::EmitString(const ConstantArray *CVA) const { |
| 702 | unsigned NumElts = CVA->getNumOperands(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 703 | if (TAI->getAscizDirective() && NumElts && |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 704 | cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 705 | O << TAI->getAscizDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 706 | printAsCString(O, CVA, NumElts-1); |
| 707 | } else { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 708 | O << TAI->getAsciiDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 709 | printAsCString(O, CVA, NumElts); |
| 710 | } |
| 711 | O << "\n"; |
| 712 | } |
| 713 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 714 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 715 | /// |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 716 | void AsmPrinter::EmitGlobalConstant(const Constant *CV) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 717 | const TargetData *TD = TM.getTargetData(); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 718 | |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 719 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 720 | EmitZeros(TD->getTypeSize(CV->getType())); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 721 | return; |
| 722 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 723 | if (CVA->isString()) { |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 724 | EmitString(CVA); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 725 | } else { // Not a string. Print the values in successive locations |
| 726 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 727 | EmitGlobalConstant(CVA->getOperand(i)); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 728 | } |
| 729 | return; |
| 730 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 731 | // Print the fields in successive locations. Pad to align if needed! |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 732 | const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 733 | uint64_t sizeSoFar = 0; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 734 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 735 | const Constant* field = CVS->getOperand(i); |
| 736 | |
| 737 | // Check if padding is needed and insert one or more 0s. |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 738 | uint64_t fieldSize = TD->getTypeSize(field->getType()); |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 739 | uint64_t padSize = ((i == e-1? cvsLayout->StructSize |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 740 | : cvsLayout->MemberOffsets[i+1]) |
| 741 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 742 | sizeSoFar += fieldSize + padSize; |
| 743 | |
| 744 | // Now print the actual field value |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 745 | EmitGlobalConstant(field); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 746 | |
| 747 | // Insert the field padding unless it's zero bytes... |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 748 | EmitZeros(padSize); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 749 | } |
| 750 | assert(sizeSoFar == cvsLayout->StructSize && |
| 751 | "Layout of constant struct may be incorrect!"); |
| 752 | return; |
| 753 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 754 | // FP Constants are printed as integer constants to avoid losing |
| 755 | // precision... |
| 756 | double Val = CFP->getValue(); |
| 757 | if (CFP->getType() == Type::DoubleTy) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 758 | if (TAI->getData64bitsDirective()) |
| 759 | O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t" |
| 760 | << TAI->getCommentString() << " double value: " << Val << "\n"; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 761 | else if (TD->isBigEndian()) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 762 | O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32) |
| 763 | << "\t" << TAI->getCommentString() |
| 764 | << " double most significant word " << Val << "\n"; |
| 765 | O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val)) |
| 766 | << "\t" << TAI->getCommentString() |
| 767 | << " double least significant word " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 768 | } else { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 769 | O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val)) |
| 770 | << "\t" << TAI->getCommentString() |
| 771 | << " double least significant word " << Val << "\n"; |
| 772 | O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32) |
| 773 | << "\t" << TAI->getCommentString() |
| 774 | << " double most significant word " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 775 | } |
| 776 | return; |
| 777 | } else { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 778 | O << TAI->getData32bitsDirective() << FloatToBits(Val) |
| 779 | << "\t" << TAI->getCommentString() << " float " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 780 | return; |
| 781 | } |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 782 | } else if (CV->getType() == Type::Int64Ty) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 783 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 784 | uint64_t Val = CI->getZExtValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 785 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 786 | if (TAI->getData64bitsDirective()) |
| 787 | O << TAI->getData64bitsDirective() << Val << "\n"; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 788 | else if (TD->isBigEndian()) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 789 | O << TAI->getData32bitsDirective() << unsigned(Val >> 32) |
| 790 | << "\t" << TAI->getCommentString() |
| 791 | << " Double-word most significant word " << Val << "\n"; |
| 792 | O << TAI->getData32bitsDirective() << unsigned(Val) |
| 793 | << "\t" << TAI->getCommentString() |
| 794 | << " Double-word least significant word " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 795 | } else { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 796 | O << TAI->getData32bitsDirective() << unsigned(Val) |
| 797 | << "\t" << TAI->getCommentString() |
| 798 | << " Double-word least significant word " << Val << "\n"; |
| 799 | O << TAI->getData32bitsDirective() << unsigned(Val >> 32) |
| 800 | << "\t" << TAI->getCommentString() |
| 801 | << " Double-word most significant word " << Val << "\n"; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 802 | } |
| 803 | return; |
| 804 | } |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 805 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) { |
| 806 | const PackedType *PTy = CP->getType(); |
| 807 | |
| 808 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
| 809 | EmitGlobalConstant(CP->getOperand(I)); |
| 810 | |
| 811 | return; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | const Type *type = CV->getType(); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 815 | printDataDirective(type); |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 816 | EmitConstantValueOnly(CV); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 817 | O << "\n"; |
| 818 | } |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 819 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 820 | void |
| 821 | AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { |
| 822 | // Target doesn't support this yet! |
| 823 | abort(); |
| 824 | } |
| 825 | |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 826 | /// PrintSpecial - Print information related to the specified machine instr |
| 827 | /// that is independent of the operand, and may be independent of the instr |
| 828 | /// itself. This can be useful for portably encoding the comment character |
| 829 | /// or other bits of target-specific knowledge into the asmstrings. The |
| 830 | /// syntax used is ${:comment}. Targets can override this to add support |
| 831 | /// for their own strange codes. |
| 832 | void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) { |
Chris Lattner | bae02cf | 2006-09-27 00:06:07 +0000 | [diff] [blame] | 833 | if (!strcmp(Code, "private")) { |
| 834 | O << TAI->getPrivateGlobalPrefix(); |
| 835 | } else if (!strcmp(Code, "comment")) { |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 836 | O << TAI->getCommentString(); |
| 837 | } else if (!strcmp(Code, "uid")) { |
| 838 | // Assign a unique ID to this machine instruction. |
| 839 | static const MachineInstr *LastMI = 0; |
| 840 | static unsigned Counter = 0U-1; |
| 841 | // If this is a new machine instruction, bump the counter. |
| 842 | if (LastMI != MI) { ++Counter; LastMI = MI; } |
| 843 | O << Counter; |
| 844 | } else { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 845 | cerr << "Unknown special formatter '" << Code |
| 846 | << "' for machine instr: " << *MI; |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 847 | exit(1); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 852 | /// printInlineAsm - This method formats and prints the specified machine |
| 853 | /// instruction that is an inline asm. |
| 854 | void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 855 | unsigned NumOperands = MI->getNumOperands(); |
| 856 | |
| 857 | // Count the number of register definitions. |
| 858 | unsigned NumDefs = 0; |
Chris Lattner | 67942f5 | 2006-09-05 20:02:51 +0000 | [diff] [blame] | 859 | for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); |
| 860 | ++NumDefs) |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 861 | assert(NumDefs != NumOperands-1 && "No asm string?"); |
| 862 | |
| 863 | assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 864 | |
| 865 | // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 866 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 867 | |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 868 | // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers. |
| 869 | if (AsmStr[0] == 0) { |
| 870 | O << "\n"; // Tab already printed, avoid double indenting next instr. |
| 871 | return; |
| 872 | } |
| 873 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 874 | O << TAI->getInlineAsmStart() << "\n\t"; |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 875 | |
Bill Wendling | eb9a42c | 2007-01-16 03:42:04 +0000 | [diff] [blame] | 876 | // The variant of the current asmprinter. |
| 877 | int AsmPrinterVariant = TAI->getAssemblerDialect(); |
| 878 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 879 | int CurVariant = -1; // The number of the {.|.|.} region we are in. |
| 880 | const char *LastEmitted = AsmStr; // One past the last character emitted. |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 881 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 882 | while (*LastEmitted) { |
| 883 | switch (*LastEmitted) { |
| 884 | default: { |
| 885 | // Not a special case, emit the string section literally. |
| 886 | const char *LiteralEnd = LastEmitted+1; |
| 887 | while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 888 | *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 889 | ++LiteralEnd; |
| 890 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 891 | O.write(LastEmitted, LiteralEnd-LastEmitted); |
| 892 | LastEmitted = LiteralEnd; |
| 893 | break; |
| 894 | } |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 895 | case '\n': |
| 896 | ++LastEmitted; // Consume newline character. |
| 897 | O << "\n\t"; // Indent code with newline. |
| 898 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 899 | case '$': { |
| 900 | ++LastEmitted; // Consume '$' character. |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 901 | bool Done = true; |
| 902 | |
| 903 | // Handle escapes. |
| 904 | switch (*LastEmitted) { |
| 905 | default: Done = false; break; |
| 906 | case '$': // $$ -> $ |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 907 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 908 | O << '$'; |
| 909 | ++LastEmitted; // Consume second '$' character. |
| 910 | break; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 911 | case '(': // $( -> same as GCC's { character. |
| 912 | ++LastEmitted; // Consume '(' character. |
| 913 | if (CurVariant != -1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 914 | cerr << "Nested variants found in inline asm string: '" |
| 915 | << AsmStr << "'\n"; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 916 | exit(1); |
| 917 | } |
| 918 | CurVariant = 0; // We're in the first variant now. |
| 919 | break; |
| 920 | case '|': |
| 921 | ++LastEmitted; // consume '|' character. |
| 922 | if (CurVariant == -1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 923 | cerr << "Found '|' character outside of variant in inline asm " |
| 924 | << "string: '" << AsmStr << "'\n"; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 925 | exit(1); |
| 926 | } |
| 927 | ++CurVariant; // We're in the next variant. |
| 928 | break; |
| 929 | case ')': // $) -> same as GCC's } char. |
| 930 | ++LastEmitted; // consume ')' character. |
| 931 | if (CurVariant == -1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 932 | cerr << "Found '}' character outside of variant in inline asm " |
| 933 | << "string: '" << AsmStr << "'\n"; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 934 | exit(1); |
| 935 | } |
| 936 | CurVariant = -1; |
| 937 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 938 | } |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 939 | if (Done) break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 940 | |
| 941 | bool HasCurlyBraces = false; |
| 942 | if (*LastEmitted == '{') { // ${variable} |
| 943 | ++LastEmitted; // Consume '{' character. |
| 944 | HasCurlyBraces = true; |
| 945 | } |
| 946 | |
| 947 | const char *IDStart = LastEmitted; |
| 948 | char *IDEnd; |
Chris Lattner | fad2912 | 2007-01-23 00:36:17 +0000 | [diff] [blame] | 949 | errno = 0; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 950 | long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. |
| 951 | if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 952 | cerr << "Bad $ operand number in inline asm string: '" |
| 953 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 954 | exit(1); |
| 955 | } |
| 956 | LastEmitted = IDEnd; |
| 957 | |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 958 | char Modifier[2] = { 0, 0 }; |
| 959 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 960 | if (HasCurlyBraces) { |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 961 | // If we have curly braces, check for a modifier character. This |
| 962 | // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. |
| 963 | if (*LastEmitted == ':') { |
| 964 | ++LastEmitted; // Consume ':' character. |
| 965 | if (*LastEmitted == 0) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 966 | cerr << "Bad ${:} expression in inline asm string: '" |
| 967 | << AsmStr << "'\n"; |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 968 | exit(1); |
| 969 | } |
| 970 | |
| 971 | Modifier[0] = *LastEmitted; |
| 972 | ++LastEmitted; // Consume modifier character. |
| 973 | } |
| 974 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 975 | if (*LastEmitted != '}') { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 976 | cerr << "Bad ${} expression in inline asm string: '" |
| 977 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 978 | exit(1); |
| 979 | } |
| 980 | ++LastEmitted; // Consume '}' character. |
| 981 | } |
| 982 | |
| 983 | if ((unsigned)Val >= NumOperands-1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 984 | cerr << "Invalid $ operand number in inline asm string: '" |
| 985 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 986 | exit(1); |
| 987 | } |
| 988 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 989 | // 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] | 990 | // operand! |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 991 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { |
| 992 | unsigned OpNo = 1; |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 993 | |
| 994 | bool Error = false; |
| 995 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 996 | // Scan to find the machine operand number for the operand. |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 997 | for (; Val; --Val) { |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 998 | if (OpNo >= MI->getNumOperands()) break; |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 999 | unsigned OpFlags = MI->getOperand(OpNo).getImmedValue(); |
| 1000 | OpNo += (OpFlags >> 3) + 1; |
| 1001 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1002 | |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1003 | if (OpNo >= MI->getNumOperands()) { |
| 1004 | Error = true; |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1005 | } else { |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1006 | unsigned OpFlags = MI->getOperand(OpNo).getImmedValue(); |
| 1007 | ++OpNo; // Skip over the ID number. |
| 1008 | |
| 1009 | AsmPrinter *AP = const_cast<AsmPrinter*>(this); |
| 1010 | if ((OpFlags & 7) == 4 /*ADDR MODE*/) { |
| 1011 | Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, |
| 1012 | Modifier[0] ? Modifier : 0); |
| 1013 | } else { |
| 1014 | Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, |
| 1015 | Modifier[0] ? Modifier : 0); |
| 1016 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1017 | } |
| 1018 | if (Error) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1019 | cerr << "Invalid operand found in inline asm: '" |
| 1020 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1021 | MI->dump(); |
| 1022 | exit(1); |
| 1023 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1024 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1025 | break; |
| 1026 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1027 | } |
| 1028 | } |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1029 | O << "\n\t" << TAI->getInlineAsmEnd() << "\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1032 | /// printLabel - This method prints a local label used by debug and |
| 1033 | /// exception handling tables. |
| 1034 | void AsmPrinter::printLabel(const MachineInstr *MI) const { |
Jim Laskey | b82313f | 2007-02-01 16:31:34 +0000 | [diff] [blame] | 1035 | O << "\n" |
| 1036 | << TAI->getPrivateGlobalPrefix() |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1037 | << "debug_loc" |
| 1038 | << MI->getOperand(0).getImmedValue() |
| 1039 | << ":\n"; |
| 1040 | } |
| 1041 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1042 | /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM |
| 1043 | /// instruction, using the specified assembler variant. Targets should |
| 1044 | /// overried this to format as appropriate. |
| 1045 | bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1046 | unsigned AsmVariant, const char *ExtraCode) { |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1047 | // Target doesn't support this yet! |
| 1048 | return true; |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1049 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1050 | |
| 1051 | bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 1052 | unsigned AsmVariant, |
| 1053 | const char *ExtraCode) { |
| 1054 | // Target doesn't support this yet! |
| 1055 | return true; |
| 1056 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1057 | |
| 1058 | /// printBasicBlockLabel - This method prints the label for the specified |
| 1059 | /// MachineBasicBlock |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1060 | void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, |
| 1061 | bool printColon, |
| 1062 | bool printComment) const { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1063 | O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_" |
Nate Begeman | 9d51eeb | 2006-05-02 17:36:46 +0000 | [diff] [blame] | 1064 | << MBB->getNumber(); |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1065 | if (printColon) |
| 1066 | O << ':'; |
Chris Lattner | 9c78ecb | 2006-10-05 21:40:14 +0000 | [diff] [blame] | 1067 | if (printComment && MBB->getBasicBlock()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1068 | O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1069 | } |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1070 | |
| 1071 | /// printSetLabel - This method prints a set label for the specified |
| 1072 | /// MachineBasicBlock |
| 1073 | void AsmPrinter::printSetLabel(unsigned uid, |
| 1074 | const MachineBasicBlock *MBB) const { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1075 | if (!TAI->getSetDirective()) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1076 | return; |
| 1077 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1078 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
| 1079 | << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1080 | printBasicBlockLabel(MBB, false, false); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1081 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1082 | << '_' << uid << '\n'; |
| 1083 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1084 | |
Evan Cheng | 41349c1 | 2006-11-01 09:23:08 +0000 | [diff] [blame] | 1085 | void AsmPrinter::printSetLabel(unsigned uid, unsigned uid2, |
| 1086 | const MachineBasicBlock *MBB) const { |
| 1087 | if (!TAI->getSetDirective()) |
| 1088 | return; |
| 1089 | |
| 1090 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
| 1091 | << getFunctionNumber() << '_' << uid << '_' << uid2 |
| 1092 | << "_set_" << MBB->getNumber() << ','; |
| 1093 | printBasicBlockLabel(MBB, false, false); |
| 1094 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 1095 | << '_' << uid << '_' << uid2 << '\n'; |
| 1096 | } |
| 1097 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1098 | /// printDataDirective - This method prints the asm directive for the |
| 1099 | /// specified type. |
| 1100 | void AsmPrinter::printDataDirective(const Type *type) { |
| 1101 | const TargetData *TD = TM.getTargetData(); |
| 1102 | switch (type->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1103 | case Type::IntegerTyID: { |
| 1104 | unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); |
| 1105 | if (BitWidth <= 8) |
| 1106 | O << TAI->getData8bitsDirective(); |
| 1107 | else if (BitWidth <= 16) |
| 1108 | O << TAI->getData16bitsDirective(); |
| 1109 | else if (BitWidth <= 32) |
| 1110 | O << TAI->getData32bitsDirective(); |
| 1111 | else if (BitWidth <= 64) { |
| 1112 | assert(TAI->getData64bitsDirective() && |
| 1113 | "Target cannot handle 64-bit constant exprs!"); |
| 1114 | O << TAI->getData64bitsDirective(); |
| 1115 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1116 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1117 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1118 | case Type::PointerTyID: |
| 1119 | if (TD->getPointerSize() == 8) { |
| 1120 | assert(TAI->getData64bitsDirective() && |
| 1121 | "Target cannot handle 64-bit pointer exprs!"); |
| 1122 | O << TAI->getData64bitsDirective(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1123 | } else { |
| 1124 | O << TAI->getData32bitsDirective(); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1125 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1126 | break; |
| 1127 | case Type::FloatTyID: case Type::DoubleTyID: |
| 1128 | assert (0 && "Should have already output floating point constant."); |
| 1129 | default: |
| 1130 | assert (0 && "Can't handle printing this type of thing"); |
| 1131 | break; |
| 1132 | } |
| 1133 | } |