CIRCT  20.0.0git
Common.h
Go to the documentation of this file.
1 //===- Common.h - Commonly used classes w/o dependencies --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // DO NOT EDIT!
10 // This file is distributed as part of an ESI package. The source for this file
11 // should always be modified within CIRCT.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 // NOLINTNEXTLINE(llvm-header-guard)
16 #ifndef ESI_COMMON_H
17 #define ESI_COMMON_H
18 
19 #include <any>
20 #include <cstdint>
21 #include <map>
22 #include <optional>
23 #include <stdexcept>
24 #include <string>
25 #include <vector>
26 
27 namespace esi {
28 class Type;
29 
30 //===----------------------------------------------------------------------===//
31 // Common accelerator description types.
32 //===----------------------------------------------------------------------===//
33 
34 struct AppID {
35  std::string name;
36  std::optional<uint32_t> idx;
37 
38  AppID(const std::string &name, std::optional<uint32_t> idx = std::nullopt)
39  : name(name), idx(idx) {}
40 
41  bool operator==(const AppID &other) const {
42  return name == other.name && idx == other.idx;
43  }
44  bool operator!=(const AppID &other) const { return !(*this == other); }
45 };
46 bool operator<(const AppID &a, const AppID &b);
47 
48 class AppIDPath : public std::vector<AppID> {
49 public:
50  using std::vector<AppID>::vector;
51 
52  AppIDPath operator+(const AppIDPath &b);
53  std::string toStr() const;
54 };
55 bool operator<(const AppIDPath &a, const AppIDPath &b);
56 
57 struct Constant {
58  std::any value;
59  std::optional<const Type *> type;
60 };
61 
62 struct ModuleInfo {
63  std::optional<std::string> name;
64  std::optional<std::string> summary;
65  std::optional<std::string> version;
66  std::optional<std::string> repo;
67  std::optional<std::string> commitHash;
68  std::map<std::string, Constant> constants;
69  std::map<std::string, std::any> extra;
70 };
71 
72 /// A description of a service port. Used pretty exclusively in setting up the
73 /// design.
75  std::string name;
76  std::string portName;
77 };
78 
79 /// Details about how to connect to a particular channel.
81  /// The name of the type of connection. Typically, the name of the DMA engine
82  /// or "cosim" if a cosimulation channel is being used.
83  std::string type;
84  /// Implementation-specific options.
85  std::map<std::string, std::any> implOptions;
86 };
87 using ChannelAssignments = std::map<std::string, ChannelAssignment>;
88 
89 /// A description of a hardware client. Used pretty exclusively in setting up
90 /// the design.
95  std::map<std::string, std::any> implOptions;
96 };
97 using HWClientDetails = std::vector<HWClientDetail>;
98 using ServiceImplDetails = std::map<std::string, std::any>;
99 
100 /// A logical chunk of data representing serialized data. Currently, just a
101 /// wrapper for a vector of bytes, which is not efficient in terms of memory
102 /// copying. This will change in the future as will the API.
103 class MessageData {
104 public:
105  /// Adopts the data vector buffer.
106  MessageData() = default;
107  MessageData(std::vector<uint8_t> &data) : data(std::move(data)) {}
108  MessageData(const uint8_t *data, size_t size) : data(data, data + size) {}
109  ~MessageData() = default;
110 
111  const uint8_t *getBytes() const { return data.data(); }
112  /// Get the size of the data in bytes.
113  size_t getSize() const { return data.size(); }
114 
115  /// Cast to a type. Throws if the size of the data does not match the size of
116  /// the message. The lifetime of the resulting pointer is tied to the lifetime
117  /// of this object.
118  template <typename T>
119  const T *as() const {
120  if (data.size() != sizeof(T))
121  throw std::runtime_error("Data size does not match type size. Size is " +
122  std::to_string(data.size()) + ", expected " +
123  std::to_string(sizeof(T)) + ".");
124  return reinterpret_cast<const T *>(data.data());
125  }
126 
127  /// Cast from a type to its raw bytes.
128  template <typename T>
129  static MessageData from(T &t) {
130  return MessageData(reinterpret_cast<const uint8_t *>(&t), sizeof(T));
131  }
132 
133  /// Convert the data to a hex string.
134  std::string toHex() const;
135 
136 private:
137  std::vector<uint8_t> data;
138 };
139 
140 } // namespace esi
141 
142 std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);
143 std::ostream &operator<<(std::ostream &, const esi::AppID &);
144 
145 //===----------------------------------------------------------------------===//
146 // Functions which should be in the standard library.
147 //===----------------------------------------------------------------------===//
148 
149 namespace esi {
150 std::string toHex(uint32_t val);
151 } // namespace esi
152 
153 #endif // ESI_COMMON_H
std::ostream & operator<<(std::ostream &, const esi::ModuleInfo &)
std::string toStr() const
Definition: Manifest.cpp:708
AppIDPath operator+(const AppIDPath &b)
Definition: Manifest.cpp:702
A logical chunk of data representing serialized data.
Definition: Common.h:103
std::string toHex() const
Convert the data to a hex string.
Definition: Common.cpp:22
~MessageData()=default
size_t getSize() const
Get the size of the data in bytes.
Definition: Common.h:113
MessageData()=default
Adopts the data vector buffer.
MessageData(const uint8_t *data, size_t size)
Definition: Common.h:108
const T * as() const
Cast to a type.
Definition: Common.h:119
MessageData(std::vector< uint8_t > &data)
Definition: Common.h:107
static MessageData from(T &t)
Cast from a type to its raw bytes.
Definition: Common.h:129
std::vector< uint8_t > data
Definition: Common.h:137
const uint8_t * getBytes() const
Definition: Common.h:111
Definition: esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition: Common.h:98
std::string toHex(uint32_t val)
Definition: Common.cpp:37
std::map< std::string, ChannelAssignment > ChannelAssignments
Definition: Common.h:87
bool operator<(const AppID &a, const AppID &b)
Definition: Manifest.cpp:714
std::vector< HWClientDetail > HWClientDetails
Definition: Common.h:97
std::string name
Definition: Common.h:35
bool operator!=(const AppID &other) const
Definition: Common.h:44
bool operator==(const AppID &other) const
Definition: Common.h:41
std::optional< uint32_t > idx
Definition: Common.h:36
AppID(const std::string &name, std::optional< uint32_t > idx=std::nullopt)
Definition: Common.h:38
Details about how to connect to a particular channel.
Definition: Common.h:80
std::map< std::string, std::any > implOptions
Implementation-specific options.
Definition: Common.h:85
std::string type
The name of the type of connection.
Definition: Common.h:83
std::any value
Definition: Common.h:58
std::optional< const Type * > type
Definition: Common.h:59
A description of a hardware client.
Definition: Common.h:91
std::map< std::string, std::any > implOptions
Definition: Common.h:95
AppIDPath relPath
Definition: Common.h:92
ChannelAssignments channelAssignments
Definition: Common.h:94
ServicePortDesc port
Definition: Common.h:93
std::optional< std::string > version
Definition: Common.h:65
std::optional< std::string > name
Definition: Common.h:63
std::map< std::string, Constant > constants
Definition: Common.h:68
std::optional< std::string > commitHash
Definition: Common.h:67
std::optional< std::string > repo
Definition: Common.h:66
std::map< std::string, std::any > extra
Definition: Common.h:69
std::optional< std::string > summary
Definition: Common.h:64
A description of a service port.
Definition: Common.h:74
std::string name
Definition: Common.h:75
std::string portName
Definition: Common.h:76