Esempio n. 1
0
 public void FixRegisterVMSymbol()
 {
     if (!symbolFixed && registerSymbols != null)
     {
         symbolFixed = true;
         JITCompiler.FixSymbol(registerSymbols);
     }
 }
        /// <summary>
        /// Parses a string from a .sml file containing a single
        /// SML instruction
        /// </summary>
        /// <param name="instruction">The string representation
        /// of an instruction</param>
        private void ParseInstruction(string instruction, int lineNumber)
        {
            #region TASK 5 & 7 - MAY REQUIRE MODIFICATION BY THE STUDENT
            // detects breakpoint removes it and adds linenumber to list
            string[] rawSplit = instruction.Split('*');
            if (rawSplit.Length > 1)
            {
                instruction = rawSplit[1].Trim();
                breakpoints.Add(lineNumber);
            }
            //Detects %
            string[] cString = instruction.Split('%');
            if (cString.Length > 2)
            {
                conditionals.Add(cString[1].Trim(), lineNumber);
                instruction = cString[2].Trim();
            }
            #endregion

            string[] tokens = null;
            if (instruction.Contains("\""))
            {
                tokens = instruction.Split(new char[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);

                // Remove any unnecessary whitespace
                for (int i = 0; i < tokens.Length; i++)
                {
                    tokens[i] = tokens[i].Trim();
                }
            }
            else
            {
                // Tokenize the instruction string by separating on spaces
                tokens = instruction.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            }


            // Ensure the correct number of operands
            if (tokens.Length > 3)
            {
                throw new SvmCompilationException(String.Format(InvalidOperandsMessage, instruction));
            }

            switch (tokens.Length)
            {
            case 1:
                program.Add(JITCompiler.CompileInstruction(tokens[0]));
                break;

            case 2:
                program.Add(JITCompiler.CompileInstruction(tokens[0], tokens[1].Trim('\"')));
                break;

            case 3:
                program.Add(JITCompiler.CompileInstruction(tokens[0], tokens[1].Trim('\"'), tokens[2].Trim('\"')));
                break;
            }
        }
Esempio n. 3
0
        internal void InitCodeBody(bool register)
        {
            if (def.HasBody)
            {
                localVarCnt = def.Body.Variables.Count;
                Dictionary <Mono.Cecil.Cil.Instruction, int> addr = new Dictionary <Mono.Cecil.Cil.Instruction, int>();

                bool noRelease = false;
                if (register)
                {
                    JITCompiler jit = new JITCompiler(appdomain, declaringType, this);
                    bodyRegister = jit.Compile(out stackRegisterCnt, out jumptablesR, addr, out registerSymbols);
                }
                else
                {
                    InitStackCodeBody(addr);
                    if (jitOnDemand)
                    {
                        noRelease = bodyRegister == null;
                    }
                }
                if (def.Body.ExceptionHandlers.Count > 0)
                {
                    ExceptionHandler[] ehs;
                    if (register)
                    {
                        if (exceptionHandlerR == null)
                        {
                            exceptionHandlerR = new Method.ExceptionHandler[def.Body.ExceptionHandlers.Count];
                        }
                        ehs = exceptionHandlerR;
                    }
                    else
                    {
                        if (exceptionHandler == null)
                        {
                            exceptionHandler = new Method.ExceptionHandler[def.Body.ExceptionHandlers.Count];
                        }
                        ehs = exceptionHandler;
                    }

                    for (int i = 0; i < def.Body.ExceptionHandlers.Count; i++)
                    {
                        var eh             = def.Body.ExceptionHandlers[i];
                        ExceptionHandler e = new ExceptionHandler();
                        e.HandlerStart = addr[eh.HandlerStart];
                        e.HandlerEnd   = eh.HandlerEnd != null ? addr[eh.HandlerEnd] - 1 : def.Body.Instructions.Count - 1;
                        e.TryStart     = addr[eh.TryStart];
                        e.TryEnd       = addr[eh.TryEnd] - 1;
                        switch (eh.HandlerType)
                        {
                        case Mono.Cecil.Cil.ExceptionHandlerType.Catch:
                            e.CatchType   = appdomain.GetType(eh.CatchType, declaringType, this);
                            e.HandlerType = ExceptionHandlerType.Catch;
                            break;

                        case Mono.Cecil.Cil.ExceptionHandlerType.Finally:
                            e.HandlerType = ExceptionHandlerType.Finally;
                            break;

                        case Mono.Cecil.Cil.ExceptionHandlerType.Fault:
                            e.HandlerType = ExceptionHandlerType.Fault;
                            break;

                        default:
                            throw new NotImplementedException();
                        }
                        ehs[i] = e;
                    }
                    //Mono.Cecil.Cil.ExceptionHandlerType.
                }
                variables = def.Body.Variables;
#if !DEBUG || DISABLE_ILRUNTIME_DEBUG
                //Release Method body to save memory
                if (!noRelease)
                {
                    def.Body = null;
                }
#endif
            }
            else
            {
                body = new OpCode[0];
            }
        }