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