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"
7 
8 namespace Svc {
9 
10 void FpySequencer::sendSignal(Signal signal) {
11  switch (signal) {
14  break;
15  }
18  break;
19  }
22  break;
23  }
26  break;
27  }
28  default: {
29  FW_ASSERT(0, static_cast<FwAssertArgType>(signal));
30  }
31  }
32 }
33 
34 // utility method for updating telemetry based on a directive error code
35 void FpySequencer::handleDirectiveErrorCode(Fpy::DirectiveId id, DirectiveError err) {
36  this->m_tlm.lastDirectiveError = err;
37  if (err != DirectiveError::NO_ERROR) {
38  this->m_tlm.directiveErrorIndex = this->currentStatementIdx();
39  this->m_tlm.directiveErrorId = id;
40  }
41 }
42 
43 Fw::Success FpySequencer::sendCmd(FwOpcodeType opcode, const U8* argBuf, FwSizeType argBufSize) {
44  Fw::ComBuffer cmdBuf;
45  Fw::SerializeStatus stat =
46  cmdBuf.serializeFrom(static_cast<FwPacketDescriptorType>(Fw::ComPacketType::FW_PACKET_COMMAND));
47  // TODO should I assert here? this really shouldn't fail, I should just add a static assert
48  // on com buf size and then assert here
50  return Fw::Success::FAILURE;
51  }
52  stat = cmdBuf.serializeFrom(opcode);
54  return Fw::Success::FAILURE;
55  }
56  stat = cmdBuf.serializeFrom(argBuf, argBufSize, Fw::Serialization::OMIT_LENGTH);
58  return Fw::Success::FAILURE;
59  }
60 
61  // calculate the unique command identifier:
62  // cmd UID is formatted like XXYY, where XX are the first two bytes of the m_sequencesStarted counter
63  // and YY are the first two bytes of the m_statementsDispatched counter.
64  // this way, we know when we get a cmd back A) whether or not it's from this sequence (modulo 2^16) and B)
65  // whether or not it's this specific instance of the cmd in the sequence, and not another one with the same opcode
66  // somewhere else in the file.
67  // if we put this uid in the context we send to the cmdDisp, we will get it back when the cmd returns
68  U32 cmdUid =
69  static_cast<U32>(((this->m_sequencesStarted & 0xFFFF) << 16) | (this->m_statementsDispatched & 0xFFFF));
70 
71  this->cmdOut_out(0, cmdBuf, cmdUid);
72 
73  return Fw::Success::SUCCESS;
74 }
75 
79  this->sendSignal(this->waitRel_directiveHandler(directive, error));
80  handleDirectiveErrorCode(Fpy::DirectiveId::WAIT_REL, error);
81 }
82 
86  this->sendSignal(this->waitAbs_directiveHandler(directive, error));
87  handleDirectiveErrorCode(Fpy::DirectiveId::WAIT_ABS, error);
88 }
89 
93  this->sendSignal(this->goto_directiveHandler(directive, error));
94  handleDirectiveErrorCode(Fpy::DirectiveId::GOTO, error);
95 }
96 
100  this->sendSignal(this->if_directiveHandler(directive, error));
101  handleDirectiveErrorCode(Fpy::DirectiveId::IF, error);
102 }
103 
107  this->sendSignal(this->noOp_directiveHandler(directive, error));
108  handleDirectiveErrorCode(Fpy::DirectiveId::NO_OP, error);
109 }
110 
113  const Svc::FpySequencer_PushTlmValDirective& directive) {
115  this->sendSignal(this->pushTlmVal_directiveHandler(directive, error));
116  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TLM_VAL, error);
117 }
118 
123  this->sendSignal(this->pushTlmValAndTime_directiveHandler(directive, error));
124  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TLM_VAL_AND_TIME, error);
125 }
126 
130  this->sendSignal(this->pushPrm_directiveHandler(directive, error));
131  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_PRM, error);
132 }
133 
137  this->sendSignal(this->constCmd_directiveHandler(directive, error));
138  handleDirectiveErrorCode(Fpy::DirectiveId::CONST_CMD, error);
139 }
140 
144  this->sendSignal(this->stackOp_directiveHandler(directive, error));
145  handleDirectiveErrorCode(directive.get__op(), error);
146 }
147 
151  this->sendSignal(this->exit_directiveHandler(directive, error));
152  handleDirectiveErrorCode(Fpy::DirectiveId::EXIT, error);
153 }
154 
158  this->sendSignal(this->allocate_directiveHandler(directive, error));
159  handleDirectiveErrorCode(Fpy::DirectiveId::ALLOCATE, error);
160 }
161 
166  this->sendSignal(this->storeRelConstOffset_directiveHandler(directive, error));
167  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_REL_CONST_OFFSET, error);
168 }
169 
173  this->sendSignal(this->pushVal_directiveHandler(directive, error));
174  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_VAL, error);
175 }
176 
180  this->sendSignal(this->loadRel_directiveHandler(directive, error));
181  handleDirectiveErrorCode(Fpy::DirectiveId::LOAD_REL, error);
182 }
183 
187  this->sendSignal(this->discard_directiveHandler(directive, error));
188  handleDirectiveErrorCode(Fpy::DirectiveId::DISCARD, error);
189 }
190 
194  this->sendSignal(this->memCmp_directiveHandler(directive, error));
195  handleDirectiveErrorCode(Fpy::DirectiveId::MEMCMP, error);
196 }
197 
201  this->sendSignal(this->stackCmd_directiveHandler(directive, error));
202  handleDirectiveErrorCode(Fpy::DirectiveId::STACK_CMD, error);
203 }
204 
208  this->sendSignal(this->pushTime_directiveHandler(directive, error));
209  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TIME, error);
210 }
211 
215  this->sendSignal(this->getField_directiveHandler(directive, error));
216  handleDirectiveErrorCode(Fpy::DirectiveId::GET_FIELD, error);
217 }
218 
222  this->sendSignal(this->peek_directiveHandler(directive, error));
223  handleDirectiveErrorCode(Fpy::DirectiveId::PEEK, error);
224 }
225 
229  this->sendSignal(this->storeRel_directiveHandler(directive, error));
230  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_REL, error);
231 }
232 
236  this->sendSignal(this->call_directiveHandler(directive, error));
237  handleDirectiveErrorCode(Fpy::DirectiveId::CALL, error);
238 }
239 
243  this->sendSignal(this->return_directiveHandler(directive, error));
244  handleDirectiveErrorCode(Fpy::DirectiveId::RETURN, error);
245 }
246 
250  this->sendSignal(this->loadAbs_directiveHandler(directive, error));
251  handleDirectiveErrorCode(Fpy::DirectiveId::LOAD_ABS, error);
252 }
253 
257  this->sendSignal(this->storeAbs_directiveHandler(directive, error));
258  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_ABS, error);
259 }
260 
265  this->sendSignal(this->storeAbsConstOffset_directiveHandler(directive, error));
266  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_ABS_CONST_OFFSET, error);
267 }
268 
270 Signal FpySequencer::waitRel_directiveHandler(const FpySequencer_WaitRelDirective& directive, DirectiveError& error) {
271  if (this->m_runtime.stack.size < 8) {
274  }
275 
276  Fw::Time wakeupTime = this->getTime();
277 
278  U32 uSeconds = this->m_runtime.stack.pop<U32>();
279  U32 seconds = this->m_runtime.stack.pop<U32>();
280 
281  wakeupTime.add(seconds, uSeconds);
282  this->m_runtime.wakeupTime = wakeupTime;
284 }
285 
287 Signal FpySequencer::waitAbs_directiveHandler(const FpySequencer_WaitAbsDirective& directive, DirectiveError& error) {
288  if (this->m_runtime.stack.size < 2 * sizeof(U32) + sizeof(FwTimeContextStoreType) + sizeof(FwTimeBaseStoreType)) {
291  }
292 
293  U32 uSeconds = this->m_runtime.stack.pop<U32>();
294  U32 seconds = this->m_runtime.stack.pop<U32>();
295  FwTimeContextStoreType ctx = this->m_runtime.stack.pop<FwTimeContextStoreType>();
296  FwTimeBaseStoreType base = this->m_runtime.stack.pop<FwTimeBaseStoreType>();
297 
298  this->m_runtime.wakeupTime = Fw::Time(static_cast<TimeBase::T>(base), ctx, seconds, uSeconds);
300 }
301 
303 Signal FpySequencer::goto_directiveHandler(const FpySequencer_GotoDirective& directive, DirectiveError& error) {
304  // check within sequence bounds, or at EOF (we allow == case cuz this just ends the sequence)
305  if (directive.get_statementIndex() > m_sequenceObj.get_header().get_statementCount()) {
308  }
309  m_runtime.nextStatementIndex = directive.get_statementIndex();
311 }
312 
314 Signal FpySequencer::if_directiveHandler(const FpySequencer_IfDirective& directive, DirectiveError& error) {
315  if (this->m_runtime.stack.size < 1) {
318  }
319  // check within sequence bounds, or at EOF (we allow == case cuz this just ends the sequence)
320  if (directive.get_falseGotoStmtIndex() > m_sequenceObj.get_header().get_statementCount()) {
323  }
324 
325  if (this->m_runtime.stack.pop<U8>() != 0) {
326  // proceed to next instruction
328  }
329 
330  // conditional false case
331  this->m_runtime.nextStatementIndex = directive.get_falseGotoStmtIndex();
333 }
334 
335 Signal FpySequencer::noOp_directiveHandler(const FpySequencer_NoOpDirective& directive, DirectiveError& error) {
337 }
338 
339 Signal FpySequencer::pushTlmVal_directiveHandler(const FpySequencer_PushTlmValDirective& directive,
340  DirectiveError& error) {
341  if (!this->isConnected_getTlmChan_OutputPort(0)) {
344  }
345  Fw::Time tlmTime;
346  Fw::TlmBuffer tlmValue;
347  Fw::TlmValid valid = this->getTlmChan_out(0, directive.get_chanId(), tlmTime, tlmValue);
348 
349  if (valid != Fw::TlmValid::VALID) {
350  // could not find this tlm chan
353  }
354 
355  if (Fpy::MAX_STACK_SIZE - tlmValue.getSize() < this->m_runtime.stack.size) {
358  }
359  this->m_runtime.stack.push(tlmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(tlmValue.getSize()));
361 }
362 
363 Signal FpySequencer::pushTlmValAndTime_directiveHandler(const FpySequencer_PushTlmValAndTimeDirective& directive,
364  DirectiveError& error) {
365  if (!this->isConnected_getTlmChan_OutputPort(0)) {
368  }
369 
370  Fw::Time tlmTime;
371  Fw::TlmBuffer tlmValue;
372  Fw::TlmValid valid = this->getTlmChan_out(0, directive.get_chanId(), tlmTime, tlmValue);
373 
374  if (valid != Fw::TlmValid::VALID) {
375  // could not find this tlm chan
378  }
379 
380  U8 tlmTimeBuf[Fw::Time::SERIALIZED_SIZE] = {};
382  Fw::SerializeStatus stat = timeEsb.serializeFrom(tlmTime);
383 
384  // coding error if this failed, we should have enough space
385  FW_ASSERT(stat == Fw::SerializeStatus::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(stat));
386 
387  // check that our stack won't overflow if we put both val and time on it
388  if (Fpy::MAX_STACK_SIZE - tlmValue.getSize() - timeEsb.getSize() < this->m_runtime.stack.size) {
391  }
392 
393  // push tlm to end of stack
394  this->m_runtime.stack.push(tlmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(tlmValue.getSize()));
395  // now push time to end of stack
396  this->m_runtime.stack.push(timeEsb.getBuffAddr(), static_cast<Fpy::StackSizeType>(timeEsb.getSize()));
398 }
399 
400 Signal FpySequencer::pushPrm_directiveHandler(const FpySequencer_PushPrmDirective& directive, DirectiveError& error) {
401  if (!this->isConnected_prmGet_OutputPort(0)) {
404  }
405 
406  Fw::ParamBuffer prmValue;
407  Fw::ParamValid valid = this->getParam_out(0, directive.get_prmId(), prmValue);
408 
409  if (valid != Fw::ParamValid::VALID) {
410  // could not find this prm in the DB
413  }
414 
415  if (Fpy::MAX_STACK_SIZE - prmValue.getSize() < this->m_runtime.stack.size) {
418  }
419 
420  this->m_runtime.stack.push(prmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(prmValue.getSize()));
422 }
423 
424 Signal FpySequencer::constCmd_directiveHandler(const FpySequencer_ConstCmdDirective& directive, DirectiveError& error) {
425  if (this->sendCmd(directive.get_opCode(), directive.get_argBuf(), directive.get__argBufSize()) ==
428  } else {
429  // now tell the SM to wait some more until we get the cmd response back
430  // if we've already got the response back this should be harmless
432  }
433 }
434 
435 DirectiveError FpySequencer::op_or() {
436  if (this->m_runtime.stack.size < sizeof(U8) * 2) {
438  }
439  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<U8>() | this->m_runtime.stack.pop<U8>()));
441 }
442 DirectiveError FpySequencer::op_and() {
443  if (this->m_runtime.stack.size < sizeof(U8) * 2) {
445  }
446  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<U8>() & this->m_runtime.stack.pop<U8>()));
448 }
449 DirectiveError FpySequencer::op_ieq() {
450  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
452  }
453  this->m_runtime.stack.push(static_cast<U8>((this->m_runtime.stack.pop<I64>() == this->m_runtime.stack.pop<I64>())
454  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
455  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
457 }
458 DirectiveError FpySequencer::op_ine() {
459  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
461  }
462  this->m_runtime.stack.push(static_cast<U8>((this->m_runtime.stack.pop<I64>() != this->m_runtime.stack.pop<I64>())
463  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
464  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
466 }
467 DirectiveError FpySequencer::op_ult() {
468  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
470  }
471  U64 rhs = this->m_runtime.stack.pop<U64>();
472  U64 lhs = this->m_runtime.stack.pop<U64>();
473  this->m_runtime.stack.push(static_cast<U8>((lhs < rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
474  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
476 }
477 DirectiveError FpySequencer::op_ule() {
478  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
480  }
481  U64 rhs = this->m_runtime.stack.pop<U64>();
482  U64 lhs = this->m_runtime.stack.pop<U64>();
483  this->m_runtime.stack.push(static_cast<U8>((lhs <= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
484  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
486 }
487 DirectiveError FpySequencer::op_ugt() {
488  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
490  }
491  U64 rhs = this->m_runtime.stack.pop<U64>();
492  U64 lhs = this->m_runtime.stack.pop<U64>();
493  this->m_runtime.stack.push(static_cast<U8>((lhs > rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
494  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
496 }
497 DirectiveError FpySequencer::op_uge() {
498  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
500  }
501  U64 rhs = this->m_runtime.stack.pop<U64>();
502  U64 lhs = this->m_runtime.stack.pop<U64>();
503  this->m_runtime.stack.push(static_cast<U8>((lhs >= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
504  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
506 }
507 DirectiveError FpySequencer::op_slt() {
508  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
510  }
511  I64 rhs = this->m_runtime.stack.pop<I64>();
512  I64 lhs = this->m_runtime.stack.pop<I64>();
513  this->m_runtime.stack.push(static_cast<U8>((lhs < rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
514  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
516 }
517 DirectiveError FpySequencer::op_sle() {
518  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
520  }
521  I64 rhs = this->m_runtime.stack.pop<I64>();
522  I64 lhs = this->m_runtime.stack.pop<I64>();
523  this->m_runtime.stack.push(static_cast<U8>((lhs <= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
524  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
526 }
527 DirectiveError FpySequencer::op_sgt() {
528  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
530  }
531  I64 rhs = this->m_runtime.stack.pop<I64>();
532  I64 lhs = this->m_runtime.stack.pop<I64>();
533  this->m_runtime.stack.push(static_cast<U8>((lhs > rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
534  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
536 }
537 DirectiveError FpySequencer::op_sge() {
538  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
540  }
541  I64 rhs = this->m_runtime.stack.pop<I64>();
542  I64 lhs = this->m_runtime.stack.pop<I64>();
543  this->m_runtime.stack.push(static_cast<U8>((lhs >= rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
544  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
546 }
547 DirectiveError FpySequencer::op_feq() {
548  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
550  }
551  F64 rhs = this->m_runtime.stack.pop<F64>();
552  F64 lhs = this->m_runtime.stack.pop<F64>();
553  // eq is true if they are equal and neither is nan
554  this->m_runtime.stack.push(static_cast<U8>((lhs == rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
555  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
557 }
558 DirectiveError FpySequencer::op_fne() {
559  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
561  }
562  F64 rhs = this->m_runtime.stack.pop<F64>();
563  F64 lhs = this->m_runtime.stack.pop<F64>();
564  // ne is true if they are not equal or either is nan
565  this->m_runtime.stack.push(static_cast<U8>((lhs != rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
566  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
568 }
569 DirectiveError FpySequencer::op_flt() {
570  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
572  }
573  F64 rhs = this->m_runtime.stack.pop<F64>();
574  F64 lhs = this->m_runtime.stack.pop<F64>();
575  this->m_runtime.stack.push(static_cast<U8>(std::isless(lhs, rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
576  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
578 }
579 DirectiveError FpySequencer::op_fle() {
580  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
582  }
583  F64 rhs = this->m_runtime.stack.pop<F64>();
584  F64 lhs = this->m_runtime.stack.pop<F64>();
585  this->m_runtime.stack.push(static_cast<U8>(std::islessequal(lhs, rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
586  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
588 }
589 DirectiveError FpySequencer::op_fgt() {
590  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
592  }
593  F64 rhs = this->m_runtime.stack.pop<F64>();
594  F64 lhs = this->m_runtime.stack.pop<F64>();
595  this->m_runtime.stack.push(static_cast<U8>(std::isgreater(lhs, rhs) ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
596  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
598 }
599 DirectiveError FpySequencer::op_fge() {
600  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
602  }
603  F64 rhs = this->m_runtime.stack.pop<F64>();
604  F64 lhs = this->m_runtime.stack.pop<F64>();
605  this->m_runtime.stack.push(static_cast<U8>(std::isgreaterequal(lhs, rhs)
606  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
607  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
609 }
610 DirectiveError FpySequencer::op_not() {
611  if (this->m_runtime.stack.size < sizeof(U8)) {
613  }
614  this->m_runtime.stack.push(static_cast<U8>((this->m_runtime.stack.pop<U8>() == 0)
615  ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE)
616  : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE)));
618 }
619 DirectiveError FpySequencer::op_fpext() {
620  // convert F32 to F64
621  if (this->m_runtime.stack.size < sizeof(F32)) {
623  }
624  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<F32>()));
626 }
627 DirectiveError FpySequencer::op_fptrunc() {
628  // convert F64 to F32
629  if (this->m_runtime.stack.size < sizeof(F64)) {
631  }
632  this->m_runtime.stack.push(static_cast<F32>(this->m_runtime.stack.pop<F64>()));
634 }
635 DirectiveError FpySequencer::op_fptosi() {
636  if (this->m_runtime.stack.size < sizeof(F64)) {
638  }
639  this->m_runtime.stack.push(static_cast<I64>(this->m_runtime.stack.pop<F64>()));
641 }
642 DirectiveError FpySequencer::op_sitofp() {
643  if (this->m_runtime.stack.size < sizeof(I64)) {
645  }
646  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<I64>()));
648 }
649 DirectiveError FpySequencer::op_fptoui() {
650  if (this->m_runtime.stack.size < sizeof(F64)) {
652  }
653  this->m_runtime.stack.push(static_cast<U64>(this->m_runtime.stack.pop<F64>()));
655 }
656 DirectiveError FpySequencer::op_uitofp() {
657  if (this->m_runtime.stack.size < sizeof(U64)) {
659  }
660  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<U64>()));
662 }
663 DirectiveError FpySequencer::op_add() {
664  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
666  }
667  I64 rhs = this->m_runtime.stack.pop<I64>();
668  I64 lhs = this->m_runtime.stack.pop<I64>();
669  // Check for overflow and underflow and return the appropriate error code
670  // Overflow can only occur with both operands positive and occurs when one operand is greater than the maximum value
671  // less the other operand. If either operand is negative or zero, overflow cannot occur.
672  if ((rhs > 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() - rhs) < lhs)) {
674  }
675  // Underflow can only occur with both operands negative and occurs when one operand is less than the minimum value
676  // minus the other operand. If either operand is positive or zero, underflow cannot occur.
677  else if ((rhs < 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() - rhs) > lhs)) {
679  }
680  this->m_runtime.stack.push(static_cast<I64>(lhs + rhs));
682 }
683 DirectiveError FpySequencer::op_sub() {
684  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
686  }
687  I64 rhs = this->m_runtime.stack.pop<I64>();
688  I64 lhs = this->m_runtime.stack.pop<I64>();
689  // Check for overflow and underflow and return the appropriate error code
690  // Overflow can only occur when the left operand is positive and the right operand is negative. It occurs when the
691  // left (positive) operand is greater than the maximum value plus the other (negative) operand. If the right
692  // operand is positive or zero, overflow cannot occur.
693  if ((rhs < 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() + rhs) < lhs)) {
695  }
696  // Underflow can only occur when the left operand is negative and the right operand is positive. It occurs when the
697  // left (negative) operand is less than the minimum value plus the other (positive) operand. If the right operand
698  // is negative or zero, underflow cannot occur.
699  else if ((rhs > 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() + rhs) > lhs)) {
701  }
702  this->m_runtime.stack.push(static_cast<I64>(lhs - rhs));
704 }
705 DirectiveError FpySequencer::op_mul() {
706  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
708  }
709  I64 rhs = this->m_runtime.stack.pop<I64>();
710  I64 lhs = this->m_runtime.stack.pop<I64>();
711  // Check for overflow and underflow and return the appropriate error code
712  // Overflow can only occur with operands of matching signs and occurs when one operand is greater (or less) than the
713  // maximum value divided by the other operand. Either operand being zero precludes overflow.
714  // Check the both positive case.
715  if ((rhs > 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() / rhs) < lhs)) {
717  }
718  // Check the both negative case
719  else if ((rhs < 0) && (lhs < 0) && ((std::numeric_limits<I64>::max() / (-1 * rhs)) < (-1 * lhs))) {
721  }
722  // Underflow can occur with operands of differing signs and occurs when one operand is less than the minimum value
723  // divided by the other operand. Either operand being zero precludes underflow.
724  // Check the case where lhs is positive.
725  else if ((rhs < 0) && (lhs > 0) && ((std::numeric_limits<I64>::min() / lhs) > rhs)) {
727  }
728  // Check the case where rhs is positive.
729  else if ((rhs > 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() / rhs) > lhs)) {
731  }
732  this->m_runtime.stack.push(static_cast<I64>(lhs * rhs));
734 }
735 DirectiveError FpySequencer::op_udiv() {
736  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
738  }
739  U64 rhs = this->m_runtime.stack.pop<U64>();
740  U64 lhs = this->m_runtime.stack.pop<U64>();
741  // Prevent division by zero
742  if (rhs == 0) {
744  }
745  this->m_runtime.stack.push(static_cast<U64>(lhs / rhs));
747 }
748 DirectiveError FpySequencer::op_sdiv() {
749  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
751  }
752 
753  I64 rhs = this->m_runtime.stack.pop<I64>();
754  I64 lhs = this->m_runtime.stack.pop<I64>();
755  // Prevent division by zero
756  if (rhs == 0) {
758  }
759  this->m_runtime.stack.push(static_cast<I64>(lhs / rhs));
761 }
762 DirectiveError FpySequencer::op_umod() {
763  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
765  }
766  U64 rhs = this->m_runtime.stack.pop<U64>();
767  if (rhs == 0) {
769  }
770  U64 lhs = this->m_runtime.stack.pop<U64>();
771  this->m_runtime.stack.push(static_cast<U64>(lhs % rhs));
773 }
774 DirectiveError FpySequencer::op_smod() {
775  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
777  }
778  I64 rhs = this->m_runtime.stack.pop<I64>();
779  if (rhs == 0) {
781  }
782  I64 lhs = this->m_runtime.stack.pop<I64>();
783  I64 res = static_cast<I64>(lhs % rhs);
784  // in order to match Python's behavior,
785  // if the signs of the remainder and divisor differ, adjust the result.
786  // this happens when the result should be positive but is negative, or vice-versa.
787  // credit Gemini 2.5 pro
788  if ((res > 0 && rhs < 0) || (res < 0 && rhs > 0)) {
789  res += rhs;
790  }
791  this->m_runtime.stack.push(res);
793 }
794 DirectiveError FpySequencer::op_fadd() {
795  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
797  }
798  F64 rhs = this->m_runtime.stack.pop<F64>();
799  F64 lhs = this->m_runtime.stack.pop<F64>();
800  this->m_runtime.stack.push(static_cast<F64>(lhs + rhs));
802 }
803 DirectiveError FpySequencer::op_fsub() {
804  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
806  }
807  F64 rhs = this->m_runtime.stack.pop<F64>();
808  F64 lhs = this->m_runtime.stack.pop<F64>();
809  this->m_runtime.stack.push(static_cast<F64>(lhs - rhs));
811 }
812 DirectiveError FpySequencer::op_fmul() {
813  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
815  }
816  F64 rhs = this->m_runtime.stack.pop<F64>();
817  F64 lhs = this->m_runtime.stack.pop<F64>();
818  this->m_runtime.stack.push(static_cast<F64>(lhs * rhs));
820 }
821 DirectiveError FpySequencer::op_fdiv() {
822  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
824  }
825  F64 rhs = this->m_runtime.stack.pop<F64>();
826  F64 lhs = this->m_runtime.stack.pop<F64>();
827  this->m_runtime.stack.push(static_cast<F64>(lhs / rhs));
829 }
830 DirectiveError FpySequencer::op_fpow() {
831  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
833  }
834  F64 rhs = this->m_runtime.stack.pop<F64>();
835  F64 lhs = this->m_runtime.stack.pop<F64>();
836  this->m_runtime.stack.push(static_cast<F64>(pow(lhs, rhs)));
838 }
839 DirectiveError FpySequencer::op_flog() {
840  if (this->m_runtime.stack.size < sizeof(F64)) {
842  }
843  F64 val = this->m_runtime.stack.pop<F64>();
844  if (val <= 0.0) {
846  }
847  this->m_runtime.stack.push(static_cast<F64>(log(val)));
849 }
850 DirectiveError FpySequencer::op_fmod() {
851  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
853  }
854  F64 rhs = this->m_runtime.stack.pop<F64>();
855  if (rhs == 0.0) {
857  }
858  F64 lhs = this->m_runtime.stack.pop<F64>();
859  this->m_runtime.stack.push(static_cast<F64>(lhs - rhs * std::floor(lhs / rhs)));
861 }
862 DirectiveError FpySequencer::op_siext_8_64() {
863  if (this->m_runtime.stack.size < sizeof(I8)) {
865  }
866  I8 src = this->m_runtime.stack.pop<I8>();
867  this->m_runtime.stack.push(static_cast<I64>(src));
869 }
870 DirectiveError FpySequencer::op_siext_16_64() {
871  if (this->m_runtime.stack.size < sizeof(I16)) {
873  }
874  I16 src = this->m_runtime.stack.pop<I16>();
875  this->m_runtime.stack.push(static_cast<I64>(src));
877 }
878 DirectiveError FpySequencer::op_siext_32_64() {
879  if (this->m_runtime.stack.size < sizeof(I32)) {
881  }
882  I32 src = this->m_runtime.stack.pop<I32>();
883  this->m_runtime.stack.push(static_cast<I64>(src));
885 }
886 DirectiveError FpySequencer::op_ziext_8_64() {
887  if (this->m_runtime.stack.size < sizeof(U8)) {
889  }
890  U8 src = this->m_runtime.stack.pop<U8>();
891  this->m_runtime.stack.push(static_cast<U64>(src));
893 }
894 DirectiveError FpySequencer::op_ziext_16_64() {
895  if (this->m_runtime.stack.size < sizeof(U16)) {
897  }
898  U16 src = this->m_runtime.stack.pop<U16>();
899  this->m_runtime.stack.push(static_cast<U64>(src));
901 }
902 DirectiveError FpySequencer::op_ziext_32_64() {
903  if (this->m_runtime.stack.size < sizeof(U32)) {
905  }
906  U32 src = this->m_runtime.stack.pop<U32>();
907  this->m_runtime.stack.push(static_cast<U64>(src));
909 }
910 DirectiveError FpySequencer::op_itrunc_64_8() {
911  if (this->m_runtime.stack.size < sizeof(U64)) {
913  }
914  U64 src = this->m_runtime.stack.pop<U64>();
915  this->m_runtime.stack.push(static_cast<U8>(src));
917 }
918 DirectiveError FpySequencer::op_itrunc_64_16() {
919  if (this->m_runtime.stack.size < sizeof(U64)) {
921  }
922  U64 src = this->m_runtime.stack.pop<U64>();
923  this->m_runtime.stack.push(static_cast<U16>(src));
925 }
926 DirectiveError FpySequencer::op_itrunc_64_32() {
927  if (this->m_runtime.stack.size < sizeof(U64)) {
929  }
930  U64 src = this->m_runtime.stack.pop<U64>();
931  this->m_runtime.stack.push(static_cast<U32>(src));
933 }
934 Signal FpySequencer::stackOp_directiveHandler(const FpySequencer_StackOpDirective& directive, DirectiveError& error) {
935  // coding error, should not have gotten to this stack op handler
936  FW_ASSERT(directive.get__op() >= Fpy::DirectiveId::OR && directive.get__op() <= Fpy::DirectiveId::ITRUNC_64_32,
937  static_cast<FwAssertArgType>(directive.get__op()));
938 
939  switch (directive.get__op()) {
941  error = this->op_or();
942  break;
944  error = this->op_and();
945  break;
947  error = this->op_ieq();
948  break;
950  error = this->op_ine();
951  break;
953  error = this->op_ult();
954  break;
956  error = this->op_ule();
957  break;
959  error = this->op_ugt();
960  break;
962  error = this->op_uge();
963  break;
965  error = this->op_slt();
966  break;
968  error = this->op_sle();
969  break;
971  error = this->op_sgt();
972  break;
974  error = this->op_sge();
975  break;
977  error = this->op_feq();
978  break;
980  error = this->op_fne();
981  break;
983  error = this->op_flt();
984  break;
986  error = this->op_fle();
987  break;
989  error = this->op_fgt();
990  break;
992  error = this->op_fge();
993  break;
995  error = this->op_not();
996  break;
998  error = this->op_fpext();
999  break;
1001  error = this->op_fptrunc();
1002  break;
1004  error = this->op_fptosi();
1005  break;
1007  error = this->op_fptoui();
1008  break;
1010  error = this->op_sitofp();
1011  break;
1013  error = this->op_uitofp();
1014  break;
1015  case Fpy::DirectiveId::ADD:
1016  error = this->op_add();
1017  break;
1018  case Fpy::DirectiveId::SUB:
1019  error = this->op_sub();
1020  break;
1021  case Fpy::DirectiveId::MUL:
1022  error = this->op_mul();
1023  break;
1025  error = this->op_udiv();
1026  break;
1028  error = this->op_sdiv();
1029  break;
1031  error = this->op_umod();
1032  break;
1034  error = this->op_smod();
1035  break;
1037  error = this->op_fadd();
1038  break;
1040  error = this->op_fsub();
1041  break;
1043  error = this->op_fmul();
1044  break;
1046  error = this->op_fdiv();
1047  break;
1049  error = this->op_fpow();
1050  break;
1052  error = this->op_flog();
1053  break;
1055  error = this->op_fmod();
1056  break;
1058  error = this->op_siext_8_64();
1059  break;
1061  error = this->op_siext_16_64();
1062  break;
1064  error = this->op_siext_32_64();
1065  break;
1067  error = this->op_ziext_8_64();
1068  break;
1070  error = this->op_ziext_16_64();
1071  break;
1073  error = this->op_ziext_32_64();
1074  break;
1076  error = this->op_itrunc_64_8();
1077  break;
1079  error = this->op_itrunc_64_16();
1080  break;
1082  error = this->op_itrunc_64_32();
1083  break;
1084  default:
1085  FW_ASSERT(0, directive.get__op());
1086  break;
1087  }
1088  if (error != DirectiveError::NO_ERROR) {
1090  }
1092 }
1093 
1094 Signal FpySequencer::exit_directiveHandler(const FpySequencer_ExitDirective& directive, DirectiveError& error) {
1095  if (this->m_runtime.stack.size < 1) {
1098  }
1099  U8 errorCode = this->m_runtime.stack.pop<U8>();
1100  // exit(0), no error
1101  if (errorCode == 0) {
1102  // just goto the end of the sequence
1103  this->m_runtime.nextStatementIndex = this->m_sequenceObj.get_header().get_statementCount();
1105  }
1106  // otherwise, kill the sequence here
1107  // raise the user defined error code as an event
1108  this->log_WARNING_HI_SequenceExitedWithError(this->m_sequenceFilePath, errorCode);
1111 }
1112 
1113 Signal FpySequencer::allocate_directiveHandler(const FpySequencer_AllocateDirective& directive, DirectiveError& error) {
1114  if (directive.get_size() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1117  }
1118  this->m_runtime.stack.pushZeroes(directive.get_size());
1120 }
1121 
1123 Signal FpySequencer::storeHelper(Fpy::StackSizeType destOffset, Fpy::StackSizeType size, DirectiveError& error) {
1124  if (this->m_runtime.stack.size < size) {
1127  }
1128  // After popping the value, would the write go out of bounds?
1129  Fpy::StackSizeType newStackSize = this->m_runtime.stack.size - size;
1130  // Overflow-safe check: destOffset + size > newStackSize
1131  // Rewritten as: check destOffset <= newStackSize first, then size > newStackSize - destOffset
1132  if (destOffset > newStackSize || size > newStackSize - destOffset) {
1135  }
1136  // Copy value to the destination location
1137  this->m_runtime.stack.copy(destOffset, this->m_runtime.stack.size - size, size);
1138  this->m_runtime.stack.size = newStackSize;
1140 }
1141 
1143 Signal FpySequencer::loadHelper(Fpy::StackSizeType srcOffset, Fpy::StackSizeType size, DirectiveError& error) {
1144  if (size > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1147  }
1148  // Overflow-safe check: srcOffset + size > stack.size
1149  // Rewritten as: check srcOffset <= stack.size first, then size > stack.size - srcOffset
1150  if (srcOffset > this->m_runtime.stack.size || size > this->m_runtime.stack.size - srcOffset) {
1153  }
1154  // Copy from source location to top of stack
1155  this->m_runtime.stack.copy(this->m_runtime.stack.size, srcOffset, size);
1156  this->m_runtime.stack.size += size;
1158 }
1159 
1160 Signal FpySequencer::storeRelConstOffset_directiveHandler(const FpySequencer_StoreRelConstOffsetDirective& directive,
1161  DirectiveError& error) {
1162  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + directive.get_lvarOffset();
1163  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1166  }
1167  return this->storeHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1168 }
1169 
1170 Signal FpySequencer::loadRel_directiveHandler(const FpySequencer_LoadRelDirective& directive, DirectiveError& error) {
1171  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + directive.get_lvarOffset();
1172  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1175  }
1176  return this->loadHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1177 }
1178 
1179 Signal FpySequencer::pushVal_directiveHandler(const FpySequencer_PushValDirective& directive, DirectiveError& error) {
1180  if (directive.get__valSize() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1183  }
1184  // copy from the bytearray in the directive to the stack, add to stack size.
1185  this->m_runtime.stack.push(const_cast<U8*>(directive.get_val()),
1186  static_cast<Fpy::StackSizeType>(directive.get__valSize()));
1188 }
1189 
1190 Signal FpySequencer::discard_directiveHandler(const FpySequencer_DiscardDirective& directive, DirectiveError& error) {
1191  if (this->m_runtime.stack.size < directive.get_size()) {
1194  }
1195  // drop the specified amount of bytes off the stack. simple as.
1196  this->m_runtime.stack.size -= directive.get_size();
1198 }
1199 
1200 Signal FpySequencer::memCmp_directiveHandler(const FpySequencer_MemCmpDirective& directive, DirectiveError& error) {
1201  // Overflow-safe check: we need size * 2 bytes on the stack
1202  // First check that size * 2 doesn't overflow: size > MAX/2 would overflow
1203  // MAX_STACK_SIZE is the upper bound for stack.size, so if size > MAX_STACK_SIZE/2, we definitely don't have enough
1204  if (directive.get_size() > Fpy::MAX_STACK_SIZE / 2) {
1207  }
1208  // Now safe to compute size * 2
1209  if (this->m_runtime.stack.size < directive.get_size() * 2) {
1212  }
1213 
1214  // find the starting offsets of the two byte arrays
1215  U64 lhsOffset = this->m_runtime.stack.size - directive.get_size() * 2;
1216  U64 rhsOffset = this->m_runtime.stack.size - directive.get_size();
1217 
1218  // "officially" remove them from the stack
1219  // you have to do this before pushing to the stack, otherwise the result would get placed
1220  // after the byte arrays
1221  this->m_runtime.stack.size -= directive.get_size() * 2;
1222 
1223  // memcmp the two byte arrays, push FW_SERIALIZE_TRUE_VALUE if they were equal, FW_SERIALIZE_FALSE_VALUE otherwise
1224  if (memcmp(this->m_runtime.stack.bytes + lhsOffset, this->m_runtime.stack.bytes + rhsOffset,
1225  directive.get_size()) == 0) {
1226  this->m_runtime.stack.push<U8>(static_cast<U8>(FW_SERIALIZE_TRUE_VALUE));
1227  } else {
1228  this->m_runtime.stack.push<U8>(static_cast<U8>(FW_SERIALIZE_FALSE_VALUE));
1229  }
1231 }
1232 
1233 Signal FpySequencer::stackCmd_directiveHandler(const FpySequencer_StackCmdDirective& directive, DirectiveError& error) {
1234  // Overflow-safe check: need argsSize + sizeof(FwOpcodeType) bytes
1235  // Check stack.size >= sizeof(FwOpcodeType) first, then stack.size - sizeof(FwOpcodeType) >= argsSize
1236  if (this->m_runtime.stack.size < sizeof(FwOpcodeType) ||
1237  this->m_runtime.stack.size - sizeof(FwOpcodeType) < directive.get_argsSize()) {
1240  }
1241 
1242  // pop the opcode of the cmd off the stack
1243  // note this means that, unlike the actual byte array that the dispatcher gets,
1244  // these cmds have opcode after the argument buffer
1245  FwOpcodeType opcode = this->m_runtime.stack.pop<FwOpcodeType>();
1246  U64 argBufOffset = this->m_runtime.stack.size - directive.get_argsSize();
1247 
1248  // update the opcode of the cmd we will await
1249  this->m_runtime.currentCmdOpcode = opcode;
1250 
1251  // also pop the args off the stack
1252  this->m_runtime.stack.size -= directive.get_argsSize();
1253 
1254  if (this->sendCmd(opcode, this->m_runtime.stack.bytes + argBufOffset, directive.get_argsSize()) ==
1257  } else {
1258  // now tell the SM to wait some more until we get the cmd response back
1259  // if we've already got the response back this should be harmless
1261  }
1262 
1264 }
1265 
1266 Signal FpySequencer::pushTime_directiveHandler(const FpySequencer_PushTimeDirective& directive, DirectiveError& error) {
1267  if (Fpy::MAX_STACK_SIZE - Fw::Time::SERIALIZED_SIZE < this->m_runtime.stack.size) {
1270  }
1271 
1272  Fw::Time currentTime = this->getTime();
1273 
1274  U8 currentTimeBuf[Fw::Time::SERIALIZED_SIZE] = {};
1275  Fw::ExternalSerializeBuffer timeEsb(currentTimeBuf, Fw::Time::SERIALIZED_SIZE);
1276  Fw::SerializeStatus stat = timeEsb.serializeFrom(currentTime);
1277 
1278  // coding error if this failed, we should have enough space
1279  FW_ASSERT(stat == Fw::SerializeStatus::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(stat));
1280 
1281  // push time to end of stack
1282  this->m_runtime.stack.push(timeEsb.getBuffAddr(), static_cast<Fpy::StackSizeType>(timeEsb.getSize()));
1284 }
1285 
1286 Signal FpySequencer::getField_directiveHandler(const FpySequencer_GetFieldDirective& directive, DirectiveError& error) {
1287  // Need sizeof(StackSizeType) for the offset AND parentSize for the parent data
1288  // Check we have enough for the offset first
1289  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType)) {
1292  }
1293  // After popping the offset, we need at least parentSize bytes remaining
1294  if (this->m_runtime.stack.size - sizeof(Fpy::StackSizeType) < directive.get_parentSize()) {
1297  }
1298 
1299  Fpy::StackSizeType offset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1300 
1301  // Overflow-safe check: offset + memberSize > parentSize
1302  // Rewritten as: check offset <= parentSize first, then memberSize > parentSize - offset
1303  if (offset > directive.get_parentSize() || directive.get_memberSize() > directive.get_parentSize() - offset) {
1304  // i think it's somewhat ambiguous whether this is a stack access out of bounds
1305  // but there isn't really an error code that better reflects this, and i guess
1306  // it's technically true
1309  }
1310 
1311  // the resulting bytes should move to the start of the parent array
1312 
1313  // Calculate the offset of the parent start in the stack
1314  Fpy::StackSizeType parentStartOffset = this->m_runtime.stack.size - directive.get_parentSize();
1315  // Overflow-safe: parentStartOffset + offset cannot overflow since offset <= parentSize
1316  // and parentStartOffset + parentSize == stack.size (which is bounded)
1317  this->m_runtime.stack.move(parentStartOffset, parentStartOffset + offset, directive.get_memberSize());
1318  // adjust stack size by the diff between the member and the parent
1319  this->m_runtime.stack.size -= (directive.get_parentSize() - directive.get_memberSize());
1321 }
1322 
1323 Signal FpySequencer::peek_directiveHandler(const FpySequencer_PeekDirective& directive, DirectiveError& error) {
1324  // must have at least two StackSizeType on stack
1325  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) * 2) {
1328  }
1329 
1330  Fpy::StackSizeType offset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1331  Fpy::StackSizeType byteCount = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1332 
1333  // Check offset doesn't exceed stack size (after both pops)
1334  if (offset > this->m_runtime.stack.size) {
1335  // would access past the bottom of the stack
1336  // note we allow the equals case because the byteCount might be 0
1339  }
1340  if (byteCount > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1341  // we would overflow the stack if we pushed this many bytes to it
1344  }
1345  // Overflow-safe check: byteCount + offset > stack.size
1346  // Rewritten as: check offset <= stack.size (done above), then byteCount > stack.size - offset
1347  if (byteCount > this->m_runtime.stack.size - offset) {
1348  // would access past the bottom of the stack
1351  }
1352  // start copying from the lowest byte of the src array
1353  U8* src = this->m_runtime.stack.top() - offset - byteCount;
1354  this->m_runtime.stack.push(src, byteCount);
1356 }
1357 
1358 Signal FpySequencer::storeRel_directiveHandler(const FpySequencer_StoreRelDirective& directive, DirectiveError& error) {
1359  // Need enough bytes for the value and the offset (SignedStackSizeType = 4 bytes)
1360  // Overflow-safe: check stack.size >= sizeof(SignedStackSizeType) first, then stack.size -
1361  // sizeof(SignedStackSizeType) >= size
1362  if (this->m_runtime.stack.size < sizeof(Fpy::SignedStackSizeType) ||
1363  this->m_runtime.stack.size - sizeof(Fpy::SignedStackSizeType) < directive.get_size()) {
1366  }
1367 
1368  // Pop the signed offset from the stack
1369  Fpy::SignedStackSizeType lvarOffset = this->m_runtime.stack.pop<Fpy::SignedStackSizeType>();
1370 
1371  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + lvarOffset;
1372  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1375  }
1376  return this->storeHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1377 }
1378 
1379 Signal FpySequencer::call_directiveHandler(const FpySequencer_CallDirective& directive, DirectiveError& error) {
1380  // Need at least 4 bytes for the target address
1381  if (this->m_runtime.stack.size < sizeof(U32)) {
1384  }
1385 
1386  // Pop the target directive index from the stack
1387  U32 target = this->m_runtime.stack.pop<U32>();
1388 
1389  // Check if we have space to push return address and saved frame pointer (8 bytes total)
1390  if (this->m_runtime.stack.size + sizeof(Fpy::StackSizeType) + sizeof(U32) > Fpy::MAX_STACK_SIZE) {
1393  }
1394 
1395  // Check target is within bounds (will also be checked at execution time)
1396  if (target > m_sequenceObj.get_header().get_statementCount()) {
1399  }
1400 
1401  // Save the return address (next instruction after CALL)
1402  U32 returnAddr = this->m_runtime.nextStatementIndex;
1403 
1404  // Set the next instruction to the target
1405  this->m_runtime.nextStatementIndex = target;
1406 
1407  // Push the return address to the stack
1408  this->m_runtime.stack.push<U32>(returnAddr);
1409 
1410  // Push the current frame pointer to the stack
1411  this->m_runtime.stack.push<Fpy::StackSizeType>(this->m_runtime.stack.currentFrameStart);
1412 
1413  // Set the new frame pointer to the current top of stack
1414  this->m_runtime.stack.currentFrameStart = this->m_runtime.stack.size;
1415 
1417 }
1418 
1419 Signal FpySequencer::return_directiveHandler(const FpySequencer_ReturnDirective& directive, DirectiveError& error) {
1420  Fpy::StackSizeType returnValSize = directive.get_returnValSize();
1421  Fpy::StackSizeType callArgsSize = directive.get_callArgsSize();
1422 
1423  // Check we have enough bytes for the return value
1424  if (this->m_runtime.stack.size < returnValSize) {
1427  }
1428 
1429  // returnValSize is guaranteed to be less than Fpy::MAX_STACK_SIZE because it's less than the stack.size
1430  // thus the memcpy won't fail
1431 
1432  // Save the return value if there is one
1433  U8 returnValue[Fpy::MAX_STACK_SIZE] = {};
1434  if (returnValSize > 0) {
1435  memcpy(returnValue, this->m_runtime.stack.top() - returnValSize, returnValSize);
1436  }
1437 
1438  // Truncate the stack to stack_frame_start (discard all local variables)
1439  if (this->m_runtime.stack.currentFrameStart > this->m_runtime.stack.size) {
1442  }
1443  this->m_runtime.stack.size = this->m_runtime.stack.currentFrameStart;
1444 
1445  // Check we have enough bytes for saved frame pointer and return address
1446  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) + sizeof(U32)) {
1449  }
1450 
1451  // Pop the saved frame pointer
1452  Fpy::StackSizeType savedFramePtr = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1453 
1454  // Pop the return address
1455  U32 returnAddr = this->m_runtime.stack.pop<U32>();
1456 
1457  // Restore the frame pointer
1458  if (savedFramePtr > this->m_runtime.stack.size) {
1461  }
1462  this->m_runtime.stack.currentFrameStart = savedFramePtr;
1463 
1464  // Validate the return address is within bounds
1465  if (returnAddr > m_sequenceObj.get_header().get_statementCount()) {
1468  }
1469 
1470  // Set the next instruction to the return address
1471  this->m_runtime.nextStatementIndex = returnAddr;
1472 
1473  // Check that we have enough bytes for the call arguments
1474  if (this->m_runtime.stack.size < callArgsSize) {
1477  }
1478  // Discard the function arguments
1479  this->m_runtime.stack.size -= callArgsSize;
1480 
1481  // Push the return value
1482  if (returnValSize > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1485  }
1486  this->m_runtime.stack.push(returnValue, returnValSize);
1487 
1489 }
1490 
1491 Signal FpySequencer::loadAbs_directiveHandler(const FpySequencer_LoadAbsDirective& directive, DirectiveError& error) {
1492  return this->loadHelper(directive.get_globalOffset(), directive.get_size(), error);
1493 }
1494 
1495 Signal FpySequencer::storeAbs_directiveHandler(const FpySequencer_StoreAbsDirective& directive, DirectiveError& error) {
1496  Fpy::StackSizeType size = directive.get_size();
1497 
1498  // Need enough bytes for the value and the offset
1499  // Overflow-safe: check stack.size >= sizeof(StackSizeType) first, then stack.size - sizeof >= size
1500  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) ||
1501  this->m_runtime.stack.size - sizeof(Fpy::StackSizeType) < size) {
1504  }
1505 
1506  // Pop the global offset from the stack
1507  Fpy::StackSizeType globalOffset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1508 
1509  return this->storeHelper(globalOffset, size, error);
1510 }
1511 
1512 Signal FpySequencer::storeAbsConstOffset_directiveHandler(const FpySequencer_StoreAbsConstOffsetDirective& directive,
1513  DirectiveError& error) {
1514  return this->storeHelper(directive.get_globalOffset(), directive.get_size(), error);
1515 }
1516 
1520  this->sendSignal(this->popEvent_directiveHandler(directive, error));
1521  handleDirectiveErrorCode(Fpy::DirectiveId::POP_EVENT, error);
1522 }
1523 
1524 Signal FpySequencer::popEvent_directiveHandler(const FpySequencer_PopEventDirective& directive, DirectiveError& error) {
1525  // Pop messageSize from the stack
1526  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType)) {
1529  }
1530  Fpy::StackSizeType messageSize = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1531 
1532  // Need message_size bytes + sizeof(LogSeverity serial type) for severity
1533  if (this->m_runtime.stack.size < messageSize + sizeof(Fw::LogSeverity::SerialType)) {
1536  }
1537 
1538  // Pop message bytes first
1539  U8 messageBuf[FW_LOG_STRING_MAX_SIZE] = {};
1540  // don't read in more than (log string size) - 1 bytes
1541  Fpy::StackSizeType clampedSize = std::min(messageSize, static_cast<Fpy::StackSizeType>(FW_LOG_STRING_MAX_SIZE - 1));
1542  // If message is larger than buffer, discard the excess bytes first (from top of stack, which is the end of the
1543  // message)
1544  if (messageSize > clampedSize) {
1545  Fpy::StackSizeType excess = messageSize - clampedSize;
1546  this->m_runtime.stack.size -= excess;
1547  }
1548  this->m_runtime.stack.pop(messageBuf, clampedSize);
1549  messageBuf[clampedSize] = '\0';
1550 
1551  // Pop severity
1552  Fw::LogSeverity::SerialType severity = this->m_runtime.stack.pop<Fw::LogSeverity::SerialType>();
1553 
1554  // Construct the message string
1555  Fw::String messageStr(reinterpret_cast<const char*>(messageBuf));
1556 
1557  // Emit the appropriate event based on severity
1558  switch (severity) {
1560  this->log_FATAL_LogFatal(this->m_sequenceFilePath, messageStr);
1561  break;
1563  this->log_WARNING_HI_LogWarningHi(this->m_sequenceFilePath, messageStr);
1564  break;
1566  this->log_WARNING_LO_LogWarningLo(this->m_sequenceFilePath, messageStr);
1567  break;
1569  this->log_COMMAND_LogCommand(this->m_sequenceFilePath, messageStr);
1570  break;
1572  this->log_ACTIVITY_HI_LogActivityHi(this->m_sequenceFilePath, messageStr);
1573  break;
1575  this->log_ACTIVITY_LO_LogActivityLo(this->m_sequenceFilePath, messageStr);
1576  break;
1578  this->log_DIAGNOSTIC_LogDiagnostic(this->m_sequenceFilePath, messageStr);
1579  break;
1580  default:
1583  }
1584 
1586 }
1587 
1588 } // namespace Svc
void directive_storeRelConstOffset_internalInterfaceHandler(const Svc::FpySequencer_StoreRelConstOffsetDirective &directive) override
Internal interface handler for directive_storeRelConstOffset.
void cmdOut_out(FwIndexType portNum, Fw::ComBuffer &data, U32 context) const
Invoke output port cmdOut.
void directive_storeRel_internalInterfaceHandler(const Svc::FpySequencer_StoreRelDirective &directive) override
Internal interface handler for directive_storeRel.
Serialization/Deserialization operation was successful.
void directive_return_internalInterfaceHandler(const Svc::FpySequencer_ReturnDirective &directive) override
Internal interface handler for directive_return.
sets the index of the next directive to execute
U8 * getBuffAddr()
Get buffer address for data filling (non-const version)
Definition: PrmBuffer.cpp:42
void directive_call_internalInterfaceHandler(const Svc::FpySequencer_CallDirective &directive) override
Internal interface handler for directive_call.
bool isConnected_getTlmChan_OutputPort(FwIndexType portNum) const
FwIdType FwOpcodeType
The type of a command opcode.
SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG) override
Serialize an 8-bit unsigned integer value.
U16 get_statementCount() const
Get member statementCount.
branches based off of the top byte of the stack
void directive_if_internalInterfaceHandler(const Svc::FpySequencer_IfDirective &directive) override
Internal interface handler for directive_if.
Representing success.
PlatformSizeType FwSizeType
stores a value to an absolute address in the stack (for global variables), offset from stack ...
void directive_loadAbs_internalInterfaceHandler(const Svc::FpySequencer_LoadAbsDirective &directive) override
Internal interface handler for directive_loadAbs.
void directive_stackOp_internalInterfaceHandler(const Svc::FpySequencer_StackOpDirective &directive) override
Internal interface handler for directive_stackOp.
called when statement successfully executed. only raised in the RUNNING.AWAITING_CMD_RESPONSE state ...
void directive_getField_internalInterfaceHandler(const Svc::FpySequencer_GetFieldDirective &directive) override
Internal interface handler for directive_getField.
I32 SignedStackSizeType
signed version of StackSizeType, used for relative offsets that can be negative
Serializable::SizeType getSize() const override
Get current buffer size.
void log_WARNING_HI_SequenceExitedWithError(const Fw::StringBase &filePath, U8 errorCode) const
Log event SequenceExitedWithError.
void directive_pushTlmValAndTime_internalInterfaceHandler(const Svc::FpySequencer_PushTlmValAndTimeDirective &directive) override
Internal interface handler for directive_pushTlmValAndTime.
int8_t I8
8-bit signed integer
Definition: BasicTypes.h:50
stores a value to a local variable at a compile-time-known offset relative to the current stack frame...
void directive_constCmd_internalInterfaceHandler(const Svc::FpySequencer_ConstCmdDirective &directive) override
Internal interface handler for directive_constCmd.
Fw::ParamValid getParam_out(FwIndexType portNum, FwPrmIdType id, Fw::ParamBuffer &val) const
Invoke output port getParam.
bool isConnected_prmGet_OutputPort(FwIndexType portNum) const
pop an opcode and arg buf off the stack, send to cmd dispatcher and await response ...
void directive_peek_internalInterfaceHandler(const Svc::FpySequencer_PeekDirective &directive) override
Internal interface handler for directive_peek.
void sequencer_sendSignal_stmtResponse_success()
Send signal stmtResponse_success to state machine sequencer.
void directive_loadRel_internalInterfaceHandler(const Svc::FpySequencer_LoadRelDirective &directive) override
Internal interface handler for directive_loadRel.
void directive_memCmp_internalInterfaceHandler(const Svc::FpySequencer_MemCmpDirective &directive) override
Internal interface handler for directive_memCmp.
void directive_pushVal_internalInterfaceHandler(const Svc::FpySequencer_PushValDirective &directive) override
Internal interface handler for directive_pushVal.
void directive_pushPrm_internalInterfaceHandler(const Svc::FpySequencer_PushPrmDirective &directive) override
Internal interface handler for directive_pushPrm.
pops bytes off the top of the stack and does nothing with them
void directive_pushTime_internalInterfaceHandler(const Svc::FpySequencer_PushTimeDirective &directive) override
Internal interface handler for directive_pushTime.
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.
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
float F32
32-bit floating point
Definition: BasicTypes.h:83
Fpy::DirectiveErrorCode DirectiveError
void directive_allocate_internalInterfaceHandler(const Svc::FpySequencer_AllocateDirective &directive) override
Internal interface handler for directive_allocate.
void directive_waitRel_internalInterfaceHandler(const FpySequencer_WaitRelDirective &directive) override
Internal interface handler for directive_waitRel.
void directive_noOp_internalInterfaceHandler(const Svc::FpySequencer_NoOpDirective &directive) override
Internal interface handler for directive_noOp.
loads a value from a local variable at a compile-time-known offset relative to the current stack fram...
void directive_stackCmd_internalInterfaceHandler(const Svc::FpySequencer_StackCmdDirective &directive) override
Internal interface handler for directive_stackCmd.
void log_ACTIVITY_HI_LogActivityHi(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogActivityHi.
void directive_discard_internalInterfaceHandler(const Svc::FpySequencer_DiscardDirective &directive) override
Internal interface handler for directive_discard.
pop two byte arrays off the top of the stack, call memcmp, push 1 if they were equal, 0 otherwise
Svc::Fpy::Header & get_header()
Get member header.
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_exit_internalInterfaceHandler(const Svc::FpySequencer_ExitDirective &directive) override
Internal interface handler for directive_exit.
stores a value to an absolute address in the stack (for global variables), const offset ...
External serialize buffer with no copy semantics.
Svc::Fpy::DirectiveId::T get__op() const
Get member _op.
void directive_waitAbs_internalInterfaceHandler(const FpySequencer_WaitAbsDirective &directive) override
Internal interface handler for directive_waitAbs.
U8 * getBuffAddr()
Get buffer address for data filling (non-const version)
Definition: TlmBuffer.cpp:42
void log_WARNING_HI_LogWarningHi(const Fw::StringBase &filePath, const Fw::StringBase &message) const
Log event LogWarningHi.
void directive_goto_internalInterfaceHandler(const Svc::FpySequencer_GotoDirective &directive) override
Internal interface handler for directive_goto.
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 sequencer_sendSignal_stmtResponse_beginSleep()
Send signal stmtResponse_beginSleep to state machine sequencer.
Representing failure.
void directive_storeAbsConstOffset_internalInterfaceHandler(const Svc::FpySequencer_StoreAbsConstOffsetDirective &directive) override
Internal interface handler for directive_storeAbsConstOffset.
sleeps for a relative duration from the current time
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:53
void directive_popEvent_internalInterfaceHandler(const Svc::FpySequencer_PopEventDirective &directive) override
Internal interface handler for directive_popEvent.
Important informational events.
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
static Time add(const Time &a, const Time &b)
Definition: Time.cpp:164
void sequencer_sendSignal_stmtResponse_keepWaiting()
Send signal stmtResponse_keepWaiting to state machine sequencer.
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)
double F64
64-bit floating point (double). Required for compiler-supplied double promotion.
Definition: BasicTypes.h:85
A fatal non-recoverable event.
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.
void directive_storeAbs_internalInterfaceHandler(const Svc::FpySequencer_StoreAbsDirective &directive) override
Internal interface handler for directive_storeAbs.
U16 FwTimeBaseStoreType
The type used to serialize a time base value.
pushes the current Fw.Time struct to the stack
called when the statement is telling the sequencer to await a later stmt response ...
FpySequencer_SequencerStateMachineStateMachineBase::Signal Signal
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
Success/Failure.
U8 SerialType
The serial representation type.
PlatformAssertArgType FwAssertArgType
The type of arguments to assert functions.
U32 StackSizeType
the type which everything referencing a size or offset on the stack is represented in ...
void directive_pushTlmVal_internalInterfaceHandler(const Svc::FpySequencer_PushTlmValDirective &directive) override
Internal interface handler for directive_pushTlmVal.
#define U64(C)
Definition: sha.h:181