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 -------------------------------------------------------------------------*/
30 #if !defined(__BORLANDC__) && !defined(_MSC_VER)
34 symbol *interrupts[256];
36 void printIval (symbol *, sym_link *, initList *, FILE *);
37 set *publics = NULL; /* public variables */
38 set *externs = NULL; /* Varibles that are declared as extern */
40 /* TODO: this should be configurable (DS803C90 uses more than 6) */
41 unsigned maxInterrupts = 6;
44 extern char *VersionString;
45 set *tmpfileSet = NULL; /* set of tmp file created by the compiler */
46 set *tmpfileNameSet = NULL; /* All are unlinked at close. */
48 /*-----------------------------------------------------------------*/
49 /* closeTmpFiles - closes all tmp files created by the compiler */
50 /* because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
51 /*-----------------------------------------------------------------*/
52 DEFSETFUNC (closeTmpFiles)
62 /*-----------------------------------------------------------------*/
63 /* rmTmpFiles - closes all tmp files created by the compiler */
64 /* because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
65 /*-----------------------------------------------------------------*/
66 DEFSETFUNC (rmTmpFiles)
78 /*-----------------------------------------------------------------*/
79 /* copyFile - copies source file to destination file */
80 /*-----------------------------------------------------------------*/
82 copyFile (FILE * dest, FILE * src)
88 if ((ch = fgetc (src)) != EOF)
93 aopLiteralLong (value * val, int offset, int size)
103 // assuming we have been warned before
107 /* if it is a float then it gets tricky */
108 /* otherwise it is fairly simple */
109 if (!IS_FLOAT (val->type)) {
110 unsigned long v = (unsigned long) floatFromVal (val);
115 tsprintf (buffer, "!immedbyte", (unsigned int) v & 0xff);
118 tsprintf (buffer, "!immedword", (unsigned int) v & 0xffff);
121 /* Hmm. Too big for now. */
124 rs = Safe_calloc (1, strlen (buffer) + 1);
125 return strcpy (rs, buffer);
128 /* PENDING: For now size must be 1 */
131 /* it is type float */
132 fl.f = (float) floatFromVal (val);
134 tsprintf (buffer, "!immedbyte", fl.c[3 - offset]);
136 tsprintf (buffer, "!immedbyte", fl.c[offset]);
138 rs = Safe_calloc (1, strlen (buffer) + 1);
139 return strcpy (rs, buffer);
142 /*-----------------------------------------------------------------*/
143 /* aopLiteral - string from a literal value */
144 /*-----------------------------------------------------------------*/
146 aopLiteral (value * val, int offset)
148 return aopLiteralLong (val, offset, 1);
151 /*-----------------------------------------------------------------*/
152 /* emitRegularMap - emit code for maps with no special cases */
153 /*-----------------------------------------------------------------*/
155 emitRegularMap (memmap * map, bool addPublics, bool arFlag)
165 /* PENDING: special case here - should remove */
166 if (!strcmp (map->sname, CODE_NAME))
167 tfprintf (map->oFile, "\t!areacode\n", map->sname);
168 else if (!strcmp (map->sname, DATA_NAME))
169 tfprintf (map->oFile, "\t!areadata\n", map->sname);
170 else if (!strcmp (map->sname, HOME_NAME))
171 tfprintf (map->oFile, "\t!areahome\n", map->sname);
173 tfprintf (map->oFile, "\t!area\n", map->sname);
176 for (sym = setFirstItem (map->syms); sym;
177 sym = setNextItem (map->syms))
181 /* if extern then add it into the extern list */
182 if (IS_EXTERN (sym->etype))
184 addSetHead (&externs, sym);
188 /* if allocation required check is needed
189 then check if the symbol really requires
190 allocation only for local variables */
192 if (arFlag && !IS_AGGREGATE (sym->type) &&
193 !(sym->_isparm && !IS_REGPARM (sym->etype)) &&
194 !sym->allocreq && sym->level)
197 /* for bitvar locals and parameters */
198 if (!arFlag && !sym->allocreq && sym->level
199 && !SPEC_ABSA (sym->etype)) {
203 /* if global variable & not static or extern
204 and addPublics allowed then add it to the public set */
205 if ((sym->level == 0 ||
206 (sym->_isparm && !IS_REGPARM (sym->etype))) &&
208 !IS_STATIC (sym->etype) &&
209 (IS_FUNC(sym->type) ? (sym->used || IFFUNC_HASBODY(sym->type)) : 1))
211 addSetHead (&publics, sym);
214 /* if extern then do nothing or is a function
216 if (IS_FUNC (sym->type))
219 /* print extra debug info if required */
221 cdbSymbol (sym, cdbFile, FALSE, FALSE);
222 if (!sym->level) /* global */
223 if (IS_STATIC (sym->etype))
224 fprintf (map->oFile, "F%s$", moduleName); /* scope is file */
226 fprintf (map->oFile, "G$"); /* scope is global */
228 /* symbol is local */
229 fprintf (map->oFile, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
230 fprintf (map->oFile, "%s$%d$%d", sym->name, sym->level, sym->block);
233 /* if it has an initial value then do it only if
234 it is a global variable */
235 if (sym->ival && sym->level == 0) {
236 if (SPEC_OCLS(sym->etype)==xidata) {
237 // create a new "XINIT (CODE)" symbol, that will be emitted later
238 newSym=copySymbol (sym);
239 SPEC_OCLS(newSym->etype)=xinit;
240 sprintf (newSym->name, "__xinit_%s", sym->name);
241 sprintf (newSym->rname,"__xinit_%s", sym->rname);
242 SPEC_CONST(newSym->etype)=1;
243 SPEC_STAT(newSym->etype)=1;
244 resolveIvalSym(newSym->ival);
246 // add it to the "XINIT (CODE)" segment
247 addSet(&xinit->syms, newSym);
250 if (IS_AGGREGATE (sym->type)) {
251 ival = initAggregates (sym, sym->ival, NULL);
253 if (getNelements(sym->type, sym->ival)>1) {
254 werror (W_EXCESS_INITIALIZERS, "scalar",
255 sym->name, sym->lineDef);
257 ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
258 decorateType (resolveSymbols (list2expr (sym->ival))));
260 codeOutFile = statsg->oFile;
263 // set ival's lineno to where the symbol was defined
264 setAstLineno (ival, lineno=sym->lineDef);
265 // check if this is not a constant expression
266 if (!constExprTree(ival)) {
267 werror (E_CONST_EXPECTED, "found expression");
268 // but try to do it anyway
271 eBBlockFromiCode (iCodeFromAst (ival));
278 /* if is has an absolute address then generate
279 an equate for this no need to allocate space */
280 if (SPEC_ABSA (sym->etype))
284 fprintf (map->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
286 if (TARGET_IS_XA51) {
289 } else if (map==bit || map==sfrbit) {
293 fprintf (map->oFile, "%s\t%s\t0x%04x\n",
295 SPEC_ADDR (sym->etype));
298 int size = getSize (sym->type);
300 werror(E_UNKNOWN_SIZE,sym->name);
304 fprintf (map->oFile, "==.\n");
306 if (IS_STATIC (sym->etype))
307 tfprintf (map->oFile, "!slabeldef\n", sym->rname);
309 tfprintf (map->oFile, "!labeldef\n", sym->rname);
310 tfprintf (map->oFile, "\t!ds\n",
311 (unsigned int) size & 0xffff);
316 /*-----------------------------------------------------------------*/
317 /* initPointer - pointer initialization code massaging */
318 /*-----------------------------------------------------------------*/
320 initPointer (initList * ilist, sym_link *toType)
323 ast *expr = list2expr (ilist);
328 /* try it the oldway first */
329 if ((val = constExprValue (expr, FALSE)))
332 /* ( ptr + constant ) */
333 if (IS_AST_OP (expr) &&
334 (expr->opval.op == '+' || expr->opval.op == '-') &&
335 IS_AST_SYM_VALUE (expr->left) &&
336 (IS_ARRAY(expr->left->ftype) || IS_PTR(expr->left->ftype)) &&
337 compareType(toType, expr->left->ftype) &&
338 IS_AST_LIT_VALUE (expr->right)) {
339 return valForCastAggr (expr->left, expr->left->ftype,
345 if (IS_AST_OP(expr) && expr->opval.op==CAST &&
346 IS_AST_OP(expr->right) && expr->right->opval.op=='&') {
347 if (compareType(toType, expr->left->ftype)!=1) {
348 werror (W_INIT_WRONG);
349 printFromToType(expr->left->ftype, toType);
355 /* no then we have to do these cludgy checks */
356 /* pointers can be initialized with address of
357 a variable or address of an array element */
358 if (IS_AST_OP (expr) && expr->opval.op == '&') {
359 /* address of symbol */
360 if (IS_AST_SYM_VALUE (expr->left)) {
361 val = copyValue (AST_VALUE (expr->left));
362 val->type = newLink ();
363 if (SPEC_SCLS (expr->left->etype) == S_CODE) {
364 DCL_TYPE (val->type) = CPOINTER;
365 DCL_PTR_CONST (val->type) = port->mem.code_ro;
367 else if (SPEC_SCLS (expr->left->etype) == S_XDATA)
368 DCL_TYPE (val->type) = FPOINTER;
369 else if (SPEC_SCLS (expr->left->etype) == S_XSTACK)
370 DCL_TYPE (val->type) = PPOINTER;
371 else if (SPEC_SCLS (expr->left->etype) == S_IDATA)
372 DCL_TYPE (val->type) = IPOINTER;
373 else if (SPEC_SCLS (expr->left->etype) == S_EEPROM)
374 DCL_TYPE (val->type) = EEPPOINTER;
376 DCL_TYPE (val->type) = POINTER;
377 val->type->next = expr->left->ftype;
378 val->etype = getSpec (val->type);
382 /* if address of indexed array */
383 if (IS_AST_OP (expr->left) && expr->left->opval.op == '[')
384 return valForArray (expr->left);
386 /* if address of structure element then
388 if (IS_AST_OP (expr->left) &&
389 expr->left->opval.op == '.') {
390 return valForStructElem (expr->left->left,
395 (&some_struct)->element */
396 if (IS_AST_OP (expr->left) &&
397 expr->left->opval.op == PTR_OP &&
398 IS_ADDRESS_OF_OP (expr->left->left)) {
399 return valForStructElem (expr->left->left->left,
403 /* case 3. (((char *) &a) +/- constant) */
404 if (IS_AST_OP (expr) &&
405 (expr->opval.op == '+' || expr->opval.op == '-') &&
406 IS_AST_OP (expr->left) && expr->left->opval.op == CAST &&
407 IS_AST_OP (expr->left->right) &&
408 expr->left->right->opval.op == '&' &&
409 IS_AST_LIT_VALUE (expr->right)) {
411 return valForCastAggr (expr->left->right->left,
412 expr->left->left->opval.lnk,
413 expr->right, expr->opval.op);
416 /* case 4. (char *)(array type) */
417 if (IS_CAST_OP(expr) && IS_AST_SYM_VALUE (expr->right) &&
418 IS_ARRAY(expr->right->ftype)) {
420 val = copyValue (AST_VALUE (expr->right));
421 val->type = newLink ();
422 if (SPEC_SCLS (expr->right->etype) == S_CODE) {
423 DCL_TYPE (val->type) = CPOINTER;
424 DCL_PTR_CONST (val->type) = port->mem.code_ro;
426 else if (SPEC_SCLS (expr->right->etype) == S_XDATA)
427 DCL_TYPE (val->type) = FPOINTER;
428 else if (SPEC_SCLS (expr->right->etype) == S_XSTACK)
429 DCL_TYPE (val->type) = PPOINTER;
430 else if (SPEC_SCLS (expr->right->etype) == S_IDATA)
431 DCL_TYPE (val->type) = IPOINTER;
432 else if (SPEC_SCLS (expr->right->etype) == S_EEPROM)
433 DCL_TYPE (val->type) = EEPPOINTER;
435 DCL_TYPE (val->type) = POINTER;
436 val->type->next = expr->right->ftype->next;
437 val->etype = getSpec (val->type);
441 werror (E_INCOMPAT_PTYPES);
446 /*-----------------------------------------------------------------*/
447 /* printChar - formats and prints a characater string with DB */
448 /*-----------------------------------------------------------------*/
450 printChar (FILE * ofile, char *s, int plen)
453 int len = strlen (s);
458 while (len && pplen < plen)
461 while (i && *s && pplen < plen)
463 if (*s < ' ' || *s == '\"' || *s=='\\')
467 tfprintf (ofile, "\t!ascii\n", buf);
468 tfprintf (ofile, "\t!db !constbyte\n", (unsigned char)*s);
483 tfprintf (ofile, "\t!ascii\n", buf);
492 tfprintf (ofile, "\t!db !constbyte\n", 0);
495 /*-----------------------------------------------------------------*/
496 /* return the generic pointer high byte for a given pointer type. */
497 /*-----------------------------------------------------------------*/
499 pointerTypeToGPByte (const int p_type, const char *iname, const char *oname)
507 werror (E_CANNOT_USE_GENERIC_POINTER,
508 iname ? iname : "<null>",
509 oname ? oname : "<null>");
516 return GPTYPE_XSTACK;
518 fprintf (stderr, "*** internal error: unknown pointer type %d in GPByte.\n",
526 /*-----------------------------------------------------------------*/
527 /* printPointerType - generates ival for pointer type */
528 /*-----------------------------------------------------------------*/
530 _printPointerType (FILE * oFile, const char *name)
532 /* if (TARGET_IS_DS390) */
533 if (options.model == MODEL_FLAT24)
535 fprintf (oFile, "\t.byte %s,(%s >> 8),(%s >> 16)", name, name, name);
539 fprintf (oFile, "\t.byte %s,(%s >> 8)", name, name);
543 /*-----------------------------------------------------------------*/
544 /* printPointerType - generates ival for pointer type */
545 /*-----------------------------------------------------------------*/
547 printPointerType (FILE * oFile, const char *name)
549 _printPointerType (oFile, name);
550 fprintf (oFile, "\n");
553 /*-----------------------------------------------------------------*/
554 /* printGPointerType - generates ival for generic pointer type */
555 /*-----------------------------------------------------------------*/
557 printGPointerType (FILE * oFile, const char *iname, const char *oname,
558 const unsigned int type)
560 _printPointerType (oFile, iname);
561 fprintf (oFile, ",#0x%02x\n", pointerTypeToGPByte (type, iname, oname));
564 /*-----------------------------------------------------------------*/
565 /* printIvalType - generates ival for int/char */
566 /*-----------------------------------------------------------------*/
568 printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile)
572 /* if initList is deep */
573 if (ilist->type == INIT_DEEP)
574 ilist = ilist->init.deep;
576 if (!IS_AGGREGATE(sym->type) && getNelements(type, ilist)>1) {
577 werror (W_EXCESS_INITIALIZERS, "scalar", sym->name, sym->lineDef);
580 if (!(val = list2val (ilist))) {
581 // assuming a warning has been thrown
585 if (val->type != type) {
586 val = valCastLiteral(type, floatFromVal(val));
589 switch (getSize (type)) {
592 tfprintf (oFile, "\t!db !constbyte\n", 0);
594 tfprintf (oFile, "\t!dbs\n",
595 aopLiteral (val, 0));
599 if (port->use_dw_for_init)
600 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
602 fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
606 tfprintf (oFile, "\t!dw !constword\n", 0);
607 tfprintf (oFile, "\t!dw !constword\n", 0);
610 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
611 aopLiteral (val, 0), aopLiteral (val, 1),
612 aopLiteral (val, 2), aopLiteral (val, 3));
618 /*-----------------------------------------------------------------*/
619 /* printIvalBitFields - generate initializer for bitfields */
620 /*-----------------------------------------------------------------*/
621 void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile)
625 initList *lilist = *ilist ;
626 unsigned long ival = 0;
632 val = list2val(lilist);
634 if (SPEC_BLEN(lsym->etype) > 8) {
635 size += ((SPEC_BLEN (lsym->etype) / 8) +
636 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
639 size = ((SPEC_BLEN (lsym->etype) / 8) +
640 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
642 i = (unsigned long)floatFromVal(val);
643 i <<= SPEC_BSTR (lsym->etype);
645 if (! ( lsym->next &&
646 (IS_BITFIELD(lsym->next->type)) &&
647 (SPEC_BSTR(lsym->next->etype)))) break;
649 lilist = lilist->next;
653 tfprintf (oFile, "\t!db !constbyte\n",ival);
657 tfprintf (oFile, "\t!dw !constword\n",ival);
660 tfprintf (oFile, "\t!db !constword,!constword\n",
661 (ival >> 8) & 0xffff, (ival & 0xffff));
668 /*-----------------------------------------------------------------*/
669 /* printIvalStruct - generates initial value for structures */
670 /*-----------------------------------------------------------------*/
672 printIvalStruct (symbol * sym, sym_link * type,
673 initList * ilist, FILE * oFile)
678 sflds = SPEC_STRUCT (type)->fields;
679 if (ilist->type != INIT_DEEP) {
680 werror (E_INIT_STRUCT, sym->name);
684 iloop = ilist->init.deep;
686 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
687 if (IS_BITFIELD(sflds->type)) {
688 printIvalBitFields(&sflds,&iloop,oFile);
690 printIval (sym, sflds->type, iloop, oFile);
694 werror (W_EXCESS_INITIALIZERS, "struct", sym->name, sym->lineDef);
699 /*-----------------------------------------------------------------*/
700 /* printIvalChar - generates initital value for character array */
701 /*-----------------------------------------------------------------*/
703 printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
711 val = list2val (ilist);
712 /* if the value is a character string */
713 if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
715 if (!DCL_ELEM (type))
716 DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
718 printChar (oFile, SPEC_CVAL (val->etype).v_char, DCL_ELEM (type));
720 if ((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0)
722 tfprintf (oFile, "\t!db !constbyte\n", 0);
730 printChar (oFile, s, strlen (s) + 1);
734 /*-----------------------------------------------------------------*/
735 /* printIvalArray - generates code for array initialization */
736 /*-----------------------------------------------------------------*/
738 printIvalArray (symbol * sym, sym_link * type, initList * ilist,
742 int lcnt = 0, size = 0;
744 /* take care of the special case */
745 /* array of characters can be init */
747 if (IS_CHAR (type->next)) {
748 if (!IS_LITERAL(list2val(ilist)->etype)) {
749 werror (E_CONST_EXPECTED);
752 if (printIvalChar (type,
753 (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
754 oFile, SPEC_CVAL (sym->etype).v_char))
757 /* not the special case */
758 if (ilist->type != INIT_DEEP)
760 werror (E_INIT_STRUCT, sym->name);
764 iloop = ilist->init.deep;
765 lcnt = DCL_ELEM (type);
770 printIval (sym, type->next, iloop, oFile);
771 iloop = (iloop ? iloop->next : NULL);
774 /* if not array limits given & we */
775 /* are out of initialisers then */
776 if (!DCL_ELEM (type) && !iloop)
779 /* no of elements given and we */
780 /* have generated for all of them */
782 /* if initializers left */
784 werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
790 /* if we have not been given a size */
791 if (!DCL_ELEM (type))
792 DCL_ELEM (type) = size;
797 /*-----------------------------------------------------------------*/
798 /* printIvalFuncPtr - generate initial value for function pointers */
799 /*-----------------------------------------------------------------*/
801 printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile)
806 val = list2val (ilist);
809 // an error has been thrown allready
813 if (IS_LITERAL(val->etype)) {
814 if (compareType(type,val->etype)==0) {
815 werror (E_INCOMPAT_TYPES);
816 printFromToType (val->type, type);
818 printIvalCharPtr (NULL, type, val, oFile);
822 /* check the types */
823 if ((dLvl = compareType (val->type, type->next)) <= 0)
825 tfprintf (oFile, "\t!dw !constword\n", 0);
829 /* now generate the name */
832 if (port->use_dw_for_init)
834 tfprintf (oFile, "\t!dws\n", val->name);
838 printPointerType (oFile, val->name);
841 else if (port->use_dw_for_init)
843 tfprintf (oFile, "\t!dws\n", val->sym->rname);
847 printPointerType (oFile, val->sym->rname);
853 /*-----------------------------------------------------------------*/
854 /* printIvalCharPtr - generates initial values for character pointers */
855 /*-----------------------------------------------------------------*/
857 printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile)
861 /* PENDING: this is _very_ mcs51 specific, including a magic
863 It's also endin specific.
865 size = getSize (type);
867 if (val->name && strlen (val->name))
869 if (size == 1) /* This appears to be Z80 specific?? */
872 "\t!dbs\n", val->name);
874 else if (size == FPTRSIZE)
876 if (port->use_dw_for_init)
878 tfprintf (oFile, "\t!dws\n", val->name);
882 printPointerType (oFile, val->name);
885 else if (size == GPTRSIZE)
888 if (IS_PTR (val->type)) {
889 type = DCL_TYPE (val->type);
891 type = PTR_TYPE (SPEC_OCLS (val->etype));
893 if (val->sym && val->sym->isstrlit) {
894 // this is a literal string
897 printGPointerType (oFile, val->name, sym->name, type);
901 fprintf (stderr, "*** internal error: unknown size in "
902 "printIvalCharPtr.\n");
907 // these are literals assigned to pointers
911 tfprintf (oFile, "\t!dbs\n", aopLiteral (val, 0));
914 if (port->use_dw_for_init)
915 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, size));
917 tfprintf (oFile, "\t.byte %s,%s\n",
918 aopLiteral (val, 0), aopLiteral (val, 1));
921 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
922 // non-zero mcs51 generic pointer
923 werror (E_LITERAL_GENERIC);
925 fprintf (oFile, "\t.byte %s,%s,%s\n",
928 aopLiteral (val, 2));
931 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
932 // non-zero ds390 generic pointer
933 werror (E_LITERAL_GENERIC);
935 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
939 aopLiteral (val, 3));
946 if (val->sym && val->sym->isstrlit && !isinSet(statsg->syms, val->sym)) {
947 addSet (&statsg->syms, val->sym);
953 /*-----------------------------------------------------------------*/
954 /* printIvalPtr - generates initial value for pointers */
955 /*-----------------------------------------------------------------*/
957 printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
963 if (ilist->type == INIT_DEEP)
964 ilist = ilist->init.deep;
966 /* function pointer */
967 if (IS_FUNC (type->next))
969 printIvalFuncPtr (type, ilist, oFile);
973 if (!(val = initPointer (ilist, type)))
976 /* if character pointer */
977 if (IS_CHAR (type->next))
978 if (printIvalCharPtr (sym, type, val, oFile))
982 if (compareType (type, val->type) == 0) {
983 werror (W_INIT_WRONG);
984 printFromToType (val->type, type);
987 /* if val is literal */
988 if (IS_LITERAL (val->etype))
990 switch (getSize (type))
993 tfprintf (oFile, "\t!db !constbyte\n", (unsigned int) floatFromVal (val) & 0xff);
996 if (port->use_dw_for_init)
997 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
999 tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
1001 case 3: // how about '390??
1002 fprintf (oFile, "\t.byte %s,%s,#0x%d\n",
1003 aopLiteral (val, 0), aopLiteral (val, 1), GPTYPE_CODE);
1009 size = getSize (type);
1011 if (size == 1) /* Z80 specific?? */
1013 tfprintf (oFile, "\t!dbs\n", val->name);
1015 else if (size == FPTRSIZE)
1017 if (port->use_dw_for_init) {
1018 tfprintf (oFile, "\t!dws\n", val->name);
1020 printPointerType (oFile, val->name);
1023 else if (size == GPTRSIZE)
1025 printGPointerType (oFile, val->name, sym->name,
1026 (IS_PTR (val->type) ? DCL_TYPE (val->type) :
1027 PTR_TYPE (SPEC_OCLS (val->etype))));
1032 /*-----------------------------------------------------------------*/
1033 /* printIval - generates code for initial value */
1034 /*-----------------------------------------------------------------*/
1036 printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
1041 /* update line number for error msgs */
1042 lineno=sym->lineDef;
1044 /* if structure then */
1045 if (IS_STRUCT (type))
1047 printIvalStruct (sym, type, ilist, oFile);
1051 /* if this is a pointer */
1054 printIvalPtr (sym, type, ilist, oFile);
1058 /* if this is an array */
1059 if (IS_ARRAY (type))
1061 printIvalArray (sym, type, ilist, oFile);
1065 /* if type is SPECIFIER */
1068 printIvalType (sym, type, ilist, oFile);
1073 /*-----------------------------------------------------------------*/
1074 /* emitStaticSeg - emitcode for the static segment */
1075 /*-----------------------------------------------------------------*/
1077 emitStaticSeg (memmap * map, FILE * out)
1081 /* fprintf(out, "\t.area\t%s\n", map->sname); */
1083 /* for all variables in this segment do */
1084 for (sym = setFirstItem (map->syms); sym;
1085 sym = setNextItem (map->syms))
1088 /* if it is "extern" then do nothing */
1089 if (IS_EXTERN (sym->etype))
1092 /* if it is not static add it to the public
1094 if (!IS_STATIC (sym->etype))
1096 addSetHead (&publics, sym);
1099 /* print extra debug info if required */
1100 if (options.debug) {
1101 cdbSymbol (sym, cdbFile, FALSE, FALSE);
1104 if (IS_STATIC (sym->etype))
1105 fprintf (out, "F%s$", moduleName); /* scope is file */
1107 fprintf (out, "G$"); /* scope is global */
1110 /* symbol is local */
1111 fprintf (out, "L%s$",
1112 (sym->localof ? sym->localof->name : "-null-"));
1113 fprintf (out, "%s$%d$%d", sym->name, sym->level, sym->block);
1116 /* if it has an absolute address */
1117 if (SPEC_ABSA (sym->etype))
1120 fprintf (out, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1122 fprintf (out, "%s\t=\t0x%04x\n",
1124 SPEC_ADDR (sym->etype));
1129 fprintf (out, " == .\n");
1131 /* if it has an initial value */
1134 fprintf (out, "%s:\n", sym->rname);
1136 resolveIvalSym (sym->ival);
1137 printIval (sym, sym->type, sym->ival, out);
1141 /* allocate space */
1142 int size = getSize (sym->type);
1145 werror(E_UNKNOWN_SIZE,sym->name);
1147 fprintf (out, "%s:\n", sym->rname);
1148 /* special case for character strings */
1149 if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) &&
1150 SPEC_CVAL (sym->etype).v_char)
1152 SPEC_CVAL (sym->etype).v_char,
1153 strlen (SPEC_CVAL (sym->etype).v_char) + 1);
1155 tfprintf (out, "\t!ds\n", (unsigned int) size & 0xffff);
1161 /*-----------------------------------------------------------------*/
1162 /* emitMaps - emits the code for the data portion the code */
1163 /*-----------------------------------------------------------------*/
1168 /* no special considerations for the following
1169 data, idata & bit & xdata */
1170 emitRegularMap (data, TRUE, TRUE);
1171 emitRegularMap (idata, TRUE, TRUE);
1172 emitRegularMap (bit, TRUE, FALSE);
1173 emitRegularMap (xdata, TRUE, TRUE);
1174 if (port->genXINIT) {
1175 emitRegularMap (xidata, TRUE, TRUE);
1177 emitRegularMap (sfr, FALSE, FALSE);
1178 emitRegularMap (sfrbit, FALSE, FALSE);
1179 emitRegularMap (home, TRUE, FALSE);
1180 emitRegularMap (code, TRUE, FALSE);
1182 emitStaticSeg (statsg, code->oFile);
1183 if (port->genXINIT) {
1184 tfprintf (code->oFile, "\t!area\n", xinit->sname);
1185 emitStaticSeg (xinit, code->oFile);
1190 /*-----------------------------------------------------------------*/
1191 /* flushStatics - flush all currently defined statics out to file */
1192 /* and delete. Temporary function */
1193 /*-----------------------------------------------------------------*/
1197 emitStaticSeg (statsg, codeOutFile);
1198 statsg->syms = NULL;
1201 /*-----------------------------------------------------------------*/
1202 /* createInterruptVect - creates the interrupt vector */
1203 /*-----------------------------------------------------------------*/
1205 createInterruptVect (FILE * vFile)
1208 mainf = newSymbol ("main", 0);
1211 /* only if the main function exists */
1212 if (!(mainf = findSymWithLevel (SymbolTab, mainf)))
1214 if (!options.cc_only && !noAssemble)
1219 /* if the main is only a prototype ie. no body then do nothing */
1220 if (!IFFUNC_HASBODY(mainf->type))
1222 /* if ! compile only then main function should be present */
1223 if (!options.cc_only && !noAssemble)
1228 tfprintf (vFile, "\t!areacode\n", CODE_NAME);
1229 fprintf (vFile, "__interrupt_vect:\n");
1232 if (!port->genIVT || !(port->genIVT (vFile, interrupts, maxInterrupts)))
1234 /* "generic" interrupt table header (if port doesn't specify one).
1235 * Look suspiciously like 8051 code to me...
1238 fprintf (vFile, "\tljmp\t__sdcc_gsinit_startup\n");
1241 /* now for the other interrupts */
1242 for (; i < maxInterrupts; i++)
1245 fprintf (vFile, "\tljmp\t%s\n\t.ds\t5\n", interrupts[i]->rname);
1247 fprintf (vFile, "\treti\n\t.ds\t7\n");
1254 ";--------------------------------------------------------\n"
1255 "; File Created by SDCC : FreeWare ANSI-C Compiler\n"};
1259 ";--------------------------------------------------------\n"};
1262 /*-----------------------------------------------------------------*/
1263 /* initialComments - puts in some initial comments */
1264 /*-----------------------------------------------------------------*/
1266 initialComments (FILE * afile)
1270 fprintf (afile, "%s", iComments1);
1271 fprintf (afile, "; Version %s %s\n", VersionString, asctime (localtime (&t)));
1272 fprintf (afile, "%s", iComments2);
1275 /*-----------------------------------------------------------------*/
1276 /* printPublics - generates .global for publics */
1277 /*-----------------------------------------------------------------*/
1279 printPublics (FILE * afile)
1283 fprintf (afile, "%s", iComments2);
1284 fprintf (afile, "; Public variables in this module\n");
1285 fprintf (afile, "%s", iComments2);
1287 for (sym = setFirstItem (publics); sym;
1288 sym = setNextItem (publics))
1289 tfprintf (afile, "\t!global\n", sym->rname);
1292 /*-----------------------------------------------------------------*/
1293 /* printExterns - generates .global for externs */
1294 /*-----------------------------------------------------------------*/
1296 printExterns (FILE * afile)
1300 fprintf (afile, "%s", iComments2);
1301 fprintf (afile, "; Externals used\n");
1302 fprintf (afile, "%s", iComments2);
1304 for (sym = setFirstItem (externs); sym;
1305 sym = setNextItem (externs))
1306 tfprintf (afile, "\t!extern\n", sym->rname);
1309 /*-----------------------------------------------------------------*/
1310 /* emitOverlay - will emit code for the overlay stuff */
1311 /*-----------------------------------------------------------------*/
1313 emitOverlay (FILE * afile)
1317 if (!elementsInSet (ovrSetSets))
1318 tfprintf (afile, "\t!area\n", port->mem.overlay_name);
1320 /* for each of the sets in the overlay segment do */
1321 for (ovrset = setFirstItem (ovrSetSets); ovrset;
1322 ovrset = setNextItem (ovrSetSets))
1327 if (elementsInSet (ovrset))
1329 /* output the area informtion */
1330 fprintf (afile, "\t.area\t%s\n", port->mem.overlay_name); /* MOF */
1333 for (sym = setFirstItem (ovrset); sym;
1334 sym = setNextItem (ovrset))
1336 /* if extern then it is in the publics table: do nothing */
1337 if (IS_EXTERN (sym->etype))
1340 /* if allocation required check is needed
1341 then check if the symbol really requires
1342 allocation only for local variables */
1343 if (!IS_AGGREGATE (sym->type) &&
1344 !(sym->_isparm && !IS_REGPARM (sym->etype))
1345 && !sym->allocreq && sym->level)
1348 /* if global variable & not static or extern
1349 and addPublics allowed then add it to the public set */
1350 if ((sym->_isparm && !IS_REGPARM (sym->etype))
1351 && !IS_STATIC (sym->etype))
1353 addSetHead (&publics, sym);
1356 /* if extern then do nothing or is a function
1358 if (IS_FUNC (sym->type))
1361 /* print extra debug info if required */
1364 cdbSymbol (sym, cdbFile, FALSE, FALSE);
1368 if (IS_STATIC (sym->etype))
1369 fprintf (afile, "F%s$", moduleName); /* scope is file */
1371 fprintf (afile, "G$"); /* scope is global */
1374 /* symbol is local */
1375 fprintf (afile, "L%s$",
1376 (sym->localof ? sym->localof->name : "-null-"));
1377 fprintf (afile, "%s$%d$%d", sym->name, sym->level, sym->block);
1380 /* if is has an absolute address then generate
1381 an equate for this no need to allocate space */
1382 if (SPEC_ABSA (sym->etype))
1386 fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1388 fprintf (afile, "%s\t=\t0x%04x\n",
1390 SPEC_ADDR (sym->etype));
1393 int size = getSize(sym->type);
1396 werror(E_UNKNOWN_SIZE,sym->name);
1399 fprintf (afile, "==.\n");
1401 /* allocate space */
1402 tfprintf (afile, "!labeldef\n", sym->rname);
1403 tfprintf (afile, "\t!ds\n", (unsigned int) getSize (sym->type) & 0xffff);
1410 /*-----------------------------------------------------------------*/
1411 /* glue - the final glue that hold the whole thing together */
1412 /*-----------------------------------------------------------------*/
1418 FILE *ovrFile = tempfile ();
1420 addSetHead (&tmpfileSet, ovrFile);
1421 /* print the global struct definitions */
1423 cdbStructBlock (0, cdbFile);
1425 vFile = tempfile ();
1426 /* PENDING: this isnt the best place but it will do */
1427 if (port->general.glue_up_main)
1429 /* create the interrupt vector table */
1430 createInterruptVect (vFile);
1433 addSetHead (&tmpfileSet, vFile);
1435 /* emit code for the all the variables declared */
1437 /* do the overlay segments */
1438 emitOverlay (ovrFile);
1440 /* now put it all together into the assembler file */
1441 /* create the assembler file name */
1443 if (!options.c1mode)
1445 sprintf (scratchFileName, srcFileName);
1446 strcat (scratchFileName, port->assembler.file_ext);
1450 strcpy (scratchFileName, options.out_name);
1453 if (!(asmFile = fopen (scratchFileName, "w")))
1455 werror (E_FILE_OPEN_ERR, scratchFileName);
1459 /* initial comments */
1460 initialComments (asmFile);
1462 /* print module name */
1463 tfprintf (asmFile, "\t!module\n", moduleName);
1464 tfprintf (asmFile, "\t!fileprelude\n");
1466 /* Let the port generate any global directives, etc. */
1467 if (port->genAssemblerPreamble)
1469 port->genAssemblerPreamble (asmFile);
1472 /* print the global variables in this module */
1473 printPublics (asmFile);
1474 if (port->assembler.externGlobal)
1475 printExterns (asmFile);
1477 /* copy the sfr segment */
1478 fprintf (asmFile, "%s", iComments2);
1479 fprintf (asmFile, "; special function registers\n");
1480 fprintf (asmFile, "%s", iComments2);
1481 copyFile (asmFile, sfr->oFile);
1483 /* copy the sbit segment */
1484 fprintf (asmFile, "%s", iComments2);
1485 fprintf (asmFile, "; special function bits \n");
1486 fprintf (asmFile, "%s", iComments2);
1487 copyFile (asmFile, sfrbit->oFile);
1489 /* copy the data segment */
1490 fprintf (asmFile, "%s", iComments2);
1491 fprintf (asmFile, "; internal ram data\n");
1492 fprintf (asmFile, "%s", iComments2);
1493 copyFile (asmFile, data->oFile);
1496 /* create the overlay segments */
1498 fprintf (asmFile, "%s", iComments2);
1499 fprintf (asmFile, "; overlayable items in internal ram \n");
1500 fprintf (asmFile, "%s", iComments2);
1501 copyFile (asmFile, ovrFile);
1504 /* create the stack segment MOF */
1505 if (mainf && IFFUNC_HASBODY(mainf->type))
1507 fprintf (asmFile, "%s", iComments2);
1508 fprintf (asmFile, "; Stack segment in internal ram \n");
1509 fprintf (asmFile, "%s", iComments2);
1510 fprintf (asmFile, "\t.area\tSSEG\t(DATA)\n"
1511 "__start__stack:\n\t.ds\t1\n\n");
1514 /* create the idata segment */
1516 fprintf (asmFile, "%s", iComments2);
1517 fprintf (asmFile, "; indirectly addressable internal ram data\n");
1518 fprintf (asmFile, "%s", iComments2);
1519 copyFile (asmFile, idata->oFile);
1522 /* copy the bit segment */
1523 fprintf (asmFile, "%s", iComments2);
1524 fprintf (asmFile, "; bit data\n");
1525 fprintf (asmFile, "%s", iComments2);
1526 copyFile (asmFile, bit->oFile);
1528 /* if external stack then reserve space of it */
1529 if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack)
1531 fprintf (asmFile, "%s", iComments2);
1532 fprintf (asmFile, "; external stack \n");
1533 fprintf (asmFile, "%s", iComments2);
1534 fprintf (asmFile, "\t.area XSEG (XDATA)\n"); /* MOF */
1535 fprintf (asmFile, "\t.ds 256\n");
1539 /* copy xtern ram data */
1540 fprintf (asmFile, "%s", iComments2);
1541 fprintf (asmFile, "; external ram data\n");
1542 fprintf (asmFile, "%s", iComments2);
1543 copyFile (asmFile, xdata->oFile);
1545 /* copy xternal initialized ram data */
1546 fprintf (asmFile, "%s", iComments2);
1547 fprintf (asmFile, "; external initialized ram data\n");
1548 fprintf (asmFile, "%s", iComments2);
1549 copyFile (asmFile, xidata->oFile);
1551 /* copy the interrupt vector table */
1552 if (mainf && IFFUNC_HASBODY(mainf->type))
1554 fprintf (asmFile, "%s", iComments2);
1555 fprintf (asmFile, "; interrupt vector \n");
1556 fprintf (asmFile, "%s", iComments2);
1557 copyFile (asmFile, vFile);
1560 /* copy global & static initialisations */
1561 fprintf (asmFile, "%s", iComments2);
1562 fprintf (asmFile, "; global & static initialisations\n");
1563 fprintf (asmFile, "%s", iComments2);
1565 /* Everywhere we generate a reference to the static_name area,
1566 * (which is currently only here), we immediately follow it with a
1567 * definition of the post_static_name area. This guarantees that
1568 * the post_static_name area will immediately follow the static_name
1571 tfprintf (asmFile, "\t!area\n", port->mem.static_name); /* MOF */
1572 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1573 tfprintf (asmFile, "\t!area\n", port->mem.static_name);
1575 if (mainf && IFFUNC_HASBODY(mainf->type))
1577 fprintf (asmFile, "__sdcc_gsinit_startup:\n");
1578 /* if external stack is specified then the
1579 higher order byte of the xdatalocation is
1580 going into P2 and the lower order going into
1582 if (options.useXstack)
1584 fprintf (asmFile, "\tmov\tP2,#0x%02x\n",
1585 (((unsigned int) options.xdata_loc) >> 8) & 0xff);
1586 fprintf (asmFile, "\tmov\t_spx,#0x%02x\n",
1587 (unsigned int) options.xdata_loc & 0xff);
1590 /* initialise the stack pointer */
1591 /* if the user specified a value then use it */
1592 if (options.stack_loc)
1593 fprintf (asmFile, "\tmov\tsp,#%d\n", options.stack_loc & 0xff);
1595 /* no: we have to compute it */
1596 if (!options.stackOnData && maxRegBank <= 3)
1597 fprintf (asmFile, "\tmov\tsp,#%d\n", ((maxRegBank + 1) * 8) - 1);
1599 fprintf (asmFile, "\tmov\tsp,#__start__stack\n"); /* MOF */
1601 fprintf (asmFile, "\tlcall\t__sdcc_external_startup\n");
1602 fprintf (asmFile, "\tmov\ta,dpl\n");
1603 fprintf (asmFile, "\tjz\t__sdcc_init_data\n");
1604 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1605 fprintf (asmFile, "__sdcc_init_data:\n");
1607 // if the port can copy the XINIT segment to XISEG
1608 if (port->genXINIT) {
1609 port->genXINIT(asmFile);
1613 copyFile (asmFile, statsg->oFile);
1615 if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type))
1617 /* This code is generated in the post-static area.
1618 * This area is guaranteed to follow the static area
1619 * by the ugly shucking and jiving about 20 lines ago.
1621 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1622 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1628 "%s", iComments2, iComments2);
1629 tfprintf (asmFile, "\t!areahome\n", HOME_NAME);
1630 copyFile (asmFile, home->oFile);
1632 /* copy over code */
1633 fprintf (asmFile, "%s", iComments2);
1634 fprintf (asmFile, "; code\n");
1635 fprintf (asmFile, "%s", iComments2);
1636 tfprintf (asmFile, "\t!areacode\n", CODE_NAME);
1637 if (mainf && IFFUNC_HASBODY(mainf->type))
1640 /* entry point @ start of CSEG */
1641 fprintf (asmFile, "__sdcc_program_startup:\n");
1643 /* put in the call to main */
1644 fprintf (asmFile, "\tlcall\t_main\n");
1645 if (options.mainreturn)
1648 fprintf (asmFile, ";\treturn from main ; will return to caller\n");
1649 fprintf (asmFile, "\tret\n");
1655 fprintf (asmFile, ";\treturn from main will lock up\n");
1656 fprintf (asmFile, "\tsjmp .\n");
1659 copyFile (asmFile, code->oFile);
1661 if (port->genAssemblerEnd) {
1662 port->genAssemblerEnd(asmFile);
1665 applyToSet (tmpfileSet, closeTmpFiles);
1666 applyToSet (tmpfileNameSet, rmTmpFiles);
1669 #if defined (__MINGW32__) || defined (__CYGWIN__) || defined (_MSC_VER)
1673 applyToSet (tmpfileSet, closeTmpFiles);
1674 applyToSet (tmpfileNameSet, rmTmpFiles);
1678 /** Creates a temporary file name a'la tmpnam which avoids the bugs
1679 in cygwin wrt c:\tmp.
1680 Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile().
1685 const char *tmpdir = NULL;
1687 tmpdir = getenv ("TMP");
1688 else if (getenv ("TEMP"))
1689 tmpdir = getenv ("TEMP");
1690 else if (getenv ("TMPDIR"))
1691 tmpdir = getenv ("TMPDIR");
1694 char *name = tempnam (tmpdir, "sdcc");
1700 return tmpnam (NULL);
1703 /** Creates a temporary file a'la tmpfile which avoids the bugs
1704 in cygwin wrt c:\tmp.
1705 Scans, in order: TMP, TEMP, TMPDIR, else uses tmpfile().
1710 const char *tmpdir = NULL;
1712 tmpdir = getenv ("TMP");
1713 else if (getenv ("TEMP"))
1714 tmpdir = getenv ("TEMP");
1715 else if (getenv ("TMPDIR"))
1716 tmpdir = getenv ("TMPDIR");
1719 char *name = Safe_strdup( tempnam (tmpdir, "sdcc"));
1722 FILE *fp = fopen (name, "w+b");
1725 addSetHead (&tmpfileNameSet, name);