blob: 242a4fbbd04cd87cc121fc9018ddcea7018ac94e [file] [log] [blame]
Chris Lattner6a8e0f52004-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 Cheng993e9cf2006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng38d5e762006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman41b1cdc2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattnerc0a1eba2005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattnerf2991ce2005-11-21 08:25:09 +000019#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000020#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000021#include "llvm/Support/Mangler.h"
Jim Laskeyb74c6662005-08-17 19:34:49 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000023#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000024#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000025#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000026using namespace llvm;
27
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000028AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm)
29: FunctionNumber(0), O(o), TM(tm),
30 CommentString("#"),
31 GlobalPrefix(""),
32 PrivateGlobalPrefix("."),
33 GlobalVarAddrPrefix(""),
34 GlobalVarAddrSuffix(""),
35 FunctionAddrPrefix(""),
36 FunctionAddrSuffix(""),
Chris Lattnera633c3132006-05-05 21:47:05 +000037 InlineAsmStart("#APP"),
38 InlineAsmEnd("#NO_APP"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000039 ZeroDirective("\t.zero\t"),
Jeff Cohenf34ddb12006-05-02 03:46:13 +000040 ZeroDirectiveSuffix(0),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000041 AsciiDirective("\t.ascii\t"),
42 AscizDirective("\t.asciz\t"),
43 Data8bitsDirective("\t.byte\t"),
44 Data16bitsDirective("\t.short\t"),
45 Data32bitsDirective("\t.long\t"),
46 Data64bitsDirective("\t.quad\t"),
47 AlignDirective("\t.align\t"),
48 AlignmentIsInBytes(true),
49 SwitchToSectionDirective("\t.section\t"),
Jeff Cohen470f4312006-05-02 03:58:45 +000050 MLSections(false),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000051 ConstantPoolSection("\t.section .rodata\n"),
Nate Begeman4ca2ea52006-04-22 18:53:45 +000052 JumpTableSection("\t.section .rodata\n"),
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000053 StaticCtorsSection("\t.section .ctors,\"aw\",@progbits"),
54 StaticDtorsSection("\t.section .dtors,\"aw\",@progbits"),
55 LCOMMDirective(0),
56 COMMDirective("\t.comm\t"),
57 COMMDirectiveTakesAlignment(true),
58 HasDotTypeDotSizeDirective(true) {
59}
60
61
Chris Lattner8488ba22006-05-09 04:59:56 +000062/// SwitchToTextSection - Switch to the specified text section of the executable
63/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000064///
Chris Lattner8488ba22006-05-09 04:59:56 +000065void AsmPrinter::SwitchToTextSection(const char *NewSection,
66 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000067 std::string NS;
Jeff Cohen470f4312006-05-02 03:58:45 +000068
69 // Microsoft ML/MASM has a fundamentally different approach to handling
70 // sections.
71
72 if (MLSections) {
Jeff Cohen470f4312006-05-02 03:58:45 +000073 if (GV && GV->hasSection())
74 NS = GV->getSection();
Jeff Cohen470f4312006-05-02 03:58:45 +000075 else
Chris Lattnerc0f0dfa2006-05-09 05:13:34 +000076 NS = NewSection;
Jeff Cohen470f4312006-05-02 03:58:45 +000077
78 if (CurrentSection != NS) {
Jeff Cohence9b9fe2006-05-06 21:27:14 +000079 if (!CurrentSection.empty())
80 O << CurrentSection << "\tends\n\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000081 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000082 O << CurrentSection << "\tsegment 'CODE'\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000083 }
84 } else {
85 if (GV && GV->hasSection())
86 NS = SwitchToSectionDirective + GV->getSection();
87 else
88 NS = std::string("\t")+NewSection;
89
90 if (CurrentSection != NS) {
91 CurrentSection = NS;
92 if (!CurrentSection.empty())
93 O << CurrentSection << '\n';
94 }
Chris Lattner2ea5c992005-11-21 07:06:27 +000095 }
96}
97
Chris Lattner8488ba22006-05-09 04:59:56 +000098/// SwitchToTextSection - Switch to the specified text section of the executable
99/// if we are not already in it!
100///
101void AsmPrinter::SwitchToDataSection(const char *NewSection,
102 const GlobalValue *GV) {
103 std::string NS;
104
105 // Microsoft ML/MASM has a fundamentally different approach to handling
106 // sections.
107
108 if (MLSections) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000109 if (GV && GV->hasSection())
110 NS = GV->getSection();
111 else
Chris Lattnerc0f0dfa2006-05-09 05:13:34 +0000112 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +0000113
114 if (CurrentSection != NS) {
115 if (!CurrentSection.empty())
116 O << CurrentSection << "\tends\n\n";
117 CurrentSection = NS;
118 O << CurrentSection << "\tsegment 'DATA'\n";
119 }
120 } else {
121 if (GV && GV->hasSection())
122 NS = SwitchToSectionDirective + GV->getSection();
123 else
124 NS = std::string("\t")+NewSection;
125
126 if (CurrentSection != NS) {
127 CurrentSection = NS;
128 if (!CurrentSection.empty())
129 O << CurrentSection << '\n';
130 }
131 }
132}
133
134
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000135bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000136 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000137
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000138 if (!M.getModuleInlineAsm().empty())
139 O << CommentString << " Start of file scope inline assembly\n"
140 << M.getModuleInlineAsm()
141 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000142
Chris Lattner8488ba22006-05-09 04:59:56 +0000143 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000144
145 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
146 DebugInfo->AnalyzeModule(M);
147 }
148
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000149 return false;
150}
151
152bool AsmPrinter::doFinalization(Module &M) {
153 delete Mang; Mang = 0;
154 return false;
155}
156
Chris Lattnerbb644e32005-11-21 07:51:36 +0000157void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000158 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000159 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000160 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000161}
162
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000163/// EmitConstantPool - Print to the current output stream assembly
164/// representations of the constants in the constant pool MCP. This is
165/// used to print out constants which have been "spilled to memory" by
166/// the code generator.
167///
168void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000169 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000170 if (CP.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000171 const TargetData *TD = TM.getTargetData();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000172
Chris Lattner8488ba22006-05-09 04:59:56 +0000173 SwitchToDataSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000174 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000175 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000176 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000177 << ":\t\t\t\t\t" << CommentString << " ";
178 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000179 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000180 if (i != e-1) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000181 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
Chris Lattnerf6190822006-02-09 04:46:04 +0000182 unsigned ValEnd = CP[i].Offset + EntSize;
183 // Emit inter-object padding for alignment.
184 EmitZeros(CP[i+1].Offset-ValEnd);
185 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000186 }
187}
188
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000189/// EmitJumpTableInfo - Print assembly representations of the jump tables used
190/// by the current function to the current output stream.
191///
192void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
193 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
194 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000195 const TargetData *TD = TM.getTargetData();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000196
197 // FIXME: someday we need to handle PIC jump tables
198 assert((TM.getRelocationModel() == Reloc::Static ||
199 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
200 "Unhandled relocation model emitting jump table information!");
201
Chris Lattner8488ba22006-05-09 04:59:56 +0000202 SwitchToDataSection(JumpTableSection, 0);
Owen Anderson20a631f2006-05-03 01:29:57 +0000203 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000204 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
205 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
206 << ":\n";
207 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
208 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
209 O << Data32bitsDirective << ' ';
210 printBasicBlockLabel(JTBBs[ii]);
211 O << '\n';
212 }
213 }
214}
215
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000216/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
217/// special global used by LLVM. If so, emit it and return true, otherwise
218/// do nothing and return false.
219bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000220 // Ignore debug and non-emitted data.
221 if (GV->getSection() == "llvm.metadata") return true;
222
223 if (!GV->hasAppendingLinkage()) return false;
224
225 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000226
227 if (GV->getName() == "llvm.used")
228 return true; // No need to emit this at all.
229
Chris Lattner3760e902006-01-12 19:17:23 +0000230 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000231 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000232 EmitAlignment(2, 0);
233 EmitXXStructorList(GV->getInitializer());
234 return true;
235 }
236
Chris Lattner3760e902006-01-12 19:17:23 +0000237 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000238 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000239 EmitAlignment(2, 0);
240 EmitXXStructorList(GV->getInitializer());
241 return true;
242 }
243
244 return false;
245}
246
247/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
248/// function pointers, ignoring the init priority.
249void AsmPrinter::EmitXXStructorList(Constant *List) {
250 // Should be an array of '{ int, void ()* }' structs. The first value is the
251 // init priority, which we ignore.
252 if (!isa<ConstantArray>(List)) return;
253 ConstantArray *InitList = cast<ConstantArray>(List);
254 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
255 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
256 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000257
258 if (CS->getOperand(1)->isNullValue())
259 return; // Found a null terminator, exit printing.
260 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000261 EmitGlobalConstant(CS->getOperand(1));
262 }
263}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000264
Chris Lattnera9b25252006-02-05 01:29:18 +0000265/// getPreferredAlignmentLog - Return the preferred alignment of the
266/// specified global, returned in log form. This includes an explicitly
267/// requested alignment (if the global has one).
268unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Owen Anderson20a631f2006-05-03 01:29:57 +0000269 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
Chris Lattnera9b25252006-02-05 01:29:18 +0000270 if (GV->getAlignment() > (1U << Alignment))
271 Alignment = Log2_32(GV->getAlignment());
272
Chris Lattnercbab2842006-02-05 01:46:49 +0000273 if (GV->hasInitializer()) {
274 // Always round up alignment of global doubles to 8 bytes.
275 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
276 Alignment = 3;
277 if (Alignment < 4) {
278 // If the global is not external, see if it is large. If so, give it a
279 // larger alignment.
Owen Anderson20a631f2006-05-03 01:29:57 +0000280 if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000281 Alignment = 4; // 16-byte alignment.
282 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000283 }
284 return Alignment;
285}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000286
Chris Lattnerbb644e32005-11-21 07:51:36 +0000287// EmitAlignment - Emit an alignment directive to the specified power of two.
288void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000289 if (GV && GV->getAlignment())
290 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000291 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000292 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
293 O << AlignDirective << NumBits << "\n";
294}
295
Chris Lattnerbb644e32005-11-21 07:51:36 +0000296/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000297///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000298void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000299 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000300 if (ZeroDirective) {
301 O << ZeroDirective << NumZeros;
302 if (ZeroDirectiveSuffix)
303 O << ZeroDirectiveSuffix;
304 O << "\n";
305 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000306 for (; NumZeros; --NumZeros)
307 O << Data8bitsDirective << "0\n";
308 }
309 }
310}
311
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000312// Print out the specified constant, without a storage class. Only the
313// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000314void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000315 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000316 O << "0";
317 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
318 assert(CB == ConstantBool::True);
319 O << "1";
320 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
321 if (((CI->getValue() << 32) >> 32) == CI->getValue())
322 O << CI->getValue();
323 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000324 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000325 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
326 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000327 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000328 // This is a constant address for a global variable or function. Use the
329 // name of the variable or function as the address value, possibly
330 // decorating it with GlobalVarAddrPrefix/Suffix or
331 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000332 if (isa<Function>(GV))
333 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000334 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000335 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000336 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000337 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000338 switch(CE->getOpcode()) {
339 case Instruction::GetElementPtr: {
340 // generate a symbolic expression for the byte address
341 const Constant *ptrVal = CE->getOperand(0);
342 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000343 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000344 if (Offset)
345 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000346 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000347 if (Offset > 0)
348 O << ") + " << Offset;
349 else if (Offset < 0)
350 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000351 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000352 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000353 }
354 break;
355 }
356 case Instruction::Cast: {
357 // Support only non-converting or widening casts for now, that is, ones
358 // that do not involve a change in value. This assertion is really gross,
359 // and may not even be a complete check.
360 Constant *Op = CE->getOperand(0);
361 const Type *OpTy = Op->getType(), *Ty = CE->getType();
362
363 // Remember, kids, pointers can be losslessly converted back and forth
364 // into 32-bit or wider integers, regardless of signedness. :-P
365 assert(((isa<PointerType>(OpTy)
366 && (Ty == Type::LongTy || Ty == Type::ULongTy
367 || Ty == Type::IntTy || Ty == Type::UIntTy))
368 || (isa<PointerType>(Ty)
369 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
370 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Owen Anderson20a631f2006-05-03 01:29:57 +0000371 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000372 && OpTy->isLosslesslyConvertibleTo(Ty))))
373 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000374 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000375 break;
376 }
377 case Instruction::Add:
378 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000379 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000380 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000381 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000382 O << ")";
383 break;
384 default:
385 assert(0 && "Unsupported operator!");
386 }
387 } else {
388 assert(0 && "Unknown constant value!");
389 }
390}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000391
392/// toOctal - Convert the low order bits of X into an octal digit.
393///
394static inline char toOctal(int X) {
395 return (X&7)+'0';
396}
397
Chris Lattner55a6d902005-11-10 18:06:33 +0000398/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000399/// the predicate isString is true.
400///
Chris Lattner55a6d902005-11-10 18:06:33 +0000401static void printAsCString(std::ostream &O, const ConstantArray *CVA,
402 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000403 assert(CVA->isString() && "Array is not string compatible!");
404
405 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000406 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000407 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000408 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000409
410 if (C == '"') {
411 O << "\\\"";
412 } else if (C == '\\') {
413 O << "\\\\";
414 } else if (isprint(C)) {
415 O << C;
416 } else {
417 switch(C) {
418 case '\b': O << "\\b"; break;
419 case '\f': O << "\\f"; break;
420 case '\n': O << "\\n"; break;
421 case '\r': O << "\\r"; break;
422 case '\t': O << "\\t"; break;
423 default:
424 O << '\\';
425 O << toOctal(C >> 6);
426 O << toOctal(C >> 3);
427 O << toOctal(C >> 0);
428 break;
429 }
430 }
431 }
432 O << "\"";
433}
434
Jeff Cohen24a62a92006-05-02 01:16:28 +0000435/// EmitString - Emit a zero-byte-terminated string constant.
436///
437void AsmPrinter::EmitString(const ConstantArray *CVA) const {
438 unsigned NumElts = CVA->getNumOperands();
439 if (AscizDirective && NumElts &&
440 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
441 O << AscizDirective;
442 printAsCString(O, CVA, NumElts-1);
443 } else {
444 O << AsciiDirective;
445 printAsCString(O, CVA, NumElts);
446 }
447 O << "\n";
448}
449
Chris Lattnerbb644e32005-11-21 07:51:36 +0000450/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000451///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000452void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000453 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000454
Chris Lattner61753bf2004-10-16 18:19:26 +0000455 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000456 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000457 return;
458 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
459 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000460 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000461 } else { // Not a string. Print the values in successive locations
462 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000463 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000464 }
465 return;
466 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
467 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000468 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000469 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000470 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
471 const Constant* field = CVS->getOperand(i);
472
473 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000474 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000475 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000476 : cvsLayout->MemberOffsets[i+1])
477 - cvsLayout->MemberOffsets[i]) - fieldSize;
478 sizeSoFar += fieldSize + padSize;
479
480 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000481 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000482
483 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000484 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000485 }
486 assert(sizeSoFar == cvsLayout->StructSize &&
487 "Layout of constant struct may be incorrect!");
488 return;
489 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
490 // FP Constants are printed as integer constants to avoid losing
491 // precision...
492 double Val = CFP->getValue();
493 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000494 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000495 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000496 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000497 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000498 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000499 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000500 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000501 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000502 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000503 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000504 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000505 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000506 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000507 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000508 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000509 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000510 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000511 }
512 return;
513 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000514 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000515 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000516 return;
517 }
518 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
519 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
520 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000521
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000522 if (Data64bitsDirective)
523 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000524 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000525 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000526 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000527 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000528 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000529 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000530 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000531 } else {
532 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000533 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000534 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000535 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000536 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000537 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000538 }
539 return;
540 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000541 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
542 const PackedType *PTy = CP->getType();
543
544 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
545 EmitGlobalConstant(CP->getOperand(I));
546
547 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000548 }
549
550 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000551 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000552 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000553 case Type::UByteTyID: case Type::SByteTyID:
554 O << Data8bitsDirective;
555 break;
556 case Type::UShortTyID: case Type::ShortTyID:
557 O << Data16bitsDirective;
558 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000559 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000560 if (TD->getPointerSize() == 8) {
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000561 O << Data64bitsDirective;
562 break;
563 }
564 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000565 case Type::UIntTyID: case Type::IntTyID:
566 O << Data32bitsDirective;
567 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000568 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000569 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
570 O << Data64bitsDirective;
571 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000572 case Type::FloatTyID: case Type::DoubleTyID:
573 assert (0 && "Should have already output floating point constant.");
574 default:
575 assert (0 && "Can't handle printing this type of thing");
576 break;
577 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000578 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000579 O << "\n";
580}
Chris Lattner061d9e22006-01-27 02:10:10 +0000581
582/// printInlineAsm - This method formats and prints the specified machine
583/// instruction that is an inline asm.
584void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnera633c3132006-05-05 21:47:05 +0000585 O << InlineAsmStart << "\n\t";
Chris Lattner57ecb562006-01-30 23:00:08 +0000586 unsigned NumOperands = MI->getNumOperands();
587
588 // Count the number of register definitions.
589 unsigned NumDefs = 0;
590 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
591 assert(NumDefs != NumOperands-1 && "No asm string?");
592
593 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000594
595 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000596 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000597
598 // The variant of the current asmprinter: FIXME: change.
599 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000600
Chris Lattneraa23fa92006-02-01 22:41:11 +0000601 int CurVariant = -1; // The number of the {.|.|.} region we are in.
602 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000603
Chris Lattneraa23fa92006-02-01 22:41:11 +0000604 while (*LastEmitted) {
605 switch (*LastEmitted) {
606 default: {
607 // Not a special case, emit the string section literally.
608 const char *LiteralEnd = LastEmitted+1;
609 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000610 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000611 ++LiteralEnd;
612 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
613 O.write(LastEmitted, LiteralEnd-LastEmitted);
614 LastEmitted = LiteralEnd;
615 break;
616 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000617 case '\n':
618 ++LastEmitted; // Consume newline character.
619 O << "\n\t"; // Indent code with newline.
620 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000621 case '$': {
622 ++LastEmitted; // Consume '$' character.
623 if (*LastEmitted == '$') { // $$ -> $
624 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
625 O << '$';
626 ++LastEmitted; // Consume second '$' character.
627 break;
628 }
629
630 bool HasCurlyBraces = false;
631 if (*LastEmitted == '{') { // ${variable}
632 ++LastEmitted; // Consume '{' character.
633 HasCurlyBraces = true;
634 }
635
636 const char *IDStart = LastEmitted;
637 char *IDEnd;
638 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
639 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
640 std::cerr << "Bad $ operand number in inline asm string: '"
641 << AsmStr << "'\n";
642 exit(1);
643 }
644 LastEmitted = IDEnd;
645
Chris Lattner34f74c12006-02-06 22:17:23 +0000646 char Modifier[2] = { 0, 0 };
647
Chris Lattneraa23fa92006-02-01 22:41:11 +0000648 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000649 // If we have curly braces, check for a modifier character. This
650 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
651 if (*LastEmitted == ':') {
652 ++LastEmitted; // Consume ':' character.
653 if (*LastEmitted == 0) {
654 std::cerr << "Bad ${:} expression in inline asm string: '"
655 << AsmStr << "'\n";
656 exit(1);
657 }
658
659 Modifier[0] = *LastEmitted;
660 ++LastEmitted; // Consume modifier character.
661 }
662
Chris Lattneraa23fa92006-02-01 22:41:11 +0000663 if (*LastEmitted != '}') {
664 std::cerr << "Bad ${} expression in inline asm string: '"
665 << AsmStr << "'\n";
666 exit(1);
667 }
668 ++LastEmitted; // Consume '}' character.
669 }
670
671 if ((unsigned)Val >= NumOperands-1) {
672 std::cerr << "Invalid $ operand number in inline asm string: '"
673 << AsmStr << "'\n";
674 exit(1);
675 }
676
Chris Lattner571d9642006-02-23 19:21:04 +0000677 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000678 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000679 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
680 unsigned OpNo = 1;
681
682 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000683 for (; Val; --Val) {
684 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
685 OpNo += (OpFlags >> 3) + 1;
686 }
Chris Lattner571d9642006-02-23 19:21:04 +0000687
Chris Lattner1d08c652006-02-24 20:21:58 +0000688 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000689 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000690
691 bool Error;
692 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
693 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
694 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
695 Modifier[0] ? Modifier : 0);
696 } else {
697 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
698 Modifier[0] ? Modifier : 0);
699 }
700 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000701 std::cerr << "Invalid operand found in inline asm: '"
702 << AsmStr << "'\n";
703 MI->dump();
704 exit(1);
705 }
Chris Lattner571d9642006-02-23 19:21:04 +0000706 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000707 break;
708 }
709 case '{':
710 ++LastEmitted; // Consume '{' character.
711 if (CurVariant != -1) {
712 std::cerr << "Nested variants found in inline asm string: '"
713 << AsmStr << "'\n";
714 exit(1);
715 }
716 CurVariant = 0; // We're in the first variant now.
717 break;
718 case '|':
719 ++LastEmitted; // consume '|' character.
720 if (CurVariant == -1) {
721 std::cerr << "Found '|' character outside of variant in inline asm "
722 << "string: '" << AsmStr << "'\n";
723 exit(1);
724 }
725 ++CurVariant; // We're in the next variant.
726 break;
727 case '}':
728 ++LastEmitted; // consume '}' character.
729 if (CurVariant == -1) {
730 std::cerr << "Found '}' character outside of variant in inline asm "
731 << "string: '" << AsmStr << "'\n";
732 exit(1);
733 }
734 CurVariant = -1;
735 break;
736 }
737 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000738 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000739}
740
741/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
742/// instruction, using the specified assembler variant. Targets should
743/// overried this to format as appropriate.
744bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000745 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000746 // Target doesn't support this yet!
747 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000748}
Chris Lattner1d08c652006-02-24 20:21:58 +0000749
750bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
751 unsigned AsmVariant,
752 const char *ExtraCode) {
753 // Target doesn't support this yet!
754 return true;
755}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000756
757/// printBasicBlockLabel - This method prints the label for the specified
758/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000759void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
760 bool printColon,
761 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000762 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
763 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000764 if (printColon)
765 O << ':';
766 if (printComment)
767 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000768}