]> git.saurik.com Git - apple/boot.git/blob - i386/nasm/nasmlib.c
boot-83.2.tar.gz
[apple/boot.git] / i386 / nasm / nasmlib.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* nasmlib.c library routines for the Netwide Assembler
25 *
26 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
27 * Julian Hall. All rights reserved. The software is
28 * redistributable under the licence given in the file "Licence"
29 * distributed in the NASM archive.
30 */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36
37 #include "nasm.h"
38 #include "nasmlib.h"
39
40 static efunc nasm_malloc_error;
41
42 #ifdef LOGALLOC
43 static FILE *logfp;
44 #endif
45
46 void nasm_set_malloc_error (efunc error) {
47 nasm_malloc_error = error;
48 #ifdef LOGALLOC
49 logfp = fopen ("malloc.log", "w");
50 setvbuf (logfp, NULL, _IOLBF, BUFSIZ);
51 fprintf (logfp, "null pointer is %p\n", NULL);
52 #endif
53 }
54
55 #ifdef LOGALLOC
56 void *nasm_malloc_log (char *file, int line, size_t size)
57 #else
58 void *nasm_malloc (size_t size)
59 #endif
60 {
61 void *p = malloc(size);
62 if (!p)
63 nasm_malloc_error (ERR_FATAL | ERR_NOFILE, "out of memory");
64 #ifdef LOGALLOC
65 else
66 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
67 file, line, (long)size, p);
68 #endif
69 return p;
70 }
71
72 #ifdef LOGALLOC
73 void *nasm_realloc_log (char *file, int line, void *q, size_t size)
74 #else
75 void *nasm_realloc (void *q, size_t size)
76 #endif
77 {
78 void *p = q ? realloc(q, size) : malloc(size);
79 if (!p)
80 nasm_malloc_error (ERR_FATAL | ERR_NOFILE, "out of memory");
81 #ifdef LOGALLOC
82 else if (q)
83 fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n",
84 file, line, q, (long)size, p);
85 else
86 fprintf(logfp, "%s %d malloc(%ld) returns %p\n",
87 file, line, (long)size, p);
88 #endif
89 return p;
90 }
91
92 #ifdef LOGALLOC
93 void nasm_free_log (char *file, int line, void *q)
94 #else
95 void nasm_free (void *q)
96 #endif
97 {
98 if (q) {
99 free (q);
100 #ifdef LOGALLOC
101 fprintf(logfp, "%s %d free(%p)\n",
102 file, line, q);
103 #endif
104 }
105 }
106
107 #ifdef LOGALLOC
108 char *nasm_strdup_log (char *file, int line, char *s)
109 #else
110 char *nasm_strdup (char *s)
111 #endif
112 {
113 char *p;
114 int size = strlen(s)+1;
115
116 p = malloc(size);
117 if (!p)
118 nasm_malloc_error (ERR_FATAL | ERR_NOFILE, "out of memory");
119 #ifdef LOGALLOC
120 else
121 fprintf(logfp, "%s %d strdup(%ld) returns %p\n",
122 file, line, (long)size, p);
123 #endif
124 strcpy (p, s);
125 return p;
126 }
127
128 #ifdef LOGALLOC
129 char *nasm_strndup_log (char *file, int line, char *s, size_t len)
130 #else
131 char *nasm_strndup (char *s, size_t len)
132 #endif
133 {
134 char *p;
135 int size = len+1;
136
137 p = malloc(size);
138 if (!p)
139 nasm_malloc_error (ERR_FATAL | ERR_NOFILE, "out of memory");
140 #ifdef LOGALLOC
141 else
142 fprintf(logfp, "%s %d strndup(%ld) returns %p\n",
143 file, line, (long)size, p);
144 #endif
145 strncpy (p, s, len);
146 p[len] = '\0';
147 return p;
148 }
149
150 int nasm_stricmp (char *s1, char *s2) {
151 while (*s1 && toupper(*s1) == toupper(*s2))
152 s1++, s2++;
153 if (!*s1 && !*s2)
154 return 0;
155 else if (toupper(*s1) < toupper(*s2))
156 return -1;
157 else
158 return 1;
159 }
160
161 int nasm_strnicmp (char *s1, char *s2, int n) {
162 while (n > 0 && *s1 && toupper(*s1) == toupper(*s2))
163 s1++, s2++, n--;
164 if ((!*s1 && !*s2) || n==0)
165 return 0;
166 else if (toupper(*s1) < toupper(*s2))
167 return -1;
168 else
169 return 1;
170 }
171
172 #define lib_isnumchar(c) ( isalnum(c) || (c) == '$')
173 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
174
175 long readnum (char *str, int *error) {
176 char *r = str, *q;
177 long radix;
178 unsigned long result, checklimit;
179 int warn = FALSE;
180
181 *error = FALSE;
182
183 while (isspace(*r)) r++; /* find start of number */
184 q = r;
185
186 while (lib_isnumchar(*q)) q++; /* find end of number */
187
188 /*
189 * If it begins 0x, 0X or $, or ends in H, it's in hex. if it
190 * ends in Q, it's octal. if it ends in B, it's binary.
191 * Otherwise, it's ordinary decimal.
192 */
193 if (*r=='0' && (r[1]=='x' || r[1]=='X'))
194 radix = 16, r += 2;
195 else if (*r=='$')
196 radix = 16, r++;
197 else if (q[-1]=='H' || q[-1]=='h')
198 radix = 16 , q--;
199 else if (q[-1]=='Q' || q[-1]=='q')
200 radix = 8 , q--;
201 else if (q[-1]=='B' || q[-1]=='b')
202 radix = 2 , q--;
203 else
204 radix = 10;
205
206 /*
207 * If this number has been found for us by something other than
208 * the ordinary scanners, then it might be malformed by having
209 * nothing between the prefix and the suffix. Check this case
210 * now.
211 */
212 if (r >= q) {
213 *error = TRUE;
214 return 0;
215 }
216
217 /*
218 * `checklimit' must be 2**32 / radix. We can't do that in
219 * 32-bit arithmetic, which we're (probably) using, so we
220 * cheat: since we know that all radices we use are even, we
221 * can divide 2**31 by radix/2 instead.
222 */
223 checklimit = 0x80000000UL / (radix>>1);
224
225 result = 0;
226 while (*r && r < q) {
227 if (*r<'0' || (*r>'9' && *r<'A') || numvalue(*r)>=radix) {
228 *error = TRUE;
229 return 0;
230 }
231 if (result >= checklimit)
232 warn = TRUE;
233 result = radix * result + numvalue(*r);
234 r++;
235 }
236
237 if (warn)
238 nasm_malloc_error (ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
239 "numeric constant %s does not fit in 32 bits",
240 str);
241
242 return result;
243 }
244
245 static long next_seg;
246
247 void seg_init(void) {
248 next_seg = 0;
249 }
250
251 long seg_alloc(void) {
252 return (next_seg += 2) - 2;
253 }
254
255 void fwriteshort (int data, FILE *fp) {
256 fputc ((int) (data & 255), fp);
257 fputc ((int) ((data >> 8) & 255), fp);
258 }
259
260 void fwritelong (long data, FILE *fp) {
261 fputc ((int) (data & 255), fp);
262 fputc ((int) ((data >> 8) & 255), fp);
263 fputc ((int) ((data >> 16) & 255), fp);
264 fputc ((int) ((data >> 24) & 255), fp);
265 }
266
267 void standard_extension (char *inname, char *outname, char *extension,
268 efunc error) {
269 char *p, *q;
270
271 if (*outname) /* file name already exists, */
272 return; /* so do nothing */
273 q = inname;
274 p = outname;
275 while (*q) *p++ = *q++; /* copy, and find end of string */
276 *p = '\0'; /* terminate it */
277 while (p > outname && *--p != '.');/* find final period (or whatever) */
278 if (*p != '.') while (*p) p++; /* go back to end if none found */
279 if (!strcmp(p, extension)) { /* is the extension already there? */
280 if (*extension)
281 error(ERR_WARNING | ERR_NOFILE,
282 "file name already ends in `%s': "
283 "output will be in `nasm.out'",
284 extension);
285 else
286 error(ERR_WARNING | ERR_NOFILE,
287 "file name already has no extension: "
288 "output will be in `nasm.out'");
289 strcpy(outname, "nasm.out");
290 } else
291 strcpy(p, extension);
292 }
293
294 #define RAA_BLKSIZE 4096 /* this many longs allocated at once */
295 #define RAA_LAYERSIZE 1024 /* this many _pointers_ allocated */
296
297 typedef struct RAA RAA;
298 typedef union RAA_UNION RAA_UNION;
299 typedef struct RAA_LEAF RAA_LEAF;
300 typedef struct RAA_BRANCH RAA_BRANCH;
301
302 struct RAA {
303 /*
304 * Number of layers below this one to get to the real data. 0
305 * means this structure is a leaf, holding RAA_BLKSIZE real
306 * data items; 1 and above mean it's a branch, holding
307 * RAA_LAYERSIZE pointers to the next level branch or leaf
308 * structures.
309 */
310 int layers;
311 /*
312 * Number of real data items spanned by one position in the
313 * `data' array at this level. This number is 1, trivially, for
314 * a leaf (level 0): for a level 1 branch it should be
315 * RAA_BLKSIZE, and for a level 2 branch it's
316 * RAA_LAYERSIZE*RAA_BLKSIZE.
317 */
318 long stepsize;
319 union RAA_UNION {
320 struct RAA_LEAF {
321 long data[RAA_BLKSIZE];
322 } l;
323 struct RAA_BRANCH {
324 struct RAA *data[RAA_LAYERSIZE];
325 } b;
326 } u;
327 };
328
329 #define LEAFSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_LEAF))
330 #define BRANCHSIZ (sizeof(RAA)-sizeof(RAA_UNION)+sizeof(RAA_BRANCH))
331
332 #define LAYERSIZ(r) ( (r)->layers==0 ? RAA_BLKSIZE : RAA_LAYERSIZE )
333
334 static struct RAA *real_raa_init (int layers) {
335 struct RAA *r;
336
337 if (layers == 0) {
338 r = nasm_malloc (LEAFSIZ);
339 memset (r->u.l.data, 0, sizeof(r->u.l.data));
340 r->layers = 0;
341 r->stepsize = 1L;
342 } else {
343 r = nasm_malloc (BRANCHSIZ);
344 memset (r->u.b.data, 0, sizeof(r->u.b.data));
345 r->layers = layers;
346 r->stepsize = RAA_BLKSIZE;
347 while (--layers)
348 r->stepsize *= RAA_LAYERSIZE;
349 }
350 return r;
351 }
352
353 struct RAA *raa_init (void) {
354 return real_raa_init (0);
355 }
356
357 void raa_free (struct RAA *r) {
358 if (r->layers == 0)
359 nasm_free (r);
360 else {
361 struct RAA **p;
362 for (p = r->u.b.data; p - r->u.b.data < RAA_LAYERSIZE; p++)
363 if (*p)
364 raa_free (*p);
365 }
366 }
367
368 long raa_read (struct RAA *r, long posn) {
369 if (posn > r->stepsize * LAYERSIZ(r))
370 return 0L;
371 while (r->layers > 0) {
372 ldiv_t l;
373 l = ldiv (posn, r->stepsize);
374 r = r->u.b.data[l.quot];
375 posn = l.rem;
376 if (!r) /* better check this */
377 return 0L;
378 }
379 return r->u.l.data[posn];
380 }
381
382 struct RAA *raa_write (struct RAA *r, long posn, long value) {
383 struct RAA *result;
384
385 if (posn < 0)
386 nasm_malloc_error (ERR_PANIC, "negative position in raa_write");
387
388 while (r->stepsize * LAYERSIZ(r) < posn) {
389 /*
390 * Must go up a layer.
391 */
392 struct RAA *s;
393
394 s = nasm_malloc (BRANCHSIZ);
395 memset (s->u.b.data, 0, sizeof(r->u.b.data));
396 s->layers = r->layers + 1;
397 s->stepsize = RAA_LAYERSIZE * r->stepsize;
398 s->u.b.data[0] = r;
399 r = s;
400 }
401
402 result = r;
403
404 while (r->layers > 0) {
405 ldiv_t l;
406 struct RAA **s;
407 l = ldiv (posn, r->stepsize);
408 s = &r->u.b.data[l.quot];
409 if (!*s)
410 *s = real_raa_init (r->layers - 1);
411 r = *s;
412 posn = l.rem;
413 }
414
415 r->u.l.data[posn] = value;
416
417 return result;
418 }
419
420 #define SAA_MAXLEN 8192
421
422 struct SAA {
423 /*
424 * members `end' and `elem_len' are only valid in first link in
425 * list; `rptr' and `rpos' are used for reading
426 */
427 struct SAA *next, *end, *rptr;
428 long elem_len, length, posn, start, rpos;
429 char *data;
430 };
431
432 struct SAA *saa_init (long elem_len) {
433 struct SAA *s;
434
435 if (elem_len > SAA_MAXLEN)
436 nasm_malloc_error (ERR_PANIC | ERR_NOFILE, "SAA with huge elements");
437
438 s = nasm_malloc (sizeof(struct SAA));
439 s->posn = s->start = 0L;
440 s->elem_len = elem_len;
441 s->length = SAA_MAXLEN - (SAA_MAXLEN % elem_len);
442 s->data = nasm_malloc (s->length);
443 s->next = NULL;
444 s->end = s;
445
446 return s;
447 }
448
449 void saa_free (struct SAA *s) {
450 struct SAA *t;
451
452 while (s) {
453 t = s->next;
454 nasm_free (s->data);
455 nasm_free (s);
456 s = t;
457 }
458 }
459
460 void *saa_wstruct (struct SAA *s) {
461 void *p;
462
463 if (s->end->length - s->end->posn < s->elem_len) {
464 s->end->next = nasm_malloc (sizeof(struct SAA));
465 s->end->next->start = s->end->start + s->end->posn;
466 s->end = s->end->next;
467 s->end->length = s->length;
468 s->end->next = NULL;
469 s->end->posn = 0L;
470 s->end->data = nasm_malloc (s->length);
471 }
472
473 p = s->end->data + s->end->posn;
474 s->end->posn += s->elem_len;
475 return p;
476 }
477
478 void saa_wbytes (struct SAA *s, void *data, long len) {
479 char *d = data;
480
481 while (len > 0) {
482 long l = s->end->length - s->end->posn;
483 if (l > len)
484 l = len;
485 if (l > 0) {
486 if (d) {
487 memcpy (s->end->data + s->end->posn, d, l);
488 d += l;
489 } else
490 memset (s->end->data + s->end->posn, 0, l);
491 s->end->posn += l;
492 len -= l;
493 }
494 if (len > 0) {
495 s->end->next = nasm_malloc (sizeof(struct SAA));
496 s->end->next->start = s->end->start + s->end->posn;
497 s->end = s->end->next;
498 s->end->length = s->length;
499 s->end->next = NULL;
500 s->end->posn = 0L;
501 s->end->data = nasm_malloc (s->length);
502 }
503 }
504 }
505
506 void saa_rewind (struct SAA *s) {
507 s->rptr = s;
508 s->rpos = 0L;
509 }
510
511 void *saa_rstruct (struct SAA *s) {
512 void *p;
513
514 if (!s->rptr)
515 return NULL;
516
517 if (s->rptr->posn - s->rpos < s->elem_len) {
518 s->rptr = s->rptr->next;
519 if (!s->rptr)
520 return NULL; /* end of array */
521 s->rpos = 0L;
522 }
523
524 p = s->rptr->data + s->rpos;
525 s->rpos += s->elem_len;
526 return p;
527 }
528
529 void *saa_rbytes (struct SAA *s, long *len) {
530 void *p;
531
532 if (!s->rptr)
533 return NULL;
534
535 p = s->rptr->data + s->rpos;
536 *len = s->rptr->posn - s->rpos;
537 s->rptr = s->rptr->next;
538 s->rpos = 0L;
539 return p;
540 }
541
542 void saa_rnbytes (struct SAA *s, void *data, long len) {
543 char *d = data;
544
545 while (len > 0) {
546 long l;
547
548 if (!s->rptr)
549 return;
550
551 l = s->rptr->posn - s->rpos;
552 if (l > len)
553 l = len;
554 if (l > 0) {
555 memcpy (d, s->rptr->data + s->rpos, l);
556 d += l;
557 s->rpos += l;
558 len -= l;
559 }
560 if (len > 0) {
561 s->rptr = s->rptr->next;
562 s->rpos = 0L;
563 }
564 }
565 }
566
567 void saa_fread (struct SAA *s, long posn, void *data, long len) {
568 struct SAA *p;
569 long pos;
570 char *cdata = data;
571
572 if (!s->rptr || posn > s->rptr->start + s->rpos)
573 saa_rewind (s);
574 while (posn >= s->rptr->start + s->rptr->posn) {
575 s->rptr = s->rptr->next;
576 if (!s->rptr)
577 return; /* what else can we do?! */
578 }
579
580 p = s->rptr;
581 pos = posn - s->rptr->start;
582 while (len) {
583 long l = s->rptr->posn - pos;
584 if (l > len)
585 l = len;
586 memcpy (cdata, s->rptr->data+pos, l);
587 len -= l;
588 cdata += l;
589 p = p->next;
590 if (!p)
591 return;
592 pos = 0L;
593 }
594 }
595
596 void saa_fwrite (struct SAA *s, long posn, void *data, long len) {
597 struct SAA *p;
598 long pos;
599 char *cdata = data;
600
601 if (!s->rptr || posn > s->rptr->start + s->rpos)
602 saa_rewind (s);
603 while (posn >= s->rptr->start + s->rptr->posn) {
604 s->rptr = s->rptr->next;
605 if (!s->rptr)
606 return; /* what else can we do?! */
607 }
608
609 p = s->rptr;
610 pos = posn - s->rptr->start;
611 while (len) {
612 long l = s->rptr->posn - pos;
613 if (l > len)
614 l = len;
615 memcpy (s->rptr->data+pos, cdata, l);
616 len -= l;
617 cdata += l;
618 p = p->next;
619 if (!p)
620 return;
621 pos = 0L;
622 }
623 }
624
625 void saa_fpwrite (struct SAA *s, FILE *fp) {
626 char *data;
627 long len;
628
629 saa_rewind (s);
630 while ( (data = saa_rbytes (s, &len)) )
631 fwrite (data, 1, len, fp);
632 }
633
634 /*
635 * Register, instruction, condition-code and prefix keywords used
636 * by the scanner.
637 */
638 #include "names.c"
639 static char *special_names[] = {
640 "byte", "dword", "far", "long", "near", "nosplit", "qword",
641 "short", "to", "tword", "word"
642 };
643 static char *prefix_names[] = {
644 "a16", "a32", "lock", "o16", "o32", "rep", "repe", "repne",
645 "repnz", "repz", "times"
646 };
647
648
649 /*
650 * Standard scanner routine used by parser.c and some output
651 * formats. It keeps a succession of temporary-storage strings in
652 * stdscan_tempstorage, which can be cleared using stdscan_reset.
653 */
654 static char **stdscan_tempstorage = NULL;
655 static int stdscan_tempsize = 0, stdscan_templen = 0;
656 #define STDSCAN_TEMP_DELTA 256
657
658 static void stdscan_pop(void) {
659 nasm_free (stdscan_tempstorage[--stdscan_templen]);
660 }
661
662 void stdscan_reset(void) {
663 while (stdscan_templen > 0)
664 stdscan_pop();
665 }
666
667 static char *stdscan_copy(char *p, int len) {
668 char *text;
669
670 text = nasm_malloc(len+1);
671 strncpy (text, p, len);
672 text[len] = '\0';
673
674 if (stdscan_templen >= stdscan_tempsize) {
675 stdscan_tempsize += STDSCAN_TEMP_DELTA;
676 stdscan_tempstorage = nasm_realloc(stdscan_tempstorage,
677 stdscan_tempsize*sizeof(char *));
678 }
679 stdscan_tempstorage[stdscan_templen++] = text;
680
681 return text;
682 }
683
684 char *stdscan_bufptr = NULL;
685 int stdscan (void *private_data, struct tokenval *tv) {
686 char ourcopy[256], *r, *s;
687
688 while (isspace(*stdscan_bufptr)) stdscan_bufptr++;
689 if (!*stdscan_bufptr)
690 return tv->t_type = 0;
691
692 /* we have a token; either an id, a number or a char */
693 if (isidstart(*stdscan_bufptr) ||
694 (*stdscan_bufptr == '$' && isidstart(stdscan_bufptr[1]))) {
695 /* now we've got an identifier */
696 int i;
697 int is_sym = FALSE;
698
699 if (*stdscan_bufptr == '$') {
700 is_sym = TRUE;
701 stdscan_bufptr++;
702 }
703
704 r = stdscan_bufptr++;
705 while (isidchar(*stdscan_bufptr)) stdscan_bufptr++;
706 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
707
708 for (s=tv->t_charptr, r=ourcopy; *s; s++)
709 *r++ = tolower (*s);
710 *r = '\0';
711 if (is_sym)
712 return tv->t_type = TOKEN_ID;/* bypass all other checks */
713 /* right, so we have an identifier sitting in temp storage. now,
714 * is it actually a register or instruction name, or what? */
715 if ((tv->t_integer=bsi(ourcopy, reg_names,
716 elements(reg_names)))>=0) {
717 tv->t_integer += EXPR_REG_START;
718 return tv->t_type = TOKEN_REG;
719 } else if ((tv->t_integer=bsi(ourcopy, insn_names,
720 elements(insn_names)))>=0) {
721 return tv->t_type = TOKEN_INSN;
722 }
723 for (i=0; i<elements(icn); i++)
724 if (!strncmp(ourcopy, icn[i], strlen(icn[i]))) {
725 char *p = ourcopy + strlen(icn[i]);
726 tv->t_integer = ico[i];
727 if ((tv->t_inttwo=bsi(p, conditions,
728 elements(conditions)))>=0)
729 return tv->t_type = TOKEN_INSN;
730 }
731 if ((tv->t_integer=bsi(ourcopy, prefix_names,
732 elements(prefix_names)))>=0) {
733 tv->t_integer += PREFIX_ENUM_START;
734 return tv->t_type = TOKEN_PREFIX;
735 }
736 if ((tv->t_integer=bsi(ourcopy, special_names,
737 elements(special_names)))>=0)
738 return tv->t_type = TOKEN_SPECIAL;
739 if (!strcmp(ourcopy, "seg"))
740 return tv->t_type = TOKEN_SEG;
741 if (!strcmp(ourcopy, "wrt"))
742 return tv->t_type = TOKEN_WRT;
743 return tv->t_type = TOKEN_ID;
744 } else if (*stdscan_bufptr == '$' && !isnumchar(stdscan_bufptr[1])) {
745 /*
746 * It's a $ sign with no following hex number; this must
747 * mean it's a Here token ($), evaluating to the current
748 * assembly location, or a Base token ($$), evaluating to
749 * the base of the current segment.
750 */
751 stdscan_bufptr++;
752 if (*stdscan_bufptr == '$') {
753 stdscan_bufptr++;
754 return tv->t_type = TOKEN_BASE;
755 }
756 return tv->t_type = TOKEN_HERE;
757 } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */
758 int rn_error;
759
760 r = stdscan_bufptr++;
761 while (isnumchar(*stdscan_bufptr))
762 stdscan_bufptr++;
763
764 if (*stdscan_bufptr == '.') {
765 /*
766 * a floating point constant
767 */
768 stdscan_bufptr++;
769 while (isnumchar(*stdscan_bufptr)) {
770 stdscan_bufptr++;
771 }
772 tv->t_charptr = stdscan_copy(r, stdscan_bufptr - r);
773 return tv->t_type = TOKEN_FLOAT;
774 }
775 r = stdscan_copy(r, stdscan_bufptr - r);
776 tv->t_integer = readnum(r, &rn_error);
777 stdscan_pop();
778 if (rn_error)
779 return tv->t_type = TOKEN_ERRNUM;/* some malformation occurred */
780 tv->t_charptr = NULL;
781 return tv->t_type = TOKEN_NUM;
782 } else if (*stdscan_bufptr == '\'' ||
783 *stdscan_bufptr == '"') {/* a char constant */
784 char quote = *stdscan_bufptr++, *r;
785 r = tv->t_charptr = stdscan_bufptr;
786 while (*stdscan_bufptr && *stdscan_bufptr != quote) stdscan_bufptr++;
787 tv->t_inttwo = stdscan_bufptr - r; /* store full version */
788 if (!*stdscan_bufptr)
789 return tv->t_type = TOKEN_ERRNUM; /* unmatched quotes */
790 tv->t_integer = 0;
791 r = stdscan_bufptr++; /* skip over final quote */
792 while (quote != *--r) {
793 tv->t_integer = (tv->t_integer<<8) + (unsigned char) *r;
794 }
795 return tv->t_type = TOKEN_NUM;
796 } else if (*stdscan_bufptr == ';') { /* a comment has happened - stay */
797 return tv->t_type = 0;
798 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '>') {
799 stdscan_bufptr += 2;
800 return tv->t_type = TOKEN_SHR;
801 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '<') {
802 stdscan_bufptr += 2;
803 return tv->t_type = TOKEN_SHL;
804 } else if (stdscan_bufptr[0] == '/' && stdscan_bufptr[1] == '/') {
805 stdscan_bufptr += 2;
806 return tv->t_type = TOKEN_SDIV;
807 } else if (stdscan_bufptr[0] == '%' && stdscan_bufptr[1] == '%') {
808 stdscan_bufptr += 2;
809 return tv->t_type = TOKEN_SMOD;
810 } else if (stdscan_bufptr[0] == '=' && stdscan_bufptr[1] == '=') {
811 stdscan_bufptr += 2;
812 return tv->t_type = TOKEN_EQ;
813 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '>') {
814 stdscan_bufptr += 2;
815 return tv->t_type = TOKEN_NE;
816 } else if (stdscan_bufptr[0] == '!' && stdscan_bufptr[1] == '=') {
817 stdscan_bufptr += 2;
818 return tv->t_type = TOKEN_NE;
819 } else if (stdscan_bufptr[0] == '<' && stdscan_bufptr[1] == '=') {
820 stdscan_bufptr += 2;
821 return tv->t_type = TOKEN_LE;
822 } else if (stdscan_bufptr[0] == '>' && stdscan_bufptr[1] == '=') {
823 stdscan_bufptr += 2;
824 return tv->t_type = TOKEN_GE;
825 } else if (stdscan_bufptr[0] == '&' && stdscan_bufptr[1] == '&') {
826 stdscan_bufptr += 2;
827 return tv->t_type = TOKEN_DBL_AND;
828 } else if (stdscan_bufptr[0] == '^' && stdscan_bufptr[1] == '^') {
829 stdscan_bufptr += 2;
830 return tv->t_type = TOKEN_DBL_XOR;
831 } else if (stdscan_bufptr[0] == '|' && stdscan_bufptr[1] == '|') {
832 stdscan_bufptr += 2;
833 return tv->t_type = TOKEN_DBL_OR;
834 } else /* just an ordinary char */
835 return tv->t_type = (unsigned char) (*stdscan_bufptr++);
836 }
837
838 /*
839 * Return TRUE if the argument is a simple scalar. (Or a far-
840 * absolute, which counts.)
841 */
842 int is_simple (expr *vect) {
843 while (vect->type && !vect->value)
844 vect++;
845 if (!vect->type)
846 return 1;
847 if (vect->type != EXPR_SIMPLE)
848 return 0;
849 do {
850 vect++;
851 } while (vect->type && !vect->value);
852 if (vect->type && vect->type < EXPR_SEGBASE+SEG_ABS) return 0;
853 return 1;
854 }
855
856 /*
857 * Return TRUE if the argument is a simple scalar, _NOT_ a far-
858 * absolute.
859 */
860 int is_really_simple (expr *vect) {
861 while (vect->type && !vect->value)
862 vect++;
863 if (!vect->type)
864 return 1;
865 if (vect->type != EXPR_SIMPLE)
866 return 0;
867 do {
868 vect++;
869 } while (vect->type && !vect->value);
870 if (vect->type) return 0;
871 return 1;
872 }
873
874 /*
875 * Return TRUE if the argument is relocatable (i.e. a simple
876 * scalar, plus at most one segment-base, plus possibly a WRT).
877 */
878 int is_reloc (expr *vect) {
879 while (vect->type && !vect->value) /* skip initial value-0 terms */
880 vect++;
881 if (!vect->type) /* trivially return TRUE if nothing */
882 return 1; /* is present apart from value-0s */
883 if (vect->type < EXPR_SIMPLE) /* FALSE if a register is present */
884 return 0;
885 if (vect->type == EXPR_SIMPLE) { /* skip over a pure number term... */
886 do {
887 vect++;
888 } while (vect->type && !vect->value);
889 if (!vect->type) /* ...returning TRUE if that's all */
890 return 1;
891 }
892 if (vect->type == EXPR_WRT) { /* skip over a WRT term... */
893 do {
894 vect++;
895 } while (vect->type && !vect->value);
896 if (!vect->type) /* ...returning TRUE if that's all */
897 return 1;
898 }
899 if (vect->value != 0 && vect->value != 1)
900 return 0; /* segment base multiplier non-unity */
901 do { /* skip over _one_ seg-base term... */
902 vect++;
903 } while (vect->type && !vect->value);
904 if (!vect->type) /* ...returning TRUE if that's all */
905 return 1;
906 return 0; /* And return FALSE if there's more */
907 }
908
909 /*
910 * Return TRUE if the argument contains an `unknown' part.
911 */
912 int is_unknown(expr *vect) {
913 while (vect->type && vect->type < EXPR_UNKNOWN)
914 vect++;
915 return (vect->type == EXPR_UNKNOWN);
916 }
917
918 /*
919 * Return TRUE if the argument contains nothing but an `unknown'
920 * part.
921 */
922 int is_just_unknown(expr *vect) {
923 while (vect->type && !vect->value)
924 vect++;
925 return (vect->type == EXPR_UNKNOWN);
926 }
927
928 /*
929 * Return the scalar part of a relocatable vector. (Including
930 * simple scalar vectors - those qualify as relocatable.)
931 */
932 long reloc_value (expr *vect) {
933 while (vect->type && !vect->value)
934 vect++;
935 if (!vect->type) return 0;
936 if (vect->type == EXPR_SIMPLE)
937 return vect->value;
938 else
939 return 0;
940 }
941
942 /*
943 * Return the segment number of a relocatable vector, or NO_SEG for
944 * simple scalars.
945 */
946 long reloc_seg (expr *vect) {
947 while (vect->type && (vect->type == EXPR_WRT || !vect->value))
948 vect++;
949 if (vect->type == EXPR_SIMPLE) {
950 do {
951 vect++;
952 } while (vect->type && (vect->type == EXPR_WRT || !vect->value));
953 }
954 if (!vect->type)
955 return NO_SEG;
956 else
957 return vect->type - EXPR_SEGBASE;
958 }
959
960 /*
961 * Return the WRT segment number of a relocatable vector, or NO_SEG
962 * if no WRT part is present.
963 */
964 long reloc_wrt (expr *vect) {
965 while (vect->type && vect->type < EXPR_WRT)
966 vect++;
967 if (vect->type == EXPR_WRT) {
968 return vect->value;
969 } else
970 return NO_SEG;
971 }
972
973 /*
974 * Binary search.
975 */
976 int bsi (char *string, char **array, int size) {
977 int i = -1, j = size; /* always, i < index < j */
978 while (j-i >= 2) {
979 int k = (i+j)/2;
980 int l = strcmp(string, array[k]);
981 if (l<0) /* it's in the first half */
982 j = k;
983 else if (l>0) /* it's in the second half */
984 i = k;
985 else /* we've got it :) */
986 return k;
987 }
988 return -1; /* we haven't got it :( */
989 }