aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/IR/DataLayout.h
blob: 0ca3f07b01a35cef88c415718633a906fe468951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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