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 <cmath>
2 #include <cstring>
3 #include <type_traits>
4 #include "Fw/Com/ComPacket.hpp"
6 
7 namespace Svc {
8 
9 void FpySequencer::sendSignal(Signal signal) {
10  switch (signal) {
13  break;
14  }
17  break;
18  }
21  break;
22  }
25  break;
26  }
27  default: {
28  FW_ASSERT(0, static_cast<FwAssertArgType>(signal));
29  }
30  }
31 }
32 
33 // utility method for updating telemetry based on a directive error code
34 void FpySequencer::handleDirectiveErrorCode(Fpy::DirectiveId id, DirectiveError err) {
35  this->m_tlm.lastDirectiveError = err;
36  if (err != DirectiveError::NO_ERROR) {
37  this->m_tlm.directiveErrorIndex = this->currentStatementIdx();
38  this->m_tlm.directiveErrorId = id;
39  }
40 }
41 
42 Fw::Success FpySequencer::sendCmd(FwOpcodeType opcode, const U8* argBuf, FwSizeType argBufSize) {
43  Fw::ComBuffer cmdBuf;
44  Fw::SerializeStatus stat =
45  cmdBuf.serializeFrom(static_cast<FwPacketDescriptorType>(Fw::ComPacketType::FW_PACKET_COMMAND));
46  // TODO should I assert here? this really shouldn't fail, I should just add a static assert
47  // on com buf size and then assert here
49  return Fw::Success::FAILURE;
50  }
51  stat = cmdBuf.serializeFrom(opcode);
53  return Fw::Success::FAILURE;
54  }
55  stat = cmdBuf.serializeFrom(argBuf, argBufSize, Fw::Serialization::OMIT_LENGTH);
57  return Fw::Success::FAILURE;
58  }
59 
60  // calculate the unique command identifier:
61  // cmd UID is formatted like XXYY, where XX are the first two bytes of the m_sequencesStarted counter
62  // and YY are the first two bytes of the m_statementsDispatched counter.
63  // this way, we know when we get a cmd back A) whether or not it's from this sequence (modulo 2^16) and B)
64  // whether or not it's this specific instance of the cmd in the sequence, and not another one with the same opcode
65  // somewhere else in the file.
66  // if we put this uid in the context we send to the cmdDisp, we will get it back when the cmd returns
67  U32 cmdUid =
68  static_cast<U32>(((this->m_sequencesStarted & 0xFFFF) << 16) | (this->m_statementsDispatched & 0xFFFF));
69 
70  this->cmdOut_out(0, cmdBuf, cmdUid);
71 
72  return Fw::Success::SUCCESS;
73 }
74 
78  this->sendSignal(this->waitRel_directiveHandler(directive, error));
79  handleDirectiveErrorCode(Fpy::DirectiveId::WAIT_REL, error);
80 }
81 
85  this->sendSignal(this->waitAbs_directiveHandler(directive, error));
86  handleDirectiveErrorCode(Fpy::DirectiveId::WAIT_ABS, error);
87 }
88 
92  this->sendSignal(this->goto_directiveHandler(directive, error));
93  handleDirectiveErrorCode(Fpy::DirectiveId::GOTO, error);
94 }
95 
99  this->sendSignal(this->if_directiveHandler(directive, error));
100  handleDirectiveErrorCode(Fpy::DirectiveId::IF, error);
101 }
102 
106  this->sendSignal(this->noOp_directiveHandler(directive, error));
107  handleDirectiveErrorCode(Fpy::DirectiveId::NO_OP, error);
108 }
109 
112  const Svc::FpySequencer_PushTlmValDirective& directive) {
114  this->sendSignal(this->pushTlmVal_directiveHandler(directive, error));
115  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TLM_VAL, error);
116 }
117 
122  this->sendSignal(this->pushTlmValAndTime_directiveHandler(directive, error));
123  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TLM_VAL_AND_TIME, error);
124 }
125 
129  this->sendSignal(this->pushPrm_directiveHandler(directive, error));
130  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_PRM, error);
131 }
132 
136  this->sendSignal(this->constCmd_directiveHandler(directive, error));
137  handleDirectiveErrorCode(Fpy::DirectiveId::CONST_CMD, error);
138 }
139 
143  this->sendSignal(this->stackOp_directiveHandler(directive, error));
144  handleDirectiveErrorCode(directive.get__op(), error);
145 }
146 
150  this->sendSignal(this->exit_directiveHandler(directive, error));
151  handleDirectiveErrorCode(Fpy::DirectiveId::EXIT, error);
152 }
153 
157  this->sendSignal(this->allocate_directiveHandler(directive, error));
158  handleDirectiveErrorCode(Fpy::DirectiveId::ALLOCATE, error);
159 }
160 
165  this->sendSignal(this->storeRelConstOffset_directiveHandler(directive, error));
166  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_REL_CONST_OFFSET, error);
167 }
168 
172  this->sendSignal(this->pushVal_directiveHandler(directive, error));
173  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_VAL, error);
174 }
175 
179  this->sendSignal(this->loadRel_directiveHandler(directive, error));
180  handleDirectiveErrorCode(Fpy::DirectiveId::LOAD_REL, error);
181 }
182 
186  this->sendSignal(this->discard_directiveHandler(directive, error));
187  handleDirectiveErrorCode(Fpy::DirectiveId::DISCARD, error);
188 }
189 
193  this->sendSignal(this->memCmp_directiveHandler(directive, error));
194  handleDirectiveErrorCode(Fpy::DirectiveId::MEMCMP, error);
195 }
196 
200  this->sendSignal(this->stackCmd_directiveHandler(directive, error));
201  handleDirectiveErrorCode(Fpy::DirectiveId::STACK_CMD, error);
202 }
203 
207  this->sendSignal(this->pushTime_directiveHandler(directive, error));
208  handleDirectiveErrorCode(Fpy::DirectiveId::PUSH_TIME, error);
209 }
210 
214  this->sendSignal(this->setFlag_directiveHandler(directive, error));
215  handleDirectiveErrorCode(Fpy::DirectiveId::SET_FLAG, error);
216 }
217 
221  this->sendSignal(this->getFlag_directiveHandler(directive, error));
222  handleDirectiveErrorCode(Fpy::DirectiveId::GET_FLAG, error);
223 }
224 
228  this->sendSignal(this->getField_directiveHandler(directive, error));
229  handleDirectiveErrorCode(Fpy::DirectiveId::GET_FIELD, error);
230 }
231 
235  this->sendSignal(this->peek_directiveHandler(directive, error));
236  handleDirectiveErrorCode(Fpy::DirectiveId::PEEK, error);
237 }
238 
242  this->sendSignal(this->storeRel_directiveHandler(directive, error));
243  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_REL, error);
244 }
245 
249  this->sendSignal(this->call_directiveHandler(directive, error));
250  handleDirectiveErrorCode(Fpy::DirectiveId::CALL, error);
251 }
252 
256  this->sendSignal(this->return_directiveHandler(directive, error));
257  handleDirectiveErrorCode(Fpy::DirectiveId::RETURN, error);
258 }
259 
263  this->sendSignal(this->loadAbs_directiveHandler(directive, error));
264  handleDirectiveErrorCode(Fpy::DirectiveId::LOAD_ABS, error);
265 }
266 
270  this->sendSignal(this->storeAbs_directiveHandler(directive, error));
271  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_ABS, error);
272 }
273 
278  this->sendSignal(this->storeAbsConstOffset_directiveHandler(directive, error));
279  handleDirectiveErrorCode(Fpy::DirectiveId::STORE_ABS_CONST_OFFSET, error);
280 }
281 
283 Signal FpySequencer::waitRel_directiveHandler(const FpySequencer_WaitRelDirective& directive, DirectiveError& error) {
284  if (this->m_runtime.stack.size < 8) {
287  }
288 
289  Fw::Time wakeupTime = this->getTime();
290 
291  U32 uSeconds = this->m_runtime.stack.pop<U32>();
292  U32 seconds = this->m_runtime.stack.pop<U32>();
293 
294  wakeupTime.add(seconds, uSeconds);
295  this->m_runtime.wakeupTime = wakeupTime;
297 }
298 
300 Signal FpySequencer::waitAbs_directiveHandler(const FpySequencer_WaitAbsDirective& directive, DirectiveError& error) {
301  if (this->m_runtime.stack.size < 10 + sizeof(FwTimeContextStoreType)) {
304  }
305 
306  U32 uSeconds = this->m_runtime.stack.pop<U32>();
307  U32 seconds = this->m_runtime.stack.pop<U32>();
308  FwTimeContextStoreType ctx = this->m_runtime.stack.pop<FwTimeContextStoreType>();
309  U16 base = this->m_runtime.stack.pop<U16>();
310 
311  this->m_runtime.wakeupTime = Fw::Time(static_cast<TimeBase::T>(base), ctx, seconds, uSeconds);
313 }
314 
316 Signal FpySequencer::goto_directiveHandler(const FpySequencer_GotoDirective& directive, DirectiveError& error) {
317  // check within sequence bounds, or at EOF (we allow == case cuz this just ends the sequence)
318  if (directive.get_statementIndex() > m_sequenceObj.get_header().get_statementCount()) {
321  }
322  m_runtime.nextStatementIndex = directive.get_statementIndex();
324 }
325 
327 Signal FpySequencer::if_directiveHandler(const FpySequencer_IfDirective& directive, DirectiveError& error) {
328  if (this->m_runtime.stack.size < 1) {
331  }
332  // check within sequence bounds, or at EOF (we allow == case cuz this just ends the sequence)
333  if (directive.get_falseGotoStmtIndex() > m_sequenceObj.get_header().get_statementCount()) {
336  }
337 
338  if (this->m_runtime.stack.pop<U8>() != 0) {
339  // proceed to next instruction
341  }
342 
343  // conditional false case
344  this->m_runtime.nextStatementIndex = directive.get_falseGotoStmtIndex();
346 }
347 
348 Signal FpySequencer::noOp_directiveHandler(const FpySequencer_NoOpDirective& directive, DirectiveError& error) {
350 }
351 
352 Signal FpySequencer::pushTlmVal_directiveHandler(const FpySequencer_PushTlmValDirective& directive,
353  DirectiveError& error) {
354  if (!this->isConnected_getTlmChan_OutputPort(0)) {
357  }
358  Fw::Time tlmTime;
359  Fw::TlmBuffer tlmValue;
360  Fw::TlmValid valid = this->getTlmChan_out(0, directive.get_chanId(), tlmTime, tlmValue);
361 
362  if (valid != Fw::TlmValid::VALID) {
363  // could not find this tlm chan
366  }
367 
368  if (Fpy::MAX_STACK_SIZE - tlmValue.getSize() < this->m_runtime.stack.size) {
371  }
372  this->m_runtime.stack.push(tlmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(tlmValue.getSize()));
374 }
375 
376 Signal FpySequencer::pushTlmValAndTime_directiveHandler(const FpySequencer_PushTlmValAndTimeDirective& directive,
377  DirectiveError& error) {
378  if (!this->isConnected_getTlmChan_OutputPort(0)) {
381  }
382 
383  Fw::Time tlmTime;
384  Fw::TlmBuffer tlmValue;
385  Fw::TlmValid valid = this->getTlmChan_out(0, directive.get_chanId(), tlmTime, tlmValue);
386 
387  if (valid != Fw::TlmValid::VALID) {
388  // could not find this tlm chan
391  }
392 
393  U8 tlmTimeBuf[Fw::Time::SERIALIZED_SIZE] = {};
395  Fw::SerializeStatus stat = timeEsb.serializeFrom(tlmTime);
396 
397  // coding error if this failed, we should have enough space
398  FW_ASSERT(stat == Fw::SerializeStatus::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(stat));
399 
400  // check that our stack won't overflow if we put both val and time on it
401  if (Fpy::MAX_STACK_SIZE - tlmValue.getSize() - timeEsb.getSize() < this->m_runtime.stack.size) {
404  }
405 
406  // push tlm to end of stack
407  this->m_runtime.stack.push(tlmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(tlmValue.getSize()));
408  // now push time to end of stack
409  this->m_runtime.stack.push(timeEsb.getBuffAddr(), static_cast<Fpy::StackSizeType>(timeEsb.getSize()));
411 }
412 
413 Signal FpySequencer::pushPrm_directiveHandler(const FpySequencer_PushPrmDirective& directive, DirectiveError& error) {
414  if (!this->isConnected_prmGet_OutputPort(0)) {
417  }
418 
419  Fw::ParamBuffer prmValue;
420  Fw::ParamValid valid = this->getParam_out(0, directive.get_prmId(), prmValue);
421 
422  if (valid != Fw::ParamValid::VALID) {
423  // could not find this prm in the DB
426  }
427 
428  if (Fpy::MAX_STACK_SIZE - prmValue.getSize() < this->m_runtime.stack.size) {
431  }
432 
433  this->m_runtime.stack.push(prmValue.getBuffAddr(), static_cast<Fpy::StackSizeType>(prmValue.getSize()));
435 }
436 
437 Signal FpySequencer::constCmd_directiveHandler(const FpySequencer_ConstCmdDirective& directive, DirectiveError& error) {
438  if (this->sendCmd(directive.get_opCode(), directive.get_argBuf(), directive.get__argBufSize()) ==
441  } else {
442  // now tell the SM to wait some more until we get the cmd response back
443  // if we've already got the response back this should be harmless
445  }
446 }
447 
448 DirectiveError FpySequencer::op_or() {
449  if (this->m_runtime.stack.size < sizeof(U8) * 2) {
451  }
452  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<U8>() | this->m_runtime.stack.pop<U8>()));
454 }
455 DirectiveError FpySequencer::op_and() {
456  if (this->m_runtime.stack.size < sizeof(U8) * 2) {
458  }
459  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<U8>() & this->m_runtime.stack.pop<U8>()));
461 }
462 DirectiveError FpySequencer::op_ieq() {
463  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
465  }
466  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<I64>() == this->m_runtime.stack.pop<I64>()));
468 }
469 DirectiveError FpySequencer::op_ine() {
470  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
472  }
473  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<I64>() != this->m_runtime.stack.pop<I64>()));
475 }
476 DirectiveError FpySequencer::op_ult() {
477  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
479  }
480  U64 rhs = this->m_runtime.stack.pop<U64>();
481  U64 lhs = this->m_runtime.stack.pop<U64>();
482  this->m_runtime.stack.push(static_cast<U8>(lhs < rhs));
484 }
485 DirectiveError FpySequencer::op_ule() {
486  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
488  }
489  U64 rhs = this->m_runtime.stack.pop<U64>();
490  U64 lhs = this->m_runtime.stack.pop<U64>();
491  this->m_runtime.stack.push(static_cast<U8>(lhs <= rhs));
493 }
494 DirectiveError FpySequencer::op_ugt() {
495  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
497  }
498  U64 rhs = this->m_runtime.stack.pop<U64>();
499  U64 lhs = this->m_runtime.stack.pop<U64>();
500  this->m_runtime.stack.push(static_cast<U8>(lhs > rhs));
502 }
503 DirectiveError FpySequencer::op_uge() {
504  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
506  }
507  U64 rhs = this->m_runtime.stack.pop<U64>();
508  U64 lhs = this->m_runtime.stack.pop<U64>();
509  this->m_runtime.stack.push(static_cast<U8>(lhs >= rhs));
511 }
512 DirectiveError FpySequencer::op_slt() {
513  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
515  }
516  I64 rhs = this->m_runtime.stack.pop<I64>();
517  I64 lhs = this->m_runtime.stack.pop<I64>();
518  this->m_runtime.stack.push(static_cast<U8>(lhs < rhs));
520 }
521 DirectiveError FpySequencer::op_sle() {
522  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
524  }
525  I64 rhs = this->m_runtime.stack.pop<I64>();
526  I64 lhs = this->m_runtime.stack.pop<I64>();
527  this->m_runtime.stack.push(static_cast<U8>(lhs <= rhs));
529 }
530 DirectiveError FpySequencer::op_sgt() {
531  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
533  }
534  I64 rhs = this->m_runtime.stack.pop<I64>();
535  I64 lhs = this->m_runtime.stack.pop<I64>();
536  this->m_runtime.stack.push(static_cast<U8>(lhs > rhs));
538 }
539 DirectiveError FpySequencer::op_sge() {
540  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
542  }
543  I64 rhs = this->m_runtime.stack.pop<I64>();
544  I64 lhs = this->m_runtime.stack.pop<I64>();
545  this->m_runtime.stack.push(static_cast<U8>(lhs >= rhs));
547 }
548 DirectiveError FpySequencer::op_feq() {
549  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
551  }
552  F64 rhs = this->m_runtime.stack.pop<F64>();
553  F64 lhs = this->m_runtime.stack.pop<F64>();
554  // eq is true if they are equal and neither is nan
555  this->m_runtime.stack.push(static_cast<U8>((lhs == rhs) ? 1 : 0));
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) ? 1 : 0));
567 }
568 DirectiveError FpySequencer::op_flt() {
569  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
571  }
572  F64 rhs = this->m_runtime.stack.pop<F64>();
573  F64 lhs = this->m_runtime.stack.pop<F64>();
574  this->m_runtime.stack.push(static_cast<U8>(std::isless(lhs, rhs)));
576 }
577 DirectiveError FpySequencer::op_fle() {
578  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
580  }
581  F64 rhs = this->m_runtime.stack.pop<F64>();
582  F64 lhs = this->m_runtime.stack.pop<F64>();
583  this->m_runtime.stack.push(static_cast<U8>(std::islessequal(lhs, rhs)));
585 }
586 DirectiveError FpySequencer::op_fgt() {
587  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
589  }
590  F64 rhs = this->m_runtime.stack.pop<F64>();
591  F64 lhs = this->m_runtime.stack.pop<F64>();
592  this->m_runtime.stack.push(static_cast<U8>(std::isgreater(lhs, rhs)));
594 }
595 DirectiveError FpySequencer::op_fge() {
596  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
598  }
599  F64 rhs = this->m_runtime.stack.pop<F64>();
600  F64 lhs = this->m_runtime.stack.pop<F64>();
601  this->m_runtime.stack.push(static_cast<U8>(std::isgreaterequal(lhs, rhs)));
603 }
604 DirectiveError FpySequencer::op_not() {
605  if (this->m_runtime.stack.size < sizeof(U8)) {
607  }
608  this->m_runtime.stack.push(static_cast<U8>(this->m_runtime.stack.pop<U8>() == 0));
610 }
611 DirectiveError FpySequencer::op_fpext() {
612  // convert F32 to F64
613  if (this->m_runtime.stack.size < sizeof(F32)) {
615  }
616  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<F32>()));
618 }
619 DirectiveError FpySequencer::op_fptrunc() {
620  // convert F64 to F32
621  if (this->m_runtime.stack.size < sizeof(F64)) {
623  }
624  this->m_runtime.stack.push(static_cast<F32>(this->m_runtime.stack.pop<F64>()));
626 }
627 DirectiveError FpySequencer::op_fptosi() {
628  if (this->m_runtime.stack.size < sizeof(F64)) {
630  }
631  this->m_runtime.stack.push(static_cast<I64>(this->m_runtime.stack.pop<F64>()));
633 }
634 DirectiveError FpySequencer::op_sitofp() {
635  if (this->m_runtime.stack.size < sizeof(I64)) {
637  }
638  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<I64>()));
640 }
641 DirectiveError FpySequencer::op_fptoui() {
642  if (this->m_runtime.stack.size < sizeof(F64)) {
644  }
645  this->m_runtime.stack.push(static_cast<U64>(this->m_runtime.stack.pop<F64>()));
647 }
648 DirectiveError FpySequencer::op_uitofp() {
649  if (this->m_runtime.stack.size < sizeof(U64)) {
651  }
652  this->m_runtime.stack.push(static_cast<F64>(this->m_runtime.stack.pop<U64>()));
654 }
655 DirectiveError FpySequencer::op_add() {
656  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
658  }
659  I64 rhs = this->m_runtime.stack.pop<I64>();
660  I64 lhs = this->m_runtime.stack.pop<I64>();
661  // Check for overflow and underflow and return the appropriate error code
662  // Overflow can only occur with both operands positive and occurs when one operand is greater than the maximum value
663  // less the other operand. If either operand is negative or zero, overflow cannot occur.
664  if ((rhs > 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() - rhs) < lhs)) {
666  }
667  // Underflow can only occur with both operands negative and occurs when one operand is less than the minimum value
668  // minus the other operand. If either operand is positive or zero, underflow cannot occur.
669  else if ((rhs < 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() - rhs) > lhs)) {
671  }
672  this->m_runtime.stack.push(static_cast<I64>(lhs + rhs));
674 }
675 DirectiveError FpySequencer::op_sub() {
676  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
678  }
679  I64 rhs = this->m_runtime.stack.pop<I64>();
680  I64 lhs = this->m_runtime.stack.pop<I64>();
681  // Check for overflow and underflow and return the appropriate error code
682  // Overflow can only occur when the left operand is positive and the right operand is negative. It occurs when the
683  // left (positive) operand is greater than the maximum value plus the other (negative) operand. If the right
684  // operand is positive or zero, overflow cannot occur.
685  if ((rhs < 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() + rhs) < lhs)) {
687  }
688  // Underflow can only occur when the left operand is negative and the right operand is positive. It occurs when the
689  // left (negative) operand is less than the minimum value plus the other (positive) operand. If the right operand
690  // is negative or zero, underflow cannot occur.
691  else if ((rhs > 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() + rhs) > lhs)) {
693  }
694  this->m_runtime.stack.push(static_cast<I64>(lhs - rhs));
696 }
697 DirectiveError FpySequencer::op_mul() {
698  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
700  }
701  I64 rhs = this->m_runtime.stack.pop<I64>();
702  I64 lhs = this->m_runtime.stack.pop<I64>();
703  // Check for overflow and underflow and return the appropriate error code
704  // Overflow can only occur with operands of matching signs and occurs when one operand is greater (or less) than the
705  // maximum value divided by the other operand. Either operand being zero precludes overflow.
706  // Check the both positive case.
707  if ((rhs > 0) && (lhs > 0) && ((std::numeric_limits<I64>::max() / rhs) < lhs)) {
709  }
710  // Check the both negative case
711  else if ((rhs < 0) && (lhs < 0) && ((std::numeric_limits<I64>::max() / (-1 * rhs)) < (-1 * lhs))) {
713  }
714  // Underflow can occur with operands of differing signs and occurs when one operand is less than the minimum value
715  // divided by the other operand. Either operand being zero precludes underflow.
716  // Check the case where lhs is positive.
717  else if ((rhs < 0) && (lhs > 0) && ((std::numeric_limits<I64>::min() / lhs) > rhs)) {
719  }
720  // Check the case where rhs is positive.
721  else if ((rhs > 0) && (lhs < 0) && ((std::numeric_limits<I64>::min() / rhs) > lhs)) {
723  }
724  this->m_runtime.stack.push(static_cast<I64>(lhs * rhs));
726 }
727 DirectiveError FpySequencer::op_udiv() {
728  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
730  }
731  U64 rhs = this->m_runtime.stack.pop<U64>();
732  U64 lhs = this->m_runtime.stack.pop<U64>();
733  // Prevent division by zero
734  if (rhs == 0) {
736  }
737  this->m_runtime.stack.push(static_cast<U64>(lhs / rhs));
739 }
740 DirectiveError FpySequencer::op_sdiv() {
741  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
743  }
744 
745  I64 rhs = this->m_runtime.stack.pop<I64>();
746  I64 lhs = this->m_runtime.stack.pop<I64>();
747  // Prevent division by zero
748  if (rhs == 0) {
750  }
751  this->m_runtime.stack.push(static_cast<I64>(lhs / rhs));
753 }
754 DirectiveError FpySequencer::op_umod() {
755  if (this->m_runtime.stack.size < sizeof(U64) * 2) {
757  }
758  U64 rhs = this->m_runtime.stack.pop<U64>();
759  if (rhs == 0) {
761  }
762  U64 lhs = this->m_runtime.stack.pop<U64>();
763  this->m_runtime.stack.push(static_cast<U64>(lhs % rhs));
765 }
766 DirectiveError FpySequencer::op_smod() {
767  if (this->m_runtime.stack.size < sizeof(I64) * 2) {
769  }
770  I64 rhs = this->m_runtime.stack.pop<I64>();
771  if (rhs == 0) {
773  }
774  I64 lhs = this->m_runtime.stack.pop<I64>();
775  I64 res = static_cast<I64>(lhs % rhs);
776  // in order to match Python's behavior,
777  // if the signs of the remainder and divisor differ, adjust the result.
778  // this happens when the result should be positive but is negative, or vice-versa.
779  // credit Gemini 2.5 pro
780  if ((res > 0 && rhs < 0) || (res < 0 && rhs > 0)) {
781  res += rhs;
782  }
783  this->m_runtime.stack.push(res);
785 }
786 DirectiveError FpySequencer::op_fadd() {
787  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
789  }
790  F64 rhs = this->m_runtime.stack.pop<F64>();
791  F64 lhs = this->m_runtime.stack.pop<F64>();
792  this->m_runtime.stack.push(static_cast<F64>(lhs + rhs));
794 }
795 DirectiveError FpySequencer::op_fsub() {
796  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
798  }
799  F64 rhs = this->m_runtime.stack.pop<F64>();
800  F64 lhs = this->m_runtime.stack.pop<F64>();
801  this->m_runtime.stack.push(static_cast<F64>(lhs - rhs));
803 }
804 DirectiveError FpySequencer::op_fmul() {
805  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
807  }
808  F64 rhs = this->m_runtime.stack.pop<F64>();
809  F64 lhs = this->m_runtime.stack.pop<F64>();
810  this->m_runtime.stack.push(static_cast<F64>(lhs * rhs));
812 }
813 DirectiveError FpySequencer::op_fdiv() {
814  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
816  }
817  F64 rhs = this->m_runtime.stack.pop<F64>();
818  F64 lhs = this->m_runtime.stack.pop<F64>();
819  this->m_runtime.stack.push(static_cast<F64>(lhs / rhs));
821 }
822 DirectiveError FpySequencer::op_fpow() {
823  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
825  }
826  F64 rhs = this->m_runtime.stack.pop<F64>();
827  F64 lhs = this->m_runtime.stack.pop<F64>();
828  this->m_runtime.stack.push(static_cast<F64>(pow(lhs, rhs)));
830 }
831 DirectiveError FpySequencer::op_flog() {
832  if (this->m_runtime.stack.size < sizeof(F64)) {
834  }
835  F64 val = this->m_runtime.stack.pop<F64>();
836  if (val <= 0.0) {
838  }
839  this->m_runtime.stack.push(static_cast<F64>(log(val)));
841 }
842 DirectiveError FpySequencer::op_fmod() {
843  if (this->m_runtime.stack.size < sizeof(F64) * 2) {
845  }
846  F64 rhs = this->m_runtime.stack.pop<F64>();
847  if (rhs == 0.0) {
849  }
850  F64 lhs = this->m_runtime.stack.pop<F64>();
851  this->m_runtime.stack.push(static_cast<F64>(lhs - rhs * std::floor(lhs / rhs)));
853 }
854 DirectiveError FpySequencer::op_siext_8_64() {
855  if (this->m_runtime.stack.size < sizeof(I8)) {
857  }
858  I8 src = this->m_runtime.stack.pop<I8>();
859  this->m_runtime.stack.push(static_cast<I64>(src));
861 }
862 DirectiveError FpySequencer::op_siext_16_64() {
863  if (this->m_runtime.stack.size < sizeof(I16)) {
865  }
866  I16 src = this->m_runtime.stack.pop<I16>();
867  this->m_runtime.stack.push(static_cast<I64>(src));
869 }
870 DirectiveError FpySequencer::op_siext_32_64() {
871  if (this->m_runtime.stack.size < sizeof(I32)) {
873  }
874  I32 src = this->m_runtime.stack.pop<I32>();
875  this->m_runtime.stack.push(static_cast<I64>(src));
877 }
878 DirectiveError FpySequencer::op_ziext_8_64() {
879  if (this->m_runtime.stack.size < sizeof(U8)) {
881  }
882  U8 src = this->m_runtime.stack.pop<U8>();
883  this->m_runtime.stack.push(static_cast<U64>(src));
885 }
886 DirectiveError FpySequencer::op_ziext_16_64() {
887  if (this->m_runtime.stack.size < sizeof(U16)) {
889  }
890  U16 src = this->m_runtime.stack.pop<U16>();
891  this->m_runtime.stack.push(static_cast<U64>(src));
893 }
894 DirectiveError FpySequencer::op_ziext_32_64() {
895  if (this->m_runtime.stack.size < sizeof(U32)) {
897  }
898  U32 src = this->m_runtime.stack.pop<U32>();
899  this->m_runtime.stack.push(static_cast<U64>(src));
901 }
902 DirectiveError FpySequencer::op_itrunc_64_8() {
903  if (this->m_runtime.stack.size < sizeof(U64)) {
905  }
906  U64 src = this->m_runtime.stack.pop<U64>();
907  this->m_runtime.stack.push(static_cast<U8>(src));
909 }
910 DirectiveError FpySequencer::op_itrunc_64_16() {
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<U16>(src));
917 }
918 DirectiveError FpySequencer::op_itrunc_64_32() {
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<U32>(src));
925 }
926 Signal FpySequencer::stackOp_directiveHandler(const FpySequencer_StackOpDirective& directive, DirectiveError& error) {
927  // coding error, should not have gotten to this stack op handler
928  FW_ASSERT(directive.get__op() >= Fpy::DirectiveId::OR && directive.get__op() <= Fpy::DirectiveId::ITRUNC_64_32,
929  static_cast<FwAssertArgType>(directive.get__op()));
930 
931  switch (directive.get__op()) {
933  error = this->op_or();
934  break;
936  error = this->op_and();
937  break;
939  error = this->op_ieq();
940  break;
942  error = this->op_ine();
943  break;
945  error = this->op_ult();
946  break;
948  error = this->op_ule();
949  break;
951  error = this->op_ugt();
952  break;
954  error = this->op_uge();
955  break;
957  error = this->op_slt();
958  break;
960  error = this->op_sle();
961  break;
963  error = this->op_sgt();
964  break;
966  error = this->op_sge();
967  break;
969  error = this->op_feq();
970  break;
972  error = this->op_fne();
973  break;
975  error = this->op_flt();
976  break;
978  error = this->op_fle();
979  break;
981  error = this->op_fgt();
982  break;
984  error = this->op_fge();
985  break;
987  error = this->op_not();
988  break;
990  error = this->op_fpext();
991  break;
993  error = this->op_fptrunc();
994  break;
996  error = this->op_fptosi();
997  break;
999  error = this->op_fptoui();
1000  break;
1002  error = this->op_sitofp();
1003  break;
1005  error = this->op_uitofp();
1006  break;
1007  case Fpy::DirectiveId::ADD:
1008  error = this->op_add();
1009  break;
1010  case Fpy::DirectiveId::SUB:
1011  error = this->op_sub();
1012  break;
1013  case Fpy::DirectiveId::MUL:
1014  error = this->op_mul();
1015  break;
1017  error = this->op_udiv();
1018  break;
1020  error = this->op_sdiv();
1021  break;
1023  error = this->op_umod();
1024  break;
1026  error = this->op_smod();
1027  break;
1029  error = this->op_fadd();
1030  break;
1032  error = this->op_fsub();
1033  break;
1035  error = this->op_fmul();
1036  break;
1038  error = this->op_fdiv();
1039  break;
1041  error = this->op_fpow();
1042  break;
1044  error = this->op_flog();
1045  break;
1047  error = this->op_fmod();
1048  break;
1050  error = this->op_siext_8_64();
1051  break;
1053  error = this->op_siext_16_64();
1054  break;
1056  error = this->op_siext_32_64();
1057  break;
1059  error = this->op_ziext_8_64();
1060  break;
1062  error = this->op_ziext_16_64();
1063  break;
1065  error = this->op_ziext_32_64();
1066  break;
1068  error = this->op_itrunc_64_8();
1069  break;
1071  error = this->op_itrunc_64_16();
1072  break;
1074  error = this->op_itrunc_64_32();
1075  break;
1076  default:
1077  FW_ASSERT(0, directive.get__op());
1078  break;
1079  }
1080  if (error != DirectiveError::NO_ERROR) {
1082  }
1084 }
1085 
1086 Signal FpySequencer::exit_directiveHandler(const FpySequencer_ExitDirective& directive, DirectiveError& error) {
1087  if (this->m_runtime.stack.size < 1) {
1090  }
1091  U8 errorCode = this->m_runtime.stack.pop<U8>();
1092  // exit(0), no error
1093  if (errorCode == 0) {
1094  // just goto the end of the sequence
1095  this->m_runtime.nextStatementIndex = this->m_sequenceObj.get_header().get_statementCount();
1097  }
1098  // otherwise, kill the sequence here
1099  // raise the user defined error code as an event
1100  this->log_WARNING_HI_SequenceExitedWithError(this->m_sequenceFilePath, errorCode);
1103 }
1104 
1105 Signal FpySequencer::allocate_directiveHandler(const FpySequencer_AllocateDirective& directive, DirectiveError& error) {
1106  if (directive.get_size() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1109  }
1110  this->m_runtime.stack.pushZeroes(directive.get_size());
1112 }
1113 
1115 Signal FpySequencer::storeHelper(Fpy::StackSizeType destOffset, Fpy::StackSizeType size, DirectiveError& error) {
1116  if (this->m_runtime.stack.size < size) {
1119  }
1120  // After popping the value, would the write go out of bounds?
1121  Fpy::StackSizeType newStackSize = this->m_runtime.stack.size - size;
1122  // Overflow-safe check: destOffset + size > newStackSize
1123  // Rewritten as: check destOffset <= newStackSize first, then size > newStackSize - destOffset
1124  if (destOffset > newStackSize || size > newStackSize - destOffset) {
1127  }
1128  // Copy value to the destination location
1129  this->m_runtime.stack.copy(destOffset, this->m_runtime.stack.size - size, size);
1130  this->m_runtime.stack.size = newStackSize;
1132 }
1133 
1135 Signal FpySequencer::loadHelper(Fpy::StackSizeType srcOffset, Fpy::StackSizeType size, DirectiveError& error) {
1136  if (size > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1139  }
1140  // Overflow-safe check: srcOffset + size > stack.size
1141  // Rewritten as: check srcOffset <= stack.size first, then size > stack.size - srcOffset
1142  if (srcOffset > this->m_runtime.stack.size || size > this->m_runtime.stack.size - srcOffset) {
1145  }
1146  // Copy from source location to top of stack
1147  this->m_runtime.stack.copy(this->m_runtime.stack.size, srcOffset, size);
1148  this->m_runtime.stack.size += size;
1150 }
1151 
1152 Signal FpySequencer::storeRelConstOffset_directiveHandler(const FpySequencer_StoreRelConstOffsetDirective& directive,
1153  DirectiveError& error) {
1154  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + directive.get_lvarOffset();
1155  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1158  }
1159  return this->storeHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1160 }
1161 
1162 Signal FpySequencer::loadRel_directiveHandler(const FpySequencer_LoadRelDirective& directive, DirectiveError& error) {
1163  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + directive.get_lvarOffset();
1164  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1167  }
1168  return this->loadHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1169 }
1170 
1171 Signal FpySequencer::pushVal_directiveHandler(const FpySequencer_PushValDirective& directive, DirectiveError& error) {
1172  if (directive.get__valSize() > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1175  }
1176  // copy from the bytearray in the directive to the stack, add to stack size.
1177  this->m_runtime.stack.push(const_cast<U8*>(directive.get_val()),
1178  static_cast<Fpy::StackSizeType>(directive.get__valSize()));
1180 }
1181 
1182 Signal FpySequencer::discard_directiveHandler(const FpySequencer_DiscardDirective& directive, DirectiveError& error) {
1183  if (this->m_runtime.stack.size < directive.get_size()) {
1186  }
1187  // drop the specified amount of bytes off the stack. simple as.
1188  this->m_runtime.stack.size -= directive.get_size();
1190 }
1191 
1192 Signal FpySequencer::memCmp_directiveHandler(const FpySequencer_MemCmpDirective& directive, DirectiveError& error) {
1193  // Overflow-safe check: we need size * 2 bytes on the stack
1194  // First check that size * 2 doesn't overflow: size > MAX/2 would overflow
1195  // MAX_STACK_SIZE is the upper bound for stack.size, so if size > MAX_STACK_SIZE/2, we definitely don't have enough
1196  if (directive.get_size() > Fpy::MAX_STACK_SIZE / 2) {
1199  }
1200  // Now safe to compute size * 2
1201  if (this->m_runtime.stack.size < directive.get_size() * 2) {
1204  }
1205 
1206  // find the starting offsets of the two byte arrays
1207  U64 lhsOffset = this->m_runtime.stack.size - directive.get_size() * 2;
1208  U64 rhsOffset = this->m_runtime.stack.size - directive.get_size();
1209 
1210  // "officially" remove them from the stack
1211  // you have to do this before pushing to the stack, otherwise the result would get placed
1212  // after the byte arrays
1213  this->m_runtime.stack.size -= directive.get_size() * 2;
1214 
1215  // memcmp the two byte arrays, push 1 if they were equal, 0 otherwise
1216  if (memcmp(this->m_runtime.stack.bytes + lhsOffset, this->m_runtime.stack.bytes + rhsOffset,
1217  directive.get_size()) == 0) {
1218  this->m_runtime.stack.push<U8>(1);
1219  } else {
1220  this->m_runtime.stack.push<U8>(0);
1221  }
1223 }
1224 
1225 Signal FpySequencer::stackCmd_directiveHandler(const FpySequencer_StackCmdDirective& directive, DirectiveError& error) {
1226  // Overflow-safe check: need argsSize + sizeof(FwOpcodeType) bytes
1227  // Check stack.size >= sizeof(FwOpcodeType) first, then stack.size - sizeof(FwOpcodeType) >= argsSize
1228  if (this->m_runtime.stack.size < sizeof(FwOpcodeType) ||
1229  this->m_runtime.stack.size - sizeof(FwOpcodeType) < directive.get_argsSize()) {
1232  }
1233 
1234  // pop the opcode of the cmd off the stack
1235  // note this means that, unlike the actual byte array that the dispatcher gets,
1236  // these cmds have opcode after the argument buffer
1237  FwOpcodeType opcode = this->m_runtime.stack.pop<FwOpcodeType>();
1238  U64 argBufOffset = this->m_runtime.stack.size - directive.get_argsSize();
1239 
1240  // update the opcode of the cmd we will await
1241  this->m_runtime.currentCmdOpcode = opcode;
1242 
1243  // also pop the args off the stack
1244  this->m_runtime.stack.size -= directive.get_argsSize();
1245 
1246  if (this->sendCmd(opcode, this->m_runtime.stack.bytes + argBufOffset, directive.get_argsSize()) ==
1249  } else {
1250  // now tell the SM to wait some more until we get the cmd response back
1251  // if we've already got the response back this should be harmless
1253  }
1254 
1256 }
1257 
1258 Signal FpySequencer::pushTime_directiveHandler(const FpySequencer_PushTimeDirective& directive, DirectiveError& error) {
1259  if (Fpy::MAX_STACK_SIZE - Fw::Time::SERIALIZED_SIZE < this->m_runtime.stack.size) {
1262  }
1263 
1264  Fw::Time currentTime = this->getTime();
1265 
1266  U8 currentTimeBuf[Fw::Time::SERIALIZED_SIZE] = {};
1267  Fw::ExternalSerializeBuffer timeEsb(currentTimeBuf, Fw::Time::SERIALIZED_SIZE);
1268  Fw::SerializeStatus stat = timeEsb.serializeFrom(currentTime);
1269 
1270  // coding error if this failed, we should have enough space
1271  FW_ASSERT(stat == Fw::SerializeStatus::FW_SERIALIZE_OK, static_cast<FwAssertArgType>(stat));
1272 
1273  // push time to end of stack
1274  this->m_runtime.stack.push(timeEsb.getBuffAddr(), static_cast<Fpy::StackSizeType>(timeEsb.getSize()));
1276 }
1277 
1278 Signal FpySequencer::setFlag_directiveHandler(const FpySequencer_SetFlagDirective& directive, DirectiveError& error) {
1279  if (this->m_runtime.stack.size < 1) {
1282  }
1283  if (directive.get_flagIdx() >= Fpy::FLAG_COUNT) {
1286  }
1287 
1288  // 1 if the stack bool is nonzero, 0 otherwise
1289  U8 flagVal = this->m_runtime.stack.pop<U8>() != 0;
1290 
1291  this->m_runtime.flags[directive.get_flagIdx()] = flagVal == 1;
1293 }
1294 
1295 Signal FpySequencer::getFlag_directiveHandler(const FpySequencer_GetFlagDirective& directive, DirectiveError& error) {
1296  if (Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size < 1) {
1299  }
1300  if (directive.get_flagIdx() >= Fpy::FLAG_COUNT) {
1303  }
1304 
1305  bool flagVal = this->m_runtime.flags[directive.get_flagIdx()];
1306  this->m_runtime.stack.push<U8>(flagVal);
1308 }
1309 
1310 Signal FpySequencer::getField_directiveHandler(const FpySequencer_GetFieldDirective& directive, DirectiveError& error) {
1311  // Need sizeof(StackSizeType) for the offset AND parentSize for the parent data
1312  // Check we have enough for the offset first
1313  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType)) {
1316  }
1317  // After popping the offset, we need at least parentSize bytes remaining
1318  if (this->m_runtime.stack.size - sizeof(Fpy::StackSizeType) < directive.get_parentSize()) {
1321  }
1322 
1323  Fpy::StackSizeType offset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1324 
1325  // Overflow-safe check: offset + memberSize > parentSize
1326  // Rewritten as: check offset <= parentSize first, then memberSize > parentSize - offset
1327  if (offset > directive.get_parentSize() || directive.get_memberSize() > directive.get_parentSize() - offset) {
1328  // i think it's somewhat ambiguous whether this is a stack access out of bounds
1329  // but there isn't really an error code that better reflects this, and i guess
1330  // it's technically true
1333  }
1334 
1335  // the resulting bytes should move to the start of the parent array
1336 
1337  // Calculate the offset of the parent start in the stack
1338  Fpy::StackSizeType parentStartOffset = this->m_runtime.stack.size - directive.get_parentSize();
1339  // Overflow-safe: parentStartOffset + offset cannot overflow since offset <= parentSize
1340  // and parentStartOffset + parentSize == stack.size (which is bounded)
1341  this->m_runtime.stack.move(parentStartOffset, parentStartOffset + offset, directive.get_memberSize());
1342  // adjust stack size by the diff between the member and the parent
1343  this->m_runtime.stack.size -= (directive.get_parentSize() - directive.get_memberSize());
1345 }
1346 
1347 Signal FpySequencer::peek_directiveHandler(const FpySequencer_PeekDirective& directive, DirectiveError& error) {
1348  // must have at least two StackSizeType on stack
1349  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) * 2) {
1352  }
1353 
1354  Fpy::StackSizeType offset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1355  Fpy::StackSizeType byteCount = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1356 
1357  // Check offset doesn't exceed stack size (after both pops)
1358  if (offset > this->m_runtime.stack.size) {
1359  // would access past the bottom of the stack
1360  // note we allow the equals case because the byteCount might be 0
1363  }
1364  if (byteCount > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1365  // we would overflow the stack if we pushed this many bytes to it
1368  }
1369  // Overflow-safe check: byteCount + offset > stack.size
1370  // Rewritten as: check offset <= stack.size (done above), then byteCount > stack.size - offset
1371  if (byteCount > this->m_runtime.stack.size - offset) {
1372  // would access past the bottom of the stack
1375  }
1376  // start copying from the lowest byte of the src array
1377  U8* src = this->m_runtime.stack.top() - offset - byteCount;
1378  this->m_runtime.stack.push(src, byteCount);
1380 }
1381 
1382 Signal FpySequencer::storeRel_directiveHandler(const FpySequencer_StoreRelDirective& directive, DirectiveError& error) {
1383  // Need enough bytes for the value and the offset (SignedStackSizeType = 4 bytes)
1384  // Overflow-safe: check stack.size >= sizeof(SignedStackSizeType) first, then stack.size -
1385  // sizeof(SignedStackSizeType) >= size
1386  if (this->m_runtime.stack.size < sizeof(Fpy::SignedStackSizeType) ||
1387  this->m_runtime.stack.size - sizeof(Fpy::SignedStackSizeType) < directive.get_size()) {
1390  }
1391 
1392  // Pop the signed offset from the stack
1393  Fpy::SignedStackSizeType lvarOffset = this->m_runtime.stack.pop<Fpy::SignedStackSizeType>();
1394 
1395  I64 addr = static_cast<I64>(this->m_runtime.stack.currentFrameStart) + lvarOffset;
1396  if (addr < 0 || addr > Fpy::MAX_STACK_SIZE) {
1399  }
1400  return this->storeHelper(static_cast<Fpy::StackSizeType>(addr), directive.get_size(), error);
1401 }
1402 
1403 Signal FpySequencer::call_directiveHandler(const FpySequencer_CallDirective& directive, DirectiveError& error) {
1404  // Need at least 4 bytes for the target address
1405  if (this->m_runtime.stack.size < sizeof(U32)) {
1408  }
1409 
1410  // Pop the target directive index from the stack
1411  U32 target = this->m_runtime.stack.pop<U32>();
1412 
1413  // Check if we have space to push return address and saved frame pointer (8 bytes total)
1414  if (this->m_runtime.stack.size + sizeof(Fpy::StackSizeType) + sizeof(U32) > Fpy::MAX_STACK_SIZE) {
1417  }
1418 
1419  // Check target is within bounds (will also be checked at execution time)
1420  if (target > m_sequenceObj.get_header().get_statementCount()) {
1423  }
1424 
1425  // Save the return address (next instruction after CALL)
1426  U32 returnAddr = this->m_runtime.nextStatementIndex;
1427 
1428  // Set the next instruction to the target
1429  this->m_runtime.nextStatementIndex = target;
1430 
1431  // Push the return address to the stack
1432  this->m_runtime.stack.push<U32>(returnAddr);
1433 
1434  // Push the current frame pointer to the stack
1435  this->m_runtime.stack.push<Fpy::StackSizeType>(this->m_runtime.stack.currentFrameStart);
1436 
1437  // Set the new frame pointer to the current top of stack
1438  this->m_runtime.stack.currentFrameStart = this->m_runtime.stack.size;
1439 
1441 }
1442 
1443 Signal FpySequencer::return_directiveHandler(const FpySequencer_ReturnDirective& directive, DirectiveError& error) {
1444  Fpy::StackSizeType returnValSize = directive.get_returnValSize();
1445  Fpy::StackSizeType callArgsSize = directive.get_callArgsSize();
1446 
1447  // Check we have enough bytes for the return value
1448  if (this->m_runtime.stack.size < returnValSize) {
1451  }
1452 
1453  // returnValSize is guaranteed to be less than Fpy::MAX_STACK_SIZE because it's less than the stack.size
1454  // thus the memcpy won't fail
1455 
1456  // Save the return value if there is one
1457  U8 returnValue[Fpy::MAX_STACK_SIZE] = {};
1458  if (returnValSize > 0) {
1459  memcpy(returnValue, this->m_runtime.stack.top() - returnValSize, returnValSize);
1460  }
1461 
1462  // Truncate the stack to stack_frame_start (discard all local variables)
1463  if (this->m_runtime.stack.currentFrameStart > this->m_runtime.stack.size) {
1466  }
1467  this->m_runtime.stack.size = this->m_runtime.stack.currentFrameStart;
1468 
1469  // Check we have enough bytes for saved frame pointer and return address
1470  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) + sizeof(U32)) {
1473  }
1474 
1475  // Pop the saved frame pointer
1476  Fpy::StackSizeType savedFramePtr = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1477 
1478  // Pop the return address
1479  U32 returnAddr = this->m_runtime.stack.pop<U32>();
1480 
1481  // Restore the frame pointer
1482  if (savedFramePtr > this->m_runtime.stack.size) {
1485  }
1486  this->m_runtime.stack.currentFrameStart = savedFramePtr;
1487 
1488  // Validate the return address is within bounds
1489  if (returnAddr > m_sequenceObj.get_header().get_statementCount()) {
1492  }
1493 
1494  // Set the next instruction to the return address
1495  this->m_runtime.nextStatementIndex = returnAddr;
1496 
1497  // Check that we have enough bytes for the call arguments
1498  if (this->m_runtime.stack.size < callArgsSize) {
1501  }
1502  // Discard the function arguments
1503  this->m_runtime.stack.size -= callArgsSize;
1504 
1505  // Push the return value
1506  if (returnValSize > Fpy::MAX_STACK_SIZE - this->m_runtime.stack.size) {
1509  }
1510  this->m_runtime.stack.push(returnValue, returnValSize);
1511 
1513 }
1514 
1515 Signal FpySequencer::loadAbs_directiveHandler(const FpySequencer_LoadAbsDirective& directive, DirectiveError& error) {
1516  return this->loadHelper(directive.get_globalOffset(), directive.get_size(), error);
1517 }
1518 
1519 Signal FpySequencer::storeAbs_directiveHandler(const FpySequencer_StoreAbsDirective& directive, DirectiveError& error) {
1520  Fpy::StackSizeType size = directive.get_size();
1521 
1522  // Need enough bytes for the value and the offset
1523  // Overflow-safe: check stack.size >= sizeof(StackSizeType) first, then stack.size - sizeof >= size
1524  if (this->m_runtime.stack.size < sizeof(Fpy::StackSizeType) ||
1525  this->m_runtime.stack.size - sizeof(Fpy::StackSizeType) < size) {
1528  }
1529 
1530  // Pop the global offset from the stack
1531  Fpy::StackSizeType globalOffset = this->m_runtime.stack.pop<Fpy::StackSizeType>();
1532 
1533  return this->storeHelper(globalOffset, size, error);
1534 }
1535 
1536 Signal FpySequencer::storeAbsConstOffset_directiveHandler(const FpySequencer_StoreAbsConstOffsetDirective& directive,
1537  DirectiveError& error) {
1538  return this->storeHelper(directive.get_globalOffset(), directive.get_size(), error);
1539 }
1540 
1541 } // namespace Svc
void directive_storeRelConstOffset_internalInterfaceHandler(const Svc::FpySequencer_StoreRelConstOffsetDirective &directive) override
Internal interface handler for directive_storeRelConstOffset.
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.
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.
Fw::TlmValid getTlmChan_out(FwIndexType portNum, FwChanIdType id, Fw::Time &timeTag, Fw::TlmBuffer &val)
Invoke output port getTlmChan.
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.
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.
U8 FwTimeContextStoreType
The type used to serialize a time context value.
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 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.
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 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.
void sequencer_sendSignal_stmtResponse_beginSleep()
Send signal stmtResponse_beginSleep to state machine sequencer.
Representing failure.
void directive_getFlag_internalInterfaceHandler(const Svc::FpySequencer_GetFlagDirective &directive) override
Internal interface handler for directive_getFlag.
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
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:134
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
bool isConnected_prmGet_OutputPort(FwIndexType portNum)
void cmdOut_out(FwIndexType portNum, Fw::ComBuffer &data, U32 context)
Invoke output port cmdOut.
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 directive_storeAbs_internalInterfaceHandler(const Svc::FpySequencer_StoreAbsDirective &directive) override
Internal interface handler for directive_storeAbs.
void directive_setFlag_internalInterfaceHandler(const Svc::FpySequencer_SetFlagDirective &directive) override
Internal interface handler for directive_setFlag.
Fw::ParamValid getParam_out(FwIndexType portNum, FwPrmIdType id, Fw::ParamBuffer &val)
Invoke output port getParam.
pushes the current Fw.Time struct to the stack
pops a bool off the stack, sets a flag with a specific index to that bool
bool isConnected_getTlmChan_OutputPort(FwIndexType portNum)
called when the statement is telling the sequencer to await a later stmt response ...
gets a flag and pushes its value as a U8 to the stack
FpySequencer_SequencerStateMachineStateMachineBase::Signal Signal
#define FW_ASSERT(...)
Definition: Assert.hpp:14
Success/Failure.
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