[clang] [llvm] [WIP] ABI Lowering Library (PR #140112)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 14 07:46:42 PDT 2025
================
@@ -0,0 +1,282 @@
+//===- 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/Support/Alignment.h"
+#include "llvm/Support/Allocator.h"
+#include "llvm/Support/TypeSize.h"
+#include <cstdint>
+
+namespace llvm {
+namespace abi {
+
+enum class TypeKind {
+ Void,
+ Integer,
+ Float,
+ Pointer,
+ Array,
+ Vector,
+ Struct,
+ Union,
+};
+
+class Type {
+protected:
+ TypeKind Kind;
+ TypeSize SizeInBits;
+ Align Alignment;
+ bool IsExplicitlyAligned;
+
+ Type(TypeKind K, TypeSize Size, Align Align, bool ExplicitAlign = false)
+ : Kind(K), SizeInBits(Size), Alignment(Align),
+ IsExplicitlyAligned(ExplicitAlign) {}
+
+public:
+ TypeKind getKind() const { return Kind; }
+ TypeSize getSizeInBits() const { return SizeInBits; }
+ Align getAlignment() const { return Alignment; }
+ bool hasExplicitAlignment() const { return IsExplicitlyAligned; }
+
+ void setExplicitAlignment(Align Align) {
+ Alignment = Align;
+ IsExplicitlyAligned = true;
+ }
+
+ 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 isUnion() const { return Kind == TypeKind::Union; }
+};
+
+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 IntegerType : public Type {
+private:
+ bool IsSigned;
+
+public:
+ IntegerType(uint64_t BitWidth, Align Align, bool Signed)
+ : Type(TypeKind::Integer, TypeSize::getFixed(BitWidth), Align),
+ IsSigned(Signed) {}
+
+ bool isSigned() const { return IsSigned; }
+
+ 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) {}
+
+ static bool classof(const Type *T) { return T->getKind() == TypeKind::Float; }
+};
+
+class PointerType : public Type {
+public:
+ PointerType(uint64_t Size, Align Align)
+ : Type(TypeKind::Pointer, TypeSize::getFixed(Size), Align) {}
+
+ static bool classof(const Type *T) {
+ return T->getKind() == TypeKind::Pointer;
+ }
+};
+
+class ArrayType : public Type {
+private:
+ const Type *ElementType;
+ uint64_t NumElements;
+
+public:
+ ArrayType(const Type *ElemType, uint64_t NumElems)
+ : Type(TypeKind::Array, ElemType->getSizeInBits() * NumElems,
+ ElemType->getAlignment()),
+ ElementType(ElemType), NumElements(NumElems) {}
+
+ const Type *getElementType() const { return ElementType; }
+ uint64_t getNumElements() const { return NumElements; }
+
+ 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,
+ NumElems.isScalable()
+ ? TypeSize(ElemType->getSizeInBits().getFixedValue() *
+ NumElems.getKnownMinValue(),
+ true)
----------------
nikic wrote:
```suggestion
NumElems.isScalable())
```
And then you don't need the ternary?
https://github.com/llvm/llvm-project/pull/140112
More information about the llvm-commits
mailing list