summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/awk/code.c
blob: 2fb4ca537c95c3e8b3c74ae346a3acd064fc7d63 (plain)
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

/********************************************
code.c
copyright 1991, 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 2, 1991.
********************************************/


/* $Log: code.c,v $
/* Revision 1.2  1993/07/02 23:57:08  jtc
/* Updated to mawk 1.1.4
/*
 * Revision 5.1  1991/12/05  07:55:43  brennan
 * 1.1 pre-release
 *
*/

/*  code.c  */

#include "mawk.h"
#include "code.h"
#include "init.h"
#include "jmp.h"
#include "field.h"


#define   MAIN_CODE_SZ  (MAIN_PAGE_SZ*sizeof(INST))

INST *code_ptr  ;
INST *main_start , *main_code_ptr ;

#if 0
INST *begin_start , *begin_code_ptr ;
INST *end_start , *end_code_ptr ;
#endif 
unsigned main_size /*, begin_size, end_size*/ ;
    /* when the code is done executing its freed,
       that's why this is global */

struct be_code begin_code , end_code ;

void  PROTO(fdump, (void) ) ;

void  be_expand( p )
  struct be_code *p ;
{ int delta ;

  main_code_ptr = code_ptr ;

  if ( p->start )
  {
    delta = p->ptr - p->start ;
    p->start = (INST*) zrealloc(p->start, INST_BYTES(p->size),
			INST_BYTES(PAGE_SZ)) ;
  }
  else
  {
    delta = 0 ;
    p->start = (INST*) zmalloc(INST_BYTES(PAGE_SZ)) ;
  }

  p->size = PAGE_SZ ;
  code_ptr = p->start + delta ;
}

void  be_shrink(p)
  struct be_code *p ;
{
  int delta = code_ptr - p->start ;

  code_ptr = main_code_ptr ;

  if ( delta > p->size )
	overflow( p == & begin_code ?
		  "BEGIN code" : "END code" , p->size) ;

  p->start = (INST*) zrealloc(p->start, INST_BYTES(p->size) ,
			INST_BYTES(delta+2)) ;

  p->ptr = p->start + delta ;
  p->size = delta + 2 ;
}
    

void  code_init()
{ 
  code_ptr = main_code_ptr = main_start 
    = (INST *) zmalloc(MAIN_CODE_SZ) ;

  code1(_OMAIN) ; 
}

void code_cleanup()
{ int some_code_flag = 0 ; /* might only have functions */ 


  /* set the END code */
  if ( end_code.start )
  { 
    end_code.ptr++ -> op =  _EXIT0 ;
    end_code.ptr++ -> op =  _HALT  ;
    end_code.size = INST_BYTES(end_code.size) ;
  }

  /* set the main code */
  if ( end_code.start || code_ptr - main_start > 1 )
  { int gl_offset = code_ptr - main_start ;
    extern INST *next_label ;
    extern int NR_flag ;

    if ( NR_flag )  code1(OL_GL_NR) ;
    else code1(OL_GL) ;

    code1(_HALT) ;

    main_size = code_ptr - main_start ;
    if ( main_size > MAIN_PAGE_SZ )
           overflow("MAIN code" , MAIN_PAGE_SZ) ;
    main_size *= sizeof(INST) ;
    code_ptr = main_start =
               (INST*) zrealloc(main_start, MAIN_CODE_SZ, main_size) ;

    next_label = main_start+gl_offset ;
    some_code_flag = 1 ;
  }
  else  /* only BEGIN */
  {
    zfree(main_start, MAIN_CODE_SZ) ;
    main_start = (INST*) 0 ;
  }

  /* set the BEGIN code */
  if ( begin_code.start )
  { 
    some_code_flag = 1 ;
    begin_code.ptr++ -> op = main_start ? _JMAIN : _EXIT0 ;
    begin_code.ptr++ -> op = _HALT ;
    begin_code.size = INST_BYTES(begin_code.size) ;

    /* execution starts at code_ptr */
    code_ptr = begin_code.start ;
  }

#if ! SM_DOS
  if ( dump_code )
  {
    fdump() ; /* dumps all user functions */
    if ( begin_code.start )
    { fprintf(stderr, "BEGIN\n") ; da(begin_code.start, stderr) ; }
    if ( end_code.start )
    { fprintf(stderr, "END\n") ; da(end_code.start, stderr) ; }
    if ( main_start )
    { fprintf(stderr, "MAIN\n") ; da(main_start, stderr) ; }
  }
#endif

  if ( some_code_flag == 0 ) mawk_exit(0) ;
}