Ejemplo n.º 1
0
        public virtual VMRoutine Assemble(BHAV bhav)
        {
            var routine = new VMRoutine();

            PopulateRoutineFields(bhav, routine);
            return(routine);
        }
Ejemplo n.º 2
0
        public virtual VMRoutine Assemble(BHAV bhav, GameIffResource res)
        {
            var routine = new VMRoutine();

            PopulateRoutineFields(bhav, routine);
            return(routine);
        }
Ejemplo n.º 3
0
        public virtual void Load(VMStackFrameMarshal input, VMContext context)
        {
            CodeOwner = FSO.Content.Content.Get().WorldObjects.Get(input.CodeOwnerGUID);

            Routine = null;
            if (input.RoutineID >= 8192)
            {
                Routine = (VMRoutine)ScopeResource.SemiGlobal.GetRoutine(input.RoutineID);
            }
            else if (input.RoutineID >= 4096)
            {
                Routine = (VMRoutine)ScopeResource.GetRoutine(input.RoutineID);
            }
            else
            {
                Routine = (VMRoutine)Global.Resource.GetRoutine(input.RoutineID);
            }

            InstructionPointer = (byte)input.InstructionPointer;
            Caller             = context.VM.GetObjectById(input.Caller);
            Callee             = context.VM.GetObjectById(input.Callee);
            StackObjectID      = input.StackObject;
            if (Routine != null && input.Locals != null && Routine.Locals > input.Locals.Length)
            {
                Locals = new short[Routine.Locals];
                Array.Copy(input.Locals, Locals, input.Locals.Length);
            }
            else
            {
                Locals = input.Locals;
            }
            Args          = input.Args;
            DiscardResult = input.DiscardResult;
            ActionTree    = input.ActionTree;
        }
Ejemplo n.º 4
0
        public void Load(VMQueuedActionMarshal input, VMContext context)
        {
            CodeOwner = FSO.Content.Content.Get().WorldObjects.Get(input.CodeOwnerGUID);

            BHAV bhav = null;

            if (input.RoutineID >= 8192)
            {
                bhav = CodeOwner.Resource.SemiGlobal.Get <BHAV>(input.RoutineID);
            }
            else if (input.RoutineID >= 4096)
            {
                bhav = CodeOwner.Resource.Get <BHAV>(input.RoutineID);
            }
            else
            {
                bhav = context.Globals.Resource.Get <BHAV>(input.RoutineID);
            }
            Routine = context.VM.Assemble(bhav);

            Callee            = context.VM.GetObjectById(input.Callee);
            StackObject       = context.VM.GetObjectById(input.StackObject);
            IconOwner         = context.VM.GetObjectById(input.IconOwner);
            Name              = input.Name;
            Args              = input.Args;
            InteractionNumber = input.InteractionNumber;
            Cancelled         = input.Cancelled;
            Priority          = input.Priority;
            Mode              = input.Mode;
            Flags             = input.Flags;
            UID      = input.UID;
            Callback = (input.Callback == null)?null:new VMActionCallback(input.Callback, context);
        }
Ejemplo n.º 5
0
        public virtual void Load(VMStackFrameMarshal input, VMContext context)
        {
            CodeOwner = FSO.Content.Content.Get().WorldObjects.Get(input.CodeOwnerGUID);

            BHAV bhav = null;

            if (input.RoutineID >= 8192)
            {
                bhav = ScopeResource.SemiGlobal.Get <BHAV>(input.RoutineID);
            }
            else if (input.RoutineID >= 4096)
            {
                bhav = ScopeResource.Get <BHAV>(input.RoutineID);
            }
            else
            {
                bhav = Global.Resource.Get <BHAV>(input.RoutineID);
            }
            Routine = VM.Assemble(bhav);

            InstructionPointer = input.InstructionPointer;
            Caller             = context.VM.GetObjectById(input.Caller);
            Callee             = context.VM.GetObjectById(input.Callee);
            StackObject        = context.VM.GetObjectById(input.StackObject);
            Locals             = input.Locals;
            Args = input.Args;
        }
