public ConvChoice(JsonData choices)
    {
        ID = (int)choices["ID"];
        //Destination = (int)choices["Destination"];


        Options    = new List <ConvOption>();
        choicedata = new List <Choices>();

        for (int i = 0; i < choices["Choices"].Count; ++i)
        {
            ConvOption opt = new ConvOption();

            if (choices["Destinations"][i] == null)
            {
                opt.DestinationNode = -1;
            }
            else
            {
                opt.DestinationNode = (int)choices["Destinations"][i];
            }

            opt.choicedata = new Choices((string)choices["Choices"][i], true);

            choicedata.Add(opt.choicedata);

            Options.Add(opt);
        }
    }
        public void Build(Stream fs, Stream fspdb)
        {
            this.IsBuild = false;
            this.Error   = null;

            var log = new DefLogger();

            this.modIL = new ILModule(log);
            try
            {
                modIL.LoadModule(fs, fspdb);
            }
            catch (Exception err)
            {
                log.Log("LoadModule Error:" + err.ToString());
                this.Error = err;
                return;
            }

            converterIL = new ModuleConverter(log);
            ConvOption option = new ConvOption();

            try
            {
                converterIL.Convert(modIL, option);
                finalAVM = converterIL.outModule.Build();
                IsBuild  = true;
            }
            catch (Exception err)
            {
                this.Error = err;
                log.Log("Convert IL->ASM Error:" + err.ToString());
                return;
            }
        }
    public void AddOption(string Text, ConvNode node, ConvNode dest)
    {
        if (!Nodes.Contains(node))
        {
            AddNode(node);
        }

        if (!Nodes.Contains(dest))
        {
            AddNode(dest);
        }


        ConvOption opt = new ConvOption();

        if (dest == null)
        {
            opt.DestinationNode = -1;
        }
        else
        {
            opt.DestinationNode = dest.ID;
        }

        opt.choicedata = new Choices(Text, true);

        //node.Options.Add(opt);
    }
Esempio n. 4
0
        public void Build(Stream fs, Stream fspdb, bool optimizer)
        {
            this.IsBuild = false;
            this.Error   = null;

            var log = new DefLogger();

            this.modIL = new ILModule(log);
            try
            {
                modIL.LoadModule(fs, fspdb);
            }
            catch (Exception err)
            {
                log.Log("LoadModule Error:" + err.ToString());
                this.Error = err;
                return;
            }

            converterIL = new ModuleConverter(log);
            ConvOption option = new ConvOption();

#if NDEBUG
            try
#endif
            {
                converterIL.Convert(modIL, option);
                finalNEF = converterIL.outModule.Build();
                if (optimizer)
                {
                    var   opbytes = NefOptimizeTool.Optimize(finalNEF);
                    float ratio   = (opbytes.Length * 100.0f) / (float)finalNEF.Length;
                    log.Log("optimization ratio = " + ratio + "%");
                    finalNEF = opbytes;
                }
                IsBuild = true;
            }
#if NDEBUG
            catch (Exception err)
            {
                this.Error = err;
                log.Log("Convert IL->ASM Error:" + err.ToString());
                return;
            }
#endif
            try
            {
                finialABI = vmtool.FuncExport.Export(converterIL.outModule, finalNEF);
            }
            catch
            {
            }
        }
