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