Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Reference tracing #232

Merged
merged 17 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/neo-vm/EvaluationStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Neo.VM.Types;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Neo.VM
{
public sealed class EvaluationStack : IReadOnlyCollection<StackItem>
{
private readonly RandomAccessStack<StackItem> innerStack = new RandomAccessStack<StackItem>();
private readonly ReferenceCounter referenceCounter;

internal EvaluationStack(ReferenceCounter referenceCounter)
{
this.referenceCounter = referenceCounter;
}

public int Count => innerStack.Count;

internal void Clear()
{
foreach (StackItem item in innerStack)
referenceCounter.RemoveStackReference(item);
innerStack.Clear();
}

internal void CopyTo(EvaluationStack stack, int count = -1)
{
innerStack.CopyTo(stack.innerStack, count);
}

public IEnumerator<StackItem> GetEnumerator()
{
return innerStack.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return innerStack.GetEnumerator();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Insert(int index, StackItem item)
{
innerStack.Insert(index, item);
referenceCounter.AddStackReference(item);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public StackItem Peek(int index = 0)
{
return innerStack.Peek(index);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public StackItem Pop()
{
if (!TryPop(out StackItem item))
throw new InvalidOperationException();
return item;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Push(StackItem item)
{
innerStack.Push(item);
referenceCounter.AddStackReference(item);
}

internal void Set(int index, StackItem item)
{
StackItem old_item = innerStack.Peek(index);
referenceCounter.RemoveStackReference(old_item);
innerStack.Set(index, item);
referenceCounter.AddStackReference(item);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryPop<T>(out T item) where T : StackItem
{
return TryRemove(0, out item);
}

internal bool TryRemove<T>(int index, out T item) where T : StackItem
{
if (!innerStack.TryRemove(index, out StackItem stackItem))
{
item = null;
return false;
}
referenceCounter.RemoveStackReference(stackItem);
item = stackItem as T;
return item != null;
}
}
}
11 changes: 5 additions & 6 deletions src/neo-vm/ExecutionContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -24,12 +23,12 @@ public sealed class ExecutionContext
/// <summary>
/// Evaluation stack
/// </summary>
public RandomAccessStack<StackItem> EvaluationStack { get; }
public EvaluationStack EvaluationStack { get; }

/// <summary>
/// Alternative stack
/// </summary>
public RandomAccessStack<StackItem> AltStack { get; }
public EvaluationStack AltStack { get; }

/// <summary>
/// Instruction pointer
Expand Down Expand Up @@ -65,12 +64,12 @@ public Instruction NextInstruction
/// <param name="script">Script</param>
/// <param name="callingScript">The calling script</param>
/// <param name="rvcount">Number of items to be returned</param>
internal ExecutionContext(Script script, Script callingScript, int rvcount)
: this(script, callingScript, rvcount, new RandomAccessStack<StackItem>(), new RandomAccessStack<StackItem>())
internal ExecutionContext(Script script, Script callingScript, int rvcount, ReferenceCounter referenceCounter)
: this(script, callingScript, rvcount, new EvaluationStack(referenceCounter), new EvaluationStack(referenceCounter))
{
}

private ExecutionContext(Script script, Script callingScript, int rvcount, RandomAccessStack<StackItem> stack, RandomAccessStack<StackItem> alt)
private ExecutionContext(Script script, Script callingScript, int rvcount, EvaluationStack stack, EvaluationStack alt)
{
this.RVCount = rvcount;
this.Script = script;
Expand Down
Loading