Esempio n. 5
0
        //Console.WriteLine("helo ha:"+args[0]); //普通输出
        //Console.WriteLine("<WARN> 这是一个严重的问题。");//警告输出,黄字
        //Console.WriteLine("<WARN|aaaa.cs(1)> 这是ee一个严重的问题。");//警告输出,带文件名行号
        //Console.WriteLine("<ERR> 这是一个严重的问题。");//错误输出,红字
        //Console.WriteLine("<ERR|aaaa.cs> 这是ee一个严重的问题。");//错误输出,带文件名
        //Console.WriteLine("SUCC");//输出这个表示编译成功
        //控制台输出约定了特别的语法
        public static bool Execute(string filename, string filepdb, ILogger log)
        {
            string onlyname = System.IO.Path.GetFileNameWithoutExtension(filename);

            ILModule mod = new ILModule(log);

            System.IO.Stream fs    = null;
            System.IO.Stream fspdb = null;

            //open file
            try
            {
                fs = System.IO.File.OpenRead(filename);

                if (System.IO.File.Exists(filepdb))
                {
                    fspdb = System.IO.File.OpenRead(filepdb);
                }
            }
            catch (Exception err)
            {
                log.Log("Open File Error:" + err.ToString());
                return(false);
            }

            var exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            var curPath = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(exePath);

            //load module
            try
            {
                mod.LoadModule(fs, fspdb);
            }
            catch (Exception err)
            {
                log.Log("LoadModule Error:" + err.ToString());
                return(false);
            }
            byte[] bytes       = null;
            bool   bSucc       = false;
            string jsonstr     = null;
            string debugmapstr = null;

            //convert and build
            try
            {
                var        conv   = new ModuleConverter(log);
                ConvOption option = new ConvOption();
                option.useNep8 = false;
                option.useSysCallInteropHash = false;
                NeoModule am = conv.Convert(mod, option);

                Directory.SetCurrentDirectory(curPath);

                bytes = am.Build();
                log.Log("convert succ");


                try
                {
                    var           outjson = FuncExport.Export(am, bytes);
                    StringBuilder sb      = new StringBuilder();
                    outjson.ConvertToStringWithFormat(sb, 0);
                    jsonstr = sb.ToString();
                    log.Log("gen abi succ");
                }
                catch (Exception err)
                {
                    log.Log("gen abi Error:" + err.ToString());
                }

                try
                {
                    var           outjson = DebugInfo.ExportDebugInfo(onlyname, am);
                    StringBuilder sb      = new StringBuilder();
                    outjson.ConvertToStringWithFormat(sb, 0);
                    debugmapstr = sb.ToString();
                    log.Log("gen debug map succ");
                }
                catch (Exception err)
                {
                    log.Log("gen debug map Error:" + err.ToString());
                }
            }
            catch (Exception err)
            {
                log.Log("Convert Error:" + err.ToString());
                return(false);
            }
            //write bytes
            try
            {
                string bytesname = onlyname + ".avm";

                System.IO.File.Delete(bytesname);
                System.IO.File.WriteAllBytes(bytesname, bytes);
                log.Log("write:" + bytesname);
                bSucc = true;
            }
            catch (Exception err)
            {
                log.Log("Write Bytes Error:" + err.ToString());
                return(false);
            }
            try
            {
                string abiname = onlyname + ".abi.json";

                System.IO.File.Delete(abiname);
                System.IO.File.WriteAllText(abiname, jsonstr);
                log.Log("write:" + abiname);
                bSucc = true;
            }
            catch (Exception err)
            {
                log.Log("Write abi Error:" + err.ToString());
                return(false);
            }
            try
            {
                string debugname = onlyname + ".debug.json";

                System.IO.File.Delete(debugname);
                System.IO.File.WriteAllText(debugname, debugmapstr);
                log.Log("write:" + debugname);
                bSucc = true;
            }
            catch (Exception err)
            {
                log.Log("Write abi Error:" + err.ToString());
                return(false);
            }
            try
            {
                fs.Dispose();
                if (fspdb != null)
                {
                    fspdb.Dispose();
                }
            }
            catch
            {
            }

            return(bSucc);
        }
