* src/SDCCmem.c (allocGlobal): don't turn S_REGISTER storage class
[fw/sdcc] / src / pic16 / main.c
index 30153b2a5a838e4c74d0f27198ef2ee66d43cb70..727bee62c2eabb59b631b3c3c191c4b0d67a066f 100644 (file)
@@ -1,15 +1,36 @@
-/** @file main.c
-    pic16 specific general functions.
+/*-------------------------------------------------------------------------
+
+  main.c - pic16 specific general functions.
+
+   Written by - Scott Dattalo scott@dattalo.com
+   Ported to PIC16 by - Martin Dubuc m.debuc@rogers.com
+    
+   Note that mlh prepended _pic16_ on the static functions.  Makes
+   it easier to set a breakpoint using the debugger.
+
+
+   This program is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by the
+   Free Software Foundation; either version 2, or (at your option) any
+   later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+-------------------------------------------------------------------------*/
 
-    Note that mlh prepended _pic16_ on the static functions.  Makes
-    it easier to set a breakpoint using the debugger.
-*/
 #include "common.h"
 #include "main.h"
 #include "ralloc.h"
 #include "device.h"
 #include "SDCCutil.h"
 #include "glue.h"
+#include "pcode.h"
 //#include "gen.h"
 
 
@@ -25,6 +46,7 @@ static char *_pic16_keywords[] =
   "bit",
   "code",
   "critical",
+  "register",
   "data",
   "far",
   "idata",
@@ -43,120 +65,445 @@ static char *_pic16_keywords[] =
   "_xdata",
   "_pdata",
   "_idata",
+  "_naked",
   NULL
 };
 
+
+pic16_sectioninfo_t pic16_sectioninfo;
+
+
+extern char *pic16_processor_base_name(void);
+
 void  pic16_pCodeInitRegisters(void);
 
 void pic16_assignRegisters (eBBlock ** ebbs, int count);
 
 static int regParmFlg = 0;     /* determine if we can register a parameter */
 
+pic16_options_t pic16_options;
+
+extern set *includeDirsSet;
+extern set *dataDirsSet;
+extern set *libFilesSet;
+
+/* Also defined in gen.h, but the #include is commented out */
+/* for an unknowned reason. - EEP */
+void pic16_emitDebuggerSymbol (char *);
+extern regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop);
+extern void pic16_emitConfigRegs(FILE *of);
+extern void pic16_emitIDRegs(FILE *of);
+
+
+
 static void
 _pic16_init (void)
 {
-  asm_addTree (&asm_asxxxx_mapping);
-  pic16_pCodeInitRegisters();
-  maxInterrupts = 2;
+       asm_addTree (&asm_asxxxx_mapping);
+       pic16_pCodeInitRegisters();
+       maxInterrupts = 2;
+
+       /* set pic16 port options to defaults */
+       pic16_options.no_banksel = 0;
+       pic16_options.opt_banksel = 0;
+       pic16_options.omit_configw = 0;
+       pic16_options.omit_ivt = 0;
+       pic16_options.leave_reset = 0;
+       pic16_options.stack_model = 0;                  /* 0 for 'small', 1 for 'large' */
+       pic16_options.ivt_loc = 0x000000;               /* default location of interrupt vectors */
+       pic16_options.nodefaultlibs = 0;                /* link default libraries */
+       pic16_options.dumpcalltree = 0;
 }
 
 static void
 _pic16_reset_regparm ()
 {
-  regParmFlg = 0;
+       regParmFlg = 0;
 }
 
 static int
 _pic16_regparm (sym_link * l)
 {
-  /* for this processor it is simple
-     can pass only the first parameter in a register */
-  //if (regParmFlg)
-  //  return 0;
-
-  regParmFlg++;// = 1;
-  return 1;
+       /* for this processor it is simple
+        * can pass only the first parameter in a register */
+       if(pic16_fstack) {
+               if(regParmFlg)return 0;
+               regParmFlg = 1;
+         return 1;
+       } else {
+               regParmFlg++;// = 1;
+         return 1;
+       }
 }
 
