* src/SDCCmain.c: #fixed bug 2224960: sdcc -MM no working correctly
[fw/sdcc] / src / pic16 / glue.c
index 43e01599e7671537eba4f08de82df1c18eb7a1b0..b26f0478ef131c2164e07b570e04ad02028bf752 100644 (file)
 #include "gen.h"
 #include "device.h"
 #include "main.h"
+#include "dbuf_string.h"
 #include <string.h>
 
-#include <string.h>
-
-
 
 extern symbol *interrupts[256];
 void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
@@ -42,12 +40,9 @@ extern int noAlloc;
 extern set *publics;
 extern set *externs;
 extern unsigned maxInterrupts;
-extern int maxRegBank;
 extern symbol *mainf;
 extern char *VersionString;
-extern FILE *codeOutFile;
-extern set *tmpfileSet;
-extern set *tmpfileNameSet;
+extern struct dbuf_s *codeOutBuf;
 extern char *iComments1;
 extern char *iComments2;
 
@@ -55,31 +50,28 @@ extern int initsfpnt;
 extern unsigned long pFile_isize;
 
 extern unsigned long pic16_countInstructions();
-set *rel_idataSymSet=NULL;
-set *fix_idataSymSet=NULL;
-
-extern DEFSETFUNC (closeTmpFiles);
-extern DEFSETFUNC (rmTmpFiles);
+set *pic16_localFunctions = NULL;
+set *rel_idataSymSet = NULL;
+set *fix_idataSymSet = NULL;
 
 extern void pic16_AnalyzeBanking (void);
-extern void pic16_OptimizeJumps ();
-extern void pic16_OptimizeBanksel ();
-extern void copyFile (FILE * dest, FILE * src);
-extern void pic16_InlinepCode(void);
-extern void pic16_writeUsedRegs(FILE *);
+extern void pic16_OptimizeJumps (void);
+extern void pic16_OptimizeBanksel (void);
+extern void pic16_InlinepCode (void);
+extern void pic16_writeUsedRegs (FILE *);
 
 extern void initialComments (FILE * afile);
 extern void printPublics (FILE * afile);
 
-void  pic16_pCodeInitRegisters(void);
-pCodeOp *pic16_popCopyReg(pCodeOpReg *pc);
-extern void pic16_pCodeConstString(char *name, char *value);
+void  pic16_pCodeInitRegisters (void);
+pCodeOp *pic16_popCopyReg (pCodeOpReg *pc);
+extern void pic16_pCodeConstString (char *name, char *value, unsigned length);
 
 
 /*-----------------------------------------------------------------*/
 /* aopLiteral - string from a literal value                        */
 /*-----------------------------------------------------------------*/
-int pic16aopLiteral (value *val, int offset)
+unsigned int pic16aopLiteral (value *val, int offset)
 {
   union {
     float f;
@@ -89,14 +81,14 @@ int pic16aopLiteral (value *val, int offset)
   /* if it is a float then it gets tricky */
   /* otherwise it is fairly simple */
   if (!(IS_FLOAT(val->type) || IS_FIXED(val->type))) {
-    unsigned long v = (unsigned long) floatFromVal(val);
+    unsigned long v = ulFromVal (val);
 
     return ( (v >> (offset * 8)) & 0xff);
   }
 
   if(IS_FIXED16X16(val->type)) {
     unsigned long v = (unsigned long)fixed16x16FromDouble( floatFromVal( val ) );
-    
+
     return ( (v >> (offset * 8)) & 0xff);
   }
 
@@ -124,77 +116,77 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
   symbol *sym;
 //  int i, size, bitvars = 0;;
 
-//     fprintf(stderr, "%s:%d map name= %s\n", __FUNCTION__, __LINE__, map->sname);
-       
-       if(addPublics)
-               fprintf (map->oFile, ";\t.area\t%s\n", map->sname);
-               /* print the area name */
+//      fprintf(stderr, "%s:%d map name= %s\n", __FUNCTION__, __LINE__, map->sname);
+
+        if(addPublics)
+                dbuf_printf (&map->oBuf, ";\t.area\t%s\n", map->sname);
+                /* print the area name */
 
-       for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms)) {
+        for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms)) {
 
 #if 0
-               fprintf(stderr, "%s\t%s: sym: %s\tused: %d\textern: %d\tstatic: %d\taggregate: %d\tregister: 0x%x\tfunction: %d\n",
-                       __FUNCTION__,
-                       map->sname, sym->name, sym->used, IS_EXTERN(sym->etype), IS_STATIC(sym->etype),
-                       IS_AGGREGATE(sym->type), (SPEC_SCLS(sym->etype) == S_REGISTER), IS_FUNC(sym->type));
-               printTypeChain( sym->type, stderr );
-               fprintf(stderr, "\n");
+                fprintf(stderr, "%s\t%s: sym: %s\tused: %d\textern: %d\tstatic: %d\taggregate: %d\tregister: 0x%x\tfunction: %d\n",
+                        __FUNCTION__,
+                        map->sname, sym->name, sym->used, IS_EXTERN(sym->etype), IS_STATIC(sym->etype),
+                        IS_AGGREGATE(sym->type), (SPEC_SCLS(sym->etype) == S_REGISTER), IS_FUNC(sym->type));
+                printTypeChain( sym->type, stderr );
+                fprintf(stderr, "\n");
 #endif
 
-               /* if extern then add to externs */
-               if (IS_EXTERN (sym->etype)) {
-                       /* reduce overhead while linking by not declaring
-                        * extern unused external functions (usually declared
-                        * in header files) */
-                       if(IS_FUNC(sym->type) && !sym->used)continue;
-                       
-                       /* make sure symbol is not in publics section */
-                       if(!checkSym(publics, sym))
-                               checkAddSym(&externs, sym);
-                       continue;
-               }
-               
-               /* if allocation required check is needed
-                * then check if the symbol really requires
-                * allocation only for local variables */
-                if (arFlag && !IS_AGGREGATE (sym->type) &&
-                       !(sym->_isparm && !IS_REGPARM (sym->etype)) &&
-                       !sym->allocreq && sym->level) {
-
-//                     fprintf(stderr, "%s:%d special case, continuing...\n", __FILE__, __LINE__);
-
-                       continue;
-               }
-
-               /* if global variable & not static or extern
-                * and addPublics allowed then add it to the public set */
-               if ((sym->used) && (sym->level == 0 ||
-                       (sym->_isparm && !IS_REGPARM (sym->etype))) &&
-                       addPublics &&
-                       !IS_STATIC (sym->etype) && !IS_FUNC(sym->type)) {
-                 
-                       checkAddSym(&publics, sym);
-               } else
+                /* if extern then add to externs */
+                if (IS_EXTERN (sym->etype)) {
+                        /* reduce overhead while linking by not declaring
+                         * extern unused external functions (usually declared
+                         * in header files) */
+                        if(IS_FUNC(sym->type) && !sym->used)continue;
+
+                        /* make sure symbol is not in publics section */
+                        if(!checkSym(publics, sym))
+                                checkAddSym(&externs, sym);
+                        continue;
+                }
+
+                /* if allocation required check is needed
+                 * then check if the symbol really requires
+                 * allocation only for local variables */
+                 if (arFlag && !IS_AGGREGATE (sym->type) &&
+                        !(sym->_isparm && !IS_REGPARM (sym->etype)) &&
+                        !sym->allocreq && sym->level) {
+
+//                      fprintf(stderr, "%s:%d special case, continuing...\n", __FILE__, __LINE__);
+
+                        continue;
+                }
+
+                /* if global variable & not static or extern
+                 * and addPublics allowed then add it to the public set */
+                if ((sym->used) && (sym->level == 0 ||
+                        (sym->_isparm && !IS_REGPARM (sym->etype))) &&
+                        addPublics &&
+                        !IS_STATIC (sym->etype) && !IS_FUNC(sym->type)) {
+
+                        checkAddSym(&publics, sym);
+                } else
                         /* new version */
-                       if(IS_STATIC(sym->etype)
-                               && !sym->ival) /* && !sym->level*/ {
-                         regs *reg;
+                        if(IS_STATIC(sym->etype)
+                                && !sym->ival) /* && !sym->level*/ {
+                          regs *reg;
                           sectSym *ssym;
                           int found=0;
 
 //                            debugf("adding symbol %s\n", sym->name);
-#define SET_IMPLICIT   1
+#define SET_IMPLICIT    1
 
 #if SET_IMPLICIT
-                               if(IS_STRUCT(sym->type))
-                                       sym->implicit = 1;
+                                if(IS_STRUCT(sym->type))
+                                        sym->implicit = 1;
 #endif
 
-                               reg = pic16_allocDirReg( operandFromSymbol( sym ));
-                               
-                               if(reg) {
+                                reg = pic16_allocDirReg( operandFromSymbol( sym ));
+
+                                if(reg) {
                                   for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
-                                   if(!strcmp(ssym->name, reg->name))found=1;
+                                    if(!strcmp(ssym->name, reg->name))found=1;
                                   }
 
                                   if(!found)
@@ -202,92 +194,92 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
 #if 0
                                   else
                                     debugf("Did find %s in pic16_rel_udata already. Check!\n", reg->name);
-//                                 checkAddSym(&publics, sym);
+//                                  checkAddSym(&publics, sym);
 #endif
 
                                 }
-                       }
-
-               /* if extern then do nothing or is a function
-                * then do nothing */
-               if (IS_FUNC (sym->type) && !IS_STATIC(sym->etype)) {
-                       if(SPEC_OCLS(sym->etype) == code) {
-//                             fprintf(stderr, "%s:%d: symbol added: %s\n", __FILE__, __LINE__, sym->rname);
-                               checkAddSym(&publics, sym);
-                       }
-                       continue;
-               }
-
-               /* if is has an absolute address then generate
-               an equate for this no need to allocate space */
-               if (SPEC_ABSA (sym->etype)) {
-//                             fprintf (stderr,"; %s == 0x%04x\t\treqv= %p nRegs= %d\n",
-//                                     sym->name, SPEC_ADDR (sym->etype), sym->reqv, sym->regType);
-
-                       fprintf (map->oFile, "%s\tEQU\t0x%04x\n",
-                               sym->rname,
-                               SPEC_ADDR (sym->etype));
-
-                       /* emit only if it is global */
-                       if(sym->level == 0) {
-                         regs *reg;
-
-                               reg = pic16_dirregWithName( sym->name );
-                               if(!reg) {
-                                       /* here */
-//                                     fprintf(stderr, "%s:%d: implicit add of symbol = %s\n",
-//                                                     __FUNCTION__, __LINE__, sym->name);
-
-                                       /* if IS_STRUCT is omitted the following
-                                        * fixes structures but break char/int etc */
+                        }
+
+                /* if extern then do nothing or is a function
+                 * then do nothing */
+                if (IS_FUNC (sym->type) && !IS_STATIC(sym->etype)) {
+                        if(SPEC_OCLS(sym->etype) == code) {
+//                              fprintf(stderr, "%s:%d: symbol added: %s\n", __FILE__, __LINE__, sym->rname);
+                                checkAddSym(&publics, sym);
+                        }
+                        continue;
+                }
+
+                /* if is has an absolute address then generate
+                an equate for this no need to allocate space */
+                if (SPEC_ABSA (sym->etype)) {
+//                              fprintf (stderr,"; %s == 0x%04x\t\treqv= %p nRegs= %d\n",
+//                                      sym->name, SPEC_ADDR (sym->etype), sym->reqv, sym->regType);
+
+                        dbuf_printf (&map->oBuf, "%s\tEQU\t0x%04x\n",
+                                sym->rname,
+                                SPEC_ADDR (sym->etype));
+
+                        /* emit only if it is global */
+                        if(sym->level == 0) {
+                          regs *reg;
+
+                                reg = pic16_dirregWithName( sym->name );
+                                if(!reg) {
+                                        /* here */
+//                                      fprintf(stderr, "%s:%d: implicit add of symbol = %s\n",
+//                                                      __FUNCTION__, __LINE__, sym->name);
+
+                                        /* if IS_STRUCT is omitted the following
+                                         * fixes structures but break char/int etc */
 #if SET_IMPLICIT
-                                       if(IS_STRUCT(sym->type))
-                                               sym->implicit = 1;              // mark as implicit
+                                        if(IS_STRUCT(sym->type))
+                                                sym->implicit = 1;              // mark as implicit
 #endif
-                                       if(!sym->ival) {
-                                               reg = pic16_allocDirReg( operandFromSymbol(sym) );
-                                               if(reg) {
-                                                       if(checkAddReg(&pic16_fix_udata, reg)) {
-                                                               /* and add to globals list if not exist */
-                                                               addSet(&publics, sym);
-                                                       }
-                                               }
-                                       } else
-                                               addSet(&publics, sym);
-                               }
-                       }
-               } else {
-                       if(!sym->used && (sym->level == 0)) {
-                         regs *reg;
-
-                               /* symbol not used, just declared probably, but its in
-                                * level 0, so we must declare it fine as global */
-                               
-//                             fprintf(stderr, "EXTRA symbol declaration sym= %s\n", sym->name);
+                                        if(!sym->ival) {
+                                                reg = pic16_allocDirReg( operandFromSymbol(sym) );
+                                                if(reg) {
+                                                        if(checkAddReg(&pic16_fix_udata, reg)) {
+                                                                /* and add to globals list if not exist */
+                                                                addSet(&publics, sym);
+                                                        }
+                                                }
+                                        } else
+                                                addSet(&publics, sym);
+                                }
+                        }
+                } else {
+                        if(!sym->used && (sym->level == 0)) {
+                          regs *reg;
+
+                                /* symbol not used, just declared probably, but its in
+                                 * level 0, so we must declare it fine as global */
+
+//                              fprintf(stderr, "EXTRA symbol declaration sym= %s\n", sym->name);
 
 #if SET_IMPLICIT
-                               if(IS_STRUCT(sym->type))
-                                       sym->implicit = 1;              // mark as implicit
+                                if(IS_STRUCT(sym->type))
+                                        sym->implicit = 1;              // mark as implicit
 #endif
-                               if(!sym->ival) {
-                                       if(IS_AGGREGATE(sym->type)) {
-                                               reg=pic16_allocRegByName(sym->rname, getSize( sym->type ), NULL);
-                                       } else {
-                                               reg = pic16_allocDirReg( operandFromSymbol( sym ) );
-                                       }
-
-                                       {
-                                         sectSym *ssym;
-                                         int found=0;
-                                 
+                                if(!sym->ival) {
+                                        if(IS_AGGREGATE(sym->type)) {
+                                                reg=pic16_allocRegByName(sym->rname, getSize( sym->type ), NULL);
+                                        } else {
+                                                reg = pic16_allocDirReg( operandFromSymbol( sym ) );
+                                        }
+
+                                        {
+                                          sectSym *ssym;
+                                          int found=0;
+
 #if 0
-                                               fprintf(stderr, "%s:%d sym->rname: %s reg: %p reg->name: %s\n", __FILE__, __LINE__,
-                                                       sym->rname, reg, (reg?reg->name:"<<NULL>>"));
+                                                fprintf(stderr, "%s:%d sym->rname: %s reg: %p reg->name: %s\n", __FILE__, __LINE__,
+                                                        sym->rname, reg, (reg?reg->name:"<<NULL>>"));
 #endif
 
                                                 if(reg) {
-                                                 for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
-                                                   if(!strcmp(ssym->name, reg->name))found=1;
+                                                  for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
+                                                    if(!strcmp(ssym->name, reg->name))found=1;
                                                   }
 
                                                   if(!found)
@@ -295,91 +287,92 @@ pic16emitRegularMap (memmap * map, bool addPublics, bool arFlag)
                                                       addSetHead(&publics, sym);
                                                     }
                                                 }
-                                       }
+                                        }
 
 
-               
-                               } else
 
-                                       addSetHead(&publics, sym);
-                       }
+                                } else
+
+                                        addSetHead(&publics, sym);
+                        }
 
 #if 0
-                       /* allocate space */
-                       /* If this is a bit variable, then allocate storage after 8 bits have been declared */
-                       /* unlike the 8051, the pic does not have a separate bit area. So we emulate bit ram */
-                       /* by grouping the bits together into groups of 8 and storing them in the normal ram. */
-                       if (IS_BITVAR (sym->etype)) {
-                               bitvars++;
-                       } else {
-                               fprintf (map->oFile, "\t%s\n", sym->rname);
-                               if ((size = (unsigned int) getSize (sym->type) & 0xffff) > 1) {
-                                       for (i = 1; i < size; i++)
-                                               fprintf (map->oFile, "\t%s_%d\n", sym->rname, i);
-                               }
-                       }
-                       fprintf (map->oFile, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff);
+                        /* allocate space */
+                        /* If this is a bit variable, then allocate storage after 8 bits have been declared */
+                        /* unlike the 8051, the pic does not have a separate bit area. So we emulate bit ram */
+                        /* by grouping the bits together into groups of 8 and storing them in the normal ram. */
+                        if (IS_BITVAR (sym->etype)) {
+                                bitvars++;
+                        } else {
+                                dbuf_printf (map->oBuf, "\t%s\n", sym->rname);
+                                if ((size = (unsigned int) getSize (sym->type) & 0xffff) > 1) {
+                                        for (i = 1; i < size; i++)
+                                                dbuf_printf (map->oBuf, "\t%s_%d\n", sym->rname, i);
+                                }
+                        }
+                        dbuf_printf (map->oBuf, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff);
 #endif
-               }
-       
-               /* FIXME -- VR Fix the following, so that syms to be placed
-                * in the idata section and let linker decide about their fate */
+                }
+
+                /* FIXME -- VR Fix the following, so that syms to be placed
+                 * in the idata section and let linker decide about their fate */
 
