informedcitizenry / 6502.Net

A .Net-based Cross-Assembler for Several 8-Bit Microprocessors
MIT License
58 stars 17 forks source link

Crash with the functions #3

Closed svallee-dev closed 4 years ago

svallee-dev commented 4 years ago

When I try to define and use a function, like so:

FP      .function val=1
            .return val * 64
        .endfunction

testFP
        .word FP(500)

The assembler freezes, and I need to kill the process manually.

svallee-dev commented 4 years ago

Not sure why this is happening yet, but I found out why the code freezes into an infinite loop when using a function in the asm code.

This exception is triggered: "Collection was modified; enumeration operation may not execute." at SymbolManager.cs:911

It get caught by the try in AssemblyController.cs (line 113), and as it doesn't have a specific handler, it ends up running this code instead:

else
{
     Assembler.PassNeeded = true;
}

Which I assume cause the code to just get back into another pass, which trigger the same exception, etc etc.

I replaced that line of code by a simple throw; so at least my process stop when an exception is encountered (so I can print it).

I'll keep digging, just thought you might be interested to know :)

svallee-dev commented 4 years ago

Ok looks like the IEnumerator ephemerals modify itself for some reason during its foreach loop, causing the exception. That makes no sense to me, whatsoever.

But as a dirty fix, I locally changed ephemerals to a List and that fixed my issue! Just... weird.

                    _ephemeralCounter--;
                    var ephemerals = new List<string>(_symbols.Keys.Where(k => k.Contains(sc, StringComparison.Ordinal)));
                    foreach (var key in ephemerals)
                        _symbols.Remove(key);
svallee-dev commented 4 years ago

Oh I see: it's not that weird. ephemerals is a dynamic query of the _symbols table (the result collection is not created on the spot) so as soon as you call a Remove(), the whole enumerator is invalidated!

So this is a valid bug, but thankfully it's an easy fix :)

informedcitizenry commented 4 years ago

thank you @svallee-dev, you are right we have to essentially make ephemerals a separate container of the symbols we are removing from _symbols, which we are mutating. Good catch!