5 #include "firm_machine.h"
6 #include "adt/strutil.h"
7 #include "adt/xmalloc.h"
8 #include <libfirm/firm.h>
10 static void set_be_option(const char *arg)
12 int res = be_parse_arg(arg);
17 static ir_entity *underscore_compilerlib_entity_creator(ident *id, ir_type *mt)
19 ir_entity *ent = new_entity(get_glob_type(), id, mt);
20 ident *ldname = id_mangle3("_", id, "");
22 set_entity_ld_ident(ent, ldname);
28 * Initialize firm codegeneration for a specific operating system.
29 * The argument is the operating system part of a target-triple
31 static bool setup_os_support(const char *os)
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);
51 bool setup_firm_for_machine(const machine_triple_t *machine)
53 const char *cpu = machine->cpu_type;
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");
77 fprintf(stderr, "Unknown cpu '%s' in target-triple\n", cpu);
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);
89 machine_triple_t *firm_get_host_machine(void)
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");
99 machine->operating_system = xstrdup("linux");
104 void firm_free_machine_triple(machine_triple_t *machine)
106 free(machine->cpu_type);
107 free(machine->manufacturer);
108 free(machine->operating_system);
112 machine_triple_t *firm_parse_machine_triple(const char *triple_string)
114 const char *manufacturer = strchr(triple_string, '-');
115 if (manufacturer == NULL) {
120 const char *os = strchr(manufacturer, '-');
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 */
130 const char *cpu = triple_string;
132 machine_triple_t *triple = XMALLOCZ(machine_triple_t);
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';
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");
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';
151 triple->operating_system = xstrdup(os);