Skip to content

Commit ff09a2f

Browse files
committed
[Z80] Backend improvements and bug fixes for v17 compatibility
- Reintroduce Z80ABIInfo from v15to strip signext/zeroext from args - Fix G_SEXT legalization: use maxScalar instead of lower (prevents s64 to s128 crash) - Add G_SMULO legalization via __mulosi4/__mulodi4 libcalls - Add G_CTTZ legalization for __builtin_ctz support - Fix G_MEMMOVE: use LDR pseudo to avoid invalid sbc hl, iy - Add wide register tuples (32/48/64bit) for fixed-point support - Fix -Oz frameset by skipping frame register spill when __frameset handles it - Reject F8 COPY fold to prevent InlineSpiller assertion - Add dead OR A,A elimination after SBC A,A - Fix WMIC deprecation in Windows CI Based-on: bd5a60c6 Based-on: e1486dc4
1 parent 9be530c commit ff09a2f

32 files changed

Lines changed: 16023 additions & 20999 deletions

.github/workflows/cmake-windows.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191
path: ez80-lto2${{env.EXE}}
9292

9393
- name: Disk Usage
94-
run: wmic logicaldisk get size,freespace,caption
94+
run: Get-CimInstance Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size
9595

9696
build-clang:
9797
needs: [build-llvm]
@@ -169,7 +169,7 @@ jobs:
169169
path: ez80-clang${{env.EXE}}
170170

171171
- name: Disk Usage
172-
run: wmic logicaldisk get size,freespace,caption
172+
run: Get-CimInstance Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size
173173

174174
nightly:
175175
runs-on: ubuntu-latest

clang/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ add_clang_library(clangCodeGen
114114
Targets/WebAssembly.cpp
115115
Targets/X86.cpp
116116
Targets/XCore.cpp
117+
Targets/Z80.cpp
117118
VarBypassDetector.cpp
118119

119120
DEPENDS

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ createTargetCodeGenInfo(CodeGenModule &CGM) {
314314
return createLoongArchTargetCodeGenInfo(
315315
CGM, Target.getPointerWidth(LangAS::Default), ABIFRLen);
316316
}
317+
case llvm::Triple::z80:
318+
case llvm::Triple::ez80:
319+
return createZ80TargetCodeGenInfo(CGM);
317320
}
318321
}
319322

clang/lib/CodeGen/TargetInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ createMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32);
468468
std::unique_ptr<TargetCodeGenInfo>
469469
createMSP430TargetCodeGenInfo(CodeGenModule &CGM);
470470

471+
std::unique_ptr<TargetCodeGenInfo>
472+
createZ80TargetCodeGenInfo(CodeGenModule &CGM);
473+
471474
std::unique_ptr<TargetCodeGenInfo>
472475
createNVPTXTargetCodeGenInfo(CodeGenModule &CGM);
473476

clang/lib/CodeGen/Targets/Z80.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//===- Z80.cpp - Z80 ABI Implementation -----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "ABIInfoImpl.h"
10+
#include "TargetInfo.h"
11+
#include "clang/AST/DeclCXX.h"
12+
13+
using namespace clang;
14+
using namespace clang::CodeGen;
15+
16+
//===----------------------------------------------------------------------===//
17+
// Z80 ABI Implementation
18+
//===----------------------------------------------------------------------===//
19+
20+
namespace {
21+
22+
class Z80ABIInfo : public DefaultABIInfo {
23+
public:
24+
Z80ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
25+
26+
private:
27+
void removeExtend(ABIArgInfo &AI) const {
28+
if (AI.isExtend()) {
29+
bool InReg = AI.getInReg();
30+
AI = ABIArgInfo::getDirect(AI.getCoerceToType());
31+
AI.setInReg(InReg);
32+
}
33+
}
34+
35+
void computeInfo(CGFunctionInfo &FI) const override {
36+
if (!getCXXABI().classifyReturnType(FI))
37+
FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
38+
removeExtend(FI.getReturnInfo());
39+
for (auto &Arg : FI.arguments())
40+
removeExtend(Arg.info = classifyArgumentType(Arg.type));
41+
}
42+
43+
Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
44+
QualType Ty) const override;
45+
};
46+
47+
class Z80TargetCodeGenInfo : public TargetCodeGenInfo {
48+
public:
49+
Z80TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
50+
: TargetCodeGenInfo(std::make_unique<Z80ABIInfo>(CGT)) {}
51+
52+
void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
53+
CodeGen::CodeGenModule &CGM) const override;
54+
};
55+
56+
} // namespace
57+
58+
Address Z80ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
59+
QualType Ty) const {
60+
ABIArgInfo AI = classifyArgumentType(Ty);
61+
removeExtend(AI);
62+
return EmitVAArgInstr(CGF, VAListAddr, Ty, AI);
63+
}
64+
65+
void Z80TargetCodeGenInfo::setTargetAttributes(const Decl *D,
66+
llvm::GlobalValue *GV,
67+
CodeGen::CodeGenModule &CGM) const {
68+
const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
69+
if (!FD)
70+
return;
71+
llvm::Function *Fn = cast<llvm::Function>(GV);
72+
73+
if (Fn->isDeclaration()) {
74+
if (FD->getAttr<AnyZ80TIFlagsAttr>())
75+
Fn->setCallingConv(llvm::CallingConv::Z80_TIFlags);
76+
return;
77+
}
78+
79+
const AnyZ80InterruptAttr *Attr = FD->getAttr<AnyZ80InterruptAttr>();
80+
if (!Attr)
81+
return;
82+
83+
const char *Kind;
84+
switch (Attr->getInterrupt()) {
85+
case AnyZ80InterruptAttr::Generic:
86+
Kind = "Generic";
87+
break;
88+
case AnyZ80InterruptAttr::Nested:
89+
Kind = "Nested";
90+
break;
91+
case AnyZ80InterruptAttr::NMI:
92+
Kind = "NMI";
93+
break;
94+
}
95+
96+
Fn->setCallingConv(llvm::CallingConv::PreserveAll);
97+
Fn->addFnAttr("interrupt", Kind);
98+
}
99+
100+
std::unique_ptr<TargetCodeGenInfo>
101+
CodeGen::createZ80TargetCodeGenInfo(CodeGenModule &CGM) {
102+
return std::make_unique<Z80TargetCodeGenInfo>(CGM.getTypes());
103+
}

llvm/include/llvm/IR/RuntimeLibcalls.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ HANDLE_LIBCALL(CTLZ_I32, "__clzsi2")
176176
HANDLE_LIBCALL(CTLZ_I48, nullptr)
177177
HANDLE_LIBCALL(CTLZ_I64, "__clzdi2")
178178
HANDLE_LIBCALL(CTLZ_I128, "__clzti2")
179+
HANDLE_LIBCALL(CTTZ_I8, nullptr)
180+
HANDLE_LIBCALL(CTTZ_I16, nullptr)
181+
HANDLE_LIBCALL(CTTZ_I24, nullptr)
182+
HANDLE_LIBCALL(CTTZ_I32, nullptr)
183+
HANDLE_LIBCALL(CTTZ_I48, nullptr)
184+
HANDLE_LIBCALL(CTTZ_I64, nullptr)
185+
HANDLE_LIBCALL(CTTZ_I128, nullptr)
179186
HANDLE_LIBCALL(POPCNT_I8, nullptr)
180187
HANDLE_LIBCALL(POPCNT_I16, nullptr)
181188
HANDLE_LIBCALL(POPCNT_I24, nullptr)

0 commit comments

Comments
 (0)