private object EvaluateConstExpr(SExpression expression, WasmValueType resultType) { var anonModule = new WasmFile(); var instructions = Assembler.AssembleInstructionExpression(expression, anonModule); var inst = ModuleInstance.Instantiate(anonModule, new SpecTestImporter()); return(inst.Evaluate(new InitializerExpression(instructions), resultType)); }
/// <summary> /// Creates a new variable from the given value. /// </summary> /// <param name="Type">The variable's type.</param> /// <param name="IsMutable">The variable's mutability.</param> /// <param name="Value">The variable's initial value.</param> /// <returns>The newly-created variable.</returns> public static Variable Create <T>(WasmValueType Type, bool IsMutable, T Value) { if (!IsInstanceOf <T>(Value, Type)) { throw new WasmException( "Cannot create a variable of type '" + ((object)Type).ToString() + "' with an initial value of type '" + GetTypeName(Value) + "'."); } return(new Variable(Value, Type, IsMutable)); }
/// <summary> /// Evaluates an initializer expression. /// </summary> /// <param name="expression">The expression to evaluate.</param> /// <param name="resultType">The result type expected from the expression.</param> /// <returns>The value obtained by evaluating the initializer expression.</returns> public object Evaluate(InitializerExpression expression, WasmValueType resultType) { var context = new InterpreterContext(this, new[] { resultType }); foreach (var instruction in expression.BodyInstructions) { Interpreter.Interpret(instruction, context); } var result = context.Pop <object>(); if (context.StackDepth > 0) { throw new WasmException( "The stack must contain exactly one value after " + "evaluating an initializer expression. Actual stack depth: " + context.StackDepth + "."); } return(result); }
/// <summary> /// Checks if the given value is an instance of the given WebAssembly value type. /// </summary> /// <param name="Value">A value.</param> /// <param name="Type">A WebAssembly value type.</param> /// <returns> /// <c>true</c> if the given value is an instance of the given WebAssembly value type; /// otherwise, <c>false</c>. /// </returns> public static bool IsInstanceOf <T>(T Value, WasmValueType Type) { switch (Type) { case WasmValueType.Int32: return(Value is int); case WasmValueType.Int64: return(Value is long); case WasmValueType.Float32: return(Value is float); case WasmValueType.Float64: return(Value is double); default: throw new WasmException("Unknown value type: " + Type); } }
/// <summary> /// Takes a WebAssembly value type and maps it to its corresponding CLR type. /// </summary> /// <param name="type">The type to map to a CLR type.</param> /// <returns>A CLR type.</returns> public static Type ToClrType(WasmValueType type) { switch (type) { case WasmValueType.Float32: return(typeof(float)); case WasmValueType.Float64: return(typeof(double)); case WasmValueType.Int32: return(typeof(int)); case WasmValueType.Int64: return(typeof(long)); default: throw new WasmException($"Cannot convert unknown WebAssembly type '{type}' to a CLR type."); } }
/// <summary> /// Gets the default value for the given WebAssembly value tyoe. /// </summary> /// <param name="Type">A WebAssembly value type.</param> /// <returns>The default value.</returns> public static object GetDefaultValue(WasmValueType Type) { switch (Type) { case WasmValueType.Int32: return(default(int)); case WasmValueType.Int64: return(default(long)); case WasmValueType.Float32: return(default(float)); case WasmValueType.Float64: return(default(double)); default: throw new WasmException("Unknown value type: " + Type); } }
/// <summary> /// Creates a new variable of the given type and mutability, and initializes /// it with the default value for the given type. /// </summary> /// <param name="Type">The variable's type.</param> /// <param name="IsMutable">The variable's mutability.</param> /// <returns>The newly-created variable.</returns> public static Variable CreateDefault(WasmValueType Type, bool IsMutable) { return(Create <object>(Type, IsMutable, GetDefaultValue(Type))); }
/// <summary> /// Creates a variable with the given value, type and mutability. /// </summary> /// <param name="Value">The variable's value.</param> /// <param name="Type">The variable's type.</param> /// <param name="IsMutable">The variable's mutability.</param> private Variable(object Value, WasmValueType Type, bool IsMutable) { this.val = Value; this.Type = Type; this.IsMutable = IsMutable; }
private static InstructionImpl ImplementAsComparisonOpCode(OpCode op, WasmValueType type) { return(ImplementAsOpCode(op, WasmValueType.Int32, type, type)); }
/// <summary> /// Creates a string representation for the given WebAssembly value type. /// </summary> /// <param name="value">The WebAssembly value type to convert to a string.</param> /// <returns>A string representation for a WebAssembly value type.</returns> public static string WasmTypeToString(WasmValueType value) { return(WasmTypeToString((WasmType)value)); }
/// <summary> /// Creates a global type from the given content type and mutability. /// </summary> /// <param name="ContentType">The type of content in the global type.</param> /// <param name="IsMutable">The global type's mutability.</param> public GlobalType(WasmValueType ContentType, bool IsMutable) { this.ContentType = ContentType; this.IsMutable = IsMutable; }
/// <summary> /// Informs that compiler context that a value is pushed onto the stack at the /// current point in the code generation process. /// </summary> /// <param name="type">The type of the value that is pushed on the stack.</param> public void Push(WasmValueType type) { StackContents.Push(type); }
/// <summary> /// Writes a textual representation of the given WebAssembly value type to /// the given text writer. /// </summary> /// <param name="Value">The value type to print to the text writer.</param> /// <param name="Writer">The writer to which the textual WebAssembly value type should be written.</param> public static void DumpWasmType(WasmValueType Value, TextWriter Writer) { DumpWasmType((WasmType)Value, Writer); }
/// <summary> /// Creates a variable with the given value, type and mutability. /// </summary> /// <param name="value">The variable's value.</param> /// <param name="type">The variable's type.</param> /// <param name="isMutable">The variable's mutability.</param> private Variable(object value, WasmValueType type, bool isMutable) { this.val = value; this.Type = type; this.IsMutable = isMutable; }
private static InstructionImpl ImplementAsUnaryOpCode(OpCode op, WasmValueType type) { return(ImplementAsOpCode(op, type, type)); }
/// <summary> /// Writes a WebAssembly value type. /// </summary> /// <param name="value">The WebAssembly language value to write.</param> /// <returns>The number of bytes used to encode the type.</returns> public int WriteWasmValueType(WasmValueType value) { return(WriteVarInt7((sbyte)value)); }
/// <summary> /// Creates a new local entry that defines <c>LocalCount</c> variables of type /// <c>LocalType</c>. /// </summary> /// <param name="LocalType">The type of the variables to define.</param> /// <param name="LocalCount">The number of local variables to define.</param> public LocalEntry(WasmValueType LocalType, uint LocalCount) { this.LocalType = LocalType; this.LocalCount = LocalCount; }
/// <summary> /// Creates a global type from the given content type and mutability. /// </summary> /// <param name="contentType">The type of content in the global type.</param> /// <param name="isMutable">The global type's mutability.</param> public GlobalType(WasmValueType contentType, bool isMutable) { this.ContentType = contentType; this.IsMutable = isMutable; }
/// <summary> /// Writes a textual representation of the given WebAssembly value type to /// the given text writer. /// </summary> /// <param name="value">The value type to print to the text writer.</param> /// <param name="writer">The writer to which the textual WebAssembly value type should be written.</param> public static void DumpWasmType(WasmValueType value, TextWriter writer) { DumpWasmType((WasmType)value, writer); }
/// <summary> /// Creates a new local entry that defines <c>LocalCount</c> variables of type /// <c>LocalType</c>. /// </summary> /// <param name="localType">The type of the variables to define.</param> /// <param name="localCount">The number of local variables to define.</param> public LocalEntry(WasmValueType localType, uint localCount) { this.LocalType = localType; this.LocalCount = localCount; }