624221508afefa606bf5a79c7320a24b6b76f2d5
[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 /* Ini file in local directory */
14 #define LOCAL_INI_FILE "firm.ini"
15
16 /* Includes to determine user's home directory */
17 #ifdef _WIN32
18 #include <ShlObj.h>
19 #define HOME_DIR_INI_FILE LOCAL_INI_FILE
20 #else
21 #include <sys/types.h>
22 #include <pwd.h>
23 #define HOME_DIR_INI_FILE ".firmrc"
24 #endif
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
34 # include <stdio.h>
35
36 #ifdef WITH_LIBCORE
37 #include <libcore/lc_opts.h>
38 #endif
39
40 # include "ident_t.h"
41 # include "firm.h"
42 # include "irflag_t.h"
43 # include "mangle.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 "type_identify.h"
55 # include "firmstat.h"
56 # include "irreflect_t.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 lc_opt_entry_t *firm_opt_get_root(void)
65 {
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 #endif
72
73 void
74 init_firm(const firm_parameter_t *param)
75 {
76   firm_parameter_t def_params;
77   unsigned int     size;
78
79   memset(&def_params, 0, sizeof(def_params));
80
81   if (param) {
82     /* check for reasonable size */
83     assert(param->size <= sizeof(def_params) && (param->size & 3) == 0 &&
84            "parameter struct not initialized ???");
85     size = sizeof(def_params);
86     if (param->size < size)
87       size = param->size;
88
89     memcpy(&def_params, param, size);
90   }
91
92         /* initialize firm flags */
93         firm_init_flags();
94   /* initialize all ident stuff */
95   init_ident(def_params.id_if, 1024);
96   /* initialize Firm hooks */
97   firm_init_hooks();
98   /* enhanced statistics, need idents and hooks */
99   firm_init_stat(def_params.enable_statistics);
100   /* Edges need hooks. */
101   init_edges();
102   /* create the type kinds. */
103   init_tpop();
104   /* create an obstack and put all tarvals in a pdeq */
105   init_tarval_1(0l);
106   /* Builds a basic program representation, so modes can be added. */
107   init_irprog_1();
108   /* initialize all modes an ir node can consist of */
109   init_mode();
110   /* initialize tarvals, and floating point arithmetic */
111   init_tarval_2();
112   /* init graph construction */
113   firm_init_irgraph();
114   /* kind of obstack initialization */
115   firm_init_mangle();
116   /* initialize all op codes an irnode can consist of */
117   init_op();
118   /* called once for each run of this library */
119   init_cons(def_params.initialize_local_func);
120   /* initialize reassociation */
121   firm_init_reassociation();
122   /* Builds a construct allowing to access all information to be constructed
123      later. */
124   init_irprog_2();
125   /* Initialize the type module ancd cr d construct some idents needed. */
126   firm_init_type(def_params.builtin_dbg, def_params.cc_mask);
127   /* initialize the entity module */
128   firm_init_entity();
129   /* allocate a hash table. */
130   init_type_identify(def_params.ti_if);
131   /* Init reflection facility. */
132   firm_init_rflct();
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 #ifndef NDEBUG
141   /* integrated debugger extension */
142   firm_init_debugger();
143 #endif
144
145 #ifdef WITH_LIBCORE
146   {
147           FILE *f;
148
149 #ifdef _WIN32
150           char path[MAX_PATH];
151           SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, path);
152           strncat(path, "\\", sizeof(path));
153 #else
154           char path[512];
155           strcpy(path, getpwuid(getuid())->pw_dir);
156           strncat(path, "/", sizeof(path));
157 #endif
158
159           strncat(path, HOME_DIR_INI_FILE, sizeof(path));
160
161           /* Process ini file in user's home. */
162           f = fopen(path, "rt");
163           if(f) {
164                   lc_opt_from_file(path, f, NULL);
165                   fclose(f);
166           }
167
168           /* Process ini file in current directory. */
169           f = fopen(LOCAL_INI_FILE, "rt");
170           if(f) {
171                   lc_opt_from_file(LOCAL_INI_FILE, f, NULL);
172                   fclose(f);
173           }
174
175           /* process arguments from the command line */
176           lc_opt_from_argv(firm_opt_get_root(), def_params.arg_prefix, def_params.argc, def_params.argv, NULL);
177   }
178 #endif
179 }
180
181 void free_firm(void) {
182   int i;
183
184   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
185     free_ir_graph(get_irp_irg(i));
186
187   free_type_entities(get_glob_type());
188   for (i = get_irp_n_types() - 1; i >= 0; --i)
189     free_type_entities(get_irp_type(i));
190
191   for (i = get_irp_n_types() - 1; i >= 0; --i)
192     free_type(get_irp_type(i));
193
194   finish_op();
195   free_ir_prog();
196
197   finish_tarval();
198   finish_mode();
199   finish_tpop();
200   finish_ident();
201 }
202
203 /* Returns the libFirm version number. */
204 void firm_get_version(firm_version_t *version) {
205   version->major = libFirm_VERSION_MAJOR;
206   version->minor = libFirm_VERSION_MINOR;
207 }