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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 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" |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/GCMetadataPrinter.h" |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Mangler.h" |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | bdc679d | 2006-11-29 00:39:47 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Streams.h" |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetAsmInfo.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 0336fdb | 2006-10-06 22:50:56 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetLowering.h" |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | 6547e40 | 2008-07-01 23:18:29 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetRegisterInfo.h" |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 34 | #include <cerrno> |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 37 | char AsmPrinter::ID = 0; |
Jim Laskey | a0f3d17 | 2006-09-07 22:06:40 +0000 | [diff] [blame] | 38 | AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm, |
| 39 | const TargetAsmInfo *T) |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 40 | : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), |
| 41 | TM(tm), TAI(T), TRI(tm.getRegisterInfo()), |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 42 | IsInTextSection(false) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 43 | {} |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 44 | |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 45 | AsmPrinter::~AsmPrinter() { |
| 46 | for (gcp_iterator I = GCMetadataPrinters.begin(), |
| 47 | E = GCMetadataPrinters.end(); I != E; ++I) |
| 48 | delete I->second; |
| 49 | } |
| 50 | |
Chris Lattner | 52f0670 | 2006-10-05 02:42:47 +0000 | [diff] [blame] | 51 | std::string AsmPrinter::getSectionForFunction(const Function &F) const { |
| 52 | return TAI->getTextSection(); |
| 53 | } |
| 54 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 56 | /// SwitchToTextSection - Switch to the specified text section of the executable |
| 57 | /// if we are not already in it! |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 58 | /// |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 59 | void AsmPrinter::SwitchToTextSection(const char *NewSection, |
| 60 | const GlobalValue *GV) { |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 61 | std::string NS; |
Chris Lattner | a7090ae | 2006-05-09 05:24:50 +0000 | [diff] [blame] | 62 | if (GV && GV->hasSection()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 63 | NS = TAI->getSwitchToSectionDirective() + GV->getSection(); |
Chris Lattner | a7090ae | 2006-05-09 05:24:50 +0000 | [diff] [blame] | 64 | else |
| 65 | NS = NewSection; |
| 66 | |
| 67 | // If we're already in this section, we're done. |
| 68 | if (CurrentSection == NS) return; |
Jeff Cohen | 51b776d | 2006-05-02 03:58:45 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 70 | // Close the current section, if applicable. |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 71 | if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 72 | O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; |
Jeff Cohen | 51b776d | 2006-05-02 03:58:45 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 74 | CurrentSection = NS; |
| 75 | |
| 76 | if (!CurrentSection.empty()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 77 | O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n'; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 78 | |
| 79 | IsInTextSection = true; |
Chris Lattner | ac28fbd | 2005-11-21 07:06:27 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 82 | /// SwitchToDataSection - Switch to the specified data section of the executable |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 83 | /// if we are not already in it! |
| 84 | /// |
| 85 | void AsmPrinter::SwitchToDataSection(const char *NewSection, |
| 86 | const GlobalValue *GV) { |
| 87 | std::string NS; |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 88 | if (GV && GV->hasSection()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 89 | NS = TAI->getSwitchToSectionDirective() + GV->getSection(); |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 90 | else |
| 91 | NS = NewSection; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 92 | |
Chris Lattner | b81cb61 | 2006-05-09 05:23:12 +0000 | [diff] [blame] | 93 | // If we're already in this section, we're done. |
| 94 | if (CurrentSection == NS) return; |
| 95 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 96 | // Close the current section, if applicable. |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 97 | if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 98 | O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 99 | |
| 100 | CurrentSection = NS; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 7faec9b | 2006-05-09 05:33:48 +0000 | [diff] [blame] | 102 | if (!CurrentSection.empty()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 103 | O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n'; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 104 | |
| 105 | IsInTextSection = false; |
Chris Lattner | 4632d7a | 2006-05-09 04:59:56 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 109 | void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { |
| 110 | MachineFunctionPass::getAnalysisUsage(AU); |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 111 | AU.addRequired<GCModuleInfo>(); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 114 | bool AsmPrinter::doInitialization(Module &M) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 115 | Mang = new Mangler(M, TAI->getGlobalPrefix()); |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 116 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 117 | GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>(); |
| 118 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
| 119 | for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) |
| 120 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) |
| 121 | MP->beginAssembly(O, *this, *TAI); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 123 | if (!M.getModuleInlineAsm().empty()) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 124 | O << TAI->getCommentString() << " Start of file scope inline assembly\n" |
Chris Lattner | 3e2fa7a | 2006-01-24 04:16:34 +0000 | [diff] [blame] | 125 | << M.getModuleInlineAsm() |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 126 | << '\n' << TAI->getCommentString() |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 127 | << " End of file scope inline assembly\n"; |
Chris Lattner | 2c1b159 | 2006-01-23 23:47:53 +0000 | [diff] [blame] | 128 | |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 129 | SwitchToDataSection(""); // Reset back to no section. |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 130 | |
Evan Cheng | 4e3f5a4 | 2008-02-04 23:06:48 +0000 | [diff] [blame] | 131 | MMI = getAnalysisToUpdate<MachineModuleInfo>(); |
| 132 | if (MMI) MMI->AnalyzeModule(M); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 133 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
| 137 | bool AsmPrinter::doFinalization(Module &M) { |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 138 | if (TAI->getWeakRefDirective()) { |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 139 | if (!ExtWeakSymbols.empty()) |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 140 | SwitchToDataSection(""); |
| 141 | |
| 142 | for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(), |
| 143 | e = ExtWeakSymbols.end(); i != e; ++i) { |
| 144 | const GlobalValue *GV = *i; |
| 145 | std::string Name = Mang->getValueName(GV); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 146 | O << TAI->getWeakRefDirective() << Name << '\n'; |
Rafael Espindola | 15404d0 | 2006-12-18 03:37:18 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 150 | if (TAI->getSetDirective()) { |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 151 | if (!M.alias_empty()) |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 152 | SwitchToTextSection(TAI->getTextSection()); |
| 153 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 154 | O << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 155 | for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 156 | I!=E; ++I) { |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 157 | std::string Name = Mang->getValueName(I); |
| 158 | std::string Target; |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 159 | |
| 160 | const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); |
| 161 | Target = Mang->getValueName(GV); |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 162 | |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 163 | if (I->hasExternalLinkage() || !TAI->getWeakRefDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 164 | O << "\t.globl\t" << Name << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 165 | else if (I->hasWeakLinkage()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 166 | O << TAI->getWeakRefDirective() << Name << '\n'; |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 167 | else if (!I->hasInternalLinkage()) |
| 168 | assert(0 && "Invalid alias linkage"); |
Anton Korobeynikov | 22c9e65 | 2008-03-11 21:41:14 +0000 | [diff] [blame] | 169 | |
| 170 | if (I->hasHiddenVisibility()) { |
| 171 | if (const char *Directive = TAI->getHiddenDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 172 | O << Directive << Name << '\n'; |
Anton Korobeynikov | 22c9e65 | 2008-03-11 21:41:14 +0000 | [diff] [blame] | 173 | } else if (I->hasProtectedVisibility()) { |
| 174 | if (const char *Directive = TAI->getProtectedDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 175 | O << Directive << Name << '\n'; |
Anton Korobeynikov | 22c9e65 | 2008-03-11 21:41:14 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 178 | O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n'; |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 179 | |
| 180 | // If the aliasee has external weak linkage it can be referenced only by |
| 181 | // alias itself. In this case it can be not in ExtWeakSymbols list. Emit |
| 182 | // weak reference in such case. |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 183 | if (GV->hasExternalWeakLinkage()) { |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 184 | if (TAI->getWeakRefDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 185 | O << TAI->getWeakRefDirective() << Target << '\n'; |
Anton Korobeynikov | 325be7c | 2007-09-06 17:21:48 +0000 | [diff] [blame] | 186 | else |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 187 | O << "\t.globl\t" << Target << '\n'; |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 188 | } |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 192 | GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>(); |
| 193 | assert(MI && "AsmPrinter didn't require GCModuleInfo?"); |
| 194 | for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) |
| 195 | if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) |
| 196 | MP->finishAssembly(O, *this, *TAI); |
Gordon Henriksen | ce22477 | 2008-01-07 01:30:38 +0000 | [diff] [blame] | 197 | |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 198 | // If we don't have any trampolines, then we don't require stack memory |
| 199 | // to be executable. Some targets have a directive to declare this. |
| 200 | Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); |
| 201 | if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) |
| 202 | if (TAI->getNonexecutableStackDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 203 | O << TAI->getNonexecutableStackDirective() << '\n'; |
Dan Gohman | a779a98 | 2008-05-05 00:28:39 +0000 | [diff] [blame] | 204 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 205 | delete Mang; Mang = 0; |
| 206 | return false; |
| 207 | } |
| 208 | |
Bill Wendling | ce61328 | 2007-09-18 09:10:16 +0000 | [diff] [blame] | 209 | std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) { |
Bill Wendling | 6e19896 | 2007-09-18 01:47:22 +0000 | [diff] [blame] | 210 | assert(MF && "No machine function?"); |
Dale Johannesen | dedf502 | 2008-04-02 20:10:52 +0000 | [diff] [blame] | 211 | std::string Name = MF->getFunction()->getName(); |
| 212 | if (Name.empty()) |
| 213 | Name = Mang->getValueName(MF->getFunction()); |
| 214 | return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix()); |
Bill Wendling | 6e19896 | 2007-09-18 01:47:22 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 217 | void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 218 | // What's my mangled name? |
Chris Lattner | 450de39 | 2005-11-10 18:36:17 +0000 | [diff] [blame] | 219 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 220 | IncrementFunctionNumber(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 223 | /// EmitConstantPool - Print to the current output stream assembly |
| 224 | /// representations of the constants in the constant pool MCP. This is |
| 225 | /// used to print out constants which have been "spilled to memory" by |
| 226 | /// the code generator. |
| 227 | /// |
| 228 | void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | fa77d43 | 2006-02-09 04:22:52 +0000 | [diff] [blame] | 229 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 230 | if (CP.empty()) return; |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 231 | |
| 232 | // Some targets require 4-, 8-, and 16- byte constant literals to be placed |
| 233 | // in special sections. |
| 234 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs; |
| 235 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs; |
| 236 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs; |
| 237 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs; |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 238 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs; |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 239 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 240 | MachineConstantPoolEntry CPE = CP[i]; |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 241 | const Type *Ty = CPE.getType(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 242 | if (TAI->getFourByteConstantSection() && |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 243 | TM.getTargetData()->getABITypeSize(Ty) == 4) |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 244 | FourByteCPs.push_back(std::make_pair(CPE, i)); |
Evan Cheng | 61c958e | 2006-09-07 18:50:20 +0000 | [diff] [blame] | 245 | else if (TAI->getEightByteConstantSection() && |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 246 | TM.getTargetData()->getABITypeSize(Ty) == 8) |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 247 | EightByteCPs.push_back(std::make_pair(CPE, i)); |
Evan Cheng | 61c958e | 2006-09-07 18:50:20 +0000 | [diff] [blame] | 248 | else if (TAI->getSixteenByteConstantSection() && |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 249 | TM.getTargetData()->getABITypeSize(Ty) == 16) |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 250 | SixteenByteCPs.push_back(std::make_pair(CPE, i)); |
| 251 | else |
| 252 | OtherCPs.push_back(std::make_pair(CPE, i)); |
| 253 | } |
| 254 | |
| 255 | unsigned Alignment = MCP->getConstantPoolAlignment(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 256 | EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs); |
| 257 | EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs); |
| 258 | EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(), |
| 259 | SixteenByteCPs); |
| 260 | EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section, |
| 264 | std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) { |
| 265 | if (CP.empty()) return; |
| 266 | |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 267 | SwitchToDataSection(Section); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 268 | EmitAlignment(Alignment); |
| 269 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 270 | O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 271 | << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << ' '; |
Chris Lattner | 65a7bd8 | 2008-08-19 04:44:30 +0000 | [diff] [blame] | 272 | WriteTypeSymbolic(O, CP[i].first.getType(), 0); |
| 273 | O << '\n'; |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 274 | if (CP[i].first.isMachineConstantPoolEntry()) |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 275 | EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal); |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 276 | else |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 277 | EmitGlobalConstant(CP[i].first.Val.ConstVal); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 278 | if (i != e-1) { |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 279 | const Type *Ty = CP[i].first.getType(); |
Evan Cheng | 2d2cec1 | 2006-06-29 00:26:09 +0000 | [diff] [blame] | 280 | unsigned EntSize = |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 281 | TM.getTargetData()->getABITypeSize(Ty); |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 282 | unsigned ValEnd = CP[i].first.getOffset() + EntSize; |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 283 | // Emit inter-object padding for alignment. |
Evan Cheng | d5a99d7 | 2006-09-14 07:35:00 +0000 | [diff] [blame] | 284 | EmitZeros(CP[i+1].first.getOffset()-ValEnd); |
Chris Lattner | 3029f92 | 2006-02-09 04:46:04 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 289 | /// EmitJumpTableInfo - Print assembly representations of the jump tables used |
| 290 | /// by the current function to the current output stream. |
| 291 | /// |
Chris Lattner | 1da31ee | 2006-10-05 03:01:21 +0000 | [diff] [blame] | 292 | void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, |
| 293 | MachineFunction &MF) { |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 294 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 295 | if (JT.empty()) return; |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 296 | |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 297 | bool IsPic = TM.getRelocationModel() == Reloc::PIC_; |
Nate Begeman | 4d9bbdc | 2006-07-27 16:46:58 +0000 | [diff] [blame] | 298 | |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 299 | // Pick the directive to use to print the jump table entries, and switch to |
| 300 | // the appropriate section. |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 301 | TargetLowering *LoweringInfo = TM.getTargetLowering(); |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 302 | |
Anton Korobeynikov | e87f52d | 2008-07-09 13:27:16 +0000 | [diff] [blame] | 303 | const char* JumpTableDataSection = TAI->getJumpTableDataSection(); |
| 304 | const Function *F = MF.getFunction(); |
| 305 | unsigned SectionFlags = TAI->SectionFlagsForGlobal(F); |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 306 | if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) || |
Anton Korobeynikov | e87f52d | 2008-07-09 13:27:16 +0000 | [diff] [blame] | 307 | !JumpTableDataSection || |
| 308 | SectionFlags & SectionFlags::Linkonce) { |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 309 | // In PIC mode, we need to emit the jump table to the same section as the |
| 310 | // function body itself, otherwise the label differences won't make sense. |
Anton Korobeynikov | e87f52d | 2008-07-09 13:27:16 +0000 | [diff] [blame] | 311 | // We should also do if the section name is NULL or function is declared in |
| 312 | // discardable section. |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 313 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 314 | } else { |
Anton Korobeynikov | 24287dd | 2006-12-19 21:04:20 +0000 | [diff] [blame] | 315 | SwitchToDataSection(JumpTableDataSection); |
Nate Begeman | 2f1ae88 | 2006-07-27 01:13:04 +0000 | [diff] [blame] | 316 | } |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 317 | |
| 318 | EmitAlignment(Log2_32(MJTI->getAlignment())); |
Chris Lattner | 0c4e678 | 2006-07-15 01:34:12 +0000 | [diff] [blame] | 319 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 320 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 321 | const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; |
Chris Lattner | 0737188 | 2006-10-28 18:10:06 +0000 | [diff] [blame] | 322 | |
| 323 | // If this jump table was deleted, ignore it. |
| 324 | if (JTBBs.empty()) continue; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 325 | |
| 326 | // For PIC codegen, if possible we want to use the SetDirective to reduce |
| 327 | // the number of relocations the assembler will generate for the jump table. |
| 328 | // Set directives are all printed before the jump table itself. |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 329 | SmallPtrSet<MachineBasicBlock*, 16> EmittedSets; |
Jim Laskey | acd80ac | 2006-12-14 19:17:33 +0000 | [diff] [blame] | 330 | if (TAI->getSetDirective() && IsPic) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 331 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 332 | if (EmittedSets.insert(JTBBs[ii])) |
| 333 | printPICJumpTableSetLabel(i, JTBBs[ii]); |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 335 | // On some targets (e.g. darwin) we want to emit two consequtive labels |
| 336 | // before each jump table. The first label is never referenced, but tells |
| 337 | // the assembler and linker the extents of the jump table object. The |
| 338 | // second label is actually referenced by the code. |
| 339 | if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix()) |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 340 | O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n"; |
Chris Lattner | 393a8ee | 2007-01-18 01:12:56 +0000 | [diff] [blame] | 341 | |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 342 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 343 | << '_' << i << ":\n"; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 344 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 345 | for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 346 | printPICJumpTableEntry(MJTI, JTBBs[ii], i); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 347 | O << '\n'; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 352 | void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, |
| 353 | const MachineBasicBlock *MBB, |
| 354 | unsigned uid) const { |
| 355 | bool IsPic = TM.getRelocationModel() == Reloc::PIC_; |
| 356 | |
| 357 | // Use JumpTableDirective otherwise honor the entry size from the jump table |
| 358 | // info. |
| 359 | const char *JTEntryDirective = TAI->getJumpTableDirective(); |
| 360 | bool HadJTEntryDirective = JTEntryDirective != NULL; |
| 361 | if (!HadJTEntryDirective) { |
| 362 | JTEntryDirective = MJTI->getEntrySize() == 4 ? |
| 363 | TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); |
| 364 | } |
| 365 | |
| 366 | O << JTEntryDirective << ' '; |
| 367 | |
| 368 | // If we have emitted set directives for the jump table entries, print |
| 369 | // them rather than the entries themselves. If we're emitting PIC, then |
| 370 | // emit the table entries as differences between two text section labels. |
| 371 | // If we're emitting non-PIC code, then emit the entries as direct |
| 372 | // references to the target basic blocks. |
| 373 | if (IsPic) { |
| 374 | if (TAI->getSetDirective()) { |
| 375 | O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() |
| 376 | << '_' << uid << "_set_" << MBB->getNumber(); |
| 377 | } else { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 378 | printBasicBlockLabel(MBB, false, false, false); |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 379 | // If the arch uses custom Jump Table directives, don't calc relative to |
| 380 | // JT |
| 381 | if (!HadJTEntryDirective) |
| 382 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" |
| 383 | << getFunctionNumber() << '_' << uid; |
| 384 | } |
| 385 | } else { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 386 | printBasicBlockLabel(MBB, false, false, false); |
Anton Korobeynikov | 9de1934 | 2007-11-14 09:18:41 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
| 390 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 391 | /// EmitSpecialLLVMGlobal - Check to see if the specified global is a |
| 392 | /// special global used by LLVM. If so, emit it and return true, otherwise |
| 393 | /// do nothing and return false. |
| 394 | bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { |
Andrew Lenharth | b753a9b | 2007-08-22 19:33:11 +0000 | [diff] [blame] | 395 | if (GV->getName() == "llvm.used") { |
| 396 | if (TAI->getUsedDirective() != 0) // No need to emit this at all. |
| 397 | EmitLLVMUsedList(GV->getInitializer()); |
| 398 | return true; |
| 399 | } |
| 400 | |
Jim Laskey | 7809811 | 2006-03-07 22:00:35 +0000 | [diff] [blame] | 401 | // Ignore debug and non-emitted data. |
| 402 | if (GV->getSection() == "llvm.metadata") return true; |
| 403 | |
| 404 | if (!GV->hasAppendingLinkage()) return false; |
| 405 | |
| 406 | assert(GV->hasInitializer() && "Not a special LLVM global!"); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 407 | |
Evan Cheng | 916d07c | 2007-06-04 20:39:18 +0000 | [diff] [blame] | 408 | const TargetData *TD = TM.getTargetData(); |
| 409 | unsigned Align = Log2_32(TD->getPointerPrefAlignment()); |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 410 | if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) { |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 411 | SwitchToDataSection(TAI->getStaticCtorsSection()); |
Evan Cheng | 916d07c | 2007-06-04 20:39:18 +0000 | [diff] [blame] | 412 | EmitAlignment(Align, 0); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 413 | EmitXXStructorList(GV->getInitializer()); |
| 414 | return true; |
| 415 | } |
| 416 | |
Chris Lattner | 5166b82 | 2006-01-12 19:17:23 +0000 | [diff] [blame] | 417 | if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) { |
Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 418 | SwitchToDataSection(TAI->getStaticDtorsSection()); |
Evan Cheng | 916d07c | 2007-06-04 20:39:18 +0000 | [diff] [blame] | 419 | EmitAlignment(Align, 0); |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 420 | EmitXXStructorList(GV->getInitializer()); |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | return false; |
| 425 | } |
| 426 | |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 427 | /// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each |
| 428 | /// global in the specified llvm.used list as being used with this directive. |
| 429 | void AsmPrinter::EmitLLVMUsedList(Constant *List) { |
| 430 | const char *Directive = TAI->getUsedDirective(); |
| 431 | |
| 432 | // Should be an array of 'sbyte*'. |
| 433 | ConstantArray *InitList = dyn_cast<ConstantArray>(List); |
| 434 | if (InitList == 0) return; |
| 435 | |
| 436 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { |
| 437 | O << Directive; |
| 438 | EmitConstantValueOnly(InitList->getOperand(i)); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 439 | O << '\n'; |
Chris Lattner | cb05af8 | 2006-09-26 03:38:18 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 443 | /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the |
| 444 | /// function pointers, ignoring the init priority. |
| 445 | void AsmPrinter::EmitXXStructorList(Constant *List) { |
| 446 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 447 | // init priority, which we ignore. |
| 448 | if (!isa<ConstantArray>(List)) return; |
| 449 | ConstantArray *InitList = cast<ConstantArray>(List); |
| 450 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 451 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 452 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
Chris Lattner | 8de324b | 2005-12-21 01:17:37 +0000 | [diff] [blame] | 453 | |
| 454 | if (CS->getOperand(1)->isNullValue()) |
| 455 | return; // Found a null terminator, exit printing. |
| 456 | // Emit the function pointer. |
Chris Lattner | ed13893 | 2005-12-13 06:32:10 +0000 | [diff] [blame] | 457 | EmitGlobalConstant(CS->getOperand(1)); |
| 458 | } |
| 459 | } |
Chris Lattner | 3b4fd32 | 2005-11-21 08:25:09 +0000 | [diff] [blame] | 460 | |
Jim Laskey | a1a19f8 | 2006-10-17 13:41:07 +0000 | [diff] [blame] | 461 | /// getGlobalLinkName - Returns the asm/link name of of the specified |
| 462 | /// global variable. Should be overridden by each target asm printer to |
| 463 | /// generate the appropriate value. |
Jim Laskey | 99e41ee | 2006-10-17 17:17:24 +0000 | [diff] [blame] | 464 | const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{ |
| 465 | std::string LinkName; |
Jim Laskey | 0374248 | 2006-11-20 20:29:06 +0000 | [diff] [blame] | 466 | |
| 467 | if (isa<Function>(GV)) { |
| 468 | LinkName += TAI->getFunctionAddrPrefix(); |
| 469 | LinkName += Mang->getValueName(GV); |
| 470 | LinkName += TAI->getFunctionAddrSuffix(); |
| 471 | } else { |
| 472 | LinkName += TAI->getGlobalVarAddrPrefix(); |
| 473 | LinkName += Mang->getValueName(GV); |
| 474 | LinkName += TAI->getGlobalVarAddrSuffix(); |
| 475 | } |
| 476 | |
Jim Laskey | 99e41ee | 2006-10-17 17:17:24 +0000 | [diff] [blame] | 477 | return LinkName; |
Jim Laskey | a1a19f8 | 2006-10-17 13:41:07 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 480 | /// EmitExternalGlobal - Emit the external reference to a global variable. |
| 481 | /// Should be overridden if an indirect reference should be used. |
| 482 | void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { |
| 483 | O << getGlobalLinkName(GV); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 488 | //===----------------------------------------------------------------------===// |
| 489 | /// LEB 128 number encoding. |
| 490 | |
| 491 | /// PrintULEB128 - Print a series of hexidecimal values (separated by commas) |
| 492 | /// representing an unsigned leb128 value. |
| 493 | void AsmPrinter::PrintULEB128(unsigned Value) const { |
| 494 | do { |
| 495 | unsigned Byte = Value & 0x7f; |
| 496 | Value >>= 7; |
| 497 | if (Value) Byte |= 0x80; |
| 498 | O << "0x" << std::hex << Byte << std::dec; |
| 499 | if (Value) O << ", "; |
| 500 | } while (Value); |
| 501 | } |
| 502 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 503 | /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) |
| 504 | /// representing a signed leb128 value. |
| 505 | void AsmPrinter::PrintSLEB128(int Value) const { |
| 506 | int Sign = Value >> (8 * sizeof(Value) - 1); |
| 507 | bool IsMore; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 508 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 509 | do { |
| 510 | unsigned Byte = Value & 0x7f; |
| 511 | Value >>= 7; |
| 512 | IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; |
| 513 | if (IsMore) Byte |= 0x80; |
| 514 | O << "0x" << std::hex << Byte << std::dec; |
| 515 | if (IsMore) O << ", "; |
| 516 | } while (IsMore); |
| 517 | } |
| 518 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 519 | //===--------------------------------------------------------------------===// |
| 520 | // Emission and print routines |
| 521 | // |
| 522 | |
| 523 | /// PrintHex - Print a value as a hexidecimal value. |
| 524 | /// |
| 525 | void AsmPrinter::PrintHex(int Value) const { |
| 526 | O << "0x" << std::hex << Value << std::dec; |
| 527 | } |
| 528 | |
| 529 | /// EOL - Print a newline character to asm stream. If a comment is present |
| 530 | /// then it will be printed first. Comments should not contain '\n'. |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 531 | void AsmPrinter::EOL() const { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 532 | O << '\n'; |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 533 | } |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 534 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 535 | void AsmPrinter::EOL(const std::string &Comment) const { |
Evan Cheng | 6547e40 | 2008-07-01 23:18:29 +0000 | [diff] [blame] | 536 | if (VerboseAsm && !Comment.empty()) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 537 | O << '\t' |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 538 | << TAI->getCommentString() |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 539 | << ' ' |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 540 | << Comment; |
| 541 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 542 | O << '\n'; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 545 | void AsmPrinter::EOL(const char* Comment) const { |
Evan Cheng | 6547e40 | 2008-07-01 23:18:29 +0000 | [diff] [blame] | 546 | if (VerboseAsm && *Comment) { |
Owen Anderson | 2599509 | 2008-07-01 21:16:27 +0000 | [diff] [blame] | 547 | O << '\t' |
| 548 | << TAI->getCommentString() |
| 549 | << ' ' |
| 550 | << Comment; |
| 551 | } |
| 552 | O << '\n'; |
| 553 | } |
| 554 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 555 | /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an |
| 556 | /// unsigned leb128 value. |
| 557 | void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { |
| 558 | if (TAI->hasLEB128()) { |
| 559 | O << "\t.uleb128\t" |
| 560 | << Value; |
| 561 | } else { |
| 562 | O << TAI->getData8bitsDirective(); |
| 563 | PrintULEB128(Value); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /// EmitSLEB128Bytes - print an assembler byte data directive to compose a |
| 568 | /// signed leb128 value. |
| 569 | void AsmPrinter::EmitSLEB128Bytes(int Value) const { |
| 570 | if (TAI->hasLEB128()) { |
| 571 | O << "\t.sleb128\t" |
| 572 | << Value; |
| 573 | } else { |
| 574 | O << TAI->getData8bitsDirective(); |
| 575 | PrintSLEB128(Value); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | /// EmitInt8 - Emit a byte directive and value. |
| 580 | /// |
| 581 | void AsmPrinter::EmitInt8(int Value) const { |
| 582 | O << TAI->getData8bitsDirective(); |
| 583 | PrintHex(Value & 0xFF); |
| 584 | } |
| 585 | |
| 586 | /// EmitInt16 - Emit a short directive and value. |
| 587 | /// |
| 588 | void AsmPrinter::EmitInt16(int Value) const { |
| 589 | O << TAI->getData16bitsDirective(); |
| 590 | PrintHex(Value & 0xFFFF); |
| 591 | } |
| 592 | |
| 593 | /// EmitInt32 - Emit a long directive and value. |
| 594 | /// |
| 595 | void AsmPrinter::EmitInt32(int Value) const { |
| 596 | O << TAI->getData32bitsDirective(); |
| 597 | PrintHex(Value); |
| 598 | } |
| 599 | |
| 600 | /// EmitInt64 - Emit a long long directive and value. |
| 601 | /// |
| 602 | void AsmPrinter::EmitInt64(uint64_t Value) const { |
| 603 | if (TAI->getData64bitsDirective()) { |
| 604 | O << TAI->getData64bitsDirective(); |
| 605 | PrintHex(Value); |
| 606 | } else { |
| 607 | if (TM.getTargetData()->isBigEndian()) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 608 | EmitInt32(unsigned(Value >> 32)); O << '\n'; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 609 | EmitInt32(unsigned(Value)); |
| 610 | } else { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 611 | EmitInt32(unsigned(Value)); O << '\n'; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 612 | EmitInt32(unsigned(Value >> 32)); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 618 | /// |
| 619 | static inline char toOctal(int X) { |
| 620 | return (X&7)+'0'; |
| 621 | } |
| 622 | |
| 623 | /// printStringChar - Print a char, escaped if necessary. |
| 624 | /// |
| 625 | static void printStringChar(std::ostream &O, unsigned char C) { |
| 626 | if (C == '"') { |
| 627 | O << "\\\""; |
| 628 | } else if (C == '\\') { |
| 629 | O << "\\\\"; |
| 630 | } else if (isprint(C)) { |
| 631 | O << C; |
| 632 | } else { |
| 633 | switch(C) { |
| 634 | case '\b': O << "\\b"; break; |
| 635 | case '\f': O << "\\f"; break; |
| 636 | case '\n': O << "\\n"; break; |
| 637 | case '\r': O << "\\r"; break; |
| 638 | case '\t': O << "\\t"; break; |
| 639 | default: |
| 640 | O << '\\'; |
| 641 | O << toOctal(C >> 6); |
| 642 | O << toOctal(C >> 3); |
| 643 | O << toOctal(C >> 0); |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | /// EmitString - Emit a string with quotes and a null terminator. |
| 650 | /// Special characters are emitted properly. |
| 651 | /// \literal (Eg. '\t') \endliteral |
| 652 | void AsmPrinter::EmitString(const std::string &String) const { |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 653 | const char* AscizDirective = TAI->getAscizDirective(); |
| 654 | if (AscizDirective) |
| 655 | O << AscizDirective; |
| 656 | else |
| 657 | O << TAI->getAsciiDirective(); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 658 | O << '\"'; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 659 | for (unsigned i = 0, N = String.size(); i < N; ++i) { |
| 660 | unsigned char C = String[i]; |
| 661 | printStringChar(O, C); |
| 662 | } |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 663 | if (AscizDirective) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 664 | O << '\"'; |
Anton Korobeynikov | fb269cf | 2007-03-06 19:25:02 +0000 | [diff] [blame] | 665 | else |
| 666 | O << "\\0\""; |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | |
Dan Gohman | 189f80d | 2007-09-24 20:58:13 +0000 | [diff] [blame] | 670 | /// EmitFile - Emit a .file directive. |
| 671 | void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const { |
| 672 | O << "\t.file\t" << Number << " \""; |
| 673 | for (unsigned i = 0, N = Name.size(); i < N; ++i) { |
| 674 | unsigned char C = Name[i]; |
| 675 | printStringChar(O, C); |
| 676 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 677 | O << '\"'; |
Dan Gohman | 189f80d | 2007-09-24 20:58:13 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 681 | //===----------------------------------------------------------------------===// |
| 682 | |
Chris Lattner | 3a42053 | 2007-05-31 18:57:45 +0000 | [diff] [blame] | 683 | // EmitAlignment - Emit an alignment directive to the specified power of |
| 684 | // two boundary. For example, if you pass in 3 here, you will get an 8 |
| 685 | // byte alignment. If a global value is specified, and if that global has |
| 686 | // an explicit alignment requested, it will unconditionally override the |
| 687 | // alignment request. However, if ForcedAlignBits is specified, this value |
| 688 | // has final say: the ultimate alignment will be the max of ForcedAlignBits |
| 689 | // and the alignment computed with NumBits and the global. |
| 690 | // |
| 691 | // The algorithm is: |
| 692 | // Align = NumBits; |
| 693 | // if (GV && GV->hasalignment) Align = GV->getalignment(); |
| 694 | // Align = std::max(Align, ForcedAlignBits); |
| 695 | // |
| 696 | void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, |
Evan Cheng | 05548eb | 2008-02-29 19:36:59 +0000 | [diff] [blame] | 697 | unsigned ForcedAlignBits, |
| 698 | bool UseFillExpr) const { |
Dale Johannesen | 00d56b9 | 2007-04-23 23:33:31 +0000 | [diff] [blame] | 699 | if (GV && GV->getAlignment()) |
Chris Lattner | 3a42053 | 2007-05-31 18:57:45 +0000 | [diff] [blame] | 700 | NumBits = Log2_32(GV->getAlignment()); |
| 701 | NumBits = std::max(NumBits, ForcedAlignBits); |
| 702 | |
Chris Lattner | 2a21c6e | 2005-11-10 18:09:27 +0000 | [diff] [blame] | 703 | if (NumBits == 0) return; // No need to emit alignment. |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 704 | if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits; |
Evan Cheng | 73a259a | 2007-07-25 23:35:07 +0000 | [diff] [blame] | 705 | O << TAI->getAlignDirective() << NumBits; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 706 | |
| 707 | unsigned FillValue = TAI->getTextAlignFillValue(); |
Evan Cheng | 05548eb | 2008-02-29 19:36:59 +0000 | [diff] [blame] | 708 | UseFillExpr &= IsInTextSection && FillValue; |
Evan Cheng | 73a259a | 2007-07-25 23:35:07 +0000 | [diff] [blame] | 709 | if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec; |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 710 | O << '\n'; |
Chris Lattner | bfddc20 | 2004-08-17 19:14:29 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Jim Laskey | bda9b0e | 2007-02-21 22:47:38 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 714 | /// EmitZeros - Emit a block of zeros. |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 715 | /// |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 716 | void AsmPrinter::EmitZeros(uint64_t NumZeros) const { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 717 | if (NumZeros) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 718 | if (TAI->getZeroDirective()) { |
| 719 | O << TAI->getZeroDirective() << NumZeros; |
| 720 | if (TAI->getZeroDirectiveSuffix()) |
| 721 | O << TAI->getZeroDirectiveSuffix(); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 722 | O << '\n'; |
Jeff Cohen | c6a057b | 2006-05-02 03:46:13 +0000 | [diff] [blame] | 723 | } else { |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 724 | for (; NumZeros; --NumZeros) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 725 | O << TAI->getData8bitsDirective() << "0\n"; |
Chris Lattner | 7d057a3 | 2004-08-17 21:38:40 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | } |
| 729 | |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 730 | // Print out the specified constant, without a storage class. Only the |
| 731 | // constants valid in constant expressions can occur here. |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 732 | void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 733 | if (CV->isNullValue() || isa<UndefValue>(CV)) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 734 | O << '0'; |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 735 | else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Scott Michel | 4315eee | 2008-06-03 06:18:19 +0000 | [diff] [blame] | 736 | O << CI->getZExtValue(); |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 737 | } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 738 | // This is a constant address for a global variable or function. Use the |
| 739 | // name of the variable or function as the address value, possibly |
| 740 | // decorating it with GlobalVarAddrPrefix/Suffix or |
| 741 | // FunctionAddrPrefix/Suffix (these all default to "" ) |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 742 | if (isa<Function>(GV)) { |
| 743 | O << TAI->getFunctionAddrPrefix() |
| 744 | << Mang->getValueName(GV) |
| 745 | << TAI->getFunctionAddrSuffix(); |
| 746 | } else { |
| 747 | O << TAI->getGlobalVarAddrPrefix() |
| 748 | << Mang->getValueName(GV) |
| 749 | << TAI->getGlobalVarAddrSuffix(); |
| 750 | } |
Duraid Madina | 855a519 | 2005-04-02 12:21:51 +0000 | [diff] [blame] | 751 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 752 | const TargetData *TD = TM.getTargetData(); |
Anton Korobeynikov | 13b7d3d | 2007-02-04 23:27:42 +0000 | [diff] [blame] | 753 | unsigned Opcode = CE->getOpcode(); |
| 754 | switch (Opcode) { |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 755 | case Instruction::GetElementPtr: { |
| 756 | // generate a symbolic expression for the byte address |
| 757 | const Constant *ptrVal = CE->getOperand(0); |
Chris Lattner | 7f6b9d2 | 2007-02-10 20:31:59 +0000 | [diff] [blame] | 758 | SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); |
| 759 | if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], |
| 760 | idxVec.size())) { |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 761 | if (Offset) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 762 | O << '('; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 763 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | 27e1921 | 2005-02-14 21:40:26 +0000 | [diff] [blame] | 764 | if (Offset > 0) |
| 765 | O << ") + " << Offset; |
| 766 | else if (Offset < 0) |
| 767 | O << ") - " << -Offset; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 768 | } else { |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 769 | EmitConstantValueOnly(ptrVal); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 770 | } |
| 771 | break; |
| 772 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 773 | case Instruction::Trunc: |
| 774 | case Instruction::ZExt: |
| 775 | case Instruction::SExt: |
| 776 | case Instruction::FPTrunc: |
| 777 | case Instruction::FPExt: |
| 778 | case Instruction::UIToFP: |
| 779 | case Instruction::SIToFP: |
| 780 | case Instruction::FPToUI: |
| 781 | case Instruction::FPToSI: |
| 782 | assert(0 && "FIXME: Don't yet support this kind of constant cast expr"); |
| 783 | break; |
Chris Lattner | cb0a681 | 2006-12-12 05:14:13 +0000 | [diff] [blame] | 784 | case Instruction::BitCast: |
| 785 | return EmitConstantValueOnly(CE->getOperand(0)); |
| 786 | |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 787 | case Instruction::IntToPtr: { |
| 788 | // Handle casts to pointers by changing them into casts to the appropriate |
| 789 | // integer type. This promotes constant folding and simplifies this code. |
| 790 | Constant *Op = CE->getOperand(0); |
| 791 | Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/); |
| 792 | return EmitConstantValueOnly(Op); |
| 793 | } |
| 794 | |
| 795 | |
| 796 | case Instruction::PtrToInt: { |
Chris Lattner | 4a6bd33 | 2006-07-29 01:57:19 +0000 | [diff] [blame] | 797 | // Support only foldable casts to/from pointers that can be eliminated by |
| 798 | // changing the pointer to the appropriately sized integer type. |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 799 | Constant *Op = CE->getOperand(0); |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 800 | const Type *Ty = CE->getType(); |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 801 | |
Chris Lattner | ddc9401 | 2006-12-12 05:18:19 +0000 | [diff] [blame] | 802 | // We can emit the pointer value into this slot if the slot is an |
| 803 | // integer slot greater or equal to the size of the pointer. |
Nick Lewycky | d622738 | 2008-08-08 06:34:07 +0000 | [diff] [blame] | 804 | if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType())) |
Chris Lattner | 4a6bd33 | 2006-07-29 01:57:19 +0000 | [diff] [blame] | 805 | return EmitConstantValueOnly(Op); |
Nick Lewycky | d622738 | 2008-08-08 06:34:07 +0000 | [diff] [blame] | 806 | |
| 807 | O << "(("; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 808 | EmitConstantValueOnly(Op); |
Nick Lewycky | d622738 | 2008-08-08 06:34:07 +0000 | [diff] [blame] | 809 | APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty)); |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 810 | |
| 811 | SmallString<40> S; |
| 812 | ptrMask.toStringUnsigned(S); |
| 813 | O << ") & " << S.c_str() << ')'; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 814 | break; |
| 815 | } |
| 816 | case Instruction::Add: |
Anton Korobeynikov | 13b7d3d | 2007-02-04 23:27:42 +0000 | [diff] [blame] | 817 | case Instruction::Sub: |
Anton Korobeynikov | feb8893 | 2007-12-18 20:53:41 +0000 | [diff] [blame] | 818 | case Instruction::And: |
| 819 | case Instruction::Or: |
| 820 | case Instruction::Xor: |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 821 | O << '('; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 822 | EmitConstantValueOnly(CE->getOperand(0)); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 823 | O << ')'; |
Anton Korobeynikov | feb8893 | 2007-12-18 20:53:41 +0000 | [diff] [blame] | 824 | switch (Opcode) { |
| 825 | case Instruction::Add: |
| 826 | O << " + "; |
| 827 | break; |
| 828 | case Instruction::Sub: |
| 829 | O << " - "; |
| 830 | break; |
| 831 | case Instruction::And: |
| 832 | O << " & "; |
| 833 | break; |
| 834 | case Instruction::Or: |
| 835 | O << " | "; |
| 836 | break; |
| 837 | case Instruction::Xor: |
| 838 | O << " ^ "; |
| 839 | break; |
| 840 | default: |
| 841 | break; |
| 842 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 843 | O << '('; |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 844 | EmitConstantValueOnly(CE->getOperand(1)); |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 845 | O << ')'; |
Chris Lattner | a80ba71 | 2004-08-16 23:15:22 +0000 | [diff] [blame] | 846 | break; |
| 847 | default: |
| 848 | assert(0 && "Unsupported operator!"); |
| 849 | } |
| 850 | } else { |
| 851 | assert(0 && "Unknown constant value!"); |
| 852 | } |
| 853 | } |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 855 | /// printAsCString - Print the specified array as a C compatible string, only if |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 856 | /// the predicate isString is true. |
| 857 | /// |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 858 | static void printAsCString(std::ostream &O, const ConstantArray *CVA, |
| 859 | unsigned LastElt) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 860 | assert(CVA->isString() && "Array is not string compatible!"); |
| 861 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 862 | O << '\"'; |
Chris Lattner | 2980cef | 2005-11-10 18:06:33 +0000 | [diff] [blame] | 863 | for (unsigned i = 0; i != LastElt; ++i) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 864 | unsigned char C = |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 865 | (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); |
Jim Laskey | f1cdea1 | 2007-01-25 15:12:02 +0000 | [diff] [blame] | 866 | printStringChar(O, C); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 867 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 868 | O << '\"'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 871 | /// EmitString - Emit a zero-byte-terminated string constant. |
| 872 | /// |
| 873 | void AsmPrinter::EmitString(const ConstantArray *CVA) const { |
| 874 | unsigned NumElts = CVA->getNumOperands(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 875 | if (TAI->getAscizDirective() && NumElts && |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 876 | cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 877 | O << TAI->getAscizDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 878 | printAsCString(O, CVA, NumElts-1); |
| 879 | } else { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 880 | O << TAI->getAsciiDirective(); |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 881 | printAsCString(O, CVA, NumElts); |
| 882 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 883 | O << '\n'; |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 886 | /// EmitGlobalConstant - Print a general LLVM constant to the .s file. |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 887 | void AsmPrinter::EmitGlobalConstant(const Constant *CV) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 888 | const TargetData *TD = TM.getTargetData(); |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 889 | unsigned Size = TD->getABITypeSize(CV->getType()); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 890 | |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 891 | if (CV->isNullValue() || isa<UndefValue>(CV)) { |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 892 | EmitZeros(Size); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 893 | return; |
| 894 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 895 | if (CVA->isString()) { |
Jeff Cohen | c884db4 | 2006-05-02 01:16:28 +0000 | [diff] [blame] | 896 | EmitString(CVA); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 897 | } else { // Not a string. Print the values in successive locations |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 898 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 899 | EmitGlobalConstant(CVA->getOperand(i)); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 900 | } |
| 901 | return; |
| 902 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 903 | // Print the fields in successive locations. Pad to align if needed! |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 904 | const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); |
Chris Lattner | dea18b6 | 2005-01-08 19:59:10 +0000 | [diff] [blame] | 905 | uint64_t sizeSoFar = 0; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 906 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 907 | const Constant* field = CVS->getOperand(i); |
| 908 | |
| 909 | // Check if padding is needed and insert one or more 0s. |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 910 | uint64_t fieldSize = TD->getABITypeSize(field->getType()); |
Duncan Sands | 0c8a13b | 2007-11-05 18:03:02 +0000 | [diff] [blame] | 911 | uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 912 | - cvsLayout->getElementOffset(i)) - fieldSize; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 913 | sizeSoFar += fieldSize + padSize; |
| 914 | |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 915 | // Now print the actual field value. |
| 916 | EmitGlobalConstant(field); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 917 | |
Duncan Sands | 0c8a13b | 2007-11-05 18:03:02 +0000 | [diff] [blame] | 918 | // Insert padding - this may include padding to increase the size of the |
| 919 | // current field up to the ABI size (if the struct is not packed) as well |
| 920 | // as padding to ensure that the next field starts at the right offset. |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 921 | EmitZeros(padSize); |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 922 | } |
Chris Lattner | b0c39a3 | 2007-02-10 19:59:22 +0000 | [diff] [blame] | 923 | assert(sizeSoFar == cvsLayout->getSizeInBytes() && |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 924 | "Layout of constant struct may be incorrect!"); |
| 925 | return; |
| 926 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 927 | // FP Constants are printed as integer constants to avoid losing |
| 928 | // precision... |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 929 | if (CFP->getType() == Type::DoubleTy) { |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 930 | double Val = CFP->getValueAPF().convertToDouble(); // for comment only |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 931 | uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue(); |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 932 | if (TAI->getData64bitsDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 933 | O << TAI->getData64bitsDirective() << i << '\t' |
| 934 | << TAI->getCommentString() << " double value: " << Val << '\n'; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 935 | else if (TD->isBigEndian()) { |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 936 | O << TAI->getData32bitsDirective() << unsigned(i >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 937 | << '\t' << TAI->getCommentString() |
| 938 | << " double most significant word " << Val << '\n'; |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 939 | O << TAI->getData32bitsDirective() << unsigned(i) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 940 | << '\t' << TAI->getCommentString() |
| 941 | << " double least significant word " << Val << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 942 | } else { |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 943 | O << TAI->getData32bitsDirective() << unsigned(i) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 944 | << '\t' << TAI->getCommentString() |
| 945 | << " double least significant word " << Val << '\n'; |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 946 | O << TAI->getData32bitsDirective() << unsigned(i >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 947 | << '\t' << TAI->getCommentString() |
| 948 | << " double most significant word " << Val << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 949 | } |
| 950 | return; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 951 | } else if (CFP->getType() == Type::FloatTy) { |
Dale Johannesen | 3f6eb74 | 2007-09-11 18:32:33 +0000 | [diff] [blame] | 952 | float Val = CFP->getValueAPF().convertToFloat(); // for comment only |
| 953 | O << TAI->getData32bitsDirective() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 954 | << CFP->getValueAPF().convertToAPInt().getZExtValue() |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 955 | << '\t' << TAI->getCommentString() << " float " << Val << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 956 | return; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 957 | } else if (CFP->getType() == Type::X86_FP80Ty) { |
| 958 | // all long double variants are printed as hex |
Dale Johannesen | 693717f | 2007-09-26 23:20:33 +0000 | [diff] [blame] | 959 | // api needed to prevent premature destruction |
| 960 | APInt api = CFP->getValueAPF().convertToAPInt(); |
| 961 | const uint64_t *p = api.getRawData(); |
Chris Lattner | 553c116 | 2008-01-27 06:09:28 +0000 | [diff] [blame] | 962 | APFloat DoubleVal = CFP->getValueAPF(); |
| 963 | DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven); |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 964 | if (TD->isBigEndian()) { |
| 965 | O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 966 | << '\t' << TAI->getCommentString() |
Chris Lattner | 553c116 | 2008-01-27 06:09:28 +0000 | [diff] [blame] | 967 | << " long double most significant halfword of ~" |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 968 | << DoubleVal.convertToDouble() << '\n'; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 969 | O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 970 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 971 | << " long double next halfword\n"; |
| 972 | O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 973 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 974 | << " long double next halfword\n"; |
| 975 | O << TAI->getData16bitsDirective() << uint16_t(p[0]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 976 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 977 | << " long double next halfword\n"; |
| 978 | O << TAI->getData16bitsDirective() << uint16_t(p[1]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 979 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 980 | << " long double least significant halfword\n"; |
| 981 | } else { |
| 982 | O << TAI->getData16bitsDirective() << uint16_t(p[1]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 983 | << '\t' << TAI->getCommentString() |
Chris Lattner | 553c116 | 2008-01-27 06:09:28 +0000 | [diff] [blame] | 984 | << " long double least significant halfword of ~" |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 985 | << DoubleVal.convertToDouble() << '\n'; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 986 | O << TAI->getData16bitsDirective() << uint16_t(p[0]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 987 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 988 | << " long double next halfword\n"; |
| 989 | O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 990 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 991 | << " long double next halfword\n"; |
| 992 | O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 993 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 994 | << " long double next halfword\n"; |
| 995 | O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 996 | << '\t' << TAI->getCommentString() |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 997 | << " long double most significant halfword\n"; |
| 998 | } |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 999 | EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty)); |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1000 | return; |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1001 | } else if (CFP->getType() == Type::PPC_FP128Ty) { |
| 1002 | // all long double variants are printed as hex |
| 1003 | // api needed to prevent premature destruction |
| 1004 | APInt api = CFP->getValueAPF().convertToAPInt(); |
| 1005 | const uint64_t *p = api.getRawData(); |
| 1006 | if (TD->isBigEndian()) { |
| 1007 | O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1008 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1009 | << " long double most significant word\n"; |
| 1010 | O << TAI->getData32bitsDirective() << uint32_t(p[0]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1011 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1012 | << " long double next word\n"; |
| 1013 | O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1014 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1015 | << " long double next word\n"; |
| 1016 | O << TAI->getData32bitsDirective() << uint32_t(p[1]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1017 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1018 | << " long double least significant word\n"; |
| 1019 | } else { |
| 1020 | O << TAI->getData32bitsDirective() << uint32_t(p[1]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1021 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1022 | << " long double least significant word\n"; |
| 1023 | O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1024 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1025 | << " long double next word\n"; |
| 1026 | O << TAI->getData32bitsDirective() << uint32_t(p[0]) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1027 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1028 | << " long double next word\n"; |
| 1029 | O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1030 | << '\t' << TAI->getCommentString() |
Dale Johannesen | fcf4d24 | 2007-10-11 23:32:15 +0000 | [diff] [blame] | 1031 | << " long double most significant word\n"; |
| 1032 | } |
| 1033 | return; |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 1034 | } else assert(0 && "Floating point constant type not handled"); |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 1035 | } else if (CV->getType() == Type::Int64Ty) { |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1036 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1037 | uint64_t Val = CI->getZExtValue(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 1038 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1039 | if (TAI->getData64bitsDirective()) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1040 | O << TAI->getData64bitsDirective() << Val << '\n'; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 1041 | else if (TD->isBigEndian()) { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1042 | O << TAI->getData32bitsDirective() << unsigned(Val >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1043 | << '\t' << TAI->getCommentString() |
| 1044 | << " Double-word most significant word " << Val << '\n'; |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1045 | O << TAI->getData32bitsDirective() << unsigned(Val) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1046 | << '\t' << TAI->getCommentString() |
| 1047 | << " Double-word least significant word " << Val << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1048 | } else { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1049 | O << TAI->getData32bitsDirective() << unsigned(Val) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1050 | << '\t' << TAI->getCommentString() |
| 1051 | << " Double-word least significant word " << Val << '\n'; |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1052 | O << TAI->getData32bitsDirective() << unsigned(Val >> 32) |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1053 | << '\t' << TAI->getCommentString() |
| 1054 | << " Double-word most significant word " << Val << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1055 | } |
| 1056 | return; |
| 1057 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1058 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { |
| 1059 | const VectorType *PTy = CP->getType(); |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 1060 | |
| 1061 | for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) |
Duncan Sands | a0fcc08 | 2008-06-04 08:21:45 +0000 | [diff] [blame] | 1062 | EmitGlobalConstant(CP->getOperand(I)); |
Nate Begeman | 8cfa57b | 2005-12-06 06:18:55 +0000 | [diff] [blame] | 1063 | |
| 1064 | return; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | const Type *type = CV->getType(); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1068 | printDataDirective(type); |
Chris Lattner | 25045bd | 2005-11-21 07:51:36 +0000 | [diff] [blame] | 1069 | EmitConstantValueOnly(CV); |
Scott Michel | eefc845 | 2008-06-03 15:39:51 +0000 | [diff] [blame] | 1070 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 1071 | SmallString<40> S; |
| 1072 | CI->getValue().toStringUnsigned(S, 16); |
| 1073 | O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str(); |
Scott Michel | eefc845 | 2008-06-03 15:39:51 +0000 | [diff] [blame] | 1074 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1075 | O << '\n'; |
Chris Lattner | 1b7e235 | 2004-08-17 06:36:49 +0000 | [diff] [blame] | 1076 | } |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | fad86b0 | 2008-08-17 07:19:36 +0000 | [diff] [blame] | 1078 | void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1079 | // Target doesn't support this yet! |
| 1080 | abort(); |
| 1081 | } |
| 1082 | |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1083 | /// PrintSpecial - Print information related to the specified machine instr |
| 1084 | /// that is independent of the operand, and may be independent of the instr |
| 1085 | /// itself. This can be useful for portably encoding the comment character |
| 1086 | /// or other bits of target-specific knowledge into the asmstrings. The |
| 1087 | /// syntax used is ${:comment}. Targets can override this to add support |
| 1088 | /// for their own strange codes. |
| 1089 | void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) { |
Chris Lattner | bae02cf | 2006-09-27 00:06:07 +0000 | [diff] [blame] | 1090 | if (!strcmp(Code, "private")) { |
| 1091 | O << TAI->getPrivateGlobalPrefix(); |
| 1092 | } else if (!strcmp(Code, "comment")) { |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1093 | O << TAI->getCommentString(); |
| 1094 | } else if (!strcmp(Code, "uid")) { |
| 1095 | // Assign a unique ID to this machine instruction. |
| 1096 | static const MachineInstr *LastMI = 0; |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1097 | static const Function *F = 0; |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1098 | static unsigned Counter = 0U-1; |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1099 | |
| 1100 | // Comparing the address of MI isn't sufficient, because machineinstrs may |
| 1101 | // be allocated to the same address across functions. |
| 1102 | const Function *ThisF = MI->getParent()->getParent()->getFunction(); |
| 1103 | |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1104 | // If this is a new machine instruction, bump the counter. |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1105 | if (LastMI != MI || F != ThisF) { |
| 1106 | ++Counter; |
| 1107 | LastMI = MI; |
Chris Lattner | 4b09252 | 2007-02-06 01:56:31 +0000 | [diff] [blame] | 1108 | F = ThisF; |
Chris Lattner | b6a24bf | 2007-02-05 21:23:52 +0000 | [diff] [blame] | 1109 | } |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1110 | O << Counter; |
| 1111 | } else { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1112 | cerr << "Unknown special formatter '" << Code |
| 1113 | << "' for machine instr: " << *MI; |
Chris Lattner | 3ce9b67 | 2006-09-26 23:59:50 +0000 | [diff] [blame] | 1114 | exit(1); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1119 | /// printInlineAsm - This method formats and prints the specified machine |
| 1120 | /// instruction that is an inline asm. |
| 1121 | void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1122 | unsigned NumOperands = MI->getNumOperands(); |
| 1123 | |
| 1124 | // Count the number of register definitions. |
| 1125 | unsigned NumDefs = 0; |
Dan Gohman | 92dfe20 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 1126 | for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef(); |
Chris Lattner | 67942f5 | 2006-09-05 20:02:51 +0000 | [diff] [blame] | 1127 | ++NumDefs) |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1128 | assert(NumDefs != NumOperands-1 && "No asm string?"); |
| 1129 | |
| 1130 | assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?"); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1131 | |
| 1132 | // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. |
Chris Lattner | f2b67cf | 2006-01-30 23:00:08 +0000 | [diff] [blame] | 1133 | const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1134 | |
Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 1135 | // If this asmstr is empty, just print the #APP/#NOAPP markers. |
| 1136 | // These are useful to see where empty asm's wound up. |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1137 | if (AsmStr[0] == 0) { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1138 | O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n'; |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1142 | O << TAI->getInlineAsmStart() << "\n\t"; |
Chris Lattner | f26f5dd | 2006-07-28 00:17:20 +0000 | [diff] [blame] | 1143 | |
Bill Wendling | eb9a42c | 2007-01-16 03:42:04 +0000 | [diff] [blame] | 1144 | // The variant of the current asmprinter. |
| 1145 | int AsmPrinterVariant = TAI->getAssemblerDialect(); |
| 1146 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1147 | int CurVariant = -1; // The number of the {.|.|.} region we are in. |
| 1148 | const char *LastEmitted = AsmStr; // One past the last character emitted. |
Chris Lattner | 2cc2f66 | 2006-02-01 01:28:23 +0000 | [diff] [blame] | 1149 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1150 | while (*LastEmitted) { |
| 1151 | switch (*LastEmitted) { |
| 1152 | default: { |
| 1153 | // Not a special case, emit the string section literally. |
| 1154 | const char *LiteralEnd = LastEmitted+1; |
| 1155 | while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1156 | *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1157 | ++LiteralEnd; |
| 1158 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 1159 | O.write(LastEmitted, LiteralEnd-LastEmitted); |
| 1160 | LastEmitted = LiteralEnd; |
| 1161 | break; |
| 1162 | } |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1163 | case '\n': |
| 1164 | ++LastEmitted; // Consume newline character. |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1165 | O << '\n'; // Indent code with newline. |
Chris Lattner | 1c05997 | 2006-05-05 21:47:05 +0000 | [diff] [blame] | 1166 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1167 | case '$': { |
| 1168 | ++LastEmitted; // Consume '$' character. |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1169 | bool Done = true; |
| 1170 | |
| 1171 | // Handle escapes. |
| 1172 | switch (*LastEmitted) { |
| 1173 | default: Done = false; break; |
| 1174 | case '$': // $$ -> $ |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1175 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) |
| 1176 | O << '$'; |
| 1177 | ++LastEmitted; // Consume second '$' character. |
| 1178 | break; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1179 | case '(': // $( -> same as GCC's { character. |
| 1180 | ++LastEmitted; // Consume '(' character. |
| 1181 | if (CurVariant != -1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1182 | cerr << "Nested variants found in inline asm string: '" |
| 1183 | << AsmStr << "'\n"; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1184 | exit(1); |
| 1185 | } |
| 1186 | CurVariant = 0; // We're in the first variant now. |
| 1187 | break; |
| 1188 | case '|': |
| 1189 | ++LastEmitted; // consume '|' character. |
| 1190 | if (CurVariant == -1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1191 | cerr << "Found '|' character outside of variant in inline asm " |
| 1192 | << "string: '" << AsmStr << "'\n"; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1193 | exit(1); |
| 1194 | } |
| 1195 | ++CurVariant; // We're in the next variant. |
| 1196 | break; |
| 1197 | case ')': // $) -> same as GCC's } char. |
| 1198 | ++LastEmitted; // consume ')' character. |
| 1199 | if (CurVariant == -1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1200 | cerr << "Found '}' character outside of variant in inline asm " |
| 1201 | << "string: '" << AsmStr << "'\n"; |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1202 | exit(1); |
| 1203 | } |
| 1204 | CurVariant = -1; |
| 1205 | break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1206 | } |
Chris Lattner | faf1dae | 2006-10-03 23:27:09 +0000 | [diff] [blame] | 1207 | if (Done) break; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1208 | |
| 1209 | bool HasCurlyBraces = false; |
| 1210 | if (*LastEmitted == '{') { // ${variable} |
| 1211 | ++LastEmitted; // Consume '{' character. |
| 1212 | HasCurlyBraces = true; |
| 1213 | } |
| 1214 | |
| 1215 | const char *IDStart = LastEmitted; |
| 1216 | char *IDEnd; |
Chris Lattner | fad2912 | 2007-01-23 00:36:17 +0000 | [diff] [blame] | 1217 | errno = 0; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1218 | long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. |
| 1219 | if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1220 | cerr << "Bad $ operand number in inline asm string: '" |
| 1221 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1222 | exit(1); |
| 1223 | } |
| 1224 | LastEmitted = IDEnd; |
| 1225 | |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1226 | char Modifier[2] = { 0, 0 }; |
| 1227 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1228 | if (HasCurlyBraces) { |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1229 | // If we have curly braces, check for a modifier character. This |
| 1230 | // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. |
| 1231 | if (*LastEmitted == ':') { |
| 1232 | ++LastEmitted; // Consume ':' character. |
| 1233 | if (*LastEmitted == 0) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1234 | cerr << "Bad ${:} expression in inline asm string: '" |
| 1235 | << AsmStr << "'\n"; |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1236 | exit(1); |
| 1237 | } |
| 1238 | |
| 1239 | Modifier[0] = *LastEmitted; |
| 1240 | ++LastEmitted; // Consume modifier character. |
| 1241 | } |
| 1242 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1243 | if (*LastEmitted != '}') { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1244 | cerr << "Bad ${} expression in inline asm string: '" |
| 1245 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1246 | exit(1); |
| 1247 | } |
| 1248 | ++LastEmitted; // Consume '}' character. |
| 1249 | } |
| 1250 | |
| 1251 | if ((unsigned)Val >= NumOperands-1) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1252 | cerr << "Invalid $ operand number in inline asm string: '" |
| 1253 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1254 | exit(1); |
| 1255 | } |
| 1256 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1257 | // 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] | 1258 | // operand! |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1259 | if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { |
| 1260 | unsigned OpNo = 1; |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1261 | |
| 1262 | bool Error = false; |
| 1263 | |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1264 | // Scan to find the machine operand number for the operand. |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 1265 | for (; Val; --Val) { |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1266 | if (OpNo >= MI->getNumOperands()) break; |
Chris Lattner | 9e33049 | 2007-12-30 20:50:28 +0000 | [diff] [blame] | 1267 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
Chris Lattner | daf6bc6 | 2006-02-24 19:50:58 +0000 | [diff] [blame] | 1268 | OpNo += (OpFlags >> 3) + 1; |
| 1269 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1271 | if (OpNo >= MI->getNumOperands()) { |
| 1272 | Error = true; |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1273 | } else { |
Chris Lattner | 9e33049 | 2007-12-30 20:50:28 +0000 | [diff] [blame] | 1274 | unsigned OpFlags = MI->getOperand(OpNo).getImm(); |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1275 | ++OpNo; // Skip over the ID number. |
| 1276 | |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1277 | if (Modifier[0]=='l') // labels are target independent |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1278 | printBasicBlockLabel(MI->getOperand(OpNo).getMBB(), |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1279 | false, false, false); |
Dale Johannesen | eb57ea7 | 2007-11-05 21:20:28 +0000 | [diff] [blame] | 1280 | else { |
| 1281 | AsmPrinter *AP = const_cast<AsmPrinter*>(this); |
| 1282 | if ((OpFlags & 7) == 4 /*ADDR MODE*/) { |
| 1283 | Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, |
| 1284 | Modifier[0] ? Modifier : 0); |
| 1285 | } else { |
| 1286 | Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, |
| 1287 | Modifier[0] ? Modifier : 0); |
| 1288 | } |
Chris Lattner | fd561cd | 2006-06-08 18:00:47 +0000 | [diff] [blame] | 1289 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1290 | } |
| 1291 | if (Error) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1292 | cerr << "Invalid operand found in inline asm: '" |
| 1293 | << AsmStr << "'\n"; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1294 | MI->dump(); |
| 1295 | exit(1); |
| 1296 | } |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 1297 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | } |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1302 | O << "\n\t" << TAI->getInlineAsmEnd() << '\n'; |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 1305 | /// printImplicitDef - This method prints the specified machine instruction |
| 1306 | /// that is an implicit def. |
| 1307 | void AsmPrinter::printImplicitDef(const MachineInstr *MI) const { |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1308 | O << '\t' << TAI->getCommentString() << " implicit-def: " |
| 1309 | << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n'; |
Evan Cheng | da47e6e | 2008-03-15 00:03:38 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1312 | /// printLabel - This method prints a local label used by debug and |
| 1313 | /// exception handling tables. |
| 1314 | void AsmPrinter::printLabel(const MachineInstr *MI) const { |
Dan Gohman | 528bc02 | 2008-07-01 00:16:26 +0000 | [diff] [blame] | 1315 | printLabel(MI->getOperand(0).getImm()); |
Jim Laskey | 1ee2925 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 1318 | void AsmPrinter::printLabel(unsigned Id) const { |
Evan Cheng | 4eecdeb | 2008-02-02 08:39:46 +0000 | [diff] [blame] | 1319 | O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n"; |
Evan Cheng | 1b08bbc | 2008-02-01 09:10:45 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1322 | /// printDeclare - This method prints a local variable declaration used by |
| 1323 | /// debug tables. |
Evan Cheng | 4e3f5a4 | 2008-02-04 23:06:48 +0000 | [diff] [blame] | 1324 | /// FIXME: It doesn't really print anything rather it inserts a DebugVariable |
| 1325 | /// entry into dwarf table. |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1326 | void AsmPrinter::printDeclare(const MachineInstr *MI) const { |
Evan Cheng | 4e3f5a4 | 2008-02-04 23:06:48 +0000 | [diff] [blame] | 1327 | int FI = MI->getOperand(0).getIndex(); |
| 1328 | GlobalValue *GV = MI->getOperand(1).getGlobal(); |
| 1329 | MMI->RecordVariable(GV, FI); |
Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1332 | /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM |
| 1333 | /// instruction, using the specified assembler variant. Targets should |
| 1334 | /// overried this to format as appropriate. |
| 1335 | bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Chris Lattner | a36cb0a | 2006-02-06 22:17:23 +0000 | [diff] [blame] | 1336 | unsigned AsmVariant, const char *ExtraCode) { |
Chris Lattner | 6609913 | 2006-02-01 22:41:11 +0000 | [diff] [blame] | 1337 | // Target doesn't support this yet! |
| 1338 | return true; |
Chris Lattner | 0264d1a | 2006-01-27 02:10:10 +0000 | [diff] [blame] | 1339 | } |
Chris Lattner | dd26033 | 2006-02-24 20:21:58 +0000 | [diff] [blame] | 1340 | |
| 1341 | bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, |
| 1342 | unsigned AsmVariant, |
| 1343 | const char *ExtraCode) { |
| 1344 | // Target doesn't support this yet! |
| 1345 | return true; |
| 1346 | } |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1347 | |
| 1348 | /// printBasicBlockLabel - This method prints the label for the specified |
| 1349 | /// MachineBasicBlock |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1350 | void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1351 | bool printAlign, |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1352 | bool printColon, |
| 1353 | bool printComment) const { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1354 | if (printAlign) { |
| 1355 | unsigned Align = MBB->getAlignment(); |
| 1356 | if (Align) |
| 1357 | EmitAlignment(Log2_32(Align)); |
| 1358 | } |
| 1359 | |
Dan Gohman | d19a53b | 2008-06-30 22:03:41 +0000 | [diff] [blame] | 1360 | O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_' |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1361 | << MBB->getNumber(); |
Nate Begeman | cdf38c4 | 2006-05-02 05:37:32 +0000 | [diff] [blame] | 1362 | if (printColon) |
| 1363 | O << ':'; |
Chris Lattner | 9c78ecb | 2006-10-05 21:40:14 +0000 | [diff] [blame] | 1364 | if (printComment && MBB->getBasicBlock()) |
Dan Gohman | 286d569 | 2007-07-30 15:06:25 +0000 | [diff] [blame] | 1365 | O << '\t' << TAI->getCommentString() << ' ' |
Evan Cheng | 77c8f76 | 2008-07-08 00:55:58 +0000 | [diff] [blame] | 1366 | << MBB->getBasicBlock()->getNameStart(); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1367 | } |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1368 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1369 | /// printPICJumpTableSetLabel - This method prints a set label for the |
| 1370 | /// specified MachineBasicBlock for a jumptable entry. |
| 1371 | void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, |
| 1372 | const MachineBasicBlock *MBB) const { |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1373 | if (!TAI->getSetDirective()) |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1374 | return; |
| 1375 | |
Jim Laskey | 563321a | 2006-09-06 18:34:40 +0000 | [diff] [blame] | 1376 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1377 | << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1378 | printBasicBlockLabel(MBB, false, false, false); |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1379 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 1380 | << '_' << uid << '\n'; |
Nate Begeman | 52a51e38 | 2006-08-12 21:29:52 +0000 | [diff] [blame] | 1381 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1382 | |
Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1383 | void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2, |
| 1384 | const MachineBasicBlock *MBB) const { |
Evan Cheng | 41349c1 | 2006-11-01 09:23:08 +0000 | [diff] [blame] | 1385 | if (!TAI->getSetDirective()) |
| 1386 | return; |
| 1387 | |
| 1388 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1389 | << getFunctionNumber() << '_' << uid << '_' << uid2 |
| 1390 | << "_set_" << MBB->getNumber() << ','; |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 1391 | printBasicBlockLabel(MBB, false, false, false); |
Evan Cheng | 347d39f | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 1392 | O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
| 1393 | << '_' << uid << '_' << uid2 << '\n'; |
Evan Cheng | 41349c1 | 2006-11-01 09:23:08 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1396 | /// printDataDirective - This method prints the asm directive for the |
| 1397 | /// specified type. |
| 1398 | void AsmPrinter::printDataDirective(const Type *type) { |
| 1399 | const TargetData *TD = TM.getTargetData(); |
| 1400 | switch (type->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1401 | case Type::IntegerTyID: { |
| 1402 | unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); |
| 1403 | if (BitWidth <= 8) |
| 1404 | O << TAI->getData8bitsDirective(); |
| 1405 | else if (BitWidth <= 16) |
| 1406 | O << TAI->getData16bitsDirective(); |
| 1407 | else if (BitWidth <= 32) |
| 1408 | O << TAI->getData32bitsDirective(); |
| 1409 | else if (BitWidth <= 64) { |
| 1410 | assert(TAI->getData64bitsDirective() && |
| 1411 | "Target cannot handle 64-bit constant exprs!"); |
| 1412 | O << TAI->getData64bitsDirective(); |
| 1413 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1414 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1415 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1416 | case Type::PointerTyID: |
| 1417 | if (TD->getPointerSize() == 8) { |
| 1418 | assert(TAI->getData64bitsDirective() && |
| 1419 | "Target cannot handle 64-bit pointer exprs!"); |
| 1420 | O << TAI->getData64bitsDirective(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1421 | } else { |
| 1422 | O << TAI->getData32bitsDirective(); |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1423 | } |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1424 | break; |
| 1425 | case Type::FloatTyID: case Type::DoubleTyID: |
Dale Johannesen | 4292d1c | 2007-09-28 18:06:58 +0000 | [diff] [blame] | 1426 | case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: |
Evan Cheng | d6594ae | 2006-09-12 21:00:35 +0000 | [diff] [blame] | 1427 | assert (0 && "Should have already output floating point constant."); |
| 1428 | default: |
| 1429 | assert (0 && "Can't handle printing this type of thing"); |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
Dale Johannesen | 4c3a5f8 | 2007-02-16 01:54:53 +0000 | [diff] [blame] | 1433 | |
Evan Cheng | ab8faba | 2008-07-08 16:40:43 +0000 | [diff] [blame] | 1434 | void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix, |
| 1435 | const char *Prefix) { |
Dale Johannesen | c215b3e | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1436 | if (Name[0]=='\"') |
Evan Cheng | ab8faba | 2008-07-08 16:40:43 +0000 | [diff] [blame] | 1437 | O << '\"'; |
| 1438 | O << TAI->getPrivateGlobalPrefix(); |
| 1439 | if (Prefix) O << Prefix; |
| 1440 | if (Name[0]=='\"') |
| 1441 | O << '\"'; |
| 1442 | if (Name[0]=='\"') |
| 1443 | O << Name[1]; |
Dale Johannesen | c215b3e | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1444 | else |
Evan Cheng | ab8faba | 2008-07-08 16:40:43 +0000 | [diff] [blame] | 1445 | O << Name; |
| 1446 | O << Suffix; |
| 1447 | if (Name[0]=='\"') |
| 1448 | O << '\"'; |
Dale Johannesen | c215b3e | 2008-05-19 21:38:18 +0000 | [diff] [blame] | 1449 | } |
Evan Cheng | 77c8f76 | 2008-07-08 00:55:58 +0000 | [diff] [blame] | 1450 | |
Evan Cheng | ab8faba | 2008-07-08 16:40:43 +0000 | [diff] [blame] | 1451 | void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) { |
Evan Cheng | 77c8f76 | 2008-07-08 00:55:58 +0000 | [diff] [blame] | 1452 | printSuffixedName(Name.c_str(), Suffix); |
| 1453 | } |
Anton Korobeynikov | f5b6a47 | 2008-08-08 18:25:07 +0000 | [diff] [blame] | 1454 | |
| 1455 | void AsmPrinter::printVisibility(const std::string& Name, |
| 1456 | unsigned Visibility) const { |
| 1457 | if (Visibility == GlobalValue::HiddenVisibility) { |
| 1458 | if (const char *Directive = TAI->getHiddenDirective()) |
| 1459 | O << Directive << Name << '\n'; |
| 1460 | } else if (Visibility == GlobalValue::ProtectedVisibility) { |
| 1461 | if (const char *Directive = TAI->getProtectedDirective()) |
| 1462 | O << Directive << Name << '\n'; |
| 1463 | } |
| 1464 | } |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1465 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1466 | GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { |
| 1467 | if (!S->usesMetadata()) |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1468 | return 0; |
| 1469 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1470 | gcp_iterator GCPI = GCMetadataPrinters.find(S); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1471 | if (GCPI != GCMetadataPrinters.end()) |
| 1472 | return GCPI->second; |
| 1473 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1474 | const char *Name = S->getName().c_str(); |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1475 | |
| 1476 | for (GCMetadataPrinterRegistry::iterator |
| 1477 | I = GCMetadataPrinterRegistry::begin(), |
| 1478 | E = GCMetadataPrinterRegistry::end(); I != E; ++I) |
| 1479 | if (strcmp(Name, I->getName()) == 0) { |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1480 | GCMetadataPrinter *GMP = I->instantiate(); |
| 1481 | GMP->S = S; |
| 1482 | GCMetadataPrinters.insert(std::make_pair(S, GMP)); |
| 1483 | return GMP; |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Gordon Henriksen | 5eca075 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 1486 | cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n"; |
Gordon Henriksen | c317a60 | 2008-08-17 12:08:44 +0000 | [diff] [blame] | 1487 | abort(); |
| 1488 | } |