예제 #1
0
        public void GetTables()
        {
            // 括号表示多少长度可以满足数据条数, grp esc 指令未包括, 各种ID要从1开始算(查询时再-1),0表示没有
            // 关系数据转成表ID指向数据ID+数据长度形式
            // 1Inst对应一个param,如果出现同名多参数的就分成2个

            // V $pfxcdt(byte:3), xxx ...前缀条件,满足条件指令才成立
            // V $pair(byte), xxx
            // V $name(short:10), xxx
            // V $superscript(byte:4), xxx

            // V $param(byte), param_pairID
            // V $param2(byte), param_pairID, param_pairID
            // V $param3(byte), param_pairID, param_pairID, param_pairID
            // V $param4(byte), param_pairID, param_pairID, param_pairID, param_pairID


            // X $hex(byte), hex_instID
            // $inst(short), type, pfxcdtID(byte:3), superscriptID(byte:4), inst_nameID, paramCount(byte:3), paramID ...指令情况
            // $inst_prefix, type, grp, rex...并到hex表里
            // $inst_table, type,...并
            // $inst_esc,...并
            // $inst_grp,...

            // X $param_pair(byte), pairID, len
            // V $hex_inst(short), instID, len ...表的内容
            // X $inst_name, nameID, len  // 并到inst里

            // 缺rm1、rm2、sib、esc表

            // $pair
            var pairs = GetOperandPairs().ToList(); // 包含grp
            // 可以生成 $name $inst_name
            List <NameIndex> names = CreateNames(); // 包含grp

            // 可以生成param 1234
            List <Param>[] param = GetParams(pairs); // 包含grp

            // $hex_inst - id,count
            Tuple <int, byte>[][] hexInst = CreateEmptyHexInst();

            List <Inst> insts = new List <Inst>(); // $inst 不包含grp

            // 根据指令字节分组
            foreach (var table in GetByteTables())
            {
                Tuple <int, byte>[] tmp = null;
                switch (table.Key)
                {
                case "Byte1":
                    tmp = hexInst[0];    // byte1;
                    break;

                case "Byte2_0F":
                    tmp = hexInst[1];    // byte2_0F;
                    break;

                case "Byte3_38":
                    tmp = hexInst[2];    // byte3_0F38;
                    break;

                case "Byte3_3A":
                    tmp = hexInst[3];    // byte3_0F3A;
                    break;

                default:
                    throw new Exception();
                }
                // 根据指令的HEX分组
                foreach (var opcode in table.ToLookup((op) => op.Hex[op.Hex.Count - 1]))
                {
                    tmp[Convert.ToInt32(opcode.Key, 16)] = new Tuple <int, byte>(insts.Count /* + 1*/, (byte)opcode.Count());
                    foreach (var instInfo in opcode)
                    {
                        Inst tmpInst = CreateInst(names, param, instInfo);

                        insts.Add(tmpInst);
                    }
                }
            }
            // 映射组
            var grpConv = from item in grpOpcode.Value
                          select new
            {
                Grp    = item,
                Name   = names.Find((val) => val.Name.SequenceEqual(item.Name)),
                Pfxcdt = item.Pfx == null ? string.Empty : string.Join("_", item.Pfx),
                OpId   = item.Operand == null ? 0 : param[item.Operand.Count - 1].FindIndex((p) => item.Operand.SequenceEqual(p.Params))
            };



            // 统计一下表
            // var hexInstCount = from item in hexInst select item.Count((val) => val != null);
            // 压缩byte3的两个表 - index - id,count
            List <Tuple <byte, Tuple <int, byte> > > zipByte3_38, zipByte3_3A;

            CreateZipByteTable(hexInst, out zipByte3_38, out zipByte3_3A);

            var pairsCode = GetOperandDefs(pairs);
            var namesCode = GetNamesDefCode(names);

            var paramCode = GetOperandGroupDefCode(param);

            var byte1 = GetHexInstCode(hexInst, 0);
            var byte2 = GetHexInstCode(hexInst, 1);

            var byte3_38_zip = GetHexInstCode(zipByte3_38, "HexTable3Zip_38");
            var byte3_3A_zip = GetHexInstCode(zipByte3_3A, "HexTable3Zip_3A");

            var instDic     = CreateEmptyInstDic();
            var hexInstCode = GetHexInstCode(insts, instDic);

            var instCode    = GetInstCode(instDic);
            var pfxInstCode = GetPfxInstCode(instDic);
            var grpInstCode = GetGrpInstCode(instDic); // inst里描述的grp,非grp表里描述的指令


            StringBuilder result = new StringBuilder();

            result.AppendLine(pairsCode);
            result.AppendLine(namesCode);
            result.AppendLine(paramCode);
            result.AppendLine(byte1);
            result.AppendLine(byte2);
            result.AppendLine(byte3_38_zip);
            result.AppendLine(byte3_3A_zip);
            result.AppendLine(hexInstCode);
            result.AppendLine(instCode);
            result.AppendLine(pfxInstCode);
            result.AppendLine(grpInstCode);

            var file = Path.GetTempFileName();

            File.AppendAllText(file, result.ToString());
            Process.Start("notepad.exe", file).WaitForExit();
            File.Delete(file);
            // escTable escInst grpTable grpInst rmTable sibTable   缺表
        }