Implement optimization for collapse instruction.
This commit is contained in:
@@ -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.
|
||||
|
@@ -80,7 +80,7 @@ namespace Cryville.Common.Pdt {
|
||||
public class Collapse : PdtInstruction {
|
||||
private string m_name;
|
||||
public string Name { get { return m_name; } }
|
||||
public LinkedListNode<PdtInstruction> Target { get; private set; }
|
||||
public LinkedListNode<PdtInstruction> Target { get; internal set; }
|
||||
public Collapse(string name, LinkedListNode<PdtInstruction> target) {
|
||||
m_name = name;
|
||||
Target = target;
|
||||
@@ -195,6 +195,9 @@ namespace Cryville.Common.Pdt {
|
||||
default: pc++; break;
|
||||
}
|
||||
}
|
||||
else if (OP_TYPE[t2.Value[0]] == 1) {
|
||||
colp = new Dictionary<LinkedListNode<PdtInstruction>, string> { { ins.Last, t2.Value } };
|
||||
}
|
||||
t1 = t2;
|
||||
break;
|
||||
case 0x0400:
|
||||
|
Reference in New Issue
Block a user