1 //===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
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 Intel format assembly language.
12 // This printer is the output mechanism used by `llc'.
14 //===----------------------------------------------------------------------===//
16 #include "X86IntelAsmPrinter.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Module.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/Support/Mangler.h"
22 #include "llvm/Target/TargetOptions.h"
25 X86IntelAsmPrinter::X86IntelAsmPrinter(std::ostream &O, X86TargetMachine &TM)
26 : X86SharedAsmPrinter(O, TM) {
29 /// runOnMachineFunction - This uses the printMachineInstruction()
30 /// method to print assembly for each instruction.
32 bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
34 // Let PassManager know we need debug information and relay
35 // the MachineDebugInfo address on to DwarfWriter.
36 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
39 SetupMachineFunction(MF);
42 // Print out constants referenced by the function
43 EmitConstantPool(MF.getConstantPool());
45 // Print out labels for the function.
46 SwitchSection(".code", MF.getFunction());
48 if (MF.getFunction()->getLinkage() == GlobalValue::ExternalLinkage)
49 O << "\tpublic " << CurrentFnName << "\n";
50 O << CurrentFnName << "\tproc near\n";
53 // Emit pre-function debug information.
54 DW.BeginFunction(&MF);
57 // Print out code for the function.
58 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
60 // Print a label for the basic block if there are any predecessors.
61 if (I->pred_begin() != I->pred_end()) {
62 printBasicBlockLabel(I, true);
65 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
67 // Print the assembly for the instruction.
69 printMachineInstruction(II);
74 // Emit post-function debug information.
78 O << CurrentFnName << "\tendp\n";
80 // We didn't modify anything.
84 void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
85 unsigned char value = MI->getOperand(Op).getImmedValue();
86 assert(value <= 7 && "Invalid ssecc argument!");
88 case 0: O << "eq"; break;
89 case 1: O << "lt"; break;
90 case 2: O << "le"; break;
91 case 3: O << "unord"; break;
92 case 4: O << "neq"; break;
93 case 5: O << "nlt"; break;
94 case 6: O << "nle"; break;
95 case 7: O << "ord"; break;
99 void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
100 const char *Modifier) {
101 const MRegisterInfo &RI = *TM.getRegisterInfo();
102 switch (MO.getType()) {
103 case MachineOperand::MO_VirtualRegister:
104 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
105 O << RI.get(MO.getReg()).Name;
107 O << "reg" << MO.getReg();
110 case MachineOperand::MO_SignExtendedImmed:
111 case MachineOperand::MO_UnextendedImmed:
112 O << (int)MO.getImmedValue();
114 case MachineOperand::MO_MachineBasicBlock:
115 printBasicBlockLabel(MO.getMachineBasicBlock());
117 case MachineOperand::MO_ConstantPoolIndex: {
118 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
119 if (!isMemOp) O << "OFFSET ";
120 O << "[" << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
121 << MO.getConstantPoolIndex();
122 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
123 O << "-\"L" << getFunctionNumber() << "$pb\"";
124 int Offset = MO.getOffset();
126 O << " + " << Offset;
132 case MachineOperand::MO_GlobalAddress: {
133 bool isCallOp = Modifier && !strcmp(Modifier, "call");
134 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
135 if (!isMemOp && !isCallOp) O << "OFFSET ";
136 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
137 GlobalValue *GV = MO.getGlobal();
138 std::string Name = Mang->getValueName(GV);
139 if (!isMemOp && !isCallOp) O << '$';
140 // Link-once, External, or Weakly-linked global variables need
141 // non-lazily-resolved stubs
142 if (GV->isExternal() || GV->hasWeakLinkage() ||
143 GV->hasLinkOnceLinkage()) {
144 // Dynamically-resolved functions need a stub for the function.
145 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
146 FnStubs.insert(Name);
147 O << "L" << Name << "$stub";
149 GVStubs.insert(Name);
150 O << "L" << Name << "$non_lazy_ptr";
153 O << Mang->getValueName(GV);
155 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
156 O << "-\"L" << getFunctionNumber() << "$pb\"";
158 O << Mang->getValueName(MO.getGlobal());
159 int Offset = MO.getOffset();
161 O << " + " << Offset;
166 case MachineOperand::MO_ExternalSymbol: {
167 bool isCallOp = Modifier && !strcmp(Modifier, "call");
168 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
169 std::string Name(GlobalPrefix);
170 Name += MO.getSymbolName();
171 FnStubs.insert(Name);
172 O << "L" << Name << "$stub";
175 if (!isCallOp) O << "OFFSET ";
176 O << GlobalPrefix << MO.getSymbolName();
180 O << "<unknown operand type>"; return;
184 void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
185 assert(isMem(MI, Op) && "Invalid memory reference!");
187 const MachineOperand &BaseReg = MI->getOperand(Op);
188 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
189 const MachineOperand &IndexReg = MI->getOperand(Op+2);
190 const MachineOperand &DispSpec = MI->getOperand(Op+3);
192 if (BaseReg.isFrameIndex()) {
193 O << "[frame slot #" << BaseReg.getFrameIndex();
194 if (DispSpec.getImmedValue())
195 O << " + " << DispSpec.getImmedValue();
201 bool NeedPlus = false;
202 if (BaseReg.getReg()) {
203 printOp(BaseReg, "mem");
207 if (IndexReg.getReg()) {
208 if (NeedPlus) O << " + ";
210 O << ScaleVal << "*";
215 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
218 printOp(DispSpec, "mem");
220 int DispVal = DispSpec.getImmedValue();
221 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
235 void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
236 O << "\"L" << getFunctionNumber() << "$pb\"\n";
237 O << "\"L" << getFunctionNumber() << "$pb\":";
240 bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
242 const MRegisterInfo &RI = *TM.getRegisterInfo();
243 unsigned Reg = MO.getReg();
244 const char *Name = RI.get(Reg).Name;
246 default: return true; // Unknown mode.
247 case 'b': // Print QImode register
249 default: return true;
250 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
253 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
256 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
259 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
276 case 'h': // Print QImode high register
278 default: return true;
279 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
282 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
285 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
288 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
293 case 'w': // Print HImode register
295 default: return true;
296 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
299 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
302 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
305 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
322 case 'k': // Print SImode register
324 default: return true;
325 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
328 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
331 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
334 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
357 /// PrintAsmOperand - Print out an operand for an inline asm expression.
359 bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
361 const char *ExtraCode) {
362 // Does this asm operand have a single letter operand modifier?
363 if (ExtraCode && ExtraCode[0]) {
364 if (ExtraCode[1] != 0) return true; // Unknown modifier.
366 switch (ExtraCode[0]) {
367 default: return true; // Unknown modifier.
368 case 'b': // Print QImode register
369 case 'h': // Print QImode high register
370 case 'w': // Print HImode register
371 case 'k': // Print SImode register
372 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
376 printOperand(MI, OpNo);
380 bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
383 const char *ExtraCode) {
384 if (ExtraCode && ExtraCode[0])
385 return true; // Unknown modifier.
386 printMemReference(MI, OpNo);
390 /// printMachineInstruction -- Print out a single X86 LLVM instruction
391 /// MI in Intel syntax to the current output stream.
393 void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
396 // Call the autogenerated instruction printer routines.
397 printInstruction(MI);
400 bool X86IntelAsmPrinter::doInitialization(Module &M) {
401 X86SharedAsmPrinter::doInitialization(M);
404 PrivateGlobalPrefix = "$";
405 AlignDirective = "\talign\t";
407 ZeroDirective = "\tdb\t";
408 ZeroDirectiveSuffix = " dup(0)";
409 AsciiDirective = "\tdb\t";
411 Data8bitsDirective = "\t.db\t";
412 Data16bitsDirective = "\t.dw\t";
413 Data32bitsDirective = "\t.dd\t";
414 Data64bitsDirective = "\t.dq\t";
415 HasDotTypeDotSizeDirective = false;
416 Mang->markCharUnacceptable('.');
418 O << "\t.686\n\t.model flat\n\n";
420 // Emit declarations for external functions.
421 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
423 O << "\textern " << Mang->getValueName(I) << ":near\n";
425 // Emit declarations for external globals.
426 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
429 O << "\textern " << Mang->getValueName(I) << ":byte\n";
430 else if (I->getLinkage() == GlobalValue::ExternalLinkage)
431 O << "\tpublic " << Mang->getValueName(I) << "\n";
437 bool X86IntelAsmPrinter::doFinalization(Module &M) {
438 X86SharedAsmPrinter::doFinalization(M);
439 SwitchSection("", 0);
444 void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
445 unsigned NumElts = CVA->getNumOperands();
447 // ML does not have escape sequences except '' for '. It also has a maximum
448 // string length of 255.
450 bool inString = false;
451 for (unsigned i = 0; i < NumElts; i++) {
452 int n = cast<ConstantInt>(CVA->getOperand(i))->getRawValue() & 255;
456 if (n >= 32 && n <= 127) {
483 len += 1 + (n > 9) + (n > 99);
504 // Include the auto-generated portion of the assembly writer.
505 #include "X86GenAsmWriter1.inc"