From 642cf40412ab2cd5ccce173f6ec6ad0ece195436 Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Fri, 9 Oct 2026 12:38:29 +0000 Subject: [PATCH] decomp(inputform): match LayoutByNW4R::create (tiInputForm 221/221 exact) (worker f17f914e) --- include/keyboard/tiInputForm.h | 3 +- src/keyboard/tiInputForm.cpp | 130 ++++++++++---------- tools/decomp-assist/o3-keyboard.attempts.md | 54 ++++++++ 3 files changed, 122 insertions(+), 65 deletions(-) diff --git a/include/keyboard/tiInputForm.h b/include/keyboard/tiInputForm.h index e3bc8666..3daa84fb 100644 --- a/include/keyboard/tiInputForm.h +++ b/include/keyboard/tiInputForm.h @@ -30,7 +30,6 @@ namespace textinput { class Base; class EventHandler; class LayoutByNW4R; - struct InputFormAnimationFile; #endif class EditBuffer { #ifdef TIMANAGER_IMPLEMENTATION @@ -357,7 +356,7 @@ namespace textinput { #if defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) friend class EventHandler; inline const nw4r::lyt::Pane* getLanguageTextPane() const; - inline void addButtonAnimation(MEMAllocator*, nw4rmanager::AnmPane*, const InputFormAnimationFile* const&, const char*); + inline nw4r::lyt::TextBox* getTextBox() const; #endif public: #if defined(MYTIMANAGER_IMPLEMENTATION) || defined(TIINPUTFORM_IMPLEMENTATION) || defined(TIMANAGER_IMPLEMENTATION) diff --git a/src/keyboard/tiInputForm.cpp b/src/keyboard/tiInputForm.cpp index b430db61..aaa4418a 100644 --- a/src/keyboard/tiInputForm.cpp +++ b/src/keyboard/tiInputForm.cpp @@ -417,7 +417,7 @@ struct ButtonAnimations { const InputFormAnimationFile* files[12]; }; -ButtonAnimations csButtonAnimations[] = { +const ButtonAnimations csButtonAnimations[] = { {KT_NormalButton, scP_txtScrll_UP, 8, NULL, { &csAninationFile__Q29textinput9inputform[0], &csAninationFile__Q29textinput9inputform[1], &csAninationFile__Q29textinput9inputform[2], &csAninationFile__Q29textinput9inputform[3], @@ -1115,29 +1115,14 @@ static inline const char* selectInputTextName(const nw4rmanager::Layout& layout, return "T_2l_TextBox"; } -inline void LayoutByNW4R::addButtonAnimation(MEMAllocator* allocator, nw4rmanager::AnmPane* animationPane, const InputFormAnimationFile* const& file, const char* bindingName) { - void* resource = mpMultiArcResourceAccessor->GetResource(0, file->fileName, NULL); - AnimTransformPane* transform = static_cast(getLayout()->CreateAnimTransform(resource, mpMultiArcResourceAccessor)); - if (!bindingName) animationPane->addAnimation(allocator, file->id, transform, false, true); - else animationPane->forceAddAnimation(allocator, file->id, transform, bindingName, false, true); +inline nw4r::lyt::TextBox* LayoutByNW4R::getTextBox() const { + nw4r::lyt::TextBox* textBox = static_cast(mpLayout->GetRootPane()->FindPaneByName(static_cast(mpLayoutData), true)); + return textBox; } void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) { - Info_* listEnd; - void* memory; nw4r::lyt::Pane* pane; - const char* bindingName; - u16 next; - Info_* rows; - u16 previous; void* handlerMemory; - u32 count; - u16 animationIndex; - Info_* selected; - nw4rmanager::AnmPane* animationPane; - nw4r::lyt::TextBox* textBox; - u16 selectedIndex; - u32 buttonIndex; mpAllocator = allocator; textdrawer::Base::create(allocator); mpString = static_cast(editBuffer->mpString); @@ -1146,27 +1131,35 @@ void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) { mriManager.mpAllocator = allocator; mriManager.mpInfo = static_cast(MEMAllocFromAllocator(allocator, (mriManager.mMaxLength + 2) * sizeof(Info_))); mriManager.init(); + Info_* rows; + Info_* row; + u16 next; + u16 previous; + u16 selectedIndex; + Info_* listEnd; + Info_* info; rows = mriManager.mpInfo; - selected = &rows[rows[mriManager.mMaxLength].Next]; - previous = selected->Back; - next = selected->Next; + row = &rows[mriManager.mMaxLength]; + row = &rows[row->Next]; + next = row->Next; + previous = row->Back; selectedIndex = rows[next].Back; rows[next].Back = previous; rows[previous].Next = next; - selected->Back = selectedIndex; - selected->Next = selectedIndex; - rows = mriManager.mpInfo; - listEnd = &mriManager.mpInfo[mriManager.mpInfo[static_cast(mriManager.mMaxLength + 1)].Back]; - if (selected->Next == selected->Back) { - next = listEnd->Next; - selected->Back = rows[next].Back; - selected->Next = next; + row->Back = selectedIndex; + row->Next = selectedIndex; + info = mriManager.mpInfo; + listEnd = &info[info[static_cast(mriManager.mMaxLength + 1)].Back]; + if (row->Back == row->Next) { + u16 head = listEnd->Next; + row->Back = info[head].Back; + row->Next = head; listEnd->Next = selectedIndex; - rows[next].Back = selectedIndex; + info[head].Back = selectedIndex; } - selected->StrCount = 0; - selected->DispRowCount = 1; - mpCursorLine = selected; + row->StrCount = 0; + row->DispRowCount = 1; + mpCursorLine = row; handlerMemory = MEMAllocFromAllocator(allocator, sizeof(EventHandler)); mpInputEventHandler = new (handlerMemory) EventHandler(this); nw4rmanager::Layout::createWithEventHandler(allocator, mpInputEventHandler); @@ -1179,7 +1172,7 @@ void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) { mpMultiArcResourceAccessor->GetFont(mpFontName)->SetAlternateChar(0xe06b); setFont(*mpMultiArcResourceAccessor->GetFont(mpFontName)); } - textBox = static_cast(mpLayout->GetRootPane()->FindPaneByName(static_cast(mpLayoutData), true)); + nw4r::lyt::TextBox* textBox = getTextBox(); textBox->SetString(L"", 0); mfCharacterSpacing = textBox->GetCharSpace(); mfLineSpacing = textBox->GetLineSpace(); @@ -1194,21 +1187,25 @@ void LayoutByNW4R::create(MEMAllocator* allocator, EditBuffer* editBuffer) { pane = mpLayout->GetRootPane()->FindPaneByName(static_cast(mpLanguageData)->textBox, true); if (!pane) pane = mpLayout->GetRootPane()->FindPaneByName(selectInputTextName(*this, mpLanguageData), true); mpPaneManager->getPaneComponentByPane(pane)->setTriggerTarget(true); - for (buttonIndex = 0; buttonIndex < 2; ++buttonIndex) { - const ButtonAnimations& button = csButtonAnimations[static_cast(buttonIndex)]; - animationPane = NULL; + for (u16 buttonIndex = 0; buttonIndex < 2; buttonIndex++) { + const ButtonAnimations& button = csButtonAnimations[buttonIndex]; + nw4rmanager::AnmPane* animationPane = NULL; switch (button.type) { case KT_NormalButton: { - memory = MEMAllocFromAllocator(allocator, sizeof(NormalButtonAnmPane)); + void* memory = MEMAllocFromAllocator(allocator, sizeof(NormalButtonAnmPane)); animationPane = new (memory) NormalButtonAnmPane(getPane(button.paneName), NULL); break; } } nw4r::ut::List_Append(&mAnmPanes, animationPane); - bindingName = button.bindingName; - count = button.count; - for (animationIndex = 0; animationIndex < count; ++animationIndex) { - addButtonAnimation(allocator, animationPane, button.files[animationIndex], bindingName); + for (u16 animationIndex = 0; animationIndex < button.count; animationIndex++) { + void* resource = mpMultiArcResourceAccessor->GetResource(0, button.files[animationIndex]->fileName); + AnimTransformPane* transform = static_cast(getLayout()->CreateAnimTransform(resource, mpMultiArcResourceAccessor)); + if (button.bindingName == NULL) { + animationPane->addAnimation(allocator, button.files[animationIndex]->id, transform, false, true); + } else { + animationPane->forceAddAnimation(allocator, button.files[animationIndex]->id, transform, button.bindingName, false, true); + } } } init(); @@ -1230,7 +1227,7 @@ void LayoutByNW4R::init() { mRepeatButtons = 0; mCharColor = csCharColor; visibleSeparator(false); - nw4r::lyt::TextBox* textBox = static_cast(mpLayout->GetRootPane()->FindPaneByName(static_cast(mpLayoutData), true)); + nw4r::lyt::TextBox* textBox = getTextBox(); textBox->SetString(L"", 0); mfCharacterSpacing = textBox->GetCharSpace(); mfLineSpacing = textBox->GetLineSpace(); @@ -1616,28 +1613,35 @@ void Base::create(MEMAllocator* allocator, EditBuffer* editBuffer) { mriManager.mpAllocator = allocator; mriManager.mpInfo = static_cast(MEMAllocFromAllocator(allocator, (mriManager.mMaxLength + 2) * sizeof(Info_))); mriManager.init(); - Info_* rows = mriManager.mpInfo; - struct RowCursor { Info_* row; }; - RowCursor cursor = { &rows[rows[mriManager.mMaxLength].Next] }; - u16 next = cursor.row->Next; - u16 previous = cursor.row->Back; - u16 selectedIndex = rows[next].Back; + Info_* rows; + Info_* row; + u16 next; + u16 previous; + u16 selectedIndex; + Info_* listEnd; + Info_* info; + rows = mriManager.mpInfo; + row = &rows[mriManager.mMaxLength]; + row = &rows[row->Next]; + next = row->Next; + previous = row->Back; + selectedIndex = rows[next].Back; rows[next].Back = previous; rows[previous].Next = next; - cursor.row->Back = selectedIndex; - cursor.row->Next = selectedIndex; - RowCursor listEnd = { &mriManager.mpInfo[mriManager.mpInfo[static_cast(mriManager.mMaxLength + 1)].Back] }; - rows = mriManager.mpInfo; - if (cursor.row->Back == cursor.row->Next) { - next = listEnd.row->Next; - cursor.row->Back = rows[next].Back; - cursor.row->Next = next; - listEnd.row->Next = selectedIndex; - rows[next].Back = selectedIndex; + row->Back = selectedIndex; + row->Next = selectedIndex; + info = mriManager.mpInfo; + listEnd = &info[info[static_cast(mriManager.mMaxLength + 1)].Back]; + if (row->Back == row->Next) { + u16 head = listEnd->Next; + row->Back = info[head].Back; + row->Next = head; + listEnd->Next = selectedIndex; + info[head].Back = selectedIndex; } - cursor.row->StrCount = 0; - cursor.row->DispRowCount = 1; - mpCursorLine = cursor.row; + row->StrCount = 0; + row->DispRowCount = 1; + mpCursorLine = row; } tistring::Decolated* Base::getCurrentString(bool inputting) { diff --git a/tools/decomp-assist/o3-keyboard.attempts.md b/tools/decomp-assist/o3-keyboard.attempts.md index b0d25555..726a3c23 100644 --- a/tools/decomp-assist/o3-keyboard.attempts.md +++ b/tools/decomp-assist/o3-keyboard.attempts.md @@ -69,3 +69,57 @@ every pass. Findings that constrain the original: then-block li left (rb2 in /tmp/o3-keyboard/ic2.txt). - Read-back then-blocks (n = out[n], n = *out, length scan) and ternary index/arms fold in IRO because IRO knows the buffer and n are zero (60-74). switch on u32 still compares with cmpwi (60-73). + +## Round b (worktree data-d4, branch agent/w1009/o3b-keyboard, base e47a8e94) + +### LayoutByNW4R::create: EXACT (120 -> 0) + +Three structural changes, each found with mwdbg captures, regsim, and a positional vreg-to-target-register map +(/tmp/o3-keyboard/cr/posmap.py: aligns backend-04 with the object and reads the target register at each def): + +1. Loop. `csButtonAnimations` is `const` in the original. It sits in .data only because entry 1's bindingName + is filled by the dynamic initializer (the target's slot is zero with no relocation; __sinit stores it). With a + const table, `button.count` and `button.bindingName` read directly in the loop are hoisted by the backend, so + count becomes the late codegen temp that round a predicted. The loop body is written out as in tiToolBar's + matched create; the addButtonAnimation helper is gone. +2. textBox. It must be numbered above the `&button.files[i]` CSE temp, so it is an inline local: an inline + `getTextBox()` getter that returns a local, used by create and init. The L"" literal has to stay in the + callers. Inside an inline member it becomes a weak @STRING@ object and .sdata grows from 40 to 46 bytes. +3. Row list. Base::create's code is duplicated in create, not inlined: inlining reverses the relative numbering + of the inline's locals (inline locals are keyed by declaration order, named locals by reverse declaration + order). Both functions now use the same code: C89 declarations rows, row, next, previous, selectedIndex, + listEnd, info. `row` is reused (`row = &rows[max]; row = &rows[row->Next];`) so the selected row is a split + IRO temp. The second mpInfo load goes into `info`, declared after listEnd. The head index is a block-scoped + `head`. Base::create stays exact and loses the RowCursor carrier struct. + +Dead ends: Base::create auto-inlined (bl, too big), `inline Base::create` (27, reversed row registers), a +RowInfoManager::newLine() helper (reloads mpInfo, 74 insns), a setupTextBox() helper holding SetString (exact +code, but .sdata changes). + +### Decolated::inputChar: not exact (13) + +Mechanism behind the target's vanished '\n' then-block, from per-pass PCode captures on GC3: +- VN (0x5976e0) merges li's within a block at pass 01 and across single-predecessor chains at passes 19/24. + A redundant li disappears only if it matches a value-number record whose holder is the same register with the + same index. Records are keyed by opcode, flags and operand count. Li's created by const-prop have 3 operands, + so they form their own record chain. +- The function-start zeroing (`input[4..0] = 0`) owns the 2-operand `li 0` record in the mode-3 block's chain, + so any 2-operand `n = 0` reset survives. That covers main's CharacterOutput and every class, inline, ternary, + switch, reference, pointer and read-back form tried. +- A POD struct with a constant initializer (`LinePosition position = {0}; ... position = LinePosition();`) stays + in memory through both const-prop passes. Array-to-register (pass 07) gives it a new vreg, const-prop (pass 17) + turns its defs into 3-operand li's, and VN at pass 24 deletes the then-block def. This reproduces the dead + `cmplwi` with no branch (tiny tests struct1/tc, real-function variants p1/z2). +- Remaining blocker for that family: the input[0] store's zero then belongs to the struct init's codegen li, + which pass 24 replaces with the function-start zero. The result is `sth r5` instead of `li r6,0; sth r6`, and + the `mr r4,r5` before the second store is lost (133 insns, 73 diffs). The target needs the store and n to share + one register whose li comes first in the block and whose then-block def is deleted. None of about 120 variants + reached that (/tmp/o3-keyboard/{t,t2,ic,grid}). combosweep: 13 -> 13. + +### tiInputForm link (not flipped) + +The unit is 221/221 exact with code and data at 100%, but a trial Matching flip fails to link: the vtables for +Decolated (tiString.o) and CommandReceiver (tiTextInputBase.o) are multiply defined, and GUIComponent's is also +emitted here. There are also about 96 extra bytes of anonymous .sdata2 floats, Base::RowInfoManager::~RowInfoManager +is emitted as a global (the target inlines it into ~Base), and only 37 of the 221 functions are in target order. +Flip reverted; the DOL is unchanged. -- 2.49.1