Esempio n. 6
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule = _in;
            //logger.Log("beginConvert.");
            this.outModule        = new NeoModule(this.logger);
            this.outModule.option = option == null ? ConvOption.Default : option;
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    NeoMethod nm = new NeoMethod();
                    if (m.Key.Contains(".cctor"))
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace  = m.Value.method.DeclaringType.FullName;
                    nm.name        = m.Value.method.FullName;
                    nm.displayName = m.Value.method.Name;

                    Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = m.Value.method.CustomAttributes;
                    foreach (var attr in ca)
                    {
                        if (attr.AttributeType.Name == "DisplayNameAttribute")
                        {
                            nm.displayName = (string)attr.ConstructorArguments[0].Value;
                        }
                    }
                    nm.inSmartContract            = m.Value.method.DeclaringType.BaseType.Name == "SmartContract";
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent();
                        ae._namespace  = e.Value.field.DeclaringType.FullName;
                        ae.name        = ae._namespace + "::" + e.Key;
                        ae.displayName = e.Value.displayName;
                        ae.returntype  = e.Value.returntype;
                        ae.paramtypes  = e.Value.paramtypes;
                        outModule.mapEvents[ae.name] = ae;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (key.Contains("_API_"))
                {
                    continue;                       //api的,不要
                }
                if (key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key.Contains(".cctor"))
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var nm = this.methodLink[m.Value];


                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        try
                        {
                            var type = m.Value.method.ReturnType.Resolve();
                            foreach (var i in type.Interfaces)
                            {
                                if (i.InterfaceType.Name == "IApiInterface")
                                {
                                    nm.returntype = "IInteropInterface";
                                }
                            }
                        }
                        catch (Exception err)
                        {
                        }

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        byte[] outcall; string name; VM.OpCode[] opcodes; string[] opdata;
                        if (IsAppCall(m.Value.method, out outcall))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsMixAttribute(m.Value.method, out opcodes, out opdata))
                        {
                            continue;
                        }

                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                if (key.Contains("::Main("))
                {
                    NeoMethod m = outModule.mapMethods[key];
                    if (m.inSmartContract)
                    {
                        foreach (var l in this.methodLink)
                        {
                            if (l.Value == m)
                            {
                                if (mainmethod != "")
                                {
                                    throw new Exception("Have too mush EntryPoint,Check it.");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("Can't find EntryPoint,Check it.");
            }
            else
            {
                //单一默认入口
                logger.Log("Find entrypoint:" + mainmethod);
            }

            outModule.mainMethod = mainmethod;
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Esempio n. 7
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule  = _in;
            this.outModule = new NeoModule(this.logger)
            {
                option = option ?? ConvOption.Default
            };
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;                        //skip system type
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                          // skip api
                }
                if (t.Key.Contains(".My."))
                {
                    continue;                         //vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    if (m.Value.method.Is_ctor())
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        //if cctor contains sth can not be as a const value.
                        //  then need 1.record these cctor's code.
                        //            2.insert them to main function
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }

                    NeoMethod nm = new NeoMethod(m.Value);
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }

                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent(e.Value);
                        outModule.mapEvents[ae.name] = ae;
                    }
                    else if (e.Value.field.IsStatic)
                    {
                        var _fieldindex = outModule.mapFields.Count;
                        var field       = new NeoField(e.Key, e.Value.type, _fieldindex);
                        outModule.mapFields[e.Value.field.FullName] = field;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;                    // skip system typee
                }
                if (key.Contains("_API_"))
                {
                    continue;                        // skip api
                }
                if (key.Contains(".My."))
                {
                    continue;                       //vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        nm.returntype = m.Value.returntype;

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        if (IsContractCall(m.Value.method, out byte[] outcall))
Esempio n. 8
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule = _in;
            //logger.Log("beginConvert.");
            this.outModule = new NeoModule(this.logger)
            {
                option = option ?? ConvOption.Default
            };
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.Is_ctor())
                    {
                        continue;
                    }
                    NeoMethod nm = new NeoMethod(m.Value);
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }

                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent(e.Value);
                        outModule.mapEvents[ae.name] = ae;
                    }
                    else if (e.Value.field.IsStatic)
                    {
                        var _fieldindex = outModule.mapFields.Count;
                        var field       = new NeoField(e.Key, e.Value.type, _fieldindex);
                        outModule.mapFields[e.Value.field.FullName] = field;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (key.Contains("_API_"))
                {
                    continue;                       //api的,不要
                }
                if (key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        try
                        {
                            var type = m.Value.method.ReturnType.Resolve();
                            foreach (var i in type.Interfaces)
                            {
                                if (i.InterfaceType.Name == "IApiInterface")
                                {
                                    nm.returntype = "IInteropInterface";
                                }
                            }
                        }
                        catch
                        {
                        }

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        if (IsAppCall(m.Value.method, out byte[] outcall))
Esempio n. 9
0
        public void Build(Stream fs, Stream fspdb, bool optimizer)
        {
            this.IsBuild      = false;
            this.Error        = null;
            this.UseOptimizer = optimizer;

            var log = new DefLogger();

            this.modIL = new ILModule(log);
            try
            {
                modIL.LoadModule(fs, fspdb);
            }
            catch (Exception err)
            {
                log.Log("LoadModule Error:" + err.ToString());
                this.Error = err;
                return;
            }

            converterIL = new ModuleConverter(log);
            Dictionary <int, int> addrConvTable = null;
            ConvOption            option        = new ConvOption();

#if NDEBUG
            try
#endif
            {
                converterIL.Convert(modIL, option);
                finalNEF = converterIL.outModule.Build();
                if (optimizer)
                {
                    var   opbytes = NefOptimizeTool.Optimize(finalNEF, out addrConvTable);
                    float ratio   = (opbytes.Length * 100.0f) / (float)finalNEF.Length;
                    log.Log("optimization ratio = " + ratio + "%");
                    finalNEF = opbytes;
                }
                IsBuild = true;
            }
#if NDEBUG
            catch (Exception err)
            {
                this.Error = err;
                log.Log("Convert IL->ASM Error:" + err.ToString());
                return;
            }
#endif
            try
            {
                finialABI = vmtool.FuncExport.Export(converterIL.outModule, finalNEF, addrConvTable);
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                var features = converterIL.outModule == null ? ContractFeatures.NoProperty : converterIL.outModule.attributes
                               .Where(u => u.AttributeType.Name == "FeaturesAttribute")
                               .Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value)
                               .FirstOrDefault();

                var extraAttributes = converterIL.outModule == null ? new List <Mono.Collections.Generic.Collection <CustomAttributeArgument> >()
                    : converterIL.outModule.attributes.Where(u => u.AttributeType.Name == "ManifestExtraAttribute").Select(attribute => attribute.ConstructorArguments).ToList();
                var storage = features.HasFlag(ContractFeatures.HasStorage).ToString().ToLowerInvariant();
                var payable = features.HasFlag(ContractFeatures.Payable).ToString().ToLowerInvariant();

                finalManifest =
                    @"{""groups"":[],""features"":{""storage"":" + storage + @",""payable"":" + payable + @"},""abi"":" +
                    finialABI +
                    @",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[],""extra"":[]" + "}";
            }
            catch
            {
            }
        }
Esempio n. 10
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule = _in;
            //logger.Log("beginConvert.");
            this.outModule = new NeoModule(this.logger)
            {
                option = option ?? ConvOption.Default
            };
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                         //api的,不要
                }
                if (t.Key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    NeoMethod nm = new NeoMethod();
                    if (m.Key.Contains(".cctor"))
                    {
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }
                    if (m.Value.method.IsConstructor)
                    {
                        continue;
                    }
                    nm._namespace  = m.Value.method.DeclaringType.FullName;
                    nm.name        = m.Value.method.FullName;
                    nm.displayName = m.Value.method.Name;

                    Mono.Collections.Generic.Collection <Mono.Cecil.CustomAttribute> ca = m.Value.method.CustomAttributes;
                    foreach (var attr in ca)
                    {
                        if (attr.AttributeType.Name == "DisplayNameAttribute")
                        {
                            nm.displayName = (string)attr.ConstructorArguments[0].Value;
                        }
                    }
                    nm.inSmartContract            = m.Value.method.DeclaringType.BaseType.Name == "SmartContract";
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent
                        {
                            _namespace  = e.Value.field.DeclaringType.FullName,
                            name        = e.Value.field.DeclaringType.FullName + "::" + e.Key,
                            displayName = e.Value.displayName,
                            returntype  = e.Value.returntype,
                            paramtypes  = e.Value.paramtypes
                        };
                        outModule.mapEvents[ae.name] = ae;
                    }
                    else if (e.Value.field.IsStatic)
                    {
                        var _fieldindex = outModule.mapFields.Count;
                        var field       = new NeoField(e.Key, e.Value.type, _fieldindex);
                        outModule.mapFields[e.Value.field.FullName] = field;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;//系统的,不要
                }
                if (key.Contains("_API_"))
                {
                    continue;                       //api的,不要
                }
                if (key.Contains(".My."))
                {
                    continue;//vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Key.Contains(".cctor"))
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;//event 自动生成的代码,不要
                    }
                    var nm = this.methodLink[m.Value];


                    //try
                    {
                        nm.returntype = m.Value.returntype;
                        try
                        {
                            var type = m.Value.method.ReturnType.Resolve();
                            foreach (var i in type.Interfaces)
                            {
                                if (i.InterfaceType.Name == "IApiInterface")
                                {
                                    nm.returntype = "IInteropInterface";
                                }
                            }
                        }
                        catch
                        {
                        }

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        if (IsAppCall(m.Value.method, out byte[] outcall))
Esempio n. 11
0
        public NeoModule Convert(ILModule _in, ConvOption option = null)
        {
            this.inModule  = _in;
            this.outModule = new NeoModule()
            {
                option = option ?? ConvOption.Default
            };
            foreach (var t in _in.mapType)
            {
                if (t.Key.Contains("<"))
                {
                    continue;                        //skip system type
                }
                if (t.Key.Contains("_API_"))
                {
                    continue;                          // skip api
                }
                if (t.Key.Contains(".My."))
                {
                    continue;                         //vb system
                }
                foreach (var m in t.Value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    if (m.Value.method.Is_ctor())
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        //if cctor contains sth can not be as a const value.
                        //  then need 1.record these cctor's code.
                        //            2.insert them to main function
                        CctorSubVM.Parse(m.Value, this.outModule);
                        continue;
                    }

                    NeoMethod nm = new NeoMethod(m.Value);
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }

                foreach (var e in t.Value.fields)
                {
                    if (e.Value.isEvent)
                    {
                        NeoEvent ae = new NeoEvent(e.Value);
                        outModule.mapEvents[ae.name] = ae;
                    }
                    else if (e.Value.field.IsStatic)
                    {
                        var _fieldindex = outModule.mapFields.Count;
                        var field       = new NeoField(e.Key, e.Value.type, _fieldindex);
                        outModule.mapFields[e.Value.field.FullName] = field;
                    }
                }
            }

            var keys = new List <string>(_in.mapType.Keys);

            foreach (var key in keys)
            {
                var value = _in.mapType[key];
                if (key.Contains("<"))
                {
                    continue;                    // skip system typee
                }
                if (key.Contains("_API_"))
                {
                    continue;                        // skip api
                }
                if (key.Contains(".My."))
                {
                    continue;                       //vb system
                }
                foreach (var m in value.methods)
                {
                    if (m.Value.method == null)
                    {
                        continue;
                    }
                    if (m.Value.method.Is_cctor())
                    {
                        continue;
                    }
                    if (m.Value.method.IsAddOn || m.Value.method.IsRemoveOn)
                    {
                        continue;                                                      // skip the code generated by event
                    }
                    var nm = this.methodLink[m.Value];

                    //try
                    {
                        nm.returntype = m.Value.returntype;

                        foreach (var src in m.Value.paramtypes)
                        {
                            nm.paramtypes.Add(new NeoParam(src.name, src.type));
                        }

                        if (IsContractCall(m.Value.method, out _))
                        {
                            continue;
                        }
                        if (IsNonCall(m.Value.method))
                        {
                            continue;
                        }
                        if (IsMixAttribute(m.Value.method, out _, out _))
                        {
                            continue;
                        }

                        if (m.Key.Contains("::Main("))
                        {
                            NeoMethod _m = outModule.mapMethods[m.Key];
                        }
                        this.ConvertMethod(m.Value, nm);
                    }
                }
            }

            if (this.outModule.mapFields.Count > MAX_STATIC_FIELDS_COUNT)
            {
                throw new Exception("too much static fields");
            }
            if (this.outModule.mapFields.Count > 0)
            {
                InsertInitializeMethod();
                logger.Log("Insert _initialize().");
            }

            var attr = outModule.mapMethods.Values.Where(u => u.inSmartContract).Select(u => u.type?.attributes.ToArray()).FirstOrDefault();

            if (attr?.Length > 0)
            {
                outModule.attributes.AddRange(attr);
            }

            var declaringTypes = outModule.mapMethods.Values
                                 .Where(u => u.inSmartContract)
                                 .Select(u => u.method?.method?.DeclaringType)
                                 .Where(u => u != null && !string.IsNullOrEmpty(u.Name))
                                 .Distinct()
                                 .ToArray();

            outModule.Name = declaringTypes.Length == 1 ? declaringTypes[0].Name : Path.GetFileNameWithoutExtension(_in.module.Name);

            this.LinkCode();

            // this.findFirstFunc();// Need to find the first method
            // Assign func addr for each method
            // Then convert the call address

            return(outModule);
        }
