/* load.c: This code "loads" code into the code segments. */
/* This file is part of bc written for MINIX.
Copyright (C) 1991 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 2 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, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
You may contact the author by:
us-mail: Philip A. Nelson
Computer Science Department, 9062
Western Washington University
Bellingham, WA 98226-9062
*************************************************************************/
program_counter load_adr
;
/* Initialize the load sequence. */
/* addbyte adds one BYTE to the current code segment. */
/* Calculate the segment and offset. */
seg
= load_adr
.pc_addr
>> BC_SEG_LOG
;
offset
= load_adr
.pc_addr
++ % BC_SEG_SIZE
;
if (functions
[func
].f_body
[seg
] == NULL
)
functions
[func
].f_body
[seg
] = (char *) bc_malloc (BC_SEG_SIZE
);
functions
[func
].f_body
[seg
][offset
] = byte
;
functions
[func
].f_code_size
++;
/* Define a label LAB to be the current program counter. */
group
= lab
>> BC_LABEL_LOG
;
offset
= lab
% BC_LABEL_GROUP
;
/* Make sure there is at least one label group. */
if (functions
[func
].f_label
== NULL
)
functions
[func
].f_label
=
(bc_label_group
*) bc_malloc (sizeof(bc_label_group
));
functions
[func
].f_label
->l_next
= NULL
;
/* Add the label group. */
temp
= functions
[func
].f_label
;
if (temp
->l_next
== NULL
)
temp
->l_next
= (bc_label_group
*) bc_malloc (sizeof(bc_label_group
));
temp
->l_next
->l_next
= NULL
;
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. */
val
= val
*10 + *(*str
)++ - '0';
/* load_code loads the CODE into the machine. */
long ap_name
; /* auto or parameter name. */
long vaf_name
; /* variable, array or function number. */
program_counter save_adr
;
if (*str
== '"') load_str
= FALSE
;
addbyte (*str
++ + 10 - 'A');
case '"': /* Starts a string. */
label_no
= long_val (&str
);
case 'B': /* Branch to label. */
case 'J': /* Jump to label. */
case 'Z': /* Branch Zero to label. */
label_no
= long_val (&str
);
fprintf (stderr
,"Program too big.\n");
addbyte ( (char) label_no
& 0xFF);
addbyte ( (char) label_no
>> 8);
case 'F': /* A function, get the name and initialize it. */
printf ("Loading function number %d\n", func
);
ap_name
= long_val (&str
);
printf ("parameter number %d\n", ap_name
);
functions
[(int)func
].f_params
=
nextarg (functions
[(int)func
].f_params
, ap_name
);
ap_name
= long_val (&str
);
printf ("auto number %d\n", ap_name
);
functions
[(int)func
].f_autos
=
nextarg (functions
[(int)func
].f_autos
, ap_name
);
case ']': /* A function end */
functions
[load_adr
.pc_func
].f_defined
= TRUE
;
case 'C': /* Call a function. */
addbyte ((func
>> 8) & 0xff | 0x80);
case 'c': /* Call a special function. */
case 'K': /* A constant.... may have an "F" in it. */
case 'd': /* Decrement. */
case 'i': /* Increment. */
case 'A': /* Array Increment */
case 'M': /* Array Decrement */
case 'L': /* Array Load */
case 'S': /* Array Store */
vaf_name
= long_val (&str
);
addbyte ((vaf_name
>> 8) & 0xff | 0x80);
addbyte (vaf_name
& 0xff);
case '@': /* A command! */
case '\n': /* Ignore the newlines */
default: /* Anything else */