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);
299 // add it to the "XINIT (CODE)" segment
300 addSet(&xinit->syms, newSym);
303 if (IS_AGGREGATE (sym->type)) {
304 ival = initAggregates (sym, sym->ival, NULL);
306 if (getNelements(sym->type, sym->ival)>1) {
307 werrorfl (filename, sym->lineDef, W_EXCESS_INITIALIZERS, "scalar",
310 ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
311 decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_CHECK));
313 codeOutFile = statsg->oFile;
316 // set ival's lineno to where the symbol was defined
317 setAstLineno (ival, lineno=sym->lineDef);
318 // check if this is not a constant expression
319 if (!constExprTree(ival)) {
320 werror (E_CONST_EXPECTED, "found expression");
321 // but try to do it anyway
324 if (!astErrors(ival))
325 eBBlockFromiCode (iCodeFromAst (ival));
332 /* if is has an absolute address then generate
333 an equate for this no need to allocate space */
334 if (SPEC_ABSA (sym->etype))
338 fprintf (map->oFile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
340 if (TARGET_IS_XA51) {
343 } else if (map==bit || map==sfrbit) {
347 fprintf (map->oFile, "%s\t%s\t0x%04x\n",
349 SPEC_ADDR (sym->etype));
352 int size = getSize (sym->type);
354 werrorfl (filename, sym->lineDef, E_UNKNOWN_SIZE, sym->name);
358 fprintf (map->oFile, "==.\n");
360 if (IS_STATIC (sym->etype))
361 tfprintf (map->oFile, "!slabeldef\n", sym->rname);
363 tfprintf (map->oFile, "!labeldef\n", sym->rname);
364 tfprintf (map->oFile, "\t!ds\n",
365 (unsigned int) size & 0xffff);
370 /*-----------------------------------------------------------------*/
371 /* initPointer - pointer initialization code massaging */
372 /*-----------------------------------------------------------------*/
374 initPointer (initList * ilist, sym_link *toType)
377 ast *expr = list2expr (ilist);
382 /* try it the oldway first */
383 if ((val = constExprValue (expr, FALSE)))
386 /* ( ptr + constant ) */
387 if (IS_AST_OP (expr) &&
388 (expr->opval.op == '+' || expr->opval.op == '-') &&
389 IS_AST_SYM_VALUE (expr->left) &&
390 (IS_ARRAY(expr->left->ftype) || IS_PTR(expr->left->ftype)) &&
391 compareType(toType, expr->left->ftype) &&
392 IS_AST_LIT_VALUE (expr->right)) {
393 return valForCastAggr (expr->left, expr->left->ftype,
399 if (IS_AST_OP(expr) && expr->opval.op==CAST &&
400 IS_AST_OP(expr->right) && expr->right->opval.op=='&') {
401 if (compareType(toType, expr->left->ftype)!=1) {
402 werror (W_INIT_WRONG);
403 printFromToType(expr->left->ftype, toType);
409 /* no then we have to do these cludgy checks */
410 /* pointers can be initialized with address of
411 a variable or address of an array element */
412 if (IS_AST_OP (expr) && expr->opval.op == '&') {
413 /* address of symbol */
414 if (IS_AST_SYM_VALUE (expr->left)) {
415 val = copyValue (AST_VALUE (expr->left));
416 val->type = newLink (DECLARATOR);
417 if (SPEC_SCLS (expr->left->etype) == S_CODE) {
418 DCL_TYPE (val->type) = CPOINTER;
419 DCL_PTR_CONST (val->type) = port->mem.code_ro;
421 else if (SPEC_SCLS (expr->left->etype) == S_XDATA)
422 DCL_TYPE (val->type) = FPOINTER;
423 else if (SPEC_SCLS (expr->left->etype) == S_XSTACK)
424 DCL_TYPE (val->type) = PPOINTER;
425 else if (SPEC_SCLS (expr->left->etype) == S_IDATA)
426 DCL_TYPE (val->type) = IPOINTER;
427 else if (SPEC_SCLS (expr->left->etype) == S_EEPROM)
428 DCL_TYPE (val->type) = EEPPOINTER;
430 DCL_TYPE (val->type) = POINTER;
431 val->type->next = expr->left->ftype;
432 val->etype = getSpec (val->type);
436 /* if address of indexed array */
437 if (IS_AST_OP (expr->left) && expr->left->opval.op == '[')
438 return valForArray (expr->left);
440 /* if address of structure element then
442 if (IS_AST_OP (expr->left) &&
443 expr->left->opval.op == '.') {
444 return valForStructElem (expr->left->left,
449 (&some_struct)->element */
450 if (IS_AST_OP (expr->left) &&
451 expr->left->opval.op == PTR_OP &&
452 IS_ADDRESS_OF_OP (expr->left->left)) {
453 return valForStructElem (expr->left->left->left,
457 /* case 3. (((char *) &a) +/- constant) */
458 if (IS_AST_OP (expr) &&
459 (expr->opval.op == '+' || expr->opval.op == '-') &&
460 IS_AST_OP (expr->left) && expr->left->opval.op == CAST &&
461 IS_AST_OP (expr->left->right) &&
462 expr->left->right->opval.op == '&' &&
463 IS_AST_LIT_VALUE (expr->right)) {
465 return valForCastAggr (expr->left->right->left,
466 expr->left->left->opval.lnk,
467 expr->right, expr->opval.op);
470 /* case 4. (char *)(array type) */
471 if (IS_CAST_OP(expr) && IS_AST_SYM_VALUE (expr->right) &&
472 IS_ARRAY(expr->right->ftype)) {
474 val = copyValue (AST_VALUE (expr->right));
475 val->type = newLink (DECLARATOR);
476 if (SPEC_SCLS (expr->right->etype) == S_CODE) {
477 DCL_TYPE (val->type) = CPOINTER;
478 DCL_PTR_CONST (val->type) = port->mem.code_ro;
480 else if (SPEC_SCLS (expr->right->etype) == S_XDATA)
481 DCL_TYPE (val->type) = FPOINTER;
482 else if (SPEC_SCLS (expr->right->etype) == S_XSTACK)
483 DCL_TYPE (val->type) = PPOINTER;
484 else if (SPEC_SCLS (expr->right->etype) == S_IDATA)
485 DCL_TYPE (val->type) = IPOINTER;
486 else if (SPEC_SCLS (expr->right->etype) == S_EEPROM)
487 DCL_TYPE (val->type) = EEPPOINTER;
489 DCL_TYPE (val->type) = POINTER;
490 val->type->next = expr->right->ftype->next;
491 val->etype = getSpec (val->type);
496 werrorfl (expr->filename, expr->lineno, E_INCOMPAT_PTYPES);
498 werror (E_INCOMPAT_PTYPES);
503 /*-----------------------------------------------------------------*/
504 /* printChar - formats and prints a characater string with DB */
505 /*-----------------------------------------------------------------*/
507 printChar (FILE * ofile, char *s, int plen)
510 int len = strlen (s);
515 while (len && pplen < plen)
518 while (i && pplen < plen)
520 if (*s < ' ' || *s == '\"' || *s=='\\')
524 tfprintf (ofile, "\t!ascii\n", buf);
525 tfprintf (ofile, "\t!db !constbyte\n", (unsigned char)*s);
540 tfprintf (ofile, "\t!ascii\n", buf);
551 tfprintf (ofile, "\t!db !constbyte\n", 0);
556 /*-----------------------------------------------------------------*/
557 /* return the generic pointer high byte for a given pointer type. */
558 /*-----------------------------------------------------------------*/
560 pointerTypeToGPByte (const int p_type, const char *iname, const char *oname)
568 werror (E_CANNOT_USE_GENERIC_POINTER,
569 iname ? iname : "<null>",
570 oname ? oname : "<null>");
577 return GPTYPE_XSTACK;
579 fprintf (stderr, "*** internal error: unknown pointer type %d in GPByte.\n",
587 /*-----------------------------------------------------------------*/
588 /* printPointerType - generates ival for pointer type */
589 /*-----------------------------------------------------------------*/
591 _printPointerType (FILE * oFile, const char *name)
593 if (options.model == MODEL_FLAT24)
595 if (port->little_endian)
596 fprintf (oFile, "\t.byte %s,(%s >> 8),(%s >> 16)", name, name, name);
598 fprintf (oFile, "\t.byte (%s >> 16),(%s >> 8),%s", name, name, name);
602 if (port->little_endian)
603 fprintf (oFile, "\t.byte %s,(%s >> 8)", name, name);
605 fprintf (oFile, "\t.byte (%s >> 8),%s", name, name);
609 /*-----------------------------------------------------------------*/
610 /* printPointerType - generates ival for pointer type */
611 /*-----------------------------------------------------------------*/
613 printPointerType (FILE * oFile, const char *name)
615 _printPointerType (oFile, name);
616 fprintf (oFile, "\n");
619 /*-----------------------------------------------------------------*/
620 /* printGPointerType - generates ival for generic pointer type */
621 /*-----------------------------------------------------------------*/
623 printGPointerType (FILE * oFile, const char *iname, const char *oname,
624 const unsigned int type)
626 _printPointerType (oFile, iname);
627 fprintf (oFile, ",#0x%02x\n", pointerTypeToGPByte (type, iname, oname));
630 /*-----------------------------------------------------------------*/
631 /* printIvalType - generates ival for int/char */
632 /*-----------------------------------------------------------------*/
634 printIvalType (symbol *sym, sym_link * type, initList * ilist, FILE * oFile)
638 /* if initList is deep */
639 if (ilist->type == INIT_DEEP)
640 ilist = ilist->init.deep;
642 if (!(val = list2val (ilist))) {
643 // assuming a warning has been thrown
647 if (val->type != type) {
648 val = valCastLiteral(type, floatFromVal(val));
651 switch (getSize (type)) {
654 tfprintf (oFile, "\t!db !constbyte\n", 0);
656 tfprintf (oFile, "\t!dbs\n",
657 aopLiteral (val, 0));
661 if (port->use_dw_for_init)
662 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
663 else if (port->little_endian)
664 fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
666 fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0));
670 tfprintf (oFile, "\t!dw !constword\n", 0);
671 tfprintf (oFile, "\t!dw !constword\n", 0);
673 else if (port->little_endian) {
674 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
675 aopLiteral (val, 0), aopLiteral (val, 1),
676 aopLiteral (val, 2), aopLiteral (val, 3));
679 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
680 aopLiteral (val, 3), aopLiteral (val, 2),
681 aopLiteral (val, 1), aopLiteral (val, 0));
687 /*-----------------------------------------------------------------*/
688 /* printIvalBitFields - generate initializer for bitfields */
689 /*-----------------------------------------------------------------*/
690 void printIvalBitFields(symbol **sym, initList **ilist, FILE * oFile)
694 initList *lilist = *ilist ;
695 unsigned long ival = 0;
701 val = list2val(lilist);
703 if (SPEC_BLEN(lsym->etype) > 8) {
704 size += ((SPEC_BLEN (lsym->etype) / 8) +
705 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
708 size = ((SPEC_BLEN (lsym->etype) / 8) +
709 (SPEC_BLEN (lsym->etype) % 8 ? 1 : 0));
711 i = (unsigned long)floatFromVal(val);
712 i <<= SPEC_BSTR (lsym->etype);
714 if (! ( lsym->next &&
715 (IS_BITFIELD(lsym->next->type)) &&
716 (SPEC_BSTR(lsym->next->etype)))) break;
718 lilist = lilist->next;
722 tfprintf (oFile, "\t!db !constbyte\n",ival);
726 tfprintf (oFile, "\t!dw !constword\n",ival);
728 case 4: /* EEP: why is this db and not dw? */
729 tfprintf (oFile, "\t!db !constword,!constword\n",
730 (ival >> 8) & 0xffff, (ival & 0xffff));
737 /*-----------------------------------------------------------------*/
738 /* printIvalStruct - generates initial value for structures */
739 /*-----------------------------------------------------------------*/
741 printIvalStruct (symbol * sym, sym_link * type,
742 initList * ilist, FILE * oFile)
747 sflds = SPEC_STRUCT (type)->fields;
748 if (ilist->type != INIT_DEEP) {
749 werrorfl (filename, sym->lineDef, E_INIT_STRUCT, sym->name);
753 iloop = ilist->init.deep;
755 for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL)) {
756 if (IS_BITFIELD(sflds->type)) {
757 printIvalBitFields(&sflds,&iloop,oFile);
759 printIval (sym, sflds->type, iloop, oFile);
763 werrorfl (filename, sym->lineDef, W_EXCESS_INITIALIZERS, "struct", sym->name);
768 /*-----------------------------------------------------------------*/
769 /* printIvalChar - generates initital value for character array */
770 /*-----------------------------------------------------------------*/
772 printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
779 val = list2val (ilist);
780 /* if the value is a character string */
781 if (IS_ARRAY (val->type) && IS_CHAR (val->etype))
783 if (!DCL_ELEM (type))
784 DCL_ELEM (type) = strlen (SPEC_CVAL (val->etype).v_char) + 1;
786 printChar (oFile, SPEC_CVAL (val->etype).v_char, DCL_ELEM (type));
794 printChar (oFile, s, strlen (s) + 1);
798 /*-----------------------------------------------------------------*/
799 /* printIvalArray - generates code for array initialization */
800 /*-----------------------------------------------------------------*/
802 printIvalArray (symbol * sym, sym_link * type, initList * ilist,
808 /* take care of the special case */
809 /* array of characters can be init */
811 if (IS_CHAR (type->next)) {
812 if (!IS_LITERAL(list2val(ilist)->etype)) {
813 werrorfl (filename, ilist->lineno, E_CONST_EXPECTED);
816 if (printIvalChar (type,
817 (ilist->type == INIT_DEEP ? ilist->init.deep : ilist),
818 oFile, SPEC_CVAL (sym->etype).v_char))
821 /* not the special case */
822 if (ilist->type != INIT_DEEP)
824 werrorfl (filename, ilist->lineno, E_INIT_STRUCT, sym->name);
828 for (iloop=ilist->init.deep; iloop; iloop=iloop->next)
830 printIval (sym, type->next, iloop, oFile);
832 if (++size > DCL_ELEM(type)) {
833 werrorfl (filename, sym->lineDef, W_EXCESS_INITIALIZERS, "array", sym->name);
838 if (DCL_ELEM(type)) {
839 // pad with zeros if needed
840 if (size<DCL_ELEM(type)) {
841 size = (DCL_ELEM(type) - size) * getSize(type->next);
843 tfprintf (oFile, "\t!db !constbyte\n", 0);
847 // we have not been given a size, but we now know it
848 DCL_ELEM (type) = size;
854 /*-----------------------------------------------------------------*/
855 /* printIvalFuncPtr - generate initial value for function pointers */
856 /*-----------------------------------------------------------------*/
858 printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile)
863 val = list2val (ilist);
866 // an error has been thrown allready
870 if (IS_LITERAL(val->etype)) {
871 if (compareType(type,val->etype)==0) {
872 werrorfl (filename, ilist->lineno, E_INCOMPAT_TYPES);
873 printFromToType (val->type, type);
875 printIvalCharPtr (NULL, type, val, oFile);
879 /* check the types */
880 if ((dLvl = compareType (val->type, type->next)) <= 0)
882 tfprintf (oFile, "\t!dw !constword\n", 0);
886 /* now generate the name */
889 if (port->use_dw_for_init)
891 tfprintf (oFile, "\t!dws\n", val->name);
895 printPointerType (oFile, val->name);
898 else if (port->use_dw_for_init)
900 tfprintf (oFile, "\t!dws\n", val->sym->rname);
904 printPointerType (oFile, val->sym->rname);
910 /*-----------------------------------------------------------------*/
911 /* printIvalCharPtr - generates initial values for character pointers */
912 /*-----------------------------------------------------------------*/
914 printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile)
918 /* PENDING: this is _very_ mcs51 specific, including a magic
920 It's also endin specific.
922 size = getSize (type);
924 if (val->name && strlen (val->name))
926 if (size == 1) /* This appears to be Z80 specific?? */
929 "\t!dbs\n", val->name);
931 else if (size == FPTRSIZE)
933 if (port->use_dw_for_init)
935 tfprintf (oFile, "\t!dws\n", val->name);
939 printPointerType (oFile, val->name);
942 else if (size == GPTRSIZE)
945 if (IS_PTR (val->type)) {
946 type = DCL_TYPE (val->type);
948 type = PTR_TYPE (SPEC_OCLS (val->etype));
950 if (val->sym && val->sym->isstrlit) {
951 // this is a literal string
954 printGPointerType (oFile, val->name, sym->name, type);
958 fprintf (stderr, "*** internal error: unknown size in "
959 "printIvalCharPtr.\n");
964 // these are literals assigned to pointers
968 tfprintf (oFile, "\t!dbs\n", aopLiteral (val, 0));
971 if (port->use_dw_for_init)
972 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, size));
973 else if (port->little_endian)
974 tfprintf (oFile, "\t.byte %s,%s\n",
975 aopLiteral (val, 0), aopLiteral (val, 1));
977 tfprintf (oFile, "\t.byte %s,%s\n",
978 aopLiteral (val, 1), aopLiteral (val, 0));
981 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
982 // non-zero mcs51 generic pointer
983 werrorfl (filename, sym->lineDef, E_LITERAL_GENERIC);
985 if (port->little_endian) {
986 fprintf (oFile, "\t.byte %s,%s,%s\n",
989 aopLiteral (val, 2));
991 fprintf (oFile, "\t.byte %s,%s,%s\n",
994 aopLiteral (val, 0));
998 if (IS_GENPTR(type) && floatFromVal(val)!=0) {
999 // non-zero ds390 generic pointer
1000 werrorfl (filename, sym->lineDef, E_LITERAL_GENERIC);
1002 if (port->little_endian) {
1003 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
1004 aopLiteral (val, 0),
1005 aopLiteral (val, 1),
1006 aopLiteral (val, 2),
1007 aopLiteral (val, 3));
1009 fprintf (oFile, "\t.byte %s,%s,%s,%s\n",
1010 aopLiteral (val, 3),
1011 aopLiteral (val, 2),
1012 aopLiteral (val, 1),
1013 aopLiteral (val, 0));
1021 if (val->sym && val->sym->isstrlit && !isinSet(statsg->syms, val->sym)) {
1022 addSet (&statsg->syms, val->sym);
1028 /*-----------------------------------------------------------------*/
1029 /* printIvalPtr - generates initial value for pointers */
1030 /*-----------------------------------------------------------------*/
1032 printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
1038 if (ilist->type == INIT_DEEP)
1039 ilist = ilist->init.deep;
1041 /* function pointer */
1042 if (IS_FUNC (type->next))
1044 printIvalFuncPtr (type, ilist, oFile);
1048 if (!(val = initPointer (ilist, type)))
1051 /* if character pointer */
1052 if (IS_CHAR (type->next))
1053 if (printIvalCharPtr (sym, type, val, oFile))
1056 /* check the type */
1057 if (compareType (type, val->type) == 0) {
1058 werrorfl (filename, ilist->lineno, W_INIT_WRONG);
1059 printFromToType (val->type, type);
1062 /* if val is literal */
1063 if (IS_LITERAL (val->etype))
1065 switch (getSize (type))
1068 tfprintf (oFile, "\t!db !constbyte\n", (unsigned int) floatFromVal (val) & 0xff);
1071 if (port->use_dw_for_init)
1072 tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2));
1073 else if (port->little_endian)
1074 tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1));
1076 tfprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 1), aopLiteral (val, 0));
1078 case 3: // how about '390??
1079 if (port->little_endian)
1081 fprintf (oFile, "\t.byte %s,%s,#0x%d\n",
1082 aopLiteral (val, 0), aopLiteral (val, 1), GPTYPE_CODE);
1086 fprintf (oFile, "\t.byte %s,%s,#0x%d\n",
1087 aopLiteral (val, 1), aopLiteral (val, 0), GPTYPE_CODE);
1094 size = getSize (type);
1096 if (size == 1) /* Z80 specific?? */
1098 tfprintf (oFile, "\t!dbs\n", val->name);
1100 else if (size == FPTRSIZE)
1102 if (port->use_dw_for_init) {
1103 tfprintf (oFile, "\t!dws\n", val->name);
1105 printPointerType (oFile, val->name);
1108 else if (size == GPTRSIZE)
1110 printGPointerType (oFile, val->name, sym->name,
1111 (IS_PTR (val->type) ? DCL_TYPE (val->type) :
1112 PTR_TYPE (SPEC_OCLS (val->etype))));
1117 /*-----------------------------------------------------------------*/
1118 /* printIval - generates code for initial value */
1119 /*-----------------------------------------------------------------*/
1121 printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
1128 /* if structure then */
1129 if (IS_STRUCT (type))
1131 printIvalStruct (sym, type, ilist, oFile);
1135 /* if this is an array */
1136 if (IS_ARRAY (type))
1138 printIvalArray (sym, type, ilist, oFile);
1142 // not an aggregate, ilist must be a node
1143 if (ilist->type!=INIT_NODE) {
1144 // or a 1-element list
1145 if (ilist->init.deep->next) {
1146 werrorfl (filename, sym->lineDef, W_EXCESS_INITIALIZERS, "scalar",
1149 ilist=ilist->init.deep;
1153 // and the type must match
1154 itype=ilist->init.node->ftype;
1156 if (compareType(type, itype)==0) {
1157 // special case for literal strings
1158 if (IS_ARRAY (itype) && IS_CHAR (getSpec(itype)) &&
1159 // which are really code pointers
1160 IS_PTR(type) && DCL_TYPE(type)==CPOINTER) {
1163 werrorfl (filename, ilist->lineno, E_TYPE_MISMATCH, "assignment", " ");
1164 printFromToType(itype, type);
1168 /* if this is a pointer */
1171 printIvalPtr (sym, type, ilist, oFile);
1175 /* if type is SPECIFIER */
1178 printIvalType (sym, type, ilist, oFile);
1183 /*-----------------------------------------------------------------*/
1184 /* emitStaticSeg - emitcode for the static segment */
1185 /*-----------------------------------------------------------------*/
1187 emitStaticSeg (memmap * map, FILE * out)
1191 /* fprintf(out, "\t.area\t%s\n", map->sname); */
1193 /* for all variables in this segment do */
1194 for (sym = setFirstItem (map->syms); sym;
1195 sym = setNextItem (map->syms))
1198 /* if it is "extern" then do nothing */
1199 if (IS_EXTERN (sym->etype))
1202 /* if it is not static add it to the public
1204 if (!IS_STATIC (sym->etype))
1206 addSetHead (&publics, sym);
1209 /* print extra debug info if required */
1210 if (options.debug) {
1214 if (IS_STATIC (sym->etype))
1215 fprintf (out, "F%s$", moduleName); /* scope is file */
1217 fprintf (out, "G$"); /* scope is global */
1220 /* symbol is local */
1221 fprintf (out, "L%s$",
1222 (sym->localof ? sym->localof->name : "-null-"));
1223 fprintf (out, "%s$%d$%d", sym->name, sym->level, sym->block);
1226 /* if it has an absolute address */
1227 if (SPEC_ABSA (sym->etype))
1230 fprintf (out, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1232 fprintf (out, "%s\t=\t0x%04x\n",
1234 SPEC_ADDR (sym->etype));
1239 fprintf (out, " == .\n");
1241 /* if it has an initial value */
1244 fprintf (out, "%s:\n", sym->rname);
1246 resolveIvalSym (sym->ival);
1247 printIval (sym, sym->type, sym->ival, out);
1249 /* if sym is a simple string and sym->ival is a string,
1250 WE don't need it anymore */
1251 if (IS_ARRAY(sym->type) && IS_CHAR(sym->type->next) &&
1252 IS_AST_SYM_VALUE(list2expr(sym->ival)) &&
1253 list2val(sym->ival)->sym->isstrlit) {
1254 freeStringSymbol(list2val(sym->ival)->sym);
1258 /* allocate space */
1259 int size = getSize (sym->type);
1262 werrorfl (filename, sym->lineDef, E_UNKNOWN_SIZE,sym->name);
1264 fprintf (out, "%s:\n", sym->rname);
1265 /* special case for character strings */
1266 if (IS_ARRAY (sym->type) && IS_CHAR (sym->type->next) &&
1267 SPEC_CVAL (sym->etype).v_char)
1269 SPEC_CVAL (sym->etype).v_char,
1272 tfprintf (out, "\t!ds\n", (unsigned int) size & 0xffff);
1278 /*-----------------------------------------------------------------*/
1279 /* emitMaps - emits the code for the data portion the code */
1280 /*-----------------------------------------------------------------*/
1285 /* no special considerations for the following
1286 data, idata & bit & xdata */
1287 emitRegularMap (data, TRUE, TRUE);
1288 emitRegularMap (idata, TRUE, TRUE);
1289 emitRegularMap (bit, TRUE, FALSE);
1290 emitRegularMap (xdata, TRUE, TRUE);
1291 if (port->genXINIT) {
1292 emitRegularMap (xidata, TRUE, TRUE);
1294 emitRegularMap (sfr, FALSE, FALSE);
1295 emitRegularMap (sfrbit, FALSE, FALSE);
1296 emitRegularMap (home, TRUE, FALSE);
1297 emitRegularMap (code, TRUE, FALSE);
1299 emitStaticSeg (statsg, code->oFile);
1300 if (port->genXINIT) {
1301 tfprintf (code->oFile, "\t!area\n", xinit->sname);
1302 emitStaticSeg (xinit, code->oFile);
1307 /*-----------------------------------------------------------------*/
1308 /* flushStatics - flush all currently defined statics out to file */
1309 /* and delete. Temporary function */
1310 /*-----------------------------------------------------------------*/
1314 emitStaticSeg (statsg, codeOutFile);
1315 statsg->syms = NULL;
1318 /*-----------------------------------------------------------------*/
1319 /* createInterruptVect - creates the interrupt vector */
1320 /*-----------------------------------------------------------------*/
1322 createInterruptVect (FILE * vFile)
1325 mainf = newSymbol ("main", 0);
1328 /* only if the main function exists */
1329 if (!(mainf = findSymWithLevel (SymbolTab, mainf)))
1331 if (!options.cc_only && !noAssemble && !options.c1mode)
1336 /* if the main is only a prototype ie. no body then do nothing */
1337 if (!IFFUNC_HASBODY(mainf->type))
1339 /* if ! compile only then main function should be present */
1340 if (!options.cc_only && !noAssemble)
1345 tfprintf (vFile, "\t!areacode\n", CODE_NAME);
1346 fprintf (vFile, "__interrupt_vect:\n");
1349 if (!port->genIVT || !(port->genIVT (vFile, interrupts, maxInterrupts)))
1351 /* "generic" interrupt table header (if port doesn't specify one).
1352 * Look suspiciously like 8051 code to me...
1355 fprintf (vFile, "\tljmp\t__sdcc_gsinit_startup\n");
1357 /* now for the other interrupts */
1358 for (; i < maxInterrupts; i++)
1362 fprintf (vFile, "\tljmp\t%s\n", interrupts[i]->rname);
1363 if ( i != maxInterrupts - 1 )
1364 fprintf (vFile, "\t.ds\t5\n");
1368 fprintf (vFile, "\treti\n");
1369 if ( i != maxInterrupts - 1 )
1370 fprintf (vFile, "\t.ds\t7\n");
1378 ";--------------------------------------------------------\n"
1379 "; File Created by SDCC : FreeWare ANSI-C Compiler\n"};
1383 ";--------------------------------------------------------\n"};
1386 /*-----------------------------------------------------------------*/
1387 /* initialComments - puts in some initial comments */
1388 /*-----------------------------------------------------------------*/
1390 initialComments (FILE * afile)
1394 fprintf (afile, "%s", iComments1);
1395 fprintf (afile, "; Version " SDCC_VERSION_STR " %s\n", asctime (localtime (&t)));
1396 fprintf (afile, "%s", iComments2);
1399 /*-----------------------------------------------------------------*/
1400 /* printPublics - generates .global for publics */
1401 /*-----------------------------------------------------------------*/
1403 printPublics (FILE * afile)
1407 fprintf (afile, "%s", iComments2);
1408 fprintf (afile, "; Public variables in this module\n");
1409 fprintf (afile, "%s", iComments2);
1411 for (sym = setFirstItem (publics); sym;
1412 sym = setNextItem (publics))
1413 tfprintf (afile, "\t!global\n", sym->rname);
1416 /*-----------------------------------------------------------------*/
1417 /* printExterns - generates .global for externs */
1418 /*-----------------------------------------------------------------*/
1420 printExterns (FILE * afile)
1424 fprintf (afile, "%s", iComments2);
1425 fprintf (afile, "; Externals used\n");
1426 fprintf (afile, "%s", iComments2);
1428 for (sym = setFirstItem (externs); sym;
1429 sym = setNextItem (externs))
1430 tfprintf (afile, "\t!extern\n", sym->rname);
1433 /*-----------------------------------------------------------------*/
1434 /* emitOverlay - will emit code for the overlay stuff */
1435 /*-----------------------------------------------------------------*/
1437 emitOverlay (FILE * afile)
1441 if (!elementsInSet (ovrSetSets))
1442 tfprintf (afile, "\t!area\n", port->mem.overlay_name);
1444 /* for each of the sets in the overlay segment do */
1445 for (ovrset = setFirstItem (ovrSetSets); ovrset;
1446 ovrset = setNextItem (ovrSetSets))
1451 if (elementsInSet (ovrset))
1453 /* output the area informtion */
1454 fprintf (afile, "\t.area\t%s\n", port->mem.overlay_name); /* MOF */
1457 for (sym = setFirstItem (ovrset); sym;
1458 sym = setNextItem (ovrset))
1460 /* if extern then it is in the publics table: do nothing */
1461 if (IS_EXTERN (sym->etype))
1464 /* if allocation required check is needed
1465 then check if the symbol really requires
1466 allocation only for local variables */
1467 if (!IS_AGGREGATE (sym->type) &&
1468 !(sym->_isparm && !IS_REGPARM (sym->etype))
1469 && !sym->allocreq && sym->level)
1472 /* if global variable & not static or extern
1473 and addPublics allowed then add it to the public set */
1474 if ((sym->_isparm && !IS_REGPARM (sym->etype))
1475 && !IS_STATIC (sym->etype))
1477 addSetHead (&publics, sym);
1480 /* if extern then do nothing or is a function
1482 if (IS_FUNC (sym->type))
1485 /* print extra debug info if required */
1490 if (IS_STATIC (sym->etype))
1491 fprintf (afile, "F%s$", moduleName); /* scope is file */
1493 fprintf (afile, "G$"); /* scope is global */
1496 /* symbol is local */
1497 fprintf (afile, "L%s$",
1498 (sym->localof ? sym->localof->name : "-null-"));
1499 fprintf (afile, "%s$%d$%d", sym->name, sym->level, sym->block);
1502 /* if is has an absolute address then generate
1503 an equate for this no need to allocate space */
1504 if (SPEC_ABSA (sym->etype))
1508 fprintf (afile, " == 0x%04x\n", SPEC_ADDR (sym->etype));
1510 fprintf (afile, "%s\t=\t0x%04x\n",
1512 SPEC_ADDR (sym->etype));
1515 int size = getSize(sym->type);
1518 werrorfl (filename, sym->lineDef, E_UNKNOWN_SIZE);
1521 fprintf (afile, "==.\n");
1523 /* allocate space */
1524 tfprintf (afile, "!labeldef\n", sym->rname);
1525 tfprintf (afile, "\t!ds\n", (unsigned int) getSize (sym->type) & 0xffff);
1533 /*-----------------------------------------------------------------*/
1534 /* spacesToUnderscores - replace spaces with underscores */
1535 /*-----------------------------------------------------------------*/
1537 spacesToUnderscores (char *dest, const char *src, size_t len)
1542 assert(dest != NULL);
1543 assert(src != NULL);
1547 for (p = dest, i = 0; *src != '\0' && i < len; ++src, ++i) {
1548 *p++ = isspace(*src) ? '_' : *src;
1556 /*-----------------------------------------------------------------*/
1557 /* glue - the final glue that hold the whole thing together */
1558 /*-----------------------------------------------------------------*/
1564 FILE *ovrFile = tempfile ();
1565 char moduleBuf[PATH_MAX];
1568 if(port->general.glue_up_main &&
1569 (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_XA51 || TARGET_IS_DS400))
1571 mcs51_like=1; /*So it has bits, sfr, sbits, data, idata, etc...*/
1578 addSetHead (&tmpfileSet, ovrFile);
1579 /* print the global struct definitions */
1583 vFile = tempfile ();
1584 /* PENDING: this isnt the best place but it will do */
1585 if (port->general.glue_up_main)
1587 /* create the interrupt vector table */
1588 createInterruptVect (vFile);
1591 addSetHead (&tmpfileSet, vFile);
1593 /* emit code for the all the variables declared */
1595 /* do the overlay segments */
1596 emitOverlay (ovrFile);
1598 outputDebugSymbols();
1600 /* now put it all together into the assembler file */
1601 /* create the assembler file name */
1603 /* -o option overrides default name? */
1604 if ((noAssemble || options.c1mode) && fullDstFileName)
1606 strncpyz (scratchFileName, fullDstFileName, PATH_MAX);
1610 strncpyz (scratchFileName, dstFileName, PATH_MAX);
1611 strncatz (scratchFileName, port->assembler.file_ext, PATH_MAX);
1614 if (!(asmFile = fopen (scratchFileName, "w")))
1616 werror (E_FILE_OPEN_ERR, scratchFileName);
1620 /* initial comments */
1621 initialComments (asmFile);
1623 /* print module name */
1624 tfprintf (asmFile, "\t!module\n",
1625 spacesToUnderscores (moduleBuf, moduleName, sizeof moduleBuf));
1628 fprintf (asmFile, "\t.optsdcc -m%s", port->target);
1630 switch(options.model)
1632 case MODEL_SMALL: fprintf (asmFile, " --model-small"); break;
1633 case MODEL_COMPACT: fprintf (asmFile, " --model-compact"); break;
1634 case MODEL_MEDIUM: fprintf (asmFile, " --model-medium"); break;
1635 case MODEL_LARGE: fprintf (asmFile, " --model-large"); break;
1636 case MODEL_FLAT24: fprintf (asmFile, " --model-flat24"); break;
1637 case MODEL_PAGE0: fprintf (asmFile, " --model-page0"); break;
1640 /*if(options.stackAuto) fprintf (asmFile, " --stack-auto");*/
1641 if(options.useXstack) fprintf (asmFile, " --xstack");
1642 /*if(options.intlong_rent) fprintf (asmFile, " --int-long-rent");*/
1643 /*if(options.float_rent) fprintf (asmFile, " --float-rent");*/
1644 if(options.noRegParams) fprintf (asmFile, " --no-reg-params");
1645 if(options.parms_in_bank1) fprintf (asmFile, " --parms-in-bank1");
1646 fprintf (asmFile, "\n");
1648 else if(TARGET_IS_Z80 || TARGET_IS_GBZ80 )
1650 fprintf (asmFile, "\t.optsdcc -m%s\n", port->target);
1653 tfprintf (asmFile, "\t!fileprelude\n");
1655 /* Let the port generate any global directives, etc. */
1656 if (port->genAssemblerPreamble)
1658 port->genAssemblerPreamble (asmFile);
1661 /* print the global variables in this module */
1662 printPublics (asmFile);
1663 if (port->assembler.externGlobal)
1664 printExterns (asmFile);
1667 ||( TARGET_IS_Z80 )) /*.p.t.20030924 need to output SFR table for Z80 as well */
1669 /* copy the sfr segment */
1670 fprintf (asmFile, "%s", iComments2);
1671 fprintf (asmFile, "; special function registers\n");
1672 fprintf (asmFile, "%s", iComments2);
1673 copyFile (asmFile, sfr->oFile);
1678 /* copy the sbit segment */
1679 fprintf (asmFile, "%s", iComments2);
1680 fprintf (asmFile, "; special function bits \n");
1681 fprintf (asmFile, "%s", iComments2);
1682 copyFile (asmFile, sfrbit->oFile);
1684 /*JCF: Create the areas for the register banks*/
1685 if(RegBankUsed[0]||RegBankUsed[1]||RegBankUsed[2]||RegBankUsed[3])
1687 fprintf (asmFile, "%s", iComments2);
1688 fprintf (asmFile, "; overlayable register banks \n");
1689 fprintf (asmFile, "%s", iComments2);
1691 fprintf (asmFile, "\t.area REG_BANK_0\t(REL,OVR,DATA)\n\t.ds 8\n");
1692 if(RegBankUsed[1]||options.parms_in_bank1)
1693 fprintf (asmFile, "\t.area REG_BANK_1\t(REL,OVR,DATA)\n\t.ds 8\n");
1695 fprintf (asmFile, "\t.area REG_BANK_2\t(REL,OVR,DATA)\n\t.ds 8\n");
1697 fprintf (asmFile, "\t.area REG_BANK_3\t(REL,OVR,DATA)\n\t.ds 8\n");
1701 /* copy the data segment */
1702 fprintf (asmFile, "%s", iComments2);
1703 fprintf (asmFile, "; %s ram data\n", mcs51_like?"internal":"");
1704 fprintf (asmFile, "%s", iComments2);
1705 copyFile (asmFile, data->oFile);
1708 /* create the overlay segments */
1710 fprintf (asmFile, "%s", iComments2);
1711 fprintf (asmFile, "; overlayable items in %s ram \n", mcs51_like?"internal":"");
1712 fprintf (asmFile, "%s", iComments2);
1713 copyFile (asmFile, ovrFile);
1716 /* create the stack segment MOF */
1717 if (mainf && IFFUNC_HASBODY(mainf->type))
1719 fprintf (asmFile, "%s", iComments2);
1720 fprintf (asmFile, "; Stack segment in internal ram \n");
1721 fprintf (asmFile, "%s", iComments2);
1722 fprintf (asmFile, "\t.area\tSSEG\t(DATA)\n"
1723 "__start__stack:\n\t.ds\t1\n\n");
1726 /* create the idata segment */
1727 if ( (idata) && (mcs51_like) ) {
1728 fprintf (asmFile, "%s", iComments2);
1729 fprintf (asmFile, "; indirectly addressable internal ram data\n");
1730 fprintf (asmFile, "%s", iComments2);
1731 copyFile (asmFile, idata->oFile);
1734 /* copy the bit segment */
1736 fprintf (asmFile, "%s", iComments2);
1737 fprintf (asmFile, "; bit data\n");
1738 fprintf (asmFile, "%s", iComments2);
1739 copyFile (asmFile, bit->oFile);
1742 /* if external stack then reserve space of it */
1743 if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack)
1745 fprintf (asmFile, "%s", iComments2);
1746 fprintf (asmFile, "; external stack \n");
1747 fprintf (asmFile, "%s", iComments2);
1748 fprintf (asmFile, "\t.area XSEG (XDATA)\n"); /* MOF */
1749 fprintf (asmFile, "\t.ds 256\n");
1753 /* copy xtern ram data */
1755 fprintf (asmFile, "%s", iComments2);
1756 fprintf (asmFile, "; external ram data\n");
1757 fprintf (asmFile, "%s", iComments2);
1758 copyFile (asmFile, xdata->oFile);
1761 /* copy xternal initialized ram data */
1762 fprintf (asmFile, "%s", iComments2);
1763 fprintf (asmFile, "; external initialized ram data\n");
1764 fprintf (asmFile, "%s", iComments2);
1765 copyFile (asmFile, xidata->oFile);
1767 /* If the port wants to generate any extra areas, let it do so. */
1768 if (port->extraAreas.genExtraAreaDeclaration)
1770 port->extraAreas.genExtraAreaDeclaration(asmFile,
1771 mainf && IFFUNC_HASBODY(mainf->type));
1774 /* copy the interrupt vector table */
1775 if (mainf && IFFUNC_HASBODY(mainf->type))
1777 fprintf (asmFile, "%s", iComments2);
1778 fprintf (asmFile, "; interrupt vector \n");
1779 fprintf (asmFile, "%s", iComments2);
1780 copyFile (asmFile, vFile);
1783 /* copy global & static initialisations */
1784 fprintf (asmFile, "%s", iComments2);
1785 fprintf (asmFile, "; global & static initialisations\n");
1786 fprintf (asmFile, "%s", iComments2);
1788 /* Everywhere we generate a reference to the static_name area,
1789 * (which is currently only here), we immediately follow it with a
1790 * definition of the post_static_name area. This guarantees that
1791 * the post_static_name area will immediately follow the static_name
1794 tfprintf (asmFile, "\t!area\n", port->mem.static_name); /* MOF */
1795 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1796 tfprintf (asmFile, "\t!area\n", port->mem.static_name);
1798 if (mainf && IFFUNC_HASBODY(mainf->type))
1800 fprintf (asmFile, "__sdcc_gsinit_startup:\n");
1801 /* if external stack is specified then the
1802 higher order byte of the xdatalocation is
1803 going into P2 and the lower order going into
1805 if (options.useXstack)
1807 fprintf (asmFile, "\tmov\tP2,#0x%02x\n",
1808 (((unsigned int) options.xdata_loc) >> 8) & 0xff);
1809 fprintf (asmFile, "\tmov\t_spx,#0x%02x\n",
1810 (unsigned int) options.xdata_loc & 0xff);
1813 // This should probably be a port option, but I'm being lazy.
1814 // on the 400, the firmware boot loader gives us a valid stack
1815 // (see '400 data sheet pg. 85 (TINI400 ROM Initialization code)
1816 if (!TARGET_IS_DS400)
1818 /* initialise the stack pointer. JCF: aslink takes care of the location */
1819 fprintf (asmFile, "\tmov\tsp,#__start__stack - 1\n"); /* MOF */
1822 fprintf (asmFile, "\tlcall\t__sdcc_external_startup\n");
1823 fprintf (asmFile, "\tmov\ta,dpl\n");
1824 fprintf (asmFile, "\tjz\t__sdcc_init_data\n");
1825 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1826 fprintf (asmFile, "__sdcc_init_data:\n");
1828 // if the port can copy the XINIT segment to XISEG
1829 if (port->genXINIT) {
1830 port->genXINIT(asmFile);
1834 copyFile (asmFile, statsg->oFile);
1836 if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type))
1838 /* This code is generated in the post-static area.
1839 * This area is guaranteed to follow the static area
1840 * by the ugly shucking and jiving about 20 lines ago.
1842 tfprintf (asmFile, "\t!area\n", port->mem.post_static_name);
1843 fprintf (asmFile, "\tljmp\t__sdcc_program_startup\n");
1849 "%s", iComments2, iComments2);
1850 tfprintf (asmFile, "\t!areahome\n", HOME_NAME);
1851 copyFile (asmFile, home->oFile);
1853 /* copy over code */
1854 fprintf (asmFile, "%s", iComments2);
1855 fprintf (asmFile, "; code\n");
1856 fprintf (asmFile, "%s", iComments2);
1857 tfprintf (asmFile, "\t!areacode\n", CODE_NAME);
1858 if (mainf && IFFUNC_HASBODY(mainf->type))
1861 /* entry point @ start of CSEG */
1862 fprintf (asmFile, "__sdcc_program_startup:\n");
1864 /* put in the call to main */
1865 fprintf (asmFile, "\tlcall\t_main\n");
1866 if (options.mainreturn)
1869 fprintf (asmFile, ";\treturn from main ; will return to caller\n");
1870 fprintf (asmFile, "\tret\n");
1876 fprintf (asmFile, ";\treturn from main will lock up\n");
1877 fprintf (asmFile, "\tsjmp .\n");
1880 copyFile (asmFile, code->oFile);
1882 if (port->genAssemblerEnd) {
1883 port->genAssemblerEnd(asmFile);
1891 /** Creates a temporary file with unoque file name
1893 - TMP, TEMP, TMPDIR env. varibles
1894 - if Un*x system: /usr/tmp and /tmp
1895 - root directory using mkstemp() if avaliable
1896 - default location using tempnam()
1899 tempfileandname(char *fname, size_t len)
1901 #define TEMPLATE "sdccXXXXXX"
1902 #define TEMPLATE_LEN ((sizeof TEMPLATE) - 1)
1904 const char *tmpdir = NULL;
1907 if ((tmpdir = getenv ("TMP")) == NULL)
1908 if ((tmpdir = getenv ("TEMP")) == NULL)
1909 tmpdir = getenv ("TMPDIR");
1913 static int warning_emitted;
1918 if (!warning_emitted)
1920 fprintf (stderr, "TMP not defined in environment, using %s for temporary files\n.", tmpdir);
1921 warning_emitted = 1;
1927 /* try with /usr/tmp and /tmp on Un*x systems */
1928 struct stat statbuf;
1930 if (tmpdir == NULL) {
1931 if (stat("/usr/tmp", &statbuf) != -1)
1932 tmpdir = "/usr/tmp";
1933 else if (stat("/tmp", &statbuf) != -1)
1941 char fnamebuf[PATH_MAX];
1944 if (fname == NULL || len == 0) {
1946 len = sizeof fnamebuf;
1950 name_len = strlen(tmpdir) + 1 + TEMPLATE_LEN;
1952 assert(name_len < len);
1953 if (!(name_len < len)) /* in NDEBUG is defined */
1954 return -1; /* buffer too small, temporary file can not be created */
1956 sprintf(fname, "%s" DIR_SEPARATOR_STRING TEMPLATE, tmpdir);
1959 name_len = TEMPLATE_LEN;
1961 assert(name_len < len);
1962 if (!(name_len < len)) /* in NDEBUG is defined */
1963 return -1; /* buffer too small, temporary file can not be created */
1965 strcpy(fname, TEMPLATE);
1968 fd = mkstemp(fname);
1972 char *name = tempnam(tmpdir, "sdcc");
1975 perror("Can't create temporary file name");
1979 assert(strlen(name) < len);
1980 if (!(strlen(name) < len)) /* in NDEBUG is defined */
1981 return -1; /* buffer too small, temporary file can not be created */
1983 strcpy(fname, name);
1985 fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IREAD | S_IWRITE);
1987 fd = open(name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
1993 perror("Can't create temporary file");
2001 /** Create a temporary file name
2007 static char fnamebuf[PATH_MAX];
2009 if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
2010 fprintf(stderr, "Can't create temporary file name!");
2021 /** Create a temporary file and add it to tmpfileNameSet,
2022 so that it is removed explicitly by rm_tmpfiles()
2023 or implicitly at program extit.
2031 char fnamebuf[PATH_MAX];
2033 if ((fd = tempfileandname(fnamebuf, sizeof fnamebuf)) == -1) {
2034 fprintf(stderr, "Can't create temporary file!");
2038 tmp = Safe_strdup(fnamebuf);
2040 addSetHead(&tmpfileNameSet, tmp);
2042 if ((fp = fdopen(fd, "w+b")) == NULL) {
2043 perror("Can't create temporary file!");