Skip to content

ZStackArray iterator and the ingamemenus.cpp blockers

Symptom

About ten functions in src/hitman3/gui/ingamemenus.cpp were kept as assembly because src/engine/zstdlib/zstackarray.h did not reproduce what the original engine/zstdlib/ztreftab.h inlined into them:

  • ZActionMenuBase::SetModified and every loop over m_ActionEntrys or m_ContainerEntrys kept the loop iterator in a stack slot (sw $13, 0($sp), reloaded after the body). With typedef T *iterator GCC keeps it in a register.
  • ZContainerMenu::AddEntry inlined ZStackArrayInsert::Add; the original calls the out-of-line instance.
  • ZContainerMenu::Draw and ZActionMenuBase::Draw test the iterator for null before the first element copy.
  • ZActionMenuBase::SetVisibility matched as a body, but defining it made GCC emit _ZTV15ZActionMenuBase and weak ZActionMenuBase D0/D1 destructors, which the reference does not contain, so the unit was rejected.
  • ZPickupMenu::UpdateEntryList and ZContainerMenu::FrameUpdate (a listed near-miss at score 29) remove all entries but one in a loop whose counter GCC reversed.

The earlier worker notes asked for ZStackArrayInsert insert and remove inlines. Those turned out to be present and correct already: Add(const T &, S) and Remove(int32) in zstackarray.h produce the original bytes. The missing piece was the iterator, plus three source-level details described below.

Diagnosis

The iterator is a class

The debug dump for this unit (analysis/uncc/hitman3/gui/ingamemenus.cpp) and for zhitman3.cpp, infodisplay.cpp and hm3cameraeventcameraclass.cpp records ZStackArray<N, T>::iterator as

struct iterator {
    T *m_pCurrent;
    bool operator!=(const iterator &) const;
    bool operator==(const iterator &) const;
    operator T *() const;
    iterator operator++(int);
    iterator &operator++();
};

with no operator-> and no operator*. tools/debug_context.py on the loops shows the inlined ztreftab.h lines 110 (!=), 126 (++), 135/137/138 (begin/end), 116 and 143 around every walk, and names the local iterator it with a stack slot (sp+0x0 in SetModified, sp+0x60 in ZContainerMenu::Draw). Because operator!= takes const iterator &, GCC 3.2 keeps the object in memory, which is the stack slot the original shows.

The default constructor zeroes m_pCurrent. With an empty default constructor, ZOldViewBase::LocateRoomsAndExits (which declares iterator it; at the top and assigns it later) swapped $s1 and $s3; with m_pCurrent(0) the dead store changes pseudo order and the function is exact again. The postfix ++ copies *this into a local and returns it; return iterator(m_pCurrent++) broke SetModified (score 90). A user-written copy constructor broke all three test functions.

Since the class has no operator->, call sites use (*it).field or T *pEntry = it;. Five existing call sites were rewritten that way.

Add is called, not inlined, because of first-use order

GCC 3.2 inlines a header template member at its first use in a unit and calls the out-of-line copy afterwards. ZContainerMenu::PostInit2 comes before AddEntry and inlines Add (its line table shows ztreftab.h 157..174). Once PostInit2 was C, AddEntry called Add as the original does. No header change was needed for this. The same rule explains why ZActionMenuBase::PostInit2 inlines allocator<char>::deallocate and later functions call it.

The null test in Draw is in the source

In ZActionMenuBase::Draw the element copied before the null test is used by the else branch that draws only the first entry while the menu is closed, so the test guards real code and is not compiler-generated. if (it) before the first *it copy reproduces both Draw functions exactly.

SetVisibility and the key function

GCC 3.2 picks the key function as the first virtual, non-pure, non-inline member in declaration order, at the end of the translation unit. A test with UpdateEntryList made non-pure and declared after SetVisibility still emitted the vtable; declared first, it did not. The reference has no _ZTV15ZActionMenuBase at all, so the original's key function was a virtual that is declared and never defined. UpdateEntryList, CheckSetRemainingActions and IgnoreAction are the only ZActionMenuBase virtuals without debug parameter names (no definition) and both derived menus override all three. UpdateEntryList is now declared first as a non-pure virtual. Defining GetEventPriority inline after the class does not help: GCC 3.2 then skips it and SetVisibility becomes the key function again.

