예제 #1
0
        public MethodInfo CompileDynamicMethod(string name, CodeTreeNode codeTreeNode)
        {
            Block generator = new Block();
            codeTreeNode.EmitRun(generator);

            DynamicMethod method = new DynamicMethod(
                name,
                typeof(object), 
                new Type[]{typeof(Runtime), typeof(IScope)},
                moduleBuilder);

            generator.Build(new DynamicMethodProxy(method));

            return method;
        }
예제 #2
0
 public void Emit(Block block)
 {
     lines.AddRange(block.lines);
 }
예제 #3
0
        private void DefineParserConstructor()
        {
            OrderedSet<string> fields = pattern.Fields;
            
            Type[] parameterTypes = new Type[1 + fields.Count];
            
            parameterTypes[0] = typeof(Source);
            
            for (int n = 1; n < parameterTypes.Length; n++)
                parameterTypes[n] = typeof(object);

            ConstructorBuilder builder = typeBuilder.DefineConstructor(
                MethodAttributes.Public,
                CallingConventions.Standard,
                parameterTypes);
            
            Block generator = new Block();
            
            // Call the base constructor
            
            ConstructorInfo baseConstructor
                = typeBuilder.BaseType.GetConstructor(new Type[]{typeof(Source)});
            
            if (baseConstructor == null)
                throw new Exception();
            
            generator.LoadArg(0);
            generator.LoadArg(1);
            generator.Call(baseConstructor);
            
            SetMethodsToInitialValues(generator);
            
            // Save the fields
            
            for (int n = 0; n < fields.Count; n++)
            {
                generator.LoadArg(0);
                generator.LoadArg(n + 2);
                generator.StoreField(typeBuilder.GetField(fields[n]));
            }
            
            generator.Return();
            
            generator.Build(new ConstructorBuilderProxy(builder));
        }
예제 #4
0
 private void SetMethodsToInitialValues(Block generator)
 {
     foreach (Method method in methods)
     {
         generator.LoadArg(0);
         generator.Load(method);
         generator.StoreField(typeBuilder.GetField(method.Name));
     }
 }
예제 #5
0
 private void DefineConstructor(Method constructor)
 {
     Type[] parameters = new Type[1 + constructor.Parameters.Count];
     
     parameters[0] = typeof(RuntimeState);
     
     for (int n = 1; n < parameters.Length; n++)
         parameters[n] = typeof(object);
     
     ConstructorBuilder builder = typeBuilder.DefineConstructor(
         MethodAttributes.Public,
         CallingConventions.Standard,
         parameters);
     
     Block generator = new Block();
     
     SetMethodsToInitialValues(generator);
     
     if (!constructor.IsInstance)
         throw new Exception();
     
     MethodInfo callMethod = typeof(Method).GetMethod("Call", new Type[]{typeof(RuntimeState), typeof(object), typeof(object[])});
     
     if (callMethod == null)
         throw new Exception();
     
     generator.Load(constructor);
     generator.LoadArg(1);
     generator.LoadArg(0);
     
     generator.Load(constructor.Parameters.Count);
     generator.NewArray(typeof(object));
     
     for (int n = 0; n < constructor.Parameters.Count; n++)
     {
         generator.Dup();
         generator.Load(n);
         generator.LoadArg(n + 1);
         generator.StoreElementRef();
     }
     
     generator.Call(callMethod);
     generator.Pop();
     
     generator.Return();
     
     generator.Build(new ConstructorBuilderProxy(builder));
 }
예제 #6
0
 private void DefineDefaultConstructor()
 {
     ConstructorBuilder builder = typeBuilder.DefineConstructor(
         MethodAttributes.Public,
         CallingConventions.Standard,
         new Type[0]);
     
     Block generator = new Block();
     
     // Call the base constructor
     
     ConstructorInfo baseConstructor
         = typeBuilder.BaseType.GetConstructor(new Type[0]);
     
     if (baseConstructor != null)
     {
         generator.LoadArg(0);
         generator.Call(baseConstructor);
     }
     
     SetMethodsToInitialValues(generator);
     
     generator.Return();
     
     generator.Build(new ConstructorBuilderProxy(builder));
 }
예제 #7
0
 public virtual void EmitSet(Block generator)
 {
     throw new NotImplementedException(TypeNames.GetName(GetType()));
 }
예제 #8
0
 public override void EmitGet(Block generator)
 {
     generator.Load(o);
 }