+
+int initsfpnt=0;               /* set to 1 if source provides a pragma for stack
+                                * so glue() later emits code to initialize stack/frame pointers */
+set *absSymSet;
+
+set *sectNames=NULL;                   /* list of section listed in pragma directives */
+set *sectSyms=NULL;                    /* list of symbols set in a specific section */
+
+
 static int
 _process_pragma(const char *sz)
 {
-  static const char *WHITE = " \t";
+  static const char *WHITE = " \t\n";
+  
   char *ptr = strtok((char *)sz, WHITE);
 
-  if (startsWith (ptr, "memmap"))
-    {
-      char     *start;
-      char     *end;
-      char     *type;
-      char     *alias;
-
-      start = strtok((char *)NULL, WHITE);
-      end = strtok((char *)NULL, WHITE);
-      type = strtok((char *)NULL, WHITE);
-      alias = strtok((char *)NULL, WHITE);
-
-      if (start != (char *)NULL
-         && end != (char *)NULL
-         && type != (char *)NULL) {
-       value           *startVal = constVal(start);
-       value           *endVal = constVal(end);
-       value           *aliasVal;
-       memRange        r;
-
-       if (alias == (char *)NULL) {
-         aliasVal = constVal(0);
-       } else {
-         aliasVal = constVal(alias);
-       }
+       /* #pragma maxram [maxram] */
+       if (startsWith (ptr, "maxram")) {
+         char *maxRAM = strtok((char *)NULL, WHITE);
 
-       r.start_address = (int)floatFromVal(startVal);
-       r.end_address = (int)floatFromVal(endVal);
-       r.alias = (int)floatFromVal(aliasVal);
-       r.bank = (r.start_address >> 7) & 0xf;
+               if (maxRAM != (char *)NULL) {
+                 int maxRAMaddress;
+                 value *maxRAMVal;
 
-       if (strcmp(type, "RAM") == 0) {
-         pic16_addMemRange(&r, 0);
-       } else if (strcmp(type, "SFR") == 0) {
-         pic16_addMemRange(&r, 1);
-       } else {
-         return 1;
+                       maxRAMVal = constVal(maxRAM);
+                       maxRAMaddress = (int)floatFromVal(maxRAMVal);
+                       pic16_setMaxRAM(maxRAMaddress);
+               }
+       }
+       
+       /* #pragma stack [stack-position] [stack-len] */
+       if(startsWith(ptr, "stack")) {
+         char *stackPosS = strtok((char *)NULL, WHITE);
+         char *stackLenS = strtok((char *)NULL, WHITE);
+         value *stackPosVal;
+         value *stackLenVal;
+         regs *reg;
+         symbol *sym;
+
+               stackPosVal = constVal( stackPosS );
+               stackPos = (unsigned int)floatFromVal( stackPosVal );
+
+               
+               if(stackLenS) {
+                       stackLenVal = constVal( stackLenS );
+                       stackLen = (unsigned int)floatFromVal( stackLenVal );
+               }
+
+               if(stackLen < 1) {
+                       stackLen = 64;
+                       fprintf(stderr, "%s:%d setting stack to default size 0x%x\n", __FILE__, __LINE__, stackLen);
+               }
+
+//             fprintf(stderr, "Initializing stack pointer at 0x%x len 0x%x\n", stackPos, stackLen);
+                               
+               reg=newReg(REG_SFR, PO_SFR_REGISTER, stackPos, "_stack", stackLen-1, 0, NULL);
+               addSet(&pic16_fix_udata, reg);
+               
+               reg = newReg(REG_SFR, PO_SFR_REGISTER, stackPos + stackLen-1, "_stack_end", 1, 0, NULL);
+               addSet(&pic16_fix_udata, reg);
+
+               sym = newSymbol("stack", 0);
+               sprintf(sym->rname, "_%s", sym->name);
+               addSet(&publics, sym);
+
+               sym = newSymbol("stack_end", 0);
+               sprintf(sym->rname, "_%s", sym->name);
+               addSet(&publics, sym);
+               
+               initsfpnt = 1;          // force glue() to initialize stack/frame pointers */
+
+         return 0;
+       }
+       
+       /* #pragma code [symbol] [location] */
+       if(startsWith(ptr, "code")) {
+         char *symname = strtok((char *)NULL, WHITE);
+         char *location = strtok((char *)NULL, WHITE);
+         absSym *absS;
+         value *addr;
+
+               absS = Safe_calloc(1, sizeof(absSym));
+               absS->name = Safe_strdup( symname );
+               addr = constVal( location );
+               absS->address = (unsigned int)floatFromVal( addr );
+
+               addSet(&absSymSet, absS);
+               fprintf(stderr, "%s:%d symbol %s will be placed in location 0x%06x in code memory\n",
+                       __FILE__, __LINE__, symname, absS->address);
+
+         return 0;
        }
-      }
-
-      return 0;
-    } else if (startsWith (ptr, "maxram")) {
-      char *maxRAM = strtok((char *)NULL, WHITE);
-
-      if (maxRAM != (char *)NULL) {
-       int     maxRAMaddress;
-       value   *maxRAMVal;
 
-       maxRAMVal = constVal(maxRAM);
-       maxRAMaddress = (int)floatFromVal(maxRAMVal);
-       pic16_setMaxRAM(maxRAMaddress);
-      }
+       /* #pragma udata [section-name] [symbol] */
+       if(startsWith(ptr, "udata")) {
+         char *sectname = strtok((char *)NULL, WHITE);
+         char *symname = strtok((char *)NULL, WHITE);
+         sectSym *ssym;
+         sectName *snam;
+         int found=0;
+         
+               while(symname) {
+
+                       ssym = Safe_calloc(1, sizeof(sectSyms));
+                       ssym->name = Safe_calloc(1, strlen(symname)+2);
+                       sprintf(ssym->name, "_%s", symname);
+                       ssym->reg = NULL;
+
+                       addSet(&sectSyms, ssym);
+
+                       found = 0;
+                       for(snam=setFirstItem(sectNames);snam;snam=setNextItem(sectNames)) {
+                               if(!strcmp(sectname, snam->name)){ found=1; break; }
+                       }
+                       
+                       if(!found) {
+                               snam = Safe_calloc(1, sizeof(sectNames));
+                               snam->name = Safe_strdup( sectname );
+                               snam->regsSet = NULL;
+                               
+                               addSet(&sectNames, snam);
+                       }
+                       
+                       ssym->section = snam;
+                               
+//                     fprintf(stderr, "%s:%d placing symbol %s at section %s (%p)\n", __FILE__, __LINE__,
+//                             ssym->name, snam->name, snam);
+
+                       symname = strtok((char *)NULL, WHITE);
+               }
+
+         return 0;
+       }
        
-      return 0;
-    }
   return 1;
 }
 
+#define REP_UDATA      "--preplace-udata-with="
+
+#define STACK_MODEL    "--pstack-model="
+#define OPT_BANKSEL    "--obanksel="
+
+#define ALT_ASM                "--asm="
+#define ALT_LINK       "--link="
+
+#define IVT_LOC                "--ivt-loc"
+#define NO_DEFLIBS     "--nodefaultlibs"
+#define MPLAB_COMPAT   "--mplab-comp"
+
+char *alt_asm=NULL;
+char *alt_link=NULL;
+
+int pic16_mplab_comp=0;
+extern int pic16_debug_verbose;
+extern int pic16_ralloc_debug;
+extern int pic16_pcode_verbose;
+
+int pic16_fstack=0;
+int pic16_enable_peeps=0;
+
+OPTION pic16_optionsTable[]= {
+       { 0,    NO_DEFLIBS,             &pic16_options.nodefaultlibs,   "do not link default libraries when linking"},
+       { 0,    "--pno-banksel",        &pic16_options.no_banksel,      "do not generate BANKSEL assembler directives"},
+       { 0,    OPT_BANKSEL,            NULL,                           "set banksel optimization level (default=0 no)"},
+       { 0,    "--pomit-config-words", &pic16_options.omit_configw,    "omit the generation of configuration words"},
+       { 0,    "--pomit-ivt",          &pic16_options.omit_ivt,        "omit the generation of the Interrupt Vector Table"},
+       { 0,    "--pleave-reset-vector",&pic16_options.leave_reset,     "when omitting IVT leave RESET vector"},
+       { 0,    STACK_MODEL,    NULL,   "use stack model 'small' (default) or 'large'"},
+
+       { 0,    "--debug-xtra",         &pic16_debug_verbose,   "show more debug info in assembly output"},
+       { 0,    "--debug-ralloc",       &pic16_ralloc_debug,    "dump register allocator debug file *.d"},
+       { 0,    "--pcode-verbose",      &pic16_pcode_verbose,   "dump pcode related info"},
+               
+       { 0,    REP_UDATA,      NULL,   "Place udata variables at another section: udata_acs, udata_ovr, udata_shr"},
+
+       { 0,    ALT_ASM,        NULL,   "Use alternative assembler"},
+       { 0,    ALT_LINK,       NULL,   "Use alternative linker"},
+
+       { 0,    "--denable-peeps",      &pic16_enable_peeps,    "explicit enable of peepholes"},
+       { 0,    IVT_LOC,        NULL,   "<nnnn> interrupt vector table location"},
+       { 0,    "--calltree",           &pic16_options.dumpcalltree,    "dump call tree in .calltree file"},
+       { 0,    MPLAB_COMPAT,           &pic16_mplab_comp,      "enable compatibility mode for MPLAB utilities (MPASM/MPLINK)"},
+       { 0,    "--fstack",             &pic16_fstack,          "enable stack optimizations"},
+       { 0,    NULL,           NULL,   NULL}
+       };
+
+
+#define ISOPT(str)     !strncmp(argv[ *i ], str, strlen(str) )
+
+extern char *getStringArg(const char *,  char **, int *, int);
+extern int getIntArg(const char *, char **, int *, int);
+
 static bool
 _pic16_parseOptions (int *pargc, char **argv, int *i)
 {
+  int j=0;
+  char *stkmodel;
+  
   /* TODO: allow port-specific command line options to specify
    * segment names here.
    */
+       /* check for arguments that have associated an integer variable */
+       while(pic16_optionsTable[j].pparameter) {
+               if(ISOPT( pic16_optionsTable[j].longOpt )) {
+                       (*pic16_optionsTable[j].pparameter)++;
+                       return TRUE;
+               }
+               j++;
+       }
+
+
+       if(ISOPT(STACK_MODEL)) {
+               stkmodel = getStringArg(STACK_MODEL, argv, i, *pargc);
+               if(STRCASECMP(stkmodel, "small"))pic16_options.stack_model = 0;
+               else if(STRCASECMP(stkmodel, "large"))pic16_options.stack_model = 1;
+               else {
+                       fprintf(stderr, "Unknown stack model: %s", stkmodel);
+                       exit(-1);
+               }
+               return TRUE;
+       }
+
+       if(ISOPT(OPT_BANKSEL)) {
+               pic16_options.opt_banksel = getIntArg(OPT_BANKSEL, argv, i, *pargc);
+               return TRUE;
+       }
+
+       if(ISOPT(REP_UDATA)) {
+               pic16_sectioninfo.at_udata = Safe_strdup(getStringArg(REP_UDATA, argv, i, *pargc));
+               return TRUE;
+       }
+       
+       if(ISOPT(ALT_ASM)) {
+               alt_asm = Safe_strdup(getStringArg(ALT_ASM, argv, i, *pargc));
+               return TRUE;
+       }
+       
+       if(ISOPT(ALT_LINK)) {
+               alt_link = Safe_strdup(getStringArg(ALT_LINK, argv, i, *pargc));
+               return TRUE;
+       }
+
+       if(ISOPT(IVT_LOC)) {
+               pic16_options.ivt_loc = getIntArg(IVT_LOC, argv, i, *pargc);
+               return TRUE;
+       }
+
   return FALSE;
 }
 
+static void _pic16_initPaths(void)
+{
+  char pic16incDir[512];
+  char pic16libDir[512];
+  set *pic16incDirsSet;
+  set *pic16libDirsSet;
+  char devlib[512];
+
+       setMainValue("mcu", pic16->name[2] );
+       addSet(&preArgvSet, Safe_strdup("-D{mcu}"));
+
+       sprintf(pic16incDir, "%s%cpic16", INCLUDE_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
+       sprintf(pic16libDir, "%s%cpic16", LIB_DIR_SUFFIX, DIR_SEPARATOR_CHAR);
+
+       if(!options.nostdinc) {
+               /* setup pic16 include directory */
+               pic16incDirsSet = appendStrSet(dataDirsSet, NULL, pic16incDir);
+               mergeSets(&includeDirsSet, pic16incDirsSet);
+       }
+       
+       if(!options.nostdlib) {
+               /* setup pic16 library directory */
+               pic16libDirsSet = appendStrSet(dataDirsSet, NULL, pic16libDir);
+               mergeSets(&libDirsSet, pic16libDirsSet);
+       }
+       
+       if(!pic16_options.nodefaultlibs) {
+               /* now add the library for the device */
+               sprintf(devlib, "%s.lib", pic16->name[2]);
+               addSet(&libFilesSet, Safe_strdup(devlib));
+       }
+}
+
+extern set *linkOptionsSet;
+char *msprintf(hTab *pvals, const char *pformat, ...);
+int my_system(const char *cmd);
+
+/* custom function to link objects */
+static void _pic16_linkEdit(void)
+{
+  hTab *linkValues=NULL;
+  char lfrm[256];
+  char *lcmd;
+  char temp[128];
+  set *tSet=NULL;
+  int ret;
+  
+       /*
+        * link command format:
+        * {linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}
+        *
+        */
+        
+       sprintf(lfrm, "{linker} {incdirs} {lflags} -o {outfile} {spec_ofiles} {ofiles} {libs}");
+                
+       shash_add(&linkValues, "linker", "gplink");
+
+       mergeSets(&tSet, libDirsSet);
+       mergeSets(&tSet, libPathsSet);
+       
+       shash_add(&linkValues, "incdirs", joinStrSet( appendStrSet(tSet, "-I\"", "\"")));
+       shash_add(&linkValues, "lflags", joinStrSet(linkOptionsSet));
+  
+       shash_add(&linkValues, "outfile", dstFileName);
+
+       if(fullSrcFileName) {
+               sprintf(temp, "%s.o", dstFileName);
+//             shash_add(&linkValues, "srcofile", temp);
+               addSetHead(&relFilesSet, temp);
+       }
+
+//     shash_add(&linkValues, "spec_ofiles", "crt0i.o");
+       shash_add(&linkValues, "ofiles", joinStrSet(relFilesSet));
+       shash_add(&linkValues, "libs", joinStrSet(libFilesSet));
+       
+       lcmd = msprintf(linkValues, lfrm);
+        
+       ret = my_system( lcmd );
+        
+       Safe_free( lcmd );
+        
+       if(ret)
+               exit(1);
+}
+
+
+/* forward declarations */
+extern const char *pic16_linkCmd[];
+extern const char *pic16_asmCmd[];
+
 static void
 _pic16_finaliseOptions (void)
 {
+       port->mem.default_local_map = data;
+       port->mem.default_globl_map = data;
 
-      port->mem.default_local_map = data;
-      port->mem.default_globl_map = data;
+       /* peepholes are disabled for the time being */
+       options.nopeep = 1;
+
+       /* explicit enable peepholes for testing */
+       if(pic16_enable_peeps)
+               options.nopeep = 0;
+
+       options.all_callee_saves = 1;           // always callee saves
+//     options.float_rent = 1;
+//     options.intlong_rent = 1;
+       
+
+       if(alt_asm && strlen(alt_asm))
+               pic16_asmCmd[0] = alt_asm;
+       
+       if(alt_link && strlen(alt_link))
+               pic16_linkCmd[0] = alt_link;
+}
+
+
+/* all the rest is commented ifdef'd out */
 #if 0
   /* Hack-o-matic: if we are using the flat24 model,
    * adjust pointer sizes.
@@ -212,11 +559,24 @@ _pic16_finaliseOptions (void)
        }
     }
 #endif
-}
+
 
 static void
 _pic16_setDefaultOptions (void)
 {
+       /* initialize to defaults section locations, names and addresses */
+       pic16_sectioninfo.at_udata      = "udata";
+
+       /* set pic16 port options to defaults */
+       pic16_options.no_banksel = 0;
+       pic16_options.opt_banksel = 0;
+       pic16_options.omit_configw = 0;
+       pic16_options.omit_ivt = 0;
+       pic16_options.leave_reset = 0;
+       pic16_options.stack_model = 0;                  /* 0 for 'small', 1 for 'large' */
+       pic16_options.ivt_loc = 0x000000;
+       pic16_options.nodefaultlibs = 0;
+       pic16_options.dumpcalltree = 0;
 }
 
 static const char *
@@ -227,36 +587,35 @@ _pic16_getRegName (struct regs *reg)
   return "err";
 }
 