Other details found on the way

  • Removing all but the last entry matches when the count is read into a signed local inside the Count() > 1 block: if (Count() > 1) { int32 lNumEntries = Count(); for (int32 i = 0; i < lNumEntries - 1; i++) ... }. A uint32 count read before the test lets GCC reverse the loop.
  • ZAction has an inline GetPriority() (action.h line 321 in the original line table, right after GetType()); it is added.
  • In ZActionMenu::CheckSetRemainingActions both walks over the local rNearItems go through prtActions = &rNearItems with if (prtActions) guards; the guards are what keep the original beqz $s3.
  • ZPickupMenu::DecideActionText declares szLocale after the clothes branch (the clothes temporary sits below it in the frame), and starts with an inlined GetHitman() whose result is unused. The line table records hm3gamedata.h line 152 at the first source line and the lui of g_pGameData it leaves in $s4 is reused by the later ZStringToUpper calls. The debug record names no local for it, so the source writes it as a discarded call, not a dead local. This needs a reviewer's decision.

Header change

src/engine/zstdlib/zstackarray.h:

-   typedef T *iterator;
+   struct iterator {
+       T *m_pCurrent;
+
+       iterator() : m_pCurrent(0) {}
+       iterator(T *pCurrent) : m_pCurrent(pCurrent) {}
+       bool operator!=(const iterator &it) const { return m_pCurrent != it.m_pCurrent; }
+       bool operator==(const iterator &it) const { return m_pCurrent == it.m_pCurrent; }
+       operator T *() const { return m_pCurrent; }
+       iterator operator++(int) {
+           iterator it = *this;
+           m_pCurrent++;
+           return it;
+       }
+       iterator &operator++() {
+           m_pCurrent++;
+           return *this;
+       }
+   };

begin() and end() are unchanged and convert through iterator(T *).

Other header edits:

  • src/hitman3/gui/ingamemenus.h: ZActionMenuBase declares virtual void UpdateEntryList(); first (was pure, declared after SetVisibility); ZActionMenuBase::Draw, ZContainerMenu::Draw and ZPickupMenu::DecideActionText are declared.
  • src/engine/eventsbase/action.h: int32 GetPriority() const.

Call sites changed from it-> to (*it). in src/engine/drawing/old/zoldviewbase.cpp (LocateRoomsAndExits) and in drafts in src/hitman3/characters/zhitman3.cpp and src/hitman3/gui/ingamemenus.cpp.

Functions now matching

src/hitman3/gui/ingamemenus.cpp:

  • ZActionMenuBase::PostInit2 (ob_003acc5c)
  • ZActionMenuBase::Draw (ob_003ad09c)
  • ZActionMenuBase::SetModified (ob_003ad464)
  • ZActionMenuBase::GetFocused (ob_003ad88c)
  • ZActionMenuBase::SetVisibility (ob_003adac8)
  • ZActionMenu::CheckSetRemainingActions (ob_003af50c)
  • ZPickupMenu::UpdateEntryList (ob_003afdf8)
  • ZPickupMenu::CheckSetRemainingActions (ob_003b004c)
  • ZPickupMenu::DecideActionText (ob_003b04b0)
  • ZContainerMenu::PostInit2 (ob_003b1018)
  • ZContainerMenu::Draw (ob_003b1404)
  • ZContainerMenu::AddEntry (ob_003b1620)
  • ZContainerMenu::FrameUpdate (ob_003b1b44)
  • ZContainerMenu::GetFocused (ob_003b1d5c)

src/hitman3/characters/zhitman3.cpp drafts that the class iterator makes exact:

  • ZHitman3::DualWeaponsDestroyWeapons (ob_0031fdc8)
  • ZHitman3::DualWeaponsGetAmmoForWeapon (ob_0031ffa0)
  • ZHitman3::InvalidateAttachedItems (ob_0032e35c)

ZOldViewBase::LocateRoomsAndExits stays exact.