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