Skip to content

Commit 2981926

Browse files
committed
add support for building with llvm20
1 parent 22f65d6 commit 2981926

File tree

12 files changed

+652
-276
lines changed

12 files changed

+652
-276
lines changed

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ find_package(gflags CONFIG REQUIRED)
5050
find_package(glog CONFIG REQUIRED)
5151
find_package(Z3 4.8 CONFIG REQUIRED)
5252
find_package(doctest CONFIG REQUIRED)
53-
find_package(LLVM CONFIG REQUIRED)
53+
find_package(LLVM 20 CONFIG REQUIRED)
54+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
5455
llvm_map_components_to_libnames(llvm_libs support core irreader bitreader bitwriter)
55-
find_package(Clang CONFIG REQUIRED)
56+
57+
find_package(MLIR 20 CONFIG REQUIRED)
58+
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
59+
60+
find_package(Clang 20 CONFIG REQUIRED)
61+
message(STATUS "Using ClangConfig.cmake in: ${Clang_DIR}")
5662

5763

5864
#

lib/AST/ASTBuilder.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ clang::IntegerLiteral *ASTBuilder::CreateIntLit(llvm::APSInt val) {
147147
// Extend the literal value based on it's sign if we have a
148148
// mismatch between the bit width of the value and inferred type.
149149
auto type_size{ctx.getIntWidth(type)};
150-
if (val.getBitWidth() != type_size && val.getMinSignedBits() < type_size) {
150+
if (val.getBitWidth() != type_size && val.getSignificantBits() < type_size) {
151151
val = val.extOrTrunc(type_size);
152152
}
153153
// Clang does this check in the `clang::IntegerLiteral::Create`, but
@@ -159,21 +159,21 @@ clang::IntegerLiteral *ASTBuilder::CreateIntLit(llvm::APSInt val) {
159159

160160
clang::CharacterLiteral *ASTBuilder::CreateCharLit(llvm::APInt val) {
161161
CHECK(val.getBitWidth() == 8U);
162-
return new (ctx) clang::CharacterLiteral(
163-
val.getLimitedValue(), clang::CharacterLiteral::CharacterKind::Ascii,
164-
ctx.IntTy, clang::SourceLocation());
162+
return new (ctx) clang::CharacterLiteral(val.getLimitedValue(),
163+
clang::CharacterLiteralKind::Ascii,
164+
ctx.IntTy, clang::SourceLocation());
165165
}
166166

167167
clang::CharacterLiteral *ASTBuilder::CreateCharLit(unsigned val) {
168-
return new (ctx) clang::CharacterLiteral(
169-
val, clang::CharacterLiteral::CharacterKind::Ascii, ctx.IntTy,
170-
clang::SourceLocation());
168+
return new (ctx)
169+
clang::CharacterLiteral(val, clang::CharacterLiteralKind::Ascii,
170+
ctx.IntTy, clang::SourceLocation());
171171
}
172172

173173
clang::StringLiteral *ASTBuilder::CreateStrLit(std::string val) {
174174
auto type{ctx.getStringLiteralArrayType(ctx.CharTy, val.size())};
175175
return clang::StringLiteral::Create(
176-
ctx, val, clang::StringLiteral::StringKind::Ordinary,
176+
ctx, val, clang::StringLiteralKind::Ordinary,
177177
/*Pascal=*/false, type, clang::SourceLocation());
178178
}
179179

@@ -199,13 +199,13 @@ clang::Expr *ASTBuilder::CreateFPLit(llvm::APFloat val) {
199199

200200
clang::Expr *ASTBuilder::CreateNull() {
201201
auto type{ctx.UnsignedIntTy};
202-
auto val{llvm::APInt::getNullValue(ctx.getTypeSize(type))};
202+
auto val{llvm::APInt::getZero(ctx.getTypeSize(type))};
203203
auto lit{CreateIntLit(val)};
204204
return CreateCStyleCast(ctx.VoidPtrTy, lit);
205205
}
206206

207207
clang::Expr *ASTBuilder::CreateUndefInteger(clang::QualType type) {
208-
auto val{llvm::APInt::getNullValue(ctx.getTypeSize(type))};
208+
auto val{llvm::APInt::getZero(ctx.getTypeSize(type))};
209209
auto lit{CreateIntLit(val)};
210210
return lit;
211211
}
@@ -253,15 +253,15 @@ clang::ParmVarDecl *ASTBuilder::CreateParamDecl(clang::DeclContext *decl_ctx,
253253
clang::RecordDecl *ASTBuilder::CreateStructDecl(clang::DeclContext *decl_ctx,
254254
clang::IdentifierInfo *id,
255255
clang::RecordDecl *prev_decl) {
256-
return clang::RecordDecl::Create(ctx, clang::TagTypeKind::TTK_Struct,
257-
decl_ctx, clang::SourceLocation(),
256+
return clang::RecordDecl::Create(ctx, clang::TagTypeKind::Struct, decl_ctx,
257+
clang::SourceLocation(),
258258
clang::SourceLocation(), id, prev_decl);
259259
}
260260

261261
clang::RecordDecl *ASTBuilder::CreateUnionDecl(clang::DeclContext *decl_ctx,
262262
clang::IdentifierInfo *id,
263263
clang::RecordDecl *prev_decl) {
264-
return clang::RecordDecl::Create(ctx, clang::TagTypeKind::TTK_Union, decl_ctx,
264+
return clang::RecordDecl::Create(ctx, clang::TagTypeKind::Union, decl_ctx,
265265
clang::SourceLocation(),
266266
clang::SourceLocation(), id, prev_decl);
267267
}

lib/AST/CXXToCDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static std::string GetMangledName(clang::NamedDecl *decl) {
2525
llvm::raw_string_ostream os(buffer);
2626
if (auto type_decl = clang::dyn_cast<clang::TypeDecl>(decl)) {
2727
auto type = clang::QualType(type_decl->getTypeForDecl(), 0);
28-
mangler->mangleTypeName(type, os);
28+
mangler->mangleCanonicalTypeName(type, os);
2929
} else if (auto cst = clang::dyn_cast<clang::CXXConstructorDecl>(decl)) {
3030
mangler->mangleName(clang::GlobalDecl(cst), os);
3131
} else if (auto dst = clang::dyn_cast<clang::CXXDestructorDecl>(decl)) {

lib/AST/StructGenerator.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static FieldInfo CreatePadding(clang::ASTContext& ast_ctx,
119119
auto padding_count{needed_padding / type_size};
120120
auto padding_arr_type{ast_ctx.getConstantArrayType(
121121
padding_type, llvm::APInt(64, padding_count), nullptr,
122-
clang::ArrayType::ArraySizeModifier::Normal, 0)};
122+
clang::ArraySizeModifier::Normal, 0)};
123123
return {name, padding_arr_type, 0};
124124
}
125125
}
@@ -146,7 +146,8 @@ static unsigned GetStructSize(clang::ASTContext& ast_ctx, ASTBuilder& ast,
146146

147147
auto tudecl{ast_ctx.getTranslationUnitDecl()};
148148
auto decl{ast.CreateStructDecl(tudecl, "temp" + std::to_string(count++))};
149-
clang::AttributeCommonInfo info{clang::SourceLocation{}};
149+
clang::AttributeCommonInfo info{nullptr, clang::SourceLocation{},
150+
clang::AttributeCommonInfo::Form::GNU()};
150151
decl->addAttr(clang::PackedAttr::Create(ast_ctx, info));
151152
for (auto& field : fields) {
152153
decl->addDecl(FieldInfoToFieldDecl(ast_ctx, ast, decl, field));
@@ -217,7 +218,9 @@ void StructGenerator::VisitFields(clang::RecordDecl* decl,
217218
auto field_count{0U};
218219
std::vector<FieldInfo> fields{};
219220
if (!isUnion) {
220-
clang::AttributeCommonInfo attrinfo{clang::SourceLocation{}};
221+
clang::AttributeCommonInfo attrinfo{
222+
nullptr, clang::SourceLocation{},
223+
clang::AttributeCommonInfo::Form::GNU()};
221224
decl->addAttr(clang::PackedAttr::Create(ast_ctx, attrinfo));
222225
}
223226

@@ -333,10 +336,10 @@ clang::QualType StructGenerator::BuildArray(llvm::DICompositeType* a) {
333336
VLOG(1) << "BuildArray: " << rellic::LLVMThingToString(a);
334337
auto base{BuildType(a->getBaseType())};
335338
auto subrange{llvm::cast<llvm::DISubrange>(a->getElements()[0])};
336-
auto* ci = subrange->getCount().get<llvm::ConstantInt*>();
337-
return ast_ctx.getConstantArrayType(
338-
base, llvm::APInt(64, ci->getZExtValue()), nullptr,
339-
clang::ArrayType::ArraySizeModifier::Normal, 0);
339+
auto* ci = llvm::dyn_cast<llvm::ConstantInt*>(subrange->getCount());
340+
return ast_ctx.getConstantArrayType(base, llvm::APInt(64, ci->getZExtValue()),
341+
nullptr, clang::ArraySizeModifier::Normal,
342+
0);
340343
}
341344

342345
clang::QualType StructGenerator::BuildDerived(llvm::DIDerivedType* d,
@@ -608,7 +611,7 @@ std::vector<clang::Expr*> StructGenerator::GetAccessor(clang::Expr* base,
608611
auto idx{field->getFieldIndex()};
609612
auto type{field->getType().getDesugaredType(ast_ctx)};
610613
auto field_offset{layout.getFieldOffset(idx)};
611-
auto field_size{field->isBitField() ? field->getBitWidthValue(ast_ctx)
614+
auto field_size{field->isBitField() ? field->getBitWidthValue()
612615
: ast_ctx.getTypeSize(type)};
613616
if (offset >= field_offset &&
614617
offset + length <= field_offset + field_size) {

lib/AST/Util.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,11 @@ clang::QualType DecompilationContext::GetQualType(llvm::Type *type) {
462462

463463
case llvm::Type::PointerTyID: {
464464
auto ptr_type{llvm::cast<llvm::PointerType>(type)};
465-
if (ptr_type->isOpaque()) {
465+
auto pointee_type = ptr_type->getContainedType(0);
466+
if (!pointee_type || pointee_type->isVoidTy()) {
466467
result = ast_ctx.VoidPtrTy;
467468
} else {
468-
result = ast_ctx.getPointerType(
469-
GetQualType(ptr_type->getNonOpaquePointerElementType()));
469+
result = ast_ctx.getPointerType(GetQualType(pointee_type));
470470
}
471471
} break;
472472

@@ -475,7 +475,7 @@ clang::QualType DecompilationContext::GetQualType(llvm::Type *type) {
475475
auto elm{GetQualType(arr->getElementType())};
476476
result = ast_ctx.getConstantArrayType(
477477
elm, llvm::APInt(64, arr->getNumElements()), nullptr,
478-
clang::ArrayType::ArraySizeModifier::Normal, 0);
478+
clang::ArraySizeModifier::Normal, 0);
479479
} break;
480480

481481
case llvm::Type::StructTyID: {
@@ -521,7 +521,7 @@ clang::QualType DecompilationContext::GetQualType(llvm::Type *type) {
521521
auto vtype{llvm::cast<llvm::FixedVectorType>(type)};
522522
auto etype{GetQualType(vtype->getElementType())};
523523
auto ecnt{vtype->getNumElements()};
524-
auto vkind{clang::VectorType::GenericVector};
524+
auto vkind{clang::VectorKind::Generic};
525525
result = ast_ctx.getVectorType(etype, ecnt, vkind);
526526
} else {
527527
THROW() << "Unknown LLVM Type: " << LLVMThingToString(type);

lib/BC/Util.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,14 @@ static llvm::LoadInst *ConvertInsertValue(llvm::InsertValueInst *I) {
258258

259259
auto F{I->getParent()->getParent()};
260260
auto alloca{new llvm::AllocaInst(I->getType(), DL.getAllocaAddrSpace(),
261-
nullptr, I->getName() + ".iv2mem", I)};
261+
nullptr, I->getName() + ".iv2mem",
262+
I->getIterator())};
262263
auto aggr_opnd{I->getAggregateOperand()};
263264
auto aggr_ty{aggr_opnd->getType()};
264265
auto ins_opnd{I->getInsertedValueOperand()};
265266

266267
if (!llvm::isa<llvm::UndefValue>(aggr_opnd)) {
267-
new llvm::StoreInst(aggr_opnd, alloca, I);
268+
new llvm::StoreInst(aggr_opnd, alloca, I->getIterator());
268269
}
269270
std::vector<llvm::Value *> indices;
270271
indices.push_back(llvm::ConstantInt::get(ctx, llvm::APInt(64, 0, false)));
@@ -273,10 +274,10 @@ static llvm::LoadInst *ConvertInsertValue(llvm::InsertValueInst *I) {
273274
llvm::ConstantInt::get(ctx, llvm::APInt(sizeof(i) * 8, i)));
274275
}
275276
auto ptr{llvm::GetElementPtrInst::Create(aggr_opnd->getType(), alloca,
276-
indices, "", I)};
277-
new llvm::StoreInst(ins_opnd, ptr, I);
278-
auto load{
279-
new llvm::LoadInst(I->getType(), alloca, I->getName() + ".reload", I)};
277+
indices, "", I->getIterator())};
278+
new llvm::StoreInst(ins_opnd, ptr, I->getIterator());
279+
auto load{new llvm::LoadInst(I->getType(), alloca, I->getName() + ".reload",
280+
I->getIterator())};
280281

281282
I->replaceAllUsesWith(load);
282283
I->eraseFromParent();
@@ -359,9 +360,9 @@ void ConvertArrayArguments(llvm::Module &m) {
359360
if (orig_func->getReturnType()->isArrayTy()) {
360361
auto undef{llvm::UndefValue::get(return_ty)};
361362
for (auto ret : Returns) {
362-
auto wrap{llvm::InsertValueInst::Create(undef, ret->getReturnValue(),
363-
indices, "", ret)};
364-
auto new_ret{llvm::ReturnInst::Create(ctx, wrap, ret)};
363+
auto wrap{llvm::InsertValueInst::Create(
364+
undef, ret->getReturnValue(), indices, "", ret->getIterator())};
365+
auto new_ret{llvm::ReturnInst::Create(ctx, wrap, ret->getIterator())};
365366
ret->eraseFromParent();
366367
}
367368
}
@@ -397,8 +398,8 @@ void ConvertArrayArguments(llvm::Module &m) {
397398
for (auto &old_arg : call->args()) {
398399
if (old_arg->getType()->isArrayTy()) {
399400
auto undef{llvm::UndefValue::get(conv_types[old_arg->getType()])};
400-
auto new_arg{llvm::InsertValueInst::Create(undef, old_arg, indices,
401-
"", call)};
401+
auto new_arg{llvm::InsertValueInst::Create(
402+
undef, old_arg, indices, "", call->getIterator())};
402403
args.push_back(new_arg);
403404
} else {
404405
args.push_back(old_arg);
@@ -407,12 +408,12 @@ void ConvertArrayArguments(llvm::Module &m) {
407408
llvm::SmallVector<std::pair<unsigned, llvm::MDNode *>, 16u> mds;
408409
auto new_call{llvm::CallInst::Create(new_func->getFunctionType(),
409410
new_func, args, call->getName(),
410-
call)};
411+
call->getIterator())};
411412
call->getAllMetadata(mds);
412413
CloneMetadataInto(new_call, mds);
413414
if (callee->getReturnType()->isArrayTy()) {
414-
auto unwrap{
415-
llvm::ExtractValueInst::Create(new_call, indices, "", call)};
415+
auto unwrap{llvm::ExtractValueInst::Create(new_call, indices, "",
416+
call->getIterator())};
416417
call->replaceAllUsesWith(unwrap);
417418
} else {
418419
call->replaceAllUsesWith(new_call);

tools/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ target_link_libraries(${RELLIC_DECOMP}
2020
PRIVATE
2121
"${PROJECT_NAME}_cxx_settings"
2222
"${PROJECT_NAME}"
23-
gflags::gflags
23+
gflags
2424
)
2525

2626
set(RELLIC_DECOMP "${RELLIC_DECOMP}" PARENT_SCOPE)
@@ -39,7 +39,7 @@ target_link_libraries(${RELLIC_HEADERGEN}
3939
PRIVATE
4040
"${PROJECT_NAME}_cxx_settings"
4141
"${PROJECT_NAME}"
42-
gflags::gflags
42+
gflags
4343
)
4444

4545
set(RELLIC_HEADERGEN "${RELLIC_HEADERGEN}" PARENT_SCOPE)
@@ -57,14 +57,22 @@ add_executable(${RELLIC_XREF}
5757
"xref/Xref.cpp"
5858
)
5959

60+
include(FetchContent)
61+
FetchContent_Declare(cpp-httplib
62+
GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git
63+
GIT_TAG v0.20.0
64+
)
65+
FetchContent_MakeAvailable(cpp-httplib)
66+
set(CPP_HTTPLIB_INCLUDE_DIRS "${cpp-httplib_SOURCE_DIR}")
67+
6068
find_path(CPP_HTTPLIB_INCLUDE_DIRS "httplib.h")
6169
target_include_directories(${RELLIC_XREF} PRIVATE ${CPP_HTTPLIB_INCLUDE_DIRS})
6270

6371
target_link_libraries(${RELLIC_XREF}
6472
PRIVATE
6573
"${PROJECT_NAME}_cxx_settings"
6674
"${PROJECT_NAME}"
67-
gflags::gflags
75+
gflags
6876
)
6977

7078
set(RELLIC_XREF "${RELLIC_XREF}" PARENT_SCOPE)
@@ -98,7 +106,7 @@ target_link_libraries(${RELLIC_REPL}
98106
PRIVATE
99107
"${PROJECT_NAME}_cxx_settings"
100108
"${PROJECT_NAME}"
101-
gflags::gflags
109+
gflags
102110
linenoise
103111
)
104112

@@ -117,7 +125,7 @@ target_link_libraries(${RELLIC_DEC2HEX}
117125
PRIVATE
118126
"${PROJECT_NAME}_cxx_settings"
119127
"${PROJECT_NAME}"
120-
gflags::gflags
128+
gflags
121129
)
122130

123131
set(RELLIC_DEC2HEX "${RELLIC_DEC2HEX}" PARENT_SCOPE)

tools/decomp/Decomp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ DEFINE_bool(lower_switch, false,
3434
DECLARE_bool(version);
3535

3636
namespace {
37-
static llvm::Optional<llvm::APInt> GetPCMetadata(llvm::Value* value) {
37+
static std::optional<llvm::APInt> GetPCMetadata(llvm::Value* value) {
3838
auto inst{llvm::dyn_cast<llvm::Instruction>(value)};
3939
if (!inst) {
40-
return llvm::Optional<llvm::APInt>();
40+
return std::nullopt;
4141
}
4242

4343
auto pc{inst->getMetadata("pc")};
4444
if (!pc) {
45-
return llvm::Optional<llvm::APInt>();
45+
return std::nullopt;
4646
}
4747

4848
auto& cop{pc->getOperand(0U)};

0 commit comments

Comments
 (0)