internal static Label FromBinaryReader(BinaryReader reader, AxData parent, int index) { Label ret = new Label(index); ret.tokenOffset = reader.ReadInt32(); return(ret); }
private PrimitiveToken ReadPrimitive(BinaryReader reader, AxData data) { PrimitiveToken ret = null; int theTokenOffset = tokenOffset; int type = reader.ReadByte(); int flag = reader.ReadByte(); int value = 0; int extraValue = -1; tokenOffset += 1; if ((flag & 0x80) == 0x80) { value = reader.ReadInt32(); tokenOffset += 2; } else { value = reader.ReadUInt16(); tokenOffset += 1; } HspDictionaryKey key = new HspDictionaryKey(); key.Type = type; key.Value = value; HspDictionaryValue dicValue; if (dictionary.CodeLookUp(key, out dicValue)) { if ((dicValue.Extra & HspCodeExtraFlags.HasExtraInt16) == HspCodeExtraFlags.HasExtraInt16) { //HSP3.0aの仕様では行頭にないif,elseはジャンプ先アドレスを持たない。 if ((flag & 0x20) == 0x20) { extraValue = reader.ReadUInt16(); tokenOffset += 1; ret = createPrimitive(data, dicValue, theTokenOffset, type, flag, value, extraValue); } else { ret = createPrimitive(data, dicValue, theTokenOffset, type, flag, value, -1); } } else { ret = createPrimitive(data, dicValue, theTokenOffset, type, flag, value, -1); } } else { ret = createPrimitive(data, new HspDictionaryValue(), theTokenOffset, type, flag, value, -1); } ret.SetName(); return(ret); }
internal static Function FromBinaryReader(BinaryReader reader, AxData parent, int index) { Function ret = new Function(index); ret.dllIndex = reader.ReadInt16(); ret.functionIndex = reader.ReadInt16(); int paramStart = reader.ReadInt32(); int paramCount = reader.ReadInt32(); if (paramCount != 0) { ret.functionParams = parent.FunctionParams.GetRange(paramStart, paramCount); } ret.strIndex = reader.ReadInt32(); if (ret.strIndex >= 0) { ret.defaultName = parent.ReadStringLiteral(ret.strIndex); } ret.paramSizeSum = reader.ReadInt32(); ret.labelIndex = reader.ReadInt32(); ret.int_0 = reader.ReadInt16(); ret.flags = reader.ReadInt16(); switch (ret.Type) { case FunctionType.defcfunc: case FunctionType.deffunc: Label label = parent.GetLabel(ret.labelIndex); if (label != null) { label.SetFunction(ret); } ret.label = label; break; case FunctionType.func: case FunctionType.cfunc: case FunctionType.comfunc: Usedll dll = parent.GetUsedll(ret.dllIndex); if (dll != null) { dll.AddFunction(ret); } ret.dll = dll; break; case FunctionType.module: parent.Modules.Add(ret); break; } return(ret); }
internal static PlugIn FromBinaryReader(BinaryReader reader, AxData parent, int index) { PlugIn ret = new PlugIn(index); ret.int_0 = reader.ReadInt32(); int dllNameOffset = reader.ReadInt32(); int exportNameOffset = reader.ReadInt32(); ret.int_3 = reader.ReadInt32(); ret.dllName = parent.ReadStringLiteral(dllNameOffset); ret.exportName = parent.ReadStringLiteral(exportNameOffset); return(ret); }
internal static Param FromBinaryReader(BinaryReader reader, AxData parent, int index) { Param ret = new Param(index); ret.paramType = reader.ReadUInt16(); if (!parent.Dictionary.ParamLookUp(ret.paramType, out ret.paramTypeName)) { ret.paramTypeName = "NULL"; } ret.deffuncIndex = reader.ReadInt16(); ret.paramStartByte = reader.ReadInt32(); return(ret); }
private void subAnalyzePreprocessor(List <LogicalLine> ret, AxData data) { if (data.Runtime != null) { ret.Add(new PreprocessorDeclaration(data.Runtime)); ret.Add(new CommentLine()); } if (data.Modules.Count != 0) { foreach (Function module in data.Modules) { LogicalLine line = new PreprocessorDeclaration(module); line.AddError("deHSPの出力する#structはHSPの公開されている言語仕様には含まれません"); ret.Add(line); //ret.Add(new EndOfModule());//#globalイラネ } } foreach (Usedll dll in data.Usedlls) { ret.Add(new PreprocessorDeclaration(dll)); List <Function> funcs = dll.GetFunctions(); if (funcs != null) { foreach (Function func in funcs) { ret.Add(new PreprocessorDeclaration(func)); } } ret.Add(new CommentLine()); } foreach (PlugIn plugin in data.PlugIns) { ret.Add(new PreprocessorDeclaration(plugin)); Dictionary <int, Cmd> cmds = plugin.GetCmds(); foreach (Cmd cmd in cmds.Values) { ret.Add(new PreprocessorDeclaration(cmd)); } ret.Add(new CommentLine()); } }
internal TokenCollection Analyze(AxData data) { if (!data.IsStarted) { throw new InvalidOperationException(); } TokenCollection stream = new TokenCollection(); BinaryReader reader = data.Reader; long sizeOfCode = data.Header.CodeSize; long startOfCode = data.StartOfCode; tokenOffset = 0; reader.BaseStream.Seek(startOfCode, SeekOrigin.Begin); while (tokenOffset < sizeOfCode) { PrimitiveToken code = ReadPrimitive(reader, data); if (code != null) { stream.Add(code); } } #if DEBUG List <int> variablesCount = null; variablesCount = subAnalyzeVariables(stream); for (int i = 0; i < variablesCount.Count; i++) { if (variablesCount[i] == 1) { string errMsg = GlobalVariablePrimitive.ToString(i); errMsg += ":この変数は一度しか使われていません"; global::KttK.HspDecompiler.HspConsole.Warning(errMsg); } if (variablesCount[i] == 0) { string errMsg = GlobalVariablePrimitive.ToString(i); errMsg += ":この変数は使われていません"; global::KttK.HspDecompiler.HspConsole.Warning(errMsg); } } #endif return(stream); }
/// <summary> /// ラベル追加 /// </summary> /// <param defaultName="ret"></param> /// <param defaultName="data"></param> private void subAnalyzeLabel(List <LogicalLine> ret, AxData data) { foreach (LogicalLine line in ret) { line.CheckLabel(); } data.DeleteInvisibleLables(); data.RenameLables(); int i = 0; foreach (Label label in data.Labels) { if (label.TokenOffset == -1) { continue; } while ((i < ret.Count) && ((ret[i].TokenOffset == -1) || (label.TokenOffset > ret[i].TokenOffset))) { i++; } //if(value){ // 〜〜 //} //*label //else{ // 〜〜 //} //となるのを回避する処理。 //入れようとしている場所(i)がelse節の直前でScoopEndの直後ならScoopEndの前に移動。 if ((i > 0) && (ret[i] is IfStatement)) { IfStatement ifStatement = ret[i] as IfStatement; if ((ret[i - 1] is ScoopEnd) && (ifStatement.isElseStatement)) { i--; } } ret.Insert(i, new PreprocessorDeclaration(label)); continue; } }
internal void SetFunction(AxData parent) { if (deffuncIndex < 0) { return; } module = parent.GetUserFunction(deffuncIndex); if (module == null) { return; } if (module.IsModuleFunction) { if (this.IsModuleType) { this.nameFormatter = module.FunctionName; } else { isStructParameter = true; } } }
internal static Usedll FromBinaryReader(BinaryReader reader, AxData parent, int index) { Usedll ret = new Usedll(index); ret.type = reader.ReadInt32(); int nameOffset = reader.ReadInt32(); ret.int_2 = reader.ReadInt32(); int clsNameOffset = reader.ReadInt32(); switch (ret.Type) { case UsedllType.usecom: ret.name = parent.ReadIidCodeLiteral(nameOffset); ret.clsName = parent.ReadStringLiteral(clsNameOffset); break; case UsedllType.uselib: ret.name = parent.ReadStringLiteral(nameOffset); break; } return(ret); }
internal List <LogicalLine> Analyze(TokenCollection stream, AxData data) { List <LogicalLine> ret = new List <LogicalLine>(); subAnalyzePreprocessor(ret, data); readingLine = ret.Count; //構文解析 while (!stream.NextIsEndOfStream) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(0); readingLine++; LogicalLine line = LogicalLineFactory.GetCodeToken(stream.GetLine()); if (line != null) { ret.Add(line); } } //ゴーストの削除 foreach2とかstopの後のgoto文とか。 //repeatなどの後に出るラベルは構文解析で削除 for (int i = 0; i < ret.Count; i++) { if (ret[i].HasFlagIsGhost) { ret[i].Visible = false; } if ((ret[i].HasFlagGhostGoto) && (i != (ret.Count - 1))) { ret[i + 1].Visible = false; } } ret = ret.FindAll(IsVisible); for (int i = 0; i < ret.Count; i++) { if (!ret[i].CheckRpn()) { ret[i].AddError("式:数式の変換に失敗"); } } //subAnalyzeScoop、subAnalyzeLabelの順でやると //if(value){ // 〜〜 //} //*label //else{ // 〜〜 //} //となった時にエラー //しかしながらsubAnalyzeLabel、subAnalyzeScoopの順でやると //if(value){ // 〜〜 //*label //} //return //となってみっともない。 //そこでsubAnalyzeScoop、subAnalyzeLabelの順で行い、subAnalyzeLabelの際に直後がelse節で直前がScoopEndなら一つ戻す処理を入れる。 subAnalyzeScoop(ret); subAnalyzeLabel(ret, data); //タブ整形や余分な行の削除を行う int tabCount = 1; for (int i = 0; i < ret.Count; i++) { if (ret[i].TabDecrement) { tabCount--; } ret[i].TabCount = tabCount; if (ret[i].TabIncrement) { tabCount++; } } for (int i = 0; i < ret.Count; i++) { if (ret[i].GetErrorMes().Count != 0) { foreach (string errMes in ret[i].GetErrorMes()) { global::KttK.HspDecompiler.HspConsole.Warning(errMes, i + 1); } } } ret[ret.Count - 1].Visible = false; //末尾に自動生成されるstop文の削除 ret = ret.FindAll(IsVisible); return(ret); }
private PrimitiveToken createPrimitive(AxData data, HspDictionaryValue dicValue, int theTokenOffset, int type, int flag, int value, int extraValue) { PrimitiveTokenDataSet dataset = new PrimitiveTokenDataSet(); dataset.Parent = data; dataset.DicValue = dicValue; dataset.TokenOffset = theTokenOffset; dataset.Type = type; dataset.Flag = flag; dataset.Value = value; dataset.Name = dicValue.Name; switch (dicValue.Type) { case HspCodeType.Label: return(new LabelPrimitive(dataset)); case HspCodeType.Integer: return(new IntegerPrimitive(dataset)); case HspCodeType.Double: return(new DoublePrimitive(dataset, data.ReadDoubleLiteral(value))); case HspCodeType.String: return(new StringPrimitive(dataset, data.ReadStringLiteral(value))); case HspCodeType.Symbol: return(new SymbolPrimitive(dataset)); case HspCodeType.Param: return(new ParameterPrimitive(dataset)); case HspCodeType.Variable: return(new GlobalVariablePrimitive(dataset)); case HspCodeType.Operator: return(new OperatorPrimitive(dataset)); case HspCodeType.IfStatement: case HspCodeType.ElseStatement: if (extraValue >= 0) { return(new IfStatementPrimitive(dataset, extraValue)); } else { return(new HspFunctionPrimitive(dataset)); } case HspCodeType.HspFunction: return(new HspFunctionPrimitive(dataset)); case HspCodeType.OnStatement: return(new OnFunctionPrimitive(dataset)); case HspCodeType.OnEventStatement: return(new OnEventFunctionPrimitive(dataset)); case HspCodeType.McallStatement: return(new McallFunctionPrimitive(dataset)); case HspCodeType.UserFunction: return(new UserFunctionPrimitive(dataset)); case HspCodeType.DllFunction: return(new DllFunctionPrimitive(dataset)); case HspCodeType.PlugInFunction: return(new PlugInFunctionPrimitive(dataset)); case HspCodeType.ComFunction: return(new ComFunctionPrimitive(dataset)); case HspCodeType.NONE: default: break; } return(new UnknownPrimitive(dataset)); }