Esempio n. 1
0
 public override void CodeGen(ScriptCodeGen scg, Token errorAt, CompValuTemp result, CompValu[] args)
 {
     args[0].PushVal(scg, errorAt, new TokenTypeFloat(null));
     scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4, (int)System.MidpointRounding.AwayFromZero);
     scg.ilGen.Emit(errorAt, OpCodes.Call, roundMethInfo);
     result.Pop(scg, errorAt, new TokenTypeFloat(null));
 }
Esempio n. 2
0
 public override void CodeGen(ScriptCodeGen scg, Token errorAt, CompValuTemp result, CompValu[] args)
 {
     for (int i = 0; i < args.Length; i++)
     {
         args[i].PushVal(scg, errorAt, argDecl.types[i]);
     }
     scg.ilGen.Emit(errorAt, OpCodes.Call, methInfo);
     result.Pop(scg, errorAt, retType);
 }
Esempio n. 3
0
        public override void CodeGen(ScriptCodeGen scg, Token errorAt, CompValuTemp result, CompValu[] args)
        {
            ScriptMyLabel itsPosLabel = scg.ilGen.DefineLabel("llAbstemp");

            args[0].PushVal(scg, errorAt);
            scg.ilGen.Emit(errorAt, OpCodes.Dup);
            scg.ilGen.Emit(errorAt, OpCodes.Ldc_I4_0);
            scg.ilGen.Emit(errorAt, OpCodes.Bge_S, itsPosLabel);
            scg.ilGen.Emit(errorAt, OpCodes.Neg);
            scg.ilGen.MarkLabel(itsPosLabel);
            result.Pop(scg, errorAt, retType);
        }
Esempio n. 4
0
        /**
         * @brief Generate call to backend API function (eg llSay()) maybe followed by a call to CheckRun().
         * @param scg    = script being compiled
         * @param result = where to place result (might be void)
         * @param args   = script-visible arguments to pass to API function
         */
        public override void CodeGen(ScriptCodeGen scg, Token errorAt, CompValuTemp result, CompValu[] args)
        {
            if (isTaggedCallsCheckRun)
            {                                                  // see if 'xmr' method that calls CheckRun() internally
                new ScriptCodeGen.CallLabel(scg, errorAt);     // if so, put a call label immediately before it
                                                               // .. so restoring the frame will jump immediately to the
                                                               // .. call without re-executing any code before this
            }
            if (!methInfo.IsStatic)
            {
                scg.PushXMRInst();                          // XMRInstanceSuperType pointer
                if (apiContextField != null)                // 'this' pointer for API function
                {
                    scg.ilGen.Emit(errorAt, OpCodes.Ldfld, apiContextField);
                }
            }
            for (int i = 0; i < args.Length; i++)             // push arguments, boxing/unboxing as needed
            {
                args[i].PushVal(scg, errorAt, argDecl.types[i]);
            }

            // this should not be needed
            //            if (methInfo.Name == "llParcelMediaQuery") {
            //                scg.ilGen.Emit (errorAt, OpCodes.Call, fixLLParcelMediaQuery);
            //            }
            // this should not be needed
            //            if (methInfo.Name == "llParcelMediaCommandList") {
            //                scg.ilGen.Emit (errorAt, OpCodes.Call, fixLLParcelMediaCommandList);
            //            }
            if (methInfo.IsVirtual)                            // call API function
            {
                scg.ilGen.Emit(errorAt, OpCodes.Callvirt, methInfo);
            }
            else
            {
                scg.ilGen.Emit(errorAt, OpCodes.Call, methInfo);
            }

            result.Pop(scg, errorAt, retType);                  // pop result, boxing/unboxing as needed
            if (isTaggedCallsCheckRun)
            {
                scg.openCallLabel = null;
            }

            if (doCheckRun)
            {
                scg.EmitCallCheckRun(errorAt, false);       // maybe call CheckRun()
            }
        }
Esempio n. 5
0
 // appears as llGetUsedMemory() in script source code
 // but actually calls xmrHeapUsed()
 public override void CodeGen(ScriptCodeGen scg, Token errorAt, CompValuTemp result, CompValu[] args)
 {
     scg.PushXMRInst();
     scg.ilGen.Emit(errorAt, OpCodes.Call, getUsedMemMethInfo);
     result.Pop(scg, errorAt, new TokenTypeInt(null));
 }
Esempio n. 6
0
 public TokenDeclInline_Math(VarDict ifd, string sig, string name, Type[] args)
     : base(ifd, false, sig, new TokenTypeFloat(null))
 {
     methInfo = ScriptCodeGen.GetStaticMethod(typeof(System.Math), name, args);
 }
