1 //===-- X86/Printer.cpp - Convert X86 code to human readable rep. ---------===//
3 // This file contains a printer that converts from our internal representation
4 // of LLVM code to a nice human readable form that is suitable for debuggging.
6 //===----------------------------------------------------------------------===//
9 #include "X86InstrInfo.h"
10 #include "llvm/Function.h"
11 #include "llvm/Constant.h"
12 #include "llvm/Target/TargetMachine.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/MachineConstantPool.h"
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "Support/Statistic.h"
17 #include "Support/hash_map"
18 #include "llvm/Type.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/SlotCalculator.h"
23 #include "Support/StringExtras.h"
24 #include "llvm/Module.h"
29 std::set<const Value*> MangledGlobals;
30 struct Printer : public MachineFunctionPass {
34 Printer(std::ostream &o) : O(o), ConstIdx(0) {}
37 virtual const char *getPassName() const {
38 return "X86 Assembly Printer";
41 void printConstantPool(MachineConstantPool *MCP);
42 bool runOnMachineFunction(MachineFunction &F);
43 std::string ConstantExprToString(const ConstantExpr* CE);
44 std::string valToExprString(const Value* V);
45 bool doInitialization(Module &M);
46 bool doFinalization(Module &M);
47 void PrintZeroBytesToPad(int numBytes);
48 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
49 void printSingleConstantValue(const Constant* CV);
51 std::map<const Value *, unsigned> NumberForBB;
54 /// createX86CodePrinterPass - Print out the specified machine code function to
55 /// the specified stream. This function should work regardless of whether or
56 /// not the function is in SSA form or not.
58 Pass *createX86CodePrinterPass(std::ostream &O) {
59 return new Printer(O);
62 // We dont want identifier names with ., space, - in them.
63 // So we replace them with _
64 static std::string makeNameProper(std::string x) {
66 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
68 case '.': tmp += "d_"; break;
69 case ' ': tmp += "s_"; break;
70 case '-': tmp += "D_"; break;
77 std::string getValueName(const Value *V) {
78 if (V->hasName()) { // Print out the label if it exists...
80 // Name mangling occurs as follows:
81 // - If V is not a global, mangling always occurs.
82 // - Otherwise, mangling occurs when any of the following are true:
83 // 1) V has internal linkage
84 // 2) V's name would collide if it is not mangled.
87 if(const GlobalValue* gv = dyn_cast<GlobalValue>(V)) {
88 if(!gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
89 // No internal linkage, name will not collide -> no mangling.
90 return makeNameProper(gv->getName());
94 // Non-global, or global with internal linkage / colliding name -> mangle.
95 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
96 makeNameProper(V->getName());
101 return "ltmp_" + itostr(Count) + "_" + utostr(V->getType()->getUniqueID());
105 // valToExprString - Helper function for ConstantExprToString().
106 // Appends result to argument string S.
108 std::string Printer::valToExprString(const Value* V) {
111 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
112 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
113 S += std::string(CB == ConstantBool::True ? "1" : "0");
114 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
115 S += itostr(CI->getValue());
116 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
117 S += utostr(CI->getValue());
118 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
119 S += ftostr(CFP->getValue());
120 else if (isa<ConstantPointerNull>(CV))
122 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
123 S += valToExprString(CPR->getValue());
124 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
125 S += ConstantExprToString(CE);
128 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
129 S += getValueName(GV);
135 assert(0 && "Cannot convert value to string");
136 S += "<illegal-value>";
141 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
142 // and return this as a string.
143 std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
145 switch(CE->getOpcode()) {
146 case Instruction::GetElementPtr:
147 { // generate a symbolic expression for the byte address
148 const Value* ptrVal = CE->getOperand(0);
149 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
150 S += "(" + valToExprString(ptrVal) + ") + ("
151 + utostr(TD->getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
155 case Instruction::Cast:
156 // Support only non-converting casts for now, i.e., a no-op.
157 // This assertion is not a complete check.
158 assert(TD->getTypeSize(CE->getType()) ==
159 TD->getTypeSize(CE->getOperand(0)->getType()));
160 S += "(" + valToExprString(CE->getOperand(0)) + ")";
163 case Instruction::Add:
164 S += "(" + valToExprString(CE->getOperand(0)) + ") + ("
165 + valToExprString(CE->getOperand(1)) + ")";
169 assert(0 && "Unsupported operator in ConstantExprToString()");
176 // Print a single constant value.
178 Printer::printSingleConstantValue(const Constant* CV)
180 assert(CV->getType() != Type::VoidTy &&
181 CV->getType() != Type::TypeTy &&
182 CV->getType() != Type::LabelTy &&
183 "Unexpected type for Constant");
185 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
186 && "Aggregate types should be handled outside this function");
188 const Type *type = CV->getType();
190 switch(type->getPrimitiveID())
192 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
195 case Type::UShortTyID: case Type::ShortTyID:
198 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
201 case Type::ULongTyID: case Type::LongTyID:
204 case Type::FloatTyID:
207 case Type::DoubleTyID:
210 case Type::ArrayTyID:
211 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
212 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
215 assert (0 && "Can't handle printing this type of array");
218 assert (0 && "Can't handle printing this type of thing");
223 if (type->isPrimitiveType())
225 if (type->isFloatingPoint()) {
226 // FP Constants are printed as integer constants to avoid losing
228 double Val = cast<ConstantFP>(CV)->getValue();
229 if (type == Type::FloatTy) {
230 float FVal = (float)Val;
231 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
232 O << *(unsigned int*)ProxyPtr;
233 } else if (type == Type::DoubleTy) {
234 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
235 O << *(uint64_t*)ProxyPtr;
237 assert(0 && "Unknown floating point type!");
240 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
242 WriteAsOperand(O, CV, false, false) << "\n";
245 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
247 // This is a constant address for a global variable or method.
248 // Use the name of the variable or method as the address value.
249 O << getValueName(CPR->getValue()) << "\n";
251 else if (isa<ConstantPointerNull>(CV))
253 // Null pointer value
256 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
258 // Constant expression built from operators, constants, and
260 O << ConstantExprToString(CE) << "\n";
264 assert(0 && "Unknown elementary type for constant");
268 // Can we treat the specified array as a string? Only if it is an array of
269 // ubytes or non-negative sbytes.
271 static bool isStringCompatible(const ConstantArray *CVA) {
272 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
273 if (ETy == Type::UByteTy) return true;
274 if (ETy != Type::SByteTy) return false;
276 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
277 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
283 // toOctal - Convert the low order bits of X into an octal letter
284 static inline char toOctal(int X) {
288 // getAsCString - Return the specified array as a C compatible string, only if
289 // the predicate isStringCompatible is true.
291 static std::string getAsCString(const ConstantArray *CVA) {
292 assert(isStringCompatible(CVA) && "Array is not string compatible!");
295 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
297 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
298 unsigned char C = (ETy == Type::SByteTy) ?
299 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
300 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
304 } else if (C == '\\') {
306 } else if (isprint(C)) {
310 case '\a': Result += "\\a"; break;
311 case '\b': Result += "\\b"; break;
312 case '\f': Result += "\\f"; break;
313 case '\n': Result += "\\n"; break;
314 case '\r': Result += "\\r"; break;
315 case '\t': Result += "\\t"; break;
316 case '\v': Result += "\\v"; break;
319 Result += toOctal(C >> 6);
320 Result += toOctal(C >> 3);
321 Result += toOctal(C >> 0);
330 // Print a constant value or values (it may be an aggregate).
331 // Uses printSingleConstantValue() to print each individual value.
333 Printer::printConstantValueOnly(const Constant* CV,
334 int numPadBytesAfter /* = 0 */)
336 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
338 if (CVA && isStringCompatible(CVA))
339 { // print the string alone and return
340 O << "\t" << ".string" << "\t" << getAsCString(CVA) << "\n";
343 { // Not a string. Print the values in successive locations
344 const std::vector<Use> &constValues = CVA->getValues();
345 for (unsigned i=0; i < constValues.size(); i++)
346 printConstantValueOnly(cast<Constant>(constValues[i].get()));
348 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
349 { // Print the fields in successive locations. Pad to align if needed!
350 const StructLayout *cvsLayout =
351 TD->getStructLayout(CVS->getType());
352 const std::vector<Use>& constValues = CVS->getValues();
353 unsigned sizeSoFar = 0;
354 for (unsigned i=0, N = constValues.size(); i < N; i++)
356 const Constant* field = cast<Constant>(constValues[i].get());
358 // Check if padding is needed and insert one or more 0s.
359 unsigned fieldSize = TD->getTypeSize(field->getType());
360 int padSize = ((i == N-1? cvsLayout->StructSize
361 : cvsLayout->MemberOffsets[i+1])
362 - cvsLayout->MemberOffsets[i]) - fieldSize;
363 sizeSoFar += (fieldSize + padSize);
365 // Now print the actual field value
366 printConstantValueOnly(field, padSize);
368 assert(sizeSoFar == cvsLayout->StructSize &&
369 "Layout of constant struct may be incorrect!");
372 printSingleConstantValue(CV);
374 if (numPadBytesAfter) {
375 unsigned numBytes = numPadBytesAfter;
376 for ( ; numBytes >= 8; numBytes -= 8)
377 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
380 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
384 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
388 // printConstantPool - Print out any constants which have been spilled to
390 void Printer::printConstantPool(MachineConstantPool *MCP){
391 const std::vector<Constant*> &CP = MCP->getConstants();
392 if (CP.empty()) return;
394 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
395 O << "\t.section .rodata\n";
396 O << "\t.align " << (unsigned)TD->getTypeAlignment(CP[i]->getType()) << "\n";
397 O << ".CPI" << i+ConstIdx << ":\t\t\t\t\t#" << *CP[i] << "\n";
398 printConstantValueOnly (CP[i]);
400 ConstIdx += CP.size(); // Don't recycle constant pool index numbers
403 /// runOnMachineFunction - This uses the X86InstructionInfo::print method
404 /// to print assembly for each instruction.
405 bool Printer::runOnMachineFunction(MachineFunction &MF) {
406 static unsigned BBNumber = 0;
407 const TargetMachine &TM = MF.getTarget();
408 const TargetInstrInfo &TII = TM.getInstrInfo();
409 TD = &TM.getTargetData();
411 // Print out constants referenced by the function
412 printConstantPool(MF.getConstantPool());
414 // Print out labels for the function.
416 O << "\t.align 16\n";
417 O << "\t.globl\t" << getValueName(MF.getFunction()) << "\n";
418 O << "\t.type\t" << getValueName(MF.getFunction()) << ", @function\n";
419 O << getValueName(MF.getFunction()) << ":\n";
422 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
424 NumberForBB[I->getBasicBlock()] = BBNumber++;
427 // Print out code for the function.
428 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
430 // Print a label for the basic block.
431 O << ".BB" << NumberForBB[I->getBasicBlock()] << ":\t# "
432 << I->getBasicBlock()->getName() << "\n";
433 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
435 // Print the assembly for the instruction.
437 TII.print(*II, O, TM);
441 // We didn't modify anything.
445 static bool isScale(const MachineOperand &MO) {
446 return MO.isImmediate() &&
447 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
448 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
451 static bool isMem(const MachineInstr *MI, unsigned Op) {
452 if (MI->getOperand(Op).isFrameIndex()) return true;
453 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
454 return Op+4 <= MI->getNumOperands() &&
455 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
456 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
459 static void printOp(std::ostream &O, const MachineOperand &MO,
460 const MRegisterInfo &RI, bool elideOffsetKeyword = false) {
461 switch (MO.getType()) {
462 case MachineOperand::MO_VirtualRegister:
463 if (Value *V = MO.getVRegValueOrNull()) {
464 O << "<" << V->getName() << ">";
468 case MachineOperand::MO_MachineRegister:
469 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
470 O << RI.get(MO.getReg()).Name;
472 O << "%reg" << MO.getReg();
475 case MachineOperand::MO_SignExtendedImmed:
476 case MachineOperand::MO_UnextendedImmed:
477 O << (int)MO.getImmedValue();
479 case MachineOperand::MO_PCRelativeDisp:
480 O << ".BB" << NumberForBB[MO.getVRegValue()] << " # PC rel: "
481 << MO.getVRegValue()->getName();
483 case MachineOperand::MO_GlobalAddress:
484 if (!elideOffsetKeyword) O << "OFFSET "; O << getValueName(MO.getGlobal());
486 case MachineOperand::MO_ExternalSymbol:
487 O << MO.getSymbolName();
490 O << "<unknown operand type>"; return;
494 static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
495 switch (Desc.TSFlags & X86II::ArgMask) {
496 default: assert(0 && "Unknown arg size!");
497 case X86II::Arg8: return "BYTE PTR";
498 case X86II::Arg16: return "WORD PTR";
499 case X86II::Arg32: return "DWORD PTR";
500 case X86II::Arg64: return "QWORD PTR";
501 case X86II::ArgF32: return "DWORD PTR";
502 case X86II::ArgF64: return "QWORD PTR";
503 case X86II::ArgF80: return "XWORD PTR";
507 static void printMemReference(std::ostream &O, const MachineInstr *MI,
508 unsigned Op, const MRegisterInfo &RI) {
509 assert(isMem(MI, Op) && "Invalid memory reference!");
511 if (MI->getOperand(Op).isFrameIndex()) {
512 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
513 if (MI->getOperand(Op+3).getImmedValue())
514 O << " + " << MI->getOperand(Op+3).getImmedValue();
517 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
518 O << "[.CPI" << MI->getOperand(Op).getConstantPoolIndex();
519 if (MI->getOperand(Op+3).getImmedValue())
520 O << " + " << MI->getOperand(Op+3).getImmedValue();
525 const MachineOperand &BaseReg = MI->getOperand(Op);
526 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
527 const MachineOperand &IndexReg = MI->getOperand(Op+2);
528 int DispVal = MI->getOperand(Op+3).getImmedValue();
531 bool NeedPlus = false;
532 if (BaseReg.getReg()) {
533 printOp(O, BaseReg, RI);
537 if (IndexReg.getReg()) {
538 if (NeedPlus) O << " + ";
540 O << ScaleVal << "*";
541 printOp(O, IndexReg, RI);
558 // print - Print out an x86 instruction in intel syntax
559 void X86InstrInfo::print(const MachineInstr *MI, std::ostream &O,
560 const TargetMachine &TM) const {
561 unsigned Opcode = MI->getOpcode();
562 const TargetInstrDescriptor &Desc = get(Opcode);
564 switch (Desc.TSFlags & X86II::FormMask) {
566 // Print pseudo-instructions as comments; either they should have been
567 // turned into real instructions by now, or they don't need to be
568 // seen by the assembler (e.g., IMPLICIT_USEs.)
570 if (Opcode == X86::PHI) {
571 printOp(O, MI->getOperand(0), RI);
573 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
574 if (i != 1) O << ", ";
576 printOp(O, MI->getOperand(i), RI);
578 printOp(O, MI->getOperand(i+1), RI);
583 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
584 MI->getOperand(0).opIsDefAndUse())) {
585 printOp(O, MI->getOperand(0), RI);
589 O << getName(MI->getOpcode());
591 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
593 if (MI->getOperand(i).opIsDefOnly() ||
594 MI->getOperand(i).opIsDefAndUse()) O << "*";
595 printOp(O, MI->getOperand(i), RI);
596 if (MI->getOperand(i).opIsDefOnly() ||
597 MI->getOperand(i).opIsDefAndUse()) O << "*";
604 // The accepted forms of Raw instructions are:
605 // 1. nop - No operand required
606 // 2. jmp foo - PC relative displacement operand
607 // 3. call bar - GlobalAddress Operand or External Symbol Operand
609 assert(MI->getNumOperands() == 0 ||
610 (MI->getNumOperands() == 1 &&
611 (MI->getOperand(0).isPCRelativeDisp() ||
612 MI->getOperand(0).isGlobalAddress() ||
613 MI->getOperand(0).isExternalSymbol())) &&
614 "Illegal raw instruction!");
615 O << getName(MI->getOpcode()) << " ";
617 if (MI->getNumOperands() == 1) {
618 printOp(O, MI->getOperand(0), RI, true); // Don't print "OFFSET"...
623 case X86II::AddRegFrm: {
624 // There are currently two forms of acceptable AddRegFrm instructions.
625 // Either the instruction JUST takes a single register (like inc, dec, etc),
626 // or it takes a register and an immediate of the same size as the register
627 // (move immediate f.e.). Note that this immediate value might be stored as
628 // an LLVM value, to represent, for example, loading the address of a global
629 // into a register. The initial register might be duplicated if this is a
630 // M_2_ADDR_REG instruction
632 assert(MI->getOperand(0).isRegister() &&
633 (MI->getNumOperands() == 1 ||
634 (MI->getNumOperands() == 2 &&
635 (MI->getOperand(1).getVRegValueOrNull() ||
636 MI->getOperand(1).isImmediate() ||
637 MI->getOperand(1).isRegister() ||
638 MI->getOperand(1).isGlobalAddress() ||
639 MI->getOperand(1).isExternalSymbol()))) &&
640 "Illegal form for AddRegFrm instruction!");
642 unsigned Reg = MI->getOperand(0).getReg();
644 O << getName(MI->getOpCode()) << " ";
645 printOp(O, MI->getOperand(0), RI);
646 if (MI->getNumOperands() == 2 &&
647 (!MI->getOperand(1).isRegister() ||
648 MI->getOperand(1).getVRegValueOrNull() ||
649 MI->getOperand(1).isGlobalAddress() ||
650 MI->getOperand(1).isExternalSymbol())) {
652 printOp(O, MI->getOperand(1), RI);
657 case X86II::MRMDestReg: {
658 // There are two acceptable forms of MRMDestReg instructions, those with 2,
661 // 2 Operands: this is for things like mov that do not read a second input
663 // 3 Operands: in this form, the first two registers (the destination, and
664 // the first operand) should be the same, post register allocation. The 3rd
665 // operand is an additional input. This should be for things like add
668 // 4 Operands: This form is for instructions which are 3 operands forms, but
669 // have a constant argument as well.
671 bool isTwoAddr = isTwoAddrInstr(Opcode);
672 assert(MI->getOperand(0).isRegister() &&
673 (MI->getNumOperands() == 2 ||
674 (isTwoAddr && MI->getOperand(1).isRegister() &&
675 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
676 (MI->getNumOperands() == 3 ||
677 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
678 && "Bad format for MRMDestReg!");
680 O << getName(MI->getOpCode()) << " ";
681 printOp(O, MI->getOperand(0), RI);
683 printOp(O, MI->getOperand(1+isTwoAddr), RI);
684 if (MI->getNumOperands() == 4) {
686 printOp(O, MI->getOperand(3), RI);
692 case X86II::MRMDestMem: {
693 // These instructions are the same as MRMDestReg, but instead of having a
694 // register reference for the mod/rm field, it's a memory reference.
696 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
697 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
699 O << getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
700 printMemReference(O, MI, 0, RI);
702 printOp(O, MI->getOperand(4), RI);
707 case X86II::MRMSrcReg: {
708 // There is a two forms that are acceptable for MRMSrcReg instructions,
709 // those with 3 and 2 operands:
711 // 3 Operands: in this form, the last register (the second input) is the
712 // ModR/M input. The first two operands should be the same, post register
713 // allocation. This is for things like: add r32, r/m32
715 // 2 Operands: this is for things like mov that do not read a second input
717 assert(MI->getOperand(0).isRegister() &&
718 MI->getOperand(1).isRegister() &&
719 (MI->getNumOperands() == 2 ||
720 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
721 && "Bad format for MRMSrcReg!");
722 if (MI->getNumOperands() == 3 &&
723 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
726 O << getName(MI->getOpCode()) << " ";
727 printOp(O, MI->getOperand(0), RI);
729 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
734 case X86II::MRMSrcMem: {
735 // These instructions are the same as MRMSrcReg, but instead of having a
736 // register reference for the mod/rm field, it's a memory reference.
738 assert(MI->getOperand(0).isRegister() &&
739 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
740 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
742 && "Bad format for MRMDestReg!");
743 if (MI->getNumOperands() == 2+4 &&
744 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
747 O << getName(MI->getOpCode()) << " ";
748 printOp(O, MI->getOperand(0), RI);
749 O << ", " << sizePtr(Desc) << " ";
750 printMemReference(O, MI, MI->getNumOperands()-4, RI);
755 case X86II::MRMS0r: case X86II::MRMS1r:
756 case X86II::MRMS2r: case X86II::MRMS3r:
757 case X86II::MRMS4r: case X86II::MRMS5r:
758 case X86II::MRMS6r: case X86II::MRMS7r: {
759 // In this form, the following are valid formats:
761 // 2. cmp reg, immediate
762 // 2. shl rdest, rinput <implicit CL or 1>
763 // 3. sbb rdest, rinput, immediate [rdest = rinput]
765 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
766 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
767 assert((MI->getNumOperands() != 2 ||
768 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
769 "Bad MRMSxR format!");
770 assert((MI->getNumOperands() < 3 ||
771 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
772 "Bad MRMSxR format!");
774 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
775 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
778 O << getName(MI->getOpCode()) << " ";
779 printOp(O, MI->getOperand(0), RI);
780 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
782 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
789 case X86II::MRMS0m: case X86II::MRMS1m:
790 case X86II::MRMS2m: case X86II::MRMS3m:
791 case X86II::MRMS4m: case X86II::MRMS5m:
792 case X86II::MRMS6m: case X86II::MRMS7m: {
793 // In this form, the following are valid formats:
795 // 2. cmp [m], immediate
796 // 2. shl [m], rinput <implicit CL or 1>
797 // 3. sbb [m], immediate
799 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
800 isMem(MI, 0) && "Bad MRMSxM format!");
801 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
802 "Bad MRMSxM format!");
804 O << getName(MI->getOpCode()) << " ";
805 O << sizePtr(Desc) << " ";
806 printMemReference(O, MI, 0, RI);
807 if (MI->getNumOperands() == 5) {
809 printOp(O, MI->getOperand(4), RI);
816 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
820 bool Printer::doInitialization(Module &M)
822 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
823 // with no % decorations on register names.
824 O << "\t.intel_syntax noprefix\n";
826 // Ripped from CWriter:
827 // Calculate which global values have names that will collide when we throw
828 // away type information.
829 { // Scope to delete the FoundNames set when we are done with it...
830 std::set<std::string> FoundNames;
831 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
832 if (I->hasName()) // If the global has a name...
833 if (FoundNames.count(I->getName())) // And the name is already used
834 MangledGlobals.insert(I); // Mangle the name
836 FoundNames.insert(I->getName()); // Otherwise, keep track of name
838 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
839 if (I->hasName()) // If the global has a name...
840 if (FoundNames.count(I->getName())) // And the name is already used
841 MangledGlobals.insert(I); // Mangle the name
843 FoundNames.insert(I->getName()); // Otherwise, keep track of name
846 return false; // success
849 bool Printer::doFinalization(Module &M)
851 // Print out module-level global variables here.
852 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
853 std::string name(getValueName(I));
854 if (I->hasInitializer()) {
855 Constant *C = I->getInitializer();
857 O << "\t.globl " << name << "\n";
858 O << "\t.type " << name << ",@object\n";
859 O << "\t.size " << name << ","
860 << (unsigned)TD->getTypeSize(I->getType()) << "\n";
861 O << "\t.align " << (unsigned)TD->getTypeAlignment(C->getType()) << "\n";
862 O << name << ":\t\t\t\t\t#" << *C << "\n";
863 printConstantValueOnly (C);
865 O << "\t.globl " << name << "\n";
866 O << "\t.comm " << name << ", "
867 << (unsigned)TD->getTypeSize(I->getType()) << ", "
868 << (unsigned)TD->getTypeAlignment(I->getType()) << "\n";
871 MangledGlobals.clear();
872 return false; // success