+//------------------------------------------------------------------------
+// emitAdjustStackDepth: Adjust the current and maximum stack depth.
+//
+// Arguments:
+// ins - the instruction. Only INS_add and INS_sub adjust the stack depth.
+// It is assumed that the add/sub is on the stack pointer.
+// val - the number of bytes to add to or subtract from the stack pointer.
+//
+// Notes:
+// 1. Alters emitCurStackLvl and possibly emitMaxStackDepth.
+// 2. emitCntStackDepth must be set (0 in prolog/epilog, one DWORD elsewhere)
+//
+void emitter::emitAdjustStackDepth(instruction ins, ssize_t val)
+{
+ // If we're in the prolog or epilog, or otherwise not tracking the stack depth, just return.
+ if (emitCntStackDepth == 0)
+ return;
+
+ if (ins == INS_sub)
+ {
+ S_UINT32 newStackLvl(emitCurStackLvl);
+ newStackLvl += S_UINT32(val);
+ noway_assert(!newStackLvl.IsOverflow());
+
+ emitCurStackLvl = newStackLvl.Value();
+
+ if (emitMaxStackDepth < emitCurStackLvl)
+ emitMaxStackDepth = emitCurStackLvl;
+ }
+ else if (ins == INS_add)
+ {
+ S_UINT32 newStackLvl = S_UINT32(emitCurStackLvl) - S_UINT32(val);
+ noway_assert(!newStackLvl.IsOverflow());
+
+ emitCurStackLvl = newStackLvl.Value();
+ }