summaryrefslogtreecommitdiff
path: root/bc/load.c
blob: 7362e3f0fdf960d52ed4bb1076aac165148d1d31 (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*  This file is part of GNU bc.

    Copyright (C) 1991-1994, 1997, 2006, 2008, 2012-2017 Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License , or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING.  If not, see
    <http://www.gnu.org/licenses>.

    You may contact the author by:
       e-mail:  philnelson@acm.org
      us-mail:  Philip A. Nelson
                Computer Science Department, 9062
                Western Washington University
                Bellingham, WA 98226-9062
       
*************************************************************************/

/* load.c:  This code "loads" code into the code segments. */

#include "bcdefs.h"
#include "proto.h"

/* Load variables. */

program_counter load_adr;
char load_str;
char load_const;

/* Initialize the load sequence. */
void
init_load (void)
{
  clear_func(0);
  load_adr.pc_func = 0;
  load_adr.pc_addr = 0;
  load_str = FALSE;
  load_const = FALSE;
}

/* addbyte adds one BYTE to the current code segment. */
void
addbyte (unsigned char thebyte)
{
  unsigned long prog_addr;
  bc_function *f;
  char *new_body;

  /* If there was an error, don't continue. */
  if (had_error) return;

  /* Calculate the segment and offset. */
  prog_addr = load_adr.pc_addr++;
  f = &functions[load_adr.pc_func];

  if (prog_addr >= f->f_body_size)
    {
      f->f_body_size *= 2;
      new_body = bc_malloc (f->f_body_size);
      memcpy(new_body, f->f_body, f->f_body_size/2);
      free (f->f_body);
      f->f_body = new_body;
    }

  /* Store the thebyte. */
  f->f_body[prog_addr] = (char) (thebyte & 0xff);
  f->f_code_size++;
}


/* Define a label LAB to be the current program counter. */

void
def_label (unsigned long lab)
{
  bc_label_group *temp;
  unsigned long group, offset, func;
    
  /* Get things ready. */
  group = lab >> BC_LABEL_LOG;
  offset = lab % BC_LABEL_GROUP;
  func = load_adr.pc_func;
  
  /* Make sure there is at least one label group. */
  if (functions[func].f_label == NULL)
    {
      functions[func].f_label = bc_malloc (sizeof(bc_label_group));
      functions[func].f_label->l_next = NULL;
    }

  /* Add the label group. */
  temp = functions[func].f_label;
  while (group > 0)
    {
      if (temp->l_next == NULL)
	{
	  temp->l_next = bc_malloc (sizeof(bc_label_group));
	  temp->l_next->l_next = NULL;
	}
      temp = temp->l_next;
      group --;
    }

  /* Define it! */
  temp->l_adrs [offset] = load_adr.pc_addr;
}

/* Several instructions have integers in the code.  They
   are all known to be legal longs.  So, no error code
   is added.  STR is the pointer to the load string and
   must be moved to the last non-digit character. */

long
long_val (const char **str)
{ int  val = 0;
  char neg = FALSE;

  if (**str == '-')
    {
      neg = TRUE;
      (*str)++;
    }
  while (isdigit((int)(**str))) 
    val = val*10 + *(*str)++ - '0';

  if (neg)
    return -val;
  else
    return val;
}


/* load_code loads the CODE into the machine. */

void
load_code (const char *code)
{
  const char *str;
  unsigned long  ap_name;	/* auto or parameter name. */
  unsigned long  label_no;
  unsigned long  vaf_name;	/* variable, array or function number. */
  unsigned long  func;
  static program_counter save_adr;

  /* Initialize. */
  str = code;
   
  /* Scan the code. */
  while (*str != 0)
    {
      /* If there was an error, don't continue. */
      if (had_error) return;

      if (load_str)
	{
	  if (*str == '"') load_str = FALSE;
	  addbyte (*str++);
	}
      else
	if (load_const)
	  {
	    if (*str == '\n') 
	      str++;
	    else
	      {
		if (*str == ':')
		  {
		    load_const = FALSE;
		    addbyte (*str++);
		  }
		else
		  if (*str == '.')
		    addbyte (*str++);
		  else
                    {
		      if (*str > 'F' && (warn_not_std || std_only))
                        {
                          if (std_only)
                            yyerror ("Error in numeric constant");
                          else
                            ct_warn ("Non-standard base in numeric constant");
                        } 
		      if (*str >= 'A')
		        addbyte (*str++ + 10 - 'A');
		      else
		        addbyte (*str++ - '0');
                    }
	      }
	  }
	else
	  {
	    switch (*str)
	      {

	      case '"':	/* Starts a string. */
		load_str = TRUE;
		break;

	      case 'N': /* A label */
		str++;
		label_no = long_val (&str);
		def_label (label_no);
		break;

	      case 'B':  /* Branch to label. */
	      case 'J':  /* Jump to label. */
	      case 'Z':  /* Branch Zero to label. */
		addbyte(*str++);
		label_no = long_val (&str);
		if (label_no > 65535L)
		  {  /* Better message? */
		    fprintf (stderr,"Program too big.\n");
		    bc_exit(1);
		  }
		addbyte ( (char) (label_no & 0xFF));
		addbyte ( (char) (label_no >> 8));
		break;

	      case 'F':  /* A function, get the name and initialize it. */
		str++;
		func = long_val (&str);
		clear_func (func);
#if DEBUG > 2
		printf ("Loading function number %d\n", func);
#endif
		/* get the parameters */
		while (*str++ != '.')
		  {
		    if (*str == '.')
		      {
			str++;
			break;
		      }
		    if (*str == '*')
		      {
			str++;
			ap_name = long_val (&str);
#if DEBUG > 2
			printf ("var parameter number %d\n", ap_name);
#endif
			functions[(int)func].f_params = 
			  nextarg (functions[(int)func].f_params, ap_name,
				   TRUE);
		      }
		    else
		      {
			ap_name = long_val (&str);
#if DEBUG > 2
			printf ("parameter number %d\n", ap_name);
#endif
			functions[(int)func].f_params = 
			  nextarg (functions[(int)func].f_params, ap_name,
				   FALSE);
		      }
		  }

		/* get the auto vars */
		while (*str != '[')
		  {
		    if (*str == ',') str++;
		    ap_name = long_val (&str);
#if DEBUG > 2
		    printf ("auto number %d\n", ap_name);
#endif
		    functions[(int)func].f_autos = 
		      nextarg (functions[(int)func].f_autos, ap_name, FALSE);
		  }
		save_adr = load_adr;
		load_adr.pc_func = func;
		load_adr.pc_addr = 0;
		break;
		
	      case ']':  /* A function end */
		functions[load_adr.pc_func].f_defined = TRUE;
		load_adr = save_adr;
		break;

	      case 'C':  /* Call a function. */
		addbyte (*str++);
		func = long_val (&str);
		if (func < 128)
		  addbyte ( (char) func);
		else
		  {
		    addbyte (((func >> 8) & 0xff) | 0x80);
		    addbyte (func & 0xff);
		  }
		if (*str == ',') str++;
		while (*str != ':')
		  addbyte (*str++);
		addbyte (':');
		break;
		
	      case 'c':  /* Call a special function. */
		addbyte (*str++);
		addbyte (*str);
		break;

	      case 'K':  /* A constant.... may have an "F" in it. */
		addbyte (*str);
		load_const = TRUE;
		break;

	      case 'd':  /* Decrement. */
	      case 'i':  /* Increment. */
	      case 'l':  /* Load. */
	      case 's':  /* Store. */
	      case 'A':  /* Array Increment */
	      case 'M':  /* Array Decrement */
	      case 'L':  /* Array Load */
	      case 'S':  /* Array Store */
		addbyte (*str++);
		vaf_name = long_val (&str);
		if (vaf_name < 128)
		  addbyte (vaf_name);
		else
		  {
		    addbyte (((vaf_name >> 8) & 0xff) | 0x80);
		    addbyte (vaf_name & 0xff);
		  }
		break;

	      case '@':  /* A command! */
		switch (*(++str))
		  {
		  case 'i':
		    init_load ();
		    break;
		  case 'r':
		    execute ();
		    break;
		  } 
		break;

	      case '\n':  /* Ignore the newlines */
		break;
		
	      default:   /* Anything else */
		addbyte (*str);	   
	      }
	    str++;
	  }
    }
}