aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/YEngine/MMRScriptCompile.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ScriptEngine/YEngine/MMRScriptCompile.cs195
1 files changed, 195 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/YEngine/MMRScriptCompile.cs b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptCompile.cs
new file mode 100644
index 0000000..d16ba47
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/YEngine/MMRScriptCompile.cs
@@ -0,0 +1,195 @@
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
28/**
29 * @brief Compile a script to produce a ScriptObjCode object
30 */
31
32using System;
33using System.IO;
34
35namespace OpenSim.Region.ScriptEngine.Yengine
36{
37 public partial class XMRInstance
38 {
39 /**
40 * @brief Compile a script to produce a ScriptObjCode object
41 * @returns object code pointer or null if compile error
42 * also can throw compile error exception
43 */
44 public ScriptObjCode Compile()
45 {
46 Stream objFileStream = null;
47 StreamWriter asmFileWriter = null;
48 string sourceHash = null;
49 TextWriter saveSource = null;
50
51 string objFileName = GetScriptFileName (m_ScriptObjCodeKey + ".yobj");
52 string tmpFileName = GetScriptFileName (m_ScriptObjCodeKey + ".ytmp");
53
54 // If we already have an object file, don't bother compiling.
55 if (!m_ForceRecomp && File.Exists(objFileName))
56 {
57 objFileStream = File.OpenRead (objFileName);
58 }
59 else
60 {
61 // If source file empty, try to read from asset server.
62 if (EmptySource (m_SourceCode))
63 m_SourceCode = FetchSource (m_CameFrom);
64
65 // Maybe write script source to a file for debugging.
66 if (m_Engine.m_ScriptDebugSaveSource)
67 {
68 string lslFileName = GetScriptFileName (m_ScriptObjCodeKey + ".lsl");
69// m_log.Debug ("[YEngine]: MMRScriptCompileSaveSource: saving to " + lslFileName);
70 saveSource = File.CreateText (lslFileName);
71 }
72
73 // Parse source string into tokens.
74 TokenBegin tokenBegin;
75 try
76 {
77 tokenBegin = TokenBegin.Construct(m_CameFrom, saveSource, ErrorHandler, m_SourceCode, out sourceHash);
78 }
79 finally
80 {
81 if (saveSource != null)
82 saveSource.Close ();
83 }
84 if (tokenBegin == null)
85 {
86 m_log.Debug ("[YEngine]: parsing errors on " + m_ScriptObjCodeKey);
87 return null;
88 }
89
90 // Create object file one way or another.
91 try
92 {
93 objFileStream = File.Create (tmpFileName);
94
95 // Create abstract syntax tree from raw tokens.
96 TokenScript tokenScript = ScriptReduce.Reduce(tokenBegin);
97 if (tokenScript == null)
98 {
99 m_log.Warn ("[YEngine]: reduction errors on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
100 PrintCompilerErrors();
101 objFileStream.Close();
102 return null;
103 }
104
105 // Compile abstract syntax tree to write object file.
106 BinaryWriter objFileWriter = new BinaryWriter (objFileStream);
107 bool ok = ScriptCodeGen.CodeGen(tokenScript, objFileWriter, sourceHash);
108 if (!ok)
109 {
110 m_log.Warn ("[YEngine]: compile error on " + m_ScriptObjCodeKey + " (" + m_CameFrom + ")");
111 PrintCompilerErrors ();
112 objFileWriter.Close ();
113 return null;
114 }
115 objFileWriter.Close ();
116
117 // File has been completely written.
118 // If there is an old one laying around, delete it now.
119 // Then re-open the new file for reading from the beginning.
120 if (File.Exists (objFileName))
121 File.Replace (tmpFileName, objFileName, null);
122 else
123 File.Move (tmpFileName, objFileName);
124
125 objFileStream = File.OpenRead (objFileName);
126 }
127 finally
128 {
129 // In case something went wrong writing temp file, delete it.
130 try
131 {
132 File.Delete (tmpFileName);
133 }
134 catch
135 {
136 }
137 }
138
139 // Since we just wrote the .xmrobj file, maybe save disassembly.
140 if (m_Engine.m_ScriptDebugSaveIL)
141 {
142 string asmFileName = GetScriptFileName (m_ScriptObjCodeKey + ".yasm");
143// m_log.Debug ("[YEngine]: MMRScriptCompileSaveILGen: saving to " + asmFileName);
144 asmFileWriter = File.CreateText (asmFileName);
145 }
146 }
147
148 // Read object file to create ScriptObjCode object.
149 // Maybe also write disassembly to a file for debugging.
150 BinaryReader objFileReader = new BinaryReader (objFileStream);
151 ScriptObjCode scriptObjCode = null;
152 try
153 {
154 scriptObjCode = new ScriptObjCode (objFileReader, asmFileWriter, null);
155 }
156 finally
157 {
158 objFileReader.Close ();
159 if (asmFileWriter != null)
160 {
161 asmFileWriter.Flush ();
162 asmFileWriter.Close ();
163 }
164 }
165
166 return scriptObjCode;
167 }
168
169 private void PrintCompilerErrors ()
170 {
171 m_log.Info ("[YEngine]: - " + m_Part.GetWorldPosition () + " " + m_DescName);
172 foreach (string error in m_CompilerErrors) {
173 m_log.Info ("[YEngine]: - " + error);
174 }
175 }
176
177 /**
178 * @brief Check for empty source, allowing for a first line of //... script engine selector.
179 */
180 public static bool EmptySource (string source)
181 {
182 int len = source.Length;
183 bool skipeol = false;
184 for (int i = 0; i < len; i ++)
185 {
186 char c = source[i];
187 skipeol &= c != '\n';
188 skipeol |= (c == '/') && (i + 1 < len) && (source[i+1] == '/');
189 if ((c > ' ') && !skipeol)
190 return false;
191 }
192 return true;
193 }
194 }
195}