xref: /original-bsd/usr.bin/pascal/src/proc.c (revision 5fb3de76)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)proc.c 1.5 03/08/81";
4 
5 #include "whoami.h"
6 #ifdef OBJ
7     /*
8      *	and the rest of the file
9      */
10 #include "0.h"
11 #include "tree.h"
12 #include "opcode.h"
13 #include "objfmt.h"
14 
15 /*
16  * The following array is used to determine which classes may be read
17  * from textfiles. It is indexed by the return value from classify.
18  */
19 #define rdops(x) rdxxxx[(x)-(TFIRST)]
20 
21 int rdxxxx[] = {
22 	0,		/* -7 file types */
23 	0,		/* -6 record types */
24 	0,		/* -5 array types */
25 	O_READE,	/* -4 scalar types */
26 	0,		/* -3 pointer types */
27 	0,		/* -2 set types */
28 	0,		/* -1 string types */
29 	0,		/*  0 nil, no type */
30 	O_READE,	/*  1 boolean */
31 	O_READC,	/*  2 character */
32 	O_READ4,	/*  3 integer */
33 	O_READ8		/*  4 real */
34 };
35 
36 /*
37  * Proc handles procedure calls.
38  * Non-builtin procedures are "buck-passed" to func (with a flag
39  * indicating that they are actually procedures.
40  * builtin procedures are handled here.
41  */
42 proc(r)
43 	int *r;
44 {
45 	register struct nl *p;
46 	register int *alv, *al, op;
47 	struct nl *filetype, *ap;
48 	int argc, *argv, typ, fmtspec, strfmt, stkcnt, *file;
49 	char fmt, format[20], *strptr;
50 	int prec, field, strnglen, fmtlen, fmtstart, pu;
51 	int *pua, *pui, *puz;
52 	int i, j, k;
53 	int itemwidth;
54 
55 #define	CONPREC 4
56 #define	VARPREC 8
57 #define	CONWIDTH 1
58 #define	VARWIDTH 2
59 #define SKIP 16
60 
61 	/*
62 	 * Verify that the name is
63 	 * defined and is that of a
64 	 * procedure.
65 	 */
66 	p = lookup(r[2]);
67 	if (p == NIL) {
68 		rvlist(r[3]);
69 		return;
70 	}
71 	if (p->class != PROC && p->class != FPROC) {
72 		error("Can't call %s, its %s not a procedure", p->symbol, classes[p->class]);
73 		rvlist(r[3]);
74 		return;
75 	}
76 	argv = r[3];
77 
78 	/*
79 	 * Call handles user defined
80 	 * procedures and functions.
81 	 */
82 	if (bn != 0) {
83 		call(p, argv, PROC, bn);
84 		return;
85 	}
86 
87 	/*
88 	 * Call to built-in procedure.
89 	 * Count the arguments.
90 	 */
91 	argc = 0;
92 	for (al = argv; al != NIL; al = al[2])
93 		argc++;
94 
95 	/*
96 	 * Switch on the operator
97 	 * associated with the built-in
98 	 * procedure in the namelist
99 	 */
100 	op = p->value[0] &~ NSTAND;
101 	if (opt('s') && (p->value[0] & NSTAND)) {
102 		standard();
103 		error("%s is a nonstandard procedure", p->symbol);
104 	}
105 	switch (op) {
106 
107 	case O_ABORT:
108 		if (argc != 0)
109 			error("null takes no arguments");
110 		return;
111 
112 	case O_FLUSH:
113 		if (argc == 0) {
114 			put(1, O_MESSAGE);
115 			return;
116 		}
117 		if (argc != 1) {
118 			error("flush takes at most one argument");
119 			return;
120 		}
121 		ap = stklval(argv[1], NIL , LREQ );
122 		if (ap == NIL)
123 			return;
124 		if (ap->class != FILET) {
125 			error("flush's argument must be a file, not %s", nameof(ap));
126 			return;
127 		}
128 		put(1, op);
129 		return;
130 
131 	case O_MESSAGE:
132 	case O_WRITEF:
133 	case O_WRITLN:
134 		/*
135 		 * Set up default file "output"'s type
136 		 */
137 		file = NIL;
138 		filetype = nl+T1CHAR;
139 		/*
140 		 * Determine the file implied
141 		 * for the write and generate
142 		 * code to make it the active file.
143 		 */
144 		if (op == O_MESSAGE) {
145 			/*
146 			 * For message, all that matters
147 			 * is that the filetype is
148 			 * a character file.
149 			 * Thus "output" will suit us fine.
150 			 */
151 			put(1, O_MESSAGE);
152 		} else if (argv != NIL && (al = argv[1])[0] != T_WEXP) {
153 			/*
154 			 * If there is a first argument which has
155 			 * no write widths, then it is potentially
156 			 * a file name.
157 			 */
158 			codeoff();
159 			ap = stkrval(argv[1], NIL , RREQ );
160 			codeon();
161 			if (ap == NIL)
162 				argv = argv[2];
163 			if (ap != NIL && ap->class == FILET) {
164 				/*
165 				 * Got "write(f, ...", make
166 				 * f the active file, and save
167 				 * it and its type for use in
168 				 * processing the rest of the
169 				 * arguments to write.
170 				 */
171 				file = argv[1];
172 				filetype = ap->type;
173 				stklval(argv[1], NIL , LREQ );
174 				put(1, O_UNIT);
175 				/*
176 				 * Skip over the first argument
177 				 */
178 				argv = argv[2];
179 				argc--;
180 			} else
181 				/*
182 				 * Set up for writing on
183 				 * standard output.
184 				 */
185 				put(1, O_UNITOUT);
186 		} else
187 			put(1, O_UNITOUT);
188 		/*
189 		 * Loop and process each
190 		 * of the arguments.
191 		 */
192 		for (; argv != NIL; argv = argv[2]) {
193 			/*
194 			 * fmtspec indicates the type (CONstant or VARiable)
195 			 *	and number (none, WIDTH, and/or PRECision)
196 			 *	of the fields in the printf format for this
197 			 *	output variable.
198 			 * stkcnt is the number of longs pushed on the stack
199 			 * fmt is the format output indicator (D, E, F, O, X, S)
200 			 * fmtstart = 0 for leading blank; = 1 for no blank
201 			 */
202 			fmtspec = NIL;
203 			stkcnt = 0;
204 			fmt = 'D';
205 			fmtstart = 1;
206 			al = argv[1];
207 			if (al == NIL)
208 				continue;
209 			if (al[0] == T_WEXP)
210 				alv = al[1];
211 			else
212 				alv = al;
213 			if (alv == NIL)
214 				continue;
215 			codeoff();
216 			ap = stkrval(alv, NIL , RREQ );
217 			codeon();
218 			if (ap == NIL)
219 				continue;
220 			typ = classify(ap);
221 			if (al[0] == T_WEXP) {
222 				/*
223 				 * Handle width expressions.
224 				 * The basic game here is that width
225 				 * expressions get evaluated. If they
226 				 * are constant, the value is placed
227 				 * directly in the format string.
228 				 * Otherwise the value is pushed onto
229 				 * the stack and an indirection is
230 				 * put into the format string.
231 				 */
232 				if (al[3] == OCT)
233 					fmt = 'O';
234 				else if (al[3] == HEX)
235 					fmt = 'X';
236 				else if (al[3] != NIL) {
237 					/*
238 					 * Evaluate second format spec
239 					 */
240 					if ( constval(al[3])
241 					    && isa( con.ctype , "i" ) ) {
242 						fmtspec += CONPREC;
243 						prec = con.crval;
244 					} else {
245 						fmtspec += VARPREC;
246 					}
247 					fmt = 'f';
248 					switch ( typ ) {
249 					case TINT:
250 						if ( opt( 's' ) ) {
251 						    standard();
252 						    error("Writing %ss with two write widths is non-standard", clnames[typ]);
253 						}
254 						/* and fall through */
255 					case TDOUBLE:
256 						break;
257 					default:
258 						error("Cannot write %ss with two write widths", clnames[typ]);
259 						continue;
260 					}
261 				}
262 				/*
263 				 * Evaluate first format spec
264 				 */
265 				if (al[2] != NIL) {
266 					if ( constval(al[2])
267 					    && isa( con.ctype , "i" ) ) {
268 						fmtspec += CONWIDTH;
269 						field = con.crval;
270 					} else {
271 						fmtspec += VARWIDTH;
272 					}
273 				}
274 				if ((fmtspec & CONPREC) && prec < 0 ||
275 				    (fmtspec & CONWIDTH) && field < 0) {
276 					error("Negative widths are not allowed");
277 					continue;
278 				}
279 			}
280 			if (filetype != nl+T1CHAR) {
281 				if (fmt == 'O' || fmt == 'X') {
282 					error("Oct/hex allowed only on text files");
283 					continue;
284 				}
285 				if (fmtspec) {
286 					error("Write widths allowed only on text files");
287 					continue;
288 				}
289 				/*
290 				 * Generalized write, i.e.
291 				 * to a non-textfile.
292 				 */
293 				stklval(file, NIL , LREQ );
294 				put(1, O_FNIL);
295 				/*
296 				 * file^ := ...
297 				 */
298 				ap = rvalue(argv[1], NIL);
299 				if (ap == NIL)
300 					continue;
301 				if (incompat(ap, filetype, argv[1])) {
302 					cerror("Type mismatch in write to non-text file");
303 					continue;
304 				}
305 				convert(ap, filetype);
306 				put(2, O_AS, width(filetype));
307 				/*
308 				 * put(file)
309 				 */
310 				put(1, O_PUT);
311 				continue;
312 			}
313 			/*
314 			 * Write to a textfile
315 			 *
316 			 * Evaluate the expression
317 			 * to be written.
318 			 */
319 			if (fmt == 'O' || fmt == 'X') {
320 				if (opt('s')) {
321 					standard();
322 					error("Oct and hex are non-standard");
323 				}
324 				if (typ == TSTR || typ == TDOUBLE) {
325 					error("Can't write %ss with oct/hex", clnames[typ]);
326 					continue;
327 				}
328 				if (typ == TCHAR || typ == TBOOL)
329 					typ = TINT;
330 			}
331 			/*
332 			 * Place the arguement on the stack. If there is
333 			 * no format specified by the programmer, implement
334 			 * the default.
335 			 */
336 			switch (typ) {
337 			case TINT:
338 				if (fmt != 'f') {
339 					ap = stkrval(alv, NIL , RREQ );
340 					stkcnt++;
341 				} else {
342 					ap = stkrval(alv, NIL , RREQ );
343 					put(1, O_ITOD);
344 					stkcnt += 2;
345 					typ = TDOUBLE;
346 					goto tdouble;
347 				}
348 				if (fmtspec == NIL) {
349 					if (fmt == 'D')
350 						field = 10;
351 					else if (fmt == 'X')
352 						field = 8;
353 					else if (fmt == 'O')
354 						field = 11;
355 					else
356 						panic("fmt1");
357 					fmtspec = CONWIDTH;
358 				}
359 				break;
360 			case TCHAR:
361 			     tchar:
362 				if (fmtspec == NIL) {
363 					put(1, O_FILE);
364 					ap = stkrval(alv, NIL , RREQ );
365 					put(1, O_WRITEC);
366 					fmtspec = SKIP;
367 					break;
368 				}
369 				ap = stkrval(alv, NIL , RREQ );
370 				stkcnt++;
371 				fmt = 'c';
372 				break;
373 			case TSCAL:
374 				warning();
375 				if (opt('s')) {
376 					standard();
377 				}
378 				error("Writing scalars to text files is non-standard");
379 			case TBOOL:
380 				stkrval(alv, NIL , RREQ );
381 				put(2, O_NAM, (long)listnames(ap));
382 #				ifdef PDP11
383 					put(2, O_CON2, 0);  /* long align ptr */
384 #				endif PDP11
385 				stkcnt++;
386 				fmt = 's';
387 				break;
388 			case TDOUBLE:
389 				ap = stkrval(alv, TDOUBLE , RREQ );
390 				stkcnt += 2;
391 			     tdouble:
392 				switch (fmtspec) {
393 				case NIL:
394 #					ifdef DEC11
395 					    field = 21;
396 #					else
397 					    field = 22;
398 #					endif DEC11
399 					prec = 14;
400 					fmt = 'e';
401 					fmtspec = CONWIDTH + CONPREC;
402 					break;
403 				case CONWIDTH:
404 					if (--field < 1)
405 						field = 1;
406 #					ifdef DEC11
407 					    prec = field - 7;
408 #					else
409 					    prec = field - 8;
410 #					endif DEC11
411 					if (prec < 1)
412 						prec = 1;
413 					fmtspec += CONPREC;
414 					fmt = 'e';
415 					break;
416 				case CONWIDTH + CONPREC:
417 				case CONWIDTH + VARPREC:
418 					if (--field < 1)
419 						field = 1;
420 				}
421 				format[0] = ' ';
422 				fmtstart = 0;
423 				break;
424 			case TSTR:
425 				constval( alv );
426 				switch ( classify( con.ctype ) ) {
427 				    case TCHAR:
428 					typ = TCHAR;
429 					goto tchar;
430 				    case TSTR:
431 					strptr = con.cpval;
432 					for (strnglen = 0;  *strptr++;  strnglen++) /* void */;
433 					strptr = con.cpval;
434 					break;
435 				    default:
436 					strnglen = width(ap);
437 					break;
438 				}
439 				fmt = 's';
440 				strfmt = fmtspec;
441 				if (fmtspec == NIL) {
442 					fmtspec = SKIP;
443 					break;
444 				}
445 				if (fmtspec & CONWIDTH) {
446 					if (field <= strnglen) {
447 						fmtspec = SKIP;
448 						break;
449 					} else
450 						field -= strnglen;
451 				}
452 				/*
453 				 * push string to implement leading blank padding
454 				 */
455 				put(2, O_LVCON, 2);
456 				putstr("", 0);
457 #				ifdef PDP11
458 					put(2, O_CON2, 0);  /* long align ptr */
459 #				endif PDP11
460 				stkcnt++;
461 				break;
462 			default:
463 				error("Can't write %ss to a text file", clnames[typ]);
464 				continue;
465 			}
466 			/*
467 			 * If there is a variable precision, evaluate it onto
468 			 * the stack
469 			 */
470 			if (fmtspec & VARPREC) {
471 				ap = stkrval(al[3], NIL , RREQ );
472 				if (ap == NIL)
473 					continue;
474 				if (isnta(ap,"i")) {
475 					error("Second write width must be integer, not %s", nameof(ap));
476 					continue;
477 				}
478 				if ( opt( 't' ) ) {
479 				    put(3, O_MAX, 0, 0);
480 				}
481 				stkcnt++;
482 			}
483 			/*
484 			 * If there is a variable width, evaluate it onto
485 			 * the stack
486 			 */
487 			if (fmtspec & VARWIDTH) {
488 				if ( ( typ == TDOUBLE && fmtspec == VARWIDTH )
489 				    || typ == TSTR ) {
490 					i = sizes[cbn].om_off -= sizeof(long);
491 					if (i < sizes[cbn].om_max)
492 						sizes[cbn].om_max = i;
493 					put(2, O_LV | cbn << 8 + INDX, i);
494 				}
495 				ap = stkrval(al[2], NIL , RREQ );
496 				if (ap == NIL)
497 					continue;
498 				if (isnta(ap,"i")) {
499 					error("First write width must be integer, not %s", nameof(ap));
500 					continue;
501 				}
502 				stkcnt++;
503 				/*
504 				 * Perform special processing on widths based
505 				 * on data type
506 				 */
507 				switch (typ) {
508 				case TDOUBLE:
509 					if (fmtspec == VARWIDTH) {
510 						fmt = 'e';
511 						put(1, O_AS4);
512 						put(2, O_RV4 | cbn << 8 + INDX, i);
513 #						ifdef DEC11
514 						    put(3, O_MAX, 8, 1);
515 #						else
516 						    put(3, O_MAX, 9, 1);
517 #						endif DEC11
518 						put(2, O_RV4 | cbn << 8 + INDX, i);
519 						stkcnt++;
520 						fmtspec += VARPREC;
521 					}
522 					put(3, O_MAX, 1, 1);
523 					break;
524 				case TSTR:
525 					put(1, O_AS4);
526 					put(2, O_RV4 | cbn << 8 + INDX, i);
527 					put(3, O_MAX, strnglen, 0);
528 					break;
529 				default:
530 					if ( opt( 't' ) ) {
531 					    put(3, O_MAX, 0, 0);
532 					}
533 					break;
534 				}
535 			}
536 			/*
537 			 * Generate the format string
538 			 */
539 			switch (fmtspec) {
540 			default:
541 				panic("fmt2");
542 			case SKIP:
543 				break;
544 			case NIL:
545 				sprintf(&format[1], "%%%c", fmt);
546 				goto fmtgen;
547 			case CONWIDTH:
548 				sprintf(&format[1], "%%%d%c", field, fmt);
549 				goto fmtgen;
550 			case VARWIDTH:
551 				sprintf(&format[1], "%%*%c", fmt);
552 				goto fmtgen;
553 			case CONWIDTH + CONPREC:
554 				sprintf(&format[1], "%%%d.%d%c", field, prec, fmt);
555 				goto fmtgen;
556 			case CONWIDTH + VARPREC:
557 				sprintf(&format[1], "%%%d.*%c", field, fmt);
558 				goto fmtgen;
559 			case VARWIDTH + CONPREC:
560 				sprintf(&format[1], "%%*.%d%c", prec, fmt);
561 				goto fmtgen;
562 			case VARWIDTH + VARPREC:
563 				sprintf(&format[1], "%%*.*%c", fmt);
564 			fmtgen:
565 				fmtlen = lenstr(&format[fmtstart], 0);
566 				put(2, O_LVCON, fmtlen);
567 				putstr(&format[fmtstart], 0);
568 				put(1, O_FILE);
569 				stkcnt += 2;
570 				put(2, O_WRITEF, stkcnt);
571 			}
572 			/*
573 			 * Write the string after its blank padding
574 			 */
575 			if (typ == TSTR) {
576 				put(1, O_FILE);
577 				put(2, O_CON24, 1);
578 				if (strfmt & VARWIDTH) {
579 					put(2, O_RV4 | cbn << 8 + INDX , i );
580 					put(2, O_MIN, strnglen);
581 				} else {
582 					if ((fmtspec & SKIP) &&
583 					   (strfmt & CONWIDTH)) {
584 						strnglen = field;
585 					}
586 					put(2, O_CON24, strnglen);
587 				}
588 				ap = stkrval(alv, NIL , RREQ );
589 				put(1, O_WRITES);
590 			}
591 		}
592 		/*
593 		 * Done with arguments.
594 		 * Handle writeln and
595 		 * insufficent number of args.
596 		 */
597 		switch (p->value[0] &~ NSTAND) {
598 			case O_WRITEF:
599 				if (argc == 0)
600 					error("Write requires an argument");
601 				break;
602 			case O_MESSAGE:
603 				if (argc == 0)
604 					error("Message requires an argument");
605 			case O_WRITLN:
606 				if (filetype != nl+T1CHAR)
607 					error("Can't 'writeln' a non text file");
608 				put(1, O_WRITLN);
609 				break;
610 		}
611 		return;
612 
613 	case O_READ4:
614 	case O_READLN:
615 		/*
616 		 * Set up default
617 		 * file "input".
618 		 */
619 		file = NIL;
620 		filetype = nl+T1CHAR;
621 		/*
622 		 * Determine the file implied
623 		 * for the read and generate
624 		 * code to make it the active file.
625 		 */
626 		if (argv != NIL) {
627 			codeoff();
628 			ap = stkrval(argv[1], NIL , RREQ );
629 			codeon();
630 			if (ap == NIL)
631 				argv = argv[2];
632 			if (ap != NIL && ap->class == FILET) {
633 				/*
634 				 * Got "read(f, ...", make
635 				 * f the active file, and save
636 				 * it and its type for use in
637 				 * processing the rest of the
638 				 * arguments to read.
639 				 */
640 				file = argv[1];
641 				filetype = ap->type;
642 				stklval(argv[1], NIL , LREQ );
643 				put(1, O_UNIT);
644 				argv = argv[2];
645 				argc--;
646 			} else {
647 				/*
648 				 * Default is read from
649 				 * standard input.
650 				 */
651 				put(1, O_UNITINP);
652 				input->nl_flags |= NUSED;
653 			}
654 		} else {
655 			put(1, O_UNITINP);
656 			input->nl_flags |= NUSED;
657 		}
658 		/*
659 		 * Loop and process each
660 		 * of the arguments.
661 		 */
662 		for (; argv != NIL; argv = argv[2]) {
663 			/*
664 			 * Get the address of the target
665 			 * on the stack.
666 			 */
667 			al = argv[1];
668 			if (al == NIL)
669 				continue;
670 			if (al[0] != T_VAR) {
671 				error("Arguments to %s must be variables, not expressions", p->symbol);
672 				continue;
673 			}
674 			ap = stklval(al, MOD|ASGN|NOUSE);
675 			if (ap == NIL)
676 				continue;
677 			if (filetype != nl+T1CHAR) {
678 				/*
679 				 * Generalized read, i.e.
680 				 * from a non-textfile.
681 				 */
682 				if (incompat(filetype, ap, argv[1] )) {
683 					error("Type mismatch in read from non-text file");
684 					continue;
685 				}
686 				/*
687 				 * var := file ^;
688 				 */
689 				if (file != NIL)
690 					stklval(file, NIL , LREQ );
691 				else /* Magic */
692 					put(2, PTR_RV, (int)input->value[0]);
693 				put(1, O_FNIL);
694 				put(2, O_IND, width(filetype));
695 				convert(filetype, ap);
696 				if (isa(ap, "bsci"))
697 					rangechk(ap, ap);
698 				put(2, O_AS, width(ap));
699 				/*
700 				 * get(file);
701 				 */
702 				put(1, O_GET);
703 				continue;
704 			}
705 			typ = classify(ap);
706 			op = rdops(typ);
707 			if (op == NIL) {
708 				error("Can't read %ss from a text file", clnames[typ]);
709 				continue;
710 			}
711 			if (op != O_READE)
712 				put(1, op);
713 			else {
714 				put(2, op, (long)listnames(ap));
715 				warning();
716 				if (opt('s')) {
717 					standard();
718 				}
719 				error("Reading scalars from text files is non-standard");
720 			}
721 			/*
722 			 * Data read is on the stack.
723 			 * Assign it.
724 			 */
725 			if (op != O_READ8 && op != O_READE)
726 				rangechk(ap, op == O_READC ? ap : nl+T4INT);
727 			gen(O_AS2, O_AS2, width(ap),
728 				op == O_READ8 ? 8 : op == O_READ4 ? 4 : 2);
729 		}
730 		/*
731 		 * Done with arguments.
732 		 * Handle readln and
733 		 * insufficient number of args.
734 		 */
735 		if (p->value[0] == O_READLN) {
736 			if (filetype != nl+T1CHAR)
737 				error("Can't 'readln' a non text file");
738 			put(1, O_READLN);
739 		}
740 		else if (argc == 0)
741 			error("read requires an argument");
742 		return;
743 
744 	case O_GET:
745 	case O_PUT:
746 		if (argc != 1) {
747 			error("%s expects one argument", p->symbol);
748 			return;
749 		}
750 		ap = stklval(argv[1], NIL , LREQ );
751 		if (ap == NIL)
752 			return;
753 		if (ap->class != FILET) {
754 			error("Argument to %s must be a file, not %s", p->symbol, nameof(ap));
755 			return;
756 		}
757 		put(1, O_UNIT);
758 		put(1, op);
759 		return;
760 
761 	case O_RESET:
762 	case O_REWRITE:
763 		if (argc == 0 || argc > 2) {
764 			error("%s expects one or two arguments", p->symbol);
765 			return;
766 		}
767 		if (opt('s') && argc == 2) {
768 			standard();
769 			error("Two argument forms of reset and rewrite are non-standard");
770 		}
771 		codeoff();
772 		ap = stklval(argv[1], MOD|NOUSE);
773 		codeon();
774 		if (ap == NIL)
775 			return;
776 		if (ap->class != FILET) {
777 			error("First argument to %s must be a file, not %s", p->symbol, nameof(ap));
778 			return;
779 		}
780 		put(2, O_CON24, text(ap) ? 0: width(ap->type));
781 		if (argc == 2) {
782 			/*
783 			 * Optional second argument
784 			 * is a string name of a
785 			 * UNIX (R) file to be associated.
786 			 */
787 			al = argv[2];
788 			codeoff();
789 			al = stkrval(al[1], NOFLAGS , RREQ );
790 			codeon();
791 			if (al == NIL)
792 				return;
793 			if (classify(al) != TSTR) {
794 				error("Second argument to %s must be a string, not %s", p->symbol, nameof(al));
795 				return;
796 			}
797 			put(2, O_CON24, width(al));
798 			al = argv[2];
799 			al = stkrval(al[1], NOFLAGS , RREQ );
800 		} else {
801 			put(2, O_CON24, 0);
802 			put(2, PTR_CON, NIL);
803 		}
804 		ap = stklval(argv[1], MOD|NOUSE);
805 		put(1, op);
806 		return;
807 
808 	case O_NEW:
809 	case O_DISPOSE:
810 		if (argc == 0) {
811 			error("%s expects at least one argument", p->symbol);
812 			return;
813 		}
814 		ap = stklval(argv[1], op == O_NEW ? ( MOD | NOUSE ) : MOD );
815 		if (ap == NIL)
816 			return;
817 		if (ap->class != PTR) {
818 			error("(First) argument to %s must be a pointer, not %s", p->symbol, nameof(ap));
819 			return;
820 		}
821 		ap = ap->type;
822 		if (ap == NIL)
823 			return;
824 		argv = argv[2];
825 		if (argv != NIL) {
826 			if (ap->class != RECORD) {
827 				error("Record required when specifying variant tags");
828 				return;
829 			}
830 			for (; argv != NIL; argv = argv[2]) {
831 				if (ap->ptr[NL_VARNT] == NIL) {
832 					error("Too many tag fields");
833 					return;
834 				}
835 				if (!isconst(argv[1])) {
836 					error("Second and successive arguments to %s must be constants", p->symbol);
837 					return;
838 				}
839 				gconst(argv[1]);
840 				if (con.ctype == NIL)
841 					return;
842 				if (incompat(con.ctype, (ap->ptr[NL_TAG])->type , NIL )) {
843 					cerror("Specified tag constant type clashed with variant case selector type");
844 					return;
845 				}
846 				for (ap = ap->ptr[NL_VARNT]; ap != NIL; ap = ap->chain)
847 					if (ap->range[0] == con.crval)
848 						break;
849 				if (ap == NIL) {
850 					error("No variant case label value equals specified constant value");
851 					return;
852 				}
853 				ap = ap->ptr[NL_VTOREC];
854 			}
855 		}
856 		put(2, op, width(ap));
857 		return;
858 
859 	case O_DATE:
860 	case O_TIME:
861 		if (argc != 1) {
862 			error("%s expects one argument", p->symbol);
863 			return;
864 		}
865 		ap = stklval(argv[1], MOD|NOUSE);
866 		if (ap == NIL)
867 			return;
868 		if (classify(ap) != TSTR || width(ap) != 10) {
869 			error("Argument to %s must be a alfa, not %s", p->symbol, nameof(ap));
870 			return;
871 		}
872 		put(1, op);
873 		return;
874 
875 	case O_HALT:
876 		if (argc != 0) {
877 			error("halt takes no arguments");
878 			return;
879 		}
880 		put(1, op);
881 		noreach = 1;
882 		return;
883 
884 	case O_ARGV:
885 		if (argc != 2) {
886 			error("argv takes two arguments");
887 			return;
888 		}
889 		ap = stkrval(argv[1], NIL , RREQ );
890 		if (ap == NIL)
891 			return;
892 		if (isnta(ap, "i")) {
893 			error("argv's first argument must be an integer, not %s", nameof(ap));
894 			return;
895 		}
896 		al = argv[2];
897 		ap = stklval(al[1], MOD|NOUSE);
898 		if (ap == NIL)
899 			return;
900 		if (classify(ap) != TSTR) {
901 			error("argv's second argument must be a string, not %s", nameof(ap));
902 			return;
903 		}
904 		put(2, op, width(ap));
905 		return;
906 
907 	case O_STLIM:
908 		if (argc != 1) {
909 			error("stlimit requires one argument");
910 			return;
911 		}
912 		ap = stkrval(argv[1], NIL , RREQ );
913 		if (ap == NIL)
914 			return;
915 		if (isnta(ap, "i")) {
916 			error("stlimit's argument must be an integer, not %s", nameof(ap));
917 			return;
918 		}
919 		if (width(ap) != 4)
920 			put(1, O_STOI);
921 		put(1, op);
922 		return;
923 
924 	case O_REMOVE:
925 		if (argc != 1) {
926 			error("remove expects one argument");
927 			return;
928 		}
929 		codeoff();
930 		ap = stkrval(argv[1], NOFLAGS , RREQ );
931 		codeon();
932 		if (ap == NIL)
933 			return;
934 		if (classify(ap) != TSTR) {
935 			error("remove's argument must be a string, not %s", nameof(ap));
936 			return;
937 		}
938 		put(2, O_CON24, width(ap));
939 		ap = stkrval(argv[1], NOFLAGS , RREQ );
940 		put(1, op);
941 		return;
942 
943 	case O_LLIMIT:
944 		if (argc != 2) {
945 			error("linelimit expects two arguments");
946 			return;
947 		}
948 		al = argv[2];
949 		ap = stkrval(al[1], NIL , RREQ );
950 		if (ap == NIL)
951 			return;
952 		if (isnta(ap, "i")) {
953 			error("linelimit's second argument must be an integer, not %s", nameof(ap));
954 			return;
955 		}
956 		ap = stklval(argv[1], NOFLAGS|NOUSE);
957 		if (ap == NIL)
958 			return;
959 		if (!text(ap)) {
960 			error("linelimit's first argument must be a text file, not %s", nameof(ap));
961 			return;
962 		}
963 		put(1, op);
964 		return;
965 	case O_PAGE:
966 		if (argc != 1) {
967 			error("page expects one argument");
968 			return;
969 		}
970 		ap = stklval(argv[1], NIL , LREQ );
971 		if (ap == NIL)
972 			return;
973 		if (!text(ap)) {
974 			error("Argument to page must be a text file, not %s", nameof(ap));
975 			return;
976 		}
977 		put(1, O_UNIT);
978 		put(1, op);
979 		return;
980 
981 	case O_PACK:
982 		if (argc != 3) {
983 			error("pack expects three arguments");
984 			return;
985 		}
986 		pu = "pack(a,i,z)";
987 		pua = argv[1];
988 		al = argv[2];
989 		pui = al[1];
990 		alv = al[2];
991 		puz = alv[1];
992 		goto packunp;
993 	case O_UNPACK:
994 		if (argc != 3) {
995 			error("unpack expects three arguments");
996 			return;
997 		}
998 		pu = "unpack(z,a,i)";
999 		puz = argv[1];
1000 		al = argv[2];
1001 		pua = al[1];
1002 		alv = al[2];
1003 		pui = alv[1];
1004 packunp:
1005 		codeoff();
1006 		ap = stklval(pua, op == O_PACK ? NOFLAGS : MOD|NOUSE);
1007 		al = (struct nl *) stklval(puz, op == O_UNPACK ? NOFLAGS : MOD|NOUSE);
1008 		codeon();
1009 		if (ap == NIL)
1010 			return;
1011 		if (ap->class != ARRAY) {
1012 			error("%s requires a to be an unpacked array, not %s", pu, nameof(ap));
1013 			return;
1014 		}
1015 		if (al->class != ARRAY) {
1016 			error("%s requires z to be a packed array, not %s", pu, nameof(ap));
1017 			return;
1018 		}
1019 		if (al->type == NIL || ap->type == NIL)
1020 			return;
1021 		if (al->type != ap->type) {
1022 			error("%s requires a and z to be arrays of the same type", pu, nameof(ap));
1023 			return;
1024 		}
1025 		k = width(al);
1026 		itemwidth = width(ap->type);
1027 		ap = ap->chain;
1028 		al = al->chain;
1029 		if (ap->chain != NIL || al->chain != NIL) {
1030 			error("%s requires a and z to be single dimension arrays", pu);
1031 			return;
1032 		}
1033 		if (ap == NIL || al == NIL)
1034 			return;
1035 		/*
1036 		 * al is the range for z i.e. u..v
1037 		 * ap is the range for a i.e. m..n
1038 		 * i will be n-m+1
1039 		 * j will be v-u+1
1040 		 */
1041 		i = ap->range[1] - ap->range[0] + 1;
1042 		j = al->range[1] - al->range[0] + 1;
1043 		if (i < j) {
1044 			error("%s cannot have more elements in a (%d) than in z (%d)", pu, j, i);
1045 			return;
1046 		}
1047 		/*
1048 		 * get n-m-(v-u) and m for the interpreter
1049 		 */
1050 		i -= j;
1051 		j = ap->range[0];
1052 		put(2, O_CON24, k);
1053 		put(2, O_CON24, i);
1054 		put(2, O_CON24, j);
1055 		put(2, O_CON24, itemwidth);
1056 		al = (struct nl *) stklval(puz, op == O_UNPACK ? NOFLAGS : MOD|NOUSE);
1057 		ap = stklval(pua, op == O_PACK ? NOFLAGS : MOD|NOUSE);
1058 		ap = stkrval((int *) pui, NLNIL , RREQ );
1059 		if (ap == NIL)
1060 			return;
1061 		put(1, op);
1062 		return;
1063 	case 0:
1064 		error("%s is an unimplemented 6400 extension", p->symbol);
1065 		return;
1066 
1067 	default:
1068 		panic("proc case");
1069 	}
1070 }
1071 #endif OBJ
1072