+ /* now put it all together into the assembler file */
+ /* create the assembler file name */
+
+ if ((noAssemble || options.c1mode) && fullDstFileName)
+ {
+ sprintf (buffer, fullDstFileName);
+ }
+ else
+ {
+ sprintf (buffer, dstFileName);
+ strcat (buffer, ".asm");
+ }
+
+ if (!(asmFile = fopen (buffer, "w"))) {
+ werror (E_FILE_OPEN_ERR, buffer);
+ exit (1);
+ }
+
+ /* prepare statistics */
+ resetpCodeStatistics ();
+
+ /* initial comments */
+ pic14initialComments (asmFile);
+
+ /* print module name */
+ fprintf (asmFile, "%s\t.file\t\"%s\"\n",
+ options.debug ? "" : ";", fullSrcFileName);
+
+ /* Let the port generate any global directives, etc. */
+ if (port->genAssemblerPreamble)
+ {
+ port->genAssemblerPreamble(asmFile);
+ }
+
+ /* print the global variables in this module */
+ //pic14printPublics (asmFile);
+
+ /* print the extern variables in this module */
+ //pic14printExterns (asmFile);
+
+ /* copy the sfr segment */
+#if 0
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; special function registers\n");
+ fprintf (asmFile, "%s", iComments2);
+ dbuf_write_and_destroy (&sfr->oBuf, asmFile);
+
+
+ if (udata_section_name) {
+ sprintf(udata_name,"%s",udata_section_name);
+ } else {
+ sprintf(udata_name,"data_%s",moduleName);
+ }
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; udata\n");
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "%s\tudata\n", udata_name);
+ dbuf_write_and_destroy(&data->oBuf, asmFile);
+#endif
+
+ /* Put all variables into a cblock */
+ AnalyzeBanking();
+
+ /* emit initialized data */
+ showAllMemmaps(asmFile);
+
+ /* print the locally defined variables in this module */
+ writeUsedRegs(asmFile);
+
+ /* create the overlay segments */
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; overlayable items in internal ram \n");
+ fprintf (asmFile, "%s", iComments2);
+ dbuf_write_and_destroy (&ovrBuf, asmFile);
+
+#if 0
+
+ /* create the stack segment MOF */
+ if (mainf && IFFUNC_HASBODY(mainf->type)) {
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; Stack segment in internal ram \n");
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, ";\t.area\tSSEG\t(DATA)\n"
+ ";__start__stack:\n;\t.ds\t1\n\n");
+ }
+
+ /* create the idata segment */
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; indirectly addressable internal ram data\n");
+ fprintf (asmFile, "%s", iComments2);
+ dbuf_write_and_destroy (&idata->oBuf, asmFile);
+
+ /* if external stack then reserve space of it */
+ if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack ) {
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; external stack \n");
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile,";\t.area XSEG (XDATA)\n"); /* MOF */
+ fprintf (asmFile,";\t.ds 256\n");
+ }
+
+ /* copy xtern ram data */
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; external ram data\n");
+ fprintf (asmFile, "%s", iComments2);
+ dbuf_write_and_destroy (&xdata->oBuf, asmFile);
+
+#endif
+
+ /* copy the bit segment */
+#if 0
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; bit data\n");
+ fprintf (asmFile, "%s", iComments2);
+ dbuf_write_and_destroy (&bit->oBuf, asmFile);
+#endif
+
+ /* copy the interrupt vector table */
+ if (mainf && IFFUNC_HASBODY(mainf->type))
+ dbuf_write_and_destroy (&vBuf, asmFile);
+ else
+ dbuf_destroy(&vBuf);
+
+ /* create interupt ventor handler */
+ pic14_emitInterruptHandler (asmFile);
+
+ /* copy over code */
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "; code\n");
+ fprintf (asmFile, "%s", iComments2);
+ fprintf (asmFile, "code_%s\t%s\n", moduleName, port->mem.code_name);
+
+ /* unknown */
+ copypCode(asmFile, 'X');
+
+ /* _main function */
+ copypCode(asmFile, 'M');
+
+ /* other functions */
+ copypCode(asmFile, code->dbName);
+
+ /* unknown */
+ copypCode(asmFile, 'P');
+
+ dumppCodeStatistics (asmFile);
+
+ fprintf (asmFile,"\tend\n");
+
+ fclose (asmFile);
+ pic14_debugLogClose();
+}
+
+/*
+ * Deal with initializers.
+ */
+#undef DEBUGprintf
+#if 0
+// debugging output
+#define DEBUGprintf printf
+#else
+// be quiet
+#define DEBUGprintf 1 ? (void)0 : (void)printf
+#endif
+
+
+void ast_print (ast * tree, FILE *outfile, int indent);
+
+#if 0
+/*
+ * Emit all memmaps.
+ */
+static void
+showInitList(initList *list, int level)
+{
+ static const char *list_type[] = { "INIT_NODE", "INIT_DEEP", "INIT_HOLE" };
+ static const char *ast_type[] = { "EX_OP", "EX_VALUE", "EX_LINK", "EX_OPERAND" };
+ struct ast *ast;
+ while (list) {
+ printf (" %d: type %u (%s), init %p, next %p\n", level, list->type, list_type[list->type], list->init.node, list->next);
+ if (list->type == INIT_DEEP) {
+ showInitList(list->init.deep, level + 1);
+ } else if (list->type == INIT_NODE) {
+ ast = list->init.node;
+ printf (" type %u (%s), level %d, block %d, seqPoint %d\n",
+ ast->type, ast_type[ast->type], ast->level, ast->block, ast->seqPoint);
+ if (ast->type == EX_VALUE) {
+ printf (" VAL %lf\n", floatFromVal(ast->opval.val));
+ } else if (ast->type == EX_LINK) {
+ printTypeChain(ast->opval.lnk, NULL);
+ } else if (ast->type == EX_OP) {
+ printf (" OP %u\n", ast->opval.op);
+ }
+ } // if
+ list = list->next;
+ } // while
+}
+#endif
+
+/*
+ * DEBUG: Print a value.
+ */
+void
+printVal(value *val)
+{
+ printf ("value %p: name %s, type %p, etype %p, sym %s, vArgs %d, lit 0x%lx/%ld\n",
+ val, val->name, val->type, val->etype,
+ val->sym ? val->sym->name : NULL, val->vArgs,
+ (long) ulFromVal (val), (long) ulFromVal (val));
+ printTypeChain(val->type, stdout);
+ printf ("\n");
+ printTypeChain(val->etype, stdout);
+ printf ("\n");
+}
+
+//prototype from ../SDCCicode.c
+operand *operandFromAst (ast * tree,int lvl);
+
+char *
+parseIvalAst (ast *node, int *inCodeSpace) {
+#define LEN 4096
+ char *buffer = NULL;
+ char *left, *right;
+
+ if (IS_AST_VALUE(node)) {
+ value *val = AST_VALUE(node);
+ symbol *sym = IS_AST_SYM_VALUE(node) ? AST_SYMBOL(node) : NULL;
+ if (inCodeSpace && val->type
+ && (IS_FUNC(val->type) || IS_CODE(getSpec(val->type))))
+ {
+ *inCodeSpace = 1;
+ }
+ if (inCodeSpace && sym
+ && (IS_FUNC(sym->type)
+ || IS_CODE(getSpec(sym->type))))
+ {
+ *inCodeSpace = 1;
+ }
+
+ DEBUGprintf ("%s: AST_VALUE\n", __FUNCTION__);
+ if (IS_AST_LIT_VALUE(node)) {
+ buffer = Safe_alloc(LEN);
+ SNPRINTF(buffer, LEN, "0x%lx", AST_ULONG_VALUE (node));
+ } else if (IS_AST_SYM_VALUE(node)) {
+ assert ( AST_SYMBOL(node) );
+ /*
+ printf ("sym %s: ", AST_SYMBOL(node)->rname);
+ printTypeChain(AST_SYMBOL(node)->type, stdout);
+ printTypeChain(AST_SYMBOL(node)->etype, stdout);
+ printf ("\n---sym %s: done\n", AST_SYMBOL(node)->rname);
+ */
+ buffer = Safe_strdup(AST_SYMBOL(node)->rname);
+ } else {
+ assert ( !"Invalid values type for initializers in AST." );
+ }
+ } else if (IS_AST_OP(node)) {
+ DEBUGprintf ("%s: AST_OP\n", __FUNCTION__);
+ switch (node->opval.op) {
+ case CAST:
+ assert (node->right);
+ buffer = parseIvalAst(node->right, inCodeSpace);
+ DEBUGprintf ("%s: %s\n", __FUNCTION__, buffer);
+ break;
+ case '&':
+ assert ( node->left && !node->right );
+ buffer = parseIvalAst(node->left, inCodeSpace);
+ DEBUGprintf ("%s: %s\n", __FUNCTION__, buffer);
+ break;
+ case '+':
+ assert (node->left && node->right );
+ left = parseIvalAst(node->left, inCodeSpace);
+ right = parseIvalAst(node->right, inCodeSpace);
+ buffer = Safe_alloc(LEN);
+ SNPRINTF(buffer, LEN, "(%s + %s)", left, right);
+ DEBUGprintf ("%s: %s\n", __FUNCTION__, buffer);
+ Safe_free(left);
+ Safe_free(right);
+ break;
+ case '[':
+ assert ( node->left && node->right );
+ assert ( IS_AST_VALUE(node->left) && AST_VALUE(node->left)->sym );
+ right = parseIvalAst(node->right, inCodeSpace);
+ buffer = Safe_alloc(LEN);
+ SNPRINTF(buffer, LEN, "(%s + %u * %s)",
+ AST_VALUE(node->left)->sym->rname, getSize(AST_VALUE(node->left)->type), right);
+ Safe_free(right);
+ DEBUGprintf ("%s: %s\n", __FUNCTION__, &buffer[0]);
+ break;
+ default:
+ assert ( !"Unhandled operation in initializer." );
+ break;
+ }
+ } else {
+ assert ( !"Invalid construct in initializer." );