-extern char *pic16_processor_base_name(void);
 
-static void
-_pic16_genAssemblerPreamble (FILE * of)
+#if 1
+static  char *_pic16_mangleFunctionName(char *sz)
 {
-  char * name = pic16_processor_base_name();
+//     fprintf(stderr, "mangled function name: %s\n", sz);
 
-  if(!name) {
+  return sz;
+}
+#endif
 
-    name = "p18f452";
-    fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
-  }
 
-  fprintf (of, "\tlist\tp=%s\n",&name[1]);
-  fprintf (of, "\tinclude \"%s.inc\"\n",name);
+static void
+_pic16_genAssemblerPreamble (FILE * of)
+{
+  char *name = pic16_processor_base_name();
+
+       if(!name) {
+               name = "p18f452";
+               fprintf(stderr,"WARNING: No Pic has been selected, defaulting to %s\n",name);
+       }
 
-#if 0
-  fprintf (of, "\t__config _CONFIG1H,0x%x\n",pic16_getConfigWord(0x300001));
-  fprintf (of, "\t__config _CONFIG2L,0x%x\n",pic16_getConfigWord(0x300002));
-  fprintf (of, "\t__config _CONFIG2H,0x%x\n",pic16_getConfigWord(0x300003));
-  fprintf (of, "\t__config _CONFIG3H,0x%x\n",pic16_getConfigWord(0x300005));
-  fprintf (of, "\t__config _CONFIG4L,0x%x\n",pic16_getConfigWord(0x300006));
-  fprintf (of, "\t__config _CONFIG5L,0x%x\n",pic16_getConfigWord(0x300008));
-  fprintf (of, "\t__config _CONFIG5H,0x%x\n",pic16_getConfigWord(0x300009));
-  fprintf (of, "\t__config _CONFIG6L,0x%x\n",pic16_getConfigWord(0x30000a));
-  fprintf (of, "\t__config _CONFIG6H,0x%x\n",pic16_getConfigWord(0x30000b));
-  fprintf (of, "\t__config _CONFIG7L,0x%x\n",pic16_getConfigWord(0x30000c));
-  fprintf (of, "\t__config _CONFIG7H,0x%x\n",pic16_getConfigWord(0x30000d));
-#endif
+       fprintf (of, "\tlist\tp=%s\n",&name[1]);
 
+       if(!pic16_options.omit_configw) {
+               pic16_emitConfigRegs(of);
+               fprintf(of, "\n");
+               pic16_emitIDRegs(of);
+       }
+       
   fprintf (of, "\tradix dec\n");
 }
 
