libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Print::FormatCompiler< Frontend > Class Template Reference

共享编译期后端,把前端事件整理成最终字节流和参数表。 / Shared compile-time backend that turns frontend events into the final byte stream plus argument table. More...

#include <format_compile.hpp>

Data Structures

struct  ResultData
 当前后端实例产出的最终精确尺寸编译格式 / Final exact-size compiled format produced by this backend instance More...
 
struct  ScratchBuilder
 由前端文本/字段事件直接喂给的单遍临时构建器。 / One-pass scratch builder fed directly by frontend text/field events. More...
 

Static Public Member Functions

static consteval auto Compile ()
 把一个前端编译成最终字节流、参数表和 writer 摘要。 / Compiles one frontend into the final byte stream, argument table, and writer profile.
 

Private Types

using Error = typename Frontend::ErrorType
 
template<size_t BlobBytes, size_t ArgCount>
using Result = ResultData<BlobBytes, ArgCount>
 

Static Private Member Functions

static consteval void EmitByte (auto &data, size_t &out, uint8_t value)
 向临时或最终字节缓冲区追加一个原始字节 / Append one raw byte into a scratch or final byte buffer
 
template<typename T >
static consteval void EmitNative (auto &data, size_t &out, T value)
 向临时或最终字节缓冲区追加一个按本机字节序编码的 POD 值 / Append one native-endian POD value into a scratch or final byte buffer
 
template<typename T >
static consteval T ReadNative (const auto &data, size_t &pos)
 从临时字节缓冲区读取一个按本机字节序编码的 POD 值 / Read one native-endian POD value from a scratch byte buffer
 
static consteval auto Failed (Error error)
 构造只携带编译错误的最小失败结果。 / Builds the minimal failed result carrying only the compile error.
 
static consteval FormatProfile ProfileForOp (FormatOp op)
 指出某个操作码族需要打开哪一个 writer-profile 位。 / Says which writer-profile bit one opcode family needs.
 
static consteval FormatOp FastFieldOp (const FormatField &field)
 为一个字段选择仍能正确打印它的最小操作码。 / Chooses the smallest opcode that can still print this field correctly.
 
static consteval bool IsRawIntegerField (const FormatField &field)
 判断字段是否满足裸整数快路径条件。 / Tests whether one field matches the raw integer fast-path gate.
 
static consteval bool IsRawTextField (const FormatField &field)
 判断字段是否满足裸文本快路径条件。 / Tests whether one field matches the raw text fast-path gate.
 

Static Private Attributes

static constexpr size_t inline_text_limit = 2 * sizeof(size_t) - 1
 超过该长度后,字面文本会从内嵌模式切换为 TextRef / Maximum inline literal size before the backend spills to TextRef.
 
static constexpr size_t max_code_bytes = 3 * Frontend::SourceSize() + 1
 
static constexpr size_t max_text_pool_bytes = Frontend::SourceSize()
 
static constexpr size_t max_arg_count = Frontend::SourceSize()
 
static constexpr uint8_t unspecified_precision = std::numeric_limits<uint8_t>::max()
 

Detailed Description

template<typename Frontend>
class LibXR::Print::FormatCompiler< Frontend >

共享编译期后端,把前端事件整理成最终字节流和参数表。 / Shared compile-time backend that turns frontend events into the final byte stream plus argument table.

Frontend contract: The frontend must provide the members using ErrorType, static constexpr const char* SourceData(), static constexpr size_t SourceSize(), and static consteval ErrorType Walk(visitor). 前端协议: 前端必须提供如下成员:using ErrorType、static constexpr const char* SourceData()、static constexpr size_t SourceSize()、以及 static consteval ErrorType Walk(visitor)。

Walk(visitor) must emit source-ordered events through:

  • visitor.Text(offset, text_size)
  • visitor.Field(const FormatField&) Walk(visitor) 必须按源串顺序发射两类事件:
  • visitor.Text(offset, text_size)
  • visitor.Field(const FormatField&)

