NASM 0.98.22
[platform/upstream/nasm.git] / nasm.c
1 /* The Netwide Assembler main program module
2  *
3  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4  * Julian Hall. All rights reserved. The software is
5  * redistributable under the licence given in the file "Licence"
6  * distributed in the NASM archive.
7  */
8
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "insns.h"
18 #include "preproc.h"
19 #include "parser.h"
20 #include "eval.h"
21 #include "assemble.h"
22 #include "labels.h"
23 #include "outform.h"
24 #include "listing.h"
25
26 struct forwrefinfo {                   /* info held on forward refs. */
27     int lineno;
28     int operand;
29 };
30
31 static int get_bits (char *value);
32 static unsigned long get_cpu (char *cpu_str);
33 static void report_error (int, char *, ...);
34 static void parse_cmdline (int, char **);
35 static void assemble_file (char *);
36 static int getkw (char *buf, char **value);
37 static void register_output_formats(void);
38 static void usage(void);
39
40 static int using_debug_info;
41 int        tasm_compatible_mode = FALSE;
42 int pass0;
43
44 static char inname[FILENAME_MAX];
45 static char outname[FILENAME_MAX];
46 static char listname[FILENAME_MAX];
47 static int globallineno;               /* for forward-reference tracking */
48 /* static int pass = 0; */
49 static struct ofmt *ofmt = NULL;
50
51 static FILE *error_file;               /* Where to write error messages */
52
53 static FILE *ofile = NULL;
54 int optimizing = -1;            /* number of optimization passes to take */
55 static int sb, cmd_sb = 16;                    /* by default */
56 static unsigned long cmd_cpu = IF_PLEVEL;       /* highest level by default */
57 static unsigned long cpu = IF_PLEVEL;           /* passed to insn_size & assemble.c */
58 int global_offset_changed;             /* referenced in labels.c */
59
60 static loc_t location;
61 int          in_abs_seg;               /* Flag we are in ABSOLUTE seg */
62 static long  abs_seg;
63
64 static struct RAA *offsets;
65 static long abs_offset;
66
67 static struct SAA *forwrefs;           /* keep track of forward references */
68 static struct forwrefinfo *forwref;
69
70 static Preproc *preproc;
71 enum op_type {
72   op_normal,                    /* Preprocess and assemble */
73   op_preprocess,                /* Preprocess only */
74   op_depend                     /* Generate dependencies */
75 };
76 static enum op_type operating_mode;
77
78 /*
79  * Which of the suppressible warnings are suppressed. Entry zero
80  * doesn't do anything. Initial defaults are given here.
81  */
82 static char suppressed[1+ERR_WARN_MAX] = {
83     0, TRUE, TRUE, TRUE, FALSE
84 };
85
86 /*
87  * The option names for the suppressible warnings. As before, entry
88  * zero does nothing.
89  */
90 static char *suppressed_names[1+ERR_WARN_MAX] = {
91     NULL, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
92 };
93
94 /*
95  * The explanations for the suppressible warnings. As before, entry
96  * zero does nothing.
97  */
98 static char *suppressed_what[1+ERR_WARN_MAX] = {
99     NULL,
100     "macro calls with wrong no. of params",
101     "cyclic macro self-references",
102     "labels alone on lines without trailing `:'",
103     "numeric constants greater than 0xFFFFFFFF"
104 };
105
106 /*
107  * This is a null preprocessor which just copies lines from input
108  * to output. It's used when someone explicitly requests that NASM
109  * not preprocess their source file.
110  */
111
112 static void no_pp_reset (char *, int, efunc, evalfunc, ListGen *);
113 static char *no_pp_getline (void);
114 static void no_pp_cleanup (int);
115 static Preproc no_pp = {
116     no_pp_reset,
117     no_pp_getline,
118     no_pp_cleanup
119 };
120
121 /*
122  * get/set current offset...
123  */
124 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
125                       raa_read(offsets,location.segment))
126 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
127                          (void)(offsets=raa_write(offsets,location.segment,(x))))
128
129 static int want_usage;
130 static int terminate_after_phase;
131 int user_nolist = 0;             /* fbk 9/2/00 */
132
133 static void nasm_fputs(char *line, FILE *ofile)
134 {
135     if (ofile) {
136         fputs(line, ofile);
137         fputc('\n', ofile);
138     } else
139         puts(line);
140 }
141
142 int main(int argc, char **argv)
143 {
144         pass0 = 1;
145     want_usage = terminate_after_phase = FALSE;
146
147     nasm_set_malloc_error (report_error);
148     offsets = raa_init();
149     forwrefs = saa_init ((long)sizeof(struct forwrefinfo));
150
151     preproc = &nasmpp;
152     operating_mode = op_normal;
153
154     error_file = stderr;
155
156     seg_init();
157
158     register_output_formats();
159
160     parse_cmdline(argc, argv);
161
162     if (terminate_after_phase)
163     {
164         if (want_usage)
165             usage();
166         return 1;
167     }
168
169     if (ofmt->stdmac)
170         pp_extra_stdmac (ofmt->stdmac);
171     parser_global_info (ofmt, &location);
172     eval_global_info (ofmt, lookup_label, &location);
173
174     /* define some macros dependent of command-line */
175     {
176         char temp [64];
177         sprintf (temp, "__OUTPUT_FORMAT__=%s\n", ofmt->shortname);
178         pp_pre_define (temp);
179     }
180
181     switch ( operating_mode ) {
182     case op_depend:
183       {
184         char *line;
185         preproc->reset (inname, 0, report_error, evaluate, &nasmlist);
186         if (outname[0] == '\0')
187           ofmt->filename (inname, outname, report_error);
188         ofile = NULL;
189         fprintf(stdout, "%s: %s", outname, inname);
190         while ( (line = preproc->getline()) )
191           nasm_free (line);
192         preproc->cleanup(0);
193         putc('\n', stdout);
194       }
195     break;
196
197     case op_preprocess:
198     {
199       char *line;
200       char *file_name = NULL;
201       long  prior_linnum=0;
202       int   lineinc=0;
203
204       if (*outname) {
205         ofile = fopen(outname, "w");
206         if (!ofile)
207           report_error (ERR_FATAL | ERR_NOFILE,
208                         "unable to open output file `%s'", outname);
209       } else
210         ofile = NULL;
211
212       location.known = FALSE;
213
214 /*      pass = 1; */
215       preproc->reset (inname, 2, report_error, evaluate, &nasmlist);
216       while ( (line = preproc->getline()) ) {
217         /*
218          * We generate %line directives if needed for later programs
219          */
220         long linnum = prior_linnum += lineinc;
221         int  altline = src_get(&linnum, &file_name);
222         if (altline) {
223           if (altline==1 && lineinc==1)
224             nasm_fputs("", ofile);
225           else {
226             lineinc = (altline != -1 || lineinc!=1);
227             fprintf(ofile ? ofile : stdout, "%%line %ld+%d %s\n",
228                     linnum, lineinc, file_name);
229           }
230           prior_linnum = linnum;
231         }
232         nasm_fputs(line, ofile);
233         nasm_free (line);
234       }
235       nasm_free(file_name);
236       preproc->cleanup(0);
237       if (ofile)
238         fclose(ofile);
239       if (ofile && terminate_after_phase)
240         remove(outname);
241     }
242     break;
243
244     case op_normal:
245       {
246         /*
247          * We must call ofmt->filename _anyway_, even if the user
248          * has specified their own output file, because some
249          * formats (eg OBJ and COFF) use ofmt->filename to find out
250          * the name of the input file and then put that inside the
251          * file.
252          */
253         ofmt->filename (inname, outname, report_error);
254         
255         ofile = fopen(outname, "wb");
256         if (!ofile) {
257           report_error (ERR_FATAL | ERR_NOFILE,
258                         "unable to open output file `%s'", outname);
259         }
260         
261         /*
262          * We must call init_labels() before ofmt->init() since
263          * some object formats will want to define labels in their
264          * init routines. (eg OS/2 defines the FLAT group)
265          */
266         init_labels ();
267         
268         ofmt->init (ofile, report_error, define_label, evaluate);
269         
270         assemble_file (inname);
271         
272         if (!terminate_after_phase) {
273           ofmt->cleanup (using_debug_info);
274           cleanup_labels ();
275             } else {
276           /*
277            * We had an fclose on the output file here, but we
278            * actually do that in all the object file drivers as well,
279            * so we're leaving out the one here.
280            *     fclose (ofile);
281            */
282           remove(outname);
283           if (listname[0])
284              remove(listname);
285         }
286       }
287     break;
288     }
289
290     if (want_usage)
291       usage();
292
293     raa_free (offsets);
294     saa_free (forwrefs);
295     eval_cleanup ();
296     nasmlib_cleanup ();
297
298     if (terminate_after_phase)
299         return 1;
300     else
301         return 0;
302 }
303
304
305 /*
306  * Get a parameter for a command line option.
307  * First arg must be in the form of e.g. -f...
308  */
309 static char *get_param (char *p, char *q, int *advance)
310 {
311     *advance = 0;
312     if (p[2])                  /* the parameter's in the option */
313     {
314         p += 2;
315         while (isspace(*p))
316             p++;
317         return p;
318     }
319     if (q && q[0])
320     {
321         *advance = 1;
322         return q;
323     }
324     report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
325                   "option `-%c' requires an argument",
326                   p[1]);
327     return NULL;
328 }
329
330 struct textargs
331 {
332         char *label;
333         int value;
334 };
335
336 #define OPT_PREFIX 0
337 #define OPT_POSTFIX 1
338 struct textargs textopts[] =
339 {
340         {"prefix",OPT_PREFIX},
341         {"postfix",OPT_POSTFIX},
342         {NULL,0}
343 };
344
345
346 int stopoptions = 0;
347 static int process_arg (char *p, char *q)
348 {
349     char *param;
350     int  i, advance = 0;
351
352     if (!p || !p[0])
353         return 0;
354
355     if (p[0]=='-' && ! stopoptions)
356     {
357         switch (p[1]) {
358           case 's':
359               error_file = stdout;
360               break;
361           case 'o':                    /* these parameters take values */
362           case 'O':
363           case 'f':
364           case 'p':
365           case 'P':
366           case 'd':
367           case 'D':
368           case 'i':
369           case 'I':
370           case 'l':
371           case 'E':
372           case 'F':
373             if ( !(param = get_param (p, q, &advance)) )
374                 break;
375             if (p[1]=='o') {           /* output file */
376                 strcpy (outname, param);
377             } else if (p[1]=='f') {    /* output format */
378                 ofmt = ofmt_find(param);
379                 if (!ofmt) {
380                     report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
381                                   "unrecognised output format `%s' - "
382                                   "use -hf for a list",
383                                   param);
384                 }
385                 else
386                     ofmt->current_dfmt = ofmt->debug_formats[0];
387             } else if (p[1]=='O') {                 /* Optimization level */
388                 int opt;
389                 if (!isdigit(*param)) report_error(ERR_FATAL,
390                      "command line optimization level must be 0..3 or <nn>");
391                 opt = atoi(param);
392                 if (opt<=0) optimizing = -1;            /* 0.98 behaviour */
393                 else if (opt==1) optimizing = 0;        /* Two passes, 0.98.09 behavior */
394                 else if (opt<=3) optimizing = opt*5;    /* Multiple passes */
395                 else optimizing = opt;                  /* Multiple passes */
396             } else if (p[1]=='P' || p[1]=='p') {    /* pre-include */
397                 pp_pre_include (param);
398             } else if (p[1]=='D' || p[1]=='d') {    /* pre-define */
399                 pp_pre_define (param);
400             } else if (p[1]=='U' || p[1]=='u') {    /* un-define */
401                 pp_pre_undefine (param);
402             } else if (p[1]=='I' || p[1]=='i') {    /* include search path */
403                 pp_include_path (param);
404             } else if (p[1]=='l') {    /* listing file */
405                 strcpy (listname, param);
406             } else if (p[1]=='E') {    /* error messages file */
407                 error_file = fopen(param, "w");
408                 if ( !error_file ) {
409                   error_file = stderr; /* Revert to default! */
410                   report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
411                                 "cannot open file `%s' for error messages",
412                                 param);
413                 }
414             } else if (p[1] == 'F') {  /* specify debug format */
415                 ofmt->current_dfmt = dfmt_find(ofmt, param);
416                 if (!ofmt->current_dfmt) {
417                     report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
418                                   "unrecognized debug format `%s' for"
419                                   " output format `%s'",
420                                   param, ofmt->shortname);
421                 }
422             }
423             break;
424           case 'g':
425             using_debug_info = TRUE;
426             break;
427           case 'h':
428             printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
429                    "[-l listfile]\n"
430                    "            [options...] [--] filename\n"
431                    "    or nasm -r   for version info (obsolete)\n"
432                    "    or nasm -v   for version info (preferred)\n\n"
433                    "    -t          Assemble in SciTech TASM compatible mode\n"
434                    "    -g          Generate debug information in selected format.\n");
435             printf("    -e          preprocess only (writes output to stdout by default)\n"
436                    "    -a          don't preprocess (assemble only)\n"
437                    "    -M          generate Makefile dependencies on stdout\n\n"
438                    "    -E<file>    redirect error messages to file\n"
439                    "    -s          redirect error messages to stdout\n\n"
440                    "    -F format   select a debugging format\n\n"
441                    "    -I<path>    adds a pathname to the include file path\n");
442             printf("    -O<digit>   optimize branch offsets (-O0 disables, default)\n"
443                    "    -P<file>    pre-includes a file\n"
444                    "    -D<macro>[=<value>] pre-defines a macro\n"
445                    "    -U<macro>   undefines a macro\n"
446                    "    -w+foo      enables warnings about foo; -w-foo disables them\n"
447                    "where foo can be:\n");
448             for (i=1; i<=ERR_WARN_MAX; i++)
449                 printf("    %-16s%s (default %s)\n",
450                        suppressed_names[i], suppressed_what[i],
451                        suppressed[i] ? "off" : "on");
452             printf ("\nresponse files should contain command line parameters"
453                     ", one per line.\n");
454             if (p[2] == 'f') {
455                 printf("\nvalid output formats for -f are"
456                        " (`*' denotes default):\n");
457                 ofmt_list(ofmt, stdout);
458             }
459             else {
460                 printf ("\nFor a list of valid output formats, use -hf.\n");
461                 printf ("For a list of debug formats, use -f <form> -y.\n");
462             }
463             exit (0);                  /* never need usage message here */
464             break;
465           case 'y':
466             printf("\nvalid debug formats for '%s' output format are"
467                    " ('*' denotes default):\n",
468                 ofmt->shortname);
469             dfmt_list(ofmt, stdout);
470             exit(0);
471             break;
472           case 't':
473             tasm_compatible_mode = TRUE;
474             break;
475           case 'r':
476           case 'v':
477             printf("NASM version %s compiled "
478 #ifdef DEBUG
479             "with -DDEBUG "
480 #endif
481             "on " __DATE__ "\n", NASM_VER);
482             exit (0);                  /* never need usage message here */
483             break;
484           case 'e':                    /* preprocess only */
485             operating_mode = op_preprocess;
486             break;
487           case 'a':                    /* assemble only - don't preprocess */
488             preproc = &no_pp;
489             break;
490           case 'w':
491             if (p[2] != '+' && p[2] != '-') {
492                 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
493                               "invalid option to `-w'");
494             } else {
495                 for (i=1; i<=ERR_WARN_MAX; i++)
496                     if (!nasm_stricmp(p+3, suppressed_names[i]))
497                         break;
498                 if (i <= ERR_WARN_MAX)
499                     suppressed[i] = (p[2] == '-');
500                 else
501                     report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
502                                   "invalid option to `-w'");
503             }
504             break;
505           case 'M':
506             operating_mode = op_depend;
507             break;
508
509           case '-':
510           {
511                 int s;
512                 
513                 if (p[2]==0) {          /* -- => stop processing options */
514                     stopoptions = 1;
515                     break;
516                 }
517                 for(s=0; textopts[s].label; s++)
518                 {
519                         if(!nasm_stricmp(p+2, textopts[s].label))
520                         {
521                                 break;
522                         }
523                 }
524
525                 switch(s)
526                 {
527                 
528                         case OPT_PREFIX:
529                         case OPT_POSTFIX:
530                         {
531                                 if (!q)
532                                 {
533                                         report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
534                                                 "option `--%s' requires an argument",
535                                                 p+2);
536                                         break;
537                                 }
538                                 else
539                                 {
540                                         advance = 1, param = q;
541                                 }
542                                 
543                                 if(s == OPT_PREFIX)
544                                 {
545                                         strncpy(lprefix,param,PREFIX_MAX-1);
546                                         lprefix[PREFIX_MAX-1]=0;
547                                         break;
548                                 }
549                                 if(s == OPT_POSTFIX)
550                                 {
551                                         strncpy(lpostfix,param,POSTFIX_MAX-1);
552                                         lpostfix[POSTFIX_MAX-1]=0;
553                                         break;
554                                 }
555                                 break;
556                         }
557                         default:
558                         {
559                                 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
560                                         "unrecognised option `--%s'",
561                                         p+2);
562                                 break;
563                         }
564                 }
565                 break;
566           }
567
568           default:
569             if (!ofmt->setinfo(GI_SWITCH,&p))
570                 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
571                           "unrecognised option `-%c'",
572                           p[1]);
573             break;
574         }
575     }
576     else
577     {
578         if (*inname) {
579             report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
580                           "more than one input file specified");
581         } else
582             strcpy(inname, p);
583     }
584
585     return advance;
586 }
587
588 #define ARG_BUF_DELTA 128
589
590 static void process_respfile (FILE *rfile)
591 {
592     char *buffer, *p, *q, *prevarg;
593     int bufsize, prevargsize;
594
595     bufsize = prevargsize = ARG_BUF_DELTA;
596     buffer = nasm_malloc(ARG_BUF_DELTA);
597     prevarg = nasm_malloc(ARG_BUF_DELTA);
598     prevarg[0] = '\0';
599
600     while (1) {   /* Loop to handle all lines in file */
601
602         p = buffer;
603         while (1) {  /* Loop to handle long lines */
604             q = fgets(p, bufsize-(p-buffer), rfile);
605             if (!q)
606                 break;
607             p += strlen(p);
608             if (p > buffer && p[-1] == '\n')
609                 break;
610             if (p-buffer > bufsize-10) {
611                 int offset;
612                 offset = p - buffer;
613                 bufsize += ARG_BUF_DELTA;
614                 buffer = nasm_realloc(buffer, bufsize);
615                 p = buffer + offset;
616             }
617         }
618
619         if (!q && p == buffer) {
620             if (prevarg[0])
621                 process_arg (prevarg, NULL);
622             nasm_free (buffer);
623             nasm_free (prevarg);
624             return;
625         }
626
627         /*
628          * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
629          * them are present at the end of the line.
630          */
631         *(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
632
633         while (p > buffer && isspace(p[-1]))
634             *--p = '\0';
635
636         p = buffer;
637         while (isspace(*p))
638             p++;
639
640         if (process_arg (prevarg, p))
641             *p = '\0';
642
643         if (strlen(p) > prevargsize-10) {
644             prevargsize += ARG_BUF_DELTA;
645             prevarg = nasm_realloc(prevarg, prevargsize);
646         }
647         strcpy (prevarg, p);
648     }
649 }
650
651 /* Function to process args from a string of args, rather than the
652  * argv array. Used by the environment variable and response file
653  * processing.
654  */
655 static void process_args (char *args) {
656     char *p, *q, *arg, *prevarg;
657     char separator = ' ';
658
659     p = args;
660     if (*p && *p != '-')
661         separator = *p++;
662     arg = NULL;
663     while (*p) {
664         q = p;
665         while (*p && *p != separator) p++;
666         while (*p == separator) *p++ = '\0';
667         prevarg = arg;
668         arg = q;
669         if (process_arg (prevarg, arg))
670             arg = NULL;
671     }
672     if (arg)
673         process_arg (arg, NULL);
674 }
675
676 static void parse_cmdline(int argc, char **argv)
677 {
678     FILE *rfile;
679     char *envreal, *envcopy=NULL, *p, *arg;
680
681     *inname = *outname = *listname = '\0';
682
683     /*
684      * First, process the NASM environment variable.
685      */
686     envreal = getenv("NASM");
687     arg = NULL;
688     if (envreal) {
689         envcopy = nasm_strdup(envreal);
690         process_args(envcopy);
691         nasm_free (envcopy);
692     }
693
694     /*
695      * Now process the actual command line.
696      */
697     while (--argc)
698     {
699         int i;
700         argv++;
701         if (argv[0][0] == '@') {
702             /* We have a response file, so process this as a set of
703              * arguments like the environment variable. This allows us
704              * to have multiple arguments on a single line, which is
705              * different to the -@resp file processing below for regular
706              * NASM.
707              */
708             char *str = malloc(2048);
709             FILE *f = fopen(&argv[0][1],"r");
710             if (!str) {
711                 printf("out of memory");
712                 exit(-1);
713                 }
714             if (f) {
715                 while (fgets(str,2048,f)) {
716                     process_args(str);
717                 }
718                 fclose(f);
719             }
720             free(str);
721             argc--;
722             argv++;
723         }
724         if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
725             if ((p = get_param (argv[0], argc > 1 ? argv[1] : NULL, &i))) {
726                 if ((rfile = fopen(p, "r"))) {
727                     process_respfile (rfile);
728                     fclose(rfile);
729                 } else
730                     report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
731                             "unable to open response file `%s'", p);
732             }
733         } else
734             i = process_arg (argv[0], argc > 1 ? argv[1] : NULL);
735         argv += i, argc -= i;
736     }
737
738     if (!*inname)
739         report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
740                       "no input file specified");
741 }
742
743
744 static void assemble_file (char *fname)
745 {
746     char   * value, * p, * q, * special, * line, debugid[80];
747     insn   output_ins;
748     int    i, rn_error, validid;
749     long   seg, offs;
750     struct tokenval tokval;
751     expr   * e;
752     int pass, pass_max;
753     int pass_cnt = 0;           /* count actual passes */
754
755     if (cmd_sb == 32 && cmd_cpu < IF_386)
756       report_error(ERR_FATAL, "command line: "
757                     "32-bit segment size requires a higher cpu");
758
759    pass_max = (optimizing>0 ? optimizing : 0) + 2;    /* passes 1, optimizing, then 2 */
760    pass0 = !(optimizing>0);             /* start at 1 if not optimizing */
761    for (pass = 1; pass <= pass_max  &&  pass0 <= 2; pass++) {
762       int pass1, pass2;
763       ldfunc def_label;
764
765       pass1 = pass < pass_max ? 1 : 2;  /* seq is 1, 1, 1,..., 1, 2 */
766       pass2 = pass > 1 ? 2 : 1;         /* seq is 1, 2, 2,..., 2, 2 */
767   /*      pass0                            seq is 0, 0, 0,..., 1, 2 */
768
769       def_label = pass > 1 ? redefine_label : define_label;
770
771
772       sb = cmd_sb;        /* set 'bits' to command line default */
773       cpu = cmd_cpu;
774       if (pass0 == 2) {
775          if (*listname)
776             nasmlist.init(listname, report_error);
777       }
778       in_abs_seg = FALSE;
779       global_offset_changed = FALSE;      /* set by redefine_label */
780       location.segment = ofmt->section(NULL, pass2, &sb);
781       if (pass > 1) {
782          saa_rewind (forwrefs);
783          forwref = saa_rstruct (forwrefs);
784          raa_free (offsets);
785          offsets = raa_init();
786       }
787       preproc->reset(fname, pass1, report_error, evaluate, &nasmlist);
788       globallineno = 0;
789       if (pass == 1) location.known = TRUE;
790       location.offset = offs = GET_CURR_OFFS;
791
792       while ( (line = preproc->getline()) )
793       {
794          globallineno++;
795
796          /* here we parse our directives; this is not handled by the 'real'
797             * parser. */
798          if ( (i = getkw (line, &value)) )
799          {
800                switch (i) {
801                case 1:               /* [SEGMENT n] */
802                   seg = ofmt->section (value, pass2, &sb);
803                   if (seg == NO_SEG) {
804                      report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
805                                     "segment name `%s' not recognised",
806                                     value);
807                   } else {
808                      in_abs_seg = FALSE;
809                      location.segment = seg;
810                   }
811                   break;
812                case 2:               /* [EXTERN label:special] */
813                   if (*value == '$') value++;   /* skip initial $ if present */
814                   if (pass0 == 2) {
815                         q = value;
816                         while (*q && *q != ':')
817                            q++;
818                         if (*q == ':') {
819                            *q++ = '\0';
820                            ofmt->symdef(value, 0L, 0L, 3, q);
821                         }
822                   } else if (pass == 1) {   /* pass == 1 */
823                         q = value;
824                         validid = TRUE;
825                         if (!isidstart(*q))
826                            validid = FALSE;
827                         while (*q && *q != ':') {
828                            if (!isidchar(*q))
829                                  validid = FALSE;
830                            q++;
831                         }
832                         if (!validid) {
833                            report_error (ERR_NONFATAL,
834                                           "identifier expected after EXTERN");
835                            break;
836                         }
837                         if (*q == ':') {
838                            *q++ = '\0';
839                            special = q;
840                         } else
841                            special = NULL;
842                         if (!is_extern(value)) {   /* allow re-EXTERN to be ignored */
843                            int temp = pass0;
844                            pass0 = 1;   /* fake pass 1 in labels.c */
845                            declare_as_global (value, special, report_error);
846                            define_label (value, seg_alloc(), 0L, NULL, FALSE, TRUE,
847                                           ofmt, report_error);
848                            pass0 = temp;
849                         }
850                   } /* else  pass0 == 1 */
851                   break;
852                case 3:               /* [BITS bits] */
853                   sb = get_bits(value);
854                   break;
855                case 4:               /* [GLOBAL symbol:special] */
856                   if (*value == '$') value++;   /* skip initial $ if present */
857                   if (pass0 == 2) { /* pass 2 */
858                         q = value;
859                         while (*q && *q != ':')
860                            q++;
861                         if (*q == ':') {
862                            *q++ = '\0';
863                            ofmt->symdef(value, 0L, 0L, 3, q);
864                         }
865                   } else if (pass2 == 1) { /* pass == 1 */
866                         q = value;
867                         validid = TRUE;
868                         if (!isidstart(*q))
869                            validid = FALSE;
870                         while (*q && *q != ':') {
871                            if (!isidchar(*q))
872                                  validid = FALSE;
873                            q++;
874                         }
875                         if (!validid) {
876                            report_error (ERR_NONFATAL,
877                                           "identifier expected after GLOBAL");
878                            break;
879                         }
880                         if (*q == ':') {
881                            *q++ = '\0';
882                            special = q;
883                         } else
884                            special = NULL;
885                         declare_as_global (value, special, report_error);
886                   } /* pass == 1 */
887                   break;
888                case 5:               /* [COMMON symbol size:special] */
889                   if (*value == '$') value++;   /* skip initial $ if present */
890                   if (pass0 == 1) {
891                         p = value;
892                         validid = TRUE;
893                         if (!isidstart(*p))
894                            validid = FALSE;
895                         while (*p && !isspace(*p)) {
896                            if (!isidchar(*p))
897                                  validid = FALSE;
898                            p++;
899                         }
900                         if (!validid) {
901                            report_error (ERR_NONFATAL,
902                                           "identifier expected after COMMON");
903                            break;
904                         }
905                         if (*p) {
906                            long size;
907
908                            while (*p && isspace(*p))
909                                  *p++ = '\0';
910                            q = p;
911                            while (*q && *q != ':')
912                                  q++;
913                            if (*q == ':') {
914                                  *q++ = '\0';
915                                  special = q;
916                            } else
917                                  special = NULL;
918                            size = readnum (p, &rn_error);
919                            if (rn_error)
920                                  report_error (ERR_NONFATAL, "invalid size specified"
921                                              " in COMMON declaration");
922                            else
923                                  define_common (value, seg_alloc(), size,
924                                                 special, ofmt, report_error);
925                         } else
926                            report_error (ERR_NONFATAL, "no size specified in"
927                                           " COMMON declaration");
928                   } else if (pass0 == 2) { /* pass == 2 */
929                         q = value;
930                         while (*q && *q != ':') {
931                            if (isspace(*q))
932                                  *q = '\0';
933                            q++;
934                         }
935                         if (*q == ':') {
936                            *q++ = '\0';
937                            ofmt->symdef(value, 0L, 0L, 3, q);
938                         }
939                   }
940                   break;
941                case 6:                       /* [ABSOLUTE address] */
942                   stdscan_reset();
943                   stdscan_bufptr = value;
944                   tokval.t_type = TOKEN_INVALID;
945                   e = evaluate(stdscan, NULL, &tokval, NULL, pass2, report_error,
946                               NULL);
947                   if (e) {
948                      if (!is_reloc(e))
949                            report_error (pass0==1 ? ERR_NONFATAL : ERR_PANIC,
950                                  "cannot use non-relocatable expression as "
951                                  "ABSOLUTE address");
952                      else {
953                            abs_seg = reloc_seg(e);
954                            abs_offset = reloc_value(e);
955                      }
956                   } else
957                      if (pass==1) abs_offset = 0x100;/* don't go near zero in case of / */
958                      else report_error (ERR_PANIC, "invalid ABSOLUTE address "
959                                     "in pass two");
960                   in_abs_seg = TRUE;
961                   location.segment = abs_seg;
962                   break;
963                case 7:    /* DEBUG       */
964                   p = value;
965                   q = debugid;
966                   validid = TRUE;
967                   if (!isidstart(*p))
968                      validid = FALSE;
969                   while (*p && !isspace(*p)) {
970                      if (!isidchar(*p))
971                            validid = FALSE;
972                      *q++ = *p++;
973                   }
974                   *q++ = 0;
975                   if (!validid) {
976                      report_error (pass==1 ? ERR_NONFATAL : ERR_PANIC,
977                                     "identifier expected after DEBUG");
978                      break;
979                   }
980                   while (*p && isspace(*p)) p++;
981                   if (pass==pass_max) ofmt->current_dfmt->debug_directive (debugid, p);
982                   break;
983                case 8:                  /* [WARNING {+|-}warn-name] */
984                   if (pass1 == 1) {
985                      while (*value && isspace(*value))
986                         value++;
987
988                      if (*value == '+' || *value == '-') {
989                         validid = (*value == '-') ? TRUE : FALSE;
990                         value++;
991                      } else
992                         validid = FALSE;
993
994                      for (i=1; i<=ERR_WARN_MAX; i++)
995                         if (!nasm_stricmp(value, suppressed_names[i]))
996                             break;
997                      if (i <= ERR_WARN_MAX)
998                         suppressed[i] = validid;
999                      else
1000                         report_error (ERR_NONFATAL, "invalid warning id in WARNING directive");
1001                   }
1002                   break;
1003                case 9:  /* cpu */
1004                   cpu = get_cpu (value);
1005                   break;
1006                case 10:        /* fbk 9/2/00 */       /* [LIST {+|-}] */
1007                    while (*value && isspace(*value))
1008                        value++;
1009
1010                    if (*value == '+') {
1011                        user_nolist = 0;
1012                      }
1013                      else {
1014                           if (*value == '-') {
1015                           user_nolist = 1;
1016                           }
1017                           else {
1018                               report_error (ERR_NONFATAL, "invalid parameter to \"list\" directive");
1019                           }
1020                       }
1021                   break;
1022                default:
1023                   if (!ofmt->directive (line+1, value, pass2))
1024                      report_error (pass1==1 ? ERR_NONFATAL : ERR_PANIC,
1025                               "unrecognised directive [%s]",
1026                               line+1);
1027                }
1028          }
1029          else         /* it isn't a directive */
1030          {
1031                parse_line (pass1, line, &output_ins,
1032                            report_error, evaluate,
1033                            def_label);
1034
1035                if (!(optimizing>0) && pass == 2) {
1036                   if (forwref != NULL && globallineno == forwref->lineno) {
1037                      output_ins.forw_ref = TRUE;
1038                      do {
1039                         output_ins.oprs[forwref->operand].opflags |= OPFLAG_FORWARD;
1040                         forwref = saa_rstruct (forwrefs);
1041                      } while (forwref != NULL && forwref->lineno == globallineno);
1042                   } else
1043                      output_ins.forw_ref = FALSE;
1044                }
1045
1046
1047                if (!(optimizing>0) && output_ins.forw_ref)
1048                {
1049                   if (pass == 1) {
1050                         for(i = 0; i < output_ins.operands; i++)
1051                         {
1052                            if (output_ins.oprs[i].opflags & OPFLAG_FORWARD)
1053                            {
1054                                     struct forwrefinfo *fwinf =
1055                                        (struct forwrefinfo *)saa_wstruct(forwrefs);
1056                                  fwinf->lineno = globallineno;
1057                                  fwinf->operand = i;
1058                            }
1059                         }
1060                   } else { /* pass == 2 */
1061                         /*
1062                         * Hack to prevent phase error in the code
1063                         *   rol ax,x
1064                         *   x equ 1
1065                         *
1066                         * If the second operand is a forward reference,
1067                         * the UNITY property of the number 1 in that
1068                         * operand is cancelled. Otherwise the above
1069                         * sequence will cause a phase error.
1070                         *
1071                         * This hack means that the above code will
1072                         * generate 286+ code.
1073                         *
1074                         * The forward reference will mean that the
1075                         * operand will not have the UNITY property on
1076                         * the first pass, so the pass behaviours will
1077                         * be consistent.
1078                         */
1079
1080                         if (output_ins.operands >= 2 &&
1081                         (output_ins.oprs[1].opflags & OPFLAG_FORWARD))
1082                         {
1083                            output_ins.oprs[1].type &= ~(ONENESS|BYTENESS);
1084                         }
1085
1086                   } /* pass == 2 */
1087
1088                } /*  forw_ref */
1089
1090
1091                if (output_ins.opcode == I_EQU) {
1092                      if (pass1 == 1)
1093                      {
1094                         /*
1095                         * Special `..' EQUs get processed in pass two,
1096                         * except `..@' macro-processor EQUs which are done
1097                         * in the normal place.
1098                         */
1099                         if (!output_ins.label)
1100                            report_error (ERR_NONFATAL,
1101                                           "EQU not preceded by label");
1102
1103                         else if (output_ins.label[0] != '.' ||
1104                                  output_ins.label[1] != '.' ||
1105                                  output_ins.label[2] == '@')
1106                         {
1107                            if (output_ins.operands == 1 &&
1108                                  (output_ins.oprs[0].type & IMMEDIATE) &&
1109                                  output_ins.oprs[0].wrt == NO_SEG)
1110                            {
1111                               int isext = output_ins.oprs[0].opflags & OPFLAG_EXTERN;
1112                               def_label (output_ins.label,
1113                                              output_ins.oprs[0].segment,
1114                                              output_ins.oprs[0].offset,
1115                                              NULL, FALSE, isext, ofmt, report_error);
1116                            }
1117                            else if (output_ins.operands == 2 &&
1118                                        (output_ins.oprs[0].type & IMMEDIATE) &&
1119                                        (output_ins.oprs[0].type & COLON) &&
1120                                        output_ins.oprs[0].segment == NO_SEG &&
1121                                        output_ins.oprs[0].wrt == NO_SEG &&
1122                                        (output_ins.oprs[1].type & IMMEDIATE) &&
1123                                        output_ins.oprs[1].segment == NO_SEG &&
1124                                        output_ins.oprs[1].wrt == NO_SEG)
1125                            {
1126                                  def_label (output_ins.label,
1127                                              output_ins.oprs[0].offset | SEG_ABS,
1128                                              output_ins.oprs[1].offset,
1129                                              NULL, FALSE, FALSE, ofmt, report_error);
1130                            }
1131                            else
1132                                  report_error(ERR_NONFATAL, "bad syntax for EQU");
1133                         }
1134                      } else {  /* pass == 2 */
1135                         /*
1136                         * Special `..' EQUs get processed here, except
1137                         * `..@' macro processor EQUs which are done above.
1138                         */
1139                         if (output_ins.label[0] == '.' &&
1140                            output_ins.label[1] == '.' &&
1141                            output_ins.label[2] != '@')
1142                         {
1143                            if (output_ins.operands == 1 &&
1144                                  (output_ins.oprs[0].type & IMMEDIATE)) {
1145                                  define_label (output_ins.label,
1146                                              output_ins.oprs[0].segment,
1147                                              output_ins.oprs[0].offset,
1148                                              NULL, FALSE, FALSE, ofmt, report_error);
1149                            }
1150                            else if (output_ins.operands == 2 &&
1151                                        (output_ins.oprs[0].type & IMMEDIATE) &&
1152                                        (output_ins.oprs[0].type & COLON) &&
1153                                        output_ins.oprs[0].segment == NO_SEG &&
1154                                        (output_ins.oprs[1].type & IMMEDIATE) &&
1155                                        output_ins.oprs[1].segment == NO_SEG)
1156                            {
1157                                  define_label (output_ins.label,
1158                                              output_ins.oprs[0].offset | SEG_ABS,
1159                                              output_ins.oprs[1].offset,
1160                                              NULL, FALSE, FALSE, ofmt, report_error);
1161                            }
1162                            else
1163                                  report_error(ERR_NONFATAL, "bad syntax for EQU");
1164                         }
1165                      }  /* pass == 2 */
1166                } else { /* instruction isn't an EQU */
1167
1168                      if (pass1 == 1) {
1169
1170                         long l = insn_size (location.segment, offs, sb, cpu,
1171                                           &output_ins, report_error);
1172
1173                         /* if (using_debug_info)  && output_ins.opcode != -1)*/
1174                         if (using_debug_info)  /* fbk 03/25/01 */
1175
1176                         {
1177                            /* this is done here so we can do debug type info */
1178                            long typeinfo = TYS_ELEMENTS(output_ins.operands);
1179                            switch (output_ins.opcode) {
1180                                     case I_RESB:
1181                                         typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_BYTE;
1182                                         break;
1183                                     case I_RESW:
1184                                         typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_WORD;
1185                                         break;
1186                                     case I_RESD:
1187                                         typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_DWORD;
1188                                         break;
1189                                     case I_RESQ:
1190                                         typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_QWORD;
1191                                         break;
1192                                     case I_REST:
1193                                         typeinfo = TYS_ELEMENTS(output_ins.oprs[0].offset) | TY_TBYTE;
1194                                         break;
1195                                     case I_DB:
1196                                         typeinfo |= TY_BYTE;
1197                                         break;
1198                                     case I_DW:
1199                                         typeinfo |= TY_WORD;
1200                                         break;
1201                                     case I_DD:
1202                                        if (output_ins.eops_float)
1203                                                 typeinfo |= TY_FLOAT;
1204                                        else
1205                                                 typeinfo |= TY_DWORD;
1206                                        break;
1207                                     case I_DQ:
1208                                        typeinfo |= TY_QWORD;
1209                                        break;
1210                                     case I_DT:
1211                                        typeinfo |= TY_TBYTE;
1212                                        break;
1213                                     default:
1214                                        typeinfo = TY_LABEL;
1215
1216                            }
1217
1218                            ofmt->current_dfmt->debug_typevalue(typeinfo);
1219
1220                         }
1221                         if (l != -1) {
1222                            offs += l;
1223                            SET_CURR_OFFS (offs);
1224                         }
1225                         /*
1226                         * else l == -1 => invalid instruction, which will be
1227                         * flagged as an error on pass 2
1228                         */
1229
1230                      } else { /* pass == 2 */
1231                         offs += assemble (location.segment, offs, sb, cpu,
1232                                        &output_ins, ofmt, report_error, &nasmlist);
1233                         SET_CURR_OFFS (offs);
1234
1235                      }
1236                } /* not an EQU */
1237                cleanup_insn (&output_ins);
1238          }
1239          nasm_free (line);
1240          location.offset = offs = GET_CURR_OFFS;
1241       } /* end while (line = preproc->getline... */
1242
1243       if (pass1==2 && global_offset_changed)
1244          report_error(ERR_NONFATAL, "phase error detected at end of assembly.");
1245
1246       if (pass1 == 1) preproc->cleanup(1);
1247
1248       if (pass1==1 && terminate_after_phase) {
1249          fclose(ofile);
1250          remove(outname);
1251          if (want_usage)
1252                usage();
1253          exit (1);
1254       }
1255       pass_cnt++;
1256       if (pass>1 && !global_offset_changed) {
1257          pass0++;
1258          if (pass0==2) pass = pass_max - 1;
1259       } else if (!(optimizing>0)) pass0++;
1260
1261    } /* for (pass=1; pass<=2; pass++) */
1262
1263    preproc->cleanup(0);
1264    nasmlist.cleanup();
1265 #if 1
1266    if (optimizing>0 && using_debug_info)        /*  -On and -g switches */
1267       fprintf(stdout,
1268                 "info:: assembly required 1+%d+1 passes\n", pass_cnt-2);
1269 #endif
1270 } /* exit from assemble_file (...) */
1271
1272
1273 static int getkw (char *buf, char **value)
1274 {
1275     char *p, *q;
1276
1277     /*  allow leading spaces or tabs */
1278     while (*buf==' ' || *buf=='\t')
1279        buf++;
1280
1281     if (*buf!='[')
1282         return 0;
1283
1284     p = buf;
1285
1286     while (*p && *p != ']') p++;
1287
1288     if (!*p)
1289         return 0;
1290
1291     q = p++;
1292
1293     while (*p && *p != ';') {
1294         if (!isspace(*p))
1295             return 0;
1296         p++;
1297     }
1298     q[1] = '\0';
1299
1300     p = buf+1;
1301     while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
1302         buf++;
1303     if (*buf==']') {
1304         *buf = '\0';
1305         *value = buf;
1306     } else {
1307         *buf++ = '\0';
1308         while (isspace(*buf)) buf++;   /* beppu - skip leading whitespace */
1309         *value = buf;
1310         while (*buf!=']') buf++;
1311         *buf++ = '\0';
1312     }
1313 #if 0
1314     for (q=p; *q; q++)
1315         *q = tolower(*q);
1316 #endif  
1317     if (!nasm_stricmp(p, "segment") || !nasm_stricmp(p, "section"))
1318         return 1;
1319     if (!nasm_stricmp(p, "extern"))
1320         return 2;
1321     if (!nasm_stricmp(p, "bits"))
1322         return 3;
1323     if (!nasm_stricmp(p, "global"))
1324         return 4;
1325     if (!nasm_stricmp(p, "common"))
1326         return 5;
1327     if (!nasm_stricmp(p, "absolute"))
1328         return 6;
1329     if (!nasm_stricmp(p, "debug"))
1330         return 7;
1331     if (!nasm_stricmp(p, "warning"))
1332         return 8;
1333     if (!nasm_stricmp(p, "cpu"))
1334         return 9;
1335     if (!nasm_stricmp(p, "list"))    /* fbk 9/2/00 */
1336         return 10;
1337     return -1;
1338 }
1339
1340 static void report_error (int severity, char *fmt, ...)
1341 {
1342     va_list ap;
1343
1344     /*
1345      * See if it's a suppressed warning.
1346      */
1347     if ((severity & ERR_MASK) == ERR_WARNING &&
1348         (severity & ERR_WARN_MASK) != 0 &&
1349         suppressed[ (severity & ERR_WARN_MASK) >> ERR_WARN_SHR ])
1350         return;                        /* and bail out if so */
1351
1352     /*
1353      * See if it's a pass-one only warning and we're not in pass one.
1354      */
1355     if ((severity & ERR_PASS1) && pass0 == 2)
1356         return;
1357
1358     if (severity & ERR_NOFILE)
1359         fputs ("nasm: ", error_file);
1360     else {
1361         char * currentfile = NULL;
1362         long lineno = 0;
1363         src_get (&lineno, &currentfile);
1364         fprintf (error_file, "%s:%ld: ", currentfile, lineno);
1365         nasm_free (currentfile);
1366     }
1367
1368     switch (severity & ERR_MASK) {
1369       case ERR_WARNING:
1370         fputs ("warning: ", error_file); break;
1371       case ERR_NONFATAL:
1372         fputs ("error: ", error_file); break;
1373       case ERR_FATAL:
1374         fputs ("fatal: ", error_file); break;
1375       case ERR_PANIC:
1376         fputs ("panic: ", error_file); break;
1377       case ERR_DEBUG:
1378         fputs("debug: ", error_file); break;
1379     }
1380
1381     va_start (ap, fmt);
1382     vfprintf (error_file, fmt, ap);
1383     fputc ('\n', error_file);
1384
1385     if (severity & ERR_USAGE)
1386         want_usage = TRUE;
1387
1388     switch (severity & ERR_MASK) {
1389       case ERR_WARNING: case ERR_DEBUG:
1390         /* no further action, by definition */
1391         break;
1392       case ERR_NONFATAL:
1393 /*     terminate_after_phase = TRUE;   *//**//* hack enables listing(!) on errors */
1394         terminate_after_phase = TRUE;
1395         break;
1396       case ERR_FATAL:
1397         if (ofile) {
1398             fclose(ofile);
1399             remove(outname);
1400         }
1401         if (want_usage)
1402             usage();
1403         exit(1);                       /* instantly die */
1404         break;                         /* placate silly compilers */
1405       case ERR_PANIC:
1406         fflush(NULL);
1407 /*      abort();        */             /* halt, catch fire, and dump core */
1408         exit(3);
1409         break;
1410     }
1411 }
1412
1413 static void usage(void)
1414 {
1415     fputs("type `nasm -h' for help\n", error_file);
1416 }
1417
1418 static void register_output_formats(void)
1419 {
1420     ofmt = ofmt_register (report_error);
1421 }
1422
1423 #define BUF_DELTA 512
1424
1425 static FILE *no_pp_fp;
1426 static efunc no_pp_err;
1427 static ListGen *no_pp_list;
1428 static long no_pp_lineinc;
1429
1430 static void no_pp_reset (char *file, int pass, efunc error, evalfunc eval,
1431                          ListGen *listgen)
1432 {
1433     src_set_fname(nasm_strdup(file));
1434     src_set_linnum(0);
1435     no_pp_lineinc = 1;
1436     no_pp_err = error;
1437     no_pp_fp = fopen(file, "r");
1438     if (!no_pp_fp)
1439         no_pp_err (ERR_FATAL | ERR_NOFILE,
1440                    "unable to open input file `%s'", file);
1441     no_pp_list = listgen;
1442     (void) pass;                       /* placate compilers */
1443     (void) eval;                       /* placate compilers */
1444 }
1445
1446 static char *no_pp_getline (void)
1447 {
1448     char *buffer, *p, *q;
1449     int bufsize;
1450
1451     bufsize = BUF_DELTA;
1452     buffer = nasm_malloc(BUF_DELTA);
1453     src_set_linnum(src_get_linnum() + no_pp_lineinc);
1454
1455     while (1) {   /* Loop to handle %line */
1456
1457         p = buffer;
1458         while (1) {  /* Loop to handle long lines */
1459             q = fgets(p, bufsize-(p-buffer), no_pp_fp);
1460             if (!q)
1461                 break;
1462             p += strlen(p);
1463             if (p > buffer && p[-1] == '\n')
1464                 break;
1465             if (p-buffer > bufsize-10) {
1466                 int offset;
1467                 offset = p - buffer;
1468                 bufsize += BUF_DELTA;
1469                 buffer = nasm_realloc(buffer, bufsize);
1470                 p = buffer + offset;
1471             }
1472         }
1473
1474         if (!q && p == buffer) {
1475             nasm_free (buffer);
1476             return NULL;
1477         }
1478
1479         /*
1480          * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1481          * them are present at the end of the line.
1482          */
1483         buffer[strcspn(buffer, "\r\n\032")] = '\0';
1484
1485         if (!strncmp(buffer, "%line", 5)) {
1486             long ln;
1487             int  li;
1488             char *nm = nasm_malloc(strlen(buffer));
1489             if (sscanf(buffer+5, "%ld+%d %s", &ln, &li, nm) == 3) {
1490                 nasm_free( src_set_fname(nm) );
1491                 src_set_linnum(ln);
1492                 no_pp_lineinc = li;
1493                 continue;
1494             }
1495             nasm_free(nm);
1496         }
1497         break;
1498     }
1499
1500     no_pp_list->line (LIST_READ, buffer);
1501
1502     return buffer;
1503 }
1504
1505 static void no_pp_cleanup (int pass)
1506 {
1507     fclose(no_pp_fp);
1508 }
1509
1510 static unsigned long get_cpu (char *value)
1511 {
1512
1513     if (!strcmp(value, "8086")) return IF_8086;
1514     if (!strcmp(value, "186")) return IF_186;
1515     if (!strcmp(value, "286")) return IF_286;
1516     if (!strcmp(value, "386")) return IF_386;
1517     if (!strcmp(value, "486")) return IF_486;
1518     if (!strcmp(value, "586")    ||
1519         !nasm_stricmp(value, "pentium") )       return IF_PENT;
1520     if (!strcmp(value, "686")  ||
1521         !nasm_stricmp(value, "ppro") ||
1522         !nasm_stricmp(value, "p2")    )         return IF_P6;
1523     if (!nasm_stricmp(value, "p3")    ||
1524         !nasm_stricmp(value, "katmai") )        return IF_KATMAI;
1525     if (!nasm_stricmp(value, "p4")    ||        /* is this right? -- jrc */
1526         !nasm_stricmp(value, "willamette") )    return IF_WILLAMETTE;
1527
1528     report_error (pass0<2 ? ERR_NONFATAL : ERR_FATAL, "unknown 'cpu' type");
1529
1530     return IF_PLEVEL;   /* the maximum level */
1531 }
1532
1533
1534 static int get_bits (char *value)
1535 {
1536     int i;
1537
1538     if ((i = atoi(value)) == 16)  return i;   /* set for a 16-bit segment */
1539     else if (i == 32) {
1540         if (cpu < IF_386) {
1541             report_error(ERR_NONFATAL,
1542                 "cannot specify 32-bit segment on processor below a 386");
1543             i = 16;
1544         }
1545     } else {
1546         report_error(pass0<2 ? ERR_NONFATAL : ERR_FATAL,
1547            "`%s' is not a valid segment size; must be 16 or 32",
1548            value);
1549         i = 16;
1550     }
1551     return i;
1552 }
1553
1554 /* end of nasm.c */