CIRCT 22.0.0git
Loading...
Searching...
No Matches
FmtDescriptor.h
Go to the documentation of this file.
1//===- FmtDescriptor.h - Format descriptor for the ArcRuntime ------------===//
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// Declares FmtDescriptor, used by arcRuntimeFormat.
10//
11// This struct is created during compilation and serialized into the generated
12// LLVM IR. It is treated as opaque by the generated LLVM IR, and therefore can
13// use implementation-defined layout and padding if needed, as long as the
14// compiler used during compilation is that same as that used when compiling
15// the runtime.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef CIRCT_DIALECT_ARC_RUNTIME_FMTDESCRIPTOR_H
20#define CIRCT_DIALECT_ARC_RUNTIME_FMTDESCRIPTOR_H
21
22#include <cassert>
23#include <cstdint>
24#include <cstring>
25#include <string_view>
26#include <type_traits>
27
28namespace circt {
29namespace arc {
30namespace runtime {
31
32/// A format descriptor, to be given to arcRuntimeFormat.
33///
34/// arcRuntimeFormat takes an array of FmtDescriptor and a variadic argument
35/// list. Each FmtDescriptor describes how to format the corresponding
36/// argument. The array is terminated by a FmtDescriptor with action Action_End.
38 /// Default construction creates an end of string descriptor.
40 std::memset(this, 0, sizeof(*this));
42 }
43
44 /// Creates a literal string descriptor.
45 ///
46 /// width: The width of the literal string in characters.
47 ///
48 /// The string itself will be passed as a variadic argument (const char*).
49 static FmtDescriptor createLiteral(int64_t width) {
52 d.literal.width = width;
53 return d;
54 }
55
56 /// Creates a small literal string descriptor.
57 static FmtDescriptor createSmallLiteral(std::string_view str) {
58 assert(str.size() < sizeof(FmtDescriptor::smallLiteral.data));
61 std::strncpy(d.smallLiteral.data, str.data(),
62 sizeof(d.smallLiteral.data) - 1);
63 d.smallLiteral.data[sizeof(d.smallLiteral.data) - 1] = '\0';
64 return d;
65 }
66
67 /// Creates an integer descriptor.
68 ///
69 /// bitwidth: The bitwidth of the integer value.
70 /// radix: The radix to use for formatting. Must be one of {2, 8, 10, 16}.
71 /// isLeftAligned: Whether the value is left aligned.
72 /// specifierWidth: The minumum width of the output in characters.
73 /// isUpperCase: Whether to use uppercase hex letters.
74 /// isSigned: Whether to treat the value as signed.
75 ///
76 /// The integer value will be passed as a variadic argument by *pointer*.
77 static FmtDescriptor createInt(int32_t bitwidth, int8_t radix,
78 bool isLeftAligned, int32_t specifierWidth,
79 bool isUpperCase, bool isSigned) {
82 d.intFmt.bitwidth = bitwidth;
83 d.intFmt.radix = radix;
84 d.intFmt.isLeftAligned = isLeftAligned;
85 d.intFmt.specifierWidth = specifierWidth;
86 d.intFmt.isUpperCase = isUpperCase;
87 d.intFmt.isSigned = isSigned;
88 return d;
89 }
90
91 /// Creates a char descriptor.
92 ///
93 /// The character value will be passed as a variadic argument by value.
97 return d;
98 }
99
100 /// The action to take for this descriptor.
101 ///
102 /// We use uint64_t to ensure that the descriptor is always 16 bytes in size
103 /// with zero padding.
104 enum Action : uint64_t {
105 /// End of the format string, no action to take.
107 /// Prints a literal string.
109 /// Prints a literal string (small string optimization).
111 /// Prints an integer.
113 /// Prints a character (%c).
115 };
117
118 /// Integer formatting options.
119 struct IntFmt {
120 /// The bitwidth of the integer value.
121 int16_t bitwidth;
122 /// The minumum width of the output in characters.
124 /// The radix to use for formatting. Must be one of {2, 8, 10, 16}.
125 int8_t radix;
126 /// Padding character (NUL if no padding is desired).
128 /// Whether the value is left aligned.
130 /// Whether to use uppercase hex letters.
132 /// Whether to treat the value as signed.
134 };
135
136 /// Literal string formatting options.
137 struct LiteralFmt {
138 /// The width of the literal string in characters. Note that the string
139 /// itself is passed as a variadic argument, and may contain NUL characters.
140 int64_t width;
141 };
142
143 /// Literal string (small string optimization).
145 /// NUL-terminated string.
146 char data[8];
147 };
148
149 union {
153 };
154};
155
156static_assert(std::is_standard_layout_v<FmtDescriptor>,
157 "FmtDescriptor must be standard layout");
158
159} // namespace runtime
160} // namespace arc
161} // namespace circt
162
163#endif // CIRCT_DIALECT_ARC_RUNTIME_FMTDESCRIPTOR_H
assert(baseType &&"element must be base type")
Definition arc.py:1
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
bool isLeftAligned
Whether the value is left aligned.
bool isSigned
Whether to treat the value as signed.
bool isUpperCase
Whether to use uppercase hex letters.
char paddingChar
Padding character (NUL if no padding is desired).
int16_t specifierWidth
The minumum width of the output in characters.
int16_t bitwidth
The bitwidth of the integer value.
int8_t radix
The radix to use for formatting. Must be one of {2, 8, 10, 16}.
Literal string formatting options.
int64_t width
The width of the literal string in characters.
Literal string (small string optimization).
A format descriptor, to be given to arcRuntimeFormat.
FmtDescriptor()
Default construction creates an end of string descriptor.
static FmtDescriptor createChar()
Creates a char descriptor.
Action
The action to take for this descriptor.
@ Action_Char
Prints a character (c).
@ Action_Literal
Prints a literal string.
@ Action_End
End of the format string, no action to take.
@ Action_LiteralSmall
Prints a literal string (small string optimization).
static FmtDescriptor createSmallLiteral(std::string_view str)
Creates a small literal string descriptor.
static FmtDescriptor createInt(int32_t bitwidth, int8_t radix, bool isLeftAligned, int32_t specifierWidth, bool isUpperCase, bool isSigned)
Creates an integer descriptor.
static FmtDescriptor createLiteral(int64_t width)
Creates a literal string descriptor.