blob: 36137b1a1c12335fb55d9fe55b3940260cff6f64 [file] [log] [blame]
Brian Gaeke4acfd032004-03-04 06:00:41 +00001//===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===//
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 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"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000029#include <cctype>
Brian Gaeke4acfd032004-03-04 06:00:41 +000030using namespace llvm;
31
32namespace {
33 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
34
35 struct V8Printer : public MachineFunctionPass {
36 /// Output stream on which we're printing assembly code.
37 ///
38 std::ostream &O;
39
40 /// Target machine description which we query for reg. names, data
41 /// layout, etc.
42 ///
43 TargetMachine &TM;
44
45 /// Name-mangler for global names.
46 ///
47 Mangler *Mang;
48
49 V8Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
50
51 /// We name each basic block in a Function with a unique number, so
52 /// that we can consistently refer to them later. This is cleared
53 /// at the beginning of each call to runOnMachineFunction().
54 ///
55 typedef std::map<const Value *, unsigned> ValueMapTy;
56 ValueMapTy NumberForBB;
57
58 /// Cache of mangled name for current function. This is
59 /// recalculated at the beginning of each call to
60 /// runOnMachineFunction().
61 ///
62 std::string CurrentFnName;
63
64 virtual const char *getPassName() const {
65 return "SparcV8 Assembly Printer";
66 }
67
68 void emitConstantValueOnly(const Constant *CV);
69 void emitGlobalConstant(const Constant *CV);
70 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke446ae112004-06-15 19:52:59 +000071 void printOperand(const MachineInstr *MI, int opNum);
Brian Gaekeceb22412004-06-18 06:27:59 +000072 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Brian Gaeke4acfd032004-03-04 06:00:41 +000073 void printMachineInstruction(const MachineInstr *MI);
74 bool runOnMachineFunction(MachineFunction &F);
75 bool doInitialization(Module &M);
76 bool doFinalization(Module &M);
77 };
78} // end of anonymous namespace
79
80/// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
81/// assembly code for a MachineFunction to the given output stream,
82/// using the given target machine description. This should work
83/// regardless of whether the function is in SSA form.
84///
85FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
86 TargetMachine &tm) {
87 return new V8Printer(o, tm);
88}
89
90/// toOctal - Convert the low order bits of X into an octal digit.
91///
92static inline char toOctal(int X) {
93 return (X&7)+'0';
94}
95
96/// getAsCString - Return the specified array as a C compatible
97/// string, only if the predicate isStringCompatible is true.
98///
99static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
100 assert(CVA->isString() && "Array is not string compatible!");
101
102 O << "\"";
103 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
104 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
105
106 if (C == '"') {
107 O << "\\\"";
108 } else if (C == '\\') {
109 O << "\\\\";
110 } else if (isprint(C)) {
111 O << C;
112 } else {
113 switch(C) {
114 case '\b': O << "\\b"; break;
115 case '\f': O << "\\f"; break;
116 case '\n': O << "\\n"; break;
117 case '\r': O << "\\r"; break;
118 case '\t': O << "\\t"; break;
119 default:
120 O << '\\';
121 O << toOctal(C >> 6);
122 O << toOctal(C >> 3);
123 O << toOctal(C >> 0);
124 break;
125 }
126 }
127 }
128 O << "\"";
129}
130
131// Print out the specified constant, without a storage class. Only the
132// constants valid in constant expressions can occur here.
133void V8Printer::emitConstantValueOnly(const Constant *CV) {
Brian Gaeke54799c22004-11-14 03:22:05 +0000134 if (CV->isNullValue() || isa<UndefValue> (CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000135 O << "0";
136 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
137 assert(CB == ConstantBool::True);
138 O << "1";
139 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
140 if (((CI->getValue() << 32) >> 32) == CI->getValue())
141 O << CI->getValue();
142 else
143 O << (unsigned long long)CI->getValue();
144 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
145 O << CI->getValue();
Chris Lattner73302482004-07-18 07:26:17 +0000146 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Brian Gaeke4acfd032004-03-04 06:00:41 +0000147 // This is a constant address for a global variable or function. Use the
148 // name of the variable or function as the address value.
Chris Lattner73302482004-07-18 07:26:17 +0000149 O << Mang->getValueName(GV);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000150 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
151 const TargetData &TD = TM.getTargetData();
152 switch(CE->getOpcode()) {
153 case Instruction::GetElementPtr: {
154 // generate a symbolic expression for the byte address
155 const Constant *ptrVal = CE->getOperand(0);
156 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
157 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
158 O << "(";
159 emitConstantValueOnly(ptrVal);
160 O << ") + " << Offset;
161 } else {
162 emitConstantValueOnly(ptrVal);
163 }
164 break;
165 }
166 case Instruction::Cast: {
167 // Support only non-converting or widening casts for now, that is, ones
168 // that do not involve a change in value. This assertion is really gross,
169 // and may not even be a complete check.
170 Constant *Op = CE->getOperand(0);
171 const Type *OpTy = Op->getType(), *Ty = CE->getType();
172
173 // Pointers on ILP32 machines can be losslessly converted back and
174 // forth into 32-bit or wider integers, regardless of signedness.
175 assert(((isa<PointerType>(OpTy)
176 && (Ty == Type::LongTy || Ty == Type::ULongTy
177 || Ty == Type::IntTy || Ty == Type::UIntTy))
178 || (isa<PointerType>(Ty)
179 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
180 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
181 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
182 && OpTy->isLosslesslyConvertibleTo(Ty))))
183 && "FIXME: Don't yet support this kind of constant cast expr");
184 O << "(";
185 emitConstantValueOnly(Op);
186 O << ")";
187 break;
188 }
189 case Instruction::Add:
190 O << "(";
191 emitConstantValueOnly(CE->getOperand(0));
192 O << ") + (";
193 emitConstantValueOnly(CE->getOperand(1));
194 O << ")";
195 break;
196 default:
197 assert(0 && "Unsupported operator!");
198 }
199 } else {
200 assert(0 && "Unknown constant value!");
201 }
202}
203
204// Print a constant value or values, with the appropriate storage class as a
205// prefix.
206void V8Printer::emitGlobalConstant(const Constant *CV) {
207 const TargetData &TD = TM.getTargetData();
208
Brian Gaeke9d2427c2004-06-18 08:59:16 +0000209 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000210 if (CVA->isString()) {
211 O << "\t.ascii\t";
212 printAsCString(O, CVA);
213 O << "\n";
214 } else { // Not a string. Print the values in successive locations
Chris Lattnercdf70122004-08-04 17:27:27 +0000215 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++)
216 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke4acfd032004-03-04 06:00:41 +0000217 }
218 return;
219 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
220 // Print the fields in successive locations. Pad to align if needed!
221 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000222 unsigned sizeSoFar = 0;
Chris Lattnercdf70122004-08-04 17:27:27 +0000223 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
224 const Constant* field = CVS->getOperand(i);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000225
226 // Check if padding is needed and insert one or more 0s.
227 unsigned fieldSize = TD.getTypeSize(field->getType());
Chris Lattnercdf70122004-08-04 17:27:27 +0000228 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Brian Gaeke4acfd032004-03-04 06:00:41 +0000229 : cvsLayout->MemberOffsets[i+1])
230 - cvsLayout->MemberOffsets[i]) - fieldSize;
231 sizeSoFar += fieldSize + padSize;
232
233 // Now print the actual field value
234 emitGlobalConstant(field);
235
236 // Insert the field padding unless it's zero bytes...
237 if (padSize)
Brian Gaeke9d2427c2004-06-18 08:59:16 +0000238 O << "\t.skip\t " << padSize << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000239 }
240 assert(sizeSoFar == cvsLayout->StructSize &&
241 "Layout of constant struct may be incorrect!");
242 return;
243 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
244 // FP Constants are printed as integer constants to avoid losing
245 // precision...
246 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000247 switch (CFP->getType()->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000248 default: assert(0 && "Unknown floating point type!");
249 case Type::FloatTyID: {
250 union FU { // Abide by C TBAA rules
251 float FVal;
252 unsigned UVal;
253 } U;
254 U.FVal = Val;
Brian Gaeke79db7402004-03-16 22:37:12 +0000255 O << ".long\t" << U.UVal << "\t! float " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000256 return;
257 }
258 case Type::DoubleTyID: {
259 union DU { // Abide by C TBAA rules
260 double FVal;
261 uint64_t UVal;
262 } U;
263 U.FVal = Val;
Brian Gaeke6fdd9e12004-09-29 19:59:06 +0000264 O << ".word\t0x" << std::hex << (U.UVal >> 32) << std::dec << "\t! double " << Val << "\n";
265 O << ".word\t0x" << std::hex << (U.UVal & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000266 return;
267 }
268 }
Brian Gaeke54799c22004-11-14 03:22:05 +0000269 } else if (isa<UndefValue> (CV)) {
270 unsigned size = TD.getTypeSize (CV->getType ());
271 O << "\t.skip\t " << size << "\n";
272 return;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000273 }
274
275 const Type *type = CV->getType();
276 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000277 switch (type->getTypeID()) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000278 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
279 O << ".byte";
280 break;
281 case Type::UShortTyID: case Type::ShortTyID:
282 O << ".word";
283 break;
284 case Type::FloatTyID: case Type::PointerTyID:
285 case Type::UIntTyID: case Type::IntTyID:
286 O << ".long";
287 break;
288 case Type::DoubleTyID:
289 case Type::ULongTyID: case Type::LongTyID:
290 O << ".quad";
291 break;
292 default:
293 assert (0 && "Can't handle printing this type of thing");
294 break;
295 }
296 O << "\t";
297 emitConstantValueOnly(CV);
298 O << "\n";
299}
300
301/// printConstantPool - Print to the current output stream assembly
302/// representations of the constants in the constant pool MCP. This is
303/// used to print out constants which have been "spilled to memory" by
304/// the code generator.
305///
306void V8Printer::printConstantPool(MachineConstantPool *MCP) {
307 const std::vector<Constant*> &CP = MCP->getConstants();
308 const TargetData &TD = TM.getTargetData();
309
310 if (CP.empty()) return;
311
312 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Brian Gaekeb27df442004-09-29 03:25:40 +0000313 O << "\t.section \".rodata\"\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000314 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
315 << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000316 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t!"
Brian Gaeke4acfd032004-03-04 06:00:41 +0000317 << *CP[i] << "\n";
318 emitGlobalConstant(CP[i]);
319 }
320}
321
322/// runOnMachineFunction - This uses the printMachineInstruction()
323/// method to print assembly for each instruction.
324///
325bool V8Printer::runOnMachineFunction(MachineFunction &MF) {
326 // BBNumber is used here so that a given Printer will never give two
327 // BBs the same name. (If you have a better way, please let me know!)
328 static unsigned BBNumber = 0;
329
330 O << "\n\n";
331 // What's my mangled name?
332 CurrentFnName = Mang->getValueName(MF.getFunction());
333
334 // Print out constants referenced by the function
335 printConstantPool(MF.getConstantPool());
336
337 // Print out labels for the function.
338 O << "\t.text\n";
339 O << "\t.align 16\n";
340 O << "\t.globl\t" << CurrentFnName << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000341 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000342 O << CurrentFnName << ":\n";
343
344 // Number each basic block so that we can consistently refer to them
345 // in PC-relative references.
346 NumberForBB.clear();
347 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
348 I != E; ++I) {
349 NumberForBB[I->getBasicBlock()] = BBNumber++;
350 }
351
352 // Print out code for the function.
353 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
354 I != E; ++I) {
355 // Print a label for the basic block.
Brian Gaeke09c13092004-06-17 19:39:23 +0000356 O << ".LBB" << Mang->getValueName(MF.getFunction ())
357 << "_" << I->getNumber () << ":\t! "
358 << I->getBasicBlock ()->getName () << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000359 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
360 II != E; ++II) {
361 // Print the assembly for the instruction.
362 O << "\t";
363 printMachineInstruction(II);
364 }
365 }
366
367 // We didn't modify anything.
368 return false;
369}
370
Brian Gaeke446ae112004-06-15 19:52:59 +0000371void V8Printer::printOperand(const MachineInstr *MI, int opNum) {
372 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000373 const MRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000374 bool CloseParen = false;
375 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
376 O << "%hi(";
377 CloseParen = true;
Misha Brukmanf54ef972004-06-24 23:38:20 +0000378 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
379 {
Brian Gaeke446ae112004-06-15 19:52:59 +0000380 O << "%lo(";
381 CloseParen = true;
382 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000383 switch (MO.getType()) {
384 case MachineOperand::MO_VirtualRegister:
385 if (Value *V = MO.getVRegValueOrNull()) {
386 O << "<" << V->getName() << ">";
Brian Gaeke446ae112004-06-15 19:52:59 +0000387 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000388 }
389 // FALLTHROUGH
390 case MachineOperand::MO_MachineRegister:
391 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaekea8b00ca2004-03-06 05:30:21 +0000392 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000393 else
394 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000395 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000396
397 case MachineOperand::MO_SignExtendedImmed:
398 case MachineOperand::MO_UnextendedImmed:
399 O << (int)MO.getImmedValue();
Brian Gaeke446ae112004-06-15 19:52:59 +0000400 break;
Brian Gaeke09c13092004-06-17 19:39:23 +0000401 case MachineOperand::MO_MachineBasicBlock: {
402 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
403 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
404 << "_" << MBBOp->getNumber () << "\t! "
405 << MBBOp->getBasicBlock ()->getName ();
406 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000407 }
Brian Gaeke09c13092004-06-17 19:39:23 +0000408 case MachineOperand::MO_PCRelativeDisp:
409 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
410 abort ();
411 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000412 case MachineOperand::MO_GlobalAddress:
413 O << Mang->getValueName(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000414 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000415 case MachineOperand::MO_ExternalSymbol:
416 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000417 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000418 case MachineOperand::MO_ConstantPoolIndex:
419 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
420 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000421 default:
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000422 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000423 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000424 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000425}
426
Brian Gaeke1c381752004-04-06 22:10:11 +0000427static bool isLoadInstruction (const MachineInstr *MI) {
428 switch (MI->getOpcode ()) {
Brian Gaekeaf0492e2004-06-24 07:37:12 +0000429 case V8::LDSB:
430 case V8::LDSH:
431 case V8::LDUB:
432 case V8::LDUH:
433 case V8::LD:
434 case V8::LDD:
435 case V8::LDFrr:
436 case V8::LDFri:
437 case V8::LDDFrr:
438 case V8::LDDFri:
Brian Gaeke1c381752004-04-06 22:10:11 +0000439 return true;
440 default:
441 return false;
442 }
443}
444
445static bool isStoreInstruction (const MachineInstr *MI) {
446 switch (MI->getOpcode ()) {
Brian Gaekeaf0492e2004-06-24 07:37:12 +0000447 case V8::STB:
448 case V8::STH:
449 case V8::ST:
450 case V8::STD:
451 case V8::STFrr:
452 case V8::STFri:
453 case V8::STDFrr:
454 case V8::STDFri:
Brian Gaeke1c381752004-04-06 22:10:11 +0000455 return true;
456 default:
457 return false;
458 }
459}
460
Brian Gaeked303a202004-07-16 10:31:47 +0000461static bool isPseudoInstruction (const MachineInstr *MI) {
462 switch (MI->getOpcode ()) {
463 case V8::PHI:
464 case V8::ADJCALLSTACKUP:
465 case V8::ADJCALLSTACKDOWN:
466 case V8::IMPLICIT_USE:
467 case V8::IMPLICIT_DEF:
468 return true;
469 default:
470 return false;
471 }
472}
473
Brian Gaekeceb22412004-06-18 06:27:59 +0000474/// printBaseOffsetPair - Print two consecutive operands of MI, starting at #i,
475/// which form a base + offset pair (which may have brackets around it, if
476/// brackets is true, or may be in the form base - constant, if offset is a
477/// negative constant).
478///
479void V8Printer::printBaseOffsetPair (const MachineInstr *MI, int i,
480 bool brackets) {
481 if (brackets) O << "[";
Brian Gaeke446ae112004-06-15 19:52:59 +0000482 printOperand (MI, i);
Brian Gaekeceb22412004-06-18 06:27:59 +0000483 if (MI->getOperand (i + 1).isImmediate()) {
484 int Val = (int) MI->getOperand (i + 1).getImmedValue ();
485 if (Val != 0) {
486 O << ((Val >= 0) ? " + " : " - ");
487 O << ((Val >= 0) ? Val : -Val);
488 }
489 } else {
490 O << " + ";
491 printOperand (MI, i + 1);
Brian Gaeke8005ed32004-04-07 17:33:56 +0000492 }
Brian Gaekeceb22412004-06-18 06:27:59 +0000493 if (brackets) O << "]";
Brian Gaeke1c381752004-04-06 22:10:11 +0000494}
495
Brian Gaeke4acfd032004-03-04 06:00:41 +0000496/// printMachineInstruction -- Print out a single SparcV8 LLVM instruction
497/// MI in GAS syntax to the current output stream.
498///
499void V8Printer::printMachineInstruction(const MachineInstr *MI) {
500 unsigned Opcode = MI->getOpcode();
Chris Lattner143e0ea2004-06-02 05:47:26 +0000501 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000502 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Brian Gaeked303a202004-07-16 10:31:47 +0000503
504 // If it's a pseudo-instruction, comment it out.
505 if (isPseudoInstruction (MI))
506 O << "! ";
507
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000508 O << Desc.Name << " ";
509
Brian Gaeke1c381752004-04-06 22:10:11 +0000510 // Printing memory instructions is a special case.
Brian Gaekefa4bb092004-04-07 04:29:03 +0000511 // for loads: %dest = op %base, offset --> op [%base + offset], %dest
Brian Gaeke8308d042004-06-17 22:34:19 +0000512 // for stores: op %base, offset, %src --> op %src, [%base + offset]
Brian Gaeke1c381752004-04-06 22:10:11 +0000513 if (isLoadInstruction (MI)) {
Brian Gaekefa4bb092004-04-07 04:29:03 +0000514 printBaseOffsetPair (MI, 1);
Brian Gaeke1c381752004-04-06 22:10:11 +0000515 O << ", ";
Brian Gaeke446ae112004-06-15 19:52:59 +0000516 printOperand (MI, 0);
Brian Gaeke1c381752004-04-06 22:10:11 +0000517 O << "\n";
518 return;
519 } else if (isStoreInstruction (MI)) {
Brian Gaeke8308d042004-06-17 22:34:19 +0000520 printOperand (MI, 2);
Brian Gaeke1c381752004-04-06 22:10:11 +0000521 O << ", ";
Brian Gaeke8308d042004-06-17 22:34:19 +0000522 printBaseOffsetPair (MI, 0);
Brian Gaeke1c381752004-04-06 22:10:11 +0000523 O << "\n";
524 return;
Brian Gaekeceb22412004-06-18 06:27:59 +0000525 } else if (Opcode == V8::JMPLrr) {
526 printBaseOffsetPair (MI, 1, false);
527 O << ", ";
528 printOperand (MI, 0);
529 O << "\n";
530 return;
Brian Gaeke1c381752004-04-06 22:10:11 +0000531 }
532
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000533 // print non-immediate, non-register-def operands
534 // then print immediate operands
535 // then print register-def operands.
Brian Gaeke446ae112004-06-15 19:52:59 +0000536 std::vector<int> print_order;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000537 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
538 if (!(MI->getOperand (i).isImmediate ()
539 || (MI->getOperand (i).isRegister ()
540 && MI->getOperand (i).isDef ())))
Brian Gaeke446ae112004-06-15 19:52:59 +0000541 print_order.push_back (i);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000542 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
543 if (MI->getOperand (i).isImmediate ())
Brian Gaeke446ae112004-06-15 19:52:59 +0000544 print_order.push_back (i);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000545 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
546 if (MI->getOperand (i).isRegister () && MI->getOperand (i).isDef ())
Brian Gaeke446ae112004-06-15 19:52:59 +0000547 print_order.push_back (i);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000548 for (unsigned i = 0, e = print_order.size (); i != e; ++i) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000549 printOperand (MI, print_order[i]);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000550 if (i != (print_order.size () - 1))
551 O << ", ";
552 }
553 O << "\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000554}
555
556bool V8Printer::doInitialization(Module &M) {
557 Mang = new Mangler(M);
558 return false; // success
559}
560
561// SwitchSection - Switch to the specified section of the executable if we are
562// not already in it!
563//
564static void SwitchSection(std::ostream &OS, std::string &CurSection,
565 const char *NewSection) {
566 if (CurSection != NewSection) {
567 CurSection = NewSection;
568 if (!CurSection.empty())
Brian Gaekeb27df442004-09-29 03:25:40 +0000569 OS << "\t.section \"" << NewSection << "\"\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000570 }
571}
572
573bool V8Printer::doFinalization(Module &M) {
574 const TargetData &TD = TM.getTargetData();
575 std::string CurSection;
576
577 // Print out module-level global variables here.
578 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
579 if (I->hasInitializer()) { // External global require no code
580 O << "\n\n";
581 std::string name = Mang->getValueName(I);
582 Constant *C = I->getInitializer();
583 unsigned Size = TD.getTypeSize(C->getType());
584 unsigned Align = TD.getTypeAlignment(C->getType());
585
586 if (C->isNullValue() &&
587 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
588 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
589 SwitchSection(O, CurSection, ".data");
590 if (I->hasInternalLinkage())
591 O << "\t.local " << name << "\n";
592
593 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
594 << "," << (unsigned)TD.getTypeAlignment(C->getType());
Brian Gaeke79db7402004-03-16 22:37:12 +0000595 O << "\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000596 WriteAsOperand(O, I, true, true, &M);
597 O << "\n";
598 } else {
599 switch (I->getLinkage()) {
600 case GlobalValue::LinkOnceLinkage:
601 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
602 // Nonnull linkonce -> weak
603 O << "\t.weak " << name << "\n";
604 SwitchSection(O, CurSection, "");
Brian Gaekeb27df442004-09-29 03:25:40 +0000605 O << "\t.section\t\".llvm.linkonce.d." << name << "\",\"aw\",@progbits\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000606 break;
607
608 case GlobalValue::AppendingLinkage:
609 // FIXME: appending linkage variables should go into a section of
610 // their name or something. For now, just emit them as external.
611 case GlobalValue::ExternalLinkage:
612 // If external or appending, declare as a global symbol
613 O << "\t.globl " << name << "\n";
614 // FALL THROUGH
615 case GlobalValue::InternalLinkage:
616 if (C->isNullValue())
617 SwitchSection(O, CurSection, ".bss");
618 else
619 SwitchSection(O, CurSection, ".data");
620 break;
621 }
622
623 O << "\t.align " << Align << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000624 O << "\t.type " << name << ",#object\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000625 O << "\t.size " << name << "," << Size << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000626 O << name << ":\t\t\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000627 WriteAsOperand(O, I, true, true, &M);
628 O << " = ";
629 WriteAsOperand(O, C, false, false, &M);
630 O << "\n";
631 emitGlobalConstant(C);
632 }
633 }
634
635 delete Mang;
636 return false; // success
637}