[clang] [llvm] [WIP] ABI Lowering Library (PR #140112)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 17 14:14:08 PDT 2025
================
@@ -0,0 +1,538 @@
+//===- ABI/Types.h ----------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file defines the Types and related helper methods concerned to the
+/// LLVMABI library which mirrors ABI related type information from
+/// the LLVM frontend.
+///
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_ABI_TYPES_H
+#define LLVM_ABI_TYPES_H
+
+#include "llvm/ADT/APFloat.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/IR/CallingConv.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/Support/Alignment.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Support/TypeSize.h"
+#include <algorithm>
+#include <cstdint>
+
+namespace llvm {
+namespace abi {
+
+enum class TypeKind {
+ Void,
+ MemberPointer,
+ Complex,
+ Integer,
+ Float,
+ Pointer,
+ Array,
+ Vector,
+ Struct,
+};
+
+class Type {
+private:
+ TypeSize getTypeStoreSize() const {
+ TypeSize StoreSizeInBits = getTypeStoreSizeInBits();
+ return {StoreSizeInBits.getKnownMinValue() / 8,
+ StoreSizeInBits.isScalable()};
+ }
+ TypeSize getTypeStoreSizeInBits() const {
+ TypeSize BaseSize = getSizeInBits();
+ uint64_t AlignedSizeInBits =
+ alignToPowerOf2(BaseSize.getKnownMinValue(), 8);
+ return {AlignedSizeInBits, BaseSize.isScalable()};
+ }
+
+protected:
+ TypeKind Kind;
+ TypeSize SizeInBits;
+ Align ABIAlignment;
+ Align PreferredAlignment;
+ bool IsExplicitlyAligned;
+
+ Type(TypeKind K, TypeSize Size, Align Align, bool ExplicitAlign = false)
+ : Kind(K), SizeInBits(Size), ABIAlignment(Align),
+ IsExplicitlyAligned(ExplicitAlign) {}
+
+public:
+ TypeKind getKind() const { return Kind; }
+ TypeSize getSizeInBits() const { return SizeInBits; }
+ Align getAlignment() const { return ABIAlignment; }
+ Align getPrefferedAlign() const { return PreferredAlignment; }
+ bool hasExplicitAlignment() const { return IsExplicitlyAligned; }
+
+ void setExplicitAlignment(Align Align) {
+ ABIAlignment = Align;
+ IsExplicitlyAligned = true;
+ }
+ TypeSize getTypeAllocSize() const {
+ return alignTo(getTypeStoreSize(), getAlignment().value());
+ }
+
+ bool isVoid() const { return Kind == TypeKind::Void; }
+ bool isInteger() const { return Kind == TypeKind::Integer; }
+ bool isFloat() const { return Kind == TypeKind::Float; }
+ bool isPointer() const { return Kind == TypeKind::Pointer; }
+ bool isArray() const { return Kind == TypeKind::Array; }
+ bool isVector() const { return Kind == TypeKind::Vector; }
+ bool isStruct() const { return Kind == TypeKind::Struct; }
+ bool isMemberPointer() const { return Kind == TypeKind::MemberPointer; }
+ bool isComplex() const { return Kind == TypeKind::Complex; }
+};
+
+class VoidType : public Type {
+public:
+ VoidType() : Type(TypeKind::Void, TypeSize::getFixed(0), Align(1)) {}
+
+ static bool classof(const Type *T) { return T->getKind() == TypeKind::Void; }
+};
+
+class ComplexType : public Type {
+public:
+ ComplexType(const Type *ElementType, uint64_t SizeInBits, Align Alignment)
+ : Type(TypeKind::Complex, TypeSize::getFixed(SizeInBits), Alignment),
+ ElementType(ElementType) {}
+
+ const Type *getElementType() const { return ElementType; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Complex;
+ }
+
+private:
+ const Type *ElementType;
+};
+
+class IntegerType : public Type {
+private:
+ bool IsSigned;
+ bool IsBoolean;
+ bool IsBitInt;
+ uint64_t ABISizeInBits;
+
+public:
+ IntegerType(uint64_t BitWidth, uint64_t ABIWidth, Align Align, bool Signed,
+ bool IsBool = false, bool BitInt = false)
+ : Type(TypeKind::Integer, TypeSize::getFixed(BitWidth), Align),
+ IsSigned(Signed), IsBoolean(IsBool), IsBitInt(BitInt),
+ ABISizeInBits(ABIWidth) {}
+
+ bool isSigned() const { return IsSigned; }
+ bool isBool() const { return IsBoolean; }
+ bool isBitInt() const { return IsBitInt; }
+ uint64_t getLogicalBitWidth() const {
+ return getSizeInBits().getFixedValue();
+ }
+ uint64_t getABISizeInBits() const { return ABISizeInBits; }
+ bool isABIWidened() const { return getLogicalBitWidth() != ABISizeInBits; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Integer;
+ }
+};
+
+class FloatType : public Type {
+private:
+ const fltSemantics *Semantics;
+
+public:
+ FloatType(const fltSemantics &FloatSemantics, Align Align)
+ : Type(TypeKind::Float,
+ TypeSize::getFixed(APFloat::getSizeInBits(FloatSemantics)), Align),
+ Semantics(&FloatSemantics) {}
+
+ const fltSemantics *getSemantics() const { return Semantics; }
+ static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
+};
+
+class PointerLikeType : public Type {
+protected:
+ unsigned AddrSpace;
+ PointerLikeType(TypeKind K, TypeSize Size, Align Align, unsigned AS = 0)
+ : Type(K, Size, Align), AddrSpace(AS) {}
+
+public:
+ virtual ~PointerLikeType() = default;
+ unsigned getAddrSpace() const { return AddrSpace; }
+ virtual bool isMemberPointer() const = 0;
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Pointer ||
+ T->getKind() == TypeKind::MemberPointer;
+ }
+};
+
+class PointerType : public PointerLikeType {
+public:
+ PointerType(uint64_t Size, Align Align, unsigned AddressSpace = 0)
+ : PointerLikeType(TypeKind::Pointer, TypeSize::getFixed(Size), Align,
+ AddressSpace) {}
+
+ bool isMemberPointer() const override { return false; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Pointer;
+ }
+};
+
+class MemberPointerType : public PointerLikeType {
+private:
+ bool IsFunctionPointer;
+ bool Has64BitPointers;
+
+public:
+ MemberPointerType(bool IsFunctionPointer, bool Has64BitPointers,
+ uint64_t SizeInBits, Align Alignment,
+ unsigned AddressSpace = 0)
+ : PointerLikeType(TypeKind::MemberPointer, TypeSize::getFixed(SizeInBits),
+ Alignment, AddressSpace),
+ IsFunctionPointer(IsFunctionPointer),
+ Has64BitPointers(Has64BitPointers) {}
+
+ bool isMemberPointer() const override { return true; }
+ bool isFunctionPointer() const { return IsFunctionPointer; }
+ bool has64BitPointers() const { return Has64BitPointers; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::MemberPointer;
+ }
+};
+
+class ArrayType : public Type {
+private:
+ const Type *ElementType;
+ uint64_t NumElements;
+ bool IsMatrix;
+
+public:
+ ArrayType(const Type *ElemType, uint64_t NumElems, bool IsMatrixType = false)
+ : Type(TypeKind::Array, ElemType->getSizeInBits() * NumElems,
+ ElemType->getAlignment()),
+ ElementType(ElemType), NumElements(NumElems), IsMatrix(IsMatrixType) {}
+
+ const Type *getElementType() const { return ElementType; }
+ uint64_t getNumElements() const { return NumElements; }
+ bool isMatrixType() const { return IsMatrix; }
+
+ static bool classof(const Type *T) { return T->getKind() == TypeKind::Array; }
+};
+
+class VectorType : public Type {
+private:
+ const Type *ElementType;
+ ElementCount NumElements;
+
+public:
+ VectorType(const Type *ElemType, ElementCount NumElems, Align Align)
+ : Type(TypeKind::Vector,
+ TypeSize(ElemType->getSizeInBits().getFixedValue() *
+ NumElems.getKnownMinValue(),
+ NumElems.isScalable()),
+ Align),
+ ElementType(ElemType), NumElements(NumElems) {}
+
+ const Type *getElementType() const { return ElementType; }
+ ElementCount getNumElements() const { return NumElements; }
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Vector;
+ }
+};
+
+struct FieldInfo {
+ const Type *FieldType;
+ uint64_t OffsetInBits;
+ bool IsBitField;
+ bool IsUnnamedBitfield;
+ uint64_t BitFieldWidth;
+
+ FieldInfo(const Type *Type, uint64_t Offset = 0, bool BitField = false,
+ uint64_t BFWidth = 0, bool IsUnnamedBF = false)
+ : FieldType(Type), OffsetInBits(Offset), IsBitField(BitField),
+ IsUnnamedBitfield(IsUnnamedBF), BitFieldWidth(BFWidth) {}
+};
+
+enum class StructPacking { Default, Packed, ExplicitPacking };
+
+class StructType : public Type {
----------------
vortex73 wrote:
I thought, since llvm calls them structs, the library could maintain consistency with that.
https://github.com/llvm/llvm-project/pull/140112
More information about the cfe-commits
mailing list