dbdcb80d327c3c293a0889570184a79e9122473d
[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
34 void be_emit_init_env(be_emit_env_t *env, FILE *F)
35 {
36         memset(env, 0, sizeof(env[0]));
37
38         env->F = F;
39         obstack_init(&env->obst);
40         env->linelength = 0;
41 }
42
43 void be_emit_destroy_env(be_emit_env_t *env)
44 {
45         obstack_free(&env->obst, NULL);
46 }
47
48 void be_emit_ident(be_emit_env_t *env, ident *id)
49 {
50         size_t len = get_id_strlen(id);
51         const char *str = get_id_str(id);
52
53         be_emit_string_len(env, str, len);
54 }
55
56 void be_emit_irvprintf(be_emit_env_t *env, const char *fmt, va_list args)
57 {
58         char buf[256];
59
60         ir_vsnprintf(buf, sizeof(buf), fmt, args);
61         be_emit_string(env, buf);
62 }
63
64 void be_emit_irprintf(be_emit_env_t *env, const char *fmt, ...)
65 {
66         va_list ap;
67
68         va_start(ap, fmt);
69         be_emit_irvprintf(env, fmt, ap);
70         va_end(ap);
71 }
72
73 void be_emit_write_line(be_emit_env_t *env)
74 {
75         char *finished_line = obstack_finish(&env->obst);
76
77         fwrite(finished_line, env->linelength, 1, env->F);
78         env->linelength = 0;
79         obstack_free(&env->obst, finished_line);
80 }
81
82 void be_emit_pad_comment(be_emit_env_t *env)
83 {
84         while(env->linelength <= 30) {
85                 be_emit_char(env, ' ');
86         }
87         be_emit_cstring(env, "    ");
88 }
89
90 void be_emit_finish_line_gas(be_emit_env_t *env, const ir_node *node)
91 {
92         dbg_info *dbg;
93         const char *sourcefile;
94         unsigned lineno;
95
96         if(node == NULL) {
97                 be_emit_char(env, '\n');
98                 be_emit_write_line(env);
99                 return;
100         }
101
102         be_emit_pad_comment(env);
103         be_emit_cstring(env, "/* ");
104         be_emit_irprintf(env, "%+F ", node);
105
106         dbg = get_irn_dbg_info(node);
107         sourcefile = be_retrieve_dbg_info(dbg, &lineno);
108         if(sourcefile != NULL) {
109                 be_emit_string(env, sourcefile);
110                 be_emit_irprintf(env, ":%u", lineno);
111         }
112         be_emit_cstring(env, " */\n");
113         be_emit_write_line(env);
114 }