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