Ejemplo n.º 6
0
        public virtual void Load(VMStackFrameMarshal input, VMContext context)
        {
            CodeOwner = GameContent.Get.WorldObjects.Get(input.CodeOwnerGUID);

            Routine = null;
            if (input.RoutineID >= 8192)
            {
                Routine = (VMRoutine)ScopeResource.SemiGlobal.GetRoutine(input.RoutineID);
            }
            else if (input.RoutineID >= 4096)
            {
                Routine = (VMRoutine)ScopeResource.GetRoutine(input.RoutineID);
            }
            else
            {
                Routine = (VMRoutine)Global.Resource.GetRoutine(input.RoutineID);
            }

            InstructionPointer = input.InstructionPointer;
            Caller             = context.VM.GetObjectById(input.Caller);
            Callee             = context.VM.GetObjectById(input.Callee);
            StackObjectID      = input.StackObject;
            Locals             = input.Locals;
            Args          = input.Args;
            DiscardResult = input.DiscardResult;
            ActionTree    = input.ActionTree;
        }
Ejemplo n.º 7
0
        public void ExecuteSubRoutine(VMStackFrame frame, VMRoutine routine, GameObject codeOwner, VMSubRoutineOperand args)
        {
            if (routine == null)
            {
                Pop(VMPrimitiveExitCode.ERROR);
                return;
            }

            var childFrame = new VMStackFrame
            {
                Routine        = routine,
                Caller         = frame.Caller,
                Callee         = frame.Callee,
                CodeOwner      = codeOwner,
                StackObject    = frame.StackObject,
                _StackObjectID = frame.StackObjectID, //pass this without doing a lookup
                ActionTree     = frame.ActionTree
            };

            childFrame.Args = new short[(routine.Arguments > 4) ? routine.Arguments : 4];
            for (var i = 0; i < childFrame.Args.Length; i++)
            {
                short argValue = (i > 3) ? (short)-1 : args.Arguments[i];
                if (argValue == -1 && args.UseTemp0)
                {
                    argValue = TempRegisters[i];
                }
                childFrame.Args[i] = argValue;
            }
            Push(childFrame);
        }
Ejemplo n.º 8
0
        private void ExecuteInstruction(VMStackFrame frame)
        {
            var instruction = frame.GetCurrentInstruction();
            var opcode      = instruction.Opcode;

            if (opcode >= 256)
            {
                VMRoutine bhav = null;

                GameObject CodeOwner;
                if (opcode >= 8192)
                {
                    // Semi-Global sub-routine call
                    bhav = (VMRoutine)frame.ScopeResource.SemiGlobal.GetRoutine(opcode);
                }
                else if (opcode >= 4096)
                {
                    // Private sub-routine call
                    bhav = (VMRoutine)frame.ScopeResource.GetRoutine(opcode);
                }
                else
                {
                    // Global sub-routine call
                    //CodeOwner = frame.Global.Resource;
                    bhav = (VMRoutine)frame.Global.Resource.GetRoutine(opcode);
                }

                CodeOwner = frame.CodeOwner;

                var operand = (VMSubRoutineOperand)instruction.Operand;
                ExecuteSubRoutine(frame, bhav, CodeOwner, operand);
#if IDE_COMPAT
                if (Stack.LastOrDefault().GetCurrentInstruction().Breakpoint || ThreadBreak == VMThreadBreakMode.StepIn)
                {
                    Breakpoint(frame, "Stepped in.");
                    ContinueExecution = false;
                }
                else
#endif
                {
                    ContinueExecution = true;
                }

                return;
            }


            var primitive = VMContext.Primitives[opcode];
            if (primitive == null)
            {
                HandleResult(frame, instruction, VMPrimitiveExitCode.GOTO_TRUE);
                return;
            }

            VMPrimitiveHandler handler = primitive.GetHandler();
            var result = handler.Execute(frame, instruction.Operand);
            HandleResult(frame, instruction, result);
        }
Ejemplo n.º 9
0
        public VMRoutineInspector(VMRoutine routine)
        {
            this.Routine = routine;

            InitializeComponent();


            this.Text = routine.ID + " " + routine.ToString();
        }
