blob: bf333b759afa59622d0c2b206f8e78920a5b65aa [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
Tony Linthicumb4b54152011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to Hexagon assembly language. This printer is
12// the output mechanism used by `llc'.
13//
Tony Linthicumb4b54152011-12-12 21:14:40 +000014//===----------------------------------------------------------------------===//
15
16
17#define DEBUG_TYPE "asm-printer"
18#include "Hexagon.h"
19#include "HexagonTargetMachine.h"
20#include "HexagonSubtarget.h"
21#include "HexagonMachineFunctionInfo.h"
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Module.h"
25#include "llvm/Assembly/Writer.h"
26#include "llvm/CodeGen/AsmPrinter.h"
27#include "llvm/CodeGen/MachineModuleInfo.h"
28#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/MC/MCStreamer.h"
32#include "llvm/MC/MCAsmInfo.h"
33#include "llvm/MC/MCSymbol.h"
34#include "llvm/Support/MathExtras.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/Compiler.h"
38#include "llvm/Support/raw_ostream.h"
Craig Topper79aa3412012-03-17 18:46:09 +000039#include "llvm/Support/TargetRegistry.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000040#include "llvm/Target/Mangler.h"
41#include "llvm/Target/TargetData.h"
42#include "llvm/Target/TargetLoweringObjectFile.h"
43#include "llvm/Target/TargetRegisterInfo.h"
44#include "llvm/Target/TargetInstrInfo.h"
45#include "llvm/Target/TargetOptions.h"
46#include "llvm/ADT/SmallPtrSet.h"
47#include "llvm/ADT/SmallString.h"
48#include "llvm/ADT/StringExtras.h"
Tony Linthicumb4b54152011-12-12 21:14:40 +000049
50using namespace llvm;
51
52static cl::opt<bool> AlignCalls(
53 "hexagon-align-calls", cl::Hidden, cl::init(true),
54 cl::desc("Insert falign after call instruction for Hexagon target"));
55
56
57namespace {
58 class HexagonAsmPrinter : public AsmPrinter {
59 const HexagonSubtarget *Subtarget;
60
61 public:
62 explicit HexagonAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
63 : AsmPrinter(TM, Streamer) {
64 Subtarget = &TM.getSubtarget<HexagonSubtarget>();
65 }
66
67 virtual const char *getPassName() const {
68 return "Hexagon Assembly Printer";
69 }
70
71 /// printInstruction - This method is automatically generated by tablegen
72 /// from the instruction set description. This method returns true if the
73 /// machine instruction was sufficiently described to print it, otherwise it
74 void printInstruction(const MachineInstr *MI, raw_ostream &O);
75 virtual void EmitInstruction(const MachineInstr *MI);
76
77 void printOp(const MachineOperand &MO, raw_ostream &O);
78
79 /// printRegister - Print register according to target requirements.
80 ///
81 void printRegister(const MachineOperand &MO, bool R0AsZero,
82 raw_ostream &O) {
83 unsigned RegNo = MO.getReg();
84 assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
85 O << getRegisterName(RegNo);
86 }
87
88 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS) {
89 const MachineOperand &MO = MI->getOperand(OpNo);
90 if (MO.isReg()) {
91 printRegister(MO, false, OS);
92 } else if (MO.isImm()) {
93 OS << MO.getImm();
94 } else {
95 printOp(MO, OS);
96 }
97 }
98
99
100 bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const;
101
102 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
103 unsigned AsmVariant, const char *ExtraCode,
104 raw_ostream &OS);
105 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
106 unsigned AsmVariant, const char *ExtraCode,
107 raw_ostream &OS);
108
109
110 void printHexagonImmOperand(const MachineInstr *MI, unsigned OpNo,
111 raw_ostream &O) {
112 int value = MI->getOperand(OpNo).getImm();
113 O << value;
114 }
115
116
117 void printHexagonNegImmOperand(const MachineInstr *MI, unsigned OpNo,
118 raw_ostream &O) {
119 int value = MI->getOperand(OpNo).getImm();
120 O << -value;
121 }
122
Sirish Pandeab7955b2012-02-15 18:52:27 +0000123 void printHexagonNOneImmOperand(const MachineInstr *MI, unsigned OpNo,
124 raw_ostream &O) const {
125 O << -1;
126 }
127
Tony Linthicumb4b54152011-12-12 21:14:40 +0000128 void printHexagonMEMriOperand(const MachineInstr *MI, unsigned OpNo,
129 raw_ostream &O) {
130 const MachineOperand &MO1 = MI->getOperand(OpNo);
131 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
132
133 O << getRegisterName(MO1.getReg())
134 << " + #"
135 << (int) MO2.getImm();
136 }
137
138
139 void printHexagonFrameIndexOperand(const MachineInstr *MI, unsigned OpNo,
140 raw_ostream &O) {
141 const MachineOperand &MO1 = MI->getOperand(OpNo);
142 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
143
144 O << getRegisterName(MO1.getReg())
145 << ", #"
146 << MO2.getImm();
147 }
148
149 void printBranchOperand(const MachineInstr *MI, unsigned OpNo,
150 raw_ostream &O) {
151 // Branches can take an immediate operand. This is used by the branch
152 // selection pass to print $+8, an eight byte displacement from the PC.
153 if (MI->getOperand(OpNo).isImm()) {
154 O << "$+" << MI->getOperand(OpNo).getImm()*4;
155 } else {
156 printOp(MI->getOperand(OpNo), O);
157 }
158 }
159
160 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
161 raw_ostream &O) {
162 }
163
164 void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
165 raw_ostream &O) {
166 }
167
168
169 void printSymbolHi(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
170 O << "#HI(";
171 if (MI->getOperand(OpNo).isImm()) {
172 printHexagonImmOperand(MI, OpNo, O);
173 } else {
174 printOp(MI->getOperand(OpNo), O);
175 }
176 O << ")";
177 }
178
179 void printSymbolLo(const MachineInstr *MI, unsigned OpNo, raw_ostream &O) {
180 O << "#HI(";
181 if (MI->getOperand(OpNo).isImm()) {
182 printHexagonImmOperand(MI, OpNo, O);
183 } else {
184 printOp(MI->getOperand(OpNo), O);
185 }
186 O << ")";
187 }
188
189 void printPredicateOperand(const MachineInstr *MI, unsigned OpNo,
190 raw_ostream &O);
191
192 void printAddrModeBasePlusOffset(const MachineInstr *MI, int OpNo,
193 raw_ostream &O);
194
195 void printGlobalOperand(const MachineInstr *MI, int OpNo, raw_ostream &O);
196 void printJumpTable(const MachineInstr *MI, int OpNo, raw_ostream &O);
197
198 void EmitAlignment(unsigned NumBits, const GlobalValue *GV = 0) const;
199
200 static const char *getRegisterName(unsigned RegNo);
201 };
202
203} // end of anonymous namespace
204
205// Include the auto-generated portion of the assembly writer.
206#include "HexagonGenAsmWriter.inc"
207
208
209void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
210 const GlobalValue *GV) const {
211
212 // For basic block level alignment, use falign.
213 if (!GV) {
214 OutStreamer.EmitRawText(StringRef("\t.falign"));
215 return;
216 }
217
218 AsmPrinter::EmitAlignment(NumBits, GV);
219}
220
221void HexagonAsmPrinter::printOp(const MachineOperand &MO, raw_ostream &O) {
222 switch (MO.getType()) {
223 case MachineOperand::MO_Immediate:
224 dbgs() << "printOp() does not handle immediate values\n";
225 abort();
Tony Linthicumb4b54152011-12-12 21:14:40 +0000226
227 case MachineOperand::MO_MachineBasicBlock:
228 O << *MO.getMBB()->getSymbol();
229 return;
230 case MachineOperand::MO_JumpTableIndex:
231 O << *GetJTISymbol(MO.getIndex());
232 // FIXME: PIC relocation model.
233 return;
234 case MachineOperand::MO_ConstantPoolIndex:
235 O << *GetCPISymbol(MO.getIndex());
236 return;
237 case MachineOperand::MO_ExternalSymbol:
238 O << *GetExternalSymbolSymbol(MO.getSymbolName());
239 return;
240 case MachineOperand::MO_GlobalAddress: {
241 // Computing the address of a global symbol, not calling it.
242 O << *Mang->getSymbol(MO.getGlobal());
243 printOffset(MO.getOffset(), O);
244 return;
245 }
246
247 default:
248 O << "<unknown operand type: " << MO.getType() << ">";
249 return;
250 }
251}
252
253
254//
255// isBlockOnlyReachableByFallthrough - We need to override this since the
256// default AsmPrinter does not print labels for any basic block that
257// is only reachable by a fall through. That works for all cases except
258// for the case in which the basic block is reachable by a fall through but
259// through an indirect from a jump table. In this case, the jump table
260// will contain a label not defined by AsmPrinter.
261//
262bool HexagonAsmPrinter::
263isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
264 if (MBB->hasAddressTaken()) {
265 return false;
266 }
267 return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
268}
269
270
271/// PrintAsmOperand - Print out an operand for an inline asm expression.
272///
273bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
274 unsigned AsmVariant,
275 const char *ExtraCode,
276 raw_ostream &OS) {
277 // Does this asm operand have a single letter operand modifier?
278 if (ExtraCode && ExtraCode[0]) {
279 if (ExtraCode[1] != 0) return true; // Unknown modifier.
280
281 switch (ExtraCode[0]) {
282 default: return true; // Unknown modifier.
283 case 'c': // Don't print "$" before a global var name or constant.
284 // Hexagon never has a prefix.
285 printOperand(MI, OpNo, OS);
286 return false;
287 case 'L': // Write second word of DImode reference.
288 // Verify that this operand has two consecutive registers.
289 if (!MI->getOperand(OpNo).isReg() ||
290 OpNo+1 == MI->getNumOperands() ||
291 !MI->getOperand(OpNo+1).isReg())
292 return true;
293 ++OpNo; // Return the high-part.
294 break;
295 case 'I':
296 // Write 'i' if an integer constant, otherwise nothing. Used to print
297 // addi vs add, etc.
298 if (MI->getOperand(OpNo).isImm())
299 OS << "i";
300 return false;
301 }
302 }
303
304 printOperand(MI, OpNo, OS);
305 return false;
306}
307
308bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
309 unsigned OpNo, unsigned AsmVariant,
310 const char *ExtraCode,
311 raw_ostream &O) {
312 if (ExtraCode && ExtraCode[0])
313 return true; // Unknown modifier.
314
315 const MachineOperand &Base = MI->getOperand(OpNo);
316 const MachineOperand &Offset = MI->getOperand(OpNo+1);
317
318 if (Base.isReg())
319 printOperand(MI, OpNo, O);
320 else
Craig Topperbc219812012-02-07 02:50:20 +0000321 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000322
323 if (Offset.isImm()) {
324 if (Offset.getImm())
325 O << " + #" << Offset.getImm();
326 }
327 else
Craig Topperbc219812012-02-07 02:50:20 +0000328 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000329
330 return false;
331}
332
333void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
334 unsigned OpNo,
335 raw_ostream &O) {
Craig Topperbc219812012-02-07 02:50:20 +0000336 llvm_unreachable("Unimplemented");
Tony Linthicumb4b54152011-12-12 21:14:40 +0000337}
338
339
340/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
341/// the current output stream.
342///
343void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
344 SmallString<128> Str;
345 raw_svector_ostream O(Str);
346
347 const MachineFunction* MF = MI->getParent()->getParent();
348 const HexagonMachineFunctionInfo* MFI =
349 (const HexagonMachineFunctionInfo*)
350 MF->getInfo<HexagonMachineFunctionInfo>();
351
352
353
354 // Print a brace for the beginning of the packet.
355 if (MFI->isStartPacket(MI)) {
356 O << "\t{" << '\n';
357 }
358
359 DEBUG( O << "// MI = " << *MI << '\n';);
360
361 // Indent
362 O << "\t";
363
364
365 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
366 if (MFI->isEndPacket(MI) && MFI->isStartPacket(MI)) {
367 O << "\t{ nop }";
368 } else {
369 O << "}";
370 }
371 printInstruction(MI, O);
Tony Linthicumb4b54152011-12-12 21:14:40 +0000372 } else if (MI->getOpcode() == Hexagon::MPYI_rin) {
373 // Handle multipy with -ve constant on Hexagon:
374 // "$dst =- mpyi($src1, #$src2)"
375 printOperand(MI, 0, O);
376 O << " =- mpyi(";
377 printOperand(MI, 1, O);
378 O << ", #";
379 printHexagonNegImmOperand(MI, 2, O);
380 O << ")";
381 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_indexed_MEM_V4) {
382 //
383 // Handle memw(Rs+u6:2) [+-]= #U5
384 //
385 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
386 int addend = MI->getOperand(2).getImm();
387 if (addend < 0)
388 O << "-= " << "#" << -addend << '\n';
389 else
390 O << "+= " << "#" << addend << '\n';
391 } else if (MI->getOpcode() == Hexagon::MEMw_ADDSUBi_MEM_V4) {
392 //
393 // Handle memw(Rs+u6:2) [+-]= #U5
394 //
395 O << "\tmemw("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
396 int addend = MI->getOperand(2).getImm();
397 if (addend < 0)
398 O << "-= " << "#" << -addend << '\n';
399 else
400 O << "+= " << "#" << addend << '\n';
401 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_indexed_MEM_V4) {
402 //
403 // Handle memh(Rs+u6:1) [+-]= #U5
404 //
405 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
406 int addend = MI->getOperand(2).getImm();
407 if (addend < 0)
408 O << "-= " << "#" << -addend << '\n';
409 else
410 O << "+= " << "#" << addend << '\n';
411 } else if (MI->getOpcode() == Hexagon::MEMh_ADDSUBi_MEM_V4) {
412 //
413 // Handle memh(Rs+u6:1) [+-]= #U5
414 //
415 O << "\tmemh("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
416 int addend = MI->getOperand(2).getImm();
417 if (addend < 0)
418 O << "-= " << "#" << -addend << '\n';
419 else
420 O << "+= " << "#" << addend << '\n';
421 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_indexed_MEM_V4) {
422 //
423 // Handle memb(Rs+u6:1) [+-]= #U5
424 //
425 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
426 int addend = MI->getOperand(2).getImm();
427 if (addend < 0)
428 O << "-= " << "#" << -addend << '\n';
429 else
430 O << "+= " << "#" << addend << '\n';
431 } else if (MI->getOpcode() == Hexagon::MEMb_ADDSUBi_MEM_V4) {
432 //
433 // Handle memb(Rs+u6:1) [+-]= #U5
434 //
435 O << "\tmemb("; printHexagonMEMriOperand(MI, 0, O); O << ") ";
436 int addend = MI->getOperand(2).getImm();
437 if (addend < 0)
438 O << "-= " << "#" << -addend << '\n';
439 else
440 O << "+= " << "#" << addend << '\n';
441 } else if (MI->getOpcode() == Hexagon::CMPbGTri_V4) {
442 //
443 // Handle Pd=cmpb.gt(Rs,#s8)
444 //
445 O << "\t";
446 printRegister(MI->getOperand(0), false, O);
447 O << " = cmpb.gt(";
448 printRegister(MI->getOperand(1), false, O);
449 O << ", ";
450 int val = MI->getOperand(2).getImm() >> 24;
451 O << "#" << val << ")" << '\n';
452 } else if (MI->getOpcode() == Hexagon::CMPhEQri_V4) {
453 //
454 // Handle Pd=cmph.eq(Rs,#8)
455 //
456 O << "\t";
457 printRegister(MI->getOperand(0), false, O);
458 O << " = cmph.eq(";
459 printRegister(MI->getOperand(1), false, O);
460 O << ", ";
461 int val = MI->getOperand(2).getImm();
462 assert((((0 <= val) && (val <= 127)) ||
463 ((65408 <= val) && (val <= 65535))) &&
464 "Not in correct range!");
465 if (val >= 65408) val -= 65536;
466 O << "#" << val << ")" << '\n';
467 } else if (MI->getOpcode() == Hexagon::CMPhGTri_V4) {
468 //
469 // Handle Pd=cmph.gt(Rs,#8)
470 //
471 O << "\t";
472 printRegister(MI->getOperand(0), false, O);
473 O << " = cmph.gt(";
474 printRegister(MI->getOperand(1), false, O);
475 O << ", ";
476 int val = MI->getOperand(2).getImm() >> 16;
477 O << "#" << val << ")" << '\n';
478 } else {
479 printInstruction(MI, O);
480 }
481
482 // Print a brace for the end of the packet.
483 if (MFI->isEndPacket(MI) && MI->getOpcode() != Hexagon::ENDLOOP0) {
484 O << "\n\t}" << '\n';
485 }
486
487 if (AlignCalls && MI->getDesc().isCall()) {
488 O << "\n\t.falign" << "\n";
489 }
490
491 OutStreamer.EmitRawText(O.str());
492 return;
493}
494
495/// PrintUnmangledNameSafely - Print out the printable characters in the name.
496/// Don't print things like \n or \0.
497// static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
498// for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
499// Name != E; ++Name)
500// if (isprint(*Name))
501// OS << *Name;
502// }
503
504
505void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
506 int OpNo, raw_ostream &O) {
507 const MachineOperand &MO1 = MI->getOperand(OpNo);
508 const MachineOperand &MO2 = MI->getOperand(OpNo+1);
509
510 O << getRegisterName(MO1.getReg())
511 << " + #"
512 << MO2.getImm();
513}
514
515
516void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
517 raw_ostream &O) {
518 const MachineOperand &MO = MI->getOperand(OpNo);
519 assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
520 "Expecting global address");
521
522 O << *Mang->getSymbol(MO.getGlobal());
523 if (MO.getOffset() != 0) {
524 O << " + ";
525 O << MO.getOffset();
526 }
527}
528
529void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
530 raw_ostream &O) {
531 const MachineOperand &MO = MI->getOperand(OpNo);
532 assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
533 "Expecting jump table index");
534
535 // Hexagon_TODO: Do we need name mangling?
536 O << *GetJTISymbol(MO.getIndex());
537}
538
539extern "C" void LLVMInitializeHexagonAsmPrinter() {
540 RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
541}