After applying this patch, the asm printer part of the backend is done. You'll never see "Assertion `AsmInfo && "MCAsmInfo not initialized."" error when invoke llc to run UniCore test case. Signed-off-by: Chen Wei-Ren <chenwj@xxxxxxxxxxxxxxxxx> --- lib/Target/UniCore/InstPrinter/LLVMBuild.txt | 23 +++++++ lib/Target/UniCore/InstPrinter/Makefile | 15 +++++ .../UniCore/InstPrinter/UniCoreInstPrinter.cpp | 44 +++++++++++++ .../UniCore/InstPrinter/UniCoreInstPrinter.h | 38 +++++++++++ lib/Target/UniCore/LLVMBuild.txt | 6 +- lib/Target/UniCore/MCTargetDesc/LLVMBuild.txt | 2 +- .../UniCore/MCTargetDesc/UniCoreMCAsmInfo.cpp | 21 ++++++ lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.h | 31 +++++++++ .../UniCore/MCTargetDesc/UniCoreMCTargetDesc.cpp | 33 ++++++++++ lib/Target/UniCore/Makefile | 8 +- lib/Target/UniCore/UniCore.td | 6 ++ lib/Target/UniCore/UniCoreAsmPrinter.cpp | 67 ++++++++++++++++++++ lib/Target/UniCore/UniCoreMCInstLower.cpp | 58 +++++++++++++++++ lib/Target/UniCore/UniCoreMCInstLower.h | 41 ++++++++++++ 14 files changed, 385 insertions(+), 8 deletions(-) create mode 100644 lib/Target/UniCore/InstPrinter/LLVMBuild.txt create mode 100644 lib/Target/UniCore/InstPrinter/Makefile create mode 100644 lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.cpp create mode 100644 lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.h create mode 100644 lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.cpp create mode 100644 lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.h create mode 100644 lib/Target/UniCore/UniCoreAsmPrinter.cpp create mode 100644 lib/Target/UniCore/UniCoreMCInstLower.cpp create mode 100644 lib/Target/UniCore/UniCoreMCInstLower.h diff --git a/lib/Target/UniCore/InstPrinter/LLVMBuild.txt b/lib/Target/UniCore/InstPrinter/LLVMBuild.txt new file mode 100644 index 0000000..a8d5ac4 --- /dev/null +++ b/lib/Target/UniCore/InstPrinter/LLVMBuild.txt @@ -0,0 +1,23 @@ +;===- ./lib/Target/UniCore/InstPrinter/LLVMBuild.txt ----------*- Conf -*--===; +; +; The LLVM Compiler Infrastructure +; +; This file is distributed under the University of Illinois Open Source +; License. See LICENSE.TXT for details. +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Library +name = UniCoreAsmPrinter +parent = UniCore +required_libraries = MC Support +add_to_library_groups = UniCore diff --git a/lib/Target/UniCore/InstPrinter/Makefile b/lib/Target/UniCore/InstPrinter/Makefile new file mode 100644 index 0000000..c43d031 --- /dev/null +++ b/lib/Target/UniCore/InstPrinter/Makefile @@ -0,0 +1,15 @@ +##===- lib/Target/UniCore/InstPrinter/Makefile ------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +LEVEL = ../../../.. +LIBRARYNAME = LLVMUniCoreAsmPrinter + +# Hack: we need to include 'main' UniCore target directory to grab private headers +CPP.Flags += -I$(PROJ_OBJ_DIR)/.. -I$(PROJ_SRC_DIR)/.. + +include $(LEVEL)/Makefile.common diff --git a/lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.cpp b/lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.cpp new file mode 100644 index 0000000..39f33af --- /dev/null +++ b/lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.cpp @@ -0,0 +1,44 @@ +//===-- UniCoreInstPrinter.cpp - Convert UniCore MCInst to asm syntax ---===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This class prints an UniCore MCInst to a .s file. +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "asm-printer" +#include "UniCore.h" +#include "UniCoreInstPrinter.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FormattedStream.h" + +using namespace llvm; + +// Include the auto-generated portion of the assembly writer. +#include "UniCoreGenAsmWriter.inc" + +void UniCoreInstPrinter::printInst(const MCInst *MI, raw_ostream &O, + StringRef Annot) { + printInstruction(MI, O); + printAnnotation(O, Annot); +} + +void UniCoreInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, + raw_ostream &O, const char *Modifier) { + assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported"); + const MCOperand &Op = MI->getOperand(OpNo); + if (Op.isReg()) { + O << getRegisterName(Op.getReg()); + } else if (Op.isImm()) { + O << Op.getImm(); + } else + assert(0 && "Unknown operand in printOperand"); +} diff --git a/lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.h b/lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.h new file mode 100644 index 0000000..81283fd --- /dev/null +++ b/lib/Target/UniCore/InstPrinter/UniCoreInstPrinter.h @@ -0,0 +1,38 @@ +//= UniCoreInstPrinter.h - Convert UniCore MCInst to asm syntax -*- C++ -*--// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This class prints a UniCore MCInst to a .s file. +// +//===----------------------------------------------------------------------===// + +#ifndef UNICOREINSTPRINTER_H +#define UNICOREINSTPRINTER_H + +#include "llvm/MC/MCInstPrinter.h" + +namespace llvm { + class MCOperand; + + class UniCoreInstPrinter : public MCInstPrinter { + public: + UniCoreInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII, + const MCRegisterInfo &MRI) + : MCInstPrinter(MAI, MII, MRI) {} + + void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot); + void printOperand(const MCInst *MI, unsigned OpNo, + raw_ostream &O, const char *Modifier = 0); + + // Autogenerated by tblgen. + void printInstruction(const MCInst *MI, raw_ostream &O); + static const char *getRegisterName(unsigned RegNo); + }; +} + +#endif diff --git a/lib/Target/UniCore/LLVMBuild.txt b/lib/Target/UniCore/LLVMBuild.txt index ac7810c..9678631 100644 --- a/lib/Target/UniCore/LLVMBuild.txt +++ b/lib/Target/UniCore/LLVMBuild.txt @@ -16,19 +16,19 @@ ;===------------------------------------------------------------------------===; [common] -subdirectories = MCTargetDesc TargetInfo +subdirectories = InstPrinter MCTargetDesc TargetInfo [component_0] type = TargetGroup name = UniCore parent = Target has_asmparser = 0 -has_asmprinter = 0 +has_asmprinter = 1 has_disassembler = 0 [component_1] type = Library name = UniCoreCodeGen parent = UniCore -required_libraries = AsmPrinter CodeGen Core UniCoreDesc UniCoreInfo SelectionDAG Support Target +required_libraries = AsmPrinter CodeGen Core MC UniCoreDesc UniCoreInfo SelectionDAG Support Target add_to_library_groups = UniCore diff --git a/lib/Target/UniCore/MCTargetDesc/LLVMBuild.txt b/lib/Target/UniCore/MCTargetDesc/LLVMBuild.txt index 9db6193..9076730 100644 --- a/lib/Target/UniCore/MCTargetDesc/LLVMBuild.txt +++ b/lib/Target/UniCore/MCTargetDesc/LLVMBuild.txt @@ -19,5 +19,5 @@ type = Library name = UniCoreDesc parent = UniCore -required_libraries = MC Support +required_libraries = MC UniCoreAsmPrinter UniCoreInfo Support Target add_to_library_groups = UniCore diff --git a/lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.cpp b/lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.cpp new file mode 100644 index 0000000..f344524 --- /dev/null +++ b/lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.cpp @@ -0,0 +1,21 @@ +//===-- UniCoreMCAsmInfo.cpp - UniCore asm properties --------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains the declarations of the UniCoreMCAsmInfo properties. +// +//===----------------------------------------------------------------------===// + +#include "UniCoreMCAsmInfo.h" +#include "llvm/ADT/StringRef.h" +using namespace llvm; + +void UniCoreMCAsmInfo::anchor() { } + +UniCoreMCAsmInfo::UniCoreMCAsmInfo(const Target &T, StringRef TT) { +} diff --git a/lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.h b/lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.h new file mode 100644 index 0000000..acc9bb1 --- /dev/null +++ b/lib/Target/UniCore/MCTargetDesc/UniCoreMCAsmInfo.h @@ -0,0 +1,31 @@ +//===-- UniCoreMCAsmInfo.h - UniCore asm properties ----------*- C++ -*--===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains the declaration of the UniCoreMCAsmInfo class. +// +//===----------------------------------------------------------------------===// + +#ifndef UNICORETARGETASMINFO_H +#define UNICORETARGETASMINFO_H + +#include "llvm/MC/MCAsmInfo.h" + +namespace llvm { + class StringRef; + class Target; + + class UniCoreMCAsmInfo : public MCAsmInfo { + virtual void anchor(); + public: + explicit UniCoreMCAsmInfo(const Target &T, StringRef TT); + }; + +} // namespace llvm + +#endif diff --git a/lib/Target/UniCore/MCTargetDesc/UniCoreMCTargetDesc.cpp b/lib/Target/UniCore/MCTargetDesc/UniCoreMCTargetDesc.cpp index 86750e4..83c3c11 100644 --- a/lib/Target/UniCore/MCTargetDesc/UniCoreMCTargetDesc.cpp +++ b/lib/Target/UniCore/MCTargetDesc/UniCoreMCTargetDesc.cpp @@ -12,6 +12,8 @@ //===----------------------------------------------------------------------===// #include "UniCoreMCTargetDesc.h" +#include "UniCoreMCAsmInfo.h" +#include "InstPrinter/UniCoreInstPrinter.h" #include "llvm/MC/MCCodeGenInfo.h" #include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCRegisterInfo.h" @@ -48,6 +50,26 @@ static MCSubtargetInfo *createUniCoreMCSubtargetInfo(StringRef TT, StringRef CPU return X; } +static MCCodeGenInfo *createUniCoreMCCodeGenInfo(StringRef TT, Reloc::Model RM, + CodeModel::Model CM, + CodeGenOpt::Level OL) { + MCCodeGenInfo *X = new MCCodeGenInfo(); + X->InitMCCodeGenInfo(RM, CM, OL); + return X; +} + +static MCInstPrinter *createUniCoreMCInstPrinter(const Target &T, + unsigned SyntaxVariant, + const MCAsmInfo &MAI, + const MCInstrInfo &MII, + const MCRegisterInfo &MRI, + const MCSubtargetInfo &STI) { + if (SyntaxVariant == 0) + return new UniCoreInstPrinter(MAI, MII, MRI); + + return 0; +} + extern "C" void LLVMInitializeUniCoreTargetMC() { // Register the MC instruction info. TargetRegistry::RegisterMCInstrInfo(TheUniCoreTarget, createUniCoreMCInstrInfo); @@ -59,4 +81,15 @@ extern "C" void LLVMInitializeUniCoreTargetMC() { // Register the MC subtarget info. TargetRegistry::RegisterMCSubtargetInfo(TheUniCoreTarget, createUniCoreMCSubtargetInfo); + + // Register the MC asm info. + RegisterMCAsmInfo<UniCoreMCAsmInfo> X(TheUniCoreTarget); + + // Register the MC codegen info. + TargetRegistry::RegisterMCCodeGenInfo(TheUniCoreTarget, + createUniCoreMCCodeGenInfo); + + // Register the MCInstPrinter. + TargetRegistry::RegisterMCInstPrinter(TheUniCoreTarget, + createUniCoreMCInstPrinter); } diff --git a/lib/Target/UniCore/Makefile b/lib/Target/UniCore/Makefile index 388da94..5ee8c67 100644 --- a/lib/Target/UniCore/Makefile +++ b/lib/Target/UniCore/Makefile @@ -11,10 +11,10 @@ LIBRARYNAME = LLVMUniCoreCodeGen TARGET = UniCore # Make sure that tblgen is run, first thing. -BUILT_SOURCES = UniCoreGenRegisterInfo.inc \ - UniCoreGenInstrInfo.inc \ - UniCoreGenSubtargetInfo.inc +BUILT_SOURCES = UniCoreGenRegisterInfo.inc UniCoreGenInstrInfo.inc \ + UniCoreGenAsmWriter.inc \ + UniCoreGenSubtargetInfo.inc -DIRS = TargetInfo MCTargetDesc +DIRS = InstPrinter TargetInfo MCTargetDesc include $(LEVEL)/Makefile.common diff --git a/lib/Target/UniCore/UniCore.td b/lib/Target/UniCore/UniCore.td index 592de5d..a1cfc05 100644 --- a/lib/Target/UniCore/UniCore.td +++ b/lib/Target/UniCore/UniCore.td @@ -42,10 +42,16 @@ def : Proc<"unicore32", []>; def UniCoreInstrInfo : InstrInfo; +def UniCoreAsmWriter : AsmWriter { + string AsmWriterClassName = "InstPrinter"; + bit isMCAsmWriter = 1; +} + //===----------------------------------------------------------------------===// // Target Declaration //===----------------------------------------------------------------------===// def UniCore : Target { let InstructionSet = UniCoreInstrInfo; + let AssemblyWriters = [UniCoreAsmWriter]; } diff --git a/lib/Target/UniCore/UniCoreAsmPrinter.cpp b/lib/Target/UniCore/UniCoreAsmPrinter.cpp new file mode 100644 index 0000000..b70e740 --- /dev/null +++ b/lib/Target/UniCore/UniCoreAsmPrinter.cpp @@ -0,0 +1,67 @@ +//===-- UniCoreAsmPrinter.cpp - UniCore LLVM assembly writer -------------===// +// +// 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 UniCore assembly language. +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "asm-printer" +#include "UniCore.h" +#include "UniCoreInstrInfo.h" +#include "UniCoreMCInstLower.h" +#include "UniCoreTargetMachine.h" +#include "InstPrinter/UniCoreInstPrinter.h" +#include "llvm/Constants.h" +#include "llvm/DerivedTypes.h" +#include "llvm/Module.h" +#include "llvm/Assembly/Writer.h" +#include "llvm/CodeGen/AsmPrinter.h" +#include "llvm/CodeGen/MachineModuleInfo.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineConstantPool.h" +#include "llvm/CodeGen/MachineInstr.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCStreamer.h" +#include "llvm/MC/MCSymbol.h" +#include "llvm/Target/Mangler.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +namespace { + class UniCoreAsmPrinter : public AsmPrinter { + public: + UniCoreAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) + : AsmPrinter(TM, Streamer) {} + + virtual const char *getPassName() const { + return "UniCore Assembly Printer"; + } + + void EmitInstruction(const MachineInstr *MI); + }; +} // end of anonymous namespace + + +//===----------------------------------------------------------------------===// +void UniCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) { + UniCoreMCInstLower MCInstLowering(OutContext, *Mang, *this); + + MCInst TmpInst; + MCInstLowering.Lower(MI, TmpInst); + OutStreamer.EmitInstruction(TmpInst); +} + +// Force static initialization. +extern "C" void LLVMInitializeUniCoreAsmPrinter() { + RegisterAsmPrinter<UniCoreAsmPrinter> X(TheUniCoreTarget); +} diff --git a/lib/Target/UniCore/UniCoreMCInstLower.cpp b/lib/Target/UniCore/UniCoreMCInstLower.cpp new file mode 100644 index 0000000..d8b19a3 --- /dev/null +++ b/lib/Target/UniCore/UniCoreMCInstLower.cpp @@ -0,0 +1,58 @@ +//=-- UniCoreMCInstLower.cpp - Convert UniCore MachineInstr to an MCInst --=// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains code to lower UniCore MachineInstrs to their corresponding +// MCInst records. +// +//===----------------------------------------------------------------------===// + +#include "UniCoreMCInstLower.h" +#include "llvm/CodeGen/AsmPrinter.h" +#include "llvm/CodeGen/MachineBasicBlock.h" +#include "llvm/CodeGen/MachineInstr.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCInst.h" +#include "llvm/Target/Mangler.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/ADT/SmallString.h" +using namespace llvm; + +void UniCoreMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const { + OutMI.setOpcode(MI->getOpcode()); + + for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { + const MachineOperand &MO = MI->getOperand(i); + + MCOperand MCOp; + switch (MO.getType()) { + default: + MI->dump(); + llvm_unreachable("unknown operand type"); + case MachineOperand::MO_Register: + // Ignore all implicit register operands. + if (MO.isImplicit()) continue; + MCOp = MCOperand::CreateReg(MO.getReg()); + break; + case MachineOperand::MO_Immediate: + MCOp = MCOperand::CreateImm(MO.getImm()); + break; + case MachineOperand::MO_MachineBasicBlock: + MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create( + MO.getMBB()->getSymbol(), Ctx)); + break; + case MachineOperand::MO_RegisterMask: + continue; + } + + OutMI.addOperand(MCOp); + } +} diff --git a/lib/Target/UniCore/UniCoreMCInstLower.h b/lib/Target/UniCore/UniCoreMCInstLower.h new file mode 100644 index 0000000..3beebc5 --- /dev/null +++ b/lib/Target/UniCore/UniCoreMCInstLower.h @@ -0,0 +1,41 @@ +//===-- UniCoreMCInstLower.h - Lower MachineInstr to MCInst ----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef OPENRISC_MCINSTLOWER_H +#define OPENRISC_MCINSTLOWER_H + +#include "llvm/Support/Compiler.h" + +namespace llvm { + class AsmPrinter; + class MCContext; + class MCInst; + class MCOperand; + class MCSymbol; + class MachineInstr; + class MachineModuleInfoMachO; + class MachineOperand; + class Mangler; + + /// UniCoreMCInstLower - This class is used to lower an MachineInstr + /// into an MCInst. +class LLVM_LIBRARY_VISIBILITY UniCoreMCInstLower { + MCContext &Ctx; + Mangler &Mang; + + AsmPrinter &Printer; +public: + UniCoreMCInstLower(MCContext &ctx, Mangler &mang, AsmPrinter &printer) + : Ctx(ctx), Mang(mang), Printer(printer) {} + void Lower(const MachineInstr *MI, MCInst &OutMI) const; +}; + +} + +#endif -- 1.7.3.4 -- Wei-Ren Chen (陳韋任) Computer Systems Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.) Tel:886-2-2788-3799 #1667 Homepage: http://people.cs.nctu.edu.tw/~chenwj