The backend only walks the frontend once. During that single walk it collects:

  • output bytecode
  • long literal-text storage
  • runtime argument metadata
  • the runtime executor profile

Then it packs those scratch buffers into the final exact-size result. 这个后端只遍历前端一次。那一遍里会同时收集:

  • 输出字节码
  • 长字面文本存储
  • 运行期参数元信息
  • 运行期执行器摘要

最后再把这些临时缓冲收拢成精确尺寸的最终结果。

Definition at line 51 of file format_compile.hpp.

Member Typedef Documentation

◆ Error

template<typename Frontend >
using LibXR::Print::FormatCompiler< Frontend >::Error = typename Frontend::ErrorType
private

Definition at line 58 of file format_compile.hpp.

◆ Result

template<typename Frontend >
template<size_t BlobBytes, size_t ArgCount>
using LibXR::Print::FormatCompiler< Frontend >::Result = ResultData<BlobBytes, ArgCount>
private

Definition at line 86 of file format_compile.hpp.

Member Function Documentation

◆ Compile()

template<typename Frontend >
static consteval auto LibXR::Print::FormatCompiler< Frontend >::Compile ( )
inlinestaticnodiscardconsteval

把一个前端编译成最终字节流、参数表和 writer 摘要。 / Compiles one frontend into the final byte stream, argument table, and writer profile.

Returns
Returns the final compiled result object, or a failed result that carries the first compile-time error. / 返回最终编译结果对象; 若失败则返回携带首个编译期错误的失败结果。

Definition at line 545 of file format_compile.hpp.

546 {
547 constexpr auto scratch = []() consteval
548 {
549 ScratchBuilder builder{};
550 builder.frontend_error = Frontend::Walk(builder);
551 return builder;
552 }();
553
554 if constexpr (scratch.frontend_error != Error::None)
555 {
556 return Failed(scratch.frontend_error);
557 }
558 else
559 {
560 return scratch.template Finish<scratch.FinalCodeBytes(), scratch.FinalBlobBytes(),
561 scratch.arg_count>();
562 }
563 }
static consteval auto Failed(Error error)
构造只携带编译错误的最小失败结果。 / Builds the minimal failed result carrying only the compile error.

◆ EmitByte()

template<typename Frontend >
static consteval void LibXR::Print::FormatCompiler< Frontend >::EmitByte ( auto & data,
size_t & out,
uint8_t value )
inlinestaticconstevalprivate

向临时或最终字节缓冲区追加一个原始字节 / Append one raw byte into a scratch or final byte buffer

Parameters
data目标字节缓冲区 / Target byte buffer
out当前写位置;会前进一个字节 / Current write position; advanced by one byte
value待追加的字节值 / Byte value to append

Definition at line 115 of file format_compile.hpp.

116 {
117 data[out++] = value;
118 }

◆ EmitNative()

template<typename Frontend >
template<typename T >
static consteval void LibXR::Print::FormatCompiler< Frontend >::EmitNative ( auto & data,
size_t & out,
T value )
inlinestaticconstevalprivate

向临时或最终字节缓冲区追加一个按本机字节序编码的 POD 值 / Append one native-endian POD value into a scratch or final byte buffer

Template Parameters
T待编码的 POD 类型 / POD type to encode
Parameters
data目标字节缓冲区 / Target byte buffer
out当前写位置;会前进 sizeof(T) / Current write position; advanced by sizeof(T)
value待追加的 POD 值 / POD value to append

Definition at line 130 of file format_compile.hpp.

131 {
132 auto bytes = std::bit_cast<std::array<uint8_t, sizeof(T)>>(value);
133 for (auto byte : bytes)
134 {
135 data[out++] = byte;
136 }
137 }

◆ Failed()

template<typename Frontend >
static consteval auto LibXR::Print::FormatCompiler< Frontend >::Failed ( Error error)
inlinestaticnodiscardconstevalprivate