Esempio n. 12
0
        public BhpModule Convert(JavaModule _in, ConvOption option = null)
        {
            this.srcModule = _in;
            //logger.Log("beginConvert.");
            this.outModule        = new BhpModule(this.logger);
            this.outModule.option = option == null ? ConvOption.Default : option;

            foreach (var c in _in.classes.Values)
            {
                if (c.skip)
                {
                    continue;
                }
                foreach (var m in c.methods)
                {
                    if (m.Value.skip)
                    {
                        continue;
                    }
                    if (m.Key[0] == '<')
                    {
                        continue;                 //系統函數不要
                    }
                    BhpMethod nm = new BhpMethod();
                    nm.name                       = c.classfile.Name + "::" + m.Key;
                    nm.displayName                = m.Key;
                    nm.isPublic                   = m.Value.method.IsPublic;
                    this.methodLink[m.Value]      = nm;
                    outModule.mapMethods[nm.name] = nm;
                }
            }

            foreach (var c in _in.classes.Values)
            {
                if (c.skip)
                {
                    continue;
                }
                foreach (var m in c.methods)
                {
                    if (m.Value.skip)
                    {
                        continue;
                    }
                    if (m.Key[0] == '<')
                    {
                        continue;                 //系統函數不要
                    }
                    var nm = this.methodLink[m.Value];
                    //try
                    {
                        this.ConvertMethod(m.Value, nm);
                    }
                    //catch (Exception err)
                    //{
                    //    logger.Log("error:" + err.Message);
                    //}
                }
            }
            //转换完了,做个link,全部拼到一起
            string mainmethod = "";

            foreach (var key in outModule.mapMethods.Keys)
            {
                var name = key.Substring(key.IndexOf("::") + 2);
                var i    = name.IndexOf('(');
                name = name.Substring(0, i);

                if (name == ("Main"))
                {
                    var m = outModule.mapMethods[key];
                    foreach (var l in this.methodLink)
                    {
                        if (l.Value == m)
                        {
                            var srcm = l.Key;
                            if (srcm.DeclaringType.superClass == "org.Bhp.smartcontract.framework.SmartContract")
                            {
                                logger.Log("Find entrypoint:" + key);
                                if (mainmethod != "")
                                {
                                    throw new Exception("Have too mush EntryPoint,Check it.");
                                }
                                mainmethod = key;
                            }
                        }
                    }
                }
            }
            if (mainmethod == "")
            {
                throw new Exception("Can't find Main Method from SmartContract,Check it.");
            }
            outModule.mainMethod = mainmethod;
            //得找到第一个函数
            this.LinkCode(mainmethod);
            //this.findFirstFunc();//得找到第一个函数
            //然后给每个method 分配一个func addr
            //还需要对所有的call 做一次地址转换

            //this.outModule.Build();
            return(outModule);
        }
