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"
|
2010-03-12 21:19:23 +00:00
|
|
|
#include "llvm/Target/Mangler.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-07-14 20:18:05 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2009-05-03 12:57:15 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
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,
|
2010-03-13 20:55:24 +00:00
|
|
|
MCStreamer &Streamer)
|
|
|
|
: AsmPrinter(O, TM, Streamer) {}
|
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);
|
2010-01-28 01:48:52 +00:00
|
|
|
void EmitInstruction(const MachineInstr *MI);
|
2009-05-03 12:57:15 +00:00
|
|
|
|
|
|
|
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: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()) {
|
2010-01-28 01:48:52 +00:00
|
|
|
default: assert(0 && "Not implemented yet!");
|
2009-05-03 13:02:04 +00:00
|
|
|
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
|
|
|
|
2010-03-06 11:41:12 +00:00
|
|
|
// If the global address expression is a part of displacement field with a
|
|
|
|
// register base, we should not emit any prefix symbol here, e.g.
|
|
|
|
// mov.w &foo, r1
|
|
|
|
// vs
|
|
|
|
// mov.w glb(r1), r2
|
|
|
|
// Otherwise (!) msp430-as will silently miscompile the output :(
|
|
|
|
if (!Modifier || strcmp(Modifier, "nohash"))
|
|
|
|
O << (isMemOp ? '&' : '#');
|
2009-10-11 19:14:02 +00:00
|
|
|
if (Offset)
|
|
|
|
O << '(' << Offset << '+';
|
2009-05-03 13:08:13 +00:00
|
|
|
|
2010-03-12 21:19:23 +00:00
|
|
|
O << *Mang->getSymbol(MO.getGlobal());
|
2010-03-06 11:41:12 +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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
2010-03-06 11:41:12 +00:00
|
|
|
// Imm here is in fact global address - print extra modifier.
|
|
|
|
if (Disp.isImm() && !Base.getReg())
|
|
|
|
O << '&';
|
|
|
|
printOperand(MI, OpNum+1, "nohash");
|
2009-11-07 17:13:35 +00:00
|
|
|
|
|
|
|
// 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) {
|
2010-01-28 01:48:52 +00:00
|
|
|
switch (MI->getOperand(OpNum).getImm()) {
|
|
|
|
default: assert(0 && "Unknown cond");
|
|
|
|
case MSP430CC::COND_E: O << "eq"; break;
|
|
|
|
case MSP430CC::COND_NE: O << "ne"; break;
|
|
|
|
case MSP430CC::COND_HS: O << "hs"; break;
|
|
|
|
case MSP430CC::COND_LO: O << "lo"; break;
|
|
|
|
case MSP430CC::COND_GE: O << "ge"; break;
|
|
|
|
case MSP430CC::COND_L: O << 'l'; break;
|
2009-05-03 13:12:23 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
2010-01-28 01:48:52 +00:00
|
|
|
void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2009-10-21 00:13:05 +00:00
|
|
|
MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
|
2009-10-21 00:11:08 +00:00
|
|
|
|
|
|
|
MCInst TmpInst;
|
|
|
|
MCInstLowering.Lower(MI, TmpInst);
|
2010-02-03 01:15:03 +00:00
|
|
|
OutStreamer.EmitInstruction(TmpInst);
|
2009-10-21 00:11:08 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|