Ejemplo n.º 10
0
        public static VMRoutine Assemble(VM vm, BHAV bhav)
        {
            var context = vm.Context;

            var routine = new VMRoutine();

            routine.Locals    = bhav.Locals;
            routine.Arguments = bhav.Args;
            routine.Type      = bhav.Type;
            routine.ID        = bhav.ChunkID;
            routine.Chunk     = bhav;
            routine.VM        = vm;
            routine.Rti       = new VMFunctionRTI {
                Name = bhav.ChunkLabel
            };

            VMInstruction[] instructions = new VMInstruction[bhav.Instructions.Length];
            for (var i = 0; i < bhav.Instructions.Length; i++)
            {
                var bhavInstruction = bhav.Instructions[i];
                var instruction     = new VMInstruction();

                instruction.Index        = (byte)i;
                instruction.Opcode       = bhavInstruction.Opcode;
                instruction.Operand      = null;
                instruction.FalsePointer = bhavInstruction.FalsePointer;
                instruction.TruePointer  = bhavInstruction.TruePointer;
                instruction.Breakpoint   = bhavInstruction.Breakpoint;
                instruction.Function     = routine;

                /** Routine call **/
                if (instruction.Opcode >= 256)
                {
                    var operand = new VMSubRoutineOperand();
                    operand.Read(bhavInstruction.Operand);
                    instruction.Operand = operand;
                }
                else
                {
                    var primitive = context.Primitives[instruction.Opcode];
                    if (primitive != null)
                    {
                        if (primitive.OperandModel != null)
                        {
                            VMPrimitiveOperand operand = (VMPrimitiveOperand)Activator.CreateInstance(primitive.OperandModel);
                            operand.Read(bhavInstruction.Operand);
                            instruction.Operand = operand;
                        }
                    }
                }
                instructions[i] = instruction;
            }
            routine.Instructions = instructions;
            return(routine);
        }
Ejemplo n.º 11
0
        public static VMRoutine Assemble(VM vm, BHAV bhav)
        {
            var context = vm.Context;

            var routine = new VMRoutine();
            routine.Locals = bhav.Locals;
            routine.Arguments = bhav.Args;
            routine.Type = bhav.Type;
            routine.ID = bhav.ChunkID;
            routine.VM = vm;
            routine.Rti = new VMFunctionRTI {
                Name = bhav.ChunkLabel
            };

            VMInstruction[] instructions = new VMInstruction[bhav.Instructions.Length];
            for (var i = 0; i < bhav.Instructions.Length; i++)
            {
                var bhavInstruction = bhav.Instructions[i];
                var instruction = new VMInstruction();

                instruction.Index = (byte)i;
                instruction.Opcode = bhavInstruction.Opcode;
                instruction.Operand = bhavInstruction.Operand;
                instruction.FalsePointer = bhavInstruction.FalsePointer;
                instruction.TruePointer = bhavInstruction.TruePointer;
                instruction.Function = routine;

                /** Routine call **/
                if (instruction.Opcode >= 256)
                {
                    var operand = new VMSubRoutineOperand();
                    operand.Read(bhavInstruction.Operand);
                    instruction.Operand = operand;
                }
                else
                {
                    var primitive = context.GetPrimitive(instruction.Opcode);
                    if (primitive != null)
                    {
                        if (primitive.OperandModel != null)
                        {
                            VMPrimitiveOperand operand = (VMPrimitiveOperand)Activator.CreateInstance(primitive.OperandModel);
                            operand.Read(bhavInstruction.Operand);
                            instruction.Operand = operand;
                        }
                    }
                }
                instructions[i] = instruction;

            }
            routine.Instructions = instructions;
            return routine;
        }
