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