2 /********************************************
4 copyright 1991-93, Michael D. Brennan
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
15 * Revision 1.6 1995/06/18 19:42:13 mike
16 * Remove some redundant declarations and add some prototypes
18 * Revision 1.5 1995/06/09 23:21:36 mike
19 * make sure there is an execution block in case user defines function,
20 * but no pattern-action pairs
22 * Revision 1.4 1995/03/08 00:06:22 mike
25 * Revision 1.3 1994/10/08 19:15:29 mike
28 * Revision 1.2 1993/07/07 00:07:38 mike
31 * Revision 1.1.1.1 1993/07/03 18:58:10 mike
34 * Revision 5.4 1993/01/14 13:11:11 mike
37 * Revision 5.3 1993/01/09 20:15:35 mike
38 * code_pop checks if the resolve_list needs relocation
40 * Revision 5.2 1993/01/07 02:50:33 mike
41 * relative vs absolute code
43 * Revision 5.1 1991/12/05 07:55:43 brennan
57 static CODEBLOCK *PROTO(new_code, (void)) ;
59 CODEBLOCK active_code ;
61 CODEBLOCK *main_code_p, *begin_code_p, *end_code_p ;
63 INST *begin_start, *main_start, *end_start ;
64 unsigned begin_size, main_size ;
66 INST *execution_start = 0 ;
69 /* grow the active code */
73 unsigned oldsize = code_limit - code_base ;
74 unsigned newsize = PAGESZ + oldsize ;
75 unsigned delta = code_ptr - code_base ;
77 if (code_ptr > code_limit) bozo("CODEWARN is too small") ;
80 zrealloc(code_base, INST_BYTES(oldsize),
81 INST_BYTES(newsize)) ;
82 code_limit = code_base + newsize ;
83 code_warn = code_limit - CODEWARN ;
84 code_ptr = code_base + delta ;
87 /* shrinks executable code that's done to its final size */
94 unsigned oldsize = INST_BYTES(p->limit - p->base) ;
95 unsigned newsize = INST_BYTES(p->ptr - p->base) ;
100 retval = (INST *) zrealloc(p->base, oldsize, newsize) ;
106 /* code an op and a pointer in the active_code */
112 register INST *p = code_ptr + 2 ;
125 /* code two ops in the active_code */
130 register INST *p = code_ptr + 2 ;
146 main_code_p = new_code() ;
148 active_code = *main_code_p ;
152 /* final code relocation
153 set_code() as in set concrete */
157 /* set the main code which is active_code */
158 if (end_code_p || code_offset > 1)
160 int gl_offset = code_offset ;
163 if (NR_flag) code2op(OL_GL_NR, _HALT) ;
164 else code2op(OL_GL, _HALT) ;
166 *main_code_p = active_code ;
167 main_start = code_shrink(main_code_p, &main_size) ;
168 next_label = main_start + gl_offset ;
169 execution_start = main_start ;
171 else /* only BEGIN */
173 zfree(code_base, INST_BYTES(PAGESZ)) ;
177 /* set the END code */
182 active_code = *end_code_p ;
183 code2op(_EXIT0, _HALT) ;
184 *end_code_p = active_code ;
185 end_start = code_shrink(end_code_p, &dummy) ;
188 /* set the BEGIN code */
191 active_code = *begin_code_p ;
192 if (main_start) code2op(_JMAIN, _HALT) ;
193 else code2op(_EXIT0, _HALT) ;
194 *begin_code_p = active_code ;
195 begin_start = code_shrink(begin_code_p, &begin_size) ;
197 execution_start = begin_start ;
200 if ( ! execution_start )
202 /* program had functions but no pattern-action bodies */
203 execution_start = begin_start = (INST*) zmalloc(2*sizeof(INST)) ;
204 execution_start[0].op = _EXIT0 ;
205 execution_start[1].op = _HALT ;
212 fdump() ; /* dumps all user functions */
214 { fprintf(stdout, "BEGIN\n") ;
215 da(begin_start, stdout) ; }
217 { fprintf(stdout, "END\n") ;
218 da(end_start, stdout) ; }
220 { fprintf(stdout, "MAIN\n") ;
221 da(main_start, stdout) ; }
228 CODEBLOCK *p = ZMALLOC(CODEBLOCK) ;
230 p->base = (INST *) zmalloc(INST_BYTES(PAGESZ)) ;
231 p->limit = p->base + PAGESZ ;
232 p->warn = p->limit - CODEWARN ;
238 /* moves the active_code from MAIN to a BEGIN or END */
244 *main_code_p = active_code ;
246 if (scope == SCOPE_BEGIN)
248 if (!begin_code_p) begin_code_p = new_code() ;
249 active_code = *begin_code_p ;
253 if (!end_code_p) end_code_p = new_code() ;
254 active_code = *end_code_p ;