* link/z80/lkmain.c,
[fw/sdcc] / link / z80 / lkmain.c
index 4f029de0c5d313609280a6f937bb660adca21680..f2d071e253bddbf0bd7dabef4feb03260a66e2f9 100644 (file)
@@ -1,5 +1,4 @@
 /* lkmain.c */
-
 /*
  * (C) Copyright 1989-1995
  * All Rights Reserved
 
 #include <stdio.h>
 #include <string.h>
-#include <alloc.h>
+//#include <alloc.h>
 #include "aslink.h"
+#include <stdlib.h>
 
 #ifndef SDK_VERSION_STRING
 #define SDK_VERSION_STRING     "3.0.0"
 #endif
-#ifdef TARGET_STRING
+#ifndef TARGET_STRING
 #define TARGET_STRING          "gbz80"
 #endif
 
+#ifdef WIN32T
+#include <time.h>
+
+void Timer(int action, char * message)
+{
+       static double start, end, total=0.0;
+    static const double secs_per_tick = 1.0 / CLOCKS_PER_SEC;
+
+    if(action==0) start=clock()*secs_per_tick;
+    else if(action==1)
+    {
+       end=clock() * secs_per_tick;
+               printf("%s \t%f seconds.\n", message, (end-start));
+               total+=end-start;
+    }
+    else
+    {
+               printf("Total time: \t%f seconds.\n", total);
+               total=0.0;
+    }
+}
+#endif
+
 /*)Module      lkmain.c
  *
  *     The module lkmain.c contains the functions which
@@ -60,7 +83,7 @@
  *
  *     The function main() evaluates the command line arguments to
  *     determine if the linker parameters are to input through 'stdin'
- *     or read from a command file.  The functiond getline() and parse()
+ *     or read from a command file.  The functiond lk_getline() and parse()
  *     are to input and evaluate the linker parameters.  The linking process
  *     proceeds by making the first pass through each .rel file in the order
  *     presented to the linker.  At the end of the first pass the setbase(),
  *             FILE *  afile()         lkmain.c
  *             int     fclose()        c_library
  *             int     fprintf()       c_library
- *             int     getline()       lklex.c
+ *             int     lk_getline()    lklex.c
  *             VOID    library()       lklibr.c
  *             VOID    link()          lkmain.c
  *             VOID    lkexit()        lkmain.c
@@ -158,12 +181,16 @@ char *default_globlp[] = {
 };
 #endif /* GAMEBOY */
 
-VOID
+int
 main(argc, argv)
 char *argv[];
 {
        register char *p;
-       register c, i;
+       register int c, i;
+
+#ifdef WIN32T
+    Timer(0, "");
+#endif
 
 #ifdef GAMEBOY
        nb_rom_banks = 2;
@@ -244,7 +271,7 @@ char *argv[];
                        }
                }
        }
-       if (startp->f_type == NULL)
+       if (startp->f_type == F_INV)
                usage();
        if (startp->f_type == F_LNK && startp->f_idp == NULL)
                usage();
@@ -258,11 +285,11 @@ char *argv[];
        filep = startp;
        while (1) {
                ip = ib;                                        
-               if (getline() == 0)
+               if (lk_getline() == 0)
                        break;
                if (pflag && sfp != stdin)
                        fprintf(stdout, "%s\n", ip);
-               if (*ip == NULL || parse())
+               if (*ip == '\0' || parse())
                        break;
        }
        if (sfp)
@@ -285,7 +312,7 @@ char *argv[];
                bsp->b_base = (struct base *)new(sizeof(struct base));
                bsp = bsp->b_base;
                bsp->b_strp = (char *)malloc(18);
-               sprintf(bsp->b_strp, "_BSS_%d=0xA000", i);
+               sprintf(bsp->b_strp, "_DATA_%d=0xA000", i);
        }
 #endif /* GAMEBOY */
 
@@ -301,7 +328,7 @@ char *argv[];
                hp = NULL;
                radix = 10;
 
-               while (getline()) {
+               while (lk_getline()) {
                        ip = ib;
                        link();
                }
@@ -376,7 +403,13 @@ char *argv[];
                        reloc('E');
                }
        }
+#ifdef WIN32T
+    Timer(1, "Linker time");
+#endif
        lkexit(lkerr);
