#include <string.h>
-#ifdef WORDS_BIGENDIAN
- #define _ENDIAN(x) (3-x)
-#else
- #define _ENDIAN(x) (x)
-#endif
-
-#define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
extern symbol *interrupts[256];
void pic16_printIval (symbol * sym, sym_link * type, initList * ilist, char ptype, void *p);
extern set *tmpfileNameSet;
extern char *iComments1;
extern char *iComments2;
-//extern void emitStaticSeg (memmap * map);
extern int initsfpnt;
+extern unsigned long pFile_isize;
+extern unsigned long pic16_countInstructions();
+set *pic16_localFunctions = NULL;
set *rel_idataSymSet=NULL;
set *fix_idataSymSet=NULL;
extern DEFSETFUNC (rmTmpFiles);
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 printPublics (FILE * afile);
void pic16_pCodeInitRegisters(void);
-pCodeOp *pic16_popGetLit(unsigned int lit);
-pCodeOp *pic16_popGetLit2(unsigned int lit, pCodeOp *arg2);
pCodeOp *pic16_popCopyReg(pCodeOpReg *pc);
extern void pic16_pCodeConstString(char *name, char *value);
+
/*-----------------------------------------------------------------*/
/* aopLiteral - string from a literal value */
/*-----------------------------------------------------------------*/
-int pic16aopLiteral (value *val, int offset)
+unsigned int pic16aopLiteral (value *val, int offset)
{
union {
float f;
/* if it is a float then it gets tricky */
/* otherwise it is fairly simple */
- if (!IS_FLOAT(val->type)) {
+ if (!(IS_FLOAT(val->type) || IS_FIXED(val->type))) {
unsigned long v = (unsigned long) floatFromVal(val);
return ( (v >> (offset * 8)) & 0xff);
}
+ if(IS_FIXED16X16(val->type)) {
+ unsigned long v = (unsigned long)fixed16x16FromDouble( floatFromVal( val ) );
+
+ return ( (v >> (offset * 8)) & 0xff);
+ }
+
/* it is type float */
fl.f = (float) floatFromVal(val);
#ifdef WORDS_BIGENDIAN
for (sym = setFirstItem (map->syms); sym; sym = setNextItem (map->syms)) {
#if 0
- fprintf(stderr, "\t%s: sym: %s\tused: %d\textern: %d\tstatic: %d\taggregate: %d\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));
+ IS_AGGREGATE(sym->type), (SPEC_SCLS(sym->etype) == S_REGISTER), IS_FUNC(sym->type));
printTypeChain( sym->type, stderr );
fprintf(stderr, "\n");
#endif
-// if(PIC16_IS_CONFIG_ADDRESS(SPEC_ADDR(sym->etype)))
-// continue;
-
-
/* if extern then add to externs */
if (IS_EXTERN (sym->etype)) {
- checkAddSym(&externs, sym);
+ /* 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
+ * then check if the symbol really requires
* allocation only for local variables */
if (arFlag && !IS_AGGREGATE (sym->type) &&
!(sym->_isparm && !IS_REGPARM (sym->etype)) &&
checkAddSym(&publics, sym);
} else
+ /* new version */
if(IS_STATIC(sym->etype)
- && !(sym->ival && !sym->level)
- ) {
+ && !sym->ival) /* && !sym->level*/ {
regs *reg;
- /* add it to udata list */
+ sectSym *ssym;
+ int found=0;
-// fprintf(stderr, "%s:%d adding %s (%s) remat=%d\n", __FILE__, __LINE__,
-// sym->name, sym->rname, sym->remat);
-
- //, OP_SYMBOL(operandFromSymbol(sym))->name);
+// debugf("adding symbol %s\n", sym->name);
#define SET_IMPLICIT 1
#if SET_IMPLICIT
#endif
reg = pic16_allocDirReg( operandFromSymbol( sym ));
- checkAddReg(&pic16_rel_udata, reg);
+
+ if(reg) {
+ for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
+ if(!strcmp(ssym->name, reg->name))found=1;
+ }
+
+ if(!found)
+ checkAddReg(&pic16_rel_udata, reg);
+#if 0
+ else
+ debugf("Did find %s in pic16_rel_udata already. Check!\n", reg->name);
+// checkAddSym(&publics, sym);
+#endif
+
+ }
}
/* if extern then do nothing or is a function
continue;
}
-#if 0
- /* print extra debug info if required */
- if (options.debug || sym->level == 0) {
- cdbWriteSymbol (sym); //, cdbFile, FALSE, FALSE);
-
- if (!sym->level) /* global */
- if (IS_STATIC (sym->etype))
- fprintf (map->oFile, "F%s_", moduleName); /* scope is file */
- else
- fprintf (map->oFile, "G_"); /* scope is global */
- else
- /* symbol is local */
- fprintf (map->oFile, "L%s_", (sym->localof ? sym->localof->name : "-null-"));
- fprintf (map->oFile, "%s_%d_%d", sym->name, sym->level, sym->block);
- }
-#endif
-
-
/* 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 (stderr,"; %s == 0x%04x\t\treqv= %p nRegs= %d\n",
// sym->name, SPEC_ADDR (sym->etype), sym->reqv, sym->regType);
} else {
reg = pic16_allocDirReg( operandFromSymbol( sym ) );
}
- if(checkAddReg(&pic16_rel_udata, reg)) {
- addSetHead(&publics, 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>>"));
+#endif
+
+ if(reg) {
+ for(ssym=setFirstItem(sectSyms); ssym; ssym=setNextItem(sectSyms)) {
+ if(!strcmp(ssym->name, reg->name))found=1;
+ }
+
+ if(!found)
+ if(checkAddReg(&pic16_rel_udata, reg)) {
+ addSetHead(&publics, sym);
+ }
+ }
}
+
+
+
} else
+
addSetHead(&publics, sym);
}
/* if it has an initial value then do it only if
it is a global variable */
- if (sym->ival && sym->level == 0) {
+ if (sym->ival
+ && ((sym->level == 0)
+ || IS_STATIC(sym->etype)) ) {
ast *ival = NULL;
#if 0
addSet(&rel_idataSymSet, copySymbol(sym));
// ival = newNode ('=', newAst_VALUE(symbolVal (sym)),
-// decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_CHECK));
+// decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
}
if(ival) {
return valCastLiteral(toType, 0.0);
}
- expr = list2expr (ilist);
+ expr = decorateType(resolveSymbols( list2expr (ilist) ), FALSE);
+// expr = list2expr( ilist );
if (!expr)
goto wrong;
if (IS_AST_SYM_VALUE (expr->left)) {
val = copyValue (AST_VALUE (expr->left));
val->type = newLink (DECLARATOR);
- if (SPEC_SCLS (expr->left->etype) == S_CODE) {
+ if(SPEC_SCLS (expr->left->etype) == S_CODE) {
DCL_TYPE (val->type) = CPOINTER;
DCL_PTR_CONST (val->type) = port->mem.code_ro;
}
DCL_TYPE (val->type) = EEPPOINTER;
else
DCL_TYPE (val->type) = POINTER;
+
val->type->next = expr->left->ftype;
val->etype = getSpec (val->type);
return val;
val->etype = getSpec (val->type);
return val;
}
+
wrong:
if (expr)
werrorfl (expr->filename, expr->lineno, E_INCOMPAT_PTYPES);
}
-/*-----------------------------------------------------------------*/
-/* return the generic pointer high byte for a given pointer type. */
-/*-----------------------------------------------------------------*/
-int pic16_pointerTypeToGPByte (const int p_type, const char *iname, const char *oname)
-{
- switch (p_type)
- {
- case IPOINTER:
- case POINTER:
- fprintf(stderr, "%s:%d pointer is IPOINTER/POINTER\n", __FILE__, __LINE__);
- return GPTYPE_NEAR;
- case GPOINTER:
- fprintf(stderr, "%s:%d pointer is GPOINTER\n", __FILE__, __LINE__);
- werror (E_CANNOT_USE_GENERIC_POINTER,
- iname ? iname : "<null>",
- oname ? oname : "<null>");
- exit (1);
- case FPOINTER:
- fprintf(stderr, "%s:%d pointer is FPOINTER\n", __FILE__, __LINE__);
- return GPTYPE_FAR;
- case CPOINTER:
- fprintf(stderr, "%s:%d pointer is CPOINTER\n", __FILE__, __LINE__);
- return GPTYPE_CODE;
- case PPOINTER:
- fprintf(stderr, "%s:%d pointer is PPOINTER\n", __FILE__, __LINE__);
- return GPTYPE_XSTACK;
- default:
- fprintf (stderr, "*** internal error: unknown pointer type %d in GPByte.\n",
- p_type);
- break;
- }
- return -1;
-}
-
-
/*-----------------------------------------------------------------*/
/* printPointerType - generates ival for pointer type */
/*-----------------------------------------------------------------*/
void pic16_printPointerType (const char *name, char ptype, void *p)
{
_pic16_printPointerType (name, ptype, p);
- pic16_flushDB(ptype, p);
+ //pic16_flushDB(ptype, p); /* breaks char* const arr[] = {&c, &c, &c}; */
}
/*-----------------------------------------------------------------*/
/* printGPointerType - generates ival for generic pointer type */
/*-----------------------------------------------------------------*/
-void pic16_printGPointerType (const char *iname, const char *oname,
- const unsigned int type, char ptype, void *p)
+void pic16_printGPointerType (const char *iname, const unsigned int itype,
+ char ptype, void *p)
{
- _pic16_printPointerType (iname, ptype, p);
- pic16_emitDB(pic16_pointerTypeToGPByte(type, iname, oname), ptype, p);
- pic16_flushDB(ptype, 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_flushDB(ptype, p); /* might break char* const arr[] = {...}; */
}
+/* set to 0 to disable debug messages */
+#define DEBUG_PRINTIVAL 0
/*-----------------------------------------------------------------*/
/* pic16_printIvalType - generates ival for int/char */
pic16_printIvalType (symbol *sym, sym_link * type, initList * ilist, char ptype, void *p)
{
value *val;
- unsigned long ulval;
- //fprintf(stderr, "%s\n",__FUNCTION__);
+// fprintf(stderr, "%s for symbol %s\n",__FUNCTION__, sym->rname);
+
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
/* if initList is deep */
if (ilist && ilist->type == INIT_DEEP)
val = valCastLiteral(type, floatFromVal(val));
}
- if(val)
- ulval = (unsigned long) floatFromVal (val);
- else
- ulval =0;
-
switch (getSize (type)) {
case 1:
- pic16_emitDB(BYTE_IN_LONG(ulval,0), ptype, p);
+ pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
break;
case 2:
- pic16_emitDB(BYTE_IN_LONG(ulval,0), ptype, p);
- pic16_emitDB(BYTE_IN_LONG(ulval,1), ptype, p);
+ 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(BYTE_IN_LONG(ulval,0), ptype, p);
- pic16_emitDB(BYTE_IN_LONG(ulval,1), ptype, p);
- pic16_emitDB(BYTE_IN_LONG(ulval,2), ptype, p);
- pic16_emitDB(BYTE_IN_LONG(ulval,3), ptype, p);
+ 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;
}
}
/* pic16_printIvalChar - generates initital value for character array */
/*--------------------------------------------------------------------*/
static int
-pic16_printIvalChar (sym_link * type, initList * ilist, char *s, char ptype, void *p)
+pic16_printIvalChar (symbol *sym, sym_link * type, initList * ilist, char *s, char ptype, void *p)
{
value *val;
- int remain;
+ int remain, len, ilen;
if(!p)
return 0;
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
- // fprintf(stderr, "%s\n",__FUNCTION__);
- if (!s)
- {
-
- val = list2val (ilist);
- /* if the value is a character string */
- if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
- {
- if (!DCL_ELEM (type))
- DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
+ if(!s) {
+ val = list2val (ilist);
- for(remain=0; remain<DCL_ELEM(type); remain++)
- pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
-
- if ((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0) {
- while (remain--) {
- pic16_emitDB(0x00, ptype, p);
- }
+ /* if the value is a character string */
+ if(IS_ARRAY (val->type) && IS_CHAR (val->etype)) {
+ /* length of initializer string (might contain \0, so do not use strlen) */
+ ilen = DCL_ELEM(val->type);
+
+ if(!DCL_ELEM (type))
+ DCL_ELEM (type) = ilen;
+
+ /* len is 0 if declartion equals initializer,
+ * >0 if declaration greater than initializer
+ * <0 if declaration less than initializer
+ * Strategy: if >0 emit 0x00 for the rest of the length,
+ * if <0 then emit only the length of declaration elements
+ * and warn user
+ */
+ len = DCL_ELEM (type) - ilen;
+
+// fprintf(stderr, "%s:%d ilen = %i len = %i DCL_ELEM(type) = %i SPEC_CVAL-len = %i\n", __FILE__, __LINE__,
+// ilen, len, DCL_ELEM(type), strlen(SPEC_CVAL(val->etype).v_char));
+
+ if(len >= 0) {
+ /* emit initializer */
+ 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);
}
- return 1;
- }
- else
- return 0;
- }
- else {
+ } else {
+ werror (W_EXCESS_INITIALIZERS, "array of chars", sym->name, sym->lineDef);
+
+ for(remain=0; remain<DCL_ELEM (type); remain++)
+ pic16_emitDB(SPEC_CVAL(val->etype).v_char[ remain ], ptype, p);
+ }
+
+
+// if((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0) {
+// }
+ return 1;
+ } else return 0;
+ } else {
for(remain=0; remain<strlen(s); remain++) {
pic16_emitDB(s[remain], ptype, p);
}
return;
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
/* take care of the special case */
/* array of characters can be init */
/* by a string */
return;
}
- if(pic16_printIvalChar (type,
+ if(pic16_printIvalChar (sym, type,
(ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
SPEC_CVAL (sym->etype).v_char, ptype, p))
return;
int size =0;
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
+
do {
unsigned long i;
val = list2val(lilist);
i <<= SPEC_BSTR (lsym->etype);
ival |= i;
if (! ( lsym->next &&
+ (lilist && lilist->next) &&
(IS_BITFIELD(lsym->next->type)) &&
(SPEC_BSTR(lsym->next->etype)))) break;
lsym = lsym->next;
switch (size) {
case 1:
pic16_emitDB(BYTE_IN_LONG(ival, 0), ptype, p);
- break;
+ break;
case 2:
pic16_emitDB(BYTE_IN_LONG(ival, 0), ptype, p);
pic16_emitDB(BYTE_IN_LONG(ival, 1), ptype, p);
- break;
+ 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;
+ 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);
}
*sym = lsym;
*ilist = lilist;
symbol *sflds;
initList *iloop = NULL;
+
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
sflds = SPEC_STRUCT (type)->fields;
if (ilist) {
iloop = ilist->init.deep;
}
- for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
+ for (; (sflds && iloop); 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);
return;
}
+/*-----------------------------------------------------------------*/
+/* printIvalUnion - generates initial value for unions */
+/*-----------------------------------------------------------------*/
+void pic16_printIvalUnion (symbol * sym, sym_link * type,
+ initList * ilist, char ptype, void *p)
+{
+ //symbol *sflds;
+ initList *iloop = NULL;
+ int i, size;
+
+
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
+ iloop = ilist;
+ i = 0;
+ while (iloop)
+ {
+ i++;
+ iloop = iloop->next;
+ } // while
+
+ size = -1;
+ if (type) size = SPEC_STRUCT(type)->size;
+
+ 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
+
+ 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 );
+}
+
+static int
+pic16_isUnion( symbol *sym, sym_link *type )
+{
+ if (type && SPEC_STRUCT(type)->type == UNION) return 1;
+ return 0;
+}
+
/*--------------------------------------------------------------------------*/
/* pic16_printIvalCharPtr - generates initial values for character pointers */
/*--------------------------------------------------------------------------*/
VR - Attempting to port this function to pic16 port - 8-Jun-2004
*/
- fprintf(stderr, "%s\n",__FUNCTION__);
+
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
size = getSize (type);
{
pic16_emitDS(val->name, ptype, p);
}
- else if (size == FPTRSIZE)
+ else if (size == 2)
{
pic16_printPointerType (val->name, ptype, p);
}
- else if (size == GPTRSIZE)
+ else if (size == 3)
{
int type;
if (IS_PTR (val->type)) {
// this is a literal string
type=CPOINTER;
}
- pic16_printGPointerType(val->name, sym->name, type, ptype, p);
+ pic16_printGPointerType(val->name, type, ptype, p);
}
else
{
fprintf (stderr, "*** internal error: unknown size in "
"printIvalCharPtr.\n");
+ assert(0);
}
}
else
switch (size)
{
case 1:
- pic16_emitDS(aopLiteral(val, 0), ptype, p);
-// tfprintf (oFile, "\t!dbs\n", aopLiteral (val, 0));
+ pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
break;
case 2:
- pic16_emitDS(aopLiteral(val, 0), ptype, p);
- pic16_emitDS(aopLiteral(val, 1), ptype, p);
-// tfprintf (oFile, "\t.byte %s,%s\n",
-// aopLiteral (val, 0), aopLiteral (val, 1));
+ pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
+ pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
break;
case 3:
- if (IS_GENPTR(type) && floatFromVal(val)!=0) {
- // non-zero mcs51 generic pointer
- werrorfl (sym->fileDef, sym->lineDef, E_LITERAL_GENERIC);
- }
-
- pic16_emitDS(aopLiteral(val, 0), ptype, p);
- pic16_emitDS(aopLiteral(val, 1), ptype, p);
- pic16_emitDS(aopLiteral(val, 2), ptype, p);
-
-// fprintf (oFile, "\t.byte %s,%s,%s\n",
-// aopLiteral (val, 0),
-// aopLiteral (val, 1),
-// aopLiteral (val, 2));
+ pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
+ pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
+ pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
break;
-
-/* no 4 bytes size long for pic16 port */
- case 4:
- if (IS_GENPTR(type) && floatFromVal(val)!=0) {
- // non-zero ds390 generic pointer
- werrorfl (sym->fileDef, sym->lineDef, E_LITERAL_GENERIC);
- }
-
-// fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
-// aopLiteral (val, 0),
-// aopLiteral (val, 1),
-// aopLiteral (val, 2),
-// aopLiteral (val, 3));
- break;
default:
assert (0);
}
}
- if (val->sym && val->sym->isstrlit && !isinSet(statsg->syms, val->sym)) {
- addSet (&statsg->syms, 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;
value *val;
int dLvl = 0;
+
+#if DEBUG_PRINTIVAL
+ fprintf(stderr, "%s\n",__FUNCTION__);
+#endif
+
if (ilist)
val = list2val (ilist);
else
}
/* now generate the name */
- if (!val->sym)
- {
- pic16_printPointerType (val->name, ptype, p);
- }
- else
- {
- pic16_printPointerType (val->sym->rname, ptype, p);
- }
+ if (!val->sym) {
+ pic16_printGPointerType (val->name, DCL_TYPE(val->type), ptype, p);
+ } else {
+ pic16_printGPointerType (val->sym->rname, 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");
+ }
+ }
+ }
return;
}
value *val;
int size;
+#if 0
+ fprintf(stderr, "%s:%d initialising pointer: %s size: %d\n", __FILE__, __LINE__,
+ sym->rname, getSize(sym->type));
+#endif
+
/* if deep then */
if (ilist && (ilist->type == INIT_DEEP))
ilist = ilist->init.deep;
{
case 1:
pic16_emitDB((unsigned int)floatFromVal(val) & 0xff, ptype, p);
-// tfprintf (oFile, "\t!db !constbyte\n", (unsigned int) floatFromVal (val) & 0xff);
break;
case 2:
pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-// tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
break;
case 3:
pic16_emitDB(pic16aopLiteral(val, 0), ptype, p);
pic16_emitDB(pic16aopLiteral(val, 1), ptype, p);
-
- if (IS_GENPTR (val->type))
- pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
- else if (IS_PTR (val->type))
- pic16_emitDB(pic16_pointerTypeToGPByte(DCL_TYPE(val->type), NULL, NULL), ptype, p);
- else
pic16_emitDB(pic16aopLiteral(val, 2), ptype, p);
+ break;
+ default:
+ fprintf(stderr, "%s:%d size = %d\n", __FILE__, __LINE__, getSize(type));
+ assert(0);
}
return;
}
{
pic16_emitDS(val->name, ptype, p);
}
- else if (size == FPTRSIZE)
+ else if (size == 2)
{
pic16_printPointerType (val->name, ptype, p);
}
- else if (size == GPTRSIZE)
+ else if (size == 3)
{
- pic16_printGPointerType (val->name, sym->name,
- (IS_PTR (val->type) ? DCL_TYPE (val->type) :
- PTR_TYPE (SPEC_OCLS (val->etype))), ptype, p);
- }
+ pic16_printGPointerType (val->name, (IS_PTR(type)?DCL_TYPE(type):PTR_TYPE(SPEC_OCLS(sym->etype))),
+ ptype, p);
+ } else
+ assert(0);
return;
}
if (!p)
return;
-// fprintf(stderr, "%s:%d generating init for %s\n", __FILE__, __LINE__, sym->name);
-
- /* if structure then */
+#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));
+#endif
+
+ /* if structure then */
if (IS_STRUCT (type))
{
- fprintf(stderr,"%s struct\n",__FUNCTION__);
- pic16_printIvalStruct (sym, type, ilist, ptype, p);
+ if (pic16_isUnion(sym, type))
+ {
+ //fprintf(stderr,"%s union\n",__FUNCTION__);
+ pic16_printIvalUnion (sym, type, ilist, ptype, p);
+ } else {
+ //fprintf(stderr,"%s struct\n",__FUNCTION__);
+ pic16_printIvalStruct (sym, type, ilist, ptype, p);
+ }
return;
}
- /* if this is an array */
+ /* if this is an array */
if (IS_ARRAY (type))
{
// fprintf(stderr,"%s array\n",__FUNCTION__);
}
}
-#if 1
+#if 0
// and the type must match
itype=ilist->init.node->ftype;
/* if this is a pointer */
if (IS_PTR (type))
{
- fprintf(stderr,"%s pointer\n",__FUNCTION__);
+// fprintf(stderr,"%s pointer\n",__FUNCTION__);
pic16_printIvalPtr (sym, type, ilist, ptype, p);
return;
}
int PIC16_IS_CONFIG_ADDRESS(int address)
{
+ return ((address >= pic16->cwInfo.confAddrStart && address <= pic16->cwInfo.confAddrEnd));
+}
+
+int PIC16_IS_IDLOC_ADDRESS(int address)
+{
+ return ((address >= pic16->idInfo.idAddrStart && address <= pic16->idInfo.idAddrEnd));
+}
- return (address >= pic16->cwInfo.confAddrStart && address <= pic16->cwInfo.confAddrEnd);
+/* 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 */
/*-----------------------------------------------------------------*/
pic16emitStaticSeg (memmap * map)
{
symbol *sym;
-
- fprintf (map->oFile, ";\t.area\t%s\n", map->sname);
+ static int didcode=0;
//fprintf(stderr, "%s\n",__FUNCTION__);
{
#if 0
- fprintf(stderr, "\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",
+ 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));
#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)));
continue;
}
- /* if it is "extern" then do nothing */
- if (IS_EXTERN (sym->etype)) {
+ if(SPEC_ABSA(sym->etype) && PIC16_IS_IDLOC_ADDRESS(SPEC_ADDR(sym->etype))) {
+ pic16_assignIdByteValue(SPEC_ADDR(sym->etype),
+ (char) floatFromVal(list2val(sym->ival)));
- /* do not emit if it is a config word declaration */
- if(!SPEC_ABSA(sym->etype)
- || (SPEC_ABSA(sym->etype) && !PIC16_IS_CONFIG_ADDRESS(SPEC_ADDR(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;
}
checkAddSym(&publics, sym);
}
-#if 0
/* print extra debug info if required */
- if (options.debug || sym->level == 0)
- {
+ 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 */
- if(cdbFile)
- cdbSymbol (sym, cdbFile, FALSE, FALSE);
-
- if (!sym->level)
- { /* global */
- if (IS_STATIC (sym->etype))
- fprintf (code->oFile, "F%s_", moduleName); /* scope is file */
- else
- fprintf (code->oFile, "G_"); /* scope is global */
- }
- else
- /* symbol is local */
- fprintf (code->oFile, "L%s_",
- (sym->localof ? sym->localof->name : "-null-"));
- fprintf (code->oFile, "%s_%d_%d", sym->name, sym->level, sym->block);
-
- }
-#endif
-
+ 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 (SPEC_ABSA (sym->etype)) {
+// fprintf(stderr, "%s:%d spec_absa is true for symbol: %s\n",
+// __FILE__, __LINE__, sym->name);
- if (options.debug || sym->level == 0)
- fprintf (code->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
-
- fprintf (code->oFile, "%s\t=\t0x%04x\n",
- sym->rname,
- SPEC_ADDR (sym->etype));
-
/* if it has an initial value */
if (sym->ival)
{
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));
- abSym->name = Safe_strdup( sym->rname );
+ 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"));
+ pb = pic16_newpCodeChain(NULL, 'A', pic16_newpCodeCharP("; Starting pCode block for absolute Ival"));
pic16_addpBlock(pb);
pcf = pic16_newpCodeFunction(moduleName, asym->name);
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);
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)
+ 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);
- /*printChar (code->oFile,
- SPEC_CVAL (sym->etype).v_char,
- strlen (SPEC_CVAL (sym->etype).v_char) + 1);*/
- else
- fprintf (code->oFile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff);
+ } 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 (options.debug || sym->level == 0)
- fprintf (code->oFile, " == .\n");
-
+
/* if it has an initial value */
- if (sym->ival)
- {
+ 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);
- pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(sym->rname,-1));
+ 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
- {
+ } 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)
+ 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);
- /*printChar (code->oFile,
- SPEC_CVAL (sym->etype).v_char,
- strlen (SPEC_CVAL (sym->etype).v_char) + 1);*/
- else
- fprintf (code->oFile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff);
+ } else {
+ assert(0);
+ }
}
}
}
{
int i;
- for(i=0;i<pic16->cwInfo.confAddrEnd-pic16->cwInfo.confAddrStart;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);
}
+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);
+}
+
static void
pic16emitMaps ()
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\t0x0000\n");
- fprintf (vFile, "__interrupt_vect:\n");
+ 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! */
port->genIVT(vFile, interrupts, maxInterrupts);
}
}
+#endif
}
static void
pic16initialComments (FILE * afile)
{
- initialComments (afile);
- fprintf (afile, "; PIC16 port for the Microchip 16-bit core micros\n");
- fprintf (afile, iComments2);
+ 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}") );
+ }
+}
+
+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;
}
/*-----------------------------------------------------------------*/
{
symbol *sym;
- 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))
- fprintf(afile, "\tglobal %s\n", sym->rname);
+ /* sanity check */
+ if(!IS_STATIC(sym->etype))
+ pic16_emitSymbolIfNew(afile, "\tglobal %s\n", sym->rname, 0);
}
/*-----------------------------------------------------------------*/
{
symbol *sym;
- fprintf(afile, "%s", iComments2);
+ /* 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);
+ pic16_emitSymbolIfNew(afile, "\textern %s\n", sym->rname, 1);
for(sym = setFirstItem(pic16_builtin_functions); sym; sym = setNextItem(pic16_builtin_functions))
- fprintf(afile, "\textern _%s\n", sym->name);
+ pic16_emitSymbolIfNew(afile, "\textern _%s\n", sym->name, 1);
}
/*-----------------------------------------------------------------*/
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 (IS_FUNC (sym->type))
continue;
-#if 0
- /* print extra debug info if required */
- if (options.debug || sym->level == 0)
- {
-
- cdbSymbol (sym, cdbFile, FALSE, FALSE);
-
- if (!sym->level)
- { /* global */
- if (IS_STATIC (sym->etype))
- fprintf (afile, "F%s_", moduleName); /* scope is file */
- else
- fprintf (afile, "G_"); /* scope is global */
- }
- else
- /* symbol is local */
- fprintf (afile, "L%s_",
- (sym->localof ? sym->localof->name : "-null-"));
- fprintf (afile, "%s_%d_%d", sym->name, sym->level, sym->block);
- }
-#endif
/* if is has an absolute address then generate
an equate for this no need to allocate space */
}
}
+void emitStatistics(FILE *asmFile)
+{
+ unsigned long isize, udsize, ramsize;
+ statistics.isize = pic16_countInstructions();
+ isize = (statistics.isize >= 0) ? statistics.isize : 0;
+ udsize = (statistics.udsize >= 0) ? statistics.udsize : 0;
+ 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),
+ isize>>1, isize>>1);
+ fprintf (asmFile, "; udata size:\t%5ld (0x%04lx) bytes (%5.2f%%)\n",
+ udsize, udsize, (udsize*100.0) / (1.0 * ramsize));
+ fprintf (asmFile, "; access size:\t%5ld (0x%04lx) bytes\n",
+ statistics.intsize, statistics.intsize);
+
+ fprintf (asmFile, "\n\n");
+}
+
+
/*-----------------------------------------------------------------*/
/* glue - the final glue that hold the whole thing together */
void
pic16glue ()
{
-
FILE *vFile;
FILE *asmFile;
FILE *ovrFile = tempfile();
+ mainf = newSymbol ("main", 0);
+ mainf->block = 0;
- mainf = newSymbol ("main", 0);
- mainf->block = 0;
-
- mainf = findSymWithLevel(SymbolTab, mainf);
-#if 0
- /* only if the main function exists */
- if (!(mainf = findSymWithLevel (SymbolTab, mainf))) {
- if (!options.cc_only)
- werror (E_NO_MAIN);
- return;
- }
-#endif
-
-// fprintf(stderr, "main function= %p (%s)\thas body= %d\n", mainf, (mainf?mainf->name:NULL), mainf?IFFUNC_HASBODY(mainf->type):-1);
+ mainf = findSymWithLevel(SymbolTab, mainf);
- addSetHead(&tmpfileSet,ovrFile);
- pic16_pCodeInitRegisters();
+ addSetHead(&tmpfileSet,ovrFile);
+ pic16_pCodeInitRegisters();
- if (mainf && IFFUNC_HASBODY(mainf->type)) {
- pBlock *pb = pic16_newpCodeChain(NULL,'X',pic16_newpCodeCharP("; Starting pCode block"));
+ if(pic16_options.no_crt && mainf && IFFUNC_HASBODY(mainf->type)) {
+ pBlock *pb = pic16_newpCodeChain(NULL,'X',pic16_newpCodeCharP("; Starting pCode block"));
- pic16_addpBlock(pb);
+ pic16_addpBlock(pb);
- /* entry point @ start of CSEG */
- pic16_addpCode2pBlock(pb,pic16_newpCodeLabel("__sdcc_program_startup",-1));
+ /* entry point @ start of CSEG */
+ pic16_addpCode2pBlock(pb,pic16_newpCodeLabel("__sdcc_program_startup",-1));
- if(initsfpnt) {
- pic16_addpCode2pBlock(pb, pic16_newpCode(POC_LFSR,
- pic16_popGetLit2(1, pic16_newpCodeOpRegFromStr("stack"))));
- pic16_addpCode2pBlock(pb, pic16_newpCode(POC_LFSR,
- pic16_popGetLit2(2, pic16_newpCodeOpRegFromStr("stack"))));
- }
+ if(initsfpnt) {
+ pic16_addpCode2pBlock(pb, pic16_newpCode(POC_LFSR,
+ pic16_popGetLit2(1, pic16_newpCodeOpRegFromStr("_stack_end"))));
+ pic16_addpCode2pBlock(pb, pic16_newpCode(POC_LFSR,
+ pic16_popGetLit2(2, pic16_newpCodeOpRegFromStr("_stack_end"))));
+ }
- /* put in the call to main */
- pic16_addpCode2pBlock(pb,pic16_newpCode(POC_CALL,pic16_newpCodeOp("_main",PO_STR)));
+ /* put in the call to main */
+ pic16_addpCode2pBlock(pb,pic16_newpCode(POC_CALL,pic16_newpCodeOp("_main",PO_STR)));
- if (options.mainreturn) {
- pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(";\treturn from main will return to caller\n"));
- pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETURN,NULL));
- } else {
- pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(";\treturn from main will lock up\n"));
- pic16_addpCode2pBlock(pb,pic16_newpCode(POC_GOTO,pic16_newpCodeOp("$",PO_STR)));
- }
- }
+ if (options.mainreturn) {
+ pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(";\treturn from main will return to caller\n"));
+ pic16_addpCode2pBlock(pb,pic16_newpCode(POC_RETURN,NULL));
+ } else {
+ pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(";\treturn from main will lock up\n"));
+ pic16_addpCode2pBlock(pb,pic16_newpCode(POC_GOTO,pic16_newpCodeOp("$",PO_STR)));
+ }
+ }
- /* At this point we've got all the code in the form of pCode structures */
- /* Now it needs to be rearranged into the order it should be placed in the */
- /* code space */
+ /* At this point we've got all the code in the form of pCode structures */
+ /* Now it needs to be rearranged into the order it should be placed in the */
+ /* code space */
- pic16_movepBlock2Head('P'); // Last
- pic16_movepBlock2Head(code->dbName);
- pic16_movepBlock2Head('X');
- pic16_movepBlock2Head(statsg->dbName); // First
+ pic16_movepBlock2Head('P'); // Last
+ pic16_movepBlock2Head(code->dbName);
+ pic16_movepBlock2Head('X');
+ pic16_movepBlock2Head(statsg->dbName); // First
- /* print the global struct definitions */
-// if (options.debug)
-// cdbStructBlock (0); //,cdbFile);
+ /* 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);
- }
+ 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);
+ }
- addSetHead(&tmpfileSet,vFile);
+ addSetHead(&tmpfileSet,vFile);
- /* emit code for the all the variables declared */
- pic16emitMaps ();
- /* do the overlay segments */
- pic16emitOverlay(ovrFile);
- pic16_AnalyzepCode('*');
+ /* emit code for the all the variables declared */
+ pic16emitMaps ();
-#if 0
- {
- FILE *cFile;
- sprintf(buffer, dstFileName);
- strcat(buffer, ".calltree");
- cFile = fopen(buffer, "w");
- pic16_printCallTree( cFile );
- fclose(cFile);
- }
+ /* do the overlay segments */
+ pic16emitOverlay(ovrFile);
+ pic16_AnalyzepCode('*');
+
+#if 1
+ if(pic16_options.dumpcalltree) {
+ FILE *cFile;
+
+ sprintf(buffer, dstFileName);
+ strcat(buffer, ".calltree");
+ cFile = fopen(buffer, "w");
+ pic16_printCallTree( cFile );
+ fclose(cFile);
+ }
#endif
- pic16_InlinepCode();
- pic16_AnalyzepCode('*');
+ pic16_InlinepCode();
+ pic16_AnalyzepCode('*');
- if(pic16_debug_verbose)
- pic16_pcode_test();
- /* now put it all together into the assembler file */
- /* create the assembler file name */
- if ((noAssemble || options.c1mode) && fullDstFileName) {
- sprintf (buffer, fullDstFileName);
- } else {
- sprintf (buffer, dstFileName);
- strcat (buffer, ".asm");
- }
+ if(pic16_debug_verbose)
+ pic16_pcode_test();
- if (!(asmFile = fopen (buffer, "w"))) {
- werror (E_FILE_OPEN_ERR, buffer);
- exit (1);
- }
-
- /* initial comments */
- pic16initialComments (asmFile);
-
- /* print module name */
- fprintf(asmFile, "#FILE\t\"%s\"\n", fullSrcFileName);
-
- /* Let the port generate any global directives, etc. */
- if (port->genAssemblerPreamble) {
- port->genAssemblerPreamble(asmFile);
- }
-
- /* print the extern variables to this module */
- pic16_printExterns(asmFile);
-
- /* print the global variables in this module */
- pic16printPublics (asmFile);
+ /* now put it all together into the assembler file */
+ /* create the assembler file name */
+ if((noAssemble || options.c1mode) && fullDstFileName) {
+ sprintf (buffer, fullDstFileName);
+ } else {
+ sprintf (buffer, dstFileName);
+ strcat (buffer, ".asm");
+ }
-#if 0
- /* copy the sfr segment */
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; special function registers\n");
- fprintf (asmFile, "%s", iComments2);
- copyFile (asmFile, sfr->oFile);
-#endif
+ if(!(asmFile = fopen (buffer, "w"))) {
+ werror (E_FILE_OPEN_ERR, buffer);
+ exit (1);
+ }
+ /* initial comments */
+ pic16initialComments (asmFile);
- /* Put all variables into a cblock */
- pic16_AnalyzeBanking();
- pic16_writeUsedRegs(asmFile);
+ /* print module name */
+ if(options.debug)
+ fprintf(asmFile, "\t.file\t\"%s\"\n", fullSrcFileName);
-#if 0
- /* create the overlay segments */
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; overlayable items in internal ram \n");
- fprintf (asmFile, "%s", iComments2);
- copyFile (asmFile, ovrFile);
-#endif
+ /* Let the port generate any global directives, etc. */
+ if(port->genAssemblerPreamble) {
+ port->genAssemblerPreamble(asmFile);
+ }
+
+ /* Put all variables into a cblock */
+ pic16_AnalyzeBanking();
#if 0
-
- /* create the stack segment MOF */
- if (mainf && IFFUNC_HASBODY(mainf->type)) {
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; Stack segment in internal ram \n");
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, ";\t.area\tSSEG\t(DATA)\n"
- ";__start__stack:\n;\t.ds\t1\n\n");
- }
+ if(pic16_options.opt_flags & OF_LR_SUPPORT) {
+ pic16_OptimizeLocalRegs();
+ }
#endif
-#if 0
- /* no indirect data in pic */
- /* create the idata segment */
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; indirectly addressable internal ram data\n");
- fprintf (asmFile, "%s", iComments2);
- copyFile (asmFile, idata->oFile);
-#endif
+ /* remove redundant BANKSELs -- added by RN 2005-01-17 */
+ 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) {
+ pic16_OptimizeJumps();
+ }
+ /* print the global variables in this module */
+ pic16printPublics (asmFile);
+
+ /* print the extern variables to this module */
+ pic16_printExterns(asmFile);
+
+ pic16_writeUsedRegs(asmFile);
#if 0
- /* no xdata in pic */
- /* if external stack then reserve space of it */
- if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack ) {
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; external stack \n");
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile,";\t.area XSEG (XDATA)\n"); /* MOF */
- fprintf (asmFile,";\t.ds 256\n");
- }
+ /* no xdata in pic */
+ /* if external stack then reserve space of it */
+ if(mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack ) {
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; external stack \n");
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile,";\t.area XSEG (XDATA)\n"); /* MOF */
+ fprintf (asmFile,";\t.ds 256\n");
+ }
#endif
#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);
+ /* 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);
#endif
#if 0
- /* copy the bit segment */
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; bit data\n");
- fprintf (asmFile, "%s", iComments2);
- copyFile (asmFile, bit->oFile);
+ /* copy the bit segment */
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; bit data\n");
+ fprintf (asmFile, "%s", iComments2);
+ copyFile (asmFile, bit->oFile);
#endif
- /* copy the interrupt vector table */
- if(mainf && IFFUNC_HASBODY(mainf->type)) {
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; interrupt vector \n");
- fprintf (asmFile, "%s", iComments2);
- copyFile (asmFile, vFile);
- }
+ /* copy the interrupt vector table */
+ if(mainf && IFFUNC_HASBODY(mainf->type)) {
+ fprintf (asmFile, "\n%s", iComments2);
+ fprintf (asmFile, "; interrupt vector \n");
+ fprintf (asmFile, "%s", iComments2);
+ copyFile (asmFile, vFile);
+ }
- /* copy global & static initialisations */
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "; global & static initialisations\n");
- fprintf (asmFile, "%s", iComments2);
+ /* copy global & static initialisations */
+ fprintf (asmFile, "\n%s", iComments2);
+ fprintf (asmFile, "; global & static initialisations\n");
+ fprintf (asmFile, "%s", iComments2);
-#if 0
- /* copy over code */
- fprintf (asmFile, "%s", iComments2);
- fprintf (asmFile, "\tcode\n");
- fprintf (asmFile, "%s", iComments2);
-#endif
-
- if(pic16_debug_verbose)
- fprintf(asmFile, "; A code from now on!\n");
- pic16_copypCode(asmFile, 'A');
-
-
- if(mainf && IFFUNC_HASBODY(mainf->type)) {
- fprintf(asmFile, "\tcode\n");
- fprintf(asmFile,"__sdcc_gsinit_startup:\n");
-
-#if 0
- /* FIXME 8051 legacy (?!) - VR 20-Jun-2003 */
- /* if external stack is specified then the
- * higher order byte of the xdatalocation is
- * going into P2 and the lower order going into */
-
- if (options.useXstack) {
- fprintf(asmFile,";\tmov\tP2,#0x%02x\n",
- (((unsigned int)options.xdata_loc) >> 8) & 0xff);
- fprintf(asmFile,";\tmov\t_spx,#0x%02x\n",
- (unsigned int)options.xdata_loc & 0xff);
- }
-#endif
- }
+ if(pic16_debug_verbose)
+ fprintf(asmFile, "; A code from now on!\n");
+
+ pic16_copypCode(asmFile, 'A');
-// copyFile (stderr, code->oFile);
+ if(pic16_options.no_crt) {
+ if(mainf && IFFUNC_HASBODY(mainf->type)) {
+ fprintf(asmFile, "\tcode\n");
+ fprintf(asmFile,"__sdcc_gsinit_startup:\n");
+ }
+ }
- fprintf(asmFile, "; I code from now on!\n");
- pic16_copypCode(asmFile, 'I');
+// copyFile (stderr, code->oFile);
- if(pic16_debug_verbose)
- fprintf(asmFile, "; dbName from now on!\n");
- pic16_copypCode(asmFile, statsg->dbName);
+ 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 (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type)) {
- fprintf (asmFile,"\tgoto\t__sdcc_program_startup\n");
- }
+ if(pic16_options.no_crt) {
+ if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type)) {
+ fprintf (asmFile,"\tgoto\t__sdcc_program_startup\n");
+ }
+ }
+ if(pic16_debug_verbose)
+ fprintf(asmFile, "; X code from now on!\n");
- if(pic16_debug_verbose)
- fprintf(asmFile, "; X code from now on!\n");
- pic16_copypCode(asmFile, 'X');
-
- if(pic16_debug_verbose)
- fprintf(asmFile, "; M code from now on!\n");
- pic16_copypCode(asmFile, 'M');
+ pic16_copypCode(asmFile, 'X');
+ if(pic16_debug_verbose)
+ fprintf(asmFile, "; M code from now on!\n");
- pic16_copypCode(asmFile, code->dbName);
+ pic16_copypCode(asmFile, 'M');
- pic16_copypCode(asmFile, 'P');
+ pic16_copypCode(asmFile, code->dbName);
+
+ pic16_copypCode(asmFile, 'P');
- fprintf (asmFile,"\tend\n");
- fclose (asmFile);
+ emitStatistics(asmFile);
- rm_tmpfiles();
+ fprintf (asmFile,"\tend\n");
+ fclose (asmFile);
+
+ rm_tmpfiles();
}