Esempio n. 13
0
        public void Build(Stream fs, Stream fspdb, bool optimizer)
        {
            this.IsBuild      = false;
            this.Error        = null;
            this.UseOptimizer = optimizer;

            var log = new DefLogger();

            this.modIL = new ILModule(log);
            try
            {
                modIL.LoadModule(fs, fspdb);
            }
            catch (Exception err)
            {
                log.Log("LoadModule Error:" + err.ToString());
                this.Error = err;
                return;
            }

            converterIL = new ModuleConverter(log);
            Dictionary <int, int> addrConvTable = null;
            ConvOption            option        = new ConvOption();

#if NDEBUG
            try
#endif
            {
                converterIL.Convert(modIL, option);
                finalNEFScript = converterIL.outModule.Build();
                if (optimizer)
                {
                    List <int> entryPoints = new List <int>();
                    foreach (var f in converterIL.outModule.mapMethods.Values)
                    {
                        if (!entryPoints.Contains(f.funcaddr))
                        {
                            entryPoints.Add(f.funcaddr);
                        }
                    }
                    var   opbytes = NefOptimizeTool.Optimize(finalNEFScript, entryPoints.ToArray(), out addrConvTable);
                    float ratio   = (opbytes.Length * 100.0f) / (float)finalNEFScript.Length;
                    log.Log("optimization ratio = " + ratio + "%");
                    finalNEFScript = opbytes;
                }
                IsBuild = true;
            }
#if NDEBUG
            catch (Exception err)
            {
                this.Error = err;
                log.Log("Convert IL->ASM Error:" + err.ToString());
                return;
            }
#endif
            try
            {
                finalABI = FuncExport.GenerateAbi(converterIL.outModule, addrConvTable);
            }
            catch (Exception err)
            {
                log.Log("Gen Abi Error:" + err.ToString());
                this.Error = err;
                return;
            }

            try
            {
                debugInfo = DebugExport.Export(converterIL.outModule, finalNEFScript, addrConvTable);
            }
            catch (Exception err)
            {
                log.Log("Gen debugInfo Error:" + err.ToString());
                this.Error = err;
                return;
            }

            try
            {
                finalManifest = FuncExport.GenerateManifest(finalABI, converterIL.outModule);
            }
            catch (Exception err)
            {
                log.Log("Gen Manifest Error:" + err.ToString());
                this.Error = err;
                return;
            }
        }