@@ -264,121 +623,149 @@ _pic16_genAssemblerPreamble (FILE * of)
 static int
 _pic16_genIVT (FILE * of, symbol ** interrupts, int maxInterrupts)
 {
-  int i;
+#if 1
+       /* PIC18F family has only two interrupts, the high and the low
+        * priority interrupts, which reside at 0x0008 and 0x0018 respectively - VR */
+
+       if((!pic16_options.omit_ivt) || (pic16_options.omit_ivt && pic16_options.leave_reset)) {
+               fprintf(of, "; RESET vector\n");
+               fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
+       }
+       
+       if(!pic16_options.omit_ivt) {
+               fprintf(of, "\tres 4\n");
+
+
+               fprintf(of, "; High priority interrupt vector 0x0008\n");
+               if(interrupts[1]) {
+                       fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
+                       fprintf(of, "\tres\t12\n"); 
+               } else {
+                       fprintf(of, "\tretfie\n");
+                       fprintf(of, "\tres\t14\n");
+               }
+
+               fprintf(of, "; Low priority interrupt vector 0x0018\n");
+               if(interrupts[2]) {
+                       fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
+               } else {
+                       fprintf(of, "\tretfie\n");
+               }
+       }
+#endif
+  return TRUE;
+}
+
+/* return True if the port can handle the type,
+ * False to convert it to function call */
+static bool _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
+{
+//     fprintf(stderr,"checking for native mult for %c (size: %d)\n", ic->op, getSize(OP_SYMBOL(IC_RESULT(ic))->type));
+
+#if 1
+       /* multiplication is fixed */
+       /* support mul for char/int/long */
+       if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) <= 4)
+               && (ic->op == '*'))return TRUE;
+#endif
 
 #if 0
