The ir_op's are stored inside the irp, so don't kill the irp()
[libfirm] / ir / common / firm.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/common/firm.c
4  * Purpose:     Central firm functionality.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef HAVE_STRING_H
18 # include <string.h>
19 #endif
20
21 # include <stdio.h>
22
23 #ifdef WITH_LIBCORE
24 #include <libcore/lc_opts.h>
25 #endif
26
27 # include "ident_t.h"
28 # include "firm.h"
29 # include "irflag_t.h"
30 # include "mangle.h"
31 /* init functions are not public */
32 # include "tv_t.h"
33 # include "tpop_t.h"
34 # include "irprog_t.h"
35 # include "irnode_t.h"
36 # include "irmode_t.h"
37 # include "ircons_t.h"
38 # include "irgraph_t.h"
39 # include "type_t.h"
40 # include "entity_t.h"
41 # include "type_identify.h"
42 # include "firmstat.h"
43 # include "irreflect_t.h"
44 # include "irarch.h"
45 # include "reassoc_t.h"
46 # include "irhooks.h"
47 # include "iredges_t.h"
48 # include "debugger.h"
49
50 #ifdef WITH_LIBCORE
51 lc_opt_entry_t *firm_opt_get_root(void)
52 {
53         static lc_opt_entry_t *grp = NULL;
54         if(!grp)
55                 grp = lc_opt_get_grp(lc_opt_root_grp(), "firm");
56         return grp;
57 }
58 #endif
59
60 void
61 init_firm(const firm_parameter_t *param)
62 {
63   firm_parameter_t def_params;
64   unsigned int     size;
65
66   memset(&def_params, 0, sizeof(def_params));
67
68   if (param) {
69     /* check for reasonable size */
70     assert(param->size <= sizeof(def_params) && (param->size & 3) == 0 &&
71            "parameter struct not initialized ???");
72     size = sizeof(def_params);
73     if (param->size < size)
74       size = param->size;
75
76     memcpy(&def_params, param, size);
77   }
78
79         /* initialize firm flags */
80         firm_init_flags();
81   /* initialize all ident stuff */
82   init_ident(def_params.id_if, 1024);
83   /* initialize Firm hooks */
84   init_hooks();
85   /* enhanced statistics, need idents and hooks */
86   init_stat(def_params.enable_statistics);
87   /* Edges need hooks. */
88   init_edges();
89   /* create the type kinds. */
90   init_tpop();
91   /* create an obstack and put all tarvals in a pdeq */
92   init_tarval_1(0l);
93   /* Builds a basic program representation, so modes can be added. */
94   init_irprog_1();
95   /* initialize all modes an ir node can consist of */
96   init_mode();
97   /* initialize tarvals, and floating point arithmetic */
98   init_tarval_2();
99   /* init graph construction */
100   firm_init_irgraph();
101   /* kind of obstack initialization */
102   firm_init_mangle();
103   /* initialize all op codes an irnode can consist of */
104   init_op();
105   /* called once for each run of this library */
106   init_cons(def_params.initialize_local_func);
107   /* initialize reassociation */
108   firm_init_reassociation();
109   /* Builds a construct allowing to access all information to be constructed
110      later. */
111   init_irprog_2();
112   /* Initialize the type module and construct some idents needed. */
113   firm_init_type(def_params.builtin_dbg, def_params.cc_mask);
114   /* initialize the entity module */
115   firm_init_entity();
116   /* allocate a hash table. */
117   init_type_identify(def_params.ti_if);
118   /* Init reflection facility. */
119   firm_init_rflct();
120
121   /* Init architecture dependent optimizations. */
122   arch_dep_init(arch_dep_default_factory);
123   arch_dep_set_opts(arch_dep_mul_to_shift | arch_dep_div_by_const | arch_dep_mod_by_const);
124
125   firm_archops_init(def_params.arch_op_settings);
126
127 #ifndef NDEBUG
128   /* integrated debugger extension */
129   firm_init_debugger();
130 #endif
131
132 #ifdef WITH_LIBCORE
133         /* Process command line and ini file. */
134
135         if(def_params.ini_file) {
136                 FILE *f = fopen(def_params.ini_file, "rt");
137                 if(f) {
138                         lc_opt_from_file(def_params.ini_file, f, NULL);
139                         fclose(f);
140                 }
141         }
142
143         lc_opt_from_argv(firm_opt_get_root(), def_params.arg_prefix,
144                         def_params.argc, def_params.argv, NULL);
145
146 #endif
147 }
148
149 void free_firm(void) {
150   int i;
151
152   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
153     free_ir_graph(get_irp_irg(i));
154
155   free_type_entities(get_glob_type());
156   for (i = get_irp_n_types() - 1; i >= 0; --i)
157     free_type_entities(get_irp_type(i));
158
159   for (i = get_irp_n_types() - 1; i >= 0; --i)
160     free_type(get_irp_type(i));
161
162   finish_op();
163   free_ir_prog();
164
165   finish_tarval();
166   finish_mode();
167   finish_tpop();
168   finish_ident();
169 }
170
171 /* Returns the libFirm version number. */
172 void firm_get_version(firm_version_t *version) {
173   version->major = libFirm_VERSION_MAJOR;
174   version->minor = libFirm_VERSION_MINOR;
175 }