Skip to content

[clang][MIPS] Add support for mipsel-windows-* targets #107744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/lib/Basic/Targets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
case llvm::Triple::NaCl:
return std::make_unique<NaClTargetInfo<NaClMips32TargetInfo>>(Triple,
Opts);
case llvm::Triple::Win32:
switch (Triple.getEnvironment()) {
case llvm::Triple::GNU:
return std::make_unique<MinGWMipsTargetInfo>(Triple, Opts);
case llvm::Triple::MSVC:
default: // Assume MSVC for unknown environments
return std::make_unique<MicrosoftMipsTargetInfo>(Triple, Opts);
}
default:
return std::make_unique<MipsTargetInfo>(Triple, Opts);
}
Expand Down
60 changes: 60 additions & 0 deletions clang/lib/Basic/Targets/Mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,63 @@ bool MipsTargetInfo::validateTarget(DiagnosticsEngine &Diags) const {

return true;
}

WindowsMipsTargetInfo::WindowsMipsTargetInfo(const llvm::Triple &Triple,
const TargetOptions &Opts)
: WindowsTargetInfo<MipsTargetInfo>(Triple, Opts), Triple(Triple) {
}

void WindowsMipsTargetInfo::getVisualStudioDefines(const LangOptions &Opts,
MacroBuilder &Builder) const {
Builder.defineMacro("_M_MRX000", "4000");
}

TargetInfo::BuiltinVaListKind
WindowsMipsTargetInfo::getBuiltinVaListKind() const {
return TargetInfo::CharPtrBuiltinVaList;
}

TargetInfo::CallingConvCheckResult
WindowsMipsTargetInfo::checkCallingConvention(CallingConv CC) const {
switch (CC) {
case CC_X86StdCall:
case CC_X86ThisCall:
case CC_X86FastCall:
case CC_X86VectorCall:
return CCCR_Ignore;
case CC_C:
case CC_OpenCLKernel:
case CC_PreserveMost:
case CC_PreserveAll:
case CC_Swift:
case CC_SwiftAsync:
return CCCR_OK;
default:
return CCCR_Warning;
}
}

// Windows MIPS, MS (C++) ABI
MicrosoftMipsTargetInfo::MicrosoftMipsTargetInfo(const llvm::Triple &Triple,
const TargetOptions &Opts)
: WindowsMipsTargetInfo(Triple, Opts) {
TheCXXABI.set(TargetCXXABI::Microsoft);
}

void MicrosoftMipsTargetInfo::getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const {
WindowsMipsTargetInfo::getTargetDefines(Opts, Builder);
WindowsMipsTargetInfo::getVisualStudioDefines(Opts, Builder);
}

MinGWMipsTargetInfo::MinGWMipsTargetInfo(const llvm::Triple &Triple,
const TargetOptions &Opts)
: WindowsMipsTargetInfo(Triple, Opts) {
TheCXXABI.set(TargetCXXABI::GenericMIPS);
}

void MinGWMipsTargetInfo::getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const {
WindowsMipsTargetInfo::getTargetDefines(Opts, Builder);
Builder.defineMacro("_MIPS_");
}
36 changes: 36 additions & 0 deletions clang/lib/Basic/Targets/Mips.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H
#define LLVM_CLANG_LIB_BASIC_TARGETS_MIPS_H

#include "OSTargets.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/TargetOptions.h"
#include "llvm/Support/Compiler.h"
Expand Down Expand Up @@ -450,6 +451,41 @@ class LLVM_LIBRARY_VISIBILITY MipsTargetInfo : public TargetInfo {
return std::make_pair(32, 32);
}
};

class LLVM_LIBRARY_VISIBILITY WindowsMipsTargetInfo
: public WindowsTargetInfo<MipsTargetInfo> {
const llvm::Triple Triple;

public:
WindowsMipsTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);

void getVisualStudioDefines(const LangOptions &Opts,
MacroBuilder &Builder) const;

BuiltinVaListKind getBuiltinVaListKind() const override;

CallingConvCheckResult checkCallingConvention(CallingConv CC) const override;
};

// Windows MIPS, MS (C++) ABI
class LLVM_LIBRARY_VISIBILITY MicrosoftMipsTargetInfo
: public WindowsMipsTargetInfo {
public:
MicrosoftMipsTargetInfo(const llvm::Triple &Triple,
const TargetOptions &Opts);

void getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const override;
};

// MIPS MinGW target
class LLVM_LIBRARY_VISIBILITY MinGWMipsTargetInfo : public WindowsMipsTargetInfo {
public:
MinGWMipsTargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);

