public RuntimeCore(Heap heap) { // The heap is initialized by the core and is used to allocate strings // Use the that heap for runtime Validity.Assert(heap != null); this.Heap = heap; RuntimeMemory = new RuntimeMemory(Heap); InterpreterProps = new Stack<InterpreterProperties>(); ReplicationGuides = new List<List<ReplicationGuide>>(); RunningBlock = 0; ExecutionState = (int)ExecutionStateEventArgs.State.kInvalid; //not yet started FFIPropertyChangedMonitor = new FFIPropertyChangedMonitor(this); ContinuationStruct = new ContinuationStructure(); watchStack = new List<StackValue>(); watchFramePointer = Constants.kInvalidIndex; WatchSymbolList = new List<SymbolNode>(); FunctionCallDepth = 0; cancellationPending = false; watchClassScope = Constants.kInvalidIndex; ExecutionInstance = CurrentExecutive = new Executive(this); ExecutiveProvider = new ExecutiveProvider(); RuntimeStatus = new ProtoCore.RuntimeStatus(this); StartPC = Constants.kInvalidPC; RuntimeData = new ProtoCore.RuntimeData(); }
public string GetType(string name) { RuntimeMemory rmem = MirrorTarget.rmem; int classcope; int block = MirrorTarget.RuntimeCore.RunningBlock; SymbolNode symbol; int index = GetSymbolIndex(name, out classcope, ref block, out symbol); StackValue val; if (symbol.functionIndex == -1 && classcope != Constants.kInvalidIndex) { val = rmem.GetMemberData(index, classcope, runtimeCore.DSExecutable); } else { val = rmem.GetSymbolValue(symbol); } if (val.IsInteger) { return("int"); } else if (val.IsDouble) { return("double"); } else if (val.IsNull) { return("null"); } else if (val.IsPointer) { int classtype = val.metaData.type; ClassNode classnode = runtimeCore.DSExecutable.classTable.ClassNodes[classtype]; return(classnode.Name); } else if (val.IsArray) { return("array"); } else if (val.IsBoolean) { return("bool"); } else if (val.IsString) { return("string"); } else { return("null"); // "Value not yet supported for tracing"; } }
/// <summary> /// array[index1][index2][...][indexN] = value, and /// indices = {index1, index2, ..., indexN} /// </summary> /// <param name="array"></param> /// <param name="indices"></param> /// <param name="value"></param> /// <param name="t"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue SetValueForIndices(StackValue array, List <StackValue> indices, StackValue value, Type t, RuntimeCore runtimeCore) { RuntimeMemory rmem = runtimeCore.RuntimeMemory; StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, runtimeCore); if (zippedIndices == null || zippedIndices.Length == 0) { return(StackValue.Null); } if (zippedIndices.Length == 1) { StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); return(ArrayUtils.SetValueForIndices(array, zippedIndices[0], coercedData, runtimeCore)); } if (t.rank > 0) { t.rank = t.rank - 1; } if (value.IsArray) { // Replication happens on both side. HeapElement dataHeapElement = GetHeapElement(value, runtimeCore); int length = Math.Min(zippedIndices.Length, dataHeapElement.VisibleSize); StackValue[] oldValues = new StackValue[length]; for (int i = 0; i < length; ++i) { StackValue coercedData = TypeSystem.Coerce(dataHeapElement.Stack[i], t, runtimeCore); oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs return(rmem.Heap.AllocateArray(oldValues, null)); } else { // Replication is only on the LHS, so collect all old values // and return them in an array. StackValue coercedData = TypeSystem.Coerce(value, t, runtimeCore); StackValue[] oldValues = new StackValue[zippedIndices.Length]; for (int i = 0; i < zippedIndices.Length; ++i) { oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, runtimeCore); } // The returned old values shouldn't have any key-value pairs return(rmem.Heap.AllocateArray(oldValues, null)); } }
/// <summary> /// Setup before execution /// This function needs to be called before attempting to execute the RuntimeCore /// It will initialize the runtime execution data and configuration /// </summary> /// <param name="compileCore"></param> /// <param name="isCodeCompiled"></param> /// <param name="context"></param> public void SetupForExecution(ProtoCore.Core compileCore, int globalStackFrameSize) { if (globalStackFrameSize > 0) { RuntimeMemory.PushFrameForGlobals(globalStackFrameSize); } RunningBlock = 0; RuntimeStatus.MessageHandler = compileCore.BuildStatus.MessageHandler; WatchSymbolList = compileCore.watchSymbolList; SetProperties(compileCore.Options, compileCore.DSExecutable, compileCore.DebuggerProperties, null, compileCore.ExprInterpreterExe); RegisterDllTypes(compileCore.DllTypesToLoad); NotifyExecutionEvent(ProtoCore.ExecutionStateEventArgs.State.kExecutionBegin); }
public Expression GetExpression() { var methodKey = GetMethodKey(); var lamdaExpression = RuntimeMemory.GetMethod(invocationStatement.Method.ParentType, methodKey); if (lamdaExpression != null) { return(InvokeLamdaExpression(lamdaExpression)); } return(InvokeMethod()); }
private static int ReadInt32() { var buf = new byte[4]; buf[0] = ReadByte(); buf[1] = ReadByte(); buf[2] = ReadByte(); buf[3] = ReadByte(); var result = BitConversion.GetInt32(buf); RuntimeMemory.FreeObject(buf); return(result); }
// traverse an class type object to get its property public Dictionary <string, Obj> GetProperties(Obj obj, bool excludeStatic = false) { RuntimeMemory rmem = MirrorTarget.rmem; if (obj == null || !obj.DsasmValue.IsPointer) { return(null); } Dictionary <string, Obj> ret = new Dictionary <string, Obj>(); int classIndex = obj.DsasmValue.metaData.type; IDictionary <int, SymbolNode> symbolList = runtimeCore.DSExecutable.classTable.ClassNodes[classIndex].Symbols.symbolList; StackValue[] svs = rmem.Heap.ToHeapObject <DSObject>(obj.DsasmValue).Values.ToArray(); int index = 0; for (int ix = 0; ix < svs.Length; ++ix) { if (excludeStatic && symbolList[ix].isStatic) { continue; } string name = symbolList[ix].name; StackValue val = svs[index]; // check if the members are primitive type if (val.IsPointer) { var pointer = rmem.Heap.ToHeapObject <DSObject>(val); var firstItem = pointer.Count == 1 ? pointer.GetValueFromIndex(0, runtimeCore) : StackValue.Null; if (pointer.Count == 1 && !firstItem.IsPointer && !firstItem.IsArray) { val = firstItem; } } ret[name] = Unpack(val); index++; } return(ret); }
/// <summary> /// Retrieve the first non-array element in an array /// </summary> /// <param name="svArray"></param> /// <param name="sv"></param> /// <param name="core"></param> /// <returns> true if the element was found </returns> public static bool GetFirstNonArrayStackValue(StackValue svArray, ref StackValue sv, RuntimeCore runtimeCore) { RuntimeMemory rmem = runtimeCore.RuntimeMemory; if (!svArray.IsArray) { return(false); } var svFound = GetFirstNonArrayStackValueRecursive(svArray, runtimeCore); if (svFound.HasValue) { sv = svFound.Value.ShallowClone(); return(true); } return(false); }
/// <summary> /// Try to get value for key from nested dictionaries. This function is /// used in the case that indexing into dictionaries that returned from /// a replicated function whose return type is dictionary. /// </summary> /// <param name="array"></param> /// <param name="key"></param> /// <param name="value"></param> /// <param name="core"></param> /// <returns></returns> public static bool TryGetValueFromNestedDictionaries(StackValue array, StackValue key, out StackValue value, RuntimeCore runtimeCore) { RuntimeMemory rmem = runtimeCore.RuntimeMemory; if (!array.IsArray) { value = StackValue.Null; return(false); } HeapElement he = GetHeapElement(array, runtimeCore); if (he.Dict != null && he.Dict.TryGetValue(key, out value)) { return(true); } var values = new List <StackValue>(); bool hasValue = false; foreach (var element in he.VisibleItems) { StackValue valueInElement; if (TryGetValueFromNestedDictionaries(element, key, out valueInElement, runtimeCore)) { hasValue = true; values.Add(valueInElement); } } if (hasValue) { value = rmem.Heap.AllocateArray(values, null); return(true); } else { value = StackValue.Null; return(false); } }
/// <summary> /// Copy an array and coerce its elements/values to target type /// </summary> /// <param name="array"></param> /// <param name="type"></param> /// <param name="core"></param> /// <returns></returns> public static StackValue CopyArray(StackValue array, Type type, RuntimeCore runtimeCore) { Validity.Assert(array.IsArray); RuntimeMemory rmem = runtimeCore.RuntimeMemory; if (!array.IsArray) { return(StackValue.Null); } HeapElement he = GetHeapElement(array, runtimeCore); Validity.Assert(he != null); int elementSize = GetElementSize(array, runtimeCore); StackValue[] elements = new StackValue[elementSize]; for (int i = 0; i < elementSize; i++) { StackValue coercedValue = TypeSystem.Coerce(he.Stack[i], type, runtimeCore); elements[i] = coercedValue; } Dictionary <StackValue, StackValue> dict = null; if (he.Dict != null) { dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(runtimeCore)); foreach (var pair in he.Dict) { StackValue key = pair.Key; StackValue value = pair.Value; StackValue coercedValue = TypeSystem.Coerce(value, type, runtimeCore); dict[key] = coercedValue; } } return(rmem.Heap.AllocateArray(elements, dict)); }
public RuntimeCore(Heap heap, Options options = null, Executable executable = null) { // The heap is initialized by the core and is used to allocate strings // Use the that heap for runtime Validity.Assert(heap != null); this.Heap = heap; RuntimeMemory = new RuntimeMemory(Heap); this.Options = options; InterpreterProps = new Stack <InterpreterProperties>(); ReplicationGuides = new List <List <ReplicationGuide> >(); AtLevels = new List <AtLevel>(); executedAstGuids = new HashSet <Guid>(); RunningBlock = 0; ExecutionState = (int)ExecutionStateEventArgs.State.Invalid; //not yet started ContinuationStruct = new ContinuationStructure(); watchStack = new List <StackValue>(); watchFramePointer = Constants.kInvalidIndex; WatchSymbolList = new List <SymbolNode>(); FunctionCallDepth = 0; cancellationPending = false; watchClassScope = Constants.kInvalidIndex; ExecutionInstance = CurrentExecutive = new Executive(this); ExecutiveProvider = new ExecutiveProvider(); RuntimeStatus = new ProtoCore.RuntimeStatus(this); StartPC = Constants.kInvalidPC; RuntimeData = new ProtoCore.RuntimeData(); DSExecutable = executable; Mirror = null; }
public static unsafe void StartProcess(string path) { if (SharedDisk == null) { return; } var fileSize = (uint)GetFileLenth(path); var target = SysCalls.GetProcessIDForCommand(SysCallTarget.CreateMemoryProcess); var fileBuf = SysCalls.RequestMessageBuffer((uint)fileSize, target); var handle = OpenFile(path); var bufSize = 128 * 1024u; //var bufSize = KMath.AlignValueCeil(fileSize, 512); var buf = (byte *)RuntimeMemory.Allocate(bufSize); var gotBytes = (uint)ReadFile(handle, buf, bufSize); var fileBufPos = 0u; //SysCalls.SetThreadPriority(30); while (gotBytes > 0) { //Console.WriteLine("got data"); for (var i = 0; i < gotBytes; i++) { ((byte *)fileBuf.Start)[fileBufPos + i] = buf[i]; } fileBufPos += gotBytes; gotBytes = (uint)ReadFile(handle, buf, bufSize); } //SysCalls.SetThreadPriority(0); RuntimeMemory.Free(buf); var cs = fileBuf.Checksum(); Console.WriteLine(cs.ToString("x")); Console.WriteLine("Starting process ..."); SysCalls.CreateMemoryProcess(fileBuf, fileSize); }
public Expression GetExpression() { var suppliedArgs = invocationStatement.Arguments; var arguments = invocationStatement.Arguments .Select(x => expressionInterpreterHandler.GetExpression(x)).ToArray(); var parameterTypes = invocationStatement.ParametersSignature.Select(x => (Type)x).ToArray(); var methodKey = GetMethodKey(); var lamdaExpression = RuntimeMemory.GetMethod(invocationStatement.Method.ParentType, methodKey); var instanceMember = invocationStatement.Method as InstanceMethodMemberStatement; if (lamdaExpression != null) { var expression = lamdaExpression.GetExpression(); var arguments1 = new List <Expression>(); if (instanceMember != null) { arguments1.Add(expressionInterpreterHandler.GetExpression(instanceMember.Parent)); } arguments1.AddRange(arguments); return(Expression.Invoke(expression, arguments1)); } BindingFlags bindingFlags = instanceMember == null ? BindingFlags.Static : BindingFlags.Instance; bindingFlags |= invocationStatement.Method.AccessModifier == HotReloading.Core.AccessModifier.Public ? BindingFlags.Public : BindingFlags.NonPublic; Type declareType = (Type)invocationStatement.Method.ParentType; var methodInfo = declareType.GetMethod(invocationStatement.Method.Name, bindingFlags, Type.DefaultBinder, parameterTypes, null); Expression[] convertedArguments = new Expression[arguments.Length]; for (int i = 0; i < convertedArguments.Length; i++) { var argument = arguments[i]; var parameter = methodInfo.GetParameters()[i]; if (parameter.ParameterType != argument.Type) { convertedArguments[i] = Expression.Convert(argument, parameter.ParameterType); } else { convertedArguments[i] = argument; } } if (instanceMember != null) { var caller = expressionInterpreterHandler.GetExpression(instanceMember.Parent); return(Expression.Call( caller, methodInfo, convertedArguments)); } return(Expression.Call(methodInfo, convertedArguments)); }
private int GetSymbolIndex(string name, out int ci, ref int block, out SymbolNode symbol) { RuntimeMemory rmem = MirrorTarget.rmem; ProtoCore.DSASM.Executable exe = runtimeCore.DSExecutable; int functionIndex = Constants.kGlobalScope; ci = Constants.kInvalidIndex; int functionBlock = Constants.kGlobalScope; if (runtimeCore.DebugProps.DebugStackFrameContains(DebugProperties.StackFrameFlagOptions.FepRun)) { ci = runtimeCore.watchClassScope = rmem.CurrentStackFrame.ClassScope; functionIndex = rmem.CurrentStackFrame.FunctionScope; functionBlock = rmem.CurrentStackFrame.FunctionBlock; } // TODO Jun: 'block' is incremented only if there was no other block provided by the programmer // This is only to address NUnit issues when retrieving a global variable // Some predefined functions are hard coded in the AST so isSingleAssocBlock will never be true //if (exe.isSingleAssocBlock) //{ // ++block; //} int index = -1; if (ci != Constants.kInvalidIndex) { ClassNode classnode = runtimeCore.DSExecutable.classTable.ClassNodes[ci]; if (functionIndex != ProtoCore.DSASM.Constants.kInvalidIndex && functionBlock != runtimeCore.RunningBlock) { index = exe.runtimeSymbols[block].IndexOf(name, Constants.kGlobalScope, Constants.kGlobalScope); } if (index == Constants.kInvalidIndex) { index = classnode.Symbols.IndexOfClass(name, ci, functionIndex); } if (index != Constants.kInvalidIndex) { symbol = classnode.Symbols.symbolList[index]; return(index); } } else { CodeBlock searchBlock = runtimeCore.DSExecutable.CompleteCodeBlocks[block]; // To detal with the case that a language block defined in a function // // def foo() // { // [Imperative] // { // a; // } // } if (functionIndex != ProtoCore.DSASM.Constants.kInvalidIndex) { if (searchBlock.IsMyAncestorBlock(functionBlock)) { while (searchBlock.codeBlockId != functionBlock) { index = exe.runtimeSymbols[searchBlock.codeBlockId].IndexOf(name, ci, ProtoCore.DSASM.Constants.kInvalidIndex); if (index == ProtoCore.DSASM.Constants.kInvalidIndex) { searchBlock = searchBlock.parent; } else { break; } } } if (index == ProtoCore.DSASM.Constants.kInvalidIndex) { index = exe.runtimeSymbols[searchBlock.codeBlockId].IndexOf(name, ci, functionIndex); } if (index == ProtoCore.DSASM.Constants.kInvalidIndex) { index = exe.runtimeSymbols[searchBlock.codeBlockId].IndexOf(name, ci, ProtoCore.DSASM.Constants.kInvalidIndex); } } else { index = exe.runtimeSymbols[searchBlock.codeBlockId].IndexOf(name, ci, ProtoCore.DSASM.Constants.kInvalidIndex); } if (index == ProtoCore.DSASM.Constants.kInvalidIndex) { searchBlock = searchBlock.parent; while (searchBlock != null) { block = searchBlock.codeBlockId; index = exe.runtimeSymbols[searchBlock.codeBlockId].IndexOf(name, ci, ProtoCore.DSASM.Constants.kInvalidIndex); if (index != ProtoCore.DSASM.Constants.kInvalidIndex) { break; } else { searchBlock = searchBlock.parent; } } } if (index != ProtoCore.DSASM.Constants.kInvalidIndex) { block = searchBlock.codeBlockId; symbol = exe.runtimeSymbols[searchBlock.codeBlockId].symbolList[index]; return(index); } } throw new NameNotFoundException { Name = name }; //throw new NotImplementedException("{F5ACC95F-AEC9-486D-BC82-FF2CB26E7E6A}"); //@TODO(Luke): Replace this with a symbol lookup exception }
private string GetGlobalVarTrace(List <string> variableTraces) { // Prints out the final Value of every symbol in the program // Traverse order: // Exelist, Globals symbols StringBuilder globaltrace = null; if (null == variableTraces) { globaltrace = new StringBuilder(); } ProtoCore.DSASM.Executable exe = MirrorTarget.exe; // Only display symbols defined in the default top-most langauge block; // Otherwise garbage information may be displayed. if (exe.runtimeSymbols.Length > 0) { int blockId = 0; // when this code block is of type construct, such as if, else, while, all the symbols inside are local //if (exe.instrStreamList[blockId] == null) // continue; SymbolTable symbolTable = exe.runtimeSymbols[blockId]; for (int i = 0; i < symbolTable.symbolList.Count; ++i) { formatParams.ResetOutputDepth(); SymbolNode symbolNode = symbolTable.symbolList[i]; bool isLocal = Constants.kGlobalScope != symbolNode.functionIndex; bool isStatic = (symbolNode.classScope != Constants.kInvalidIndex && symbolNode.isStatic); if (symbolNode.isArgument || isLocal || isStatic || symbolNode.isTemp) { // These have gone out of scope, their values no longer exist continue; } RuntimeMemory rmem = MirrorTarget.rmem; StackValue sv = rmem.GetSymbolValue(symbolNode); string formattedString = GetFormattedValue(symbolNode.name, GetStringValue(sv, rmem.Heap, blockId)); if (null != globaltrace) { int maxLength = 1020; while (formattedString.Length > maxLength) { globaltrace.AppendLine(formattedString.Substring(0, maxLength)); formattedString = formattedString.Remove(0, maxLength); } globaltrace.AppendLine(formattedString); } if (null != variableTraces) { variableTraces.Add(formattedString); } } formatParams.ResetOutputDepth(); } return((null == globaltrace) ? string.Empty : globaltrace.ToString()); }
public string GetClassTrace(StackValue val, Heap heap, int langblock, bool forPrint) { if (!formatParams.ContinueOutputTrace()) { return("..."); } RuntimeMemory rmem = MirrorTarget.rmem; Executable exe = MirrorTarget.exe; ClassTable classTable = MirrorTarget.RuntimeCore.DSExecutable.classTable; int classtype = val.metaData.type; if (classtype < 0 || (classtype >= classTable.ClassNodes.Count)) { formatParams.RestoreOutputTraceDepth(); return(string.Empty); } ClassNode classnode = classTable.ClassNodes[classtype]; if (classnode.IsImportedClass) { var helper = DLLFFIHandler.GetModuleHelper(FFILanguage.CSharp); var marshaller = helper.GetMarshaller(runtimeCore); var strRep = marshaller.GetStringValue(val); formatParams.RestoreOutputTraceDepth(); return(strRep); } else { var obj = heap.ToHeapObject <DSObject>(val); List <string> visibleProperties = null; if (null != propertyFilter) { if (!propertyFilter.TryGetValue(classnode.Name, out visibleProperties)) { visibleProperties = null; } } StringBuilder classtrace = new StringBuilder(); if (classnode.Symbols != null && classnode.Symbols.symbolList.Count > 0) { bool firstPropertyDisplayed = false; for (int n = 0; n < obj.Count; ++n) { SymbolNode symbol = classnode.Symbols.symbolList[n]; string propName = symbol.name; if ((null != visibleProperties) && visibleProperties.Contains(propName) == false) { continue; // This property is not to be displayed. } if (firstPropertyDisplayed) { classtrace.Append(", "); } string propValue = ""; if (symbol.isStatic) { var staticSymbol = exe.runtimeSymbols[langblock].symbolList[symbol.symbolTableIndex]; StackValue staticProp = rmem.GetSymbolValue(staticSymbol); propValue = GetStringValue(staticProp, heap, langblock, forPrint); } else { propValue = GetStringValue(obj.GetValueFromIndex(symbol.index, runtimeCore), heap, langblock, forPrint); } classtrace.Append(string.Format("{0} = {1}", propName, propValue)); firstPropertyDisplayed = true; } } else { var stringValues = obj.Values.Select(x => GetStringValue(x, heap, langblock, forPrint)) .ToList(); for (int n = 0; n < stringValues.Count(); ++n) { if (0 != n) { classtrace.Append(", "); } classtrace.Append(stringValues[n]); } } formatParams.RestoreOutputTraceDepth(); if (classtype >= (int)ProtoCore.PrimitiveType.MaxPrimitive) { if (forPrint) { return(string.Format("{0}{{{1}}}", classnode.Name, classtrace.ToString())); } else { string tempstr = (string.Format("{0}({1})", classnode.Name, classtrace.ToString())); return(tempstr); } } return(classtrace.ToString()); } }
public void Dispose() { RuntimeMemory.FreeObject(Data); }
public static void Setup() { Tracker.Reset(); RuntimeMemory.Reset(); }
public static void Reset() { RuntimeMemory.Reset(); Tracker.LastValue = null; }
public unsafe ConsoleBuffer(ConsoleDevice dev) { Rows = dev.Rows; Columns = dev.Columns; Chars = (ConsoleChar *)RuntimeMemory.AllocateCleared(sizeof(ConsoleChar) * Rows * Columns); }