plcopen 0.22.0
载入中...
搜索中...
未找到
types.h
浏览该文件的文档.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5#include <cstring>
6
7#include "st/type_desc.h"
9
10// L0 value-type universe (approved st-l0-semantics 1.2): six elementary
11// types. TIME is int64 nanoseconds end-to-end; quantization happens only at
12// timer consumption (matrix 3.4). The detail helpers below define the wrap
13// arithmetic (matrix 1.8) shared verbatim by constant folding and the VM so
14// the two can never diverge.
15
16namespace plcopen::core::st
17{
18
19enum class Type : std::uint8_t
20{
21 bool_ = 0,
22 int_ = 1, // 16-bit signed
23 dint = 2, // 32-bit signed
24 real = 3, // IEEE-754 binary32
25 lreal = 4, // IEEE-754 binary64
26 time = 5, // int64 nanoseconds
27 // L1a scalar universe (approved st-l1a-semantics 2.1-2.3). Values are
28 // append-only: the six L0 codes stay stable.
29 sint = 6, // 8-bit signed
30 lint = 7, // 64-bit signed
31 usint = 8, // 8-bit unsigned
32 uint_ = 9, // 16-bit unsigned
33 udint = 10, // 32-bit unsigned
34 ulint = 11, // 64-bit unsigned
35 byte_ = 12, // 8-bit bit string (no numeric order)
36 word = 13, // 16-bit bit string
37 dword = 14, // 32-bit bit string
38 lword = 15, // 64-bit bit string
39 axis_ref = 16, // opaque host-bound AxisModel handle (L2a)
40 char_ = 17,
41 wchar = 18,
42 string_ = 19,
43 wstring = 20,
44 date = 21,
45 tod = 22,
46 dt = 23,
47 group_ref = 24, // opaque host-bound AxisGroup handle (L2c)
48};
49
50inline constexpr int kTypeCount = 25;
51
53{
54 switch(type) {
55 case Type::bool_: return builtin::bool_;
56 case Type::sint: return builtin::sint;
57 case Type::int_: return builtin::int_;
58 case Type::dint: return builtin::dint;
59 case Type::lint: return builtin::lint;
60 case Type::usint: return builtin::usint;
61 case Type::uint_: return builtin::uint_;
62 case Type::udint: return builtin::udint;
63 case Type::ulint: return builtin::ulint;
64 case Type::real: return builtin::real;
65 case Type::lreal: return builtin::lreal;
66 case Type::time: return builtin::time;
67 case Type::byte_: return builtin::byte_;
68 case Type::word: return builtin::word;
69 case Type::dword: return builtin::dword;
70 case Type::lword: return builtin::lword;
73 case Type::char_: return builtin::usint;
74 case Type::wchar: return builtin::udint;
75 case Type::date: return builtin::date;
76 case Type::tod: return builtin::tod;
77 case Type::dt: return builtin::dt;
78 case Type::string_:
79 case Type::wstring: return invalid_type_id;
80 }
81 return invalid_type_id;
82}
83
85{
86 switch(id) {
87 case builtin::bool_: return Type::bool_;
88 case builtin::sint: return Type::sint;
89 case builtin::int_: return Type::int_;
90 case builtin::dint: return Type::dint;
91 case builtin::lint: return Type::lint;
92 case builtin::usint: return Type::usint;
93 case builtin::uint_: return Type::uint_;
94 case builtin::udint: return Type::udint;
95 case builtin::ulint: return Type::ulint;
96 case builtin::real: return Type::real;
97 case builtin::lreal: return Type::lreal;
98 case builtin::time: return Type::time;
99 case builtin::date: return Type::date;
100 case builtin::tod: return Type::tod;
101 case builtin::dt: return Type::dt;
102 case builtin::byte_: return Type::byte_;
103 case builtin::word: return Type::word;
104 case builtin::dword: return Type::dword;
105 case builtin::lword: return Type::lword;
108 default: return Type::bool_;
109 }
110}
111
112constexpr bool is_signed_int(Type type)
113{
114 return type == Type::sint || type == Type::int_ || type == Type::dint ||
115 type == Type::lint;
116}
117
118constexpr bool is_unsigned_int(Type type)
119{
120 return type == Type::usint || type == Type::uint_ ||
122}
123
124constexpr bool is_integer(Type type)
125{
127}
128
129constexpr bool is_bitstring(Type type)
130{
131 return type == Type::byte_ || type == Type::word ||
133}
134
135constexpr bool is_real_family(Type type)
136{
137 return type == Type::real || type == Type::lreal;
138}
139
140constexpr int width_bits(Type type)
141{
142 switch(type) {
143 case Type::bool_: return 1;
144 case Type::sint:
145 case Type::usint:
146 case Type::byte_: return 8;
147 case Type::int_:
148 case Type::uint_:
149 case Type::word: return 16;
150 case Type::dint:
151 case Type::udint:
152 case Type::dword:
153 case Type::real: return 32;
154 default: return 64;
155 }
156}
157
158// Lossless implicit-widening whitelist (approved st-l1a-semantics 3.1):
159// signed chain, unsigned chain, narrow-unsigned to wider-signed,
160// REAL->LREAL, bit-string widening. Everything else is explicit.
161constexpr bool widens_to(Type from, Type to)
162{
163 if(from == to) {
164 return false;
165 }
166 if(is_signed_int(from) && is_signed_int(to)) {
167 return width_bits(from) < width_bits(to);
168 }
169 if(is_unsigned_int(from) && is_unsigned_int(to)) {
170 return width_bits(from) < width_bits(to);
171 }
172 if(is_unsigned_int(from) && is_signed_int(to)) {
173 return width_bits(from) < width_bits(to);
174 }
175 if(from == Type::real && to == Type::lreal) {
176 return true;
177 }
178 if(is_bitstring(from) && is_bitstring(to)) {
179 return width_bits(from) < width_bits(to);
180 }
181 return false;
182}
183
184// FB identity is generated from the checked-in PLCopen/IEC authorities.
185// RTC remains excluded because it depends on a wall-clock calendar source.
187
188inline constexpr int kFbTypeCount = static_cast<int>(FbType::count);
189
190namespace detail
191{
192
193inline std::uint64_t double_bits(double value)
194{
195 std::uint64_t bits = 0;
196 std::memcpy(&bits, &value, sizeof(bits));
197 return bits;
198}
199
200inline double bits_double(std::uint64_t bits)
201{
202 double value = 0.0;
203 std::memcpy(&value, &bits, sizeof(value));
204 return value;
205}
206
207inline std::int64_t wrap8(std::int64_t value)
208{
209 return static_cast<std::int8_t>(static_cast<std::uint8_t>(
210 static_cast<std::uint64_t>(value)));
211}
212
213inline std::int64_t wrap16(std::int64_t value)
214{
215 return static_cast<std::int16_t>(static_cast<std::uint16_t>(
216 static_cast<std::uint64_t>(value)));
217}
218
219inline std::int64_t wrap32(std::int64_t value)
220{
221 return static_cast<std::int32_t>(static_cast<std::uint32_t>(
222 static_cast<std::uint64_t>(value)));
223}
224
225inline std::int64_t wrap_add64(std::int64_t a, std::int64_t b)
226{
227 return static_cast<std::int64_t>(static_cast<std::uint64_t>(a) +
228 static_cast<std::uint64_t>(b));
229}
230
231inline std::int64_t wrap_sub64(std::int64_t a, std::int64_t b)
232{
233 return static_cast<std::int64_t>(static_cast<std::uint64_t>(a) -
234 static_cast<std::uint64_t>(b));
235}
236
237inline std::int64_t wrap_mul64(std::int64_t a, std::int64_t b)
238{
239 return static_cast<std::int64_t>(static_cast<std::uint64_t>(a) *
240 static_cast<std::uint64_t>(b));
241}
242
243// Deterministic double -> modular-u64 reduction for float->integer
244// conversions whose rounded value exceeds 64 bits (matrix 4.2/4.3 wrap
245// policy). fmod on a finite double is exact, so both platforms agree.
246inline std::uint64_t wrap_double_to_u64(double value)
247{
248 constexpr double kTwo64 = 18446744073709551616.0;
249 constexpr double kTwo63 = 9223372036854775808.0;
250 // fmod on a finite double is exact; the negative branch negates in the
251 // integer domain because m + 2^64 is NOT exact in double (spacing near
252 // 2^64 is 4096 -- small negatives would collapse to 2^64 and wrap to 0).
253 double m = std::fmod(value, kTwo64);
254 const bool negative = m < 0.0;
255 if(negative) {
256 m = -m;
257 }
258 std::uint64_t magnitude = 0;
259 if(m >= kTwo63) {
260 magnitude = static_cast<std::uint64_t>(m - kTwo63) +
261 0x8000000000000000ULL;
262 } else {
263 magnitude = static_cast<std::uint64_t>(m);
264 }
265 return negative ? (0ULL - magnitude) : magnitude;
266}
267
268// Canonical slot form (approved st-l1a-semantics 2.4): signed values are
269// sign-extended int64, unsigned and bit-string values zero-extended uint64.
270// Re-canonicalizing after every arithmetic step is what makes each width's
271// wrap semantics (matrix 1.8/4.2) hold, and what makes every whitelist
272// widening a runtime no-op.
273inline std::uint64_t canon(Type type, std::uint64_t raw)
274{
275 switch(type) {
276 case Type::bool_:
277 return raw ? 1 : 0;
278 case Type::sint:
279 return static_cast<std::uint64_t>(wrap8(static_cast<std::int64_t>(raw)));
280 case Type::int_:
281 return static_cast<std::uint64_t>(wrap16(static_cast<std::int64_t>(raw)));
282 case Type::dint:
283 return static_cast<std::uint64_t>(wrap32(static_cast<std::int64_t>(raw)));
284 case Type::usint:
285 case Type::byte_:
286 return raw & 0xFFULL;
287 case Type::uint_:
288 case Type::word:
289 return raw & 0xFFFFULL;
290 case Type::udint:
291 case Type::dword:
292 return raw & 0xFFFFFFFFULL;
293 default:
294 // lint / lint-width families / time: 64-bit native.
295 return raw;
296 }
297}
298
299} // namespace detail
300
301
302constexpr const char *to_string(Type type)
303{
304 switch(type) {
305 case Type::bool_: return "BOOL";
306 case Type::int_: return "INT";
307 case Type::dint: return "DINT";
308 case Type::real: return "REAL";
309 case Type::lreal: return "LREAL";
310 case Type::time: return "TIME";
311 case Type::sint: return "SINT";
312 case Type::lint: return "LINT";
313 case Type::usint: return "USINT";
314 case Type::uint_: return "UINT";
315 case Type::udint: return "UDINT";
316 case Type::ulint: return "ULINT";
317 case Type::byte_: return "BYTE";
318 case Type::word: return "WORD";
319 case Type::dword: return "DWORD";
320 case Type::lword: return "LWORD";
321 case Type::axis_ref: return "AXIS_REF";
322 case Type::char_: return "CHAR";
323 case Type::wchar: return "WCHAR";
324 case Type::string_: return "STRING";
325 case Type::wstring: return "WSTRING";
326 case Type::date: return "DATE";
327 case Type::tod: return "TOD";
328 case Type::dt: return "DT";
329 case Type::group_ref: return "GROUP_REF";
330 }
331 return "?";
332}
333
334constexpr const char *to_string(FbType type)
335{
336 const generated::StBindingFbMetadata *metadata =
338 return metadata != nullptr ? metadata->name.data() : "?";
339}
340
341} // namespace plcopen::core::st
std::uint64_t double_bits(double value)
std::int64_t wrap_add64(std::int64_t a, std::int64_t b)
std::uint64_t canon(Type type, std::uint64_t raw)
std::int64_t wrap_sub64(std::int64_t a, std::int64_t b)
std::int64_t wrap16(std::int64_t value)
std::int64_t wrap8(std::int64_t value)
std::uint64_t wrap_double_to_u64(double value)
std::int64_t wrap_mul64(std::int64_t a, std::int64_t b)
double bits_double(std::uint64_t bits)
std::int64_t wrap32(std::int64_t value)
constexpr const StBindingFbMetadata * st_binding_fb_metadata(StBindingFbType type) noexcept
constexpr bool is_integer(Type type)
std::uint32_t TypeId
constexpr int kTypeCount
定义 types.h:50
constexpr int width_bits(Type type)
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 kFbTypeCount
constexpr bool is_unsigned_int(Type type)
constexpr TypeId type_id(Type type)
定义 types.h:52
constexpr bool is_signed_int(Type type)
constexpr TypeId invalid_type_id
constexpr Type type_from_id(TypeId id)
定义 types.h:84
generated::StBindingFbType FbType
constexpr bool is_bitstring(Type type)