| 1 | |
| 2 | /******************************************** |
| 3 | code.c |
| 4 | copyright 1991, Michael D. Brennan |
| 5 | |
| 6 | This is a source file for mawk, an implementation of |
| 7 | the AWK programming language. |
| 8 | |
| 9 | Mawk is distributed without warranty under the terms of |
| 10 | the GNU General Public License, version 2, 1991. |
| 11 | ********************************************/ |
| 12 | |
| 13 | |
| 14 | /* $Log: code.c,v $ |
| 15 | * Revision 5.1 91/12/05 07:55:43 brennan |
| 16 | * 1.1 pre-release |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | /* code.c */ |
| 21 | |
| 22 | #include "mawk.h" |
| 23 | #include "code.h" |
| 24 | #include "init.h" |
| 25 | #include "jmp.h" |
| 26 | #include "field.h" |
| 27 | |
| 28 | |
| 29 | #define MAIN_CODE_SZ (MAIN_PAGE_SZ*sizeof(INST)) |
| 30 | |
| 31 | INST *code_ptr ; |
| 32 | INST *main_start , *main_code_ptr ; |
| 33 | |
| 34 | #if 0 |
| 35 | INST *begin_start , *begin_code_ptr ; |
| 36 | INST *end_start , *end_code_ptr ; |
| 37 | #endif |
| 38 | unsigned main_size /*, begin_size, end_size*/ ; |
| 39 | /* when the code is done executing its freed, |
| 40 | that's why this is global */ |
| 41 | |
| 42 | struct be_code begin_code , end_code ; |
| 43 | |
| 44 | void PROTO(fdump, (void) ) ; |
| 45 | |
| 46 | void be_expand( p ) |
| 47 | struct be_code *p ; |
| 48 | { int delta ; |
| 49 | |
| 50 | main_code_ptr = code_ptr ; |
| 51 | |
| 52 | if ( p->start ) |
| 53 | { |
| 54 | delta = p->ptr - p->start ; |
| 55 | p->start = (INST*) zrealloc(p->start, INST_BYTES(p->size), |
| 56 | INST_BYTES(PAGE_SZ)) ; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | delta = 0 ; |
| 61 | p->start = (INST*) zmalloc(INST_BYTES(PAGE_SZ)) ; |
| 62 | } |
| 63 | |
| 64 | p->size = PAGE_SZ ; |
| 65 | code_ptr = p->start + delta ; |
| 66 | } |
| 67 | |
| 68 | void be_shrink(p) |
| 69 | struct be_code *p ; |
| 70 | { |
| 71 | int delta = code_ptr - p->start ; |
| 72 | |
| 73 | code_ptr = main_code_ptr ; |
| 74 | |
| 75 | if ( delta > p->size ) |
| 76 | overflow( p == & begin_code ? |
| 77 | "BEGIN code" : "END code" , p->size) ; |
| 78 | |
| 79 | p->start = (INST*) zrealloc(p->start, INST_BYTES(p->size) , |
| 80 | INST_BYTES(delta+2)) ; |
| 81 | |
| 82 | p->ptr = p->start + delta ; |
| 83 | p->size = delta + 2 ; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void code_init() |
| 88 | { |
| 89 | code_ptr = main_code_ptr = main_start |
| 90 | = (INST *) zmalloc(MAIN_CODE_SZ) ; |
| 91 | |
| 92 | code1(_OMAIN) ; |
| 93 | } |
| 94 | |
| 95 | void code_cleanup() |
| 96 | { int some_code_flag = 0 ; /* might only have functions */ |
| 97 | |
| 98 | |
| 99 | /* set the END code */ |
| 100 | if ( end_code.start ) |
| 101 | { |
| 102 | end_code.ptr++ -> op = _EXIT0 ; |
| 103 | end_code.ptr++ -> op = _HALT ; |
| 104 | end_code.size = INST_BYTES(end_code.size) ; |
| 105 | } |
| 106 | |
| 107 | /* set the main code */ |
| 108 | if ( end_code.start || code_ptr - main_start > 1 ) |
| 109 | { int gl_offset = code_ptr - main_start ; |
| 110 | extern INST *next_label ; |
| 111 | extern int NR_flag ; |
| 112 | |
| 113 | if ( NR_flag ) code1(OL_GL_NR) ; |
| 114 | else code1(OL_GL) ; |
| 115 | |
| 116 | code1(_HALT) ; |
| 117 | |
| 118 | main_size = code_ptr - main_start ; |
| 119 | if ( main_size > MAIN_PAGE_SZ ) |
| 120 | overflow("MAIN code" , MAIN_PAGE_SZ) ; |
| 121 | main_size *= sizeof(INST) ; |
| 122 | code_ptr = main_start = |
| 123 | (INST*) zrealloc(main_start, MAIN_CODE_SZ, main_size) ; |
| 124 | |
| 125 | next_label = main_start+gl_offset ; |
| 126 | some_code_flag = 1 ; |
| 127 | } |
| 128 | else /* only BEGIN */ |
| 129 | { |
| 130 | zfree(main_start, MAIN_CODE_SZ) ; |
| 131 | main_start = (INST*) 0 ; |
| 132 | } |
| 133 | |
| 134 | /* set the BEGIN code */ |
| 135 | if ( begin_code.start ) |
| 136 | { |
| 137 | some_code_flag = 1 ; |
| 138 | begin_code.ptr++ -> op = main_start ? _JMAIN : _EXIT0 ; |
| 139 | begin_code.ptr++ -> op = _HALT ; |
| 140 | begin_code.size = INST_BYTES(begin_code.size) ; |
| 141 | |
| 142 | /* execution starts at code_ptr */ |
| 143 | code_ptr = begin_code.start ; |
| 144 | } |
| 145 | |
| 146 | #if ! SM_DOS |
| 147 | if ( dump_code ) |
| 148 | { |
| 149 | fdump() ; /* dumps all user functions */ |
| 150 | if ( begin_code.start ) |
| 151 | { fprintf(stderr, "BEGIN\n") ; da(begin_code.start, stderr) ; } |
| 152 | if ( end_code.start ) |
| 153 | { fprintf(stderr, "END\n") ; da(end_code.start, stderr) ; } |
| 154 | if ( main_start ) |
| 155 | { fprintf(stderr, "MAIN\n") ; da(main_start, stderr) ; } |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | if ( some_code_flag == 0 ) mawk_exit(0) ; |
| 160 | } |
| 161 | |