blob: c983d5f89413915b699c063f3174b2a7d58be1a6 [file] [log] [blame]
Brian Gaeke4acfd032004-03-04 06:00:41 +00001//===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaeke4acfd032004-03-04 06:00:41 +00003// 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.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaeke4acfd032004-03-04 06:00:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format Sparc V8 assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SparcV8.h"
16#include "SparcV8InstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
20#include "llvm/Assembly/Writer.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/Mangler.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/StringExtras.h"
28#include "llvm/Support/CommandLine.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000029#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000030#include <cctype>
Brian Gaeke4acfd032004-03-04 06:00:41 +000031using namespace llvm;
32
33namespace {
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
35
Chris Lattner994b7352005-12-16 06:34:17 +000036 struct SparcV8AsmPrinter : public MachineFunctionPass {
Brian Gaeke4acfd032004-03-04 06:00:41 +000037 /// Output stream on which we're printing assembly code.
38 ///
39 std::ostream &O;
40
41 /// Target machine description which we query for reg. names, data
42 /// layout, etc.
43 ///
44 TargetMachine &TM;
45
46 /// Name-mangler for global names.
47 ///
48 Mangler *Mang;
49
Chris Lattner994b7352005-12-16 06:34:17 +000050 SparcV8AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke4acfd032004-03-04 06:00:41 +000051
52 /// We name each basic block in a Function with a unique number, so
53 /// that we can consistently refer to them later. This is cleared
54 /// at the beginning of each call to runOnMachineFunction().
55 ///
56 typedef std::map<const Value *, unsigned> ValueMapTy;
57 ValueMapTy NumberForBB;
58
59 /// Cache of mangled name for current function. This is
60 /// recalculated at the beginning of each call to
61 /// runOnMachineFunction().
62 ///
63 std::string CurrentFnName;
64
65 virtual const char *getPassName() const {
66 return "SparcV8 Assembly Printer";
67 }
68
69 void emitConstantValueOnly(const Constant *CV);
70 void emitGlobalConstant(const Constant *CV);
71 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke446ae112004-06-15 19:52:59 +000072 void printOperand(const MachineInstr *MI, int opNum);
Brian Gaeke4acfd032004-03-04 06:00:41 +000073 void printMachineInstruction(const MachineInstr *MI);
Chris Lattner994b7352005-12-16 06:34:17 +000074 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000075 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke4acfd032004-03-04 06:00:41 +000076 bool doInitialization(Module &M);
77 bool doFinalization(Module &M);
78 };
79} // end of anonymous namespace
80
Chris Lattner994b7352005-12-16 06:34:17 +000081#include "SparcV8GenAsmWriter.inc"
82
Brian Gaeke4acfd032004-03-04 06:00:41 +000083/// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
84/// assembly code for a MachineFunction to the given output stream,
85/// using the given target machine description. This should work
86/// regardless of whether the function is in SSA form.
87///
88FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
89 TargetMachine &tm) {
Chris Lattner994b7352005-12-16 06:34:17 +000090 return new SparcV8AsmPrinter(o, tm);
Brian Gaeke4acfd032004-03-04 06:00:41 +000091}
92
93/// toOctal - Convert the low order bits of X into an octal digit.
94///
95static inline char toOctal(int X) {
96 return (X&7)+'0';
97}
98
99/// getAsCString - Return the specified array as a C compatible
100/// string, only if the predicate isStringCompatible is true.
101///
102static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
103 assert(CVA->isString() && "Array is not string compatible!");
104
105 O << "\"";
106 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
107 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
108
109 if (C == '"') {
110 O << "\\\"";
111 } else if (C == '\\') {
112 O << "\\\\";
113 } else if (isprint(C)) {
114 O << C;
115 } else {
116 switch(C) {
117 case '\b': O << "\\b"; break;
118 case '\f': O << "\\f"; break;
119 case '\n': O << "\\n"; break;
120 case '\r': O << "\\r"; break;
121 case '\t': O << "\\t"; break;
122 default:
123 O << '\\';
124 O << toOctal(C >> 6);
125 O << toOctal(C >> 3);
126 O << toOctal(C >> 0);
127 break;
128 }
129 }
130 }
131 O << "\"";
132}
133
134// Print out the specified constant, without a storage class. Only the
135// constants valid in constant expressions can occur here.
Chris Lattner994b7352005-12-16 06:34:17 +0000136void SparcV8AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Brian Gaeke54799c22004-11-14 03:22:05 +0000137 if (CV->isNullValue() || isa<UndefValue> (CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000138 O << "0";
139 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
140 assert(CB == ConstantBool::True);
141 O << "1";
142 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
143 if (((CI->getValue() << 32) >> 32) == CI->getValue())
144 O << CI->getValue();
145 else
146 O << (unsigned long long)CI->getValue();
147 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
148 O << CI->getValue();
Chris Lattner73302482004-07-18 07:26:17 +0000149 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000150 // This is a constant address for a global variable or function. Use the
151 // name of the variable or function as the address value.
Chris Lattner73302482004-07-18 07:26:17 +0000152 O << Mang->getValueName(GV);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000153 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
154 const TargetData &TD = TM.getTargetData();
155 switch(CE->getOpcode()) {
156 case Instruction::GetElementPtr: {
157 // generate a symbolic expression for the byte address
158 const Constant *ptrVal = CE->getOperand(0);
159 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
160 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
161 O << "(";
162 emitConstantValueOnly(ptrVal);
163 O << ") + " << Offset;
164 } else {
165 emitConstantValueOnly(ptrVal);
166 }
167 break;
168 }
169 case Instruction::Cast: {
170 // Support only non-converting or widening casts for now, that is, ones
171 // that do not involve a change in value. This assertion is really gross,
172 // and may not even be a complete check.
173 Constant *Op = CE->getOperand(0);
174 const Type *OpTy = Op->getType(), *Ty = CE->getType();
175
176 // Pointers on ILP32 machines can be losslessly converted back and
177 // forth into 32-bit or wider integers, regardless of signedness.
178 assert(((isa<PointerType>(OpTy)
179 && (Ty == Type::LongTy || Ty == Type::ULongTy
180 || Ty == Type::IntTy || Ty == Type::UIntTy))
181 || (isa<PointerType>(Ty)
182 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
183 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
184 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
185 && OpTy->isLosslesslyConvertibleTo(Ty))))
186 && "FIXME: Don't yet support this kind of constant cast expr");
187 O << "(";
188 emitConstantValueOnly(Op);
189 O << ")";
190 break;
191 }
192 case Instruction::Add:
193 O << "(";
194 emitConstantValueOnly(CE->getOperand(0));
195 O << ") + (";
196 emitConstantValueOnly(CE->getOperand(1));
197 O << ")";
198 break;
199 default:
200 assert(0 && "Unsupported operator!");
201 }
202 } else {
203 assert(0 && "Unknown constant value!");
204 }
205}
206
207// Print a constant value or values, with the appropriate storage class as a
208// prefix.
Chris Lattner994b7352005-12-16 06:34:17 +0000209void SparcV8AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000210 const TargetData &TD = TM.getTargetData();
211
Brian Gaeke9d2427c2004-06-18 08:59:16 +0000212 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000213 if (CVA->isString()) {
214 O << "\t.ascii\t";
215 printAsCString(O, CVA);
216 O << "\n";
217 } else { // Not a string. Print the values in successive locations
Chris Lattnercdf70122004-08-04 17:27:27 +0000218 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++)
219 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke4acfd032004-03-04 06:00:41 +0000220 }
221 return;
222 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
223 // Print the fields in successive locations. Pad to align if needed!
224 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000225 unsigned sizeSoFar = 0;
Chris Lattnercdf70122004-08-04 17:27:27 +0000226 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
227 const Constant* field = CVS->getOperand(i);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000228
229 // Check if padding is needed and insert one or more 0s.
230 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattnercdf70122004-08-04 17:27:27 +0000231 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Brian Gaeke4acfd032004-03-04 06:00:41 +0000232 : cvsLayout->MemberOffsets[i+1])
233 - cvsLayout->MemberOffsets[i]) - fieldSize;
234 sizeSoFar += fieldSize + padSize;
235
236 // Now print the actual field value
237 emitGlobalConstant(field);
238
239 // Insert the field padding unless it's zero bytes...
240 if (padSize)
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000241 O << "\t.skip\t " << padSize << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000242 }
243 assert(sizeSoFar == cvsLayout->StructSize &&
244 "Layout of constant struct may be incorrect!");
245 return;
246 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
247 // FP Constants are printed as integer constants to avoid losing
248 // precision...
249 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000250 switch (CFP->getType()->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000251 default: assert(0 && "Unknown floating point type!");
252 case Type::FloatTyID: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000253 O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000254 return;
255 }
256 case Type::DoubleTyID: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000257 O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
258 O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000259 return;
260 }
261 }
Brian Gaeke54799c22004-11-14 03:22:05 +0000262 } else if (isa<UndefValue> (CV)) {
263 unsigned size = TD.getTypeSize (CV->getType ());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000264 O << "\t.skip\t " << size << "\n";
Brian Gaeke54799c22004-11-14 03:22:05 +0000265 return;
Brian Gaeke4dd043f2004-11-23 21:10:49 +0000266 } else if (isa<ConstantAggregateZero> (CV)) {
267 unsigned size = TD.getTypeSize (CV->getType ());
268 for (unsigned i = 0; i < size; ++i)
Misha Brukman27177f82005-04-22 18:06:01 +0000269 O << "\t.byte 0\n";
Brian Gaeke4dd043f2004-11-23 21:10:49 +0000270 return;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000271 }
272
273 const Type *type = CV->getType();
274 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000275 switch (type->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000276 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
277 O << ".byte";
278 break;
279 case Type::UShortTyID: case Type::ShortTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000280 O << ".half";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000281 break;
282 case Type::FloatTyID: case Type::PointerTyID:
283 case Type::UIntTyID: case Type::IntTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000284 O << ".word";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000285 break;
286 case Type::DoubleTyID:
287 case Type::ULongTyID: case Type::LongTyID:
Brian Gaeke3bf960c2004-12-09 18:51:01 +0000288 O << ".xword";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000289 break;
290 default:
291 assert (0 && "Can't handle printing this type of thing");
292 break;
293 }
294 O << "\t";
295 emitConstantValueOnly(CV);
296 O << "\n";
297}
298
299/// printConstantPool - Print to the current output stream assembly
300/// representations of the constants in the constant pool MCP. This is
301/// used to print out constants which have been "spilled to memory" by
302/// the code generator.
303///
Chris Lattner994b7352005-12-16 06:34:17 +0000304void SparcV8AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000305 const std::vector<Constant*> &CP = MCP->getConstants();
306 const TargetData &TD = TM.getTargetData();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000307
Brian Gaeke4acfd032004-03-04 06:00:41 +0000308 if (CP.empty()) return;
309
310 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Brian Gaekeb27df442004-09-29 03:25:40 +0000311 O << "\t.section \".rodata\"\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000312 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
313 << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000314 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t!"
Brian Gaeke4acfd032004-03-04 06:00:41 +0000315 << *CP[i] << "\n";
316 emitGlobalConstant(CP[i]);
317 }
318}
319
320/// runOnMachineFunction - This uses the printMachineInstruction()
321/// method to print assembly for each instruction.
322///
Chris Lattner994b7352005-12-16 06:34:17 +0000323bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000324 // BBNumber is used here so that a given Printer will never give two
325 // BBs the same name. (If you have a better way, please let me know!)
326 static unsigned BBNumber = 0;
327
328 O << "\n\n";
329 // What's my mangled name?
330 CurrentFnName = Mang->getValueName(MF.getFunction());
331
332 // Print out constants referenced by the function
333 printConstantPool(MF.getConstantPool());
334
335 // Print out labels for the function.
336 O << "\t.text\n";
337 O << "\t.align 16\n";
338 O << "\t.globl\t" << CurrentFnName << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000339 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000340 O << CurrentFnName << ":\n";
341
342 // Number each basic block so that we can consistently refer to them
343 // in PC-relative references.
344 NumberForBB.clear();
345 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
346 I != E; ++I) {
347 NumberForBB[I->getBasicBlock()] = BBNumber++;
348 }
349
350 // Print out code for the function.
351 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
352 I != E; ++I) {
353 // Print a label for the basic block.
Brian Gaeke09c13092004-06-17 19:39:23 +0000354 O << ".LBB" << Mang->getValueName(MF.getFunction ())
355 << "_" << I->getNumber () << ":\t! "
356 << I->getBasicBlock ()->getName () << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000357 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000358 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000359 // Print the assembly for the instruction.
Brian Gaeke4acfd032004-03-04 06:00:41 +0000360 printMachineInstruction(II);
361 }
362 }
363
364 // We didn't modify anything.
365 return false;
366}
367
Chris Lattner994b7352005-12-16 06:34:17 +0000368void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000369 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000370 const MRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000371 bool CloseParen = false;
372 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
373 O << "%hi(";
374 CloseParen = true;
Misha Brukmanf54ef972004-06-24 23:38:20 +0000375 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
376 {
Brian Gaeke446ae112004-06-15 19:52:59 +0000377 O << "%lo(";
378 CloseParen = true;
379 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000380 switch (MO.getType()) {
381 case MachineOperand::MO_VirtualRegister:
382 if (Value *V = MO.getVRegValueOrNull()) {
383 O << "<" << V->getName() << ">";
Brian Gaeke446ae112004-06-15 19:52:59 +0000384 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000385 }
386 // FALLTHROUGH
387 case MachineOperand::MO_MachineRegister:
388 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaekea8b00ca2004-03-06 05:30:21 +0000389 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000390 else
391 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000392 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000393
394 case MachineOperand::MO_SignExtendedImmed:
395 case MachineOperand::MO_UnextendedImmed:
396 O << (int)MO.getImmedValue();
Brian Gaeke446ae112004-06-15 19:52:59 +0000397 break;
Brian Gaeke09c13092004-06-17 19:39:23 +0000398 case MachineOperand::MO_MachineBasicBlock: {
399 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
400 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
401 << "_" << MBBOp->getNumber () << "\t! "
402 << MBBOp->getBasicBlock ()->getName ();
403 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000404 }
Brian Gaeke09c13092004-06-17 19:39:23 +0000405 case MachineOperand::MO_PCRelativeDisp:
406 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
407 abort ();
408 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000409 case MachineOperand::MO_GlobalAddress:
410 O << Mang->getValueName(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000411 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000412 case MachineOperand::MO_ExternalSymbol:
413 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000414 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000415 case MachineOperand::MO_ConstantPoolIndex:
416 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
417 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000418 default:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000419 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000420 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000421 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000422}
423
Brian Gaeke4acfd032004-03-04 06:00:41 +0000424/// printMachineInstruction -- Print out a single SparcV8 LLVM instruction
425/// MI in GAS syntax to the current output stream.
426///
Chris Lattner994b7352005-12-16 06:34:17 +0000427void SparcV8AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
Chris Lattner994b7352005-12-16 06:34:17 +0000428 O << "\t";
Chris Lattner1c4f4352005-12-16 06:52:00 +0000429 if (printInstruction(MI)) return;
Chris Lattner994b7352005-12-16 06:34:17 +0000430
Brian Gaeke4acfd032004-03-04 06:00:41 +0000431 unsigned Opcode = MI->getOpcode();
Chris Lattner143e0ea2004-06-02 05:47:26 +0000432 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000433 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Brian Gaeked303a202004-07-16 10:31:47 +0000434
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000435 O << Desc.Name << " ";
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000436
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000437 // print non-immediate, non-register-def operands
438 // then print immediate operands
439 // then print register-def operands.
Brian Gaeke446ae112004-06-15 19:52:59 +0000440 std::vector<int> print_order;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000441 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
442 if (!(MI->getOperand (i).isImmediate ()
443 || (MI->getOperand (i).isRegister ()
444 && MI->getOperand (i).isDef ())))
Brian Gaeke446ae112004-06-15 19:52:59 +0000445 print_order.push_back (i);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000446 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
447 if (MI->getOperand (i).isImmediate ())
Brian Gaeke446ae112004-06-15 19:52:59 +0000448 print_order.push_back (i);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000449 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
450 if (MI->getOperand (i).isRegister () && MI->getOperand (i).isDef ())
Brian Gaeke446ae112004-06-15 19:52:59 +0000451 print_order.push_back (i);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000452 for (unsigned i = 0, e = print_order.size (); i != e; ++i) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000453 printOperand (MI, print_order[i]);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000454 if (i != (print_order.size () - 1))
455 O << ", ";
456 }
457 O << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000458}
459
Chris Lattner994b7352005-12-16 06:34:17 +0000460bool SparcV8AsmPrinter::doInitialization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000461 Mang = new Mangler(M);
462 return false; // success
463}
464
465// SwitchSection - Switch to the specified section of the executable if we are
466// not already in it!
467//
468static void SwitchSection(std::ostream &OS, std::string &CurSection,
469 const char *NewSection) {
470 if (CurSection != NewSection) {
471 CurSection = NewSection;
472 if (!CurSection.empty())
Brian Gaekeb27df442004-09-29 03:25:40 +0000473 OS << "\t.section \"" << NewSection << "\"\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000474 }
475}
476
Chris Lattner994b7352005-12-16 06:34:17 +0000477bool SparcV8AsmPrinter::doFinalization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000478 const TargetData &TD = TM.getTargetData();
479 std::string CurSection;
480
481 // Print out module-level global variables here.
Chris Lattnere4d5c442005-03-15 04:54:21 +0000482 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Brian Gaeke4acfd032004-03-04 06:00:41 +0000483 if (I->hasInitializer()) { // External global require no code
484 O << "\n\n";
485 std::string name = Mang->getValueName(I);
486 Constant *C = I->getInitializer();
487 unsigned Size = TD.getTypeSize(C->getType());
488 unsigned Align = TD.getTypeAlignment(C->getType());
489
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000490 if (C->isNullValue() &&
Brian Gaeke4acfd032004-03-04 06:00:41 +0000491 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
492 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
493 SwitchSection(O, CurSection, ".data");
494 if (I->hasInternalLinkage())
495 O << "\t.local " << name << "\n";
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000496
Brian Gaeke4acfd032004-03-04 06:00:41 +0000497 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
498 << "," << (unsigned)TD.getTypeAlignment(C->getType());
Brian Gaeke79db7402004-03-16 22:37:12 +0000499 O << "\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000500 WriteAsOperand(O, I, true, true, &M);
501 O << "\n";
502 } else {
503 switch (I->getLinkage()) {
504 case GlobalValue::LinkOnceLinkage:
505 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
506 // Nonnull linkonce -> weak
507 O << "\t.weak " << name << "\n";
508 SwitchSection(O, CurSection, "");
Brian Gaekeb27df442004-09-29 03:25:40 +0000509 O << "\t.section\t\".llvm.linkonce.d." << name << "\",\"aw\",@progbits\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000510 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000511
Brian Gaeke4acfd032004-03-04 06:00:41 +0000512 case GlobalValue::AppendingLinkage:
513 // FIXME: appending linkage variables should go into a section of
514 // their name or something. For now, just emit them as external.
515 case GlobalValue::ExternalLinkage:
516 // If external or appending, declare as a global symbol
517 O << "\t.globl " << name << "\n";
518 // FALL THROUGH
519 case GlobalValue::InternalLinkage:
520 if (C->isNullValue())
521 SwitchSection(O, CurSection, ".bss");
522 else
523 SwitchSection(O, CurSection, ".data");
524 break;
Misha Brukmanc11c44f2004-11-19 21:49:19 +0000525 case GlobalValue::GhostLinkage:
526 std::cerr << "Should not have any unmaterialized functions!\n";
527 abort();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000528 }
529
530 O << "\t.align " << Align << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000531 O << "\t.type " << name << ",#object\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000532 O << "\t.size " << name << "," << Size << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000533 O << name << ":\t\t\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000534 WriteAsOperand(O, I, true, true, &M);
535 O << " = ";
536 WriteAsOperand(O, C, false, false, &M);
537 O << "\n";
538 emitGlobalConstant(C);
539 }
540 }
541
542 delete Mang;
543 return false; // success
544}