Ejemplo n.º 12
0
        protected void PopulateRoutineFields(BHAV bhav, VMRoutine routine)
        {
            routine.Locals    = bhav.Locals;
            routine.Arguments = bhav.Args;
            routine.Type      = bhav.Type;
            routine.ID        = bhav.ChunkID;
            routine.Chunk     = bhav;
            routine.Rti       = new VMFunctionRTI
            {
                Name = bhav.ChunkLabel
            };

            VMInstruction[] instructions = new VMInstruction[bhav.Instructions.Length];
            for (var i = 0; i < bhav.Instructions.Length; i++)
            {
                var bhavInstruction = bhav.Instructions[i];
                var instruction     = new VMInstruction();

                instruction.Index        = (byte)i;
                instruction.Opcode       = bhavInstruction.Opcode;
                instruction.Operand      = null;
                instruction.FalsePointer = bhavInstruction.FalsePointer;
                instruction.TruePointer  = bhavInstruction.TruePointer;
                instruction.Breakpoint   = bhavInstruction.Breakpoint;
                instruction.Function     = routine;

                /** Routine call **/
                if (instruction.Opcode >= 256)
                {
                    var operand = new VMSubRoutineOperand();
                    operand.Read(bhavInstruction.Operand);
                    instruction.Operand = operand;
                }
                else
                {
                    var primitive = VMContext.Primitives[instruction.Opcode];
                    if (primitive != null)
                    {
                        if (primitive.OperandModel != null)
                        {
                            VMPrimitiveOperand operand = (VMPrimitiveOperand)Activator.CreateInstance(primitive.OperandModel);
                            operand.Read(bhavInstruction.Operand);
                            instruction.Operand = operand;
                        }
                    }
                }
                instructions[i] = instruction;
            }
            routine.Instructions = instructions;
        }
Ejemplo n.º 13
0
        public void Load(VMQueuedActionMarshal input, VMContext context)
        {
            CodeOwner = FSO.Content.Content.Get().WorldObjects.Get(input.CodeOwnerGUID);

            if (input.RoutineID >= 8192)
            {
                ActionRoutine = (VMRoutine)CodeOwner.Resource.SemiGlobal.GetRoutine(input.RoutineID);
            }
            else if (input.RoutineID >= 4096)
            {
                ActionRoutine = (VMRoutine)CodeOwner.Resource.GetRoutine(input.RoutineID);
            }
            else
            {
                ActionRoutine = (VMRoutine)context.Globals.Resource.GetRoutine(input.RoutineID);
            }

            if (input.CheckRoutineID != 0)
            {
                if (input.CheckRoutineID >= 8192)
                {
                    CheckRoutine = (VMRoutine)CodeOwner.Resource.SemiGlobal.GetRoutine(input.CheckRoutineID);
                }
                else if (input.CheckRoutineID >= 4096)
                {
                    CheckRoutine = (VMRoutine)CodeOwner.Resource.GetRoutine(input.CheckRoutineID);
                }
                else
                {
                    CheckRoutine = (VMRoutine)context.Globals.Resource.GetRoutine(input.CheckRoutineID);
                }
            }

            Callee            = context.VM.GetObjectById(input.Callee);
            StackObject       = context.VM.GetObjectById(input.StackObject);
            IconOwner         = context.VM.GetObjectById(input.IconOwner);
            Name              = input.Name;
            Args              = input.Args;
            InteractionNumber = input.InteractionNumber;
            Cancelled         = input.Cancelled;
            Priority          = input.Priority;
            Mode              = input.Mode;
            Flags             = input.Flags;
            Flags2            = input.Flags2;
            UID      = input.UID;
            Callback = (input.Callback == null)?null:new VMActionCallback(input.Callback, context);

            InteractionResult  = input.InteractionResult;
            ResultCheckCounter = input.ResultCheckCounter;
        }
