blob: c5d6cb206fb3f236c01946cd77a697e62c703fe9 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Owen Anderson07000c62006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000024#include "llvm/Target/TargetMachine.h"
Duraid Madina2e096c12005-12-28 06:29:02 +000025#include <iostream>
Chris Lattner66099132006-02-01 22:41:11 +000026#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000027using namespace llvm;
28
Chris Lattnered138932005-12-13 06:32:10 +000029AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
30: FunctionNumber(0), O(o), TM(tm),
31 CommentString("#"),
32 GlobalPrefix(""),
33 PrivateGlobalPrefix("."),
34 GlobalVarAddrPrefix(""),
35 GlobalVarAddrSuffix(""),
36 FunctionAddrPrefix(""),
37 FunctionAddrSuffix(""),
Chris Lattner1c059972006-05-05 21:47:05 +000038 InlineAsmStart("#APP"),
39 InlineAsmEnd("#NO_APP"),
Chris Lattnered138932005-12-13 06:32:10 +000040 ZeroDirective("\t.zero\t"),
Jeff Cohenc6a057b2006-05-02 03:46:13 +000041 ZeroDirectiveSuffix(0),
Chris Lattnered138932005-12-13 06:32:10 +000042 AsciiDirective("\t.ascii\t"),
43 AscizDirective("\t.asciz\t"),
44 Data8bitsDirective("\t.byte\t"),
45 Data16bitsDirective("\t.short\t"),
46 Data32bitsDirective("\t.long\t"),
47 Data64bitsDirective("\t.quad\t"),
48 AlignDirective("\t.align\t"),
49 AlignmentIsInBytes(true),
50 SwitchToSectionDirective("\t.section\t"),
Chris Lattner7faec9b2006-05-09 05:33:48 +000051 TextSectionStartSuffix(""),
52 DataSectionStartSuffix(""),
53 SectionEndDirectiveSuffix(0),
Chris Lattnered138932005-12-13 06:32:10 +000054 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman37efe672006-04-22 18:53:45 +000055 JumpTableSection("\t.section .rodata\n"),
Chris Lattnered138932005-12-13 06:32:10 +000056 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
57 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
58 LCOMMDirective(0),
59 COMMDirective("\t.comm\t"),
60 COMMDirectiveTakesAlignment(true),
61 HasDotTypeDotSizeDirective(true) {
62}
63
64
Chris Lattner4632d7a2006-05-09 04:59:56 +000065/// SwitchToTextSection - Switch to the specified text section of the executable
66/// if we are not already in it!
Chris Lattnerac28fbd2005-11-21 07:06:27 +000067///
Chris Lattner4632d7a2006-05-09 04:59:56 +000068void AsmPrinter::SwitchToTextSection(const char *NewSection,
69 const GlobalValue *GV) {
Chris Lattnerac28fbd2005-11-21 07:06:27 +000070 std::string NS;
Chris Lattnera7090ae2006-05-09 05:24:50 +000071 if (GV && GV->hasSection())
Chris Lattnerc8d37c62006-05-09 16:41:59 +000072 NS = SwitchToSectionDirective + GV->getSection();
Chris Lattnera7090ae2006-05-09 05:24:50 +000073 else
74 NS = NewSection;
75
76 // If we're already in this section, we're done.
77 if (CurrentSection == NS) return;
Jeff Cohen51b776d2006-05-02 03:58:45 +000078
Chris Lattner7faec9b2006-05-09 05:33:48 +000079 // Close the current section, if applicable.
80 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
81 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
Jeff Cohen51b776d2006-05-02 03:58:45 +000082
Chris Lattner7faec9b2006-05-09 05:33:48 +000083 CurrentSection = NS;
84
85 if (!CurrentSection.empty())
86 O << CurrentSection << TextSectionStartSuffix << '\n';
Chris Lattnerac28fbd2005-11-21 07:06:27 +000087}
88
Chris Lattner4632d7a2006-05-09 04:59:56 +000089/// SwitchToTextSection - Switch to the specified text section of the executable
90/// if we are not already in it!
91///
92void AsmPrinter::SwitchToDataSection(const char *NewSection,
93 const GlobalValue *GV) {
94 std::string NS;
Chris Lattnerb81cb612006-05-09 05:23:12 +000095 if (GV && GV->hasSection())
96 NS = SwitchToSectionDirective + GV->getSection();
97 else
98 NS = NewSection;
Chris Lattner4632d7a2006-05-09 04:59:56 +000099
Chris Lattnerb81cb612006-05-09 05:23:12 +0000100 // If we're already in this section, we're done.
101 if (CurrentSection == NS) return;
102
Chris Lattner7faec9b2006-05-09 05:33:48 +0000103 // Close the current section, if applicable.
104 if (SectionEndDirectiveSuffix && !CurrentSection.empty())
105 O << CurrentSection << SectionEndDirectiveSuffix << "\n";
106
107 CurrentSection = NS;
Chris Lattner4632d7a2006-05-09 04:59:56 +0000108
Chris Lattner7faec9b2006-05-09 05:33:48 +0000109 if (!CurrentSection.empty())
110 O << CurrentSection << DataSectionStartSuffix << '\n';
Chris Lattner4632d7a2006-05-09 04:59:56 +0000111}
112
113
Chris Lattnera80ba712004-08-16 23:15:22 +0000114bool AsmPrinter::doInitialization(Module &M) {
Chris Lattneraf2bf0a2004-08-17 06:06:19 +0000115 Mang = new Mangler(M, GlobalPrefix);
Chris Lattner2c1b1592006-01-23 23:47:53 +0000116
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000117 if (!M.getModuleInlineAsm().empty())
118 O << CommentString << " Start of file scope inline assembly\n"
119 << M.getModuleInlineAsm()
120 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000121
Chris Lattner4632d7a2006-05-09 04:59:56 +0000122 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskeyb3e789a2006-01-26 20:21:46 +0000123
124 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
125 DebugInfo->AnalyzeModule(M);
126 }
127
Chris Lattnera80ba712004-08-16 23:15:22 +0000128 return false;
129}
130
131bool AsmPrinter::doFinalization(Module &M) {
132 delete Mang; Mang = 0;
133 return false;
134}
135
Chris Lattner25045bd2005-11-21 07:51:36 +0000136void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000137 // What's my mangled name?
Chris Lattner450de392005-11-10 18:36:17 +0000138 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner77bc2282005-11-21 08:13:27 +0000139 IncrementFunctionNumber();
Chris Lattnera80ba712004-08-16 23:15:22 +0000140}
141
Chris Lattner3b4fd322005-11-21 08:25:09 +0000142/// EmitConstantPool - Print to the current output stream assembly
143/// representations of the constants in the constant pool MCP. This is
144/// used to print out constants which have been "spilled to memory" by
145/// the code generator.
146///
147void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000148 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000149 if (CP.empty()) return;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000150
Chris Lattner4632d7a2006-05-09 04:59:56 +0000151 SwitchToDataSection(ConstantPoolSection, 0);
Chris Lattner3029f922006-02-09 04:46:04 +0000152 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattner3b4fd322005-11-21 08:25:09 +0000153 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner3b4fd322005-11-21 08:25:09 +0000154 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng246ae0d2006-03-01 22:18:09 +0000155 << ":\t\t\t\t\t" << CommentString << " ";
156 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerfa77d432006-02-09 04:22:52 +0000157 EmitGlobalConstant(CP[i].Val);
Chris Lattner3029f922006-02-09 04:46:04 +0000158 if (i != e-1) {
Owen Andersona69571c2006-05-03 01:29:57 +0000159 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000160 unsigned ValEnd = CP[i].Offset + EntSize;
161 // Emit inter-object padding for alignment.
162 EmitZeros(CP[i+1].Offset-ValEnd);
163 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000164 }
165}
166
Nate Begeman37efe672006-04-22 18:53:45 +0000167/// EmitJumpTableInfo - Print assembly representations of the jump tables used
168/// by the current function to the current output stream.
169///
170void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
171 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
172 if (JT.empty()) return;
Owen Andersona69571c2006-05-03 01:29:57 +0000173 const TargetData *TD = TM.getTargetData();
Nate Begeman37efe672006-04-22 18:53:45 +0000174
175 // FIXME: someday we need to handle PIC jump tables
176 assert((TM.getRelocationModel() == Reloc::Static ||
177 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
178 "Unhandled relocation model emitting jump table information!");
179
Chris Lattner4632d7a2006-05-09 04:59:56 +0000180 SwitchToDataSection(JumpTableSection, 0);
Owen Andersona69571c2006-05-03 01:29:57 +0000181 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Nate Begeman37efe672006-04-22 18:53:45 +0000182 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
183 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
184 << ":\n";
185 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
186 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
187 O << Data32bitsDirective << ' ';
188 printBasicBlockLabel(JTBBs[ii]);
189 O << '\n';
190 }
191 }
192}
193
Chris Lattnered138932005-12-13 06:32:10 +0000194/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
195/// special global used by LLVM. If so, emit it and return true, otherwise
196/// do nothing and return false.
197bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey78098112006-03-07 22:00:35 +0000198 // Ignore debug and non-emitted data.
199 if (GV->getSection() == "llvm.metadata") return true;
200
201 if (!GV->hasAppendingLinkage()) return false;
202
203 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000204
205 if (GV->getName() == "llvm.used")
206 return true; // No need to emit this at all.
207
Chris Lattner5166b822006-01-12 19:17:23 +0000208 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000209 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000210 EmitAlignment(2, 0);
211 EmitXXStructorList(GV->getInitializer());
212 return true;
213 }
214
Chris Lattner5166b822006-01-12 19:17:23 +0000215 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000216 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnered138932005-12-13 06:32:10 +0000217 EmitAlignment(2, 0);
218 EmitXXStructorList(GV->getInitializer());
219 return true;
220 }
221
222 return false;
223}
224
225/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
226/// function pointers, ignoring the init priority.
227void AsmPrinter::EmitXXStructorList(Constant *List) {
228 // Should be an array of '{ int, void ()* }' structs. The first value is the
229 // init priority, which we ignore.
230 if (!isa<ConstantArray>(List)) return;
231 ConstantArray *InitList = cast<ConstantArray>(List);
232 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
233 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
234 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000235
236 if (CS->getOperand(1)->isNullValue())
237 return; // Found a null terminator, exit printing.
238 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000239 EmitGlobalConstant(CS->getOperand(1));
240 }
241}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000242
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000243/// getPreferredAlignmentLog - Return the preferred alignment of the
244/// specified global, returned in log form. This includes an explicitly
245/// requested alignment (if the global has one).
246unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskey35f8c202006-06-15 13:10:58 +0000247 const Type *ElemType = GV->getType()->getElementType();
248 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000249 if (GV->getAlignment() > (1U << Alignment))
250 Alignment = Log2_32(GV->getAlignment());
251
Chris Lattner519ea2a2006-02-05 01:46:49 +0000252 if (GV->hasInitializer()) {
Chris Lattner519ea2a2006-02-05 01:46:49 +0000253 if (Alignment < 4) {
254 // If the global is not external, see if it is large. If so, give it a
255 // larger alignment.
Jim Laskey35f8c202006-06-15 13:10:58 +0000256 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattner519ea2a2006-02-05 01:46:49 +0000257 Alignment = 4; // 16-byte alignment.
258 }
Chris Lattner4d57e0c2006-02-05 01:29:18 +0000259 }
260 return Alignment;
261}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000262
Chris Lattner25045bd2005-11-21 07:51:36 +0000263// EmitAlignment - Emit an alignment directive to the specified power of two.
264void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnera1ab72d2005-11-14 19:00:06 +0000265 if (GV && GV->getAlignment())
266 NumBits = Log2_32(GV->getAlignment());
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000267 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattnerbfddc202004-08-17 19:14:29 +0000268 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
269 O << AlignDirective << NumBits << "\n";
270}
271
Chris Lattner25045bd2005-11-21 07:51:36 +0000272/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000273///
Chris Lattner25045bd2005-11-21 07:51:36 +0000274void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000275 if (NumZeros) {
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000276 if (ZeroDirective) {
277 O << ZeroDirective << NumZeros;
278 if (ZeroDirectiveSuffix)
279 O << ZeroDirectiveSuffix;
280 O << "\n";
281 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000282 for (; NumZeros; --NumZeros)
283 O << Data8bitsDirective << "0\n";
284 }
285 }
286}
287
Chris Lattnera80ba712004-08-16 23:15:22 +0000288// Print out the specified constant, without a storage class. Only the
289// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000290void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000291 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattnera80ba712004-08-16 23:15:22 +0000292 O << "0";
293 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
294 assert(CB == ConstantBool::True);
295 O << "1";
296 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
297 if (((CI->getValue() << 32) >> 32) == CI->getValue())
298 O << CI->getValue();
299 else
Duraid Madina45606572005-05-15 13:05:48 +0000300 O << (uint64_t)CI->getValue();
Chris Lattnera80ba712004-08-16 23:15:22 +0000301 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
302 O << CI->getValue();
Chris Lattner450de392005-11-10 18:36:17 +0000303 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000304 // This is a constant address for a global variable or function. Use the
305 // name of the variable or function as the address value, possibly
306 // decorating it with GlobalVarAddrPrefix/Suffix or
307 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattner450de392005-11-10 18:36:17 +0000308 if (isa<Function>(GV))
309 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000310 else
Chris Lattner450de392005-11-10 18:36:17 +0000311 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina855a5192005-04-02 12:21:51 +0000312 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000313 const TargetData *TD = TM.getTargetData();
Chris Lattnera80ba712004-08-16 23:15:22 +0000314 switch(CE->getOpcode()) {
315 case Instruction::GetElementPtr: {
316 // generate a symbolic expression for the byte address
317 const Constant *ptrVal = CE->getOperand(0);
318 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Andersona69571c2006-05-03 01:29:57 +0000319 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner27e19212005-02-14 21:40:26 +0000320 if (Offset)
321 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000322 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000323 if (Offset > 0)
324 O << ") + " << Offset;
325 else if (Offset < 0)
326 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000327 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000328 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000329 }
330 break;
331 }
332 case Instruction::Cast: {
333 // Support only non-converting or widening casts for now, that is, ones
334 // that do not involve a change in value. This assertion is really gross,
335 // and may not even be a complete check.
336 Constant *Op = CE->getOperand(0);
337 const Type *OpTy = Op->getType(), *Ty = CE->getType();
338
339 // Remember, kids, pointers can be losslessly converted back and forth
340 // into 32-bit or wider integers, regardless of signedness. :-P
341 assert(((isa<PointerType>(OpTy)
342 && (Ty == Type::LongTy || Ty == Type::ULongTy
343 || Ty == Type::IntTy || Ty == Type::UIntTy))
344 || (isa<PointerType>(Ty)
345 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
346 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Owen Andersona69571c2006-05-03 01:29:57 +0000347 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
Chris Lattnera80ba712004-08-16 23:15:22 +0000348 && OpTy->isLosslesslyConvertibleTo(Ty))))
349 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner25045bd2005-11-21 07:51:36 +0000350 EmitConstantValueOnly(Op);
Chris Lattnera80ba712004-08-16 23:15:22 +0000351 break;
352 }
353 case Instruction::Add:
354 O << "(";
Chris Lattner25045bd2005-11-21 07:51:36 +0000355 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattnera80ba712004-08-16 23:15:22 +0000356 O << ") + (";
Chris Lattner25045bd2005-11-21 07:51:36 +0000357 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattnera80ba712004-08-16 23:15:22 +0000358 O << ")";
359 break;
360 default:
361 assert(0 && "Unsupported operator!");
362 }
363 } else {
364 assert(0 && "Unknown constant value!");
365 }
366}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000367
368/// toOctal - Convert the low order bits of X into an octal digit.
369///
370static inline char toOctal(int X) {
371 return (X&7)+'0';
372}
373
Chris Lattner2980cef2005-11-10 18:06:33 +0000374/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000375/// the predicate isString is true.
376///
Chris Lattner2980cef2005-11-10 18:06:33 +0000377static void printAsCString(std::ostream &O, const ConstantArray *CVA,
378 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000379 assert(CVA->isString() && "Array is not string compatible!");
380
381 O << "\"";
Chris Lattner2980cef2005-11-10 18:06:33 +0000382 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000383 unsigned char C =
Chris Lattnerdea18b62005-01-08 19:59:10 +0000384 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000385
386 if (C == '"') {
387 O << "\\\"";
388 } else if (C == '\\') {
389 O << "\\\\";
390 } else if (isprint(C)) {
391 O << C;
392 } else {
393 switch(C) {
394 case '\b': O << "\\b"; break;
395 case '\f': O << "\\f"; break;
396 case '\n': O << "\\n"; break;
397 case '\r': O << "\\r"; break;
398 case '\t': O << "\\t"; break;
399 default:
400 O << '\\';
401 O << toOctal(C >> 6);
402 O << toOctal(C >> 3);
403 O << toOctal(C >> 0);
404 break;
405 }
406 }
407 }
408 O << "\"";
409}
410
Jeff Cohenc884db42006-05-02 01:16:28 +0000411/// EmitString - Emit a zero-byte-terminated string constant.
412///
413void AsmPrinter::EmitString(const ConstantArray *CVA) const {
414 unsigned NumElts = CVA->getNumOperands();
415 if (AscizDirective && NumElts &&
416 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
417 O << AscizDirective;
418 printAsCString(O, CVA, NumElts-1);
419 } else {
420 O << AsciiDirective;
421 printAsCString(O, CVA, NumElts);
422 }
423 O << "\n";
424}
425
Chris Lattner25045bd2005-11-21 07:51:36 +0000426/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner1b7e2352004-08-17 06:36:49 +0000427///
Chris Lattner25045bd2005-11-21 07:51:36 +0000428void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Andersona69571c2006-05-03 01:29:57 +0000429 const TargetData *TD = TM.getTargetData();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000430
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000431 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000432 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000433 return;
434 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
435 if (CVA->isString()) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000436 EmitString(CVA);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000437 } else { // Not a string. Print the values in successive locations
438 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattner25045bd2005-11-21 07:51:36 +0000439 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner1b7e2352004-08-17 06:36:49 +0000440 }
441 return;
442 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
443 // Print the fields in successive locations. Pad to align if needed!
Owen Andersona69571c2006-05-03 01:29:57 +0000444 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000445 uint64_t sizeSoFar = 0;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000446 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
447 const Constant* field = CVS->getOperand(i);
448
449 // Check if padding is needed and insert one or more 0s.
Owen Andersona69571c2006-05-03 01:29:57 +0000450 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattnerdea18b62005-01-08 19:59:10 +0000451 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner1b7e2352004-08-17 06:36:49 +0000452 : cvsLayout->MemberOffsets[i+1])
453 - cvsLayout->MemberOffsets[i]) - fieldSize;
454 sizeSoFar += fieldSize + padSize;
455
456 // Now print the actual field value
Chris Lattner25045bd2005-11-21 07:51:36 +0000457 EmitGlobalConstant(field);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000458
459 // Insert the field padding unless it's zero bytes...
Chris Lattner25045bd2005-11-21 07:51:36 +0000460 EmitZeros(padSize);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000461 }
462 assert(sizeSoFar == cvsLayout->StructSize &&
463 "Layout of constant struct may be incorrect!");
464 return;
465 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
466 // FP Constants are printed as integer constants to avoid losing
467 // precision...
468 double Val = CFP->getValue();
469 if (CFP->getType() == Type::DoubleTy) {
Chris Lattnere85a5a92004-08-17 06:48:16 +0000470 if (Data64bitsDirective)
Jim Laskeycb6682f2005-08-17 19:34:49 +0000471 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattner7d057a32004-08-17 21:38:40 +0000472 << " double value: " << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000473 else if (TD->isBigEndian()) {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000474 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000475 << "\t" << CommentString << " double most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000476 << Val << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000477 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000478 << "\t" << CommentString << " double least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000479 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000480 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000481 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000482 << "\t" << CommentString << " double least significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000483 << "\n";
Jim Laskeycb6682f2005-08-17 19:34:49 +0000484 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000485 << "\t" << CommentString << " double most significant word " << Val
Chris Lattner0554fb62004-08-17 16:27:05 +0000486 << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000487 }
488 return;
489 } else {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000490 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattner0554fb62004-08-17 16:27:05 +0000491 << " float " << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000492 return;
493 }
494 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
495 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
496 uint64_t Val = CI->getRawValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000497
Chris Lattnere85a5a92004-08-17 06:48:16 +0000498 if (Data64bitsDirective)
499 O << Data64bitsDirective << Val << "\n";
Owen Andersona69571c2006-05-03 01:29:57 +0000500 else if (TD->isBigEndian()) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000501 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000502 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000503 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000504 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000505 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000506 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000507 } else {
508 O << Data32bitsDirective << unsigned(Val)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000509 << "\t" << CommentString << " Double-word least significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000510 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000511 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattnerf746a7d2004-08-18 02:22:55 +0000512 << "\t" << CommentString << " Double-word most significant word "
Chris Lattner0554fb62004-08-17 16:27:05 +0000513 << Val << "\n";
Chris Lattner1b7e2352004-08-17 06:36:49 +0000514 }
515 return;
516 }
Nate Begeman8cfa57b2005-12-06 06:18:55 +0000517 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
518 const PackedType *PTy = CP->getType();
519
520 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
521 EmitGlobalConstant(CP->getOperand(I));
522
523 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000524 }
525
526 const Type *type = CV->getType();
Chris Lattner1b7e2352004-08-17 06:36:49 +0000527 switch (type->getTypeID()) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000528 case Type::BoolTyID:
Chris Lattner1b7e2352004-08-17 06:36:49 +0000529 case Type::UByteTyID: case Type::SByteTyID:
530 O << Data8bitsDirective;
531 break;
532 case Type::UShortTyID: case Type::ShortTyID:
533 O << Data16bitsDirective;
534 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000535 case Type::PointerTyID:
Owen Andersona69571c2006-05-03 01:29:57 +0000536 if (TD->getPointerSize() == 8) {
Evan Chenga9767f62006-06-15 08:10:56 +0000537 assert(Data64bitsDirective &&
538 "Target cannot handle 64-bit pointer exprs!");
Andrew Lenharth571c9c32005-02-04 13:47:16 +0000539 O << Data64bitsDirective;
540 break;
541 }
542 //Fall through for pointer size == int size
Chris Lattner1b7e2352004-08-17 06:36:49 +0000543 case Type::UIntTyID: case Type::IntTyID:
544 O << Data32bitsDirective;
545 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000546 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner660538c2005-08-08 04:26:32 +0000547 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
548 O << Data64bitsDirective;
549 break;
Chris Lattner1b7e2352004-08-17 06:36:49 +0000550 case Type::FloatTyID: case Type::DoubleTyID:
551 assert (0 && "Should have already output floating point constant.");
552 default:
553 assert (0 && "Can't handle printing this type of thing");
554 break;
555 }
Chris Lattner25045bd2005-11-21 07:51:36 +0000556 EmitConstantValueOnly(CV);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000557 O << "\n";
558}
Chris Lattner0264d1a2006-01-27 02:10:10 +0000559
560/// printInlineAsm - This method formats and prints the specified machine
561/// instruction that is an inline asm.
562void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner1c059972006-05-05 21:47:05 +0000563 O << InlineAsmStart << "\n\t";
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000564 unsigned NumOperands = MI->getNumOperands();
565
566 // Count the number of register definitions.
567 unsigned NumDefs = 0;
568 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
569 assert(NumDefs != NumOperands-1 && "No asm string?");
570
571 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +0000572
573 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000574 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +0000575
576 // The variant of the current asmprinter: FIXME: change.
577 int AsmPrinterVariant = 0;
Chris Lattnerf2b67cf2006-01-30 23:00:08 +0000578
Chris Lattner66099132006-02-01 22:41:11 +0000579 int CurVariant = -1; // The number of the {.|.|.} region we are in.
580 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +0000581
Chris Lattner66099132006-02-01 22:41:11 +0000582 while (*LastEmitted) {
583 switch (*LastEmitted) {
584 default: {
585 // Not a special case, emit the string section literally.
586 const char *LiteralEnd = LastEmitted+1;
587 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +0000588 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +0000589 ++LiteralEnd;
590 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
591 O.write(LastEmitted, LiteralEnd-LastEmitted);
592 LastEmitted = LiteralEnd;
593 break;
594 }
Chris Lattner1c059972006-05-05 21:47:05 +0000595 case '\n':
596 ++LastEmitted; // Consume newline character.
597 O << "\n\t"; // Indent code with newline.
598 break;
Chris Lattner66099132006-02-01 22:41:11 +0000599 case '$': {
600 ++LastEmitted; // Consume '$' character.
601 if (*LastEmitted == '$') { // $$ -> $
602 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
603 O << '$';
604 ++LastEmitted; // Consume second '$' character.
605 break;
606 }
607
608 bool HasCurlyBraces = false;
609 if (*LastEmitted == '{') { // ${variable}
610 ++LastEmitted; // Consume '{' character.
611 HasCurlyBraces = true;
612 }
613
614 const char *IDStart = LastEmitted;
615 char *IDEnd;
616 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
617 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
618 std::cerr << "Bad $ operand number in inline asm string: '"
619 << AsmStr << "'\n";
620 exit(1);
621 }
622 LastEmitted = IDEnd;
623
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000624 char Modifier[2] = { 0, 0 };
625
Chris Lattner66099132006-02-01 22:41:11 +0000626 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000627 // If we have curly braces, check for a modifier character. This
628 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
629 if (*LastEmitted == ':') {
630 ++LastEmitted; // Consume ':' character.
631 if (*LastEmitted == 0) {
632 std::cerr << "Bad ${:} expression in inline asm string: '"
633 << AsmStr << "'\n";
634 exit(1);
635 }
636
637 Modifier[0] = *LastEmitted;
638 ++LastEmitted; // Consume modifier character.
639 }
640
Chris Lattner66099132006-02-01 22:41:11 +0000641 if (*LastEmitted != '}') {
642 std::cerr << "Bad ${} expression in inline asm string: '"
643 << AsmStr << "'\n";
644 exit(1);
645 }
646 ++LastEmitted; // Consume '}' character.
647 }
648
649 if ((unsigned)Val >= NumOperands-1) {
650 std::cerr << "Invalid $ operand number in inline asm string: '"
651 << AsmStr << "'\n";
652 exit(1);
653 }
654
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000655 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +0000656 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000657 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
658 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000659
660 bool Error = false;
661
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000662 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000663 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000664 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +0000665 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
666 OpNo += (OpFlags >> 3) + 1;
667 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000668
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000669 if (OpNo >= MI->getNumOperands()) {
670 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +0000671 } else {
Chris Lattnerfd561cd2006-06-08 18:00:47 +0000672 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
673 ++OpNo; // Skip over the ID number.
674
675 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
676 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
677 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
678 Modifier[0] ? Modifier : 0);
679 } else {
680 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
681 Modifier[0] ? Modifier : 0);
682 }
Chris Lattnerdd260332006-02-24 20:21:58 +0000683 }
684 if (Error) {
Chris Lattner66099132006-02-01 22:41:11 +0000685 std::cerr << "Invalid operand found in inline asm: '"
686 << AsmStr << "'\n";
687 MI->dump();
688 exit(1);
689 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +0000690 }
Chris Lattner66099132006-02-01 22:41:11 +0000691 break;
692 }
693 case '{':
694 ++LastEmitted; // Consume '{' character.
695 if (CurVariant != -1) {
696 std::cerr << "Nested variants found in inline asm string: '"
697 << AsmStr << "'\n";
698 exit(1);
699 }
700 CurVariant = 0; // We're in the first variant now.
701 break;
702 case '|':
703 ++LastEmitted; // consume '|' character.
704 if (CurVariant == -1) {
705 std::cerr << "Found '|' character outside of variant in inline asm "
706 << "string: '" << AsmStr << "'\n";
707 exit(1);
708 }
709 ++CurVariant; // We're in the next variant.
710 break;
711 case '}':
712 ++LastEmitted; // consume '}' character.
713 if (CurVariant == -1) {
714 std::cerr << "Found '}' character outside of variant in inline asm "
715 << "string: '" << AsmStr << "'\n";
716 exit(1);
717 }
718 CurVariant = -1;
719 break;
720 }
721 }
Chris Lattner1c059972006-05-05 21:47:05 +0000722 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattner66099132006-02-01 22:41:11 +0000723}
724
725/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
726/// instruction, using the specified assembler variant. Targets should
727/// overried this to format as appropriate.
728bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +0000729 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +0000730 // Target doesn't support this yet!
731 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +0000732}
Chris Lattnerdd260332006-02-24 20:21:58 +0000733
734bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
735 unsigned AsmVariant,
736 const char *ExtraCode) {
737 // Target doesn't support this yet!
738 return true;
739}
Nate Begeman37efe672006-04-22 18:53:45 +0000740
741/// printBasicBlockLabel - This method prints the label for the specified
742/// MachineBasicBlock
Nate Begemancdf38c42006-05-02 05:37:32 +0000743void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
744 bool printColon,
745 bool printComment) const {
Nate Begeman9d51eeb2006-05-02 17:36:46 +0000746 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
747 << MBB->getNumber();
Nate Begemancdf38c42006-05-02 05:37:32 +0000748 if (printColon)
749 O << ':';
750 if (printComment)
751 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman37efe672006-04-22 18:53:45 +0000752}