Esempio n. 14
0
        public bool compile(string filename, string filetext, out byte[] avmtext, out string abitext, out string maptext, out string hash)
        {
            var tree = CSharpSyntaxTree.ParseText(filetext);
            var comp = CSharpCompilation.Create("aaa.dll", new[] { tree },
                                                new[] { ref1, ref2, ref3, ref4 }, op);

            var fs     = new MemoryStream();
            var fspdb  = new MemoryStream();
            var result = comp.Emit(fs, fspdb);

            fs.Seek(0, SeekOrigin.Begin);
            fspdb.Seek(0, SeekOrigin.Begin);

            ILModule mod = new ILModule();

            mod.LoadModule(fs, fspdb);

            NeoModule am = null;

            byte[] bytes   = null;
            string jsonstr = null;
            string mapInfo = null;

            ConvOption option = new ConvOption()
            {
                useNep8 = false
            };
            var conv = new ModuleConverter(new DefLogger());

            am = conv.Convert(mod, option);
            // *.avm
            bytes   = am.Build();
            avmtext = bytes;

            // *.abi.json
            var outjson = vmtool.FuncExport.Export(am, bytes);

            jsonstr = outjson.ToString();
            abitext = jsonstr;
            hash    = outjson["hash"].ToString();

            // *.map.json
            Neo.Compiler.MyJson.JsonNode_Array arr = new Neo.Compiler.MyJson.JsonNode_Array();
            foreach (var m in am.mapMethods)
            {
                Neo.Compiler.MyJson.JsonNode_Object item = new Neo.Compiler.MyJson.JsonNode_Object();
                arr.Add(item);
                item.SetDictValue("name", m.Value.displayName);
                item.SetDictValue("addr", m.Value.funcaddr.ToString("X04"));
                Neo.Compiler.MyJson.JsonNode_Array infos = new Neo.Compiler.MyJson.JsonNode_Array();
                item.SetDictValue("map", infos);
                foreach (var c in m.Value.body_Codes)
                {
                    if (c.Value.debugcode != null)
                    {
                        var debugcode = c.Value.debugcode.ToLower();
                        //if (debugcode.Contains(".cs"))
                        {
                            infos.AddArrayValue(c.Value.addr.ToString("X04") + "-" + c.Value.debugline.ToString());
                        }
                    }
                }
            }
            mapInfo = arr.ToString();
            maptext = mapInfo;

            try
            {
                fs.Dispose();
                if (fspdb != null)
                {
                    fspdb.Dispose();
                }
            }
            catch
            {
            }
            return(true);
        }