Ejemplo n.º 14
0
        public VMPrimitiveExitCode ExecuteSubRoutine(VMStackFrame frame, ushort opcode, VMSubRoutineOperand operand)
        {
            VMRoutine bhav = null;

            GameObject CodeOwner;

            if (opcode >= 8192)
            {
                // Semi-Global sub-routine call
                bhav = (VMRoutine)frame.ScopeResource.SemiGlobal.GetRoutine(opcode);
            }
            else if (opcode >= 4096)
            {
                // Private sub-routine call
                bhav = (VMRoutine)frame.ScopeResource.GetRoutine(opcode);
            }
            else
            {
                // Global sub-routine call
                //CodeOwner = frame.Global.Resource;
                bhav = (VMRoutine)frame.Global.Resource.GetRoutine(opcode);
            }

            CodeOwner = frame.CodeOwner;

            ExecuteSubRoutine(frame, bhav, CodeOwner, operand);
#if IDE_COMPAT
            if (Stack.LastOrDefault().GetCurrentInstruction().Breakpoint || ThreadBreak == VMThreadBreakMode.StepIn)
            {
                Breakpoint(frame, "Stepped in.");
                ContinueExecution = false;
            }
            else
#endif
            {
                ContinueExecution = true;
            }

            return(VMPrimitiveExitCode.CONTINUE);
        }
Ejemplo n.º 15
0
        private VMRoutine RoutineFromModule(BHAV bhav, SimAnticsModule module)
        {
            object    func = module.GetFunction(bhav.ChunkID);
            VMRoutine routine;

            if (func != null)
            {
                if (func is IBHAV)
                {
                    routine = new VMAOTRoutine((IBHAV)func);
                }
                else
                {
                    routine = new VMAOTInlineRoutine((IInlineBHAV)func);
                }
            }
            else
            {
                routine = new VMRoutine();
            }
            PopulateRoutineFields(bhav, routine);
            return(routine);
        }
Ejemplo n.º 16
0
        public override VMRoutine Assemble(BHAV bhav, GameIffResource res)
        {
            VMRoutine routine;
            var       assembly = Assemblies.GetModuleFor(bhav.ChunkParent);
            object    func     = assembly?.GetFunction(bhav.ChunkID);

            if (func != null)
            {
                if (func is IBHAV)
                {
                    routine = new VMAOTRoutine((IBHAV)func);
                }
                else
                {
                    routine = new VMAOTInlineRoutine((IInlineBHAV)func);
                }
            }
            else
            {
                routine = new VMRoutine();
            }
            PopulateRoutineFields(bhav, routine);
            return(routine);
        }
Ejemplo n.º 17
0
        public void Load(VMQueuedActionMarshal input, VMContext context)
        {
            CodeOwner = FSO.Content.Content.Get().WorldObjects.Get(input.CodeOwnerGUID);

            BHAV bhav = null;
            if (input.RoutineID >= 8192) bhav = CodeOwner.Resource.SemiGlobal.Get<BHAV>(input.RoutineID);
            else if (input.RoutineID >= 4096) bhav = CodeOwner.Resource.Get<BHAV>(input.RoutineID);
            else bhav = context.Globals.Resource.Get<BHAV>(input.RoutineID);
            ActionRoutine = context.VM.Assemble(bhav);

            if (input.CheckRoutineID != 0)
            {
                if (input.CheckRoutineID >= 8192) bhav = CodeOwner.Resource.SemiGlobal.Get<BHAV>(input.CheckRoutineID);
                else if (input.CheckRoutineID >= 4096) bhav = CodeOwner.Resource.Get<BHAV>(input.CheckRoutineID);
                else bhav = context.Globals.Resource.Get<BHAV>(input.CheckRoutineID);
                CheckRoutine = context.VM.Assemble(bhav);
            }

            Callee = context.VM.GetObjectById(input.Callee);
            StackObject = context.VM.GetObjectById(input.StackObject);
            IconOwner = context.VM.GetObjectById(input.IconOwner);
            Name = input.Name;
            Args = input.Args;
            InteractionNumber = input.InteractionNumber;
            Cancelled = input.Cancelled;
            Priority = input.Priority;
            Mode = input.Mode;
            Flags = input.Flags;
            Flags2 = input.Flags2;
            UID = input.UID;
            Callback = (input.Callback == null)?null:new VMActionCallback(input.Callback, context);
        }
Ejemplo n.º 18
0
 public VMBHAVOwnerPair(VMRoutine routine, GameObject owner)
 {
     this.bhav    = routine.Chunk;
     this.routine = routine;
     this.owner   = owner;
 }
