1 /*
2  *  Ecmascript bytecode
3  */
4 
5 #ifndef DUK_JS_BYTECODE_H_INCLUDED
6 #define DUK_JS_BYTECODE_H_INCLUDED
7 
8 /*
9  *  Logical instruction layout
10  *  ==========================
11  *
12  *  !3!3!2!2!2!2!2!2!2!2!2!2!1!1!1!1!1!1!1!1!1!1! ! ! ! ! ! ! ! ! ! !
13  *  !1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!9!8!7!6!5!4!3!2!1!0!
14  *  +---------------------------------------------------+-----------+
15  *  !       C         !       B         !      A        !    OP     !
16  *  +---------------------------------------------------+-----------+
17  *
18  *  OP (6 bits):  opcode (DUK_OP_*), access should be fastest
19  *  A (8 bits):   typically a target register number
20  *  B (9 bits):   typically first source register/constant number
21  *  C (9 bits):   typically second source register/constant number
22  *
23  *  Some instructions combine BC or ABC together for larger parameter values.
24  *  Signed integers (e.g. jump offsets) are encoded as unsigned, with an opcode
25  *  specific bias.  B and C may denote a register or a constant, see
26  *  DUK_BC_ISREG() and DUK_BC_ISCONST().
27  *
28  *  Note: macro naming is a bit misleading, e.g. "ABC" in macro name but
29  *  the field layout is logically "CBA".
30  */
31 
32 typedef duk_uint32_t duk_instr_t;
33 
34 #define DUK_DEC_OP(x)               ((x) & 0x3fUL)
35 #define DUK_DEC_A(x)                (((x) >> 6) & 0xffUL)
36 #define DUK_DEC_B(x)                (((x) >> 14) & 0x1ffUL)
37 #define DUK_DEC_C(x)                (((x) >> 23) & 0x1ffUL)
38 #define DUK_DEC_BC(x)               (((x) >> 14) & 0x3ffffUL)
39 #define DUK_DEC_ABC(x)              (((x) >> 6) & 0x3ffffffUL)
40 
41 #define DUK_ENC_OP(op)              ((duk_instr_t) (op))
42 #define DUK_ENC_OP_ABC(op,abc)      ((duk_instr_t) ( \
43                                         (((duk_instr_t) (abc)) << 6) | \
44                                         ((duk_instr_t) (op)) \
45                                     ))
46 #define DUK_ENC_OP_A_BC(op,a,bc)    ((duk_instr_t) ( \
47                                         (((duk_instr_t) (bc)) << 14) | \
48                                         (((duk_instr_t) (a)) << 6) | \
49                                         ((duk_instr_t) (op)) \
50                                     ))
51 #define DUK_ENC_OP_A_B_C(op,a,b,c)  ((duk_instr_t) ( \
52                                         (((duk_instr_t) (c)) << 23) | \
53                                         (((duk_instr_t) (b)) << 14) | \
54                                         (((duk_instr_t) (a)) << 6) | \
55                                         ((duk_instr_t) (op)) \
56                                     ))
57 #define DUK_ENC_OP_A_B(op,a,b)      DUK_ENC_OP_A_B_C(op,a,b,0)
58 #define DUK_ENC_OP_A(op,a)          DUK_ENC_OP_A_B_C(op,a,0,0)
59 
60 /* Constants should be signed so that signed arithmetic involving them
61  * won't cause values to be coerced accidentally to unsigned.
62  */
63 #define DUK_BC_OP_MIN               0
64 #define DUK_BC_OP_MAX               0x3fL
65 #define DUK_BC_A_MIN                0
66 #define DUK_BC_A_MAX                0xffL
67 #define DUK_BC_B_MIN                0
68 #define DUK_BC_B_MAX                0x1ffL
69 #define DUK_BC_C_MIN                0
70 #define DUK_BC_C_MAX                0x1ffL
71 #define DUK_BC_BC_MIN               0
72 #define DUK_BC_BC_MAX               0x3ffffL
73 #define DUK_BC_ABC_MIN              0
74 #define DUK_BC_ABC_MAX              0x3ffffffL
75 #define DUK_BC_EXTRAOP_MIN          DUK_BC_A_MIN
76 #define DUK_BC_EXTRAOP_MAX          DUK_BC_A_MAX
77 
78 #define DUK_OP_LDREG                0
79 #define DUK_OP_STREG                1
80 #define DUK_OP_LDCONST              2
81 #define DUK_OP_LDINT                3
82 #define DUK_OP_LDINTX               4
83 #define DUK_OP_MPUTOBJ              5
84 #define DUK_OP_MPUTOBJI             6
85 #define DUK_OP_MPUTARR              7
86 #define DUK_OP_MPUTARRI             8
87 #define DUK_OP_NEW                  9
88 #define DUK_OP_NEWI                 10
89 #define DUK_OP_REGEXP               11
90 #define DUK_OP_CSREG                12
91 #define DUK_OP_CSREGI               13
92 #define DUK_OP_GETVAR               14
93 #define DUK_OP_PUTVAR               15
94 #define DUK_OP_DECLVAR              16
95 #define DUK_OP_DELVAR               17
96 #define DUK_OP_CSVAR                18
97 #define DUK_OP_CSVARI               19
98 #define DUK_OP_CLOSURE              20
99 #define DUK_OP_GETPROP              21
100 #define DUK_OP_PUTPROP              22
101 #define DUK_OP_DELPROP              23
102 #define DUK_OP_CSPROP               24
103 #define DUK_OP_CSPROPI              25
104 #define DUK_OP_ADD                  26
105 #define DUK_OP_SUB                  27
106 #define DUK_OP_MUL                  28
107 #define DUK_OP_DIV                  29
108 #define DUK_OP_MOD                  30
109 #define DUK_OP_BAND                 31
110 #define DUK_OP_BOR                  32
111 #define DUK_OP_BXOR                 33
112 #define DUK_OP_BASL                 34
113 #define DUK_OP_BLSR                 35
114 #define DUK_OP_BASR                 36
115 #define DUK_OP_EQ                   37
116 #define DUK_OP_NEQ                  38
117 #define DUK_OP_SEQ                  39
118 #define DUK_OP_SNEQ                 40
119 #define DUK_OP_GT                   41
120 #define DUK_OP_GE                   42
121 #define DUK_OP_LT                   43
122 #define DUK_OP_LE                   44
123 #define DUK_OP_IF                   45
124 #define DUK_OP_JUMP                 46
125 #define DUK_OP_RETURN               47
126 #define DUK_OP_CALL                 48
127 #define DUK_OP_CALLI                49
128 #define DUK_OP_TRYCATCH             50
129 #define DUK_OP_EXTRA                51
130 #define DUK_OP_PREINCR              52  /* pre/post opcode values have constraints, */
131 #define DUK_OP_PREDECR              53  /* see duk_js_executor.c */
132 #define DUK_OP_POSTINCR             54
133 #define DUK_OP_POSTDECR             55
134 #define DUK_OP_PREINCV              56
135 #define DUK_OP_PREDECV              57
136 #define DUK_OP_POSTINCV             58
137 #define DUK_OP_POSTDECV             59
138 #define DUK_OP_PREINCP              60
139 #define DUK_OP_PREDECP              61
140 #define DUK_OP_POSTINCP             62
141 #define DUK_OP_POSTDECP             63
142 #define DUK_OP_NONE                 64  /* dummy value used as marker */
143 
144 /* DUK_OP_EXTRA, sub-operation in A */
145 #define DUK_EXTRAOP_NOP             0
146 #define DUK_EXTRAOP_INVALID         1
147 #define DUK_EXTRAOP_LDTHIS          2
148 #define DUK_EXTRAOP_LDUNDEF         3
149 #define DUK_EXTRAOP_LDNULL          4
150 #define DUK_EXTRAOP_LDTRUE          5
151 #define DUK_EXTRAOP_LDFALSE         6
152 #define DUK_EXTRAOP_NEWOBJ          7
153 #define DUK_EXTRAOP_NEWARR          8
154 #define DUK_EXTRAOP_SETALEN         9
155 #define DUK_EXTRAOP_TYPEOF          10
156 #define DUK_EXTRAOP_TYPEOFID        11
157 #define DUK_EXTRAOP_INITENUM        12
158 #define DUK_EXTRAOP_NEXTENUM        13
159 #define DUK_EXTRAOP_INITSET         14
160 #define DUK_EXTRAOP_INITSETI        15
161 #define DUK_EXTRAOP_INITGET         16
162 #define DUK_EXTRAOP_INITGETI        17
163 #define DUK_EXTRAOP_ENDTRY          18
164 #define DUK_EXTRAOP_ENDCATCH        19
165 #define DUK_EXTRAOP_ENDFIN          20
166 #define DUK_EXTRAOP_THROW           21
167 #define DUK_EXTRAOP_INVLHS          22
168 #define DUK_EXTRAOP_UNM             23
169 #define DUK_EXTRAOP_UNP             24
170 #define DUK_EXTRAOP_DEBUGGER        25
171 #define DUK_EXTRAOP_BREAK           26
172 #define DUK_EXTRAOP_CONTINUE        27
173 #define DUK_EXTRAOP_BNOT            28
174 #define DUK_EXTRAOP_LNOT            29
175 #define DUK_EXTRAOP_INSTOF          30
176 #define DUK_EXTRAOP_IN              31
177 #define DUK_EXTRAOP_LABEL           32
178 #define DUK_EXTRAOP_ENDLABEL        33
179 
180 /* DUK_OP_CALL flags in A */
181 #define DUK_BC_CALL_FLAG_TAILCALL           (1 << 0)
182 #define DUK_BC_CALL_FLAG_EVALCALL           (1 << 1)
183 
184 /* DUK_OP_TRYCATCH flags in A */
185 #define DUK_BC_TRYCATCH_FLAG_HAVE_CATCH     (1 << 0)
186 #define DUK_BC_TRYCATCH_FLAG_HAVE_FINALLY   (1 << 1)
187 #define DUK_BC_TRYCATCH_FLAG_CATCH_BINDING  (1 << 2)
188 #define DUK_BC_TRYCATCH_FLAG_WITH_BINDING   (1 << 3)
189 
190 /* DUK_OP_RETURN flags in A */
191 #define DUK_BC_RETURN_FLAG_HAVE_RETVAL      (1 << 0)
192 
193 /* DUK_OP_DECLVAR flags in A; bottom bits are reserved for propdesc flags (DUK_PROPDESC_FLAG_XXX) */
194 #define DUK_BC_DECLVAR_FLAG_UNDEF_VALUE     (1 << 4)  /* use 'undefined' for value automatically */
195 #define DUK_BC_DECLVAR_FLAG_FUNC_DECL       (1 << 5)  /* function declaration */
196 
197 /* misc constants and helper macros */
198 #define DUK_BC_REGLIMIT             256  /* if B/C is >= this value, refers to a const */
199 #define DUK_BC_ISREG(x)             ((x) < DUK_BC_REGLIMIT)
200 #define DUK_BC_ISCONST(x)           ((x) >= DUK_BC_REGLIMIT)
201 #define DUK_BC_LDINT_BIAS           (1L << 17)
202 #define DUK_BC_LDINTX_SHIFT         18
203 #define DUK_BC_JUMP_BIAS            (1L << 25)
204 
205 #endif  /* DUK_JS_BYTECODE_H_INCLUDED */
206