示例#1
0
        /// <summary>
        /// Emit IL for the given expression
        /// </summary>
        /// <typeparam name="TInLeft"></typeparam>
        /// <typeparam name="TInRight"></typeparam>
        /// <typeparam name="TOut"></typeparam>
        /// <typeparam name="TEmit"></typeparam>
        /// <param name="expr"></param>
        /// <param name="emitter"></param>
        /// <param name="errorLabel"></param>
        /// <param name="leftConst">Indicates if the left parameter is a constant</param>
        /// <param name="rightConst">Indicates if the right parameter is a constant</param>
        /// <returns>A tuple, indicating the type left on the stack by this expression and if the expression **is** potentially fallible (i.e. errorLabel may be jumped to)</returns>
        public static ConvertResult ConvertBinary <TInLeft, TInRight, TOut, TEmit>(
            this Expression <Func <TInLeft, TInRight, TOut> > expr,
            OptimisingEmitter <TEmit> emitter,
            ExceptionBlock errorLabel,
            Value?leftConst,
            Value?rightConst
            )
        {
            if (expr.ReturnType == typeof(StaticError))
            {
                return(new ConvertResult(typeof(StaticError), null, null));
            }

            // Try to convert expression without putting things into locals. Only works with certain expressions.
            var fast = TryConvertBinaryFastPath(expr, emitter, errorLabel, leftConst, rightConst);

            if (fast != null)
            {
                return(fast.Value);
            }

            // Put the parameters into local, ready to be used later
            using var localRight = emitter.DeclareLocal(typeof(TInRight), "ConvertBinary_Right", false);
            emitter.StoreLocal(localRight);
            using var localLeft = emitter.DeclareLocal(typeof(TInLeft), "ConvertBinary_Left", false);
            emitter.StoreLocal(localLeft);
            var parameters = new Dictionary <string, Parameter> {
                { expr.Parameters[0].Name !, new Parameter(localLeft, leftConst) },
        public void EmitLoad(Line line)
        {
            // Find every variable that is loaded anywhere in the line
            var loadFinder = new FindReadVariables();

            loadFinder.Visit(line);

            // Find every variable that is written to anywhere in the line
            var storeFinder = new FindAssignedVariables();

            storeFinder.Visit(line);
            _mutated.UnionWith(storeFinder.Names);

            // Only cache things which read or written more than once in the line
            var toCache = new HashSet <VariableName>();

            foreach (var(name, count) in loadFinder.Counts)
            {
                if (count > 1)
                {
                    toCache.Add(name);
                }
            }
            foreach (var(name, count) in storeFinder.Counts)
            {
                if (count > 1)
                {
                    toCache.Add(name);
                }
            }

            // Load up everything that will be read later in the line
            // The `IsDirty` flags protect against writing back uninitialised variables at the end
            foreach (var variable in toCache)
            {
                var type  = _types.TypeOf(variable) ?? StackType.YololValue;
                var local = _emitter.DeclareLocal(type.ToType(), $"CacheFor_{variable.Name}", false);

                EmitLoadValue(variable);
                StaticUnbox(type);
                _emitter.StoreLocal(local);
                _cache.Add(variable, new TypedLocal(type, local));

                var dirty = _emitter.DeclareLocal(typeof(bool), $"CacheFor_{variable.Name}_IsDirty");
                _emitter.LoadConstant(false);
                _emitter.StoreLocal(dirty);
                _cacheDirty.Add(variable, dirty);
            }
        }
示例#3
0
 /// <summary>
 /// Call an instance method on a given type, taking zero arguments
 /// </summary>
 /// <typeparam name="TEmit"></typeparam>
 /// <typeparam name="TCallee"></typeparam>
 /// <param name="emitter"></param>
 /// <param name="methodName"></param>
 public static void CallRuntimeThis0 <TEmit, TCallee>(this OptimisingEmitter <TEmit> emitter, string methodName)
 {
     using (var local = emitter.DeclareLocal(typeof(TCallee), "CallRuntimeThis0_Callee", false))
     {
         emitter.StoreLocal(local);
         emitter.LoadLocalAddress(local, false);
         emitter.CallRuntimeN <TEmit, TCallee>(methodName);
     }
 }
示例#4
0
        /// <summary>
        /// Get the value of a field (even a private one) on a type
        /// </summary>
        /// <typeparam name="TEmit"></typeparam>
        /// <typeparam name="TType"></typeparam>
        /// <param name="emitter"></param>
        /// <param name="fieldName"></param>
        public static void GetRuntimeFieldValue <TEmit, TType>(this OptimisingEmitter <TEmit> emitter, string fieldName)
        {
            var field = typeof(TType).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static) !;

            using (var local = emitter.DeclareLocal(typeof(TType), "GetRuntimeFieldValue_Callee", false))
            {
                emitter.StoreLocal(local);
                emitter.LoadLocalAddress(local, true);
                emitter.LoadField(field);
            }
        }
示例#5
0
        /// <summary>
        /// Get the value of a property on a type
        /// </summary>
        /// <typeparam name="TEmit"></typeparam>
        /// <typeparam name="TType"></typeparam>
        /// <param name="emitter"></param>
        /// <param name="propertyName"></param>
        public static void GetRuntimePropertyValue <TEmit, TType>(this OptimisingEmitter <TEmit> emitter, string propertyName)
        {
            // ReSharper disable once PossibleNullReferenceException
            var method = typeof(TType).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static) !.GetMethod !;

            using (var local = emitter.DeclareLocal(typeof(TType), "GetRuntimePropertyValue_Callee", false))
            {
                emitter.StoreLocal(local);
                emitter.LoadLocalAddress(local, true);
                emitter.Call(method);
            }
        }
        /// <summary>
        /// Compile a line of Yolol into the given IL emitter
        /// </summary>
        /// <param name="line"></param>
        /// <param name="emit"></param>
        /// <param name="lineNumber"></param>
        /// <param name="maxLines"></param>
        /// <param name="maxStringLength"></param>
        /// <param name="internalVariableMap"></param>
        /// <param name="externalVariableMap"></param>
        /// <param name="staticTypes"></param>
        /// <param name="changeDetection"></param>
        public static void Compile(
            this Line line,
            Emit <Func <ArraySegment <Value>, ArraySegment <Value>, LineResult> > emit,
            int lineNumber,
            int maxLines,
            int maxStringLength,
            IReadonlyInternalsMap internalVariableMap,
            IReadonlyExternalsMap externalVariableMap,
            IReadOnlyDictionary <VariableName, Type>?staticTypes = null,
            bool changeDetection = false
            )
        {
            using (var emitter = new OptimisingEmitter <Func <ArraySegment <Value>, ArraySegment <Value>, LineResult> >(emit))
            {
                void EmitFallthroughCalc()
                {
                    if (lineNumber == maxLines)
                    {
                        emitter.LoadConstant(1);
                    }
                    else
                    {
                        emitter.LoadConstant(lineNumber + 1);
                    }
                }

                // Special case for totally empty lines
                if (line.Statements.Statements.Count == 0)
                {
                    EmitFallthroughCalc();
                    emitter.LoadConstant(0ul);
                    emitter.NewObject <ChangeSet, ulong>();
                    emitter.NewObject(typeof(LineResult).GetConstructor(new[] { typeof(int), typeof(ChangeSet) }) !);
                    emitter.Return();
                    return;
                }

                // Begin an exception block to catch Yolol runtime errors
                var exBlock = emitter.BeginExceptionBlock();

                // Create a local to store the return address from inside the try/catch block
                var retAddr = emitter.DeclareLocal <int>("ret_addr", initializeReused: false);

                // Create a local for the change bit set
                var changeSet = changeDetection ? emitter.DeclareLocal <ulong>("change_set") : null;

                // Store the default return address to go to
                EmitFallthroughCalc();
                emitter.StoreLocal(retAddr);

                // Create a label which any `goto` statements can use. They drop their destination PC on the stack and then jump to this label
                var gotoLabel = emitter.DefineLabel2("encountered_goto");

                var types = new StaticTypeTracker(staticTypes);

                // Create a memory accessor which manages reading and writing the memory arrays
                using (var accessor = new ArraySegmentMemoryAccessor <Func <ArraySegment <Value>, ArraySegment <Value>, LineResult> >(
                           emitter,
                           1,
                           0,
                           internalVariableMap,
                           externalVariableMap,
                           types,
                           changeSet
                           ))
                {
                    accessor.EmitLoad(line);

                    // Convert the entire line into IL
                    var converter = new ConvertLineVisitor <Func <ArraySegment <Value>, ArraySegment <Value>, LineResult> >(emitter, maxLines, accessor, exBlock, gotoLabel, types, maxStringLength);
                    converter.Visit(line);

                    // When a line finishes (with no gotos in the line) call flow eventually reaches here. Go to the next line.
                    emitter.Leave(exBlock);

                    // Create a block to handle gotos. The destination will already be on the stack, so just return
                    if (gotoLabel.IsUsed)
                    {
                        emitter.MarkLabel(gotoLabel);
                        emitter.StoreLocal(retAddr);
                        emitter.Leave(exBlock);
                    }

                    // Catch all execution exceptions and return the appropriate next line number to fall through to
                    var catchBlock = emitter.BeginCatchBlock <ExecutionException>(exBlock);
#if DEBUG
                    using (var ex = emitter.DeclareLocal(typeof(ExecutionException), initializeReused: false))
                    {
                        emitter.StoreLocal(ex);
                        emitter.WriteLine("execution exception: {0}", ex);
                    }
#else
                    emitter.Pop();
#endif

                    // Close the exception block which was wrapping the entire method
                    emitter.EndCatchBlock(catchBlock);
                    emitter.EndExceptionBlock(exBlock);
                }

                // Load the return address from inside the catch block
                emitter.LoadLocal(retAddr);

                // Create the change set, either from the computer value or the default value (which indicates that everything has changed)
                if (changeSet != null)
                {
                    emitter.LoadLocal(changeSet);
                }
                else
                {
                    emitter.LoadConstant(ulong.MaxValue);
                }
                emitter.NewObject <ChangeSet, ulong>();

                emitter.NewObject(typeof(LineResult).GetConstructor(new[] { typeof(int), typeof(ChangeSet) }) !);
                emitter.Return();

                emitter.Optimise();

#if DEBUG
                Console.WriteLine($"// {line}");
                Console.WriteLine(emitter.ToString());
                Console.WriteLine($"------------------------------");
#endif
            }
        }