Skip to content

Commit

Permalink
Reorder some instructions in dynamic compiler, update assembly version.
Browse files Browse the repository at this point in the history
  • Loading branch information
r3c committed Jun 10, 2014
1 parent 4001e94 commit 25fd47e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cottle/Cottle.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<iconUrl>https://raw.github.com/r3c/Cottle/master/icon.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>Add generators, fix bug in map values, move namespaces.</releaseNotes>
<releaseNotes>Add dynamic compiler & optimizer.</releaseNotes>
<copyright>$copyright$</copyright>
<tags>Cottle Template Engine Text Transform Format</tags>
</metadata>
Expand Down
4 changes: 2 additions & 2 deletions Cottle/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut
// en utilisant '*', comme indiqué ci-dessous :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.3.0")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
17 changes: 12 additions & 5 deletions Cottle/src/Documents/Dynamic/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void CompileCommand (Command command, Label exit, int depth, bool isolat
case CommandType.AssignFunction:
this.EmitPushScope ();
this.EmitPushValue (command.Name);
this.EmitPushValue (new FunctionValue (new Function (command.Arguments, command.Body, this.trimmer)));
this.EmitPushValue (new FunctionValue (new Function (command.Arguments, command.Body, this.trimmer, string.Empty)));
this.EmitCallScopeSet (command.Mode);

break;
Expand Down Expand Up @@ -485,16 +485,24 @@ private void CompileExpression (Expression expression)
this.EmitPushScope ();
this.EmitPushOutput ();

value = this.LocalReserve<Value> ();

this.generator.Emit (OpCodes.Callvirt, Resolver.Method<Func<IFunction, IList<Value>, IScope, TextWriter, Value>> ((f, a, s, o) => f.Execute (a, s, o)));
this.generator.Emit (OpCodes.Stloc, value);
this.generator.Emit (OpCodes.Br_S, success);

// Emit void value on error
this.generator.MarkLabel (failure);

this.EmitPushVoid ();

this.generator.Emit (OpCodes.Stloc, value);

// Value is already available on stack
this.generator.MarkLabel (success);
this.generator.Emit (OpCodes.Ldloc, value);

this.LocalRelease<Value> (value);

break;

Expand All @@ -511,10 +519,6 @@ private void CompileExpression (Expression expression)

for (int i = 0; i < expression.Elements.Length; ++i)
{
this.generator.Emit (OpCodes.Ldloc, arguments);
this.generator.Emit (OpCodes.Ldc_I4, i);
this.generator.Emit (OpCodes.Ldelema, typeof (KeyValuePair<Value, Value>));

this.CompileExpression (expression.Elements[i].Key);

key = this.LocalReserve<Value> ();
Expand All @@ -526,6 +530,9 @@ private void CompileExpression (Expression expression)
value = this.LocalReserve<Value> ();

this.generator.Emit (OpCodes.Stloc, value);
this.generator.Emit (OpCodes.Ldloc, arguments);
this.generator.Emit (OpCodes.Ldc_I4, i);
this.generator.Emit (OpCodes.Ldelema, typeof (KeyValuePair<Value, Value>));
this.generator.Emit (OpCodes.Ldloc, key);
this.generator.Emit (OpCodes.Ldloc, value);
this.generator.Emit (OpCodes.Newobj, constructor);
Expand Down
45 changes: 29 additions & 16 deletions Cottle/src/Documents/Dynamic/Function.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;

namespace Cottle.Documents.Dynamic
Expand All @@ -17,36 +18,24 @@ class Function : IFunction

#region Constructors

// private static int g = 0;

public Function (IEnumerable<string> arguments, Command command, Trimmer trimmer)
public Function (IEnumerable<string> arguments, Command command, Trimmer trimmer, string name)
{
Compiler compiler;
DynamicMethod method;

method = new DynamicMethod (string.Empty, typeof (Value), new [] {typeof (Storage), typeof (IList<Value>), typeof (IScope), typeof (TextWriter)}, this.GetType ());
compiler = new Compiler (method.GetILGenerator (), trimmer);

// int p = g++;
if (!string.IsNullOrEmpty (name))
this.Save (arguments, command, trimmer, name);

this.storage = compiler.Compile (arguments, command);
this.renderer = (Renderer)method.CreateDelegate (typeof (Renderer));
/*
var assemblyName = new System.Reflection.AssemblyName ("Dynamic_" + p);
var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assemblyBuilder.DefineDynamicModule (assemblyName.Name, assemblyName.Name + ".dll");
var program = moduleBuilder.DefineType ("Program",System.Reflection.TypeAttributes.Public);
var main = program.DefineMethod ("Main", System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Static, typeof (Value), new [] {typeof (Storage), typeof (IList<Value>), typeof (IScope), typeof (TextWriter)});
compiler = new Compiler (main.GetILGenerator (), trimmer);
compiler.Compile (arguments, command);
program.CreateType();
assemblyBuilder.Save (assemblyName.Name + ".dll");
*/
}

#endregion

#region Methods
#region Methods / Public

public int CompareTo (IFunction other)
{
Expand Down Expand Up @@ -81,5 +70,29 @@ public override string ToString ()
}

#endregion

#region Methods / Private

private void Save (IEnumerable<string> arguments, Command command, Trimmer trimmer, string name)
{
AssemblyBuilder assembly;
Compiler compiler;
MethodBuilder method;
ModuleBuilder module;
TypeBuilder program;

assembly = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName (name), AssemblyBuilderAccess.RunAndSave);
module = assembly.DefineDynamicModule (name, name + ".dll");
program = module.DefineType ("Program", TypeAttributes.Public);
method = program.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static, typeof (Value), new [] {typeof (Storage), typeof (IList<Value>), typeof (IScope), typeof (TextWriter)});

compiler = new Compiler (method.GetILGenerator (), trimmer);
compiler.Compile (arguments, command);

program.CreateType();
assembly.Save (name + ".dll");
}

#endregion
}
}
2 changes: 1 addition & 1 deletion Cottle/src/Documents/DynamicDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public DynamicDocument (TextReader reader, ISetting setting)
});
}

this.main = new Function (new string[0], parser.Parse (reader), setting.Trimmer);
this.main = new Function (new string[0], parser.Parse (reader), setting.Trimmer, string.Empty);
}

public DynamicDocument (TextReader reader) :
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ TODO
----

- Add sample "include" function in documentation
- Reduce .maxstack size in dynamic documents (Cottle.Documents.Dynamic.Compiler)
- Support new "extended syntax" (Cottle.Parsers.DefaultParser)

DONE
Expand All @@ -15,3 +14,4 @@ DONE
- Fix scope enter/leave issue with "return" keyword (Cottle.Documents.Dynamic.Compiler)
- Optimize string and value storage lookup (Cottle.Documents.Dynamic.Compiler)
- Write "return" optimizer (Cottle.Parsers.Post.Optimizers.ReturnOptimizer)
- Reduce .maxstack size in dynamic documents (Cottle.Documents.Dynamic.Compiler)

0 comments on commit 25fd47e

Please sign in to comment.