Kannel: Open Source WAP and SMS gateway  svn-r5335
wsasm.h
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  *
59  * wsasm.h
60  *
61  * Author: Markku Rossi <mtr@iki.fi>
62  *
63  * Copyright (c) 1999-2000 WAPIT OY LTD.
64  * All rights reserved.
65  *
66  * Byte-code assembler definitions.
67  *
68  */
69 
70 #ifndef WSASM_H
71 #define WSASM_H
72 
73 /********************* Opcodes for the ASM instructions *****************/
74 
75 /* The instruction classes. */
76 
77 /* Class 1: 1xxPPPPP */
78 
79 #define WS_ASM_CLASS1P(op) (((op) & 0x80) == 0x80)
80 #define WS_ASM_CLASS1_OP(op) ((op) & 0xe0)
81 #define WS_ASM_CLASS1_ARG(op) ((op) & 0x1f)
82 
83 /* Class 2: 010xPPPP */
84 
85 #define WS_ASM_CLASS2P(op) (((op) & 0xe0) == 0x40)
86 #define WS_ASM_CLASS2_OP(op) ((op) & 0xf0)
87 #define WS_ASM_CLASS2_ARG(op) ((op) & 0x0f)
88 
89 /* Class 3: 011xxPPP */
90 
91 #define WS_ASM_CLASS3P(op) (((op) & 0xe0) == 0x60)
92 #define WS_ASM_CLASS3_OP(op) ((op) & 0x78)
93 #define WS_ASM_CLASS3_ARG(op) ((op) & 0x07)
94 
95 /* Class 4: 00xxxxxx */
96 
97 #define WS_ASM_CLASS4P(op) (((op) & 0xc0) == 0x00)
98 #define WS_ASM_CLASS4_OP(op) (op)
99 
100 /* Get the opcode of the operand `op'. The operand `op' can belong to
101  any of the classes 1-4. */
102 #define WS_ASM_OP(op) \
103  (WS_ASM_CLASS1P(op) \
104  ? WS_ASM_CLASS1_OP(op) \
105  : (WS_ASM_CLASS2P(op) \
106  ? WS_ASM_CLASS2_OP(op) \
107  : (WS_ASM_CLASS3P(op) \
108  ? WS_ASM_CLASS3_OP(op) \
109  : WS_ASM_CLASS4_OP(op))))
110 
111 /* Get the implicit argument of the operand `op'. The operand `op'
112  can belong to any of the classes 1-4. For the class 4 operands,
113  this returns 0. */
114 #define WS_ASM_ARG(op) \
115  (WS_ASM_CLASS1P(op) \
116  ? WS_ASM_CLASS1_ARG(op) \
117  : (WS_ASM_CLASS2P(op) \
118  ? WS_ASM_CLASS2_ARG(op) \
119  : (WS_ASM_CLASS3P(op) \
120  ? WS_ASM_CLASS3_ARG(op) \
121  : 0)))
122 
123 /* Create an operand with implicit argument from the operand `op' and
124  argument `arg'. */
125 #define WS_ASM_GLUE(op, arg) ((WsByte) (((WsByte) (op)) | ((WsByte) (arg))))
126 
127 
128 /* The instruction opcodes. Opcode Binary Size */
129 
130 #define WS_ASM_JUMP_FW_S 0x80 /* 10000000 1 */
131 #define WS_ASM_JUMP_FW 0x01 /* 00000001 2 */
132 #define WS_ASM_JUMP_FW_W 0x02 /* 00000010 3 */
133 
134 #define WS_ASM_JUMP_BW_S 0xa0 /* 10100000 1 */
135 #define WS_ASM_JUMP_BW 0x03 /* 00000011 2 */
136 #define WS_ASM_JUMP_BW_W 0x04 /* 00000100 3 */
137 
138 #define WS_ASM_TJUMP_FW_S 0xc0 /* 11000000 1 */
139 #define WS_ASM_TJUMP_FW 0x05 /* 00000101 2 */
140 #define WS_ASM_TJUMP_FW_W 0x06 /* 00000110 3 */
141 
142 #define WS_ASM_TJUMP_BW 0x07 /* 00000111 2 */
143 #define WS_ASM_TJUMP_BW_W 0x08 /* 00001000 3 */
144 
145 #define WS_ASM_CALL_S 0x60 /* 01100000 1 */
146 #define WS_ASM_CALL 0x09 /* 00001001 2 */
147 
148 #define WS_ASM_CALL_LIB_S 0x68 /* 01101000 2 */
149 #define WS_ASM_CALL_LIB 0x0a /* 00001010 3 */
150 #define WS_ASM_CALL_LIB_W 0x0b /* 00001011 4 */
151 
152 #define WS_ASM_CALL_URL 0x0c /* 00001100 4 */
153 #define WS_ASM_CALL_URL_W 0x0d /* 00001101 6 */
154 
155 #define WS_ASM_LOAD_VAR_S 0xe0 /* 11100000 1 */
156 #define WS_ASM_LOAD_VAR 0x0e /* 00001110 2 */
157 
158 #define WS_ASM_STORE_VAR_S 0x40 /* 01000000 1 */
159 #define WS_ASM_STORE_VAR 0x0f /* 00001111 2 */
160 
161 #define WS_ASM_INCR_VAR_S 0x70 /* 01110000 1 */
162 #define WS_ASM_INCR_VAR 0x10 /* 00010000 2 */
163 
164 #define WS_ASM_DECR_VAR 0x11 /* 00010001 2 */
165 
166 #define WS_ASM_LOAD_CONST_S 0x50 /* 01010000 1 */
167 #define WS_ASM_LOAD_CONST 0x12 /* 00010010 2 */
168 #define WS_ASM_LOAD_CONST_W 0x13 /* 00010011 3 */
169 
170 #define WS_ASM_CONST_0 0x14 /* 00010100 1 */
171 #define WS_ASM_CONST_1 0x15 /* 00010101 1 */
172 #define WS_ASM_CONST_M1 0x16 /* 00010110 1 */
173 #define WS_ASM_CONST_ES 0x17 /* 00010111 1 */
174 #define WS_ASM_CONST_INVALID 0x18 /* 00011000 1 */
175 #define WS_ASM_CONST_TRUE 0x19 /* 00011001 1 */
176 #define WS_ASM_CONST_FALSE 0x1a /* 00011010 1 */
177 
178 #define WS_ASM_INCR 0x1b /* 00011011 1 */
179 #define WS_ASM_DECR 0x1c /* 00011100 1 */
180 #define WS_ASM_ADD_ASG 0x1d /* 00011101 2 */
181 #define WS_ASM_SUB_ASG 0x1e /* 00011110 2 */
182 #define WS_ASM_UMINUS 0x1f /* 00011111 1 */
183 #define WS_ASM_ADD 0x20 /* 00100000 1 */
184 #define WS_ASM_SUB 0x21 /* 00100001 1 */
185 #define WS_ASM_MUL 0x22 /* 00100010 1 */
186 #define WS_ASM_DIV 0x23 /* 00100011 1 */
187 #define WS_ASM_IDIV 0x24 /* 00100100 1 */
188 #define WS_ASM_REM 0x25 /* 00100101 1 */
189 
190 #define WS_ASM_B_AND 0x26 /* 00100110 1 */
191 #define WS_ASM_B_OR 0x27 /* 00100111 1 */
192 #define WS_ASM_B_XOR 0x28 /* 00101000 1 */
193 #define WS_ASM_B_NOT 0x29 /* 00101001 1 */
194 #define WS_ASM_B_LSHIFT 0x2a /* 00101010 1 */
195 #define WS_ASM_B_RSSHIFT 0x2b /* 00101011 1 */
196 #define WS_ASM_B_RSZSHIFT 0x2c /* 00101100 1 */
197 
198 #define WS_ASM_EQ 0x2d /* 00101101 1 */
199 #define WS_ASM_LE 0x2e /* 00101110 1 */
200 #define WS_ASM_LT 0x2f /* 00101111 1 */
201 #define WS_ASM_GE 0x30 /* 00110000 1 */
202 #define WS_ASM_GT 0x31 /* 00110001 1 */
203 #define WS_ASM_NE 0x32 /* 00110010 1 */
204 
205 #define WS_ASM_NOT 0x33 /* 00110011 1 */
206 #define WS_ASM_SCAND 0x34 /* 00110100 1 */
207 #define WS_ASM_SCOR 0x35 /* 00110101 1 */
208 #define WS_ASM_TOBOOL 0x36 /* 00110110 1 */
209 
210 #define WS_ASM_POP 0x37 /* 00110111 1 */
211 
212 #define WS_ASM_TYPEOF 0x38 /* 00111000 1 */
213 #define WS_ASM_ISVALID 0x39 /* 00111001 1 */
214 
215 #define WS_ASM_RETURN 0x3a /* 00111010 1 */
216 #define WS_ASM_RETURN_ES 0x3b /* 00111011 1 */
217 
218 #define WS_ASM_DEBUG 0x3c /* 00111100 1 */
219 
220 /********************* Pseudo opcodes for assembler *********************/
221 
222 /* These are pseudo opcodes grouping together several real byte-code
223  opcodes. These are used in the symbolic assembler. */
224 
225 #define WS_ASM_P_LABEL 0x0100
226 #define WS_ASM_P_JUMP 0x0200
227 #define WS_ASM_P_TJUMP 0x0300
228 #define WS_ASM_P_CALL 0x0400
229 #define WS_ASM_P_CALL_LIB 0x0500
230 #define WS_ASM_P_CALL_URL 0x0600
231 #define WS_ASM_P_LOAD_VAR 0x0700
232 #define WS_ASM_P_STORE_VAR 0x0800
233 #define WS_ASM_P_INCR_VAR 0x0900
234 #define WS_ASM_P_LOAD_CONST 0x0a00
235 
236 /* Check whether the instruction `ins' is a pseudo-branch
237  instruction. */
238 #define WS_ASM_P_BRANCH(ins) \
239  ((ins)->type == WS_ASM_P_JUMP || (ins)->type == WS_ASM_P_TJUMP)
240 
241 /********************* Symbolic assembler instructions ******************/
242 
243 #define ws_label_idx u.ivalues.i1
244 #define ws_label_refcount u.ivalues.i2
245 #define ws_findex u.ivalues.i1
246 #define ws_lindex u.ivalues.i2
247 #define ws_args u.ivalues.i3
248 #define ws_vindex u.ivalues.i1
249 #define ws_cindex u.ivalues.i1
250 #define ws_label u.branch.label
251 #define ws_offset u.branch.offset
252 
254 {
255  struct WsAsmInsRec *next;
256  struct WsAsmInsRec *prev;
258 
259  /* The source stream line number. */
261 
262  /* The operands offset in the linearized byte-code stream. */
264 
265  union
266  {
267  /* The target label for branch instructions. */
268  struct
269  {
271 
272  /* The offset argument of the branch operand. This is the
273  adjustment that must be performed for the pc after this
274  instruction. */
276  }
277  branch;
278 
279  struct
280  {
284  }
285  ivalues;
286  } u;
287 };
288 
289 typedef struct WsAsmInsRec WsAsmIns;
290 
291 
292 /* Link the instruction `ins' to the end of the symbolic assembler
293  chain, currently being constructed in `compiler'. */
294 void ws_asm_link(WsCompilerPtr compiler, WsAsmIns *ins);
295 
296 /* Print the current assembler instructions of the compiler
297  `compiler'. */
298 void ws_asm_print(WsCompilerPtr compiler);
299 
300 /* Disassemble the byte-code `code', `len' to the standard output. */
301 void ws_asm_dasm(WsCompilerPtr compiler, const unsigned char *code,
302  size_t len);
303 
304 /* Linearize the assembler, currently being constructed in `compiler',
305  into `compiler->byte_code'. */
306 void ws_asm_linearize(WsCompilerPtr compiler);
307 
308 /* Constructors for assembler instructions. */
309 
310 /* Create a label instruction. */
312 
313 /* Create a branch instruction `ins' to label `label'. */
315  WsAsmIns *label);
316 
317 /* Create a local call instruction to function `findex'. */
319 
320 /* Create a library call instruction to function `findex' from the
321  libary `lindex'. */
323  WsUInt8 findex, WsUInt16 lindex);
324 
325 /* Create an URL call instruction for function `findex' from the URL
326  `urlindex' with `args' arguments. The arguments `urlindex' and
327  `findex' pont to the constant pool. */
329  WsUInt16 findex, WsUInt16 urlindex, WsUInt8 args);
330 
331 /* Create a variable modification instruction `ins' for the variable
332  `vindex'. */
334  WsUInt8 vindex);
335 
336 /* Create a constant loading instruction for the constant `cindex'. */
338  WsUInt16 cindex);
339 
340 /* Create an instruction `ins'. */
342 
343 #endif /* not WSASM_H */
WsUInt16 i2
Definition: wsasm.h:282
WsAsmIns * ws_asm_call_url(WsCompilerPtr compiler, WsUInt32 line, WsUInt16 findex, WsUInt16 urlindex, WsUInt8 args)
WsUInt32 i1
Definition: wsasm.h:281
struct WsAsmInsRec::@113::@115 ivalues
unsigned long WsUInt32
Definition: wsint.h:122
int code
Definition: smsc_cimd2.c:346
WsUInt16 i3
Definition: wsasm.h:283
WsUInt32 offset
Definition: wsasm.h:263
WsAsmIns * ws_asm_ins(WsCompilerPtr compiler, WsUInt32 line, WsUInt8 opcode)
unsigned char WsUInt8
Definition: wsint.h:116
union WsAsmInsRec::@113 u
WsUInt16 type
Definition: wsasm.h:257
WsAsmIns * ws_asm_load_const(WsCompilerPtr compiler, WsUInt32 line, WsUInt16 cindex)
struct WsAsmInsRec::@113::@114 branch
void ws_asm_link(WsCompilerPtr compiler, WsAsmIns *ins)
unsigned short WsUInt16
Definition: wsint.h:119
WsAsmIns * ws_asm_variable(WsCompilerPtr compiler, WsUInt32 line, WsUInt16 ins, WsUInt8 vindex)
struct WsAsmInsRec * next
Definition: wsasm.h:255
void ws_asm_dasm(WsCompilerPtr compiler, const unsigned char *code, size_t len)
Definition: wsasm.c:185
struct WsAsmInsRec * label
Definition: wsasm.h:270
void ws_asm_linearize(WsCompilerPtr compiler)
struct WsAsmInsRec * prev
Definition: wsasm.h:256
WsAsmIns * ws_asm_call_lib(WsCompilerPtr compiler, WsUInt32 line, WsUInt8 findex, WsUInt16 lindex)
WsUInt32 line
Definition: wsasm.h:260
WsAsmIns * ws_asm_branch(WsCompilerPtr compiler, WsUInt32 line, WsUInt16 ins, WsAsmIns *label)
WsAsmIns * ws_asm_label(WsCompilerPtr compiler, WsUInt32 line)
void ws_asm_print(WsCompilerPtr compiler)
WsAsmIns * ws_asm_call(WsCompilerPtr compiler, WsUInt32 line, WsUInt8 findex)
int opcode
Definition: test_cimd2.c:769
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.