JP2005085051A - Software converter - Google Patents
Software converter Download PDFInfo
- Publication number
- JP2005085051A JP2005085051A JP2003317600A JP2003317600A JP2005085051A JP 2005085051 A JP2005085051 A JP 2005085051A JP 2003317600 A JP2003317600 A JP 2003317600A JP 2003317600 A JP2003317600 A JP 2003317600A JP 2005085051 A JP2005085051 A JP 2005085051A
- Authority
- JP
- Japan
- Prior art keywords
- function
- label
- instruction
- source code
- branch
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Landscapes
- Devices For Executing Special Programs (AREA)
- Stored Programmes (AREA)
Abstract
Description
本発明は、あるマイクロコンピュータ(以下、マイコンと称す。)のCPUで実行可能なアセンブリ言語で記述されたソースコード(以下、アセンブリ言語ソースコードと称す。)を、別のCPUを搭載したマイコンで実行可能な高級言語(CやC++など)のソースコード(以下、高級言語ソースコードと称す。)に逆変換するソフトウェア変換装置に関する。 The present invention relates to a source code described in an assembly language (hereinafter referred to as assembly language source code) executable by a CPU of a certain microcomputer (hereinafter referred to as a microcomputer) by a microcomputer equipped with another CPU. The present invention relates to a software conversion device that performs reverse conversion into executable high-level language (C, C ++, etc.) source code (hereinafter referred to as high-level language source code).
セット機能の多機能化にともない、組み込みマイコンのソフトウェア開発において、高級言語であるC言語やC++言語などを用いてソフトウェアを開発する機会が増加している。一方で、従来から用いられてきたアセンブリ言語のソフトウェア資産を流用する機会も多く、アセンブリ言語ソースコードを高級言語ソースコードに逆変換(逆コンパイル)し、高級言語ソースコードで一元管理する必要性が高まってきている。このようなソフトウェア変換装置(逆コンパイラ)は、主に組み込みマイコンのソフトウェア開発に用いられている(例えば、特許文献1参照。)。 As the set functions become multi-functional, opportunities for developing software using C language, C ++ language, etc., which are high-level languages, are increasing in software development of embedded microcomputers. On the other hand, there are many opportunities to divert assembly language software assets that have been used in the past, and there is a need to reverse-convert (decompile) assembly language source code into high-level language source code and centrally manage it using high-level language source code. It is increasing. Such a software conversion device (decompiler) is mainly used for software development of an embedded microcomputer (for example, see Patent Document 1).
従来のソフトウェア変換装置は、アセンブリ言語の命令を対応する高級言語の命令に1対1で置き換えることで、逆変換を実施していた。
例えば、
Label equ *
MOV A,ram
ADD A,#2
MOV ram,A
のようなアセンブリ言語ソースコードを、
Label:
D1 = ram;
D1 += 2;
ram = D1;
のように、対応する高級言語ソースコードに変換していた。
A conventional software conversion apparatus performs reverse conversion by replacing assembly language instructions with corresponding high-level language instructions on a one-to-one basis.
For example,
Label equ *
MOV A, ram
ADD A, # 2
MOV ram, A
Assembly language source code, such as
Label:
D1 = ram;
D1 + = 2;
ram = D1;
It was converted into the corresponding high-level language source code.
しかしながら、従来のソフトウェア変換装置は、命令を1対1に置き換えるのみであるため、アセンブリ言語で記述されたcall文などの分岐命令を単に高級言語の関数コールに変換するのみであり、逆コンパイル後の高級言語ソースコードにはその分岐先のラベル(call文の引数となっているラベルなど)が関数に変換されることなくそのまま残っていた。そのため、逆コンパイル後の高級言語ソースコードに対してコンパイルしたとき、関数コールの分岐先の関数が存在しないためにエラーとなり、コンパイルできない場合も多かった。
本発明は、上記問題点に鑑み、逆コンパイル後の高級言語ソースコード内の先頭ラベルと分岐ラベル(call文の引数となっているラベルなど)に関連する命令を関数の形式に変換し、高級言語ソースコード内の命令をすべて関数に属するものにし、関数化した分岐ラベルでその関数を分割することにより、高級言語の文法に則ったソースコードを生成するソフトウェア変換装置を提供することを目的とする。 In view of the above problems, the present invention converts an instruction related to a head label and a branch label (such as a label that is an argument of a call statement) in a high-level language source code after decompilation into a function format. An object of the present invention is to provide a software conversion device that generates a source code that conforms to the grammar of a high-level language by making all instructions in a language source code belong to a function and dividing the function by a functionalized branch label. To do.
一方、このようにすべての命令が関数に属するようにし、かつ関数化した分岐ラベルでその関数を分割した場合、ラベルに分岐する命令(goto文など)の分岐先のラベルがその分岐命令の関数単位外のラベルとなり、コンパイルできなくなるおそれがある。 On the other hand, when all the instructions belong to the function and the function is divided by the functionalized branch label, the branch destination label of the instruction that branches to the label (such as a goto statement) is the function of the branch instruction. There is a possibility that it will become a label out of the unit and cannot be compiled.
そこで、本発明は、上記問題点に鑑み、関数単位外のラベルに分岐する命令の分岐先のラベルに関連する高級言語ソースコード内の命令を関数の形式に変換し、高級言語ソースコードをその分岐先のラベルでさらに分割することにより、高級言語の文法に則ったソースコードを生成するソフトウェア変換装置を提供することを目的とする。 Therefore, in view of the above problems, the present invention converts an instruction in a high-level language source code related to a branch destination label of an instruction that branches to a label outside a function unit into a function format, and converts the high-level language source code into the function format. It is an object of the present invention to provide a software conversion device that generates source code in accordance with a grammar of a high-level language by further dividing by a branch destination label.
本発明の請求項1記載のソフトウェア変換装置は、アセンブリ言語ソースコードを高級言語ソースコードに逆変換するソフトウェア変換装置において、アセンブリ言語ソースコードの中から先頭のラベルと分岐ラベルを検出するラベル検出手段と、前記ラベル検出手段の検出結果に応じて、検出したラベルに関連する高級言語ソースコード内の命令を関数の形式に変換し、該高級言語ソースコードを分岐ラベルで分割する第1の関数分割手段とを備えることを特徴とする。
The software conversion device according to
本発明の請求項2記載のソフトウェア変換装置は、請求項1記載のソフトウェア変換装置であって、前記第1の関数分割手段により関数の形式に変換された高級言語ソースコードの中から、別の関数単位内のラベルへ分岐する分岐命令の分岐先のラベルを検出する分岐先ラベル検出手段と、前記分岐先ラベル検出手段の検出結果に応じて、検出した前記分岐先のラベルに関連する高級言語ソースコード内の命令を関数の形式に変換し、該高級言語ソースコードを前記分岐先のラベルで分割するとともに、前記別の関数単位内のラベルへ分岐する分岐命令を関数へ分岐するための関数コールに置き換える第2の関数分割手段とを備えることを特徴とする。
A software conversion device according to claim 2 of the present invention is the software conversion device according to
本発明の請求項3記載のソフトウェア変換装置は、請求項1もしくは2のいずれかに記載のソフトウェア変換装置であって、前記第1の関数分割手段は、高級言語ソースコードの先頭のラベルを関数化するとともに該高級言語ソースコードの終端に関数終了を表す記号を挿入して該高級言語ソースコードの先頭のラベル以下の命令を関数内の命令とし、前記分岐ラベルを関数化するとともにその直前に関数の分割を表す記号を挿入して該高級言語ソースコードを分割することを特徴とする。
The software conversion device according to claim 3 of the present invention is the software conversion device according to any one of
本発明の請求項4記載のソフトウェア変換装置は、請求項2乃至3のいずれかに記載のソフトウェア変換装置であって、前記第2の関数分割手段は、前記分岐先のラベルを関数化するとともにその直前に関数の分割を表す記号を挿入して該高級言語ソースコードを分割することを特徴とする。
The software conversion device according to
本発明の請求項5記載のソフトウェア変換装置は、請求項3もしくは4のいずれかに記載のソフトウェア変換装置であって、前記第1の関数分割手段は、前記関数の分割を表す記号に加えて、その直前に関数化された分岐ラベルへ分岐する関数コールを挿入することを特徴とする。
The software conversion device according to claim 5 of the present invention is the software conversion device according to
本発明の請求項6記載のソフトウェア変換装置は、請求項4もしくは5のいずれかに記載のソフトウェア変換装置であって、前記第2の関数分割手段は、前記関数の分割を表す記号に加えて、その直前に関数化された分岐先のラベルへ分岐する関数コールを挿入することを特徴とする。 A software conversion device according to a sixth aspect of the present invention is the software conversion device according to any one of the fourth or fifth aspects, wherein the second function dividing means includes a symbol representing the division of the function. , A function call for branching to a branch destination label that has been functionalized immediately before is inserted.
本発明の請求項7記載のソフトウェア変換装置は、請求項6記載のソフトウェア変換装置であって、前記分岐ラベルや前記分岐先のラベルの直前に配置されている命令が関数終了命令であることを検出する関数終了命令検出手段と、前記関数終了命令検出手段による関数終了命令の検出に応じて、前記分岐ラベルや前記分岐先のラベルの直前に関数コールが挿入されないようにする不要命令省略手段とを備えることを特徴とする。 The software conversion device according to claim 7 of the present invention is the software conversion device according to claim 6, wherein the instruction arranged immediately before the branch label or the branch destination label is a function end instruction. A function end instruction detecting unit for detecting, and an unnecessary instruction omitting unit for preventing a function call from being inserted immediately before the branch label or the branch destination label in response to detection of the function end instruction by the function end instruction detecting unit; It is characterized by providing.
本発明の請求項8記載のソフトウェア変換装置は、請求項3乃至7のいずれかに記載のソフトウェア変換装置であって、前記分岐ラベルや前記分岐先のラベルの直前に配置されている命令がテーブルデータ命令であることを検出するテーブルデータ命令検出手段と、前記テーブルデータ命令検出手段によるテーブルデータ命令の検出に応じて、前記関数の分割を表す記号の挿入先を前記分岐ラベルや前記分岐先のラベルの直前に代えてテーブルデータ命令の直前にし、テーブルデータ命令が関数単位外に配置されるようにするテーブルデータ配置変更手段とを備えることを特徴とする。 The software conversion device according to claim 8 of the present invention is the software conversion device according to any one of claims 3 to 7, wherein an instruction arranged immediately before the branch label or the branch destination label is a table. A table data instruction detecting means for detecting that the instruction is a data instruction, and in response to detection of the table data instruction by the table data instruction detecting means, an insertion destination of the symbol representing the division of the function is set to the branch label or the branch destination Table data arrangement changing means is provided immediately before the table data instruction instead of immediately before the label so that the table data instruction is arranged outside the function unit.
本発明の請求項9記載のソフトウェア変換装置は、請求項1乃至8のいずれかに記載のソフトウェア変換装置であって、前記分岐ラベルがcall文の引数であることを特徴とする。
A software conversion device according to claim 9 of the present invention is the software conversion device according to any one of
本発明の請求項10記載のソフトウェア変換装置は、請求項2乃至9のいずれかに記載のソフトウェア変換装置であって、前記別の関数単位内のラベルへ分岐する分岐命令がgoto文であることを特徴とする。 The software conversion device according to claim 10 of the present invention is the software conversion device according to any one of claims 2 to 9, wherein the branch instruction for branching to the label in the other function unit is a goto statement. It is characterized by.
本発明の請求項11記載のソフトウェア変換装置は、請求項7乃至10のいずれかに記載のソフトウェア変換装置であって、前記関数終了命令がreturn命令またはgoto命令であることを特徴とする。 A software conversion device according to an eleventh aspect of the present invention is the software conversion device according to any one of the seventh to tenth aspects, wherein the function end instruction is a return instruction or a goto instruction.
本発明によれば、アセンブリ言語を高級言語に逆コンパイル(逆変換)するに際し、アセンブリ言語にcall文などのラベルに分岐する命令が記述されている場合であっても、その分岐命令の分岐先のラベル(分岐ラベル)を関数化し、高級言語のソースコード内の命令をすべて関数に属するものにし、関数化した分岐ラベルでこの関数を分割するので、高級言語の文法に則ったソースコードを生成することができる。 According to the present invention, when an assembly language is decompiled into a high-level language (inverse conversion), even when an instruction that branches to a label such as a call statement is described in the assembly language, the branch destination of the branch instruction The label (branch label) is converted into a function, all the instructions in the high-level language source code belong to the function, and this function is divided by the functionalized branch label, so the source code conforming to the high-level language grammar is generated. can do.
また、このようにすべての命令を関数に属するものにし、かつ関数化した分岐ラベルでその関数を分割することにより、goto文などのラベルに分岐する命令の分岐先のラベルがその分岐命令の関数単位外のラベルとなった場合であっても、この分岐命令を関数コールに置き換え、その分岐先のラベルを関数化し、この関数化した分岐先のラベルでさらに高級言語ソースコードを分割するので、高級言語の文法に則ったソースコードを生成することができる。 In addition, when all instructions belong to a function in this way and the function is divided by a functionalized branch label, the branch destination label of an instruction that branches to a label such as a goto statement is the function of the branch instruction. Even when the label is out of the unit, this branch instruction is replaced with a function call, the label of the branch destination is converted into a function, and the high-level language source code is further divided by the functionalized branch destination label. Source code that conforms to the grammar of high-level languages can be generated.
また、関数化した分岐ラベルや分岐先のラベルで関数を分割するに際し、関数を分割する記号の直前にその直後の関数化したラベルへ分岐する関数コールを挿入するので、分岐することなしに高級言語ソースコードの流れの順に処理が行われる場合であっても、論理の整合性を取ることができる。 In addition, when a function is divided by a functionalized branch label or a branch destination label, a function call that branches to the functionalized label immediately after that is inserted immediately before the symbol that divides the function. Even if processing is performed in the order of the flow of the language source code, logic consistency can be achieved.
一方、関数化する分岐ラベルや分岐先のラベルの直前の命令が関数終了命令である場合には関数コールを挿入しないので、ソースコードのコードサイズを圧縮することができる。 On the other hand, since the function call is not inserted when the instruction immediately before the branch label to be functionalized or the branch destination label is a function end instruction, the code size of the source code can be compressed.
また、関数化する分岐ラベルや分岐先のラベルの直前の命令がテーブルデータ命令である場合には、そのテーブルデータ命令の直前で関数が終了するようにするので、高級言語の文法に則ったソースコードを生成することができる。 If the instruction immediately before the branch label to be converted or the label at the branch destination is a table data instruction, the function ends immediately before the table data instruction. Code can be generated.
以下、本発明の実施の形態について、図面を交えて説明する。
図1は、本実施の形態におけるソフトウェア変換装置の構成を示す概略図である。
図1において、ラベル検出手段102はアセンブリ言語ソースコード101の中から先頭のラベルと分岐ラベル(例えば、call文の引数となっているラベル)を検出し、これらのラベルをデータベースに登録する。逆コンパイラ103はアセンブリ言語ソースコード101を逆コンパイルして高級言語ソースコード104に変換する。
Hereinafter, embodiments of the present invention will be described with reference to the drawings.
FIG. 1 is a schematic diagram showing a configuration of a software conversion apparatus according to the present embodiment.
In FIG. 1, a
第1の関数分割手段105は、データベースに登録されたラベルに関連する高級言語ソースコード104内の命令を関数の形式に変換し、高級言語ソースコード104を分岐ラベルで分割する。ここでは、第1の関数分割手段105は、高級言語ソースコード104の先頭のラベルを関数化するとともに高級言語ソースコード104の終端に関数終了を表す記号“}”を挿入して高級言語ソースコード104の先頭のラベル以下の命令を関数内の命令とし、分岐ラベルを関数化するとともにその直前に関数の分割を表す記号(関数終了を表す記号)“}”を挿入して高級言語ソースコード104を分割する。
The first
さらに、第1の関数分割手段105は、分岐ラベルの直前にその分岐ラベルへ分岐する関数コールを挿入する。
また、第1の関数分割手段105内の関数終了命令検出手段106は、分岐ラベルの直前に配置されているreturn命令やgoto命令などの関数終了命令を検出する。
Further, the first function dividing means 105 inserts a function call that branches to the branch label immediately before the branch label.
The function end instruction detection means 106 in the first function dividing means 105 detects a function end instruction such as a return instruction or a goto instruction arranged immediately before the branch label.
不要命令省略手段107は、関数終了命令検出手段106による関数終了命令の検出に応じて、関数コールが挿入されないようにする。
テーブルデータ命令検出手段108は、分岐ラベルの直前に配置されているテーブルデータ命令を検出する。
The unnecessary
The table data
テーブルデータ配置変更手段109は、テーブルデータ命令検出手段108によりテーブルデータ命令が検出された場合、関数終了を表す記号“}”の挿入先を分岐ラベルの直前からテーブルデータ命令の直前に変更し、テーブルデータ命令が後述する関数単位外に配置されるようにする。
When the table data instruction is detected by the table data
また、分岐先ラベル検出手段110は、第1の関数分割手段105により関数の形式に変換された高級言語ソースコードの中から、別の関数単位内のラベルへ分岐する分岐命令の分岐先のラベルを検出する。
Further, the branch destination
第2の関数分割手段111は、分岐先ラベル検出手段110により検出された分岐先のラベルに関連する高級言語ソースコード内の命令を関数の形式に変換し、この高級言語ソースコードを分岐先のラベルで分割するとともに、分岐命令を関数へ分岐するための関数コールに置き換える。ここでは、第2の関数分割手段111は、分岐先のラベルを関数化するとともにその直前に関数の分割を表す記号(関数終了を表す記号)“}”を挿入して該高級言語ソースコードを分割する。
The second
さらに、第2の関数分割手段111は、分岐先のラベルの直前にこの分岐先のラベルへ分岐する関数コールを挿入する。
また、第2の関数分割手段111内の関数終了命令検出手段106は、分岐先のラベルの直前に配置されているreturn命令やgoto命令などの関数終了命令を検出する。
Further, the second
In addition, the function end instruction detection means 106 in the second function dividing means 111 detects a function end instruction such as a return instruction or a goto instruction arranged immediately before the branch destination label.
不要命令省略手段107は、関数終了命令検出手段106による関数終了命令の検出に応じて、関数コールが挿入されないようにする。
テーブルデータ命令検出手段108は、分岐先のラベルの直前に配置されているテーブルデータ命令を検出する。
The unnecessary
The table data
テーブルデータ配置変更手段109は、テーブルデータ命令検出手段108によりテーブルデータ命令が検出された場合、関数終了を表す記号“}”の挿入先を分岐先のラベルの直前からテーブルデータ命令の直前に変更し、テーブルデータ命令が後述する関数単位外に配置されるようにする。
When the table data instruction is detected by the table data
本実施の形態におけるソフトウェア変換装置は、以上のような構成からなることにより目的とする高級言語ソースコード112を得ることができる。
以下、該ソフトウェア変換装置の動作について、まずラベル検出手段と第1の関数分割手段の動作から詳説する。
The software conversion apparatus according to the present embodiment can obtain the target high-level
Hereinafter, the operation of the software conversion apparatus will be described in detail from the operations of the label detecting means and the first function dividing means.
図2(a)は、call文の検索による関数分割を説明するためのフローチャート図である。まず、ステップS201において、アセンブリ言語ソースコードから先頭のラベルと分岐ラベルであるcall文の引数になっているラベルをすべて検索する。次に、ステップS202において、ステップS201で検索したすべてのラベルを関数テーブル(データベース)204に登録する。最後に、ステップS203において、関数テーブル204に登録されたラベルに基づいて、アセンブリ言語ソースコードを逆コンパイルして得た高級言語ソースコード内の先頭のラベルとcall文の引数になっているラベルに関連する命令を関数の形式に変換する。 FIG. 2A is a flowchart for explaining the function division by searching for a call sentence. First, in step S201, all labels that are arguments of the call statement as the head label and branch label are searched from the assembly language source code. Next, in step S202, all the labels searched in step S201 are registered in the function table (database) 204. Finally, in step S203, based on the label registered in the function table 204, the first label in the high-level language source code obtained by decompiling the assembly language source code and the label that is the argument of the call statement are used. Convert related instructions to function form.
この変換処理は、以下のような処理により行う。例えばラベルLabel1がcall文の引数になっており、関数テーブルに登録されている場合、アセンブリ言語ソースコードを逆コンパイルして得た高級言語ソースコードには、
Label1:
と記述されている。そこで、まずこのラベルの直前にgoto文を記述して、
goto Label1;
Label1:
とした後、この“Label1:”の直前に関数終了を表す記号(関数の分割を表す記号)“}”を挿入するとともに、このラベルを関数化して、
Label1( );return;
}
void Label1(void){
と置き換える。なお、ラベルLabel1が関数化されるので、“goto Label1;”は関数へ分岐するための関数コール“Label1( );return;”に置換される。
This conversion process is performed by the following process. For example, when the label Label1 is an argument of a call statement and is registered in the function table, the high-level language source code obtained by decompiling the assembly language source code includes:
Label1:
It is described. So, first, put a goto statement just before this label,
goto Label1;
Label1:
Then, a symbol indicating the end of the function (a symbol indicating the division of the function) “}” is inserted immediately before “Label1:”, and this label is converted into a function.
Label1 (); return;
}
void Label1 (void) {
Replace with Since the label Label1 is functionalized, “goto Label1;” is replaced with a function call “Label1 (); return;” for branching to the function.
また、一番先頭のラベルも関数テーブル204に登録しておき、この一番先頭のラベルを関数化するとともに、ファイルの終端に関数終了を表す記号“}”を挿入することにより、すべての命令が関数に属するようになる。 The first label is also registered in the function table 204, and the first label is converted into a function, and a symbol “}” indicating the end of the function is inserted at the end of the file, so that all instructions Belongs to a function.
例えば、先頭のラベルがラベルLabel1の場合、アセンブリ言語ソースコードを逆コンパイルして得た高級言語ソースコードには、
Label1:
と記述されており、このラベルを関数化して
void Label1(void){
と置き換えるとともに、ファイルの終端に関数終了を表す記号“}”を挿入する。
For example, when the first label is a label Label1, high-level language source code obtained by decompiling assembly language source code includes:
Label1:
And this label is converted into a function, void Label1 (void) {
And the symbol “}” indicating the end of the function is inserted at the end of the file.
以上のように、ソースコード内の一番先頭のラベルを関数化してファイルの終端に関数終了を表す記号“}”を挿入するとともに、call文の引数となっているラベル(分岐ラベル)の直前に関数終了を表す記号“}”を挿入してこのラベルを関数化することにより、すべての命令が関数に属するようになり、かつcall文の引数となっているラベルで高級言語ソースコードが分割される。 As described above, the first label in the source code is converted into a function, and the symbol “}” indicating the end of the function is inserted at the end of the file, and immediately before the label (branch label) that is the argument of the call statement. By inserting the symbol “}” indicating the end of the function into a function and making this label a function, all instructions belong to the function, and the high-level language source code is divided by the label that is the argument of the call statement Is done.
図3は本実施の形態におけるcall文の検索による関数分割の一実施例である。
まず、アセンブリ言語ソースコード301より、先頭のラベルであるラベルLabel1とcall文の引数であるラベルLabel3を関数テーブルに登録する。
FIG. 3 shows an example of function division by retrieval of a call sentence in the present embodiment.
First, from the assembly
次に、この関数テーブルに基づいて、アセンブリ言語ソースコード301を逆コンパイルして得たソースコード302を関数分割する。すなわち、ラベルLabel3の直前にgoto文“goto Label3;”を記述してソースコード303とした後、ラベルLabel1を関数化してファイルの終端に関数終了を表す記号“}”を挿入するとともに、ラベルLabel3の直前に関数終了を表す記号“}”を挿入してこのラベルを関数化することにより、ラベルLabel3で関数分割された高級言語ソースコード304を得る。
Next, based on this function table, the
なお、ソースコード303のgoto文“goto Label4”はラベルLabel4へ分岐するgoto命令であるのでそのまま残され、goto文“goto Label3”は関数“void Label3(void){”へ分岐するgoto命令であるので関数コール“Label3( );return;”に置換される。
Note that the goto statement “goto Label4” in the
以上のようにcall文の検索による関数分割を行うことで、全命令が関数に属するようになる。しかしながら、この処理のみでは、関数外のラベルへ分岐するgoto文(分岐命令)が残る場合がある。 As described above, by performing function division by searching for a call statement, all instructions belong to functions. However, only this processing may leave a goto statement (branch instruction) that branches to a label outside the function.
例えば、図3に示す例では、ソースコード304に
goto Label4:
が残り、関数“void Label1(void){”から関数“void Label3(void){”へ分岐する構成になり、高級言語の文法に則さずに、コンパイル時に不具合が発生する。
For example, in the example shown in FIG. 3, goto Label4:
Therefore, the function “void Label1 (void) {” branches to the function “void Label3 (void) {”, and a problem occurs at the time of compiling without conforming to the grammar of the high-level language.
そこで、本実施の形態ではこの問題を回避するために、分岐先ラベル検出手段と第2の関数分割手段により、関数外のラベルに分岐するgoto文の検索による関数分割を行う。 Therefore, in this embodiment, in order to avoid this problem, function division is performed by searching for a goto statement that branches to a label outside the function by the branch destination label detection means and the second function division means.
図2(b)は、goto文の検索による関数分割を説明するためのフローチャート図である。
まず、処理開始時におけるステップS211では、関数テーブル214はcall文の検索による関数分割の処理時に得た関数テーブル204と同じものである。
FIG. 2B is a flowchart for explaining function division by searching for a goto sentence.
First, in step S211 at the start of processing, the function table 214 is the same as the function table 204 obtained at the time of function division processing by retrieval of a call statement.
次に、ステップS212において、call文の検索による関数分割後のソースコードの先頭から順に関数化されていないラベル(分岐先のラベル)を検索する。関数化されていないラベルが見つかった場合、そのラベルに分岐するgoto文(分岐命令)をすべて検索する。 Next, in step S212, labels that are not functionalized (branch destination labels) are searched in order from the beginning of the source code after function division by the search of the call statement. If a non-functionalized label is found, all goto statements (branch instructions) that branch to that label are searched.
続いて、ステップS213において、ステップS212にて検索されたラベルで高級言語ソースコードを関数分割するか否かの判定を行う。
この判定処理は以下のようにして行う。すなわち、関数テーブル214に基づいて、関数化されたラベルから次の関数化されたラベルの直前の行までを1つの関数単位とし、関数化されていないラベルとそのラベルに分岐するgoto文が同じ関数単位に属しているか否かを判定する。関数化されていないラベルとそのラベルに分岐するすべてのgoto文が同じ関数単位に属する場合は、そのラベルとgoto文をそのまま残し、ステップS212へ移行して次の関数化されていないラベルの検索に移る。一方、関数化されていないラベルとそのラベルに分岐するgoto文が別の関数単位に属する場合、ステップS214へ移行し、そのラベルに関連する高級言語ソースコード内の命令を関数の形式に変換する。本実施の形態では、このようにして別の関数単位内のラベルへ分岐する分岐命令の分岐先のラベルの検出を行う。
Subsequently, in step S213, it is determined whether or not the high-level language source code is function-divided using the label searched in step S212.
This determination process is performed as follows. That is, based on the function table 214, one functional unit is from the functionalized label to the line immediately before the next functionalized label, and the non-functionalized label and the goto statement branching to the label are the same. Determine whether it belongs to a function unit. If the unfunctionalized label and all the goto statements that branch to the label belong to the same function unit, the label and the goto statement are left as they are, and the process proceeds to step S212 to search for the next unfunctionalized label. Move on. On the other hand, if the label that is not functionalized and the goto statement that branches to the label belong to another function unit, the process proceeds to step S214, and the instruction in the high-level language source code related to the label is converted into the function format. . In this embodiment, the branch destination label of a branch instruction that branches to a label in another function unit is detected in this way.
また、ステップS214における変換処理は、以下のような処理により行う。例えばラベルLabel1が関数化されていないラベルであって、このラベルLabel1に分岐するgoto文が別の関数単位に属する場合、call文の検索による関数分割後のソースコードには、
Label1:
と記述されている。そこで、まずこのラベルの直前にgoto文を記述して、
goto Label1;
Label1:
とした後、この“Label1:”の直前に関数終了を表す記号(関数の分割を表す記号)“}”を挿入するとともに、このラベルを関数化し、
Label1( );return;
}
void Label1(void){
と置き換え、ラベルLabel1で高級言語ソースコードを関数分割する。
The conversion process in step S214 is performed by the following process. For example, when the label Label1 is a non-functionalized label and the goto statement that branches to the label Label1 belongs to another function unit, the source code after the function division by the search of the call statement includes
Label1:
It is described. So, first, put a goto statement just before this label,
goto Label1;
Label1:
After that, a symbol indicating the end of the function (a symbol indicating the division of the function) “}” is inserted immediately before “Label1:”, and this label is converted into a function.
Label1 (); return;
}
void Label1 (void) {
And the high-level language source code is function-divided by the label Label1.
また、このようにラベルLabel1が関数化されるので、ラベルLabel1へ分岐する命令である“goto Label1;”は関数へ分岐するための関数コール“Label1( );return;”に置換される。 In addition, since the label Label1 is functionalized in this way, “goto Label1;”, which is an instruction for branching to the label Label1, is replaced with a function call “Label1 (); return;” for branching to the function.
このように新たにラベルを関数化した後、ステップS211へ戻ってそのラベルを関数テーブル214に追加登録し(関数一覧作成)、再度、先頭から関数化されていないラベルの検索を開始する。 After the label is newly converted into a function in this way, the process returns to step S211 to additionally register the label in the function table 214 (function list creation), and the search for the label that has not been converted to a function is started again from the beginning.
以上の処理を関数化されていないラベルとそのラベルに分岐するgoto文がすべて同じ関数単位に属するようになるまで繰り返すことにより、高級言語の文法に則したソースコードを得ることができる。 By repeating the above processing until a label that is not functionalized and a goto statement that branches to the label all belong to the same function unit, a source code that conforms to the grammar of a high-level language can be obtained.
このgoto文の検索による関数分割を図4に示す一実施例に則して説明する。図4において、ソースコード304は図3に示すソースコード304と同じものである。
まず、ソースコード304の先頭から順に関数化されていないラベルを検索し、ラベルLabel2を検出する。次に、このラベルLabel2に分岐するgoto文を検索する。図4に示す例ではこのラベルに関数単位の外から分岐してくるgoto文(別の関数単位内のラベルに分岐する分岐命令)は存在しないので、ラベルLabel2では高級言語ソースコードの関数分割を行わない。
The function division by the search of the goto sentence will be described in accordance with an embodiment shown in FIG. In FIG. 4, the
First, labels that are not functionalized are searched in order from the top of the
続いて、再度ソースコード304の先頭から順に関数化されていないラベルを検索し、ラベルLabel4を検出する。次に、このラベルLabel4に分岐するgoto文を検索する。図4に示す例では、ラベルLabel4は関数“void Label3(void){”の関数単位に属し、ラベルLabel4へ分岐するgoto文はその関数単位とは別の関数“void Label1(void){”に属するので、ラベルLabel4で高級言語ソースコードの関数分割を行う。
Subsequently, the labels that are not functionalized are sequentially searched again from the top of the
すなわち、ソースコード304内のラベルLabel4の直前にgoto文“goto Label4;”を記述してソースコード305とした後、ラベルLabel4の直前に関数終了を表す記号“}”を挿入してこのラベルを関数化することにより、ラベルLabel4で分割された高級言語ソースコード306を得る。
That is, after describing the goto statement “goto Label4;” immediately before the label Label4 in the
続いて、ソースコード306の先頭から順に関数化されていないラベルの検索を行う。図4に示す例では、関数化されていないラベルはこれ以上存在しないので、ここで作業を終了する。
Subsequently, a search for labels that are not functionalized is performed in order from the top of the
以上のように、call文の検索による関数分割後の高級言語ソースコードから、別の関数単位内のラベルに分岐するgoto文(分岐命令)の分岐先のラベルを検出し、この分岐先のラベルの直前に関数終了を表す記号“}”挿入するとともにこのラベルを関数化し、かつそのgoto文を関数コールに置き換えることにより、高級言語の関数の文法に則したソースコードを得ることができる。 As described above, the branch destination label of a goto statement (branch instruction) that branches to a label in another function unit is detected from the high-level language source code after function division by the search of the call statement, and this branch destination label is detected. By inserting the symbol “}” indicating the end of the function immediately before “”, converting this label into a function, and replacing the goto statement with a function call, a source code conforming to the grammar of the function of the high-level language can be obtained.
なお、別の関数単位内のラベルに分岐するgoto文(分岐命令)の分岐先のラベルを検出するたびに関数分割を行うのではなく、すべての該当ラベルを関数テーブルに追加登録した後、関数分割するようにしてもよい。 In addition, function division is not performed every time a branch destination label of a goto statement (branch instruction) that branches to a label in another function unit is detected, but after all the corresponding labels are additionally registered in the function table, You may make it divide | segment.
このように、call文の検索による関数分割およびgoto文の検索による関数分割によって、全命令が関数に属し、かつ高級言語の関数の文法に則したソースコードを得ることができる。 As described above, the function division by the search of the call sentence and the function division by the search of the goto sentence can obtain a source code in which all instructions belong to the function and comply with the grammar of the function of the high-level language.
しかしながら、これらの処理により本来不要な命令を追加してしまう場合がある。
例えば、図3、4に示す例では、ラベルLabel3(分岐ラベル)とラベルLabel4(分岐先のラベル)を関数化する際に、そのラベルの直前にgoto命令(関数コール)が挿入される。
However, these processes may add unnecessary instructions.
For example, in the example shown in FIGS. 3 and 4, when a label Label3 (branch label) and a label Label4 (branch destination label) are functionalized, a goto instruction (function call) is inserted immediately before the label.
この命令は、分岐することなしにソースコードの流れの順にLabel3、Label4の処理が行われる場合の対策のために配置しているものである。しかし、図3、4に示す例では、これらの命令はreturn命令の直後に配置されることになるため実行されることは無い。 This instruction is arranged as a countermeasure when the processing of Label3 and Label4 is performed in the order of the source code flow without branching. However, in the example shown in FIGS. 3 and 4, since these instructions are arranged immediately after the return instruction, they are not executed.
そこで、本実施の形態では、関数終了命令検出手段と不要命令削除手段により、関数分割する際に、分割する直前の命令(分岐ラベルや分岐先のラベルの直前の命令)がreturn命令やgoto命令のような関数終了命令の場合には、分岐ラベルや分岐先のラベルの直前にgoto命令を挿入しないようにする。 Therefore, in the present embodiment, when the function is divided by the function end instruction detection means and the unnecessary instruction deletion means, the instruction immediately before the division (the instruction immediately before the branch label or branch destination label) is the return instruction or the goto instruction. In the case of such a function end instruction, a goto instruction is not inserted immediately before a branch label or a branch destination label.
図5(a)は、分割する直前の命令がreturn命令もしくはgoto命令の場合の関数分割を説明するためのフローチャート図である。
まず、ステップS501おいて、call文またはgoto文の検索による関数分割を行うに際し、分割する直前の命令がreturn命令もしくはgoto命令であるか否かを判定する。この判定処理は、関数テーブルに基づいて行ってもよいし、関数テーブルにラベルを登録する際に行ってもよい。
FIG. 5A is a flowchart for explaining function division when the instruction immediately before division is a return instruction or a goto instruction.
First, in step S501, when function division is performed by searching for a call statement or a goto statement, it is determined whether the instruction immediately before the division is a return instruction or a goto instruction. This determination process may be performed based on the function table or may be performed when a label is registered in the function table.
分割する直前の命令がreturn命令もしくはgoto命令であった場合には、ステップS502において、新たに挿入したgoto命令を省略して、あるいはgoto命令を挿入することなく関数分割の処理を施し、それ以外の命令の場合にはステップS503において、goto命令をそのまま配置して関数分割の処理を施し論理の整合性を取る。 If the instruction immediately before the division is a return instruction or a goto instruction, in step S502, the newly inserted goto instruction is omitted or a function division process is performed without inserting a goto instruction. In step S503, the goto instruction is arranged as it is, and function division processing is performed to ensure logic consistency.
例えば、図3に示すソースコード302に対してcall文の検索による関数分割およびgoto文の検索による関数分割を行うに際し、上述したgoto命令を省略する処理を施すことにより、図6に示すソースコード601を得ることができる。このソースコード601は、図4に示すソースコード306と比較して、関数“void Label3(void){”と“void Label4(void){”の前に配置されている関数コール“Label3();return;”と“Label4();return;”が省略されている。
For example, when performing function division by retrieval of a call statement and function division by retrieval of a goto statement with respect to the
さらに、ソースコードがテーブルデータ命令を含む場合、上述したcall文またはgoto文の検索による関数分割を行うとテーブルデータ命令が関数に含まれてしまい、高級言語の文法に則さないソースコードに変換されるという問題がある。ここで、テーブルデータ命令とは、プログラム上の例えば関数の中で使用されるデータ列のみをテーブル化したものを指す。 Furthermore, when the source code includes a table data instruction, if the function is divided by searching for the above-described call statement or goto statement, the table data instruction is included in the function and converted into source code that does not conform to the grammar of the high-level language. There is a problem of being. Here, the table data instruction refers to a table in which only a data string used in, for example, a function on a program is tabulated.
例えば、図7(a)に示すソースコード701に対して、上述したcall文の検索による関数分割およびgoto文の検索による関数分割を行うと、ラベルLabel4の関数化にともない、テーブルデータTable1(dc命令によりデータを2バイト分確保している)が関数内部に配置されたソースコード702に変換される。
For example, when the function division by the search of the call statement and the function division by the search of the goto statement are performed on the
そこで、本実施の形態では、テーブルデータ命令検索手段とテーブルデータ配置変更手段により、関数分割する際に、分割する直前の命令(分岐ラベルや分岐先のラベルの直前の命令)がテーブルデータ命令の場合には、関数の分割を表す記号(関数の終了を表す記号)“}”の挿入先を分岐ラベルや分岐先のラベルの直前からテーブルデータ命令の直前に変更し、テーブルデータ命令の配置を関数単位外へ変更する。 Therefore, in this embodiment, when the function is divided by the table data instruction search means and the table data arrangement change means, the instruction immediately before the division (the instruction immediately before the branch label or the branch destination label) is the table data instruction. In this case, the insertion destination of the symbol indicating the division of the function (the symbol indicating the end of the function) “}” is changed from immediately before the branch label or branch label to immediately before the table data instruction, and the arrangement of the table data instruction is changed. Change outside function unit.
図5(b)は、分割する直前の命令がテーブルデータ命令の場合の関数分割を説明するためのフローチャート図である。
まず、ステップS511において、call文またはgoto文の検索による関数分割を行うに際し、分割する直前の命令がテーブルデータ命令であるか否かを判定する。この判定処理は、関数テーブルに基づいて行ってもよいし、関数テーブルにラベルを登録する際に行ってもよい。
FIG. 5B is a flowchart for explaining function division when the instruction immediately before division is a table data instruction.
First, in step S511, when performing function division by searching for a call statement or a goto statement, it is determined whether or not the instruction immediately before the division is a table data instruction. This determination process may be performed based on the function table or may be performed when a label is registered in the function table.
分割する直前の命令がテーブルデータ命令であった場合、ステップS512において、関数終了を表す記号“}”の挿入先を分岐ラベルや分岐先のラベルの直前からテーブルデータ命令の直前に変更して関数分割を行う。それ以外の命令の場合は、ステップS513において、そのまま関数分割を行うことにより論理の整合性を取ることが可能となる。 If the instruction immediately before the division is a table data instruction, in step S512, the insertion destination of the symbol “}” indicating the end of the function is changed from immediately before the branch label or branch destination label to immediately before the table data instruction. Split. In the case of other instructions, in step S513, it is possible to obtain logical consistency by performing function division as it is.
例えば、図7(b)に示すソースコード701に対してcall文の検索による関数分割およびgoto文の検索による関数分割を行うに際し、上述したテーブルデータ命令の配置を変更する処理を施すことにより、テーブルデータTable1を関数内部に含まないソースコード703を得ることができる。すなわち、ソースコード701においてラベルLabel4を関数化する際に、関数終了を表す記号“}”の挿入先をラベルLabel4の直前からテーブルデータ命令を示すラベル“Table1:”の直前に変更することにより、関数内部にテーブルデータ命令を含まないソースコード703を得ることが可能となる。
For example, when performing function division by calling a call statement and function division by searching a goto statement on the
なお、分割する直前に配置されているテーブルデータ命令を検出し、さらにその検出したテーブルデータ命令の直前に配置されているreturn命令もしくはgoto命令を検出することで、上述のgoto命令を省略する処理とテーブルデータの配置を更する処理を合わせて行うようにしてもよい。 Processing for omitting the above-mentioned goto instruction by detecting a table data instruction arranged immediately before the division and detecting a return instruction or a goto instruction arranged immediately before the detected table data instruction. And processing for changing the arrangement of the table data may be performed together.
本発明にかかるソフトウェア変換装置は、アセンブリ言語ソースコードから高級言語の文法に則ったソースコードを生成することができるので、あるマイコンのCPUで実行可能なアセンブリ言語ソースコードを、別のCPUを搭載したマイコンで実行可能な高級言語のソースコードに逆変換するソフトウェア変換装置などに適用できる。 Since the software conversion apparatus according to the present invention can generate a source code conforming to a high-level language grammar from an assembly language source code, an assembly language source code that can be executed by a CPU of a certain microcomputer is mounted on another CPU. It can be applied to a software conversion device that performs reverse conversion into high-level language source code that can be executed by a microcomputer.
101 アセンブリ言語ソースコード
102 ラベル検出手段
103 逆コンパイラ
104 高級言語ソースコード
105 第1の関数分割手段
106 関数終了命令検出手段
107 不要命令省略手段
108 テーブルデータ命令検出手段
109 テーブルデータ配置変更手段
110 分岐先ラベル検出手段
111 第2の関数分割手段
112 目的とする高級言語ソースコード
101 assembly
Claims (11)
アセンブリ言語ソースコードの中から先頭のラベルと分岐ラベルを検出するラベル検出手段と、
前記ラベル検出手段の検出結果に応じて、検出したラベルに関連する高級言語ソースコード内の命令を関数の形式に変換し、該高級言語ソースコードを分岐ラベルで分割する第1の関数分割手段と
を備えることを特徴とするソフトウェア変換装置。 In a software conversion device that converts assembly language source code back to high-level language source code,
Label detection means for detecting the leading label and branch label from assembly language source code;
First function dividing means for converting an instruction in the high-level language source code related to the detected label into a function format according to the detection result of the label detection means, and dividing the high-level language source code by a branch label; A software conversion device comprising:
前記第1の関数分割手段により関数の形式に変換された高級言語ソースコードの中から、別の関数単位内のラベルへ分岐する分岐命令の分岐先のラベルを検出する分岐先ラベル検出手段と、
前記分岐先ラベル検出手段の検出結果に応じて、検出した前記分岐先のラベルに関連する高級言語ソースコード内の命令を関数の形式に変換し、該高級言語ソースコードを前記分岐先のラベルで分割するとともに、前記別の関数単位内のラベルへ分岐する分岐命令を関数へ分岐するための関数コールに置き換える第2の関数分割手段と
を備えることを特徴とするソフトウェア変換装置。 The software conversion device according to claim 1,
Branch destination label detecting means for detecting a branch destination label of a branch instruction that branches to a label in another function unit from the high-level language source code converted into the function format by the first function dividing means;
In accordance with the detection result of the branch destination label detection means, the instruction in the high-level language source code related to the detected branch destination label is converted into a function format, and the high-level language source code is converted into the branch destination label. A software conversion device comprising: a second function dividing unit that divides and replaces a branch instruction that branches to a label in another function unit with a function call for branching to a function.
前記分岐ラベルや前記分岐先のラベルの直前に配置されている命令が関数終了命令であることを検出する関数終了命令検出手段と、
前記関数終了命令検出手段による関数終了命令の検出に応じて、前記分岐ラベルや前記分岐先のラベルの直前に関数コールが挿入されないようにする不要命令省略手段と
を備えることを特徴とするソフトウェア変換装置。 The software conversion device according to claim 6,
Function end instruction detection means for detecting that an instruction arranged immediately before the branch label or the branch destination label is a function end instruction;
Software conversion comprising: unnecessary instruction omission means for preventing a function call from being inserted immediately before the branch label or the branch destination label in response to detection of a function end instruction by the function end instruction detection means apparatus.
前記分岐ラベルや前記分岐先のラベルの直前に配置されている命令がテーブルデータ命令であることを検出するテーブルデータ命令検出手段と、
前記テーブルデータ命令検出手段によるテーブルデータ命令の検出に応じて、前記関数の分割を表す記号の挿入先を前記分岐ラベルや前記分岐先のラベルの直前に代えてテーブルデータ命令の直前にし、テーブルデータ命令が関数単位外に配置されるようにするテーブルデータ配置変更手段と
を備えることを特徴とするソフトウェア変換装置。 A software conversion device according to any one of claims 3 to 7,
Table data instruction detection means for detecting that an instruction arranged immediately before the branch label or the branch destination label is a table data instruction;
In response to detection of the table data instruction by the table data instruction detection means, the insertion destination of the symbol representing the division of the function is changed to immediately before the table data instruction instead of immediately before the branch label or the branch destination label, and the table data A software conversion apparatus comprising: table data arrangement changing means for arranging an instruction outside a function unit.
11. The software conversion apparatus according to claim 7, wherein the function end instruction is a return instruction or a goto instruction.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
JP2003317600A JP2005085051A (en) | 2003-09-10 | 2003-09-10 | Software converter |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
JP2003317600A JP2005085051A (en) | 2003-09-10 | 2003-09-10 | Software converter |
Publications (1)
Publication Number | Publication Date |
---|---|
JP2005085051A true JP2005085051A (en) | 2005-03-31 |
Family
ID=34417100
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
JP2003317600A Pending JP2005085051A (en) | 2003-09-10 | 2003-09-10 | Software converter |
Country Status (1)
Country | Link |
---|---|
JP (1) | JP2005085051A (en) |
Cited By (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
WO2018161283A1 (en) * | 2017-03-08 | 2018-09-13 | 华为技术有限公司 | Packet processing method and apparatus |
-
2003
- 2003-09-10 JP JP2003317600A patent/JP2005085051A/en active Pending
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
WO2018161283A1 (en) * | 2017-03-08 | 2018-09-13 | 华为技术有限公司 | Packet processing method and apparatus |
CN110352400A (en) * | 2017-03-08 | 2019-10-18 | 华为技术有限公司 | Method and apparatus for handling message |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
Grönniger et al. | Monticore: a framework for the development of textual domain specific languages | |
JP5967618B2 (en) | Method for optimizing binary code of a language having access to binary-coded decimal variables, and computer and computer program thereof | |
CN108139891A (en) | Include suggesting for the missing of external file | |
JP2006243838A (en) | Program development device | |
Zhu et al. | BiYacc: Roll your parser and reflective printer into one | |
JP2008276735A (en) | Program code converter and program code conversion method | |
JP4888790B2 (en) | Contract definition function verification apparatus, method and program thereof | |
Diatchki et al. | High-level views on low-level representations | |
JP2005085051A (en) | Software converter | |
CN115809442A (en) | Method, device and equipment for obfuscating reverse code and readable storage medium | |
JP5109646B2 (en) | Program source conversion device | |
CN114691151A (en) | Optimized code decompiling method and system based on deep learning | |
JP6264179B2 (en) | Compiling method, compiling device, and compiling program | |
KR20090011974A (en) | Method for extracting the target files of compilation | |
CN114816435A (en) | Software development method based on reverse technology | |
JP2018173699A (en) | Template generation device, template generation method and program | |
MacLennan | The Synmac syntax macroprocessor: Introduction and manual, version 5 | |
JP5238560B2 (en) | Translation apparatus, method, and program | |
JP2009258796A (en) | Program development device and program development method | |
Kats et al. | Interactive disambiguation of meta programs with concrete object syntax | |
US9792197B2 (en) | Apparatus and program | |
JP7083473B2 (en) | Input support device | |
KR101670726B1 (en) | Method for code conversion using debugging information of intermediate language code, apparatus and computer-readable recording medium with program therefor | |
Okuda et al. | Lake symbols for island parsing | |
CN114327469A (en) | Code analysis method, device, equipment and medium |