-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split decompilation to AST and Writer (#26)
* feat: split decompilation to AST and Writer Most of the code taken from: https://github.com/scaleton-labs/tvm-disassembler * add attribution for files from https://github.com/scaleton-labs/tvm-disassembler and to README.md * add 2 new opcodes for the new code
- Loading branch information
Showing
15 changed files
with
852 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@tact-lang/opcode", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"main": "dist/index.js", | ||
"repository": "https://github.com/tact-lang/ton-opcode.git", | ||
"author": "Steve Korshakov <[email protected]>", | ||
|
@@ -25,7 +25,8 @@ | |
"ts-jest": "^29.0.5", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.2", | ||
"tvm-disassembler": "^3.0.0" | ||
"tvm-disassembler": "^3.0.0", | ||
"@scaleton/func-debug-symbols": "^0.1.4" | ||
}, | ||
"peerDependencies": { | ||
"@ton/core": ">=0.49.2", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// This file is based on code from https://github.com/scaleton-labs/tvm-disassembler | ||
|
||
import { | ||
BlockNode, | ||
InstructionNode, | ||
MethodNode, | ||
ProcedureNode, | ||
ProgramNode, | ||
ReferenceNode, | ||
ScalarNode, | ||
NodeType, | ||
ControlRegisterNode, | ||
StackEntryNode, | ||
GlobalVariableNode, | ||
MethodReferenceNode, | ||
} from './nodes'; | ||
|
||
export class AST { | ||
static program( | ||
methods: MethodNode[], | ||
procedures: ProcedureNode[], | ||
): ProgramNode { | ||
return { | ||
type: NodeType.PROGRAM, | ||
methods, | ||
procedures, | ||
}; | ||
} | ||
|
||
static method( | ||
id: number, | ||
body: BlockNode, | ||
sourceHash: string, | ||
sourceOffset: number, | ||
): MethodNode { | ||
return { | ||
type: NodeType.METHOD, | ||
id, | ||
body, | ||
hash: sourceHash, | ||
offset: sourceOffset, | ||
}; | ||
} | ||
|
||
static procedure(hash: string, body: BlockNode): ProcedureNode { | ||
return { | ||
type: NodeType.PROCEDURE, | ||
hash, | ||
body, | ||
}; | ||
} | ||
|
||
static block( | ||
instructions: InstructionNode[], | ||
hash: string, | ||
offset: number, | ||
length: number, | ||
): BlockNode { | ||
return { | ||
type: NodeType.BLOCK, | ||
instructions, | ||
hash, | ||
offset, | ||
length, | ||
}; | ||
} | ||
|
||
static instruction( | ||
opcode: InstructionNode['opcode'], | ||
args: InstructionNode['arguments'], | ||
offset: number, | ||
length: number, | ||
hash: string, | ||
): InstructionNode { | ||
return { | ||
type: NodeType.INSTRUCTION, | ||
opcode, | ||
arguments: args, | ||
offset, | ||
length, | ||
hash, | ||
}; | ||
} | ||
|
||
static scalar(value: string | number | bigint): ScalarNode { | ||
return { | ||
type: NodeType.SCALAR, | ||
value, | ||
}; | ||
} | ||
|
||
static reference(hash: string): ReferenceNode { | ||
return { | ||
type: NodeType.REFERENCE, | ||
hash, | ||
}; | ||
} | ||
|
||
static controlRegister(index: number): ControlRegisterNode { | ||
return { | ||
type: NodeType.CONTROL_REGISTER, | ||
value: index, | ||
}; | ||
} | ||
|
||
static stackEntry(index: number): StackEntryNode { | ||
return { | ||
type: NodeType.STACK_ENTRY, | ||
value: index, | ||
}; | ||
} | ||
|
||
static globalVariable(index: number): GlobalVariableNode { | ||
return { | ||
type: NodeType.GLOBAL_VARIABLE, | ||
value: index, | ||
}; | ||
} | ||
|
||
static methodReference(method: number): MethodReferenceNode { | ||
return { | ||
type: NodeType.METHOD_REFERENCE, | ||
methodId: method, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './nodes'; | ||
export * from './AST'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// This file is based on code from https://github.com/scaleton-labs/tvm-disassembler | ||
|
||
import {Cell} from '@ton/core'; | ||
import {OpCode} from '../codepage/opcodes.gen'; | ||
|
||
export enum NodeType { | ||
PROGRAM, | ||
METHOD, | ||
BLOCK, | ||
INSTRUCTION, | ||
SCALAR, | ||
REFERENCE, | ||
PROCEDURE, | ||
CONTROL_REGISTER, | ||
STACK_ENTRY, | ||
GLOBAL_VARIABLE, | ||
METHOD_REFERENCE, | ||
} | ||
|
||
export type ControlRegisterNode = { | ||
type: NodeType.CONTROL_REGISTER; | ||
value: number; | ||
}; | ||
|
||
export type StackEntryNode = { | ||
type: NodeType.STACK_ENTRY; | ||
value: number; | ||
}; | ||
|
||
export type GlobalVariableNode = { | ||
type: NodeType.GLOBAL_VARIABLE; | ||
value: number; | ||
}; | ||
|
||
export type ScalarNode = { | ||
type: NodeType.SCALAR; | ||
value: number | string | bigint | Cell; | ||
}; | ||
|
||
export type ReferenceNode = { | ||
type: NodeType.REFERENCE; | ||
hash: string; | ||
}; | ||
|
||
export type MethodReferenceNode = { | ||
type: NodeType.METHOD_REFERENCE; | ||
methodId: number; | ||
}; | ||
|
||
export type InstructionNode = { | ||
type: NodeType.INSTRUCTION; | ||
opcode: OpCode['code']; | ||
arguments: ( | ||
| ScalarNode | ||
| BlockNode | ||
| ReferenceNode | ||
| StackEntryNode | ||
| ControlRegisterNode | ||
| GlobalVariableNode | ||
| MethodReferenceNode | ||
)[]; | ||
offset: number; | ||
length: number; | ||
hash: string; | ||
}; | ||
|
||
export type BlockNode = { | ||
type: NodeType.BLOCK; | ||
instructions: InstructionNode[]; | ||
hash: string; | ||
offset: number; | ||
length: number; | ||
}; | ||
|
||
export type MethodNode = { | ||
type: NodeType.METHOD; | ||
id: number; | ||
body: BlockNode; | ||
hash: string; | ||
offset: number; | ||
}; | ||
|
||
export type ProcedureNode = { | ||
type: NodeType.PROCEDURE; | ||
hash: string; | ||
body: BlockNode; | ||
}; | ||
|
||
export type ProgramNode = { | ||
type: NodeType.PROGRAM; | ||
methods: MethodNode[]; | ||
procedures: ProcedureNode[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.