Esempio n. 7
0
 public abstract void CodeGen(ScriptCodeGen scg, Token errorAt, CompValuTemp result, CompValu[] args);
Esempio n. 8
0
        /**
         * @brief Compile a script to produce a ScriptObjCode object
         * @returns object code pointer or null if compile error
         *          also can throw compile error exception
         */
        public ScriptObjCode Compile()
        {
            Stream       objFileStream = null;
            StreamWriter asmFileWriter = null;
            string       sourceHash    = null;
            TextWriter   saveSource    = null;

            string objFileName = GetScriptFileName(m_ScriptObjCodeKey + ".yobj");
            string tmpFileName = GetScriptFileName(m_ScriptObjCodeKey + ".ytmp");

            // If we already have an object file, don't bother compiling.
            if (!m_ForceRecomp && File.Exists(objFileName))
            {
                objFileStream = File.OpenRead(objFileName);
            }
            else
            {
                // If source file empty, try to read from asset server.
                if (EmptySource(m_SourceCode))
                {
                    m_SourceCode = FetchSource(m_CameFrom);
                }

                // Maybe write script source to a file for debugging.
                if (m_Engine.m_ScriptDebugSaveSource)
                {
                    string lslFileName = GetScriptFileName(m_ScriptObjCodeKey + ".lsl");
//                    m_log.Debug ("[YEngine]: MMRScriptCompileSaveSource: saving to " + lslFileName);
                    saveSource = File.CreateText(lslFileName);
                }

                // Parse source string into tokens.
                TokenBegin tokenBegin;
                try
                {
                    tokenBegin = TokenBegin.Construct(m_CameFrom, saveSource, ErrorHandler, m_SourceCode, out sourceHash);
                }
                finally
                {
                    if (saveSource != null)
                    {
                        saveSource.Close();
                    }
                }
                if (tokenBegin == null)
                {
                    m_log.Debug("[YEngine]: parsing errors on " + m_ScriptObjCodeKey);
                    return(null);
                }

                // Create object file one way or another.
                try
                {
                    // Create abstract syntax tree from raw tokens.
                    TokenScript tokenScript = ScriptReduce.Reduce(tokenBegin);
                    if (tokenScript == null)
                    {
                        m_log.Warn("[YEngine]: reduction errors on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
                        PrintCompilerErrors();
                        return(null);
                    }

                    // Compile abstract syntax tree to write object file.
                    using (BinaryWriter objFileWriter = new BinaryWriter(File.Create(tmpFileName)))
                    {
                        bool ok = ScriptCodeGen.CodeGen(tokenScript, objFileWriter, sourceHash);
                        if (!ok)
                        {
                            m_log.Warn("[YEngine]: compile error on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
                            PrintCompilerErrors();
                            return(null);
                        }
                    }

                    // File has been completely written.
                    // If there is an old one laying around, delete it now.
                    // Then re-open the new file for reading from the beginning.
                    if (File.Exists(objFileName))
                    {
                        File.Replace(tmpFileName, objFileName, null);
                    }
                    else
                    {
                        File.Move(tmpFileName, objFileName);
                    }

                    objFileStream = File.OpenRead(objFileName);
                }
                finally
                {
                    // In case something went wrong writing temp file, delete it.
                    File.Delete(tmpFileName);
                }

                // Since we just wrote the .xmrobj file, maybe save disassembly.
                if (m_Engine.m_ScriptDebugSaveIL)
                {
                    string asmFileName = GetScriptFileName(m_ScriptObjCodeKey + ".yasm");
//                    m_log.Debug ("[YEngine]: MMRScriptCompileSaveILGen: saving to " + asmFileName);
                    asmFileWriter = File.CreateText(asmFileName);
                }
            }

            // Read object file to create ScriptObjCode object.
            // Maybe also write disassembly to a file for debugging.
            BinaryReader  objFileReader = new BinaryReader(objFileStream);
            ScriptObjCode scriptObjCode = null;

            try
            {
                scriptObjCode = new ScriptObjCode(objFileReader, asmFileWriter, null);
            }
            finally
            {
                objFileReader.Close();
                if (asmFileWriter != null)
                {
                    asmFileWriter.Flush();
                    asmFileWriter.Close();
                }
            }

            return(scriptObjCode);
        }
Esempio n. 9
0
        /**
         * @brief Compile a script to produce a ScriptObjCode object
         * @returns object code pointer or null if compile error
         *          also can throw compile error exception
         */
        public ScriptObjCode Compile()
        {
            bool         oldObjFile    = false;
            Stream       objFileStream = null;
            StreamWriter asmFileWriter = null;
            string       envar         = null;
            string       sourceHash    = null;
            TextWriter   saveSource    = null;

            string asmFileName = GetScriptFileName(m_ScriptObjCodeKey + ".xmrasm");
            string lslFileName = GetScriptFileName(m_ScriptObjCodeKey + ".lsl");
            string objFileName = GetScriptFileName(m_ScriptObjCodeKey + ".xmrobj");
            string tmpFileName = GetScriptFileName(m_ScriptObjCodeKey + ".xmrtmp");

            /*
             * If we already have an object file, don't bother compiling.
             */
            if (!m_ForceRecomp && File.Exists(objFileName))
            {
                objFileStream = File.OpenRead(objFileName);
                oldObjFile    = true;
            }
            else
            {
                /*
                 * If source file empty, try to read from asset server.
                 */
                if (EmptySource(m_SourceCode))
                {
                    m_SourceCode = FetchSource(m_CameFrom);
                }

                /*
                 * Maybe write script source to a file for debugging.
                 */
                envar = Environment.GetEnvironmentVariable("MMRScriptCompileSaveSource");
                if ((envar != null) && ((envar[0] & 1) != 0))
                {
                    m_log.Debug("[XMREngine]: MMRScriptCompileSaveSource: saving to " + lslFileName);
                    saveSource = File.CreateText(lslFileName);
                }

                /*
                 * Parse source string into tokens.
                 */
                TokenBegin tokenBegin;
                try {
                    tokenBegin = TokenBegin.Construct(m_CameFrom, saveSource, ErrorHandler, m_SourceCode, out sourceHash);
                } finally {
                    if (saveSource != null)
                    {
                        saveSource.Close();
                    }
                }
                if (tokenBegin == null)
                {
                    m_log.Debug("[XMREngine]: parsing errors on " + m_ScriptObjCodeKey);
                    return(null);
                }

                /*
                 * Create object file one way or another.
                 */
                try {
                    objFileStream = File.Create(tmpFileName);

                    /*
                     * Create abstract syntax tree from raw tokens.
                     */
                    TokenScript tokenScript = ScriptReduce.Reduce(tokenBegin);
                    if (tokenScript == null)
                    {
                        m_log.Warn("[XMREngine]: reduction errors on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
                        PrintCompilerErrors();
                        return(null);
                    }

                    /*
                     * Compile abstract syntax tree to write object file.
                     */
                    BinaryWriter objFileWriter = new BinaryWriter(objFileStream);
                    bool         ok            = ScriptCodeGen.CodeGen(tokenScript, objFileWriter, sourceHash);
                    if (!ok)
                    {
                        m_log.Warn("[XMREngine]: compile error on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
                        PrintCompilerErrors();
                        objFileStream.Close();
                        return(null);
                    }
                    objFileStream.Close();

                    /*
                     * File has been completely written.
                     * If there is an old one laying around, delete it now.
                     * Then re-open the new file for reading from the beginning.
                     */
                    if (File.Exists(objFileName))
                    {
                        File.Replace(tmpFileName, objFileName, null);
                    }
                    else
                    {
                        File.Move(tmpFileName, objFileName);
                    }
                    objFileStream = File.OpenRead(objFileName);
                } finally {
                    /*
                     * In case something went wrong writing temp file, delete it.
                     */
                    try {
                        File.Delete(tmpFileName);
                    } catch {
                    }
                }

                /*
                 * Since we just wrote the .xmrobj file, maybe save disassembly.
                 */
                envar = Environment.GetEnvironmentVariable("MMRScriptCompileSaveILGen");
                if ((envar != null) && ((envar[0] & 1) != 0))
                {
                    m_log.Debug("[XMREngine]: MMRScriptCompileSaveILGen: saving to " + asmFileName);
                    asmFileWriter = File.CreateText(asmFileName);
                }
            }

            /*
             * Read object file to create ScriptObjCode object.
             * Maybe also write disassembly to a file for debugging.
             */
            BinaryReader  objFileReader = new BinaryReader(objFileStream);
            ScriptObjCode scriptObjCode = null;

            try {
                scriptObjCode = new ScriptObjCode(objFileReader, asmFileWriter, null);
                if (scriptObjCode != null)
                {
                    scriptObjCode.fileDateUtc = File.GetLastWriteTimeUtc(objFileName);
                }
            } finally {
                objFileReader.Close();
                if (asmFileWriter != null)
                {
                    asmFileWriter.Flush();
                    asmFileWriter.Close();
                }
            }

            /*
             * Maybe an old object file has reached its expiration date.
             */
            if (oldObjFile && (scriptObjCode != null) && scriptObjCode.IsExpired())
            {
                m_log.Debug("[XMREngine]: expiration reached on " + m_ScriptObjCodeKey + ", reloading");
                m_ForceRecomp = true;
                scriptObjCode = Compile();
            }

            return(scriptObjCode);
        }