aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptObjCode.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/XMREngine/MMRScriptObjCode.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/XMREngine/MMRScriptObjCode.cs256
1 files changed, 0 insertions, 256 deletions
diff --git a/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptObjCode.cs b/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptObjCode.cs
deleted file mode 100644
index 038dfcd..0000000
--- a/OpenSim/Region/ScriptEngine/XMREngine/MMRScriptObjCode.cs
+++ /dev/null
@@ -1,256 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using OpenSim.Region.ScriptEngine.XMREngine;
29using System;
30using System.Collections.Generic;
31using System.IO;
32using System.Reflection;
33using System.Reflection.Emit;
34
35namespace OpenSim.Region.ScriptEngine.XMREngine
36{
37 public delegate void ScriptEventHandler (XMRInstAbstract instance);
38
39 /*
40 * This object represents the output of the compilation.
41 * Once the compilation is complete, its contents should be
42 * considered 'read-only', so it can be shared among multiple
43 * instances of the script.
44 *
45 * It gets created by ScriptCodeGen.
46 * It gets used by XMRInstance to create script instances.
47 */
48 public class ScriptObjCode
49 {
50 public string sourceHash; // source text hash code
51
52 public XMRInstArSizes glblSizes = new XMRInstArSizes ();
53 // number of global variables of various types
54
55 public string[] stateNames; // convert state number to corresponding string
56
57 public ScriptEventHandler[,] scriptEventHandlerTable;
58 // entrypoints to all event handler functions
59 // 1st subscript = state code number (0=default)
60 // 2nd subscript = event code number
61 // null entry means no handler defined for that state,event
62
63 public Dictionary<string, TokenDeclSDType> sdObjTypesName;
64 // all script-defined types by name
65 public TokenDeclSDType[] sdObjTypesIndx;
66 // all script-defined types by sdTypeIndex
67
68 public Dictionary<Type, string> sdDelTypes;
69 // all script-defined delegates (including anonymous)
70
71 public Dictionary<string, DynamicMethod> dynamicMethods;
72 // all dyanmic methods
73
74 public Dictionary<string, KeyValuePair<int, ScriptSrcLoc>[]> scriptSrcLocss;
75 // method,iloffset -> source file,line,posn
76
77 public int refCount; // used by engine to keep track of number of
78 // instances that are using this object code
79
80 public Dictionary<string,Dictionary<int,string>> globalVarNames = new Dictionary<string,Dictionary<int,string>> ();
81
82 public DateTime fileDateUtc;
83 public int expiryDays = Int32.MaxValue;
84 public bool IsExpired ()
85 {
86 return (DateTime.UtcNow.Ticks - fileDateUtc.Ticks) / 10000000 / 86400 >= expiryDays;
87 }
88
89 /**
90 * @brief Fill in ScriptObjCode from an XMREngine object file.
91 * 'objFileReader' is a serialized form of the CIL code we generated
92 * 'asmFileWriter' is where we write the disassembly to (or null if not wanted)
93 * 'srcFileWriter' is where we write the decompilation to (or null if not wanted)
94 * Throws an exception if there is any error (theoretically).
95 */
96 public ScriptObjCode (BinaryReader objFileReader, TextWriter asmFileWriter, TextWriter srcFileWriter)
97 {
98 /*
99 * Check version number to make sure we know how to process file contents.
100 */
101 char[] ocm = objFileReader.ReadChars (ScriptCodeGen.OBJECT_CODE_MAGIC.Length);
102 if (new String (ocm) != ScriptCodeGen.OBJECT_CODE_MAGIC) {
103 throw new Exception ("not an XMR object file (bad magic)");
104 }
105 int cvv = objFileReader.ReadInt32 ();
106 if (cvv != ScriptCodeGen.COMPILED_VERSION_VALUE) {
107 throw new CVVMismatchException (cvv, ScriptCodeGen.COMPILED_VERSION_VALUE);
108 }
109
110 /*
111 * Fill in simple parts of scriptObjCode object.
112 */
113 sourceHash = objFileReader.ReadString ();
114 expiryDays = objFileReader.ReadInt32 ();
115 glblSizes.ReadFromFile (objFileReader);
116
117 int nStates = objFileReader.ReadInt32 ();
118
119 stateNames = new string[nStates];
120 for (int i = 0; i < nStates; i ++) {
121 stateNames[i] = objFileReader.ReadString ();
122 if (asmFileWriter != null) {
123 asmFileWriter.WriteLine (" state[{0}] = {1}", i, stateNames[i]);
124 }
125 }
126
127 if (asmFileWriter != null) {
128 glblSizes.WriteAsmFile (asmFileWriter, "numGbl");
129 }
130
131 string gblName;
132 while ((gblName = objFileReader.ReadString ()) != "") {
133 string gblType = objFileReader.ReadString ();
134 int gblIndex = objFileReader.ReadInt32 ();
135 Dictionary<int,string> names;
136 if (!globalVarNames.TryGetValue (gblType, out names)) {
137 names = new Dictionary<int,string> ();
138 globalVarNames.Add (gblType, names);
139 }
140 names.Add (gblIndex, gblName);
141 if (asmFileWriter != null) {
142 asmFileWriter.WriteLine (" {0} = {1}[{2}]", gblName, gblType, gblIndex);
143 }
144 }
145
146 /*
147 * Read in script-defined types.
148 */
149 sdObjTypesName = new Dictionary<string, TokenDeclSDType> ();
150 sdDelTypes = new Dictionary<Type, string> ();
151 int maxIndex = -1;
152 while ((gblName = objFileReader.ReadString ()) != "") {
153 TokenDeclSDType sdt = TokenDeclSDType.ReadFromFile (sdObjTypesName,
154 gblName, objFileReader, asmFileWriter);
155 sdObjTypesName.Add (gblName, sdt);
156 if (maxIndex < sdt.sdTypeIndex) maxIndex = sdt.sdTypeIndex;
157 if (sdt is TokenDeclSDTypeDelegate) {
158 sdDelTypes.Add (sdt.GetSysType (), gblName);
159 }
160 }
161 sdObjTypesIndx = new TokenDeclSDType[maxIndex+1];
162 foreach (TokenDeclSDType sdt in sdObjTypesName.Values) {
163 sdObjTypesIndx[sdt.sdTypeIndex] = sdt;
164 }
165
166 /*
167 * Now fill in the methods (the hard part).
168 */
169 scriptEventHandlerTable = new ScriptEventHandler[nStates,(int)ScriptEventCode.Size];
170 dynamicMethods = new Dictionary<string, DynamicMethod> ();
171 scriptSrcLocss = new Dictionary<string, KeyValuePair<int, ScriptSrcLoc>[]> ();
172
173 ObjectTokens objectTokens = null;
174 if (asmFileWriter != null) {
175 objectTokens = new OTDisassemble (this, asmFileWriter);
176 } else if (srcFileWriter != null) {
177 objectTokens = new OTDecompile (this, srcFileWriter);
178 }
179
180 try {
181 ScriptObjWriter.CreateObjCode (sdObjTypesName, objFileReader, this, objectTokens);
182 } finally {
183 if (objectTokens != null) objectTokens.Close ();
184 }
185
186 /*
187 * We enter all script event handler methods in the ScriptEventHandler table.
188 * They are named: <statename> <eventname>
189 */
190 foreach (KeyValuePair<string, DynamicMethod> kvp in dynamicMethods) {
191 string methName = kvp.Key;
192 int i = methName.IndexOf (' ');
193 if (i < 0) continue;
194 string stateName = methName.Substring (0, i);
195 string eventName = methName.Substring (++ i);
196 int stateCode;
197 for (stateCode = stateNames.Length; -- stateCode >= 0;) {
198 if (stateNames[stateCode] == stateName) break;
199 }
200 int eventCode = (int)Enum.Parse (typeof (ScriptEventCode), eventName);
201 scriptEventHandlerTable[stateCode,eventCode] =
202 (ScriptEventHandler)kvp.Value.CreateDelegate (typeof (ScriptEventHandler));
203 }
204
205 /*
206 * Fill in all script-defined class vtables.
207 */
208 foreach (TokenDeclSDType sdt in sdObjTypesIndx) {
209 if ((sdt != null) && (sdt is TokenDeclSDTypeClass)) {
210 TokenDeclSDTypeClass sdtc = (TokenDeclSDTypeClass)sdt;
211 sdtc.FillVTables (this);
212 }
213 }
214 }
215
216 /**
217 * @brief Called once for every method found in objFileReader file.
218 * It enters the method in the ScriptObjCode object table so it can be called.
219 */
220 public void EndMethod (DynamicMethod method, Dictionary<int, ScriptSrcLoc> srcLocs)
221 {
222 /*
223 * Save method object code pointer.
224 */
225 dynamicMethods.Add (method.Name, method);
226
227 /*
228 * Build and sort iloffset -> source code location array.
229 */
230 int n = srcLocs.Count;
231 KeyValuePair<int, ScriptSrcLoc>[] srcLocArray = new KeyValuePair<int, ScriptSrcLoc>[n];
232 n = 0;
233 foreach (KeyValuePair<int, ScriptSrcLoc> kvp in srcLocs) srcLocArray[n++] = kvp;
234 Array.Sort (srcLocArray, endMethodWrapper);
235
236 /*
237 * Save sorted array.
238 */
239 scriptSrcLocss.Add (method.Name, srcLocArray);
240 }
241
242 /**
243 * @brief Called once for every method found in objFileReader file.
244 * It enters the method in the ScriptObjCode object table so it can be called.
245 */
246 private static EndMethodWrapper endMethodWrapper = new EndMethodWrapper ();
247 private class EndMethodWrapper : System.Collections.IComparer {
248 public int Compare (object x, object y)
249 {
250 KeyValuePair<int, ScriptSrcLoc> kvpx = (KeyValuePair<int, ScriptSrcLoc>)x;
251 KeyValuePair<int, ScriptSrcLoc> kvpy = (KeyValuePair<int, ScriptSrcLoc>)y;
252 return kvpx.Key - kvpy.Key;
253 }
254 }
255 }
256}