1 /*-----------------------------------------------------------------*/
2 /* SDCCmem.c - 8051 memory management routines */
3 /*-----------------------------------------------------------------*/
8 memmap *xstack= NULL ; /* xternal stack data */
9 memmap *istack= NULL; /* internal stack */
10 memmap *code = NULL; /* code segment */
11 memmap *data = NULL; /* internal data upto 128 */
12 memmap *xdata = NULL; /* external data */
13 memmap *idata = NULL; /* internal data upto 256 */
14 memmap *bit = NULL; /* bit addressable space */
15 memmap *statsg= NULL; /* the constant data segment */
16 memmap *sfr = NULL; /* register space */
17 memmap *reg = NULL; /* register space */
18 memmap *sfrbit= NULL; /* sfr bit space */
19 memmap *generic=NULL; /* is a generic pointer */
20 memmap *overlay=NULL; /* overlay segment */
21 memmap *eeprom =NULL; /* eeprom location */
23 /* this is a set of sets each set containing
24 symbols in a single overlay */
25 set *ovrSetSets = NULL;
27 extern set *operKeyReset ;
28 extern set *tmpfileSet ;
29 extern symbol *interrupts[];
31 int fatalError = 0 ;/* fatal error flag */
33 /*-----------------------------------------------------------------*/
34 /* allocMap - allocates a memory map */
35 /*-----------------------------------------------------------------*/
36 memmap *allocMap (char rspace, /* sfr space */
37 char farmap, /* far or near segment */
38 char paged , /* can this segment be paged */
39 char direct, /* directly addressable */
40 char bitaddr, /* bit addressable space*/
41 char codemap, /* this is code space */
42 unsigned sloc, /* starting location */
43 const char *name, /* 2 character name */
44 char dbName , /* debug name */
45 int ptrType /* pointer type for this space */
50 if (!(map = GC_malloc(sizeof(memmap)))) {
51 werror(E_OUT_OF_MEM,__FILE__,sizeof(memmap));
55 memset(map, ZERO, sizeof(memmap));
59 map->direct = direct ;
60 map->bitsp = bitaddr ;
61 map->codesp = codemap ;
64 map->dbName = dbName ;
65 map->ptrType= ptrType;
66 if (!(map->oFile = tempfile())) {
67 werror(E_TMPFILE_FAILED);
70 addSetHead (&tmpfileSet,map->oFile);
75 /*-----------------------------------------------------------------*/
76 /* initMem - allocates and initializes all the segments */
77 /*-----------------------------------------------------------------*/
80 /* allocate all the segments */
81 /* xternal stack segment ;
89 POINTER-TYPE - FPOINTER
91 xstack = allocMap (0, 1, 1, 0, 0, 0, options.xstack_loc, XSTACK_NAME,'A',PPOINTER);
93 /* internal stack segment ;
101 POINTER-TYPE - POINTER
103 istack = allocMap (0, 0, 0, 0, 0, 0,options.stack_loc, ISTACK_NAME,'B',POINTER);
113 POINTER-TYPE - CPOINTER
115 code = allocMap (0, 1, 0, 0, 0, 1, options.code_loc, CODE_NAME,'C',CPOINTER);
117 /* Static segment (code for variables );
125 POINTER-TYPE - CPOINTER
127 statsg = allocMap (0, 1, 0, 0, 0, 1,0, STATIC_NAME,'D',CPOINTER);
129 /* Data segment - internal storage segment ;
137 POINTER-TYPE - POINTER
139 data = allocMap (0, 0, 0, 1, 0, 0, options.data_loc, DATA_NAME,'E',POINTER);
141 /* overlay segment - same as internal storage segment ;
149 POINTER-TYPE - POINTER
151 overlay = allocMap (0, 0, 0, 1, 0, 0, options.data_loc, DATA_NAME,'E',POINTER);
153 /* Xternal Data segment -
161 POINTER-TYPE - FPOINTER
163 xdata = allocMap (0, 1, 0, 0, 0, 0, options.xdata_loc, XDATA_NAME,'F',FPOINTER);
165 /* Inderectly addressed internal data segment
173 POINTER-TYPE - IPOINTER
175 idata = allocMap (0, 0, 0, 0, 0, 0, options.idata_loc,IDATA_NAME,'G',IPOINTER);
177 /* Static segment (code for variables );
185 POINTER-TYPE - _NONE_
187 bit = allocMap (0, 0, 0, 1, 1, 0,0, BIT_NAME,'H',0);
189 /* Special function register space :-
197 POINTER-TYPE - _NONE_
199 sfr = allocMap (1,0, 0, 1, 0, 0,0, REG_NAME,'I',0);
209 POINTER-TYPE - _NONE_
211 reg = allocMap (1,0, 0, 0, 0, 0, 0,REG_NAME,' ',0);
221 POINTER-TYPE - _NONE_
223 sfrbit = allocMap (1,0, 0, 1, 1, 0,0, REG_NAME,'J',0);
233 POINTER-TYPE - EEPPOINTER
235 eeprom = allocMap (0,1, 0, 0, 0, 0,0, REG_NAME,'K',EEPPOINTER);
237 /* the unknown map */
238 generic = allocMap (1,0, 0, 1, 1, 0,0, REG_NAME,' ',GPOINTER);
242 /*-----------------------------------------------------------------*/
243 /* allocIntoSeg - puts a symbol into a memory segment */
244 /*-----------------------------------------------------------------*/
245 void allocIntoSeg (symbol *sym)
247 memmap *segment = SPEC_OCLS(sym->etype);
248 addSet (&segment->syms,sym);
251 /*-----------------------------------------------------------------*/
252 /* allocGlobal - aassigns the output segment to a global var */
253 /*-----------------------------------------------------------------*/
254 void allocGlobal ( symbol *sym )
256 /* symbol name is internal name */
257 sprintf (sym->rname,"%s%s", port->fun_prefix, sym->name);
259 /* add it to the operandKey reset */
260 addSet(&operKeyReset,sym);
262 /* if this is a literal e.g. enumerated type */
263 /* put it in the data segment & do nothing */
264 if (IS_LITERAL(sym->etype)) {
265 SPEC_OCLS(sym->etype) = data ;
269 /* if this is a function then assign code space */
270 if (IS_FUNC(sym->type)) {
271 SPEC_OCLS(sym->etype) = code ;
272 /* if this is an interrupt service routine
273 then put it in the interrupt service array */
274 if (IS_ISR(sym->etype)) {
276 if (interrupts[SPEC_INTN(sym->etype)])
277 werror(E_INT_DEFINED,
278 SPEC_INTN(sym->etype),
279 interrupts[SPEC_INTN(sym->etype)]->name);
281 interrupts[SPEC_INTN(sym->etype)] = sym;
283 /* automagically extend the maximum interrupts */
284 if (SPEC_INTN(sym->etype) >= maxInterrupts )
285 maxInterrupts = SPEC_INTN(sym->etype) + 1;
287 /* if it is not compiler defined */
294 /* if this is a SFR or SBIT */
295 if ( SPEC_SCLS(sym->etype) == S_SFR ||
296 SPEC_SCLS(sym->etype) == S_SBIT ) {
298 /* if both absolute address & initial */
299 /* value specified then error */
300 if ( IS_ABSOLUTE (sym->etype) && sym->ival ) {
301 werror(E_SFR_INIT,sym->name);
305 SPEC_OCLS(sym->etype) =
306 (SPEC_SCLS(sym->etype) == S_SFR ? sfr : sfrbit);
312 /* if this is a bit variable and no storage class */
313 if ( SPEC_NOUN(sym->etype) == V_BIT
314 && SPEC_SCLS(sym->etype) == S_BIT ) {
315 SPEC_OCLS(sym->etype) = bit ;
320 /* if bit storage class */
321 if ( SPEC_SCLS(sym->etype) == S_SBIT ) {
322 SPEC_OCLS(sym->etype) = bit;
327 /* register storage class ignored changed to FIXED */
328 if ( SPEC_SCLS(sym->etype) == S_REGISTER )
329 SPEC_SCLS(sym->etype) = S_FIXED ;
331 /* if data specified then */
332 if (SPEC_SCLS(sym->etype) == S_DATA) {
333 /* set the output class */
334 SPEC_OCLS(sym->etype) = data ;
335 /* generate the symbol */
340 /* if it is fixed, then allocate depending on the */
341 /* current memory model,same for automatics */
342 if ( SPEC_SCLS(sym->etype) == S_FIXED ||
343 SPEC_SCLS(sym->etype) == S_AUTO ) {
344 /* set the output class */
345 SPEC_OCLS(sym->etype) = port->mem.default_globl_map ;
346 /* generate the symbol */
351 /* if code change to constant */
352 if ( SPEC_SCLS(sym->etype) == S_CODE ||
353 SPEC_SCLS(sym->etype) == S_CONSTANT ) {
354 SPEC_OCLS(sym->etype) = statsg ;
359 if ( SPEC_SCLS(sym->etype) == S_XDATA ) {
360 SPEC_OCLS(sym->etype) = xdata ;
365 if ( SPEC_SCLS(sym->etype) == S_IDATA ) {
366 SPEC_OCLS(sym->etype) = idata ;
372 if ( SPEC_SCLS(sym->etype) == S_EEPROM ) {
373 SPEC_OCLS(sym->etype) = eeprom ;
381 /*-----------------------------------------------------------------*/
382 /* allocParms - parameters are always passed on stack */
383 /*-----------------------------------------------------------------*/
384 void allocParms ( value *val )
389 for ( lval = val ; lval ; lval = lval->next, pNum++ ) {
391 /* check the declaration */
392 checkDecl (lval->sym);
394 /* if this a register parm then allocate
395 it as a local variable by adding it
396 to the first block we see in the body */
397 if (IS_REGPARM(lval->etype))
400 /* mark it as my parameter */
401 lval->sym->ismyparm = 1;
402 lval->sym->localof = currFunc;
405 /* if automatic variables r 2b stacked */
406 if ( options.stackAuto || IS_RENT(currFunc->etype)) {
409 lval->sym->onStack = 1;
411 /* choose which stack 2 use */
412 /* use xternal stack */
413 if ( options.useXstack ) {
414 /* PENDING: stack direction support */
415 SPEC_OCLS(lval->etype) = SPEC_OCLS(lval->sym->etype) = xstack ;
416 SPEC_STAK(lval->etype) = SPEC_STAK(lval->sym->etype) = lval->sym->stack =
417 xstackPtr - getSize(lval->type);
418 xstackPtr -= getSize (lval->type);
420 else { /* use internal stack */
421 SPEC_OCLS(lval->etype) = SPEC_OCLS(lval->sym->etype) = istack ;
422 if (port->stack.direction > 0) {
423 SPEC_STAK(lval->etype) = SPEC_STAK(lval->sym->etype) = lval->sym->stack =
424 stackPtr - ( SPEC_BANK(currFunc->etype) ? port->stack.bank_overhead : 0) -
425 getSize(lval->type) -
426 (IS_ISR(currFunc->etype) ? port->stack.isr_overhead : 0);
427 stackPtr -= getSize (lval->type);
430 /* This looks like the wrong order but it turns out OK... */
431 /* PENDING: isr, bank overhead, ... */
432 SPEC_STAK(lval->etype) = SPEC_STAK(lval->sym->etype) = lval->sym->stack = stackPtr;
433 stackPtr += getSize (lval->type);
436 allocIntoSeg(lval->sym);
438 else { /* allocate them in the automatic space */
439 /* generate a unique name */
440 sprintf (lval->sym->rname,"%s%s_PARM_%d", port->fun_prefix, currFunc->name,pNum);
441 strcpy (lval->name,lval->sym->rname);
443 /* if declared in external storage */
444 if (SPEC_SCLS(lval->etype) == S_XDATA)
445 SPEC_OCLS(lval->etype) = SPEC_OCLS(lval->sym->etype) = xdata;
447 /* other wise depending on the memory model
448 note here that we put it into the overlay segment
449 first, we will remove it from the overlay segment
450 after the overlay determination has been done */
451 SPEC_OCLS(lval->etype) = SPEC_OCLS(lval->sym->etype) =
452 ( options.model ? port->mem.default_local_map :
453 (options.noOverlay ? port->mem.default_local_map
456 allocIntoSeg(lval->sym);
463 /*-----------------------------------------------------------------*/
464 /* deallocParms - parameters are always passed on stack */
465 /*-----------------------------------------------------------------*/
466 void deallocParms ( value *val )
470 for ( lval = val ; lval ; lval = lval->next ) {
472 /* unmark is myparm */
473 lval->sym->ismyparm = 0;
474 /* if on stack then depending on which stack */
476 /* delete it from the symbol table */
477 deleteSym (SymbolTab,lval->sym,lval->sym->name);
479 if (!lval->sym->isref) {
480 lval->sym->allocreq = 1;
481 werror(W_NO_REFERENCE,currFunc->name,
482 "function argument",lval->sym->name);
485 /* move the rname if any to the name for both val & sym */
486 /* and leave a copy of it in the symbol table */
487 if (lval->sym->rname[0]) {
488 char buffer[SDCC_NAME_MAX];
489 strcpy(buffer,lval->sym->rname);
490 lval->sym = copySymbol(lval->sym);
491 strcpy(lval->sym->rname,buffer);
492 strcpy(lval->name,strcpy(lval->sym->name,lval->sym->rname));
493 addSym (SymbolTab, lval->sym, lval->sym->name,
494 lval->sym->level,lval->sym->block);
495 lval->sym->_isparm = 1;
496 addSet(&operKeyReset,lval->sym);
504 /*-----------------------------------------------------------------*/
505 /* allocLocal - allocate local variables */
506 /*-----------------------------------------------------------------*/
507 void allocLocal ( symbol *sym )
510 /* generate an unique name */
511 sprintf(sym->rname,"%s%s_%s_%d_%d",
513 currFunc->name,sym->name,sym->level,sym->block);
516 sym->localof = currFunc;
518 /* if this is a static variable */
519 if ( IS_STATIC (sym->etype)) {
520 /* SPEC_OCLS(sym->etype) = (options.model ? xdata : data ); */
521 SPEC_OCLS(sym->etype) = port->mem.default_local_map;
527 /* if volatile then */
528 if (IS_VOLATILE(sym->etype))
531 /* this is automatic */
533 /* if it to be placed on the stack */
534 if ( options.stackAuto || reentrant) {
537 if ( options.useXstack ) {
538 /* PENDING: stack direction for xstack */
539 SPEC_OCLS(sym->etype) = xstack ;
540 SPEC_STAK(sym->etype) = sym->stack = (xstackPtr + 1);
541 xstackPtr += getSize (sym->type) ;
544 SPEC_OCLS(sym->etype) = istack ;
545 if (port->stack.direction > 0) {
546 SPEC_STAK(sym->etype) = sym->stack = ( stackPtr + 1);
547 stackPtr += getSize (sym->type) ;
550 stackPtr -= getSize (sym->type);
551 SPEC_STAK(sym->etype) = sym->stack = stackPtr;
558 /* else depending on the storage class specified */
559 if ( SPEC_SCLS(sym->etype) == S_XDATA ) {
560 SPEC_OCLS(sym->etype) = xdata ;
565 if ( (SPEC_SCLS(sym->etype) == S_CODE ||
566 SPEC_SCLS(sym->etype) == S_CONSTANT) &&
568 SPEC_OCLS(sym->etype) = statsg ;
573 if ( SPEC_SCLS(sym->etype) == S_IDATA ) {
574 SPEC_OCLS(sym->etype) = idata ;
580 /* if this is a function then assign code space */
581 if (IS_FUNC(sym->type)) {
582 SPEC_OCLS(sym->etype) = code ;
586 /* if this is a SFR or SBIT */
587 if ( SPEC_SCLS(sym->etype) == S_SFR ||
588 SPEC_SCLS(sym->etype) == S_SBIT ) {
590 /* if both absolute address & initial */
591 /* value specified then error */
592 if ( IS_ABSOLUTE (sym->etype) && sym->ival ) {
593 werror(E_SFR_INIT,sym->name);
597 SPEC_OCLS(sym->etype) =
598 (SPEC_SCLS(sym->etype) == S_SFR ? sfr : sfrbit);
604 /* if this is a bit variable and no storage class */
605 if ( SPEC_NOUN(sym->etype) == V_BIT
606 && (SPEC_SCLS(sym->etype) == S_BIT)) {
607 SPEC_OCLS(sym->etype) = bit ;
612 if ( SPEC_SCLS(sym->etype) == S_DATA ) {
613 SPEC_OCLS(sym->etype) = (options.noOverlay ? data : overlay );
618 if ( SPEC_SCLS(sym->etype) == S_EEPROM ) {
619 SPEC_OCLS(sym->etype) = eeprom;
624 /* again note that we have put it into the overlay segment
625 will remove and put into the 'data' segment if required after
626 overlay analysis has been done */
627 SPEC_OCLS(sym->etype) = ( options.model ? port->mem.default_local_map :
628 (options.noOverlay ? port->mem.default_local_map
633 /*-----------------------------------------------------------------*/
634 /* deallocLocal - deallocates the local variables */
635 /*-----------------------------------------------------------------*/
636 void deallocLocal ( symbol *csym )
640 for ( sym = csym ; sym ; sym = sym->next) {
644 /* if it is on the stack */
646 if (options.useXstack)
647 xstackPtr -= getSize(sym->type);
649 stackPtr -= getSize(sym->type);
651 /* if not used give a warning */
652 if (!sym->isref && !IS_STATIC(sym->etype))
653 werror(W_NO_REFERENCE,currFunc->name,
654 "local variable",sym->name);
655 /* now delete it from the symbol table */
656 deleteSym (SymbolTab,sym,sym->name);
660 /*-----------------------------------------------------------------*/
661 /* overlay2data - moves declarations from the overlay seg to data */
662 /*-----------------------------------------------------------------*/
667 for (sym = setFirstItem(overlay->syms); sym;
668 sym = setNextItem(overlay->syms)) {
670 SPEC_OCLS(sym->etype) = data;
674 setToNull((void **) &overlay->syms);
678 /*-----------------------------------------------------------------*/
679 /* overlay2Set - will add all symbols from the overlay segment to */
680 /* the set of sets containing the overlable symbols */
681 /*-----------------------------------------------------------------*/
687 for (sym = setFirstItem(overlay->syms); sym;
688 sym = setNextItem(overlay->syms)) {
693 setToNull((void **) &overlay->syms);
694 addSet (&ovrSetSets,oset);
698 /*-----------------------------------------------------------------*/
699 /* allocVariables - creates decl & assign storage class for a v */
700 /*-----------------------------------------------------------------*/
701 int allocVariables ( symbol *symChain )
708 /* go thru the symbol chain */
709 for ( sym = symChain ; sym ; sym = sym->next ) {
711 /* if this is a typedef then add it */
712 /* to the typedef table */
713 if (IS_TYPEDEF(sym->etype)) {
714 /* check if the typedef already exists */
715 csym = findSym (TypedefTab, NULL, sym->name );
716 if ( csym && csym->level == sym->level )
717 werror(E_DUPLICATE_TYPEDEF,sym->name);
719 addSym (TypedefTab, sym , sym->name,sym->level,sym->block);
720 continue ; /* go to the next one */
722 /* make sure it already exist */
723 csym = findSymWithLevel (SymbolTab, sym);
724 if (! csym || (csym && csym->level != sym->level) )
727 /* check the declaration */
730 /* if this is a function or a pointer to function */
731 /* then args processing */
732 if (funcInChain(csym->type)) {
734 processFuncArgs (csym, 1);
735 /* if register bank specified then update maxRegBank */
736 if (maxRegBank < SPEC_BANK(csym->etype))
737 maxRegBank = SPEC_BANK(csym->etype);
740 /* if this is a extern variable then change the */
741 /* level to zero temporarily */
742 if (IS_EXTERN(csym->etype) || IS_FUNC(csym->type) ) {
743 saveLevel = csym->level ;
747 /* if this is a literal then it is an enumerated */
748 /* type so need not allocate it space for it */
749 if (IS_LITERAL(sym->etype))
752 /* generate the actual declaration */
756 stack += getSize(csym->type) ;
761 /* restore the level */
762 if (IS_EXTERN(csym->etype) || IS_FUNC(csym->type))
763 csym->level = saveLevel;
769 /*-----------------------------------------------------------------*/
770 /* redoStackOffsets :- will reassign the values for stack offsets */
771 /*-----------------------------------------------------------------*/
772 void redoStackOffsets ()
778 /* after register allocation is complete we know
779 which variables will need to be assigned space
780 on the stack. We will eliminate those variables
781 which do not have the allocReq flag thus reducing
783 for ( sym = setFirstItem(istack->syms); sym;
784 sym = setNextItem(istack->syms)) {
786 int size = getSize(sym->type);
787 /* nothing to do with parameters so continue */
788 if ((sym->_isparm && !IS_REGPARM(sym->etype)))
791 if ( IS_AGGREGATE(sym->type)) {
792 if (port->stack.direction > 0) {
793 SPEC_STAK(sym->etype) = sym->stack = ( sPtr + 1);
798 SPEC_STAK(sym->etype) = sym->stack = sPtr;
803 /* if allocation not required then subtract
804 size from overall stack size & continue */
805 if (!sym->allocreq) {
806 currFunc->stack -= size;
807 SPEC_STAK(currFunc->etype) -= size;
811 if (port->stack.direction > 0) {
812 SPEC_STAK(sym->etype) = sym->stack = ( sPtr + 1);
817 SPEC_STAK(sym->etype) = sym->stack = sPtr;
821 /* do the same for the external stack */
823 for ( sym = setFirstItem(xstack->syms); sym;
824 sym = setNextItem(xstack->syms)) {
826 int size = getSize(sym->type);
827 /* nothing to do with parameters so continue */
828 if ((sym->_isparm && !IS_REGPARM(sym->etype)))
831 if (IS_AGGREGATE(sym->type)) {
832 SPEC_STAK(sym->etype) = sym->stack = ( xsPtr + 1);
837 /* if allocation not required then subtract
838 size from overall stack size & continue */
839 if (!sym->allocreq) {
840 currFunc->xstack -= size;
841 SPEC_STAK(currFunc->etype) -= size;
845 SPEC_STAK(sym->etype) = sym->stack = ( xsPtr + 1);
849 /* if the debug option is set then output the
850 symbols to the map file */
852 for (sym = setFirstItem(istack->syms); sym;
853 sym = setNextItem(istack->syms))
854 cdbSymbol(sym,cdbFile,FALSE,FALSE);
856 for (sym = setFirstItem(xstack->syms); sym;
857 sym = setNextItem(xstack->syms))
858 cdbSymbol(sym,cdbFile,FALSE,FALSE);
862 /*-----------------------------------------------------------------*/
863 /* printAllocInfoSeg- print the allocation for a given section */
864 /*-----------------------------------------------------------------*/
865 static void printAllocInfoSeg ( memmap *map, symbol *func, FILE *of)
870 if (!map->syms) return;
872 for (sym = setFirstItem(map->syms); sym;
873 sym = setNextItem(map->syms)) {
875 if (sym->level == 0) continue;
876 if (sym->localof != func) continue ;
877 fprintf(of,";%-25s Allocated to ",sym->name);
879 /* if assigned to registers */
880 if (!sym->allocreq && sym->reqv) {
882 sym = OP_SYMBOL(sym->reqv);
883 fprintf(of,"registers ");
884 for (i = 0 ; i < 4 && sym->regs[i] ; i++)
885 fprintf(of,"%s ",port->getRegName(sym->regs[i]));
892 fprintf(of,"stack - offset %d\n",sym->stack);
896 /* otherwise give rname */
897 fprintf(of,"in memory with name '%s'\n",sym->rname);
901 /*-----------------------------------------------------------------*/
902 /* printAllocInfo - prints allocation information for a function */
903 /*-----------------------------------------------------------------*/
904 void printAllocInfo( symbol * func, FILE *of)
906 if (!of) of = stdout;
908 /* must be called after register allocation is complete */
909 fprintf(of,";------------------------------------------------------------\n");
910 fprintf(of,";Allocation info for local variables in function '%s'\n",func->name);
911 fprintf(of,";------------------------------------------------------------\n");
913 printAllocInfoSeg(xstack,func,of);
914 printAllocInfoSeg(istack,func,of);
915 printAllocInfoSeg(code,func,of);
916 printAllocInfoSeg(data,func,of);
917 printAllocInfoSeg(xdata,func,of);
918 printAllocInfoSeg(idata,func,of);
919 printAllocInfoSeg(sfr,func,of);
920 printAllocInfoSeg(sfrbit,func,of);