void getTargetDefines(const LangOptions &Opts,
MacroBuilder &Builder) const override;
};
} // namespace targets
} // namespace clang

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ createTargetCodeGenInfo(CodeGenModule &CGM) {
case llvm::Triple::mipsel:
if (Triple.getOS() == llvm::Triple::NaCl)
return createPNaClTargetCodeGenInfo(CGM);
else if (Triple.getOS() == llvm::Triple::Win32)
return createWindowsMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/true);
return createMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/true);

case llvm::Triple::mips64:
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ createM68kTargetCodeGenInfo(CodeGenModule &CGM);
std::unique_ptr<TargetCodeGenInfo>
createMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32);

std::unique_ptr<TargetCodeGenInfo>
createWindowsMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32);

std::unique_ptr<TargetCodeGenInfo>
createMSP430TargetCodeGenInfo(CodeGenModule &CGM);

Expand Down
23 changes: 23 additions & 0 deletions clang/lib/CodeGen/Targets/Mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
return SizeOfUnwindException;
}
};

class WindowsMIPSTargetCodeGenInfo : public MIPSTargetCodeGenInfo {
public:
WindowsMIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
: MIPSTargetCodeGenInfo(CGT, IsO32) {}

void getDependentLibraryOption(llvm::StringRef Lib,
llvm::SmallString<24> &Opt) const override {
Opt = "/DEFAULTLIB:";
Opt += qualifyWindowsLibrary(Lib);
}

void getDetectMismatchOption(llvm::StringRef Name,
llvm::StringRef Value,
llvm::SmallString<32> &Opt) const override {
Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
}
};
}

void MipsABIInfo::CoerceToIntArgs(
Expand Down Expand Up @@ -436,3 +454,8 @@ std::unique_ptr<TargetCodeGenInfo>
CodeGen::createMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32) {
return std::make_unique<MIPSTargetCodeGenInfo>(CGM.getTypes(), IsOS32);
}

std::unique_ptr<TargetCodeGenInfo>
CodeGen::createWindowsMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32) {
return std::make_unique<WindowsMIPSTargetCodeGenInfo>(CGM.getTypes(), IsOS32);
}
3 changes: 3 additions & 0 deletions clang/lib/Driver/ToolChains/MinGW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
else
CmdArgs.push_back("arm64pe");
break;
case llvm::Triple::mipsel:
CmdArgs.push_back("mipspe");
break;
default:
D.Diag(diag::err_target_unknown_triple) << TC.getEffectiveTriple().str();
}
Expand Down
1 change: 1 addition & 0 deletions clang/test/CodeGen/pragma-comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// RUN: %clang_cc1 %s -triple x86_64-scei-ps4 -fms-extensions -emit-llvm -o - | FileCheck -check-prefix ELF %s --implicit-check-not llvm.linker.options
// RUN: %clang_cc1 %s -triple x86_64-sie-ps5 -fms-extensions -emit-llvm -o - | FileCheck -check-prefix ELF %s --implicit-check-not llvm.linker.options
// RUN: %clang_cc1 %s -triple aarch64-windows-msvc -fms-extensions -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 %s -triple mipsel-windows-msvc -fms-extensions -emit-llvm -o - | FileCheck %s

#pragma comment(lib, "msvcrt.lib")
#pragma comment(lib, "kernel32")
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Preprocessor/predefined-win-macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@
// CHECK-ARM64EC-WIN: #define _WIN32 1
// CHECK-ARM64EC-WIN: #define _WIN64 1

// RUN: %clang_cc1 -triple mipsel-windows %s -E -dM -o - \
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-MIPSEL-WIN

// CHECK-MIPSEL-WIN: #define _M_MRX000 4000
// CHECK-MIPSEL-WIN: #define _WIN32 1
// CHECK-MIPSEL-WIN-NOT: #define _MIPS_ 1

// RUN: %clang_cc1 -triple i686-windows-gnu %s -E -dM -o - \
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-X86-MINGW

Expand Down Expand Up @@ -168,3 +175,12 @@
// CHECK-ARM64EC-MINGW: #define __arm64ec__ 1
// CHECK-ARM64EC-MINGW: #define __x86_64 1
// CHECK-ARM64EC-MINGW: #define __x86_64__ 1

// RUN: %clang_cc1 -triple mipsel-windows-gnu %s -E -dM -o - \
// RUN: | FileCheck -match-full-lines %s --check-prefix=CHECK-MIPSEL-MINGW

