378f7dc993d66d52d51fc49a99e87a577094e9ed
[libfirm] / ir / common / firm.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     Central firm functionality.
23  * @author    Martin Trapp, Christian Schaefer, Goetz Lindenmaier
24  * @version   $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_FIRM_REVISION_H
31 # include "firm_revision.h"
32 #endif
33
34 #include "firm_config.h"
35
36 #ifdef HAVE_STRING_H
37 # include <string.h>
38 #endif
39 #ifdef HAVE_STDIO_H
40 # include <stdio.h>
41 #endif
42
43 #ifdef WITH_LIBCORE
44 # include <libcore/lc_opts.h>
45 #endif
46
47 #include "ident_t.h"
48 #include "firm.h"
49 #include "irflag_t.h"
50 /* init functions are not public */
51 #include "tv_t.h"
52 #include "tpop_t.h"
53 #include "irprog_t.h"
54 #include "irnode_t.h"
55 #include "irmode_t.h"
56 #include "ircons_t.h"
57 #include "irgraph_t.h"
58 #include "type_t.h"
59 #include "entity_t.h"
60 #include "firmstat.h"
61 #include "irarch.h"
62 #include "reassoc_t.h"
63 #include "funccall_t.h"
64 #include "irhooks.h"
65 #include "iredges_t.h"
66 #include "debugger.h"
67
68 #ifdef WITH_LIBCORE
69 /* returns the firm root */
70 lc_opt_entry_t *firm_opt_get_root(void) {
71         static lc_opt_entry_t *grp = NULL;
72         if(!grp)
73                 grp = lc_opt_get_grp(lc_opt_root_grp(), "firm");
74         return grp;
75 }
76 #endif
77
78 void firm_init_options(const char *arg_prefix, int argc, const char **argv) {
79 #ifdef LIBCORE
80         /* parse any init files for firm */
81         lc_opts_init("firm", firm_opt_get_root(), arg_prefix, argc, argv);
82 #else
83         (void) arg_prefix;
84         (void) argc;
85         (void) argv;
86 #endif
87 }
88
89 void init_firm(const firm_parameter_t *param)
90 {
91         firm_parameter_t def_params;
92         unsigned int     size;
93
94         memset(&def_params, 0, sizeof(def_params));
95
96         if (param) {
97                 /* check for reasonable size */
98                 assert(param->size <= sizeof(def_params) && (param->size & 3) == 0 &&
99                                 "parameter struct not initialized ???");
100                 size = sizeof(def_params);
101                 if (param->size < size)
102                         size = param->size;
103
104                 memcpy(&def_params, param, size);
105         }
106
107         /* initialize firm flags */
108         firm_init_flags();
109         /* initialize all ident stuff */
110         init_ident(def_params.id_if, 1024);
111         /* initialize Firm hooks */
112         firm_init_hooks();
113         /* enhanced statistics, need idents and hooks */
114         firm_init_stat(def_params.enable_statistics);
115         /* Edges need hooks. */
116         init_edges();
117         /* create the type kinds. */
118         init_tpop();
119         /* create an obstack and put all tarvals in a pdeq */
120         init_tarval_1(0l);
121         /* Builds a basic program representation, so modes can be added. */
122         init_irprog_1();
123         /* initialize all modes an ir node can consist of */
124         init_mode();
125         /* initialize tarvals, and floating point arithmetic */
126         init_tarval_2();
127         /* init graph construction */
128         firm_init_irgraph();
129         /* kind of obstack initialization */
130         firm_init_mangle();
131         /* initialize all op codes an irnode can consist of */
132         init_op();
133         /* called once for each run of this library */
134         init_cons(def_params.initialize_local_func);
135         /* initialize reassociation */
136         firm_init_reassociation();
137         /* initialize function call optimization */
138         firm_init_funccalls();
139         /* Builds a construct allowing to access all information to be constructed
140            later. */
141         init_irprog_2();
142         /* Initialize the type module and construct some idents needed. */
143         firm_init_type(def_params.builtin_dbg, def_params.cc_mask);
144         /* initialize the entity module */
145         firm_init_entity();
146         /* allocate a hash table. */
147         init_type_identify(def_params.ti_if);
148
149         /* Init architecture dependent optimizations. */
150         arch_dep_init(arch_dep_default_factory);
151         arch_dep_set_opts(0);
152
153         firm_archops_init(def_params.arch_op_settings);
154
155 #ifdef DEBUG_libfirm
156         /* integrated debugger extension */
157         firm_init_debugger();
158 #endif
159 }
160
161 void free_firm(void) {
162         int i;
163
164         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
165                 free_ir_graph(get_irp_irg(i));
166
167         free_type_entities(get_glob_type());
168         for (i = get_irp_n_types() - 1; i >= 0; --i)
169                 free_type_entities(get_irp_type(i));
170
171         for (i = get_irp_n_types() - 1; i >= 0; --i)
172                 free_type(get_irp_type(i));
173
174         finish_op();
175         free_ir_prog();
176
177         finish_tarval();
178         finish_mode();
179         finish_tpop();
180         finish_ident();
181 }
182
183 /* Returns the libFirm version number. */
184 void firm_get_version(firm_version_t *version) {
185         version->major    = libfirm_VERSION_MAJOR;
186         version->minor    = libfirm_VERSION_MINOR;
187 #ifdef libfirm_VERSION_REVISION
188         version->revision = libfirm_VERSION_REVISION;
189 #else
190         version->revision = "";
191 #endif
192         version->build    = "";
193 }