blob: 90dc6b914853bb715ba4e3e2f89b4ebedb6f6ad9 [file] [log] [blame]
Nick Lewyckyf7a3c502010-09-07 18:14:24 +00001//===-- PTXAsmPrinter.cpp - PTX LLVM assembly writer ----------------------===//
2//
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 PTX assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PTX.h"
16#include "PTXTargetMachine.h"
Eric Christopher50880d02010-09-18 18:52:28 +000017#include "llvm/Support/raw_ostream.h"
18#include "llvm/ADT/SmallString.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000019#include "llvm/CodeGen/AsmPrinter.h"
Eric Christopher50880d02010-09-18 18:52:28 +000020#include "llvm/CodeGen/MachineInstr.h"
21#include "llvm/MC/MCStreamer.h"
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000022#include "llvm/Target/TargetRegistry.h"
23
24using namespace llvm;
25
26namespace {
27 class PTXAsmPrinter : public AsmPrinter {
28 public:
29 explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) :
30 AsmPrinter(TM, Streamer) {}
31 const char *getPassName() const { return "PTX Assembly Printer"; }
Eric Christopher50880d02010-09-18 18:52:28 +000032
33 virtual void EmitInstruction(const MachineInstr *MI);
34
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000035 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
36
Eric Christopher50880d02010-09-18 18:52:28 +000037 // autogen'd.
38 void printInstruction(const MachineInstr *MI, raw_ostream &OS);
39 static const char *getRegisterName(unsigned RegNo);
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000040 };
41} // namespace
42
Eric Christopher50880d02010-09-18 18:52:28 +000043void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
44 SmallString<128> str;
Che-Liang Chioub48f2c22010-10-19 13:14:40 +000045 raw_svector_ostream OS(str);
46 printInstruction(MI, OS);
47 OS << ';';
48 OutStreamer.EmitRawText(OS.str());
49}
50
51void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
52 raw_ostream &OS) {
53 const MachineOperand &MO = MI->getOperand(opNum);
54
55 switch (MO.getType()) {
56 default:
57 llvm_unreachable("<unknown operand type>");
58 break;
59 case MachineOperand::MO_Register:
60 OS << getRegisterName(MO.getReg());
61 break;
62 case MachineOperand::MO_Immediate:
63 OS << (int) MO.getImm();
64 break;
65 }
Eric Christopher50880d02010-09-18 18:52:28 +000066}
67
68#include "PTXGenAsmWriter.inc"
69
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000070// Force static initialization.
Eric Christopher50880d02010-09-18 18:52:28 +000071extern "C" void LLVMInitializePTXAsmPrinter() {
Nick Lewyckyf7a3c502010-09-07 18:14:24 +000072 RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
73}