ba05a4fce2d262d9e104f63c649641387c838511
[cparser] / driver / firm_machine.c
1 #include <config.h>
2
3 #include <assert.h>
4 #include <stdbool.h>
5 #include "firm_machine.h"
6 #include "adt/strutil.h"
7 #include "adt/xmalloc.h"
8 #include <libfirm/firm.h>
9
10 static void set_be_option(const char *arg)
11 {
12         int res = be_parse_arg(arg);
13         (void) res;
14         assert(res);
15 }
16
17 static ir_entity *underscore_compilerlib_entity_creator(ident *id, ir_type *mt)
18 {
19         ir_entity *ent    = new_entity(get_glob_type(), id, mt);
20         ident     *ldname = id_mangle3("_", id, "");
21
22         set_entity_ld_ident(ent, ldname);
23
24         return ent;
25 }
26
27 /**
28  * Initialize firm codegeneration for a specific operating system.
29  * The argument is the operating system part of a target-triple
30  */
31 static bool setup_os_support(const char *os)
32 {
33         if (strstr(os, "linux") != NULL || strstr(os, "bsd") != NULL
34                         || streq(os, "solaris")) {
35                 set_be_option("ia32-gasmode=elf");
36         } else if (streq(os, "darwin")) {
37                 set_be_option("ia32-gasmode=macho");
38                 set_be_option("ia32-stackalign=4");
39                 set_be_option("pic=true");
40                 set_compilerlib_entity_creator(underscore_compilerlib_entity_creator);
41         } else if (strstr(os, "mingw") != NULL || streq(os, "win32")) {
42                 set_be_option("ia32-gasmode=mingw");
43                 set_compilerlib_entity_creator(underscore_compilerlib_entity_creator);
44         } else {
45                 return false;
46         }
47
48         return true;
49 }
50
51 bool setup_firm_for_machine(const machine_triple_t *machine)
52 {
53         const char *cpu = machine->cpu_type;
54
55         if (streq(cpu, "i386")) {
56                 set_be_option("isa=ia32");
57                 set_be_option("ia32-arch=i386");
58         } else if (streq(cpu, "i486")) {
59                 set_be_option("isa=ia32");
60                 set_be_option("ia32-arch=i486");
61         } else if (streq(cpu, "i586")) {
62                 set_be_option("isa=ia32");
63                 set_be_option("ia32-arch=i586");
64         } else if (streq(cpu, "i686")) {
65                 set_be_option("isa=ia32");
66                 set_be_option("ia32-arch=i686");
67         } else if (streq(cpu, "i786")) {
68                 set_be_option("isa=ia32");
69                 set_be_option("ia32-arch=pentium4");
70         } else if (streq(cpu, "x86_64")) {
71                 set_be_option("isa=amd64");
72         } else if (streq(cpu, "sparc")) {
73                 set_be_option("isa=sparc");
74         } else if (streq(cpu, "arm")) {
75                 set_be_option("isa=arm");
76         } else {
77                 fprintf(stderr, "Unknown cpu '%s' in target-triple\n", cpu);
78                 return false;
79         }
80
81         /* process operating system */
82         if (!setup_os_support(machine->operating_system)) {
83                 fprintf(stderr, "Unknown operating system '%s' in target-triple\n", machine->operating_system);
84                 return false;
85         }
86         return true;
87 }
88
89 machine_triple_t *firm_get_host_machine(void)
90 {
91         machine_triple_t *machine = XMALLOC(machine_triple_t);
92         machine->cpu_type = xstrdup("i386");
93         machine->manufacturer = xstrdup("unknown");
94 #if defined(_WIN32) || defined(__CYGWIN__)
95         machine->operating_system = xstrdup("win32");
96 #elif defined(__APPLE__)
97         machine->operating_system = xstrdup("darwin");
98 #else
99         machine->operating_system = xstrdup("linux");
100 #endif
101         return machine;
102 }
103
104 void firm_free_machine_triple(machine_triple_t *machine)
105 {
106         free(machine->cpu_type);
107         free(machine->manufacturer);
108         free(machine->operating_system);
109         free(machine);
110 }
111
112 machine_triple_t *firm_parse_machine_triple(const char *triple_string)
113 {
114         const char *manufacturer = strchr(triple_string, '-');
115         if (manufacturer == NULL) {
116                 return NULL;
117         }
118         manufacturer += 1;
119
120         const char *os = strchr(manufacturer, '-');
121         if (os == NULL) {
122                 return false;
123         }
124         os += 1;
125
126         /* Note: Triples are more or less defined by what the config.guess and
127          * config.sub scripts from GNU autoconf emit. We have to lookup there what
128          * triples are possible */
129
130         const char *cpu = triple_string;
131
132         machine_triple_t *triple = XMALLOCZ(machine_triple_t);
133
134         size_t cpu_type_len = manufacturer-cpu;
135         triple->cpu_type = XMALLOCN(char, cpu_type_len);
136         memcpy(triple->cpu_type, cpu, cpu_type_len-1);
137         triple->cpu_type[cpu_type_len-1] = '\0';
138
139         /* process manufacturer, alot of people incorrectly leave out the
140          * manufacturer instead of using unknown- */
141         if (strstart(manufacturer, "linux")) {
142                 triple->manufacturer = xstrdup("unknown");
143                 os = manufacturer;
144         } else {
145                 size_t manufacturer_len = os-manufacturer;
146                 triple->manufacturer = XMALLOCN(char, manufacturer_len);
147                 memcpy(triple->manufacturer, manufacturer, manufacturer_len-1);
148                 triple->manufacturer[manufacturer_len-1] = '\0';
149         }
150
151         triple->operating_system = xstrdup(os);
152         return triple;
153 }