CIRCT 22.0.0git
Loading...
Searching...
No Matches
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 <span>
24#include <stdexcept>
25#include <string>
26#include <vector>
27
28namespace esi {
29class Type;
30
31//===----------------------------------------------------------------------===//
32// Common accelerator description types.
33//===----------------------------------------------------------------------===//
34
35struct AppID {
36 std::string name;
37 std::optional<uint32_t> idx;
38
39 AppID(const std::string &name, std::optional<uint32_t> idx = std::nullopt)
40 : name(name), idx(idx) {}
41
42 bool operator==(const AppID &other) const {
43 return name == other.name && idx == other.idx;
44 }
45 bool operator!=(const AppID &other) const { return !(*this == other); }
46 friend std::ostream &operator<<(std::ostream &os, const AppID &id);
47
48 std::string toString() const {
49 if (idx.has_value())
50 return name + "[" + std::to_string(idx.value()) + "]";
51 return name;
52 }
53};
54bool operator<(const AppID &a, const AppID &b);
55
56class AppIDPath : public std::vector<AppID> {
57public:
58 using std::vector<AppID>::vector;
59
60 AppIDPath operator+(const AppIDPath &b) const;
61 AppIDPath parent() const;
62 std::string toStr() const;
63 friend std::ostream &operator<<(std::ostream &os, const AppIDPath &path);
64};
65bool operator<(const AppIDPath &a, const AppIDPath &b);
66
67struct Constant {
68 std::any value;
69 std::optional<const Type *> type;
70};
71
72struct ModuleInfo {
73 std::optional<std::string> name;
74 std::optional<std::string> summary;
75 std::optional<std::string> version;
76 std::optional<std::string> repo;
77 std::optional<std::string> commitHash;
78 std::map<std::string, Constant> constants;
79 std::map<std::string, std::any> extra;
80};
81
82/// A description of a service port. Used pretty exclusively in setting up the
83/// design.
85 std::string name;
86 std::string portName;
87};
88
89/// Details about how to connect to a particular channel.
91 /// The name of the type of connection. Typically, the name of the DMA engine
92 /// or "cosim" if a cosimulation channel is being used.
93 std::string type;
94 /// Implementation-specific options.
95 std::map<std::string, std::any> implOptions;
96};
97using ChannelAssignments = std::map<std::string, ChannelAssignment>;
98
99/// A description of a hardware client. Used pretty exclusively in setting up
100/// the design.
107using HWClientDetails = std::vector<HWClientDetail>;
108using ServiceImplDetails = std::map<std::string, std::any>;
109
110/// A logical chunk of data representing serialized data. Currently, just a
111/// wrapper for a vector of bytes, which is not efficient in terms of memory
112/// copying. This will change in the future as will the API.
114public:
115 /// Adopts the data vector buffer.
116 MessageData() = default;
117 MessageData(std::span<const uint8_t> data)
118 : data(data.data(), data.data() + data.size()) {}
119 MessageData(std::vector<uint8_t> &data) : data(std::move(data)) {}
120 MessageData(std::vector<uint8_t> &&data) : data(std::move(data)) {}
121 MessageData(const uint8_t *data, size_t size) : data(data, data + size) {}
122 ~MessageData() = default;
123
124 const uint8_t *getBytes() const { return data.data(); }
125
126 /// Get the data as a vector of bytes.
127 const std::vector<uint8_t> &getData() const { return data; }
128
129 /// Implicit conversion to a vector/span of bytes, to play nice with other
130 /// APIs that accept bytearray-like things.
131 operator const std::vector<uint8_t> &() const { return data; }
132 operator std::span<const uint8_t>() const { return data; }
133
134 /// Move the data out of this object.
135 std::vector<uint8_t> takeData() { return std::move(data); }
136
137 /// Get the size of the data in bytes.
138 size_t getSize() const { return data.size(); }
139 size_t size() const { return getSize(); }
140
141 /// Returns true if this message contains no data.
142 bool empty() const { return data.empty(); }
143
144 /// Cast to a type. Throws if the size of the data does not match the size of
145 /// the message. The lifetime of the resulting pointer is tied to the lifetime
146 /// of this object.
147 template <typename T>
148 const T *as() const {
149 if (data.size() != sizeof(T))
150 throw std::runtime_error("Data size does not match type size. Size is " +
151 std::to_string(data.size()) + ", expected " +
152 std::to_string(sizeof(T)) + ".");
153 return reinterpret_cast<const T *>(data.data());
154 }
155
156 /// Cast from a type to its raw bytes.
157 template <typename T>
158 static MessageData from(T &t) {
159 return MessageData(reinterpret_cast<const uint8_t *>(&t), sizeof(T));
160 }
161
162 /// Convert the data to a hex string.
163 std::string toHex() const;
164
165private:
166 std::vector<uint8_t> data;
167};
168
169} // namespace esi
170
171std::ostream &operator<<(std::ostream &, const esi::ModuleInfo &);
172
173//===----------------------------------------------------------------------===//
174// Functions which should be in the standard library.
175//===----------------------------------------------------------------------===//
176
177namespace esi {
178std::string toHex(void *val);
179std::string toHex(uint64_t val);
180} // namespace esi
181
182#endif // ESI_COMMON_H
std::ostream & operator<<(std::ostream &, const esi::ModuleInfo &)
std::string toStr() const
Definition Manifest.cpp:739
friend std::ostream & operator<<(std::ostream &os, const AppIDPath &path)
Definition Manifest.cpp:765
AppIDPath operator+(const AppIDPath &b) const
Definition Manifest.cpp:726
AppIDPath parent() const
Definition Manifest.cpp:732
A logical chunk of data representing serialized data.
Definition Common.h:113
const std::vector< uint8_t > & getData() const
Get the data as a vector of bytes.
Definition Common.h:127
const uint8_t * getBytes() const
Definition Common.h:124
std::string toHex() const
Convert the data to a hex string.
Definition Common.cpp:22
~MessageData()=default
const T * as() const
Cast to a type.
Definition Common.h:148
std::vector< uint8_t > takeData()
Move the data out of this object.
Definition Common.h:135
size_t getSize() const
Get the size of the data in bytes.
Definition Common.h:138
MessageData()=default
Adopts the data vector buffer.
MessageData(const uint8_t *data, size_t size)
Definition Common.h:121
size_t size() const
Definition Common.h:139
MessageData(std::vector< uint8_t > &data)
Definition Common.h:119
MessageData(std::span< const uint8_t > data)
Definition Common.h:117
bool empty() const
Returns true if this message contains no data.
Definition Common.h:142
static MessageData from(T &t)
Cast from a type to its raw bytes.
Definition Common.h:158
std::vector< uint8_t > data
Definition Common.h:166
MessageData(std::vector< uint8_t > &&data)
Definition Common.h:120
Definition esi.py:1
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:108
std::string toHex(void *val)
Definition Common.cpp:37
std::map< std::string, ChannelAssignment > ChannelAssignments
Definition Common.h:97
bool operator<(const AppID &a, const AppID &b)
Definition Manifest.cpp:745
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:107
std::string name
Definition Common.h:36
bool operator!=(const AppID &other) const
Definition Common.h:45
std::string toString() const
Definition Common.h:48
bool operator==(const AppID &other) const
Definition Common.h:42
friend std::ostream & operator<<(std::ostream &os, const AppID &id)
Definition Manifest.cpp:759
std::optional< uint32_t > idx
Definition Common.h:37
AppID(const std::string &name, std::optional< uint32_t > idx=std::nullopt)
Definition Common.h:39
Details about how to connect to a particular channel.
Definition Common.h:90
std::map< std::string, std::any > implOptions
Implementation-specific options.
Definition Common.h:95
std::string type
The name of the type of connection.
Definition Common.h:93
std::any value
Definition Common.h:68
std::optional< const Type * > type
Definition Common.h:69
A description of a hardware client.
Definition Common.h:101
std::map< std::string, std::any > implOptions
Definition Common.h:105
AppIDPath relPath
Definition Common.h:102
ChannelAssignments channelAssignments
Definition Common.h:104
ServicePortDesc port
Definition Common.h:103
std::optional< std::string > version
Definition Common.h:75
std::optional< std::string > name
Definition Common.h:73
std::map< std::string, Constant > constants
Definition Common.h:78
std::optional< std::string > commitHash
Definition Common.h:77
std::optional< std::string > repo
Definition Common.h:76
std::map< std::string, std::any > extra
Definition Common.h:79
std::optional< std::string > summary
Definition Common.h:74
A description of a service port.
Definition Common.h:84
std::string name
Definition Common.h:85
std::string portName
Definition Common.h:86