Added execution time functions for win32
[fw/sdcc] / link / z80 / lkmain.c
1 /* lkmain.c */
2 /*
3  * (C) Copyright 1989-1995
4  * All Rights Reserved
5  *
6  * Alan R. Baldwin
7  * 721 Berkeley St.
8  * Kent, Ohio  44240
9  */
10
11 /*
12  * Extensions: P. Felber
13  */
14
15 #include <stdio.h>
16 #include <string.h>
17 //#include <alloc.h>
18 #include "aslink.h"
19 #include <stdlib.h>
20
21 #ifndef SDK_VERSION_STRING
22 #define SDK_VERSION_STRING      "3.0.0"
23 #endif
24 #ifndef TARGET_STRING
25 #define TARGET_STRING           "gbz80"
26 #endif
27
28 #ifdef WIN32T
29 #include <time.h>
30
31 void Timer(int action, char * message)
32 {
33         static double start, end, total=0.0;
34     static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
35
36     if(action==0) start=clock()*secs_per_tick;
37     else if(action==1)
38     {
39         end=clock() * secs_per_tick;
40                 printf("%s \t%f seconds.\n", message, (end-start));
41                 total+=end-start;
42     }
43     else
44     {
45                 printf("Total time: \t%f seconds.\n", total);
46                 total=0.0;
47     }
48 }
49 #endif
50
51 /*)Module       lkmain.c
52  *
53  *      The module lkmain.c contains the functions which
54  *      (1) input the linker options, parameters, and specifications
55  *      (2) perform a two pass link
56  *      (3) produce the appropriate linked data output and/or
57  *          link map file and/or relocated listing files.
58  *
59  *      lkmain.c contains the following functions:
60  *              FILE *  afile(fn,ft,wf)
61  *              VOID    bassav()
62  *              VOID    gblsav()
63  *              VOID    link()
64  *              VOID    lkexit()
65  *              VOID    main(argc,argv)
66  *              VOID    map()
67  *              int     parse()
68  *              VOID    setbas()
69  *              VOID    setgbl()
70  *              VOID    usage()
71  *
72  *      lkmain.c contains the following local variables:
73  *              char *  usetext[]       array of pointers to the
74  *                                      command option tect lines
75  *
76  */
77
78 /*)Function     VOID    main(argc,argv)
79  *
80  *              int     argc            number of command line arguments + 1
81  *              char *  argv[]          array of pointers to the command line
82  *                                      arguments
83  *
84  *      The function main() evaluates the command line arguments to
85  *      determine if the linker parameters are to input through 'stdin'
86  *      or read from a command file.  The functiond getline() and parse()
87  *      are to input and evaluate the linker parameters.  The linking process
88  *      proceeds by making the first pass through each .rel file in the order
89  *      presented to the linker.  At the end of the first pass the setbase(),
90  *      lnkarea(), setgbl(), and symdef() functions are called to evaluate
91  *      the base address terms, link all areas, define global variables,
92  *      and look for undefined symbols.  Following these routines a linker
93  *      map file may be produced and the linker output files may be opened.
94  *      The second pass through the .rel files will output the linked data
95  *      in one of the four supported formats.
96  *
97  *      local variables:
98  *              char *  p               pointer to an argument string
99  *              int     c               character from argument string
100  *              int     i               loop counter
101  *
102  *      global variables:
103  *                                      text line in ib[]
104  *              lfile   *cfp            The pointer *cfp points to the
105  *                                      current lfile structure
106  *              char    ctype[]         array of character types, one per
107  *                                      ASCII character
108  *              lfile   *filep          The pointer *filep points to the
109  *                                      beginning of a linked list of
110  *                                      lfile structures.
111  *              head    *hp             Pointer to the current
112  *                                      head structure
113  *              char    ib[NINPUT]      .rel file text line
114  *              char    *ip             pointer into the .rel file
115  *              lfile   *linkp          pointer to first lfile structure
116  *                                      containing an input .rel file
117  *                                      specification
118  *              int     lkerr           error flag
119  *              int     mflag           Map output flag
120  *              int     oflag           Output file type flag
121  *              FILE    *ofp            Output file handle
122  *                                      for word formats
123  *              FILE    *ofph           Output file handle
124  *                                      for high byte format
125  *              FILE    *ofpl           Output file handle
126  *                                      for low byte format
127  *              int     pass            linker pass number
128  *              int     pflag           print linker command file flag
129  *              int     radix           current number conversion radix
130  *              FILE    *sfp            The file handle sfp points to the
131  *                                      currently open file
132  *              lfile   *startp         asmlnk startup file structure
133  *              FILE *  stdin           c_library
134  *              FILE *  stdout          c_library
135  *
136  *      functions called:
137  *              FILE *  afile()         lkmain.c
138  *              int     fclose()        c_library
139  *              int     fprintf()       c_library
140  *              int     getline()       lklex.c
141  *              VOID    library()       lklibr.c
142  *              VOID    link()          lkmain.c
143  *              VOID    lkexit()        lkmain.c
144  *              VOID    lnkarea()       lkarea.c
145  *              VOID    map()           lkmain.c
146  *              VOID    new()           lksym.c
147  *              int     parse()         lkmain.c
148  *              VOID    reloc()         lkreloc.c
149  *              VOID    search()        lklibr.c
150  *              VOID    setbas()        lkmain.c
151  *              VOID    setgbl()        lkmain.c
152  *              VOID    symdef()        lksym.c
153  *              VOID    usage()         lkmain.c
154  *
155  *      side effects:
156  *              Completion of main() completes the linking process
157  *              and may produce a map file (.map) and/or a linked
158  *              data files (.ihx or .s19) and/or one or more
159  *              relocated listing files (.rst).
160  */
161
162 #ifdef SDK
163 int binary = 0;
164 #endif /* SDK */
165 #ifdef GAMEBOY
166 char *default_basep[] = {
167   "_CODE=0x0200",
168   "_DATA=0xC0A0",
169   NULL
170 };
171
172 char *default_globlp[] = {
173   /* DMA transfer must start at multiples of 0x100 */
174   ".OAM=0xC000",
175   ".STACK=0xE000",
176   ".refresh_OAM=0xFF80",
177
178   ".init=0x0000",
179
180   NULL
181 };
182 #endif /* GAMEBOY */
183
184 int
185 main(argc, argv)
186 char *argv[];
187 {
188         register char *p;
189         register int c, i;
190
191 #ifdef WIN32T
192     Timer(0, "");
193 #endif
194
195 #ifdef GAMEBOY
196         nb_rom_banks = 2;
197         nb_ram_banks = 0;
198         mbc_type = 0;
199         symflag=0;
200
201         for(i = 0; default_basep[i] != NULL; i++) {
202                 if(basep == NULL) {
203                         basep = (struct base *)new(sizeof(struct base));
204                         bsp = basep;
205                 } else {
206                         bsp->b_base = (struct base *)new(sizeof(struct base));
207                         bsp = bsp->b_base;
208                 }
209                 bsp->b_strp = default_basep[i];
210         }
211         for(i = 0; default_globlp[i] != NULL; i++) {
212                 if(globlp == NULL) {
213                         globlp = (struct globl *)new(sizeof(struct globl));
214                         gsp = globlp;
215                 } else {
216                         gsp->g_globl = (struct globl *)new(sizeof(struct globl));
217                         gsp = gsp->g_globl;
218                 }
219                 gsp->g_strp = default_globlp[i];
220         }
221 #endif /* GAMEBOY */
222 #ifndef SDK
223         fprintf(stdout, "\n");
224 #endif /* SDK */
225
226         startp = (struct lfile *) new (sizeof (struct lfile));
227
228         pflag = 1;
229         for (i=1; i<argc; ++i) {
230                 p = argv[i];
231                 if (*p == '-') {
232                         while (ctype[c = *(++p)] & LETTER) {
233                                 switch(c) {
234
235                                 case 'c':
236                                 case 'C':
237                                         startp->f_type = F_STD;
238                                         break;
239
240                                 case 'f':
241                                 case 'F':
242                                         startp->f_type = F_LNK;
243                                         break;
244                                         
245                                 case 'n':
246                                 case 'N':
247                                         pflag = 0;
248                                         break;
249
250                                 case 'p':
251                                 case 'P':
252                                         pflag = 1;
253                                         break;
254
255                                 default:
256                                         usage();
257                                 }
258                         }
259
260 #ifdef SDK                      
261                         if(c == '-') {
262                                 startp->f_type = F_CMD;
263                                 startp->f_idp = (char *)&argv[i+1];
264                                 break;
265                         }
266 #endif /* SDK */
267
268                 } else {
269                         if (startp->f_type == F_LNK) {
270                                 startp->f_idp = p;
271                         }
272                 }
273         }
274         if (startp->f_type == F_INV)
275                 usage();
276         if (startp->f_type == F_LNK && startp->f_idp == NULL)
277                 usage();
278 #ifdef SDK
279         if (startp->f_type == F_CMD && startp->f_idp == NULL)
280                 usage();
281 #endif /* SDK */
282
283         cfp = NULL;
284         sfp = NULL;
285         filep = startp;
286         while (1) {
287                 ip = ib;                                        
288                 if (getline() == 0)
289                         break;
290                 if (pflag && sfp != stdin)
291                         fprintf(stdout, "%s\n", ip);
292                 if (*ip == '\0' || parse())
293                         break;
294         }
295         if (sfp)
296                 fclose(sfp);
297         if (linkp == NULL)
298                 usage();
299 #ifdef SDK
300         if (linkp->f_flp == NULL)
301                 usage();
302 #endif /* SDK */
303
304 #ifdef GAMEBOY
305         for(i = 1; i < nb_rom_banks; i++) {
306                 bsp->b_base = (struct base *)new(sizeof(struct base));
307                 bsp = bsp->b_base;
308                 bsp->b_strp = (char *)malloc(18);
309                 sprintf(bsp->b_strp, "_CODE_%d=0x4000", i);
310         }
311         for(i = 0; i < nb_ram_banks; i++) {
312                 bsp->b_base = (struct base *)new(sizeof(struct base));
313                 bsp = bsp->b_base;
314                 bsp->b_strp = (char *)malloc(18);
315                 sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
316         }
317 #endif /* GAMEBOY */
318
319         syminit();
320         for (pass=0; pass<2; ++pass) {
321                 cfp = NULL;
322                 sfp = NULL;
323 #ifdef SDK
324                 filep = linkp->f_flp;
325 #else /* SDK */
326                 filep = linkp;
327 #endif /* SDK */
328                 hp = NULL;
329                 radix = 10;
330
331                 while (getline()) {
332                         ip = ib;
333                         link();
334                 }
335                 if (pass == 0) {
336                         /*
337                          * Search libraries for global symbols
338                          */
339                         search();
340                         /*
341                          * Set area base addresses.
342                          */
343                         setbas();
344                         /*
345                          * Link all area addresses.
346                          */
347                         lnkarea();
348                         /*
349                          * Process global definitions.
350                          */
351                         setgbl();
352                         /*
353                          * Check for undefined globals.
354                          */
355                         symdef(stderr);
356 #ifdef SDK
357                         if (symflag) 
358                                 sym();
359 #endif
360                         /*
361                          * Output Link Map.
362                          */
363                         if (mflag)
364                                 map();
365                         /*
366                          * Open output file
367                          */
368                         if (oflag == 1) {
369 #ifdef SDK
370                                 ofp = afile(linkp->f_idp, "ihx", 1);
371 #else /* SDK */
372                                 ofp = afile(linkp->f_idp, "IHX", 1);
373 #endif /* SDK */
374                                 if (ofp == NULL) {
375                                         lkexit(1);
376                                 }
377                         } else
378                         if (oflag == 2) {
379 #ifdef SDK
380                                 ofp = afile(linkp->f_idp, "s19", 1);
381 #else /* SDK */
382                                 ofp = afile(linkp->f_idp, "S19", 1);
383 #endif /* SDK */
384                                 if (ofp == NULL) {
385                                         lkexit(1);
386                                 }
387 #ifdef SDK
388                         } else
389                         if (oflag == 3) {
390                                 binary = 1;
391                                 ofp = afile(linkp->f_idp, "", 1);
392                                 binary = 0;
393                                 if (ofp == NULL) {
394                                         lkexit(1);
395                                 }
396 #endif /* SDK */
397                         }
398                 } else {
399                         /*
400                          * Link in library files
401                          */
402                         library();
403                         reloc('E');
404                 }
405         }
406 #ifdef WIN32T
407     Timer(1, "Linker time");
408 #endif
409         lkexit(lkerr);
410
411         /* Never get here. */
412         return 0;
413 }
414
415 /*)Function     VOID    lkexit(i)
416  *
417  *                      int     i       exit code
418  *
419  *      The function lkexit() explicitly closes all open
420  *      files and then terminates the program.
421  *
422  *      local variables:
423  *              none
424  *
425  *      global variables:
426  *              FILE *  mfp             file handle for .map
427  *              FILE *  ofp             file handle for .ihx/.s19
428  *              FILE *  rfp             file hanlde for .rst
429  *              FILE *  sfp             file handle for .rel
430  *              FILE *  tfp             file handle for .lst
431  *
432  *      functions called:
433  *              int     fclose()        c_library
434  *              VOID    exit()          c_library
435  *
436  *      side effects:
437  *              All files closed. Program terminates.
438  */
439
440 VOID 
441 lkexit(i)
442 int i;
443 {
444         if (mfp != NULL) fclose(mfp);
445         if (ofp != NULL) fclose(ofp);
446         if (rfp != NULL) fclose(rfp);
447         if (sfp != NULL) fclose(sfp);
448         if (tfp != NULL) fclose(tfp);
449         exit(i);
450 }
451
452 /*)Function     link()
453  *
454  *      The function link() evaluates the directives for each line of
455  *      text read from the .rel file(s).  The valid directives processed
456  *      are:
457  *              X, D, Q, H, M, A, S, T, R, and P.
458  *
459  *      local variables:
460  *              int     c               first non blank character of a line
461  *
462  *      global variables:
463  *              head    *headp          The pointer to the first
464  *                                      head structure of a linked list
465  *              head    *hp             Pointer to the current
466  *                                      head structure
467  *              int     pass            linker pass number
468  *              int     radix           current number conversion radix
469  *
470  *      functions called:
471  *              char    endline()       lklex.c
472  *              VOID    module()        lkhead.c
473  *              VOID    newarea()       lkarea.c
474  *              VOID    newhead()       lkhead.c
475  *              sym *   newsym()        lksym.c
476  *              VOID    reloc()         lkreloc.c
477  *
478  *      side effects:
479  *              Head, area, and symbol structures are created and
480  *              the radix is set as the .rel file(s) are read.
481  */
482
483 VOID
484 link()
485 {
486         register int c;
487
488         if ((c=endline()) == 0) { return; }
489         switch (c) {
490
491         case 'X':
492                 radix = 16;
493                 break;
494
495         case 'D':
496                 radix = 10;
497                 break;
498
499         case 'Q':
500                 radix = 8;
501                 break;
502
503         case 'H':
504                 if (pass == 0) {
505                         newhead();
506                 } else {
507                         if (hp == 0) {
508                                 hp = headp;
509                         } else {
510                                 hp = hp->h_hp;
511                         }
512                 }
513                 sdp.s_area = NULL;
514                 sdp.s_areax = NULL;
515                 sdp.s_addr = 0;
516                 break;
517
518         case 'M':
519                 if (pass == 0)
520                         module();
521                 break;
522
523         case 'A':
524                 if (pass == 0)
525                         newarea();
526                 if (sdp.s_area == NULL) {
527                         sdp.s_area = areap;
528                         sdp.s_areax = areap->a_axp;
529                         sdp.s_addr = 0;
530                 }
531                 break;
532
533         case 'S':
534                 if (pass == 0)
535                         newsym();
536                 break;
537
538         case 'T':
539         case 'R':
540         case 'P':
541                 if (pass == 0)
542                         break;
543                 reloc(c);
544                 break;
545
546         default:
547                 break;
548         }
549         if (c == 'X' || c == 'D' || c == 'Q') {
550                 if ((c = get()) == 'H') {
551                         hilo = 1;
552                 } else
553                 if (c == 'L') {
554                         hilo = 0;
555                 }
556         }
557 }
558
559 /*)Function     VOID    map()
560  *
561  *      The function map() opens the output map file and calls the various
562  *      routines to
563  *      (1) output the variables in each area,
564  *      (2) list the files processed with module names,
565  *      (3) list the libraries file processed,
566  *      (4) list base address definitions,
567  *      (5) list global variable definitions, and
568  *      (6) list any undefined variables.
569  *
570  *      local variables:
571  *              int     i               counter
572  *              head *  hdp             pointer to head structure
573  *              lbfile *lbfh            pointer to library file structure
574  *
575  *      global variables:
576  *              area    *ap             Pointer to the current
577  *                                      area structure
578  *              area    *areap          The pointer to the first
579  *                                      area structure of a linked list
580  *              base    *basep          The pointer to the first
581  *                                      base structure
582  *              base    *bsp            Pointer to the current
583  *                                      base structure
584  *              lfile   *filep          The pointer *filep points to the
585  *                                      beginning of a linked list of
586  *                                      lfile structures.
587  *              globl   *globlp         The pointer to the first
588  *                                      globl structure
589  *              globl   *gsp            Pointer to the current
590  *                                      globl structure
591  *              head    *headp          The pointer to the first
592  *                                      head structure of a linked list
593  *              lbfile  *lbfhead        The pointer to the first
594  *                                      lbfile structure of a linked list
595  *              lfile   *linkp          pointer to first lfile structure
596  *                                      containing an input REL file
597  *                                      specification
598  *              int     lop             current line number on page
599  *              FILE    *mfp            Map output file handle
600  *              int     page            current page number
601  *
602  *      functions called:
603  *              FILE *  afile()         lkmain.c
604  *              int     fprintf()       c_library
605  *              VOID    lkexit()        lkmain.c
606  *              VOID    lstarea()       lklist.c
607  *              VOID    newpag()        lklist.c
608  *              VOID    symdef()        lksym.c
609  *
610  *      side effects:
611  *              The map file is created.
612  */
613
614 #ifndef MLH_MAP
615 VOID
616 map()
617 {
618         register i;
619         register struct head *hdp;
620         register struct lbfile *lbfh;
621
622         /*
623          * Open Map File
624          */
625 #ifdef SDK
626         mfp = afile(linkp->f_idp, "map", 1);
627 #else /* SDK */
628         mfp = afile(linkp->f_idp, "MAP", 1);
629 #endif /* SDK */
630         if (mfp == NULL) {
631                 lkexit(1);
632         }
633
634         /*
635          * Output Map Area Lists
636          */
637         page = 0;
638         lop  = NLPP;
639         ap = areap;
640         while (ap) {
641                 lstarea(ap);
642                 ap = ap->a_ap;
643         }
644         /*
645          * List Linked Files
646          */
647         newpag(mfp);
648         fprintf(mfp, "\nFiles Linked      [ module(s) ]\n\n");
649         hdp = headp;
650 #ifdef SDK
651         filep = linkp->f_flp;
652 #else /* SDK */
653         filep = linkp;
654 #endif /* SDK */
655         while (filep) {
656                 fprintf(mfp, "%-16s", filep->f_idp);
657                 i = 0;
658                 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
659                         if (i % 5) {
660                             fprintf(mfp, ", %8.8s", hdp->m_id);
661                         } else {
662                             if (i) {
663                                 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
664                             } else {
665                                 fprintf(mfp, "  [ %8.8s", hdp->m_id);
666                             }
667                         }
668                         hdp = hdp->h_hp;
669                         i++;
670                 }
671                 if (i)
672                         fprintf(mfp, " ]");
673                 fprintf(mfp, "\n");
674                 filep = filep->f_flp;
675         }
676         /*
677          * List Linked Libraries
678          */
679         if (lbfhead != NULL) {
680                 fprintf(mfp,
681         "\nLibraries Linked                    [   object  file   ]\n\n");
682                 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
683                         fprintf(mfp, "%-32s    [ %16.16s ]\n",
684                                 lbfh->libspc, lbfh->relfil);
685                 }
686                 fprintf(mfp, "\n");
687         }
688         /*
689          * List Base Address Definitions
690          */
691         if (basep) {
692                 newpag(mfp);
693                 fprintf(mfp, "\nUser Base Address Definitions\n\n");
694                 bsp = basep;
695                 while (bsp) {
696                         fprintf(mfp, "%s\n", bsp->b_strp);
697                         bsp = bsp->b_base;
698                 }
699         }
700         /*
701          * List Global Definitions
702          */
703         if (globlp) {
704                 newpag(mfp);
705                 fprintf(mfp, "\nUser Global Definitions\n\n");
706                 gsp = globlp;
707                 while (gsp) {
708                         fprintf(mfp, "%s\n", gsp->g_strp);
709                         gsp = gsp->g_globl;
710                 }
711         }
712         fprintf(mfp, "\n\f");
713         symdef(mfp);
714 }
715 #else
716 VOID map()
717 {
718         register struct head *hdp;
719         register struct lbfile *lbfh;
720
721         /*
722          * Open Map File
723          */
724 #ifdef SDK
725         mfp = afile(linkp->f_idp, "map", 1);
726 #else /* SDK */
727         mfp = afile(linkp->f_idp, "MAP", 1);
728 #endif /* SDK */
729         if (mfp == NULL) {
730                 lkexit(1);
731         }
732
733         /*
734          *Output Map Area Lists
735          */
736         page = 0;
737         lop  = NLPP;
738         ap = areap;
739         while (ap) {
740                 lstarea(ap);
741                 ap = ap->a_ap;
742         }
743         /*
744          * List Linked Files
745          */
746         hdp = headp;
747 #ifdef SDK
748         filep = linkp->f_flp;
749 #else /* SDK */
750         filep = linkp;
751 #endif /* SDK */
752         if (filep) {
753                 fprintf( mfp, "MODULES\n");
754         }
755         while (filep) {
756                 fprintf(mfp, "\tFILE %s\n", filep->f_idp);
757                 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
758                         if (strlen(hdp->m_id)>0)
759                                 fprintf(mfp, "\t\tNAME %s\n", hdp->m_id);
760                         hdp = hdp->h_hp;
761                 }
762                 filep = filep->f_flp;
763         }
764         /*
765          * List Linked Libraries
766          */
767         if (lbfhead != NULL) {
768                 fprintf(mfp, "LIBRARIES\n");
769                 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
770                         fprintf(mfp,    "\tLIBRARY %s\n"
771                                         "\t\tMODULE %s\n",
772                                 lbfh->libspc, lbfh->relfil);
773                 }
774         }
775         /*
776          * List Base Address Definitions
777          */
778         if (basep) {
779                 fprintf(mfp, "USERBASEDEF\n");
780                 bsp = basep;
781                 while (bsp) {
782                         fprintf(mfp, "\t%s\n", bsp->b_strp);
783                         bsp = bsp->b_base;
784                 }
785         }
786         /*
787          * List Global Definitions
788          */
789         if (globlp) {
790                 fprintf(mfp, "USERGLOBALDEF\n");
791                 gsp = globlp;
792                 while (gsp) {
793                         fprintf(mfp, "\t%s\n", gsp->g_strp);
794                         gsp = gsp->g_globl;
795                 }
796         }
797         symdef(mfp);
798 #ifdef SDK
799         if (mfp!=NULL) {
800                 fclose(mfp);
801                 mfp = NULL;
802         }
803 #endif
804 }
805 #endif /* MLH_MAP */
806
807 #ifdef SDK
808 /* PENDING */
809 VOID lstareatosym(struct area *xp);
810
811 VOID sym()
812 {
813         /*
814          * Open sym File
815          */
816         mfp = afile(linkp->f_idp, "sym", 1);
817         if (mfp == NULL) {
818                 lkexit(1);
819         }
820         fprintf( mfp,   "; no$gmb format .sym file\n"
821                         "; Generated automagically by ASxxxx linker %s (SDK " SDK_VERSION_STRING ")\n"
822                 , VERSION );
823         /*
824          * Output sym Area Lists
825          */
826         page = 0;
827         lop  = NLPP;
828         ap = areap;
829         while (ap) {
830                 lstareatosym(ap);
831                 ap = ap->a_ap;
832         }
833         if (mfp!=NULL) {
834                 fclose(mfp);
835                 mfp = NULL;
836         }
837 }
838 #endif /* SDK */
839
840 /*)Function     int     parse()
841  *
842  *      The function parse() evaluates all command line or file input
843  *      linker directives and updates the appropriate variables.
844  *
845  *      local variables:
846  *              int     c               character value
847  *              char    fid[]           file id string
848  *
849  *      global variables:
850  *              char    ctype[]         array of character types, one per
851  *                                      ASCII character
852  *              lfile   *lfp            pointer to current lfile structure
853  *                                      being processed by parse()
854  *              lfile   *linkp          pointer to first lfile structure
855  *                                      containing an input REL file
856  *                                      specification
857  *              int     mflag           Map output flag
858  *              int     oflag           Output file type flag
859  *              int     pflag           print linker command file flag
860  *              FILE *  stderr          c_library
861  *              int     uflag           Relocated listing flag
862  *              int     xflag           Map file radix type flag
863  *
864  *      Functions called:
865  *              VOID    addlib()        lklibr.c
866  *              VOID    addpath()       lklibr.c
867  *              VOID    bassav()        lkmain.c
868  *              int     fprintf()       c_library
869  *              VOID    gblsav()        lkmain.c
870  *              VOID    getfid()        lklex.c
871  *              char    getnb()         lklex.c
872  *              VOID    lkexit()        lkmain.c
873  *              char *  strcpy()        c_library
874  *              int     strlen()        c_library
875  *
876  *      side effects:
877  *              Various linker flags are updated and the linked
878  *              structure lfile is created.
879  */
880
881 int
882 parse()
883 {
884         register int c;
885         char fid[NINPUT];
886
887         while ((c = getnb()) != 0) {
888                 if ( c == '-') {
889                         while (ctype[c=get()] & LETTER) {
890                                 switch(c) {
891
892                                 case 'i':
893                                 case 'I':
894                                         oflag = 1;
895                                         break;
896
897                                 case 's':
898                                 case 'S':
899                                         oflag = 2;
900                                         break;
901 #ifdef GAMEBOY
902                                 case 'y':
903                                 case 'Y':
904                                         c = get();
905                                         if(c == 'O' || c == 'o')
906                                                 nb_rom_banks = expr(0);
907                                         else if(c == 'A' || c == 'a')
908                                                 nb_ram_banks = expr(0);
909                                         else if(c == 'T' || c == 't')
910                                                 mbc_type = expr(0);
911                                         else if(c == 'N' || c == 'n') {
912                                                 int i = 0;
913                                                 if(getnb() != '=' || getnb() != '"') {
914                                                         fprintf(stderr, "Syntax error in -YN=\"name\" flag\n");
915                                                         lkexit(1);
916                                                 }
917                                                 while((c = get()) != '"' && i < 16) {
918                                                         cart_name[i++] = c;
919                                                 }
920                                                 if(i < 16)
921                                                         cart_name[i] = 0;
922                                                 else
923                                                         while(get() != '"')
924                                                                 ;
925                                         } else if(c == 'P' || c == 'p') {
926                                                 patch *p = patches;
927
928                                                 patches = (patch *)malloc(sizeof(patch));
929                                                 patches->next = p;
930                                                 patches->addr = expr(0);
931                                                 if(getnb() != '=') {
932                                                         fprintf(stderr, "Syntax error in -YHaddr=val flag\n");
933                                                         lkexit(1);
934                                                 }
935                                                 patches->value = expr(0);
936                                         } else {
937                                                 fprintf(stderr, "Invalid option\n");
938                                                 lkexit(1);
939                                         }
940                                         break;
941
942 #endif /* GAMEBOY */
943 #ifdef SDK
944                                 case 'j':
945                                 case 'J':
946                                         ++symflag;
947                                         break;
948                                 case 'z':
949                                 case 'Z':
950                                         oflag = 3;
951                                         break;
952 #endif /* SDK */
953                                 case 'm':
954                                 case 'M':
955                                         ++mflag;
956                                         break;
957
958                                 case 'u':
959                                 case 'U':
960                                         uflag = 1;
961                                         break;
962
963                                 case 'x':
964                                 case 'X':
965                                         xflag = 0;
966                                         break;
967
968                                 case 'q':
969                                 case 'Q':
970                                         xflag = 1;
971                                         break;
972
973                                 case 'd':
974                                 case 'D':
975                                         xflag = 2;
976                                         break;
977
978                                 case 'e':
979                                 case 'E':
980                                         return(1);
981
982                                 case 'n':
983                                 case 'N':
984                                         pflag = 0;
985                                         break;
986
987                                 case 'p':
988                                 case 'P':
989                                         pflag = 1;
990                                         break;
991
992                                 case 'b':
993                                 case 'B':
994                                         bassav();
995                                         return(0);
996
997                                 case 'g':
998                                 case 'G':
999                                         gblsav();
1000                                         return(0);
1001
1002                                 case 'k':
1003                                 case 'K':
1004                                         addpath();
1005                                         return(0);
1006
1007                                 case 'l':
1008                                 case 'L':
1009                                         addlib();
1010                                         return(0);
1011
1012                                 default:
1013                                         fprintf(stderr, "Invalid option\n");
1014                                         lkexit(1);
1015                                 }
1016                         }
1017                 } else
1018                 if (ctype[c] != ILL) {
1019                         if (linkp == NULL) {
1020                                 linkp = (struct lfile *)
1021                                         new (sizeof (struct lfile));
1022                                 lfp = linkp;
1023                         } else {
1024                                 lfp->f_flp = (struct lfile *)
1025                                                 new (sizeof (struct lfile));
1026                                 lfp = lfp->f_flp;
1027                         }
1028                         getfid(fid, c);
1029                         lfp->f_idp = (char *) new (strlen(fid)+1);
1030                         strcpy(lfp->f_idp, fid);
1031                         lfp->f_type = F_REL;
1032                 } else {
1033                         fprintf(stderr, "Invalid input");
1034                         lkexit(1);
1035                 }
1036         }
1037         return(0);
1038 }
1039
1040 /*)Function     VOID    bassav()
1041  *
1042  *      The function bassav() creates a linked structure containing
1043  *      the base address strings input to the linker.
1044  *
1045  *      local variables:
1046  *              none
1047  *
1048  *      global variables:
1049  *              base    *basep          The pointer to the first
1050  *                                      base structure
1051  *              base    *bsp            Pointer to the current
1052  *                                      base structure
1053  *              char    *ip             pointer into the REL file
1054  *                                      text line in ib[]
1055  *
1056  *       functions called:
1057  *              char    getnb()         lklex.c
1058  *              VOID *  new()           lksym.c
1059  *              int     strlen()        c_library
1060  *              char *  strcpy()        c_library
1061  *              VOID    unget()         lklex.c
1062  *
1063  *      side effects:
1064  *              The basep structure is created.
1065  */
1066
1067 VOID
1068 bassav()
1069 {
1070         if (basep == NULL) {
1071                 basep = (struct base *)
1072                         new (sizeof (struct base));
1073                 bsp = basep;
1074         } else {
1075                 bsp->b_base = (struct base *)
1076                                 new (sizeof (struct base));
1077                 bsp = bsp->b_base;
1078         }
1079         unget(getnb());
1080         bsp->b_strp = (char *) new (strlen(ip)+1);
1081         strcpy(bsp->b_strp, ip);
1082 }
1083         
1084 /*)Function     VOID    setbas()
1085  *
1086  *      The function setbas() scans the base address lines in hte
1087  *      basep structure, evaluates the arguments, and sets beginning
1088  *      address of the specified areas.
1089  *
1090  *      local variables:
1091  *              int     v               expression value
1092  *              char    id[]            base id string
1093  *
1094  *      global variables:
1095  *              area    *ap             Pointer to the current
1096  *                                      area structure
1097  *              area    *areap          The pointer to the first
1098  *                                      area structure of a linked list
1099  *              base    *basep          The pointer to the first
1100  *                                      base structure
1101  *              base    *bsp            Pointer to the current
1102  *                                      base structure
1103  *              char    *ip             pointer into the REL file
1104  *                                      text line in ib[]
1105  *              int     lkerr           error flag
1106  *
1107  *       functions called:
1108  *              Addr_T  expr()          lkeval.c
1109  *              int     fprintf()       c_library
1110  *              VOID    getid()         lklex.c
1111  *              char    getnb()         lklex.c
1112  *              int     symeq()         lksym.c
1113  *
1114  *      side effects:
1115  *              The base address of an area is set.
1116  */
1117
1118 VOID
1119 setbas()
1120 {
1121         register int v;
1122         char id[NCPS];
1123
1124         bsp = basep;
1125         while (bsp) {
1126                 ip = bsp->b_strp;
1127                 getid(id, -1);
1128                 if (getnb() == '=') {
1129                         v = expr(0);
1130                         for (ap = areap; ap != NULL; ap = ap->a_ap) {
1131                                 if (symeq(id, ap->a_id))
1132                                         break;
1133                         }
1134                         if (ap == NULL) {
1135 #ifndef SDK
1136                                 fprintf(stderr,
1137                                 "No definition of area %s\n", id);
1138                                 lkerr++;
1139 #endif /* SDK */
1140                         } else {
1141                                 ap->a_addr = v;
1142                         }
1143                 } else {
1144                         fprintf(stderr, "No '=' in base expression");
1145                         lkerr++;
1146                 }
1147                 bsp = bsp->b_base;
1148         }
1149 }
1150
1151 /*)Function     VOID    gblsav()
1152  *
1153  *      The function gblsav() creates a linked structure containing
1154  *      the global variable strings input to the linker.
1155  *
1156  *      local variable:
1157  *              none
1158  *
1159  *      global variables:
1160  *              globl   *globlp         The pointer to the first
1161  *                                      globl structure
1162  *              globl   *gsp            Pointer to the current
1163  *                                      globl structure
1164  *              char    *ip             pointer into the REL file
1165  *                                      text line in ib[]
1166  *              int     lkerr           error flag
1167  *
1168  *      functions called:
1169  *              char    getnb()         lklex.c
1170  *              VOID *  new()           lksym.c
1171  *              int     strlen()        c_library
1172  *              char *  strcpy()        c_library
1173  *              VOID    unget()         lklex.c
1174  *
1175  *      side effects:
1176  *              The globlp structure is created.
1177  */
1178
1179 VOID
1180 gblsav()
1181 {
1182         if (globlp == NULL) {
1183                 globlp = (struct globl *)
1184                         new (sizeof (struct globl));
1185                 gsp = globlp;
1186         } else {
1187                 gsp->g_globl = (struct globl *)
1188                                 new (sizeof (struct globl));
1189                 gsp = gsp->g_globl;
1190         }
1191         unget(getnb());
1192         gsp->g_strp = (char *) new (strlen(ip)+1);
1193         strcpy(gsp->g_strp, ip);
1194 }
1195         
1196 /*)Function     VOID    setgbl()
1197  *
1198  *      The function setgbl() scans the global variable lines in hte
1199  *      globlp structure, evaluates the arguments, and sets a variable
1200  *      to this value.
1201  *
1202  *      local variables:
1203  *              int     v               expression value
1204  *              char    id[]            base id string
1205  *              sym *   sp              pointer to a symbol structure
1206  *
1207  *      global variables:
1208  *              char    *ip             pointer into the REL file
1209  *                                      text line in ib[]
1210  *              globl   *globlp         The pointer to the first
1211  *                                      globl structure
1212  *              globl   *gsp            Pointer to the current
1213  *                                      globl structure
1214  *              FILE *  stderr          c_library
1215  *              int     lkerr           error flag
1216  *
1217  *       functions called:
1218  *              Addr_T  expr()          lkeval.c
1219  *              int     fprintf()       c_library
1220  *              VOID    getid()         lklex.c
1221  *              char    getnb()         lklex.c
1222  *              sym *   lkpsym()        lksym.c
1223  *
1224  *      side effects:
1225  *              The value of a variable is set.
1226  */
1227
1228 VOID
1229 setgbl()
1230 {
1231         register int v;
1232         register struct sym *sp;
1233         char id[NCPS];
1234
1235         gsp = globlp;
1236         while (gsp) {
1237                 ip = gsp->g_strp;
1238                 getid(id, -1);
1239                 if (getnb() == '=') {
1240                         v = expr(0);
1241                         sp = lkpsym(id, 0);
1242                         if (sp == NULL) {
1243 #ifndef SDK
1244                                 fprintf(stderr,
1245                                 "No definition of symbol %s\n", id);
1246                                 lkerr++;
1247 #endif /* SDK */
1248                         } else {
1249 #ifndef SDK
1250                                 if (sp->s_flag & S_DEF) {
1251                                         fprintf(stderr,
1252                                         "Redefinition of symbol %s\n", id);
1253                                         lkerr++;
1254                                         sp->s_axp = NULL;
1255                                 }
1256 #endif /* SDK */
1257                                 sp->s_addr = v;
1258                                 sp->s_type |= S_DEF;
1259                         }
1260                 } else {
1261                         fprintf(stderr, "No '=' in global expression");
1262                         lkerr++;
1263                 }
1264                 gsp = gsp->g_globl;
1265         }
1266 }
1267
1268 /*)Function     FILE *  afile(fn,, ft, wf)
1269  *
1270  *              char *  fn              file specification string
1271  *              char *  ft              file type string
1272  *              int     wf              read(0)/write(1) flag
1273  *
1274  *      The function afile() opens a file for reading or writing.
1275  *              (1)     If the file type specification string ft
1276  *                      is not NULL then a file specification is
1277  *                      constructed with the file path\name in fn
1278  *                      and the extension in ft.
1279  *              (2)     If the file type specification string ft
1280  *                      is NULL then the file specification is
1281  *                      constructed from fn.  If fn does not have
1282  *                      a file type then the default .rel file
1283  *                      type is appended to the file specification.
1284  *
1285  *      afile() returns a file handle for the opened file or aborts
1286  *      the assembler on an open error.
1287  *
1288  *      local variables:
1289  *              int     c               character value
1290  *              char    fb[]            constructed file specification string
1291  *              FILE *  fp              filehandle for opened file
1292  *              char *  p1              pointer to filespec string fn
1293  *              char *  p2              pointer to filespec string fb
1294  *              char *  p3              pointer to filetype string ft
1295  *
1296  *      global variables:
1297  *              int     lkerr           error flag
1298  *
1299  *      functions called:
1300  *              FILE *  fopen()         c_library
1301  *              int     fprintf()       c_library
1302  *
1303  *      side effects:
1304  *              File is opened for read or write.
1305  */
1306
1307 FILE *
1308 afile(fn, ft, wf)
1309 char *fn;
1310 char *ft;
1311 {
1312         register char *p1, *p2, *p3;
1313         register int c;
1314         FILE *fp;
1315         char fb[FILSPC];
1316
1317         p1 = fn;
1318         p2 = fb;
1319         p3 = ft;
1320         while ((c = *p1++) != 0 && c != FSEPX) {
1321                 if (p2 < &fb[FILSPC-4])
1322                         *p2++ = c;
1323         }
1324         *p2++ = FSEPX;
1325         if (*p3 == 0) {
1326                 if (c == FSEPX) {
1327                         p3 = p1;
1328                 } else {
1329 #ifdef SDK
1330                         p3 = "rel";
1331 #else /* SDK */
1332                         p3 = "REL";
1333 #endif /* SDK */
1334                 }
1335         }
1336         while ((c = *p3++) != 0) {
1337                 if (p2 < &fb[FILSPC-1])
1338                         *p2++ = c;
1339         }
1340         *p2++ = 0;
1341 #ifdef SDK
1342         if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
1343 #else /* SDK */
1344         if ((fp = fopen(fb, wf?"w":"r")) == NULL) {
1345 #endif /* SDK */
1346                 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1347                 lkerr++;
1348         }
1349         return (fp);
1350 }
1351
1352 char *usetxt[] = {
1353 #ifdef SDK
1354         "Distributed with SDK " SDK_VERSION_STRING ", built on " __DATE__ " " __TIME__,
1355         "Compile options: SDK Target " TARGET_STRING
1356 #ifdef INDEXLIB
1357         " INDEXLIB"
1358 #endif
1359         "\n",
1360 #endif
1361         "Startup:",
1362 #ifdef SDK
1363         "  --   [Commands]              Non-interactive command line input",
1364 #endif /* SDK */
1365         "  -c                           Command line input",
1366         "  -f   file[LNK]               File input",
1367         "  -p   Prompt and echo of file[LNK] to stdout (default)",
1368         "  -n   No echo of file[LNK] to stdout",
1369 #ifdef SDK
1370         "Usage: [-Options] outfile file [file ...]",
1371 #else /* SDK */
1372         "Usage: [-Options] file [file ...]",
1373 #endif /* SDK */
1374         "Librarys:",
1375         "  -k   Library path specification, one per -k",
1376         "  -l   Library file specification, one per -l",
1377         "Relocation:",
1378         "  -b   area base address = expression",
1379         "  -g   global symbol = expression",
1380 #ifdef GAMEBOY
1381         "  -yo  Number of rom banks (default: 2)",
1382         "  -ya  Number of ram banks (default: 0)",
1383         "  -yt  MBC type (default: no MBC)",
1384         "  -yn  Name of program (default: name of output file)",
1385         "  -yp# Patch one byte in the output GB file (# is: addr=byte)",
1386 #endif /* GAMEBOY */
1387         "Map format:",
1388         "  -m   Map output generated as file[MAP]",
1389 #ifdef SDK
1390         "  -j   no$gmb symbol file generated as file[SYM]",
1391 #endif /* SDK */
1392         "  -x   Hexidecimal (default)",
1393         "  -d   Decimal",
1394         "  -q   Octal",
1395         "Output:",
1396         "  -i   Intel Hex as file[IHX]",
1397         "  -s   Motorola S19 as file[S19]",
1398 #ifdef SDK
1399 #ifdef GAMEGEAR
1400         "  -z   Gamegear image as file[GG]",
1401 #else
1402         "  -z   Gameboy image as file[GB]",
1403 #endif /* GAMEGEAR */
1404 #endif /* SDK */
1405         "List:",
1406         "  -u   Update listing file(s) with link data as file(s)[.RST]",
1407         "End:",
1408         "  -e   or null line terminates input",
1409         "",
1410         0
1411 };
1412
1413 /*)Function     VOID    usage()
1414  *
1415  *      The function usage() outputs to the stderr device the
1416  *      assembler name and version and a list of valid assembler options.
1417  *
1418  *      local variables:
1419  *              char ** dp              pointer to an array of
1420  *                                      text string pointers.
1421  *
1422  *      global variables:
1423  *              FILE *  stderr          c_library
1424  *
1425  *      functions called:
1426  *              int     fprintf()       c_library
1427  *
1428  *      side effects:
1429  *              none
1430  */
1431
1432 VOID
1433 usage()
1434 {
1435         register char   **dp;
1436
1437         fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1438         for (dp = usetxt; *dp; dp++)
1439                 fprintf(stderr, "%s\n", *dp);
1440         lkexit(1);
1441 }