-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.c
195 lines (162 loc) · 4.39 KB
/
code.c
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
/********************************************
code.c
copyright 1991-93,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
#include "mawk.h"
#include "code.h"
#include "init.h"
#include "field.h"
// static CODEBLOCK * code_create_block( void );
CODEBLOCK active_code;
CODEBLOCK * main_code_p;
CODEBLOCK * begin_code_p;
CODEBLOCK * end_code_p;
INST * begin_start;
INST * main_start;
INST * end_start;
unsigned begin_size;
unsigned main_size;
// INST *execution_start = 0 ;
/* grow the active code */
void
code_grow( void )
{
unsigned oldsize = code_limit - code_base;
unsigned newsize = PAGESZ + oldsize;
unsigned delta = code_ptr - code_base;
if ( code_ptr > code_limit )
bozo( "CODEWARN is too small" );
code_base = (INST *)
zrealloc( code_base, INST_BYTES( oldsize ),
INST_BYTES( newsize ) );
code_limit = code_base + newsize;
code_warn = code_limit - CODEWARN;
code_ptr = code_base + delta;
}
/* shrinks executable code that's done to its final size */
INST *
code_shrink( CODEBLOCK * p, unsigned * sizep )
{
unsigned oldsize = INST_BYTES( p->limit - p->base );
unsigned newsize = INST_BYTES( p->ptr - p->base );
INST * retval;
*sizep = newsize;
retval = (INST *)zrealloc( p->base, oldsize, newsize );
ZFREE( p );
return retval;
}
/* code an op and a pointer in the active_code */
void
xcode2( int op, void * ptr )
{
register INST * p = code_ptr + 2;
if ( p >= code_warn ) {
code_grow();
p = code_ptr + 2;
}
p[-2].op = op;
p[-1].ptr = ptr;
code_ptr = p;
}
/* code two ops in the active_code */
void
code2op( int x, int y )
{
register INST * p = code_ptr + 2;
if ( p >= code_warn ) {
code_grow();
p = code_ptr + 2;
}
p[-2].op = x;
p[-1].op = y;
code_ptr = p;
}
void
code_init( void )
{
main_code_p = code_create_block();
active_code = *main_code_p;
code1( _OMAIN );
}
/* final code relocation
set_code() as in set concrete */
void
set_code( void )
{
/* set the main code which is active_code */
if ( end_code_p || code_offset > 1 ) {
int gl_offset = code_offset;
extern int NR_flag;
if ( NR_flag )
code2op( OL_GL_NR, _HALT );
else
code2op( OL_GL, _HALT );
*main_code_p = active_code;
main_start = code_shrink( main_code_p, &main_size );
next_label = main_start + gl_offset;
execution_start = main_start;
}
else /* only BEGIN */
{
zfree( code_base, INST_BYTES( PAGESZ ) );
ZFREE( main_code_p );
}
/* set the END code */
if ( end_code_p ) {
unsigned dummy;
active_code = *end_code_p;
code2op( _EXIT0, _HALT );
*end_code_p = active_code;
end_start = code_shrink( end_code_p, &dummy );
}
/* set the BEGIN code */
if ( begin_code_p ) {
active_code = *begin_code_p;
if ( main_start )
code2op( _JMAIN, _HALT );
else
code2op( _EXIT0, _HALT );
*begin_code_p = active_code;
begin_start = code_shrink( begin_code_p, &begin_size );
execution_start = begin_start;
}
if ( !execution_start ) {
/* program had functions but no pattern-action bodies */
execution_start = begin_start = (INST *)zmalloc( 2 * sizeof( INST ) );
execution_start[0].op = _EXIT0;
execution_start[1].op = _HALT;
}
}
void
dump_code( void )
{
fdump(); /* dumps all user functions */
if ( begin_start ) {
fprintf( stdout, "BEGIN\n" );
da( begin_start, stdout );
}
if ( end_start ) {
fprintf( stdout, "END\n" );
da( end_start, stdout );
}
if ( main_start ) {
fprintf( stdout, "MAIN\n" );
da( main_start, stdout );
}
}
CODEBLOCK *
code_create_block( void )
{
CODEBLOCK * p = ZMALLOC( CODEBLOCK );
p->base = (INST *)zmalloc( INST_BYTES( PAGESZ ) );
p->limit = p->base + PAGESZ;
p->warn = p->limit - CODEWARN;
p->ptr = p->base;
return p;
}