diff --git a/src/ILCompiler.WebAssembly/src/CodeGen/EvaluationStack.cs b/src/ILCompiler.WebAssembly/src/CodeGen/EvaluationStack.cs
index de79dd67f..98f862c54 100644
--- a/src/ILCompiler.WebAssembly/src/CodeGen/EvaluationStack.cs
+++ b/src/ILCompiler.WebAssembly/src/CodeGen/EvaluationStack.cs
@@ -9,7 +9,6 @@ using ILCompiler.Compiler.CppCodeGen;
using Internal.TypeSystem;
using LLVMSharp;
using ILCompiler.CodeGen;
-using System.Collections.Generic;
namespace Internal.IL
{
@@ -87,7 +86,7 @@ namespace Internal.IL
/// Position where to insert
public void InsertAt(T v, int pos)
{
- Debug.Assert(pos <= _top, "Invalid insertion point");
+ Debug.Assert(pos < _top, "Invalid insertion point");
if (_top >= _stack.Length)
{
@@ -167,20 +166,6 @@ namespace Internal.IL
}
}
- class LLVMTypeRefEqualityComparer : IEqualityComparer
- {
- public static LLVMTypeRefEqualityComparer Instance = new LLVMTypeRefEqualityComparer();
- public bool Equals(LLVMTypeRef x, LLVMTypeRef y)
- {
- return x.Pointer.Equals(y.Pointer);
- }
-
- public int GetHashCode(LLVMTypeRef obj)
- {
- return obj.Pointer.GetHashCode();
- }
- }
-
///
/// Abstract representation of a stack entry
///
@@ -196,53 +181,18 @@ namespace Internal.IL
///
public TypeDesc Type { get; }
- Dictionary _castValues = new Dictionary(LLVMTypeRefEqualityComparer.Instance);
-
- public LLVMValueRef ValueAsType(LLVMTypeRef type, LLVMBuilderRef builder)
- {
- return ValueAsTypeInternal(type, builder, false);
- }
-
- public LLVMValueRef ValueAsType(TypeDesc type, LLVMBuilderRef builder)
- {
- return ValueAsType(ILImporter.GetLLVMTypeForTypeDesc(type), builder);
- }
-
- public LLVMValueRef ValueForStackKind(StackValueKind kind, LLVMBuilderRef builder, bool signExtend)
- {
- if (kind == StackValueKind.Int32)
- return ValueAsInt32(builder, signExtend);
- else if (kind == StackValueKind.Int64)
- return ValueAsInt64(builder, signExtend);
- else if (kind == StackValueKind.Float)
- return ValueAsType(LLVM.FloatType(), builder);
- else if (kind == StackValueKind.NativeInt || kind == StackValueKind.ByRef || kind == StackValueKind.ObjRef)
- return ValueAsInt32(builder, false);
- else
- throw new NotImplementedException();
- }
-
- public LLVMValueRef ValueAsInt32(LLVMBuilderRef builder, bool signExtend)
- {
- return ValueAsTypeInternal(LLVM.Int32Type(), builder, signExtend);
- }
-
- public LLVMValueRef ValueAsInt64(LLVMBuilderRef builder, bool signExtend)
- {
- return ValueAsTypeInternal(LLVM.Int32Type(), builder, signExtend);
- }
-
- protected abstract LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend);
+ public LLVMValueRef LLVMValue { get; set; }
///
/// Initializes a new instance of StackEntry.
///
/// Kind of entry.
/// Type if any of entry.
- protected StackEntry(StackValueKind kind, TypeDesc type = null)
+ protected StackEntry(StackValueKind kind, LLVMValueRef llvmValue, TypeDesc type = null)
{
Kind = kind;
Type = type;
+ LLVMValue = llvmValue;
}
///
@@ -256,6 +206,45 @@ namespace Internal.IL
///
/// A new instance of the same type as the current entry.
public abstract StackEntry Duplicate();
+
+ ///
+ /// Overridden and sealed to force descendants to override .
+ ///
+ /// String representation of current entry
+ public override sealed string ToString()
+ {
+ StringBuilder s = new StringBuilder();
+ BuildRepresentation(s);
+ return s.ToString();
+ }
+
+ ///
+ /// Build a representation of current entry in .
+ ///
+ /// StringBuilder where representation will be saved.
+ protected virtual void BuildRepresentation(StringBuilder s)
+ {
+ Debug.Assert(s != null, "StringBuilder is null.");
+ if (Type != null)
+ {
+ s.Append(Type);
+ if (Kind != StackValueKind.Unknown)
+ {
+ s.Append('(');
+ s.Append(Kind);
+ s.Append(')');
+ }
+ }
+ else if (Kind != StackValueKind.Unknown)
+ {
+ if (Kind != StackValueKind.Unknown)
+ {
+ s.Append('(');
+ s.Append(Kind);
+ s.Append(')');
+ }
+ }
+ }
}
///
@@ -263,7 +252,7 @@ namespace Internal.IL
///
internal abstract class ConstantEntry : StackEntry
{
- protected ConstantEntry(StackValueKind kind, TypeDesc type = null) : base(kind, type)
+ protected ConstantEntry(StackValueKind kind, LLVMValueRef llvmValue, TypeDesc type = null) : base(kind, llvmValue, type)
{
}
@@ -282,38 +271,28 @@ namespace Internal.IL
{
public T Value { get; }
- protected ConstantEntry(StackValueKind kind, T value, TypeDesc type = null) : base(kind, type)
+ protected ConstantEntry(StackValueKind kind, T value, LLVMValueRef llvmValue, TypeDesc type = null) : base(kind, llvmValue, type)
{
Value = value;
}
+
+ protected override void BuildRepresentation(StringBuilder s)
+ {
+ base.BuildRepresentation(s);
+ if (s.Length > 0)
+ {
+ s.Append(' ');
+ }
+ s.Append(Value);
+ }
}
internal class Int32ConstantEntry : ConstantEntry
{
- public Int32ConstantEntry(int value, TypeDesc type = null) : base(StackValueKind.Int32, value, type)
+ public Int32ConstantEntry(int value, TypeDesc type = null) : base(StackValueKind.Int32, value, LLVM.ConstInt(LLVM.Int32Type(), (ulong)value, LLVMMisc.False), type)
{
}
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
- {
- if (type.TypeKind == LLVMTypeKind.LLVMPointerTypeKind && Value == 0)
- {
- return LLVM.ConstPointerNull(type);
- }
- else if (type.TypeKind == LLVMTypeKind.LLVMPointerTypeKind && Value != 0)
- {
- return LLVM.ConstIntToPtr(LLVM.ConstInt(LLVM.Int32Type(), (ulong)Value, LLVMMisc.False), type);
- }
- else if (type.TypeKind != LLVMTypeKind.LLVMIntegerTypeKind)
- {
- throw new NotImplementedException();
- }
- else
- {
- return LLVM.ConstInt(type, (ulong)Value, LLVMMisc.False);
- }
- }
-
public override StackEntry Duplicate()
{
return new Int32ConstantEntry(Value, Type);
@@ -345,7 +324,7 @@ namespace Internal.IL
internal class Int64ConstantEntry : ConstantEntry
{
- public Int64ConstantEntry(long value, TypeDesc type = null) : base(StackValueKind.Int64, value, type)
+ public Int64ConstantEntry(long value, TypeDesc type = null) : base(StackValueKind.Int64, value, LLVM.ConstInt(LLVM.Int64Type(), (ulong)value, LLVMMisc.False), type)
{
}
@@ -354,26 +333,6 @@ namespace Internal.IL
return new Int64ConstantEntry(Value, Type);
}
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
- {
- if (type.TypeKind == LLVMTypeKind.LLVMPointerTypeKind && Value == 0)
- {
- return LLVM.ConstPointerNull(type);
- }
- else if (type.TypeKind == LLVMTypeKind.LLVMPointerTypeKind && Value != 0)
- {
- return LLVM.ConstIntToPtr(LLVM.ConstInt(LLVM.Int64Type(), (ulong)Value, LLVMMisc.False), type);
- }
- else if (type.TypeKind != LLVMTypeKind.LLVMIntegerTypeKind)
- {
- throw new NotImplementedException();
- }
- else
- {
- return LLVM.ConstInt(type, (ulong)Value, LLVMMisc.False);
- }
- }
-
public override bool IsCastNecessary(TypeDesc destType)
{
switch (destType.UnderlyingType.Category)
@@ -404,15 +363,10 @@ namespace Internal.IL
internal class FloatConstantEntry : ConstantEntry
{
- public FloatConstantEntry(double value, TypeDesc type = null) : base(StackValueKind.Float, value, type)
+ public FloatConstantEntry(double value, TypeDesc type = null) : base(StackValueKind.Float, value, LLVM.ConstReal(LLVM.FloatType(), value), type)
{
}
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
- {
- return LLVM.ConstReal(type, Value);
- }
-
public override StackEntry Duplicate()
{
return new FloatConstantEntry(Value, Type);
@@ -428,77 +382,34 @@ namespace Internal.IL
/// String representation of current expression
///
public string Name { get; set; }
- public LLVMValueRef RawLLVMValue { get; set; }
+
///
/// Initializes new instance of ExpressionEntry
///
/// Kind of entry
/// String representation of entry
/// Type if any of entry
- public ExpressionEntry(StackValueKind kind, string name, LLVMValueRef llvmValue, TypeDesc type = null) : base(kind, type)
+ public ExpressionEntry(StackValueKind kind, string name, LLVMValueRef llvmValue, TypeDesc type = null) : base(kind, llvmValue, type)
{
Name = name;
- RawLLVMValue = llvmValue;
}
public override StackEntry Duplicate()
{
- return new ExpressionEntry(Kind, Name, RawLLVMValue, Type);
+ return new ExpressionEntry(Kind, Name, LLVMValue, Type);
}
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
+ protected override void BuildRepresentation(StringBuilder s)
{
- //TODO: deal with sign extension here
- return ILImporter.CastIfNecessary(builder, RawLLVMValue, type);
+ base.BuildRepresentation(s);
+ if (s.Length > 0)
+ {
+ s.Append(' ');
+ }
+ s.Append(Name);
}
}
-
- internal class LoadExpressionEntry : ExpressionEntry
- {
- ///
- /// Initializes new instance of ExpressionEntry
- ///
- /// Kind of entry
- /// String representation of entry
- /// Type if any of entry
- public LoadExpressionEntry(StackValueKind kind, string name, LLVMValueRef llvmValue, TypeDesc type = null) : base(kind, name, llvmValue, type)
- {
- }
-
- public override StackEntry Duplicate()
- {
- return new LoadExpressionEntry(Kind, Name, RawLLVMValue, Type);
- }
-
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
- {
- return ILImporter.LoadValue(builder, RawLLVMValue, Type, type, signExtend);
- }
- }
-
- internal class AddressExpressionEntry : ExpressionEntry
- {
- ///
- /// Initializes new instance of ExpressionEntry
- ///
- /// Kind of entry
- /// String representation of entry
- /// Type if any of entry
- public AddressExpressionEntry(StackValueKind kind, string name, LLVMValueRef llvmValue, TypeDesc type = null) : base(kind, name, llvmValue, type)
- {
- }
-
- public override StackEntry Duplicate()
- {
- return new LoadExpressionEntry(Kind, Name, RawLLVMValue, Type);
- }
-
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
- {
- return ILImporter.CastIfNecessary(builder, RawLLVMValue, type);
- }
- }
-
+
///
/// Entry representing some token (either of TypeDesc, MethodDesc or FieldDesc) along with its string representation
///
@@ -506,19 +417,21 @@ namespace Internal.IL
{
public T LdToken { get; }
- public LdTokenEntry(StackValueKind kind, string name, T token, TypeDesc type = null) : base(kind, name, default(LLVMValueRef), type)
+ public LdTokenEntry(StackValueKind kind, string name, T token, LLVMValueRef value, TypeDesc type = null) : base(kind, name, value, type)
{
LdToken = token;
}
public override StackEntry Duplicate()
{
- return new LdTokenEntry(Kind, Name, LdToken, Type);
+ return new LdTokenEntry(Kind, Name, LdToken, LLVMValue, Type);
}
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
+ protected override void BuildRepresentation(StringBuilder s)
{
- return ILImporter.CastIfNecessary(builder, RawLLVMValue, type);
+ base.BuildRepresentation(s);
+ s.Append(' ');
+ s.Append(LdToken);
}
}
@@ -529,7 +442,7 @@ namespace Internal.IL
///
public static InvalidEntry Entry = new InvalidEntry();
- protected InvalidEntry() : base(StackValueKind.Unknown, null)
+ protected InvalidEntry() : base(StackValueKind.Unknown, default(LLVMValueRef), null)
{
}
@@ -538,33 +451,9 @@ namespace Internal.IL
return this;
}
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
+ protected override void BuildRepresentation(StringBuilder s)
{
- throw new InvalidOperationException();
- }
- }
-
- ///
- /// Entry representing a writable sharable stack entry that can survive from one basic block to another
- ///
- internal class SpilledExpressionEntry : ExpressionEntry
- {
- public int LocalIndex;
- private ILImporter _importer;
- public SpilledExpressionEntry(StackValueKind kind, string name, TypeDesc type, int localIndex, ILImporter importer) : base(kind, name, new LLVMValueRef(IntPtr.Zero), type)
- {
- LocalIndex = localIndex;
- _importer = importer;
- }
-
- protected override LLVMValueRef ValueAsTypeInternal(LLVMTypeRef type, LLVMBuilderRef builder, bool signExtend)
- {
- return _importer.LoadTemp(LocalIndex, type);
- }
-
- public override StackEntry Duplicate()
- {
- return new SpilledExpressionEntry(Kind, Name, Type, LocalIndex, _importer);
+ s.Append("Invalid Entry");
}
}
}
diff --git a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs
index c41d66da7..8c7886288 100644
--- a/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs
+++ b/src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs
@@ -19,13 +19,6 @@ namespace Internal.IL
// backend before the actual compilation happens to gain insights into the code.
partial class ILImporter
{
- public enum LocalVarKind
- {
- Argument,
- Local,
- Temp
- }
-
ArrayBuilder