-               /* if it has an initial value then do it only if
-                       it is a global variable */
+                /* if it has an initial value then do it only if
+                        it is a global variable */
 
-               if (sym->ival
-                 && ((sym->level == 0)
-                     || IS_STATIC(sym->etype)) ) {
-                 ast *ival = NULL;
+                if (sym->ival
+                  && ((sym->level == 0)
+                      || IS_STATIC(sym->etype)) ) {
+                  ast *ival = NULL;
 
 #if 0
-                       if(SPEC_OCLS(sym->etype)==data) {
-                               fprintf(stderr, "%s: sym %s placed in data segment\n", map->sname, sym->name);
-                       }
+                        if(SPEC_OCLS(sym->etype)==data) {
+                                fprintf(stderr, "%s: sym %s placed in data segment\n", map->sname, sym->name);
+                        }
 
-                       if(SPEC_OCLS(sym->etype)==code) {
-                               fprintf(stderr, "%s: sym %s placed in code segment\n", map->sname, sym->name);
-                       }
+                        if(SPEC_OCLS(sym->etype)==code) {
+                                fprintf(stderr, "%s: sym %s placed in code segment\n", map->sname, sym->name);
+                        }
 #endif
 
 #if 0
-                       fprintf(stderr, "'%s': sym '%s' has initial value SPEC_ABSA: %d, IS_AGGREGATE: %d\n",
-                               map->sname, sym->name, SPEC_ABSA(sym->etype), IS_AGGREGATE(sym->type));
+                        fprintf(stderr, "'%s': sym '%s' has initial value SPEC_ABSA: %d, IS_AGGREGATE: %d\n",
+                                map->sname, sym->name, SPEC_ABSA(sym->etype), IS_AGGREGATE(sym->type));
 #endif
 
-                       if (IS_AGGREGATE (sym->type)) {
-                               if(SPEC_ABSA(sym->etype))
-                                       addSet(&fix_idataSymSet, copySymbol(sym));
-                               else
-                                       addSet(&rel_idataSymSet, copySymbol(sym));
-//                             ival = initAggregates (sym, sym->ival, NULL);
-                       } else {
-                               if(SPEC_ABSA(sym->etype))
-                                       addSet(&fix_idataSymSet, copySymbol(sym));
-                               else
-                                       addSet(&rel_idataSymSet, copySymbol(sym));
-
-//                                     ival = newNode ('=', newAst_VALUE(symbolVal (sym)),
-//                                             decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
-                       }
-
-                       if(ival) {
-                               setAstLineno(ival, sym->lineDef);
-                               codeOutFile = statsg->oFile;
-                               GcurMemmap = statsg;
-                               eBBlockFromiCode (iCodeFromAst (ival));
-                               sym->ival = NULL;
-                       }
-               }
-       }
+                        if (IS_AGGREGATE (sym->type)) {
+                                if(SPEC_ABSA(sym->etype))
+                                        addSet(&fix_idataSymSet, copySymbol(sym));
+                                else
+                                        addSet(&rel_idataSymSet, copySymbol(sym));
+//                              ival = initAggregates (sym, sym->ival, NULL);
+                        } else {
+                                if(SPEC_ABSA(sym->etype))
+                                        addSet(&fix_idataSymSet, copySymbol(sym));
+                                else
+                                        addSet(&rel_idataSymSet, copySymbol(sym));
+
+//                                      ival = newNode ('=', newAst_VALUE(symbolVal (sym)),
+//                                              decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
+                        }
+
+                        if(ival) {
+                                setAstFileLine (ival, sym->fileDef, sym->lineDef);
+                                codeOutBuf = &statsg->oBuf;
+                                GcurMemmap = statsg;
+                                eBBlockFromiCode (iCodeFromAst (ival));
+                                sym->ival = NULL;
+                        }
+                }
+        }
 }
 
 
 /*-----------------------------------------------------------------*/
 /* pic16_initPointer - pointer initialization code massaging       */
 /*-----------------------------------------------------------------*/
