1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19 
20 /**
21  * @file
22  * @brief       Interface for assembler output.
23  * @author      Matthias Braun
24  * @date        12.03.2007
25  */
26 #include "config.h"
27 
28 #include "beemitter.h"
29 #include "be_t.h"
30 #include "irnode_t.h"
31 #include "irprintf.h"
32 #include "ident.h"
33 #include "tv.h"
34 #include "dbginfo.h"
35 
36 FILE           *emit_file;
37 struct obstack  emit_obst;
38 
be_emit_init(FILE * file)39 void be_emit_init(FILE *file)
40 {
41 	emit_file       = file;
42 	obstack_init(&emit_obst);
43 }
44 
be_emit_exit(void)45 void be_emit_exit(void)
46 {
47 	obstack_free(&emit_obst, NULL);
48 }
49 
be_emit_irvprintf(const char * fmt,va_list args)50 void be_emit_irvprintf(const char *fmt, va_list args)
51 {
52 	ir_obst_vprintf(&emit_obst, fmt, args);
53 }
54 
be_emit_irprintf(const char * fmt,...)55 void be_emit_irprintf(const char *fmt, ...)
56 {
57 	va_list ap;
58 
59 	va_start(ap, fmt);
60 	be_emit_irvprintf(fmt, ap);
61 	va_end(ap);
62 }
63 
be_emit_write_line(void)64 void be_emit_write_line(void)
65 {
66 	size_t  len  = obstack_object_size(&emit_obst);
67 	char   *line = (char*)obstack_finish(&emit_obst);
68 
69 	fwrite(line, 1, len, emit_file);
70 	obstack_free(&emit_obst, line);
71 }
72 
be_emit_pad_comment(void)73 void be_emit_pad_comment(void)
74 {
75 	size_t len = obstack_object_size(&emit_obst);
76 	if (len > 30)
77 		len = 30;
78 	/* 34 spaces */
79 	be_emit_string_len("                                  ", 34 - len);
80 }
81 
be_emit_finish_line_gas(const ir_node * node)82 void be_emit_finish_line_gas(const ir_node *node)
83 {
84 	dbg_info  *dbg;
85 	src_loc_t  loc;
86 
87 	if (node == NULL || !be_options.verbose_asm) {
88 		be_emit_char('\n');
89 		be_emit_write_line();
90 		return;
91 	}
92 
93 	be_emit_pad_comment();
94 	be_emit_cstring("/* ");
95 	be_emit_irprintf("%+F ", node);
96 
97 	dbg = get_irn_dbg_info(node);
98 	loc = ir_retrieve_dbg_info(dbg);
99 	if (loc.file) {
100 		be_emit_string(loc.file);
101 		if (loc.line != 0) {
102 			be_emit_irprintf(":%u", loc.line);
103 			if (loc.column != 0) {
104 				be_emit_irprintf(":%u", loc.column);
105 			}
106 		}
107 	}
108 	be_emit_cstring(" */\n");
109 	be_emit_write_line();
110 }
111