* src/pic/gen.c,
[fw/sdcc] / src / pic / gen.c
index c3d8251e538ec7c077f6caed664756723e2d601f..cfbd202863db2b4e8998f631b2215d03c82d02f0 100644 (file)
@@ -1278,20 +1278,19 @@ pCodeOp *popGetExternal (char *str)
        
        if (str) {
          symbol *sym;
-         bool found = 0;
 
-         for (sym = setFirstItem (externs); !found && sym; sym = setNextItem (externs))
+         for (sym = setFirstItem (externs); sym; sym = setNextItem (externs))
          {
-           if (!strcmp (str, sym->rname))
-             found = 1;
+           if (!strcmp (str, sym->rname)) break;
          }
          
-         if (!found)
+         if (!sym)
          {
            sym = newSymbol(str, 0);
            strncpy(sym->rname, str, SDCC_NAME_MAX);
            addSet (&externs, sym);
          } // if
+         sym->used++;
        }
        return pcop;
 }
@@ -1826,13 +1825,26 @@ static void get_returnvalue (operand *op, int offset, int idx)
 
 static void call_libraryfunc (char *name)
 {
-  /* library code might reside in different page... */
-  emitpcode (POC_PAGESEL, popGetWithString (name, 1));
-  /* call the library function */
-  emitpcode (POC_CALL, popGetExternal (name));
-  /* might return from different page... */
-  emitpcode (POC_PAGESEL, popGetWithString ("$", 0));
+    symbol *sym;
+
+    /* library code might reside in different page... */
+    emitpcode (POC_PAGESEL, popGetWithString (name, 1));
+    /* call the library function */
+    emitpcode (POC_CALL, popGetExternal (name));
+    /* might return from different page... */
+    emitpcode (POC_PAGESEL, popGetWithString ("$", 0));
+
+    /* create symbol, mark it as `extern' */
+    sym = findSym(SymbolTab, NULL, name);
+    if (!sym) {
+       sym = newSymbol(name, 0);
+       strncpy(sym->rname, name, SDCC_NAME_MAX);
+       addSym(SymbolTab, sym, sym->rname, 0, 0, 0);
+       addSet(&externs, sym); 
+    } // if
+    sym->used++;
 }
+
 #if 0
 /*-----------------------------------------------------------------*/
 /* reAdjustPreg - points a register back to where it should        */
@@ -2826,6 +2838,13 @@ static void genFunction (iCode *ic)
 
        pic14_emitcode("","%s:",sym->rname);
        addpCode2pBlock(pb,newpCodeFunction(NULL,sym->rname,!IS_STATIC (sym->etype)));
+
+       /* mark symbol as NOT extern (even if it was declared so previously) */
+       assert(IS_SPEC(sym->etype));
+       SPEC_EXTR(sym->etype) = 0;
+       sym->cdef = 0;
+       if (!SPEC_OCLS(sym->etype)) SPEC_OCLS(sym->etype) = code;
+       addSetIfnotP(&SPEC_OCLS(sym->etype)->syms, sym);
        
        ftype = operandType(IC_LEFT(ic));
        
@@ -3096,8 +3115,7 @@ registers :-) */
 #endif
                
                /* if debug then send end of function */
-               /*      if (options.debug && currFunc) { */
-               if (currFunc) {
+               if (options.debug && debugFile && currFunc) {
                        debugFile->writeEndFunction (currFunc, ic, 1);
                }
                
@@ -3132,7 +3150,7 @@ registers :-) */
                }
                
                /* if debug then send end of function */
-               if (currFunc) {
+               if (options.debug && debugFile && currFunc) {
                        debugFile->writeEndFunction (currFunc, ic, 1);
                }
                
@@ -9622,7 +9640,16 @@ static void genGenPointerSet (operand *right, operand *result, iCode *ic)
          int size = AOP_SIZE(right);
          int idx = 0;
 
-         assert (size == getSize(OP_SYM_ETYPE(result)));
+         /* The following assertion fails for
+          *   struct foo { char a; char b; } bar;
+          *   void demo(struct foo *dst, char c) { dst->b = c; }
+          * as size will be 1 (sizeof(c)), whereas dst->b will be accessed
+          * using (((char *)dst)+1), whose OP_SYM_ETYPE still is struct foo
+          * of size 2.
+          * The frontend seems to guarantee that IC_LEFT has the correct size,
+          * it works fine both for larger and smaller types of `char c'.
+          * */
+         //assert (size == getSize(OP_SYM_ETYPE(result)));
          assert (size > 0 && size <= 4);
 
          /* pass arguments */
@@ -10539,10 +10566,8 @@ void genpic14Code (iCode *lic)
        addpBlock(pb);
        
        /* if debug information required */
-       if (options.debug && currFunc) { 
-               if (currFunc) {
-                       debugFile->writeFunction (currFunc, lic);
-               }
+       if (options.debug && debugFile && currFunc) { 
+               debugFile->writeFunction (currFunc, lic);
        }