f9949ac48f2ed7dba42c5d850d8c7c4a2549bae8
[libfirm] / ir / be / beemitter.h
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 #ifndef FIRM_BE_BEEMITTER_H
28 #define FIRM_BE_BEEMITTER_H
29
30 #include <stdio.h>
31 #include <stdarg.h>
32 #include "firm_types.h"
33 #include "obst.h"
34 #include "be.h"
35
36 /* framework for emitting data (usually the final assembly code) */
37
38 /** The emitter environment. */
39 typedef struct be_emit_env_t {
40         FILE           *F;         /**< The handle of the (assembler) file that is written to. */
41         struct obstack obst;       /**< An obstack for temporary storage. */
42         int            linelength; /**< The length of the current line. */
43 } be_emit_env_t;
44
45 #define NULL_EMITTER    { NULL, {}, 0 }
46
47 /**
48  * Emit a character to the (assembler) output.
49  *
50  * @param env  the emitter environment
51  */
52 static INLINE void be_emit_char(be_emit_env_t *env, char c) {
53         obstack_1grow(&env->obst, c);
54         env->linelength++;
55 }
56
57 /**
58  * Emit a string to the (assembler) output.
59  *
60  * @param env  the emitter environment
61  * @param str  the string
62  * @param l    the length of the given string
63  */
64 static INLINE void be_emit_string_len(be_emit_env_t *env, const char *str,
65                                       size_t l)
66 {
67         obstack_grow(&env->obst, str, l);
68         env->linelength += l;
69 }
70
71 /**
72  * Emit a null-terminated string to the (assembler) output.
73  *
74  * @param env  the emitter environment
75  * @param str  the null-terminated string
76  */
77 static INLINE void be_emit_string(be_emit_env_t *env, const char *str)
78 {
79         size_t len = strlen(str);
80         be_emit_string_len(env, str, len);
81 }
82
83 /**
84  * Emit a C string-constant to the (assembler) output.
85  *
86  * @param env  the emitter environment
87  * @param str  the null-terminated string constant
88  */
89 #define be_emit_cstring(env, str) do { be_emit_string_len(env, str, sizeof(str)-1); } while(0)
90
91 /**
92  * Initializes an emitter environment.
93  *
94  * @param env  the (uninitialized) emitter environment
95  * @param F    a file handle where the emitted file is written to.
96  */
97 void be_emit_init_env(be_emit_env_t *env, FILE *F);
98
99 /**
100  * Destroys the given emitter environment.
101  *
102  * @param env  the emitter environment
103  */
104 void be_emit_destroy_env(be_emit_env_t *env);
105
106 /**
107  * Emit an ident to the (assembler) output.
108  *
109  * @param env  the emitter environment
110  * @param id   the ident to be emitted
111  */
112 void be_emit_ident(be_emit_env_t *env, ident *id);
113
114 /**
115  * Emit a firm tarval.
116  *
117  * @param env  the emitter environment
118  * @param tv   the tarval to be emitted
119  */
120 void be_emit_tarval(be_emit_env_t *env, tarval *tv);
121
122 /**
123  * Emit the output of an ir_printf.
124  *
125  * @param env  the emitter environment
126  * @param fmt  the ir_printf format
127  */
128 void be_emit_irprintf(be_emit_env_t *env, const char *fmt, ...);
129
130 /**
131  * Emit the output of an ir_vprintf.
132  *
133  * @param env  the emitter environment
134  * @param fmt  the ir_printf format
135  */
136 void be_emit_irvprintf(be_emit_env_t *env, const char *fmt, va_list args);
137
138 /**
139  * Flush the line in the current line buffer to the emitter file.
140  *
141  * @param env  the emitter environment
142  */
143 void be_emit_write_line(be_emit_env_t *env);
144
145 /**
146  * Flush the line in the current line buffer to the emitter file and
147  * appends a gas-style comment with the node number and writes the line
148  *
149  * @param env   the emitter environment
150  * @param node  the node to get the debug info from
151  */
152 void be_emit_finish_line_gas(be_emit_env_t *env, const ir_node *node);
153
154 /**
155  * Emit spaces until the comment position is reached.
156  *
157  * @param env  the emitter environment
158  */
159 void be_emit_pad_comment(be_emit_env_t *env);
160
161 #endif /* FIRM_BE_BEEMITTER_H */