f4f0449f3cc0f86b4c190173d0b04c258ecd21b7
[libfirm] / ir / be / beemitter.c
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 "irnode_t.h"
30 #include "irprintf.h"
31 #include "ident.h"
32 #include "tv.h"
33 #include "dbginfo.h"
34
35 FILE           *emit_file;
36 struct obstack  emit_obst;
37
38 void be_emit_init(FILE *file)
39 {
40         emit_file       = file;
41         obstack_init(&emit_obst);
42 }
43
44 void be_emit_exit(void)
45 {
46         obstack_free(&emit_obst, NULL);
47 }
48
49 void be_emit_ident(ident *id)
50 {
51         size_t      len = get_id_strlen(id);
52         const char *str = get_id_str(id);
53
54         be_emit_string_len(str, len);
55 }
56
57 void be_emit_tarval(ir_tarval *tv)
58 {
59         char buf[64];
60
61         tarval_snprintf(buf, sizeof(buf), tv);
62         be_emit_string(buf);
63 }
64
65 void be_emit_irvprintf(const char *fmt, va_list args)
66 {
67         ir_obst_vprintf(&emit_obst, fmt, args);
68 }
69
70 void be_emit_irprintf(const char *fmt, ...)
71 {
72         va_list ap;
73
74         va_start(ap, fmt);
75         be_emit_irvprintf(fmt, ap);
76         va_end(ap);
77 }
78
79 void be_emit_write_line(void)
80 {
81         size_t  len  = obstack_object_size(&emit_obst);
82         char   *line = (char*)obstack_finish(&emit_obst);
83
84         fwrite(line, 1, len, emit_file);
85         obstack_free(&emit_obst, line);
86 }
87
88 void be_emit_pad_comment(void)
89 {
90         size_t len = obstack_object_size(&emit_obst);
91         if (len > 30)
92                 len = 30;
93         /* 34 spaces */
94         be_emit_string_len("                                  ", 34 - len);
95 }
96
97 void be_emit_finish_line_gas(const ir_node *node)
98 {
99         dbg_info   *dbg;
100         src_loc_t   loc;
101
102         if (node == NULL) {
103                 be_emit_char('\n');
104                 be_emit_write_line();
105                 return;
106         }
107
108         be_emit_pad_comment();
109         be_emit_cstring("/* ");
110         be_emit_irprintf("%+F ", node);
111
112         dbg = get_irn_dbg_info(node);
113         loc = ir_retrieve_dbg_info(dbg);
114         if (loc.file) {
115                 be_emit_string(loc.file);
116                 if (loc.line != 0) {
117                         be_emit_irprintf(":%u", loc.line);
118                         if (loc.column != 0) {
119                                 be_emit_irprintf(":%u", loc.column);
120                         }
121                 }
122         }
123         be_emit_cstring(" */\n");
124         be_emit_write_line();
125 }