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