blob: 247cceda7da9deb18453abd373b315b96d290667 [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
Chris Lattnerdc822412006-10-05 02:42:47 +000035std::string AsmPrinter::getSectionForFunction(const Function &F) const {
36 return TAI->getTextSection();
37}
38
Chris Lattnerf0e9aef2005-12-13 06:32:10 +000039
Chris Lattner8488ba22006-05-09 04:59:56 +000040/// SwitchToTextSection - Switch to the specified text section of the executable
41/// if we are not already in it!
Chris Lattner2ea5c992005-11-21 07:06:27 +000042///
Chris Lattner8488ba22006-05-09 04:59:56 +000043void AsmPrinter::SwitchToTextSection(const char *NewSection,
44 const GlobalValue *GV) {
Chris Lattner2ea5c992005-11-21 07:06:27 +000045 std::string NS;
Chris Lattner8c2bfc02006-05-09 05:24:50 +000046 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000047 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner8c2bfc02006-05-09 05:24:50 +000048 else
49 NS = NewSection;
50
51 // If we're already in this section, we're done.
52 if (CurrentSection == NS) return;
Jeff Cohen470f4312006-05-02 03:58:45 +000053
Chris Lattner4ebc6a22006-05-09 05:33:48 +000054 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000055 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
56 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Jeff Cohen470f4312006-05-02 03:58:45 +000057
Chris Lattner4ebc6a22006-05-09 05:33:48 +000058 CurrentSection = NS;
59
60 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000061 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Chris Lattner2ea5c992005-11-21 07:06:27 +000062}
63
Nate Begeman78756502006-07-27 01:13:04 +000064/// SwitchToDataSection - Switch to the specified data section of the executable
Chris Lattner8488ba22006-05-09 04:59:56 +000065/// if we are not already in it!
66///
67void AsmPrinter::SwitchToDataSection(const char *NewSection,
68 const GlobalValue *GV) {
69 std::string NS;
Chris Lattner6341df82006-05-09 05:23:12 +000070 if (GV && GV->hasSection())
Jim Laskeya6211dc2006-09-06 18:34:40 +000071 NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner6341df82006-05-09 05:23:12 +000072 else
73 NS = NewSection;
Chris Lattner8488ba22006-05-09 04:59:56 +000074
Chris Lattner6341df82006-05-09 05:23:12 +000075 // If we're already in this section, we're done.
76 if (CurrentSection == NS) return;
77
Chris Lattner4ebc6a22006-05-09 05:33:48 +000078 // Close the current section, if applicable.
Jim Laskeya6211dc2006-09-06 18:34:40 +000079 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
80 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << "\n";
Chris Lattner4ebc6a22006-05-09 05:33:48 +000081
82 CurrentSection = NS;
Chris Lattner8488ba22006-05-09 04:59:56 +000083
Chris Lattner4ebc6a22006-05-09 05:33:48 +000084 if (!CurrentSection.empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000085 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Chris Lattner8488ba22006-05-09 04:59:56 +000086}
87
88
Chris Lattner6a8e0f52004-08-16 23:15:22 +000089bool AsmPrinter::doInitialization(Module &M) {
Jim Laskeya6211dc2006-09-06 18:34:40 +000090 Mang = new Mangler(M, TAI->getGlobalPrefix());
Chris Lattnere3a79262006-01-23 23:47:53 +000091
Chris Lattner00fcdfe2006-01-24 04:16:34 +000092 if (!M.getModuleInlineAsm().empty())
Jim Laskeya6211dc2006-09-06 18:34:40 +000093 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner00fcdfe2006-01-24 04:16:34 +000094 << M.getModuleInlineAsm()
Jim Laskeya6211dc2006-09-06 18:34:40 +000095 << "\n" << TAI->getCommentString()
96 << " End of file scope inline assembly\n";
Chris Lattnere3a79262006-01-23 23:47:53 +000097
Chris Lattner8488ba22006-05-09 04:59:56 +000098 SwitchToDataSection("", 0); // Reset back to no section.
Jim Laskey0bbdc552006-01-26 20:21:46 +000099
100 if (MachineDebugInfo *DebugInfo = getAnalysisToUpdate<MachineDebugInfo>()) {
101 DebugInfo->AnalyzeModule(M);
102 }
103
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000104 return false;
105}
106
107bool AsmPrinter::doFinalization(Module &M) {
108 delete Mang; Mang = 0;
109 return false;
110}
111
Chris Lattnerbb644e32005-11-21 07:51:36 +0000112void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000113 // What's my mangled name?
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000114 CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner08adbd132005-11-21 08:13:27 +0000115 IncrementFunctionNumber();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000116}
117
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000118/// EmitConstantPool - Print to the current output stream assembly
119/// representations of the constants in the constant pool MCP. This is
120/// used to print out constants which have been "spilled to memory" by
121/// the code generator.
122///
123void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerba972642006-02-09 04:22:52 +0000124 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000125 if (CP.empty()) return;
Evan Chengec1d60b2006-06-29 00:26:09 +0000126
127 // Some targets require 4-, 8-, and 16- byte constant literals to be placed
128 // in special sections.
129 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
130 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
131 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
132 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000133 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000134 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Evan Chengec1d60b2006-06-29 00:26:09 +0000135 MachineConstantPoolEntry CPE = CP[i];
Evan Cheng2ad050f2006-09-14 07:35:00 +0000136 const Type *Ty = CPE.getType();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000137 if (TAI->getFourByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000138 TM.getTargetData()->getTypeSize(Ty) == 4)
139 FourByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000140 else if (TAI->getEightByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000141 TM.getTargetData()->getTypeSize(Ty) == 8)
142 EightByteCPs.push_back(std::make_pair(CPE, i));
Evan Cheng95b3dde2006-09-07 18:50:20 +0000143 else if (TAI->getSixteenByteConstantSection() &&
Evan Chengec1d60b2006-06-29 00:26:09 +0000144 TM.getTargetData()->getTypeSize(Ty) == 16)
145 SixteenByteCPs.push_back(std::make_pair(CPE, i));
146 else
147 OtherCPs.push_back(std::make_pair(CPE, i));
148 }
149
150 unsigned Alignment = MCP->getConstantPoolAlignment();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000151 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs);
152 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs);
153 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(),
154 SixteenByteCPs);
155 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs);
Evan Chengec1d60b2006-06-29 00:26:09 +0000156}
157
158void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section,
159 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) {
160 if (CP.empty()) return;
161
162 SwitchToDataSection(Section, 0);
163 EmitAlignment(Alignment);
164 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000165 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
166 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << " ";
Evan Cheng2ad050f2006-09-14 07:35:00 +0000167 WriteTypeSymbolic(O, CP[i].first.getType(), 0) << '\n';
168 if (CP[i].first.isMachineConstantPoolEntry())
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000169 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000170 else
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000171 EmitGlobalConstant(CP[i].first.Val.ConstVal);
Chris Lattnerf6190822006-02-09 04:46:04 +0000172 if (i != e-1) {
Evan Cheng2ad050f2006-09-14 07:35:00 +0000173 const Type *Ty = CP[i].first.getType();
Evan Chengec1d60b2006-06-29 00:26:09 +0000174 unsigned EntSize =
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000175 TM.getTargetData()->getTypeSize(Ty);
Evan Cheng2ad050f2006-09-14 07:35:00 +0000176 unsigned ValEnd = CP[i].first.getOffset() + EntSize;
Chris Lattnerf6190822006-02-09 04:46:04 +0000177 // Emit inter-object padding for alignment.
Evan Cheng2ad050f2006-09-14 07:35:00 +0000178 EmitZeros(CP[i+1].first.getOffset()-ValEnd);
Chris Lattnerf6190822006-02-09 04:46:04 +0000179 }
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000180 }
181}
182
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000183/// EmitJumpTableInfo - Print assembly representations of the jump tables used
184/// by the current function to the current output stream.
185///
186void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI) {
187 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
188 if (JT.empty()) return;
Owen Anderson20a631f2006-05-03 01:29:57 +0000189 const TargetData *TD = TM.getTargetData();
Nate Begemanefc312a2006-07-27 16:46:58 +0000190
191 // JTEntryDirective is a string to print sizeof(ptr) for non-PIC jump tables,
192 // and 32 bits for PIC since PIC jump table entries are differences, not
193 // pointers to blocks.
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000194 // Use the architecture specific relocation directive, if it is set
195 const char *JTEntryDirective = TAI->getJumpTableDirective();
196 if (!JTEntryDirective)
197 JTEntryDirective = TAI->getData32bitsDirective();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000198
Nate Begeman78756502006-07-27 01:13:04 +0000199 // Pick the directive to use to print the jump table entries, and switch to
200 // the appropriate section.
201 if (TM.getRelocationModel() == Reloc::PIC_) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000202 SwitchToTextSection(TAI->getJumpTableTextSection(), 0);
Nate Begeman78756502006-07-27 01:13:04 +0000203 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000204 SwitchToDataSection(TAI->getJumpTableDataSection(), 0);
Nate Begeman78756502006-07-27 01:13:04 +0000205 if (TD->getPointerSize() == 8)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000206 JTEntryDirective = TAI->getData64bitsDirective();
Nate Begeman78756502006-07-27 01:13:04 +0000207 }
Owen Anderson20a631f2006-05-03 01:29:57 +0000208 EmitAlignment(Log2_32(TD->getPointerAlignment()));
Chris Lattnerfd5a8eb2006-07-15 01:34:12 +0000209
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000210 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman984c1a42006-08-12 21:29:52 +0000211 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
212
213 // For PIC codegen, if possible we want to use the SetDirective to reduce
214 // the number of relocations the assembler will generate for the jump table.
215 // Set directives are all printed before the jump table itself.
216 std::set<MachineBasicBlock*> EmittedSets;
Jim Laskeya6211dc2006-09-06 18:34:40 +0000217 if (TAI->getSetDirective() && TM.getRelocationModel() == Reloc::PIC_)
Nate Begeman984c1a42006-08-12 21:29:52 +0000218 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
219 if (EmittedSets.insert(JTBBs[ii]).second)
220 printSetLabel(i, JTBBs[ii]);
221
Jim Laskeya6211dc2006-09-06 18:34:40 +0000222 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
223 << '_' << i << ":\n";
Nate Begeman984c1a42006-08-12 21:29:52 +0000224
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000225 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Nate Begemanefc312a2006-07-27 16:46:58 +0000226 O << JTEntryDirective << ' ';
Nate Begeman984c1a42006-08-12 21:29:52 +0000227 // If we have emitted set directives for the jump table entries, print
228 // them rather than the entries themselves. If we're emitting PIC, then
229 // emit the table entries as differences between two text section labels.
230 // If we're emitting non-PIC code, then emit the entries as direct
231 // references to the target basic blocks.
232 if (!EmittedSets.empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000233 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
234 << '_' << i << "_set_" << JTBBs[ii]->getNumber();
Nate Begeman984c1a42006-08-12 21:29:52 +0000235 } else if (TM.getRelocationModel() == Reloc::PIC_) {
236 printBasicBlockLabel(JTBBs[ii], false, false);
Andrew Lenharth783a4a92006-09-24 19:45:58 +0000237 //If the arch uses custom Jump Table directives, don't calc relative to JT
238 if (!TAI->getJumpTableDirective())
239 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
240 << getFunctionNumber() << '_' << i;
Nate Begeman984c1a42006-08-12 21:29:52 +0000241 } else {
242 printBasicBlockLabel(JTBBs[ii], false, false);
Nate Begeman78756502006-07-27 01:13:04 +0000243 }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000244 O << '\n';
245 }
246 }
247}
248
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000249/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
250/// special global used by LLVM. If so, emit it and return true, otherwise
251/// do nothing and return false.
252bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Jim Laskey313570f2006-03-07 22:00:35 +0000253 // Ignore debug and non-emitted data.
254 if (GV->getSection() == "llvm.metadata") return true;
255
256 if (!GV->hasAppendingLinkage()) return false;
257
258 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000259
Chris Lattner66af3902006-09-26 03:38:18 +0000260 if (GV->getName() == "llvm.used") {
261 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
262 EmitLLVMUsedList(GV->getInitializer());
263 return true;
264 }
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000265
Chris Lattner3760e902006-01-12 19:17:23 +0000266 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000267 SwitchToDataSection(TAI->getStaticCtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000268 EmitAlignment(2, 0);
269 EmitXXStructorList(GV->getInitializer());
270 return true;
271 }
272
Chris Lattner3760e902006-01-12 19:17:23 +0000273 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000274 SwitchToDataSection(TAI->getStaticDtorsSection(), 0);
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000275 EmitAlignment(2, 0);
276 EmitXXStructorList(GV->getInitializer());
277 return true;
278 }
279
280 return false;
281}
282
Chris Lattner66af3902006-09-26 03:38:18 +0000283/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
284/// global in the specified llvm.used list as being used with this directive.
285void AsmPrinter::EmitLLVMUsedList(Constant *List) {
286 const char *Directive = TAI->getUsedDirective();
287
288 // Should be an array of 'sbyte*'.
289 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
290 if (InitList == 0) return;
291
292 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
293 O << Directive;
294 EmitConstantValueOnly(InitList->getOperand(i));
295 O << "\n";
296 }
297}
298
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000299/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
300/// function pointers, ignoring the init priority.
301void AsmPrinter::EmitXXStructorList(Constant *List) {
302 // Should be an array of '{ int, void ()* }' structs. The first value is the
303 // init priority, which we ignore.
304 if (!isa<ConstantArray>(List)) return;
305 ConstantArray *InitList = cast<ConstantArray>(List);
306 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
307 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
308 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner434ffe42005-12-21 01:17:37 +0000309
310 if (CS->getOperand(1)->isNullValue())
311 return; // Found a null terminator, exit printing.
312 // Emit the function pointer.
Chris Lattnerf0e9aef2005-12-13 06:32:10 +0000313 EmitGlobalConstant(CS->getOperand(1));
314 }
315}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000316
Chris Lattnera9b25252006-02-05 01:29:18 +0000317/// getPreferredAlignmentLog - Return the preferred alignment of the
318/// specified global, returned in log form. This includes an explicitly
319/// requested alignment (if the global has one).
320unsigned AsmPrinter::getPreferredAlignmentLog(const GlobalVariable *GV) const {
Jim Laskeydce07562006-06-15 13:10:58 +0000321 const Type *ElemType = GV->getType()->getElementType();
322 unsigned Alignment = TM.getTargetData()->getTypeAlignmentShift(ElemType);
Chris Lattnera9b25252006-02-05 01:29:18 +0000323 if (GV->getAlignment() > (1U << Alignment))
324 Alignment = Log2_32(GV->getAlignment());
325
Chris Lattnercbab2842006-02-05 01:46:49 +0000326 if (GV->hasInitializer()) {
Jim Laskey3519b872006-06-15 19:37:14 +0000327 // Always round up alignment of global doubles to 8 bytes.
328 if (GV->getType()->getElementType() == Type::DoubleTy && Alignment < 3)
329 Alignment = 3;
Chris Lattnercbab2842006-02-05 01:46:49 +0000330 if (Alignment < 4) {
331 // If the global is not external, see if it is large. If so, give it a
332 // larger alignment.
Jim Laskeydce07562006-06-15 13:10:58 +0000333 if (TM.getTargetData()->getTypeSize(ElemType) > 128)
Chris Lattnercbab2842006-02-05 01:46:49 +0000334 Alignment = 4; // 16-byte alignment.
335 }
Chris Lattnera9b25252006-02-05 01:29:18 +0000336 }
337 return Alignment;
338}
Chris Lattnerf2991ce2005-11-21 08:25:09 +0000339
Chris Lattnerbb644e32005-11-21 07:51:36 +0000340// EmitAlignment - Emit an alignment directive to the specified power of two.
341void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV) const {
Chris Lattnerdd8eeed2005-11-14 19:00:06 +0000342 if (GV && GV->getAlignment())
343 NumBits = Log2_32(GV->getAlignment());
Chris Lattner747960d2005-11-10 18:09:27 +0000344 if (NumBits == 0) return; // No need to emit alignment.
Jim Laskeya6211dc2006-09-06 18:34:40 +0000345 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
346 O << TAI->getAlignDirective() << NumBits << "\n";
Chris Lattner1d35c162004-08-17 19:14:29 +0000347}
348
Chris Lattnerbb644e32005-11-21 07:51:36 +0000349/// EmitZeros - Emit a block of zeros.
Chris Lattnerea751992004-08-17 21:38:40 +0000350///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000351void AsmPrinter::EmitZeros(uint64_t NumZeros) const {
Chris Lattnerea751992004-08-17 21:38:40 +0000352 if (NumZeros) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000353 if (TAI->getZeroDirective()) {
354 O << TAI->getZeroDirective() << NumZeros;
355 if (TAI->getZeroDirectiveSuffix())
356 O << TAI->getZeroDirectiveSuffix();
Jeff Cohenf34ddb12006-05-02 03:46:13 +0000357 O << "\n";
358 } else {
Chris Lattnerea751992004-08-17 21:38:40 +0000359 for (; NumZeros; --NumZeros)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000360 O << TAI->getData8bitsDirective() << "0\n";
Chris Lattnerea751992004-08-17 21:38:40 +0000361 }
362 }
363}
364
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000365// Print out the specified constant, without a storage class. Only the
366// constants valid in constant expressions can occur here.
Chris Lattnerbb644e32005-11-21 07:51:36 +0000367void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner61753bf2004-10-16 18:19:26 +0000368 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000369 O << "0";
370 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
Chris Lattnereea983e2006-09-28 23:17:41 +0000371 assert(CB->getValue());
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000372 O << "1";
373 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
374 if (((CI->getValue() << 32) >> 32) == CI->getValue())
375 O << CI->getValue();
376 else
Duraid Madina73c4dba2005-05-15 13:05:48 +0000377 O << (uint64_t)CI->getValue();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000378 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
379 O << CI->getValue();
Chris Lattnerc0a1eba2005-11-10 18:36:17 +0000380 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina73a316d2005-04-02 12:21:51 +0000381 // This is a constant address for a global variable or function. Use the
382 // name of the variable or function as the address value, possibly
383 // decorating it with GlobalVarAddrPrefix/Suffix or
384 // FunctionAddrPrefix/Suffix (these all default to "" )
Jim Laskeya6211dc2006-09-06 18:34:40 +0000385 if (isa<Function>(GV)) {
386 O << TAI->getFunctionAddrPrefix()
387 << Mang->getValueName(GV)
388 << TAI->getFunctionAddrSuffix();
389 } else {
390 O << TAI->getGlobalVarAddrPrefix()
391 << Mang->getValueName(GV)
392 << TAI->getGlobalVarAddrSuffix();
393 }
Duraid Madina73a316d2005-04-02 12:21:51 +0000394 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000395 const TargetData *TD = TM.getTargetData();
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000396 switch(CE->getOpcode()) {
397 case Instruction::GetElementPtr: {
398 // generate a symbolic expression for the byte address
399 const Constant *ptrVal = CE->getOperand(0);
400 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Owen Anderson20a631f2006-05-03 01:29:57 +0000401 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), idxVec)) {
Chris Lattner145569b2005-02-14 21:40:26 +0000402 if (Offset)
403 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000404 EmitConstantValueOnly(ptrVal);
Chris Lattner145569b2005-02-14 21:40:26 +0000405 if (Offset > 0)
406 O << ") + " << Offset;
407 else if (Offset < 0)
408 O << ") - " << -Offset;
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000409 } else {
Chris Lattnerbb644e32005-11-21 07:51:36 +0000410 EmitConstantValueOnly(ptrVal);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000411 }
412 break;
413 }
414 case Instruction::Cast: {
Chris Lattner85492422006-07-29 01:57:19 +0000415 // Support only foldable casts to/from pointers that can be eliminated by
416 // changing the pointer to the appropriately sized integer type.
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000417 Constant *Op = CE->getOperand(0);
418 const Type *OpTy = Op->getType(), *Ty = CE->getType();
419
Chris Lattner85492422006-07-29 01:57:19 +0000420 // Handle casts to pointers by changing them into casts to the appropriate
421 // integer type. This promotes constant folding and simplifies this code.
422 if (isa<PointerType>(Ty)) {
423 const Type *IntPtrTy = TD->getIntPtrType();
424 Op = ConstantExpr::getCast(Op, IntPtrTy);
425 return EmitConstantValueOnly(Op);
426 }
427
428 // We know the dest type is not a pointer. Is the src value a pointer or
429 // integral?
430 if (isa<PointerType>(OpTy) || OpTy->isIntegral()) {
431 // We can emit the pointer value into this slot if the slot is an
432 // integer slot greater or equal to the size of the pointer.
433 if (Ty->isIntegral() && TD->getTypeSize(Ty) >= TD->getTypeSize(OpTy))
434 return EmitConstantValueOnly(Op);
435 }
436
437 assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattnerbb644e32005-11-21 07:51:36 +0000438 EmitConstantValueOnly(Op);
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000439 break;
440 }
441 case Instruction::Add:
442 O << "(";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000443 EmitConstantValueOnly(CE->getOperand(0));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000444 O << ") + (";
Chris Lattnerbb644e32005-11-21 07:51:36 +0000445 EmitConstantValueOnly(CE->getOperand(1));
Chris Lattner6a8e0f52004-08-16 23:15:22 +0000446 O << ")";
447 break;
448 default:
449 assert(0 && "Unsupported operator!");
450 }
451 } else {
452 assert(0 && "Unknown constant value!");
453 }
454}
Chris Lattner8452a1f2004-08-17 06:36:49 +0000455
456/// toOctal - Convert the low order bits of X into an octal digit.
457///
458static inline char toOctal(int X) {
459 return (X&7)+'0';
460}
461
Chris Lattner55a6d902005-11-10 18:06:33 +0000462/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner8452a1f2004-08-17 06:36:49 +0000463/// the predicate isString is true.
464///
Chris Lattner55a6d902005-11-10 18:06:33 +0000465static void printAsCString(std::ostream &O, const ConstantArray *CVA,
466 unsigned LastElt) {
Chris Lattner8452a1f2004-08-17 06:36:49 +0000467 assert(CVA->isString() && "Array is not string compatible!");
468
469 O << "\"";
Chris Lattner55a6d902005-11-10 18:06:33 +0000470 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukman835702a2005-04-21 22:36:52 +0000471 unsigned char C =
Chris Lattner0b955fd2005-01-08 19:59:10 +0000472 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000473
474 if (C == '"') {
475 O << "\\\"";
476 } else if (C == '\\') {
477 O << "\\\\";
478 } else if (isprint(C)) {
479 O << C;
480 } else {
481 switch(C) {
482 case '\b': O << "\\b"; break;
483 case '\f': O << "\\f"; break;
484 case '\n': O << "\\n"; break;
485 case '\r': O << "\\r"; break;
486 case '\t': O << "\\t"; break;
487 default:
488 O << '\\';
489 O << toOctal(C >> 6);
490 O << toOctal(C >> 3);
491 O << toOctal(C >> 0);
492 break;
493 }
494 }
495 }
496 O << "\"";
497}
498
Jeff Cohen24a62a92006-05-02 01:16:28 +0000499/// EmitString - Emit a zero-byte-terminated string constant.
500///
501void AsmPrinter::EmitString(const ConstantArray *CVA) const {
502 unsigned NumElts = CVA->getNumOperands();
Jim Laskeya6211dc2006-09-06 18:34:40 +0000503 if (TAI->getAscizDirective() && NumElts &&
Jeff Cohen24a62a92006-05-02 01:16:28 +0000504 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getRawValue() == 0) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000505 O << TAI->getAscizDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000506 printAsCString(O, CVA, NumElts-1);
507 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000508 O << TAI->getAsciiDirective();
Jeff Cohen24a62a92006-05-02 01:16:28 +0000509 printAsCString(O, CVA, NumElts);
510 }
511 O << "\n";
512}
513
Chris Lattnerbb644e32005-11-21 07:51:36 +0000514/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Chris Lattner8452a1f2004-08-17 06:36:49 +0000515///
Chris Lattnerbb644e32005-11-21 07:51:36 +0000516void AsmPrinter::EmitGlobalConstant(const Constant *CV) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000517 const TargetData *TD = TM.getTargetData();
Chris Lattner8452a1f2004-08-17 06:36:49 +0000518
Chris Lattner61753bf2004-10-16 18:19:26 +0000519 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000520 EmitZeros(TD->getTypeSize(CV->getType()));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000521 return;
522 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
523 if (CVA->isString()) {
Jeff Cohen24a62a92006-05-02 01:16:28 +0000524 EmitString(CVA);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000525 } else { // Not a string. Print the values in successive locations
526 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Chris Lattnerbb644e32005-11-21 07:51:36 +0000527 EmitGlobalConstant(CVA->getOperand(i));
Chris Lattner8452a1f2004-08-17 06:36:49 +0000528 }
529 return;
530 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
531 // Print the fields in successive locations. Pad to align if needed!
Owen Anderson20a631f2006-05-03 01:29:57 +0000532 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000533 uint64_t sizeSoFar = 0;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000534 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
535 const Constant* field = CVS->getOperand(i);
536
537 // Check if padding is needed and insert one or more 0s.
Owen Anderson20a631f2006-05-03 01:29:57 +0000538 uint64_t fieldSize = TD->getTypeSize(field->getType());
Chris Lattner0b955fd2005-01-08 19:59:10 +0000539 uint64_t padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner8452a1f2004-08-17 06:36:49 +0000540 : cvsLayout->MemberOffsets[i+1])
541 - cvsLayout->MemberOffsets[i]) - fieldSize;
542 sizeSoFar += fieldSize + padSize;
543
544 // Now print the actual field value
Chris Lattnerbb644e32005-11-21 07:51:36 +0000545 EmitGlobalConstant(field);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000546
547 // Insert the field padding unless it's zero bytes...
Chris Lattnerbb644e32005-11-21 07:51:36 +0000548 EmitZeros(padSize);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000549 }
550 assert(sizeSoFar == cvsLayout->StructSize &&
551 "Layout of constant struct may be incorrect!");
552 return;
553 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
554 // FP Constants are printed as integer constants to avoid losing
555 // precision...
556 double Val = CFP->getValue();
557 if (CFP->getType() == Type::DoubleTy) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000558 if (TAI->getData64bitsDirective())
559 O << TAI->getData64bitsDirective() << DoubleToBits(Val) << "\t"
560 << TAI->getCommentString() << " double value: " << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000561 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000562 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
563 << "\t" << TAI->getCommentString()
564 << " double most significant word " << Val << "\n";
565 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
566 << "\t" << TAI->getCommentString()
567 << " double least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000568 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000569 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val))
570 << "\t" << TAI->getCommentString()
571 << " double least significant word " << Val << "\n";
572 O << TAI->getData32bitsDirective() << unsigned(DoubleToBits(Val) >> 32)
573 << "\t" << TAI->getCommentString()
574 << " double most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000575 }
576 return;
577 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000578 O << TAI->getData32bitsDirective() << FloatToBits(Val)
579 << "\t" << TAI->getCommentString() << " float " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000580 return;
581 }
582 } else if (CV->getType() == Type::ULongTy || CV->getType() == Type::LongTy) {
583 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
584 uint64_t Val = CI->getRawValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000585
Jim Laskeya6211dc2006-09-06 18:34:40 +0000586 if (TAI->getData64bitsDirective())
587 O << TAI->getData64bitsDirective() << Val << "\n";
Owen Anderson20a631f2006-05-03 01:29:57 +0000588 else if (TD->isBigEndian()) {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000589 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
590 << "\t" << TAI->getCommentString()
591 << " Double-word most significant word " << Val << "\n";
592 O << TAI->getData32bitsDirective() << unsigned(Val)
593 << "\t" << TAI->getCommentString()
594 << " Double-word least significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000595 } else {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000596 O << TAI->getData32bitsDirective() << unsigned(Val)
597 << "\t" << TAI->getCommentString()
598 << " Double-word least significant word " << Val << "\n";
599 O << TAI->getData32bitsDirective() << unsigned(Val >> 32)
600 << "\t" << TAI->getCommentString()
601 << " Double-word most significant word " << Val << "\n";
Chris Lattner8452a1f2004-08-17 06:36:49 +0000602 }
603 return;
604 }
Nate Begeman41b1cdc2005-12-06 06:18:55 +0000605 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(CV)) {
606 const PackedType *PTy = CP->getType();
607
608 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
609 EmitGlobalConstant(CP->getOperand(I));
610
611 return;
Chris Lattner8452a1f2004-08-17 06:36:49 +0000612 }
613
614 const Type *type = CV->getType();
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000615 printDataDirective(type);
Chris Lattnerbb644e32005-11-21 07:51:36 +0000616 EmitConstantValueOnly(CV);
Chris Lattner8452a1f2004-08-17 06:36:49 +0000617 O << "\n";
618}
Chris Lattner061d9e22006-01-27 02:10:10 +0000619
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000620void
621AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
622 // Target doesn't support this yet!
623 abort();
624}
625
Chris Lattnera32814b2006-09-26 23:59:50 +0000626/// PrintSpecial - Print information related to the specified machine instr
627/// that is independent of the operand, and may be independent of the instr
628/// itself. This can be useful for portably encoding the comment character
629/// or other bits of target-specific knowledge into the asmstrings. The
630/// syntax used is ${:comment}. Targets can override this to add support
631/// for their own strange codes.
632void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) {
Chris Lattner30b47082006-09-27 00:06:07 +0000633 if (!strcmp(Code, "private")) {
634 O << TAI->getPrivateGlobalPrefix();
635 } else if (!strcmp(Code, "comment")) {
Chris Lattnera32814b2006-09-26 23:59:50 +0000636 O << TAI->getCommentString();
637 } else if (!strcmp(Code, "uid")) {
638 // Assign a unique ID to this machine instruction.
639 static const MachineInstr *LastMI = 0;
640 static unsigned Counter = 0U-1;
641 // If this is a new machine instruction, bump the counter.
642 if (LastMI != MI) { ++Counter; LastMI = MI; }
643 O << Counter;
644 } else {
645 std::cerr << "Unknown special formatter '" << Code
646 << "' for machine instr: " << *MI;
647 exit(1);
648 }
649}
650
651
Chris Lattner061d9e22006-01-27 02:10:10 +0000652/// printInlineAsm - This method formats and prints the specified machine
653/// instruction that is an inline asm.
654void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattner57ecb562006-01-30 23:00:08 +0000655 unsigned NumOperands = MI->getNumOperands();
656
657 // Count the number of register definitions.
658 unsigned NumDefs = 0;
Chris Lattner45456d42006-09-05 20:02:51 +0000659 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
660 ++NumDefs)
Chris Lattner57ecb562006-01-30 23:00:08 +0000661 assert(NumDefs != NumOperands-1 && "No asm string?");
662
663 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?");
Chris Lattneraa23fa92006-02-01 22:41:11 +0000664
665 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattner57ecb562006-01-30 23:00:08 +0000666 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattneraa23fa92006-02-01 22:41:11 +0000667
Chris Lattner3390cbe2006-07-28 00:17:20 +0000668 // If this asmstr is empty, don't bother printing the #APP/#NOAPP markers.
669 if (AsmStr[0] == 0) {
670 O << "\n"; // Tab already printed, avoid double indenting next instr.
671 return;
672 }
673
Jim Laskeya6211dc2006-09-06 18:34:40 +0000674 O << TAI->getInlineAsmStart() << "\n\t";
Chris Lattner3390cbe2006-07-28 00:17:20 +0000675
Chris Lattneraa23fa92006-02-01 22:41:11 +0000676 // The variant of the current asmprinter: FIXME: change.
677 int AsmPrinterVariant = 0;
Chris Lattner57ecb562006-01-30 23:00:08 +0000678
Chris Lattneraa23fa92006-02-01 22:41:11 +0000679 int CurVariant = -1; // The number of the {.|.|.} region we are in.
680 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner3a5ed552006-02-01 01:28:23 +0000681
Chris Lattneraa23fa92006-02-01 22:41:11 +0000682 while (*LastEmitted) {
683 switch (*LastEmitted) {
684 default: {
685 // Not a special case, emit the string section literally.
686 const char *LiteralEnd = LastEmitted+1;
687 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattnera633c3132006-05-05 21:47:05 +0000688 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattneraa23fa92006-02-01 22:41:11 +0000689 ++LiteralEnd;
690 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
691 O.write(LastEmitted, LiteralEnd-LastEmitted);
692 LastEmitted = LiteralEnd;
693 break;
694 }
Chris Lattnera633c3132006-05-05 21:47:05 +0000695 case '\n':
696 ++LastEmitted; // Consume newline character.
697 O << "\n\t"; // Indent code with newline.
698 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000699 case '$': {
700 ++LastEmitted; // Consume '$' character.
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000701 bool Done = true;
702
703 // Handle escapes.
704 switch (*LastEmitted) {
705 default: Done = false; break;
706 case '$': // $$ -> $
Chris Lattneraa23fa92006-02-01 22:41:11 +0000707 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
708 O << '$';
709 ++LastEmitted; // Consume second '$' character.
710 break;
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000711 case '(': // $( -> same as GCC's { character.
712 ++LastEmitted; // Consume '(' character.
713 if (CurVariant != -1) {
714 std::cerr << "Nested variants found in inline asm string: '"
715 << AsmStr << "'\n";
716 exit(1);
717 }
718 CurVariant = 0; // We're in the first variant now.
719 break;
720 case '|':
721 ++LastEmitted; // consume '|' character.
722 if (CurVariant == -1) {
723 std::cerr << "Found '|' character outside of variant in inline asm "
724 << "string: '" << AsmStr << "'\n";
725 exit(1);
726 }
727 ++CurVariant; // We're in the next variant.
728 break;
729 case ')': // $) -> same as GCC's } char.
730 ++LastEmitted; // consume ')' character.
731 if (CurVariant == -1) {
732 std::cerr << "Found '}' character outside of variant in inline asm "
733 << "string: '" << AsmStr << "'\n";
734 exit(1);
735 }
736 CurVariant = -1;
737 break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000738 }
Chris Lattnerfc416fa2006-10-03 23:27:09 +0000739 if (Done) break;
Chris Lattneraa23fa92006-02-01 22:41:11 +0000740
741 bool HasCurlyBraces = false;
742 if (*LastEmitted == '{') { // ${variable}
743 ++LastEmitted; // Consume '{' character.
744 HasCurlyBraces = true;
745 }
746
747 const char *IDStart = LastEmitted;
748 char *IDEnd;
749 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
750 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
751 std::cerr << "Bad $ operand number in inline asm string: '"
752 << AsmStr << "'\n";
753 exit(1);
754 }
755 LastEmitted = IDEnd;
756
Chris Lattner34f74c12006-02-06 22:17:23 +0000757 char Modifier[2] = { 0, 0 };
758
Chris Lattneraa23fa92006-02-01 22:41:11 +0000759 if (HasCurlyBraces) {
Chris Lattner34f74c12006-02-06 22:17:23 +0000760 // If we have curly braces, check for a modifier character. This
761 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
762 if (*LastEmitted == ':') {
763 ++LastEmitted; // Consume ':' character.
764 if (*LastEmitted == 0) {
765 std::cerr << "Bad ${:} expression in inline asm string: '"
766 << AsmStr << "'\n";
767 exit(1);
768 }
769
770 Modifier[0] = *LastEmitted;
771 ++LastEmitted; // Consume modifier character.
772 }
773
Chris Lattneraa23fa92006-02-01 22:41:11 +0000774 if (*LastEmitted != '}') {
775 std::cerr << "Bad ${} expression in inline asm string: '"
776 << AsmStr << "'\n";
777 exit(1);
778 }
779 ++LastEmitted; // Consume '}' character.
780 }
781
782 if ((unsigned)Val >= NumOperands-1) {
783 std::cerr << "Invalid $ operand number in inline asm string: '"
784 << AsmStr << "'\n";
785 exit(1);
786 }
787
Chris Lattner571d9642006-02-23 19:21:04 +0000788 // Okay, we finally have a value number. Ask the target to print this
Chris Lattneraa23fa92006-02-01 22:41:11 +0000789 // operand!
Chris Lattner571d9642006-02-23 19:21:04 +0000790 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
791 unsigned OpNo = 1;
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000792
793 bool Error = false;
794
Chris Lattner571d9642006-02-23 19:21:04 +0000795 // Scan to find the machine operand number for the operand.
Chris Lattner5af3fde2006-02-24 19:50:58 +0000796 for (; Val; --Val) {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000797 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner5af3fde2006-02-24 19:50:58 +0000798 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
799 OpNo += (OpFlags >> 3) + 1;
800 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000801
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000802 if (OpNo >= MI->getNumOperands()) {
803 Error = true;
Chris Lattner1d08c652006-02-24 20:21:58 +0000804 } else {
Chris Lattner8f8b5e42006-06-08 18:00:47 +0000805 unsigned OpFlags = MI->getOperand(OpNo).getImmedValue();
806 ++OpNo; // Skip over the ID number.
807
808 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
809 if ((OpFlags & 7) == 4 /*ADDR MODE*/) {
810 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
811 Modifier[0] ? Modifier : 0);
812 } else {
813 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
814 Modifier[0] ? Modifier : 0);
815 }
Chris Lattner1d08c652006-02-24 20:21:58 +0000816 }
817 if (Error) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000818 std::cerr << "Invalid operand found in inline asm: '"
819 << AsmStr << "'\n";
820 MI->dump();
821 exit(1);
822 }
Chris Lattner571d9642006-02-23 19:21:04 +0000823 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000824 break;
825 }
Chris Lattneraa23fa92006-02-01 22:41:11 +0000826 }
827 }
Jim Laskeya6211dc2006-09-06 18:34:40 +0000828 O << "\n\t" << TAI->getInlineAsmEnd() << "\n";
Chris Lattneraa23fa92006-02-01 22:41:11 +0000829}
830
831/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
832/// instruction, using the specified assembler variant. Targets should
833/// overried this to format as appropriate.
834bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattner34f74c12006-02-06 22:17:23 +0000835 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattneraa23fa92006-02-01 22:41:11 +0000836 // Target doesn't support this yet!
837 return true;
Chris Lattner061d9e22006-01-27 02:10:10 +0000838}
Chris Lattner1d08c652006-02-24 20:21:58 +0000839
840bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
841 unsigned AsmVariant,
842 const char *ExtraCode) {
843 // Target doesn't support this yet!
844 return true;
845}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000846
847/// printBasicBlockLabel - This method prints the label for the specified
848/// MachineBasicBlock
Nate Begemanb9d4f832006-05-02 05:37:32 +0000849void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
850 bool printColon,
851 bool printComment) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000852 O << TAI->getPrivateGlobalPrefix() << "BB" << FunctionNumber << "_"
Nate Begeman4971ba52006-05-02 17:36:46 +0000853 << MBB->getNumber();
Nate Begemanb9d4f832006-05-02 05:37:32 +0000854 if (printColon)
855 O << ':';
856 if (printComment)
Jim Laskeya6211dc2006-09-06 18:34:40 +0000857 O << '\t' << TAI->getCommentString() << MBB->getBasicBlock()->getName();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000858}
Nate Begeman984c1a42006-08-12 21:29:52 +0000859
860/// printSetLabel - This method prints a set label for the specified
861/// MachineBasicBlock
862void AsmPrinter::printSetLabel(unsigned uid,
863 const MachineBasicBlock *MBB) const {
Jim Laskeya6211dc2006-09-06 18:34:40 +0000864 if (!TAI->getSetDirective())
Nate Begeman984c1a42006-08-12 21:29:52 +0000865 return;
866
Jim Laskeya6211dc2006-09-06 18:34:40 +0000867 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
868 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Nate Begeman984c1a42006-08-12 21:29:52 +0000869 printBasicBlockLabel(MBB, false, false);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000870 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Nate Begeman984c1a42006-08-12 21:29:52 +0000871 << '_' << uid << '\n';
872}
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000873
874/// printDataDirective - This method prints the asm directive for the
875/// specified type.
876void AsmPrinter::printDataDirective(const Type *type) {
877 const TargetData *TD = TM.getTargetData();
878 switch (type->getTypeID()) {
879 case Type::BoolTyID:
880 case Type::UByteTyID: case Type::SByteTyID:
881 O << TAI->getData8bitsDirective();
882 break;
883 case Type::UShortTyID: case Type::ShortTyID:
884 O << TAI->getData16bitsDirective();
885 break;
886 case Type::PointerTyID:
887 if (TD->getPointerSize() == 8) {
888 assert(TAI->getData64bitsDirective() &&
889 "Target cannot handle 64-bit pointer exprs!");
890 O << TAI->getData64bitsDirective();
891 break;
892 }
893 //Fall through for pointer size == int size
894 case Type::UIntTyID: case Type::IntTyID:
895 O << TAI->getData32bitsDirective();
896 break;
897 case Type::ULongTyID: case Type::LongTyID:
898 assert(TAI->getData64bitsDirective() &&
899 "Target cannot handle 64-bit constant exprs!");
900 O << TAI->getData64bitsDirective();
901 break;
902 case Type::FloatTyID: case Type::DoubleTyID:
903 assert (0 && "Should have already output floating point constant.");
904 default:
905 assert (0 && "Can't handle printing this type of thing");
906 break;
907 }
908}