构造只携带编译错误的最小失败结果。 / Builds the minimal failed result carrying only the compile error.

Parameters
errorCompile-time failure category. / 编译期失败类别。
Returns
Returns a minimal failed result object. / 返回最小失败结果对象。

Definition at line 165 of file format_compile.hpp.

166 {
167 Result<1, 0> result{};
168 result.compile_error = error;
169 return result;
170 }

◆ FastFieldOp()

template<typename Frontend >
static consteval FormatOp LibXR::Print::FormatCompiler< Frontend >::FastFieldOp ( const FormatField & field)
inlinestaticnodiscardconstevalprivate

为一个字段选择仍能正确打印它的最小操作码。 / Chooses the smallest opcode that can still print this field correctly.

Definition at line 210 of file format_compile.hpp.

211 {
212 const bool raw_integer_field = IsRawIntegerField(field);
213 const bool raw_text_field = IsRawTextField(field);
214
215 if (raw_integer_field && field.type == FormatType::Signed32 &&
216 field.pack == FormatPackKind::I32)
217 {
218 return FormatOp::I32Dec;
219 }
220
221 if (field.type == FormatType::Unsigned32 && field.pack == FormatPackKind::U32 &&
222 raw_integer_field)
223 {
224 return FormatOp::U32Dec;
225 }
226
227 if (raw_integer_field && field.type == FormatType::Binary32 &&
228 field.pack == FormatPackKind::U32)
229 {
230 return FormatOp::U32Binary;
231 }
232
233 if (raw_integer_field && field.type == FormatType::Octal32 &&
234 field.pack == FormatPackKind::U32)
235 {
236 return FormatOp::U32Octal;
237 }
238
239 if (raw_integer_field && field.type == FormatType::HexLower32 &&
240 field.pack == FormatPackKind::U32)
241 {
242 return FormatOp::U32HexLower;
243 }
244
245 if (raw_integer_field && field.type == FormatType::HexUpper32 &&
246 field.pack == FormatPackKind::U32)
247 {
248 return FormatOp::U32HexUpper;
249 }
250
251 if (raw_text_field && field.type == FormatType::Character &&
252 field.pack == FormatPackKind::Character)
253 {
254 return FormatOp::CharacterRaw;
255 }
256
257 if (raw_text_field && field.type == FormatType::String &&
258 field.pack == FormatPackKind::StringView)
259 {
260 return FormatOp::StringRaw;
261 }
262
263 if (field.type == FormatType::Unsigned32 && field.pack == FormatPackKind::U32 &&
264 field.flags == static_cast<uint8_t>(FormatFlag::ZeroPad) && field.fill == ' ' &&
265 field.width != 0 && field.precision == unspecified_precision)
266 {
267 return FormatOp::U32ZeroPadWidth;
268 }
269
270 return FormatOp::GenericField;
271 }
static consteval bool IsRawTextField(const FormatField &field)
判断字段是否满足裸文本快路径条件。 / Tests whether one field matches the raw text fast-path gate.
static consteval bool IsRawIntegerField(const FormatField &field)
判断字段是否满足裸整数快路径条件。 / Tests whether one field matches the raw integer fast-path gate.

◆ IsRawIntegerField()

template<typename Frontend >
static consteval bool LibXR::Print::FormatCompiler< Frontend >::IsRawIntegerField ( const FormatField & field)
inlinestaticnodiscardconstevalprivate

判断字段是否满足裸整数快路径条件。 / Tests whether one field matches the raw integer fast-path gate.

Definition at line 277 of file format_compile.hpp.

278 {
279 return field.flags == 0 && field.fill == ' ' && field.width == 0 &&
280 field.precision == unspecified_precision;
281 }

◆ IsRawTextField()

template<typename Frontend >
static consteval bool LibXR::Print::FormatCompiler< Frontend >::IsRawTextField ( const FormatField & field)
inlinestaticnodiscardconstevalprivate

