MMRScriptCompile.cs 7.6 KB

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