96 std::int64_t
utc_dt_ns() const noexcept {
return utc_dt_ns_; }
100 if(program_ !=
nullptr && fb_area_ !=
nullptr) {
101 for(
const FbInfo &
fb : program_->fbs) {
106 process_image_ =
nullptr;
108 visible_vars_ =
nullptr;
109 execution_vars_ =
nullptr;
110 sfc_runtime_ =
nullptr;
111 sfc_runner_ =
nullptr;
119 remaining_budget_ = 0;
120 scan_started_ =
false;
121 defer_process_image_publish_ =
false;
122 axis_targets_ =
nullptr;
123 group_targets_ =
nullptr;
124 path_tables_ =
nullptr;
125 path_descriptions_ =
nullptr;
126 cam_switch_tables_ =
nullptr;
127 cam_switch_outputs_ =
nullptr;
128 cam_track_options_ =
nullptr;
129 cam_tables_ =
nullptr;
130 position_profiles_ =
nullptr;
131 velocity_profiles_ =
nullptr;
132 acceleration_profiles_ =
nullptr;
133 kin_transforms_ =
nullptr;
135 debug_hooks_ =
nullptr;
142 std::size_t buffer_bytes, std::int64_t task_period_ns)
148 if(buffer ==
nullptr || task_period_ns <= 0) {
151 if((
reinterpret_cast<std::uintptr_t
>(buffer) & 7U) != 0) {
154 if(buffer_bytes <
program.required_bytes()) {
158 visible_vars_ = vars_;
159 execution_vars_ = vars_;
160 fb_area_ = buffer +
program.vars_bytes;
161 stack_ =
reinterpret_cast<std::uint64_t *
>(fb_area_ +
program.fb_bytes);
162 std::memset(buffer, 0,
program.required_bytes());
163 unsigned char *binding_storage =
164 buffer +
program.binding_storage_offset();
165 load_binding_storage(
program, binding_storage);
166 sfc_runner_ = ::new(
static_cast<void *
>(
168 sfc_runtime_ = buffer +
program.sfc_storage_offset();
169 if(
program.needs_debug_runtime_storage()) {
170 ::new(
static_cast<void *
>(
171 buffer +
program.debug_runtime_storage_offset()))
177 if(!
program.initial_data.empty()) {
178 std::memcpy(vars_,
program.initial_data.data(),
183 if(desc ==
nullptr ||
190 !valid_string_object(vars_ + var.
offset, *desc)) {
195 fb_init(
fb.type, fb_area_ +
fb.offset, task_period_ns);
197 task_period_ns_ = task_period_ns;
199 initialize_sfc_runtime();
205 remaining_budget_ = 0;
206 scan_started_ =
false;
211 std::size_t buffer_bytes, std::int64_t task_period_ns,
218 load(
program, buffer, buffer_bytes, task_period_ns);
224 unsigned char *buffer, std::size_t buffer_bytes,
225 std::int64_t task_period_ns)
231 if(!
program.programs.empty()) {
238 if(c >=
'A' && c <=
'Z') {
239 c =
static_cast<char>(c -
'A' +
'a');
248 selected = &candidate;
252 if(selected ==
nullptr) {
255 }
else if(!
program.program_name.empty()) {
259 if(c >=
'A' && c <=
'Z') {
260 c =
static_cast<char>(c -
'A' +
'a');
262 if(c !=
program.program_name[i]) {
271 return load(*selected, buffer, buffer_bytes, task_period_ns);
286 if(program_ ==
nullptr || name ==
nullptr) {
292 std::uint16_t axis_slot = 0;
293 std::uint16_t group_slot = 0;
294 for(
const VarInfo &var : program_->vars) {
303 if(!ascii_name_equal(name, var.
lower)) {
306 if(target.
pointer() ==
nullptr) {
309 if(target.
kind() != kind) {
313 slot >= axis_targets_->size()) ||
315 slot >= group_targets_->size())) {
318 std::uint64_t handle = 0;
319 std::memcpy(&handle, vars_ + var.
offset,
sizeof(handle));
324 (*axis_targets_)[slot] =
330 group->set_task_cycle_period_ns(task_period_ns_);
332 group->task_cycle_period_ns() != task_period_ns_) {
335 (*group_targets_)[slot] =
group;
337 handle =
static_cast<std::uint64_t
>(slot) + 1U;
338 std::memcpy(vars_ + var.
offset, &handle,
sizeof(handle));
347 return bind_named_handle(
349 [&](std::size_t slot) {
350 (*path_tables_)[slot] = table;
351 return BindingError::ok;
364 const std::size_t axis_count = waypoints[0].
target.
size;
368 for(std::size_t index = 0; index <
count; ++index) {
369 if(waypoints[index].target.size != axis_count ||
374 decoded.
waypoints[index] = waypoints[index];
377 return bind_named_handle(
379 path_descriptions_->size(), [&](std::size_t slot) {
380 (*path_descriptions_)[slot] = decoded;
381 return BindingError::ok;
388 switch(transform.
kind) {
390 if(transform.
kinematics ==
nullptr || transform.
pose !=
nullptr) {
395 if(transform.
pose ==
nullptr || transform.
kinematics !=
nullptr) {
405 return bind_named_handle(
407 kin_transforms_->size(), [&](std::size_t slot) {
408 (*kin_transforms_)[slot] = transform;
409 return BindingError::ok;
419 for(std::size_t index = 0; index <
count; ++index) {
421 if(
action.track_number == 0U ||
423 !std::isfinite(
action.on_position) ||
432 (
action.cam_switch_mode !=
435 (
action.cam_switch_mode ==
440 action.duration_ns <= 0)) {
444 return bind_named_handle(
446 cam_switch_tables_->size(), [&](std::size_t slot) {
447 (*cam_switch_tables_)[slot] = {data, count};
459 return bind_named_handle(
461 cam_switch_outputs_->size(), [&](std::size_t slot) {
462 (*cam_switch_outputs_)[slot] = {data, count};
475 return bind_named_handle(
477 cam_track_options_->size(), [&](std::size_t slot) {
478 (*cam_track_options_)[slot] = {data, count};
484 std::size_t count,
bool periodic)
489 return bind_named_handle(
491 [&](std::size_t slot) {
492 (*cam_tables_)[slot] = view;
493 return BindingError::ok;
503 if(count < 1U || count > decoded.
segments.size()) {
506 for(std::size_t index = 0; index < count; ++index) {
507 std::int64_t cycles = 0;
508 if(entries[index].time_ns <= 0 ||
510 entries[index].time_ns, task_period_ns_, cycles) ||
511 !std::isfinite(entries[index].position) ||
512 !std::isfinite(entries[index].velocity) ||
513 !std::isfinite(entries[index].acceleration) ||
514 !std::isfinite(entries[index].deceleration) ||
515 !std::isfinite(entries[index].jerk)) {
527 decoded.
count = count;
529 *position_profiles_, decoded);
538 if(count < 1U || count > decoded.
segments.size()) {
541 for(std::size_t index = 0; index < count; ++index) {
542 std::int64_t cycles = 0;
543 if(entries[index].time_ns <= 0 ||
545 entries[index].time_ns, task_period_ns_, cycles) ||
546 !std::isfinite(entries[index].velocity) ||
547 !std::isfinite(entries[index].acceleration) ||
548 !std::isfinite(entries[index].deceleration) ||
549 !std::isfinite(entries[index].jerk)) {
559 decoded.
count = count;
561 *velocity_profiles_, decoded);
570 if(count < 1U || count > decoded.
segments.size()) {
573 for(std::size_t index = 0; index < count; ++index) {
574 std::int64_t cycles = 0;
575 if(entries[index].time_ns <= 0 ||
577 entries[index].time_ns, task_period_ns_, cycles) ||
578 !std::isfinite(entries[index].acceleration)) {
581 decoded.
segments[index].duration_cycles = cycles;
584 decoded.
count = count;
586 *acceleration_profiles_, decoded);
614 defer_process_image_publish_ = defer;
618 return remaining_budget_;
622 return static_cast<std::uint32_t
>(instruction_pc_);
626 return program_ ==
nullptr ?
"" : program_->program_name.c_str();
630 std::size_t mapping)
noexcept
632 debug_hooks_ = hooks;
634 if(hooks !=
nullptr && runtime !=
nullptr) {
635 runtime->
mapping =
static_cast<std::uint32_t
>(mapping);
644 return runtime->last_entry;
651 return runtime->last_instruction_id;
658 unsigned char *destination)
const noexcept
660 if(destination ==
nullptr || visible_vars_ ==
nullptr ||
661 program_ ==
nullptr || offset > program_->vars_bytes ||
662 size > program_->vars_bytes - offset)
664 std::memcpy(destination, visible_vars_ + offset, size);
669 const char *step_name,
670 bool &active)
const noexcept
673 if(program_ ==
nullptr || network_name ==
nullptr ||
674 step_name ==
nullptr || sfc_runtime_ ==
nullptr)
677 if(!ascii_name_equal(network_name, network.
lower))
continue;
678 for(std::size_t index = 0; index < network.
steps.size(); ++index) {
679 if(ascii_name_equal(step_name, network.
steps[index].lower)) {
680 const std::uint32_t offset =
684 active = sfc_runtime_[offset + index] != 0;
694 std::size_t step)
const noexcept
696 if(program_ ==
nullptr || sfc_runtime_ ==
nullptr ||
697 network >= program_->sfc_networks.size() ||
698 step >= program_->sfc_networks[network].steps.size())
700 return sfc_runtime_[program_->sfc_networks[network]
701 .committed_active_offset +
step] != 0;
705 std::size_t capacity)
noexcept
707 if((records ==
nullptr) != (capacity == 0))
710 sfc_runner_->trace = records;
711 sfc_runner_->trace_capacity = capacity;
712 sfc_runner_->trace_size = 0;
713 sfc_runner_->trace_dropped = 0;
714 sfc_runner_->trace_scan_begin_size = 0;
715 sfc_runner_->trace_scan_begin_dropped = 0;
722 return sfc_runner_->trace_dropped;
727 if(program_ ==
nullptr || network_name ==
nullptr ||
running())
730 if(!ascii_name_equal(network_name, network.
lower))
continue;
731 reset_sfc_network(network);
739 return sfc_runner_->fault_network_index;
743 return sfc_runner_->fault_element_index;
747 return sfc_runner_->fault_element_kind;
756 if(process_image_ !=
nullptr)
757 process_image_->begin_scan(*program_, vars_);
758 if(!program_->sfc_networks.empty()) {
759 execution_vars_ = sfc_runtime_;
761 execution_vars_ = vars_;
763 scan_started_ =
true;
768 remaining_budget_ = budget;
769 sfc_runner_->active_region =
nullptr;
770 sfc_runner_->phase = program_->sfc_networks.empty()
774 sfc_runner_->network_index = 0;
775 sfc_runner_->item_index = 0;
776 sfc_runner_->subitem_index = 0;
777 sfc_runner_->work_remaining = 0;
778 sfc_runner_->reducer = 0;
779 sfc_runner_->action_suppressed =
false;
780 sfc_runner_->fault_network_index = invalid_sfc_index;
781 sfc_runner_->fault_element_index = invalid_sfc_index;
783 sfc_runner_->trace_scan_begin_size = sfc_runner_->trace_size;
784 sfc_runner_->trace_scan_begin_dropped = sfc_runner_->trace_dropped;
791 if(sfc_commit_started()) rollback_sfc_commit();
792 sfc_runner_->trace_size = sfc_runner_->trace_scan_begin_size;
793 sfc_runner_->trace_dropped = sfc_runner_->trace_scan_begin_dropped;
799 remaining_budget_ = 0;
800 execution_vars_ = vars_;
801 visible_vars_ = vars_;
802 sfc_runner_->active_region =
nullptr;
804 sfc_runner_->subitem_index = 0;
805 sfc_runner_->work_remaining = 0;
806 sfc_runner_->reducer = 0;
807 sfc_runner_->action_suppressed =
false;
817 std::int64_t executed = 0;
818 return resume(std::numeric_limits<std::int64_t>::max(), executed);
834 const std::uint8_t *code =
nullptr;
835 std::size_t size = 0;
836 const std::uint64_t *constants =
nullptr;
837 std::size_t constant_count = 0;
838 std::int32_t stack_limit = 0;
839 const auto select_code = [&]() {
840 if(sfc_runner_->active_region !=
nullptr) {
841 code = sfc_runner_->active_region->code.data();
842 size = sfc_runner_->active_region->code.size();
843 constants = sfc_runner_->active_region->constants.data();
844 constant_count = sfc_runner_->active_region->constants.size();
845 stack_limit = sfc_runner_->active_region->stack_slots;
847 code = program_->code.data();
848 size = program_->code.size();
849 constants = program_->constants.data();
850 constant_count = program_->constants.size();
851 stack_limit = program_->stack_slots;
855 std::size_t &pc = pc_;
856 std::int32_t &sp = sp_;
857 std::int64_t &remaining = remaining_budget_;
858 const auto charge_operation = [&remaining](std::uint32_t cost) {
859 const std::int64_t extra = cost > 1U
860 ?
static_cast<std::int64_t
>(cost - 1U)
862 if(remaining < extra) {
871 if(!program_->sfc_networks.empty() &&
878 advance_sfc_runner();
879 if(sfc_runner_->active_region !=
nullptr) {
899 instruction_pc_ = pc;
900 const Op op =
static_cast<Op>(code[pc++]);
903 std::uint32_t probe = 0;
904 if(!rd32(code, size, pc, probe))
906 const SourceMap *map = sfc_runner_->active_region ==
nullptr
907 ? &program_->source_map
908 : &sfc_runner_->active_region->source_map;
909 if(probe >= map->
entries.size())
912 if(runtime ==
nullptr)
918 static_cast<std::uint32_t
>(instruction_pc_);
919 if(debug_hooks_ !=
nullptr &&
920 debug_hooks_->on_probe(
922 static_cast<std::uint32_t
>(instruction_pc_))) {
923 pc = instruction_pc_;
933 if(!program_->sfc_networks.empty()) {
934 if(sfc_runner_->active_region !=
nullptr)
939 }
else if(process_image_ !=
nullptr) {
940 process_image_->commit_scan(
941 *program_, vars_, !defer_process_image_publish_);
947 std::uint16_t index = 0;
948 if(!rd16(code, size, pc, index) || index >= constant_count ||
952 stack_[sp++] = constants[index];
956 std::uint32_t offset = 0;
957 std::uint32_t
type = 0;
958 std::uint64_t value = 0;
959 if(!rd32(code, size, pc, offset) ||
960 !rd32(code, size, pc,
type) || sp >= stack_limit ||
961 !load_value(offset,
type, value)) {
964 stack_[sp++] = value;
968 std::uint32_t offset = 0;
969 std::uint32_t
type = 0;
970 if(!rd32(code, size, pc, offset) ||
971 !rd32(code, size, pc,
type) || sp < 1 ||
972 !store_value(offset,
type, stack_[sp - 1])) {
980 std::uint32_t offset = 0;
981 std::uint32_t
type = 0;
982 if(!rd32(code, size, pc, offset) ||
983 !rd32(code, size, pc,
type) || pc >= size) {
986 const std::uint8_t count = code[pc++];
987 const int needed =
static_cast<int>(count) +
991 sp -
static_cast<int>(count) >= stack_limit)) {
994 const int index_base = sp -
static_cast<int>(count);
995 for(std::uint8_t i = 0; i < count; ++i) {
996 std::uint64_t lower_raw = 0;
997 std::uint64_t upper_raw = 0;
998 std::uint64_t stride = 0;
999 if(!rd64(code, size, pc, lower_raw) ||
1000 !rd64(code, size, pc, upper_raw) ||
1001 !rd64(code, size, pc, stride)) {
1004 const std::int64_t lower =
1005 static_cast<std::int64_t
>(lower_raw);
1006 const std::int64_t upper =
1007 static_cast<std::int64_t
>(upper_raw);
1008 const std::int64_t value = as_i64(
1009 stack_[index_base +
static_cast<int>(i)]);
1010 if(value < lower || value > upper) {
1013 const std::uint64_t delta =
1014 static_cast<std::uint64_t
>(value - lower) * stride;
1015 if(delta > std::numeric_limits<std::uint32_t>::max() -
1019 offset +=
static_cast<std::uint32_t
>(delta);
1022 std::uint64_t value = 0;
1024 if(!load_value(offset,
type, value)) {
1027 stack_[sp++] = value;
1029 const std::uint64_t value =
1030 stack_[index_base - 1];
1031 if(!store_value(offset,
type, value)) {
1039 std::uint32_t destination = 0;
1040 std::uint32_t source = 0;
1041 std::uint32_t count = 0;
1042 if(!rd32(code, size, pc, destination) ||
1043 !rd32(code, size, pc, source) ||
1044 !rd32(code, size, pc, count) ||
1045 destination > program_->vars_bytes ||
1046 count > program_->vars_bytes - destination ||
1047 source > program_->vars_bytes ||
1048 count > program_->vars_bytes - source) {
1051 std::memmove(execution_vars_ + destination,
1052 execution_vars_ + source, count);
1057 std::uint32_t destination = 0;
1058 std::uint32_t destination_type = 0;
1059 std::uint32_t source = 0;
1060 std::uint32_t source_type = 0;
1061 if(!rd32(code, size, pc, destination) ||
1062 !rd32(code, size, pc, destination_type) ||
1063 !rd32(code, size, pc, source) ||
1064 !rd32(code, size, pc, source_type)) {
1067 const TypeDesc *dst = program_->types.get(destination_type);
1068 const TypeDesc *src = program_->types.get(source_type);
1069 if(dst ==
nullptr || src ==
nullptr || dst->
kind != src->
kind ||
1072 destination > program_->vars_bytes ||
1073 dst->
size > program_->vars_bytes - destination) {
1076 const std::uint32_t operation_cost = std::max(
1078 if(!charge_operation(operation_cost)) {
1081 const unsigned char *source_bytes =
nullptr;
1083 if(source > program_->vars_bytes ||
1084 src->
size > program_->vars_bytes - source) {
1087 source_bytes = execution_vars_ + source;
1089 if(source > program_->string_constants.size() ||
1090 4U > program_->string_constants.size() - source) {
1093 source_bytes = program_->string_constants.data() + source;
1095 const std::uint32_t length = read_le32(source_bytes);
1099 const std::uint64_t width =
1101 const std::uint64_t count = 4U + width * length;
1103 count > program_->string_constants.size() - source) {
1106 if(!valid_string_object(source_bytes, *src)) {
1115 std::memset(execution_vars_ + destination, 0,
1116 static_cast<std::size_t
>(dst->
size));
1117 std::memmove(execution_vars_ + destination, source_bytes,
1118 static_cast<std::size_t
>(count));
1125 const std::uint8_t comparison = code[pc++];
1126 const unsigned char *objects[2] = {
nullptr,
nullptr};
1127 const TypeDesc *descs[2] = {
nullptr,
nullptr};
1128 for(
int i = 0; i < 2; ++i) {
1132 const std::uint8_t constant = code[pc++];
1133 std::uint32_t offset = 0;
1134 std::uint32_t
id = 0;
1135 if(!rd32(code, size, pc, offset) ||
1136 !rd32(code, size, pc,
id)) {
1139 descs[i] = program_->types.get(
id);
1140 if(descs[i] ==
nullptr ||
1146 if(offset > program_->string_constants.size() ||
1147 4U > program_->string_constants.size() - offset) {
1150 objects[i] = program_->string_constants.data() + offset;
1151 const std::uint32_t length = read_le32(objects[i]);
1153 static_cast<std::uint64_t
>(length) *
1155 if(length > descs[i]->
string.capacity ||
1157 program_->string_constants.size() - offset) {
1161 if(offset > program_->vars_bytes ||
1162 descs[i]->
size > program_->vars_bytes - offset) {
1165 objects[i] = execution_vars_ + offset;
1168 if(descs[0]->kind != descs[1]->kind || sp >= stack_limit) {
1171 const std::uint32_t operation_cost = std::max(
1172 descs[0]->
string.capacity, descs[1]->
string.capacity);
1173 if(!charge_operation(operation_cost)) {
1176 const int order = compare_strings(objects[0], *descs[0],
1177 objects[1], *descs[1]);
1181 bool result =
false;
1182 switch(comparison) {
1183 case 8: result = order == 0;
break;
1184 case 9: result = order != 0;
break;
1185 case 10: result = order < 0;
break;
1186 case 11: result = order > 0;
break;
1187 case 12: result = order <= 0;
break;
1188 case 13: result = order >= 0;
break;
1191 stack_[sp++] = result ? 1U : 0U;
1195 std::uint32_t offset = 0;
1196 std::uint32_t
id = 0;
1197 if(!rd32(code, size, pc, offset) ||
1198 !rd32(code, size, pc,
id) || sp < 1) {
1201 const TypeDesc *desc = program_->types.get(
id);
1202 if(desc ==
nullptr ||
1205 offset > program_->vars_bytes ||
1206 desc->
size > program_->vars_bytes - offset) {
1212 if(!valid_string_object(execution_vars_ + offset, *desc)) {
1215 const std::int64_t index = as_i64(stack_[sp - 1]);
1216 const std::uint32_t length = read_le32(execution_vars_ + offset);
1217 if(index < 1 ||
static_cast<std::uint64_t
>(index) > length) {
1221 stack_[sp - 1] = read_le32(
1222 execution_vars_ + offset + 4U +
1223 (
static_cast<std::size_t
>(index) - 1U) * 4U);
1225 stack_[sp - 1] = execution_vars_[offset + 4U +
1226 static_cast<std::uint32_t
>(index) - 1U];
1234 if(stack_[sp - 1] > 0x10FFFFU ||
1235 (stack_[sp - 1] >= 0xD800U &&
1236 stack_[sp - 1] <= 0xDFFFU)) {
1241 if(pc >= size || sp < 2) {
1244 const std::uint8_t kind = code[pc++];
1245 const std::int64_t
right = as_i64(stack_[--sp]);
1246 const std::int64_t
left = as_i64(stack_[sp - 1]);
1247 std::int64_t value = 0;
1249 const std::int64_t delta = kind == 0U ?
right : -
right;
1250 value =
left + delta;
1251 if(value < std::numeric_limits<std::int32_t>::min() ||
1252 value > std::numeric_limits<std::int32_t>::max()) {
1255 }
else if(kind <= 3U) {
1256 constexpr std::int64_t day = 86400000000000LL;
1257 const std::int64_t delta =
right % day;
1258 value = kind == 2U ?
left + delta :
left - delta;
1263 }
else if(kind <= 5U) {
1266 std::numeric_limits<std::int64_t>::max() -
right) ||
1268 std::numeric_limits<std::int64_t>::min() -
right))) ||
1270 ((right < 0 && left >
1271 std::numeric_limits<std::int64_t>::max() +
right) ||
1273 std::numeric_limits<std::int64_t>::min() +
right)))) {
1280 stack_[sp - 1] =
static_cast<std::uint64_t
>(value);
1295 const std::int64_t b = as_i64(stack_[--sp]);
1296 const std::int64_t a = as_i64(stack_[--sp]);
1297 std::int64_t value = 0;
1324 stack_[sp++] = as_u64(value);
1335 const std::int64_t b = as_i64(stack_[--sp]);
1336 const std::int64_t a = as_i64(stack_[--sp]);
1340 std::int64_t value = 0;
1355 stack_[sp++] = as_u64(value);
1364 const std::int64_t a = as_i64(stack_[--sp]);
1365 const std::int64_t value =
1369 stack_[sp++] = as_u64(value);
1389 default: value = a / b;
break;
1420 default: value = a / b;
break;
1440 const bool b = stack_[--sp] != 0;
1441 const bool a = stack_[--sp] != 0;
1450 stack_[sp++] = value ? 1 : 0;
1457 stack_[sp - 1] = stack_[sp - 1] == 0 ? 1 : 0;
1470 const std::int64_t b = as_i64(stack_[--sp]);
1471 const std::int64_t a = as_i64(stack_[--sp]);
1479 default: value = a >= b;
break;
1481 stack_[sp++] = value ? 1 : 0;
1503 default: value = a >= b;
break;
1505 stack_[sp++] = value ? 1 : 0;
1510 std::uint32_t target = 0;
1511 if(!rd32(code, size, pc, target) || target > size) {
1518 std::uint32_t target = 0;
1519 if(!rd32(code, size, pc, target) || target > size || sp < 1) {
1522 if(stack_[--sp] == 0) {
1529 std::uint32_t offset = 0;
1530 std::uint64_t value = 0;
1531 if(!rd32(code, size, pc, offset) ||
1532 !load_raw64(offset, value)) {
1535 if(as_i64(value) == 0) {
1541 std::uint32_t ctrl = 0;
1542 std::uint32_t to = 0;
1543 std::uint32_t by = 0;
1544 std::uint32_t
type = 0;
1545 std::uint64_t control_raw = 0;
1546 std::uint64_t bound_raw = 0;
1547 std::uint64_t step_raw = 0;
1548 if(!rd32(code, size, pc, ctrl) || !rd32(code, size, pc, to) ||
1549 !rd32(code, size, pc, by) || !rd32(code, size, pc,
type) ||
1550 sp >= stack_limit || !load_value(ctrl,
type, control_raw) ||
1551 !load_value(to,
type, bound_raw) ||
1552 !load_value(by,
type, step_raw)) {
1555 const std::int64_t control = as_i64(control_raw);
1556 const std::int64_t bound = as_i64(bound_raw);
1557 const std::int64_t
step = as_i64(step_raw);
1558 const bool keep_running =
1559 step >= 0 ? control <= bound : control >= bound;
1560 stack_[sp++] = keep_running ? 1 : 0;
1565 std::uint32_t ctrl = 0;
1566 std::uint32_t by = 0;
1567 std::uint32_t
type = 0;
1568 std::uint64_t control = 0;
1569 std::uint64_t
step = 0;
1570 if(!rd32(code, size, pc, ctrl) || !rd32(code, size, pc, by) ||
1571 !rd32(code, size, pc,
type) ||
1572 !load_value(ctrl,
type, control) ||
1577 as_i64(control), as_i64(
step));
1578 const std::uint64_t stored = as_u64(
1581 if(!store_value(ctrl,
type, stored)) {
1588 std::uint16_t
fb = 0;
1589 if(!rd16(code, size, pc,
fb) || pc >= size || sp < 1 ||
1590 fb >= program_->fbs.size()) {
1593 const std::uint8_t pin = code[pc++];
1594 const FbInfo &info = program_->fbs[
fb];
1598 std::int64_t value = as_i64(stack_[--sp]);
1603 if(!resolve_tagged_reference_payload(
1604 pin_desc.
type_id, value, tagged) ||
1614 if(!resolve_sequence_payload(pin_desc.
type_id, value,
1622 const Type pin_type = pin_desc.
type;
1623 if(!resolve_reference_payload(pin_type, pin_desc.
type_id,
1634 std::uint16_t
fb = 0;
1635 std::uint32_t offset = 0;
1637 if(!rd16(code, size, pc,
fb) || pc >= size ||
1638 fb >= program_->fbs.size()) {
1641 const std::uint8_t pin = code[pc++];
1642 if(!rd32(code, size, pc, offset) ||
1643 !rd32(code, size, pc,
type_id)) {
1646 const FbInfo &info = program_->fbs[
fb];
1653 offset > program_->vars_bytes ||
1654 desc->
size > program_->vars_bytes - offset ||
1655 !charge_operation(
static_cast<std::uint32_t
>(desc->
size)) ||
1657 execution_vars_ + offset,
type_id,
1658 static_cast<std::uint32_t
>(desc->
size))) {
1664 std::uint16_t
fb = 0;
1665 if(!rd16(code, size, pc,
fb) ||
fb >= program_->fbs.size()) {
1668 const FbInfo &info = program_->fbs[
fb];
1673 std::uint16_t
fb = 0;
1674 if(!rd16(code, size, pc,
fb) || pc >= size ||
1675 sp >= stack_limit ||
fb >= program_->fbs.size()) {
1678 const std::uint8_t pin = code[pc++];
1679 const FbInfo &info = program_->fbs[
fb];
1687 std::int64_t handle = 0;
1691 !encode_tagged_reference_payload(
1692 pin_desc.
type_id, tagged, handle)) {
1695 stack_[sp++] = as_u64(handle);
1700 std::int64_t handle = 0;
1703 !encode_sequence_payload(pin_desc.
type_id, sequence,
1707 stack_[sp++] = as_u64(handle);
1710 std::uint64_t value = 0;
1715 std::int64_t resolved = as_i64(value);
1716 if(!encode_reference_payload(pin_desc.
type, pin_desc.
type_id,
1720 stack_[sp++] = as_u64(resolved);
1724 std::uint16_t
fb = 0;
1725 std::uint32_t offset = 0;
1727 if(!rd16(code, size, pc,
fb) || pc >= size ||
1728 fb >= program_->fbs.size()) {
1731 const std::uint8_t pin = code[pc++];
1732 if(!rd32(code, size, pc, offset) ||
1733 !rd32(code, size, pc,
type_id)) {
1736 const FbInfo &info = program_->fbs[
fb];
1741 offset > program_->vars_bytes ||
1742 desc->
size > program_->vars_bytes - offset ||
1743 !charge_operation(
static_cast<std::uint32_t
>(desc->
size)) ||
1745 execution_vars_ + offset,
type_id,
1746 static_cast<std::uint32_t
>(desc->
size))) {
1758 const std::uint8_t
sub = code[pc++];
1764 const std::int64_t a = as_i64(stack_[sp - 1]);
1773 const std::uint64_t rb = stack_[--sp];
1774 const std::uint64_t ra = stack_[--sp];
1775 std::uint64_t bits = 0;
1778 case 0: bits = ra + rb;
break;
1779 case 1: bits = ra - rb;
break;
1780 case 2: bits = ra * rb;
break;
1797 const std::int64_t a = as_i64(ra);
1798 const std::int64_t b = as_i64(rb);
1799 std::int64_t value = 0;
1814 value = b == -1 ? 0 : a % b;
1819 bits =
static_cast<std::uint64_t
>(value);
1826 if(pc >= size || sp < 2) {
1829 const std::uint8_t
sub = code[pc++];
1830 const std::uint64_t b = stack_[--sp];
1831 const std::uint64_t a = stack_[--sp];
1834 case 0: value = a < b;
break;
1835 case 1: value = a > b;
break;
1836 case 2: value = a <= b;
break;
1837 case 3: value = a >= b;
break;
1841 stack_[sp++] = value ? 1 : 0;
1851 const std::uint64_t b = stack_[--sp];
1852 const std::uint64_t a = stack_[--sp];
1859 if(pc >= size || sp < 1) {
1868 if(pc >= size || sp < 2) {
1871 const std::uint8_t
sub = code[pc++];
1872 const std::uint64_t
rs = stack_[--sp];
1873 const std::int64_t t = as_i64(stack_[--sp]);
1874 std::uint64_t bits = 0;
1877 bits =
static_cast<std::uint64_t
>(
1881 const std::int64_t
s = as_i64(
rs);
1885 bits =
static_cast<std::uint64_t
>(
1892 const double value =
sub == 2
1893 ?
static_cast<double>(t) *
s
1894 :
static_cast<double>(t) /
s;
1895 if(!std::isfinite(value)) {
1904 stack_[sp++] = bits;
1909 if(pc >= size || sp < 2) {
1915 const double value = std::pow(a, b);
1919 static_cast<float>(value)))
1925 if(pc >= size || sp < 1) {
1937 static_cast<double>(as_i64(stack_[sp - 1])));
1945 static_cast<double>(stack_[sp - 1]));
1958 if(pc >= size || sp < 1) {
1963 if(!std::isfinite(value)) {
1968 : std::nearbyint(value);
1977 stack_[sp - 1] = stack_[sp - 1] != 0 ? 1 : 0;
1989 std::uint32_t
id = 0;
1990 if(!rd32(code, size, pc,
id) || sp < 1) {
1993 const TypeDesc *desc = program_->types.get(
id);
1994 if(desc ==
nullptr ||
1999 const std::uint64_t bits = stack_[sp - 1];
2010 const std::int64_t value = as_i64(bits);
2023 std::uint16_t count = 0;
2024 std::uint16_t dynamic_count = 0;
2025 if(!rd16(code, size, pc, count) ||
2026 !rd16(code, size, pc, dynamic_count) || count > 256U ||
2027 sp <
static_cast<std::int32_t
>(dynamic_count)) {
2030 if(!charge_operation(count)) {
2037 if(remaining <= 0) {
2040 std::uint32_t destinations[256] = {};
2041 std::uint32_t sources[256] = {};
2042 std::uint32_t bytes[256] = {};
2043 const std::int32_t index_base =
2044 sp -
static_cast<std::int32_t
>(dynamic_count);
2045 std::int32_t index_at = index_base;
2046 for(std::uint16_t item = 0; item < count; ++item) {
2047 std::uint32_t source = 0;
2048 std::uint32_t source_type = 0;
2049 std::uint32_t destination = 0;
2050 std::uint32_t destination_type = 0;
2051 if(!rd32(code, size, pc, source) ||
2052 !rd32(code, size, pc, source_type) ||
2053 !rd32(code, size, pc, destination) ||
2054 !rd32(code, size, pc, destination_type) || pc >= size) {
2057 const std::uint8_t indices = code[pc++];
2058 if(index_at + indices > sp) {
2061 for(std::uint8_t i = 0; i < indices; ++i) {
2062 std::uint64_t lower_raw = 0;
2063 std::uint64_t upper_raw = 0;
2064 std::uint64_t stride = 0;
2065 if(!rd64(code, size, pc, lower_raw) ||
2066 !rd64(code, size, pc, upper_raw) ||
2067 !rd64(code, size, pc, stride)) {
2070 const std::int64_t lower =
2071 static_cast<std::int64_t
>(lower_raw);
2072 const std::int64_t upper =
2073 static_cast<std::int64_t
>(upper_raw);
2074 const std::int64_t value = as_i64(stack_[index_at++]);
2075 if(value < lower || value > upper) {
2078 const std::uint64_t delta =
2079 static_cast<std::uint64_t
>(value - lower) * stride;
2080 if(delta > std::numeric_limits<std::uint32_t>::max() -
2084 destination +=
static_cast<std::uint32_t
>(delta);
2086 if(source_type != destination_type) {
2089 const TypeDesc *desc = program_->types.get(destination_type);
2090 if(desc ==
nullptr || desc->
size == 0 ||
2091 source > program_->vars_bytes ||
2092 desc->
size > program_->vars_bytes - source ||
2093 destination > program_->vars_bytes ||
2094 desc->
size > program_->vars_bytes - destination) {
2097 sources[item] = source;
2098 destinations[item] = destination;
2099 bytes[item] =
static_cast<std::uint32_t
>(desc->
size);
2100 for(std::uint16_t prior = 0; prior < item; ++prior) {
2101 const std::uint64_t prior_end =
2102 static_cast<std::uint64_t
>(destinations[prior]) +
2104 const std::uint64_t current_end =
2105 static_cast<std::uint64_t
>(destination) +
2107 if(destination < prior_end &&
2108 destinations[prior] < current_end) {
2113 if(index_at != sp) {
2116 for(std::uint16_t item = 0; item < count; ++item) {
2117 std::memmove(execution_vars_ + destinations[item],
2118 execution_vars_ + sources[item], bytes[item]);
2127 if(stack_[sp - 1] == 0) {
2132 if(pc >= size || sp >= stack_limit) {
2135 const std::uint8_t constant = code[pc++];
2136 std::uint32_t offset = 0;
2137 std::uint32_t
id = 0;
2138 if(!rd32(code, size, pc, offset) ||
2139 !rd32(code, size, pc,
id)) {
2142 const TypeDesc *desc = program_->types.get(
id);
2147 const unsigned char *
object =
nullptr;
2149 if(offset > program_->string_constants.size() ||
2150 desc->
size > program_->string_constants.size() - offset) {
2153 object = program_->string_constants.data() + offset;
2155 if(offset > program_->vars_bytes ||
2156 desc->
size > program_->vars_bytes - offset) {
2159 object = execution_vars_ + offset;
2161 if(!valid_string_object(
object, *desc) ||
2162 !charge_operation(read_le32(
object))) {
2163 return latch(valid_string_object(
object, *desc)
2167 stack_[sp++] = read_le32(
object);
2174 const std::uint8_t argc = code[pc++];
2176 if(argc == 0 || sp < argc ||
2180 if(!charge_operation(argc)) {
2183 const int base = sp - argc;
2185 auto real = [
this](std::uint64_t raw) {
2188 auto put_real = [
this,
type](
double value) {
2192 auto less = [floating, &real,
type](std::uint64_t a,
2194 if(floating)
return real(a) < real(b);
2196 return static_cast<std::int64_t
>(a) <
2197 static_cast<std::int64_t
>(b);
2199 auto equal = [floating, &real](std::uint64_t a,
2201 return floating ? real(a) == real(b) : a == b;
2203 std::uint64_t value = stack_[
base];
2206 if(floating) value = put_real(std::fabs(real(value)));
2207 else if(
static_cast<std::int64_t
>(value) < 0)
2210 const double x = real(value);
2225 value = put_real(y);
2230 value = put_real(std::pow(real(stack_[
base]),
2231 real(stack_[
base + 1])));
2234 for(
int i =
base + 1; i < sp; ++i) {
2236 ? less(stack_[i], value) : less(value, stack_[i]);
2237 if(take) value = stack_[i];
2239 }
else if(floating) {
2240 double y = real(value);
2243 for(
int i =
base + 1; i < sp; ++i)
2245 ? y + real(stack_[i]) : y * real(stack_[i]);
2247 const double rhs = real(stack_[
base + 1]);
2250 : std::fmod(y, rhs);
2252 value = put_real(y);
2254 auto apply = [
type](std::uint64_t a, std::uint64_t b,
2263 const std::int64_t x =
static_cast<std::int64_t
>(a);
2264 const std::int64_t y =
static_cast<std::int64_t
>(b);
2265 if(x == std::numeric_limits<std::int64_t>::min() && y == -1)
2272 for(
int i =
base + 1; i < sp; ++i)
2273 value = apply(value, stack_[i], function);
2276 if(less(stack_[
base + 2], stack_[
base]))
2278 value = less(stack_[
base + 1], stack_[
base]) ? stack_[
base]
2279 : less(stack_[
base + 2], stack_[
base + 1])
2280 ? stack_[
base + 2] : stack_[
base + 1];
2282 value = stack_[
base] ? stack_[
base + 2] : stack_[
base + 1];
2284 const std::int64_t index =
static_cast<std::int64_t
>(stack_[
base]);
2285 if(index < 0 || index >= argc - 1)
2287 value = stack_[
base + 1 +
static_cast<int>(index)];
2291 for(
int i =
base; i + 1 < sp && result; ++i) {
2292 const bool lt = less(stack_[i], stack_[i + 1]);
2293 const bool eq =
equal(stack_[i], stack_[i + 1]);
2304 value = result ? 1U : 0U;
2307 const std::int64_t count =
static_cast<std::int64_t
>(stack_[
base + 1]);
2310 const std::uint64_t mask = width == 64 ? ~0ULL : ((1ULL << width) - 1U);
2311 const std::uint64_t
input = stack_[
base] & mask;
2313 value =
static_cast<std::uint64_t
>(count) >= width ? 0U
2315 ? (input << count) & mask : input >> count;
2317 const unsigned rotate =
static_cast<unsigned>(count % width);
2318 value = rotate == 0 ?
input
2320 ? ((
input << rotate) | (
input >> (width - rotate))) & mask
2321 : ((
input >> rotate) | (
input << (width - rotate))) & mask;
2324 constexpr std::int64_t day = 86400000000000LL;
2325 const std::int64_t a =
static_cast<std::int64_t
>(stack_[
base]);
2326 const std::int64_t b =
static_cast<std::int64_t
>(stack_[
base + 1]);
2327 std::int64_t out = 0;
2328 auto add_overflow = [](std::int64_t x, std::int64_t y, std::int64_t &z) {
2329 if((y > 0 && x > std::numeric_limits<std::int64_t>::max() - y) ||
2330 (y < 0 && x < std::numeric_limits<std::int64_t>::min() - y))
return true;
2331 z = x + y;
return false;
2333 auto sub_overflow = [](std::int64_t x, std::int64_t y,
2335 if((y > 0 && x < std::numeric_limits<std::int64_t>::min() + y) ||
2336 (y < 0 && x > std::numeric_limits<std::int64_t>::max() + y))
return true;
2337 z = x - y;
return false;
2339 auto mul_overflow = [](std::int64_t x, std::int64_t y,
2341 if(x == 0 || y == 0) { z = 0;
return false; }
2342 if((x == -1 && y == std::numeric_limits<std::int64_t>::min()) ||
2343 (y == -1 && x == std::numeric_limits<std::int64_t>::min()))
return true;
2345 if((y > 0 && x > std::numeric_limits<std::int64_t>::max() / y) ||
2346 (y < 0 && y < std::numeric_limits<std::int64_t>::min() / x))
return true;
2348 if((y > 0 && x < std::numeric_limits<std::int64_t>::min() / y) ||
2349 (y < 0 && x < std::numeric_limits<std::int64_t>::max() / y))
return true;
2351 z = x * y;
return false;
2360 if(sub_overflow(a, b, out))
2364 out = ((a + b % day) % day + day) % day;
break;
2366 out = ((a - b % day) % day + day) % day;
break;
2368 { std::int64_t days = 0;
2369 if(sub_overflow(a, b, days) || mul_overflow(days, day, out))
2373 if(mul_overflow(a, b, out))
2378 if(a == std::numeric_limits<std::int64_t>::min() && b == -1)
2382 { std::int64_t date_ns = 0;
2383 if(mul_overflow(a, day, date_ns) ||
2384 add_overflow(date_ns, b, out))
2389 value =
static_cast<std::uint64_t
>(out);
2391 stack_[
base] = value;
2398 const std::uint8_t argc = code[pc++];
2399 std::uint32_t destination = 0;
2400 std::uint32_t destination_id = 0;
2401 if(argc == 0 || argc > 32 || !rd32(code, size, pc, destination) ||
2402 !rd32(code, size, pc, destination_id))
2404 const unsigned char *objects[32] = {};
2406 bool string_arg[32] = {};
2407 int scalar_count = 0;
2408 std::uint32_t capacities[32] = {};
2409 std::uint8_t string_count = 0;
2410 for(std::uint8_t i = 0; i < argc; ++i) {
2412 string_arg[i] = code[pc++] != 0;
2413 if(!string_arg[i]) { ++scalar_count;
continue; }
2415 const bool constant = code[pc++] != 0;
2416 std::uint32_t offset = 0,
id = 0;
2417 if(!rd32(code, size, pc, offset) || !rd32(code, size, pc,
id))
2419 descs[i] = program_->types.get(
id);
2420 if(descs[i] ==
nullptr ||
2423 capacities[string_count++] =
static_cast<std::uint32_t
>(
2426 if(offset > program_->string_constants.size() ||
2427 4U > program_->string_constants.size() - offset)
2429 objects[i] = program_->string_constants.data() + offset;
2430 const std::uint32_t units = read_le32(objects[i]);
2431 const std::uint64_t bytes = 4U +
static_cast<std::uint64_t
>(units) *
2433 if(bytes > program_->string_constants.size() - offset)
2436 if(offset > program_->vars_bytes || descs[i]->
size > program_->vars_bytes - offset)
2438 objects[i] = execution_vars_ + offset;
2440 if(!valid_string_object(objects[i], *descs[i]))
2443 const auto valid_signature = [&]() {
2444 const auto string_at = [&](std::uint8_t index) {
2445 return index < argc && string_arg[index];
2447 const auto scalar_at = [&](std::uint8_t index) {
2448 return index < argc && !string_arg[index];
2452 return argc == 1U && string_at(0);
2455 return argc == 2U && string_at(0) && scalar_at(1);
2458 return argc == 3U && string_at(0) && scalar_at(1) &&
2461 if(argc < 2U)
return false;
2462 for(std::uint8_t i = 0; i < argc; ++i)
2463 if(!string_at(i))
return false;
2466 return argc == 3U && string_at(0) && string_at(1) &&
2469 return argc == 4U && string_at(0) && string_at(1) &&
2470 scalar_at(2) && scalar_at(3);
2472 return argc == 2U && string_at(0) && string_at(1);
2479 if(!valid_signature() ||
2481 (destination == std::numeric_limits<std::uint32_t>::max()))
2483 for(std::uint8_t i = 0; i < argc; ++i) {
2484 if(string_arg[i] && (objects[i] ==
nullptr || descs[i] ==
nullptr))
2488 destination == std::numeric_limits<std::uint32_t>::max()
2489 ? nullptr : program_->types.get(destination_id);
2490 if(destination != std::numeric_limits<std::uint32_t>::max() &&
2491 (destination_desc ==
nullptr ||
2496 const std::uint32_t destination_capacity =
2497 destination_desc ==
nullptr ? 0U
2498 :
static_cast<std::uint32_t
>(
2501 function, capacities, string_count, destination_capacity);
2502 if(sp < scalar_count || !charge_operation(operation_cost))
2505 const int scalar_base = sp - scalar_count;
2507 auto scalar = [&](std::uint8_t argument) -> std::int64_t {
2509 for(std::uint8_t i = 0; i < argument; ++i)
2510 if(!string_arg[i]) ++at;
2511 return static_cast<std::int64_t
>(stack_[scalar_base + at]);
2513 auto scalar_length = [&](
const unsigned char *object,
2516 const std::uint32_t bytes = read_le32(
object);
2517 std::uint32_t at = 0, count = 0, codepoint = 0;
2518 while(at < bytes && next_utf8(
object + 4U, bytes, at, codepoint)) ++count;
2521 auto unit_at = [&](
const unsigned char *object,
const TypeDesc &desc,
2522 std::uint32_t scalar_index) {
2524 const std::uint32_t bytes = read_le32(
object);
2525 std::uint32_t at = 0, count = 0, codepoint = 0;
2526 while(count < scalar_index && at < bytes) {
2527 next_utf8(
object + 4U, bytes, at, codepoint); ++count;
2532 std::int64_t answer = 0;
2534 answer = scalar_length(objects[0], *descs[0]);
2536 const std::uint32_t hay_units = read_le32(objects[0]);
2537 const std::uint32_t needle_units = read_le32(objects[1]);
2538 answer = needle_units == 0 ? 1 : 0;
2539 const std::uint32_t width =
2541 for(std::uint32_t pos = 0;
2542 answer == 0 && pos + needle_units <= hay_units;
2545 for(std::uint32_t unit = 0;
2546 unit < needle_units && match; ++unit) {
2547 const std::uint32_t hay_value = width == 4U
2548 ? read_le32(objects[0] + 4U +
2549 (
static_cast<std::size_t
>(pos) +
2551 : objects[0][4U + pos + unit];
2552 const std::uint32_t needle_value = width == 4U
2553 ? read_le32(objects[1] + 4U +
2554 static_cast<std::size_t
>(unit) *
2556 : objects[1][4U + unit];
2557 match = hay_value == needle_value;
2561 answer =
static_cast<std::int64_t
>(pos) + 1;
2563 std::uint32_t
byte = 0, scalar_pos = 0,
2565 while(
byte < pos && next_utf8(
2566 objects[0] + 4U, hay_units,
byte,
2567 codepoint)) ++scalar_pos;
2569 static_cast<std::int64_t
>(scalar_pos) + 1;
2576 stack_[sp++] =
static_cast<std::uint64_t
>(answer);
2579 const TypeDesc *dst = destination_desc;
2580 if(dst ==
nullptr || destination == std::numeric_limits<std::uint32_t>::max() ||
2581 destination > program_->vars_bytes || dst->
size > program_->vars_bytes - destination)
2583 std::array<unsigned char, 16388>
output{};
2584 std::uint32_t output_units = 0;
2585 bool capacity_exceeded =
false;
2586 const std::uint32_t destination_width =
2588 const std::uint32_t destination_bytes =
2591 auto append = [&](
const unsigned char *object,
const TypeDesc &desc,
2592 std::uint32_t first, std::uint32_t last) {
2593 const std::uint32_t
begin = unit_at(
object, desc, first);
2594 const std::uint32_t end = unit_at(
object, desc, last);
2595 const std::uint32_t bytes = end -
begin;
2596 if(bytes > destination_bytes - output_units ||
2597 4U + output_units + bytes >
output.size()) {
2598 capacity_exceeded =
true;
2601 std::memcpy(
output.data() + 4U + output_units,
object + 4U +
begin, bytes);
2602 output_units += bytes;
2605 const std::uint32_t source_length = scalar_length(objects[0], *descs[0]);
2609 for(std::uint8_t i = 0; i < argc && valid; ++i)
2610 valid = append(objects[i], *descs[i], 0, scalar_length(objects[i], *descs[i]));
2613 const std::int64_t count = scalar(1);
2614 if(count < 0) valid =
false;
2615 else valid = append(objects[0], *descs[0], 0,
2616 std::min<std::uint32_t>(source_length,
static_cast<std::uint32_t
>(count)));
2620 const std::int64_t count = scalar(1);
2621 if(count < 0) valid =
false;
2622 else {
const std::uint32_t take = std::min<std::uint32_t>(source_length,
static_cast<std::uint32_t
>(count));
2623 valid = append(objects[0], *descs[0], source_length - take, source_length); }
2627 const std::int64_t count = scalar(1), position = scalar(2);
2628 if(count < 0 || position < 1 || position >
static_cast<std::int64_t
>(source_length) + 1) valid =
false;
2629 else {
const std::uint32_t
begin =
static_cast<std::uint32_t
>(position - 1);
2630 valid = append(objects[0], *descs[0],
begin,
2631 std::min<std::uint32_t>(source_length,
begin +
static_cast<std::uint32_t
>(count))); }
2635 const std::int64_t position = scalar(2);
2636 if(position < 1 || position >
static_cast<std::int64_t
>(source_length) + 1) valid =
false;
2637 else {
const std::uint32_t at =
static_cast<std::uint32_t
>(position - 1);
2638 valid = append(objects[0], *descs[0], 0, at) &&
2639 append(objects[1], *descs[1], 0, scalar_length(objects[1], *descs[1])) &&
2640 append(objects[0], *descs[0], at, source_length); }
2644 const std::int64_t count = scalar(1), position = scalar(2);
2645 if(count < 0 || position < 1 || position >
static_cast<std::int64_t
>(source_length) + 1) valid =
false;
2646 else {
const std::uint32_t at =
static_cast<std::uint32_t
>(position - 1);
2647 const std::uint32_t end = std::min<std::uint32_t>(source_length, at +
static_cast<std::uint32_t
>(count));
2648 valid = append(objects[0], *descs[0], 0, at) && append(objects[0], *descs[0], end, source_length); }
2652 const std::int64_t count = scalar(2), position = scalar(3);
2653 if(count < 0 || position < 1 || position >
static_cast<std::int64_t
>(source_length) + 1) valid =
false;
2654 else {
const std::uint32_t at =
static_cast<std::uint32_t
>(position - 1);
2655 const std::uint32_t end = std::min<std::uint32_t>(source_length, at +
static_cast<std::uint32_t
>(count));
2656 valid = append(objects[0], *descs[0], 0, at) &&
2657 append(objects[1], *descs[1], 0, scalar_length(objects[1], *descs[1])) &&
2658 append(objects[0], *descs[0], end, source_length); }
2663 if(!valid)
return latch(capacity_exceeded
2666 const std::uint32_t width = destination_width;
2667 const std::uint32_t stored_units = width == 4U ? output_units / 4U : output_units;
2670 output[0] =
static_cast<unsigned char>(stored_units);
2671 output[1] =
static_cast<unsigned char>(stored_units >> 8U);
2672 output[2] =
static_cast<unsigned char>(stored_units >> 16U);
2673 output[3] =
static_cast<unsigned char>(stored_units >> 24U);
2674 std::memset(execution_vars_ + destination, 0, dst->
size);
2675 std::memcpy(execution_vars_ + destination,
output.data(),
2692 return program_ ? program_->vars.size() : 0;
2697 if(!program_ || index >= program_->vars.size()) {
2700 return &program_->vars[index];
2706 if(!program_ || name ==
nullptr) {
2709 for(std::size_t i = 0; i < program_->vars.size(); ++i) {
2710 const char *lower = program_->vars[i].lower.c_str();
2713 for(; name[k] !=
'\0' && lower[k] !=
'\0'; ++k) {
2715 if(c >=
'A' && c <=
'Z') {
2716 c =
static_cast<char>(c -
'A' +
'a');
2723 if(match && name[k] ==
'\0' && lower[k] ==
'\0') {
2724 return static_cast<int>(i);
2732 if(!program_ || index >= program_->vars.size()) {
2735 std::uint64_t value = 0;
2736 return load_value_at(visible_vars_, program_->vars[index].offset,
2737 program_->vars[index].type_id, value)
2744 if(!program_ || index >= program_->vars.size()) {
2747 std::uint64_t value = 0;
2748 return load_value_at(visible_vars_, program_->vars[index].offset,
2749 program_->vars[index].type_id, value)
2760 static constexpr std::uint32_t invalid_sfc_index =
2761 std::numeric_limits<std::uint32_t>::max();
2762 static constexpr std::size_t sfc_block_bytes = 16;
2763 static constexpr std::uint8_t action_direct = 1U;
2764 static constexpr std::uint8_t action_store = 2U;
2765 static constexpr std::uint8_t action_reset = 4U;
2766 static constexpr std::uint8_t action_expire = 8U;
2768 unsigned char *sfc_at(std::uint32_t offset)
const noexcept
2770 return sfc_runtime_ + offset;
2773 bool sfc_commit_started() const noexcept
2775 return sfc_runner_->phase == SfcRunnerPhase::commit_vars ||
2776 sfc_runner_->phase == SfcRunnerPhase::commit_active ||
2777 sfc_runner_->phase == SfcRunnerPhase::commit_blocks ||
2778 sfc_runner_->phase == SfcRunnerPhase::commit_actions ||
2779 sfc_runner_->phase == SfcRunnerPhase::finish;
2782 void rollback_sfc_commit() noexcept
2784 if(program_ ==
nullptr || sfc_runtime_ ==
nullptr)
return;
2785 std::memcpy(vars_, sfc_at(program_->sfc_committed_shadow_offset()),
2786 program_->vars_bytes);
2787 for(
const SfcNetworkInfo &network : program_->sfc_networks) {
2788 std::memcpy(sfc_at(network.committed_active_offset),
2789 sfc_at(network.shadow_active_offset),
2790 network.steps.size());
2791 std::memcpy(sfc_at(network.committed_block_offset),
2792 sfc_at(network.shadow_block_offset),
2793 network.action_blocks.size() * sfc_block_bytes);
2794 std::memcpy(sfc_at(network.committed_action_offset),
2795 sfc_at(network.shadow_action_offset),
2796 network.actions.size());
2800 static std::int64_t block_elapsed(
const unsigned char *state)
noexcept
2802 std::int64_t result = 0;
2803 std::memcpy(&result, state,
sizeof(result));
2807 static void block_elapsed(
unsigned char *state,
2808 std::int64_t value)
noexcept
2810 std::memcpy(state, &value,
sizeof(value));
2813 static std::int64_t saturating_add(std::int64_t left,
2814 std::int64_t right)
noexcept
2816 if(right > 0 && left > std::numeric_limits<std::int64_t>::max() - right)
2817 return std::numeric_limits<std::int64_t>::max();
2821 void reset_sfc_network(
const SfcNetworkInfo &network)
noexcept
2823 std::memset(sfc_at(network.committed_active_offset), 0,
2824 network.steps.size());
2825 std::memset(sfc_at(network.staged_active_offset), 0,
2826 network.steps.size());
2827 std::memset(sfc_at(network.firing_offset), 0,
2828 network.transitions.size());
2829 std::memset(sfc_at(network.committed_block_offset), 0,
2830 network.action_blocks.size() * sfc_block_bytes);
2831 std::memset(sfc_at(network.staged_block_offset), 0,
2832 network.action_blocks.size() * sfc_block_bytes);
2833 std::memset(sfc_at(network.committed_action_offset), 0,
2834 network.actions.size());
2835 std::memset(sfc_at(network.staged_action_offset), 0,
2836 network.actions.size());
2837 std::memset(sfc_at(network.direct_action_offset), 0,
2838 network.actions.size());
2839 for(std::size_t index = 0; index < network.steps.size(); ++index) {
2840 if(!network.steps[index].initial)
continue;
2841 sfc_at(network.committed_active_offset)[index] = 1U;
2842 sfc_at(network.staged_active_offset)[index] = 1U;
2846 void initialize_sfc_runtime() noexcept
2848 if(program_ ==
nullptr || sfc_runtime_ ==
nullptr ||
2849 program_->sfc_runtime_bytes == 0)
return;
2850 std::memset(sfc_runtime_, 0, program_->sfc_runtime_bytes);
2851 for(
const SfcNetworkInfo &network : program_->sfc_networks)
2852 reset_sfc_network(network);
2855 bool transition_eligible(
const SfcNetworkInfo &network,
2856 std::size_t transition_index)
const noexcept
2859 network.transitions[transition_index];
2862 const unsigned char *
active =
2863 sfc_at(network.committed_active_offset);
2864 for(std::uint16_t source :
transition.sources)
2865 if(source >= network.steps.size() || active[source] == 0)
2867 const unsigned char *firing = sfc_at(network.firing_offset);
2868 for(std::size_t prior = 0; prior < transition_index; ++prior) {
2869 if(firing[prior] == 0)
continue;
2870 for(std::uint16_t source :
transition.sources) {
2871 if(std::find(network.transitions[prior].sources.begin(),
2872 network.transitions[prior].sources.end(),
2874 network.transitions[prior].sources.end())
return false;
2880 void begin_sfc_regions() noexcept
2882 sfc_runner_->phase = SfcRunnerPhase::stage_active;
2883 sfc_runner_->network_index = 0;
2884 sfc_runner_->item_index = 0;
2885 sfc_runner_->subitem_index = 0;
2886 sfc_runner_->active_region =
nullptr;
2887 sfc_runner_->work_remaining = 0;
2888 sfc_runner_->reducer = 0;
2891 void finish_sfc_region() noexcept
2893 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
2894 sfc_runner_->active_region =
nullptr;
2897 SfcNetworkInfo
const &network =
2898 program_->sfc_networks[sfc_runner_->network_index];
2899 if(sfc_runner_->phase == SfcRunnerPhase::transitions &&
2900 sfc_runner_->item_index < network.transitions.size()) {
2901 const SfcRegionInfo &condition =
2902 network.transitions[sfc_runner_->item_index].condition;
2903 std::uint64_t
raw = 0;
2904 if(load_value(condition.result_offset,
type_id(Type::bool_), raw) &&
2906 sfc_at(network.firing_offset)[sfc_runner_->item_index] = 1U;
2909 ++sfc_runner_->item_index;
2910 sfc_runner_->work_remaining = 0;
2911 sfc_runner_->active_region =
nullptr;
2914 void update_sfc_qualifier_block(SfcNetworkInfo
const &network,
2915 std::size_t index)
noexcept
2917 unsigned char *
active = sfc_at(network.staged_active_offset);
2918 const unsigned char *old_active =
2919 sfc_at(network.committed_active_offset);
2920 const SfcActionBlockInfo &block = network.action_blocks[index];
2921 unsigned char *state = sfc_at(network.staged_block_offset) +
2922 index * sfc_block_bytes;
2923 const bool signal = block.step < network.steps.size() &&
2925 const bool activation = block.step < network.steps.size() &&
2926 (signal || old_active[block.step] != 0);
2927 const bool previous = state[9] != 0;
2928 const bool rising = activation && !previous;
2929 std::int64_t elapsed = block_elapsed(state);
2930 bool pending = state[8] != 0;
2931 bool latched = state[10] != 0;
2932 std::uint8_t contribution = 0;
2936 case SfcQualifier::n:
2937 if(signal) contribution |= action_direct;
2939 case SfcQualifier::s:
2940 if(rising) latched =
true;
2941 if(latched) contribution |= action_store;
2943 case SfcQualifier::r:
2944 if(signal) contribution |= action_reset;
2946 case SfcQualifier::p:
2947 if(rising) contribution |= action_direct;
2949 case SfcQualifier::l:
2953 elapsed = rising ? task_period_ns_ :
2954 saturating_add(elapsed, task_period_ns_);
2955 if(block.duration_ns > 0 &&
2956 elapsed <= block.duration_ns)
2957 contribution |= action_direct;
2960 case SfcQualifier::d:
2964 elapsed = rising ? task_period_ns_ :
2965 saturating_add(elapsed, task_period_ns_);
2966 if(elapsed >= block.duration_ns)
2967 contribution |= action_direct;
2970 case SfcQualifier::sd:
2971 if(rising && !latched) pending =
true;
2973 elapsed = saturating_add(elapsed, task_period_ns_);
2974 if(elapsed >= block.duration_ns) {
2977 contribution |= action_store;
2980 if(latched) contribution |= action_store;
2982 case SfcQualifier::ds:
2983 if(!activation && !latched) {
2986 }
else if(activation && !latched) {
2988 elapsed = rising ? task_period_ns_ :
2989 saturating_add(elapsed, task_period_ns_);
2990 if(elapsed >= block.duration_ns) {
2993 contribution |= action_store;
2996 if(latched) contribution |= action_store;
2998 case SfcQualifier::sl:
3001 elapsed = task_period_ns_;
3002 contribution |= action_store;
3003 }
else if(latched) {
3004 elapsed = saturating_add(elapsed, task_period_ns_);
3006 if(latched && elapsed >= block.duration_ns) {
3008 contribution |= action_expire;
3010 if(latched) contribution |= action_store;
3013 block_elapsed(state, elapsed);
3014 state[8] = pending ? 1U : 0U;
3015 state[9] = signal ? 1U : 0U;
3016 state[10] = latched ? 1U : 0U;
3017 state[11] = contribution;
3020 void start_sfc_phase(SfcRunnerPhase phase)
noexcept
3022 sfc_runner_->phase = phase;
3023 sfc_runner_->network_index = 0;
3024 sfc_runner_->item_index = 0;
3025 sfc_runner_->subitem_index = 0;
3026 sfc_runner_->work_remaining = 0;
3029 std::uint64_t transition_check_cost(
3030 const SfcNetworkInfo &network, std::size_t index)
const noexcept
3032 const SfcTransitionInfo &
transition = network.transitions[index];
3033 std::uint64_t cost = 1U +
transition.sources.size();
3034 for(std::size_t prior = 0; prior < index; ++prior)
3036 network.transitions[prior].sources.size();
3040 void advance_sfc_runner() noexcept
3043 if(sfc_runner_->phase == SfcRunnerPhase::stage_vars) {
3044 if(sfc_runner_->item_index >= program_->vars_bytes) {
3045 visible_vars_ = sfc_at(
3046 program_->sfc_committed_shadow_offset());
3047 sfc_runner_->phase = SfcRunnerPhase::base;
3048 sfc_runner_->item_index = 0;
3049 sfc_runner_->subitem_index = 0;
3052 if(sfc_runner_->subitem_index == 0) {
3053 sfc_runtime_[sfc_runner_->item_index] = vars_[sfc_runner_->item_index];
3054 sfc_runner_->subitem_index = 1;
3056 sfc_at(program_->sfc_committed_shadow_offset())
3057 [sfc_runner_->item_index] = vars_[sfc_runner_->item_index];
3058 ++sfc_runner_->item_index;
3059 sfc_runner_->subitem_index = 0;
3060 if(sfc_runner_->item_index >= program_->vars_bytes) {
3061 visible_vars_ = sfc_at(
3062 program_->sfc_committed_shadow_offset());
3063 sfc_runner_->phase = SfcRunnerPhase::base;
3064 sfc_runner_->item_index = 0;
3069 if(sfc_runner_->phase == SfcRunnerPhase::stage_active) {
3070 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3071 start_sfc_phase(SfcRunnerPhase::stage_firing);
3074 const SfcNetworkInfo &network =
3075 program_->sfc_networks[sfc_runner_->network_index];
3076 if(sfc_runner_->item_index >= network.steps.size()) {
3077 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3079 if(sfc_runner_->subitem_index == 0) {
3080 sfc_at(network.staged_active_offset)[sfc_runner_->item_index] =
3081 sfc_at(network.committed_active_offset)[sfc_runner_->item_index];
3082 sfc_runner_->subitem_index = 1;
3084 sfc_at(network.shadow_active_offset)[sfc_runner_->item_index] =
3085 sfc_at(network.committed_active_offset)[sfc_runner_->item_index];
3086 ++sfc_runner_->item_index;
3087 sfc_runner_->subitem_index = 0;
3091 if(sfc_runner_->phase == SfcRunnerPhase::stage_firing) {
3092 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3093 start_sfc_phase(SfcRunnerPhase::stage_blocks);
3096 const SfcNetworkInfo &network =
3097 program_->sfc_networks[sfc_runner_->network_index];
3098 if(sfc_runner_->item_index >= network.transitions.size()) {
3099 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3101 sfc_at(network.firing_offset)[sfc_runner_->item_index++] = 0;
3104 if(sfc_runner_->phase == SfcRunnerPhase::stage_blocks) {
3105 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3106 start_sfc_phase(SfcRunnerPhase::stage_actions);
3109 const SfcNetworkInfo &network =
3110 program_->sfc_networks[sfc_runner_->network_index];
3111 if(sfc_runner_->item_index >= network.action_blocks.size()) {
3112 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3114 const std::size_t
byte = sfc_runner_->subitem_index;
3115 const std::size_t offset =
3116 sfc_runner_->item_index * sfc_block_bytes +
byte / 2U;
3117 if((
byte & 1U) == 0)
3118 sfc_at(network.staged_block_offset)[offset] =
3119 sfc_at(network.committed_block_offset)[offset];
3121 sfc_at(network.shadow_block_offset)[offset] =
3122 sfc_at(network.committed_block_offset)[offset];
3123 ++sfc_runner_->subitem_index;
3124 if(sfc_runner_->subitem_index == sfc_block_bytes * 2U) {
3125 ++sfc_runner_->item_index;
3126 sfc_runner_->subitem_index = 0;
3130 if(sfc_runner_->phase == SfcRunnerPhase::stage_actions ||
3131 sfc_runner_->phase == SfcRunnerPhase::stage_direct) {
3132 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3133 start_sfc_phase(sfc_runner_->phase == SfcRunnerPhase::stage_actions
3134 ? SfcRunnerPhase::stage_direct
3135 : SfcRunnerPhase::transitions);
3138 const SfcNetworkInfo &network =
3139 program_->sfc_networks[sfc_runner_->network_index];
3140 if(sfc_runner_->item_index >= network.actions.size()) {
3141 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3143 if(sfc_runner_->phase == SfcRunnerPhase::stage_actions) {
3144 if(sfc_runner_->subitem_index == 0) {
3145 sfc_at(network.staged_action_offset)[sfc_runner_->item_index] =
3146 sfc_at(network.committed_action_offset)
3147 [sfc_runner_->item_index];
3148 sfc_runner_->subitem_index = 1;
3150 sfc_at(network.shadow_action_offset)[sfc_runner_->item_index] =
3151 sfc_at(network.committed_action_offset)
3152 [sfc_runner_->item_index];
3153 ++sfc_runner_->item_index;
3154 sfc_runner_->subitem_index = 0;
3157 sfc_at(network.direct_action_offset)[sfc_runner_->item_index] = 0;
3158 ++sfc_runner_->item_index;
3162 if(sfc_runner_->phase == SfcRunnerPhase::transitions) {
3163 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3164 start_sfc_phase(SfcRunnerPhase::active_updates);
3167 const SfcNetworkInfo &network =
3168 program_->sfc_networks[sfc_runner_->network_index];
3169 if(sfc_runner_->item_index >= network.transitions.size()) {
3170 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
3171 sfc_runner_->work_remaining = 0;
continue;
3173 if(sfc_runner_->work_remaining == 0)
3174 sfc_runner_->work_remaining = transition_check_cost(
3175 network, sfc_runner_->item_index);
3176 if(--sfc_runner_->work_remaining != 0)
return;
3177 if(transition_eligible(network, sfc_runner_->item_index)) {
3178 sfc_runner_->active_region =
3179 &network.transitions[sfc_runner_->item_index].condition;
3182 ++sfc_runner_->item_index;
3185 if(sfc_runner_->phase == SfcRunnerPhase::active_updates) {
3186 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3187 start_sfc_phase(SfcRunnerPhase::qualifier_blocks);
3190 const SfcNetworkInfo &network =
3191 program_->sfc_networks[sfc_runner_->network_index];
3192 if(sfc_runner_->item_index >= network.transitions.size()) {
3193 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
3194 sfc_runner_->subitem_index = 0;
continue;
3197 network.transitions[sfc_runner_->item_index];
3199 sfc_at(network.firing_offset)[sfc_runner_->item_index] != 0;
3200 if(sfc_runner_->subitem_index == 0) {
3201 ++sfc_runner_->subitem_index;
3204 if(sfc_runner_->subitem_index <=
transition.sources.size()) {
3206 sfc_at(network.staged_active_offset)
3207 [
transition.sources[sfc_runner_->subitem_index - 1]] = 0;
3208 ++sfc_runner_->subitem_index;
3211 const std::size_t target = sfc_runner_->subitem_index -
3215 sfc_at(network.staged_active_offset)
3217 ++sfc_runner_->subitem_index;
3218 if(target + 1U <
transition.targets.size())
return;
3220 ++sfc_runner_->item_index;
3221 sfc_runner_->subitem_index = 0;
3224 if(sfc_runner_->phase == SfcRunnerPhase::qualifier_blocks) {
3225 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3226 start_sfc_phase(SfcRunnerPhase::qualifier_actions);
3229 const SfcNetworkInfo &network =
3230 program_->sfc_networks[sfc_runner_->network_index];
3231 if(sfc_runner_->item_index >= network.action_blocks.size()) {
3232 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3234 update_sfc_qualifier_block(network, sfc_runner_->item_index++);
3237 if(sfc_runner_->phase == SfcRunnerPhase::qualifier_actions) {
3238 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3239 start_sfc_phase(SfcRunnerPhase::actions);
3242 const SfcNetworkInfo &network =
3243 program_->sfc_networks[sfc_runner_->network_index];
3244 if(sfc_runner_->item_index >= network.actions.size()) {
3245 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
3246 sfc_runner_->subitem_index = 0;
continue;
3248 const std::size_t blocks = network.action_blocks.size();
3249 if(sfc_runner_->subitem_index == 0) {
3250 sfc_runner_->reducer = 0;
3251 ++sfc_runner_->subitem_index;
3254 if(sfc_runner_->subitem_index <= blocks) {
3255 const std::size_t block = sfc_runner_->subitem_index - 1U;
3256 if(network.action_blocks[block].action == sfc_runner_->item_index)
3257 sfc_runner_->reducer |=
3258 sfc_at(network.staged_block_offset)
3259 [block * sfc_block_bytes + 11U];
3260 ++sfc_runner_->subitem_index;
3263 unsigned char *stored = sfc_at(network.staged_action_offset);
3264 if(sfc_runner_->subitem_index == blocks + 1U) {
3265 sfc_runner_->action_suppressed =
3266 (sfc_runner_->reducer & (action_reset | action_expire)) != 0;
3267 if((sfc_runner_->reducer & action_reset) != 0 ||
3268 (sfc_runner_->reducer & action_expire) != 0)
3269 stored[sfc_runner_->item_index] = 0;
3270 else if((sfc_runner_->reducer & action_store) != 0)
3271 stored[sfc_runner_->item_index] = 1U;
3272 ++sfc_runner_->subitem_index;
3275 const std::size_t cleanup = sfc_runner_->subitem_index - blocks - 2U;
3276 if(cleanup < blocks) {
3277 if((sfc_runner_->reducer & action_reset) != 0 &&
3278 network.action_blocks[cleanup].action == sfc_runner_->item_index) {
3279 unsigned char *state =
3280 sfc_at(network.staged_block_offset) +
3281 cleanup * sfc_block_bytes;
3282 const unsigned char previous = state[9];
3283 std::memset(state, 0, sfc_block_bytes);
3284 state[9] = previous;
3286 ++sfc_runner_->subitem_index;
3289 sfc_at(network.direct_action_offset)[sfc_runner_->item_index] =
3290 !sfc_runner_->action_suppressed &&
3291 (stored[sfc_runner_->item_index] != 0 ||
3292 (sfc_runner_->reducer & action_direct) != 0)
3294 ++sfc_runner_->item_index;
3295 sfc_runner_->subitem_index = 0;
3298 if(sfc_runner_->phase == SfcRunnerPhase::actions) {
3299 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3300 start_sfc_phase(SfcRunnerPhase::trace_exits);
3303 const SfcNetworkInfo &network =
3304 program_->sfc_networks[sfc_runner_->network_index];
3305 if(sfc_runner_->item_index >= network.actions.size()) {
3306 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3308 if(sfc_at(network.direct_action_offset)[sfc_runner_->item_index] != 0) {
3309 sfc_runner_->active_region = &network.actions[sfc_runner_->item_index].region;
3312 ++sfc_runner_->item_index;
3317 advance_sfc_trace_and_commit();
3320 void append_sfc_trace(std::size_t network_index, SfcEventKind kind,
3321 std::size_t entity_index, std::uint8_t qualifier,
3322 std::int32_t line, std::int32_t column,
3323 std::int32_t end_line,
3324 std::int32_t end_column)
noexcept
3326 if(sfc_runner_->trace_size >= sfc_runner_->trace_capacity) {
3327 ++sfc_runner_->trace_dropped;
3330 SfcTraceRecord &record = sfc_runner_->trace[sfc_runner_->trace_size++];
3331 record = SfcTraceRecord{};
3332 record.scan = sfc_runner_->scan;
3333 record.network =
static_cast<std::uint32_t
>(network_index);
3335 record.qualifier = qualifier;
3337 record.column = column;
3338 record.end_line = end_line;
3339 record.end_column = end_column;
3340 const std::uint32_t index =
static_cast<std::uint32_t
>(entity_index);
3341 if(kind == SfcEventKind::step_exit ||
3342 kind == SfcEventKind::step_enter) record.step = index;
3343 else if(kind == SfcEventKind::transition_fire)
3344 record.transition = index;
3345 else if(kind == SfcEventKind::qualifier_update) record.block = index;
3346 else if(kind == SfcEventKind::action_execute) record.action = index;
3349 void advance_sfc_trace_and_commit() noexcept
3352 if(sfc_runner_->phase == SfcRunnerPhase::commit_vars) {
3353 if(sfc_runner_->item_index < program_->vars_bytes) {
3354 vars_[sfc_runner_->item_index] = sfc_runtime_[sfc_runner_->item_index];
3355 ++sfc_runner_->item_index;
3358 start_sfc_phase(SfcRunnerPhase::commit_active);
3361 if(sfc_runner_->network_index >= program_->sfc_networks.size()) {
3363 switch(sfc_runner_->phase) {
3364 case SfcRunnerPhase::trace_exits:
3365 next = SfcRunnerPhase::trace_transitions;
break;
3366 case SfcRunnerPhase::trace_transitions:
3367 next = SfcRunnerPhase::trace_enters;
break;
3368 case SfcRunnerPhase::trace_enters:
3369 next = SfcRunnerPhase::trace_qualifiers;
break;
3370 case SfcRunnerPhase::trace_qualifiers:
3371 next = SfcRunnerPhase::trace_actions;
break;
3372 case SfcRunnerPhase::trace_actions:
3373 next = SfcRunnerPhase::commit_vars;
break;
3374 case SfcRunnerPhase::commit_active:
3375 next = SfcRunnerPhase::commit_blocks;
break;
3376 case SfcRunnerPhase::commit_blocks:
3377 next = SfcRunnerPhase::commit_actions;
break;
3378 case SfcRunnerPhase::commit_actions:
3379 next = SfcRunnerPhase::finish;
break;
3382 start_sfc_phase(next);
3385 const SfcNetworkInfo &network =
3386 program_->sfc_networks[sfc_runner_->network_index];
3387 const unsigned char *old_active =
3388 sfc_at(network.committed_active_offset);
3389 const unsigned char *new_active =
3390 sfc_at(network.staged_active_offset);
3391 if(sfc_runner_->phase == SfcRunnerPhase::trace_exits ||
3392 sfc_runner_->phase == SfcRunnerPhase::trace_enters) {
3393 if(sfc_runner_->item_index >= network.steps.size()) {
3394 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3396 const std::size_t
step = sfc_runner_->item_index++;
3397 const bool emit = sfc_runner_->phase == SfcRunnerPhase::trace_exits
3398 ? old_active[
step] != 0 && new_active[
step] == 0
3399 : old_active[
step] == 0 && new_active[
step] != 0;
3401 append_sfc_trace(sfc_runner_->network_index,
3402 sfc_runner_->phase == SfcRunnerPhase::trace_exits
3403 ? SfcEventKind::step_exit
3404 : SfcEventKind::step_enter,
3405 step, 0, network.steps[step].line,
3406 network.steps[step].column,
3407 network.steps[step].end_line,
3408 network.steps[step].end_column);
3411 if(sfc_runner_->phase == SfcRunnerPhase::trace_transitions) {
3412 if(sfc_runner_->item_index >= network.transitions.size()) {
3413 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3415 const std::size_t
transition = sfc_runner_->item_index++;
3416 if(sfc_at(network.firing_offset)[transition] != 0)
3417 append_sfc_trace(sfc_runner_->network_index,
3418 SfcEventKind::transition_fire,
3420 network.transitions[transition].line,
3421 network.transitions[transition].column,
3422 network.transitions[transition].end_line,
3423 network.transitions[transition].end_column);
3426 if(sfc_runner_->phase == SfcRunnerPhase::trace_qualifiers) {
3427 if(sfc_runner_->item_index >= network.action_blocks.size()) {
3428 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3430 const std::size_t block = sfc_runner_->item_index++;
3431 append_sfc_trace(sfc_runner_->network_index,
3432 SfcEventKind::qualifier_update, block,
3433 network.action_blocks[block].qualifier,
3434 network.action_blocks[block].line,
3435 network.action_blocks[block].column,
3436 network.action_blocks[block].end_line,
3437 network.action_blocks[block].end_column);
3440 if(sfc_runner_->phase == SfcRunnerPhase::trace_actions) {
3441 if(sfc_runner_->item_index >= network.actions.size()) {
3442 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3444 const std::size_t
action = sfc_runner_->item_index++;
3445 const unsigned char *execute =
3446 sfc_at(network.direct_action_offset);
3447 if(execute[action] != 0)
3448 append_sfc_trace(sfc_runner_->network_index,
3449 SfcEventKind::action_execute, action, 0,
3450 network.actions[action].line,
3451 network.actions[action].column,
3452 network.actions[action].end_line,
3453 network.actions[action].end_column);
3456 if(sfc_runner_->phase == SfcRunnerPhase::commit_active) {
3457 if(sfc_runner_->item_index >= network.steps.size()) {
3458 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3460 sfc_at(network.committed_active_offset)[sfc_runner_->item_index] =
3461 sfc_at(network.staged_active_offset)[sfc_runner_->item_index];
3462 ++sfc_runner_->item_index;
3465 if(sfc_runner_->phase == SfcRunnerPhase::commit_blocks) {
3466 if(sfc_runner_->item_index >= network.action_blocks.size()) {
3467 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3469 const std::size_t offset =
3470 sfc_runner_->item_index * sfc_block_bytes + sfc_runner_->subitem_index;
3471 sfc_at(network.committed_block_offset)[offset] =
3472 sfc_at(network.staged_block_offset)[offset];
3473 ++sfc_runner_->subitem_index;
3474 if(sfc_runner_->subitem_index == sfc_block_bytes) {
3475 ++sfc_runner_->item_index;
3476 sfc_runner_->subitem_index = 0;
3480 if(sfc_runner_->phase == SfcRunnerPhase::commit_actions) {
3481 if(sfc_runner_->item_index >= network.actions.size()) {
3482 ++sfc_runner_->network_index; sfc_runner_->item_index = 0;
continue;
3484 sfc_at(network.committed_action_offset)[sfc_runner_->item_index] =
3485 sfc_at(network.staged_action_offset)[sfc_runner_->item_index];
3486 ++sfc_runner_->item_index;
3489 if(sfc_runner_->phase == SfcRunnerPhase::finish) {
3490 execution_vars_ = vars_;
3491 visible_vars_ = vars_;
3492 ++sfc_runner_->scan;
3493 if(process_image_ !=
nullptr)
3494 process_image_->commit_scan(
3495 *program_, vars_, !defer_process_image_publish_);
3496 sfc_runner_->phase = SfcRunnerPhase::none;
3502 template <
typename Storage>
3503 static Storage *construct_binding_storage(
unsigned char *&cursor)
3505 static_assert(
alignof(Storage) <= 8U);
3506 Storage *storage = ::new(
static_cast<void *
>(cursor)) Storage{};
3507 cursor +=
sizeof(Storage);
3511 void load_binding_storage(
const Program &program,
unsigned char *cursor)
3513 for(std::uint8_t value =
3514 static_cast<std::uint8_t
>(BindingStorageKind::axis_targets);
3516 static_cast<std::uint8_t
>(BindingStorageKind::kin_transforms);
3520 if(!
program.uses_binding_storage(kind))
continue;
3522 case BindingStorageKind::axis_targets:
3524 construct_binding_storage<AxisTargetStorage>(cursor);
3526 case BindingStorageKind::group_targets:
3528 construct_binding_storage<GroupTargetStorage>(cursor);
3530 case BindingStorageKind::path_tables:
3532 construct_binding_storage<PathTableStorage>(cursor);
3534 case BindingStorageKind::path_descriptions:
3535 path_descriptions_ =
3536 construct_binding_storage<PathDescriptionStorage>(cursor);
3538 case BindingStorageKind::cam_switch_tables:
3539 cam_switch_tables_ =
3540 construct_binding_storage<CamSwitchTableStorage>(cursor);
3542 case BindingStorageKind::cam_switch_outputs:
3543 cam_switch_outputs_ =
3544 construct_binding_storage<CamSwitchOutputStorage>(cursor);
3546 case BindingStorageKind::cam_track_options:
3547 cam_track_options_ =
3548 construct_binding_storage<CamTrackOptionStorage>(cursor);
3550 case BindingStorageKind::cam_tables:
3552 construct_binding_storage<CamTableStorage>(cursor);
3554 case BindingStorageKind::position_profiles:
3555 position_profiles_ =
3556 construct_binding_storage<PositionProfileStorage>(cursor);
3558 case BindingStorageKind::velocity_profiles:
3559 velocity_profiles_ =
3560 construct_binding_storage<VelocityProfileStorage>(cursor);
3562 case BindingStorageKind::acceleration_profiles:
3563 acceleration_profiles_ = construct_binding_storage<
3566 case BindingStorageKind::kin_transforms:
3568 construct_binding_storage<KinTransformStorage>(cursor);
3570 case BindingStorageKind::none:
break;
3575 template <
typename Register>
3576 BindingError bind_named_handle(
const char *name, TypeId expected,
3577 std::size_t capacity,
3578 Register register_value)
3580 if(program_ ==
nullptr || name ==
nullptr)
return BindingError::unknown;
3581 if(scan_started_)
return BindingError::locked;
3582 std::size_t slot = 0;
3583 for(
const VarInfo &var : program_->vars) {
3584 if(var.type_id != expected)
continue;
3585 if(!ascii_name_equal(name, var.lower)) {
3589 if(slot >= capacity)
return BindingError::capacity_exceeded;
3590 std::uint64_t handle = 0;
3591 std::memcpy(&handle, vars_ + var.offset,
sizeof(handle));
3592 if(handle != 0U)
return BindingError::duplicate;
3594 if(result != BindingError::ok)
return result;
3595 handle =
static_cast<std::uint64_t
>(slot) + 1U;
3596 std::memcpy(vars_ + var.offset, &handle,
sizeof(handle));
3597 return BindingError::ok;
3599 return BindingError::unknown;
3602 template <std::
size_t Capacity>
3603 BindingError bind_profile(
const char *name, TypeId type,
3604 std::array<BindingProfileSlot, Capacity> &profiles,
3605 const BindingProfileSlot &decoded)
3607 return bind_named_handle(
3608 name, type, profiles.size(), [&](std::size_t slot) {
3609 profiles[slot] = decoded;
3610 return BindingError::ok;
3616 if(type == Type::axis_ref || type_id == binding_type::axis_ref ||
3617 type_id == binding_type::mc_input_ref ||
3618 type_id == binding_type::mc_output_ref) {
3619 return BindingTargetKind::axis;
3621 if(type == Type::group_ref || type_id == binding_type::group_ref) {
3622 return BindingTargetKind::group;
3624 return BindingTargetKind::invalid;
3627 template <
typename Text>
3628 static bool ascii_name_equal(
const char *name,
const Text &lower)
3630 std::size_t index = 0;
3631 while(name[index] !=
'\0' && index < lower.size()) {
3632 char value = name[index];
3633 if(value >=
'A' && value <=
'Z') {
3634 value =
static_cast<char>(value -
'A' +
'a');
3636 if(value != lower[index])
return false;
3639 return index == lower.size() && name[index] ==
'\0';
3642 bool resolve_reference_payload(Type type, TypeId type_id,
3643 std::int64_t &value)
const
3646 if(kind == BindingTargetKind::invalid)
return true;
3647 const std::uint64_t handle =
static_cast<std::uint64_t
>(value);
3652 const std::size_t slot =
static_cast<std::size_t
>(handle - 1U);
3653 void *target =
nullptr;
3654 if(kind == BindingTargetKind::axis) {
3655 if(slot >= axis_targets_->size())
return false;
3656 target = (*axis_targets_)[slot];
3658 if(slot >= group_targets_->size())
return false;
3659 target = (*group_targets_)[slot];
3661 if(target ==
nullptr)
return false;
3662 value =
static_cast<std::int64_t
>(
3663 reinterpret_cast<std::uintptr_t
>(target));
3667 bool encode_reference_payload(Type type, TypeId type_id,
3668 std::int64_t &value)
const
3671 if(kind == BindingTargetKind::invalid)
return true;
3672 if(value == 0)
return true;
3673 const std::uintptr_t target =
static_cast<std::uintptr_t
>(value);
3674 if(kind == BindingTargetKind::axis) {
3675 for(std::size_t slot = 0; slot < axis_targets_->size(); ++slot) {
3676 if(
reinterpret_cast<std::uintptr_t
>((*axis_targets_)[slot]) ==
3678 value =
static_cast<std::int64_t
>(slot + 1U);
3683 for(std::size_t slot = 0; slot < group_targets_->size(); ++slot) {
3684 if(
reinterpret_cast<std::uintptr_t
>((*group_targets_)[slot]) ==
3686 value =
static_cast<std::int64_t
>(slot + 1U);
3694 bool resolve_sequence_payload(
3695 TypeId type, std::int64_t value,
3696 generated::StBindingNativeSequenceValue &sequence)
const
3702 if(value < 0)
return false;
3703 const std::uint64_t handle =
static_cast<std::uint64_t
>(value);
3704 const std::size_t slot =
static_cast<std::size_t
>(handle - 1U);
3706 case binding_type::mc_path_table:
3707 if(slot >= path_tables_->size() ||
3708 (*path_tables_)[slot] ==
nullptr) {
3711 sequence = {(*path_tables_)[slot], 1U,
false};
3713 case binding_type::mc_path_description:
3714 if(slot >= path_descriptions_->size() ||
3715 (*path_descriptions_)[slot].count == 0U) {
3718 sequence = {&(*path_descriptions_)[slot], 1U,
false};
3720 case binding_type::mc_cam_switch_table_view:
3721 if(slot >= cam_switch_tables_->size() ||
3722 (*cam_switch_tables_)[slot].data ==
nullptr) {
3725 sequence = {(*cam_switch_tables_)[slot].data,
3726 (*cam_switch_tables_)[slot].size,
false};
3728 case binding_type::mc_cam_switch_outputs_view:
3729 if(slot >= cam_switch_outputs_->size() ||
3730 (*cam_switch_outputs_)[slot].data ==
nullptr) {
3733 sequence = {(*cam_switch_outputs_)[slot].data,
3734 (*cam_switch_outputs_)[slot].size,
false};
3736 case binding_type::mc_cam_track_options_view:
3737 if(slot >= cam_track_options_->size() ||
3738 (*cam_track_options_)[slot].data ==
nullptr) {
3741 sequence = {(*cam_track_options_)[slot].data,
3742 (*cam_track_options_)[slot].size,
false};
3744 case binding_type::mc_cam_table_view:
3745 if(slot >= cam_tables_->size() ||
3746 (*cam_tables_)[slot].points ==
nullptr) {
3749 sequence = {(*cam_tables_)[slot].points,
3750 (*cam_tables_)[slot].size,
3751 (*cam_tables_)[slot].periodic};
3753 case binding_type::mc_time_position:
3754 return resolve_profile_payload(*position_profiles_, slot,
3756 case binding_type::mc_time_velocity:
3757 return resolve_profile_payload(*velocity_profiles_, slot,
3759 case binding_type::mc_time_acceleration:
3760 return resolve_profile_payload(*acceleration_profiles_, slot,
3762 default:
return false;
3766 bool resolve_tagged_reference_payload(
3767 TypeId type, std::int64_t value,
3768 generated::StBindingNativeTaggedReferenceValue &tagged)
const
3770 if(type != binding_type::mc_kin_transform_ref)
return false;
3775 if(value < 0)
return false;
3776 const std::size_t slot =
static_cast<std::size_t
>(
3777 static_cast<std::uint64_t
>(value) - 1U);
3778 if(slot >= kin_transforms_->size())
return false;
3779 const axis::KinTransformRef &transform = (*kin_transforms_)[slot];
3780 switch(transform.kind) {
3781 case axis::KinTransformKind::kinematics:
3782 if(transform.kinematics ==
nullptr || transform.pose !=
nullptr) {
3786 transform.kinematics,
3787 generated::StBindingNativeTaggedReferenceTag::kinematics};
3789 case axis::KinTransformKind::pose:
3790 if(transform.pose ==
nullptr || transform.kinematics !=
nullptr) {
3793 tagged = {transform.pose,
3794 generated::StBindingNativeTaggedReferenceTag::pose};
3796 case axis::KinTransformKind::none:
3797 default:
return false;
3801 bool encode_tagged_reference_payload(
3803 const generated::StBindingNativeTaggedReferenceValue &tagged,
3804 std::int64_t &handle)
const
3806 if(type != binding_type::mc_kin_transform_ref)
return false;
3808 generated::StBindingNativeTaggedReferenceTag::none &&
3809 tagged.data ==
nullptr) {
3813 for(std::size_t slot = 0; slot < kin_transforms_->size(); ++slot) {
3814 const axis::KinTransformRef &transform = (*kin_transforms_)[slot];
3815 const bool matches =
3817 generated::StBindingNativeTaggedReferenceTag::kinematics &&
3818 transform.kind == axis::KinTransformKind::kinematics &&
3819 transform.kinematics == tagged.data &&
3820 transform.pose ==
nullptr) ||
3822 generated::StBindingNativeTaggedReferenceTag::pose &&
3823 transform.kind == axis::KinTransformKind::pose &&
3824 transform.pose == tagged.data &&
3825 transform.kinematics ==
nullptr);
3827 handle =
static_cast<std::int64_t
>(slot + 1U);
3834 bool encode_sequence_payload(
3835 TypeId type,
const generated::StBindingNativeSequenceValue &sequence,
3836 std::int64_t &handle)
const
3844 case binding_type::mc_path_table:
3846 return find_sequence_handle(*path_tables_,
sequence.data, handle);
3847 case binding_type::mc_path_description:
3849 for(std::size_t slot = 0; slot < path_descriptions_->size();
3851 if((*path_descriptions_)[slot].count != 0U &&
3852 &(*path_descriptions_)[slot] ==
sequence.data) {
3853 handle =
static_cast<std::int64_t
>(slot + 1U);
3858 case binding_type::mc_cam_switch_table_view:
3860 for(std::size_t slot = 0; slot < cam_switch_tables_->size();
3862 const auto &view = (*cam_switch_tables_)[slot];
3864 handle =
static_cast<std::int64_t
>(slot + 1U);
3869 case binding_type::mc_cam_switch_outputs_view:
3871 for(std::size_t slot = 0; slot < cam_switch_outputs_->size();
3873 const auto &view = (*cam_switch_outputs_)[slot];
3876 handle =
static_cast<std::int64_t
>(slot + 1U);
3881 case binding_type::mc_cam_track_options_view:
3883 for(std::size_t slot = 0; slot < cam_track_options_->size();
3885 const auto &view = (*cam_track_options_)[slot];
3888 handle =
static_cast<std::int64_t
>(slot + 1U);
3893 case binding_type::mc_cam_table_view:
3894 for(std::size_t slot = 0; slot < cam_tables_->size(); ++slot) {
3895 const auto &view = (*cam_tables_)[slot];
3898 handle =
static_cast<std::int64_t
>(slot + 1U);
3903 case binding_type::mc_time_position:
3905 return find_profile_handle(*position_profiles_, sequence, handle);
3906 case binding_type::mc_time_velocity:
3908 return find_profile_handle(*velocity_profiles_, sequence, handle);
3909 case binding_type::mc_time_acceleration:
3911 return find_profile_handle(*acceleration_profiles_, sequence,
3913 default:
return false;
3917 template <std::
size_t Capacity>
3918 static bool resolve_profile_payload(
3919 const std::array<BindingProfileSlot, Capacity> &profiles,
3921 generated::StBindingNativeSequenceValue &sequence)
3923 if(slot >= profiles.size() || profiles[slot].count == 0U)
return false;
3924 sequence = {profiles[slot].segments.data(), profiles[slot].count,
3929 template <
typename Po
inter, std::
size_t Capacity>
3930 static bool find_sequence_handle(
const std::array<Pointer, Capacity> &values,
3931 const void *data, std::int64_t &handle)
3933 for(std::size_t slot = 0; slot < values.size(); ++slot) {
3934 if(values[slot] == data) {
3935 handle =
static_cast<std::int64_t
>(slot + 1U);
3942 template <std::
size_t Capacity>
3943 static bool find_profile_handle(
3944 const std::array<BindingProfileSlot, Capacity> &profiles,
3945 const generated::StBindingNativeSequenceValue &sequence,
3946 std::int64_t &handle)
3948 for(std::size_t slot = 0; slot < profiles.size(); ++slot) {
3949 if(profiles[slot].count ==
sequence.count &&
3950 profiles[slot].segments.data() ==
sequence.data) {
3951 handle =
static_cast<std::int64_t
>(slot + 1U);
3958 static bool rd16(
const std::uint8_t *code, std::size_t size,
3959 std::size_t &pc, std::uint16_t &value)
3964 value =
static_cast<std::uint16_t
>(
3965 static_cast<std::uint16_t
>(code[pc]) |
3966 (
static_cast<std::uint16_t
>(code[pc + 1]) << 8));
3971 static std::uint32_t read_le32(
const unsigned char *bytes)
3973 return static_cast<std::uint32_t
>(bytes[0]) |
3974 (
static_cast<std::uint32_t
>(bytes[1]) << 8U) |
3975 (
static_cast<std::uint32_t
>(bytes[2]) << 16U) |
3976 (
static_cast<std::uint32_t
>(bytes[3]) << 24U);
3979 static bool next_utf8(
const unsigned char *bytes, std::uint32_t length,
3980 std::uint32_t &at, std::uint32_t &scalar)
3985 const std::uint8_t lead = bytes[at++];
3991 if(lead >= 0xC2U && lead <= 0xDFU) {
3992 scalar = lead & 0x1FU;
3994 }
else if(lead >= 0xE0U && lead <= 0xEFU) {
3995 scalar = lead & 0x0FU;
3997 }
else if(lead >= 0xF0U && lead <= 0xF4U) {
3998 scalar = lead & 0x07U;
4003 if(count > length - at) {
4006 for(
unsigned i = 0; i <
count; ++i) {
4007 const std::uint8_t part = bytes[at++];
4008 if((part & 0xC0U) != 0x80U) {
4012 ((lead == 0xE0U && part < 0xA0U) ||
4013 (lead == 0xEDU && part > 0x9FU) ||
4014 (lead == 0xF0U && part < 0x90U) ||
4015 (lead == 0xF4U && part > 0x8FU))) {
4018 scalar = (scalar << 6U) | (part & 0x3FU);
4020 return scalar <= 0x10FFFFU &&
4021 !(scalar >= 0xD800U && scalar <= 0xDFFFU);
4024 static bool valid_string_object(
const unsigned char *
object,
4025 const TypeDesc &desc)
4027 const std::uint32_t length = read_le32(
object);
4028 if(length > desc.string.capacity) {
4031 if(desc.kind == TypeKind::wstring) {
4032 for(std::uint32_t i = 0; i < length; ++i) {
4033 const std::uint32_t scalar =
4034 read_le32(
object + 4U +
4035 static_cast<std::size_t
>(i) * 4U);
4036 if(scalar > 0x10FFFFU ||
4037 (scalar >= 0xD800U && scalar <= 0xDFFFU)) {
4043 if(desc.kind != TypeKind::string) {
4046 std::uint32_t at = 0;
4047 while(at < length) {
4048 std::uint32_t scalar = 0;
4049 if(!next_utf8(
object + 4U, length, at, scalar)) {
4056 static int compare_strings(
const unsigned char *left,
4057 const TypeDesc &left_desc,
4058 const unsigned char *right,
4059 const TypeDesc &right_desc)
4061 if(!valid_string_object(left, left_desc) ||
4062 !valid_string_object(right, right_desc)) {
4065 const std::uint32_t left_length = read_le32(left);
4066 const std::uint32_t right_length = read_le32(right);
4067 if(left_desc.kind == TypeKind::wstring) {
4068 const std::uint32_t common = left_length < right_length
4071 for(std::uint32_t i = 0; i < common; ++i) {
4072 const std::size_t byte_offset =
4073 static_cast<std::size_t
>(i) * 4U;
4074 const std::uint32_t a = read_le32(left + 4U + byte_offset);
4075 const std::uint32_t b = read_le32(right + 4U + byte_offset);
4077 return a < b ? -1 : 1;
4080 return left_length == right_length ? 0
4081 : left_length < right_length ? -1 : 1;
4083 std::uint32_t left_at = 0;
4084 std::uint32_t right_at = 0;
4085 while(left_at < left_length && right_at < right_length) {
4086 std::uint32_t a = 0;
4087 std::uint32_t b = 0;
4088 if(!next_utf8(left + 4U, left_length, left_at, a) ||
4089 !next_utf8(right + 4U, right_length, right_at, b)) {
4093 return a < b ? -1 : 1;
4096 return left_at == left_length && right_at == right_length ? 0
4097 : left_at == left_length ? -1 : 1;
4100 static bool rd32(
const std::uint8_t *code, std::size_t size,
4101 std::size_t &pc, std::uint32_t &value)
4106 value =
static_cast<std::uint32_t
>(code[pc]) |
4107 (
static_cast<std::uint32_t
>(code[pc + 1]) << 8) |
4108 (
static_cast<std::uint32_t
>(code[pc + 2]) << 16) |
4109 (
static_cast<std::uint32_t
>(code[pc + 3]) << 24);
4114 static bool rd64(
const std::uint8_t *code, std::size_t size,
4115 std::size_t &pc, std::uint64_t &value)
4117 std::uint32_t low = 0;
4118 std::uint32_t high = 0;
4119 if(!rd32(code, size, pc, low) || !rd32(code, size, pc, high)) {
4122 value =
static_cast<std::uint64_t
>(low) |
4123 (
static_cast<std::uint64_t
>(high) << 32U);
4127 bool load_raw64_at(
const unsigned char *storage, std::uint32_t offset,
4128 std::uint64_t &value)
const
4130 if(program_ ==
nullptr || offset > program_->vars_bytes ||
4131 8U > program_->vars_bytes - offset) {
4134 std::memcpy(&value, storage + offset,
sizeof(value));
4138 bool load_raw64(std::uint32_t offset, std::uint64_t &value)
const
4140 return load_raw64_at(execution_vars_, offset, value);
4143 bool store_raw64(std::uint32_t offset, std::uint64_t value)
4145 if(program_ ==
nullptr || offset > program_->vars_bytes ||
4146 8U > program_->vars_bytes - offset) {
4149 std::memcpy(execution_vars_ + offset, &value,
sizeof(value));
4153 bool load_value(std::uint32_t offset, TypeId type_id,
4154 std::uint64_t &value)
const
4156 return load_value_at(execution_vars_, offset, type_id, value);
4159 bool load_value_at(
const unsigned char *storage, std::uint32_t offset,
4160 TypeId type_id, std::uint64_t &value)
const
4162 if(program_ ==
nullptr || storage ==
nullptr) {
4165 const TypeDesc *desc = program_->types.get(type_id);
4166 if(desc ==
nullptr) {
4168 load_raw64_at(storage, offset, value);
4170 if(desc->kind == TypeKind::array || desc->kind == TypeKind::struct_ ||
4171 desc->size == 0 || desc->size > 8 ||
4172 offset > program_->vars_bytes ||
4173 desc->size > program_->vars_bytes - offset) {
4176 std::uint64_t
raw = 0;
4177 for(std::uint64_t i = 0; i < desc->size; ++i) {
4178 raw |=
static_cast<std::uint64_t
>(storage[offset + i]) <<
4182 if(desc->kind == TypeKind::enum_) {
4183 base_id = desc->enum_base;
4184 }
else if(desc->kind == TypeKind::subrange) {
4185 base_id = desc->subrange.base;
4188 if(type == Type::real) {
4189 const std::uint32_t raw32 =
static_cast<std::uint32_t
>(
raw);
4190 float number = 0.0F;
4191 std::memcpy(&number, &raw32,
sizeof(number));
4192 value = detail::double_bits(
static_cast<double>(number));
4195 if((
is_signed_int(type) || type == Type::date) && desc->size < 8) {
4196 const unsigned bits =
static_cast<unsigned>(desc->size * 8U);
4197 const std::uint64_t sign = std::uint64_t{1} << (bits - 1U);
4198 raw = (
raw ^ sign) - sign;
4204 bool store_value(std::uint32_t offset, TypeId type_id,
4205 std::uint64_t value)
4207 if(program_ ==
nullptr) {
4210 if(process_image_ !=
nullptr) {
4211 (void)process_image_->forced_store(*program_, offset, type_id,
4214 const TypeDesc *desc = program_->types.get(type_id);
4215 if(desc ==
nullptr) {
4218 if(desc->kind == TypeKind::array || desc->kind == TypeKind::struct_ ||
4219 desc->size == 0 || desc->size > 8 ||
4220 offset > program_->vars_bytes ||
4221 desc->size > program_->vars_bytes - offset) {
4225 if(desc->kind == TypeKind::enum_) {
4226 base_id = desc->enum_base;
4227 }
else if(desc->kind == TypeKind::subrange) {
4228 base_id = desc->subrange.base;
4231 const float number =
4232 static_cast<float>(detail::bits_double(value));
4233 std::uint32_t
raw = 0;
4234 std::memcpy(&raw, &number,
sizeof(raw));
4237 for(std::uint64_t i = 0; i < desc->size; ++i) {
4238 execution_vars_[offset + i] =
4239 static_cast<unsigned char>(value >> (i * 8U));
4241 if(process_image_ !=
nullptr)
4242 process_image_->mark_store(*program_, offset, type_id);
4246 static std::int64_t as_i64(std::uint64_t raw)
4248 return static_cast<std::int64_t
>(
raw);
4251 static std::uint64_t as_u64(std::int64_t value)
4253 return static_cast<std::uint64_t
>(value);
4256 DebugRuntimeStorage *debug_runtime_storage() noexcept
4258 if(program_ ==
nullptr || vars_ ==
nullptr ||
4259 !program_->needs_debug_runtime_storage())
4261 return reinterpret_cast<DebugRuntimeStorage *
>(
4262 vars_ + program_->debug_runtime_storage_offset());
4265 const DebugRuntimeStorage *debug_runtime_storage() const noexcept
4267 if(program_ ==
nullptr || vars_ ==
nullptr ||
4268 !program_->needs_debug_runtime_storage())
4270 return reinterpret_cast<const DebugRuntimeStorage *
>(
4271 vars_ + program_->debug_runtime_storage_offset());
4276 if(sfc_runner_->active_region !=
nullptr &&
4277 sfc_runner_->network_index < program_->sfc_networks.size()) {
4278 sfc_runner_->fault_network_index =
4279 static_cast<std::uint32_t
>(sfc_runner_->network_index);
4280 sfc_runner_->fault_element_index =
4281 static_cast<std::uint32_t
>(sfc_runner_->item_index);
4282 sfc_runner_->fault_element_kind =
4283 sfc_runner_->phase == SfcRunnerPhase::transitions
4284 ? TaskFaultElementKind::transition
4285 : TaskFaultElementKind::action;
4287 if(sfc_commit_started()) rollback_sfc_commit();
4288 execution_vars_ = vars_;
4289 visible_vars_ = vars_;
4290 sfc_runner_->trace_size = sfc_runner_->trace_scan_begin_size;
4291 sfc_runner_->trace_dropped = sfc_runner_->trace_scan_begin_dropped;
4292 sfc_runner_->active_region =
nullptr;
4294 execution_state_ = InstanceState::faulted;
4298 const Program *program_ =
nullptr;
4299 ProcessImage *process_image_ =
nullptr;
4300 unsigned char *vars_ =
nullptr;
4301 const unsigned char *visible_vars_ =
nullptr;
4302 unsigned char *execution_vars_ =
nullptr;
4303 unsigned char *sfc_runtime_ =
nullptr;
4304 SfcRunnerStorage *sfc_runner_ =
nullptr;
4305 unsigned char *fb_area_ =
nullptr;
4306 std::uint64_t *stack_ =
nullptr;
4309 std::size_t pc_ = 0;
4310 std::uint32_t instruction_pc_ = 0;
4311 std::int32_t sp_ = 0;
4312 std::int64_t remaining_budget_ = 0;
4313 bool scan_started_ =
false;
4314 bool defer_process_image_publish_ =
false;
4315 std::int64_t task_period_ns_ = 0;
4316 std::int64_t utc_dt_ns_ = 0;
4317 RuntimeDebugHooks *debug_hooks_ =
nullptr;