1 //===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/MathExtras.h"
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36 struct SparcV8AsmPrinter : public MachineFunctionPass {
37 /// Output stream on which we're printing assembly code.
41 /// Target machine description which we query for reg. names, data
46 /// Name-mangler for global names.
50 SparcV8AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
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().
56 typedef std::map<const Value *, unsigned> ValueMapTy;
57 ValueMapTy NumberForBB;
59 /// Cache of mangled name for current function. This is
60 /// recalculated at the beginning of each call to
61 /// runOnMachineFunction().
63 std::string CurrentFnName;
65 virtual const char *getPassName() const {
66 return "SparcV8 Assembly Printer";
69 void emitConstantValueOnly(const Constant *CV);
70 void emitGlobalConstant(const Constant *CV);
71 void printConstantPool(MachineConstantPool *MCP);
72 void printOperand(const MachineInstr *MI, int opNum);
73 void printBaseOffsetPair (const MachineInstr *MI, int i);
74 void printMachineInstruction(const MachineInstr *MI);
75 bool printInstruction(const MachineInstr *MI); // autogenerated.
76 bool runOnMachineFunction(MachineFunction &F);
77 bool doInitialization(Module &M);
78 bool doFinalization(Module &M);
80 } // end of anonymous namespace
82 #include "SparcV8GenAsmWriter.inc"
84 /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
85 /// assembly code for a MachineFunction to the given output stream,
86 /// using the given target machine description. This should work
87 /// regardless of whether the function is in SSA form.
89 FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
91 return new SparcV8AsmPrinter(o, tm);
94 /// toOctal - Convert the low order bits of X into an octal digit.
96 static inline char toOctal(int X) {
100 /// getAsCString - Return the specified array as a C compatible
101 /// string, only if the predicate isStringCompatible is true.
103 static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
104 assert(CVA->isString() && "Array is not string compatible!");
107 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
108 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
112 } else if (C == '\\') {
114 } else if (isprint(C)) {
118 case '\b': O << "\\b"; break;
119 case '\f': O << "\\f"; break;
120 case '\n': O << "\\n"; break;
121 case '\r': O << "\\r"; break;
122 case '\t': O << "\\t"; break;
125 O << toOctal(C >> 6);
126 O << toOctal(C >> 3);
127 O << toOctal(C >> 0);
135 // Print out the specified constant, without a storage class. Only the
136 // constants valid in constant expressions can occur here.
137 void SparcV8AsmPrinter::emitConstantValueOnly(const Constant *CV) {
138 if (CV->isNullValue() || isa<UndefValue> (CV))
140 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
141 assert(CB == ConstantBool::True);
143 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
144 if (((CI->getValue() << 32) >> 32) == CI->getValue())
147 O << (unsigned long long)CI->getValue();
148 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
150 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
151 // This is a constant address for a global variable or function. Use the
152 // name of the variable or function as the address value.
153 O << Mang->getValueName(GV);
154 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
155 const TargetData &TD = TM.getTargetData();
156 switch(CE->getOpcode()) {
157 case Instruction::GetElementPtr: {
158 // generate a symbolic expression for the byte address
159 const Constant *ptrVal = CE->getOperand(0);
160 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
161 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
163 emitConstantValueOnly(ptrVal);
164 O << ") + " << Offset;
166 emitConstantValueOnly(ptrVal);
170 case Instruction::Cast: {
171 // Support only non-converting or widening casts for now, that is, ones
172 // that do not involve a change in value. This assertion is really gross,
173 // and may not even be a complete check.
174 Constant *Op = CE->getOperand(0);
175 const Type *OpTy = Op->getType(), *Ty = CE->getType();
177 // Pointers on ILP32 machines can be losslessly converted back and
178 // forth into 32-bit or wider integers, regardless of signedness.
179 assert(((isa<PointerType>(OpTy)
180 && (Ty == Type::LongTy || Ty == Type::ULongTy
181 || Ty == Type::IntTy || Ty == Type::UIntTy))
182 || (isa<PointerType>(Ty)
183 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
184 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
185 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
186 && OpTy->isLosslesslyConvertibleTo(Ty))))
187 && "FIXME: Don't yet support this kind of constant cast expr");
189 emitConstantValueOnly(Op);
193 case Instruction::Add:
195 emitConstantValueOnly(CE->getOperand(0));
197 emitConstantValueOnly(CE->getOperand(1));
201 assert(0 && "Unsupported operator!");
204 assert(0 && "Unknown constant value!");
208 // Print a constant value or values, with the appropriate storage class as a
210 void SparcV8AsmPrinter::emitGlobalConstant(const Constant *CV) {
211 const TargetData &TD = TM.getTargetData();
213 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
214 if (CVA->isString()) {
216 printAsCString(O, CVA);
218 } else { // Not a string. Print the values in successive locations
219 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++)
220 emitGlobalConstant(CVA->getOperand(i));
223 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
224 // Print the fields in successive locations. Pad to align if needed!
225 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
226 unsigned sizeSoFar = 0;
227 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) {
228 const Constant* field = CVS->getOperand(i);
230 // Check if padding is needed and insert one or more 0s.
231 unsigned fieldSize = TD.getTypeSize(field->getType());
232 unsigned padSize = ((i == e-1? cvsLayout->StructSize
233 : cvsLayout->MemberOffsets[i+1])
234 - cvsLayout->MemberOffsets[i]) - fieldSize;
235 sizeSoFar += fieldSize + padSize;
237 // Now print the actual field value
238 emitGlobalConstant(field);
240 // Insert the field padding unless it's zero bytes...
242 O << "\t.skip\t " << padSize << "\n";
244 assert(sizeSoFar == cvsLayout->StructSize &&
245 "Layout of constant struct may be incorrect!");
247 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
248 // FP Constants are printed as integer constants to avoid losing
250 double Val = CFP->getValue();
251 switch (CFP->getType()->getTypeID()) {
252 default: assert(0 && "Unknown floating point type!");
253 case Type::FloatTyID: {
254 O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n";
257 case Type::DoubleTyID: {
258 O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n";
259 O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n";
263 } else if (isa<UndefValue> (CV)) {
264 unsigned size = TD.getTypeSize (CV->getType ());
265 O << "\t.skip\t " << size << "\n";
267 } else if (isa<ConstantAggregateZero> (CV)) {
268 unsigned size = TD.getTypeSize (CV->getType ());
269 for (unsigned i = 0; i < size; ++i)
274 const Type *type = CV->getType();
276 switch (type->getTypeID()) {
277 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
280 case Type::UShortTyID: case Type::ShortTyID:
283 case Type::FloatTyID: case Type::PointerTyID:
284 case Type::UIntTyID: case Type::IntTyID:
287 case Type::DoubleTyID:
288 case Type::ULongTyID: case Type::LongTyID:
292 assert (0 && "Can't handle printing this type of thing");
296 emitConstantValueOnly(CV);
300 /// printConstantPool - Print to the current output stream assembly
301 /// representations of the constants in the constant pool MCP. This is
302 /// used to print out constants which have been "spilled to memory" by
303 /// the code generator.
305 void SparcV8AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
306 const std::vector<Constant*> &CP = MCP->getConstants();
307 const TargetData &TD = TM.getTargetData();
309 if (CP.empty()) return;
311 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
312 O << "\t.section \".rodata\"\n";
313 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
315 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t!"
317 emitGlobalConstant(CP[i]);
321 /// runOnMachineFunction - This uses the printMachineInstruction()
322 /// method to print assembly for each instruction.
324 bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
325 // BBNumber is used here so that a given Printer will never give two
326 // BBs the same name. (If you have a better way, please let me know!)
327 static unsigned BBNumber = 0;
330 // What's my mangled name?
331 CurrentFnName = Mang->getValueName(MF.getFunction());
333 // Print out constants referenced by the function
334 printConstantPool(MF.getConstantPool());
336 // Print out labels for the function.
338 O << "\t.align 16\n";
339 O << "\t.globl\t" << CurrentFnName << "\n";
340 O << "\t.type\t" << CurrentFnName << ", #function\n";
341 O << CurrentFnName << ":\n";
343 // Number each basic block so that we can consistently refer to them
344 // in PC-relative references.
346 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
348 NumberForBB[I->getBasicBlock()] = BBNumber++;
351 // Print out code for the function.
352 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
354 // Print a label for the basic block.
355 O << ".LBB" << Mang->getValueName(MF.getFunction ())
356 << "_" << I->getNumber () << ":\t! "
357 << I->getBasicBlock ()->getName () << "\n";
358 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
360 // Print the assembly for the instruction.
361 printMachineInstruction(II);
365 // We didn't modify anything.
369 void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
370 const MachineOperand &MO = MI->getOperand (opNum);
371 const MRegisterInfo &RI = *TM.getRegisterInfo();
372 bool CloseParen = false;
373 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
376 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
381 switch (MO.getType()) {
382 case MachineOperand::MO_VirtualRegister:
383 if (Value *V = MO.getVRegValueOrNull()) {
384 O << "<" << V->getName() << ">";
388 case MachineOperand::MO_MachineRegister:
389 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
390 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
392 O << "%reg" << MO.getReg();
395 case MachineOperand::MO_SignExtendedImmed:
396 case MachineOperand::MO_UnextendedImmed:
397 O << (int)MO.getImmedValue();
399 case MachineOperand::MO_MachineBasicBlock: {
400 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
401 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
402 << "_" << MBBOp->getNumber () << "\t! "
403 << MBBOp->getBasicBlock ()->getName ();
406 case MachineOperand::MO_PCRelativeDisp:
407 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
410 case MachineOperand::MO_GlobalAddress:
411 O << Mang->getValueName(MO.getGlobal());
413 case MachineOperand::MO_ExternalSymbol:
414 O << MO.getSymbolName();
416 case MachineOperand::MO_ConstantPoolIndex:
417 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
420 O << "<unknown operand type>"; abort (); break;
422 if (CloseParen) O << ")";
425 static bool isLoadInstruction (const MachineInstr *MI) {
426 switch (MI->getOpcode ()) {
443 static bool isStoreInstruction (const MachineInstr *MI) {
444 switch (MI->getOpcode ()) {
459 static bool isPseudoInstruction (const MachineInstr *MI) {
460 switch (MI->getOpcode ()) {
462 case V8::ADJCALLSTACKUP:
463 case V8::ADJCALLSTACKDOWN:
464 case V8::IMPLICIT_USE:
465 case V8::IMPLICIT_DEF:
472 /// printBaseOffsetPair - Print two consecutive operands of MI, starting at #i,
473 /// which form a base + offset pair (which may have brackets around it, if
474 /// brackets is true, or may be in the form base - constant, if offset is a
475 /// negative constant).
477 void SparcV8AsmPrinter::printBaseOffsetPair (const MachineInstr *MI, int i) {
479 printOperand (MI, i);
480 if (MI->getOperand (i + 1).isImmediate()) {
481 int Val = (int) MI->getOperand (i + 1).getImmedValue ();
483 O << ((Val >= 0) ? " + " : " - ");
484 O << ((Val >= 0) ? Val : -Val);
488 printOperand (MI, i + 1);
493 /// printMachineInstruction -- Print out a single SparcV8 LLVM instruction
494 /// MI in GAS syntax to the current output stream.
496 void SparcV8AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
498 if (printInstruction(MI)) return;
500 unsigned Opcode = MI->getOpcode();
501 const TargetInstrInfo &TII = *TM.getInstrInfo();
502 const TargetInstrDescriptor &Desc = TII.get(Opcode);
504 // If it's a pseudo-instruction, comment it out.
505 if (isPseudoInstruction (MI))
508 O << Desc.Name << " ";
510 // Printing memory instructions is a special case.
511 // for loads: %dest = op %base, offset --> op [%base + offset], %dest
512 // for stores: op %base, offset, %src --> op %src, [%base + offset]
513 if (isLoadInstruction (MI)) {
514 printBaseOffsetPair (MI, 1);
516 printOperand (MI, 0);
519 } else if (isStoreInstruction (MI)) {
520 printOperand (MI, 2);
522 printBaseOffsetPair (MI, 0);
527 // print non-immediate, non-register-def operands
528 // then print immediate operands
529 // then print register-def operands.
530 std::vector<int> print_order;
531 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
532 if (!(MI->getOperand (i).isImmediate ()
533 || (MI->getOperand (i).isRegister ()
534 && MI->getOperand (i).isDef ())))
535 print_order.push_back (i);
536 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
537 if (MI->getOperand (i).isImmediate ())
538 print_order.push_back (i);
539 for (unsigned i = 0; i < MI->getNumOperands (); ++i)
540 if (MI->getOperand (i).isRegister () && MI->getOperand (i).isDef ())
541 print_order.push_back (i);
542 for (unsigned i = 0, e = print_order.size (); i != e; ++i) {
543 printOperand (MI, print_order[i]);
544 if (i != (print_order.size () - 1))
550 bool SparcV8AsmPrinter::doInitialization(Module &M) {
551 Mang = new Mangler(M);
552 return false; // success
555 // SwitchSection - Switch to the specified section of the executable if we are
556 // not already in it!
558 static void SwitchSection(std::ostream &OS, std::string &CurSection,
559 const char *NewSection) {
560 if (CurSection != NewSection) {
561 CurSection = NewSection;
562 if (!CurSection.empty())
563 OS << "\t.section \"" << NewSection << "\"\n";
567 bool SparcV8AsmPrinter::doFinalization(Module &M) {
568 const TargetData &TD = TM.getTargetData();
569 std::string CurSection;
571 // Print out module-level global variables here.
572 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
573 if (I->hasInitializer()) { // External global require no code
575 std::string name = Mang->getValueName(I);
576 Constant *C = I->getInitializer();
577 unsigned Size = TD.getTypeSize(C->getType());
578 unsigned Align = TD.getTypeAlignment(C->getType());
580 if (C->isNullValue() &&
581 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
582 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
583 SwitchSection(O, CurSection, ".data");
584 if (I->hasInternalLinkage())
585 O << "\t.local " << name << "\n";
587 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
588 << "," << (unsigned)TD.getTypeAlignment(C->getType());
590 WriteAsOperand(O, I, true, true, &M);
593 switch (I->getLinkage()) {
594 case GlobalValue::LinkOnceLinkage:
595 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
596 // Nonnull linkonce -> weak
597 O << "\t.weak " << name << "\n";
598 SwitchSection(O, CurSection, "");
599 O << "\t.section\t\".llvm.linkonce.d." << name << "\",\"aw\",@progbits\n";
602 case GlobalValue::AppendingLinkage:
603 // FIXME: appending linkage variables should go into a section of
604 // their name or something. For now, just emit them as external.
605 case GlobalValue::ExternalLinkage:
606 // If external or appending, declare as a global symbol
607 O << "\t.globl " << name << "\n";
609 case GlobalValue::InternalLinkage:
610 if (C->isNullValue())
611 SwitchSection(O, CurSection, ".bss");
613 SwitchSection(O, CurSection, ".data");
615 case GlobalValue::GhostLinkage:
616 std::cerr << "Should not have any unmaterialized functions!\n";
620 O << "\t.align " << Align << "\n";
621 O << "\t.type " << name << ",#object\n";
622 O << "\t.size " << name << "," << Size << "\n";
623 O << name << ":\t\t\t\t! ";
624 WriteAsOperand(O, I, true, true, &M);
626 WriteAsOperand(O, C, false, false, &M);
628 emitGlobalConstant(C);
633 return false; // success