Implementing the driver integration within clang – Beyond Instruction Selection-2

  1. Following the getter methods, we must also define methods that perform various checks on the M88k target. The first one checks if the M88k target has a particular target feature, supplied in the form of a string. Secondly, we add a function to validate the constraints when inline assembly is used. Finally, we have a function that checks if a specific CPU is valid for the M88k target, also supplied in the form of a string: bool hasFeature(StringRef Feature) const override;
    bool validateAsmConstraint(const char *&Name,
    TargetInfo::ConstraintInfo &info) const override;
    bool isValidCPUName(StringRef Name) const override;
  2. Next, let’s declare setter methods for our M88kTargetInfo class. The first one simply sets the specific M88k CPU that we want to target, while the second method sets a vector to contain all of the valid supported CPUs for M88k: bool setCPU(const std::string &Name) override;
    void fillValidCPUList(SmallVectorImpl &Values) const override;
    };
  3. To complete our header for the driver implementation, let’s conclude our namespaces and macro definition that we added in the beginning:

} // namespace targets
} // namespace clang
endif // LLVM_CLANG_LIB_BASIC_TARGETS_M88K_H

  1. Now that we’ve completed the M88k header file within clang/lib/Basic/Targets, we must add the corresponding TargetInfo C++ implementation within clang/lib/Basic/Targets/M88k.cpp. We’ll start by including the required header files, especially the new M88k.h header we have just created:

include “M88k.h”
include “clang/Basic/Builtins.h”
include “clang/Basic/Diagnostic.h”
include “clang/Basic/TargetBuiltins.h”
include “llvm/ADT/StringExtras.h”
include “llvm/ADT/StringRef.h”
include “llvm/ADT/StringSwitch.h”
include “llvm/TargetParser/TargetParser.h”
include

  1. As we did previously in the header, we start with the clang and targets namespaces, and then also begin implementing the constructor for the M88kTargetInfo class:

namespace clang {
namespace targets {
M88kTargetInfo::M88kTargetInfo(const llvm::Triple &Triple,
const TargetOptions &)
: TargetInfo(Triple) {

  1. Within the constructor, we set the data layout string for the M88k target. As you may have seen before, this data layout string is seen at the top of the emitted LLVM IR files. An explanation of each section of the data layout string is described here: std::string Layout = “”;
    Layout += “E”; // M68k is Big Endian
    Layout += “-m:e”;
    Layout += “-p:32:32:32”; // Pointers are 32 bit.
    // All scalar types are naturally aligned.
    Layout += “-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64”;
    // Floats and doubles are also naturally aligned.
    Layout += “-f32:32:32-f64:64:64”;
    // We prefer 16 bits of aligned for all globals; see above.
    Layout += “-a:8:16”;
    Layout += “-n32”; // Integer registers are 32bits.
    resetDataLayout(Layout);

Leave a Reply

Your email address will not be published. Required fields are marked *