F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
FpySequencerDirectives.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cmath>
3 #include <cstring>
4 #include <type_traits>
5 #include "Fw/Com/ComPacket.hpp"
8 
9 namespace Svc {
10 
11 void FpySequencer::sendSignal(Signal signal) {
12  switch (signal) {
15  break;
16  }
19  break;
20  }
23  break;
24  }
27  break;
28  }
29  default: {
30  FW_ASSERT(false, static_cast<FwAssertArgType>(signal));
31  }
32  }
33 }
34 
35 // utility method for updating telemetry based on a directive error code
36 void FpySequencer::handleDirectiveErrorCode(Fpy::DirectiveId id, DirectiveError err) {
37  this->m_tlm.lastDirectiveError = err;
38  if (err != DirectiveError::NO_ERROR) {
39  this->m_tlm.directiveErrorIndex = this->currentStatementIdx();
40  this->m_tlm.directiveErrorId = id;
41  }
42 }
43 
44 Fw::Success FpySequencer::sendCmd(FwOpcodeType opcode, const U8* argBuf, FwSizeType argBufSize) {
45  Fw::ComBuffer cmdBuf;
46  Fw::SerializeStatus stat =
47  cmdBuf.serializeFrom(static_cast<FwPacketDescriptorType>(Fw::ComPacketType::FW_PACKET_COMMAND));
48  // TODO should I assert here? this really shouldn't fail, I should just add a static assert
49  // on com buf size and then assert here
51  return Fw::Success::FAILURE;
52  }
53  stat = cmdBuf.serializeFrom(opcode);
55  return Fw::Success::FAILURE;
56  }
57  stat = cmdBuf.serializeFrom(argBuf, argBufSize, Fw::Serialization::OMIT_LENGTH);
59  return Fw::Success::FAILURE;
60  }
61 
62  // calculate the unique command identifier:
63  // cmd UID is formatted like XXYY, where XX are the first two bytes of the m_sequencesStarted counter
64  // and YY are the first two bytes of the m_statementsDispatched counter.
65  // this way, we know when we get a cmd back A) whether or not it's from this sequence (modulo 2^16) and B)
66  // whether or not it's this specific instance of the cmd in the sequence, and not another one with the same opcode
67  // somewhere else in the file.
68  // if we put this uid in the context we send to the cmdDisp, we will get it back when the cmd returns
69  U32 cmdUid =
70  static_cast<U32>(((this->m_sequencesStarted & 0xFFFF) << 16) | (this->m_statementsDispatched & 0xFFFF));
71 
72  this->cmdOut_out(0, cmdBuf, cmdUid);
73 
74  return Fw::Success::SUCCESS;
75 }
76 
77 // returns true if a directive can be executed. a directive is only ever dispatched in
78 // RUNNING.DISPATCH_STATEMENT, after which the sequencer waits in RUNNING.AWAITING_STATEMENT_RESPONSE.
79 // CANCEL and RUN are higher priority than directives, so a sequence can be cancelled while its directive is
80 // still queued, and a new sequence can be started and dispatch its first statement before that directive is
81 // serviced. neither of those directives may execute
82 bool FpySequencer::checkDirectiveExpected(Fpy::DirectiveId directive, U64 sequenceIndex) {
84  this->log_WARNING_LO_DirectiveWhileNotAwaiting(directive, static_cast<I32>(this->sequencer_getState()));
85  return false;
86  }
87  if (sequenceIndex != this->m_sequencesStarted) {
88  this->log_WARNING_LO_DirectiveFromOldSequence(directive, sequenceIndex, this->m_sequencesStarted);
89  return false;
90  }
91  return true;
92 }
93 
96  U64 sequenceIndex) {
97  if (!this->checkDirectiveExpected(Fpy::DirectiveId::WAIT_REL, sequenceIndex)) {
98  return;
99  }
101  this->sendSignal(this->waitRel_directiveHandler(directive, error));
102  handleDirectiveErrorCode(Fpy::DirectiveId::WAIT_REL, error);
103 }
104 
107  U64 sequenceIndex) {
108  if (!this->checkDirectiveExpected(Fpy::DirectiveId::WAIT_ABS, sequenceIndex)) {
109  return;
110  }
112  this->sendSignal(this->waitAbs_directiveHandler(directive, error));
113  handleDirectiveErrorCode(Fpy::DirectiveId::WAIT_ABS, error);
114 }
115 
118  U64 sequenceIndex) {
119  if (!this->checkDirectiveExpected(Fpy::DirectiveId::GOTO, sequenceIndex)) {
120  return;
121  }
123  this->sendSignal(this->goto_directiveHandler(directive, error));
124  handleDirectiveErrorCode(Fpy::DirectiveId::GOTO, error);
125 }
126 
129  U64 sequenceIndex) {
130  if (!this->checkDirectiveExpected(Fpy::DirectiveId::IF, sequenceIndex)) {
131  return;
132  }
134  this->sendSignal(this->if_directiveHandler(directive, error));
135  handleDirectiveErrorCode(Fpy::DirectiveId::IF, error);
136 }
137 
140  U64 sequenceIndex) {
141  if (!this->checkDirectiveExpected(Fpy::DirectiveId::NO_OP, sequenceIndex)) {
142  return;
143  }
145  this->sendSignal(this->noOp_directiveHandler(directive, error));
146  handleDirectiveErrorCode(Fpy::DirectiveId::NO_OP, error);
147 }
148 
151  U64 sequenceIndex) {
152  if (!this->checkDirectiveExpected(Fpy::DirectiveId::PUSH_TLM_VAL, sequenceIndex)) {
153  return;
154  }
156  this->sendSignal(this->pushTlmVal_directiveHandler(directive, error));
157  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TLM_VAL, error);
158 }
159 
163  U64 sequenceIndex) {
164  if (!this->checkDirectiveExpected(Fpy::DirectiveId::PUSH_TLM_VAL_AND_TIME, sequenceIndex)) {
165  return;
166  }
168  this->sendSignal(this->pushTlmValAndTime_directiveHandler(directive, error));
169  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TLM_VAL_AND_TIME, error);
170 }
171 
174  U64 sequenceIndex) {
175  if (!this->checkDirectiveExpected(Fpy::DirectiveId::PUSH_PRM, sequenceIndex)) {
176  return;
177  }
179  this->sendSignal(this->pushPrm_directiveHandler(directive, error));
180  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_PRM, error);
181 }
182 
185  U64 sequenceIndex) {
186  if (!this->checkDirectiveExpected(Fpy::DirectiveId::CONST_CMD, sequenceIndex)) {
187  return;
188  }
190  this->sendSignal(this->constCmd_directiveHandler(directive, error));
191  handleDirectiveErrorCode(Fpy::DirectiveId::CONST_CMD, error);
192 }
193 
196  U64 sequenceIndex) {
197  if (!this->checkDirectiveExpected(directive.get__op(), sequenceIndex)) {
198  return;
199  }
201  this->sendSignal(this->stackOp_directiveHandler(directive, error));
202  handleDirectiveErrorCode(directive.get__op(), error);
203 }
204 
207  U64 sequenceIndex) {
208  if (!this->checkDirectiveExpected(Fpy::DirectiveId::EXIT, sequenceIndex)) {
209  return;
210  }
212  this->sendSignal(this->exit_directiveHandler(directive, error));
213  handleDirectiveErrorCode(Fpy::DirectiveId::EXIT, error);
214 }
215 
218  U64 sequenceIndex) {
219  if (!this->checkDirectiveExpected(Fpy::DirectiveId::ALLOCATE, sequenceIndex)) {
220  return;
221  }
223  this->sendSignal(this->allocate_directiveHandler(directive, error));
224  handleDirectiveErrorCode(Fpy::DirectiveId::ALLOCATE, error);
225 }
226 
230  U64 sequenceIndex) {
231  if (!this->checkDirectiveExpected(Fpy::DirectiveId::STORE_REL_CONST_OFFSET, sequenceIndex)) {
232  return;
233  }
235  this->sendSignal(this->storeRelConstOffset_directiveHandler(directive, error));
236  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_REL_CONST_OFFSET, error);
237 }
238 
241  U64 sequenceIndex) {
242  if (!this->checkDirectiveExpected(Fpy::DirectiveId::PUSH_VAL, sequenceIndex)) {
243  return;
244  }
246  this->sendSignal(this->pushVal_directiveHandler(directive, error));
247  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_VAL, error);
248 }
249 
252  U64 sequenceIndex) {
253  if (!this->checkDirectiveExpected(Fpy::DirectiveId::LOAD_REL, sequenceIndex)) {
254  return;
255  }
257  this->sendSignal(this->loadRel_directiveHandler(directive, error));
258  handleDirectiveErrorCode(Fpy::DirectiveId::LOAD_REL, error);
259 }
260 
263  U64 sequenceIndex) {
264  if (!this->checkDirectiveExpected(Fpy::DirectiveId::DISCARD, sequenceIndex)) {
265  return;
266  }
268  this->sendSignal(this->discard_directiveHandler(directive, error));
269  handleDirectiveErrorCode(Fpy::DirectiveId::DISCARD, error);
270 }
271 
274  U64 sequenceIndex) {
275  if (!this->checkDirectiveExpected(Fpy::DirectiveId::MEMCMP, sequenceIndex)) {
276  return;
277  }
279  this->sendSignal(this->memCmp_directiveHandler(directive, error));
280  handleDirectiveErrorCode(Fpy::DirectiveId::MEMCMP, error);
281 }
282 
285  U64 sequenceIndex) {
286  if (!this->checkDirectiveExpected(Fpy::DirectiveId::STACK_CMD, sequenceIndex)) {
287  return;
288  }
290  this->sendSignal(this->stackCmd_directiveHandler(directive, error));
291  handleDirectiveErrorCode(Fpy::DirectiveId::STACK_CMD, error);
292 }
293 
296  U64 sequenceIndex) {
297  if (!this->checkDirectiveExpected(Fpy::DirectiveId::PUSH_TIME, sequenceIndex)) {
298  return;
299  }
301  this->sendSignal(this->pushTime_directiveHandler(directive, error));
302  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TIME, error);
303 }
304 
307  U64 sequenceIndex) {
308  if (!this->checkDirectiveExpected(Fpy::DirectiveId::SET_SEED, sequenceIndex)) {
309  return;
310  }
312  this->sendSignal(this->setSeed_directiveHandler(directive, error));
313  handleDirectiveErrorCode(Fpy::DirectiveId::SET_SEED, error);
314 }
315 
318  U64 sequenceIndex) {
319  if (!this->checkDirectiveExpected(Fpy::DirectiveId::PUSH_RAND, sequenceIndex)) {
320  return;
321  }
323  this->sendSignal(this->pushRand_directiveHandler(directive, error));
324  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_RAND, error);
325 }
326 
329  U64 sequenceIndex) {
330  if (!this->checkDirectiveExpected(Fpy::DirectiveId::GET_FIELD, sequenceIndex)) {
331  return;
332  }
334  this->sendSignal(this->getField_directiveHandler(directive, error));
335  handleDirectiveErrorCode(Fpy::DirectiveId::GET_FIELD, error);
336 }
337 
340  U64 sequenceIndex) {
341  if (!this->checkDirectiveExpected(Fpy::DirectiveId::PEEK, sequenceIndex)) {
342  return;
343  }
345  this->sendSignal(this->peek_directiveHandler(directive, error));
346  handleDirectiveErrorCode(Fpy::DirectiveId::PEEK, error);
347 }
348 
351  U64 sequenceIndex) {
352  if (!this->checkDirectiveExpected(Fpy::DirectiveId::STORE_REL, sequenceIndex)) {
353  return;
354  }
356  this->sendSignal(this->storeRel_directiveHandler(directive, error));
357  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_REL, error);
358 }
359 
362  U64 sequenceIndex) {
363  if (!this->checkDirectiveExpected(Fpy::DirectiveId::CALL, sequenceIndex)) {
364  return;
365  }
367  this->sendSignal(this->call_directiveHandler(directive, error));
368  handleDirectiveErrorCode(Fpy::DirectiveId::CALL, error);
369 }
370 
373  U64 sequenceIndex) {
374  if (!this->checkDirectiveExpected(Fpy::DirectiveId::RETURN, sequenceIndex)) {
375  return;
376  }
378  this->sendSignal(this->return_directiveHandler(directive, error));
379  handleDirectiveErrorCode(Fpy::DirectiveId::RETURN, error);
380 }
381 
384  U64 sequenceIndex) {
385  if (!this->checkDirectiveExpected(Fpy::DirectiveId::LOAD_ABS, sequenceIndex)) {
386  return;
387  }
389  this->sendSignal(this->loadAbs_directiveHandler(directive, error));
390  handleDirectiveErrorCode(Fpy::DirectiveId::LOAD_ABS, error);
391 }
392 
395  U64 sequenceIndex) {
396  if (!this->checkDirectiveExpected(Fpy::DirectiveId::STORE_ABS, sequenceIndex)) {
397  return;
398  }
400  this->sendSignal(this->storeAbs_directiveHandler(directive, error));
401  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_ABS, error);
402 }
403 
407  U64 sequenceIndex) {
408  if (!this->checkDirectiveExpected(Fpy::DirectiveId::STORE_ABS_CONST_OFFSET, sequenceIndex)) {
409  return;
410  }
412  this->sendSignal(this->storeAbsConstOffset_directiveHandler(directive, error));
413  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_ABS_CONST_OFFSET, error);
414 }
415 
418  U64 sequenceIndex) {
419  if (!this->checkDirectiveExpected(Fpy::DirectiveId::POP_EVENT, sequenceIndex)) {
420  return;
421  }
423  this->sendSignal(this->popEvent_directiveHandler(directive, error));
424  handleDirectiveErrorCode(Fpy::DirectiveId::POP_EVENT, error);
425 }
426 
430  U64 sequenceIndex) {
431  if (!this->checkDirectiveExpected(Fpy::DirectiveId::POP_SERIALIZABLE, sequenceIndex)) {
432  return;
433  }
435  this->sendSignal(this->popSerializable_directiveHandler(directive, error));
436  handleDirectiveErrorCode(Fpy::DirectiveId::POP_SERIALIZABLE, error);
437 }
438 
440 Signal FpySequencer::waitRel_directiveHandler(const FpySequencer_WaitRelDirective& directive, DirectiveError& error) {
441  if (this->m_runtime.stack.size < 8) {
444  }
445 
446  Fw::Time wakeupTime = this->getTime();
447 
448  U32 uSeconds = this->m_runtime.stack.pop<U32>();
449  U32 seconds = this->m_runtime.stack.pop<U32>();
450 
451  if (uSeconds >= 1000000) {
454  }
455 
456  wakeupTime.add(seconds, uSeconds);
457  this->m_runtime.wakeupTime = wakeupTime;
459 }
460 
462 Signal FpySequencer::waitAbs_directiveHandler(const FpySequencer_WaitAbsDirective& directive, DirectiveError& error) {
463  if (this->m_runtime.stack.size < 2 * sizeof(U32) + sizeof(FwTimeContextStoreType) + sizeof(FwTimeBaseStoreType)) {
466  }
467 
468  U32 uSeconds = this->m_runtime.stack.pop<U32>();
469  U32 seconds = this->m_runtime.stack.pop<U32>();
470  FwTimeContextStoreType ctx = this->m_runtime.stack.pop<FwTimeContextStoreType>();
471  FwTimeBaseStoreType base = this->m_runtime.stack.pop<FwTimeBaseStoreType>();
472 
473  if (uSeconds >= 1000000) {
476  }
477 
478  this->m_runtime.wakeupTime = Fw::Time(static_cast<TimeBase::T>(base), ctx, seconds, uSeconds);
480 }
481 
483 Signal FpySequencer::goto_directiveHandler(const FpySequencer_GotoDirective& directive, DirectiveError& error) {
484  // check within sequence bounds, or at EOF (we allow == case cuz this just ends the sequence)
485  if (directive.get_statementIndex() > m_sequenceObj.get_header().get_statementCount()) {
488  }
489  m_runtime.nextStatementIndex = directive.get_statementIndex();
491 }
492 
494 Signal FpySequencer::if_directiveHandler(const FpySequencer_IfDirective& directive, DirectiveError& error) {
495  if (this->m_runtime.stack.size < 1) {
498  }
499  // check within sequence bounds, or at EOF (we allow == case cuz this just ends the sequence)
500  if (directive.get_falseGotoStmtIndex() > m_sequenceObj.get_header().get_statementCount()) {
503  }
504 
505  if (this->m_runtime.stack.pop<U8>() != 0) {
506  // proceed to next instruction
508  }
509 
510  // conditional false case
511  this->m_runtime.nextStatementIndex = directive.get_falseGotoStmtIndex();
513 }
514 
515 Signal FpySequencer::noOp_directiveHandler(const FpySequencer_NoOpDirective& directive, DirectiveError& error) {
517 }
518 
519 Signal FpySequencer::pushTlmVal_directiveHandler(const FpySequencer_PushTlmValDirective& directive,
520  DirectiveError& error) {
521  if (!this->isConnected_getTlmChan_OutputPort(0)) {
524  }
525  Fw::Time tlmTime;
526  Fw::TlmBuffer tlmValue;
527  Fw::TlmValid valid = this->getTlmChan_out(0, directive.get_chanId(), tlmTime, tlmValue);
528 
529  if (valid != Fw::TlmValid::VALID) {
530  // could not find this tlm chan
533  }
534 
535  if (tlmValue.getSize() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
538  }
539  this->m_runtime.stack.push(tlmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(tlmValue.getSize()));
541 }
542 
543 Signal FpySequencer::pushTlmValAndTime_directiveHandler(const FpySequencer_PushTlmValAndTimeDirective& directive,
544  DirectiveError& error) {
545  if (!this->isConnected_getTlmChan_OutputPort(0)) {
548  }
549 
550  Fw::Time tlmTime;
551  Fw::TlmBuffer tlmValue;
552  Fw::TlmValid valid = this->getTlmChan_out(0, directive.get_chanId(), tlmTime, tlmValue);
553 
554  if (valid != Fw::TlmValid::VALID) {
555  // could not find this tlm chan
558  }
559 
560  U8 tlmTimeBuf[Fw::Time::SERIALIZED_SIZE] = {};
562  Fw::SerializeStatus stat = timeEsb.serializeFrom(tlmTime);
563 
564  // coding error if this failed, we should have enough space
565  FW_ASSERT(stat == Fw::SerializeStatus::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(stat));
566 
567  // check that our stack won't overflow if we put both val and time on it
568  if (tlmValue.getSize() + timeEsb.getSize() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
571  }
572 
573  // push tlm to end of stack
574  this->m_runtime.stack.push(tlmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(tlmValue.getSize()));
575  // now push time to end of stack
576  this->m_runtime.stack.push(timeEsb.getBuffAddr(), static_cast<Fpy::StackSizeType>(timeEsb.getSize()));
578 }
579 
580 Signal FpySequencer::pushPrm_directiveHandler(const FpySequencer_PushPrmDirective& directive, DirectiveError& error) {
581  if (!this->isConnected_getParam_OutputPort(0)) {
584  }
585 
586  Fw::ParamBuffer prmValue;
587  Fw::ParamValid valid = this->getParam_out(0, directive.get_prmId(), prmValue);
588 
589  if (valid != Fw::ParamValid::VALID) {
590  // could not find this prm in the DB
593  }
594 
595  if (prmValue.getSize() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
598  }
599 
600  this->m_runtime.stack.push(prmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(prmValue.getSize()));
602 }
603 
604 Signal FpySequencer::constCmd_directiveHandler(const FpySequencer_ConstCmdDirective& directive, DirectiveError& error) {
605  // the cmd response code will be pushed to the stack when it comes back, so make sure
606  // there is room for it now, before the cmd is dispatched
607  if (sizeof(Fw::CmdResponse::SerialType) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
610  }
611  if (this->sendCmd(directive.get_opCode(), directive.get_argBuf(), directive.get__argBufSize()) ==
614  } else {
615  // now tell the SM to wait some more until we get the cmd response back
616  // if we've already got the response back this should be harmless
618  }
619 }
620 
621 DirectiveError FpySequencer::op_or() {
622  if (this->m_runtime.stack.size < sizeof(U8) * 2) {
624  }
625  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<U8>() | this->m_runtime.stack.pop<U8>()));
627 }
628 DirectiveError FpySequencer::op_and() {
629  if (this->m_runtime.stack.size < sizeof(U8) * 2) {
631  }
632  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<U8>() & this->m_runtime.stack.pop<U8>()));
634 }
635 DirectiveError FpySequencer::op_ieq() {
636  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
638  }
639  this->m_runtime.stack.push(static_cast<U8>((this->m_runtime.stack.pop<I64>() == this->m_runtime.stack.pop<I64>())
640  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
641  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
643 }
644 DirectiveError FpySequencer::op_ine() {
645  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
647  }
648  this->m_runtime.stack.push(static_cast<U8>((this->m_runtime.stack.pop<I64>() != this->m_runtime.stack.pop<I64>())
649  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
650  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
652 }
653 DirectiveError FpySequencer::op_ult() {
654  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
656  }
657  U64 rhs = this->m_runtime.stack.pop<U64>();
658  U64 lhs = this->m_runtime.stack.pop<U64>();
659  this->m_runtime.stack.push(static_cast<U8>((lhs < rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
660  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
662 }
663 DirectiveError FpySequencer::op_ule() {
664  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
666  }
667  U64 rhs = this->m_runtime.stack.pop<U64>();
668  U64 lhs = this->m_runtime.stack.pop<U64>();
669  this->m_runtime.stack.push(static_cast<U8>((lhs <= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
670  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
672 }
673 DirectiveError FpySequencer::op_ugt() {
674  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
676  }
677  U64 rhs = this->m_runtime.stack.pop<U64>();
678  U64 lhs = this->m_runtime.stack.pop<U64>();
679  this->m_runtime.stack.push(static_cast<U8>((lhs > rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
680  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
682 }
683 DirectiveError FpySequencer::op_uge() {
684  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
686  }
687  U64 rhs = this->m_runtime.stack.pop<U64>();
688  U64 lhs = this->m_runtime.stack.pop<U64>();
689  this->m_runtime.stack.push(static_cast<U8>((lhs >= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
690  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
692 }
693 DirectiveError FpySequencer::op_slt() {
694  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
696  }
697  I64 rhs = this->m_runtime.stack.pop<I64>();
698  I64 lhs = this->m_runtime.stack.pop<I64>();
699  this->m_runtime.stack.push(static_cast<U8>((lhs < rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
700  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
702 }
703 DirectiveError FpySequencer::op_sle() {
704  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
706  }
707  I64 rhs = this->m_runtime.stack.pop<I64>();
708  I64 lhs = this->m_runtime.stack.pop<I64>();
709  this->m_runtime.stack.push(static_cast<U8>((lhs <= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
710  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
712 }
713 DirectiveError FpySequencer::op_sgt() {
714  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
716  }
717  I64 rhs = this->m_runtime.stack.pop<I64>();
718  I64 lhs = this->m_runtime.stack.pop<I64>();
719  this->m_runtime.stack.push(static_cast<U8>((lhs > rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
720  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
722 }
723 DirectiveError FpySequencer::op_sge() {
724  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
726  }
727  I64 rhs = this->m_runtime.stack.pop<I64>();
728  I64 lhs = this->m_runtime.stack.pop<I64>();
729  this->m_runtime.stack.push(static_cast<U8>((lhs >= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
730  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
732 }
733 DirectiveError FpySequencer::op_feq() {
734  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
736  }
737  F64 rhs = this->m_runtime.stack.pop<F64>();
738  F64 lhs = this->m_runtime.stack.pop<F64>();
739  // eq is true if they are equal and neither is nan
740  this->m_runtime.stack.push(static_cast<U8>((lhs == rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
741  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
743 }
744 DirectiveError FpySequencer::op_fne() {
745  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
747  }
748  F64 rhs = this->m_runtime.stack.pop<F64>();
749  F64 lhs = this->m_runtime.stack.pop<F64>();
750  // ne is true if they are not equal or either is nan
751  this->m_runtime.stack.push(static_cast<U8>((lhs != rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
752  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
754 }
755 DirectiveError FpySequencer::op_flt() {
756  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
758  }
759  F64 rhs = this->m_runtime.stack.pop<F64>();
760  F64 lhs = this->m_runtime.stack.pop<F64>();
761  this->m_runtime.stack.push(static_cast<U8>(std::isless(lhs, rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
762  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
764 }
765 DirectiveError FpySequencer::op_fle() {
766  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
768  }
769  F64 rhs = this->m_runtime.stack.pop<F64>();
770  F64 lhs = this->m_runtime.stack.pop<F64>();
771  this->m_runtime.stack.push(static_cast<U8>(std::islessequal(lhs, rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
772  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
774 }
775 DirectiveError FpySequencer::op_fgt() {
776  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
778  }
779  F64 rhs = this->m_runtime.stack.pop<F64>();
780  F64 lhs = this->m_runtime.stack.pop<F64>();
781  this->m_runtime.stack.push(static_cast<U8>(std::isgreater(lhs, rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
782  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
784 }
785 DirectiveError FpySequencer::op_fge() {
786  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
788  }
789  F64 rhs = this->m_runtime.stack.pop<F64>();
790  F64 lhs = this->m_runtime.stack.pop<F64>();
791  this->m_runtime.stack.push(static_cast<U8>(std::isgreaterequal(lhs, rhs)
792  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
793  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
795 }
796 DirectiveError FpySequencer::op_not() {
797  if (this->m_runtime.stack.size < sizeof(U8)) {
799  }
800  this->m_runtime.stack.push(static_cast<U8>((this->m_runtime.stack.pop<U8>() == 0)
801  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
802  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
804 }
805 DirectiveError FpySequencer::op_fpext() {
806  // convert F32 to F64
807  if (this->m_runtime.stack.size < sizeof(F32)) {
809  }
810  // the result is wider than the operand, so it needs room to grow
811  if (sizeof(F64) - sizeof(F32) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
813  }
814  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<F32>()));
816 }
817 DirectiveError FpySequencer::op_fptrunc() {
818  // convert F64 to F32
819  if (this->m_runtime.stack.size < sizeof(F64)) {
821  }
822  this->m_runtime.stack.push(static_cast<F32>(this->m_runtime.stack.pop<F64>()));
824 }
825 DirectiveError FpySequencer::op_fptosi() {
826  if (this->m_runtime.stack.size < sizeof(F64)) {
828  }
829  F64 val = this->m_runtime.stack.pop<F64>();
830  // NaN -> 0, out-of-range clamps, in-range truncates toward
831  // zero. The raw static_cast is UB for NaN and out-of-range values.
832  // 2^63 is exactly representable as F64 and is one past I64 max; -2^63
833  // is exactly I64 min and in range.
834  const F64 bound = std::ldexp(1.0, 63);
835  I64 result;
836  if (std::isnan(val)) {
837  result = 0;
838  } else if (val >= bound) {
839  result = std::numeric_limits<I64>::max();
840  } else if (val < -bound) {
842  } else {
843  result = static_cast<I64>(val);
844  }
845  this->m_runtime.stack.push(result);
847 }
848 DirectiveError FpySequencer::op_sitofp() {
849  if (this->m_runtime.stack.size < sizeof(I64)) {
851  }
852  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<I64>()));
854 }
855 DirectiveError FpySequencer::op_fptoui() {
856  if (this->m_runtime.stack.size < sizeof(F64)) {
858  }
859  F64 val = this->m_runtime.stack.pop<F64>();
860  // NaN -> 0, negatives truncate to at most 0 and clamp there,
861  // 2^64 (one past U64 max) and above clamp to U64 max. The raw
862  // static_cast is UB for NaN and out-of-range values.
863  const F64 bound = std::ldexp(1.0, 64);
864  U64 result;
865  if (std::isnan(val) || val < 0.0) {
866  result = 0;
867  } else if (val >= bound) {
868  result = std::numeric_limits<U64>::max();
869  } else {
870  result = static_cast<U64>(val);
871  }
872  this->m_runtime.stack.push(result);
874 }
875 DirectiveError FpySequencer::op_uitofp() {
876  if (this->m_runtime.stack.size < sizeof(U64)) {
878  }
879  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<U64>()));
881 }
882 DirectiveError FpySequencer::op_add() {
883  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
885  }
886  I64 rhs = this->m_runtime.stack.pop<I64>();
887  I64 lhs = this->m_runtime.stack.pop<I64>();
888  // Check for overflow and underflow and return the appropriate error code
889  // Overflow can only occur with both operands positive and occurs when one operand is greater than the maximum value
890  // less the other operand. If either operand is negative or zero, overflow cannot occur.
891  if ((rhs > 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() - rhs) < lhs)) {
893  }
894  // Underflow can only occur with both operands negative and occurs when one operand is less than the minimum value
895  // minus the other operand. If either operand is positive or zero, underflow cannot occur.
896  else if ((rhs < 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() - rhs) > lhs)) {
898  }
899  this->m_runtime.stack.push(static_cast<I64>(lhs + rhs));
901 }
902 DirectiveError FpySequencer::op_sub() {
903  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
905  }
906  I64 rhs = this->m_runtime.stack.pop<I64>();
907  I64 lhs = this->m_runtime.stack.pop<I64>();
908  // Check for overflow and underflow and return the appropriate error code
909  // Overflow can only occur when the left operand is positive and the right operand is negative. It occurs when the
910  // left (positive) operand is greater than the maximum value plus the other (negative) operand. If the right
911  // operand is positive or zero, overflow cannot occur.
912  if ((rhs < 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() + rhs) < lhs)) {
914  }
915  // Underflow can only occur when the left operand is negative and the right operand is positive. It occurs when the
916  // left (negative) operand is less than the minimum value plus the other (positive) operand. If the right operand
917  // is negative or zero, underflow cannot occur.
918  else if ((rhs > 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() + rhs) > lhs)) {
920  }
921  this->m_runtime.stack.push(static_cast<I64>(lhs - rhs));
923 }
924 DirectiveError FpySequencer::op_mul() {
925  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
927  }
928  I64 rhs = this->m_runtime.stack.pop<I64>();
929  I64 lhs = this->m_runtime.stack.pop<I64>();
930  // Check for overflow and underflow and return the appropriate error code
931  // Overflow can only occur with operands of matching signs and occurs when one operand is greater (or less) than the
932  // maximum value divided by the other operand. Either operand being zero precludes overflow.
933  // Check the both positive case.
934  if ((rhs > 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() / rhs) < lhs)) {
936  }
937  // Check the both negative case. Compare without negation: negating a value of min is undefined behavior
938  else if ((rhs < 0) && (lhs < 0) && (lhs < (std::numeric_limits<I64>::max() / rhs))) {
940  }
941  // Underflow can occur with operands of differing signs and occurs when one operand is less than the minimum value
942  // divided by the other operand. Either operand being zero precludes underflow.
943  // Check the case where lhs is positive.
944  else if ((rhs < 0) && (lhs > 0) && ((std::numeric_limits<I64>::min() / lhs) > rhs)) {
946  }
947  // Check the case where rhs is positive.
948  else if ((rhs > 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() / rhs) > lhs)) {
950  }
951  this->m_runtime.stack.push(static_cast<I64>(lhs * rhs));
953 }
954 DirectiveError FpySequencer::op_udiv() {
955  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
957  }
958  U64 rhs = this->m_runtime.stack.pop<U64>();
959  U64 lhs = this->m_runtime.stack.pop<U64>();
960  // Prevent division by zero
961  if (rhs == 0) {
963  }
964  this->m_runtime.stack.push(static_cast<U64>(lhs / rhs));
966 }
967 DirectiveError FpySequencer::op_sdiv() {
968  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
970  }
971 
972  I64 rhs = this->m_runtime.stack.pop<I64>();
973  I64 lhs = this->m_runtime.stack.pop<I64>();
974  // Prevent division by zero
975  if (rhs == 0) {
977  }
978  // The one signed division that can overflow: |I64 min / -1| = 2^63 is not
979  // representable (and the C++ expression is UB, SIGFPE on x86)
980  if ((lhs == std::numeric_limits<I64>::min()) && (rhs == -1)) {
982  }
983  // C++ / truncates toward zero; adjust to match Python's floored division:
984  // an inexact quotient with differing operand signs floors one below the
985  // truncated result. This mirrors op_smod.
986  I64 quotient = lhs / rhs;
987  if (((lhs % rhs) != 0) && ((lhs < 0) != (rhs < 0))) {
988  quotient -= 1;
989  }
990  this->m_runtime.stack.push(quotient);
992 }
993 DirectiveError FpySequencer::op_umod() {
994  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
996  }
997  U64 rhs = this->m_runtime.stack.pop<U64>();
998  if (rhs == 0) {
1000  }
1001  U64 lhs = this->m_runtime.stack.pop<U64>();
1002  this->m_runtime.stack.push(static_cast<U64>(lhs % rhs));
1003  return DirectiveError::NO_ERROR;
1004 }
1005 DirectiveError FpySequencer::op_smod() {
1006  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
1008  }
1009  I64 rhs = this->m_runtime.stack.pop<I64>();
1010  if (rhs == 0) {
1012  }
1013  I64 lhs = this->m_runtime.stack.pop<I64>();
1014  // I64 min % -1 is 0, the mathematical remainder (matching wasm i64.rem_s),
1015  // but the C++ expression is UB (SIGFPE on x86) so it must be special-cased
1016  if ((lhs == std::numeric_limits<I64>::min()) && (rhs == -1)) {
1017  this->m_runtime.stack.push(static_cast<I64>(0));
1018  return DirectiveError::NO_ERROR;
1019  }
1020  I64 res = static_cast<I64>(lhs % rhs);
1021  // in order to match Python's behavior,
1022  // if the signs of the remainder and divisor differ, adjust the result.
1023  // this happens when the result should be positive but is negative, or vice-versa.
1024  // credit Gemini 2.5 pro
1025  if ((res > 0 && rhs < 0) || (res < 0 && rhs > 0)) {
1026  res += rhs;
1027  }
1028  this->m_runtime.stack.push(res);
1029  return DirectiveError::NO_ERROR;
1030 }
1031 DirectiveError FpySequencer::op_fadd() {
1032  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
1034  }
1035  F64 rhs = this->m_runtime.stack.pop<F64>();
1036  F64 lhs = this->m_runtime.stack.pop<F64>();
1037  this->m_runtime.stack.push(static_cast<F64>(lhs + rhs));
1038  return DirectiveError::NO_ERROR;
1039 }
1040 DirectiveError FpySequencer::op_fsub() {
1041  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
1043  }
1044  F64 rhs = this->m_runtime.stack.pop<F64>();
1045  F64 lhs = this->m_runtime.stack.pop<F64>();
1046  this->m_runtime.stack.push(static_cast<F64>(lhs - rhs));
1047  return DirectiveError::NO_ERROR;
1048 }
1049 DirectiveError FpySequencer::op_fmul() {
1050  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
1052  }
1053  F64 rhs = this->m_runtime.stack.pop<F64>();
1054  F64 lhs = this->m_runtime.stack.pop<F64>();
1055  this->m_runtime.stack.push(static_cast<F64>(lhs * rhs));
1056  return DirectiveError::NO_ERROR;
1057 }
1058 DirectiveError FpySequencer::op_fdiv() {
1059  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
1061  }
1062  F64 rhs = this->m_runtime.stack.pop<F64>();
1063  F64 lhs = this->m_runtime.stack.pop<F64>();
1064  this->m_runtime.stack.push(static_cast<F64>(lhs / rhs));
1065  return DirectiveError::NO_ERROR;
1066 }
1067 DirectiveError FpySequencer::op_fpow() {
1068  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
1070  }
1071  F64 rhs = this->m_runtime.stack.pop<F64>();
1072  F64 lhs = this->m_runtime.stack.pop<F64>();
1073  this->m_runtime.stack.push(static_cast<F64>(pow(lhs, rhs)));
1074  return DirectiveError::NO_ERROR;
1075 }
1076 DirectiveError FpySequencer::op_flog() {
1077  if (this->m_runtime.stack.size < sizeof(F64)) {
1079  }
1080  F64 val = this->m_runtime.stack.pop<F64>();
1081  if (val <= 0.0) {
1083  }
1084  this->m_runtime.stack.push(static_cast<F64>(log(val)));
1085  return DirectiveError::NO_ERROR;
1086 }
1087 DirectiveError FpySequencer::op_fmod() {
1088  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
1090  }
1091  F64 rhs = this->m_runtime.stack.pop<F64>();
1092  F64 lhs = this->m_runtime.stack.pop<F64>();
1093  // std::fmod computes the exact truncated remainder (sign of lhs) with no
1094  // intermediate rounding. A zero divisor yields NaN, matching Rust and C#.
1095  F64 res = std::fmod(lhs, rhs);
1096  // Adjust to match Python's floored-modulo semantics: if the signs of the
1097  // remainder and divisor differ, add the divisor once. This mirrors op_smod
1098  // and is the exact frem + fadd the VM model computes (at most one rounded add).
1099  if ((res > 0 && rhs < 0) || (res < 0 && rhs > 0)) {
1100  res += rhs;
1101  } else if (res == 0) {
1102  // Python normalizes an exact-multiple result so the zero carries the
1103  // divisor's sign (CPython float_rem); fmod leaves the dividend's.
1104  res = std::copysign(0.0, rhs);
1105  }
1106  this->m_runtime.stack.push(res);
1107  return DirectiveError::NO_ERROR;
1108 }
1109 DirectiveError FpySequencer::op_siext_8_64() {
1110  if (this->m_runtime.stack.size < sizeof(I8)) {
1112  }
1113  // the result is wider than the operand, so it needs room to grow
1114  if (sizeof(I64) - sizeof(I8) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1116  }
1117  I8 src = this->m_runtime.stack.pop<I8>();
1118  this->m_runtime.stack.push(static_cast<I64>(src));
1119  return DirectiveError::NO_ERROR;
1120 }
1121 DirectiveError FpySequencer::op_siext_16_64() {
1122  if (this->m_runtime.stack.size < sizeof(I16)) {
1124  }
1125  // the result is wider than the operand, so it needs room to grow
1126  if (sizeof(I64) - sizeof(I16) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1128  }
1129  I16 src = this->m_runtime.stack.pop<I16>();
1130  this->m_runtime.stack.push(static_cast<I64>(src));
1131  return DirectiveError::NO_ERROR;
1132 }
1133 DirectiveError FpySequencer::op_siext_32_64() {
1134  if (this->m_runtime.stack.size < sizeof(I32)) {
1136  }
1137  // the result is wider than the operand, so it needs room to grow
1138  if (sizeof(I64) - sizeof(I32) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1140  }
1141  I32 src = this->m_runtime.stack.pop<I32>();
1142  this->m_runtime.stack.push(static_cast<I64>(src));
1143  return DirectiveError::NO_ERROR;
1144 }
1145 DirectiveError FpySequencer::op_ziext_8_64() {
1146  if (this->m_runtime.stack.size < sizeof(U8)) {
1148  }
1149  // the result is wider than the operand, so it needs room to grow
1150  if (sizeof(U64) - sizeof(U8) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1152  }
1153  U8 src = this->m_runtime.stack.pop<U8>();
1154  this->m_runtime.stack.push(static_cast<U64>(src));
1155  return DirectiveError::NO_ERROR;
1156 }
1157 DirectiveError FpySequencer::op_ziext_16_64() {
1158  if (this->m_runtime.stack.size < sizeof(U16)) {
1160  }
1161  // the result is wider than the operand, so it needs room to grow
1162  if (sizeof(U64) - sizeof(U16) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1164  }
1165  U16 src = this->m_runtime.stack.pop<U16>();
1166  this->m_runtime.stack.push(static_cast<U64>(src));
1167  return DirectiveError::NO_ERROR;
1168 }
1169 DirectiveError FpySequencer::op_ziext_32_64() {
1170  if (this->m_runtime.stack.size < sizeof(U32)) {
1172  }
1173  // the result is wider than the operand, so it needs room to grow
1174  if (sizeof(U64) - sizeof(U32) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1176  }
1177  U32 src = this->m_runtime.stack.pop<U32>();
1178  this->m_runtime.stack.push(static_cast<U64>(src));
1179  return DirectiveError::NO_ERROR;
1180 }
1181 DirectiveError FpySequencer::op_itrunc_64_8() {
1182  if (this->m_runtime.stack.size < sizeof(U64)) {
1184  }
1185  U64 src = this->m_runtime.stack.pop<U64>();
1186  this->m_runtime.stack.push(static_cast<U8>(src));
1187  return DirectiveError::NO_ERROR;
1188 }
1189 DirectiveError FpySequencer::op_itrunc_64_16() {
1190  if (this->m_runtime.stack.size < sizeof(U64)) {
1192  }
1193  U64 src = this->m_runtime.stack.pop<U64>();
1194  this->m_runtime.stack.push(static_cast<U16>(src));
1195  return DirectiveError::NO_ERROR;
1196 }
1197 DirectiveError FpySequencer::op_itrunc_64_32() {
1198  if (this->m_runtime.stack.size < sizeof(U64)) {
1200  }
1201  U64 src = this->m_runtime.stack.pop<U64>();
1202  this->m_runtime.stack.push(static_cast<U32>(src));
1203  return DirectiveError::NO_ERROR;
1204 }
1205 DirectiveError FpySequencer::op_ffloor() {
1206  if (this->m_runtime.stack.size < sizeof(F64)) {
1208  }
1209  F64 val = this->m_runtime.stack.pop<F64>();
1210  // std::floor implements IEEE 754 roundToIntegralTowardNegative: +-0, +-inf
1211  // and NaN pass through, and the sign of a zero is preserved.
1212  this->m_runtime.stack.push(std::floor(val));
1213  return DirectiveError::NO_ERROR;
1214 }
1215 DirectiveError FpySequencer::op_iabs() {
1216  if (this->m_runtime.stack.size < sizeof(I64)) {
1218  }
1219  I64 val = this->m_runtime.stack.pop<I64>();
1220  // abs(I64 min) is not representable in I64 (and -val on it is UB)
1221  if (val == std::numeric_limits<I64>::min()) {
1223  }
1224  this->m_runtime.stack.push(val < 0 ? -val : val);
1225  return DirectiveError::NO_ERROR;
1226 }
1227 DirectiveError FpySequencer::op_fabs() {
1228  if (this->m_runtime.stack.size < sizeof(F64)) {
1230  }
1231  F64 val = this->m_runtime.stack.pop<F64>();
1232  // IEEE 754 abs: clears the sign bit and changes nothing else, so NaN
1233  // payloads pass through.
1234  this->m_runtime.stack.push(std::fabs(val));
1235  return DirectiveError::NO_ERROR;
1236 }
1237 Signal FpySequencer::stackOp_directiveHandler(const FpySequencer_StackOpDirective& directive, DirectiveError& error) {
1238  // coding error, should not have gotten to this stack op handler
1239  FW_ASSERT((directive.get__op() >= Fpy::DirectiveId::OR && directive.get__op() <= Fpy::DirectiveId::ITRUNC_64_32) ||
1240  (directive.get__op() >= Fpy::DirectiveId::FFLOOR && directive.get__op() <= Fpy::DirectiveId::FABS),
1241  static_cast<FwAssertArgType>(directive.get__op()));
1242 
1243  switch (directive.get__op()) {
1244  case Fpy::DirectiveId::OR:
1245  error = this->op_or();
1246  break;
1247  case Fpy::DirectiveId::AND:
1248  error = this->op_and();
1249  break;
1250  case Fpy::DirectiveId::IEQ:
1251  error = this->op_ieq();
1252  break;
1253  case Fpy::DirectiveId::INE:
1254  error = this->op_ine();
1255  break;
1256  case Fpy::DirectiveId::ULT:
1257  error = this->op_ult();
1258  break;
1259  case Fpy::DirectiveId::ULE:
1260  error = this->op_ule();
1261  break;
1262  case Fpy::DirectiveId::UGT:
1263  error = this->op_ugt();
1264  break;
1265  case Fpy::DirectiveId::UGE:
1266  error = this->op_uge();
1267  break;
1268  case Fpy::DirectiveId::SLT:
1269  error = this->op_slt();
1270  break;
1271  case Fpy::DirectiveId::SLE:
1272  error = this->op_sle();
1273  break;
1274  case Fpy::DirectiveId::SGT:
1275  error = this->op_sgt();
1276  break;
1277  case Fpy::DirectiveId::SGE:
1278  error = this->op_sge();
1279  break;
1280  case Fpy::DirectiveId::FEQ:
1281  error = this->op_feq();
1282  break;
1283  case Fpy::DirectiveId::FNE:
1284  error = this->op_fne();
1285  break;
1286  case Fpy::DirectiveId::FLT:
1287  error = this->op_flt();
1288  break;
1289  case Fpy::DirectiveId::FLE:
1290  error = this->op_fle();
1291  break;
1292  case Fpy::DirectiveId::FGT:
1293  error = this->op_fgt();
1294  break;
1295  case Fpy::DirectiveId::FGE:
1296  error = this->op_fge();
1297  break;
1298  case Fpy::DirectiveId::NOT:
1299  error = this->op_not();
1300  break;
1302  error = this->op_fpext();
1303  break;
1305  error = this->op_fptrunc();
1306  break;
1308  error = this->op_fptosi();
1309  break;
1311  error = this->op_fptoui();
1312  break;
1314  error = this->op_sitofp();
1315  break;
1317  error = this->op_uitofp();
1318  break;
1319  case Fpy::DirectiveId::ADD:
1320  error = this->op_add();
1321  break;
1322  case Fpy::DirectiveId::SUB:
1323  error = this->op_sub();
1324  break;
1325  case Fpy::DirectiveId::MUL:
1326  error = this->op_mul();
1327  break;
1329  error = this->op_udiv();
1330  break;
1332  error = this->op_sdiv();
1333  break;
1335  error = this->op_umod();
1336  break;
1338  error = this->op_smod();
1339  break;
1341  error = this->op_fadd();
1342  break;
1344  error = this->op_fsub();
1345  break;
1347  error = this->op_fmul();
1348  break;
1350  error = this->op_fdiv();
1351  break;
1353  error = this->op_fpow();
1354  break;
1356  error = this->op_flog();
1357  break;
1359  error = this->op_fmod();
1360  break;
1362  error = this->op_siext_8_64();
1363  break;
1365  error = this->op_siext_16_64();
1366  break;
1368  error = this->op_siext_32_64();
1369  break;
1371  error = this->op_ziext_8_64();
1372  break;
1374  error = this->op_ziext_16_64();
1375  break;
1377  error = this->op_ziext_32_64();
1378  break;
1380  error = this->op_itrunc_64_8();
1381  break;
1383  error = this->op_itrunc_64_16();
1384  break;
1386  error = this->op_itrunc_64_32();
1387  break;
1389  error = this->op_ffloor();
1390  break;
1392  error = this->op_iabs();
1393  break;
1395  error = this->op_fabs();
1396  break;
1397  default:
1398  FW_ASSERT(false, directive.get__op());
1399  break;
1400  }
1401  if (error != DirectiveError::NO_ERROR) {
1403  }
1405 }
1406 
1407 Signal FpySequencer::exit_directiveHandler(const FpySequencer_ExitDirective& directive, DirectiveError& error) {
1408  if (this->m_runtime.stack.size < sizeof(I32)) {
1411  }
1412  I32 errorCode = this->m_runtime.stack.pop<I32>();
1413  // exit(0), no error
1414  if (errorCode == 0) {
1415  // just goto the end of the sequence
1416  this->m_runtime.nextStatementIndex = this->m_sequenceObj.get_header().get_statementCount();
1418  }
1419  // otherwise, kill the sequence here
1420  // raise the user defined error code as an event
1421  this->log_WARNING_HI_SequenceExitedWithError(this->m_sequenceFilePath, errorCode);
1424 }
1425 
1426 Signal FpySequencer::allocate_directiveHandler(const FpySequencer_AllocateDirective& directive, DirectiveError& error) {
1427  if (directive.get_size() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1430  }
1431  this->m_runtime.stack.pushZeroes(directive.get_size());
1433 }
1434 
1436 Signal FpySequencer::storeHelper(Fpy::StackSizeType destOffset, Fpy::StackSizeType size, DirectiveError& error) {
1437  if (this->m_runtime.stack.size < size) {
1440  }
1441  // After popping the value, would the write go out of bounds?
1442  Fpy::StackSizeType newStackSize = this->m_runtime.stack.size - size;
1443  // Overflow-safe check: destOffset + size > newStackSize
1444  // Rewritten as: check destOffset <= newStackSize first, then size > newStackSize - destOffset
1445  if (destOffset > newStackSize || size > newStackSize - destOffset) {
1448  }
1449  // Copy value to the destination location
1450  this->m_runtime.stack.copy(destOffset, this->m_runtime.stack.size - size, size);
1451  this->m_runtime.stack.size = newStackSize;
1453 }
1454 
1456 Signal FpySequencer::loadHelper(Fpy::StackSizeType srcOffset, Fpy::StackSizeType size, DirectiveError& error) {
1457  if (size > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1460  }
1461  // Overflow-safe check: srcOffset + size > stack.size
1462  // Rewritten as: check srcOffset <= stack.size first, then size > stack.size - srcOffset
1463  if (srcOffset > this->m_runtime.stack.size || size > this->m_runtime.stack.size - srcOffset) {
1466  }
1467  // Copy from source location to top of stack
1468  this->m_runtime.stack.copy(this->m_runtime.stack.size, srcOffset, size);
1469  this->m_runtime.stack.size += size;
1471 }
1472 
1473 Signal FpySequencer::storeRelConstOffset_directiveHandler(const FpySequencer_StoreRelConstOffsetDirective& directive,
1474  DirectiveError& error) {
1475  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + directive.get_lvarOffset();
1476  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1479  }
1480  return this->storeHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1481 }
1482 
1483 Signal FpySequencer::loadRel_directiveHandler(const FpySequencer_LoadRelDirective& directive, DirectiveError& error) {
1484  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + directive.get_lvarOffset();
1485  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1488  }
1489  return this->loadHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1490 }
1491 
1492 Signal FpySequencer::pushVal_directiveHandler(const FpySequencer_PushValDirective& directive, DirectiveError& error) {
1493  if (directive.get__valSize() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1496  }
1497  // copy from the bytearray in the directive to the stack, add to stack size.
1498  this->m_runtime.stack.push(const_cast<U8*>(directive.get_val()),
1499  static_cast<Fpy::StackSizeType>(directive.get__valSize()));
1501 }
1502 
1503 Signal FpySequencer::discard_directiveHandler(const FpySequencer_DiscardDirective& directive, DirectiveError& error) {
1504  if (this->m_runtime.stack.size < directive.get_size()) {
1507  }
1508  // drop the specified amount of bytes off the stack. simple as.
1509  this->m_runtime.stack.size -= directive.get_size();
1511 }
1512 
1513 Signal FpySequencer::memCmp_directiveHandler(const FpySequencer_MemCmpDirective& directive, DirectiveError& error) {
1514  // Overflow-safe check: we need size * 2 bytes on the stack
1515  // First check that size * 2 doesn't overflow: size > MAX/2 would overflow
1516  // MAX_STACK_SIZE is the upper bound for stack.size, so if size > MAX_STACK_SIZE/2, we definitely don't have enough
1517  if (directive.get_size() > Fpy::MAX_STACK_SIZE / 2) {
1520  }
1521  // Now safe to compute size * 2
1522  if (this->m_runtime.stack.size < directive.get_size() * 2) {
1525  }
1526 
1527  // find the starting offsets of the two byte arrays
1528  U64 lhsOffset = this->m_runtime.stack.size - directive.get_size() * 2;
1529  U64 rhsOffset = this->m_runtime.stack.size - directive.get_size();
1530 
1531  // "officially" remove them from the stack
1532  // you have to do this before pushing to the stack, otherwise the result would get placed
1533  // after the byte arrays
1534  this->m_runtime.stack.size -= directive.get_size() * 2;
1535 
1536  // memcmp the two byte arrays, push FW_SERIALIZE_TRUE_VALUE if they were equal, FW_SERIALIZE_FALSE_VALUE otherwise
1537  if (memcmp(this->m_runtime.stack.bytes + lhsOffset, this->m_runtime.stack.bytes + rhsOffset,
1538  directive.get_size()) == 0) {
1539  this->m_runtime.stack.push<U8>(static_cast<U8>(FW_SERIALIZE_TRUE_VALUE));
1540  } else {
1541  this->m_runtime.stack.push<U8>(static_cast<U8>(FW_SERIALIZE_FALSE_VALUE));
1542  }
1544 }
1545 
1546 Signal FpySequencer::stackCmd_directiveHandler(const FpySequencer_StackCmdDirective& directive, DirectiveError& error) {
1547  // Overflow-safe check: need argsSize + sizeof(FwOpcodeType) bytes
1548  // Check stack.size >= sizeof(FwOpcodeType) first, then stack.size - sizeof(FwOpcodeType) >= argsSize
1549  if (this->m_runtime.stack.size < sizeof(FwOpcodeType) ||
1550  this->m_runtime.stack.size - sizeof(FwOpcodeType) < directive.get_argsSize()) {
1553  }
1554 
1555  // pop the opcode of the cmd off the stack
1556  // note this means that, unlike the actual byte array that the dispatcher gets,
1557  // these cmds have opcode after the argument buffer
1558  FwOpcodeType opcode = this->m_runtime.stack.pop<FwOpcodeType>();
1559  U64 argBufOffset = this->m_runtime.stack.size - directive.get_argsSize();
1560 
1561  // update the opcode of the cmd we will await
1562  this->m_runtime.currentCmdOpcode = opcode;
1563 
1564  // also pop the args off the stack
1565  this->m_runtime.stack.size -= directive.get_argsSize();
1566 
1567  // the cmd response code will be pushed to the stack when it comes back, so make sure
1568  // there is room for it now, before the cmd is dispatched. popping the opcode above
1569  // frees some room, but FwOpcodeType is configurable so it may not be enough
1570  if (sizeof(Fw::CmdResponse::SerialType) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1573  }
1574 
1575  if (this->sendCmd(opcode, this->m_runtime.stack.bytes + argBufOffset, directive.get_argsSize()) ==
1578  } else {
1579  // now tell the SM to wait some more until we get the cmd response back
1580  // if we've already got the response back this should be harmless
1582  }
1583 
1585 }
1586 
1587 Signal FpySequencer::pushTime_directiveHandler(const FpySequencer_PushTimeDirective& directive, DirectiveError& error) {
1588  if (Fw::Time::SERIALIZED_SIZE > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1591  }
1592 
1593  Fw::Time currentTime = this->getTime();
1594 
1595  U8 currentTimeBuf[Fw::Time::SERIALIZED_SIZE] = {};
1596  Fw::ExternalSerializeBuffer timeEsb(currentTimeBuf, Fw::Time::SERIALIZED_SIZE);
1597  Fw::SerializeStatus stat = timeEsb.serializeFrom(currentTime);
1598 
1599  // coding error if this failed, we should have enough space
1600  FW_ASSERT(stat == Fw::SerializeStatus::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(stat));
1601 
1602  // push time to end of stack
1603  this->m_runtime.stack.push(timeEsb.getBuffAddr(), static_cast<Fpy::StackSizeType>(timeEsb.getSize()));
1605 }
1606 
1607 Signal FpySequencer::setSeed_directiveHandler(const FpySequencer_SetSeedDirective& directive, DirectiveError& error) {
1608  if (this->m_runtime.stack.size < sizeof(U32)) {
1611  }
1612 
1613  U32 seed = this->m_runtime.stack.pop<U32>();
1614  this->m_runtime.rng.seed(seed);
1615  this->m_runtime.rngSeeded = true;
1617 }
1618 
1619 Signal FpySequencer::pushRand_directiveHandler(const FpySequencer_PushRandDirective& directive, DirectiveError& error) {
1620  if (sizeof(U32) > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1623  }
1624 
1625  if (!this->m_runtime.rngSeeded) {
1626  Fw::Time currentTime = this->getTime();
1627  std::seed_seq seedSeq{static_cast<U32>(currentTime.getTimeBase()), static_cast<U32>(currentTime.getContext()),
1628  currentTime.getSeconds(), currentTime.getUSeconds()};
1629  this->m_runtime.rng.seed(seedSeq);
1630  this->m_runtime.rngSeeded = true;
1631  }
1632 
1633  U32 randVal = static_cast<U32>(this->m_runtime.rng());
1634  this->m_runtime.stack.push(randVal);
1636 }
1637 
1638 Signal FpySequencer::getField_directiveHandler(const FpySequencer_GetFieldDirective& directive, DirectiveError& error) {
1639  // Need sizeof(StackSizeType) for the offset AND parentSize for the parent data
1640  // Check we have enough for the offset first
1641  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType)) {
1644  }
1645  // After popping the offset, we need at least parentSize bytes remaining
1646  if (this->m_runtime.stack.size - sizeof(Fpy::StackSizeType) < directive.get_parentSize()) {
1649  }
1650 
1651  Fpy::StackSizeType offset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1652 
1653  // Overflow-safe check: offset + memberSize > parentSize
1654  // Rewritten as: check offset <= parentSize first, then memberSize > parentSize - offset
1655  if (offset > directive.get_parentSize() || directive.get_memberSize() > directive.get_parentSize() - offset) {
1656  // i think it's somewhat ambiguous whether this is a stack access out of bounds
1657  // but there isn't really an error code that better reflects this, and i guess
1658  // it's technically true
1661  }
1662 
1663  // the resulting bytes should move to the start of the parent array
1664 
1665  // Calculate the offset of the parent start in the stack
1666  Fpy::StackSizeType parentStartOffset = this->m_runtime.stack.size - directive.get_parentSize();
1667  // Overflow-safe: parentStartOffset + offset cannot overflow since offset <= parentSize
1668  // and parentStartOffset + parentSize == stack.size (which is bounded)
1669  this->m_runtime.stack.move(parentStartOffset, parentStartOffset + offset, directive.get_memberSize());
1670  // adjust stack size by the diff between the member and the parent
1671  this->m_runtime.stack.size -= (directive.get_parentSize() - directive.get_memberSize());
1673 }
1674 
1675 Signal FpySequencer::peek_directiveHandler(const FpySequencer_PeekDirective& directive, DirectiveError& error) {
1676  // must have at least two StackSizeType on stack
1677  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) * 2) {
1680  }
1681 
1682  Fpy::StackSizeType offset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1683  Fpy::StackSizeType byteCount = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1684 
1685  // Check offset doesn't exceed stack size (after both pops)
1686  if (offset > this->m_runtime.stack.size) {
1687  // would access past the bottom of the stack
1688  // note we allow the equals case because the byteCount might be 0
1691  }
1692  if (byteCount > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1693  // we would overflow the stack if we pushed this many bytes to it
1696  }
1697  // Overflow-safe check: byteCount + offset > stack.size
1698  // Rewritten as: check offset <= stack.size (done above), then byteCount > stack.size - offset
1699  if (byteCount > this->m_runtime.stack.size - offset) {
1700  // would access past the bottom of the stack
1703  }
1704  // start copying from the lowest byte of the src array
1705  U8* src = this->m_runtime.stack.top() - offset - byteCount;
1706  this->m_runtime.stack.push(src, byteCount);
1708 }
1709 
1710 Signal FpySequencer::storeRel_directiveHandler(const FpySequencer_StoreRelDirective& directive, DirectiveError& error) {
1711  // Need enough bytes for the value and the offset (SignedStackSizeType = 4 bytes)
1712  // Overflow-safe: check stack.size >= sizeof(SignedStackSizeType) first, then stack.size -
1713  // sizeof(SignedStackSizeType) >= size
1714  if (this->m_runtime.stack.size < sizeof(Fpy::SignedStackSizeType) ||
1715  this->m_runtime.stack.size - sizeof(Fpy::SignedStackSizeType) < directive.get_size()) {
1718  }
1719 
1720  // Pop the signed offset from the stack
1721  Fpy::SignedStackSizeType lvarOffset = this->m_runtime.stack.pop<Fpy::SignedStackSizeType>();
1722 
1723  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + lvarOffset;
1724  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1727  }
1728  return this->storeHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1729 }
1730 
1731 Signal FpySequencer::call_directiveHandler(const FpySequencer_CallDirective& directive, DirectiveError& error) {
1732  // Need at least 4 bytes for the target address
1733  if (this->m_runtime.stack.size < sizeof(U32)) {
1736  }
1737 
1738  // Pop the target directive index from the stack
1739  U32 target = this->m_runtime.stack.pop<U32>();
1740 
1741  // Check if we have space to push return address and saved frame pointer (8 bytes total)
1742  if (this->m_runtime.stack.size + sizeof(Fpy::StackSizeType) + sizeof(U32) > Fpy::MAX_STACK_SIZE) {
1745  }
1746 
1747  // Check target is within bounds (will also be checked at execution time)
1748  if (target > m_sequenceObj.get_header().get_statementCount()) {
1751  }
1752 
1753  // Save the return address (next instruction after CALL)
1754  U32 returnAddr = this->m_runtime.nextStatementIndex;
1755 
1756  // Set the next instruction to the target
1757  this->m_runtime.nextStatementIndex = target;
1758 
1759  // Push the return address to the stack
1760  this->m_runtime.stack.push<U32>(returnAddr);
1761 
1762  // Push the current frame pointer to the stack
1763  this->m_runtime.stack.push<Fpy::StackSizeType>(this->m_runtime.stack.currentFrameStart);
1764 
1765  // Set the new frame pointer to the current top of stack
1766  this->m_runtime.stack.currentFrameStart = this->m_runtime.stack.size;
1767 
1769 }
1770 
1771 Signal FpySequencer::return_directiveHandler(const FpySequencer_ReturnDirective& directive, DirectiveError& error) {
1772  Fpy::StackSizeType returnValSize = directive.get_returnValSize();
1773  Fpy::StackSizeType callArgsSize = directive.get_callArgsSize();
1774 
1775  // Check we have enough bytes for the return value
1776  if (this->m_runtime.stack.size < returnValSize) {
1779  }
1780 
1781  // Remember where the return value lives; it is moved down the stack below rather than copied
1782  // through a local buffer, which at Fpy::MAX_STACK_SIZE would not fit a typical task stack
1783  const Fpy::StackSizeType returnValOffset = this->m_runtime.stack.size - returnValSize;
1784 
1785  // Truncate the stack to stack_frame_start (discard all local variables)
1786  if (this->m_runtime.stack.currentFrameStart > this->m_runtime.stack.size) {
1789  }
1790  this->m_runtime.stack.size = this->m_runtime.stack.currentFrameStart;
1791 
1792  // Check we have enough bytes for saved frame pointer and return address
1793  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) + sizeof(U32)) {
1796  }
1797 
1798  // Pop the saved frame pointer
1799  Fpy::StackSizeType savedFramePtr = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1800 
1801  // Pop the return address
1802  U32 returnAddr = this->m_runtime.stack.pop<U32>();
1803 
1804  // Restore the frame pointer
1805  if (savedFramePtr > this->m_runtime.stack.size) {
1808  }
1809  this->m_runtime.stack.currentFrameStart = savedFramePtr;
1810 
1811  // Validate the return address is within bounds
1812  if (returnAddr > m_sequenceObj.get_header().get_statementCount()) {
1815  }
1816 
1817  // Set the next instruction to the return address
1818  this->m_runtime.nextStatementIndex = returnAddr;
1819 
1820  // Check that we have enough bytes for the call arguments
1821  if (this->m_runtime.stack.size < callArgsSize) {
1824  }
1825  // Discard the function arguments
1826  this->m_runtime.stack.size -= callArgsSize;
1827 
1828  // Push the return value
1829  if (returnValSize > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1832  }
1833  if (returnValSize > 0) {
1834  // Not Stack::move: the source region sits above the truncated stack size, which
1835  // Stack::move rejects. Both regions were bounds-checked above against MAX_STACK_SIZE.
1836  (void)memmove(this->m_runtime.stack.top(), &this->m_runtime.stack.bytes[returnValOffset], returnValSize);
1837  this->m_runtime.stack.size += returnValSize;
1838  }
1839 
1841 }
1842 
1843 Signal FpySequencer::loadAbs_directiveHandler(const FpySequencer_LoadAbsDirective& directive, DirectiveError& error) {
1844  return this->loadHelper(directive.get_globalOffset(), directive.get_size(), error);
1845 }
1846 
1847 Signal FpySequencer::storeAbs_directiveHandler(const FpySequencer_StoreAbsDirective& directive, DirectiveError& error) {
1848  Fpy::StackSizeType size = directive.get_size();
1849 
1850  // Need enough bytes for the value and the offset
1851  // Overflow-safe: check stack.size >= sizeof(StackSizeType) first, then stack.size - sizeof >= size
1852  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) ||
1853  this->m_runtime.stack.size - sizeof(Fpy::StackSizeType) < size) {
1856  }
1857 
1858  // Pop the global offset from the stack
1859  Fpy::StackSizeType globalOffset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1860 
1861  return this->storeHelper(globalOffset, size, error);
1862 }
1863 
1864 Signal FpySequencer::storeAbsConstOffset_directiveHandler(const FpySequencer_StoreAbsConstOffsetDirective& directive,
1865  DirectiveError& error) {
1866  return this->storeHelper(directive.get_globalOffset(), directive.get_size(), error);
1867 }
1868 
1869 Signal FpySequencer::popEvent_directiveHandler(const FpySequencer_PopEventDirective& directive, DirectiveError& error) {
1870  // Pop messageSize from the stack
1871  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType)) {
1874  }
1875  Fpy::StackSizeType messageSize = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1876 
1877  const Fpy::StackSizeType severitySize = static_cast<Fpy::StackSizeType>(sizeof(Fw::LogSeverity::SerialType));
1878 
1879  // Need message_size bytes + sizeof(LogSeverity serial type) for severity
1880  if (this->m_runtime.stack.size < severitySize || this->m_runtime.stack.size - severitySize < messageSize) {
1883  }
1884 
1885  // Pop message bytes first
1886  U8 messageBuf[FW_LOG_STRING_MAX_SIZE] = {};
1887  // don't read in more than (log string size) - 1 bytes
1888  Fpy::StackSizeType clampedSize = std::min(messageSize, static_cast<Fpy::StackSizeType>(FW_LOG_STRING_MAX_SIZE - 1));
1889  // If message is larger than buffer, discard the excess bytes first (from top of stack, which is the end of the
1890  // message)
1891  if (messageSize > clampedSize) {
1892  Fpy::StackSizeType excess = messageSize - clampedSize;
1893  FW_ASSERT(this->m_runtime.stack.size >= excess, static_cast<FwAssertArgType>(this->m_runtime.stack.size),
1894  static_cast<FwAssertArgType>(excess));
1895  this->m_runtime.stack.size -= excess;
1896  }
1897  this->m_runtime.stack.pop(messageBuf, clampedSize);
1898  messageBuf[clampedSize] = '\0';
1899 
1900  // Pop severity
1901  Fw::LogSeverity::SerialType severity = this->m_runtime.stack.pop<Fw::LogSeverity::SerialType>();
1902 
1903  // Construct the message string
1904  Fw::String messageStr(reinterpret_cast<const char*>(messageBuf));
1905 
1906  // Emit the appropriate event based on severity
1907  switch (severity) {
1909  this->log_FATAL_LogFatal(this->m_sequenceFilePath, messageStr);
1910  break;
1912  this->log_WARNING_HI_LogWarningHi(this->m_sequenceFilePath, messageStr);
1913  break;
1915  this->log_WARNING_LO_LogWarningLo(this->m_sequenceFilePath, messageStr);
1916  break;
1918  this->log_COMMAND_LogCommand(this->m_sequenceFilePath, messageStr);
1919  break;
1921  this->log_ACTIVITY_HI_LogActivityHi(this->m_sequenceFilePath, messageStr);
1922  break;
1924  this->log_ACTIVITY_LO_LogActivityLo(this->m_sequenceFilePath, messageStr);
1925  break;
1927  this->log_DIAGNOSTIC_LogDiagnostic(this->m_sequenceFilePath, messageStr);
1928  break;
1929  default:
1932  }
1933 
1935 }
1936 
1937 Signal FpySequencer::popSerializable_directiveHandler(const FpySequencer_PopSerializableDirective& directive,
1938  DirectiveError& error) {
1939  // No size assertion here: an oversized size is untrusted sequence content and is rejected by
1940  // the stack check below, since the stack can never hold more than Fpy::MAX_STACK_SIZE bytes
1941 
1942  // Validate port index is in range (using enum constant value)
1943  constexpr FwIndexType MAX_PORTS = static_cast<FwIndexType>(Svc::Fpy::SerialPortIndex::MAX_SERIAL_PORTS);
1944  const FwIndexType portIndex = directive.get_portIndex();
1945 
1946  // Check for negative port index or out of bounds
1947  if (portIndex < 0 || portIndex >= MAX_PORTS) {
1950  }
1951 
1952  // Check port is connected
1953  if (!this->isConnected_serialOut_OutputPort(portIndex)) {
1956  }
1957 
1958  // Validate data size on stack
1959  if (this->m_runtime.stack.size < directive.get_size()) {
1962  }
1963 
1964  // Create external buffer referencing stack data (no copy)
1965  U8* dataPtr = this->m_runtime.stack.top() - directive.get_size();
1966  Fw::ExternalSerializeBuffer buf(dataPtr, directive.get_size());
1967 
1968  // Set buffer length and verify success
1969  Fw::SerializeStatus stat = buf.setBuffLen(directive.get_size());
1970  FW_ASSERT(stat == Fw::SerializeStatus::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(stat));
1971 
1972  // Call output port; a typed downstream port reports deserialize failures here, which is
1973  // untrusted sequence content (e.g. undersized payload), not an invariant to assert on
1974  Fw::SerializeStatus portStatus = this->serialOut_out(portIndex, buf);
1975  if (portStatus != Fw::SerializeStatus::FW_SERIALIZE_OK) {
1978  }
1979 
1980  // Pop data from stack
1981  this->m_runtime.stack.size -= directive.get_size();
1982 
1984 }
1985 
1986 } // namespace Svc
void directive_allocate_internalInterfaceHandler(const Svc::FpySequencer_AllocateDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_allocate.
void cmdOut_out(FwIndexType portNum, Fw::ComBuffer &data, U32 context) const
Invoke output port cmdOut.
Serialization/Deserialization operation was successful.
sets the index of the next directive to execute
void directive_stackOp_internalInterfaceHandler(const Svc::FpySequencer_StackOpDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_stackOp.
bool isConnected_getTlmChan_OutputPort(FwIndexType portNum) const
void directive_waitAbs_internalInterfaceHandler(const FpySequencer_WaitAbsDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_waitAbs.
FwIdType FwOpcodeType
The type of a command opcode.
U16 get_statementCount() const
Get member statementCount.
void directive_call_internalInterfaceHandler(const Svc::FpySequencer_CallDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_call.
branches based off of the top byte of the stack
Representing success.
bool isConnected_getParam_OutputPort(FwIndexType portNum) const
PlatformSizeType FwSizeType
stores a value to an absolute address in the stack (for global variables), offset from stack ...
void directive_storeRelConstOffset_internalInterfaceHandler(const Svc::FpySequencer_StoreRelConstOffsetDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_storeRelConstOffset.
called when statement successfully executed. only raised in the RUNNING.AWAITING_CMD_RESPONSE state ...
void directive_loadAbs_internalInterfaceHandler(const Svc::FpySequencer_LoadAbsDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_loadAbs.
void directive_storeAbsConstOffset_internalInterfaceHandler(const Svc::FpySequencer_StoreAbsConstOffsetDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_storeAbsConstOffset.
I32 SignedStackSizeType
signed version of StackSizeType, used for relative offsets that can be negative
Serializable::SizeType getSize() const override
Get current buffer size.
void directive_pushVal_internalInterfaceHandler(const Svc::FpySequencer_PushValDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_pushVal.
void directive_noOp_internalInterfaceHandler(const Svc::FpySequencer_NoOpDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_noOp.
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:51
void log_WARNING_LO_DirectiveWhileNotAwaiting(const Svc::Fpy::DirectiveId &directive, I32 state) const
Log event DirectiveWhileNotAwaiting.
stores a value to a local variable at a compile-time-known offset relative to the current stack frame...
Fw::SerializeStatus serialOut_out(FwIndexType portNum, Fw::LinearBufferBase &buffer)
Invoke output port serialOut.
Fw::ParamValid getParam_out(FwIndexType portNum, FwPrmIdType id, Fw::ParamBuffer &val) const
Invoke output port getParam.
void directive_loadRel_internalInterfaceHandler(const Svc::FpySequencer_LoadRelDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_loadRel.
pop an opcode and arg buf off the stack, send to cmd dispatcher and await response ...
void log_WARNING_LO_DirectiveFromOldSequence(const Svc::Fpy::DirectiveId &directive, U64 oldSequenceIdx, U64 currentSequenceIdx) const
Log event DirectiveFromOldSequence.
TimeBase getTimeBase() const
Definition: Time.cpp:136
void sequencer_sendSignal_stmtResponse_success()
Send signal stmtResponse_success to state machine sequencer.
SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG) override
Serialize an 8-bit unsigned integer value.
REQUIRED: Maximum number of serial ports. This sentinel value MUST be named.
void directive_pushPrm_internalInterfaceHandler(const Svc::FpySequencer_PushPrmDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_pushPrm.
pops bytes off the top of the stack and does nothing with them
void log_DIAGNOSTIC_LogDiagnostic(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogDiagnostic.
void log_COMMAND_LogCommand(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogCommand.
void directive_goto_internalInterfaceHandler(const Svc::FpySequencer_GotoDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_goto.
U8 FwTimeContextStoreType
The type used to serialize a time context value.
Software diagnostic events.
void log_ACTIVITY_LO_LogActivityLo(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogActivityLo.
SerializeStatus
forward declaration for string
Svc_FpySequencer_SequencerStateMachine::State sequencer_getState() const
Get the state of state machine instance sequencer.
void log_WARNING_HI_SequenceExitedWithError(const Fw::StringBase &filePath, I32 errorCode) const
Log event SequenceExitedWithError.
float F32
32-bit floating point
Definition: BasicTypes.h:84
Fpy::DirectiveErrorCode DirectiveError
loads a value from a local variable at a compile-time-known offset relative to the current stack fram...
void log_ACTIVITY_HI_LogActivityHi(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogActivityHi.
pop two byte arrays off the top of the stack, call memcmp, push 1 if they were equal, 0 otherwise
U8 * getBuffAddr()
Get buffer address for data filling (non-const version)
Svc::Fpy::Header & get_header()
Get member header.
void directive_setSeed_internalInterfaceHandler(const Svc::FpySequencer_SetSeedDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_setSeed.
Less important informational events.
pops a severity and message from the stack and emits an F Prime event
An activity related to commanding.
A less serious but recoverable event.
Omit length from serialization.
void directive_storeRel_internalInterfaceHandler(const Svc::FpySequencer_StoreRelDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_storeRel.
stores a value to an absolute address in the stack (for global variables), const offset ...
External serialize buffer with no copy semantics.
U32 getSeconds() const
Definition: Time.cpp:128
Svc::Fpy::DirectiveId::T get__op() const
Get member _op.
U8 SerialType
The serial representation type.
void directive_storeAbs_internalInterfaceHandler(const Svc::FpySequencer_StoreAbsDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_storeAbs.
void directive_stackCmd_internalInterfaceHandler(const Svc::FpySequencer_StackCmdDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_stackCmd.
void directive_pushRand_internalInterfaceHandler(const Svc::FpySequencer_PushRandDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_pushRand.
void log_WARNING_HI_LogWarningHi(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogWarningHi.
peeks at N bytes from the stack, starting from an offset relative to the top of the stack ...
void sequencer_sendSignal_stmtResponse_failure()
Send signal stmtResponse_failure to state machine sequencer.
A serious but recoverable event.
void directive_constCmd_internalInterfaceHandler(const Svc::FpySequencer_ConstCmdDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_constCmd.
void directive_return_internalInterfaceHandler(const Svc::FpySequencer_ReturnDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_return.
void sequencer_sendSignal_stmtResponse_beginSleep()
Send signal stmtResponse_beginSleep to state machine sequencer.
Representing failure.
void directive_getField_internalInterfaceHandler(const Svc::FpySequencer_GetFieldDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_getField.
sleeps for a relative duration from the current time
void directive_pushTlmValAndTime_internalInterfaceHandler(const Svc::FpySequencer_PushTlmValAndTimeDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_pushTlmValAndTime.
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
bool isConnected_serialOut_OutputPort(FwIndexType portNum) const
void directive_pushTlmVal_internalInterfaceHandler(const Svc::FpySequencer_PushTlmValDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_pushTlmVal.
SerializeStatus setBuffLen(Serializable::SizeType length) override
Set buffer length manually.
Important informational events.
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
U8 SerialType
The serial representation type.
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:174
void sequencer_sendSignal_stmtResponse_keepWaiting()
Send signal stmtResponse_keepWaiting to state machine sequencer.
void directive_if_internalInterfaceHandler(const Svc::FpySequencer_IfDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_if.
FwTimeContextStoreType getContext() const
Definition: Time.cpp:140
U32 getUSeconds() const
Definition: Time.cpp:132
void directive_exit_internalInterfaceHandler(const Svc::FpySequencer_ExitDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_exit.
void directive_popSerializable_internalInterfaceHandler(const Svc::FpySequencer_PopSerializableDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_popSerializable.
called when the statement unsuccessfully executed. only raised in the RUNNING.AWAITING_CMD_RESPONSE s...
loads a value from an absolute address in the stack (for global variables)
PlatformIndexType FwIndexType
pops a U32 from the stack and uses it to seed the RNG used by PushRandDirective
void directive_waitRel_internalInterfaceHandler(const FpySequencer_WaitRelDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_waitRel.
double F64
64-bit floating point (double). Required for compiler-supplied double promotion.
Definition: BasicTypes.h:86
pops serialized data from stack and sends to serial output port
A fatal non-recoverable event.
void directive_peek_internalInterfaceHandler(const Svc::FpySequencer_PeekDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_peek.
void log_WARNING_LO_LogWarningLo(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogWarningLo.
stores a value to a local variable at a runtime-determined offset relative to the current stack frame...
RateGroupDivider component implementation.
Enum representing parameter validity.
void log_FATAL_LogFatal(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogFatal.
U16 FwTimeBaseStoreType
The type used to serialize a time base value.
pushes the current Fw.Time struct to the stack
void directive_pushTime_internalInterfaceHandler(const Svc::FpySequencer_PushTimeDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_pushTime.
called when the statement is telling the sequencer to await a later stmt response ...
FpySequencer_SequencerStateMachineStateMachineBase::Signal Signal
void directive_popEvent_internalInterfaceHandler(const Svc::FpySequencer_PopEventDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_popEvent.
Fw::TlmValid getTlmChan_out(FwIndexType portNum, FwChanIdType id, Fw::Time &timeTag, Fw::TlmBuffer &val) const
Invoke output port getTlmChan.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
void directive_memCmp_internalInterfaceHandler(const Svc::FpySequencer_MemCmpDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_memCmp.
Success/Failure.
void directive_discard_internalInterfaceHandler(const Svc::FpySequencer_DiscardDirective &directive, U64 sequenceIndex) override
Internal interface handler for directive_discard.
U32 StackSizeType
the type which everything referencing a size or offset on the stack is represented in ...