aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs
diff options
context:
space:
mode:
authorUbitUmarov2016-08-31 09:15:08 +0100
committerUbitUmarov2016-08-31 09:15:08 +0100
commit5afc5fe343172c315adec97cd8348357a196d5d2 (patch)
tree88d8410c4f32c8505c048dc45e674bb5d9146212 /OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs
parentXengine: remove a no thread safe locking, don't allocate a few objects only n... (diff)
downloadopensim-SC-5afc5fe343172c315adec97cd8348357a196d5d2.zip
opensim-SC-5afc5fe343172c315adec97cd8348357a196d5d2.tar.gz
opensim-SC-5afc5fe343172c315adec97cd8348357a196d5d2.tar.bz2
opensim-SC-5afc5fe343172c315adec97cd8348357a196d5d2.tar.xz
Xengine: try to reduce memory pressure of scripts compile. Still ugly code, possible mistakes, but i need to share it before i loose it :) )
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs133
1 files changed, 96 insertions, 37 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs
index af324bf..67762a3 100644
--- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Compiler.cs
@@ -81,8 +81,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
81 81
82 // private object m_syncy = new object(); 82 // private object m_syncy = new object();
83 83
84 private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider(); 84// private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider();
85 private static VBCodeProvider VBcodeProvider = new VBCodeProvider(); 85// private static VBCodeProvider VBcodeProvider = new VBCodeProvider();
86 86
87 // private static int instanceID = new Random().Next(0, int.MaxValue); // Unique number to use on our compiled files 87 // private static int instanceID = new Random().Next(0, int.MaxValue); // Unique number to use on our compiled files
88 private static UInt64 scriptCompileCounter = 0; // And a counter 88 private static UInt64 scriptCompileCounter = 0; // And a counter
@@ -356,14 +356,24 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
356 throw new Exception(errtext); 356 throw new Exception(errtext);
357 } 357 }
358 358
359 string compileScript = source; 359 string compileScript = string.Empty;
360 360
361 if (language == enumCompileType.lsl) 361 if (language == enumCompileType.lsl)
362 { 362 {
363 // Its LSL, convert it to C# 363 // Its LSL, convert it to C#
364
365 StringBuilder sb = new StringBuilder(16394);
366
364 LSL_Converter = (ICodeConverter)new CSCodeGenerator(comms, m_insertCoopTerminationCalls); 367 LSL_Converter = (ICodeConverter)new CSCodeGenerator(comms, m_insertCoopTerminationCalls);
365 compileScript = LSL_Converter.Convert(source); 368 AddCSScriptHeader(
369 m_scriptEngine.ScriptClassName,
370 m_scriptEngine.ScriptBaseClassName,
371 m_scriptEngine.ScriptBaseClassParameters,
372 sb);
366 373
374 LSL_Converter.Convert(source,sb);
375 AddCSScriptTail(sb);
376 compileScript = sb.ToString();
367 // copy converter warnings into our warnings. 377 // copy converter warnings into our warnings.
368 foreach (string warning in LSL_Converter.GetWarnings()) 378 foreach (string warning in LSL_Converter.GetWarnings())
369 { 379 {
@@ -374,22 +384,24 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
374 // Write the linemap to a file and save it in our dictionary for next time. 384 // Write the linemap to a file and save it in our dictionary for next time.
375 m_lineMaps[assembly] = linemap; 385 m_lineMaps[assembly] = linemap;
376 WriteMapFile(assembly + ".map", linemap); 386 WriteMapFile(assembly + ".map", linemap);
387 LSL_Converter.Clear();
377 } 388 }
378 389 else
379 switch (language) 390 {
380 { 391 switch (language)
381 case enumCompileType.cs: 392 {
382 case enumCompileType.lsl: 393 case enumCompileType.cs:
383 compileScript = CreateCSCompilerScript( 394 compileScript = CreateCSCompilerScript(
384 compileScript, 395 compileScript,
385 m_scriptEngine.ScriptClassName, 396 m_scriptEngine.ScriptClassName,
386 m_scriptEngine.ScriptBaseClassName, 397 m_scriptEngine.ScriptBaseClassName,
387 m_scriptEngine.ScriptBaseClassParameters); 398 m_scriptEngine.ScriptBaseClassParameters);
388 break; 399 break;
389 case enumCompileType.vb: 400 case enumCompileType.vb:
390 compileScript = CreateVBCompilerScript( 401 compileScript = CreateVBCompilerScript(
391 compileScript, m_scriptEngine.ScriptClassName, m_scriptEngine.ScriptBaseClassName); 402 compileScript, m_scriptEngine.ScriptClassName, m_scriptEngine.ScriptBaseClassName);
392 break; 403 break;
404 }
393 } 405 }
394 406
395 assembly = CompileFromDotNetText(compileScript, language, asset, assembly); 407 assembly = CompileFromDotNetText(compileScript, language, asset, assembly);
@@ -419,6 +431,34 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
419// return compileScript; 431// return compileScript;
420// } 432// }
421 433
434 public static void AddCSScriptHeader(string className, string baseClassName, ParameterInfo[] constructorParameters, StringBuilder sb)
435 {
436 sb.Append(string.Format(
437@"using OpenSim.Region.ScriptEngine.Shared;
438using System.Collections.Generic;
439
440namespace SecondLife
441{{
442 public class {0} : {1}
443 {{
444 public {0}({2}) : base({3}) {{}}
445",
446 className,
447 baseClassName,
448 constructorParameters != null
449 ? string.Join(", ", Array.ConvertAll<ParameterInfo, string>(constructorParameters, pi => pi.ToString()))
450 : "",
451 constructorParameters != null
452 ? string.Join(", ", Array.ConvertAll<ParameterInfo, string>(constructorParameters, pi => pi.Name))
453 : ""
454 ));
455 }
456
457 public static void AddCSScriptTail(StringBuilder sb)
458 {
459 sb.Append(string.Format("\n }}\n}}\n"));
460 }
461
422 public static string CreateCSCompilerScript( 462 public static string CreateCSCompilerScript(
423 string compileScript, string className, string baseClassName, ParameterInfo[] constructorParameters) 463 string compileScript, string className, string baseClassName, ParameterInfo[] constructorParameters)
424 { 464 {
@@ -511,8 +551,6 @@ namespace SecondLife
511 // Do actual compile 551 // Do actual compile
512 CompilerParameters parameters = new CompilerParameters(); 552 CompilerParameters parameters = new CompilerParameters();
513 553
514 parameters.IncludeDebugInformation = true;
515
516 string rootPath = AppDomain.CurrentDomain.BaseDirectory; 554 string rootPath = AppDomain.CurrentDomain.BaseDirectory;
517 555
518 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath, 556 parameters.ReferencedAssemblies.Add(Path.Combine(rootPath,
@@ -532,26 +570,44 @@ namespace SecondLife
532 parameters.IncludeDebugInformation = CompileWithDebugInformation; 570 parameters.IncludeDebugInformation = CompileWithDebugInformation;
533 //parameters.WarningLevel = 1; // Should be 4? 571 //parameters.WarningLevel = 1; // Should be 4?
534 parameters.TreatWarningsAsErrors = false; 572 parameters.TreatWarningsAsErrors = false;
535 573 parameters.GenerateInMemory = false;
574
536 CompilerResults results; 575 CompilerResults results;
576
577 CodeDomProvider provider;
537 switch (lang) 578 switch (lang)
538 { 579 {
539 case enumCompileType.vb: 580 case enumCompileType.vb:
540 results = VBcodeProvider.CompileAssemblyFromSource( 581// results = VBcodeProvider.CompileAssemblyFromSource(
541 parameters, Script); 582// parameters, Script);
583 provider = CodeDomProvider.CreateProvider("VisualBasic");
542 break; 584 break;
543 case enumCompileType.cs: 585 case enumCompileType.cs:
544 case enumCompileType.lsl: 586 case enumCompileType.lsl:
587 provider = CodeDomProvider.CreateProvider("CSharp");
588 break;
589 default:
590 throw new Exception("Compiler is not able to recongnize " +
591 "language type \"" + lang.ToString() + "\"");
592 }
593
594 if(provider == null)
595 throw new Exception("Compiler failed to load ");
596
597
545 bool complete = false; 598 bool complete = false;
546 bool retried = false; 599 bool retried = false;
600
547 do 601 do
548 { 602 {
549 lock (CScodeProvider) 603// lock (CScodeProvider)
550 { 604// {
551 results = CScodeProvider.CompileAssemblyFromSource( 605// results = CScodeProvider.CompileAssemblyFromSource(
606// parameters, Script);
607// }
608
609 results = provider.CompileAssemblyFromSource(
552 parameters, Script); 610 parameters, Script);
553 }
554
555 // Deal with an occasional segv in the compiler. 611 // Deal with an occasional segv in the compiler.
556 // Rarely, if ever, occurs twice in succession. 612 // Rarely, if ever, occurs twice in succession.
557 // Line # == 0 and no file name are indications that 613 // Line # == 0 and no file name are indications that
@@ -575,11 +631,11 @@ namespace SecondLife
575 complete = true; 631 complete = true;
576 } 632 }
577 } while (!complete); 633 } while (!complete);
578 break; 634// break;
579 default: 635// default:
580 throw new Exception("Compiler is not able to recongnize " + 636// throw new Exception("Compiler is not able to recongnize " +
581 "language type \"" + lang.ToString() + "\""); 637// "language type \"" + lang.ToString() + "\"");
582 } 638// }
583 639
584// foreach (Type type in results.CompiledAssembly.GetTypes()) 640// foreach (Type type in results.CompiledAssembly.GetTypes())
585// { 641// {
@@ -628,6 +684,8 @@ namespace SecondLife
628 } 684 }
629 } 685 }
630 686
687 provider.Dispose();
688
631 if (hadErrors) 689 if (hadErrors)
632 { 690 {
633 throw new Exception(errtext); 691 throw new Exception(errtext);
@@ -785,15 +843,16 @@ namespace SecondLife
785 843
786 private static void WriteMapFile(string filename, Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap) 844 private static void WriteMapFile(string filename, Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap)
787 { 845 {
788 string mapstring = String.Empty; 846 StringBuilder mapbuilder = new StringBuilder(1024);
847
789 foreach (KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> kvp in linemap) 848 foreach (KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>> kvp in linemap)
790 { 849 {
791 KeyValuePair<int, int> k = kvp.Key; 850 KeyValuePair<int, int> k = kvp.Key;
792 KeyValuePair<int, int> v = kvp.Value; 851 KeyValuePair<int, int> v = kvp.Value;
793 mapstring += String.Format("{0},{1},{2},{3}\n", k.Key, k.Value, v.Key, v.Value); 852 mapbuilder.Append(String.Format("{0},{1},{2},{3}\n", k.Key, k.Value, v.Key, v.Value));
794 } 853 }
795 854
796 Byte[] mapbytes = Encoding.ASCII.GetBytes(mapstring); 855 Byte[] mapbytes = Encoding.ASCII.GetBytes(mapbuilder.ToString());
797 856
798 using (FileStream mfs = File.Create(filename)) 857 using (FileStream mfs = File.Create(filename))
799 mfs.Write(mapbytes, 0, mapbytes.Length); 858 mfs.Write(mapbytes, 0, mapbytes.Length);