Implement optimization for collapse instruction.

This commit is contained in:
2022-10-30 01:39:22 +08:00
parent 0a0be73ea7
commit 13131b0f31
4 changed files with 84 additions and 5 deletions

View File

@@ -41,8 +41,10 @@ namespace Cryville.Common.Pdt {
public void Optimize(PdtExpression exp) {
_framecount = 0;
_goffset = 0;
List<PdtInstruction.Collapse> ct;
var cols = new Dictionary<LinkedListNode<PdtInstruction>, List<PdtInstruction.Collapse>>();
var il = exp.Instructions;
for (_rip = il.First; _rip != null; _rip = _rip.Next) {
for (_rip = il.First; _rip != null; _rip = _rip == null ? il.First : _rip.Next) {
var i = _rip.Value;
if (i is PdtInstruction.Operate) {
int fc0 = _framecount;
@@ -63,6 +65,33 @@ namespace Cryville.Common.Pdt {
for (var j = 0; j <= fc1; j++) il.Remove(_rip.Previous);
}
}
else if (i is PdtInstruction.Collapse) {
var t = (PdtInstruction.Collapse)i;
try {
var pins = _rip;
i.Execute(this);
if (_rip == pins) {
_rip = _rip.Next;
il.Remove(_rip.Previous);
il.Remove(_rip.Previous);
_rip = _rip.Previous;
}
else {
_rip = pins.Previous;
while (_rip.Next != t.Target) il.Remove(_rip.Next);
il.Remove(_rip.Next);
if (cols.TryGetValue(t.Target, out ct)) {
foreach (var u in ct) u.Target = _rip;
cols.Remove(t.Target);
cols.Add(_rip, ct);
}
}
}
catch (Exception) {
if (cols.TryGetValue(t.Target, out ct)) ct.Add(t);
else cols.Add(t.Target, new List<PdtInstruction.Collapse> { t });
}
}
else if (i is PdtInstruction.PushVariable) {
i.Execute(this);
var frame = _stack[_framecount - 1];
@@ -72,6 +101,15 @@ namespace Cryville.Common.Pdt {
}
}
else i.Execute(this);
if (_rip != null && cols.TryGetValue(_rip, out ct)) {
unsafe {
fixed (StackFrame* frame = &_stack[_framecount - 1]) {
frame->Type = PdtInternalType.Error;
frame->Offset = -1;
frame->Length = 0;
}
}
}
}
exp.IsConstant = true;
exp.IsPotentialConstant = true;
@@ -118,7 +156,10 @@ namespace Cryville.Common.Pdt {
/// <param name="value">The value of the variable.</param>
protected abstract void GetVariable(string name, out int type, out byte[] value);
internal void Operate(ref string name, int pc) {
Operate(GetOperator(name, pc), pc);
PdtOperator op;
try { op = GetOperator(name, pc); }
catch (Exception) { _framecount -= pc; return; }
Operate(op, pc);
}
/// <summary>
/// Gets an operator of the specified name and the suggested parameter count.