-value *pic16_initPointer (initList * ilist, sym_link *toType)
+static value *
+pic16_initPointer (initList * ilist, sym_link *toType)
 {
   value *val;
   ast *expr;
@@ -390,14 +383,14 @@ value *pic16_initPointer (initList * ilist, sym_link *toType)
 
   expr = decorateType(resolveSymbols( list2expr (ilist) ), FALSE);
 //  expr = list2expr( ilist );
-  
+
   if (!expr)
     goto wrong;
-  
+
   /* try it the old way first */
-  if ((val = constExprValue (expr, FALSE)))
+  if (expr->etype && (val = constExprValue (expr, FALSE)))
     return val;
-  
+
   /* ( ptr + constant ) */
   if (IS_AST_OP (expr) &&
       (expr->opval.op == '+' || expr->opval.op == '-') &&
@@ -409,7 +402,7 @@ value *pic16_initPointer (initList * ilist, sym_link *toType)
                            expr->right,
                            expr->opval.op);
   }
-  
+
   /* (char *)&a */
   if (IS_AST_OP(expr) && expr->opval.op==CAST &&
       IS_AST_OP(expr->right) && expr->right->opval.op=='&') {
@@ -426,8 +419,8 @@ value *pic16_initPointer (initList * ilist, sym_link *toType)
      a variable or address of an array element */
   if (IS_AST_OP (expr) && expr->opval.op == '&') {
     /* address of symbol */
-    if (IS_AST_SYM_VALUE (expr->left)) {
-      val = copyValue (AST_VALUE (expr->left));
+    if (IS_AST_SYM_VALUE (expr->left) && expr->left->etype) {
+      val = AST_VALUE (expr->left);
       val->type = newLink (DECLARATOR);
       if(SPEC_SCLS (expr->left->etype) == S_CODE) {
         DCL_TYPE (val->type) = CPOINTER;
@@ -521,20 +514,22 @@ value *pic16_initPointer (initList * ilist, sym_link *toType)
 /*-----------------------------------------------------------------*/
 /* printPointerType - generates ival for pointer type              */
 /*-----------------------------------------------------------------*/
-void _pic16_printPointerType (const char *name, char ptype, void *p)
+static void
+_pic16_printPointerType (const char *name, char ptype, void *p)
 {
   char buf[256];
 
-       sprintf(buf, "LOW(%s)", name);
-       pic16_emitDS(buf, ptype, p);
-       sprintf(buf, "HIGH(%s)", name);
-       pic16_emitDS(buf, ptype, p);
+  sprintf (buf, "LOW(%s)", name);
+  pic16_emitDS (buf, ptype, p);
+  sprintf (buf, "HIGH(%s)", name);
+  pic16_emitDS (buf, ptype, p);
 }
 
 /*-----------------------------------------------------------------*/
 /* printPointerType - generates ival for pointer type              */
 /*-----------------------------------------------------------------*/
-void pic16_printPointerType (const char *name, char ptype, void *p)
+static void
+pic16_printPointerType (const char *name, char ptype, void *p)
 {
   _pic16_printPointerType (name, ptype, p);
   //pic16_flushDB(ptype, p); /* breaks char* const arr[] = {&c, &c, &c}; */
@@ -543,46 +538,56 @@ void pic16_printPointerType (const char *name, char ptype, void *p)
 /*-----------------------------------------------------------------*/
 /* printGPointerType - generates ival for generic pointer type     */
 /*-----------------------------------------------------------------*/
-void pic16_printGPointerType (const char *iname, const unsigned int itype,
+static void
+pic16_printGPointerType (const char *iname, const unsigned int itype,
   char ptype, void *p)
 {
   char buf[256];
-  
-    _pic16_printPointerType (iname, ptype, p);
-
-    switch( itype ) {
-      case FPOINTER:
-      case CPOINTER:
-      case GPOINTER:
-      case FUNCTION:
-        {
-          sprintf(buf, "UPPER(%s)", iname);
-          pic16_emitDS(buf, ptype, p);
-        }; break;
-      case POINTER:
-      case IPOINTER:
-        sprintf(buf, "0x80");
-        pic16_emitDS(buf, ptype, p);
-        break;
-      default:
-        debugf("itype = %d\n", itype );
-        assert( 0 );
+
+  _pic16_printPointerType (iname, ptype, p);
+
+  switch (itype)
+    {
+    case CPOINTER: /* fall through */
+    case FUNCTION: /* fall through */
+    case GPOINTER:
+      /* GPTRs pointing to __data space should be reported as POINTERs */
+      sprintf (buf, "UPPER(%s)", iname);
+      pic16_emitDS (buf, ptype, p);
+      break;
+
+    case POINTER:  /* fall through */
+    case FPOINTER: /* fall through */
+    case IPOINTER: /* fall through */
+    case PPOINTER: /* __data space */
+      sprintf (buf, "0x%02x", GPTR_TAG_DATA);
+      pic16_emitDS (buf, ptype, p);
+      break;
+
+    default:
+      debugf ("itype = %d\n", itype );
+      assert (0);
     }
 
-    //pic16_flushDB(ptype, p); /* might break char* const arr[] = {...}; */
+    if (itype == GPOINTER) {
+      fprintf(stderr, "%s: initialized generic pointer with unknown storage class assumes object in code space\n", __func__);
+    }
+
+  //pic16_flushDB(ptype, p); /* might break char* const arr[] = {...}; */
 }
 
 
 /* set to 0 to disable debug messages */
-#define DEBUG_PRINTIVAL        0
+#define DEBUG_PRINTIVAL 0
 
 /*-----------------------------------------------------------------*/
 /* pic16_printIvalType - generates ival for int/char               */
 /*-----------------------------------------------------------------*/
-static void 
+static void
 pic16_printIvalType (symbol *sym, sym_link * type, initList * ilist, char ptype, void *p)
 {
   value *val;
+  int i;
 
 //  fprintf(stderr, "%s for symbol %s\n",__FUNCTION__, sym->rname);
 
@@ -601,39 +606,22 @@ pic16_printIvalType (symbol *sym, sym_link * type, initList * ilist, char ptype,
 
   if (!(val = list2val (ilist))) {
     // assuming a warning has been thrown
-    val=constVal("0");
+    val = constCharVal (0);
   }
 
   if (val->type != type) {
     val = valCastLiteral(type, floatFromVal(val));
   }
 
-  switch (getSize (type)) {
-  case 1:
-    pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-    break;
-
-  case 2:
-    pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-    pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-    break;
-  case 3:
-    pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-    pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-    pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
-  case 4:
-    pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-    pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-    pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
-    pic16_emitDB(pic16aopLiteral(val, 3), ptype, p);
-    break;
-  }
+  for (i = 0; i < getSize (type); i++) {
+    pic16_emitDB(pic16aopLiteral(val, i), ptype, p);
+  } // for
 }
 
 /*--------------------------------------------------------------------*/
 /* pic16_printIvalChar - generates initital value for character array */
 /*--------------------------------------------------------------------*/
-static int 
+static int
 pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, char ptype, void *p)
 {
   value *val;
@@ -654,8 +642,11 @@ pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, ch
       /* length of initializer string (might contain \0, so do not use strlen) */
       ilen = DCL_ELEM(val->type);
 
+#if 0
+      /* This causes structflexarray.c to fail. */
       if(!DCL_ELEM (type))
         DCL_ELEM (type) = ilen;
+#endif
 
       /* len is 0 if declartion equals initializer,
        * >0 if declaration greater than initializer
@@ -671,20 +662,26 @@ pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, ch
 
       if(len >= 0) {
         /* emit initializer */
-        for(remain=0; remain<ilen; remain++)
+        for(remain=0; remain<ilen; remain++) {
           pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
-
-         /* fill array with 0x00 */
-          while(len--) {
-            pic16_emitDB(0x00, ptype, p);
-          }
+        } // for
+
+        /* fill array with 0x00 */
+        while(len--) {
+          pic16_emitDB(0x00, ptype, p);
+        } // while
+      } else if (!DCL_ELEM (type)) {
+        // flexible arrays: char str[] = "something"; */
+        for(remain=0; remain<ilen; remain++) {
+          pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
+        } // for
       } else {
         werror (W_EXCESS_INITIALIZERS, "array of chars", sym->name, sym->lineDef);
-
-        for(remain=0; remain<DCL_ELEM (type); remain++)
+        for(remain=0; remain<DCL_ELEM (type); remain++) {
           pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
-      }
-      
+        } // for
+      } // if
+
 
 //      if((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0) {
 //      }
@@ -701,9 +698,9 @@ pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, ch
 /*-----------------------------------------------------------------*/
 /* pic16_printIvalArray - generates code for array initialization        */
 /*-----------------------------------------------------------------*/
-static void 
+static void
 pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
-               char ptype, void *p)
+                char ptype, void *p)
 {
   initList *iloop;
   int lcnt = 0, size = 0;
@@ -718,15 +715,15 @@ pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
   /* take care of the special   case  */
   /* array of characters can be init  */
   /* by a string                      */
-  if (IS_CHAR (type->next)) {
+  if (IS_CHAR (type->next) && ilist) {
     if (!IS_LITERAL(list2val(ilist)->etype)) {
       werror (W_INIT_WRONG);
       return;
     }
 
     if(pic16_printIvalChar (sym, type,
-                      (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
-                      SPEC_CVAL (sym->etype).v_char, ptype, p))
+                       (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
+                       SPEC_CVAL (sym->etype).v_char, ptype, p))
       return;
   }
   /* not the special case             */
@@ -736,7 +733,7 @@ pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
       return;
     }
 
-  iloop = ilist->init.deep;
+  iloop = (ilist ? ilist->init.deep : NULL);
   lcnt = DCL_ELEM (type);
 
   for (;;)
@@ -749,22 +746,25 @@ pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
       /* if not array limits given & we */
       /* are out of initialisers then   */
       if (!DCL_ELEM (type) && !iloop)
-       break;
+        break;
 
       /* no of elements given and we    */
       /* have generated for all of them */
       if (!--lcnt) {
-       /* if initializers left */
-       if (iloop) {
-         werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
-       }
-       break;
+        /* if initializers left */
+        if (iloop) {
+          werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
+        }
+        break;
       }
     }
 
+#if 0
+  /* This causes bug #1843745. */
   /* if we have not been given a size  */
   if (!DCL_ELEM (type))
     DCL_ELEM (type) = size;
+#endif
 
   return;
 }
@@ -772,12 +772,14 @@ pic16_printIvalArray (symbol * sym, sym_link * type, initList * ilist,
 /*-----------------------------------------------------------------*/
 /* pic16_printIvalBitFields - generate initializer for bitfields   */
 /*-----------------------------------------------------------------*/
-void pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *p)
+static void
+pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *p)
 {
   value *val ;
   symbol *lsym = *sym;
   initList *lilist = *ilist ;
   unsigned long ival = 0;
+  unsigned long i;
   int size =0;
 
 
@@ -787,7 +789,6 @@ void pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *
 
 
   do {
-    unsigned long i;
     val = list2val(lilist);
     if (size) {
       if (SPEC_BLEN(lsym->etype) > 8) {
@@ -798,7 +799,7 @@ void pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *
       size = ((SPEC_BLEN (lsym->etype) / 8) +
               (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
     }
-    i = (unsigned long)floatFromVal(val);
+    i = (ulFromVal (val) & ((1ul << SPEC_BLEN (lsym->etype)) - 1ul));
     i <<= SPEC_BSTR (lsym->etype);
     ival |= i;
     if (! ( lsym->next &&
@@ -808,36 +809,21 @@ void pic16_printIvalBitFields(symbol **sym, initList **ilist, char ptype, void *
     lsym = lsym->next;
     lilist = lilist->next;
   } while (1);
-  switch (size) {
-  case 1:
-       pic16_emitDB(BYTE_IN_LONG(ival, 0), ptype, p);
-       break;
-
-  case 2:
-       pic16_emitDB(BYTE_IN_LONG(ival, 0), ptype, p);
-       pic16_emitDB(BYTE_IN_LONG(ival, 1), ptype, p);
-    break;
-
-  case 4: /* EEP: why is this db and not dw? */
-       pic16_emitDB(BYTE_IN_LONG(ival, 0), ptype, p);
-       pic16_emitDB(BYTE_IN_LONG(ival, 1), ptype, p);
-       pic16_emitDB(BYTE_IN_LONG(ival, 2), ptype, p);
-       pic16_emitDB(BYTE_IN_LONG(ival, 3), ptype, p);
-    break;
-  default:
-       /* VR - only 1,2,4 size long can be handled???? Why? */
-       fprintf(stderr, "%s:%d: unhandled case. Contact author.\n", __FILE__, __LINE__);
-       assert(0);
-  }
+
+  for (i = 0; i < size; i++) {
+    pic16_emitDB(BYTE_IN_LONG(ival, i), ptype, p);
+  } // for
+
   *sym = lsym;
   *ilist = lilist;
 }
+
 
 /*-----------------------------------------------------------------*/
 /* printIvalStruct - generates initial value for structures        */
 /*-----------------------------------------------------------------*/
-void pic16_printIvalStruct (symbol * sym, sym_link * type,
+static void
+pic16_printIvalStruct (symbol * sym, sym_link * type,
                  initList * ilist, char ptype, void *p)
 {
   symbol *sflds;
@@ -859,7 +845,7 @@ void pic16_printIvalStruct (symbol * sym, sym_link * type,
     iloop = ilist->init.deep;
   }
 
-  for (; (sflds && iloop); sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
+  for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
 //    fprintf(stderr, "%s:%d sflds: %p\tiloop = %p\n", __FILE__, __LINE__, sflds, iloop);
     if (IS_BITFIELD(sflds->type)) {
       pic16_printIvalBitFields(&sflds, &iloop, ptype, p);
@@ -876,43 +862,41 @@ void pic16_printIvalStruct (symbol * sym, sym_link * type,
 /*-----------------------------------------------------------------*/
 /* printIvalUnion - generates initial value for unions             */
 /*-----------------------------------------------------------------*/
-void pic16_printIvalUnion (symbol * sym, sym_link * type,
+static void
+pic16_printIvalUnion (symbol * sym, sym_link * type,
                  initList * ilist, char ptype, void *p)
 {
   //symbol *sflds;
   initList *iloop = NULL;
-  int i, size;
-
+  int size;
+  symbol *sflds = NULL;
 
 #if DEBUG_PRINTIVAL
   fprintf(stderr, "%s\n",__FUNCTION__);
 #endif
 
-  iloop = ilist;
-  i = 0;
-  while (iloop)
-  {
-    i++;
-    iloop = iloop->next;
-  } // while
+  assert (type);
 
-  size = -1;
-  if (type) size = SPEC_STRUCT(type)->size;
+  sflds = SPEC_STRUCT (type)->fields;
 
-  if (i == 1 && size >= 0 && size <= sizeof(long))
-  {
-    unsigned long val = (unsigned long)floatFromVal(list2val(ilist));
-    while (size--)
-    {
-      pic16_emitDB(val, ptype, p);
-      val >>= 8;
-    } // while
-    return;
-  } // if
+  if (ilist) {
+    if (ilist->type != INIT_DEEP) {
+      werrorfl (sym->fileDef, sym->lineDef, E_INIT_STRUCT, sym->name);
+      return;
+    }
 
-  fprintf( stderr, "INCOMPLETE SUPPORT FOR INITIALIZED union---FALLING BACK TO struct\n" );
-  fprintf( stderr, "This is a bug. Please file a bug-report with your source attached.\n" );
-  pic16_printIvalStruct( sym, type, ilist, ptype, p );
+    iloop = ilist->init.deep;
+  }
+
+  size = SPEC_STRUCT(type)->size;
+  sflds = SPEC_STRUCT(type)->fields;
+  pic16_printIval (sym, sflds->type, iloop, ptype, p);
+
+  /* if the first field is not the longest, fill with 0s */
+  while (size > getSize (sflds->type)) {
+      pic16_emitDB(0, ptype, p);
+      size--;
+  } // while
 }
 
 static int
@@ -925,9 +909,11 @@ pic16_isUnion( symbol *sym, sym_link *type )
 /*--------------------------------------------------------------------------*/
 /* pic16_printIvalCharPtr - generates initial values for character pointers */
 /*--------------------------------------------------------------------------*/
-int pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char ptype, void *p)
+static int
+pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char ptype, void *p)
 {
   int size = 0;
+  int i;
 
   /* PENDING: this is _very_ mcs51 specific, including a magic
      number...
@@ -947,7 +933,7 @@ int pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char pty
     {
       if (size == 1)            /* This appears to be Z80 specific?? */
         {
-         pic16_emitDS(val->name, ptype, p);
+          pic16_emitDS(val->name, ptype, p);
         }
       else if (size == 2)
         {
@@ -956,14 +942,10 @@ int pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char pty
       else if (size == 3)
         {
           int type;
-          if (IS_PTR (val->type)) {
-            type = DCL_TYPE (val->type);
-          } else {
-            type = PTR_TYPE (SPEC_OCLS (val->etype));
-          }
+          type = PTR_TYPE (SPEC_OCLS (val->etype));
           if (val->sym && val->sym->isstrlit) {
             // this is a literal string
-            type=CPOINTER;
+            type = CPOINTER;
           }
           pic16_printGPointerType(val->name, type, ptype, p);
         }
@@ -971,35 +953,21 @@ int pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char pty
         {
           fprintf (stderr, "*** internal error: unknown size in "
                    "printIvalCharPtr.\n");
-         assert(0);
+          assert(0);
         }
     }
   else
     {
       // these are literals assigned to pointers
-      switch (size)
+      for (i = 0; i < size; i++)
         {
-        case 1:
-         pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-          break;
-        case 2:
-         pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-         pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-          break;
-        case 3:
-         pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-         pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-         pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
-          break;
-
-        default:
-          assert (0);
-        }
+          pic16_emitDB(pic16aopLiteral(val, i), ptype, p);
+        } // for
     }
 
-  if (val->sym && val->sym->isstrlit) {        // && !isinSet(statsg->syms, val->sym)) {
-       if(ptype == 'p' && !isinSet(statsg->syms, val->sym))addSet (&statsg->syms, val->sym);
-       else if(ptype == 'f' /*&& !isinSet(rel_idataSymSet, val->sym)*/)addSet(&rel_idataSymSet, val->sym);
+  if (val->sym && val->sym->isstrlit) { // && !isinSet(statsg->syms, val->sym)) {
+        if(ptype == 'p' && !isinSet(statsg->syms, val->sym))addSet (&statsg->syms, val->sym);
+        else if(ptype == 'f' /*&& !isinSet(rel_idataSymSet, val->sym)*/)addSet(&rel_idataSymSet, val->sym);
   }
 
   return 1;
@@ -1008,7 +976,8 @@ int pic16_printIvalCharPtr (symbol * sym, sym_link * type, value * val, char pty
 /*-----------------------------------------------------------------------*/
 /* pic16_printIvalFuncPtr - generate initial value for function pointers */
 /*-----------------------------------------------------------------------*/
-void pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void *p)
+static void
+pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void *p)
 {
   value *val;
   int dLvl = 0;
@@ -1025,11 +994,11 @@ void pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void
 
   if (!val) {
     // an error has been thrown already
-    val=constVal("0");
+    val = constCharVal (0);
   }
 
   if (IS_LITERAL(val->etype)) {
-    if (compareType(type, val->etype) == 0) {
+    if (0 && compareType(type, val->etype) == 0) {
       werrorfl (ilist->filename, ilist->lineno, E_INCOMPAT_TYPES);
       printFromToType (val->type, type);
     }
@@ -1046,19 +1015,19 @@ void pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void
 
   /* now generate the name */
   if (!val->sym) {
-      pic16_printGPointerType (val->name, DCL_TYPE(val->type), ptype, p);
+      pic16_printGPointerType (val->name, CPOINTER /*DCL_TYPE(val->type)*/, ptype, p);
   } else {
-      pic16_printGPointerType (val->sym->rname, DCL_TYPE(val->type), ptype, p);
+      pic16_printGPointerType (val->sym->rname, CPOINTER /*DCL_TYPE(val->type)*/, ptype, p);
 
       if(IS_FUNC(val->sym->type) && !val->sym->used && !IS_STATIC(val->sym->etype)) {
-        
+
         if(!checkSym(publics, val->sym))
-         if(checkAddSym(&externs, val->sym) && (ptype == 'f')) {
-           /* this has not been declared as extern
-            * so declare it as a 'late extern' just after the symbol */
-           fprintf((FILE *)p, ";\tdeclare symbol as extern\n");
-           fprintf((FILE *)p, "\textern\t%s\n", val->sym->rname);
-           fprintf((FILE *)p, ";\tcontinue variable declaration\n");
+          if(checkAddSym(&externs, val->sym) && (ptype == 'f')) {
+            /* this has not been declared as extern
+             * so declare it as a 'late extern' just after the symbol */
+            fprintf((FILE *)p, ";\tdeclare symbol as extern\n");
+            fprintf((FILE *)p, "\textern\t%s\n", val->sym->rname);
+            fprintf((FILE *)p, ";\tcontinue variable declaration\n");
           }
       }
   }
@@ -1070,14 +1039,16 @@ void pic16_printIvalFuncPtr (sym_link * type, initList * ilist, char ptype, void
 /*-----------------------------------------------------------------*/
 /* pic16_printIvalPtr - generates initial value for pointers       */
 /*-----------------------------------------------------------------*/
-void pic16_printIvalPtr (symbol * sym, sym_link * type, initList * ilist, char ptype, void *p)
+static void
+pic16_printIvalPtr (symbol * sym, sym_link * type, initList * ilist, char ptype, void *p)
 {
   value *val;
   int size;
+  int i;
 
 #if 0
-       fprintf(stderr, "%s:%d initialising pointer: %s size: %d\n", __FILE__, __LINE__,
-               sym->rname, getSize(sym->type));
+        fprintf(stderr, "%s:%d initialising pointer: %s size: %d\n", __FILE__, __LINE__,
+                sym->rname, getSize(sym->type));
 #endif
 
   /* if deep then   */
@@ -1105,48 +1076,36 @@ void pic16_printIvalPtr (symbol * sym, sym_link * type, initList * ilist, char p
     printFromToType (val->type, type);
   }
 
+  size = getSize (type);
+
   /* if val is literal */
   if (IS_LITERAL (val->etype))
     {
-      switch (getSize (type))
+      for (i = 0; i < size; i++)
         {
-        case 1:
-            pic16_emitDB((unsigned int)floatFromVal(val) & 0xff, ptype, p);
-            break;
-        case 2:
-            pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-            pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-            break;
-        case 3:
-            pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
-            pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-            pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
-            break;
-        default:
-               fprintf(stderr, "%s:%d size = %d\n", __FILE__, __LINE__, getSize(type));
-               assert(0);
-        }
+          pic16_emitDB(pic16aopLiteral(val, i), ptype, p);
+        } // for
       return;
     }
 
-
-  size = getSize (type);
-
   if (size == 1)                /* Z80 specific?? */
     {
       pic16_emitDS(val->name, ptype, p);
     }
   else if (size == 2)
     {
-        pic16_printPointerType (val->name, ptype, p);
+      pic16_printPointerType (val->name, ptype, p);
     }
   else if (size == 3)
     {
-      pic16_printGPointerType (val->name, (IS_PTR(type)?DCL_TYPE(type):PTR_TYPE(SPEC_OCLS(sym->etype))),
-                          ptype, p);
-    } else
-       assert(0);
-  return;
+      int itype = 0;
+      itype = PTR_TYPE (SPEC_OCLS (val->etype));
+      pic16_printGPointerType (val->name, itype, ptype, p);
+    }
+  else
+    {
+      assert(0);
+    }
 }
 
 
@@ -1157,16 +1116,16 @@ void pic16_printIvalPtr (symbol * sym, sym_link * type, initList * ilist, char p
 void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptype, void *p)
 {
 //  sym_link *itype;
-  
+
   if (!p)
     return;
 
 #if 0
-       fprintf(stderr, "%s:%d generating init for %s\n", __FILE__, __LINE__, sym->name);
-       fprintf(stderr, "%s: IS_STRUCT: %d  IS_ARRAY: %d  IS_PTR: %d  IS_SPEC: %d\n", sym->name,
-               IS_STRUCT(type), IS_ARRAY(type), IS_PTR(type), IS_SPEC(type));
+        fprintf(stderr, "%s:%d generating init for %s\n", __FILE__, __LINE__, sym->name);
+        fprintf(stderr, "%s: IS_STRUCT: %d  IS_ARRAY: %d  IS_PTR: %d  IS_SPEC: %d\n", sym->name,
+                IS_STRUCT(type), IS_ARRAY(type), IS_PTR(type), IS_SPEC(type));
 #endif
-       
+
   /* if structure then */
   if (IS_STRUCT (type))
     {
@@ -1174,7 +1133,7 @@ void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptyp
         {
           //fprintf(stderr,"%s union\n",__FUNCTION__);
           pic16_printIvalUnion (sym, type, ilist, ptype, p);
-       } else {
+        } else {
           //fprintf(stderr,"%s struct\n",__FUNCTION__);
           pic16_printIvalStruct (sym, type, ilist, ptype, p);
         }
@@ -1184,7 +1143,7 @@ void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptyp
   /* if this is an array */
   if (IS_ARRAY (type))
     {
-//     fprintf(stderr,"%s array\n",__FUNCTION__);
+//      fprintf(stderr,"%s array\n",__FUNCTION__);
       pic16_printIvalArray (sym, type, ilist, ptype, p);
       return;
     }
@@ -1234,29 +1193,24 @@ void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptyp
   /* if type is SPECIFIER */
   if (IS_SPEC (type))
     {
-//     fprintf(stderr,"%s spec\n",__FUNCTION__);
+//      fprintf(stderr,"%s spec\n",__FUNCTION__);
       pic16_printIvalType (sym, type, ilist, ptype, p);
       return;
     }
 }
 
-int PIC16_IS_CONFIG_ADDRESS(int address)
+static int
+PIC16_IS_CONFIG_ADDRESS(int address)
 {
   return ((address >= pic16->cwInfo.confAddrStart && address <= pic16->cwInfo.confAddrEnd));
 }
 
-int PIC16_IS_IDLOC_ADDRESS(int address)
+static int
+PIC16_IS_IDLOC_ADDRESS(int address)
 {
    return ((address >= pic16->idInfo.idAddrStart && address <= pic16->idInfo.idAddrEnd));
 }
 
-/* wrapper function for the above */
-int PIC16_IS_HWREG_ADDRESS(int address)
-{
-  return (PIC16_IS_CONFIG_ADDRESS(address) || PIC16_IS_IDLOC_ADDRESS(address));
-}
-
-
 /*-----------------------------------------------------------------*/
 /* emitStaticSeg - emitcode for the static segment                 */
 /*-----------------------------------------------------------------*/
@@ -1264,171 +1218,168 @@ static void
 pic16emitStaticSeg (memmap * map)
 {
   symbol *sym;
-  static int didcode=0;
+  static int didcode = 0;
 
   //fprintf(stderr, "%s\n",__FUNCTION__);
 
-  pic16_initDB();
+  pic16_initDB ();
 
   /* for all variables in this segment do */
-  for (sym = setFirstItem (map->syms); sym;
-       sym = setNextItem (map->syms))
+  for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms))
     {
 
 #if 0
-       fprintf(stderr, "%s\t%s: sym: %s\tused: %d\tSPEC_ABSA: %d\tSPEC_AGGREGATE: %d\tCODE: %d\n\
-CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__,
-               map->sname, sym->name, sym->used, SPEC_ABSA(sym->etype), IS_AGGREGATE(sym->type),
-               IS_CODE(sym->etype), IN_CODESPACE( SPEC_OCLS(sym->etype)), IS_CONSTANT(sym->etype),
-               IS_PTR_CONST(sym->etype), SPEC_CONST(sym->etype));
-       printTypeChain( sym->type, stderr );
-       fprintf(stderr, "\n");
+      fprintf (stderr, "%s\t%s: sym: %s\tused: %d\tSPEC_ABSA: %d\tSPEC_AGGREGATE: %d\tCODE: %d\n\
+CODESPACE: %d\tCONST: %d\tPTRCONST: %d\tSPEC_CONST: %d\n", __FUNCTION__, map->sname, sym->name, sym->used, SPEC_ABSA (sym->etype), IS_AGGREGATE (sym->type), IS_CODE (sym->etype), IN_CODESPACE (SPEC_OCLS (sym->etype)), IS_CONSTANT (sym->etype), IS_PTR_CONST (sym->etype), SPEC_CONST (sym->etype));
+      printTypeChain (sym->type, stderr);
+      fprintf (stderr, "\n");
 #endif
 
-        if(SPEC_ABSA(sym->etype) && PIC16_IS_CONFIG_ADDRESS(SPEC_ADDR(sym->etype))) {
-               pic16_assignConfigWordValue(SPEC_ADDR(sym->etype),
-                       (int) floatFromVal(list2val(sym->ival)));
+      if (SPEC_ABSA (sym->etype) && PIC16_IS_CONFIG_ADDRESS (SPEC_ADDR (sym->etype)))
+        {
+          pic16_assignConfigWordValue (SPEC_ADDR (sym->etype), (int) ulFromVal (list2val (sym->ival)));
 
-               continue;
+          continue;
         }
 
-        if(SPEC_ABSA(sym->etype) && PIC16_IS_IDLOC_ADDRESS(SPEC_ADDR(sym->etype))) {
-               pic16_assignIdByteValue(SPEC_ADDR(sym->etype),
-                       (char) floatFromVal(list2val(sym->ival)));
+      if (SPEC_ABSA (sym->etype) && PIC16_IS_IDLOC_ADDRESS (SPEC_ADDR (sym->etype)))
+        {
+          pic16_assignIdByteValue (SPEC_ADDR (sym->etype), (char) ulFromVal (list2val (sym->ival)));
 
-               continue;
+          continue;
         }
 
-       /* if it is "extern" then do nothing */
-       if (IS_EXTERN (sym->etype)/* && !SPEC_ABSA(sym->etype)*/) {
-               checkAddSym(&externs, sym);
-         continue;
-       }
+      /* if it is "extern" then do nothing */
+      if (IS_EXTERN (sym->etype) /* && !SPEC_ABSA(sym->etype) */ )
+        {
+          checkAddSym (&externs, sym);
+          continue;
+        }
 
-       /* if it is not static add it to the public
-          table */
-       if (!IS_STATIC (sym->etype)) {
-               /* do not emit if it is a config word declaration */
-               checkAddSym(&publics, sym);
-       }
+      /* if it is not static add it to the public
+         table */
+      if (!IS_STATIC (sym->etype))
+        {
+          /* do not emit if it is a config word declaration */
+          checkAddSym (&publics, sym);
+        }
 
       /* print extra debug info if required */
-      if (options.debug || sym->level == 0) {
-         /* NOTE to me - cdbFile may be null in which case,
-          * the sym name will be printed to stdout. oh well */
-          debugFile->writeSymbol(sym);
-      }
-        
+      if (options.debug || sym->level == 0)
+        {
+          /* NOTE to me - cdbFile may be null in which case,
+           * the sym name will be printed to stdout. oh well */
+          debugFile->writeSymbol (sym);
+        }
+
       /* if it has an absolute address */
-      if (SPEC_ABSA (sym->etype)) {
-//             fprintf(stderr, "%s:%d spec_absa is true for symbol: %s\n",
-//                     __FILE__, __LINE__, sym->name);
-                       
-         /* if it has an initial value */
-         if (sym->ival)
-           {
-             pBlock *pb;
+      if (SPEC_ABSA (sym->etype))
+        {
+//        fprintf(stderr, "%s:%d spec_absa is true for symbol: %s\n",
+//                __FILE__, __LINE__, sym->name);
+
+          if (!sym->ival && IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) && SPEC_CVAL (sym->etype).v_char)
+            {
+              /* symbol has absolute address but no initial value */
+              /* special case for character strings */
+
+//            fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, sym->rname);
+
+              pic16_pCodeConstString (sym->rname, SPEC_CVAL (sym->etype).v_char, getSize (sym->type));
+            }
+          else
+            {
+              pBlock *pb;
               symbol *asym;
               absSym *abSym;
               pCode *pcf;
-              
+
               /* symbol has absolute address and initial value */
-             noAlloc++;
-             resolveIvalSym (sym->ival, sym->type);
-             asym = newSymbol(sym->rname, 0);
-             abSym = Safe_calloc(1, sizeof(absSym));
-             strcpy(abSym->name, sym->rname);
-             abSym->address = SPEC_ADDR( sym->etype );
-             addSet(&absSymSet, abSym);
-             
-             pb = pic16_newpCodeChain(NULL, 'A', pic16_newpCodeCharP("; Starting pCode block for absolute Ival"));
-             pic16_addpBlock(pb);
-
-             pcf = pic16_newpCodeFunction(moduleName, asym->name);
-             PCF(pcf)->absblock = 1;
-             
-             pic16_addpCode2pBlock(pb,pcf);
-             pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(sym->rname,-1));
-             //fprintf(stderr, "%s:%d [1] generating init for label: %s\n", __FILE__, __LINE__, sym->rname);
-             pic16_printIval(sym, sym->type, sym->ival, 'p', (void *)pb);
-              pic16_flushDB('p', (void *)pb);
-
-             pic16_addpCode2pBlock(pb, pic16_newpCodeFunction(NULL, NULL));
-             noAlloc--;
-           }
-         else
-           {
-
-             /* symbol has absolute address but no initial value */
-             
-             /* allocate space */
-             fprintf (code->oFile, "%s:\n", sym->rname);
-
-             /* special case for character strings */
-             if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) &&
-                 SPEC_CVAL (sym->etype).v_char) {
-
-//             fprintf(stderr, "%s:%d printing code string from %s\n", __FILE__, __LINE__, sym->rname);
-
-               pic16_pCodeConstString(sym->rname , SPEC_CVAL (sym->etype).v_char);
-             } else {
-               fprintf (stderr, "%s:%u(%s): do not know how to intialize symbol %s\n", __FILE__, __LINE__, __FUNCTION__, sym->rname);
-               assert(0);
-             }
-           }
-  
-       } else {
-//             fprintf(stderr, "%s:%d spec_absa is false for symbol: %s\n",
-//                     __FILE__, __LINE__, sym->name);
-
-               
-         /* if it has an initial value */
-         if (sym->ival) {
-             pBlock *pb;
-
-             /* symbol doesn't have absolute address but has initial value */
-             fprintf (code->oFile, "%s:\n", sym->rname);
-             noAlloc++;
-             resolveIvalSym (sym->ival, sym->type);
-
-             pb = pic16_newpCodeChain(NULL, 'P',pic16_newpCodeCharP("; Starting pCode block for Ival"));
-             pic16_addpBlock(pb);
-
-             if(!didcode) {
-               /* make sure that 'code' directive is emitted before, once */
-               pic16_addpCode2pBlock(pb, pic16_newpCodeAsmDir("code", NULL));
-               
-               didcode++;
-             }
-
-             pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(sym->rname,-1));
-             //fprintf(stderr, "%s:%d [2] generating init for label: %s\n", __FILE__, __LINE__, sym->rname);
-             pic16_printIval(sym, sym->type, sym->ival, 'p', (void *)pb);
-              pic16_flushDB('p', (void *)pb);
-             noAlloc--;
-           } else {
-
-             /* symbol doesn't have absolute address and no initial value */
-             /* allocate space */
-//           fprintf(stderr, "%s:%d [3] generating init for label: %s\n", __FILE__, __LINE__, sym->rname);
-             fprintf (code->oFile, "%s:\n", sym->rname);
-             /* special case for character strings */
-             if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) &&
-                 SPEC_CVAL (sym->etype).v_char) {
-               
-//             fprintf(stderr, "%s:%d printing code string for %s\n", __FILE__, __LINE__, sym->rname);
-
-               pic16_pCodeConstString(sym->rname , SPEC_CVAL (sym->etype).v_char);
-             } else {
-               assert(0);
-             }
-           }
-       }
+              ++noAlloc;
+              resolveIvalSym (sym->ival, sym->type);
+              asym = newSymbol (sym->rname, 0);
+              abSym = Safe_calloc (1, sizeof (absSym));
+              strcpy (abSym->name, sym->rname);
+              abSym->address = SPEC_ADDR (sym->etype);
+              addSet (&absSymSet, abSym);
+
+              pb = pic16_newpCodeChain (NULL, 'A', pic16_newpCodeCharP ("; Starting pCode block for absolute Ival"));
+              pic16_addpBlock (pb);
+
+              pcf = pic16_newpCodeFunction (moduleName, asym->name);
+              PCF (pcf)->absblock = 1;
+
+              pic16_addpCode2pBlock (pb, pcf);
+              pic16_addpCode2pBlock (pb, pic16_newpCodeLabel (sym->rname, -1));
+              //fprintf(stderr, "%s:%d [1] generating init for label: %s\n", __FILE__, __LINE__, sym->rname);
+              /* if it has an initial value */
+              if (sym->ival)
+                {
+                  pic16_printIval (sym, sym->type, sym->ival, 'p', (void *) pb);
+                  pic16_flushDB ('p', (void *) pb);
+                }
+
+              pic16_addpCode2pBlock (pb, pic16_newpCodeFunction (NULL, NULL));
+              --noAlloc;
+            }
+        }
+      else
+        {
+//        fprintf(stderr, "%s:%d spec_absa is false for symbol: %s\n",
+//               __FILE__, __LINE__, sym->name);
+
+
+          /* if it has an initial value */
+          if (sym->ival)
+            {
+              pBlock *pb;
+
+              /* symbol doesn't have absolute address but has initial value */
+              dbuf_printf (&code->oBuf, "%s:\n", sym->rname);
+              ++noAlloc;
+              resolveIvalSym (sym->ival, sym->type);
+
+              pb = pic16_newpCodeChain (NULL, 'P', pic16_newpCodeCharP ("; Starting pCode block for Ival"));
+              pic16_addpBlock (pb);
+
+              if (!didcode)
+                {
+                  /* make sure that 'code' directive is emitted before, once */
+                  pic16_addpCode2pBlock (pb, pic16_newpCodeAsmDir ("code", NULL));
+
+                  ++didcode;
+                }
+
+              pic16_addpCode2pBlock (pb, pic16_newpCodeLabel (sym->rname, -1));
+              //fprintf(stderr, "%s:%d [2] generating init for label: %s\n", __FILE__, __LINE__, sym->rname);
+              pic16_printIval (sym, sym->type, sym->ival, 'p', (void *) pb);
+              pic16_flushDB ('p', (void *) pb);
+              --noAlloc;
+            }
+          else
+            {
+
+              /* symbol doesn't have absolute address and no initial value */
+              /* allocate space */
+//            fprintf(stderr, "%s:%d [3] generating init for label: %s\n", __FILE__, __LINE__, sym->rname);
+              dbuf_printf (&code->oBuf, "%s:\n", sym->rname);
+              /* special case for character strings */
+              if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) && SPEC_CVAL (sym->etype).v_char)
+                {
+
+//                fprintf(stderr, "%s:%d printing code string for %s\n", __FILE__, __LINE__, sym->rname);
+
+                  pic16_pCodeConstString (sym->rname, SPEC_CVAL (sym->etype).v_char, getSize (sym->type));
+                }
+              else
+                {
+                  assert (0);
+                }
+            }
+        }
     }
-
 }
 
-
 /*-----------------------------------------------------------------*/
 /* pic16_emitConfigRegs - emits the configuration registers              */
 /*-----------------------------------------------------------------*/
@@ -1436,22 +1387,22 @@ void pic16_emitConfigRegs(FILE *of)
 {
   int i;
 
-       for(i=0;i<=(pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart);i++)
-               if(pic16->cwInfo.crInfo[i].emit)        //mask != -1)
-                       fprintf (of, "\t__config 0x%x, 0x%hhx\n",
-                               pic16->cwInfo.confAddrStart+i,
-                               pic16->cwInfo.crInfo[i].value);
+        for(i=0;i<=(pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart);i++)
+                if(pic16->cwInfo.crInfo[i].emit)        //mask != -1)
+                        fprintf (of, "\t__config 0x%x, 0x%hhx\n",
+                                pic16->cwInfo.confAddrStart+i,
+                                pic16->cwInfo.crInfo[i].value);
 }
 
 void pic16_emitIDRegs(FILE *of)
 {
   int i;
 
-       for(i=0;i<=(pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart);i++)
-               if(pic16->idInfo.irInfo[i].emit)
-                       fprintf (of, "\t__idlocs 0x%06x, 0x%hhx\n",
-                               pic16->idInfo.idAddrStart+i,
-                               pic16->idInfo.irInfo[i].value);
+        for(i=0;i<=(pic16->idInfo.idAddrEnd-pic16->idInfo.idAddrStart);i++)
+                if(pic16->idInfo.irInfo[i].emit)
+                        fprintf (of, "\t__idlocs 0x%06x, 0x%hhx\n",
+                                pic16->idInfo.idAddrStart+i,
+                                pic16->idInfo.irInfo[i].value);
 }
 
 
@@ -1468,36 +1419,37 @@ pic16emitMaps ()
   pic16emitRegularMap (sfrbit, FALSE, FALSE);
   pic16emitRegularMap (code, TRUE, FALSE);
   pic16emitStaticSeg (statsg);
+  pic16emitStaticSeg (c_abs);
 }
 
 /*-----------------------------------------------------------------*/
 /* createInterruptVect - creates the interrupt vector              */
 /*-----------------------------------------------------------------*/
 static void
-pic16createInterruptVect (FILE * vFile)
+pic16createInterruptVect (struct dbuf_s * vBuf)
 {
-       /* if the main is only a prototype ie. no body then do nothing */
+        /* if the main is only a prototype ie. no body then do nothing */
 #if 0
-       if (!IFFUNC_HASBODY(mainf->type)) {
-               /* if ! compile only then main function should be present */
-               if (!options.cc_only)
-                       werror (E_NO_MAIN);
-               return;
-       }
+        if (!IFFUNC_HASBODY(mainf->type)) {
+                /* if ! compile only then main function should be present */
+                if (!options.cc_only)
+                        werror (E_NO_MAIN);
+                return;
+        }
 #endif
 #if 0
-       if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
-               fprintf (vFile, ";\t.area\t%s\n", CODE_NAME);
-               fprintf(vFile, ".intvecs\tcode\t0x%06x\n", pic16_options.ivt_loc);
-
-               /* this is an overkill since WE are the port,
-                * and we know if we have a genIVT function! */
-               if(port->genIVT) {
-                       port->genIVT(vFile, interrupts, maxInterrupts);
-               }
-       }
+        if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
+                dbuf_printf (vBuf, ";\t.area\t%s\n", CODE_NAME);
+                dbuf_printf (vBuf, ".intvecs\tcode\t0x%06x\n", pic16_options.ivt_loc);
+
+                /* this is an overkill since WE are the port,
+                 * and we know if we have a genIVT function! */
+                if(port->genIVT) {
+                        port->genIVT(vFile, interrupts, maxInterrupts);
+                }
+        }
 #endif
-       
+
 }
 
 
@@ -1507,19 +1459,56 @@ pic16createInterruptVect (FILE * vFile)
 static void
 pic16initialComments (FILE * afile)
 {
-       initialComments (afile);
-       fprintf (afile, "; PIC16 port for the Microchip 16-bit core micros\n");
-       if(xinst)
-         fprintf (afile, "; * Extended Instruction Set\n");
-         
-       if(pic16_mplab_comp)
-               fprintf(afile, "; * MPLAB/MPASM/MPASMWIN/MPLINK compatibility mode enabled\n");
-       fprintf (afile, iComments2);
-
-       if(options.debug) {
-               fprintf (afile, "\n\t.ident \"SDCC version %s #%s [pic16 port]%s\"\n",
-                               SDCC_VERSION_STR, getBuildNumber(), (!xinst?"":" {extended}") );
-       }
+    initialComments (afile);
+    fprintf (afile, "; PIC16 port for the Microchip 16-bit core micros\n");
+    if (pic16_options.xinst) {
+        fprintf (afile, "; * Extended Instruction Set\n");
+    } // if
+
+    if (pic16_mplab_comp) {
+        fprintf(afile, "; * MPLAB/MPASM/MPASMWIN/MPLINK compatibility mode enabled\n");
+    } // if
+    fprintf (afile, "%s", iComments2);
+
+    if (options.debug) {
+        fprintf (afile, "\n\t.ident \"SDCC version %s #%s [pic16 port]%s\"\n",
+                SDCC_VERSION_STR, getBuildNumber(), (!pic16_options.xinst?"":" {extended}") );
+    } // if
+}
+
+int
+pic16_stringInSet(const char *str, set **world, int autoAdd)
+{
+  char *s;
+
+  if (!str) return 1;
+  assert(world);
+
+  for (s = setFirstItem(*world); s; s = setNextItem(*world))
+  {
+    /* found in set */
+    if (0 == strcmp(s, str)) return 1;
+  }
+
+  /* not found */
+  if (autoAdd) addSet(world, Safe_strdup(str));
+  return 0;
+}
+
+static int
+pic16_emitSymbolIfNew(FILE *file, const char *fmt, const char *sym, int checkLocals)
+{
+  static set *emitted = NULL;
+
+  if (!pic16_stringInSet(sym, &emitted, 1)) {
+    /* sym was not in emittedSymbols */
+    if (!checkLocals || !pic16_stringInSet(sym, &pic16_localFunctions, 0)) {
+      /* sym is not a locally defined function---avoid bug #1443651 */
+      fprintf( file, fmt, sym );
+      return 0;
+    }
+  }
+  return 1;
 }
 
 /*-----------------------------------------------------------------*/
@@ -1530,14 +1519,14 @@ pic16printPublics (FILE *afile)
 {
   symbol *sym;
 
-       fprintf (afile, "\n%s", iComments2);
-       fprintf (afile, "; public variables in this module\n");
-       fprintf (afile, "%s", iComments2);
+        fprintf (afile, "\n%s", iComments2);
+        fprintf (afile, "; public variables in this module\n");
+        fprintf (afile, "%s", iComments2);
 
-       for(sym = setFirstItem (publics); sym; sym = setNextItem (publics))
-         /* sanity check */
-         if(!IS_STATIC(sym->etype))
-               fprintf(afile, "\tglobal %s\n", sym->rname);
+        for(sym = setFirstItem (publics); sym; sym = setNextItem (publics))
+          /* sanity check */
+          if(!IS_STATIC(sym->etype))
+                pic16_emitSymbolIfNew(afile, "\tglobal %s\n", sym->rname, 0);
 }
 
 /*-----------------------------------------------------------------*/
@@ -1548,31 +1537,31 @@ pic16_printExterns(FILE *afile)
 {
   symbol *sym;
 
-       /* print nothing if no externs to declare */
-       if(!elementsInSet(externs) && !elementsInSet(pic16_builtin_functions))
-               return;
+        /* print nothing if no externs to declare */
+        if(!elementsInSet(externs) && !elementsInSet(pic16_builtin_functions))
+                return;
 
-       fprintf(afile, "\n%s", iComments2);
-       fprintf(afile, "; extern variables in this module\n");
-       fprintf(afile, "%s", iComments2);
-       
-       for(sym = setFirstItem(externs); sym; sym = setNextItem(externs))
-               fprintf(afile, "\textern %s\n", sym->rname);
+        fprintf(afile, "\n%s", iComments2);
+        fprintf(afile, "; extern variables in this module\n");
+        fprintf(afile, "%s", iComments2);
 
-       for(sym = setFirstItem(pic16_builtin_functions); sym; sym = setNextItem(pic16_builtin_functions))
-               fprintf(afile, "\textern _%s\n", sym->name);
+        for(sym = setFirstItem(externs); sym; sym = setNextItem(externs))
+                pic16_emitSymbolIfNew(afile, "\textern %s\n", sym->rname, 1);
+
+        for(sym = setFirstItem(pic16_builtin_functions); sym; sym = setNextItem(pic16_builtin_functions))
+                pic16_emitSymbolIfNew(afile, "\textern _%s\n", sym->name, 1);
 }
 
 /*-----------------------------------------------------------------*/
 /* emitOverlay - will emit code for the overlay stuff              */
 /*-----------------------------------------------------------------*/
 static void
-pic16emitOverlay (FILE * afile)
+pic16emitOverlay (struct dbuf_s *aBuf)
 {
   set *ovrset;
 
   if (!elementsInSet (ovrSetSets))
-    fprintf (afile, ";\t.area\t%s\n", port->mem.overlay_name);
+    dbuf_printf (aBuf, ";\t.area\t%s\n", port->mem.overlay_name);
 
   /* for each of the sets in the overlay segment do */
   for (ovrset = setFirstItem (ovrSetSets); ovrset;
@@ -1582,74 +1571,75 @@ pic16emitOverlay (FILE * afile)
       symbol *sym;
 
       if (elementsInSet (ovrset))
-       {
-         /* this dummy area is used to fool the assembler
-            otherwise the assembler will append each of these
-            declarations into one chunk and will not overlay
-            sad but true */
-         fprintf (afile, ";\t.area _DUMMY\n");
-         /* output the area informtion */
-         fprintf (afile, ";\t.area\t%s\n", port->mem.overlay_name);    /* MOF */
-       }
+        {
+          /* this dummy area is used to fool the assembler
+             otherwise the assembler will append each of these
+             declarations into one chunk and will not overlay
+             sad but true */
+          dbuf_printf (aBuf, ";\t.area _DUMMY\n");
+          /* output the area informtion */
+          dbuf_printf (aBuf, ";\t.area\t%s\n", port->mem.overlay_name); /* MOF */
+        }
 
       for (sym = setFirstItem (ovrset); sym;
-          sym = setNextItem (ovrset))
-       {
-
-         /* if extern then do nothing */
-         if (IS_EXTERN (sym->etype))
-           continue;
-
-         /* if allocation required check is needed
-            then check if the symbol really requires
-            allocation only for local variables */
-         if (!IS_AGGREGATE (sym->type) &&
-             !(sym->_isparm && !IS_REGPARM (sym->etype))
-             && !sym->allocreq && sym->level)
-           continue;
-
-         /* if global variable & not static or extern
-            and addPublics allowed then add it to the public set */
-         if ((sym->_isparm && !IS_REGPARM (sym->etype))
-             && !IS_STATIC (sym->etype)) {
-//           fprintf(stderr, "%s:%d %s accessed\n", __FILE__, __LINE__, __FUNCTION__);
-             checkAddSym(&publics, sym);
-//         addSetHead (&publics, sym);
-         }
-
-         /* if extern then do nothing or is a function
-            then do nothing */
-         if (IS_FUNC (sym->type))
-           continue;
-
-
-         /* if is has an absolute address then generate
-            an equate for this no need to allocate space */
-         if (SPEC_ABSA (sym->etype))
-           {
-
-             if (options.debug || sym->level == 0)
-               fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
-
-             fprintf (afile, "%s\t=\t0x%04x\n",
-                      sym->rname,
-                      SPEC_ADDR (sym->etype));
-           }
-         else
-           {
-             if (options.debug || sym->level == 0)
-               fprintf (afile, "==.\n");
-
-             /* allocate space */
-             fprintf (afile, "%s:\n", sym->rname);
-             fprintf (afile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff);
-           }
-
-       }
+           sym = setNextItem (ovrset))
+        {
+
+          /* if extern then do nothing */
+          if (IS_EXTERN (sym->etype))
+            continue;
+
+          /* if allocation required check is needed
+             then check if the symbol really requires
+             allocation only for local variables */
+          if (!IS_AGGREGATE (sym->type) &&
+              !(sym->_isparm && !IS_REGPARM (sym->etype))
+              && !sym->allocreq && sym->level)
+            continue;
+
+          /* if global variable & not static or extern
+             and addPublics allowed then add it to the public set */
+          if ((sym->_isparm && !IS_REGPARM (sym->etype))
+              && !IS_STATIC (sym->etype)) {
+//            fprintf(stderr, "%s:%d %s accessed\n", __FILE__, __LINE__, __FUNCTION__);
+              checkAddSym(&publics, sym);
+//          addSetHead (&publics, sym);
+          }
+
+          /* if extern then do nothing or is a function
+             then do nothing */
+          if (IS_FUNC (sym->type))
+            continue;
+
+
+          /* if is has an absolute address then generate
+             an equate for this no need to allocate space */
+          if (SPEC_ABSA (sym->etype))
+            {
+
+              if (options.debug || sym->level == 0)
+                dbuf_printf (aBuf, " == 0x%04x\n", SPEC_ADDR (sym->etype));
+
+              dbuf_printf (aBuf, "%s\t=\t0x%04x\n",
+                       sym->rname,
+                       SPEC_ADDR (sym->etype));
+            }
+          else
+            {
+              if (options.debug || sym->level == 0)
+                dbuf_printf (aBuf, "==.\n");
+
+              /* allocate space */
+              dbuf_printf (aBuf, "%s:\n", sym->rname);
+              dbuf_printf (aBuf, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff);
+            }
+
+        }
     }
 }
 
-void emitStatistics(FILE *asmFile)
+static void
+emitStatistics(FILE *asmFile)
 {
   unsigned long isize, udsize, ramsize;
   statistics.isize = pic16_countInstructions();
@@ -1658,7 +1648,7 @@ void emitStatistics(FILE *asmFile)
   ramsize = pic16 ? pic16->RAMsize : 0x200;
   ramsize -= 256; /* ignore access bank and SFRs */
   if (ramsize == 0) ramsize = 64; /* prevent division by zero (below) */
-       
+
   fprintf (asmFile, "\n\n; Statistics:\n");
   fprintf (asmFile, "; code size:\t%5ld (0x%04lx) bytes (%5.2f%%)\n;           \t%5ld (0x%04lx) words\n",
     isize, isize, (isize*100.0)/(128UL << 10),
@@ -1679,16 +1669,18 @@ void emitStatistics(FILE *asmFile)
 void
 pic16glue ()
 {
-  FILE *vFile;
   FILE *asmFile;
-  FILE *ovrFile = tempfile();
+  struct dbuf_s ovrBuf;
+  struct dbuf_s vBuf;
+
+    dbuf_init(&ovrBuf, 4096);
+    dbuf_init(&vBuf, 4096);
 
     mainf = newSymbol ("main", 0);
     mainf->block = 0;
 
     mainf = findSymWithLevel(SymbolTab, mainf);
 
-    addSetHead(&tmpfileSet,ovrFile);
     pic16_pCodeInitRegisters();
 
     if(pic16_options.no_crt && mainf && IFFUNC_HASBODY(mainf->type)) {
@@ -1729,27 +1721,24 @@ pic16glue ()
 
     /* print the global struct definitions */
 
-    vFile = tempfile();
     /* PENDING: this isnt the best place but it will do */
     if (port->general.glue_up_main) {
       /* create the interrupt vector table */
-      pic16createInterruptVect (vFile);
+      pic16createInterruptVect (&vBuf);
     }
 
-    addSetHead(&tmpfileSet,vFile);
-    
     /* emit code for the all the variables declared */
     pic16emitMaps ();
 
     /* do the overlay segments */
-    pic16emitOverlay(ovrFile);
+    pic16emitOverlay(&ovrBuf);
     pic16_AnalyzepCode('*');
 
 #if 1
     if(pic16_options.dumpcalltree) {
       FILE *cFile;
-        
-        sprintf(buffer, dstFileName);
+
+        sprintf(buffer, "%s", dstFileName);
         strcat(buffer, ".calltree");
         cFile = fopen(buffer, "w");
         pic16_printCallTree( cFile );
@@ -1767,9 +1756,9 @@ pic16glue ()
     /* now put it all together into the assembler file */
     /* create the assembler file name */
     if((noAssemble || options.c1mode)  && fullDstFileName) {
-      sprintf (buffer, fullDstFileName);
+      sprintf (buffer, "%s", fullDstFileName);
     } else {
-      sprintf (buffer, dstFileName);
+      sprintf (buffer, "%s", dstFileName);
       strcat (buffer, ".asm");
     }
 
@@ -1777,7 +1766,7 @@ pic16glue ()
       werror (E_FILE_OPEN_ERR, buffer);
       exit (1);
     }
-    
+
     /* initial comments */
     pic16initialComments (asmFile);
 
@@ -1789,7 +1778,7 @@ pic16glue ()
     if(port->genAssemblerPreamble) {
       port->genAssemblerPreamble(asmFile);
     }
-       
+
     /* Put all variables into a cblock */
     pic16_AnalyzeBanking();
 
@@ -1803,18 +1792,18 @@ pic16glue ()
     if(pic16_options.opt_banksel > 1) {
       pic16_OptimizeBanksel();
     }
-           
+
     /* turn GOTOs into BRAs -- added by RN 2004-11-16 */
-    if(pic16_options.opt_flags & OF_OPTIMIZE_GOTO) {
+    if(!(pic16_options.opt_flags & OF_NO_OPTIMIZE_GOTO)) {
       pic16_OptimizeJumps();
     }
 
-    /* print the extern variables to this module */
-    pic16_printExterns(asmFile);
-       
     /* print the global variables in this module */
     pic16printPublics (asmFile);
 
+    /* print the extern variables to this module */
+    pic16_printExterns(asmFile);
+
     pic16_writeUsedRegs(asmFile);
 
 #if 0
@@ -1829,13 +1818,13 @@ pic16glue ()
     }
 #endif
 
-#if 0  
+#if 0
     /* no xdata in pic */
     /* copy xtern ram data */
     fprintf (asmFile, "%s", iComments2);
     fprintf (asmFile, "; external ram data\n");
     fprintf (asmFile, "%s", iComments2);
-    copyFile (asmFile, xdata->oFile);
+    dbuf_write_and_destroy (&xdata->oBuf, asmFile);
 #endif
 
 #if 0
@@ -1843,7 +1832,7 @@ pic16glue ()
     fprintf (asmFile, "%s", iComments2);
     fprintf (asmFile, "; bit data\n");
     fprintf (asmFile, "%s", iComments2);
-    copyFile (asmFile, bit->oFile);
+    dbuf_write_and_destroy (&bit->oBuf, asmFile);
 #endif
 
     /* copy the interrupt vector table */
@@ -1851,17 +1840,17 @@ pic16glue ()
       fprintf (asmFile, "\n%s", iComments2);
       fprintf (asmFile, "; interrupt vector \n");
       fprintf (asmFile, "%s", iComments2);
-      copyFile (asmFile, vFile);
+      dbuf_write_and_destroy (&vBuf, asmFile);
     }
-    
+
     /* copy global & static initialisations */
     fprintf (asmFile, "\n%s", iComments2);
     fprintf (asmFile, "; global & static initialisations\n");
     fprintf (asmFile, "%s", iComments2);
-    
+
     if(pic16_debug_verbose)
       fprintf(asmFile, "; A code from now on!\n");
-    
+
     pic16_copypCode(asmFile, 'A');
 
     if(pic16_options.no_crt) {
@@ -1871,14 +1860,14 @@ pic16glue ()
       }
     }
 
-//    copyFile (stderr, code->oFile);
+//    dbuf_write_and_destroy (&code->oBuf, stderr);
 
     fprintf(asmFile, "; I code from now on!\n");
     pic16_copypCode(asmFile, 'I');
 
     if(pic16_debug_verbose)
       fprintf(asmFile, "; dbName from now on!\n");
-    
+
     pic16_copypCode(asmFile, statsg->dbName);
 
     if(pic16_options.no_crt) {
@@ -1886,7 +1875,7 @@ pic16glue ()
         fprintf (asmFile,"\tgoto\t__sdcc_program_startup\n");
       }
     }
-       
+
     if(pic16_debug_verbose)
       fprintf(asmFile, "; X code from now on!\n");
 
@@ -1898,13 +1887,11 @@ pic16glue ()
     pic16_copypCode(asmFile, 'M');
 
     pic16_copypCode(asmFile, code->dbName);
-    
+
     pic16_copypCode(asmFile, 'P');
 
     emitStatistics(asmFile);
 
     fprintf (asmFile,"\tend\n");
     fclose (asmFile);
-    
-    rm_tmpfiles();
 }