added new licence header
[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  * Author:      Matthias Braun
22  * Date:                12.03.2007
23  * Copyright:   (c) Universitaet Karlsruhe
24  * License:     This file is protected by GPL -  GNU GENERAL PUBLIC LICENSE.
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "beemitter.h"
31 #include "irprintf.h"
32
33 void be_emit_init_env(be_emit_env_t *env, FILE *F)
34 {
35         memset(env, 0, sizeof(env[0]));
36
37         env->F = F;
38         obstack_init(&env->obst);
39         env->linelength = 0;
40 }
41
42 void be_emit_destroy_env(be_emit_env_t *env)
43 {
44         obstack_free(&env->obst, NULL);
45 }
46
47 void be_emit_ident(be_emit_env_t *env, ident *id)
48 {
49         size_t len = get_id_strlen(id);
50         const char *str = get_id_str(id);
51
52         be_emit_string_len(env, str, len);
53 }
54
55 void be_emit_irvprintf(be_emit_env_t *env, const char *fmt, va_list args)
56 {
57         char buf[256];
58
59         ir_vsnprintf(buf, sizeof(buf), fmt, args);
60         be_emit_string(env, buf);
61 }
62
63 void be_emit_irprintf(be_emit_env_t *env, const char *fmt, ...)
64 {
65         va_list ap;
66
67         va_start(ap, fmt);
68         be_emit_irvprintf(env, fmt, ap);
69         va_end(ap);
70 }
71
72 void be_emit_write_line(be_emit_env_t *env)
73 {
74         char *finished_line = obstack_finish(&env->obst);
75
76         fwrite(finished_line, env->linelength, 1, env->F);
77         env->linelength = 0;
78         obstack_free(&env->obst, finished_line);
79 }
80
81 void be_emit_pad_comment(be_emit_env_t *env)
82 {
83         while(env->linelength <= 30) {
84                 be_emit_char(env, ' ');
85         }
86         be_emit_cstring(env, "    ");
87 }
88
89 void be_emit_finish_line_gas(be_emit_env_t *env, const ir_node *node)
90 {
91         dbg_info *dbg;
92         const char *sourcefile;
93         unsigned lineno;
94
95         if(node == NULL) {
96                 be_emit_char(env, '\n');
97                 be_emit_write_line(env);
98                 return;
99         }
100
101         be_emit_pad_comment(env);
102         be_emit_cstring(env, "/* ");
103         be_emit_irprintf(env, "%+F ", node);
104
105         dbg = get_irn_dbg_info(node);
106         sourcefile = be_retrieve_dbg_info(dbg, &lineno);
107         if(sourcefile != NULL) {
108                 be_emit_string(env, sourcefile);
109                 be_emit_irprintf(env, ":%u", lineno);
110         }
111         be_emit_cstring(env, " */\n");
112         be_emit_write_line(env);
113 }