2009-07-25 06:49:55 +00:00
|
|
|
//===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
|
2009-05-03 12:57:15 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to the MSP430 assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
#include "MSP430.h"
|
|
|
|
#include "MSP430InstrInfo.h"
|
2009-10-21 00:10:47 +00:00
|
|
|
#include "MSP430InstPrinter.h"
|
2009-08-22 20:48:53 +00:00
|
|
|
#include "MSP430MCAsmInfo.h"
|
2009-10-21 00:11:08 +00:00
|
|
|
#include "MSP430MCInstLower.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
#include "MSP430TargetMachine.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2009-11-07 17:12:58 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/DwarfWriter.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2009-10-21 00:11:08 +00:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2009-08-19 05:49:37 +00:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2009-09-13 17:14:04 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-07-28 03:13:23 +00:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2009-07-25 06:49:55 +00:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-10-21 00:11:08 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-07-14 20:18:05 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(EmittedInsts, "Number of machine instrs printed");
|
|
|
|
|
2009-10-21 00:11:08 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
EnableMCInst("enable-msp430-mcinst-printer", cl::Hidden,
|
|
|
|
cl::desc("enable experimental mcinst gunk in the msp430 backend"));
|
|
|
|
|
2009-05-03 12:57:15 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
class MSP430AsmPrinter : public AsmPrinter {
|
2009-05-03 12:57:15 +00:00
|
|
|
public:
|
2009-07-15 23:17:20 +00:00
|
|
|
MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
|
2009-08-22 21:43:10 +00:00
|
|
|
const MCAsmInfo *MAI, bool V)
|
|
|
|
: AsmPrinter(O, TM, MAI, V) {}
|
2009-05-03 12:57:15 +00:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "MSP430 Assembly Printer";
|
|
|
|
}
|
|
|
|
|
2009-10-21 00:10:47 +00:00
|
|
|
void printMCInst(const MCInst *MI) {
|
|
|
|
MSP430InstPrinter(O, *MAI).printInstruction(MI);
|
|
|
|
}
|
2009-05-03 13:06:03 +00:00
|
|
|
void printOperand(const MachineInstr *MI, int OpNum,
|
|
|
|
const char* Modifier = 0);
|
2009-10-21 00:13:25 +00:00
|
|
|
void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
|
|
|
|
printOperand(MI, OpNum);
|
|
|
|
}
|
2009-05-03 13:06:03 +00:00
|
|
|
void printSrcMemOperand(const MachineInstr *MI, int OpNum,
|
|
|
|
const char* Modifier = 0);
|
2009-05-03 13:12:23 +00:00
|
|
|
void printCCOperand(const MachineInstr *MI, int OpNum);
|
2009-05-03 12:57:15 +00:00
|
|
|
void printMachineInstruction(const MachineInstr * MI);
|
2009-08-26 13:44:29 +00:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode);
|
2009-10-11 19:13:34 +00:00
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo, unsigned AsmVariant,
|
|
|
|
const char *ExtraCode);
|
2009-10-21 00:11:08 +00:00
|
|
|
void printInstructionThroughMCStreamer(const MachineInstr *MI);
|
2009-05-03 13:17:11 +00:00
|
|
|
|
2009-05-03 12:57:15 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AsmPrinter::getAnalysisUsage(AU);
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2009-11-07 17:12:58 +00:00
|
|
|
|
2009-05-03 13:01:41 +00:00
|
|
|
bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
2009-05-03 13:17:11 +00:00
|
|
|
SetupMachineFunction(MF);
|
2009-05-03 13:17:31 +00:00
|
|
|
O << "\n\n";
|
2010-01-27 00:17:20 +00:00
|
|
|
|
|
|
|
EmitFunctionHeader();
|
2009-05-03 13:17:11 +00:00
|
|
|
|
2009-05-03 13:01:41 +00:00
|
|
|
// Print out code for the function.
|
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
// Print a label for the basic block.
|
2009-10-06 17:38:38 +00:00
|
|
|
EmitBasicBlockStart(I);
|
2009-05-03 13:01:41 +00:00
|
|
|
|
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
2009-05-03 13:17:31 +00:00
|
|
|
II != E; ++II)
|
2009-05-03 13:01:41 +00:00
|
|
|
// Print the assembly for the instruction.
|
|
|
|
printMachineInstruction(II);
|
|
|
|
}
|
|
|
|
|
2010-01-17 21:43:43 +00:00
|
|
|
if (MAI->hasDotTypeDotSizeDirective())
|
|
|
|
O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
|
2009-05-03 13:17:11 +00:00
|
|
|
|
2010-01-26 22:06:58 +00:00
|
|
|
// Print out constants referenced by the function
|
|
|
|
EmitConstantPool(MF.getConstantPool());
|
|
|
|
EmitJumpTableInfo(MF);
|
|
|
|
|
2009-05-03 12:57:15 +00:00
|
|
|
// We didn't modify anything
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-03 13:02:04 +00:00
|
|
|
void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
|
2009-05-03 12:57:15 +00:00
|
|
|
++EmittedInsts;
|
|
|
|
|
2009-10-06 02:19:11 +00:00
|
|
|
processDebugLoc(MI, true);
|
2009-09-09 23:14:36 +00:00
|
|
|
|
2009-11-07 17:12:21 +00:00
|
|
|
printInstructionThroughMCStreamer(MI);
|
2009-10-11 19:14:02 +00:00
|
|
|
|
2009-11-13 21:34:57 +00:00
|
|
|
if (VerboseAsm)
|
2009-09-09 23:14:36 +00:00
|
|
|
EmitComments(*MI);
|
|
|
|
O << '\n';
|
2009-10-06 02:19:11 +00:00
|
|
|
|
|
|
|
processDebugLoc(MI, false);
|
2009-05-03 12:57:15 +00:00
|
|
|
}
|
2009-05-03 13:02:04 +00:00
|
|
|
|
2009-05-03 13:06:03 +00:00
|
|
|
void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
|
|
|
|
const char* Modifier) {
|
2009-05-03 13:02:04 +00:00
|
|
|
const MachineOperand &MO = MI->getOperand(OpNum);
|
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_Register:
|
2009-11-07 17:12:21 +00:00
|
|
|
O << MSP430InstPrinter::getRegisterName(MO.getReg());
|
2009-05-03 13:08:13 +00:00
|
|
|
return;
|
2009-05-03 13:02:04 +00:00
|
|
|
case MachineOperand::MO_Immediate:
|
2009-05-03 13:06:03 +00:00
|
|
|
if (!Modifier || strcmp(Modifier, "nohash"))
|
|
|
|
O << '#';
|
|
|
|
O << MO.getImm();
|
2009-05-03 13:08:13 +00:00
|
|
|
return;
|
2009-05-03 13:02:04 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2010-01-26 04:55:51 +00:00
|
|
|
O << *MO.getMBB()->getSymbol(OutContext);
|
2009-05-03 13:08:13 +00:00
|
|
|
return;
|
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
bool isMemOp = Modifier && !strcmp(Modifier, "mem");
|
2009-10-11 19:14:02 +00:00
|
|
|
uint64_t Offset = MO.getOffset();
|
2009-05-03 13:08:13 +00:00
|
|
|
|
2009-10-11 19:14:02 +00:00
|
|
|
O << (isMemOp ? '&' : '#');
|
|
|
|
if (Offset)
|
|
|
|
O << '(' << Offset << '+';
|
2009-05-03 13:08:13 +00:00
|
|
|
|
2010-01-17 21:43:43 +00:00
|
|
|
O << *GetGlobalValueSymbol(MO.getGlobal());
|
2010-01-16 00:21:18 +00:00
|
|
|
|
2009-10-11 19:14:02 +00:00
|
|
|
if (Offset)
|
|
|
|
O << ')';
|
2009-05-03 13:08:13 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2009-05-03 13:14:46 +00:00
|
|
|
case MachineOperand::MO_ExternalSymbol: {
|
2009-10-11 19:14:02 +00:00
|
|
|
bool isMemOp = Modifier && !strcmp(Modifier, "mem");
|
2010-01-16 00:21:18 +00:00
|
|
|
O << (isMemOp ? '&' : '#');
|
|
|
|
O << MAI->getGlobalPrefix() << MO.getSymbolName();
|
2009-05-03 13:14:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-05-03 13:02:04 +00:00
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Not implemented yet!");
|
2009-05-03 13:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-03 13:06:03 +00:00
|
|
|
|
|
|
|
void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
|
|
|
|
const char* Modifier) {
|
2009-05-03 13:09:40 +00:00
|
|
|
const MachineOperand &Base = MI->getOperand(OpNum);
|
|
|
|
const MachineOperand &Disp = MI->getOperand(OpNum+1);
|
2009-05-03 13:09:10 +00:00
|
|
|
|
2009-11-07 17:13:35 +00:00
|
|
|
// Print displacement first
|
|
|
|
if (!Disp.isImm()) {
|
|
|
|
printOperand(MI, OpNum+1, "mem");
|
|
|
|
} else {
|
|
|
|
if (!Base.getReg())
|
|
|
|
O << '&';
|
|
|
|
|
|
|
|
printOperand(MI, OpNum+1, "nohash");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Print register base field
|
|
|
|
if (Base.getReg()) {
|
|
|
|
O << '(';
|
2009-05-03 13:09:10 +00:00
|
|
|
printOperand(MI, OpNum);
|
2009-11-07 17:13:35 +00:00
|
|
|
O << ')';
|
|
|
|
}
|
2009-05-03 13:06:03 +00:00
|
|
|
}
|
|
|
|
|
2009-05-03 13:12:23 +00:00
|
|
|
void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
|
|
|
|
unsigned CC = MI->getOperand(OpNum).getImm();
|
|
|
|
|
|
|
|
switch (CC) {
|
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unsupported CC code");
|
2009-05-03 13:12:23 +00:00
|
|
|
break;
|
2009-10-21 19:16:49 +00:00
|
|
|
case MSP430CC::COND_E:
|
2009-05-03 13:16:54 +00:00
|
|
|
O << "eq";
|
2009-05-03 13:12:23 +00:00
|
|
|
break;
|
2009-10-21 19:16:49 +00:00
|
|
|
case MSP430CC::COND_NE:
|
2009-05-03 13:12:23 +00:00
|
|
|
O << "ne";
|
|
|
|
break;
|
2009-10-21 19:16:49 +00:00
|
|
|
case MSP430CC::COND_HS:
|
2009-05-03 13:12:23 +00:00
|
|
|
O << "hs";
|
|
|
|
break;
|
2009-10-21 19:16:49 +00:00
|
|
|
case MSP430CC::COND_LO:
|
2009-05-03 13:12:23 +00:00
|
|
|
O << "lo";
|
|
|
|
break;
|
2009-10-21 19:16:49 +00:00
|
|
|
case MSP430CC::COND_GE:
|
2009-05-03 13:12:23 +00:00
|
|
|
O << "ge";
|
|
|
|
break;
|
2009-10-21 19:16:49 +00:00
|
|
|
case MSP430CC::COND_L:
|
2009-05-03 13:12:23 +00:00
|
|
|
O << 'l';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-25 06:49:55 +00:00
|
|
|
|
2009-08-26 13:44:29 +00:00
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode) {
|
|
|
|
// Does this asm operand have a single letter operand modifier?
|
|
|
|
if (ExtraCode && ExtraCode[0])
|
|
|
|
return true; // Unknown modifier.
|
|
|
|
|
|
|
|
printOperand(MI, OpNo);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-11 19:13:34 +00:00
|
|
|
bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo, unsigned AsmVariant,
|
|
|
|
const char *ExtraCode) {
|
|
|
|
if (ExtraCode && ExtraCode[0]) {
|
|
|
|
return true; // Unknown modifier.
|
|
|
|
}
|
|
|
|
printSrcMemOperand(MI, OpNo);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-21 00:11:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-11-07 17:12:21 +00:00
|
|
|
void MSP430AsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI){
|
2009-10-21 00:11:08 +00:00
|
|
|
|
2009-10-21 00:13:05 +00:00
|
|
|
MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
|
2009-10-21 00:11:08 +00:00
|
|
|
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case TargetInstrInfo::DBG_LABEL:
|
|
|
|
case TargetInstrInfo::EH_LABEL:
|
|
|
|
case TargetInstrInfo::GC_LABEL:
|
|
|
|
printLabel(MI);
|
|
|
|
return;
|
|
|
|
case TargetInstrInfo::KILL:
|
2009-11-04 19:24:37 +00:00
|
|
|
printKill(MI);
|
2009-10-21 00:11:08 +00:00
|
|
|
return;
|
|
|
|
case TargetInstrInfo::INLINEASM:
|
|
|
|
printInlineAsm(MI);
|
|
|
|
return;
|
|
|
|
case TargetInstrInfo::IMPLICIT_DEF:
|
|
|
|
printImplicitDef(MI);
|
|
|
|
return;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MCInst TmpInst;
|
|
|
|
MCInstLowering.Lower(MI, TmpInst);
|
|
|
|
|
|
|
|
printMCInst(&TmpInst);
|
|
|
|
}
|
|
|
|
|
2009-11-07 17:12:21 +00:00
|
|
|
static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
|
|
|
|
unsigned SyntaxVariant,
|
|
|
|
const MCAsmInfo &MAI,
|
|
|
|
raw_ostream &O) {
|
|
|
|
if (SyntaxVariant == 0)
|
|
|
|
return new MSP430InstPrinter(O, MAI);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-14 19:06:50 +00:00
|
|
|
// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeMSP430AsmPrinter() {
|
|
|
|
RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
|
2009-11-07 17:12:21 +00:00
|
|
|
TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
|
|
|
|
createMSP430MCInstPrinter);
|
2009-07-25 06:49:55 +00:00
|
|
|
}
|