Exemplo n.º 1
0
 public void Emit(Token errorAt, OpCode opcode, ScriptObjWriter method)
 {
     if (method == null)
     {
         throw new ArgumentNullException("method");
     }
     objFileWriter.Write((byte)ScriptObjWriterCode.EmitMethodInt);
     WriteOpCode(errorAt, opcode);
     objFileWriter.Write(method.methName);
 }
Exemplo n.º 2
0
 public void Emit(Token errorAt, OpCode opcode, ScriptObjWriter method)
 {
 }
Exemplo n.º 3
0
        /**
         * @brief Fill in ScriptObjCode from an XMREngine object file.
         *   'objFileReader' is a serialized form of the CIL code we generated
         *   'asmFileWriter' is where we write the disassembly to (or null if not wanted)
         *   'srcFileWriter' is where we write the decompilation to (or null if not wanted)
         * Throws an exception if there is any error (theoretically).
         */
        public ScriptObjCode(BinaryReader objFileReader, TextWriter asmFileWriter, TextWriter srcFileWriter)
        {
            /*
             * Check version number to make sure we know how to process file contents.
             */
            char[] ocm = objFileReader.ReadChars(ScriptCodeGen.OBJECT_CODE_MAGIC.Length);
            if (new String(ocm) != ScriptCodeGen.OBJECT_CODE_MAGIC)
            {
                throw new Exception("not an XMR object file (bad magic)");
            }
            int cvv = objFileReader.ReadInt32();

            if (cvv != ScriptCodeGen.COMPILED_VERSION_VALUE)
            {
                throw new CVVMismatchException(cvv, ScriptCodeGen.COMPILED_VERSION_VALUE);
            }

            /*
             * Fill in simple parts of scriptObjCode object.
             */
            sourceHash = objFileReader.ReadString();
            expiryDays = objFileReader.ReadInt32();
            glblSizes.ReadFromFile(objFileReader);

            int nStates = objFileReader.ReadInt32();

            stateNames = new string[nStates];
            for (int i = 0; i < nStates; i++)
            {
                stateNames[i] = objFileReader.ReadString();
                if (asmFileWriter != null)
                {
                    asmFileWriter.WriteLine("  state[{0}] = {1}", i, stateNames[i]);
                }
            }

            if (asmFileWriter != null)
            {
                glblSizes.WriteAsmFile(asmFileWriter, "numGbl");
            }

            string gblName;

            while ((gblName = objFileReader.ReadString()) != "")
            {
                string gblType  = objFileReader.ReadString();
                int    gblIndex = objFileReader.ReadInt32();
                Dictionary <int, string> names;
                if (!globalVarNames.TryGetValue(gblType, out names))
                {
                    names = new Dictionary <int, string> ();
                    globalVarNames.Add(gblType, names);
                }
                names.Add(gblIndex, gblName);
                if (asmFileWriter != null)
                {
                    asmFileWriter.WriteLine("  {0} = {1}[{2}]", gblName, gblType, gblIndex);
                }
            }

            /*
             * Read in script-defined types.
             */
            sdObjTypesName = new Dictionary <string, TokenDeclSDType> ();
            sdDelTypes     = new Dictionary <Type, string> ();
            int maxIndex = -1;

            while ((gblName = objFileReader.ReadString()) != "")
            {
                TokenDeclSDType sdt = TokenDeclSDType.ReadFromFile(sdObjTypesName,
                                                                   gblName, objFileReader, asmFileWriter);
                sdObjTypesName.Add(gblName, sdt);
                if (maxIndex < sdt.sdTypeIndex)
                {
                    maxIndex = sdt.sdTypeIndex;
                }
                if (sdt is TokenDeclSDTypeDelegate)
                {
                    sdDelTypes.Add(sdt.GetSysType(), gblName);
                }
            }
            sdObjTypesIndx = new TokenDeclSDType[maxIndex + 1];
            foreach (TokenDeclSDType sdt in sdObjTypesName.Values)
            {
                sdObjTypesIndx[sdt.sdTypeIndex] = sdt;
            }

            /*
             * Now fill in the methods (the hard part).
             */
            scriptEventHandlerTable = new ScriptEventHandler[nStates, (int)ScriptEventCode.Size];
            dynamicMethods          = new Dictionary <string, DynamicMethod> ();
            scriptSrcLocss          = new Dictionary <string, KeyValuePair <int, ScriptSrcLoc>[]> ();

            ObjectTokens objectTokens = null;

            if (asmFileWriter != null)
            {
                objectTokens = new OTDisassemble(this, asmFileWriter);
            }
            else if (srcFileWriter != null)
            {
                objectTokens = new OTDecompile(this, srcFileWriter);
            }

            try {
                ScriptObjWriter.CreateObjCode(sdObjTypesName, objFileReader, this, objectTokens);
            } finally {
                if (objectTokens != null)
                {
                    objectTokens.Close();
                }
            }

            /*
             * We enter all script event handler methods in the ScriptEventHandler table.
             * They are named:  <statename> <eventname>
             */
            foreach (KeyValuePair <string, DynamicMethod> kvp in dynamicMethods)
            {
                string methName = kvp.Key;
                int    i        = methName.IndexOf(' ');
                if (i < 0)
                {
                    continue;
                }
                string stateName = methName.Substring(0, i);
                string eventName = methName.Substring(++i);
                int    stateCode;
                for (stateCode = stateNames.Length; --stateCode >= 0;)
                {
                    if (stateNames[stateCode] == stateName)
                    {
                        break;
                    }
                }
                int eventCode = (int)Enum.Parse(typeof(ScriptEventCode), eventName);
                scriptEventHandlerTable[stateCode, eventCode] =
                    (ScriptEventHandler)kvp.Value.CreateDelegate(typeof(ScriptEventHandler));
            }

            /*
             * Fill in all script-defined class vtables.
             */
            foreach (TokenDeclSDType sdt in sdObjTypesIndx)
            {
                if ((sdt != null) && (sdt is TokenDeclSDTypeClass))
                {
                    TokenDeclSDTypeClass sdtc = (TokenDeclSDTypeClass)sdt;
                    sdtc.FillVTables(this);
                }
            }
        }