Esempio n. 15
0
        public void Build(Stream fs, Stream fspdb, bool optimizer)
        {
            this.IsBuild      = false;
            this.Error        = null;
            this.UseOptimizer = optimizer;

            var log = new DefLogger();

            this.modIL = new ILModule(log);
            try
            {
                modIL.LoadModule(fs, fspdb);
            }
            catch (Exception err)
            {
                log.Log("LoadModule Error:" + err.ToString());
                this.Error = err;
                return;
            }

            converterIL = new ModuleConverter(log);
            Dictionary <int, int> addrConvTable = null;
            ConvOption            option        = new ConvOption();

#if NDEBUG
            try
#endif
            {
                converterIL.Convert(modIL, option);
                finalNEFScript = converterIL.outModule.Build();
                if (optimizer)
                {
                    List <int> entryPoints = new List <int>();
                    foreach (var f in converterIL.outModule.mapMethods.Values)
                    {
                        if (!entryPoints.Contains(f.funcaddr))
                        {
                            entryPoints.Add(f.funcaddr);
                        }
                    }
                    var   opbytes = NefOptimizeTool.Optimize(finalNEFScript, entryPoints.ToArray(), out addrConvTable);
                    float ratio   = (opbytes.Length * 100.0f) / (float)finalNEFScript.Length;
                    log.Log("optimization ratio = " + ratio + "%");
                    finalNEFScript = opbytes;
                }
                IsBuild = true;
            }
#if NDEBUG
            catch (Exception err)
            {
                this.Error = err;
                log.Log("Convert IL->ASM Error:" + err.ToString());
                return;
            }
#endif
            try
            {
                finalABI = FuncExport.GenerateAbi(converterIL.outModule, addrConvTable);
            }
            catch (Exception err)
            {
                log.Log("Gen Abi Error:" + err.ToString());
                this.Error = err;
                return;
            }

            try
            {
                debugInfo = DebugExport.Export(converterIL.outModule, finalNEFScript, addrConvTable);
            }
            catch (Exception err)
            {
                log.Log("Gen debugInfo Error:" + err.ToString());
                this.Error = err;
                return;
            }

            try
            {
                finalManifest = FuncExport.GenerateManifest(finalABI, converterIL.outModule);
            }
            catch (Exception err)
            {
                log.Log("Gen Manifest Error:" + err.ToString());
                this.Error = err;
                return;
            }

            try
            {
                nefFile = new NefFile
                {
                    Compiler = "neon-" + Version.Parse(((AssemblyFileVersionAttribute)Assembly.GetAssembly(typeof(Program))
                                                        .GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version).ToString(),
                    Tokens = converterIL.methodTokens.ToArray(),
                    Script = finalNEFScript
                };
                nefFile.CheckSum = NefFile.ComputeChecksum(nefFile);
            }
            catch (Exception err)
            {
                log.Log("Write Bytes Error:" + err.ToString());
                return;
            }
        }