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