blob: bfc0519806c3edf5a4425446c76ecd8ba312699b [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"
Jim Laskeya6211dc2006-09-06 18:34:40 +000023#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Chris Lattner6a8e0f52004-08-16 23:15:22 +000025#include "llvm/Target/TargetMachine.h"
Duraid Madina26b037e2005-12-28 06:29:02 +000026#include <iostream>
Chris Lattneraa23fa92006-02-01 22:41:11 +000027#include <cerrno>
Chris Lattner6a8e0f52004-08-16 23:15:22 +000028using namespace llvm;
29
Jim Laskey261779b2006-09-07 22:06:40 +000030AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
31 const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000032: FunctionNumber(0), O(o), TM(tm), TAI(T)
33{}
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000034
35
Chris Lattner8488ba22006-05-09 04:59:56 +000036/// SwitchToTextSection - Switch to the specified text section of the executable
37/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000038///
Chris Lattner8488ba22006-05-09 04:59:56 +000039void AsmPrinter::SwitchToTextSection(const char *NewSection,
40 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000041 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000042 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000043 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000044 else
45 NS = NewSection;
46
47 // If we're already in this section, we're done.
48 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000049
Chris Lattner4ebc6a22006-05-09 05:33:48 +000050 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000051 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
52 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000053
Chris Lattner4ebc6a22006-05-09 05:33:48 +000054 CurrentSection = NS;
55
56 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000057 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000058}
59
Nate Begeman78756502006-07-27 01:13:04 +000060/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000061/// if we are not already in it!
62///
63void AsmPrinter::SwitchToDataSection(const char *NewSection,
64 const GlobalValue *GV) {
65 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000066 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000067 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000068 else
69 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000070
Chris Lattner6341df82006-05-09 05:23:12 +000071 // If we're already in this section, we're done.
72 if (CurrentSection == NS) return;
73
Chris Lattner4ebc6a22006-05-09 05:33:48 +000074 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000075 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
76 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000077
78 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000079
Chris Lattner4ebc6a22006-05-09 05:33:48 +000080 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000081 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000082}
83
84
Chris Lattner6a8e0f52004-08-16 23:15:22 +000085bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000086 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000087
Chris Lattner00fcdfe2006-01-24 04:16:34 +000088 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000089 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +000090 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +000091 << "\n" << TAI->getCommentString()
92 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000093
Chris Lattner8488ba22006-05-09 04:59:56 +000094 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +000095
96 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
97 DebugInfo->AnalyzeModule(M);
98 }
99
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000100 return false;
101}
102
103bool AsmPrinter::doFinalization(Module &M) {
104 delete Mang; Mang = 0;
105 return false;
106}
107
Chris Lattnerbb644e32005-11-21 07:51:36 +0000108void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000109 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000110 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000111 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000112}
113
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000114/// EmitConstantPool - Print to the current output stream assembly
115/// representations of the constants in the constant pool MCP. This is
116/// used to print out constants which have been "spilled to memory" by
117/// the code generator.
118///
119void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000120 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000121 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000122
123 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
124 // in special sections.
125 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
126 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
127 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
128 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000129 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000130 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000131 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000132 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000133 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000134 TM.getTargetData()->getTypeSize(Ty) == 4)
135 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000136 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000137 TM.getTargetData()->getTypeSize(Ty) == 8)
138 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000139 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000140 TM.getTargetData()->getTypeSize(Ty) == 16)
141 SixteenByteCPs.push_back(std::make_pair(CPE, i));
142 else
143 OtherCPs.push_back(std::make_pair(CPE, i));
144 }
145
146 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000147 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
148 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
149 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
150 SixteenByteCPs);
151 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000152}
153
154void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
155 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
156 if (CP.empty()) return;
157
158 SwitchToDataSection(Section, 0);
159 EmitAlignment(Alignment);
160 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000161 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
162 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000163 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
164 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000165 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000166 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000167 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000168 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000169 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000170 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000171 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000172 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000173 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000174 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000175 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000176 }
177}
178
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000179/// EmitJumpTableInfo - Print assembly representations of the jump tables used
180/// by the current function to the current output stream.
181///
182void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
183 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
184 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000185 const TargetData *TD = TM.getTargetData();
Nate Begemanefc312a2006-07-27 16:46:58 +0000186
187 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
188 // and 32 bits for PIC since PIC jump table entries are differences, not
189 // pointers to blocks.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000190 // Use the architecture specific relocation directive, if it is set
191 const char *JTEntryDirective = TAI->getJumpTableDirective();
192 if (!JTEntryDirective)
193 JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000194
Nate Begeman78756502006-07-27 01:13:04 +0000195 // Pick the directive to use to print the jump table entries, and switch to
196 // the appropriate section.
197 if (TM.getRelocationModel() == Reloc::PIC_) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000198 SwitchToTextSection(TAI->getJumpTableTextSection(), 0);
Nate Begeman78756502006-07-27 01:13:04 +0000199 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000200 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Nate Begeman78756502006-07-27 01:13:04 +0000201 if (TD->getPointerSize() == 8)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000202 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman78756502006-07-27 01:13:04 +0000203 }
Owen Anderson20a631f2006-05-03 01:29:57 +0000204 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000205
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000206 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000207 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
208
209 // For PIC codegen, if possible we want to use the SetDirective to reduce
210 // the number of relocations the assembler will generate for the jump table.
211 // Set directives are all printed before the jump table itself.
212 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeya6211dc2006-09-06 18:34:40 +0000213 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman984c1a42006-08-12 21:29:52 +0000214 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
215 if (EmittedSets.insert(JTBBs[ii]).second)
216 printSetLabel(i, JTBBs[ii]);
217
Jim Laskeya6211dc2006-09-06 18:34:40 +0000218 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
219 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000220
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000221 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000222 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000223 // If we have emitted set directives for the jump table entries, print
224 // them rather than the entries themselves. If we're emitting PIC, then
225 // emit the table entries as differences between two text section labels.
226 // If we're emitting non-PIC code, then emit the entries as direct
227 // references to the target basic blocks.
228 if (!EmittedSets.empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000229 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
230 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman984c1a42006-08-12 21:29:52 +0000231 } else if (TM.getRelocationModel() == Reloc::PIC_) {
232 printBasicBlockLabel(JTBBs[ii], false, false);
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000233 //If the arch uses custom Jump Table directives, don't calc relative to JT
234 if (!TAI->getJumpTableDirective())
235 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
236 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000237 } else {
238 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000239 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000240 O << '\n';
241 }
242 }
243}
244
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000245/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
246/// special global used by LLVM. If so, emit it and return true, otherwise
247/// do nothing and return false.
248bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000249 // Ignore debug and non-emitted data.
250 if (GV->getSection() == "llvm.metadata") return true;
251
252 if (!GV->hasAppendingLinkage()) return false;
253
254 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000255
Chris Lattner66af3902006-09-26 03:38:18 +0000256 if (GV->getName() == "llvm.used") {
257 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
258 EmitLLVMUsedList(GV->getInitializer());
259 return true;
260 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000261
Chris Lattner3760e902006-01-12 19:17:23 +0000262 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000263 SwitchToDataSection(TAI->getStaticCtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000264 EmitAlignment(2, 0);
265 EmitXXStructorList(GV->getInitializer());
266 return true;
267 }
268
Chris Lattner3760e902006-01-12 19:17:23 +0000269 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000270 SwitchToDataSection(TAI->getStaticDtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000271 EmitAlignment(2, 0);
272 EmitXXStructorList(GV->getInitializer());
273 return true;
274 }
275
276 return false;
277}
278
Chris Lattner66af3902006-09-26 03:38:18 +0000279/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
280/// global in the specified llvm.used list as being used with this directive.
281void AsmPrinter::EmitLLVMUsedList(Constant *List) {
282 const char *Directive = TAI->getUsedDirective();
283
284 // Should be an array of 'sbyte*'.
285 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
286 if (InitList == 0) return;
287
288 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
289 O << Directive;
290 EmitConstantValueOnly(InitList->getOperand(i));
291 O << "\n";
292 }
293}
294
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000295/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
296/// function pointers, ignoring the init priority.
297void AsmPrinter::EmitXXStructorList(Constant *List) {
298 // Should be an array of '{ int, void ()* }' structs. The first value is the
299 // init priority, which we ignore.
300 if (!isa<ConstantArray>(List)) return;
301 ConstantArray *InitList = cast<ConstantArray>(List);
302 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
303 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
304 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000305
306 if (CS->getOperand(1)->isNullValue())
307 return; // Found a null terminator, exit printing.
308 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000309 EmitGlobalConstant(CS->getOperand(1));
310 }
311}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000312
Chris Lattnera9b25252006-02-05 01:29:18 +0000313/// getPreferredAlignmentLog - Return the preferred alignment of the
314/// specified global, returned in log form. This includes an explicitly
315/// requested alignment (if the global has one).
316unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskeydce07562006-06-15 13:10:58 +0000317 const Type *ElemType = GV->getType()->getElementType();
318 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattnera9b25252006-02-05 01:29:18 +0000319 if (GV->getAlignment() > (1U << Alignment))
320 Alignment = Log2_32(GV->getAlignment());
321
Chris Lattnercbab2842006-02-05 01:46:49 +0000322 if (GV->hasInitializer()) {
Jim Laskey3519b872006-06-15 19:37:14 +0000323 // Always round up alignment of global doubles to 8 bytes.
324 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
325 Alignment = 3;
Chris Lattnercbab2842006-02-05 01:46:49 +0000326 if (Alignment < 4) {
327 // If the global is not external, see if it is large. If so, give it a
328 // larger alignment.
Jim Laskeydce07562006-06-15 13:10:58 +0000329 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000330 Alignment = 4; // 16-byte alignment.
331 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000332 }
333 return Alignment;
334}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000335
Chris Lattnerbb644e32005-11-21 07:51:36 +0000336// EmitAlignment - Emit an alignment directive to the specified power of two.
337void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000338 if (GV && GV->getAlignment())
339 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000340 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000341 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
342 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000343}
344
Chris Lattnerbb644e32005-11-21 07:51:36 +0000345/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000346///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000347void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000348 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000349 if (TAI->getZeroDirective()) {
350 O << TAI->getZeroDirective() << NumZeros;
351 if (TAI->getZeroDirectiveSuffix())
352 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000353 O << "\n";
354 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000355 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000356 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000357 }
358 }
359}
360
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000361// Print out the specified constant, without a storage class. Only the
362// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000363void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000364 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000365 O << "0";
366 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
367 assert(CB == ConstantBool::True);
368 O << "1";
369 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
370 if (((CI->getValue() << 32) >> 32) == CI->getValue())
371 O << CI->getValue();
372 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000373 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000374 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
375 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000376 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000377 // This is a constant address for a global variable or function. Use the
378 // name of the variable or function as the address value, possibly
379 // decorating it with GlobalVarAddrPrefix/Suffix or
380 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000381 if (isa<Function>(GV)) {
382 O << TAI->getFunctionAddrPrefix()
383 << Mang->getValueName(GV)
384 << TAI->getFunctionAddrSuffix();
385 } else {
386 O << TAI->getGlobalVarAddrPrefix()
387 << Mang->getValueName(GV)
388 << TAI->getGlobalVarAddrSuffix();
389 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000390 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000391 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000392 switch(CE->getOpcode()) {
393 case Instruction::GetElementPtr: {
394 // generate a symbolic expression for the byte address
395 const Constant *ptrVal = CE->getOperand(0);
396 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000397 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000398 if (Offset)
399 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000400 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000401 if (Offset > 0)
402 O << ") + " << Offset;
403 else if (Offset < 0)
404 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000405 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000406 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000407 }
408 break;
409 }
410 case Instruction::Cast: {
Chris Lattner85492422006-07-29 01:57:19 +0000411 // Support only foldable casts to/from pointers that can be eliminated by
412 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000413 Constant *Op = CE->getOperand(0);
414 const Type *OpTy = Op->getType(), *Ty = CE->getType();
415
Chris Lattner85492422006-07-29 01:57:19 +0000416 // Handle casts to pointers by changing them into casts to the appropriate
417 // integer type. This promotes constant folding and simplifies this code.
418 if (isa<PointerType>(Ty)) {
419 const Type *IntPtrTy = TD->getIntPtrType();
420 Op = ConstantExpr::getCast(Op, IntPtrTy);
421 return EmitConstantValueOnly(Op);
422 }
423
424 // We know the dest type is not a pointer. Is the src value a pointer or
425 // integral?
426 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
427 // We can emit the pointer value into this slot if the slot is an
428 // integer slot greater or equal to the size of the pointer.
429 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
430 return EmitConstantValueOnly(Op);
431 }
432
433 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000434 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000435 break;
436 }
437 case Instruction::Add:
438 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000439 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000440 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000441 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000442 O << ")";
443 break;
444 default:
445 assert(0 && "Unsupported operator!");
446 }
447 } else {
448 assert(0 && "Unknown constant value!");
449 }
450}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000451
452/// toOctal - Convert the low order bits of X into an octal digit.
453///
454static inline char toOctal(int X) {
455 return (X&7)+'0';
456}
457
Chris Lattner55a6d902005-11-10 18:06:33 +0000458/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000459/// the predicate isString is true.
460///
Chris Lattner55a6d902005-11-10 18:06:33 +0000461static void printAsCString(std::ostream &O, const ConstantArray *CVA,
462 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000463 assert(CVA->isString() && "Array is not string compatible!");
464
465 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000466 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000467 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000468 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000469
470 if (C == '"') {
471 O << "\\\"";
472 } else if (C == '\\') {
473 O << "\\\\";
474 } else if (isprint(C)) {
475 O << C;
476 } else {
477 switch(C) {
478 case '\b': O << "\\b"; break;
479 case '\f': O << "\\f"; break;
480 case '\n': O << "\\n"; break;
481 case '\r': O << "\\r"; break;
482 case '\t': O << "\\t"; break;
483 default:
484 O << '\\';
485 O << toOctal(C >> 6);
486 O << toOctal(C >> 3);
487 O << toOctal(C >> 0);
488 break;
489 }
490 }
491 }
492 O << "\"";
493}
494
Jeff Cohen24a62a92006-05-02 01:16:28 +0000495/// EmitString - Emit a zero-byte-terminated string constant.
496///
497void AsmPrinter::EmitString(const ConstantArray *CVA) const {
498 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000499 if (TAI->getAscizDirective() && NumElts &&
Jeff Cohen24a62a92006-05-02 01:16:28 +0000500 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000501 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000502 printAsCString(O, CVA, NumElts-1);
503 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000504 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000505 printAsCString(O, CVA, NumElts);
506 }
507 O << "\n";
508}
509
Chris Lattnerbb644e32005-11-21 07:51:36 +0000510/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000511///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000512void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000513 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000514
Chris Lattner61753bf2004-10-16 18:19:26 +0000515 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000516 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000517 return;
518 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
519 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000520 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000521 } else { // Not a string. Print the values in successive locations
522 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000523 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000524 }
525 return;
526 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
527 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000528 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000529 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000530 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
531 const Constant* field = CVS->getOperand(i);
532
533 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000534 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000535 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000536 : cvsLayout->MemberOffsets[i+1])
537 - cvsLayout->MemberOffsets[i]) - fieldSize;
538 sizeSoFar += fieldSize + padSize;
539
540 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000541 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000542
543 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000544 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000545 }
546 assert(sizeSoFar == cvsLayout->StructSize &&
547 "Layout of constant struct may be incorrect!");
548 return;
549 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
550 // FP Constants are printed as integer constants to avoid losing
551 // precision...
552 double Val = CFP->getValue();
553 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000554 if (TAI->getData64bitsDirective())
555 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
556 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000557 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000558 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
559 << "\t" << TAI->getCommentString()
560 << " double most significant word " << Val << "\n";
561 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
562 << "\t" << TAI->getCommentString()
563 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000564 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000565 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
566 << "\t" << TAI->getCommentString()
567 << " double least significant word " << Val << "\n";
568 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
569 << "\t" << TAI->getCommentString()
570 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000571 }
572 return;
573 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000574 O << TAI->getData32bitsDirective() << FloatToBits(Val)
575 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000576 return;
577 }
578 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
579 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
580 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000581
Jim Laskeya6211dc2006-09-06 18:34:40 +0000582 if (TAI->getData64bitsDirective())
583 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000584 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000585 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
586 << "\t" << TAI->getCommentString()
587 << " Double-word most significant word " << Val << "\n";
588 O << TAI->getData32bitsDirective() << unsigned(Val)
589 << "\t" << TAI->getCommentString()
590 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000591 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000592 O << TAI->getData32bitsDirective() << unsigned(Val)
593 << "\t" << TAI->getCommentString()
594 << " Double-word least significant word " << Val << "\n";
595 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
596 << "\t" << TAI->getCommentString()
597 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000598 }
599 return;
600 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000601 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
602 const PackedType *PTy = CP->getType();
603
604 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
605 EmitGlobalConstant(CP->getOperand(I));
606
607 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000608 }
609
610 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000611 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000612 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000613 O << "\n";
614}
Chris Lattner061d9e22006-01-27 02:10:10 +0000615
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000616void
617AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
618 // Target doesn't support this yet!
619 abort();
620}
621
Chris Lattner061d9e22006-01-27 02:10:10 +0000622/// printInlineAsm - This method formats and prints the specified machine
623/// instruction that is an inline asm.
624void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000625 unsigned NumOperands = MI->getNumOperands();
626
627 // Count the number of register definitions.
628 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000629 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
630 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000631 assert(NumDefs != NumOperands-1 && "No asm string?");
632
633 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000634
635 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000636 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000637
Chris Lattner3390cbe2006-07-28 00:17:20 +0000638 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
639 if (AsmStr[0] == 0) {
640 O << "\n"; // Tab already printed, avoid double indenting next instr.
641 return;
642 }
643
Jim Laskeya6211dc2006-09-06 18:34:40 +0000644 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000645
Chris Lattneraa23fa92006-02-01 22:41:11 +0000646 // The variant of the current asmprinter: FIXME: change.
647 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000648
Chris Lattneraa23fa92006-02-01 22:41:11 +0000649 int CurVariant = -1; // The number of the {.|.|.} region we are in.
650 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000651
Chris Lattneraa23fa92006-02-01 22:41:11 +0000652 while (*LastEmitted) {
653 switch (*LastEmitted) {
654 default: {
655 // Not a special case, emit the string section literally.
656 const char *LiteralEnd = LastEmitted+1;
657 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000658 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000659 ++LiteralEnd;
660 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
661 O.write(LastEmitted, LiteralEnd-LastEmitted);
662 LastEmitted = LiteralEnd;
663 break;
664 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000665 case '\n':
666 ++LastEmitted; // Consume newline character.
667 O << "\n\t"; // Indent code with newline.
668 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000669 case '$': {
670 ++LastEmitted; // Consume '$' character.
671 if (*LastEmitted == '$') { // $$ -> $
672 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
673 O << '$';
674 ++LastEmitted; // Consume second '$' character.
675 break;
676 }
677
678 bool HasCurlyBraces = false;
679 if (*LastEmitted == '{') { // ${variable}
680 ++LastEmitted; // Consume '{' character.
681 HasCurlyBraces = true;
682 }
683
684 const char *IDStart = LastEmitted;
685 char *IDEnd;
686 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
687 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
688 std::cerr << "Bad $ operand number in inline asm string: '"
689 << AsmStr << "'\n";
690 exit(1);
691 }
692 LastEmitted = IDEnd;
693
Chris Lattner34f74c12006-02-06 22:17:23 +0000694 char Modifier[2] = { 0, 0 };
695
Chris Lattneraa23fa92006-02-01 22:41:11 +0000696 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000697 // If we have curly braces, check for a modifier character. This
698 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
699 if (*LastEmitted == ':') {
700 ++LastEmitted; // Consume ':' character.
701 if (*LastEmitted == 0) {
702 std::cerr << "Bad ${:} expression in inline asm string: '"
703 << AsmStr << "'\n";
704 exit(1);
705 }
706
707 Modifier[0] = *LastEmitted;
708 ++LastEmitted; // Consume modifier character.
709 }
710
Chris Lattneraa23fa92006-02-01 22:41:11 +0000711 if (*LastEmitted != '}') {
712 std::cerr << "Bad ${} expression in inline asm string: '"
713 << AsmStr << "'\n";
714 exit(1);
715 }
716 ++LastEmitted; // Consume '}' character.
717 }
718
719 if ((unsigned)Val >= NumOperands-1) {
720 std::cerr << "Invalid $ operand number in inline asm string: '"
721 << AsmStr << "'\n";
722 exit(1);
723 }
724
Chris Lattner571d9642006-02-23 19:21:04 +0000725 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000726 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000727 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
728 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000729
730 bool Error = false;
731
Chris Lattner571d9642006-02-23 19:21:04 +0000732 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000733 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000734 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000735 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
736 OpNo += (OpFlags >> 3) + 1;
737 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000738
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000739 if (OpNo >= MI->getNumOperands()) {
740 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000741 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000742 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
743 ++OpNo; // Skip over the ID number.
744
745 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
746 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
747 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
748 Modifier[0] ? Modifier : 0);
749 } else {
750 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
751 Modifier[0] ? Modifier : 0);
752 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000753 }
754 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000755 std::cerr << "Invalid operand found in inline asm: '"
756 << AsmStr << "'\n";
757 MI->dump();
758 exit(1);
759 }
Chris Lattner571d9642006-02-23 19:21:04 +0000760 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000761 break;
762 }
763 case '{':
764 ++LastEmitted; // Consume '{' character.
765 if (CurVariant != -1) {
766 std::cerr << "Nested variants found in inline asm string: '"
767 << AsmStr << "'\n";
768 exit(1);
769 }
770 CurVariant = 0; // We're in the first variant now.
771 break;
772 case '|':
773 ++LastEmitted; // consume '|' character.
774 if (CurVariant == -1) {
775 std::cerr << "Found '|' character outside of variant in inline asm "
776 << "string: '" << AsmStr << "'\n";
777 exit(1);
778 }
779 ++CurVariant; // We're in the next variant.
780 break;
781 case '}':
782 ++LastEmitted; // consume '}' character.
783 if (CurVariant == -1) {
784 std::cerr << "Found '}' character outside of variant in inline asm "
785 << "string: '" << AsmStr << "'\n";
786 exit(1);
787 }
788 CurVariant = -1;
789 break;
790 }
791 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000792 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000793}
794
795/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
796/// instruction, using the specified assembler variant. Targets should
797/// overried this to format as appropriate.
798bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000799 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000800 // Target doesn't support this yet!
801 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000802}
Chris Lattner1d08c652006-02-24 20:21:58 +0000803
804bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
805 unsigned AsmVariant,
806 const char *ExtraCode) {
807 // Target doesn't support this yet!
808 return true;
809}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000810
811/// printBasicBlockLabel - This method prints the label for the specified
812/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000813void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
814 bool printColon,
815 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000816 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +0000817 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000818 if (printColon)
819 O << ':';
820 if (printComment)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000821 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000822}
Nate Begeman984c1a42006-08-12 21:29:52 +0000823
824/// printSetLabel - This method prints a set label for the specified
825/// MachineBasicBlock
826void AsmPrinter::printSetLabel(unsigned uid,
827 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000828 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +0000829 return;
830
Jim Laskeya6211dc2006-09-06 18:34:40 +0000831 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
832 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +0000833 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000834 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +0000835 << '_' << uid << '\n';
836}
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000837
838/// printDataDirective - This method prints the asm directive for the
839/// specified type.
840void AsmPrinter::printDataDirective(const Type *type) {
841 const TargetData *TD = TM.getTargetData();
842 switch (type->getTypeID()) {
843 case Type::BoolTyID:
844 case Type::UByteTyID: case Type::SByteTyID:
845 O << TAI->getData8bitsDirective();
846 break;
847 case Type::UShortTyID: case Type::ShortTyID:
848 O << TAI->getData16bitsDirective();
849 break;
850 case Type::PointerTyID:
851 if (TD->getPointerSize() == 8) {
852 assert(TAI->getData64bitsDirective() &&
853 "Target cannot handle 64-bit pointer exprs!");
854 O << TAI->getData64bitsDirective();
855 break;
856 }
857 //Fall through for pointer size == int size
858 case Type::UIntTyID: case Type::IntTyID:
859 O << TAI->getData32bitsDirective();
860 break;
861 case Type::ULongTyID: case Type::LongTyID:
862 assert(TAI->getData64bitsDirective() &&
863 "Target cannot handle 64-bit constant exprs!");
864 O << TAI->getData64bitsDirective();
865 break;
866 case Type::FloatTyID: case Type::DoubleTyID:
867 assert (0 && "Should have already output floating point constant.");
868 default:
869 assert (0 && "Can't handle printing this type of thing");
870 break;
871 }
872}