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[256];
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 extern char *VersionString;
49 set *pipeSet = NULL; /* set of pipes */
50 set *tmpfileSet = NULL; /* set of tmp file created by the compiler */
51 set *tmpfileNameSet = NULL; /* All are unlinked at close. */
53 /*-----------------------------------------------------------------*/
54 /* closePipes - closes all pipes created by the compiler */
55 /*-----------------------------------------------------------------*/
56 DEFSETFUNC (closePipes)
69 /*-----------------------------------------------------------------*/
70 /* closeTmpFiles - closes all tmp files created by the compiler */
71 /* because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
72 /*-----------------------------------------------------------------*/
73 DEFSETFUNC (closeTmpFiles)
86 /*-----------------------------------------------------------------*/
87 /* rmTmpFiles - unlinks all tmp files created by the compiler */
88 /* because of BRAIN DEAD MS/DOS & CYGNUS Libraries */
89 /*-----------------------------------------------------------------*/
90 DEFSETFUNC (rmTmpFiles)
104 /*-----------------------------------------------------------------*/
105 /* rm_tmpfiles - close and remove temporary files and delete sets */
106 /*-----------------------------------------------------------------*/
110 /* close temporary files */
111 applyToSet (pipeSet, closePipes);
112 /* close temporary files */
113 deleteSet (&pipeSet);
115 applyToSet (tmpfileSet, closeTmpFiles);
116 /* remove temporary files */
117 applyToSet (tmpfileNameSet, rmTmpFiles);
118 /* delete temorary file sets */
119 deleteSet (&tmpfileSet);
120 deleteSet (&tmpfileNameSet);
123 /*-----------------------------------------------------------------*/
124 /* copyFile - copies source file to destination file */
125 /*-----------------------------------------------------------------*/
127 copyFile (FILE * dest, FILE * src)
133 if ((ch = fgetc (src)) != EOF)
138 aopLiteralLong (value * val, int offset, int size)
147 // assuming we have been warned before
151 /* if it is a float then it gets tricky */
152 /* otherwise it is fairly simple */
153 if (!IS_FLOAT (val->type)) {
154 unsigned long v = (unsigned long) floatFromVal (val);
159 tsprintf (buffer, sizeof(buffer),
160 "!immedbyte", (unsigned int) v & 0xff);
163 tsprintf (buffer, sizeof(buffer),
164 "!immedword", (unsigned int) v & 0xffff);
167 /* Hmm. Too big for now. */
170 return Safe_strdup (buffer);
173 /* PENDING: For now size must be 1 */
176 /* it is type float */
177 fl.f = (float) floatFromVal (val);
178 #ifdef WORDS_BIGENDIAN
179 tsprintf (buffer, sizeof(buffer),
180 "!immedbyte", fl.c[3 - offset]);
182 tsprintf (buffer, sizeof(buffer),
183 "!immedbyte", fl.c[offset]);
185 return Safe_strdup (buffer);
188 /*-----------------------------------------------------------------*/
189 /* aopLiteral - string from a literal value */
190 /*-----------------------------------------------------------------*/
192 aopLiteral (value * val, int offset)
194 return aopLiteralLong (val, offset, 1);
197 /*-----------------------------------------------------------------*/
198 /* emitRegularMap - emit code for maps with no special cases */
199 /*-----------------------------------------------------------------*/
201 emitRegularMap (memmap * map, bool addPublics, bool arFlag)
211 /* PENDING: special case here - should remove */
212 if (!strcmp (map->sname, CODE_NAME))
213 tfprintf (map->oFile, "\t!areacode\n", map->sname);
214 else if (!strcmp (map->sname, DATA_NAME))
215 tfprintf (map->oFile, "\t!areadata\n", map->sname);
216 else if (!strcmp (map->sname, HOME_NAME))
217 tfprintf (map->oFile, "\t!areahome\n", map->sname);
219 tfprintf (map->oFile, "\t!area\n", map->sname);
222 for (sym = setFirstItem (map->syms); sym;
223 sym = setNextItem (map->syms))
227 /* if extern then add it into the extern list */
228 if (IS_EXTERN (sym->etype))
230 addSetHead (&externs, sym);
234 /* if allocation required check is needed
235 then check if the symbol really requires
236 allocation only for local variables */
238 if (arFlag && !IS_AGGREGATE (sym->type) &&
239 !(sym->_isparm && !IS_REGPARM (sym->etype)) &&
240 !sym->allocreq && sym->level)
243 /* for bitvar locals and parameters */
244 if (!arFlag && !sym->allocreq && sym->level
245 && !SPEC_ABSA (sym->etype)) {
249 /* if global variable & not static or extern
250 and addPublics allowed then add it to the public set */
251 if ((sym->level == 0 ||
252 (sym->_isparm && !IS_REGPARM (sym->etype))) &&
254 !IS_STATIC (sym->etype) &&
255 (IS_FUNC(sym->type) ? (sym->used || IFFUNC_HASBODY(sym->type)) : 1))
257 addSetHead (&publics, sym);
260 /* if extern then do nothing or is a function
262 if (IS_FUNC (sym->type))
265 /* print extra debug info if required */
267 cdbSymbol (sym, cdbFile, FALSE, FALSE);
268 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 */
274 /* symbol is local */
275 fprintf (map->oFile, "L%s$", (sym->localof ? sym->localof->name : "-null-"));
276 fprintf (map->oFile, "%s$%d$%d", sym->name, sym->level, sym->block);
279 /* if it has an initial value then do it only if
280 it is a global variable */
281 if (sym->ival && sym->level == 0) {
282 if (SPEC_OCLS(sym->etype)==xidata) {
283 // create a new "XINIT (CODE)" symbol, that will be emitted later
284 newSym=copySymbol (sym);
285 SPEC_OCLS(newSym->etype)=xinit;
286 SNPRINTF (newSym->name, sizeof(newSym->name), "__xinit_%s", sym->name);
287 SNPRINTF (newSym->rname, sizeof(newSym->rname), "__xinit_%s", sym->rname);
288 SPEC_CONST(newSym->etype)=1;
289 SPEC_STAT(newSym->etype)=1;
290 resolveIvalSym(newSym->ival);
292 // add it to the "XINIT (CODE)" segment
293 addSet(&xinit->syms, newSym);
296 if (IS_AGGREGATE (sym->type)) {
297 ival = initAggregates (sym, sym->ival, NULL);
299 if (getNelements(sym->type, sym->ival)>1) {
300 werror (W_EXCESS_INITIALIZERS, "scalar",
301 sym->name, sym->lineDef);
303 ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
304 decorateType (resolveSymbols (list2expr (sym->ival))));
306 codeOutFile = statsg->oFile;
309 // set ival's lineno to where the symbol was defined
310 setAstLineno (ival, lineno=sym->lineDef);
311 // check if this is not a constant expression
312 if (!constExprTree(ival)) {
313 werror (E_CONST_EXPECTED, "found expression");
314 // but try to do it anyway
317 eBBlockFromiCode (iCodeFromAst (ival));
324 /* if is has an absolute address then generate
325 an equate for this no need to allocate space */
326 if (SPEC_ABSA (sym->etype))
330 fprintf (map->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
332 if (TARGET_IS_XA51) {
335 } else if (map==bit || map==sfrbit) {
339 fprintf (map->oFile, "%s\t%s\t0x%04x\n",
341 SPEC_ADDR (sym->etype));
344 int size = getSize (sym->type);
346 werror(E_UNKNOWN_SIZE,sym->name);
350 fprintf (map->oFile, "==.\n");
352 if (IS_STATIC (sym->etype))
353 tfprintf (map->oFile, "!slabeldef\n", sym->rname);
355 tfprintf (map->oFile, "!labeldef\n", sym->rname);
356 tfprintf (map->oFile, "\t!ds\n",
357 (unsigned int) size & 0xffff);
362 /*-----------------------------------------------------------------*/
363 /* initPointer - pointer initialization code massaging */
364 /*-----------------------------------------------------------------*/
366 initPointer (initList * ilist, sym_link *toType)
369 ast *expr = list2expr (ilist);
374 /* try it the oldway first */
375 if ((val = constExprValue (expr, FALSE)))
378 /* ( ptr + constant ) */
379 if (IS_AST_OP (expr) &&
380 (expr->opval.op == '+' || expr->opval.op == '-') &&
381 IS_AST_SYM_VALUE (expr->left) &&
382 (IS_ARRAY(expr->left->ftype) || IS_PTR(expr->left->ftype)) &&
383 compareType(toType, expr->left->ftype) &&
384 IS_AST_LIT_VALUE (expr->right)) {
385 return valForCastAggr (expr->left, expr->left->ftype,
391 if (IS_AST_OP(expr) && expr->opval.op==CAST &&
392 IS_AST_OP(expr->right) && expr->right->opval.op=='&') {
393 if (compareType(toType, expr->left->ftype)!=1) {
394 werror (W_INIT_WRONG);
395 printFromToType(expr->left->ftype, toType);
401 /* no then we have to do these cludgy checks */
402 /* pointers can be initialized with address of
403 a variable or address of an array element */
404 if (IS_AST_OP (expr) && expr->opval.op == '&') {
405 /* address of symbol */
406 if (IS_AST_SYM_VALUE (expr->left)) {
407 val = copyValue (AST_VALUE (expr->left));
408 val->type = newLink ();
409 if (SPEC_SCLS (expr->left->etype) == S_CODE) {
410 DCL_TYPE (val->type) = CPOINTER;
411 DCL_PTR_CONST (val->type) = port->mem.code_ro;
413 else if (SPEC_SCLS (expr->left->etype) == S_XDATA)
414 DCL_TYPE (val->type) = FPOINTER;
415 else if (SPEC_SCLS (expr->left->etype) == S_XSTACK)
416 DCL_TYPE (val->type) = PPOINTER;
417 else if (SPEC_SCLS (expr->left->etype) == S_IDATA)
418 DCL_TYPE (val->type) = IPOINTER;
419 else if (SPEC_SCLS (expr->left->etype) == S_EEPROM)
420 DCL_TYPE (val->type) = EEPPOINTER;
422 DCL_TYPE (val->type) = POINTER;
423 val->type->next = expr->left->ftype;
424 val->etype = getSpec (val->type);
428 /* if address of indexed array */
429 if (IS_AST_OP (expr->left) && expr->left->opval.op == '[')
430 return valForArray (expr->left);
432 /* if address of structure element then
434 if (IS_AST_OP (expr->left) &&
435 expr->left->opval.op == '.') {
436 return valForStructElem (expr->left->left,
441 (&some_struct)->element */
442 if (IS_AST_OP (expr->left) &&
443 expr->left->opval.op == PTR_OP &&
444 IS_ADDRESS_OF_OP (expr->left->left)) {
445 return valForStructElem (expr->left->left->left,
449 /* case 3. (((char *) &a) +/- constant) */
450 if (IS_AST_OP (expr) &&
451 (expr->opval.op == '+' || expr->opval.op == '-') &&
452 IS_AST_OP (expr->left) && expr->left->opval.op == CAST &&
453 IS_AST_OP (expr->left->right) &&
454 expr->left->right->opval.op == '&' &&
455 IS_AST_LIT_VALUE (expr->right)) {
457 return valForCastAggr (expr->left->right->left,
458 expr->left->left->opval.lnk,
459 expr->right, expr->opval.op);
462 /* case 4. (char *)(array type) */
463 if (IS_CAST_OP(expr) && IS_AST_SYM_VALUE (expr->right) &&
464 IS_ARRAY(expr->right->ftype)) {
466 val = copyValue (AST_VALUE (expr->right));
467 val->type = newLink ();
468 if (SPEC_SCLS (expr->right->etype) == S_CODE) {
469 DCL_TYPE (val->type) = CPOINTER;
470 DCL_PTR_CONST (val->type) = port->mem.code_ro;
472 else if (SPEC_SCLS (expr->right->etype) == S_XDATA)
473 DCL_TYPE (val->type) = FPOINTER;
474 else if (SPEC_SCLS (expr->right->etype) == S_XSTACK)
475 DCL_TYPE (val->type) = PPOINTER;
476 else if (SPEC_SCLS (expr->right->etype) == S_IDATA)
477 DCL_TYPE (val->type) = IPOINTER;
478 else if (SPEC_SCLS (expr->right->etype) == S_EEPROM)
479 DCL_TYPE (val->type) = EEPPOINTER;
481 DCL_TYPE (val->type) = POINTER;
482 val->type->next = expr->right->ftype->next;
483 val->etype = getSpec (val->type);
487 werror (E_INCOMPAT_PTYPES);
492 /*-----------------------------------------------------------------*/
493 /* printChar - formats and prints a characater string with DB */
494 /*-----------------------------------------------------------------*/
496 printChar (FILE * ofile, char *s, int plen)
499 int len = strlen (s);
504 while (len && pplen < plen)
507 while (i && *s && pplen < plen)
509 if (*s < ' ' || *s == '\"' || *s=='\\')
513 tfprintf (ofile, "\t!ascii\n", buf);
514 tfprintf (ofile, "\t!db !constbyte\n", (unsigned char)*s);
529 tfprintf (ofile, "\t!ascii\n", buf);
538 tfprintf (ofile, "\t!db !constbyte\n", 0);
541 /*-----------------------------------------------------------------*/
542 /* return the generic pointer high byte for a given pointer type. */
543 /*-----------------------------------------------------------------*/
545 pointerTypeToGPByte (const int p_type, const char *iname, const char *oname)
553 werror (E_CANNOT_USE_GENERIC_POINTER,
554 iname ? iname : "<null>",
555 oname ? oname : "<null>");
562 return GPTYPE_XSTACK;
564 fprintf (stderr, "*** internal error: unknown pointer type %d in GPByte.\n",
572 /*-----------------------------------------------------------------*/
573 /* printPointerType - generates ival for pointer type */
574 /*-----------------------------------------------------------------*/
576 _printPointerType (FILE * oFile, const char *name)
578 /* if (TARGET_IS_DS390) */
579 if (options.model == MODEL_FLAT24)
581 fprintf (oFile, "\t.byte %s,(%s >> 8),(%s >> 16)", name, name, name);
585 fprintf (oFile, "\t.byte %s,(%s >> 8)", name, name);
589 /*-----------------------------------------------------------------*/
590 /* printPointerType - generates ival for pointer type */
591 /*-----------------------------------------------------------------*/
593 printPointerType (FILE * oFile, const char *name)
595 _printPointerType (oFile, name);
596 fprintf (oFile, "\n");
599 /*-----------------------------------------------------------------*/
600 /* printGPointerType - generates ival for generic pointer type */
601 /*-----------------------------------------------------------------*/
603 printGPointerType (FILE * oFile, const char *iname, const char *oname,
604 const unsigned int type)
606 _printPointerType (oFile, iname);
607 fprintf (oFile, ",#0x%02x\n", pointerTypeToGPByte (type, iname, oname));
610 /*-----------------------------------------------------------------*/
611 /* printIvalType - generates ival for int/char */
612 /*-----------------------------------------------------------------*/
614 printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile)
618 /* if initList is deep */
619 if (ilist->type == INIT_DEEP)
620 ilist = ilist->init.deep;
622 if (!IS_AGGREGATE(sym->type) && getNelements(type, ilist)>1) {
623 werror (W_EXCESS_INITIALIZERS, "scalar", sym->name, sym->lineDef);
626 if (!(val = list2val (ilist))) {
627 // assuming a warning has been thrown
631 if (val->type != type) {
632 val = valCastLiteral(type, floatFromVal(val));
635 switch (getSize (type)) {
638 tfprintf (oFile, "\t!db !constbyte\n", 0);
640 tfprintf (oFile, "\t!dbs\n",
641 aopLiteral (val, 0));
645 if (port->use_dw_for_init)
646 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
648 fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
652 tfprintf (oFile, "\t!dw !constword\n", 0);
653 tfprintf (oFile, "\t!dw !constword\n", 0);
656 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
657 aopLiteral (val, 0), aopLiteral (val, 1),
658 aopLiteral (val, 2), aopLiteral (val, 3));
664 /*-----------------------------------------------------------------*/
665 /* printIvalBitFields - generate initializer for bitfields */
666 /*-----------------------------------------------------------------*/
667 void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile)
671 initList *lilist = *ilist ;
672 unsigned long ival = 0;
678 val = list2val(lilist);
680 if (SPEC_BLEN(lsym->etype) > 8) {
681 size += ((SPEC_BLEN (lsym->etype) / 8) +
682 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
685 size = ((SPEC_BLEN (lsym->etype) / 8) +
686 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
688 i = (unsigned long)floatFromVal(val);
689 i <<= SPEC_BSTR (lsym->etype);
691 if (! ( lsym->next &&
692 (IS_BITFIELD(lsym->next->type)) &&
693 (SPEC_BSTR(lsym->next->etype)))) break;
695 lilist = lilist->next;
699 tfprintf (oFile, "\t!db !constbyte\n",ival);
703 tfprintf (oFile, "\t!dw !constword\n",ival);
706 tfprintf (oFile, "\t!db !constword,!constword\n",
707 (ival >> 8) & 0xffff, (ival & 0xffff));
714 /*-----------------------------------------------------------------*/
715 /* printIvalStruct - generates initial value for structures */
716 /*-----------------------------------------------------------------*/
718 printIvalStruct (symbol * sym, sym_link * type,
719 initList * ilist, FILE * oFile)
724 sflds = SPEC_STRUCT (type)->fields;
725 if (ilist->type != INIT_DEEP) {
726 werror (E_INIT_STRUCT, sym->name);
730 iloop = ilist->init.deep;
732 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
733 if (IS_BITFIELD(sflds->type)) {
734 printIvalBitFields(&sflds,&iloop,oFile);
736 printIval (sym, sflds->type, iloop, oFile);
740 werror (W_EXCESS_INITIALIZERS, "struct", sym->name, sym->lineDef);
745 /*-----------------------------------------------------------------*/
746 /* printIvalChar - generates initital value for character array */
747 /*-----------------------------------------------------------------*/
749 printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
757 val = list2val (ilist);
758 /* if the value is a character string */
759 if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
761 if (!DCL_ELEM (type))
762 DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
764 printChar (oFile, SPEC_CVAL (val->etype).v_char, DCL_ELEM (type));
766 if ((remain = (DCL_ELEM (type) - strlen (SPEC_CVAL (val->etype).v_char) - 1)) > 0)
768 tfprintf (oFile, "\t!db !constbyte\n", 0);
776 printChar (oFile, s, strlen (s) + 1);
780 /*-----------------------------------------------------------------*/
781 /* printIvalArray - generates code for array initialization */
782 /*-----------------------------------------------------------------*/
784 printIvalArray (symbol * sym, sym_link * type, initList * ilist,
788 int lcnt = 0, size = 0;
791 /* take care of the special case */
792 /* array of characters can be init */
794 if (IS_CHAR (type->next)) {
795 if (!IS_LITERAL(list2val(ilist)->etype)) {
796 werror (E_CONST_EXPECTED);
799 if (printIvalChar (type,
800 (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
801 oFile, SPEC_CVAL (sym->etype).v_char))
804 /* not the special case */
805 if (ilist->type != INIT_DEEP)
807 werror (E_INIT_STRUCT, sym->name);
811 iloop = ilist->init.deep;
812 lcnt = DCL_ELEM (type);
813 for (last_type = type->next; last_type && DCL_ELEM (last_type); last_type = last_type->next)
814 lcnt *= DCL_ELEM (last_type);
819 printIval (sym, type->next, iloop, oFile);
820 iloop = (iloop ? iloop->next : NULL);
823 /* if not array limits given & we */
824 /* are out of initialisers then */
825 if (!DCL_ELEM (type) && !iloop)
828 /* no of elements given and we */
829 /* have generated for all of them */
831 /* if initializers left */
833 werror (W_EXCESS_INITIALIZERS, "array", sym->name, sym->lineDef);
839 /* if we have not been given a size */
840 if (!DCL_ELEM (type))
841 DCL_ELEM (type) = size;
846 /*-----------------------------------------------------------------*/
847 /* printIvalFuncPtr - generate initial value for function pointers */
848 /*-----------------------------------------------------------------*/
850 printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile)
855 val = list2val (ilist);
858 // an error has been thrown allready
862 if (IS_LITERAL(val->etype)) {
863 if (compareType(type,val->etype)==0) {
864 werror (E_INCOMPAT_TYPES);
865 printFromToType (val->type, type);
867 printIvalCharPtr (NULL, type, val, oFile);
871 /* check the types */
872 if ((dLvl = compareType (val->type, type->next)) <= 0)
874 tfprintf (oFile, "\t!dw !constword\n", 0);
878 /* now generate the name */
881 if (port->use_dw_for_init)
883 tfprintf (oFile, "\t!dws\n", val->name);
887 printPointerType (oFile, val->name);
890 else if (port->use_dw_for_init)
892 tfprintf (oFile, "\t!dws\n", val->sym->rname);
896 printPointerType (oFile, val->sym->rname);
902 /*-----------------------------------------------------------------*/
903 /* printIvalCharPtr - generates initial values for character pointers */
904 /*-----------------------------------------------------------------*/
906 printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile)
910 /* PENDING: this is _very_ mcs51 specific, including a magic
912 It's also endin specific.
914 size = getSize (type);
916 if (val->name && strlen (val->name))
918 if (size == 1) /* This appears to be Z80 specific?? */
921 "\t!dbs\n", val->name);
923 else if (size == FPTRSIZE)
925 if (port->use_dw_for_init)
927 tfprintf (oFile, "\t!dws\n", val->name);
931 printPointerType (oFile, val->name);
934 else if (size == GPTRSIZE)
937 if (IS_PTR (val->type)) {
938 type = DCL_TYPE (val->type);
940 type = PTR_TYPE (SPEC_OCLS (val->etype));
942 if (val->sym && val->sym->isstrlit) {
943 // this is a literal string
946 printGPointerType (oFile, val->name, sym->name, type);
950 fprintf (stderr, "*** internal error: unknown size in "
951 "printIvalCharPtr.\n");
956 // these are literals assigned to pointers
960 tfprintf (oFile, "\t!dbs\n", aopLiteral (val, 0));
963 if (port->use_dw_for_init)
964 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, size));
966 tfprintf (oFile, "\t.byte %s,%s\n",
967 aopLiteral (val, 0), aopLiteral (val, 1));
970 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
971 // non-zero mcs51 generic pointer
972 werror (E_LITERAL_GENERIC);
974 fprintf (oFile, "\t.byte %s,%s,%s\n",
977 aopLiteral (val, 2));
980 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
981 // non-zero ds390 generic pointer
982 werror (E_LITERAL_GENERIC);
984 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
988 aopLiteral (val, 3));
995 if (val->sym && val->sym->isstrlit && !isinSet(statsg->syms, val->sym)) {
996 addSet (&statsg->syms, val->sym);
1002 /*-----------------------------------------------------------------*/
1003 /* printIvalPtr - generates initial value for pointers */
1004 /*-----------------------------------------------------------------*/
1006 printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
1012 if (ilist->type == INIT_DEEP)
1013 ilist = ilist->init.deep;
1015 /* function pointer */
1016 if (IS_FUNC (type->next))
1018 printIvalFuncPtr (type, ilist, oFile);
1022 if (!(val = initPointer (ilist, type)))
1025 /* if character pointer */
1026 if (IS_CHAR (type->next))
1027 if (printIvalCharPtr (sym, type, val, oFile))
1030 /* check the type */
1031 if (compareType (type, val->type) == 0) {
1032 werror (W_INIT_WRONG);
1033 printFromToType (val->type, type);
1036 /* if val is literal */
1037 if (IS_LITERAL (val->etype))
1039 switch (getSize (type))
1042 tfprintf (oFile, "\t!db !constbyte\n", (unsigned int) floatFromVal (val) & 0xff);
1045 if (port->use_dw_for_init)
1046 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
1048 tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
1050 case 3: // how about '390??
1051 fprintf (oFile, "\t.byte %s,%s,#0x%d\n",
1052 aopLiteral (val, 0), aopLiteral (val, 1), GPTYPE_CODE);
1058 size = getSize (type);
1060 if (size == 1) /* Z80 specific?? */
1062 tfprintf (oFile, "\t!dbs\n", val->name);
1064 else if (size == FPTRSIZE)
1066 if (port->use_dw_for_init) {
1067 tfprintf (oFile, "\t!dws\n", val->name);
1069 printPointerType (oFile, val->name);
1072 else if (size == GPTRSIZE)
1074 printGPointerType (oFile, val->name, sym->name,
1075 (IS_PTR (val->type) ? DCL_TYPE (val->type) :
1076 PTR_TYPE (SPEC_OCLS (val->etype))));
1081 /*-----------------------------------------------------------------*/
1082 /* printIval - generates code for initial value */
1083 /*-----------------------------------------------------------------*/
1085 printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
1090 /* update line number for error msgs */
1091 lineno=sym->lineDef;
1093 /* if structure then */
1094 if (IS_STRUCT (type))
1096 printIvalStruct (sym, type, ilist, oFile);
1100 /* if this is a pointer */
1103 printIvalPtr (sym, type, ilist, oFile);
1107 /* if this is an array */
1108 if (IS_ARRAY (type))
1110 printIvalArray (sym, type, ilist, oFile);
1114 /* if type is SPECIFIER */
1117 printIvalType (sym, type, ilist, oFile);
1122 /*-----------------------------------------------------------------*/
1123 /* emitStaticSeg - emitcode for the static segment */
1124 /*-----------------------------------------------------------------*/
1126 emitStaticSeg (memmap * map, FILE * out)
1130 /* fprintf(out, "\t.area\t%s\n", map->sname); */
1132 /* for all variables in this segment do */
1133 for (sym = setFirstItem (map->syms); sym;
1134 sym = setNextItem (map->syms))
1137 /* if it is "extern" then do nothing */
1138 if (IS_EXTERN (sym->etype))
1141 /* if it is not static add it to the public
1143 if (!IS_STATIC (sym->etype))
1145 addSetHead (&publics, sym);
1148 /* print extra debug info if required */
1149 if (options.debug) {
1150 cdbSymbol (sym, cdbFile, FALSE, FALSE);
1153 if (IS_STATIC (sym->etype))
1154 fprintf (out, "F%s$", moduleName); /* scope is file */
1156 fprintf (out, "G$"); /* scope is global */
1159 /* symbol is local */
1160 fprintf (out, "L%s$",
1161 (sym->localof ? sym->localof->name : "-null-"));
1162 fprintf (out, "%s$%d$%d", sym->name, sym->level, sym->block);
1165 /* if it has an absolute address */
1166 if (SPEC_ABSA (sym->etype))
1169 fprintf (out, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1171 fprintf (out, "%s\t=\t0x%04x\n",
1173 SPEC_ADDR (sym->etype));
1178 fprintf (out, " == .\n");
1180 /* if it has an initial value */
1183 fprintf (out, "%s:\n", sym->rname);
1185 resolveIvalSym (sym->ival);
1186 printIval (sym, sym->type, sym->ival, out);
1188 /* if sym is a simple string and sym->ival is a string,
1189 WE don't need it anymore */
1190 if (IS_ARRAY(sym->type) && IS_CHAR(sym->type->next) &&
1191 IS_AST_SYM_VALUE(list2expr(sym->ival)) &&
1192 list2val(sym->ival)->sym->isstrlit) {
1193 freeStringSymbol(list2val(sym->ival)->sym);
1197 /* allocate space */
1198 int size = getSize (sym->type);
1201 werror(E_UNKNOWN_SIZE,sym->name);
1203 fprintf (out, "%s:\n", sym->rname);
1204 /* special case for character strings */
1205 if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) &&
1206 SPEC_CVAL (sym->etype).v_char)
1208 SPEC_CVAL (sym->etype).v_char,
1209 strlen (SPEC_CVAL (sym->etype).v_char) + 1);
1211 tfprintf (out, "\t!ds\n", (unsigned int) size & 0xffff);
1217 /*-----------------------------------------------------------------*/
1218 /* emitMaps - emits the code for the data portion the code */
1219 /*-----------------------------------------------------------------*/
1224 /* no special considerations for the following
1225 data, idata & bit & xdata */
1226 emitRegularMap (data, TRUE, TRUE);
1227 emitRegularMap (idata, TRUE, TRUE);
1228 emitRegularMap (bit, TRUE, FALSE);
1229 emitRegularMap (xdata, TRUE, TRUE);
1230 if (port->genXINIT) {
1231 emitRegularMap (xidata, TRUE, TRUE);
1233 emitRegularMap (sfr, FALSE, FALSE);
1234 emitRegularMap (sfrbit, FALSE, FALSE);
1235 emitRegularMap (home, TRUE, FALSE);
1236 emitRegularMap (code, TRUE, FALSE);
1238 emitStaticSeg (statsg, code->oFile);
1239 if (port->genXINIT) {
1240 tfprintf (code->oFile, "\t!area\n", xinit->sname);
1241 emitStaticSeg (xinit, code->oFile);
1246 /*-----------------------------------------------------------------*/
1247 /* flushStatics - flush all currently defined statics out to file */
1248 /* and delete. Temporary function */
1249 /*-----------------------------------------------------------------*/
1253 emitStaticSeg (statsg, codeOutFile);
1254 statsg->syms = NULL;
1257 /*-----------------------------------------------------------------*/
1258 /* createInterruptVect - creates the interrupt vector */
1259 /*-----------------------------------------------------------------*/
1261 createInterruptVect (FILE * vFile)
1264 mainf = newSymbol ("main", 0);
1267 /* only if the main function exists */
1268 if (!(mainf = findSymWithLevel (SymbolTab, mainf)))
1270 if (!options.cc_only && !noAssemble && !options.c1mode)
1275 /* if the main is only a prototype ie. no body then do nothing */
1276 if (!IFFUNC_HASBODY(mainf->type))
1278 /* if ! compile only then main function should be present */
1279 if (!options.cc_only && !noAssemble)
1284 tfprintf (vFile, "\t!areacode\n", CODE_NAME);
1285 fprintf (vFile, "__interrupt_vect:\n");
1288 if (!port->genIVT || !(port->genIVT (vFile, interrupts, maxInterrupts)))
1290 /* "generic" interrupt table header (if port doesn't specify one).
1291 * Look suspiciously like 8051 code to me...
1294 fprintf (vFile, "\tljmp\t__sdcc_gsinit_startup\n");
1297 /* now for the other interrupts */
1298 for (; i < maxInterrupts; i++)
1301 fprintf (vFile, "\tljmp\t%s\n\t.ds\t5\n", interrupts[i]->rname);
1303 fprintf (vFile, "\treti\n\t.ds\t7\n");
1310 ";--------------------------------------------------------\n"
1311 "; File Created by SDCC : FreeWare ANSI-C Compiler\n"};
1315 ";--------------------------------------------------------\n"};
1318 /*-----------------------------------------------------------------*/
1319 /* initialComments - puts in some initial comments */
1320 /*-----------------------------------------------------------------*/
1322 initialComments (FILE * afile)
1326 fprintf (afile, "%s", iComments1);
1327 fprintf (afile, "; Version %s %s\n", VersionString, asctime (localtime (&t)));
1328 fprintf (afile, "%s", iComments2);
1331 /*-----------------------------------------------------------------*/
1332 /* printPublics - generates .global for publics */
1333 /*-----------------------------------------------------------------*/
1335 printPublics (FILE * afile)
1339 fprintf (afile, "%s", iComments2);
1340 fprintf (afile, "; Public variables in this module\n");
1341 fprintf (afile, "%s", iComments2);
1343 for (sym = setFirstItem (publics); sym;
1344 sym = setNextItem (publics))
1345 tfprintf (afile, "\t!global\n", sym->rname);
1348 /*-----------------------------------------------------------------*/
1349 /* printExterns - generates .global for externs */
1350 /*-----------------------------------------------------------------*/
1352 printExterns (FILE * afile)
1356 fprintf (afile, "%s", iComments2);
1357 fprintf (afile, "; Externals used\n");
1358 fprintf (afile, "%s", iComments2);
1360 for (sym = setFirstItem (externs); sym;
1361 sym = setNextItem (externs))
1362 tfprintf (afile, "\t!extern\n", sym->rname);
1365 /*-----------------------------------------------------------------*/
1366 /* emitOverlay - will emit code for the overlay stuff */
1367 /*-----------------------------------------------------------------*/
1369 emitOverlay (FILE * afile)
1373 if (!elementsInSet (ovrSetSets))
1374 tfprintf (afile, "\t!area\n", port->mem.overlay_name);
1376 /* for each of the sets in the overlay segment do */
1377 for (ovrset = setFirstItem (ovrSetSets); ovrset;
1378 ovrset = setNextItem (ovrSetSets))
1383 if (elementsInSet (ovrset))
1385 /* output the area informtion */
1386 fprintf (afile, "\t.area\t%s\n", port->mem.overlay_name); /* MOF */
1389 for (sym = setFirstItem (ovrset); sym;
1390 sym = setNextItem (ovrset))
1392 /* if extern then it is in the publics table: do nothing */
1393 if (IS_EXTERN (sym->etype))
1396 /* if allocation required check is needed
1397 then check if the symbol really requires
1398 allocation only for local variables */
1399 if (!IS_AGGREGATE (sym->type) &&
1400 !(sym->_isparm && !IS_REGPARM (sym->etype))
1401 && !sym->allocreq && sym->level)
1404 /* if global variable & not static or extern
1405 and addPublics allowed then add it to the public set */
1406 if ((sym->_isparm && !IS_REGPARM (sym->etype))
1407 && !IS_STATIC (sym->etype))
1409 addSetHead (&publics, sym);
1412 /* if extern then do nothing or is a function
1414 if (IS_FUNC (sym->type))
1417 /* print extra debug info if required */
1420 cdbSymbol (sym, cdbFile, FALSE, FALSE);
1424 if (IS_STATIC (sym->etype))
1425 fprintf (afile, "F%s$", moduleName); /* scope is file */
1427 fprintf (afile, "G$"); /* scope is global */
1430 /* symbol is local */
1431 fprintf (afile, "L%s$",
1432 (sym->localof ? sym->localof->name : "-null-"));
1433 fprintf (afile, "%s$%d$%d", sym->name, sym->level, sym->block);
1436 /* if is has an absolute address then generate
1437 an equate for this no need to allocate space */
1438 if (SPEC_ABSA (sym->etype))
1442 fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1444 fprintf (afile, "%s\t=\t0x%04x\n",
1446 SPEC_ADDR (sym->etype));
1449 int size = getSize(sym->type);
1452 werror(E_UNKNOWN_SIZE,sym->name);
1455 fprintf (afile, "==.\n");
1457 /* allocate space */
1458 tfprintf (afile, "!labeldef\n", sym->rname);
1459 tfprintf (afile, "\t!ds\n", (unsigned int) getSize (sym->type) & 0xffff);
1466 /*-----------------------------------------------------------------*/
1467 /* glue - the final glue that hold the whole thing together */
1468 /*-----------------------------------------------------------------*/
1474 FILE *ovrFile = tempfile ();
1476 addSetHead (&tmpfileSet, ovrFile);
1477 /* print the global struct definitions */
1479 cdbStructBlock (0, cdbFile);
1481 vFile = tempfile ();
1482 /* PENDING: this isnt the best place but it will do */
1483 if (port->general.glue_up_main)
1485 /* create the interrupt vector table */
1486 createInterruptVect (vFile);
1489 addSetHead (&tmpfileSet, vFile);
1491 /* emit code for the all the variables declared */
1493 /* do the overlay segments */
1494 emitOverlay (ovrFile);
1496 /* now put it all together into the assembler file */
1497 /* create the assembler file name */
1499 /* -o option overrides default name? */
1500 if ((noAssemble || options.c1mode) && fullDstFileName)
1502 strncpyz (scratchFileName, fullDstFileName, PATH_MAX);
1506 strncpyz (scratchFileName, dstFileName, PATH_MAX);
1507 strncatz (scratchFileName, port->assembler.file_ext, PATH_MAX);
1510 if (!(asmFile = fopen (scratchFileName, "w")))
1512 werror (E_FILE_OPEN_ERR, scratchFileName);
1516 /* initial comments */
1517 initialComments (asmFile);
1519 /* print module name */
1520 tfprintf (asmFile, "\t!module\n", moduleName);
1521 tfprintf (asmFile, "\t!fileprelude\n");
1523 /* Let the port generate any global directives, etc. */
1524 if (port->genAssemblerPreamble)
1526 port->genAssemblerPreamble (asmFile);
1529 /* print the global variables in this module */
1530 printPublics (asmFile);
1531 if (port->assembler.externGlobal)
1532 printExterns (asmFile);
1534 /* copy the sfr segment */
1535 fprintf (asmFile, "%s", iComments2);
1536 fprintf (asmFile, "; special function registers\n");
1537 fprintf (asmFile, "%s", iComments2);
1538 copyFile (asmFile, sfr->oFile);
1540 /* copy the sbit segment */
1541 fprintf (asmFile, "%s", iComments2);
1542 fprintf (asmFile, "; special function bits \n");
1543 fprintf (asmFile, "%s", iComments2);
1544 copyFile (asmFile, sfrbit->oFile);
1546 /*JCF: Create the areas for the register banks*/
1547 if(port->general.glue_up_main &&
1548 (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_XA51))
1550 if(RegBankUsed[0]||RegBankUsed[1]||RegBankUsed[2]||RegBankUsed[3])
1552 fprintf (asmFile, "%s", iComments2);
1553 fprintf (asmFile, "; overlayable register banks \n");
1554 fprintf (asmFile, "%s", iComments2);
1556 fprintf (asmFile, "\t.area REG_BANK_0\t(REL,OVR,DATA)\n\t.ds 8\n");
1557 if(RegBankUsed[1]||options.parms_in_bank1)
1558 fprintf (asmFile, "\t.area REG_BANK_1\t(REL,OVR,DATA)\n\t.ds 8\n");
1560 fprintf (asmFile, "\t.area REG_BANK_2\t(REL,OVR,DATA)\n\t.ds 8\n");
1562 fprintf (asmFile, "\t.area REG_BANK_3\t(REL,OVR,DATA)\n\t.ds 8\n");
1566 /* copy the data segment */
1567 fprintf (asmFile, "%s", iComments2);
1568 fprintf (asmFile, "; internal ram data\n");
1569 fprintf (asmFile, "%s", iComments2);
1570 copyFile (asmFile, data->oFile);
1573 /* create the overlay segments */
1575 fprintf (asmFile, "%s", iComments2);
1576 fprintf (asmFile, "; overlayable items in internal ram \n");
1577 fprintf (asmFile, "%s", iComments2);
1578 copyFile (asmFile, ovrFile);
1581 /* create the stack segment MOF */
1582 if (mainf && IFFUNC_HASBODY(mainf->type))
1584 fprintf (asmFile, "%s", iComments2);
1585 fprintf (asmFile, "; Stack segment in internal ram \n");
1586 fprintf (asmFile, "%s", iComments2);
1587 fprintf (asmFile, "\t.area\tSSEG\t(DATA)\n"
1588 "__start__stack:\n\t.ds\t1\n\n");
1591 /* create the idata segment */
1593 fprintf (asmFile, "%s", iComments2);
1594 fprintf (asmFile, "; indirectly addressable internal ram data\n");
1595 fprintf (asmFile, "%s", iComments2);
1596 copyFile (asmFile, idata->oFile);
1599 /* copy the bit segment */
1600 fprintf (asmFile, "%s", iComments2);
1601 fprintf (asmFile, "; bit data\n");
1602 fprintf (asmFile, "%s", iComments2);
1603 copyFile (asmFile, bit->oFile);
1605 /* if external stack then reserve space of it */
1606 if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack)
1608 fprintf (asmFile, "%s", iComments2);
1609 fprintf (asmFile, "; external stack \n");
1610 fprintf (asmFile, "%s", iComments2);
1611 fprintf (asmFile, "\t.area XSEG (XDATA)\n"); /* MOF */
1612 fprintf (asmFile, "\t.ds 256\n");
1616 /* copy xtern ram data */
1617 fprintf (asmFile, "%s", iComments2);
1618 fprintf (asmFile, "; external ram data\n");
1619 fprintf (asmFile, "%s", iComments2);
1620 copyFile (asmFile, xdata->oFile);
1622 /* copy xternal initialized ram data */
1623 fprintf (asmFile, "%s", iComments2);
1624 fprintf (asmFile, "; external initialized ram data\n");
1625 fprintf (asmFile, "%s", iComments2);
1626 copyFile (asmFile, xidata->oFile);
1628 /* copy the interrupt vector table */
1629 if (mainf && IFFUNC_HASBODY(mainf->type))
1631 fprintf (asmFile, "%s", iComments2);
1632 fprintf (asmFile, "; interrupt vector \n");
1633 fprintf (asmFile, "%s", iComments2);
1634 copyFile (asmFile, vFile);
1637 /* copy global & static initialisations */
1638 fprintf (asmFile, "%s", iComments2);
1639 fprintf (asmFile, "; global & static initialisations\n");
1640 fprintf (asmFile, "%s", iComments2);
1642 /* Everywhere we generate a reference to the static_name area,
1643 * (which is currently only here), we immediately follow it with a
1644 * definition of the post_static_name area. This guarantees that
1645 * the post_static_name area will immediately follow the static_name
1648 tfprintf (asmFile, "\t!area\n", port->mem.static_name); /* MOF */
1649 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1650 tfprintf (asmFile, "\t!area\n", port->mem.static_name);
1652 if (mainf && IFFUNC_HASBODY(mainf->type))
1654 fprintf (asmFile, "__sdcc_gsinit_startup:\n");
1655 /* if external stack is specified then the
1656 higher order byte of the xdatalocation is
1657 going into P2 and the lower order going into
1659 if (options.useXstack)
1661 fprintf (asmFile, "\tmov\tP2,#0x%02x\n",
1662 (((unsigned int) options.xdata_loc) >> 8) & 0xff);
1663 fprintf (asmFile, "\tmov\t_spx,#0x%02x\n",
1664 (unsigned int) options.xdata_loc & 0xff);
1667 /* initialise the stack pointer. JCF: aslink takes care of the location */
1668 fprintf (asmFile, "\tmov\tsp,#__start__stack - 1\n"); /* MOF */
1670 fprintf (asmFile, "\tlcall\t__sdcc_external_startup\n");
1671 fprintf (asmFile, "\tmov\ta,dpl\n");
1672 fprintf (asmFile, "\tjz\t__sdcc_init_data\n");
1673 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1674 fprintf (asmFile, "__sdcc_init_data:\n");
1676 // if the port can copy the XINIT segment to XISEG
1677 if (port->genXINIT) {
1678 port->genXINIT(asmFile);
1682 copyFile (asmFile, statsg->oFile);
1684 if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type))
1686 /* This code is generated in the post-static area.
1687 * This area is guaranteed to follow the static area
1688 * by the ugly shucking and jiving about 20 lines ago.
1690 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1691 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1697 "%s", iComments2, iComments2);
1698 tfprintf (asmFile, "\t!areahome\n", HOME_NAME);
1699 copyFile (asmFile, home->oFile);
1701 /* copy over code */
1702 fprintf (asmFile, "%s", iComments2);
1703 fprintf (asmFile, "; code\n");
1704 fprintf (asmFile, "%s", iComments2);
1705 tfprintf (asmFile, "\t!areacode\n", CODE_NAME);
1706 if (mainf && IFFUNC_HASBODY(mainf->type))
1709 /* entry point @ start of CSEG */
1710 fprintf (asmFile, "__sdcc_program_startup:\n");
1712 /* put in the call to main */
1713 fprintf (asmFile, "\tlcall\t_main\n");
1714 if (options.mainreturn)
1717 fprintf (asmFile, ";\treturn from main ; will return to caller\n");
1718 fprintf (asmFile, "\tret\n");
1724 fprintf (asmFile, ";\treturn from main will lock up\n");
1725 fprintf (asmFile, "\tsjmp .\n");
1728 copyFile (asmFile, code->oFile);
1730 if (port->genAssemblerEnd) {
1731 port->genAssemblerEnd(asmFile);
1739 /** Creates a temporary file with unoque file name
1741 - TMP, TEMP, TMPDIR env. varibles
1742 - if Un*x system: /usr/tmp and /tmp
1743 - root directory using mkstemp() if avaliable
1744 - default location using tempnam()
1747 tempfileandname(char *fname, size_t len)
1749 #define TEMPLATE "sdccXXXXXX"
1750 #define TEMPLATE_LEN ((sizeof TEMPLATE) - 1)
1752 const char *tmpdir = NULL;
1755 if ((tmpdir = getenv ("TMP")) == NULL)
1756 if ((tmpdir = getenv ("TEMP")) == NULL)
1757 tmpdir = getenv ("TMPDIR");
1761 /* try with /usr/tmp and /tmp on Un*x systems */
1762 struct stat statbuf;
1764 if (tmpdir == NULL) {
1765 if (stat("/usr/tmp", &statbuf) != -1)
1766 tmpdir = "/usr/tmp";
1767 else if (stat("/tmp", &statbuf) != -1)
1775 char fnamebuf[PATH_MAX];
1778 if (fname == NULL || len == 0) {
1780 len = sizeof fnamebuf;
1784 name_len = strlen(tmpdir) + 1 + TEMPLATE_LEN;
1786 assert(name_len < len);
1787 if (!(name_len < len)) /* in NDEBUG is defined */
1788 return -1; /* buffer too small, temporary file can not be created */
1790 sprintf(fname, "%s" DIR_SEPARATOR_STRING TEMPLATE, tmpdir);
1793 name_len = TEMPLATE_LEN;
1795 assert(name_len < len);
1796 if (!(name_len < len)) /* in NDEBUG is defined */
1797 return -1; /* buffer too small, temporary file can not be created */
1799 strcpy(fname, TEMPLATE);
1802 fd = mkstemp(fname);
1806 char *name = tempnam(tmpdir, "sdcc");
1809 perror("Can't create temporary file name");
1813 assert(strlen(name) < len);
1814 if (!(strlen(name) < len)) /* in NDEBUG is defined */
1815 return -1; /* buffer too small, temporary file can not be created */
1817 strcpy(fname, name);
1819 fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
1821 fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
1827 perror("Can't create temporary file");
1835 /** Create a temporary file name
1841 static char fnamebuf[PATH_MAX];
1843 if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
1844 fprintf(stderr, "Can't create temporary file name!");
1855 /** Create a temporary file and add it to tmpfileNameSet,
1856 so that it is removed explicitly by rm_tmpfiles()
1857 or implicitly at program extit.
1865 char fnamebuf[PATH_MAX];
1867 if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
1868 fprintf(stderr, "Can't create temporary file!");
1872 tmp = Safe_strdup(fnamebuf);
1874 addSetHead(&tmpfileNameSet, tmp);
1876 if ((fp = fdopen(fd, "w+b")) == NULL) {
1877 perror("Can't create temporary file!");