/// <summary> /// Emit code to dynamically check for errors. If an error would occur clear out the stack and jump away to the given label /// </summary> /// <typeparam name="TEmit"></typeparam> /// <param name="emitter"></param> /// <param name="errorLabel"></param> /// <param name="parameters"></param> public void EmitDynamicWillThrow <TEmit>(OptimisingEmitter <TEmit> emitter, Compiler.Emitter.Instructions.ExceptionBlock errorLabel, IReadOnlyList <Local> parameters) { // Load parameters back onto stack for (var i = parameters.Count - 1; i >= 0; i--) { emitter.LoadLocal(parameters[i]); } // Invoke the `will throw` method to discover if this invocation would trigger a runtime error emitter.Call(WillThrow); // Create a label to jump past the error handling for the normal case var noThrowLabel = emitter.DefineLabel(); // Jump past error handling if this is ok emitter.BranchIfFalse(noThrowLabel); // If execution reaches here it means an error would occur in this operation emitter.Leave(errorLabel); emitter.MarkLabel(noThrowLabel); }
/// <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 } }