// CHECK-MIPSEL-MINGW-NOT: #define _M_MRX000 4000
// CHECK-MIPSEL-MINGW: #define _MIPS_ 1
// CHECK-MIPSEL-MINGW: #define _WIN32 1
// CHECK-MIPSEL-MINGW: #define __mips 32
// CHECK-MIPSEL-MINGW: #define __mips__ 1
1 change: 1 addition & 0 deletions lld/COFF/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static const auto ARM64EC = llvm::COFF::IMAGE_FILE_MACHINE_ARM64EC;
static const auto ARM64X = llvm::COFF::IMAGE_FILE_MACHINE_ARM64X;
static const auto ARMNT = llvm::COFF::IMAGE_FILE_MACHINE_ARMNT;
static const auto I386 = llvm::COFF::IMAGE_FILE_MACHINE_I386;
static const auto MIPS = llvm::COFF::IMAGE_FILE_MACHINE_R4000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part (adding a yet-unused shorthand in lld/COFF/Config.h) of this commit (primarily llvm/lib/Object stuff) seems unrelated and should be moved to a later commit.


enum class ExportSource {
Unset,
Expand Down
18 changes: 18 additions & 0 deletions llvm/include/llvm/BinaryFormat/COFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,24 @@ enum RelocationTypesARM64 : unsigned {
IMAGE_REL_ARM64_REL32 = 0x0011,
};

enum RelocationTypesMips : unsigned {
IMAGE_REL_MIPS_ABSOLUTE = 0x0000,
IMAGE_REL_MIPS_REFHALF = 0x0001,
IMAGE_REL_MIPS_REFWORD = 0x0002,
IMAGE_REL_MIPS_JMPADDR = 0x0003,
IMAGE_REL_MIPS_REFHI = 0x0004,
IMAGE_REL_MIPS_REFLO = 0x0005,
IMAGE_REL_MIPS_GPREL = 0x0006,
IMAGE_REL_MIPS_LITERAL = 0x0007,
IMAGE_REL_MIPS_SECTION = 0x000A,
IMAGE_REL_MIPS_SECREL = 0x000B,
IMAGE_REL_MIPS_SECRELLO = 0x000C,
IMAGE_REL_MIPS_SECRELHI = 0x000D,
IMAGE_REL_MIPS_JMPADDR16 = 0x0010,
IMAGE_REL_MIPS_REFWORDNB = 0x0022,
IMAGE_REL_MIPS_PAIR = 0x0025,
};

enum DynamicRelocationType : unsigned {
IMAGE_DYNAMIC_RELOCATION_GUARD_RF_PROLOGUE = 1,
IMAGE_DYNAMIC_RELOCATION_GUARD_RF_EPILOGUE = 2,
Expand Down
88 changes: 87 additions & 1 deletion llvm/include/llvm/DebugInfo/CodeView/CodeViewRegisters.def
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#if !defined(CV_REGISTERS_ALL) && !defined(CV_REGISTERS_X86) && \
!defined(CV_REGISTERS_ARM) && \
!defined(CV_REGISTERS_ARM64)
!defined(CV_REGISTERS_ARM64) && \
!defined(CV_REGISTERS_MIPS)
#error Need include at least one register set.
#endif

Expand Down Expand Up @@ -793,3 +794,88 @@ CV_REGISTER(ARM64_H31, 301)
#pragma pop_macro("ARM64_FPCR")

#endif // defined(CV_REGISTERS_ALL) || defined(CV_REGISTERS_ARM64)

#if defined(CV_REGISTERS_ALL) || defined(CV_REGISTERS_MIPS)

// MIPS registers
CV_REGISTER(MIPS_NOREG, 0)

// General purpose integer registers

CV_REGISTER(MIPS_ZERO, 10)
CV_REGISTER(MIPS_AT, 11)
CV_REGISTER(MIPS_V0, 12)
CV_REGISTER(MIPS_V1, 13)
CV_REGISTER(MIPS_A0, 14)
CV_REGISTER(MIPS_A1, 15)
CV_REGISTER(MIPS_A2, 16)
CV_REGISTER(MIPS_A3, 17)
CV_REGISTER(MIPS_T0, 18)
CV_REGISTER(MIPS_T1, 19)
CV_REGISTER(MIPS_T2, 20)
CV_REGISTER(MIPS_T3, 21)
CV_REGISTER(MIPS_T4, 22)
CV_REGISTER(MIPS_T5, 23)
CV_REGISTER(MIPS_T6, 24)
CV_REGISTER(MIPS_T7, 25)
CV_REGISTER(MIPS_S0, 26)
CV_REGISTER(MIPS_S1, 27)
CV_REGISTER(MIPS_S2, 28)
CV_REGISTER(MIPS_S3, 29)
CV_REGISTER(MIPS_S4, 30)
CV_REGISTER(MIPS_S5, 31)
CV_REGISTER(MIPS_S6, 32)
CV_REGISTER(MIPS_S7, 33)
CV_REGISTER(MIPS_T8, 34)
CV_REGISTER(MIPS_T9, 35)
CV_REGISTER(MIPS_K0, 36)
CV_REGISTER(MIPS_K1, 37)
CV_REGISTER(MIPS_GP, 38)
CV_REGISTER(MIPS_SP, 39)
CV_REGISTER(MIPS_S8, 40)
CV_REGISTER(MIPS_RA, 41)
CV_REGISTER(MIPS_LO, 42)
CV_REGISTER(MIPS_HI, 43)

// Status registers

CV_REGISTER(MIPS_Fir, 50)
CV_REGISTER(MIPS_Psr, 51)

// Floating-point registers

CV_REGISTER(MIPS_F0, 60)
CV_REGISTER(MIPS_F1, 61)
CV_REGISTER(MIPS_F2, 62)
CV_REGISTER(MIPS_F3, 63)
CV_REGISTER(MIPS_F4, 64)
CV_REGISTER(MIPS_F5, 65)
CV_REGISTER(MIPS_F6, 66)
CV_REGISTER(MIPS_F7, 67)
CV_REGISTER(MIPS_F8, 68)
CV_REGISTER(MIPS_F9, 69)
CV_REGISTER(MIPS_F10, 70)
CV_REGISTER(MIPS_F11, 71)
CV_REGISTER(MIPS_F12, 72)
CV_REGISTER(MIPS_F13, 73)
CV_REGISTER(MIPS_F14, 74)
CV_REGISTER(MIPS_F15, 75)
CV_REGISTER(MIPS_F16, 76)
CV_REGISTER(MIPS_F17, 77)
CV_REGISTER(MIPS_F18, 78)
CV_REGISTER(MIPS_F19, 79)
CV_REGISTER(MIPS_F20, 80)
CV_REGISTER(MIPS_F21, 81)
CV_REGISTER(MIPS_F22, 82)
CV_REGISTER(MIPS_F23, 83)
CV_REGISTER(MIPS_F24, 84)
CV_REGISTER(MIPS_F25, 85)
CV_REGISTER(MIPS_F26, 86)
CV_REGISTER(MIPS_F27, 87)
CV_REGISTER(MIPS_F28, 88)
CV_REGISTER(MIPS_F29, 89)
CV_REGISTER(MIPS_F30, 90)
CV_REGISTER(MIPS_F31, 91)
CV_REGISTER(MIPS_Fsr, 92)

#endif // defined(CV_REGISTERS_ALL) || defined(CV_REGISTERS_MIPS)
2 changes: 2 additions & 0 deletions llvm/include/llvm/Object/WindowsMachineFlag.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ template <typename T> Triple::ArchType getMachineArchType(T machine) {
case COFF::IMAGE_FILE_MACHINE_ARM64EC:
case COFF::IMAGE_FILE_MACHINE_ARM64X:
return llvm::Triple::ArchType::aarch64;
case COFF::IMAGE_FILE_MACHINE_R4000:
return llvm::Triple::ArchType::mipsel;
default:
return llvm::Triple::ArchType::UnknownArch;
}
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/ObjectYAML/COFFYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ struct ScalarEnumerationTraits<COFF::RelocationTypeAMD64> {
static void enumeration(IO &IO, COFF::RelocationTypeAMD64 &Value);
};

template <>
struct ScalarEnumerationTraits<COFF::RelocationTypesMips> {
static void enumeration(IO &IO, COFF::RelocationTypesMips &Value);
};

template <>
struct ScalarEnumerationTraits<COFF::RelocationTypesARM> {
static void enumeration(IO &IO, COFF::RelocationTypesARM &Value);
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ static CPUType mapArchToCVCPUType(Triple::ArchType Type) {
return CPUType::ARMNT;
case Triple::ArchType::aarch64:
return CPUType::ARM64;
case Triple::ArchType::mipsel:
return CPUType::MIPS;
default:
report_fatal_error("target architecture doesn't map to a CodeView CPUType");
}
Expand Down
Loading
Loading