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 -------------------------------------------------------------------------*/
38 symbol *interrupts[INTNO_MAX+1];
40 void printIval (symbol *, sym_link *, initList *, FILE *);
41 set *publics = NULL; /* public variables */
42 set *externs = NULL; /* Varibles that are declared as extern */
44 /* TODO: this should be configurable (DS803C90 uses more than 6) */
45 unsigned maxInterrupts = 6;
48 set *pipeSet = NULL; /* set of pipes */
49 set *tmpfileSet = NULL; /* set of tmp file created by the compiler */
50 set *tmpfileNameSet = NULL; /* All are unlinked at close. */
52 /*-----------------------------------------------------------------*/
53 /* closePipes - closes all pipes created by the compiler */
54 /*-----------------------------------------------------------------*/
55 DEFSETFUNC (closePipes)
68 /*-----------------------------------------------------------------*/
69 /* closeTmpFiles - closes all tmp files created by the compiler */
70 /* because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
71 /*-----------------------------------------------------------------*/
72 DEFSETFUNC (closeTmpFiles)
85 /*-----------------------------------------------------------------*/
86 /* rmTmpFiles - unlinks all tmp files created by the compiler */
87 /* because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
88 /*-----------------------------------------------------------------*/
89 DEFSETFUNC (rmTmpFiles)
103 /*-----------------------------------------------------------------*/
104 /* rm_tmpfiles - close and remove temporary files and delete sets */
105 /*-----------------------------------------------------------------*/
109 /* close temporary files */
110 applyToSet (pipeSet, closePipes);
111 /* close temporary files */
112 deleteSet (&pipeSet);
114 applyToSet (tmpfileSet, closeTmpFiles);
115 /* remove temporary files */
116 applyToSet (tmpfileNameSet, rmTmpFiles);
117 /* delete temorary file sets */
118 deleteSet (&tmpfileSet);
119 deleteSet (&tmpfileNameSet);
122 /*-----------------------------------------------------------------*/
123 /* copyFile - copies source file to destination file */
124 /*-----------------------------------------------------------------*/
126 copyFile (FILE * dest, FILE * src)
132 if ((ch = fgetc (src)) != EOF)
137 aopLiteralLong (value * val, int offset, int size)
146 // assuming we have been warned before
150 /* if it is a float then it gets tricky */
151 /* otherwise it is fairly simple */
152 if (!IS_FLOAT (val->type)) {
153 unsigned long v = (unsigned long) floatFromVal (val);
158 tsprintf (buffer, sizeof(buffer),
159 "!immedbyte", (unsigned int) v & 0xff);
162 tsprintf (buffer, sizeof(buffer),
163 "!immedword", (unsigned int) v & 0xffff);
166 /* Hmm. Too big for now. */
169 return Safe_strdup (buffer);
172 /* PENDING: For now size must be 1 */
175 /* it is type float */
176 fl.f = (float) floatFromVal (val);
177 #ifdef WORDS_BIGENDIAN
178 tsprintf (buffer, sizeof(buffer),
179 "!immedbyte", fl.c[3 - offset]);
181 tsprintf (buffer, sizeof(buffer),
182 "!immedbyte", fl.c[offset]);
184 return Safe_strdup (buffer);
187 /*-----------------------------------------------------------------*/
188 /* aopLiteral - string from a literal value */
189 /*-----------------------------------------------------------------*/
191 aopLiteral (value * val, int offset)
193 return aopLiteralLong (val, offset, 1);
196 /*-----------------------------------------------------------------*/
197 /* emitRegularMap - emit code for maps with no special cases */
198 /*-----------------------------------------------------------------*/
200 emitRegularMap (memmap * map, bool addPublics, bool arFlag)
210 /* PENDING: special case here - should remove */
211 if (!strcmp (map->sname, CODE_NAME))
212 tfprintf (map->oFile, "\t!areacode\n", map->sname);
213 else if (!strcmp (map->sname, DATA_NAME))
214 tfprintf (map->oFile, "\t!areadata\n", map->sname);
215 else if (!strcmp (map->sname, HOME_NAME))
216 tfprintf (map->oFile, "\t!areahome\n", map->sname);
218 tfprintf (map->oFile, "\t!area\n", map->sname);
221 for (sym = setFirstItem (map->syms); sym;
222 sym = setNextItem (map->syms))
226 /* if extern then add it into the extern list */
227 if (IS_EXTERN (sym->etype))
229 addSetHead (&externs, sym);
233 /* if allocation required check is needed
234 then check if the symbol really requires
235 allocation only for local variables */
237 if (arFlag && !IS_AGGREGATE (sym->type) &&
238 !(sym->_isparm && !IS_REGPARM (sym->etype)) &&
239 !sym->allocreq && sym->level)
242 /* for bitvar locals and parameters */
243 if (!arFlag && !sym->allocreq && sym->level
244 && !SPEC_ABSA (sym->etype)) {
248 /* if global variable & not static or extern
249 and addPublics allowed then add it to the public set */
250 if ((sym->level == 0 ||
251 (sym->_isparm && !IS_REGPARM (sym->etype))) &&
253 !IS_STATIC (sym->etype) &&
254 (IS_FUNC(sym->type) ? (sym->used || IFFUNC_HASBODY(sym->type)) : 1))
256 addSetHead (&publics, sym);
259 /* if extern then do nothing or is a function
261 if (IS_FUNC (sym->type) && !(sym->isitmp))
264 /* print extra debug info if required */
267 if (!sym->level) /* global */
269 if (IS_STATIC (sym->etype))
270 fprintf (map->oFile, "F%s$", moduleName); /* scope is file */
272 fprintf (map->oFile, "G$"); /* scope is global */
276 /* symbol is local */
277 fprintf (map->oFile, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
279 fprintf (map->oFile, "%s$%d$%d", sym->name, sym->level, sym->block);
282 /* if it has an initial value then do it only if
283 it is a global variable */
284 if (sym->ival && sym->level == 0) {
285 if (SPEC_OCLS(sym->etype)==xidata) {
286 /* create a new "XINIT (CODE)" symbol, that will be emitted later
288 newSym=copySymbol (sym);
289 SPEC_OCLS(newSym->etype)=xinit;
290 SNPRINTF (newSym->name, sizeof(newSym->name), "__xinit_%s", sym->name);
291 SNPRINTF (newSym->rname, sizeof(newSym->rname), "__xinit_%s", sym->rname);
292 if (IS_SPEC (newSym->type))
293 SPEC_CONST (newSym->type) = 1;
295 DCL_PTR_CONST (newSym->type) = 1;
296 SPEC_STAT(newSym->etype)=1;
297 resolveIvalSym(newSym->ival, newSym->type);
299 // add it to the "XINIT (CODE)" segment
300 addSet(&xinit->syms, newSym);
302 if (!SPEC_ABSA (sym->etype))
304 FILE *tmpFile = tempfile ();
305 addSetHead (&tmpfileSet, tmpFile);
306 // before allocation we must parse the sym->ival tree
307 // but without actually generating initialization code
309 resolveIvalSym (sym->ival, sym->type);
310 printIval (sym, sym->type, sym->ival, tmpFile);
316 if (IS_AGGREGATE (sym->type)) {
317 ival = initAggregates (sym, sym->ival, NULL);
319 if (getNelements(sym->type, sym->ival)>1) {
320 werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "scalar",
323 ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
324 decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_CHECK));
326 codeOutFile = statsg->oFile;
329 // set ival's lineno to where the symbol was defined
330 setAstLineno (ival, lineno=sym->lineDef);
331 // check if this is not a constant expression
332 if (!constExprTree(ival)) {
333 werror (E_CONST_EXPECTED, "found expression");
334 // but try to do it anyway
337 if (!astErrors(ival))
338 eBBlockFromiCode (iCodeFromAst (ival));
345 /* if it has an absolute address then generate
346 an equate for this no need to allocate space */
347 if (SPEC_ABSA (sym->etype))
351 fprintf (map->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
353 if (TARGET_IS_XA51) {
356 } else if (map==bit || map==sfrbit) {
360 fprintf (map->oFile, "%s\t%s\t0x%04x\n",
362 SPEC_ADDR (sym->etype));
365 int size = getAllocSize (sym->type);
367 werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE, sym->name);
371 fprintf (map->oFile, "==.\n");
373 if (IS_STATIC (sym->etype))
374 tfprintf (map->oFile, "!slabeldef\n", sym->rname);
376 tfprintf (map->oFile, "!labeldef\n", sym->rname);
377 tfprintf (map->oFile, "\t!ds\n",
378 (unsigned int) size & 0xffff);
383 /*-----------------------------------------------------------------*/
384 /* initPointer - pointer initialization code massaging */
385 /*-----------------------------------------------------------------*/
387 initPointer (initList * ilist, sym_link *toType)
393 return valCastLiteral(toType, 0.0);
396 expr = list2expr (ilist);
401 /* try it the old way first */
402 if ((val = constExprValue (expr, FALSE)))
405 /* ( ptr + constant ) */
406 if (IS_AST_OP (expr) &&
407 (expr->opval.op == '+' || expr->opval.op == '-') &&
408 IS_AST_SYM_VALUE (expr->left) &&
409 (IS_ARRAY(expr->left->ftype) || IS_PTR(expr->left->ftype)) &&
410 compareType(toType, expr->left->ftype) &&
411 IS_AST_LIT_VALUE (expr->right)) {
412 return valForCastAggr (expr->left, expr->left->ftype,
418 if (IS_AST_OP(expr) && expr->opval.op==CAST &&
419 IS_AST_OP(expr->right) && expr->right->opval.op=='&') {
420 if (compareType(toType, expr->left->ftype)!=1) {
421 werror (W_INIT_WRONG);
422 printFromToType(expr->left->ftype, toType);
428 /* no then we have to do these cludgy checks */
429 /* pointers can be initialized with address of
430 a variable or address of an array element */
431 if (IS_AST_OP (expr) && expr->opval.op == '&') {
432 /* address of symbol */
433 if (IS_AST_SYM_VALUE (expr->left)) {
434 val = copyValue (AST_VALUE (expr->left));
435 val->type = newLink (DECLARATOR);
436 if (SPEC_SCLS (expr->left->etype) == S_CODE) {
437 DCL_TYPE (val->type) = CPOINTER;
438 DCL_PTR_CONST (val->type) = port->mem.code_ro;
440 else if (SPEC_SCLS (expr->left->etype) == S_XDATA)
441 DCL_TYPE (val->type) = FPOINTER;
442 else if (SPEC_SCLS (expr->left->etype) == S_XSTACK)
443 DCL_TYPE (val->type) = PPOINTER;
444 else if (SPEC_SCLS (expr->left->etype) == S_IDATA)
445 DCL_TYPE (val->type) = IPOINTER;
446 else if (SPEC_SCLS (expr->left->etype) == S_EEPROM)
447 DCL_TYPE (val->type) = EEPPOINTER;
449 DCL_TYPE (val->type) = POINTER;
450 val->type->next = expr->left->ftype;
451 val->etype = getSpec (val->type);
455 /* if address of indexed array */
456 if (IS_AST_OP (expr->left) && expr->left->opval.op == '[')
457 return valForArray (expr->left);
459 /* if address of structure element then
461 if (IS_AST_OP (expr->left) &&
462 expr->left->opval.op == '.') {
463 return valForStructElem (expr->left->left,
468 (&some_struct)->element */
469 if (IS_AST_OP (expr->left) &&
470 expr->left->opval.op == PTR_OP &&
471 IS_ADDRESS_OF_OP (expr->left->left)) {
472 return valForStructElem (expr->left->left->left,
476 /* case 3. (((char *) &a) +/- constant) */
477 if (IS_AST_OP (expr) &&
478 (expr->opval.op == '+' || expr->opval.op == '-') &&
479 IS_AST_OP (expr->left) && expr->left->opval.op == CAST &&
480 IS_AST_OP (expr->left->right) &&
481 expr->left->right->opval.op == '&' &&
482 IS_AST_LIT_VALUE (expr->right)) {
484 return valForCastAggr (expr->left->right->left,
485 expr->left->left->opval.lnk,
486 expr->right, expr->opval.op);
489 /* case 4. (char *)(array type) */
490 if (IS_CAST_OP(expr) && IS_AST_SYM_VALUE (expr->right) &&
491 IS_ARRAY(expr->right->ftype)) {
493 val = copyValue (AST_VALUE (expr->right));
494 val->type = newLink (DECLARATOR);
495 if (SPEC_SCLS (expr->right->etype) == S_CODE) {
496 DCL_TYPE (val->type) = CPOINTER;
497 DCL_PTR_CONST (val->type) = port->mem.code_ro;
499 else if (SPEC_SCLS (expr->right->etype) == S_XDATA)
500 DCL_TYPE (val->type) = FPOINTER;
501 else if (SPEC_SCLS (expr->right->etype) == S_XSTACK)
502 DCL_TYPE (val->type) = PPOINTER;
503 else if (SPEC_SCLS (expr->right->etype) == S_IDATA)
504 DCL_TYPE (val->type) = IPOINTER;
505 else if (SPEC_SCLS (expr->right->etype) == S_EEPROM)
506 DCL_TYPE (val->type) = EEPPOINTER;
508 DCL_TYPE (val->type) = POINTER;
509 val->type->next = expr->right->ftype->next;
510 val->etype = getSpec (val->type);
515 werrorfl (expr->filename, expr->lineno, E_INCOMPAT_PTYPES);
517 werror (E_INCOMPAT_PTYPES);
522 /*-----------------------------------------------------------------*/
523 /* printChar - formats and prints a characater string with DB */
524 /*-----------------------------------------------------------------*/
526 printChar (FILE * ofile, char *s, int plen)
534 while (len && pplen < plen)
537 while (i && pplen < plen)
539 if (*s < ' ' || *s == '\"' || *s=='\\')
543 tfprintf (ofile, "\t!ascii\n", buf);
544 tfprintf (ofile, "\t!db !constbyte\n", (unsigned char)*s);
559 tfprintf (ofile, "\t!ascii\n", buf);
570 tfprintf (ofile, "\t!db !constbyte\n", 0);
575 /*-----------------------------------------------------------------*/
576 /* return the generic pointer high byte for a given pointer type. */
577 /*-----------------------------------------------------------------*/
579 pointerTypeToGPByte (const int p_type, const char *iname, const char *oname)
587 werror (E_CANNOT_USE_GENERIC_POINTER,
588 iname ? iname : "<null>",
589 oname ? oname : "<null>");
596 return GPTYPE_XSTACK;
598 fprintf (stderr, "*** internal error: unknown pointer type %d in GPByte.\n",
606 /*-----------------------------------------------------------------*/
607 /* printPointerType - generates ival for pointer type */
608 /*-----------------------------------------------------------------*/
610 _printPointerType (FILE * oFile, const char *name)
612 if (options.model == MODEL_FLAT24)
614 if (port->little_endian)
615 fprintf (oFile, "\t.byte %s,(%s >> 8),(%s >> 16)", name, name, name);
617 fprintf (oFile, "\t.byte (%s >> 16),(%s >> 8),%s", name, name, name);
621 if (port->little_endian)
622 fprintf (oFile, "\t.byte %s,(%s >> 8)", name, name);
624 fprintf (oFile, "\t.byte (%s >> 8),%s", name, name);
628 /*-----------------------------------------------------------------*/
629 /* printPointerType - generates ival for pointer type */
630 /*-----------------------------------------------------------------*/
632 printPointerType (FILE * oFile, const char *name)
634 _printPointerType (oFile, name);
635 fprintf (oFile, "\n");
638 /*-----------------------------------------------------------------*/
639 /* printGPointerType - generates ival for generic pointer type */
640 /*-----------------------------------------------------------------*/
642 printGPointerType (FILE * oFile, const char *iname, const char *oname,
643 const unsigned int type)
645 _printPointerType (oFile, iname);
646 fprintf (oFile, ",#0x%02x\n", pointerTypeToGPByte (type, iname, oname));
649 /*-----------------------------------------------------------------*/
650 /* printIvalType - generates ival for int/char */
651 /*-----------------------------------------------------------------*/
653 printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile)
657 /* if initList is deep */
658 if (ilist && (ilist->type == INIT_DEEP))
659 ilist = ilist->init.deep;
661 if (!(val = list2val (ilist))) {
662 // assuming a warning has been thrown
666 if (val->type != type) {
667 val = valCastLiteral(type, floatFromVal(val));
670 switch (getSize (type)) {
673 tfprintf (oFile, "\t!db !constbyte\n", 0);
675 tfprintf (oFile, "\t!dbs\n",
676 aopLiteral (val, 0));
680 if (port->use_dw_for_init)
681 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
682 else if (port->little_endian)
683 fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
685 fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0));
689 tfprintf (oFile, "\t!dw !constword\n", 0);
690 tfprintf (oFile, "\t!dw !constword\n", 0);
692 else if (port->little_endian) {
693 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
694 aopLiteral (val, 0), aopLiteral (val, 1),
695 aopLiteral (val, 2), aopLiteral (val, 3));
698 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
699 aopLiteral (val, 3), aopLiteral (val, 2),
700 aopLiteral (val, 1), aopLiteral (val, 0));
706 /*-----------------------------------------------------------------*/
707 /* printIvalBitFields - generate initializer for bitfields */
708 /*-----------------------------------------------------------------*/
709 void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile)
713 initList *lilist = *ilist ;
714 unsigned long ival = 0;
720 val = list2val(lilist);
722 if (SPEC_BLEN(lsym->etype) > 8) {
723 size += ((SPEC_BLEN (lsym->etype) / 8) +
724 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
727 size = ((SPEC_BLEN (lsym->etype) / 8) +
728 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
730 i = (unsigned long)floatFromVal(val);
731 i <<= SPEC_BSTR (lsym->etype);
733 if (! ( lsym->next &&
734 (IS_BITFIELD(lsym->next->type)) &&
735 (SPEC_BSTR(lsym->next->etype)))) break;
737 lilist = lilist->next;
741 tfprintf (oFile, "\t!db !constbyte\n",ival);
745 tfprintf (oFile, "\t!dw !constword\n",ival);
747 case 4: /* EEP: why is this db and not dw? */
748 tfprintf (oFile, "\t!db !constword,!constword\n",
749 (ival >> 8) & 0xffff, (ival & 0xffff));
756 /*-----------------------------------------------------------------*/
757 /* printIvalStruct - generates initial value for structures */
758 /*-----------------------------------------------------------------*/
760 printIvalStruct (symbol * sym, sym_link * type,
761 initList * ilist, FILE * oFile)
764 initList *iloop = NULL;
766 sflds = SPEC_STRUCT (type)->fields;
769 if (ilist->type != INIT_DEEP) {
770 werrorfl (sym->fileDef, sym->lineDef, E_INIT_STRUCT, sym->name);
774 iloop = ilist->init.deep;
777 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
778 if (IS_BITFIELD(sflds->type)) {
779 printIvalBitFields(&sflds,&iloop,oFile);
781 printIval (sym, sflds->type, iloop, oFile);
785 werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "struct", sym->name);
790 /*-----------------------------------------------------------------*/
791 /* printIvalChar - generates initital value for character array */
792 /*-----------------------------------------------------------------*/
794 printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
801 val = list2val (ilist);
802 /* if the value is a character string */
803 if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
805 if (!DCL_ELEM (type))
806 DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
808 printChar (oFile, SPEC_CVAL (val->etype).v_char, DCL_ELEM (type));
816 printChar (oFile, s, strlen (s) + 1);
820 /*-----------------------------------------------------------------*/
821 /* printIvalArray - generates code for array initialization */
822 /*-----------------------------------------------------------------*/
824 printIvalArray (symbol * sym, sym_link * type, initList * ilist,
831 /* take care of the special case */
832 /* array of characters can be init */
834 if (IS_CHAR (type->next)) {
835 if (!IS_LITERAL(list2val(ilist)->etype)) {
836 werrorfl (ilist->filename, ilist->lineno, E_CONST_EXPECTED);
839 if (printIvalChar (type,
840 (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
841 oFile, SPEC_CVAL (sym->etype).v_char))
844 /* not the special case */
845 if (ilist->type != INIT_DEEP) {
846 werrorfl (ilist->filename, ilist->lineno, E_INIT_STRUCT, sym->name);
850 for (iloop=ilist->init.deep; iloop; iloop=iloop->next) {
851 if ((++size > DCL_ELEM(type)) && DCL_ELEM(type)) {
852 werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "array", sym->name);
855 printIval (sym, type->next, iloop, oFile);
859 if (DCL_ELEM(type)) {
860 // pad with zeros if needed
861 if (size<DCL_ELEM(type)) {
862 size = (DCL_ELEM(type) - size) * getSize(type->next);
864 tfprintf (oFile, "\t!db !constbyte\n", 0);
868 // we have not been given a size, but we now know it
869 DCL_ELEM (type) = size;
875 /*-----------------------------------------------------------------*/
876 /* printIvalFuncPtr - generate initial value for function pointers */
877 /*-----------------------------------------------------------------*/
879 printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile)
885 val = list2val (ilist);
887 val = valCastLiteral(type, 0.0);
890 // an error has been thrown already
894 if (IS_LITERAL(val->etype)) {
895 if (compareType(type, val->etype) == 0) {
896 werrorfl (ilist->filename, ilist->lineno, E_INCOMPAT_TYPES);
897 printFromToType (val->type, type);
899 printIvalCharPtr (NULL, type, val, oFile);
903 /* check the types */
904 if ((dLvl = compareType (val->type, type->next)) <= 0)
906 tfprintf (oFile, "\t!dw !constword\n", 0);
910 /* now generate the name */
913 if (port->use_dw_for_init)
915 tfprintf (oFile, "\t!dws\n", val->name);
919 printPointerType (oFile, val->name);
922 else if (port->use_dw_for_init)
924 tfprintf (oFile, "\t!dws\n", val->sym->rname);
928 printPointerType (oFile, val->sym->rname);
934 /*-----------------------------------------------------------------*/
935 /* printIvalCharPtr - generates initial values for character pointers */
936 /*-----------------------------------------------------------------*/
938 printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile)
942 /* PENDING: this is _very_ mcs51 specific, including a magic
944 It's also endin specific.
946 size = getSize (type);
948 if (val->name && strlen (val->name))
950 if (size == 1) /* This appears to be Z80 specific?? */
953 "\t!dbs\n", val->name);
955 else if (size == FPTRSIZE)
957 if (port->use_dw_for_init)
959 tfprintf (oFile, "\t!dws\n", val->name);
963 printPointerType (oFile, val->name);
966 else if (size == GPTRSIZE)
969 if (IS_PTR (val->type)) {
970 type = DCL_TYPE (val->type);
972 type = PTR_TYPE (SPEC_OCLS (val->etype));
974 if (val->sym && val->sym->isstrlit) {
975 // this is a literal string
978 printGPointerType (oFile, val->name, sym->name, type);
982 fprintf (stderr, "*** internal error: unknown size in "
983 "printIvalCharPtr.\n");
988 // these are literals assigned to pointers
992 tfprintf (oFile, "\t!dbs\n", aopLiteral (val, 0));
995 if (port->use_dw_for_init)
996 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, size));
997 else if (port->little_endian)
998 tfprintf (oFile, "\t.byte %s,%s\n",
999 aopLiteral (val, 0), aopLiteral (val, 1));
1001 tfprintf (oFile, "\t.byte %s,%s\n",
1002 aopLiteral (val, 1), aopLiteral (val, 0));
1005 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
1006 // non-zero mcs51 generic pointer
1007 werrorfl (sym->fileDef, sym->lineDef, E_LITERAL_GENERIC);
1009 if (port->little_endian) {
1010 fprintf (oFile, "\t.byte %s,%s,%s\n",
1011 aopLiteral (val, 0),
1012 aopLiteral (val, 1),
1013 aopLiteral (val, 2));
1015 fprintf (oFile, "\t.byte %s,%s,%s\n",
1016 aopLiteral (val, 2),
1017 aopLiteral (val, 1),
1018 aopLiteral (val, 0));
1022 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
1023 // non-zero ds390 generic pointer
1024 werrorfl (sym->fileDef, sym->lineDef, E_LITERAL_GENERIC);
1026 if (port->little_endian) {
1027 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
1028 aopLiteral (val, 0),
1029 aopLiteral (val, 1),
1030 aopLiteral (val, 2),
1031 aopLiteral (val, 3));
1033 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
1034 aopLiteral (val, 3),
1035 aopLiteral (val, 2),
1036 aopLiteral (val, 1),
1037 aopLiteral (val, 0));
1045 if (val->sym && val->sym->isstrlit && !isinSet(statsg->syms, val->sym)) {
1046 addSet (&statsg->syms, val->sym);
1052 /*-----------------------------------------------------------------*/
1053 /* printIvalPtr - generates initial value for pointers */
1054 /*-----------------------------------------------------------------*/
1056 printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
1062 if (ilist && (ilist->type == INIT_DEEP))
1063 ilist = ilist->init.deep;
1065 /* function pointer */
1066 if (IS_FUNC (type->next))
1068 printIvalFuncPtr (type, ilist, oFile);
1072 if (!(val = initPointer (ilist, type)))
1075 /* if character pointer */
1076 if (IS_CHAR (type->next))
1077 if (printIvalCharPtr (sym, type, val, oFile))
1080 /* check the type */
1081 if (compareType (type, val->type) == 0) {
1082 werrorfl (ilist->filename, ilist->lineno, W_INIT_WRONG);
1083 printFromToType (val->type, type);
1086 /* if val is literal */
1087 if (IS_LITERAL (val->etype))
1089 switch (getSize (type))
1092 tfprintf (oFile, "\t!db !constbyte\n", (unsigned int) floatFromVal (val) & 0xff);
1095 if (port->use_dw_for_init)
1096 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
1097 else if (port->little_endian)
1098 tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
1100 tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0));
1102 case 3: // how about '390??
1103 fprintf (oFile, "; generic printIvalPtr\n");
1104 if (port->little_endian)
1106 fprintf (oFile, "\t.byte %s,%s",
1107 aopLiteral (val, 0), aopLiteral (val, 1));
1111 fprintf (oFile, "\t.byte %s,%s",
1112 aopLiteral (val, 1), aopLiteral (val, 0));
1114 if (IS_GENPTR (val->type))
1115 fprintf (oFile, ",%s\n", aopLiteral (val, 2));
1116 else if (IS_PTR (val->type))
1117 fprintf (oFile, ",#%x\n", pointerTypeToGPByte (DCL_TYPE (val->type), NULL, NULL));
1119 fprintf (oFile, ",%s\n", aopLiteral (val, 2));
1125 size = getSize (type);
1127 if (size == 1) /* Z80 specific?? */
1129 tfprintf (oFile, "\t!dbs\n", val->name);
1131 else if (size == FPTRSIZE)
1133 if (port->use_dw_for_init) {
1134 tfprintf (oFile, "\t!dws\n", val->name);
1136 printPointerType (oFile, val->name);
1139 else if (size == GPTRSIZE)
1141 printGPointerType (oFile, val->name, sym->name,
1142 (IS_PTR (val->type) ? DCL_TYPE (val->type) :
1143 PTR_TYPE (SPEC_OCLS (val->etype))));
1148 /*-----------------------------------------------------------------*/
1149 /* printIval - generates code for initial value */
1150 /*-----------------------------------------------------------------*/
1152 printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
1156 /* if structure then */
1157 if (IS_STRUCT (type))
1159 printIvalStruct (sym, type, ilist, oFile);
1163 /* if this is an array */
1164 if (IS_ARRAY (type))
1166 printIvalArray (sym, type, ilist, oFile);
1172 // not an aggregate, ilist must be a node
1173 if (ilist->type!=INIT_NODE) {
1174 // or a 1-element list
1175 if (ilist->init.deep->next) {
1176 werrorfl (sym->fileDef, sym->lineDef, W_EXCESS_INITIALIZERS, "scalar",
1179 ilist=ilist->init.deep;
1183 // and the type must match
1184 itype=ilist->init.node->ftype;
1186 if (compareType(type, itype)==0) {
1187 // special case for literal strings
1188 if (IS_ARRAY (itype) && IS_CHAR (getSpec(itype)) &&
1189 // which are really code pointers
1190 IS_PTR(type) && DCL_TYPE(type)==CPOINTER) {
1193 werrorfl (ilist->filename, ilist->lineno, E_TYPE_MISMATCH, "assignment", " ");
1194 printFromToType(itype, type);
1199 /* if this is a pointer */
1202 printIvalPtr (sym, type, ilist, oFile);
1206 /* if type is SPECIFIER */
1209 printIvalType (sym, type, ilist, oFile);
1214 /*-----------------------------------------------------------------*/
1215 /* emitStaticSeg - emitcode for the static segment */
1216 /*-----------------------------------------------------------------*/
1218 emitStaticSeg (memmap * map, FILE * out)
1222 /* fprintf(out, "\t.area\t%s\n", map->sname); */
1224 /* for all variables in this segment do */
1225 for (sym = setFirstItem (map->syms); sym;
1226 sym = setNextItem (map->syms))
1229 /* if it is "extern" then do nothing */
1230 if (IS_EXTERN (sym->etype))
1233 /* if it is not static add it to the public
1235 if (!IS_STATIC (sym->etype))
1237 addSetHead (&publics, sym);
1240 /* print extra debug info if required */
1241 if (options.debug) {
1245 if (IS_STATIC (sym->etype))
1246 fprintf (out, "F%s$", moduleName); /* scope is file */
1248 fprintf (out, "G$"); /* scope is global */
1251 /* symbol is local */
1252 fprintf (out, "L%s$",
1253 (sym->localof ? sym->localof->name : "-null-"));
1254 fprintf (out, "%s$%d$%d", sym->name, sym->level, sym->block);
1257 /* if it has an absolute address */
1258 if (SPEC_ABSA (sym->etype))
1261 fprintf (out, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1263 fprintf (out, "%s\t=\t0x%04x\n",
1265 SPEC_ADDR (sym->etype));
1270 fprintf (out, " == .\n");
1272 /* if it has an initial value */
1275 fprintf (out, "%s:\n", sym->rname);
1277 resolveIvalSym (sym->ival, sym->type);
1278 printIval (sym, sym->type, sym->ival, out);
1280 /* if sym is a simple string and sym->ival is a string,
1281 WE don't need it anymore */
1282 if (IS_ARRAY(sym->type) && IS_CHAR(sym->type->next) &&
1283 IS_AST_SYM_VALUE(list2expr(sym->ival)) &&
1284 list2val(sym->ival)->sym->isstrlit) {
1285 freeStringSymbol(list2val(sym->ival)->sym);
1289 /* allocate space */
1290 int size = getSize (sym->type);
1293 werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE,sym->name);
1295 fprintf (out, "%s:\n", sym->rname);
1296 /* special case for character strings */
1297 if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) &&
1298 SPEC_CVAL (sym->etype).v_char)
1300 SPEC_CVAL (sym->etype).v_char,
1303 tfprintf (out, "\t!ds\n", (unsigned int) size & 0xffff);
1309 /*-----------------------------------------------------------------*/
1310 /* emitMaps - emits the code for the data portion the code */
1311 /*-----------------------------------------------------------------*/
1315 int publicsfr = TARGET_IS_MCS51; /* Ideally, this should be true for all */
1316 /* ports but let's be conservative - EEP */
1319 /* no special considerations for the following
1320 data, idata & bit & xdata */
1321 emitRegularMap (data, TRUE, TRUE);
1322 emitRegularMap (idata, TRUE, TRUE);
1323 emitRegularMap (bit, TRUE, FALSE);
1324 emitRegularMap (xdata, TRUE, TRUE);
1325 if (port->genXINIT) {
1326 emitRegularMap (xidata, TRUE, TRUE);
1328 emitRegularMap (sfr, publicsfr, FALSE);
1329 emitRegularMap (sfrbit, publicsfr, FALSE);
1330 emitRegularMap (home, TRUE, FALSE);
1331 emitRegularMap (code, TRUE, FALSE);
1333 emitStaticSeg (statsg, code->oFile);
1334 if (port->genXINIT) {
1335 tfprintf (code->oFile, "\t!area\n", xinit->sname);
1336 emitStaticSeg (xinit, code->oFile);
1341 /*-----------------------------------------------------------------*/
1342 /* flushStatics - flush all currently defined statics out to file */
1343 /* and delete. Temporary function */
1344 /*-----------------------------------------------------------------*/
1348 emitStaticSeg (statsg, codeOutFile);
1349 statsg->syms = NULL;
1352 /*-----------------------------------------------------------------*/
1353 /* createInterruptVect - creates the interrupt vector */
1354 /*-----------------------------------------------------------------*/
1356 createInterruptVect (FILE * vFile)
1359 mainf = newSymbol ("main", 0);
1362 /* only if the main function exists */
1363 if (!(mainf = findSymWithLevel (SymbolTab, mainf)))
1365 if (!options.cc_only && !noAssemble && !options.c1mode)
1370 /* if the main is only a prototype ie. no body then do nothing */
1371 if (!IFFUNC_HASBODY(mainf->type))
1373 /* if ! compile only then main function should be present */
1374 if (!options.cc_only && !noAssemble)
1379 tfprintf (vFile, "\t!areacode\n", CODE_NAME);
1380 fprintf (vFile, "__interrupt_vect:\n");
1383 if (!port->genIVT || !(port->genIVT (vFile, interrupts, maxInterrupts)))
1385 /* "generic" interrupt table header (if port doesn't specify one).
1386 * Look suspiciously like 8051 code to me...
1389 fprintf (vFile, "\tljmp\t__sdcc_gsinit_startup\n");
1391 /* now for the other interrupts */
1392 for (; i < maxInterrupts; i++)
1396 fprintf (vFile, "\tljmp\t%s\n", interrupts[i]->rname);
1397 if ( i != maxInterrupts - 1 )
1398 fprintf (vFile, "\t.ds\t5\n");
1402 fprintf (vFile, "\treti\n");
1403 if ( i != maxInterrupts - 1 )
1404 fprintf (vFile, "\t.ds\t7\n");
1412 ";--------------------------------------------------------\n"
1413 "; File Created by SDCC : FreeWare ANSI-C Compiler\n"};
1417 ";--------------------------------------------------------\n"};
1420 /*-----------------------------------------------------------------*/
1421 /* initialComments - puts in some initial comments */
1422 /*-----------------------------------------------------------------*/
1424 initialComments (FILE * afile)
1428 fprintf (afile, "%s", iComments1);
1429 fprintf (afile, "; Version " SDCC_VERSION_STR " (%s)\n", __DATE__);
1430 fprintf (afile, "; This file generated %s", asctime (localtime (&t)));
1431 fprintf (afile, "%s", iComments2);
1434 /*-----------------------------------------------------------------*/
1435 /* printPublics - generates .global for publics */
1436 /*-----------------------------------------------------------------*/
1438 printPublics (FILE * afile)
1442 fprintf (afile, "%s", iComments2);
1443 fprintf (afile, "; Public variables in this module\n");
1444 fprintf (afile, "%s", iComments2);
1446 for (sym = setFirstItem (publics); sym;
1447 sym = setNextItem (publics))
1448 tfprintf (afile, "\t!global\n", sym->rname);
1451 /*-----------------------------------------------------------------*/
1452 /* printExterns - generates .global for externs */
1453 /*-----------------------------------------------------------------*/
1455 printExterns (FILE * afile)
1459 fprintf (afile, "%s", iComments2);
1460 fprintf (afile, "; Externals used\n");
1461 fprintf (afile, "%s", iComments2);
1463 for (sym = setFirstItem (externs); sym;
1464 sym = setNextItem (externs))
1465 tfprintf (afile, "\t!extern\n", sym->rname);
1468 /*-----------------------------------------------------------------*/
1469 /* emitOverlay - will emit code for the overlay stuff */
1470 /*-----------------------------------------------------------------*/
1472 emitOverlay (FILE * afile)
1476 if (!elementsInSet (ovrSetSets))
1477 tfprintf (afile, "\t!area\n", port->mem.overlay_name);
1479 /* for each of the sets in the overlay segment do */
1480 for (ovrset = setFirstItem (ovrSetSets); ovrset;
1481 ovrset = setNextItem (ovrSetSets))
1486 if (elementsInSet (ovrset))
1488 /* output the area informtion */
1489 fprintf (afile, "\t.area\t%s\n", port->mem.overlay_name); /* MOF */
1492 for (sym = setFirstItem (ovrset); sym;
1493 sym = setNextItem (ovrset))
1495 /* if extern then it is in the publics table: do nothing */
1496 if (IS_EXTERN (sym->etype))
1499 /* if allocation required check is needed
1500 then check if the symbol really requires
1501 allocation only for local variables */
1502 if (!IS_AGGREGATE (sym->type) &&
1503 !(sym->_isparm && !IS_REGPARM (sym->etype))
1504 && !sym->allocreq && sym->level)
1507 /* if global variable & not static or extern
1508 and addPublics allowed then add it to the public set */
1509 if ((sym->_isparm && !IS_REGPARM (sym->etype))
1510 && !IS_STATIC (sym->etype))
1512 addSetHead (&publics, sym);
1515 /* if extern then do nothing or is a function
1517 if (IS_FUNC (sym->type))
1520 /* print extra debug info if required */
1525 if (IS_STATIC (sym->etype))
1526 fprintf (afile, "F%s$", moduleName); /* scope is file */
1528 fprintf (afile, "G$"); /* scope is global */
1531 /* symbol is local */
1532 fprintf (afile, "L%s$",
1533 (sym->localof ? sym->localof->name : "-null-"));
1534 fprintf (afile, "%s$%d$%d", sym->name, sym->level, sym->block);
1537 /* if is has an absolute address then generate
1538 an equate for this no need to allocate space */
1539 if (SPEC_ABSA (sym->etype))
1543 fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1545 fprintf (afile, "%s\t=\t0x%04x\n",
1547 SPEC_ADDR (sym->etype));
1550 int size = getSize(sym->type);
1553 werrorfl (sym->fileDef, sym->lineDef, E_UNKNOWN_SIZE);
1556 fprintf (afile, "==.\n");
1558 /* allocate space */
1559 tfprintf (afile, "!labeldef\n", sym->rname);
1560 tfprintf (afile, "\t!ds\n", (unsigned int) getSize (sym->type) & 0xffff);
1568 /*-----------------------------------------------------------------*/
1569 /* spacesToUnderscores - replace spaces with underscores */
1570 /*-----------------------------------------------------------------*/
1572 spacesToUnderscores (char *dest, const char *src, size_t len)
1577 assert(dest != NULL);
1578 assert(src != NULL);
1582 for (p = dest, i = 0; *src != '\0' && i < len; ++src, ++i) {
1583 *p++ = isspace(*src) ? '_' : *src;
1591 /*-----------------------------------------------------------------*/
1592 /* glue - the final glue that hold the whole thing together */
1593 /*-----------------------------------------------------------------*/
1599 FILE *ovrFile = tempfile ();
1600 char moduleBuf[PATH_MAX];
1603 if(port->general.glue_up_main &&
1604 (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_XA51 || TARGET_IS_DS400))
1606 mcs51_like=1; /*So it has bits, sfr, sbits, data, idata, etc...*/
1613 addSetHead (&tmpfileSet, ovrFile);
1614 /* print the global struct definitions */
1618 vFile = tempfile ();
1619 /* PENDING: this isnt the best place but it will do */
1620 if (port->general.glue_up_main)
1622 /* create the interrupt vector table */
1623 createInterruptVect (vFile);
1626 addSetHead (&tmpfileSet, vFile);
1628 /* emit code for the all the variables declared */
1630 /* do the overlay segments */
1631 emitOverlay (ovrFile);
1633 outputDebugSymbols();
1635 /* now put it all together into the assembler file */
1636 /* create the assembler file name */
1638 /* -o option overrides default name? */
1639 if ((noAssemble || options.c1mode) && fullDstFileName)
1641 strncpyz (scratchFileName, fullDstFileName, PATH_MAX);
1645 strncpyz (scratchFileName, dstFileName, PATH_MAX);
1646 strncatz (scratchFileName, port->assembler.file_ext, PATH_MAX);
1649 if (!(asmFile = fopen (scratchFileName, "w")))
1651 werror (E_FILE_OPEN_ERR, scratchFileName);
1655 /* initial comments */
1656 initialComments (asmFile);
1658 /* print module name */
1659 tfprintf (asmFile, "\t!module\n",
1660 spacesToUnderscores (moduleBuf, moduleName, sizeof moduleBuf));
1663 fprintf (asmFile, "\t.optsdcc -m%s", port->target);
1665 switch(options.model)
1667 case MODEL_SMALL: fprintf (asmFile, " --model-small"); break;
1668 case MODEL_COMPACT: fprintf (asmFile, " --model-compact"); break;
1669 case MODEL_MEDIUM: fprintf (asmFile, " --model-medium"); break;
1670 case MODEL_LARGE: fprintf (asmFile, " --model-large"); break;
1671 case MODEL_FLAT24: fprintf (asmFile, " --model-flat24"); break;
1672 case MODEL_PAGE0: fprintf (asmFile, " --model-page0"); break;
1675 /*if(options.stackAuto) fprintf (asmFile, " --stack-auto");*/
1676 if(options.useXstack) fprintf (asmFile, " --xstack");
1677 /*if(options.intlong_rent) fprintf (asmFile, " --int-long-rent");*/
1678 /*if(options.float_rent) fprintf (asmFile, " --float-rent");*/
1679 if(options.noRegParams) fprintf (asmFile, " --no-reg-params");
1680 if(options.parms_in_bank1) fprintf (asmFile, " --parms-in-bank1");
1681 fprintf (asmFile, "\n");
1683 else if(TARGET_IS_Z80 || TARGET_IS_GBZ80 || TARGET_IS_HC08)
1685 fprintf (asmFile, "\t.optsdcc -m%s\n", port->target);
1688 tfprintf (asmFile, "\t!fileprelude\n");
1690 /* Let the port generate any global directives, etc. */
1691 if (port->genAssemblerPreamble)
1693 port->genAssemblerPreamble (asmFile);
1696 /* print the global variables in this module */
1697 printPublics (asmFile);
1698 if (port->assembler.externGlobal)
1699 printExterns (asmFile);
1702 ||( TARGET_IS_Z80 )) /*.p.t.20030924 need to output SFR table for Z80 as well */
1704 /* copy the sfr segment */
1705 fprintf (asmFile, "%s", iComments2);
1706 fprintf (asmFile, "; special function registers\n");
1707 fprintf (asmFile, "%s", iComments2);
1708 copyFile (asmFile, sfr->oFile);
1713 /* copy the sbit segment */
1714 fprintf (asmFile, "%s", iComments2);
1715 fprintf (asmFile, "; special function bits \n");
1716 fprintf (asmFile, "%s", iComments2);
1717 copyFile (asmFile, sfrbit->oFile);
1719 /*JCF: Create the areas for the register banks*/
1720 if(RegBankUsed[0]||RegBankUsed[1]||RegBankUsed[2]||RegBankUsed[3])
1722 fprintf (asmFile, "%s", iComments2);
1723 fprintf (asmFile, "; overlayable register banks \n");
1724 fprintf (asmFile, "%s", iComments2);
1726 fprintf (asmFile, "\t.area REG_BANK_0\t(REL,OVR,DATA)\n\t.ds 8\n");
1727 if(RegBankUsed[1]||options.parms_in_bank1)
1728 fprintf (asmFile, "\t.area REG_BANK_1\t(REL,OVR,DATA)\n\t.ds 8\n");
1730 fprintf (asmFile, "\t.area REG_BANK_2\t(REL,OVR,DATA)\n\t.ds 8\n");
1732 fprintf (asmFile, "\t.area REG_BANK_3\t(REL,OVR,DATA)\n\t.ds 8\n");
1736 /* copy the data segment */
1737 fprintf (asmFile, "%s", iComments2);
1738 fprintf (asmFile, "; %s ram data\n", mcs51_like?"internal":"");
1739 fprintf (asmFile, "%s", iComments2);
1740 copyFile (asmFile, data->oFile);
1743 /* create the overlay segments */
1745 fprintf (asmFile, "%s", iComments2);
1746 fprintf (asmFile, "; overlayable items in %s ram \n", mcs51_like?"internal":"");
1747 fprintf (asmFile, "%s", iComments2);
1748 copyFile (asmFile, ovrFile);
1751 /* create the stack segment MOF */
1752 if (mainf && IFFUNC_HASBODY(mainf->type))
1754 fprintf (asmFile, "%s", iComments2);
1755 fprintf (asmFile, "; Stack segment in internal ram \n");
1756 fprintf (asmFile, "%s", iComments2);
1757 fprintf (asmFile, "\t.area\tSSEG\t(DATA)\n"
1758 "__start__stack:\n\t.ds\t1\n\n");
1761 /* create the idata segment */
1762 if ( (idata) && (mcs51_like) ) {
1763 fprintf (asmFile, "%s", iComments2);
1764 fprintf (asmFile, "; indirectly addressable internal ram data\n");
1765 fprintf (asmFile, "%s", iComments2);
1766 copyFile (asmFile, idata->oFile);
1769 /* copy the bit segment */
1771 fprintf (asmFile, "%s", iComments2);
1772 fprintf (asmFile, "; bit data\n");
1773 fprintf (asmFile, "%s", iComments2);
1774 copyFile (asmFile, bit->oFile);
1777 /* if external stack then reserve space of it */
1778 if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack)
1780 fprintf (asmFile, "%s", iComments2);
1781 fprintf (asmFile, "; external stack \n");
1782 fprintf (asmFile, "%s", iComments2);
1783 fprintf (asmFile, "\t.area XSEG (XDATA)\n"); /* MOF */
1784 fprintf (asmFile, "\t.ds 256\n");
1788 /* copy xtern ram data */
1790 fprintf (asmFile, "%s", iComments2);
1791 fprintf (asmFile, "; external ram data\n");
1792 fprintf (asmFile, "%s", iComments2);
1793 copyFile (asmFile, xdata->oFile);
1796 /* copy xternal initialized ram data */
1797 fprintf (asmFile, "%s", iComments2);
1798 fprintf (asmFile, "; external initialized ram data\n");
1799 fprintf (asmFile, "%s", iComments2);
1800 copyFile (asmFile, xidata->oFile);
1802 /* If the port wants to generate any extra areas, let it do so. */
1803 if (port->extraAreas.genExtraAreaDeclaration)
1805 port->extraAreas.genExtraAreaDeclaration(asmFile,
1806 mainf && IFFUNC_HASBODY(mainf->type));
1809 /* copy the interrupt vector table */
1810 if (mainf && IFFUNC_HASBODY(mainf->type))
1812 fprintf (asmFile, "%s", iComments2);
1813 fprintf (asmFile, "; interrupt vector \n");
1814 fprintf (asmFile, "%s", iComments2);
1815 copyFile (asmFile, vFile);
1818 /* copy global & static initialisations */
1819 fprintf (asmFile, "%s", iComments2);
1820 fprintf (asmFile, "; global & static initialisations\n");
1821 fprintf (asmFile, "%s", iComments2);
1823 /* Everywhere we generate a reference to the static_name area,
1824 * (which is currently only here), we immediately follow it with a
1825 * definition of the post_static_name area. This guarantees that
1826 * the post_static_name area will immediately follow the static_name
1829 tfprintf (asmFile, "\t!area\n", port->mem.static_name); /* MOF */
1830 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1831 tfprintf (asmFile, "\t!area\n", port->mem.static_name);
1833 if (mainf && IFFUNC_HASBODY(mainf->type))
1835 if (port->genInitStartup)
1837 port->genInitStartup(asmFile);
1841 fprintf (asmFile, "__sdcc_gsinit_startup:\n");
1842 /* if external stack is specified then the
1843 higher order byte of the xdatalocation is
1844 going into P2 and the lower order going into
1846 if (options.useXstack)
1848 fprintf (asmFile, "\tmov\tP2,#0x%02x\n",
1849 (((unsigned int) options.xdata_loc) >> 8) & 0xff);
1850 fprintf (asmFile, "\tmov\t_spx,#0x%02x\n",
1851 (unsigned int) options.xdata_loc & 0xff);
1854 // This should probably be a port option, but I'm being lazy.
1855 // on the 400, the firmware boot loader gives us a valid stack
1856 // (see '400 data sheet pg. 85 (TINI400 ROM Initialization code)
1857 if (!TARGET_IS_DS400)
1859 /* initialise the stack pointer. JCF: aslink takes care of the location */
1860 fprintf (asmFile, "\tmov\tsp,#__start__stack - 1\n"); /* MOF */
1863 fprintf (asmFile, "\tlcall\t__sdcc_external_startup\n");
1864 fprintf (asmFile, "\tmov\ta,dpl\n");
1865 fprintf (asmFile, "\tjz\t__sdcc_init_data\n");
1866 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1867 fprintf (asmFile, "__sdcc_init_data:\n");
1869 // if the port can copy the XINIT segment to XISEG
1872 port->genXINIT(asmFile);
1877 copyFile (asmFile, statsg->oFile);
1879 if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type))
1881 /* This code is generated in the post-static area.
1882 * This area is guaranteed to follow the static area
1883 * by the ugly shucking and jiving about 20 lines ago.
1885 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1886 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1892 "%s", iComments2, iComments2);
1893 tfprintf (asmFile, "\t!areahome\n", HOME_NAME);
1894 copyFile (asmFile, home->oFile);
1896 /* copy over code */
1897 fprintf (asmFile, "%s", iComments2);
1898 fprintf (asmFile, "; code\n");
1899 fprintf (asmFile, "%s", iComments2);
1900 tfprintf (asmFile, "\t!areacode\n", CODE_NAME);
1901 if (mainf && IFFUNC_HASBODY(mainf->type))
1904 /* entry point @ start of CSEG */
1905 fprintf (asmFile, "__sdcc_program_startup:\n");
1907 /* put in jump or call to main */
1908 if (options.mainreturn)
1910 fprintf (asmFile, "\tljmp\t_main\n"); /* needed? */
1911 fprintf (asmFile, ";\treturn from main will return to caller\n");
1915 fprintf (asmFile, "\tlcall\t_main\n");
1916 fprintf (asmFile, ";\treturn from main will lock up\n");
1917 fprintf (asmFile, "\tsjmp .\n");
1920 copyFile (asmFile, code->oFile);
1922 if (port->genAssemblerEnd) {
1923 port->genAssemblerEnd(asmFile);
1931 /** Creates a temporary file with unique file name
1933 - TMP, TEMP, TMPDIR env. varibles
1934 - if Un*x system: /usr/tmp and /tmp
1935 - root directory using mkstemp() if avaliable
1936 - default location using tempnam()
1939 tempfileandname(char *fname, size_t len)
1941 #define TEMPLATE "sdccXXXXXX"
1942 #define TEMPLATE_LEN ((sizeof TEMPLATE) - 1)
1944 const char *tmpdir = NULL;
1947 if ((tmpdir = getenv ("TMP")) == NULL)
1948 if ((tmpdir = getenv ("TEMP")) == NULL)
1949 tmpdir = getenv ("TMPDIR");
1953 static int warning_emitted;
1958 if (!warning_emitted)
1960 fprintf (stderr, "TMP not defined in environment, using %s for temporary files\n.", tmpdir);
1961 warning_emitted = 1;
1967 /* try with /usr/tmp and /tmp on Un*x systems */
1968 struct stat statbuf;
1970 if (tmpdir == NULL) {
1971 if (stat("/usr/tmp", &statbuf) != -1)
1972 tmpdir = "/usr/tmp";
1973 else if (stat("/tmp", &statbuf) != -1)
1981 char fnamebuf[PATH_MAX];
1984 if (fname == NULL || len == 0) {
1986 len = sizeof fnamebuf;
1990 name_len = strlen(tmpdir) + 1 + TEMPLATE_LEN;
1992 assert(name_len < len);
1993 if (!(name_len < len)) /* in NDEBUG is defined */
1994 return -1; /* buffer too small, temporary file can not be created */
1996 sprintf(fname, "%s" DIR_SEPARATOR_STRING TEMPLATE, tmpdir);
1999 name_len = TEMPLATE_LEN;
2001 assert(name_len < len);
2002 if (!(name_len < len)) /* in NDEBUG is defined */
2003 return -1; /* buffer too small, temporary file can not be created */
2005 strcpy(fname, TEMPLATE);
2008 fd = mkstemp(fname);
2012 char *name = tempnam(tmpdir, "sdcc");
2015 perror("Can't create temporary file name");
2019 assert(strlen(name) < len);
2020 if (!(strlen(name) < len)) /* in NDEBUG is defined */
2021 return -1; /* buffer too small, temporary file can not be created */
2023 strcpy(fname, name);
2025 fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
2027 fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
2033 perror("Can't create temporary file");
2041 /** Create a temporary file name
2047 static char fnamebuf[PATH_MAX];
2049 if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
2050 fprintf(stderr, "Can't create temporary file name!");
2061 /** Create a temporary file and add it to tmpfileNameSet,
2062 so that it is removed explicitly by rm_tmpfiles()
2063 or implicitly at program extit.
2071 char fnamebuf[PATH_MAX];
2073 if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
2074 fprintf(stderr, "Can't create temporary file!");
2078 tmp = Safe_strdup(fnamebuf);
2080 addSetHead(&tmpfileNameSet, tmp);
2082 if ((fp = fdopen(fd, "w+b")) == NULL) {
2083 perror("Can't create temporary file!");