-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.h
374 lines (283 loc) · 9.88 KB
/
syntax.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/***********************************************************************************
Copyright 2014 Arizona Board of Regents; all rights reserved.
This software is being provided by the copyright holders under the
following license. By obtaining, using and/or copying this software, you
agree that you have read, understood, and will comply with the following
terms and conditions:
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee or royalty is hereby granted,
provided that the full text of this notice appears on all copies of the
software and documentation or portions thereof, including modifications,
that you make.
This software is provided "as is," and copyright holders make no
representations or warranties, expressed or implied. By way of example, but
not limitation, copyright holders make no representations or warranties of
merchantability or fitness for any particular purpose or that the use of the
software or documentation will not infringe any third party patents,
copyrights, trademarks or other rights. Copyright holders will bear no
liability for any use of this software or documentation.
The name and trademarks of copyright holders may not be used in advertising
or publicity pertaining to the software without specific, written prior
permission. Title to copyright in this software and any associated
documentation will at all times remain with copyright holders.
***********************************************************************************/
/*
* syntax.h
*
* Created on: May 17, 2011
* Author: genlu
*/
#ifndef SYNTAX_H_
#define SYNTAX_H_
#include <stdint.h>
#include "prv_types.h"
#include "array_list.h"
#include "writeSet.h"
#define PRINT_ALL 0
#define PRINT_SLICE 1
typedef struct SyntaxStack SyntaxStack;
/*********************************************************************************/
typedef struct AndOrTarget{
ADDRESS targetAddr;
int num;
}AndOrTarget;
/*********************************************************************************/
typedef enum{
//binary
OP_ADD,
OP_SUB,
OP_MUL,
OP_DIV,
OP_MOD,
OP_BITAND,
OP_BITOR,
OP_GE,
OP_GT,
OP_LE,
OP_LT,
OP_NE,
OP_EQ,
OP_INITPROP,
OP_ASSIGN,
//unary
OP_NOT,
// for INC and DEC (EXP_UN)
OP_VINC,
OP_VDEC,
OP_INCV,
OP_DECV,
// for AND and OR,
OP_AND,
OP_OR
} Operator;
/*********************************************************************************/
typedef enum{
EXP_NAME, //a name (for var/prop/func/etc)
EXP_UNDEFINED,
EXP_CONST_VALUE, // string, double, int
EXP_BIN, //binary op
EXP_UN, //unary op
EXP_CALL, // a function call, contains a list with func name node and its parameter nodes
EXP_NEW, // a constructor call, i.e. new abc()
EXP_ARRAY_ELM,
EXP_ARRAY_INIT, //and array initialzation struct, as [1,2,3...], contains a list of
EXP_PROP, //contain 2 children, one is the name/prep represent obj, other is name represent prop
EXP_EVAL,
EXP_TOKEN // this is for processing AND/OR instructions, serve as a temp token in the expression stack
} ExpTreeNodeType;
typedef struct ExpTreeNode ExpTreeNode;
struct ExpTreeNode{
ExpTreeNodeType type;
uint32_t flags;
union{
//EXP_NAME
char *name;
//EXP_CONST_VALUE
double const_value_double;
char *const_value_str;
long const_value_int;
bool const_value_bool;
//EXP_UN
struct{
char *name; // this is for inc/dec operation only
Operator op;
ExpTreeNode *lval;
} un_op;
//EXP_BIN
struct{
Operator op;
ExpTreeNode *lval;
ExpTreeNode *rval;
} bin_op;
//EXP_CALL and EXP_NEW, EXP_EVAL
struct{
ExpTreeNode *name;
int num_paras;
ArrayList *parameters; //this is a list of ExpTreeNode
BasicBlock *funcEntryBlock;
} func;
//EXP_ARRAY_ELM
struct{
ExpTreeNode *name;
ExpTreeNode *index;
}array_elm;
//EXP_ARRAY_INIT
struct{
int size;
ArrayList *initValues; //an list of ExpTreeNode
}array_init;
//EXP_PROP
struct{
ExpTreeNode *objName;
ExpTreeNode *propName;
} prop;
struct{
ADDRESS targetAddr;
Operator op;
} tok;
} u;
};
#define EXP_IS_STRING (1<<0)
#define EXP_IS_INT (1<<1)
#define EXP_IS_DOUBLE (1<<2)
#define EXP_IS_BOOL (1<<3)
#define EXP_IS_FUNCTION_OBJ (1<<4)
#define EXP_IS_PROP_INIT (1<<5)
#define EXP_IS_NULL (1<<6)
#define EXP_IS_REGEXP (1<<7)
#define EXP_IS_DOC_WRITE (1<<8)
#define EXP_IN_SLICE (1<<16)
#define EXP_SET_FLAG(exp, flag) (exp->flags |= flag)
#define EXP_CLR_FLAG(exp, flag) (exp->flags &= ~flag)
#define EXP_HAS_FLAG(exp, flag) (exp->flags & flag)
/*********************************************************************************/
typedef enum {
TN_FUNCTION,
TN_BLOCK, //basic block
//TN_INC_DEC,
TN_IF_ELSE,
TN_GOTO,
TN_RETURN,
TN_RETEXP,
TN_EXP, //expression
TN_DEFVAR,
TN_WHILE,
TN_TRY_CATCH,
TN_THROW //throw uses only the in SyntaxTreeNode
} SyntaxTreeNodeType;
typedef struct SyntaxTreeNode SyntaxTreeNode;
struct SyntaxTreeNode{
int id;
SyntaxTreeNodeType type;
uint32_t flags;
SyntaxTreeNode *parent; //parent SyntaxTreeNode in AST
ArrayList *predsList; //a list of predesessor syntaxTreeNode of this node
//(ONLY for those end with a goto to this node)
union{
struct{
int cfg_id; //old id is the id# BasicBlock contained in this SyntaxTreeNode when its created
BasicBlock *cfgBlock;
ArrayList *statements; // a list of SyntaxTreeNode
//SyntaxStack *stack;
} block;
struct{
ExpTreeNode *cond;
NaturalLoop *loopStruct;
BasicBlock *header;
SyntaxTreeNode *synHeader; //pointer to the header syntaxTree nnde
ArrayList *loopBody;
ArrayList *loopBody2; //this list is merely used for easier check of loop membership
int _bodyBranchType; // -1: inf loop(no branch) | 0: body is branch not taken | 1: body is branch taken
//SyntaxTreeNode *checkPointInLoop;
//SyntaxTreeNode *checkPointAfterLoop;
}loop;
struct{
Function *funcStruct;
ArrayList *funcBody; // a list of SyntaxTreeNode
SyntaxTreeNode *synFuncEntryNode; //pointer to the func entry syntax tree node
//SyntaxTreeNode *checkPoint;
} func;
ExpTreeNode *expNode;
struct{
BasicBlock *targetBlock;
SyntaxTreeNode *synTargetBlock; //for continue and break, this point to a parent block it cont/break
} go_to;
struct{
int _ifeq;
ExpTreeNode *cond;
// ifeq: if-path->adj_path else-path->branch_path
// ifne: if-path->branch_path else-path->adj_path
ArrayList *if_path; //both are lists of SyntaxTreeNodes
ArrayList *else_path;
//SyntaxTreeNode *checkPointIfPath;
//SyntaxTreeNode *checkPointElsePath;
} if_else;
//current implementation is incomplete and only for signature function generation
struct{
ArrayList *try_body;
ArrayList *catch_body;
} try_catch;
//todo:
//switch
} u;
};
#define TN_IS_GOTO_BREAK (1<<0)
#define TN_IS_GOTO_CONTINUE (1<<1)
#define TN_NOT_SHOW_LABEL (1<<2)
#define TN_HIDE (1<<3) //don't print this entire statement(s) in printSyntaxNode function (used for go_to nodes)
#define TN_AFTER_RELINK_GOTO (1<<4) //this flag indicates that syntax node id should be uesd in printed label
#define TN_IS_LOOP_HEADER (1<<5)
#define TN_DONE_MOVE (1<<6)
#define TN_HAS_CHECKPOINT (1<<7)
#define TN_IN_SLICE (1<<16)
#define TN_IS_CHECK_POINT (1<<17) //all if(decisionVec[index++]!=THIS_PATH) throw "wrong path"
#define TN_IS_SIG_FUNCTION (1<<18) //including function, try-catch statement,
// return false in catch and return true at the end of function
// all check points and decision vector
#define TN_SET_FLAG(tn, flag) (tn->flags |= flag)
#define TN_CLR_FLAG(tn, flag) (tn->flags &= ~flag)
#define TN_HAS_FLAG(tn, flag) (tn->flags & flag)
/*********************************************************************************/
typedef enum{
SYN_NODE,
EXP_NODE
} NodeType;
typedef struct StackNode StackNode;
struct StackNode{
NodeType type;
void *node; //pointer to ExpTreeNode or SyntaxTreeNode, depends on type
StackNode *prev; //point to the direction of stack top
StackNode *next; //point to the direction of stack bottom
};
struct SyntaxStack{
StackNode *bottom;
StackNode *top;
int number;
};
StackNode *popSyntaxStack(SyntaxStack *stack);
void pushSyntaxStack(SyntaxStack *stack, StackNode *node);
SyntaxStack *createSyntaxStack(void);
/*********************************************************************************/
StackNode *popSyntaxStack(SyntaxStack *stack);
void pushSyntaxStack(SyntaxStack *stack, StackNode *node);
StackNode *createSyntaxStackNode();
void destroySyntaxStackNode(StackNode *node );
SyntaxStack *createSyntaxStack(void);
void stackTest();
void printSyntaxTreeNode(SyntaxTreeNode *node, int slice_flag);
ExpTreeNode *createExpTreeNode();
void destroyExpTreeNode(ExpTreeNode *node);
SyntaxTreeNode *createSyntaxTreeNode();
void destroySyntaxTreeNode(SyntaxTreeNode *node);
int SyntaxTreeNodeCompareByBlockID(void *i1, void *i2);
SyntaxTreeNode *buildSyntaxTreeForBlock(BasicBlock *block, uint32_t flag, ArrayList *funObjTable, ArrayList *funcCFGs, int slice_flag);
ArrayList *buildSyntaxTree(InstrList *iList, ArrayList *blockList, ArrayList *loopList, ArrayList *funcCFGs, ArrayList *funcObjTable, int slice_flag);
SyntaxTreeNode *findAdjacentStatement(ArrayList *syntaxTree, SyntaxTreeNode *node);
SyntaxTreeNode *findNextStatement(ArrayList *syntaxTree, SyntaxTreeNode *node);
//moving syntaxNode around in syntax tree
SyntaxTreeNode *insertBefore(ArrayList *syntaxTree, SyntaxTreeNode *node_ins, SyntaxTreeNode *node_target);
SyntaxTreeNode *insertAfter(ArrayList *syntaxTree, SyntaxTreeNode *node_ins, SyntaxTreeNode *node_target);
SyntaxTreeNode *moveToBefore(ArrayList *syntaxTree, SyntaxTreeNode *node_move, SyntaxTreeNode *node_target);
SyntaxTreeNode *moveToAfter(ArrayList *syntaxTree, SyntaxTreeNode *node_move, SyntaxTreeNode *node_target);
void transformSyntaxTree(ArrayList *syntaxTree);
#endif /* SYNTAX_H_ */