-  if (options.model != MODEL_FLAT24)
-    {
-      /* Let the default code handle it. */
-      return FALSE;
-    }
+       /* support div for char/int/long */
+       if((getSize(OP_SYMBOL(IC_LEFT(ic))->type ) <= 0)
+               && (ic->op == '/'))return TRUE;
 #endif
+       
+  return FALSE;
+}
 
-       /* PIC18F family has only two interrupts, the high and the low
-        * priority interrupts, which reside in 0x0008 and 0x0018 respectively */
 
-       fprintf(of, "; RESET vector\n");
-       fprintf(of, "\tgoto\t__sdcc_gsinit_startup\n");
-       fprintf(of, "\tres 2\n");
+#if 0
+/* Do CSE estimation */
+static bool cseCostEstimation (iCode *ic, iCode *pdic)
+{
+//    operand *result = IC_RESULT(ic);
+//    sym_link *result_type = operandType(result);
 
 
-       fprintf(of, "; High priority interrupt vector 0x0008\n");
-       
-       if(interrupts[1]) {
-               fprintf(of, "\tgoto\t%s\n", interrupts[1]->rname);
-               fprintf(of, "\tres\t6\n");
-       } else {
-               fprintf(of, "\tretfie\n");
-               fprintf(of, "\tres\t14\n");
-       }
+       /* VR -- this is an adhoc. Put here after conversation
+        * with Erik Epetrich */
 
-       fprintf(of, "; Low priority interrupt vector 0x0018\n");
-       if(interrupts[2]) {
-               fprintf(of, "\tgoto\t%s\n", interrupts[2]->rname);
-       } else {
-               fprintf(of, "\tretfie\n");
+       if(ic->op == '<'
+               || ic->op == '>'
+               || ic->op == EQ_OP) {
+
+               fprintf(stderr, "%d %s\n", __LINE__, __FUNCTION__);
+         return 0;
        }
+
 #if 0
-  /* now for the other interrupts */
-  for (i = 0; i < maxInterrupts; i++)
-    {
-       fprintf(of, "; %s priority interrupt vector 0x%s\n", (i==0)?"high":"low", (i==0)?"0008":"0018");
-      if (interrupts[i])
-       {
-         fprintf (of, "\tgoto\t%s\n\tres\t4\n", interrupts[i]->rname);
-       }
-      else
-       {
-         fprintf (of, "\tretfie\n\tres\t7\n");
-       }
+    /* if it is a pointer then return ok for now */
+    if (IC_RESULT(ic) && IS_PTR(result_type)) return 1;
+
+    /* if bitwise | add & subtract then no since mcs51 is pretty good at it
+       so we will cse only if they are local (i.e. both ic & pdic belong to
+       the same basic block */
+    if (IS_BITWISE_OP(ic) || ic->op == '+' || ic->op == '-') {
+        /* then if they are the same Basic block then ok */
+        if (ic->eBBlockNum == pdic->eBBlockNum) return 1;
+        else return 0;
     }
 #endif
 
-  return TRUE;
+    /* for others it is cheaper to do the cse */
+    return 1;
 }
+#endif
+
 
+/* Indicate which extended bit operations this port supports */
 static bool
-_hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
+hasExtBitOp (int op, int size)
 {
-  //  sym_link *test = NULL;
-  //  value *val;
-
-  fprintf(stderr,"checking for native mult\n");
-
-  if ( ic->op != '*')
-    {
-      return FALSE;
-    }
-
-  return TRUE;
-/*
-  if ( IS_LITERAL (left))
-    {
-      fprintf(stderr,"left is lit\n");
-      test = left;
-      val = OP_VALUE (IC_LEFT (ic));
-    }
-  else if ( IS_LITERAL (right))
-    {
-      fprintf(stderr,"right is lit\n");
-      test = left;
-      val = OP_VALUE (IC_RIGHT (ic));
-    }
+  if (op == RRC
+      || op == RLC
+      /* || op == GETHBIT */ /* GETHBIT doesn't look complete for PIC */
+     )
+    return TRUE;
   else
-    {
-      fprintf(stderr,"oops, neither is lit so no\n");
-      return FALSE;
-    }
-
-  if ( getSize (test) <= 2)
-    {
-      fprintf(stderr,"yep\n");
-      return TRUE;
-    }
-  fprintf(stderr,"nope\n");
+    return FALSE;
+}
 
-  return FALSE;
-*/
+/* Indicate the expense of an access to an output storage class */
+static int
+oclsExpense (struct memmap *oclass)
+{
+  /* The IN_FARSPACE test is compatible with historical behaviour, */
+  /* but I don't think it is applicable to PIC. If so, please feel */
+  /* free to remove this test -- EEP */
+  if (IN_FARSPACE(oclass))
+    return 1;
+    
+  return 0;
 }
 
-/** $1 is always the basename.
+/** $1 is the input object file (PIC16 specific)       // >>always the basename<<.
     $2 is always the output file.
-    $3 varies
+    $3 -L path and -l libraries
     $l is the list of extra options that should be there somewhere...
     MUST be terminated with a NULL.
 */
-static const char *_linkCmd[] =
+const char *pic16_linkCmd[] =
 {
-  "gplink", "\"$1.o\"", "-o $1", "$l", NULL
+  "gplink", "$l", "-o \"$2\"", "\"$1\"","$3", NULL
 };
 
-/* Sigh. This really is not good. For now, I recommend:
- * sdcc -S -mpic16 file.c
- * the -S option does not compile or link
- */
-static const char *_asmCmd[] =
+
+
+/** $1 is always the basename.
+    $2 is always the output file.
+    $3 varies (nothing currently)
+    $l is the list of extra options that should be there somewhere...
+    MUST be terminated with a NULL.
+*/
+const char *pic16_asmCmd[] =
 {
-  "gpasm", "-c  -I/usr/local/share/gputils/header", "\"$1.asm\"", NULL
+  "gpasm", "$l", "$3", "-c", "\"$1.asm\"", "-o \"$2\"", NULL
 
 };
 
@@ -388,7 +775,7 @@ PORT pic16_port =
   TARGET_ID_PIC16,
   "pic16",
   "MCU PIC16",                 /* Target name */
-  "p18f442",                    /* Processor */
+  "p18f452",                    /* Processor */
   {
     pic16glue,
     TRUE,                      /* Emit glue around main */
@@ -396,32 +783,36 @@ PORT pic16_port =
     MODEL_SMALL
   },
   {
-    _asmCmd,
-    NULL,
-    NULL,
-    NULL,
-       //"-plosgffc",          /* Options with debug */
-       //"-plosgff",           /* Options without debug */
-    0,
-    ".asm",
+    pic16_asmCmd,              /* assembler command and arguments */
+    NULL,                      /* alternate macro based form */
+    "-g",                      /* arguments for debug mode */
+    NULL,                      /* arguments for normal mode */
+    0,                         /* print externs as global */
+    ".asm",                    /* assembler file extension */
     NULL                       /* no do_assemble function */
   },
   {
-    _linkCmd,
-    NULL,
-    NULL,
-    ".rel"
+    NULL,                      //    pic16_linkCmd,            /* linker command and arguments */
+    NULL,                      /* alternate macro based form */
+    _pic16_linkEdit,           //NULL,                 /* no do_link function */
+    ".o",                      /* extension for object files */
+    0                          /* no need for linker file */
   },
   {
     _defaultRules
   },
   {
-       /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
-    1, 2, 2, 4, 2, 2, 2, 1, 4, 4
-       /* TSD - I changed the size of gptr from 3 to 1. However, it should be
-          2 so that we can accomodate the PIC's with 4 register banks (like the
-          16f877)
-        */
+       /* Sizes */
+    1,         /* char */
+    2,         /* short */
+    2,         /* int */
+    4,         /* long */
+    2,         /* ptr */
+    3,         /* fptr, far pointers (see Microchip) */
+    2,         /* gptr */
+    1,         /* bit */
+    4,         /* float */
+    4          /* max */
   },
   {
     "XSEG    (XDATA)",         // xstack
@@ -440,20 +831,33 @@ PORT pic16_port =
     NULL,                      // xinit
     NULL,                      // default location for auto vars
     NULL,                      // default location for global vars
-    1                          // code is read only
+    1                          // code is read only 1=yes
+  },
+  {
+    NULL,              /* genExtraAreaDeclaration */
+    NULL               /* genExatrAreaLinkOptions */
   },
-  { NULL, NULL },
   {
-    +1, 1, 4, 1, 1, 0
+       /* stack related information */
+    -1,                        /* -1 stack grows downwards, +1 upwards */
+    1,                 /* extra overhead when calling between banks */
+    4,                 /* extra overhead when the function is an ISR */
+    1,                 /* extra overhead for a function call */
+    1,                 /* re-entrant space */
+    0                  /* 'banked' call overhead, mild overlap with bank_overhead */
   },
     /* pic16 has an 8 bit mul */
   {
-    1, -1
+     0, -1
+  },
+  {
+    pic16_emitDebuggerSymbol
   },
   "_",
   _pic16_init,
   _pic16_parseOptions,
-  NULL,
+  pic16_optionsTable,
+  _pic16_initPaths,
   _pic16_finaliseOptions,
   _pic16_setDefaultOptions,
   pic16_assignRegisters,
@@ -463,12 +867,16 @@ PORT pic16_port =
   NULL,                                /* no genAssemblerEnd */
   _pic16_genIVT,
   NULL, // _pic16_genXINIT
+  NULL,                        /* genInitStartup */
   _pic16_reset_regparm,
   _pic16_regparm,
   _process_pragma,                             /* process a pragma */
-  NULL,
+  _pic16_mangleFunctionName,                           /* mangles function name */
   _hasNativeMulFor,
-  FALSE,
+  hasExtBitOp,                 /* hasExtBitOp */
+  oclsExpense,                 /* oclsExpense */
+  FALSE,                       
+  TRUE,                                /* little endian */
   0,                           /* leave lt */
   0,                           /* leave gt */
   1,                           /* transform <= to ! > */
@@ -476,7 +884,7 @@ PORT pic16_port =
   1,                           /* transform != to !(a == b) */
   0,                           /* leave == */
   FALSE,                        /* No array initializer support. */
-  0,                            /* no CSE cost estimation yet */
+  0,   //cseCostEstimation,            /* !!!no CSE cost estimation yet */
   NULL,                        /* no builtin functions */
   GPOINTER,                    /* treat unqualified pointers as "generic" pointers */
   1,                           /* reset labelKey to 1 */