Ejemplo n.º 19
0
 public VMRoutineInspector(VMRoutine routine)
 {
     this.Routine = routine;
     InitializeComponent();
 }
Ejemplo n.º 20
0
        public override VMRoutine Assemble(BHAV bhav, GameIffResource res)
        {
            VMRoutine routine;
            // firstly, try to find the assembly

            //var assembly = Assemblies.GetModuleFor(bhav.ChunkParent);

            // three possibilities:
            // - module is loading (null for now): create a JIT routine, which will be updated with the appropriate function when it is available
            // - module is finished loading: create an AOT routine, which hsa the function baked in
            // - forced interpreter (not impl yet)

            //if this bhav has a cached jit module, just use that rather than doing a lookup.

            var aot = (SimAnticsModule)bhav.ChunkParent.CachedJITModule;

            if (aot != null)
            {
                return(RoutineFromModule(bhav, aot));
            }

            var assembly = Assemblies.GetModuleFor(res);

            if (assembly != null)
            {
                if (assembly.Module != null)
                {
                    return(RoutineFromModule(bhav, assembly.Module));
                }
                // need to load it or wait for loading.
                var result = new VMRoslynRoutine();
                PopulateRoutineFields(bhav, result);
                assembly.GetModuleAsync().ContinueWith(moduleT =>
                {
                    //TODO: thread safe for server
                    // the main concern is injecting a JIT routine while the interpreter is running that function.
                    // in that case, it could attempt to enter into the the JIT routine at an invalid entry point
                    // (JIT execution can only enter from the start, loop points or after yield)
                    GameThread.InUpdate(() =>
                    {
                        SimAnticsModule module = (moduleT.IsFaulted) ? null : moduleT.Result;
                        object func            = module?.GetFunction(bhav.ChunkID);
                        if (func != null)
                        {
                            if (func is IBHAV)
                            {
                                result.SetJITRoutine((IBHAV)func);
                            }
                            else
                            {
                                result.SetJITRoutine((IInlineBHAV)func);
                            }
                        }
                        else
                        {
                            // failed... stay on interpreter
                        }
                    });
                });
                return(result);
            }

            routine = new VMRoutine();
            PopulateRoutineFields(bhav, routine);
            return(routine);
        }
Ejemplo n.º 21
0
        public bool RunInMyStack(VMRoutine routine, GameObject CodeOwner, short[] passVars, VMEntity stackObj)
        {
            //a little bit hacky. We may not need to do as serious a context switch as this.
            var OldStack      = Stack;
            var OldQueue      = Queue;
            var OldCheck      = IsCheck;
            var OldQueueBlock = ActiveQueueBlock;

            VMStackFrame prevFrame = new VMStackFrame()
            {
                Caller = Entity, Callee = Entity
            };

            if (Stack.Count > 0)
            {
                prevFrame = Stack[Stack.Count - 1];
                Stack     = new List <VMStackFrame>()
                {
                    prevFrame
                };
            }
            else
            {
                Stack = new List <VMStackFrame>();
            }

            Queue = new List <VMQueuedAction>();
            if (Queue.Count > 0)
            {
                Queue.Add(Queue[0]);
            }
            IsCheck = true;

            ExecuteSubRoutine(prevFrame, routine, CodeOwner, new VMSubRoutineOperand(passVars));
            Stack.RemoveAt(0);
            if (Stack.Count == 0)
            {
                Stack = OldStack;
                Queue = OldQueue;
                return(false);
                //bhav was invalid/empty
            }
            var frame = Stack[Stack.Count - 1];

            frame.StackObject = stackObj;

            try
            {
                while (Stack.Count > 0)
                {
                    NextInstruction();
                }
            }
            catch (Exception e)
            {
                if (e is ThreadAbortException)
                {
                    throw e;
                }
                //we need to catch these so that the parent can be restored.
            }

            //copy child stack things to parent stack
            Stack            = OldStack;
            Queue            = OldQueue;
            IsCheck          = OldCheck;
            ActiveQueueBlock = OldQueueBlock;

            return((LastStackExitCode == VMPrimitiveExitCode.RETURN_TRUE) ? true : false);
        }