JSIR


JSIR is an Intermediate Representation for JavaScript Analysis

View the Project on GitHub too4words/jsir

We see a need to stardarize an intermediate representation (IR) for those working on static analysis tools for JavaScript. Our hope that different researchers will contribute translators to JSIR that run on different platforms such as Java, .NET, and, finally, JavaScript itself.

Since it is in many cases desirable to do program analysis enterily in the browser, it should also be possible to create JSIR by making REST calls to a server responsible for converting. One possibilty for the AST format supported by Esprima.

Below is an ANTLR v4 grammar for JSIR. Note that we use the /* @name {RHS}*/ syntax to help code generation tools give meaninful human-readable names to various data members within the generated classes or structures.

You can also download the Antlr file.

  
  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
/*
    - SSA-numbered bindings 
    - lexical scopes
    - fully dismantles control flow
    - support for CFGs with Entry and Exit BasicBlocks
    - the Blocks array in a ControlFlowGraph does NOT contain Entry and Exit BasicBlocks
*/
/* @namespace {Microsoft.CodeAnalysis.JSIRGrammar} */
grammar JSIR;
prog : Program;

Mode : 'strict' | 'non';
Boolean : 'true' | 'false';
Eval : 'eval';
Null : 'null';
This : 'this';
Undefined : 'undefined';
Number : NUMBER+;

Value :             
    Number
  | ConstantString
  | Boolean
  | Eval
  | Null
  | This
  | Undefined
  | Binding
  | FunctionReference   
;

Program : GlobalScope /* @name {functionDeclarations}*/FunctionDeclaration* ControlFlowGraph;

Statement :
      Assignment
    | Call  
    | DebuggerStatement
    | ReturnStatement
    ;

Call : StaticMethodCall | MethodCall | ConstructorCall ;

Assignment : 
      SimpleAssignment 
    | FieldAssignement 
    | ArrayAssignment     
           ;

SimpleAssignment     :   
    /* @name {LHS}*/Binding 
    '=' 
    /* @name {RHS}*/Expression;
FieldAssignement    :   
    /* @name {LHS}*/Binding 
    '.' /* @name {field}*/Binding 
    '=' 
    /* @name {RHS}*/Binding;
ArrayAssignment     :   
    /* @name {LHS}*/Binding 
    '[' 
    /* @name {index}*/ Binding 
    ']' 
    '=' 
    /* @name {RHS}*/Binding;

StaticMethodCall      : 
    /* @name {LHS}*/Binding
    '=' 
    /* @name {target}*/Binding
    '(' /* @name {arguments}*/Binding (',' /* @name {arguments}*/Binding)*')' 
    ';';

MethodCall :   
    /* @name {LHS}*/Binding 
    '=' 
    /* @name {target}*/Binding 
    '.' 
    /* @name {field}*/STRING
    '(' 
        /* @name {parameter}*/Binding ? (',' /* @name {parameter}*/Binding)* 
    ')';
ConstructorCall     :   
    /* @name {LHS}*/Binding 
    '=' 
    'new' 
    /* @name {function}*/Binding 
    '(' /* @name {parameter}*/Binding ? (',' /* @name {parameter}*/Binding)* ')'
    ;

Binding : Type /* @name {variable}*/ID '_' Version;// Scope;

// This is an uninterpreted type 
Type    : STRING;

/* This is intended to represent the SSA version */
Version : NUMBER+;

Scope : GlobalScope | LocalScope;
GlobalScope : 
    /* @name {bindings}*/Binding (/* @name {bindings}*/Binding)*
    ;
LocalScope : 
    'var' 
    '{' /* @name {parent}*/Scope '}'
    /* @name {bindings}*/Binding (/* @name {bindings}*/Binding)*
    ';';

Expression :
    FunctionDeclaration
  | ObjectLiteral
  | ArrayLiteral
  | ArrayReference
  | FieldReference
  | PropertyNameIterator
  | BinaryOperation
  | UnaryOperation
  | PhiFunction 
  | IotaFunction 
  | DeleteOperation  
  | Value
    ;

