public override void Compile(CompileContext context)
 {
     foreach (Expression exp in ChildNodes) {
         exp.Compile(context);
     }
     context.ILGenerator.Emit(OpCodes.Newobj, _constructor);
 }
 public override void Compile(CompileContext context)
 {
     ILGenerator il = context.ILGenerator;
     Left.Compile(context); if (Left is Variable) il.Emit(OpCodes.Unbox_Any, typeof(int));
     Right.Compile(context); if (Right is Variable)  il.Emit(OpCodes.Unbox_Any, typeof(int));
     switch (this.Op) {
         case ArithmeticBinOp.Divide:
             il.Emit(OpCodes.Div);
             break;
         case ArithmeticBinOp.Minus:
             il.Emit(OpCodes.Sub_Ovf);
             break;
         case ArithmeticBinOp.Multiply:
             il.Emit(OpCodes.Mul_Ovf);
             break;
         case ArithmeticBinOp.Plus:
             il.Emit(OpCodes.Add_Ovf);
             break;
         case ArithmeticBinOp.Modulo:
             il.Emit(OpCodes.Rem);
             break;
         default:
             throw new Exception("Unknown arithmetic binary operation: " + this.Op);
     }
 }
 public override void Compile(CompileContext context)
 {
     ILGenerator il = context.ILGenerator;
     Left.Compile(context); if (Left is Variable) il.Emit(OpCodes.Unbox_Any, typeof(int));
     Right.Compile(context); if (Right is Variable) il.Emit(OpCodes.Unbox_Any, typeof(int));
     switch (this.Op) {
         case RelationalBinOp.Equal:
             il.Emit(OpCodes.Ceq);
             break;
         case RelationalBinOp.NotEqual:
             il.Emit(OpCodes.Ceq);
             il.Emit(OpCodes.Ldc_I4_0);
             il.Emit(OpCodes.Ceq);
             break;
         case RelationalBinOp.GreaterThan:
             il.Emit(OpCodes.Cgt);
             break;
         case RelationalBinOp.LessThan:
             il.Emit(OpCodes.Clt);
             break;
         case RelationalBinOp.GreaterThanOrEqual:
             il.Emit(OpCodes.Clt);
             il.Emit(OpCodes.Ldc_I4_0);
             il.Emit(OpCodes.Ceq);
             break;
         case RelationalBinOp.LessThanOrEqual:
             il.Emit(OpCodes.Cgt);
             il.Emit(OpCodes.Ldc_I4_0);
             il.Emit(OpCodes.Ceq);
             break;
         default:
             throw new Exception("Unknown relational binary operation: " + this.Op);
     }
 }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(ChannelSyncAction));

            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, Name);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldc_I4, _children.Count);
            il.Emit(OpCodes.Ldc_I4_0);
            il.Emit(OpCodes.Newobj, typeof(ChannelSyncAction).GetConstructor(new Type[] { typeof(string), typeof(ProcessBase), typeof(int), typeof(bool) }));
            il.Emit(OpCodes.Stloc, syncObject);

            //Now put the result of any expression we have into the sync objects
            foreach (Expression exp in _children) {
                il.Emit(OpCodes.Ldloc, syncObject);
                exp.Compile(context);
                if (exp is ArithmeticExpression || exp is MethodCallExpression) {
                    il.Emit(OpCodes.Box, typeof(int));
                }
                il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("AddValue"));
            }

            il.Emit(OpCodes.Ldloc, syncObject);
            il.Emit(OpCodes.Call, SyncMethod);
            //Do nothing here after. In an action class that actually does something we would
            //compile it here.
            context.MarkSequencePoint(this.LexicalInfo);
        }
 public override void Compile(CompileContext context)
 {
     if (_value) {
         context.ILGenerator.Emit(OpCodes.Ldc_I4_1);
     } else {
         context.ILGenerator.Emit(OpCodes.Ldc_I4_0);
     }
 }
        protected void OverridePreProcess(CompileContext context, MethodInfo newMethod)
        {
            MethodInfo baseGetPreProcess = typeof(ProcessBase).GetMethod("get_PreProcess");
            MethodBuilder getPreProcess = context.Type.Builder.DefineMethod("get_PreProcess", MethodAttributes.Public | MethodAttributes.Virtual, typeof(PreProcessAction), new Type[] { });

            ILGenerator il = getPreProcess.GetILGenerator();
            il.Emit(OpCodes.Ldnull);
            il.Emit(OpCodes.Ldftn, newMethod);
            il.Emit(OpCodes.Newobj, typeof(PreProcessAction).GetConstructors()[0]);
            il.Emit(OpCodes.Ret);
            context.Type.Builder.DefineMethodOverride(getPreProcess, baseGetPreProcess);
        }
        public override void Compile(CompileContext context)
        {
            string innerTypeName = "Repl_" + context.Type.Name;
            ILGenerator il = context.ILGenerator;
            TypeInfo newType = null;
            newType = Process.CompileNewProcessStart(context, innerTypeName);
            Process.Compile(context);
            Process.CompileNewProcessEnd(context);
            Label startLoop = il.DefineLabel();
            il.MarkLabel(startLoop);
            LocalBuilder replCount = il.DeclareLocal(typeof(int));

            //Give the replicated process the variables as they are at this point...
            foreach (string paramName in newType.ConstructorParameters) {
                il.Emit(OpCodes.Ldloc, context.Type.GetLocal(paramName));
            }

            LocalBuilder loc = il.DeclareLocal(newType.Builder);
            il.Emit(OpCodes.Newobj, newType.Constructor);
            il.Emit(OpCodes.Stloc, loc);
            il.Emit(OpCodes.Ldloc, loc);

            //Set this process as the parent of the new proc, that allows it to activate this thread
            //again once it is past its first input action.
            il.Emit(OpCodes.Ldarg_0); //load the "this" pointer
            il.Emit(OpCodes.Call, MethodResolver.GetMethod(typeof(ProcessBase), "set_Parent"));

            //start the new instance of the replicated process
            il.Emit(OpCodes.Ldloc, loc);
            il.Emit(OpCodes.Call, MethodResolver.GetMethod(typeof(ProcessBase), "Run"));

            //Count how many we've emitted
            il.Emit(OpCodes.Ldloc, replCount);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Add);
            il.Emit(OpCodes.Stloc, replCount);

            //Print that information.
            il.Emit(OpCodes.Ldstr, "Number of " + innerTypeName + " started: ");
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new Type[] {typeof(string)}));
            il.Emit(OpCodes.Ldloc, replCount);
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] {typeof(int)}));

            //Suspend ourselves, we will be woken up by the replicated process once it gets
            //past its first action...
            il.Emit(OpCodes.Call, typeof(Thread).GetMethod("get_CurrentThread"));
            il.Emit(OpCodes.Call, typeof(Thread).GetMethod("Suspend"));
            il.Emit(OpCodes.Br, startLoop);
        }
 public override void Compile(CompileContext context)
 {
     if (_local != null) {
         context.ILGenerator.Emit(OpCodes.Ldloc, _local);
     } else {
         LocalBuilder local = context.Type.GetLocal(Name);
         if (local == null) {
             local = context.ILGenerator.DeclareLocal(typeof(object));
             if (context.Options.Debug) {
                 local.SetLocalSymInfo(Name);
             }
             context.Type.Locals.Add(Name, local);
         }
         context.ILGenerator.Emit(OpCodes.Ldloc, local);
     }
 }
 public override void Compile(CompileContext context)
 {
     ILGenerator il = context.ILGenerator;
     Left.Compile(context);
     Right.Compile(context);
     switch (this.Op) {
         case LogicalBinOp.And:
             il.Emit(OpCodes.And);
             break;
         case LogicalBinOp.Or:
             il.Emit(OpCodes.Or);
             break;
         case LogicalBinOp.Xor:
             il.Emit(OpCodes.Xor);
             break;
         default:
             throw new Exception("Unknown logical binary operation: " + this.Op);
     }
 }
        public override void Compile(CompileContext context)
        {
            if (_mapping.Count > 0) {
                MethodBuilder relabel = context.Type.Builder.DefineMethod("RelabelAction", MethodAttributes.Public | MethodAttributes.Static, typeof(IAction), new Type[] { typeof(IAction) });
                ILGenerator il = relabel.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Isinst, typeof(ChannelSyncAction));
                Label isChannelSync = il.DefineLabel();
                il.Emit(OpCodes.Brtrue, isChannelSync);

                ////If not ChannelSync then return the passed in argument
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ret);
                il.MarkLabel(isChannelSync);

                LocalBuilder channel = il.DeclareLocal(typeof(ChannelSyncAction));
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Castclass, typeof(ChannelSyncAction));
                il.Emit(OpCodes.Stloc, channel);
                Label end = il.DefineLabel();
                //Now we've got a ChannelSync object
                foreach (string from in _mapping.Keys) {
                    string to = _mapping[from];
                    //Check whether the key matches the current name
                    il.Emit(OpCodes.Ldloc, channel);
                    il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("get_Name"));
                    il.Emit(OpCodes.Ldstr, from);
                    il.Emit(OpCodes.Call, typeof(string).GetMethod("op_Equality"));
                    Label nextCheck = il.DefineLabel();

                    il.Emit(OpCodes.Brfalse, nextCheck);
                    il.Emit(OpCodes.Ldloc, channel);
                    il.Emit(OpCodes.Ldstr, to);
                    il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("set_Name"));
                    il.Emit(OpCodes.Br, end);
                    il.MarkLabel(nextCheck);
                }
                il.MarkLabel(end);
                il.Emit(OpCodes.Ldloc, channel);
                il.Emit(OpCodes.Ret);
                base.OverridePreProcess(context, relabel);
            }
        }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);

            il.Emit(OpCodes.Ldarg_0); //this, to call the Sync

            //Create methodcall sync object
            il.Emit(OpCodes.Ldstr, this.Name);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Newobj, typeof(MethodCallAction).GetConstructor(new Type[] { typeof(string), typeof(ProcessBase)}));

            //Call sync
            il.Emit(OpCodes.Call, SyncMethod);
            context.MarkSequencePoint(this.LexicalInfo);
            //Now compile the actual method call
            MethodCallExpr.Compile(context);
        }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(ChannelSyncAction));
            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, Name);
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldc_I4, _children.Count);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Newobj, typeof(ChannelSyncAction).GetConstructor(new Type[] { typeof(string), typeof(ProcessBase), typeof(int), typeof(bool) }));
            il.Emit(OpCodes.Stloc, syncObject);
            il.Emit(OpCodes.Ldloc, syncObject);
            il.Emit(OpCodes.Call, SyncMethod);
            context.MarkSequencePoint(this.LexicalInfo);

            //Save values to variables
            for (int i = 0; i < _children.Count; i++) {
                Variable var = (Variable)_children[i];

                if (context.Options.Optimize && !var.IsUsed) {
                    continue;
                }
                //Get the value to assign to it...
                il.Emit(OpCodes.Ldloc, syncObject);
                il.Emit(OpCodes.Ldc_I4, i);
                il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("GetValue"));
                //...and assign it
                LocalBuilder local = context.Type.GetLocal(var.Name);
                if (local == null) { //Inactions can be defining occurrences, so just create it...
                    local = context.ILGenerator.DeclareLocal(typeof(object));
                    if (context.Options.Debug) {
                        local.SetLocalSymInfo(var.Name);
                    }
                    context.Type.Locals.Add(var.Name, local);
                }
                il.Emit(OpCodes.Stloc, local);
            }
        }
        void CompileTupleSpaces(CompileContext context)
        {
            ILGenerator il = context.ILGenerator;
            LocalBuilder loc = il.DeclareLocal(typeof(Locality));
            LocalBuilder arr = il.DeclareLocal(typeof(object[]));
            this.tuples.Sort(delegate(TupleInfo t1, TupleInfo t2) {
                return t1.Locality.CompareTo(t2.Locality);
            });
            string lastLocality = "";
            foreach (TupleInfo ti in this.tuples) {
                if (lastLocality != ti.Locality) {
                    il.Emit(OpCodes.Ldstr, ti.Locality);
                    il.Emit(OpCodes.Call, typeof(Net).GetMethod("AddLocality"));
                    il.Emit(OpCodes.Stloc, loc);
                    lastLocality = ti.Locality;
                }
                il.Emit(OpCodes.Ldc_I4, ti.Items.Count);
                il.Emit(OpCodes.Newarr, typeof(object));
                il.Emit(OpCodes.Stloc, arr);

                for (int i = 0; i < ti.Items.Count; i++) {
                    object elem = ti.Items[i];
                    il.Emit(OpCodes.Ldloc, arr);
                    il.Emit(OpCodes.Ldc_I4, i);
                    if (elem is int) {
                        il.Emit(OpCodes.Ldc_I4, (int)elem);
                        il.Emit(OpCodes.Box, typeof(int));
                    } else if (elem is string) {
                        il.Emit(OpCodes.Ldstr, (string) elem);
                    }
                    il.Emit(OpCodes.Stelem_Ref);
                }
                il.Emit(OpCodes.Ldloc, loc);
                il.Emit(OpCodes.Ldloc, arr);
                il.Emit(OpCodes.Newobj, typeof(Tuple).GetConstructors()[0]);
                il.Emit(OpCodes.Call, typeof(Locality).GetMethod("Out"));
            }
        }
        public override void Compile(CompileContext context)
        {
            List<Process> processes = this.Processes;

            for (int i = 0; i < processes.Count; i++) {
                Process p = processes[i];
                string innerTypeName = "Parallel" + (i + 1);

                if (context.Options.Optimize && !p.IsUsed) {
                    continue;
                }
                TypeInfo newProcType = null;
                if (p.HasRestrictionsOrPreProcess || !(p is ProcessConstant)) {
                    newProcType = p.CompileNewProcessStart(context, innerTypeName);
                }
                p.Compile(context);

                if (newProcType != null) {
                    p.CompileNewProcessEnd(context);
                    EmitRunProcess(context, newProcType, false, p.LexicalInfo, true);
                }
            }
        }
        public override void Compile(CompileContext context)
        {
            List<Process> processes = this.Processes;

            for (int i = 0; i < processes.Count; i++) {
                Process p = (Process)processes[i];

                if (context.Options.Optimize && !p.IsUsed) {
                    continue;
                }

                if (p is ProcessConstant && !p.HasRestrictionsOrPreProcess) {
                    //Don't need to create a special proc just for wrapping this
                    ProcessConstant pc = (ProcessConstant)p;
                    EmitRunProcess(context, context.GetType(pc.Name), true, p.LexicalInfo, true);
                } else {
                    string innerTypeName = "NonDeterministic" + (i + 1);
                    TypeInfo newType  = p.CompileNewProcessStart(context, innerTypeName);
                    p.Compile(context);
                    p.CompileNewProcessEnd(context);
                    EmitRunProcess(context, newType, true, p.LexicalInfo, true);
                }
            }
        }
        public override void Compile(CompileContext context)
        {
            if (_channelNames.Count > 0) {
                MethodBuilder restrict = context.Type.Builder.DefineMethod("RestrictByName", MethodAttributes.Public | MethodAttributes.Static, typeof(bool), new Type[] { typeof(IAction) });
                ILGenerator il = restrict.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Isinst, typeof(ChannelSyncAction));
                Label isChannelSync = il.DefineLabel();
                il.Emit(OpCodes.Brtrue, isChannelSync);

                ////If not ChannelSync then return false
                il.Emit(OpCodes.Ldc_I4_0);
                il.Emit(OpCodes.Ret);
                il.MarkLabel(isChannelSync);

                LocalBuilder channel = il.DeclareLocal(typeof(ChannelSyncAction));
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Castclass, typeof(ChannelSyncAction));
                il.Emit(OpCodes.Stloc, channel);

                //Now we've got a ChannelSync object
                foreach (string channelName in _channelNames) {
                    il.Emit(OpCodes.Ldloc, channel);
                    il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("get_Name"));
                    il.Emit(OpCodes.Ldstr, channelName);
                    il.Emit(OpCodes.Call, typeof(string).GetMethod("op_Equality"));
                }
                //Or them all together
                for (int i = 0; i < _channelNames.Count - 1; i++) {
                    il.Emit(OpCodes.Or);
                }

                il.Emit(OpCodes.Ret);
                base.OverrideRestrict(context, restrict);
            }
        }
 public override void Compile(CompileContext context)
 {
     if (context.Options.Optimize && !ElseBranch.IsUsed) {
         IfBranch.Compile(context);
         return;
     } else if (context.Options.Optimize && !IfBranch.IsUsed) {
         ElseBranch.Compile(context);
         return;
     }
     Label elseStart = context.ILGenerator.DefineLabel();
     Label elseEnd = context.ILGenerator.DefineLabel();
     context.MarkSequencePoint(Expression.LexicalInfo);
     this.Expression.Compile(context);
     context.ILGenerator.Emit(OpCodes.Brfalse, elseStart);
     IfBranch.Compile(context);
     context.ILGenerator.Emit(OpCodes.Br, elseEnd);
     context.ILGenerator.MarkLabel(elseStart);
     ElseBranch.Compile(context);
     context.ILGenerator.MarkLabel(elseEnd);
     if (context.Options.Debug && ElseBranch.LexicalInfo.EndLine != 0) {
         LexicalInfo l = ElseBranch.LexicalInfo;
         context.ILGenerator.MarkSequencePoint(context.DebugWriter, l.EndLine, l.EndColumn + 1, l.EndLine, l.EndColumn + 1);
     }
 }
        public void Compile(CompileOptions options)
        {
            string absolutePath = Path.Combine(Environment.CurrentDirectory, options.OutputFile);
            string filename = Path.GetFileName(absolutePath);
            string folder = Path.GetDirectoryName(absolutePath);
            AssemblyName name = new AssemblyName(filename);
            AssemblyBuilder assembly = Thread.GetDomain().DefineDynamicAssembly(name, AssemblyBuilderAccess.Save, folder);
            ModuleBuilder module = assembly.DefineDynamicModule(options.OutputFile, filename, options.Debug);
            if (options.EmbedPLR) {
                EmbedAssembly(Assembly.GetExecutingAssembly());
            }

            if (_embeddedAssemblies.Count > 0) {
                GenerateAssemblyLookup(module);
            }

            CompileContext context = new CompileContext();
            context.Options = options;
            context.Module = module;
            MethodBuilder mainMethod = module.DefineGlobalMethod("Main", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[] { });
            if (options.Debug) {
                context.DebugWriter = module.DefineDocument(options.Arguments[0], Guid.Empty, Guid.Empty, SymDocumentType.Text);
                module.SetUserEntryPoint(mainMethod);
            }

            context.Module = module;
            context.ImportedClasses = _importedClasses;

            if (BeforeCompile != null) {
                BeforeCompile(context);
            }

            foreach (ProcessDefinition procdef in this) {
                procdef.CompileSignature(module, context);
            }

            if (!context.ImportedClasses.Contains(typeof(PLR.Runtime.BuiltIns).FullName)) {
                context.ImportedClasses.Add(typeof(PLR.Runtime.BuiltIns).FullName);
            }
            context.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly());
            if (options.References != "") {

                foreach (string assemblyName in options.References.Split(',')) {
                    string absoluteAssemblyPath = Path.Combine(Directory.GetCurrentDirectory(), assemblyName);
                    if (!File.Exists(absoluteAssemblyPath)) {
                        Console.Error.WriteLine("Assembly '{0}' does not exist!", absoluteAssemblyPath);
                        Environment.Exit(1);
                    }
                    context.ReferencedAssemblies.Add(Assembly.LoadFile(absoluteAssemblyPath));
                }
            }

            foreach (ProcessDefinition procdef in this) {
                context.CurrentMasterType = null;
                procdef.Compile(context);
                context.CurrentMasterType = null;
            }
            List<LocalBuilder> initial = new List<LocalBuilder>();
            context.PushIL(mainMethod.GetILGenerator());
            if (MainMethodStart != null) {
                MainMethodStart(context);
            }
            foreach (ProcessDefinition procdef in this) {
                if (procdef.EntryProc) {
                    TypeInfo startProc = context.GetType(procdef.FullName);
                    LocalBuilder loc = context.ILGenerator.DeclareLocal(startProc.Builder);
                    context.ILGenerator.Emit(OpCodes.Newobj, startProc.Constructor);
                    context.ILGenerator.Emit(OpCodes.Stloc, loc);
                }
            }

            //Run Scheduler, who now knows all the new Processes
            CallScheduler("Run", true, context);

            if (MainMethodEnd!= null) {
                MainMethodEnd(context);
            }

            //return 0;
            context.ILGenerator.Emit(OpCodes.Ldc_I4_0);
            context.ILGenerator.Emit(OpCodes.Ret);

            if (AfterCompile != null) {
                AfterCompile(context);
            }
            module.CreateGlobalFunctions();
            assembly.SetEntryPoint(mainMethod, PEFileKinds.ConsoleApplication);
            assembly.Save(filename);
        }
 public override void Compile(CompileContext context)
 {
 }
        private void GenerateAssemblyLookup(ModuleBuilder module)
        {
            if (_embeddedAssemblies.Count == 0) {
                return;
            }
            foreach (Assembly ass in _embeddedAssemblies) {
                string shortname = ass.FullName.Substring(0, ass.FullName.IndexOf(","));
                string tempfile = Path.GetTempFileName();
                File.Copy(new Uri(ass.EscapedCodeBase).LocalPath, tempfile, true);
                MemoryStream ms = new MemoryStream(File.ReadAllBytes(tempfile));
                ms.Seek(0, SeekOrigin.Begin);
                module.DefineManifestResource(shortname, ms, ResourceAttributes.Public);
                File.Delete(tempfile);
            }

            MethodBuilder resolveAssemblyMethod = module.DefineGlobalMethod("ResolveAssembly", MethodAttributes.Public | MethodAttributes.Static, typeof(Assembly), new Type[] { typeof(object), typeof(System.ResolveEventArgs) });
            ILGenerator ilResolve = resolveAssemblyMethod.GetILGenerator();
            CompileContext resolvecontext = new CompileContext();
            resolvecontext.PushIL(ilResolve);
            LocalBuilder localStream = ilResolve.DeclareLocal(typeof(Stream));
            LocalBuilder localBuf = ilResolve.DeclareLocal(typeof(byte[]));
            LocalBuilder localName = ilResolve.DeclareLocal(typeof(string));

            ilResolve.Emit(OpCodes.Ldarg_1);
            ilResolve.Emit(OpCodes.Call, typeof(ResolveEventArgs).GetMethod("get_Name"));
            ilResolve.Emit(OpCodes.Stloc, localName);

            ilResolve.Emit(OpCodes.Ldloc, localName);
            ilResolve.Emit(OpCodes.Ldc_I4_0);
            ilResolve.Emit(OpCodes.Ldloc, localName);
            ilResolve.Emit(OpCodes.Ldstr, ",");
            ilResolve.Emit(OpCodes.Call, typeof(string).GetMethod("IndexOf", new Type[] { typeof(string) }));
            ilResolve.Emit(OpCodes.Call, typeof(string).GetMethod("Substring", new Type[] { typeof(int), typeof(int) }));
            ilResolve.Emit(OpCodes.Stloc, localName);

            Assign(localStream, Call(Call(typeof(Assembly), "GetExecutingAssembly", false), "GetManifestResourceStream", false, localName), resolvecontext);

            Label notNull = ilResolve.DefineLabel();
            ilResolve.Emit(OpCodes.Ldloc, localStream);
            ilResolve.Emit(OpCodes.Brtrue, notNull);
            {
                //Not found, just return null
                ilResolve.Emit(OpCodes.Ldnull);
                ilResolve.Emit(OpCodes.Ret);
            }
            ilResolve.MarkLabel(notNull);
            Call(localStream, "get_Length", false).Compile(resolvecontext);
            ilResolve.Emit(OpCodes.Conv_Ovf_I);
            ilResolve.Emit(OpCodes.Newarr, typeof(System.Byte));
            ilResolve.Emit(OpCodes.Stloc, localBuf);

            ilResolve.Emit(OpCodes.Ldloc, localStream);
            ilResolve.Emit(OpCodes.Ldloc, localBuf);
            ilResolve.Emit(OpCodes.Ldc_I4_0);
            ilResolve.Emit(OpCodes.Ldloc, localBuf);
            ilResolve.Emit(OpCodes.Ldlen);
            ilResolve.Emit(OpCodes.Conv_I4);
            ilResolve.Emit(OpCodes.Callvirt, typeof(Stream).GetMethod("Read", new Type[] { typeof(byte[]), typeof(int), typeof(int) }));
            ilResolve.Emit(OpCodes.Pop);

            //Notify that we loaded this embedded...
            ilResolve.Emit(OpCodes.Ldarg_1);
            ilResolve.Emit(OpCodes.Call, typeof(ResolveEventArgs).GetMethod("get_Name"));
            ilResolve.Emit(OpCodes.Ldstr, " was not found externally, loading embedded version...");
            ilResolve.Emit(OpCodes.Call, typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }));
            ilResolve.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));

            Call(typeof(Assembly), "Load", false, localBuf).Compile(resolvecontext);
            ilResolve.Emit(OpCodes.Ret);
            resolvecontext.PopIL();

            MethodBuilder moduleInitializer = module.DefineGlobalMethod(".cctor", MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.RTSpecialName, null, new Type[] { });
            ILGenerator ilStartup = moduleInitializer.GetILGenerator();
            ilStartup.Emit(OpCodes.Call, typeof(System.AppDomain).GetMethod("get_CurrentDomain"));
            ilStartup.Emit(OpCodes.Ldnull);
            ilStartup.Emit(OpCodes.Ldftn, resolveAssemblyMethod);
            ilStartup.Emit(OpCodes.Newobj, MethodResolver.GetConstructor(typeof(System.ResolveEventHandler)));
            ilStartup.Emit(OpCodes.Callvirt, MethodResolver.GetMethod(typeof(System.AppDomain), "add_AssemblyResolve"));
            ilStartup.Emit(OpCodes.Ret);
        }
        public void CompileNewProcessEnd(CompileContext context)
        {
            if (this.WrapInTryCatch) {
                context.ILGenerator.BeginCatchBlock(typeof(ProcessKilledException));
                context.ILGenerator.Emit(OpCodes.Pop); //Pop the exception off the stack
                EmitDebug("Caught ProcessKilledException", context);
                //Just catch here to abort, don't do anything
                context.ILGenerator.EndExceptionBlock();
            }

            Call(new ThisPointer(typeof(ProcessBase)), "Die", true).Compile(context);
            context.ILGenerator.Emit(OpCodes.Ret);
            context.Type.Builder.CreateType();
            context.PopType();
            context.PopIL();
        }
        public override void Compile(CompileContext context)
        {
            Type[] argtypes = GetArgTypes();

            if (_method == null) {
                _method = context.GetMethod(_methodName, argtypes);
            }

            if (_instance != null) {
                _instance.Compile(context);
            }

            ParameterInfo[] actualParams = _method.GetParameters();
            for (int i = 0 ; i< ChildNodes.Count; i++) {
                Expression exp = (Expression)ChildNodes[i];
                exp.Compile(context);
                if (exp.Type.Equals(typeof(int))
                    && actualParams[i].ParameterType.Equals(typeof(object))) {
                    context.ILGenerator.Emit(OpCodes.Box, typeof(int));
                }
                if (exp is Variable) {
                    context.ILGenerator.Emit(OpCodes.Unbox_Any, typeof(int));
                }
            }

            if (_method.IsVirtual) {
                context.ILGenerator.Emit(OpCodes.Callvirt, _method);
            } else {
                context.ILGenerator.Emit(OpCodes.Call, _method);
            }
            if (PopReturnValue && _method.ReturnType != typeof(void)) {
                context.ILGenerator.Emit(OpCodes.Pop);
            }
        }
 public override void Compile(CompileContext context)
 {
     foreach (Expression exp in this.Expressions) {
         exp.Compile(context);
         if (exp is ArithmeticExpression) {
             context.ILGenerator.Emit(OpCodes.Box, typeof(Int32));
         }
     }
     context.MarkSequencePoint(this.LexicalInfo);
     EmitRunProcess(context, context.GetType(this.FullName), false, LexicalInfo, false);
 }
 public override void Compile(CompileContext context)
 {
     context.ILGenerator.Emit(OpCodes.Ldstr, _value);
 }
 public override void Compile(CompileContext context)
 {
     _exp.Compile(context); if (_exp is Variable) context.ILGenerator.Emit(OpCodes.Unbox_Any, typeof(int));
     context.ILGenerator.Emit(OpCodes.Neg);
 }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(KLAIMAction));

            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, this.ToString());
            il.Emit(OpCodes.Newobj, typeof(KLAIMAction).GetConstructors()[0]);
            il.Emit(OpCodes.Call, SyncMethod);

            //..and here we actually do something...
            context.MarkSequencePoint(this.LexicalInfo);

            if (this.At is PLRString && ((PLRString)this.At).Value == "Screen") {
                CompileOutAtScreen(context);
                return;
            }

            LocalBuilder loc = il.DeclareLocal(typeof(Locality));
            LocalBuilder arr = il.DeclareLocal(typeof(object[]));

            if (this.At is Variable && ((Variable)this.At).Name == "self") {
                il.Emit(OpCodes.Ldstr, this.Locality);
            } else {
                this.At.Compile(context);
            }
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("GetLocality"));
            il.Emit(OpCodes.Stloc, loc);

            il.Emit(OpCodes.Ldc_I4, this.ChildNodes.Count-1);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc, arr);

            for (int i = 1; i < this.ChildNodes.Count; i++) {
                Node node = this.ChildNodes[i];
                il.Emit(OpCodes.Ldloc, arr);
                il.Emit(OpCodes.Ldc_I4, i-1);
                node.Compile(context);
                if (node is ArithmeticExpression) {
                    il.Emit(OpCodes.Box, typeof(int));
                }
                il.Emit(OpCodes.Stelem_Ref);
            }

            //Create the Tuple from the array
            LocalBuilder tuple = il.DeclareLocal(typeof(Tuple));
            il.Emit(OpCodes.Ldloc, arr);
            il.Emit(OpCodes.Newobj, typeof(Tuple).GetConstructor(new Type[] { typeof(object[]) }));
            il.Emit(OpCodes.Stloc, tuple);

            il.Emit(OpCodes.Ldloc, loc);
            il.Emit(OpCodes.Ldloc, tuple);
            il.Emit(OpCodes.Call, typeof(Locality).GetMethod("Out"));

            //Now lets print out the net for fun...

            il.EmitWriteLine("************** CURRENT TUPLES **************");
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("Display"));
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] {typeof(string)}));
        }
        private void CompileOutAtScreen(CompileContext context)
        {
            ILGenerator il = context.ILGenerator;
            LocalBuilder builder = il.DeclareLocal(typeof(StringBuilder));
            il.Emit(OpCodes.Newobj, typeof(StringBuilder).GetConstructor(new Type[] { }));
            il.Emit(OpCodes.Stloc, builder);

            for (int i = 1; i < this.ChildNodes.Count;i++ ) {
                Node node = this.ChildNodes[i];
                il.Emit(OpCodes.Ldloc, builder);
                node.Compile(context);
                if (node is ArithmeticExpression) {
                    il.Emit(OpCodes.Box, typeof(int));
                }
                il.Emit(OpCodes.Call, typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(object) }));

                if (i != ChildNodes.Count - 1) {
                    il.Emit(OpCodes.Ldloc, builder);
                    il.Emit(OpCodes.Ldstr, ", ");
                    il.Emit(OpCodes.Call, typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) }));
                }
            }

            il.Emit(OpCodes.Ldloc, builder);
            il.Emit(OpCodes.Call, typeof(StringBuilder).GetMethod("ToString", new Type[]{}));
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
        }
 public override void Compile(CompileContext context)
 {
     context.ILGenerator.Emit(OpCodes.Ldnull);
 }
        public override void Compile(CompileContext context)
        {
            Type procType = typeof(ProcessBase);
            ILGenerator il = context.ILGenerator;

            EmitDebug("Preparing to sync now...", context);
            LocalBuilder syncObject = il.DeclareLocal(typeof(KLAIMAction));

            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldarg_0); //this
            il.Emit(OpCodes.Ldstr, this.ToString());
            il.Emit(OpCodes.Newobj, typeof(KLAIMAction).GetConstructors()[0]);
            il.Emit(OpCodes.Call, SyncMethod);

            //..and here we actually do something...
            context.MarkSequencePoint(this.LexicalInfo);

            LocalBuilder loc = il.DeclareLocal(typeof(Locality));
            LocalBuilder arr = il.DeclareLocal(typeof(object[]));
            LocalBuilder tuple = il.DeclareLocal(typeof(Tuple));

            if (this.At is Variable && ((Variable)this.At).Name == "self") {
                il.Emit(OpCodes.Ldstr, this.Locality);
            } else {
                this.At.Compile(context);
            }
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("GetLocality"));
            il.Emit(OpCodes.Stloc, loc);

            il.Emit(OpCodes.Ldc_I4, this.ChildNodes.Count - 1);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc, arr);

            for (int i = 1; i < this.ChildNodes.Count; i++) {
                Node node = this.ChildNodes[i];
                il.Emit(OpCodes.Ldloc, arr);
                il.Emit(OpCodes.Ldc_I4, i - 1);
                node.Compile(context);
                if (node is ArithmeticExpression) {
                    il.Emit(OpCodes.Box, typeof(int));
                }
                il.Emit(OpCodes.Stelem_Ref);
            }

            il.Emit(OpCodes.Ldloc, loc);
            il.Emit(OpCodes.Ldloc, arr);
            il.Emit(OpCodes.Call, typeof(Locality).GetMethod(_methodName));

            //We might be blocked at this location for a very long time...

            il.Emit(OpCodes.Stloc, tuple);
            //Now lets bind our variables... if they are used in the process
            for (int i = 1; i < this.ChildNodes.Count; i++) {
                if (_children[i] is VariableBinding) {
                    VariableBinding var = (VariableBinding)_children[i];
                    if (context.Options.Optimize && !var.IsUsed) {
                        continue;
                    }

                    //Get the value to assign to it...
                    il.Emit(OpCodes.Ldloc, tuple);
                    il.Emit(OpCodes.Ldc_I4, i - 1);
                    il.Emit(OpCodes.Call, typeof(Tuple).GetMethod("GetValueAt"));

                    //...and assign it
                    LocalBuilder bindLoc = context.Type.GetLocal(var.Name);
                    if (bindLoc == null) {
                        bindLoc = il.DeclareLocal(typeof(object));
                        context.Type.Locals.Add(var.Name, bindLoc);
                        if (context.Options.Debug) {
                            bindLoc.SetLocalSymInfo(var.Name);
                        }
                    }
                    il.Emit(OpCodes.Stloc, bindLoc);
                }
            }

            //Ok, we got past the input action. If our parent is not null then we are a replicated
            //process and should notify our parent that we have really started.
            Label noParent = il.DefineLabel();
            il.Emit(OpCodes.Ldarg_0); //Load the "this" pointer
            il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("get_Parent"));
            il.Emit(OpCodes.Brfalse, noParent);
            {
                il.Emit(OpCodes.Ldarg_0); //Load the "this" pointer
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("get_Parent"));
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("get_Thread")); //find our parent thread
                il.Emit(OpCodes.Call, typeof(Thread).GetMethod("Resume"));

                //Now we've woken up our parent. Set the Parent property to null, so it doesn't get passed
                //on to the rest of this process, KLAIM has no need for the Parent and passing it on might
                //lead to later in actions spawning extra instances of the replicated process.
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldnull);
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("set_Parent"));
            }
            il.MarkLabel(noParent);

            //Now lets print out the net for fun...

            il.EmitWriteLine("************** CURRENT TUPLES **************");
            il.Emit(OpCodes.Call, typeof(Net).GetMethod("Display"));
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
            //TODO: Notify possible replicated process
        }
 public override void Compile(CompileContext context)
 {
     MethodInfo newMethod = context.GetMethod(_methodName);
     base.OverridePreProcess(context, newMethod);
 }