sourcepawn/decompiler/code_analyzer.h
author GoD-Tony <aangione@gmail.com>
Sun Aug 04 10:03:40 2013 -0400 (2013-08-04)
changeset 3971 d2e8100b2814
parent 2325 7a24ee5557fb
permissions -rw-r--r--
Added ISDKHooks interface with entity listeners (bug 5602, r=psychonic).
     1 #ifndef _INCLUDE_SP_DECOMP_CODE_ANALYZER_H_
     2 #define _INCLUDE_SP_DECOMP_CODE_ANALYZER_H_
     3 
     4 #include <sp_vm_types.h>
     5 
     6 enum Opcode
     7 {
     8 	_OP_VAR		= -1,		/* Variable container */
     9 	_OP_STMT	= -2,		/* Statement contained */
    10 	_OP_CONST	= -3,		/* Constant value */
    11 	_OP_STORE	= -4,		/* Store operation (binary assignment) */
    12 	_OP_NEW		= -5,		/* Variable declaration */
    13 	_OP_MODULO	= -6,		/* Binary modulo operator */
    14 	_OP_DEFINE	= -7,		/* "define" node for SSA */
    15 	_OP_USE		= -8,		/* "use" node for SSA */
    16 #define OPDEF(num,name,str,params) \
    17 	OP_##name = num,
    18 #include "opcodes.tbl"
    19 #undef OPDEF
    20 };
    21 
    22 struct sp_opdef_t
    23 {
    24 	const char *name;
    25 	int params;
    26 };
    27 
    28 struct FuncVar;
    29 
    30 struct BaseNode
    31 {
    32 	BaseNode(cell_t _op) : op(_op)
    33 	{
    34 	}
    35 	cell_t op;
    36 };
    37 
    38 struct ConstNode : public BaseNode
    39 {
    40 	ConstNode(cell_t _val) : BaseNode(_OP_CONST), val(_val)
    41 	{
    42 	}
    43 	cell_t val;
    44 };
    45 
    46 struct StmtNode : public BaseNode
    47 {
    48 	StmtNode(cell_t _op, BaseNode *_node, StmtNode *_next) : BaseNode(_op)
    49 		, node(_node)
    50 		, next(_next)
    51 	{
    52 	}
    53 	BaseNode *node;
    54 	StmtNode *next;
    55 };
    56 
    57 struct FunctionInfo;
    58 
    59 struct CallNode : public BaseNode
    60 {
    61 	CallNode(FunctionInfo *f, BaseNode **t, unsigned int c) : BaseNode(OP_CALL)
    62 		, func(f)
    63 		, args(t)
    64 		, argc(c)
    65 	{
    66 	}
    67 	FunctionInfo *func;
    68 	BaseNode **args;
    69 	unsigned int argc;
    70 };
    71 
    72 struct DefineNode : public BaseNode
    73 {
    74 	DefineNode(BaseNode *o, const char *fmt, ...);
    75 	char name[32];
    76 	BaseNode *other;
    77 };
    78 
    79 struct UseNode : public BaseNode
    80 {
    81 	UseNode(DefineNode *n) : BaseNode(_OP_USE), def(n)
    82 	{
    83 	}
    84 	DefineNode *def;
    85 };
    86 
    87 struct UnaryNode : public BaseNode
    88 {
    89 	UnaryNode(cell_t _op, BaseNode *_node) : BaseNode(_op), node(_node)
    90 	{
    91 	}
    92 	BaseNode *node;
    93 };
    94 
    95 struct VarNode : public BaseNode
    96 {
    97 	VarNode(FuncVar *_var) : BaseNode(_OP_VAR), var(_var)
    98 	{
    99 	}
   100 	FuncVar *var;
   101 };
   102 
   103 struct BinOpNode : public BaseNode
   104 {
   105 	BinOpNode(cell_t _op, BaseNode *_left, BaseNode *_right) : BaseNode(_op)
   106 		,left(_left)
   107 		,right(_right)
   108 	{
   109 	}
   110 	BaseNode *left;
   111 	BaseNode *right;
   112 };
   113 
   114 struct DeclNode : public BaseNode
   115 {
   116 	DeclNode(VarNode *_var, BaseNode *_expr) : BaseNode(_OP_NEW), var(_var), expr(_expr)
   117 	{
   118 	}
   119 	VarNode *var;
   120 	BaseNode *expr;
   121 };
   122 
   123 struct FuncVar
   124 {
   125 	sp_fdbg_symbol_t *sym;
   126 	sp_fdbg_arraydim_t *dims;
   127 	sp_tag_t *tag;
   128 	const char *name;
   129 	unsigned int dimcount;
   130 	DeclNode *new_stmt;
   131 };
   132 
   133 struct FunctionInfo
   134 {
   135 	char name[64];
   136 	sp_fdbg_symbol_t *sym;
   137 	FuncVar known_args[SP_MAX_EXEC_PARAMS];
   138 	unsigned int num_known_args;
   139 	sp_tag_t *tag;
   140 	uint32_t code_addr;
   141 	uint32_t code_end;
   142 	bool is_public;
   143 	FunctionInfo *next;
   144 	FuncVar *known_vars;
   145 	unsigned int num_known_vars;
   146 };
   147 
   148 struct sp_decomp_t;
   149 
   150 int Sp_DecompFunction(sp_decomp_t *dc, uint32_t code_addr, bool is_public);
   151 
   152 #endif //_INCLUDE_SP_DECOMP_CODE_ANALYZER_H_