diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-13 12:55:12 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-08-13 12:55:12 +0800 |
| commit | fde3e045d4dcc5c404cb4216389e18b0e720b8e0 (patch) | |
| tree | bdb3f421a87028594d122a6dcae7150c2e5589ca /backend/include | |
| parent | aa7c85ca71859aab15bdf1e6dc1ea3a5aa5d1e28 (diff) | |
| download | rcc-project-fde3e045d4dcc5c404cb4216389e18b0e720b8e0.tar.gz rcc-project-fde3e045d4dcc5c404cb4216389e18b0e720b8e0.tar.xz | |
[IR] Split LL parsing, assembly writing, and data layout
Diffstat (limited to 'backend/include')
| -rw-r--r-- | backend/include/rcc/AsmParser/LLParser.h | 36 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/GlobalObject.h | 2 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MIRYamlMapping.h | 4 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MachineConstantPool.h | 2 | ||||
| -rw-r--r-- | backend/include/rcc/CodeGen/MachineMemOperand.h | 2 | ||||
| -rw-r--r-- | backend/include/rcc/IR/Constants.h (renamed from backend/include/rcc/CodeGen/GenericConstant.h) | 9 | ||||
| -rw-r--r-- | backend/include/rcc/IR/DataLayout.h | 82 | ||||
| -rw-r--r-- | backend/include/rcc/MIR/LLVMConstantSyntax.h | 52 |
8 files changed, 128 insertions, 61 deletions
diff --git a/backend/include/rcc/AsmParser/LLParser.h b/backend/include/rcc/AsmParser/LLParser.h new file mode 100644 index 0000000..e15a006 --- /dev/null +++ b/backend/include/rcc/AsmParser/LLParser.h @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_ASMPARSER_LLPARSER_H +#define RCC_ASMPARSER_LLPARSER_H + +#include "rcc/IR/Constants.h" +#include "rcc/Support/Error.h" + +#include <cstddef> +#include <span> +#include <string_view> + +namespace rcc { + +struct LLNamedTypeDefinition final { + std::string_view Name; + std::string_view Definition; + bool NumericName = false; +}; + +struct LLParserOptions final { + std::span<const LLNamedTypeDefinition> NamedTypes; + std::size_t MaximumDepth = 1024; + std::size_t MaximumNodes = std::size_t{1} << 24U; + std::size_t MaximumTokenLength = std::size_t{1} << 24U; +}; + +[[nodiscard]] Expected<GenericConstantData> parseLLVMConstant(std::string_view Text, + LLParserOptions Options = {}); + +[[nodiscard]] Expected<GenericConstantType> parseLLVMType(std::string_view Text, + LLParserOptions Options = {}); + +} // namespace rcc + +#endif // RCC_ASMPARSER_LLPARSER_H diff --git a/backend/include/rcc/CodeGen/GlobalObject.h b/backend/include/rcc/CodeGen/GlobalObject.h index 798dcd8..1ddc34a 100644 --- a/backend/include/rcc/CodeGen/GlobalObject.h +++ b/backend/include/rcc/CodeGen/GlobalObject.h @@ -3,9 +3,9 @@ #ifndef RCC_CODEGEN_GLOBALOBJECT_H #define RCC_CODEGEN_GLOBALOBJECT_H -#include "rcc/CodeGen/GenericConstant.h" #include "rcc/CodeGen/MachineIRTypes.h" #include "rcc/CodeGen/TargetConstant.h" +#include "rcc/IR/Constants.h" #include "rcc/Support/Error.h" #include <cstddef> diff --git a/backend/include/rcc/CodeGen/MIRYamlMapping.h b/backend/include/rcc/CodeGen/MIRYamlMapping.h index 82880ca..c25559b 100644 --- a/backend/include/rcc/CodeGen/MIRYamlMapping.h +++ b/backend/include/rcc/CodeGen/MIRYamlMapping.h @@ -9,7 +9,7 @@ #include "rcc/CodeGen/MachineJumpTableInfo.h" #include "rcc/CodeGen/MachineMetadata.h" #include "rcc/CodeGen/TargetMIRTextInfo.h" -#include "rcc/MIR/LLVMConstantSyntax.h" +#include "rcc/IR/DataLayout.h" #include "rcc/Support/Error.h" #include <cstdint> @@ -106,7 +106,7 @@ private: [[nodiscard]] bool convertTargetNode(const yaml::Node &Source, MIRTargetNode &Destination); [[nodiscard]] bool validateDocumentReferences(const MachineFunction &Function); [[nodiscard]] std::string_view effectiveDataLayout() const noexcept; - [[nodiscard]] LLVMConstantSyntaxOptions pointerLayoutOptions() const noexcept; + [[nodiscard]] Expected<DataLayout> dataLayout() const; [[nodiscard]] MachineBasicBlock *blockAt(MachineFunction &Function, std::uint32_t Number) const noexcept; diff --git a/backend/include/rcc/CodeGen/MachineConstantPool.h b/backend/include/rcc/CodeGen/MachineConstantPool.h index a0c269b..104e559 100644 --- a/backend/include/rcc/CodeGen/MachineConstantPool.h +++ b/backend/include/rcc/CodeGen/MachineConstantPool.h @@ -3,10 +3,10 @@ #ifndef RCC_CODEGEN_MACHINECONSTANTPOOL_H #define RCC_CODEGEN_MACHINECONSTANTPOOL_H -#include "rcc/CodeGen/GenericConstant.h" #include "rcc/CodeGen/MachineIRTypes.h" #include "rcc/CodeGen/MachineMemOperand.h" #include "rcc/CodeGen/TargetConstant.h" +#include "rcc/IR/Constants.h" #include "rcc/Support/Error.h" #include <cstddef> diff --git a/backend/include/rcc/CodeGen/MachineMemOperand.h b/backend/include/rcc/CodeGen/MachineMemOperand.h index c5c1357..d236fd3 100644 --- a/backend/include/rcc/CodeGen/MachineMemOperand.h +++ b/backend/include/rcc/CodeGen/MachineMemOperand.h @@ -3,10 +3,10 @@ #ifndef RCC_CODEGEN_MACHINEMEMOPERAND_H #define RCC_CODEGEN_MACHINEMEMOPERAND_H -#include "rcc/CodeGen/GenericConstant.h" #include "rcc/CodeGen/LowLevelType.h" #include "rcc/CodeGen/MachineIRTypes.h" #include "rcc/CodeGen/MachineMetadata.h" +#include "rcc/IR/Constants.h" #include <cstdint> #include <initializer_list> diff --git a/backend/include/rcc/CodeGen/GenericConstant.h b/backend/include/rcc/IR/Constants.h index 4219c24..9f894f2 100644 --- a/backend/include/rcc/CodeGen/GenericConstant.h +++ b/backend/include/rcc/IR/Constants.h @@ -1,11 +1,10 @@ // SPDX-License-Identifier: GPL-3.0-or-later -#ifndef RCC_CODEGEN_GENERICCONSTANT_H -#define RCC_CODEGEN_GENERICCONSTANT_H +#ifndef RCC_IR_CONSTANTS_H +#define RCC_IR_CONSTANTS_H #include "rcc/ADT/APFloat.h" #include "rcc/ADT/APInt.h" -#include "rcc/CodeGen/LowLevelType.h" #include "rcc/Support/Error.h" #include <cstddef> @@ -63,6 +62,7 @@ struct GenericConstantType final { [[nodiscard]] static Expected<GenericConstantType> named(std::string Name, bool NumericName = false); + [[nodiscard]] const GenericConstantType &resolved() const noexcept; [[nodiscard]] Expected<void> verify() const; friend bool operator==(const GenericConstantType &Left, @@ -164,6 +164,7 @@ struct GenericConstantData final { [[nodiscard]] static Expected<GenericConstantData> byteString(GenericConstantType Type, std::vector<std::byte> Bytes); + [[nodiscard]] Expected<std::string> toLLVMString() const; [[nodiscard]] Expected<void> verify() const; [[nodiscard]] bool referencesSymbol() const noexcept; @@ -173,4 +174,4 @@ struct GenericConstantData final { } // namespace rcc -#endif // RCC_CODEGEN_GENERICCONSTANT_H +#endif // RCC_IR_CONSTANTS_H diff --git a/backend/include/rcc/IR/DataLayout.h b/backend/include/rcc/IR/DataLayout.h new file mode 100644 index 0000000..0ca3f07 --- /dev/null +++ b/backend/include/rcc/IR/DataLayout.h @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RCC_IR_DATALAYOUT_H +#define RCC_IR_DATALAYOUT_H + +#include "rcc/IR/Constants.h" +#include "rcc/Support/Error.h" + +#include <cstddef> +#include <cstdint> +#include <string> +#include <string_view> +#include <vector> + +namespace rcc { + +class DataLayout final { +public: + [[nodiscard]] static Expected<DataLayout> parse(std::string_view LayoutString); + + [[nodiscard]] Expected<std::uint64_t> getTypeAllocSize(const GenericConstantType &Type) const; + [[nodiscard]] Expected<std::uint64_t> getABITypeAlign(const GenericConstantType &Type) const; + [[nodiscard]] Expected<std::uint64_t> getPrefTypeAlign(const GenericConstantType &Type) const; + [[nodiscard]] Expected<std::uint32_t> getPointerSizeInBits(std::uint32_t AddressSpace = 0) const; + +private: + struct AlignmentSpec final { + std::uint64_t ABI = 1; + std::uint64_t Preferred = 1; + }; + + struct PrimitiveSpec final { + char Kind = '\0'; + std::uint32_t BitWidth = 0; + AlignmentSpec Alignment; + }; + + struct PointerSpec final { + std::uint32_t AddressSpace = 0; + std::uint32_t SizeInBits = 0; + AlignmentSpec Alignment; + std::string AddressSpaceName; + }; + + struct TypeLayout final { + std::uint64_t StoreSize = 0; + std::uint64_t AllocationSize = 0; + std::uint64_t ABIAlignment = 1; + std::uint64_t PreferredAlignment = 1; + }; + + [[nodiscard]] static Expected<std::uint64_t> parseUnsignedField(std::string_view Text, + std::string_view Description); + [[nodiscard]] static Expected<std::uint64_t> alignmentBytes(std::string_view Text, bool AllowZero, + std::string_view Description); + [[nodiscard]] static std::vector<std::string_view> split(std::string_view Text, char Separator); + + void setPrimitiveSpec(PrimitiveSpec Entry); + void setPointerSpec(PointerSpec Entry); + [[nodiscard]] AlignmentSpec naturalAlignment(std::uint64_t StoreSize) const noexcept; + [[nodiscard]] AlignmentSpec scalarAlignment(char Kind, std::uint32_t BitWidth, + std::uint64_t StoreSize) const noexcept; + [[nodiscard]] AlignmentSpec integerAlignment(std::uint32_t BitWidth, + std::uint64_t StoreSize) const noexcept; + [[nodiscard]] AlignmentSpec floatingAlignment(std::uint32_t BitWidth, + std::uint64_t StoreSize) const noexcept; + [[nodiscard]] AlignmentSpec vectorAlignment(std::uint32_t BitWidth, + std::uint64_t StoreSize) const noexcept; + [[nodiscard]] Expected<PointerSpec> getPointerSpec(std::uint32_t AddressSpace) const; + [[nodiscard]] Expected<TypeLayout> getTypeLayout(const GenericConstantType &Type, + std::vector<std::string> &NamedTypeStack, + std::size_t Depth) const; + + bool Explicit = false; + std::vector<PrimitiveSpec> PrimitiveSpecs; + std::vector<PointerSpec> PointerSpecs; + AlignmentSpec AggregateAlignment{1, 8}; +}; + +} // namespace rcc + +#endif // RCC_IR_DATALAYOUT_H diff --git a/backend/include/rcc/MIR/LLVMConstantSyntax.h b/backend/include/rcc/MIR/LLVMConstantSyntax.h deleted file mode 100644 index 7e18d60..0000000 --- a/backend/include/rcc/MIR/LLVMConstantSyntax.h +++ /dev/null @@ -1,52 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later - -#ifndef RCC_MIR_LLVMCONSTANTSYNTAX_H -#define RCC_MIR_LLVMCONSTANTSYNTAX_H - -#include "rcc/CodeGen/GenericConstant.h" -#include "rcc/CodeGen/MachineIRTypes.h" -#include "rcc/Support/Error.h" - -#include <cstddef> -#include <cstdint> -#include <span> -#include <string> -#include <string_view> - -namespace rcc::mir { - -struct LLVMNamedTypeDefinition final { - std::string_view Name; - std::string_view Definition; - bool NumericName = false; -}; - -struct LLVMConstantSyntaxOptions final { - std::string_view DataLayout; - std::span<const LLVMNamedTypeDefinition> NamedTypes; - std::size_t MaximumDepth = 1024; - std::size_t MaximumNodes = std::size_t{1} << 24U; - std::size_t MaximumTokenLength = std::size_t{1} << 24U; -}; - -[[nodiscard]] Expected<GenericConstantData> -parseLLVMConstant(std::string_view Text, LLVMConstantSyntaxOptions Options = {}); - -[[nodiscard]] Expected<GenericConstantType> parseLLVMType(std::string_view Text, - LLVMConstantSyntaxOptions Options = {}); - -[[nodiscard]] Expected<std::string> printLLVMConstant(const GenericConstantData &Constant); - -[[nodiscard]] Expected<std::uint64_t> -llvmConstantStorageSize(const GenericConstantType &Type, LLVMConstantSyntaxOptions Options = {}); - -[[nodiscard]] Expected<Alignment> -llvmConstantPreferredAlignment(const GenericConstantType &Type, - LLVMConstantSyntaxOptions Options = {}); - -[[nodiscard]] Expected<std::uint32_t> llvmPointerSizeInBits(std::uint32_t AddressSpace, - LLVMConstantSyntaxOptions Options = {}); - -} // namespace rcc::mir - -#endif // RCC_MIR_LLVMCONSTANTSYNTAX_H |
