blob: dda8fd0d0545db6de355ec0cdb5df876aef202dd [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 Lattnere64f7642006-05-09 05:15:58 +000082 if (!CurrentSection.empty())
83 O << CurrentSection << "\tsegment 'CODE'\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000084 }
85 } else {
86 if (GV && GV->hasSection())
87 NS = SwitchToSectionDirective + GV->getSection();
88 else
89 NS = std::string("\t")+NewSection;
90
91 if (CurrentSection != NS) {
92 CurrentSection = NS;
93 if (!CurrentSection.empty())
94 O << CurrentSection << '\n';
95 }
Chris Lattner2ea5c992005-11-21 07:06:27 +000096 }
97}
98
Chris Lattner8488ba22006-05-09 04:59:56 +000099/// SwitchToTextSection - Switch to the specified text section of the executable
100/// if we are not already in it!
101///
102void AsmPrinter::SwitchToDataSection(const char *NewSection,
103 const GlobalValue *GV) {
104 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +0000105 if (GV && GV->hasSection())
106 NS = SwitchToSectionDirective + GV->getSection();
107 else
108 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +0000109
Chris Lattner6341df82006-05-09 05:23:12 +0000110 // If we're already in this section, we're done.
111 if (CurrentSection == NS) return;
112
Chris Lattner8488ba22006-05-09 04:59:56 +0000113 // Microsoft ML/MASM has a fundamentally different approach to handling
114 // sections.
115
116 if (MLSections) {
Chris Lattner6341df82006-05-09 05:23:12 +0000117 if (!CurrentSection.empty())
118 O << CurrentSection << "\tends\n\n";
119 CurrentSection = NS;
120 if (!CurrentSection.empty())
121 O << CurrentSection << "\tsegment 'DATA'\n";
Chris Lattner8488ba22006-05-09 04:59:56 +0000122 } else {
Chris Lattner6341df82006-05-09 05:23:12 +0000123 CurrentSection = NS;
124 if (!CurrentSection.empty())
125 O << CurrentSection << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +0000126 }
127}
128
129
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000130bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner6d42cbd2004-08-17 06:06:19 +0000131 Mang = new Mangler(M, GlobalPrefix);
Chris Lattnere3a79262006-01-23 23:47:53 +0000132
Chris Lattner00fcdfe2006-01-24 04:16:34 +0000133 if (!M.getModuleInlineAsm().empty())
134 O << CommentString << " Start of file scope inline assembly\n"
135 << M.getModuleInlineAsm()
136 << "\n" << CommentString << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +0000137
Chris Lattner8488ba22006-05-09 04:59:56 +0000138 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +0000139
140 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
141 DebugInfo->AnalyzeModule(M);
142 }
143
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000144 return false;
145}
146
147bool AsmPrinter::doFinalization(Module &M) {
148 delete Mang; Mang = 0;
149 return false;
150}
151
Chris Lattnerbb644e32005-11-21 07:51:36 +0000152void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000153 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000154 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000155 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000156}
157
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000158/// EmitConstantPool - Print to the current output stream assembly
159/// representations of the constants in the constant pool MCP. This is
160/// used to print out constants which have been "spilled to memory" by
161/// the code generator.
162///
163void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000164 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000165 if (CP.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000166 const TargetData *TD = TM.getTargetData();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000167
Chris Lattner8488ba22006-05-09 04:59:56 +0000168 SwitchToDataSection(ConstantPoolSection, 0);
Chris Lattnerf6190822006-02-09 04:46:04 +0000169 EmitAlignment(MCP->getConstantPoolAlignment());
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000170 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000171 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << '_' << i
Evan Cheng38d5e762006-03-01 22:18:09 +0000172 << ":\t\t\t\t\t" << CommentString << " ";
173 WriteTypeSymbolic(O, CP[i].Val->getType(), 0) << '\n';
Chris Lattnerba972642006-02-09 04:22:52 +0000174 EmitGlobalConstant(CP[i].Val);
Chris Lattnerf6190822006-02-09 04:46:04 +0000175 if (i != e-1) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000176 unsigned EntSize = TM.getTargetData()->getTypeSize(CP[i].Val->getType());
Chris Lattnerf6190822006-02-09 04:46:04 +0000177 unsigned ValEnd = CP[i].Offset + EntSize;
178 // Emit inter-object padding for alignment.
179 EmitZeros(CP[i+1].Offset-ValEnd);
180 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000181 }
182}
183
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000184/// EmitJumpTableInfo - Print assembly representations of the jump tables used
185/// by the current function to the current output stream.
186///
187void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
188 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
189 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000190 const TargetData *TD = TM.getTargetData();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000191
192 // FIXME: someday we need to handle PIC jump tables
193 assert((TM.getRelocationModel() == Reloc::Static ||
194 TM.getRelocationModel() == Reloc::DynamicNoPIC) &&
195 "Unhandled relocation model emitting jump table information!");
196
Chris Lattner8488ba22006-05-09 04:59:56 +0000197 SwitchToDataSection(JumpTableSection, 0);
Owen Anderson20a631f2006-05-03 01:29:57 +0000198 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000199 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
200 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << '_' << i
201 << ":\n";
202 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
203 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
204 O << Data32bitsDirective << ' ';
205 printBasicBlockLabel(JTBBs[ii]);
206 O << '\n';
207 }
208 }
209}
210
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000211/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
212/// special global used by LLVM. If so, emit it and return true, otherwise
213/// do nothing and return false.
214bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000215 // Ignore debug and non-emitted data.
216 if (GV->getSection() == "llvm.metadata") return true;
217
218 if (!GV->hasAppendingLinkage()) return false;
219
220 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000221
222 if (GV->getName() == "llvm.used")
223 return true; // No need to emit this at all.
224
Chris Lattner3760e902006-01-12 19:17:23 +0000225 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000226 SwitchToDataSection(StaticCtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000227 EmitAlignment(2, 0);
228 EmitXXStructorList(GV->getInitializer());
229 return true;
230 }
231
Chris Lattner3760e902006-01-12 19:17:23 +0000232 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000233 SwitchToDataSection(StaticDtorsSection, 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000234 EmitAlignment(2, 0);
235 EmitXXStructorList(GV->getInitializer());
236 return true;
237 }
238
239 return false;
240}
241
242/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
243/// function pointers, ignoring the init priority.
244void AsmPrinter::EmitXXStructorList(Constant *List) {
245 // Should be an array of '{ int, void ()* }' structs. The first value is the
246 // init priority, which we ignore.
247 if (!isa<ConstantArray>(List)) return;
248 ConstantArray *InitList = cast<ConstantArray>(List);
249 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
250 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
251 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000252
253 if (CS->getOperand(1)->isNullValue())
254 return; // Found a null terminator, exit printing.
255 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000256 EmitGlobalConstant(CS->getOperand(1));
257 }
258}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000259
Chris Lattnera9b25252006-02-05 01:29:18 +0000260/// getPreferredAlignmentLog - Return the preferred alignment of the
261/// specified global, returned in log form. This includes an explicitly
262/// requested alignment (if the global has one).
263unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Owen Anderson20a631f2006-05-03 01:29:57 +0000264 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(GV->getType());
Chris Lattnera9b25252006-02-05 01:29:18 +0000265 if (GV->getAlignment() > (1U << Alignment))
266 Alignment = Log2_32(GV->getAlignment());
267
Chris Lattnercbab2842006-02-05 01:46:49 +0000268 if (GV->hasInitializer()) {
269 // Always round up alignment of global doubles to 8 bytes.
270 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
271 Alignment = 3;
272 if (Alignment < 4) {
273 // If the global is not external, see if it is large. If so, give it a
274 // larger alignment.
Owen Anderson20a631f2006-05-03 01:29:57 +0000275 if (TM.getTargetData()->getTypeSize(GV->getType()->getElementType()) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000276 Alignment = 4; // 16-byte alignment.
277 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000278 }
279 return Alignment;
280}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000281
Chris Lattnerbb644e32005-11-21 07:51:36 +0000282// EmitAlignment - Emit an alignment directive to the specified power of two.
283void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000284 if (GV && GV->getAlignment())
285 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000286 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner1d35c162004-08-17 19:14:29 +0000287 if (AlignmentIsInBytes) NumBits = 1 << NumBits;
288 O << AlignDirective << NumBits << "\n";
289}
290
Chris Lattnerbb644e32005-11-21 07:51:36 +0000291/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000292///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000293void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000294 if (NumZeros) {
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000295 if (ZeroDirective) {
296 O << ZeroDirective << NumZeros;
297 if (ZeroDirectiveSuffix)
298 O << ZeroDirectiveSuffix;
299 O << "\n";
300 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000301 for (; NumZeros; --NumZeros)
302 O << Data8bitsDirective << "0\n";
303 }
304 }
305}
306
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000307// Print out the specified constant, without a storage class. Only the
308// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000309void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000310 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000311 O << "0";
312 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
313 assert(CB == ConstantBool::True);
314 O << "1";
315 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
316 if (((CI->getValue() << 32) >> 32) == CI->getValue())
317 O << CI->getValue();
318 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000319 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000320 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
321 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000322 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000323 // This is a constant address for a global variable or function. Use the
324 // name of the variable or function as the address value, possibly
325 // decorating it with GlobalVarAddrPrefix/Suffix or
326 // FunctionAddrPrefix/Suffix (these all default to "" )
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000327 if (isa<Function>(GV))
328 O << FunctionAddrPrefix << Mang->getValueName(GV) << FunctionAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000329 else
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000330 O << GlobalVarAddrPrefix << Mang->getValueName(GV) << GlobalVarAddrSuffix;
Duraid Madina73a316d2005-04-02 12:21:51 +0000331 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000332 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000333 switch(CE->getOpcode()) {
334 case Instruction::GetElementPtr: {
335 // generate a symbolic expression for the byte address
336 const Constant *ptrVal = CE->getOperand(0);
337 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000338 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000339 if (Offset)
340 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000341 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000342 if (Offset > 0)
343 O << ") + " << Offset;
344 else if (Offset < 0)
345 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000346 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000347 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000348 }
349 break;
350 }
351 case Instruction::Cast: {
352 // Support only non-converting or widening casts for now, that is, ones
353 // that do not involve a change in value. This assertion is really gross,
354 // and may not even be a complete check.
355 Constant *Op = CE->getOperand(0);
356 const Type *OpTy = Op->getType(), *Ty = CE->getType();
357
358 // Remember, kids, pointers can be losslessly converted back and forth
359 // into 32-bit or wider integers, regardless of signedness. :-P
360 assert(((isa<PointerType>(OpTy)
361 && (Ty == Type::LongTy || Ty == Type::ULongTy
362 || Ty == Type::IntTy || Ty == Type::UIntTy))
363 || (isa<PointerType>(Ty)
364 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
365 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Owen Anderson20a631f2006-05-03 01:29:57 +0000366 || (((TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000367 && OpTy->isLosslesslyConvertibleTo(Ty))))
368 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000369 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000370 break;
371 }
372 case Instruction::Add:
373 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000374 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000375 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000376 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000377 O << ")";
378 break;
379 default:
380 assert(0 && "Unsupported operator!");
381 }
382 } else {
383 assert(0 && "Unknown constant value!");
384 }
385}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000386
387/// toOctal - Convert the low order bits of X into an octal digit.
388///
389static inline char toOctal(int X) {
390 return (X&7)+'0';
391}
392
Chris Lattner55a6d902005-11-10 18:06:33 +0000393/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000394/// the predicate isString is true.
395///
Chris Lattner55a6d902005-11-10 18:06:33 +0000396static void printAsCString(std::ostream &O, const ConstantArray *CVA,
397 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000398 assert(CVA->isString() && "Array is not string compatible!");
399
400 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000401 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000402 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000403 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000404
405 if (C == '"') {
406 O << "\\\"";
407 } else if (C == '\\') {
408 O << "\\\\";
409 } else if (isprint(C)) {
410 O << C;
411 } else {
412 switch(C) {
413 case '\b': O << "\\b"; break;
414 case '\f': O << "\\f"; break;
415 case '\n': O << "\\n"; break;
416 case '\r': O << "\\r"; break;
417 case '\t': O << "\\t"; break;
418 default:
419 O << '\\';
420 O << toOctal(C >> 6);
421 O << toOctal(C >> 3);
422 O << toOctal(C >> 0);
423 break;
424 }
425 }
426 }
427 O << "\"";
428}
429
Jeff Cohen24a62a92006-05-02 01:16:28 +0000430/// EmitString - Emit a zero-byte-terminated string constant.
431///
432void AsmPrinter::EmitString(const ConstantArray *CVA) const {
433 unsigned NumElts = CVA->getNumOperands();
434 if (AscizDirective && NumElts &&
435 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
436 O << AscizDirective;
437 printAsCString(O, CVA, NumElts-1);
438 } else {
439 O << AsciiDirective;
440 printAsCString(O, CVA, NumElts);
441 }
442 O << "\n";
443}
444
Chris Lattnerbb644e32005-11-21 07:51:36 +0000445/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000446///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000447void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000448 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000449
Chris Lattner61753bf2004-10-16 18:19:26 +0000450 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000451 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000452 return;
453 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
454 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000455 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000456 } else { // Not a string. Print the values in successive locations
457 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000458 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000459 }
460 return;
461 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
462 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000463 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000464 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000465 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
466 const Constant* field = CVS->getOperand(i);
467
468 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000469 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000470 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000471 : cvsLayout->MemberOffsets[i+1])
472 - cvsLayout->MemberOffsets[i]) - fieldSize;
473 sizeSoFar += fieldSize + padSize;
474
475 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000476 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000477
478 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000479 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000480 }
481 assert(sizeSoFar == cvsLayout->StructSize &&
482 "Layout of constant struct may be incorrect!");
483 return;
484 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
485 // FP Constants are printed as integer constants to avoid losing
486 // precision...
487 double Val = CFP->getValue();
488 if (CFP->getType() == Type::DoubleTy) {
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000489 if (Data64bitsDirective)
Jim Laskeyb74c6662005-08-17 19:34:49 +0000490 O << Data64bitsDirective << DoubleToBits(Val) << "\t" << CommentString
Chris Lattnerea751992004-08-17 21:38:40 +0000491 << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000492 else if (TD->isBigEndian()) {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000493 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000494 << "\t" << CommentString << " double most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000495 << Val << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000496 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000497 << "\t" << CommentString << " double least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000498 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000499 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000500 O << Data32bitsDirective << unsigned(DoubleToBits(Val))
Chris Lattner10262ab2004-08-18 02:22:55 +0000501 << "\t" << CommentString << " double least significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000502 << "\n";
Jim Laskeyb74c6662005-08-17 19:34:49 +0000503 O << Data32bitsDirective << unsigned(DoubleToBits(Val) >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000504 << "\t" << CommentString << " double most significant word " << Val
Chris Lattnerda6beac2004-08-17 16:27:05 +0000505 << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000506 }
507 return;
508 } else {
Jim Laskeyb74c6662005-08-17 19:34:49 +0000509 O << Data32bitsDirective << FloatToBits(Val) << "\t" << CommentString
Chris Lattnerda6beac2004-08-17 16:27:05 +0000510 << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000511 return;
512 }
513 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
514 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
515 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000516
Chris Lattner9fa0fc42004-08-17 06:48:16 +0000517 if (Data64bitsDirective)
518 O << Data64bitsDirective << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000519 else if (TD->isBigEndian()) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000520 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000521 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000522 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000523 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000524 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000525 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000526 } else {
527 O << Data32bitsDirective << unsigned(Val)
Chris Lattner10262ab2004-08-18 02:22:55 +0000528 << "\t" << CommentString << " Double-word least significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000529 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000530 O << Data32bitsDirective << unsigned(Val >> 32)
Chris Lattner10262ab2004-08-18 02:22:55 +0000531 << "\t" << CommentString << " Double-word most significant word "
Chris Lattnerda6beac2004-08-17 16:27:05 +0000532 << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000533 }
534 return;
535 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000536 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
537 const PackedType *PTy = CP->getType();
538
539 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
540 EmitGlobalConstant(CP->getOperand(I));
541
542 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000543 }
544
545 const Type *type = CV->getType();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000546 switch (type->getTypeID()) {
Misha Brukman835702a2005-04-21 22:36:52 +0000547 case Type::BoolTyID:
Chris Lattner8452a1f2004-08-17 06:36:49 +0000548 case Type::UByteTyID: case Type::SByteTyID:
549 O << Data8bitsDirective;
550 break;
551 case Type::UShortTyID: case Type::ShortTyID:
552 O << Data16bitsDirective;
553 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000554 case Type::PointerTyID:
Owen Anderson20a631f2006-05-03 01:29:57 +0000555 if (TD->getPointerSize() == 8) {
Andrew Lenharthc8770aa2005-02-04 13:47:16 +0000556 O << Data64bitsDirective;
557 break;
558 }
559 //Fall through for pointer size == int size
Chris Lattner8452a1f2004-08-17 06:36:49 +0000560 case Type::UIntTyID: case Type::IntTyID:
561 O << Data32bitsDirective;
562 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000563 case Type::ULongTyID: case Type::LongTyID:
Chris Lattner88e2d2e2005-08-08 04:26:32 +0000564 assert(Data64bitsDirective &&"Target cannot handle 64-bit constant exprs!");
565 O << Data64bitsDirective;
566 break;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000567 case Type::FloatTyID: case Type::DoubleTyID:
568 assert (0 && "Should have already output floating point constant.");
569 default:
570 assert (0 && "Can't handle printing this type of thing");
571 break;
572 }
Chris Lattnerbb644e32005-11-21 07:51:36 +0000573 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000574 O << "\n";
575}
Chris Lattner061d9e22006-01-27 02:10:10 +0000576
577/// printInlineAsm - This method formats and prints the specified machine
578/// instruction that is an inline asm.
579void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnera633c3132006-05-05 21:47:05 +0000580 O << InlineAsmStart << "\n\t";
Chris Lattner57ecb562006-01-30 23:00:08 +0000581 unsigned NumOperands = MI->getNumOperands();
582
583 // Count the number of register definitions.
584 unsigned NumDefs = 0;
585 for (; MI->getOperand(NumDefs).isDef(); ++NumDefs)
586 assert(NumDefs != NumOperands-1 && "No asm string?");
587
588 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000589
590 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000591 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000592
593 // The variant of the current asmprinter: FIXME: change.
594 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000595
Chris Lattneraa23fa92006-02-01 22:41:11 +0000596 int CurVariant = -1; // The number of the {.|.|.} region we are in.
597 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000598
Chris Lattneraa23fa92006-02-01 22:41:11 +0000599 while (*LastEmitted) {
600 switch (*LastEmitted) {
601 default: {
602 // Not a special case, emit the string section literally.
603 const char *LiteralEnd = LastEmitted+1;
604 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000605 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000606 ++LiteralEnd;
607 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
608 O.write(LastEmitted, LiteralEnd-LastEmitted);
609 LastEmitted = LiteralEnd;
610 break;
611 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000612 case '\n':
613 ++LastEmitted; // Consume newline character.
614 O << "\n\t"; // Indent code with newline.
615 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000616 case '$': {
617 ++LastEmitted; // Consume '$' character.
618 if (*LastEmitted == '$') { // $$ -> $
619 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
620 O << '$';
621 ++LastEmitted; // Consume second '$' character.
622 break;
623 }
624
625 bool HasCurlyBraces = false;
626 if (*LastEmitted == '{') { // ${variable}
627 ++LastEmitted; // Consume '{' character.
628 HasCurlyBraces = true;
629 }
630
631 const char *IDStart = LastEmitted;
632 char *IDEnd;
633 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
634 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
635 std::cerr << "Bad $ operand number in inline asm string: '"
636 << AsmStr << "'\n";
637 exit(1);
638 }
639 LastEmitted = IDEnd;
640
Chris Lattner34f74c12006-02-06 22:17:23 +0000641 char Modifier[2] = { 0, 0 };
642
Chris Lattneraa23fa92006-02-01 22:41:11 +0000643 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000644 // If we have curly braces, check for a modifier character. This
645 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
646 if (*LastEmitted == ':') {
647 ++LastEmitted; // Consume ':' character.
648 if (*LastEmitted == 0) {
649 std::cerr << "Bad ${:} expression in inline asm string: '"
650 << AsmStr << "'\n";
651 exit(1);
652 }
653
654 Modifier[0] = *LastEmitted;
655 ++LastEmitted; // Consume modifier character.
656 }
657
Chris Lattneraa23fa92006-02-01 22:41:11 +0000658 if (*LastEmitted != '}') {
659 std::cerr << "Bad ${} expression in inline asm string: '"
660 << AsmStr << "'\n";
661 exit(1);
662 }
663 ++LastEmitted; // Consume '}' character.
664 }
665
666 if ((unsigned)Val >= NumOperands-1) {
667 std::cerr << "Invalid $ operand number in inline asm string: '"
668 << AsmStr << "'\n";
669 exit(1);
670 }
671
Chris Lattner571d9642006-02-23 19:21:04 +0000672 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000673 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000674 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
675 unsigned OpNo = 1;
676
677 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000678 for (; Val; --Val) {
679 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
680 OpNo += (OpFlags >> 3) + 1;
681 }
Chris Lattner571d9642006-02-23 19:21:04 +0000682
Chris Lattner1d08c652006-02-24 20:21:58 +0000683 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
Chris Lattner571d9642006-02-23 19:21:04 +0000684 ++OpNo; // Skip over the ID number.
Chris Lattner1d08c652006-02-24 20:21:58 +0000685
686 bool Error;
687 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
688 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
689 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
690 Modifier[0] ? Modifier : 0);
691 } else {
692 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
693 Modifier[0] ? Modifier : 0);
694 }
695 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000696 std::cerr << "Invalid operand found in inline asm: '"
697 << AsmStr << "'\n";
698 MI->dump();
699 exit(1);
700 }
Chris Lattner571d9642006-02-23 19:21:04 +0000701 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000702 break;
703 }
704 case '{':
705 ++LastEmitted; // Consume '{' character.
706 if (CurVariant != -1) {
707 std::cerr << "Nested variants found in inline asm string: '"
708 << AsmStr << "'\n";
709 exit(1);
710 }
711 CurVariant = 0; // We're in the first variant now.
712 break;
713 case '|':
714 ++LastEmitted; // consume '|' character.
715 if (CurVariant == -1) {
716 std::cerr << "Found '|' character outside of variant in inline asm "
717 << "string: '" << AsmStr << "'\n";
718 exit(1);
719 }
720 ++CurVariant; // We're in the next variant.
721 break;
722 case '}':
723 ++LastEmitted; // consume '}' character.
724 if (CurVariant == -1) {
725 std::cerr << "Found '}' character outside of variant in inline asm "
726 << "string: '" << AsmStr << "'\n";
727 exit(1);
728 }
729 CurVariant = -1;
730 break;
731 }
732 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000733 O << "\n\t" << InlineAsmEnd << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000734}
735
736/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
737/// instruction, using the specified assembler variant. Targets should
738/// overried this to format as appropriate.
739bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000740 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000741 // Target doesn't support this yet!
742 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000743}
Chris Lattner1d08c652006-02-24 20:21:58 +0000744
745bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
746 unsigned AsmVariant,
747 const char *ExtraCode) {
748 // Target doesn't support this yet!
749 return true;
750}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000751
752/// printBasicBlockLabel - This method prints the label for the specified
753/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000754void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
755 bool printColon,
756 bool printComment) const {
Nate Begeman4971ba52006-05-02 17:36:46 +0000757 O << PrivateGlobalPrefix << "BB" << FunctionNumber << "_"
758 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000759 if (printColon)
760 O << ':';
761 if (printComment)
762 O << '\t' << CommentString << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000763}