Debian Codegraph @

doc/bc_notes

raw
1 Op Codes
2 ========
3 Op_illegal, /* illegal entry == 0 */
4
5 /* binary operators */
6 Op_times,
7 Op_times_i, all _i operators here are used to optimise arithmetic
8 Op_quotient, by converting push, push , op to just push, op_i
9 Op_quotient_i,
10 Op_mod,
11 Op_mod_i,
12 Op_plus,
13 Op_plus_i,
14 Op_minus,
15 Op_minus_i,
16 Op_exp,
17 Op_exp_i,
18 Op_concat,
19
20 /* line range instruction pairs */
21 Op_line_range, /* flags for Op_cond_pair */
22 Op_cond_pair, /* conditional pair */
23
24 Op_subscript, calculate array subscript
25
26 /* unary operators */
27 Op_preincrement,
28 Op_predecrement,
29 Op_postincrement,
30 Op_postdecrement,
31 Op_unary_minus,
32 Op_field_spec, usage: push #, field_spec -> $#
33
34 /* unary relationals */
35 Op_not,
36
37 /* assignments */
38 Op_assign, simple assign
39 Op_assign_times, *= etc
40 Op_assign_quotient,
41 Op_assign_mod,
42 Op_assign_plus,
43 Op_assign_minus,
44 Op_assign_exp,
45 Op_assign_concat, optimised concatenation
46
47 /* boolean binaries */
48 Op_and, /* a left subexpression in && */
49 Op_and_final, /* right subexpression of && */
50 Op_or,
51 Op_or_final,
52
53 /* binary relationals */
54 Op_equal,
55 Op_notequal,
56 Op_less,
57 Op_greater,
58 Op_leq,
59 Op_geq,
60 Op_match,
61 Op_match_rec, /* match $0 */
62 Op_nomatch,
63
64 Op_rule,
65
66 /* keywords */
67 Op_K_case,
68 Op_K_default,
69 Op_K_break,
70 Op_K_continue,
71 Op_K_print,
72 Op_K_print_rec,
73 Op_K_printf,
74 Op_K_next,
75 Op_K_exit,
76 Op_K_return,
77 Op_K_delete,
78 Op_K_delete_loop,
79 Op_K_getline,
80 Op_K_nextfile,
81
82 Op_builtin,
83 Op_in_array, /* boolean test of membership in array */
84
85 /* function call instruction pairs */
86 Op_func_call,
87
88 Op_push, /* variable */
89 Op_push_i, /* number, string */
90 Op_push_re, /* regex */
91 Op_push_array,
92 Op_push_param,
93 Op_push_lhs, var assignment
94 Op_subscript_lhs, array assignment
95 Op_field_spec_lhs, field assignment
96 Op_no_op, /* jump target */
97 Op_pop, /* pop an item from the runtime stack */
98 Op_jmp,
99 Op_jmp_true,
100 Op_jmp_false,
101 Op_push_loop, /* break (continue) target for loop */
102 Op_pop_loop, end of loop
103 Op_get_record, read next input line
104 Op_newfile, open next input file
105 Op_arrayfor_init, initialise array for loop
106 Op_arrayfor_incr, next in array for loop
107 Op_arrayfor_final, end of array loop
108
109 Op_var_update, /* update value of NR, NF or FNR */
110 Op_var_assign,
111 Op_field_assign,
112
113 Op_ext_func,
114 Op_func,
115
116 Op_exec_count, debugging etc
117 Op_breakpoint,
118 Op_lint,
119 Op_exit, /* end of instructions, contains final exit value */
120
121 /*--------- end proper opcodes -------*/
122
123 /* program structures */
124 Op_K_do,
125 Op_K_for,
126 Op_K_arrayfor,
127 Op_K_while,
128 Op_K_switch,
129 Op_K_if,
130 Op_K_else,
131 Op_K_function,
132 Op_cond_exp,
133 Op_list,
134 Op_case_list,
135
136 /* I/O redirection for print statements */
137 Op_redirect_output,
138 Op_redirect_append,
139 Op_redirect_pipe,
140 Op_redirect_pipein,
141 Op_redirect_input,
142 Op_redirect_twoway,
143
144 Op_final /* sentry value, not legal */
145
146
147 INSTRUCTIONs
148 ============
149
150 typedef struct exp_instruction {
151 struct exp_instruction *nexti;
152 union {
153 NODE *dn;
154 struct exp_instruction *di;
155 NODE *(*fptr) P((int));
156 long dl;
157 unsigned long long ldl;
158 char *name;
159 } d;
160
161 union {
162 long xl;
163 NODE *xn;
164 void (*aptr) P((void));
165 struct exp_instruction *xi;
166 struct break_point *bpt;
167 } x;
168
169 short source_line;
170 OPCODE opcode;
171 } INSTRUCTION;
172
173 #define lextok d.name
174 #define func_name d.name
175
176 #define memory d.dn
177 #define builtin d.fptr
178 #define builtin_idx d.dl
179
180 #define expr_count x.xl
181
182 #define target_continue d.di
183 #define target_jmp d.di
184 #define target_break x.xi
185
186 /* Op_rule */
187 #define in_rule x.xl
188 #define source_file d.name
189
190 /* Op_K_case, Op_K_default */
191 #define target_stmt x.xi
192
193 /* Op_case_list, Op_K_switch */
194 #define case_val d.di
195 #define case_stmt x.xi
196 #define switch_dflt x.xi
197 #define switch_body d.di /* pretty printing and profiling */
198
199 /* Op_K_exit */
200 #define target_exit x.xi
201
202 /* Op_exit */
203 #define exit_value d.dl
204
205 /* Op_K_getline */
206 #define into_var x.xl
207
208 /* Op_K_getline, Op_K_print, Op_K_print_rec, Op_K_printf */
209 #define redir_type d.dl
210
211 /* Op_arrayfor_incr */
212 #define array_var x.xn
213
214 /* Op_line_range */
215 #define triggered x.xl
216
217 /* Op_cond_pair */
218 #define line_range x.xi
219
220 /* Op_func_call, Op_func */
221 #define func_body x.xn
222
223 /* Op_subscript */
224 #define sub_count d.dl
225
226 /* Op_push_lhs, Op_subscript_lhs, Op_field_spec_lhs */
227 #define do_reference x.xl
228
229 /* Op_list, Op_rule, Op_func */
230 #define lasti d.di
231 #define firsti x.xi
232
233 /* Op_rule, Op_func */
234 #define last_line x.xl
235 #define first_line source_line
236
237 /* Op_lint */
238 #define lint_type d.dl
239
240 /* Op_field_spec_lhs */
241 #define target_assign d.di
242
243 /* Op_field_assign */
244 #define field_assign x.aptr
245
246 /* Op_concat */
247 #define var_concat d.dl
248
249 /* Op_breakpoint */
250 #define break_pt x.bpt
251
252 /*------------------ pretty printing/profiling --------*/
253
254 /* Op_exec_count */
255 #define exec_count d.ldl
256
257 /* Op_K_while */
258 #define while_body d.di
259
260 /* Op_K_do */
261 #define doloop_cond d.di
262
263 /* Op_K_for */
264 #define forloop_cond d.di
265 #define forloop_body x.xi
266
267 /* Op_K_if */
268 #define branch_if d.di
269 #define branch_else x.xi
270
271 /* Op_K_else */
272 #define branch_end x.xi
273
274 /* Op_line_range */
275 #define condpair_left d.di
276 #define condpair_right x.xi
277
278 Programs consist of a list of INSTRUCTIONs linked by nexti pointers.
279 Each source statement generates a list of INSTRUCTIONs which are then in turn linked to create the program list.
280
281 The list for a given source statement is delimited by the lasti (last INSTRUCTION) pointer.
282 (The first INSTRUCTION in any rule is identified by the firsti pointer. This is only used in profiling (I think).)
283
284 Each INSTRUCTION can be linked to a NODE via the d.dn (memory) pointer.
285
286 The BEGIN rule(s) are prepended to the program list and the END rule(s) are appended to complete the program build process.
287
288 At run-time, the "virtual machine" walks the program list using the nexti pointers.
289
290 The list_xxx functions are used to create and manipulate these INSTRUCTION lists.
291
292 list_create creates an INSTRUCTION with opcode Op_list and nexti and lasti pointers pointing to itself (an empty list).
293
294 list_append adds a single INSTRUCTION to the end of an existing list and list_prepend does the same at the start of an existing list.
295 list_merge adds one list to the end of another (and frees the "header" INSTRUCTION of the added list);
296
297 The list_append, list_prepend and list_merge functions reset the nexti and lasti pointers as needed.
298
299
300
301 Run-time Stack
302 ==============
303
304 Stack is allocated in eval.c line 1040 with an initial 256 (NODE) entries.
305 If this stack fills, additional blocks of 256 entries are added.
306
307 The various push operators add NODE entries to the stack for processing by subsequent operators.
308 Once a stack variable is used and no longer required, it is popped off and (if temporary) freed.
309 Current stack position is in stack_ptr.
310
311 Most operators (eg builtin functions) expect all of their "arguments" to be on the stack.
312 Some operators (eg Op_plus_i etc) expect one operand on the stack and another in the NODE linked to the
313 operator via the d.dn (memory) pointer.
314
315
316 Debugger
317 ========
318 The dgawk debugger usage is described in README.DGAWK. I suggest that we make -DBCDEBUG a default setting (or remove the ifdefs)
319 so that the dump and trace commands are always available.
320
321 dgawk operates in exactly the same way as gawk except that it offers the ability to dump the program list, to step through the program list, to set breakpoints and to view variables etc.
322
323 dgawk uses exactly the same "virtual machine" as gawk so the results are identical. The only difference is that dgawk checks to see if any debug action is required before executing each INSTRUCTION.
324