blob: 74820c8690ddc2437af891874fc4e379374f3ceb [file] [log] [blame]
Chris Lattner72614082002-10-25 22:55:53 +00001//===-- X86/Printer.cpp - Convert X86 code to human readable rep. ---------===//
2//
3// This file contains a printer that converts from our internal representation
Brian Gaekeb44210d2003-07-07 18:34:20 +00004// of LLVM code to a nice human readable form that is suitable for debugging.
Chris Lattner72614082002-10-25 22:55:53 +00005//
6//===----------------------------------------------------------------------===//
7
8#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +00009#include "X86InstrInfo.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000010#include "llvm/Function.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000011#include "llvm/Constant.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000012#include "llvm/Target/TargetMachine.h"
Chris Lattner0285a332002-12-28 20:25:38 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000014#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner233ad712002-11-21 01:33:44 +000016#include "Support/Statistic.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000017#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"
Chris Lattner72614082002-10-25 22:55:53 +000025
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000026namespace {
Brian Gaeked7908f62003-06-27 00:00:48 +000027 std::set<const Value *> MangledGlobals;
Chris Lattner0285a332002-12-28 20:25:38 +000028 struct Printer : public MachineFunctionPass {
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000029 std::ostream &O;
Brian Gaeked7908f62003-06-27 00:00:48 +000030 typedef std::map<const Value *, unsigned> ValueMapTy;
31 ValueMapTy NumberForBB;
Brian Gaeke5e001572003-06-26 18:02:30 +000032 Printer(std::ostream &o) : O(o) {}
Brian Gaeke01d79ff2003-06-25 18:01:07 +000033 const TargetData *TD;
Brian Gaeked7908f62003-06-27 00:00:48 +000034 std::string CurrentFnName;
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000035 virtual const char *getPassName() const {
36 return "X86 Assembly Printer";
37 }
38
Brian Gaeked7908f62003-06-27 00:00:48 +000039 void printMachineInstruction(const MachineInstr *MI, std::ostream &O,
40 const TargetMachine &TM) const;
41 void printOp(std::ostream &O, const MachineOperand &MO,
42 const MRegisterInfo &RI, bool elideOffsetKeyword = false) const;
43 void printMemReference(std::ostream &O, const MachineInstr *MI,
44 unsigned Op,
45 const MRegisterInfo &RI) const;
Brian Gaeke01d79ff2003-06-25 18:01:07 +000046 void printConstantPool(MachineConstantPool *MCP);
47 bool runOnMachineFunction(MachineFunction &F);
48 std::string ConstantExprToString(const ConstantExpr* CE);
49 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000050 bool doInitialization(Module &M);
51 bool doFinalization(Module &M);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000052 void PrintZeroBytesToPad(int numBytes);
53 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
54 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000055 };
Brian Gaeked7908f62003-06-27 00:00:48 +000056} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000057
Chris Lattnerdbb61c62002-11-17 22:53:13 +000058/// createX86CodePrinterPass - Print out the specified machine code function to
59/// the specified stream. This function should work regardless of whether or
60/// not the function is in SSA form or not.
61///
Chris Lattner0285a332002-12-28 20:25:38 +000062Pass *createX86CodePrinterPass(std::ostream &O) {
63 return new Printer(O);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000064}
65
Brian Gaeked7908f62003-06-27 00:00:48 +000066// We don't want identifier names with ., space, or - in them,
67// so we replace them with underscores.
Brian Gaekebc601fe2003-06-25 22:00:39 +000068static std::string makeNameProper(std::string x) {
69 std::string tmp;
70 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
71 switch (*sI) {
72 case '.': tmp += "d_"; break;
73 case ' ': tmp += "s_"; break;
74 case '-': tmp += "D_"; break;
75 default: tmp += *sI;
76 }
Brian Gaekebc601fe2003-06-25 22:00:39 +000077 return tmp;
78}
79
Brian Gaeked7908f62003-06-27 00:00:48 +000080static std::string getValueName(const Value *V) {
Brian Gaekebc601fe2003-06-25 22:00:39 +000081 if (V->hasName()) { // Print out the label if it exists...
Brian Gaekebc601fe2003-06-25 22:00:39 +000082 // Name mangling occurs as follows:
83 // - If V is not a global, mangling always occurs.
84 // - Otherwise, mangling occurs when any of the following are true:
85 // 1) V has internal linkage
86 // 2) V's name would collide if it is not mangled.
87 //
Brian Gaekebc601fe2003-06-25 22:00:39 +000088 if(const GlobalValue* gv = dyn_cast<GlobalValue>(V)) {
89 if(!gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
90 // No internal linkage, name will not collide -> no mangling.
91 return makeNameProper(gv->getName());
92 }
93 }
Brian Gaekebc601fe2003-06-25 22:00:39 +000094 // Non-global, or global with internal linkage / colliding name -> mangle.
95 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
96 makeNameProper(V->getName());
97 }
Brian Gaekebc601fe2003-06-25 22:00:39 +000098 static int Count = 0;
99 Count++;
100 return "ltmp_" + itostr(Count) + "_" + utostr(V->getType()->getUniqueID());
101}
102
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000103// valToExprString - Helper function for ConstantExprToString().
104// Appends result to argument string S.
105//
106std::string Printer::valToExprString(const Value* V) {
107 std::string S;
108 bool failed = false;
109 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
110 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
111 S += std::string(CB == ConstantBool::True ? "1" : "0");
112 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
113 S += itostr(CI->getValue());
114 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
115 S += utostr(CI->getValue());
116 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
117 S += ftostr(CFP->getValue());
118 else if (isa<ConstantPointerNull>(CV))
119 S += "0";
120 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
121 S += valToExprString(CPR->getValue());
122 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
123 S += ConstantExprToString(CE);
124 else
125 failed = true;
126 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000127 S += getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000128 }
129 else
130 failed = true;
131
132 if (failed) {
133 assert(0 && "Cannot convert value to string");
134 S += "<illegal-value>";
135 }
136 return S;
137}
138
139// ConstantExprToString() - Convert a ConstantExpr to an asm expression
140// and return this as a string.
141std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
142 std::string S;
143 switch(CE->getOpcode()) {
144 case Instruction::GetElementPtr:
145 { // generate a symbolic expression for the byte address
146 const Value* ptrVal = CE->getOperand(0);
147 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
148 S += "(" + valToExprString(ptrVal) + ") + ("
149 + utostr(TD->getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
150 break;
151 }
152
153 case Instruction::Cast:
154 // Support only non-converting casts for now, i.e., a no-op.
155 // This assertion is not a complete check.
156 assert(TD->getTypeSize(CE->getType()) ==
157 TD->getTypeSize(CE->getOperand(0)->getType()));
158 S += "(" + valToExprString(CE->getOperand(0)) + ")";
159 break;
160
161 case Instruction::Add:
162 S += "(" + valToExprString(CE->getOperand(0)) + ") + ("
163 + valToExprString(CE->getOperand(1)) + ")";
164 break;
165
166 default:
167 assert(0 && "Unsupported operator in ConstantExprToString()");
168 break;
169 }
170
171 return S;
172}
173
174// Print a single constant value.
175void
176Printer::printSingleConstantValue(const Constant* CV)
177{
178 assert(CV->getType() != Type::VoidTy &&
179 CV->getType() != Type::TypeTy &&
180 CV->getType() != Type::LabelTy &&
181 "Unexpected type for Constant");
182
183 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
184 && "Aggregate types should be handled outside this function");
185
186 const Type *type = CV->getType();
187 O << "\t";
188 switch(type->getPrimitiveID())
189 {
190 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
191 O << ".byte";
192 break;
193 case Type::UShortTyID: case Type::ShortTyID:
194 O << ".word";
195 break;
196 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
197 O << ".long";
198 break;
199 case Type::ULongTyID: case Type::LongTyID:
200 O << ".quad";
201 break;
202 case Type::FloatTyID:
203 O << ".long";
204 break;
205 case Type::DoubleTyID:
206 O << ".quad";
207 break;
208 case Type::ArrayTyID:
209 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
210 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
211 O << ".string";
212 else
213 assert (0 && "Can't handle printing this type of array");
214 break;
215 default:
216 assert (0 && "Can't handle printing this type of thing");
217 break;
218 }
219 O << "\t";
220
221 if (type->isPrimitiveType())
222 {
223 if (type->isFloatingPoint()) {
224 // FP Constants are printed as integer constants to avoid losing
225 // precision...
226 double Val = cast<ConstantFP>(CV)->getValue();
227 if (type == Type::FloatTy) {
228 float FVal = (float)Val;
229 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
230 O << *(unsigned int*)ProxyPtr;
231 } else if (type == Type::DoubleTy) {
232 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
233 O << *(uint64_t*)ProxyPtr;
234 } else {
235 assert(0 && "Unknown floating point type!");
236 }
237
238 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
239 } else {
240 WriteAsOperand(O, CV, false, false) << "\n";
241 }
242 }
243 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
244 {
245 // This is a constant address for a global variable or method.
246 // Use the name of the variable or method as the address value.
Brian Gaekebc601fe2003-06-25 22:00:39 +0000247 O << getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000248 }
249 else if (isa<ConstantPointerNull>(CV))
250 {
251 // Null pointer value
252 O << "0\n";
253 }
254 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
255 {
256 // Constant expression built from operators, constants, and
257 // symbolic addrs
258 O << ConstantExprToString(CE) << "\n";
259 }
260 else
261 {
262 assert(0 && "Unknown elementary type for constant");
263 }
264}
265
266// Can we treat the specified array as a string? Only if it is an array of
267// ubytes or non-negative sbytes.
268//
269static bool isStringCompatible(const ConstantArray *CVA) {
270 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
271 if (ETy == Type::UByteTy) return true;
272 if (ETy != Type::SByteTy) return false;
273
274 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
275 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
276 return false;
277
278 return true;
279}
280
281// toOctal - Convert the low order bits of X into an octal letter
282static inline char toOctal(int X) {
283 return (X&7)+'0';
284}
285
286// getAsCString - Return the specified array as a C compatible string, only if
287// the predicate isStringCompatible is true.
288//
289static std::string getAsCString(const ConstantArray *CVA) {
290 assert(isStringCompatible(CVA) && "Array is not string compatible!");
291
292 std::string Result;
293 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
294 Result = "\"";
295 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
296 unsigned char C = (ETy == Type::SByteTy) ?
297 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
298 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
299
300 if (C == '"') {
301 Result += "\\\"";
302 } else if (C == '\\') {
303 Result += "\\\\";
304 } else if (isprint(C)) {
305 Result += C;
306 } else {
307 switch(C) {
308 case '\a': Result += "\\a"; break;
309 case '\b': Result += "\\b"; break;
310 case '\f': Result += "\\f"; break;
311 case '\n': Result += "\\n"; break;
312 case '\r': Result += "\\r"; break;
313 case '\t': Result += "\\t"; break;
314 case '\v': Result += "\\v"; break;
315 default:
316 Result += '\\';
317 Result += toOctal(C >> 6);
318 Result += toOctal(C >> 3);
319 Result += toOctal(C >> 0);
320 break;
321 }
322 }
323 }
324 Result += "\"";
325 return Result;
326}
327
328// Print a constant value or values (it may be an aggregate).
329// Uses printSingleConstantValue() to print each individual value.
330void
331Printer::printConstantValueOnly(const Constant* CV,
332 int numPadBytesAfter /* = 0 */)
333{
334 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
335
336 if (CVA && isStringCompatible(CVA))
337 { // print the string alone and return
338 O << "\t" << ".string" << "\t" << getAsCString(CVA) << "\n";
339 }
340 else if (CVA)
341 { // Not a string. Print the values in successive locations
342 const std::vector<Use> &constValues = CVA->getValues();
343 for (unsigned i=0; i < constValues.size(); i++)
344 printConstantValueOnly(cast<Constant>(constValues[i].get()));
345 }
346 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
347 { // Print the fields in successive locations. Pad to align if needed!
348 const StructLayout *cvsLayout =
349 TD->getStructLayout(CVS->getType());
350 const std::vector<Use>& constValues = CVS->getValues();
351 unsigned sizeSoFar = 0;
352 for (unsigned i=0, N = constValues.size(); i < N; i++)
353 {
354 const Constant* field = cast<Constant>(constValues[i].get());
355
356 // Check if padding is needed and insert one or more 0s.
357 unsigned fieldSize = TD->getTypeSize(field->getType());
358 int padSize = ((i == N-1? cvsLayout->StructSize
359 : cvsLayout->MemberOffsets[i+1])
360 - cvsLayout->MemberOffsets[i]) - fieldSize;
361 sizeSoFar += (fieldSize + padSize);
362
363 // Now print the actual field value
364 printConstantValueOnly(field, padSize);
365 }
366 assert(sizeSoFar == cvsLayout->StructSize &&
367 "Layout of constant struct may be incorrect!");
368 }
369 else
370 printSingleConstantValue(CV);
371
372 if (numPadBytesAfter) {
373 unsigned numBytes = numPadBytesAfter;
374 for ( ; numBytes >= 8; numBytes -= 8)
375 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
376 if (numBytes >= 4)
377 {
378 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
379 numBytes -= 4;
380 }
381 while (numBytes--)
382 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
383 }
384}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000385
Chris Lattnerb7089442003-01-13 00:35:03 +0000386// printConstantPool - Print out any constants which have been spilled to
387// memory...
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000388void Printer::printConstantPool(MachineConstantPool *MCP){
Chris Lattnerb7089442003-01-13 00:35:03 +0000389 const std::vector<Constant*> &CP = MCP->getConstants();
390 if (CP.empty()) return;
391
392 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
393 O << "\t.section .rodata\n";
Brian Gaeke5e001572003-06-26 18:02:30 +0000394 O << "\t.align " << (unsigned)TD->getTypeAlignment(CP[i]->getType())
395 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000396 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
397 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000398 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000399 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000400}
401
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000402/// runOnMachineFunction - This uses the X86InstructionInfo::print method
Brian Gaeke6559bb92002-11-14 22:32:30 +0000403/// to print assembly for each instruction.
Chris Lattner0285a332002-12-28 20:25:38 +0000404bool Printer::runOnMachineFunction(MachineFunction &MF) {
405 static unsigned BBNumber = 0;
406 const TargetMachine &TM = MF.getTarget();
Chris Lattner3501fea2003-01-14 22:00:31 +0000407 const TargetInstrInfo &TII = TM.getInstrInfo();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000408 TD = &TM.getTargetData();
Brian Gaeke6559bb92002-11-14 22:32:30 +0000409
Brian Gaeked7908f62003-06-27 00:00:48 +0000410 // What's my mangled name?
411 CurrentFnName = getValueName(MF.getFunction());
412
Chris Lattnerb7089442003-01-13 00:35:03 +0000413 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000414 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000415
Brian Gaeke6559bb92002-11-14 22:32:30 +0000416 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000417 O << "\t.text\n";
418 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000419 O << "\t.globl\t" << CurrentFnName << "\n";
420 O << "\t.type\t" << CurrentFnName << ", @function\n";
421 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000422
Brian Gaeked7908f62003-06-27 00:00:48 +0000423 // Number each basic block so that we can consistently refer to them
424 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000425 NumberForBB.clear();
426 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
427 I != E; ++I) {
428 NumberForBB[I->getBasicBlock()] = BBNumber++;
429 }
430
Brian Gaeke6559bb92002-11-14 22:32:30 +0000431 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000432 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
433 I != E; ++I) {
434 // Print a label for the basic block.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000435 O << ".BB" << NumberForBB[I->getBasicBlock()] << ":\t# "
436 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000437 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
438 II != E; ++II) {
439 // Print the assembly for the instruction.
440 O << "\t";
Brian Gaeked7908f62003-06-27 00:00:48 +0000441 printMachineInstruction(*II, O, TM);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000442 }
Chris Lattner0285a332002-12-28 20:25:38 +0000443 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000444
445 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000446 return false;
447}
448
Chris Lattner3d3067b2002-11-21 20:44:15 +0000449static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000450 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000451 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
452 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000453}
454
455static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000456 if (MI->getOperand(Op).isFrameIndex()) return true;
457 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000458 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000459 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
460 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000461}
462
Brian Gaeked7908f62003-06-27 00:00:48 +0000463void Printer::printOp(std::ostream &O, const MachineOperand &MO,
464 const MRegisterInfo &RI,
465 bool elideOffsetKeyword /* = false */) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000466 switch (MO.getType()) {
467 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000468 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000469 O << "<" << V->getName() << ">";
470 return;
471 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000472 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000473 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000474 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
475 O << RI.get(MO.getReg()).Name;
476 else
477 O << "%reg" << MO.getReg();
478 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000479
480 case MachineOperand::MO_SignExtendedImmed:
481 case MachineOperand::MO_UnextendedImmed:
482 O << (int)MO.getImmedValue();
483 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000484 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000485 {
486 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
487 assert (i != NumberForBB.end()
488 && "Could not find a BB I previously put in the NumberForBB map!");
489 O << ".BB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
490 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000491 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000492 case MachineOperand::MO_GlobalAddress:
Brian Gaekebc601fe2003-06-25 22:00:39 +0000493 if (!elideOffsetKeyword) O << "OFFSET "; O << getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000494 return;
495 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000496 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000497 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000498 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000499 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000500 }
501}
502
Chris Lattner3501fea2003-01-14 22:00:31 +0000503static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000504 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000505 default: assert(0 && "Unknown arg size!");
506 case X86II::Arg8: return "BYTE PTR";
507 case X86II::Arg16: return "WORD PTR";
508 case X86II::Arg32: return "DWORD PTR";
509 case X86II::Arg64: return "QWORD PTR";
510 case X86II::ArgF32: return "DWORD PTR";
511 case X86II::ArgF64: return "QWORD PTR";
512 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000513 }
514}
515
Brian Gaeked7908f62003-06-27 00:00:48 +0000516void Printer::printMemReference(std::ostream &O, const MachineInstr *MI,
517 unsigned Op,
518 const MRegisterInfo &RI) const {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000519 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000520
521 if (MI->getOperand(Op).isFrameIndex()) {
522 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
523 if (MI->getOperand(Op+3).getImmedValue())
524 O << " + " << MI->getOperand(Op+3).getImmedValue();
525 O << "]";
526 return;
527 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000528 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000529 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000530 if (MI->getOperand(Op+3).getImmedValue())
531 O << " + " << MI->getOperand(Op+3).getImmedValue();
532 O << "]";
533 return;
534 }
535
Chris Lattner3d3067b2002-11-21 20:44:15 +0000536 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000537 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000538 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000539 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000540
541 O << "[";
542 bool NeedPlus = false;
543 if (BaseReg.getReg()) {
544 printOp(O, BaseReg, RI);
545 NeedPlus = true;
546 }
547
548 if (IndexReg.getReg()) {
549 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000550 if (ScaleVal != 1)
551 O << ScaleVal << "*";
Chris Lattner3d3067b2002-11-21 20:44:15 +0000552 printOp(O, IndexReg, RI);
553 NeedPlus = true;
554 }
555
Chris Lattner0285a332002-12-28 20:25:38 +0000556 if (DispVal) {
557 if (NeedPlus)
558 if (DispVal > 0)
559 O << " + ";
560 else {
561 O << " - ";
562 DispVal = -DispVal;
563 }
564 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000565 }
566 O << "]";
567}
568
Brian Gaeked7908f62003-06-27 00:00:48 +0000569/// printMachineInstruction -- Print out an x86 instruction in intel syntax
570///
571void Printer::printMachineInstruction(const MachineInstr *MI, std::ostream &O,
572 const TargetMachine &TM) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000573 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000574 const TargetInstrInfo &TII = TM.getInstrInfo();
575 const TargetInstrDescriptor &Desc = TII.get(Opcode);
576 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000577
Chris Lattnereca1f632002-12-25 05:09:01 +0000578 switch (Desc.TSFlags & X86II::FormMask) {
579 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000580 // Print pseudo-instructions as comments; either they should have been
581 // turned into real instructions by now, or they don't need to be
582 // seen by the assembler (e.g., IMPLICIT_USEs.)
583 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000584 if (Opcode == X86::PHI) {
585 printOp(O, MI->getOperand(0), RI);
586 O << " = phi ";
587 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
588 if (i != 1) O << ", ";
589 O << "[";
590 printOp(O, MI->getOperand(i), RI);
591 O << ", ";
592 printOp(O, MI->getOperand(i+1), RI);
593 O << "]";
594 }
595 } else {
596 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000597 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
598 MI->getOperand(0).opIsDefAndUse())) {
Chris Lattnereca1f632002-12-25 05:09:01 +0000599 printOp(O, MI->getOperand(0), RI);
600 O << " = ";
601 ++i;
602 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000603 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000604
605 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
606 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000607 if (MI->getOperand(i).opIsDefOnly() ||
608 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000609 printOp(O, MI->getOperand(i), RI);
Vikram S. Adve49cab032003-05-27 00:03:17 +0000610 if (MI->getOperand(i).opIsDefOnly() ||
611 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000612 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000613 }
614 O << "\n";
615 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000616
Chris Lattnerf9f60882002-11-18 06:56:51 +0000617 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000618 // The accepted forms of Raw instructions are:
619 // 1. nop - No operand required
620 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000621 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000622 //
623 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000624 (MI->getNumOperands() == 1 &&
625 (MI->getOperand(0).isPCRelativeDisp() ||
626 MI->getOperand(0).isGlobalAddress() ||
627 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000628 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000629 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000630
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000631 if (MI->getNumOperands() == 1) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000632 printOp(O, MI->getOperand(0), RI, true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000633 }
634 O << "\n";
635 return;
636
Chris Lattner77875d82002-11-21 02:00:20 +0000637 case X86II::AddRegFrm: {
638 // There are currently two forms of acceptable AddRegFrm instructions.
639 // Either the instruction JUST takes a single register (like inc, dec, etc),
640 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000641 // (move immediate f.e.). Note that this immediate value might be stored as
642 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000643 // into a register. The initial register might be duplicated if this is a
644 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000645 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000646 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000647 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000648 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000649 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000650 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000651 MI->getOperand(1).isRegister() ||
652 MI->getOperand(1).isGlobalAddress() ||
653 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000654 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000655
Chris Lattner77875d82002-11-21 02:00:20 +0000656 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000657
Brian Gaeked7908f62003-06-27 00:00:48 +0000658 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattner77875d82002-11-21 02:00:20 +0000659 printOp(O, MI->getOperand(0), RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000660 if (MI->getNumOperands() == 2 &&
661 (!MI->getOperand(1).isRegister() ||
662 MI->getOperand(1).getVRegValueOrNull() ||
663 MI->getOperand(1).isGlobalAddress() ||
664 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000665 O << ", ";
Chris Lattner675dd2c2002-11-21 17:09:01 +0000666 printOp(O, MI->getOperand(1), RI);
Chris Lattner77875d82002-11-21 02:00:20 +0000667 }
668 O << "\n";
669 return;
670 }
Chris Lattner233ad712002-11-21 01:33:44 +0000671 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000672 // There are two acceptable forms of MRMDestReg instructions, those with 2,
673 // 3 and 4 operands:
674 //
675 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000676 //
677 // 3 Operands: in this form, the first two registers (the destination, and
678 // the first operand) should be the same, post register allocation. The 3rd
679 // operand is an additional input. This should be for things like add
680 // instructions.
681 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000682 // 4 Operands: This form is for instructions which are 3 operands forms, but
683 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000684 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000685 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000686 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000687 (MI->getNumOperands() == 2 ||
688 (isTwoAddr && MI->getOperand(1).isRegister() &&
689 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
690 (MI->getNumOperands() == 3 ||
691 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000692 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000693
Brian Gaeked7908f62003-06-27 00:00:48 +0000694 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000695 printOp(O, MI->getOperand(0), RI);
696 O << ", ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000697 printOp(O, MI->getOperand(1+isTwoAddr), RI);
698 if (MI->getNumOperands() == 4) {
699 O << ", ";
700 printOp(O, MI->getOperand(3), RI);
701 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000702 O << "\n";
703 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000704 }
Chris Lattner18042332002-11-21 21:03:39 +0000705
706 case X86II::MRMDestMem: {
707 // These instructions are the same as MRMDestReg, but instead of having a
708 // register reference for the mod/rm field, it's a memory reference.
709 //
710 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000711 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000712
Brian Gaeked7908f62003-06-27 00:00:48 +0000713 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Chris Lattner18042332002-11-21 21:03:39 +0000714 printMemReference(O, MI, 0, RI);
715 O << ", ";
716 printOp(O, MI->getOperand(4), RI);
717 O << "\n";
718 return;
719 }
720
Chris Lattner233ad712002-11-21 01:33:44 +0000721 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000722 // There is a two forms that are acceptable for MRMSrcReg instructions,
723 // those with 3 and 2 operands:
724 //
725 // 3 Operands: in this form, the last register (the second input) is the
726 // ModR/M input. The first two operands should be the same, post register
727 // allocation. This is for things like: add r32, r/m32
728 //
729 // 2 Operands: this is for things like mov that do not read a second input
730 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000731 assert(MI->getOperand(0).isRegister() &&
732 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000733 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000734 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000735 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000736 if (MI->getNumOperands() == 3 &&
737 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
738 O << "**";
739
Brian Gaeked7908f62003-06-27 00:00:48 +0000740 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattner644e1ab2002-11-21 00:30:01 +0000741 printOp(O, MI->getOperand(0), RI);
742 O << ", ";
743 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
744 O << "\n";
745 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000746 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000747
Chris Lattner3d3067b2002-11-21 20:44:15 +0000748 case X86II::MRMSrcMem: {
749 // These instructions are the same as MRMSrcReg, but instead of having a
750 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000751 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000752 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000753 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000754 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000755 isMem(MI, 2))
756 && "Bad format for MRMDestReg!");
757 if (MI->getNumOperands() == 2+4 &&
758 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
759 O << "**";
760
Brian Gaeked7908f62003-06-27 00:00:48 +0000761 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattner3d3067b2002-11-21 20:44:15 +0000762 printOp(O, MI->getOperand(0), RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000763 O << ", " << sizePtr(Desc) << " ";
Chris Lattner3d3067b2002-11-21 20:44:15 +0000764 printMemReference(O, MI, MI->getNumOperands()-4, RI);
765 O << "\n";
766 return;
767 }
768
Chris Lattner675dd2c2002-11-21 17:09:01 +0000769 case X86II::MRMS0r: case X86II::MRMS1r:
770 case X86II::MRMS2r: case X86II::MRMS3r:
771 case X86II::MRMS4r: case X86II::MRMS5r:
772 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000773 // In this form, the following are valid formats:
774 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000775 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000776 // 2. shl rdest, rinput <implicit CL or 1>
777 // 3. sbb rdest, rinput, immediate [rdest = rinput]
778 //
779 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000780 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000781 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000782 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000783 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000784 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000785 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000786 "Bad MRMSxR format!");
787
Chris Lattnerd9096832002-12-15 08:01:39 +0000788 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000789 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
790 O << "**";
791
Brian Gaeked7908f62003-06-27 00:00:48 +0000792 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattner675dd2c2002-11-21 17:09:01 +0000793 printOp(O, MI->getOperand(0), RI);
Chris Lattnerd9096832002-12-15 08:01:39 +0000794 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000795 O << ", ";
Chris Lattner1d53ce42002-11-21 23:30:00 +0000796 printOp(O, MI->getOperand(MI->getNumOperands()-1), RI);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000797 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000798 if (Desc.TSFlags & X86II::PrintImplUses) {
799 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
800 O << ", " << RI.get(*p).Name;
801 }
802 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000803 O << "\n";
804
805 return;
806 }
807
Chris Lattnerb7089442003-01-13 00:35:03 +0000808 case X86II::MRMS0m: case X86II::MRMS1m:
809 case X86II::MRMS2m: case X86II::MRMS3m:
810 case X86II::MRMS4m: case X86II::MRMS5m:
811 case X86II::MRMS6m: case X86II::MRMS7m: {
812 // In this form, the following are valid formats:
813 // 1. sete [m]
814 // 2. cmp [m], immediate
815 // 2. shl [m], rinput <implicit CL or 1>
816 // 3. sbb [m], immediate
817 //
818 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
819 isMem(MI, 0) && "Bad MRMSxM format!");
820 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
821 "Bad MRMSxM format!");
Brian Gaekeb44210d2003-07-07 18:34:20 +0000822 // Work around GNU assembler bugs in FSTP and FLD.
823 if (MI->getOpCode() == X86::FSTPr80) {
824 if ((MI->getOperand(0).getReg() == X86::ESP)
825 && (MI->getOperand(1).getImmedValue() == 1)) {
826 int DispVal = MI->getOperand(3).getImmedValue();
827 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
828 unsigned int val = (unsigned int) DispVal;
829 O << ".byte 0xdb, 0xbc, 0x24\n\t";
830 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
831 } else { // 1 byte disp.
832 unsigned char val = (unsigned char) DispVal;
833 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
834 << std::dec << "\t# ";
835 }
836 }
837 } else if (MI->getOpCode() == X86::FLDr80) {
838 if ((MI->getOperand(0).getReg() == X86::ESP)
839 && (MI->getOperand(1).getImmedValue() == 1)) {
840 int DispVal = MI->getOperand(3).getImmedValue();
841 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
842 unsigned int val = (unsigned int) DispVal;
843 O << ".byte 0xdb, 0xac, 0x24\n\t";
844 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
845 } else { // 1 byte disp.
846 unsigned char val = (unsigned char) DispVal;
847 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
848 << std::dec << "\t# ";
849 }
850 }
851 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000852 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000853 O << sizePtr(Desc) << " ";
854 printMemReference(O, MI, 0, RI);
855 if (MI->getNumOperands() == 5) {
856 O << ", ";
857 printOp(O, MI->getOperand(4), RI);
858 }
859 O << "\n";
860 return;
861 }
862
Chris Lattnerf9f60882002-11-18 06:56:51 +0000863 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000864 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000865 }
Chris Lattner72614082002-10-25 22:55:53 +0000866}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000867
868bool Printer::doInitialization(Module &M)
869{
870 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
871 // with no % decorations on register names.
872 O << "\t.intel_syntax noprefix\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000873
874 // Ripped from CWriter:
875 // Calculate which global values have names that will collide when we throw
876 // away type information.
877 { // Scope to delete the FoundNames set when we are done with it...
878 std::set<std::string> FoundNames;
879 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
880 if (I->hasName()) // If the global has a name...
881 if (FoundNames.count(I->getName())) // And the name is already used
882 MangledGlobals.insert(I); // Mangle the name
883 else
884 FoundNames.insert(I->getName()); // Otherwise, keep track of name
885
886 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
887 if (I->hasName()) // If the global has a name...
888 if (FoundNames.count(I->getName())) // And the name is already used
889 MangledGlobals.insert(I); // Mangle the name
890 else
891 FoundNames.insert(I->getName()); // Otherwise, keep track of name
892 }
893
Brian Gaeke9e474c42003-06-19 19:32:32 +0000894 return false; // success
895}
896
897bool Printer::doFinalization(Module &M)
898{
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000899 // Print out module-level global variables here.
900 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000901 std::string name(getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000902 if (I->hasInitializer()) {
903 Constant *C = I->getInitializer();
904 O << "\t.data\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000905 O << "\t.globl " << name << "\n";
906 O << "\t.type " << name << ",@object\n";
907 O << "\t.size " << name << ","
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000908 << (unsigned)TD->getTypeSize(I->getType()) << "\n";
909 O << "\t.align " << (unsigned)TD->getTypeAlignment(C->getType()) << "\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000910 O << name << ":\t\t\t\t\t#" << *C << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000911 printConstantValueOnly (C);
912 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000913 O << "\t.globl " << name << "\n";
914 O << "\t.comm " << name << ", "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000915 << (unsigned)TD->getTypeSize(I->getType()) << ", "
916 << (unsigned)TD->getTypeAlignment(I->getType()) << "\n";
917 }
918 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000919 MangledGlobals.clear();
Brian Gaeke9e474c42003-06-19 19:32:32 +0000920 return false; // success
921}
Brian Gaeked7908f62003-06-27 00:00:48 +0000922
923