Skip to content

Commit

Permalink
fix typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
2over12 committed Dec 5, 2023
1 parent a54f51b commit 69e6732
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/anvill/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Metadata.h>

#include <memory>
Expand Down Expand Up @@ -41,6 +42,10 @@ class Arch;
} // namespace remill
namespace anvill {

llvm::StructType *getOrCreateNamedStruct(llvm::LLVMContext &context,
llvm::StringRef Name);


struct TypeSpecificationError final {
enum class ErrorCode {
InvalidSpecFormat,
Expand Down
7 changes: 6 additions & 1 deletion lib/Protobuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ ProtobufTranslator::DecodeType(const ::specification::TypeSpec &obj) const {
if (this->type_names.count(obj.alias())) {
TypeSpec res = TypeName(type_names.at(obj.alias()));
return res;
} else if (this->type_map.count(obj.alias())) {
TypeSpec tspec = this->type_map.at(obj.alias());
return tspec;
} else {
LOG(ERROR) << "Unknown alias id " << obj.alias();
return {BaseType::Void};
Expand Down Expand Up @@ -811,6 +814,7 @@ anvill::Result<TypeSpec, std::string> ProtobufTranslator::DecodeType(
const std::unordered_map<std::int64_t, std::string> &named_types) {
if (obj.has_alias()) {
auto alias = obj.alias();

if (named_types.contains(alias)) {
TypeSpec tname = TypeName(named_types.at(alias));
return tname;
Expand Down Expand Up @@ -928,7 +932,8 @@ Result<std::monostate, std::string> ProtobufTranslator::DecodeTypeMap(


std::string name = names.at(k);
llvm::StructType::create(this->context, sty->elements(), name);
auto res = getOrCreateNamedStruct(this->context, name);
res->setBody(sty->elements());
}
type_names[k] = names.at(k);
} else {
Expand Down
13 changes: 12 additions & 1 deletion lib/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,14 +766,25 @@ TypeTranslator::DecodeFromSpec(TypeSpec spec) const {

if (std::holds_alternative<TypeName>(spec)) {
auto nm = std::get<TypeName>(spec);
auto sty = llvm::StructType::getTypeByName(this->impl->context, nm.name);
auto sty = getOrCreateNamedStruct(this->impl->context, nm.name);
CHECK(sty);
return sty;
}

return TypeSpecificationError{TypeSpecificationError::ErrorCode::InvalidState,
"Unhandled type specification variant"};
}

llvm::StructType *getOrCreateNamedStruct(llvm::LLVMContext &context,
llvm::StringRef Name) {
auto res = llvm::StructType::getTypeByName(context, Name);
if (res) {
return res;
}

return llvm::StructType::create(context, Name);
}

namespace {

template <unsigned kSize>
Expand Down

0 comments on commit 69e6732

Please sign in to comment.