plcopen 0.22.0
载入中...
搜索中...
未找到
conv.h
浏览该文件的文档.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <string_view>
6
7#include "st/types.h"
8
9// L1a conversion matrix (approved st-l1a-semantics 4.x): the single source
10// of truth for every <SRC>_TO_<DST> cell. Sema resolves function names
11// through it, codegen lowers each semantic kind to opcodes, the test suite
12// enumerates it cell by cell, and the same enumeration is dumped for the
13// YAML three-way check (matrix 7.1). 15 scalar types pairwise (210 cells)
14// plus the two declared TIME cells and the TRUNC family.
15
16namespace plcopen::core::st
17{
18
19enum class ConvKind : std::uint8_t
20{
21 unsupported = 0, // cell explicitly absent (function does not exist)
22 identity, // canonical form already matches (pure type change)
23 wrap, // integer/bit-string re-canonicalization (matrix 4.2/4.5)
24 int_to_float, // signed canonical -> double (+ narrow for REAL)
25 uint_to_float, // unsigned/bit canonical -> double (+ narrow for REAL)
26 float_round, // round-half-even then wrap; NaN/Inf faults (matrix 4.3)
27 float_narrow, // LREAL -> REAL (IEEE nearest; overflow -> +-inf)
28 float_widen, // REAL -> LREAL (exact)
29 to_bool_int, // canonical != 0
30 to_bool_float, // double value != 0.0 (minus zero is FALSE)
31};
32
33// The 15 pairwise scalar types (TIME handled by its two declared cells).
39inline constexpr int kConvTypeCount = 15;
40
41// Semantics of the <from>_TO_<to> cell (matrix 4.2-4.7).
42constexpr ConvKind conv_kind(Type from, Type to)
43{
44 if(from == to) {
45 return ConvKind::unsupported; // no self-conversion function
46 }
47 if(from == Type::time || to == Type::time) {
48 // Only TIME_TO_LINT / LINT_TO_TIME exist (matrix 4.6).
49 if(from == Type::time && to == Type::lint) {
50 return ConvKind::identity;
51 }
52 if(from == Type::lint && to == Type::time) {
53 return ConvKind::identity;
54 }
56 }
57 const bool from_int = is_integer(from) || is_bitstring(from);
58 const bool to_int = is_integer(to) || is_bitstring(to);
59 if(to == Type::bool_) {
62 }
63 if(from == Type::bool_) {
64 if(to_int) {
65 return ConvKind::wrap; // canonical 0/1 always fits
66 }
68 }
69 if(from_int && to_int) {
70 // Widening within the canonical form is a pure type change.
71 return widens_to(from, to) ? ConvKind::identity : ConvKind::wrap;
72 }
73 if(from_int && is_real_family(to)) {
74 return is_unsigned_int(from) || is_bitstring(from)
77 }
78 if(is_real_family(from) && to_int) {
80 }
81 if(from == Type::lreal && to == Type::real) {
83 }
84 if(from == Type::real && to == Type::lreal) {
86 }
88}
89
90constexpr const char *to_string(ConvKind kind)
91{
92 switch(kind) {
93 case ConvKind::unsupported: return "unsupported";
94 case ConvKind::identity: return "identity";
95 case ConvKind::wrap: return "wrap";
96 case ConvKind::int_to_float: return "int_to_float";
97 case ConvKind::uint_to_float: return "uint_to_float";
98 case ConvKind::float_round: return "float_round";
99 case ConvKind::float_narrow: return "float_narrow";
100 case ConvKind::float_widen: return "float_widen";
101 case ConvKind::to_bool_int: return "to_bool_int";
102 case ConvKind::to_bool_float: return "to_bool_float";
103 }
104 return "?";
105}
106
108{
112 bool trunc = false; // TRUNC_* family (toward zero instead of half-even)
113};
114
115// Resolves a lowercase identifier to a conversion descriptor. Covers the
116// pairwise matrix, the two TIME cells, and TRUNC_INT/DINT/LINT (matrix
117// 4.8). Returns false when the name is not a conversion function.
118inline bool resolve_conversion(std::string_view lower, ConvDesc &desc)
119{
120 const auto type_by_lower = [](std::string_view text, Type &out) {
121 for(int i = 0; i < kTypeCount; ++i) {
122 const Type type = static_cast<Type>(i);
123 const char *name = to_string(type);
124 std::string lower_name;
125 for(const char *c = name; *c; ++c) {
126 lower_name.push_back(
127 static_cast<char>(*c >= 'A' && *c <= 'Z' ? *c - 'A' + 'a'
128 : *c));
129 }
130 if(text == lower_name) {
131 out = type;
132 return true;
133 }
134 }
135 return false;
136 };
137
138 if(lower == "trunc_int" || lower == "trunc_dint" || lower == "trunc_lint") {
139 desc.from = Type::lreal; // accepts REAL via widening
140 desc.to = lower == "trunc_int"
141 ? Type::int_
142 : (lower == "trunc_dint" ? Type::dint : Type::lint);
144 desc.trunc = true;
145 return true;
146 }
147 const std::size_t sep = lower.find("_to_");
148 if(sep == std::string_view::npos) {
149 return false;
150 }
151 Type from = Type::bool_;
152 Type to = Type::bool_;
153 if(!type_by_lower(lower.substr(0, sep), from) ||
154 !type_by_lower(lower.substr(sep + 4), to)) {
155 return false;
156 }
157 const ConvKind kind = conv_kind(from, to);
158 if(kind == ConvKind::unsupported) {
159 return false;
160 }
161 desc.from = from;
162 desc.to = to;
163 desc.kind = kind;
164 desc.trunc = false;
165 return true;
166}
167
168} // namespace plcopen::core::st
constexpr bool is_integer(Type type)
constexpr int kTypeCount
定义 types.h:50
constexpr bool is_real_family(Type type)
constexpr const char * to_string(ConvKind kind)
定义 conv.h:90
constexpr bool widens_to(Type from, Type to)
constexpr int kConvTypeCount
定义 conv.h:39
constexpr ConvKind conv_kind(Type from, Type to)
定义 conv.h:42
constexpr bool is_unsigned_int(Type type)
bool resolve_conversion(std::string_view lower, ConvDesc &desc)
定义 conv.h:118
constexpr Type kConvTypes[]
定义 conv.h:34
constexpr bool is_bitstring(Type type)