blob: 81a955c0186ec5dc6f644da130362d507f87ba26 [file] [log] [blame]
//===- subzero/src/IceTypeConverter.h - Convert ICE/LLVM Types --*- C++ -*-===//
//
// The Subzero Code Generator
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines how to convert LLVM types to ICE types, and ICE types
// to LLVM types.
//
//===----------------------------------------------------------------------===//
#ifndef SUBZERO_SRC_ICETYPECONVERTER_H
#define SUBZERO_SRC_ICETYPECONVERTER_H
#include "IceDefs.h"
#include "IceTypes.h"
#include "llvm/IR/DerivedTypes.h"
namespace llvm {
class LLVMContext;
} // end of llvm namespace.
namespace Ice {
/// Converts LLVM types to ICE types, and ICE types to LLVM types.
class TypeConverter {
TypeConverter(const TypeConverter &) LLVM_DELETED_FUNCTION;
TypeConverter &operator=(const TypeConverter &) LLVM_DELETED_FUNCTION;
public:
/// Context is the context to use to build llvm types.
TypeConverter(llvm::LLVMContext &Context);
/// Returns the LLVM type for the corresponding ICE type Ty.
llvm::Type *convertToLLVMType(Type Ty) const {
// Note: We use "at" here in case Ty wasn't registered.
return LLVMTypes.at(Ty);
}
/// Converts LLVM type LLVMTy to an ICE type. Returns
/// Ice::IceType_NUM if unable to convert.
Type convertToIceType(llvm::Type *LLVMTy) const {
std::map<llvm::Type *, Type>::const_iterator Pos = LLVM2IceMap.find(LLVMTy);
if (Pos == LLVM2IceMap.end())
return convertToIceTypeOther(LLVMTy);
return Pos->second;
}
/// Returns ICE model of pointer type.
Type getIcePointerType() const { return IceType_i32; }
/// Returns LLVM integer type with specified number of bits. Returns
/// NULL if not a valid PNaCl integer type.
llvm::Type *getLLVMIntegerType(unsigned NumBits) const;
/// Returns the LLVM vector type for Size and Ty arguments. Returns
/// NULL if not a valid PNaCl vector type.
llvm::Type *getLLVMVectorType(unsigned Size, Type Ty) const;
private:
// The LLVM context to use to build LLVM types.
llvm::LLVMContext &Context;
// The list of allowable LLVM types. Indexed by ICE type.
std::vector<llvm::Type *> LLVMTypes;
// The inverse mapping of LLVMTypes.
std::map<llvm::Type *, Type> LLVM2IceMap;
// Add LLVM/ICE pair to internal tables.
void AddLLVMType(Type Ty, llvm::Type *LLVMTy);
// Converts types not in LLVM2IceMap.
Type convertToIceTypeOther(llvm::Type *LLVMTy) const;
};
} // end of Ice namespace.
#endif // SUBZERO_SRC_ICETYPECONVERTER_H