判断字段是否满足裸文本快路径条件。 / Tests whether one field matches the raw text fast-path gate.

Definition at line 287 of file format_compile.hpp.

288 {
289 return (field.flags == 0 ||
290 field.flags == static_cast<uint8_t>(FormatFlag::LeftAlign)) &&
291 field.fill == ' ' && field.width == 0 &&
292 field.precision == unspecified_precision;
293 }

◆ ProfileForOp()

template<typename Frontend >
static consteval FormatProfile LibXR::Print::FormatCompiler< Frontend >::ProfileForOp ( FormatOp op)
inlinestaticnodiscardconstevalprivate

指出某个操作码族需要打开哪一个 writer-profile 位。 / Says which writer-profile bit one opcode family needs.

Parameters
op待分类的操作码 / Opcode to classify
Returns
对应的窄路径 profile 位;无需窄路径时返回 None / Required narrow-path profile bit, or None when no narrow path is needed

Definition at line 179 of file format_compile.hpp.

180 {
181 switch (op)
182 {
183 case FormatOp::U32Dec:
184 case FormatOp::U32ZeroPadWidth:
185 case FormatOp::I32Dec:
186 case FormatOp::U32Binary:
187 case FormatOp::U32Octal:
188 case FormatOp::U32HexLower:
189 case FormatOp::U32HexUpper:
190 return FormatProfile::NarrowInt;
191 case FormatOp::StringRaw:
192 case FormatOp::CharacterRaw:
193 return FormatProfile::TextArg;
194 case FormatOp::GenericField:
195 return FormatProfile::None;
196 case FormatOp::TextInline:
197 case FormatOp::TextRef:
198 case FormatOp::TextSpace:
199 case FormatOp::End:
200 return FormatProfile::None;
201 }
202
203 return FormatProfile::None;
204 }

◆ ReadNative()

template<typename Frontend >
template<typename T >
static consteval T LibXR::Print::FormatCompiler< Frontend >::ReadNative ( const auto & data,
size_t & pos )
inlinestaticnodiscardconstevalprivate

从临时字节缓冲区读取一个按本机字节序编码的 POD 值 / Read one native-endian POD value from a scratch byte buffer

Template Parameters
T待读取的 POD 类型 / POD type to decode
Parameters
data源字节缓冲区 / Source byte buffer
pos当前读取位置;会前进 sizeof(T) / Current read position; advanced by sizeof(T)
Returns
返回解码后的 POD 值 / Returns the decoded POD value

Definition at line 149 of file format_compile.hpp.

150 {
151 std::array<uint8_t, sizeof(T)> bytes{};
152 for (size_t i = 0; i < sizeof(T); ++i)
153 {
154 bytes[i] = data[pos++];
155 }
156 return std::bit_cast<T>(bytes);
157 }

Field Documentation

◆ inline_text_limit

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::inline_text_limit = 2 * sizeof(size_t) - 1
staticconstexprprivate

超过该长度后,字面文本会从内嵌模式切换为 TextRef / Maximum inline literal size before the backend spills to TextRef.

Definition at line 92 of file format_compile.hpp.

◆ max_arg_count

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::max_arg_count = Frontend::SourceSize()
staticconstexprprivate

Definition at line 105 of file format_compile.hpp.

◆ max_code_bytes

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::max_code_bytes = 3 * Frontend::SourceSize() + 1
staticconstexprprivate

Definition at line 103 of file format_compile.hpp.

◆ max_text_pool_bytes

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::max_text_pool_bytes = Frontend::SourceSize()
staticconstexprprivate

Definition at line 104 of file format_compile.hpp.

◆ unspecified_precision

template<typename Frontend >
uint8_t LibXR::Print::FormatCompiler< Frontend >::unspecified_precision = std::numeric_limits<uint8_t>::max()
staticconstexprprivate

Definition at line 106 of file format_compile.hpp.


The documentation for this class was generated from the following file: