Define architecture-dependent constants within the Asm target only
This commit is contained in:
parent
ee6396b2e0
commit
8b09da5042
22
src/main.c
22
src/main.c
|
@ -15,7 +15,6 @@
|
|||
|
||||
|
||||
|
||||
#include <sys/utsname.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
@ -82,7 +81,6 @@ static int _usage(void)
|
|||
|
||||
/* public */
|
||||
/* main */
|
||||
static int _main_default_defines(C99Prefs * prefs);
|
||||
static int _main_default_paths(C99Prefs * prefs);
|
||||
static int _main_add_define(C99Prefs * prefs, char * define);
|
||||
static int _main_add_path(C99Prefs * prefs, char const * path);
|
||||
|
@ -96,8 +94,7 @@ int main(int argc, char * argv[])
|
|||
int o;
|
||||
|
||||
memset(&prefs, 0, sizeof(prefs));
|
||||
if(_main_default_defines(&prefs) != 0
|
||||
|| _main_default_paths(&prefs) != 0)
|
||||
if(_main_default_paths(&prefs) != 0)
|
||||
return 2;
|
||||
while((o = getopt(argc, argv, "cD:EgI:L:m:M:o:O123sU:W")) != -1)
|
||||
switch(o)
|
||||
|
@ -167,23 +164,6 @@ int main(int argc, char * argv[])
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int _main_default_defines(C99Prefs * prefs)
|
||||
/* FIXME define these in the "asm" plug-in instead */
|
||||
{
|
||||
struct utsname uts;
|
||||
static char sysname[sizeof(uts.sysname) + 7];
|
||||
static char machine[sizeof(uts.machine) + 7];
|
||||
|
||||
if(uname(&uts) != 0)
|
||||
return error_set_print(PROGNAME, 1, "%s", strerror(errno));
|
||||
snprintf(sysname, sizeof(sysname), "__%s__=1", uts.sysname);
|
||||
snprintf(machine, sizeof(machine), "__%s__=1", uts.machine);
|
||||
if(_main_add_define(prefs, sysname) != 0
|
||||
|| _main_add_define(prefs, machine) != 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _main_default_paths(C99Prefs * prefs)
|
||||
{
|
||||
char * paths[] = { "/usr/include" };
|
||||
|
|
|
@ -16,17 +16,19 @@
|
|||
|
||||
|
||||
#include <Devel/Asm.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include "C99/target.h"
|
||||
#ifdef DEBUG
|
||||
# include "../../config.h"
|
||||
#endif
|
||||
|
||||
|
||||
/* as */
|
||||
/* asm */
|
||||
/* private */
|
||||
/* types */
|
||||
typedef enum _AsmOption
|
||||
|
@ -40,6 +42,8 @@ typedef enum _AsmOption
|
|||
typedef struct _AsmTargetArch
|
||||
{
|
||||
char const * name;
|
||||
int (*init)(C99Helper * helper, char const * arch);
|
||||
void (*destroy)(C99Helper * helper);
|
||||
int (*function_begin)(char const * name);
|
||||
int (*function_call)(char const * name);
|
||||
int (*function_end)(void);
|
||||
|
@ -57,22 +61,6 @@ static C99Option _asm_options[ASO_COUNT + 1] =
|
|||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
/* platforms */
|
||||
#include "asm/amd64.c"
|
||||
#include "asm/i386.c"
|
||||
#include "asm/i486.c"
|
||||
#include "asm/i586.c"
|
||||
#include "asm/i686.c"
|
||||
|
||||
static AsmTargetArch * _asm_arch[] =
|
||||
{
|
||||
&_asm_arch_amd64,
|
||||
&_asm_arch_i386,
|
||||
&_asm_arch_i486,
|
||||
&_asm_arch_i586,
|
||||
&_asm_arch_i686
|
||||
};
|
||||
|
||||
|
||||
/* protected */
|
||||
/* prototypes */
|
||||
|
@ -97,18 +85,36 @@ C99TargetPlugin target_plugin =
|
|||
NULL /* FIXME implement label_set */
|
||||
};
|
||||
|
||||
/* platforms */
|
||||
#include "asm/amd64.c"
|
||||
#include "asm/i386.c"
|
||||
#include "asm/i486.c"
|
||||
#include "asm/i586.c"
|
||||
#include "asm/i686.c"
|
||||
|
||||
static AsmTargetArch * _asm_arch[] =
|
||||
{
|
||||
&_asm_arch_amd64,
|
||||
&_asm_arch_i386,
|
||||
&_asm_arch_i486,
|
||||
&_asm_arch_i586,
|
||||
&_asm_arch_i686
|
||||
};
|
||||
|
||||
|
||||
/* protected */
|
||||
/* functions */
|
||||
/* asm_init */
|
||||
static int _init_arch(char const * arch);
|
||||
static int _init_defines(char const * format);
|
||||
static int _init_arch(C99Helper * helper, char const * arch);
|
||||
static int _init_format(C99Helper * helper, char const * format);
|
||||
|
||||
static int _asm_init(char const * outfile, int optlevel)
|
||||
{
|
||||
C99 * c99;
|
||||
char const * arch = _asm_options[ASO_ARCH].value;
|
||||
char const * format = _asm_options[ASO_FORMAT].value;
|
||||
|
||||
c99 = target_plugin.helper->c99;
|
||||
_asm_optlevel = optlevel;
|
||||
if((_asm_as = asm_new(arch, format)) == NULL)
|
||||
return 1;
|
||||
|
@ -120,8 +126,9 @@ static int _asm_init(char const * outfile, int optlevel)
|
|||
fprintf(stderr, "DEBUG: %s: architecture \"%s\", format \"%s\"\n",
|
||||
PACKAGE, asm_get_arch(_asm_as), asm_get_format(_asm_as));
|
||||
#endif
|
||||
if(_init_arch(asm_get_arch(_asm_as)) != 0
|
||||
|| _init_defines(asm_get_format(_asm_as)) != 0
|
||||
if(_init_arch(target_plugin.helper, asm_get_arch(_asm_as)) != 0
|
||||
|| _init_format(target_plugin.helper,
|
||||
asm_get_format(_asm_as)) != 0
|
||||
|| asm_open_assemble(_asm_as, outfile) != 0
|
||||
|| _asm_section(".text") != 0)
|
||||
{
|
||||
|
@ -134,10 +141,12 @@ static int _asm_init(char const * outfile, int optlevel)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int _init_arch(char const * arch)
|
||||
static int _init_arch(C99Helper * helper, char const * arch)
|
||||
{
|
||||
AsmTargetArch * aarch = NULL;
|
||||
size_t i;
|
||||
struct utsname uts;
|
||||
static char sysname[sizeof(uts.sysname) + 5];
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, arch);
|
||||
|
@ -151,21 +160,26 @@ static int _init_arch(char const * arch)
|
|||
if(aarch == NULL)
|
||||
return -error_set_code(1, "%s%s", "Unsupported architecture ",
|
||||
arch);
|
||||
if(uname(&uts) != 0)
|
||||
return -error_set_code(1, "%s", strerror(errno));
|
||||
snprintf(sysname, sizeof(sysname), "__%s__", uts.sysname);
|
||||
if(helper->define_add(helper->c99, sysname, "1") != 0)
|
||||
return -1;
|
||||
if(aarch->init != NULL && aarch->init(helper, arch) != 0)
|
||||
return -1;
|
||||
target_plugin.function_begin = aarch->function_begin;
|
||||
target_plugin.function_call = aarch->function_call;
|
||||
target_plugin.function_end = aarch->function_end;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _init_defines(char const * format)
|
||||
static int _init_format(C99Helper * helper, char const * format)
|
||||
{
|
||||
C99 * c99;
|
||||
size_t len;
|
||||
char * p;
|
||||
size_t i;
|
||||
int c;
|
||||
|
||||
c99 = target_plugin.helper->c99;
|
||||
len = strlen(format) + 5;
|
||||
if((p = malloc(len)) == NULL)
|
||||
return 1;
|
||||
|
@ -175,7 +189,7 @@ static int _init_defines(char const * format)
|
|||
c = p[i];
|
||||
p[i] = toupper(c);
|
||||
}
|
||||
target_plugin.helper->define_add(c99, p, NULL);
|
||||
helper->define_add(helper->c99, p, NULL);
|
||||
free(p);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
/* amd64 */
|
||||
/* prototypes */
|
||||
static int _asm_arch_amd64_init(C99Helper * helper, char const * name);
|
||||
static int _asm_arch_amd64_function_begin(char const * name);
|
||||
static int _asm_arch_amd64_function_call(char const * name);
|
||||
static int _asm_arch_amd64_function_end(void);
|
||||
|
@ -26,6 +27,8 @@ static int _asm_arch_amd64_function_end(void);
|
|||
static AsmTargetArch _asm_arch_amd64 =
|
||||
{
|
||||
"amd64",
|
||||
_asm_arch_amd64_init,
|
||||
NULL,
|
||||
_asm_arch_amd64_function_begin,
|
||||
_asm_arch_amd64_function_call,
|
||||
_asm_arch_amd64_function_end
|
||||
|
@ -33,6 +36,13 @@ static AsmTargetArch _asm_arch_amd64 =
|
|||
|
||||
|
||||
/* functions */
|
||||
/* asm_arch_amd64_init */
|
||||
static int _asm_arch_amd64_init(C99Helper * helper, char const * name)
|
||||
{
|
||||
return helper->define_add(helper->c99, "__amd64__", "1");
|
||||
}
|
||||
|
||||
|
||||
/* asm_arch_amd64_function_begin */
|
||||
static int _asm_arch_amd64_function_begin(char const * name)
|
||||
{
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
/* i386 */
|
||||
/* prototypes */
|
||||
static int _asm_arch_i386_init(C99Helper * helper, char const * name);
|
||||
static int _asm_arch_i386_function_begin(char const * name);
|
||||
static int _asm_arch_i386_function_call(char const * name);
|
||||
static int _asm_arch_i386_function_end(void);
|
||||
|
@ -26,6 +27,8 @@ static int _asm_arch_i386_function_end(void);
|
|||
static AsmTargetArch _asm_arch_i386 =
|
||||
{
|
||||
"i386",
|
||||
_asm_arch_i386_init,
|
||||
NULL,
|
||||
_asm_arch_i386_function_begin,
|
||||
_asm_arch_i386_function_call,
|
||||
_asm_arch_i386_function_end
|
||||
|
@ -33,6 +36,13 @@ static AsmTargetArch _asm_arch_i386 =
|
|||
|
||||
|
||||
/* functions */
|
||||
/* asm_arch_i386_init */
|
||||
static int _asm_arch_i386_init(C99Helper * helper, char const * name)
|
||||
{
|
||||
return helper->define_add(helper->c99, "__i386__", "1");
|
||||
}
|
||||
|
||||
|
||||
/* asm_arch_i386_function_begin */
|
||||
static int _asm_arch_i386_function_begin(char const * name)
|
||||
{
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
static AsmTargetArch _asm_arch_i486 =
|
||||
{
|
||||
"i486",
|
||||
_asm_arch_i386_init,
|
||||
NULL,
|
||||
_asm_arch_i386_function_begin,
|
||||
_asm_arch_i386_function_call,
|
||||
_asm_arch_i386_function_end
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
static AsmTargetArch _asm_arch_i586 =
|
||||
{
|
||||
"i586",
|
||||
_asm_arch_i386_init,
|
||||
NULL,
|
||||
_asm_arch_i386_function_begin,
|
||||
_asm_arch_i386_function_call,
|
||||
_asm_arch_i386_function_end
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
static AsmTargetArch _asm_arch_i686 =
|
||||
{
|
||||
"i686",
|
||||
_asm_arch_i386_init,
|
||||
NULL,
|
||||
_asm_arch_i386_function_begin,
|
||||
_asm_arch_i386_function_call,
|
||||
_asm_arch_i386_function_end
|
||||
|
|
Loading…
Reference in New Issue
Block a user