aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/YEngine/MMRScriptObjCode.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/YEngine/MMRScriptObjCode.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/YEngine/MMRScriptObjCode.cs237
1 files changed, 237 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptObjCode.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptObjCode.cs
new file mode 100644
index 0000000..ba72e39
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptObjCode.cs
@@ -0,0 +1,237 @@
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.Yengine;
29using System;
30using System.Collections.Generic;
31using System.IO;
32using System.Reflection;
33using System.Reflection.Emit;
34
35namespace OpenSim.Region.ScriptEngine.Yengine
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 public XMRInstArSizes glblSizes = new XMRInstArSizes();
52 // number of global variables of various types
53
54 public string[] stateNames; // convert state number to corresponding string
55 public ScriptEventHandler[,] scriptEventHandlerTable;
56 // entrypoints to all event handler functions
57 // 1st subscript = state code number (0=default)
58 // 2nd subscript = event code number
59 // null entry means no handler defined for that state,event
60
61 public Dictionary<string, TokenDeclSDType> sdObjTypesName;
62 // all script-defined types by name
63
64 public TokenDeclSDType[] sdObjTypesIndx;
65 // all script-defined types by sdTypeIndex
66
67 public Dictionary<Type, string> sdDelTypes;
68 // all script-defined delegates (including anonymous)
69
70 public Dictionary<string, DynamicMethod> dynamicMethods;
71 // all dyanmic methods
72
73 public Dictionary<string, KeyValuePair<int, ScriptSrcLoc>[]> scriptSrcLocss;
74 // method,iloffset -> source file,line,posn
75
76 public int refCount; // used by engine to keep track of number of
77 // instances that are using this object code
78
79 public Dictionary<string, Dictionary<int, string>> globalVarNames = new Dictionary<string, Dictionary<int, string>>();
80
81 /**
82 * @brief Fill in ScriptObjCode from an YEngine object file.
83 * 'objFileReader' is a serialized form of the CIL code we generated
84 * 'asmFileWriter' is where we write the disassembly to (or null if not wanted)
85 * 'srcFileWriter' is where we write the decompilation to (or null if not wanted)
86 * Throws an exception if there is any error (theoretically).
87 */
88 public ScriptObjCode(BinaryReader objFileReader, TextWriter asmFileWriter, TextWriter srcFileWriter)
89 {
90 // Check version number to make sure we know how to process file contents.
91 char[] ocm = objFileReader.ReadChars(ScriptCodeGen.OBJECT_CODE_MAGIC.Length);
92 if(new String(ocm) != ScriptCodeGen.OBJECT_CODE_MAGIC)
93 throw new Exception("not an Yengine object file (bad magic)");
94
95 int cvv = objFileReader.ReadInt32();
96 if(cvv != ScriptCodeGen.COMPILED_VERSION_VALUE)
97 throw new CVVMismatchException(cvv, ScriptCodeGen.COMPILED_VERSION_VALUE);
98
99 // Fill in simple parts of scriptObjCode object.
100 sourceHash = objFileReader.ReadString();
101 glblSizes.ReadFromFile(objFileReader);
102 int nStates = objFileReader.ReadInt32();
103
104 stateNames = new string[nStates];
105 for(int i = 0; i < nStates; i++)
106 {
107 stateNames[i] = objFileReader.ReadString();
108 if(asmFileWriter != null)
109 asmFileWriter.WriteLine(" state[{0}] = {1}", i, stateNames[i]);
110 }
111
112 if(asmFileWriter != null)
113 glblSizes.WriteAsmFile(asmFileWriter, "numGbl");
114
115 string gblName;
116 while((gblName = objFileReader.ReadString()) != "")
117 {
118 string gblType = objFileReader.ReadString();
119 int gblIndex = objFileReader.ReadInt32();
120 Dictionary<int, string> names;
121 if(!globalVarNames.TryGetValue(gblType, out names))
122 {
123 names = new Dictionary<int, string>();
124 globalVarNames.Add(gblType, names);
125 }
126 names.Add(gblIndex, gblName);
127 if(asmFileWriter != null)
128 asmFileWriter.WriteLine(" {0} = {1}[{2}]", gblName, gblType, gblIndex);
129 }
130
131 // Read in script-defined types.
132 sdObjTypesName = new Dictionary<string, TokenDeclSDType>();
133 sdDelTypes = new Dictionary<Type, string>();
134 int maxIndex = -1;
135 while((gblName = objFileReader.ReadString()) != "")
136 {
137 TokenDeclSDType sdt = TokenDeclSDType.ReadFromFile(sdObjTypesName,
138 gblName, objFileReader, asmFileWriter);
139 sdObjTypesName.Add(gblName, sdt);
140 if(maxIndex < sdt.sdTypeIndex)
141 maxIndex = sdt.sdTypeIndex;
142 if(sdt is TokenDeclSDTypeDelegate)
143 sdDelTypes.Add(sdt.GetSysType(), gblName);
144 }
145 sdObjTypesIndx = new TokenDeclSDType[maxIndex + 1];
146 foreach(TokenDeclSDType sdt in sdObjTypesName.Values)
147 sdObjTypesIndx[sdt.sdTypeIndex] = sdt;
148
149 // Now fill in the methods (the hard part).
150 scriptEventHandlerTable = new ScriptEventHandler[nStates, (int)ScriptEventCode.Size];
151 dynamicMethods = new Dictionary<string, DynamicMethod>();
152 scriptSrcLocss = new Dictionary<string, KeyValuePair<int, ScriptSrcLoc>[]>();
153
154 ObjectTokens objectTokens = null;
155 if(asmFileWriter != null)
156 objectTokens = new OTDisassemble(this, asmFileWriter);
157 else if(srcFileWriter != null)
158 objectTokens = new OTDecompile(this, srcFileWriter);
159
160 try
161 {
162 ScriptObjWriter.CreateObjCode(sdObjTypesName, objFileReader, this, objectTokens);
163 }
164 finally
165 {
166 if(objectTokens != null)
167 objectTokens.Close();
168 }
169
170 // We enter all script event handler methods in the ScriptEventHandler table.
171 // They are named: <statename> <eventname>
172 foreach(KeyValuePair<string, DynamicMethod> kvp in dynamicMethods)
173 {
174 string methName = kvp.Key;
175 int i = methName.IndexOf(' ');
176 if(i < 0)
177 continue;
178 string stateName = methName.Substring(0, i);
179 string eventName = methName.Substring(++i);
180 int stateCode;
181 for(stateCode = stateNames.Length; --stateCode >= 0;)
182 if(stateNames[stateCode] == stateName)
183 break;
184
185 int eventCode = (int)Enum.Parse(typeof(ScriptEventCode), eventName);
186 scriptEventHandlerTable[stateCode, eventCode] =
187 (ScriptEventHandler)kvp.Value.CreateDelegate(typeof(ScriptEventHandler));
188 }
189
190 // Fill in all script-defined class vtables.
191 foreach(TokenDeclSDType sdt in sdObjTypesIndx)
192 {
193 if((sdt != null) && (sdt is TokenDeclSDTypeClass))
194 {
195 TokenDeclSDTypeClass sdtc = (TokenDeclSDTypeClass)sdt;
196 sdtc.FillVTables(this);
197 }
198 }
199 }
200
201 /**
202 * @brief Called once for every method found in objFileReader file.
203 * It enters the method in the ScriptObjCode object table so it can be called.
204 */
205 public void EndMethod(DynamicMethod method, Dictionary<int, ScriptSrcLoc> srcLocs)
206 {
207 // Save method object code pointer.
208 dynamicMethods.Add(method.Name, method);
209
210 // Build and sort iloffset -> source code location array.
211 int n = srcLocs.Count;
212 KeyValuePair<int, ScriptSrcLoc>[] srcLocArray = new KeyValuePair<int, ScriptSrcLoc>[n];
213 n = 0;
214 foreach(KeyValuePair<int, ScriptSrcLoc> kvp in srcLocs)
215 srcLocArray[n++] = kvp;
216 Array.Sort(srcLocArray, endMethodWrapper);
217
218 // Save sorted array.
219 scriptSrcLocss.Add(method.Name, srcLocArray);
220 }
221
222 /**
223 * @brief Called once for every method found in objFileReader file.
224 * It enters the method in the ScriptObjCode object table so it can be called.
225 */
226 private static EndMethodWrapper endMethodWrapper = new EndMethodWrapper();
227 private class EndMethodWrapper: System.Collections.IComparer
228 {
229 public int Compare(object x, object y)
230 {
231 KeyValuePair<int, ScriptSrcLoc> kvpx = (KeyValuePair<int, ScriptSrcLoc>)x;
232 KeyValuePair<int, ScriptSrcLoc> kvpy = (KeyValuePair<int, ScriptSrcLoc>)y;
233 return kvpx.Key - kvpy.Key;
234 }
235 }
236 }
237}