1 /*-------------------------------------------------------------------------
3 SDCCglue.c - glues everything we have done together into one file.
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
25 #include "../common.h"
35 #ifdef WORDS_BIGENDIAN
36 #define _ENDIAN(x) (3-x)
38 #define _ENDIAN(x) (x)
41 #define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
43 extern symbol *interrupts[256];
44 static void printIval (symbol * sym, sym_link * type, initList * ilist, pBlock *pb);
48 extern unsigned maxInterrupts;
49 extern int maxRegBank;
51 extern char *VersionString;
52 extern FILE *codeOutFile;
53 extern set *tmpfileSet;
54 extern set *tmpfileNameSet;
55 extern char *iComments1;
56 extern char *iComments2;
57 //extern void emitStaticSeg (memmap * map);
58 set *pic14_localFunctions = NULL;
60 extern DEFSETFUNC (closeTmpFiles);
61 extern DEFSETFUNC (rmTmpFiles);
63 extern void AnalyzeBanking (void);
64 extern void copyFile (FILE * dest, FILE * src);
65 extern void ReuseReg(void);
66 extern void InlinepCode(void);
67 extern void writeUsedRegs(FILE *);
69 extern void initialComments (FILE * afile);
70 extern void printPublics (FILE * afile);
72 extern void printChar (FILE * ofile, char *s, int plen);
73 void pCodeInitRegisters(void);
74 int getConfigWord(int address);
75 int getHasSecondConfigReg(void);
76 int pic14_getSharebankSize(void);
77 int pic14_getSharebankAddress(void);
79 char *udata_section_name=0; // FIXME Temporary fix to change udata section name -- VR
80 int pic14_hasInterrupt = 0; // Indicates whether to emit interrupt handler or not
82 /*-----------------------------------------------------------------*/
83 /* aopLiteral - string from a literal value */
84 /*-----------------------------------------------------------------*/
85 unsigned int pic14aopLiteral (value *val, int offset)
92 /* if it is a float then it gets tricky */
93 /* otherwise it is fairly simple */
94 if (!IS_FLOAT(val->type)) {
95 unsigned long v = (unsigned long) floatFromVal(val);
97 return ( (v >> (offset * 8)) & 0xff);
100 /* it is type float */
101 fl.f = (float) floatFromVal(val);
102 #ifdef WORDS_BIGENDIAN
103 return fl.c[3-offset];
110 /* Check whether the given reg is shared amongst all .o files of a project.
111 * This is true for the pseudo stack and WSAVE, SSAVE and PSAVE. */
113 is_shared_address (int addr)
115 return ((addr > Gstack_base_addr - 18)
116 && (addr <= Gstack_base_addr));
120 pic14_is_shared (regs *reg)
123 return is_shared_address (reg->address);
127 is_valid_identifier( const char *name )
133 /* only accept [a-zA-Z_][a-zA-Z0-9_] */
134 if (!((a >= 'a' && a <= 'z')
135 || (a >= 'A' && a <= 'z')
140 while ((a = *name++))
142 if (!((a >= 'a' && a <= 'z')
143 || (a >= 'A' && a <= 'Z')
144 || (a >= '0' && a <= '9')
149 /* valid identifier */
153 /* set of already emitted symbols; we store only pointers to the emitted
154 * symbol names so these MUST NO BE CHANGED afterwards... */
155 static set *symbolsEmitted = NULL;
157 /*-------------------------------------------------------------------*/
158 /* emitSymbolToFile - write a symbol definition only if it is not */
159 /* already present */
160 /*-------------------------------------------------------------------*/
162 emitSymbolToFile (FILE *of, const char *name, const char *section_type, int size, int addr, int useEQU, int globalize)
165 static unsigned int sec_idx = 0;
167 /* workaround: variables declared via `sbit' result in a numeric
168 * identifier (0xHH), EQU'ing them is invalid, so just ignore it.
169 * sbit is heavily used in the inc2h-generated header files!
171 if (!is_valid_identifier(name))
173 //fprintf( stderr, "%s:%s:%u: ignored symbol: %s\n", __FILE__, __FUNCTION__, __LINE__, name );
177 /* check whether the symbol is already defined */
178 for (sym = (const char *) setFirstItem (symbolsEmitted);
180 sym = (const char *) setNextItem (symbolsEmitted))
182 if (!strcmp (sym, name))
184 //fprintf (stderr, "%s: already emitted: %s\n", __FUNCTION__, name);
189 /* new symbol -- define it */
190 //fprintf (stderr, "%s: emitting %s (%d)\n", __FUNCTION__, name, size);
192 fprintf (of, "%s\tEQU\t0x%04x\n", name, addr);
195 /* we place each symbol into a section of its own to allow the linker
196 * to distribute the data into all available memory banks */
197 if (!section_type) section_type = "udata";
200 /* absolute symbols are handled in pic14_constructAbsMap */
203 /* workaround gpasm bug with symbols being EQUated and placed in absolute sections */
204 if (1 || !is_shared_address (addr))
206 if (globalize) fprintf (of, "\tglobal\t%s\n", name);
207 fprintf (of, "udata_%s_%u\t%s\t0x%04x\n", moduleName,
208 sec_idx++, "udata_ovr", addr);
209 fprintf (of, "%s\tres\t%d\n", name, size);
213 /* EQUs cannot be exported... */
214 fprintf (of, "%s\tEQU\t0x%04x\n", name, addr);
218 if (globalize) fprintf (of, "\tglobal\t%s\n", name);
219 fprintf (of, "udata_%s_%u\t%s\n", moduleName,
220 sec_idx++, section_type);
221 fprintf (of, "%s\tres\t%d\n", name, size);
225 //if (options.verbose) fprintf (stderr, "%s: emitted %s\n", __FUNCTION__, name);
226 addSet (&symbolsEmitted, (void *) name);
229 #define IS_DEFINED_HERE(sym) (!IS_EXTERN(sym->etype))
230 extern int IS_CONFIG_ADDRESS( int addr );
232 pic14_constructAbsMap (FILE *ofile)
234 memmap *maps[] = { data, sfr, NULL };
239 int addr, min=-1, max=-1;
242 for (i=0; maps[i] != NULL; i++)
244 for (sym = (symbol *)setFirstItem (maps[i]->syms);
245 sym; sym = setNextItem (maps[i]->syms))
247 if (IS_DEFINED_HERE(sym) && SPEC_ABSA(sym->etype))
249 addr = SPEC_ADDR(sym->etype);
251 /* handle CONFIG words here */
252 if (IS_CONFIG_ADDRESS( addr ))
254 //fprintf( stderr, "%s: assignment to CONFIG@0x%x found\n", __FUNCTION__, addr );
255 //fprintf( stderr, "ival: %p (0x%x)\n", sym->ival, (int)list2int( sym->ival ) );
257 pic14_assignConfigWordValue( addr, (int)list2int( sym->ival ) );
259 fprintf( stderr, "ERROR: Symbol %s, which is covering a __CONFIG word must be initialized!\n", sym->name );
264 if (max == -1 || addr > max) max = addr;
265 if (min == -1 || addr < min) min = addr;
266 //fprintf (stderr, "%s: sym %s @ 0x%x\n", __FUNCTION__, sym->name, addr);
267 aliases = hTabItemWithKey (ht, addr);
269 /* May not use addSetHead, as we cannot update the
270 * list's head in the hastable `ht'. */
271 addSet (&aliases, sym);
273 fprintf( stderr, "%s: now %d aliases for %s @ 0x%x\n",
274 __FUNCTION__, elementsInSet(aliases), sym->name, addr);
277 addSet (&aliases, sym);
278 hTabAddItem (&ht, addr, aliases);
284 /* now emit definitions for all absolute symbols */
285 fprintf (ofile, "%s", iComments2);
286 fprintf (ofile, "; absolute symbol definitions\n");
287 fprintf (ofile, "%s", iComments2);
288 for (addr=min; addr <= max; addr++)
291 aliases = hTabItemWithKey (ht, addr);
292 if (aliases && elementsInSet(aliases)) {
293 fprintf (ofile, "udata_abs_%s_%x\tudata_ovr\t0x%04x",
294 moduleName, addr, addr);
295 for (sym = setFirstItem (aliases); sym;
296 sym = setNextItem (aliases))
298 /* emit STATUS as well as _STATUS, required for SFRs only */
299 fprintf (ofile, "\n%s", sym->name);
300 fprintf (ofile, "\n%s", sym->rname);
301 if (getSize(sym->type) > size) {
302 size = getSize(sym->type);
305 fprintf (ofile, "\tres\t%d\n", size);
309 /* also emit STK symbols
310 * XXX: This is ugly and fails as soon as devices start to get
311 * differently sized sharebanks, since STK12 will be
312 * required by larger devices but only up to STK03 might
313 * be defined using smaller devices. */
314 fprintf (ofile, "\n");
315 if (!pic14_options.isLibrarySource)
317 fprintf (ofile, "\tglobal PSAVE\n");
318 fprintf (ofile, "\tglobal SSAVE\n");
319 fprintf (ofile, "\tglobal WSAVE\n");
320 for (i=pic14_getSharebankSize()-4; i >= 0; i--) {
321 fprintf (ofile, "\tglobal STK%02d\n", i);
323 fprintf (ofile, "sharebank udata_ovr 0x%04x\n",
324 pic14_getSharebankAddress() - pic14_getSharebankSize() + 1);
325 fprintf (ofile, "PSAVE\tres 1\n");
326 fprintf (ofile, "SSAVE\tres 1\n");
327 fprintf (ofile, "WSAVE\tres 1\n");
328 /* fill rest of sharebank with stack STKxx .. STK00 */
329 for (i=pic14_getSharebankSize()-4; i >= 0; i--) {
330 fprintf (ofile, "STK%02d\tres 1\n", i);
333 /* declare STKxx as extern for all files
334 * except the one containing main() */
335 fprintf (ofile, "\textern PSAVE\n");
336 fprintf (ofile, "\textern SSAVE\n");
337 fprintf (ofile, "\textern WSAVE\n");
338 for (i=pic14_getSharebankSize()-4; i >= 0; i--) {
339 fprintf (ofile, "\textern STK%02d\n", i);
344 /*-----------------------------------------------------------------*/
345 /* emitRegularMap - emit code for maps with no special cases */
346 /*-----------------------------------------------------------------*/
348 pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
353 /* print the area name */
355 fprintf (map->oFile, ";\t.area\t%s\n", map->sname);
357 for (sym = setFirstItem (map->syms); sym;
358 sym = setNextItem (map->syms)) {
360 //printf("%s\n",sym->name);
362 /* ignore if config word */
363 if (SPEC_ABSA(sym->etype)
364 && IS_CONFIG_ADDRESS(SPEC_ADDR(sym->etype)))
367 /* if extern then add it into the extern list */
368 if (IS_EXTERN (sym->etype)) {
369 addSetHead (&externs, sym);
373 /* if allocation required check is needed
374 then check if the symbol really requires
375 allocation only for local variables */
376 if (arFlag && !IS_AGGREGATE (sym->type) &&
377 !(sym->_isparm && !IS_REGPARM (sym->etype)) &&
378 !sym->allocreq && sym->level)
381 /* if global variable & not static or extern
382 and addPublics allowed then add it to the public set */
383 if ((sym->level == 0 ||
384 (sym->_isparm && !IS_REGPARM (sym->etype))) &&
386 !IS_STATIC (sym->etype))
388 //fprintf( stderr, "%s: made public %s\n", __FUNCTION__, sym->name );
389 addSetHead (&publics, sym);
392 // PIC code allocates its own registers - so ignore parameter variable generated by processFuncArgs()
395 /* if extern then do nothing or is a function
397 if (IS_FUNC (sym->type))
400 /* print extra debug info if required */
401 if (options.debug || sym->level == 0)
403 if (!sym->level) /* global */
404 if (IS_STATIC (sym->etype))
405 fprintf (map->oFile, "F%s_", moduleName); /* scope is file */
407 fprintf (map->oFile, "G_"); /* scope is global */
409 /* symbol is local */
410 fprintf (map->oFile, "L%s_", (sym->localof ? sym->localof->name : "-null-"));
411 fprintf (map->oFile, "%s_%d_%d", sym->name, sym->level, sym->block);
414 /* absolute symbols are handled in pic14_constructAbsMap */
415 if (SPEC_ABSA(sym->etype) && IS_DEFINED_HERE(sym))
418 /* if it has an absolute address then generate
419 an equate for this no need to allocate space */
420 if (0 && SPEC_ABSA (sym->etype))
422 //if (options.debug || sym->level == 0)
423 //fprintf (map->oFile,"; == 0x%04x\n",SPEC_ADDR (sym->etype));
425 fprintf (map->oFile, "%s\tEQU\t0x%04x\n",
427 SPEC_ADDR (sym->etype));
433 /* If this is a bit variable, then allocate storage after 8 bits have been declared */
434 /* unlike the 8051, the pic does not have a separate bit area. So we emulate bit ram */
435 /* by grouping the bits together into groups of 8 and storing them in the normal ram. */
436 if (IS_BITVAR (sym->etype))
442 emitSymbolToFile (map->oFile,
445 getSize (sym->type) & 0xffff,
446 SPEC_ABSA(sym->etype)
447 ? SPEC_ADDR(sym->etype)
455 if ((size = (unsigned int) getSize (sym->type) & 0xffff) > 1)
457 for (i = 1; i < size; i++)
458 fprintf (map->oFile, "\t%s_%d\n", sym->rname, i);
463 //fprintf (map->oFile, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff);
466 /* if it has a initial value then do it only if
467 it is a global variable */
468 if (sym->ival && sym->level == 0) {
471 if (IS_AGGREGATE (sym->type))
472 ival = initAggregates (sym, sym->ival, NULL);
474 ival = newNode ('=', newAst_VALUE(symbolVal (sym)),
475 decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_TYPE_NONE));
476 codeOutFile = statsg->oFile;
478 eBBlockFromiCode (iCodeFromAst (ival));
485 /*-----------------------------------------------------------------*/
486 /* printIvalType - generates ival for int/char */
487 /*-----------------------------------------------------------------*/
489 printIvalType (symbol *sym, sym_link * type, initList * ilist, pBlock *pb)
494 //fprintf(stderr, "%s\n",__FUNCTION__);
496 /* if initList is deep */
497 if (ilist->type == INIT_DEEP)
498 ilist = ilist->init.deep;
500 if (!IS_AGGREGATE(sym->type) && getNelements(type, ilist)>1) {
501 werror (W_EXCESS_INITIALIZERS, "scalar", sym->name, sym->lineDef);
504 if (!(val = list2val (ilist))) {
505 // assuming a warning has been thrown
509 if (val->type != type) {
510 val = valCastLiteral(type, floatFromVal(val));
514 ulval = (unsigned long) floatFromVal (val);
518 switch (getSize (type)) {
520 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
524 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
525 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,1))));
529 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,0))));
530 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,1))));
531 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,2))));
532 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,3))));
537 /*-----------------------------------------------------------------*/
538 /* printIvalBitFields - generate initializer for bitfields */
539 /*-----------------------------------------------------------------*/
540 static void printIvalBitFields(symbol **sym, initList **ilist, pBlock *pb )
544 initList *lilist = *ilist ;
545 unsigned long ival = 0;
551 val = list2val(lilist);
553 if (SPEC_BLEN(lsym->etype) > 8) {
554 size += ((SPEC_BLEN (lsym->etype) / 8) +
555 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
558 size = ((SPEC_BLEN (lsym->etype) / 8) +
559 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
561 i = (unsigned long)floatFromVal(val);
562 i <<= SPEC_BSTR (lsym->etype);
564 if (! ( lsym->next &&
565 (IS_BITFIELD(lsym->next->type)) &&
566 (SPEC_BSTR(lsym->next->etype)))) break;
568 lilist = lilist->next;
572 //tfprintf (oFile, "\t!db !constbyte\n",ival);
573 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(ival)));
577 //tfprintf (oFile, "\t!dw !constword\n",ival);
578 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(ival>>8)));
579 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(ival)));
582 //tfprintf (oFile, "\t!db !constword,!constword\n",(ival >> 8) & 0xffff, (ival & 0xffff));
583 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(ival>>24)));
584 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(ival>>16)));
585 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(ival>>8)));
586 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(ival)));
593 /*-----------------------------------------------------------------*/
594 /* printIvalStruct - generates initial value for structures */
595 /*-----------------------------------------------------------------*/
596 static void printIvalStruct (symbol * sym, sym_link * type, initList * ilist, pBlock *pb)
599 initList *iloop = NULL;
601 sflds = SPEC_STRUCT (type)->fields;
604 if (ilist->type != INIT_DEEP) {
605 werrorfl (sym->fileDef, sym->lineDef, E_INIT_STRUCT, sym->name);
609 iloop = ilist->init.deep;
612 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
613 if (IS_BITFIELD(sflds->type)) {
614 printIvalBitFields(&sflds,&iloop,pb);
616 printIval (sym, sflds->type, iloop, pb);
620 werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "struct", sym->name);
625 /*-----------------------------------------------------------------*/
626 /* printIvalChar - generates initital value for character array */
627 /*-----------------------------------------------------------------*/
629 printIvalChar (sym_link * type, initList * ilist, pBlock *pb, char *s)
637 //fprintf(stderr, "%s\n",__FUNCTION__);
641 val = list2val (ilist);
643 /* if the value is a character string */
644 if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
646 ilen = DCL_ELEM(val->type);
648 if (!DCL_ELEM (type))
649 DCL_ELEM (type) = ilen;
651 /* emit string constant */
652 for (remain = 0; remain < ilen; remain++) {
653 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(SPEC_CVAL(val->etype).v_char[remain])));
656 /* fill array up to desired size */
657 if ((remain = (DCL_ELEM (type) - ilen)) > 0)
659 //tfprintf (oFile, "\t!db !constbyte\n", 0);
660 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(0)));
667 //printChar (oFile, s, strlen (s) + 1);
669 for(remain=0; remain<(int)strlen(s); remain++) {
670 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(s[remain])));
671 //fprintf(stderr,"0x%02x ",s[remain]);
673 //fprintf(stderr,"\n");
678 /*-----------------------------------------------------------------*/
679 /* printIvalArray - generates code for array initialization */
680 /*-----------------------------------------------------------------*/
682 printIvalArray (symbol * sym, sym_link * type, initList * ilist,
691 /* take care of the special case */
692 /* array of characters can be init */
694 if (IS_CHAR (type->next)) {
695 //fprintf(stderr,"%s:%d - is_char\n",__FUNCTION__,__LINE__);
696 if (!IS_LITERAL(list2val(ilist)->etype)) {
697 werror (W_INIT_WRONG);
700 if (printIvalChar (type,
701 (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
702 pb, SPEC_CVAL (sym->etype).v_char))
705 /* not the special case */
706 if (ilist->type != INIT_DEEP) {
707 werrorfl (ilist->filename, ilist->lineno, E_INIT_STRUCT, sym->name);
711 for (iloop=ilist->init.deep; iloop; iloop=iloop->next) {
712 if ((++size > DCL_ELEM(type)) && DCL_ELEM(type)) {
713 werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "array", sym->name);
716 printIval (sym, type->next, iloop, pb);
720 if (DCL_ELEM(type)) {
721 // pad with zeros if needed
722 if (size<DCL_ELEM(type)) {
723 size = (DCL_ELEM(type) - size) * getSize(type->next);
725 //tfprintf (oFile, "\t!db !constbyte\n", 0);
726 addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(0)));
730 // we have not been given a size, but we now know it
731 DCL_ELEM (type) = size;
737 /*-----------------------------------------------------------------*/
738 /* printIvalPtr - generates code for initial value of pointers */
739 /*-----------------------------------------------------------------*/
740 extern value *initPointer (initList *, sym_link *toType);
743 printIvalPtr (symbol * sym, sym_link * type, initList * ilist, pBlock *pb)
750 fprintf (stderr, "FIXME: initializers for pointers...\n");
751 printTypeChain (type, stderr);
753 fprintf (stderr, "symbol: %s, DCL_TYPE():%d, DCL_ELEM():%d, IS_ARRAY():%d", sym->rname, DCL_TYPE(type), DCL_ELEM(type), IS_ARRAY(type));
754 fprintf (stderr, "ilist: type=%d (INIT_DEEP=%d, INIT_NODE=%d)\n", ilist->type, INIT_DEEP, INIT_NODE);
756 if (ilist && (ilist->type == INIT_DEEP))
757 ilist = ilist->init.deep;
759 /* function pointers */
760 if (IS_FUNC (type->next))
762 assert ( !"function pointers not yet handled" );
763 //printIvalFuncPtr (type, ilist, pb);
766 if (!(val = initPointer (ilist, type)))
769 if (IS_CHAR (type->next))
771 if (printIvalChar (type, ilist, pb, NULL)) return;
775 if (compareType (type, val->type) == 0)
777 werrorfl (ilist->filename, ilist->lineno, W_INIT_WRONG);
778 printFromToType (val->type, type);
781 if (IS_LITERAL (val->etype))
783 switch (getSize (type))
786 fprintf (stderr, "BYTE: %i\n", (unsigned char)floatFromVal (val) & 0x00FF);
789 fprintf (stderr, "WORD: %i\n", (unsigned int)floatFromVal (val) & 0x00FFFF);
791 case 3: /* gneric pointers */
792 assert ( !"generic pointers not yet handled" );
794 fprintf (stderr, "LONG: %i\n", (unsigned int)floatFromVal (val) & 0x0000FFFFFFFF);
797 assert ( !"invaild size of value -- aborting" );
803 /* now handle symbolic values */
804 switch (getSize (type))
807 fprintf (stderr, "BYTE: %s", val->name);
810 fprintf (stderr, "WORD: %s", val->name);
813 fprintf (stderr, "LONG: %s", val->name);
816 assert ( !"invalid size of (symbolic) value -- aborting" );
820 /*-----------------------------------------------------------------*/
821 /* printIval - generates code for initial value */
822 /*-----------------------------------------------------------------*/
824 printIval (symbol * sym, sym_link * type, initList * ilist, pBlock *pb)
829 /* if structure then */
830 if (IS_STRUCT (type))
832 //fprintf(stderr,"%s struct: %s\n",__FUNCTION__, sym->rname);
833 printIvalStruct (sym, type, ilist, pb);
837 /* if this is an array */
840 //fprintf(stderr,"%s array: %s\n",__FUNCTION__, sym->rname);
841 printIvalArray (sym, type, ilist, pb);
845 /* if this is a pointer */
848 //fprintf(stderr,"%s pointer: %s\n",__FUNCTION__, sym->rname);
849 printIvalPtr (sym, type, ilist, pb);
853 /* if type is SPECIFIER */
856 //fprintf(stderr,"%s spec %s\n",__FUNCTION__, sym->rname);
857 printIvalType (sym, type, ilist, pb);
862 extern void pCodeConstString(char *name, char *value);
863 /*-----------------------------------------------------------------*/
864 /* emitStaticSeg - emitcode for the static segment */
865 /*-----------------------------------------------------------------*/
867 pic14emitStaticSeg (memmap * map)
871 fprintf (map->oFile, ";\t.area\t%s\n", map->sname);
873 //fprintf(stderr, "%s\n",__FUNCTION__);
875 /* for all variables in this segment do */
876 for (sym = setFirstItem (map->syms); sym;
877 sym = setNextItem (map->syms))
879 /* if extern then add it into the extern list */
880 if (IS_EXTERN (sym->etype)) {
881 addSetHead (&externs, sym);
885 /* if it is not static add it to the public
887 if (!IS_STATIC (sym->etype))
888 addSetHead (&publics, sym);
890 /* print extra debug info if required */
891 if (options.debug || sym->level == 0)
895 if (IS_STATIC (sym->etype))
896 fprintf (code->oFile, "F%s_", moduleName); /* scope is file */
898 fprintf (code->oFile, "G_"); /* scope is global */
901 /* symbol is local */
902 fprintf (code->oFile, "L%s_",
903 (sym->localof ? sym->localof->name : "-null-"));
904 fprintf (code->oFile, "%s_%d_%d", sym->name, sym->level, sym->block);
908 /* if it has an absolute address */
909 if (SPEC_ABSA (sym->etype))
911 if (options.debug || sym->level == 0)
912 fprintf (code->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
914 fprintf (code->oFile, "%s\t=\t0x%04x\n",
916 SPEC_ADDR (sym->etype));
920 if (options.debug || sym->level == 0)
921 fprintf (code->oFile, " == .\n");
923 /* if it has an initial value */
928 fprintf (code->oFile, "%s:\n", sym->rname);
930 resolveIvalSym (sym->ival, sym->type);
931 //printIval (sym, sym->type, sym->ival, code->oFile);
932 pb = newpCodeChain(NULL, 'P',newpCodeCharP("; Starting pCode block for Ival"));
934 addpCode2pBlock(pb,newpCodeLabel(sym->rname,-1));
936 printIval (sym, sym->type, sym->ival, pb);
943 fprintf (code->oFile, "%s:\n", sym->rname);
944 /* special case for character strings */
945 if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) &&
946 SPEC_CVAL (sym->etype).v_char)
947 pCodeConstString(sym->rname , SPEC_CVAL (sym->etype).v_char);
948 /*printChar (code->oFile,
949 SPEC_CVAL (sym->etype).v_char,
950 strlen (SPEC_CVAL (sym->etype).v_char) + 1);*/
952 fprintf (code->oFile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff);
960 /*-----------------------------------------------------------------*/
961 /* emitMaps - emits the code for the data portion the code */
962 /*-----------------------------------------------------------------*/
966 pic14_constructAbsMap (sfr->oFile);
967 /* no special considerations for the following
968 data, idata & bit & xdata */
969 pic14emitRegularMap (data, TRUE, TRUE);
970 pic14emitRegularMap (idata, TRUE, TRUE);
971 pic14emitRegularMap (bit, TRUE, FALSE);
972 pic14emitRegularMap (xdata, TRUE, TRUE);
973 pic14emitRegularMap (sfr, TRUE, FALSE);
974 pic14emitRegularMap (sfrbit, FALSE, FALSE);
975 pic14emitRegularMap (code, TRUE, FALSE);
976 pic14emitStaticSeg (statsg);
979 /*-----------------------------------------------------------------*/
980 /* createInterruptVect - creates the interrupt vector */
981 /*-----------------------------------------------------------------*/
983 pic14createInterruptVect (FILE * vFile)
985 mainf = newSymbol ("main", 0);
988 /* only if the main function exists */
989 if (!(mainf = findSymWithLevel (SymbolTab, mainf)))
991 struct options *op = &options;
992 if (!(op->cc_only || noAssemble))
993 // werror (E_NO_MAIN);
994 fprintf(stderr,"WARNING: function 'main' undefined\n");
998 /* if the main is only a prototype ie. no body then do nothing */
999 if (!IFFUNC_HASBODY(mainf->type))
1001 /* if ! compile only then main function should be present */
1002 if (!(options.cc_only || noAssemble))
1003 // werror (E_NO_MAIN);
1004 fprintf(stderr,"WARNING: function 'main' undefined\n");
1008 fprintf (vFile, "%s", iComments2);
1009 fprintf (vFile, "; reset vector \n");
1010 fprintf (vFile, "%s", iComments2);
1011 fprintf (vFile, "STARTUP\t%s\n", CODE_NAME); // Lkr file should place section STARTUP at address 0x0
1012 fprintf (vFile, "\tnop\n"); /* first location for used by incircuit debugger */
1013 fprintf( vFile, "\tpagesel __sdcc_gsinit_startup\n");
1014 fprintf (vFile, "\tgoto\t__sdcc_gsinit_startup\n");
1018 /*-----------------------------------------------------------------*/
1019 /* initialComments - puts in some initial comments */
1020 /*-----------------------------------------------------------------*/
1022 pic14initialComments (FILE * afile)
1024 initialComments (afile);
1025 fprintf (afile, "; PIC port for the 14-bit core\n");
1026 fprintf (afile, iComments2);
1031 pic14_stringInSet(const char *str, set **world, int autoAdd)
1038 for (s = setFirstItem(*world); s; s = setNextItem(*world))
1041 if (0 == strcmp(s, str)) return 1;
1045 if (autoAdd) addSet(world, Safe_strdup(str));
1050 pic14_emitSymbolIfNew(FILE *file, const char *fmt, const char *sym, int checkLocals)
1052 static set *emitted = NULL;
1054 if (!pic14_stringInSet(sym, &emitted, 1)) {
1055 /* sym was not in emittedSymbols */
1056 if (!checkLocals || !pic14_stringInSet(sym, &pic14_localFunctions, 0)) {
1057 /* sym is not a locally defined function---avoid bug #1443651 */
1058 fprintf( file, fmt, sym );
1065 /*-----------------------------------------------------------------*/
1066 /* printExterns - generates extern for external variables */
1067 /*-----------------------------------------------------------------*/
1069 pic14printExterns (FILE * afile)
1073 fprintf (afile, "%s", iComments2);
1074 fprintf (afile, "; extern variables in this module\n");
1075 fprintf (afile, "%s", iComments2);
1077 for (sym = setFirstItem (externs); sym; sym = setNextItem (externs))
1078 pic14_emitSymbolIfNew(afile, "\textern %s\n", sym->rname, 1);
1081 /*-----------------------------------------------------------------*/
1082 /* printPublics - generates .global for publics */
1083 /*-----------------------------------------------------------------*/
1085 pic14printPublics (FILE * afile)
1089 fprintf (afile, "%s", iComments2);
1090 fprintf (afile, "; publics variables in this module\n");
1091 fprintf (afile, "%s", iComments2);
1093 for (sym = setFirstItem (publics); sym;
1094 sym = setNextItem (publics)) {
1096 if(!IS_BITFIELD(sym->type) && ((IS_FUNC(sym->type) || sym->allocreq))) {
1097 if (!IS_BITVAR(sym->type))
1098 pic14_emitSymbolIfNew(afile, "\tglobal %s\n", sym->rname, 0);
1100 /* Absolute variables are defines in the asm file as equates and thus can not be made global. */
1101 /* Not any longer! */
1102 //if (!SPEC_ABSA (sym->etype))
1103 pic14_emitSymbolIfNew(afile, "\tglobal %s\n", sym->rname, 0);
1108 /*-----------------------------------------------------------------*/
1109 /* emitOverlay - will emit code for the overlay stuff */
1110 /*-----------------------------------------------------------------*/
1112 pic14emitOverlay (FILE * afile)
1116 /* if (!elementsInSet (ovrSetSets))*/
1118 /* the hack below, fixes translates for devices which
1119 * only have udata_shr memory */
1120 fprintf (afile, "%s\t%s\n",
1121 (elementsInSet(ovrSetSets)?"":";"),
1122 port->mem.overlay_name);
1124 /* for each of the sets in the overlay segment do */
1125 for (ovrset = setFirstItem (ovrSetSets); ovrset;
1126 ovrset = setNextItem (ovrSetSets))
1131 if (elementsInSet (ovrset))
1133 /* this dummy area is used to fool the assembler
1134 otherwise the assembler will append each of these
1135 declarations into one chunk and will not overlay
1138 /* I don't think this applies to us. We are using gpasm. CRF */
1140 fprintf (afile, ";\t.area _DUMMY\n");
1141 /* output the area informtion */
1142 fprintf (afile, ";\t.area\t%s\n", port->mem.overlay_name); /* MOF */
1145 for (sym = setFirstItem (ovrset); sym;
1146 sym = setNextItem (ovrset))
1149 /* if extern then do nothing */
1150 if (IS_EXTERN (sym->etype))
1153 /* if allocation required check is needed
1154 then check if the symbol really requires
1155 allocation only for local variables */
1156 if (!IS_AGGREGATE (sym->type) &&
1157 !(sym->_isparm && !IS_REGPARM (sym->etype))
1158 && !sym->allocreq && sym->level)
1161 /* if global variable & not static or extern
1162 and addPublics allowed then add it to the public set */
1163 if ((sym->_isparm && !IS_REGPARM (sym->etype))
1164 && !IS_STATIC (sym->etype))
1165 addSetHead (&publics, sym);
1167 /* if extern then do nothing or is a function
1169 if (IS_FUNC (sym->type))
1172 /* print extra debug info if required */
1173 if (options.debug || sym->level == 0)
1177 if (IS_STATIC (sym->etype))
1178 fprintf (afile, "F%s_", moduleName); /* scope is file */
1180 fprintf (afile, "G_"); /* scope is global */
1183 /* symbol is local */
1184 fprintf (afile, "L%s_",
1185 (sym->localof ? sym->localof->name : "-null-"));
1186 fprintf (afile, "%s_%d_%d", sym->name, sym->level, sym->block);
1189 /* if is has an absolute address then generate
1190 an equate for this no need to allocate space */
1191 if (SPEC_ABSA (sym->etype))
1194 if (options.debug || sym->level == 0)
1195 fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1197 fprintf (afile, "%s\t=\t0x%04x\n",
1199 SPEC_ADDR (sym->etype));
1203 if (options.debug || sym->level == 0)
1204 fprintf (afile, "==.\n");
1206 /* allocate space */
1207 fprintf (afile, "%s:\n", sym->rname);
1208 fprintf (afile, "\t.ds\t0x%04x\n", (unsigned int) getSize (sym->type) & 0xffff);
1217 pic14_emitInterruptHandler (FILE * asmFile)
1219 if (pic14_hasInterrupt)
1222 fprintf (asmFile, "%s", iComments2);
1223 fprintf (asmFile, "; interrupt and initialization code\n");
1224 fprintf (asmFile, "%s", iComments2);
1225 // Note - for mplink may have to enlarge section vectors in .lnk file
1226 // Note: Do NOT name this code_interrupt to avoid nameclashes with
1227 // source files's code segment (interrupt.c -> code_interrupt)
1228 fprintf (asmFile, "c_interrupt\t%s\t0x4\n", CODE_NAME);
1230 /* interrupt service routine */
1231 fprintf (asmFile, "__sdcc_interrupt\n");
1232 copypCode(asmFile, 'I');
1236 /*-----------------------------------------------------------------*/
1237 /* glue - the final glue that hold the whole thing together */
1238 /*-----------------------------------------------------------------*/
1242 char udata_name[80];
1245 FILE *ovrFile = tempfile();
1247 addSetHead(&tmpfileSet,ovrFile);
1248 pCodeInitRegisters();
1250 /* check for main() */
1251 mainf = newSymbol ("main", 0);
1253 mainf = findSymWithLevel (SymbolTab, mainf);
1255 if (!mainf || !IFFUNC_HASBODY(mainf->type))
1257 /* main missing -- import stack from main module */
1258 //fprintf (stderr, "main() missing -- assuming we are NOT the main module\n");
1259 pic14_options.isLibrarySource = 1;
1263 if (mainf && IFFUNC_HASBODY(mainf->type)) {
1265 pBlock *pb = newpCodeChain(NULL,'X',newpCodeCharP("; Starting pCode block"));
1268 /* entry point @ start of CSEG */
1269 addpCode2pBlock(pb,newpCodeLabel("__sdcc_program_startup",-1));
1270 /* put in the call to main */
1271 addpCode2pBlock(pb,newpCode(POC_CALL,newpCodeOp("_main",PO_STR)));
1273 if (options.mainreturn) {
1275 addpCode2pBlock(pb,newpCodeCharP(";\treturn from main will return to caller\n"));
1276 addpCode2pBlock(pb,newpCode(POC_RETURN,NULL));
1280 addpCode2pBlock(pb,newpCodeCharP(";\treturn from main will lock up\n"));
1281 addpCode2pBlock(pb,newpCode(POC_GOTO,newpCodeOp("$",PO_STR)));
1287 /* At this point we've got all the code in the form of pCode structures */
1288 /* Now it needs to be rearranged into the order it should be placed in the */
1291 movepBlock2Head('P'); // Last
1292 movepBlock2Head(code->dbName);
1293 movepBlock2Head('X');
1294 movepBlock2Head(statsg->dbName); // First
1297 /* print the global struct definitions */
1303 addSetHead(&tmpfileSet,vFile);
1305 /* emit code for the all the variables declared */
1307 /* do the overlay segments */
1308 pic14emitOverlay(ovrFile);
1310 /* PENDING: this isnt the best place but it will do */
1311 if (port->general.glue_up_main) {
1312 /* create the interrupt vector table */
1313 pic14createInterruptVect (vFile);
1318 ReuseReg(); // ReuseReg where call tree permits
1327 /* now put it all together into the assembler file */
1328 /* create the assembler file name */
1330 if ((noAssemble || options.c1mode) && fullDstFileName)
1332 sprintf (buffer, fullDstFileName);
1336 sprintf (buffer, dstFileName);
1337 strcat (buffer, ".asm");
1340 if (!(asmFile = fopen (buffer, "w"))) {
1341 werror (E_FILE_OPEN_ERR, buffer);
1345 /* prepare statistics */
1346 resetpCodeStatistics ();
1348 /* initial comments */
1349 pic14initialComments (asmFile);
1351 /* print module name */
1352 fprintf (asmFile, ";\t.module %s\n", moduleName);
1354 /* Let the port generate any global directives, etc. */
1355 if (port->genAssemblerPreamble)
1357 port->genAssemblerPreamble(asmFile);
1360 /* Emit the __config directive */
1361 pic14_emitConfigWord (asmFile);
1363 /* print the global variables in this module */
1364 pic14printPublics (asmFile);
1366 /* print the extern variables in this module */
1367 pic14printExterns (asmFile);
1369 /* copy the sfr segment */
1370 fprintf (asmFile, "%s", iComments2);
1371 fprintf (asmFile, "; special function registers\n");
1372 fprintf (asmFile, "%s", iComments2);
1373 copyFile (asmFile, sfr->oFile);
1376 if (udata_section_name) {
1377 sprintf(udata_name,"%s",udata_section_name);
1379 sprintf(udata_name,"data_%s",moduleName);
1381 fprintf (asmFile, "%s", iComments2);
1382 fprintf (asmFile, "; udata\n");
1383 fprintf (asmFile, "%s", iComments2);
1384 fprintf (asmFile, "%s\tudata\n", udata_name);
1385 copyFile (asmFile, data->oFile);
1387 /* Put all variables into a cblock */
1389 writeUsedRegs(asmFile);
1391 /* create the overlay segments */
1392 fprintf (asmFile, "%s", iComments2);
1393 fprintf (asmFile, "; overlayable items in internal ram \n");
1394 fprintf (asmFile, "%s", iComments2);
1395 copyFile (asmFile, ovrFile);
1399 /* create the stack segment MOF */
1400 if (mainf && IFFUNC_HASBODY(mainf->type)) {
1401 fprintf (asmFile, "%s", iComments2);
1402 fprintf (asmFile, "; Stack segment in internal ram \n");
1403 fprintf (asmFile, "%s", iComments2);
1404 fprintf (asmFile, ";\t.area\tSSEG\t(DATA)\n"
1405 ";__start__stack:\n;\t.ds\t1\n\n");
1408 /* create the idata segment */
1409 fprintf (asmFile, "%s", iComments2);
1410 fprintf (asmFile, "; indirectly addressable internal ram data\n");
1411 fprintf (asmFile, "%s", iComments2);
1412 copyFile (asmFile, idata->oFile);
1414 /* if external stack then reserve space of it */
1415 if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack ) {
1416 fprintf (asmFile, "%s", iComments2);
1417 fprintf (asmFile, "; external stack \n");
1418 fprintf (asmFile, "%s", iComments2);
1419 fprintf (asmFile,";\t.area XSEG (XDATA)\n"); /* MOF */
1420 fprintf (asmFile,";\t.ds 256\n");
1423 /* copy xtern ram data */
1424 fprintf (asmFile, "%s", iComments2);
1425 fprintf (asmFile, "; external ram data\n");
1426 fprintf (asmFile, "%s", iComments2);
1427 copyFile (asmFile, xdata->oFile);
1431 /* copy the bit segment */
1432 fprintf (asmFile, "%s", iComments2);
1433 fprintf (asmFile, "; bit data\n");
1434 fprintf (asmFile, "%s", iComments2);
1435 copyFile (asmFile, bit->oFile);
1437 /* copy the interrupt vector table */
1438 if (mainf && IFFUNC_HASBODY(mainf->type)) {
1439 copyFile (asmFile, vFile);
1442 /* create interupt ventor handler */
1443 pic14_emitInterruptHandler (asmFile);
1445 if (mainf && IFFUNC_HASBODY(mainf->type)) {
1446 /* initialize data memory */
1447 fprintf (asmFile, "code_init\t%s\n", CODE_NAME); // Note - for mplink may have to enlarge section vectors in .lnk file
1448 fprintf (asmFile,"__sdcc_gsinit_startup\n");
1449 /* FIXME: This is temporary. The idata section should be used. If
1450 not, we could add a special feature to the linker. This will
1451 work in the mean time. Put all initalized data in main.c */
1452 copypCode(asmFile, statsg->dbName);
1453 fprintf (asmFile,"\tpagesel _main\n");
1454 fprintf (asmFile,"\tgoto _main\n");
1459 /* copy global & static initialisations */
1460 fprintf (asmFile, "%s", iComments2);
1461 fprintf (asmFile, "; global & static initialisations\n");
1462 fprintf (asmFile, "%s", iComments2);
1463 copypCode(asmFile, statsg->dbName);
1467 /* copy over code */
1468 fprintf (asmFile, "%s", iComments2);
1469 fprintf (asmFile, "; code\n");
1470 fprintf (asmFile, "%s", iComments2);
1471 fprintf (asmFile, "code_%s\t%s\n", moduleName, port->mem.code_name);
1474 copypCode(asmFile, 'X');
1476 /* _main function */
1477 copypCode(asmFile, 'M');
1479 /* other functions */
1480 copypCode(asmFile, code->dbName);
1483 copypCode(asmFile, 'P');
1485 dumppCodeStatistics (asmFile);
1487 fprintf (asmFile,"\tend\n");