+
+        /* Never get here. */
+        return 0;
 }
 
 /*)Function    VOID    lkexit(i)
@@ -450,11 +483,34 @@ int i;
 VOID
 link()
 {
-       register c;
+       register int c;
 
        if ((c=endline()) == 0) { return; }
        switch (c) {
 
+    case 'O': /*For some important sdcc options*/
+        if (pass == 0)
+        {
+            if(strlen(sdccopt)==0)
+            {
+                strcpy(sdccopt, &ip[1]);
+                strcpy(sdccopt_module, curr_module);
+            }
+            else
+            {
+                if(strcmp(sdccopt, &ip[1])!=0)
+                {
+                                   fprintf(stderr,
+                                   "?ASlink-Warning-Conflicting sdcc options:\n"
+                    "   \"%s\" in module \"%s\" and\n"
+                    "   \"%s\" in module \"%s\".\n",
+                    sdccopt, sdccopt_module, &ip[1], curr_module);
+                                   lkerr++;
+                }
+            }
+        }
+               break;
+
        case 'X':
                radix = 16;
                break;
@@ -484,7 +540,10 @@ link()
 
        case 'M':
                if (pass == 0)
+        {
+            strcpy(curr_module, &ip[1]);
                        module();
+        }
                break;
 
        case 'A':
@@ -682,7 +741,6 @@ map()
 #else
 VOID map()
 {
-       register i;
        register struct head *hdp;
        register struct lbfile *lbfh;
 
@@ -773,12 +831,11 @@ VOID map()
 #endif /* MLH_MAP */
 
 #ifdef SDK
+/* PENDING */
+VOID lstareatosym(struct area *xp);
+
 VOID sym()
 {
-       register i;
-       register struct head *hdp;
-       register struct lbfile *lbfh;
-
        /*
         * Open sym File
         */
@@ -850,10 +907,12 @@ VOID sym()
 int
 parse()
 {
-       register c;
+       register int c;
        char fid[NINPUT];
 
        while ((c = getnb()) != 0) {
+               if (c == ';')
+                       return(0);
                if ( c == '-') {
                        while (ctype[c=get()] & LETTER) {
                                switch(c) {
@@ -892,8 +951,6 @@ parse()
                                                        while(get() != '"')
                                                                ;
                                        } else if(c == 'P' || c == 'p') {
-                                               unsigned int addr;
-                                               unsigned char value;
                                                patch *p = patches;
 
                                                patches = (patch *)malloc(sizeof(patch));
@@ -985,6 +1042,8 @@ parse()
                                        lkexit(1);
                                }
                        }
+                       if (c == ';')
+                               return(0);
                } else
                if (ctype[c] != ILL) {
                        if (linkp == NULL) {
@@ -1076,7 +1135,7 @@ bassav()
  *             int     lkerr           error flag
  *
  *      functions called:
- *             addr_t  expr()          lkeval.c
+ *             Addr_T  expr()          lkeval.c
  *             int     fprintf()       c_library
  *             VOID    getid()         lklex.c
  *             char    getnb()         lklex.c
@@ -1089,7 +1148,7 @@ bassav()
 VOID
 setbas()
 {
-       register v;
+       register int v;
        char id[NCPS];
 
        bsp = basep;
@@ -1186,7 +1245,7 @@ gblsav()
  *             int     lkerr           error flag
  *
  *      functions called:
- *             addr_t  expr()          lkeval.c
+ *             Addr_T  expr()          lkeval.c
  *             int     fprintf()       c_library
  *             VOID    getid()         lklex.c
  *             char    getnb()         lklex.c
@@ -1199,7 +1258,7 @@ gblsav()
 VOID
 setgbl()
 {
-       register v;
+       register int v;
        register struct sym *sp;
        char id[NCPS];
 
@@ -1280,11 +1339,16 @@ afile(fn, ft, wf)
 char *fn;
 char *ft;
 {
+#if 0
        register char *p1, *p2, *p3;
-       register c;
+       register int c;
+#else
+       int i;
+#endif 
        FILE *fp;
        char fb[FILSPC];
 
+#if 0  
        p1 = fn;
        p2 = fb;
        p3 = ft;
@@ -1309,6 +1373,32 @@ char *ft;
                        *p2++ = c;
        }
        *p2++ = 0;
+#else
+       /*Look backward the name path and get rid of the extension, if any*/
+       i=strlen(fn);
+       for(; (fn[i]!='.')&&(fn[i]!='\\')&&(fn[i]!='/')&&(i>0); i--);
+       if( (fn[i]=='.') && *ft && strcmp(ft, "lnk") )
+       {
+               strncpy(fb, fn, i);
+               fb[i]=0;
+       }
+       else
+       {
+               strcpy(fb, fn);
+       }
+
+       /*Add the extension*/
+       if (fb[i] != '.')
+       {
+               strcat(fb, ".");
+#ifdef SDK
+               strcat(fb, strlen(ft)?ft:"rel");
+#else
+               strcat(fb, strlen(ft)?ft:"REL");
+#endif
+       }
+#endif
+
 #ifdef SDK
        if ((fp = fopen(fb, wf?(binary?"wb":"w"):(binary?"rb":"r"))) == NULL) {
 #else /* SDK */