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