[LLVMdev] Newbie question for registering new target with LLVM

Sandeep Kumar Singh deepdondo007 at gmail.com
Fri Oct 12 02:12:18 PDT 2012


Hi all, llvm newbie here.

I'm trying to learn porting with llvm for study purpose. This is my first query
on llvm mailing list.I have some idea about GCC. I choose 'rx' as a target to
port as it is also available in GCC. I have done some initial changes with llvm
source code to register target with llvm. I need to verify these changes. Can
anyone please take a chance to verify it.

Build Command(s):
=================
configure --enable-targets=Rx,arm --prefix=/home/sandeep/LLVM/prefix/
--enable-languages=c,c++
make
make install

File(s) that I have modified to register new target with LLVM infrastructure:
=============================================================================
1) llvm/configure

  1.1) Rx-*)        llvm_cv_target_arch="RX" ;;
  1.2) Rx)          TARGET_HAS_JIT=0
  1.3) case "$enableval" in
       all) TARGETS_TO_BUILD="X86 Sparc PowerPC Alpha ARM Mips CellSPU XCore
            MSP430 SystemZ Blackfin CBackend CppBackend MBlaze PTX Rx" ;;
  1.4) Rx)  TARGETS_TO_BUILD="Rx $TARGETS_TO_BUILD" ;;
  1.5) Rx)  TARGETS_TO_BUILD="Rx $TARGETS_TO_BUILD" ;;

2) lib/Support/Triple.cpp
  2.1) case Rx:      return "Rx";
  2.2) if (Name == "Rx")    return Rx;

3) lib/Target/Rx/RxTargetMachine.h
namespace llvm {
  class RxTargetMachine : public LLVMTargetMachine {
  public:
        RxTargetMachine(const Target &T, StringRef TT,
                        StringRef CPU, StringRef FS,
                        Reloc::Model RM, CodeModel::Model CM) ;
};
        extern Target TheRxTarget;
}

4) lib/Target/Rx/TargetInfo/RxTargetInfo.cpp
using namespace llvm;
Target llvm::TheRxTarget ;
extern "C" void LLVMInitializeRxTargetInfo() {
  RegisterTarget<Triple::Rx> X(TheRxTarget, "rx", "Rx");
}
extern "C" void LLVMInitializeRxTargetMC() {}

5) lib/Target/Rx/TargetInfo/Makefile
LEVEL = ../../../..
LIBRARYNAME = LLVMRxInfo

# Hack: we need to include 'main' target directory to grab private headers
CPPFLAGS = -I$(PROJ_OBJ_DIR)/.. -I$(PROJ_SRC_DIR)/..
include $(LEVEL)/Makefile.common

6) lib/Target/Rx/TargetInfo/CMakeLists.txt
include_directories( ${CMAKE_CURRENT_BINARY_DIR}/.. $ \
{CMAKE_CURRENT_SOURCE_DIR}/.. )

add_llvm_library(LLVMRxInfo
  RxTargetInfo.cpp
  )

add_llvm_library_dependencies(LLVMRxInfo
  LLVMSupport
  LLVMTarget
  )

7) lib/Target/Rx/RxTargetMachine.cpp
using namespace llvm;
extern "C" void LLVMInitializeRxTarget() {
  RegisterTargetMachine<RxTargetMachine> X(TheRxTarget);
}
RxTargetMachine::RxTargetMachine(const Target &T, StringRef TT,
                                 StringRef CPU, StringRef FS,
                                 Reloc::Model RM, CodeModel::Model CM)
                                 : LLVMTargetMachine(T, TT, CPU, FS, RM, CM)
{ }

8) lib/Target/Rx/Rx.td
include "llvm/Target/Target.td"
def RxInstrInfo : InstrInfo;
def Rx : Target {
  let InstructionSet = RxInstrInfo;
}

9) lib/Target/Rx/RxAsmPrinter.cpp
using namespace llvm;
extern "C" void LLVMInitializeRxAsmPrinter() {
    RegisterAsmPrinter<RxAsmPrinter> X(TheRxTarget);
}

RxAsmPrinter::RxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
                           : AsmPrinter(TM, Streamer){}

10) lib/Target/Rx/Makefile
LEVEL = ../../..
LIBRARYNAME = LLVMRx
TARGET = Rx
DIRS = TargetInfo
include $(LEVEL)/Makefile.common

11) lib/Target/Rx/CMakeLists.txt
set(LLVM_TARGET_DEFINITIONS Rx.td)
add_llvm_target(Rx
  RxTargetMachine.cpp
  RxAsmPrinter.cpp
  )

add_subdirectory(TargetInfo)

12) lib/Target/Rx/RxAsmPrinter.h
namespace llvm {
class RxAsmPrinter : public AsmPrinter {
public:
    explicit RxAsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
};
    extern Target TheRxTarget;
}

13) include/llvm/ADT/Triple.h
    Rx,

14)          autoconf/configure.ac
    Rx)          AC_SUBST(TARGET_HAS_JIT,0) ;;
    rx)          TARGETS_TO_BUILD="Rx $TARGETS_TO_BUILD" ;;
    Rx)          TARGETS_TO_BUILD="Rx $TARGETS_TO_BUILD" ;;
    Rx)          AC_SUBST(TARGET_HAS_JIT,0) ;;

After done above changes, dummy compiler is built successfully. I have tested:

Registered target is tested with below tool:
=============================================
[sandeep at D-5745 LLVM]$ prefix/bin/llc --version
Low Level Virtual Machine (http://llvm.org/):
  llvm version 3.0
  Optimized build.
  Built Jul  2 2012 (15:19:13).
  Host: i386-pc-linux-gnu
  Host CPU: penryn

  Registered Targets:
    Rx    - rx  <===  New registered target
    arm   - ARM
    thumb - Thumb

Doubts/question(s)
==================
1) To build dummy compiler:

 1.1) Please verify all above change(s) are OK? or I have modify some code?
 1.2) In LLVM, can we proceed our development like as GCC i.e. incremental
      approach? In case of yes, please provide me any reference.
 1.3) Please help me from anyone of llvm lovers :-) to proceed further. What
      modifications required to generate rx assembly of hello word program.
	
Any help will be highly appreciated.

Sandeep Kumar Singh



More information about the llvm-dev mailing list