- Allow an arbitrary (for arbitrary < 32) number of in_rBAR and !in_rBAR constraints...
[libfirm] / ir / be / beemitter.c
1 /*
2  * Copyright (C) 1995-2007 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  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "beemitter.h"
32 #include "irprintf.h"
33 #include "ident.h"
34 #include "tv.h"
35
36 FILE           *emit_file;
37 struct obstack  emit_obst;
38 int             emit_linelength;
39
40 void be_emit_init(FILE *file)
41 {
42         emit_file       = file;
43         emit_linelength = 0;
44         obstack_init(&emit_obst);
45 }
46
47 void be_emit_exit(void)
48 {
49         obstack_free(&emit_obst, NULL);
50 }
51
52 void be_emit_ident(ident *id)
53 {
54         size_t      len = get_id_strlen(id);
55         const char *str = get_id_str(id);
56
57         be_emit_string_len(str, len);
58 }
59
60 void be_emit_tarval(tarval *tv)
61 {
62         char buf[64];
63
64         tarval_snprintf(buf, sizeof(buf), tv);
65         be_emit_string(buf);
66 }
67
68 void be_emit_irvprintf(const char *fmt, va_list args)
69 {
70         char buf[256];
71
72         ir_vsnprintf(buf, sizeof(buf), fmt, args);
73         be_emit_string(buf);
74 }
75
76 void be_emit_irprintf(const char *fmt, ...)
77 {
78         va_list ap;
79
80         va_start(ap, fmt);
81         be_emit_irvprintf(fmt, ap);
82         va_end(ap);
83 }
84
85 void be_emit_write_line(void)
86 {
87         char *finished_line = obstack_finish(&emit_obst);
88
89         fwrite(finished_line, emit_linelength, 1, emit_file);
90         emit_linelength = 0;
91         obstack_free(&emit_obst, finished_line);
92 }
93
94 void be_emit_pad_comment(void)
95 {
96         while(emit_linelength <= 30) {
97                 be_emit_char(' ');
98         }
99         be_emit_cstring("    ");
100 }
101
102 void be_emit_finish_line_gas(const ir_node *node)
103 {
104         dbg_info   *dbg;
105         const char *sourcefile;
106         unsigned    lineno;
107
108         if(node == NULL) {
109                 be_emit_char('\n');
110                 be_emit_write_line();
111                 return;
112         }
113
114         be_emit_pad_comment();
115         be_emit_cstring("/* ");
116         be_emit_irprintf("%+F ", node);
117
118         dbg        = get_irn_dbg_info(node);
119         sourcefile = be_retrieve_dbg_info(dbg, &lineno);
120         if(sourcefile != NULL) {
121                 be_emit_string(sourcefile);
122                 be_emit_irprintf(":%u", lineno);
123         }
124         be_emit_cstring(" */\n");
125         be_emit_write_line();
126 }