FunctionDeclaration:
'function' /* @name {name}*/ID 
    '(' /* @name {arguments}*/Binding (',' /* @name {arguments}*/Binding)* ')' 
    '{' 
    Mode 
    // entry and exit blocks are not included in the list blocks above
    /* @name {declarations}*/LocalScope 
    /* @name {nestedFunctions}*/FunctionDeclaration*
    ControlFlowGraph
    '}'
    ;
ControlFlowGraph :
    /* @name {entryBlock}*/BasicBlock 
    /* @name {blocks}*/BasicBlock* 
    /* @name {exitBlock}*/BasicBlock       
    ;

KeyValuePair : /* @name {field}*/STRING ':' /* @name {value}*/Value;
ObjectLiteral:
'{' /* @name {pair}*/KeyValuePair (',' /* @name {pair}*/KeyValuePair)* '}';

ArrayLiteral :
'[' /* @name {elements}*/Binding (',' /* @name {elements}*/Binding)* ']';
ArrayReference:
/* @name {target}*/Binding '[' /* @name {index}*/Binding ']';
FieldReference:
/* @name {target}*/Binding '.' /* @name {field}*/STRING;

PropertyNameIterator: 
      'nextProperty' '('
      /* @name {object}*/ Binding ',' /* @name {previousPropName}*/ Binding
      ')' ;

DeleteBinding: 
    'delete' 
    /* @name {target}*/Binding 
    '.' 
    /* @name {field}*/Binding;
DeleteArrayReference: 
    'delete' 
    /* @name {target}*/Binding 
    '[' 
    /* @name {index}*/Binding 
    ']';
DeleteValue : 
    'delete' Binding;

BinaryOperation : 
    /* @name {LHS}*/Binding 
    BinaryOperator 
    /* @name {RHS}*/Binding;
UnaryOperation : 
    UnaryOperator Binding;

PhiFunction :
    'phi' 
    '('
    /* @name {bindings}*/Binding
    (',' /* @name {bindings}*/Binding)*
    ')'
    ;
IotaFunction : 
    'iota'             
    '('
    /* @name {variable}*/ID
    ')'
    ;

FunctionReference : 
    'ref'             
    '('
    /* @name {variable}*/ID
    ')'
    ;

DebuggerStatement : 'debugger';
ReturnStatement : 'return' /* @name {value} */Binding;

ConstantString : STRING;
Label: STRING;

Jump :
    SimpleGoto
  | ConditionalGoto
  | ThrowTo;

SimpleGoto : 'goto' /* @name {labels}*/Label (',' /* @name {labels}*/Label)*;
ConditionalGoto : 
    'goto' 
    /* @name {predicate}*/Binding 
    /* @name {label1}*/Label 
    /* @name {label2}*/Label;
ThrowTo : 
    'throwto' Binding;

BasicBlock :
    RegularBasicBlock |
    ExceptionalBasicBlock;

//  EmptyBasicBlock |
//EmptyBasicBlock : Label Jump;

RegularBasicBlock : Label Statement* Jump;

ExceptionalBasicBlock : 'set-catch-label' Label Binding Statement* Jump;

UnaryOperator    : '!'| '-' | '+' | '~'| 'typeof' | 'void' | '++'| '--';
BinaryOperator   : '+'|'-'|'^'|'|'|'*'|'/'|'%'| 
                   '&&' | '||' | 
                   '|' | '&' | '>>' | '<<'| '>>>' | 
                   '^' | 'instanceof' |
                   '==' | '===' | '!=' | '!=='
                 ;

///////////////////////////////

STRING
    :   '"' StringCharacters? '"' |
        '\'' StringCharacters? '\'' ;
StringCharacters
    :   ('a'..'z')|('A'..'Z')|('0'..'9') ;

fragment NUMBER : '0'..'9';
fragment ID : ('a'..'z' | 'A'..'Z' | '_' | '-' | 'a'..'z' | 'A'..'Z' | '$' | NUMBER)+;

//WS : ('\r'|'\n'|'\t') -> skip ;
//COMMENT : (
//	('#'| '//') ~[\r\n]* '\r'? '\n'
//		| '/*[' .*? ']*/
//	)  -> skip ;