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