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

Commit

Permalink
[Interpreter] Load Constants (#6284)
Browse files Browse the repository at this point in the history
This PR adds support for loading constants to the interpreter. The following OpCodes are now supported:

* `ldc.*`
* `ldnull`
* `ldstr`
  • Loading branch information
tonerdo authored and MichalStrehovsky committed Sep 4, 2018
1 parent d82d460 commit 0ed3a00
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private void ImportCalli(int token)

private void ImportLoadNull()
{
throw new NotImplementedException();
_interpreter.EvaluationStack.Push(new ObjectRefStackItem(null));
}

private void ImportReturn()
Expand All @@ -163,8 +163,15 @@ private void ImportReturn()
case StackValueKind.Unknown:
case StackValueKind.NativeInt:
case StackValueKind.Float:
if (stackItem.Type == WellKnownType.Single)
_interpreter.SetReturnValue(((FloatStackItem)stackItem).Value);
else if (stackItem.Type == WellKnownType.Double)
_interpreter.SetReturnValue(((DoubleStackItem)stackItem).Value);
break;
case StackValueKind.ByRef:
case StackValueKind.ObjRef:
_interpreter.SetReturnValue(((ObjectRefStackItem)stackItem).Value);
break;
case StackValueKind.ValueType:
default:
break;
Expand All @@ -179,9 +186,14 @@ private void ImportLoadInt(long value, StackValueKind kind)
_interpreter.EvaluationStack.Push(new Int64StackItem(value));
}

private void ImportLoadFloat(float value)
{
_interpreter.EvaluationStack.Push(new FloatStackItem(value));
}

private void ImportLoadFloat(double value)
{
throw new NotImplementedException();
_interpreter.EvaluationStack.Push(new DoubleStackItem(value));
}

private void ImportShiftOperation(ILOpcode opcode)
Expand Down Expand Up @@ -374,9 +386,10 @@ private void ImportCasting(ILOpcode opCode, int v)
throw new NotImplementedException();
}

private void ImportLoadString(int v)
private void ImportLoadString(int token)
{
throw new NotImplementedException();
string str = (string)_methodIL.GetObject(token);
_interpreter.EvaluationStack.Push(new ObjectRefStackItem(str));
}

private void ImportBinaryOperation(ILOpcode opCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,66 @@

using System;
using Internal.IL;
using Internal.TypeSystem;

namespace Internal.Runtime.Interpreter
{
internal abstract class StackItem
{
public StackValueKind Kind { get; set; }
public StackValueKind Kind { get; protected set; }
public WellKnownType Type { get; protected set; }
}

internal class StackItem<T> : StackItem
{
public T Value { get; }

public StackItem(T value, StackValueKind kind)
public StackItem(T value, StackValueKind kind, WellKnownType type)
{
Value = value;
Kind = kind;
Type = type;
}
}

internal class Int32StackItem : StackItem<int>
{
public Int32StackItem(int value) : base(value, StackValueKind.Int32)
public Int32StackItem(int value) : base(value, StackValueKind.Int32, WellKnownType.Int32)
{
}
}

internal class Int64StackItem : StackItem<long>
{
public Int64StackItem(long value) : base(value, StackValueKind.Int64)
public Int64StackItem(long value) : base(value, StackValueKind.Int64, WellKnownType.Int64)
{
}
}

internal class FloatStackItem : StackItem<double>
internal class FloatStackItem : StackItem<float>
{
public FloatStackItem(double value) : base(value, StackValueKind.Float)
public FloatStackItem(float value) : base(value, StackValueKind.Float, WellKnownType.Single)
{
}
}

internal class DoubleStackItem : StackItem<double>
{
public DoubleStackItem(double value) : base(value, StackValueKind.Float, WellKnownType.Double)
{
}
}

internal class ValueTypeStackItem : StackItem<ValueType>
{
public ValueTypeStackItem(ValueType value) : base(value, StackValueKind.ValueType)
public ValueTypeStackItem(ValueType value) : base(value, StackValueKind.ValueType, WellKnownType.ValueType)
{
}
}

internal class ObjectRefStackItem : StackItem<Object>
{
public ObjectRefStackItem(Object value) : base(value, StackValueKind.ObjRef)
public ObjectRefStackItem(Object value) : base(value, StackValueKind.ObjRef, WellKnownType.Object)
{
}
}
Expand Down

0 comments on commit 0ed3a00

Please sign in to comment.