From: Peter Johnson Date: Sun, 22 Apr 2007 03:32:46 +0000 (-0000) Subject: Bite the bullet and convert tabs to spaces. Previously yasm's source has X-Git-Tag: v0.6.1~15^2~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=00473ca98171eb19615109f5a8512993da43a77a;p=yasm Bite the bullet and convert tabs to spaces. Previously yasm's source has been using a mix of tabs and 4 spaces to indent; this looks horrible if tab size is ever not 8. While I debated converting to tab-only indentation that would have been a far higher impact to the source. svn path=/trunk/yasm/; revision=1825 --- diff --git a/Mkfiles/dj/config.h b/Mkfiles/dj/config.h index 42bc8ca5..759ced57 100644 --- a/Mkfiles/dj/config.h +++ b/Mkfiles/dj/config.h @@ -1,8 +1,8 @@ /* $Id$ */ -#define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) -#define yasm__abspath(path) yasm__abspath_win(path) -#define yasm__combpath(from, to) yasm__combpath_win(from, to) +#define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) +#define yasm__abspath(path) yasm__abspath_win(path) +#define yasm__combpath(from, to) yasm__combpath_win(from, to) /* */ /* #undef ENABLE_NLS */ diff --git a/Mkfiles/vc/config.h b/Mkfiles/vc/config.h index 22008e47..942f3224 100644 --- a/Mkfiles/vc/config.h +++ b/Mkfiles/vc/config.h @@ -1,8 +1,8 @@ /* $Id$ */ -#define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) -#define yasm__abspath(path) yasm__abspath_win(path) -#define yasm__combpath(from, to) yasm__combpath_win(from, to) +#define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) +#define yasm__abspath(path) yasm__abspath_win(path) +#define yasm__combpath(from, to) yasm__combpath_win(from, to) /* */ /* #undef ENABLE_NLS */ diff --git a/Mkfiles/vc8/config.h b/Mkfiles/vc8/config.h index ece338b3..91168c61 100644 --- a/Mkfiles/vc8/config.h +++ b/Mkfiles/vc8/config.h @@ -1,8 +1,8 @@ /* $Id: config.h 1137 2004-09-04 01:24:57Z peter $ */ -#define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) -#define yasm__abspath(path) yasm__abspath_win(path) -#define yasm__combpath(from, to) yasm__combpath_win(from, to) +#define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) +#define yasm__abspath(path) yasm__abspath_win(path) +#define yasm__combpath(from, to) yasm__combpath_win(from, to) /* */ /* #undef ENABLE_NLS */ diff --git a/frontends/yasm/yasm-options.c b/frontends/yasm/yasm-options.c index fed8d694..87f6e240 100644 --- a/frontends/yasm/yasm-options.c +++ b/frontends/yasm/yasm-options.c @@ -43,7 +43,7 @@ /* Options Parser */ int parse_cmdline(int argc, char **argv, opt_option *options, size_t nopts, - void (*print_error) (const char *fmt, ...)) + void (*print_error) (const char *fmt, ...)) { int errors = 0, warnings = 0; size_t i; @@ -53,99 +53,99 @@ parse_cmdline(int argc, char **argv, opt_option *options, size_t nopts, fail: while (--argc) { - argv++; - - if (argv[0][0] == '-') { /* opt */ - got_it = 0; - if (argv[0][1] == '-') { /* lopt */ - if (argv[0][2] == '\0') { /* --, end of options */ - /* Handle rest of args as non-options */ - while (--argc) { - argv++; - if (not_an_option_handler(argv[0])) - errors++; - } - return errors; - } - - for (i = 0; i < nopts; i++) { - if (options[i].lopt && - strncmp(&argv[0][2], options[i].lopt, - strlen(options[i].lopt)) == 0) { - char *param; - - if (options[i].takes_param) { - param = strchr(&argv[0][2], '='); - if (!param) { - print_error( - _("option `--%s' needs an argument!"), - options[i].lopt); - errors++; - goto fail; - } else { - *param = '\0'; - param++; - } - } else - param = NULL; - - if (!options[i]. - handler(&argv[0][2], param, options[i].extra)) - got_it = 1; - break; - } - } - if (!got_it && !other_option_handler(argv[0])) - got_it = 1; - if (!got_it) { - print_error(_("warning: unrecognized option `%s'"), - argv[0]); - warnings++; - } - } else if (argv[0][1] == '\0') { /* just -, is non-option */ - if (not_an_option_handler(argv[0])) - errors++; - } else { /* sopt */ - for (i = 0; i < nopts; i++) { - if (argv[0][1] == options[i].sopt) { - char *cmd = &argv[0][1]; - char *param; - - if (options[i].takes_param) { - param = argv[1]; - if (argv[0][2] != '\0') - param = &argv[0][2]; - else if (param == NULL || *param == '-') { - print_error( - _("option `-%c' needs an argument!"), - options[i].sopt); - errors++; - goto fail; - } else { - argc--; - argv++; - } - } else - param = NULL; - - if (!options[i].handler(cmd, param, options[i].extra)) - got_it = 1; - break; - } - } - if (!got_it && !other_option_handler(argv[0])) - got_it = 1; - if (!got_it) { - print_error(_("warning: unrecognized option `%s'"), - argv[0]); - warnings++; - } - } - } else { /* not an option, then it should be a file or something */ - - if (not_an_option_handler(argv[0])) - errors++; - } + argv++; + + if (argv[0][0] == '-') { /* opt */ + got_it = 0; + if (argv[0][1] == '-') { /* lopt */ + if (argv[0][2] == '\0') { /* --, end of options */ + /* Handle rest of args as non-options */ + while (--argc) { + argv++; + if (not_an_option_handler(argv[0])) + errors++; + } + return errors; + } + + for (i = 0; i < nopts; i++) { + if (options[i].lopt && + strncmp(&argv[0][2], options[i].lopt, + strlen(options[i].lopt)) == 0) { + char *param; + + if (options[i].takes_param) { + param = strchr(&argv[0][2], '='); + if (!param) { + print_error( + _("option `--%s' needs an argument!"), + options[i].lopt); + errors++; + goto fail; + } else { + *param = '\0'; + param++; + } + } else + param = NULL; + + if (!options[i]. + handler(&argv[0][2], param, options[i].extra)) + got_it = 1; + break; + } + } + if (!got_it && !other_option_handler(argv[0])) + got_it = 1; + if (!got_it) { + print_error(_("warning: unrecognized option `%s'"), + argv[0]); + warnings++; + } + } else if (argv[0][1] == '\0') { /* just -, is non-option */ + if (not_an_option_handler(argv[0])) + errors++; + } else { /* sopt */ + for (i = 0; i < nopts; i++) { + if (argv[0][1] == options[i].sopt) { + char *cmd = &argv[0][1]; + char *param; + + if (options[i].takes_param) { + param = argv[1]; + if (argv[0][2] != '\0') + param = &argv[0][2]; + else if (param == NULL || *param == '-') { + print_error( + _("option `-%c' needs an argument!"), + options[i].sopt); + errors++; + goto fail; + } else { + argc--; + argv++; + } + } else + param = NULL; + + if (!options[i].handler(cmd, param, options[i].extra)) + got_it = 1; + break; + } + } + if (!got_it && !other_option_handler(argv[0])) + got_it = 1; + if (!got_it) { + print_error(_("warning: unrecognized option `%s'"), + argv[0]); + warnings++; + } + } + } else { /* not an option, then it should be a file or something */ + + if (not_an_option_handler(argv[0])) + errors++; + } } DEBUG((stderr, "parse_cmdline: finished\n")); @@ -161,50 +161,50 @@ help_msg(const char *msg, const char *tail, opt_option *options, size_t nopts) printf("%s", gettext(msg)); for (i = 0; i < nopts; i++) { - size_t shortopt_len = 0; - size_t longopt_len = 0; - - optbuf[0] = 0; - optopt[0] = 0; - - if (options[i].takes_param) { - if (options[i].sopt) { - sprintf(optbuf, "-%c <%s>", options[i].sopt, - options[i].param_desc ? options[i]. - param_desc : _("param")); - shortopt_len = strlen(optbuf); - } - if (options[i].sopt && options[i].lopt) - strcat(optbuf, ", "); - if (options[i].lopt) { - sprintf(optopt, "--%s=<%s>", options[i].lopt, - options[i].param_desc ? options[i]. - param_desc : _("param")); - strcat(optbuf, optopt); - longopt_len = strlen(optbuf); - } - } else { - if (options[i].sopt) { - sprintf(optbuf, "-%c", options[i].sopt); - shortopt_len = strlen(optbuf); - } - if (options[i].sopt && options[i].lopt) - strcat(optbuf, ", "); - if (options[i].lopt) { - sprintf(optopt, "--%s", options[i].lopt); - strcat(optbuf, optopt); - longopt_len = strlen(optbuf); - } - } - - /* split [-s ], [--long ] if it destroys columns */ - if (shortopt_len && longopt_len && longopt_len > 22) { - optbuf[shortopt_len] = '\0'; - printf(" %-22s %s\n", optopt, gettext(options[i].description)); - printf(" %s\n", optbuf); - } - else - printf(" %-22s %s\n", optbuf, gettext(options[i].description)); + size_t shortopt_len = 0; + size_t longopt_len = 0; + + optbuf[0] = 0; + optopt[0] = 0; + + if (options[i].takes_param) { + if (options[i].sopt) { + sprintf(optbuf, "-%c <%s>", options[i].sopt, + options[i].param_desc ? options[i]. + param_desc : _("param")); + shortopt_len = strlen(optbuf); + } + if (options[i].sopt && options[i].lopt) + strcat(optbuf, ", "); + if (options[i].lopt) { + sprintf(optopt, "--%s=<%s>", options[i].lopt, + options[i].param_desc ? options[i]. + param_desc : _("param")); + strcat(optbuf, optopt); + longopt_len = strlen(optbuf); + } + } else { + if (options[i].sopt) { + sprintf(optbuf, "-%c", options[i].sopt); + shortopt_len = strlen(optbuf); + } + if (options[i].sopt && options[i].lopt) + strcat(optbuf, ", "); + if (options[i].lopt) { + sprintf(optopt, "--%s", options[i].lopt); + strcat(optbuf, optopt); + longopt_len = strlen(optbuf); + } + } + + /* split [-s ], [--long ] if it destroys columns */ + if (shortopt_len && longopt_len && longopt_len > 22) { + optbuf[shortopt_len] = '\0'; + printf(" %-22s %s\n", optopt, gettext(options[i].description)); + printf(" %s\n", optbuf); + } + else + printf(" %-22s %s\n", optbuf, gettext(options[i].description)); } printf("%s", gettext(tail)); diff --git a/frontends/yasm/yasm-options.h b/frontends/yasm/yasm-options.h index 04cf0434..d78bec30 100644 --- a/frontends/yasm/yasm-options.h +++ b/frontends/yasm/yasm-options.h @@ -42,7 +42,7 @@ typedef struct opt_option_s int takes_param; int (*handler) (char *cmd, /*@null@*/ char *param, int extra); - int extra; /* extra value for handler */ + int extra; /* extra value for handler */ /* description to use in help_msg() */ /*@observer@*/ const char *description; @@ -64,12 +64,12 @@ int other_option_handler(char *option); * nopts - options count */ int parse_cmdline(int argc, char **argv, opt_option *options, size_t nopts, - void (*print_error) (const char *fmt, ...)); + void (*print_error) (const char *fmt, ...)); /* display help message msg followed by list of options in options and followed * by tail */ void help_msg(const char *msg, const char *tail, opt_option *options, - size_t nopts); + size_t nopts); #endif diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index effe0e99..d30539a9 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -66,7 +66,7 @@ static int special_options = 0; static int preproc_only = 0; static unsigned int force_strict = 0; static int generate_make_dependencies = 0; -static int warning_error = 0; /* warnings being treated as errors */ +static int warning_error = 0; /* warnings being treated as errors */ static FILE *errfile; /*@null@*/ /*@only@*/ static char *error_filename = NULL; static enum { @@ -75,10 +75,10 @@ static enum { } ewmsg_style = EWSTYLE_GNU; /*@null@*/ /*@dependent@*/ static FILE *open_file(const char *filename, - const char *mode); + const char *mode); static void check_errors(/*@only@*/ yasm_errwarns *errwarns, - /*@only@*/ yasm_object *object, - /*@only@*/ yasm_linemap *linemap); + /*@only@*/ yasm_object *object, + /*@only@*/ yasm_linemap *linemap); static void cleanup(/*@null@*/ /*@only@*/ yasm_object *object); /* Forward declarations: cmd line parser handlers */ @@ -103,20 +103,20 @@ static int opt_ewmsg_handler(char *cmd, /*@null@*/ char *param, int extra); static int opt_makedep_handler(char *cmd, /*@null@*/ char *param, int extra); static /*@only@*/ char *replace_extension(const char *orig, /*@null@*/ - const char *ext, const char *def); + const char *ext, const char *def); static void print_error(const char *fmt, ...); static /*@exits@*/ void handle_yasm_int_error(const char *file, - unsigned int line, - const char *message); + unsigned int line, + const char *message); static /*@exits@*/ void handle_yasm_fatal(const char *message, va_list va); static const char *handle_yasm_gettext(const char *msgid); static void print_yasm_error(const char *filename, unsigned long line, - const char *msg, /*@null@*/ const char *xref_fn, - unsigned long xref_line, - /*@null@*/ const char *xref_msg); + const char *msg, /*@null@*/ const char *xref_fn, + unsigned long xref_line, + /*@null@*/ const char *xref_msg); static void print_yasm_warning(const char *filename, unsigned long line, - const char *msg); + const char *msg); static void apply_preproc_builtins(void); static void apply_preproc_saved_options(void); @@ -232,83 +232,83 @@ do_preproc_only(FILE *in) /* Default output to stdout if not specified or generating dependency makefiles */ if (!obj_filename || generate_make_dependencies) { - out = stdout; + out = stdout; /* determine the object filename if not specified, but we need a file name for the makefile rule */ if (generate_make_dependencies && !obj_filename) { if (in == stdin) /* Default to yasm.out if no obj filename specified */ - obj_filename = yasm__xstrdup("yasm.out"); + obj_filename = yasm__xstrdup("yasm.out"); else { /* replace (or add) extension to base filename */ - yasm__splitpath(in_filename, &base_filename); - if (base_filename[0] == '\0') - obj_filename = yasm__xstrdup("yasm.out"); - else - obj_filename = replace_extension(base_filename, + yasm__splitpath(in_filename, &base_filename); + if (base_filename[0] == '\0') + obj_filename = yasm__xstrdup("yasm.out"); + else + obj_filename = replace_extension(base_filename, cur_objfmt_module->extension, "yasm.out"); - } + } } } else { - /* Open output (object) file */ - out = open_file(obj_filename, "wt"); - if (!out) { - yasm_xfree(preproc_buf); - return EXIT_FAILURE; - } + /* Open output (object) file */ + out = open_file(obj_filename, "wt"); + if (!out) { + yasm_xfree(preproc_buf); + return EXIT_FAILURE; + } } /* Pre-process until done */ cur_preproc = yasm_preproc_create(cur_preproc_module, in, in_filename, - linemap, errwarns); + linemap, errwarns); apply_preproc_builtins(); apply_preproc_saved_options(); if (generate_make_dependencies) { - size_t totlen; + size_t totlen; fprintf(stdout, "%s: %s", obj_filename, in_filename); - totlen = strlen(obj_filename)+2+strlen(in_filename); - - while ((got = yasm_preproc_get_included_file(cur_preproc, preproc_buf, - PREPROC_BUF_SIZE)) != 0) { - totlen += got; - if (totlen > 72) { - fputs(" \\\n ", stdout); - totlen = 2; - } - fputc(' ', stdout); + totlen = strlen(obj_filename)+2+strlen(in_filename); + + while ((got = yasm_preproc_get_included_file(cur_preproc, preproc_buf, + PREPROC_BUF_SIZE)) != 0) { + totlen += got; + if (totlen > 72) { + fputs(" \\\n ", stdout); + totlen = 2; + } + fputc(' ', stdout); fwrite(preproc_buf, got, 1, stdout); - } - fputc('\n', stdout); + } + fputc('\n', stdout); } else { while ((got = yasm_preproc_input(cur_preproc, preproc_buf, - PREPROC_BUF_SIZE)) != 0) - fwrite(preproc_buf, got, 1, out); + PREPROC_BUF_SIZE)) != 0) + fwrite(preproc_buf, got, 1, out); } if (in != stdin) - fclose(in); + fclose(in); if (out != stdout) - fclose(out); + fclose(out); if (yasm_errwarns_num_errors(errwarns, warning_error) > 0) { - yasm_errwarns_output_all(errwarns, linemap, warning_error, - print_yasm_error, print_yasm_warning); - if (out != stdout) - remove(obj_filename); - yasm_xfree(preproc_buf); - yasm_linemap_destroy(linemap); - yasm_errwarns_destroy(errwarns); - cleanup(NULL); - return EXIT_FAILURE; + yasm_errwarns_output_all(errwarns, linemap, warning_error, + print_yasm_error, print_yasm_warning); + if (out != stdout) + remove(obj_filename); + yasm_xfree(preproc_buf); + yasm_linemap_destroy(linemap); + yasm_errwarns_destroy(errwarns); + cleanup(NULL); + return EXIT_FAILURE; } yasm_errwarns_output_all(errwarns, linemap, warning_error, - print_yasm_error, print_yasm_warning); + print_yasm_error, print_yasm_warning); yasm_xfree(preproc_buf); yasm_linemap_destroy(linemap); yasm_errwarns_destroy(errwarns); @@ -333,75 +333,75 @@ do_assemble(FILE *in) /* determine the object filename if not specified */ if (!obj_filename) { - if (in == stdin) - /* Default to yasm.out if no obj filename specified */ - obj_filename = yasm__xstrdup("yasm.out"); - else { - /* replace (or add) extension to base filename */ - yasm__splitpath(in_filename, &base_filename); - if (base_filename[0] == '\0') - obj_filename = yasm__xstrdup("yasm.out"); - else - obj_filename = replace_extension(base_filename, - cur_objfmt_module->extension, - "yasm.out"); - } + if (in == stdin) + /* Default to yasm.out if no obj filename specified */ + obj_filename = yasm__xstrdup("yasm.out"); + else { + /* replace (or add) extension to base filename */ + yasm__splitpath(in_filename, &base_filename); + if (base_filename[0] == '\0') + obj_filename = yasm__xstrdup("yasm.out"); + else + obj_filename = replace_extension(base_filename, + cur_objfmt_module->extension, + "yasm.out"); + } } /* Set up architecture using machine and parser. */ if (!machine_name) { - /* If we're using x86 and the default objfmt bits is 64, default the - * machine to amd64. When we get more arches with multiple machines, - * we should do this in a more modular fashion. - */ - if (strcmp(cur_arch_module->keyword, "x86") == 0 && - cur_objfmt_module->default_x86_mode_bits == 64) - machine_name = yasm__xstrdup("amd64"); - else - machine_name = - yasm__xstrdup(cur_arch_module->default_machine_keyword); + /* If we're using x86 and the default objfmt bits is 64, default the + * machine to amd64. When we get more arches with multiple machines, + * we should do this in a more modular fashion. + */ + if (strcmp(cur_arch_module->keyword, "x86") == 0 && + cur_objfmt_module->default_x86_mode_bits == 64) + machine_name = yasm__xstrdup("amd64"); + else + machine_name = + yasm__xstrdup(cur_arch_module->default_machine_keyword); } cur_arch = cur_arch_module->create(machine_name, - cur_parser_module->keyword, - &arch_error); + cur_parser_module->keyword, + &arch_error); if (!cur_arch) { - switch (arch_error) { - case YASM_ARCH_CREATE_BAD_MACHINE: - print_error(_("%s: `%s' is not a valid %s for %s `%s'"), - _("FATAL"), machine_name, _("machine"), - _("architecture"), cur_arch_module->keyword); - break; - case YASM_ARCH_CREATE_BAD_PARSER: - print_error(_("%s: `%s' is not a valid %s for %s `%s'"), - _("FATAL"), cur_parser_module->keyword, - _("parser"), _("architecture"), - cur_arch_module->keyword); - break; - default: - print_error(_("%s: unknown architecture error"), _("FATAL")); - } - - return EXIT_FAILURE; + switch (arch_error) { + case YASM_ARCH_CREATE_BAD_MACHINE: + print_error(_("%s: `%s' is not a valid %s for %s `%s'"), + _("FATAL"), machine_name, _("machine"), + _("architecture"), cur_arch_module->keyword); + break; + case YASM_ARCH_CREATE_BAD_PARSER: + print_error(_("%s: `%s' is not a valid %s for %s `%s'"), + _("FATAL"), cur_parser_module->keyword, + _("parser"), _("architecture"), + cur_arch_module->keyword); + break; + default: + print_error(_("%s: unknown architecture error"), _("FATAL")); + } + + return EXIT_FAILURE; } /* Create object */ object = yasm_object_create(in_filename, obj_filename, cur_arch, - cur_objfmt_module, cur_dbgfmt_module); + cur_objfmt_module, cur_dbgfmt_module); if (!object) { - yasm_error_class eclass; - unsigned long xrefline; - /*@only@*/ /*@null@*/ char *estr, *xrefstr; - - yasm_error_fetch(&eclass, &estr, &xrefline, &xrefstr); - print_error("%s: %s", _("FATAL"), estr); - yasm_xfree(estr); - yasm_xfree(xrefstr); - - if (in != stdin) - fclose(in); - cleanup(object); - return EXIT_FAILURE; + yasm_error_class eclass; + unsigned long xrefline; + /*@only@*/ /*@null@*/ char *estr, *xrefstr; + + yasm_error_fetch(&eclass, &estr, &xrefline, &xrefstr); + print_error("%s: %s", _("FATAL"), estr); + yasm_xfree(estr); + yasm_xfree(xrefstr); + + if (in != stdin) + fclose(in); + cleanup(object); + return EXIT_FAILURE; } /* Get a fresh copy of objfmt_module as it may have changed. */ @@ -412,48 +412,48 @@ do_assemble(FILE *in) */ matched = 0; for (i=0; cur_parser_module->preproc_keywords[i]; i++) - if (yasm__strcasecmp(cur_parser_module->preproc_keywords[i], - cur_preproc_module->keyword) == 0) - matched = 1; + if (yasm__strcasecmp(cur_parser_module->preproc_keywords[i], + cur_preproc_module->keyword) == 0) + matched = 1; if (!matched) { - print_error(_("%s: `%s' is not a valid %s for %s `%s'"), _("FATAL"), - cur_preproc_module->keyword, _("preprocessor"), - _("parser"), cur_parser_module->keyword); - if (in != stdin) - fclose(in); - cleanup(object); - return EXIT_FAILURE; + print_error(_("%s: `%s' is not a valid %s for %s `%s'"), _("FATAL"), + cur_preproc_module->keyword, _("preprocessor"), + _("parser"), cur_parser_module->keyword); + if (in != stdin) + fclose(in); + cleanup(object); + return EXIT_FAILURE; } cur_preproc = cur_preproc_module->create(in, in_filename, linemap, - errwarns); + errwarns); apply_preproc_builtins(); apply_preproc_saved_options(); /* Get initial x86 BITS setting from object format */ if (strcmp(cur_arch_module->keyword, "x86") == 0) { - yasm_arch_set_var(cur_arch, "mode_bits", - cur_objfmt_module->default_x86_mode_bits); + yasm_arch_set_var(cur_arch, "mode_bits", + cur_objfmt_module->default_x86_mode_bits); } yasm_arch_set_var(cur_arch, "force_strict", force_strict); /* Parse! */ cur_parser_module->do_parse(object, cur_preproc, in, list_filename != NULL, - linemap, errwarns); + linemap, errwarns); /* Close input file */ if (in != stdin) - fclose(in); + fclose(in); check_errors(errwarns, object, linemap); /* Check for undefined symbols */ yasm_symtab_parser_finalize(object->symtab, - strcmp(cur_parser_module->keyword, "gas")==0 || - strcmp(cur_parser_module->keyword, "gnu")==0, - errwarns); + strcmp(cur_parser_module->keyword, "gas")==0 || + strcmp(cur_parser_module->keyword, "gnu")==0, + errwarns); check_errors(errwarns, object, linemap); /* Finalize parse */ @@ -470,44 +470,44 @@ do_assemble(FILE *in) /* open the object file for output (if not already opened by dbg objfmt) */ if (!obj && strcmp(cur_objfmt_module->keyword, "dbg") != 0) { - obj = open_file(obj_filename, "wb"); - if (!obj) { - cleanup(object); - return EXIT_FAILURE; - } + obj = open_file(obj_filename, "wb"); + if (!obj) { + cleanup(object); + return EXIT_FAILURE; + } } /* Write the object file */ yasm_objfmt_output(object, obj?obj:stderr, - strcmp(cur_dbgfmt_module->keyword, "null"), errwarns); + strcmp(cur_dbgfmt_module->keyword, "null"), errwarns); /* Close object file */ if (obj) - fclose(obj); + fclose(obj); /* If we had an error at this point, we also need to delete the output * object file (to make sure it's not left newer than the source). */ if (yasm_errwarns_num_errors(errwarns, warning_error) > 0) - remove(obj_filename); + remove(obj_filename); check_errors(errwarns, object, linemap); /* Open and write the list file */ if (list_filename) { - FILE *list = open_file(list_filename, "wt"); - if (!list) { - cleanup(object); - return EXIT_FAILURE; - } - /* Initialize the list format */ - cur_listfmt = yasm_listfmt_create(cur_listfmt_module, in_filename, - obj_filename); - yasm_listfmt_output(cur_listfmt, list, linemap, cur_arch); - fclose(list); + FILE *list = open_file(list_filename, "wt"); + if (!list) { + cleanup(object); + return EXIT_FAILURE; + } + /* Initialize the list format */ + cur_listfmt = yasm_listfmt_create(cur_listfmt_module, in_filename, + obj_filename); + yasm_listfmt_output(cur_listfmt, list, linemap, cur_arch); + fclose(list); } yasm_errwarns_output_all(errwarns, linemap, warning_error, - print_yasm_error, print_yasm_warning); + print_yasm_error, print_yasm_warning); yasm_linemap_destroy(linemap); yasm_errwarns_destroy(errwarns); @@ -543,37 +543,37 @@ main(int argc, char *argv[]) STAILQ_INIT(&preproc_options); if (parse_cmdline(argc, argv, options, NELEMS(options), print_error)) - return EXIT_FAILURE; + return EXIT_FAILURE; switch (special_options) { - case SPECIAL_SHOW_HELP: - /* Does gettext calls internally */ - help_msg(help_head, help_tail, options, NELEMS(options)); - return EXIT_SUCCESS; - case SPECIAL_SHOW_VERSION: - for (i=0; imachines; - printf(_("Available %s for %s `%s':\n"), _("machines"), - _("architecture"), cur_arch_module->keyword); - while (m->keyword && m->name) { - print_list_keyword_desc(m->name, m->keyword); - m++; - } - return EXIT_SUCCESS; + const yasm_arch_machine *m = cur_arch_module->machines; + printf(_("Available %s for %s `%s':\n"), _("machines"), + _("architecture"), cur_arch_module->keyword); + while (m->keyword && m->name) { + print_list_keyword_desc(m->name, m->keyword); + m++; + } + return EXIT_SUCCESS; } /* Default to NASM as the parser */ if (!cur_parser_module) { - cur_parser_module = yasm_load_parser("nasm"); - if (!cur_parser_module) { - print_error(_("%s: could not load default %s"), _("FATAL"), - _("parser")); - cleanup(NULL); - return EXIT_FAILURE; - } + cur_parser_module = yasm_load_parser("nasm"); + if (!cur_parser_module) { + print_error(_("%s: could not load default %s"), _("FATAL"), + _("parser")); + cleanup(NULL); + return EXIT_FAILURE; + } } /* If not already specified, default to the parser's default preproc. */ if (!cur_preproc_module) { - cur_preproc_module = - yasm_load_preproc(cur_parser_module->default_preproc_keyword); - if (!cur_preproc_module) { - print_error(_("%s: could not load default %s"), _("FATAL"), - _("preprocessor")); - cleanup(NULL); - return EXIT_FAILURE; - } + cur_preproc_module = + yasm_load_preproc(cur_parser_module->default_preproc_keyword); + if (!cur_preproc_module) { + print_error(_("%s: could not load default %s"), _("FATAL"), + _("preprocessor")); + cleanup(NULL); + return EXIT_FAILURE; + } } /* Determine input filename and open input file. */ if (!in_filename) { - print_error(_("No input files specified")); - return EXIT_FAILURE; + print_error(_("No input files specified")); + return EXIT_FAILURE; } else if (strcmp(in_filename, "-") != 0) { - /* Open the input file (if not standard input) */ - in = fopen(in_filename, "rt"); - if (!in) { - print_error(_("%s: could not open file `%s'"), _("FATAL"), - in_filename); - yasm_xfree(in_filename); - if (obj_filename) - yasm_xfree(obj_filename); - return EXIT_FAILURE; - } + /* Open the input file (if not standard input) */ + in = fopen(in_filename, "rt"); + if (!in) { + print_error(_("%s: could not open file `%s'"), _("FATAL"), + in_filename); + yasm_xfree(in_filename); + if (obj_filename) + yasm_xfree(obj_filename); + return EXIT_FAILURE; + } } else { - /* Filename was "-", read stdin */ - in = stdin; + /* Filename was "-", read stdin */ + in = stdin; } /* handle preproc-only case here */ if (preproc_only) - return do_preproc_only(in); + return do_preproc_only(in); /* If list file enabled, make sure we have a list format loaded. */ if (list_filename) { - /* If not already specified, default to nasm as the list format. */ - if (!cur_listfmt_module) { - cur_listfmt_module = yasm_load_listfmt("nasm"); - if (!cur_listfmt_module) { - print_error(_("%s: could not load default %s"), _("FATAL"), - _("list format")); - return EXIT_FAILURE; - } - } + /* If not already specified, default to nasm as the list format. */ + if (!cur_listfmt_module) { + cur_listfmt_module = yasm_load_listfmt("nasm"); + if (!cur_listfmt_module) { + print_error(_("%s: could not load default %s"), _("FATAL"), + _("list format")); + return EXIT_FAILURE; + } + } } /* If not already specified, default to null as the debug format. */ if (!cur_dbgfmt_module) { - cur_dbgfmt_module = yasm_load_dbgfmt("null"); - if (!cur_dbgfmt_module) { - print_error(_("%s: could not load default %s"), _("FATAL"), - _("debug format")); - return EXIT_FAILURE; - } + cur_dbgfmt_module = yasm_load_dbgfmt("null"); + if (!cur_dbgfmt_module) { + print_error(_("%s: could not load default %s"), _("FATAL"), + _("debug format")); + return EXIT_FAILURE; + } } return do_assemble(in); @@ -696,21 +696,21 @@ open_file(const char *filename, const char *mode) f = fopen(filename, mode); if (!f) - print_error(_("could not open file `%s'"), filename); + print_error(_("could not open file `%s'"), filename); return f; } static void check_errors(yasm_errwarns *errwarns, yasm_object *object, - yasm_linemap *linemap) + yasm_linemap *linemap) { if (yasm_errwarns_num_errors(errwarns, warning_error) > 0) { - yasm_errwarns_output_all(errwarns, linemap, warning_error, - print_yasm_error, print_yasm_warning); - yasm_linemap_destroy(linemap); - yasm_errwarns_destroy(errwarns); - cleanup(object); - exit(EXIT_FAILURE); + yasm_errwarns_output_all(errwarns, linemap, warning_error, + print_yasm_error, print_yasm_warning); + yasm_linemap_destroy(linemap); + yasm_errwarns_destroy(errwarns); + cleanup(object); + exit(EXIT_FAILURE); } } @@ -718,43 +718,43 @@ check_errors(yasm_errwarns *errwarns, yasm_object *object, * Useful for detecting memory leaks, but slows down execution unnecessarily * (as the OS will free everything we miss here). */ -#define DO_FREE 1 +#define DO_FREE 1 /* Cleans up all allocated structures. */ static void cleanup(yasm_object *object) { if (DO_FREE) { - if (cur_listfmt) - yasm_listfmt_destroy(cur_listfmt); - if (cur_preproc) - yasm_preproc_destroy(cur_preproc); - if (object) - yasm_object_destroy(object); + if (cur_listfmt) + yasm_listfmt_destroy(cur_listfmt); + if (cur_preproc) + yasm_preproc_destroy(cur_preproc); + if (object) + yasm_object_destroy(object); - yasm_floatnum_cleanup(); - yasm_intnum_cleanup(); + yasm_floatnum_cleanup(); + yasm_intnum_cleanup(); - yasm_errwarn_cleanup(); + yasm_errwarn_cleanup(); - BitVector_Shutdown(); + BitVector_Shutdown(); } if (DO_FREE) { - if (in_filename) - yasm_xfree(in_filename); - if (obj_filename) - yasm_xfree(obj_filename); - if (list_filename) - yasm_xfree(list_filename); - if (machine_name) - yasm_xfree(machine_name); - if (objfmt_keyword) - yasm_xfree(objfmt_keyword); + if (in_filename) + yasm_xfree(in_filename); + if (obj_filename) + yasm_xfree(obj_filename); + if (list_filename) + yasm_xfree(list_filename); + if (machine_name) + yasm_xfree(machine_name); + if (objfmt_keyword) + yasm_xfree(objfmt_keyword); } if (errfile != stderr && errfile != stdout) - fclose(errfile); + fclose(errfile); } /* @@ -764,9 +764,9 @@ int not_an_option_handler(char *param) { if (in_filename) { - print_error( - _("warning: can open only one input file, only the last file will be processed")); - yasm_xfree(in_filename); + print_error( + _("warning: can open only one input file, only the last file will be processed")); + yasm_xfree(in_filename); } in_filename = yasm__xstrdup(param); @@ -779,14 +779,14 @@ other_option_handler(char *option) { /* Accept, but ignore, -O and -Onnn, for compatibility with NASM. */ if (option[0] == '-' && option[1] == 'O') { - int n = 2; - for (;;) { - if (option[n] == '\0') - return 0; - if (!isdigit(option[n])) - return 1; - n++; - } + int n = 2; + for (;;) { + if (option[n] == '\0') + return 0; + if (!isdigit(option[n])) + return 1; + n++; + } } return 1; } @@ -795,7 +795,7 @@ static int opt_special_handler(/*@unused@*/ char *cmd, /*@unused@*/ char *param, int extra) { if (special_options == 0) - special_options = extra; + special_options = extra; return 0; } @@ -805,15 +805,15 @@ opt_arch_handler(/*@unused@*/ char *cmd, char *param, /*@unused@*/ int extra) assert(param != NULL); cur_arch_module = yasm_load_arch(param); if (!cur_arch_module) { - if (!strcmp("help", param)) { - printf(_("Available yasm %s:\n"), _("architectures")); - yasm_list_arch(print_list_keyword_desc); - special_options = SPECIAL_LISTED; - return 0; - } - print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), - _("architecture"), param); - exit(EXIT_FAILURE); + if (!strcmp("help", param)) { + printf(_("Available yasm %s:\n"), _("architectures")); + yasm_list_arch(print_list_keyword_desc); + special_options = SPECIAL_LISTED; + return 0; + } + print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), + _("architecture"), param); + exit(EXIT_FAILURE); } return 0; } @@ -824,15 +824,15 @@ opt_parser_handler(/*@unused@*/ char *cmd, char *param, /*@unused@*/ int extra) assert(param != NULL); cur_parser_module = yasm_load_parser(param); if (!cur_parser_module) { - if (!strcmp("help", param)) { - printf(_("Available yasm %s:\n"), _("parsers")); - yasm_list_parser(print_list_keyword_desc); - special_options = SPECIAL_LISTED; - return 0; - } - print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), _("parser"), - param); - exit(EXIT_FAILURE); + if (!strcmp("help", param)) { + printf(_("Available yasm %s:\n"), _("parsers")); + yasm_list_parser(print_list_keyword_desc); + special_options = SPECIAL_LISTED; + return 0; + } + print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), _("parser"), + param); + exit(EXIT_FAILURE); } return 0; } @@ -843,15 +843,15 @@ opt_preproc_handler(/*@unused@*/ char *cmd, char *param, /*@unused@*/ int extra) assert(param != NULL); cur_preproc_module = yasm_load_preproc(param); if (!cur_preproc_module) { - if (!strcmp("help", param)) { - printf(_("Available yasm %s:\n"), _("preprocessors")); - yasm_list_preproc(print_list_keyword_desc); - special_options = SPECIAL_LISTED; - return 0; - } - print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), - _("preprocessor"), param); - exit(EXIT_FAILURE); + if (!strcmp("help", param)) { + printf(_("Available yasm %s:\n"), _("preprocessors")); + yasm_list_preproc(print_list_keyword_desc); + special_options = SPECIAL_LISTED; + return 0; + } + print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), + _("preprocessor"), param); + exit(EXIT_FAILURE); } return 0; } @@ -863,21 +863,21 @@ opt_objfmt_handler(/*@unused@*/ char *cmd, char *param, /*@unused@*/ int extra) assert(param != NULL); cur_objfmt_module = yasm_load_objfmt(param); if (!cur_objfmt_module) { - if (!strcmp("help", param)) { - printf(_("Available yasm %s:\n"), _("object formats")); - yasm_list_objfmt(print_list_keyword_desc); - special_options = SPECIAL_LISTED; - return 0; - } - print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), - _("object format"), param); - exit(EXIT_FAILURE); + if (!strcmp("help", param)) { + printf(_("Available yasm %s:\n"), _("object formats")); + yasm_list_objfmt(print_list_keyword_desc); + special_options = SPECIAL_LISTED; + return 0; + } + print_error(_("%s: unrecognized %s `%s'"), _("FATAL"), + _("object format"), param); + exit(EXIT_FAILURE); } if (objfmt_keyword) - yasm_xfree(objfmt_keyword); + yasm_xfree(objfmt_keyword); objfmt_keyword = yasm__xstrdup(param); for (i=0; i #include -#define MAXLINE 1024 +#define MAXLINE 1024 int main(int argc, char *argv[]) @@ -41,56 +41,56 @@ main(int argc, char *argv[]) size_t len; if (argc < 4) { - fprintf(stderr, "Usage: %s [ ...]\n", - argv[0]); - return EXIT_FAILURE; + fprintf(stderr, "Usage: %s [ ...]\n", + argv[0]); + return EXIT_FAILURE; } out = fopen(argv[2], "wt"); if (!out) { - fprintf(stderr, "Could not open `%s'.\n", argv[2]); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s'.\n", argv[2]); + return EXIT_FAILURE; } str = malloc(MAXLINE); fprintf(out, "/* This file auto-generated from %s by genstring.c" - " - don't edit it */\n\n" - "static const char *%s[] = {\n", argv[3], argv[1]); + " - don't edit it */\n\n" + "static const char *%s[] = {\n", argv[3], argv[1]); for (i=3; i 0 && (strp[len-1] == ' ' || strp[len-1] == '\t' || - strp[len-1] == '\n')) { - strp[len-1] = '\0'; - len--; - } + /* strip off trailing whitespace */ + len = strlen(strp); + while (len > 0 && (strp[len-1] == ' ' || strp[len-1] == '\t' || + strp[len-1] == '\n')) { + strp[len-1] = '\0'; + len--; + } - /* output as string to output file */ - fprintf(out, " \""); - while (*strp != '\0') { - if (*strp == '\\' || *strp == '"') - fputc('\\', out); - fputc(*strp, out); - strp++; - } - fprintf(out, "\",\n"); - } + /* output as string to output file */ + fprintf(out, " \""); + while (*strp != '\0') { + if (*strp == '\\' || *strp == '"') + fputc('\\', out); + fputc(*strp, out); + strp++; + } + fprintf(out, "\",\n"); + } - fclose(in); + fclose(in); } fprintf(out, "};\n"); diff --git a/libyasm/arch.c b/libyasm/arch.c index dd849ebc..7f405c7a 100644 --- a/libyasm/arch.c +++ b/libyasm/arch.c @@ -92,16 +92,16 @@ yasm_operand_create_imm(/*@only@*/ yasm_expr *val) reg = yasm_expr_get_reg(&val, 0); if (reg) { - retval = yasm_operand_create_reg(*reg); - yasm_expr_destroy(val); + retval = yasm_operand_create_reg(*reg); + yasm_expr_destroy(val); } else { - retval = yasm_xmalloc(sizeof(yasm_insn_operand)); - retval->type = YASM_INSN__OPERAND_IMM; - retval->data.val = val; - retval->targetmod = 0; - retval->size = 0; - retval->deref = 0; - retval->strict = 0; + retval = yasm_xmalloc(sizeof(yasm_insn_operand)); + retval->type = YASM_INSN__OPERAND_IMM; + retval->data.val = val; + retval->targetmod = 0; + retval->size = 0; + retval->deref = 0; + retval->strict = 0; } return retval; @@ -109,34 +109,34 @@ yasm_operand_create_imm(/*@only@*/ yasm_expr *val) void yasm_operand_print(const yasm_insn_operand *op, FILE *f, int indent_level, - yasm_arch *arch) + yasm_arch *arch) { switch (op->type) { - case YASM_INSN__OPERAND_REG: - fprintf(f, "%*sReg=", indent_level, ""); - yasm_arch_reg_print(arch, op->data.reg, f); - fprintf(f, "\n"); - break; - case YASM_INSN__OPERAND_SEGREG: - fprintf(f, "%*sSegReg=", indent_level, ""); - yasm_arch_segreg_print(arch, op->data.reg, f); - fprintf(f, "\n"); - break; - case YASM_INSN__OPERAND_MEMORY: - fprintf(f, "%*sMemory=\n", indent_level, ""); - yasm_ea_print(op->data.ea, f, indent_level); - break; - case YASM_INSN__OPERAND_IMM: - fprintf(f, "%*sImm=", indent_level, ""); - yasm_expr_print(op->data.val, f); - fprintf(f, "\n"); - break; + case YASM_INSN__OPERAND_REG: + fprintf(f, "%*sReg=", indent_level, ""); + yasm_arch_reg_print(arch, op->data.reg, f); + fprintf(f, "\n"); + break; + case YASM_INSN__OPERAND_SEGREG: + fprintf(f, "%*sSegReg=", indent_level, ""); + yasm_arch_segreg_print(arch, op->data.reg, f); + fprintf(f, "\n"); + break; + case YASM_INSN__OPERAND_MEMORY: + fprintf(f, "%*sMemory=\n", indent_level, ""); + yasm_ea_print(op->data.ea, f, indent_level); + break; + case YASM_INSN__OPERAND_IMM: + fprintf(f, "%*sImm=", indent_level, ""); + yasm_expr_print(op->data.val, f); + fprintf(f, "\n"); + break; } fprintf(f, "%*sTargetMod=%lx\n", indent_level+1, "", - (unsigned long)op->targetmod); + (unsigned long)op->targetmod); fprintf(f, "%*sSize=%u\n", indent_level+1, "", op->size); fprintf(f, "%*sDeref=%d, Strict=%d\n", indent_level+1, "", (int)op->deref, - (int)op->strict); + (int)op->strict); } void @@ -146,43 +146,43 @@ yasm_ops_delete(yasm_insn_operands *headp, int content) cur = STAILQ_FIRST(headp); while (cur) { - next = STAILQ_NEXT(cur, link); - if (content) - switch (cur->type) { - case YASM_INSN__OPERAND_MEMORY: - yasm_ea_destroy(cur->data.ea); - break; - case YASM_INSN__OPERAND_IMM: - yasm_expr_destroy(cur->data.val); - break; - default: - break; - } - yasm_xfree(cur); - cur = next; + next = STAILQ_NEXT(cur, link); + if (content) + switch (cur->type) { + case YASM_INSN__OPERAND_MEMORY: + yasm_ea_destroy(cur->data.ea); + break; + case YASM_INSN__OPERAND_IMM: + yasm_expr_destroy(cur->data.val); + break; + default: + break; + } + yasm_xfree(cur); + cur = next; } STAILQ_INIT(headp); } /*@null@*/ yasm_insn_operand * yasm_ops_append(yasm_insn_operands *headp, - /*@returned@*/ /*@null@*/ yasm_insn_operand *op) + /*@returned@*/ /*@null@*/ yasm_insn_operand *op) { if (op) { - STAILQ_INSERT_TAIL(headp, op, link); - return op; + STAILQ_INSERT_TAIL(headp, op, link); + return op; } return (yasm_insn_operand *)NULL; } void yasm_ops_print(const yasm_insn_operands *headp, FILE *f, int indent_level, - yasm_arch *arch) + yasm_arch *arch) { yasm_insn_operand *cur; STAILQ_FOREACH (cur, headp, link) - yasm_operand_print(cur, f, indent_level, arch); + yasm_operand_print(cur, f, indent_level, arch); } yasm_insn_operands * diff --git a/libyasm/arch.h b/libyasm/arch.h index 6ee9cdd9..79758347 100644 --- a/libyasm/arch.h +++ b/libyasm/arch.h @@ -36,27 +36,27 @@ /** Errors that may be returned by yasm_arch_module::create(). */ typedef enum yasm_arch_create_error { - YASM_ARCH_CREATE_OK = 0, /**< No error. */ - YASM_ARCH_CREATE_BAD_MACHINE, /**< Unrecognized machine name. */ - YASM_ARCH_CREATE_BAD_PARSER /**< Unrecognized parser name. */ + YASM_ARCH_CREATE_OK = 0, /**< No error. */ + YASM_ARCH_CREATE_BAD_MACHINE, /**< Unrecognized machine name. */ + YASM_ARCH_CREATE_BAD_PARSER /**< Unrecognized parser name. */ } yasm_arch_create_error; /** Return values for yasm_arch_module::parse_check_insnprefix(). */ typedef enum yasm_arch_insnprefix { - YASM_ARCH_NOTINSNPREFIX = 0, /**< Unrecognized */ - YASM_ARCH_INSN, /**< An instruction */ - YASM_ARCH_PREFIX /**< An instruction prefix */ + YASM_ARCH_NOTINSNPREFIX = 0, /**< Unrecognized */ + YASM_ARCH_INSN, /**< An instruction */ + YASM_ARCH_PREFIX /**< An instruction prefix */ } yasm_arch_insnprefix; /** Types of registers / target modifiers that may be returned by * yasm_arch_module::parse_check_regtmod(). */ typedef enum yasm_arch_regtmod { - YASM_ARCH_NOTREGTMOD = 0, /**< Unrecognized */ - YASM_ARCH_REG, /**< A "normal" register */ - YASM_ARCH_REGGROUP, /**< A group of indexable registers */ - YASM_ARCH_SEGREG, /**< A segment register */ - YASM_ARCH_TARGETMOD /**< A target modifier (for jumps) */ + YASM_ARCH_NOTREGTMOD = 0, /**< Unrecognized */ + YASM_ARCH_REG, /**< A "normal" register */ + YASM_ARCH_REGGROUP, /**< A group of indexable registers */ + YASM_ARCH_SEGREG, /**< A segment register */ + YASM_ARCH_TARGETMOD /**< A target modifier (for jumps) */ } yasm_arch_regtmod; /** An instruction operand (opaque type). */ @@ -91,9 +91,9 @@ typedef struct yasm_arch_machine { /** YASM architecture module interface. * \note All "data" in parser-related functions (yasm_arch_parse_*) needs to - * start the parse initialized to 0 to make it okay for a parser-related - * function to use/check previously stored data to see if it's been - * called before on the same piece of data. + * start the parse initialized to 0 to make it okay for a parser-related + * function to use/check previously stored data to see if it's been + * called before on the same piece of data. */ typedef struct yasm_arch_module { /** One-line description of the architecture. @@ -114,7 +114,7 @@ typedef struct yasm_arch_module { * Call yasm_arch_create() instead of calling this function. */ /*@only@*/ yasm_arch * (*create) (const char *machine, const char *parser, - yasm_arch_create_error *error); + yasm_arch_create_error *error); /** Module-level implementation of yasm_arch_destroy(). * Call yasm_arch_destroy() instead of calling this function. @@ -140,15 +140,15 @@ typedef struct yasm_arch_module { * Call yasm_arch_parse_check_insnprefix() instead of calling this function. */ yasm_arch_insnprefix (*parse_check_insnprefix) - (yasm_arch *arch, /*@out@*/ uintptr_t data[4], const char *id, - size_t id_len); + (yasm_arch *arch, /*@out@*/ uintptr_t data[4], const char *id, + size_t id_len); /** Module-level implementation of yasm_arch_parse_check_regtmod(). * Call yasm_arch_parse_check_regtmod() instead of calling this function. */ yasm_arch_regtmod (*parse_check_regtmod) - (yasm_arch *arch, /*@out@*/ uintptr_t *data, const char *id, - size_t id_len); + (yasm_arch *arch, /*@out@*/ uintptr_t *data, const char *id, + size_t id_len); /** Module-level implementation of yasm_arch_get_fill(). * Call yasm_arch_get_fill() instead of calling this function. @@ -159,25 +159,25 @@ typedef struct yasm_arch_module { * Call yasm_arch_finalize_insn() instead of calling this function. */ void (*finalize_insn) - (yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, - const uintptr_t data[4], int num_operands, - /*@null@*/ yasm_insn_operands *operands, int num_prefixes, - uintptr_t **prefixes, int num_segregs, const uintptr_t *segregs); + (yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, + const uintptr_t data[4], int num_operands, + /*@null@*/ yasm_insn_operands *operands, int num_prefixes, + uintptr_t **prefixes, int num_segregs, const uintptr_t *segregs); /** Module-level implementation of yasm_arch_floatnum_tobytes(). * Call yasm_arch_floatnum_tobytes() instead of calling this function. */ int (*floatnum_tobytes) (yasm_arch *arch, const yasm_floatnum *flt, - unsigned char *buf, size_t destsize, - size_t valsize, size_t shift, int warn); + unsigned char *buf, size_t destsize, + size_t valsize, size_t shift, int warn); /** Module-level implementation of yasm_arch_intnum_tobytes(). * Call yasm_arch_intnum_tobytes() instead of calling this function. */ int (*intnum_tobytes) (yasm_arch *arch, const yasm_intnum *intn, - unsigned char *buf, size_t destsize, size_t valsize, - int shift, const yasm_bytecode *bc, - int warn); + unsigned char *buf, size_t destsize, size_t valsize, + int shift, const yasm_bytecode *bc, + int warn); /** Module-level implementation of yasm_arch_get_reg_size(). * Call yasm_arch_get_reg_size() instead of calling this function. @@ -188,7 +188,7 @@ typedef struct yasm_arch_module { * Call yasm_arch_reggroup_get_reg() instead of calling this function. */ uintptr_t (*reggroup_get_reg) (yasm_arch *arch, uintptr_t reggroup, - unsigned long regindex); + unsigned long regindex); /** Module-level implementation of yasm_arch_reg_print(). * Call yasm_arch_reg_print() instead of calling this function. @@ -238,21 +238,21 @@ struct yasm_insn_operand { /** Operand type. */ enum yasm_insn_operand_type { - YASM_INSN__OPERAND_REG = 1, /**< A register. */ - YASM_INSN__OPERAND_SEGREG, /**< A segment register. */ - YASM_INSN__OPERAND_MEMORY, /**< An effective address - * (memory reference). */ - YASM_INSN__OPERAND_IMM /**< An immediate or jump target. */ + YASM_INSN__OPERAND_REG = 1, /**< A register. */ + YASM_INSN__OPERAND_SEGREG, /**< A segment register. */ + YASM_INSN__OPERAND_MEMORY, /**< An effective address + * (memory reference). */ + YASM_INSN__OPERAND_IMM /**< An immediate or jump target. */ } type; /** Operand data. */ union { - uintptr_t reg; /**< Arch data for reg/segreg. */ - yasm_effaddr *ea; /**< Effective address for memory references. */ - yasm_expr *val; /**< Value of immediate or jump target. */ + uintptr_t reg; /**< Arch data for reg/segreg. */ + yasm_effaddr *ea; /**< Effective address for memory references. */ + yasm_expr *val; /**< Value of immediate or jump target. */ } data; - uintptr_t targetmod; /**< Arch target modifier, 0 if none. */ + uintptr_t targetmod; /**< Arch target modifier, 0 if none. */ /** Specified size of the operand, in bits. 0 if not user-specified. */ unsigned int size:8; @@ -281,63 +281,63 @@ struct yasm_insn_operand { #endif /** Get the one-line description of an architecture. - * \param arch architecture + * \param arch architecture * \return One-line description of architecture. */ const char *yasm_arch_name(const yasm_arch *arch); /** Get the keyword used to select an architecture. - * \param arch architecture + * \param arch architecture * \return Architecture keyword. */ const char *yasm_arch_keyword(const yasm_arch *arch); /** Get the word size of an architecture. - * \param arch architecture + * \param arch architecture * \return Word size (in bits). */ unsigned int yasm_arch_wordsize(const yasm_arch *arch); /** Get the minimum instruction length of an architecture. - * \param arch architecture + * \param arch architecture * \return Minimum instruction length (in bytes). */ unsigned int yasm_arch_min_insn_len(const yasm_arch *arch); /** Create architecture. - * \param module architecture module - * \param machine keyword of machine in use (must be one listed in - * #yasm_arch_module.machines) - * \param parser keyword of parser in use - * \param error error return value + * \param module architecture module + * \param machine keyword of machine in use (must be one listed in + * #yasm_arch_module.machines) + * \param parser keyword of parser in use + * \param error error return value * \return NULL on error (error returned in error parameter), otherwise new - * architecture. + * architecture. */ /*@only@*/ yasm_arch *yasm_arch_create(const yasm_arch_module *module, - const char *machine, const char *parser, - /*@out@*/ yasm_arch_create_error *error); + const char *machine, const char *parser, + /*@out@*/ yasm_arch_create_error *error); /** Clean up, free any architecture-allocated memory. - * \param arch architecture + * \param arch architecture */ void yasm_arch_destroy(/*@only@*/ yasm_arch *arch); /** Get architecture's active machine name. - * \param arch architecture + * \param arch architecture * \return Active machine name. */ const char *yasm_arch_get_machine(const yasm_arch *arch); /** Get architecture's active address size, in bits. - * \param arch architecture + * \param arch architecture * \return Active address size (in bits). */ unsigned int yasm_arch_get_address_size(const yasm_arch *arch); /** Set any arch-specific variables. For example, "mode_bits" in x86. - * \param arch architecture - * \param var variable name - * \param val value to set + * \param arch architecture + * \param var variable name + * \param val value to set * \return Zero on success, non-zero on failure (variable does not exist). */ int yasm_arch_set_var(yasm_arch *arch, const char *var, unsigned long val); @@ -347,11 +347,11 @@ int yasm_arch_set_var(yasm_arch *arch, const char *var, unsigned long val); * should return #YASM_ARCH_NOTINSNPREFIX so they can be treated as normal * symbols. Any additional data beyond just the type (almost always necessary) * should be returned into the space provided by the data parameter. - * \param arch architecture - * \param data extra identification information (yasm_arch-specific) - * [output] - * \param id identifier as in the input file - * \param id_len length of id string + * \param arch architecture + * \param data extra identification information (yasm_arch-specific) + * [output] + * \param id identifier as in the input file + * \param id_len length of id string * \return Identifier type (#YASM_ARCH_NOTINSNPREFIX if unrecognized) */ yasm_arch_insnprefix yasm_arch_parse_check_insnprefix @@ -363,11 +363,11 @@ yasm_arch_insnprefix yasm_arch_parse_check_insnprefix * return #YASM_ARCH_NOTREGTMOD. Any additional data beyond just the type * (almost always necessary) should be returned into the space provided by the * data parameter. - * \param arch architecture - * \param data extra identification information (yasm_arch-specific) - * [output] - * \param id identifier as in the input file - * \param id_len length of id string + * \param arch architecture + * \param data extra identification information (yasm_arch-specific) + * [output] + * \param id identifier as in the input file + * \param id_len length of id string * \return Identifier type (#YASM_ARCH_NOTREGTMOD if unrecognized) */ yasm_arch_regtmod yasm_arch_parse_check_regtmod @@ -375,7 +375,7 @@ yasm_arch_regtmod yasm_arch_parse_check_regtmod size_t id_len); /** Get NOP fill patterns for 1-15 bytes of fill. - * \param arch architecture + * \param arch architecture * \return 16-entry array of arrays; [0] is unused, [1] - [15] point to arrays * of 1-15 bytes (respectively) in length. */ @@ -383,17 +383,17 @@ const unsigned char **yasm_arch_get_fill(const yasm_arch *arch); /** Finalize an instruction from a semi-generic insn description. Note an * existing bytecode is required. - * \param arch architecture - * \param bc bytecode to finalize - * \param prev_bc previous bytecode in section - * \param data instruction data (from parse_check_id()); all - * zero indicates an empty instruction - * \param num_operands number of operands - * \param operands list of operands (in parse order) - * \param num_prefixes number of prefixes - * \param prefixes array of 4-element prefix data - * \param num_segregs number of segment register prefixes - * \param segregs array of segment register data + * \param arch architecture + * \param bc bytecode to finalize + * \param prev_bc previous bytecode in section + * \param data instruction data (from parse_check_id()); all + * zero indicates an empty instruction + * \param num_operands number of operands + * \param operands list of operands (in parse order) + * \param num_prefixes number of prefixes + * \param prefixes array of 4-element prefix data + * \param num_segregs number of segment register prefixes + * \param segregs array of segment register data * \return If no match is found (the instruction is invalid), no action is * performed and an error is recorded. */ @@ -408,75 +408,75 @@ void yasm_arch_finalize_insn * significant bits by the shift parameter. The destination bits are * cleared before being set. * Architecture-specific because of endianness. - * \param arch architecture - * \param flt floating point value - * \param buf buffer to write into - * \param destsize destination size (in bytes) - * \param valsize size (in bits) - * \param shift left shift (in bits) - * \param warn enables standard overflow/underflow warnings + * \param arch architecture + * \param flt floating point value + * \param buf buffer to write into + * \param destsize destination size (in bytes) + * \param valsize size (in bits) + * \param shift left shift (in bits) + * \param warn enables standard overflow/underflow warnings * \return Nonzero on error. */ int yasm_arch_floatnum_tobytes(yasm_arch *arch, const yasm_floatnum *flt, - unsigned char *buf, size_t destsize, - size_t valsize, size_t shift, int warn); + unsigned char *buf, size_t destsize, + size_t valsize, size_t shift, int warn); /** Output #yasm_intnum to buffer. Puts the value into the least * significant bits of the destination, or may be shifted into more * significant bits by the shift parameter. The destination bits are * cleared before being set. - * \param arch architecture - * \param intn integer value - * \param buf buffer to write into - * \param destsize destination size (in bytes) - * \param valsize size (in bits) - * \param shift left shift (in bits); may be negative to specify right - * shift (standard warnings include truncation to boundary) - * \param bc bytecode being output ("parent" of value) - * \param warn enables standard warnings (value doesn't fit into - * valsize bits) + * \param arch architecture + * \param intn integer value + * \param buf buffer to write into + * \param destsize destination size (in bytes) + * \param valsize size (in bits) + * \param shift left shift (in bits); may be negative to specify right + * shift (standard warnings include truncation to boundary) + * \param bc bytecode being output ("parent" of value) + * \param warn enables standard warnings (value doesn't fit into + * valsize bits) * \return Nonzero on error. */ int yasm_arch_intnum_tobytes(yasm_arch *arch, const yasm_intnum *intn, - unsigned char *buf, size_t destsize, - size_t valsize, int shift, - const yasm_bytecode *bc, int warn); + unsigned char *buf, size_t destsize, + size_t valsize, int shift, + const yasm_bytecode *bc, int warn); /** Get the equivalent size of a register in bits. - * \param arch architecture - * \param reg register + * \param arch architecture + * \param reg register * \return 0 if there is no suitable equivalent size, otherwise the size. */ unsigned int yasm_arch_get_reg_size(yasm_arch *arch, uintptr_t reg); /** Get a specific register of a register group, based on the register * group and the index within the group. - * \param arch architecture - * \param reggroup register group - * \param regindex register index + * \param arch architecture + * \param reggroup register group + * \param regindex register index * \return 0 if regindex is not valid for that register group, otherwise the * specific register value. */ uintptr_t yasm_arch_reggroup_get_reg(yasm_arch *arch, uintptr_t reggroup, - unsigned long regindex); + unsigned long regindex); /** Print a register. For debugging purposes. - * \param arch architecture - * \param reg register - * \param f file + * \param arch architecture + * \param reg register + * \param f file */ void yasm_arch_reg_print(yasm_arch *arch, uintptr_t reg, FILE *f); /** Print a segment register. For debugging purposes. - * \param arch architecture - * \param segreg segment register - * \param f file + * \param arch architecture + * \param segreg segment register + * \param f file */ void yasm_arch_segreg_print(yasm_arch *arch, uintptr_t segreg, FILE *f); /** Create an effective address from an expression. - * \param arch architecture - * \param e expression (kept, do not delete) + * \param arch architecture + * \param e expression (kept, do not delete) * \return Newly allocated effective address. */ yasm_effaddr *yasm_arch_ea_create(yasm_arch *arch, /*@keep@*/ yasm_expr *e); @@ -508,26 +508,26 @@ yasm_effaddr *yasm_arch_ea_create(yasm_arch *arch, /*@keep@*/ yasm_expr *e); ((yasm_arch_base *)arch)->module->set_var(arch, var, val) #define yasm_arch_parse_check_insnprefix(arch, data, id, id_len) \ ((yasm_arch_base *)arch)->module->parse_check_insnprefix(arch, data, id, \ - id_len) + id_len) #define yasm_arch_parse_check_regtmod(arch, data, id, id_len) \ ((yasm_arch_base *)arch)->module->parse_check_regtmod(arch, data, id, \ - id_len) + id_len) #define yasm_arch_get_fill(arch) \ ((yasm_arch_base *)arch)->module->get_fill(arch) #define yasm_arch_finalize_insn(arch, bc, prev_bc, data, num_operands, \ - operands, num_prefixes, prefixes, \ - num_segregs, segregs) \ + operands, num_prefixes, prefixes, \ + num_segregs, segregs) \ ((yasm_arch_base *)arch)->module->finalize_insn \ - (arch, bc, prev_bc, data, num_operands, operands, num_prefixes, \ - prefixes, num_segregs, segregs) + (arch, bc, prev_bc, data, num_operands, operands, num_prefixes, \ + prefixes, num_segregs, segregs) #define yasm_arch_floatnum_tobytes(arch, flt, buf, destsize, valsize, shift, \ - warn) \ + warn) \ ((yasm_arch_base *)arch)->module->floatnum_tobytes \ - (arch, flt, buf, destsize, valsize, shift, warn) + (arch, flt, buf, destsize, valsize, shift, warn) #define yasm_arch_intnum_tobytes(arch, intn, buf, destsize, valsize, shift, \ - bc, warn) \ + bc, warn) \ ((yasm_arch_base *)arch)->module->intnum_tobytes \ - (arch, intn, buf, destsize, valsize, shift, bc, warn) + (arch, intn, buf, destsize, valsize, shift, bc, warn) #define yasm_arch_get_reg_size(arch, reg) \ ((yasm_arch_base *)arch)->module->get_reg_size(arch, reg) #define yasm_arch_reggroup_get_reg(arch, regg, regi) \ @@ -542,19 +542,19 @@ yasm_effaddr *yasm_arch_ea_create(yasm_arch *arch, /*@keep@*/ yasm_expr *e); #endif /** Create an instruction operand from a register. - * \param reg register + * \param reg register * \return Newly allocated operand. */ yasm_insn_operand *yasm_operand_create_reg(uintptr_t reg); /** Create an instruction operand from a segment register. - * \param segreg segment register + * \param segreg segment register * \return Newly allocated operand. */ yasm_insn_operand *yasm_operand_create_segreg(uintptr_t segreg); /** Create an instruction operand from an effective address. - * \param ea effective address + * \param ea effective address * \return Newly allocated operand. */ yasm_insn_operand *yasm_operand_create_mem(/*@only@*/ yasm_effaddr *ea); @@ -562,19 +562,19 @@ yasm_insn_operand *yasm_operand_create_mem(/*@only@*/ yasm_effaddr *ea); /** Create an instruction operand from an immediate expression. * Looks for cases of a single register and creates a register variant of * #yasm_insn_operand. - * \param val immediate expression + * \param val immediate expression * \return Newly allocated operand. */ yasm_insn_operand *yasm_operand_create_imm(/*@only@*/ yasm_expr *val); /** Print an instruction operand. For debugging purposes. - * \param arch architecture - * \param f file - * \param indent_level indentation level - * \param op instruction operand + * \param arch architecture + * \param f file + * \param indent_level indentation level + * \param op instruction operand */ void yasm_operand_print(const yasm_insn_operand *op, FILE *f, int indent_level, - yasm_arch *arch); + yasm_arch *arch); /** Create a new list of instruction operands. * \return Newly allocated list. @@ -582,56 +582,56 @@ void yasm_operand_print(const yasm_insn_operand *op, FILE *f, int indent_level, yasm_insn_operands *yasm_ops_create(void); /** Destroy a list of instruction operands (created with yasm_ops_create()). - * \param headp list of instruction operands - * \param content if nonzero, deletes content of each operand + * \param headp list of instruction operands + * \param content if nonzero, deletes content of each operand */ void yasm_ops_destroy(yasm_insn_operands *headp, int content); /** Get the first operand in a list of instruction operands. - * \param headp list of instruction operands + * \param headp list of instruction operands * \return First operand in list (NULL if list is empty). */ yasm_insn_operand *yasm_ops_first(yasm_insn_operands *headp); /** Get the next operand in a list of instruction operands. - * \param cur previous operand + * \param cur previous operand * \return Next operand in list (NULL if cur was the last operand). */ yasm_insn_operand *yasm_operand_next(yasm_insn_operand *cur); #ifdef YASM_LIB_INTERNAL -#define yasm_ops_initialize(headp) STAILQ_INIT(headp) -#define yasm_ops_first(headp) STAILQ_FIRST(headp) -#define yasm_operand_next(cur) STAILQ_NEXT(cur, link) +#define yasm_ops_initialize(headp) STAILQ_INIT(headp) +#define yasm_ops_first(headp) STAILQ_FIRST(headp) +#define yasm_operand_next(cur) STAILQ_NEXT(cur, link) /** Delete (free allocated memory for) a list of instruction operands (created * with yasm_ops_initialize()). - * \param headp list of instruction operands - * \param content if nonzero, deletes content of each operand + * \param headp list of instruction operands + * \param content if nonzero, deletes content of each operand */ void yasm_ops_delete(yasm_insn_operands *headp, int content); #endif /** Add data value to the end of a list of instruction operands. * \note Does not make a copy of the operand; so don't pass this function - * static or local variables, and discard the op pointer after calling - * this function. - * \param headp list of instruction operands - * \param op operand (may be NULL) + * static or local variables, and discard the op pointer after calling + * this function. + * \param headp list of instruction operands + * \param op operand (may be NULL) * \return If operand was actually appended (it wasn't NULL), the operand; - * otherwise NULL. + * otherwise NULL. */ /*@null@*/ yasm_insn_operand *yasm_ops_append (yasm_insn_operands *headp, /*@returned@*/ /*@null@*/ yasm_insn_operand *op); /** Print a list of instruction operands. For debugging purposes. - * \param arch architecture - * \param f file - * \param indent_level indentation level - * \param headp list of instruction operands + * \param arch architecture + * \param f file + * \param indent_level indentation level + * \param headp list of instruction operands */ void yasm_ops_print(const yasm_insn_operands *headp, FILE *f, int indent_level, - yasm_arch *arch); + yasm_arch *arch); #endif diff --git a/libyasm/assocdat.c b/libyasm/assocdat.c index 83d12b88..7b5408bc 100644 --- a/libyasm/assocdat.c +++ b/libyasm/assocdat.c @@ -52,30 +52,30 @@ yasm__assoc_data_create(void) assoc_data->size = 0; assoc_data->alloc = 2; assoc_data->vector = yasm_xmalloc(assoc_data->alloc * - sizeof(assoc_data_item)); + sizeof(assoc_data_item)); return assoc_data; } void * yasm__assoc_data_get(yasm__assoc_data *assoc_data, - const yasm_assoc_data_callback *callback) + const yasm_assoc_data_callback *callback) { size_t i; if (!assoc_data) - return NULL; + return NULL; for (i=0; isize; i++) { - if (assoc_data->vector[i].callback == callback) - return assoc_data->vector[i].data; + if (assoc_data->vector[i].callback == callback) + return assoc_data->vector[i].data; } return NULL; } yasm__assoc_data * yasm__assoc_data_add(yasm__assoc_data *assoc_data_arg, - const yasm_assoc_data_callback *callback, void *data) + const yasm_assoc_data_callback *callback, void *data) { yasm__assoc_data *assoc_data; assoc_data_item *item = NULL; @@ -83,33 +83,33 @@ yasm__assoc_data_add(yasm__assoc_data *assoc_data_arg, /* Create a new assoc_data if necessary */ if (assoc_data_arg) - assoc_data = assoc_data_arg; + assoc_data = assoc_data_arg; else - assoc_data = yasm__assoc_data_create(); + assoc_data = yasm__assoc_data_create(); /* See if there's already assocated data for this callback */ for (i=0; isize; i++) { - if (assoc_data->vector[i].callback == callback) - item = &assoc_data->vector[i]; + if (assoc_data->vector[i].callback == callback) + item = &assoc_data->vector[i]; } /* No? Then append a new one */ if (!item) { - assoc_data->size++; - if (assoc_data->size > assoc_data->alloc) { - assoc_data->alloc *= 2; - assoc_data->vector = - yasm_xrealloc(assoc_data->vector, - assoc_data->alloc * sizeof(assoc_data_item)); - } - item = &assoc_data->vector[assoc_data->size-1]; - item->callback = callback; - item->data = NULL; + assoc_data->size++; + if (assoc_data->size > assoc_data->alloc) { + assoc_data->alloc *= 2; + assoc_data->vector = + yasm_xrealloc(assoc_data->vector, + assoc_data->alloc * sizeof(assoc_data_item)); + } + item = &assoc_data->vector[assoc_data->size-1]; + item->callback = callback; + item->data = NULL; } /* Delete existing data (if any) */ if (item->data && item->data != data) - item->callback->destroy(item->data); + item->callback->destroy(item->data); item->data = data; @@ -122,17 +122,17 @@ yasm__assoc_data_destroy(yasm__assoc_data *assoc_data) size_t i; if (!assoc_data) - return; + return; for (i=0; isize; i++) - assoc_data->vector[i].callback->destroy(assoc_data->vector[i].data); + assoc_data->vector[i].callback->destroy(assoc_data->vector[i].data); yasm_xfree(assoc_data->vector); yasm_xfree(assoc_data); } void yasm__assoc_data_print(const yasm__assoc_data *assoc_data, FILE *f, - int indent_level) + int indent_level) { /*TODO*/ } diff --git a/libyasm/assocdat.h b/libyasm/assocdat.h index 5cce0e73..09415260 100644 --- a/libyasm/assocdat.h +++ b/libyasm/assocdat.h @@ -41,8 +41,8 @@ typedef struct yasm__assoc_data yasm__assoc_data; /*@only@*/ yasm__assoc_data *yasm__assoc_data_create(void); /** Get associated data for a data callback. - * \param assoc_data container of associated data - * \param callback callback used when adding data + * \param assoc_data container of associated data + * \param callback callback used when adding data * \return Associated data (NULL if none). */ /*@dependent@*/ /*@null@*/ void *yasm__assoc_data_get @@ -51,9 +51,9 @@ typedef struct yasm__assoc_data yasm__assoc_data; /** Add associated data to a associated data container. * \attention Deletes any existing associated data for that data callback. - * \param assoc_data container of associated data - * \param callback callback - * \param data data to associate + * \param assoc_data container of associated data + * \param callback callback + * \param data data to associate */ /*@only@*/ yasm__assoc_data *yasm__assoc_data_add (/*@null@*/ /*@only@*/ yasm__assoc_data *assoc_data, @@ -66,6 +66,6 @@ void yasm__assoc_data_destroy /** Print all associated data in a container. */ void yasm__assoc_data_print(const yasm__assoc_data *assoc_data, FILE *f, - int indent_level); + int indent_level); #endif diff --git a/libyasm/bc-align.c b/libyasm/bc-align.c index 0a3a3a78..c71fdc99 100644 --- a/libyasm/bc-align.c +++ b/libyasm/bc-align.c @@ -41,7 +41,7 @@ typedef struct bytecode_align { - /*@only@*/ yasm_expr *boundary; /* alignment boundary */ + /*@only@*/ yasm_expr *boundary; /* alignment boundary */ /* What to fill intervening locations with, NULL if using code_fill */ /*@only@*/ /*@null@*/ yasm_expr *fill; @@ -57,13 +57,13 @@ static void bc_align_destroy(void *contents); static void bc_align_print(const void *contents, FILE *f, int indent_level); static void bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); static int bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data); + void *add_span_data); static int bc_align_expand(yasm_bytecode *bc, int span, long old_val, - long new_val, /*@out@*/ long *neg_thres, - /*@out@*/ long *pos_thres); + long new_val, /*@out@*/ long *neg_thres, + /*@out@*/ long *pos_thres); static int bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); static const yasm_bytecode_callback bc_align_callback = { bc_align_destroy, @@ -81,11 +81,11 @@ bc_align_destroy(void *contents) { bytecode_align *align = (bytecode_align *)contents; if (align->boundary) - yasm_expr_destroy(align->boundary); + yasm_expr_destroy(align->boundary); if (align->fill) - yasm_expr_destroy(align->fill); + yasm_expr_destroy(align->fill); if (align->maxskip) - yasm_expr_destroy(align->maxskip); + yasm_expr_destroy(align->maxskip); yasm_xfree(contents); } @@ -108,132 +108,132 @@ bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) { bytecode_align *align = (bytecode_align *)bc->contents; if (!yasm_expr_get_intnum(&align->boundary, 0)) - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("align boundary must be a constant")); + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("align boundary must be a constant")); if (align->fill && !yasm_expr_get_intnum(&align->fill, 0)) - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("align fill must be a constant")); + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("align fill must be a constant")); if (align->maxskip && !yasm_expr_get_intnum(&align->maxskip, 0)) - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("align maximum skip must be a constant")); + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("align maximum skip must be a constant")); } static int bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { long neg_thres = 0; long pos_thres = 0; if (bc_align_expand(bc, 0, 0, (long)bc->offset, &neg_thres, - &pos_thres) < 0) - return -1; + &pos_thres) < 0) + return -1; return 0; } static int bc_align_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { bytecode_align *align = (bytecode_align *)bc->contents; unsigned long end; unsigned long boundary = - yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0)); + yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0)); if (boundary == 0) { - bc->len = 0; - *pos_thres = new_val; - return 0; + bc->len = 0; + *pos_thres = new_val; + return 0; } end = (unsigned long)new_val; if ((unsigned long)new_val & (boundary-1)) - end = ((unsigned long)new_val & ~(boundary-1)) + boundary; + end = ((unsigned long)new_val & ~(boundary-1)) + boundary; *pos_thres = (long)end; bc->len = end - (unsigned long)new_val; if (align->maxskip) { - unsigned long maxskip = - yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0)); - if (bc->len > maxskip) { - *pos_thres = (long)end-maxskip-1; - bc->len = 0; - } + unsigned long maxskip = + yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0)); + if (bc->len > maxskip) { + *pos_thres = (long)end-maxskip-1; + bc->len = 0; + } } return 1; } static int bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { bytecode_align *align = (bytecode_align *)bc->contents; unsigned long len; unsigned long boundary = - yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0)); + yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0)); if (boundary == 0) - return 0; + return 0; else { - unsigned long end = bc->offset; - if (bc->offset & (boundary-1)) - end = (bc->offset & ~(boundary-1)) + boundary; - len = end - bc->offset; - if (len == 0) - return 0; - if (align->maxskip) { - unsigned long maxskip = - yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0)); - if (len > maxskip) - return 0; - } + unsigned long end = bc->offset; + if (bc->offset & (boundary-1)) + end = (bc->offset & ~(boundary-1)) + boundary; + len = end - bc->offset; + if (len == 0) + return 0; + if (align->maxskip) { + unsigned long maxskip = + yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0)); + if (len > maxskip) + return 0; + } } if (align->fill) { - unsigned long v; - v = yasm_intnum_get_uint(yasm_expr_get_intnum(&align->fill, 0)); - memset(*bufp, (int)v, len); - *bufp += len; + unsigned long v; + v = yasm_intnum_get_uint(yasm_expr_get_intnum(&align->fill, 0)); + memset(*bufp, (int)v, len); + *bufp += len; } else if (align->code_fill) { - unsigned long maxlen = 15; - while (!align->code_fill[maxlen] && maxlen>0) - maxlen--; - if (maxlen == 0) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("could not find any code alignment size")); - return 1; - } - - /* Fill with maximum code fill as much as possible */ - while (len > maxlen) { - memcpy(*bufp, align->code_fill[maxlen], maxlen); - *bufp += maxlen; - len -= maxlen; - } - - if (!align->code_fill[len]) { - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid alignment size %d"), len); - return 1; - } - /* Handle rest of code fill */ - memcpy(*bufp, align->code_fill[len], len); - *bufp += len; + unsigned long maxlen = 15; + while (!align->code_fill[maxlen] && maxlen>0) + maxlen--; + if (maxlen == 0) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("could not find any code alignment size")); + return 1; + } + + /* Fill with maximum code fill as much as possible */ + while (len > maxlen) { + memcpy(*bufp, align->code_fill[maxlen], maxlen); + *bufp += maxlen; + len -= maxlen; + } + + if (!align->code_fill[len]) { + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid alignment size %d"), len); + return 1; + } + /* Handle rest of code fill */ + memcpy(*bufp, align->code_fill[len], len); + *bufp += len; } else { - /* Just fill with 0 */ - memset(*bufp, 0, len); - *bufp += len; + /* Just fill with 0 */ + memset(*bufp, 0, len); + *bufp += len; } return 0; } yasm_bytecode * yasm_bc_create_align(yasm_expr *boundary, yasm_expr *fill, - yasm_expr *maxskip, const unsigned char **code_fill, - unsigned long line) + yasm_expr *maxskip, const unsigned char **code_fill, + unsigned long line) { bytecode_align *align = yasm_xmalloc(sizeof(bytecode_align)); diff --git a/libyasm/bc-data.c b/libyasm/bc-data.c index 8a32f72a..67753f95 100644 --- a/libyasm/bc-data.c +++ b/libyasm/bc-data.c @@ -48,11 +48,11 @@ struct yasm_dataval { enum { DV_EMPTY, DV_VALUE, DV_RAW, DV_ULEB128, DV_SLEB128 } type; union { - yasm_value val; - struct { - /*@only@*/ unsigned char *contents; - unsigned long len; - } raw; + yasm_value val; + struct { + /*@only@*/ unsigned char *contents; + unsigned long len; + } raw; } data; }; @@ -65,10 +65,10 @@ static void bc_data_destroy(void *contents); static void bc_data_print(const void *contents, FILE *f, int indent_level); static void bc_data_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); static int bc_data_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data); + void *add_span_data); static int bc_data_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); static const yasm_bytecode_callback bc_data_callback = { bc_data_destroy, @@ -107,39 +107,39 @@ bc_data_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) /* Convert values from simple expr to value. */ STAILQ_FOREACH(dv, &bc_data->datahead, link) { - switch (dv->type) { - case DV_VALUE: - if (yasm_value_finalize(&dv->data.val, prev_bc)) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("data expression too complex")); - return; - } - break; - case DV_ULEB128: - case DV_SLEB128: - intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("LEB128 requires constant values")); - return; - } - /* Warn for negative values in unsigned environment. - * This could be an error instead: the likelihood this is - * desired is very low! - */ - if (yasm_intnum_sign(intn) == -1 && dv->type == DV_ULEB128) - yasm_warn_set(YASM_WARN_GENERAL, - N_("negative value in unsigned LEB128")); - break; - default: - break; - } + switch (dv->type) { + case DV_VALUE: + if (yasm_value_finalize(&dv->data.val, prev_bc)) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("data expression too complex")); + return; + } + break; + case DV_ULEB128: + case DV_SLEB128: + intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("LEB128 requires constant values")); + return; + } + /* Warn for negative values in unsigned environment. + * This could be an error instead: the likelihood this is + * desired is very low! + */ + if (yasm_intnum_sign(intn) == -1 && dv->type == DV_ULEB128) + yasm_warn_set(YASM_WARN_GENERAL, + N_("negative value in unsigned LEB128")); + break; + default: + break; + } } } static int bc_data_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { bytecode_data *bc_data = (bytecode_data *)bc->contents; yasm_dataval *dv; @@ -147,24 +147,24 @@ bc_data_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, /* Count up element sizes, rounding up string length. */ STAILQ_FOREACH(dv, &bc_data->datahead, link) { - switch (dv->type) { - case DV_EMPTY: - break; - case DV_VALUE: - bc->len += dv->data.val.size/8; - break; - case DV_RAW: - bc->len += dv->data.raw.len; - break; - case DV_ULEB128: - case DV_SLEB128: - intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); - if (!intn) - yasm_internal_error(N_("non-constant in data_tobytes")); - bc->len += - yasm_intnum_size_leb128(intn, dv->type == DV_SLEB128); - break; - } + switch (dv->type) { + case DV_EMPTY: + break; + case DV_VALUE: + bc->len += dv->data.val.size/8; + break; + case DV_RAW: + bc->len += dv->data.raw.len; + break; + case DV_ULEB128: + case DV_SLEB128: + intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); + if (!intn) + yasm_internal_error(N_("non-constant in data_tobytes")); + bc->len += + yasm_intnum_size_leb128(intn, dv->type == DV_SLEB128); + break; + } } return 0; @@ -172,8 +172,8 @@ bc_data_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int bc_data_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { bytecode_data *bc_data = (bytecode_data *)bc->contents; yasm_dataval *dv; @@ -182,28 +182,28 @@ bc_data_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, unsigned int val_len; STAILQ_FOREACH(dv, &bc_data->datahead, link) { - switch (dv->type) { - case DV_EMPTY: - break; - case DV_VALUE: - val_len = dv->data.val.size/8; - if (output_value(&dv->data.val, *bufp, val_len, - (unsigned long)(*bufp-bufp_orig), bc, 1, d)) - return 1; - *bufp += val_len; - break; - case DV_RAW: - memcpy(*bufp, dv->data.raw.contents, dv->data.raw.len); - *bufp += dv->data.raw.len; - break; - case DV_ULEB128: - case DV_SLEB128: - intn = yasm_expr_get_intnum(&dv->data.val.abs, 234); - if (!intn) - yasm_internal_error(N_("non-constant in data_tobytes")); - *bufp += - yasm_intnum_get_leb128(intn, *bufp, dv->type == DV_SLEB128); - } + switch (dv->type) { + case DV_EMPTY: + break; + case DV_VALUE: + val_len = dv->data.val.size/8; + if (output_value(&dv->data.val, *bufp, val_len, + (unsigned long)(*bufp-bufp_orig), bc, 1, d)) + return 1; + *bufp += val_len; + break; + case DV_RAW: + memcpy(*bufp, dv->data.raw.contents, dv->data.raw.len); + *bufp += dv->data.raw.len; + break; + case DV_ULEB128: + case DV_SLEB128: + intn = yasm_expr_get_intnum(&dv->data.val.abs, 234); + if (!intn) + yasm_internal_error(N_("non-constant in data_tobytes")); + *bufp += + yasm_intnum_get_leb128(intn, *bufp, dv->type == DV_SLEB128); + } } return 0; @@ -211,7 +211,7 @@ bc_data_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_bytecode * yasm_bc_create_data(yasm_datavalhead *datahead, unsigned int size, - int append_zero, yasm_arch *arch, unsigned long line) + int append_zero, yasm_arch *arch, unsigned long line) { bytecode_data *data = yasm_xmalloc(sizeof(bytecode_data)); yasm_bytecode *bc = yasm_bc_create_common(&bc_data_callback, data, line); @@ -226,47 +226,47 @@ yasm_bc_create_data(yasm_datavalhead *datahead, unsigned int size, * precisely paired with the second loop. */ STAILQ_FOREACH(dv, datahead, link) { - switch (dv->type) { - case DV_EMPTY: - break; - case DV_VALUE: - case DV_ULEB128: - case DV_SLEB128: - intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); - if (intn && dv->type == DV_VALUE && (arch || size == 1)) - len += size; - else if (intn && dv->type == DV_ULEB128) - len += yasm_intnum_size_leb128(intn, 0); - else if (intn && dv->type == DV_SLEB128) - len += yasm_intnum_size_leb128(intn, 1); - else { - if (len > 0) { - /* Create bytecode for all previous len */ - dvo = yasm_dv_create_raw(yasm_xmalloc(len), len); - STAILQ_INSERT_TAIL(&data->datahead, dvo, link); - len = 0; - } - - /* Create bytecode for this value */ - dvo = yasm_xmalloc(sizeof(yasm_dataval)); - STAILQ_INSERT_TAIL(&data->datahead, dvo, link); - } - break; - case DV_RAW: - rlen = dv->data.raw.len; - /* find count, rounding up to nearest multiple of size */ - rlen = (rlen + size - 1) / size; - len += rlen*size; - break; - } - if (append_zero) - len++; + switch (dv->type) { + case DV_EMPTY: + break; + case DV_VALUE: + case DV_ULEB128: + case DV_SLEB128: + intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); + if (intn && dv->type == DV_VALUE && (arch || size == 1)) + len += size; + else if (intn && dv->type == DV_ULEB128) + len += yasm_intnum_size_leb128(intn, 0); + else if (intn && dv->type == DV_SLEB128) + len += yasm_intnum_size_leb128(intn, 1); + else { + if (len > 0) { + /* Create bytecode for all previous len */ + dvo = yasm_dv_create_raw(yasm_xmalloc(len), len); + STAILQ_INSERT_TAIL(&data->datahead, dvo, link); + len = 0; + } + + /* Create bytecode for this value */ + dvo = yasm_xmalloc(sizeof(yasm_dataval)); + STAILQ_INSERT_TAIL(&data->datahead, dvo, link); + } + break; + case DV_RAW: + rlen = dv->data.raw.len; + /* find count, rounding up to nearest multiple of size */ + rlen = (rlen + size - 1) / size; + len += rlen*size; + break; + } + if (append_zero) + len++; } /* Create final dataval for any trailing length */ if (len > 0) { - dvo = yasm_dv_create_raw(yasm_xmalloc(len), len); - STAILQ_INSERT_TAIL(&data->datahead, dvo, link); + dvo = yasm_dv_create_raw(yasm_xmalloc(len), len); + STAILQ_INSERT_TAIL(&data->datahead, dvo, link); } /* Second iteration: copy data and delete input datavals. */ @@ -274,64 +274,64 @@ yasm_bc_create_data(yasm_datavalhead *datahead, unsigned int size, dvo = STAILQ_FIRST(&data->datahead); len = 0; while (dv && dvo) { - switch (dv->type) { - case DV_EMPTY: - break; - case DV_VALUE: - case DV_ULEB128: - case DV_SLEB128: - intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); - if (intn && dv->type == DV_VALUE && (arch || size == 1)) { - if (size == 1) - yasm_intnum_get_sized(intn, - &dvo->data.raw.contents[len], - 1, 8, 0, 0, 1); - else - yasm_arch_intnum_tobytes(arch, intn, - &dvo->data.raw.contents[len], - size, size*8, 0, bc, 1); - yasm_value_delete(&dv->data.val); - len += size; - } else if (intn && dv->type == DV_ULEB128) { - len += yasm_intnum_get_leb128(intn, - &dvo->data.raw.contents[len], - 0); - yasm_value_delete(&dv->data.val); - } else if (intn && dv->type == DV_SLEB128) { - len += yasm_intnum_get_leb128(intn, - &dvo->data.raw.contents[len], - 1); - yasm_value_delete(&dv->data.val); - } else { - if (len > 0) - dvo = STAILQ_NEXT(dvo, link); - dvo->type = dv->type; - dvo->data.val = dv->data.val; /* structure copy */ - dvo->data.val.size = size*8; /* remember size */ - dvo = STAILQ_NEXT(dvo, link); - len = 0; - } - break; - case DV_RAW: - rlen = dv->data.raw.len; - memcpy(&dvo->data.raw.contents[len], dv->data.raw.contents, - rlen); - yasm_xfree(dv->data.raw.contents); - len += rlen; - /* pad with 0's to nearest multiple of size */ - rlen %= size; - if (rlen > 0) { - rlen = size-rlen; - for (i=0; idata.raw.contents[len++] = 0; - } - break; - } - if (append_zero) - dvo->data.raw.contents[len++] = 0; - dv2 = STAILQ_NEXT(dv, link); - yasm_xfree(dv); - dv = dv2; + switch (dv->type) { + case DV_EMPTY: + break; + case DV_VALUE: + case DV_ULEB128: + case DV_SLEB128: + intn = yasm_expr_get_intnum(&dv->data.val.abs, 0); + if (intn && dv->type == DV_VALUE && (arch || size == 1)) { + if (size == 1) + yasm_intnum_get_sized(intn, + &dvo->data.raw.contents[len], + 1, 8, 0, 0, 1); + else + yasm_arch_intnum_tobytes(arch, intn, + &dvo->data.raw.contents[len], + size, size*8, 0, bc, 1); + yasm_value_delete(&dv->data.val); + len += size; + } else if (intn && dv->type == DV_ULEB128) { + len += yasm_intnum_get_leb128(intn, + &dvo->data.raw.contents[len], + 0); + yasm_value_delete(&dv->data.val); + } else if (intn && dv->type == DV_SLEB128) { + len += yasm_intnum_get_leb128(intn, + &dvo->data.raw.contents[len], + 1); + yasm_value_delete(&dv->data.val); + } else { + if (len > 0) + dvo = STAILQ_NEXT(dvo, link); + dvo->type = dv->type; + dvo->data.val = dv->data.val; /* structure copy */ + dvo->data.val.size = size*8; /* remember size */ + dvo = STAILQ_NEXT(dvo, link); + len = 0; + } + break; + case DV_RAW: + rlen = dv->data.raw.len; + memcpy(&dvo->data.raw.contents[len], dv->data.raw.contents, + rlen); + yasm_xfree(dv->data.raw.contents); + len += rlen; + /* pad with 0's to nearest multiple of size */ + rlen %= size; + if (rlen > 0) { + rlen = size-rlen; + for (i=0; idata.raw.contents[len++] = 0; + } + break; + } + if (append_zero) + dvo->data.raw.contents[len++] = 0; + dv2 = STAILQ_NEXT(dv, link); + yasm_xfree(dv); + dv = dv2; } return bc; @@ -344,17 +344,17 @@ yasm_bc_create_leb128(yasm_datavalhead *datahead, int sign, unsigned long line) /* Convert all values into LEB type, error on strings/raws */ STAILQ_FOREACH(dv, datahead, link) { - switch (dv->type) { - case DV_VALUE: - dv->type = sign ? DV_SLEB128 : DV_ULEB128; - break; - case DV_RAW: - yasm_error_set(YASM_ERROR_VALUE, - N_("LEB128 does not allow string constants")); - break; - default: - break; - } + switch (dv->type) { + case DV_VALUE: + dv->type = sign ? DV_SLEB128 : DV_ULEB128; + break; + case DV_RAW: + yasm_error_set(YASM_ERROR_VALUE, + N_("LEB128 does not allow string constants")); + break; + default: + break; + } } return yasm_bc_create_data(datahead, 0, 0, 0, line); @@ -390,19 +390,19 @@ yasm_dvs_delete(yasm_datavalhead *headp) cur = STAILQ_FIRST(headp); while (cur) { - next = STAILQ_NEXT(cur, link); - switch (cur->type) { - case DV_VALUE: - yasm_value_delete(&cur->data.val); - break; - case DV_RAW: - yasm_xfree(cur->data.raw.contents); - break; - default: - break; - } - yasm_xfree(cur); - cur = next; + next = STAILQ_NEXT(cur, link); + switch (cur->type) { + case DV_VALUE: + yasm_value_delete(&cur->data.val); + break; + case DV_RAW: + yasm_xfree(cur->data.raw.contents); + break; + default: + break; + } + yasm_xfree(cur); + cur = next; } STAILQ_INIT(headp); } @@ -411,8 +411,8 @@ yasm_dataval * yasm_dvs_append(yasm_datavalhead *headp, yasm_dataval *dv) { if (dv) { - STAILQ_INSERT_TAIL(headp, dv, link); - return dv; + STAILQ_INSERT_TAIL(headp, dv, link); + return dv; } return (yasm_dataval *)NULL; } @@ -424,31 +424,31 @@ yasm_dvs_print(const yasm_datavalhead *head, FILE *f, int indent_level) unsigned long i; STAILQ_FOREACH(cur, head, link) { - switch (cur->type) { - case DV_EMPTY: - fprintf(f, "%*sEmpty\n", indent_level, ""); - break; - case DV_VALUE: - fprintf(f, "%*sValue:\n", indent_level, ""); - yasm_value_print(&cur->data.val, f, indent_level+1); - break; - case DV_RAW: - fprintf(f, "%*sLength=%lu\n", indent_level, "", - cur->data.raw.len); - fprintf(f, "%*sBytes=[", indent_level, ""); - for (i=0; idata.raw.len; i++) - fprintf(f, "0x%02x, ", cur->data.raw.contents[i]); - fprintf(f, "]\n"); - break; - case DV_ULEB128: - fprintf(f, "%*sULEB128 value:\n", indent_level, ""); - yasm_value_print(&cur->data.val, f, indent_level+1); - break; - case DV_SLEB128: - fprintf(f, "%*sSLEB128 value:\n", indent_level, ""); - yasm_value_print(&cur->data.val, f, indent_level+1); - break; - } + switch (cur->type) { + case DV_EMPTY: + fprintf(f, "%*sEmpty\n", indent_level, ""); + break; + case DV_VALUE: + fprintf(f, "%*sValue:\n", indent_level, ""); + yasm_value_print(&cur->data.val, f, indent_level+1); + break; + case DV_RAW: + fprintf(f, "%*sLength=%lu\n", indent_level, "", + cur->data.raw.len); + fprintf(f, "%*sBytes=[", indent_level, ""); + for (i=0; idata.raw.len; i++) + fprintf(f, "0x%02x, ", cur->data.raw.contents[i]); + fprintf(f, "]\n"); + break; + case DV_ULEB128: + fprintf(f, "%*sULEB128 value:\n", indent_level, ""); + yasm_value_print(&cur->data.val, f, indent_level+1); + break; + case DV_SLEB128: + fprintf(f, "%*sSLEB128 value:\n", indent_level, ""); + yasm_value_print(&cur->data.val, f, indent_level+1); + break; + } } } diff --git a/libyasm/bc-incbin.c b/libyasm/bc-incbin.c index 79c1a9fb..cb4a6ce1 100644 --- a/libyasm/bc-incbin.c +++ b/libyasm/bc-incbin.c @@ -46,8 +46,8 @@ typedef struct bytecode_incbin { - /*@only@*/ char *filename; /* file to include data from */ - const char *from; /* filename of what contained incbin */ + /*@only@*/ char *filename; /* file to include data from */ + const char *from; /* filename of what contained incbin */ /* starting offset to read from (NULL=0) */ /*@only@*/ /*@null@*/ yasm_expr *start; @@ -60,10 +60,10 @@ static void bc_incbin_destroy(void *contents); static void bc_incbin_print(const void *contents, FILE *f, int indent_level); static void bc_incbin_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); static int bc_incbin_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data); + void *add_span_data); static int bc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); static const yasm_bytecode_callback bc_incbin_callback = { bc_incbin_destroy, @@ -92,17 +92,17 @@ bc_incbin_print(const void *contents, FILE *f, int indent_level) const bytecode_incbin *incbin = (const bytecode_incbin *)contents; fprintf(f, "%*s_IncBin_\n", indent_level, ""); fprintf(f, "%*sFilename=`%s'\n", indent_level, "", - incbin->filename); + incbin->filename); fprintf(f, "%*sStart=", indent_level, ""); if (!incbin->start) - fprintf(f, "nil (0)"); + fprintf(f, "nil (0)"); else - yasm_expr_print(incbin->start, f); + yasm_expr_print(incbin->start, f); fprintf(f, "%*sMax Len=", indent_level, ""); if (!incbin->maxlen) - fprintf(f, "nil (unlimited)"); + fprintf(f, "nil (unlimited)"); else - yasm_expr_print(incbin->maxlen, f); + yasm_expr_print(incbin->maxlen, f); fprintf(f, "\n"); } @@ -113,25 +113,25 @@ bc_incbin_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) yasm_value val; if (yasm_value_finalize_expr(&val, incbin->start, prev_bc, 0)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("start expression too complex")); + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("start expression too complex")); else if (val.rel) - yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, - N_("start expression not absolute")); + yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, + N_("start expression not absolute")); incbin->start = val.abs; if (yasm_value_finalize_expr(&val, incbin->maxlen, prev_bc, 0)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("maximum length expression too complex")); + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("maximum length expression too complex")); else if (val.rel) - yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, - N_("maximum length expression not absolute")); + yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, + N_("maximum length expression not absolute")); incbin->maxlen = val.abs; } static int bc_incbin_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { bytecode_incbin *incbin = (bytecode_incbin *)bc->contents; FILE *f; @@ -140,66 +140,66 @@ bc_incbin_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, /* Try to convert start to integer value */ if (incbin->start) { - num = yasm_expr_get_intnum(&incbin->start, 0); - if (num) - start = yasm_intnum_get_uint(num); - if (!num) { - /* FIXME */ - yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED, - N_("incbin does not yet understand non-constant")); - return -1; - } + num = yasm_expr_get_intnum(&incbin->start, 0); + if (num) + start = yasm_intnum_get_uint(num); + if (!num) { + /* FIXME */ + yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED, + N_("incbin does not yet understand non-constant")); + return -1; + } } /* Try to convert maxlen to integer value */ if (incbin->maxlen) { - num = yasm_expr_get_intnum(&incbin->maxlen, 0); - if (num) - maxlen = yasm_intnum_get_uint(num); - if (!num) { - /* FIXME */ - yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED, - N_("incbin does not yet understand non-constant")); - return -1; - } + num = yasm_expr_get_intnum(&incbin->maxlen, 0); + if (num) + maxlen = yasm_intnum_get_uint(num); + if (!num) { + /* FIXME */ + yasm_error_set(YASM_ERROR_NOT_IMPLEMENTED, + N_("incbin does not yet understand non-constant")); + return -1; + } } /* Open file and determine its length */ f = yasm_fopen_include(incbin->filename, incbin->from, "rb", NULL); if (!f) { - yasm_error_set(YASM_ERROR_IO, - N_("`incbin': unable to open file `%s'"), - incbin->filename); - return -1; + yasm_error_set(YASM_ERROR_IO, + N_("`incbin': unable to open file `%s'"), + incbin->filename); + return -1; } if (fseek(f, 0L, SEEK_END) < 0) { - yasm_error_set(YASM_ERROR_IO, - N_("`incbin': unable to seek on file `%s'"), - incbin->filename); - return -1; + yasm_error_set(YASM_ERROR_IO, + N_("`incbin': unable to seek on file `%s'"), + incbin->filename); + return -1; } flen = (unsigned long)ftell(f); fclose(f); /* Compute length of incbin from start, maxlen, and len */ if (start > flen) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("`incbin': start past end of file `%s'"), - incbin->filename); - start = flen; + yasm_warn_set(YASM_WARN_GENERAL, + N_("`incbin': start past end of file `%s'"), + incbin->filename); + start = flen; } flen -= start; if (incbin->maxlen) - if (maxlen < flen) - flen = maxlen; + if (maxlen < flen) + flen = maxlen; bc->len += flen; return 0; } static int bc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { bytecode_incbin *incbin = (bytecode_incbin *)bc->contents; FILE *f; @@ -208,37 +208,37 @@ bc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Convert start to integer value */ if (incbin->start) { - num = yasm_expr_get_intnum(&incbin->start, 0); - if (!num) - yasm_internal_error( - N_("could not determine start in bc_tobytes_incbin")); - start = yasm_intnum_get_uint(num); + num = yasm_expr_get_intnum(&incbin->start, 0); + if (!num) + yasm_internal_error( + N_("could not determine start in bc_tobytes_incbin")); + start = yasm_intnum_get_uint(num); } /* Open file */ f = yasm_fopen_include(incbin->filename, incbin->from, "rb", NULL); if (!f) { - yasm_error_set(YASM_ERROR_IO, N_("`incbin': unable to open file `%s'"), - incbin->filename); - return 1; + yasm_error_set(YASM_ERROR_IO, N_("`incbin': unable to open file `%s'"), + incbin->filename); + return 1; } /* Seek to start of data */ if (fseek(f, (long)start, SEEK_SET) < 0) { - yasm_error_set(YASM_ERROR_IO, - N_("`incbin': unable to seek on file `%s'"), - incbin->filename); - fclose(f); - return 1; + yasm_error_set(YASM_ERROR_IO, + N_("`incbin': unable to seek on file `%s'"), + incbin->filename); + fclose(f); + return 1; } /* Read len bytes */ if (fread(*bufp, 1, (size_t)bc->len, f) < (size_t)bc->len) { - yasm_error_set(YASM_ERROR_IO, - N_("`incbin': unable to read %lu bytes from file `%s'"), - bc->len, incbin->filename); - fclose(f); - return 1; + yasm_error_set(YASM_ERROR_IO, + N_("`incbin': unable to read %lu bytes from file `%s'"), + bc->len, incbin->filename); + fclose(f); + return 1; } *bufp += bc->len; @@ -248,7 +248,7 @@ bc_incbin_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_bytecode * yasm_bc_create_incbin(char *filename, yasm_expr *start, yasm_expr *maxlen, - yasm_linemap *linemap, unsigned long line) + yasm_linemap *linemap, unsigned long line) { bytecode_incbin *incbin = yasm_xmalloc(sizeof(bytecode_incbin)); unsigned long xline; diff --git a/libyasm/bc-insn.c b/libyasm/bc-insn.c index b4d9ce1b..7250bc16 100644 --- a/libyasm/bc-insn.c +++ b/libyasm/bc-insn.c @@ -61,10 +61,10 @@ static void bc_insn_destroy(void *contents); static void bc_insn_print(const void *contents, FILE *f, int indent_level); static void bc_insn_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); static int bc_insn_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data); + void *add_span_data); static int bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); static const yasm_bytecode_callback bc_insn_callback = { bc_insn_destroy, @@ -87,7 +87,7 @@ void yasm_ea_set_len(yasm_effaddr *ptr, unsigned int len) { if (!ptr) - return; + return; /* Currently don't warn if length truncated, as this is called only from * an explicit override, where we expect the user knows what they're doing. @@ -100,7 +100,7 @@ void yasm_ea_set_nosplit(yasm_effaddr *ptr, unsigned int nosplit) { if (!ptr) - return; + return; ptr->nosplit = (unsigned char)nosplit; } @@ -109,7 +109,7 @@ void yasm_ea_set_strong(yasm_effaddr *ptr, unsigned int strong) { if (!ptr) - return; + return; ptr->strong = (unsigned char)strong; } @@ -118,11 +118,11 @@ void yasm_ea_set_segreg(yasm_effaddr *ea, uintptr_t segreg) { if (!ea) - return; + return; if (segreg != 0 && ea->segreg != 0) - yasm_warn_set(YASM_WARN_GENERAL, - N_("multiple segment overrides, using leftmost")); + yasm_warn_set(YASM_WARN_GENERAL, + N_("multiple segment overrides, using leftmost")); ea->segreg = segreg; } @@ -153,15 +153,15 @@ bc_insn_destroy(void *contents) { bytecode_insn *insn = (bytecode_insn *)contents; if (insn->num_operands > 0) - yasm_ops_delete(&insn->operands, 0); + yasm_ops_delete(&insn->operands, 0); if (insn->num_prefixes > 0) { - int i; - for (i=0; inum_prefixes; i++) - yasm_xfree(insn->prefixes[i]); - yasm_xfree(insn->prefixes); + int i; + for (i=0; inum_prefixes; i++) + yasm_xfree(insn->prefixes[i]); + yasm_xfree(insn->prefixes); } if (insn->num_segregs > 0) - yasm_xfree(insn->segregs); + yasm_xfree(insn->segregs); yasm_xfree(contents); } @@ -182,65 +182,65 @@ bc_insn_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) /* Simplify the operands' expressions first. */ for (i = 0, op = yasm_ops_first(&insn->operands); - op && inum_operands; op = yasm_operand_next(op), i++) { - /* Check operand type */ - switch (op->type) { - case YASM_INSN__OPERAND_MEMORY: - /* Don't get over-ambitious here; some archs' memory expr - * parser are sensitive to the presence of *1, etc, so don't - * simplify reg*1 identities. - */ - if (op->data.ea) - op->data.ea->disp.abs = - yasm_expr__level_tree(op->data.ea->disp.abs, 1, 1, 0, - 0, NULL, NULL); - if (yasm_error_occurred()) { - /* Add a pointer to where it was used to the error */ - yasm_error_fetch(&eclass, &str, &xrefline, &xrefstr); - if (xrefstr) { - yasm_error_set_xref(xrefline, "%s", xrefstr); - yasm_xfree(xrefstr); - } - if (str) { - yasm_error_set(eclass, "%s in memory expression", str); - yasm_xfree(str); - } - return; - } - break; - case YASM_INSN__OPERAND_IMM: - op->data.val = - yasm_expr__level_tree(op->data.val, 1, 1, 1, 0, NULL, - NULL); - if (yasm_error_occurred()) { - /* Add a pointer to where it was used to the error */ - yasm_error_fetch(&eclass, &str, &xrefline, &xrefstr); - if (xrefstr) { - yasm_error_set_xref(xrefline, "%s", xrefstr); - yasm_xfree(xrefstr); - } - if (str) { - yasm_error_set(eclass, "%s in immediate expression", - str); - yasm_xfree(str); - } - return; - } - break; - default: - break; - } + op && inum_operands; op = yasm_operand_next(op), i++) { + /* Check operand type */ + switch (op->type) { + case YASM_INSN__OPERAND_MEMORY: + /* Don't get over-ambitious here; some archs' memory expr + * parser are sensitive to the presence of *1, etc, so don't + * simplify reg*1 identities. + */ + if (op->data.ea) + op->data.ea->disp.abs = + yasm_expr__level_tree(op->data.ea->disp.abs, 1, 1, 0, + 0, NULL, NULL); + if (yasm_error_occurred()) { + /* Add a pointer to where it was used to the error */ + yasm_error_fetch(&eclass, &str, &xrefline, &xrefstr); + if (xrefstr) { + yasm_error_set_xref(xrefline, "%s", xrefstr); + yasm_xfree(xrefstr); + } + if (str) { + yasm_error_set(eclass, "%s in memory expression", str); + yasm_xfree(str); + } + return; + } + break; + case YASM_INSN__OPERAND_IMM: + op->data.val = + yasm_expr__level_tree(op->data.val, 1, 1, 1, 0, NULL, + NULL); + if (yasm_error_occurred()) { + /* Add a pointer to where it was used to the error */ + yasm_error_fetch(&eclass, &str, &xrefline, &xrefstr); + if (xrefstr) { + yasm_error_set_xref(xrefline, "%s", xrefstr); + yasm_xfree(xrefstr); + } + if (str) { + yasm_error_set(eclass, "%s in immediate expression", + str); + yasm_xfree(str); + } + return; + } + break; + default: + break; + } } yasm_arch_finalize_insn(insn->arch, bc, prev_bc, insn->insn_data, - insn->num_operands, &insn->operands, - insn->num_prefixes, insn->prefixes, - insn->num_segregs, insn->segregs); + insn->num_operands, &insn->operands, + insn->num_prefixes, insn->prefixes, + insn->num_segregs, insn->segregs); } static int bc_insn_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("bc_insn_calc_len() is not implemented")); /*@notreached@*/ @@ -249,8 +249,8 @@ bc_insn_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { yasm_internal_error(N_("bc_insn_tobytes() is not implemented")); /*@notreached@*/ @@ -259,8 +259,8 @@ bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_bytecode * yasm_bc_create_insn(yasm_arch *arch, const uintptr_t insn_data[4], - int num_operands, /*@null@*/ yasm_insn_operands *operands, - unsigned long line) + int num_operands, /*@null@*/ yasm_insn_operands *operands, + unsigned long line) { bytecode_insn *insn = yasm_xmalloc(sizeof(bytecode_insn)); @@ -271,9 +271,9 @@ yasm_bc_create_insn(yasm_arch *arch, const uintptr_t insn_data[4], insn->insn_data[3] = insn_data[3]; insn->num_operands = num_operands; if (operands) - insn->operands = *operands; /* structure copy */ + insn->operands = *operands; /* structure copy */ else - yasm_ops_initialize(&insn->operands); + yasm_ops_initialize(&insn->operands); insn->num_prefixes = 0; insn->prefixes = NULL; insn->num_segregs = 0; @@ -310,8 +310,8 @@ yasm_bc_insn_add_prefix(yasm_bytecode *bc, const uintptr_t prefix_data[4]) assert(bc->callback == bc_insn_callback); insn->prefixes = - yasm_xrealloc(insn->prefixes, - (insn->num_prefixes+1)*sizeof(uintptr_t *)); + yasm_xrealloc(insn->prefixes, + (insn->num_prefixes+1)*sizeof(uintptr_t *)); insn->prefixes[insn->num_prefixes] = yasm_xmalloc(4*sizeof(uintptr_t)); insn->prefixes[insn->num_prefixes][0] = prefix_data[0]; insn->prefixes[insn->num_prefixes][1] = prefix_data[1]; @@ -328,7 +328,7 @@ yasm_bc_insn_add_seg_prefix(yasm_bytecode *bc, uintptr_t segreg) assert(bc->callback == bc_insn_callback); insn->segregs = - yasm_xrealloc(insn->segregs, (insn->num_segregs+1)*sizeof(uintptr_t)); + yasm_xrealloc(insn->segregs, (insn->num_segregs+1)*sizeof(uintptr_t)); insn->segregs[insn->num_segregs] = segreg; insn->num_segregs++; } diff --git a/libyasm/bc-int.h b/libyasm/bc-int.h index 5bcd22e3..14038c63 100644 --- a/libyasm/bc-int.h +++ b/libyasm/bc-int.h @@ -32,16 +32,16 @@ typedef struct yasm_bytecode_callback { void (*print) (const void *contents, FILE *f, int indent_level); void (*finalize) (yasm_bytecode *bc, yasm_bytecode *prev_bc); int (*calc_len) (yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data); + void *add_span_data); int (*expand) (yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres); + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres); int (*tobytes) (yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); enum yasm_bytecode_special_type { - YASM_BC_SPECIAL_NONE = 0, - YASM_BC_SPECIAL_RESERVE,/* Reserves space instead of outputting data */ - YASM_BC_SPECIAL_OFFSET /* Adjusts offset instead of calculating len */ + YASM_BC_SPECIAL_NONE = 0, + YASM_BC_SPECIAL_RESERVE,/* Reserves space instead of outputting data */ + YASM_BC_SPECIAL_OFFSET /* Adjusts offset instead of calculating len */ } special; } yasm_bytecode_callback; @@ -56,15 +56,15 @@ struct yasm_bytecode { /* number of times bytecode is repeated, NULL=1. */ /*@only@*/ /*@null@*/ yasm_expr *multiple; - unsigned long len; /* total length of entire bytecode - (not including multiple copies) */ - long mult_int; /* number of copies: integer version */ + unsigned long len; /* total length of entire bytecode + (not including multiple copies) */ + long mult_int; /* number of copies: integer version */ /* where it came from */ unsigned long line; /* other assembler state info */ - unsigned long offset; /* ~0UL if unknown */ + unsigned long offset; /* ~0UL if unknown */ unsigned long bc_index; /* NULL-terminated array of labels that point to this bytecode (as the @@ -76,10 +76,10 @@ struct yasm_bytecode { }; /** Create a bytecode of any specified type. - * \param callback bytecode callback functions, if NULL, creates empty - * bytecode (may not be resolved or output) - * \param contents type-specific data - * \param line virtual line (from yasm_linemap) + * \param callback bytecode callback functions, if NULL, creates empty + * bytecode (may not be resolved or output) + * \param contents type-specific data + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode of the specified type. */ /*@only@*/ yasm_bytecode *yasm_bc_create_common @@ -87,13 +87,13 @@ struct yasm_bytecode { /*@only@*/ /*@null@*/ void *contents, unsigned long line); /** Transform a bytecode of any type into a different type. - * \param bc bytecode to transform - * \param callback new bytecode callback function - * \param contents new type-specific data + * \param bc bytecode to transform + * \param callback new bytecode callback function + * \param contents new type-specific data */ void yasm_bc_transform(yasm_bytecode *bc, - const yasm_bytecode_callback *callback, - void *contents); + const yasm_bytecode_callback *callback, + void *contents); /** Common bytecode callback finalize function, for where no finalization * is ever required for this type of bytecode. @@ -108,6 +108,6 @@ int yasm_bc_expand_common (yasm_bytecode *bc, int span, long old_val, long new_val, /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres); -#define yasm_bc__next(x) STAILQ_NEXT(x, link) +#define yasm_bc__next(x) STAILQ_NEXT(x, link) #endif diff --git a/libyasm/bc-org.c b/libyasm/bc-org.c index 67b278e8..001d2005 100644 --- a/libyasm/bc-org.c +++ b/libyasm/bc-org.c @@ -43,20 +43,20 @@ typedef struct bytecode_org { - unsigned long start; /* target starting offset within section */ + unsigned long start; /* target starting offset within section */ } bytecode_org; static void bc_org_destroy(void *contents); static void bc_org_print(const void *contents, FILE *f, int indent_level); static void bc_org_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); static int bc_org_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data); + void *add_span_data); static int bc_org_expand(yasm_bytecode *bc, int span, long old_val, - long new_val, /*@out@*/ long *neg_thres, - /*@out@*/ long *pos_thres); + long new_val, /*@out@*/ long *neg_thres, + /*@out@*/ long *pos_thres); static int bc_org_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); static const yasm_bytecode_callback bc_org_callback = { bc_org_destroy, @@ -90,29 +90,29 @@ bc_org_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) static int bc_org_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { bytecode_org *org = (bytecode_org *)bc->contents; long neg_thres = 0; long pos_thres = org->start; if (bc_org_expand(bc, 0, 0, (long)bc->offset, &neg_thres, &pos_thres) < 0) - return -1; + return -1; return 0; } static int bc_org_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { bytecode_org *org = (bytecode_org *)bc->contents; /* Check for overrun */ if ((unsigned long)new_val > org->start) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("ORG overlap with already existing data")); - return -1; + yasm_error_set(YASM_ERROR_GENERAL, + N_("ORG overlap with already existing data")); + return -1; } /* Generate space to start offset */ @@ -122,21 +122,21 @@ bc_org_expand(yasm_bytecode *bc, int span, long old_val, long new_val, static int bc_org_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { bytecode_org *org = (bytecode_org *)bc->contents; unsigned long len, i; /* Sanity check for overrun */ if (bc->offset > org->start) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("ORG overlap with already existing data")); - return 1; + yasm_error_set(YASM_ERROR_GENERAL, + N_("ORG overlap with already existing data")); + return 1; } len = org->start - bc->offset; for (i=0; inumitems, f); fprintf(f, "\n%*sItem Size=%u\n", indent_level, "", - (unsigned int)reserve->itemsize); + (unsigned int)reserve->itemsize); } static void @@ -93,16 +93,16 @@ bc_reserve_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) bytecode_reserve *reserve = (bytecode_reserve *)bc->contents; /* multiply reserve expression into multiple */ if (!bc->multiple) - bc->multiple = reserve->numitems; + bc->multiple = reserve->numitems; else - bc->multiple = yasm_expr_create_tree(bc->multiple, YASM_EXPR_MUL, - reserve->numitems, bc->line); + bc->multiple = yasm_expr_create_tree(bc->multiple, YASM_EXPR_MUL, + reserve->numitems, bc->line); reserve->numitems = NULL; } static int bc_reserve_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { bytecode_reserve *reserve = (bytecode_reserve *)bc->contents; bc->len += reserve->itemsize; @@ -111,8 +111,8 @@ bc_reserve_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int bc_reserve_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { yasm_internal_error(N_("bc_reserve_tobytes called")); /*@notreached@*/ @@ -121,7 +121,7 @@ bc_reserve_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_bytecode * yasm_bc_create_reserve(yasm_expr *numitems, unsigned int itemsize, - unsigned long line) + unsigned long line) { bytecode_reserve *reserve = yasm_xmalloc(sizeof(bytecode_reserve)); diff --git a/libyasm/bitvect.c b/libyasm/bitvect.c index c39f9b65..dad4826b 100644 --- a/libyasm/bitvect.c +++ b/libyasm/bitvect.c @@ -1780,11 +1780,11 @@ BitVector_from_Dec_static_data *BitVector_from_Dec_static_Boot(N_word bits) data->rank = BitVector_Create(bits,FALSE); data->temp = BitVector_Create(bits,FALSE); } else { - data->term = NULL; - data->base = NULL; - data->prod = NULL; - data->rank = NULL; - data->temp = NULL; + data->term = NULL; + data->base = NULL; + data->prod = NULL; + data->rank = NULL; + data->temp = NULL; } return data; } @@ -1802,7 +1802,7 @@ void BitVector_from_Dec_static_Shutdown(BitVector_from_Dec_static_data *data) } ErrCode BitVector_from_Dec_static(BitVector_from_Dec_static_data *data, - wordptr addr, charptr string) + wordptr addr, charptr string) { ErrCode error = ErrCode_Ok; N_word bits = bits_(addr); @@ -1824,11 +1824,11 @@ ErrCode BitVector_from_Dec_static(BitVector_from_Dec_static_data *data, if (bits > 0) { - term = data->term; - base = data->base; - prod = data->prod; - rank = data->rank; - temp = data->temp; + term = data->term; + base = data->base; + prod = data->prod; + rank = data->rank; + temp = data->temp; length = strlen((char *) string); if (length == 0) return(ErrCode_Pars); @@ -1840,10 +1840,10 @@ ErrCode BitVector_from_Dec_static(BitVector_from_Dec_static_data *data, if (--length == 0) return(ErrCode_Pars); } string += length; - if (init) - { - BitVector_Empty(prod); - BitVector_Empty(rank); + if (init) + { + BitVector_Empty(prod); + BitVector_Empty(rank); } BitVector_Empty(addr); *base = EXP10; @@ -3873,9 +3873,9 @@ void Matrix_Transpose(wordptr X, N_int rowsX, N_int colsX, /* ARTISTIC LICENSE: */ /*****************************************************************************/ /* - The "Artistic License" + The "Artistic License" - Preamble + Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some @@ -3886,30 +3886,30 @@ reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the - Copyright Holder, and derivatives of that collection of files - created through textual modification. + "Package" refers to the collection of files distributed by the + Copyright Holder, and derivatives of that collection of files + created through textual modification. - "Standard Version" refers to such a Package if it has not been - modified, or has been modified in accordance with the wishes - of the Copyright Holder as specified below. + "Standard Version" refers to such a Package if it has not been + modified, or has been modified in accordance with the wishes + of the Copyright Holder as specified below. - "Copyright Holder" is whoever is named in the copyright or - copyrights for the package. + "Copyright Holder" is whoever is named in the copyright or + copyrights for the package. - "You" is you, if you're thinking about copying or distributing - this Package. + "You" is you, if you're thinking about copying or distributing + this Package. - "Reasonable copying fee" is whatever you can justify on the - basis of media cost, duplication charges, time of people involved, - and so on. (You will not be required to justify it to the - Copyright Holder, but only to the computing community at large - as a market that must bear the fee.) + "Reasonable copying fee" is whatever you can justify on the + basis of media cost, duplication charges, time of people involved, + and so on. (You will not be required to justify it to the + Copyright Holder, but only to the computing community at large + as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item - itself, though there may be fees involved in handling the item. - It also means that recipients of the item may redistribute it - under the same conditions they received it. + "Freely Available" means that no fee is charged for the item + itself, though there may be fees involved in handling the item. + It also means that recipients of the item may redistribute it + under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you @@ -3999,7 +3999,7 @@ products derived from this software without specific prior written permission. IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. - The End + The End */ /*****************************************************************************/ /* GNU GENERAL PUBLIC LICENSE: */ diff --git a/libyasm/bitvect.h b/libyasm/bitvect.h index 4bdcf3ba..e2a10238 100644 --- a/libyasm/bitvect.h +++ b/libyasm/bitvect.h @@ -213,7 +213,7 @@ typedef struct BitVector_from_Dec_static_data BitVector_from_Dec_static_data; BitVector_from_Dec_static_data *BitVector_from_Dec_static_Boot(N_word bits); void BitVector_from_Dec_static_Shutdown(/*@null@*/ BitVector_from_Dec_static_data *data); ErrCode BitVector_from_Dec_static(BitVector_from_Dec_static_data *data, - /*@out@*/ wordptr addr, charptr string); + /*@out@*/ wordptr addr, charptr string); /*@only@*/ charptr BitVector_to_Enum (wordptr addr); ErrCode BitVector_from_Enum (/*@out@*/ wordptr addr, charptr string); @@ -389,9 +389,9 @@ void Matrix_Transpose (wordptr X, N_int rowsX, N_int colsX, /* ARTISTIC LICENSE: */ /*****************************************************************************/ /* - The "Artistic License" + The "Artistic License" - Preamble + Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some @@ -402,30 +402,30 @@ reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the - Copyright Holder, and derivatives of that collection of files - created through textual modification. + "Package" refers to the collection of files distributed by the + Copyright Holder, and derivatives of that collection of files + created through textual modification. - "Standard Version" refers to such a Package if it has not been - modified, or has been modified in accordance with the wishes - of the Copyright Holder as specified below. + "Standard Version" refers to such a Package if it has not been + modified, or has been modified in accordance with the wishes + of the Copyright Holder as specified below. - "Copyright Holder" is whoever is named in the copyright or - copyrights for the package. + "Copyright Holder" is whoever is named in the copyright or + copyrights for the package. - "You" is you, if you're thinking about copying or distributing - this Package. + "You" is you, if you're thinking about copying or distributing + this Package. - "Reasonable copying fee" is whatever you can justify on the - basis of media cost, duplication charges, time of people involved, - and so on. (You will not be required to justify it to the - Copyright Holder, but only to the computing community at large - as a market that must bear the fee.) + "Reasonable copying fee" is whatever you can justify on the + basis of media cost, duplication charges, time of people involved, + and so on. (You will not be required to justify it to the + Copyright Holder, but only to the computing community at large + as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item - itself, though there may be fees involved in handling the item. - It also means that recipients of the item may redistribute it - under the same conditions they received it. + "Freely Available" means that no fee is charged for the item + itself, though there may be fees involved in handling the item. + It also means that recipients of the item may redistribute it + under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you @@ -515,7 +515,7 @@ products derived from this software without specific prior written permission. IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. - The End + The End */ /*****************************************************************************/ /* GNU GENERAL PUBLIC LICENSE: */ diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index 80fcaa88..a93f53a8 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -47,10 +47,10 @@ void yasm_bc_set_multiple(yasm_bytecode *bc, yasm_expr *e) { if (bc->multiple) - bc->multiple = yasm_expr_create_tree(bc->multiple, YASM_EXPR_MUL, e, - e->line); + bc->multiple = yasm_expr_create_tree(bc->multiple, YASM_EXPR_MUL, e, + e->line); else - bc->multiple = e; + bc->multiple = e; } void @@ -60,7 +60,7 @@ yasm_bc_finalize_common(yasm_bytecode *bc, yasm_bytecode *prev_bc) int yasm_bc_expand_common(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { yasm_internal_error(N_("bytecode does not have any dependent spans")); /*@unreached@*/ @@ -69,17 +69,17 @@ yasm_bc_expand_common(yasm_bytecode *bc, int span, long old_val, long new_val, void yasm_bc_transform(yasm_bytecode *bc, const yasm_bytecode_callback *callback, - void *contents) + void *contents) { if (bc->callback) - bc->callback->destroy(bc->contents); + bc->callback->destroy(bc->contents); bc->callback = callback; bc->contents = contents; } yasm_bytecode * yasm_bc_create_common(const yasm_bytecode_callback *callback, void *contents, - unsigned long line) + unsigned long line) { yasm_bytecode *bc = yasm_xmalloc(sizeof(yasm_bytecode)); @@ -89,7 +89,7 @@ yasm_bc_create_common(const yasm_bytecode_callback *callback, void *contents, bc->len = 0; bc->mult_int = 1; bc->line = line; - bc->offset = ~0UL; /* obviously incorrect / uninitialized value */ + bc->offset = ~0UL; /* obviously incorrect / uninitialized value */ bc->symrecs = NULL; bc->contents = contents; @@ -106,20 +106,20 @@ void yasm_bc__add_symrec(yasm_bytecode *bc, yasm_symrec *sym) { if (!bc->symrecs) { - bc->symrecs = yasm_xmalloc(2*sizeof(yasm_symrec *)); - bc->symrecs[0] = sym; - bc->symrecs[1] = NULL; + bc->symrecs = yasm_xmalloc(2*sizeof(yasm_symrec *)); + bc->symrecs[0] = sym; + bc->symrecs[1] = NULL; } else { - /* Very inefficient implementation for large numbers of symbols. But - * that would be very unusual, so use the simple algorithm instead. - */ - size_t count = 1; - while (bc->symrecs[count]) - count++; - bc->symrecs = yasm_xrealloc(bc->symrecs, - (count+2)*sizeof(yasm_symrec *)); - bc->symrecs[count] = sym; - bc->symrecs[count+1] = NULL; + /* Very inefficient implementation for large numbers of symbols. But + * that would be very unusual, so use the simple algorithm instead. + */ + size_t count = 1; + while (bc->symrecs[count]) + count++; + bc->symrecs = yasm_xrealloc(bc->symrecs, + (count+2)*sizeof(yasm_symrec *)); + bc->symrecs[count] = sym; + bc->symrecs[count+1] = NULL; } } @@ -127,13 +127,13 @@ void yasm_bc_destroy(yasm_bytecode *bc) { if (!bc) - return; + return; if (bc->callback) - bc->callback->destroy(bc->contents); + bc->callback->destroy(bc->contents); yasm_expr_destroy(bc->multiple); if (bc->symrecs) - yasm_xfree(bc->symrecs); + yasm_xfree(bc->symrecs); yasm_xfree(bc); } @@ -141,14 +141,14 @@ void yasm_bc_print(const yasm_bytecode *bc, FILE *f, int indent_level) { if (!bc->callback) - fprintf(f, "%*s_Empty_\n", indent_level, ""); + fprintf(f, "%*s_Empty_\n", indent_level, ""); else - bc->callback->print(bc->contents, f, indent_level); + bc->callback->print(bc->contents, f, indent_level); fprintf(f, "%*sMultiple=", indent_level, ""); if (!bc->multiple) - fprintf(f, "nil (1)"); + fprintf(f, "nil (1)"); else - yasm_expr_print(bc->multiple, f); + yasm_expr_print(bc->multiple, f); fprintf(f, "\n%*sLength=%lu\n", indent_level, "", bc->len); fprintf(f, "%*sLine Index=%lu\n", indent_level, "", bc->line); fprintf(f, "%*sOffset=%lx\n", indent_level, "", bc->offset); @@ -158,26 +158,26 @@ void yasm_bc_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) { if (bc->callback) - bc->callback->finalize(bc, prev_bc); + bc->callback->finalize(bc, prev_bc); if (bc->multiple) { - yasm_value val; - - if (yasm_value_finalize_expr(&val, bc->multiple, prev_bc, 0)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("multiple expression too complex")); - else if (val.rel) - yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, - N_("multiple expression not absolute")); - /* Finalize creates NULL output if value=0, but bc->multiple is NULL - * if value=1 (this difference is to make the common case small). - * However, this means we need to set bc->multiple explicitly to 0 - * here if val.abs is NULL. - */ - if (val.abs) - bc->multiple = val.abs; - else - bc->multiple = yasm_expr_create_ident( - yasm_expr_int(yasm_intnum_create_uint(0)), bc->line); + yasm_value val; + + if (yasm_value_finalize_expr(&val, bc->multiple, prev_bc, 0)) + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("multiple expression too complex")); + else if (val.rel) + yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, + N_("multiple expression not absolute")); + /* Finalize creates NULL output if value=0, but bc->multiple is NULL + * if value=1 (this difference is to make the common case small). + * However, this means we need to set bc->multiple explicitly to 0 + * here if val.abs is NULL. + */ + if (val.abs) + bc->multiple = val.abs; + else + bc->multiple = yasm_expr_create_ident( + yasm_expr_int(yasm_intnum_create_uint(0)), bc->line); } } @@ -188,14 +188,14 @@ yasm_calc_bc_dist(yasm_bytecode *precbc1, yasm_bytecode *precbc2) yasm_intnum *intn; if (precbc1->section != precbc2->section) - return NULL; + return NULL; dist1 = yasm_bc_next_offset(precbc1); dist2 = yasm_bc_next_offset(precbc2); if (dist2 < dist1) { - intn = yasm_intnum_create_uint(dist1 - dist2); - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); - return intn; + intn = yasm_intnum_create_uint(dist1 - dist2); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + return intn; } dist2 -= dist1; return yasm_intnum_create_uint(dist2); @@ -209,72 +209,72 @@ yasm_bc_next_offset(yasm_bytecode *precbc) int yasm_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { int retval = 0; bc->len = 0; if (!bc->callback) - yasm_internal_error(N_("got empty bytecode in yasm_bc_calc_len")); + yasm_internal_error(N_("got empty bytecode in yasm_bc_calc_len")); else - retval = bc->callback->calc_len(bc, add_span, add_span_data); + retval = bc->callback->calc_len(bc, add_span, add_span_data); /* Check for multiples */ bc->mult_int = 1; if (bc->multiple) { - /*@dependent@*/ /*@null@*/ const yasm_intnum *num; - - num = yasm_expr_get_intnum(&bc->multiple, 0); - if (num) { - if (yasm_intnum_sign(num) < 0) { - yasm_error_set(YASM_ERROR_VALUE, N_("multiple is negative")); - retval = -1; - } else - bc->mult_int = yasm_intnum_get_int(num); - } else { - if (yasm_expr__contains(bc->multiple, YASM_EXPR_FLOAT)) { - yasm_error_set(YASM_ERROR_VALUE, - N_("expression must not contain floating point value")); - retval = -1; - } else { - yasm_value value; - yasm_value_initialize(&value, bc->multiple, 0); - add_span(add_span_data, bc, 0, &value, 0, 0); - bc->mult_int = 0; /* assume 0 to start */ - } - } + /*@dependent@*/ /*@null@*/ const yasm_intnum *num; + + num = yasm_expr_get_intnum(&bc->multiple, 0); + if (num) { + if (yasm_intnum_sign(num) < 0) { + yasm_error_set(YASM_ERROR_VALUE, N_("multiple is negative")); + retval = -1; + } else + bc->mult_int = yasm_intnum_get_int(num); + } else { + if (yasm_expr__contains(bc->multiple, YASM_EXPR_FLOAT)) { + yasm_error_set(YASM_ERROR_VALUE, + N_("expression must not contain floating point value")); + retval = -1; + } else { + yasm_value value; + yasm_value_initialize(&value, bc->multiple, 0); + add_span(add_span_data, bc, 0, &value, 0, 0); + bc->mult_int = 0; /* assume 0 to start */ + } + } } /* If we got an error somewhere along the line, clear out any calc len */ if (retval < 0) - bc->len = 0; + bc->len = 0; return retval; } int yasm_bc_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { if (span == 0) { - bc->mult_int = new_val; - return 1; + bc->mult_int = new_val; + return 1; } if (!bc->callback) { - yasm_internal_error(N_("got empty bytecode in yasm_bc_expand")); - /*@unreached@*/ - return -1; + yasm_internal_error(N_("got empty bytecode in yasm_bc_expand")); + /*@unreached@*/ + return -1; } else - return bc->callback->expand(bc, span, old_val, new_val, neg_thres, - pos_thres); + return bc->callback->expand(bc, span, old_val, new_val, neg_thres, + pos_thres); } /*@null@*/ /*@only@*/ unsigned char * yasm_bc_tobytes(yasm_bytecode *bc, unsigned char *buf, unsigned long *bufsize, - /*@out@*/ int *gap, void *d, - yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc) + /*@out@*/ int *gap, void *d, + yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc) /*@sets *buf@*/ { /*@only@*/ /*@null@*/ unsigned char *mybuf = NULL; @@ -283,36 +283,36 @@ yasm_bc_tobytes(yasm_bytecode *bc, unsigned char *buf, unsigned long *bufsize, int error = 0; if (yasm_bc_get_multiple(bc, &bc->mult_int, 1) || bc->mult_int == 0) { - *bufsize = 0; - return NULL; + *bufsize = 0; + return NULL; } /* special case for reserve bytecodes */ if (bc->callback->special == YASM_BC_SPECIAL_RESERVE) { - *bufsize = bc->len*bc->mult_int; - *gap = 1; - return NULL; /* we didn't allocate a buffer */ + *bufsize = bc->len*bc->mult_int; + *gap = 1; + return NULL; /* we didn't allocate a buffer */ } *gap = 0; if (*bufsize < bc->len*bc->mult_int) { - mybuf = yasm_xmalloc(bc->len*bc->mult_int); - destbuf = mybuf; + mybuf = yasm_xmalloc(bc->len*bc->mult_int); + destbuf = mybuf; } else - destbuf = buf; + destbuf = buf; *bufsize = bc->len*bc->mult_int; if (!bc->callback) - yasm_internal_error(N_("got empty bytecode in bc_tobytes")); + yasm_internal_error(N_("got empty bytecode in bc_tobytes")); else for (i=0; imult_int; i++) { - origbuf = destbuf; - error = bc->callback->tobytes(bc, &destbuf, d, output_value, - output_reloc); + origbuf = destbuf; + error = bc->callback->tobytes(bc, &destbuf, d, output_value, + output_reloc); - if (!error && ((unsigned long)(destbuf - origbuf) != bc->len)) - yasm_internal_error( - N_("written length does not match optimized length")); + if (!error && ((unsigned long)(destbuf - origbuf) != bc->len)) + yasm_internal_error( + N_("written length does not match optimized length")); } return mybuf; @@ -325,17 +325,17 @@ yasm_bc_get_multiple(yasm_bytecode *bc, long *multiple, int calc_bc_dist) *multiple = 1; if (bc->multiple) { - num = yasm_expr_get_intnum(&bc->multiple, calc_bc_dist); - if (!num) { - yasm_error_set(YASM_ERROR_VALUE, - N_("could not determine multiple")); - return 1; - } - if (yasm_intnum_sign(num) < 0) { - yasm_error_set(YASM_ERROR_VALUE, N_("multiple is negative")); - return 1; - } - *multiple = yasm_intnum_get_int(num); + num = yasm_expr_get_intnum(&bc->multiple, calc_bc_dist); + if (!num) { + yasm_error_set(YASM_ERROR_VALUE, + N_("could not determine multiple")); + return 1; + } + if (yasm_intnum_sign(num) < 0) { + yasm_error_set(YASM_ERROR_VALUE, N_("multiple is negative")); + return 1; + } + *multiple = yasm_intnum_get_int(num); } return 0; } diff --git a/libyasm/bytecode.h b/libyasm/bytecode.h index e856a631..8c315603 100644 --- a/libyasm/bytecode.h +++ b/libyasm/bytecode.h @@ -40,37 +40,37 @@ typedef struct yasm_effaddr yasm_effaddr; /** Callbacks for effective address implementations. */ typedef struct yasm_effaddr_callback { /** Destroy the effective address (freeing it). - * \param ea effective address + * \param ea effective address */ void (*destroy) (/*@only@*/ yasm_effaddr *ea); /** Print the effective address. - * \param ea effective address - * \param f file to output to - * \param indent_level indentation level + * \param ea effective address + * \param f file to output to + * \param indent_level indentation level */ void (*print) (const yasm_effaddr *ea, FILE *f, int indent_level); } yasm_effaddr_callback; /** An effective address. */ struct yasm_effaddr { - const yasm_effaddr_callback *callback; /**< callback functions */ + const yasm_effaddr_callback *callback; /**< callback functions */ - yasm_value disp; /**< address displacement */ + yasm_value disp; /**< address displacement */ - uintptr_t segreg; /**< segment register override (0 if none) */ + uintptr_t segreg; /**< segment register override (0 if none) */ unsigned char need_nonzero_len; /**< 1 if length of disp must be >0. */ - unsigned char need_disp; /**< 1 if a displacement should be present - * in the output. - */ - unsigned char nosplit; /**< 1 if reg*2 should not be split into - * reg+reg. (0 if not) - */ - unsigned char strong; /**< 1 if effective address is *definitely* - * an effective address, e.g. in GAS if - * expr(,1) form is used vs. just expr. - */ + unsigned char need_disp; /**< 1 if a displacement should be present + * in the output. + */ + unsigned char nosplit; /**< 1 if reg*2 should not be split into + * reg+reg. (0 if not) + */ + unsigned char strong; /**< 1 if effective address is *definitely* + * an effective address, e.g. in GAS if + * expr(,1) form is used vs. just expr. + */ }; /** A data value (opaque type). */ @@ -83,15 +83,15 @@ typedef struct yasm_datavalhead yasm_datavalhead; #endif /** Get the displacement portion of an effective address. - * \param ea effective address + * \param ea effective address * \return Expression representing the displacement (read-only). */ /*@observer@*/ const yasm_expr *yasm_ea_get_disp(const yasm_effaddr *ea); /** Set the length of the displacement portion of an effective address. * The length is specified in bits. - * \param ea effective address - * \param len length in bits + * \param ea effective address + * \param len length in bits */ void yasm_ea_set_len(yasm_effaddr *ea, unsigned int len); @@ -99,9 +99,9 @@ void yasm_ea_set_len(yasm_effaddr *ea, unsigned int len); * The nosplit flag indicates (for architectures that support complex effective * addresses such as x86) if various types of complex effective addresses can * be split into different forms in order to minimize instruction length. - * \param ea effective address - * \param nosplit nosplit flag setting (0=splits allowed, nonzero=splits - * not allowed) + * \param ea effective address + * \param nosplit nosplit flag setting (0=splits allowed, nonzero=splits + * not allowed) */ void yasm_ea_set_nosplit(yasm_effaddr *ea, unsigned int nosplit); @@ -110,47 +110,47 @@ void yasm_ea_set_nosplit(yasm_effaddr *ea, unsigned int nosplit); * effective address. This is used in e.g. the GAS parser to differentiate * between "expr" (which might or might not be an effective address) and * "expr(,1)" (which is definitely an effective address). - * \param ea effective address - * \param strong strong flag setting (0=not strong, nonzero=strong) + * \param ea effective address + * \param strong strong flag setting (0=not strong, nonzero=strong) */ void yasm_ea_set_strong(yasm_effaddr *ea, unsigned int strong); /** Set segment override for an effective address. * Some architectures (such as x86) support segment overrides on effective * addresses. A override of an override will result in a warning. - * \param ea effective address - * \param segreg segment register (0 if none) + * \param ea effective address + * \param segreg segment register (0 if none) */ void yasm_ea_set_segreg(yasm_effaddr *ea, uintptr_t segreg); /** Delete (free allocated memory for) an effective address. - * \param ea effective address (only pointer to it). + * \param ea effective address (only pointer to it). */ void yasm_ea_destroy(/*@only@*/ yasm_effaddr *ea); /** Print an effective address. For debugging purposes. - * \param f file - * \param indent_level indentation level - * \param ea effective address + * \param f file + * \param indent_level indentation level + * \param ea effective address */ void yasm_ea_print(const yasm_effaddr *ea, FILE *f, int indent_level); /** Set multiple field of a bytecode. * A bytecode can be repeated a number of times when output. This function * sets that multiple. - * \param bc bytecode - * \param e multiple (kept, do not free) + * \param bc bytecode + * \param e multiple (kept, do not free) */ void yasm_bc_set_multiple(yasm_bytecode *bc, /*@keep@*/ yasm_expr *e); /** Create a bytecode containing data value(s). - * \param datahead list of data values (kept, do not free) - * \param size storage size (in bytes) for each data value - * \param append_zero append a single zero byte after each data value - * (if non-zero) - * \param arch architecture (optional); if provided, data items - * are directly simplified to bytes if possible - * \param line virtual line (from yasm_linemap) + * \param datahead list of data values (kept, do not free) + * \param size storage size (in bytes) for each data value + * \param append_zero append a single zero byte after each data value + * (if non-zero) + * \param arch architecture (optional); if provided, data items + * are directly simplified to bytes if possible + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode. */ /*@only@*/ yasm_bytecode *yasm_bc_create_data @@ -158,18 +158,18 @@ void yasm_bc_set_multiple(yasm_bytecode *bc, /*@keep@*/ yasm_expr *e); /*@null@*/ yasm_arch *arch, unsigned long line); /** Create a bytecode containing LEB128-encoded data value(s). - * \param datahead list of data values (kept, do not free) - * \param sign signedness (1=signed, 0=unsigned) of each data value - * \param line virtual line (from yasm_linemap) + * \param datahead list of data values (kept, do not free) + * \param sign signedness (1=signed, 0=unsigned) of each data value + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode. */ /*@only@*/ yasm_bytecode *yasm_bc_create_leb128 (yasm_datavalhead *datahead, int sign, unsigned long line); /** Create a bytecode reserving space. - * \param numitems number of reserve "items" (kept, do not free) - * \param itemsize reserved size (in bytes) for each item - * \param line virtual line (from yasm_linemap) + * \param numitems number of reserve "items" (kept, do not free) + * \param itemsize reserved size (in bytes) for each item + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode. */ /*@only@*/ yasm_bytecode *yasm_bc_create_reserve @@ -177,13 +177,13 @@ void yasm_bc_set_multiple(yasm_bytecode *bc, /*@keep@*/ yasm_expr *e); unsigned long line); /** Create a bytecode that includes a binary file verbatim. - * \param filename path to binary file (kept, do not free) - * \param start starting location in file (in bytes) to read data from - * (kept, do not free); may be NULL to indicate 0 - * \param maxlen maximum number of bytes to read from the file (kept, do - * do not free); may be NULL to indicate no maximum - * \param linemap line mapping repository - * \param line virtual line (from yasm_linemap) for the bytecode + * \param filename path to binary file (kept, do not free) + * \param start starting location in file (in bytes) to read data from + * (kept, do not free); may be NULL to indicate 0 + * \param maxlen maximum number of bytes to read from the file (kept, do + * do not free); may be NULL to indicate no maximum + * \param linemap line mapping repository + * \param line virtual line (from yasm_linemap) for the bytecode * \return Newly allocated bytecode. */ /*@only@*/ yasm_bytecode *yasm_bc_create_incbin @@ -192,11 +192,11 @@ void yasm_bc_set_multiple(yasm_bytecode *bc, /*@keep@*/ yasm_expr *e); unsigned long line); /** Create a bytecode that aligns the following bytecode to a boundary. - * \param boundary byte alignment (must be a power of two) - * \param fill fill data (if NULL, code_fill or 0 is used) - * \param maxskip maximum number of bytes to skip - * \param code_fill code fill data (if NULL, 0 is used) - * \param line virtual line (from yasm_linemap) + * \param boundary byte alignment (must be a power of two) + * \param fill fill data (if NULL, code_fill or 0 is used) + * \param maxskip maximum number of bytes to skip + * \param code_fill code fill data (if NULL, 0 is used) + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode. * \note The precedence on generated fill is as follows: * - from fill parameter (if not NULL) @@ -210,19 +210,19 @@ void yasm_bc_set_multiple(yasm_bytecode *bc, /*@keep@*/ yasm_expr *e); /** Create a bytecode that puts the following bytecode at a fixed section * offset. - * \param start section offset of following bytecode - * \param line virtual line (from yasm_linemap) + * \param start section offset of following bytecode + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode. */ /*@only@*/ yasm_bytecode *yasm_bc_create_org (unsigned long start, unsigned long line); /** Create a bytecode that represents a single instruction. - * \param arch instruction's architecture - * \param insn_data data that identifies the type of instruction - * \param num_operands number of operands - * \param operands instruction operands (may be NULL if no operands) - * \param line virtual line (from yasm_linemap) + * \param arch instruction's architecture + * \param insn_data data that identifies the type of instruction + * \param num_operands number of operands + * \param operands instruction operands (may be NULL if no operands) + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode. * \note Keeps the list of operands; do not call yasm_ops_delete() after * giving operands to this function. @@ -233,30 +233,30 @@ void yasm_bc_set_multiple(yasm_bytecode *bc, /*@keep@*/ yasm_expr *e); /** Create a bytecode that represents a single empty (0 length) instruction. * This is used for handling solitary prefixes. - * \param arch instruction's architecture - * \param line virtual line (from yasm_linemap) + * \param arch instruction's architecture + * \param line virtual line (from yasm_linemap) * \return Newly allocated bytecode. */ /*@only@*/ yasm_bytecode *yasm_bc_create_empty_insn(yasm_arch *arch, - unsigned long line); + unsigned long line); /** Associate a prefix with an instruction bytecode. - * \param bc instruction bytecode - * \param prefix_data data the identifies the prefix + * \param bc instruction bytecode + * \param prefix_data data the identifies the prefix */ void yasm_bc_insn_add_prefix(yasm_bytecode *bc, - const uintptr_t prefix_data[4]); + const uintptr_t prefix_data[4]); /** Associate a segment prefix with an instruction bytecode. - * \param bc instruction bytecode - * \param segreg data the identifies the segment register + * \param bc instruction bytecode + * \param segreg data the identifies the segment register */ void yasm_bc_insn_add_seg_prefix(yasm_bytecode *bc, uintptr_t segreg); /** Get the section that contains a particular bytecode. - * \param bc bytecode + * \param bc bytecode * \return Section containing bc (can be NULL if bytecode is not part of a - * section). + * section). */ /*@dependent@*/ /*@null@*/ yasm_section *yasm_bc_get_section (yasm_bytecode *bc); @@ -264,35 +264,35 @@ void yasm_bc_insn_add_seg_prefix(yasm_bytecode *bc, uintptr_t segreg); #ifdef YASM_LIB_INTERNAL /** Add to the list of symrecs that reference a bytecode. For symrec use * only. - * \param bc bytecode - * \param sym symbol + * \param bc bytecode + * \param sym symbol */ void yasm_bc__add_symrec(yasm_bytecode *bc, /*@dependent@*/ yasm_symrec *sym); #endif /** Delete (free allocated memory for) a bytecode. - * \param bc bytecode (only pointer to it); may be NULL + * \param bc bytecode (only pointer to it); may be NULL */ void yasm_bc_destroy(/*@only@*/ /*@null@*/ yasm_bytecode *bc); /** Print a bytecode. For debugging purposes. - * \param f file - * \param indent_level indentation level - * \param bc bytecode + * \param f file + * \param indent_level indentation level + * \param bc bytecode */ void yasm_bc_print(const yasm_bytecode *bc, FILE *f, int indent_level); /** Finalize a bytecode after parsing. - * \param bc bytecode - * \param prev_bc bytecode directly preceding bc in a list of bytecodes + * \param bc bytecode + * \param prev_bc bytecode directly preceding bc in a list of bytecodes */ void yasm_bc_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); /** Determine the distance between the starting offsets of two bytecodes. - * \param precbc1 preceding bytecode to the first bytecode - * \param precbc2 preceding bytecode to the second bytecode + * \param precbc1 preceding bytecode to the first bytecode + * \param precbc2 preceding bytecode to the second bytecode * \return Distance in bytes between the two bytecodes (bc2-bc1), or NULL if - * the distance was indeterminate. + * the distance was indeterminate. * \warning Only valid /after/ optimization. */ /*@null@*/ /*@only@*/ yasm_intnum *yasm_calc_bc_dist @@ -300,21 +300,21 @@ void yasm_bc_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); /** Get the offset of the next bytecode (the next bytecode doesn't have to * actually exist). - * \param precbc preceding bytecode + * \param precbc preceding bytecode * \return Offset of the next bytecode in bytes. * \warning Only valid /after/ optimization. */ unsigned long yasm_bc_next_offset(yasm_bytecode *precbc); /** Add a dependent span for a bytecode. - * \param add_span_data add_span_data passed into bc_calc_len() - * \param bc bytecode containing span - * \param id non-zero identifier for span; may be any non-zero value - * if <0, expand is called for any change; - * if >0, expand is only called when exceeds threshold - * \param value dependent value for bytecode expansion - * \param neg_thres negative threshold for long/short decision - * \param pos_thres positive threshold for long/short decision + * \param add_span_data add_span_data passed into bc_calc_len() + * \param bc bytecode containing span + * \param id non-zero identifier for span; may be any non-zero value + * if <0, expand is called for any change; + * if >0, expand is only called when exceeds threshold + * \param value dependent value for bytecode expansion + * \param neg_thres negative threshold for long/short decision + * \param pos_thres positive threshold for long/short decision */ typedef void (*yasm_bc_add_span_func) (void *add_span_data, yasm_bytecode *bc, int id, const yasm_value *value, @@ -325,24 +325,24 @@ typedef void (*yasm_bc_add_span_func) * increases, it could cause the bytecode size to increase. * Any bytecode multiple is NOT included in the length or spans generation; * this must be handled at a higher level. - * \param bc bytecode - * \param add_span function to call to add a span - * \param add_span_data extra data to be passed to add_span function + * \param bc bytecode + * \param add_span function to call to add a span + * \param add_span_data extra data to be passed to add_span function * \return 0 if no error occurred, nonzero if there was an error recognized * (and output) during execution. * \note May store to bytecode updated expressions and the short length. */ int yasm_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data); + void *add_span_data); /** Recalculate a bytecode's length based on an expanded span length. - * \param bc bytecode - * \param span span ID (as given to yasm_bc_add_span_func in + * \param bc bytecode + * \param span span ID (as given to yasm_bc_add_span_func in * yasm_bc_calc_len) - * \param old_val previous span value - * \param new_val new span value - * \param neg_thres negative threshold for long/short decision (returned) - * \param pos_thres postivie threshold for long/short decision (returned) + * \param old_val previous span value + * \param new_val new span value + * \param neg_thres negative threshold for long/short decision (returned) + * \param pos_thres postivie threshold for long/short decision (returned) * \return 0 if bc no longer dependent on this span's length, negative if * there was an error recognized (and output) during execution, and * positive if bc size may increase for this span further based on the @@ -350,24 +350,24 @@ int yasm_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, * \note May store to bytecode updated expressions and the updated length. */ int yasm_bc_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres); + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres); /** Convert a bytecode into its byte representation. - * \param bc bytecode - * \param buf byte representation destination buffer - * \param bufsize size of buf (in bytes) prior to call; size of the - * generated data after call - * \param gap if nonzero, indicates the data does not really need to - * exist in the object file; if nonzero, contents of buf - * are undefined [output] - * \param d data to pass to each call to output_value/output_reloc - * \param output_value function to call to convert values into their byte - * representation - * \param output_reloc function to call to output relocation entries - * for a single sym + * \param bc bytecode + * \param buf byte representation destination buffer + * \param bufsize size of buf (in bytes) prior to call; size of the + * generated data after call + * \param gap if nonzero, indicates the data does not really need to + * exist in the object file; if nonzero, contents of buf + * are undefined [output] + * \param d data to pass to each call to output_value/output_reloc + * \param output_value function to call to convert values into their byte + * representation + * \param output_reloc function to call to output relocation entries + * for a single sym * \return Newly allocated buffer that should be used instead of buf for - * reading the byte representation, or NULL if buf was big enough to - * hold the entire byte representation. + * reading the byte representation, or NULL if buf was big enough to + * hold the entire byte representation. * \note Calling twice on the same bytecode may \em not produce the same * results on the second call, as calling this function may result in * non-reversible changes to the bytecode. @@ -379,70 +379,70 @@ int yasm_bc_expand(yasm_bytecode *bc, int span, long old_val, long new_val, /*@sets *buf@*/; /** Get the bytecode multiple value as an integer. - * \param bc bytecode - * \param multiple multiple value (output) - * \param calc_bc_dist nonzero if distances between bytecodes should be - * calculated, 0 if error should be returned in this case + * \param bc bytecode + * \param multiple multiple value (output) + * \param calc_bc_dist nonzero if distances between bytecodes should be + * calculated, 0 if error should be returned in this case * \return 1 on error (set with yasm_error_set), 0 on success. */ int yasm_bc_get_multiple(yasm_bytecode *bc, /*@out@*/ long *multiple, - int calc_bc_dist); + int calc_bc_dist); /** Create a new data value from an expression. - * \param expn expression + * \param expn expression * \return Newly allocated data value. */ yasm_dataval *yasm_dv_create_expr(/*@keep@*/ yasm_expr *expn); /** Create a new data value from a string. - * \param contents string (may contain NULs) - * \param len length of string + * \param contents string (may contain NULs) + * \param len length of string * \return Newly allocated data value. */ yasm_dataval *yasm_dv_create_string(/*@keep@*/ char *contents, size_t len); /** Create a new data value from raw bytes data. - * \param contents raw data (may contain NULs) - * \param len length + * \param contents raw data (may contain NULs) + * \param len length * \return Newly allocated data value. */ yasm_dataval *yasm_dv_create_raw(/*@keep@*/ unsigned char *contents, - unsigned long len); + unsigned long len); #ifndef YASM_DOXYGEN #define yasm_dv_create_string(s, l) yasm_dv_create_raw((unsigned char *)(s), \ - (unsigned long)(l)) + (unsigned long)(l)) #endif /** Initialize a list of data values. - * \param headp list of data values + * \param headp list of data values */ void yasm_dvs_initialize(yasm_datavalhead *headp); #ifdef YASM_LIB_INTERNAL -#define yasm_dvs_initialize(headp) STAILQ_INIT(headp) +#define yasm_dvs_initialize(headp) STAILQ_INIT(headp) #endif /** Delete (free allocated memory for) a list of data values. - * \param headp list of data values + * \param headp list of data values */ void yasm_dvs_delete(yasm_datavalhead *headp); /** Add data value to the end of a list of data values. * \note Does not make a copy of the data value; so don't pass this function - * static or local variables, and discard the dv pointer after calling - * this function. - * \param headp data value list - * \param dv data value (may be NULL) + * static or local variables, and discard the dv pointer after calling + * this function. + * \param headp data value list + * \param dv data value (may be NULL) * \return If data value was actually appended (it wasn't NULL), the data - * value; otherwise NULL. + * value; otherwise NULL. */ /*@null@*/ yasm_dataval *yasm_dvs_append (yasm_datavalhead *headp, /*@returned@*/ /*@null@*/ yasm_dataval *dv); /** Print a data value list. For debugging purposes. - * \param f file - * \param indent_level indentation level - * \param headp data value list + * \param f file + * \param indent_level indentation level + * \param headp data value list */ void yasm_dvs_print(const yasm_datavalhead *headp, FILE *f, int indent_level); diff --git a/libyasm/compat-queue.h b/libyasm/compat-queue.h index 5d73bd04..e9077be9 100644 --- a/libyasm/compat-queue.h +++ b/libyasm/compat-queue.h @@ -2,7 +2,7 @@ * implementation for systems that don't have it. * * Copyright (c) 1991, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -28,12 +28,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)queue.h 8.5 (Berkeley) 8/20/94 + * @(#)queue.h 8.5 (Berkeley) 8/20/94 * $FreeBSD: src/sys/sys/queue.h,v 1.32.2.4 2001/03/31 03:33:39 hsu Exp $ */ #ifndef SYS_QUEUE_H -#define SYS_QUEUE_H +#define SYS_QUEUE_H /* * This file defines four types of data structures: singly-linked lists, @@ -77,380 +77,380 @@ * For details on the use of these macros, see the queue(3) manual page. * * - * SLIST LIST STAILQ TAILQ - * _HEAD + + + + - * _HEAD_INITIALIZER + + + + - * _ENTRY + + + + - * _INIT + + + + - * _EMPTY + + + + - * _FIRST + + + + - * _NEXT + + + + - * _PREV - - - + - * _LAST - - + + - * _FOREACH + + + + - * _FOREACH_SAFE + + + + - * _FOREACH_REVERSE - - - + - * _FOREACH_REVERSE_SAFE - - - + - * _INSERT_HEAD + + + + - * _INSERT_BEFORE - + - + - * _INSERT_AFTER + + + + - * _INSERT_TAIL - - + + - * _CONCAT - - + + - * _REMOVE_HEAD + - + - - * _REMOVE + + + + + * SLIST LIST STAILQ TAILQ + * _HEAD + + + + + * _HEAD_INITIALIZER + + + + + * _ENTRY + + + + + * _INIT + + + + + * _EMPTY + + + + + * _FIRST + + + + + * _NEXT + + + + + * _PREV - - - + + * _LAST - - + + + * _FOREACH + + + + + * _FOREACH_SAFE + + + + + * _FOREACH_REVERSE - - - + + * _FOREACH_REVERSE_SAFE - - - + + * _INSERT_HEAD + + + + + * _INSERT_BEFORE - + - + + * _INSERT_AFTER + + + + + * _INSERT_TAIL - - + + + * _CONCAT - - + + + * _REMOVE_HEAD + - + - + * _REMOVE + + + + * */ /* * Singly-linked List declarations. */ -#define SLIST_HEAD(name, type) \ -struct name { \ - struct type *slh_first; /* first element */ \ +#define SLIST_HEAD(name, type) \ +struct name { \ + struct type *slh_first; /* first element */ \ } -#define SLIST_HEAD_INITIALIZER(head) \ - { NULL } +#define SLIST_HEAD_INITIALIZER(head) \ + { NULL } -#define SLIST_ENTRY(type) \ -struct { \ - struct type *sle_next; /* next element */ \ +#define SLIST_ENTRY(type) \ +struct { \ + struct type *sle_next; /* next element */ \ } /* * Singly-linked List functions. */ -#define SLIST_EMPTY(head) ((head)->slh_first == NULL) +#define SLIST_EMPTY(head) ((head)->slh_first == NULL) -#define SLIST_FIRST(head) ((head)->slh_first) +#define SLIST_FIRST(head) ((head)->slh_first) -#define SLIST_FOREACH(var, head, field) \ - for ((var) = SLIST_FIRST((head)); \ - (var); \ - (var) = SLIST_NEXT((var), field)) +#define SLIST_FOREACH(var, head, field) \ + for ((var) = SLIST_FIRST((head)); \ + (var); \ + (var) = SLIST_NEXT((var), field)) -#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = SLIST_FIRST((head)); \ - (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ - (var) = (tvar)) +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST((head)); \ + (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ + (var) = (tvar)) -#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ - for ((varp) = &SLIST_FIRST((head)); \ - ((var) = *(varp)) != NULL; \ - (varp) = &SLIST_NEXT((var), field)) +#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \ + for ((varp) = &SLIST_FIRST((head)); \ + ((var) = *(varp)) != NULL; \ + (varp) = &SLIST_NEXT((var), field)) -#define SLIST_INIT(head) do { \ - SLIST_FIRST((head)) = NULL; \ +#define SLIST_INIT(head) do { \ + SLIST_FIRST((head)) = NULL; \ } while (0) -#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ - SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ - SLIST_NEXT((slistelm), field) = (elm); \ +#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \ + SLIST_NEXT((slistelm), field) = (elm); \ } while (0) -#define SLIST_INSERT_HEAD(head, elm, field) do { \ - SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ - SLIST_FIRST((head)) = (elm); \ +#define SLIST_INSERT_HEAD(head, elm, field) do { \ + SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \ + SLIST_FIRST((head)) = (elm); \ } while (0) -#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) - -#define SLIST_REMOVE(head, elm, type, field) do { \ - if (SLIST_FIRST((head)) == (elm)) { \ - SLIST_REMOVE_HEAD((head), field); \ - } \ - else { \ - struct type *curelm = SLIST_FIRST((head)); \ - while (SLIST_NEXT(curelm, field) != (elm)) \ - curelm = SLIST_NEXT(curelm, field); \ - SLIST_NEXT(curelm, field) = \ - SLIST_NEXT(SLIST_NEXT(curelm, field), field); \ - } \ +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) + +#define SLIST_REMOVE(head, elm, type, field) do { \ + if (SLIST_FIRST((head)) == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } \ + else { \ + struct type *curelm = SLIST_FIRST((head)); \ + while (SLIST_NEXT(curelm, field) != (elm)) \ + curelm = SLIST_NEXT(curelm, field); \ + SLIST_NEXT(curelm, field) = \ + SLIST_NEXT(SLIST_NEXT(curelm, field), field); \ + } \ } while (0) -#define SLIST_REMOVE_HEAD(head, field) do { \ - SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ +#define SLIST_REMOVE_HEAD(head, field) do { \ + SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \ } while (0) /* * Singly-linked Tail queue declarations. */ -#define STAILQ_HEAD(name, type) \ -struct name { \ - struct type *stqh_first;/* first element */ \ - struct type **stqh_last;/* addr of last next element */ \ +#define STAILQ_HEAD(name, type) \ +struct name { \ + struct type *stqh_first;/* first element */ \ + struct type **stqh_last;/* addr of last next element */ \ } -#define STAILQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).stqh_first } +#define STAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).stqh_first } -#define STAILQ_ENTRY(type) \ -struct { \ - struct type *stqe_next; /* next element */ \ +#define STAILQ_ENTRY(type) \ +struct { \ + struct type *stqe_next; /* next element */ \ } /* * Singly-linked Tail queue functions. */ -#define STAILQ_CONCAT(head1, head2) do { \ - if (!STAILQ_EMPTY((head2))) { \ - *(head1)->stqh_last = (head2)->stqh_first; \ - (head1)->stqh_last = (head2)->stqh_last; \ - STAILQ_INIT((head2)); \ - } \ +#define STAILQ_CONCAT(head1, head2) do { \ + if (!STAILQ_EMPTY((head2))) { \ + *(head1)->stqh_last = (head2)->stqh_first; \ + (head1)->stqh_last = (head2)->stqh_last; \ + STAILQ_INIT((head2)); \ + } \ } while (0) -#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) +#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL) -#define STAILQ_FIRST(head) ((head)->stqh_first) +#define STAILQ_FIRST(head) ((head)->stqh_first) -#define STAILQ_FOREACH(var, head, field) \ - for((var) = STAILQ_FIRST((head)); \ - (var); \ - (var) = STAILQ_NEXT((var), field)) +#define STAILQ_FOREACH(var, head, field) \ + for((var) = STAILQ_FIRST((head)); \ + (var); \ + (var) = STAILQ_NEXT((var), field)) -#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = STAILQ_FIRST((head)); \ - (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) +#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = STAILQ_FIRST((head)); \ + (var) && ((tvar) = STAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) -#define STAILQ_INIT(head) do { \ - STAILQ_FIRST((head)) = NULL; \ - (head)->stqh_last = &STAILQ_FIRST((head)); \ +#define STAILQ_INIT(head) do { \ + STAILQ_FIRST((head)) = NULL; \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ } while (0) -#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ - if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ - (head)->stqh_last = &STAILQ_NEXT((elm), field); \ - STAILQ_NEXT((tqelm), field) = (elm); \ +#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ + STAILQ_NEXT((tqelm), field) = (elm); \ } while (0) -#define STAILQ_INSERT_HEAD(head, elm, field) do { \ - if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ - (head)->stqh_last = &STAILQ_NEXT((elm), field); \ - STAILQ_FIRST((head)) = (elm); \ +#define STAILQ_INSERT_HEAD(head, elm, field) do { \ + if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ + STAILQ_FIRST((head)) = (elm); \ } while (0) -#define STAILQ_INSERT_TAIL(head, elm, field) do { \ - STAILQ_NEXT((elm), field) = NULL; \ - *(head)->stqh_last = (elm); \ - (head)->stqh_last = &STAILQ_NEXT((elm), field); \ +#define STAILQ_INSERT_TAIL(head, elm, field) do { \ + STAILQ_NEXT((elm), field) = NULL; \ + *(head)->stqh_last = (elm); \ + (head)->stqh_last = &STAILQ_NEXT((elm), field); \ } while (0) -#define STAILQ_LAST(head, type, field) \ - (STAILQ_EMPTY((head)) ? \ - NULL : \ - ((struct type *) \ - ((char *)((head)->stqh_last) - offsetof(struct type, field)))) - -#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) - -#define STAILQ_REMOVE(head, elm, type, field) do { \ - if (STAILQ_FIRST((head)) == (elm)) { \ - STAILQ_REMOVE_HEAD((head), field); \ - } \ - else { \ - struct type *curelm = STAILQ_FIRST((head)); \ - while (STAILQ_NEXT(curelm, field) != (elm)) \ - curelm = STAILQ_NEXT(curelm, field); \ - if ((STAILQ_NEXT(curelm, field) = \ - STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\ - (head)->stqh_last = &STAILQ_NEXT((curelm), field);\ - } \ +#define STAILQ_LAST(head, type, field) \ + (STAILQ_EMPTY((head)) ? \ + NULL : \ + ((struct type *) \ + ((char *)((head)->stqh_last) - offsetof(struct type, field)))) + +#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next) + +#define STAILQ_REMOVE(head, elm, type, field) do { \ + if (STAILQ_FIRST((head)) == (elm)) { \ + STAILQ_REMOVE_HEAD((head), field); \ + } \ + else { \ + struct type *curelm = STAILQ_FIRST((head)); \ + while (STAILQ_NEXT(curelm, field) != (elm)) \ + curelm = STAILQ_NEXT(curelm, field); \ + if ((STAILQ_NEXT(curelm, field) = \ + STAILQ_NEXT(STAILQ_NEXT(curelm, field), field)) == NULL)\ + (head)->stqh_last = &STAILQ_NEXT((curelm), field);\ + } \ } while (0) -#define STAILQ_REMOVE_HEAD(head, field) do { \ - if ((STAILQ_FIRST((head)) = \ - STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ - (head)->stqh_last = &STAILQ_FIRST((head)); \ +#define STAILQ_REMOVE_HEAD(head, field) do { \ + if ((STAILQ_FIRST((head)) = \ + STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ } while (0) -#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ - if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ - (head)->stqh_last = &STAILQ_FIRST((head)); \ +#define STAILQ_REMOVE_HEAD_UNTIL(head, elm, field) do { \ + if ((STAILQ_FIRST((head)) = STAILQ_NEXT((elm), field)) == NULL) \ + (head)->stqh_last = &STAILQ_FIRST((head)); \ } while (0) /* * List declarations. */ -#define LIST_HEAD(name, type) \ -struct name { \ - struct type *lh_first; /* first element */ \ +#define LIST_HEAD(name, type) \ +struct name { \ + struct type *lh_first; /* first element */ \ } -#define LIST_HEAD_INITIALIZER(head) \ - { NULL } +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } -#define LIST_ENTRY(type) \ -struct { \ - struct type *le_next; /* next element */ \ - struct type **le_prev; /* address of previous next element */ \ +#define LIST_ENTRY(type) \ +struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ } /* * List functions. */ -#define LIST_EMPTY(head) ((head)->lh_first == NULL) +#define LIST_EMPTY(head) ((head)->lh_first == NULL) -#define LIST_FIRST(head) ((head)->lh_first) +#define LIST_FIRST(head) ((head)->lh_first) -#define LIST_FOREACH(var, head, field) \ - for ((var) = LIST_FIRST((head)); \ - (var); \ - (var) = LIST_NEXT((var), field)) +#define LIST_FOREACH(var, head, field) \ + for ((var) = LIST_FIRST((head)); \ + (var); \ + (var) = LIST_NEXT((var), field)) -#define LIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = LIST_FIRST((head)); \ - (var) && ((tvar) = LIST_NEXT((var), field), 1); \ - (var) = (tvar)) +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) -#define LIST_INIT(head) do { \ - LIST_FIRST((head)) = NULL; \ +#define LIST_INIT(head) do { \ + LIST_FIRST((head)) = NULL; \ } while (0) -#define LIST_INSERT_AFTER(listelm, elm, field) do { \ - if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ - LIST_NEXT((listelm), field)->field.le_prev = \ - &LIST_NEXT((elm), field); \ - LIST_NEXT((listelm), field) = (elm); \ - (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ +#define LIST_INSERT_AFTER(listelm, elm, field) do { \ + if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\ + LIST_NEXT((listelm), field)->field.le_prev = \ + &LIST_NEXT((elm), field); \ + LIST_NEXT((listelm), field) = (elm); \ + (elm)->field.le_prev = &LIST_NEXT((listelm), field); \ } while (0) -#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ - (elm)->field.le_prev = (listelm)->field.le_prev; \ - LIST_NEXT((elm), field) = (listelm); \ - *(listelm)->field.le_prev = (elm); \ - (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ +#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + LIST_NEXT((elm), field) = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &LIST_NEXT((elm), field); \ } while (0) -#define LIST_INSERT_HEAD(head, elm, field) do { \ - if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ - LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ - LIST_FIRST((head)) = (elm); \ - (elm)->field.le_prev = &LIST_FIRST((head)); \ +#define LIST_INSERT_HEAD(head, elm, field) do { \ + if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \ + LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\ + LIST_FIRST((head)) = (elm); \ + (elm)->field.le_prev = &LIST_FIRST((head)); \ } while (0) -#define LIST_NEXT(elm, field) ((elm)->field.le_next) +#define LIST_NEXT(elm, field) ((elm)->field.le_next) -#define LIST_REMOVE(elm, field) do { \ - if (LIST_NEXT((elm), field) != NULL) \ - LIST_NEXT((elm), field)->field.le_prev = \ - (elm)->field.le_prev; \ - *(elm)->field.le_prev = LIST_NEXT((elm), field); \ +#define LIST_REMOVE(elm, field) do { \ + if (LIST_NEXT((elm), field) != NULL) \ + LIST_NEXT((elm), field)->field.le_prev = \ + (elm)->field.le_prev; \ + *(elm)->field.le_prev = LIST_NEXT((elm), field); \ } while (0) /* * Tail queue declarations. */ -#define TAILQ_HEAD(name, type) \ -struct name { \ - struct type *tqh_first; /* first element */ \ - struct type **tqh_last; /* addr of last next element */ \ +#define TAILQ_HEAD(name, type) \ +struct name { \ + struct type *tqh_first; /* first element */ \ + struct type **tqh_last; /* addr of last next element */ \ } -#define TAILQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).tqh_first } +#define TAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).tqh_first } -#define TAILQ_ENTRY(type) \ -struct { \ - struct type *tqe_next; /* next element */ \ - struct type **tqe_prev; /* address of previous next element */ \ +#define TAILQ_ENTRY(type) \ +struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ } /* * Tail queue functions. */ -#define TAILQ_CONCAT(head1, head2, field) do { \ - if (!TAILQ_EMPTY(head2)) { \ - *(head1)->tqh_last = (head2)->tqh_first; \ - (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ - (head1)->tqh_last = (head2)->tqh_last; \ - TAILQ_INIT((head2)); \ - } \ +#define TAILQ_CONCAT(head1, head2, field) do { \ + if (!TAILQ_EMPTY(head2)) { \ + *(head1)->tqh_last = (head2)->tqh_first; \ + (head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \ + (head1)->tqh_last = (head2)->tqh_last; \ + TAILQ_INIT((head2)); \ + } \ } while (0) -#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) +#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL) -#define TAILQ_FIRST(head) ((head)->tqh_first) +#define TAILQ_FIRST(head) ((head)->tqh_first) -#define TAILQ_FOREACH(var, head, field) \ - for ((var) = TAILQ_FIRST((head)); \ - (var); \ - (var) = TAILQ_NEXT((var), field)) +#define TAILQ_FOREACH(var, head, field) \ + for ((var) = TAILQ_FIRST((head)); \ + (var); \ + (var) = TAILQ_NEXT((var), field)) -#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = TAILQ_FIRST((head)); \ - (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ - (var) = (tvar)) +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = TAILQ_FIRST((head)); \ + (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \ + (var) = (tvar)) -#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ - for ((var) = TAILQ_LAST((head), headname); \ - (var); \ - (var) = TAILQ_PREV((var), headname, field)) +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var); \ + (var) = TAILQ_PREV((var), headname, field)) -#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ - for ((var) = TAILQ_LAST((head), headname); \ - (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ - (var) = (tvar)) +#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ + for ((var) = TAILQ_LAST((head), headname); \ + (var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \ + (var) = (tvar)) -#define TAILQ_INIT(head) do { \ - TAILQ_FIRST((head)) = NULL; \ - (head)->tqh_last = &TAILQ_FIRST((head)); \ +#define TAILQ_INIT(head) do { \ + TAILQ_FIRST((head)) = NULL; \ + (head)->tqh_last = &TAILQ_FIRST((head)); \ } while (0) -#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ - if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ - TAILQ_NEXT((elm), field)->field.tqe_prev = \ - &TAILQ_NEXT((elm), field); \ - else { \ - (head)->tqh_last = &TAILQ_NEXT((elm), field); \ - } \ - TAILQ_NEXT((listelm), field) = (elm); \ - (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ + if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); \ + else { \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + } \ + TAILQ_NEXT((listelm), field) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \ } while (0) -#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ - (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ - TAILQ_NEXT((elm), field) = (listelm); \ - *(listelm)->field.tqe_prev = (elm); \ - (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ +#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + TAILQ_NEXT((elm), field) = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \ } while (0) -#define TAILQ_INSERT_HEAD(head, elm, field) do { \ - if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ - TAILQ_FIRST((head))->field.tqe_prev = \ - &TAILQ_NEXT((elm), field); \ - else \ - (head)->tqh_last = &TAILQ_NEXT((elm), field); \ - TAILQ_FIRST((head)) = (elm); \ - (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ +#define TAILQ_INSERT_HEAD(head, elm, field) do { \ + if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \ + TAILQ_FIRST((head))->field.tqe_prev = \ + &TAILQ_NEXT((elm), field); \ + else \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ + TAILQ_FIRST((head)) = (elm); \ + (elm)->field.tqe_prev = &TAILQ_FIRST((head)); \ } while (0) -#define TAILQ_INSERT_TAIL(head, elm, field) do { \ - TAILQ_NEXT((elm), field) = NULL; \ - (elm)->field.tqe_prev = (head)->tqh_last; \ - *(head)->tqh_last = (elm); \ - (head)->tqh_last = &TAILQ_NEXT((elm), field); \ +#define TAILQ_INSERT_TAIL(head, elm, field) do { \ + TAILQ_NEXT((elm), field) = NULL; \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &TAILQ_NEXT((elm), field); \ } while (0) -#define TAILQ_LAST(head, headname) \ - (*(((struct headname *)((head)->tqh_last))->tqh_last)) +#define TAILQ_LAST(head, headname) \ + (*(((struct headname *)((head)->tqh_last))->tqh_last)) -#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) -#define TAILQ_PREV(elm, headname, field) \ - (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) +#define TAILQ_PREV(elm, headname, field) \ + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) -#define TAILQ_REMOVE(head, elm, field) do { \ - if ((TAILQ_NEXT((elm), field)) != NULL) \ - TAILQ_NEXT((elm), field)->field.tqe_prev = \ - (elm)->field.tqe_prev; \ - else { \ - (head)->tqh_last = (elm)->field.tqe_prev; \ - } \ - *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ +#define TAILQ_REMOVE(head, elm, field) do { \ + if ((TAILQ_NEXT((elm), field)) != NULL) \ + TAILQ_NEXT((elm), field)->field.tqe_prev = \ + (elm)->field.tqe_prev; \ + else { \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + } \ + *(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \ } while (0) #endif /* !SYS_QUEUE_H */ diff --git a/libyasm/coretype.h b/libyasm/coretype.h index b07d8d68..c54c046d 100644 --- a/libyasm/coretype.h +++ b/libyasm/coretype.h @@ -57,14 +57,14 @@ typedef struct yasm_dbgfmt_module yasm_dbgfmt_module; */ typedef struct yasm_assoc_data_callback { /** Free memory allocated for associated data. - * \param data associated data + * \param data associated data */ void (*destroy) (/*@only@*/ void *data); /** Print a description of allocated data. For debugging purposes. - * \param data associated data - * \param f output file - * \param indent_level indentation level + * \param data associated data + * \param f output file + * \param indent_level indentation level */ void (*print) (void *data, FILE *f, int indent_level); } yasm_assoc_data_callback; @@ -172,7 +172,7 @@ typedef struct yasm_value { } yasm_value; /** Maximum value of #yasm_value.rshift */ -#define YASM_VALUE_RSHIFT_MAX 127 +#define YASM_VALUE_RSHIFT_MAX 127 /** Line number mapping repository (opaque type). \see linemap.h for related * functions. @@ -200,40 +200,40 @@ typedef struct yasm_insn_operands yasm_insn_operands; /** Expression operators usable in #yasm_expr expressions. */ typedef enum yasm_expr_op { - YASM_EXPR_IDENT, /**< No operation, just a value. */ - YASM_EXPR_ADD, /**< Arithmetic addition (+). */ - YASM_EXPR_SUB, /**< Arithmetic subtraction (-). */ - YASM_EXPR_MUL, /**< Arithmetic multiplication (*). */ - YASM_EXPR_DIV, /**< Arithmetic unsigned division. */ - YASM_EXPR_SIGNDIV, /**< Arithmetic signed division. */ - YASM_EXPR_MOD, /**< Arithmetic unsigned modulus. */ - YASM_EXPR_SIGNMOD, /**< Arithmetic signed modulus. */ - YASM_EXPR_NEG, /**< Arithmetic negation (-). */ - YASM_EXPR_NOT, /**< Bitwise negation. */ - YASM_EXPR_OR, /**< Bitwise OR. */ - YASM_EXPR_AND, /**< Bitwise AND. */ - YASM_EXPR_XOR, /**< Bitwise XOR. */ - YASM_EXPR_XNOR, /**< Bitwise XNOR. */ - YASM_EXPR_NOR, /**< Bitwise NOR. */ - YASM_EXPR_SHL, /**< Shift left (logical). */ - YASM_EXPR_SHR, /**< Shift right (logical). */ - YASM_EXPR_LOR, /**< Logical OR. */ - YASM_EXPR_LAND, /**< Logical AND. */ - YASM_EXPR_LNOT, /**< Logical negation. */ - YASM_EXPR_LXOR, /**< Logical XOR. */ - YASM_EXPR_LXNOR, /**< Logical XNOR. */ - YASM_EXPR_LNOR, /**< Logical NOR. */ - YASM_EXPR_LT, /**< Less than comparison. */ - YASM_EXPR_GT, /**< Greater than comparison. */ - YASM_EXPR_EQ, /**< Equality comparison. */ - YASM_EXPR_LE, /**< Less than or equal to comparison. */ - YASM_EXPR_GE, /**< Greater than or equal to comparison. */ - YASM_EXPR_NE, /**< Not equal comparison. */ - YASM_EXPR_NONNUM, /**< Start of non-numeric operations (not an op). */ - YASM_EXPR_SEG, /**< SEG operator (gets segment portion of address). */ - YASM_EXPR_WRT, /**< WRT operator (gets offset of address relative to - * some other segment). */ - YASM_EXPR_SEGOFF /**< The ':' in segment:offset. */ + YASM_EXPR_IDENT, /**< No operation, just a value. */ + YASM_EXPR_ADD, /**< Arithmetic addition (+). */ + YASM_EXPR_SUB, /**< Arithmetic subtraction (-). */ + YASM_EXPR_MUL, /**< Arithmetic multiplication (*). */ + YASM_EXPR_DIV, /**< Arithmetic unsigned division. */ + YASM_EXPR_SIGNDIV, /**< Arithmetic signed division. */ + YASM_EXPR_MOD, /**< Arithmetic unsigned modulus. */ + YASM_EXPR_SIGNMOD, /**< Arithmetic signed modulus. */ + YASM_EXPR_NEG, /**< Arithmetic negation (-). */ + YASM_EXPR_NOT, /**< Bitwise negation. */ + YASM_EXPR_OR, /**< Bitwise OR. */ + YASM_EXPR_AND, /**< Bitwise AND. */ + YASM_EXPR_XOR, /**< Bitwise XOR. */ + YASM_EXPR_XNOR, /**< Bitwise XNOR. */ + YASM_EXPR_NOR, /**< Bitwise NOR. */ + YASM_EXPR_SHL, /**< Shift left (logical). */ + YASM_EXPR_SHR, /**< Shift right (logical). */ + YASM_EXPR_LOR, /**< Logical OR. */ + YASM_EXPR_LAND, /**< Logical AND. */ + YASM_EXPR_LNOT, /**< Logical negation. */ + YASM_EXPR_LXOR, /**< Logical XOR. */ + YASM_EXPR_LXNOR, /**< Logical XNOR. */ + YASM_EXPR_LNOR, /**< Logical NOR. */ + YASM_EXPR_LT, /**< Less than comparison. */ + YASM_EXPR_GT, /**< Greater than comparison. */ + YASM_EXPR_EQ, /**< Equality comparison. */ + YASM_EXPR_LE, /**< Less than or equal to comparison. */ + YASM_EXPR_GE, /**< Greater than or equal to comparison. */ + YASM_EXPR_NE, /**< Not equal comparison. */ + YASM_EXPR_NONNUM, /**< Start of non-numeric operations (not an op). */ + YASM_EXPR_SEG, /**< SEG operator (gets segment portion of address). */ + YASM_EXPR_WRT, /**< WRT operator (gets offset of address relative to + * some other segment). */ + YASM_EXPR_SEGOFF /**< The ':' in segment:offset. */ } yasm_expr_op; /** Convert yasm_value to its byte representation. Usually implemented by @@ -241,19 +241,19 @@ typedef enum yasm_expr_op { * Must put the value into the least significant bits of the destination, * unless shifted into more significant bits by the shift parameter. The * destination bits must be cleared before being set. - * \param value value - * \param buf buffer for byte representation - * \param destsize destination size (in bytes) - * \param offset offset (in bytes) of the expr contents from the start - * of the bytecode (needed for relative) - * \param bc current bytecode (usually passed into higher-level - * calling function) - * \param warn enables standard warnings: zero for none; - * nonzero for overflow/underflow floating point warnings; - * negative for signed integer warnings, - * positive for unsigned integer warnings - * \param d objfmt-specific data (passed into higher-level calling - * function) + * \param value value + * \param buf buffer for byte representation + * \param destsize destination size (in bytes) + * \param offset offset (in bytes) of the expr contents from the start + * of the bytecode (needed for relative) + * \param bc current bytecode (usually passed into higher-level + * calling function) + * \param warn enables standard warnings: zero for none; + * nonzero for overflow/underflow floating point warnings; + * negative for signed integer warnings, + * positive for unsigned integer warnings + * \param d objfmt-specific data (passed into higher-level calling + * function) * \return Nonzero if an error occurred, 0 otherwise. */ typedef int (*yasm_output_value_func) @@ -263,18 +263,18 @@ typedef int (*yasm_output_value_func) /** Convert a symbol reference to its byte representation. Usually implemented * by object formats and debug formats to keep track of relocations generated * by themselves. - * \param sym symbol - * \param bc current bytecode (usually passed into higher-level - * calling function) - * \param buf buffer for byte representation - * \param destsize destination size (in bytes) - * \param valsize size (in bits) - * \param warn enables standard warnings: zero for none; - * nonzero for overflow/underflow floating point warnings; - * negative for signed integer warnings, - * positive for unsigned integer warnings - * \param d objfmt-specific data (passed into higher-level calling - * function) + * \param sym symbol + * \param bc current bytecode (usually passed into higher-level + * calling function) + * \param buf buffer for byte representation + * \param destsize destination size (in bytes) + * \param valsize size (in bits) + * \param warn enables standard warnings: zero for none; + * nonzero for overflow/underflow floating point warnings; + * negative for signed integer warnings, + * positive for unsigned integer warnings + * \param d objfmt-specific data (passed into higher-level calling + * function) * \return Nonzero if an error occurred, 0 otherwise. */ typedef int (*yasm_output_reloc_func) @@ -283,50 +283,50 @@ typedef int (*yasm_output_reloc_func) /** Sort an array using merge sort algorithm. * \internal - * \param base base of array - * \param nmemb number of elements in array - * \param size size of each array element + * \param base base of array + * \param nmemb number of elements in array + * \param size size of each array element * \param compar element comparison function */ int yasm__mergesort(void *base, size_t nmemb, size_t size, - int (*compar)(const void *, const void *)); + int (*compar)(const void *, const void *)); /** Separate string by delimiters. * \internal * \param stringp string - * \param delim set of 1 or more delimiters + * \param delim set of 1 or more delimiters * \return First/next substring. */ /*@null@*/ char *yasm__strsep(char **stringp, const char *delim); /** Compare two strings, ignoring case differences. * \internal - * \param s1 string 1 - * \param s2 string 2 + * \param s1 string 1 + * \param s2 string 2 * \return 0 if strings are equal, -1 if s1s2. */ int yasm__strcasecmp(const char *s1, const char *s2); /** Compare portion of two strings, ignoring case differences. * \internal - * \param s1 string 1 - * \param s2 string 2 - * \param n maximum number of characters to compare + * \param s1 string 1 + * \param s2 string 2 + * \param n maximum number of characters to compare * \return 0 if strings are equal, -1 if s1s2. */ int yasm__strncasecmp(const char *s1, const char *s2, size_t n); /** strdup() implementation using yasm_xmalloc(). * \internal - * \param str string + * \param str string * \return Newly allocated duplicate string. */ /*@only@*/ char *yasm__xstrdup(const char *str); /** strndup() implementation using yasm_xmalloc(). * \internal - * \param str string - * \param len maximum number of characters to copy + * \param str string + * \param len maximum number of characters to copy * \return Newly allocated duplicate string. */ /*@only@*/ char *yasm__xstrndup(const char *str, size_t len); @@ -334,7 +334,7 @@ int yasm__strncasecmp(const char *s1, const char *s2, size_t n); /** Error-checking memory allocation. A default implementation is provided * that calls yasm_fatal() on allocation errors. * A replacement should \em never return NULL. - * \param size number of bytes to allocate + * \param size number of bytes to allocate * \return Allocated memory block. */ extern /*@only@*/ /*@out@*/ void * (*yasm_xmalloc) (size_t size); @@ -342,7 +342,7 @@ extern /*@only@*/ /*@out@*/ void * (*yasm_xmalloc) (size_t size); /** Error-checking memory allocation (with clear-to-0). A default * implementation is provided that calls yasm_fatal() on allocation errors. * A replacement should \em never return NULL. - * \param size number of elements to allocate + * \param size number of elements to allocate * \param elsize size (in bytes) of each element * \return Allocated and cleared memory block. */ @@ -361,7 +361,7 @@ extern /*@only@*/ void * (*yasm_xrealloc) /** Error-checking memory deallocation. A default implementation is provided * that calls yasm_fatal() on allocation errors. - * \param p memory block to free + * \param p memory block to free */ extern void (*yasm_xfree) (/*@only@*/ /*@out@*/ /*@null@*/ void *p) /*@modifies p@*/; diff --git a/libyasm/dbgfmt.h b/libyasm/dbgfmt.h index 021127e7..f0cdfff7 100644 --- a/libyasm/dbgfmt.h +++ b/libyasm/dbgfmt.h @@ -58,7 +58,7 @@ struct yasm_dbgfmt_module { /** Create debug format. * Module-level implementation of yasm_dbgfmt_create(). * The filenames are provided solely for informational purposes. - * \param object object + * \param object object * \return NULL if object format does not provide needed support. */ /*@null@*/ /*@only@*/ yasm_dbgfmt * (*create) (yasm_object *object); @@ -72,7 +72,7 @@ struct yasm_dbgfmt_module { * Call yasm_dbgfmt_generate() instead of calling this function. */ void (*generate) (yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns); + yasm_errwarns *errwarns); }; /** Get the keyword used to select a debug format. @@ -84,26 +84,26 @@ const char *yasm_dbgfmt_keyword(const yasm_dbgfmt *dbgfmt); /** Initialize debug output for use. Must call before any other debug * format functions. The filenames are provided solely for informational * purposes. - * \param module debug format module - * \param object object to generate debugging information for + * \param module debug format module + * \param object object to generate debugging information for * \return NULL if object format does not provide needed support. */ /*@null@*/ /*@only@*/ yasm_dbgfmt *yasm_dbgfmt_create (const yasm_dbgfmt_module *module, yasm_object *object); /** Cleans up any allocated debug format memory. - * \param dbgfmt debug format + * \param dbgfmt debug format */ void yasm_dbgfmt_destroy(/*@only@*/ yasm_dbgfmt *dbgfmt); /** Generate debugging information bytecodes. - * \param object object - * \param linemap virtual/physical line mapping - * \param errwarns error/warning set + * \param object object + * \param linemap virtual/physical line mapping + * \param errwarns error/warning set * \note Errors and warnings are stored into errwarns. */ void yasm_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns); + yasm_errwarns *errwarns); #ifndef YASM_DOXYGEN @@ -119,7 +119,7 @@ void yasm_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, ((yasm_dbgfmt_base *)dbgfmt)->module->destroy(dbgfmt) #define yasm_dbgfmt_generate(object, linemap, ews) \ ((yasm_dbgfmt_base *)((object)->dbgfmt))->module->generate \ - (object, linemap, ews) + (object, linemap, ews) #endif diff --git a/libyasm/errwarn.c b/libyasm/errwarn.c index 17fef16b..04479b60 100644 --- a/libyasm/errwarn.c +++ b/libyasm/errwarn.c @@ -40,7 +40,7 @@ #include "errwarn.h" -#define MSG_MAXSIZE 1024 +#define MSG_MAXSIZE 1024 /* Default handlers for replacable functions */ static /*@exits@*/ void def_internal_error_ @@ -113,9 +113,9 @@ yasm_errwarn_initialize(void) { /* Default enabled warnings. See errwarn.h for a list. */ warn_class_enabled = - (1UL<previous_we; first = SLIST_FIRST(&errwarns->errwarns); if (!ins_we || !first) - action = INS_HEAD; + action = INS_HEAD; while (action == INS_NONE) { - next = SLIST_NEXT(ins_we, link); - if (line < ins_we->line) { - if (ins_we == first) - action = INS_HEAD; - else - ins_we = first; - } else if (!next) - action = INS_AFTER; - else if (line >= ins_we->line && line < next->line) - action = INS_AFTER; - else - ins_we = next; + next = SLIST_NEXT(ins_we, link); + if (line < ins_we->line) { + if (ins_we == first) + action = INS_HEAD; + else + ins_we = first; + } else if (!next) + action = INS_AFTER; + else if (line >= ins_we->line && line < next->line) + action = INS_AFTER; + else + ins_we = next; } if (replace_parser_error && ins_we && ins_we->type == WE_PARSERERROR) { - /* overwrite last error */ - we = ins_we; + /* overwrite last error */ + we = ins_we; } else { - /* add a new error */ - we = yasm_xmalloc(sizeof(errwarn_data)); - - we->type = WE_UNKNOWN; - we->line = line; - we->xrefline = 0; - we->msg = NULL; - we->xrefmsg = NULL; - - if (action == INS_HEAD) - SLIST_INSERT_HEAD(&errwarns->errwarns, we, link); - else if (action == INS_AFTER) { - assert(ins_we != NULL); - SLIST_INSERT_AFTER(ins_we, we, link); - } else - yasm_internal_error(N_("Unexpected errwarn insert action")); + /* add a new error */ + we = yasm_xmalloc(sizeof(errwarn_data)); + + we->type = WE_UNKNOWN; + we->line = line; + we->xrefline = 0; + we->msg = NULL; + we->xrefmsg = NULL; + + if (action == INS_HEAD) + SLIST_INSERT_HEAD(&errwarns->errwarns, we, link); + else if (action == INS_AFTER) { + assert(ins_we != NULL); + SLIST_INSERT_AFTER(ins_we, we, link); + } else + yasm_internal_error(N_("Unexpected errwarn insert action")); } /* Remember previous err/warn */ @@ -248,9 +248,9 @@ void yasm_error_clear(void) { if (yasm_estr) - yasm_xfree(yasm_estr); + yasm_xfree(yasm_estr); if (yasm_exrefstr) - yasm_xfree(yasm_exrefstr); + yasm_xfree(yasm_exrefstr); yasm_eclass = YASM_ERROR_NONE; yasm_estr = NULL; yasm_exrefline = 0; @@ -261,9 +261,9 @@ int yasm_error_matches(yasm_error_class eclass) { if (yasm_eclass == YASM_ERROR_NONE) - return eclass == YASM_ERROR_NONE; + return eclass == YASM_ERROR_NONE; if (yasm_eclass == YASM_ERROR_GENERAL) - return eclass == YASM_ERROR_GENERAL; + return eclass == YASM_ERROR_GENERAL; return (yasm_eclass & eclass) == eclass; } @@ -271,7 +271,7 @@ void yasm_error_set_va(yasm_error_class eclass, const char *format, va_list va) { if (yasm_eclass != YASM_ERROR_NONE) - return; + return; yasm_eclass = eclass; yasm_estr = yasm_xmalloc(MSG_MAXSIZE+1); @@ -295,7 +295,7 @@ void yasm_error_set_xref_va(unsigned long xrefline, const char *format, va_list va) { if (yasm_eclass != YASM_ERROR_NONE) - return; + return; yasm_exrefline = xrefline; @@ -318,7 +318,7 @@ yasm_error_set_xref(unsigned long xrefline, const char *format, ...) void yasm_error_fetch(yasm_error_class *eclass, char **str, unsigned long *xrefline, - char **xrefstr) + char **xrefstr) { *eclass = yasm_eclass; *str = yasm_estr; @@ -334,13 +334,13 @@ void yasm_warn_clear(void) { /* Delete all error/warnings */ while (!STAILQ_EMPTY(&yasm_warns)) { - warn *w = STAILQ_FIRST(&yasm_warns); + warn *w = STAILQ_FIRST(&yasm_warns); - if (w->wstr) - yasm_xfree(w->wstr); + if (w->wstr) + yasm_xfree(w->wstr); - STAILQ_REMOVE_HEAD(&yasm_warns, link); - yasm_xfree(w); + STAILQ_REMOVE_HEAD(&yasm_warns, link); + yasm_xfree(w); } } @@ -348,7 +348,7 @@ yasm_warn_class yasm_warn_occurred(void) { if (STAILQ_EMPTY(&yasm_warns)) - return YASM_WARN_NONE; + return YASM_WARN_NONE; return STAILQ_FIRST(&yasm_warns)->wclass; } @@ -358,7 +358,7 @@ yasm_warn_set_va(yasm_warn_class wclass, const char *format, va_list va) warn *w; if (!(warn_class_enabled & (1UL<wclass = wclass; @@ -386,9 +386,9 @@ yasm_warn_fetch(yasm_warn_class *wclass, char **str) warn *w = STAILQ_FIRST(&yasm_warns); if (!w) { - *wclass = YASM_WARN_NONE; - *str = NULL; - return; + *wclass = YASM_WARN_NONE; + *str = NULL; + return; } *wclass = w->wclass; @@ -434,14 +434,14 @@ yasm_errwarns_destroy(yasm_errwarns *errwarns) /* Delete all error/warnings */ while (!SLIST_EMPTY(&errwarns->errwarns)) { - we = SLIST_FIRST(&errwarns->errwarns); - if (we->msg) - yasm_xfree(we->msg); - if (we->xrefmsg) - yasm_xfree(we->xrefmsg); - - SLIST_REMOVE_HEAD(&errwarns->errwarns, link); - yasm_xfree(we); + we = SLIST_FIRST(&errwarns->errwarns); + if (we->msg) + yasm_xfree(we->msg); + if (we->xrefmsg) + yasm_xfree(we->xrefmsg); + + SLIST_REMOVE_HEAD(&errwarns->errwarns, link); + yasm_xfree(we); } yasm_xfree(errwarns); @@ -451,25 +451,25 @@ void yasm_errwarn_propagate(yasm_errwarns *errwarns, unsigned long line) { if (yasm_eclass != YASM_ERROR_NONE) { - errwarn_data *we = errwarn_data_new(errwarns, line, 1); - yasm_error_class eclass; - - yasm_error_fetch(&eclass, &we->msg, &we->xrefline, &we->xrefmsg); - if (eclass != YASM_ERROR_GENERAL - && (eclass & YASM_ERROR_PARSE) == YASM_ERROR_PARSE) - we->type = WE_PARSERERROR; - else - we->type = WE_ERROR; - errwarns->ecount++; + errwarn_data *we = errwarn_data_new(errwarns, line, 1); + yasm_error_class eclass; + + yasm_error_fetch(&eclass, &we->msg, &we->xrefline, &we->xrefmsg); + if (eclass != YASM_ERROR_GENERAL + && (eclass & YASM_ERROR_PARSE) == YASM_ERROR_PARSE) + we->type = WE_PARSERERROR; + else + we->type = WE_ERROR; + errwarns->ecount++; } while (!STAILQ_EMPTY(&yasm_warns)) { - errwarn_data *we = errwarn_data_new(errwarns, line, 0); - yasm_warn_class wclass; + errwarn_data *we = errwarn_data_new(errwarns, line, 0); + yasm_warn_class wclass; - yasm_warn_fetch(&wclass, &we->msg); - we->type = WE_WARNING; - errwarns->wcount++; + yasm_warn_fetch(&wclass, &we->msg); + we->type = WE_WARNING; + errwarns->wcount++; } } @@ -477,16 +477,16 @@ unsigned int yasm_errwarns_num_errors(yasm_errwarns *errwarns, int warning_as_error) { if (warning_as_error) - return errwarns->ecount+errwarns->wcount; + return errwarns->ecount+errwarns->wcount; else - return errwarns->ecount; + return errwarns->ecount; } void yasm_errwarns_output_all(yasm_errwarns *errwarns, yasm_linemap *lm, - int warning_as_error, - yasm_print_error_func print_error, - yasm_print_warning_func print_warning) + int warning_as_error, + yasm_print_error_func print_error, + yasm_print_warning_func print_warning) { errwarn_data *we; const char *filename, *xref_filename; @@ -494,27 +494,27 @@ yasm_errwarns_output_all(yasm_errwarns *errwarns, yasm_linemap *lm, /* If we're treating warnings as errors, tell the user about it. */ if (warning_as_error && warning_as_error != 2) { - print_error("", 0, - yasm_gettext_hook(N_("warnings being treated as errors")), - NULL, 0, NULL); - warning_as_error = 2; + print_error("", 0, + yasm_gettext_hook(N_("warnings being treated as errors")), + NULL, 0, NULL); + warning_as_error = 2; } /* Output error/warnings. */ SLIST_FOREACH(we, &errwarns->errwarns, link) { - /* Output error/warning */ - yasm_linemap_lookup(lm, we->line, &filename, &line); - if (we->xrefline) - yasm_linemap_lookup(lm, we->xrefline, &xref_filename, &xref_line); - else { - xref_filename = NULL; - xref_line = 0; - } - if (we->type == WE_ERROR || we->type == WE_PARSERERROR) - print_error(filename, line, we->msg, xref_filename, xref_line, - we->xrefmsg); - else - print_warning(filename, line, we->msg); + /* Output error/warning */ + yasm_linemap_lookup(lm, we->line, &filename, &line); + if (we->xrefline) + yasm_linemap_lookup(lm, we->xrefline, &xref_filename, &xref_line); + else { + xref_filename = NULL; + xref_line = 0; + } + if (we->type == WE_ERROR || we->type == WE_PARSERERROR) + print_error(filename, line, we->msg, xref_filename, xref_line, + we->xrefmsg); + else + print_warning(filename, line, we->msg); } } diff --git a/libyasm/errwarn.h b/libyasm/errwarn.h index 64b73b59..b0a1aae2 100644 --- a/libyasm/errwarn.h +++ b/libyasm/errwarn.h @@ -36,10 +36,10 @@ /** Warning classes (that may be enabled/disabled). */ typedef enum yasm_warn_class { - YASM_WARN_NONE = 0, /**< No warning */ - YASM_WARN_GENERAL, /**< Non-specific warnings */ + YASM_WARN_NONE = 0, /**< No warning */ + YASM_WARN_GENERAL, /**< Non-specific warnings */ YASM_WARN_UNREC_CHAR, /**< Unrecognized characters (while tokenizing) */ - YASM_WARN_PREPROC, /**< Preprocessor warnings */ + YASM_WARN_PREPROC, /**< Preprocessor warnings */ YASM_WARN_ORPHAN_LABEL, /**< Label alone on a line without a colon */ YASM_WARN_UNINIT_CONTENTS, /**< Uninitialized space in code/data section */ YASM_WARN_SIZE_OVERRIDE /**< Double size override */ @@ -47,23 +47,23 @@ typedef enum yasm_warn_class { /** Error classes. Bitmask-based to support limited subclassing. */ typedef enum yasm_error_class { - YASM_ERROR_NONE = 0x0000, /**< No error */ - YASM_ERROR_GENERAL = 0xFFFF, /**< Non-specific */ - YASM_ERROR_ARITHMETIC = 0x0001, /**< Arithmetic error (general) */ - YASM_ERROR_OVERFLOW = 0x8001, /**< Arithmetic overflow */ - YASM_ERROR_FLOATING_POINT = 0x4001, /**< Floating point error */ - YASM_ERROR_ZERO_DIVISION = 0x2001, /**< Divide-by-zero */ - YASM_ERROR_ASSERTION = 0x0002, /**< Assertion error */ - YASM_ERROR_VALUE = 0x0004, /**< Value inappropriate - * (e.g. not in range) */ - YASM_ERROR_NOT_ABSOLUTE = 0x8004, /**< Absolute expression required */ - YASM_ERROR_TOO_COMPLEX = 0x4004, /**< Expression too complex */ - YASM_ERROR_NOT_CONSTANT = 0x2004, /**< Constant expression required */ - YASM_ERROR_IO = 0x0008, /**< I/O error */ - YASM_ERROR_NOT_IMPLEMENTED = 0x0010, /**< Not implemented error */ - YASM_ERROR_TYPE = 0x0020, /**< Type error */ - YASM_ERROR_SYNTAX = 0x0040, /**< Syntax error */ - YASM_ERROR_PARSE = 0x8040 /**< Parser error */ + YASM_ERROR_NONE = 0x0000, /**< No error */ + YASM_ERROR_GENERAL = 0xFFFF, /**< Non-specific */ + YASM_ERROR_ARITHMETIC = 0x0001, /**< Arithmetic error (general) */ + YASM_ERROR_OVERFLOW = 0x8001, /**< Arithmetic overflow */ + YASM_ERROR_FLOATING_POINT = 0x4001, /**< Floating point error */ + YASM_ERROR_ZERO_DIVISION = 0x2001, /**< Divide-by-zero */ + YASM_ERROR_ASSERTION = 0x0002, /**< Assertion error */ + YASM_ERROR_VALUE = 0x0004, /**< Value inappropriate + * (e.g. not in range) */ + YASM_ERROR_NOT_ABSOLUTE = 0x8004, /**< Absolute expression required */ + YASM_ERROR_TOO_COMPLEX = 0x4004, /**< Expression too complex */ + YASM_ERROR_NOT_CONSTANT = 0x2004, /**< Constant expression required */ + YASM_ERROR_IO = 0x0008, /**< I/O error */ + YASM_ERROR_NOT_IMPLEMENTED = 0x0010, /**< Not implemented error */ + YASM_ERROR_TYPE = 0x0020, /**< Type error */ + YASM_ERROR_SYNTAX = 0x0040, /**< Syntax error */ + YASM_ERROR_PARSE = 0x8040 /**< Parser error */ } yasm_error_class; /** Initialize any internal data structures. */ @@ -96,7 +96,7 @@ extern /*@exits@*/ void (*yasm_internal_error_) * \warning This function must NOT return to calling code; exit or longjmp * instead. * \param message fatal error message - * \param va va_list argument list for message + * \param va va_list argument list for message */ extern /*@exits@*/ void (*yasm_fatal) (const char *message, va_list va); @@ -104,7 +104,7 @@ extern /*@exits@*/ void (*yasm_fatal) (const char *message, va_list va); * \warning This function calls #yasm_fatal, and thus does not return to the * calling code. * \param message fatal error message - * \param ... argument list for message + * \param ... argument list for message */ /*@exits@*/ void yasm__fatal(const char *message, ...); @@ -131,14 +131,14 @@ int yasm_error_matches(yasm_error_class eclass); #ifndef YASM_DOXYGEN extern yasm_error_class yasm_eclass; -#define yasm_error_occurred() yasm_eclass +#define yasm_error_occurred() yasm_eclass #endif /** Set the error indicator (va_list version). Has no effect if the error * indicator is already set. * \param eclass error class * \param format printf format string - * \param va argument list for format + * \param va argument list for format */ void yasm_error_set_va(yasm_error_class eclass, const char *format, va_list va); @@ -146,7 +146,7 @@ void yasm_error_set_va(yasm_error_class eclass, const char *format, va_list va); * set. * \param eclass error class * \param format printf format string - * \param ... argument list for format + * \param ... argument list for format */ void yasm_error_set(yasm_error_class eclass, const char *format, ...) /*@printflike@*/; @@ -156,17 +156,17 @@ void yasm_error_set(yasm_error_class eclass, const char *format, ...) * function must be called prior to its corresponding yasm_error_set() call. * \param xrefline virtual line to cross-reference to (should not be 0) * \param format printf format string - * \param va argument list for format + * \param va argument list for format */ void yasm_error_set_xref_va(unsigned long xrefline, const char *format, - va_list va); + va_list va); /** Set a cross-reference for a new error. Has no effect if the error * indicator is already set (e.g. with yasm_error_set()). This function * must be called prior to its corresponding yasm_error_set() call. * \param xrefline virtual line to cross-reference to (should not be 0) * \param format printf format string - * \param ... argument list for format + * \param ... argument list for format */ void yasm_error_set_xref(unsigned long xrefline, const char *format, ...) /*@printflike@*/; @@ -179,14 +179,14 @@ void yasm_error_set_xref(unsigned long xrefline, const char *format, ...) * all output values are set to 0 (including eclass, which is set to * YASM_ERROR_NONE). * \param eclass error class (output) - * \param str error message + * \param str error message * \param xrefline virtual line used for cross-referencing (0 if no xref) * \param xrefstr cross-reference error message (NULL if no xref) */ void yasm_error_fetch(/*@out@*/ yasm_error_class *eclass, - /*@out@*/ /*@only@*/ /*@null@*/ char **str, - /*@out@*/ unsigned long *xrefline, - /*@out@*/ /*@only@*/ /*@null@*/ char **xrefstr); + /*@out@*/ /*@only@*/ /*@null@*/ char **str, + /*@out@*/ unsigned long *xrefline, + /*@out@*/ /*@only@*/ /*@null@*/ char **xrefstr); /** Unconditionally clear all warning indicators, freeing any associated data. * Has no effect if no warning indicators have been set. @@ -203,14 +203,14 @@ yasm_warn_class yasm_warn_occurred(void); /** Add a warning indicator (va_list version). * \param wclass warning class * \param format printf format string - * \param va argument list for format + * \param va argument list for format */ void yasm_warn_set_va(yasm_warn_class wclass, const char *format, va_list va); /** Add a warning indicator. * \param wclass warning class * \param format printf format string - * \param ... argument list for format + * \param ... argument list for format */ void yasm_warn_set(yasm_warn_class wclass, const char *format, ...) /*@printflike@*/; @@ -223,10 +223,10 @@ void yasm_warn_set(yasm_warn_class wclass, const char *format, ...) * all output values are set to 0 (including wclass, which is set to * YASM_WARN_NONE). * \param wclass warning class (output) - * \param str warning message + * \param str warning message */ void yasm_warn_fetch(/*@out@*/ yasm_warn_class *wclass, - /*@out@*/ /*@only@*/ char **str); + /*@out@*/ /*@only@*/ char **str); /** Enable a class of warnings. * \param wclass warning class @@ -264,20 +264,20 @@ void yasm_errwarns_destroy(/*@only@*/ yasm_errwarns *errwarns); void yasm_errwarn_propagate(yasm_errwarns *errwarns, unsigned long line); /** Get total number of errors logged. - * \param errwarns error/warning set + * \param errwarns error/warning set * \param warning_as_error if nonzero, warnings are treated as errors. * \return Number of errors. */ unsigned int yasm_errwarns_num_errors(yasm_errwarns *errwarns, - int warning_as_error); + int warning_as_error); /** Print out an error. - * \param fn filename of source file - * \param line line number - * \param msg error message - * \param xref_fn cross-referenced source filename - * \param xref_line cross-referenced line number - * \param xref_msg cross-referenced error message + * \param fn filename of source file + * \param line line number + * \param msg error message + * \param xref_fn cross-referenced source filename + * \param xref_line cross-referenced line number + * \param xref_msg cross-referenced error message */ typedef void (*yasm_print_error_func) (const char *fn, unsigned long line, const char *msg, @@ -285,19 +285,19 @@ typedef void (*yasm_print_error_func) /*@null@*/ const char *xref_msg); /** Print out a warning. - * \param fn filename of source file - * \param line line number - * \param msg warning message + * \param fn filename of source file + * \param line line number + * \param msg warning message */ typedef void (*yasm_print_warning_func) (const char *fn, unsigned long line, const char *msg); /** Outputs error/warning set in sorted order (sorted by virtual line number). - * \param errwarns error/warning set - * \param lm line map (to convert virtual lines into filename/line pairs) + * \param errwarns error/warning set + * \param lm line map (to convert virtual lines into filename/line pairs) * \param warning_as_error if nonzero, treat warnings as errors. - * \param print_error function called to print out errors - * \param print_warning function called to print out warnings + * \param print_error function called to print out errors + * \param print_warning function called to print out warnings */ void yasm_errwarns_output_all (yasm_errwarns *errwarns, yasm_linemap *lm, int warning_as_error, @@ -305,13 +305,13 @@ void yasm_errwarns_output_all /** Convert a possibly unprintable character into a printable string. * \internal - * \param ch possibly unprintable character + * \param ch possibly unprintable character * \return Printable string representation (static buffer). */ char *yasm__conv_unprint(int ch); /** Hook for library users to map to gettext() if GNU gettext is being used. - * \param msgid message catalog identifier + * \param msgid message catalog identifier * \return Translated message. */ extern const char * (*yasm_gettext_hook) (const char *msgid); diff --git a/libyasm/expr-int.h b/libyasm/expr-int.h index c5e0689e..941726d1 100644 --- a/libyasm/expr-int.h +++ b/libyasm/expr-int.h @@ -45,13 +45,13 @@ typedef enum yasm_expr__type { struct yasm_expr__item { yasm_expr__type type; union { - yasm_bytecode *precbc; - yasm_symrec *sym; - yasm_expr *expn; - yasm_intnum *intn; - yasm_floatnum *flt; - uintptr_t reg; - unsigned int subst; + yasm_bytecode *precbc; + yasm_symrec *sym; + yasm_expr *expn; + yasm_intnum *intn; + yasm_floatnum *flt; + uintptr_t reg; + unsigned int subst; } data; }; @@ -62,7 +62,7 @@ struct yasm_expr { yasm_expr_op op; unsigned long line; int numterms; - yasm_expr__item terms[2]; /* structure may be extended to include more */ + yasm_expr__item terms[2]; /* structure may be extended to include more */ }; /* Traverse over expression tree in order, calling func for each leaf @@ -93,26 +93,26 @@ int yasm_expr__contains(const yasm_expr *e, yasm_expr__type t); /** Transform symrec-symrec terms in expression into YASM_EXPR_SUBST items. * Calls the callback function for each symrec-symrec term. - * \param ep expression (pointer to) - * \param cbd callback data passed to callback function - * \param callback callback function: given subst index for bytecode - * pair, bytecode pair (bc2-bc1), and cbd (callback data) + * \param ep expression (pointer to) + * \param cbd callback data passed to callback function + * \param callback callback function: given subst index for bytecode + * pair, bytecode pair (bc2-bc1), and cbd (callback data) * \return Number of transformations made. */ int yasm_expr__bc_dist_subst(yasm_expr **ep, void *cbd, - void (*callback) (unsigned int subst, - yasm_bytecode *precbc, - yasm_bytecode *precbc2, - void *cbd)); + void (*callback) (unsigned int subst, + yasm_bytecode *precbc, + yasm_bytecode *precbc2, + void *cbd)); /** Substitute items into expr YASM_EXPR_SUBST items (by index). Items are * copied, so caller is responsible for freeing array of items. - * \param e expression - * \param num_items number of items in items array - * \param items items array + * \param e expression + * \param num_items number of items in items array + * \param items items array * \return 1 on error (index out of range). */ int yasm_expr__subst(yasm_expr *e, unsigned int num_items, - const yasm_expr__item *items); + const yasm_expr__item *items); #endif diff --git a/libyasm/expr.c b/libyasm/expr.c index 4495b28f..604f2fd6 100644 --- a/libyasm/expr.c +++ b/libyasm/expr.c @@ -47,9 +47,9 @@ static int expr_traverse_nodes_post(/*@null@*/ yasm_expr *e, - /*@null@*/ void *d, - int (*func) (/*@null@*/ yasm_expr *e, - /*@null@*/ void *d)); + /*@null@*/ void *d, + int (*func) (/*@null@*/ yasm_expr *e, + /*@null@*/ void *d)); static void expr_delete_term(yasm_expr__item *term, int recurse); /* Bitmap of used items. We should really never need more than 2 at a time, @@ -63,7 +63,7 @@ static yasm_expr__item itempool[31]; /*@-compmempass@*/ yasm_expr * yasm_expr_create(yasm_expr_op op, yasm_expr__item *left, - yasm_expr__item *right, unsigned long line) + yasm_expr__item *right, unsigned long line) { yasm_expr *ptr, *sube; unsigned long z; @@ -74,47 +74,47 @@ yasm_expr_create(yasm_expr_op op, yasm_expr__item *left, ptr->terms[0].type = YASM_EXPR_NONE; ptr->terms[1].type = YASM_EXPR_NONE; if (left) { - ptr->terms[0] = *left; /* structure copy */ - z = (unsigned long)(left-itempool); - if (z>=31) - yasm_internal_error(N_("could not find expritem in pool")); - itempool_used &= ~(1<numterms++; - - /* Search downward until we find something *other* than an - * IDENT, then bring it up to the current level. - */ - while (ptr->terms[0].type == YASM_EXPR_EXPR && - ptr->terms[0].data.expn->op == YASM_EXPR_IDENT) { - sube = ptr->terms[0].data.expn; - ptr->terms[0] = sube->terms[0]; /* structure copy */ - /*@-usereleased@*/ - yasm_xfree(sube); - /*@=usereleased@*/ - } + ptr->terms[0] = *left; /* structure copy */ + z = (unsigned long)(left-itempool); + if (z>=31) + yasm_internal_error(N_("could not find expritem in pool")); + itempool_used &= ~(1<numterms++; + + /* Search downward until we find something *other* than an + * IDENT, then bring it up to the current level. + */ + while (ptr->terms[0].type == YASM_EXPR_EXPR && + ptr->terms[0].data.expn->op == YASM_EXPR_IDENT) { + sube = ptr->terms[0].data.expn; + ptr->terms[0] = sube->terms[0]; /* structure copy */ + /*@-usereleased@*/ + yasm_xfree(sube); + /*@=usereleased@*/ + } } else { - yasm_internal_error(N_("Right side of expression must exist")); + yasm_internal_error(N_("Right side of expression must exist")); } if (right) { - ptr->terms[1] = *right; /* structure copy */ - z = (unsigned long)(right-itempool); - if (z>=31) - yasm_internal_error(N_("could not find expritem in pool")); - itempool_used &= ~(1<numterms++; - - /* Search downward until we find something *other* than an - * IDENT, then bring it up to the current level. - */ - while (ptr->terms[1].type == YASM_EXPR_EXPR && - ptr->terms[1].data.expn->op == YASM_EXPR_IDENT) { - sube = ptr->terms[1].data.expn; - ptr->terms[1] = sube->terms[0]; /* structure copy */ - /*@-usereleased@*/ - yasm_xfree(sube); - /*@=usereleased@*/ - } + ptr->terms[1] = *right; /* structure copy */ + z = (unsigned long)(right-itempool); + if (z>=31) + yasm_internal_error(N_("could not find expritem in pool")); + itempool_used &= ~(1<numterms++; + + /* Search downward until we find something *other* than an + * IDENT, then bring it up to the current level. + */ + while (ptr->terms[1].type == YASM_EXPR_EXPR && + ptr->terms[1].data.expn->op == YASM_EXPR_IDENT) { + sube = ptr->terms[1].data.expn; + ptr->terms[1] = sube->terms[0]; /* structure copy */ + /*@-usereleased@*/ + yasm_xfree(sube); + /*@=usereleased@*/ + } } ptr->line = line; @@ -131,11 +131,11 @@ expr_get_item(void) unsigned long v = itempool_used & 0x7fffffff; while (v & 1) { - v >>= 1; - z++; + v >>= 1; + z++; } if (z>=31) - yasm_internal_error(N_("too many expritems")); + yasm_internal_error(N_("too many expritems")); itempool_used |= 1<op != YASM_EXPR_ADD) - return e; + return e; for (i=0; inumterms; i++) { - int j; - yasm_expr *sube; - yasm_intnum *intn; - yasm_symrec *sym = NULL; - /*@dependent@*/ yasm_section *sect2; - /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc2; - - /* First look for an (-1*symrec) term */ - if (e->terms[i].type != YASM_EXPR_EXPR) - continue; - sube = e->terms[i].data.expn; - if (sube->op != YASM_EXPR_MUL || sube->numterms != 2) - continue; - - if (sube->terms[0].type == YASM_EXPR_INT && - (sube->terms[1].type == YASM_EXPR_SYM || - sube->terms[1].type == YASM_EXPR_PRECBC)) { - intn = sube->terms[0].data.intn; - if (sube->terms[1].type == YASM_EXPR_PRECBC) - precbc = sube->terms[1].data.precbc; - else - sym = sube->terms[1].data.sym; - } else if ((sube->terms[0].type == YASM_EXPR_SYM || - sube->terms[0].type == YASM_EXPR_PRECBC) && - sube->terms[1].type == YASM_EXPR_INT) { - if (sube->terms[0].type == YASM_EXPR_PRECBC) - precbc = sube->terms[0].data.precbc; - else - sym = sube->terms[0].data.sym; - intn = sube->terms[1].data.intn; - } else - continue; - - if (!yasm_intnum_is_neg1(intn)) - continue; - - if (sym && !yasm_symrec_get_label(sym, &precbc)) - continue; - sect2 = yasm_bc_get_section(precbc); - - /* Now look for a symrec term in the same segment */ - for (j=0; jnumterms; j++) { - if (((e->terms[j].type == YASM_EXPR_SYM && - yasm_symrec_get_label(e->terms[j].data.sym, &precbc2)) || - (e->terms[j].type == YASM_EXPR_PRECBC && - (precbc2 = e->terms[j].data.precbc))) && - (sect = yasm_bc_get_section(precbc2)) && - sect == sect2 && - callback(&e->terms[j], precbc, precbc2, cbd)) { - /* Delete the matching (-1*symrec) term */ - yasm_expr_destroy(sube); - e->terms[i].type = YASM_EXPR_NONE; - break; /* stop looking for matching symrec term */ - } - } + int j; + yasm_expr *sube; + yasm_intnum *intn; + yasm_symrec *sym = NULL; + /*@dependent@*/ yasm_section *sect2; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc2; + + /* First look for an (-1*symrec) term */ + if (e->terms[i].type != YASM_EXPR_EXPR) + continue; + sube = e->terms[i].data.expn; + if (sube->op != YASM_EXPR_MUL || sube->numterms != 2) + continue; + + if (sube->terms[0].type == YASM_EXPR_INT && + (sube->terms[1].type == YASM_EXPR_SYM || + sube->terms[1].type == YASM_EXPR_PRECBC)) { + intn = sube->terms[0].data.intn; + if (sube->terms[1].type == YASM_EXPR_PRECBC) + precbc = sube->terms[1].data.precbc; + else + sym = sube->terms[1].data.sym; + } else if ((sube->terms[0].type == YASM_EXPR_SYM || + sube->terms[0].type == YASM_EXPR_PRECBC) && + sube->terms[1].type == YASM_EXPR_INT) { + if (sube->terms[0].type == YASM_EXPR_PRECBC) + precbc = sube->terms[0].data.precbc; + else + sym = sube->terms[0].data.sym; + intn = sube->terms[1].data.intn; + } else + continue; + + if (!yasm_intnum_is_neg1(intn)) + continue; + + if (sym && !yasm_symrec_get_label(sym, &precbc)) + continue; + sect2 = yasm_bc_get_section(precbc); + + /* Now look for a symrec term in the same segment */ + for (j=0; jnumterms; j++) { + if (((e->terms[j].type == YASM_EXPR_SYM && + yasm_symrec_get_label(e->terms[j].data.sym, &precbc2)) || + (e->terms[j].type == YASM_EXPR_PRECBC && + (precbc2 = e->terms[j].data.precbc))) && + (sect = yasm_bc_get_section(precbc2)) && + sect == sect2 && + callback(&e->terms[j], precbc, precbc2, cbd)) { + /* Delete the matching (-1*symrec) term */ + yasm_expr_destroy(sube); + e->terms[i].type = YASM_EXPR_NONE; + break; /* stop looking for matching symrec term */ + } + } } /* Clean up any deleted (EXPR_NONE) terms */ numterms = 0; for (i=0; inumterms; i++) { - if (e->terms[i].type != YASM_EXPR_NONE) - e->terms[numterms++] = e->terms[i]; /* structure copy */ + if (e->terms[i].type != YASM_EXPR_NONE) + e->terms[numterms++] = e->terms[i]; /* structure copy */ } if (e->numterms != numterms) { - e->numterms = numterms; - e = yasm_xrealloc(e, sizeof(yasm_expr)+((numterms<2) ? 0 : - sizeof(yasm_expr__item)*(numterms-2))); - if (numterms == 1) - e->op = YASM_EXPR_IDENT; + e->numterms = numterms; + e = yasm_xrealloc(e, sizeof(yasm_expr)+((numterms<2) ? 0 : + sizeof(yasm_expr__item)*(numterms-2))); + if (numterms == 1) + e->op = YASM_EXPR_IDENT; } return e; @@ -295,11 +295,11 @@ expr_xform_bc_dist_base(/*@returned@*/ /*@only@*/ yasm_expr *e, static int expr_xform_bc_dist_cb(yasm_expr__item *ei, yasm_bytecode *precbc, - yasm_bytecode *precbc2, /*@null@*/ void *d) + yasm_bytecode *precbc2, /*@null@*/ void *d) { yasm_intnum *dist = yasm_calc_bc_dist(precbc, precbc2); if (!dist) - return 0; + return 0; /* Change the term to an integer */ ei->type = YASM_EXPR_INT; ei->data.intn = dist; @@ -317,14 +317,14 @@ expr_xform_bc_dist(/*@returned@*/ /*@only@*/ yasm_expr *e) typedef struct bc_dist_subst_cbd { void (*callback) (unsigned int subst, yasm_bytecode *precbc, - yasm_bytecode *precbc2, void *cbd); + yasm_bytecode *precbc2, void *cbd); void *cbd; unsigned int subst; } bc_dist_subst_cbd; static int expr_bc_dist_subst_cb(yasm_expr__item *ei, yasm_bytecode *precbc, - yasm_bytecode *precbc2, /*@null@*/ void *d) + yasm_bytecode *precbc2, /*@null@*/ void *d) { bc_dist_subst_cbd *my_cbd = d; assert(my_cbd != NULL); @@ -345,17 +345,17 @@ expr_xform_bc_dist_subst(yasm_expr *e, void *d) int yasm_expr__bc_dist_subst(yasm_expr **ep, void *cbd, - void (*callback) (unsigned int subst, - yasm_bytecode *precbc, - yasm_bytecode *precbc2, - void *cbd)) + void (*callback) (unsigned int subst, + yasm_bytecode *precbc, + yasm_bytecode *precbc2, + void *cbd)) { - bc_dist_subst_cbd my_cbd; /* callback info for low-level callback */ + bc_dist_subst_cbd my_cbd; /* callback info for low-level callback */ my_cbd.callback = callback; my_cbd.cbd = cbd; my_cbd.subst = 0; *ep = yasm_expr__level_tree(*ep, 1, 1, 1, 0, &expr_xform_bc_dist_subst, - &my_cbd); + &my_cbd); return my_cbd.subst; } @@ -371,7 +371,7 @@ expr_xform_neg_item(yasm_expr *e, yasm_expr__item *ei) sube->numterms = 2; sube->terms[0].type = YASM_EXPR_INT; sube->terms[0].data.intn = yasm_intnum_create_int(-1); - sube->terms[1] = *ei; /* structure copy */ + sube->terms[1] = *ei; /* structure copy */ /* Replace original ExprItem with subexp */ ei->type = YASM_EXPR_EXPR; @@ -391,58 +391,58 @@ expr_xform_neg_helper(/*@returned@*/ /*@only@*/ yasm_expr *e) int i; switch (e->op) { - case YASM_EXPR_ADD: - /* distribute (recursively if expr) over terms */ - for (i=0; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_EXPR) - e->terms[i].data.expn = - expr_xform_neg_helper(e->terms[i].data.expn); - else - expr_xform_neg_item(e, &e->terms[i]); - } - break; - case YASM_EXPR_SUB: - /* change op to ADD, and recursively negate left side (if expr) */ - e->op = YASM_EXPR_ADD; - if (e->terms[0].type == YASM_EXPR_EXPR) - e->terms[0].data.expn = - expr_xform_neg_helper(e->terms[0].data.expn); - else - expr_xform_neg_item(e, &e->terms[0]); - break; - case YASM_EXPR_NEG: - /* Negating a negated value? Make it an IDENT. */ - e->op = YASM_EXPR_IDENT; - break; - case YASM_EXPR_IDENT: - /* Negating an ident? Change it into a MUL w/ -1 if there's no - * floatnums present below; if there ARE floatnums, recurse. - */ - if (e->terms[0].type == YASM_EXPR_FLOAT) - yasm_floatnum_calc(e->terms[0].data.flt, YASM_EXPR_NEG, NULL); - else if (e->terms[0].type == YASM_EXPR_EXPR && - yasm_expr__contains(e->terms[0].data.expn, YASM_EXPR_FLOAT)) - expr_xform_neg_helper(e->terms[0].data.expn); - else { - e->op = YASM_EXPR_MUL; - e->numterms = 2; - e->terms[1].type = YASM_EXPR_INT; - e->terms[1].data.intn = yasm_intnum_create_int(-1); - } - break; - default: - /* Everything else. MUL will be combined when it's leveled. - * Make a new expr (to replace e) with -1*e. - */ - ne = yasm_xmalloc(sizeof(yasm_expr)); - ne->op = YASM_EXPR_MUL; - ne->line = e->line; - ne->numterms = 2; - ne->terms[0].type = YASM_EXPR_INT; - ne->terms[0].data.intn = yasm_intnum_create_int(-1); - ne->terms[1].type = YASM_EXPR_EXPR; - ne->terms[1].data.expn = e; - return ne; + case YASM_EXPR_ADD: + /* distribute (recursively if expr) over terms */ + for (i=0; inumterms; i++) { + if (e->terms[i].type == YASM_EXPR_EXPR) + e->terms[i].data.expn = + expr_xform_neg_helper(e->terms[i].data.expn); + else + expr_xform_neg_item(e, &e->terms[i]); + } + break; + case YASM_EXPR_SUB: + /* change op to ADD, and recursively negate left side (if expr) */ + e->op = YASM_EXPR_ADD; + if (e->terms[0].type == YASM_EXPR_EXPR) + e->terms[0].data.expn = + expr_xform_neg_helper(e->terms[0].data.expn); + else + expr_xform_neg_item(e, &e->terms[0]); + break; + case YASM_EXPR_NEG: + /* Negating a negated value? Make it an IDENT. */ + e->op = YASM_EXPR_IDENT; + break; + case YASM_EXPR_IDENT: + /* Negating an ident? Change it into a MUL w/ -1 if there's no + * floatnums present below; if there ARE floatnums, recurse. + */ + if (e->terms[0].type == YASM_EXPR_FLOAT) + yasm_floatnum_calc(e->terms[0].data.flt, YASM_EXPR_NEG, NULL); + else if (e->terms[0].type == YASM_EXPR_EXPR && + yasm_expr__contains(e->terms[0].data.expn, YASM_EXPR_FLOAT)) + expr_xform_neg_helper(e->terms[0].data.expn); + else { + e->op = YASM_EXPR_MUL; + e->numterms = 2; + e->terms[1].type = YASM_EXPR_INT; + e->terms[1].data.intn = yasm_intnum_create_int(-1); + } + break; + default: + /* Everything else. MUL will be combined when it's leveled. + * Make a new expr (to replace e) with -1*e. + */ + ne = yasm_xmalloc(sizeof(yasm_expr)); + ne->op = YASM_EXPR_MUL; + ne->line = e->line; + ne->numterms = 2; + ne->terms[0].type = YASM_EXPR_INT; + ne->terms[0].data.intn = yasm_intnum_create_int(-1); + ne->terms[1].type = YASM_EXPR_EXPR; + ne->terms[1].data.expn = e; + return ne; } return e; } @@ -459,23 +459,23 @@ static /*@only@*/ yasm_expr * expr_xform_neg(/*@returned@*/ /*@only@*/ yasm_expr *e) { switch (e->op) { - case YASM_EXPR_NEG: - /* Turn -x into -1*x */ - e->op = YASM_EXPR_IDENT; - return expr_xform_neg_helper(e); - case YASM_EXPR_SUB: - /* Turn a-b into a+(-1*b) */ - - /* change op to ADD, and recursively negate right side (if expr) */ - e->op = YASM_EXPR_ADD; - if (e->terms[1].type == YASM_EXPR_EXPR) - e->terms[1].data.expn = - expr_xform_neg_helper(e->terms[1].data.expn); - else - expr_xform_neg_item(e, &e->terms[1]); - break; - default: - break; + case YASM_EXPR_NEG: + /* Turn -x into -1*x */ + e->op = YASM_EXPR_IDENT; + return expr_xform_neg_helper(e); + case YASM_EXPR_SUB: + /* Turn a-b into a+(-1*b) */ + + /* change op to ADD, and recursively negate right side (if expr) */ + e->op = YASM_EXPR_ADD; + if (e->terms[1].type == YASM_EXPR_EXPR) + e->terms[1].data.expn = + expr_xform_neg_helper(e->terms[1].data.expn); + else + expr_xform_neg_item(e, &e->terms[1]); + break; + default: + break; } return e; @@ -489,9 +489,9 @@ expr_is_constant(yasm_expr_op op, yasm_intnum *intn) { int iszero = yasm_intnum_is_zero(intn); return ((iszero && op == YASM_EXPR_MUL) || - (iszero && op == YASM_EXPR_AND) || - (iszero && op == YASM_EXPR_LAND) || - (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_OR)); + (iszero && op == YASM_EXPR_AND) || + (iszero && op == YASM_EXPR_LAND) || + (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_OR)); } /* Look for simple "left" identities like 0+x, 1*x, etc. */ @@ -500,11 +500,11 @@ expr_can_destroy_int_left(yasm_expr_op op, yasm_intnum *intn) { int iszero = yasm_intnum_is_zero(intn); return ((yasm_intnum_is_pos1(intn) && op == YASM_EXPR_MUL) || - (iszero && op == YASM_EXPR_ADD) || - (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_AND) || - (!iszero && op == YASM_EXPR_LAND) || - (iszero && op == YASM_EXPR_OR) || - (iszero && op == YASM_EXPR_LOR)); + (iszero && op == YASM_EXPR_ADD) || + (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_AND) || + (!iszero && op == YASM_EXPR_LAND) || + (iszero && op == YASM_EXPR_OR) || + (iszero && op == YASM_EXPR_LOR)); } /* Look for simple "right" identities like x+|-0, x*&/1 */ @@ -514,15 +514,15 @@ expr_can_destroy_int_right(yasm_expr_op op, yasm_intnum *intn) int iszero = yasm_intnum_is_zero(intn); int ispos1 = yasm_intnum_is_pos1(intn); return ((ispos1 && op == YASM_EXPR_MUL) || - (ispos1 && op == YASM_EXPR_DIV) || - (iszero && op == YASM_EXPR_ADD) || - (iszero && op == YASM_EXPR_SUB) || - (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_AND) || - (!iszero && op == YASM_EXPR_LAND) || - (iszero && op == YASM_EXPR_OR) || - (iszero && op == YASM_EXPR_LOR) || - (iszero && op == YASM_EXPR_SHL) || - (iszero && op == YASM_EXPR_SHR)); + (ispos1 && op == YASM_EXPR_DIV) || + (iszero && op == YASM_EXPR_ADD) || + (iszero && op == YASM_EXPR_SUB) || + (yasm_intnum_is_neg1(intn) && op == YASM_EXPR_AND) || + (!iszero && op == YASM_EXPR_LAND) || + (iszero && op == YASM_EXPR_OR) || + (iszero && op == YASM_EXPR_LOR) || + (iszero && op == YASM_EXPR_SHL) || + (iszero && op == YASM_EXPR_SHR)); } /* Check for and simplify identities. Returns new number of expr terms. @@ -534,7 +534,7 @@ expr_can_destroy_int_right(yasm_expr_op op, yasm_intnum *intn) */ static int expr_simplify_identity(yasm_expr *e, int numterms, int int_term, - int simplify_reg_mul) + int simplify_reg_mul) { int i; int save_numterms; @@ -545,27 +545,27 @@ expr_simplify_identity(yasm_expr *e, int numterms, int int_term, save_numterms = e->numterms; e->numterms = numterms; if (simplify_reg_mul || e->op != YASM_EXPR_MUL - || !yasm_intnum_is_pos1(e->terms[int_term].data.intn) - || !yasm_expr__contains(e, YASM_EXPR_REG)) { - /* Check for simple identities that delete the intnum. - * Don't delete if the intnum is the only thing in the expn. - */ - if ((int_term == 0 && numterms > 1 && - expr_can_destroy_int_left(e->op, e->terms[0].data.intn)) || - (int_term > 0 && - expr_can_destroy_int_right(e->op, e->terms[int_term].data.intn))) { - /* Delete the intnum */ - yasm_intnum_destroy(e->terms[int_term].data.intn); - - /* Slide everything to its right over by 1 */ - if (int_term != numterms-1) /* if it wasn't last.. */ - memmove(&e->terms[int_term], &e->terms[int_term+1], - (numterms-1-int_term)*sizeof(yasm_expr__item)); - - /* Update numterms */ - numterms--; - int_term = -1; /* no longer an int term */ - } + || !yasm_intnum_is_pos1(e->terms[int_term].data.intn) + || !yasm_expr__contains(e, YASM_EXPR_REG)) { + /* Check for simple identities that delete the intnum. + * Don't delete if the intnum is the only thing in the expn. + */ + if ((int_term == 0 && numterms > 1 && + expr_can_destroy_int_left(e->op, e->terms[0].data.intn)) || + (int_term > 0 && + expr_can_destroy_int_right(e->op, e->terms[int_term].data.intn))) { + /* Delete the intnum */ + yasm_intnum_destroy(e->terms[int_term].data.intn); + + /* Slide everything to its right over by 1 */ + if (int_term != numterms-1) /* if it wasn't last.. */ + memmove(&e->terms[int_term], &e->terms[int_term+1], + (numterms-1-int_term)*sizeof(yasm_expr__item)); + + /* Update numterms */ + numterms--; + int_term = -1; /* no longer an int term */ + } } e->numterms = save_numterms; @@ -573,29 +573,29 @@ expr_simplify_identity(yasm_expr *e, int numterms, int int_term, * Don't bother if the intnum is the only thing in the expn. */ if (numterms > 1 && int_term != -1 && - expr_is_constant(e->op, e->terms[int_term].data.intn)) { - /* Loop through, deleting everything but the integer term */ - for (i=0; inumterms; i++) - if (i != int_term) - expr_delete_term(&e->terms[i], 1); - - /* Move integer term to the first term (if not already there) */ - if (int_term != 0) - e->terms[0] = e->terms[int_term]; /* structure copy */ - - /* Set numterms to 1 */ - numterms = 1; + expr_is_constant(e->op, e->terms[int_term].data.intn)) { + /* Loop through, deleting everything but the integer term */ + for (i=0; inumterms; i++) + if (i != int_term) + expr_delete_term(&e->terms[i], 1); + + /* Move integer term to the first term (if not already there) */ + if (int_term != 0) + e->terms[0] = e->terms[int_term]; /* structure copy */ + + /* Set numterms to 1 */ + numterms = 1; } /* Compute NOT, NEG, and LNOT on single intnum. */ if (numterms == 1 && int_term == 0 && - (e->op == YASM_EXPR_NOT || e->op == YASM_EXPR_NEG || - e->op == YASM_EXPR_LNOT)) - yasm_intnum_calc(e->terms[0].data.intn, e->op, NULL); + (e->op == YASM_EXPR_NOT || e->op == YASM_EXPR_NEG || + e->op == YASM_EXPR_LNOT)) + yasm_intnum_calc(e->terms[0].data.intn, e->op, NULL); /* Change expression to IDENT if possible. */ if (numterms == 1) - e->op = YASM_EXPR_IDENT; + e->op = YASM_EXPR_IDENT; /* Return the updated numterms */ return numterms; @@ -615,7 +615,7 @@ expr_simplify_identity(yasm_expr *e, int numterms, int int_term, /*@-mustfree@*/ static /*@only@*/ yasm_expr * expr_level_op(/*@returned@*/ /*@only@*/ yasm_expr *e, int fold_const, - int simplify_ident, int simplify_reg_mul) + int simplify_ident, int simplify_reg_mul) { int i, j, o, fold_numterms, level_numterms, level_fold_numterms; int first_int_term = -1; @@ -624,51 +624,51 @@ expr_level_op(/*@returned@*/ /*@only@*/ yasm_expr *e, int fold_const, * Go ahead and bring up any IDENT'ed values. */ while (e->op == YASM_EXPR_IDENT && e->terms[0].type == YASM_EXPR_EXPR) { - yasm_expr *sube = e->terms[0].data.expn; - yasm_xfree(e); - e = sube; + yasm_expr *sube = e->terms[0].data.expn; + yasm_xfree(e); + e = sube; } /* If non-numeric expression, don't fold constants. */ if (e->op > YASM_EXPR_NONNUM) - fold_const = 0; + fold_const = 0; level_numterms = e->numterms; level_fold_numterms = 0; for (i=0; inumterms; i++) { - /* Search downward until we find something *other* than an - * IDENT, then bring it up to the current level. - */ - while (e->terms[i].type == YASM_EXPR_EXPR && - e->terms[i].data.expn->op == YASM_EXPR_IDENT) { - yasm_expr *sube = e->terms[i].data.expn; - e->terms[i] = sube->terms[0]; - yasm_xfree(sube); - } - - if (e->terms[i].type == YASM_EXPR_EXPR && - e->terms[i].data.expn->op == e->op) { - /* It's an expression w/the same operator, add in its numterms. - * But don't forget to subtract one for the expr itself! - */ - level_numterms += e->terms[i].data.expn->numterms - 1; - - /* If we're folding constants, count up the number of constants - * that will be merged in. - */ - if (fold_const) - for (j=0; jterms[i].data.expn->numterms; j++) - if (e->terms[i].data.expn->terms[j].type == - YASM_EXPR_INT) - level_fold_numterms++; - } - - /* Find the first integer term (if one is present) if we're folding - * constants. - */ - if (fold_const && first_int_term == -1 && - e->terms[i].type == YASM_EXPR_INT) - first_int_term = i; + /* Search downward until we find something *other* than an + * IDENT, then bring it up to the current level. + */ + while (e->terms[i].type == YASM_EXPR_EXPR && + e->terms[i].data.expn->op == YASM_EXPR_IDENT) { + yasm_expr *sube = e->terms[i].data.expn; + e->terms[i] = sube->terms[0]; + yasm_xfree(sube); + } + + if (e->terms[i].type == YASM_EXPR_EXPR && + e->terms[i].data.expn->op == e->op) { + /* It's an expression w/the same operator, add in its numterms. + * But don't forget to subtract one for the expr itself! + */ + level_numterms += e->terms[i].data.expn->numterms - 1; + + /* If we're folding constants, count up the number of constants + * that will be merged in. + */ + if (fold_const) + for (j=0; jterms[i].data.expn->numterms; j++) + if (e->terms[i].data.expn->terms[j].type == + YASM_EXPR_INT) + level_fold_numterms++; + } + + /* Find the first integer term (if one is present) if we're folding + * constants. + */ + if (fold_const && first_int_term == -1 && + e->terms[i].type == YASM_EXPR_INT) + first_int_term = i; } /* Look for other integer terms if there's one and combine. @@ -677,120 +677,120 @@ expr_level_op(/*@returned@*/ /*@only@*/ yasm_expr *e, int fold_const, */ fold_numterms = e->numterms; if (first_int_term != -1) { - for (i=first_int_term+1, o=first_int_term+1; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_INT) { - yasm_intnum_calc(e->terms[first_int_term].data.intn, e->op, - e->terms[i].data.intn); - fold_numterms--; - level_numterms--; - /* make sure to delete folded intnum */ - yasm_intnum_destroy(e->terms[i].data.intn); - } else if (o != i) { - /* copy term if it changed places */ - e->terms[o++] = e->terms[i]; - } else - o++; - } - - if (simplify_ident) { - int new_fold_numterms; - /* Simplify identities and make IDENT if possible. */ - new_fold_numterms = - expr_simplify_identity(e, fold_numterms, first_int_term, - simplify_reg_mul); - level_numterms -= fold_numterms-new_fold_numterms; - fold_numterms = new_fold_numterms; - } - if (fold_numterms == 1) - e->op = YASM_EXPR_IDENT; + for (i=first_int_term+1, o=first_int_term+1; inumterms; i++) { + if (e->terms[i].type == YASM_EXPR_INT) { + yasm_intnum_calc(e->terms[first_int_term].data.intn, e->op, + e->terms[i].data.intn); + fold_numterms--; + level_numterms--; + /* make sure to delete folded intnum */ + yasm_intnum_destroy(e->terms[i].data.intn); + } else if (o != i) { + /* copy term if it changed places */ + e->terms[o++] = e->terms[i]; + } else + o++; + } + + if (simplify_ident) { + int new_fold_numterms; + /* Simplify identities and make IDENT if possible. */ + new_fold_numterms = + expr_simplify_identity(e, fold_numterms, first_int_term, + simplify_reg_mul); + level_numterms -= fold_numterms-new_fold_numterms; + fold_numterms = new_fold_numterms; + } + if (fold_numterms == 1) + e->op = YASM_EXPR_IDENT; } /* Only level operators that allow more than two operand terms. * Also don't bother leveling if it's not necessary to bring up any terms. */ if ((e->op != YASM_EXPR_ADD && e->op != YASM_EXPR_MUL && - e->op != YASM_EXPR_OR && e->op != YASM_EXPR_AND && - e->op != YASM_EXPR_LOR && e->op != YASM_EXPR_LAND && - e->op != YASM_EXPR_LXOR && e->op != YASM_EXPR_XOR) || - level_numterms <= fold_numterms) { - /* Downsize e if necessary */ - if (fold_numterms < e->numterms && e->numterms > 2) - e = yasm_xrealloc(e, sizeof(yasm_expr)+((fold_numterms<2) ? 0 : - sizeof(yasm_expr__item)*(fold_numterms-2))); - /* Update numterms */ - e->numterms = fold_numterms; - return e; + e->op != YASM_EXPR_OR && e->op != YASM_EXPR_AND && + e->op != YASM_EXPR_LOR && e->op != YASM_EXPR_LAND && + e->op != YASM_EXPR_LXOR && e->op != YASM_EXPR_XOR) || + level_numterms <= fold_numterms) { + /* Downsize e if necessary */ + if (fold_numterms < e->numterms && e->numterms > 2) + e = yasm_xrealloc(e, sizeof(yasm_expr)+((fold_numterms<2) ? 0 : + sizeof(yasm_expr__item)*(fold_numterms-2))); + /* Update numterms */ + e->numterms = fold_numterms; + return e; } /* Adjust numterms for constant folding from terms being "pulled up". * Careful: if there's no integer term in e, then save space for it. */ if (fold_const) { - level_numterms -= level_fold_numterms; - if (first_int_term == -1 && level_fold_numterms != 0) - level_numterms++; + level_numterms -= level_fold_numterms; + if (first_int_term == -1 && level_fold_numterms != 0) + level_numterms++; } /* Alloc more (or conceivably less, but not usually) space for e */ e = yasm_xrealloc(e, sizeof(yasm_expr)+((level_numterms<2) ? 0 : - sizeof(yasm_expr__item)*(level_numterms-2))); + sizeof(yasm_expr__item)*(level_numterms-2))); /* Copy up ExprItem's. Iterate from right to left to keep the same * ordering as was present originally. * Combine integer terms as necessary. */ for (i=e->numterms-1, o=level_numterms-1; i>=0; i--) { - if (e->terms[i].type == YASM_EXPR_EXPR && - e->terms[i].data.expn->op == e->op) { - /* bring up subexpression */ - yasm_expr *sube = e->terms[i].data.expn; - - /* copy terms right to left */ - for (j=sube->numterms-1; j>=0; j--) { - if (fold_const && sube->terms[j].type == YASM_EXPR_INT) { - /* Need to fold it in.. but if there's no int term already, - * just copy into a new one. - */ - if (first_int_term == -1) { - first_int_term = o--; - e->terms[first_int_term] = sube->terms[j]; /* struc */ - } else { - yasm_intnum_calc(e->terms[first_int_term].data.intn, - e->op, sube->terms[j].data.intn); - /* make sure to delete folded intnum */ - yasm_intnum_destroy(sube->terms[j].data.intn); - } - } else { - if (o == first_int_term) - o--; - e->terms[o--] = sube->terms[j]; /* structure copy */ - } - } - - /* delete subexpression, but *don't delete nodes* (as we've just - * copied them!) - */ - yasm_xfree(sube); - } else if (o != i) { - /* copy operand if it changed places */ - if (o == first_int_term) - o--; - e->terms[o] = e->terms[i]; - /* If we moved the first_int_term, change first_int_num too */ - if (i == first_int_term) - first_int_term = o; - o--; - } + if (e->terms[i].type == YASM_EXPR_EXPR && + e->terms[i].data.expn->op == e->op) { + /* bring up subexpression */ + yasm_expr *sube = e->terms[i].data.expn; + + /* copy terms right to left */ + for (j=sube->numterms-1; j>=0; j--) { + if (fold_const && sube->terms[j].type == YASM_EXPR_INT) { + /* Need to fold it in.. but if there's no int term already, + * just copy into a new one. + */ + if (first_int_term == -1) { + first_int_term = o--; + e->terms[first_int_term] = sube->terms[j]; /* struc */ + } else { + yasm_intnum_calc(e->terms[first_int_term].data.intn, + e->op, sube->terms[j].data.intn); + /* make sure to delete folded intnum */ + yasm_intnum_destroy(sube->terms[j].data.intn); + } + } else { + if (o == first_int_term) + o--; + e->terms[o--] = sube->terms[j]; /* structure copy */ + } + } + + /* delete subexpression, but *don't delete nodes* (as we've just + * copied them!) + */ + yasm_xfree(sube); + } else if (o != i) { + /* copy operand if it changed places */ + if (o == first_int_term) + o--; + e->terms[o] = e->terms[i]; + /* If we moved the first_int_term, change first_int_num too */ + if (i == first_int_term) + first_int_term = o; + o--; + } } /* Simplify identities, make IDENT if possible, and save to e->numterms. */ if (simplify_ident && first_int_term != -1) { - e->numterms = expr_simplify_identity(e, level_numterms, - first_int_term, simplify_reg_mul); + e->numterms = expr_simplify_identity(e, level_numterms, + first_int_term, simplify_reg_mul); } else { - e->numterms = level_numterms; - if (level_numterms == 1) - e->op = YASM_EXPR_IDENT; + e->numterms = level_numterms; + if (level_numterms == 1) + e->op = YASM_EXPR_IDENT; } return e; @@ -811,33 +811,33 @@ expr_expand_equ(yasm_expr *e, yasm__exprhead *eh) /* traverse terms */ for (i=0; inumterms; i++) { - const yasm_expr *equ_expr; - - /* Expand equ's. */ - if (e->terms[i].type == YASM_EXPR_SYM && - (equ_expr = yasm_symrec_get_equ(e->terms[i].data.sym))) { - yasm__exprentry *np; - - /* Check for circular reference */ - SLIST_FOREACH(np, eh, next) { - if (np->e == equ_expr) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("circular reference detected")); - return e; - } - } - - e->terms[i].type = YASM_EXPR_EXPR; - e->terms[i].data.expn = yasm_expr_copy(equ_expr); - - /* Remember we saw this equ and recurse */ - ee.e = equ_expr; - SLIST_INSERT_HEAD(eh, &ee, next); - e->terms[i].data.expn = expr_expand_equ(e->terms[i].data.expn, eh); - SLIST_REMOVE_HEAD(eh, next); - } else if (e->terms[i].type == YASM_EXPR_EXPR) - /* Recurse */ - e->terms[i].data.expn = expr_expand_equ(e->terms[i].data.expn, eh); + const yasm_expr *equ_expr; + + /* Expand equ's. */ + if (e->terms[i].type == YASM_EXPR_SYM && + (equ_expr = yasm_symrec_get_equ(e->terms[i].data.sym))) { + yasm__exprentry *np; + + /* Check for circular reference */ + SLIST_FOREACH(np, eh, next) { + if (np->e == equ_expr) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("circular reference detected")); + return e; + } + } + + e->terms[i].type = YASM_EXPR_EXPR; + e->terms[i].data.expn = yasm_expr_copy(equ_expr); + + /* Remember we saw this equ and recurse */ + ee.e = equ_expr; + SLIST_INSERT_HEAD(eh, &ee, next); + e->terms[i].data.expn = expr_expand_equ(e->terms[i].data.expn, eh); + SLIST_REMOVE_HEAD(eh, next); + } else if (e->terms[i].type == YASM_EXPR_EXPR) + /* Recurse */ + e->terms[i].data.expn = expr_expand_equ(e->terms[i].data.expn, eh); } return e; @@ -845,9 +845,9 @@ expr_expand_equ(yasm_expr *e, yasm__exprhead *eh) static yasm_expr * expr_level_tree(yasm_expr *e, int fold_const, int simplify_ident, - int simplify_reg_mul, int calc_bc_dist, - yasm_expr_xform_func expr_xform_extra, - void *expr_xform_extra_data) + int simplify_reg_mul, int calc_bc_dist, + yasm_expr_xform_func expr_xform_extra, + void *expr_xform_extra_data) { int i; @@ -855,32 +855,32 @@ expr_level_tree(yasm_expr *e, int fold_const, int simplify_ident, /* traverse terms */ for (i=0; inumterms; i++) { - /* Recurse */ - if (e->terms[i].type == YASM_EXPR_EXPR) - e->terms[i].data.expn = - expr_level_tree(e->terms[i].data.expn, fold_const, - simplify_ident, simplify_reg_mul, calc_bc_dist, - expr_xform_extra, expr_xform_extra_data); + /* Recurse */ + if (e->terms[i].type == YASM_EXPR_EXPR) + e->terms[i].data.expn = + expr_level_tree(e->terms[i].data.expn, fold_const, + simplify_ident, simplify_reg_mul, calc_bc_dist, + expr_xform_extra, expr_xform_extra_data); } /* Check for SEG of SEG:OFF, if we match, simplify to just the segment */ if (e->op == YASM_EXPR_SEG && e->terms[0].type == YASM_EXPR_EXPR && - e->terms[0].data.expn->op == YASM_EXPR_SEGOFF) { - e->op = YASM_EXPR_IDENT; - e->terms[0].data.expn->op = YASM_EXPR_IDENT; - /* Destroy the second (offset) term */ - expr_delete_term(&e->terms[1], 1); + e->terms[0].data.expn->op == YASM_EXPR_SEGOFF) { + e->op = YASM_EXPR_IDENT; + e->terms[0].data.expn->op = YASM_EXPR_IDENT; + /* Destroy the second (offset) term */ + expr_delete_term(&e->terms[1], 1); } /* do callback */ e = expr_level_op(e, fold_const, simplify_ident, simplify_reg_mul); if (calc_bc_dist || expr_xform_extra) { - if (calc_bc_dist) - e = expr_xform_bc_dist(e); - if (expr_xform_extra) - e = expr_xform_extra(e, expr_xform_extra_data); - e = expr_level_tree(e, fold_const, simplify_ident, simplify_reg_mul, - 0, NULL, NULL); + if (calc_bc_dist) + e = expr_xform_bc_dist(e); + if (expr_xform_extra) + e = expr_xform_extra(e, expr_xform_extra_data); + e = expr_level_tree(e, fold_const, simplify_ident, simplify_reg_mul, + 0, NULL, NULL); } return e; } @@ -888,19 +888,19 @@ expr_level_tree(yasm_expr *e, int fold_const, int simplify_ident, /* Level an entire expn tree, expanding equ's as we go */ yasm_expr * yasm_expr__level_tree(yasm_expr *e, int fold_const, int simplify_ident, - int simplify_reg_mul, int calc_bc_dist, - yasm_expr_xform_func expr_xform_extra, - void *expr_xform_extra_data) + int simplify_reg_mul, int calc_bc_dist, + yasm_expr_xform_func expr_xform_extra, + void *expr_xform_extra_data) { yasm__exprhead eh; SLIST_INIT(&eh); if (!e) - return 0; + return 0; e = expr_expand_equ(e, &eh); e = expr_level_tree(e, fold_const, simplify_ident, simplify_reg_mul, - calc_bc_dist, expr_xform_extra, expr_xform_extra_data); + calc_bc_dist, expr_xform_extra, expr_xform_extra_data); return e; } @@ -927,27 +927,27 @@ yasm_expr__order_terms(yasm_expr *e) { /* don't bother reordering if only one element */ if (e->numterms == 1) - return; + return; /* only reorder some types of operations */ switch (e->op) { - case YASM_EXPR_ADD: - case YASM_EXPR_MUL: - case YASM_EXPR_OR: - case YASM_EXPR_AND: - case YASM_EXPR_XOR: - case YASM_EXPR_LOR: - case YASM_EXPR_LAND: - case YASM_EXPR_LXOR: - /* Use mergesort to sort. It's fast on already sorted values and a - * stable sort (multiple terms of same type are kept in the same - * order). - */ - yasm__mergesort(e->terms, (size_t)e->numterms, - sizeof(yasm_expr__item), expr_order_terms_compare); - break; - default: - break; + case YASM_EXPR_ADD: + case YASM_EXPR_MUL: + case YASM_EXPR_OR: + case YASM_EXPR_AND: + case YASM_EXPR_XOR: + case YASM_EXPR_LOR: + case YASM_EXPR_LAND: + case YASM_EXPR_LXOR: + /* Use mergesort to sort. It's fast on already sorted values and a + * stable sort (multiple terms of same type are kept in the same + * order). + */ + yasm__mergesort(e->terms, (size_t)e->numterms, + sizeof(yasm_expr__item), expr_order_terms_compare); + break; + default: + break; } } @@ -956,31 +956,31 @@ expr_item_copy(yasm_expr__item *dest, const yasm_expr__item *src) { dest->type = src->type; switch (src->type) { - case YASM_EXPR_SYM: - /* Symbols don't need to be copied */ - dest->data.sym = src->data.sym; - break; - case YASM_EXPR_PRECBC: - /* Nor do direct bytecode references */ - dest->data.precbc = src->data.precbc; - break; - case YASM_EXPR_EXPR: - dest->data.expn = yasm_expr__copy_except(src->data.expn, -1); - break; - case YASM_EXPR_INT: - dest->data.intn = yasm_intnum_copy(src->data.intn); - break; - case YASM_EXPR_FLOAT: - dest->data.flt = yasm_floatnum_copy(src->data.flt); - break; - case YASM_EXPR_REG: - dest->data.reg = src->data.reg; - break; - case YASM_EXPR_SUBST: - dest->data.subst = src->data.subst; - break; - default: - break; + case YASM_EXPR_SYM: + /* Symbols don't need to be copied */ + dest->data.sym = src->data.sym; + break; + case YASM_EXPR_PRECBC: + /* Nor do direct bytecode references */ + dest->data.precbc = src->data.precbc; + break; + case YASM_EXPR_EXPR: + dest->data.expn = yasm_expr__copy_except(src->data.expn, -1); + break; + case YASM_EXPR_INT: + dest->data.intn = yasm_intnum_copy(src->data.intn); + break; + case YASM_EXPR_FLOAT: + dest->data.flt = yasm_floatnum_copy(src->data.flt); + break; + case YASM_EXPR_REG: + dest->data.reg = src->data.reg; + break; + case YASM_EXPR_SUBST: + dest->data.subst = src->data.subst; + break; + default: + break; } } @@ -992,14 +992,14 @@ yasm_expr__copy_except(const yasm_expr *e, int except) int i; n = yasm_xmalloc(sizeof(yasm_expr) + - sizeof(yasm_expr__item)*(e->numterms<2?0:e->numterms-2)); + sizeof(yasm_expr__item)*(e->numterms<2?0:e->numterms-2)); n->op = e->op; n->line = e->line; n->numterms = e->numterms; for (i=0; inumterms; i++) { - if (i != except) - expr_item_copy(&n->terms[i], &e->terms[i]); + if (i != except) + expr_item_copy(&n->terms[i], &e->terms[i]); } return n; @@ -1015,18 +1015,18 @@ static void expr_delete_term(yasm_expr__item *term, int recurse) { switch (term->type) { - case YASM_EXPR_INT: - yasm_intnum_destroy(term->data.intn); - break; - case YASM_EXPR_FLOAT: - yasm_floatnum_destroy(term->data.flt); - break; - case YASM_EXPR_EXPR: - if (recurse) - yasm_expr_destroy(term->data.expn); - break; - default: - break; + case YASM_EXPR_INT: + yasm_intnum_destroy(term->data.intn); + break; + case YASM_EXPR_FLOAT: + yasm_floatnum_destroy(term->data.flt); + break; + case YASM_EXPR_EXPR: + if (recurse) + yasm_expr_destroy(term->data.expn); + break; + default: + break; } } @@ -1035,9 +1035,9 @@ expr_destroy_each(/*@only@*/ yasm_expr *e, /*@unused@*/ void *d) { int i; for (i=0; inumterms; i++) - expr_delete_term(&e->terms[i], 0); - yasm_xfree(e); /* free ourselves */ - return 0; /* don't stop recursion */ + expr_delete_term(&e->terms[i], 0); + yasm_xfree(e); /* free ourselves */ + return 0; /* don't stop recursion */ } /*@-mustfree@*/ @@ -1077,16 +1077,16 @@ expr_subst_callback(yasm_expr__item *ei, void *d) { subst_cbd *cbd = d; if (ei->type != YASM_EXPR_SUBST) - return 0; + return 0; if (ei->data.subst >= cbd->num_items) - return 1; /* error */ + return 1; /* error */ expr_item_copy(ei, &cbd->items[ei->data.subst]); return 0; } int yasm_expr__subst(yasm_expr *e, unsigned int num_items, - const yasm_expr__item *items) + const yasm_expr__item *items) { subst_cbd cbd; cbd.num_items = num_items; @@ -1102,19 +1102,19 @@ yasm_expr__subst(yasm_expr *e, unsigned int num_items, */ static int expr_traverse_nodes_post(yasm_expr *e, void *d, - int (*func) (/*@null@*/ yasm_expr *e, - /*@null@*/ void *d)) + int (*func) (/*@null@*/ yasm_expr *e, + /*@null@*/ void *d)) { int i; if (!e) - return 0; + return 0; /* traverse terms */ for (i=0; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_EXPR && - expr_traverse_nodes_post(e->terms[i].data.expn, d, func)) - return 1; + if (e->terms[i].type == YASM_EXPR_EXPR && + expr_traverse_nodes_post(e->terms[i].data.expn, d, func)) + return 1; } /* do callback */ @@ -1133,17 +1133,17 @@ yasm_expr__traverse_leaves_in_const(const yasm_expr *e, void *d, int i; if (!e) - return 0; + return 0; for (i=0; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_EXPR) { - if (yasm_expr__traverse_leaves_in_const(e->terms[i].data.expn, d, - func)) - return 1; - } else { - if (func(&e->terms[i], d)) - return 1; - } + if (e->terms[i].type == YASM_EXPR_EXPR) { + if (yasm_expr__traverse_leaves_in_const(e->terms[i].data.expn, d, + func)) + return 1; + } else { + if (func(&e->terms[i], d)) + return 1; + } } return 0; } @@ -1160,16 +1160,16 @@ yasm_expr__traverse_leaves_in(yasm_expr *e, void *d, int i; if (!e) - return 0; + return 0; for (i=0; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_EXPR) { - if (yasm_expr__traverse_leaves_in(e->terms[i].data.expn, d, func)) - return 1; - } else { - if (func(&e->terms[i], d)) - return 1; - } + if (e->terms[i].type == YASM_EXPR_EXPR) { + if (yasm_expr__traverse_leaves_in(e->terms[i].data.expn, d, func)) + return 1; + } else { + if (func(&e->terms[i], d)) + return 1; + } } return 0; } @@ -1184,15 +1184,15 @@ yasm_expr_extract_deep_segoff(yasm_expr **ep) /* Try to extract at this level */ retval = yasm_expr_extract_segoff(ep); if (retval) - return retval; + return retval; /* Not at this level? Search any expr children. */ for (i=0; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_EXPR) { - retval = yasm_expr_extract_deep_segoff(&e->terms[i].data.expn); - if (retval) - return retval; - } + if (e->terms[i].type == YASM_EXPR_EXPR) { + retval = yasm_expr_extract_deep_segoff(&e->terms[i].data.expn); + if (retval) + return retval; + } } /* Didn't find one */ @@ -1207,23 +1207,23 @@ yasm_expr_extract_segoff(yasm_expr **ep) /* If not SEG:OFF, we can't do this transformation */ if (e->op != YASM_EXPR_SEGOFF) - return NULL; + return NULL; /* Extract the SEG portion out to its own expression */ if (e->terms[0].type == YASM_EXPR_EXPR) - retval = e->terms[0].data.expn; + retval = e->terms[0].data.expn; else { - /* Need to build IDENT expression to hold non-expression contents */ - retval = yasm_xmalloc(sizeof(yasm_expr)); - retval->op = YASM_EXPR_IDENT; - retval->numterms = 1; - retval->terms[0] = e->terms[0]; /* structure copy */ + /* Need to build IDENT expression to hold non-expression contents */ + retval = yasm_xmalloc(sizeof(yasm_expr)); + retval->op = YASM_EXPR_IDENT; + retval->numterms = 1; + retval->terms[0] = e->terms[0]; /* structure copy */ } /* Delete the SEG: portion by changing the expression into an IDENT */ e->op = YASM_EXPR_IDENT; e->numterms = 1; - e->terms[0] = e->terms[1]; /* structure copy */ + e->terms[0] = e->terms[1]; /* structure copy */ return retval; } @@ -1236,17 +1236,17 @@ yasm_expr_extract_wrt(yasm_expr **ep) /* If not WRT, we can't do this transformation */ if (e->op != YASM_EXPR_WRT) - return NULL; + return NULL; /* Extract the right side portion out to its own expression */ if (e->terms[1].type == YASM_EXPR_EXPR) - retval = e->terms[1].data.expn; + retval = e->terms[1].data.expn; else { - /* Need to build IDENT expression to hold non-expression contents */ - retval = yasm_xmalloc(sizeof(yasm_expr)); - retval->op = YASM_EXPR_IDENT; - retval->numterms = 1; - retval->terms[0] = e->terms[1]; /* structure copy */ + /* Need to build IDENT expression to hold non-expression contents */ + retval = yasm_xmalloc(sizeof(yasm_expr)); + retval->op = YASM_EXPR_IDENT; + retval->numterms = 1; + retval->terms[0] = e->terms[1]; /* structure copy */ } /* Delete the right side portion by changing the expr into an IDENT */ @@ -1263,9 +1263,9 @@ yasm_expr_get_intnum(yasm_expr **ep, int calc_bc_dist) *ep = yasm_expr_simplify(*ep, calc_bc_dist); if ((*ep)->op == YASM_EXPR_IDENT && (*ep)->terms[0].type == YASM_EXPR_INT) - return (*ep)->terms[0].data.intn; + return (*ep)->terms[0].data.intn; else - return (yasm_intnum *)NULL; + return (yasm_intnum *)NULL; } /*@=unqualifiedtrans =nullderef -nullstate -onlytrans@*/ @@ -1274,12 +1274,12 @@ const yasm_symrec * yasm_expr_get_symrec(yasm_expr **ep, int simplify) { if (simplify) - *ep = yasm_expr_simplify(*ep, 0); + *ep = yasm_expr_simplify(*ep, 0); if ((*ep)->op == YASM_EXPR_IDENT && (*ep)->terms[0].type == YASM_EXPR_SYM) - return (*ep)->terms[0].data.sym; + return (*ep)->terms[0].data.sym; else - return (yasm_symrec *)NULL; + return (yasm_symrec *)NULL; } /*@=unqualifiedtrans =nullderef -nullstate -onlytrans@*/ @@ -1288,12 +1288,12 @@ const uintptr_t * yasm_expr_get_reg(yasm_expr **ep, int simplify) { if (simplify) - *ep = yasm_expr_simplify(*ep, 0); + *ep = yasm_expr_simplify(*ep, 0); if ((*ep)->op == YASM_EXPR_IDENT && (*ep)->terms[0].type == YASM_EXPR_REG) - return &((*ep)->terms[0].data.reg); + return &((*ep)->terms[0].data.reg); else - return NULL; + return NULL; } /*@=unqualifiedtrans =nullderef -nullstate -onlytrans@*/ @@ -1304,145 +1304,145 @@ yasm_expr_print(const yasm_expr *e, FILE *f) int i; if (!e) { - fprintf(f, "(nil)"); - return; + fprintf(f, "(nil)"); + return; } switch (e->op) { - case YASM_EXPR_ADD: - strcpy(opstr, "+"); - break; - case YASM_EXPR_SUB: - strcpy(opstr, "-"); - break; - case YASM_EXPR_MUL: - strcpy(opstr, "*"); - break; - case YASM_EXPR_DIV: - strcpy(opstr, "/"); - break; - case YASM_EXPR_SIGNDIV: - strcpy(opstr, "//"); - break; - case YASM_EXPR_MOD: - strcpy(opstr, "%"); - break; - case YASM_EXPR_SIGNMOD: - strcpy(opstr, "%%"); - break; - case YASM_EXPR_NEG: - fprintf(f, "-"); - opstr[0] = 0; - break; - case YASM_EXPR_NOT: - fprintf(f, "~"); - opstr[0] = 0; - break; - case YASM_EXPR_OR: - strcpy(opstr, "|"); - break; - case YASM_EXPR_AND: - strcpy(opstr, "&"); - break; - case YASM_EXPR_XOR: - strcpy(opstr, "^"); - break; - case YASM_EXPR_XNOR: - strcpy(opstr, "XNOR"); - break; - case YASM_EXPR_NOR: - strcpy(opstr, "NOR"); - break; - case YASM_EXPR_SHL: - strcpy(opstr, "<<"); - break; - case YASM_EXPR_SHR: - strcpy(opstr, ">>"); - break; - case YASM_EXPR_LOR: - strcpy(opstr, "||"); - break; - case YASM_EXPR_LAND: - strcpy(opstr, "&&"); - break; - case YASM_EXPR_LNOT: - strcpy(opstr, "!"); - break; - case YASM_EXPR_LXOR: - strcpy(opstr, "^^"); - break; - case YASM_EXPR_LXNOR: - strcpy(opstr, "LXNOR"); - break; - case YASM_EXPR_LNOR: - strcpy(opstr, "LNOR"); - break; - case YASM_EXPR_LT: - strcpy(opstr, "<"); - break; - case YASM_EXPR_GT: - strcpy(opstr, ">"); - break; - case YASM_EXPR_LE: - strcpy(opstr, "<="); - break; - case YASM_EXPR_GE: - strcpy(opstr, ">="); - break; - case YASM_EXPR_NE: - strcpy(opstr, "!="); - break; - case YASM_EXPR_EQ: - strcpy(opstr, "=="); - break; - case YASM_EXPR_SEG: - fprintf(f, "SEG "); - opstr[0] = 0; - break; - case YASM_EXPR_WRT: - strcpy(opstr, " WRT "); - break; - case YASM_EXPR_SEGOFF: - strcpy(opstr, ":"); - break; - case YASM_EXPR_IDENT: - opstr[0] = 0; - break; - default: - strcpy(opstr, " !UNK! "); - break; + case YASM_EXPR_ADD: + strcpy(opstr, "+"); + break; + case YASM_EXPR_SUB: + strcpy(opstr, "-"); + break; + case YASM_EXPR_MUL: + strcpy(opstr, "*"); + break; + case YASM_EXPR_DIV: + strcpy(opstr, "/"); + break; + case YASM_EXPR_SIGNDIV: + strcpy(opstr, "//"); + break; + case YASM_EXPR_MOD: + strcpy(opstr, "%"); + break; + case YASM_EXPR_SIGNMOD: + strcpy(opstr, "%%"); + break; + case YASM_EXPR_NEG: + fprintf(f, "-"); + opstr[0] = 0; + break; + case YASM_EXPR_NOT: + fprintf(f, "~"); + opstr[0] = 0; + break; + case YASM_EXPR_OR: + strcpy(opstr, "|"); + break; + case YASM_EXPR_AND: + strcpy(opstr, "&"); + break; + case YASM_EXPR_XOR: + strcpy(opstr, "^"); + break; + case YASM_EXPR_XNOR: + strcpy(opstr, "XNOR"); + break; + case YASM_EXPR_NOR: + strcpy(opstr, "NOR"); + break; + case YASM_EXPR_SHL: + strcpy(opstr, "<<"); + break; + case YASM_EXPR_SHR: + strcpy(opstr, ">>"); + break; + case YASM_EXPR_LOR: + strcpy(opstr, "||"); + break; + case YASM_EXPR_LAND: + strcpy(opstr, "&&"); + break; + case YASM_EXPR_LNOT: + strcpy(opstr, "!"); + break; + case YASM_EXPR_LXOR: + strcpy(opstr, "^^"); + break; + case YASM_EXPR_LXNOR: + strcpy(opstr, "LXNOR"); + break; + case YASM_EXPR_LNOR: + strcpy(opstr, "LNOR"); + break; + case YASM_EXPR_LT: + strcpy(opstr, "<"); + break; + case YASM_EXPR_GT: + strcpy(opstr, ">"); + break; + case YASM_EXPR_LE: + strcpy(opstr, "<="); + break; + case YASM_EXPR_GE: + strcpy(opstr, ">="); + break; + case YASM_EXPR_NE: + strcpy(opstr, "!="); + break; + case YASM_EXPR_EQ: + strcpy(opstr, "=="); + break; + case YASM_EXPR_SEG: + fprintf(f, "SEG "); + opstr[0] = 0; + break; + case YASM_EXPR_WRT: + strcpy(opstr, " WRT "); + break; + case YASM_EXPR_SEGOFF: + strcpy(opstr, ":"); + break; + case YASM_EXPR_IDENT: + opstr[0] = 0; + break; + default: + strcpy(opstr, " !UNK! "); + break; } for (i=0; inumterms; i++) { - switch (e->terms[i].type) { - case YASM_EXPR_PRECBC: - fprintf(f, "{%lx}", - yasm_bc_next_offset(e->terms[i].data.precbc)); - break; - case YASM_EXPR_SYM: - fprintf(f, "%s", yasm_symrec_get_name(e->terms[i].data.sym)); - break; - case YASM_EXPR_EXPR: - fprintf(f, "("); - yasm_expr_print(e->terms[i].data.expn, f); - fprintf(f, ")"); - break; - case YASM_EXPR_INT: - yasm_intnum_print(e->terms[i].data.intn, f); - break; - case YASM_EXPR_FLOAT: - yasm_floatnum_print(e->terms[i].data.flt, f); - break; - case YASM_EXPR_REG: - /* FIXME */ - /*yasm_arch_reg_print(arch, e->terms[i].data.reg, f);*/ - break; - case YASM_EXPR_SUBST: - fprintf(f, "[%u]", e->terms[i].data.subst); - break; - case YASM_EXPR_NONE: - break; - } - if (i < e->numterms-1) - fprintf(f, "%s", opstr); + switch (e->terms[i].type) { + case YASM_EXPR_PRECBC: + fprintf(f, "{%lx}", + yasm_bc_next_offset(e->terms[i].data.precbc)); + break; + case YASM_EXPR_SYM: + fprintf(f, "%s", yasm_symrec_get_name(e->terms[i].data.sym)); + break; + case YASM_EXPR_EXPR: + fprintf(f, "("); + yasm_expr_print(e->terms[i].data.expn, f); + fprintf(f, ")"); + break; + case YASM_EXPR_INT: + yasm_intnum_print(e->terms[i].data.intn, f); + break; + case YASM_EXPR_FLOAT: + yasm_floatnum_print(e->terms[i].data.flt, f); + break; + case YASM_EXPR_REG: + /* FIXME */ + /*yasm_arch_reg_print(arch, e->terms[i].data.reg, f);*/ + break; + case YASM_EXPR_SUBST: + fprintf(f, "[%u]", e->terms[i].data.subst); + break; + case YASM_EXPR_NONE: + break; + } + if (i < e->numterms-1) + fprintf(f, "%s", opstr); } } diff --git a/libyasm/expr.h b/libyasm/expr.h index 59f0cd00..7ec2f7ac 100644 --- a/libyasm/expr.h +++ b/libyasm/expr.h @@ -38,10 +38,10 @@ typedef struct yasm_expr__item yasm_expr__item; /** Create a new expression e=a op b. - * \param op operation - * \param a expression item a - * \param b expression item b (optional depending on op) - * \param line virtual line (where expression defined) + * \param op operation + * \param a expression item a + * \param b expression item b (optional depending on op) + * \param line virtual line (where expression defined) * \return Newly allocated expression. */ /*@only@*/ yasm_expr *yasm_expr_create @@ -55,85 +55,85 @@ typedef struct yasm_expr__item yasm_expr__item; /*@only@*/ yasm_expr__item *yasm_expr_precbc(/*@keep@*/ yasm_bytecode *precbc); /** Create a new symbol expression item. - * \param sym symbol + * \param sym symbol * \return Newly allocated expression item. */ /*@only@*/ yasm_expr__item *yasm_expr_sym(/*@keep@*/ yasm_symrec *sym); /** Create a new expression expression item. - * \param e expression + * \param e expression * \return Newly allocated expression item. */ /*@only@*/ yasm_expr__item *yasm_expr_expr(/*@keep@*/ yasm_expr *e); /** Create a new intnum expression item. - * \param intn intnum + * \param intn intnum * \return Newly allocated expression item. */ /*@only@*/ yasm_expr__item *yasm_expr_int(/*@keep@*/ yasm_intnum *intn); /** Create a new floatnum expression item. - * \param flt floatnum + * \param flt floatnum * \return Newly allocated expression item. */ /*@only@*/ yasm_expr__item *yasm_expr_float(/*@keep@*/ yasm_floatnum *flt); /** Create a new register expression item. - * \param reg register + * \param reg register * \return Newly allocated expression item. */ /*@only@*/ yasm_expr__item *yasm_expr_reg(uintptr_t reg); /** Create a new expression tree e=l op r. - * \param l expression for left side of new expression - * \param o operation - * \param r expression for right side of new expression - * \param i line index + * \param l expression for left side of new expression + * \param o operation + * \param r expression for right side of new expression + * \param i line index * \return Newly allocated expression. */ #define yasm_expr_create_tree(l,o,r,i) \ yasm_expr_create ((o), yasm_expr_expr(l), yasm_expr_expr(r), i) /** Create a new expression branch e=op r. - * \param o operation - * \param r expression for right side of new expression - * \param i line index + * \param o operation + * \param r expression for right side of new expression + * \param i line index * \return Newly allocated expression. */ #define yasm_expr_create_branch(o,r,i) \ yasm_expr_create ((o), yasm_expr_expr(r), (yasm_expr__item *)NULL, i) /** Create a new expression identity e=r. - * \param r expression for identity within new expression - * \param i line index + * \param r expression for identity within new expression + * \param i line index * \return Newly allocated expression. */ #define yasm_expr_create_ident(r,i) \ yasm_expr_create (YASM_EXPR_IDENT, (r), (yasm_expr__item *)NULL, i) /** Duplicate an expression. - * \param e expression + * \param e expression * \return Newly allocated expression identical to e. */ yasm_expr *yasm_expr_copy(const yasm_expr *e); /** Destroy (free allocated memory for) an expression. - * \param e expression + * \param e expression */ void yasm_expr_destroy(/*@only@*/ /*@null@*/ yasm_expr *e); /** Determine if an expression is a specified operation (at the top level). - * \param e expression - * \param op operator + * \param e expression + * \param op operator * \return Nonzero if the expression was the specified operation at the top * level, zero otherwise. */ int yasm_expr_is_op(const yasm_expr *e, yasm_expr_op op); /** Extra transformation function for yasm_expr__level_tree(). - * \param e expression being simplified - * \param d data provided as expr_xform_extra_data to - * yasm_expr__level_tree() + * \param e expression being simplified + * \param d data provided as expr_xform_extra_data to + * yasm_expr__level_tree() * \return Transformed e. */ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) @@ -141,14 +141,14 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) /** Level an entire expression tree. * \internal - * \param e expression - * \param fold_const enable constant folding if nonzero + * \param e expression + * \param fold_const enable constant folding if nonzero * \param simplify_ident simplify identities * \param simplify_reg_mul simplify REG*1 identities - * \param calc_bc_dist nonzero if distances between bytecodes should be - * calculated, 0 if they should be left intact + * \param calc_bc_dist nonzero if distances between bytecodes should be + * calculated, 0 if they should be left intact * \param expr_xform_extra extra transformation function - * \param expr_xform_extra_data data to pass to expr_xform_extra + * \param expr_xform_extra_data data to pass to expr_xform_extra * \return Leveled expression. */ /*@only@*/ /*@null@*/ yasm_expr *yasm_expr__level_tree @@ -160,8 +160,8 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) /** Simplify an expression as much as possible. Eliminates extraneous * branches and simplifies integer-only subexpressions. Simplified version * of yasm_expr__level_tree(). - * \param e expression - * \param cbd if distance between bytecodes should be calculated + * \param e expression + * \param cbd if distance between bytecodes should be calculated * \return Simplified expression. */ #define yasm_expr_simplify(e, cbd) \ @@ -169,7 +169,7 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) /** Extract the segment portion of an expression containing SEG:OFF, leaving * the offset. - * \param ep expression (pointer to) + * \param ep expression (pointer to) * \return NULL if unable to extract a segment (expr does not contain a * YASM_EXPR_SEGOFF operator), otherwise the segment expression. * The input expression is modified such that on return, it's the @@ -178,7 +178,7 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) /*@only@*/ /*@null@*/ yasm_expr *yasm_expr_extract_deep_segoff(yasm_expr **ep); /** Extract the segment portion of a SEG:OFF expression, leaving the offset. - * \param ep expression (pointer to) + * \param ep expression (pointer to) * \return NULL if unable to extract a segment (YASM_EXPR_SEGOFF not the * top-level operator), otherwise the segment expression. The input * expression is modified such that on return, it's the offset @@ -188,7 +188,7 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) /** Extract the right portion (y) of a x WRT y expression, leaving the left * portion (x). - * \param ep expression (pointer to) + * \param ep expression (pointer to) * \return NULL if unable to extract (YASM_EXPR_WRT not the top-level * operator), otherwise the right side of the WRT expression. The * input expression is modified such that on return, it's the left side @@ -197,9 +197,9 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) /*@only@*/ /*@null@*/ yasm_expr *yasm_expr_extract_wrt(yasm_expr **ep); /** Get the integer value of an expression if it's just an integer. - * \param ep expression (pointer to) - * \param calc_bc_dist nonzero if distances between bytecodes should be - * calculated, 0 if NULL should be returned in this case + * \param ep expression (pointer to) + * \param calc_bc_dist nonzero if distances between bytecodes should be + * calculated, 0 if NULL should be returned in this case * \return NULL if the expression is too complex (contains anything other than * integers, ie floats, non-valued labels, registers); otherwise the * intnum value of the expression. @@ -208,8 +208,8 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) (yasm_expr **ep, int calc_bc_dist); /** Get the symbol value of an expression if it's just a symbol. - * \param ep expression (pointer to) - * \param simplify if nonzero, simplify the expression first + * \param ep expression (pointer to) + * \param simplify if nonzero, simplify the expression first * \return NULL if the expression is too complex; otherwise the symbol value of * the expression. */ @@ -217,8 +217,8 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) (yasm_expr **ep, int simplify); /** Get the register value of an expression if it's just a register. - * \param ep expression (pointer to) - * \param simplify if nonzero, simplify the expression first + * \param ep expression (pointer to) + * \param simplify if nonzero, simplify the expression first * \return NULL if the expression is too complex; otherwise the register value * of the expression. */ @@ -226,8 +226,8 @@ typedef /*@only@*/ yasm_expr * (*yasm_expr_xform_func) (yasm_expr **ep, int simplify); /** Print an expression. For debugging purposes. - * \param e expression - * \param f file + * \param e expression + * \param f file */ void yasm_expr_print(/*@null@*/ const yasm_expr *e, FILE *f); diff --git a/libyasm/file.c b/libyasm/file.c index 403be057..84b776fe 100644 --- a/libyasm/file.c +++ b/libyasm/file.c @@ -39,7 +39,7 @@ #include "errwarn.h" #include "file.h" -#define BSIZE 8192 /* Fill block size */ +#define BSIZE 8192 /* Fill block size */ void @@ -58,48 +58,48 @@ void yasm_scanner_delete(yasm_scanner *s) { if (s->bot) { - yasm_xfree(s->bot); - s->bot = NULL; + yasm_xfree(s->bot); + s->bot = NULL; } } int yasm_fill_helper(yasm_scanner *s, unsigned char **cursor, - size_t (*input_func) (void *d, unsigned char *buf, - size_t max), - void *input_func_data) + size_t (*input_func) (void *d, unsigned char *buf, + size_t max), + void *input_func_data) { size_t cnt; int first = 0; if (s->eof) - return 0; + return 0; cnt = s->tok - s->bot; if (cnt > 0) { - memmove(s->bot, s->tok, (size_t)(s->lim - s->tok)); - s->tok = s->bot; - s->ptr -= cnt; - *cursor -= cnt; - s->lim -= cnt; + memmove(s->bot, s->tok, (size_t)(s->lim - s->tok)); + s->tok = s->bot; + s->ptr -= cnt; + *cursor -= cnt; + s->lim -= cnt; } if (!s->bot) - first = 1; + first = 1; if ((s->top - s->lim) < BSIZE) { - unsigned char *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE); - memcpy(buf, s->tok, (size_t)(s->lim - s->tok)); - s->tok = buf; - s->ptr = &buf[s->ptr - s->bot]; - *cursor = &buf[*cursor - s->bot]; - s->lim = &buf[s->lim - s->bot]; - s->top = &s->lim[BSIZE]; - if (s->bot) - yasm_xfree(s->bot); - s->bot = buf; + unsigned char *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE); + memcpy(buf, s->tok, (size_t)(s->lim - s->tok)); + s->tok = buf; + s->ptr = &buf[s->ptr - s->bot]; + *cursor = &buf[*cursor - s->bot]; + s->lim = &buf[s->lim - s->bot]; + s->top = &s->lim[BSIZE]; + if (s->bot) + yasm_xfree(s->bot); + s->bot = buf; } if ((cnt = input_func(input_func_data, s->lim, BSIZE)) == 0) { - s->eof = &s->lim[cnt]; - *s->eof++ = '\n'; + s->eof = &s->lim[cnt]; + *s->eof++ = '\n'; } s->lim += cnt; return first; @@ -113,59 +113,59 @@ yasm_unescape_cstring(unsigned char *str, size_t *len) unsigned char t[4]; while ((size_t)(s-str)<*len) { - if (*s == '\\' && (size_t)(&s[1]-str)<*len) { - s++; - switch (*s) { - case 'b': *o = '\b'; s++; break; - case 'f': *o = '\f'; s++; break; - case 'n': *o = '\n'; s++; break; - case 'r': *o = '\r'; s++; break; - case 't': *o = '\t'; s++; break; - case 'x': - /* hex escape; grab last two digits */ - s++; - while ((size_t)(&s[2]-str)<*len && isxdigit(s[0]) - && isxdigit(s[1]) && isxdigit(s[2])) - s++; - if ((size_t)(s-str)<*len && isxdigit(*s)) { - t[0] = *s++; - t[1] = '\0'; - t[2] = '\0'; - if ((size_t)(s-str)<*len && isxdigit(*s)) - t[1] = *s++; - *o = (unsigned char)strtoul((char *)t, NULL, 16); - } else - *o = '\0'; - break; - default: - if (isdigit(*s)) { - int warn = 0; - /* octal escape */ - if (*s > '7') - warn = 1; - *o = *s++ - '0'; - if ((size_t)(s-str)<*len && isdigit(*s)) { - if (*s > '7') - warn = 1; - *o <<= 3; - *o += *s++ - '0'; - if ((size_t)(s-str)<*len && isdigit(*s)) { - if (*s > '7') - warn = 1; - *o <<= 3; - *o += *s++ - '0'; - } - } - if (warn) - yasm_warn_set(YASM_WARN_GENERAL, - N_("octal value out of range")); - } else - *o = *s++; - break; - } - o++; - } else - *o++ = *s++; + if (*s == '\\' && (size_t)(&s[1]-str)<*len) { + s++; + switch (*s) { + case 'b': *o = '\b'; s++; break; + case 'f': *o = '\f'; s++; break; + case 'n': *o = '\n'; s++; break; + case 'r': *o = '\r'; s++; break; + case 't': *o = '\t'; s++; break; + case 'x': + /* hex escape; grab last two digits */ + s++; + while ((size_t)(&s[2]-str)<*len && isxdigit(s[0]) + && isxdigit(s[1]) && isxdigit(s[2])) + s++; + if ((size_t)(s-str)<*len && isxdigit(*s)) { + t[0] = *s++; + t[1] = '\0'; + t[2] = '\0'; + if ((size_t)(s-str)<*len && isxdigit(*s)) + t[1] = *s++; + *o = (unsigned char)strtoul((char *)t, NULL, 16); + } else + *o = '\0'; + break; + default: + if (isdigit(*s)) { + int warn = 0; + /* octal escape */ + if (*s > '7') + warn = 1; + *o = *s++ - '0'; + if ((size_t)(s-str)<*len && isdigit(*s)) { + if (*s > '7') + warn = 1; + *o <<= 3; + *o += *s++ - '0'; + if ((size_t)(s-str)<*len && isdigit(*s)) { + if (*s > '7') + warn = 1; + *o <<= 3; + *o += *s++ - '0'; + } + } + if (warn) + yasm_warn_set(YASM_WARN_GENERAL, + N_("octal value out of range")); + } else + *o = *s++; + break; + } + o++; + } else + *o++ = *s++; } *len = o-str; } @@ -176,18 +176,18 @@ yasm__splitpath_unix(const char *path, /*@out@*/ const char **tail) const char *s; s = strrchr(path, '/'); if (!s) { - /* No head */ - *tail = path; - return 0; + /* No head */ + *tail = path; + return 0; } *tail = s+1; /* Strip trailing ./ on path */ while ((s-1)>=path && *(s-1) == '.' && *s == '/' - && !((s-2)>=path && *(s-2) == '.')) - s -= 2; + && !((s-2)>=path && *(s-2) == '.')) + s -= 2; /* Strip trailing slashes on path (except leading) */ while (s>path && *s == '/') - s--; + s--; /* Return length of head */ return s-path+1; } @@ -200,28 +200,28 @@ yasm__splitpath_win(const char *path, /*@out@*/ const char **tail) /* split off drive letter first, if any */ if (isalpha(path[0]) && path[1] == ':') - basepath += 2; + basepath += 2; s = basepath; while (*s != '\0') - s++; + s++; while (s >= basepath && *s != '\\' && *s != '/') - s--; + s--; if (s < basepath) { - *tail = basepath; - if (path == basepath) - return 0; /* No head */ - else - return 2; /* Drive letter is head */ + *tail = basepath; + if (path == basepath) + return 0; /* No head */ + else + return 2; /* Drive letter is head */ } *tail = s+1; /* Strip trailing .\ or ./ on path */ while ((s-1)>=basepath && *(s-1) == '.' && (*s == '/' || *s == '\\') - && !((s-2)>=basepath && *(s-2) == '.')) - s -= 2; + && !((s-2)>=basepath && *(s-2) == '.')) + s -= 2; /* Strip trailing slashes on path (except leading) */ while (s>basepath && (*s == '/' || *s == '\\')) - s--; + s--; /* Return length of head */ return s-path+1; } @@ -264,9 +264,9 @@ yasm__abspath_win(const char *path) /* Replace all / with \ */ ch = abspath; while (*ch) { - if (*ch == '/') - *ch = '\\'; - ch++; + if (*ch == '/') + *ch = '\\'; + ch++; } return abspath; @@ -280,34 +280,34 @@ yasm__combpath_unix(const char *from, const char *to) char *out; if (to[0] == '/') { - /* absolute "to" */ - out = yasm_xmalloc(strlen(to)+1); - /* Combine any double slashes when copying */ - for (j=0; *to; to++) { - if (*to == '/' && *(to+1) == '/') - continue; - out[j++] = *to; - } - out[j++] = '\0'; - return out; + /* absolute "to" */ + out = yasm_xmalloc(strlen(to)+1); + /* Combine any double slashes when copying */ + for (j=0; *to; to++) { + if (*to == '/' && *(to+1) == '/') + continue; + out[j++] = *to; + } + out[j++] = '\0'; + return out; } /* Get path component; note this strips trailing slash */ pathlen = yasm__splitpath_unix(from, &tail); - out = yasm_xmalloc(pathlen+strlen(to)+2); /* worst case maximum len */ + out = yasm_xmalloc(pathlen+strlen(to)+2); /* worst case maximum len */ /* Combine any double slashes when copying */ for (i=0, j=0; i 0 && out[pathlen-1] != '/') - out[pathlen++] = '/'; + out[pathlen++] = '/'; /* Now scan from left to right through "to", stripping off "." and ".."; * if we see "..", back up one directory in out unless last directory in @@ -318,39 +318,39 @@ yasm__combpath_unix(const char *from, const char *to) * the same as "foo"). */ for (;;) { - if (to[0] == '.' && to[1] == '/') { - to += 2; /* current directory */ - while (*to == '/') - to++; /* strip off any additional slashes */ - } else if (pathlen == 0) - break; /* no more "from" path left, we're done */ - else if (to[0] == '.' && to[1] == '.' && to[2] == '/') { - if (pathlen >= 3 && out[pathlen-1] == '/' && out[pathlen-2] == '.' - && out[pathlen-3] == '.') { - /* can't ".." against a "..", so we're done. */ - break; - } - - to += 3; /* throw away "../" */ - while (*to == '/') - to++; /* strip off any additional slashes */ - - /* and back out last directory in "out" if not already at root */ - if (pathlen > 1) { - pathlen--; /* strip off trailing '/' */ - while (pathlen > 0 && out[pathlen-1] != '/') - pathlen--; - } - } else - break; + if (to[0] == '.' && to[1] == '/') { + to += 2; /* current directory */ + while (*to == '/') + to++; /* strip off any additional slashes */ + } else if (pathlen == 0) + break; /* no more "from" path left, we're done */ + else if (to[0] == '.' && to[1] == '.' && to[2] == '/') { + if (pathlen >= 3 && out[pathlen-1] == '/' && out[pathlen-2] == '.' + && out[pathlen-3] == '.') { + /* can't ".." against a "..", so we're done. */ + break; + } + + to += 3; /* throw away "../" */ + while (*to == '/') + to++; /* strip off any additional slashes */ + + /* and back out last directory in "out" if not already at root */ + if (pathlen > 1) { + pathlen--; /* strip off trailing '/' */ + while (pathlen > 0 && out[pathlen-1] != '/') + pathlen--; + } + } else + break; } /* Copy "to" to tail of output, and we're done */ /* Combine any double slashes when copying */ for (j=pathlen; *to; to++) { - if (*to == '/' && *(to+1) == '/') - continue; - out[j++] = *to; + if (*to == '/' && *(to+1) == '/') + continue; + out[j++] = *to; } out[j++] = '\0'; @@ -365,43 +365,43 @@ yasm__combpath_win(const char *from, const char *to) char *out; if ((isalpha(to[0]) && to[1] == ':') || (to[0] == '/' || to[0] == '\\')) { - /* absolute or drive letter "to" */ - out = yasm_xmalloc(strlen(to)+1); - /* Combine any double slashes when copying */ - for (j=0; *to; to++) { - if ((*to == '/' || *to == '\\') - && (*(to+1) == '/' || *(to+1) == '\\')) - continue; - if (*to == '/') - out[j++] = '\\'; - else - out[j++] = *to; - } - out[j++] = '\0'; - return out; + /* absolute or drive letter "to" */ + out = yasm_xmalloc(strlen(to)+1); + /* Combine any double slashes when copying */ + for (j=0; *to; to++) { + if ((*to == '/' || *to == '\\') + && (*(to+1) == '/' || *(to+1) == '\\')) + continue; + if (*to == '/') + out[j++] = '\\'; + else + out[j++] = *to; + } + out[j++] = '\0'; + return out; } /* Get path component; note this strips trailing slash */ pathlen = yasm__splitpath_win(from, &tail); - out = yasm_xmalloc(pathlen+strlen(to)+2); /* worst case maximum len */ + out = yasm_xmalloc(pathlen+strlen(to)+2); /* worst case maximum len */ /* Combine any double slashes when copying */ for (i=0, j=0; i 0 && out[pathlen-1] != '\\' - && !(pathlen == 2 && isalpha(out[0]) && out[1] == ':')) - out[pathlen++] = '\\'; + && !(pathlen == 2 && isalpha(out[0]) && out[1] == ':')) + out[pathlen++] = '\\'; /* Now scan from left to right through "to", stripping off "." and ".."; * if we see "..", back up one directory in out unless last directory in @@ -412,44 +412,44 @@ yasm__combpath_win(const char *from, const char *to) * the same as "foo"). */ for (;;) { - if (to[0] == '.' && (to[1] == '/' || to[1] == '\\')) { - to += 2; /* current directory */ - while (*to == '/' || *to == '\\') - to++; /* strip off any additional slashes */ - } else if (pathlen == 0 - || (pathlen == 2 && isalpha(out[0]) && out[1] == ':')) - break; /* no more "from" path left, we're done */ - else if (to[0] == '.' && to[1] == '.' - && (to[2] == '/' || to[2] == '\\')) { - if (pathlen >= 3 && out[pathlen-1] == '\\' - && out[pathlen-2] == '.' && out[pathlen-3] == '.') { - /* can't ".." against a "..", so we're done. */ - break; - } - - to += 3; /* throw away "../" (or "..\") */ - while (*to == '/' || *to == '\\') - to++; /* strip off any additional slashes */ - - /* and back out last directory in "out" if not already at root */ - if (pathlen > 1) { - pathlen--; /* strip off trailing '/' */ - while (pathlen > 0 && out[pathlen-1] != '\\') - pathlen--; - } - } else - break; + if (to[0] == '.' && (to[1] == '/' || to[1] == '\\')) { + to += 2; /* current directory */ + while (*to == '/' || *to == '\\') + to++; /* strip off any additional slashes */ + } else if (pathlen == 0 + || (pathlen == 2 && isalpha(out[0]) && out[1] == ':')) + break; /* no more "from" path left, we're done */ + else if (to[0] == '.' && to[1] == '.' + && (to[2] == '/' || to[2] == '\\')) { + if (pathlen >= 3 && out[pathlen-1] == '\\' + && out[pathlen-2] == '.' && out[pathlen-3] == '.') { + /* can't ".." against a "..", so we're done. */ + break; + } + + to += 3; /* throw away "../" (or "..\") */ + while (*to == '/' || *to == '\\') + to++; /* strip off any additional slashes */ + + /* and back out last directory in "out" if not already at root */ + if (pathlen > 1) { + pathlen--; /* strip off trailing '/' */ + while (pathlen > 0 && out[pathlen-1] != '\\') + pathlen--; + } + } else + break; } /* Copy "to" to tail of output, and we're done */ /* Combine any double slashes when copying */ for (j=pathlen; *to; to++) { - if ((*to == '/' || *to == '\\') && (*(to+1) == '/' || *(to+1) == '\\')) - continue; - if (*to == '/') - out[j++] = '\\'; - else - out[j++] = *to; + if ((*to == '/' || *to == '\\') && (*(to+1) == '/' || *(to+1) == '\\')) + continue; + if (*to == '/') + out[j++] = '\\'; + else + out[j++] = *to; } out[j++] = '\0'; @@ -465,7 +465,7 @@ STAILQ_HEAD(, incpath) incpaths = STAILQ_HEAD_INITIALIZER(incpaths); FILE * yasm_fopen_include(const char *iname, const char *from, const char *mode, - char **oname) + char **oname) { FILE *f; char *combine; @@ -473,33 +473,33 @@ yasm_fopen_include(const char *iname, const char *from, const char *mode, /* Try directly relative to from first, then each of the include paths */ if (from) { - combine = yasm__combpath(from, iname); - f = fopen(combine, mode); - if (f) { - if (oname) - *oname = combine; - else - yasm_xfree(combine); - return f; - } - yasm_xfree(combine); + combine = yasm__combpath(from, iname); + f = fopen(combine, mode); + if (f) { + if (oname) + *oname = combine; + else + yasm_xfree(combine); + return f; + } + yasm_xfree(combine); } STAILQ_FOREACH(np, &incpaths, link) { - combine = yasm__combpath(np->path, iname); - f = fopen(combine, mode); - if (f) { - if (oname) - *oname = combine; - else - yasm_xfree(combine); - return f; - } - yasm_xfree(combine); + combine = yasm__combpath(np->path, iname); + f = fopen(combine, mode); + if (f) { + if (oname) + *oname = combine; + else + yasm_xfree(combine); + return f; + } + yasm_xfree(combine); } if (oname) - *oname = NULL; + *oname = NULL; return NULL; } @@ -510,10 +510,10 @@ yasm_delete_include_paths(void) n1 = STAILQ_FIRST(&incpaths); while (n1) { - n2 = STAILQ_NEXT(n1, link); - yasm_xfree(n1->path); - yasm_xfree(n1); - n1 = n2; + n2 = STAILQ_NEXT(n1, link); + yasm_xfree(n1->path); + yasm_xfree(n1); + n1 = n2; } STAILQ_INIT(&incpaths); } @@ -528,8 +528,8 @@ yasm_add_include_path(const char *path) memcpy(np->path, path, len+1); /* Add trailing slash if it is missing */ if (path[len-1] != '\\' && path[len-1] != '/') { - np->path[len] = '/'; - np->path[len+1] = '\0'; + np->path[len] = '/'; + np->path[len+1] = '\0'; } STAILQ_INSERT_TAIL(&incpaths, np, link); @@ -539,9 +539,9 @@ size_t yasm_fwrite_16_l(unsigned short val, FILE *f) { if (fputc(val & 0xFF, f) == EOF) - return 0; + return 0; if (fputc((val >> 8) & 0xFF, f) == EOF) - return 0; + return 0; return 1; } @@ -549,13 +549,13 @@ size_t yasm_fwrite_32_l(unsigned long val, FILE *f) { if (fputc((int)(val & 0xFF), f) == EOF) - return 0; + return 0; if (fputc((int)((val >> 8) & 0xFF), f) == EOF) - return 0; + return 0; if (fputc((int)((val >> 16) & 0xFF), f) == EOF) - return 0; + return 0; if (fputc((int)((val >> 24) & 0xFF), f) == EOF) - return 0; + return 0; return 1; } @@ -563,9 +563,9 @@ size_t yasm_fwrite_16_b(unsigned short val, FILE *f) { if (fputc((val >> 8) & 0xFF, f) == EOF) - return 0; + return 0; if (fputc(val & 0xFF, f) == EOF) - return 0; + return 0; return 1; } @@ -573,12 +573,12 @@ size_t yasm_fwrite_32_b(unsigned long val, FILE *f) { if (fputc((int)((val >> 24) & 0xFF), f) == EOF) - return 0; + return 0; if (fputc((int)((val >> 16) & 0xFF), f) == EOF) - return 0; + return 0; if (fputc((int)((val >> 8) & 0xFF), f) == EOF) - return 0; + return 0; if (fputc((int)(val & 0xFF), f) == EOF) - return 0; + return 0; return 1; } diff --git a/libyasm/file.h b/libyasm/file.h index e739cfd1..d138cf0d 100644 --- a/libyasm/file.h +++ b/libyasm/file.h @@ -36,13 +36,13 @@ /** Re2c scanner state. */ typedef struct yasm_scanner { - unsigned char *bot; /**< Bottom of scan buffer */ - unsigned char *tok; /**< Start of token */ - unsigned char *ptr; /**< Scan marker */ - unsigned char *cur; /**< Cursor (1 past end of token) */ - unsigned char *lim; /**< Limit of good data */ - unsigned char *top; /**< Top of scan buffer */ - unsigned char *eof; /**< End of file */ + unsigned char *bot; /**< Bottom of scan buffer */ + unsigned char *tok; /**< Start of token */ + unsigned char *ptr; /**< Scan marker */ + unsigned char *cur; /**< Cursor (1 past end of token) */ + unsigned char *lim; /**< Limit of good data */ + unsigned char *top; /**< Top of scan buffer */ + unsigned char *eof; /**< End of file */ } yasm_scanner; /** Initialize scanner state. @@ -56,13 +56,13 @@ void yasm_scanner_initialize(yasm_scanner *scanner); void yasm_scanner_delete(yasm_scanner *scanner); /** Fill a scanner state structure with data coming from an input function. - * \param scanner Re2c scanner state - * \param cursor Re2c scan cursor - * \param input_func Input function to read data; takes buffer and maximum - * number of bytes, returns number of bytes read. + * \param scanner Re2c scanner state + * \param cursor Re2c scan cursor + * \param input_func Input function to read data; takes buffer and maximum + * number of bytes, returns number of bytes read. * \param input_func_data Data to pass as the first parameter to input_func * \return 1 if this was the first time this function was called on this - * scanner state, 0 otherwise. + * scanner state, 0 otherwise. */ int yasm_fill_helper (yasm_scanner *scanner, unsigned char **cursor, @@ -75,16 +75,16 @@ int yasm_fill_helper * - hex escapes: reads as many hex digits as possible, takes last 2 as value. * - oct escapes: takes up to 3 digits 0-9 and scales appropriately, with * warning. - * \param str C-style string (updated in place) - * \param len length of string (updated with new length) + * \param str C-style string (updated in place) + * \param len length of string (updated with new length) */ void yasm_unescape_cstring(unsigned char *str, size_t *len); /** Split a UNIX pathname into head (directory) and tail (base filename) * portions. * \internal - * \param path pathname - * \param tail (returned) base filename + * \param path pathname + * \param tail (returned) base filename * \return Length of head (directory). */ size_t yasm__splitpath_unix(const char *path, /*@out@*/ const char **tail); @@ -92,8 +92,8 @@ size_t yasm__splitpath_unix(const char *path, /*@out@*/ const char **tail); /** Split a Windows pathname into head (directory) and tail (base filename) * portions. * \internal - * \param path pathname - * \param tail (returned) base filename + * \param path pathname + * \param tail (returned) base filename * \return Length of head (directory). */ size_t yasm__splitpath_win(const char *path, /*@out@*/ const char **tail); @@ -101,30 +101,30 @@ size_t yasm__splitpath_win(const char *path, /*@out@*/ const char **tail); /** Split a pathname into head (directory) and tail (base filename) portions. * Unless otherwise defined, defaults to yasm__splitpath_unix(). * \internal - * \param path pathname - * \param tail (returned) base filename + * \param path pathname + * \param tail (returned) base filename * \return Length of head (directory). */ #ifndef yasm__splitpath # if defined (_WIN32) || defined (WIN32) || defined (__MSDOS__) || \ defined (__DJGPP__) || defined (__OS2__) || defined (__CYGWIN__) || \ defined (__CYGWIN32__) -# define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) +# define yasm__splitpath(path, tail) yasm__splitpath_win(path, tail) # else -# define yasm__splitpath(path, tail) yasm__splitpath_unix(path, tail) +# define yasm__splitpath(path, tail) yasm__splitpath_unix(path, tail) # endif #endif /** Convert a UNIX relative or absolute pathname into an absolute pathname. * \internal - * \param path pathname + * \param path pathname * \return Absolute version of path (newly allocated). */ /*@only@*/ char *yasm__abspath_unix(const char *path); /** Convert a Windows relative or absolute pathname into an absolute pathname. * \internal - * \param path pathname + * \param path pathname * \return Absolute version of path (newly allocated). */ /*@only@*/ char *yasm__abspath_win(const char *path); @@ -132,16 +132,16 @@ size_t yasm__splitpath_win(const char *path, /*@out@*/ const char **tail); /** Convert a relative or absolute pathname into an absolute pathname. * Unless otherwise defined, defaults to yasm__abspath_unix(). * \internal - * \param path pathname + * \param path pathname * \return Absolute version of path (newly allocated). */ #ifndef yasm__abspath # if defined (_WIN32) || defined (WIN32) || defined (__MSDOS__) || \ defined (__DJGPP__) || defined (__OS2__) || defined (__CYGWIN__) || \ defined (__CYGWIN32__) -# define yasm__abspath(path) yasm__abspath_win(path) +# define yasm__abspath(path) yasm__abspath_win(path) # else -# define yasm__abspath(path) yasm__abspath_unix(path) +# define yasm__abspath(path) yasm__abspath_unix(path) # endif #endif @@ -149,8 +149,8 @@ size_t yasm__splitpath_win(const char *path, /*@out@*/ const char **tail); * when you're in the directory containing "from". Result is relative if both * from and to are relative. * \internal - * \param from from pathname - * \param to to pathname + * \param from from pathname + * \param to to pathname * \return Combined path (newly allocated). */ char *yasm__combpath_unix(const char *from, const char *to); @@ -159,8 +159,8 @@ char *yasm__combpath_unix(const char *from, const char *to); * when you're in the directory containing "from". Result is relative if both * from and to are relative. * \internal - * \param from from pathname - * \param to to pathname + * \param from from pathname + * \param to to pathname * \return Combined path (newly allocated). */ char *yasm__combpath_win(const char *from, const char *to); @@ -170,17 +170,17 @@ char *yasm__combpath_win(const char *from, const char *to); * from and to are relative. * Unless otherwise defined, defaults to yasm__combpath_unix(). * \internal - * \param from from pathname - * \param to to pathname + * \param from from pathname + * \param to to pathname * \return Combined path (newly allocated). */ #ifndef yasm__combpath # if defined (_WIN32) || defined (WIN32) || defined (__MSDOS__) || \ defined (__DJGPP__) || defined (__OS2__) || defined (__CYGWIN__) || \ defined (__CYGWIN32__) -# define yasm__combpath(from, to) yasm__combpath_win(from, to) +# define yasm__combpath(from, to) yasm__combpath_win(from, to) # else -# define yasm__combpath(from, to) yasm__combpath_unix(from, to) +# define yasm__combpath(from, to) yasm__combpath_unix(from, to) # endif #endif @@ -195,11 +195,11 @@ char *yasm__combpath_win(const char *from, const char *to); * is saved into oname, and the fopen'ed FILE * is returned. If not found, * NULL is returned. * - * \param iname file to include - * \param from file doing the including - * \param mode fopen mode string - * \param oname full pathname of included file (may be relative). NULL - * may be passed if this is unwanted. + * \param iname file to include + * \param from file doing the including + * \param mode fopen mode string + * \param oname full pathname of included file (may be relative). NULL + * may be passed if this is unwanted. * \return fopen'ed include file, or NULL if not found. */ /*@null@*/ FILE *yasm_fopen_include @@ -214,136 +214,136 @@ void yasm_delete_include_paths(void); * If path is relative, it is treated by yasm_fopen_include() as relative to * the current working directory. * - * \param path path to add + * \param path path to add */ void yasm_add_include_path(const char *path); /** Write an 8-bit value to a buffer, incrementing buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 8-bit value + * \param ptr buffer + * \param val 8-bit value */ -#define YASM_WRITE_8(ptr, val) \ - *((ptr)++) = (unsigned char)((val) & 0xFF) +#define YASM_WRITE_8(ptr, val) \ + *((ptr)++) = (unsigned char)((val) & 0xFF) /** Write a 16-bit value to a buffer in little endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_WRITE_16_L(ptr, val) \ - do { \ - *((ptr)++) = (unsigned char)((val) & 0xFF); \ - *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ - } while (0) +#define YASM_WRITE_16_L(ptr, val) \ + do { \ + *((ptr)++) = (unsigned char)((val) & 0xFF); \ + *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ + } while (0) /** Write a 32-bit value to a buffer in little endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value + * \param ptr buffer + * \param val 32-bit value */ -#define YASM_WRITE_32_L(ptr, val) \ - do { \ - *((ptr)++) = (unsigned char)((val) & 0xFF); \ - *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ - *((ptr)++) = (unsigned char)(((val) >> 16) & 0xFF); \ - *((ptr)++) = (unsigned char)(((val) >> 24) & 0xFF); \ - } while (0) +#define YASM_WRITE_32_L(ptr, val) \ + do { \ + *((ptr)++) = (unsigned char)((val) & 0xFF); \ + *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ + *((ptr)++) = (unsigned char)(((val) >> 16) & 0xFF); \ + *((ptr)++) = (unsigned char)(((val) >> 24) & 0xFF); \ + } while (0) /** Write a 16-bit value to a buffer in big endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_WRITE_16_B(ptr, val) \ - do { \ - *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ - *((ptr)++) = (unsigned char)((val) & 0xFF); \ - } while (0) +#define YASM_WRITE_16_B(ptr, val) \ + do { \ + *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ + *((ptr)++) = (unsigned char)((val) & 0xFF); \ + } while (0) /** Write a 32-bit value to a buffer in big endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value + * \param ptr buffer + * \param val 32-bit value */ -#define YASM_WRITE_32_B(ptr, val) \ - do { \ - *((ptr)++) = (unsigned char)(((val) >> 24) & 0xFF); \ - *((ptr)++) = (unsigned char)(((val) >> 16) & 0xFF); \ - *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ - *((ptr)++) = (unsigned char)((val) & 0xFF); \ - } while (0) +#define YASM_WRITE_32_B(ptr, val) \ + do { \ + *((ptr)++) = (unsigned char)(((val) >> 24) & 0xFF); \ + *((ptr)++) = (unsigned char)(((val) >> 16) & 0xFF); \ + *((ptr)++) = (unsigned char)(((val) >> 8) & 0xFF); \ + *((ptr)++) = (unsigned char)((val) & 0xFF); \ + } while (0) /** Write an 8-bit value to a buffer. Does not increment buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 8-bit value + * \param ptr buffer + * \param val 8-bit value */ -#define YASM_SAVE_8(ptr, val) \ - *(ptr) = (unsigned char)((val) & 0xFF) +#define YASM_SAVE_8(ptr, val) \ + *(ptr) = (unsigned char)((val) & 0xFF) /** Write a 16-bit value to a buffer in little endian. Does not increment * buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_SAVE_16_L(ptr, val) \ - do { \ - *(ptr) = (unsigned char)((val) & 0xFF); \ - *((ptr)+1) = (unsigned char)(((val) >> 8) & 0xFF); \ - } while (0) +#define YASM_SAVE_16_L(ptr, val) \ + do { \ + *(ptr) = (unsigned char)((val) & 0xFF); \ + *((ptr)+1) = (unsigned char)(((val) >> 8) & 0xFF); \ + } while (0) /** Write a 32-bit value to a buffer in little endian. Does not increment * buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value + * \param ptr buffer + * \param val 32-bit value */ -#define YASM_SAVE_32_L(ptr, val) \ - do { \ - *(ptr) = (unsigned char)((val) & 0xFF); \ - *((ptr)+1) = (unsigned char)(((val) >> 8) & 0xFF); \ - *((ptr)+2) = (unsigned char)(((val) >> 16) & 0xFF); \ - *((ptr)+3) = (unsigned char)(((val) >> 24) & 0xFF); \ - } while (0) +#define YASM_SAVE_32_L(ptr, val) \ + do { \ + *(ptr) = (unsigned char)((val) & 0xFF); \ + *((ptr)+1) = (unsigned char)(((val) >> 8) & 0xFF); \ + *((ptr)+2) = (unsigned char)(((val) >> 16) & 0xFF); \ + *((ptr)+3) = (unsigned char)(((val) >> 24) & 0xFF); \ + } while (0) /** Write a 16-bit value to a buffer in big endian. Does not increment buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_SAVE_16_B(ptr, val) \ - do { \ - *(ptr) = (unsigned char)(((val) >> 8) & 0xFF); \ - *((ptr)+1) = (unsigned char)((val) & 0xFF); \ - } while (0) +#define YASM_SAVE_16_B(ptr, val) \ + do { \ + *(ptr) = (unsigned char)(((val) >> 8) & 0xFF); \ + *((ptr)+1) = (unsigned char)((val) & 0xFF); \ + } while (0) /** Write a 32-bit value to a buffer in big endian. Does not increment buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value + * \param ptr buffer + * \param val 32-bit value */ -#define YASM_SAVE_32_B(ptr, val) \ - do { \ - *(ptr) = (unsigned char)(((val) >> 24) & 0xFF); \ - *((ptr)+1) = (unsigned char)(((val) >> 16) & 0xFF); \ - *((ptr)+2) = (unsigned char)(((val) >> 8) & 0xFF); \ - *((ptr)+3) = (unsigned char)((val) & 0xFF); \ - } while (0) +#define YASM_SAVE_32_B(ptr, val) \ + do { \ + *(ptr) = (unsigned char)(((val) >> 24) & 0xFF); \ + *((ptr)+1) = (unsigned char)(((val) >> 16) & 0xFF); \ + *((ptr)+2) = (unsigned char)(((val) >> 8) & 0xFF); \ + *((ptr)+3) = (unsigned char)((val) & 0xFF); \ + } while (0) /** Direct-to-file version of YASM_SAVE_16_L(). * \note Using the macro multiple times with a single fwrite() call will * probably be faster than calling this function many times. - * \param val 16-bit value - * \param f file + * \param val 16-bit value + * \param f file * \return 1 if the write was successful, 0 if not (just like fwrite()). */ size_t yasm_fwrite_16_l(unsigned short val, FILE *f); @@ -351,8 +351,8 @@ size_t yasm_fwrite_16_l(unsigned short val, FILE *f); /** Direct-to-file version of YASM_SAVE_32_L(). * \note Using the macro multiple times with a single fwrite() call will * probably be faster than calling this function many times. - * \param val 32-bit value - * \param f file + * \param val 32-bit value + * \param f file * \return 1 if the write was successful, 0 if not (just like fwrite()). */ size_t yasm_fwrite_32_l(unsigned long val, FILE *f); @@ -360,8 +360,8 @@ size_t yasm_fwrite_32_l(unsigned long val, FILE *f); /** Direct-to-file version of YASM_SAVE_16_B(). * \note Using the macro multiple times with a single fwrite() call will * probably be faster than calling this function many times. - * \param val 16-bit value - * \param f file + * \param val 16-bit value + * \param f file * \return 1 if the write was successful, 0 if not (just like fwrite()). */ size_t yasm_fwrite_16_b(unsigned short val, FILE *f); @@ -369,130 +369,130 @@ size_t yasm_fwrite_16_b(unsigned short val, FILE *f); /** Direct-to-file version of YASM_SAVE_32_B(). * \note Using the macro multiple times with a single fwrite() call will * probably be faster than calling this function many times. - * \param val 32-bit value - * \param f file + * \param val 32-bit value + * \param f file * \return 1 if the write was successful, 0 if not (just like fwrite()). */ size_t yasm_fwrite_32_b(unsigned long val, FILE *f); /** Read an 8-bit value from a buffer, incrementing buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 8-bit value + * \param ptr buffer + * \param val 8-bit value */ -#define YASM_READ_8(val, ptr) \ - (val) = *((ptr)++) & 0xFF +#define YASM_READ_8(val, ptr) \ + (val) = *((ptr)++) & 0xFF /** Read a 16-bit value from a buffer in little endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_READ_16_L(val, ptr) \ - do { \ - (val) = *((ptr)++) & 0xFF; \ - (val) |= (*((ptr)++) & 0xFF) << 8; \ - } while (0) +#define YASM_READ_16_L(val, ptr) \ + do { \ + (val) = *((ptr)++) & 0xFF; \ + (val) |= (*((ptr)++) & 0xFF) << 8; \ + } while (0) /** Read a 32-bit value from a buffer in little endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value + * \param ptr buffer + * \param val 32-bit value */ -#define YASM_READ_32_L(val, ptr) \ - do { \ - (val) = *((ptr)++) & 0xFF; \ - (val) |= (*((ptr)++) & 0xFF) << 8; \ - (val) |= (*((ptr)++) & 0xFF) << 16; \ - (val) |= (*((ptr)++) & 0xFF) << 24; \ - } while (0) +#define YASM_READ_32_L(val, ptr) \ + do { \ + (val) = *((ptr)++) & 0xFF; \ + (val) |= (*((ptr)++) & 0xFF) << 8; \ + (val) |= (*((ptr)++) & 0xFF) << 16; \ + (val) |= (*((ptr)++) & 0xFF) << 24; \ + } while (0) /** Read a 16-bit value from a buffer in big endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_READ_16_B(val, ptr) \ - do { \ - (val) = (*((ptr)++) & 0xFF) << 8; \ - (val) |= *((ptr)++) & 0xFF; \ - } while (0) +#define YASM_READ_16_B(val, ptr) \ + do { \ + (val) = (*((ptr)++) & 0xFF) << 8; \ + (val) |= *((ptr)++) & 0xFF; \ + } while (0) /** Read a 32-bit value from a buffer in big endian, incrementing buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value + * \param ptr buffer + * \param val 32-bit value */ -#define YASM_READ_32_B(val, ptr) \ - do { \ - (val) = (*((ptr)++) & 0xFF) << 24; \ - (val) |= (*((ptr)++) & 0xFF) << 16; \ - (val) |= (*((ptr)++) & 0xFF) << 8; \ - (val) |= *((ptr)++) & 0xFF; \ - } while (0) +#define YASM_READ_32_B(val, ptr) \ + do { \ + (val) = (*((ptr)++) & 0xFF) << 24; \ + (val) |= (*((ptr)++) & 0xFF) << 16; \ + (val) |= (*((ptr)++) & 0xFF) << 8; \ + (val) |= *((ptr)++) & 0xFF; \ + } while (0) /** Read an 8-bit value from a buffer. Does not increment buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 8-bit value + * \param ptr buffer + * \param val 8-bit value */ -#define YASM_LOAD_8(val, ptr) \ - (val) = *(ptr) & 0xFF +#define YASM_LOAD_8(val, ptr) \ + (val) = *(ptr) & 0xFF /** Read a 16-bit value from a buffer in little endian. Does not increment * buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_LOAD_16_L(val, ptr) \ - do { \ - (val) = *(ptr) & 0xFF; \ - (val) |= (*((ptr)+1) & 0xFF) << 8; \ - } while (0) +#define YASM_LOAD_16_L(val, ptr) \ + do { \ + (val) = *(ptr) & 0xFF; \ + (val) |= (*((ptr)+1) & 0xFF) << 8; \ + } while (0) /** Read a 32-bit value from a buffer in little endian. Does not increment * buffer pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value + * \param ptr buffer + * \param val 32-bit value */ -#define YASM_LOAD_32_L(val, ptr) \ - do { \ - (val) = (unsigned long)(*(ptr) & 0xFF); \ - (val) |= (unsigned long)((*((ptr)+1) & 0xFF) << 8); \ - (val) |= (unsigned long)((*((ptr)+2) & 0xFF) << 16); \ - (val) |= (unsigned long)((*((ptr)+3) & 0xFF) << 24); \ - } while (0) +#define YASM_LOAD_32_L(val, ptr) \ + do { \ + (val) = (unsigned long)(*(ptr) & 0xFF); \ + (val) |= (unsigned long)((*((ptr)+1) & 0xFF) << 8); \ + (val) |= (unsigned long)((*((ptr)+2) & 0xFF) << 16); \ + (val) |= (unsigned long)((*((ptr)+3) & 0xFF) << 24); \ + } while (0) /** Read a 16-bit value from a buffer in big endian. Does not increment buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 16-bit value + * \param ptr buffer + * \param val 16-bit value */ -#define YASM_LOAD_16_B(val, ptr) \ - do { \ - (val) = (*(ptr) & 0xFF) << 8; \ - (val) |= *((ptr)+1) & 0xFF; \ - } while (0) +#define YASM_LOAD_16_B(val, ptr) \ + do { \ + (val) = (*(ptr) & 0xFF) << 8; \ + (val) |= *((ptr)+1) & 0xFF; \ + } while (0) /** Read a 32-bit value from a buffer in big endian. Does not increment buffer * pointer. * \note Only works properly if ptr is an (unsigned char *). - * \param ptr buffer - * \param val 32-bit value - */ -#define YASM_LOAD_32_B(val, ptr) \ - do { \ - (val) = (unsigned long)((*(ptr) & 0xFF) << 24); \ - (val) |= (unsigned long)((*((ptr)+1) & 0xFF) << 16); \ - (val) |= (unsigned long)((*((ptr)+2) & 0xFF) << 8); \ - (val) |= (unsigned long)(*((ptr)+3) & 0xFF); \ - } while (0) + * \param ptr buffer + * \param val 32-bit value + */ +#define YASM_LOAD_32_B(val, ptr) \ + do { \ + (val) = (unsigned long)((*(ptr) & 0xFF) << 24); \ + (val) |= (unsigned long)((*((ptr)+1) & 0xFF) << 16); \ + (val) |= (unsigned long)((*((ptr)+2) & 0xFF) << 8); \ + (val) |= (unsigned long)(*((ptr)+3) & 0xFF); \ + } while (0) #endif diff --git a/libyasm/floatnum.c b/libyasm/floatnum.c index 09dddc27..9a7e02ae 100644 --- a/libyasm/floatnum.c +++ b/libyasm/floatnum.c @@ -50,24 +50,24 @@ * Mantissa does NOT have an implied one bit (it's explicit). */ struct yasm_floatnum { - /*@only@*/ wordptr mantissa; /* Allocated to MANT_BITS bits */ + /*@only@*/ wordptr mantissa; /* Allocated to MANT_BITS bits */ unsigned short exponent; unsigned char sign; unsigned char flags; }; /* constants describing parameters of internal floating point format */ -#define MANT_BITS 80 -#define MANT_BYTES 10 -#define MANT_SIGDIGITS 24 -#define EXP_BIAS 0x7FFF -#define EXP_INF 0xFFFF -#define EXP_MAX 0xFFFE -#define EXP_MIN 1 -#define EXP_ZERO 0 +#define MANT_BITS 80 +#define MANT_BYTES 10 +#define MANT_SIGDIGITS 24 +#define EXP_BIAS 0x7FFF +#define EXP_INF 0xFFFF +#define EXP_MAX 0xFFFE +#define EXP_MIN 1 +#define EXP_ZERO 0 /* Flag settings for flags field */ -#define FLAG_ISZERO 1<<0 +#define FLAG_ISZERO 1<<0 /* Note this structure integrates the floatnum structure */ typedef struct POT_Entry_s { @@ -77,8 +77,8 @@ typedef struct POT_Entry_s { /* "Source" for POT_Entry. */ typedef struct POT_Entry_Source_s { - unsigned char mantissa[MANT_BYTES]; /* little endian mantissa */ - unsigned short exponent; /* Bias 32767 exponent */ + unsigned char mantissa[MANT_BYTES]; /* little endian mantissa */ + unsigned short exponent; /* Bias 32767 exponent */ } POT_Entry_Source; /* Power of ten tables used by the floating point I/O routines. @@ -173,9 +173,9 @@ yasm_floatnum_initialize(void) /* Initialize entry[0..12] */ for (i=12; i>=0; i--) { - POT_Table_Init_Entry(&POT_TableN[i], &POT_TableN_Source[i], 0-dec_exp); - POT_Table_Init_Entry(&POT_TableP[i+1], &POT_TableP_Source[i], dec_exp); - dec_exp *= 2; /* Update decimal exponent */ + POT_Table_Init_Entry(&POT_TableN[i], &POT_TableN_Source[i], 0-dec_exp); + POT_Table_Init_Entry(&POT_TableP[i+1], &POT_TableP_Source[i], dec_exp); + dec_exp *= 2; /* Update decimal exponent */ } /* Initialize entry[13] */ @@ -200,8 +200,8 @@ yasm_floatnum_cleanup(void) POT_TableP--; for (i=0; i<14; i++) { - BitVector_Destroy(POT_TableN[i].f.mantissa); - BitVector_Destroy(POT_TableP[i].f.mantissa); + BitVector_Destroy(POT_TableN[i].f.mantissa); + BitVector_Destroy(POT_TableP[i].f.mantissa); } BitVector_Destroy(POT_TableP[14].f.mantissa); @@ -216,15 +216,15 @@ floatnum_normalize(yasm_floatnum *flt) long norm_amt; if (BitVector_is_empty(flt->mantissa)) { - flt->exponent = 0; - return; + flt->exponent = 0; + return; } /* Look for the highest set bit, shift to make it the MSB, and adjust * exponent. Don't let exponent go negative. */ norm_amt = (MANT_BITS-1)-Set_Max(flt->mantissa); if (norm_amt > (long)flt->exponent) - norm_amt = (long)flt->exponent; + norm_amt = (long)flt->exponent; BitVector_Move_Left(flt->mantissa, (N_int)norm_amt); flt->exponent -= (unsigned short)norm_amt; } @@ -242,24 +242,24 @@ floatnum_mul(yasm_floatnum *acc, const yasm_floatnum *op) /* Check for multiply by 0 */ if (BitVector_is_empty(acc->mantissa) || BitVector_is_empty(op->mantissa)) { - BitVector_Empty(acc->mantissa); - acc->exponent = EXP_ZERO; - return; + BitVector_Empty(acc->mantissa); + acc->exponent = EXP_ZERO; + return; } /* Add exponents, checking for overflow/underflow. */ expon = (((int)acc->exponent)-EXP_BIAS) + (((int)op->exponent)-EXP_BIAS); expon += EXP_BIAS; if (expon > EXP_MAX) { - /* Overflow; return infinity. */ - BitVector_Empty(acc->mantissa); - acc->exponent = EXP_INF; - return; + /* Overflow; return infinity. */ + BitVector_Empty(acc->mantissa); + acc->exponent = EXP_INF; + return; } else if (expon < EXP_MIN) { - /* Underflow; return zero. */ - BitVector_Empty(acc->mantissa); - acc->exponent = EXP_ZERO; - return; + /* Underflow; return zero. */ + BitVector_Empty(acc->mantissa); + acc->exponent = EXP_ZERO; + return; } /* Add one to the final exponent, as the multiply shifts one extra time. */ @@ -289,7 +289,7 @@ floatnum_mul(yasm_floatnum *acc, const yasm_floatnum *op) */ norm_amt = (MANT_BITS*2-1)-Set_Max(product); if (norm_amt > (long)acc->exponent) - norm_amt = (long)acc->exponent; + norm_amt = (long)acc->exponent; BitVector_Move_Left(product, (N_int)norm_amt); acc->exponent -= (unsigned short)norm_amt; @@ -306,7 +306,7 @@ yasm_floatnum * yasm_floatnum_create(const char *str) { yasm_floatnum *flt; - int dec_exponent, dec_exp_add; /* decimal (powers of 10) exponent */ + int dec_exponent, dec_exp_add; /* decimal (powers of 10) exponent */ int POT_index; wordptr operand[2]; int sig_digits; @@ -329,17 +329,17 @@ yasm_floatnum_create(const char *str) /* check for + or - character and skip */ if (*str == '-') { - flt->sign = 1; - str++; + flt->sign = 1; + str++; } else if (*str == '+') { - flt->sign = 0; - str++; + flt->sign = 0; + str++; } else - flt->sign = 0; + flt->sign = 0; /* eliminate any leading zeros (which do not count as significant digits) */ while (*str == '0') - str++; + str++; /* When we reach the end of the leading zeros, first check for a decimal * point. If the number is of the form "0---0.0000" we need to get rid @@ -347,78 +347,78 @@ yasm_floatnum_create(const char *str) * digits. */ if (*str == '.') { - str++; - while (*str == '0') { - str++; - dec_exponent--; - } + str++; + while (*str == '0') { + str++; + dec_exponent--; + } } else { - /* The number is of the form "yyy.xxxx" (where y <> 0). */ - while (isdigit(*str)) { - /* See if we've processed more than the max significant digits: */ - if (sig_digits < MANT_SIGDIGITS) { - /* Multiply mantissa by 10 [x = (x<<1)+(x<<3)] */ - BitVector_shift_left(flt->mantissa, 0); - BitVector_Copy(operand[0], flt->mantissa); - BitVector_Move_Left(flt->mantissa, 2); - carry = 0; - BitVector_add(operand[1], operand[0], flt->mantissa, &carry); - - /* Add in current digit */ - BitVector_Empty(operand[0]); - BitVector_Chunk_Store(operand[0], 4, 0, (N_long)(*str-'0')); - carry = 0; - BitVector_add(flt->mantissa, operand[1], operand[0], &carry); - } else { - /* Can't integrate more digits with mantissa, so instead just - * raise by a power of ten. - */ - dec_exponent++; - } - sig_digits++; - str++; - } - - if (*str == '.') - str++; - else - decimal_pt = 0; + /* The number is of the form "yyy.xxxx" (where y <> 0). */ + while (isdigit(*str)) { + /* See if we've processed more than the max significant digits: */ + if (sig_digits < MANT_SIGDIGITS) { + /* Multiply mantissa by 10 [x = (x<<1)+(x<<3)] */ + BitVector_shift_left(flt->mantissa, 0); + BitVector_Copy(operand[0], flt->mantissa); + BitVector_Move_Left(flt->mantissa, 2); + carry = 0; + BitVector_add(operand[1], operand[0], flt->mantissa, &carry); + + /* Add in current digit */ + BitVector_Empty(operand[0]); + BitVector_Chunk_Store(operand[0], 4, 0, (N_long)(*str-'0')); + carry = 0; + BitVector_add(flt->mantissa, operand[1], operand[0], &carry); + } else { + /* Can't integrate more digits with mantissa, so instead just + * raise by a power of ten. + */ + dec_exponent++; + } + sig_digits++; + str++; + } + + if (*str == '.') + str++; + else + decimal_pt = 0; } if (decimal_pt) { - /* Process the digits to the right of the decimal point. */ - while (isdigit(*str)) { - /* See if we've processed more than 19 significant digits: */ - if (sig_digits < 19) { - /* Raise by a power of ten */ - dec_exponent--; - - /* Multiply mantissa by 10 [x = (x<<1)+(x<<3)] */ - BitVector_shift_left(flt->mantissa, 0); - BitVector_Copy(operand[0], flt->mantissa); - BitVector_Move_Left(flt->mantissa, 2); - carry = 0; - BitVector_add(operand[1], operand[0], flt->mantissa, &carry); - - /* Add in current digit */ - BitVector_Empty(operand[0]); - BitVector_Chunk_Store(operand[0], 4, 0, (N_long)(*str-'0')); - carry = 0; - BitVector_add(flt->mantissa, operand[1], operand[0], &carry); - } - sig_digits++; - str++; - } + /* Process the digits to the right of the decimal point. */ + while (isdigit(*str)) { + /* See if we've processed more than 19 significant digits: */ + if (sig_digits < 19) { + /* Raise by a power of ten */ + dec_exponent--; + + /* Multiply mantissa by 10 [x = (x<<1)+(x<<3)] */ + BitVector_shift_left(flt->mantissa, 0); + BitVector_Copy(operand[0], flt->mantissa); + BitVector_Move_Left(flt->mantissa, 2); + carry = 0; + BitVector_add(operand[1], operand[0], flt->mantissa, &carry); + + /* Add in current digit */ + BitVector_Empty(operand[0]); + BitVector_Chunk_Store(operand[0], 4, 0, (N_long)(*str-'0')); + carry = 0; + BitVector_add(flt->mantissa, operand[1], operand[0], &carry); + } + sig_digits++; + str++; + } } if (*str == 'e' || *str == 'E') { - str++; - /* We just saw the "E" character, now read in the exponent value and - * add it into dec_exponent. - */ - dec_exp_add = 0; - sscanf(str, "%d", &dec_exp_add); - dec_exponent += dec_exp_add; + str++; + /* We just saw the "E" character, now read in the exponent value and + * add it into dec_exponent. + */ + dec_exp_add = 0; + sscanf(str, "%d", &dec_exp_add); + dec_exponent += dec_exp_add; } /* Free calculation variables. */ @@ -427,12 +427,12 @@ yasm_floatnum_create(const char *str) /* Normalize the number, checking for 0 first. */ if (BitVector_is_empty(flt->mantissa)) { - /* Mantissa is 0, zero exponent too. */ - flt->exponent = 0; - /* Set zero flag so output functions don't see 0 value as underflow. */ - flt->flags |= FLAG_ISZERO; - /* Return 0 value. */ - return flt; + /* Mantissa is 0, zero exponent too. */ + flt->exponent = 0; + /* Set zero flag so output functions don't see 0 value as underflow. */ + flt->flags |= FLAG_ISZERO; + /* Return 0 value. */ + return flt; } /* Exponent if already norm. */ flt->exponent = (unsigned short)(0x7FFF+(MANT_BITS-1)); @@ -443,48 +443,48 @@ yasm_floatnum_create(const char *str) * up this operation (and make it more accurate). */ if (dec_exponent > 0) { - POT_index = 0; - /* Until we hit 1.0 or finish exponent or overflow */ - while ((POT_index < 14) && (dec_exponent != 0) && - (flt->exponent != EXP_INF)) { - /* Find the first power of ten in the table which is just less than - * the exponent. - */ - while (dec_exponent < POT_TableP[POT_index].dec_exponent) - POT_index++; - - if (POT_index < 14) { - /* Subtract out what we're multiplying in from exponent */ - dec_exponent -= POT_TableP[POT_index].dec_exponent; - - /* Multiply by current power of 10 */ - floatnum_mul(flt, &POT_TableP[POT_index].f); - } - } + POT_index = 0; + /* Until we hit 1.0 or finish exponent or overflow */ + while ((POT_index < 14) && (dec_exponent != 0) && + (flt->exponent != EXP_INF)) { + /* Find the first power of ten in the table which is just less than + * the exponent. + */ + while (dec_exponent < POT_TableP[POT_index].dec_exponent) + POT_index++; + + if (POT_index < 14) { + /* Subtract out what we're multiplying in from exponent */ + dec_exponent -= POT_TableP[POT_index].dec_exponent; + + /* Multiply by current power of 10 */ + floatnum_mul(flt, &POT_TableP[POT_index].f); + } + } } else if (dec_exponent < 0) { - POT_index = 0; - /* Until we hit 1.0 or finish exponent or underflow */ - while ((POT_index < 14) && (dec_exponent != 0) && - (flt->exponent != EXP_ZERO)) { - /* Find the first power of ten in the table which is just less than - * the exponent. - */ - while (dec_exponent > POT_TableN[POT_index].dec_exponent) - POT_index++; - - if (POT_index < 14) { - /* Subtract out what we're multiplying in from exponent */ - dec_exponent -= POT_TableN[POT_index].dec_exponent; - - /* Multiply by current power of 10 */ - floatnum_mul(flt, &POT_TableN[POT_index].f); - } - } + POT_index = 0; + /* Until we hit 1.0 or finish exponent or underflow */ + while ((POT_index < 14) && (dec_exponent != 0) && + (flt->exponent != EXP_ZERO)) { + /* Find the first power of ten in the table which is just less than + * the exponent. + */ + while (dec_exponent > POT_TableN[POT_index].dec_exponent) + POT_index++; + + if (POT_index < 14) { + /* Subtract out what we're multiplying in from exponent */ + dec_exponent -= POT_TableN[POT_index].dec_exponent; + + /* Multiply by current power of 10 */ + floatnum_mul(flt, &POT_TableN[POT_index].f); + } + } } /* Round the result. (Don't round underflow or overflow). */ if ((flt->exponent != EXP_INF) && (flt->exponent != EXP_ZERO)) - BitVector_increment(flt->mantissa); + BitVector_increment(flt->mantissa); return flt; } @@ -511,12 +511,12 @@ yasm_floatnum_destroy(yasm_floatnum *flt) int yasm_floatnum_calc(yasm_floatnum *acc, yasm_expr_op op, - /*@unused@*/ yasm_floatnum *operand) + /*@unused@*/ yasm_floatnum *operand) { if (op != YASM_EXPR_NEG) { - yasm_error_set(YASM_ERROR_FLOATING_POINT, - N_("Unsupported floating-point arithmetic operation")); - return 1; + yasm_error_set(YASM_ERROR_FLOATING_POINT, + N_("Unsupported floating-point arithmetic operation")); + return 1; } acc->sign ^= 1; return 0; @@ -528,8 +528,8 @@ yasm_floatnum_get_int(const yasm_floatnum *flt, unsigned long *ret_val) unsigned char t[4]; if (yasm_floatnum_get_sized(flt, t, 4, 32, 0, 0, 0)) { - *ret_val = 0xDEADBEEFUL; /* Obviously incorrect return value */ - return 1; + *ret_val = 0xDEADBEEFUL; /* Obviously incorrect return value */ + return 1; } YASM_LOAD_32_L(*ret_val, &t[0]); @@ -549,8 +549,8 @@ yasm_floatnum_get_int(const yasm_floatnum *flt, unsigned long *ret_val) */ static int floatnum_get_common(const yasm_floatnum *flt, /*@out@*/ unsigned char *ptr, - N_int byte_size, N_int mant_bits, int implicit1, - N_int exp_bits) + N_int byte_size, N_int mant_bits, int implicit1, + N_int exp_bits) { long exponent = (long)flt->exponent; wordptr output; @@ -565,45 +565,45 @@ floatnum_get_common(const yasm_floatnum *flt, /*@out@*/ unsigned char *ptr, /* copy mantissa */ BitVector_Interval_Copy(output, flt->mantissa, 0, - (N_int)((MANT_BITS-implicit1)-mant_bits), - mant_bits); + (N_int)((MANT_BITS-implicit1)-mant_bits), + mant_bits); /* round mantissa */ if (BitVector_bit_test(flt->mantissa, (MANT_BITS-implicit1)-(mant_bits+1))) - BitVector_increment(output); + BitVector_increment(output); if (BitVector_bit_test(output, mant_bits)) { - /* overflowed, so zero mantissa (and set explicit bit if necessary) */ - BitVector_Empty(output); - BitVector_Bit_Copy(output, mant_bits-1, !implicit1); - /* and up the exponent (checking for overflow) */ - if (exponent+1 >= EXP_INF) - overflow = 1; - else - exponent++; + /* overflowed, so zero mantissa (and set explicit bit if necessary) */ + BitVector_Empty(output); + BitVector_Bit_Copy(output, mant_bits-1, !implicit1); + /* and up the exponent (checking for overflow) */ + if (exponent+1 >= EXP_INF) + overflow = 1; + else + exponent++; } /* adjust the exponent to the output bias, checking for overflow */ exponent -= EXP_BIAS-exp_bias; if (exponent >= exp_inf) - overflow = 1; + overflow = 1; else if (exponent <= 0) - underflow = 1; + underflow = 1; /* underflow and overflow both set!? */ if (underflow && overflow) - yasm_internal_error(N_("Both underflow and overflow set")); + yasm_internal_error(N_("Both underflow and overflow set")); /* check for underflow or overflow and set up appropriate output */ if (underflow) { - BitVector_Empty(output); - exponent = 0; - if (!(flt->flags & FLAG_ISZERO)) - retval = -1; + BitVector_Empty(output); + exponent = 0; + if (!(flt->flags & FLAG_ISZERO)) + retval = -1; } else if (overflow) { - BitVector_Empty(output); - exponent = exp_inf; - retval = 1; + BitVector_Empty(output); + exponent = exp_inf; + retval = 1; } /* move exponent into place */ @@ -615,8 +615,8 @@ floatnum_get_common(const yasm_floatnum *flt, /*@out@*/ unsigned char *ptr, /* get little-endian bytes */ buf = BitVector_Block_Read(output, &len); if (len < byte_size) - yasm_internal_error( - N_("Byte length of BitVector does not match bit length")); + yasm_internal_error( + N_("Byte length of BitVector does not match bit length")); /* copy to output */ memcpy(ptr, buf, byte_size*sizeof(unsigned char)); @@ -661,36 +661,36 @@ floatnum_get_common(const yasm_floatnum *flt, /*@out@*/ unsigned char *ptr, */ int yasm_floatnum_get_sized(const yasm_floatnum *flt, unsigned char *ptr, - size_t destsize, size_t valsize, size_t shift, - int bigendian, int warn) + size_t destsize, size_t valsize, size_t shift, + int bigendian, int warn) { int retval; if (destsize*8 != valsize || shift>0 || bigendian) { - /* TODO */ - yasm_internal_error(N_("unsupported floatnum functionality")); + /* TODO */ + yasm_internal_error(N_("unsupported floatnum functionality")); } switch (destsize) { - case 4: - retval = floatnum_get_common(flt, ptr, 4, 23, 1, 8); - break; - case 8: - retval = floatnum_get_common(flt, ptr, 8, 52, 1, 11); - break; - case 10: - retval = floatnum_get_common(flt, ptr, 10, 64, 0, 15); - break; - default: - yasm_internal_error(N_("Invalid float conversion size")); - /*@notreached@*/ - return 1; + case 4: + retval = floatnum_get_common(flt, ptr, 4, 23, 1, 8); + break; + case 8: + retval = floatnum_get_common(flt, ptr, 8, 52, 1, 11); + break; + case 10: + retval = floatnum_get_common(flt, ptr, 10, 64, 0, 15); + break; + default: + yasm_internal_error(N_("Invalid float conversion size")); + /*@notreached@*/ + return 1; } if (warn) { - if (retval < 0) - yasm_warn_set(YASM_WARN_GENERAL, - N_("underflow in floating point expression")); - else if (retval > 0) - yasm_warn_set(YASM_WARN_GENERAL, - N_("overflow in floating point expression")); + if (retval < 0) + yasm_warn_set(YASM_WARN_GENERAL, + N_("underflow in floating point expression")); + else if (retval > 0) + yasm_warn_set(YASM_WARN_GENERAL, + N_("overflow in floating point expression")); } return retval; } @@ -700,12 +700,12 @@ int yasm_floatnum_check_size(/*@unused@*/ const yasm_floatnum *flt, size_t size) { switch (size) { - case 32: - case 64: - case 80: - return 1; - default: - return 0; + case 32: + case 64: + case 80: + return 1; + default: + return 0; } } @@ -719,27 +719,27 @@ yasm_floatnum_print(const yasm_floatnum *flt, FILE *f) /* Internal format */ str = BitVector_to_Hex(flt->mantissa); fprintf(f, "%c %s *2^%04x\n", flt->sign?'-':'+', (char *)str, - flt->exponent); + flt->exponent); yasm_xfree(str); /* 32-bit (single precision) format */ fprintf(f, "32-bit: %d: ", - yasm_floatnum_get_sized(flt, out, 4, 32, 0, 0, 0)); + yasm_floatnum_get_sized(flt, out, 4, 32, 0, 0, 0)); for (i=0; i<4; i++) - fprintf(f, "%02x ", out[i]); + fprintf(f, "%02x ", out[i]); fprintf(f, "\n"); /* 64-bit (double precision) format */ fprintf(f, "64-bit: %d: ", - yasm_floatnum_get_sized(flt, out, 8, 64, 0, 0, 0)); + yasm_floatnum_get_sized(flt, out, 8, 64, 0, 0, 0)); for (i=0; i<8; i++) - fprintf(f, "%02x ", out[i]); + fprintf(f, "%02x ", out[i]); fprintf(f, "\n"); /* 80-bit (extended precision) format */ fprintf(f, "80-bit: %d: ", - yasm_floatnum_get_sized(flt, out, 10, 80, 0, 0, 0)); + yasm_floatnum_get_sized(flt, out, 10, 80, 0, 0, 0)); for (i=0; i<10; i++) - fprintf(f, "%02x ", out[i]); + fprintf(f, "%02x ", out[i]); fprintf(f, "\n"); } diff --git a/libyasm/floatnum.h b/libyasm/floatnum.h index 3e488f10..e7cbea3e 100644 --- a/libyasm/floatnum.h +++ b/libyasm/floatnum.h @@ -44,42 +44,42 @@ void yasm_floatnum_cleanup(void); /** Create a new floatnum from a decimal string. The input string must be in * standard C representation ([+-]123.456e[-+]789). - * \param str floating point decimal string + * \param str floating point decimal string * \return Newly allocated floatnum. */ /*@only@*/ yasm_floatnum *yasm_floatnum_create(const char *str); /** Duplicate a floatnum. - * \param flt floatnum + * \param flt floatnum * \return Newly allocated floatnum with the same value as flt. */ /*@only@*/ yasm_floatnum *yasm_floatnum_copy(const yasm_floatnum *flt); /** Destroy (free allocated memory for) a floatnum. - * \param flt floatnum + * \param flt floatnum */ void yasm_floatnum_destroy(/*@only@*/ yasm_floatnum *flt); /** Floating point calculation function: acc = acc op operand. * \note Not all operations in yasm_expr_op may be supported; unsupported * operations will result in an error. - * \param acc floatnum accumulator - * \param op operation + * \param acc floatnum accumulator + * \param op operation * \param operand floatnum operand * \return Nonzero on error. */ int yasm_floatnum_calc(yasm_floatnum *acc, yasm_expr_op op, - yasm_floatnum *operand); + yasm_floatnum *operand); /** Convert a floatnum to single-precision and return as 32-bit value. * The 32-bit value is a "standard" C value (eg, of unknown endian). - * \param flt floatnum + * \param flt floatnum * \param ret_val pointer to storage for 32-bit output * \return Nonzero if flt can't fit into single precision: -1 if underflow * occurred, 1 if overflow occurred. */ int yasm_floatnum_get_int(const yasm_floatnum *flt, - /*@out@*/ unsigned long *ret_val); + /*@out@*/ unsigned long *ret_val); /** Output a #yasm_floatnum to buffer in little-endian or big-endian. Puts the * value into the least significant bits of the destination, or may be shifted @@ -88,33 +88,33 @@ int yasm_floatnum_get_int(const yasm_floatnum *flt, * \note Not all sizes are valid. Currently, only 32 (single-precision), 64 * (double-precision), and 80 (extended-precision) are valid sizes. * Use yasm_floatnum_check_size() to check for supported sizes. - * \param flt floatnum - * \param ptr pointer to storage for size bytes of output + * \param flt floatnum + * \param ptr pointer to storage for size bytes of output * \param destsize destination size (in bytes) * \param valsize size (in bits) - * \param shift left shift (in bits) + * \param shift left shift (in bits) * \param bigendian endianness (nonzero=big, zero=little) - * \param warn enables standard overflow/underflow warnings + * \param warn enables standard overflow/underflow warnings * \return Nonzero if flt can't fit into the specified precision: -1 if * underflow occurred, 1 if overflow occurred. */ int yasm_floatnum_get_sized(const yasm_floatnum *flt, unsigned char *ptr, - size_t destsize, size_t valsize, size_t shift, - int bigendian, int warn); + size_t destsize, size_t valsize, size_t shift, + int bigendian, int warn); /** Basic check to see if size is valid for flt conversion (using * yasm_floatnum_get_sized()). Doesn't actually check for underflow/overflow * but rather checks for size=32,64,80 * (at present). - * \param flt floatnum - * \param size number of bits of output space + * \param flt floatnum + * \param size number of bits of output space * \return 1 if valid size, 0 if invalid size. */ int yasm_floatnum_check_size(const yasm_floatnum *flt, size_t size); /** Print various representations of a floatnum. For debugging purposes only. - * \param f file - * \param flt floatnum + * \param f file + * \param flt floatnum */ void yasm_floatnum_print(const yasm_floatnum *flt, FILE *f); diff --git a/libyasm/genmodule.c b/libyasm/genmodule.c index ee35c830..a3088c50 100644 --- a/libyasm/genmodule.c +++ b/libyasm/genmodule.c @@ -32,9 +32,9 @@ #include "compat-queue.h" -#define OUTPUT "module.c" +#define OUTPUT "module.c" #define MAXNAME 128 -#define MAXLINE 1024 +#define MAXLINE 1024 #define MAXMODULES 128 #define MAXINCLUDES 256 @@ -54,14 +54,14 @@ main(int argc, char *argv[]) char *modules[MAXMODULES]; int num_modules = 0; STAILQ_HEAD(includehead, include) includes = - STAILQ_HEAD_INITIALIZER(includes); + STAILQ_HEAD_INITIALIZER(includes); include *inc; int isam = 0; int linecont = 0; if (argc != 3) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return EXIT_FAILURE; + fprintf(stderr, "Usage: %s \n", argv[0]); + return EXIT_FAILURE; } str = malloc(MAXLINE); @@ -79,149 +79,149 @@ main(int argc, char *argv[]) isam = argv[2][len-2] == 'a' && argv[2][len-1] == 'm'; while (!STAILQ_EMPTY(&includes)) { - inc = STAILQ_FIRST(&includes); - STAILQ_REMOVE_HEAD(&includes, link); - in = fopen(inc->filename, "rt"); - if (!in) { - fprintf(stderr, "Could not open `%s'.\n", inc->filename); - return EXIT_FAILURE; - } - free(inc->filename); - free(inc); - - while (fgets(str, MAXLINE, in)) { - /* Strip off any trailing whitespace */ - len = strlen(str); - if (len > 0) { - strp = &str[len-1]; - while (len > 0 && isspace(*strp)) { - *strp-- = '\0'; - len--; - } - } - - strp = str; - - /* Skip whitespace */ - while (isspace(*strp)) - strp++; - - /* Skip comments */ - if (*strp == '#') - continue; - - /* If line continuation, skip to continue copy */ - if (linecont) - goto keepgoing; - - /* Check for include if original input is .am file */ - if (isam && strncmp(strp, "include", 7) == 0 && isspace(strp[7])) { - strp += 7; - while (isspace(*strp)) - strp++; - /* Build new include and add to end of list */ - inc = malloc(sizeof(include)); - inc->filename = malloc(strlen(strp)+1); - strcpy(inc->filename, strp); - STAILQ_INSERT_TAIL(&includes, inc, link); - continue; - } - - /* Check for YASM_MODULES = or += */ - if (strncmp(strp, "YASM_MODULES", 12) != 0) - continue; - strp += 12; - while (isspace(*strp)) - strp++; - if (strncmp(strp, "+=", 2) != 0 && *strp != '=') - continue; - if (*strp == '+') - strp++; - strp++; - while (isspace(*strp)) - strp++; + inc = STAILQ_FIRST(&includes); + STAILQ_REMOVE_HEAD(&includes, link); + in = fopen(inc->filename, "rt"); + if (!in) { + fprintf(stderr, "Could not open `%s'.\n", inc->filename); + return EXIT_FAILURE; + } + free(inc->filename); + free(inc); + + while (fgets(str, MAXLINE, in)) { + /* Strip off any trailing whitespace */ + len = strlen(str); + if (len > 0) { + strp = &str[len-1]; + while (len > 0 && isspace(*strp)) { + *strp-- = '\0'; + len--; + } + } + + strp = str; + + /* Skip whitespace */ + while (isspace(*strp)) + strp++; + + /* Skip comments */ + if (*strp == '#') + continue; + + /* If line continuation, skip to continue copy */ + if (linecont) + goto keepgoing; + + /* Check for include if original input is .am file */ + if (isam && strncmp(strp, "include", 7) == 0 && isspace(strp[7])) { + strp += 7; + while (isspace(*strp)) + strp++; + /* Build new include and add to end of list */ + inc = malloc(sizeof(include)); + inc->filename = malloc(strlen(strp)+1); + strcpy(inc->filename, strp); + STAILQ_INSERT_TAIL(&includes, inc, link); + continue; + } + + /* Check for YASM_MODULES = or += */ + if (strncmp(strp, "YASM_MODULES", 12) != 0) + continue; + strp += 12; + while (isspace(*strp)) + strp++; + if (strncmp(strp, "+=", 2) != 0 && *strp != '=') + continue; + if (*strp == '+') + strp++; + strp++; + while (isspace(*strp)) + strp++; keepgoing: - /* Check for continuation */ - if (len > 0 && str[len-1] == '\\') { - str[len-1] = '\0'; - while (isspace(*strp)) - *strp-- = '\0'; - linecont = 1; - } else - linecont = 0; - - while (*strp != '\0') { - /* Copy module name */ - modules[num_modules] = malloc(MAXNAME); - len = 0; - while (*strp != '\0' && !isspace(*strp)) - modules[num_modules][len++] = *strp++; - modules[num_modules][len] = '\0'; - num_modules++; - - while (isspace(*strp)) - strp++; - } - } - fclose(in); + /* Check for continuation */ + if (len > 0 && str[len-1] == '\\') { + str[len-1] = '\0'; + while (isspace(*strp)) + *strp-- = '\0'; + linecont = 1; + } else + linecont = 0; + + while (*strp != '\0') { + /* Copy module name */ + modules[num_modules] = malloc(MAXNAME); + len = 0; + while (*strp != '\0' && !isspace(*strp)) + modules[num_modules][len++] = *strp++; + modules[num_modules][len] = '\0'; + num_modules++; + + while (isspace(*strp)) + strp++; + } + } + fclose(in); } out = fopen(OUTPUT, "wt"); if (!out) { - fprintf(stderr, "Could not open `%s'.\n", OUTPUT); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s'.\n", OUTPUT); + return EXIT_FAILURE; } fprintf(out, "/* This file auto-generated by genmodule.c" - " - don't edit it */\n\n"); + " - don't edit it */\n\n"); in = fopen(argv[1], "rt"); if (!in) { - fprintf(stderr, "Could not open `%s'.\n", argv[1]); - fclose(out); - remove(OUTPUT); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s'.\n", argv[1]); + fclose(out); + remove(OUTPUT); + return EXIT_FAILURE; } len = 0; while (fgets(str, MAXLINE, in)) { - if (strncmp(str, "MODULES_", 8) == 0) { - len = 0; - strp = str+8; - while (*strp != '\0' && *strp != '_') { - len++; - strp++; - } - *strp = '\0'; - - for (i=0; iBaseValue & 1) -#define SetSubTrie(h, n, v) do { \ - if ((uintptr_t)(v) & 1) \ - h->error_func(__FILE__, __LINE__, \ - N_("Subtrie is seen as subtrie before flag is set (misaligned?)")); \ - (n)->BaseValue = (uintptr_t)(v) | 1; \ +#define IsSubTrie(n) ((n)->BaseValue & 1) +#define SetSubTrie(h, n, v) do { \ + if ((uintptr_t)(v) & 1) \ + h->error_func(__FILE__, __LINE__, \ + N_("Subtrie is seen as subtrie before flag is set (misaligned?)")); \ + (n)->BaseValue = (uintptr_t)(v) | 1; \ } while (0) -#define SetValue(h, n, v) do { \ - if ((uintptr_t)(v) & 1) \ - h->error_func(__FILE__, __LINE__, \ - N_("Value is seen as subtrie (misaligned?)")); \ - (n)->BaseValue = (uintptr_t)(v); \ +#define SetValue(h, n, v) do { \ + if ((uintptr_t)(v) & 1) \ + h->error_func(__FILE__, __LINE__, \ + N_("Value is seen as subtrie (misaligned?)")); \ + (n)->BaseValue = (uintptr_t)(v); \ } while (0) -#define GetSubTrie(n) (HAMTNode *)(((n)->BaseValue | 1) ^ 1) +#define GetSubTrie(n) (HAMTNode *)(((n)->BaseValue | 1) ^ 1) static unsigned long HashKey(const char *key) { unsigned long a=31415, b=27183, vHash; for (vHash=0; *key; key++, a*=b) - vHash = a*vHash + *key; + vHash = a*vHash + *key; return vHash; } @@ -92,7 +92,7 @@ ReHashKey(const char *key, int Level) { unsigned long a=31415, b=27183, vHash; for (vHash=0; *key; key++, a*=b) - vHash = a*vHash*(unsigned long)Level + *key; + vHash = a*vHash*(unsigned long)Level + *key; return vHash; } @@ -101,7 +101,7 @@ HashKey_nocase(const char *key) { unsigned long a=31415, b=27183, vHash; for (vHash=0; *key; key++, a*=b) - vHash = a*vHash + tolower(*key); + vHash = a*vHash + tolower(*key); return vHash; } @@ -110,7 +110,7 @@ ReHashKey_nocase(const char *key, int Level) { unsigned long a=31415, b=27183, vHash; for (vHash=0; *key; key++, a*=b) - vHash = a*vHash*(unsigned long)Level + tolower(*key); + vHash = a*vHash*(unsigned long)Level + tolower(*key); return vHash; } @@ -125,17 +125,17 @@ HAMT_create(int nocase, /*@exits@*/ void (*error_func) hamt->root = yasm_xmalloc(32*sizeof(HAMTNode)); for (i=0; i<32; i++) { - hamt->root[i].BitMapKey = 0; - hamt->root[i].BaseValue = 0; + hamt->root[i].BitMapKey = 0; + hamt->root[i].BaseValue = 0; } hamt->error_func = error_func; if (nocase) { - hamt->HashKey = HashKey_nocase; - hamt->ReHashKey = ReHashKey_nocase; + hamt->HashKey = HashKey_nocase; + hamt->ReHashKey = ReHashKey_nocase; } else { - hamt->HashKey = HashKey; - hamt->ReHashKey = ReHashKey; + hamt->HashKey = HashKey; + hamt->ReHashKey = ReHashKey; } return hamt; @@ -145,17 +145,17 @@ static void HAMT_delete_trie(HAMTNode *node) { if (IsSubTrie(node)) { - unsigned long i, Size; + unsigned long i, Size; - /* Count total number of bits in bitmap to determine size */ - BitCount(Size, node->BitMapKey); - Size &= 0x1F; - if (Size == 0) - Size = 32; + /* Count total number of bits in bitmap to determine size */ + BitCount(Size, node->BitMapKey); + Size &= 0x1F; + if (Size == 0) + Size = 32; - for (i=0; ientries)) { - HAMTEntry *entry; - entry = STAILQ_FIRST(&hamt->entries); - STAILQ_REMOVE_HEAD(&hamt->entries, next); - deletefunc(entry->data); - yasm_xfree(entry); + HAMTEntry *entry; + entry = STAILQ_FIRST(&hamt->entries); + STAILQ_REMOVE_HEAD(&hamt->entries, next); + deletefunc(entry->data); + yasm_xfree(entry); } /* delete trie */ for (i=0; i<32; i++) - HAMT_delete_trie(&hamt->root[i]); + HAMT_delete_trie(&hamt->root[i]); yasm_xfree(hamt->root); yasm_xfree(hamt); @@ -183,14 +183,14 @@ HAMT_destroy(HAMT *hamt, void (*deletefunc) (/*@only@*/ void *data)) int HAMT_traverse(HAMT *hamt, void *d, - int (*func) (/*@dependent@*/ /*@null@*/ void *node, - /*@null@*/ void *d)) + int (*func) (/*@dependent@*/ /*@null@*/ void *node, + /*@null@*/ void *d)) { HAMTEntry *entry; STAILQ_FOREACH(entry, &hamt->entries, next) { - int retval = func(entry->data, d); - if (retval != 0) - return retval; + int retval = func(entry->data, d); + if (retval != 0) + return retval; } return 0; } @@ -216,7 +216,7 @@ HAMTEntry_get_data(const HAMTEntry *entry) /*@-temptrans -kepttrans -mustfree@*/ void * HAMT_insert(HAMT *hamt, const char *str, void *data, int *replace, - void (*deletefunc) (/*@only@*/ void *data)) + void (*deletefunc) (/*@only@*/ void *data)) { HAMTNode *node, *newnodes; HAMTEntry *entry; @@ -229,141 +229,141 @@ HAMT_insert(HAMT *hamt, const char *str, void *data, int *replace, node = &hamt->root[keypart]; if (!node->BaseValue) { - node->BitMapKey = key; - entry = yasm_xmalloc(sizeof(HAMTEntry)); - entry->str = str; - entry->data = data; - STAILQ_INSERT_TAIL(&hamt->entries, entry, next); - SetValue(hamt, node, entry); - if (IsSubTrie(node)) - hamt->error_func(__FILE__, __LINE__, - N_("Data is seen as subtrie (misaligned?)")); - *replace = 1; - return data; + node->BitMapKey = key; + entry = yasm_xmalloc(sizeof(HAMTEntry)); + entry->str = str; + entry->data = data; + STAILQ_INSERT_TAIL(&hamt->entries, entry, next); + SetValue(hamt, node, entry); + if (IsSubTrie(node)) + hamt->error_func(__FILE__, __LINE__, + N_("Data is seen as subtrie (misaligned?)")); + *replace = 1; + return data; } for (;;) { - if (!(IsSubTrie(node))) { - if (node->BitMapKey == key - && strcmp(((HAMTEntry *)(node->BaseValue))->str, str) == 0) { - /*@-branchstate@*/ - if (*replace) { - deletefunc(((HAMTEntry *)(node->BaseValue))->data); - ((HAMTEntry *)(node->BaseValue))->str = str; - ((HAMTEntry *)(node->BaseValue))->data = data; - } else - deletefunc(data); - /*@=branchstate@*/ - return ((HAMTEntry *)(node->BaseValue))->data; - } else { - unsigned long key2 = node->BitMapKey; - /* build tree downward until keys differ */ - for (;;) { - unsigned long keypart2; - - /* replace node with subtrie */ - keypartbits += 5; - if (keypartbits > 30) { - /* Exceeded 32 bits: rehash */ - key = hamt->ReHashKey(str, level); - key2 = hamt->ReHashKey( - ((HAMTEntry *)(node->BaseValue))->str, level); - keypartbits = 0; - } - keypart = (key >> keypartbits) & 0x1F; - keypart2 = (key2 >> keypartbits) & 0x1F; - - if (keypart == keypart2) { - /* Still equal, build one-node subtrie and continue - * downward. - */ - newnodes = yasm_xmalloc(sizeof(HAMTNode)); - newnodes[0] = *node; /* structure copy */ - node->BitMapKey = 1<str = str; - entry->data = data; - STAILQ_INSERT_TAIL(&hamt->entries, entry, next); - - /* Copy nodes into subtrie based on order */ - if (keypart2 < keypart) { - newnodes[0] = *node; /* structure copy */ - newnodes[1].BitMapKey = key; - SetValue(hamt, &newnodes[1], entry); - } else { - newnodes[0].BitMapKey = key; - SetValue(hamt, &newnodes[0], entry); - newnodes[1] = *node; /* structure copy */ - } - - /* Set bits in bitmap corresponding to keys */ - node->BitMapKey = (1UL< 30) { - /* Exceeded 32 bits of current key: rehash */ - key = hamt->ReHashKey(str, level); - keypartbits = 0; - } - keypart = (key >> keypartbits) & 0x1F; - if (!(node->BitMapKey & (1< add node to table */ - unsigned long Size; - - /* set bit to 1 */ - node->BitMapKey |= 1<BitMapKey); - Size &= 0x1F; - if (Size == 0) - Size = 32; - newnodes = yasm_xmalloc(Size*sizeof(HAMTNode)); - - /* Count bits below to find where to insert new node at */ - BitCount(Map, node->BitMapKey & ~((~0UL)<str = str; - entry->data = data; - STAILQ_INSERT_TAIL(&hamt->entries, entry, next); - SetValue(hamt, &newnodes[Map], entry); - SetSubTrie(hamt, node, newnodes); - - *replace = 1; - return data; - } - - /* Count bits below */ - BitCount(Map, node->BitMapKey & ~((~0UL)<BitMapKey == key + && strcmp(((HAMTEntry *)(node->BaseValue))->str, str) == 0) { + /*@-branchstate@*/ + if (*replace) { + deletefunc(((HAMTEntry *)(node->BaseValue))->data); + ((HAMTEntry *)(node->BaseValue))->str = str; + ((HAMTEntry *)(node->BaseValue))->data = data; + } else + deletefunc(data); + /*@=branchstate@*/ + return ((HAMTEntry *)(node->BaseValue))->data; + } else { + unsigned long key2 = node->BitMapKey; + /* build tree downward until keys differ */ + for (;;) { + unsigned long keypart2; + + /* replace node with subtrie */ + keypartbits += 5; + if (keypartbits > 30) { + /* Exceeded 32 bits: rehash */ + key = hamt->ReHashKey(str, level); + key2 = hamt->ReHashKey( + ((HAMTEntry *)(node->BaseValue))->str, level); + keypartbits = 0; + } + keypart = (key >> keypartbits) & 0x1F; + keypart2 = (key2 >> keypartbits) & 0x1F; + + if (keypart == keypart2) { + /* Still equal, build one-node subtrie and continue + * downward. + */ + newnodes = yasm_xmalloc(sizeof(HAMTNode)); + newnodes[0] = *node; /* structure copy */ + node->BitMapKey = 1<str = str; + entry->data = data; + STAILQ_INSERT_TAIL(&hamt->entries, entry, next); + + /* Copy nodes into subtrie based on order */ + if (keypart2 < keypart) { + newnodes[0] = *node; /* structure copy */ + newnodes[1].BitMapKey = key; + SetValue(hamt, &newnodes[1], entry); + } else { + newnodes[0].BitMapKey = key; + SetValue(hamt, &newnodes[0], entry); + newnodes[1] = *node; /* structure copy */ + } + + /* Set bits in bitmap corresponding to keys */ + node->BitMapKey = (1UL< 30) { + /* Exceeded 32 bits of current key: rehash */ + key = hamt->ReHashKey(str, level); + keypartbits = 0; + } + keypart = (key >> keypartbits) & 0x1F; + if (!(node->BitMapKey & (1< add node to table */ + unsigned long Size; + + /* set bit to 1 */ + node->BitMapKey |= 1<BitMapKey); + Size &= 0x1F; + if (Size == 0) + Size = 32; + newnodes = yasm_xmalloc(Size*sizeof(HAMTNode)); + + /* Count bits below to find where to insert new node at */ + BitCount(Map, node->BitMapKey & ~((~0UL)<str = str; + entry->data = data; + STAILQ_INSERT_TAIL(&hamt->entries, entry, next); + SetValue(hamt, &newnodes[Map], entry); + SetSubTrie(hamt, node, newnodes); + + *replace = 1; + return data; + } + + /* Count bits below */ + BitCount(Map, node->BitMapKey & ~((~0UL)<root[keypart]; if (!node->BaseValue) - return NULL; + return NULL; for (;;) { - if (!(IsSubTrie(node))) { - if (node->BitMapKey == key) - return ((HAMTEntry *)(node->BaseValue))->data; - else - return NULL; - } - - /* Subtree: look up in bitmap */ - keypartbits += 5; - if (keypartbits > 30) { - /* Exceeded 32 bits of current key: rehash */ - key = hamt->ReHashKey(str, level); - keypartbits = 0; - } - keypart = (key >> keypartbits) & 0x1F; - if (!(node->BitMapKey & (1< no match */ - - /* Count bits below */ - BitCount(Map, node->BitMapKey & ~((~0UL)<BitMapKey == key) + return ((HAMTEntry *)(node->BaseValue))->data; + else + return NULL; + } + + /* Subtree: look up in bitmap */ + keypartbits += 5; + if (keypartbits > 30) { + /* Exceeded 32 bits of current key: rehash */ + key = hamt->ReHashKey(str, level); + keypartbits = 0; + } + keypart = (key >> keypartbits) & 0x1F; + if (!(node->BitMapKey & (1< no match */ + + /* Count bits below */ + BitCount(Map, node->BitMapKey & ~((~0UL)<32 bits) */ + unsigned long ul; /* integer value (for integers <=32 bits) */ + wordptr bv; /* bit vector (for integers >32 bits) */ } val; enum { INTNUM_UL, INTNUM_BV } type; }; @@ -87,23 +87,23 @@ yasm_intnum_create_dec(char *str) yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum)); switch (BitVector_from_Dec_static(from_dec_data, conv_bv, - (unsigned char *)str)) { - case ErrCode_Pars: - yasm_error_set(YASM_ERROR_VALUE, N_("invalid decimal literal")); - break; - case ErrCode_Ovfl: - yasm_error_set(YASM_ERROR_OVERFLOW, - N_("Numeric constant too large for internal format")); - break; - default: - break; + (unsigned char *)str)) { + case ErrCode_Pars: + yasm_error_set(YASM_ERROR_VALUE, N_("invalid decimal literal")); + break; + case ErrCode_Ovfl: + yasm_error_set(YASM_ERROR_OVERFLOW, + N_("Numeric constant too large for internal format")); + break; + default: + break; } if (Set_Max(conv_bv) < 32) { - intn->type = INTNUM_UL; - intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); + intn->type = INTNUM_UL; + intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); } else { - intn->type = INTNUM_BV; - intn->val.bv = BitVector_Clone(conv_bv); + intn->type = INTNUM_BV; + intn->val.bv = BitVector_Clone(conv_bv); } return intn; @@ -115,22 +115,22 @@ yasm_intnum_create_bin(char *str) yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum)); switch (BitVector_from_Bin(conv_bv, (unsigned char *)str)) { - case ErrCode_Pars: - yasm_error_set(YASM_ERROR_VALUE, N_("invalid binary literal")); - break; - case ErrCode_Ovfl: - yasm_error_set(YASM_ERROR_OVERFLOW, - N_("Numeric constant too large for internal format")); - break; - default: - break; + case ErrCode_Pars: + yasm_error_set(YASM_ERROR_VALUE, N_("invalid binary literal")); + break; + case ErrCode_Ovfl: + yasm_error_set(YASM_ERROR_OVERFLOW, + N_("Numeric constant too large for internal format")); + break; + default: + break; } if (Set_Max(conv_bv) < 32) { - intn->type = INTNUM_UL; - intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); + intn->type = INTNUM_UL; + intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); } else { - intn->type = INTNUM_BV; - intn->val.bv = BitVector_Clone(conv_bv); + intn->type = INTNUM_BV; + intn->val.bv = BitVector_Clone(conv_bv); } return intn; @@ -142,22 +142,22 @@ yasm_intnum_create_oct(char *str) yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum)); switch (BitVector_from_Oct(conv_bv, (unsigned char *)str)) { - case ErrCode_Pars: - yasm_error_set(YASM_ERROR_VALUE, N_("invalid octal literal")); - break; - case ErrCode_Ovfl: - yasm_error_set(YASM_ERROR_OVERFLOW, - N_("Numeric constant too large for internal format")); - break; - default: - break; + case ErrCode_Pars: + yasm_error_set(YASM_ERROR_VALUE, N_("invalid octal literal")); + break; + case ErrCode_Ovfl: + yasm_error_set(YASM_ERROR_OVERFLOW, + N_("Numeric constant too large for internal format")); + break; + default: + break; } if (Set_Max(conv_bv) < 32) { - intn->type = INTNUM_UL; - intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); + intn->type = INTNUM_UL; + intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); } else { - intn->type = INTNUM_BV; - intn->val.bv = BitVector_Clone(conv_bv); + intn->type = INTNUM_BV; + intn->val.bv = BitVector_Clone(conv_bv); } return intn; @@ -169,22 +169,22 @@ yasm_intnum_create_hex(char *str) yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum)); switch (BitVector_from_Hex(conv_bv, (unsigned char *)str)) { - case ErrCode_Pars: - yasm_error_set(YASM_ERROR_VALUE, N_("invalid hex literal")); - break; - case ErrCode_Ovfl: - yasm_error_set(YASM_ERROR_OVERFLOW, - N_("Numeric constant too large for internal format")); - break; - default: - break; + case ErrCode_Pars: + yasm_error_set(YASM_ERROR_VALUE, N_("invalid hex literal")); + break; + case ErrCode_Ovfl: + yasm_error_set(YASM_ERROR_OVERFLOW, + N_("Numeric constant too large for internal format")); + break; + default: + break; } if (Set_Max(conv_bv) < 32) { - intn->type = INTNUM_UL; - intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); + intn->type = INTNUM_UL; + intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); } else { - intn->type = INTNUM_BV; - intn->val.bv = BitVector_Clone(conv_bv); + intn->type = INTNUM_BV; + intn->val.bv = BitVector_Clone(conv_bv); } return intn; @@ -198,42 +198,42 @@ yasm_intnum_create_charconst_nasm(const char *str) size_t len = strlen(str); if(len*8 > BITVECT_NATIVE_SIZE) - yasm_error_set(YASM_ERROR_OVERFLOW, - N_("Character constant too large for internal format")); + yasm_error_set(YASM_ERROR_OVERFLOW, + N_("Character constant too large for internal format")); if (len > 4) { - BitVector_Empty(conv_bv); - intn->type = INTNUM_BV; + BitVector_Empty(conv_bv); + intn->type = INTNUM_BV; } else { - intn->val.ul = 0; - intn->type = INTNUM_UL; + intn->val.ul = 0; + intn->type = INTNUM_UL; } switch (len) { - case 4: - intn->val.ul |= ((unsigned long)str[3]) & 0xff; - intn->val.ul <<= 8; - /*@fallthrough@*/ - case 3: - intn->val.ul |= ((unsigned long)str[2]) & 0xff; - intn->val.ul <<= 8; - /*@fallthrough@*/ - case 2: - intn->val.ul |= ((unsigned long)str[1]) & 0xff; - intn->val.ul <<= 8; - /*@fallthrough@*/ - case 1: - intn->val.ul |= ((unsigned long)str[0]) & 0xff; - case 0: - break; - default: - /* >32 bit conversion */ - while (len) { - BitVector_Move_Left(conv_bv, 8); - BitVector_Chunk_Store(conv_bv, 8, 0, - (unsigned long)str[--len]); - } - intn->val.bv = BitVector_Clone(conv_bv); + case 4: + intn->val.ul |= ((unsigned long)str[3]) & 0xff; + intn->val.ul <<= 8; + /*@fallthrough@*/ + case 3: + intn->val.ul |= ((unsigned long)str[2]) & 0xff; + intn->val.ul <<= 8; + /*@fallthrough@*/ + case 2: + intn->val.ul |= ((unsigned long)str[1]) & 0xff; + intn->val.ul <<= 8; + /*@fallthrough@*/ + case 1: + intn->val.ul |= ((unsigned long)str[0]) & 0xff; + case 0: + break; + default: + /* >32 bit conversion */ + while (len) { + BitVector_Move_Left(conv_bv, 8); + BitVector_Chunk_Store(conv_bv, 8, 0, + (unsigned long)str[--len]); + } + intn->val.bv = BitVector_Clone(conv_bv); } return intn; @@ -258,7 +258,7 @@ yasm_intnum_create_int(long i) /* positive numbers can go through the uint() function */ if (i >= 0) - return yasm_intnum_create_uint((unsigned long)i); + return yasm_intnum_create_uint((unsigned long)i); BitVector_Empty(conv_bv); BitVector_Chunk_Store(conv_bv, 32, 0, (unsigned long)(-i)); @@ -273,7 +273,7 @@ yasm_intnum_create_int(long i) yasm_intnum * yasm_intnum_create_leb128(const unsigned char *ptr, int sign, - unsigned long *size) + unsigned long *size) { yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum)); const unsigned char *ptr_orig = ptr; @@ -281,27 +281,27 @@ yasm_intnum_create_leb128(const unsigned char *ptr, int sign, BitVector_Empty(conv_bv); for (;;) { - BitVector_Chunk_Store(conv_bv, 7, i, *ptr); - i += 7; - if ((*ptr & 0x80) != 0x80) - break; - ptr++; + BitVector_Chunk_Store(conv_bv, 7, i, *ptr); + i += 7; + if ((*ptr & 0x80) != 0x80) + break; + ptr++; } *size = (unsigned long)(ptr-ptr_orig)+1; if(i > BITVECT_NATIVE_SIZE) - yasm_error_set(YASM_ERROR_OVERFLOW, - N_("Numeric constant too large for internal format")); + yasm_error_set(YASM_ERROR_OVERFLOW, + N_("Numeric constant too large for internal format")); else if (sign && (*ptr & 0x40) == 0x40) - BitVector_Interval_Fill(conv_bv, i, BITVECT_NATIVE_SIZE-1); + BitVector_Interval_Fill(conv_bv, i, BITVECT_NATIVE_SIZE-1); if (Set_Max(conv_bv) < 32) { - intn->type = INTNUM_UL; - intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); + intn->type = INTNUM_UL; + intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); } else { - intn->type = INTNUM_BV; - intn->val.bv = BitVector_Clone(conv_bv); + intn->type = INTNUM_BV; + intn->val.bv = BitVector_Clone(conv_bv); } return intn; @@ -309,35 +309,35 @@ yasm_intnum_create_leb128(const unsigned char *ptr, int sign, yasm_intnum * yasm_intnum_create_sized(unsigned char *ptr, int sign, size_t srcsize, - int bigendian) + int bigendian) { yasm_intnum *intn = yasm_xmalloc(sizeof(yasm_intnum)); unsigned long i = 0; if (srcsize*8 > BITVECT_NATIVE_SIZE) - yasm_error_set(YASM_ERROR_OVERFLOW, - N_("Numeric constant too large for internal format")); + yasm_error_set(YASM_ERROR_OVERFLOW, + N_("Numeric constant too large for internal format")); /* Read the buffer into a bitvect */ BitVector_Empty(conv_bv); if (bigendian) { - /* TODO */ - yasm_internal_error(N_("big endian not implemented")); + /* TODO */ + yasm_internal_error(N_("big endian not implemented")); } else { - for (i = 0; i < srcsize; i++) - BitVector_Chunk_Store(conv_bv, 8, i*8, ptr[i]); + for (i = 0; i < srcsize; i++) + BitVector_Chunk_Store(conv_bv, 8, i*8, ptr[i]); } /* Sign extend if needed */ if (srcsize*8 < BITVECT_NATIVE_SIZE && sign && (ptr[i] & 0x80) == 0x80) - BitVector_Interval_Fill(conv_bv, i*8, BITVECT_NATIVE_SIZE-1); + BitVector_Interval_Fill(conv_bv, i*8, BITVECT_NATIVE_SIZE-1); if (Set_Max(conv_bv) < 32) { - intn->type = INTNUM_UL; - intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); + intn->type = INTNUM_UL; + intn->val.ul = BitVector_Chunk_Read(conv_bv, 32, 0); } else { - intn->type = INTNUM_BV; - intn->val.bv = BitVector_Clone(conv_bv); + intn->type = INTNUM_BV; + intn->val.bv = BitVector_Clone(conv_bv); } return intn; @@ -349,12 +349,12 @@ yasm_intnum_copy(const yasm_intnum *intn) yasm_intnum *n = yasm_xmalloc(sizeof(yasm_intnum)); switch (intn->type) { - case INTNUM_UL: - n->val.ul = intn->val.ul; - break; - case INTNUM_BV: - n->val.bv = BitVector_Clone(intn->val.bv); - break; + case INTNUM_UL: + n->val.ul = intn->val.ul; + break; + case INTNUM_BV: + n->val.bv = BitVector_Clone(intn->val.bv); + break; } n->type = intn->type; @@ -365,7 +365,7 @@ void yasm_intnum_destroy(yasm_intnum *intn) { if (intn->type == INTNUM_BV) - BitVector_Destroy(intn->val.bv); + BitVector_Destroy(intn->val.bv); yasm_xfree(intn); } @@ -381,206 +381,206 @@ yasm_intnum_calc(yasm_intnum *acc, yasm_expr_op op, yasm_intnum *operand) * Bit vector results must be calculated through intermediate storage. */ if (acc->type == INTNUM_BV) - op1 = acc->val.bv; + op1 = acc->val.bv; else { - op1 = op1static; - BitVector_Empty(op1); - BitVector_Chunk_Store(op1, 32, 0, acc->val.ul); + op1 = op1static; + BitVector_Empty(op1); + BitVector_Chunk_Store(op1, 32, 0, acc->val.ul); } if (operand) { - if (operand->type == INTNUM_BV) - op2 = operand->val.bv; - else { - op2 = op2static; - BitVector_Empty(op2); - BitVector_Chunk_Store(op2, 32, 0, operand->val.ul); - } + if (operand->type == INTNUM_BV) + op2 = operand->val.bv; + else { + op2 = op2static; + BitVector_Empty(op2); + BitVector_Chunk_Store(op2, 32, 0, operand->val.ul); + } } if (!operand && op != YASM_EXPR_NEG && op != YASM_EXPR_NOT && - op != YASM_EXPR_LNOT) { - yasm_error_set(YASM_ERROR_ARITHMETIC, - N_("operation needs an operand")); - BitVector_Empty(result); - return 1; + op != YASM_EXPR_LNOT) { + yasm_error_set(YASM_ERROR_ARITHMETIC, + N_("operation needs an operand")); + BitVector_Empty(result); + return 1; } /* A operation does a bitvector computation if result is allocated. */ switch (op) { - case YASM_EXPR_ADD: - BitVector_add(result, op1, op2, &carry); - break; - case YASM_EXPR_SUB: - BitVector_sub(result, op1, op2, &carry); - break; - case YASM_EXPR_MUL: - BitVector_Multiply(result, op1, op2); - break; - case YASM_EXPR_DIV: - /* TODO: make sure op1 and op2 are unsigned */ - if (BitVector_is_empty(op2)) { - yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); - BitVector_Empty(result); - return 1; - } else - BitVector_Divide(result, op1, op2, spare); - break; - case YASM_EXPR_SIGNDIV: - if (BitVector_is_empty(op2)) { - yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); - BitVector_Empty(result); - return 1; - } else - BitVector_Divide(result, op1, op2, spare); - break; - case YASM_EXPR_MOD: - /* TODO: make sure op1 and op2 are unsigned */ - if (BitVector_is_empty(op2)) { - yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); - BitVector_Empty(result); - return 1; - } else - BitVector_Divide(spare, op1, op2, result); - break; - case YASM_EXPR_SIGNMOD: - if (BitVector_is_empty(op2)) { - yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); - BitVector_Empty(result); - return 1; - } else - BitVector_Divide(spare, op1, op2, result); - break; - case YASM_EXPR_NEG: - BitVector_Negate(result, op1); - break; - case YASM_EXPR_NOT: - Set_Complement(result, op1); - break; - case YASM_EXPR_OR: - Set_Union(result, op1, op2); - break; - case YASM_EXPR_AND: - Set_Intersection(result, op1, op2); - break; - case YASM_EXPR_XOR: - Set_ExclusiveOr(result, op1, op2); - break; - case YASM_EXPR_XNOR: - Set_ExclusiveOr(result, op1, op2); - Set_Complement(result, result); - break; - case YASM_EXPR_NOR: - Set_Union(result, op1, op2); - Set_Complement(result, result); - break; - case YASM_EXPR_SHL: - if (operand->type == INTNUM_UL) { - BitVector_Copy(result, op1); - BitVector_Move_Left(result, (N_int)operand->val.ul); - } else /* don't even bother, just zero result */ - BitVector_Empty(result); - break; - case YASM_EXPR_SHR: - if (operand->type == INTNUM_UL) { - BitVector_Copy(result, op1); - carry = BitVector_msb_(op1); - count = (N_int)operand->val.ul; - while (count-- > 0) - BitVector_shift_right(result, carry); - } else /* don't even bother, just zero result */ - BitVector_Empty(result); - break; - case YASM_EXPR_LOR: - BitVector_Empty(result); - BitVector_LSB(result, !BitVector_is_empty(op1) || - !BitVector_is_empty(op2)); - break; - case YASM_EXPR_LAND: - BitVector_Empty(result); - BitVector_LSB(result, !BitVector_is_empty(op1) && - !BitVector_is_empty(op2)); - break; - case YASM_EXPR_LNOT: - BitVector_Empty(result); - BitVector_LSB(result, BitVector_is_empty(op1)); - break; - case YASM_EXPR_LXOR: - BitVector_Empty(result); - BitVector_LSB(result, !BitVector_is_empty(op1) ^ - !BitVector_is_empty(op2)); - break; - case YASM_EXPR_LXNOR: - BitVector_Empty(result); - BitVector_LSB(result, !(!BitVector_is_empty(op1) ^ - !BitVector_is_empty(op2))); - break; - case YASM_EXPR_LNOR: - BitVector_Empty(result); - BitVector_LSB(result, !(!BitVector_is_empty(op1) || - !BitVector_is_empty(op2))); - break; - case YASM_EXPR_EQ: - BitVector_Empty(result); - BitVector_LSB(result, BitVector_equal(op1, op2)); - break; - case YASM_EXPR_LT: - BitVector_Empty(result); - BitVector_LSB(result, BitVector_Compare(op1, op2) < 0); - break; - case YASM_EXPR_GT: - BitVector_Empty(result); - BitVector_LSB(result, BitVector_Compare(op1, op2) > 0); - break; - case YASM_EXPR_LE: - BitVector_Empty(result); - BitVector_LSB(result, BitVector_Compare(op1, op2) <= 0); - break; - case YASM_EXPR_GE: - BitVector_Empty(result); - BitVector_LSB(result, BitVector_Compare(op1, op2) >= 0); - break; - case YASM_EXPR_NE: - BitVector_Empty(result); - BitVector_LSB(result, !BitVector_equal(op1, op2)); - break; - case YASM_EXPR_SEG: - yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"), - "SEG"); - break; - case YASM_EXPR_WRT: - yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"), - "WRT"); - break; - case YASM_EXPR_SEGOFF: - yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"), - ":"); - break; - case YASM_EXPR_IDENT: - if (result) - BitVector_Copy(result, op1); - break; - default: - yasm_error_set(YASM_ERROR_ARITHMETIC, - N_("invalid operation in intnum calculation")); - BitVector_Empty(result); - return 1; + case YASM_EXPR_ADD: + BitVector_add(result, op1, op2, &carry); + break; + case YASM_EXPR_SUB: + BitVector_sub(result, op1, op2, &carry); + break; + case YASM_EXPR_MUL: + BitVector_Multiply(result, op1, op2); + break; + case YASM_EXPR_DIV: + /* TODO: make sure op1 and op2 are unsigned */ + if (BitVector_is_empty(op2)) { + yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); + BitVector_Empty(result); + return 1; + } else + BitVector_Divide(result, op1, op2, spare); + break; + case YASM_EXPR_SIGNDIV: + if (BitVector_is_empty(op2)) { + yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); + BitVector_Empty(result); + return 1; + } else + BitVector_Divide(result, op1, op2, spare); + break; + case YASM_EXPR_MOD: + /* TODO: make sure op1 and op2 are unsigned */ + if (BitVector_is_empty(op2)) { + yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); + BitVector_Empty(result); + return 1; + } else + BitVector_Divide(spare, op1, op2, result); + break; + case YASM_EXPR_SIGNMOD: + if (BitVector_is_empty(op2)) { + yasm_error_set(YASM_ERROR_ZERO_DIVISION, N_("divide by zero")); + BitVector_Empty(result); + return 1; + } else + BitVector_Divide(spare, op1, op2, result); + break; + case YASM_EXPR_NEG: + BitVector_Negate(result, op1); + break; + case YASM_EXPR_NOT: + Set_Complement(result, op1); + break; + case YASM_EXPR_OR: + Set_Union(result, op1, op2); + break; + case YASM_EXPR_AND: + Set_Intersection(result, op1, op2); + break; + case YASM_EXPR_XOR: + Set_ExclusiveOr(result, op1, op2); + break; + case YASM_EXPR_XNOR: + Set_ExclusiveOr(result, op1, op2); + Set_Complement(result, result); + break; + case YASM_EXPR_NOR: + Set_Union(result, op1, op2); + Set_Complement(result, result); + break; + case YASM_EXPR_SHL: + if (operand->type == INTNUM_UL) { + BitVector_Copy(result, op1); + BitVector_Move_Left(result, (N_int)operand->val.ul); + } else /* don't even bother, just zero result */ + BitVector_Empty(result); + break; + case YASM_EXPR_SHR: + if (operand->type == INTNUM_UL) { + BitVector_Copy(result, op1); + carry = BitVector_msb_(op1); + count = (N_int)operand->val.ul; + while (count-- > 0) + BitVector_shift_right(result, carry); + } else /* don't even bother, just zero result */ + BitVector_Empty(result); + break; + case YASM_EXPR_LOR: + BitVector_Empty(result); + BitVector_LSB(result, !BitVector_is_empty(op1) || + !BitVector_is_empty(op2)); + break; + case YASM_EXPR_LAND: + BitVector_Empty(result); + BitVector_LSB(result, !BitVector_is_empty(op1) && + !BitVector_is_empty(op2)); + break; + case YASM_EXPR_LNOT: + BitVector_Empty(result); + BitVector_LSB(result, BitVector_is_empty(op1)); + break; + case YASM_EXPR_LXOR: + BitVector_Empty(result); + BitVector_LSB(result, !BitVector_is_empty(op1) ^ + !BitVector_is_empty(op2)); + break; + case YASM_EXPR_LXNOR: + BitVector_Empty(result); + BitVector_LSB(result, !(!BitVector_is_empty(op1) ^ + !BitVector_is_empty(op2))); + break; + case YASM_EXPR_LNOR: + BitVector_Empty(result); + BitVector_LSB(result, !(!BitVector_is_empty(op1) || + !BitVector_is_empty(op2))); + break; + case YASM_EXPR_EQ: + BitVector_Empty(result); + BitVector_LSB(result, BitVector_equal(op1, op2)); + break; + case YASM_EXPR_LT: + BitVector_Empty(result); + BitVector_LSB(result, BitVector_Compare(op1, op2) < 0); + break; + case YASM_EXPR_GT: + BitVector_Empty(result); + BitVector_LSB(result, BitVector_Compare(op1, op2) > 0); + break; + case YASM_EXPR_LE: + BitVector_Empty(result); + BitVector_LSB(result, BitVector_Compare(op1, op2) <= 0); + break; + case YASM_EXPR_GE: + BitVector_Empty(result); + BitVector_LSB(result, BitVector_Compare(op1, op2) >= 0); + break; + case YASM_EXPR_NE: + BitVector_Empty(result); + BitVector_LSB(result, !BitVector_equal(op1, op2)); + break; + case YASM_EXPR_SEG: + yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"), + "SEG"); + break; + case YASM_EXPR_WRT: + yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"), + "WRT"); + break; + case YASM_EXPR_SEGOFF: + yasm_error_set(YASM_ERROR_ARITHMETIC, N_("invalid use of '%s'"), + ":"); + break; + case YASM_EXPR_IDENT: + if (result) + BitVector_Copy(result, op1); + break; + default: + yasm_error_set(YASM_ERROR_ARITHMETIC, + N_("invalid operation in intnum calculation")); + BitVector_Empty(result); + return 1; } /* Try to fit the result into 32 bits if possible */ if (Set_Max(result) < 32) { - if (acc->type == INTNUM_BV) { - BitVector_Destroy(acc->val.bv); - acc->type = INTNUM_UL; - } - acc->val.ul = BitVector_Chunk_Read(result, 32, 0); + if (acc->type == INTNUM_BV) { + BitVector_Destroy(acc->val.bv); + acc->type = INTNUM_UL; + } + acc->val.ul = BitVector_Chunk_Read(result, 32, 0); } else { - if (acc->type == INTNUM_BV) { - BitVector_Copy(acc->val.bv, result); - } else { - acc->type = INTNUM_BV; - acc->val.bv = BitVector_Clone(result); - } + if (acc->type == INTNUM_BV) { + BitVector_Copy(acc->val.bv, result); + } else { + acc->type = INTNUM_BV; + acc->val.bv = BitVector_Clone(result); + } } return 0; } @@ -596,8 +596,8 @@ void yasm_intnum_set_uint(yasm_intnum *intn, unsigned long val) { if (intn->type == INTNUM_BV) { - BitVector_Destroy(intn->val.bv); - intn->type = INTNUM_UL; + BitVector_Destroy(intn->val.bv); + intn->type = INTNUM_UL; } intn->val.ul = val; } @@ -607,8 +607,8 @@ yasm_intnum_set_int(yasm_intnum *intn, long val) { /* positive numbers can go through the uint() function */ if (val >= 0) { - yasm_intnum_set_uint(intn, (unsigned long)val); - return; + yasm_intnum_set_uint(intn, (unsigned long)val); + return; } BitVector_Empty(conv_bv); @@ -616,10 +616,10 @@ yasm_intnum_set_int(yasm_intnum *intn, long val) BitVector_Negate(conv_bv, conv_bv); if (intn->type == INTNUM_BV) - BitVector_Copy(intn->val.bv, conv_bv); + BitVector_Copy(intn->val.bv, conv_bv); else { - intn->val.bv = BitVector_Clone(conv_bv); - intn->type = INTNUM_BV; + intn->val.bv = BitVector_Clone(conv_bv); + intn->type = INTNUM_BV; } } @@ -645,26 +645,26 @@ int yasm_intnum_sign(const yasm_intnum *intn) { if (intn->type == INTNUM_UL) { - if (intn->val.ul == 0) - return 0; - else - return 1; + if (intn->val.ul == 0) + return 0; + else + return 1; } else - return BitVector_Sign(intn->val.bv); + return BitVector_Sign(intn->val.bv); } unsigned long yasm_intnum_get_uint(const yasm_intnum *intn) { switch (intn->type) { - case INTNUM_UL: - return intn->val.ul; - case INTNUM_BV: - return BitVector_Chunk_Read(intn->val.bv, 32, 0); - default: - yasm_internal_error(N_("unknown intnum type")); - /*@notreached@*/ - return 0; + case INTNUM_UL: + return intn->val.ul; + case INTNUM_BV: + return BitVector_Chunk_Read(intn->val.bv, 32, 0); + default: + yasm_internal_error(N_("unknown intnum type")); + /*@notreached@*/ + return 0; } } @@ -672,39 +672,39 @@ long yasm_intnum_get_int(const yasm_intnum *intn) { switch (intn->type) { - case INTNUM_UL: - /* unsigned long values are always positive; max out if needed */ - return (intn->val.ul & 0x80000000) ? LONG_MAX : (long)intn->val.ul; - case INTNUM_BV: - if (BitVector_msb_(intn->val.bv)) { - /* it's negative: negate the bitvector to get a positive - * number, then negate the positive number. - */ - unsigned long ul; - - BitVector_Negate(conv_bv, intn->val.bv); - if (Set_Max(conv_bv) >= 32) { - /* too negative */ - return LONG_MIN; - } - ul = BitVector_Chunk_Read(conv_bv, 32, 0); - /* check for too negative */ - return (ul & 0x80000000) ? LONG_MIN : -((long)ul); - } - - /* it's positive, and since it's a BV, it must be >0x7FFFFFFF */ - return LONG_MAX; - default: - yasm_internal_error(N_("unknown intnum type")); - /*@notreached@*/ - return 0; + case INTNUM_UL: + /* unsigned long values are always positive; max out if needed */ + return (intn->val.ul & 0x80000000) ? LONG_MAX : (long)intn->val.ul; + case INTNUM_BV: + if (BitVector_msb_(intn->val.bv)) { + /* it's negative: negate the bitvector to get a positive + * number, then negate the positive number. + */ + unsigned long ul; + + BitVector_Negate(conv_bv, intn->val.bv); + if (Set_Max(conv_bv) >= 32) { + /* too negative */ + return LONG_MIN; + } + ul = BitVector_Chunk_Read(conv_bv, 32, 0); + /* check for too negative */ + return (ul & 0x80000000) ? LONG_MIN : -((long)ul); + } + + /* it's positive, and since it's a BV, it must be >0x7FFFFFFF */ + return LONG_MAX; + default: + yasm_internal_error(N_("unknown intnum type")); + /*@notreached@*/ + return 0; } } void yasm_intnum_get_sized(const yasm_intnum *intn, unsigned char *ptr, - size_t destsize, size_t valsize, int shift, - int bigendian, int warn) + size_t destsize, size_t valsize, int shift, + int bigendian, int warn) { wordptr op1 = op1static, op2; unsigned char *buf; @@ -714,48 +714,48 @@ yasm_intnum_get_sized(const yasm_intnum *intn, unsigned char *ptr, /* Currently don't support destinations larger than our native size */ if (destsize*8 > BITVECT_NATIVE_SIZE) - yasm_internal_error(N_("destination too large")); + yasm_internal_error(N_("destination too large")); /* General size warnings */ if (warn<0 && !yasm_intnum_check_size(intn, valsize, rshift, 1)) - yasm_warn_set(YASM_WARN_GENERAL, - N_("value does not fit in signed %d bit field"), - valsize); + yasm_warn_set(YASM_WARN_GENERAL, + N_("value does not fit in signed %d bit field"), + valsize); if (warn>0 && !yasm_intnum_check_size(intn, valsize, rshift, 2)) - yasm_warn_set(YASM_WARN_GENERAL, - N_("value does not fit in %d bit field"), valsize); + yasm_warn_set(YASM_WARN_GENERAL, + N_("value does not fit in %d bit field"), valsize); /* Read the original data into a bitvect */ if (bigendian) { - /* TODO */ - yasm_internal_error(N_("big endian not implemented")); + /* TODO */ + yasm_internal_error(N_("big endian not implemented")); } else - BitVector_Block_Store(op1, ptr, (N_int)destsize); + BitVector_Block_Store(op1, ptr, (N_int)destsize); /* If not already a bitvect, convert value to be written to a bitvect */ if (intn->type == INTNUM_BV) - op2 = intn->val.bv; + op2 = intn->val.bv; else { - op2 = op2static; - BitVector_Empty(op2); - BitVector_Chunk_Store(op2, 32, 0, intn->val.ul); + op2 = op2static; + BitVector_Empty(op2); + BitVector_Chunk_Store(op2, 32, 0, intn->val.ul); } /* Check low bits if right shifting and warnings enabled */ if (warn && rshift > 0) { - BitVector_Copy(conv_bv, op2); - BitVector_Move_Left(conv_bv, (N_int)(BITVECT_NATIVE_SIZE-rshift)); - if (!BitVector_is_empty(conv_bv)) - yasm_warn_set(YASM_WARN_GENERAL, - N_("misaligned value, truncating to boundary")); + BitVector_Copy(conv_bv, op2); + BitVector_Move_Left(conv_bv, (N_int)(BITVECT_NATIVE_SIZE-rshift)); + if (!BitVector_is_empty(conv_bv)) + yasm_warn_set(YASM_WARN_GENERAL, + N_("misaligned value, truncating to boundary")); } /* Shift right if needed */ if (rshift > 0) { - carry_in = BitVector_msb_(op2); - while (rshift-- > 0) - BitVector_shift_right(op2, carry_in); - shift = 0; + carry_in = BitVector_msb_(op2); + while (rshift-- > 0) + BitVector_shift_right(op2, carry_in); + shift = 0; } /* Write the new value into the destination bitvect */ @@ -764,56 +764,56 @@ yasm_intnum_get_sized(const yasm_intnum *intn, unsigned char *ptr, /* Write out the new data */ buf = BitVector_Block_Read(op1, &len); if (bigendian) { - /* TODO */ - yasm_internal_error(N_("big endian not implemented")); + /* TODO */ + yasm_internal_error(N_("big endian not implemented")); } else - memcpy(ptr, buf, destsize); + memcpy(ptr, buf, destsize); yasm_xfree(buf); } /* Return 1 if okay size, 0 if not */ int yasm_intnum_check_size(const yasm_intnum *intn, size_t size, size_t rshift, - int rangetype) + int rangetype) { wordptr val; /* If not already a bitvect, convert value to a bitvect */ if (intn->type == INTNUM_BV) { - if (rshift > 0) { - val = conv_bv; - BitVector_Copy(val, intn->val.bv); - } else - val = intn->val.bv; + if (rshift > 0) { + val = conv_bv; + BitVector_Copy(val, intn->val.bv); + } else + val = intn->val.bv; } else { - val = conv_bv; - BitVector_Empty(val); - BitVector_Chunk_Store(val, 32, 0, intn->val.ul); + val = conv_bv; + BitVector_Empty(val); + BitVector_Chunk_Store(val, 32, 0, intn->val.ul); } if (size >= BITVECT_NATIVE_SIZE) - return 1; + return 1; if (rshift > 0) { - int carry_in = BitVector_msb_(val); - while (rshift-- > 0) - BitVector_shift_right(val, carry_in); + int carry_in = BitVector_msb_(val); + while (rshift-- > 0) + BitVector_shift_right(val, carry_in); } if (rangetype > 0) { - if (BitVector_msb_(val)) { - /* it's negative */ - int retval; - - BitVector_Negate(conv_bv, val); - BitVector_dec(conv_bv, conv_bv); - retval = Set_Max(conv_bv) < (long)size-1; - - return retval; - } - - if (rangetype == 1) - size--; + if (BitVector_msb_(val)) { + /* it's negative */ + int retval; + + BitVector_Negate(conv_bv, val); + BitVector_dec(conv_bv, conv_bv); + retval = Set_Max(conv_bv) < (long)size-1; + + return retval; + } + + if (rangetype == 1) + size--; } return (Set_Max(val) < (long)size); } @@ -827,32 +827,32 @@ yasm_intnum_in_range(const yasm_intnum *intn, long low, long high) /* If not already a bitvect, convert value to be written to a bitvect */ if (intn->type == INTNUM_BV) - val = intn->val.bv; + val = intn->val.bv; else { - BitVector_Empty(val); - BitVector_Chunk_Store(val, 32, 0, intn->val.ul); + BitVector_Empty(val); + BitVector_Chunk_Store(val, 32, 0, intn->val.ul); } /* Convert high and low to bitvects */ BitVector_Empty(lval); if (low >= 0) - BitVector_Chunk_Store(lval, 32, 0, (unsigned long)low); + BitVector_Chunk_Store(lval, 32, 0, (unsigned long)low); else { - BitVector_Chunk_Store(lval, 32, 0, (unsigned long)(-low)); - BitVector_Negate(lval, lval); + BitVector_Chunk_Store(lval, 32, 0, (unsigned long)(-low)); + BitVector_Negate(lval, lval); } BitVector_Empty(hval); if (high >= 0) - BitVector_Chunk_Store(hval, 32, 0, (unsigned long)high); + BitVector_Chunk_Store(hval, 32, 0, (unsigned long)high); else { - BitVector_Chunk_Store(hval, 32, 0, (unsigned long)(-high)); - BitVector_Negate(hval, hval); + BitVector_Chunk_Store(hval, 32, 0, (unsigned long)(-high)); + BitVector_Negate(hval, hval); } /* Compare! */ return (BitVector_Compare(val, lval) >= 0 - && BitVector_Compare(val, hval) <= 0); + && BitVector_Compare(val, hval) <= 0); } static unsigned long @@ -862,27 +862,27 @@ get_leb128(wordptr val, unsigned char *ptr, int sign) unsigned char *ptr_orig = ptr; if (sign) { - /* Signed mode */ - if (BitVector_msb_(val)) { - /* Negative */ - BitVector_Negate(conv_bv, val); - size = Set_Max(conv_bv)+2; - } else { - /* Positive */ - size = Set_Max(val)+2; - } + /* Signed mode */ + if (BitVector_msb_(val)) { + /* Negative */ + BitVector_Negate(conv_bv, val); + size = Set_Max(conv_bv)+2; + } else { + /* Positive */ + size = Set_Max(val)+2; + } } else { - /* Unsigned mode */ - size = Set_Max(val)+1; + /* Unsigned mode */ + size = Set_Max(val)+1; } /* Positive/Unsigned write */ for (i=0; itype == INTNUM_UL && intn->val.ul == 0) { - *ptr = 0; - return 1; + *ptr = 0; + return 1; } /* If not already a bitvect, convert value to be written to a bitvect */ if (intn->type == INTNUM_BV) - val = intn->val.bv; + val = intn->val.bv; else { - BitVector_Empty(val); - BitVector_Chunk_Store(val, 32, 0, intn->val.ul); + BitVector_Empty(val); + BitVector_Chunk_Store(val, 32, 0, intn->val.ul); } return get_leb128(val, ptr, sign); @@ -934,15 +934,15 @@ yasm_intnum_size_leb128(const yasm_intnum *intn, int sign) /* Shortcut 0 */ if (intn->type == INTNUM_UL && intn->val.ul == 0) { - return 1; + return 1; } /* If not already a bitvect, convert value to a bitvect */ if (intn->type == INTNUM_BV) - val = intn->val.bv; + val = intn->val.bv; else { - BitVector_Empty(val); - BitVector_Chunk_Store(val, 32, 0, intn->val.ul); + BitVector_Empty(val); + BitVector_Chunk_Store(val, 32, 0, intn->val.ul); } return size_leb128(val, sign); @@ -955,16 +955,16 @@ yasm_get_sleb128(long v, unsigned char *ptr) /* Shortcut 0 */ if (v == 0) { - *ptr = 0; - return 1; + *ptr = 0; + return 1; } BitVector_Empty(val); if (v >= 0) - BitVector_Chunk_Store(val, 32, 0, (unsigned long)v); + BitVector_Chunk_Store(val, 32, 0, (unsigned long)v); else { - BitVector_Chunk_Store(val, 32, 0, (unsigned long)(-v)); - BitVector_Negate(val, val); + BitVector_Chunk_Store(val, 32, 0, (unsigned long)(-v)); + BitVector_Negate(val, val); } return get_leb128(val, ptr, 1); } @@ -975,14 +975,14 @@ yasm_size_sleb128(long v) wordptr val = op1static; if (v == 0) - return 1; + return 1; BitVector_Empty(val); if (v >= 0) - BitVector_Chunk_Store(val, 32, 0, (unsigned long)v); + BitVector_Chunk_Store(val, 32, 0, (unsigned long)v); else { - BitVector_Chunk_Store(val, 32, 0, (unsigned long)(-v)); - BitVector_Negate(val, val); + BitVector_Chunk_Store(val, 32, 0, (unsigned long)(-v)); + BitVector_Negate(val, val); } return size_leb128(val, 1); } @@ -994,8 +994,8 @@ yasm_get_uleb128(unsigned long v, unsigned char *ptr) /* Shortcut 0 */ if (v == 0) { - *ptr = 0; - return 1; + *ptr = 0; + return 1; } BitVector_Empty(val); @@ -1009,7 +1009,7 @@ yasm_size_uleb128(unsigned long v) wordptr val = op1static; if (v == 0) - return 1; + return 1; BitVector_Empty(val); BitVector_Chunk_Store(val, 32, 0, v); @@ -1022,14 +1022,14 @@ yasm_intnum_get_str(const yasm_intnum *intn) unsigned char *s; switch (intn->type) { - case INTNUM_UL: - s = yasm_xmalloc(16); - sprintf((char *)s, "%lu", intn->val.ul); - return (char *)s; - break; - case INTNUM_BV: - return (char *)BitVector_to_Dec(intn->val.bv); - break; + case INTNUM_UL: + s = yasm_xmalloc(16); + sprintf((char *)s, "%lu", intn->val.ul); + return (char *)s; + break; + case INTNUM_BV: + return (char *)BitVector_to_Dec(intn->val.bv); + break; } /*@notreached@*/ return NULL; @@ -1041,13 +1041,13 @@ yasm_intnum_print(const yasm_intnum *intn, FILE *f) unsigned char *s; switch (intn->type) { - case INTNUM_UL: - fprintf(f, "0x%lx", intn->val.ul); - break; - case INTNUM_BV: - s = BitVector_to_Hex(intn->val.bv); - fprintf(f, "0x%s", (char *)s); - yasm_xfree(s); - break; + case INTNUM_UL: + fprintf(f, "0x%lx", intn->val.ul); + break; + case INTNUM_BV: + s = BitVector_to_Hex(intn->val.bv); + fprintf(f, "0x%s", (char *)s); + yasm_xfree(s); + break; } } diff --git a/libyasm/intnum.h b/libyasm/intnum.h index f6c44c6e..58144170 100644 --- a/libyasm/intnum.h +++ b/libyasm/intnum.h @@ -41,25 +41,25 @@ void yasm_intnum_initialize(void); void yasm_intnum_cleanup(void); /** Create a new intnum from a decimal string. - * \param str decimal string + * \param str decimal string * \return Newly allocated intnum. */ /*@only@*/ yasm_intnum *yasm_intnum_create_dec(char *str); /** Create a new intnum from a binary string. - * \param str binary string + * \param str binary string * \return Newly allocated intnum. */ /*@only@*/ yasm_intnum *yasm_intnum_create_bin(char *str); /** Create a new intnum from an octal string. - * \param str octal string + * \param str octal string * \return Newly allocated intnum. */ /*@only@*/ yasm_intnum *yasm_intnum_create_oct(char *str); /** Create a new intnum from a hexidecimal string. - * \param str hexidecimal string + * \param str hexidecimal string * \return Newly allocated intnum. */ /*@only@*/ yasm_intnum *yasm_intnum_create_hex(char *str); @@ -67,27 +67,27 @@ void yasm_intnum_cleanup(void); /** Convert character constant to integer value, using NASM rules. NASM syntax * supports automatic conversion from strings such as 'abcd' to a 32-bit * integer value. This function performs those conversions. - * \param str character constant string + * \param str character constant string * \return Newly allocated intnum. */ /*@only@*/ yasm_intnum *yasm_intnum_create_charconst_nasm(const char *str); /** Create a new intnum from an unsigned integer value. - * \param i unsigned integer value + * \param i unsigned integer value * \return Newly allocated intnum. */ /*@only@*/ yasm_intnum *yasm_intnum_create_uint(unsigned long i); /** Create a new intnum from an signed integer value. - * \param i signed integer value + * \param i signed integer value * \return Newly allocated intnum. */ /*@only@*/ yasm_intnum *yasm_intnum_create_int(long i); /** Create a new intnum from LEB128-encoded form. - * \param ptr pointer to start of LEB128 encoded form - * \param sign signed (1) or unsigned (0) LEB128 format - * \param size number of bytes read from ptr (output) + * \param ptr pointer to start of LEB128 encoded form + * \param sign signed (1) or unsigned (0) LEB128 format + * \param size number of bytes read from ptr (output) * \return Newly allocated intnum. Number of bytes read returned into * bytes_read parameter. */ @@ -96,8 +96,8 @@ void yasm_intnum_cleanup(void); /** Create a new intnum from a little-endian or big-endian buffer. * In little endian, the LSB is in ptr[0]. - * \param ptr pointer to start of buffer - * \param sign signed (1) or unsigned (0) source + * \param ptr pointer to start of buffer + * \param sign signed (1) or unsigned (0) source * \param srcsize source buffer size (in bytes) * \param bigendian endianness (nonzero=big, zero=little) */ @@ -105,63 +105,63 @@ void yasm_intnum_cleanup(void); (unsigned char *ptr, int sign, size_t srcsize, int bigendian); /** Duplicate an intnum. - * \param intn intnum + * \param intn intnum * \return Newly allocated intnum with the same value as intn. */ /*@only@*/ yasm_intnum *yasm_intnum_copy(const yasm_intnum *intn); /** Destroy (free allocated memory for) an intnum. - * \param intn intnum + * \param intn intnum */ void yasm_intnum_destroy(/*@only@*/ yasm_intnum *intn); /** Floating point calculation function: acc = acc op operand. * \note Not all operations in yasm_expr_op may be supported; unsupported * operations will result in an error. - * \param acc intnum accumulator - * \param op operation + * \param acc intnum accumulator + * \param op operation * \param operand intnum operand * \return Nonzero if error occurred. */ int yasm_intnum_calc(yasm_intnum *acc, yasm_expr_op op, yasm_intnum *operand); /** Zero an intnum. - * \param intn intnum + * \param intn intnum */ void yasm_intnum_zero(yasm_intnum *intn); /** Set an intnum to an unsigned integer. - * \param intn intnum - * \param val integer value + * \param intn intnum + * \param val integer value */ void yasm_intnum_set_uint(yasm_intnum *intn, unsigned long val); /** Set an intnum to an signed integer. - * \param intn intnum - * \param val integer value + * \param intn intnum + * \param val integer value */ void yasm_intnum_set_int(yasm_intnum *intn, long val); /** Simple value check for 0. - * \param acc intnum + * \param acc intnum * \return Nonzero if acc==0. */ int yasm_intnum_is_zero(const yasm_intnum *acc); /** Simple value check for 1. - * \param acc intnum + * \param acc intnum * \return Nonzero if acc==1. */ int yasm_intnum_is_pos1(const yasm_intnum *acc); /** Simple value check for -1. - * \param acc intnum + * \param acc intnum * \return Nonzero if acc==-1. */ int yasm_intnum_is_neg1(const yasm_intnum *acc); /** Simple sign check. - * \param acc intnum + * \param acc intnum * \return -1 if negative, 0 if zero, +1 if positive */ int yasm_intnum_sign(const yasm_intnum *acc); @@ -170,7 +170,7 @@ int yasm_intnum_sign(const yasm_intnum *acc); * C format (eg, of unknown endian). * \note Parameter intnum is truncated to fit into 32 bits. Use * intnum_check_size() to check for overflow. - * \param intn intnum + * \param intn intnum * \return Unsigned 32-bit value of intn. */ unsigned long yasm_intnum_get_uint(const yasm_intnum *intn); @@ -179,7 +179,7 @@ unsigned long yasm_intnum_get_uint(const yasm_intnum *intn); * format (eg, of unknown endian). * \note Parameter intnum is truncated to fit into 32 bits. Use * intnum_check_size() to check for overflow. - * \param intn intnum + * \param intn intnum * \return Signed 32-bit value of intn. */ long yasm_intnum_get_int(const yasm_intnum *intn); @@ -188,94 +188,94 @@ long yasm_intnum_get_int(const yasm_intnum *intn); * value into the least significant bits of the destination, or may be shifted * into more significant bits by the shift parameter. The destination bits are * cleared before being set. [0] should be the first byte output to the file. - * \param intn intnum - * \param ptr pointer to storage for size bytes of output + * \param intn intnum + * \param ptr pointer to storage for size bytes of output * \param destsize destination size (in bytes) * \param valsize size (in bits) - * \param shift left shift (in bits); may be negative to specify right - * shift (standard warnings include truncation to boundary) + * \param shift left shift (in bits); may be negative to specify right + * shift (standard warnings include truncation to boundary) * \param bigendian endianness (nonzero=big, zero=little) - * \param warn enables standard warnings (value doesn't fit into valsize - * bits): <0=signed warnings, >0=unsigned warnings, 0=no warn + * \param warn enables standard warnings (value doesn't fit into valsize + * bits): <0=signed warnings, >0=unsigned warnings, 0=no warn */ void yasm_intnum_get_sized(const yasm_intnum *intn, unsigned char *ptr, - size_t destsize, size_t valsize, int shift, - int bigendian, int warn); + size_t destsize, size_t valsize, int shift, + int bigendian, int warn); /** Check to see if intnum will fit without overflow into size bits. - * \param intn intnum - * \param size number of bits of output space + * \param intn intnum + * \param size number of bits of output space * \param rshift right shift * \param rangetype signed/unsigned range selection: - * 0 => (0, unsigned max); - * 1 => (signed min, signed max); - * 2 => (signed min, unsigned max) + * 0 => (0, unsigned max); + * 1 => (signed min, signed max); + * 2 => (signed min, unsigned max) * \return Nonzero if intnum will fit. */ int yasm_intnum_check_size(const yasm_intnum *intn, size_t size, - size_t rshift, int rangetype); + size_t rshift, int rangetype); /** Check to see if intnum will fit into a particular numeric range. - * \param intn intnum - * \param low low end of range (inclusive) - * \param high high end of range (inclusive) + * \param intn intnum + * \param low low end of range (inclusive) + * \param high high end of range (inclusive) * \return Nonzero if intnum is within range. */ int yasm_intnum_in_range(const yasm_intnum *intn, long low, long high); /** Output #yasm_intnum to buffer in LEB128-encoded form. - * \param intn intnum - * \param ptr pointer to storage for output bytes - * \param sign signedness of LEB128 encoding (0=unsigned, 1=signed) + * \param intn intnum + * \param ptr pointer to storage for output bytes + * \param sign signedness of LEB128 encoding (0=unsigned, 1=signed) * \return Number of bytes generated. */ unsigned long yasm_intnum_get_leb128(const yasm_intnum *intn, - unsigned char *ptr, int sign); + unsigned char *ptr, int sign); /** Calculate number of bytes LEB128-encoded form of #yasm_intnum will take. - * \param intn intnum - * \param sign signedness of LEB128 encoding (0=unsigned, 1=signed) + * \param intn intnum + * \param sign signedness of LEB128 encoding (0=unsigned, 1=signed) * \return Number of bytes. */ unsigned long yasm_intnum_size_leb128(const yasm_intnum *intn, int sign); /** Output integer to buffer in signed LEB128-encoded form. - * \param v integer - * \param ptr pointer to storage for output bytes + * \param v integer + * \param ptr pointer to storage for output bytes * \return Number of bytes generated. */ unsigned long yasm_get_sleb128(long v, unsigned char *ptr); /** Calculate number of bytes signed LEB128-encoded form of integer will take. - * \param v integer + * \param v integer * \return Number of bytes. */ unsigned long yasm_size_sleb128(long v); /** Output integer to buffer in unsigned LEB128-encoded form. - * \param v integer - * \param ptr pointer to storage for output bytes + * \param v integer + * \param ptr pointer to storage for output bytes * \return Number of bytes generated. */ unsigned long yasm_get_uleb128(unsigned long v, unsigned char *ptr); /** Calculate number of bytes unsigned LEB128-encoded form of integer will take. - * \param v integer + * \param v integer * \return Number of bytes. */ unsigned long yasm_size_uleb128(unsigned long v); /** Get an intnum as a signed decimal string. The returned string will * contain a leading '-' if the intnum is negative. - * \param intn intnum + * \param intn intnum * \return Newly allocated string containing the decimal representation of * the intnum. */ /*@only@*/ char *yasm_intnum_get_str(const yasm_intnum *intn); /** Print an intnum. For debugging purposes. - * \param f file - * \param intn intnum + * \param f file + * \param intn intnum */ void yasm_intnum_print(const yasm_intnum *intn, FILE *f); diff --git a/libyasm/inttree.c b/libyasm/inttree.c index dbb29215..2c408f1c 100644 --- a/libyasm/inttree.c +++ b/libyasm/inttree.c @@ -21,8 +21,8 @@ abort();} static void Assert(int assertion, const char *error) { if (!assertion) { - fprintf(stderr, "Assertion Failed: %s\n", error); - abort(); + fprintf(stderr, "Assertion Failed: %s\n", error); + abort(); } } #endif @@ -46,7 +46,7 @@ static void DeleteFixUp(IntervalTree *, IntervalTreeNode *); #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS static void CheckMaxHighFields(const IntervalTree *, IntervalTreeNode *); static int CheckMaxHighFieldsHelper(const IntervalTree *, IntervalTreeNode *y, - const int currentHigh, int match); + const int currentHigh, int match); static void IT_CheckAssumptions(const IntervalTree *); #endif @@ -59,11 +59,11 @@ ITN_create(long low, long high, void *data) IntervalTreeNode *itn = yasm_xmalloc(sizeof(IntervalTreeNode)); itn->data = data; if (low < high) { - itn->low = low; - itn->high = high; + itn->low = low; + itn->high = high; } else { - itn->low = high; - itn->high = low; + itn->low = high; + itn->high = low; } itn->maxHigh = high; return itn; @@ -89,7 +89,7 @@ IT_create(void) /* the following are used for the Enumerate function */ it->recursionNodeStackSize = 128; it->recursionNodeStack = (it_recursion_node *) - yasm_xmalloc(it->recursionNodeStackSize*sizeof(it_recursion_node)); + yasm_xmalloc(it->recursionNodeStackSize*sizeof(it_recursion_node)); it->recursionNodeStackTop = 1; it->recursionNodeStack[0].start_node = NULL; @@ -133,7 +133,7 @@ LeftRotate(IntervalTree *it, IntervalTreeNode *x) x->right=y->left; if (y->left != it->nil) - y->left->parent=x; /* used to use sentinel here */ + y->left->parent=x; /* used to use sentinel here */ /* and do an unconditional assignment instead of testing for nil */ y->parent=x->parent; @@ -142,9 +142,9 @@ LeftRotate(IntervalTree *it, IntervalTreeNode *x) * count on the root sentinel to implicitly take care of this case */ if (x == x->parent->left) - x->parent->left=y; + x->parent->left=y; else - x->parent->right=y; + x->parent->right=y; y->left=x; x->parent=y; @@ -155,7 +155,7 @@ LeftRotate(IntervalTree *it, IntervalTreeNode *x) #elif defined(DEBUG_ASSERT) Assert(!it->nil->red,"nil not red in ITLeftRotate"); Assert((it->nil->maxHigh=LONG_MIN), - "nil->maxHigh != LONG_MIN in ITLeftRotate"); + "nil->maxHigh != LONG_MIN in ITLeftRotate"); #endif } @@ -198,7 +198,7 @@ RightRotate(IntervalTree *it, IntervalTreeNode *y) y->left=x->right; if (it->nil != x->right) - x->right->parent=y; /*used to use sentinel here */ + x->right->parent=y; /*used to use sentinel here */ /* and do an unconditional assignment instead of testing for nil */ /* Instead of checking if x->parent is the root as in the book, we @@ -206,9 +206,9 @@ RightRotate(IntervalTree *it, IntervalTreeNode *y) */ x->parent=y->parent; if (y == y->parent->left) - y->parent->left=x; + y->parent->left=x; else - y->parent->right=x; + y->parent->right=x; x->right=y; y->parent=x; @@ -219,7 +219,7 @@ RightRotate(IntervalTree *it, IntervalTreeNode *y) #elif defined(DEBUG_ASSERT) Assert(!it->nil->red,"nil not red in ITRightRotate"); Assert((it->nil->maxHigh=LONG_MIN), - "nil->maxHigh != LONG_MIN in ITRightRotate"); + "nil->maxHigh != LONG_MIN in ITRightRotate"); #endif } @@ -249,22 +249,22 @@ TreeInsertHelp(IntervalTree *it, IntervalTreeNode *z) y=it->root; x=it->root->left; while( x != it->nil) { - y=x; - if (x->low > z->low) - x=x->left; - else /* x->low <= z->low */ - x=x->right; + y=x; + if (x->low > z->low) + x=x->left; + else /* x->low <= z->low */ + x=x->right; } z->parent=y; if ((y == it->root) || (y->low > z->low)) - y->left=z; + y->left=z; else - y->right=z; + y->right=z; #if defined(DEBUG_ASSERT) Assert(!it->nil->red,"nil not red in ITTreeInsertHelp"); Assert((it->nil->maxHigh=INT_MIN), - "nil->maxHigh != INT_MIN in ITTreeInsertHelp"); + "nil->maxHigh != INT_MIN in ITTreeInsertHelp"); #endif } @@ -286,8 +286,8 @@ static void FixUpMaxHigh(IntervalTree *it, IntervalTreeNode *x) { while(x != it->root) { - x->maxHigh=ITMax(x->high,ITMax(x->left->maxHigh,x->right->maxHigh)); - x=x->parent; + x->maxHigh=ITMax(x->high,ITMax(x->left->maxHigh,x->right->maxHigh)); + x=x->parent; } #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS IT_CheckAssumptions(it); @@ -324,41 +324,41 @@ IT_insert(IntervalTree *it, long low, long high, void *data) newNode = x; x->red=1; while(x->parent->red) { /* use sentinel instead of checking for root */ - if (x->parent == x->parent->parent->left) { - y=x->parent->parent->right; - if (y->red) { - x->parent->red=0; - y->red=0; - x->parent->parent->red=1; - x=x->parent->parent; - } else { - if (x == x->parent->right) { - x=x->parent; - LeftRotate(it, x); - } - x->parent->red=0; - x->parent->parent->red=1; - RightRotate(it, x->parent->parent); - } - } else { /* case for x->parent == x->parent->parent->right */ + if (x->parent == x->parent->parent->left) { + y=x->parent->parent->right; + if (y->red) { + x->parent->red=0; + y->red=0; + x->parent->parent->red=1; + x=x->parent->parent; + } else { + if (x == x->parent->right) { + x=x->parent; + LeftRotate(it, x); + } + x->parent->red=0; + x->parent->parent->red=1; + RightRotate(it, x->parent->parent); + } + } else { /* case for x->parent == x->parent->parent->right */ /* this part is just like the section above with */ /* left and right interchanged */ - y=x->parent->parent->left; - if (y->red) { - x->parent->red=0; - y->red=0; - x->parent->parent->red=1; - x=x->parent->parent; - } else { - if (x == x->parent->left) { - x=x->parent; - RightRotate(it, x); - } - x->parent->red=0; - x->parent->parent->red=1; - LeftRotate(it, x->parent->parent); - } - } + y=x->parent->parent->left; + if (y->red) { + x->parent->red=0; + y->red=0; + x->parent->parent->red=1; + x=x->parent->parent; + } else { + if (x == x->parent->left) { + x=x->parent; + RightRotate(it, x); + } + x->parent->red=0; + x->parent->parent->red=1; + LeftRotate(it, x->parent->parent); + } + } } it->root->left->red=0; @@ -368,7 +368,7 @@ IT_insert(IntervalTree *it, long low, long high, void *data) Assert(!it->nil->red,"nil not red in ITTreeInsert"); Assert(!it->root->red,"root not red in ITTreeInsert"); Assert((it->nil->maxHigh=LONG_MIN), - "nil->maxHigh != LONG_MIN in ITTreeInsert"); + "nil->maxHigh != LONG_MIN in ITTreeInsert"); #endif return newNode; } @@ -392,18 +392,18 @@ IT_get_successor(const IntervalTree *it, IntervalTreeNode *x) IntervalTreeNode *y; if (it->nil != (y = x->right)) { /* assignment to y is intentional */ - while(y->left != it->nil) /* returns the minium of the right subtree of x */ - y=y->left; - return y; + while(y->left != it->nil) /* returns the minium of the right subtree of x */ + y=y->left; + return y; } else { - y=x->parent; - while(x == y->right) { /* sentinel used instead of checking for nil */ - x=y; - y=y->parent; - } - if (y == it->root) - return(it->nil); - return y; + y=x->parent; + while(x == y->right) { /* sentinel used instead of checking for nil */ + x=y; + y=y->parent; + } + if (y == it->root) + return(it->nil); + return y; } } @@ -426,18 +426,18 @@ IT_get_predecessor(const IntervalTree *it, IntervalTreeNode *x) IntervalTreeNode *y; if (it->nil != (y = x->left)) { /* assignment to y is intentional */ - while(y->right != it->nil) /* returns the maximum of the left subtree of x */ - y=y->right; - return y; + while(y->right != it->nil) /* returns the maximum of the left subtree of x */ + y=y->right; + return y; } else { - y=x->parent; - while(x == y->left) { - if (y == it->root) - return(it->nil); - x=y; - y=y->parent; - } - return y; + y=x->parent; + while(x == y->left) { + if (y == it->root) + return(it->nil); + x=y; + y=y->parent; + } + return y; } } @@ -458,24 +458,24 @@ IT_get_predecessor(const IntervalTree *it, IntervalTreeNode *x) static void ITN_print(const IntervalTreeNode *itn, IntervalTreeNode *nil, - IntervalTreeNode *root) + IntervalTreeNode *root) { printf(", l=%li, h=%li, mH=%li", itn->low, itn->high, itn->maxHigh); printf(" l->low="); if (itn->left == nil) - printf("NULL"); + printf("NULL"); else - printf("%li", itn->left->low); + printf("%li", itn->left->low); printf(" r->low="); if (itn->right == nil) - printf("NULL"); + printf("NULL"); else - printf("%li", itn->right->low); + printf("%li", itn->right->low); printf(" p->low="); if (itn->parent == root) - printf("NULL"); + printf("NULL"); else - printf("%li", itn->parent->low); + printf("%li", itn->parent->low); printf(" red=%i\n", itn->red); } @@ -483,9 +483,9 @@ static void TreePrintHelper(const IntervalTree *it, IntervalTreeNode *x) { if (x != it->nil) { - TreePrintHelper(it, x->left); - ITN_print(x, it->nil, it->root); - TreePrintHelper(it, x->right); + TreePrintHelper(it, x->left); + ITN_print(x, it->nil, it->root); + TreePrintHelper(it, x->right); } } @@ -495,40 +495,40 @@ IT_destroy(IntervalTree *it) IntervalTreeNode *x = it->root->left; SLIST_HEAD(, nodeent) stuffToFree = SLIST_HEAD_INITIALIZER(stuffToFree); struct nodeent { - SLIST_ENTRY(nodeent) link; - struct IntervalTreeNode *node; + SLIST_ENTRY(nodeent) link; + struct IntervalTreeNode *node; } *np; if (x != it->nil) { - if (x->left != it->nil) { - np = yasm_xmalloc(sizeof(struct nodeent)); - np->node = x->left; - SLIST_INSERT_HEAD(&stuffToFree, np, link); - } - if (x->right != it->nil) { - np = yasm_xmalloc(sizeof(struct nodeent)); - np->node = x->right; - SLIST_INSERT_HEAD(&stuffToFree, np, link); - } - yasm_xfree(x); - while (!SLIST_EMPTY(&stuffToFree)) { - np = SLIST_FIRST(&stuffToFree); - x = np->node; - SLIST_REMOVE_HEAD(&stuffToFree, link); - yasm_xfree(np); - - if (x->left != it->nil) { - np = yasm_xmalloc(sizeof(struct nodeent)); - np->node = x->left; - SLIST_INSERT_HEAD(&stuffToFree, np, link); - } - if (x->right != it->nil) { - np = yasm_xmalloc(sizeof(struct nodeent)); - np->node = x->right; - SLIST_INSERT_HEAD(&stuffToFree, np, link); - } - yasm_xfree(x); - } + if (x->left != it->nil) { + np = yasm_xmalloc(sizeof(struct nodeent)); + np->node = x->left; + SLIST_INSERT_HEAD(&stuffToFree, np, link); + } + if (x->right != it->nil) { + np = yasm_xmalloc(sizeof(struct nodeent)); + np->node = x->right; + SLIST_INSERT_HEAD(&stuffToFree, np, link); + } + yasm_xfree(x); + while (!SLIST_EMPTY(&stuffToFree)) { + np = SLIST_FIRST(&stuffToFree); + x = np->node; + SLIST_REMOVE_HEAD(&stuffToFree, link); + yasm_xfree(np); + + if (x->left != it->nil) { + np = yasm_xmalloc(sizeof(struct nodeent)); + np->node = x->left; + SLIST_INSERT_HEAD(&stuffToFree, np, link); + } + if (x->right != it->nil) { + np = yasm_xmalloc(sizeof(struct nodeent)); + np->node = x->right; + SLIST_INSERT_HEAD(&stuffToFree, np, link); + } + yasm_xfree(x); + } } yasm_xfree(it->nil); @@ -581,55 +581,55 @@ DeleteFixUp(IntervalTree *it, IntervalTreeNode *x) IntervalTreeNode *rootLeft = it->root->left; while ((!x->red) && (rootLeft != x)) { - if (x == x->parent->left) { - w=x->parent->right; - if (w->red) { - w->red=0; - x->parent->red=1; - LeftRotate(it, x->parent); - w=x->parent->right; - } - if ( (!w->right->red) && (!w->left->red) ) { - w->red=1; - x=x->parent; - } else { - if (!w->right->red) { - w->left->red=0; - w->red=1; - RightRotate(it, w); - w=x->parent->right; - } - w->red=x->parent->red; - x->parent->red=0; - w->right->red=0; - LeftRotate(it, x->parent); - x=rootLeft; /* this is to exit while loop */ - } - } else { /* the code below is has left and right switched from above */ - w=x->parent->left; - if (w->red) { - w->red=0; - x->parent->red=1; - RightRotate(it, x->parent); - w=x->parent->left; - } - if ((!w->right->red) && (!w->left->red)) { - w->red=1; - x=x->parent; - } else { - if (!w->left->red) { - w->right->red=0; - w->red=1; - LeftRotate(it, w); - w=x->parent->left; - } - w->red=x->parent->red; - x->parent->red=0; - w->left->red=0; - RightRotate(it, x->parent); - x=rootLeft; /* this is to exit while loop */ - } - } + if (x == x->parent->left) { + w=x->parent->right; + if (w->red) { + w->red=0; + x->parent->red=1; + LeftRotate(it, x->parent); + w=x->parent->right; + } + if ( (!w->right->red) && (!w->left->red) ) { + w->red=1; + x=x->parent; + } else { + if (!w->right->red) { + w->left->red=0; + w->red=1; + RightRotate(it, w); + w=x->parent->right; + } + w->red=x->parent->red; + x->parent->red=0; + w->right->red=0; + LeftRotate(it, x->parent); + x=rootLeft; /* this is to exit while loop */ + } + } else { /* the code below is has left and right switched from above */ + w=x->parent->left; + if (w->red) { + w->red=0; + x->parent->red=1; + RightRotate(it, x->parent); + w=x->parent->left; + } + if ((!w->right->red) && (!w->left->red)) { + w->red=1; + x=x->parent; + } else { + if (!w->left->red) { + w->right->red=0; + w->red=1; + LeftRotate(it, w); + w=x->parent->left; + } + w->red=x->parent->red; + x->parent->red=0; + w->left->red=0; + RightRotate(it, x->parent); + x=rootLeft; /* this is to exit while loop */ + } + } } x->red=0; @@ -638,7 +638,7 @@ DeleteFixUp(IntervalTree *it, IntervalTreeNode *x) #elif defined(DEBUG_ASSERT) Assert(!it->nil->red,"nil not black in ITDeleteFixUp"); Assert((it->nil->maxHigh=LONG_MIN), - "nil->maxHigh != LONG_MIN in ITDeleteFixUp"); + "nil->maxHigh != LONG_MIN in ITDeleteFixUp"); #endif } @@ -665,61 +665,61 @@ IT_delete_node(IntervalTree *it, IntervalTreeNode *z, long *low, long *high) IntervalTreeNode *x, *y; void *returnValue = z->data; if (low) - *low = z->low; + *low = z->low; if (high) - *high = z->high; + *high = z->high; y= ((z->left == it->nil) || (z->right == it->nil)) ? - z : IT_get_successor(it, z); + z : IT_get_successor(it, z); x= (y->left == it->nil) ? y->right : y->left; if (it->root == (x->parent = y->parent)) - /* assignment of y->p to x->p is intentional */ - it->root->left=x; + /* assignment of y->p to x->p is intentional */ + it->root->left=x; else { - if (y == y->parent->left) - y->parent->left=x; - else - y->parent->right=x; + if (y == y->parent->left) + y->parent->left=x; + else + y->parent->right=x; } if (y != z) { /* y should not be nil in this case */ #ifdef DEBUG_ASSERT - Assert( (y!=it->nil),"y is nil in DeleteNode \n"); + Assert( (y!=it->nil),"y is nil in DeleteNode \n"); #endif - /* y is the node to splice out and x is its child */ + /* y is the node to splice out and x is its child */ - y->maxHigh = INT_MIN; - y->left=z->left; - y->right=z->right; - y->parent=z->parent; - z->left->parent=z->right->parent=y; - if (z == z->parent->left) - z->parent->left=y; - else - z->parent->right=y; - FixUpMaxHigh(it, x->parent); - if (!(y->red)) { - y->red = z->red; - DeleteFixUp(it, x); - } else - y->red = z->red; - yasm_xfree(z); + y->maxHigh = INT_MIN; + y->left=z->left; + y->right=z->right; + y->parent=z->parent; + z->left->parent=z->right->parent=y; + if (z == z->parent->left) + z->parent->left=y; + else + z->parent->right=y; + FixUpMaxHigh(it, x->parent); + if (!(y->red)) { + y->red = z->red; + DeleteFixUp(it, x); + } else + y->red = z->red; + yasm_xfree(z); #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS - IT_CheckAssumptions(it); + IT_CheckAssumptions(it); #elif defined(DEBUG_ASSERT) - Assert(!it->nil->red,"nil not black in ITDelete"); - Assert((it->nil->maxHigh=LONG_MIN),"nil->maxHigh != LONG_MIN in ITDelete"); + Assert(!it->nil->red,"nil not black in ITDelete"); + Assert((it->nil->maxHigh=LONG_MIN),"nil->maxHigh != LONG_MIN in ITDelete"); #endif } else { - FixUpMaxHigh(it, x->parent); - if (!(y->red)) - DeleteFixUp(it, x); - yasm_xfree(y); + FixUpMaxHigh(it, x->parent); + if (!(y->red)) + DeleteFixUp(it, x); + yasm_xfree(y); #ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS - IT_CheckAssumptions(it); + IT_CheckAssumptions(it); #elif defined(DEBUG_ASSERT) - Assert(!it->nil->red,"nil not black in ITDelete"); - Assert((it->nil->maxHigh=LONG_MIN),"nil->maxHigh != LONG_MIN in ITDelete"); + Assert(!it->nil->red,"nil not black in ITDelete"); + Assert((it->nil->maxHigh=LONG_MIN),"nil->maxHigh != LONG_MIN in ITDelete"); #endif } return returnValue; @@ -743,9 +743,9 @@ static int Overlap(int a1, int a2, int b1, int b2) { if (a1 <= b1) - return (b1 <= a2); + return (b1 <= a2); else - return (a1 <= b2); + return (a1 <= b2); } @@ -789,7 +789,7 @@ Overlap(int a1, int a2, int b1, int b2) */ void IT_enumerate(IntervalTree *it, long low, long high, void *cbd, - void (*callback) (IntervalTreeNode *node, void *cbd)) + void (*callback) (IntervalTreeNode *node, void *cbd)) { IntervalTreeNode *x=it->root->left; int stuffToDo = (x != it->nil); @@ -798,43 +798,43 @@ IT_enumerate(IntervalTree *it, long low, long high, void *cbd, #ifdef DEBUG_ASSERT Assert((it->recursionNodeStackTop == 1), - "recursionStack not empty when entering IntervalTree::Enumerate"); + "recursionStack not empty when entering IntervalTree::Enumerate"); #endif it->currentParent = 0; while (stuffToDo) { - if (Overlap(low,high,x->low,x->high) ) { - callback(x, cbd); - it->recursionNodeStack[it->currentParent].tryRightBranch=1; - } - if(x->left->maxHigh >= low) { /* implies x != nil */ - if (it->recursionNodeStackTop == it->recursionNodeStackSize) { - it->recursionNodeStackSize *= 2; - it->recursionNodeStack = (it_recursion_node *) - yasm_xrealloc(it->recursionNodeStack, - it->recursionNodeStackSize * sizeof(it_recursion_node)); - } - it->recursionNodeStack[it->recursionNodeStackTop].start_node = x; - it->recursionNodeStack[it->recursionNodeStackTop].tryRightBranch = 0; - it->recursionNodeStack[it->recursionNodeStackTop].parentIndex = it->currentParent; - it->currentParent = it->recursionNodeStackTop++; - x = x->left; - } else { - x = x->right; - } - stuffToDo = (x != it->nil); - while (!stuffToDo && (it->recursionNodeStackTop > 1)) { - if (it->recursionNodeStack[--it->recursionNodeStackTop].tryRightBranch) { - x=it->recursionNodeStack[it->recursionNodeStackTop].start_node->right; - it->currentParent=it->recursionNodeStack[it->recursionNodeStackTop].parentIndex; - it->recursionNodeStack[it->currentParent].tryRightBranch=1; - stuffToDo = (x != it->nil); - } - } + if (Overlap(low,high,x->low,x->high) ) { + callback(x, cbd); + it->recursionNodeStack[it->currentParent].tryRightBranch=1; + } + if(x->left->maxHigh >= low) { /* implies x != nil */ + if (it->recursionNodeStackTop == it->recursionNodeStackSize) { + it->recursionNodeStackSize *= 2; + it->recursionNodeStack = (it_recursion_node *) + yasm_xrealloc(it->recursionNodeStack, + it->recursionNodeStackSize * sizeof(it_recursion_node)); + } + it->recursionNodeStack[it->recursionNodeStackTop].start_node = x; + it->recursionNodeStack[it->recursionNodeStackTop].tryRightBranch = 0; + it->recursionNodeStack[it->recursionNodeStackTop].parentIndex = it->currentParent; + it->currentParent = it->recursionNodeStackTop++; + x = x->left; + } else { + x = x->right; + } + stuffToDo = (x != it->nil); + while (!stuffToDo && (it->recursionNodeStackTop > 1)) { + if (it->recursionNodeStack[--it->recursionNodeStackTop].tryRightBranch) { + x=it->recursionNodeStack[it->recursionNodeStackTop].start_node->right; + it->currentParent=it->recursionNodeStack[it->recursionNodeStackTop].parentIndex; + it->recursionNodeStack[it->currentParent].tryRightBranch=1; + stuffToDo = (x != it->nil); + } + } } #ifdef DEBUG_ASSERT Assert((it->recursionNodeStackTop == 1), - "recursionStack not empty when exiting IntervalTree::Enumerate"); + "recursionStack not empty when exiting IntervalTree::Enumerate"); #endif } @@ -842,21 +842,21 @@ IT_enumerate(IntervalTree *it, long low, long high, void *cbd, static int CheckMaxHighFieldsHelper(const IntervalTree *it, IntervalTreeNode *y, - int currentHigh, int match) + int currentHigh, int match) { if (y != it->nil) { - match = CheckMaxHighFieldsHelper(it, y->left, currentHigh, match) ? - 1 : match; - VERIFY(y->high <= currentHigh); - if (y->high == currentHigh) - match = 1; - match = CheckMaxHighFieldsHelper(it, y->right, currentHigh, match) ? - 1 : match; + match = CheckMaxHighFieldsHelper(it, y->left, currentHigh, match) ? + 1 : match; + VERIFY(y->high <= currentHigh); + if (y->high == currentHigh) + match = 1; + match = CheckMaxHighFieldsHelper(it, y->right, currentHigh, match) ? + 1 : match; } return match; } - + /* Make sure the maxHigh fields for everything makes sense. * * If something is wrong, print a warning and exit */ @@ -864,12 +864,12 @@ static void CheckMaxHighFields(const IntervalTree *it, IntervalTreeNode *x) { if (x != it->nil) { - CheckMaxHighFields(it, x->left); - if(!(CheckMaxHighFieldsHelper(it, x, x->maxHigh, 0) > 0)) { - fprintf(stderr, "error found in CheckMaxHighFields.\n"); - abort(); - } - CheckMaxHighFields(it, x->right); + CheckMaxHighFields(it, x->left); + if(!(CheckMaxHighFieldsHelper(it, x, x->maxHigh, 0) > 0)) { + fprintf(stderr, "error found in CheckMaxHighFields.\n"); + abort(); + } + CheckMaxHighFields(it, x->right); } } diff --git a/libyasm/inttree.h b/libyasm/inttree.h index 2b754984..972f5713 100644 --- a/libyasm/inttree.h +++ b/libyasm/inttree.h @@ -49,11 +49,11 @@ IntervalTree *IT_create(void); void IT_destroy(IntervalTree *); void IT_print(const IntervalTree *); void *IT_delete_node(IntervalTree *, IntervalTreeNode *, long *low, - long *high); + long *high); IntervalTreeNode *IT_insert(IntervalTree *, long low, long high, void *data); IntervalTreeNode *IT_get_predecessor(const IntervalTree *, IntervalTreeNode *); IntervalTreeNode *IT_get_successor(const IntervalTree *, IntervalTreeNode *); void IT_enumerate(IntervalTree *, long low, long high, void *cbd, - void (*callback) (IntervalTreeNode *node, void *cbd)); + void (*callback) (IntervalTreeNode *node, void *cbd)); #endif diff --git a/libyasm/linemap.c b/libyasm/linemap.c index bf28cb49..3639baa3 100644 --- a/libyasm/linemap.c +++ b/libyasm/linemap.c @@ -86,7 +86,7 @@ filename_delete_one(/*@only@*/ void *d) void yasm_linemap_set(yasm_linemap *linemap, const char *filename, - unsigned long file_line, unsigned long line_inc) + unsigned long file_line, unsigned long line_inc) { char *copy; int replace = 0; @@ -94,11 +94,11 @@ yasm_linemap_set(yasm_linemap *linemap, const char *filename, /* Create a new mapping in the map */ if (linemap->map->size >= linemap->map->allocated) { - /* allocate another size bins when full for 2x space */ - linemap->map->vector = - yasm_xrealloc(linemap->map->vector, 2*linemap->map->allocated - *sizeof(line_mapping)); - linemap->map->allocated *= 2; + /* allocate another size bins when full for 2x space */ + linemap->map->vector = + yasm_xrealloc(linemap->map->vector, 2*linemap->map->allocated + *sizeof(line_mapping)); + linemap->map->allocated *= 2; } mapping = &linemap->map->vector[linemap->map->size]; linemap->map->size++; @@ -106,19 +106,19 @@ yasm_linemap_set(yasm_linemap *linemap, const char *filename, /* Fill it */ if (!filename) { - if (linemap->map->size >= 2) - mapping->filename = - linemap->map->vector[linemap->map->size-2].filename; - else - filename = "unknown"; + if (linemap->map->size >= 2) + mapping->filename = + linemap->map->vector[linemap->map->size-2].filename; + else + filename = "unknown"; } if (filename) { - /* Copy the filename (via shared storage) */ - copy = yasm__xstrdup(filename); - /*@-aliasunique@*/ - mapping->filename = HAMT_insert(linemap->filenames, copy, copy, - &replace, filename_delete_one); - /*@=aliasunique@*/ + /* Copy the filename (via shared storage) */ + copy = yasm__xstrdup(filename); + /*@-aliasunique@*/ + mapping->filename = HAMT_insert(linemap->filenames, copy, copy, + &replace, filename_delete_one); + /*@=aliasunique@*/ } mapping->line = linemap->current; @@ -128,7 +128,7 @@ yasm_linemap_set(yasm_linemap *linemap, const char *filename, unsigned long yasm_linemap_poke(yasm_linemap *linemap, const char *filename, - unsigned long file_line) + unsigned long file_line) { unsigned long line; line_mapping *mapping = &linemap->map->vector[linemap->map->size-1]; @@ -140,7 +140,7 @@ yasm_linemap_poke(yasm_linemap *linemap, const char *filename, linemap->current++; yasm_linemap_set(linemap, mapping->filename, mapping->file_line, - mapping->line_inc); + mapping->line_inc); return line; } @@ -164,10 +164,10 @@ yasm_linemap_create(void) /* initialize source line information array */ linemap->source_info_size = 2; linemap->source_info = yasm_xmalloc(linemap->source_info_size * - sizeof(line_source_info)); + sizeof(line_source_info)); for (i=0; isource_info_size; i++) { - linemap->source_info[i].bc = NULL; - linemap->source_info[i].source = NULL; + linemap->source_info[i].bc = NULL; + linemap->source_info[i].source = NULL; } return linemap; @@ -178,18 +178,18 @@ yasm_linemap_destroy(yasm_linemap *linemap) { size_t i; for (i=0; isource_info_size; i++) { - if (linemap->source_info[i].source) - yasm_xfree(linemap->source_info[i].source); + if (linemap->source_info[i].source) + yasm_xfree(linemap->source_info[i].source); } yasm_xfree(linemap->source_info); if (linemap->map) { - yasm_xfree(linemap->map->vector); - yasm_xfree(linemap->map); + yasm_xfree(linemap->map->vector); + yasm_xfree(linemap->map); } if (linemap->filenames) - HAMT_destroy(linemap->filenames, filename_delete_one); + HAMT_destroy(linemap->filenames, filename_delete_one); yasm_xfree(linemap); } @@ -202,24 +202,24 @@ yasm_linemap_get_current(yasm_linemap *linemap) void yasm_linemap_add_source(yasm_linemap *linemap, yasm_bytecode *bc, - const char *source) + const char *source) { size_t i; while (linemap->current > linemap->source_info_size) { - /* allocate another size bins when full for 2x space */ - linemap->source_info = yasm_xrealloc(linemap->source_info, - 2*linemap->source_info_size*sizeof(line_source_info)); - for (i=linemap->source_info_size; isource_info_size*2; i++) { - linemap->source_info[i].bc = NULL; - linemap->source_info[i].source = NULL; - } - linemap->source_info_size *= 2; + /* allocate another size bins when full for 2x space */ + linemap->source_info = yasm_xrealloc(linemap->source_info, + 2*linemap->source_info_size*sizeof(line_source_info)); + for (i=linemap->source_info_size; isource_info_size*2; i++) { + linemap->source_info[i].bc = NULL; + linemap->source_info[i].source = NULL; + } + linemap->source_info_size *= 2; } /* Delete existing info for that line (if any) */ if (linemap->source_info[linemap->current-1].source) - yasm_xfree(linemap->source_info[linemap->current-1].source); + yasm_xfree(linemap->source_info[linemap->current-1].source); linemap->source_info[linemap->current-1].bc = bc; linemap->source_info[linemap->current-1].source = yasm__xstrdup(source); @@ -233,7 +233,7 @@ yasm_linemap_goto_next(yasm_linemap *linemap) void yasm_linemap_lookup(yasm_linemap *linemap, unsigned long line, - const char **filename, unsigned long *file_line) + const char **filename, unsigned long *file_line) { line_mapping *mapping; unsigned long vindex, step; @@ -246,12 +246,12 @@ yasm_linemap_lookup(yasm_linemap *linemap, unsigned long line, /* start step as the greatest power of 2 <= size */ step = 1; while (step*2<=linemap->map->size) - step*=2; + step*=2; while (step>0) { - if (vindex+step < linemap->map->size - && linemap->map->vector[vindex+step].line <= line) - vindex += step; - step /= 2; + if (vindex+step < linemap->map->size + && linemap->map->vector[vindex+step].line <= line) + vindex += step; + step /= 2; } mapping = &linemap->map->vector[vindex]; @@ -261,19 +261,19 @@ yasm_linemap_lookup(yasm_linemap *linemap, unsigned long line, int yasm_linemap_traverse_filenames(yasm_linemap *linemap, /*@null@*/ void *d, - int (*func) (const char *filename, void *d)) + int (*func) (const char *filename, void *d)) { return HAMT_traverse(linemap->filenames, d, (int (*) (void *, void *))func); } int yasm_linemap_get_source(yasm_linemap *linemap, unsigned long line, - yasm_bytecode **bcp, const char **sourcep) + yasm_bytecode **bcp, const char **sourcep) { if (line > linemap->source_info_size) { - *bcp = NULL; - *sourcep = NULL; - return 1; + *bcp = NULL; + *sourcep = NULL; + return 1; } *bcp = linemap->source_info[line-1].bc; diff --git a/libyasm/linemap.h b/libyasm/linemap.h index 411d995e..0078b266 100644 --- a/libyasm/linemap.h +++ b/libyasm/linemap.h @@ -40,43 +40,43 @@ yasm_linemap *yasm_linemap_create(void); /** Clean up any memory allocated for a repository. - * \param linemap line mapping repository + * \param linemap line mapping repository */ void yasm_linemap_destroy(yasm_linemap *linemap); /** Get the current line position in a repository. - * \param linemap line mapping repository + * \param linemap line mapping repository * \return Current virtual line. */ unsigned long yasm_linemap_get_current(yasm_linemap *linemap); /** Get bytecode and source line information, if any, for a virtual line. - * \param linemap line mapping repository - * \param line virtual line - * \param bcp pointer to return bytecode into - * \param sourcep pointer to return source code line pointer into + * \param linemap line mapping repository + * \param line virtual line + * \param bcp pointer to return bytecode into + * \param sourcep pointer to return source code line pointer into * \return Zero if source line information available for line, nonzero if not. * \note If source line information is not available, bcp and sourcep targets * are set to NULL. */ int yasm_linemap_get_source(yasm_linemap *linemap, unsigned long line, - /*@null@*/ yasm_bytecode **bcp, - const char **sourcep); + /*@null@*/ yasm_bytecode **bcp, + const char **sourcep); /** Add bytecode and source line information to the current virtual line. * \attention Deletes any existing bytecode and source line information for * the current virtual line. - * \param linemap line mapping repository - * \param bc bytecode (if any) - * \param source source code line + * \param linemap line mapping repository + * \param bc bytecode (if any) + * \param source source code line * \note The source code line pointer is NOT kept, it is strdup'ed. */ void yasm_linemap_add_source(yasm_linemap *linemap, - /*@null@*/ yasm_bytecode *bc, - const char *source); + /*@null@*/ yasm_bytecode *bc, + const char *source); /** Go to the next line (increments the current virtual line). - * \param linemap line mapping repository + * \param linemap line mapping repository * \return The current (new) virtual line. */ unsigned long yasm_linemap_goto_next(yasm_linemap *linemap); @@ -84,43 +84,43 @@ unsigned long yasm_linemap_goto_next(yasm_linemap *linemap); /** Set a new file/line physical association starting point at the current * virtual line. line_inc indicates how much the "real" line is incremented * by for each virtual line increment (0 is perfectly legal). - * \param linemap line mapping repository - * \param filename physical file name (if NULL, not changed) - * \param file_line physical line number - * \param line_inc line increment + * \param linemap line mapping repository + * \param filename physical file name (if NULL, not changed) + * \param file_line physical line number + * \param line_inc line increment */ void yasm_linemap_set(yasm_linemap *linemap, /*@null@*/ const char *filename, - unsigned long file_line, unsigned long line_inc); + unsigned long file_line, unsigned long line_inc); /** Poke a single file/line association, restoring the original physical * association starting point. Caution: increments the current virtual line * twice. - * \param linemap line mapping repository - * \param filename physical file name (if NULL, not changed) - * \param file_line physical line number + * \param linemap line mapping repository + * \param filename physical file name (if NULL, not changed) + * \param file_line physical line number * \return The virtual line number of the poked association. */ unsigned long yasm_linemap_poke(yasm_linemap *linemap, - /*@null@*/ const char *filename, - unsigned long file_line); + /*@null@*/ const char *filename, + unsigned long file_line); /** Look up the associated physical file and line for a virtual line. - * \param linemap line mapping repository - * \param line virtual line - * \param filename physical file name (output) - * \param file_line physical line number (output) + * \param linemap line mapping repository + * \param line virtual line + * \param filename physical file name (output) + * \param file_line physical line number (output) */ void yasm_linemap_lookup(yasm_linemap *linemap, unsigned long line, - /*@out@*/ const char **filename, - /*@out@*/ unsigned long *file_line); + /*@out@*/ const char **filename, + /*@out@*/ unsigned long *file_line); /** Traverses all filenames used in a linemap, calling a function on each * filename. - * \param linemap line mapping repository - * \param d data pointer passed to func on each call - * \param func function + * \param linemap line mapping repository + * \param d data pointer passed to func on each call + * \param func function * \return Stops early (and returns func's return value) if func returns a - * nonzero value; otherwise 0. + * nonzero value; otherwise 0. */ int yasm_linemap_traverse_filenames (yasm_linemap *linemap, /*@null@*/ void *d, diff --git a/libyasm/listfmt.h b/libyasm/listfmt.h index 3d3a72a4..f862abee 100644 --- a/libyasm/listfmt.h +++ b/libyasm/listfmt.h @@ -60,7 +60,7 @@ typedef struct yasm_listfmt_module { * \return NULL if unable to initialize. */ /*@null@*/ /*@only@*/ yasm_listfmt * (*create) - (const char *in_filename, const char *obj_filename); + (const char *in_filename, const char *obj_filename); /** Module-level implementation of yasm_listfmt_destroy(). * Call yasm_listfmt_destroy() instead of calling this function. @@ -71,7 +71,7 @@ typedef struct yasm_listfmt_module { * Call yasm_listfmt_output() instead of calling this function. */ void (*output) (yasm_listfmt *listfmt, FILE *f, yasm_linemap *linemap, - yasm_arch *arch); + yasm_arch *arch); } yasm_listfmt_module; /** Get the keyword used to select a list format. @@ -83,7 +83,7 @@ const char *yasm_listfmt_keyword(const yasm_listfmt *listfmt); /** Initialize list format for use. Must call before any other list * format functions. The filenames are provided solely for informational * purposes. - * \param module list format module + * \param module list format module * \param in_filename primary input filename * \param obj_filename object filename * \return NULL if object format does not provide needed support. @@ -93,19 +93,19 @@ const char *yasm_listfmt_keyword(const yasm_listfmt *listfmt); const char *obj_filename); /** Cleans up any allocated list format memory. - * \param listfmt list format + * \param listfmt list format */ void yasm_listfmt_destroy(/*@only@*/ yasm_listfmt *listfmt); /** Write out list to the list file. * This function may call all read-only yasm_* functions as necessary. - * \param listfmt list format - * \param f output list file - * \param linemap line mapping repository - * \param arch architecture + * \param listfmt list format + * \param f output list file + * \param linemap line mapping repository + * \param arch architecture */ void yasm_listfmt_output(yasm_listfmt *listfmt, FILE *f, - yasm_linemap *linemap, yasm_arch *arch); + yasm_linemap *linemap, yasm_arch *arch); #ifndef YASM_DOXYGEN diff --git a/libyasm/md5.c b/libyasm/md5.c index af045fbe..e02bcf2c 100644 --- a/libyasm/md5.c +++ b/libyasm/md5.c @@ -42,17 +42,17 @@ static unsigned long getu32(const unsigned char *addr) { - return (((((unsigned long)addr[3] << 8) | addr[2]) << 8) - | addr[1]) << 8 | addr[0]; + return (((((unsigned long)addr[3] << 8) | addr[2]) << 8) + | addr[1]) << 8 | addr[0]; } static void putu32(unsigned long data, unsigned char *addr) { - addr[0] = (unsigned char)data; - addr[1] = (unsigned char)(data >> 8); - addr[2] = (unsigned char)(data >> 16); - addr[3] = (unsigned char)(data >> 24); + addr[0] = (unsigned char)data; + addr[1] = (unsigned char)(data >> 8); + addr[2] = (unsigned char)(data >> 16); + addr[3] = (unsigned char)(data >> 24); } /* @@ -62,13 +62,13 @@ putu32(unsigned long data, unsigned char *addr) void yasm_md5_init(yasm_md5_context *ctx) { - ctx->buf[0] = 0x67452301; - ctx->buf[1] = 0xefcdab89; - ctx->buf[2] = 0x98badcfe; - ctx->buf[3] = 0x10325476; + ctx->buf[0] = 0x67452301; + ctx->buf[1] = 0xefcdab89; + ctx->buf[2] = 0x98badcfe; + ctx->buf[3] = 0x10325476; - ctx->bits[0] = 0; - ctx->bits[1] = 0; + ctx->bits[0] = 0; + ctx->bits[1] = 0; } /* @@ -77,47 +77,47 @@ yasm_md5_init(yasm_md5_context *ctx) */ void yasm_md5_update(yasm_md5_context *ctx, unsigned char const *buf, - unsigned long len) + unsigned long len) { - unsigned long t; + unsigned long t; - /* Update bitcount */ + /* Update bitcount */ - t = ctx->bits[0]; - if ((ctx->bits[0] = (t + ((unsigned long)len << 3)) & 0xffffffff) < t) - ctx->bits[1]++; /* Carry from low to high */ - ctx->bits[1] += len >> 29; + t = ctx->bits[0]; + if ((ctx->bits[0] = (t + ((unsigned long)len << 3)) & 0xffffffff) < t) + ctx->bits[1]++; /* Carry from low to high */ + ctx->bits[1] += len >> 29; - t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ + t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ - /* Handle any leading odd-sized chunks */ + /* Handle any leading odd-sized chunks */ - if ( t ) { - unsigned char *p = ctx->in + t; + if ( t ) { + unsigned char *p = ctx->in + t; - t = 64-t; - if (len < t) { - memcpy(p, buf, len); - return; - } - memcpy(p, buf, t); - yasm_md5_transform (ctx->buf, ctx->in); - buf += t; - len -= t; - } + t = 64-t; + if (len < t) { + memcpy(p, buf, len); + return; + } + memcpy(p, buf, t); + yasm_md5_transform (ctx->buf, ctx->in); + buf += t; + len -= t; + } - /* Process data in 64-byte chunks */ + /* Process data in 64-byte chunks */ - while (len >= 64) { - memcpy(ctx->in, buf, 64); - yasm_md5_transform (ctx->buf, ctx->in); - buf += 64; - len -= 64; - } + while (len >= 64) { + memcpy(ctx->in, buf, 64); + yasm_md5_transform (ctx->buf, ctx->in); + buf += 64; + len -= 64; + } - /* Handle any remaining bytes of data. */ + /* Handle any remaining bytes of data. */ - memcpy(ctx->in, buf, len); + memcpy(ctx->in, buf, len); } /* @@ -127,43 +127,43 @@ yasm_md5_update(yasm_md5_context *ctx, unsigned char const *buf, void yasm_md5_final(unsigned char digest[16], yasm_md5_context *ctx) { - unsigned count; - unsigned char *p; - - /* Compute number of bytes mod 64 */ - count = (ctx->bits[0] >> 3) & 0x3F; - - /* Set the first char of padding to 0x80. This is safe since there is - always at least one byte free */ - p = ctx->in + count; - *p++ = 0x80; - - /* Bytes of padding needed to make 64 bytes */ - count = 64 - 1 - count; - - /* Pad out to 56 mod 64 */ - if (count < 8) { - /* Two lots of padding: Pad the first block to 64 bytes */ - memset(p, 0, count); - yasm_md5_transform (ctx->buf, ctx->in); - - /* Now fill the next block with 56 bytes */ - memset(ctx->in, 0, 56); - } else { - /* Pad block to 56 bytes */ - memset(p, 0, count-8); - } - - /* Append length in bits and transform */ - putu32(ctx->bits[0], ctx->in + 56); - putu32(ctx->bits[1], ctx->in + 60); - - yasm_md5_transform (ctx->buf, ctx->in); - putu32(ctx->buf[0], digest); - putu32(ctx->buf[1], digest + 4); - putu32(ctx->buf[2], digest + 8); - putu32(ctx->buf[3], digest + 12); - memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ + unsigned count; + unsigned char *p; + + /* Compute number of bytes mod 64 */ + count = (ctx->bits[0] >> 3) & 0x3F; + + /* Set the first char of padding to 0x80. This is safe since there is + always at least one byte free */ + p = ctx->in + count; + *p++ = 0x80; + + /* Bytes of padding needed to make 64 bytes */ + count = 64 - 1 - count; + + /* Pad out to 56 mod 64 */ + if (count < 8) { + /* Two lots of padding: Pad the first block to 64 bytes */ + memset(p, 0, count); + yasm_md5_transform (ctx->buf, ctx->in); + + /* Now fill the next block with 56 bytes */ + memset(ctx->in, 0, 56); + } else { + /* Pad block to 56 bytes */ + memset(p, 0, count-8); + } + + /* Append length in bits and transform */ + putu32(ctx->bits[0], ctx->in + 56); + putu32(ctx->bits[1], ctx->in + 60); + + yasm_md5_transform (ctx->buf, ctx->in); + putu32(ctx->buf[0], digest); + putu32(ctx->buf[1], digest + 4); + putu32(ctx->buf[2], digest + 8); + putu32(ctx->buf[3], digest + 12); + memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */ } #ifndef ASM_MD5 @@ -178,7 +178,7 @@ yasm_md5_final(unsigned char digest[16], yasm_md5_context *ctx) /* This is the central step in the MD5 algorithm. */ #define MD5STEP(f, w, x, y, z, data, s) \ - ( w += f(x, y, z) + data, w &= 0xffffffff, w = w<>(32-s), w += x ) + ( w += f(x, y, z) + data, w &= 0xffffffff, w = w<>(32-s), w += x ) /* * The core of the MD5 algorithm, this alters an existing MD5 hash to @@ -188,90 +188,90 @@ yasm_md5_final(unsigned char digest[16], yasm_md5_context *ctx) void yasm_md5_transform(unsigned long buf[4], const unsigned char inraw[64]) { - register unsigned long a, b, c, d; - unsigned long in[16]; - int i; - - for (i = 0; i < 16; ++i) - in[i] = getu32 (inraw + 4 * i); - - a = buf[0]; - b = buf[1]; - c = buf[2]; - d = buf[3]; - - MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); - MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); - MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); - MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); - MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); - MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); - MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); - MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); - MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); - MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); - MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); - MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); - MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); - MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); - MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); - MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); - - MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); - MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); - MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); - MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); - MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); - MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); - MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); - MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); - MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); - MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); - MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); - MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); - MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); - MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); - MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); - MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); - - MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); - MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); - MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); - MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); - MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); - MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); - MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); - MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); - MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); - MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); - MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); - MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); - MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); - MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); - MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); - MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); - - MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); - MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); - MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); - MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); - MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); - MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); - MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); - MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); - MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); - MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); - MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); - MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); - MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); - MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); - MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); - MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); - - buf[0] += a; - buf[1] += b; - buf[2] += c; - buf[3] += d; + register unsigned long a, b, c, d; + unsigned long in[16]; + int i; + + for (i = 0; i < 16; ++i) + in[i] = getu32 (inraw + 4 * i); + + a = buf[0]; + b = buf[1]; + c = buf[2]; + d = buf[3]; + + MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); + MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); + MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); + MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); + MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); + MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); + MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); + MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); + MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); + MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); + MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); + MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); + MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); + MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); + MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); + MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); + + MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); + MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); + MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); + MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); + MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); + MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); + MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); + MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); + MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); + MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); + MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); + MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); + MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); + MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); + MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); + MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); + + MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); + MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); + MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); + MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); + MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); + MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); + MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); + MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); + MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); + MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); + MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); + MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); + MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); + MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); + MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); + MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); + + MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); + MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); + MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); + MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); + MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); + MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); + MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); + MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); + MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); + MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); + MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); + MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); + MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); + MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); + MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); + MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); + + buf[0] += a; + buf[1] += b; + buf[2] += c; + buf[3] += d; } #endif @@ -283,28 +283,28 @@ yasm_md5_transform(unsigned long buf[4], const unsigned char inraw[64]) int main (int argc, char **argv) { - yasm_md5_context context; - unsigned char checksum[16]; - int i; - int j; - - if (argc < 2) - { - fprintf (stderr, "usage: %s string-to-hash\n", argv[0]); - exit (1); - } - for (j = 1; j < argc; ++j) - { - printf ("MD5 (\"%s\") = ", argv[j]); - yasm_md5_init (&context); - yasm_md5_update (&context, argv[j], strlen (argv[j])); - yasm_md5_final (checksum, &context); - for (i = 0; i < 16; i++) - { - printf ("%02x", (unsigned int) checksum[i]); - } - printf ("\n"); - } - return 0; + yasm_md5_context context; + unsigned char checksum[16]; + int i; + int j; + + if (argc < 2) + { + fprintf (stderr, "usage: %s string-to-hash\n", argv[0]); + exit (1); + } + for (j = 1; j < argc; ++j) + { + printf ("MD5 (\"%s\") = ", argv[j]); + yasm_md5_init (&context); + yasm_md5_update (&context, argv[j], strlen (argv[j])); + yasm_md5_final (checksum, &context); + for (i = 0; i < 16; i++) + { + printf ("%02x", (unsigned int) checksum[i]); + } + printf ("\n"); + } + return 0; } #endif /* TEST */ diff --git a/libyasm/md5.h b/libyasm/md5.h index 91be1c55..a7dbc9ab 100644 --- a/libyasm/md5.h +++ b/libyasm/md5.h @@ -14,14 +14,14 @@ and always using it seems to have few disadvantages. */ typedef struct yasm_md5_context { - unsigned long buf[4]; - unsigned long bits[2]; - unsigned char in[64]; + unsigned long buf[4]; + unsigned long bits[2]; + unsigned char in[64]; } yasm_md5_context; void yasm_md5_init(yasm_md5_context *context); void yasm_md5_update(yasm_md5_context *context, unsigned char const *buf, - unsigned long len); + unsigned long len); void yasm_md5_final(unsigned char digest[16], yasm_md5_context *context); void yasm_md5_transform(unsigned long buf[4], const unsigned char in[64]); diff --git a/libyasm/mergesort.c b/libyasm/mergesort.c index 87e451d0..82fe52a0 100644 --- a/libyasm/mergesort.c +++ b/libyasm/mergesort.c @@ -2,7 +2,7 @@ * mergesort() implementation for systems that don't have it. * * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Peter McIlroy. @@ -36,7 +36,7 @@ /*@unused@*/ RCSID("$Id$"); #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; +static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; #endif /* LIBC_SCCS and not lint */ #ifdef HAVE_MERGESORT @@ -52,7 +52,7 @@ static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; */ #define NATURAL -#define THRESHOLD 16 /* Best choice for natural merge cut-off. */ +#define THRESHOLD 16 /* Best choice for natural merge cut-off. */ /* #define NATURAL to get hybrid natural merge. * (The default is pairwise merging.) @@ -64,29 +64,29 @@ static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94"; #endif static void setup(unsigned char *, unsigned char *, size_t, size_t, - int (*)(const void *, const void *)); + int (*)(const void *, const void *)); static void insertionsort(unsigned char *, size_t, size_t, - int (*)(const void *, const void *)); + int (*)(const void *, const void *)); #define ISIZE sizeof(int) #define PSIZE sizeof(unsigned char *) -#define ICOPY_LIST(src, dst, last) \ - do \ - *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ - while(src < last) -#define ICOPY_ELT(src, dst, i) \ - do \ - *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ - while (i -= ISIZE) +#define ICOPY_LIST(src, dst, last) \ + do \ + *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ + while(src < last) +#define ICOPY_ELT(src, dst, i) \ + do \ + *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ + while (i -= ISIZE) -#define CCOPY_LIST(src, dst, last) \ - do \ - *dst++ = *src++; \ - while (src < last) -#define CCOPY_ELT(src, dst, i) \ - do \ - *dst++ = *src++; \ - while (i -= 1) +#define CCOPY_LIST(src, dst, last) \ + do \ + *dst++ = *src++; \ + while (src < last) +#define CCOPY_ELT(src, dst, i) \ + do \ + *dst++ = *src++; \ + while (i -= 1) /* * Find the next possible pointer head. (Trickery for forcing an array @@ -94,176 +94,176 @@ static void insertionsort(unsigned char *, size_t, size_t, * boundaries. */ /* Assumption: PSIZE is a power of 2. */ -#define EVAL(p) (unsigned char **) \ - ((unsigned char *)0 + \ - (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1))) -#endif /*HAVE_MERGESORT*/ +#define EVAL(p) (unsigned char **) \ + ((unsigned char *)0 + \ + (((unsigned char *)p + PSIZE - 1 - (unsigned char *) 0) & ~(PSIZE - 1))) +#endif /*HAVE_MERGESORT*/ /* * Arguments are as for qsort. */ int yasm__mergesort(void *base, size_t nmemb, size_t size, - int (*cmp)(const void *, const void *)) + int (*cmp)(const void *, const void *)) { #ifdef HAVE_MERGESORT return mergesort(base, nmemb, size, cmp); #else - size_t i; - int sense; - int big, iflag; - unsigned char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; - unsigned char *list2, *list1, *p2, *p, *last, **p1; + size_t i; + int sense; + int big, iflag; + unsigned char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; + unsigned char *list2, *list1, *p2, *p, *last, **p1; - if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ + if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ #ifdef EINVAL - errno = EINVAL; + errno = EINVAL; #endif - return (-1); - } + return (-1); + } - if (nmemb == 0) - return (0); + if (nmemb == 0) + return (0); - /* - * XXX - * Stupid subtraction for the Cray. - */ - iflag = 0; - if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) - iflag = 1; + /* + * XXX + * Stupid subtraction for the Cray. + */ + iflag = 0; + if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) + iflag = 1; - if ((list2 = yasm_xmalloc(nmemb * size + PSIZE)) == NULL) - return (-1); + if ((list2 = yasm_xmalloc(nmemb * size + PSIZE)) == NULL) + return (-1); - list1 = base; - setup(list1, list2, nmemb, size, cmp); - last = list2 + nmemb * size; - i = 0; - big = 0; - while (*EVAL(list2) != last) { - l2 = list1; - p1 = EVAL(list1); - for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { - p2 = *EVAL(p2); - f1 = l2; - f2 = l1 = list1 + (p2 - list2); - if (p2 != last) - p2 = *EVAL(p2); - l2 = list1 + (p2 - list2); - while (f1 < l1 && f2 < l2) { - if ((*cmp)(f1, f2) <= 0) { - q = f2; - b = f1, t = l1; - sense = -1; - } else { - q = f1; - b = f2, t = l2; - sense = 0; - } - if (!big) { /* here i = 0 */ - while ((b += size) < t && cmp(q, b) >sense) - if (++i == 6) { - big = 1; - goto EXPONENTIAL; - } - } else { -EXPONENTIAL: for (i = size; ; i <<= 1) - if ((p = (b + i)) >= t) { - if ((p = t - size) > b && - (*cmp)(q, p) <= sense) - t = p; - else - b = p; - break; - } else if ((*cmp)(q, p) <= sense) { - t = p; - if (i == size) - big = 0; - goto FASTCASE; - } else - b = p; - while (t > b+size) { - i = (((t - b) / size) >> 1) * size; - if ((*cmp)(q, p = b + i) <= sense) - t = p; - else - b = p; - } - goto COPY; -FASTCASE: while (i > size) - if ((*cmp)(q, - p = b + (i >>= 1)) <= sense) - t = p; - else - b = p; -COPY: b = t; - } - i = size; - if (q == f1) { - if (iflag) { - ICOPY_LIST(f2, tp2, b); - ICOPY_ELT(f1, tp2, i); - } else { - CCOPY_LIST(f2, tp2, b); - CCOPY_ELT(f1, tp2, i); - } - } else { - if (iflag) { - ICOPY_LIST(f1, tp2, b); - ICOPY_ELT(f2, tp2, i); - } else { - CCOPY_LIST(f1, tp2, b); - CCOPY_ELT(f2, tp2, i); - } - } - } - if (f2 < l2) { - if (iflag) - ICOPY_LIST(f2, tp2, l2); - else - CCOPY_LIST(f2, tp2, l2); - } else if (f1 < l1) { - if (iflag) - ICOPY_LIST(f1, tp2, l1); - else - CCOPY_LIST(f1, tp2, l1); - } - *p1 = l2; - } - tp2 = list1; /* swap list1, list2 */ - list1 = list2; - list2 = tp2; - last = list2 + nmemb*size; - } - if (base == list2) { - memmove(list2, list1, nmemb*size); - list2 = list1; - } - yasm_xfree(list2); - return (0); -#endif /*HAVE_MERGESORT*/ + list1 = base; + setup(list1, list2, nmemb, size, cmp); + last = list2 + nmemb * size; + i = 0; + big = 0; + while (*EVAL(list2) != last) { + l2 = list1; + p1 = EVAL(list1); + for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { + p2 = *EVAL(p2); + f1 = l2; + f2 = l1 = list1 + (p2 - list2); + if (p2 != last) + p2 = *EVAL(p2); + l2 = list1 + (p2 - list2); + while (f1 < l1 && f2 < l2) { + if ((*cmp)(f1, f2) <= 0) { + q = f2; + b = f1, t = l1; + sense = -1; + } else { + q = f1; + b = f2, t = l2; + sense = 0; + } + if (!big) { /* here i = 0 */ + while ((b += size) < t && cmp(q, b) >sense) + if (++i == 6) { + big = 1; + goto EXPONENTIAL; + } + } else { +EXPONENTIAL: for (i = size; ; i <<= 1) + if ((p = (b + i)) >= t) { + if ((p = t - size) > b && + (*cmp)(q, p) <= sense) + t = p; + else + b = p; + break; + } else if ((*cmp)(q, p) <= sense) { + t = p; + if (i == size) + big = 0; + goto FASTCASE; + } else + b = p; + while (t > b+size) { + i = (((t - b) / size) >> 1) * size; + if ((*cmp)(q, p = b + i) <= sense) + t = p; + else + b = p; + } + goto COPY; +FASTCASE: while (i > size) + if ((*cmp)(q, + p = b + (i >>= 1)) <= sense) + t = p; + else + b = p; +COPY: b = t; + } + i = size; + if (q == f1) { + if (iflag) { + ICOPY_LIST(f2, tp2, b); + ICOPY_ELT(f1, tp2, i); + } else { + CCOPY_LIST(f2, tp2, b); + CCOPY_ELT(f1, tp2, i); + } + } else { + if (iflag) { + ICOPY_LIST(f1, tp2, b); + ICOPY_ELT(f2, tp2, i); + } else { + CCOPY_LIST(f1, tp2, b); + CCOPY_ELT(f2, tp2, i); + } + } + } + if (f2 < l2) { + if (iflag) + ICOPY_LIST(f2, tp2, l2); + else + CCOPY_LIST(f2, tp2, l2); + } else if (f1 < l1) { + if (iflag) + ICOPY_LIST(f1, tp2, l1); + else + CCOPY_LIST(f1, tp2, l1); + } + *p1 = l2; + } + tp2 = list1; /* swap list1, list2 */ + list1 = list2; + list2 = tp2; + last = list2 + nmemb*size; + } + if (base == list2) { + memmove(list2, list1, nmemb*size); + list2 = list1; + } + yasm_xfree(list2); + return (0); +#endif /*HAVE_MERGESORT*/ } #ifndef HAVE_MERGESORT -#define swap(a, b) { \ - s = b; \ - i = size; \ - do { \ - tmp = *a; *a++ = *s; *s++ = tmp; \ - } while (--i); \ - a -= size; \ - } -#define reverse(bot, top) { \ - s = top; \ - do { \ - i = size; \ - do { \ - tmp = *bot; *bot++ = *s; *s++ = tmp; \ - } while (--i); \ - s -= size2; \ - } while(bot < s); \ +#define swap(a, b) { \ + s = b; \ + i = size; \ + do { \ + tmp = *a; *a++ = *s; *s++ = tmp; \ + } while (--i); \ + a -= size; \ + } +#define reverse(bot, top) { \ + s = top; \ + do { \ + i = size; \ + do { \ + tmp = *bot; *bot++ = *s; *s++ = tmp; \ + } while (--i); \ + s -= size2; \ + } while(bot < s); \ } /* @@ -276,70 +276,70 @@ void setup(unsigned char *list1, unsigned char *list2, size_t n, size_t size, int (*cmp)(const void *, const void *)) { - size_t i; - unsigned int tmp; - int length, sense; - size_t size2; - unsigned char *f1, *f2, *s, *l2, *last, *p2; + size_t i; + unsigned int tmp; + int length, sense; + size_t size2; + unsigned char *f1, *f2, *s, *l2, *last, *p2; - size2 = size*2; - if (n <= 5) { - insertionsort(list1, n, size, cmp); - *EVAL(list2) = (unsigned char*) list2 + n*size; - return; - } - /* - * Avoid running pointers out of bounds; limit n to evens - * for simplicity. - */ - i = 4 + (n & 1); - insertionsort(list1 + (n - i) * size, i, size, cmp); - last = list1 + size * (n - i); - *EVAL(list2 + (last - list1)) = list2 + n * size; + size2 = size*2; + if (n <= 5) { + insertionsort(list1, n, size, cmp); + *EVAL(list2) = (unsigned char*) list2 + n*size; + return; + } + /* + * Avoid running pointers out of bounds; limit n to evens + * for simplicity. + */ + i = 4 + (n & 1); + insertionsort(list1 + (n - i) * size, i, size, cmp); + last = list1 + size * (n - i); + *EVAL(list2 + (last - list1)) = list2 + n * size; #ifdef NATURAL - p2 = list2; - f1 = list1; - sense = (cmp(f1, f1 + size) > 0); - for (; f1 < last; sense = !sense) { - length = 2; - /* Find pairs with same sense. */ - for (f2 = f1 + size2; f2 < last; f2 += size2) { - if ((cmp(f2, f2+ size) > 0) != sense) - break; - length += 2; - } - if (length < THRESHOLD) { /* Pairwise merge */ - do { - p2 = *EVAL(p2) = f1 + size2 - list1 + list2; - if (sense > 0) - swap (f1, f1 + size); - } while ((f1 += size2) < f2); - } else { /* Natural merge */ - l2 = f2; - for (f2 = f1 + size2; f2 < l2; f2 += size2) { - if ((cmp(f2-size, f2) > 0) != sense) { - p2 = *EVAL(p2) = f2 - list1 + list2; - if (sense > 0) - reverse(f1, f2-size); - f1 = f2; - } - } - if (sense > 0) - reverse (f1, f2-size); - f1 = f2; - if (f2 < last || cmp(f2 - size, f2) > 0) - p2 = *EVAL(p2) = f2 - list1 + list2; - else - p2 = *EVAL(p2) = list2 + n*size; - } - } -#else /* pairwise merge only. */ - for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { - p2 = *EVAL(p2) = p2 + size2; - if (cmp (f1, f1 + size) > 0) - swap(f1, f1 + size); - } + p2 = list2; + f1 = list1; + sense = (cmp(f1, f1 + size) > 0); + for (; f1 < last; sense = !sense) { + length = 2; + /* Find pairs with same sense. */ + for (f2 = f1 + size2; f2 < last; f2 += size2) { + if ((cmp(f2, f2+ size) > 0) != sense) + break; + length += 2; + } + if (length < THRESHOLD) { /* Pairwise merge */ + do { + p2 = *EVAL(p2) = f1 + size2 - list1 + list2; + if (sense > 0) + swap (f1, f1 + size); + } while ((f1 += size2) < f2); + } else { /* Natural merge */ + l2 = f2; + for (f2 = f1 + size2; f2 < l2; f2 += size2) { + if ((cmp(f2-size, f2) > 0) != sense) { + p2 = *EVAL(p2) = f2 - list1 + list2; + if (sense > 0) + reverse(f1, f2-size); + f1 = f2; + } + } + if (sense > 0) + reverse (f1, f2-size); + f1 = f2; + if (f2 < last || cmp(f2 - size, f2) > 0) + p2 = *EVAL(p2) = f2 - list1 + list2; + else + p2 = *EVAL(p2) = list2 + n*size; + } + } +#else /* pairwise merge only. */ + for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { + p2 = *EVAL(p2) = p2 + size2; + if (cmp (f1, f1 + size) > 0) + swap(f1, f1 + size); + } #endif /* NATURAL */ } @@ -349,17 +349,17 @@ setup(unsigned char *list1, unsigned char *list2, size_t n, size_t size, */ static void insertionsort(unsigned char *a, size_t n, size_t size, - int (*cmp)(const void *, const void *)) + int (*cmp)(const void *, const void *)) { - unsigned char *ai, *s, *t, *u, tmp; - size_t i; + unsigned char *ai, *s, *t, *u, tmp; + size_t i; - for (ai = a+size; --n >= 1; ai += size) - for (t = ai; t > a; t -= size) { - u = t - size; - if (cmp(u, t) <= 0) - break; - swap(u, t); - } + for (ai = a+size; --n >= 1; ai += size) + for (t = ai; t > a; t -= size) { + u = t - size; + if (cmp(u, t) <= 0) + break; + swap(u, t); + } } -#endif /*HAVE_MERGESORT*/ +#endif /*HAVE_MERGESORT*/ diff --git a/libyasm/module.h b/libyasm/module.h index 4606057f..11439e6d 100644 --- a/libyasm/module.h +++ b/libyasm/module.h @@ -39,34 +39,34 @@ typedef enum yasm_module_type { /*@dependent@*/ /*@null@*/ void *yasm_load_module (yasm_module_type type, const char *keyword); -#define yasm_load_arch(keyword) \ +#define yasm_load_arch(keyword) \ yasm_load_module(YASM_MODULE_ARCH, keyword) -#define yasm_load_dbgfmt(keyword) \ +#define yasm_load_dbgfmt(keyword) \ yasm_load_module(YASM_MODULE_DBGFMT, keyword) -#define yasm_load_objfmt(keyword) \ +#define yasm_load_objfmt(keyword) \ yasm_load_module(YASM_MODULE_OBJFMT, keyword) -#define yasm_load_listfmt(keyword) \ +#define yasm_load_listfmt(keyword) \ yasm_load_module(YASM_MODULE_LISTFMT, keyword) -#define yasm_load_parser(keyword) \ +#define yasm_load_parser(keyword) \ yasm_load_module(YASM_MODULE_PARSER, keyword) -#define yasm_load_preproc(keyword) \ +#define yasm_load_preproc(keyword) \ yasm_load_module(YASM_MODULE_PREPROC, keyword) void yasm_list_modules (yasm_module_type type, void (*printfunc) (const char *name, const char *keyword)); -#define yasm_list_arch(func) \ +#define yasm_list_arch(func) \ yasm_list_modules(YASM_MODULE_ARCH, func) -#define yasm_list_dbgfmt(func) \ +#define yasm_list_dbgfmt(func) \ yasm_list_modules(YASM_MODULE_DBGFMT, func) -#define yasm_list_objfmt(func) \ +#define yasm_list_objfmt(func) \ yasm_list_modules(YASM_MODULE_OBJFMT, func) -#define yasm_list_listfmt(func) \ +#define yasm_list_listfmt(func) \ yasm_list_modules(YASM_MODULE_LISTFMT, func) -#define yasm_list_parser(func) \ +#define yasm_list_parser(func) \ yasm_list_modules(YASM_MODULE_PARSER, func) -#define yasm_list_preproc(func) \ +#define yasm_list_preproc(func) \ yasm_list_modules(YASM_MODULE_PREPROC, func) #endif diff --git a/libyasm/objfmt.h b/libyasm/objfmt.h index ce610b06..8d73bebe 100644 --- a/libyasm/objfmt.h +++ b/libyasm/objfmt.h @@ -80,8 +80,8 @@ struct yasm_objfmt_module { /** Create object format. * Module-level implementation of yasm_objfmt_create(). * Call yasm_objfmt_create() instead of calling this function. - * \param object object - * \param a architecture in use + * \param object object + * \param a architecture in use * \return NULL if architecture/machine combination not supported. */ /*@null@*/ /*@only@*/ yasm_objfmt * (*create) (yasm_object *object); @@ -90,7 +90,7 @@ struct yasm_objfmt_module { * Call yasm_objfmt_output() instead of calling this function. */ void (*output) (yasm_object *o, FILE *f, int all_syms, - yasm_errwarns *errwarns); + yasm_errwarns *errwarns); /** Module-level implementation of yasm_objfmt_destroy(). * Call yasm_objfmt_destroy() instead of calling this function. @@ -106,14 +106,14 @@ struct yasm_objfmt_module { * Call yasm_objfmt_section_switch() instead of calling this function. */ /*@observer@*/ /*@null@*/ yasm_section * - (*section_switch)(yasm_object *object, yasm_valparamhead *valparams, - /*@null@*/ yasm_valparamhead *objext_valparams, - unsigned long line); + (*section_switch)(yasm_object *object, yasm_valparamhead *valparams, + /*@null@*/ yasm_valparamhead *objext_valparams, + unsigned long line); }; /** Create object format. - * \param module object format module - * \param object object + * \param module object format module + * \param object object * \return NULL if architecture/machine combination not supported. */ /*@null@*/ /*@only@*/ yasm_objfmt *yasm_objfmt_create @@ -122,18 +122,18 @@ struct yasm_objfmt_module { /** Write out (post-optimized) sections to the object file. * This function may call yasm_symrec_* functions as necessary (including * yasm_symrec_traverse()) to retrieve symbolic information. - * \param object object - * \param f output object file - * \param all_syms if nonzero, all symbols should be included in - * the object file - * \param errwarns error/warning set + * \param object object + * \param f output object file + * \param all_syms if nonzero, all symbols should be included in + * the object file + * \param errwarns error/warning set * \note Errors and warnings are stored into errwarns. */ void yasm_objfmt_output(yasm_object *object, FILE *f, int all_syms, - yasm_errwarns *errwarns); + yasm_errwarns *errwarns); /** Cleans up any allocated object format memory. - * \param objfmt object format + * \param objfmt object format */ void yasm_objfmt_destroy(/*@only@*/ yasm_objfmt *objfmt); @@ -146,10 +146,10 @@ yasm_section *yasm_objfmt_add_default_section(yasm_object *object); /** Switch object file sections. The first val of the valparams should * be the section name. Calls yasm_object_get_general() to actually get * the section. - * \param object object - * \param valparams value/parameters - * \param objext_valparams object format-specific value/parameters - * \param line virtual line (from yasm_linemap) + * \param object object + * \param valparams value/parameters + * \param objext_valparams object format-specific value/parameters + * \param line virtual line (from yasm_linemap) * \return NULL on error, otherwise new section. */ /*@observer@*/ /*@null@*/ yasm_section *yasm_objfmt_section_switch @@ -164,15 +164,15 @@ yasm_section *yasm_objfmt_add_default_section(yasm_object *object); #define yasm_objfmt_output(object, f, all_syms, ews) \ ((yasm_objfmt_base *)((object)->objfmt))->module->output \ - (object, f, all_syms, ews) + (object, f, all_syms, ews) #define yasm_objfmt_destroy(objfmt) \ ((yasm_objfmt_base *)objfmt)->module->destroy(objfmt) #define yasm_objfmt_section_switch(object, vpms, oe_vpms, line) \ ((yasm_objfmt_base *)((object)->objfmt))->module->section_switch \ - (object, vpms, oe_vpms, line) + (object, vpms, oe_vpms, line) #define yasm_objfmt_add_default_section(object) \ ((yasm_objfmt_base *)((object)->objfmt))->module->add_default_section \ - (object) + (object) #endif diff --git a/libyasm/parser.h b/libyasm/parser.h index e85b217e..0583cbe2 100644 --- a/libyasm/parser.h +++ b/libyasm/parser.h @@ -52,18 +52,18 @@ typedef struct yasm_parser_module { const char *default_preproc_keyword; /** Parse a source file into an object. - * \param object object to parse into (already created) - * \param pp preprocessor - * \param f initial starting file - * \param save_input nonzero if the parser should save the original - * lines of source into the object's linemap (via - * yasm_linemap_add_data()). - * \param errwarns error/warning set + * \param object object to parse into (already created) + * \param pp preprocessor + * \param f initial starting file + * \param save_input nonzero if the parser should save the original + * lines of source into the object's linemap (via + * yasm_linemap_add_data()). + * \param errwarns error/warning set * \note Parse errors and warnings are stored into errwarns. */ void (*do_parse) - (yasm_object *object, yasm_preproc *pp, FILE *f, int save_input, - yasm_linemap *linemap, yasm_errwarns *errwarns); + (yasm_object *object, yasm_preproc *pp, FILE *f, int save_input, + yasm_linemap *linemap, yasm_errwarns *errwarns); } yasm_parser_module; #endif diff --git a/libyasm/phash.c b/libyasm/phash.c index 01941c28..2ce1938d 100644 --- a/libyasm/phash.c +++ b/libyasm/phash.c @@ -111,36 +111,36 @@ phash_lookup( /*---------------------------------------- handle most of the key */ while (len >= 12) { - a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24)); - a &= 0xffffffff; - b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24)); - b &= 0xffffffff; - c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24)); - c &= 0xffffffff; - mix(a,b,c); - k += 12; len -= 12; + a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24)); + a &= 0xffffffff; + b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24)); + b &= 0xffffffff; + c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24)); + c &= 0xffffffff; + mix(a,b,c); + k += 12; len -= 12; } /*------------------------------------- handle the last 11 bytes */ c += (ub4)length; switch(len) /* all the case statements fall through */ { - case 11: c+=((ub4)k[10]<<24); - case 10: c+=((ub4)k[9]<<16); - case 9 : c+=((ub4)k[8]<<8); - c &= 0xffffffff; - /* the first byte of c is reserved for the length */ - case 8 : b+=((ub4)k[7]<<24); - case 7 : b+=((ub4)k[6]<<16); - case 6 : b+=((ub4)k[5]<<8); - case 5 : b+=k[4]; - b &= 0xffffffff; - case 4 : a+=((ub4)k[3]<<24); - case 3 : a+=((ub4)k[2]<<16); - case 2 : a+=((ub4)k[1]<<8); - case 1 : a+=k[0]; - a &= 0xffffffff; - /* case 0: nothing left to add */ + case 11: c+=((ub4)k[10]<<24); + case 10: c+=((ub4)k[9]<<16); + case 9 : c+=((ub4)k[8]<<8); + c &= 0xffffffff; + /* the first byte of c is reserved for the length */ + case 8 : b+=((ub4)k[7]<<24); + case 7 : b+=((ub4)k[6]<<16); + case 6 : b+=((ub4)k[5]<<8); + case 5 : b+=k[4]; + b &= 0xffffffff; + case 4 : a+=((ub4)k[3]<<24); + case 3 : a+=((ub4)k[2]<<16); + case 2 : a+=((ub4)k[1]<<8); + case 1 : a+=k[0]; + a &= 0xffffffff; + /* case 0: nothing left to add */ } mix(a,b,c); /*-------------------------------------------- report the result */ @@ -208,56 +208,56 @@ phash_checksum( /*---------------------------------------- handle most of the key */ while (len >= 32) { - a += (k[0] +(k[1]<<8) +(k[2]<<16) +(k[3]<<24)); - b += (k[4] +(k[5]<<8) +(k[6]<<16) +(k[7]<<24)); - c += (k[8] +(k[9]<<8) +(k[10]<<16)+(k[11]<<24)); - d += (k[12]+(k[13]<<8)+(k[14]<<16)+(k[15]<<24)); - e += (k[16]+(k[17]<<8)+(k[18]<<16)+(k[19]<<24)); - f += (k[20]+(k[21]<<8)+(k[22]<<16)+(k[23]<<24)); - g += (k[24]+(k[25]<<8)+(k[26]<<16)+(k[27]<<24)); - h += (k[28]+(k[29]<<8)+(k[30]<<16)+(k[31]<<24)); - mixc(a,b,c,d,e,f,g,h); - mixc(a,b,c,d,e,f,g,h); - mixc(a,b,c,d,e,f,g,h); - mixc(a,b,c,d,e,f,g,h); - k += 32; len -= 32; + a += (k[0] +(k[1]<<8) +(k[2]<<16) +(k[3]<<24)); + b += (k[4] +(k[5]<<8) +(k[6]<<16) +(k[7]<<24)); + c += (k[8] +(k[9]<<8) +(k[10]<<16)+(k[11]<<24)); + d += (k[12]+(k[13]<<8)+(k[14]<<16)+(k[15]<<24)); + e += (k[16]+(k[17]<<8)+(k[18]<<16)+(k[19]<<24)); + f += (k[20]+(k[21]<<8)+(k[22]<<16)+(k[23]<<24)); + g += (k[24]+(k[25]<<8)+(k[26]<<16)+(k[27]<<24)); + h += (k[28]+(k[29]<<8)+(k[30]<<16)+(k[31]<<24)); + mixc(a,b,c,d,e,f,g,h); + mixc(a,b,c,d,e,f,g,h); + mixc(a,b,c,d,e,f,g,h); + mixc(a,b,c,d,e,f,g,h); + k += 32; len -= 32; } /*------------------------------------- handle the last 31 bytes */ h += (ub4)length; switch(len) { - case 31: h+=(k[30]<<24); - case 30: h+=(k[29]<<16); - case 29: h+=(k[28]<<8); - case 28: g+=(k[27]<<24); - case 27: g+=(k[26]<<16); - case 26: g+=(k[25]<<8); - case 25: g+=k[24]; - case 24: f+=(k[23]<<24); - case 23: f+=(k[22]<<16); - case 22: f+=(k[21]<<8); - case 21: f+=k[20]; - case 20: e+=(k[19]<<24); - case 19: e+=(k[18]<<16); - case 18: e+=(k[17]<<8); - case 17: e+=k[16]; - case 16: d+=(k[15]<<24); - case 15: d+=(k[14]<<16); - case 14: d+=(k[13]<<8); - case 13: d+=k[12]; - case 12: c+=(k[11]<<24); - case 11: c+=(k[10]<<16); - case 10: c+=(k[9]<<8); - case 9 : c+=k[8]; - case 8 : b+=(k[7]<<24); - case 7 : b+=(k[6]<<16); - case 6 : b+=(k[5]<<8); - case 5 : b+=k[4]; - case 4 : a+=(k[3]<<24); - case 3 : a+=(k[2]<<16); - case 2 : a+=(k[1]<<8); - case 1 : a+=k[0]; + case 31: h+=(k[30]<<24); + case 30: h+=(k[29]<<16); + case 29: h+=(k[28]<<8); + case 28: g+=(k[27]<<24); + case 27: g+=(k[26]<<16); + case 26: g+=(k[25]<<8); + case 25: g+=k[24]; + case 24: f+=(k[23]<<24); + case 23: f+=(k[22]<<16); + case 22: f+=(k[21]<<8); + case 21: f+=k[20]; + case 20: e+=(k[19]<<24); + case 19: e+=(k[18]<<16); + case 18: e+=(k[17]<<8); + case 17: e+=k[16]; + case 16: d+=(k[15]<<24); + case 15: d+=(k[14]<<16); + case 14: d+=(k[13]<<8); + case 13: d+=k[12]; + case 12: c+=(k[11]<<24); + case 11: c+=(k[10]<<16); + case 10: c+=(k[9]<<8); + case 9 : c+=k[8]; + case 8 : b+=(k[7]<<24); + case 7 : b+=(k[6]<<16); + case 6 : b+=(k[5]<<8); + case 5 : b+=k[4]; + case 4 : a+=(k[3]<<24); + case 3 : a+=(k[2]<<16); + case 2 : a+=(k[1]<<8); + case 1 : a+=k[0]; } mixc(a,b,c,d,e,f,g,h); mixc(a,b,c,d,e,f,g,h); diff --git a/libyasm/phash.h b/libyasm/phash.h index bf4cb47c..c4cc88a0 100644 --- a/libyasm/phash.h +++ b/libyasm/phash.h @@ -11,5 +11,5 @@ Source is http://burtleburtle.net/bob/c/lookupa.h */ unsigned long phash_lookup(const char *k, size_t length, - unsigned long level); + unsigned long level); void phash_checksum(const char *k, size_t length, unsigned long *state); diff --git a/libyasm/preproc.h b/libyasm/preproc.h index 96b13173..d7171421 100644 --- a/libyasm/preproc.h +++ b/libyasm/preproc.h @@ -56,17 +56,17 @@ typedef struct yasm_preproc_module { * Module-level implementation of yasm_preproc_create(). * Call yasm_preproc_create() instead of calling this function. * - * \param f initial starting file - * \param in_filename initial starting filename - * \param lm line mapping repository - * \param errwarns error/warnning set. + * \param f initial starting file + * \param in_filename initial starting filename + * \param lm line mapping repository + * \param errwarns error/warnning set. * \return New preprocessor. * * \note Any preprocessor errors and warnings are stored into errwarns. */ /*@only@*/ yasm_preproc * (*create) (FILE *f, const char *in_filename, - yasm_linemap *lm, - yasm_errwarns *errwarns); + yasm_linemap *lm, + yasm_errwarns *errwarns); /** Module-level implementation of yasm_preproc_destroy(). * Call yasm_preproc_destroy() instead of calling this function. @@ -77,7 +77,7 @@ typedef struct yasm_preproc_module { * Call yasm_preproc_input() instead of calling this function. */ size_t (*input) (yasm_preproc *preproc, /*@out@*/ char *buf, - size_t max_size); + size_t max_size); /** Module-level implementation of yasm_preproc_get_included_file(). * Call yasm_preproc_get_included_file() instead of calling this function. @@ -109,11 +109,11 @@ typedef struct yasm_preproc_module { /** Initialize preprocessor. * The preprocessor needs access to the object format module to find out * any output format specific macros. - * \param module preprocessor module - * \param f initial starting file - * \param in_filename initial starting file filename - * \param lm line mapping repository - * \param errwarns error/warning set + * \param module preprocessor module + * \param f initial starting file + * \param in_filename initial starting file filename + * \param lm line mapping repository + * \param errwarns error/warning set * \return New preprocessor. * \note Errors/warnings are stored into errwarns. */ @@ -122,55 +122,55 @@ typedef struct yasm_preproc_module { yasm_linemap *lm, yasm_errwarns *errwarns); /** Cleans up any allocated preproc memory. - * \param preproc preprocessor + * \param preproc preprocessor */ void yasm_preproc_destroy(/*@only@*/ yasm_preproc *preproc); /** Gets more preprocessed source code (up to max_size bytes) into buf. * More than a single line may be returned in buf. - * \param preproc preprocessor - * \param buf destination buffer for preprocessed source - * \param max_size maximum number of bytes that can be returned in buf + * \param preproc preprocessor + * \param buf destination buffer for preprocessed source + * \param max_size maximum number of bytes that can be returned in buf * \return Actual number of bytes returned in buf. */ size_t yasm_preproc_input(yasm_preproc *preproc, /*@out@*/ char *buf, - size_t max_size); + size_t max_size); /** Get the next filename included by the source code. - * \param preproc preprocessor - * \param buf destination buffer for filename - * \param max_size maximum number of bytes that can be returned in buf + * \param preproc preprocessor + * \param buf destination buffer for filename + * \param max_size maximum number of bytes that can be returned in buf * \return Actual number of bytes returned in buf. */ size_t yasm_preproc_get_included_file(yasm_preproc *preproc, - /*@out@*/ char *buf, size_t max_size); + /*@out@*/ char *buf, size_t max_size); /** Pre-include a file. - * \param preproc preprocessor - * \param filename filename + * \param preproc preprocessor + * \param filename filename */ void yasm_preproc_add_include_file(yasm_preproc *preproc, - const char *filename); + const char *filename); /** Pre-define a macro. - * \param preproc preprocessor - * \param macronameval "name=value" string + * \param preproc preprocessor + * \param macronameval "name=value" string */ void yasm_preproc_predefine_macro(yasm_preproc *preproc, - const char *macronameval); + const char *macronameval); /** Un-define a macro. - * \param preproc preprocessor - * \param macroname macro name + * \param preproc preprocessor + * \param macroname macro name */ void yasm_preproc_undefine_macro(yasm_preproc *preproc, const char *macroname); /** Define a builtin macro, preprocessed before the "standard" macros. - * \param preproc preprocessor - * \param macronameval "name=value" string + * \param preproc preprocessor + * \param macronameval "name=value" string */ void yasm_preproc_define_builtin(yasm_preproc *preproc, - const char *macronameval); + const char *macronameval); #ifndef YASM_DOXYGEN @@ -189,12 +189,12 @@ void yasm_preproc_define_builtin(yasm_preproc *preproc, ((yasm_preproc_base *)preproc)->module->add_include_file(preproc, filename) #define yasm_preproc_predefine_macro(preproc, macronameval) \ ((yasm_preproc_base *)preproc)->module->predefine_macro(preproc, \ - macronameval) + macronameval) #define yasm_preproc_undefine_macro(preproc, macroname) \ ((yasm_preproc_base *)preproc)->module->undefine_macro(preproc, macroname) #define yasm_preproc_define_builtin(preproc, macronameval) \ ((yasm_preproc_base *)preproc)->module->define_builtin(preproc, \ - macronameval) + macronameval) #endif diff --git a/libyasm/section.c b/libyasm/section.c index 02b6741d..0011491d 100644 --- a/libyasm/section.c +++ b/libyasm/section.c @@ -64,17 +64,17 @@ struct yasm_section { enum { SECTION_GENERAL, SECTION_ABSOLUTE } type; union { - /* SECTION_GENERAL data */ - struct { - /*@owned@*/ char *name; /* strdup()'ed name (given by user) */ - } general; - /* SECTION_ABSOLUTE data */ - struct { - /* Internally created first symrec in section. Used by - * yasm_expr__level_tree during absolute reference expansion. - */ - /*@dependent@*/ yasm_symrec *first; - } absolute; + /* SECTION_GENERAL data */ + struct { + /*@owned@*/ char *name; /* strdup()'ed name (given by user) */ + } general; + /* SECTION_ABSOLUTE data */ + struct { + /* Internally created first symrec in section. Used by + * yasm_expr__level_tree during absolute reference expansion. + */ + /*@dependent@*/ yasm_symrec *first; + } absolute; } data; /* associated data; NULL if none */ @@ -82,14 +82,14 @@ struct yasm_section { /*@owned@*/ yasm_expr *start; /* Starting address of section contents */ - unsigned long align; /* Section alignment */ + unsigned long align; /* Section alignment */ - unsigned long opt_flags; /* storage for optimizer flags */ + unsigned long opt_flags; /* storage for optimizer flags */ - int code; /* section contains code (instructions) */ - int res_only; /* allow only resb family of bytecodes? */ - int def; /* "default" section, e.g. not specified by - using section directive */ + int code; /* section contains code (instructions) */ + int res_only; /* allow only resb family of bytecodes? */ + int def; /* "default" section, e.g. not specified by + using section directive */ /* the bytecodes for the section's contents */ /*@reldef@*/ STAILQ_HEAD(yasm_bytecodehead, yasm_bytecode) bcs; @@ -113,37 +113,37 @@ typedef struct yasm_directive_wrap { static void dir_extern(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_symrec *sym; sym = yasm_symtab_declare(object->symtab, vp->val, YASM_SYM_EXTERN, line); if (objext_valparams) { - yasm_valparamhead *vps = yasm_vps_create(); - *vps = *objext_valparams; /* structure copy */ - yasm_vps_initialize(objext_valparams); /* don't double-free */ - yasm_symrec_set_objext_valparams(sym, vps); + yasm_valparamhead *vps = yasm_vps_create(); + *vps = *objext_valparams; /* structure copy */ + yasm_vps_initialize(objext_valparams); /* don't double-free */ + yasm_symrec_set_objext_valparams(sym, vps); } } static void dir_global(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_symrec *sym; sym = yasm_symtab_declare(object->symtab, vp->val, YASM_SYM_GLOBAL, line); if (objext_valparams) { - yasm_valparamhead *vps = yasm_vps_create(); - *vps = *objext_valparams; /* structure copy */ - yasm_vps_initialize(objext_valparams); /* don't double-free */ - yasm_symrec_set_objext_valparams(sym, vps); + yasm_valparamhead *vps = yasm_vps_create(); + *vps = *objext_valparams; /* structure copy */ + yasm_vps_initialize(objext_valparams); /* don't double-free */ + yasm_symrec_set_objext_valparams(sym, vps); } } static void dir_common(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_valparam *vp2 = yasm_vps_next(vp); @@ -151,42 +151,42 @@ dir_common(yasm_object *object, yasm_valparamhead *valparams, yasm_symrec *sym; if (!size) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("no size specified in %s declaration"), "COMMON"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("no size specified in %s declaration"), "COMMON"); + return; } sym = yasm_symtab_declare(object->symtab, vp->val, YASM_SYM_COMMON, line); yasm_symrec_set_common_size(sym, size); if (objext_valparams) { - yasm_valparamhead *vps = yasm_vps_create(); - *vps = *objext_valparams; /* structure copy */ - yasm_vps_initialize(objext_valparams); /* don't double-free */ - yasm_symrec_set_objext_valparams(sym, vps); + yasm_valparamhead *vps = yasm_vps_create(); + *vps = *objext_valparams; /* structure copy */ + yasm_vps_initialize(objext_valparams); /* don't double-free */ + yasm_symrec_set_objext_valparams(sym, vps); } } static void dir_section(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_section *new_section = - yasm_objfmt_section_switch(object, valparams, objext_valparams, line); + yasm_objfmt_section_switch(object, valparams, objext_valparams, line); if (new_section) - object->cur_section = new_section; + object->cur_section = new_section; else - yasm_error_set(YASM_ERROR_SYNTAX, - N_("invalid argument to directive `%s'"), "SECTION"); + yasm_error_set(YASM_ERROR_SYNTAX, + N_("invalid argument to directive `%s'"), "SECTION"); } static const yasm_directive object_directives[] = { - { ".extern", "gas", dir_extern, YASM_DIR_ID_REQUIRED }, - { ".global", "gas", dir_global, YASM_DIR_ID_REQUIRED }, - { ".globl", "gas", dir_global, YASM_DIR_ID_REQUIRED }, - { "extern", "nasm", dir_extern, YASM_DIR_ID_REQUIRED }, - { "global", "nasm", dir_global, YASM_DIR_ID_REQUIRED }, - { "common", "nasm", dir_common, YASM_DIR_ID_REQUIRED }, - { "section", "nasm", dir_section, YASM_DIR_ARG_REQUIRED }, - { "segment", "nasm", dir_section, YASM_DIR_ARG_REQUIRED }, + { ".extern", "gas", dir_extern, YASM_DIR_ID_REQUIRED }, + { ".global", "gas", dir_global, YASM_DIR_ID_REQUIRED }, + { ".globl", "gas", dir_global, YASM_DIR_ID_REQUIRED }, + { "extern", "nasm", dir_extern, YASM_DIR_ID_REQUIRED }, + { "global", "nasm", dir_global, YASM_DIR_ID_REQUIRED }, + { "common", "nasm", dir_common, YASM_DIR_ID_REQUIRED }, + { "section", "nasm", dir_section, YASM_DIR_ARG_REQUIRED }, + { "segment", "nasm", dir_section, YASM_DIR_ARG_REQUIRED }, { NULL, NULL, NULL, 0 } }; @@ -206,33 +206,33 @@ static void directives_add(yasm_object *object, /*@null@*/ const yasm_directive *dir) { if (!dir) - return; + return; while (dir->name) { - HAMT *level2 = HAMT_search(object->directives, dir->parser); - int replace; - yasm_directive_wrap *wrap = yasm_xmalloc(sizeof(yasm_directive_wrap)); - - if (!level2) { - replace = 0; - level2 = HAMT_insert(object->directives, dir->parser, - HAMT_create(1, yasm_internal_error_), - &replace, directive_level1_delete); - } - replace = 0; - wrap->directive = dir; - HAMT_insert(level2, dir->name, wrap, &replace, - directive_level2_delete); - dir++; + HAMT *level2 = HAMT_search(object->directives, dir->parser); + int replace; + yasm_directive_wrap *wrap = yasm_xmalloc(sizeof(yasm_directive_wrap)); + + if (!level2) { + replace = 0; + level2 = HAMT_insert(object->directives, dir->parser, + HAMT_create(1, yasm_internal_error_), + &replace, directive_level1_delete); + } + replace = 0; + wrap->directive = dir; + HAMT_insert(level2, dir->name, wrap, &replace, + directive_level2_delete); + dir++; } } /*@-compdestroy@*/ yasm_object * yasm_object_create(const char *src_filename, const char *obj_filename, - /*@kept@*/ yasm_arch *arch, - const yasm_objfmt_module *objfmt_module, - const yasm_dbgfmt_module *dbgfmt_module) + /*@kept@*/ yasm_arch *arch, + const yasm_objfmt_module *objfmt_module, + const yasm_dbgfmt_module *dbgfmt_module) { yasm_object *object = yasm_xmalloc(sizeof(yasm_object)); int matched, i; @@ -258,11 +258,11 @@ yasm_object_create(const char *src_filename, const char *obj_filename, /* Initialize the object format */ object->objfmt = yasm_objfmt_create(objfmt_module, object); if (!object->objfmt) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("object format `%s' does not support architecture `%s' machine `%s'"), - objfmt_module->keyword, ((yasm_arch_base *)arch)->module->keyword, - yasm_arch_get_machine(arch)); - goto error; + yasm_error_set(YASM_ERROR_GENERAL, + N_("object format `%s' does not support architecture `%s' machine `%s'"), + objfmt_module->keyword, ((yasm_arch_base *)arch)->module->keyword, + yasm_arch_get_machine(arch)); + goto error; } /* Get a fresh copy of objfmt_module as it may have changed. */ @@ -276,32 +276,32 @@ yasm_object_create(const char *src_filename, const char *obj_filename, */ matched = 0; for (i=0; objfmt_module->dbgfmt_keywords[i]; i++) - if (yasm__strcasecmp(objfmt_module->dbgfmt_keywords[i], - dbgfmt_module->keyword) == 0) - matched = 1; + if (yasm__strcasecmp(objfmt_module->dbgfmt_keywords[i], + dbgfmt_module->keyword) == 0) + matched = 1; if (!matched) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("`%s' is not a valid debug format for object format `%s'"), - dbgfmt_module->keyword, objfmt_module->keyword); - goto error; + yasm_error_set(YASM_ERROR_GENERAL, + N_("`%s' is not a valid debug format for object format `%s'"), + dbgfmt_module->keyword, objfmt_module->keyword); + goto error; } /* Initialize the debug format */ object->dbgfmt = yasm_dbgfmt_create(dbgfmt_module, object); if (!object->dbgfmt) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("debug format `%s' does not work with object format `%s'"), - dbgfmt_module->keyword, objfmt_module->keyword); - goto error; + yasm_error_set(YASM_ERROR_GENERAL, + N_("debug format `%s' does not work with object format `%s'"), + dbgfmt_module->keyword, objfmt_module->keyword); + goto error; } /* Add directives to HAMT. Note ordering here determines priority. */ directives_add(object, - ((yasm_objfmt_base *)object->objfmt)->module->directives); + ((yasm_objfmt_base *)object->objfmt)->module->directives); directives_add(object, - ((yasm_dbgfmt_base *)object->dbgfmt)->module->directives); + ((yasm_dbgfmt_base *)object->dbgfmt)->module->directives); directives_add(object, - ((yasm_arch_base *)object->arch)->module->directives); + ((yasm_arch_base *)object->arch)->module->directives); directives_add(object, object_directives); return object; @@ -315,8 +315,8 @@ error: /*@-onlytrans@*/ yasm_section * yasm_object_get_general(yasm_object *object, const char *name, - yasm_expr *start, unsigned long align, int code, - int res_only, int *isnew, unsigned long line) + yasm_expr *start, unsigned long align, int code, + int res_only, int *isnew, unsigned long line) { yasm_section *s; yasm_bytecode *bc; @@ -325,11 +325,11 @@ yasm_object_get_general(yasm_object *object, const char *name, * that name. */ STAILQ_FOREACH(s, &object->sections, link) { - if (s->type == SECTION_GENERAL && - strcmp(s->data.general.name, name) == 0) { - *isnew = 0; - return s; - } + if (s->type == SECTION_GENERAL && + strcmp(s->data.general.name, name) == 0) { + *isnew = 0; + return s; + } } /* No: we have to allocate and create a new one. */ @@ -343,11 +343,11 @@ yasm_object_get_general(yasm_object *object, const char *name, s->data.general.name = yasm__xstrdup(name); s->assoc_data = NULL; if (start) - s->start = start; + s->start = start; else - s->start = - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), - line); + s->start = + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), + line); s->align = align; /* Initialize bytecodes with one empty bytecode (acts as "prior" for first @@ -375,7 +375,7 @@ yasm_object_get_general(yasm_object *object, const char *name, /*@-onlytrans@*/ yasm_section * yasm_object_create_absolute(yasm_object *object, yasm_expr *start, - unsigned long line) + unsigned long line) { yasm_section *s; yasm_bytecode *bc; @@ -401,7 +401,7 @@ yasm_object_create_absolute(yasm_object *object, yasm_expr *start, s->destroy_reloc = NULL; s->data.absolute.first = - yasm_symtab_define_label(object->symtab, ".absstart", bc, 0, 0); + yasm_symtab_define_label(object->symtab, ".absstart", bc, 0, 0); s->code = 0; s->res_only = 1; @@ -413,23 +413,23 @@ yasm_object_create_absolute(yasm_object *object, yasm_expr *start, int yasm_object_directive(yasm_object *object, const char *name, - const char *parser, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, - unsigned long line) + const char *parser, yasm_valparamhead *valparams, + yasm_valparamhead *objext_valparams, + unsigned long line) { HAMT *level2; yasm_directive_wrap *wrap; level2 = HAMT_search(object->directives, parser); if (!level2) - return 1; + return 1; wrap = HAMT_search(level2, name); if (!wrap) - return 1; + return 1; yasm_call_directive(wrap->directive, object, valparams, objext_valparams, - line); + line); return 0; } @@ -484,14 +484,14 @@ yasm_section_get_object(const yasm_section *sect) void * yasm_section_get_data(yasm_section *sect, - const yasm_assoc_data_callback *callback) + const yasm_assoc_data_callback *callback) { return yasm__assoc_data_get(sect->assoc_data, callback); } void yasm_section_add_data(yasm_section *sect, - const yasm_assoc_data_callback *callback, void *data) + const yasm_assoc_data_callback *callback, void *data) { sect->assoc_data = yasm__assoc_data_add(sect->assoc_data, callback, data); } @@ -505,16 +505,16 @@ yasm_object_destroy(yasm_object *object) * due to an error in yasm_object_create(), so look out for NULLs. */ if (object->objfmt) - yasm_objfmt_destroy(object->objfmt); + yasm_objfmt_destroy(object->objfmt); if (object->dbgfmt) - yasm_dbgfmt_destroy(object->dbgfmt); + yasm_dbgfmt_destroy(object->dbgfmt); /* Delete sections */ cur = STAILQ_FIRST(&object->sections); while (cur) { - next = STAILQ_NEXT(cur, link); - yasm_section_destroy(cur); - cur = next; + next = STAILQ_NEXT(cur, link); + yasm_section_destroy(cur); + cur = next; } /* Delete directives HAMT */ @@ -529,7 +529,7 @@ yasm_object_destroy(yasm_object *object) /* Delete architecture */ if (object->arch) - yasm_arch_destroy(object->arch); + yasm_arch_destroy(object->arch); yasm_xfree(object); } @@ -545,8 +545,8 @@ yasm_object_print(const yasm_object *object, FILE *f, int indent_level) /* Print sections and bytecodes */ STAILQ_FOREACH(cur, &object->sections, link) { - fprintf(f, "%*sSection:\n", indent_level, ""); - yasm_section_print(cur, f, indent_level+1, 1); + fprintf(f, "%*sSection:\n", indent_level, ""); + yasm_section_print(cur, f, indent_level+1, 1); } } @@ -557,35 +557,35 @@ yasm_object_finalize(yasm_object *object, yasm_errwarns *errwarns) /* Iterate through sections */ STAILQ_FOREACH(sect, &object->sections, link) { - yasm_bytecode *cur = STAILQ_FIRST(§->bcs); - yasm_bytecode *prev; - - /* Skip our locally created empty bytecode first. */ - prev = cur; - cur = STAILQ_NEXT(cur, link); - - /* Iterate through the remainder, if any. */ - while (cur) { - /* Finalize */ - yasm_bc_finalize(cur, prev); - yasm_errwarn_propagate(errwarns, cur->line); - prev = cur; - cur = STAILQ_NEXT(cur, link); - } + yasm_bytecode *cur = STAILQ_FIRST(§->bcs); + yasm_bytecode *prev; + + /* Skip our locally created empty bytecode first. */ + prev = cur; + cur = STAILQ_NEXT(cur, link); + + /* Iterate through the remainder, if any. */ + while (cur) { + /* Finalize */ + yasm_bc_finalize(cur, prev); + yasm_errwarn_propagate(errwarns, cur->line); + prev = cur; + cur = STAILQ_NEXT(cur, link); + } } } int yasm_object_sections_traverse(yasm_object *object, /*@null@*/ void *d, - int (*func) (yasm_section *sect, - /*@null@*/ void *d)) + int (*func) (yasm_section *sect, + /*@null@*/ void *d)) { yasm_section *cur; STAILQ_FOREACH(cur, &object->sections, link) { - int retval = func(cur, d); - if (retval != 0) - return retval; + int retval = func(cur, d); + if (retval != 0) + return retval; } return 0; } @@ -597,9 +597,9 @@ yasm_object_find_general(yasm_object *object, const char *name) yasm_section *cur; STAILQ_FOREACH(cur, &object->sections, link) { - if (cur->type == SECTION_GENERAL && - strcmp(cur->data.general.name, name) == 0) - return cur; + if (cur->type == SECTION_GENERAL && + strcmp(cur->data.general.name, name) == 0) + return cur; } return NULL; } @@ -607,13 +607,13 @@ yasm_object_find_general(yasm_object *object, const char *name) void yasm_section_add_reloc(yasm_section *sect, yasm_reloc *reloc, - void (*destroy_func) (/*@only@*/ void *reloc)) + void (*destroy_func) (/*@only@*/ void *reloc)) { STAILQ_INSERT_TAIL(§->relocs, reloc, link); if (!destroy_func) - yasm_internal_error(N_("NULL destroy function given to add_reloc")); + yasm_internal_error(N_("NULL destroy function given to add_reloc")); else if (sect->destroy_reloc && destroy_func != sect->destroy_reloc) - yasm_internal_error(N_("different destroy function given to add_reloc")); + yasm_internal_error(N_("different destroy function given to add_reloc")); sect->destroy_reloc = destroy_func; } @@ -654,21 +654,21 @@ yasm_bytecode * yasm_section_bcs_append(yasm_section *sect, yasm_bytecode *bc) { if (bc) { - if (bc->callback) { - bc->section = sect; /* record parent section */ - STAILQ_INSERT_TAIL(§->bcs, bc, link); - return bc; - } else - yasm_xfree(bc); + if (bc->callback) { + bc->section = sect; /* record parent section */ + STAILQ_INSERT_TAIL(§->bcs, bc, link); + return bc; + } else + yasm_xfree(bc); } return (yasm_bytecode *)NULL; } int yasm_section_bcs_traverse(yasm_section *sect, - /*@null@*/ yasm_errwarns *errwarns, - /*@null@*/ void *d, - int (*func) (yasm_bytecode *bc, /*@null@*/ void *d)) + /*@null@*/ yasm_errwarns *errwarns, + /*@null@*/ void *d, + int (*func) (yasm_bytecode *bc, /*@null@*/ void *d)) { yasm_bytecode *cur = STAILQ_FIRST(§->bcs); @@ -677,12 +677,12 @@ yasm_section_bcs_traverse(yasm_section *sect, /* Iterate through the remainder, if any. */ while (cur) { - int retval = func(cur, d); - if (errwarns) - yasm_errwarn_propagate(errwarns, cur->line); - if (retval != 0) - return retval; - cur = STAILQ_NEXT(cur, link); + int retval = func(cur, d); + if (errwarns) + yasm_errwarn_propagate(errwarns, cur->line); + if (retval != 0) + return retval; + cur = STAILQ_NEXT(cur, link); } return 0; } @@ -691,7 +691,7 @@ const char * yasm_section_get_name(const yasm_section *sect) { if (sect->type == SECTION_GENERAL) - return sect->data.general.name; + return sect->data.general.name; return NULL; } @@ -699,13 +699,13 @@ yasm_symrec * yasm_section_abs_get_sym(const yasm_section *sect) { if (sect->type == SECTION_ABSOLUTE) - return sect->data.absolute.first; + return sect->data.absolute.first; return NULL; } void yasm_section_set_start(yasm_section *sect, yasm_expr *start, - unsigned long line) + unsigned long line) { yasm_expr_destroy(sect->start); sect->start = start; @@ -719,7 +719,7 @@ yasm_section_get_start(const yasm_section *sect) void yasm_section_set_align(yasm_section *sect, unsigned long align, - unsigned long line) + unsigned long line) { sect->align = align; } @@ -737,10 +737,10 @@ yasm_section_destroy(yasm_section *sect) yasm_reloc *r_cur, *r_next; if (!sect) - return; + return; if (sect->type == SECTION_GENERAL) { - yasm_xfree(sect->data.general.name); + yasm_xfree(sect->data.general.name); } yasm__assoc_data_destroy(sect->assoc_data); yasm_expr_destroy(sect->start); @@ -748,18 +748,18 @@ yasm_section_destroy(yasm_section *sect) /* Delete bytecodes */ cur = STAILQ_FIRST(§->bcs); while (cur) { - next = STAILQ_NEXT(cur, link); - yasm_bc_destroy(cur); - cur = next; + next = STAILQ_NEXT(cur, link); + yasm_bc_destroy(cur); + cur = next; } /* Delete relocations */ r_cur = STAILQ_FIRST(§->relocs); while (r_cur) { - r_next = STAILQ_NEXT(r_cur, link); - yasm_intnum_destroy(r_cur->addr); - sect->destroy_reloc(r_cur); - r_cur = r_next; + r_next = STAILQ_NEXT(r_cur, link); + yasm_intnum_destroy(r_cur->addr); + sect->destroy_reloc(r_cur); + r_cur = r_next; } yasm_xfree(sect); @@ -767,22 +767,22 @@ yasm_section_destroy(yasm_section *sect) void yasm_section_print(const yasm_section *sect, FILE *f, int indent_level, - int print_bcs) + int print_bcs) { if (!sect) { - fprintf(f, "%*s(none)\n", indent_level, ""); - return; + fprintf(f, "%*s(none)\n", indent_level, ""); + return; } fprintf(f, "%*stype=", indent_level, ""); switch (sect->type) { - case SECTION_GENERAL: - fprintf(f, "general\n%*sname=%s\n", indent_level, "", - sect->data.general.name); - break; - case SECTION_ABSOLUTE: - fprintf(f, "absolute\n"); - break; + case SECTION_GENERAL: + fprintf(f, "general\n%*sname=%s\n", indent_level, "", + sect->data.general.name); + break; + case SECTION_ABSOLUTE: + fprintf(f, "absolute\n"); + break; } fprintf(f, "%*sstart=", indent_level, ""); @@ -790,19 +790,19 @@ yasm_section_print(const yasm_section *sect, FILE *f, int indent_level, fprintf(f, "\n"); if (sect->assoc_data) { - fprintf(f, "%*sAssociated data:\n", indent_level, ""); - yasm__assoc_data_print(sect->assoc_data, f, indent_level+1); + fprintf(f, "%*sAssociated data:\n", indent_level, ""); + yasm__assoc_data_print(sect->assoc_data, f, indent_level+1); } if (print_bcs) { - yasm_bytecode *cur; + yasm_bytecode *cur; - fprintf(f, "%*sBytecodes:\n", indent_level, ""); + fprintf(f, "%*sBytecodes:\n", indent_level, ""); - STAILQ_FOREACH(cur, §->bcs, link) { - fprintf(f, "%*sNext Bytecode:\n", indent_level+1, ""); - yasm_bc_print(cur, f, indent_level+2); - } + STAILQ_FOREACH(cur, §->bcs, link) { + fprintf(f, "%*sNext Bytecode:\n", indent_level+1, ""); + yasm_bc_print(cur, f, indent_level+2); + } } } @@ -864,10 +864,10 @@ yasm_section_print(const yasm_section *sect, FILE *f, int indent_level, * How times is handled: * * TIMES: Handled separately from bytecode "raw" size. If not span-dependent, - * trivial (just multiplied in at any bytecode size increase). Span - * dependent times update on any change (span ID 0). If the resultant - * next bytecode offset would be less than the old next bytecode offset, - * error. Otherwise increase offset and update dependent spans. + * trivial (just multiplied in at any bytecode size increase). Span + * dependent times update on any change (span ID 0). If the resultant + * next bytecode offset would be less than the old next bytecode offset, + * error. Otherwise increase offset and update dependent spans. * * To reduce interval tree size, a first expansion pass is performed * before the spans are added to the tree. @@ -919,14 +919,14 @@ typedef struct yasm_offset_setter { typedef struct yasm_span_term { yasm_bytecode *precbc, *precbc2; - yasm_span *span; /* span this term is a member of */ + yasm_span *span; /* span this term is a member of */ long cur_val, new_val; unsigned int subst; } yasm_span_term; struct yasm_span { - /*@reldef@*/ TAILQ_ENTRY(yasm_span) link; /* for allocation tracking */ - /*@reldef@*/ STAILQ_ENTRY(yasm_span) linkq; /* for Q */ + /*@reldef@*/ TAILQ_ENTRY(yasm_span) link; /* for allocation tracking */ + /*@reldef@*/ STAILQ_ENTRY(yasm_span) linkq; /* for Q */ /*@dependent@*/ yasm_bytecode *bc; @@ -963,22 +963,22 @@ typedef struct optimize_data { /*@reldef@*/ STAILQ_HEAD(, yasm_span) QA, QB; /*@only@*/ IntervalTree *itree; /*@reldef@*/ STAILQ_HEAD(, yasm_offset_setter) offset_setters; - long len_diff; /* used only for optimize_term_expand */ - yasm_span *span; /* used only for check_cycle */ + long len_diff; /* used only for optimize_term_expand */ + yasm_span *span; /* used only for check_cycle */ yasm_offset_setter *os; } optimize_data; static yasm_span * create_span(yasm_bytecode *bc, int id, /*@null@*/ const yasm_value *value, - long neg_thres, long pos_thres, yasm_offset_setter *os) + long neg_thres, long pos_thres, yasm_offset_setter *os) { yasm_span *span = yasm_xmalloc(sizeof(yasm_span)); span->bc = bc; if (value) - yasm_value_init_copy(&span->depval, value); + yasm_value_init_copy(&span->depval, value); else - yasm_value_initialize(&span->depval, NULL, 0); + yasm_value_initialize(&span->depval, NULL, 0); span->rel_term = NULL; span->terms = NULL; span->items = NULL; @@ -997,7 +997,7 @@ create_span(yasm_bytecode *bc, int id, /*@null@*/ const yasm_value *value, static void optimize_add_span(void *add_span_data, yasm_bytecode *bc, int id, - const yasm_value *value, long neg_thres, long pos_thres) + const yasm_value *value, long neg_thres, long pos_thres) { optimize_data *optd = (optimize_data *)add_span_data; yasm_span *span; @@ -1007,16 +1007,16 @@ optimize_add_span(void *add_span_data, yasm_bytecode *bc, int id, static void add_span_term(unsigned int subst, yasm_bytecode *precbc, - yasm_bytecode *precbc2, void *d) + yasm_bytecode *precbc2, void *d) { yasm_span *span = d; yasm_intnum *intn; if (subst >= span->num_terms) { - /* Linear expansion since total number is essentially always small */ - span->num_terms = subst+1; - span->terms = yasm_xrealloc(span->terms, - span->num_terms*sizeof(yasm_span_term)); + /* Linear expansion since total number is essentially always small */ + span->num_terms = subst+1; + span->terms = yasm_xrealloc(span->terms, + span->num_terms*sizeof(yasm_span_term)); } span->terms[subst].precbc = precbc; span->terms[subst].precbc2 = precbc2; @@ -1025,7 +1025,7 @@ add_span_term(unsigned int subst, yasm_bytecode *precbc, intn = yasm_calc_bc_dist(precbc, precbc2); if (!intn) - yasm_internal_error(N_("could not calculate bc distance")); + yasm_internal_error(N_("could not calculate bc distance")); span->terms[subst].cur_val = 0; span->terms[subst].new_val = yasm_intnum_get_int(intn); yasm_intnum_destroy(intn); @@ -1038,50 +1038,50 @@ span_create_terms(yasm_span *span) /* Split out sym-sym terms in absolute portion of dependent value */ if (span->depval.abs) { - span->num_terms = yasm_expr__bc_dist_subst(&span->depval.abs, span, - add_span_term); - if (span->num_terms > 0) { - span->items = yasm_xmalloc(span->num_terms*sizeof(yasm_expr__item)); - for (i=0; inum_terms; i++) { - /* Create items with dummy value */ - span->items[i].type = YASM_EXPR_INT; - span->items[i].data.intn = yasm_intnum_create_int(0); - - /* Check for circular references */ - if (span->id <= 0 && - ((span->bc->bc_index > span->terms[i].precbc->bc_index && - span->bc->bc_index <= span->terms[i].precbc2->bc_index) || - (span->bc->bc_index > span->terms[i].precbc2->bc_index && - span->bc->bc_index <= span->terms[i].precbc->bc_index))) - yasm_error_set(YASM_ERROR_VALUE, - N_("circular reference detected")); - } - } + span->num_terms = yasm_expr__bc_dist_subst(&span->depval.abs, span, + add_span_term); + if (span->num_terms > 0) { + span->items = yasm_xmalloc(span->num_terms*sizeof(yasm_expr__item)); + for (i=0; inum_terms; i++) { + /* Create items with dummy value */ + span->items[i].type = YASM_EXPR_INT; + span->items[i].data.intn = yasm_intnum_create_int(0); + + /* Check for circular references */ + if (span->id <= 0 && + ((span->bc->bc_index > span->terms[i].precbc->bc_index && + span->bc->bc_index <= span->terms[i].precbc2->bc_index) || + (span->bc->bc_index > span->terms[i].precbc2->bc_index && + span->bc->bc_index <= span->terms[i].precbc->bc_index))) + yasm_error_set(YASM_ERROR_VALUE, + N_("circular reference detected")); + } + } } /* Create term for relative portion of dependent value */ if (span->depval.rel) { - yasm_bytecode *rel_precbc; - int sym_local; - - sym_local = yasm_symrec_get_label(span->depval.rel, &rel_precbc); - if (span->depval.wrt || span->depval.seg_of || span->depval.section_rel - || !sym_local) - return; /* we can't handle SEG, WRT, or external symbols */ - if (rel_precbc->section != span->bc->section) - return; /* not in this section */ - if (!span->depval.curpos_rel) - return; /* not PC-relative */ - - span->rel_term = yasm_xmalloc(sizeof(yasm_span_term)); - span->rel_term->precbc = NULL; - span->rel_term->precbc2 = rel_precbc; - span->rel_term->span = span; - span->rel_term->subst = ~0U; - - span->rel_term->cur_val = 0; - span->rel_term->new_val = yasm_bc_next_offset(rel_precbc) - - span->bc->offset; + yasm_bytecode *rel_precbc; + int sym_local; + + sym_local = yasm_symrec_get_label(span->depval.rel, &rel_precbc); + if (span->depval.wrt || span->depval.seg_of || span->depval.section_rel + || !sym_local) + return; /* we can't handle SEG, WRT, or external symbols */ + if (rel_precbc->section != span->bc->section) + return; /* not in this section */ + if (!span->depval.curpos_rel) + return; /* not PC-relative */ + + span->rel_term = yasm_xmalloc(sizeof(yasm_span_term)); + span->rel_term->precbc = NULL; + span->rel_term->precbc2 = rel_precbc; + span->rel_term->span = span; + span->rel_term->subst = ~0U; + + span->rel_term->cur_val = 0; + span->rel_term->new_val = yasm_bc_next_offset(rel_precbc) - + span->bc->offset; } } @@ -1094,40 +1094,40 @@ recalc_normal_span(yasm_span *span) span->new_val = 0; if (span->depval.abs) { - yasm_expr *abs_copy = yasm_expr_copy(span->depval.abs); - /*@null@*/ /*@dependent@*/ yasm_intnum *num; - - /* Update sym-sym terms and substitute back into expr */ - unsigned int i; - for (i=0; inum_terms; i++) - yasm_intnum_set_int(span->items[i].data.intn, - span->terms[i].new_val); - yasm_expr__subst(abs_copy, span->num_terms, span->items); - num = yasm_expr_get_intnum(&abs_copy, 0); - if (num) - span->new_val = yasm_intnum_get_int(num); - else - span->new_val = LONG_MAX; /* too complex; force to longest form */ - yasm_expr_destroy(abs_copy); + yasm_expr *abs_copy = yasm_expr_copy(span->depval.abs); + /*@null@*/ /*@dependent@*/ yasm_intnum *num; + + /* Update sym-sym terms and substitute back into expr */ + unsigned int i; + for (i=0; inum_terms; i++) + yasm_intnum_set_int(span->items[i].data.intn, + span->terms[i].new_val); + yasm_expr__subst(abs_copy, span->num_terms, span->items); + num = yasm_expr_get_intnum(&abs_copy, 0); + if (num) + span->new_val = yasm_intnum_get_int(num); + else + span->new_val = LONG_MAX; /* too complex; force to longest form */ + yasm_expr_destroy(abs_copy); } if (span->rel_term) { - if (span->new_val != LONG_MAX && span->rel_term->new_val != LONG_MAX) - span->new_val += span->rel_term->new_val >> span->depval.rshift; - else - span->new_val = LONG_MAX; /* too complex; force to longest form */ + if (span->new_val != LONG_MAX && span->rel_term->new_val != LONG_MAX) + span->new_val += span->rel_term->new_val >> span->depval.rshift; + else + span->new_val = LONG_MAX; /* too complex; force to longest form */ } else if (span->depval.rel) - span->new_val = LONG_MAX; /* too complex; force to longest form */ + span->new_val = LONG_MAX; /* too complex; force to longest form */ if (span->new_val == LONG_MAX) - span->active = 0; + span->active = 0; /* If id<=0, flag update on any change */ if (span->id <= 0) - return (span->new_val != span->cur_val); + return (span->new_val != span->cur_val); return (span->new_val < span->neg_thres - || span->new_val > span->pos_thres); + || span->new_val > span->pos_thres); } /* Updates all bytecode offsets. For offset-based bytecodes, calls expand @@ -1140,33 +1140,33 @@ update_all_bc_offsets(yasm_object *object, yasm_errwarns *errwarns) int saw_error = 0; STAILQ_FOREACH(sect, &object->sections, link) { - unsigned long offset = 0; - - yasm_bytecode *bc = STAILQ_FIRST(§->bcs); - yasm_bytecode *prevbc; - - /* Skip our locally created empty bytecode first. */ - prevbc = bc; - bc = STAILQ_NEXT(bc, link); - - /* Iterate through the remainder, if any. */ - while (bc) { - if (bc->callback->special == YASM_BC_SPECIAL_OFFSET) { - /* Recalculate/adjust len of offset-based bytecodes here */ - long neg_thres = 0; - long pos_thres = (long)yasm_bc_next_offset(bc); - int retval = yasm_bc_expand(bc, 1, 0, - (long)yasm_bc_next_offset(prevbc), - &neg_thres, &pos_thres); - yasm_errwarn_propagate(errwarns, bc->line); - if (retval < 0) - saw_error = 1; - } - bc->offset = offset; - offset += bc->len*bc->mult_int; - prevbc = bc; - bc = STAILQ_NEXT(bc, link); - } + unsigned long offset = 0; + + yasm_bytecode *bc = STAILQ_FIRST(§->bcs); + yasm_bytecode *prevbc; + + /* Skip our locally created empty bytecode first. */ + prevbc = bc; + bc = STAILQ_NEXT(bc, link); + + /* Iterate through the remainder, if any. */ + while (bc) { + if (bc->callback->special == YASM_BC_SPECIAL_OFFSET) { + /* Recalculate/adjust len of offset-based bytecodes here */ + long neg_thres = 0; + long pos_thres = (long)yasm_bc_next_offset(bc); + int retval = yasm_bc_expand(bc, 1, 0, + (long)yasm_bc_next_offset(prevbc), + &neg_thres, &pos_thres); + yasm_errwarn_propagate(errwarns, bc->line); + if (retval < 0) + saw_error = 1; + } + bc->offset = offset; + offset += bc->len*bc->mult_int; + prevbc = bc; + bc = STAILQ_NEXT(bc, link); + } } return saw_error; } @@ -1178,16 +1178,16 @@ span_destroy(/*@only@*/ yasm_span *span) yasm_value_delete(&span->depval); if (span->rel_term) - yasm_xfree(span->rel_term); + yasm_xfree(span->rel_term); if (span->terms) - yasm_xfree(span->terms); + yasm_xfree(span->terms); if (span->items) { - for (i=0; inum_terms; i++) - yasm_intnum_destroy(span->items[i].data.intn); - yasm_xfree(span->items); + for (i=0; inum_terms; i++) + yasm_intnum_destroy(span->items[i].data.intn); + yasm_xfree(span->items); } if (span->backtrace) - yasm_xfree(span->backtrace); + yasm_xfree(span->backtrace); yasm_xfree(span); } @@ -1201,16 +1201,16 @@ optimize_cleanup(optimize_data *optd) s1 = TAILQ_FIRST(&optd->spans); while (s1) { - s2 = TAILQ_NEXT(s1, link); - span_destroy(s1); - s1 = s2; + s2 = TAILQ_NEXT(s1, link); + span_destroy(s1); + s1 = s2; } os1 = STAILQ_FIRST(&optd->offset_setters); while (os1) { - os2 = STAILQ_NEXT(os1, link); - yasm_xfree(os1); - os1 = os2; + os2 = STAILQ_NEXT(os1, link); + yasm_xfree(os1); + os1 = os2; } } @@ -1222,23 +1222,23 @@ optimize_itree_add(IntervalTree *itree, yasm_span *span, yasm_span_term *term) /* Update term length */ if (term->precbc) - precbc_index = term->precbc->bc_index; + precbc_index = term->precbc->bc_index; else - precbc_index = span->bc->bc_index-1; + precbc_index = span->bc->bc_index-1; if (term->precbc2) - precbc2_index = term->precbc2->bc_index; + precbc2_index = term->precbc2->bc_index; else - precbc2_index = span->bc->bc_index-1; + precbc2_index = span->bc->bc_index-1; if (precbc_index < precbc2_index) { - low = precbc_index+1; - high = precbc2_index; + low = precbc_index+1; + high = precbc2_index; } else if (precbc_index > precbc2_index) { - low = precbc2_index+1; - high = precbc_index; + low = precbc2_index+1; + high = precbc_index; } else - return; /* difference is same bc - always 0! */ + return; /* difference is same bc - always 0! */ IT_insert(itree, (long)low, (long)high, term); } @@ -1253,42 +1253,42 @@ check_cycle(IntervalTreeNode *node, void *d) /* Only check for cycles in id=0 spans */ if (depspan->id > 0) - return; + return; /* Check for a circular reference by looking to see if this dependent * span is in our backtrace. */ if (optd->span->backtrace) { - yasm_span *s; - while ((s = optd->span->backtrace[bt_size])) { - bt_size++; - if (s == depspan) - yasm_error_set(YASM_ERROR_VALUE, - N_("circular reference detected")); - } + yasm_span *s; + while ((s = optd->span->backtrace[bt_size])) { + bt_size++; + if (s == depspan) + yasm_error_set(YASM_ERROR_VALUE, + N_("circular reference detected")); + } } /* Add our complete backtrace and ourselves to backtrace of dependent * span. */ if (!depspan->backtrace) { - depspan->backtrace = yasm_xmalloc((bt_size+2)*sizeof(yasm_span *)); - if (bt_size > 0) - memcpy(depspan->backtrace, optd->span->backtrace, - bt_size*sizeof(yasm_span *)); - depspan->backtrace[bt_size] = optd->span; - depspan->backtrace[bt_size+1] = NULL; - return; + depspan->backtrace = yasm_xmalloc((bt_size+2)*sizeof(yasm_span *)); + if (bt_size > 0) + memcpy(depspan->backtrace, optd->span->backtrace, + bt_size*sizeof(yasm_span *)); + depspan->backtrace[bt_size] = optd->span; + depspan->backtrace[bt_size+1] = NULL; + return; } while (depspan->backtrace[dep_bt_size]) - dep_bt_size++; + dep_bt_size++; depspan->backtrace = - yasm_xrealloc(depspan->backtrace, - (dep_bt_size+bt_size+2)*sizeof(yasm_span *)); + yasm_xrealloc(depspan->backtrace, + (dep_bt_size+bt_size+2)*sizeof(yasm_span *)); if (bt_size > 0) - memcpy(&depspan->backtrace[dep_bt_size], optd->span->backtrace, - (bt_size-1)*sizeof(yasm_span *)); + memcpy(&depspan->backtrace[dep_bt_size], optd->span->backtrace, + (bt_size-1)*sizeof(yasm_span *)); depspan->backtrace[dep_bt_size+bt_size] = optd->span; depspan->backtrace[dep_bt_size+bt_size+1] = NULL; } @@ -1304,38 +1304,38 @@ optimize_term_expand(IntervalTreeNode *node, void *d) /* Don't expand inactive spans */ if (!span->active) - return; + return; /* Update term length */ if (term->precbc) - precbc_index = term->precbc->bc_index; + precbc_index = term->precbc->bc_index; else - precbc_index = span->bc->bc_index-1; + precbc_index = span->bc->bc_index-1; if (term->precbc2) - precbc2_index = term->precbc2->bc_index; + precbc2_index = term->precbc2->bc_index; else - precbc2_index = span->bc->bc_index-1; + precbc2_index = span->bc->bc_index-1; if (precbc_index < precbc2_index) - term->new_val += len_diff; + term->new_val += len_diff; else - term->new_val -= len_diff; + term->new_val -= len_diff; /* If already on Q, don't re-add */ if (span->active == 2) - return; + return; /* Update term and check against thresholds */ if (!recalc_normal_span(span)) - return; /* didn't exceed thresholds, we're done */ + return; /* didn't exceed thresholds, we're done */ /* Exceeded thresholds, need to add to Q for expansion */ if (span->id <= 0) - STAILQ_INSERT_TAIL(&optd->QA, span, linkq); + STAILQ_INSERT_TAIL(&optd->QA, span, linkq); else - STAILQ_INSERT_TAIL(&optd->QB, span, linkq); - span->active = 2; /* Mark as being in Q */ + STAILQ_INSERT_TAIL(&optd->QB, span, linkq); + span->active = 2; /* Mark as being in Q */ } void @@ -1367,270 +1367,270 @@ yasm_object_optimize(yasm_object *object, yasm_errwarns *errwarns) /* Step 1a */ STAILQ_FOREACH(sect, &object->sections, link) { - unsigned long offset = 0; - - yasm_bytecode *bc = STAILQ_FIRST(§->bcs); - yasm_bytecode *prevbc; - - bc->bc_index = bc_index++; - - /* Skip our locally created empty bytecode first. */ - prevbc = bc; - bc = STAILQ_NEXT(bc, link); - - /* Iterate through the remainder, if any. */ - while (bc) { - bc->bc_index = bc_index++; - bc->offset = offset; - - retval = yasm_bc_calc_len(bc, optimize_add_span, &optd); - yasm_errwarn_propagate(errwarns, bc->line); - if (retval) - saw_error = 1; - else { - if (bc->callback->special == YASM_BC_SPECIAL_OFFSET) { - /* Remember it as offset setter */ - os->bc = bc; - os->thres = yasm_bc_next_offset(bc); - - /* Create new placeholder */ - os = yasm_xmalloc(sizeof(yasm_offset_setter)); - os->bc = NULL; - os->cur_val = 0; - os->new_val = 0; - os->thres = 0; - STAILQ_INSERT_TAIL(&optd.offset_setters, os, link); - optd.os = os; - - if (bc->multiple) { - yasm_error_set(YASM_ERROR_VALUE, - N_("cannot combine multiples and setting assembly position")); - yasm_errwarn_propagate(errwarns, bc->line); - saw_error = 1; - } - } - - offset += bc->len*bc->mult_int; - } - - prevbc = bc; - bc = STAILQ_NEXT(bc, link); - } + unsigned long offset = 0; + + yasm_bytecode *bc = STAILQ_FIRST(§->bcs); + yasm_bytecode *prevbc; + + bc->bc_index = bc_index++; + + /* Skip our locally created empty bytecode first. */ + prevbc = bc; + bc = STAILQ_NEXT(bc, link); + + /* Iterate through the remainder, if any. */ + while (bc) { + bc->bc_index = bc_index++; + bc->offset = offset; + + retval = yasm_bc_calc_len(bc, optimize_add_span, &optd); + yasm_errwarn_propagate(errwarns, bc->line); + if (retval) + saw_error = 1; + else { + if (bc->callback->special == YASM_BC_SPECIAL_OFFSET) { + /* Remember it as offset setter */ + os->bc = bc; + os->thres = yasm_bc_next_offset(bc); + + /* Create new placeholder */ + os = yasm_xmalloc(sizeof(yasm_offset_setter)); + os->bc = NULL; + os->cur_val = 0; + os->new_val = 0; + os->thres = 0; + STAILQ_INSERT_TAIL(&optd.offset_setters, os, link); + optd.os = os; + + if (bc->multiple) { + yasm_error_set(YASM_ERROR_VALUE, + N_("cannot combine multiples and setting assembly position")); + yasm_errwarn_propagate(errwarns, bc->line); + saw_error = 1; + } + } + + offset += bc->len*bc->mult_int; + } + + prevbc = bc; + bc = STAILQ_NEXT(bc, link); + } } if (saw_error) { - optimize_cleanup(&optd); - return; + optimize_cleanup(&optd); + return; } /* Step 1b */ TAILQ_FOREACH_SAFE(span, &optd.spans, link, span_temp) { - span_create_terms(span); - if (yasm_error_occurred()) { - yasm_errwarn_propagate(errwarns, span->bc->line); - saw_error = 1; - } else if (recalc_normal_span(span)) { - retval = yasm_bc_expand(span->bc, span->id, span->cur_val, - span->new_val, &span->neg_thres, - &span->pos_thres); - yasm_errwarn_propagate(errwarns, span->bc->line); - if (retval < 0) - saw_error = 1; - else if (retval > 0) { - if (!span->active) { - yasm_error_set(YASM_ERROR_VALUE, - N_("secondary expansion of an external/complex value")); - yasm_errwarn_propagate(errwarns, span->bc->line); - saw_error = 1; - } - } else { - TAILQ_REMOVE(&optd.spans, span, link); - span_destroy(span); - continue; - } - } - span->cur_val = span->new_val; + span_create_terms(span); + if (yasm_error_occurred()) { + yasm_errwarn_propagate(errwarns, span->bc->line); + saw_error = 1; + } else if (recalc_normal_span(span)) { + retval = yasm_bc_expand(span->bc, span->id, span->cur_val, + span->new_val, &span->neg_thres, + &span->pos_thres); + yasm_errwarn_propagate(errwarns, span->bc->line); + if (retval < 0) + saw_error = 1; + else if (retval > 0) { + if (!span->active) { + yasm_error_set(YASM_ERROR_VALUE, + N_("secondary expansion of an external/complex value")); + yasm_errwarn_propagate(errwarns, span->bc->line); + saw_error = 1; + } + } else { + TAILQ_REMOVE(&optd.spans, span, link); + span_destroy(span); + continue; + } + } + span->cur_val = span->new_val; } if (saw_error) { - optimize_cleanup(&optd); - return; + optimize_cleanup(&optd); + return; } /* Step 1c */ if (update_all_bc_offsets(object, errwarns)) { - optimize_cleanup(&optd); - return; + optimize_cleanup(&optd); + return; } /* Step 1d */ STAILQ_INIT(&optd.QB); TAILQ_FOREACH(span, &optd.spans, link) { - yasm_intnum *intn; - - /* Update span terms based on new bc offsets */ - for (i=0; inum_terms; i++) { - intn = yasm_calc_bc_dist(span->terms[i].precbc, - span->terms[i].precbc2); - if (!intn) - yasm_internal_error(N_("could not calculate bc distance")); - span->terms[i].cur_val = span->terms[i].new_val; - span->terms[i].new_val = yasm_intnum_get_int(intn); - yasm_intnum_destroy(intn); - } - if (span->rel_term) { - span->rel_term->cur_val = span->rel_term->new_val; - if (span->rel_term->precbc2) - span->rel_term->new_val = - yasm_bc_next_offset(span->rel_term->precbc2) - - span->bc->offset; - else - span->rel_term->new_val = span->bc->offset - - yasm_bc_next_offset(span->rel_term->precbc); - } - - if (recalc_normal_span(span)) { - /* Exceeded threshold, add span to QB */ - STAILQ_INSERT_TAIL(&optd.QB, span, linkq); - span->active = 2; - } + yasm_intnum *intn; + + /* Update span terms based on new bc offsets */ + for (i=0; inum_terms; i++) { + intn = yasm_calc_bc_dist(span->terms[i].precbc, + span->terms[i].precbc2); + if (!intn) + yasm_internal_error(N_("could not calculate bc distance")); + span->terms[i].cur_val = span->terms[i].new_val; + span->terms[i].new_val = yasm_intnum_get_int(intn); + yasm_intnum_destroy(intn); + } + if (span->rel_term) { + span->rel_term->cur_val = span->rel_term->new_val; + if (span->rel_term->precbc2) + span->rel_term->new_val = + yasm_bc_next_offset(span->rel_term->precbc2) - + span->bc->offset; + else + span->rel_term->new_val = span->bc->offset - + yasm_bc_next_offset(span->rel_term->precbc); + } + + if (recalc_normal_span(span)) { + /* Exceeded threshold, add span to QB */ + STAILQ_INSERT_TAIL(&optd.QB, span, linkq); + span->active = 2; + } } /* Do we need step 2? If not, go ahead and exit. */ if (STAILQ_EMPTY(&optd.QB)) { - optimize_cleanup(&optd); - return; + optimize_cleanup(&optd); + return; } /* Update offset-setters values */ STAILQ_FOREACH(os, &optd.offset_setters, link) { - if (!os->bc) - continue; - os->thres = yasm_bc_next_offset(os->bc); - os->new_val = os->bc->offset; - os->cur_val = os->new_val; + if (!os->bc) + continue; + os->thres = yasm_bc_next_offset(os->bc); + os->new_val = os->bc->offset; + os->cur_val = os->new_val; } /* Build up interval tree */ TAILQ_FOREACH(span, &optd.spans, link) { - for (i=0; inum_terms; i++) - optimize_itree_add(optd.itree, span, &span->terms[i]); - if (span->rel_term) - optimize_itree_add(optd.itree, span, span->rel_term); + for (i=0; inum_terms; i++) + optimize_itree_add(optd.itree, span, &span->terms[i]); + if (span->rel_term) + optimize_itree_add(optd.itree, span, span->rel_term); } /* Look for cycles in times expansion (span.id==0) */ TAILQ_FOREACH(span, &optd.spans, link) { - if (span->id > 0) - continue; - optd.span = span; - IT_enumerate(optd.itree, (long)span->bc->bc_index, - (long)span->bc->bc_index, &optd, check_cycle); - if (yasm_error_occurred()) { - yasm_errwarn_propagate(errwarns, span->bc->line); - saw_error = 1; - } + if (span->id > 0) + continue; + optd.span = span; + IT_enumerate(optd.itree, (long)span->bc->bc_index, + (long)span->bc->bc_index, &optd, check_cycle); + if (yasm_error_occurred()) { + yasm_errwarn_propagate(errwarns, span->bc->line); + saw_error = 1; + } } if (saw_error) { - optimize_cleanup(&optd); - return; + optimize_cleanup(&optd); + return; } /* Step 2 */ STAILQ_INIT(&optd.QA); while (!STAILQ_EMPTY(&optd.QA) || !(STAILQ_EMPTY(&optd.QB))) { - unsigned long orig_len; - long offset_diff; - - /* QA is for TIMES, update those first, then update non-TIMES. - * This is so that TIMES can absorb increases before we look at - * expanding non-TIMES BCs. - */ - if (!STAILQ_EMPTY(&optd.QA)) { - span = STAILQ_FIRST(&optd.QA); - STAILQ_REMOVE_HEAD(&optd.QA, linkq); - } else { - span = STAILQ_FIRST(&optd.QB); - STAILQ_REMOVE_HEAD(&optd.QB, linkq); - } - - if (!span->active) - continue; - span->active = 1; /* no longer in Q */ - - /* Make sure we ended up ultimately exceeding thresholds; due to - * offset BCs we may have been placed on Q and then reduced in size - * again. - */ - if (!recalc_normal_span(span)) - continue; - - orig_len = span->bc->len * span->bc->mult_int; - - retval = yasm_bc_expand(span->bc, span->id, span->cur_val, - span->new_val, &span->neg_thres, - &span->pos_thres); - yasm_errwarn_propagate(errwarns, span->bc->line); - - if (retval < 0) { - /* error */ - saw_error = 1; - continue; - } else if (retval > 0) { - /* another threshold, keep active */ - for (i=0; inum_terms; i++) - span->terms[i].cur_val = span->terms[i].new_val; - if (span->rel_term) - span->rel_term->cur_val = span->rel_term->new_val; - span->cur_val = span->new_val; - } else - span->active = 0; /* we're done with this span */ - - optd.len_diff = span->bc->len * span->bc->mult_int - orig_len; - if (optd.len_diff == 0) - continue; /* didn't increase in size */ - - /* Iterate over all spans dependent across the bc just expanded */ - IT_enumerate(optd.itree, (long)span->bc->bc_index, - (long)span->bc->bc_index, &optd, optimize_term_expand); - - /* Iterate over offset-setters that follow the bc just expanded. - * Stop iteration if: - * - no more offset-setters in this section - * - offset-setter didn't move its following offset - */ - os = span->os; - offset_diff = optd.len_diff; - while (os->bc && os->bc->section == span->bc->section - && offset_diff != 0) { - unsigned long old_next_offset = os->cur_val + os->bc->len; - long neg_thres_temp; - - if (offset_diff < 0 && (unsigned long)(-offset_diff) > os->new_val) - yasm_internal_error(N_("org/align went to negative offset")); - os->new_val += offset_diff; - - orig_len = os->bc->len; - retval = yasm_bc_expand(os->bc, 1, (long)os->cur_val, - (long)os->new_val, &neg_thres_temp, - (long *)&os->thres); - yasm_errwarn_propagate(errwarns, os->bc->line); - - offset_diff = os->new_val + os->bc->len - old_next_offset; - optd.len_diff = os->bc->len - orig_len; - if (optd.len_diff != 0) - IT_enumerate(optd.itree, (long)os->bc->bc_index, - (long)os->bc->bc_index, &optd, optimize_term_expand); - - os->cur_val = os->new_val; - os = STAILQ_NEXT(os, link); - } + unsigned long orig_len; + long offset_diff; + + /* QA is for TIMES, update those first, then update non-TIMES. + * This is so that TIMES can absorb increases before we look at + * expanding non-TIMES BCs. + */ + if (!STAILQ_EMPTY(&optd.QA)) { + span = STAILQ_FIRST(&optd.QA); + STAILQ_REMOVE_HEAD(&optd.QA, linkq); + } else { + span = STAILQ_FIRST(&optd.QB); + STAILQ_REMOVE_HEAD(&optd.QB, linkq); + } + + if (!span->active) + continue; + span->active = 1; /* no longer in Q */ + + /* Make sure we ended up ultimately exceeding thresholds; due to + * offset BCs we may have been placed on Q and then reduced in size + * again. + */ + if (!recalc_normal_span(span)) + continue; + + orig_len = span->bc->len * span->bc->mult_int; + + retval = yasm_bc_expand(span->bc, span->id, span->cur_val, + span->new_val, &span->neg_thres, + &span->pos_thres); + yasm_errwarn_propagate(errwarns, span->bc->line); + + if (retval < 0) { + /* error */ + saw_error = 1; + continue; + } else if (retval > 0) { + /* another threshold, keep active */ + for (i=0; inum_terms; i++) + span->terms[i].cur_val = span->terms[i].new_val; + if (span->rel_term) + span->rel_term->cur_val = span->rel_term->new_val; + span->cur_val = span->new_val; + } else + span->active = 0; /* we're done with this span */ + + optd.len_diff = span->bc->len * span->bc->mult_int - orig_len; + if (optd.len_diff == 0) + continue; /* didn't increase in size */ + + /* Iterate over all spans dependent across the bc just expanded */ + IT_enumerate(optd.itree, (long)span->bc->bc_index, + (long)span->bc->bc_index, &optd, optimize_term_expand); + + /* Iterate over offset-setters that follow the bc just expanded. + * Stop iteration if: + * - no more offset-setters in this section + * - offset-setter didn't move its following offset + */ + os = span->os; + offset_diff = optd.len_diff; + while (os->bc && os->bc->section == span->bc->section + && offset_diff != 0) { + unsigned long old_next_offset = os->cur_val + os->bc->len; + long neg_thres_temp; + + if (offset_diff < 0 && (unsigned long)(-offset_diff) > os->new_val) + yasm_internal_error(N_("org/align went to negative offset")); + os->new_val += offset_diff; + + orig_len = os->bc->len; + retval = yasm_bc_expand(os->bc, 1, (long)os->cur_val, + (long)os->new_val, &neg_thres_temp, + (long *)&os->thres); + yasm_errwarn_propagate(errwarns, os->bc->line); + + offset_diff = os->new_val + os->bc->len - old_next_offset; + optd.len_diff = os->bc->len - orig_len; + if (optd.len_diff != 0) + IT_enumerate(optd.itree, (long)os->bc->bc_index, + (long)os->bc->bc_index, &optd, optimize_term_expand); + + os->cur_val = os->new_val; + os = STAILQ_NEXT(os, link); + } } if (saw_error) { - optimize_cleanup(&optd); - return; + optimize_cleanup(&optd); + return; } /* Step 3 */ diff --git a/libyasm/section.h b/libyasm/section.h index 1b1b18f3..d4f5cc86 100644 --- a/libyasm/section.h +++ b/libyasm/section.h @@ -42,20 +42,20 @@ typedef struct yasm_reloc yasm_reloc; #ifdef YASM_LIB_INTERNAL struct yasm_reloc { /*@reldef@*/ STAILQ_ENTRY(yasm_reloc) link; - yasm_intnum *addr; /**< Offset (address) within section */ - /*@dependent@*/ yasm_symrec *sym; /**< Relocated symbol */ + yasm_intnum *addr; /**< Offset (address) within section */ + /*@dependent@*/ yasm_symrec *sym; /**< Relocated symbol */ }; #endif /** An object. This is the internal representation of an object file. */ struct yasm_object { - /*@owned@*/ char *src_filename; /**< Source filename */ - /*@owned@*/ char *obj_filename; /**< Object filename */ + /*@owned@*/ char *src_filename; /**< Source filename */ + /*@owned@*/ char *obj_filename; /**< Object filename */ - /*@owned@*/ yasm_symtab *symtab; /**< Symbol table */ - /*@owned@*/ yasm_arch *arch; /**< Target architecture */ - /*@owned@*/ yasm_objfmt *objfmt; /**< Object format */ - /*@owned@*/ yasm_dbgfmt *dbgfmt; /**< Debug format */ + /*@owned@*/ yasm_symtab *symtab; /**< Symbol table */ + /*@owned@*/ yasm_arch *arch; /**< Target architecture */ + /*@owned@*/ yasm_objfmt *objfmt; /**< Object format */ + /*@owned@*/ yasm_dbgfmt *dbgfmt; /**< Debug format */ /** Currently active section. Used by some directives. */ /*@dependent@*/ yasm_section *cur_section; @@ -74,11 +74,11 @@ struct yasm_object { /** Create a new object. A default section is created as the first section. * An empty symbol table (yasm_symtab) and line mapping (yasm_linemap) are * automatically created. - * \param src_filename source filename (e.g. "file.asm") - * \param obj_filename object filename (e.g. "file.o") - * \param arch architecture - * \param objfmt_module object format module - * \param dbgfmt_module debug format module + * \param src_filename source filename (e.g. "file.asm") + * \param obj_filename object filename (e.g. "file.o") + * \param arch architecture + * \param objfmt_module object format module + * \param dbgfmt_module debug format module * \return Newly allocated object, or NULL on error. */ /*@null@*/ /*@only@*/ yasm_object *yasm_object_create @@ -90,17 +90,17 @@ struct yasm_object { /** Create a new, or continue an existing, general section. The section is * added to the object if there's not already a section by that name. * \param object object - * \param name section name - * \param start starting address (ignored if section already exists), - * NULL if 0 or don't care. - * \param align alignment in bytes (0 if none) - * \param code if nonzero, section is intended to contain code - * (e.g. alignment should be made with NOP instructions, not 0) + * \param name section name + * \param start starting address (ignored if section already exists), + * NULL if 0 or don't care. + * \param align alignment in bytes (0 if none) + * \param code if nonzero, section is intended to contain code + * (e.g. alignment should be made with NOP instructions, not 0) * \param res_only if nonzero, only space-reserving bytecodes are allowed in - * the section (ignored if section already exists) - * \param isnew output; set to nonzero if section did not already exist + * the section (ignored if section already exists) + * \param isnew output; set to nonzero if section did not already exist * \param line virtual line of section declaration (ignored if section - * already exists) + * already exists) * \return New section. */ /*@dependent@*/ yasm_section *yasm_object_get_general @@ -111,8 +111,8 @@ struct yasm_object { /** Create a new absolute section. No checking is performed at creation to * check for overlaps with other absolute sections. * \param object object - * \param start starting address (expression) - * \param line virtual line of section declaration + * \param start starting address (expression) + * \param line virtual line of section declaration * \return New section. */ /*@dependent@*/ yasm_section *yasm_object_create_absolute @@ -120,117 +120,117 @@ struct yasm_object { /** Handle a directive. Passed down to object format, debug format, or * architecture as appropriate. - * \param object object - * \param name directive name - * \param parser parser keyword - * \param valparams value/parameters - * \param objext_valparams "object format-specific" value/parameters - * \param line virtual line (from yasm_linemap) + * \param object object + * \param name directive name + * \param parser parser keyword + * \param valparams value/parameters + * \param objext_valparams "object format-specific" value/parameters + * \param line virtual line (from yasm_linemap) * \return 0 if directive recognized, nonzero if unrecognized. */ int yasm_object_directive(yasm_object *object, const char *name, - const char *parser, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, - unsigned long line); + const char *parser, yasm_valparamhead *valparams, + yasm_valparamhead *objext_valparams, + unsigned long line); /** Delete (free allocated memory for) an object. All sections in the * object and all bytecodes within those sections are also deleted. - * \param object object + * \param object object */ void yasm_object_destroy(/*@only@*/ yasm_object *object); /** Print an object. For debugging purposes. - * \param object object - * \param f file - * \param indent_level indentation level + * \param object object + * \param f file + * \param indent_level indentation level */ void yasm_object_print(const yasm_object *object, FILE *f, int indent_level); /** Finalize an object after parsing. - * \param object object - * \param errwarns error/warning set + * \param object object + * \param errwarns error/warning set * \note Errors/warnings are stored into errwarns. */ void yasm_object_finalize(yasm_object *object, yasm_errwarns *errwarns); /** Traverses all sections in an object, calling a function on each section. - * \param object object - * \param d data pointer passed to func on each call - * \param func function + * \param object object + * \param d data pointer passed to func on each call + * \param func function * \return Stops early (and returns func's return value) if func returns a - * nonzero value; otherwise 0. + * nonzero value; otherwise 0. */ int yasm_object_sections_traverse (yasm_object *object, /*@null@*/ void *d, int (*func) (yasm_section *sect, /*@null@*/ void *d)); /** Find a general section in an object, based on its name. - * \param object object - * \param name section name + * \param object object + * \param name section name * \return Section matching name, or NULL if no match found. */ /*@dependent@*/ /*@null@*/ yasm_section *yasm_object_find_general (yasm_object *object, const char *name); /** Change the source filename for an object. - * \param object object - * \param src_filename new source filename (e.g. "file.asm") + * \param object object + * \param src_filename new source filename (e.g. "file.asm") */ void yasm_object_set_source_fn(yasm_object *object, const char *src_filename); /** Optimize an object. Takes the unoptimized object and optimizes it. * If successful, the object is ready for output to an object file. - * \param object object - * \param errwarns error/warning set + * \param object object + * \param errwarns error/warning set * \note Optimization failures are stored into errwarns. */ void yasm_object_optimize(yasm_object *object, yasm_errwarns *errwarns); /** Determine if a section is absolute or general. - * \param sect section + * \param sect section * \return Nonzero if section is absolute. */ int yasm_section_is_absolute(yasm_section *sect); /** Determine if a section is flagged to contain code. - * \param sect section + * \param sect section * \return Nonzero if section is flagged to contain code. */ int yasm_section_is_code(yasm_section *sect); /** Get yasm_optimizer-specific flags. For yasm_optimizer use only. - * \param sect section + * \param sect section * \return Optimizer-specific flags. */ unsigned long yasm_section_get_opt_flags(const yasm_section *sect); /** Set yasm_optimizer-specific flags. For yasm_optimizer use only. - * \param sect section + * \param sect section * \param opt_flags optimizer-specific flags. */ void yasm_section_set_opt_flags(yasm_section *sect, unsigned long opt_flags); /** Determine if a section was declared as the "default" section (e.g. not * created through a section directive). - * \param sect section + * \param sect section * \return Nonzero if section was declared as default. */ int yasm_section_is_default(const yasm_section *sect); /** Set section "default" flag to a new value. - * \param sect section - * \param def new value of default flag + * \param sect section + * \param def new value of default flag */ void yasm_section_set_default(yasm_section *sect, int def); /** Get object owner of a section. - * \param sect section + * \param sect section * \return Object this section is a part of. */ yasm_object *yasm_section_get_object(const yasm_section *sect); /** Get assocated data for a section and data callback. - * \param sect section + * \param sect section * \param callback callback used when adding data * \return Associated data (NULL if none). */ @@ -239,18 +239,18 @@ yasm_object *yasm_section_get_object(const yasm_section *sect); /** Add associated data to a section. * \attention Deletes any existing associated data for that data callback. - * \param sect section + * \param sect section * \param callback callback - * \param data data to associate + * \param data data to associate */ void yasm_section_add_data(yasm_section *sect, - const yasm_assoc_data_callback *callback, - /*@null@*/ /*@only@*/ void *data); + const yasm_assoc_data_callback *callback, + /*@null@*/ /*@only@*/ void *data); /** Add a relocation to a section. - * \param sect section - * \param reloc relocation - * \param destroy_func function that can destroy the relocation + * \param sect section + * \param reloc relocation + * \param destroy_func function that can destroy the relocation * \note Does not make a copy of reloc. The same destroy_func must be * used for all relocations in a section or an internal error will occur. * The section will destroy the relocation address; it is the caller's @@ -260,63 +260,63 @@ void yasm_section_add_reloc(yasm_section *sect, yasm_reloc *reloc, void (*destroy_func) (/*@only@*/ void *reloc)); /** Get the first relocation for a section. - * \param sect section + * \param sect section * \return First relocation for section. NULL if no relocations. */ /*@null@*/ yasm_reloc *yasm_section_relocs_first(yasm_section *sect); /** Get the next relocation for a section. - * \param reloc previous relocation + * \param reloc previous relocation * \return Next relocation for section. NULL if no more relocations. */ /*@null@*/ yasm_reloc *yasm_section_reloc_next(yasm_reloc *reloc); #ifdef YASM_LIB_INTERNAL -#define yasm_section_reloc_next(x) STAILQ_NEXT((x), link) +#define yasm_section_reloc_next(x) STAILQ_NEXT((x), link) #endif /** Get the basic relocation information for a relocation. - * \param reloc relocation - * \param addrp address of relocation within section (returned) - * \param symp relocated symbol (returned) + * \param reloc relocation + * \param addrp address of relocation within section (returned) + * \param symp relocated symbol (returned) */ void yasm_reloc_get(yasm_reloc *reloc, yasm_intnum **addrp, - /*@dependent@*/ yasm_symrec **symp); + /*@dependent@*/ yasm_symrec **symp); /** Get the first bytecode in a section. - * \param sect section + * \param sect section * \return First bytecode in section (at least one empty bytecode is always - * present). + * present). */ yasm_bytecode *yasm_section_bcs_first(yasm_section *sect); /** Get the last bytecode in a section. - * \param sect section + * \param sect section * \return Last bytecode in section (at least one empty bytecode is always - * present). + * present). */ yasm_bytecode *yasm_section_bcs_last(yasm_section *sect); /** Add bytecode to the end of a section. * \note Does not make a copy of bc; so don't pass this function static or - * local variables, and discard the bc pointer after calling this - * function. - * \param sect section - * \param bc bytecode (may be NULL) + * local variables, and discard the bc pointer after calling this + * function. + * \param sect section + * \param bc bytecode (may be NULL) * \return If bytecode was actually appended (it wasn't NULL or empty), the - * bytecode; otherwise NULL. + * bytecode; otherwise NULL. */ /*@only@*/ /*@null@*/ yasm_bytecode *yasm_section_bcs_append (yasm_section *sect, /*@returned@*/ /*@only@*/ /*@null@*/ yasm_bytecode *bc); /** Traverses all bytecodes in a section, calling a function on each bytecode. - * \param sect section + * \param sect section * \param errwarns error/warning set (may be NULL) - * \param d data pointer passed to func on each call (may be NULL) - * \param func function + * \param d data pointer passed to func on each call (may be NULL) + * \param func function * \return Stops early (and returns func's return value) if func returns a - * nonzero value; otherwise 0. + * nonzero value; otherwise 0. * \note If errwarns is non-NULL, yasm_errwarn_propagate() is called after * each call to func (with the bytecode's line number). */ @@ -339,42 +339,42 @@ int yasm_section_bcs_traverse (const yasm_section *sect); /** Change starting address of a section. - * \param sect section - * \param start starting address - * \param line virtual line + * \param sect section + * \param start starting address + * \param line virtual line */ void yasm_section_set_start(yasm_section *sect, /*@only@*/ yasm_expr *start, - unsigned long line); + unsigned long line); /** Get starting address of a section. - * \param sect section + * \param sect section * \return Starting address (may be a complex expression if section is - * absolute). + * absolute). */ /*@observer@*/ const yasm_expr *yasm_section_get_start (const yasm_section *sect); /** Change alignment of a section. - * \param sect section - * \param align alignment in bytes - * \param line virtual line + * \param sect section + * \param align alignment in bytes + * \param line virtual line */ void yasm_section_set_align(yasm_section *sect, unsigned long align, - unsigned long line); + unsigned long line); /** Get alignment of a section. - * \param sect section + * \param sect section * \return Alignment in bytes (0 if none). */ unsigned long yasm_section_get_align(const yasm_section *sect); /** Print a section. For debugging purposes. - * \param f file - * \param indent_level indentation level - * \param sect section - * \param print_bcs if nonzero, print bytecodes within section + * \param f file + * \param indent_level indentation level + * \param sect section + * \param print_bcs if nonzero, print bytecodes within section */ void yasm_section_print(/*@null@*/ const yasm_section *sect, FILE *f, - int indent_level, int print_bcs); + int indent_level, int print_bcs); #endif diff --git a/libyasm/strcasecmp.c b/libyasm/strcasecmp.c index 441d0456..66424c6b 100644 --- a/libyasm/strcasecmp.c +++ b/libyasm/strcasecmp.c @@ -3,7 +3,7 @@ * or strcmpi(). * * Copyright (c) 1987, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -40,7 +40,7 @@ #endif #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93"; +static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include @@ -57,14 +57,14 @@ yasm__strcasecmp(const char *s1, const char *s2) #elif HAVE_STRCMPI return strcmpi(s1, s2); #else - const unsigned char - *us1 = (const unsigned char *)s1, - *us2 = (const unsigned char *)s2; + const unsigned char + *us1 = (const unsigned char *)s1, + *us2 = (const unsigned char *)s2; - while (tolower(*us1) == tolower(*us2++)) - if (*us1++ == '\0') - return (0); - return (tolower(*us1) - tolower(*--us2)); + while (tolower(*us1) == tolower(*us2++)) + if (*us1++ == '\0') + return (0); + return (tolower(*us1) - tolower(*--us2)); #endif } @@ -80,18 +80,18 @@ yasm__strncasecmp(const char *s1, const char *s2, size_t n) #elif HAVE_STRCMPI return strncmpi(s1, s2, n); #else - const unsigned char - *us1 = (const unsigned char *)s1, - *us2 = (const unsigned char *)s2; + const unsigned char + *us1 = (const unsigned char *)s1, + *us2 = (const unsigned char *)s2; - if (n != 0) { - do { - if (tolower(*us1) != tolower(*us2++)) - return (tolower(*us1) - tolower(*--us2)); - if (*us1++ == '\0') - break; - } while (--n != 0); - } - return (0); + if (n != 0) { + do { + if (tolower(*us1) != tolower(*us2++)) + return (tolower(*us1) - tolower(*--us2)); + if (*us1++ == '\0') + break; + } while (--n != 0); + } + return (0); #endif } diff --git a/libyasm/strsep.c b/libyasm/strsep.c index 28e261f0..f57c1b0a 100644 --- a/libyasm/strsep.c +++ b/libyasm/strsep.c @@ -2,7 +2,7 @@ * strsep() implementation for systems that don't have it. * * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -35,7 +35,7 @@ #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; +static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #ifdef HAVE_STRSEP @@ -60,28 +60,28 @@ yasm__strsep(char **stringp, const char *delim) #ifdef HAVE_STRSEP return strsep(stringp, delim); #else - register char *s; - register const char *spanp; - register int c, sc; - char *tok; + register char *s; + register const char *spanp; + register int c, sc; + char *tok; - if ((s = *stringp) == NULL) - return (NULL); - for (tok = s;;) { - c = *s++; - spanp = delim; - do { - if ((sc = *spanp++) == c) { - if (c == 0) - s = NULL; - else - s[-1] = 0; - *stringp = s; - return (tok); - } - } while (sc != 0); - } - /* NOTREACHED */ + if ((s = *stringp) == NULL) + return (NULL); + for (tok = s;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *stringp = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ #endif } /*@=nullstate@*/ diff --git a/libyasm/symrec.c b/libyasm/symrec.c index 30b6c91f..705f71ca 100644 --- a/libyasm/symrec.c +++ b/libyasm/symrec.c @@ -50,14 +50,14 @@ typedef enum { - SYM_UNKNOWN, /* for unknown type (COMMON/EXTERN) */ - SYM_EQU, /* for EQU defined symbols (expressions) */ - SYM_LABEL, /* for labels */ - SYM_CURPOS, /* for labels representing the current - assembly position */ - SYM_SPECIAL /* for special symbols that need to be in - the symbol table but otherwise have no - purpose */ + SYM_UNKNOWN, /* for unknown type (COMMON/EXTERN) */ + SYM_EQU, /* for EQU defined symbols (expressions) */ + SYM_LABEL, /* for labels */ + SYM_CURPOS, /* for labels representing the current + assembly position */ + SYM_SPECIAL /* for special symbols that need to be in + the symbol table but otherwise have no + purpose */ } sym_type; struct yasm_symrec { @@ -65,14 +65,14 @@ struct yasm_symrec { sym_type type; yasm_sym_status status; yasm_sym_vis visibility; - unsigned long def_line; /* line where symbol was first defined */ - unsigned long decl_line; /* line where symbol was first declared */ - unsigned long use_line; /* line where symbol was first used */ + unsigned long def_line; /* line where symbol was first defined */ + unsigned long decl_line; /* line where symbol was first declared */ + unsigned long use_line; /* line where symbol was first used */ union { - yasm_expr *expn; /* equ value */ + yasm_expr *expn; /* equ value */ - /* bytecode immediately preceding a label */ - /*@dependent@*/ yasm_bytecode *precbc; + /* bytecode immediately preceding a label */ + /*@dependent@*/ yasm_bytecode *precbc; } value; /* associated data; NULL if none */ @@ -144,7 +144,7 @@ symrec_destroy_one(/*@only@*/ void *d) yasm_symrec *sym = d; yasm_xfree(sym->name); if (sym->type == SYM_EQU && (sym->status & YASM_SYM_VALUED)) - yasm_expr_destroy(sym->value.expn); + yasm_expr_destroy(sym->value.expn); yasm__assoc_data_destroy(sym->assoc_data); yasm_xfree(sym); } @@ -172,7 +172,7 @@ symtab_get_or_new_in_table(yasm_symtab *symtab, /*@only@*/ char *name) rec->status = YASM_SYM_NOSTATUS; return HAMT_insert(symtab->sym_table, name, rec, &replace, - symrec_destroy_one); + symrec_destroy_one); } static /*@partial@*/ /*@dependent@*/ yasm_symrec * @@ -196,15 +196,15 @@ symtab_get_or_new(yasm_symtab *symtab, const char *name, int in_table) char *symname = yasm__xstrdup(name); if (in_table) - return symtab_get_or_new_in_table(symtab, symname); + return symtab_get_or_new_in_table(symtab, symname); else - return symtab_get_or_new_not_in_table(symtab, symname); + return symtab_get_or_new_not_in_table(symtab, symname); } /*@=freshtrans =mustfree@*/ int yasm_symtab_traverse(yasm_symtab *symtab, void *d, - int (*func) (yasm_symrec *sym, void *d)) + int (*func) (yasm_symrec *sym, void *d)) { return HAMT_traverse(symtab->sym_table, d, (int (*) (void *, void *))func); } @@ -236,7 +236,7 @@ yasm_symtab_abs_sym(yasm_symtab *symtab) rec->use_line = 0; rec->type = SYM_EQU; rec->value.expn = - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0); + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0); rec->status |= YASM_SYM_DEFINED|YASM_SYM_VALUED|YASM_SYM_USED; return rec; } @@ -246,7 +246,7 @@ yasm_symtab_use(yasm_symtab *symtab, const char *name, unsigned long line) { yasm_symrec *rec = symtab_get_or_new(symtab, name, 1); if (rec->use_line == 0) - rec->use_line = line; /* set line number of first use */ + rec->use_line = line; /* set line number of first use */ rec->status |= YASM_SYM_USED; return rec; } @@ -259,34 +259,34 @@ yasm_symtab_get(yasm_symtab *symtab, const char *name) static /*@dependent@*/ yasm_symrec * symtab_define(yasm_symtab *symtab, const char *name, sym_type type, - int in_table, unsigned long line) + int in_table, unsigned long line) { yasm_symrec *rec = symtab_get_or_new(symtab, name, in_table); /* Has it been defined before (either by DEFINED or COMMON/EXTERN)? */ if (rec->status & YASM_SYM_DEFINED) { - yasm_error_set_xref(rec->def_line!=0 ? rec->def_line : rec->decl_line, - N_("`%s' previously defined here"), name); - yasm_error_set(YASM_ERROR_GENERAL, N_("redefinition of `%s'"), - name); + yasm_error_set_xref(rec->def_line!=0 ? rec->def_line : rec->decl_line, + N_("`%s' previously defined here"), name); + yasm_error_set(YASM_ERROR_GENERAL, N_("redefinition of `%s'"), + name); } else { - if (rec->visibility & YASM_SYM_EXTERN) - yasm_warn_set(YASM_WARN_GENERAL, - N_("`%s' both defined and declared extern"), name); - rec->def_line = line; /* set line number of definition */ - rec->type = type; - rec->status |= YASM_SYM_DEFINED; + if (rec->visibility & YASM_SYM_EXTERN) + yasm_warn_set(YASM_WARN_GENERAL, + N_("`%s' both defined and declared extern"), name); + rec->def_line = line; /* set line number of definition */ + rec->type = type; + rec->status |= YASM_SYM_DEFINED; } return rec; } yasm_symrec * yasm_symtab_define_equ(yasm_symtab *symtab, const char *name, yasm_expr *e, - unsigned long line) + unsigned long line) { yasm_symrec *rec = symtab_define(symtab, name, SYM_EQU, 1, line); if (yasm_error_occurred()) - return rec; + return rec; rec->value.expn = e; rec->status |= YASM_SYM_VALUED; return rec; @@ -294,36 +294,36 @@ yasm_symtab_define_equ(yasm_symtab *symtab, const char *name, yasm_expr *e, yasm_symrec * yasm_symtab_define_label(yasm_symtab *symtab, const char *name, - yasm_bytecode *precbc, int in_table, - unsigned long line) + yasm_bytecode *precbc, int in_table, + unsigned long line) { yasm_symrec *rec = symtab_define(symtab, name, SYM_LABEL, in_table, line); if (yasm_error_occurred()) - return rec; + return rec; rec->value.precbc = precbc; if (in_table && precbc) - yasm_bc__add_symrec(precbc, rec); + yasm_bc__add_symrec(precbc, rec); return rec; } yasm_symrec * yasm_symtab_define_curpos(yasm_symtab *symtab, const char *name, - yasm_bytecode *precbc, unsigned long line) + yasm_bytecode *precbc, unsigned long line) { yasm_symrec *rec = symtab_define(symtab, name, SYM_CURPOS, 0, line); if (yasm_error_occurred()) - return rec; + return rec; rec->value.precbc = precbc; return rec; } yasm_symrec * yasm_symtab_define_special(yasm_symtab *symtab, const char *name, - yasm_sym_vis vis) + yasm_sym_vis vis) { yasm_symrec *rec = symtab_define(symtab, name, SYM_SPECIAL, 1, 0); if (yasm_error_occurred()) - return rec; + return rec; rec->status |= YASM_SYM_VALUED; rec->visibility = vis; return rec; @@ -331,7 +331,7 @@ yasm_symtab_define_special(yasm_symtab *symtab, const char *name, yasm_symrec * yasm_symtab_declare(yasm_symtab *symtab, const char *name, yasm_sym_vis vis, - unsigned long line) + unsigned long line) { yasm_symrec *rec = symtab_get_or_new(symtab, name, 1); yasm_symrec_declare(rec, vis, line); @@ -353,16 +353,16 @@ yasm_symrec_declare(yasm_symrec *rec, yasm_sym_vis vis, unsigned long line) * X 1 - 1 - */ if ((vis == YASM_SYM_GLOBAL) || - (!(rec->status & YASM_SYM_DEFINED) && - (!(rec->visibility & (YASM_SYM_COMMON | YASM_SYM_EXTERN)) || - ((rec->visibility & YASM_SYM_COMMON) && (vis == YASM_SYM_COMMON)) || - ((rec->visibility & YASM_SYM_EXTERN) && (vis == YASM_SYM_EXTERN))))) { - rec->decl_line = line; - rec->visibility |= vis; + (!(rec->status & YASM_SYM_DEFINED) && + (!(rec->visibility & (YASM_SYM_COMMON | YASM_SYM_EXTERN)) || + ((rec->visibility & YASM_SYM_COMMON) && (vis == YASM_SYM_COMMON)) || + ((rec->visibility & YASM_SYM_EXTERN) && (vis == YASM_SYM_EXTERN))))) { + rec->decl_line = line; + rec->visibility |= vis; } else - yasm_error_set(YASM_ERROR_GENERAL, - N_("duplicate definition of `%s'; first defined on line %lu"), - rec->name, rec->def_line!=0 ? rec->def_line : rec->decl_line); + yasm_error_set(YASM_ERROR_GENERAL, + N_("duplicate definition of `%s'; first defined on line %lu"), + rec->name, rec->def_line!=0 ? rec->def_line : rec->decl_line); } typedef struct symtab_finalize_info { @@ -379,16 +379,16 @@ symtab_parser_finalize_checksym(yasm_symrec *sym, /*@null@*/ void *d) /* error if a symbol is used but never defined or extern/common declared */ if ((sym->status & YASM_SYM_USED) && !(sym->status & YASM_SYM_DEFINED) && - !(sym->visibility & (YASM_SYM_EXTERN | YASM_SYM_COMMON))) { - if (info->undef_extern) - sym->visibility |= YASM_SYM_EXTERN; - else { - yasm_error_set(YASM_ERROR_GENERAL, - N_("undefined symbol `%s' (first use)"), sym->name); - yasm_errwarn_propagate(info->errwarns, sym->use_line); - if (sym->use_line < info->firstundef_line) - info->firstundef_line = sym->use_line; - } + !(sym->visibility & (YASM_SYM_EXTERN | YASM_SYM_COMMON))) { + if (info->undef_extern) + sym->visibility |= YASM_SYM_EXTERN; + else { + yasm_error_set(YASM_ERROR_GENERAL, + N_("undefined symbol `%s' (first use)"), sym->name); + yasm_errwarn_propagate(info->errwarns, sym->use_line); + if (sym->use_line < info->firstundef_line) + info->firstundef_line = sym->use_line; + } } /* Change labels in absolute sections into EQUs with value @@ -397,18 +397,18 @@ symtab_parser_finalize_checksym(yasm_symrec *sym, /*@null@*/ void *d) * caught in EQU expansion. */ if (sym->type == SYM_LABEL && sym->value.precbc - && (sect = yasm_bc_get_section(sym->value.precbc)) - && yasm_section_is_absolute(sect)) { - sym->type = SYM_EQU; - sym->value.expn = yasm_expr_create_tree( - yasm_expr_create(YASM_EXPR_SUB, - yasm_expr_precbc(sym->value.precbc), - yasm_expr_precbc(yasm_section_bcs_first(sect)), - sym->def_line), - YASM_EXPR_ADD, - yasm_expr_copy(yasm_section_get_start(sect)), - sym->def_line); - sym->status |= YASM_SYM_VALUED; + && (sect = yasm_bc_get_section(sym->value.precbc)) + && yasm_section_is_absolute(sect)) { + sym->type = SYM_EQU; + sym->value.expn = yasm_expr_create_tree( + yasm_expr_create(YASM_EXPR_SUB, + yasm_expr_precbc(sym->value.precbc), + yasm_expr_precbc(yasm_section_bcs_first(sect)), + sym->def_line), + YASM_EXPR_ADD, + yasm_expr_copy(yasm_section_get_start(sect)), + sym->def_line); + sym->status |= YASM_SYM_VALUED; } return 0; @@ -416,7 +416,7 @@ symtab_parser_finalize_checksym(yasm_symrec *sym, /*@null@*/ void *d) void yasm_symtab_parser_finalize(yasm_symtab *symtab, int undef_extern, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { symtab_finalize_info info; info.firstundef_line = ULONG_MAX; @@ -424,9 +424,9 @@ yasm_symtab_parser_finalize(yasm_symtab *symtab, int undef_extern, info.errwarns = errwarns; yasm_symtab_traverse(symtab, &info, symtab_parser_finalize_checksym); if (info.firstundef_line < ULONG_MAX) { - yasm_error_set(YASM_ERROR_GENERAL, - N_(" (Each undefined symbol is reported only once.)")); - yasm_errwarn_propagate(errwarns, info.firstundef_line); + yasm_error_set(YASM_ERROR_GENERAL, + N_(" (Each undefined symbol is reported only once.)")); + yasm_errwarn_propagate(errwarns, info.firstundef_line); } } @@ -436,10 +436,10 @@ yasm_symtab_destroy(yasm_symtab *symtab) HAMT_destroy(symtab->sym_table, symrec_destroy_one); while (!SLIST_EMPTY(&symtab->non_table_syms)) { - non_table_symrec *sym = SLIST_FIRST(&symtab->non_table_syms); - SLIST_REMOVE_HEAD(&symtab->non_table_syms, link); - symrec_destroy_one(sym->rec); - yasm_xfree(sym); + non_table_symrec *sym = SLIST_FIRST(&symtab->non_table_syms); + SLIST_REMOVE_HEAD(&symtab->non_table_syms, link); + symrec_destroy_one(sym->rec); + yasm_xfree(sym); } yasm_xfree(symtab); @@ -511,18 +511,18 @@ const yasm_expr * yasm_symrec_get_equ(const yasm_symrec *sym) { if (sym->type == SYM_EQU && (sym->status & YASM_SYM_VALUED)) - return sym->value.expn; + return sym->value.expn; return (const yasm_expr *)NULL; } int yasm_symrec_get_label(const yasm_symrec *sym, - yasm_symrec_get_label_bytecodep *precbc) + yasm_symrec_get_label_bytecodep *precbc) { if (!(sym->type == SYM_LABEL || sym->type == SYM_CURPOS) - || !sym->value.precbc) { - *precbc = (yasm_symrec_get_label_bytecodep)0xDEADBEEF; - return 0; + || !sym->value.precbc) { + *precbc = (yasm_symrec_get_label_bytecodep)0xDEADBEEF; + return 0; } *precbc = sym->value.precbc; return 1; @@ -532,7 +532,7 @@ int yasm_symrec_is_abs(const yasm_symrec *sym) { return (sym->def_line == 0 && sym->type == SYM_EQU && - sym->name[0] == '\0'); + sym->name[0] == '\0'); } int @@ -549,7 +549,7 @@ yasm_symrec_is_curpos(const yasm_symrec *sym) void yasm_symrec_set_objext_valparams(yasm_symrec *sym, - /*@only@*/ yasm_valparamhead *objext_valparams) + /*@only@*/ yasm_valparamhead *objext_valparams) { yasm_symrec_add_data(sym, &objext_valparams_cb, objext_valparams); } @@ -562,7 +562,7 @@ yasm_symrec_get_objext_valparams(yasm_symrec *sym) void yasm_symrec_set_common_size(yasm_symrec *sym, - /*@only@*/ yasm_expr *common_size) + /*@only@*/ yasm_expr *common_size) { yasm_expr **ep = yasm_xmalloc(sizeof(yasm_expr *)); *ep = common_size; @@ -577,14 +577,14 @@ yasm_symrec_get_common_size(yasm_symrec *sym) void * yasm_symrec_get_data(yasm_symrec *sym, - const yasm_assoc_data_callback *callback) + const yasm_assoc_data_callback *callback) { return yasm__assoc_data_get(sym->assoc_data, callback); } void yasm_symrec_add_data(yasm_symrec *sym, - const yasm_assoc_data_callback *callback, void *data) + const yasm_assoc_data_callback *callback, void *data) { sym->assoc_data = yasm__assoc_data_add(sym->assoc_data, callback, data); } @@ -593,69 +593,69 @@ void yasm_symrec_print(const yasm_symrec *sym, FILE *f, int indent_level) { switch (sym->type) { - case SYM_UNKNOWN: - fprintf(f, "%*s-Unknown (Common/Extern)-\n", indent_level, ""); - break; - case SYM_EQU: - fprintf(f, "%*s_EQU_\n", indent_level, ""); - fprintf(f, "%*sExpn=", indent_level, ""); - if (sym->status & YASM_SYM_VALUED) - yasm_expr_print(sym->value.expn, f); - else - fprintf(f, "***UNVALUED***"); - fprintf(f, "\n"); - break; - case SYM_LABEL: - case SYM_CURPOS: - fprintf(f, "%*s_%s_\n%*sSection:\n", indent_level, "", - sym->type == SYM_LABEL ? "Label" : "CurPos", - indent_level, ""); - yasm_section_print(yasm_bc_get_section(sym->value.precbc), f, - indent_level+1, 0); - fprintf(f, "%*sPreceding bytecode:\n", indent_level, ""); - yasm_bc_print(sym->value.precbc, f, indent_level+1); - break; - case SYM_SPECIAL: - fprintf(f, "%*s-Special-\n", indent_level, ""); - break; + case SYM_UNKNOWN: + fprintf(f, "%*s-Unknown (Common/Extern)-\n", indent_level, ""); + break; + case SYM_EQU: + fprintf(f, "%*s_EQU_\n", indent_level, ""); + fprintf(f, "%*sExpn=", indent_level, ""); + if (sym->status & YASM_SYM_VALUED) + yasm_expr_print(sym->value.expn, f); + else + fprintf(f, "***UNVALUED***"); + fprintf(f, "\n"); + break; + case SYM_LABEL: + case SYM_CURPOS: + fprintf(f, "%*s_%s_\n%*sSection:\n", indent_level, "", + sym->type == SYM_LABEL ? "Label" : "CurPos", + indent_level, ""); + yasm_section_print(yasm_bc_get_section(sym->value.precbc), f, + indent_level+1, 0); + fprintf(f, "%*sPreceding bytecode:\n", indent_level, ""); + yasm_bc_print(sym->value.precbc, f, indent_level+1); + break; + case SYM_SPECIAL: + fprintf(f, "%*s-Special-\n", indent_level, ""); + break; } fprintf(f, "%*sStatus=", indent_level, ""); if (sym->status == YASM_SYM_NOSTATUS) - fprintf(f, "None\n"); + fprintf(f, "None\n"); else { - if (sym->status & YASM_SYM_USED) - fprintf(f, "Used,"); - if (sym->status & YASM_SYM_DEFINED) - fprintf(f, "Defined,"); - if (sym->status & YASM_SYM_VALUED) - fprintf(f, "Valued,"); - if (sym->status & YASM_SYM_NOTINTABLE) - fprintf(f, "Not in Table,"); - fprintf(f, "\n"); + if (sym->status & YASM_SYM_USED) + fprintf(f, "Used,"); + if (sym->status & YASM_SYM_DEFINED) + fprintf(f, "Defined,"); + if (sym->status & YASM_SYM_VALUED) + fprintf(f, "Valued,"); + if (sym->status & YASM_SYM_NOTINTABLE) + fprintf(f, "Not in Table,"); + fprintf(f, "\n"); } fprintf(f, "%*sVisibility=", indent_level, ""); if (sym->visibility == YASM_SYM_LOCAL) - fprintf(f, "Local\n"); + fprintf(f, "Local\n"); else { - if (sym->visibility & YASM_SYM_GLOBAL) - fprintf(f, "Global,"); - if (sym->visibility & YASM_SYM_COMMON) - fprintf(f, "Common,"); - if (sym->visibility & YASM_SYM_EXTERN) - fprintf(f, "Extern,"); - fprintf(f, "\n"); + if (sym->visibility & YASM_SYM_GLOBAL) + fprintf(f, "Global,"); + if (sym->visibility & YASM_SYM_COMMON) + fprintf(f, "Common,"); + if (sym->visibility & YASM_SYM_EXTERN) + fprintf(f, "Extern,"); + fprintf(f, "\n"); } if (sym->assoc_data) { - fprintf(f, "%*sAssociated data:\n", indent_level, ""); - yasm__assoc_data_print(sym->assoc_data, f, indent_level+1); + fprintf(f, "%*sAssociated data:\n", indent_level, ""); + yasm__assoc_data_print(sym->assoc_data, f, indent_level+1); } fprintf(f, "%*sLine Index (Defined)=%lu\n", indent_level, "", - sym->def_line); + sym->def_line); fprintf(f, "%*sLine Index (Declared)=%lu\n", indent_level, "", - sym->decl_line); + sym->decl_line); fprintf(f, "%*sLine Index (Used)=%lu\n", indent_level, "", sym->use_line); } diff --git a/libyasm/symrec.h b/libyasm/symrec.h index 624a3e98..050ee1a1 100644 --- a/libyasm/symrec.h +++ b/libyasm/symrec.h @@ -39,10 +39,10 @@ * with a visibility of #YASM_SYM_EXTERN or #YASM_SYM_COMMON. */ typedef enum yasm_sym_status { - YASM_SYM_NOSTATUS = 0, /**< no status */ - YASM_SYM_USED = 1 << 0, /**< for use before definition */ - YASM_SYM_DEFINED = 1 << 1, /**< once it's been defined in the file */ - YASM_SYM_VALUED = 1 << 2, /**< once its value has been determined */ + YASM_SYM_NOSTATUS = 0, /**< no status */ + YASM_SYM_USED = 1 << 0, /**< for use before definition */ + YASM_SYM_DEFINED = 1 << 1, /**< once it's been defined in the file */ + YASM_SYM_VALUED = 1 << 2, /**< once its value has been determined */ YASM_SYM_NOTINTABLE = 1 << 3 /**< if it's not in sym_table (ex. '$') */ } yasm_sym_status; @@ -50,11 +50,11 @@ typedef enum yasm_sym_status { * \note YASM_SYM_EXTERN and YASM_SYM_COMMON are mutually exclusive. */ typedef enum yasm_sym_vis { - YASM_SYM_LOCAL = 0, /**< Default, local only */ - YASM_SYM_GLOBAL = 1 << 0, /**< If symbol is declared GLOBAL */ - YASM_SYM_COMMON = 1 << 1, /**< If symbol is declared COMMON */ - YASM_SYM_EXTERN = 1 << 2, /**< If symbol is declared EXTERN */ - YASM_SYM_DLOCAL = 1 << 3 /**< If symbol is explicitly declared LOCAL */ + YASM_SYM_LOCAL = 0, /**< Default, local only */ + YASM_SYM_GLOBAL = 1 << 0, /**< If symbol is declared GLOBAL */ + YASM_SYM_COMMON = 1 << 1, /**< If symbol is declared COMMON */ + YASM_SYM_EXTERN = 1 << 2, /**< If symbol is declared EXTERN */ + YASM_SYM_DLOCAL = 1 << 3 /**< If symbol is explicitly declared LOCAL */ } yasm_sym_vis; /** Create a new symbol table. */ @@ -79,7 +79,7 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); /** Get a reference to (use) a symbol. The symbol does not necessarily need to * be defined before it is used. * \param symtab symbol table - * \param name symbol name + * \param name symbol name * \param line virtual line where referenced * \return Symbol (dependent pointer, do not free). */ @@ -90,7 +90,7 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); * when an internal assembler usage of a symbol shouldn't be treated like a * normal user symbol usage. * \param symtab symbol table - * \param name symbol name + * \param name symbol name * \return Symbol (dependent pointer, do not free). May be NULL if symbol * doesn't exist. */ @@ -99,8 +99,8 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); /** Define a symbol as an EQU value. * \param symtab symbol table - * \param name symbol (EQU) name - * \param e EQU value (expression) + * \param name symbol (EQU) name + * \param e EQU value (expression) * \param line virtual line of EQU * \return Symbol (dependent pointer, do not free). */ @@ -110,11 +110,11 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); /** Define a symbol as a label. * \param symtab symbol table - * \param name symbol (label) name + * \param name symbol (label) name * \param precbc bytecode preceding label * \param in_table nonzero if the label should be inserted into the symbol * table (some specially-generated ones should not be) - * \param line virtual line of label + * \param line virtual line of label * \return Symbol (dependent pointer, do not free). */ /*@dependent@*/ yasm_symrec *yasm_symtab_define_label @@ -126,9 +126,9 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); * as value_finalize_scan() looks for usage of this symbol type for special * handling. The symbol created is not inserted into the symbol table. * \param symtab symbol table - * \param name symbol (label) name + * \param name symbol (label) name * \param precbc bytecode preceding label - * \param line virtual line of label + * \param line virtual line of label * \return Symbol (dependent pointer, do not free). */ /*@dependent@*/ yasm_symrec *yasm_symtab_define_curpos @@ -139,8 +139,8 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); * defined name, but have no other data associated with it within the * standard symrec. * \param symtab symbol table - * \param name symbol name - * \param vis symbol visibility + * \param name symbol name + * \param vis symbol visibility * \return Symbol (dependent pointer, do not free). */ /*@dependent@*/ yasm_symrec *yasm_symtab_define_special @@ -149,8 +149,8 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); /** Declare external visibility of a symbol. * \note Not all visibility combinations are allowed. * \param symtab symbol table - * \param name symbol name - * \param vis visibility + * \param name symbol name + * \param vis visibility * \param line virtual line of visibility-setting * \return Symbol (dependent pointer, do not free). */ @@ -161,15 +161,15 @@ void yasm_symtab_destroy(/*@only@*/ yasm_symtab *symtab); /** Declare external visibility of a symbol. * \note Not all visibility combinations are allowed. * \param symrec symbol - * \param vis visibility + * \param vis visibility * \param line virtual line of visibility-setting */ void yasm_symrec_declare(yasm_symrec *symrec, yasm_sym_vis vis, - unsigned long line); + unsigned long line); /** Callback function for yasm_symrec_traverse(). - * \param sym symbol - * \param d data passed into yasm_symrec_traverse() + * \param sym symbol + * \param d data passed into yasm_symrec_traverse() * \return Nonzero to stop symbol traversal. */ typedef int (*yasm_symtab_traverse_callback) @@ -177,8 +177,8 @@ typedef int (*yasm_symtab_traverse_callback) /** Traverse all symbols in the symbol table. * \param symtab symbol table - * \param d data to pass to each call of callback function - * \param func callback function called on each symbol + * \param d data to pass to each call of callback function + * \param func callback function called on each symbol * \return Nonzero value returned by callback function if it ever returned * nonzero. */ @@ -196,73 +196,73 @@ typedef struct yasm_symtab_iter yasm_symtab_iter; const yasm_symtab_iter *yasm_symtab_first(const yasm_symtab *symtab); /** Move a symbol table iterator to the next symbol in the symbol table. - * \param prev Previous iterator value + * \param prev Previous iterator value * \return Next iterator value, or NULL if no more symbols in the table. */ /*@null@*/ const yasm_symtab_iter *yasm_symtab_next (const yasm_symtab_iter *prev); /** Get the symbol corresponding to the current symbol table iterator value. - * \param cur iterator value + * \param cur iterator value * \return Corresponding symbol. */ yasm_symrec *yasm_symtab_iter_value(const yasm_symtab_iter *cur); /** Finalize symbol table after parsing stage. Checks for symbols that are * used but never defined or declared #YASM_SYM_EXTERN or #YASM_SYM_COMMON. - * \param symtab symbol table - * \param undef_extern if nonzero, all undef syms should be declared extern - * \param errwarns error/warning set + * \param symtab symbol table + * \param undef_extern if nonzero, all undef syms should be declared extern + * \param errwarns error/warning set * \note Errors/warnings are stored into errwarns. */ void yasm_symtab_parser_finalize(yasm_symtab *symtab, int undef_extern, - yasm_errwarns *errwarns); + yasm_errwarns *errwarns); /** Print the symbol table. For debugging purposes. - * \param symtab symbol table - * \param f file - * \param indent_level indentation level + * \param symtab symbol table + * \param f file + * \param indent_level indentation level */ void yasm_symtab_print(yasm_symtab *symtab, FILE *f, int indent_level); /** Get the name of a symbol. - * \param sym symbol + * \param sym symbol * \return Symbol name. */ /*@observer@*/ const char *yasm_symrec_get_name(const yasm_symrec *sym); /** Get the visibility of a symbol. - * \param sym symbol + * \param sym symbol * \return Symbol visibility. */ yasm_sym_vis yasm_symrec_get_visibility(const yasm_symrec *sym); /** Get the status of a symbol. - * \param sym symbol + * \param sym symbol * \return Symbol status. */ yasm_sym_status yasm_symrec_get_status(const yasm_symrec *sym); /** Get the virtual line of where a symbol was first defined. - * \param sym symbol - * \return line virtual line + * \param sym symbol + * \return line virtual line */ unsigned long yasm_symrec_get_def_line(const yasm_symrec *sym); /** Get the virtual line of where a symbol was first declared. - * \param sym symbol - * \return line virtual line + * \param sym symbol + * \return line virtual line */ unsigned long yasm_symrec_get_decl_line(const yasm_symrec *sym); /** Get the virtual line of where a symbol was first used. - * \param sym symbol - * \return line virtual line + * \param sym symbol + * \return line virtual line */ unsigned long yasm_symrec_get_use_line(const yasm_symrec *sym); /** Get EQU value of a symbol. - * \param sym symbol + * \param sym symbol * \return EQU value, or NULL if symbol is not an EQU or is not defined. */ /*@observer@*/ /*@null@*/ const yasm_expr *yasm_symrec_get_equ @@ -272,65 +272,65 @@ unsigned long yasm_symrec_get_use_line(const yasm_symrec *sym); typedef /*@dependent@*/ yasm_bytecode *yasm_symrec_get_label_bytecodep; /** Get the label location of a symbol. - * \param sym symbol + * \param sym symbol * \param precbc bytecode preceding label (output) * \return 0 if not symbol is not a label or if the symbol's visibility is * #YASM_SYM_EXTERN or #YASM_SYM_COMMON (not defined in the file). */ int yasm_symrec_get_label(const yasm_symrec *sym, - /*@out@*/ yasm_symrec_get_label_bytecodep *precbc); + /*@out@*/ yasm_symrec_get_label_bytecodep *precbc); /** Determine if symbol is the "absolute" symbol created by * yasm_symtab_abs_sym(). - * \param sym symbol + * \param sym symbol * \return 0 if symbol is not the "absolute" symbol, nonzero otherwise. */ int yasm_symrec_is_abs(const yasm_symrec *sym); /** Determine if symbol is a special symbol. - * \param sym symbol + * \param sym symbol * \return 0 if symbol is not a special symbol, nonzero otherwise. */ int yasm_symrec_is_special(const yasm_symrec *sym); /** Determine if symbol is a label representing the current assembly position. - * \param sym symbol + * \param sym symbol * \return 0 if symbol is not a current position label, nonzero otherwise. */ int yasm_symrec_is_curpos(const yasm_symrec *sym); /** Set object-extended valparams. - * \param sym symbol - * \param objext_valparams object-extended valparams + * \param sym symbol + * \param objext_valparams object-extended valparams */ void yasm_symrec_set_objext_valparams (yasm_symrec *sym, /*@only@*/ yasm_valparamhead *objext_valparams); /** Get object-extended valparams, if any, associated with symbol's * declaration. - * \param sym symbol + * \param sym symbol * \return Object-extended valparams (NULL if none). */ /*@null@*/ /*@dependent@*/ yasm_valparamhead *yasm_symrec_get_objext_valparams (yasm_symrec *sym); /** Set common size of symbol. - * \param sym symbol - * \param common_size common size expression + * \param sym symbol + * \param common_size common size expression */ void yasm_symrec_set_common_size (yasm_symrec *sym, /*@only@*/ yasm_expr *common_size); /** Get common size of symbol, if symbol is declared COMMON and a size was set * for it. - * \param sym symbol + * \param sym symbol * \return Common size (NULL if none). */ /*@dependent@*/ /*@null@*/ yasm_expr **yasm_symrec_get_common_size (yasm_symrec *sym); /** Get associated data for a symbol and data callback. - * \param sym symbol + * \param sym symbol * \param callback callback used when adding data * \return Associated data (NULL if none). */ @@ -339,18 +339,18 @@ void yasm_symrec_set_common_size /** Add associated data to a symbol. * \attention Deletes any existing associated data for that data callback. - * \param sym symbol + * \param sym symbol * \param callback callback - * \param data data to associate + * \param data data to associate */ void yasm_symrec_add_data(yasm_symrec *sym, - const yasm_assoc_data_callback *callback, - /*@only@*/ /*@null@*/ void *data); + const yasm_assoc_data_callback *callback, + /*@only@*/ /*@null@*/ void *data); /** Print a symbol. For debugging purposes. - * \param f file - * \param indent_level indentation level - * \param sym symbol + * \param f file + * \param indent_level indentation level + * \param sym symbol */ void yasm_symrec_print(const yasm_symrec *sym, FILE *f, int indent_level); diff --git a/libyasm/tests/bitvect_test.c b/libyasm/tests/bitvect_test.c index 3887046c..087025ad 100644 --- a/libyasm/tests/bitvect_test.c +++ b/libyasm/tests/bitvect_test.c @@ -40,36 +40,36 @@ static int test_boot(void) { if (BitVector_Boot() != ErrCode_Ok) - return 1; + return 1; return 0; } typedef struct Val_s { const char *ascii; - unsigned char result[10]; /* 80 bit result, little endian */ + unsigned char result[10]; /* 80 bit result, little endian */ } Val; Val oct_small_vals[] = { - { "0", - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + { "0", + {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} }, - { "1", - {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + { "1", + {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} }, - { "77", - {0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} + { "77", + {0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} }, }; Val oct_large_vals[] = { - { "7654321076543210", - {0x88, 0xC6, 0xFA, 0x88, 0xC6, 0xFA, 0x00, 0x00, 0x00, 0x00} + { "7654321076543210", + {0x88, 0xC6, 0xFA, 0x88, 0xC6, 0xFA, 0x00, 0x00, 0x00, 0x00} }, - { "12634727612534126530214", - {0x8C, 0xB0, 0x5A, 0xE1, 0xAA, 0xF8, 0x3A, 0x67, 0x05, 0x00} + { "12634727612534126530214", + {0x8C, 0xB0, 0x5A, 0xE1, 0xAA, 0xF8, 0x3A, 0x67, 0x05, 0x00} }, - { "61076543210", - {0x88, 0xC6, 0xFA, 0x88, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} + { "61076543210", + {0x88, 0xC6, 0xFA, 0x88, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} }, }; @@ -101,20 +101,20 @@ num_check(Val *val) strcpy((char *)ascii, val->ascii); strcpy(result_msg, "parser failure"); if(BitVector_from_Oct(testval, ascii) != ErrCode_Ok) - return 1; + return 1; result = BitVector_Block_Read(testval, &len); for (i=0; i<10; i++) - if (result[i] != val->result[i]) - ret = 1; + if (result[i] != val->result[i]) + ret = 1; if (ret) { - strcpy(result_msg, val->ascii); - for (i=0; i<10; i++) - sprintf((char *)ascii+3*i, "%02x ", result[i]); - strcat(result_msg, ": "); - strcat(result_msg, (char *)ascii); + strcpy(result_msg, val->ascii); + for (i=0; i<10; i++) + sprintf((char *)ascii+3*i, "%02x ", result[i]); + strcat(result_msg, ": "); + strcat(result_msg, (char *)ascii); } free(result); @@ -128,8 +128,8 @@ test_oct_small_num(void) int i, num = sizeof(oct_small_vals)/sizeof(Val); for (i=0; i0 ? 'F':'.'); fflush(stdout); if (nf > 0) - sprintf(failed, "%s ** F: %s failed!\n", failed, testname); + sprintf(failed, "%s ** F: %s failed!\n", failed, testname); return nf; } -#define runtest(x,y,z) runtest_(#x,test_##x,y,z) +#define runtest(x,y,z) runtest_(#x,test_##x,y,z) int main(void) @@ -178,6 +178,6 @@ main(void) nf += runtest(oct_small_num, num_family_setup, num_family_teardown); nf += runtest(oct_large_num, num_family_setup, num_family_teardown); printf(" +%d-%d/3 %d%%\n%s", - 3-nf, nf, 100*(3-nf)/3, failed); + 3-nf, nf, 100*(3-nf)/3, failed); return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/libyasm/tests/combpath_test.c b/libyasm/tests/combpath_test.c index 5667cc7d..00276135 100644 --- a/libyasm/tests/combpath_test.c +++ b/libyasm/tests/combpath_test.c @@ -104,18 +104,18 @@ run_test(Test_Entry *test) const char *funcname; if (test->combpath == &yasm__combpath_unix) - funcname = "unix"; + funcname = "unix"; else - funcname = "win"; + funcname = "win"; out = test->combpath(test->from, test->to); if (strcmp(out, test->out) != 0) { - sprintf(failmsg, - "combpath_%s(\"%s\", \"%s\"): expected \"%s\", got \"%s\"!", - funcname, test->from, test->to, test->out, out); - yasm_xfree(out); - return 1; + sprintf(failmsg, + "combpath_%s(\"%s\", \"%s\"): expected \"%s\", got \"%s\"!", + funcname, test->from, test->to, test->out, out); + yasm_xfree(out); + return 1; } yasm_xfree(out); @@ -132,15 +132,15 @@ main(void) failed[0] = '\0'; printf("Test combpath_test: "); for (i=0; i0 ? 'F':'.'); - fflush(stdout); - if (fail) - sprintf(failed, "%s ** F: %s\n", failed, failmsg); - nf += fail; + int fail = run_test(&tests[i]); + printf("%c", fail>0 ? 'F':'.'); + fflush(stdout); + if (fail) + sprintf(failed, "%s ** F: %s\n", failed, failmsg); + nf += fail; } printf(" +%d-%d/%d %d%%\n%s", - numtests-nf, nf, numtests, 100*(numtests-nf)/numtests, failed); + numtests-nf, nf, numtests, 100*(numtests-nf)/numtests, failed); return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/libyasm/tests/floatnum_test.c b/libyasm/tests/floatnum_test.c index a7bdb698..e9e36a3c 100644 --- a/libyasm/tests/floatnum_test.c +++ b/libyasm/tests/floatnum_test.c @@ -39,18 +39,18 @@ /* constants describing parameters of internal floating point format. * (these should match those in src/floatnum.c !) */ -#define MANT_BITS 80 -#define MANT_BYTES 10 +#define MANT_BITS 80 +#define MANT_BYTES 10 typedef struct Init_Entry_s { /* input ASCII value */ const char *ascii; /* correct output from ASCII conversion */ - unsigned char mantissa[MANT_BYTES]; /* little endian mantissa - first - byte is not checked for - correctness. */ - unsigned short exponent; /* bias 32767 exponent */ + unsigned char mantissa[MANT_BYTES]; /* little endian mantissa - first + byte is not checked for + correctness. */ + unsigned short exponent; /* bias 32767 exponent */ unsigned char sign; unsigned char flags; @@ -65,35 +65,35 @@ typedef struct Init_Entry_s { /* Values used for normalized tests */ static Init_Entry normalized_vals[] = { - { "3.141592653589793", - {0xc6,0x0d,0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9},0x8000,0,0, - 0, {0xdb,0x0f,0x49,0x40}, - 0, {0x18,0x2d,0x44,0x54,0xfb,0x21,0x09,0x40}, - 0, {0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9,0x00,0x40} + { "3.141592653589793", + {0xc6,0x0d,0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9},0x8000,0,0, + 0, {0xdb,0x0f,0x49,0x40}, + 0, {0x18,0x2d,0x44,0x54,0xfb,0x21,0x09,0x40}, + 0, {0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9,0x00,0x40} }, - { "-3.141592653589793", - {0xc6,0x0d,0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9},0x8000,1,0, - 0, {0xdb,0x0f,0x49,0xc0}, - 0, {0x18,0x2d,0x44,0x54,0xfb,0x21,0x09,0xc0}, - 0, {0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9,0x00,0xc0} + { "-3.141592653589793", + {0xc6,0x0d,0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9},0x8000,1,0, + 0, {0xdb,0x0f,0x49,0xc0}, + 0, {0x18,0x2d,0x44,0x54,0xfb,0x21,0x09,0xc0}, + 0, {0xe9,0xbd,0x68,0x21,0xa2,0xda,0x0f,0xc9,0x00,0xc0} }, - { "1.e16", - {0x00,0x00,0x00,0x00,0x00,0x04,0xbf,0xc9,0x1b,0x8e},0x8034,0,0, - 0, {0xca,0x1b,0x0e,0x5a}, - 0, {0x00,0x80,0xe0,0x37,0x79,0xc3,0x41,0x43}, - 0, {0x00,0x00,0x00,0x04,0xbf,0xc9,0x1b,0x8e,0x34,0x40} + { "1.e16", + {0x00,0x00,0x00,0x00,0x00,0x04,0xbf,0xc9,0x1b,0x8e},0x8034,0,0, + 0, {0xca,0x1b,0x0e,0x5a}, + 0, {0x00,0x80,0xe0,0x37,0x79,0xc3,0x41,0x43}, + 0, {0x00,0x00,0x00,0x04,0xbf,0xc9,0x1b,0x8e,0x34,0x40} }, - { "1.6e-20", - {0xf6,0xd3,0xee,0x7b,0xda,0x74,0x50,0xa0,0x1d,0x97},0x7fbd,0,0, - 0, {0xa0,0x1d,0x97,0x1e}, - 0, {0x4f,0x9b,0x0e,0x0a,0xb4,0xe3,0xd2,0x3b}, - 0, {0xef,0x7b,0xda,0x74,0x50,0xa0,0x1d,0x97,0xbd,0x3f} + { "1.6e-20", + {0xf6,0xd3,0xee,0x7b,0xda,0x74,0x50,0xa0,0x1d,0x97},0x7fbd,0,0, + 0, {0xa0,0x1d,0x97,0x1e}, + 0, {0x4f,0x9b,0x0e,0x0a,0xb4,0xe3,0xd2,0x3b}, + 0, {0xef,0x7b,0xda,0x74,0x50,0xa0,0x1d,0x97,0xbd,0x3f} }, - { "-5876.", - {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xa0,0xb7},0x800b,1,0, - 0, {0x00,0xa0,0xb7,0xc5}, - 0, {0x00,0x00,0x00,0x00,0x00,0xf4,0xb6,0xc0}, - 0, {0x00,0x00,0x00,0x00,0x00,0x00,0xa0,0xb7,0x0b,0xc0} + { "-5876.", + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xa0,0xb7},0x800b,1,0, + 0, {0x00,0xa0,0xb7,0xc5}, + 0, {0x00,0x00,0x00,0x00,0x00,0xf4,0xb6,0xc0}, + 0, {0x00,0x00,0x00,0x00,0x00,0x00,0xa0,0xb7,0x0b,0xc0} }, }; @@ -102,43 +102,43 @@ static Init_Entry normalized_vals[] = { */ static Init_Entry normalized_edgecase_vals[] = { /* 32-bit edges */ - { "1.1754943508222875e-38", - {0xd5,0xf2,0x82,0xff,0xff,0xff,0xff,0xff,0xff,0xff},0x7f80,0,0, - 0, {0x00,0x00,0x80,0x00}, - 0, {0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x38}, - 0, {0x83,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x80,0x3f} + { "1.1754943508222875e-38", + {0xd5,0xf2,0x82,0xff,0xff,0xff,0xff,0xff,0xff,0xff},0x7f80,0,0, + 0, {0x00,0x00,0x80,0x00}, + 0, {0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x38}, + 0, {0x83,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x80,0x3f} }, - { "3.4028234663852886e+38", - {0x21,0x35,0x0a,0x00,0x00,0x00,0x00,0xff,0xff,0xff},0x807e,0,0, - 0, {0xff,0xff,0x7f,0x7f}, - 0, {0x00,0x00,0x00,0xe0,0xff,0xff,0xef,0x47}, - 0, {0x0a,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0x7e,0x40} + { "3.4028234663852886e+38", + {0x21,0x35,0x0a,0x00,0x00,0x00,0x00,0xff,0xff,0xff},0x807e,0,0, + 0, {0xff,0xff,0x7f,0x7f}, + 0, {0x00,0x00,0x00,0xe0,0xff,0xff,0xef,0x47}, + 0, {0x0a,0x00,0x00,0x00,0x00,0xff,0xff,0xff,0x7e,0x40} }, /* 64-bit edges */ - { "2.2250738585072014E-308", - {0x26,0x18,0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x80},0x7c01,0,0, - -1, {0x00,0x00,0x00,0x00}, - 0, {0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00}, - 0, {0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x3c} + { "2.2250738585072014E-308", + {0x26,0x18,0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x80},0x7c01,0,0, + -1, {0x00,0x00,0x00,0x00}, + 0, {0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00}, + 0, {0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x3c} }, - { "1.7976931348623157E+308", - {0x26,0x6b,0xac,0xf7,0xff,0xff,0xff,0xff,0xff,0xff},0x83fe,0,0, - 1, {0x00,0x00,0x80,0x7f}, - 0, {0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x7f}, - 0, {0xac,0xf7,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x43} + { "1.7976931348623157E+308", + {0x26,0x6b,0xac,0xf7,0xff,0xff,0xff,0xff,0xff,0xff},0x83fe,0,0, + 1, {0x00,0x00,0x80,0x7f}, + 0, {0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x7f}, + 0, {0xac,0xf7,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x43} }, /* 80-bit edges */ -/* { "3.3621E-4932", - {},,0,0, - -1, {0x00,0x00,0x00,0x00}, - -1, {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, - 0, {} +/* { "3.3621E-4932", + {},,0,0, + -1, {0x00,0x00,0x00,0x00}, + -1, {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + 0, {} }, - { "1.1897E+4932", - {},,0,0, - 1, {0x00,0x00,0x80,0x7f}, - 1, {}, - 0, {} + { "1.1897E+4932", + {},,0,0, + 1, {0x00,0x00,0x80,0x7f}, + 1, {}, + 0, {} },*/ /* internal format edges */ /* { @@ -168,26 +168,26 @@ new_check_flt(Init_Entry *val) unsigned int len; mantissa = BitVector_Block_Read(flt->mantissa, &len); - for (i=1;imantissa[i]) - result = 1; + for (i=1;imantissa[i]) + result = 1; free(mantissa); if (result) { - strcat(result_msg, "mantissa"); - return 1; + strcat(result_msg, "mantissa"); + return 1; } if (flt->exponent != val->exponent) { - strcat(result_msg, "exponent"); - return 1; + strcat(result_msg, "exponent"); + return 1; } if (flt->sign != val->sign) { - strcat(result_msg, "sign"); - return 1; + strcat(result_msg, "sign"); + return 1; } if (flt->flags != val->flags) { - strcat(result_msg, "flags"); - return 1; + strcat(result_msg, "flags"); + return 1; } return 0; } @@ -199,10 +199,10 @@ test_new_normalized(void) int i, num = sizeof(normalized_vals)/sizeof(Init_Entry); for (i=0; i0 ? 'F':'.'); fflush(stdout); if (nf > 0) - sprintf(failed, "%s ** F: %s failed!\n", failed, testname); + sprintf(failed, "%s ** F: %s failed!\n", failed, testname); return nf; } -#define runtest(x,y,z) runtest_(#x,test_##x,y,z) +#define runtest(x,y,z) runtest_(#x,test_##x,y,z) int main(void) { int nf = 0; if (BitVector_Boot() != ErrCode_Ok) - return EXIT_FAILURE; + return EXIT_FAILURE; yasm_floatnum_initialize(); failed[0] = '\0'; @@ -441,6 +441,6 @@ main(void) nf += runtest(get_extended_normalized, get_family_setup, get_family_teardown); nf += runtest(get_extended_normalized_edgecase, get_family_setup, get_family_teardown); printf(" +%d-%d/8 %d%%\n%s", - 8-nf, nf, 100*(8-nf)/8, failed); + 8-nf, nf, 100*(8-nf)/8, failed); return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/libyasm/tests/leb128_test.c b/libyasm/tests/leb128_test.c index f2e45852..85d5530c 100644 --- a/libyasm/tests/leb128_test.c +++ b/libyasm/tests/leb128_test.c @@ -91,38 +91,38 @@ run_output_test(Test_Entry *test) yasm_xfree(valstr); if (test->negate) - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); size = yasm_intnum_size_leb128(intn, test->sign); if (size != test->outsize) { - yasm_intnum_destroy(intn); - sprintf(failmsg, "%ssigned %s%s size() bad size: expected %lu, got %lu!", - test->sign?"":"un", test->negate?"-":"", test->input, - test->outsize, size); - return 1; + yasm_intnum_destroy(intn); + sprintf(failmsg, "%ssigned %s%s size() bad size: expected %lu, got %lu!", + test->sign?"":"un", test->negate?"-":"", test->input, + test->outsize, size); + return 1; } for (i=0; isign); if (size != test->outsize) { - yasm_intnum_destroy(intn); - sprintf(failmsg, "%ssigned %s%s get() bad size: expected %lu, got %lu!", - test->sign?"":"un", test->negate?"-":"", test->input, - test->outsize, size); - return 1; + yasm_intnum_destroy(intn); + sprintf(failmsg, "%ssigned %s%s get() bad size: expected %lu, got %lu!", + test->sign?"":"un", test->negate?"-":"", test->input, + test->outsize, size); + return 1; } bad = 0; for (i=0; ioutsize && !bad; i++) { - if (out[i] != test->result[i]) - bad = 1; + if (out[i] != test->result[i]) + bad = 1; } if (bad) { - yasm_intnum_destroy(intn); - sprintf(failmsg, "%ssigned %s%s get() bad output!", - test->sign?"":"un", test->negate?"-":"", test->input); - return 1; + yasm_intnum_destroy(intn); + sprintf(failmsg, "%ssigned %s%s get() bad output!", + test->sign?"":"un", test->negate?"-":"", test->input); + return 1; } yasm_intnum_destroy(intn); @@ -140,25 +140,25 @@ run_input_test(Test_Entry *test) yasm_xfree(valstr); if (test->negate) - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); testn = yasm_intnum_create_leb128(test->result, test->sign, &size); if (size != test->outsize) { - yasm_intnum_destroy(testn); - yasm_intnum_destroy(intn); - sprintf(failmsg, "%ssigned %s%s create() bad size: expected %lu, got %lu!", - test->sign?"":"un", test->negate?"-":"", test->input, - test->outsize, size); - return 1; + yasm_intnum_destroy(testn); + yasm_intnum_destroy(intn); + sprintf(failmsg, "%ssigned %s%s create() bad size: expected %lu, got %lu!", + test->sign?"":"un", test->negate?"-":"", test->input, + test->outsize, size); + return 1; } yasm_intnum_calc(intn, YASM_EXPR_EQ, testn); if (!yasm_intnum_is_pos1(intn)) { - yasm_intnum_destroy(testn); - yasm_intnum_destroy(intn); - sprintf(failmsg, "%ssigned %s%s create() bad output!", - test->sign?"":"un", test->negate?"-":"", test->input); - return 1; + yasm_intnum_destroy(testn); + yasm_intnum_destroy(intn); + sprintf(failmsg, "%ssigned %s%s create() bad output!", + test->sign?"":"un", test->negate?"-":"", test->input); + return 1; } yasm_intnum_destroy(testn); @@ -174,33 +174,33 @@ main(void) int i; if (BitVector_Boot() != ErrCode_Ok) - return EXIT_FAILURE; + return EXIT_FAILURE; yasm_intnum_initialize(); failed[0] = '\0'; printf("Test leb128_test: "); for (i=0; i0 ? 'F':'.'); - fflush(stdout); - if (fail) - sprintf(failed, "%s ** F: %s\n", failed, failmsg); - nf += fail; - - fail = run_input_test(&tests[i]); - printf("%c", fail>0 ? 'F':'.'); - fflush(stdout); - if (fail) - sprintf(failed, "%s ** F: %s\n", failed, failmsg); - nf += fail; + int fail; + + fail = run_output_test(&tests[i]); + printf("%c", fail>0 ? 'F':'.'); + fflush(stdout); + if (fail) + sprintf(failed, "%s ** F: %s\n", failed, failmsg); + nf += fail; + + fail = run_input_test(&tests[i]); + printf("%c", fail>0 ? 'F':'.'); + fflush(stdout); + if (fail) + sprintf(failed, "%s ** F: %s\n", failed, failmsg); + nf += fail; } yasm_intnum_cleanup(); printf(" +%d-%d/%d %d%%\n%s", - numtests*2-nf, nf, numtests*2, 100*(numtests*2-nf)/(numtests*2), - failed); + numtests*2-nf, nf, numtests*2, 100*(numtests*2-nf)/(numtests*2), + failed); return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/libyasm/tests/splitpath_test.c b/libyasm/tests/splitpath_test.c index c96d907a..a8888cb9 100644 --- a/libyasm/tests/splitpath_test.c +++ b/libyasm/tests/splitpath_test.c @@ -114,24 +114,24 @@ run_test(Test_Entry *test) const char *funcname; if (test->splitpath == &yasm__splitpath_unix) - funcname = "unix"; + funcname = "unix"; else - funcname = "win"; + funcname = "win"; headlen = test->splitpath(test->input, &tail); if (headlen != test->headlen) { - sprintf(failmsg, - "splitpath_%s(\"%s\") bad head len: expected %lu, got %lu!", - funcname, test->input, (unsigned long)test->headlen, - (unsigned long)headlen); - return 1; + sprintf(failmsg, + "splitpath_%s(\"%s\") bad head len: expected %lu, got %lu!", + funcname, test->input, (unsigned long)test->headlen, + (unsigned long)headlen); + return 1; } if (strcmp(tail, test->tail) != 0) { - sprintf(failmsg, - "splitpath_%s(\"%s\") bad tail: expected \"%s\", got \"%s\"!", - funcname, test->input, test->tail, tail); - return 1; + sprintf(failmsg, + "splitpath_%s(\"%s\") bad tail: expected \"%s\", got \"%s\"!", + funcname, test->input, test->tail, tail); + return 1; } return 0; @@ -147,15 +147,15 @@ main(void) failed[0] = '\0'; printf("Test splitpath_test: "); for (i=0; i0 ? 'F':'.'); - fflush(stdout); - if (fail) - sprintf(failed, "%s ** F: %s\n", failed, failmsg); - nf += fail; + int fail = run_test(&tests[i]); + printf("%c", fail>0 ? 'F':'.'); + fflush(stdout); + if (fail) + sprintf(failed, "%s ** F: %s\n", failed, failmsg); + nf += fail; } printf(" +%d-%d/%d %d%%\n%s", - numtests-nf, nf, numtests, 100*(numtests-nf)/numtests, failed); + numtests-nf, nf, numtests, 100*(numtests-nf)/numtests, failed); return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/libyasm/tests/uncstring_test.c b/libyasm/tests/uncstring_test.c index 22ca3fc3..ef88552c 100644 --- a/libyasm/tests/uncstring_test.c +++ b/libyasm/tests/uncstring_test.c @@ -57,7 +57,7 @@ typedef struct Test_Entry { static Test_Entry tests[] = { {"noescape", 8, "noescape", 8, NULL}, - {"noescape2", 10, "noescape2", 10, NULL}, /* includes trailing zero */ + {"noescape2", 10, "noescape2", 10, NULL}, /* includes trailing zero */ {"\\\\\\b\\f\\n\\r\\t\\\"", 14, "\\\b\f\n\r\t\"", 7, NULL}, {"\\a", 2, "a", 1, NULL}, /* hex tests */ @@ -97,38 +97,38 @@ run_test(Test_Entry *test) yasm_unescape_cstring((unsigned char *)str, &len); if (len != test->result_len) { - sprintf(failmsg, - "unescape_cstring(\"%s\", %lu) bad output len: expected %lu, got %lu!", - test->input, (unsigned long)test->in_len, - (unsigned long)test->result_len, (unsigned long)len); - return 1; + sprintf(failmsg, + "unescape_cstring(\"%s\", %lu) bad output len: expected %lu, got %lu!", + test->input, (unsigned long)test->in_len, + (unsigned long)test->result_len, (unsigned long)len); + return 1; } if (strncmp(str, test->result, len) != 0) { - sprintf(failmsg, - "unescape_cstring(\"%s\", %lu) bad output: expected \"%s\", got \"%s\"!", - test->input, (unsigned long)test->in_len, test->result, str); - return 1; + sprintf(failmsg, + "unescape_cstring(\"%s\", %lu) bad output: expected \"%s\", got \"%s\"!", + test->input, (unsigned long)test->in_len, test->result, str); + return 1; } yasm_warn_fetch(&wclass, &wstr); if (wstr != NULL && test->warn == NULL) { - sprintf(failmsg, - "unescape_cstring(\"%s\", %lu) unexpected warning: %s!", - test->input, (unsigned long)test->in_len, wstr); - return 1; + sprintf(failmsg, + "unescape_cstring(\"%s\", %lu) unexpected warning: %s!", + test->input, (unsigned long)test->in_len, wstr); + return 1; } if (wstr == NULL && test->warn != NULL) { - sprintf(failmsg, - "unescape_cstring(\"%s\", %lu) expected warning: %s, did not get it!", - test->input, (unsigned long)test->in_len, test->warn); - return 1; + sprintf(failmsg, + "unescape_cstring(\"%s\", %lu) expected warning: %s, did not get it!", + test->input, (unsigned long)test->in_len, test->warn); + return 1; } if (wstr && test->warn && strcmp(wstr, test->warn) != 0) { - sprintf(failmsg, - "unescape_cstring(\"%s\", %lu) expected warning: %s, got %s!", - test->input, (unsigned long)test->in_len, test->warn, wstr); - return 1; + sprintf(failmsg, + "unescape_cstring(\"%s\", %lu) expected warning: %s, got %s!", + test->input, (unsigned long)test->in_len, test->warn, wstr); + return 1; } yasm_xfree(wstr); @@ -147,16 +147,16 @@ main(void) failed[0] = '\0'; printf("Test uncstring_test: "); for (i=0; i0 ? 'F':'.'); - fflush(stdout); - if (fail) - sprintf(failed, "%s ** F: %s\n", failed, failmsg); - nf += fail; + int fail = run_test(&tests[i]); + printf("%c", fail>0 ? 'F':'.'); + fflush(stdout); + if (fail) + sprintf(failed, "%s ** F: %s\n", failed, failmsg); + nf += fail; } printf(" +%d-%d/%d %d%%\n%s", - numtests-nf, nf, numtests, 100*(numtests-nf)/numtests, failed); + numtests-nf, nf, numtests, 100*(numtests-nf)/numtests, failed); yasm_errwarn_cleanup(); return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE; diff --git a/libyasm/valparam.c b/libyasm/valparam.c index ee3acd54..8f51167f 100644 --- a/libyasm/valparam.c +++ b/libyasm/valparam.c @@ -39,26 +39,26 @@ void yasm_call_directive(const yasm_directive *directive, yasm_object *object, - yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *valparams, + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp; if ((directive->flags & (YASM_DIR_ARG_REQUIRED|YASM_DIR_ID_REQUIRED)) && - (!valparams || !yasm_vps_first(valparams))) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("directive `%s' requires an argument"), - directive->name); - return; + (!valparams || !yasm_vps_first(valparams))) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("directive `%s' requires an argument"), + directive->name); + return; } if (valparams) { - vp = yasm_vps_first(valparams); - if ((directive->flags & YASM_DIR_ID_REQUIRED) && !vp->val) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("directive `%s' requires an identifier parameter"), - directive->name); - return; - } + vp = yasm_vps_first(valparams); + if ((directive->flags & YASM_DIR_ID_REQUIRED) && !vp->val) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("directive `%s' requires an identifier parameter"), + directive->name); + return; + } } directive->handler(object, valparams, objext_valparams, line); } @@ -76,16 +76,16 @@ yasm_vp_create(/*@keep@*/ char *v, /*@keep@*/ yasm_expr *p) yasm_vp_expr(yasm_valparam *vp, yasm_symtab *symtab, unsigned long line) { if (!vp) - return NULL; + return NULL; if (vp->val) { - return yasm_expr_create_ident(yasm_expr_sym( - yasm_symtab_use(symtab, vp->val, line)), line); + return yasm_expr_create_ident(yasm_expr_sym( + yasm_symtab_use(symtab, vp->val, line)), line); } else if (vp->param) { - yasm_expr *e = vp->param; - vp->param = NULL; /* to avoid double-free */ - return e; + yasm_expr *e = vp->param; + vp->param = NULL; /* to avoid double-free */ + return e; } else - return NULL; + return NULL; } void @@ -95,13 +95,13 @@ yasm_vps_delete(yasm_valparamhead *headp) cur = STAILQ_FIRST(headp); while (cur) { - next = STAILQ_NEXT(cur, link); - if (cur->val) - yasm_xfree(cur->val); - if (cur->param) - yasm_expr_destroy(cur->param); - yasm_xfree(cur); - cur = next; + next = STAILQ_NEXT(cur, link); + if (cur->val) + yasm_xfree(cur->val); + if (cur->param) + yasm_expr_destroy(cur->param); + yasm_xfree(cur); + cur = next; } STAILQ_INIT(headp); } @@ -112,22 +112,22 @@ yasm_vps_print(const yasm_valparamhead *headp, FILE *f) const yasm_valparam *vp; if(!headp) { - fprintf(f, "(none)"); - return; + fprintf(f, "(none)"); + return; } yasm_vps_foreach(vp, headp) { - if (vp->val) - fprintf(f, "(\"%s\",", vp->val); - else - fprintf(f, "((nil),"); - if (vp->param) - yasm_expr_print(vp->param, f); - else - fprintf(f, "(nil)"); - fprintf(f, ")"); - if (yasm_vps_next(vp)) - fprintf(f, ","); + if (vp->val) + fprintf(f, "(\"%s\",", vp->val); + else + fprintf(f, "((nil),"); + if (vp->param) + yasm_expr_print(vp->param, f); + else + fprintf(f, "(nil)"); + fprintf(f, ")"); + if (yasm_vps_next(vp)) + fprintf(f, ","); } } @@ -152,7 +152,7 @@ void yasm_vps_append(yasm_valparamhead *headp, /*@keep@*/ yasm_valparam *vp) { if (vp) - STAILQ_INSERT_TAIL(headp, vp, link); + STAILQ_INSERT_TAIL(headp, vp, link); } /* Non-macro yasm_vps_first() for non-YASM_LIB_INTERNAL users. */ diff --git a/libyasm/valparam.h b/libyasm/valparam.h index 8c0dcf88..f007d0e6 100644 --- a/libyasm/valparam.h +++ b/libyasm/valparam.h @@ -38,8 +38,8 @@ /** Value/parameter pair. \internal */ struct yasm_valparam { /*@reldef@*/ STAILQ_ENTRY(yasm_valparam) link; /**< Next pair in list */ - /*@owned@*/ /*@null@*/ char *val; /**< Value */ - /*@owned@*/ /*@null@*/ yasm_expr *param; /**< Parameter */ + /*@owned@*/ /*@null@*/ char *val; /**< Value */ + /*@owned@*/ /*@null@*/ yasm_expr *param; /**< Parameter */ }; /** Linked list of value/parameter pairs. \internal */ @@ -54,42 +54,42 @@ struct yasm_directive { */ /*@null@*/ const char *name; - const char *parser; /**< Parser keyword */ + const char *parser; /**< Parser keyword */ /** Handler callback function for the directive. - * \param object object - * \param valparams value/parameters - * \param objext_valparams object format-specific value/parameters - * \param line virtual line (from yasm_linemap) + * \param object object + * \param valparams value/parameters + * \param objext_valparams object format-specific value/parameters + * \param line virtual line (from yasm_linemap) */ void (*handler) (yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line); + yasm_valparamhead *objext_valparams, unsigned long line); /** Flags for pre-handler parameter checking. */ enum yasm_directive_flags { - YASM_DIR_ANY = 0, /**< Any valparams accepted */ - YASM_DIR_ARG_REQUIRED = 1, /**< Require at least 1 valparam */ - YASM_DIR_ID_REQUIRED = 2 /**< First valparam must be ID */ + YASM_DIR_ANY = 0, /**< Any valparams accepted */ + YASM_DIR_ARG_REQUIRED = 1, /**< Require at least 1 valparam */ + YASM_DIR_ID_REQUIRED = 2 /**< First valparam must be ID */ } flags; }; /** Call a directive. Performs any valparam checks asked for by the * directive prior to call. Note that for a variety of reasons, a directive * can generate an error. - * \param directive directive - * \param object object - * \param valparams value/parameters - * \param objext_valparams object format-specific value/parameters - * \param line virtual line (from yasm_linemap) + * \param directive directive + * \param object object + * \param valparams value/parameters + * \param objext_valparams object format-specific value/parameters + * \param line virtual line (from yasm_linemap) */ void yasm_call_directive(const yasm_directive *directive, yasm_object *object, - yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, - unsigned long line); + yasm_valparamhead *valparams, + yasm_valparamhead *objext_valparams, + unsigned long line); /** Create a new valparam. - * \param v value - * \param p parameter + * \param v value + * \param p parameter * \return Newly allocated valparam. */ yasm_valparam *yasm_vp_create(/*@keep@*/ char *v, /*@keep@*/ yasm_expr *p); @@ -97,9 +97,9 @@ yasm_valparam *yasm_vp_create(/*@keep@*/ char *v, /*@keep@*/ yasm_expr *p); /** Get a valparam as an expr. If the valparam is a value, it's treated * as a symbol (yasm_symtab_use() is called to convert it). The valparam * is modified as necessary to avoid double-frees. - * \param vp valparam - * \param symtab symbol table - * \param line virtual line + * \param vp valparam + * \param symtab symbol table + * \param line virtual line * \return Expression, or NULL if vp is NULL or if val and param are both NULL. */ /*@null@*/ /*@only@*/ yasm_expr *yasm_vp_expr @@ -111,65 +111,65 @@ yasm_valparam *yasm_vp_create(/*@keep@*/ char *v, /*@keep@*/ yasm_expr *p); yasm_valparamhead *yasm_vps_create(void); /** Destroy a list of valparams (created with yasm_vps_create). - * \param headp list of valparams + * \param headp list of valparams */ void yasm_vps_destroy(yasm_valparamhead *headp); #ifdef YASM_LIB_INTERNAL /** Initialize linked list of valparams. - * \param headp linked list + * \param headp linked list */ -#define yasm_vps_initialize(headp) STAILQ_INIT(headp) +#define yasm_vps_initialize(headp) STAILQ_INIT(headp) /** Destroy (free allocated memory for) linked list of valparams (created with * yasm_vps_initialize). * \warning Deletes val/params. - * \param headp linked list + * \param headp linked list */ void yasm_vps_delete(yasm_valparamhead *headp); #endif /** Append valparam to tail of linked list. - * \param headp linked list - * \param vp valparam + * \param headp linked list + * \param vp valparam */ void yasm_vps_append(yasm_valparamhead *headp, /*@keep@*/ yasm_valparam *vp); #ifdef YASM_LIB_INTERNAL -#define yasm_vps_append(headp, vp) do { \ - if (vp) \ - STAILQ_INSERT_TAIL(headp, vp, link); \ +#define yasm_vps_append(headp, vp) do { \ + if (vp) \ + STAILQ_INSERT_TAIL(headp, vp, link); \ } while(0) #endif /** Get first valparam in linked list. - * \param headp linked list + * \param headp linked list * \return First valparam in linked list. */ /*@null@*/ /*@dependent@*/ yasm_valparam *yasm_vps_first (yasm_valparamhead *headp); #ifdef YASM_LIB_INTERNAL -#define yasm_vps_first(headp) STAILQ_FIRST(headp) +#define yasm_vps_first(headp) STAILQ_FIRST(headp) #endif /** Get next valparam in linked list. - * \param cur previous valparam in linked list + * \param cur previous valparam in linked list * \return Next valparam in linked list. */ /*@null@*/ /*@dependent@*/ yasm_valparam *yasm_vps_next(yasm_valparam *cur); #ifdef YASM_LIB_INTERNAL -#define yasm_vps_next(cur) STAILQ_NEXT(cur, link) +#define yasm_vps_next(cur) STAILQ_NEXT(cur, link) /** Iterate through linked list of valparams. * \internal - * \param iter iterator variable - * \param headp linked list + * \param iter iterator variable + * \param headp linked list */ -#define yasm_vps_foreach(iter, headp) STAILQ_FOREACH(iter, headp, link) +#define yasm_vps_foreach(iter, headp) STAILQ_FOREACH(iter, headp, link) #endif /** Print linked list of valparams. For debugging purposes. - * \param f file - * \param headp linked list + * \param f file + * \param headp linked list */ void yasm_vps_print(/*@null@*/ const yasm_valparamhead *headp, FILE *f); diff --git a/libyasm/value.c b/libyasm/value.c index 90cfeeb3..458f1b15 100644 --- a/libyasm/value.c +++ b/libyasm/value.c @@ -49,7 +49,7 @@ void yasm_value_initialize(/*@out@*/ yasm_value *value, - /*@null@*/ /*@kept@*/ yasm_expr *e, unsigned int size) + /*@null@*/ /*@kept@*/ yasm_expr *e, unsigned int size) { value->abs = e; value->rel = NULL; @@ -66,7 +66,7 @@ yasm_value_initialize(/*@out@*/ yasm_value *value, void yasm_value_init_sym(/*@out@*/ yasm_value *value, /*@null@*/ yasm_symrec *sym, - unsigned int size) + unsigned int size) { value->abs = NULL; value->rel = sym; @@ -101,14 +101,14 @@ void yasm_value_delete(yasm_value *value) { if (value->abs) - yasm_expr_destroy(value->abs); + yasm_expr_destroy(value->abs); value->abs = NULL; value->rel = NULL; } void yasm_value_set_curpos_rel(yasm_value *value, yasm_bytecode *bc, - unsigned int ip_rel) + unsigned int ip_rel) { value->curpos_rel = 1; value->ip_rel = ip_rel; @@ -117,14 +117,14 @@ yasm_value_set_curpos_rel(yasm_value *value, yasm_bytecode *bc, * to a custom absolute symbol. */ if (!value->rel) { - yasm_object *object = yasm_section_get_object(yasm_bc_get_section(bc)); - value->rel = yasm_symtab_abs_sym(object->symtab); + yasm_object *object = yasm_section_get_object(yasm_bc_get_section(bc)); + value->rel = yasm_symtab_abs_sym(object->symtab); } } static int value_finalize_scan(yasm_value *value, yasm_expr *e, - /*@null@*/ yasm_bytecode *expr_precbc, int ssym_not_ok) + /*@null@*/ yasm_bytecode *expr_precbc, int ssym_not_ok) { int i; /*@dependent@*/ yasm_section *sect; @@ -141,7 +141,7 @@ value_finalize_scan(yasm_value *value, yasm_expr *e, * This is a bitmask to keep things small, as this is a recursive * routine and we don't want to eat up stack space. */ - unsigned long used; /* for ADD */ + unsigned long used; /* for ADD */ /* Thanks to this running after a simplify, we don't need to iterate * down through IDENTs or handle SUB. @@ -155,294 +155,294 @@ value_finalize_scan(yasm_value *value, yasm_expr *e, * symrec with it. */ switch (e->op) { - case YASM_EXPR_ADD: - /* Okay for single symrec anywhere in expr. - * Check for single symrec anywhere. - * Handle symrec-symrec by checking for (-1*symrec) - * and symrec term pairs (where both symrecs are in the same - * segment). - */ - if (e->numterms > 32) - yasm__fatal(N_("expression on line %d has too many add terms;" - " internal limit of 32"), e->line); - - used = 0; - - for (i=0; inumterms; i++) { - int j; - yasm_expr *sube; - yasm_intnum *intn; - yasm_symrec *sym; - /*@dependent@*/ yasm_section *sect2; - /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc2; - - /* First look for an (-1*symrec) term */ - if (e->terms[i].type != YASM_EXPR_EXPR) - continue; - sube = e->terms[i].data.expn; - - if (sube->op != YASM_EXPR_MUL || sube->numterms != 2) { - /* recurse instead */ - if (value_finalize_scan(value, sube, expr_precbc, - ssym_not_ok)) - return 1; - continue; - } - - if (sube->terms[0].type == YASM_EXPR_INT && - sube->terms[1].type == YASM_EXPR_SYM) { - intn = sube->terms[0].data.intn; - sym = sube->terms[1].data.sym; - } else if (sube->terms[0].type == YASM_EXPR_SYM && - sube->terms[1].type == YASM_EXPR_INT) { - sym = sube->terms[0].data.sym; - intn = sube->terms[1].data.intn; - } else { - if (value_finalize_scan(value, sube, expr_precbc, - ssym_not_ok)) - return 1; - continue; - } - - if (!yasm_intnum_is_neg1(intn)) { - if (value_finalize_scan(value, sube, expr_precbc, - ssym_not_ok)) - return 1; - continue; - } - - if (!yasm_symrec_get_label(sym, &precbc)) { - if (value_finalize_scan(value, sube, expr_precbc, - ssym_not_ok)) - return 1; - continue; - } - sect2 = yasm_bc_get_section(precbc); - - /* Now look for a unused symrec term in the same segment */ - for (j=0; jnumterms; j++) { - if (e->terms[j].type == YASM_EXPR_SYM - && yasm_symrec_get_label(e->terms[j].data.sym, - &precbc2) - && (sect = yasm_bc_get_section(precbc2)) - && sect == sect2 - && (used & (1<numterms && !value->curpos_rel - && (yasm_symrec_is_curpos(sym) - || (expr_precbc - && sect2 == yasm_bc_get_section(expr_precbc)))) { - for (j=0; jnumterms; j++) { - if (e->terms[j].type == YASM_EXPR_SYM - && yasm_symrec_get_label(e->terms[j].data.sym, - &precbc2) - && (used & (1<rel || ssym_not_ok) - return 1; - value->rel = e->terms[j].data.sym; - value->curpos_rel = 1; - if (yasm_symrec_is_curpos(sym)) { - /* Replace both symrec portions with 0 */ - yasm_expr_destroy(sube); - e->terms[i].type = YASM_EXPR_INT; - e->terms[i].data.intn = - yasm_intnum_create_uint(0); - e->terms[j].type = YASM_EXPR_INT; - e->terms[j].data.intn = - yasm_intnum_create_uint(0); - } else { - /* Replace positive portion with curpos */ - yasm_object *object = - yasm_section_get_object(sect2); - yasm_symtab *symtab = object->symtab; - e->terms[j].data.sym = - yasm_symtab_define_curpos - (symtab, ".", expr_precbc, e->line); - } - break; /* stop looking */ - } - } - } - - - if (j == e->numterms) - return 1; /* We didn't find a match! */ - } - - /* Look for unmatched symrecs. If we've already found one or - * we don't WANT to find one, error out. - */ - for (i=0; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_SYM - && (used & (1<rel || ssym_not_ok) - return 1; - value->rel = e->terms[i].data.sym; - /* and replace with 0 */ - e->terms[i].type = YASM_EXPR_INT; - e->terms[i].data.intn = yasm_intnum_create_uint(0); - } - } - break; - case YASM_EXPR_SHR: - /* Okay for single symrec in LHS and constant on RHS. - * Single symrecs are not okay on RHS. - * If RHS is non-constant, don't allow single symrec on LHS. - * XXX: should rshift be an expr instead?? - */ - - /* Check for not allowed cases on RHS */ - switch (e->terms[1].type) { - case YASM_EXPR_REG: - case YASM_EXPR_FLOAT: - return 1; /* not legal */ - case YASM_EXPR_SYM: - return 1; - case YASM_EXPR_EXPR: - if (value_finalize_scan(value, e->terms[1].data.expn, - expr_precbc, 1)) - return 1; - break; - default: - break; - } - - /* Check for single sym and allowed cases on LHS */ - switch (e->terms[0].type) { - /*case YASM_EXPR_REG: ????? should this be illegal ????? */ - case YASM_EXPR_FLOAT: - return 1; /* not legal */ - case YASM_EXPR_SYM: - if (value->rel || ssym_not_ok) - return 1; - value->rel = e->terms[0].data.sym; - /* and replace with 0 */ - e->terms[0].type = YASM_EXPR_INT; - e->terms[0].data.intn = yasm_intnum_create_uint(0); - break; - case YASM_EXPR_EXPR: - /* recurse */ - if (value_finalize_scan(value, e->terms[0].data.expn, - expr_precbc, ssym_not_ok)) - return 1; - break; - default: - break; /* ignore */ - } - - /* Handle RHS */ - if (!value->rel) - break; /* no handling needed */ - if (e->terms[1].type != YASM_EXPR_INT) - return 1; /* can't shift sym by non-constant integer */ - shamt = yasm_intnum_get_uint(e->terms[1].data.intn); - if ((shamt + value->rshift) > YASM_VALUE_RSHIFT_MAX) - return 1; /* total shift would be too large */ - value->rshift += shamt; - - /* Just leave SHR in place */ - break; - case YASM_EXPR_SEG: - /* Okay for single symrec (can only be done once). - * Not okay for anything BUT a single symrec as an immediate - * child. - */ - if (e->terms[0].type != YASM_EXPR_SYM) - return 1; - - if (value->seg_of) - return 1; /* multiple SEG not legal */ - value->seg_of = 1; - - if (value->rel || ssym_not_ok) - return 1; /* got a relative portion somewhere else? */ - value->rel = e->terms[0].data.sym; - - /* replace with ident'ed 0 */ - e->op = YASM_EXPR_IDENT; - e->terms[0].type = YASM_EXPR_INT; - e->terms[0].data.intn = yasm_intnum_create_uint(0); - break; - case YASM_EXPR_WRT: - /* Okay for single symrec in LHS and either a register or single - * symrec (as an immediate child) on RHS. - * If a single symrec on RHS, can only be done once. - * WRT reg is left in expr for arch to look at. - */ - - /* Handle RHS */ - switch (e->terms[1].type) { - case YASM_EXPR_SYM: - if (value->wrt) - return 1; - value->wrt = e->terms[1].data.sym; - /* and drop the WRT portion */ - e->op = YASM_EXPR_IDENT; - e->numterms = 1; - break; - case YASM_EXPR_REG: - break; /* ignore */ - default: - return 1; - } - - /* Handle LHS */ - switch (e->terms[0].type) { - case YASM_EXPR_SYM: - if (value->rel || ssym_not_ok) - return 1; - value->rel = e->terms[0].data.sym; - /* and replace with 0 */ - e->terms[0].type = YASM_EXPR_INT; - e->terms[0].data.intn = yasm_intnum_create_uint(0); - break; - case YASM_EXPR_EXPR: - /* recurse */ - return value_finalize_scan(value, e->terms[0].data.expn, - expr_precbc, ssym_not_ok); - default: - break; /* ignore */ - } - - break; - default: - /* Single symrec not allowed anywhere */ - for (i=0; inumterms; i++) { - switch (e->terms[i].type) { - case YASM_EXPR_SYM: - return 1; - case YASM_EXPR_EXPR: - /* recurse */ - return value_finalize_scan(value, - e->terms[i].data.expn, - expr_precbc, 1); - default: - break; - } - } - break; + case YASM_EXPR_ADD: + /* Okay for single symrec anywhere in expr. + * Check for single symrec anywhere. + * Handle symrec-symrec by checking for (-1*symrec) + * and symrec term pairs (where both symrecs are in the same + * segment). + */ + if (e->numterms > 32) + yasm__fatal(N_("expression on line %d has too many add terms;" + " internal limit of 32"), e->line); + + used = 0; + + for (i=0; inumterms; i++) { + int j; + yasm_expr *sube; + yasm_intnum *intn; + yasm_symrec *sym; + /*@dependent@*/ yasm_section *sect2; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc2; + + /* First look for an (-1*symrec) term */ + if (e->terms[i].type != YASM_EXPR_EXPR) + continue; + sube = e->terms[i].data.expn; + + if (sube->op != YASM_EXPR_MUL || sube->numterms != 2) { + /* recurse instead */ + if (value_finalize_scan(value, sube, expr_precbc, + ssym_not_ok)) + return 1; + continue; + } + + if (sube->terms[0].type == YASM_EXPR_INT && + sube->terms[1].type == YASM_EXPR_SYM) { + intn = sube->terms[0].data.intn; + sym = sube->terms[1].data.sym; + } else if (sube->terms[0].type == YASM_EXPR_SYM && + sube->terms[1].type == YASM_EXPR_INT) { + sym = sube->terms[0].data.sym; + intn = sube->terms[1].data.intn; + } else { + if (value_finalize_scan(value, sube, expr_precbc, + ssym_not_ok)) + return 1; + continue; + } + + if (!yasm_intnum_is_neg1(intn)) { + if (value_finalize_scan(value, sube, expr_precbc, + ssym_not_ok)) + return 1; + continue; + } + + if (!yasm_symrec_get_label(sym, &precbc)) { + if (value_finalize_scan(value, sube, expr_precbc, + ssym_not_ok)) + return 1; + continue; + } + sect2 = yasm_bc_get_section(precbc); + + /* Now look for a unused symrec term in the same segment */ + for (j=0; jnumterms; j++) { + if (e->terms[j].type == YASM_EXPR_SYM + && yasm_symrec_get_label(e->terms[j].data.sym, + &precbc2) + && (sect = yasm_bc_get_section(precbc2)) + && sect == sect2 + && (used & (1<numterms && !value->curpos_rel + && (yasm_symrec_is_curpos(sym) + || (expr_precbc + && sect2 == yasm_bc_get_section(expr_precbc)))) { + for (j=0; jnumterms; j++) { + if (e->terms[j].type == YASM_EXPR_SYM + && yasm_symrec_get_label(e->terms[j].data.sym, + &precbc2) + && (used & (1<rel || ssym_not_ok) + return 1; + value->rel = e->terms[j].data.sym; + value->curpos_rel = 1; + if (yasm_symrec_is_curpos(sym)) { + /* Replace both symrec portions with 0 */ + yasm_expr_destroy(sube); + e->terms[i].type = YASM_EXPR_INT; + e->terms[i].data.intn = + yasm_intnum_create_uint(0); + e->terms[j].type = YASM_EXPR_INT; + e->terms[j].data.intn = + yasm_intnum_create_uint(0); + } else { + /* Replace positive portion with curpos */ + yasm_object *object = + yasm_section_get_object(sect2); + yasm_symtab *symtab = object->symtab; + e->terms[j].data.sym = + yasm_symtab_define_curpos + (symtab, ".", expr_precbc, e->line); + } + break; /* stop looking */ + } + } + } + + + if (j == e->numterms) + return 1; /* We didn't find a match! */ + } + + /* Look for unmatched symrecs. If we've already found one or + * we don't WANT to find one, error out. + */ + for (i=0; inumterms; i++) { + if (e->terms[i].type == YASM_EXPR_SYM + && (used & (1<rel || ssym_not_ok) + return 1; + value->rel = e->terms[i].data.sym; + /* and replace with 0 */ + e->terms[i].type = YASM_EXPR_INT; + e->terms[i].data.intn = yasm_intnum_create_uint(0); + } + } + break; + case YASM_EXPR_SHR: + /* Okay for single symrec in LHS and constant on RHS. + * Single symrecs are not okay on RHS. + * If RHS is non-constant, don't allow single symrec on LHS. + * XXX: should rshift be an expr instead?? + */ + + /* Check for not allowed cases on RHS */ + switch (e->terms[1].type) { + case YASM_EXPR_REG: + case YASM_EXPR_FLOAT: + return 1; /* not legal */ + case YASM_EXPR_SYM: + return 1; + case YASM_EXPR_EXPR: + if (value_finalize_scan(value, e->terms[1].data.expn, + expr_precbc, 1)) + return 1; + break; + default: + break; + } + + /* Check for single sym and allowed cases on LHS */ + switch (e->terms[0].type) { + /*case YASM_EXPR_REG: ????? should this be illegal ????? */ + case YASM_EXPR_FLOAT: + return 1; /* not legal */ + case YASM_EXPR_SYM: + if (value->rel || ssym_not_ok) + return 1; + value->rel = e->terms[0].data.sym; + /* and replace with 0 */ + e->terms[0].type = YASM_EXPR_INT; + e->terms[0].data.intn = yasm_intnum_create_uint(0); + break; + case YASM_EXPR_EXPR: + /* recurse */ + if (value_finalize_scan(value, e->terms[0].data.expn, + expr_precbc, ssym_not_ok)) + return 1; + break; + default: + break; /* ignore */ + } + + /* Handle RHS */ + if (!value->rel) + break; /* no handling needed */ + if (e->terms[1].type != YASM_EXPR_INT) + return 1; /* can't shift sym by non-constant integer */ + shamt = yasm_intnum_get_uint(e->terms[1].data.intn); + if ((shamt + value->rshift) > YASM_VALUE_RSHIFT_MAX) + return 1; /* total shift would be too large */ + value->rshift += shamt; + + /* Just leave SHR in place */ + break; + case YASM_EXPR_SEG: + /* Okay for single symrec (can only be done once). + * Not okay for anything BUT a single symrec as an immediate + * child. + */ + if (e->terms[0].type != YASM_EXPR_SYM) + return 1; + + if (value->seg_of) + return 1; /* multiple SEG not legal */ + value->seg_of = 1; + + if (value->rel || ssym_not_ok) + return 1; /* got a relative portion somewhere else? */ + value->rel = e->terms[0].data.sym; + + /* replace with ident'ed 0 */ + e->op = YASM_EXPR_IDENT; + e->terms[0].type = YASM_EXPR_INT; + e->terms[0].data.intn = yasm_intnum_create_uint(0); + break; + case YASM_EXPR_WRT: + /* Okay for single symrec in LHS and either a register or single + * symrec (as an immediate child) on RHS. + * If a single symrec on RHS, can only be done once. + * WRT reg is left in expr for arch to look at. + */ + + /* Handle RHS */ + switch (e->terms[1].type) { + case YASM_EXPR_SYM: + if (value->wrt) + return 1; + value->wrt = e->terms[1].data.sym; + /* and drop the WRT portion */ + e->op = YASM_EXPR_IDENT; + e->numterms = 1; + break; + case YASM_EXPR_REG: + break; /* ignore */ + default: + return 1; + } + + /* Handle LHS */ + switch (e->terms[0].type) { + case YASM_EXPR_SYM: + if (value->rel || ssym_not_ok) + return 1; + value->rel = e->terms[0].data.sym; + /* and replace with 0 */ + e->terms[0].type = YASM_EXPR_INT; + e->terms[0].data.intn = yasm_intnum_create_uint(0); + break; + case YASM_EXPR_EXPR: + /* recurse */ + return value_finalize_scan(value, e->terms[0].data.expn, + expr_precbc, ssym_not_ok); + default: + break; /* ignore */ + } + + break; + default: + /* Single symrec not allowed anywhere */ + for (i=0; inumterms; i++) { + switch (e->terms[i].type) { + case YASM_EXPR_SYM: + return 1; + case YASM_EXPR_EXPR: + /* recurse */ + return value_finalize_scan(value, + e->terms[i].data.expn, + expr_precbc, 1); + default: + break; + } + } + break; } return 0; @@ -450,63 +450,63 @@ value_finalize_scan(yasm_value *value, yasm_expr *e, int yasm_value_finalize_expr(yasm_value *value, yasm_expr *e, - yasm_bytecode *precbc, unsigned int size) + yasm_bytecode *precbc, unsigned int size) { if (!e) { - yasm_value_initialize(value, NULL, size); - return 0; + yasm_value_initialize(value, NULL, size); + return 0; } yasm_value_initialize(value, - yasm_expr__level_tree(e, 1, 1, 0, 0, NULL, NULL), - size); + yasm_expr__level_tree(e, 1, 1, 0, 0, NULL, NULL), + size); /* quit early if there was an issue in simplify() */ if (yasm_error_occurred()) - return 1; + return 1; /* Handle trivial (IDENT) cases immediately */ if (value->abs->op == YASM_EXPR_IDENT) { - switch (value->abs->terms[0].type) { - case YASM_EXPR_INT: - if (yasm_intnum_is_zero(value->abs->terms[0].data.intn)) { - yasm_expr_destroy(value->abs); - value->abs = NULL; - } - return 0; - case YASM_EXPR_REG: - case YASM_EXPR_FLOAT: - return 0; - case YASM_EXPR_SYM: - value->rel = value->abs->terms[0].data.sym; - yasm_expr_destroy(value->abs); - value->abs = NULL; - return 0; - case YASM_EXPR_EXPR: - /* Bring up lower values. */ - while (value->abs->op == YASM_EXPR_IDENT - && value->abs->terms[0].type == YASM_EXPR_EXPR) { - yasm_expr *sube = value->abs->terms[0].data.expn; - yasm_xfree(value->abs); - value->abs = sube; - } - break; - default: - yasm_internal_error(N_("unexpected expr term type")); - } + switch (value->abs->terms[0].type) { + case YASM_EXPR_INT: + if (yasm_intnum_is_zero(value->abs->terms[0].data.intn)) { + yasm_expr_destroy(value->abs); + value->abs = NULL; + } + return 0; + case YASM_EXPR_REG: + case YASM_EXPR_FLOAT: + return 0; + case YASM_EXPR_SYM: + value->rel = value->abs->terms[0].data.sym; + yasm_expr_destroy(value->abs); + value->abs = NULL; + return 0; + case YASM_EXPR_EXPR: + /* Bring up lower values. */ + while (value->abs->op == YASM_EXPR_IDENT + && value->abs->terms[0].type == YASM_EXPR_EXPR) { + yasm_expr *sube = value->abs->terms[0].data.expn; + yasm_xfree(value->abs); + value->abs = sube; + } + break; + default: + yasm_internal_error(N_("unexpected expr term type")); + } } if (value_finalize_scan(value, value->abs, precbc, 0)) - return 1; + return 1; value->abs = yasm_expr__level_tree(value->abs, 1, 1, 0, 0, NULL, NULL); /* Simplify 0 in abs to NULL */ if (value->abs->op == YASM_EXPR_IDENT - && value->abs->terms[0].type == YASM_EXPR_INT - && yasm_intnum_is_zero(value->abs->terms[0].data.intn)) { - yasm_expr_destroy(value->abs); - value->abs = NULL; + && value->abs->terms[0].type == YASM_EXPR_INT + && yasm_intnum_is_zero(value->abs->terms[0].data.intn)) { + yasm_expr_destroy(value->abs); + value->abs = NULL; } return 0; } @@ -526,56 +526,56 @@ yasm_value_get_intnum(yasm_value *value, yasm_bytecode *bc, int calc_bc_dist) int sym_local; if (value->abs) { - /* Handle integer expressions, if non-integer or too complex, return - * NULL. - */ - intn = yasm_expr_get_intnum(&value->abs, calc_bc_dist); - if (!intn) - return NULL; + /* Handle integer expressions, if non-integer or too complex, return + * NULL. + */ + intn = yasm_expr_get_intnum(&value->abs, calc_bc_dist); + if (!intn) + return NULL; } if (value->rel) { - /* If relative portion is not in bc section, return NULL. - * Otherwise get the relative portion's offset. - */ - /*@dependent@*/ yasm_bytecode *rel_prevbc; - unsigned long dist; - - if (!bc) - return NULL; /* Can't calculate relative value */ - - sym_local = yasm_symrec_get_label(value->rel, &rel_prevbc); - if (value->wrt || value->seg_of || value->section_rel || !sym_local) - return NULL; /* we can't handle SEG, WRT, or external symbols */ - if (rel_prevbc->section != bc->section) - return NULL; /* not in this section */ - if (!value->curpos_rel) - return NULL; /* not PC-relative */ - - /* Calculate value relative to current assembly position */ - dist = yasm_bc_next_offset(rel_prevbc); - if (dist < bc->offset) { - outval = yasm_intnum_create_uint(bc->offset - dist); - yasm_intnum_calc(outval, YASM_EXPR_NEG, NULL); - } else { - dist -= bc->offset; - outval = yasm_intnum_create_uint(dist); - } - - if (value->rshift > 0) { - /*@only@*/ yasm_intnum *shamt = - yasm_intnum_create_uint((unsigned long)value->rshift); - yasm_intnum_calc(outval, YASM_EXPR_SHR, shamt); - yasm_intnum_destroy(shamt); - } - /* Add in absolute portion */ - if (intn) - yasm_intnum_calc(outval, YASM_EXPR_ADD, intn); - return outval; + /* If relative portion is not in bc section, return NULL. + * Otherwise get the relative portion's offset. + */ + /*@dependent@*/ yasm_bytecode *rel_prevbc; + unsigned long dist; + + if (!bc) + return NULL; /* Can't calculate relative value */ + + sym_local = yasm_symrec_get_label(value->rel, &rel_prevbc); + if (value->wrt || value->seg_of || value->section_rel || !sym_local) + return NULL; /* we can't handle SEG, WRT, or external symbols */ + if (rel_prevbc->section != bc->section) + return NULL; /* not in this section */ + if (!value->curpos_rel) + return NULL; /* not PC-relative */ + + /* Calculate value relative to current assembly position */ + dist = yasm_bc_next_offset(rel_prevbc); + if (dist < bc->offset) { + outval = yasm_intnum_create_uint(bc->offset - dist); + yasm_intnum_calc(outval, YASM_EXPR_NEG, NULL); + } else { + dist -= bc->offset; + outval = yasm_intnum_create_uint(dist); + } + + if (value->rshift > 0) { + /*@only@*/ yasm_intnum *shamt = + yasm_intnum_create_uint((unsigned long)value->rshift); + yasm_intnum_calc(outval, YASM_EXPR_SHR, shamt); + yasm_intnum_destroy(shamt); + } + /* Add in absolute portion */ + if (intn) + yasm_intnum_calc(outval, YASM_EXPR_ADD, intn); + return outval; } if (intn) - return yasm_intnum_copy(intn); + return yasm_intnum_copy(intn); /* No absolute or relative portions: output 0 */ return yasm_intnum_create_uint(0); @@ -583,8 +583,8 @@ yasm_value_get_intnum(yasm_value *value, yasm_bytecode *bc, int calc_bc_dist) int yasm_value_output_basic(yasm_value *value, /*@out@*/ unsigned char *buf, - size_t destsize, yasm_bytecode *bc, int warn, - yasm_arch *arch) + size_t destsize, yasm_bytecode *bc, int warn, + yasm_arch *arch) { /*@dependent@*/ /*@null@*/ yasm_intnum *intn = NULL; /*@only@*/ yasm_intnum *outval; @@ -593,107 +593,107 @@ yasm_value_output_basic(yasm_value *value, /*@out@*/ unsigned char *buf, unsigned int valsize = value->size; if (value->abs) { - /* Handle floating point expressions */ - if (!value->rel && value->abs->op == YASM_EXPR_IDENT - && value->abs->terms[0].type == YASM_EXPR_FLOAT) { - if (yasm_arch_floatnum_tobytes(arch, value->abs->terms[0].data.flt, - buf, destsize, valsize, 0, warn)) - return -1; - else - return 1; - } - - /* Check for complex float expressions */ - if (yasm_expr__contains(value->abs, YASM_EXPR_FLOAT)) { - yasm_error_set(YASM_ERROR_FLOATING_POINT, - N_("floating point expression too complex")); - return -1; - } - - /* Handle normal integer expressions */ - intn = yasm_expr_get_intnum(&value->abs, 1); - - if (!intn) { - /* Second try before erroring: yasm_expr_get_intnum doesn't handle - * SEG:OFF, so try simplifying out any to just the OFF portion, - * then getting the intnum again. - */ - yasm_expr *seg = yasm_expr_extract_deep_segoff(&value->abs); - if (seg) - yasm_expr_destroy(seg); - intn = yasm_expr_get_intnum(&value->abs, 1); - } - - if (!intn) { - /* Still don't have an integer! */ - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("expression too complex")); - return -1; - } + /* Handle floating point expressions */ + if (!value->rel && value->abs->op == YASM_EXPR_IDENT + && value->abs->terms[0].type == YASM_EXPR_FLOAT) { + if (yasm_arch_floatnum_tobytes(arch, value->abs->terms[0].data.flt, + buf, destsize, valsize, 0, warn)) + return -1; + else + return 1; + } + + /* Check for complex float expressions */ + if (yasm_expr__contains(value->abs, YASM_EXPR_FLOAT)) { + yasm_error_set(YASM_ERROR_FLOATING_POINT, + N_("floating point expression too complex")); + return -1; + } + + /* Handle normal integer expressions */ + intn = yasm_expr_get_intnum(&value->abs, 1); + + if (!intn) { + /* Second try before erroring: yasm_expr_get_intnum doesn't handle + * SEG:OFF, so try simplifying out any to just the OFF portion, + * then getting the intnum again. + */ + yasm_expr *seg = yasm_expr_extract_deep_segoff(&value->abs); + if (seg) + yasm_expr_destroy(seg); + intn = yasm_expr_get_intnum(&value->abs, 1); + } + + if (!intn) { + /* Still don't have an integer! */ + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("expression too complex")); + return -1; + } } /* Adjust warn for signed/unsigned integer warnings */ if (warn != 0) - warn = value->sign ? -1 : 1; + warn = value->sign ? -1 : 1; if (value->rel) { - /* If relative portion is not in bc section, don't try to handle it - * here. Otherwise get the relative portion's offset. - */ - /*@dependent@*/ yasm_bytecode *rel_prevbc; - unsigned long dist; - - sym_local = yasm_symrec_get_label(value->rel, &rel_prevbc); - if (value->wrt || value->seg_of || value->section_rel || !sym_local) - return 0; /* we can't handle SEG, WRT, or external symbols */ - if (rel_prevbc->section != bc->section) - return 0; /* not in this section */ - if (!value->curpos_rel) - return 0; /* not PC-relative */ - - /* Calculate value relative to current assembly position */ - dist = yasm_bc_next_offset(rel_prevbc); - if (dist < bc->offset) { - outval = yasm_intnum_create_uint(bc->offset - dist); - yasm_intnum_calc(outval, YASM_EXPR_NEG, NULL); - } else { - dist -= bc->offset; - outval = yasm_intnum_create_uint(dist); - } - - if (value->rshift > 0) { - /*@only@*/ yasm_intnum *shamt = - yasm_intnum_create_uint((unsigned long)value->rshift); - yasm_intnum_calc(outval, YASM_EXPR_SHR, shamt); - yasm_intnum_destroy(shamt); - } - /* Add in absolute portion */ - if (intn) - yasm_intnum_calc(outval, YASM_EXPR_ADD, intn); - /* Output! */ - if (yasm_arch_intnum_tobytes(arch, outval, buf, destsize, valsize, 0, - bc, warn)) - retval = -1; - yasm_intnum_destroy(outval); - return retval; + /* If relative portion is not in bc section, don't try to handle it + * here. Otherwise get the relative portion's offset. + */ + /*@dependent@*/ yasm_bytecode *rel_prevbc; + unsigned long dist; + + sym_local = yasm_symrec_get_label(value->rel, &rel_prevbc); + if (value->wrt || value->seg_of || value->section_rel || !sym_local) + return 0; /* we can't handle SEG, WRT, or external symbols */ + if (rel_prevbc->section != bc->section) + return 0; /* not in this section */ + if (!value->curpos_rel) + return 0; /* not PC-relative */ + + /* Calculate value relative to current assembly position */ + dist = yasm_bc_next_offset(rel_prevbc); + if (dist < bc->offset) { + outval = yasm_intnum_create_uint(bc->offset - dist); + yasm_intnum_calc(outval, YASM_EXPR_NEG, NULL); + } else { + dist -= bc->offset; + outval = yasm_intnum_create_uint(dist); + } + + if (value->rshift > 0) { + /*@only@*/ yasm_intnum *shamt = + yasm_intnum_create_uint((unsigned long)value->rshift); + yasm_intnum_calc(outval, YASM_EXPR_SHR, shamt); + yasm_intnum_destroy(shamt); + } + /* Add in absolute portion */ + if (intn) + yasm_intnum_calc(outval, YASM_EXPR_ADD, intn); + /* Output! */ + if (yasm_arch_intnum_tobytes(arch, outval, buf, destsize, valsize, 0, + bc, warn)) + retval = -1; + yasm_intnum_destroy(outval); + return retval; } if (value->seg_of || value->rshift || value->curpos_rel || value->ip_rel - || value->section_rel) - return 0; /* We can't handle this with just an absolute */ + || value->section_rel) + return 0; /* We can't handle this with just an absolute */ if (intn) { - /* Output just absolute portion */ - if (yasm_arch_intnum_tobytes(arch, intn, buf, destsize, valsize, 0, bc, - warn)) - retval = -1; + /* Output just absolute portion */ + if (yasm_arch_intnum_tobytes(arch, intn, buf, destsize, valsize, 0, bc, + warn)) + retval = -1; } else { - /* No absolute or relative portions: output 0 */ - outval = yasm_intnum_create_uint(0); - if (yasm_arch_intnum_tobytes(arch, outval, buf, destsize, valsize, 0, - bc, warn)) - retval = -1; - yasm_intnum_destroy(outval); + /* No absolute or relative portions: output 0 */ + outval = yasm_intnum_create_uint(0); + if (yasm_arch_intnum_tobytes(arch, outval, buf, destsize, valsize, 0, + bc, warn)) + retval = -1; + yasm_intnum_destroy(outval); } return retval; } @@ -702,28 +702,28 @@ void yasm_value_print(const yasm_value *value, FILE *f, int indent_level) { fprintf(f, "%*s%u-bit, %ssigned", indent_level, "", value->size, - value->sign ? "" : "un"); + value->sign ? "" : "un"); fprintf(f, "%*sAbsolute portion=", indent_level, ""); yasm_expr_print(value->abs, f); fprintf(f, "\n"); if (value->rel) { - fprintf(f, "%*sRelative to=%s%s\n", indent_level, "", - value->seg_of ? "SEG " : "", - yasm_symrec_get_name(value->rel)); - if (value->wrt) - fprintf(f, "%*s(With respect to=%s)\n", indent_level, "", - yasm_symrec_get_name(value->wrt)); - if (value->rshift > 0) - fprintf(f, "%*s(Right shifted by=%u)\n", indent_level, "", - value->rshift); - if (value->curpos_rel) - fprintf(f, "%*s(Relative to current position)\n", indent_level, - ""); - if (value->ip_rel) - fprintf(f, "%*s(IP-relative)\n", indent_level, ""); - if (value->jump_target) - fprintf(f, "%*s(Jump target)\n", indent_level, ""); - if (value->section_rel) - fprintf(f, "%*s(Section-relative)\n", indent_level, ""); + fprintf(f, "%*sRelative to=%s%s\n", indent_level, "", + value->seg_of ? "SEG " : "", + yasm_symrec_get_name(value->rel)); + if (value->wrt) + fprintf(f, "%*s(With respect to=%s)\n", indent_level, "", + yasm_symrec_get_name(value->wrt)); + if (value->rshift > 0) + fprintf(f, "%*s(Right shifted by=%u)\n", indent_level, "", + value->rshift); + if (value->curpos_rel) + fprintf(f, "%*s(Relative to current position)\n", indent_level, + ""); + if (value->ip_rel) + fprintf(f, "%*s(IP-relative)\n", indent_level, ""); + if (value->jump_target) + fprintf(f, "%*s(Jump target)\n", indent_level, ""); + if (value->section_rel) + fprintf(f, "%*s(Section-relative)\n", indent_level, ""); } } diff --git a/libyasm/value.h b/libyasm/value.h index 043c4651..cb338617 100644 --- a/libyasm/value.h +++ b/libyasm/value.h @@ -41,52 +41,52 @@ * parsing simply to ensure all fields of the value are initialized; after * the parse is complete, yasm_value_extract() should be called to finalize * the value. The value defaults to unsigned. - * \param value value to be initialized - * \param e expression (kept) - * \param size value size (in bits) + * \param value value to be initialized + * \param e expression (kept) + * \param size value size (in bits) */ void yasm_value_initialize(/*@out@*/ yasm_value *value, - /*@null@*/ /*@kept@*/ yasm_expr *e, - unsigned int size); + /*@null@*/ /*@kept@*/ yasm_expr *e, + unsigned int size); /** Initialize a #yasm_value with just a symrec. No processing is performed, * the symrec is simply stuck into value.rel and the other fields are * initialized. - * \param value value to be initialized - * \param sym symrec - * \param size value size (in bits) + * \param value value to be initialized + * \param sym symrec + * \param size value size (in bits) */ void yasm_value_init_sym(/*@out@*/ yasm_value *value, - /*@null@*/ yasm_symrec *sym, unsigned int size); + /*@null@*/ yasm_symrec *sym, unsigned int size); /** Initialize a #yasm_value as a copy of another yasm_value. Any expressions * within orig are copied, so it's safe to delete the copy. - * \param value value (copy to create) - * \param orig original value + * \param value value (copy to create) + * \param orig original value */ void yasm_value_init_copy(yasm_value *value, const yasm_value *orig); /** Frees any memory inside value; does not free value itself. - * \param value value + * \param value value */ void yasm_value_delete(yasm_value *value); /** Set a value to be relative to the current assembly position rather than * relative to the section start. - * \param value value - * \param bc bytecode containing value + * \param value value + * \param bc bytecode containing value * \param ip_rel if nonzero, indicates IP-relative data relocation, - * sometimes used to generate special relocations + * sometimes used to generate special relocations * \note If value is just an absolute value, will get an absolute symrec to * reference to (via bc's symbol table). */ void yasm_value_set_curpos_rel(yasm_value *value, yasm_bytecode *bc, - unsigned int ip_rel); + unsigned int ip_rel); /** Perform yasm_value_finalize_expr() on a value that already exists from * being initialized with yasm_value_initialize(). - * \param value value - * \param precbc previous bytecode to bytecode containing value + * \param value value + * \param precbc previous bytecode to bytecode containing value * \return Nonzero if value could not be split. */ int yasm_value_finalize(yasm_value *value, /*@null@*/ yasm_bytecode *precbc); @@ -98,10 +98,10 @@ int yasm_value_finalize(yasm_value *value, /*@null@*/ yasm_bytecode *precbc); * and yasm_value_finalize(). First expands references to symrecs in * absolute sections by expanding with the absolute section start plus the * symrec offset within the absolute section. - * \param value value to store split portions into - * \param e expression input - * \param precbc previous bytecode to bytecode containing expression - * \param size value size (in bits) + * \param value value to store split portions into + * \param e expression input + * \param precbc previous bytecode to bytecode containing expression + * \param size value size (in bits) * \return Nonzero if the expr could not be split into a value for some * reason (e.g. the relative portion was not added, but multiplied, * etc). @@ -111,17 +111,17 @@ int yasm_value_finalize(yasm_value *value, /*@null@*/ yasm_bytecode *precbc); * before the parse is complete will usually result in an error return. */ int yasm_value_finalize_expr(/*@out@*/ yasm_value *value, - /*@null@*/ /*@kept@*/ yasm_expr *e, - /*@null@*/ yasm_bytecode *precbc, - unsigned int size); + /*@null@*/ /*@kept@*/ yasm_expr *e, + /*@null@*/ yasm_bytecode *precbc, + unsigned int size); /** Get value if absolute or PC-relative section-local relative. Returns NULL * otherwise. - * \param value value - * \param bc current bytecode (for PC-relative calculation); if - * NULL, NULL is returned for PC-relative values. - * \param calc_bc_dist if nonzero, calculates bytecode distances in absolute - * portion of value + * \param value value + * \param bc current bytecode (for PC-relative calculation); if + * NULL, NULL is returned for PC-relative values. + * \param calc_bc_dist if nonzero, calculates bytecode distances in absolute + * portion of value * \note Adds in value.rel (correctly) if PC-relative and in the same section * as bc (and there is no WRT or SEG). * \return Intnum if can be resolved to integer value, otherwise NULL. @@ -132,15 +132,15 @@ int yasm_value_finalize_expr(/*@out@*/ yasm_value *value, /** Output value if constant or PC-relative section-local. This should be * used from objfmt yasm_output_value_func() functions. * functions. - * \param value value - * \param buf buffer for byte representation - * \param destsize destination size (in bytes) - * \param bc current bytecode (usually passed into higher-level - * calling function) - * \param warn enables standard warnings: zero for none; - * nonzero for overflow/underflow floating point and - * integer warnings - * \param arch architecture + * \param value value + * \param buf buffer for byte representation + * \param destsize destination size (in bytes) + * \param bc current bytecode (usually passed into higher-level + * calling function) + * \param warn enables standard warnings: zero for none; + * nonzero for overflow/underflow floating point and + * integer warnings + * \param arch architecture * \note Adds in value.rel (correctly) if PC-relative and in the same section * as bc (and there is no WRT or SEG); if this is not the desired * behavior, e.g. a reloc is needed in this case, don't use this @@ -153,9 +153,9 @@ int yasm_value_output_basic yasm_bytecode *bc, int warn, yasm_arch *arch); /** Print a value. For debugging purposes. - * \param value value - * \param indent_level indentation level - * \param f file + * \param value value + * \param indent_level indentation level + * \param f file */ void yasm_value_print(const yasm_value *value, FILE *f, int indent_level); diff --git a/libyasm/xmalloc.c b/libyasm/xmalloc.c index 5f947d97..e65236d1 100644 --- a/libyasm/xmalloc.c +++ b/libyasm/xmalloc.c @@ -63,10 +63,10 @@ def_xmalloc(size_t size) void *newmem; if (size == 0) - size = 1; + size = 1; newmem = malloc(size); if (!newmem) - yasm__fatal(N_("out of memory")); + yasm__fatal(N_("out of memory")); return newmem; } @@ -77,11 +77,11 @@ def_xcalloc(size_t nelem, size_t elsize) void *newmem; if (nelem == 0 || elsize == 0) - nelem = elsize = 1; + nelem = elsize = 1; newmem = calloc(nelem, elsize); if (!newmem) - yasm__fatal(N_("out of memory")); + yasm__fatal(N_("out of memory")); return newmem; } @@ -92,13 +92,13 @@ def_xrealloc(void *oldmem, size_t size) void *newmem; if (size == 0) - size = 1; + size = 1; if (!oldmem) - newmem = malloc(size); + newmem = malloc(size); else - newmem = realloc(oldmem, size); + newmem = realloc(oldmem, size); if (!newmem) - yasm__fatal(N_("out of memory")); + yasm__fatal(N_("out of memory")); return newmem; } @@ -107,6 +107,6 @@ static void def_xfree(void *p) { if (!p) - return; + return; free(p); } diff --git a/libyasm/xstrdup.c b/libyasm/xstrdup.c index c52c12cc..f5938eee 100644 --- a/libyasm/xstrdup.c +++ b/libyasm/xstrdup.c @@ -2,7 +2,7 @@ * strdup() implementation with error checking (using xmalloc). * * Copyright (c) 1988, 1993 - * The Regents of the University of California. All rights reserved. + * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -35,7 +35,7 @@ RCSID("$Id$"); #include "coretype.h" #if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; +static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ @@ -46,22 +46,22 @@ static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; char * yasm__xstrdup(const char *str) { - size_t len; - char *copy; + size_t len; + char *copy; - len = strlen(str) + 1; - copy = yasm_xmalloc(len); - memcpy(copy, str, len); - return (copy); + len = strlen(str) + 1; + copy = yasm_xmalloc(len); + memcpy(copy, str, len); + return (copy); } char * yasm__xstrndup(const char *str, size_t len) { - char *copy; + char *copy; - copy = yasm_xmalloc(len+1); - memcpy(copy, str, len); - copy[len] = '\0'; - return (copy); + copy = yasm_xmalloc(len+1); + memcpy(copy, str, len); + copy[len] = '\0'; + return (copy); } diff --git a/modules/arch/lc3b/lc3barch.c b/modules/arch/lc3b/lc3barch.c index bbf28956..69cfbd2c 100644 --- a/modules/arch/lc3b/lc3barch.c +++ b/modules/arch/lc3b/lc3barch.c @@ -38,20 +38,20 @@ yasm_arch_module yasm_lc3b_LTX_arch; static /*@only@*/ yasm_arch * lc3b_create(const char *machine, const char *parser, - /*@out@*/ yasm_arch_create_error *error) + /*@out@*/ yasm_arch_create_error *error) { yasm_arch_base *arch; *error = YASM_ARCH_CREATE_OK; if (yasm__strcasecmp(machine, "lc3b") != 0) { - *error = YASM_ARCH_CREATE_BAD_MACHINE; - return NULL; + *error = YASM_ARCH_CREATE_BAD_MACHINE; + return NULL; } if (yasm__strcasecmp(parser, "nasm") != 0) { - *error = YASM_ARCH_CREATE_BAD_PARSER; - return NULL; + *error = YASM_ARCH_CREATE_BAD_PARSER; + return NULL; } arch = yasm_xmalloc(sizeof(yasm_arch_base)); @@ -85,12 +85,12 @@ lc3b_set_var(yasm_arch *arch, const char *var, unsigned long val) static int lc3b_parse_directive(/*@unused@*/ yasm_arch *arch, - /*@unused@*/ const char *name, - /*@unused@*/ /*@null@*/ yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, - /*@unused@*/ yasm_object *object, - /*@unused@*/ unsigned long line) + /*@unused@*/ const char *name, + /*@unused@*/ /*@null@*/ yasm_valparamhead *valparams, + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + /*@unused@*/ yasm_object *object, + /*@unused@*/ unsigned long line) { return 1; } @@ -100,33 +100,33 @@ lc3b_get_fill(const yasm_arch *arch) { /* NOP pattern is all 0's per LC-3b Assembler 3.50 output */ static const unsigned char *fill[16] = { - NULL, /* unused */ - NULL, /* 1 - illegal; all opcodes are 2 bytes long */ - (const unsigned char *) - "\x00\x00", /* 4 */ - NULL, /* 3 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00", /* 4 */ - NULL, /* 5 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00", /* 6 */ - NULL, /* 7 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 8 */ - "\x00\x00", - NULL, /* 9 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 10 */ - "\x00\x00\x00\x00", - NULL, /* 11 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 12 */ - "\x00\x00\x00\x00\x00\x00", - NULL, /* 13 - illegal */ - (const unsigned char *) - "\x00\x00\x00\x00\x00\x00" /* 14 */ - "\x00\x00\x00\x00\x00\x00\x00\x00", - NULL /* 15 - illegal */ + NULL, /* unused */ + NULL, /* 1 - illegal; all opcodes are 2 bytes long */ + (const unsigned char *) + "\x00\x00", /* 4 */ + NULL, /* 3 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00", /* 4 */ + NULL, /* 5 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00", /* 6 */ + NULL, /* 7 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 8 */ + "\x00\x00", + NULL, /* 9 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 10 */ + "\x00\x00\x00\x00", + NULL, /* 11 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 12 */ + "\x00\x00\x00\x00\x00\x00", + NULL, /* 13 - illegal */ + (const unsigned char *) + "\x00\x00\x00\x00\x00\x00" /* 14 */ + "\x00\x00\x00\x00\x00\x00\x00\x00", + NULL /* 15 - illegal */ }; return fill; } @@ -139,8 +139,8 @@ lc3b_get_reg_size(/*@unused@*/ yasm_arch *arch, /*@unused@*/ unsigned long reg) static unsigned long lc3b_reggroup_get_reg(/*@unused@*/ yasm_arch *arch, - /*@unused@*/ unsigned long reggroup, - /*@unused@*/ unsigned long regindex) + /*@unused@*/ unsigned long reggroup, + /*@unused@*/ unsigned long regindex) { return 0; } @@ -153,11 +153,11 @@ lc3b_reg_print(/*@unused@*/ yasm_arch *arch, unsigned long reg, FILE *f) static int lc3b_floatnum_tobytes(yasm_arch *arch, const yasm_floatnum *flt, - unsigned char *buf, size_t destsize, size_t valsize, - size_t shift, int warn) + unsigned char *buf, size_t destsize, size_t valsize, + size_t shift, int warn) { yasm_error_set(YASM_ERROR_FLOATING_POINT, - N_("LC-3b does not support floating point")); + N_("LC-3b does not support floating point")); return 1; } @@ -194,7 +194,7 @@ yasm_arch_module yasm_lc3b_LTX_arch = { lc3b_get_reg_size, lc3b_reggroup_get_reg, lc3b_reg_print, - NULL, /*yasm_lc3b__segreg_print*/ + NULL, /*yasm_lc3b__segreg_print*/ lc3b_ea_create_expr, lc3b_machines, "lc3b", diff --git a/modules/arch/lc3b/lc3barch.h b/modules/arch/lc3b/lc3barch.h index 7d199bb9..466d07b7 100644 --- a/modules/arch/lc3b/lc3barch.h +++ b/modules/arch/lc3b/lc3barch.h @@ -29,26 +29,26 @@ /* Types of immediate. All immediates are stored in the LSBs of the insn. */ typedef enum lc3b_imm_type { - LC3B_IMM_NONE = 0, /* no immediate */ - LC3B_IMM_4, /* 4-bit */ - LC3B_IMM_5, /* 5-bit */ - LC3B_IMM_6_WORD, /* 6-bit, word-multiple (byte>>1) */ - LC3B_IMM_6_BYTE, /* 6-bit, byte-multiple */ - LC3B_IMM_8, /* 8-bit, word-multiple (byte>>1) */ - LC3B_IMM_9, /* 9-bit, signed, word-multiple (byte>>1) */ - LC3B_IMM_9_PC /* 9-bit, signed, word-multiple, PC relative */ + LC3B_IMM_NONE = 0, /* no immediate */ + LC3B_IMM_4, /* 4-bit */ + LC3B_IMM_5, /* 5-bit */ + LC3B_IMM_6_WORD, /* 6-bit, word-multiple (byte>>1) */ + LC3B_IMM_6_BYTE, /* 6-bit, byte-multiple */ + LC3B_IMM_8, /* 8-bit, word-multiple (byte>>1) */ + LC3B_IMM_9, /* 9-bit, signed, word-multiple (byte>>1) */ + LC3B_IMM_9_PC /* 9-bit, signed, word-multiple, PC relative */ } lc3b_imm_type; /* Bytecode types */ typedef struct lc3b_insn { - yasm_value imm; /* immediate or relative value */ - lc3b_imm_type imm_type; /* size of the immediate */ + yasm_value imm; /* immediate or relative value */ + lc3b_imm_type imm_type; /* size of the immediate */ /* PC origin if needed */ /*@null@*/ /*@dependent@*/ yasm_bytecode *origin_prevbc; - unsigned int opcode; /* opcode */ + unsigned int opcode; /* opcode */ } lc3b_insn; void yasm_lc3b__bc_transform_insn(yasm_bytecode *bc, lc3b_insn *insn); diff --git a/modules/arch/lc3b/lc3bbc.c b/modules/arch/lc3b/lc3bbc.c index 59e607fa..bd438196 100644 --- a/modules/arch/lc3b/lc3bbc.c +++ b/modules/arch/lc3b/lc3bbc.c @@ -38,12 +38,12 @@ static void lc3b_bc_insn_destroy(void *contents); static void lc3b_bc_insn_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static yasm_bc_resolve_flags lc3b_bc_insn_resolve (yasm_bytecode *bc, int save, yasm_calc_bc_dist_func calc_bc_dist); static int lc3b_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, - void *d, yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + void *d, yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); /* Bytecode callback structures */ @@ -79,55 +79,55 @@ lc3b_bc_insn_print(const void *contents, FILE *f, int indent_level) fprintf(f, "%*s_Instruction_\n", indent_level, ""); fprintf(f, "%*sImmediate Value:", indent_level, ""); if (!insn->imm.abs) - fprintf(f, " (nil)\n"); + fprintf(f, " (nil)\n"); else { - indent_level++; - fprintf(f, "\n"); - yasm_value_print(&insn->imm, f, indent_level); - fprintf(f, "%*sType=", indent_level, ""); - switch (insn->imm_type) { - case LC3B_IMM_NONE: - fprintf(f, "NONE-SHOULDN'T HAPPEN"); - break; - case LC3B_IMM_4: - fprintf(f, "4-bit"); - break; - case LC3B_IMM_5: - fprintf(f, "5-bit"); - break; - case LC3B_IMM_6_WORD: - fprintf(f, "6-bit, word-multiple"); - break; - case LC3B_IMM_6_BYTE: - fprintf(f, "6-bit, byte-multiple"); - break; - case LC3B_IMM_8: - fprintf(f, "8-bit, word-multiple"); - break; - case LC3B_IMM_9: - fprintf(f, "9-bit, signed, word-multiple"); - break; - case LC3B_IMM_9_PC: - fprintf(f, "9-bit, signed, word-multiple, PC-relative"); - break; - } - indent_level--; + indent_level++; + fprintf(f, "\n"); + yasm_value_print(&insn->imm, f, indent_level); + fprintf(f, "%*sType=", indent_level, ""); + switch (insn->imm_type) { + case LC3B_IMM_NONE: + fprintf(f, "NONE-SHOULDN'T HAPPEN"); + break; + case LC3B_IMM_4: + fprintf(f, "4-bit"); + break; + case LC3B_IMM_5: + fprintf(f, "5-bit"); + break; + case LC3B_IMM_6_WORD: + fprintf(f, "6-bit, word-multiple"); + break; + case LC3B_IMM_6_BYTE: + fprintf(f, "6-bit, byte-multiple"); + break; + case LC3B_IMM_8: + fprintf(f, "8-bit, word-multiple"); + break; + case LC3B_IMM_9: + fprintf(f, "9-bit, signed, word-multiple"); + break; + case LC3B_IMM_9_PC: + fprintf(f, "9-bit, signed, word-multiple, PC-relative"); + break; + } + indent_level--; } /* FIXME fprintf(f, "\n%*sOrigin=", indent_level, ""); if (insn->origin) { - fprintf(f, "\n"); - yasm_symrec_print(insn->origin, f, indent_level+1); + fprintf(f, "\n"); + yasm_symrec_print(insn->origin, f, indent_level+1); } else - fprintf(f, "(nil)\n"); + fprintf(f, "(nil)\n"); */ fprintf(f, "%*sOpcode: %04x\n", indent_level, "", - (unsigned int)insn->opcode); + (unsigned int)insn->opcode); } static yasm_bc_resolve_flags lc3b_bc_insn_resolve(yasm_bytecode *bc, int save, - yasm_calc_bc_dist_func calc_bc_dist) + yasm_calc_bc_dist_func calc_bc_dist) { lc3b_insn *insn = (lc3b_insn *)bc->contents; yasm_bytecode *target_prevbc; @@ -141,29 +141,29 @@ lc3b_bc_insn_resolve(yasm_bytecode *bc, int save, * saving. */ if (insn->imm_type != LC3B_IMM_9_PC || !save) - return YASM_BC_RESOLVE_MIN_LEN; + return YASM_BC_RESOLVE_MIN_LEN; if (!insn->imm.rel) - num = yasm_intnum_create_uint(0); + num = yasm_intnum_create_uint(0); else if (!yasm_symrec_get_label(insn->imm.rel, &target_prevbc) - || target_prevbc->section != insn->origin_prevbc->section - || !(num = calc_bc_dist(insn->origin_prevbc, target_prevbc))) { - /* External or out of segment, so we can't check distance. */ - return YASM_BC_RESOLVE_MIN_LEN; + || target_prevbc->section != insn->origin_prevbc->section + || !(num = calc_bc_dist(insn->origin_prevbc, target_prevbc))) { + /* External or out of segment, so we can't check distance. */ + return YASM_BC_RESOLVE_MIN_LEN; } if (insn->imm.abs) { - /*@only@*/ yasm_expr *temp = yasm_expr_copy(insn->imm.abs); - /*@dependent@*/ /*@null@*/ yasm_intnum *num2; - num2 = yasm_expr_get_intnum(&temp, calc_bc_dist); - if (!num2) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("jump target too complex")); - yasm_expr_destroy(temp); - return YASM_BC_RESOLVE_ERROR | YASM_BC_RESOLVE_UNKNOWN_LEN; - } - yasm_intnum_calc(num, YASM_EXPR_ADD, num2); - yasm_expr_destroy(temp); + /*@only@*/ yasm_expr *temp = yasm_expr_copy(insn->imm.abs); + /*@dependent@*/ /*@null@*/ yasm_intnum *num2; + num2 = yasm_expr_get_intnum(&temp, calc_bc_dist); + if (!num2) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("jump target too complex")); + yasm_expr_destroy(temp); + return YASM_BC_RESOLVE_ERROR | YASM_BC_RESOLVE_UNKNOWN_LEN; + } + yasm_intnum_calc(num, YASM_EXPR_ADD, num2); + yasm_expr_destroy(temp); } rel = yasm_intnum_get_int(num); @@ -171,16 +171,16 @@ lc3b_bc_insn_resolve(yasm_bytecode *bc, int save, rel -= 2; /* 9-bit signed, word-multiple displacement */ if (rel < -512 || rel > 511) { - yasm_error_set(YASM_ERROR_OVERFLOW, N_("target out of range")); - return YASM_BC_RESOLVE_ERROR | YASM_BC_RESOLVE_UNKNOWN_LEN; + yasm_error_set(YASM_ERROR_OVERFLOW, N_("target out of range")); + return YASM_BC_RESOLVE_ERROR | YASM_BC_RESOLVE_UNKNOWN_LEN; } return YASM_BC_RESOLVE_MIN_LEN; } static int lc3b_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { lc3b_insn *insn = (lc3b_insn *)bc->contents; /*@only@*/ yasm_intnum *delta; @@ -190,66 +190,66 @@ lc3b_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Insert immediate into opcode. */ switch (insn->imm_type) { - case LC3B_IMM_NONE: - break; - case LC3B_IMM_4: - insn->imm.size = 4; - if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) - return 1; - break; - case LC3B_IMM_5: - insn->imm.size = 5; - if (output_value(&insn->imm, *bufp, 2, 0, bc, -1, d)) - return 1; - break; - case LC3B_IMM_6_WORD: - insn->imm.size = 6; - if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) - return 1; - break; - case LC3B_IMM_6_BYTE: - insn->imm.size = 6; - if (output_value(&insn->imm, *bufp, 2, 0, bc, -1, d)) - return 1; - break; - case LC3B_IMM_8: - insn->imm.size = 8; - if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) - return 1; - break; - case LC3B_IMM_9_PC: - /* Adjust relative displacement to end of bytecode */ - delta = yasm_intnum_create_int(-1); - if (!insn->imm.abs) - insn->imm.abs = yasm_expr_create_ident(yasm_expr_int(delta), - bc->line); - else - insn->imm.abs = - yasm_expr_create(YASM_EXPR_ADD, - yasm_expr_expr(insn->imm.abs), - yasm_expr_int(delta), bc->line); + case LC3B_IMM_NONE: + break; + case LC3B_IMM_4: + insn->imm.size = 4; + if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) + return 1; + break; + case LC3B_IMM_5: + insn->imm.size = 5; + if (output_value(&insn->imm, *bufp, 2, 0, bc, -1, d)) + return 1; + break; + case LC3B_IMM_6_WORD: + insn->imm.size = 6; + if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) + return 1; + break; + case LC3B_IMM_6_BYTE: + insn->imm.size = 6; + if (output_value(&insn->imm, *bufp, 2, 0, bc, -1, d)) + return 1; + break; + case LC3B_IMM_8: + insn->imm.size = 8; + if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) + return 1; + break; + case LC3B_IMM_9_PC: + /* Adjust relative displacement to end of bytecode */ + delta = yasm_intnum_create_int(-1); + if (!insn->imm.abs) + insn->imm.abs = yasm_expr_create_ident(yasm_expr_int(delta), + bc->line); + else + insn->imm.abs = + yasm_expr_create(YASM_EXPR_ADD, + yasm_expr_expr(insn->imm.abs), + yasm_expr_int(delta), bc->line); - insn->imm.size = 9; - if (output_value(&insn->imm, *bufp, 2, 0, bc, -1, d)) - return 1; - break; - case LC3B_IMM_9: - insn->imm.size = 9; - if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) - return 1; - break; - default: - yasm_internal_error(N_("Unrecognized immediate type")); + insn->imm.size = 9; + if (output_value(&insn->imm, *bufp, 2, 0, bc, -1, d)) + return 1; + break; + case LC3B_IMM_9: + insn->imm.size = 9; + if (output_value(&insn->imm, *bufp, 2, 0, bc, 1, d)) + return 1; + break; + default: + yasm_internal_error(N_("Unrecognized immediate type")); } - *bufp += 2; /* all instructions are 2 bytes in size */ + *bufp += 2; /* all instructions are 2 bytes in size */ return 0; } int yasm_lc3b__intnum_tobytes(yasm_arch *arch, const yasm_intnum *intn, - unsigned char *buf, size_t destsize, size_t valsize, - int shift, const yasm_bytecode *bc, int warn) + unsigned char *buf, size_t destsize, size_t valsize, + int shift, const yasm_bytecode *bc, int warn) { /* Write value out. */ yasm_intnum_get_sized(intn, buf, destsize, valsize, shift, 0, warn); diff --git a/modules/arch/lc3b/lc3bid.re b/modules/arch/lc3b/lc3bid.re index 0c7e82a4..cb1e538f 100644 --- a/modules/arch/lc3b/lc3bid.re +++ b/modules/arch/lc3b/lc3bid.re @@ -39,8 +39,8 @@ RCSID("$Id$"); * parameters are read from the arch-specific data in LSB->MSB order. * (only for asthetic reasons in the lexer code below, no practical reason). */ -#define MOD_OpHAdd (1UL<<0) /* Parameter adds to upper 8 bits of insn */ -#define MOD_OpLAdd (1UL<<1) /* Parameter adds to lower 8 bits of insn */ +#define MOD_OpHAdd (1UL<<0) /* Parameter adds to upper 8 bits of insn */ +#define MOD_OpLAdd (1UL<<1) /* Parameter adds to lower 8 bits of insn */ /* Operand types. These are more detailed than the "general" types for all * architectures, as they include the size, for instance. @@ -71,25 +71,25 @@ RCSID("$Id$"); * 6 = 9-bit signed immediate, word-multiple * 7 = 9-bit signed offset from next PC ($+2), word-multiple */ -#define OPT_Imm 0x0 -#define OPT_Reg 0x1 -#define OPT_MASK 0x1 - -#define OPA_None (0<<1) -#define OPA_DR (1<<1) -#define OPA_SR (2<<1) -#define OPA_Imm (3<<1) -#define OPA_MASK (3<<1) - -#define OPI_None (LC3B_IMM_NONE<<3) -#define OPI_4 (LC3B_IMM_4<<3) -#define OPI_5 (LC3B_IMM_5<<3) -#define OPI_6W (LC3B_IMM_6_WORD<<3) -#define OPI_6B (LC3B_IMM_6_BYTE<<3) -#define OPI_8 (LC3B_IMM_8<<3) -#define OPI_9 (LC3B_IMM_9<<3) -#define OPI_9PC (LC3B_IMM_9_PC<<3) -#define OPI_MASK (7<<3) +#define OPT_Imm 0x0 +#define OPT_Reg 0x1 +#define OPT_MASK 0x1 + +#define OPA_None (0<<1) +#define OPA_DR (1<<1) +#define OPA_SR (2<<1) +#define OPA_Imm (3<<1) +#define OPA_MASK (3<<1) + +#define OPI_None (LC3B_IMM_NONE<<3) +#define OPI_4 (LC3B_IMM_4<<3) +#define OPI_5 (LC3B_IMM_5<<3) +#define OPI_6W (LC3B_IMM_6_WORD<<3) +#define OPI_6B (LC3B_IMM_6_BYTE<<3) +#define OPI_8 (LC3B_IMM_8<<3) +#define OPI_9 (LC3B_IMM_9<<3) +#define OPI_9PC (LC3B_IMM_9_PC<<3) +#define OPI_MASK (7<<3) typedef struct lc3b_insn_info { /* Opcode modifiers for variations of instruction. As each modifier reads @@ -111,13 +111,13 @@ typedef struct lc3b_insn_info { } lc3b_insn_info; /* Define lexer arch-specific data with 0-3 modifiers. */ -#define DEF_INSN_DATA(group, mod) do { \ +#define DEF_INSN_DATA(group, mod) do { \ data[0] = (unsigned long)group##_insn; \ data[1] = ((mod)<<8) | \ - ((unsigned char)(sizeof(group##_insn)/sizeof(lc3b_insn_info))); \ + ((unsigned char)(sizeof(group##_insn)/sizeof(lc3b_insn_info))); \ } while (0) -#define RET_INSN(group, mod) do { \ +#define RET_INSN(group, mod) do { \ DEF_INSN_DATA(group, mod); \ return YASM_ARCH_INSN; \ } while (0) @@ -174,11 +174,11 @@ static const lc3b_insn_info trap_insn[] = { void yasm_lc3b__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, - yasm_bytecode *prev_bc, const unsigned long data[4], - int num_operands, - /*@null@*/ yasm_insn_operands *operands, - int num_prefixes, unsigned long **prefixes, - int num_segregs, const unsigned long *segregs) + yasm_bytecode *prev_bc, const unsigned long data[4], + int num_operands, + /*@null@*/ yasm_insn_operands *operands, + int num_prefixes, unsigned long **prefixes, + int num_segregs, const unsigned long *segregs) { lc3b_insn *insn; int num_info = (int)(data[1]&0xFF); @@ -192,49 +192,49 @@ yasm_lc3b__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, * First match wins. */ for (; num_info>0 && !found; num_info--, info++) { - int mismatch = 0; - - /* Match # of operands */ - if (num_operands != info->num_operands) - continue; - - if (!operands) { - found = 1; /* no operands -> must have a match here. */ - break; - } - - /* Match each operand type and size */ - for(i = 0, op = yasm_ops_first(operands); op && inum_operands && - !mismatch; op = yasm_operand_next(op), i++) { - /* Check operand type */ - switch ((int)(info->operands[i] & OPT_MASK)) { - case OPT_Imm: - if (op->type != YASM_INSN__OPERAND_IMM) - mismatch = 1; - break; - case OPT_Reg: - if (op->type != YASM_INSN__OPERAND_REG) - mismatch = 1; - break; - default: - yasm_internal_error(N_("invalid operand type")); - } - - if (mismatch) - break; - } - - if (!mismatch) { - found = 1; - break; - } + int mismatch = 0; + + /* Match # of operands */ + if (num_operands != info->num_operands) + continue; + + if (!operands) { + found = 1; /* no operands -> must have a match here. */ + break; + } + + /* Match each operand type and size */ + for(i = 0, op = yasm_ops_first(operands); op && inum_operands && + !mismatch; op = yasm_operand_next(op), i++) { + /* Check operand type */ + switch ((int)(info->operands[i] & OPT_MASK)) { + case OPT_Imm: + if (op->type != YASM_INSN__OPERAND_IMM) + mismatch = 1; + break; + case OPT_Reg: + if (op->type != YASM_INSN__OPERAND_REG) + mismatch = 1; + break; + default: + yasm_internal_error(N_("invalid operand type")); + } + + if (mismatch) + break; + } + + if (!mismatch) { + found = 1; + break; + } } if (!found) { - /* Didn't find a matching one */ - yasm_error_set(YASM_ERROR_TYPE, - N_("invalid combination of opcode and operands")); - return; + /* Didn't find a matching one */ + yasm_error_set(YASM_ERROR_TYPE, + N_("invalid combination of opcode and operands")); + return; } /* Copy what we can from info */ @@ -246,75 +246,75 @@ yasm_lc3b__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, /* Apply modifiers */ if (info->modifiers & MOD_OpHAdd) { - insn->opcode += ((unsigned int)(mod_data & 0xFF))<<8; - mod_data >>= 8; + insn->opcode += ((unsigned int)(mod_data & 0xFF))<<8; + mod_data >>= 8; } if (info->modifiers & MOD_OpLAdd) { - insn->opcode += (unsigned int)(mod_data & 0xFF); - /*mod_data >>= 8;*/ + insn->opcode += (unsigned int)(mod_data & 0xFF); + /*mod_data >>= 8;*/ } /* Go through operands and assign */ if (operands) { - for(i = 0, op = yasm_ops_first(operands); op && inum_operands; - op = yasm_operand_next(op), i++) { - - switch ((int)(info->operands[i] & OPA_MASK)) { - case OPA_None: - /* Throw away the operand contents */ - if (op->type == YASM_INSN__OPERAND_IMM) - yasm_expr_destroy(op->data.val); - break; - case OPA_DR: - if (op->type != YASM_INSN__OPERAND_REG) - yasm_internal_error(N_("invalid operand conversion")); - insn->opcode |= ((unsigned int)(op->data.reg & 0x7)) << 9; - break; - case OPA_SR: - if (op->type != YASM_INSN__OPERAND_REG) - yasm_internal_error(N_("invalid operand conversion")); - insn->opcode |= ((unsigned int)(op->data.reg & 0x7)) << 6; - break; - case OPA_Imm: - insn->imm_type = (info->operands[i] & OPI_MASK)>>3; - switch (op->type) { - case YASM_INSN__OPERAND_IMM: - if (insn->imm_type == LC3B_IMM_6_WORD - || insn->imm_type == LC3B_IMM_8 - || insn->imm_type == LC3B_IMM_9 - || insn->imm_type == LC3B_IMM_9_PC) - op->data.val = yasm_expr_create(YASM_EXPR_SHR, - yasm_expr_expr(op->data.val), - yasm_expr_int(yasm_intnum_create_uint(1)), - op->data.val->line); - if (yasm_value_finalize_expr(&insn->imm, - op->data.val, 0)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("immediate expression too complex")); - break; - case YASM_INSN__OPERAND_REG: - if (yasm_value_finalize_expr(&insn->imm, - yasm_expr_create_ident(yasm_expr_int( - yasm_intnum_create_uint(op->data.reg & 0x7)), - bc->line), 0)) - yasm_internal_error(N_("reg expr too complex?")); - break; - default: - yasm_internal_error(N_("invalid operand conversion")); - } - break; - default: - yasm_internal_error(N_("unknown operand action")); - } - } - - if (insn->imm_type == LC3B_IMM_9_PC) { - insn->origin_prevbc = prev_bc; - if (insn->imm.seg_of || insn->imm.rshift > 1 - || insn->imm.curpos_rel) - yasm_error_set(YASM_ERROR_VALUE, N_("invalid jump target")); - insn->imm.curpos_rel = 1; - } + for(i = 0, op = yasm_ops_first(operands); op && inum_operands; + op = yasm_operand_next(op), i++) { + + switch ((int)(info->operands[i] & OPA_MASK)) { + case OPA_None: + /* Throw away the operand contents */ + if (op->type == YASM_INSN__OPERAND_IMM) + yasm_expr_destroy(op->data.val); + break; + case OPA_DR: + if (op->type != YASM_INSN__OPERAND_REG) + yasm_internal_error(N_("invalid operand conversion")); + insn->opcode |= ((unsigned int)(op->data.reg & 0x7)) << 9; + break; + case OPA_SR: + if (op->type != YASM_INSN__OPERAND_REG) + yasm_internal_error(N_("invalid operand conversion")); + insn->opcode |= ((unsigned int)(op->data.reg & 0x7)) << 6; + break; + case OPA_Imm: + insn->imm_type = (info->operands[i] & OPI_MASK)>>3; + switch (op->type) { + case YASM_INSN__OPERAND_IMM: + if (insn->imm_type == LC3B_IMM_6_WORD + || insn->imm_type == LC3B_IMM_8 + || insn->imm_type == LC3B_IMM_9 + || insn->imm_type == LC3B_IMM_9_PC) + op->data.val = yasm_expr_create(YASM_EXPR_SHR, + yasm_expr_expr(op->data.val), + yasm_expr_int(yasm_intnum_create_uint(1)), + op->data.val->line); + if (yasm_value_finalize_expr(&insn->imm, + op->data.val, 0)) + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("immediate expression too complex")); + break; + case YASM_INSN__OPERAND_REG: + if (yasm_value_finalize_expr(&insn->imm, + yasm_expr_create_ident(yasm_expr_int( + yasm_intnum_create_uint(op->data.reg & 0x7)), + bc->line), 0)) + yasm_internal_error(N_("reg expr too complex?")); + break; + default: + yasm_internal_error(N_("invalid operand conversion")); + } + break; + default: + yasm_internal_error(N_("unknown operand action")); + } + } + + if (insn->imm_type == LC3B_IMM_9_PC) { + insn->origin_prevbc = prev_bc; + if (insn->imm.seg_of || insn->imm.rshift > 1 + || insn->imm.curpos_rel) + yasm_error_set(YASM_ERROR_VALUE, N_("invalid jump target")); + insn->imm.curpos_rel = 1; + } } /* Transform the bytecode */ @@ -322,11 +322,11 @@ yasm_lc3b__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, } -#define YYCTYPE unsigned char -#define YYCURSOR id -#define YYLIMIT id -#define YYMARKER marker -#define YYFILL(n) (void)(n) +#define YYCTYPE unsigned char +#define YYCURSOR id +#define YYLIMIT id +#define YYMARKER marker +#define YYFILL(n) (void)(n) void yasm_lc3b__parse_cpu(yasm_arch *arch, const char *cpuid, size_t cpuid_len) @@ -335,79 +335,79 @@ yasm_lc3b__parse_cpu(yasm_arch *arch, const char *cpuid, size_t cpuid_len) yasm_arch_regtmod yasm_lc3b__parse_check_regtmod(yasm_arch *arch, unsigned long *data, - const char *oid, size_t id_len) + const char *oid, size_t id_len) { const YYCTYPE *id = (const YYCTYPE *)oid; /*const char *marker;*/ /*!re2c - /* integer registers */ - 'r' [0-7] { - *data = (oid[1]-'0'); - return YASM_ARCH_REG; - } - - /* catchalls */ - [\001-\377]+ { - return YASM_ARCH_NOTREGTMOD; - } - [\000] { - return YASM_ARCH_NOTREGTMOD; - } + /* integer registers */ + 'r' [0-7] { + *data = (oid[1]-'0'); + return YASM_ARCH_REG; + } + + /* catchalls */ + [\001-\377]+ { + return YASM_ARCH_NOTREGTMOD; + } + [\000] { + return YASM_ARCH_NOTREGTMOD; + } */ } yasm_arch_insnprefix yasm_lc3b__parse_check_insnprefix(yasm_arch *arch, unsigned long data[4], - const char *oid, size_t id_len) + const char *oid, size_t id_len) { const YYCTYPE *id = (const YYCTYPE *)oid; /*const char *marker;*/ /*!re2c - /* instructions */ + /* instructions */ - 'add' { RET_INSN(addand, 0x00); } - 'and' { RET_INSN(addand, 0x40); } + 'add' { RET_INSN(addand, 0x00); } + 'and' { RET_INSN(addand, 0x40); } - 'br' { RET_INSN(br, 0x00); } - 'brn' { RET_INSN(br, 0x08); } - 'brz' { RET_INSN(br, 0x04); } - 'brp' { RET_INSN(br, 0x02); } - 'brnz' { RET_INSN(br, 0x0C); } - 'brnp' { RET_INSN(br, 0x0A); } - 'brzp' { RET_INSN(br, 0x06); } - 'brnzp' { RET_INSN(br, 0x0E); } - 'jsr' { RET_INSN(br, 0x40); } + 'br' { RET_INSN(br, 0x00); } + 'brn' { RET_INSN(br, 0x08); } + 'brz' { RET_INSN(br, 0x04); } + 'brp' { RET_INSN(br, 0x02); } + 'brnz' { RET_INSN(br, 0x0C); } + 'brnp' { RET_INSN(br, 0x0A); } + 'brzp' { RET_INSN(br, 0x06); } + 'brnzp' { RET_INSN(br, 0x0E); } + 'jsr' { RET_INSN(br, 0x40); } - 'jmp' { RET_INSN(jmp, 0); } + 'jmp' { RET_INSN(jmp, 0); } - 'lea' { RET_INSN(lea, 0); } + 'lea' { RET_INSN(lea, 0); } - 'ld' { RET_INSN(ldst, 0x20); } - 'ldi' { RET_INSN(ldst, 0xA0); } - 'st' { RET_INSN(ldst, 0x30); } - 'sti' { RET_INSN(ldst, 0xB0); } + 'ld' { RET_INSN(ldst, 0x20); } + 'ldi' { RET_INSN(ldst, 0xA0); } + 'st' { RET_INSN(ldst, 0x30); } + 'sti' { RET_INSN(ldst, 0xB0); } - 'ldb' { RET_INSN(ldstb, 0x60); } - 'stb' { RET_INSN(ldstb, 0x70); } + 'ldb' { RET_INSN(ldstb, 0x60); } + 'stb' { RET_INSN(ldstb, 0x70); } - 'not' { RET_INSN(not, 0); } + 'not' { RET_INSN(not, 0); } - 'ret' { RET_INSN(nooperand, 0xCE); } - 'rti' { RET_INSN(nooperand, 0x80); } - 'nop' { RET_INSN(nooperand, 0); } + 'ret' { RET_INSN(nooperand, 0xCE); } + 'rti' { RET_INSN(nooperand, 0x80); } + 'nop' { RET_INSN(nooperand, 0); } - 'lshf' { RET_INSN(shift, 0x00); } - 'rshfl' { RET_INSN(shift, 0x10); } - 'rshfa' { RET_INSN(shift, 0x30); } + 'lshf' { RET_INSN(shift, 0x00); } + 'rshfl' { RET_INSN(shift, 0x10); } + 'rshfa' { RET_INSN(shift, 0x30); } - 'trap' { RET_INSN(trap, 0); } + 'trap' { RET_INSN(trap, 0); } - /* catchalls */ - [\001-\377]+ { - return YASM_ARCH_NOTINSNPREFIX; - } - [\000] { - return YASM_ARCH_NOTINSNPREFIX; - } + /* catchalls */ + [\001-\377]+ { + return YASM_ARCH_NOTINSNPREFIX; + } + [\000] { + return YASM_ARCH_NOTINSNPREFIX; + } */ } diff --git a/modules/arch/x86/tests/sse4.c b/modules/arch/x86/tests/sse4.c index b8803699..419a1271 100644 --- a/modules/arch/x86/tests/sse4.c +++ b/modules/arch/x86/tests/sse4.c @@ -125,7 +125,7 @@ static void test_pmulhrsw_c(short *pDst, short *pSrc, int xmm) for ( i = 0; i < (4 << xmm); i++ ) { int a = pSrc[ i ] * pDst[ i ]; - pDst[i] = (short)(((a >> 14) + 1) >> 1); + pDst[i] = (short)(((a >> 14) + 1) >> 1); } } @@ -136,7 +136,7 @@ static void test_pmaddubsw_c(unsigned char *pDst, signed char *pSrc, int xmm) for ( i = 0; i < (4 << xmm); i++ ) { int a = pSrc[ 2 * i ] * pDst[ 2 * i ] + pSrc[ 2 * i + 1 ] * pDst[ 2 * i + 1]; - ((signed short *)pDst)[i] = SAT(a); + ((signed short *)pDst)[i] = SAT(a); } } @@ -215,9 +215,9 @@ void main(int nArgC, char *pArgv[]) CHECK_FUNCTIONS( phsubd ); CHECK_FUNCTIONS( pmulhrsw ); - CHECK_FUNCTIONS( pmaddubsw ); + CHECK_FUNCTIONS( pmaddubsw ); - CHECK_FUNCTIONS( pshufb ); + CHECK_FUNCTIONS( pshufb ); CHECK_FUNCTIONS( palignr ); } } \ No newline at end of file diff --git a/modules/arch/x86/x86arch.c b/modules/arch/x86/x86arch.c index 387c7e5d..11af5c49 100644 --- a/modules/arch/x86/x86arch.c +++ b/modules/arch/x86/x86arch.c @@ -38,7 +38,7 @@ yasm_arch_module yasm_x86_LTX_arch; static /*@only@*/ yasm_arch * x86_create(const char *machine, const char *parser, - /*@out@*/ yasm_arch_create_error *error) + /*@out@*/ yasm_arch_create_error *error) { yasm_arch_x86 *arch_x86; unsigned int amd64_machine; @@ -46,12 +46,12 @@ x86_create(const char *machine, const char *parser, *error = YASM_ARCH_CREATE_OK; if (yasm__strcasecmp(machine, "x86") == 0) - amd64_machine = 0; + amd64_machine = 0; else if (yasm__strcasecmp(machine, "amd64") == 0) - amd64_machine = 1; + amd64_machine = 1; else { - *error = YASM_ARCH_CREATE_BAD_MACHINE; - return NULL; + *error = YASM_ARCH_CREATE_BAD_MACHINE; + return NULL; } arch_x86 = yasm_xmalloc(sizeof(yasm_arch_x86)); @@ -64,14 +64,14 @@ x86_create(const char *machine, const char *parser, arch_x86->force_strict = 0; if (yasm__strcasecmp(parser, "nasm") == 0) - arch_x86->parser = X86_PARSER_NASM; + arch_x86->parser = X86_PARSER_NASM; else if (yasm__strcasecmp(parser, "gas") == 0 - || yasm__strcasecmp(parser, "gnu") == 0) - arch_x86->parser = X86_PARSER_GAS; + || yasm__strcasecmp(parser, "gnu") == 0) + arch_x86->parser = X86_PARSER_GAS; else { - yasm_xfree(arch_x86); - *error = YASM_ARCH_CREATE_BAD_PARSER; - return NULL; + yasm_xfree(arch_x86); + *error = YASM_ARCH_CREATE_BAD_PARSER; + return NULL; } return (yasm_arch *)arch_x86; @@ -88,9 +88,9 @@ x86_get_machine(const yasm_arch *arch) { const yasm_arch_x86 *arch_x86 = (const yasm_arch_x86 *)arch; if (arch_x86->amd64_machine) - return "amd64"; + return "amd64"; else - return "x86"; + return "x86"; } static unsigned int @@ -98,11 +98,11 @@ x86_get_address_size(const yasm_arch *arch) { const yasm_arch_x86 *arch_x86 = (const yasm_arch_x86 *)arch; if (arch_x86->mode_bits != 0) - return arch_x86->mode_bits; + return arch_x86->mode_bits; if (arch_x86->amd64_machine) - return 64; + return 64; else - return 32; + return 32; } static int @@ -110,42 +110,42 @@ x86_set_var(yasm_arch *arch, const char *var, unsigned long val) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch; if (yasm__strcasecmp(var, "mode_bits") == 0) - arch_x86->mode_bits = (unsigned char)val; + arch_x86->mode_bits = (unsigned char)val; else if (yasm__strcasecmp(var, "force_strict") == 0) - arch_x86->force_strict = (unsigned char)val; + arch_x86->force_strict = (unsigned char)val; else - return 1; + return 1; return 0; } static void x86_dir_cpu(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)object->arch; yasm_valparam *vp; yasm_vps_foreach(vp, valparams) { - if (vp->val) - yasm_x86__parse_cpu(arch_x86, vp->val, strlen(vp->val)); - else if (vp->param) { - const yasm_intnum *intcpu; - intcpu = yasm_expr_get_intnum(&vp->param, 0); - if (!intcpu) - yasm_error_set(YASM_ERROR_SYNTAX, - N_("invalid argument to [%s]"), "CPU"); - else { - char strcpu[16]; - sprintf(strcpu, "%lu", yasm_intnum_get_uint(intcpu)); - yasm_x86__parse_cpu(arch_x86, strcpu, strlen(strcpu)); - } - } + if (vp->val) + yasm_x86__parse_cpu(arch_x86, vp->val, strlen(vp->val)); + else if (vp->param) { + const yasm_intnum *intcpu; + intcpu = yasm_expr_get_intnum(&vp->param, 0); + if (!intcpu) + yasm_error_set(YASM_ERROR_SYNTAX, + N_("invalid argument to [%s]"), "CPU"); + else { + char strcpu[16]; + sprintf(strcpu, "%lu", yasm_intnum_get_uint(intcpu)); + yasm_x86__parse_cpu(arch_x86, strcpu, strlen(strcpu)); + } + } } } static void x86_dir_bits(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)object->arch; yasm_valparam *vp; @@ -153,18 +153,18 @@ x86_dir_bits(yasm_object *object, yasm_valparamhead *valparams, long lval; if ((vp = yasm_vps_first(valparams)) && !vp->val && vp->param != NULL && - (intn = yasm_expr_get_intnum(&vp->param, 0)) != NULL && - (lval = yasm_intnum_get_int(intn)) && - (lval == 16 || lval == 32 || lval == 64)) - arch_x86->mode_bits = (unsigned char)lval; + (intn = yasm_expr_get_intnum(&vp->param, 0)) != NULL && + (lval = yasm_intnum_get_int(intn)) && + (lval == 16 || lval == 32 || lval == 64)) + arch_x86->mode_bits = (unsigned char)lval; else - yasm_error_set(YASM_ERROR_VALUE, N_("invalid argument to [%s]"), - "BITS"); + yasm_error_set(YASM_ERROR_VALUE, N_("invalid argument to [%s]"), + "BITS"); } static void x86_dir_code16(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)object->arch; arch_x86->mode_bits = 16; @@ -172,7 +172,7 @@ x86_dir_code16(yasm_object *object, yasm_valparamhead *valparams, static void x86_dir_code32(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)object->arch; arch_x86->mode_bits = 32; @@ -180,7 +180,7 @@ x86_dir_code32(yasm_object *object, yasm_valparamhead *valparams, static void x86_dir_code64(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)object->arch; arch_x86->mode_bits = 64; @@ -193,166 +193,166 @@ x86_get_fill(const yasm_arch *arch) /* Fill patterns that GAS uses. */ static const unsigned char *fill16[16] = { - NULL, /* unused */ - (const unsigned char *) - "\x90", /* 1 - nop */ - (const unsigned char *) - "\x89\xf6", /* 2 - mov si, si */ - (const unsigned char *) - "\x8d\x74\x00", /* 3 - lea si, [si+byte 0] */ - (const unsigned char *) - "\x8d\xb4\x00\x00", /* 4 - lea si, [si+word 0] */ - (const unsigned char *) - "\x90" /* 5 - nop */ - "\x8d\xb4\x00\x00", /* lea si, [si+word 0] */ - (const unsigned char *) - "\x89\xf6" /* 6 - mov si, si */ - "\x8d\xbd\x00\x00", /* lea di, [di+word 0] */ - (const unsigned char *) - "\x8d\x74\x00" /* 7 - lea si, [si+byte 0] */ - "\x8d\xbd\x00\x00", /* lea di, [di+word 0] */ - (const unsigned char *) - "\x8d\xb4\x00\x00" /* 8 - lea si, [si+word 0] */ - "\x8d\xbd\x00\x00", /* lea di, [di+word 0] */ - (const unsigned char *) - "\xeb\x07\x90\x90\x90\x90\x90" /* 9 - jmp $+9; nop fill */ - "\x90\x90", - (const unsigned char *) - "\xeb\x08\x90\x90\x90\x90\x90" /* 10 - jmp $+10; nop fill */ - "\x90\x90\x90", - (const unsigned char *) - "\xeb\x09\x90\x90\x90\x90\x90" /* 11 - jmp $+11; nop fill */ - "\x90\x90\x90\x90", - (const unsigned char *) - "\xeb\x0a\x90\x90\x90\x90\x90" /* 12 - jmp $+12; nop fill */ - "\x90\x90\x90\x90\x90", - (const unsigned char *) - "\xeb\x0b\x90\x90\x90\x90\x90" /* 13 - jmp $+13; nop fill */ - "\x90\x90\x90\x90\x90\x90", - (const unsigned char *) - "\xeb\x0c\x90\x90\x90\x90\x90" /* 14 - jmp $+14; nop fill */ - "\x90\x90\x90\x90\x90\x90\x90", - (const unsigned char *) - "\xeb\x0d\x90\x90\x90\x90\x90" /* 15 - jmp $+15; nop fill */ - "\x90\x90\x90\x90\x90\x90\x90\x90" + NULL, /* unused */ + (const unsigned char *) + "\x90", /* 1 - nop */ + (const unsigned char *) + "\x89\xf6", /* 2 - mov si, si */ + (const unsigned char *) + "\x8d\x74\x00", /* 3 - lea si, [si+byte 0] */ + (const unsigned char *) + "\x8d\xb4\x00\x00", /* 4 - lea si, [si+word 0] */ + (const unsigned char *) + "\x90" /* 5 - nop */ + "\x8d\xb4\x00\x00", /* lea si, [si+word 0] */ + (const unsigned char *) + "\x89\xf6" /* 6 - mov si, si */ + "\x8d\xbd\x00\x00", /* lea di, [di+word 0] */ + (const unsigned char *) + "\x8d\x74\x00" /* 7 - lea si, [si+byte 0] */ + "\x8d\xbd\x00\x00", /* lea di, [di+word 0] */ + (const unsigned char *) + "\x8d\xb4\x00\x00" /* 8 - lea si, [si+word 0] */ + "\x8d\xbd\x00\x00", /* lea di, [di+word 0] */ + (const unsigned char *) + "\xeb\x07\x90\x90\x90\x90\x90" /* 9 - jmp $+9; nop fill */ + "\x90\x90", + (const unsigned char *) + "\xeb\x08\x90\x90\x90\x90\x90" /* 10 - jmp $+10; nop fill */ + "\x90\x90\x90", + (const unsigned char *) + "\xeb\x09\x90\x90\x90\x90\x90" /* 11 - jmp $+11; nop fill */ + "\x90\x90\x90\x90", + (const unsigned char *) + "\xeb\x0a\x90\x90\x90\x90\x90" /* 12 - jmp $+12; nop fill */ + "\x90\x90\x90\x90\x90", + (const unsigned char *) + "\xeb\x0b\x90\x90\x90\x90\x90" /* 13 - jmp $+13; nop fill */ + "\x90\x90\x90\x90\x90\x90", + (const unsigned char *) + "\xeb\x0c\x90\x90\x90\x90\x90" /* 14 - jmp $+14; nop fill */ + "\x90\x90\x90\x90\x90\x90\x90", + (const unsigned char *) + "\xeb\x0d\x90\x90\x90\x90\x90" /* 15 - jmp $+15; nop fill */ + "\x90\x90\x90\x90\x90\x90\x90\x90" }; static const unsigned char *fill32[16] = { - NULL, /* unused */ - (const unsigned char *) - "\x90", /* 1 - nop */ - (const unsigned char *) - "\x89\xf6", /* 2 - mov esi, esi */ - (const unsigned char *) - "\x8d\x76\x00", /* 3 - lea esi, [esi+byte 0] */ - (const unsigned char *) - "\x8d\x74\x26\x00", /* 4 - lea esi, [esi*1+byte 0] */ - (const unsigned char *) - "\x90" /* 5 - nop */ - "\x8d\x74\x26\x00", /* lea esi, [esi*1+byte 0] */ - (const unsigned char *) - "\x8d\xb6\x00\x00\x00\x00", /* 6 - lea esi, [esi+dword 0] */ - (const unsigned char *) - "\x8d\xb4\x26\x00\x00\x00\x00", /* 7 - lea esi, [esi*1+dword 0] */ - (const unsigned char *) - "\x90" /* 8 - nop */ - "\x8d\xb4\x26\x00\x00\x00\x00", /* lea esi, [esi*1+dword 0] */ + NULL, /* unused */ + (const unsigned char *) + "\x90", /* 1 - nop */ + (const unsigned char *) + "\x89\xf6", /* 2 - mov esi, esi */ + (const unsigned char *) + "\x8d\x76\x00", /* 3 - lea esi, [esi+byte 0] */ + (const unsigned char *) + "\x8d\x74\x26\x00", /* 4 - lea esi, [esi*1+byte 0] */ + (const unsigned char *) + "\x90" /* 5 - nop */ + "\x8d\x74\x26\x00", /* lea esi, [esi*1+byte 0] */ + (const unsigned char *) + "\x8d\xb6\x00\x00\x00\x00", /* 6 - lea esi, [esi+dword 0] */ + (const unsigned char *) + "\x8d\xb4\x26\x00\x00\x00\x00", /* 7 - lea esi, [esi*1+dword 0] */ + (const unsigned char *) + "\x90" /* 8 - nop */ + "\x8d\xb4\x26\x00\x00\x00\x00", /* lea esi, [esi*1+dword 0] */ #if 0 - /* GAS uses these */ - (const unsigned char *) - "\x89\xf6" /* 9 - mov esi, esi */ - "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0] */ - (const unsigned char *) - "\x8d\x76\x00" /* 10 - lea esi, [esi+byte 0] */ - "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi+dword 0] */ - (const unsigned char *) - "\x8d\x74\x26\x00" /* 11 - lea esi, [esi*1+byte 0] */ - "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0]*/ - (const unsigned char *) - "\x8d\xb6\x00\x00\x00\x00" /* 12 - lea esi, [esi+dword 0] */ - "\x8d\xbf\x00\x00\x00\x00", /* lea edi, [edi+dword 0] */ - (const unsigned char *) - "\x8d\xb6\x00\x00\x00\x00" /* 13 - lea esi, [esi+dword 0] */ - "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0]*/ - (const unsigned char *) - "\x8d\xb4\x26\x00\x00\x00\x00" /* 14 - lea esi, [esi*1+dword 0]*/ - "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0]*/ + /* GAS uses these */ + (const unsigned char *) + "\x89\xf6" /* 9 - mov esi, esi */ + "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0] */ + (const unsigned char *) + "\x8d\x76\x00" /* 10 - lea esi, [esi+byte 0] */ + "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi+dword 0] */ + (const unsigned char *) + "\x8d\x74\x26\x00" /* 11 - lea esi, [esi*1+byte 0] */ + "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0]*/ + (const unsigned char *) + "\x8d\xb6\x00\x00\x00\x00" /* 12 - lea esi, [esi+dword 0] */ + "\x8d\xbf\x00\x00\x00\x00", /* lea edi, [edi+dword 0] */ + (const unsigned char *) + "\x8d\xb6\x00\x00\x00\x00" /* 13 - lea esi, [esi+dword 0] */ + "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0]*/ + (const unsigned char *) + "\x8d\xb4\x26\x00\x00\x00\x00" /* 14 - lea esi, [esi*1+dword 0]*/ + "\x8d\xbc\x27\x00\x00\x00\x00", /* lea edi, [edi*1+dword 0]*/ #else - /* But on newer processors, these are recommended */ - (const unsigned char *) - "\xeb\x07\x90\x90\x90\x90\x90" /* 9 - jmp $+9; nop fill */ - "\x90\x90", - (const unsigned char *) - "\xeb\x08\x90\x90\x90\x90\x90" /* 10 - jmp $+10; nop fill */ - "\x90\x90\x90", - (const unsigned char *) - "\xeb\x09\x90\x90\x90\x90\x90" /* 11 - jmp $+11; nop fill */ - "\x90\x90\x90\x90", - (const unsigned char *) - "\xeb\x0a\x90\x90\x90\x90\x90" /* 12 - jmp $+12; nop fill */ - "\x90\x90\x90\x90\x90", - (const unsigned char *) - "\xeb\x0b\x90\x90\x90\x90\x90" /* 13 - jmp $+13; nop fill */ - "\x90\x90\x90\x90\x90\x90", - (const unsigned char *) - "\xeb\x0c\x90\x90\x90\x90\x90" /* 14 - jmp $+14; nop fill */ - "\x90\x90\x90\x90\x90\x90\x90", + /* But on newer processors, these are recommended */ + (const unsigned char *) + "\xeb\x07\x90\x90\x90\x90\x90" /* 9 - jmp $+9; nop fill */ + "\x90\x90", + (const unsigned char *) + "\xeb\x08\x90\x90\x90\x90\x90" /* 10 - jmp $+10; nop fill */ + "\x90\x90\x90", + (const unsigned char *) + "\xeb\x09\x90\x90\x90\x90\x90" /* 11 - jmp $+11; nop fill */ + "\x90\x90\x90\x90", + (const unsigned char *) + "\xeb\x0a\x90\x90\x90\x90\x90" /* 12 - jmp $+12; nop fill */ + "\x90\x90\x90\x90\x90", + (const unsigned char *) + "\xeb\x0b\x90\x90\x90\x90\x90" /* 13 - jmp $+13; nop fill */ + "\x90\x90\x90\x90\x90\x90", + (const unsigned char *) + "\xeb\x0c\x90\x90\x90\x90\x90" /* 14 - jmp $+14; nop fill */ + "\x90\x90\x90\x90\x90\x90\x90", #endif - (const unsigned char *) - "\xeb\x0d\x90\x90\x90\x90\x90" /* 15 - jmp $+15; nop fill */ - "\x90\x90\x90\x90\x90\x90\x90\x90" + (const unsigned char *) + "\xeb\x0d\x90\x90\x90\x90\x90" /* 15 - jmp $+15; nop fill */ + "\x90\x90\x90\x90\x90\x90\x90\x90" }; static const unsigned char *fill64[16] = { - NULL, /* unused */ - (const unsigned char *) - "\x90", /* 1 - nop */ - (const unsigned char *) - "\x66\x90", /* 2 - o16; nop */ - (const unsigned char *) - "\x66\x66\x90", /* 3 - o16; o16; nop */ - (const unsigned char *) - "\x66\x66\x66\x90", /* 4 - o16; o16; o16; nop */ - (const unsigned char *) - "\x66\x66\x90\x66\x90", /* 5 */ - (const unsigned char *) - "\x66\x66\x90\x66\x66\x90", /* 6 */ - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x90", /* 7 */ - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x66" /* 8 */ - "\x90", - (const unsigned char *) - "\x66\x66\x90\x66\x66\x90\x66" /* 9 */ - "\x66\x90", - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x90" /* 10 */ - "\x66\x66\x90", - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x66" /* 11 */ - "\x90\x66\x66\x90", - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x66" /* 12 */ - "\x90\x66\x66\x66\x90", - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x90" /* 13 */ - "\x66\x66\x90\x66\x66\x90", - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x66" /* 14 */ - "\x90\x66\x66\x90\x66\x66\x90", - (const unsigned char *) - "\x66\x66\x66\x90\x66\x66\x66" /* 15 */ - "\x90\x66\x66\x66\x90\x66\x66\x90" + NULL, /* unused */ + (const unsigned char *) + "\x90", /* 1 - nop */ + (const unsigned char *) + "\x66\x90", /* 2 - o16; nop */ + (const unsigned char *) + "\x66\x66\x90", /* 3 - o16; o16; nop */ + (const unsigned char *) + "\x66\x66\x66\x90", /* 4 - o16; o16; o16; nop */ + (const unsigned char *) + "\x66\x66\x90\x66\x90", /* 5 */ + (const unsigned char *) + "\x66\x66\x90\x66\x66\x90", /* 6 */ + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x90", /* 7 */ + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x66" /* 8 */ + "\x90", + (const unsigned char *) + "\x66\x66\x90\x66\x66\x90\x66" /* 9 */ + "\x66\x90", + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x90" /* 10 */ + "\x66\x66\x90", + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x66" /* 11 */ + "\x90\x66\x66\x90", + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x66" /* 12 */ + "\x90\x66\x66\x66\x90", + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x90" /* 13 */ + "\x66\x66\x90\x66\x66\x90", + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x66" /* 14 */ + "\x90\x66\x66\x90\x66\x66\x90", + (const unsigned char *) + "\x66\x66\x66\x90\x66\x66\x66" /* 15 */ + "\x90\x66\x66\x66\x90\x66\x66\x90" }; switch (arch_x86->mode_bits) { - case 16: - return fill16; - case 32: - return fill32; - case 64: - return fill64; - default: - yasm_error_set(YASM_ERROR_VALUE, - N_("Invalid mode_bits in x86_get_fill")); - return NULL; + case 16: + return fill16; + case 32: + return fill32; + case 64: + return fill64; + default: + yasm_error_set(YASM_ERROR_VALUE, + N_("Invalid mode_bits in x86_get_fill")); + return NULL; } } @@ -360,49 +360,49 @@ unsigned int yasm_x86__get_reg_size(yasm_arch *arch, uintptr_t reg) { switch ((x86_expritem_reg_size)(reg & ~0xFUL)) { - case X86_REG8: - case X86_REG8X: - return 8; - case X86_REG16: - return 16; - case X86_REG32: - case X86_CRREG: - case X86_DRREG: - case X86_TRREG: - return 32; - case X86_REG64: - case X86_MMXREG: - return 64; - case X86_XMMREG: - return 128; - case X86_FPUREG: - return 80; - default: - yasm_error_set(YASM_ERROR_VALUE, N_("unknown register size")); + case X86_REG8: + case X86_REG8X: + return 8; + case X86_REG16: + return 16; + case X86_REG32: + case X86_CRREG: + case X86_DRREG: + case X86_TRREG: + return 32; + case X86_REG64: + case X86_MMXREG: + return 64; + case X86_XMMREG: + return 128; + case X86_FPUREG: + return 80; + default: + yasm_error_set(YASM_ERROR_VALUE, N_("unknown register size")); } return 0; } static uintptr_t x86_reggroup_get_reg(yasm_arch *arch, uintptr_t reggroup, - unsigned long regindex) + unsigned long regindex) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch; switch ((x86_expritem_reg_size)(reggroup & ~0xFUL)) { - case X86_XMMREG: - if (arch_x86->mode_bits == 64) { - if (regindex > 15) - return 0; - return reggroup | (regindex & 15); - } - /*@fallthrough@*/ - case X86_MMXREG: - case X86_FPUREG: - if (regindex > 7) - return 0; - return reggroup | (regindex & 7); - default: - yasm_error_set(YASM_ERROR_VALUE, N_("bad register group")); + case X86_XMMREG: + if (arch_x86->mode_bits == 64) { + if (regindex > 15) + return 0; + return reggroup | (regindex & 15); + } + /*@fallthrough@*/ + case X86_MMXREG: + case X86_FPUREG: + if (regindex > 7) + return 0; + return reggroup | (regindex & 7); + default: + yasm_error_set(YASM_ERROR_VALUE, N_("bad register group")); } return 0; } @@ -412,58 +412,58 @@ x86_reg_print(yasm_arch *arch, uintptr_t reg, FILE *f) { static const char *name8[] = {"al","cl","dl","bl","ah","ch","dh","bh"}; static const char *name8x[] = { - "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", - "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" + "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", + "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" }; static const char *name16[] = { - "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" - "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" + "ax", "cx", "dx", "bx", "sp", "bp", "si", "di" + "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" }; static const char *name32[] = { - "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" - "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" + "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi" + "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" }; static const char *name64[] = { - "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi" - "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" + "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi" + "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" }; switch ((x86_expritem_reg_size)(reg & ~0xFUL)) { - case X86_REG8: - fprintf(f, "%s", name8[reg&0xF]); - break; - case X86_REG8X: - fprintf(f, "%s", name8x[reg&0xF]); - break; - case X86_REG16: - fprintf(f, "%s", name16[reg&0xF]); - break; - case X86_REG32: - fprintf(f, "%s", name32[reg&0xF]); - break; - case X86_REG64: - fprintf(f, "%s", name64[reg&0xF]); - break; - case X86_MMXREG: - fprintf(f, "mm%d", (int)(reg&0xF)); - break; - case X86_XMMREG: - fprintf(f, "xmm%d", (int)(reg&0xF)); - break; - case X86_CRREG: - fprintf(f, "cr%d", (int)(reg&0xF)); - break; - case X86_DRREG: - fprintf(f, "dr%d", (int)(reg&0xF)); - break; - case X86_TRREG: - fprintf(f, "tr%d", (int)(reg&0xF)); - break; - case X86_FPUREG: - fprintf(f, "st%d", (int)(reg&0xF)); - break; - default: - yasm_error_set(YASM_ERROR_VALUE, N_("unknown register size")); + case X86_REG8: + fprintf(f, "%s", name8[reg&0xF]); + break; + case X86_REG8X: + fprintf(f, "%s", name8x[reg&0xF]); + break; + case X86_REG16: + fprintf(f, "%s", name16[reg&0xF]); + break; + case X86_REG32: + fprintf(f, "%s", name32[reg&0xF]); + break; + case X86_REG64: + fprintf(f, "%s", name64[reg&0xF]); + break; + case X86_MMXREG: + fprintf(f, "mm%d", (int)(reg&0xF)); + break; + case X86_XMMREG: + fprintf(f, "xmm%d", (int)(reg&0xF)); + break; + case X86_CRREG: + fprintf(f, "cr%d", (int)(reg&0xF)); + break; + case X86_DRREG: + fprintf(f, "dr%d", (int)(reg&0xF)); + break; + case X86_TRREG: + fprintf(f, "tr%d", (int)(reg&0xF)); + break; + case X86_FPUREG: + fprintf(f, "st%d", (int)(reg&0xF)); + break; + default: + yasm_error_set(YASM_ERROR_VALUE, N_("unknown register size")); } } @@ -482,11 +482,11 @@ static const yasm_arch_machine x86_machines[] = { }; static const yasm_directive x86_directives[] = { - { "cpu", "nasm", x86_dir_cpu, YASM_DIR_ARG_REQUIRED }, - { "bits", "nasm", x86_dir_bits, YASM_DIR_ARG_REQUIRED }, - { ".code16", "gas", x86_dir_code16, YASM_DIR_ANY }, - { ".code32", "gas", x86_dir_code32, YASM_DIR_ANY }, - { ".code64", "gas", x86_dir_code64, YASM_DIR_ANY }, + { "cpu", "nasm", x86_dir_cpu, YASM_DIR_ARG_REQUIRED }, + { "bits", "nasm", x86_dir_bits, YASM_DIR_ARG_REQUIRED }, + { ".code16", "gas", x86_dir_code16, YASM_DIR_ANY }, + { ".code32", "gas", x86_dir_code32, YASM_DIR_ANY }, + { ".code64", "gas", x86_dir_code64, YASM_DIR_ANY }, { NULL, NULL, NULL, 0 } }; diff --git a/modules/arch/x86/x86arch.h b/modules/arch/x86/x86arch.h index 95fe775e..7228f02e 100644 --- a/modules/arch/x86/x86arch.h +++ b/modules/arch/x86/x86arch.h @@ -28,53 +28,53 @@ #define YASM_X86ARCH_H /* Available CPU feature flags */ -#define CPU_Any (0UL) /* Any old cpu will do */ -#define CPU_086 CPU_Any -#define CPU_186 (1UL<<0) /* i186 or better required */ -#define CPU_286 (1UL<<1) /* i286 or better required */ -#define CPU_386 (1UL<<2) /* i386 or better required */ -#define CPU_486 (1UL<<3) /* i486 or better required */ -#define CPU_586 (1UL<<4) /* i585 or better required */ -#define CPU_686 (1UL<<5) /* i686 or better required */ -#define CPU_P3 (1UL<<6) /* Pentium3 or better required */ -#define CPU_P4 (1UL<<7) /* Pentium4 or better required */ -#define CPU_IA64 (1UL<<8) /* IA-64 or better required */ -#define CPU_K6 (1UL<<9) /* AMD K6 or better required */ -#define CPU_Athlon (1UL<<10) /* AMD Athlon or better required */ -#define CPU_Hammer (1UL<<11) /* AMD Sledgehammer or better required */ -#define CPU_FPU (1UL<<12) /* FPU support required */ -#define CPU_MMX (1UL<<13) /* MMX support required */ -#define CPU_SSE (1UL<<14) /* Streaming SIMD extensions required */ -#define CPU_SSE2 (1UL<<15) /* Streaming SIMD extensions 2 required */ -#define CPU_SSE3 (1UL<<16) /* Streaming SIMD extensions 3 required */ -#define CPU_3DNow (1UL<<17) /* 3DNow! support required */ -#define CPU_Cyrix (1UL<<18) /* Cyrix-specific instruction */ -#define CPU_AMD (1UL<<19) /* AMD-specific inst. (older than K6) */ -#define CPU_SMM (1UL<<20) /* System Management Mode instruction */ -#define CPU_Prot (1UL<<21) /* Protected mode only instruction */ -#define CPU_Undoc (1UL<<22) /* Undocumented instruction */ -#define CPU_Obs (1UL<<23) /* Obsolete instruction */ -#define CPU_Priv (1UL<<24) /* Priveleged instruction */ -#define CPU_SVM (1UL<<25) /* Secure Virtual Machine instruction */ -#define CPU_PadLock (1UL<<25) /* VIA PadLock instruction */ -#define CPU_EM64T (1UL<<26) /* Intel EM64T or better */ +#define CPU_Any (0UL) /* Any old cpu will do */ +#define CPU_086 CPU_Any +#define CPU_186 (1UL<<0) /* i186 or better required */ +#define CPU_286 (1UL<<1) /* i286 or better required */ +#define CPU_386 (1UL<<2) /* i386 or better required */ +#define CPU_486 (1UL<<3) /* i486 or better required */ +#define CPU_586 (1UL<<4) /* i585 or better required */ +#define CPU_686 (1UL<<5) /* i686 or better required */ +#define CPU_P3 (1UL<<6) /* Pentium3 or better required */ +#define CPU_P4 (1UL<<7) /* Pentium4 or better required */ +#define CPU_IA64 (1UL<<8) /* IA-64 or better required */ +#define CPU_K6 (1UL<<9) /* AMD K6 or better required */ +#define CPU_Athlon (1UL<<10) /* AMD Athlon or better required */ +#define CPU_Hammer (1UL<<11) /* AMD Sledgehammer or better required */ +#define CPU_FPU (1UL<<12) /* FPU support required */ +#define CPU_MMX (1UL<<13) /* MMX support required */ +#define CPU_SSE (1UL<<14) /* Streaming SIMD extensions required */ +#define CPU_SSE2 (1UL<<15) /* Streaming SIMD extensions 2 required */ +#define CPU_SSE3 (1UL<<16) /* Streaming SIMD extensions 3 required */ +#define CPU_3DNow (1UL<<17) /* 3DNow! support required */ +#define CPU_Cyrix (1UL<<18) /* Cyrix-specific instruction */ +#define CPU_AMD (1UL<<19) /* AMD-specific inst. (older than K6) */ +#define CPU_SMM (1UL<<20) /* System Management Mode instruction */ +#define CPU_Prot (1UL<<21) /* Protected mode only instruction */ +#define CPU_Undoc (1UL<<22) /* Undocumented instruction */ +#define CPU_Obs (1UL<<23) /* Obsolete instruction */ +#define CPU_Priv (1UL<<24) /* Priveleged instruction */ +#define CPU_SVM (1UL<<25) /* Secure Virtual Machine instruction */ +#define CPU_PadLock (1UL<<25) /* VIA PadLock instruction */ +#define CPU_EM64T (1UL<<26) /* Intel EM64T or better */ #define CPU_SSE4 (1UL<<27) /* Streaming SIMD extensions 4 required */ /* Technically not CPU capabilities, they do affect what instructions are * available. These are tested against BITS==64. */ -#define CPU_64 (1UL<<30) /* Only available in 64-bit mode */ -#define CPU_Not64 (1UL<<31) /* Not available (invalid) in 64-bit mode */ +#define CPU_64 (1UL<<30) /* Only available in 64-bit mode */ +#define CPU_Not64 (1UL<<31) /* Not available (invalid) in 64-bit mode */ typedef struct yasm_arch_x86 { - yasm_arch_base arch; /* base structure */ + yasm_arch_base arch; /* base structure */ /* What instructions/features are enabled? */ unsigned long cpu_enabled; unsigned int amd64_machine; enum { - X86_PARSER_NASM, - X86_PARSER_GAS + X86_PARSER_NASM, + X86_PARSER_GAS } parser; unsigned char mode_bits; unsigned char force_strict; @@ -85,17 +85,17 @@ typedef struct yasm_arch_x86 { */ typedef enum { X86_REG8 = 0x1<<4, - X86_REG8X = 0x2<<4, /* 64-bit mode only, REX prefix version of REG8 */ + X86_REG8X = 0x2<<4, /* 64-bit mode only, REX prefix version of REG8 */ X86_REG16 = 0x3<<4, X86_REG32 = 0x4<<4, - X86_REG64 = 0x5<<4, /* 64-bit mode only */ + X86_REG64 = 0x5<<4, /* 64-bit mode only */ X86_FPUREG = 0x6<<4, X86_MMXREG = 0x7<<4, X86_XMMREG = 0x8<<4, X86_CRREG = 0x9<<4, X86_DRREG = 0xA<<4, X86_TRREG = 0xB<<4, - X86_RIP = 0xC<<4 /* 64-bit mode only, always RIP (regnum ignored) */ + X86_RIP = 0xC<<4 /* 64-bit mode only, always RIP (regnum ignored) */ } x86_expritem_reg_size; typedef enum { @@ -134,98 +134,98 @@ typedef enum { * in 64-bit mode or if it wasn't needed to express reg. */ int yasm_x86__set_rex_from_reg(unsigned char *rex, unsigned char *low3, - uintptr_t reg, unsigned int bits, - x86_rex_bit_pos rexbit); + uintptr_t reg, unsigned int bits, + x86_rex_bit_pos rexbit); /* Effective address type */ typedef struct x86_effaddr { - yasm_effaddr ea; /* base structure */ + yasm_effaddr ea; /* base structure */ /* How the spare (register) bits in Mod/RM are handled: * Even if valid_modrm=0, the spare bits are still valid (don't overwrite!) * They're set in bytecode_create_insn(). */ unsigned char modrm; - unsigned char valid_modrm; /* 1 if Mod/RM byte currently valid, 0 if not */ - unsigned char need_modrm; /* 1 if Mod/RM byte needed, 0 if not */ + unsigned char valid_modrm; /* 1 if Mod/RM byte currently valid, 0 if not */ + unsigned char need_modrm; /* 1 if Mod/RM byte needed, 0 if not */ unsigned char sib; - unsigned char valid_sib; /* 1 if SIB byte currently valid, 0 if not */ - unsigned char need_sib; /* 1 if SIB byte needed, 0 if not, - 0xff if unknown */ + unsigned char valid_sib; /* 1 if SIB byte currently valid, 0 if not */ + unsigned char need_sib; /* 1 if SIB byte needed, 0 if not, + 0xff if unknown */ } x86_effaddr; void yasm_x86__ea_init(x86_effaddr *x86_ea, unsigned int spare, - yasm_bytecode *precbc); + yasm_bytecode *precbc); void yasm_x86__ea_set_disponly(x86_effaddr *x86_ea); x86_effaddr *yasm_x86__ea_create_reg(unsigned long reg, unsigned char *rex, - unsigned int bits); + unsigned int bits); x86_effaddr *yasm_x86__ea_create_imm (/*@keep@*/ yasm_expr *imm, unsigned int im_len); yasm_effaddr *yasm_x86__ea_create_expr(yasm_arch *arch, - /*@keep@*/ yasm_expr *e); + /*@keep@*/ yasm_expr *e); void yasm_x86__bc_insn_opersize_override(yasm_bytecode *bc, - unsigned int opersize); + unsigned int opersize); void yasm_x86__bc_insn_addrsize_override(yasm_bytecode *bc, - unsigned int addrsize); + unsigned int addrsize); void yasm_x86__bc_insn_set_lockrep_prefix(yasm_bytecode *bc, - unsigned int prefix); + unsigned int prefix); /* Bytecode types */ typedef struct x86_common { - unsigned char addrsize; /* 0 or =mode_bits => no override */ - unsigned char opersize; /* 0 or =mode_bits => no override */ - unsigned char lockrep_pre; /* 0 indicates no prefix */ + unsigned char addrsize; /* 0 or =mode_bits => no override */ + unsigned char opersize; /* 0 or =mode_bits => no override */ + unsigned char lockrep_pre; /* 0 indicates no prefix */ unsigned char mode_bits; } x86_common; typedef struct x86_opcode { - unsigned char opcode[3]; /* opcode */ + unsigned char opcode[3]; /* opcode */ unsigned char len; } x86_opcode; typedef struct x86_insn { - x86_common common; /* common x86 information */ + x86_common common; /* common x86 information */ x86_opcode opcode; /*@null@*/ x86_effaddr *x86_ea; /* effective address */ - /*@null@*/ yasm_value *imm; /* immediate or relative value */ + /*@null@*/ yasm_value *imm; /* immediate or relative value */ unsigned char def_opersize_64; /* default operand size in 64-bit mode */ unsigned char special_prefix; /* "special" prefix (0=none) */ - unsigned char rex; /* REX AMD64 extension, 0 if none, - 0xff if not allowed (high 8 bit reg used) */ + unsigned char rex; /* REX AMD64 extension, 0 if none, + 0xff if not allowed (high 8 bit reg used) */ /* Postponed (from parsing to later binding) action options. */ enum { - /* None */ - X86_POSTOP_NONE = 0, - - /* Instructions that take a sign-extended imm8 as well as imm values - * (eg, the arith instructions and a subset of the imul instructions) - * should set this and put the imm8 form as the "normal" opcode (in - * the first one or two bytes) and non-imm8 form in the second or - * third byte of the opcode. - */ - X86_POSTOP_SIGNEXT_IMM8, - - /* Override any attempt at address-size override to 16 bits, and never - * generate a prefix. This is used for the ENTER opcode. - */ - X86_POSTOP_ADDRESS16 + /* None */ + X86_POSTOP_NONE = 0, + + /* Instructions that take a sign-extended imm8 as well as imm values + * (eg, the arith instructions and a subset of the imul instructions) + * should set this and put the imm8 form as the "normal" opcode (in + * the first one or two bytes) and non-imm8 form in the second or + * third byte of the opcode. + */ + X86_POSTOP_SIGNEXT_IMM8, + + /* Override any attempt at address-size override to 16 bits, and never + * generate a prefix. This is used for the ENTER opcode. + */ + X86_POSTOP_ADDRESS16 } postop; } x86_insn; typedef struct x86_jmp { - x86_common common; /* common x86 information */ + x86_common common; /* common x86 information */ x86_opcode shortop, nearop; - yasm_value target; /* jump target */ + yasm_value target; /* jump target */ /* which opcode are we using? */ /* The *FORCED forms are specified in the source as such */ @@ -237,11 +237,11 @@ typedef struct x86_jmp { * This bytecode is not legal in 64-bit mode. */ typedef struct x86_jmpfar { - x86_common common; /* common x86 information */ + x86_common common; /* common x86 information */ x86_opcode opcode; - yasm_value segment; /* target segment */ - yasm_value offset; /* target offset */ + yasm_value segment; /* target segment */ + yasm_value offset; /* target offset */ } x86_jmpfar; void yasm_x86__bc_transform_insn(yasm_bytecode *bc, x86_insn *insn); @@ -260,7 +260,7 @@ int yasm_x86__expr_checkea int address16_op, unsigned char *rex, yasm_bytecode *bc); void yasm_x86__parse_cpu(yasm_arch_x86 *arch_x86, const char *cpuid, - size_t cpuid_len); + size_t cpuid_len); yasm_arch_insnprefix yasm_x86__parse_check_insnprefix (yasm_arch *arch, /*@out@*/ uintptr_t data[4], const char *id, diff --git a/modules/arch/x86/x86bc.c b/modules/arch/x86/x86bc.c index 88fa21c9..288fd31f 100644 --- a/modules/arch/x86/x86bc.c +++ b/modules/arch/x86/x86bc.c @@ -44,35 +44,35 @@ static void x86_ea_print(const yasm_effaddr *ea, FILE *f, int indent_level); static void x86_bc_insn_destroy(void *contents); static void x86_bc_insn_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int x86_bc_insn_calc_len(yasm_bytecode *bc, - yasm_bc_add_span_func add_span, - void *add_span_data); + yasm_bc_add_span_func add_span, + void *add_span_data); static int x86_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, - long new_val, /*@out@*/ long *neg_thres, - /*@out@*/ long *pos_thres); + long new_val, /*@out@*/ long *neg_thres, + /*@out@*/ long *pos_thres); static int x86_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, - void *d, yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + void *d, yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); static void x86_bc_jmp_destroy(void *contents); static void x86_bc_jmp_print(const void *contents, FILE *f, int indent_level); static int x86_bc_jmp_calc_len(yasm_bytecode *bc, - yasm_bc_add_span_func add_span, - void *add_span_data); + yasm_bc_add_span_func add_span, + void *add_span_data); static int x86_bc_jmp_expand(yasm_bytecode *bc, int span, long old_val, - long new_val, /*@out@*/ long *neg_thres, - /*@out@*/ long *pos_thres); + long new_val, /*@out@*/ long *neg_thres, + /*@out@*/ long *pos_thres); static int x86_bc_jmp_tobytes(yasm_bytecode *bc, unsigned char **bufp, - void *d, yasm_output_value_func output_value, - /*@null@*/ yasm_output_reloc_func output_reloc); + void *d, yasm_output_value_func output_value, + /*@null@*/ yasm_output_reloc_func output_reloc); static void x86_bc_jmpfar_destroy(void *contents); static void x86_bc_jmpfar_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int x86_bc_jmpfar_calc_len(yasm_bytecode *bc, - yasm_bc_add_span_func add_span, - void *add_span_data); + yasm_bc_add_span_func add_span, + void *add_span_data); static int x86_bc_jmpfar_tobytes (yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_output_value_func output_value, @@ -119,31 +119,31 @@ static const yasm_bytecode_callback x86_bc_callback_jmpfar = { int yasm_x86__set_rex_from_reg(unsigned char *rex, unsigned char *low3, - uintptr_t reg, unsigned int bits, - x86_rex_bit_pos rexbit) + uintptr_t reg, unsigned int bits, + x86_rex_bit_pos rexbit) { *low3 = (unsigned char)(reg&7); if (bits == 64) { - x86_expritem_reg_size size = (x86_expritem_reg_size)(reg & ~0xFUL); - - if (size == X86_REG8X || (reg & 0xF) >= 8) { - /* Check to make sure we can set it */ - if (*rex == 0xff) { - yasm_error_set(YASM_ERROR_TYPE, - N_("cannot use A/B/C/DH with instruction needing REX")); - return 1; - } - *rex |= 0x40 | (((reg & 8) >> 3) << rexbit); - } else if (size == X86_REG8 && (reg & 7) >= 4) { - /* AH/BH/CH/DH, so no REX allowed */ - if (*rex != 0 && *rex != 0xff) { - yasm_error_set(YASM_ERROR_TYPE, - N_("cannot use A/B/C/DH with instruction needing REX")); - return 1; - } - *rex = 0xff; /* Flag so we can NEVER set it (see above) */ - } + x86_expritem_reg_size size = (x86_expritem_reg_size)(reg & ~0xFUL); + + if (size == X86_REG8X || (reg & 0xF) >= 8) { + /* Check to make sure we can set it */ + if (*rex == 0xff) { + yasm_error_set(YASM_ERROR_TYPE, + N_("cannot use A/B/C/DH with instruction needing REX")); + return 1; + } + *rex |= 0x40 | (((reg & 8) >> 3) << rexbit); + } else if (size == X86_REG8 && (reg & 7) >= 4) { + /* AH/BH/CH/DH, so no REX allowed */ + if (*rex != 0 && *rex != 0xff) { + yasm_error_set(YASM_ERROR_TYPE, + N_("cannot use A/B/C/DH with instruction needing REX")); + return 1; + } + *rex = 0xff; /* Flag so we can NEVER set it (see above) */ + } } return 0; @@ -169,12 +169,12 @@ yasm_x86__bc_transform_jmpfar(yasm_bytecode *bc, x86_jmpfar *jmpfar) void yasm_x86__ea_init(x86_effaddr *x86_ea, unsigned int spare, - yasm_bytecode *precbc) + yasm_bytecode *precbc) { if (yasm_value_finalize(&x86_ea->ea.disp, precbc)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("effective address too complex")); - x86_ea->modrm &= 0xC7; /* zero spare/reg bits */ + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("effective address too complex")); + x86_ea->modrm &= 0xC7; /* zero spare/reg bits */ x86_ea->modrm |= (spare << 3) & 0x38; /* plug in provided bits */ } @@ -189,13 +189,13 @@ yasm_x86__ea_set_disponly(x86_effaddr *x86_ea) x86_effaddr * yasm_x86__ea_create_reg(unsigned long reg, unsigned char *rex, - unsigned int bits) + unsigned int bits) { x86_effaddr *x86_ea; unsigned char rm; if (yasm_x86__set_rex_from_reg(rex, &rm, reg, bits, X86_REX_B)) - return NULL; + return NULL; x86_ea = yasm_xmalloc(sizeof(x86_effaddr)); @@ -206,7 +206,7 @@ yasm_x86__ea_create_reg(unsigned long reg, unsigned char *rex, x86_ea->ea.nosplit = 0; x86_ea->ea.strong = 0; x86_ea->ea.segreg = 0; - x86_ea->modrm = 0xC0 | rm; /* Mod=11, R/M=Reg, Reg=0 */ + x86_ea->modrm = 0xC0 | rm; /* Mod=11, R/M=Reg, Reg=0 */ x86_ea->valid_modrm = 1; x86_ea->need_modrm = 1; x86_ea->sib = 0; @@ -226,19 +226,19 @@ yasm_x86__ea_create_expr(yasm_arch *arch, yasm_expr *e) x86_ea->ea.callback = &x86_ea_callback; if (arch_x86->parser == X86_PARSER_GAS) { - /* Need to change foo+rip into foo wrt rip. - * Note this assumes a particular ordering coming from the parser - * to work (it's not very smart)! - */ - if (e->op == YASM_EXPR_ADD && e->terms[0].type == YASM_EXPR_REG - && e->terms[0].data.reg == X86_RIP) { - /* replace register with 0 */ - e->terms[0].type = YASM_EXPR_INT; - e->terms[0].data.intn = yasm_intnum_create_uint(0); - /* build new wrt expression */ - e = yasm_expr_create(YASM_EXPR_WRT, yasm_expr_expr(e), - yasm_expr_reg(X86_RIP), e->line); - } + /* Need to change foo+rip into foo wrt rip. + * Note this assumes a particular ordering coming from the parser + * to work (it's not very smart)! + */ + if (e->op == YASM_EXPR_ADD && e->terms[0].type == YASM_EXPR_REG + && e->terms[0].data.reg == X86_RIP) { + /* replace register with 0 */ + e->terms[0].type = YASM_EXPR_INT; + e->terms[0].data.intn = yasm_intnum_create_uint(0); + /* build new wrt expression */ + e = yasm_expr_create(YASM_EXPR_WRT, yasm_expr_expr(e), + yasm_expr_reg(X86_RIP), e->line); + } } yasm_value_initialize(&x86_ea->ea.disp, e, 0); x86_ea->ea.need_nonzero_len = 0; @@ -286,56 +286,56 @@ yasm_x86__ea_create_imm(yasm_expr *imm, unsigned int im_len) void yasm_x86__bc_apply_prefixes(x86_common *common, unsigned char *rex, - int num_prefixes, uintptr_t **prefixes) + int num_prefixes, uintptr_t **prefixes) { int i; int first = 1; for (i=0; ilockrep_pre != 0) - yasm_warn_set(YASM_WARN_GENERAL, - N_("multiple LOCK or REP prefixes, using leftmost")); - common->lockrep_pre = (unsigned char)prefixes[i][1]; - break; - case X86_ADDRSIZE: - common->addrsize = (unsigned char)prefixes[i][1]; - break; - case X86_OPERSIZE: - common->opersize = (unsigned char)prefixes[i][1]; - break; - case X86_SEGREG: - /* This is a hack.. we should really be putting this in the - * the effective address! - */ - common->lockrep_pre = (unsigned char)prefixes[i][1]; - break; - case X86_REX: - if (!rex) - yasm_warn_set(YASM_WARN_GENERAL, - N_("ignoring REX prefix on jump")); - else if (*rex == 0xff) - yasm_warn_set(YASM_WARN_GENERAL, - N_("REX prefix not allowed on this instruction, ignoring")); - else { - if (*rex != 0) { - if (first) - yasm_warn_set(YASM_WARN_GENERAL, - N_("overriding generated REX prefix")); - else - yasm_warn_set(YASM_WARN_GENERAL, - N_("multiple REX prefixes, using leftmost")); - } - /* Here we assume that we can't get this prefix in non - * 64 bit mode due to checks in parse_check_prefix(). - */ - common->mode_bits = 64; - *rex = (unsigned char)prefixes[i][1]; - } - first = 0; - break; - } + switch ((x86_parse_insn_prefix)prefixes[i][0]) { + case X86_LOCKREP: + if (common->lockrep_pre != 0) + yasm_warn_set(YASM_WARN_GENERAL, + N_("multiple LOCK or REP prefixes, using leftmost")); + common->lockrep_pre = (unsigned char)prefixes[i][1]; + break; + case X86_ADDRSIZE: + common->addrsize = (unsigned char)prefixes[i][1]; + break; + case X86_OPERSIZE: + common->opersize = (unsigned char)prefixes[i][1]; + break; + case X86_SEGREG: + /* This is a hack.. we should really be putting this in the + * the effective address! + */ + common->lockrep_pre = (unsigned char)prefixes[i][1]; + break; + case X86_REX: + if (!rex) + yasm_warn_set(YASM_WARN_GENERAL, + N_("ignoring REX prefix on jump")); + else if (*rex == 0xff) + yasm_warn_set(YASM_WARN_GENERAL, + N_("REX prefix not allowed on this instruction, ignoring")); + else { + if (*rex != 0) { + if (first) + yasm_warn_set(YASM_WARN_GENERAL, + N_("overriding generated REX prefix")); + else + yasm_warn_set(YASM_WARN_GENERAL, + N_("multiple REX prefixes, using leftmost")); + } + /* Here we assume that we can't get this prefix in non + * 64 bit mode due to checks in parse_check_prefix(). + */ + common->mode_bits = 64; + *rex = (unsigned char)prefixes[i][1]; + } + first = 0; + break; + } } } @@ -344,10 +344,10 @@ x86_bc_insn_destroy(void *contents) { x86_insn *insn = (x86_insn *)contents; if (insn->x86_ea) - yasm_ea_destroy((yasm_effaddr *)insn->x86_ea); + yasm_ea_destroy((yasm_effaddr *)insn->x86_ea); if (insn->imm) { - yasm_value_delete(insn->imm); - yasm_xfree(insn->imm); + yasm_value_delete(insn->imm); + yasm_xfree(insn->imm); } yasm_xfree(contents); } @@ -379,34 +379,34 @@ x86_ea_print(const yasm_effaddr *ea, FILE *f, int indent_level) { const x86_effaddr *x86_ea = (const x86_effaddr *)ea; fprintf(f, "%*sSegmentOv=%02x\n", indent_level, "", - (unsigned int)x86_ea->ea.segreg); + (unsigned int)x86_ea->ea.segreg); fprintf(f, "%*sModRM=%03o ValidRM=%u NeedRM=%u\n", indent_level, "", - (unsigned int)x86_ea->modrm, (unsigned int)x86_ea->valid_modrm, - (unsigned int)x86_ea->need_modrm); + (unsigned int)x86_ea->modrm, (unsigned int)x86_ea->valid_modrm, + (unsigned int)x86_ea->need_modrm); fprintf(f, "%*sSIB=%03o ValidSIB=%u NeedSIB=%u\n", indent_level, "", - (unsigned int)x86_ea->sib, (unsigned int)x86_ea->valid_sib, - (unsigned int)x86_ea->need_sib); + (unsigned int)x86_ea->sib, (unsigned int)x86_ea->valid_sib, + (unsigned int)x86_ea->need_sib); } static void x86_common_print(const x86_common *common, FILE *f, int indent_level) { fprintf(f, "%*sAddrSize=%u OperSize=%u LockRepPre=%02x BITS=%u\n", - indent_level, "", - (unsigned int)common->addrsize, - (unsigned int)common->opersize, - (unsigned int)common->lockrep_pre, - (unsigned int)common->mode_bits); + indent_level, "", + (unsigned int)common->addrsize, + (unsigned int)common->opersize, + (unsigned int)common->lockrep_pre, + (unsigned int)common->mode_bits); } static void x86_opcode_print(const x86_opcode *opcode, FILE *f, int indent_level) { fprintf(f, "%*sOpcode: %02x %02x %02x OpLen=%u\n", indent_level, "", - (unsigned int)opcode->opcode[0], - (unsigned int)opcode->opcode[1], - (unsigned int)opcode->opcode[2], - (unsigned int)opcode->len); + (unsigned int)opcode->opcode[0], + (unsigned int)opcode->opcode[1], + (unsigned int)opcode->opcode[2], + (unsigned int)opcode->len); } static void @@ -417,25 +417,25 @@ x86_bc_insn_print(const void *contents, FILE *f, int indent_level) fprintf(f, "%*s_Instruction_\n", indent_level, ""); fprintf(f, "%*sEffective Address:", indent_level, ""); if (insn->x86_ea) { - fprintf(f, "\n"); - yasm_ea_print((yasm_effaddr *)insn->x86_ea, f, indent_level+1); + fprintf(f, "\n"); + yasm_ea_print((yasm_effaddr *)insn->x86_ea, f, indent_level+1); } else - fprintf(f, " (nil)\n"); + fprintf(f, " (nil)\n"); fprintf(f, "%*sImmediate Value:", indent_level, ""); if (!insn->imm) - fprintf(f, " (nil)\n"); + fprintf(f, " (nil)\n"); else { - indent_level++; - fprintf(f, "\n"); - yasm_value_print(insn->imm, f, indent_level); - indent_level--; + indent_level++; + fprintf(f, "\n"); + yasm_value_print(insn->imm, f, indent_level); + indent_level--; } x86_opcode_print(&insn->opcode, f, indent_level); x86_common_print(&insn->common, f, indent_level); fprintf(f, "%*sSpPre=%02x REX=%03o PostOp=%u\n", indent_level, "", - (unsigned int)insn->special_prefix, - (unsigned int)insn->rex, - (unsigned int)insn->postop); + (unsigned int)insn->special_prefix, + (unsigned int)insn->rex, + (unsigned int)insn->postop); } static void @@ -452,34 +452,34 @@ x86_bc_jmp_print(const void *contents, FILE *f, int indent_level) */ fprintf(f, "\n%*sShort Form:\n", indent_level, ""); if (jmp->shortop.len == 0) - fprintf(f, "%*sNone\n", indent_level+1, ""); + fprintf(f, "%*sNone\n", indent_level+1, ""); else - x86_opcode_print(&jmp->shortop, f, indent_level+1); + x86_opcode_print(&jmp->shortop, f, indent_level+1); fprintf(f, "%*sNear Form:\n", indent_level, ""); if (jmp->nearop.len == 0) - fprintf(f, "%*sNone\n", indent_level+1, ""); + fprintf(f, "%*sNone\n", indent_level+1, ""); else - x86_opcode_print(&jmp->nearop, f, indent_level+1); + x86_opcode_print(&jmp->nearop, f, indent_level+1); fprintf(f, "%*sOpSel=", indent_level, ""); switch (jmp->op_sel) { - case JMP_NONE: - fprintf(f, "None"); - break; - case JMP_SHORT: - fprintf(f, "Short"); - break; - case JMP_NEAR: - fprintf(f, "Near"); - break; - case JMP_SHORT_FORCED: - fprintf(f, "Forced Short"); - break; - case JMP_NEAR_FORCED: - fprintf(f, "Forced Near"); - break; - default: - fprintf(f, "UNKNOWN!!"); - break; + case JMP_NONE: + fprintf(f, "None"); + break; + case JMP_SHORT: + fprintf(f, "Short"); + break; + case JMP_NEAR: + fprintf(f, "Near"); + break; + case JMP_SHORT_FORCED: + fprintf(f, "Forced Short"); + break; + case JMP_NEAR_FORCED: + fprintf(f, "Forced Near"); + break; + default: + fprintf(f, "UNKNOWN!!"); + break; } x86_common_print(&jmp->common, f, indent_level); } @@ -504,108 +504,108 @@ x86_common_calc_len(const x86_common *common) unsigned int len = 0; if (common->addrsize != 0 && common->addrsize != common->mode_bits) - len++; + len++; if (common->opersize != 0 && - ((common->mode_bits != 64 && common->opersize != common->mode_bits) || - (common->mode_bits == 64 && common->opersize == 16))) - len++; + ((common->mode_bits != 64 && common->opersize != common->mode_bits) || + (common->mode_bits == 64 && common->opersize == 16))) + len++; if (common->lockrep_pre != 0) - len++; + len++; return len; } static int x86_bc_insn_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { x86_insn *insn = (x86_insn *)bc->contents; x86_effaddr *x86_ea = insn->x86_ea; yasm_value *imm = insn->imm; if (x86_ea) { - /* Check validity of effective address and calc R/M bits of - * Mod/RM byte and SIB byte. We won't know the Mod field - * of the Mod/RM byte until we know more about the - * displacement. - */ - if (yasm_x86__expr_checkea(x86_ea, &insn->common.addrsize, - insn->common.mode_bits, insn->postop == X86_POSTOP_ADDRESS16, - &insn->rex, bc)) - /* failed, don't bother checking rest of insn */ - return -1; - - if (x86_ea->ea.disp.size == 0 && x86_ea->ea.need_nonzero_len) { - /* Handle unknown case, default to byte-sized and set as - * critical expression. - */ - x86_ea->ea.disp.size = 8; - add_span(add_span_data, bc, 1, &x86_ea->ea.disp, -128, 127); - } - bc->len += x86_ea->ea.disp.size/8; - - /* Handle address16 postop case */ - if (insn->postop == X86_POSTOP_ADDRESS16) - insn->common.addrsize = 0; - - /* Compute length of ea and add to total */ - bc->len += x86_ea->need_modrm + (x86_ea->need_sib ? 1:0); - bc->len += (x86_ea->ea.segreg != 0) ? 1 : 0; + /* Check validity of effective address and calc R/M bits of + * Mod/RM byte and SIB byte. We won't know the Mod field + * of the Mod/RM byte until we know more about the + * displacement. + */ + if (yasm_x86__expr_checkea(x86_ea, &insn->common.addrsize, + insn->common.mode_bits, insn->postop == X86_POSTOP_ADDRESS16, + &insn->rex, bc)) + /* failed, don't bother checking rest of insn */ + return -1; + + if (x86_ea->ea.disp.size == 0 && x86_ea->ea.need_nonzero_len) { + /* Handle unknown case, default to byte-sized and set as + * critical expression. + */ + x86_ea->ea.disp.size = 8; + add_span(add_span_data, bc, 1, &x86_ea->ea.disp, -128, 127); + } + bc->len += x86_ea->ea.disp.size/8; + + /* Handle address16 postop case */ + if (insn->postop == X86_POSTOP_ADDRESS16) + insn->common.addrsize = 0; + + /* Compute length of ea and add to total */ + bc->len += x86_ea->need_modrm + (x86_ea->need_sib ? 1:0); + bc->len += (x86_ea->ea.segreg != 0) ? 1 : 0; } if (imm) { - unsigned int immlen = imm->size; - - /* TODO: check imm->len vs. sized len from expr? */ - - /* Handle signext_imm8 postop special-casing */ - if (insn->postop == X86_POSTOP_SIGNEXT_IMM8) { - /*@null@*/ /*@only@*/ yasm_intnum *num; - num = yasm_value_get_intnum(imm, NULL, 0); - - if (!num) { - /* Unknown; default to byte form and set as critical - * expression. - */ - immlen = 8; - add_span(add_span_data, bc, 2, imm, -128, 127); - } else { - if (yasm_intnum_in_range(num, -128, 127)) { - /* We can use the sign-extended byte form: shorten - * the immediate length to 1 and make the byte form - * permanent. - */ - imm->size = 8; - imm->sign = 1; - immlen = 8; - } else { - /* We can't. Copy over the word-sized opcode. */ - insn->opcode.opcode[0] = - insn->opcode.opcode[insn->opcode.len]; - insn->opcode.len = 1; - } - insn->postop = X86_POSTOP_NONE; - yasm_intnum_destroy(num); - } - } - - bc->len += immlen/8; + unsigned int immlen = imm->size; + + /* TODO: check imm->len vs. sized len from expr? */ + + /* Handle signext_imm8 postop special-casing */ + if (insn->postop == X86_POSTOP_SIGNEXT_IMM8) { + /*@null@*/ /*@only@*/ yasm_intnum *num; + num = yasm_value_get_intnum(imm, NULL, 0); + + if (!num) { + /* Unknown; default to byte form and set as critical + * expression. + */ + immlen = 8; + add_span(add_span_data, bc, 2, imm, -128, 127); + } else { + if (yasm_intnum_in_range(num, -128, 127)) { + /* We can use the sign-extended byte form: shorten + * the immediate length to 1 and make the byte form + * permanent. + */ + imm->size = 8; + imm->sign = 1; + immlen = 8; + } else { + /* We can't. Copy over the word-sized opcode. */ + insn->opcode.opcode[0] = + insn->opcode.opcode[insn->opcode.len]; + insn->opcode.len = 1; + } + insn->postop = X86_POSTOP_NONE; + yasm_intnum_destroy(num); + } + } + + bc->len += immlen/8; } bc->len += insn->opcode.len; bc->len += x86_common_calc_len(&insn->common); bc->len += (insn->special_prefix != 0) ? 1:0; if (insn->rex != 0xff && - (insn->rex != 0 || - (insn->common.mode_bits == 64 && insn->common.opersize == 64 && - insn->def_opersize_64 != 64))) - bc->len++; + (insn->rex != 0 || + (insn->common.mode_bits == 64 && insn->common.opersize == 64 && + insn->def_opersize_64 != 64))) + bc->len++; return 0; } static int x86_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { x86_insn *insn = (x86_insn *)bc->contents; x86_effaddr *x86_ea = insn->x86_ea; @@ -613,27 +613,27 @@ x86_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, long new_val, yasm_value *imm = insn->imm; if (ea && span == 1) { - /* Change displacement length into word-sized */ - if (ea->disp.size == 8) { - ea->disp.size = (insn->common.addrsize == 16) ? 16 : 32; - x86_ea->modrm &= ~0300; - x86_ea->modrm |= 0200; - bc->len--; - bc->len += ea->disp.size/8; - } + /* Change displacement length into word-sized */ + if (ea->disp.size == 8) { + ea->disp.size = (insn->common.addrsize == 16) ? 16 : 32; + x86_ea->modrm &= ~0300; + x86_ea->modrm |= 0200; + bc->len--; + bc->len += ea->disp.size/8; + } } if (imm && span == 2) { - if (insn->postop == X86_POSTOP_SIGNEXT_IMM8) { - /* Update bc->len for new opcode and immediate size */ - bc->len -= insn->opcode.len; - bc->len += imm->size/8; - - /* Change to the word-sized opcode */ - insn->opcode.opcode[0] = insn->opcode.opcode[insn->opcode.len]; - insn->opcode.len = 1; - insn->postop = X86_POSTOP_NONE; - } + if (insn->postop == X86_POSTOP_SIGNEXT_IMM8) { + /* Update bc->len for new opcode and immediate size */ + bc->len -= insn->opcode.len; + bc->len += imm->size/8; + + /* Change to the word-sized opcode */ + insn->opcode.opcode[0] = insn->opcode.opcode[insn->opcode.len]; + insn->opcode.len = 1; + insn->postop = X86_POSTOP_NONE; + } } return 0; @@ -641,7 +641,7 @@ x86_bc_insn_expand(yasm_bytecode *bc, int span, long old_val, long new_val, static int x86_bc_jmp_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { x86_jmp *jmp = (x86_jmp *)bc->contents; yasm_bytecode *target_prevbc; @@ -649,86 +649,86 @@ x86_bc_jmp_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, /* As opersize may be 0, figure out its "real" value. */ opersize = (jmp->common.opersize == 0) ? - jmp->common.mode_bits : jmp->common.opersize; + jmp->common.mode_bits : jmp->common.opersize; bc->len += x86_common_calc_len(&jmp->common); if (jmp->op_sel == JMP_NEAR_FORCED || jmp->shortop.len == 0) { - if (jmp->nearop.len == 0) { - yasm_error_set(YASM_ERROR_TYPE, N_("near jump does not exist")); - return -1; - } - - /* Near jump, no spans needed */ - if (jmp->shortop.len == 0) - jmp->op_sel = JMP_NEAR; - bc->len += jmp->nearop.len; - bc->len += (opersize == 16) ? 2 : 4; - return 0; + if (jmp->nearop.len == 0) { + yasm_error_set(YASM_ERROR_TYPE, N_("near jump does not exist")); + return -1; + } + + /* Near jump, no spans needed */ + if (jmp->shortop.len == 0) + jmp->op_sel = JMP_NEAR; + bc->len += jmp->nearop.len; + bc->len += (opersize == 16) ? 2 : 4; + return 0; } if (jmp->op_sel == JMP_SHORT_FORCED || jmp->nearop.len == 0) { - if (jmp->shortop.len == 0) { - yasm_error_set(YASM_ERROR_TYPE, N_("short jump does not exist")); - return -1; - } - - /* We want to be sure to error if we exceed short length, so - * put it in as a dependent expression (falling through). - */ + if (jmp->shortop.len == 0) { + yasm_error_set(YASM_ERROR_TYPE, N_("short jump does not exist")); + return -1; + } + + /* We want to be sure to error if we exceed short length, so + * put it in as a dependent expression (falling through). + */ } if (jmp->target.rel - && (!yasm_symrec_get_label(jmp->target.rel, &target_prevbc) - || target_prevbc->section != bc->section)) { - /* External or out of segment, so we can't check distance. - * Allowing short jumps depends on the objfmt supporting - * 8-bit relocs. While most don't, some might, so allow it here. - * Otherwise default to word-sized. - * The objfmt will error if not supported. - */ - if (jmp->op_sel == JMP_SHORT_FORCED || jmp->nearop.len == 0) { - if (jmp->op_sel == JMP_NONE) - jmp->op_sel = JMP_SHORT; - bc->len += jmp->shortop.len + 1; - } else { - jmp->op_sel = JMP_NEAR; - bc->len += jmp->nearop.len; - bc->len += (opersize == 16) ? 2 : 4; - } - return 0; + && (!yasm_symrec_get_label(jmp->target.rel, &target_prevbc) + || target_prevbc->section != bc->section)) { + /* External or out of segment, so we can't check distance. + * Allowing short jumps depends on the objfmt supporting + * 8-bit relocs. While most don't, some might, so allow it here. + * Otherwise default to word-sized. + * The objfmt will error if not supported. + */ + if (jmp->op_sel == JMP_SHORT_FORCED || jmp->nearop.len == 0) { + if (jmp->op_sel == JMP_NONE) + jmp->op_sel = JMP_SHORT; + bc->len += jmp->shortop.len + 1; + } else { + jmp->op_sel = JMP_NEAR; + bc->len += jmp->nearop.len; + bc->len += (opersize == 16) ? 2 : 4; + } + return 0; } /* Default to short jump and generate span */ if (jmp->op_sel == JMP_NONE) - jmp->op_sel = JMP_SHORT; + jmp->op_sel = JMP_SHORT; bc->len += jmp->shortop.len + 1; add_span(add_span_data, bc, 1, &jmp->target, -128+(long)bc->len, - 127+(long)bc->len); + 127+(long)bc->len); return 0; } static int x86_bc_jmp_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { x86_jmp *jmp = (x86_jmp *)bc->contents; unsigned char opersize; if (span != 1) - yasm_internal_error(N_("unrecognized span id")); + yasm_internal_error(N_("unrecognized span id")); /* As opersize may be 0, figure out its "real" value. */ opersize = (jmp->common.opersize == 0) ? - jmp->common.mode_bits : jmp->common.opersize; + jmp->common.mode_bits : jmp->common.opersize; if (jmp->op_sel == JMP_SHORT_FORCED || jmp->nearop.len == 0) { - yasm_error_set(YASM_ERROR_VALUE, N_("short jump out of range")); - return -1; + yasm_error_set(YASM_ERROR_VALUE, N_("short jump out of range")); + return -1; } if (jmp->op_sel == JMP_NEAR) - yasm_internal_error(N_("trying to expand an already-near jump")); + yasm_internal_error(N_("trying to expand an already-near jump")); /* Upgrade to a near jump */ jmp->op_sel = JMP_NEAR; @@ -741,16 +741,16 @@ x86_bc_jmp_expand(yasm_bytecode *bc, int span, long old_val, long new_val, static int x86_bc_jmpfar_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { x86_jmpfar *jmpfar = (x86_jmpfar *)bc->contents; unsigned char opersize; opersize = (jmpfar->common.opersize == 0) ? - jmpfar->common.mode_bits : jmpfar->common.opersize; + jmpfar->common.mode_bits : jmpfar->common.opersize; bc->len += jmpfar->opcode.len; - bc->len += 2; /* segment */ + bc->len += 2; /* segment */ bc->len += (opersize == 16) ? 2 : 4; bc->len += x86_common_calc_len(&jmpfar->common); @@ -759,18 +759,18 @@ x86_bc_jmpfar_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static void x86_common_tobytes(const x86_common *common, unsigned char **bufp, - unsigned int segreg) + unsigned int segreg) { if (segreg != 0) - YASM_WRITE_8(*bufp, (unsigned char)segreg); + YASM_WRITE_8(*bufp, (unsigned char)segreg); if (common->addrsize != 0 && common->addrsize != common->mode_bits) - YASM_WRITE_8(*bufp, 0x67); + YASM_WRITE_8(*bufp, 0x67); if (common->opersize != 0 && - ((common->mode_bits != 64 && common->opersize != common->mode_bits) || - (common->mode_bits == 64 && common->opersize == 16))) - YASM_WRITE_8(*bufp, 0x66); + ((common->mode_bits != 64 && common->opersize != common->mode_bits) || + (common->mode_bits == 64 && common->opersize == 16))) + YASM_WRITE_8(*bufp, 0x66); if (common->lockrep_pre != 0) - YASM_WRITE_8(*bufp, common->lockrep_pre); + YASM_WRITE_8(*bufp, common->lockrep_pre); } static void @@ -778,13 +778,13 @@ x86_opcode_tobytes(const x86_opcode *opcode, unsigned char **bufp) { unsigned int i; for (i=0; ilen; i++) - YASM_WRITE_8(*bufp, opcode->opcode[i]); + YASM_WRITE_8(*bufp, opcode->opcode[i]); } static int x86_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { x86_insn *insn = (x86_insn *)bc->contents; /*@null@*/ x86_effaddr *x86_ea = (x86_effaddr *)insn->x86_ea; @@ -793,19 +793,19 @@ x86_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Prefixes */ x86_common_tobytes(&insn->common, bufp, - x86_ea ? (unsigned int)(x86_ea->ea.segreg>>8) : 0); + x86_ea ? (unsigned int)(x86_ea->ea.segreg>>8) : 0); if (insn->special_prefix != 0) - YASM_WRITE_8(*bufp, insn->special_prefix); + YASM_WRITE_8(*bufp, insn->special_prefix); if (insn->rex != 0xff) { - if (insn->common.mode_bits == 64 && insn->common.opersize == 64 && - insn->def_opersize_64 != 64) - insn->rex |= 0x48; - if (insn->rex != 0) { - if (insn->common.mode_bits != 64) - yasm_internal_error( - N_("x86: got a REX prefix in non-64-bit mode")); - YASM_WRITE_8(*bufp, insn->rex); - } + if (insn->common.mode_bits == 64 && insn->common.opersize == 64 && + insn->def_opersize_64 != 64) + insn->rex |= 0x48; + if (insn->rex != 0) { + if (insn->common.mode_bits != 64) + yasm_internal_error( + N_("x86: got a REX prefix in non-64-bit mode")); + YASM_WRITE_8(*bufp, insn->rex); + } } /* Opcode */ @@ -815,57 +815,57 @@ x86_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, * displacement (if required). */ if (x86_ea) { - if (x86_ea->need_modrm) { - if (!x86_ea->valid_modrm) - yasm_internal_error(N_("invalid Mod/RM in x86 tobytes_insn")); - YASM_WRITE_8(*bufp, x86_ea->modrm); - } - - if (x86_ea->need_sib) { - if (!x86_ea->valid_sib) - yasm_internal_error(N_("invalid SIB in x86 tobytes_insn")); - YASM_WRITE_8(*bufp, x86_ea->sib); - } - - if (x86_ea->ea.need_disp) { - unsigned int disp_len = x86_ea->ea.disp.size/8; - - if (x86_ea->ea.disp.ip_rel) { - /* Adjust relative displacement to end of bytecode */ - /*@only@*/ yasm_intnum *delta; - delta = yasm_intnum_create_int(-(long)bc->len); - if (!x86_ea->ea.disp.abs) - x86_ea->ea.disp.abs = - yasm_expr_create_ident(yasm_expr_int(delta), bc->line); - else - x86_ea->ea.disp.abs = - yasm_expr_create(YASM_EXPR_ADD, - yasm_expr_expr(x86_ea->ea.disp.abs), - yasm_expr_int(delta), bc->line); - } - if (output_value(&x86_ea->ea.disp, *bufp, disp_len, - (unsigned long)(*bufp-bufp_orig), bc, 1, d)) - return 1; - *bufp += disp_len; - } + if (x86_ea->need_modrm) { + if (!x86_ea->valid_modrm) + yasm_internal_error(N_("invalid Mod/RM in x86 tobytes_insn")); + YASM_WRITE_8(*bufp, x86_ea->modrm); + } + + if (x86_ea->need_sib) { + if (!x86_ea->valid_sib) + yasm_internal_error(N_("invalid SIB in x86 tobytes_insn")); + YASM_WRITE_8(*bufp, x86_ea->sib); + } + + if (x86_ea->ea.need_disp) { + unsigned int disp_len = x86_ea->ea.disp.size/8; + + if (x86_ea->ea.disp.ip_rel) { + /* Adjust relative displacement to end of bytecode */ + /*@only@*/ yasm_intnum *delta; + delta = yasm_intnum_create_int(-(long)bc->len); + if (!x86_ea->ea.disp.abs) + x86_ea->ea.disp.abs = + yasm_expr_create_ident(yasm_expr_int(delta), bc->line); + else + x86_ea->ea.disp.abs = + yasm_expr_create(YASM_EXPR_ADD, + yasm_expr_expr(x86_ea->ea.disp.abs), + yasm_expr_int(delta), bc->line); + } + if (output_value(&x86_ea->ea.disp, *bufp, disp_len, + (unsigned long)(*bufp-bufp_orig), bc, 1, d)) + return 1; + *bufp += disp_len; + } } /* Immediate (if required) */ if (imm) { - unsigned int imm_len; - if (insn->postop == X86_POSTOP_SIGNEXT_IMM8) { - /* If we got here with this postop still set, we need to force - * imm size to 8 here. - */ - imm->size = 8; - imm->sign = 1; - imm_len = 1; - } else - imm_len = imm->size/8; - if (output_value(imm, *bufp, imm_len, (unsigned long)(*bufp-bufp_orig), - bc, 1, d)) - return 1; - *bufp += imm_len; + unsigned int imm_len; + if (insn->postop == X86_POSTOP_SIGNEXT_IMM8) { + /* If we got here with this postop still set, we need to force + * imm size to 8 here. + */ + imm->size = 8; + imm->sign = 1; + imm_len = 1; + } else + imm_len = imm->size/8; + if (output_value(imm, *bufp, imm_len, (unsigned long)(*bufp-bufp_orig), + bc, 1, d)) + return 1; + *bufp += imm_len; } return 0; @@ -873,8 +873,8 @@ x86_bc_insn_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, static int x86_bc_jmp_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { x86_jmp *jmp = (x86_jmp *)bc->contents; unsigned char opersize; @@ -887,79 +887,79 @@ x86_bc_jmp_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* As opersize may be 0, figure out its "real" value. */ opersize = (jmp->common.opersize == 0) ? - jmp->common.mode_bits : jmp->common.opersize; + jmp->common.mode_bits : jmp->common.opersize; /* Check here again to see if forms are actually legal. */ switch (jmp->op_sel) { - case JMP_SHORT_FORCED: - case JMP_SHORT: - /* 1 byte relative displacement */ - if (jmp->shortop.len == 0) - yasm_internal_error(N_("short jump does not exist")); - - /* Opcode */ - x86_opcode_tobytes(&jmp->shortop, bufp); - - /* Adjust relative displacement to end of bytecode */ - delta = yasm_intnum_create_int(-(long)bc->len); - if (!jmp->target.abs) - jmp->target.abs = yasm_expr_create_ident(yasm_expr_int(delta), - bc->line); - else - jmp->target.abs = - yasm_expr_create(YASM_EXPR_ADD, - yasm_expr_expr(jmp->target.abs), - yasm_expr_int(delta), bc->line); - - jmp->target.size = 8; - if (output_value(&jmp->target, *bufp, 1, - (unsigned long)(*bufp-bufp_orig), bc, -1, d)) - return 1; - *bufp += 1; - break; - case JMP_NEAR_FORCED: - case JMP_NEAR: - /* 2/4 byte relative displacement (depending on operand size) */ - if (jmp->nearop.len == 0) { - yasm_error_set(YASM_ERROR_TYPE, - N_("near jump does not exist")); - return 1; - } - - /* Opcode */ - x86_opcode_tobytes(&jmp->nearop, bufp); - - i = (opersize == 16) ? 2 : 4; - - /* Adjust relative displacement to end of bytecode */ - delta = yasm_intnum_create_int(-(long)bc->len); - if (!jmp->target.abs) - jmp->target.abs = yasm_expr_create_ident(yasm_expr_int(delta), - bc->line); - else - jmp->target.abs = - yasm_expr_create(YASM_EXPR_ADD, - yasm_expr_expr(jmp->target.abs), - yasm_expr_int(delta), bc->line); - - jmp->target.size = i*8; - if (output_value(&jmp->target, *bufp, i, - (unsigned long)(*bufp-bufp_orig), bc, -1, d)) - return 1; - *bufp += i; - break; - case JMP_NONE: - yasm_internal_error(N_("jump op_sel cannot be JMP_NONE in tobytes")); - default: - yasm_internal_error(N_("unrecognized relative jump op_sel")); + case JMP_SHORT_FORCED: + case JMP_SHORT: + /* 1 byte relative displacement */ + if (jmp->shortop.len == 0) + yasm_internal_error(N_("short jump does not exist")); + + /* Opcode */ + x86_opcode_tobytes(&jmp->shortop, bufp); + + /* Adjust relative displacement to end of bytecode */ + delta = yasm_intnum_create_int(-(long)bc->len); + if (!jmp->target.abs) + jmp->target.abs = yasm_expr_create_ident(yasm_expr_int(delta), + bc->line); + else + jmp->target.abs = + yasm_expr_create(YASM_EXPR_ADD, + yasm_expr_expr(jmp->target.abs), + yasm_expr_int(delta), bc->line); + + jmp->target.size = 8; + if (output_value(&jmp->target, *bufp, 1, + (unsigned long)(*bufp-bufp_orig), bc, -1, d)) + return 1; + *bufp += 1; + break; + case JMP_NEAR_FORCED: + case JMP_NEAR: + /* 2/4 byte relative displacement (depending on operand size) */ + if (jmp->nearop.len == 0) { + yasm_error_set(YASM_ERROR_TYPE, + N_("near jump does not exist")); + return 1; + } + + /* Opcode */ + x86_opcode_tobytes(&jmp->nearop, bufp); + + i = (opersize == 16) ? 2 : 4; + + /* Adjust relative displacement to end of bytecode */ + delta = yasm_intnum_create_int(-(long)bc->len); + if (!jmp->target.abs) + jmp->target.abs = yasm_expr_create_ident(yasm_expr_int(delta), + bc->line); + else + jmp->target.abs = + yasm_expr_create(YASM_EXPR_ADD, + yasm_expr_expr(jmp->target.abs), + yasm_expr_int(delta), bc->line); + + jmp->target.size = i*8; + if (output_value(&jmp->target, *bufp, i, + (unsigned long)(*bufp-bufp_orig), bc, -1, d)) + return 1; + *bufp += i; + break; + case JMP_NONE: + yasm_internal_error(N_("jump op_sel cannot be JMP_NONE in tobytes")); + default: + yasm_internal_error(N_("unrecognized relative jump op_sel")); } return 0; } static int x86_bc_jmpfar_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - /*@unused@*/ yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + /*@unused@*/ yasm_output_reloc_func output_reloc) { x86_jmpfar *jmpfar = (x86_jmpfar *)bc->contents; unsigned int i; @@ -971,19 +971,19 @@ x86_bc_jmpfar_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* As opersize may be 0, figure out its "real" value. */ opersize = (jmpfar->common.opersize == 0) ? - jmpfar->common.mode_bits : jmpfar->common.opersize; + jmpfar->common.mode_bits : jmpfar->common.opersize; /* Absolute displacement: segment and offset */ i = (opersize == 16) ? 2 : 4; jmpfar->offset.size = i*8; if (output_value(&jmpfar->offset, *bufp, i, - (unsigned long)(*bufp-bufp_orig), bc, 1, d)) - return 1; + (unsigned long)(*bufp-bufp_orig), bc, 1, d)) + return 1; *bufp += i; jmpfar->segment.size = 16; if (output_value(&jmpfar->segment, *bufp, 2, - (unsigned long)(*bufp-bufp_orig), bc, 1, d)) - return 1; + (unsigned long)(*bufp-bufp_orig), bc, 1, d)) + return 1; *bufp += 2; return 0; @@ -991,8 +991,8 @@ x86_bc_jmpfar_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, int yasm_x86__intnum_tobytes(yasm_arch *arch, const yasm_intnum *intn, - unsigned char *buf, size_t destsize, size_t valsize, - int shift, const yasm_bytecode *bc, int warn) + unsigned char *buf, size_t destsize, size_t valsize, + int shift, const yasm_bytecode *bc, int warn) { /* Write value out. */ yasm_intnum_get_sized(intn, buf, destsize, valsize, shift, 0, warn); diff --git a/modules/arch/x86/x86expr.c b/modules/arch/x86/x86expr.c index 33d246e0..40fd80bc 100644 --- a/modules/arch/x86/x86expr.c +++ b/modules/arch/x86/x86expr.c @@ -35,7 +35,7 @@ typedef struct x86_checkea_reg3264_data { - int *regs; /* total multiplier for each reg */ + int *regs; /* total multiplier for each reg */ unsigned char bits; unsigned char addrsize; } x86_checkea_reg3264_data; @@ -45,28 +45,28 @@ typedef struct x86_checkea_reg3264_data { */ static /*@null@*/ /*@dependent@*/ int * x86_expr_checkea_get_reg3264(yasm_expr__item *ei, int *regnum, - /*returned*/ void *d) + /*returned*/ void *d) { x86_checkea_reg3264_data *data = d; switch ((x86_expritem_reg_size)(ei->data.reg & ~0xFUL)) { - case X86_REG32: - if (data->addrsize != 32) - return 0; - *regnum = (unsigned int)(ei->data.reg & 0xF); - break; - case X86_REG64: - if (data->addrsize != 64) - return 0; - *regnum = (unsigned int)(ei->data.reg & 0xF); - break; - case X86_RIP: - if (data->bits != 64) - return 0; - *regnum = 16; - break; - default: - return 0; + case X86_REG32: + if (data->addrsize != 32) + return 0; + *regnum = (unsigned int)(ei->data.reg & 0xF); + break; + case X86_REG64: + if (data->addrsize != 64) + return 0; + *regnum = (unsigned int)(ei->data.reg & 0xF); + break; + case X86_RIP: + if (data->bits != 64) + return 0; + *regnum = 16; + break; + default: + return 0; } /* overwrite with 0 to eliminate register from displacement expr */ @@ -78,7 +78,7 @@ x86_expr_checkea_get_reg3264(yasm_expr__item *ei, int *regnum, } typedef struct x86_checkea_reg16_data { - int bx, si, di, bp; /* total multiplier for each reg */ + int bx, si, di, bp; /* total multiplier for each reg */ } x86_checkea_reg16_data; /* Only works if ei->type == EXPR_REG (doesn't check). @@ -100,14 +100,14 @@ x86_expr_checkea_get_reg16(yasm_expr__item *ei, int *regnum, void *d) /* don't allow 32-bit registers */ if ((ei->data.reg & ~0xFUL) != X86_REG16) - return 0; + return 0; /* & 7 for sanity check */ *regnum = (unsigned int)(ei->data.reg & 0x7); /* only allow BX, SI, DI, BP */ if (!reg16[*regnum]) - return 0; + return 0; /* overwrite with 0 to eliminate register from displacement expr */ ei->type = YASM_EXPR_INT; @@ -142,95 +142,95 @@ x86_expr_checkea_distcheck_reg(yasm_expr **ep, unsigned int bits) yasm_expr *e = *ep; int i; int havereg = -1, havereg_expr = -1; - int retval = 1; /* default to legal, no changes */ + int retval = 1; /* default to legal, no changes */ for (i=0; inumterms; i++) { - switch (e->terms[i].type) { - case YASM_EXPR_REG: - /* Check op to make sure it's valid to use w/register. */ - switch (e->op) { - case YASM_EXPR_MUL: - /* Check for reg*reg */ - if (havereg != -1) - return 0; - break; - case YASM_EXPR_ADD: - case YASM_EXPR_IDENT: - break; - default: - return 0; - } - havereg = i; - break; - case YASM_EXPR_FLOAT: - /* Floats not allowed. */ - return 0; - case YASM_EXPR_EXPR: - if (yasm_expr__contains(e->terms[i].data.expn, - YASM_EXPR_REG)) { - int ret2; - - /* Check op to make sure it's valid to use w/register. */ - if (e->op != YASM_EXPR_ADD && e->op != YASM_EXPR_MUL) - return 0; - /* Check for reg*reg */ - if (e->op == YASM_EXPR_MUL && havereg != -1) - return 0; - havereg = i; - havereg_expr = i; - /* Recurse to check lower levels */ - ret2 = - x86_expr_checkea_distcheck_reg(&e->terms[i].data.expn, - bits); - if (ret2 == 0) - return 0; - if (ret2 == 2) - retval = 2; - } else if (yasm_expr__contains(e->terms[i].data.expn, - YASM_EXPR_FLOAT)) - return 0; /* Disallow floats */ - break; - default: - break; - } + switch (e->terms[i].type) { + case YASM_EXPR_REG: + /* Check op to make sure it's valid to use w/register. */ + switch (e->op) { + case YASM_EXPR_MUL: + /* Check for reg*reg */ + if (havereg != -1) + return 0; + break; + case YASM_EXPR_ADD: + case YASM_EXPR_IDENT: + break; + default: + return 0; + } + havereg = i; + break; + case YASM_EXPR_FLOAT: + /* Floats not allowed. */ + return 0; + case YASM_EXPR_EXPR: + if (yasm_expr__contains(e->terms[i].data.expn, + YASM_EXPR_REG)) { + int ret2; + + /* Check op to make sure it's valid to use w/register. */ + if (e->op != YASM_EXPR_ADD && e->op != YASM_EXPR_MUL) + return 0; + /* Check for reg*reg */ + if (e->op == YASM_EXPR_MUL && havereg != -1) + return 0; + havereg = i; + havereg_expr = i; + /* Recurse to check lower levels */ + ret2 = + x86_expr_checkea_distcheck_reg(&e->terms[i].data.expn, + bits); + if (ret2 == 0) + return 0; + if (ret2 == 2) + retval = 2; + } else if (yasm_expr__contains(e->terms[i].data.expn, + YASM_EXPR_FLOAT)) + return 0; /* Disallow floats */ + break; + default: + break; + } } /* just exit if no registers were used */ if (havereg == -1) - return retval; + return retval; /* Distribute */ if (e->op == YASM_EXPR_MUL && havereg_expr != -1) { - yasm_expr *ne; - - retval = 2; /* we're going to change it */ - - /* The reg expn *must* be EXPR_ADD at this point. Sanity check. */ - if (e->terms[havereg_expr].type != YASM_EXPR_EXPR || - e->terms[havereg_expr].data.expn->op != YASM_EXPR_ADD) - yasm_internal_error(N_("Register expression not ADD or EXPN")); - - /* Iterate over each term in reg expn */ - for (i=0; iterms[havereg_expr].data.expn->numterms; i++) { - /* Copy everything EXCEPT havereg_expr term into new expression */ - ne = yasm_expr__copy_except(e, havereg_expr); - assert(ne != NULL); - /* Copy reg expr term into uncopied (empty) term in new expn */ - ne->terms[havereg_expr] = - e->terms[havereg_expr].data.expn->terms[i]; /* struct copy */ - /* Overwrite old reg expr term with new expn */ - e->terms[havereg_expr].data.expn->terms[i].type = YASM_EXPR_EXPR; - e->terms[havereg_expr].data.expn->terms[i].data.expn = ne; - } - - /* Replace e with expanded reg expn */ - ne = e->terms[havereg_expr].data.expn; - e->terms[havereg_expr].type = YASM_EXPR_NONE; /* don't delete it! */ - yasm_expr_destroy(e); /* but everything else */ - e = ne; - /*@-onlytrans@*/ - *ep = ne; - /*@=onlytrans@*/ + yasm_expr *ne; + + retval = 2; /* we're going to change it */ + + /* The reg expn *must* be EXPR_ADD at this point. Sanity check. */ + if (e->terms[havereg_expr].type != YASM_EXPR_EXPR || + e->terms[havereg_expr].data.expn->op != YASM_EXPR_ADD) + yasm_internal_error(N_("Register expression not ADD or EXPN")); + + /* Iterate over each term in reg expn */ + for (i=0; iterms[havereg_expr].data.expn->numterms; i++) { + /* Copy everything EXCEPT havereg_expr term into new expression */ + ne = yasm_expr__copy_except(e, havereg_expr); + assert(ne != NULL); + /* Copy reg expr term into uncopied (empty) term in new expn */ + ne->terms[havereg_expr] = + e->terms[havereg_expr].data.expn->terms[i]; /* struct copy */ + /* Overwrite old reg expr term with new expn */ + e->terms[havereg_expr].data.expn->terms[i].type = YASM_EXPR_EXPR; + e->terms[havereg_expr].data.expn->terms[i].data.expn = ne; + } + + /* Replace e with expanded reg expn */ + ne = e->terms[havereg_expr].data.expn; + e->terms[havereg_expr].type = YASM_EXPR_NONE; /* don't delete it! */ + yasm_expr_destroy(e); /* but everything else */ + e = ne; + /*@-onlytrans@*/ + *ep = ne; + /*@=onlytrans@*/ } return retval; @@ -264,122 +264,122 @@ x86_expr_checkea_getregusage(yasm_expr **ep, /*@null@*/ int *indexreg, /* Check for WRT rip first */ wrt = yasm_expr_extract_wrt(ep); if (wrt && wrt->op == YASM_EXPR_IDENT && - wrt->terms[0].type == YASM_EXPR_REG) { - if (bits != 64) { /* only valid in 64-bit mode */ - yasm_expr_destroy(wrt); - return 1; - } - reg = get_reg(&wrt->terms[0], ®num, data); - if (!reg || regnum != 16) { /* only accept rip */ - yasm_expr_destroy(wrt); - return 1; - } - (*reg)++; - - /* Delete WRT. Set pcrel to 1 to indicate to x86 - * bytecode code to do PC-relative displacement transform. - */ - *pcrel = 1; - yasm_expr_destroy(wrt); + wrt->terms[0].type == YASM_EXPR_REG) { + if (bits != 64) { /* only valid in 64-bit mode */ + yasm_expr_destroy(wrt); + return 1; + } + reg = get_reg(&wrt->terms[0], ®num, data); + if (!reg || regnum != 16) { /* only accept rip */ + yasm_expr_destroy(wrt); + return 1; + } + (*reg)++; + + /* Delete WRT. Set pcrel to 1 to indicate to x86 + * bytecode code to do PC-relative displacement transform. + */ + *pcrel = 1; + yasm_expr_destroy(wrt); } else if (wrt) { - yasm_expr_destroy(wrt); - return 1; + yasm_expr_destroy(wrt); + return 1; } /*@=unqualifiedtrans@*/ assert(*ep != NULL); e = *ep; switch (x86_expr_checkea_distcheck_reg(ep, bits)) { - case 0: - return 1; - case 2: - /* Need to simplify again */ - *ep = yasm_expr__level_tree(*ep, 1, 1, indexreg == 0, 0, NULL, - NULL); - e = *ep; - break; - default: - break; + case 0: + return 1; + case 2: + /* Need to simplify again */ + *ep = yasm_expr__level_tree(*ep, 1, 1, indexreg == 0, 0, NULL, + NULL); + e = *ep; + break; + default: + break; } switch (e->op) { - case YASM_EXPR_ADD: - /* Prescan for non-int multipliers against a reg. - * This is invalid due to the optimizer structure. - */ - for (i=0; inumterms; i++) - if (e->terms[i].type == YASM_EXPR_EXPR) { - yasm_expr__order_terms(e->terms[i].data.expn); - if (e->terms[i].data.expn->terms[0].type == - YASM_EXPR_REG) { - if (e->terms[i].data.expn->numterms > 2) - return 1; - if (e->terms[i].data.expn->terms[1].type != - YASM_EXPR_INT) - return 1; - } - } - - /*@fallthrough@*/ - case YASM_EXPR_IDENT: - /* Check each term for register (and possible multiplier). */ - for (i=0; inumterms; i++) { - if (e->terms[i].type == YASM_EXPR_REG) { - reg = get_reg(&e->terms[i], ®num, data); - if (!reg) - return 1; - (*reg)++; - /* Let last, largest multipler win indexreg */ - if (indexreg && *reg > 0 && indexval <= *reg && - !indexmult) { - *indexreg = regnum; - indexval = *reg; - } - } else if (e->terms[i].type == YASM_EXPR_EXPR) { - /* Already ordered from ADD above, just grab the value. - * Sanity check for EXPR_INT. - */ - if (e->terms[i].data.expn->terms[0].type == - YASM_EXPR_REG) { - if (e->terms[i].data.expn->terms[1].type != - YASM_EXPR_INT) - yasm_internal_error( - N_("Non-integer value in reg expn")); - reg = get_reg(&e->terms[i].data.expn->terms[0], - ®num, data); - if (!reg) - return 1; - (*reg) += yasm_intnum_get_int( - e->terms[i].data.expn->terms[1].data.intn); - /* Let last, largest multipler win indexreg */ - if (indexreg && *reg > 0 && indexval <= *reg) { - *indexreg = regnum; - indexval = *reg; - indexmult = 1; - } - } - } - } - break; - case YASM_EXPR_MUL: - /* Here, too, check for non-int multipliers against a reg. */ - yasm_expr__order_terms(e); - if (e->terms[0].type == YASM_EXPR_REG) { - if (e->numterms > 2) - return 1; - if (e->terms[1].type != YASM_EXPR_INT) - return 1; - reg = get_reg(&e->terms[0], ®num, data); - if (!reg) - return 1; - (*reg) += yasm_intnum_get_int(e->terms[1].data.intn); - if (indexreg) - *indexreg = regnum; - } - break; - default: - /* Should never get here! */ - yasm_internal_error(N_("unexpected expr op")); + case YASM_EXPR_ADD: + /* Prescan for non-int multipliers against a reg. + * This is invalid due to the optimizer structure. + */ + for (i=0; inumterms; i++) + if (e->terms[i].type == YASM_EXPR_EXPR) { + yasm_expr__order_terms(e->terms[i].data.expn); + if (e->terms[i].data.expn->terms[0].type == + YASM_EXPR_REG) { + if (e->terms[i].data.expn->numterms > 2) + return 1; + if (e->terms[i].data.expn->terms[1].type != + YASM_EXPR_INT) + return 1; + } + } + + /*@fallthrough@*/ + case YASM_EXPR_IDENT: + /* Check each term for register (and possible multiplier). */ + for (i=0; inumterms; i++) { + if (e->terms[i].type == YASM_EXPR_REG) { + reg = get_reg(&e->terms[i], ®num, data); + if (!reg) + return 1; + (*reg)++; + /* Let last, largest multipler win indexreg */ + if (indexreg && *reg > 0 && indexval <= *reg && + !indexmult) { + *indexreg = regnum; + indexval = *reg; + } + } else if (e->terms[i].type == YASM_EXPR_EXPR) { + /* Already ordered from ADD above, just grab the value. + * Sanity check for EXPR_INT. + */ + if (e->terms[i].data.expn->terms[0].type == + YASM_EXPR_REG) { + if (e->terms[i].data.expn->terms[1].type != + YASM_EXPR_INT) + yasm_internal_error( + N_("Non-integer value in reg expn")); + reg = get_reg(&e->terms[i].data.expn->terms[0], + ®num, data); + if (!reg) + return 1; + (*reg) += yasm_intnum_get_int( + e->terms[i].data.expn->terms[1].data.intn); + /* Let last, largest multipler win indexreg */ + if (indexreg && *reg > 0 && indexval <= *reg) { + *indexreg = regnum; + indexval = *reg; + indexmult = 1; + } + } + } + } + break; + case YASM_EXPR_MUL: + /* Here, too, check for non-int multipliers against a reg. */ + yasm_expr__order_terms(e); + if (e->terms[0].type == YASM_EXPR_REG) { + if (e->numterms > 2) + return 1; + if (e->terms[1].type != YASM_EXPR_INT) + return 1; + reg = get_reg(&e->terms[0], ®num, data); + if (!reg) + return 1; + (*reg) += yasm_intnum_get_int(e->terms[1].data.intn); + if (indexreg) + *indexreg = regnum; + } + break; + default: + /* Should never get here! */ + yasm_internal_error(N_("unexpected expr op")); } /* Simplify expr, which is now really just the displacement. This @@ -402,78 +402,78 @@ x86_expr_checkea_getregusage(yasm_expr **ep, /*@null@*/ int *indexreg, /*@-nullstate@*/ static int x86_checkea_calc_displen(x86_effaddr *x86_ea, unsigned int wordsize, int noreg, - int dispreq) + int dispreq) { /*@null@*/ /*@only@*/ yasm_intnum *num; - x86_ea->valid_modrm = 0; /* default to not yet valid */ + x86_ea->valid_modrm = 0; /* default to not yet valid */ switch (x86_ea->ea.disp.size) { - case 0: - break; - /* If not 0, the displacement length was forced; set the Mod bits - * appropriately and we're done with the ModRM byte. - */ - case 8: - /* Byte is only a valid override if there are registers in the - * EA. With no registers, we must have a 16/32 value. - */ - if (noreg) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("invalid displacement size; fixed")); - x86_ea->ea.disp.size = wordsize; - } else - x86_ea->modrm |= 0100; - x86_ea->valid_modrm = 1; - return 0; - case 16: - case 32: - /* Don't allow changing displacement different from BITS setting - * directly; require an address-size override to change it. - */ - if (wordsize != x86_ea->ea.disp.size) { - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid effective address (displacement size)")); - return 1; - } - if (!noreg) - x86_ea->modrm |= 0200; - x86_ea->valid_modrm = 1; - return 0; - default: - /* we shouldn't ever get any other size! */ - yasm_internal_error(N_("strange EA displacement size")); + case 0: + break; + /* If not 0, the displacement length was forced; set the Mod bits + * appropriately and we're done with the ModRM byte. + */ + case 8: + /* Byte is only a valid override if there are registers in the + * EA. With no registers, we must have a 16/32 value. + */ + if (noreg) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("invalid displacement size; fixed")); + x86_ea->ea.disp.size = wordsize; + } else + x86_ea->modrm |= 0100; + x86_ea->valid_modrm = 1; + return 0; + case 16: + case 32: + /* Don't allow changing displacement different from BITS setting + * directly; require an address-size override to change it. + */ + if (wordsize != x86_ea->ea.disp.size) { + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid effective address (displacement size)")); + return 1; + } + if (!noreg) + x86_ea->modrm |= 0200; + x86_ea->valid_modrm = 1; + return 0; + default: + /* we shouldn't ever get any other size! */ + yasm_internal_error(N_("strange EA displacement size")); } /* The displacement length hasn't been forced (or the forcing wasn't * valid), try to determine what it is. */ if (noreg) { - /* No register in ModRM expression, so it must be disp16/32, - * and as the Mod bits are set to 0 by the caller, we're done - * with the ModRM byte. - */ - x86_ea->ea.disp.size = wordsize; - x86_ea->valid_modrm = 1; - return 0; + /* No register in ModRM expression, so it must be disp16/32, + * and as the Mod bits are set to 0 by the caller, we're done + * with the ModRM byte. + */ + x86_ea->ea.disp.size = wordsize; + x86_ea->valid_modrm = 1; + return 0; } if (dispreq) { - /* for BP/EBP, there *must* be a displacement value, but we - * may not know the size (8 or 16/32) for sure right now. - */ - x86_ea->ea.need_nonzero_len = 1; + /* for BP/EBP, there *must* be a displacement value, but we + * may not know the size (8 or 16/32) for sure right now. + */ + x86_ea->ea.need_nonzero_len = 1; } if (x86_ea->ea.disp.rel) { - /* Relative displacement; basically all object formats need non-byte - * for relocation here, so just do that. (TODO: handle this - * differently?) - */ - x86_ea->ea.disp.size = wordsize; - x86_ea->modrm |= 0200; - x86_ea->valid_modrm = 1; - return 0; + /* Relative displacement; basically all object formats need non-byte + * for relocation here, so just do that. (TODO: handle this + * differently?) + */ + x86_ea->ea.disp.size = wordsize; + x86_ea->modrm |= 0200; + x86_ea->valid_modrm = 1; + return 0; } /* At this point there's 3 possibilities for the displacement: @@ -487,33 +487,33 @@ x86_checkea_calc_displen(x86_effaddr *x86_ea, unsigned int wordsize, int noreg, */ num = yasm_value_get_intnum(&x86_ea->ea.disp, NULL, 0); if (!num) { - /* Still has unknown values. */ - x86_ea->ea.need_nonzero_len = 1; - x86_ea->modrm |= 0100; - x86_ea->valid_modrm = 1; - return 0; + /* Still has unknown values. */ + x86_ea->ea.need_nonzero_len = 1; + x86_ea->modrm |= 0100; + x86_ea->valid_modrm = 1; + return 0; } /* Figure out what size displacement we will have. */ if (yasm_intnum_is_zero(num) && !x86_ea->ea.need_nonzero_len) { - /* If we know that the displacement is 0 right now, - * go ahead and delete the expr and make it so no - * displacement value is included in the output. - * The Mod bits of ModRM are set to 0 above, and - * we're done with the ModRM byte! - */ - yasm_value_delete(&x86_ea->ea.disp); - x86_ea->ea.need_disp = 0; + /* If we know that the displacement is 0 right now, + * go ahead and delete the expr and make it so no + * displacement value is included in the output. + * The Mod bits of ModRM are set to 0 above, and + * we're done with the ModRM byte! + */ + yasm_value_delete(&x86_ea->ea.disp); + x86_ea->ea.need_disp = 0; } else if (yasm_intnum_in_range(num, -128, 127)) { - /* It fits into a signed byte */ - x86_ea->ea.disp.size = 8; - x86_ea->modrm |= 0100; + /* It fits into a signed byte */ + x86_ea->ea.disp.size = 8; + x86_ea->modrm |= 0100; } else { - /* It's a 16/32-bit displacement */ - x86_ea->ea.disp.size = wordsize; - x86_ea->modrm |= 0200; + /* It's a 16/32-bit displacement */ + x86_ea->ea.disp.size = wordsize; + x86_ea->modrm |= 0200; } - x86_ea->valid_modrm = 1; /* We're done with ModRM */ + x86_ea->valid_modrm = 1; /* We're done with ModRM */ yasm_intnum_destroy(num); return 0; @@ -526,449 +526,449 @@ x86_expr_checkea_getregsize_callback(yasm_expr__item *ei, void *d) unsigned char *addrsize = (unsigned char *)d; if (ei->type == YASM_EXPR_REG) { - switch ((x86_expritem_reg_size)(ei->data.reg & ~0xFUL)) { - case X86_REG16: - *addrsize = 16; - break; - case X86_REG32: - *addrsize = 32; - break; - case X86_REG64: - case X86_RIP: - *addrsize = 64; - break; - default: - return 0; - } - return 1; + switch ((x86_expritem_reg_size)(ei->data.reg & ~0xFUL)) { + case X86_REG16: + *addrsize = 16; + break; + case X86_REG32: + *addrsize = 32; + break; + case X86_REG64: + case X86_RIP: + *addrsize = 64; + break; + default: + return 0; + } + return 1; } else - return 0; + return 0; } int yasm_x86__expr_checkea(x86_effaddr *x86_ea, unsigned char *addrsize, - unsigned int bits, int address16_op, unsigned char *rex, - yasm_bytecode *bc) + unsigned int bits, int address16_op, unsigned char *rex, + yasm_bytecode *bc) { int retval; if (*addrsize == 0) { - /* we need to figure out the address size from what we know about: - * - the displacement length - * - what registers are used in the expression - * - the bits setting - */ - switch (x86_ea->ea.disp.size) { - case 16: - /* must be 16-bit */ - *addrsize = 16; - break; - case 64: - /* We have to support this for the MemOffs case, but it's - * otherwise illegal. It's also illegal in non-64-bit mode. - */ - if (x86_ea->need_modrm || x86_ea->need_sib) { - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid effective address (displacement size)")); - return 1; - } - *addrsize = 64; - break; - case 32: - /* Must be 32-bit in 16-bit or 32-bit modes. In 64-bit mode, - * we don't know unless we look at the registers, except in the - * MemOffs case (see the end of this function). - */ - if (bits != 64 || (!x86_ea->need_modrm && !x86_ea->need_sib)) { - *addrsize = 32; - break; - } - /*@fallthrough@*/ - default: - /* check for use of 16 or 32-bit registers; if none are used - * default to bits setting. - */ - if (!x86_ea->ea.disp.abs || - !yasm_expr__traverse_leaves_in(x86_ea->ea.disp.abs, - addrsize, x86_expr_checkea_getregsize_callback)) - *addrsize = bits; - /* TODO: Add optional warning here if switched address size - * from bits setting just by register use.. eg [ax] in - * 32-bit mode would generate a warning. - */ - } + /* we need to figure out the address size from what we know about: + * - the displacement length + * - what registers are used in the expression + * - the bits setting + */ + switch (x86_ea->ea.disp.size) { + case 16: + /* must be 16-bit */ + *addrsize = 16; + break; + case 64: + /* We have to support this for the MemOffs case, but it's + * otherwise illegal. It's also illegal in non-64-bit mode. + */ + if (x86_ea->need_modrm || x86_ea->need_sib) { + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid effective address (displacement size)")); + return 1; + } + *addrsize = 64; + break; + case 32: + /* Must be 32-bit in 16-bit or 32-bit modes. In 64-bit mode, + * we don't know unless we look at the registers, except in the + * MemOffs case (see the end of this function). + */ + if (bits != 64 || (!x86_ea->need_modrm && !x86_ea->need_sib)) { + *addrsize = 32; + break; + } + /*@fallthrough@*/ + default: + /* check for use of 16 or 32-bit registers; if none are used + * default to bits setting. + */ + if (!x86_ea->ea.disp.abs || + !yasm_expr__traverse_leaves_in(x86_ea->ea.disp.abs, + addrsize, x86_expr_checkea_getregsize_callback)) + *addrsize = bits; + /* TODO: Add optional warning here if switched address size + * from bits setting just by register use.. eg [ax] in + * 32-bit mode would generate a warning. + */ + } } if ((*addrsize == 32 || *addrsize == 64) && - ((x86_ea->need_modrm && !x86_ea->valid_modrm) || - (x86_ea->need_sib && !x86_ea->valid_sib))) { - int i; - unsigned char low3; - typedef enum { - REG3264_NONE = -1, - REG3264_EAX = 0, - REG3264_ECX, - REG3264_EDX, - REG3264_EBX, - REG3264_ESP, - REG3264_EBP, - REG3264_ESI, - REG3264_EDI, - REG64_R8, - REG64_R9, - REG64_R10, - REG64_R11, - REG64_R12, - REG64_R13, - REG64_R14, - REG64_R15, - REG64_RIP - } reg3264type; - int reg3264mult[17] = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0}; - x86_checkea_reg3264_data reg3264_data; - int basereg = REG3264_NONE; /* "base" register (for SIB) */ - int indexreg = REG3264_NONE; /* "index" register (for SIB) */ - - /* We can only do 64-bit addresses in 64-bit mode. */ - if (*addrsize == 64 && bits != 64) { - yasm_error_set(YASM_ERROR_TYPE, - N_("invalid effective address (64-bit in non-64-bit mode)")); - return 1; - } - - reg3264_data.regs = reg3264mult; - reg3264_data.bits = bits; - reg3264_data.addrsize = *addrsize; - if (x86_ea->ea.disp.abs) { - int pcrel = 0; - switch (x86_expr_checkea_getregusage - (&x86_ea->ea.disp.abs, &indexreg, &pcrel, bits, - ®3264_data, x86_expr_checkea_get_reg3264)) { - case 1: - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid effective address")); - return 1; - case 2: - if (pcrel) - yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); - return 2; - default: - if (pcrel) - yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); - break; - } - } - - /* If indexreg mult is 0, discard it. - * This is possible because of the way indexreg is found in - * expr_checkea_getregusage(). - */ - if (indexreg != REG3264_NONE && reg3264mult[indexreg] == 0) - indexreg = REG3264_NONE; - - /* Find a basereg (*1, but not indexreg), if there is one. - * Also, if an indexreg hasn't been assigned, try to find one. - * Meanwhile, check to make sure there's no negative register mults. - */ - for (i=0; i<17; i++) { - if (reg3264mult[i] < 0) { - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid effective address")); - return 1; - } - if (i != indexreg && reg3264mult[i] == 1 && - basereg == REG3264_NONE) - basereg = i; - else if (indexreg == REG3264_NONE && reg3264mult[i] > 0) - indexreg = i; - } - - /* Handle certain special cases of indexreg mults when basereg is - * empty. - */ - if (indexreg != REG3264_NONE && basereg == REG3264_NONE) - switch (reg3264mult[indexreg]) { - case 1: - /* Only optimize this way if nosplit wasn't specified */ - if (!x86_ea->ea.nosplit) { - basereg = indexreg; - indexreg = -1; - } - break; - case 2: - /* Only split if nosplit wasn't specified */ - if (!x86_ea->ea.nosplit) { - basereg = indexreg; - reg3264mult[indexreg] = 1; - } - break; - case 3: - case 5: - case 9: - basereg = indexreg; - reg3264mult[indexreg]--; - break; - } - - /* Make sure there's no other registers than the basereg and indexreg - * we just found. - */ - for (i=0; i<17; i++) - if (i != basereg && i != indexreg && reg3264mult[i] != 0) { - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid effective address")); - return 1; - } - - /* Check the index multiplier value for validity if present. */ - if (indexreg != REG3264_NONE && reg3264mult[indexreg] != 1 && - reg3264mult[indexreg] != 2 && reg3264mult[indexreg] != 4 && - reg3264mult[indexreg] != 8) { - yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); - return 1; - } - - /* ESP is not a legal indexreg. */ - if (indexreg == REG3264_ESP) { - /* If mult>1 or basereg is ESP also, there's no way to make it - * legal. - */ - if (reg3264mult[REG3264_ESP] > 1 || basereg == REG3264_ESP) { - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid effective address")); - return 1; - } - /* If mult==1 and basereg is not ESP, swap indexreg w/basereg. */ - indexreg = basereg; - basereg = REG3264_ESP; - } - - /* RIP is only legal if it's the ONLY register used. */ - if (indexreg == REG64_RIP || - (basereg == REG64_RIP && indexreg != REG3264_NONE)) { - yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); - return 1; - } - - /* At this point, we know the base and index registers and that the - * memory expression is (essentially) valid. Now build the ModRM and - * (optional) SIB bytes. - */ - - /* First determine R/M (Mod is later determined from disp size) */ - x86_ea->need_modrm = 1; /* we always need ModRM */ - if (basereg == REG3264_NONE && indexreg == REG3264_NONE) { - /* Just a disp32: in 64-bit mode the RM encoding is used for RIP - * offset addressing, so we need to use the SIB form instead. - */ - if (bits == 64) { - x86_ea->modrm |= 4; - x86_ea->need_sib = 1; - } else { - x86_ea->modrm |= 5; - x86_ea->sib = 0; - x86_ea->valid_sib = 0; - x86_ea->need_sib = 0; - } - } else if (basereg == REG64_RIP) { - x86_ea->modrm |= 5; - x86_ea->sib = 0; - x86_ea->valid_sib = 0; - x86_ea->need_sib = 0; - /* RIP always requires a 32-bit displacement */ - x86_ea->valid_modrm = 1; - x86_ea->ea.disp.size = 32; - return 0; - } else if (indexreg == REG3264_NONE) { - /* basereg only */ - /* Don't need to go to the full effort of determining what type - * of register basereg is, as x86_set_rex_from_reg doesn't pay - * much attention. - */ - if (yasm_x86__set_rex_from_reg(rex, &low3, - (unsigned int)(X86_REG64 | basereg), - bits, X86_REX_B)) - return 1; - x86_ea->modrm |= low3; - /* we don't need an SIB *unless* basereg is ESP or R12 */ - if (basereg == REG3264_ESP || basereg == REG64_R12) - x86_ea->need_sib = 1; - else { - x86_ea->sib = 0; - x86_ea->valid_sib = 0; - x86_ea->need_sib = 0; - } - } else { - /* index or both base and index */ - x86_ea->modrm |= 4; - x86_ea->need_sib = 1; - } - - /* Determine SIB if needed */ - if (x86_ea->need_sib == 1) { - x86_ea->sib = 0; /* start with 0 */ - - /* Special case: no basereg */ - if (basereg == REG3264_NONE) - x86_ea->sib |= 5; - else { - if (yasm_x86__set_rex_from_reg(rex, &low3, (unsigned int) - (X86_REG64 | basereg), bits, - X86_REX_B)) - return 1; - x86_ea->sib |= low3; - } - - /* Put in indexreg, checking for none case */ - if (indexreg == REG3264_NONE) - x86_ea->sib |= 040; - /* Any scale field is valid, just leave at 0. */ - else { - if (yasm_x86__set_rex_from_reg(rex, &low3, (unsigned int) - (X86_REG64 | indexreg), bits, - X86_REX_X)) - return 1; - x86_ea->sib |= low3 << 3; - /* Set scale field, 1 case -> 0, so don't bother. */ - switch (reg3264mult[indexreg]) { - case 2: - x86_ea->sib |= 0100; - break; - case 4: - x86_ea->sib |= 0200; - break; - case 8: - x86_ea->sib |= 0300; - break; - } - } - - x86_ea->valid_sib = 1; /* Done with SIB */ - } - - /* Calculate displacement length (if possible) */ - retval = x86_checkea_calc_displen - (x86_ea, 32, basereg == REG3264_NONE, - basereg == REG3264_EBP || basereg == REG64_R13); - return retval; + ((x86_ea->need_modrm && !x86_ea->valid_modrm) || + (x86_ea->need_sib && !x86_ea->valid_sib))) { + int i; + unsigned char low3; + typedef enum { + REG3264_NONE = -1, + REG3264_EAX = 0, + REG3264_ECX, + REG3264_EDX, + REG3264_EBX, + REG3264_ESP, + REG3264_EBP, + REG3264_ESI, + REG3264_EDI, + REG64_R8, + REG64_R9, + REG64_R10, + REG64_R11, + REG64_R12, + REG64_R13, + REG64_R14, + REG64_R15, + REG64_RIP + } reg3264type; + int reg3264mult[17] = {0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0}; + x86_checkea_reg3264_data reg3264_data; + int basereg = REG3264_NONE; /* "base" register (for SIB) */ + int indexreg = REG3264_NONE; /* "index" register (for SIB) */ + + /* We can only do 64-bit addresses in 64-bit mode. */ + if (*addrsize == 64 && bits != 64) { + yasm_error_set(YASM_ERROR_TYPE, + N_("invalid effective address (64-bit in non-64-bit mode)")); + return 1; + } + + reg3264_data.regs = reg3264mult; + reg3264_data.bits = bits; + reg3264_data.addrsize = *addrsize; + if (x86_ea->ea.disp.abs) { + int pcrel = 0; + switch (x86_expr_checkea_getregusage + (&x86_ea->ea.disp.abs, &indexreg, &pcrel, bits, + ®3264_data, x86_expr_checkea_get_reg3264)) { + case 1: + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid effective address")); + return 1; + case 2: + if (pcrel) + yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); + return 2; + default: + if (pcrel) + yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); + break; + } + } + + /* If indexreg mult is 0, discard it. + * This is possible because of the way indexreg is found in + * expr_checkea_getregusage(). + */ + if (indexreg != REG3264_NONE && reg3264mult[indexreg] == 0) + indexreg = REG3264_NONE; + + /* Find a basereg (*1, but not indexreg), if there is one. + * Also, if an indexreg hasn't been assigned, try to find one. + * Meanwhile, check to make sure there's no negative register mults. + */ + for (i=0; i<17; i++) { + if (reg3264mult[i] < 0) { + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid effective address")); + return 1; + } + if (i != indexreg && reg3264mult[i] == 1 && + basereg == REG3264_NONE) + basereg = i; + else if (indexreg == REG3264_NONE && reg3264mult[i] > 0) + indexreg = i; + } + + /* Handle certain special cases of indexreg mults when basereg is + * empty. + */ + if (indexreg != REG3264_NONE && basereg == REG3264_NONE) + switch (reg3264mult[indexreg]) { + case 1: + /* Only optimize this way if nosplit wasn't specified */ + if (!x86_ea->ea.nosplit) { + basereg = indexreg; + indexreg = -1; + } + break; + case 2: + /* Only split if nosplit wasn't specified */ + if (!x86_ea->ea.nosplit) { + basereg = indexreg; + reg3264mult[indexreg] = 1; + } + break; + case 3: + case 5: + case 9: + basereg = indexreg; + reg3264mult[indexreg]--; + break; + } + + /* Make sure there's no other registers than the basereg and indexreg + * we just found. + */ + for (i=0; i<17; i++) + if (i != basereg && i != indexreg && reg3264mult[i] != 0) { + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid effective address")); + return 1; + } + + /* Check the index multiplier value for validity if present. */ + if (indexreg != REG3264_NONE && reg3264mult[indexreg] != 1 && + reg3264mult[indexreg] != 2 && reg3264mult[indexreg] != 4 && + reg3264mult[indexreg] != 8) { + yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); + return 1; + } + + /* ESP is not a legal indexreg. */ + if (indexreg == REG3264_ESP) { + /* If mult>1 or basereg is ESP also, there's no way to make it + * legal. + */ + if (reg3264mult[REG3264_ESP] > 1 || basereg == REG3264_ESP) { + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid effective address")); + return 1; + } + /* If mult==1 and basereg is not ESP, swap indexreg w/basereg. */ + indexreg = basereg; + basereg = REG3264_ESP; + } + + /* RIP is only legal if it's the ONLY register used. */ + if (indexreg == REG64_RIP || + (basereg == REG64_RIP && indexreg != REG3264_NONE)) { + yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); + return 1; + } + + /* At this point, we know the base and index registers and that the + * memory expression is (essentially) valid. Now build the ModRM and + * (optional) SIB bytes. + */ + + /* First determine R/M (Mod is later determined from disp size) */ + x86_ea->need_modrm = 1; /* we always need ModRM */ + if (basereg == REG3264_NONE && indexreg == REG3264_NONE) { + /* Just a disp32: in 64-bit mode the RM encoding is used for RIP + * offset addressing, so we need to use the SIB form instead. + */ + if (bits == 64) { + x86_ea->modrm |= 4; + x86_ea->need_sib = 1; + } else { + x86_ea->modrm |= 5; + x86_ea->sib = 0; + x86_ea->valid_sib = 0; + x86_ea->need_sib = 0; + } + } else if (basereg == REG64_RIP) { + x86_ea->modrm |= 5; + x86_ea->sib = 0; + x86_ea->valid_sib = 0; + x86_ea->need_sib = 0; + /* RIP always requires a 32-bit displacement */ + x86_ea->valid_modrm = 1; + x86_ea->ea.disp.size = 32; + return 0; + } else if (indexreg == REG3264_NONE) { + /* basereg only */ + /* Don't need to go to the full effort of determining what type + * of register basereg is, as x86_set_rex_from_reg doesn't pay + * much attention. + */ + if (yasm_x86__set_rex_from_reg(rex, &low3, + (unsigned int)(X86_REG64 | basereg), + bits, X86_REX_B)) + return 1; + x86_ea->modrm |= low3; + /* we don't need an SIB *unless* basereg is ESP or R12 */ + if (basereg == REG3264_ESP || basereg == REG64_R12) + x86_ea->need_sib = 1; + else { + x86_ea->sib = 0; + x86_ea->valid_sib = 0; + x86_ea->need_sib = 0; + } + } else { + /* index or both base and index */ + x86_ea->modrm |= 4; + x86_ea->need_sib = 1; + } + + /* Determine SIB if needed */ + if (x86_ea->need_sib == 1) { + x86_ea->sib = 0; /* start with 0 */ + + /* Special case: no basereg */ + if (basereg == REG3264_NONE) + x86_ea->sib |= 5; + else { + if (yasm_x86__set_rex_from_reg(rex, &low3, (unsigned int) + (X86_REG64 | basereg), bits, + X86_REX_B)) + return 1; + x86_ea->sib |= low3; + } + + /* Put in indexreg, checking for none case */ + if (indexreg == REG3264_NONE) + x86_ea->sib |= 040; + /* Any scale field is valid, just leave at 0. */ + else { + if (yasm_x86__set_rex_from_reg(rex, &low3, (unsigned int) + (X86_REG64 | indexreg), bits, + X86_REX_X)) + return 1; + x86_ea->sib |= low3 << 3; + /* Set scale field, 1 case -> 0, so don't bother. */ + switch (reg3264mult[indexreg]) { + case 2: + x86_ea->sib |= 0100; + break; + case 4: + x86_ea->sib |= 0200; + break; + case 8: + x86_ea->sib |= 0300; + break; + } + } + + x86_ea->valid_sib = 1; /* Done with SIB */ + } + + /* Calculate displacement length (if possible) */ + retval = x86_checkea_calc_displen + (x86_ea, 32, basereg == REG3264_NONE, + basereg == REG3264_EBP || basereg == REG64_R13); + return retval; } else if (*addrsize == 16 && x86_ea->need_modrm && !x86_ea->valid_modrm) { - static const unsigned char modrm16[16] = { - 0006 /* disp16 */, 0007 /* [BX] */, 0004 /* [SI] */, - 0000 /* [BX+SI] */, 0005 /* [DI] */, 0001 /* [BX+DI] */, - 0377 /* invalid */, 0377 /* invalid */, 0006 /* [BP]+d */, - 0377 /* invalid */, 0002 /* [BP+SI] */, 0377 /* invalid */, - 0003 /* [BP+DI] */, 0377 /* invalid */, 0377 /* invalid */, - 0377 /* invalid */ - }; - x86_checkea_reg16_data reg16mult = {0, 0, 0, 0}; - enum { - HAVE_NONE = 0, - HAVE_BX = 1<<0, - HAVE_SI = 1<<1, - HAVE_DI = 1<<2, - HAVE_BP = 1<<3 - } havereg = HAVE_NONE; - - /* 64-bit mode does not allow 16-bit addresses */ - if (bits == 64 && !address16_op) { - yasm_error_set(YASM_ERROR_TYPE, - N_("16-bit addresses not supported in 64-bit mode")); - return 1; - } - - /* 16-bit cannot have SIB */ - x86_ea->sib = 0; - x86_ea->valid_sib = 0; - x86_ea->need_sib = 0; - - if (x86_ea->ea.disp.abs) { - int pcrel = 0; - switch (x86_expr_checkea_getregusage - (&x86_ea->ea.disp.abs, (int *)NULL, &pcrel, bits, - ®16mult, x86_expr_checkea_get_reg16)) { - case 1: - yasm_error_set(YASM_ERROR_VALUE, - N_("invalid effective address")); - return 1; - case 2: - if (pcrel) - yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); - return 2; - default: - if (pcrel) - yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); - break; - } - } - - /* reg multipliers not 0 or 1 are illegal. */ - if (reg16mult.bx & ~1 || reg16mult.si & ~1 || reg16mult.di & ~1 || - reg16mult.bp & ~1) { - yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); - return 1; - } - - /* Set havereg appropriately */ - if (reg16mult.bx > 0) - havereg |= HAVE_BX; - if (reg16mult.si > 0) - havereg |= HAVE_SI; - if (reg16mult.di > 0) - havereg |= HAVE_DI; - if (reg16mult.bp > 0) - havereg |= HAVE_BP; - - /* Check the modrm value for invalid combinations. */ - if (modrm16[havereg] & 0070) { - yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); - return 1; - } - - /* Set ModRM byte for registers */ - x86_ea->modrm |= modrm16[havereg]; - - /* Calculate displacement length (if possible) */ - retval = x86_checkea_calc_displen - (x86_ea, 16, havereg == HAVE_NONE, havereg == HAVE_BP); - return retval; + static const unsigned char modrm16[16] = { + 0006 /* disp16 */, 0007 /* [BX] */, 0004 /* [SI] */, + 0000 /* [BX+SI] */, 0005 /* [DI] */, 0001 /* [BX+DI] */, + 0377 /* invalid */, 0377 /* invalid */, 0006 /* [BP]+d */, + 0377 /* invalid */, 0002 /* [BP+SI] */, 0377 /* invalid */, + 0003 /* [BP+DI] */, 0377 /* invalid */, 0377 /* invalid */, + 0377 /* invalid */ + }; + x86_checkea_reg16_data reg16mult = {0, 0, 0, 0}; + enum { + HAVE_NONE = 0, + HAVE_BX = 1<<0, + HAVE_SI = 1<<1, + HAVE_DI = 1<<2, + HAVE_BP = 1<<3 + } havereg = HAVE_NONE; + + /* 64-bit mode does not allow 16-bit addresses */ + if (bits == 64 && !address16_op) { + yasm_error_set(YASM_ERROR_TYPE, + N_("16-bit addresses not supported in 64-bit mode")); + return 1; + } + + /* 16-bit cannot have SIB */ + x86_ea->sib = 0; + x86_ea->valid_sib = 0; + x86_ea->need_sib = 0; + + if (x86_ea->ea.disp.abs) { + int pcrel = 0; + switch (x86_expr_checkea_getregusage + (&x86_ea->ea.disp.abs, (int *)NULL, &pcrel, bits, + ®16mult, x86_expr_checkea_get_reg16)) { + case 1: + yasm_error_set(YASM_ERROR_VALUE, + N_("invalid effective address")); + return 1; + case 2: + if (pcrel) + yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); + return 2; + default: + if (pcrel) + yasm_value_set_curpos_rel(&x86_ea->ea.disp, bc, 1); + break; + } + } + + /* reg multipliers not 0 or 1 are illegal. */ + if (reg16mult.bx & ~1 || reg16mult.si & ~1 || reg16mult.di & ~1 || + reg16mult.bp & ~1) { + yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); + return 1; + } + + /* Set havereg appropriately */ + if (reg16mult.bx > 0) + havereg |= HAVE_BX; + if (reg16mult.si > 0) + havereg |= HAVE_SI; + if (reg16mult.di > 0) + havereg |= HAVE_DI; + if (reg16mult.bp > 0) + havereg |= HAVE_BP; + + /* Check the modrm value for invalid combinations. */ + if (modrm16[havereg] & 0070) { + yasm_error_set(YASM_ERROR_VALUE, N_("invalid effective address")); + return 1; + } + + /* Set ModRM byte for registers */ + x86_ea->modrm |= modrm16[havereg]; + + /* Calculate displacement length (if possible) */ + retval = x86_checkea_calc_displen + (x86_ea, 16, havereg == HAVE_NONE, havereg == HAVE_BP); + return retval; } else if (!x86_ea->need_modrm && !x86_ea->need_sib) { - /* Special case for MOV MemOffs opcode: displacement but no modrm. */ - switch (*addrsize) { - case 64: - if (bits != 64) { - yasm_error_set(YASM_ERROR_TYPE, - N_("invalid effective address (64-bit in non-64-bit mode)")); - return 1; - } - x86_ea->ea.disp.size = 64; - break; - case 32: - x86_ea->ea.disp.size = 32; - break; - case 16: - /* 64-bit mode does not allow 16-bit addresses */ - if (bits == 64 && !address16_op) { - yasm_error_set(YASM_ERROR_TYPE, - N_("16-bit addresses not supported in 64-bit mode")); - return 1; - } - x86_ea->ea.disp.size = 16; - break; - } + /* Special case for MOV MemOffs opcode: displacement but no modrm. */ + switch (*addrsize) { + case 64: + if (bits != 64) { + yasm_error_set(YASM_ERROR_TYPE, + N_("invalid effective address (64-bit in non-64-bit mode)")); + return 1; + } + x86_ea->ea.disp.size = 64; + break; + case 32: + x86_ea->ea.disp.size = 32; + break; + case 16: + /* 64-bit mode does not allow 16-bit addresses */ + if (bits == 64 && !address16_op) { + yasm_error_set(YASM_ERROR_TYPE, + N_("16-bit addresses not supported in 64-bit mode")); + return 1; + } + x86_ea->ea.disp.size = 16; + break; + } } return 0; } int yasm_x86__floatnum_tobytes(yasm_arch *arch, const yasm_floatnum *flt, - unsigned char *buf, size_t destsize, size_t valsize, - size_t shift, int warn) + unsigned char *buf, size_t destsize, size_t valsize, + size_t shift, int warn) { if (!yasm_floatnum_check_size(flt, valsize)) { - yasm_error_set(YASM_ERROR_FLOATING_POINT, - N_("invalid floating point constant size")); - return 1; + yasm_error_set(YASM_ERROR_FLOATING_POINT, + N_("invalid floating point constant size")); + return 1; } yasm_floatnum_get_sized(flt, buf, destsize, valsize, shift, 0, warn); diff --git a/modules/arch/x86/x86id.c b/modules/arch/x86/x86id.c index 977c5928..e99d731d 100644 --- a/modules/arch/x86/x86id.c +++ b/modules/arch/x86/x86id.c @@ -43,33 +43,33 @@ static const char *cpu_find_reverse(unsigned long cpu); * parameters are read from the arch-specific data in LSB->MSB order. * (only for asthetic reasons in the lexer code below, no practical reason). */ -#define MOD_Gap0 (1UL<<0) /* Eats a parameter */ -#define MOD_Op2Add (1UL<<1) /* Parameter adds to opcode byte 2 */ -#define MOD_Gap1 (1UL<<2) /* Eats a parameter */ -#define MOD_Op1Add (1UL<<3) /* Parameter adds to opcode byte 1 */ -#define MOD_Gap2 (1UL<<4) /* Eats a parameter */ -#define MOD_Op0Add (1UL<<5) /* Parameter adds to opcode byte 0 */ -#define MOD_PreAdd (1UL<<6) /* Parameter adds to "special" prefix */ -#define MOD_SpAdd (1UL<<7) /* Parameter adds to "spare" value */ -#define MOD_OpSizeR (1UL<<8) /* Parameter replaces opersize */ -#define MOD_Imm8 (1UL<<9) /* Parameter is included as immediate byte */ -#define MOD_AdSizeR (1UL<<10) /* Parameter replaces addrsize (jmp only) */ -#define MOD_DOpS64R (1UL<<11) /* Parameter replaces default 64-bit opersize */ -#define MOD_Op1AddSp (1UL<<12) /* Parameter is added as "spare" to opcode byte 2 */ +#define MOD_Gap0 (1UL<<0) /* Eats a parameter */ +#define MOD_Op2Add (1UL<<1) /* Parameter adds to opcode byte 2 */ +#define MOD_Gap1 (1UL<<2) /* Eats a parameter */ +#define MOD_Op1Add (1UL<<3) /* Parameter adds to opcode byte 1 */ +#define MOD_Gap2 (1UL<<4) /* Eats a parameter */ +#define MOD_Op0Add (1UL<<5) /* Parameter adds to opcode byte 0 */ +#define MOD_PreAdd (1UL<<6) /* Parameter adds to "special" prefix */ +#define MOD_SpAdd (1UL<<7) /* Parameter adds to "spare" value */ +#define MOD_OpSizeR (1UL<<8) /* Parameter replaces opersize */ +#define MOD_Imm8 (1UL<<9) /* Parameter is included as immediate byte */ +#define MOD_AdSizeR (1UL<<10) /* Parameter replaces addrsize (jmp only) */ +#define MOD_DOpS64R (1UL<<11) /* Parameter replaces default 64-bit opersize */ +#define MOD_Op1AddSp (1UL<<12) /* Parameter is added as "spare" to opcode byte 2 */ /* Modifiers that aren't: these are used with the GAS parser to indicate * special cases. */ -#define MOD_GasOnly (1UL<<13) /* Only available in GAS mode */ -#define MOD_GasIllegal (1UL<<14) /* Illegal in GAS mode */ -#define MOD_GasNoRev (1UL<<15) /* Don't reverse operands */ -#define MOD_GasSufB (1UL<<16) /* GAS B suffix ok */ -#define MOD_GasSufW (1UL<<17) /* GAS W suffix ok */ -#define MOD_GasSufL (1UL<<18) /* GAS L suffix ok */ -#define MOD_GasSufQ (1UL<<19) /* GAS Q suffix ok */ -#define MOD_GasSufS (1UL<<20) /* GAS S suffix ok */ +#define MOD_GasOnly (1UL<<13) /* Only available in GAS mode */ +#define MOD_GasIllegal (1UL<<14) /* Illegal in GAS mode */ +#define MOD_GasNoRev (1UL<<15) /* Don't reverse operands */ +#define MOD_GasSufB (1UL<<16) /* GAS B suffix ok */ +#define MOD_GasSufW (1UL<<17) /* GAS W suffix ok */ +#define MOD_GasSufL (1UL<<18) /* GAS L suffix ok */ +#define MOD_GasSufQ (1UL<<19) /* GAS Q suffix ok */ +#define MOD_GasSufS (1UL<<20) /* GAS S suffix ok */ #define MOD_GasSuf_SHIFT 16 -#define MOD_GasSuf_MASK (0x1FUL<<16) +#define MOD_GasSuf_MASK (0x1FUL<<16) /* Operand types. These are more detailed than the "general" types for all * architectures, as they include the size, for instance. @@ -150,76 +150,76 @@ static const char *cpu_find_reverse(unsigned long cpu); * 3 = forced 16-bit address size (override ignored, no prefix) * 4 = large imm64 that can become a sign-extended imm32. */ -#define OPT_Imm 0x0 -#define OPT_Reg 0x1 -#define OPT_Mem 0x2 -#define OPT_RM 0x3 -#define OPT_SIMDReg 0x4 -#define OPT_SIMDRM 0x5 -#define OPT_SegReg 0x6 -#define OPT_CRReg 0x7 -#define OPT_DRReg 0x8 -#define OPT_TRReg 0x9 -#define OPT_ST0 0xA -#define OPT_Areg 0xB -#define OPT_Creg 0xC -#define OPT_Dreg 0xD -#define OPT_CS 0xE -#define OPT_DS 0xF -#define OPT_ES 0x10 -#define OPT_FS 0x11 -#define OPT_GS 0x12 -#define OPT_SS 0x13 -#define OPT_CR4 0x14 -#define OPT_MemOffs 0x15 -#define OPT_Imm1 0x16 +#define OPT_Imm 0x0 +#define OPT_Reg 0x1 +#define OPT_Mem 0x2 +#define OPT_RM 0x3 +#define OPT_SIMDReg 0x4 +#define OPT_SIMDRM 0x5 +#define OPT_SegReg 0x6 +#define OPT_CRReg 0x7 +#define OPT_DRReg 0x8 +#define OPT_TRReg 0x9 +#define OPT_ST0 0xA +#define OPT_Areg 0xB +#define OPT_Creg 0xC +#define OPT_Dreg 0xD +#define OPT_CS 0xE +#define OPT_DS 0xF +#define OPT_ES 0x10 +#define OPT_FS 0x11 +#define OPT_GS 0x12 +#define OPT_SS 0x13 +#define OPT_CR4 0x14 +#define OPT_MemOffs 0x15 +#define OPT_Imm1 0x16 #define OPT_ImmNotSegOff 0x17 -#define OPT_MASK 0x1F - -#define OPS_Any (0UL<<5) -#define OPS_8 (1UL<<5) -#define OPS_16 (2UL<<5) -#define OPS_32 (3UL<<5) -#define OPS_64 (4UL<<5) -#define OPS_80 (5UL<<5) -#define OPS_128 (6UL<<5) -#define OPS_BITS (7UL<<5) -#define OPS_MASK (7UL<<5) -#define OPS_SHIFT 5 - -#define OPS_Relaxed (1UL<<8) -#define OPS_RMASK (1UL<<8) - -#define OPEAS_Not64 (0UL<<9) -#define OPEAS_64 (1UL<<9) -#define OPEAS_MASK (1UL<<9) - -#define OPTM_None (0UL<<10) -#define OPTM_Near (1UL<<10) -#define OPTM_Short (2UL<<10) -#define OPTM_Far (3UL<<10) -#define OPTM_To (4UL<<10) -#define OPTM_MASK (7UL<<10) - -#define OPA_None (0UL<<13) -#define OPA_EA (1UL<<13) -#define OPA_Imm (2UL<<13) -#define OPA_SImm (3UL<<13) -#define OPA_Spare (4UL<<13) -#define OPA_Op0Add (5UL<<13) -#define OPA_Op1Add (6UL<<13) -#define OPA_SpareEA (7UL<<13) -#define OPA_JmpRel (8UL<<13) -#define OPA_AdSizeR (9UL<<13) -#define OPA_JmpFar (0xAUL<<13) -#define OPA_MASK (0xFUL<<13) - -#define OPAP_None (0UL<<17) -#define OPAP_SImm8 (1UL<<17) -#define OPAP_ShortMov (2UL<<17) -#define OPAP_A16 (3UL<<17) +#define OPT_MASK 0x1F + +#define OPS_Any (0UL<<5) +#define OPS_8 (1UL<<5) +#define OPS_16 (2UL<<5) +#define OPS_32 (3UL<<5) +#define OPS_64 (4UL<<5) +#define OPS_80 (5UL<<5) +#define OPS_128 (6UL<<5) +#define OPS_BITS (7UL<<5) +#define OPS_MASK (7UL<<5) +#define OPS_SHIFT 5 + +#define OPS_Relaxed (1UL<<8) +#define OPS_RMASK (1UL<<8) + +#define OPEAS_Not64 (0UL<<9) +#define OPEAS_64 (1UL<<9) +#define OPEAS_MASK (1UL<<9) + +#define OPTM_None (0UL<<10) +#define OPTM_Near (1UL<<10) +#define OPTM_Short (2UL<<10) +#define OPTM_Far (3UL<<10) +#define OPTM_To (4UL<<10) +#define OPTM_MASK (7UL<<10) + +#define OPA_None (0UL<<13) +#define OPA_EA (1UL<<13) +#define OPA_Imm (2UL<<13) +#define OPA_SImm (3UL<<13) +#define OPA_Spare (4UL<<13) +#define OPA_Op0Add (5UL<<13) +#define OPA_Op1Add (6UL<<13) +#define OPA_SpareEA (7UL<<13) +#define OPA_JmpRel (8UL<<13) +#define OPA_AdSizeR (9UL<<13) +#define OPA_JmpFar (0xAUL<<13) +#define OPA_MASK (0xFUL<<13) + +#define OPAP_None (0UL<<17) +#define OPAP_SImm8 (1UL<<17) +#define OPAP_ShortMov (2UL<<17) +#define OPAP_A16 (3UL<<17) #define OPAP_SImm32Avail (4UL<<17) -#define OPAP_MASK (7UL<<17) +#define OPAP_MASK (7UL<<17) typedef struct x86_insn_info { /* The CPU feature flags needed to execute this instruction. This is OR'ed @@ -867,7 +867,7 @@ static const x86_insn_info arith_insn[] = { { CPU_Hammer|CPU_64, MOD_Op2Add|MOD_Op1AddSp|MOD_GasSufQ, 64, 0, 0, 2, {0x83, 0xC0, 0x05}, 0, 2, {OPT_Areg|OPS_64|OPA_None, - OPT_Imm|OPS_32|OPS_Relaxed|OPA_Imm|OPAP_SImm8, 0} }, + OPT_Imm|OPS_32|OPS_Relaxed|OPA_Imm|OPAP_SImm8, 0} }, { CPU_Any, MOD_Gap0|MOD_SpAdd|MOD_GasSufB, 0, 0, 0, 1, {0x80, 0, 0}, 0, 2, {OPT_RM|OPS_8|OPA_EA, OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm, 0} }, @@ -878,10 +878,10 @@ static const x86_insn_info arith_insn[] = { {OPT_RM|OPS_16|OPA_EA, OPT_Imm|OPS_8|OPA_SImm, 0} }, { CPU_Any, MOD_Gap0|MOD_SpAdd|MOD_GasIllegal, 16, 0, 0, 1, {0x83, 0x81, 0}, 0, 2, {OPT_RM|OPS_16|OPS_Relaxed|OPA_EA, - OPT_Imm|OPS_16|OPA_Imm|OPAP_SImm8, 0} }, + OPT_Imm|OPS_16|OPA_Imm|OPAP_SImm8, 0} }, { CPU_Any, MOD_Gap0|MOD_SpAdd|MOD_GasSufW, 16, 0, 0, 1, {0x83, 0x81, 0}, 0, 2, {OPT_RM|OPS_16|OPA_EA, - OPT_Imm|OPS_16|OPS_Relaxed|OPA_Imm|OPAP_SImm8, 0} }, + OPT_Imm|OPS_16|OPS_Relaxed|OPA_Imm|OPAP_SImm8, 0} }, { CPU_386, MOD_Gap0|MOD_SpAdd|MOD_GasSufL, 32, 0, 0, 1, {0x83, 0, 0}, 0, 2, {OPT_RM|OPS_32|OPA_EA, OPT_Imm|OPS_8|OPA_SImm, 0} }, @@ -894,7 +894,7 @@ static const x86_insn_info arith_insn[] = { OPT_Imm|OPS_32|OPA_Imm|OPAP_SImm8, 0} }, { CPU_386, MOD_Gap0|MOD_SpAdd|MOD_GasSufL, 32, 0, 0, 1, {0x83, 0x81, 0}, 0, 2, {OPT_RM|OPS_32|OPA_EA, - OPT_Imm|OPS_32|OPS_Relaxed|OPA_Imm|OPAP_SImm8, 0} }, + OPT_Imm|OPS_32|OPS_Relaxed|OPA_Imm|OPAP_SImm8, 0} }, /* No relaxed-RM mode for 64-bit destinations; see above Not64 comment. */ { CPU_Hammer|CPU_64, MOD_Gap0|MOD_SpAdd|MOD_GasSufQ, 64, 0, 0, 1, @@ -1137,10 +1137,10 @@ static const x86_insn_info shlrd_insn[] = { OPT_Creg|OPS_8|OPA_None} }, { CPU_Hammer|CPU_64, MOD_Op1Add|MOD_GasSufQ, 64, 0, 0, 2, {0x0F, 0x00, 0}, 0, 3, {OPT_RM|OPS_64|OPS_Relaxed|OPA_EA, OPT_Reg|OPS_64|OPA_Spare, - OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm} }, + OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm} }, { CPU_Hammer|CPU_64, MOD_Op1Add|MOD_GasSufQ, 64, 0, 0, 2, {0x0F, 0x01, 0}, 0, 3, {OPT_RM|OPS_64|OPS_Relaxed|OPA_EA, OPT_Reg|OPS_64|OPA_Spare, - OPT_Creg|OPS_8|OPA_None} }, + OPT_Creg|OPS_8|OPA_None} }, /* GAS parser supports two-operand form for shift with CL count */ { CPU_386, MOD_Op1Add|MOD_GasOnly|MOD_GasSufW, 16, 0, 0, 2, {0x0F, 0x01, 0}, 0, 2, @@ -1298,14 +1298,14 @@ static const x86_insn_info retnf_insn[] = { static const x86_insn_info enter_insn[] = { { CPU_186|CPU_Not64, MOD_GasNoRev|MOD_GasSufL, 0, 0, 0, 1, {0xC8, 0, 0}, 0, 2, {OPT_Imm|OPS_16|OPS_Relaxed|OPA_EA|OPAP_A16, - OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm, 0} }, + OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm, 0} }, { CPU_Hammer|CPU_64, MOD_GasNoRev|MOD_GasSufQ, 64, 64, 0, 1, {0xC8, 0, 0}, 0, 2, {OPT_Imm|OPS_16|OPS_Relaxed|OPA_EA|OPAP_A16, - OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm, 0} }, + OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm, 0} }, /* GAS suffix version */ { CPU_186, MOD_GasOnly|MOD_GasNoRev|MOD_GasSufW, 16, 0, 0, 1, {0xC8, 0, 0}, 0, 2, {OPT_Imm|OPS_16|OPS_Relaxed|OPA_EA|OPAP_A16, - OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm, 0} }, + OPT_Imm|OPS_8|OPS_Relaxed|OPA_Imm, 0} }, }; /* Conditional jumps */ @@ -2097,7 +2097,7 @@ static const x86_insn_info xbts_insn[] = { static void x86_finalize_common(x86_common *common, const x86_insn_info *info, - uintptr_t mode_bits) + uintptr_t mode_bits) { common->addrsize = 0; common->opersize = info->opersize; @@ -2116,9 +2116,9 @@ x86_finalize_opcode(x86_opcode *opcode, const x86_insn_info *info) static void x86_finalize_jmpfar(yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, - const uintptr_t data[4], int num_operands, - yasm_insn_operands *operands, int num_prefixes, - uintptr_t **prefixes, const x86_insn_info *info) + const uintptr_t data[4], int num_operands, + yasm_insn_operands *operands, int num_prefixes, + uintptr_t **prefixes, const x86_insn_info *info) { x86_jmpfar *jmpfar; yasm_insn_operand *op; @@ -2131,32 +2131,32 @@ x86_finalize_jmpfar(yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, op = yasm_ops_first(operands); if (op->type == YASM_INSN__OPERAND_IMM && - yasm_expr_is_op(op->data.val, YASM_EXPR_SEGOFF)) { - /* SEG:OFF expression; split it. */ - segment = yasm_expr_extract_segoff(&op->data.val); - if (!segment) - yasm_internal_error(N_("didn't get SEG:OFF expression in jmpfar")); - if (yasm_value_finalize_expr(&jmpfar->segment, segment, prev_bc, 16)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("jump target segment too complex")); - if (yasm_value_finalize_expr(&jmpfar->offset, op->data.val, prev_bc, - 0)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("jump target offset too complex")); + yasm_expr_is_op(op->data.val, YASM_EXPR_SEGOFF)) { + /* SEG:OFF expression; split it. */ + segment = yasm_expr_extract_segoff(&op->data.val); + if (!segment) + yasm_internal_error(N_("didn't get SEG:OFF expression in jmpfar")); + if (yasm_value_finalize_expr(&jmpfar->segment, segment, prev_bc, 16)) + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("jump target segment too complex")); + if (yasm_value_finalize_expr(&jmpfar->offset, op->data.val, prev_bc, + 0)) + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("jump target offset too complex")); } else if (op->targetmod == X86_FAR) { - /* "FAR imm" target needs to become "seg imm:imm". */ - if (yasm_value_finalize_expr(&jmpfar->offset, - yasm_expr_copy(op->data.val), prev_bc, 0) - || yasm_value_finalize_expr(&jmpfar->segment, op->data.val, - prev_bc, 16)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("jump target expression too complex")); - jmpfar->segment.seg_of = 1; + /* "FAR imm" target needs to become "seg imm:imm". */ + if (yasm_value_finalize_expr(&jmpfar->offset, + yasm_expr_copy(op->data.val), prev_bc, 0) + || yasm_value_finalize_expr(&jmpfar->segment, op->data.val, + prev_bc, 16)) + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("jump target expression too complex")); + jmpfar->segment.seg_of = 1; } else - yasm_internal_error(N_("didn't get FAR expression in jmpfar")); + yasm_internal_error(N_("didn't get FAR expression in jmpfar")); yasm_x86__bc_apply_prefixes((x86_common *)jmpfar, NULL, num_prefixes, - prefixes); + prefixes); /* Transform the bytecode */ yasm_x86__bc_transform_jmpfar(bc, jmpfar); @@ -2164,9 +2164,9 @@ x86_finalize_jmpfar(yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, static void x86_finalize_jmp(yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, - const uintptr_t data[4], int num_operands, - yasm_insn_operands *operands, int num_prefixes, - uintptr_t **prefixes, const x86_insn_info *jinfo) + const uintptr_t data[4], int num_operands, + yasm_insn_operands *operands, int num_prefixes, + uintptr_t **prefixes, const x86_insn_info *jinfo) { x86_jmp *jmp; int num_info = (int)(data[1]&0xFF); @@ -2180,39 +2180,39 @@ x86_finalize_jmp(yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, /* We know the target is in operand 0, but sanity check for Imm. */ op = yasm_ops_first(operands); if (op->type != YASM_INSN__OPERAND_IMM) - yasm_internal_error(N_("invalid operand conversion")); + yasm_internal_error(N_("invalid operand conversion")); jmp = yasm_xmalloc(sizeof(x86_jmp)); x86_finalize_common(&jmp->common, jinfo, mode_bits); if (yasm_value_finalize_expr(&jmp->target, op->data.val, prev_bc, 0)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("jump target expression too complex")); + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("jump target expression too complex")); if (jmp->target.seg_of || jmp->target.rshift || jmp->target.curpos_rel) - yasm_error_set(YASM_ERROR_VALUE, N_("invalid jump target")); + yasm_error_set(YASM_ERROR_VALUE, N_("invalid jump target")); yasm_value_set_curpos_rel(&jmp->target, bc, 0); jmp->target.jump_target = 1; /* See if the user explicitly specified short/near/far. */ switch ((int)(jinfo->operands[0] & OPTM_MASK)) { - case OPTM_Short: - jmp->op_sel = JMP_SHORT_FORCED; - break; - case OPTM_Near: - jmp->op_sel = JMP_NEAR_FORCED; - break; - default: - jmp->op_sel = JMP_NONE; + case OPTM_Short: + jmp->op_sel = JMP_SHORT_FORCED; + break; + case OPTM_Near: + jmp->op_sel = JMP_NEAR_FORCED; + break; + default: + jmp->op_sel = JMP_NONE; } /* Check for address size setting in second operand, if present */ if (jinfo->num_operands > 1 && - (jinfo->operands[1] & OPA_MASK) == OPA_AdSizeR) - jmp->common.addrsize = (unsigned char) - size_lookup[(jinfo->operands[1] & OPS_MASK)>>OPS_SHIFT]; + (jinfo->operands[1] & OPA_MASK) == OPA_AdSizeR) + jmp->common.addrsize = (unsigned char) + size_lookup[(jinfo->operands[1] & OPS_MASK)>>OPS_SHIFT]; /* Check for address size override */ if (jinfo->modifiers & MOD_AdSizeR) - jmp->common.addrsize = (unsigned char)(mod_data & 0xFF); + jmp->common.addrsize = (unsigned char)(mod_data & 0xFF); /* Scan through other infos for this insn looking for short/near versions. * Needs to match opersize and number of operands, also be within CPU. @@ -2220,57 +2220,57 @@ x86_finalize_jmp(yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, jmp->shortop.len = 0; jmp->nearop.len = 0; for (; num_info>0 && (jmp->shortop.len == 0 || jmp->nearop.len == 0); - num_info--, info++) { - unsigned long cpu = info->cpu; - - if ((cpu & CPU_64) && mode_bits != 64) - continue; - if ((cpu & CPU_Not64) && mode_bits == 64) - continue; - cpu &= ~(CPU_64 | CPU_Not64); - - if ((data[2] & cpu) != cpu) - continue; - - if (info->num_operands == 0) - continue; - - if ((info->operands[0] & OPA_MASK) != OPA_JmpRel) - continue; - - if (info->opersize != jmp->common.opersize) - continue; - - switch ((int)(info->operands[0] & OPTM_MASK)) { - case OPTM_Short: - x86_finalize_opcode(&jmp->shortop, info); - if (info->modifiers & MOD_Op0Add) - jmp->shortop.opcode[0] += (unsigned char)(mod_data & 0xFF); - break; - case OPTM_Near: - x86_finalize_opcode(&jmp->nearop, info); - if (info->modifiers & MOD_Op1Add) - jmp->nearop.opcode[1] += (unsigned char)(mod_data & 0xFF); - break; - } + num_info--, info++) { + unsigned long cpu = info->cpu; + + if ((cpu & CPU_64) && mode_bits != 64) + continue; + if ((cpu & CPU_Not64) && mode_bits == 64) + continue; + cpu &= ~(CPU_64 | CPU_Not64); + + if ((data[2] & cpu) != cpu) + continue; + + if (info->num_operands == 0) + continue; + + if ((info->operands[0] & OPA_MASK) != OPA_JmpRel) + continue; + + if (info->opersize != jmp->common.opersize) + continue; + + switch ((int)(info->operands[0] & OPTM_MASK)) { + case OPTM_Short: + x86_finalize_opcode(&jmp->shortop, info); + if (info->modifiers & MOD_Op0Add) + jmp->shortop.opcode[0] += (unsigned char)(mod_data & 0xFF); + break; + case OPTM_Near: + x86_finalize_opcode(&jmp->nearop, info); + if (info->modifiers & MOD_Op1Add) + jmp->nearop.opcode[1] += (unsigned char)(mod_data & 0xFF); + break; + } } if ((jmp->op_sel == JMP_SHORT_FORCED) && (jmp->nearop.len == 0)) - yasm_error_set(YASM_ERROR_TYPE, - N_("no SHORT form of that jump instruction exists")); + yasm_error_set(YASM_ERROR_TYPE, + N_("no SHORT form of that jump instruction exists")); if ((jmp->op_sel == JMP_NEAR_FORCED) && (jmp->shortop.len == 0)) - yasm_error_set(YASM_ERROR_TYPE, - N_("no NEAR form of that jump instruction exists")); + yasm_error_set(YASM_ERROR_TYPE, + N_("no NEAR form of that jump instruction exists")); if (jmp->op_sel == JMP_NONE) { - if (jmp->nearop.len == 0) - jmp->op_sel = JMP_SHORT_FORCED; - if (jmp->shortop.len == 0) - jmp->op_sel = JMP_NEAR_FORCED; + if (jmp->nearop.len == 0) + jmp->op_sel = JMP_SHORT_FORCED; + if (jmp->shortop.len == 0) + jmp->op_sel = JMP_NEAR_FORCED; } yasm_x86__bc_apply_prefixes((x86_common *)jmp, NULL, num_prefixes, - prefixes); + prefixes); /* Transform the bytecode */ yasm_x86__bc_transform_jmp(bc, jmp); @@ -2278,10 +2278,10 @@ x86_finalize_jmp(yasm_arch *arch, yasm_bytecode *bc, yasm_bytecode *prev_bc, static const x86_insn_info * x86_find_match(yasm_arch *arch, int num_info, const x86_insn_info *info, - uintptr_t cpu, unsigned int mode_bits, unsigned int suffix, - int num_operands, yasm_insn_operand **ops, - yasm_insn_operand **rev_ops, const unsigned int *size_lookup, - int bypass) + uintptr_t cpu, unsigned int mode_bits, unsigned int suffix, + int num_operands, yasm_insn_operand **ops, + yasm_insn_operand **rev_ops, const unsigned int *size_lookup, + int bypass) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch; int found = 0; @@ -2290,330 +2290,330 @@ x86_find_match(yasm_arch *arch, int num_info, const x86_insn_info *info, * First match wins. */ for (; num_info>0 && !found; num_info--, info++) { - yasm_insn_operand *op, **use_ops; - unsigned long icpu; - unsigned int size; - int mismatch = 0; - int i; - - /* Match CPU */ - icpu = info->cpu; - - if ((icpu & CPU_64) && mode_bits != 64) - continue; - if ((icpu & CPU_Not64) && mode_bits == 64) - continue; - icpu &= ~(CPU_64 | CPU_Not64); - - if (bypass != 7 && (cpu & icpu) != icpu) - continue; - - /* Match # of operands */ - if (num_operands != info->num_operands) - continue; - - /* Match parser mode */ - if ((info->modifiers & MOD_GasOnly) - && arch_x86->parser != X86_PARSER_GAS) - continue; - if ((info->modifiers & MOD_GasIllegal) - && arch_x86->parser == X86_PARSER_GAS) - continue; - - /* Match suffix (if required) */ - if (suffix != 0 && suffix != 0x80 - && ((suffix<modifiers) == 0) - continue; - - /* Use reversed operands in GAS mode if not otherwise specified */ - use_ops = ops; - if (arch_x86->parser == X86_PARSER_GAS - && !(info->modifiers & MOD_GasNoRev)) - use_ops = rev_ops; - - if (num_operands == 0) { - found = 1; /* no operands -> must have a match here. */ - break; - } - - /* Match each operand type and size */ - for (i = 0, op = use_ops[0]; op && inum_operands && !mismatch; - op = use_ops[++i]) { - /* Check operand type */ - switch ((int)(info->operands[i] & OPT_MASK)) { - case OPT_Imm: - if (op->type != YASM_INSN__OPERAND_IMM) - mismatch = 1; - break; - case OPT_RM: - if (op->type == YASM_INSN__OPERAND_MEMORY) - break; - /*@fallthrough@*/ - case OPT_Reg: - if (op->type != YASM_INSN__OPERAND_REG) - mismatch = 1; - else { - switch ((x86_expritem_reg_size)(op->data.reg&~0xFUL)) { - case X86_REG8: - case X86_REG8X: - case X86_REG16: - case X86_REG32: - case X86_REG64: - case X86_FPUREG: - break; - default: - mismatch = 1; - break; - } - } - break; - case OPT_Mem: - if (op->type != YASM_INSN__OPERAND_MEMORY) - mismatch = 1; - break; - case OPT_SIMDRM: - if (op->type == YASM_INSN__OPERAND_MEMORY) - break; - /*@fallthrough@*/ - case OPT_SIMDReg: - if (op->type != YASM_INSN__OPERAND_REG) - mismatch = 1; - else { - switch ((x86_expritem_reg_size)(op->data.reg&~0xFUL)) { - case X86_MMXREG: - case X86_XMMREG: - break; - default: - mismatch = 1; - break; - } - } - break; - case OPT_SegReg: - if (op->type != YASM_INSN__OPERAND_SEGREG) - mismatch = 1; - break; - case OPT_CRReg: - if (op->type != YASM_INSN__OPERAND_REG || - (op->data.reg & ~0xFUL) != X86_CRREG) - mismatch = 1; - break; - case OPT_DRReg: - if (op->type != YASM_INSN__OPERAND_REG || - (op->data.reg & ~0xFUL) != X86_DRREG) - mismatch = 1; - break; - case OPT_TRReg: - if (op->type != YASM_INSN__OPERAND_REG || - (op->data.reg & ~0xFUL) != X86_TRREG) - mismatch = 1; - break; - case OPT_ST0: - if (op->type != YASM_INSN__OPERAND_REG || - op->data.reg != X86_FPUREG) - mismatch = 1; - break; - case OPT_Areg: - if (op->type != YASM_INSN__OPERAND_REG || - ((info->operands[i] & OPS_MASK) == OPS_8 && - op->data.reg != (X86_REG8 | 0) && - op->data.reg != (X86_REG8X | 0)) || - ((info->operands[i] & OPS_MASK) == OPS_16 && - op->data.reg != (X86_REG16 | 0)) || - ((info->operands[i] & OPS_MASK) == OPS_32 && - op->data.reg != (X86_REG32 | 0)) || - ((info->operands[i] & OPS_MASK) == OPS_64 && - op->data.reg != (X86_REG64 | 0))) - mismatch = 1; - break; - case OPT_Creg: - if (op->type != YASM_INSN__OPERAND_REG || - ((info->operands[i] & OPS_MASK) == OPS_8 && - op->data.reg != (X86_REG8 | 1) && - op->data.reg != (X86_REG8X | 1)) || - ((info->operands[i] & OPS_MASK) == OPS_16 && - op->data.reg != (X86_REG16 | 1)) || - ((info->operands[i] & OPS_MASK) == OPS_32 && - op->data.reg != (X86_REG32 | 1)) || - ((info->operands[i] & OPS_MASK) == OPS_64 && - op->data.reg != (X86_REG64 | 1))) - mismatch = 1; - break; - case OPT_Dreg: - if (op->type != YASM_INSN__OPERAND_REG || - ((info->operands[i] & OPS_MASK) == OPS_8 && - op->data.reg != (X86_REG8 | 2) && - op->data.reg != (X86_REG8X | 2)) || - ((info->operands[i] & OPS_MASK) == OPS_16 && - op->data.reg != (X86_REG16 | 2)) || - ((info->operands[i] & OPS_MASK) == OPS_32 && - op->data.reg != (X86_REG32 | 2)) || - ((info->operands[i] & OPS_MASK) == OPS_64 && - op->data.reg != (X86_REG64 | 2))) - mismatch = 1; - break; - case OPT_CS: - if (op->type != YASM_INSN__OPERAND_SEGREG || - (op->data.reg & 0xF) != 1) - mismatch = 1; - break; - case OPT_DS: - if (op->type != YASM_INSN__OPERAND_SEGREG || - (op->data.reg & 0xF) != 3) - mismatch = 1; - break; - case OPT_ES: - if (op->type != YASM_INSN__OPERAND_SEGREG || - (op->data.reg & 0xF) != 0) - mismatch = 1; - break; - case OPT_FS: - if (op->type != YASM_INSN__OPERAND_SEGREG || - (op->data.reg & 0xF) != 4) - mismatch = 1; - break; - case OPT_GS: - if (op->type != YASM_INSN__OPERAND_SEGREG || - (op->data.reg & 0xF) != 5) - mismatch = 1; - break; - case OPT_SS: - if (op->type != YASM_INSN__OPERAND_SEGREG || - (op->data.reg & 0xF) != 2) - mismatch = 1; - break; - case OPT_CR4: - if (op->type != YASM_INSN__OPERAND_REG || - op->data.reg != (X86_CRREG | 4)) - mismatch = 1; - break; - case OPT_MemOffs: - if (op->type != YASM_INSN__OPERAND_MEMORY || - yasm_expr__contains(yasm_ea_get_disp(op->data.ea), - YASM_EXPR_REG)) - mismatch = 1; - break; - case OPT_Imm1: - if (op->type == YASM_INSN__OPERAND_IMM) { - const yasm_intnum *num; - num = yasm_expr_get_intnum(&op->data.val, 0); - if (!num || !yasm_intnum_is_pos1(num)) - mismatch = 1; - } else - mismatch = 1; - break; - case OPT_ImmNotSegOff: - if (op->type != YASM_INSN__OPERAND_IMM || - op->targetmod != 0 || - yasm_expr_is_op(op->data.val, YASM_EXPR_SEGOFF)) - mismatch = 1; - break; - default: - yasm_internal_error(N_("invalid operand type")); - } - - if (mismatch) - break; - - /* Check operand size */ - size = size_lookup[(info->operands[i] & OPS_MASK)>>OPS_SHIFT]; - if (suffix != 0) { - /* Require relaxed operands for GAS mode (don't allow - * per-operand sizing). - */ - if (op->type == YASM_INSN__OPERAND_REG && op->size == 0) { - /* Register size must exactly match */ - if (yasm_x86__get_reg_size(arch, op->data.reg) != size) - mismatch = 1; - } else if (((info->operands[i] & OPT_MASK) == OPT_Imm - || (info->operands[i] & OPT_MASK) == OPT_ImmNotSegOff - || (info->operands[i] & OPT_MASK) == OPT_Imm1) - && (info->operands[i] & OPS_RMASK) != OPS_Relaxed - && (info->operands[i] & OPA_MASK) != OPA_JmpRel) - mismatch = 1; - } else { - if (op->type == YASM_INSN__OPERAND_REG && op->size == 0) { - /* Register size must exactly match */ - if ((bypass == 4 && i == 0) || (bypass == 5 && i == 1) - || (bypass == 6 && i == 3)) - ; - else if (yasm_x86__get_reg_size(arch, - op->data.reg) != size) - mismatch = 1; - } else { - if ((bypass == 1 && i == 0) || (bypass == 2 && i == 1) - || (bypass == 3 && i == 3)) - ; - else if ((info->operands[i] & OPS_RMASK) == OPS_Relaxed) { - /* Relaxed checking */ - if (size != 0 && op->size != size && op->size != 0) - mismatch = 1; - } else { - /* Strict checking */ - if (op->size != size) - mismatch = 1; - } - } - } - - if (mismatch) - break; - - /* Check for 64-bit effective address size in NASM mode */ - if (suffix == 0 && op->type == YASM_INSN__OPERAND_MEMORY) { - if ((info->operands[i] & OPEAS_MASK) == OPEAS_64) { - if (op->data.ea->disp.size != 64) - mismatch = 1; - } else if (op->data.ea->disp.size == 64) - mismatch = 1; - } - - if (mismatch) - break; - - /* Check target modifier */ - switch ((int)(info->operands[i] & OPTM_MASK)) { - case OPTM_None: - if (op->targetmod != 0) - mismatch = 1; - break; - case OPTM_Near: - if (op->targetmod != X86_NEAR) - mismatch = 1; - break; - case OPTM_Short: - if (op->targetmod != X86_SHORT) - mismatch = 1; - break; - case OPTM_Far: - if (op->targetmod != X86_FAR) - mismatch = 1; - break; - case OPTM_To: - if (op->targetmod != X86_TO) - mismatch = 1; - break; - default: - yasm_internal_error(N_("invalid target modifier type")); - } - } - - if (!mismatch) { - found = 1; - break; - } + yasm_insn_operand *op, **use_ops; + unsigned long icpu; + unsigned int size; + int mismatch = 0; + int i; + + /* Match CPU */ + icpu = info->cpu; + + if ((icpu & CPU_64) && mode_bits != 64) + continue; + if ((icpu & CPU_Not64) && mode_bits == 64) + continue; + icpu &= ~(CPU_64 | CPU_Not64); + + if (bypass != 7 && (cpu & icpu) != icpu) + continue; + + /* Match # of operands */ + if (num_operands != info->num_operands) + continue; + + /* Match parser mode */ + if ((info->modifiers & MOD_GasOnly) + && arch_x86->parser != X86_PARSER_GAS) + continue; + if ((info->modifiers & MOD_GasIllegal) + && arch_x86->parser == X86_PARSER_GAS) + continue; + + /* Match suffix (if required) */ + if (suffix != 0 && suffix != 0x80 + && ((suffix<modifiers) == 0) + continue; + + /* Use reversed operands in GAS mode if not otherwise specified */ + use_ops = ops; + if (arch_x86->parser == X86_PARSER_GAS + && !(info->modifiers & MOD_GasNoRev)) + use_ops = rev_ops; + + if (num_operands == 0) { + found = 1; /* no operands -> must have a match here. */ + break; + } + + /* Match each operand type and size */ + for (i = 0, op = use_ops[0]; op && inum_operands && !mismatch; + op = use_ops[++i]) { + /* Check operand type */ + switch ((int)(info->operands[i] & OPT_MASK)) { + case OPT_Imm: + if (op->type != YASM_INSN__OPERAND_IMM) + mismatch = 1; + break; + case OPT_RM: + if (op->type == YASM_INSN__OPERAND_MEMORY) + break; + /*@fallthrough@*/ + case OPT_Reg: + if (op->type != YASM_INSN__OPERAND_REG) + mismatch = 1; + else { + switch ((x86_expritem_reg_size)(op->data.reg&~0xFUL)) { + case X86_REG8: + case X86_REG8X: + case X86_REG16: + case X86_REG32: + case X86_REG64: + case X86_FPUREG: + break; + default: + mismatch = 1; + break; + } + } + break; + case OPT_Mem: + if (op->type != YASM_INSN__OPERAND_MEMORY) + mismatch = 1; + break; + case OPT_SIMDRM: + if (op->type == YASM_INSN__OPERAND_MEMORY) + break; + /*@fallthrough@*/ + case OPT_SIMDReg: + if (op->type != YASM_INSN__OPERAND_REG) + mismatch = 1; + else { + switch ((x86_expritem_reg_size)(op->data.reg&~0xFUL)) { + case X86_MMXREG: + case X86_XMMREG: + break; + default: + mismatch = 1; + break; + } + } + break; + case OPT_SegReg: + if (op->type != YASM_INSN__OPERAND_SEGREG) + mismatch = 1; + break; + case OPT_CRReg: + if (op->type != YASM_INSN__OPERAND_REG || + (op->data.reg & ~0xFUL) != X86_CRREG) + mismatch = 1; + break; + case OPT_DRReg: + if (op->type != YASM_INSN__OPERAND_REG || + (op->data.reg & ~0xFUL) != X86_DRREG) + mismatch = 1; + break; + case OPT_TRReg: + if (op->type != YASM_INSN__OPERAND_REG || + (op->data.reg & ~0xFUL) != X86_TRREG) + mismatch = 1; + break; + case OPT_ST0: + if (op->type != YASM_INSN__OPERAND_REG || + op->data.reg != X86_FPUREG) + mismatch = 1; + break; + case OPT_Areg: + if (op->type != YASM_INSN__OPERAND_REG || + ((info->operands[i] & OPS_MASK) == OPS_8 && + op->data.reg != (X86_REG8 | 0) && + op->data.reg != (X86_REG8X | 0)) || + ((info->operands[i] & OPS_MASK) == OPS_16 && + op->data.reg != (X86_REG16 | 0)) || + ((info->operands[i] & OPS_MASK) == OPS_32 && + op->data.reg != (X86_REG32 | 0)) || + ((info->operands[i] & OPS_MASK) == OPS_64 && + op->data.reg != (X86_REG64 | 0))) + mismatch = 1; + break; + case OPT_Creg: + if (op->type != YASM_INSN__OPERAND_REG || + ((info->operands[i] & OPS_MASK) == OPS_8 && + op->data.reg != (X86_REG8 | 1) && + op->data.reg != (X86_REG8X | 1)) || + ((info->operands[i] & OPS_MASK) == OPS_16 && + op->data.reg != (X86_REG16 | 1)) || + ((info->operands[i] & OPS_MASK) == OPS_32 && + op->data.reg != (X86_REG32 | 1)) || + ((info->operands[i] & OPS_MASK) == OPS_64 && + op->data.reg != (X86_REG64 | 1))) + mismatch = 1; + break; + case OPT_Dreg: + if (op->type != YASM_INSN__OPERAND_REG || + ((info->operands[i] & OPS_MASK) == OPS_8 && + op->data.reg != (X86_REG8 | 2) && + op->data.reg != (X86_REG8X | 2)) || + ((info->operands[i] & OPS_MASK) == OPS_16 && + op->data.reg != (X86_REG16 | 2)) || + ((info->operands[i] & OPS_MASK) == OPS_32 && + op->data.reg != (X86_REG32 | 2)) || + ((info->operands[i] & OPS_MASK) == OPS_64 && + op->data.reg != (X86_REG64 | 2))) + mismatch = 1; + break; + case OPT_CS: + if (op->type != YASM_INSN__OPERAND_SEGREG || + (op->data.reg & 0xF) != 1) + mismatch = 1; + break; + case OPT_DS: + if (op->type != YASM_INSN__OPERAND_SEGREG || + (op->data.reg & 0xF) != 3) + mismatch = 1; + break; + case OPT_ES: + if (op->type != YASM_INSN__OPERAND_SEGREG || + (op->data.reg & 0xF) != 0) + mismatch = 1; + break; + case OPT_FS: + if (op->type != YASM_INSN__OPERAND_SEGREG || + (op->data.reg & 0xF) != 4) + mismatch = 1; + break; + case OPT_GS: + if (op->type != YASM_INSN__OPERAND_SEGREG || + (op->data.reg & 0xF) != 5) + mismatch = 1; + break; + case OPT_SS: + if (op->type != YASM_INSN__OPERAND_SEGREG || + (op->data.reg & 0xF) != 2) + mismatch = 1; + break; + case OPT_CR4: + if (op->type != YASM_INSN__OPERAND_REG || + op->data.reg != (X86_CRREG | 4)) + mismatch = 1; + break; + case OPT_MemOffs: + if (op->type != YASM_INSN__OPERAND_MEMORY || + yasm_expr__contains(yasm_ea_get_disp(op->data.ea), + YASM_EXPR_REG)) + mismatch = 1; + break; + case OPT_Imm1: + if (op->type == YASM_INSN__OPERAND_IMM) { + const yasm_intnum *num; + num = yasm_expr_get_intnum(&op->data.val, 0); + if (!num || !yasm_intnum_is_pos1(num)) + mismatch = 1; + } else + mismatch = 1; + break; + case OPT_ImmNotSegOff: + if (op->type != YASM_INSN__OPERAND_IMM || + op->targetmod != 0 || + yasm_expr_is_op(op->data.val, YASM_EXPR_SEGOFF)) + mismatch = 1; + break; + default: + yasm_internal_error(N_("invalid operand type")); + } + + if (mismatch) + break; + + /* Check operand size */ + size = size_lookup[(info->operands[i] & OPS_MASK)>>OPS_SHIFT]; + if (suffix != 0) { + /* Require relaxed operands for GAS mode (don't allow + * per-operand sizing). + */ + if (op->type == YASM_INSN__OPERAND_REG && op->size == 0) { + /* Register size must exactly match */ + if (yasm_x86__get_reg_size(arch, op->data.reg) != size) + mismatch = 1; + } else if (((info->operands[i] & OPT_MASK) == OPT_Imm + || (info->operands[i] & OPT_MASK) == OPT_ImmNotSegOff + || (info->operands[i] & OPT_MASK) == OPT_Imm1) + && (info->operands[i] & OPS_RMASK) != OPS_Relaxed + && (info->operands[i] & OPA_MASK) != OPA_JmpRel) + mismatch = 1; + } else { + if (op->type == YASM_INSN__OPERAND_REG && op->size == 0) { + /* Register size must exactly match */ + if ((bypass == 4 && i == 0) || (bypass == 5 && i == 1) + || (bypass == 6 && i == 3)) + ; + else if (yasm_x86__get_reg_size(arch, + op->data.reg) != size) + mismatch = 1; + } else { + if ((bypass == 1 && i == 0) || (bypass == 2 && i == 1) + || (bypass == 3 && i == 3)) + ; + else if ((info->operands[i] & OPS_RMASK) == OPS_Relaxed) { + /* Relaxed checking */ + if (size != 0 && op->size != size && op->size != 0) + mismatch = 1; + } else { + /* Strict checking */ + if (op->size != size) + mismatch = 1; + } + } + } + + if (mismatch) + break; + + /* Check for 64-bit effective address size in NASM mode */ + if (suffix == 0 && op->type == YASM_INSN__OPERAND_MEMORY) { + if ((info->operands[i] & OPEAS_MASK) == OPEAS_64) { + if (op->data.ea->disp.size != 64) + mismatch = 1; + } else if (op->data.ea->disp.size == 64) + mismatch = 1; + } + + if (mismatch) + break; + + /* Check target modifier */ + switch ((int)(info->operands[i] & OPTM_MASK)) { + case OPTM_None: + if (op->targetmod != 0) + mismatch = 1; + break; + case OPTM_Near: + if (op->targetmod != X86_NEAR) + mismatch = 1; + break; + case OPTM_Short: + if (op->targetmod != X86_SHORT) + mismatch = 1; + break; + case OPTM_Far: + if (op->targetmod != X86_FAR) + mismatch = 1; + break; + case OPTM_To: + if (op->targetmod != X86_TO) + mismatch = 1; + break; + default: + yasm_internal_error(N_("invalid target modifier type")); + } + } + + if (!mismatch) { + found = 1; + break; + } } if (!found) - return NULL; + return NULL; return info; } static void x86_match_error(yasm_arch *arch, int num_info, const x86_insn_info *info, - uintptr_t cpu, unsigned int mode_bits, unsigned int suffix, - int num_operands, yasm_insn_operand **ops, - yasm_insn_operand **rev_ops, const unsigned int *size_lookup) + uintptr_t cpu, unsigned int mode_bits, unsigned int suffix, + int num_operands, yasm_insn_operand **ops, + yasm_insn_operand **rev_ops, const unsigned int *size_lookup) { const x86_insn_info *i; int ni; @@ -2623,57 +2623,57 @@ x86_match_error(yasm_arch *arch, int num_info, const x86_insn_info *info, /* Check for matching # of operands */ found = 0; for (ni=num_info, i=info; ni>0; ni--, i++) { - if (num_operands == i->num_operands) { - found = 1; - break; - } + if (num_operands == i->num_operands) { + found = 1; + break; + } } if (!found) { - yasm_error_set(YASM_ERROR_TYPE, N_("invalid number of operands")); - return; + yasm_error_set(YASM_ERROR_TYPE, N_("invalid number of operands")); + return; } for (bypass=1; bypass<8; bypass++) { - i = x86_find_match(arch, num_info, info, cpu, mode_bits, suffix, - num_operands, ops, rev_ops, size_lookup, bypass); - if (i) - break; + i = x86_find_match(arch, num_info, info, cpu, mode_bits, suffix, + num_operands, ops, rev_ops, size_lookup, bypass); + if (i) + break; } switch (bypass) { - case 1: - case 4: - yasm_error_set(YASM_ERROR_TYPE, - N_("invalid size for operand %d"), 1); - break; - case 2: - case 5: - yasm_error_set(YASM_ERROR_TYPE, - N_("invalid size for operand %d"), 2); - break; - case 3: - case 6: - yasm_error_set(YASM_ERROR_TYPE, - N_("invalid size for operand %d"), 3); - break; - case 7: - yasm_error_set(YASM_ERROR_TYPE, - N_("requires CPU%s"), - cpu_find_reverse(i->cpu & ~(CPU_64 | CPU_Not64))); - break; - default: - yasm_error_set(YASM_ERROR_TYPE, - N_("invalid combination of opcode and operands")); + case 1: + case 4: + yasm_error_set(YASM_ERROR_TYPE, + N_("invalid size for operand %d"), 1); + break; + case 2: + case 5: + yasm_error_set(YASM_ERROR_TYPE, + N_("invalid size for operand %d"), 2); + break; + case 3: + case 6: + yasm_error_set(YASM_ERROR_TYPE, + N_("invalid size for operand %d"), 3); + break; + case 7: + yasm_error_set(YASM_ERROR_TYPE, + N_("requires CPU%s"), + cpu_find_reverse(i->cpu & ~(CPU_64 | CPU_Not64))); + break; + default: + yasm_error_set(YASM_ERROR_TYPE, + N_("invalid combination of opcode and operands")); } } void yasm_x86__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, - yasm_bytecode *prev_bc, const uintptr_t data[4], - int num_operands, - /*@null@*/ yasm_insn_operands *operands, - int num_prefixes, uintptr_t **prefixes, - int num_segregs, const uintptr_t *segregs) + yasm_bytecode *prev_bc, const uintptr_t data[4], + int num_operands, + /*@null@*/ yasm_insn_operands *operands, + int num_prefixes, uintptr_t **prefixes, + int num_segregs, const uintptr_t *segregs) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch; x86_insn *insn; @@ -2694,30 +2694,30 @@ yasm_x86__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, size_lookup[7] = mode_bits; if (!info) { - num_info = 1; - info = empty_insn; + num_info = 1; + info = empty_insn; } /* Build local array of operands from list, since we know we have a max * of 3 operands. */ if (num_operands > 3) { - yasm_error_set(YASM_ERROR_TYPE, N_("too many operands")); - return; + yasm_error_set(YASM_ERROR_TYPE, N_("too many operands")); + return; } ops[0] = ops[1] = ops[2] = ops[3] = NULL; for (i = 0, op = yasm_ops_first(operands); op && i < num_operands; - op = yasm_operand_next(op), i++) - ops[i] = op; + op = yasm_operand_next(op), i++) + ops[i] = op; /* If we're running in GAS mode, build a reverse array of the operands * as most GAS instructions have reversed operands from Intel style. */ if (arch_x86->parser == X86_PARSER_GAS) { - rev_ops[0] = rev_ops[1] = rev_ops[2] = rev_ops[3] = NULL; - for (i = num_operands-1, op = yasm_ops_first(operands); op && i >= 0; - op = yasm_operand_next(op), i--) - rev_ops[i] = op; + rev_ops[0] = rev_ops[1] = rev_ops[2] = rev_ops[3] = NULL; + for (i = num_operands-1, op = yasm_ops_first(operands); op && i >= 0; + op = yasm_operand_next(op), i--) + rev_ops[i] = op; } /* If we're running in GAS mode, look at the first insn_info to see @@ -2725,56 +2725,56 @@ yasm_x86__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, * operands and adjust for dereferences / lack thereof. */ if (arch_x86->parser == X86_PARSER_GAS - && (info->operands[0] & OPA_MASK) == OPA_JmpRel) { - for (i = 0, op = ops[0]; op; op = ops[++i]) { - if (!op->deref && (op->type == YASM_INSN__OPERAND_REG - || (op->type == YASM_INSN__OPERAND_MEMORY - && op->data.ea->strong))) - yasm_warn_set(YASM_WARN_GENERAL, - N_("indirect call without `*'")); - if (!op->deref && op->type == YASM_INSN__OPERAND_MEMORY - && !op->data.ea->strong) { - /* Memory that is not dereferenced, and not strong, is - * actually an immediate for the purposes of relative jumps. - */ - if (op->data.ea->segreg != 0) - yasm_warn_set(YASM_WARN_GENERAL, - N_("skipping prefixes on this instruction")); - imm = op->data.ea->disp.abs; - op->data.ea->disp.abs = NULL; - yasm_ea_destroy(op->data.ea); - op->type = YASM_INSN__OPERAND_IMM; - op->data.val = imm; - } - } + && (info->operands[0] & OPA_MASK) == OPA_JmpRel) { + for (i = 0, op = ops[0]; op; op = ops[++i]) { + if (!op->deref && (op->type == YASM_INSN__OPERAND_REG + || (op->type == YASM_INSN__OPERAND_MEMORY + && op->data.ea->strong))) + yasm_warn_set(YASM_WARN_GENERAL, + N_("indirect call without `*'")); + if (!op->deref && op->type == YASM_INSN__OPERAND_MEMORY + && !op->data.ea->strong) { + /* Memory that is not dereferenced, and not strong, is + * actually an immediate for the purposes of relative jumps. + */ + if (op->data.ea->segreg != 0) + yasm_warn_set(YASM_WARN_GENERAL, + N_("skipping prefixes on this instruction")); + imm = op->data.ea->disp.abs; + op->data.ea->disp.abs = NULL; + yasm_ea_destroy(op->data.ea); + op->type = YASM_INSN__OPERAND_IMM; + op->data.val = imm; + } + } } info = x86_find_match(arch, num_info, info, data[2], mode_bits, suffix, - num_operands, ops, rev_ops, size_lookup, 0); + num_operands, ops, rev_ops, size_lookup, 0); if (!info) { - /* Didn't find a match */ - info = (const x86_insn_info *)data[0]; - if (!info) - info = empty_insn; - x86_match_error(arch, num_info, info, data[2], mode_bits, suffix, - num_operands, ops, rev_ops, size_lookup); - return; + /* Didn't find a match */ + info = (const x86_insn_info *)data[0]; + if (!info) + info = empty_insn; + x86_match_error(arch, num_info, info, data[2], mode_bits, suffix, + num_operands, ops, rev_ops, size_lookup); + return; } if (operands) { - switch (info->operands[0] & OPA_MASK) { - case OPA_JmpRel: - /* Shortcut to JmpRel */ - x86_finalize_jmp(arch, bc, prev_bc, data, num_operands, - operands, num_prefixes, prefixes, info); - return; - case OPA_JmpFar: - /* Shortcut to JmpFar */ - x86_finalize_jmpfar(arch, bc, prev_bc, data, num_operands, - operands, num_prefixes, prefixes, info); - return; - } + switch (info->operands[0] & OPA_MASK) { + case OPA_JmpRel: + /* Shortcut to JmpRel */ + x86_finalize_jmp(arch, bc, prev_bc, data, num_operands, + operands, num_prefixes, prefixes, info); + return; + case OPA_JmpFar: + /* Shortcut to JmpFar */ + x86_finalize_jmpfar(arch, bc, prev_bc, data, num_operands, + operands, num_prefixes, prefixes, info); + return; + } } /* Copy what we can from info */ @@ -2793,278 +2793,278 @@ yasm_x86__finalize_insn(yasm_arch *arch, yasm_bytecode *bc, /* Apply modifiers */ if (info->modifiers & MOD_Gap0) - mod_data >>= 8; + mod_data >>= 8; if (info->modifiers & MOD_Op2Add) { - insn->opcode.opcode[2] += (unsigned char)(mod_data & 0xFF); - mod_data >>= 8; + insn->opcode.opcode[2] += (unsigned char)(mod_data & 0xFF); + mod_data >>= 8; } if (info->modifiers & MOD_Gap1) - mod_data >>= 8; + mod_data >>= 8; if (info->modifiers & MOD_Op1Add) { - insn->opcode.opcode[1] += (unsigned char)(mod_data & 0xFF); - mod_data >>= 8; + insn->opcode.opcode[1] += (unsigned char)(mod_data & 0xFF); + mod_data >>= 8; } if (info->modifiers & MOD_Gap2) - mod_data >>= 8; + mod_data >>= 8; if (info->modifiers & MOD_Op0Add) { - insn->opcode.opcode[0] += (unsigned char)(mod_data & 0xFF); - mod_data >>= 8; + insn->opcode.opcode[0] += (unsigned char)(mod_data & 0xFF); + mod_data >>= 8; } if (info->modifiers & MOD_PreAdd) { - insn->special_prefix += (unsigned char)(mod_data & 0xFF); - mod_data >>= 8; + insn->special_prefix += (unsigned char)(mod_data & 0xFF); + mod_data >>= 8; } if (info->modifiers & MOD_SpAdd) { - spare += (unsigned char)(mod_data & 0xFF); - mod_data >>= 8; + spare += (unsigned char)(mod_data & 0xFF); + mod_data >>= 8; } if (info->modifiers & MOD_OpSizeR) { - insn->common.opersize = (unsigned char)(mod_data & 0xFF); - mod_data >>= 8; + insn->common.opersize = (unsigned char)(mod_data & 0xFF); + mod_data >>= 8; } if (info->modifiers & MOD_Imm8) { - imm = yasm_expr_create_ident(yasm_expr_int( - yasm_intnum_create_uint(mod_data & 0xFF)), bc->line); - im_len = 8; - mod_data >>= 8; + imm = yasm_expr_create_ident(yasm_expr_int( + yasm_intnum_create_uint(mod_data & 0xFF)), bc->line); + im_len = 8; + mod_data >>= 8; } if (info->modifiers & MOD_DOpS64R) { - insn->def_opersize_64 = (unsigned char)(mod_data & 0xFF); - mod_data >>= 8; + insn->def_opersize_64 = (unsigned char)(mod_data & 0xFF); + mod_data >>= 8; } if (info->modifiers & MOD_Op1AddSp) { - insn->opcode.opcode[1] += (unsigned char)(mod_data & 0xFF)<<3; - /*mod_data >>= 8;*/ + insn->opcode.opcode[1] += (unsigned char)(mod_data & 0xFF)<<3; + /*mod_data >>= 8;*/ } /* Go through operands and assign */ if (operands) { - yasm_insn_operand **use_ops = ops; - - /* Use reversed operands in GAS mode if not otherwise specified */ - if (arch_x86->parser == X86_PARSER_GAS - && !(info->modifiers & MOD_GasNoRev)) - use_ops = rev_ops; - - for (i = 0, op = use_ops[0]; op && inum_operands; - op = use_ops[++i]) { - switch ((int)(info->operands[i] & OPA_MASK)) { - case OPA_None: - /* Throw away the operand contents */ - switch (op->type) { - case YASM_INSN__OPERAND_REG: - case YASM_INSN__OPERAND_SEGREG: - break; - case YASM_INSN__OPERAND_MEMORY: - yasm_ea_destroy(op->data.ea); - break; - case YASM_INSN__OPERAND_IMM: - yasm_expr_destroy(op->data.val); - break; - } - break; - case OPA_EA: - switch (op->type) { - case YASM_INSN__OPERAND_REG: - insn->x86_ea = yasm_x86__ea_create_reg( - (unsigned long)op->data.reg, &insn->rex, - mode_bits); - break; - case YASM_INSN__OPERAND_SEGREG: - yasm_internal_error( - N_("invalid operand conversion")); - case YASM_INSN__OPERAND_MEMORY: - insn->x86_ea = (x86_effaddr *)op->data.ea; - if ((info->operands[i] & OPT_MASK) == OPT_MemOffs) - /* Special-case for MOV MemOffs instruction */ - yasm_x86__ea_set_disponly(insn->x86_ea); - break; - case YASM_INSN__OPERAND_IMM: - insn->x86_ea = - yasm_x86__ea_create_imm(op->data.val, - size_lookup[(info->operands[i] & - OPS_MASK)>>OPS_SHIFT]); - break; - } - break; - case OPA_Imm: - if (op->type == YASM_INSN__OPERAND_IMM) { - imm = op->data.val; - im_len = size_lookup[(info->operands[i] & - OPS_MASK)>>OPS_SHIFT]; - } else - yasm_internal_error(N_("invalid operand conversion")); - break; - case OPA_SImm: - if (op->type == YASM_INSN__OPERAND_IMM) { - imm = op->data.val; - im_len = size_lookup[(info->operands[i] & - OPS_MASK)>>OPS_SHIFT]; - im_sign = 1; - } else - yasm_internal_error(N_("invalid operand conversion")); - break; - case OPA_Spare: - if (op->type == YASM_INSN__OPERAND_SEGREG) - spare = (unsigned char)(op->data.reg&7); - else if (op->type == YASM_INSN__OPERAND_REG) { - if (yasm_x86__set_rex_from_reg(&insn->rex, &spare, - op->data.reg, mode_bits, X86_REX_R)) - return; - } else - yasm_internal_error(N_("invalid operand conversion")); - break; - case OPA_Op0Add: - if (op->type == YASM_INSN__OPERAND_REG) { - unsigned char opadd; - if (yasm_x86__set_rex_from_reg(&insn->rex, &opadd, - op->data.reg, mode_bits, X86_REX_B)) - return; - insn->opcode.opcode[0] += opadd; - } else - yasm_internal_error(N_("invalid operand conversion")); - break; - case OPA_Op1Add: - if (op->type == YASM_INSN__OPERAND_REG) { - unsigned char opadd; - if (yasm_x86__set_rex_from_reg(&insn->rex, &opadd, - op->data.reg, mode_bits, X86_REX_B)) - return; - insn->opcode.opcode[1] += opadd; - } else - yasm_internal_error(N_("invalid operand conversion")); - break; - case OPA_SpareEA: - if (op->type == YASM_INSN__OPERAND_REG) { - insn->x86_ea = - yasm_x86__ea_create_reg((unsigned long)op->data.reg, - &insn->rex, mode_bits); - if (!insn->x86_ea || - yasm_x86__set_rex_from_reg(&insn->rex, &spare, - op->data.reg, mode_bits, X86_REX_R)) { - if (insn->x86_ea) - yasm_xfree(insn->x86_ea); - yasm_xfree(insn); - return; - } - } else - yasm_internal_error(N_("invalid operand conversion")); - break; - default: - yasm_internal_error(N_("unknown operand action")); - } - - if ((info->operands[i] & OPS_MASK) == OPS_BITS) - insn->common.opersize = (unsigned char)mode_bits; - - switch ((int)(info->operands[i] & OPAP_MASK)) { - case OPAP_None: - break; - case OPAP_SImm8: - /* Check operand strictness; if strict and non-8-bit, - * pre-emptively expand to full size. - * For unspecified size case, still optimize. - */ - if (!(arch_x86->force_strict || op->strict) - || op->size == 0) - insn->postop = X86_POSTOP_SIGNEXT_IMM8; - else if (op->size != 8) { - insn->opcode.opcode[0] = - insn->opcode.opcode[insn->opcode.len]; - insn->opcode.len = 1; - } - break; - case OPAP_ShortMov: - do_postop = OPAP_ShortMov; - break; - case OPAP_A16: - insn->postop = X86_POSTOP_ADDRESS16; - break; - case OPAP_SImm32Avail: - do_postop = OPAP_SImm32Avail; - break; - default: - yasm_internal_error( - N_("unknown operand postponed action")); - } - } + yasm_insn_operand **use_ops = ops; + + /* Use reversed operands in GAS mode if not otherwise specified */ + if (arch_x86->parser == X86_PARSER_GAS + && !(info->modifiers & MOD_GasNoRev)) + use_ops = rev_ops; + + for (i = 0, op = use_ops[0]; op && inum_operands; + op = use_ops[++i]) { + switch ((int)(info->operands[i] & OPA_MASK)) { + case OPA_None: + /* Throw away the operand contents */ + switch (op->type) { + case YASM_INSN__OPERAND_REG: + case YASM_INSN__OPERAND_SEGREG: + break; + case YASM_INSN__OPERAND_MEMORY: + yasm_ea_destroy(op->data.ea); + break; + case YASM_INSN__OPERAND_IMM: + yasm_expr_destroy(op->data.val); + break; + } + break; + case OPA_EA: + switch (op->type) { + case YASM_INSN__OPERAND_REG: + insn->x86_ea = yasm_x86__ea_create_reg( + (unsigned long)op->data.reg, &insn->rex, + mode_bits); + break; + case YASM_INSN__OPERAND_SEGREG: + yasm_internal_error( + N_("invalid operand conversion")); + case YASM_INSN__OPERAND_MEMORY: + insn->x86_ea = (x86_effaddr *)op->data.ea; + if ((info->operands[i] & OPT_MASK) == OPT_MemOffs) + /* Special-case for MOV MemOffs instruction */ + yasm_x86__ea_set_disponly(insn->x86_ea); + break; + case YASM_INSN__OPERAND_IMM: + insn->x86_ea = + yasm_x86__ea_create_imm(op->data.val, + size_lookup[(info->operands[i] & + OPS_MASK)>>OPS_SHIFT]); + break; + } + break; + case OPA_Imm: + if (op->type == YASM_INSN__OPERAND_IMM) { + imm = op->data.val; + im_len = size_lookup[(info->operands[i] & + OPS_MASK)>>OPS_SHIFT]; + } else + yasm_internal_error(N_("invalid operand conversion")); + break; + case OPA_SImm: + if (op->type == YASM_INSN__OPERAND_IMM) { + imm = op->data.val; + im_len = size_lookup[(info->operands[i] & + OPS_MASK)>>OPS_SHIFT]; + im_sign = 1; + } else + yasm_internal_error(N_("invalid operand conversion")); + break; + case OPA_Spare: + if (op->type == YASM_INSN__OPERAND_SEGREG) + spare = (unsigned char)(op->data.reg&7); + else if (op->type == YASM_INSN__OPERAND_REG) { + if (yasm_x86__set_rex_from_reg(&insn->rex, &spare, + op->data.reg, mode_bits, X86_REX_R)) + return; + } else + yasm_internal_error(N_("invalid operand conversion")); + break; + case OPA_Op0Add: + if (op->type == YASM_INSN__OPERAND_REG) { + unsigned char opadd; + if (yasm_x86__set_rex_from_reg(&insn->rex, &opadd, + op->data.reg, mode_bits, X86_REX_B)) + return; + insn->opcode.opcode[0] += opadd; + } else + yasm_internal_error(N_("invalid operand conversion")); + break; + case OPA_Op1Add: + if (op->type == YASM_INSN__OPERAND_REG) { + unsigned char opadd; + if (yasm_x86__set_rex_from_reg(&insn->rex, &opadd, + op->data.reg, mode_bits, X86_REX_B)) + return; + insn->opcode.opcode[1] += opadd; + } else + yasm_internal_error(N_("invalid operand conversion")); + break; + case OPA_SpareEA: + if (op->type == YASM_INSN__OPERAND_REG) { + insn->x86_ea = + yasm_x86__ea_create_reg((unsigned long)op->data.reg, + &insn->rex, mode_bits); + if (!insn->x86_ea || + yasm_x86__set_rex_from_reg(&insn->rex, &spare, + op->data.reg, mode_bits, X86_REX_R)) { + if (insn->x86_ea) + yasm_xfree(insn->x86_ea); + yasm_xfree(insn); + return; + } + } else + yasm_internal_error(N_("invalid operand conversion")); + break; + default: + yasm_internal_error(N_("unknown operand action")); + } + + if ((info->operands[i] & OPS_MASK) == OPS_BITS) + insn->common.opersize = (unsigned char)mode_bits; + + switch ((int)(info->operands[i] & OPAP_MASK)) { + case OPAP_None: + break; + case OPAP_SImm8: + /* Check operand strictness; if strict and non-8-bit, + * pre-emptively expand to full size. + * For unspecified size case, still optimize. + */ + if (!(arch_x86->force_strict || op->strict) + || op->size == 0) + insn->postop = X86_POSTOP_SIGNEXT_IMM8; + else if (op->size != 8) { + insn->opcode.opcode[0] = + insn->opcode.opcode[insn->opcode.len]; + insn->opcode.len = 1; + } + break; + case OPAP_ShortMov: + do_postop = OPAP_ShortMov; + break; + case OPAP_A16: + insn->postop = X86_POSTOP_ADDRESS16; + break; + case OPAP_SImm32Avail: + do_postop = OPAP_SImm32Avail; + break; + default: + yasm_internal_error( + N_("unknown operand postponed action")); + } + } } if (insn->x86_ea) { - yasm_x86__ea_init(insn->x86_ea, spare, prev_bc); - for (i=0; ix86_ea->ea, segregs[i]); + yasm_x86__ea_init(insn->x86_ea, spare, prev_bc); + for (i=0; ix86_ea->ea, segregs[i]); } else if (num_segregs > 0 && insn->special_prefix == 0) { - if (num_segregs > 1) - yasm_warn_set(YASM_WARN_GENERAL, - N_("multiple segment overrides, using leftmost")); - insn->special_prefix = (unsigned char)(segregs[num_segregs-1]>>8); + if (num_segregs > 1) + yasm_warn_set(YASM_WARN_GENERAL, + N_("multiple segment overrides, using leftmost")); + insn->special_prefix = (unsigned char)(segregs[num_segregs-1]>>8); } else if (num_segregs > 0) - yasm_internal_error(N_("unhandled segment prefix")); + yasm_internal_error(N_("unhandled segment prefix")); if (imm) { - insn->imm = yasm_xmalloc(sizeof(yasm_value)); - if (yasm_value_finalize_expr(insn->imm, imm, prev_bc, 0)) - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("immediate expression too complex")); - insn->imm->size = im_len; - insn->imm->sign = im_sign; + insn->imm = yasm_xmalloc(sizeof(yasm_value)); + if (yasm_value_finalize_expr(insn->imm, imm, prev_bc, 0)) + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("immediate expression too complex")); + insn->imm->size = im_len; + insn->imm->sign = im_sign; } else - insn->imm = NULL; + insn->imm = NULL; yasm_x86__bc_apply_prefixes((x86_common *)insn, &insn->rex, num_prefixes, - prefixes); + prefixes); if (insn->postop == X86_POSTOP_ADDRESS16 && insn->common.addrsize) { - yasm_warn_set(YASM_WARN_GENERAL, N_("address size override ignored")); - insn->common.addrsize = 0; + yasm_warn_set(YASM_WARN_GENERAL, N_("address size override ignored")); + insn->common.addrsize = 0; } /* Handle non-span-dependent post-ops here */ switch (do_postop) { - case OPAP_ShortMov: - /* Long (modrm+sib) mov instructions in amd64 can be optimized into - * short mov instructions if a 32-bit address override is applied in - * 64-bit mode to an EA of just an offset (no registers) and the - * target register is al/ax/eax/rax. - */ - if (insn->common.mode_bits == 64 && insn->common.addrsize == 32 && - (!insn->x86_ea->ea.disp.abs || - !yasm_expr__contains(insn->x86_ea->ea.disp.abs, - YASM_EXPR_REG))) { - yasm_x86__ea_set_disponly(insn->x86_ea); - /* Make the short form permanent. */ - insn->opcode.opcode[0] = insn->opcode.opcode[1]; - } - insn->opcode.opcode[1] = 0; /* avoid possible confusion */ - break; - case OPAP_SImm32Avail: - /* Used for 64-bit mov immediate, which can take a sign-extended - * imm32 as well as imm64 values. The imm32 form is put in the - * second byte of the opcode and its ModRM byte is put in the third - * byte of the opcode. - */ - if (!insn->imm->abs || - (yasm_expr_get_intnum(&insn->imm->abs, 0) && - yasm_intnum_check_size( - yasm_expr_get_intnum(&insn->imm->abs, 0), 32, 0, 1))) { - /* Throwaway REX byte */ - unsigned char rex_temp = 0; - - /* Build ModRM EA - CAUTION: this depends on - * opcode 0 being a mov instruction! - */ - insn->x86_ea = yasm_x86__ea_create_reg( - (unsigned long)insn->opcode.opcode[0]-0xB8, &rex_temp, 64); - - /* Make the imm32s form permanent. */ - insn->opcode.opcode[0] = insn->opcode.opcode[1]; - insn->imm->size = 32; - } - insn->opcode.opcode[1] = 0; /* avoid possible confusion */ - break; - default: - break; + case OPAP_ShortMov: + /* Long (modrm+sib) mov instructions in amd64 can be optimized into + * short mov instructions if a 32-bit address override is applied in + * 64-bit mode to an EA of just an offset (no registers) and the + * target register is al/ax/eax/rax. + */ + if (insn->common.mode_bits == 64 && insn->common.addrsize == 32 && + (!insn->x86_ea->ea.disp.abs || + !yasm_expr__contains(insn->x86_ea->ea.disp.abs, + YASM_EXPR_REG))) { + yasm_x86__ea_set_disponly(insn->x86_ea); + /* Make the short form permanent. */ + insn->opcode.opcode[0] = insn->opcode.opcode[1]; + } + insn->opcode.opcode[1] = 0; /* avoid possible confusion */ + break; + case OPAP_SImm32Avail: + /* Used for 64-bit mov immediate, which can take a sign-extended + * imm32 as well as imm64 values. The imm32 form is put in the + * second byte of the opcode and its ModRM byte is put in the third + * byte of the opcode. + */ + if (!insn->imm->abs || + (yasm_expr_get_intnum(&insn->imm->abs, 0) && + yasm_intnum_check_size( + yasm_expr_get_intnum(&insn->imm->abs, 0), 32, 0, 1))) { + /* Throwaway REX byte */ + unsigned char rex_temp = 0; + + /* Build ModRM EA - CAUTION: this depends on + * opcode 0 being a mov instruction! + */ + insn->x86_ea = yasm_x86__ea_create_reg( + (unsigned long)insn->opcode.opcode[0]-0xB8, &rex_temp, 64); + + /* Make the imm32s form permanent. */ + insn->opcode.opcode[0] = insn->opcode.opcode[1]; + insn->imm->size = 32; + } + insn->opcode.opcode[1] = 0; /* avoid possible confusion */ + break; + default: + break; } /* Transform the bytecode */ @@ -3091,13 +3091,13 @@ typedef struct insnprefix_parse_data { /* suffix flags for instructions */ enum { - NONE = 0, - SUF_B = (MOD_GasSufB >> MOD_GasSuf_SHIFT), - SUF_W = (MOD_GasSufW >> MOD_GasSuf_SHIFT), - SUF_L = (MOD_GasSufL >> MOD_GasSuf_SHIFT), - SUF_Q = (MOD_GasSufQ >> MOD_GasSuf_SHIFT), - SUF_S = (MOD_GasSufS >> MOD_GasSuf_SHIFT), - WEAK = 0x80 /* Relaxed operand mode for GAS */ + NONE = 0, + SUF_B = (MOD_GasSufB >> MOD_GasSuf_SHIFT), + SUF_W = (MOD_GasSufW >> MOD_GasSuf_SHIFT), + SUF_L = (MOD_GasSufL >> MOD_GasSuf_SHIFT), + SUF_Q = (MOD_GasSufQ >> MOD_GasSuf_SHIFT), + SUF_S = (MOD_GasSufS >> MOD_GasSuf_SHIFT), + WEAK = 0x80 /* Relaxed operand mode for GAS */ } flags; } insnprefix_parse_data; #define INSN(name, flags, group, mod, cpu) \ @@ -3111,9 +3111,9 @@ typedef struct cpu_parse_data { unsigned long cpu; enum { - CPU_MODE_VERBATIM, - CPU_MODE_SET, - CPU_MODE_CLEAR + CPU_MODE_VERBATIM, + CPU_MODE_SET, + CPU_MODE_CLEAR } mode; } cpu_parse_data; @@ -3124,12 +3124,12 @@ typedef struct regtmod_parse_data { } regtmod_parse_data; #define REG(name, type, index, bits) \ { name, (((unsigned long)YASM_ARCH_REG) << 24) | \ - (((unsigned long)bits) << 16) | (type | index) } + (((unsigned long)bits) << 16) | (type | index) } #define REGGROUP(name, group) \ { name, (((unsigned long)YASM_ARCH_REGGROUP) << 24) | (group) } #define SEGREG(name, prefix, num, bits) \ { name, (((unsigned long)YASM_ARCH_SEGREG) << 24) | \ - (((unsigned long)bits) << 16) | (prefix << 8) | (num) } + (((unsigned long)bits) << 16) | (prefix << 8) | (num) } #define TARGETMOD(name, mod) \ { name, (((unsigned long)YASM_ARCH_TARGETMOD) << 24) | (mod) } @@ -3144,71 +3144,71 @@ cpu_find_reverse(unsigned long cpu) cpuname[0] = '\0'; if (cpu & CPU_Prot) - strcat(cpuname, " Protected"); + strcat(cpuname, " Protected"); if (cpu & CPU_Undoc) - strcat(cpuname, " Undocumented"); + strcat(cpuname, " Undocumented"); if (cpu & CPU_Obs) - strcat(cpuname, " Obsolete"); + strcat(cpuname, " Obsolete"); if (cpu & CPU_Priv) - strcat(cpuname, " Privileged"); + strcat(cpuname, " Privileged"); if (cpu & CPU_FPU) - strcat(cpuname, " FPU"); + strcat(cpuname, " FPU"); if (cpu & CPU_MMX) - strcat(cpuname, " MMX"); + strcat(cpuname, " MMX"); if (cpu & CPU_SSE) - strcat(cpuname, " SSE"); + strcat(cpuname, " SSE"); if (cpu & CPU_SSE2) - strcat(cpuname, " SSE2"); + strcat(cpuname, " SSE2"); if (cpu & CPU_SSE3) - strcat(cpuname, " SSE3"); + strcat(cpuname, " SSE3"); if (cpu & CPU_3DNow) - strcat(cpuname, " 3DNow"); + strcat(cpuname, " 3DNow"); if (cpu & CPU_Cyrix) - strcat(cpuname, " Cyrix"); + strcat(cpuname, " Cyrix"); if (cpu & CPU_AMD) - strcat(cpuname, " AMD"); + strcat(cpuname, " AMD"); if (cpu & CPU_SMM) - strcat(cpuname, " SMM"); + strcat(cpuname, " SMM"); if (cpu & CPU_SVM) - strcat(cpuname, " SVM"); + strcat(cpuname, " SVM"); if (cpu & CPU_PadLock) - strcat(cpuname, " PadLock"); + strcat(cpuname, " PadLock"); if (cpu & CPU_EM64T) - strcat(cpuname, " EM64T"); + strcat(cpuname, " EM64T"); if (cpu & CPU_SSE4) - strcat(cpuname, " SSSE3"); + strcat(cpuname, " SSSE3"); if (cpu & CPU_186) - strcat(cpuname, " 186"); + strcat(cpuname, " 186"); if (cpu & CPU_286) - strcat(cpuname, " 286"); + strcat(cpuname, " 286"); if (cpu & CPU_386) - strcat(cpuname, " 386"); + strcat(cpuname, " 386"); if (cpu & CPU_486) - strcat(cpuname, " 486"); + strcat(cpuname, " 486"); if (cpu & CPU_586) - strcat(cpuname, " 586"); + strcat(cpuname, " 586"); if (cpu & CPU_686) - strcat(cpuname, " 686"); + strcat(cpuname, " 686"); if (cpu & CPU_P3) - strcat(cpuname, " P3"); + strcat(cpuname, " P3"); if (cpu & CPU_P4) - strcat(cpuname, " P4"); + strcat(cpuname, " P4"); if (cpu & CPU_IA64) - strcat(cpuname, " IA64"); + strcat(cpuname, " IA64"); if (cpu & CPU_K6) - strcat(cpuname, " K6"); + strcat(cpuname, " K6"); if (cpu & CPU_Athlon) - strcat(cpuname, " Athlon"); + strcat(cpuname, " Athlon"); if (cpu & CPU_Hammer) - strcat(cpuname, " Hammer"); + strcat(cpuname, " Hammer"); return cpuname; } yasm_arch_insnprefix yasm_x86__parse_check_insnprefix(yasm_arch *arch, uintptr_t data[4], - const char *id, size_t id_len) + const char *id, size_t id_len) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch; /*@null@*/ const insnprefix_parse_data *pdata; @@ -3216,121 +3216,121 @@ yasm_x86__parse_check_insnprefix(yasm_arch *arch, uintptr_t data[4], static char lcaseid[16]; if (id_len > 15) - return YASM_ARCH_NOTINSNPREFIX; + return YASM_ARCH_NOTINSNPREFIX; for (i=0; iparser) { - case X86_PARSER_NASM: - pdata = insnprefix_nasm_find(lcaseid, id_len); - break; - case X86_PARSER_GAS: - pdata = insnprefix_gas_find(lcaseid, id_len); - break; - default: - pdata = NULL; + case X86_PARSER_NASM: + pdata = insnprefix_nasm_find(lcaseid, id_len); + break; + case X86_PARSER_GAS: + pdata = insnprefix_gas_find(lcaseid, id_len); + break; + default: + pdata = NULL; } if (!pdata) - return YASM_ARCH_NOTINSNPREFIX; + return YASM_ARCH_NOTINSNPREFIX; if (pdata->group) { - unsigned long cpu = pdata->data2; - - if ((cpu & CPU_64) && arch_x86->mode_bits != 64) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("`%s' is an instruction in 64-bit mode"), id); - return YASM_ARCH_NOTINSNPREFIX; - } - if ((cpu & CPU_Not64) && arch_x86->mode_bits == 64) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("`%s' invalid in 64-bit mode"), id); - data[0] = (uintptr_t)not64_insn; - data[1] = NELEMS(not64_insn); - data[2] = CPU_Not64; - data[3] = arch_x86->mode_bits; - return YASM_ARCH_INSN; - } - - cpu &= ~(CPU_64 | CPU_Not64); - if ((arch_x86->cpu_enabled & cpu) != cpu) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("`%s' is an instruction in CPU%s"), id, - cpu_find_reverse(cpu)); - return YASM_ARCH_NOTINSNPREFIX; - } - - data[0] = (uintptr_t)pdata->group; - data[1] = pdata->data1; - data[2] = arch_x86->cpu_enabled; - data[3] = (((unsigned long)pdata->flags)<<8) | arch_x86->mode_bits; - return YASM_ARCH_INSN; + unsigned long cpu = pdata->data2; + + if ((cpu & CPU_64) && arch_x86->mode_bits != 64) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("`%s' is an instruction in 64-bit mode"), id); + return YASM_ARCH_NOTINSNPREFIX; + } + if ((cpu & CPU_Not64) && arch_x86->mode_bits == 64) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("`%s' invalid in 64-bit mode"), id); + data[0] = (uintptr_t)not64_insn; + data[1] = NELEMS(not64_insn); + data[2] = CPU_Not64; + data[3] = arch_x86->mode_bits; + return YASM_ARCH_INSN; + } + + cpu &= ~(CPU_64 | CPU_Not64); + if ((arch_x86->cpu_enabled & cpu) != cpu) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("`%s' is an instruction in CPU%s"), id, + cpu_find_reverse(cpu)); + return YASM_ARCH_NOTINSNPREFIX; + } + + data[0] = (uintptr_t)pdata->group; + data[1] = pdata->data1; + data[2] = arch_x86->cpu_enabled; + data[3] = (((unsigned long)pdata->flags)<<8) | arch_x86->mode_bits; + return YASM_ARCH_INSN; } else { - unsigned long type = pdata->data1; - unsigned long value = pdata->data2; - - if (arch_x86->mode_bits == 64 && type == X86_OPERSIZE && value == 32) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("Cannot override data size to 32 bits in 64-bit mode")); - return YASM_ARCH_NOTINSNPREFIX; - } - - if (arch_x86->mode_bits == 64 && type == X86_ADDRSIZE && value == 16) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("Cannot override address size to 16 bits in 64-bit mode")); - return YASM_ARCH_NOTINSNPREFIX; - } - - if ((type == X86_REX || - (value == 64 && (type == X86_OPERSIZE || type == X86_ADDRSIZE))) - && arch_x86->mode_bits != 64) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("`%s' is a prefix in 64-bit mode"), id); - return YASM_ARCH_NOTINSNPREFIX; - } - data[0] = type; - data[1] = value; - return YASM_ARCH_PREFIX; + unsigned long type = pdata->data1; + unsigned long value = pdata->data2; + + if (arch_x86->mode_bits == 64 && type == X86_OPERSIZE && value == 32) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("Cannot override data size to 32 bits in 64-bit mode")); + return YASM_ARCH_NOTINSNPREFIX; + } + + if (arch_x86->mode_bits == 64 && type == X86_ADDRSIZE && value == 16) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("Cannot override address size to 16 bits in 64-bit mode")); + return YASM_ARCH_NOTINSNPREFIX; + } + + if ((type == X86_REX || + (value == 64 && (type == X86_OPERSIZE || type == X86_ADDRSIZE))) + && arch_x86->mode_bits != 64) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("`%s' is a prefix in 64-bit mode"), id); + return YASM_ARCH_NOTINSNPREFIX; + } + data[0] = type; + data[1] = value; + return YASM_ARCH_PREFIX; } } void yasm_x86__parse_cpu(yasm_arch_x86 *arch_x86, const char *cpuid, - size_t cpuid_len) + size_t cpuid_len) { /*@null@*/ const cpu_parse_data *pdata; size_t i; static char lcaseid[16]; if (cpuid_len > 15) - return; + return; for (i=0; imode) { - case CPU_MODE_VERBATIM: - arch_x86->cpu_enabled = pdata->cpu; - break; - case CPU_MODE_SET: - arch_x86->cpu_enabled |= pdata->cpu; - break; - case CPU_MODE_CLEAR: - arch_x86->cpu_enabled &= ~pdata->cpu; - break; + case CPU_MODE_VERBATIM: + arch_x86->cpu_enabled = pdata->cpu; + break; + case CPU_MODE_SET: + arch_x86->cpu_enabled |= pdata->cpu; + break; + case CPU_MODE_CLEAR: + arch_x86->cpu_enabled &= ~pdata->cpu; + break; } } yasm_arch_regtmod yasm_x86__parse_check_regtmod(yasm_arch *arch, uintptr_t *data, - const char *id, size_t id_len) + const char *id, size_t id_len) { yasm_arch_x86 *arch_x86 = (yasm_arch_x86 *)arch; /*@null@*/ const regtmod_parse_data *pdata; @@ -3340,28 +3340,28 @@ yasm_x86__parse_check_regtmod(yasm_arch *arch, uintptr_t *data, yasm_arch_regtmod type; if (id_len > 7) - return YASM_ARCH_NOTREGTMOD; + return YASM_ARCH_NOTREGTMOD; for (i=0; iregtmod >> 24); bits = (pdata->regtmod >> 16) & 0xFF; if (type == YASM_ARCH_REG && bits != 0 && arch_x86->mode_bits != bits) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("`%s' is a register in %u-bit mode"), id, bits); - return YASM_ARCH_NOTREGTMOD; + yasm_warn_set(YASM_WARN_GENERAL, + N_("`%s' is a register in %u-bit mode"), id, bits); + return YASM_ARCH_NOTREGTMOD; } if (type == YASM_ARCH_SEGREG && bits != 0 && arch_x86->mode_bits == bits) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("`%s' segment register ignored in %u-bit mode"), id, - bits); + yasm_warn_set(YASM_WARN_GENERAL, + N_("`%s' segment register ignored in %u-bit mode"), id, + bits); } *data = pdata->regtmod & 0x0000FFFFUL; diff --git a/modules/dbgfmts/codeview/cv-dbgfmt.c b/modules/dbgfmts/codeview/cv-dbgfmt.c index 6ec6407d..6016e821 100644 --- a/modules/dbgfmts/codeview/cv-dbgfmt.c +++ b/modules/dbgfmts/codeview/cv-dbgfmt.c @@ -47,12 +47,12 @@ cv_dbgfmt_create(yasm_object *object, yasm_dbgfmt_module *module, int version) dbgfmt_cv->filenames_allocated = 32; dbgfmt_cv->filenames_size = 0; dbgfmt_cv->filenames = - yasm_xmalloc(sizeof(cv_filename)*dbgfmt_cv->filenames_allocated); + yasm_xmalloc(sizeof(cv_filename)*dbgfmt_cv->filenames_allocated); for (i=0; ifilenames_allocated; i++) { - dbgfmt_cv->filenames[i].pathname = NULL; - dbgfmt_cv->filenames[i].filename = NULL; - dbgfmt_cv->filenames[i].str_off = 0; - dbgfmt_cv->filenames[i].info_off = 0; + dbgfmt_cv->filenames[i].pathname = NULL; + dbgfmt_cv->filenames[i].filename = NULL; + dbgfmt_cv->filenames[i].str_off = 0; + dbgfmt_cv->filenames[i].info_off = 0; } dbgfmt_cv->version = version; @@ -72,8 +72,8 @@ cv_dbgfmt_destroy(/*@only@*/ yasm_dbgfmt *dbgfmt) yasm_dbgfmt_cv *dbgfmt_cv = (yasm_dbgfmt_cv *)dbgfmt; size_t i; for (i=0; ifilenames_size; i++) { - if (dbgfmt_cv->filenames[i].pathname) - yasm_xfree(dbgfmt_cv->filenames[i].pathname); + if (dbgfmt_cv->filenames[i].pathname) + yasm_xfree(dbgfmt_cv->filenames[i].pathname); } yasm_xfree(dbgfmt_cv->filenames); yasm_xfree(dbgfmt); @@ -93,7 +93,7 @@ yasm_cv__append_bc(yasm_section *sect, yasm_bytecode *bc) static void cv_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_cv__generate_symline(object, linemap, errwarns); yasm_cv__generate_type(object); diff --git a/modules/dbgfmts/codeview/cv-dbgfmt.h b/modules/dbgfmts/codeview/cv-dbgfmt.h index 3e8db324..36cdcff3 100644 --- a/modules/dbgfmts/codeview/cv-dbgfmt.h +++ b/modules/dbgfmts/codeview/cv-dbgfmt.h @@ -28,16 +28,16 @@ #define YASM_CV_DBGFMT_H typedef struct { - char *pathname; /* full pathname (drive+basepath+filename) */ - char *filename; /* filename as yasm knows it internally */ - unsigned long str_off; /* offset into pathname string table */ - unsigned long info_off; /* offset into source info table */ - unsigned char digest[16]; /* MD5 digest of source file */ + char *pathname; /* full pathname (drive+basepath+filename) */ + char *filename; /* filename as yasm knows it internally */ + unsigned long str_off; /* offset into pathname string table */ + unsigned long info_off; /* offset into source info table */ + unsigned char digest[16]; /* MD5 digest of source file */ } cv_filename; /* Global data */ typedef struct yasm_dbgfmt_cv { - yasm_dbgfmt_base dbgfmt; /* base structure */ + yasm_dbgfmt_base dbgfmt; /* base structure */ cv_filename *filenames; size_t filenames_size; diff --git a/modules/dbgfmts/codeview/cv-symline.c b/modules/dbgfmts/codeview/cv-symline.c index 068ac566..d40f4010 100644 --- a/modules/dbgfmts/codeview/cv-symline.c +++ b/modules/dbgfmts/codeview/cv-symline.c @@ -37,80 +37,80 @@ #include "cv-dbgfmt.h" enum cv8_symheadtype { - CV8_DEBUG_SYMS = 0xF1, /* CV5 symbol information */ - CV8_LINE_NUMS = 0xF2, /* line numbers for a section */ - CV8_FILE_STRTAB = 0xF3, /* filename string table */ - CV8_FILE_INFO = 0xF4 /* source file info */ + CV8_DEBUG_SYMS = 0xF1, /* CV5 symbol information */ + CV8_LINE_NUMS = 0xF2, /* line numbers for a section */ + CV8_FILE_STRTAB = 0xF3, /* filename string table */ + CV8_FILE_INFO = 0xF4 /* source file info */ }; enum cv_symtype { /* Non-modal Symbols */ - CV_S_COMPILE = 0x0001, /* Compile Flag */ - CV_S_REGISTER = 0x0002, /* Register */ - CV_S_CONSTANT = 0x0003, /* Constant */ - CV_S_UDT = 0x0004, /* User-defined Type */ - CV_S_SSEARCH = 0x0005, /* Start Search */ - CV_S_END = 0x0006, /* End of Block */ - CV_S_SKIP = 0x0007, /* Skip Record */ - CV_S_OBJNAME = 0x0009, /* Object File Name */ - CV_S_ENDARG = 0x000a, /* End of Arguments */ - CV_S_COBOLUDT = 0x000b, /* COBOL User-defined Type */ - CV_S_MANYREG = 0x000c, /* Many Registers */ - CV_S_RETURN = 0x000d, /* Function Return */ - CV_S_ENTRYTHIS = 0x000e, /* "this" at Method Entry */ + CV_S_COMPILE = 0x0001, /* Compile Flag */ + CV_S_REGISTER = 0x0002, /* Register */ + CV_S_CONSTANT = 0x0003, /* Constant */ + CV_S_UDT = 0x0004, /* User-defined Type */ + CV_S_SSEARCH = 0x0005, /* Start Search */ + CV_S_END = 0x0006, /* End of Block */ + CV_S_SKIP = 0x0007, /* Skip Record */ + CV_S_OBJNAME = 0x0009, /* Object File Name */ + CV_S_ENDARG = 0x000a, /* End of Arguments */ + CV_S_COBOLUDT = 0x000b, /* COBOL User-defined Type */ + CV_S_MANYREG = 0x000c, /* Many Registers */ + CV_S_RETURN = 0x000d, /* Function Return */ + CV_S_ENTRYTHIS = 0x000e, /* "this" at Method Entry */ /* Symbols for 16:16 Segmented Architectures */ - CV_S_BPREL16 = 0x0100, /* BP Relative 16:16 */ - CV_S_LDATA16 = 0x0101, /* Local Data 16:16 */ - CV_S_GDATA16 = 0x0102, /* Global Data Symbol 16:16 */ - CV_S_PUB16 = 0x0103, /* Public Symbol 16:16 */ - CV_S_LPROC16 = 0x0104, /* Local Start 16:16 */ - CV_S_GPROC16 = 0x0105, /* Global Start 16:16 */ - CV_S_THUNK16 = 0x0106, /* Thunk Start 16:16 */ - CV_S_BLOCK16 = 0x0107, /* Block Start 16:16 */ - CV_S_WITH16 = 0x0108, /* With Start 16:16 */ - CV_S_LABEL16 = 0x0109, /* Code Label 16:16 */ - CV_S_CEXMODEL16 = 0x0110, /* Change Execution Model 16:16 */ - CV_S_VFTPATH16 = 0x010b, /* Virtual Function Table Path 16:16 */ - CV_S_REGREL16 = 0x010c, /* Register Relative 16:16 */ + CV_S_BPREL16 = 0x0100, /* BP Relative 16:16 */ + CV_S_LDATA16 = 0x0101, /* Local Data 16:16 */ + CV_S_GDATA16 = 0x0102, /* Global Data Symbol 16:16 */ + CV_S_PUB16 = 0x0103, /* Public Symbol 16:16 */ + CV_S_LPROC16 = 0x0104, /* Local Start 16:16 */ + CV_S_GPROC16 = 0x0105, /* Global Start 16:16 */ + CV_S_THUNK16 = 0x0106, /* Thunk Start 16:16 */ + CV_S_BLOCK16 = 0x0107, /* Block Start 16:16 */ + CV_S_WITH16 = 0x0108, /* With Start 16:16 */ + CV_S_LABEL16 = 0x0109, /* Code Label 16:16 */ + CV_S_CEXMODEL16 = 0x0110, /* Change Execution Model 16:16 */ + CV_S_VFTPATH16 = 0x010b, /* Virtual Function Table Path 16:16 */ + CV_S_REGREL16 = 0x010c, /* Register Relative 16:16 */ /* Symbols for 16:32 Segmented Architectures */ - CV_S_BPREL32 = 0x0200, /* BP Relative 16:32 */ - CV_S_LDATA32 = 0x0201, /* Local Data 16:32 */ - CV_S_GDATA32 = 0x0202, /* Global Data Symbol 16:32 */ - CV_S_PUB32 = 0x0203, /* Public Symbol 16:32 */ - CV_S_LPROC32 = 0x0204, /* Local Start 16:32 */ - CV_S_GPROC32 = 0x0205, /* Global Start 16:32 */ - CV_S_THUNK32 = 0x0206, /* Thunk Start 16:32 */ - CV_S_BLOCK32 = 0x0207, /* Block Start 16:32 */ - CV_S_WITH32 = 0x0208, /* With Start 16:32 */ - CV_S_LABEL32 = 0x0209, /* Code Label 16:32 */ - CV_S_CEXMODEL32 = 0x0210, /* Change Execution Model 16:32 */ - CV_S_VFTPATH32 = 0x020b, /* Virtual Function Table Path 16:32 */ - CV_S_REGREL32 = 0x020c, /* Register Relative 16:32 */ - CV_S_LTHREAD32 = 0x020d, /* Local Thread Storage 16:32 */ - CV_S_GTHREAD32 = 0x020e, /* Global Thread Storage 16:32 */ + CV_S_BPREL32 = 0x0200, /* BP Relative 16:32 */ + CV_S_LDATA32 = 0x0201, /* Local Data 16:32 */ + CV_S_GDATA32 = 0x0202, /* Global Data Symbol 16:32 */ + CV_S_PUB32 = 0x0203, /* Public Symbol 16:32 */ + CV_S_LPROC32 = 0x0204, /* Local Start 16:32 */ + CV_S_GPROC32 = 0x0205, /* Global Start 16:32 */ + CV_S_THUNK32 = 0x0206, /* Thunk Start 16:32 */ + CV_S_BLOCK32 = 0x0207, /* Block Start 16:32 */ + CV_S_WITH32 = 0x0208, /* With Start 16:32 */ + CV_S_LABEL32 = 0x0209, /* Code Label 16:32 */ + CV_S_CEXMODEL32 = 0x0210, /* Change Execution Model 16:32 */ + CV_S_VFTPATH32 = 0x020b, /* Virtual Function Table Path 16:32 */ + CV_S_REGREL32 = 0x020c, /* Register Relative 16:32 */ + CV_S_LTHREAD32 = 0x020d, /* Local Thread Storage 16:32 */ + CV_S_GTHREAD32 = 0x020e, /* Global Thread Storage 16:32 */ /* Symbols for MIPS */ - CV_S_LPROCMIPS = 0x0300, /* Local procedure start MIPS */ - CV_S_GPROCMIPS = 0x0301, /* Global procedure start MIPS */ + CV_S_LPROCMIPS = 0x0300, /* Local procedure start MIPS */ + CV_S_GPROCMIPS = 0x0301, /* Global procedure start MIPS */ /* Symbols for CV8 - strings are 0 terminated rather than length-prefix. * Incomplete and unofficial. */ - CV8_S_OBJNAME = 0x1101, /* Object File Name */ - CV8_S_LABEL32 = 0x1105, /* Code Label 16:32 */ - CV8_S_LDATA32 = 0x110c, /* Local Data 16:32 */ - CV8_S_GDATA32 = 0x110d, /* Global Data 16:32 */ - CV8_S_LPROC32 = 0x1110, /* Local Start 16:32 */ - CV8_S_COMPILE = 0x1116 /* Compile Flag */ + CV8_S_OBJNAME = 0x1101, /* Object File Name */ + CV8_S_LABEL32 = 0x1105, /* Code Label 16:32 */ + CV8_S_LDATA32 = 0x110c, /* Local Data 16:32 */ + CV8_S_GDATA32 = 0x110d, /* Global Data 16:32 */ + CV8_S_LPROC32 = 0x1110, /* Local Start 16:32 */ + CV8_S_COMPILE = 0x1116 /* Compile Flag */ }; typedef struct cv8_symhead { enum cv8_symheadtype type; yasm_bytecode *start_prevbc; yasm_bytecode *end_prevbc; - int first; /* nonzero if first symhead in section */ + int first; /* nonzero if first symhead in section */ } cv8_symhead; typedef struct cv8_fileinfo { @@ -132,9 +132,9 @@ typedef struct cv8_lineset { typedef struct cv8_lineinfo { STAILQ_ENTRY(cv8_lineinfo) link; - const cv_filename *fn; /* filename associated with line numbers */ - yasm_section *sect; /* section line numbers are for */ - yasm_symrec *sectsym; /* symbol for beginning of sect */ + const cv_filename *fn; /* filename associated with line numbers */ + yasm_section *sect; /* section line numbers are for */ + yasm_symrec *sectsym; /* symbol for beginning of sect */ unsigned long num_linenums; STAILQ_HEAD(, cv8_lineset) linesets; } cv8_lineinfo; @@ -153,15 +153,15 @@ typedef struct cv_sym { enum cv_symtype type; const char *format; union { - unsigned long i; - void *p; + unsigned long i; + void *p; } args[10]; } cv_sym; /* Bytecode callback function prototypes */ static void cv8_symhead_bc_destroy(void *contents); static void cv8_symhead_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int cv8_symhead_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int cv8_symhead_bc_tobytes @@ -171,7 +171,7 @@ static int cv8_symhead_bc_tobytes static void cv8_fileinfo_bc_destroy(void *contents); static void cv8_fileinfo_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int cv8_fileinfo_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int cv8_fileinfo_bc_tobytes @@ -181,7 +181,7 @@ static int cv8_fileinfo_bc_tobytes static void cv8_lineinfo_bc_destroy(void *contents); static void cv8_lineinfo_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int cv8_lineinfo_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int cv8_lineinfo_bc_tobytes @@ -240,7 +240,7 @@ static const yasm_bytecode_callback cv_sym_bc_callback = { }; static cv8_symhead *cv8_add_symhead(yasm_section *sect, unsigned long type, - int first); + int first); static void cv8_set_symhead_end(cv8_symhead *head, yasm_bytecode *end_prevbc); static yasm_bytecode *cv8_add_fileinfo @@ -256,8 +256,8 @@ cv8_add_sym_objname(yasm_section *sect, /*@keep@*/ char *objname) cv_sym *cvs = yasm_xmalloc(sizeof(cv_sym)); cvs->type = CV8_S_OBJNAME; cvs->format = "wZ"; - cvs->args[0].i = 0; /* signature (0=asm) */ - cvs->args[1].p = objname; /* object filename */ + cvs->args[0].i = 0; /* signature (0=asm) */ + cvs->args[1].p = objname; /* object filename */ bc = yasm_bc_create_common(&cv_sym_bc_callback, cvs, 0); bc->len = cv_sym_size(cvs); @@ -267,27 +267,27 @@ cv8_add_sym_objname(yasm_section *sect, /*@keep@*/ char *objname) static cv_sym * cv8_add_sym_compile(yasm_object *object, yasm_section *sect, - /*@keep@*/ char *creator) + /*@keep@*/ char *creator) { yasm_bytecode *bc; cv_sym *cvs = yasm_xmalloc(sizeof(cv_sym)); cvs->type = CV8_S_COMPILE; cvs->format = "wwwwZh"; - cvs->args[0].i = 3; /* language (3=Masm) */ + cvs->args[0].i = 3; /* language (3=Masm) */ /* target processor; 0xD0 = AMD64 */ if (strcmp(yasm_arch_keyword(object->arch), "x86") == 0) { - if (strcmp(yasm_arch_get_machine(object->arch), "amd64") == 0) - cvs->args[1].i = 0xD0; - else - cvs->args[1].i = 0x6; /* 686, FIXME */ + if (strcmp(yasm_arch_get_machine(object->arch), "amd64") == 0) + cvs->args[1].i = 0xD0; + else + cvs->args[1].i = 0x6; /* 686, FIXME */ } else - cvs->args[1].i = 0; /* XXX: unknown */ + cvs->args[1].i = 0; /* XXX: unknown */ - cvs->args[2].i = 0; /* flags (assume 0 for now) */ - cvs->args[3].i = 0; /* creator version number (assume 0 for now) */ - cvs->args[4].p = creator; /* creator string */ - cvs->args[5].i = 0; /* no pairs of key/value */ + cvs->args[2].i = 0; /* flags (assume 0 for now) */ + cvs->args[3].i = 0; /* creator version number (assume 0 for now) */ + cvs->args[4].p = creator; /* creator string */ + cvs->args[5].i = 0; /* no pairs of key/value */ bc = yasm_bc_create_common(&cv_sym_bc_callback, cvs, 0); bc->len = cv_sym_size(cvs); @@ -302,8 +302,8 @@ cv8_add_sym_label(yasm_section *sect, yasm_symrec *sym) cv_sym *cvs = yasm_xmalloc(sizeof(cv_sym)); cvs->type = CV8_S_LABEL32; cvs->format = "YbZ"; - cvs->args[0].p = sym; /* symrec for label */ - cvs->args[1].i = 0; /* flags (assume 0 for now) */ + cvs->args[0].p = sym; /* symrec for label */ + cvs->args[1].i = 0; /* flags (assume 0 for now) */ cvs->args[2].p = yasm__xstrdup(yasm_symrec_get_name(sym)); bc = yasm_bc_create_common(&cv_sym_bc_callback, cvs, 0); @@ -314,14 +314,14 @@ cv8_add_sym_label(yasm_section *sect, yasm_symrec *sym) static cv_sym * cv8_add_sym_data(yasm_section *sect, unsigned long type, yasm_symrec *sym, - int is_global) + int is_global) { yasm_bytecode *bc; cv_sym *cvs = yasm_xmalloc(sizeof(cv_sym)); cvs->type = is_global ? CV8_S_GDATA32 : CV8_S_LDATA32; cvs->format = "wYZ"; - cvs->args[0].i = type; /* type index */ - cvs->args[1].p = sym; /* symrec for label */ + cvs->args[0].i = type; /* type index */ + cvs->args[1].p = sym; /* symrec for label */ cvs->args[2].p = yasm__xstrdup(yasm_symrec_get_name(sym)); bc = yasm_bc_create_common(&cv_sym_bc_callback, cvs, 0); @@ -332,7 +332,7 @@ cv8_add_sym_data(yasm_section *sect, unsigned long type, yasm_symrec *sym, static size_t cv_dbgfmt_add_file(yasm_dbgfmt_cv *dbgfmt_cv, size_t filenum, - const char *filename) + const char *filename) { char *pathname; size_t i; @@ -343,27 +343,27 @@ cv_dbgfmt_add_file(yasm_dbgfmt_cv *dbgfmt_cv, size_t filenum, /* Put the filename into the filename table */ if (filenum == 0) { - /* Look to see if we already have that filename in the table */ - for (; filenumfilenames_size; filenum++) { - if (!dbgfmt_cv->filenames[filenum].filename || - strcmp(dbgfmt_cv->filenames[filenum].filename, filename) == 0) - break; - } + /* Look to see if we already have that filename in the table */ + for (; filenumfilenames_size; filenum++) { + if (!dbgfmt_cv->filenames[filenum].filename || + strcmp(dbgfmt_cv->filenames[filenum].filename, filename) == 0) + break; + } } else - filenum--; /* array index is 0-based */ + filenum--; /* array index is 0-based */ /* Realloc table if necessary */ if (filenum >= dbgfmt_cv->filenames_allocated) { - size_t old_allocated = dbgfmt_cv->filenames_allocated; - dbgfmt_cv->filenames_allocated = filenum+32; - dbgfmt_cv->filenames = yasm_xrealloc(dbgfmt_cv->filenames, - sizeof(cv_filename)*dbgfmt_cv->filenames_allocated); - for (i=old_allocated; ifilenames_allocated; i++) { - dbgfmt_cv->filenames[i].pathname = NULL; - dbgfmt_cv->filenames[i].filename = NULL; - dbgfmt_cv->filenames[i].str_off = 0; - dbgfmt_cv->filenames[i].info_off = 0; - } + size_t old_allocated = dbgfmt_cv->filenames_allocated; + dbgfmt_cv->filenames_allocated = filenum+32; + dbgfmt_cv->filenames = yasm_xrealloc(dbgfmt_cv->filenames, + sizeof(cv_filename)*dbgfmt_cv->filenames_allocated); + for (i=old_allocated; ifilenames_allocated; i++) { + dbgfmt_cv->filenames[i].pathname = NULL; + dbgfmt_cv->filenames[i].filename = NULL; + dbgfmt_cv->filenames[i].str_off = 0; + dbgfmt_cv->filenames[i].info_off = 0; + } } /* Calculate MD5 checksum of file */ @@ -371,18 +371,18 @@ cv_dbgfmt_add_file(yasm_dbgfmt_cv *dbgfmt_cv, size_t filenum, yasm_md5_init(&context); f = fopen(filename, "rb"); if (!f) - yasm__fatal(N_("codeview: could not open source file")); + yasm__fatal(N_("codeview: could not open source file")); while ((len = fread(buf, 1, 1024, f)) > 0) - yasm_md5_update(&context, buf, (unsigned long)len); + yasm_md5_update(&context, buf, (unsigned long)len); yasm_md5_final(dbgfmt_cv->filenames[filenum].digest, &context); fclose(f); yasm_xfree(buf); /* Actually save in table */ if (dbgfmt_cv->filenames[filenum].pathname) - yasm_xfree(dbgfmt_cv->filenames[filenum].pathname); + yasm_xfree(dbgfmt_cv->filenames[filenum].pathname); if (dbgfmt_cv->filenames[filenum].filename) - yasm_xfree(dbgfmt_cv->filenames[filenum].filename); + yasm_xfree(dbgfmt_cv->filenames[filenum].filename); pathname = yasm__abspath(filename); dbgfmt_cv->filenames[filenum].pathname = pathname; @@ -390,7 +390,7 @@ cv_dbgfmt_add_file(yasm_dbgfmt_cv *dbgfmt_cv, size_t filenum, /* Update table size */ if (filenum >= dbgfmt_cv->filenames_size) - dbgfmt_cv->filenames_size = filenum + 1; + dbgfmt_cv->filenames_size = filenum + 1; return filenum; } @@ -403,7 +403,7 @@ cv_append_str(yasm_section *sect, const char *str) yasm_dvs_initialize(&dvs); yasm_dvs_append(&dvs, yasm_dv_create_string(yasm__xstrdup(str), - strlen(str))); + strlen(str))); bc = yasm_bc_create_data(&dvs, 1, 1, NULL, 0); yasm_bc_finalize(bc, yasm_cv__append_bc(sect, bc)); yasm_bc_calc_len(bc, NULL, NULL); @@ -434,69 +434,69 @@ cv_generate_line_bc(yasm_bytecode *bc, /*@null@*/ void *d) yasm_section *sect = yasm_bc_get_section(bc); if (nextbc && bc->offset == nextbc->offset) - return 0; + return 0; yasm_linemap_lookup(info->linemap, bc->line, &filename, &line); if (!info->cv8_cur_li - || strcmp(filename, info->cv8_cur_li->fn->filename) != 0) { - yasm_bytecode *sectbc; - char symname[8]; - - /* first see if we already have a lineinfo that is for this section and - * filename - */ - STAILQ_FOREACH(info->cv8_cur_li, &info->cv8_lineinfos, link) { - if (sect == info->cv8_cur_li->sect - && strcmp(filename, info->cv8_cur_li->fn->filename) == 0) - break; - } - - if (info->cv8_cur_li) { - info->cv8_cur_ls = STAILQ_LAST(&info->cv8_cur_li->linesets, - cv8_lineset, link); - goto done; /* found one */ - } - - /* Nope; find file */ - for (i=0; ifilenames_size; i++) { - if (strcmp(filename, dbgfmt_cv->filenames[i].filename) == 0) - break; - } - if (i >= dbgfmt_cv->filenames_size) - yasm_internal_error(N_("could not find filename in table")); - - /* and create new lineinfo structure */ - info->cv8_cur_li = yasm_xmalloc(sizeof(cv8_lineinfo)); - info->cv8_cur_li->fn = &dbgfmt_cv->filenames[i]; - info->cv8_cur_li->sect = sect; - sectbc = yasm_section_bcs_first(sect); - if (sectbc->symrecs && sectbc->symrecs[0]) - info->cv8_cur_li->sectsym = sectbc->symrecs[0]; - else { - sprintf(symname, ".%06u", info->num_lineinfos++); - info->cv8_cur_li->sectsym = - yasm_symtab_define_label(info->object->symtab, symname, sectbc, - 1, 0); - } - info->cv8_cur_li->num_linenums = 0; - STAILQ_INIT(&info->cv8_cur_li->linesets); - STAILQ_INSERT_TAIL(&info->cv8_lineinfos, info->cv8_cur_li, link); - info->cv8_cur_ls = NULL; + || strcmp(filename, info->cv8_cur_li->fn->filename) != 0) { + yasm_bytecode *sectbc; + char symname[8]; + + /* first see if we already have a lineinfo that is for this section and + * filename + */ + STAILQ_FOREACH(info->cv8_cur_li, &info->cv8_lineinfos, link) { + if (sect == info->cv8_cur_li->sect + && strcmp(filename, info->cv8_cur_li->fn->filename) == 0) + break; + } + + if (info->cv8_cur_li) { + info->cv8_cur_ls = STAILQ_LAST(&info->cv8_cur_li->linesets, + cv8_lineset, link); + goto done; /* found one */ + } + + /* Nope; find file */ + for (i=0; ifilenames_size; i++) { + if (strcmp(filename, dbgfmt_cv->filenames[i].filename) == 0) + break; + } + if (i >= dbgfmt_cv->filenames_size) + yasm_internal_error(N_("could not find filename in table")); + + /* and create new lineinfo structure */ + info->cv8_cur_li = yasm_xmalloc(sizeof(cv8_lineinfo)); + info->cv8_cur_li->fn = &dbgfmt_cv->filenames[i]; + info->cv8_cur_li->sect = sect; + sectbc = yasm_section_bcs_first(sect); + if (sectbc->symrecs && sectbc->symrecs[0]) + info->cv8_cur_li->sectsym = sectbc->symrecs[0]; + else { + sprintf(symname, ".%06u", info->num_lineinfos++); + info->cv8_cur_li->sectsym = + yasm_symtab_define_label(info->object->symtab, symname, sectbc, + 1, 0); + } + info->cv8_cur_li->num_linenums = 0; + STAILQ_INIT(&info->cv8_cur_li->linesets); + STAILQ_INSERT_TAIL(&info->cv8_lineinfos, info->cv8_cur_li, link); + info->cv8_cur_ls = NULL; } done: /* build new lineset if necessary */ if (!info->cv8_cur_ls || info->cv8_cur_ls->num_pairs >= 126) { - info->cv8_cur_ls = yasm_xmalloc(sizeof(cv8_lineset)); - info->cv8_cur_ls->num_pairs = 0; - STAILQ_INSERT_TAIL(&info->cv8_cur_li->linesets, info->cv8_cur_ls, link); + info->cv8_cur_ls = yasm_xmalloc(sizeof(cv8_lineset)); + info->cv8_cur_ls->num_pairs = 0; + STAILQ_INSERT_TAIL(&info->cv8_cur_li->linesets, info->cv8_cur_ls, link); } /* add linepair for this bytecode */ info->cv8_cur_ls->pairs[info->cv8_cur_ls->num_pairs].offset = bc->offset; info->cv8_cur_ls->pairs[info->cv8_cur_ls->num_pairs].line = - 0x80000000 | line; + 0x80000000 | line; info->cv8_cur_ls->num_pairs++; info->cv8_cur_li->num_linenums++; @@ -509,7 +509,7 @@ cv_generate_line_section(yasm_section *sect, /*@null@*/ void *d) cv_line_info *info = (cv_line_info *)d; if (!yasm_section_is_code(sect)) - return 0; /* not code, so no line data for this section */ + return 0; /* not code, so no line data for this section */ info->cv8_cur_li = NULL; info->cv8_cur_ls = NULL; @@ -538,20 +538,20 @@ cv_generate_sym(yasm_symrec *sym, void *d) * symbols). */ if (name[0] == '.' || !yasm_symrec_get_label(sym, &precbc)) - return 0; + return 0; /* TODO: add data types; until then, just mark everything as UBYTE */ if (yasm_section_is_code(yasm_bc_get_section(precbc))) - cv8_add_sym_label(info->debug_symline, sym); + cv8_add_sym_label(info->debug_symline, sym); else - cv8_add_sym_data(info->debug_symline, 0x20, sym, - yasm_symrec_get_visibility(sym) & YASM_SYM_GLOBAL?1:0); + cv8_add_sym_data(info->debug_symline, 0x20, sym, + yasm_symrec_get_visibility(sym) & YASM_SYM_GLOBAL?1:0); return 0; } yasm_section * yasm_cv__generate_symline(yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_dbgfmt_cv *dbgfmt_cv = (yasm_dbgfmt_cv *)object->dbgfmt; cv_line_info info; @@ -564,14 +564,14 @@ yasm_cv__generate_symline(yasm_object *object, yasm_linemap *linemap, /* Generate filenames based on linemap */ yasm_linemap_traverse_filenames(linemap, dbgfmt_cv, - cv_generate_filename); + cv_generate_filename); info.object = object; info.dbgfmt_cv = dbgfmt_cv; info.linemap = linemap; info.errwarns = errwarns; info.debug_symline = - yasm_object_get_general(object, ".debug$S", 0, 1, 0, 0, &new, 0); + yasm_object_get_general(object, ".debug$S", 0, 1, 0, 0, &new, 0); info.num_lineinfos = 0; STAILQ_INIT(&info.cv8_lineinfos); info.cv8_cur_li = NULL; @@ -582,23 +582,23 @@ yasm_cv__generate_symline(yasm_object *object, yasm_linemap *linemap, cv_append_str(info.debug_symline, ""); off = 1; for (i=0; ifilenames_size; i++) { - if (!dbgfmt_cv->filenames[i].pathname) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("codeview file number %d unassigned"), i+1); - yasm_errwarn_propagate(errwarns, 0); - continue; - } - bc = cv_append_str(info.debug_symline, - dbgfmt_cv->filenames[i].pathname); - dbgfmt_cv->filenames[i].str_off = off; - off += bc->len; + if (!dbgfmt_cv->filenames[i].pathname) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("codeview file number %d unassigned"), i+1); + yasm_errwarn_propagate(errwarns, 0); + continue; + } + bc = cv_append_str(info.debug_symline, + dbgfmt_cv->filenames[i].pathname); + dbgfmt_cv->filenames[i].str_off = off; + off += bc->len; } cv8_set_symhead_end(head, yasm_section_bcs_last(info.debug_symline)); /* Align 4 */ bc = yasm_bc_create_align - (yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), 0), - NULL, NULL, NULL, 0); + (yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), 0), + NULL, NULL, NULL, 0); yasm_bc_finalize(bc, yasm_cv__append_bc(info.debug_symline, bc)); yasm_bc_calc_len(bc, NULL, NULL); @@ -606,11 +606,11 @@ yasm_cv__generate_symline(yasm_object *object, yasm_linemap *linemap, head = cv8_add_symhead(info.debug_symline, CV8_FILE_INFO, 0); off = 0; for (i=0; ifilenames_size; i++) { - if (!dbgfmt_cv->filenames[i].pathname) - continue; - bc = cv8_add_fileinfo(info.debug_symline, &dbgfmt_cv->filenames[i]); - dbgfmt_cv->filenames[i].info_off = off; - off += bc->len; + if (!dbgfmt_cv->filenames[i].pathname) + continue; + bc = cv8_add_fileinfo(info.debug_symline, &dbgfmt_cv->filenames[i]); + dbgfmt_cv->filenames[i].info_off = off; + off += bc->len; } cv8_set_symhead_end(head, yasm_section_bcs_last(info.debug_symline)); @@ -618,15 +618,15 @@ yasm_cv__generate_symline(yasm_object *object, yasm_linemap *linemap, /* Generate line numbers for sections */ yasm_object_sections_traverse(object, (void *)&info, - cv_generate_line_section); + cv_generate_line_section); /* Output line numbers for sections */ STAILQ_FOREACH(li, &info.cv8_lineinfos, link) { - head = cv8_add_symhead(info.debug_symline, CV8_LINE_NUMS, 0); - bc = yasm_bc_create_common(&cv8_lineinfo_bc_callback, li, 0); - bc->len = 24+li->num_linenums*8; - yasm_cv__append_bc(info.debug_symline, bc); - cv8_set_symhead_end(head, yasm_section_bcs_last(info.debug_symline)); + head = cv8_add_symhead(info.debug_symline, CV8_LINE_NUMS, 0); + bc = yasm_bc_create_common(&cv8_lineinfo_bc_callback, li, 0); + bc->len = 24+li->num_linenums*8; + yasm_cv__append_bc(info.debug_symline, bc); + cv8_set_symhead_end(head, yasm_section_bcs_last(info.debug_symline)); } /* Already aligned 4 */ @@ -635,18 +635,18 @@ yasm_cv__generate_symline(yasm_object *object, yasm_linemap *linemap, head = cv8_add_symhead(info.debug_symline, CV8_DEBUG_SYMS, 0); /* add object and compile flag first */ cv8_add_sym_objname(info.debug_symline, - yasm__abspath(object->obj_filename)); + yasm__abspath(object->obj_filename)); cv8_add_sym_compile(object, info.debug_symline, - yasm__xstrdup(PACKAGE_NAME " " PACKAGE_INTVER "." - PACKAGE_BUILD)); + yasm__xstrdup(PACKAGE_NAME " " PACKAGE_INTVER "." + PACKAGE_BUILD)); /* then iterate through symbol table */ yasm_symtab_traverse(object->symtab, &info, cv_generate_sym); cv8_set_symhead_end(head, yasm_section_bcs_last(info.debug_symline)); /* Align 4 at end */ bc = yasm_bc_create_align - (yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), 0), - NULL, NULL, NULL, 0); + (yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), 0), + NULL, NULL, NULL, 0); yasm_bc_finalize(bc, yasm_cv__append_bc(info.debug_symline, bc)); yasm_bc_calc_len(bc, NULL, NULL); @@ -655,7 +655,7 @@ yasm_cv__generate_symline(yasm_object *object, yasm_linemap *linemap, static void cv_out_sym(yasm_symrec *sym, unsigned long off, yasm_bytecode *bc, - unsigned char **bufp, void *d, yasm_output_value_func output_value) + unsigned char **bufp, void *d, yasm_output_value_func output_value) { yasm_value val; @@ -685,9 +685,9 @@ cv8_add_symhead(yasm_section *sect, unsigned long type, int first) bc = yasm_bc_create_common(&cv8_symhead_bc_callback, head, 0); if (first) - bc->len = 12; + bc->len = 12; else - bc->len = 8; + bc->len = 8; head->end_prevbc = bc; yasm_cv__append_bc(sect, bc); @@ -714,7 +714,7 @@ cv8_symhead_bc_print(const void *contents, FILE *f, int indent_level) static int cv8_symhead_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a codeview symhead bytecode")); /*@notreached@*/ @@ -723,8 +723,8 @@ cv8_symhead_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int cv8_symhead_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { yasm_object *object = yasm_section_get_object(bc->section); cv8_symhead *head = (cv8_symhead *)bc->contents; @@ -734,8 +734,8 @@ cv8_symhead_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, cval = yasm_intnum_create_uint(4); /* Output "version" if first */ if (head->first) { - yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 0); - buf += 4; + yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 0); + buf += 4; } /* Type contained - 4 bytes */ @@ -787,7 +787,7 @@ cv8_fileinfo_bc_print(const void *contents, FILE *f, int indent_level) static int cv8_fileinfo_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a codeview fileinfo bytecode")); /*@notreached@*/ @@ -796,8 +796,8 @@ cv8_fileinfo_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int cv8_fileinfo_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { yasm_object *object = yasm_section_get_object(bc->section); cv8_fileinfo *fi = (cv8_fileinfo *)bc->contents; @@ -817,7 +817,7 @@ cv8_fileinfo_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Checksum */ for (i=0; i<16; i++) - YASM_WRITE_8(buf, fi->fn->digest[i]); + YASM_WRITE_8(buf, fi->fn->digest[i]); /* Pad */ YASM_WRITE_8(buf, 0); @@ -838,9 +838,9 @@ cv8_lineinfo_bc_destroy(void *contents) /* delete line sets */ ls1 = STAILQ_FIRST(&li->linesets); while (ls1) { - ls2 = STAILQ_NEXT(ls1, link); - yasm_xfree(ls1); - ls1 = ls2; + ls2 = STAILQ_NEXT(ls1, link); + yasm_xfree(ls1); + ls1 = ls2; } yasm_xfree(contents); @@ -854,7 +854,7 @@ cv8_lineinfo_bc_print(const void *contents, FILE *f, int indent_level) static int cv8_lineinfo_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a codeview linehead bytecode")); /*@notreached@*/ @@ -863,8 +863,8 @@ cv8_lineinfo_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int cv8_lineinfo_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { yasm_object *object = yasm_section_get_object(bc->section); cv8_lineinfo *li = (cv8_lineinfo *)bc->contents; @@ -882,7 +882,7 @@ cv8_lineinfo_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Section length covered by line number info */ cval = yasm_calc_bc_dist(yasm_section_bcs_first(li->sect), - yasm_section_bcs_last(li->sect)); + yasm_section_bcs_last(li->sect)); yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 0); buf += 4; @@ -904,18 +904,18 @@ cv8_lineinfo_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Offset / line number pairs */ i = 0; STAILQ_FOREACH(ls, &li->linesets, link) { - unsigned long j; - for (j=0; inum_linenums && j<126; i++, j++) { - /* offset in section */ - yasm_intnum_set_uint(cval, ls->pairs[j].offset); - yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 0); - buf += 4; - - /* line number in file */ - yasm_intnum_set_uint(cval, ls->pairs[j].line); - yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 0); - buf += 4; - } + unsigned long j; + for (j=0; inum_linenums && j<126; i++, j++) { + /* offset in section */ + yasm_intnum_set_uint(cval, ls->pairs[j].offset); + yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 0); + buf += 4; + + /* line number in file */ + yasm_intnum_set_uint(cval, ls->pairs[j].line); + yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 0); + buf += 4; + } } *bufp = buf; @@ -933,40 +933,40 @@ cv_sym_size(const cv_sym *cvs) int arg = 0; while (*ch) { - switch (*ch) { - case 'b': - len++; - arg++; - break; - case 'h': - len += 2; - arg++; - break; - case 'w': - len += 4; - arg++; - break; - case 'Y': - len += 6; /* XXX: will be 4 in 16-bit version */ - arg++; - break; - case 'T': - len += 4; /* XXX: will be 2 in CV4 */ - arg++; - break; - case 'S': - len += 1; /* XXX: is this 1 or 2? */ - slen = (unsigned long)strlen((const char *)cvs->args[arg++].p); - len += slen <= 0xff ? slen : 0xff; - break; - case 'Z': - len += - (unsigned long)strlen((const char *)cvs->args[arg++].p) + 1; - break; - default: - yasm_internal_error(N_("unknown sym format character")); - } - ch++; + switch (*ch) { + case 'b': + len++; + arg++; + break; + case 'h': + len += 2; + arg++; + break; + case 'w': + len += 4; + arg++; + break; + case 'Y': + len += 6; /* XXX: will be 4 in 16-bit version */ + arg++; + break; + case 'T': + len += 4; /* XXX: will be 2 in CV4 */ + arg++; + break; + case 'S': + len += 1; /* XXX: is this 1 or 2? */ + slen = (unsigned long)strlen((const char *)cvs->args[arg++].p); + len += slen <= 0xff ? slen : 0xff; + break; + case 'Z': + len += + (unsigned long)strlen((const char *)cvs->args[arg++].p) + 1; + break; + default: + yasm_internal_error(N_("unknown sym format character")); + } + ch++; } return len; @@ -980,22 +980,22 @@ cv_sym_bc_destroy(void *contents) int arg = 0; while (*ch) { - switch (*ch) { - case 'b': - case 'h': - case 'w': - case 'Y': - case 'T': - arg++; - break; /* nothing to destroy */ - case 'S': - case 'Z': - yasm_xfree(cvs->args[arg++].p); - break; - default: - yasm_internal_error(N_("unknown sym format character")); - } - ch++; + switch (*ch) { + case 'b': + case 'h': + case 'w': + case 'Y': + case 'T': + arg++; + break; /* nothing to destroy */ + case 'S': + case 'Z': + yasm_xfree(cvs->args[arg++].p); + break; + default: + yasm_internal_error(N_("unknown sym format character")); + } + ch++; } yasm_xfree(contents); @@ -1009,7 +1009,7 @@ cv_sym_bc_print(const void *contents, FILE *f, int indent_level) static int cv_sym_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a codeview sym bytecode")); /*@notreached@*/ @@ -1018,8 +1018,8 @@ cv_sym_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int cv_sym_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { yasm_object *object = yasm_section_get_object(bc->section); cv_sym *cvs = (cv_sym *)bc->contents; @@ -1040,52 +1040,52 @@ cv_sym_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, buf += 2; while (*ch) { - switch (*ch) { - case 'b': - YASM_WRITE_8(buf, cvs->args[arg].i); - arg++; - break; - case 'h': - yasm_intnum_set_uint(cval, cvs->args[arg++].i); - yasm_arch_intnum_tobytes(object->arch, cval, buf, 2, 16, 0, - bc, 0); - buf += 2; - break; - case 'w': - yasm_intnum_set_uint(cval, cvs->args[arg++].i); - yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, - bc, 0); - buf += 4; - break; - case 'Y': - cv_out_sym((yasm_symrec *)cvs->args[arg++].p, - (unsigned long)(buf-(*bufp)), bc, &buf, d, - output_value); - break; - case 'T': - yasm_intnum_set_uint(cval, cvs->args[arg++].i); - yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, - bc, 0); - buf += 4; /* XXX: will be 2 in CV4 */ - break; - case 'S': - len = strlen((char *)cvs->args[arg].p); - len = len <= 0xff ? len : 0xff; - YASM_WRITE_8(buf, len); - memcpy(buf, (char *)cvs->args[arg].p, len); - buf += len; - arg++; - break; - case 'Z': - len = strlen((char *)cvs->args[arg].p)+1; - memcpy(buf, (char *)cvs->args[arg].p, len); - buf += len; - arg++; - break; - default: - yasm_internal_error(N_("unknown leaf format character")); - } - ch++; + switch (*ch) { + case 'b': + YASM_WRITE_8(buf, cvs->args[arg].i); + arg++; + break; + case 'h': + yasm_intnum_set_uint(cval, cvs->args[arg++].i); + yasm_arch_intnum_tobytes(object->arch, cval, buf, 2, 16, 0, + bc, 0); + buf += 2; + break; + case 'w': + yasm_intnum_set_uint(cval, cvs->args[arg++].i); + yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, + bc, 0); + buf += 4; + break; + case 'Y': + cv_out_sym((yasm_symrec *)cvs->args[arg++].p, + (unsigned long)(buf-(*bufp)), bc, &buf, d, + output_value); + break; + case 'T': + yasm_intnum_set_uint(cval, cvs->args[arg++].i); + yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, + bc, 0); + buf += 4; /* XXX: will be 2 in CV4 */ + break; + case 'S': + len = strlen((char *)cvs->args[arg].p); + len = len <= 0xff ? len : 0xff; + YASM_WRITE_8(buf, len); + memcpy(buf, (char *)cvs->args[arg].p, len); + buf += len; + arg++; + break; + case 'Z': + len = strlen((char *)cvs->args[arg].p)+1; + memcpy(buf, (char *)cvs->args[arg].p, len); + buf += len; + arg++; + break; + default: + yasm_internal_error(N_("unknown leaf format character")); + } + ch++; } *bufp = buf; diff --git a/modules/dbgfmts/codeview/cv-type.c b/modules/dbgfmts/codeview/cv-type.c index ecec7f9b..17f53926 100644 --- a/modules/dbgfmts/codeview/cv-type.c +++ b/modules/dbgfmts/codeview/cv-type.c @@ -38,421 +38,421 @@ enum cv_reservedtype { /* Bitfields representation - type */ - CV_TYPE_SPECIAL = 0x00<<4, /* Special */ - CV_TYPE_SIGNED = 0x01<<4, /* Signed integral value */ - CV_TYPE_UNSIGNED = 0x02<<4, /* Unsigned integral value */ - CV_TYPE_BOOLEAN = 0x03<<4, /* Boolean */ - CV_TYPE_REAL = 0x04<<4, /* Real */ - CV_TYPE_COMPLEX = 0x05<<4, /* Complex */ - CV_TYPE_SPECIAL2 = 0x06<<4, /* Special2 */ - CV_TYPE_REALINT = 0x07<<4, /* Really int value */ + CV_TYPE_SPECIAL = 0x00<<4, /* Special */ + CV_TYPE_SIGNED = 0x01<<4, /* Signed integral value */ + CV_TYPE_UNSIGNED = 0x02<<4, /* Unsigned integral value */ + CV_TYPE_BOOLEAN = 0x03<<4, /* Boolean */ + CV_TYPE_REAL = 0x04<<4, /* Real */ + CV_TYPE_COMPLEX = 0x05<<4, /* Complex */ + CV_TYPE_SPECIAL2 = 0x06<<4, /* Special2 */ + CV_TYPE_REALINT = 0x07<<4, /* Really int value */ /* "size" of CV_TYPE_SPECIAL */ - CV_SPECIAL_NOTYPE = 0x00<<0, /* No type */ - CV_SPECIAL_ABS = 0x01<<0, /* Absolute symbol */ - CV_SPECIAL_SEG = 0x02<<0, /* Segment */ - CV_SPECIAL_VOID = 0x03<<0, /* Void */ - CV_SPECIAL_CURRENCY = 0x04<<0, /* Basic 8-byte currency value */ - CV_SPECIAL_NEARBSTR = 0x05<<0, /* Near Basic string */ - CV_SPECIAL_FARBSTR = 0x06<<0, /* Far Basic string */ + CV_SPECIAL_NOTYPE = 0x00<<0, /* No type */ + CV_SPECIAL_ABS = 0x01<<0, /* Absolute symbol */ + CV_SPECIAL_SEG = 0x02<<0, /* Segment */ + CV_SPECIAL_VOID = 0x03<<0, /* Void */ + CV_SPECIAL_CURRENCY = 0x04<<0, /* Basic 8-byte currency value */ + CV_SPECIAL_NEARBSTR = 0x05<<0, /* Near Basic string */ + CV_SPECIAL_FARBSTR = 0x06<<0, /* Far Basic string */ /* Size of CV_TYPE_SIGNED, CV_TYPE_UNSIGNED, and CV_TYPE_BOOLEAN */ - CV_INTEGER_1BYTE = 0x00<<0, /* 1 byte */ - CV_INTEGER_2BYTE = 0x01<<0, /* 2 byte */ - CV_INTEGER_4BYTE = 0x02<<0, /* 4 byte */ - CV_INTEGER_8BYTE = 0x03<<0, /* 8 byte */ + CV_INTEGER_1BYTE = 0x00<<0, /* 1 byte */ + CV_INTEGER_2BYTE = 0x01<<0, /* 2 byte */ + CV_INTEGER_4BYTE = 0x02<<0, /* 4 byte */ + CV_INTEGER_8BYTE = 0x03<<0, /* 8 byte */ /* Size of CV_TYPE_REAL and CV_TYPE_COMPLEX */ - CV_REAL_32BIT = 0x00<<0, /* 32 bit */ - CV_REAL_64BIT = 0x01<<0, /* 64 bit */ - CV_REAL_80BIT = 0x02<<0, /* 80 bit */ - CV_REAL_128BIT = 0x03<<0, /* 128 bit */ - CV_REAL_48BIT = 0x04<<0, /* 48 bit */ + CV_REAL_32BIT = 0x00<<0, /* 32 bit */ + CV_REAL_64BIT = 0x01<<0, /* 64 bit */ + CV_REAL_80BIT = 0x02<<0, /* 80 bit */ + CV_REAL_128BIT = 0x03<<0, /* 128 bit */ + CV_REAL_48BIT = 0x04<<0, /* 48 bit */ /* "size" of CV_TYPE_SPECIAL2 */ - CV_SPECIAL2_BIT = 0x00<<0, /* Bit */ - CV_SPECIAL2_PASCHAR = 0x01<<0, /* Pascal CHAR */ + CV_SPECIAL2_BIT = 0x00<<0, /* Bit */ + CV_SPECIAL2_PASCHAR = 0x01<<0, /* Pascal CHAR */ /* Size of CV_TYPE_REALINT */ - CV_REALINT_CHAR = 0x00<<0, /* Char */ - CV_REALINT_WCHAR = 0x01<<0, /* Wide character */ - CV_REALINT_S2BYTE = 0x02<<0, /* 2-byte signed integer */ - CV_REALINT_U2BYTE = 0x03<<0, /* 2-byte unsigned integer */ - CV_REALINT_S4BYTE = 0x04<<0, /* 4-byte signed integer */ - CV_REALINT_U4BYTE = 0x05<<0, /* 4-byte unsigned integer */ - CV_REALINT_S8BYTE = 0x06<<0, /* 8-byte signed integer */ - CV_REALINT_U8BYTE = 0x07<<0, /* 8-byte unsigned integer */ + CV_REALINT_CHAR = 0x00<<0, /* Char */ + CV_REALINT_WCHAR = 0x01<<0, /* Wide character */ + CV_REALINT_S2BYTE = 0x02<<0, /* 2-byte signed integer */ + CV_REALINT_U2BYTE = 0x03<<0, /* 2-byte unsigned integer */ + CV_REALINT_S4BYTE = 0x04<<0, /* 4-byte signed integer */ + CV_REALINT_U4BYTE = 0x05<<0, /* 4-byte unsigned integer */ + CV_REALINT_S8BYTE = 0x06<<0, /* 8-byte signed integer */ + CV_REALINT_U8BYTE = 0x07<<0, /* 8-byte unsigned integer */ /* Mode */ - CV_MODE_DIRECT = 0x00<<8, /* Direct; not a pointer */ - CV_MODE_NEAR = 0x01<<8, /* Near pointer */ - CV_MODE_FAR = 0x02<<8, /* Far pointer */ - CV_MODE_HUGE = 0x03<<8, /* Huge pointer */ - CV_MODE_NEAR32 = 0x04<<8, /* 32-bit near pointer */ - CV_MODE_FAR32 = 0x05<<8, /* 32-bit far pointer */ - CV_MODE_NEAR64 = 0x06<<8, /* 64-bit near pointer */ + CV_MODE_DIRECT = 0x00<<8, /* Direct; not a pointer */ + CV_MODE_NEAR = 0x01<<8, /* Near pointer */ + CV_MODE_FAR = 0x02<<8, /* Far pointer */ + CV_MODE_HUGE = 0x03<<8, /* Huge pointer */ + CV_MODE_NEAR32 = 0x04<<8, /* 32-bit near pointer */ + CV_MODE_FAR32 = 0x05<<8, /* 32-bit far pointer */ + CV_MODE_NEAR64 = 0x06<<8, /* 64-bit near pointer */ /* Pure primitive type listing - based on above bitfields */ /* Special Types */ - CV_T_NOTYPE = 0x0000, /* Uncharacterized type (no type) */ - CV_T_ABS = 0x0001, /* Absolute symbol */ - CV_T_SEGMENT = 0x0002, /* Segment type */ - CV_T_VOID = 0x0003, /* Void */ - CV_T_PVOID = 0x0103, /* Near pointer to void */ - CV_T_PFVOID = 0x0203, /* Far pointer to void */ - CV_T_PHVOID = 0x0303, /* Huge pointer to void */ - CV_T_32PVOID = 0x0403, /* 32-bit near pointer to void */ - CV_T_32PFVOID = 0x0503, /* 32-bit far pointer to void */ - CV_T_CURRENCY = 0x0004, /* Basic 8-byte currency value */ - CV_T_NBASICSTR = 0x0005, /* Near Basic string */ - CV_T_FBASICSTR = 0x0006, /* Far Basic string */ - CV_T_BIT = 0x0060, /* Bit */ - CV_T_PASCHAR = 0x0061, /* Pascal CHAR */ + CV_T_NOTYPE = 0x0000, /* Uncharacterized type (no type) */ + CV_T_ABS = 0x0001, /* Absolute symbol */ + CV_T_SEGMENT = 0x0002, /* Segment type */ + CV_T_VOID = 0x0003, /* Void */ + CV_T_PVOID = 0x0103, /* Near pointer to void */ + CV_T_PFVOID = 0x0203, /* Far pointer to void */ + CV_T_PHVOID = 0x0303, /* Huge pointer to void */ + CV_T_32PVOID = 0x0403, /* 32-bit near pointer to void */ + CV_T_32PFVOID = 0x0503, /* 32-bit far pointer to void */ + CV_T_CURRENCY = 0x0004, /* Basic 8-byte currency value */ + CV_T_NBASICSTR = 0x0005, /* Near Basic string */ + CV_T_FBASICSTR = 0x0006, /* Far Basic string */ + CV_T_BIT = 0x0060, /* Bit */ + CV_T_PASCHAR = 0x0061, /* Pascal CHAR */ /* Character Types */ - CV_T_CHAR = 0x0010, /* 8-bit signed */ - CV_T_UCHAR = 0x0020, /* 8-bit unsigned */ - CV_T_PCHAR = 0x0110, /* Near pointer to 8-bit signed */ - CV_T_PUCHAR = 0x0120, /* Near pointer to 8-bit unsigned */ - CV_T_PFCHAR = 0x0210, /* Far pointer to 8-bit signed */ - CV_T_PFUCHAR = 0x0220, /* Far pointer to 8-bit unsigned */ - CV_T_PHCHAR = 0x0310, /* Huge pointer to 8-bit signed */ - CV_T_PHUCHAR = 0x0320, /* Huge pointer to 8-bit unsigned */ - CV_T_32PCHAR = 0x0410, /* 16:32 near pointer to 8-bit signed */ - CV_T_32PUCHAR = 0x0420, /* 16:32 near pointer to 8-bit unsigned */ - CV_T_32PFCHAR = 0x0510, /* 16:32 far pointer to 8-bit signed */ - CV_T_32PFUCHAR = 0x0520, /* 16:32 far pointer to 8-bit unsigned */ + CV_T_CHAR = 0x0010, /* 8-bit signed */ + CV_T_UCHAR = 0x0020, /* 8-bit unsigned */ + CV_T_PCHAR = 0x0110, /* Near pointer to 8-bit signed */ + CV_T_PUCHAR = 0x0120, /* Near pointer to 8-bit unsigned */ + CV_T_PFCHAR = 0x0210, /* Far pointer to 8-bit signed */ + CV_T_PFUCHAR = 0x0220, /* Far pointer to 8-bit unsigned */ + CV_T_PHCHAR = 0x0310, /* Huge pointer to 8-bit signed */ + CV_T_PHUCHAR = 0x0320, /* Huge pointer to 8-bit unsigned */ + CV_T_32PCHAR = 0x0410, /* 16:32 near pointer to 8-bit signed */ + CV_T_32PUCHAR = 0x0420, /* 16:32 near pointer to 8-bit unsigned */ + CV_T_32PFCHAR = 0x0510, /* 16:32 far pointer to 8-bit signed */ + CV_T_32PFUCHAR = 0x0520, /* 16:32 far pointer to 8-bit unsigned */ /* Real Character Types */ - CV_T_RCHAR = 0x0070, /* Real char */ - CV_T_PRCHAR = 0x0170, /* Near pointer to a real char */ - CV_T_PFRCHAR = 0x0270, /* Far pointer to a real char */ - CV_T_PHRCHAR = 0x0370, /* Huge pointer to a real char */ - CV_T_32PRCHAR = 0x0470, /* 16:32 near pointer to a real char */ - CV_T_32PFRCHAR = 0x0570, /* 16:32 far pointer to a real char */ + CV_T_RCHAR = 0x0070, /* Real char */ + CV_T_PRCHAR = 0x0170, /* Near pointer to a real char */ + CV_T_PFRCHAR = 0x0270, /* Far pointer to a real char */ + CV_T_PHRCHAR = 0x0370, /* Huge pointer to a real char */ + CV_T_32PRCHAR = 0x0470, /* 16:32 near pointer to a real char */ + CV_T_32PFRCHAR = 0x0570, /* 16:32 far pointer to a real char */ /* Wide Character Types */ - CV_T_WCHAR = 0x0071, /* Wide char */ - CV_T_PWCHAR = 0x0171, /* Near pointer to a wide char */ - CV_T_PFWCHAR = 0x0271, /* Far pointer to a wide char */ - CV_T_PHWCHAR = 0x0371, /* Huge pointer to a wide char */ - CV_T_32PWCHAR = 0x0471, /* 16:32 near pointer to a wide char */ - CV_T_32PFWCHAR = 0x0571, /* 16:32 far pointer to a wide char */ + CV_T_WCHAR = 0x0071, /* Wide char */ + CV_T_PWCHAR = 0x0171, /* Near pointer to a wide char */ + CV_T_PFWCHAR = 0x0271, /* Far pointer to a wide char */ + CV_T_PHWCHAR = 0x0371, /* Huge pointer to a wide char */ + CV_T_32PWCHAR = 0x0471, /* 16:32 near pointer to a wide char */ + CV_T_32PFWCHAR = 0x0571, /* 16:32 far pointer to a wide char */ /* Real 16-bit Integer Types */ - CV_T_INT2 = 0x0072, /* Real 16-bit signed int */ - CV_T_UINT2 = 0x0073, /* Real 16-bit unsigned int */ - CV_T_PINT2 = 0x0172, /* Near pointer to 16-bit signed int */ - CV_T_PUINT2 = 0x0173, /* Near pointer to 16-bit unsigned int */ - CV_T_PFINT2 = 0x0272, /* Far pointer to 16-bit signed int */ - CV_T_PFUINT2 = 0x0273, /* Far pointer to 16-bit unsigned int */ - CV_T_PHINT2 = 0x0372, /* Huge pointer to 16-bit signed int */ - CV_T_PHUINT2 = 0x0373, /* Huge pointer to 16-bit unsigned int */ - CV_T_32PINT2 = 0x0472, /* 16:32 near pointer to 16-bit signed int */ - CV_T_32PUINT2 = 0x0473, /* 16:32 near pointer to 16-bit unsigned int */ - CV_T_32PFINT2 = 0x0572, /* 16:32 far pointer to 16-bit signed int */ - CV_T_32PFUINT2 = 0x0573, /* 16:32 far pointer to 16-bit unsigned int */ + CV_T_INT2 = 0x0072, /* Real 16-bit signed int */ + CV_T_UINT2 = 0x0073, /* Real 16-bit unsigned int */ + CV_T_PINT2 = 0x0172, /* Near pointer to 16-bit signed int */ + CV_T_PUINT2 = 0x0173, /* Near pointer to 16-bit unsigned int */ + CV_T_PFINT2 = 0x0272, /* Far pointer to 16-bit signed int */ + CV_T_PFUINT2 = 0x0273, /* Far pointer to 16-bit unsigned int */ + CV_T_PHINT2 = 0x0372, /* Huge pointer to 16-bit signed int */ + CV_T_PHUINT2 = 0x0373, /* Huge pointer to 16-bit unsigned int */ + CV_T_32PINT2 = 0x0472, /* 16:32 near pointer to 16-bit signed int */ + CV_T_32PUINT2 = 0x0473, /* 16:32 near pointer to 16-bit unsigned int */ + CV_T_32PFINT2 = 0x0572, /* 16:32 far pointer to 16-bit signed int */ + CV_T_32PFUINT2 = 0x0573, /* 16:32 far pointer to 16-bit unsigned int */ /* 16-bit Short Types */ - CV_T_SHORT = 0x0011, /* 16-bit signed */ - CV_T_USHORT = 0x0021, /* 16-bit unsigned */ - CV_T_PSHORT = 0x0111, /* Near pointer to 16-bit signed */ - CV_T_PUSHORT = 0x0121, /* Near pointer to 16-bit unsigned */ - CV_T_PFSHORT = 0x0211, /* Far pointer to 16-bit signed */ - CV_T_PFUSHORT = 0x0221, /* Far pointer to 16-bit unsigned */ - CV_T_PHSHORT = 0x0311, /* Huge pointer to 16-bit signed */ - CV_T_PHUSHORT = 0x0321, /* Huge pointer to 16-bit unsigned */ - CV_T_32PSHORT = 0x0411, /* 16:32 near pointer to 16-bit signed */ - CV_T_32PUSHORT = 0x0421, /* 16:32 near pointer to 16-bit unsigned */ - CV_T_32PFSHORT = 0x0511, /* 16:32 far pointer to 16-bit signed */ - CV_T_32PFUSHORT = 0x0521, /* 16:32 far pointer to 16-bit unsigned */ + CV_T_SHORT = 0x0011, /* 16-bit signed */ + CV_T_USHORT = 0x0021, /* 16-bit unsigned */ + CV_T_PSHORT = 0x0111, /* Near pointer to 16-bit signed */ + CV_T_PUSHORT = 0x0121, /* Near pointer to 16-bit unsigned */ + CV_T_PFSHORT = 0x0211, /* Far pointer to 16-bit signed */ + CV_T_PFUSHORT = 0x0221, /* Far pointer to 16-bit unsigned */ + CV_T_PHSHORT = 0x0311, /* Huge pointer to 16-bit signed */ + CV_T_PHUSHORT = 0x0321, /* Huge pointer to 16-bit unsigned */ + CV_T_32PSHORT = 0x0411, /* 16:32 near pointer to 16-bit signed */ + CV_T_32PUSHORT = 0x0421, /* 16:32 near pointer to 16-bit unsigned */ + CV_T_32PFSHORT = 0x0511, /* 16:32 far pointer to 16-bit signed */ + CV_T_32PFUSHORT = 0x0521, /* 16:32 far pointer to 16-bit unsigned */ /* Real 32-bit Integer Types */ - CV_T_INT4 = 0x0074, /* Real 32-bit signed int */ - CV_T_UINT4 = 0x0075, /* Real 32-bit unsigned int */ - CV_T_PINT4 = 0x0174, /* Near pointer to 32-bit signed int */ - CV_T_PUINT4 = 0x0175, /* Near pointer to 32-bit unsigned int */ - CV_T_PFINT4 = 0x0274, /* Far pointer to 32-bit signed int */ - CV_T_PFUINT4 = 0x0275, /* Far pointer to 32-bit unsigned int */ - CV_T_PHINT4 = 0x0374, /* Huge pointer to 32-bit signed int */ - CV_T_PHUINT4 = 0x0375, /* Huge pointer to 32-bit unsigned int */ - CV_T_32PINT4 = 0x0474, /* 16:32 near pointer to 32-bit signed int */ - CV_T_32PUINT4 = 0x0475, /* 16:32 near pointer to 32-bit unsigned int */ - CV_T_32PFINT4 = 0x0574, /* 16:32 far pointer to 32-bit signed int */ - CV_T_32PFUINT4 = 0x0575, /* 16:32 far pointer to 32-bit unsigned int */ + CV_T_INT4 = 0x0074, /* Real 32-bit signed int */ + CV_T_UINT4 = 0x0075, /* Real 32-bit unsigned int */ + CV_T_PINT4 = 0x0174, /* Near pointer to 32-bit signed int */ + CV_T_PUINT4 = 0x0175, /* Near pointer to 32-bit unsigned int */ + CV_T_PFINT4 = 0x0274, /* Far pointer to 32-bit signed int */ + CV_T_PFUINT4 = 0x0275, /* Far pointer to 32-bit unsigned int */ + CV_T_PHINT4 = 0x0374, /* Huge pointer to 32-bit signed int */ + CV_T_PHUINT4 = 0x0375, /* Huge pointer to 32-bit unsigned int */ + CV_T_32PINT4 = 0x0474, /* 16:32 near pointer to 32-bit signed int */ + CV_T_32PUINT4 = 0x0475, /* 16:32 near pointer to 32-bit unsigned int */ + CV_T_32PFINT4 = 0x0574, /* 16:32 far pointer to 32-bit signed int */ + CV_T_32PFUINT4 = 0x0575, /* 16:32 far pointer to 32-bit unsigned int */ /* 32-bit Long Types */ - CV_T_LONG = 0x0012, /* 32-bit signed */ - CV_T_ULONG = 0x0022, /* 32-bit unsigned */ - CV_T_PLONG = 0x0112, /* Near pointer to 32-bit signed */ - CV_T_PULONG = 0x0122, /* Near pointer to 32-bit unsigned */ - CV_T_PFLONG = 0x0212, /* Far pointer to 32-bit signed */ - CV_T_PFULONG = 0x0222, /* Far pointer to 32-bit unsigned */ - CV_T_PHLONG = 0x0312, /* Huge pointer to 32-bit signed */ - CV_T_PHULONG = 0x0322, /* Huge pointer to 32-bit unsigned */ - CV_T_32PLONG = 0x0412, /* 16:32 near pointer to 32-bit signed */ - CV_T_32PULONG = 0x0422, /* 16:32 near pointer to 32-bit unsigned */ - CV_T_32PFLONG = 0x0512, /* 16:32 far pointer to 32-bit signed */ - CV_T_32PFULONG = 0x0522, /* 16:32 far pointer to 32-bit unsigned */ + CV_T_LONG = 0x0012, /* 32-bit signed */ + CV_T_ULONG = 0x0022, /* 32-bit unsigned */ + CV_T_PLONG = 0x0112, /* Near pointer to 32-bit signed */ + CV_T_PULONG = 0x0122, /* Near pointer to 32-bit unsigned */ + CV_T_PFLONG = 0x0212, /* Far pointer to 32-bit signed */ + CV_T_PFULONG = 0x0222, /* Far pointer to 32-bit unsigned */ + CV_T_PHLONG = 0x0312, /* Huge pointer to 32-bit signed */ + CV_T_PHULONG = 0x0322, /* Huge pointer to 32-bit unsigned */ + CV_T_32PLONG = 0x0412, /* 16:32 near pointer to 32-bit signed */ + CV_T_32PULONG = 0x0422, /* 16:32 near pointer to 32-bit unsigned */ + CV_T_32PFLONG = 0x0512, /* 16:32 far pointer to 32-bit signed */ + CV_T_32PFULONG = 0x0522, /* 16:32 far pointer to 32-bit unsigned */ /* Real 64-bit int Types */ - CV_T_INT8 = 0x0076, /* 64-bit signed int */ - CV_T_UINT8 = 0x0077, /* 64-bit unsigned int */ - CV_T_PINT8 = 0x0176, /* Near pointer to 64-bit signed int */ - CV_T_PUINT8 = 0x0177, /* Near pointer to 64-bit unsigned int */ - CV_T_PFINT8 = 0x0276, /* Far pointer to 64-bit signed int */ - CV_T_PFUINT8 = 0x0277, /* Far pointer to 64-bit unsigned int */ - CV_T_PHINT8 = 0x0376, /* Huge pointer to 64-bit signed int */ - CV_T_PHUINT8 = 0x0377, /* Huge pointer to 64-bit unsigned int */ - CV_T_32PINT8 = 0x0476, /* 16:32 near pointer to 64-bit signed int */ - CV_T_32PUINT8 = 0x0477, /* 16:32 near pointer to 64-bit unsigned int */ - CV_T_32PFINT8 = 0x0576, /* 16:32 far pointer to 64-bit signed int */ - CV_T_32PFUINT8 = 0x0577, /* 16:32 far pointer to 64-bit unsigned int */ + CV_T_INT8 = 0x0076, /* 64-bit signed int */ + CV_T_UINT8 = 0x0077, /* 64-bit unsigned int */ + CV_T_PINT8 = 0x0176, /* Near pointer to 64-bit signed int */ + CV_T_PUINT8 = 0x0177, /* Near pointer to 64-bit unsigned int */ + CV_T_PFINT8 = 0x0276, /* Far pointer to 64-bit signed int */ + CV_T_PFUINT8 = 0x0277, /* Far pointer to 64-bit unsigned int */ + CV_T_PHINT8 = 0x0376, /* Huge pointer to 64-bit signed int */ + CV_T_PHUINT8 = 0x0377, /* Huge pointer to 64-bit unsigned int */ + CV_T_32PINT8 = 0x0476, /* 16:32 near pointer to 64-bit signed int */ + CV_T_32PUINT8 = 0x0477, /* 16:32 near pointer to 64-bit unsigned int */ + CV_T_32PFINT8 = 0x0576, /* 16:32 far pointer to 64-bit signed int */ + CV_T_32PFUINT8 = 0x0577, /* 16:32 far pointer to 64-bit unsigned int */ /* 64-bit Integral Types */ - CV_T_QUAD = 0x0013, /* 64-bit signed */ - CV_T_UQUAD = 0x0023, /* 64-bit unsigned */ - CV_T_PQUAD = 0x0113, /* Near pointer to 64-bit signed */ - CV_T_PUQUAD = 0x0123, /* Near pointer to 64-bit unsigned */ - CV_T_PFQUAD = 0x0213, /* Far pointer to 64-bit signed */ - CV_T_PFUQUAD = 0x0223, /* Far pointer to 64-bit unsigned */ - CV_T_PHQUAD = 0x0313, /* Huge pointer to 64-bit signed */ - CV_T_PHUQUAD = 0x0323, /* Huge pointer to 64-bit unsigned */ - CV_T_32PQUAD = 0x0413, /* 16:32 near pointer to 64-bit signed */ - CV_T_32PUQUAD = 0x0423, /* 16:32 near pointer to 64-bit unsigned */ - CV_T_32PFQUAD = 0x0513, /* 16:32 far pointer to 64-bit signed */ - CV_T_32PFUQUAD = 0x0523, /* 16:32 far pointer to 64-bit unsigned */ + CV_T_QUAD = 0x0013, /* 64-bit signed */ + CV_T_UQUAD = 0x0023, /* 64-bit unsigned */ + CV_T_PQUAD = 0x0113, /* Near pointer to 64-bit signed */ + CV_T_PUQUAD = 0x0123, /* Near pointer to 64-bit unsigned */ + CV_T_PFQUAD = 0x0213, /* Far pointer to 64-bit signed */ + CV_T_PFUQUAD = 0x0223, /* Far pointer to 64-bit unsigned */ + CV_T_PHQUAD = 0x0313, /* Huge pointer to 64-bit signed */ + CV_T_PHUQUAD = 0x0323, /* Huge pointer to 64-bit unsigned */ + CV_T_32PQUAD = 0x0413, /* 16:32 near pointer to 64-bit signed */ + CV_T_32PUQUAD = 0x0423, /* 16:32 near pointer to 64-bit unsigned */ + CV_T_32PFQUAD = 0x0513, /* 16:32 far pointer to 64-bit signed */ + CV_T_32PFUQUAD = 0x0523, /* 16:32 far pointer to 64-bit unsigned */ /* 32-bit Real Types */ - CV_T_REAL32 = 0x0040, /* 32-bit real */ - CV_T_PREAL32 = 0x0140, /* Near pointer to 32-bit real */ - CV_T_PFREAL32 = 0x0240, /* Far pointer to 32-bit real */ - CV_T_PHREAL32 = 0x0340, /* Huge pointer to 32-bit real */ - CV_T_32PREAL32 = 0x0440, /* 16:32 near pointer to 32-bit real */ - CV_T_32PFREAL32 = 0x0540, /* 16:32 far pointer to 32-bit real */ + CV_T_REAL32 = 0x0040, /* 32-bit real */ + CV_T_PREAL32 = 0x0140, /* Near pointer to 32-bit real */ + CV_T_PFREAL32 = 0x0240, /* Far pointer to 32-bit real */ + CV_T_PHREAL32 = 0x0340, /* Huge pointer to 32-bit real */ + CV_T_32PREAL32 = 0x0440, /* 16:32 near pointer to 32-bit real */ + CV_T_32PFREAL32 = 0x0540, /* 16:32 far pointer to 32-bit real */ /* 48-bit Real Types */ - CV_T_REAL48 = 0x0044, /* 48-bit real */ - CV_T_PREAL48 = 0x0144, /* Near pointer to 48-bit real */ - CV_T_PFREAL48 = 0x0244, /* Far pointer to 48-bit real */ - CV_T_PHREAL48 = 0x0344, /* Huge pointer to 48-bit real */ - CV_T_32PREAL48 = 0x0444, /* 16:32 near pointer to 48-bit real */ - CV_T_32PFREAL48 = 0x0544, /* 16:32 far pointer to 48-bit real */ + CV_T_REAL48 = 0x0044, /* 48-bit real */ + CV_T_PREAL48 = 0x0144, /* Near pointer to 48-bit real */ + CV_T_PFREAL48 = 0x0244, /* Far pointer to 48-bit real */ + CV_T_PHREAL48 = 0x0344, /* Huge pointer to 48-bit real */ + CV_T_32PREAL48 = 0x0444, /* 16:32 near pointer to 48-bit real */ + CV_T_32PFREAL48 = 0x0544, /* 16:32 far pointer to 48-bit real */ /* 64-bit Real Types */ - CV_T_REAL64 = 0x0041, /* 64-bit real */ - CV_T_PREAL64 = 0x0141, /* Near pointer to 64-bit real */ - CV_T_PFREAL64 = 0x0241, /* Far pointer to 64-bit real */ - CV_T_PHREAL64 = 0x0341, /* Huge pointer to 64-bit real */ - CV_T_32PREAL64 = 0x0441, /* 16:32 near pointer to 64-bit real */ - CV_T_32PFREAL64 = 0x0541, /* 16:32 far pointer to 64-bit real */ + CV_T_REAL64 = 0x0041, /* 64-bit real */ + CV_T_PREAL64 = 0x0141, /* Near pointer to 64-bit real */ + CV_T_PFREAL64 = 0x0241, /* Far pointer to 64-bit real */ + CV_T_PHREAL64 = 0x0341, /* Huge pointer to 64-bit real */ + CV_T_32PREAL64 = 0x0441, /* 16:32 near pointer to 64-bit real */ + CV_T_32PFREAL64 = 0x0541, /* 16:32 far pointer to 64-bit real */ /* 80-bit Real Types */ - CV_T_REAL80 = 0x0042, /* 80-bit real */ - CV_T_PREAL80 = 0x0142, /* Near pointer to 80-bit real */ - CV_T_PFREAL80 = 0x0242, /* Far pointer to 80-bit real */ - CV_T_PHREAL80 = 0x0342, /* Huge pointer to 80-bit real */ - CV_T_32PREAL80 = 0x0442, /* 16:32 near pointer to 80-bit real */ - CV_T_32PFREAL80 = 0x0542, /* 16:32 far pointer to 80-bit real */ + CV_T_REAL80 = 0x0042, /* 80-bit real */ + CV_T_PREAL80 = 0x0142, /* Near pointer to 80-bit real */ + CV_T_PFREAL80 = 0x0242, /* Far pointer to 80-bit real */ + CV_T_PHREAL80 = 0x0342, /* Huge pointer to 80-bit real */ + CV_T_32PREAL80 = 0x0442, /* 16:32 near pointer to 80-bit real */ + CV_T_32PFREAL80 = 0x0542, /* 16:32 far pointer to 80-bit real */ /* 128-bit Real Types */ - CV_T_REAL128 = 0x0043, /* 128-bit real */ - CV_T_PREAL128 = 0x0143, /* Near pointer to 128-bit real */ - CV_T_PFREAL128 = 0x0243, /* Far pointer to 128-bit real */ - CV_T_PHREAL128 = 0x0343, /* Huge pointer to 128-bit real */ - CV_T_32PREAL128 = 0x0443, /* 16:32 near pointer to 128-bit real */ - CV_T_32PFREAL128 = 0x0543, /* 16:32 far pointer to 128-bit real */ + CV_T_REAL128 = 0x0043, /* 128-bit real */ + CV_T_PREAL128 = 0x0143, /* Near pointer to 128-bit real */ + CV_T_PFREAL128 = 0x0243, /* Far pointer to 128-bit real */ + CV_T_PHREAL128 = 0x0343, /* Huge pointer to 128-bit real */ + CV_T_32PREAL128 = 0x0443, /* 16:32 near pointer to 128-bit real */ + CV_T_32PFREAL128 = 0x0543, /* 16:32 far pointer to 128-bit real */ /* 32-bit Complex Types */ - CV_T_CPLX32 = 0x0050, /* 32-bit complex */ - CV_T_PCPLX32 = 0x0150, /* Near pointer to 32-bit complex */ - CV_T_PFCPLX32 = 0x0250, /* Far pointer to 32-bit complex */ - CV_T_PHCPLX32 = 0x0350, /* Huge pointer to 32-bit complex */ - CV_T_32PCPLX32 = 0x0450, /* 16:32 near pointer to 32-bit complex */ - CV_T_32PFCPLX32 = 0x0550, /* 16:32 far pointer to 32-bit complex */ + CV_T_CPLX32 = 0x0050, /* 32-bit complex */ + CV_T_PCPLX32 = 0x0150, /* Near pointer to 32-bit complex */ + CV_T_PFCPLX32 = 0x0250, /* Far pointer to 32-bit complex */ + CV_T_PHCPLX32 = 0x0350, /* Huge pointer to 32-bit complex */ + CV_T_32PCPLX32 = 0x0450, /* 16:32 near pointer to 32-bit complex */ + CV_T_32PFCPLX32 = 0x0550, /* 16:32 far pointer to 32-bit complex */ /* 64-bit Complex Types */ - CV_T_CPLX64 = 0x0051, /* 64-bit complex */ - CV_T_PCPLX64 = 0x0151, /* Near pointer to 64-bit complex */ - CV_T_PFCPLX64 = 0x0251, /* Far pointer to 64-bit complex */ - CV_T_PHCPLX64 = 0x0351, /* Huge pointer to 64-bit complex */ - CV_T_32PCPLX64 = 0x0451, /* 16:32 near pointer to 64-bit complex */ - CV_T_32PFCPLX64 = 0x0551, /* 16:32 far pointer to 64-bit complex */ + CV_T_CPLX64 = 0x0051, /* 64-bit complex */ + CV_T_PCPLX64 = 0x0151, /* Near pointer to 64-bit complex */ + CV_T_PFCPLX64 = 0x0251, /* Far pointer to 64-bit complex */ + CV_T_PHCPLX64 = 0x0351, /* Huge pointer to 64-bit complex */ + CV_T_32PCPLX64 = 0x0451, /* 16:32 near pointer to 64-bit complex */ + CV_T_32PFCPLX64 = 0x0551, /* 16:32 far pointer to 64-bit complex */ /* 80-bit Complex Types */ - CV_T_CPLX80 = 0x0052, /* 80-bit complex */ - CV_T_PCPLX80 = 0x0152, /* Near pointer to 80-bit complex */ - CV_T_PFCPLX80 = 0x0252, /* Far pointer to 80-bit complex */ - CV_T_PHCPLX80 = 0x0352, /* Huge pointer to 80-bit complex */ - CV_T_32PCPLX80 = 0x0452, /* 16:32 near pointer to 80-bit complex */ - CV_T_32PFCPLX80 = 0x0552, /* 16:32 far pointer to 80-bit complex */ + CV_T_CPLX80 = 0x0052, /* 80-bit complex */ + CV_T_PCPLX80 = 0x0152, /* Near pointer to 80-bit complex */ + CV_T_PFCPLX80 = 0x0252, /* Far pointer to 80-bit complex */ + CV_T_PHCPLX80 = 0x0352, /* Huge pointer to 80-bit complex */ + CV_T_32PCPLX80 = 0x0452, /* 16:32 near pointer to 80-bit complex */ + CV_T_32PFCPLX80 = 0x0552, /* 16:32 far pointer to 80-bit complex */ /* 128-bit Complex Types */ - CV_T_CPLX128 = 0x0053, /* 128-bit complex */ - CV_T_PCPLX128 = 0x0153, /* Near pointer to 128-bit complex */ - CV_T_PFCPLX128 = 0x0253, /* Far pointer to 128-bit complex */ - CV_T_PHCPLX128 = 0x0353, /* Huge pointer to 128-bit real */ - CV_T_32PCPLX128 = 0x0453, /* 16:32 near pointer to 128-bit complex */ - CV_T_32PFCPLX128 = 0x0553, /* 16:32 far pointer to 128-bit complex */ + CV_T_CPLX128 = 0x0053, /* 128-bit complex */ + CV_T_PCPLX128 = 0x0153, /* Near pointer to 128-bit complex */ + CV_T_PFCPLX128 = 0x0253, /* Far pointer to 128-bit complex */ + CV_T_PHCPLX128 = 0x0353, /* Huge pointer to 128-bit real */ + CV_T_32PCPLX128 = 0x0453, /* 16:32 near pointer to 128-bit complex */ + CV_T_32PFCPLX128 = 0x0553, /* 16:32 far pointer to 128-bit complex */ /* Boolean Types */ - CV_T_BOOL08 = 0x0030, /* 8-bit Boolean */ - CV_T_BOOL16 = 0x0031, /* 16-bit Boolean */ - CV_T_BOOL32 = 0x0032, /* 32-bit Boolean */ - CV_T_BOOL64 = 0x0033, /* 64-bit Boolean */ - CV_T_PBOOL08 = 0x0130, /* Near pointer to 8-bit Boolean */ - CV_T_PBOOL16 = 0x0131, /* Near pointer to 16-bit Boolean */ - CV_T_PBOOL32 = 0x0132, /* Near pointer to 32-bit Boolean */ - CV_T_PBOOL64 = 0x0133, /* Near pointer to 64-bit Boolean */ - CV_T_PFBOOL08 = 0x0230, /* Far pointer to 8-bit Boolean */ - CV_T_PFBOOL16 = 0x0231, /* Far pointer to 16-bit Boolean */ - CV_T_PFBOOL32 = 0x0232, /* Far pointer to 32-bit Boolean */ - CV_T_PFBOOL64 = 0x0233, /* Far pointer to 64-bit Boolean */ - CV_T_PHBOOL08 = 0x0330, /* Huge pointer to 8-bit Boolean */ - CV_T_PHBOOL16 = 0x0331, /* Huge pointer to 16-bit Boolean */ - CV_T_PHBOOL32 = 0x0332, /* Huge pointer to 32-bit Boolean */ - CV_T_PHBOOL64 = 0x0333, /* Huge pointer to 64-bit Boolean */ - CV_T_32PBOOL08 = 0x0430, /* 16:32 near pointer to 8-bit Boolean */ - CV_T_32PBOOL16 = 0x0431, /* 16:32 near pointer to 16-bit Boolean */ - CV_T_32PBOOL32 = 0x0432, /* 16:32 near pointer to 32-bit Boolean */ - CV_T_32PBOOL64 = 0x0433, /* 16:32 near pointer to 64-bit Boolean */ - CV_T_32PFBOOL08 = 0x0530, /* 16:32 far pointer to 8-bit Boolean */ - CV_T_32PFBOOL16 = 0x0531, /* 16:32 far pointer to 16-bit Boolean */ - CV_T_32PFBOOL32 = 0x0532, /* 16:32 far pointer to 32-bit Boolean */ - CV_T_32PFBOOL64 = 0x0533, /* 16:32 far pointer to 64-bit Boolean */ + CV_T_BOOL08 = 0x0030, /* 8-bit Boolean */ + CV_T_BOOL16 = 0x0031, /* 16-bit Boolean */ + CV_T_BOOL32 = 0x0032, /* 32-bit Boolean */ + CV_T_BOOL64 = 0x0033, /* 64-bit Boolean */ + CV_T_PBOOL08 = 0x0130, /* Near pointer to 8-bit Boolean */ + CV_T_PBOOL16 = 0x0131, /* Near pointer to 16-bit Boolean */ + CV_T_PBOOL32 = 0x0132, /* Near pointer to 32-bit Boolean */ + CV_T_PBOOL64 = 0x0133, /* Near pointer to 64-bit Boolean */ + CV_T_PFBOOL08 = 0x0230, /* Far pointer to 8-bit Boolean */ + CV_T_PFBOOL16 = 0x0231, /* Far pointer to 16-bit Boolean */ + CV_T_PFBOOL32 = 0x0232, /* Far pointer to 32-bit Boolean */ + CV_T_PFBOOL64 = 0x0233, /* Far pointer to 64-bit Boolean */ + CV_T_PHBOOL08 = 0x0330, /* Huge pointer to 8-bit Boolean */ + CV_T_PHBOOL16 = 0x0331, /* Huge pointer to 16-bit Boolean */ + CV_T_PHBOOL32 = 0x0332, /* Huge pointer to 32-bit Boolean */ + CV_T_PHBOOL64 = 0x0333, /* Huge pointer to 64-bit Boolean */ + CV_T_32PBOOL08 = 0x0430, /* 16:32 near pointer to 8-bit Boolean */ + CV_T_32PBOOL16 = 0x0431, /* 16:32 near pointer to 16-bit Boolean */ + CV_T_32PBOOL32 = 0x0432, /* 16:32 near pointer to 32-bit Boolean */ + CV_T_32PBOOL64 = 0x0433, /* 16:32 near pointer to 64-bit Boolean */ + CV_T_32PFBOOL08 = 0x0530, /* 16:32 far pointer to 8-bit Boolean */ + CV_T_32PFBOOL16 = 0x0531, /* 16:32 far pointer to 16-bit Boolean */ + CV_T_32PFBOOL32 = 0x0532, /* 16:32 far pointer to 32-bit Boolean */ + CV_T_32PFBOOL64 = 0x0533, /* 16:32 far pointer to 64-bit Boolean */ /* Non-primitive types are stored in the TYPES section (generated in * cv-type.c) and start at this index (e.g. 0x1000 is the first type * in TYPES, 0x1001 the second, etc. */ - CV_FIRST_NONPRIM = 0x1000 + CV_FIRST_NONPRIM = 0x1000 }; enum cv_leaftype { /* Leaf indices for type records that can be referenced from symbols */ - CV4_LF_MODIFIER = 0x0001, /* Type Modifier */ - CV4_LF_POINTER = 0x0002, /* Pointer */ - CV4_LF_ARRAY = 0x0003, /* Simple Array */ - CV4_LF_CLASS = 0x0004, /* Classes */ - CV4_LF_STRUCTURE = 0x0005, /* Structures */ - CV4_LF_UNION = 0x0006, /* Unions */ - CV4_LF_ENUM = 0x0007, /* Enumeration */ - CV4_LF_PROCEDURE = 0x0008, /* Procedure */ - CV4_LF_MFUNCTION = 0x0009, /* Member Function */ - CV4_LF_VTSHAPE = 0x000a, /* Virtual Function Table Shape */ - CV4_LF_BARRAY = 0x000d, /* Basic Array */ - CV4_LF_LABEL = 0x000e, /* Label */ - CV4_LF_NULL = 0x000f, /* Null */ - CV4_LF_DIMARRAY = 0x0011, /* Multiply Dimensioned Array */ - CV4_LF_VFTPATH = 0x0012, /* Path to Virtual Function Table */ - CV4_LF_PRECOMP = 0x0013, /* Reference Precompiled Types */ - CV4_LF_ENDPRECOMP = 0x0014, /* End of Precompiled Types */ + CV4_LF_MODIFIER = 0x0001, /* Type Modifier */ + CV4_LF_POINTER = 0x0002, /* Pointer */ + CV4_LF_ARRAY = 0x0003, /* Simple Array */ + CV4_LF_CLASS = 0x0004, /* Classes */ + CV4_LF_STRUCTURE = 0x0005, /* Structures */ + CV4_LF_UNION = 0x0006, /* Unions */ + CV4_LF_ENUM = 0x0007, /* Enumeration */ + CV4_LF_PROCEDURE = 0x0008, /* Procedure */ + CV4_LF_MFUNCTION = 0x0009, /* Member Function */ + CV4_LF_VTSHAPE = 0x000a, /* Virtual Function Table Shape */ + CV4_LF_BARRAY = 0x000d, /* Basic Array */ + CV4_LF_LABEL = 0x000e, /* Label */ + CV4_LF_NULL = 0x000f, /* Null */ + CV4_LF_DIMARRAY = 0x0011, /* Multiply Dimensioned Array */ + CV4_LF_VFTPATH = 0x0012, /* Path to Virtual Function Table */ + CV4_LF_PRECOMP = 0x0013, /* Reference Precompiled Types */ + CV4_LF_ENDPRECOMP = 0x0014, /* End of Precompiled Types */ /* CodeView 5.0 version */ - CV5_LF_MODIFIER = 0x1001, /* Type Modifier */ - CV5_LF_POINTER = 0x1002, /* Pointer */ - CV5_LF_ARRAY = 0x1003, /* Simple Array */ - CV5_LF_CLASS = 0x1004, /* Classes */ - CV5_LF_STRUCTURE = 0x1005, /* Structures */ - CV5_LF_UNION = 0x1006, /* Unions */ - CV5_LF_ENUM = 0x1007, /* Enumeration */ - CV5_LF_PROCEDURE = 0x1008, /* Procedure */ - CV5_LF_MFUNCTION = 0x1009, /* Member Function */ - CV5_LF_VTSHAPE = 0x000a, /* Virtual Function Table Shape */ - CV5_LF_BARRAY = 0x100d, /* Basic Array */ - CV5_LF_LABEL = 0x000e, /* Label */ - CV5_LF_NULL = 0x000f, /* Null */ - CV5_LF_DIMARRAY = 0x100c, /* Multiply Dimensioned Array */ - CV5_LF_VFTPATH = 0x100d, /* Path to Virtual Function Table */ - CV5_LF_PRECOMP = 0x100e, /* Reference Precompiled Types */ - CV5_LF_ENDPRECOMP = 0x0014, /* End of Precompiled Types */ - CV5_LF_TYPESERVER = 0x0016, /* Reference Typeserver */ + CV5_LF_MODIFIER = 0x1001, /* Type Modifier */ + CV5_LF_POINTER = 0x1002, /* Pointer */ + CV5_LF_ARRAY = 0x1003, /* Simple Array */ + CV5_LF_CLASS = 0x1004, /* Classes */ + CV5_LF_STRUCTURE = 0x1005, /* Structures */ + CV5_LF_UNION = 0x1006, /* Unions */ + CV5_LF_ENUM = 0x1007, /* Enumeration */ + CV5_LF_PROCEDURE = 0x1008, /* Procedure */ + CV5_LF_MFUNCTION = 0x1009, /* Member Function */ + CV5_LF_VTSHAPE = 0x000a, /* Virtual Function Table Shape */ + CV5_LF_BARRAY = 0x100d, /* Basic Array */ + CV5_LF_LABEL = 0x000e, /* Label */ + CV5_LF_NULL = 0x000f, /* Null */ + CV5_LF_DIMARRAY = 0x100c, /* Multiply Dimensioned Array */ + CV5_LF_VFTPATH = 0x100d, /* Path to Virtual Function Table */ + CV5_LF_PRECOMP = 0x100e, /* Reference Precompiled Types */ + CV5_LF_ENDPRECOMP = 0x0014, /* End of Precompiled Types */ + CV5_LF_TYPESERVER = 0x0016, /* Reference Typeserver */ /* Leaf indices for type records that can be referenced from other type * records */ - CV4_LF_SKIP = 0x0200, /* Skip */ - CV4_LF_ARGLIST = 0x0201, /* Argument List */ - CV4_LF_DEFARG = 0x0202, /* Default Argument */ - CV4_LF_LIST = 0x0203, /* Arbitrary List */ - CV4_LF_FIELDLIST = 0x0204, /* Field List */ - CV4_LF_DERIVED = 0x0205, /* Derived Classes */ - CV4_LF_BITFIELD = 0x0206, /* Bit Fields */ - CV4_LF_METHODLIST = 0x0207, /* Method List */ - CV4_LF_DIMCONU = 0x0208, /* Dimensioned Array with Constant Upper Bound */ - CV4_LF_DIMCONLU = 0x0209, /* Dimensioned Array with Constant Lower and Upper Bounds */ - CV4_LF_DIMVARU = 0x020a, /* Dimensioned Array with Variable Upper Bound */ - CV4_LF_DIMVARLU = 0x020b, /* Dimensioned Array with Variable Lower and Upper Bounds */ - CV4_LF_REFSYM = 0x020c, /* Referenced Symbol */ + CV4_LF_SKIP = 0x0200, /* Skip */ + CV4_LF_ARGLIST = 0x0201, /* Argument List */ + CV4_LF_DEFARG = 0x0202, /* Default Argument */ + CV4_LF_LIST = 0x0203, /* Arbitrary List */ + CV4_LF_FIELDLIST = 0x0204, /* Field List */ + CV4_LF_DERIVED = 0x0205, /* Derived Classes */ + CV4_LF_BITFIELD = 0x0206, /* Bit Fields */ + CV4_LF_METHODLIST = 0x0207, /* Method List */ + CV4_LF_DIMCONU = 0x0208, /* Dimensioned Array with Constant Upper Bound */ + CV4_LF_DIMCONLU = 0x0209, /* Dimensioned Array with Constant Lower and Upper Bounds */ + CV4_LF_DIMVARU = 0x020a, /* Dimensioned Array with Variable Upper Bound */ + CV4_LF_DIMVARLU = 0x020b, /* Dimensioned Array with Variable Lower and Upper Bounds */ + CV4_LF_REFSYM = 0x020c, /* Referenced Symbol */ /* CodeView 5.0 version */ - CV5_LF_SKIP = 0x1200, /* Skip */ - CV5_LF_ARGLIST = 0x1201, /* Argument List */ - CV5_LF_DEFARG = 0x1202, /* Default Argument */ - CV5_LF_FIELDLIST = 0x1203, /* Field List */ - CV5_LF_DERIVED = 0x1204, /* Derived Classes */ - CV5_LF_BITFIELD = 0x1205, /* Bit Fields */ - CV5_LF_METHODLIST = 0x1206, /* Method List */ - CV5_LF_DIMCONU = 0x1207, /* Dimensioned Array with Constant Upper Bound */ - CV5_LF_DIMCONLU = 0x1208, /* Dimensioned Array with Constant Lower and Upper Bounds */ - CV5_LF_DIMVARU = 0x1209, /* Dimensioned Array with Variable Upper Bound */ - CV5_LF_DIMVARLU = 0x120a, /* Dimensioned Array with Variable Lower and Upper Bounds */ - CV5_LF_REFSYM = 0x020c, /* Referenced Symbol */ + CV5_LF_SKIP = 0x1200, /* Skip */ + CV5_LF_ARGLIST = 0x1201, /* Argument List */ + CV5_LF_DEFARG = 0x1202, /* Default Argument */ + CV5_LF_FIELDLIST = 0x1203, /* Field List */ + CV5_LF_DERIVED = 0x1204, /* Derived Classes */ + CV5_LF_BITFIELD = 0x1205, /* Bit Fields */ + CV5_LF_METHODLIST = 0x1206, /* Method List */ + CV5_LF_DIMCONU = 0x1207, /* Dimensioned Array with Constant Upper Bound */ + CV5_LF_DIMCONLU = 0x1208, /* Dimensioned Array with Constant Lower and Upper Bounds */ + CV5_LF_DIMVARU = 0x1209, /* Dimensioned Array with Variable Upper Bound */ + CV5_LF_DIMVARLU = 0x120a, /* Dimensioned Array with Variable Lower and Upper Bounds */ + CV5_LF_REFSYM = 0x020c, /* Referenced Symbol */ /* Leaf indices for fields of complex lists */ - CV4_LF_BCLASS = 0x0400, /* Real Base Class */ - CV4_LF_VBCLASS = 0x0401, /* Direct Virtual Base Class */ - CV4_LF_IVBCLASS = 0x0402, /* Indirect Virtual Base Class */ - CV4_LF_ENUMERATE = 0x0403, /* Enumeration Name and Value */ - CV4_LF_FRIENDFCN = 0x0404, /* Friend Function */ - CV4_LF_INDEX = 0x0405, /* Index To Another Type Record */ - CV4_LF_MEMBER = 0x0406, /* Data Member */ - CV4_LF_STMEMBER = 0x0407, /* Static Data Member */ - CV4_LF_METHOD = 0x0408, /* Method */ - CV4_LF_NESTTYPE = 0x0409, /* Nested Type Definition */ - CV4_LF_VFUNCTAB = 0x040a, /* Virtual Function Table Pointer */ - CV4_LF_FRIENDCLS = 0x040b, /* Friend Class */ - CV4_LF_ONEMETHOD = 0x040c, /* One Method */ - CV4_LF_VFUNCOFF = 0x040d, /* Virtual Function Offset */ + CV4_LF_BCLASS = 0x0400, /* Real Base Class */ + CV4_LF_VBCLASS = 0x0401, /* Direct Virtual Base Class */ + CV4_LF_IVBCLASS = 0x0402, /* Indirect Virtual Base Class */ + CV4_LF_ENUMERATE = 0x0403, /* Enumeration Name and Value */ + CV4_LF_FRIENDFCN = 0x0404, /* Friend Function */ + CV4_LF_INDEX = 0x0405, /* Index To Another Type Record */ + CV4_LF_MEMBER = 0x0406, /* Data Member */ + CV4_LF_STMEMBER = 0x0407, /* Static Data Member */ + CV4_LF_METHOD = 0x0408, /* Method */ + CV4_LF_NESTTYPE = 0x0409, /* Nested Type Definition */ + CV4_LF_VFUNCTAB = 0x040a, /* Virtual Function Table Pointer */ + CV4_LF_FRIENDCLS = 0x040b, /* Friend Class */ + CV4_LF_ONEMETHOD = 0x040c, /* One Method */ + CV4_LF_VFUNCOFF = 0x040d, /* Virtual Function Offset */ /* CodeView 5.0 version */ - CV5_LF_BCLASS = 0x1400, /* Real Base Class */ - CV5_LF_VBCLASS = 0x1401, /* Direct Virtual Base Class */ - CV5_LF_IVBCLASS = 0x1402, /* Indirect Virtual Base Class */ - CV5_LF_ENUMERATE = 0x0403, /* Enumeration Name and Value */ - CV5_LF_FRIENDFCN = 0x1403, /* Friend Function */ - CV5_LF_INDEX = 0x1404, /* Index To Another Type Record */ - CV5_LF_MEMBER = 0x1405, /* Data Member */ - CV5_LF_STMEMBER = 0x1406, /* Static Data Member */ - CV5_LF_METHOD = 0x1407, /* Method */ - CV5_LF_NESTTYPE = 0x1408, /* Nested Type Definition */ - CV5_LF_VFUNCTAB = 0x1409, /* Virtual Function Table Pointer */ - CV5_LF_FRIENDCLS = 0x140a, /* Friend Class */ - CV5_LF_ONEMETHOD = 0x140b, /* One Method */ - CV5_LF_VFUNCOFF = 0x140c, /* Virtual Function Offset */ - CV5_LF_NESTTYPEEX = 0x140d, /* Nested Type Extended Definition */ - CV5_LF_MEMBERMODIFY = 0x140e, /* Member Modification */ + CV5_LF_BCLASS = 0x1400, /* Real Base Class */ + CV5_LF_VBCLASS = 0x1401, /* Direct Virtual Base Class */ + CV5_LF_IVBCLASS = 0x1402, /* Indirect Virtual Base Class */ + CV5_LF_ENUMERATE = 0x0403, /* Enumeration Name and Value */ + CV5_LF_FRIENDFCN = 0x1403, /* Friend Function */ + CV5_LF_INDEX = 0x1404, /* Index To Another Type Record */ + CV5_LF_MEMBER = 0x1405, /* Data Member */ + CV5_LF_STMEMBER = 0x1406, /* Static Data Member */ + CV5_LF_METHOD = 0x1407, /* Method */ + CV5_LF_NESTTYPE = 0x1408, /* Nested Type Definition */ + CV5_LF_VFUNCTAB = 0x1409, /* Virtual Function Table Pointer */ + CV5_LF_FRIENDCLS = 0x140a, /* Friend Class */ + CV5_LF_ONEMETHOD = 0x140b, /* One Method */ + CV5_LF_VFUNCOFF = 0x140c, /* Virtual Function Offset */ + CV5_LF_NESTTYPEEX = 0x140d, /* Nested Type Extended Definition */ + CV5_LF_MEMBERMODIFY = 0x140e, /* Member Modification */ /* XXX: CodeView 5.0 spec also lists 0x040f as LF_MEMBERMODIFY? */ /* Leaf indices for numeric fields of symbols and type records */ - CV_LF_NUMERIC = 0x8000, - CV_LF_CHAR = 0x8000, /* Signed Char (8-bit) */ - CV_LF_SHORT = 0x8001, /* Signed Short (16-bit) */ - CV_LF_USHORT = 0x8002, /* Unsigned Short (16-bit) */ - CV_LF_LONG = 0x8003, /* Signed Long (32-bit) */ - CV_LF_ULONG = 0x8004, /* Unsigned Long (32-bit) */ - CV_LF_REAL32 = 0x8005, /* 32-bit Float */ - CV_LF_REAL64 = 0x8006, /* 64-bit Float */ - CV_LF_REAL80 = 0x8007, /* 80-bit Float */ - CV_LF_REAL128 = 0x8008, /* 128-bit Float */ - CV_LF_QUADWORD = 0x8009, /* Signed Quad Word (64-bit) */ - CV_LF_UQUADWORD = 0x800a, /* Unsigned Quad Word (64-bit) */ - CV_LF_REAL48 = 0x800b, /* 48-bit Float */ - CV_LF_COMPLEX32 = 0x800c, /* 32-bit Complex */ - CV_LF_COMPLEX64 = 0x800d, /* 64-bit Complex */ - CV_LF_COMPLEX80 = 0x800e, /* 80-bit Complex */ - CV_LF_COMPLEX128 = 0x800f, /* 128-bit Complex */ - CV_LF_VARSTRING = 0x8010, /* Variable-length String */ + CV_LF_NUMERIC = 0x8000, + CV_LF_CHAR = 0x8000, /* Signed Char (8-bit) */ + CV_LF_SHORT = 0x8001, /* Signed Short (16-bit) */ + CV_LF_USHORT = 0x8002, /* Unsigned Short (16-bit) */ + CV_LF_LONG = 0x8003, /* Signed Long (32-bit) */ + CV_LF_ULONG = 0x8004, /* Unsigned Long (32-bit) */ + CV_LF_REAL32 = 0x8005, /* 32-bit Float */ + CV_LF_REAL64 = 0x8006, /* 64-bit Float */ + CV_LF_REAL80 = 0x8007, /* 80-bit Float */ + CV_LF_REAL128 = 0x8008, /* 128-bit Float */ + CV_LF_QUADWORD = 0x8009, /* Signed Quad Word (64-bit) */ + CV_LF_UQUADWORD = 0x800a, /* Unsigned Quad Word (64-bit) */ + CV_LF_REAL48 = 0x800b, /* 48-bit Float */ + CV_LF_COMPLEX32 = 0x800c, /* 32-bit Complex */ + CV_LF_COMPLEX64 = 0x800d, /* 64-bit Complex */ + CV_LF_COMPLEX80 = 0x800e, /* 80-bit Complex */ + CV_LF_COMPLEX128 = 0x800f, /* 128-bit Complex */ + CV_LF_VARSTRING = 0x8010, /* Variable-length String */ /* Leaf padding bytes */ - CV_LF_PAD0 = 0xf0, - CV_LF_PAD1 = 0xf1, - CV_LF_PAD2 = 0xf2, - CV_LF_PAD3 = 0xf3, - CV_LF_PAD4 = 0xf4, - CV_LF_PAD5 = 0xf5, - CV_LF_PAD6 = 0xf6, - CV_LF_PAD7 = 0xf7, - CV_LF_PAD8 = 0xf8, - CV_LF_PAD9 = 0xf9, - CV_LF_PAD10 = 0xfa, - CV_LF_PAD11 = 0xfb, - CV_LF_PAD12 = 0xfc, - CV_LF_PAD13 = 0xfc, - CV_LF_PAD14 = 0xfe, - CV_LF_PAD15 = 0xff + CV_LF_PAD0 = 0xf0, + CV_LF_PAD1 = 0xf1, + CV_LF_PAD2 = 0xf2, + CV_LF_PAD3 = 0xf3, + CV_LF_PAD4 = 0xf4, + CV_LF_PAD5 = 0xf5, + CV_LF_PAD6 = 0xf6, + CV_LF_PAD7 = 0xf7, + CV_LF_PAD8 = 0xf8, + CV_LF_PAD9 = 0xf9, + CV_LF_PAD10 = 0xfa, + CV_LF_PAD11 = 0xfb, + CV_LF_PAD12 = 0xfc, + CV_LF_PAD13 = 0xfc, + CV_LF_PAD14 = 0xfe, + CV_LF_PAD15 = 0xff }; /* Leaves use a bit of meta-programming to encode formats: each character @@ -466,15 +466,15 @@ enum cv_leaftype { */ typedef struct cv_leaf { enum cv_leaftype type; - const char *format; /* format of args */ + const char *format; /* format of args */ union { - unsigned long i; - void *p; + unsigned long i; + void *p; } args[6]; } cv_leaf; typedef struct cv_type { - unsigned long indx; /* type # (must be same as output order) */ + unsigned long indx; /* type # (must be same as output order) */ size_t num_leaves; /*@null@*/ /*@only@*/ cv_leaf **leaves; } cv_type; @@ -524,7 +524,7 @@ yasm_cv__generate_type(yasm_object *object) cv_type *type; debug_type = - yasm_object_get_general(object, ".debug$T", 0, 1, 0, 0, &new, 0); + yasm_object_get_general(object, ".debug$T", 0, 1, 0, 0, &new, 0); /* Add label type */ type = cv_type_create(indx++); @@ -543,25 +543,25 @@ cv_leaf_destroy(cv_leaf *leaf) int arg = 0; while (*ch) { - switch (*ch) { - case 'b': - case 'h': - case 'w': - arg++; - break; /* nothing to destroy */ - case 'L': - cv_leaf_destroy((cv_leaf *)leaf->args[arg++].p); - break; - case 'T': - arg++; /* nothing to destroy */ - break; - case 'S': - yasm_xfree(leaf->args[arg++].p); - break; - default: - yasm_internal_error(N_("unknown leaf format character")); - } - ch++; + switch (*ch) { + case 'b': + case 'h': + case 'w': + arg++; + break; /* nothing to destroy */ + case 'L': + cv_leaf_destroy((cv_leaf *)leaf->args[arg++].p); + break; + case 'T': + arg++; /* nothing to destroy */ + break; + case 'S': + yasm_xfree(leaf->args[arg++].p); + break; + default: + yasm_internal_error(N_("unknown leaf format character")); + } + ch++; } } @@ -574,35 +574,35 @@ cv_leaf_size(const cv_leaf *leaf) int arg = 0; while (*ch) { - switch (*ch) { - case 'b': - len++; - arg++; - break; - case 'h': - len += 2; - arg++; - break; - case 'w': - len += 4; - arg++; - break; - case 'L': - len += cv_leaf_size((const cv_leaf *)leaf->args[arg++].p); - break; - case 'T': - len += 4; /* XXX: will be 2 in CV4 */ - arg++; - break; - case 'S': - len += 1; /* XXX: is this 1 or 2? */ - slen = (unsigned long)strlen((const char *)leaf->args[arg++].p); - len += slen <= 0xff ? slen : 0xff; - break; - default: - yasm_internal_error(N_("unknown leaf format character")); - } - ch++; + switch (*ch) { + case 'b': + len++; + arg++; + break; + case 'h': + len += 2; + arg++; + break; + case 'w': + len += 4; + arg++; + break; + case 'L': + len += cv_leaf_size((const cv_leaf *)leaf->args[arg++].p); + break; + case 'T': + len += 4; /* XXX: will be 2 in CV4 */ + arg++; + break; + case 'S': + len += 1; /* XXX: is this 1 or 2? */ + slen = (unsigned long)strlen((const char *)leaf->args[arg++].p); + len += slen <= 0xff ? slen : 0xff; + break; + default: + yasm_internal_error(N_("unknown leaf format character")); + } + ch++; } return len; @@ -610,7 +610,7 @@ cv_leaf_size(const cv_leaf *leaf) static void cv_leaf_tobytes(const cv_leaf *leaf, yasm_bytecode *bc, yasm_arch *arch, - unsigned char **bufp, yasm_intnum *cval) + unsigned char **bufp, yasm_intnum *cval) { unsigned char *buf = *bufp; const char *ch = leaf->format; @@ -623,43 +623,43 @@ cv_leaf_tobytes(const cv_leaf *leaf, yasm_bytecode *bc, yasm_arch *arch, buf += 2; while (*ch) { - switch (*ch) { - case 'b': - YASM_WRITE_8(buf, leaf->args[arg].i); - arg++; - break; - case 'h': - yasm_intnum_set_uint(cval, leaf->args[arg++].i); - yasm_arch_intnum_tobytes(arch, cval, buf, 2, 16, 0, bc, 0); - buf += 2; - break; - case 'w': - yasm_intnum_set_uint(cval, leaf->args[arg++].i); - yasm_arch_intnum_tobytes(arch, cval, buf, 4, 32, 0, bc, 0); - buf += 4; - break; - case 'L': - cv_leaf_tobytes((const cv_leaf *)leaf->args[arg++].p, bc, arch, - &buf, cval); - break; - case 'T': - yasm_intnum_set_uint(cval, - ((const cv_type *)leaf->args[arg++].p)->indx); - yasm_arch_intnum_tobytes(arch, cval, buf, 4, 32, 0, bc, 0); - buf += 4; /* XXX: will be 2 in CV4 */ - break; - case 'S': - len = strlen((const char *)leaf->args[arg].p); - len = len <= 0xff ? len : 0xff; - YASM_WRITE_8(buf, len); - memcpy(buf, (const char *)leaf->args[arg].p, len); - buf += len; - arg++; - break; - default: - yasm_internal_error(N_("unknown leaf format character")); - } - ch++; + switch (*ch) { + case 'b': + YASM_WRITE_8(buf, leaf->args[arg].i); + arg++; + break; + case 'h': + yasm_intnum_set_uint(cval, leaf->args[arg++].i); + yasm_arch_intnum_tobytes(arch, cval, buf, 2, 16, 0, bc, 0); + buf += 2; + break; + case 'w': + yasm_intnum_set_uint(cval, leaf->args[arg++].i); + yasm_arch_intnum_tobytes(arch, cval, buf, 4, 32, 0, bc, 0); + buf += 4; + break; + case 'L': + cv_leaf_tobytes((const cv_leaf *)leaf->args[arg++].p, bc, arch, + &buf, cval); + break; + case 'T': + yasm_intnum_set_uint(cval, + ((const cv_type *)leaf->args[arg++].p)->indx); + yasm_arch_intnum_tobytes(arch, cval, buf, 4, 32, 0, bc, 0); + buf += 4; /* XXX: will be 2 in CV4 */ + break; + case 'S': + len = strlen((const char *)leaf->args[arg].p); + len = len <= 0xff ? len : 0xff; + YASM_WRITE_8(buf, len); + memcpy(buf, (const char *)leaf->args[arg].p, len); + buf += len; + arg++; + break; + default: + yasm_internal_error(N_("unknown leaf format character")); + } + ch++; } *bufp = buf; @@ -686,7 +686,7 @@ cv_type_append_leaf(cv_type *type, /*@keep@*/ cv_leaf *leaf) * until we add structure support. */ type->leaves = yasm_xrealloc(type->leaves, - type->num_leaves*sizeof(cv_leaf *)); + type->num_leaves*sizeof(cv_leaf *)); type->leaves[type->num_leaves-1] = leaf; } @@ -698,9 +698,9 @@ cv_type_bc_destroy(void *contents) size_t i; for (i=0; inum_leaves; i++) - cv_leaf_destroy(type->leaves[i]); + cv_leaf_destroy(type->leaves[i]); if (type->leaves) - yasm_xfree(type->leaves); + yasm_xfree(type->leaves); yasm_xfree(contents); } @@ -712,30 +712,30 @@ cv_type_bc_print(const void *contents, FILE *f, int indent_level) static int cv_type_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { cv_type *type = (cv_type *)bc->contents; size_t i; if (type->indx == CV_FIRST_NONPRIM) - bc->len = 4+2; + bc->len = 4+2; else - bc->len = 2; + bc->len = 2; for (i=0; inum_leaves; i++) - bc->len += cv_leaf_size(type->leaves[i]); + bc->len += cv_leaf_size(type->leaves[i]); /* Pad to multiple of 4 */ if (bc->len & 0x3) - bc->len += 4-(bc->len & 0x3); + bc->len += 4-(bc->len & 0x3); return 0; } static int cv_type_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { yasm_object *object = yasm_section_get_object(bc->section); cv_type *type = (cv_type *)bc->contents; @@ -744,11 +744,11 @@ cv_type_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, size_t i; unsigned long reclen = bc->len - 2; - cval = yasm_intnum_create_uint(4); /* version */ + cval = yasm_intnum_create_uint(4); /* version */ if (type->indx == CV_FIRST_NONPRIM) { - yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 1); - buf += 4; - reclen -= 4; + yasm_arch_intnum_tobytes(object->arch, cval, buf, 4, 32, 0, bc, 1); + buf += 4; + reclen -= 4; } /* Total length of record (following this field) - 2 bytes */ @@ -758,18 +758,18 @@ cv_type_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Leaves */ for (i=0; inum_leaves; i++) - cv_leaf_tobytes(type->leaves[i], bc, object->arch, &buf, cval); + cv_leaf_tobytes(type->leaves[i], bc, object->arch, &buf, cval); /* Pad to multiple of 4 */ switch ((buf-(*bufp)) & 0x3) { - case 3: - YASM_WRITE_8(buf, CV_LF_PAD3); - case 2: - YASM_WRITE_8(buf, CV_LF_PAD2); - case 1: - YASM_WRITE_8(buf, CV_LF_PAD1); - case 0: - break; + case 3: + YASM_WRITE_8(buf, CV_LF_PAD3); + case 2: + YASM_WRITE_8(buf, CV_LF_PAD2); + case 1: + YASM_WRITE_8(buf, CV_LF_PAD1); + case 0: + break; } *bufp = buf; diff --git a/modules/dbgfmts/dwarf2/dwarf2-aranges.c b/modules/dbgfmts/dwarf2/dwarf2-aranges.c index e277cdaa..b144224f 100644 --- a/modules/dbgfmts/dwarf2/dwarf2-aranges.c +++ b/modules/dbgfmts/dwarf2/dwarf2-aranges.c @@ -35,7 +35,7 @@ static void dwarf2_append_arange(yasm_section *debug_aranges, /*@only@*/ yasm_expr *start, - /*@only@*/ yasm_expr *length, unsigned int sizeof_address) + /*@only@*/ yasm_expr *length, unsigned int sizeof_address) { yasm_datavalhead dvs; yasm_bytecode *bc; @@ -64,17 +64,17 @@ dwarf2_generate_aranges_section(yasm_section *sect, /*@null@*/ void *d) dsd = yasm_section_get_data(sect, &yasm_dwarf2__section_data_cb); if (!dsd) - return 0; /* no line data for this section */ + return 0; /* no line data for this section */ /* Create address range descriptor */ start = yasm_expr_create_ident( - yasm_expr_sym(yasm_dwarf2__bc_sym(info->object->symtab, - yasm_section_bcs_first(sect))), 0); + yasm_expr_sym(yasm_dwarf2__bc_sym(info->object->symtab, + yasm_section_bcs_first(sect))), 0); length = yasm_expr_create_ident( - yasm_expr_int(yasm_calc_bc_dist(yasm_section_bcs_first(sect), - yasm_section_bcs_last(sect))), 0); + yasm_expr_int(yasm_calc_bc_dist(yasm_section_bcs_first(sect), + yasm_section_bcs_last(sect))), 0); dwarf2_append_arange(info->debug_aranges, start, length, - dbgfmt_dwarf2->sizeof_address); + dbgfmt_dwarf2->sizeof_address); return 0; } @@ -90,19 +90,19 @@ yasm_dwarf2__generate_aranges(yasm_object *object, yasm_section *debug_info) dwarf2_aranges_info info; debug_aranges = - yasm_object_get_general(object, ".debug_aranges", 0, - 2*dbgfmt_dwarf2->sizeof_address, 0, 0, &new, - 0); + yasm_object_get_general(object, ".debug_aranges", 0, + 2*dbgfmt_dwarf2->sizeof_address, 0, 0, &new, + 0); /* header */ head = yasm_dwarf2__add_head(dbgfmt_dwarf2, debug_aranges, debug_info, 1, - 1); + 1); /* align ranges to 2x address size (range size) */ bc = yasm_bc_create_align( - yasm_expr_create_ident(yasm_expr_int( - yasm_intnum_create_uint(dbgfmt_dwarf2->sizeof_address*2)), 0), - NULL, NULL, NULL, 0); + yasm_expr_create_ident(yasm_expr_int( + yasm_intnum_create_uint(dbgfmt_dwarf2->sizeof_address*2)), 0), + NULL, NULL, NULL, 0); yasm_bc_finalize(bc, yasm_dwarf2__append_bc(debug_aranges, bc)); yasm_bc_calc_len(bc, NULL, NULL); @@ -111,13 +111,13 @@ yasm_dwarf2__generate_aranges(yasm_object *object, yasm_section *debug_info) info.dbgfmt_dwarf2 = dbgfmt_dwarf2; yasm_object_sections_traverse(object, (void *)&info, - dwarf2_generate_aranges_section); + dwarf2_generate_aranges_section); /* Terminate with empty address range descriptor */ dwarf2_append_arange(debug_aranges, - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0), - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0), - dbgfmt_dwarf2->sizeof_address); + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0), + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0), + dbgfmt_dwarf2->sizeof_address); /* mark end of aranges information */ yasm_dwarf2__set_head_end(head, yasm_section_bcs_last(debug_aranges)); diff --git a/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.c b/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.c index e498f0d1..23e797e6 100644 --- a/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.c +++ b/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.c @@ -44,7 +44,7 @@ struct dwarf2_head { /* Bytecode callback function prototypes */ static void dwarf2_head_bc_destroy(void *contents); static void dwarf2_head_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int dwarf2_head_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int dwarf2_head_bc_tobytes @@ -80,7 +80,7 @@ static /*@null@*/ /*@only@*/ yasm_dbgfmt * dwarf2_dbgfmt_create(yasm_object *object) { yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = - yasm_xmalloc(sizeof(yasm_dbgfmt_dwarf2)); + yasm_xmalloc(sizeof(yasm_dbgfmt_dwarf2)); size_t i; dbgfmt_dwarf2->dbgfmt.module = &yasm_dwarf2_LTX_dbgfmt; @@ -88,28 +88,28 @@ dwarf2_dbgfmt_create(yasm_object *object) dbgfmt_dwarf2->dirs_allocated = 32; dbgfmt_dwarf2->dirs_size = 0; dbgfmt_dwarf2->dirs = - yasm_xmalloc(sizeof(char *)*dbgfmt_dwarf2->dirs_allocated); + yasm_xmalloc(sizeof(char *)*dbgfmt_dwarf2->dirs_allocated); dbgfmt_dwarf2->filenames_allocated = 32; dbgfmt_dwarf2->filenames_size = 0; dbgfmt_dwarf2->filenames = - yasm_xmalloc(sizeof(dwarf2_filename)*dbgfmt_dwarf2->filenames_allocated); + yasm_xmalloc(sizeof(dwarf2_filename)*dbgfmt_dwarf2->filenames_allocated); for (i=0; ifilenames_allocated; i++) { - dbgfmt_dwarf2->filenames[i].pathname = NULL; - dbgfmt_dwarf2->filenames[i].filename = NULL; - dbgfmt_dwarf2->filenames[i].dir = 0; + dbgfmt_dwarf2->filenames[i].pathname = NULL; + dbgfmt_dwarf2->filenames[i].filename = NULL; + dbgfmt_dwarf2->filenames[i].dir = 0; } dbgfmt_dwarf2->format = DWARF2_FORMAT_32BIT; /* TODO: flexible? */ dbgfmt_dwarf2->sizeof_address = yasm_arch_get_address_size(object->arch)/8; switch (dbgfmt_dwarf2->format) { - case DWARF2_FORMAT_32BIT: - dbgfmt_dwarf2->sizeof_offset = 4; - break; - case DWARF2_FORMAT_64BIT: - dbgfmt_dwarf2->sizeof_offset = 8; - break; + case DWARF2_FORMAT_32BIT: + dbgfmt_dwarf2->sizeof_offset = 4; + break; + case DWARF2_FORMAT_64BIT: + dbgfmt_dwarf2->sizeof_offset = 8; + break; } dbgfmt_dwarf2->min_insn_len = yasm_arch_min_insn_len(object->arch); @@ -122,14 +122,14 @@ dwarf2_dbgfmt_destroy(/*@only@*/ yasm_dbgfmt *dbgfmt) yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)dbgfmt; size_t i; for (i=0; idirs_size; i++) - if (dbgfmt_dwarf2->dirs[i]) - yasm_xfree(dbgfmt_dwarf2->dirs[i]); + if (dbgfmt_dwarf2->dirs[i]) + yasm_xfree(dbgfmt_dwarf2->dirs[i]); yasm_xfree(dbgfmt_dwarf2->dirs); for (i=0; ifilenames_size; i++) { - if (dbgfmt_dwarf2->filenames[i].pathname) - yasm_xfree(dbgfmt_dwarf2->filenames[i].pathname); - if (dbgfmt_dwarf2->filenames[i].filename) - yasm_xfree(dbgfmt_dwarf2->filenames[i].filename); + if (dbgfmt_dwarf2->filenames[i].pathname) + yasm_xfree(dbgfmt_dwarf2->filenames[i].pathname); + if (dbgfmt_dwarf2->filenames[i].filename) + yasm_xfree(dbgfmt_dwarf2->filenames[i].filename); } yasm_xfree(dbgfmt_dwarf2->filenames); yasm_xfree(dbgfmt); @@ -149,7 +149,7 @@ yasm_dwarf2__append_bc(yasm_section *sect, yasm_bytecode *bc) static void dwarf2_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt; size_t num_line_sections; @@ -159,8 +159,8 @@ dwarf2_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, * based on the asm source. */ debug_line = yasm_dwarf2__generate_line(object, linemap, errwarns, - dbgfmt_dwarf2->filenames_size == 0, - &main_code, &num_line_sections); + dbgfmt_dwarf2->filenames_size == 0, + &main_code, &num_line_sections); /* If we don't have a .debug_info (or it's empty), generate the minimal * set of .debug_info, .debug_aranges, and .debug_abbrev so that the @@ -168,11 +168,11 @@ dwarf2_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, */ debug_info = yasm_object_find_general(object, ".debug_info"); if (num_line_sections > 0 && - (!debug_info || yasm_section_bcs_first(debug_info) - == yasm_section_bcs_last(debug_info))) { - debug_info = yasm_dwarf2__generate_info(object, debug_line, main_code); - yasm_dwarf2__generate_aranges(object, debug_info); - /*yasm_dwarf2__generate_pubnames(object, debug_info);*/ + (!debug_info || yasm_section_bcs_first(debug_info) + == yasm_section_bcs_last(debug_info))) { + debug_info = yasm_dwarf2__generate_info(object, debug_line, main_code); + yasm_dwarf2__generate_aranges(object, debug_info); + /*yasm_dwarf2__generate_pubnames(object, debug_info);*/ } } @@ -181,9 +181,9 @@ yasm_dwarf2__bc_sym(yasm_symtab *symtab, yasm_bytecode *bc) { /*@dependent@*/ yasm_symrec *sym; if (bc->symrecs && bc->symrecs[0]) - sym = bc->symrecs[0]; + sym = bc->symrecs[0]; else - sym = yasm_symtab_define_label(symtab, ".bcsym", bc, 0, 0); + sym = yasm_symtab_define_label(symtab, ".bcsym", bc, 0, 0); return sym; } @@ -201,20 +201,20 @@ yasm_dwarf2__add_head bc = yasm_bc_create_common(&dwarf2_head_bc_callback, head, 0); bc->len = dbgfmt_dwarf2->sizeof_offset + 2; if (dbgfmt_dwarf2->format == DWARF2_FORMAT_64BIT) - bc->len += 4; + bc->len += 4; if (debug_ptr) { - head->debug_ptr = debug_ptr; - bc->len += dbgfmt_dwarf2->sizeof_offset; + head->debug_ptr = debug_ptr; + bc->len += dbgfmt_dwarf2->sizeof_offset; } else - head->debug_ptr = NULL; + head->debug_ptr = NULL; head->with_address = with_address; head->with_segment = with_segment; if (with_address) - bc->len++; + bc->len++; if (with_segment) - bc->len++; + bc->len++; head->end_prevbc = bc; yasm_dwarf2__append_bc(sect, bc); @@ -241,7 +241,7 @@ dwarf2_head_bc_print(const void *contents, FILE *f, int indent_level) static int dwarf2_head_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a dwarf2 head bytecode")); /*@notreached@*/ @@ -250,8 +250,8 @@ dwarf2_head_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int dwarf2_head_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { yasm_object *object = yasm_section_get_object(bc->section); yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt; @@ -260,10 +260,10 @@ dwarf2_head_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_intnum *intn, *cval; if (dbgfmt_dwarf2->format == DWARF2_FORMAT_64BIT) { - YASM_WRITE_8(buf, 0xff); - YASM_WRITE_8(buf, 0xff); - YASM_WRITE_8(buf, 0xff); - YASM_WRITE_8(buf, 0xff); + YASM_WRITE_8(buf, 0xff); + YASM_WRITE_8(buf, 0xff); + YASM_WRITE_8(buf, 0xff); + YASM_WRITE_8(buf, 0xff); } /* Total length of aranges info (following this field) */ @@ -271,8 +271,8 @@ dwarf2_head_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, intn = yasm_calc_bc_dist(head->start_prevbc, head->end_prevbc); yasm_intnum_calc(intn, YASM_EXPR_SUB, cval); yasm_arch_intnum_tobytes(object->arch, intn, buf, - dbgfmt_dwarf2->sizeof_offset, - dbgfmt_dwarf2->sizeof_offset*8, 0, bc, 0); + dbgfmt_dwarf2->sizeof_offset, + dbgfmt_dwarf2->sizeof_offset*8, 0, bc, 0); buf += dbgfmt_dwarf2->sizeof_offset; yasm_intnum_destroy(intn); @@ -283,23 +283,23 @@ dwarf2_head_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Pointer to another debug section */ if (head->debug_ptr) { - yasm_value value; - yasm_value_init_sym(&value, - yasm_dwarf2__bc_sym(object->symtab, - yasm_section_bcs_first(head->debug_ptr)), - dbgfmt_dwarf2->sizeof_offset*8); - output_value(&value, buf, dbgfmt_dwarf2->sizeof_offset, - (unsigned long)(buf-*bufp), bc, 0, d); - buf += dbgfmt_dwarf2->sizeof_offset; + yasm_value value; + yasm_value_init_sym(&value, + yasm_dwarf2__bc_sym(object->symtab, + yasm_section_bcs_first(head->debug_ptr)), + dbgfmt_dwarf2->sizeof_offset*8); + output_value(&value, buf, dbgfmt_dwarf2->sizeof_offset, + (unsigned long)(buf-*bufp), bc, 0, d); + buf += dbgfmt_dwarf2->sizeof_offset; } /* Size of the offset portion of the address */ if (head->with_address) - YASM_WRITE_8(buf, dbgfmt_dwarf2->sizeof_address); + YASM_WRITE_8(buf, dbgfmt_dwarf2->sizeof_address); /* Size of a segment descriptor. 0 = flat address space */ if (head->with_segment) - YASM_WRITE_8(buf, 0); + YASM_WRITE_8(buf, 0); *bufp = buf; @@ -316,9 +316,9 @@ dwarf2_section_data_destroy(void *data) /* Delete locations */ n1 = STAILQ_FIRST(&dsd->locs); while (n1) { - n2 = STAILQ_NEXT(n1, link); - yasm_xfree(n1); - n1 = n2; + n2 = STAILQ_NEXT(n1, link); + yasm_xfree(n1); + n1 = n2; } yasm_xfree(data); @@ -331,10 +331,10 @@ dwarf2_section_data_print(void *data, FILE *f, int indent_level) } static const yasm_directive dwarf2_directives[] = { - { ".loc", "gas", yasm_dwarf2__dir_loc, YASM_DIR_ARG_REQUIRED }, - { ".file", "gas", yasm_dwarf2__dir_file, YASM_DIR_ARG_REQUIRED }, - { "loc", "nasm", yasm_dwarf2__dir_loc, YASM_DIR_ARG_REQUIRED }, - { "file", "nasm", yasm_dwarf2__dir_file, YASM_DIR_ARG_REQUIRED }, + { ".loc", "gas", yasm_dwarf2__dir_loc, YASM_DIR_ARG_REQUIRED }, + { ".file", "gas", yasm_dwarf2__dir_file, YASM_DIR_ARG_REQUIRED }, + { "loc", "nasm", yasm_dwarf2__dir_loc, YASM_DIR_ARG_REQUIRED }, + { "file", "nasm", yasm_dwarf2__dir_file, YASM_DIR_ARG_REQUIRED }, { NULL, NULL, NULL, 0 } }; diff --git a/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.h b/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.h index eeba0a19..38ba0c53 100644 --- a/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.h +++ b/modules/dbgfmts/dwarf2/dwarf2-dbgfmt.h @@ -30,15 +30,15 @@ #define WITH_DWARF3 1 typedef struct { - char *pathname; /* full filename */ - char *filename; /* basename of full filename */ - unsigned long dir; /* index into directories array for relative path; - * 0 for current directory. */ + char *pathname; /* full filename */ + char *filename; /* basename of full filename */ + unsigned long dir; /* index into directories array for relative path; + * 0 for current directory. */ } dwarf2_filename; /* Global data */ typedef struct yasm_dbgfmt_dwarf2 { - yasm_dbgfmt_base dbgfmt; /* base structure */ + yasm_dbgfmt_base dbgfmt; /* base structure */ char **dirs; unsigned long dirs_size; @@ -49,8 +49,8 @@ typedef struct yasm_dbgfmt_dwarf2 { unsigned long filenames_allocated; enum { - DWARF2_FORMAT_32BIT, - DWARF2_FORMAT_64BIT + DWARF2_FORMAT_32BIT, + DWARF2_FORMAT_64BIT } format; unsigned int sizeof_address, sizeof_offset, min_insn_len; @@ -63,22 +63,22 @@ typedef struct dwarf2_loc { unsigned long vline; /* virtual line number of .loc directive */ /* source information */ - unsigned long file; /* index into table of filenames */ - unsigned long line; /* source line number */ + unsigned long file; /* index into table of filenames */ + unsigned long line; /* source line number */ unsigned long column; /* source column */ int isa_change; unsigned long isa; enum { - IS_STMT_NOCHANGE = 0, - IS_STMT_SET, - IS_STMT_CLEAR + IS_STMT_NOCHANGE = 0, + IS_STMT_SET, + IS_STMT_CLEAR } is_stmt; int basic_block; int prologue_end; int epilogue_begin; - yasm_bytecode *bc; /* first bytecode following */ - yasm_symrec *sym; /* last symbol preceding */ + yasm_bytecode *bc; /* first bytecode following */ + yasm_symrec *sym; /* last symbol preceding */ } dwarf2_loc; /* Per-section data */ @@ -94,7 +94,7 @@ extern const yasm_assoc_data_callback yasm_dwarf2__section_data_cb; yasm_bytecode *yasm_dwarf2__append_bc(yasm_section *sect, yasm_bytecode *bc); /*@dependent@*/ yasm_symrec *yasm_dwarf2__bc_sym(yasm_symtab *symtab, - yasm_bytecode *bc); + yasm_bytecode *bc); typedef struct dwarf2_head dwarf2_head; dwarf2_head *yasm_dwarf2__add_head @@ -108,19 +108,19 @@ yasm_section *yasm_dwarf2__generate_line int asm_source, /*@out@*/ yasm_section **main_code, /*@out@*/ size_t *num_line_sections); void yasm_dwarf2__dir_loc(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, - unsigned long line); + yasm_valparamhead *objext_valparams, + unsigned long line); void yasm_dwarf2__dir_file(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, - unsigned long line); + yasm_valparamhead *objext_valparams, + unsigned long line); /* Address range table functions */ yasm_section *yasm_dwarf2__generate_aranges(yasm_object *object, - yasm_section *debug_info); + yasm_section *debug_info); /* Name lookup table functions */ yasm_section *yasm_dwarf2__generate_pubnames(yasm_object *object, - yasm_section *debug_info); + yasm_section *debug_info); /* Information functions */ yasm_section *yasm_dwarf2__generate_info diff --git a/modules/dbgfmts/dwarf2/dwarf2-info.c b/modules/dbgfmts/dwarf2/dwarf2-info.c index f17db488..e08ea1c0 100644 --- a/modules/dbgfmts/dwarf2/dwarf2-info.c +++ b/modules/dbgfmts/dwarf2/dwarf2-info.c @@ -40,7 +40,7 @@ #include "dwarf2-dbgfmt.h" -#define DW_LANG_Mips_Assembler 0x8001 +#define DW_LANG_Mips_Assembler 0x8001 /* Tag encodings */ typedef enum { @@ -202,7 +202,7 @@ typedef struct dwarf2_abbrev { static void dwarf2_abbrev_bc_destroy(void *contents); static void dwarf2_abbrev_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int dwarf2_abbrev_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int dwarf2_abbrev_bc_tobytes @@ -225,7 +225,7 @@ static const yasm_bytecode_callback dwarf2_abbrev_bc_callback = { static unsigned long dwarf2_add_abbrev_attr(dwarf2_abbrev *abbrev, dwarf_attribute name, - dwarf_form form) + dwarf_form form) { dwarf2_abbrev_attr *attr = yasm_xmalloc(sizeof(dwarf2_abbrev_attr)); attr->name = name; @@ -236,7 +236,7 @@ dwarf2_add_abbrev_attr(dwarf2_abbrev *abbrev, dwarf_attribute name, static void dwarf2_append_expr(yasm_section *sect, /*@only@*/ yasm_expr *expr, - unsigned int size, int leb) + unsigned int size, int leb) { yasm_datavalhead dvs; yasm_bytecode *bc; @@ -244,9 +244,9 @@ dwarf2_append_expr(yasm_section *sect, /*@only@*/ yasm_expr *expr, yasm_dvs_initialize(&dvs); yasm_dvs_append(&dvs, yasm_dv_create_expr(expr)); if (leb == 0) - bc = yasm_bc_create_data(&dvs, size, 0, NULL, 0); + bc = yasm_bc_create_data(&dvs, size, 0, NULL, 0); else - bc = yasm_bc_create_leb128(&dvs, leb<0, 0); + bc = yasm_bc_create_leb128(&dvs, leb<0, 0); yasm_bc_finalize(bc, yasm_dwarf2__append_bc(sect, bc)); yasm_bc_calc_len(bc, NULL, NULL); } @@ -259,7 +259,7 @@ dwarf2_append_str(yasm_section *sect, const char *str) yasm_dvs_initialize(&dvs); yasm_dvs_append(&dvs, yasm_dv_create_string(yasm__xstrdup(str), - strlen(str))); + strlen(str))); bc = yasm_bc_create_data(&dvs, 1, 1, NULL, 0); yasm_bc_finalize(bc, yasm_dwarf2__append_bc(sect, bc)); yasm_bc_calc_len(bc, NULL, NULL); @@ -267,7 +267,7 @@ dwarf2_append_str(yasm_section *sect, const char *str) yasm_section * yasm_dwarf2__generate_info(yasm_object *object, yasm_section *debug_line, - yasm_section *main_code) + yasm_section *main_code) { yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt; int new; @@ -276,9 +276,9 @@ yasm_dwarf2__generate_info(yasm_object *object, yasm_section *debug_line, dwarf2_head *head; char *buf; yasm_section *debug_abbrev = - yasm_object_get_general(object, ".debug_abbrev", 0, 4, 0, 0, &new, 0); + yasm_object_get_general(object, ".debug_abbrev", 0, 4, 0, 0, &new, 0); yasm_section *debug_info = - yasm_object_get_general(object, ".debug_info", 0, 4, 0, 0, &new, 0); + yasm_object_get_general(object, ".debug_info", 0, 4, 0, 0, &new, 0); yasm_section_set_align(debug_abbrev, 0, 0); yasm_section_set_align(debug_info, 0, 0); @@ -290,7 +290,7 @@ yasm_dwarf2__generate_info(yasm_object *object, yasm_section *debug_line, abbrev->tag = DW_TAG_compile_unit; abbrev->has_children = 0; abc->len = yasm_size_uleb128(abbrev->id) + yasm_size_uleb128(abbrev->tag) - + 3; + + 3; STAILQ_INIT(&abbrev->attrs); yasm_dwarf2__append_bc(debug_abbrev, abc); @@ -303,34 +303,34 @@ yasm_dwarf2__generate_info(yasm_object *object, yasm_section *debug_line, /* generating info using abbrev 1 */ dwarf2_append_expr(debug_info, - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(1)), 0), - 0, 1); + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(1)), 0), + 0, 1); /* statement list (line numbers) */ abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_stmt_list, DW_FORM_data4); dwarf2_append_expr(debug_info, - yasm_expr_create_ident(yasm_expr_sym( - yasm_dwarf2__bc_sym(object->symtab, - yasm_section_bcs_first(debug_line))), 0), - dbgfmt_dwarf2->sizeof_offset, 0); + yasm_expr_create_ident(yasm_expr_sym( + yasm_dwarf2__bc_sym(object->symtab, + yasm_section_bcs_first(debug_line))), 0), + dbgfmt_dwarf2->sizeof_offset, 0); if (main_code) { - yasm_symrec *first; - first = yasm_dwarf2__bc_sym(object->symtab, - yasm_section_bcs_first(main_code)); - /* All code is contiguous in one section */ - abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_low_pc, DW_FORM_addr); - dwarf2_append_expr(debug_info, - yasm_expr_create_ident(yasm_expr_sym(first), 0), - dbgfmt_dwarf2->sizeof_address, 0); - - abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_high_pc, DW_FORM_addr); - dwarf2_append_expr(debug_info, - yasm_expr_create(YASM_EXPR_ADD, yasm_expr_sym(first), - yasm_expr_int(yasm_calc_bc_dist( - yasm_section_bcs_first(main_code), - yasm_section_bcs_last(main_code))), 0), - dbgfmt_dwarf2->sizeof_address, 0); + yasm_symrec *first; + first = yasm_dwarf2__bc_sym(object->symtab, + yasm_section_bcs_first(main_code)); + /* All code is contiguous in one section */ + abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_low_pc, DW_FORM_addr); + dwarf2_append_expr(debug_info, + yasm_expr_create_ident(yasm_expr_sym(first), 0), + dbgfmt_dwarf2->sizeof_address, 0); + + abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_high_pc, DW_FORM_addr); + dwarf2_append_expr(debug_info, + yasm_expr_create(YASM_EXPR_ADD, yasm_expr_sym(first), + yasm_expr_int(yasm_calc_bc_dist( + yasm_section_bcs_first(main_code), + yasm_section_bcs_last(main_code))), 0), + dbgfmt_dwarf2->sizeof_address, 0); } /* input filename */ @@ -350,8 +350,8 @@ yasm_dwarf2__generate_info(yasm_object *object, yasm_section *debug_line, /* language - no standard code for assembler, use MIPS as a substitute */ abc->len += dwarf2_add_abbrev_attr(abbrev, DW_AT_language, DW_FORM_data2); dwarf2_append_expr(debug_info, - yasm_expr_create_ident(yasm_expr_int( - yasm_intnum_create_uint(DW_LANG_Mips_Assembler)), 0), 2, 0); + yasm_expr_create_ident(yasm_expr_int( + yasm_intnum_create_uint(DW_LANG_Mips_Assembler)), 0), 2, 0); /* Terminate list of abbreviations */ abbrev = yasm_xmalloc(sizeof(dwarf2_abbrev)); @@ -378,9 +378,9 @@ dwarf2_abbrev_bc_destroy(void *contents) /* Delete attributes */ n1 = STAILQ_FIRST(&abbrev->attrs); while (n1) { - n2 = STAILQ_NEXT(n1, link); - yasm_xfree(n1); - n1 = n2; + n2 = STAILQ_NEXT(n1, link); + yasm_xfree(n1); + n1 = n2; } yasm_xfree(contents); @@ -394,7 +394,7 @@ dwarf2_abbrev_bc_print(const void *contents, FILE *f, int indent_level) static int dwarf2_abbrev_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a dwarf2 aranges head bytecode")); /*@notreached@*/ @@ -403,17 +403,17 @@ dwarf2_abbrev_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int dwarf2_abbrev_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { dwarf2_abbrev *abbrev = (dwarf2_abbrev *)bc->contents; unsigned char *buf = *bufp; dwarf2_abbrev_attr *attr; if (abbrev->id == 0) { - YASM_WRITE_8(buf, 0); - *bufp = buf; - return 0; + YASM_WRITE_8(buf, 0); + *bufp = buf; + return 0; } buf += yasm_get_uleb128(abbrev->id, buf); @@ -421,8 +421,8 @@ dwarf2_abbrev_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, YASM_WRITE_8(buf, abbrev->has_children); STAILQ_FOREACH(attr, &abbrev->attrs, link) { - buf += yasm_get_uleb128(attr->name, buf); - buf += yasm_get_uleb128(attr->form, buf); + buf += yasm_get_uleb128(attr->name, buf); + buf += yasm_get_uleb128(attr->form, buf); } YASM_WRITE_8(buf, 0); diff --git a/modules/dbgfmts/dwarf2/dwarf2-line.c b/modules/dbgfmts/dwarf2/dwarf2-line.c index 6b80ac31..dc26b371 100644 --- a/modules/dbgfmts/dwarf2/dwarf2-line.c +++ b/modules/dbgfmts/dwarf2/dwarf2-line.c @@ -59,19 +59,19 @@ typedef enum { /* # of LEB128 operands needed for each of the above opcodes */ static unsigned char line_opcode_num_operands[DWARF2_LINE_OPCODE_BASE-1] = { - 0, /* DW_LNS_copy */ - 1, /* DW_LNS_advance_pc */ - 1, /* DW_LNS_advance_line */ - 1, /* DW_LNS_set_file */ - 1, /* DW_LNS_set_column */ - 0, /* DW_LNS_negate_stmt */ - 0, /* DW_LNS_set_basic_block */ - 0, /* DW_LNS_const_add_pc */ - 1, /* DW_LNS_fixed_advance_pc */ + 0, /* DW_LNS_copy */ + 1, /* DW_LNS_advance_pc */ + 1, /* DW_LNS_advance_line */ + 1, /* DW_LNS_set_file */ + 1, /* DW_LNS_set_column */ + 0, /* DW_LNS_negate_stmt */ + 0, /* DW_LNS_set_basic_block */ + 0, /* DW_LNS_const_add_pc */ + 1, /* DW_LNS_fixed_advance_pc */ #ifdef WITH_DWARF3 - 0, /* DW_LNS_set_prologue_end */ - 0, /* DW_LNS_set_epilogue_begin */ - 1 /* DW_LNS_set_isa */ + 0, /* DW_LNS_set_prologue_end */ + 0, /* DW_LNS_set_epilogue_begin */ + 1 /* DW_LNS_set_isa */ #endif }; @@ -83,15 +83,15 @@ typedef enum { } dwarf_line_number_ext_op; /* Base and range for line offsets in special opcodes */ -#define DWARF2_LINE_BASE -5 -#define DWARF2_LINE_RANGE 14 +#define DWARF2_LINE_BASE -5 +#define DWARF2_LINE_RANGE 14 -#define DWARF2_MAX_SPECIAL_ADDR_DELTA \ +#define DWARF2_MAX_SPECIAL_ADDR_DELTA \ (((255-DWARF2_LINE_OPCODE_BASE)/DWARF2_LINE_RANGE)*\ dbgfmt_dwarf2->min_insn_len) /* Initial value of is_stmt register */ -#define DWARF2_LINE_DEFAULT_IS_STMT 1 +#define DWARF2_LINE_DEFAULT_IS_STMT 1 /* Line number state machine register state */ typedef struct dwarf2_line_state { @@ -128,7 +128,7 @@ typedef struct dwarf2_line_op { /* Bytecode callback function prototypes */ static void dwarf2_spp_bc_destroy(void *contents); static void dwarf2_spp_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int dwarf2_spp_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int dwarf2_spp_bc_tobytes @@ -138,7 +138,7 @@ static int dwarf2_spp_bc_tobytes static void dwarf2_line_op_bc_destroy(void *contents); static void dwarf2_line_op_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static int dwarf2_line_op_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int dwarf2_line_op_bc_tobytes @@ -170,7 +170,7 @@ static const yasm_bytecode_callback dwarf2_line_op_bc_callback = { static size_t dwarf2_dbgfmt_add_file(yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2, unsigned long filenum, - const char *pathname) + const char *pathname) { size_t dirlen; const char *filename; @@ -180,62 +180,62 @@ dwarf2_dbgfmt_add_file(yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2, unsigned long filenum, dir = 0; dirlen = yasm__splitpath(pathname, &filename); if (dirlen > 0) { - /* Look to see if we already have that dir in the table */ - for (dir=1; dirdirs_size+1; dir++) { - if (strncmp(dbgfmt_dwarf2->dirs[dir-1], pathname, dirlen) == 0 - && dbgfmt_dwarf2->dirs[dir-1][dirlen] == '\0') - break; - } - if (dir >= dbgfmt_dwarf2->dirs_size+1) { - /* Not found in table, add to end, reallocing if necessary */ - if (dir >= dbgfmt_dwarf2->dirs_allocated+1) { - dbgfmt_dwarf2->dirs_allocated = dir+32; - dbgfmt_dwarf2->dirs = yasm_xrealloc(dbgfmt_dwarf2->dirs, - sizeof(char *)*dbgfmt_dwarf2->dirs_allocated); - } - dbgfmt_dwarf2->dirs[dir-1] = yasm__xstrndup(pathname, dirlen); - dbgfmt_dwarf2->dirs_size = dir; - } + /* Look to see if we already have that dir in the table */ + for (dir=1; dirdirs_size+1; dir++) { + if (strncmp(dbgfmt_dwarf2->dirs[dir-1], pathname, dirlen) == 0 + && dbgfmt_dwarf2->dirs[dir-1][dirlen] == '\0') + break; + } + if (dir >= dbgfmt_dwarf2->dirs_size+1) { + /* Not found in table, add to end, reallocing if necessary */ + if (dir >= dbgfmt_dwarf2->dirs_allocated+1) { + dbgfmt_dwarf2->dirs_allocated = dir+32; + dbgfmt_dwarf2->dirs = yasm_xrealloc(dbgfmt_dwarf2->dirs, + sizeof(char *)*dbgfmt_dwarf2->dirs_allocated); + } + dbgfmt_dwarf2->dirs[dir-1] = yasm__xstrndup(pathname, dirlen); + dbgfmt_dwarf2->dirs_size = dir; + } } /* Put the filename into the filename table */ if (filenum == 0) { - /* Look to see if we already have that filename in the table */ - for (; filenumfilenames_size; filenum++) { - if (!dbgfmt_dwarf2->filenames[filenum].filename || - (dbgfmt_dwarf2->filenames[filenum].dir == dir - && strcmp(dbgfmt_dwarf2->filenames[filenum].filename, - filename) == 0)) - break; - } + /* Look to see if we already have that filename in the table */ + for (; filenumfilenames_size; filenum++) { + if (!dbgfmt_dwarf2->filenames[filenum].filename || + (dbgfmt_dwarf2->filenames[filenum].dir == dir + && strcmp(dbgfmt_dwarf2->filenames[filenum].filename, + filename) == 0)) + break; + } } else - filenum--; /* array index is 0-based */ + filenum--; /* array index is 0-based */ /* Realloc table if necessary */ if (filenum >= dbgfmt_dwarf2->filenames_allocated) { - unsigned long old_allocated = dbgfmt_dwarf2->filenames_allocated; - dbgfmt_dwarf2->filenames_allocated = filenum+32; - dbgfmt_dwarf2->filenames = yasm_xrealloc(dbgfmt_dwarf2->filenames, - sizeof(dwarf2_filename)*dbgfmt_dwarf2->filenames_allocated); - for (i=old_allocated; ifilenames_allocated; i++) { - dbgfmt_dwarf2->filenames[i].pathname = NULL; - dbgfmt_dwarf2->filenames[i].filename = NULL; - dbgfmt_dwarf2->filenames[i].dir = 0; - } + unsigned long old_allocated = dbgfmt_dwarf2->filenames_allocated; + dbgfmt_dwarf2->filenames_allocated = filenum+32; + dbgfmt_dwarf2->filenames = yasm_xrealloc(dbgfmt_dwarf2->filenames, + sizeof(dwarf2_filename)*dbgfmt_dwarf2->filenames_allocated); + for (i=old_allocated; ifilenames_allocated; i++) { + dbgfmt_dwarf2->filenames[i].pathname = NULL; + dbgfmt_dwarf2->filenames[i].filename = NULL; + dbgfmt_dwarf2->filenames[i].dir = 0; + } } /* Actually save in table */ if (dbgfmt_dwarf2->filenames[filenum].pathname) - yasm_xfree(dbgfmt_dwarf2->filenames[filenum].pathname); + yasm_xfree(dbgfmt_dwarf2->filenames[filenum].pathname); if (dbgfmt_dwarf2->filenames[filenum].filename) - yasm_xfree(dbgfmt_dwarf2->filenames[filenum].filename); + yasm_xfree(dbgfmt_dwarf2->filenames[filenum].filename); dbgfmt_dwarf2->filenames[filenum].pathname = yasm__xstrdup(pathname); dbgfmt_dwarf2->filenames[filenum].filename = yasm__xstrdup(filename); dbgfmt_dwarf2->filenames[filenum].dir = dir; /* Update table size */ if (filenum >= dbgfmt_dwarf2->filenames_size) - dbgfmt_dwarf2->filenames_size = filenum + 1; + dbgfmt_dwarf2->filenames_size = filenum + 1; return filenum; } @@ -245,7 +245,7 @@ dwarf2_dbgfmt_add_file(yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2, unsigned long filenum, */ static yasm_bytecode * dwarf2_dbgfmt_append_line_op(yasm_section *sect, dwarf_line_number_op opcode, - /*@only@*/ /*@null@*/ yasm_intnum *operand) + /*@only@*/ /*@null@*/ yasm_intnum *operand) { dwarf2_line_op *line_op = yasm_xmalloc(sizeof(dwarf2_line_op)); yasm_bytecode *bc; @@ -259,8 +259,8 @@ dwarf2_dbgfmt_append_line_op(yasm_section *sect, dwarf_line_number_op opcode, bc = yasm_bc_create_common(&dwarf2_line_op_bc_callback, line_op, 0); bc->len = 1; if (operand) - bc->len += yasm_intnum_size_leb128(operand, - opcode == DW_LNS_advance_line); + bc->len += yasm_intnum_size_leb128(operand, + opcode == DW_LNS_advance_line); yasm_dwarf2__append_bc(sect, bc); return bc; @@ -271,9 +271,9 @@ dwarf2_dbgfmt_append_line_op(yasm_section *sect, dwarf_line_number_op opcode, */ static yasm_bytecode * dwarf2_dbgfmt_append_line_ext_op(yasm_section *sect, - dwarf_line_number_ext_op ext_opcode, - unsigned long ext_operandsize, - /*@null@*/ yasm_symrec *ext_operand) + dwarf_line_number_ext_op ext_opcode, + unsigned long ext_operandsize, + /*@null@*/ yasm_symrec *ext_operand) { dwarf2_line_op *line_op = yasm_xmalloc(sizeof(dwarf2_line_op)); yasm_bytecode *bc; @@ -286,7 +286,7 @@ dwarf2_dbgfmt_append_line_ext_op(yasm_section *sect, bc = yasm_bc_create_common(&dwarf2_line_op_bc_callback, line_op, 0); bc->len = 2 + yasm_intnum_size_leb128(line_op->operand, 0) + - ext_operandsize; + ext_operandsize; yasm_dwarf2__append_bc(sect, bc); return bc; @@ -301,33 +301,33 @@ dwarf2_dbgfmt_finalize_locs(yasm_section *sect, dwarf2_section_data *dsd) bc = yasm_section_bcs_first(sect); STAILQ_FOREACH(loc, &dsd->locs, link) { - /* Find the first bytecode following this loc by looking at - * the virtual line numbers. XXX: this assumes the source file - * order will be the same as the actual section order. If we ever - * implement subsegs this will NOT necessarily be true and this logic - * will need to be fixed to handle it! - * - * Keep track of last symbol seen prior to the loc. - */ - while (bc && bc->line <= loc->vline) { - if (bc->symrecs) { - int i = 0; - while (bc->symrecs[i]) { - lastsym = bc->symrecs[i]; - i++; - } - } - bc = yasm_bc__next(bc); - } - loc->sym = lastsym; - loc->bc = bc; + /* Find the first bytecode following this loc by looking at + * the virtual line numbers. XXX: this assumes the source file + * order will be the same as the actual section order. If we ever + * implement subsegs this will NOT necessarily be true and this logic + * will need to be fixed to handle it! + * + * Keep track of last symbol seen prior to the loc. + */ + while (bc && bc->line <= loc->vline) { + if (bc->symrecs) { + int i = 0; + while (bc->symrecs[i]) { + lastsym = bc->symrecs[i]; + i++; + } + } + bc = yasm_bc__next(bc); + } + loc->sym = lastsym; + loc->bc = bc; } } static int dwarf2_dbgfmt_gen_line_op(yasm_section *debug_line, dwarf2_line_state *state, - const dwarf2_loc *loc, - /*@null@*/ const dwarf2_loc *nextloc) + const dwarf2_loc *loc, + /*@null@*/ const dwarf2_loc *nextloc) { unsigned long addr_delta; long line_delta; @@ -335,63 +335,63 @@ dwarf2_dbgfmt_gen_line_op(yasm_section *debug_line, dwarf2_line_state *state, yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = state->dbgfmt_dwarf2; if (state->file != loc->file) { - state->file = loc->file; - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_file, - yasm_intnum_create_uint(state->file)); + state->file = loc->file; + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_file, + yasm_intnum_create_uint(state->file)); } if (state->column != loc->column) { - state->column = loc->column; - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_column, - yasm_intnum_create_uint(state->column)); + state->column = loc->column; + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_column, + yasm_intnum_create_uint(state->column)); } #ifdef WITH_DWARF3 if (loc->isa_change) { - state->isa = loc->isa; - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_isa, - yasm_intnum_create_uint(state->isa)); + state->isa = loc->isa; + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_isa, + yasm_intnum_create_uint(state->isa)); } #endif if (state->is_stmt == 0 && loc->is_stmt == IS_STMT_SET) { - state->is_stmt = 1; - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_negate_stmt, NULL); + state->is_stmt = 1; + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_negate_stmt, NULL); } else if (state->is_stmt == 1 && loc->is_stmt == IS_STMT_CLEAR) { - state->is_stmt = 0; - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_negate_stmt, NULL); + state->is_stmt = 0; + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_negate_stmt, NULL); } if (loc->basic_block) { - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_basic_block, NULL); + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_basic_block, NULL); } #ifdef WITH_DWARF3 if (loc->prologue_end) { - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_prologue_end, NULL); + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_prologue_end, NULL); } if (loc->epilogue_begin) { - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_epilogue_begin, - NULL); + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_set_epilogue_begin, + NULL); } #endif /* If multiple loc for the same location, use last */ if (nextloc && nextloc->bc->offset == loc->bc->offset) - return 0; + return 0; if (!state->precbc) { - /* Set the starting address for the section */ - if (!loc->sym) { - /* shouldn't happen! */ - yasm_error_set(YASM_ERROR_GENERAL, - N_("could not find label prior to loc")); - return 1; - } - dwarf2_dbgfmt_append_line_ext_op(debug_line, DW_LNE_set_address, - dbgfmt_dwarf2->sizeof_address, loc->sym); - addr_delta = 0; + /* Set the starting address for the section */ + if (!loc->sym) { + /* shouldn't happen! */ + yasm_error_set(YASM_ERROR_GENERAL, + N_("could not find label prior to loc")); + return 1; + } + dwarf2_dbgfmt_append_line_ext_op(debug_line, DW_LNE_set_address, + dbgfmt_dwarf2->sizeof_address, loc->sym); + addr_delta = 0; } else if (loc->bc) { - if (state->precbc->offset > loc->bc->offset) - yasm_internal_error(N_("dwarf2 address went backwards?")); - addr_delta = loc->bc->offset - state->precbc->offset; + if (state->precbc->offset > loc->bc->offset) + yasm_internal_error(N_("dwarf2 address went backwards?")); + addr_delta = loc->bc->offset - state->precbc->offset; } else - return 0; /* ran out of bytecodes! XXX: do something? */ + return 0; /* ran out of bytecodes! XXX: do something? */ /* Generate appropriate opcode(s). Address can only increment, * whereas line number can go backwards. @@ -401,42 +401,42 @@ dwarf2_dbgfmt_gen_line_op(yasm_section *debug_line, dwarf2_line_state *state, /* First handle the line delta */ if (line_delta < DWARF2_LINE_BASE - || line_delta >= DWARF2_LINE_BASE+DWARF2_LINE_RANGE) { - /* Won't fit in special opcode, use (signed) line advance */ - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_advance_line, - yasm_intnum_create_int(line_delta)); - line_delta = 0; + || line_delta >= DWARF2_LINE_BASE+DWARF2_LINE_RANGE) { + /* Won't fit in special opcode, use (signed) line advance */ + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_advance_line, + yasm_intnum_create_int(line_delta)); + line_delta = 0; } /* Next handle the address delta */ opcode1 = DWARF2_LINE_OPCODE_BASE + line_delta - DWARF2_LINE_BASE + - DWARF2_LINE_RANGE * (addr_delta / dbgfmt_dwarf2->min_insn_len); + DWARF2_LINE_RANGE * (addr_delta / dbgfmt_dwarf2->min_insn_len); opcode2 = DWARF2_LINE_OPCODE_BASE + line_delta - DWARF2_LINE_BASE + - DWARF2_LINE_RANGE * ((addr_delta - DWARF2_MAX_SPECIAL_ADDR_DELTA) / - dbgfmt_dwarf2->min_insn_len); + DWARF2_LINE_RANGE * ((addr_delta - DWARF2_MAX_SPECIAL_ADDR_DELTA) / + dbgfmt_dwarf2->min_insn_len); if (line_delta == 0 && addr_delta == 0) { - /* Both line and addr deltas are 0: do DW_LNS_copy */ - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_copy, NULL); + /* Both line and addr deltas are 0: do DW_LNS_copy */ + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_copy, NULL); } else if (addr_delta <= DWARF2_MAX_SPECIAL_ADDR_DELTA && opcode1 <= 255) { - /* Addr delta in range of special opcode */ - dwarf2_dbgfmt_append_line_op(debug_line, opcode1, NULL); + /* Addr delta in range of special opcode */ + dwarf2_dbgfmt_append_line_op(debug_line, opcode1, NULL); } else if (addr_delta <= 2*DWARF2_MAX_SPECIAL_ADDR_DELTA - && opcode2 <= 255) { - /* Addr delta in range of const_add_pc + special */ - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_const_add_pc, NULL); - dwarf2_dbgfmt_append_line_op(debug_line, opcode2, NULL); + && opcode2 <= 255) { + /* Addr delta in range of const_add_pc + special */ + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_const_add_pc, NULL); + dwarf2_dbgfmt_append_line_op(debug_line, opcode2, NULL); } else { - /* Need advance_pc */ - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_advance_pc, - yasm_intnum_create_uint(addr_delta)); - /* Take care of any remaining line_delta and add entry to matrix */ - if (line_delta == 0) - dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_copy, NULL); - else { - unsigned int opcode; - opcode = DWARF2_LINE_OPCODE_BASE + line_delta - DWARF2_LINE_BASE; - dwarf2_dbgfmt_append_line_op(debug_line, opcode, NULL); - } + /* Need advance_pc */ + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_advance_pc, + yasm_intnum_create_uint(addr_delta)); + /* Take care of any remaining line_delta and add entry to matrix */ + if (line_delta == 0) + dwarf2_dbgfmt_append_line_op(debug_line, DW_LNS_copy, NULL); + else { + unsigned int opcode; + opcode = DWARF2_LINE_OPCODE_BASE + line_delta - DWARF2_LINE_BASE; + dwarf2_dbgfmt_append_line_op(debug_line, opcode, NULL); + } } state->precbc = loc->bc; return 0; @@ -462,44 +462,44 @@ dwarf2_generate_line_bc(yasm_bytecode *bc, /*@null@*/ void *d) /*@null@*/ yasm_bytecode *nextbc = yasm_bc__next(bc); if (nextbc && bc->offset == nextbc->offset) - return 0; + return 0; info->loc.vline = bc->line; info->loc.bc = bc; /* Keep track of last symbol seen */ if (bc->symrecs) { - i = 0; - while (bc->symrecs[i]) { - info->loc.sym = bc->symrecs[i]; - i++; - } + i = 0; + while (bc->symrecs[i]) { + info->loc.sym = bc->symrecs[i]; + i++; + } } yasm_linemap_lookup(info->linemap, bc->line, &filename, &info->loc.line); /* Find file index; just linear search it unless it was the last used */ if (info->lastfile > 0 - && strcmp(filename, dbgfmt_dwarf2->filenames[info->lastfile-1].pathname) - == 0) - info->loc.file = info->lastfile; + && strcmp(filename, dbgfmt_dwarf2->filenames[info->lastfile-1].pathname) + == 0) + info->loc.file = info->lastfile; else { - for (i=0; ifilenames_size; i++) { - if (strcmp(filename, dbgfmt_dwarf2->filenames[i].pathname) == 0) - break; - } - if (i >= dbgfmt_dwarf2->filenames_size) - yasm_internal_error(N_("could not find filename in table")); - info->loc.file = i+1; - info->lastfile = i+1; + for (i=0; ifilenames_size; i++) { + if (strcmp(filename, dbgfmt_dwarf2->filenames[i].pathname) == 0) + break; + } + if (i >= dbgfmt_dwarf2->filenames_size) + yasm_internal_error(N_("could not find filename in table")); + info->loc.file = i+1; + info->lastfile = i+1; } if (dwarf2_dbgfmt_gen_line_op(info->debug_line, info->state, &info->loc, - NULL)) - return 1; + NULL)) + return 1; return 0; } typedef struct dwarf2_line_info { - yasm_section *debug_line; /* section to which line number info goes */ + yasm_section *debug_line; /* section to which line number info goes */ yasm_object *object; yasm_linemap *linemap; yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2; @@ -528,13 +528,13 @@ dwarf2_generate_line_section(yasm_section *sect, /*@null@*/ void *d) dsd = yasm_section_get_data(sect, &yasm_dwarf2__section_data_cb); if (!dsd) { - if (info->asm_source && yasm_section_is_code(sect)) { - /* Create line data for asm code sections */ - dsd = yasm_xmalloc(sizeof(dwarf2_section_data)); - STAILQ_INIT(&dsd->locs); - yasm_section_add_data(sect, &yasm_dwarf2__section_data_cb, dsd); - } else - return 0; /* no line data for this section */ + if (info->asm_source && yasm_section_is_code(sect)) { + /* Create line data for asm code sections */ + dsd = yasm_xmalloc(sizeof(dwarf2_section_data)); + STAILQ_INIT(&dsd->locs); + yasm_section_add_data(sect, &yasm_dwarf2__section_data_cb, dsd); + } else + return 0; /* no line data for this section */ } info->num_sections++; @@ -551,46 +551,46 @@ dwarf2_generate_line_section(yasm_section *sect, /*@null@*/ void *d) state.precbc = NULL; if (info->asm_source) { - dwarf2_line_bc_info bcinfo; - - bcinfo.debug_line = info->debug_line; - bcinfo.object = info->object; - bcinfo.linemap = info->linemap; - bcinfo.dbgfmt_dwarf2 = dbgfmt_dwarf2; - bcinfo.state = &state; - bcinfo.lastfile = 0; - bcinfo.loc.isa_change = 0; - bcinfo.loc.column = 0; - bcinfo.loc.is_stmt = IS_STMT_NOCHANGE; - bcinfo.loc.basic_block = 0; - bcinfo.loc.prologue_end = 0; - bcinfo.loc.epilogue_begin = 0; - bcinfo.loc.sym = NULL; - - /* bcs_traverse() skips first "dummy" bytecode, so look at it - * separately to determine the initial symrec. - */ - bc = yasm_section_bcs_first(sect); - if (bc->symrecs) { - size_t i = 0; - while (bc->symrecs[i]) { - bcinfo.loc.sym = bc->symrecs[i]; - i++; - } - } - - yasm_section_bcs_traverse(sect, info->errwarns, &bcinfo, - dwarf2_generate_line_bc); + dwarf2_line_bc_info bcinfo; + + bcinfo.debug_line = info->debug_line; + bcinfo.object = info->object; + bcinfo.linemap = info->linemap; + bcinfo.dbgfmt_dwarf2 = dbgfmt_dwarf2; + bcinfo.state = &state; + bcinfo.lastfile = 0; + bcinfo.loc.isa_change = 0; + bcinfo.loc.column = 0; + bcinfo.loc.is_stmt = IS_STMT_NOCHANGE; + bcinfo.loc.basic_block = 0; + bcinfo.loc.prologue_end = 0; + bcinfo.loc.epilogue_begin = 0; + bcinfo.loc.sym = NULL; + + /* bcs_traverse() skips first "dummy" bytecode, so look at it + * separately to determine the initial symrec. + */ + bc = yasm_section_bcs_first(sect); + if (bc->symrecs) { + size_t i = 0; + while (bc->symrecs[i]) { + bcinfo.loc.sym = bc->symrecs[i]; + i++; + } + } + + yasm_section_bcs_traverse(sect, info->errwarns, &bcinfo, + dwarf2_generate_line_bc); } else { - /*@null@*/ dwarf2_loc *loc; + /*@null@*/ dwarf2_loc *loc; - dwarf2_dbgfmt_finalize_locs(sect, dsd); + dwarf2_dbgfmt_finalize_locs(sect, dsd); - STAILQ_FOREACH(loc, &dsd->locs, link) { - if (dwarf2_dbgfmt_gen_line_op(info->debug_line, &state, loc, - STAILQ_NEXT(loc, link))) - return 1; - } + STAILQ_FOREACH(loc, &dsd->locs, link) { + if (dwarf2_dbgfmt_gen_line_op(info->debug_line, &state, loc, + STAILQ_NEXT(loc, link))) + return 1; + } } /* End sequence: bring address to end of section, then output end @@ -598,17 +598,17 @@ dwarf2_generate_line_section(yasm_section *sect, /*@null@*/ void *d) * want an extra entry in the line matrix. */ if (!state.precbc) - state.precbc = yasm_section_bcs_first(sect); + state.precbc = yasm_section_bcs_first(sect); bc = yasm_section_bcs_last(sect); addr_delta = yasm_bc_next_offset(bc) - state.precbc->offset; if (addr_delta == DWARF2_MAX_SPECIAL_ADDR_DELTA) - dwarf2_dbgfmt_append_line_op(info->debug_line, DW_LNS_const_add_pc, - NULL); + dwarf2_dbgfmt_append_line_op(info->debug_line, DW_LNS_const_add_pc, + NULL); else if (addr_delta > 0) - dwarf2_dbgfmt_append_line_op(info->debug_line, DW_LNS_advance_pc, - yasm_intnum_create_uint(addr_delta)); + dwarf2_dbgfmt_append_line_op(info->debug_line, DW_LNS_advance_pc, + yasm_intnum_create_uint(addr_delta)); dwarf2_dbgfmt_append_line_ext_op(info->debug_line, DW_LNE_end_sequence, 0, - NULL); + NULL); return 0; } @@ -623,9 +623,9 @@ dwarf2_generate_filename(const char *filename, void *d) yasm_section * yasm_dwarf2__generate_line(yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns, int asm_source, - /*@out@*/ yasm_section **main_code, - /*@out@*/ size_t *num_line_sections) + yasm_errwarns *errwarns, int asm_source, + /*@out@*/ yasm_section **main_code, + /*@out@*/ size_t *num_line_sections) { yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt; dwarf2_line_info info; @@ -636,9 +636,9 @@ yasm_dwarf2__generate_line(yasm_object *object, yasm_linemap *linemap, dwarf2_head *head; if (asm_source) { - /* Generate dirs and filenames based on linemap */ - yasm_linemap_traverse_filenames(linemap, dbgfmt_dwarf2, - dwarf2_generate_filename); + /* Generate dirs and filenames based on linemap */ + yasm_linemap_traverse_filenames(linemap, dbgfmt_dwarf2, + dwarf2_generate_filename); } info.num_sections = 0; @@ -648,7 +648,7 @@ yasm_dwarf2__generate_line(yasm_object *object, yasm_linemap *linemap, info.linemap = linemap; info.dbgfmt_dwarf2 = dbgfmt_dwarf2; info.debug_line = yasm_object_get_general(object, ".debug_line", 0, 1, 0, - 0, &new, 0); + 0, &new, 0); last = yasm_section_bcs_last(info.debug_line); /* header */ @@ -658,40 +658,40 @@ yasm_dwarf2__generate_line(yasm_object *object, yasm_linemap *linemap, spp = yasm_xmalloc(sizeof(dwarf2_spp)); sppbc = yasm_bc_create_common(&dwarf2_spp_bc_callback, spp, 0); sppbc->len = dbgfmt_dwarf2->sizeof_offset + 5 + - NELEMS(line_opcode_num_operands); + NELEMS(line_opcode_num_operands); /* directory list */ for (i=0; idirs_size; i++) - sppbc->len += (unsigned long)strlen(dbgfmt_dwarf2->dirs[i])+1; + sppbc->len += (unsigned long)strlen(dbgfmt_dwarf2->dirs[i])+1; sppbc->len++; /* filename list */ for (i=0; ifilenames_size; i++) { - if (!dbgfmt_dwarf2->filenames[i].filename) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("dwarf2 file number %d unassigned"), i+1); - yasm_errwarn_propagate(errwarns, 0); - continue; - } - sppbc->len += - (unsigned long)strlen(dbgfmt_dwarf2->filenames[i].filename) + 1 + - yasm_size_uleb128(dbgfmt_dwarf2->filenames[i].dir) + 2; + if (!dbgfmt_dwarf2->filenames[i].filename) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("dwarf2 file number %d unassigned"), i+1); + yasm_errwarn_propagate(errwarns, 0); + continue; + } + sppbc->len += + (unsigned long)strlen(dbgfmt_dwarf2->filenames[i].filename) + 1 + + yasm_size_uleb128(dbgfmt_dwarf2->filenames[i].dir) + 2; } sppbc->len++; yasm_dwarf2__append_bc(info.debug_line, sppbc); /* statement program */ yasm_object_sections_traverse(object, (void *)&info, - dwarf2_generate_line_section); + dwarf2_generate_line_section); /* mark end of line information */ yasm_dwarf2__set_head_end(head, yasm_section_bcs_last(info.debug_line)); *num_line_sections = info.num_sections; if (info.num_sections == 1) - *main_code = info.last_code; + *main_code = info.last_code; else - *main_code = NULL; + *main_code = NULL; return info.debug_line; } @@ -709,7 +709,7 @@ dwarf2_spp_bc_print(const void *contents, FILE *f, int indent_level) static int dwarf2_spp_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a dwarf2 spp bytecode")); /*@notreached@*/ @@ -718,8 +718,8 @@ dwarf2_spp_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int dwarf2_spp_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { yasm_object *object = yasm_section_get_object(bc->section); yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt; @@ -729,41 +729,41 @@ dwarf2_spp_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Prologue length (following this field) */ cval = yasm_intnum_create_uint(bc->len - (unsigned long)(buf-*bufp) - - dbgfmt_dwarf2->sizeof_offset); + dbgfmt_dwarf2->sizeof_offset); yasm_arch_intnum_tobytes(object->arch, cval, buf, - dbgfmt_dwarf2->sizeof_offset, - dbgfmt_dwarf2->sizeof_offset*8, 0, bc, 0); + dbgfmt_dwarf2->sizeof_offset, + dbgfmt_dwarf2->sizeof_offset*8, 0, bc, 0); buf += dbgfmt_dwarf2->sizeof_offset; - YASM_WRITE_8(buf, dbgfmt_dwarf2->min_insn_len); /* minimum_instr_len */ - YASM_WRITE_8(buf, DWARF2_LINE_DEFAULT_IS_STMT); /* default_is_stmt */ - YASM_WRITE_8(buf, DWARF2_LINE_BASE); /* line_base */ - YASM_WRITE_8(buf, DWARF2_LINE_RANGE); /* line_range */ - YASM_WRITE_8(buf, DWARF2_LINE_OPCODE_BASE); /* opcode_base */ + YASM_WRITE_8(buf, dbgfmt_dwarf2->min_insn_len); /* minimum_instr_len */ + YASM_WRITE_8(buf, DWARF2_LINE_DEFAULT_IS_STMT); /* default_is_stmt */ + YASM_WRITE_8(buf, DWARF2_LINE_BASE); /* line_base */ + YASM_WRITE_8(buf, DWARF2_LINE_RANGE); /* line_range */ + YASM_WRITE_8(buf, DWARF2_LINE_OPCODE_BASE); /* opcode_base */ /* Standard opcode # operands array */ for (i=0; idirs_size; i++) { - len = strlen(dbgfmt_dwarf2->dirs[i])+1; - memcpy(buf, dbgfmt_dwarf2->dirs[i], len); - buf += len; + len = strlen(dbgfmt_dwarf2->dirs[i])+1; + memcpy(buf, dbgfmt_dwarf2->dirs[i], len); + buf += len; } /* finish with single 0 byte */ YASM_WRITE_8(buf, 0); /* filename list */ for (i=0; ifilenames_size; i++) { - len = strlen(dbgfmt_dwarf2->filenames[i].filename)+1; - memcpy(buf, dbgfmt_dwarf2->filenames[i].filename, len); - buf += len; + len = strlen(dbgfmt_dwarf2->filenames[i].filename)+1; + memcpy(buf, dbgfmt_dwarf2->filenames[i].filename, len); + buf += len; - /* dir */ - buf += yasm_get_uleb128(dbgfmt_dwarf2->filenames[i].dir, buf); - YASM_WRITE_8(buf, 0); /* time */ - YASM_WRITE_8(buf, 0); /* length */ + /* dir */ + buf += yasm_get_uleb128(dbgfmt_dwarf2->filenames[i].dir, buf); + YASM_WRITE_8(buf, 0); /* time */ + YASM_WRITE_8(buf, 0); /* length */ } /* finish with single 0 byte */ YASM_WRITE_8(buf, 0); @@ -779,7 +779,7 @@ dwarf2_line_op_bc_destroy(void *contents) { dwarf2_line_op *line_op = (dwarf2_line_op *)contents; if (line_op->operand) - yasm_intnum_destroy(line_op->operand); + yasm_intnum_destroy(line_op->operand); yasm_xfree(contents); } @@ -791,7 +791,7 @@ dwarf2_line_op_bc_print(const void *contents, FILE *f, int indent_level) static int dwarf2_line_op_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to calc_len a dwarf2 line_op bytecode")); /*@notreached@*/ @@ -800,26 +800,26 @@ dwarf2_line_op_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int dwarf2_line_op_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { dwarf2_line_op *line_op = (dwarf2_line_op *)bc->contents; unsigned char *buf = *bufp; YASM_WRITE_8(buf, line_op->opcode); if (line_op->operand) - buf += yasm_intnum_get_leb128(line_op->operand, buf, - line_op->opcode == DW_LNS_advance_line); + buf += yasm_intnum_get_leb128(line_op->operand, buf, + line_op->opcode == DW_LNS_advance_line); if (line_op->ext_opcode > 0) { - YASM_WRITE_8(buf, line_op->ext_opcode); - if (line_op->ext_operand) { - yasm_value value; - yasm_value_init_sym(&value, line_op->ext_operand, - line_op->ext_operandsize*8); - output_value(&value, buf, line_op->ext_operandsize, - (unsigned long)(buf-*bufp), bc, 0, d); - buf += line_op->ext_operandsize; - } + YASM_WRITE_8(buf, line_op->ext_opcode); + if (line_op->ext_operand) { + yasm_value value; + yasm_value_init_sym(&value, line_op->ext_operand, + line_op->ext_operandsize*8); + output_value(&value, buf, line_op->ext_operandsize, + (unsigned long)(buf-*bufp), bc, 0, d); + buf += line_op->ext_operandsize; + } } *bufp = buf; @@ -828,7 +828,7 @@ dwarf2_line_op_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, void yasm_dwarf2__dir_loc(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp; @@ -838,48 +838,48 @@ yasm_dwarf2__dir_loc(yasm_object *object, yasm_valparamhead *valparams, /* File number (required) */ if (!valparams || !(vp = yasm_vps_first(valparams)) || !vp->param) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("file number required")); - yasm_xfree(loc); - return; + yasm_error_set(YASM_ERROR_SYNTAX, N_("file number required")); + yasm_xfree(loc); + return; } intn = yasm_expr_get_intnum(&vp->param, 0); if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("file number is not a constant")); - yasm_xfree(loc); - return; + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("file number is not a constant")); + yasm_xfree(loc); + return; } if (yasm_intnum_sign(intn) != 1) { - yasm_error_set(YASM_ERROR_VALUE, N_("file number less than one")); - yasm_xfree(loc); - return; + yasm_error_set(YASM_ERROR_VALUE, N_("file number less than one")); + yasm_xfree(loc); + return; } loc->file = yasm_intnum_get_uint(intn); /* Line number (required) */ vp = yasm_vps_next(vp); if (!vp || !vp->param) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("line number required")); - yasm_xfree(loc); - return; + yasm_error_set(YASM_ERROR_SYNTAX, N_("line number required")); + yasm_xfree(loc); + return; } intn = yasm_expr_get_intnum(&vp->param, 0); if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("file number is not a constant")); - yasm_xfree(loc); - return; + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("file number is not a constant")); + yasm_xfree(loc); + return; } loc->line = yasm_intnum_get_uint(intn); /* Generate new section data if it doesn't already exist */ dsd = yasm_section_get_data(object->cur_section, - &yasm_dwarf2__section_data_cb); + &yasm_dwarf2__section_data_cb); if (!dsd) { - dsd = yasm_xmalloc(sizeof(dwarf2_section_data)); - STAILQ_INIT(&dsd->locs); - yasm_section_add_data(object->cur_section, - &yasm_dwarf2__section_data_cb, dsd); + dsd = yasm_xmalloc(sizeof(dwarf2_section_data)); + STAILQ_INIT(&dsd->locs); + yasm_section_add_data(object->cur_section, + &yasm_dwarf2__section_data_cb, dsd); } /* Defaults for optional settings */ @@ -894,73 +894,73 @@ yasm_dwarf2__dir_loc(yasm_object *object, yasm_valparamhead *valparams, /* Optional column number */ vp = yasm_vps_next(vp); if (vp && vp->param) { - intn = yasm_expr_get_intnum(&vp->param, 0); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("column number is not a constant")); - yasm_xfree(loc); - return; - } - loc->column = yasm_intnum_get_uint(intn); - vp = yasm_vps_next(vp); + intn = yasm_expr_get_intnum(&vp->param, 0); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("column number is not a constant")); + yasm_xfree(loc); + return; + } + loc->column = yasm_intnum_get_uint(intn); + vp = yasm_vps_next(vp); } /* Other options */ while (vp && vp->val) { - if (yasm__strcasecmp(vp->val, "basic_block") == 0) - loc->basic_block = 1; - else if (yasm__strcasecmp(vp->val, "prologue_end") == 0) - loc->prologue_end = 1; - else if (yasm__strcasecmp(vp->val, "epilogue_begin") == 0) - loc->epilogue_begin = 1; - else if (yasm__strcasecmp(vp->val, "is_stmt") == 0) { - if (!vp->param) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("is_stmt requires value")); - yasm_xfree(loc); - return; - } - intn = yasm_expr_get_intnum(&vp->param, 0); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("is_stmt value is not a constant")); - yasm_xfree(loc); - return; - } - if (yasm_intnum_is_zero(intn)) - loc->is_stmt = IS_STMT_SET; - else if (yasm_intnum_is_pos1(intn)) - loc->is_stmt = IS_STMT_CLEAR; - else { - yasm_error_set(YASM_ERROR_VALUE, - N_("is_stmt value not 0 or 1")); - yasm_xfree(loc); - return; - } - } else if (yasm__strcasecmp(vp->val, "isa") == 0) { - if (!vp->param) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("isa requires value")); - yasm_xfree(loc); - return; - } - intn = yasm_expr_get_intnum(&vp->param, 0); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("isa value is not a constant")); - yasm_xfree(loc); - return; - } - if (yasm_intnum_sign(intn) < 0) { - yasm_error_set(YASM_ERROR_VALUE, - N_("isa value less than zero")); - yasm_xfree(loc); - return; - } - loc->isa_change = 1; - loc->isa = yasm_intnum_get_uint(intn); - } else - yasm_warn_set(YASM_WARN_GENERAL, - N_("unrecognized loc option `%s'"), vp->val); + if (yasm__strcasecmp(vp->val, "basic_block") == 0) + loc->basic_block = 1; + else if (yasm__strcasecmp(vp->val, "prologue_end") == 0) + loc->prologue_end = 1; + else if (yasm__strcasecmp(vp->val, "epilogue_begin") == 0) + loc->epilogue_begin = 1; + else if (yasm__strcasecmp(vp->val, "is_stmt") == 0) { + if (!vp->param) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("is_stmt requires value")); + yasm_xfree(loc); + return; + } + intn = yasm_expr_get_intnum(&vp->param, 0); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("is_stmt value is not a constant")); + yasm_xfree(loc); + return; + } + if (yasm_intnum_is_zero(intn)) + loc->is_stmt = IS_STMT_SET; + else if (yasm_intnum_is_pos1(intn)) + loc->is_stmt = IS_STMT_CLEAR; + else { + yasm_error_set(YASM_ERROR_VALUE, + N_("is_stmt value not 0 or 1")); + yasm_xfree(loc); + return; + } + } else if (yasm__strcasecmp(vp->val, "isa") == 0) { + if (!vp->param) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("isa requires value")); + yasm_xfree(loc); + return; + } + intn = yasm_expr_get_intnum(&vp->param, 0); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("isa value is not a constant")); + yasm_xfree(loc); + return; + } + if (yasm_intnum_sign(intn) < 0) { + yasm_error_set(YASM_ERROR_VALUE, + N_("isa value less than zero")); + yasm_xfree(loc); + return; + } + loc->isa_change = 1; + loc->isa = yasm_intnum_get_uint(intn); + } else + yasm_warn_set(YASM_WARN_GENERAL, + N_("unrecognized loc option `%s'"), vp->val); } /* Append new location */ @@ -972,7 +972,7 @@ yasm_dwarf2__dir_loc(yasm_object *object, yasm_valparamhead *valparams, void yasm_dwarf2__dir_file(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_dbgfmt_dwarf2 *dbgfmt_dwarf2 = (yasm_dbgfmt_dwarf2 *)object->dbgfmt; yasm_valparam *vp; @@ -980,32 +980,32 @@ yasm_dwarf2__dir_file(yasm_object *object, yasm_valparamhead *valparams, unsigned long filenum; if (!valparams) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] requires an argument"), - "FILE"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] requires an argument"), + "FILE"); + return; } vp = yasm_vps_first(valparams); if (vp->val) { - /* Just a bare filename */ - yasm_object_set_source_fn(object, vp->val); - return; + /* Just a bare filename */ + yasm_object_set_source_fn(object, vp->val); + return; } /* Otherwise.. first vp is the file number */ file_intn = yasm_expr_get_intnum(&vp->param, 0); if (!file_intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("file number is not a constant")); - return; + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("file number is not a constant")); + return; } filenum = yasm_intnum_get_uint(file_intn); vp = yasm_vps_next(vp); if (!vp || !vp->val) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("file number given but no filename")); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("file number given but no filename")); + return; } dwarf2_dbgfmt_add_file(dbgfmt_dwarf2, filenum, vp->val); diff --git a/modules/dbgfmts/null/null-dbgfmt.c b/modules/dbgfmts/null/null-dbgfmt.c index 44766934..1afa1bb6 100644 --- a/modules/dbgfmts/null/null-dbgfmt.c +++ b/modules/dbgfmts/null/null-dbgfmt.c @@ -50,7 +50,7 @@ null_dbgfmt_destroy(/*@only@*/ yasm_dbgfmt *dbgfmt) static void null_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { } @@ -59,7 +59,7 @@ null_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, yasm_dbgfmt_module yasm_null_LTX_dbgfmt = { "No debugging info", "null", - NULL, /* no directives */ + NULL, /* no directives */ null_dbgfmt_create, null_dbgfmt_destroy, null_dbgfmt_generate diff --git a/modules/dbgfmts/stabs/stabs-dbgfmt.c b/modules/dbgfmts/stabs/stabs-dbgfmt.c index 1942d4b1..a3000c28 100644 --- a/modules/dbgfmts/stabs/stabs-dbgfmt.c +++ b/modules/dbgfmts/stabs/stabs-dbgfmt.c @@ -32,68 +32,68 @@ #include typedef enum { - N_UNDF = 0x00, /* Undefined */ - N_GSYM = 0x20, /* Global symbol */ - N_FNAME = 0x22, /* Function name (BSD Fortran) */ - N_FUN = 0x24, /* Function name or Text segment variable */ - N_STSYM = 0x26, /* Data segment file-scope variable */ - N_LCSYM = 0x28, /* BSS segment file-scope variable */ - N_MAIN = 0x2a, /* Name of main routine */ - N_ROSYM = 0x2c, /* Variable in .rodata section */ - N_PC = 0x30, /* Global symbol (Pascal) */ - N_SYMS = 0x32, /* Number of symbols (Ultrix V4.0) */ - N_NOMAP = 0x34, /* No DST map */ - N_OBJ = 0x38, /* Object file (Solaris2) */ - N_OPT = 0x3c, /* Debugger options (Solaris2) */ - N_RSYM = 0x40, /* Register variable */ - N_M2C = 0x42, /* Modula-2 compilation unit */ - N_SLINE = 0x44, /* Line numbers in .text segment */ - N_DSLINE = 0x46, /* Line numbers in .data segment */ - N_BSLINE = 0x48, /* Line numbers in .bss segment */ - N_BROWS = 0x48, /* Source code .cb file's path */ - N_DEFD = 0x4a, /* GNU Modula-2 definition module dependency */ - N_FLINE = 0x4c, /* Function start/body/end line numbers (Solaris2) */ - N_EHDECL = 0x50, /* GNU C++ exception variable */ - N_MOD2 = 0x50, /* Modula2 info for imc (Ultrix V4.0) */ - N_CATCH = 0x54, /* GNU C++ catch clause */ - N_SSYM = 0x60, /* Structure or union element */ - N_ENDM = 0x62, /* Last stab for module (Solaris2) */ - N_SO = 0x64, /* Path and name of source files */ - N_LSYM = 0x80, /* Stack variable */ - N_BINCL = 0x84, /* Beginning of include file */ - N_SOL = 0x84, /* Name of include file */ - N_PSYM = 0xa0, /* Parameter variable */ - N_EINCL = 0xa2, /* End of include file */ - N_ENTRY = 0xa4, /* Alternate entry point */ - N_LBRAC = 0xc0, /* Beginning of lexical block */ - N_EXCL = 0xc2, /* Placeholder for a deleted include file */ - N_SCOPE = 0xc4, /* Modula 2 scope info (Sun) */ - N_RBRAC = 0xe0, /* End of lexical block */ - N_BCOMM = 0xe2, /* Begin named common block */ - N_ECOMM = 0xe4, /* End named common block */ - N_ECOML = 0xe8, /* Member of common block */ - N_WITH = 0xea, /* Pascal with statement: type,,0,0,offset (Solaris2) */ - N_NBTEXT = 0xf0, /* Gould non-base registers */ - N_NBDATA = 0xf2, /* Gould non-base registers */ - N_NBBSS = 0xf4, /* Gould non-base registers */ - N_NBSTS = 0xf6, /* Gould non-base registers */ - N_NBLCS = 0xf8 /* Gould non-base registers */ + N_UNDF = 0x00, /* Undefined */ + N_GSYM = 0x20, /* Global symbol */ + N_FNAME = 0x22, /* Function name (BSD Fortran) */ + N_FUN = 0x24, /* Function name or Text segment variable */ + N_STSYM = 0x26, /* Data segment file-scope variable */ + N_LCSYM = 0x28, /* BSS segment file-scope variable */ + N_MAIN = 0x2a, /* Name of main routine */ + N_ROSYM = 0x2c, /* Variable in .rodata section */ + N_PC = 0x30, /* Global symbol (Pascal) */ + N_SYMS = 0x32, /* Number of symbols (Ultrix V4.0) */ + N_NOMAP = 0x34, /* No DST map */ + N_OBJ = 0x38, /* Object file (Solaris2) */ + N_OPT = 0x3c, /* Debugger options (Solaris2) */ + N_RSYM = 0x40, /* Register variable */ + N_M2C = 0x42, /* Modula-2 compilation unit */ + N_SLINE = 0x44, /* Line numbers in .text segment */ + N_DSLINE = 0x46, /* Line numbers in .data segment */ + N_BSLINE = 0x48, /* Line numbers in .bss segment */ + N_BROWS = 0x48, /* Source code .cb file's path */ + N_DEFD = 0x4a, /* GNU Modula-2 definition module dependency */ + N_FLINE = 0x4c, /* Function start/body/end line numbers (Solaris2) */ + N_EHDECL = 0x50, /* GNU C++ exception variable */ + N_MOD2 = 0x50, /* Modula2 info for imc (Ultrix V4.0) */ + N_CATCH = 0x54, /* GNU C++ catch clause */ + N_SSYM = 0x60, /* Structure or union element */ + N_ENDM = 0x62, /* Last stab for module (Solaris2) */ + N_SO = 0x64, /* Path and name of source files */ + N_LSYM = 0x80, /* Stack variable */ + N_BINCL = 0x84, /* Beginning of include file */ + N_SOL = 0x84, /* Name of include file */ + N_PSYM = 0xa0, /* Parameter variable */ + N_EINCL = 0xa2, /* End of include file */ + N_ENTRY = 0xa4, /* Alternate entry point */ + N_LBRAC = 0xc0, /* Beginning of lexical block */ + N_EXCL = 0xc2, /* Placeholder for a deleted include file */ + N_SCOPE = 0xc4, /* Modula 2 scope info (Sun) */ + N_RBRAC = 0xe0, /* End of lexical block */ + N_BCOMM = 0xe2, /* Begin named common block */ + N_ECOMM = 0xe4, /* End named common block */ + N_ECOML = 0xe8, /* Member of common block */ + N_WITH = 0xea, /* Pascal with statement: type,,0,0,offset (Solaris2) */ + N_NBTEXT = 0xf0, /* Gould non-base registers */ + N_NBDATA = 0xf2, /* Gould non-base registers */ + N_NBBSS = 0xf4, /* Gould non-base registers */ + N_NBSTS = 0xf6, /* Gould non-base registers */ + N_NBLCS = 0xf8 /* Gould non-base registers */ } stabs_stab_type; typedef struct yasm_dbgfmt_stabs { - yasm_dbgfmt_base dbgfmt; /* base structure */ + yasm_dbgfmt_base dbgfmt; /* base structure */ } yasm_dbgfmt_stabs; typedef struct { - unsigned long lastline; /* track line and file of bytecodes */ + unsigned long lastline; /* track line and file of bytecodes */ unsigned long curline; const char *lastfile; const char *curfile; - unsigned int stablen; /* size of a stab for current machine */ - unsigned long stabcount; /* count stored stabs; doesn't include first */ + unsigned int stablen; /* size of a stab for current machine */ + unsigned long stabcount; /* count stored stabs; doesn't include first */ - yasm_section *stab; /* sections to which stabs, stabstrs appended */ + yasm_section *stab; /* sections to which stabs, stabstrs appended */ yasm_section *stabstr; yasm_bytecode *basebc; /* base bytecode from which to track SLINEs */ @@ -104,20 +104,20 @@ typedef struct { } stabs_info; typedef struct { - /*@null@*/ yasm_bytecode *bcstr; /* bytecode in stabstr for string */ - stabs_stab_type type; /* stab type: N_* */ - unsigned char other; /* unused, but stored here anyway */ - unsigned short desc; /* description element of a stab */ - /*@null@*/ yasm_symrec *symvalue; /* value element needing relocation */ - /*@null@*/yasm_bytecode *bcvalue; /* relocated stab's bytecode */ - unsigned long value; /* fallthrough value if above NULL */ + /*@null@*/ yasm_bytecode *bcstr; /* bytecode in stabstr for string */ + stabs_stab_type type; /* stab type: N_* */ + unsigned char other; /* unused, but stored here anyway */ + unsigned short desc; /* description element of a stab */ + /*@null@*/ yasm_symrec *symvalue; /* value element needing relocation */ + /*@null@*/yasm_bytecode *bcvalue; /* relocated stab's bytecode */ + unsigned long value; /* fallthrough value if above NULL */ } stabs_stab; /* Bytecode callback function prototypes */ static void stabs_bc_str_destroy(void *contents); static void stabs_bc_str_print(const void *contents, FILE *f, int - indent_level); + indent_level); static int stabs_bc_str_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int stabs_bc_str_tobytes @@ -127,7 +127,7 @@ static int stabs_bc_str_tobytes static void stabs_bc_stab_destroy(void *contents); static void stabs_bc_stab_print(const void *contents, FILE *f, int - indent_level); + indent_level); static int stabs_bc_stab_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int stabs_bc_stab_tobytes @@ -196,9 +196,9 @@ stabs_dbgfmt_append_bcstr(yasm_section *sect, const char *str) /* Requires a string bytecode, or NULL, for its string entry */ static stabs_stab * stabs_dbgfmt_append_stab(stabs_info *info, yasm_section *sect, - /*@null@*/ yasm_bytecode *bcstr, stabs_stab_type type, - unsigned long desc, /*@null@*/ yasm_symrec *symvalue, - /*@null@*/ yasm_bytecode *bcvalue, unsigned long value) + /*@null@*/ yasm_bytecode *bcstr, stabs_stab_type type, + unsigned long desc, /*@null@*/ yasm_symrec *symvalue, + /*@null@*/ yasm_bytecode *bcvalue, unsigned long value) { yasm_bytecode *bc; stabs_stab *stab = yasm_xmalloc(sizeof(stabs_stab)); @@ -212,7 +212,7 @@ stabs_dbgfmt_append_stab(stabs_info *info, yasm_section *sect, stab->value = value; bc = yasm_bc_create_common(&stabs_bc_stab_callback, stab, - bcvalue ? bcvalue->line : 0); + bcvalue ? bcvalue->line : 0); bc->len = info->stablen; bc->offset = yasm_bc_next_offset(yasm_section_bcs_last(sect)); @@ -243,10 +243,10 @@ stabs_dbgfmt_generate_n_fun(stabs_info *info, yasm_bytecode *bc) str = yasm_xmalloc(strlen(name)+4); strcpy(str, name); strcat(str, ":F1"); - stabs_dbgfmt_append_stab(info, info->stab, - stabs_dbgfmt_append_bcstr(info->stabstr, str), - N_FUN, 0, sym, info->basebc, 0); - yasm_xfree(str); + stabs_dbgfmt_append_stab(info, info->stab, + stabs_dbgfmt_append_bcstr(info->stabstr, str), + N_FUN, 0, sym, info->basebc, 0); + yasm_xfree(str); break; } } @@ -256,24 +256,24 @@ stabs_dbgfmt_generate_bcs(yasm_bytecode *bc, void *d) { stabs_info *info = (stabs_info *)d; yasm_linemap_lookup(info->linemap, bc->line, &info->curfile, - &info->curline); + &info->curline); /* check for new function */ stabs_dbgfmt_generate_n_fun(info, bc); if (info->lastfile != info->curfile) { - info->lastline = 0; /* new file, so line changes */ - /*stabs_dbgfmt_append_stab(info, info->stab, - stabs_dbgfmt_append_bcstr(info->stabstr, info->curfile), - N_SOL, 0, NULL, bc, 0);*/ + info->lastline = 0; /* new file, so line changes */ + /*stabs_dbgfmt_append_stab(info, info->stab, + stabs_dbgfmt_append_bcstr(info->stabstr, info->curfile), + N_SOL, 0, NULL, bc, 0);*/ } /* output new line stabs if there's a basebc (known function) */ if (info->basebc != NULL && info->curline != info->lastline) { - info->lastline = bc->line; - stabs_dbgfmt_append_stab(info, info->stab, NULL, N_SLINE, - info->curline, NULL, NULL, - bc->offset - info->basebc->offset); + info->lastline = bc->line; + stabs_dbgfmt_append_stab(info, info->stab, NULL, N_SLINE, + info->curline, NULL, NULL, + bc->offset - info->basebc->offset); } info->lastline = info->curline; @@ -295,15 +295,15 @@ stabs_dbgfmt_generate_sections(yasm_section *sect, /*@null@*/ void *d) stabs_dbgfmt_generate_n_fun(d, yasm_section_bcs_first(sect)); yasm_section_bcs_traverse(sect, info->errwarns, d, - stabs_dbgfmt_generate_bcs); + stabs_dbgfmt_generate_bcs); if (yasm__strcasecmp(sectname, ".text")==0) { /* Close out last function by appending a null SO stab after last bc */ yasm_bytecode *bc = yasm_section_bcs_last(sect); yasm_symrec *sym = - yasm_symtab_define_label(info->object->symtab, ".n_so", bc, 1, - bc->line); - stabs_dbgfmt_append_stab(info, info->stab, 0, N_SO, 0, sym, bc, 0); + yasm_symtab_define_label(info->object->symtab, ".n_so", bc, 1, + bc->line); + stabs_dbgfmt_append_stab(info, info->stab, 0, N_SO, 0, sym, bc, 0); } return 1; @@ -311,7 +311,7 @@ stabs_dbgfmt_generate_sections(yasm_section *sect, /*@null@*/ void *d) static void stabs_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { stabs_info info; int new; @@ -326,7 +326,7 @@ stabs_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, info.stablen = 12; } else /* unknown machine; generate nothing */ - return; + return; info.object = object; info.linemap = linemap; @@ -335,33 +335,33 @@ stabs_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, info.stabcount = 0; info.stab = yasm_object_get_general(object, ".stab", 0, 4, 0, 0, &new, 0); if (!new) { - yasm_bytecode *last = yasm_section_bcs_last(info.stab); - if (last == NULL) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("stabs debugging conflicts with user-defined section .stab")); - yasm_errwarn_propagate(errwarns, - yasm_section_bcs_first(info.stab)->line); - } else { - yasm_warn_set(YASM_WARN_GENERAL, - N_("stabs debugging overrides empty section .stab")); - yasm_errwarn_propagate(errwarns, 0); - } + yasm_bytecode *last = yasm_section_bcs_last(info.stab); + if (last == NULL) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("stabs debugging conflicts with user-defined section .stab")); + yasm_errwarn_propagate(errwarns, + yasm_section_bcs_first(info.stab)->line); + } else { + yasm_warn_set(YASM_WARN_GENERAL, + N_("stabs debugging overrides empty section .stab")); + yasm_errwarn_propagate(errwarns, 0); + } } info.stabstr = - yasm_object_get_general(object, ".stabstr", 0, 1, 0, 0, &new, 0); + yasm_object_get_general(object, ".stabstr", 0, 1, 0, 0, &new, 0); if (!new) { - yasm_bytecode *last = yasm_section_bcs_last(info.stabstr); - if (last == NULL) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("stabs debugging conflicts with user-defined section .stabstr")); - yasm_errwarn_propagate(errwarns, - yasm_section_bcs_first(info.stab)->line); - } else { - yasm_warn_set(YASM_WARN_GENERAL, - N_("stabs debugging overrides empty section .stabstr")); - yasm_errwarn_propagate(errwarns, 0); - } + yasm_bytecode *last = yasm_section_bcs_last(info.stabstr); + if (last == NULL) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("stabs debugging conflicts with user-defined section .stabstr")); + yasm_errwarn_propagate(errwarns, + yasm_section_bcs_first(info.stab)->line); + } else { + yasm_warn_set(YASM_WARN_GENERAL, + N_("stabs debugging overrides empty section .stabstr")); + yasm_errwarn_propagate(errwarns, 0); + } } /* initial pseudo-stab */ @@ -380,15 +380,15 @@ stabs_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, firstbc = yasm_section_bcs_first(stext); /* N_SO file stab */ stabs_dbgfmt_append_stab(&info, info.stab, filebc, N_SO, 0, - firstsym, firstbc, 0); + firstsym, firstbc, 0); yasm_object_sections_traverse(object, (void *)&info, - stabs_dbgfmt_generate_sections); + stabs_dbgfmt_generate_sections); /* fill initial pseudo-stab's fields */ laststr = yasm_section_bcs_last(info.stabstr); if (laststr == NULL) - yasm_internal_error(".stabstr has no entries"); + yasm_internal_error(".stabstr has no entries"); stab->bcvalue = NULL; stab->symvalue = NULL; @@ -397,17 +397,17 @@ stabs_dbgfmt_generate(yasm_object *object, yasm_linemap *linemap, stab->type = N_UNDF; stab->other = 0; if (info.stabcount > 0xffff) { - yasm_warn_set(YASM_WARN_GENERAL, N_("over 65535 stabs")); - yasm_errwarn_propagate(errwarns, 0); - stab->desc = 0xffff; + yasm_warn_set(YASM_WARN_GENERAL, N_("over 65535 stabs")); + yasm_errwarn_propagate(errwarns, 0); + stab->desc = 0xffff; } else - stab->desc = (unsigned short)info.stabcount; + stab->desc = (unsigned short)info.stabcount; } static int stabs_bc_stab_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { /* This entire function, essentially the core of rendering stabs to a file, * needs to become endian aware. Size appears not to be an issue, as known @@ -422,16 +422,16 @@ stabs_bc_stab_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, YASM_WRITE_16_L(buf, stab->desc); if (stab->symvalue != NULL) { - bc->offset += 8; - output_reloc(stab->symvalue, bc, buf, 4, 32, 0, d); - bc->offset -= 8; - buf += 4; + bc->offset += 8; + output_reloc(stab->symvalue, bc, buf, 4, 32, 0, d); + bc->offset -= 8; + buf += 4; } else if (stab->bcvalue != NULL) { - YASM_WRITE_32_L(buf, stab->bcvalue->offset); + YASM_WRITE_32_L(buf, stab->bcvalue->offset); } else { - YASM_WRITE_32_L(buf, stab->value); + YASM_WRITE_32_L(buf, stab->value); } *bufp = buf; @@ -440,8 +440,8 @@ stabs_bc_stab_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, static int stabs_bc_str_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { const char *str = (const char *)bc->contents; unsigned char *buf = *bufp; @@ -471,8 +471,8 @@ stabs_bc_stab_print(const void *contents, FILE *f, int indent_level) const stabs_stab *stab = (const stabs_stab *)contents; const char *str = ""; fprintf(f, "%*s.stabs \"%s\", 0x%x, 0x%x, 0x%x, 0x%lx\n", - indent_level, "", str, stab->type, stab->other, stab->desc, - stab->bcvalue ? stab->bcvalue->offset : stab->value); + indent_level, "", str, stab->type, stab->other, stab->desc, + stab->bcvalue ? stab->bcvalue->offset : stab->value); } static void @@ -483,7 +483,7 @@ stabs_bc_str_print(const void *contents, FILE *f, int indent_level) static int stabs_bc_stab_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to resolve a stabs stab bytecode")); /*@notreached@*/ @@ -492,7 +492,7 @@ stabs_bc_stab_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int stabs_bc_str_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { yasm_internal_error(N_("tried to resolve a stabs str bytecode")); /*@notreached@*/ @@ -503,7 +503,7 @@ stabs_bc_str_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, yasm_dbgfmt_module yasm_stabs_LTX_dbgfmt = { "Stabs debugging format", "stabs", - NULL, /* no directives */ + NULL, /* no directives */ stabs_dbgfmt_create, stabs_dbgfmt_destroy, stabs_dbgfmt_generate diff --git a/modules/listfmts/nasm/nasm-listfmt.c b/modules/listfmts/nasm/nasm-listfmt.c index 0e0f24b2..42d3291c 100644 --- a/modules/listfmts/nasm/nasm-listfmt.c +++ b/modules/listfmts/nasm/nasm-listfmt.c @@ -47,21 +47,21 @@ yasm_listfmt_module yasm_nasm_LTX_listfmt; typedef struct sectreloc { /*@reldef@*/ SLIST_ENTRY(sectreloc) link; yasm_section *sect; - /*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */ + /*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */ unsigned long next_reloc_addr; } sectreloc; typedef struct bcreloc { /*@reldef@*/ STAILQ_ENTRY(bcreloc) link; - unsigned long offset; /* start of reloc from start of bytecode */ - size_t size; /* size of reloc in bytes */ - int rel; /* PC/IP-relative or "absolute" */ + unsigned long offset; /* start of reloc from start of bytecode */ + size_t size; /* size of reloc in bytes */ + int rel; /* PC/IP-relative or "absolute" */ } bcreloc; typedef struct nasm_listfmt_output_info { yasm_arch *arch; /*@reldef@*/ STAILQ_HEAD(bcrelochead, bcreloc) bcrelocs; - /*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */ + /*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */ unsigned long next_reloc_addr; } nasm_listfmt_output_info; @@ -82,8 +82,8 @@ nasm_listfmt_destroy(/*@only@*/ yasm_listfmt *listfmt) static int nasm_listfmt_output_value(yasm_value *value, unsigned char *buf, - unsigned int destsize, unsigned long offset, - yasm_bytecode *bc, int warn, /*@null@*/ void *d) + unsigned int destsize, unsigned long offset, + yasm_bytecode *bc, int warn, /*@null@*/ void *d) { /*@null@*/ nasm_listfmt_output_info *info = (nasm_listfmt_output_info *)d; /*@dependent@*/ /*@null@*/ yasm_intnum *intn; @@ -93,50 +93,50 @@ nasm_listfmt_output_value(yasm_value *value, unsigned char *buf, /* Output */ switch (yasm_value_output_basic(value, buf, destsize, bc, warn, - info->arch)) { - case -1: - return 1; - case 0: - break; - default: - return 0; + info->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; } /* Generate reloc if needed */ if (info->next_reloc && info->next_reloc_addr == bc->offset+offset) { - bcreloc *reloc = yasm_xmalloc(sizeof(bcreloc)); - reloc->offset = offset; - reloc->size = destsize; - reloc->rel = value->curpos_rel; - STAILQ_INSERT_TAIL(&info->bcrelocs, reloc, link); - - /* Get next reloc's info */ - info->next_reloc = yasm_section_reloc_next(info->next_reloc); - if (info->next_reloc) { - yasm_intnum *addr; - yasm_symrec *sym; - yasm_reloc_get(info->next_reloc, &addr, &sym); - info->next_reloc_addr = yasm_intnum_get_uint(addr); - } + bcreloc *reloc = yasm_xmalloc(sizeof(bcreloc)); + reloc->offset = offset; + reloc->size = destsize; + reloc->rel = value->curpos_rel; + STAILQ_INSERT_TAIL(&info->bcrelocs, reloc, link); + + /* Get next reloc's info */ + info->next_reloc = yasm_section_reloc_next(info->next_reloc); + if (info->next_reloc) { + yasm_intnum *addr; + yasm_symrec *sym; + yasm_reloc_get(info->next_reloc, &addr, &sym); + info->next_reloc_addr = yasm_intnum_get_uint(addr); + } } if (value->abs) { - intn = yasm_expr_get_intnum(&value->abs, 0); - if (intn) - return yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize, - valsize, 0, bc, 0); - else { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("relocation too complex")); - return 1; - } + intn = yasm_expr_get_intnum(&value->abs, 0); + if (intn) + return yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize, + valsize, 0, bc, 0); + else { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("relocation too complex")); + return 1; + } } else { - int retval; - intn = yasm_intnum_create_uint(0); - retval = yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize, - valsize, 0, bc, 0); - yasm_intnum_destroy(intn); - return retval; + int retval; + intn = yasm_intnum_create_uint(0); + retval = yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize, + valsize, 0, bc, 0); + yasm_intnum_destroy(intn); + return retval; } return 0; @@ -144,7 +144,7 @@ nasm_listfmt_output_value(yasm_value *value, unsigned char *buf, static void nasm_listfmt_output(yasm_listfmt *listfmt, FILE *f, yasm_linemap *linemap, - yasm_arch *arch) + yasm_arch *arch) { yasm_bytecode *bc; const char *source; @@ -164,131 +164,131 @@ nasm_listfmt_output(yasm_listfmt *listfmt, FILE *f, yasm_linemap *linemap, buf = yasm_xmalloc(REGULAR_BUF_SIZE); while (!yasm_linemap_get_source(linemap, line, &bc, &source)) { - if (!bc) { - fprintf(f, "%6lu %*s%s\n", listline++, 32, "", source); - } else { - /* get the next relocation for the bytecode's section */ - sect = yasm_bc_get_section(bc); - if (!last_hist || last_hist->sect != sect) { - int found = 0; - - /* look through reloc_hist for matching section */ - SLIST_FOREACH(last_hist, &reloc_hist, link) { - if (last_hist->sect == sect) { - found = 1; - break; - } - } - - if (!found) { - /* not found, add to list*/ - last_hist = yasm_xmalloc(sizeof(sectreloc)); - last_hist->sect = sect; - last_hist->next_reloc = yasm_section_relocs_first(sect); - - if (last_hist->next_reloc) { - yasm_intnum *addr; - yasm_symrec *sym; - yasm_reloc_get(last_hist->next_reloc, &addr, &sym); - last_hist->next_reloc_addr = - yasm_intnum_get_uint(addr); - } - - SLIST_INSERT_HEAD(&reloc_hist, last_hist, link); - } - } - - info.next_reloc = last_hist->next_reloc; - info.next_reloc_addr = last_hist->next_reloc_addr; - STAILQ_INIT(&info.bcrelocs); - - /* loop over bytecodes on this line (usually only one) */ - while (bc && bc->line == line) { - /*@null@*/ /*@only@*/ unsigned char *bigbuf; - unsigned long size = REGULAR_BUF_SIZE; - long multiple; - unsigned long offset = bc->offset; - unsigned char *origp, *p; - int gap; - - /* convert bytecode into bytes, recording relocs along the - * way - */ - bigbuf = yasm_bc_tobytes(bc, buf, &size, &gap, &info, - nasm_listfmt_output_value, NULL); - yasm_bc_get_multiple(bc, &multiple, 1); - if (multiple <= 0) - size = 0; - else - size /= multiple; - - /* output bytes with reloc information */ - origp = bigbuf ? bigbuf : buf; - p = origp; - reloc = STAILQ_FIRST(&info.bcrelocs); - if (gap) { - fprintf(f, "%6lu %08lX %*s%s\n", listline++, offset, - 18, "", source ? source : ""); - } else while (size > 0) { - int i; - - fprintf(f, "%6lu %08lX ", listline++, offset); - for (i=0; i<18 && size > 0; size--) { - if (reloc && (unsigned long)(p-origp) == - reloc->offset) { - fprintf(f, "%c", reloc->rel ? '(' : '['); - i++; - } - fprintf(f, "%02X", *(p++)); - i+=2; - if (reloc && (unsigned long)(p-origp) == - reloc->offset+reloc->size) { - fprintf(f, "%c", reloc->rel ? ')' : ']'); - i++; - reloc = STAILQ_NEXT(reloc, link); - } - } - if (size > 0) - fprintf(f, "-"); - else { - if (multiple > 1) { - fprintf(f, ""); - i += 6; - } - fprintf(f, "%*s", 18-i+1, ""); - } - if (source) { - fprintf(f, " %s", source); - source = NULL; - } - fprintf(f, "\n"); - } - - if (bigbuf) - yasm_xfree(bigbuf); - bc = STAILQ_NEXT(bc, link); - } - - /* delete bcrelocs (newly generated next bytecode if any) */ - reloc = STAILQ_FIRST(&info.bcrelocs); - while (reloc) { - bcreloc *reloc2 = STAILQ_NEXT(reloc, link); - yasm_xfree(reloc); - reloc = reloc2; - } - - /* save reloc context */ - last_hist->next_reloc = info.next_reloc; - last_hist->next_reloc_addr = info.next_reloc_addr; - } - line++; + if (!bc) { + fprintf(f, "%6lu %*s%s\n", listline++, 32, "", source); + } else { + /* get the next relocation for the bytecode's section */ + sect = yasm_bc_get_section(bc); + if (!last_hist || last_hist->sect != sect) { + int found = 0; + + /* look through reloc_hist for matching section */ + SLIST_FOREACH(last_hist, &reloc_hist, link) { + if (last_hist->sect == sect) { + found = 1; + break; + } + } + + if (!found) { + /* not found, add to list*/ + last_hist = yasm_xmalloc(sizeof(sectreloc)); + last_hist->sect = sect; + last_hist->next_reloc = yasm_section_relocs_first(sect); + + if (last_hist->next_reloc) { + yasm_intnum *addr; + yasm_symrec *sym; + yasm_reloc_get(last_hist->next_reloc, &addr, &sym); + last_hist->next_reloc_addr = + yasm_intnum_get_uint(addr); + } + + SLIST_INSERT_HEAD(&reloc_hist, last_hist, link); + } + } + + info.next_reloc = last_hist->next_reloc; + info.next_reloc_addr = last_hist->next_reloc_addr; + STAILQ_INIT(&info.bcrelocs); + + /* loop over bytecodes on this line (usually only one) */ + while (bc && bc->line == line) { + /*@null@*/ /*@only@*/ unsigned char *bigbuf; + unsigned long size = REGULAR_BUF_SIZE; + long multiple; + unsigned long offset = bc->offset; + unsigned char *origp, *p; + int gap; + + /* convert bytecode into bytes, recording relocs along the + * way + */ + bigbuf = yasm_bc_tobytes(bc, buf, &size, &gap, &info, + nasm_listfmt_output_value, NULL); + yasm_bc_get_multiple(bc, &multiple, 1); + if (multiple <= 0) + size = 0; + else + size /= multiple; + + /* output bytes with reloc information */ + origp = bigbuf ? bigbuf : buf; + p = origp; + reloc = STAILQ_FIRST(&info.bcrelocs); + if (gap) { + fprintf(f, "%6lu %08lX %*s%s\n", listline++, offset, + 18, "", source ? source : ""); + } else while (size > 0) { + int i; + + fprintf(f, "%6lu %08lX ", listline++, offset); + for (i=0; i<18 && size > 0; size--) { + if (reloc && (unsigned long)(p-origp) == + reloc->offset) { + fprintf(f, "%c", reloc->rel ? '(' : '['); + i++; + } + fprintf(f, "%02X", *(p++)); + i+=2; + if (reloc && (unsigned long)(p-origp) == + reloc->offset+reloc->size) { + fprintf(f, "%c", reloc->rel ? ')' : ']'); + i++; + reloc = STAILQ_NEXT(reloc, link); + } + } + if (size > 0) + fprintf(f, "-"); + else { + if (multiple > 1) { + fprintf(f, ""); + i += 6; + } + fprintf(f, "%*s", 18-i+1, ""); + } + if (source) { + fprintf(f, " %s", source); + source = NULL; + } + fprintf(f, "\n"); + } + + if (bigbuf) + yasm_xfree(bigbuf); + bc = STAILQ_NEXT(bc, link); + } + + /* delete bcrelocs (newly generated next bytecode if any) */ + reloc = STAILQ_FIRST(&info.bcrelocs); + while (reloc) { + bcreloc *reloc2 = STAILQ_NEXT(reloc, link); + yasm_xfree(reloc); + reloc = reloc2; + } + + /* save reloc context */ + last_hist->next_reloc = info.next_reloc; + last_hist->next_reloc_addr = info.next_reloc_addr; + } + line++; } /* delete reloc history */ while (!SLIST_EMPTY(&reloc_hist)) { - last_hist = SLIST_FIRST(&reloc_hist); - SLIST_REMOVE_HEAD(&reloc_hist, link); - yasm_xfree(last_hist); + last_hist = SLIST_FIRST(&reloc_hist); + SLIST_REMOVE_HEAD(&reloc_hist, link); + yasm_xfree(last_hist); } yasm_xfree(buf); diff --git a/modules/objfmts/bin/bin-objfmt.c b/modules/objfmts/bin/bin-objfmt.c index b3b12628..337302c0 100644 --- a/modules/objfmts/bin/bin-objfmt.c +++ b/modules/objfmts/bin/bin-objfmt.c @@ -33,10 +33,10 @@ #include -#define REGULAR_OUTBUF_SIZE 1024 +#define REGULAR_OUTBUF_SIZE 1024 typedef struct yasm_objfmt_bin { - yasm_objfmt_base objfmt; /* base structure */ + yasm_objfmt_base objfmt; /* base structure */ } yasm_objfmt_bin; yasm_objfmt_module yasm_bin_LTX_objfmt; @@ -56,9 +56,9 @@ bin_objfmt_create(yasm_object *object) */ static unsigned long bin_objfmt_align_section(yasm_section *sect, yasm_section *prevsect, - unsigned long base, - /*@out@*/ unsigned long *prevsectlen, - /*@out@*/ unsigned long *padamt) + unsigned long base, + /*@out@*/ unsigned long *prevsectlen, + /*@out@*/ unsigned long *padamt) { unsigned long start; unsigned long align; @@ -76,7 +76,7 @@ bin_objfmt_align_section(yasm_section *sect, yasm_section *prevsect, align = yasm_section_get_align(sect); if (start & (align-1)) - start = (start & ~(align-1)) + align; + start = (start & ~(align-1)) + align; *padamt = start - (base + *prevsectlen); @@ -89,13 +89,13 @@ typedef struct bin_objfmt_output_info { /*@dependent@*/ FILE *f; /*@only@*/ unsigned char *buf; /*@observer@*/ const yasm_section *sect; - unsigned long start; /* what normal variables go against */ - unsigned long abs_start; /* what absolutes go against */ + unsigned long start; /* what normal variables go against */ + unsigned long abs_start; /* what absolutes go against */ } bin_objfmt_output_info; static /*@only@*/ yasm_expr * bin_objfmt_expr_xform(/*@returned@*/ /*@only@*/ yasm_expr *e, - /*@unused@*/ /*@null@*/ void *d) + /*@unused@*/ /*@null@*/ void *d) { int i; /*@dependent@*/ yasm_section *sect; @@ -103,20 +103,20 @@ bin_objfmt_expr_xform(/*@returned@*/ /*@only@*/ yasm_expr *e, /*@null@*/ yasm_intnum *dist; for (i=0; inumterms; i++) { - /* Transform symrecs that reference sections into - * start expr + intnum(dist). - */ - if (e->terms[i].type == YASM_EXPR_SYM && - yasm_symrec_get_label(e->terms[i].data.sym, &precbc) && - (sect = yasm_bc_get_section(precbc)) && - (dist = yasm_calc_bc_dist(yasm_section_bcs_first(sect), precbc))) { - const yasm_expr *start = yasm_section_get_start(sect); - e->terms[i].type = YASM_EXPR_EXPR; - e->terms[i].data.expn = - yasm_expr_create(YASM_EXPR_ADD, - yasm_expr_expr(yasm_expr_copy(start)), - yasm_expr_int(dist), e->line); - } + /* Transform symrecs that reference sections into + * start expr + intnum(dist). + */ + if (e->terms[i].type == YASM_EXPR_SYM && + yasm_symrec_get_label(e->terms[i].data.sym, &precbc) && + (sect = yasm_bc_get_section(precbc)) && + (dist = yasm_calc_bc_dist(yasm_section_bcs_first(sect), precbc))) { + const yasm_expr *start = yasm_section_get_start(sect); + e->terms[i].type = YASM_EXPR_EXPR; + e->terms[i].data.expn = + yasm_expr_create(YASM_EXPR_ADD, + yasm_expr_expr(yasm_expr_copy(start)), + yasm_expr_int(dist), e->line); + } } return e; @@ -124,9 +124,9 @@ bin_objfmt_expr_xform(/*@returned@*/ /*@only@*/ yasm_expr *e, static int bin_objfmt_output_value(yasm_value *value, unsigned char *buf, - unsigned int destsize, - /*@unused@*/ unsigned long offset, yasm_bytecode *bc, - int warn, /*@null@*/ void *d) + unsigned int destsize, + /*@unused@*/ unsigned long offset, yasm_bytecode *bc, + int warn, /*@null@*/ void *d) { /*@null@*/ bin_objfmt_output_info *info = (bin_objfmt_output_info *)d; /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; @@ -136,75 +136,75 @@ bin_objfmt_output_value(yasm_value *value, unsigned char *buf, /* Binary objects we need to resolve against object, not against section. */ if (value->rel && !value->curpos_rel - && yasm_symrec_get_label(value->rel, &precbc) - && (sect = yasm_bc_get_section(precbc))) { - unsigned int rshift = (unsigned int)value->rshift; - yasm_expr *syme; - if (value->rshift > 0) - syme = yasm_expr_create(YASM_EXPR_SHR, yasm_expr_sym(value->rel), - yasm_expr_int(yasm_intnum_create_uint(rshift)), bc->line); - else - syme = yasm_expr_create_ident(yasm_expr_sym(value->rel), bc->line); - - if (!value->abs) - value->abs = syme; - else - value->abs = - yasm_expr_create(YASM_EXPR_ADD, yasm_expr_expr(value->abs), - yasm_expr_expr(syme), bc->line); - value->rel = NULL; - value->rshift = 0; + && yasm_symrec_get_label(value->rel, &precbc) + && (sect = yasm_bc_get_section(precbc))) { + unsigned int rshift = (unsigned int)value->rshift; + yasm_expr *syme; + if (value->rshift > 0) + syme = yasm_expr_create(YASM_EXPR_SHR, yasm_expr_sym(value->rel), + yasm_expr_int(yasm_intnum_create_uint(rshift)), bc->line); + else + syme = yasm_expr_create_ident(yasm_expr_sym(value->rel), bc->line); + + if (!value->abs) + value->abs = syme; + else + value->abs = + yasm_expr_create(YASM_EXPR_ADD, yasm_expr_expr(value->abs), + yasm_expr_expr(syme), bc->line); + value->rel = NULL; + value->rshift = 0; } /* Simplify absolute portion of value, transforming symrecs */ if (value->abs) - value->abs = yasm_expr__level_tree - (value->abs, 1, 1, 1, 0, bin_objfmt_expr_xform, NULL); + value->abs = yasm_expr__level_tree + (value->abs, 1, 1, 1, 0, bin_objfmt_expr_xform, NULL); /* Output */ switch (yasm_value_output_basic(value, buf, destsize, bc, warn, - info->object->arch)) { - case -1: - return 1; - case 0: - break; - default: - return 0; + info->object->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; } /* Absolute value; handle it here as output_basic won't understand it */ if (value->rel && yasm_symrec_is_abs(value->rel)) { - if (value->curpos_rel) { - /* Calculate value relative to current assembly position */ - /*@only@*/ yasm_intnum *outval; - unsigned int valsize = value->size; - int retval = 0; - - outval = yasm_intnum_create_uint(bc->offset + info->abs_start); - yasm_intnum_calc(outval, YASM_EXPR_NEG, NULL); - - if (value->rshift > 0) { - /*@only@*/ yasm_intnum *shamt = - yasm_intnum_create_uint((unsigned long)value->rshift); - yasm_intnum_calc(outval, YASM_EXPR_SHR, shamt); - yasm_intnum_destroy(shamt); - } - /* Add in absolute portion */ - if (value->abs) - yasm_intnum_calc(outval, YASM_EXPR_ADD, - yasm_expr_get_intnum(&value->abs, 1)); - /* Output! */ - if (yasm_arch_intnum_tobytes(info->object->arch, outval, buf, - destsize, valsize, 0, bc, warn)) - retval = 1; - yasm_intnum_destroy(outval); - return retval; - } + if (value->curpos_rel) { + /* Calculate value relative to current assembly position */ + /*@only@*/ yasm_intnum *outval; + unsigned int valsize = value->size; + int retval = 0; + + outval = yasm_intnum_create_uint(bc->offset + info->abs_start); + yasm_intnum_calc(outval, YASM_EXPR_NEG, NULL); + + if (value->rshift > 0) { + /*@only@*/ yasm_intnum *shamt = + yasm_intnum_create_uint((unsigned long)value->rshift); + yasm_intnum_calc(outval, YASM_EXPR_SHR, shamt); + yasm_intnum_destroy(shamt); + } + /* Add in absolute portion */ + if (value->abs) + yasm_intnum_calc(outval, YASM_EXPR_ADD, + yasm_expr_get_intnum(&value->abs, 1)); + /* Output! */ + if (yasm_arch_intnum_tobytes(info->object->arch, outval, buf, + destsize, valsize, 0, bc, warn)) + retval = 1; + yasm_intnum_destroy(outval); + return retval; + } } /* Couldn't output, assume it contains an external reference. */ yasm_error_set(YASM_ERROR_GENERAL, - N_("binary object format does not support external references")); + N_("binary object format does not support external references")); return 1; } @@ -219,36 +219,36 @@ bin_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) assert(info != NULL); bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info, - bin_objfmt_output_value, NULL); + bin_objfmt_output_value, NULL); /* Don't bother doing anything else if size ended up being 0. */ if (size == 0) { - if (bigbuf) - yasm_xfree(bigbuf); - return 0; + if (bigbuf) + yasm_xfree(bigbuf); + return 0; } /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { - unsigned long left; - yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, - N_("uninitialized space declared in code/data section: zeroing")); - /* Write out in chunks */ - memset(info->buf, 0, REGULAR_OUTBUF_SIZE); - left = size; - while (left > REGULAR_OUTBUF_SIZE) { - fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); - left -= REGULAR_OUTBUF_SIZE; - } - fwrite(info->buf, left, 1, info->f); + unsigned long left; + yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, + N_("uninitialized space declared in code/data section: zeroing")); + /* Write out in chunks */ + memset(info->buf, 0, REGULAR_OUTBUF_SIZE); + left = size; + while (left > REGULAR_OUTBUF_SIZE) { + fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); + left -= REGULAR_OUTBUF_SIZE; + } + fwrite(info->buf, left, 1, info->f); } else { - /* Output buf (or bigbuf if non-NULL) to file */ - fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f); + /* Output buf (or bigbuf if non-NULL) to file */ + fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f); } /* If bigbuf was allocated, free it */ if (bigbuf) - yasm_xfree(bigbuf); + yasm_xfree(bigbuf); return 0; } @@ -261,24 +261,24 @@ bin_objfmt_check_sym(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (vis & YASM_SYM_EXTERN) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("binary object format does not support extern variables")); - yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); + yasm_warn_set(YASM_WARN_GENERAL, + N_("binary object format does not support extern variables")); + yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); } else if (vis & YASM_SYM_GLOBAL) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("binary object format does not support global variables")); - yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); + yasm_warn_set(YASM_WARN_GENERAL, + N_("binary object format does not support global variables")); + yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); } else if (vis & YASM_SYM_COMMON) { - yasm_error_set(YASM_ERROR_TYPE, - N_("binary object format does not support common variables")); - yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); + yasm_error_set(YASM_ERROR_TYPE, + N_("binary object format does not support common variables")); + yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); } return 0; } static void bin_objfmt_output(yasm_object *object, FILE *f, /*@unused@*/ int all_syms, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { /*@observer@*/ /*@null@*/ yasm_section *text, *data, *bss, *prevsect; /*@null@*/ yasm_expr *startexpr; @@ -302,7 +302,7 @@ bin_objfmt_output(yasm_object *object, FILE *f, /*@unused@*/ int all_syms, bss = yasm_object_find_general(object, ".bss"); if (!text) - yasm_internal_error(N_("No `.text' section in bin objfmt output")); + yasm_internal_error(N_("No `.text' section in bin objfmt output")); /* First determine the actual starting offsets for .data and .bss. * As the order in the file is .text -> .data -> .bss (not present), @@ -316,10 +316,10 @@ bin_objfmt_output(yasm_object *object, FILE *f, /*@unused@*/ int all_syms, assert(startexpr != NULL); startnum = yasm_expr_get_intnum(&startexpr, 0); if (!startnum) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("ORG expression too complex")); - yasm_errwarn_propagate(errwarns, startexpr->line); - return; + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("ORG expression too complex")); + yasm_errwarn_propagate(errwarns, startexpr->line); + return; } start = yasm_intnum_get_uint(startnum); yasm_expr_destroy(startexpr); @@ -331,41 +331,41 @@ bin_objfmt_output(yasm_object *object, FILE *f, /*@unused@*/ int all_syms, prevsectlenptr = &textlen; prevsectpadptr = &textpad; if (data) { - start = bin_objfmt_align_section(data, prevsect, start, - prevsectlenptr, prevsectpadptr); - yasm_section_set_start(data, yasm_expr_create_ident( - yasm_expr_int(yasm_intnum_create_uint(start)), 0), 0); - datastart = start; - prevsect = data; - prevsectlenptr = &datalen; - prevsectpadptr = &datapad; + start = bin_objfmt_align_section(data, prevsect, start, + prevsectlenptr, prevsectpadptr); + yasm_section_set_start(data, yasm_expr_create_ident( + yasm_expr_int(yasm_intnum_create_uint(start)), 0), 0); + datastart = start; + prevsect = data; + prevsectlenptr = &datalen; + prevsectpadptr = &datapad; } if (bss) { - start = bin_objfmt_align_section(bss, prevsect, start, - prevsectlenptr, prevsectpadptr); - yasm_section_set_start(bss, yasm_expr_create_ident( - yasm_expr_int(yasm_intnum_create_uint(start)), 0), 0); + start = bin_objfmt_align_section(bss, prevsect, start, + prevsectlenptr, prevsectpadptr); + yasm_section_set_start(bss, yasm_expr_create_ident( + yasm_expr_int(yasm_intnum_create_uint(start)), 0), 0); } /* Output .text first. */ info.sect = text; info.start = textstart; yasm_section_bcs_traverse(text, errwarns, &info, - bin_objfmt_output_bytecode); + bin_objfmt_output_bytecode); /* If .data is present, output it */ if (data) { - /* Add padding to align .data. Just use a for loop, as this will - * seldom be very many bytes. - */ - for (i=0; isymtab, sectname, - yasm_section_bcs_first(sect), 1, line); + yasm_section_bcs_first(sect), 1, line); } static yasm_section * @@ -396,17 +396,17 @@ bin_objfmt_add_default_section(yasm_object *object) retval = yasm_object_get_general(object, ".text", 0, 16, 1, 0, &isnew, 0); if (isnew) { - bin_objfmt_init_new_section(object, retval, ".text", 0); - yasm_section_set_default(retval, 1); + bin_objfmt_init_new_section(object, retval, ".text", 0); + yasm_section_set_default(retval, 1); } return retval; } static /*@observer@*/ /*@null@*/ yasm_section * bin_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, - unsigned long line) + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + unsigned long line) { yasm_valparam *vp; yasm_section *retval; @@ -418,88 +418,88 @@ bin_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, int have_align = 0; if ((vp = yasm_vps_first(valparams)) && !vp->param && vp->val != NULL) { - /* If it's the first section output (.text) start at 0, otherwise - * make sure the start is > 128. - */ - sectname = vp->val; - if (strcmp(sectname, ".text") == 0) - start = 0; - else if (strcmp(sectname, ".data") == 0) - start = 200; - else if (strcmp(sectname, ".bss") == 0) { - start = 200; - resonly = 1; - } else { - /* other section names not recognized. */ - yasm_error_set(YASM_ERROR_GENERAL, - N_("segment name `%s' not recognized"), sectname); - return NULL; - } - - /* Check for ALIGN qualifier */ - while ((vp = yasm_vps_next(vp))) { - if (!vp->val) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized numeric qualifier")); - continue; - } - - if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { - /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; - - if (strcmp(sectname, ".text") == 0) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("cannot specify an alignment to the `%s' section"), - sectname); - return NULL; - } - - align_expr = yasm_expr_get_intnum(&vp->param, 0); - if (!align_expr) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not an integer"), - vp->val); - return NULL; - } - align = yasm_intnum_get_uint(align_expr); - - /* Alignments must be a power of two. */ - if (!is_exp2(align)) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), - vp->val); - return NULL; - } - - have_align = 1; - } - } - - retval = yasm_object_get_general(object, sectname, - yasm_expr_create_ident( - yasm_expr_int(yasm_intnum_create_uint(start)), line), align, - strcmp(sectname, ".text") == 0, resonly, &isnew, line); - - if (isnew) - bin_objfmt_init_new_section(object, retval, sectname, line); - - if (isnew || yasm_section_is_default(retval)) { - yasm_section_set_default(retval, 0); - yasm_section_set_align(retval, align, line); - } else if (have_align) - yasm_warn_set(YASM_WARN_GENERAL, - N_("alignment value ignored on section redeclaration")); - - return retval; + /* If it's the first section output (.text) start at 0, otherwise + * make sure the start is > 128. + */ + sectname = vp->val; + if (strcmp(sectname, ".text") == 0) + start = 0; + else if (strcmp(sectname, ".data") == 0) + start = 200; + else if (strcmp(sectname, ".bss") == 0) { + start = 200; + resonly = 1; + } else { + /* other section names not recognized. */ + yasm_error_set(YASM_ERROR_GENERAL, + N_("segment name `%s' not recognized"), sectname); + return NULL; + } + + /* Check for ALIGN qualifier */ + while ((vp = yasm_vps_next(vp))) { + if (!vp->val) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized numeric qualifier")); + continue; + } + + if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { + /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; + + if (strcmp(sectname, ".text") == 0) { + yasm_error_set(YASM_ERROR_GENERAL, + N_("cannot specify an alignment to the `%s' section"), + sectname); + return NULL; + } + + align_expr = yasm_expr_get_intnum(&vp->param, 0); + if (!align_expr) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not an integer"), + vp->val); + return NULL; + } + align = yasm_intnum_get_uint(align_expr); + + /* Alignments must be a power of two. */ + if (!is_exp2(align)) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not a power of two"), + vp->val); + return NULL; + } + + have_align = 1; + } + } + + retval = yasm_object_get_general(object, sectname, + yasm_expr_create_ident( + yasm_expr_int(yasm_intnum_create_uint(start)), line), align, + strcmp(sectname, ".text") == 0, resonly, &isnew, line); + + if (isnew) + bin_objfmt_init_new_section(object, retval, sectname, line); + + if (isnew || yasm_section_is_default(retval)) { + yasm_section_set_default(retval, 0); + yasm_section_set_align(retval, align, line); + } else if (have_align) + yasm_warn_set(YASM_WARN_GENERAL, + N_("alignment value ignored on section redeclaration")); + + return retval; } else - return NULL; + return NULL; } static void bin_objfmt_dir_org(yasm_object *object, - /*@null@*/ yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, unsigned long line) + /*@null@*/ yasm_valparamhead *valparams, + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_section *sect; yasm_valparam *vp; @@ -507,32 +507,32 @@ bin_objfmt_dir_org(yasm_object *object, /*@null@*/ yasm_expr *start = NULL; if (!valparams) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] requires an argument"), - "ORG"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] requires an argument"), + "ORG"); + return; } /* ORG takes just a simple integer as param */ vp = yasm_vps_first(valparams); if (vp->val) - start = yasm_expr_create_ident(yasm_expr_sym(yasm_symtab_use( - object->symtab, vp->val, line)), line); + start = yasm_expr_create_ident(yasm_expr_sym(yasm_symtab_use( + object->symtab, vp->val, line)), line); else if (vp->param) { - start = vp->param; - vp->param = NULL; /* Don't let valparams delete it */ + start = vp->param; + vp->param = NULL; /* Don't let valparams delete it */ } if (!start) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("argument to ORG must be expression")); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("argument to ORG must be expression")); + return; } /* ORG changes the start of the .text section */ sect = yasm_object_find_general(object, ".text"); if (!sect) - yasm_internal_error( - N_("bin objfmt: .text section does not exist before ORG is called?")); + yasm_internal_error( + N_("bin objfmt: .text section does not exist before ORG is called?")); yasm_section_set_start(sect, start, line); } @@ -544,7 +544,7 @@ static const char *bin_objfmt_dbgfmt_keywords[] = { }; static const yasm_directive bin_objfmt_directives[] = { - { "org", "nasm", bin_objfmt_dir_org, YASM_DIR_ARG_REQUIRED }, + { "org", "nasm", bin_objfmt_dir_org, YASM_DIR_ARG_REQUIRED }, { NULL, NULL, NULL, 0 } }; diff --git a/modules/objfmts/coff/coff-objfmt.c b/modules/objfmts/coff/coff-objfmt.c index c93e8eaf..4d619621 100644 --- a/modules/objfmts/coff/coff-objfmt.c +++ b/modules/objfmts/coff/coff-objfmt.c @@ -36,7 +36,7 @@ #include "coff-objfmt.h" -#define REGULAR_OUTBUF_SIZE 1024 +#define REGULAR_OUTBUF_SIZE 1024 /* Defining this to 0 sets all section VMA's to 0 rather than as the same as * the LMA. According to the DJGPP COFF Spec, this should be set to 1 @@ -45,117 +45,117 @@ * with VMA=0. Who's right? This is #defined as changing this setting affects * several places in the code. */ -#define COFF_SET_VMA (!objfmt_coff->win32) +#define COFF_SET_VMA (!objfmt_coff->win32) -#define COFF_MACHINE_I386 0x014C -#define COFF_MACHINE_AMD64 0x8664 +#define COFF_MACHINE_I386 0x014C +#define COFF_MACHINE_AMD64 0x8664 -#define COFF_F_LNNO 0x0004 /* line number info NOT present */ -#define COFF_F_LSYMS 0x0008 /* local symbols NOT present */ -#define COFF_F_AR32WR 0x0100 /* 32-bit little endian file */ +#define COFF_F_LNNO 0x0004 /* line number info NOT present */ +#define COFF_F_LSYMS 0x0008 /* local symbols NOT present */ +#define COFF_F_AR32WR 0x0100 /* 32-bit little endian file */ typedef struct coff_reloc { yasm_reloc reloc; enum { - COFF_RELOC_ABSOLUTE = 0, /* absolute, no reloc needed */ - - /* I386 relocations */ - COFF_RELOC_I386_ADDR16 = 0x1, /* 16-bit absolute reference */ - COFF_RELOC_I386_REL16 = 0x2, /* 16-bit PC-relative reference */ - COFF_RELOC_I386_ADDR32 = 0x6, /* 32-bit absolute reference */ - COFF_RELOC_I386_ADDR32NB = 0x7, /* 32-bit absolute ref w/o base */ - COFF_RELOC_I386_SEG12 = 0x9, /* 16-bit absolute segment ref */ - COFF_RELOC_I386_SECTION = 0xA, /* section index */ - COFF_RELOC_I386_SECREL = 0xB, /* offset from start of segment */ - COFF_RELOC_I386_TOKEN = 0xC, /* CLR metadata token */ - COFF_RELOC_I386_SECREL7 = 0xD, /* 7-bit offset from base of sect */ - COFF_RELOC_I386_REL32 = 0x14, /* 32-bit PC-relative reference */ - - /* AMD64 relocations */ - COFF_RELOC_AMD64_ADDR64 = 0x1, /* 64-bit address (VA) */ - COFF_RELOC_AMD64_ADDR32 = 0x2, /* 32-bit address (VA) */ - COFF_RELOC_AMD64_ADDR32NB = 0x3, /* 32-bit address w/o base (RVA) */ - COFF_RELOC_AMD64_REL32 = 0x4, /* 32-bit relative (0 byte dist) */ - COFF_RELOC_AMD64_REL32_1 = 0x5, /* 32-bit relative (1 byte dist) */ - COFF_RELOC_AMD64_REL32_2 = 0x6, /* 32-bit relative (2 byte dist) */ - COFF_RELOC_AMD64_REL32_3 = 0x7, /* 32-bit relative (3 byte dist) */ - COFF_RELOC_AMD64_REL32_4 = 0x8, /* 32-bit relative (4 byte dist) */ - COFF_RELOC_AMD64_REL32_5 = 0x9, /* 32-bit relative (5 byte dist) */ - COFF_RELOC_AMD64_SECTION = 0xA, /* section index */ - COFF_RELOC_AMD64_SECREL = 0xB, /* 32-bit offset from base of sect */ - COFF_RELOC_AMD64_SECREL7 = 0xC, /* 7-bit offset from base of sect */ - COFF_RELOC_AMD64_TOKEN = 0xD /* CLR metadata token */ - } type; /* type of relocation */ + COFF_RELOC_ABSOLUTE = 0, /* absolute, no reloc needed */ + + /* I386 relocations */ + COFF_RELOC_I386_ADDR16 = 0x1, /* 16-bit absolute reference */ + COFF_RELOC_I386_REL16 = 0x2, /* 16-bit PC-relative reference */ + COFF_RELOC_I386_ADDR32 = 0x6, /* 32-bit absolute reference */ + COFF_RELOC_I386_ADDR32NB = 0x7, /* 32-bit absolute ref w/o base */ + COFF_RELOC_I386_SEG12 = 0x9, /* 16-bit absolute segment ref */ + COFF_RELOC_I386_SECTION = 0xA, /* section index */ + COFF_RELOC_I386_SECREL = 0xB, /* offset from start of segment */ + COFF_RELOC_I386_TOKEN = 0xC, /* CLR metadata token */ + COFF_RELOC_I386_SECREL7 = 0xD, /* 7-bit offset from base of sect */ + COFF_RELOC_I386_REL32 = 0x14, /* 32-bit PC-relative reference */ + + /* AMD64 relocations */ + COFF_RELOC_AMD64_ADDR64 = 0x1, /* 64-bit address (VA) */ + COFF_RELOC_AMD64_ADDR32 = 0x2, /* 32-bit address (VA) */ + COFF_RELOC_AMD64_ADDR32NB = 0x3, /* 32-bit address w/o base (RVA) */ + COFF_RELOC_AMD64_REL32 = 0x4, /* 32-bit relative (0 byte dist) */ + COFF_RELOC_AMD64_REL32_1 = 0x5, /* 32-bit relative (1 byte dist) */ + COFF_RELOC_AMD64_REL32_2 = 0x6, /* 32-bit relative (2 byte dist) */ + COFF_RELOC_AMD64_REL32_3 = 0x7, /* 32-bit relative (3 byte dist) */ + COFF_RELOC_AMD64_REL32_4 = 0x8, /* 32-bit relative (4 byte dist) */ + COFF_RELOC_AMD64_REL32_5 = 0x9, /* 32-bit relative (5 byte dist) */ + COFF_RELOC_AMD64_SECTION = 0xA, /* section index */ + COFF_RELOC_AMD64_SECREL = 0xB, /* 32-bit offset from base of sect */ + COFF_RELOC_AMD64_SECREL7 = 0xC, /* 7-bit offset from base of sect */ + COFF_RELOC_AMD64_TOKEN = 0xD /* CLR metadata token */ + } type; /* type of relocation */ } coff_reloc; -#define COFF_STYP_TEXT 0x00000020UL -#define COFF_STYP_DATA 0x00000040UL -#define COFF_STYP_BSS 0x00000080UL -#define COFF_STYP_INFO 0x00000200UL -#define COFF_STYP_STD_MASK 0x000003FFUL -#define COFF_STYP_ALIGN_MASK 0x00F00000UL -#define COFF_STYP_ALIGN_SHIFT 20 -#define COFF_STYP_NRELOC_OVFL 0x01000000UL -#define COFF_STYP_DISCARD 0x02000000UL -#define COFF_STYP_NOCACHE 0x04000000UL -#define COFF_STYP_NOPAGE 0x08000000UL -#define COFF_STYP_SHARED 0x10000000UL -#define COFF_STYP_EXECUTE 0x20000000UL -#define COFF_STYP_READ 0x40000000UL -#define COFF_STYP_WRITE 0x80000000UL -#define COFF_STYP_WIN32_MASK 0xFF000000UL - -#define COFF_FLAG_NOBASE (1UL<<0) /* Use no-base (NB) relocs */ +#define COFF_STYP_TEXT 0x00000020UL +#define COFF_STYP_DATA 0x00000040UL +#define COFF_STYP_BSS 0x00000080UL +#define COFF_STYP_INFO 0x00000200UL +#define COFF_STYP_STD_MASK 0x000003FFUL +#define COFF_STYP_ALIGN_MASK 0x00F00000UL +#define COFF_STYP_ALIGN_SHIFT 20 +#define COFF_STYP_NRELOC_OVFL 0x01000000UL +#define COFF_STYP_DISCARD 0x02000000UL +#define COFF_STYP_NOCACHE 0x04000000UL +#define COFF_STYP_NOPAGE 0x08000000UL +#define COFF_STYP_SHARED 0x10000000UL +#define COFF_STYP_EXECUTE 0x20000000UL +#define COFF_STYP_READ 0x40000000UL +#define COFF_STYP_WRITE 0x80000000UL +#define COFF_STYP_WIN32_MASK 0xFF000000UL + +#define COFF_FLAG_NOBASE (1UL<<0) /* Use no-base (NB) relocs */ typedef struct coff_section_data { - /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ - unsigned int scnum; /* section number (1=first section) */ + /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ + unsigned int scnum; /* section number (1=first section) */ unsigned long flags; /* section flags (see COFF_STYP_* above) */ - unsigned long addr; /* starting memory address (first section -> 0) */ + unsigned long addr; /* starting memory address (first section -> 0) */ unsigned long scnptr; /* file ptr to raw data */ - unsigned long size; /* size of raw data (section data) in bytes */ + unsigned long size; /* size of raw data (section data) in bytes */ unsigned long relptr; /* file ptr to relocation */ unsigned long nreloc; /* number of relocation entries >64k -> error */ unsigned long flags2; /* internal flags (see COFF_FLAG_* above) */ - unsigned long strtab_name; /* strtab offset of name if name > 8 chars */ - int isdebug; /* is a debug section? */ + unsigned long strtab_name; /* strtab offset of name if name > 8 chars */ + int isdebug; /* is a debug section? */ } coff_section_data; typedef enum coff_symrec_sclass { - COFF_SCL_EFCN = 0xff, /* physical end of function */ + COFF_SCL_EFCN = 0xff, /* physical end of function */ COFF_SCL_NULL = 0, - COFF_SCL_AUTO = 1, /* automatic variable */ - COFF_SCL_EXT = 2, /* external symbol */ - COFF_SCL_STAT = 3, /* static */ - COFF_SCL_REG = 4, /* register variable */ - COFF_SCL_EXTDEF = 5, /* external definition */ - COFF_SCL_LABEL = 6, /* label */ - COFF_SCL_ULABEL = 7, /* undefined label */ - COFF_SCL_MOS = 8, /* member of structure */ - COFF_SCL_ARG = 9, /* function argument */ - COFF_SCL_STRTAG = 10, /* structure tag */ - COFF_SCL_MOU = 11, /* member of union */ - COFF_SCL_UNTAG = 12, /* union tag */ - COFF_SCL_TPDEF = 13, /* type definition */ - COFF_SCL_USTATIC = 14, /* undefined static */ - COFF_SCL_ENTAG = 15, /* enumeration tag */ - COFF_SCL_MOE = 16, /* member of enumeration */ - COFF_SCL_REGPARM = 17, /* register parameter */ - COFF_SCL_FIELD = 18, /* bit field */ - COFF_SCL_AUTOARG = 19, /* auto argument */ - COFF_SCL_LASTENT = 20, /* dummy entry (end of block) */ - COFF_SCL_BLOCK = 100, /* ".bb" or ".eb" */ - COFF_SCL_FCN = 101, /* ".bf" or ".ef" */ - COFF_SCL_EOS = 102, /* end of structure */ - COFF_SCL_FILE = 103, /* file name */ - COFF_SCL_LINE = 104, /* line # reformatted as symbol table entry */ - COFF_SCL_ALIAS = 105, /* duplicate tag */ - COFF_SCL_HIDDEN = 106 /* ext symbol in dmert public lib */ + COFF_SCL_AUTO = 1, /* automatic variable */ + COFF_SCL_EXT = 2, /* external symbol */ + COFF_SCL_STAT = 3, /* static */ + COFF_SCL_REG = 4, /* register variable */ + COFF_SCL_EXTDEF = 5, /* external definition */ + COFF_SCL_LABEL = 6, /* label */ + COFF_SCL_ULABEL = 7, /* undefined label */ + COFF_SCL_MOS = 8, /* member of structure */ + COFF_SCL_ARG = 9, /* function argument */ + COFF_SCL_STRTAG = 10, /* structure tag */ + COFF_SCL_MOU = 11, /* member of union */ + COFF_SCL_UNTAG = 12, /* union tag */ + COFF_SCL_TPDEF = 13, /* type definition */ + COFF_SCL_USTATIC = 14, /* undefined static */ + COFF_SCL_ENTAG = 15, /* enumeration tag */ + COFF_SCL_MOE = 16, /* member of enumeration */ + COFF_SCL_REGPARM = 17, /* register parameter */ + COFF_SCL_FIELD = 18, /* bit field */ + COFF_SCL_AUTOARG = 19, /* auto argument */ + COFF_SCL_LASTENT = 20, /* dummy entry (end of block) */ + COFF_SCL_BLOCK = 100, /* ".bb" or ".eb" */ + COFF_SCL_FCN = 101, /* ".bf" or ".ef" */ + COFF_SCL_EOS = 102, /* end of structure */ + COFF_SCL_FILE = 103, /* file name */ + COFF_SCL_LINE = 104, /* line # reformatted as symbol table entry */ + COFF_SCL_ALIAS = 105, /* duplicate tag */ + COFF_SCL_HIDDEN = 106 /* ext symbol in dmert public lib */ } coff_symrec_sclass; typedef union coff_symtab_auxent { /* no data needed for section symbol auxent, all info avail from sym */ - /*@owned@*/ char *fname; /* filename aux entry */ + /*@owned@*/ char *fname; /* filename aux entry */ } coff_symtab_auxent; typedef enum coff_symtab_auxtype { @@ -165,29 +165,29 @@ typedef enum coff_symtab_auxtype { } coff_symtab_auxtype; typedef struct coff_symrec_data { - unsigned long index; /* assigned COFF symbol table index */ - coff_symrec_sclass sclass; /* storage class */ + unsigned long index; /* assigned COFF symbol table index */ + coff_symrec_sclass sclass; /* storage class */ - int numaux; /* number of auxiliary entries */ + int numaux; /* number of auxiliary entries */ coff_symtab_auxtype auxtype; /* type of aux entries */ - coff_symtab_auxent aux[1]; /* actually may be any size (including 0) */ + coff_symtab_auxent aux[1]; /* actually may be any size (including 0) */ } coff_symrec_data; typedef struct yasm_objfmt_coff { - yasm_objfmt_base objfmt; /* base structure */ + yasm_objfmt_base objfmt; /* base structure */ - unsigned int parse_scnum; /* sect numbering in parser */ - int win32; /* nonzero for win32/64 output */ - int win64; /* nonzero for win64 output */ + unsigned int parse_scnum; /* sect numbering in parser */ + int win32; /* nonzero for win32/64 output */ + int win64; /* nonzero for win64 output */ - unsigned int machine; /* COFF machine to use */ + unsigned int machine; /* COFF machine to use */ - coff_symrec_data *filesym_data; /* Data for .file symbol */ + coff_symrec_data *filesym_data; /* Data for .file symbol */ /* data for win64 proc_frame and related directives */ - unsigned long proc_frame; /* Line number of start of proc, or 0 */ - unsigned long done_prolog; /* Line number of end of prologue, or 0 */ - /*@null@*/ coff_unwind_info *unwind; /* Unwind info */ + unsigned long proc_frame; /* Line number of start of proc, or 0 */ + unsigned long done_prolog; /* Line number of end of prologue, or 0 */ + /*@null@*/ coff_unwind_info *unwind; /* Unwind info */ } yasm_objfmt_coff; typedef struct coff_objfmt_output_info { @@ -198,11 +198,11 @@ typedef struct coff_objfmt_output_info { /*@only@*/ unsigned char *buf; yasm_section *sect; /*@dependent@*/ coff_section_data *csd; - unsigned long addr; /* start of next section */ + unsigned long addr; /* start of next section */ - unsigned long indx; /* current symbol index */ - int all_syms; /* outputting all symbols? */ - unsigned long strtab_offset; /* current string table offset */ + unsigned long indx; /* current symbol index */ + int all_syms; /* outputting all symbols? */ + unsigned long strtab_offset; /* current string table offset */ } coff_objfmt_output_info; static void coff_section_data_destroy(/*@only@*/ void *d); @@ -228,12 +228,12 @@ yasm_objfmt_module yasm_win64_LTX_objfmt; static /*@dependent@*/ coff_symrec_data * coff_objfmt_sym_set_data(yasm_symrec *sym, coff_symrec_sclass sclass, - int numaux, coff_symtab_auxtype auxtype) + int numaux, coff_symtab_auxtype auxtype) { coff_symrec_data *sym_data; sym_data = yasm_xmalloc(sizeof(coff_symrec_data) + - (numaux-1)*sizeof(coff_symtab_auxent)); + (numaux-1)*sizeof(coff_symtab_auxent)); sym_data->index = 0; sym_data->sclass = sclass; sym_data->numaux = numaux; @@ -252,18 +252,18 @@ coff_common_create(yasm_object *object) /* Only support x86 arch */ if (yasm__strcasecmp(yasm_arch_keyword(object->arch), "x86") != 0) { - yasm_xfree(objfmt_coff); - return NULL; + yasm_xfree(objfmt_coff); + return NULL; } objfmt_coff->parse_scnum = 1; /* section numbering starts at 1 */ /* FIXME: misuse of NULL bytecode here; it works, but only barely. */ filesym = yasm_symtab_define_special(object->symtab, ".file", - YASM_SYM_GLOBAL); + YASM_SYM_GLOBAL); objfmt_coff->filesym_data = - coff_objfmt_sym_set_data(filesym, COFF_SCL_FILE, 1, - COFF_SYMTAB_AUX_FILE); + coff_objfmt_sym_set_data(filesym, COFF_SCL_FILE, 1, + COFF_SYMTAB_AUX_FILE); /* Filename is set in coff_objfmt_output */ objfmt_coff->filesym_data->aux[0].fname = NULL; @@ -280,20 +280,20 @@ coff_objfmt_create(yasm_object *object) yasm_objfmt_coff *objfmt_coff = coff_common_create(object); if (objfmt_coff) { - /* Support x86 and amd64 machines of x86 arch */ - if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), "x86") == 0) - objfmt_coff->machine = COFF_MACHINE_I386; - else if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), - "amd64") == 0) - objfmt_coff->machine = COFF_MACHINE_AMD64; - else { - yasm_xfree(objfmt_coff); - return NULL; - } - - objfmt_coff->objfmt.module = &yasm_coff_LTX_objfmt; - objfmt_coff->win32 = 0; - objfmt_coff->win64 = 0; + /* Support x86 and amd64 machines of x86 arch */ + if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), "x86") == 0) + objfmt_coff->machine = COFF_MACHINE_I386; + else if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), + "amd64") == 0) + objfmt_coff->machine = COFF_MACHINE_AMD64; + else { + yasm_xfree(objfmt_coff); + return NULL; + } + + objfmt_coff->objfmt.module = &yasm_coff_LTX_objfmt; + objfmt_coff->win32 = 0; + objfmt_coff->win64 = 0; } return (yasm_objfmt *)objfmt_coff; } @@ -304,24 +304,24 @@ win32_objfmt_create(yasm_object *object) yasm_objfmt_coff *objfmt_coff = coff_common_create(object); if (objfmt_coff) { - /* Support x86 and amd64 machines of x86 arch. - * (amd64 machine supported for backwards compatibility) - */ - if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), - "x86") == 0) { - objfmt_coff->machine = COFF_MACHINE_I386; - objfmt_coff->objfmt.module = &yasm_win32_LTX_objfmt; - } else if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), - "amd64") == 0) { - objfmt_coff->machine = COFF_MACHINE_AMD64; - objfmt_coff->objfmt.module = &yasm_win64_LTX_objfmt; - objfmt_coff->win64 = 1; - } else { - yasm_xfree(objfmt_coff); - return NULL; - } - - objfmt_coff->win32 = 1; + /* Support x86 and amd64 machines of x86 arch. + * (amd64 machine supported for backwards compatibility) + */ + if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), + "x86") == 0) { + objfmt_coff->machine = COFF_MACHINE_I386; + objfmt_coff->objfmt.module = &yasm_win32_LTX_objfmt; + } else if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), + "amd64") == 0) { + objfmt_coff->machine = COFF_MACHINE_AMD64; + objfmt_coff->objfmt.module = &yasm_win64_LTX_objfmt; + objfmt_coff->win64 = 1; + } else { + yasm_xfree(objfmt_coff); + return NULL; + } + + objfmt_coff->win32 = 1; } return (yasm_objfmt *)objfmt_coff; } @@ -332,25 +332,25 @@ win64_objfmt_create(yasm_object *object) yasm_objfmt_coff *objfmt_coff = coff_common_create(object); if (objfmt_coff) { - /* Support amd64 machine of x86 arch */ - if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), - "amd64") == 0) { - objfmt_coff->machine = COFF_MACHINE_AMD64; - } else { - yasm_xfree(objfmt_coff); - return NULL; - } - - objfmt_coff->objfmt.module = &yasm_win64_LTX_objfmt; - objfmt_coff->win32 = 1; - objfmt_coff->win64 = 1; + /* Support amd64 machine of x86 arch */ + if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), + "amd64") == 0) { + objfmt_coff->machine = COFF_MACHINE_AMD64; + } else { + yasm_xfree(objfmt_coff); + return NULL; + } + + objfmt_coff->objfmt.module = &yasm_win64_LTX_objfmt; + objfmt_coff->win32 = 1; + objfmt_coff->win64 = 1; } return (yasm_objfmt *)objfmt_coff; } static coff_section_data * coff_objfmt_init_new_section(yasm_object *object, yasm_section *sect, - const char *sectname, unsigned long line) + const char *sectname, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; coff_section_data *data; @@ -370,7 +370,7 @@ coff_objfmt_init_new_section(yasm_object *object, yasm_section *sect, yasm_section_add_data(sect, &coff_section_data_cb, data); sym = yasm_symtab_define_label(object->symtab, sectname, - yasm_section_bcs_first(sect), 1, line); + yasm_section_bcs_first(sect), 1, line); yasm_symrec_declare(sym, YASM_SYM_GLOBAL, line); coff_objfmt_sym_set_data(sym, COFF_SCL_STAT, 1, COFF_SYMTAB_AUX_SECT); data->sym = sym; @@ -385,21 +385,21 @@ coff_objfmt_init_remaining_section(yasm_section *sect, /*@null@*/ void *d) /* Skip absolute sections */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); csd = yasm_section_get_data(sect, &coff_section_data_cb); if (!csd) { - /* Initialize new one */ - const char *sectname = yasm_section_get_name(sect); - csd = coff_objfmt_init_new_section(info->object, sect, sectname, 0); - if (yasm__strncasecmp(sectname, ".debug", 6)==0) { - csd->flags = COFF_STYP_DATA; - if (info->objfmt_coff->win32) - csd->flags |= COFF_STYP_DISCARD|COFF_STYP_READ; - csd->isdebug = 1; - } else - csd->flags = COFF_STYP_TEXT; + /* Initialize new one */ + const char *sectname = yasm_section_get_name(sect); + csd = coff_objfmt_init_new_section(info->object, sect, sectname, 0); + if (yasm__strncasecmp(sectname, ".debug", 6)==0) { + csd->flags = COFF_STYP_DATA; + if (info->objfmt_coff->win32) + csd->flags |= COFF_STYP_DISCARD|COFF_STYP_READ; + csd->isdebug = 1; + } else + csd->flags = COFF_STYP_TEXT; } return 0; @@ -413,7 +413,7 @@ coff_objfmt_set_section_addr(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); csd = yasm_section_get_data(sect, &coff_section_data_cb); @@ -427,8 +427,8 @@ coff_objfmt_set_section_addr(yasm_section *sect, /*@null@*/ void *d) static int coff_objfmt_output_value(yasm_value *value, unsigned char *buf, - unsigned int destsize, unsigned long offset, - yasm_bytecode *bc, int warn, /*@null@*/ void *d) + unsigned int destsize, unsigned long offset, + yasm_bytecode *bc, int warn, /*@null@*/ void *d) { /*@null@*/ coff_objfmt_output_info *info = (coff_objfmt_output_info *)d; yasm_objfmt_coff *objfmt_coff; @@ -442,215 +442,215 @@ coff_objfmt_output_value(yasm_value *value, unsigned char *buf, objfmt_coff = info->objfmt_coff; if (value->abs) - value->abs = yasm_expr_simplify(value->abs, 1); + value->abs = yasm_expr_simplify(value->abs, 1); /* Try to output constant and PC-relative section-local first. * Note this does NOT output any value with a SEG, WRT, external, * cross-section, or non-PC-relative reference (those are handled below). */ switch (yasm_value_output_basic(value, buf, destsize, bc, warn, - info->object->arch)) { - case -1: - return 1; - case 0: - break; - default: - return 0; + info->object->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; } /* Handle other expressions, with relocation if necessary */ if (value->rshift > 0 - || (value->seg_of && (value->wrt || value->curpos_rel)) - || (value->section_rel && (value->wrt || value->curpos_rel))) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("coff: relocation too complex")); - return 1; + || (value->seg_of && (value->wrt || value->curpos_rel)) + || (value->section_rel && (value->wrt || value->curpos_rel))) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("coff: relocation too complex")); + return 1; } intn_val = 0; intn_minus = 0; if (value->rel) { - yasm_sym_vis vis = yasm_symrec_get_visibility(value->rel); - /*@dependent@*/ /*@null@*/ yasm_symrec *sym = value->rel; - unsigned long addr; - coff_reloc *reloc; - - /* Sometimes we want the relocation to be generated against one - * symbol but the value generated correspond to a different symbol. - * This is done through (sym being referenced) WRT (sym used for - * reloc). Note both syms need to be in the same section! - */ - if (value->wrt) { - /*@dependent@*/ /*@null@*/ yasm_bytecode *rel_precbc, *wrt_precbc; - if (!yasm_symrec_get_label(sym, &rel_precbc) - || !yasm_symrec_get_label(value->wrt, &wrt_precbc)) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("coff: wrt expression too complex")); - return 1; - } - dist = yasm_calc_bc_dist(wrt_precbc, rel_precbc); - if (!dist) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("coff: cannot wrt across sections")); - return 1; - } - sym = value->wrt; - } - - if (vis & YASM_SYM_COMMON) { - /* In standard COFF, COMMON symbols have their length added in */ - if (!objfmt_coff->win32) { - /*@dependent@*/ /*@null@*/ coff_symrec_data *csymd; - /*@dependent@*/ /*@null@*/ yasm_expr **csize_expr; - /*@dependent@*/ /*@null@*/ yasm_intnum *common_size; - - csymd = yasm_symrec_get_data(sym, &coff_symrec_data_cb); - assert(csymd != NULL); - csize_expr = yasm_symrec_get_common_size(sym); - assert(csize_expr != NULL); - common_size = yasm_expr_get_intnum(csize_expr, 1); - if (!common_size) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("coff: common size too complex")); - return 1; - } - - if (yasm_intnum_sign(common_size) < 0) { - yasm_error_set(YASM_ERROR_VALUE, - N_("coff: common size is negative")); - return 1; - } - - intn_val += yasm_intnum_get_uint(common_size); - } - } else if (!(vis & YASM_SYM_EXTERN) && !objfmt_coff->win64) { - /*@dependent@*/ /*@null@*/ yasm_bytecode *sym_precbc; - - /* Local symbols need relocation to their section's start */ - if (yasm_symrec_get_label(sym, &sym_precbc)) { - yasm_section *sym_sect = yasm_bc_get_section(sym_precbc); - /*@null@*/ coff_section_data *sym_csd; - sym_csd = yasm_section_get_data(sym_sect, - &coff_section_data_cb); - assert(sym_csd != NULL); - sym = sym_csd->sym; - intn_val = yasm_bc_next_offset(sym_precbc); - if (COFF_SET_VMA) - intn_val += sym_csd->addr; - } - } - - if (value->curpos_rel) { - /* For standard COFF, need to adjust to start of section, e.g. - * subtract out the bytecode offset. - * For Win32 COFF, need to adjust based on value size and position. - * For Win64 COFF that's IP-relative, adjust to next bytecode; - * the difference between the offset+destsize and BC length is - * taken care of by special relocation types. - */ - if (objfmt_coff->win64 && value->ip_rel) - intn_val += bc->len*bc->mult_int; - else if (objfmt_coff->win32) - intn_val += offset+destsize; - else - intn_minus = bc->offset; - } - - if (value->seg_of || value->section_rel) { - /* Segment or section-relative generation; zero value. */ - intn_val = 0; - intn_minus = 0; - } - - /* Generate reloc */ - reloc = yasm_xmalloc(sizeof(coff_reloc)); - addr = bc->offset + offset; - if (COFF_SET_VMA) - addr += info->addr; - reloc->reloc.addr = yasm_intnum_create_uint(addr); - reloc->reloc.sym = sym; - - if (value->curpos_rel) { - if (objfmt_coff->machine == COFF_MACHINE_I386) { - if (valsize == 32) - reloc->type = COFF_RELOC_I386_REL32; - else { - yasm_error_set(YASM_ERROR_TYPE, - N_("coff: invalid relocation size")); - return 1; - } - } else if (objfmt_coff->machine == COFF_MACHINE_AMD64) { - if (valsize != 32) { - yasm_error_set(YASM_ERROR_TYPE, - N_("coff: invalid relocation size")); - return 1; - } - if (!value->ip_rel) - reloc->type = COFF_RELOC_AMD64_REL32; - else switch (bc->len*bc->mult_int - (offset+destsize)) { - case 0: - reloc->type = COFF_RELOC_AMD64_REL32; - break; - case 1: - reloc->type = COFF_RELOC_AMD64_REL32_1; - break; - case 2: - reloc->type = COFF_RELOC_AMD64_REL32_2; - break; - case 3: - reloc->type = COFF_RELOC_AMD64_REL32_3; - break; - case 4: - reloc->type = COFF_RELOC_AMD64_REL32_4; - break; - case 5: - reloc->type = COFF_RELOC_AMD64_REL32_5; - break; - default: - yasm_error_set(YASM_ERROR_TYPE, - N_("coff: invalid relocation size")); - return 1; - } - } else - yasm_internal_error(N_("coff objfmt: unrecognized machine")); - } else if (value->seg_of) { - if (objfmt_coff->machine == COFF_MACHINE_I386) - reloc->type = COFF_RELOC_I386_SECTION; - else if (objfmt_coff->machine == COFF_MACHINE_AMD64) - reloc->type = COFF_RELOC_AMD64_SECTION; - else - yasm_internal_error(N_("coff objfmt: unrecognized machine")); - } else if (value->section_rel) { - if (objfmt_coff->machine == COFF_MACHINE_I386) - reloc->type = COFF_RELOC_I386_SECREL; - else if (objfmt_coff->machine == COFF_MACHINE_AMD64) - reloc->type = COFF_RELOC_AMD64_SECREL; - else - yasm_internal_error(N_("coff objfmt: unrecognized machine")); - } else { - if (objfmt_coff->machine == COFF_MACHINE_I386) { - if (info->csd->flags2 & COFF_FLAG_NOBASE) - reloc->type = COFF_RELOC_I386_ADDR32NB; - else - reloc->type = COFF_RELOC_I386_ADDR32; - } else if (objfmt_coff->machine == COFF_MACHINE_AMD64) { - if (valsize == 32) { - if (info->csd->flags2 & COFF_FLAG_NOBASE) - reloc->type = COFF_RELOC_AMD64_ADDR32NB; - else - reloc->type = COFF_RELOC_AMD64_ADDR32; - } else if (valsize == 64) - reloc->type = COFF_RELOC_AMD64_ADDR64; - else { - yasm_error_set(YASM_ERROR_TYPE, - N_("coff: invalid relocation size")); - return 1; - } - } else - yasm_internal_error(N_("coff objfmt: unrecognized machine")); - } - info->csd->nreloc++; - yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); + yasm_sym_vis vis = yasm_symrec_get_visibility(value->rel); + /*@dependent@*/ /*@null@*/ yasm_symrec *sym = value->rel; + unsigned long addr; + coff_reloc *reloc; + + /* Sometimes we want the relocation to be generated against one + * symbol but the value generated correspond to a different symbol. + * This is done through (sym being referenced) WRT (sym used for + * reloc). Note both syms need to be in the same section! + */ + if (value->wrt) { + /*@dependent@*/ /*@null@*/ yasm_bytecode *rel_precbc, *wrt_precbc; + if (!yasm_symrec_get_label(sym, &rel_precbc) + || !yasm_symrec_get_label(value->wrt, &wrt_precbc)) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("coff: wrt expression too complex")); + return 1; + } + dist = yasm_calc_bc_dist(wrt_precbc, rel_precbc); + if (!dist) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("coff: cannot wrt across sections")); + return 1; + } + sym = value->wrt; + } + + if (vis & YASM_SYM_COMMON) { + /* In standard COFF, COMMON symbols have their length added in */ + if (!objfmt_coff->win32) { + /*@dependent@*/ /*@null@*/ coff_symrec_data *csymd; + /*@dependent@*/ /*@null@*/ yasm_expr **csize_expr; + /*@dependent@*/ /*@null@*/ yasm_intnum *common_size; + + csymd = yasm_symrec_get_data(sym, &coff_symrec_data_cb); + assert(csymd != NULL); + csize_expr = yasm_symrec_get_common_size(sym); + assert(csize_expr != NULL); + common_size = yasm_expr_get_intnum(csize_expr, 1); + if (!common_size) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("coff: common size too complex")); + return 1; + } + + if (yasm_intnum_sign(common_size) < 0) { + yasm_error_set(YASM_ERROR_VALUE, + N_("coff: common size is negative")); + return 1; + } + + intn_val += yasm_intnum_get_uint(common_size); + } + } else if (!(vis & YASM_SYM_EXTERN) && !objfmt_coff->win64) { + /*@dependent@*/ /*@null@*/ yasm_bytecode *sym_precbc; + + /* Local symbols need relocation to their section's start */ + if (yasm_symrec_get_label(sym, &sym_precbc)) { + yasm_section *sym_sect = yasm_bc_get_section(sym_precbc); + /*@null@*/ coff_section_data *sym_csd; + sym_csd = yasm_section_get_data(sym_sect, + &coff_section_data_cb); + assert(sym_csd != NULL); + sym = sym_csd->sym; + intn_val = yasm_bc_next_offset(sym_precbc); + if (COFF_SET_VMA) + intn_val += sym_csd->addr; + } + } + + if (value->curpos_rel) { + /* For standard COFF, need to adjust to start of section, e.g. + * subtract out the bytecode offset. + * For Win32 COFF, need to adjust based on value size and position. + * For Win64 COFF that's IP-relative, adjust to next bytecode; + * the difference between the offset+destsize and BC length is + * taken care of by special relocation types. + */ + if (objfmt_coff->win64 && value->ip_rel) + intn_val += bc->len*bc->mult_int; + else if (objfmt_coff->win32) + intn_val += offset+destsize; + else + intn_minus = bc->offset; + } + + if (value->seg_of || value->section_rel) { + /* Segment or section-relative generation; zero value. */ + intn_val = 0; + intn_minus = 0; + } + + /* Generate reloc */ + reloc = yasm_xmalloc(sizeof(coff_reloc)); + addr = bc->offset + offset; + if (COFF_SET_VMA) + addr += info->addr; + reloc->reloc.addr = yasm_intnum_create_uint(addr); + reloc->reloc.sym = sym; + + if (value->curpos_rel) { + if (objfmt_coff->machine == COFF_MACHINE_I386) { + if (valsize == 32) + reloc->type = COFF_RELOC_I386_REL32; + else { + yasm_error_set(YASM_ERROR_TYPE, + N_("coff: invalid relocation size")); + return 1; + } + } else if (objfmt_coff->machine == COFF_MACHINE_AMD64) { + if (valsize != 32) { + yasm_error_set(YASM_ERROR_TYPE, + N_("coff: invalid relocation size")); + return 1; + } + if (!value->ip_rel) + reloc->type = COFF_RELOC_AMD64_REL32; + else switch (bc->len*bc->mult_int - (offset+destsize)) { + case 0: + reloc->type = COFF_RELOC_AMD64_REL32; + break; + case 1: + reloc->type = COFF_RELOC_AMD64_REL32_1; + break; + case 2: + reloc->type = COFF_RELOC_AMD64_REL32_2; + break; + case 3: + reloc->type = COFF_RELOC_AMD64_REL32_3; + break; + case 4: + reloc->type = COFF_RELOC_AMD64_REL32_4; + break; + case 5: + reloc->type = COFF_RELOC_AMD64_REL32_5; + break; + default: + yasm_error_set(YASM_ERROR_TYPE, + N_("coff: invalid relocation size")); + return 1; + } + } else + yasm_internal_error(N_("coff objfmt: unrecognized machine")); + } else if (value->seg_of) { + if (objfmt_coff->machine == COFF_MACHINE_I386) + reloc->type = COFF_RELOC_I386_SECTION; + else if (objfmt_coff->machine == COFF_MACHINE_AMD64) + reloc->type = COFF_RELOC_AMD64_SECTION; + else + yasm_internal_error(N_("coff objfmt: unrecognized machine")); + } else if (value->section_rel) { + if (objfmt_coff->machine == COFF_MACHINE_I386) + reloc->type = COFF_RELOC_I386_SECREL; + else if (objfmt_coff->machine == COFF_MACHINE_AMD64) + reloc->type = COFF_RELOC_AMD64_SECREL; + else + yasm_internal_error(N_("coff objfmt: unrecognized machine")); + } else { + if (objfmt_coff->machine == COFF_MACHINE_I386) { + if (info->csd->flags2 & COFF_FLAG_NOBASE) + reloc->type = COFF_RELOC_I386_ADDR32NB; + else + reloc->type = COFF_RELOC_I386_ADDR32; + } else if (objfmt_coff->machine == COFF_MACHINE_AMD64) { + if (valsize == 32) { + if (info->csd->flags2 & COFF_FLAG_NOBASE) + reloc->type = COFF_RELOC_AMD64_ADDR32NB; + else + reloc->type = COFF_RELOC_AMD64_ADDR32; + } else if (valsize == 64) + reloc->type = COFF_RELOC_AMD64_ADDR64; + else { + yasm_error_set(YASM_ERROR_TYPE, + N_("coff: invalid relocation size")); + return 1; + } + } else + yasm_internal_error(N_("coff objfmt: unrecognized machine")); + } + info->csd->nreloc++; + yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); } /* Build up final integer output from intn_val, intn_minus, value->abs, @@ -658,32 +658,32 @@ coff_objfmt_output_value(yasm_value *value, unsigned char *buf, * intnums above (except for dist). */ if (intn_minus <= intn_val) - intn = yasm_intnum_create_uint(intn_val-intn_minus); + intn = yasm_intnum_create_uint(intn_val-intn_minus); else { - intn = yasm_intnum_create_uint(intn_minus-intn_val); - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + intn = yasm_intnum_create_uint(intn_minus-intn_val); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); } if (value->abs) { - yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); - if (!intn2) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("coff: relocation too complex")); - yasm_intnum_destroy(intn); - if (dist) - yasm_intnum_destroy(dist); - return 1; - } - yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); + yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); + if (!intn2) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("coff: relocation too complex")); + yasm_intnum_destroy(intn); + if (dist) + yasm_intnum_destroy(dist); + return 1; + } + yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); } if (dist) { - yasm_intnum_calc(intn, YASM_EXPR_ADD, dist); - yasm_intnum_destroy(dist); + yasm_intnum_calc(intn, YASM_EXPR_ADD, dist); + yasm_intnum_destroy(dist); } retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize, - valsize, 0, bc, warn); + valsize, 0, bc, warn); yasm_intnum_destroy(intn); return retval; } @@ -699,38 +699,38 @@ coff_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) assert(info != NULL); bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info, - coff_objfmt_output_value, NULL); + coff_objfmt_output_value, NULL); /* Don't bother doing anything else if size ended up being 0. */ if (size == 0) { - if (bigbuf) - yasm_xfree(bigbuf); - return 0; + if (bigbuf) + yasm_xfree(bigbuf); + return 0; } info->csd->size += size; /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { - unsigned long left; - yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, - N_("uninitialized space declared in code/data section: zeroing")); - /* Write out in chunks */ - memset(info->buf, 0, REGULAR_OUTBUF_SIZE); - left = size; - while (left > REGULAR_OUTBUF_SIZE) { - fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); - left -= REGULAR_OUTBUF_SIZE; - } - fwrite(info->buf, left, 1, info->f); + unsigned long left; + yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, + N_("uninitialized space declared in code/data section: zeroing")); + /* Write out in chunks */ + memset(info->buf, 0, REGULAR_OUTBUF_SIZE); + left = size; + while (left > REGULAR_OUTBUF_SIZE) { + fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); + left -= REGULAR_OUTBUF_SIZE; + } + fwrite(info->buf, left, 1, info->f); } else { - /* Output buf (or bigbuf if non-NULL) to file */ - fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f); + /* Output buf (or bigbuf if non-NULL) to file */ + fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f); } /* If bigbuf was allocated, free it */ if (bigbuf) - yasm_xfree(bigbuf); + yasm_xfree(bigbuf); return 0; } @@ -746,7 +746,7 @@ coff_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); csd = yasm_section_get_data(sect, &coff_section_data_cb); @@ -754,58 +754,58 @@ coff_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d) /* Add to strtab if in win32 format and name > 8 chars */ if (info->objfmt_coff->win32) { - size_t namelen = strlen(yasm_section_get_name(sect)); - if (namelen > 8) { - csd->strtab_name = info->strtab_offset; - info->strtab_offset += (unsigned long)(namelen + 1); - } + size_t namelen = strlen(yasm_section_get_name(sect)); + if (namelen > 8) { + csd->strtab_name = info->strtab_offset; + info->strtab_offset += (unsigned long)(namelen + 1); + } } if (!csd->isdebug) - csd->addr = info->addr; + csd->addr = info->addr; if ((csd->flags & COFF_STYP_STD_MASK) == COFF_STYP_BSS) { - /* Don't output BSS sections. - * TODO: Check for non-reserve bytecodes? - */ - pos = 0; /* position = 0 because it's not in the file */ - csd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); + /* Don't output BSS sections. + * TODO: Check for non-reserve bytecodes? + */ + pos = 0; /* position = 0 because it's not in the file */ + csd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); } else { - pos = ftell(info->f); - if (pos == -1) { - yasm__fatal(N_("could not get file position on output file")); - /*@notreached@*/ - return 1; - } - - info->sect = sect; - info->csd = csd; - yasm_section_bcs_traverse(sect, info->errwarns, info, - coff_objfmt_output_bytecode); - - /* Sanity check final section size */ - if (csd->size != yasm_bc_next_offset(yasm_section_bcs_last(sect))) - yasm_internal_error( - N_("coff: section computed size did not match actual size")); + pos = ftell(info->f); + if (pos == -1) { + yasm__fatal(N_("could not get file position on output file")); + /*@notreached@*/ + return 1; + } + + info->sect = sect; + info->csd = csd; + yasm_section_bcs_traverse(sect, info->errwarns, info, + coff_objfmt_output_bytecode); + + /* Sanity check final section size */ + if (csd->size != yasm_bc_next_offset(yasm_section_bcs_last(sect))) + yasm_internal_error( + N_("coff: section computed size did not match actual size")); } /* Empty? Go on to next section */ if (csd->size == 0) - return 0; + return 0; if (!csd->isdebug) - info->addr += csd->size; + info->addr += csd->size; csd->scnptr = (unsigned long)pos; /* No relocations to output? Go on to next section */ if (csd->nreloc == 0) - return 0; + return 0; pos = ftell(info->f); if (pos == -1) { - yasm__fatal(N_("could not get file position on output file")); - /*@notreached@*/ - return 1; + yasm__fatal(N_("could not get file position on output file")); + /*@notreached@*/ + return 1; } csd->relptr = (unsigned long)pos; @@ -813,30 +813,30 @@ coff_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d) * (NRELOC_OVFL) and the first relocation contains the number of relocs. */ if (csd->nreloc >= 64*1024 && info->objfmt_coff->win32) { - localbuf = info->buf; - YASM_WRITE_32_L(localbuf, csd->nreloc+1); /* address of relocation */ - YASM_WRITE_32_L(localbuf, 0); /* relocated symbol */ - YASM_WRITE_16_L(localbuf, 0); /* type of relocation */ - fwrite(info->buf, 10, 1, info->f); + localbuf = info->buf; + YASM_WRITE_32_L(localbuf, csd->nreloc+1); /* address of relocation */ + YASM_WRITE_32_L(localbuf, 0); /* relocated symbol */ + YASM_WRITE_16_L(localbuf, 0); /* type of relocation */ + fwrite(info->buf, 10, 1, info->f); } reloc = (coff_reloc *)yasm_section_relocs_first(sect); while (reloc) { - /*@null@*/ coff_symrec_data *csymd; - localbuf = info->buf; + /*@null@*/ coff_symrec_data *csymd; + localbuf = info->buf; - csymd = yasm_symrec_get_data(reloc->reloc.sym, &coff_symrec_data_cb); - if (!csymd) - yasm_internal_error( - N_("coff: no symbol data for relocated symbol")); + csymd = yasm_symrec_get_data(reloc->reloc.sym, &coff_symrec_data_cb); + if (!csymd) + yasm_internal_error( + N_("coff: no symbol data for relocated symbol")); - yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); - localbuf += 4; /* address of relocation */ - YASM_WRITE_32_L(localbuf, csymd->index); /* relocated symbol */ - YASM_WRITE_16_L(localbuf, reloc->type); /* type of relocation */ - fwrite(info->buf, 10, 1, info->f); + yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); + localbuf += 4; /* address of relocation */ + YASM_WRITE_32_L(localbuf, csymd->index); /* relocated symbol */ + YASM_WRITE_16_L(localbuf, reloc->type); /* type of relocation */ + fwrite(info->buf, 10, 1, info->f); - reloc = (coff_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); + reloc = (coff_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); } return 0; @@ -851,7 +851,7 @@ coff_objfmt_output_sectstr(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; /* Add to strtab if in win32 format and name > 8 chars */ if (!info->objfmt_coff->win32) @@ -860,7 +860,7 @@ coff_objfmt_output_sectstr(yasm_section *sect, /*@null@*/ void *d) name = yasm_section_get_name(sect); len = strlen(name); if (len > 8) - fwrite(name, len+1, 1, info->f); + fwrite(name, len+1, 1, info->f); return 0; } @@ -875,7 +875,7 @@ coff_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); objfmt_coff = info->objfmt_coff; @@ -884,53 +884,53 @@ coff_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) /* Check to see if alignment is supported size */ if (align > 8192) - align = 8192; + align = 8192; /* Convert alignment into flags setting */ csd->flags &= ~COFF_STYP_ALIGN_MASK; while (align != 0) { - csd->flags += 1<>= 1; + csd->flags += 1<>= 1; } /* section name */ localbuf = info->buf; if (strlen(yasm_section_get_name(sect)) > 8) { - char namenum[30]; - sprintf(namenum, "/%ld", csd->strtab_name); - strncpy((char *)localbuf, namenum, 8); + char namenum[30]; + sprintf(namenum, "/%ld", csd->strtab_name); + strncpy((char *)localbuf, namenum, 8); } else - strncpy((char *)localbuf, yasm_section_get_name(sect), 8); + strncpy((char *)localbuf, yasm_section_get_name(sect), 8); localbuf += 8; if (csd->isdebug) { - YASM_WRITE_32_L(localbuf, 0); /* physical address */ - YASM_WRITE_32_L(localbuf, 0); /* virtual address */ + YASM_WRITE_32_L(localbuf, 0); /* physical address */ + YASM_WRITE_32_L(localbuf, 0); /* virtual address */ } else { - YASM_WRITE_32_L(localbuf, csd->addr); /* physical address */ - if (COFF_SET_VMA) - YASM_WRITE_32_L(localbuf, csd->addr);/* virtual address */ - else - YASM_WRITE_32_L(localbuf, 0); /* virtual address */ + YASM_WRITE_32_L(localbuf, csd->addr); /* physical address */ + if (COFF_SET_VMA) + YASM_WRITE_32_L(localbuf, csd->addr);/* virtual address */ + else + YASM_WRITE_32_L(localbuf, 0); /* virtual address */ } - YASM_WRITE_32_L(localbuf, csd->size); /* section size */ - YASM_WRITE_32_L(localbuf, csd->scnptr); /* file ptr to data */ - YASM_WRITE_32_L(localbuf, csd->relptr); /* file ptr to relocs */ - YASM_WRITE_32_L(localbuf, 0); /* file ptr to line nums */ + YASM_WRITE_32_L(localbuf, csd->size); /* section size */ + YASM_WRITE_32_L(localbuf, csd->scnptr); /* file ptr to data */ + YASM_WRITE_32_L(localbuf, csd->relptr); /* file ptr to relocs */ + YASM_WRITE_32_L(localbuf, 0); /* file ptr to line nums */ if (csd->nreloc >= 64*1024) { - /* Win32/64 has special handling for this case. */ - if (objfmt_coff->win32) - csd->flags |= COFF_STYP_NRELOC_OVFL; - else { - yasm_warn_set(YASM_WARN_GENERAL, - N_("too many relocations in section `%s'"), - yasm_section_get_name(sect)); - yasm_errwarn_propagate(info->errwarns, 0); - } - YASM_WRITE_16_L(localbuf, 0xFFFF); /* max out */ + /* Win32/64 has special handling for this case. */ + if (objfmt_coff->win32) + csd->flags |= COFF_STYP_NRELOC_OVFL; + else { + yasm_warn_set(YASM_WARN_GENERAL, + N_("too many relocations in section `%s'"), + yasm_section_get_name(sect)); + yasm_errwarn_propagate(info->errwarns, 0); + } + YASM_WRITE_16_L(localbuf, 0xFFFF); /* max out */ } else - YASM_WRITE_16_L(localbuf, csd->nreloc); /* num of relocation entries */ - YASM_WRITE_16_L(localbuf, 0); /* num of line number entries */ - YASM_WRITE_32_L(localbuf, csd->flags); /* flags */ + YASM_WRITE_16_L(localbuf, csd->nreloc); /* num of relocation entries */ + YASM_WRITE_16_L(localbuf, 0); /* num of line number entries */ + YASM_WRITE_32_L(localbuf, csd->flags); /* flags */ fwrite(info->buf, 40, 1, info->f); return 0; @@ -947,18 +947,18 @@ coff_objfmt_count_sym(yasm_symrec *sym, /*@null@*/ void *d) sym_data = yasm_symrec_get_data(sym, &coff_symrec_data_cb); if ((vis & (YASM_SYM_EXTERN|YASM_SYM_GLOBAL|YASM_SYM_COMMON)) && !sym_data) - sym_data = coff_objfmt_sym_set_data(sym, COFF_SCL_EXT, 0, - COFF_SYMTAB_AUX_NONE); + sym_data = coff_objfmt_sym_set_data(sym, COFF_SCL_EXT, 0, + COFF_SYMTAB_AUX_NONE); if (info->all_syms || vis != YASM_SYM_LOCAL) { - /* Save index in symrec data */ - if (!sym_data) { - sym_data = coff_objfmt_sym_set_data(sym, COFF_SCL_STAT, 0, - COFF_SYMTAB_AUX_NONE); - } - sym_data->index = info->indx; - - info->indx += sym_data->numaux + 1; + /* Save index in symrec data */ + if (!sym_data) { + sym_data = coff_objfmt_sym_set_data(sym, COFF_SCL_STAT, 0, + COFF_SYMTAB_AUX_NONE); + } + sym_data->index = info->indx; + + info->indx += sym_data->numaux + 1; } return 0; } @@ -973,139 +973,139 @@ coff_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d) /* Don't output local syms unless outputting all syms */ if (info->all_syms || vis != YASM_SYM_LOCAL) { - const char *name = yasm_symrec_get_name(sym); - const yasm_expr *equ_val; - const yasm_intnum *intn; - unsigned char *localbuf; - size_t len = strlen(name); - int aux; - /*@dependent@*/ /*@null@*/ coff_symrec_data *csymd; - unsigned long value = 0; - unsigned int scnum = 0xfffe; /* -2 = debugging symbol */ - /*@dependent@*/ /*@null@*/ yasm_section *sect; - /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; - unsigned long scnlen = 0; /* for sect auxent */ - unsigned long nreloc = 0; /* for sect auxent */ - yasm_objfmt_coff *objfmt_coff = info->objfmt_coff; - - /* Get symrec's of_data (needed for storage class) */ - csymd = yasm_symrec_get_data(sym, &coff_symrec_data_cb); - if (!csymd) - yasm_internal_error(N_("coff: expected sym data to be present")); - - /* Look at symrec for value/scnum/etc. */ - if (yasm_symrec_get_label(sym, &precbc)) { - if (precbc) - sect = yasm_bc_get_section(precbc); - else - sect = NULL; - /* it's a label: get value and offset. - * If there is not a section, leave as debugging symbol. - */ - if (sect) { - /*@dependent@*/ /*@null@*/ coff_section_data *csectd; - csectd = yasm_section_get_data(sect, &coff_section_data_cb); - if (csectd) { - scnum = csectd->scnum; - scnlen = csectd->size; - nreloc = csectd->nreloc; - if (COFF_SET_VMA) - value = csectd->addr; - } else if (yasm_section_is_absolute(sect)) { - yasm_expr *abs_start; - - abs_start = yasm_expr_copy(yasm_section_get_start(sect)); - intn = yasm_expr_get_intnum(&abs_start, 1); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("absolute section start not an integer expression")); - yasm_errwarn_propagate(info->errwarns, - abs_start->line); - } else - value = yasm_intnum_get_uint(intn); - yasm_expr_destroy(abs_start); - - scnum = 0xffff; /* -1 = absolute symbol */ - } else - yasm_internal_error(N_("didn't understand section")); - if (precbc) - value += yasm_bc_next_offset(precbc); - } - } else if ((equ_val = yasm_symrec_get_equ(sym))) { - yasm_expr *equ_val_copy = yasm_expr_copy(equ_val); - intn = yasm_expr_get_intnum(&equ_val_copy, 1); - if (!intn) { - if (vis & YASM_SYM_GLOBAL) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("global EQU value not an integer expression")); - yasm_errwarn_propagate(info->errwarns, equ_val->line); - } - } else - value = yasm_intnum_get_uint(intn); - yasm_expr_destroy(equ_val_copy); - - scnum = 0xffff; /* -1 = absolute symbol */ - } else { - if (vis & YASM_SYM_COMMON) { - /*@dependent@*/ /*@null@*/ yasm_expr **csize_expr; - csize_expr = yasm_symrec_get_common_size(sym); - assert(csize_expr != NULL); - intn = yasm_expr_get_intnum(csize_expr, 1); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("COMMON data size not an integer expression")); - yasm_errwarn_propagate(info->errwarns, - (*csize_expr)->line); - } else - value = yasm_intnum_get_uint(intn); - scnum = 0; - } - if (vis & YASM_SYM_EXTERN) - scnum = 0; - } - - localbuf = info->buf; - if (len > 8) { - YASM_WRITE_32_L(localbuf, 0); /* "zeros" field */ - YASM_WRITE_32_L(localbuf, info->strtab_offset); /* strtab offset */ - info->strtab_offset += (unsigned long)(len+1); - } else { - /* <8 chars, so no string table entry needed */ - strncpy((char *)localbuf, name, 8); - localbuf += 8; - } - YASM_WRITE_32_L(localbuf, value); /* value */ - YASM_WRITE_16_L(localbuf, scnum); /* section number */ - YASM_WRITE_16_L(localbuf, 0); /* type is always zero (for now) */ - YASM_WRITE_8(localbuf, csymd->sclass); /* storage class */ - YASM_WRITE_8(localbuf, csymd->numaux); /* number of aux entries */ - fwrite(info->buf, 18, 1, info->f); - for (aux=0; auxnumaux; aux++) { - localbuf = info->buf; - memset(localbuf, 0, 18); - switch (csymd->auxtype) { - case COFF_SYMTAB_AUX_NONE: - break; - case COFF_SYMTAB_AUX_SECT: - YASM_WRITE_32_L(localbuf, scnlen); /* section length */ - YASM_WRITE_16_L(localbuf, nreloc); /* number relocs */ - YASM_WRITE_16_L(localbuf, 0); /* number line nums */ - break; - case COFF_SYMTAB_AUX_FILE: - len = strlen(csymd->aux[0].fname); - if (len > 14) { - YASM_WRITE_32_L(localbuf, 0); - YASM_WRITE_32_L(localbuf, info->strtab_offset); - info->strtab_offset += (unsigned long)(len+1); - } else - strncpy((char *)localbuf, csymd->aux[0].fname, 14); - break; - default: - yasm_internal_error( - N_("coff: unrecognized aux symtab type")); - } - fwrite(info->buf, 18, 1, info->f); - } + const char *name = yasm_symrec_get_name(sym); + const yasm_expr *equ_val; + const yasm_intnum *intn; + unsigned char *localbuf; + size_t len = strlen(name); + int aux; + /*@dependent@*/ /*@null@*/ coff_symrec_data *csymd; + unsigned long value = 0; + unsigned int scnum = 0xfffe; /* -2 = debugging symbol */ + /*@dependent@*/ /*@null@*/ yasm_section *sect; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; + unsigned long scnlen = 0; /* for sect auxent */ + unsigned long nreloc = 0; /* for sect auxent */ + yasm_objfmt_coff *objfmt_coff = info->objfmt_coff; + + /* Get symrec's of_data (needed for storage class) */ + csymd = yasm_symrec_get_data(sym, &coff_symrec_data_cb); + if (!csymd) + yasm_internal_error(N_("coff: expected sym data to be present")); + + /* Look at symrec for value/scnum/etc. */ + if (yasm_symrec_get_label(sym, &precbc)) { + if (precbc) + sect = yasm_bc_get_section(precbc); + else + sect = NULL; + /* it's a label: get value and offset. + * If there is not a section, leave as debugging symbol. + */ + if (sect) { + /*@dependent@*/ /*@null@*/ coff_section_data *csectd; + csectd = yasm_section_get_data(sect, &coff_section_data_cb); + if (csectd) { + scnum = csectd->scnum; + scnlen = csectd->size; + nreloc = csectd->nreloc; + if (COFF_SET_VMA) + value = csectd->addr; + } else if (yasm_section_is_absolute(sect)) { + yasm_expr *abs_start; + + abs_start = yasm_expr_copy(yasm_section_get_start(sect)); + intn = yasm_expr_get_intnum(&abs_start, 1); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("absolute section start not an integer expression")); + yasm_errwarn_propagate(info->errwarns, + abs_start->line); + } else + value = yasm_intnum_get_uint(intn); + yasm_expr_destroy(abs_start); + + scnum = 0xffff; /* -1 = absolute symbol */ + } else + yasm_internal_error(N_("didn't understand section")); + if (precbc) + value += yasm_bc_next_offset(precbc); + } + } else if ((equ_val = yasm_symrec_get_equ(sym))) { + yasm_expr *equ_val_copy = yasm_expr_copy(equ_val); + intn = yasm_expr_get_intnum(&equ_val_copy, 1); + if (!intn) { + if (vis & YASM_SYM_GLOBAL) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("global EQU value not an integer expression")); + yasm_errwarn_propagate(info->errwarns, equ_val->line); + } + } else + value = yasm_intnum_get_uint(intn); + yasm_expr_destroy(equ_val_copy); + + scnum = 0xffff; /* -1 = absolute symbol */ + } else { + if (vis & YASM_SYM_COMMON) { + /*@dependent@*/ /*@null@*/ yasm_expr **csize_expr; + csize_expr = yasm_symrec_get_common_size(sym); + assert(csize_expr != NULL); + intn = yasm_expr_get_intnum(csize_expr, 1); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("COMMON data size not an integer expression")); + yasm_errwarn_propagate(info->errwarns, + (*csize_expr)->line); + } else + value = yasm_intnum_get_uint(intn); + scnum = 0; + } + if (vis & YASM_SYM_EXTERN) + scnum = 0; + } + + localbuf = info->buf; + if (len > 8) { + YASM_WRITE_32_L(localbuf, 0); /* "zeros" field */ + YASM_WRITE_32_L(localbuf, info->strtab_offset); /* strtab offset */ + info->strtab_offset += (unsigned long)(len+1); + } else { + /* <8 chars, so no string table entry needed */ + strncpy((char *)localbuf, name, 8); + localbuf += 8; + } + YASM_WRITE_32_L(localbuf, value); /* value */ + YASM_WRITE_16_L(localbuf, scnum); /* section number */ + YASM_WRITE_16_L(localbuf, 0); /* type is always zero (for now) */ + YASM_WRITE_8(localbuf, csymd->sclass); /* storage class */ + YASM_WRITE_8(localbuf, csymd->numaux); /* number of aux entries */ + fwrite(info->buf, 18, 1, info->f); + for (aux=0; auxnumaux; aux++) { + localbuf = info->buf; + memset(localbuf, 0, 18); + switch (csymd->auxtype) { + case COFF_SYMTAB_AUX_NONE: + break; + case COFF_SYMTAB_AUX_SECT: + YASM_WRITE_32_L(localbuf, scnlen); /* section length */ + YASM_WRITE_16_L(localbuf, nreloc); /* number relocs */ + YASM_WRITE_16_L(localbuf, 0); /* number line nums */ + break; + case COFF_SYMTAB_AUX_FILE: + len = strlen(csymd->aux[0].fname); + if (len > 14) { + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, info->strtab_offset); + info->strtab_offset += (unsigned long)(len+1); + } else + strncpy((char *)localbuf, csymd->aux[0].fname, 14); + break; + default: + yasm_internal_error( + N_("coff: unrecognized aux symtab type")); + } + fwrite(info->buf, 18, 1, info->f); + } } return 0; } @@ -1120,35 +1120,35 @@ coff_objfmt_output_str(yasm_symrec *sym, /*@null@*/ void *d) /* Don't output local syms unless outputting all syms */ if (info->all_syms || vis != YASM_SYM_LOCAL) { - const char *name = yasm_symrec_get_name(sym); - /*@dependent@*/ /*@null@*/ coff_symrec_data *csymd; - size_t len = strlen(name); - int aux; - - csymd = yasm_symrec_get_data(sym, &coff_symrec_data_cb); - if (!csymd) - yasm_internal_error(N_("coff: expected sym data to be present")); - - if (len > 8) - fwrite(name, len+1, 1, info->f); - for (aux=0; auxnumaux; aux++) { - switch (csymd->auxtype) { - case COFF_SYMTAB_AUX_FILE: - len = strlen(csymd->aux[0].fname); - if (len > 14) - fwrite(csymd->aux[0].fname, len+1, 1, info->f); - break; - default: - break; - } - } + const char *name = yasm_symrec_get_name(sym); + /*@dependent@*/ /*@null@*/ coff_symrec_data *csymd; + size_t len = strlen(name); + int aux; + + csymd = yasm_symrec_get_data(sym, &coff_symrec_data_cb); + if (!csymd) + yasm_internal_error(N_("coff: expected sym data to be present")); + + if (len > 8) + fwrite(name, len+1, 1, info->f); + for (aux=0; auxnumaux; aux++) { + switch (csymd->auxtype) { + case COFF_SYMTAB_AUX_FILE: + len = strlen(csymd->aux[0].fname); + if (len > 14) + fwrite(csymd->aux[0].fname, len+1, 1, info->f); + break; + default: + break; + } + } } return 0; } static void coff_objfmt_output(yasm_object *object, FILE *f, int all_syms, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; coff_objfmt_output_info info; @@ -1160,18 +1160,18 @@ coff_objfmt_output(yasm_object *object, FILE *f, int all_syms, unsigned long ts; if (objfmt_coff->proc_frame) { - yasm_error_set_xref(objfmt_coff->proc_frame, - N_("procedure started here")); - yasm_error_set(YASM_ERROR_GENERAL, - N_("end of file in procedure frame")); - yasm_errwarn_propagate(errwarns, 0); - return; + yasm_error_set_xref(objfmt_coff->proc_frame, + N_("procedure started here")); + yasm_error_set(YASM_ERROR_GENERAL, + N_("end of file in procedure frame")); + yasm_errwarn_propagate(errwarns, 0); + return; } if (objfmt_coff->filesym_data->aux[0].fname) - yasm_xfree(objfmt_coff->filesym_data->aux[0].fname); + yasm_xfree(objfmt_coff->filesym_data->aux[0].fname); objfmt_coff->filesym_data->aux[0].fname = - yasm__xstrdup(object->src_filename); + yasm__xstrdup(object->src_filename); /* Force all syms for win64 because they're needed for relocations. * FIXME: Not *all* syms need to be output, only the ones needed for @@ -1190,13 +1190,13 @@ coff_objfmt_output(yasm_object *object, FILE *f, int all_syms, * we've not initialized so far. */ yasm_object_sections_traverse(object, &info, - coff_objfmt_init_remaining_section); + coff_objfmt_init_remaining_section); /* Allocate space for headers by seeking forward */ if (fseek(f, (long)(20+40*(objfmt_coff->parse_scnum-1)), SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } /* Finalize symbol table (assign index to each symbol) */ @@ -1207,27 +1207,27 @@ coff_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Section data/relocs */ if (COFF_SET_VMA) { - /* If we're setting the VMA, we need to do a first section pass to - * determine each section's addr value before actually outputting - * relocations, as a relocation's section address is added into the - * addends in the generated code. - */ - info.addr = 0; - if (yasm_object_sections_traverse(object, &info, - coff_objfmt_set_section_addr)) - return; + /* If we're setting the VMA, we need to do a first section pass to + * determine each section's addr value before actually outputting + * relocations, as a relocation's section address is added into the + * addends in the generated code. + */ + info.addr = 0; + if (yasm_object_sections_traverse(object, &info, + coff_objfmt_set_section_addr)) + return; } info.addr = 0; if (yasm_object_sections_traverse(object, &info, - coff_objfmt_output_section)) - return; + coff_objfmt_output_section)) + return; /* Symbol table */ pos = ftell(f); if (pos == -1) { - yasm__fatal(N_("could not get file position on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not get file position on output file")); + /*@notreached@*/ + return; } symtab_pos = (unsigned long)pos; yasm_symtab_traverse(object->symtab, &info, coff_objfmt_output_sym); @@ -1239,30 +1239,30 @@ coff_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Write headers */ if (fseek(f, 0, SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } localbuf = info.buf; - YASM_WRITE_16_L(localbuf, objfmt_coff->machine); /* magic number */ + YASM_WRITE_16_L(localbuf, objfmt_coff->machine); /* magic number */ YASM_WRITE_16_L(localbuf, objfmt_coff->parse_scnum-1);/* number of sects */ if (getenv("YASM_TEST_SUITE")) - ts = 0; + ts = 0; else - ts = (unsigned long)time(NULL); - YASM_WRITE_32_L(localbuf, ts); /* time/date stamp */ - YASM_WRITE_32_L(localbuf, symtab_pos); /* file ptr to symtab */ - YASM_WRITE_32_L(localbuf, symtab_count); /* number of symtabs */ - YASM_WRITE_16_L(localbuf, 0); /* size of optional header (none) */ + ts = (unsigned long)time(NULL); + YASM_WRITE_32_L(localbuf, ts); /* time/date stamp */ + YASM_WRITE_32_L(localbuf, symtab_pos); /* file ptr to symtab */ + YASM_WRITE_32_L(localbuf, symtab_count); /* number of symtabs */ + YASM_WRITE_16_L(localbuf, 0); /* size of optional header (none) */ /* flags */ flags = 0; if (strcmp(yasm_dbgfmt_keyword(object->dbgfmt), "null")==0) - flags = COFF_F_LNNO; + flags = COFF_F_LNNO; if (!all_syms) - flags |= COFF_F_LSYMS; + flags |= COFF_F_LSYMS; if (objfmt_coff->machine != COFF_MACHINE_AMD64) - flags |= COFF_F_AR32WR; + flags |= COFF_F_AR32WR; YASM_WRITE_16_L(localbuf, flags); fwrite(info.buf, 20, 1, f); @@ -1276,9 +1276,9 @@ coff_objfmt_destroy(yasm_objfmt *objfmt) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)objfmt; if (objfmt_coff->filesym_data->aux[0].fname) - yasm_xfree(objfmt_coff->filesym_data->aux[0].fname); + yasm_xfree(objfmt_coff->filesym_data->aux[0].fname); if (objfmt_coff->unwind) - yasm_win64__uwinfo_destroy(objfmt_coff->unwind); + yasm_win64__uwinfo_destroy(objfmt_coff->unwind); yasm_xfree(objfmt); } @@ -1292,20 +1292,20 @@ coff_objfmt_add_default_section(yasm_object *object) retval = yasm_object_get_general(object, ".text", 0, 16, 1, 0, &isnew, 0); if (isnew) { - csd = coff_objfmt_init_new_section(object, retval, ".text", 0); - csd->flags = COFF_STYP_TEXT; - if (objfmt_coff->win32) - csd->flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; - yasm_section_set_default(retval, 1); + csd = coff_objfmt_init_new_section(object, retval, ".text", 0); + csd->flags = COFF_STYP_TEXT; + if (objfmt_coff->win32) + csd->flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; + yasm_section_set_default(retval, 1); } return retval; } static /*@observer@*/ /*@null@*/ yasm_section * coff_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, - unsigned long line) + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparam *vp = yasm_vps_first(valparams); @@ -1321,284 +1321,284 @@ coff_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, coff_section_data *csd; static const struct { - const char *name; - unsigned long stdflags; /* if 0, win32 only qualifier */ - unsigned long win32flags; - unsigned long flags2; - /* Mode: 0 => clear specified bits - * 1 => set specified bits - * 2 => clear all bits, then set specified bits - */ - int mode; + const char *name; + unsigned long stdflags; /* if 0, win32 only qualifier */ + unsigned long win32flags; + unsigned long flags2; + /* Mode: 0 => clear specified bits + * 1 => set specified bits + * 2 => clear all bits, then set specified bits + */ + int mode; } flagquals[] = { - { "code", COFF_STYP_TEXT, COFF_STYP_EXECUTE | COFF_STYP_READ, 0, 2 }, - { "text", COFF_STYP_TEXT, COFF_STYP_EXECUTE | COFF_STYP_READ, 0, 2 }, - { "data", COFF_STYP_DATA, COFF_STYP_READ | COFF_STYP_WRITE, 0, 2 }, - { "bss", COFF_STYP_BSS, COFF_STYP_READ | COFF_STYP_WRITE, 0, 2 }, - { "info", COFF_STYP_INFO, COFF_STYP_DISCARD | COFF_STYP_READ, 0, 2 }, - { "discard", 0, COFF_STYP_DISCARD, 0, 1 }, - { "nodiscard", 0, COFF_STYP_DISCARD, 0, 0 }, - { "cache", 0, COFF_STYP_NOCACHE, 0, 0 }, - { "nocache", 0, COFF_STYP_NOCACHE, 0, 1 }, - { "page", 0, COFF_STYP_NOPAGE, 0, 0 }, - { "nopage", 0, COFF_STYP_NOPAGE, 0, 1 }, - { "share", 0, COFF_STYP_SHARED, 0, 1 }, - { "noshare", 0, COFF_STYP_SHARED, 0, 0 }, - { "execute", 0, COFF_STYP_EXECUTE, 0, 1 }, - { "noexecute", 0, COFF_STYP_EXECUTE, 0, 0 }, - { "read", 0, COFF_STYP_READ, 0, 1 }, - { "noread", 0, COFF_STYP_READ, 0, 0 }, - { "write", 0, COFF_STYP_WRITE, 0, 1 }, - { "nowrite", 0, COFF_STYP_WRITE, 0, 0 }, - { "base", 0, 0, COFF_FLAG_NOBASE, 0 }, - { "nobase", 0, 0, COFF_FLAG_NOBASE, 1 }, + { "code", COFF_STYP_TEXT, COFF_STYP_EXECUTE | COFF_STYP_READ, 0, 2 }, + { "text", COFF_STYP_TEXT, COFF_STYP_EXECUTE | COFF_STYP_READ, 0, 2 }, + { "data", COFF_STYP_DATA, COFF_STYP_READ | COFF_STYP_WRITE, 0, 2 }, + { "bss", COFF_STYP_BSS, COFF_STYP_READ | COFF_STYP_WRITE, 0, 2 }, + { "info", COFF_STYP_INFO, COFF_STYP_DISCARD | COFF_STYP_READ, 0, 2 }, + { "discard", 0, COFF_STYP_DISCARD, 0, 1 }, + { "nodiscard", 0, COFF_STYP_DISCARD, 0, 0 }, + { "cache", 0, COFF_STYP_NOCACHE, 0, 0 }, + { "nocache", 0, COFF_STYP_NOCACHE, 0, 1 }, + { "page", 0, COFF_STYP_NOPAGE, 0, 0 }, + { "nopage", 0, COFF_STYP_NOPAGE, 0, 1 }, + { "share", 0, COFF_STYP_SHARED, 0, 1 }, + { "noshare", 0, COFF_STYP_SHARED, 0, 0 }, + { "execute", 0, COFF_STYP_EXECUTE, 0, 1 }, + { "noexecute", 0, COFF_STYP_EXECUTE, 0, 0 }, + { "read", 0, COFF_STYP_READ, 0, 1 }, + { "noread", 0, COFF_STYP_READ, 0, 0 }, + { "write", 0, COFF_STYP_WRITE, 0, 1 }, + { "nowrite", 0, COFF_STYP_WRITE, 0, 0 }, + { "base", 0, 0, COFF_FLAG_NOBASE, 0 }, + { "nobase", 0, 0, COFF_FLAG_NOBASE, 1 }, }; if (!vp || vp->param || !vp->val) - return NULL; + return NULL; sectname = vp->val; if (strlen(sectname) > 8 && !objfmt_coff->win32) { - /* win32 format supports >8 character section names in object - * files via "/nnnn" (where nnnn is decimal offset into string table), - * so only warn for regular COFF. - */ - yasm_warn_set(YASM_WARN_GENERAL, - N_("COFF section names limited to 8 characters: truncating")); - sectname[8] = '\0'; + /* win32 format supports >8 character section names in object + * files via "/nnnn" (where nnnn is decimal offset into string table), + * so only warn for regular COFF. + */ + yasm_warn_set(YASM_WARN_GENERAL, + N_("COFF section names limited to 8 characters: truncating")); + sectname[8] = '\0'; } if (strcmp(sectname, ".data") == 0) { - flags = COFF_STYP_DATA; - if (objfmt_coff->win32) { - flags |= COFF_STYP_READ | COFF_STYP_WRITE; - if (objfmt_coff->machine == COFF_MACHINE_AMD64) - align = 16; - else - align = 4; - } + flags = COFF_STYP_DATA; + if (objfmt_coff->win32) { + flags |= COFF_STYP_READ | COFF_STYP_WRITE; + if (objfmt_coff->machine == COFF_MACHINE_AMD64) + align = 16; + else + align = 4; + } } else if (strcmp(sectname, ".bss") == 0) { - flags = COFF_STYP_BSS; - if (objfmt_coff->win32) { - flags |= COFF_STYP_READ | COFF_STYP_WRITE; - if (objfmt_coff->machine == COFF_MACHINE_AMD64) - align = 16; - else - align = 4; - } - resonly = 1; + flags = COFF_STYP_BSS; + if (objfmt_coff->win32) { + flags |= COFF_STYP_READ | COFF_STYP_WRITE; + if (objfmt_coff->machine == COFF_MACHINE_AMD64) + align = 16; + else + align = 4; + } + resonly = 1; } else if (strcmp(sectname, ".text") == 0) { - flags = COFF_STYP_TEXT; - if (objfmt_coff->win32) { - flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; - align = 16; - } - iscode = 1; + flags = COFF_STYP_TEXT; + if (objfmt_coff->win32) { + flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; + align = 16; + } + iscode = 1; } else if (strcmp(sectname, ".rdata") == 0 - || strncmp(sectname, ".rodata", 7) == 0 - || strncmp(sectname, ".rdata$", 7) == 0) { - flags = COFF_STYP_DATA; - if (objfmt_coff->win32) { - flags |= COFF_STYP_READ; - align = 8; - } else - yasm_warn_set(YASM_WARN_GENERAL, - N_("Standard COFF does not support read-only data sections")); + || strncmp(sectname, ".rodata", 7) == 0 + || strncmp(sectname, ".rdata$", 7) == 0) { + flags = COFF_STYP_DATA; + if (objfmt_coff->win32) { + flags |= COFF_STYP_READ; + align = 8; + } else + yasm_warn_set(YASM_WARN_GENERAL, + N_("Standard COFF does not support read-only data sections")); } else if (strcmp(sectname, ".drectve") == 0) { - flags = COFF_STYP_INFO; - if (objfmt_coff->win32) - flags |= COFF_STYP_DISCARD | COFF_STYP_READ; + flags = COFF_STYP_INFO; + if (objfmt_coff->win32) + flags |= COFF_STYP_DISCARD | COFF_STYP_READ; } else if (objfmt_coff->win64 && strcmp(sectname, ".pdata") == 0) { - flags = COFF_STYP_DATA | COFF_STYP_READ; - align = 4; - flags2 = COFF_FLAG_NOBASE; + flags = COFF_STYP_DATA | COFF_STYP_READ; + align = 4; + flags2 = COFF_FLAG_NOBASE; } else if (objfmt_coff->win64 && strcmp(sectname, ".xdata") == 0) { - flags = COFF_STYP_DATA | COFF_STYP_READ; - align = 8; + flags = COFF_STYP_DATA | COFF_STYP_READ; + align = 8; } else if (strcmp(sectname, ".comment") == 0) { - flags = COFF_STYP_INFO; - if (objfmt_coff->win32) - flags |= COFF_STYP_DISCARD | COFF_STYP_READ; + flags = COFF_STYP_INFO; + if (objfmt_coff->win32) + flags |= COFF_STYP_DISCARD | COFF_STYP_READ; } else if (yasm__strncasecmp(sectname, ".debug", 6)==0) { - flags = COFF_STYP_DATA; - if (objfmt_coff->win32) - flags |= COFF_STYP_DISCARD|COFF_STYP_READ; - align = 1; + flags = COFF_STYP_DATA; + if (objfmt_coff->win32) + flags |= COFF_STYP_DISCARD|COFF_STYP_READ; + align = 1; } else { - /* Default to code */ - flags = COFF_STYP_TEXT; - if (objfmt_coff->win32) - flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; - iscode = 1; + /* Default to code */ + flags = COFF_STYP_TEXT; + if (objfmt_coff->win32) + flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; + iscode = 1; } while ((vp = yasm_vps_next(vp))) { - size_t i; - int match, win32warn; - - win32warn = 0; - - if (!vp->val) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized numeric qualifier")); - continue; - } - - match = 0; - for (i=0; ival, flagquals[i].name) == 0) { - if (!objfmt_coff->win32 && flagquals[i].stdflags == 0) - win32warn = 1; - else switch (flagquals[i].mode) { - case 0: - flags &= ~flagquals[i].stdflags; - flags2 &= ~flagquals[i].flags2; - if (objfmt_coff->win32) - flags &= ~flagquals[i].win32flags; - if (flagquals[i].win32flags & COFF_STYP_EXECUTE) - iscode = 0; - break; - case 1: - flags |= flagquals[i].stdflags; - flags2 |= flagquals[i].flags2; - if (objfmt_coff->win32) - flags |= flagquals[i].win32flags; - if (flagquals[i].win32flags & COFF_STYP_EXECUTE) - iscode = 1; - break; - case 2: - flags &= ~COFF_STYP_STD_MASK; - flags |= flagquals[i].stdflags; - flags2 = flagquals[i].flags2; - if (objfmt_coff->win32) { - flags &= ~COFF_STYP_WIN32_MASK; - flags |= flagquals[i].win32flags; - } - if (flagquals[i].win32flags & COFF_STYP_EXECUTE) - iscode = 1; - break; - } - flags_override = 1; - match = 1; - } - } - - if (match) - ; - else if (yasm__strncasecmp(vp->val, "gas_", 4) == 0) { - /* GAS-style flags */ - int alloc = 0, load = 0, readonly = 0, code = 0, data = 0; - int shared = 0; - iscode = 0; - for (i=4; ival); i++) { - switch (vp->val[i]) { - case 'a': - break; - case 'b': - alloc = 1; - load = 0; - break; - case 'n': - load = 0; - break; - case 's': - shared = 1; - /*@fallthrough@*/ - case 'd': - data = 1; - load = 1; - readonly = 0; - case 'x': - code = 1; - load = 1; - break; - case 'r': - data = 1; - load = 1; - readonly = 1; - break; - case 'w': - readonly = 0; - break; - default: - yasm_warn_set(YASM_WARN_GENERAL, - N_("unrecognized section attribute: `%c'"), - vp->val[i]); - } - } - if (code) { - flags = COFF_STYP_TEXT; - if (objfmt_coff->win32) - flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; - iscode = 1; - } else if (data) { - flags = COFF_STYP_DATA; - if (objfmt_coff->win32) - flags |= COFF_STYP_READ | COFF_STYP_WRITE; - } else if (readonly) { - flags = COFF_STYP_DATA; - if (objfmt_coff->win32) - flags |= COFF_STYP_READ; - } else if (load) - flags = COFF_STYP_TEXT; - else if (alloc) - flags = COFF_STYP_BSS; - if (shared && objfmt_coff->win32) - flags |= COFF_STYP_SHARED; - } else if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { - if (objfmt_coff->win32) { - /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; - align_expr = yasm_expr_get_intnum(&vp->param, 0); - if (!align_expr) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not an integer"), - vp->val); - return NULL; - } - align = yasm_intnum_get_uint(align_expr); - - /* Alignments must be a power of two. */ - if (!is_exp2(align)) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), - vp->val); - return NULL; - } - - /* Check to see if alignment is supported size */ - if (align > 8192) { - yasm_error_set(YASM_ERROR_VALUE, - N_("Win32 does not support alignments > 8192")); - return NULL; - } - - } else - win32warn = 1; - } else - yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), - vp->val); - - if (win32warn) - yasm_warn_set(YASM_WARN_GENERAL, - N_("Standard COFF does not support qualifier `%s'"), vp->val); + size_t i; + int match, win32warn; + + win32warn = 0; + + if (!vp->val) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized numeric qualifier")); + continue; + } + + match = 0; + for (i=0; ival, flagquals[i].name) == 0) { + if (!objfmt_coff->win32 && flagquals[i].stdflags == 0) + win32warn = 1; + else switch (flagquals[i].mode) { + case 0: + flags &= ~flagquals[i].stdflags; + flags2 &= ~flagquals[i].flags2; + if (objfmt_coff->win32) + flags &= ~flagquals[i].win32flags; + if (flagquals[i].win32flags & COFF_STYP_EXECUTE) + iscode = 0; + break; + case 1: + flags |= flagquals[i].stdflags; + flags2 |= flagquals[i].flags2; + if (objfmt_coff->win32) + flags |= flagquals[i].win32flags; + if (flagquals[i].win32flags & COFF_STYP_EXECUTE) + iscode = 1; + break; + case 2: + flags &= ~COFF_STYP_STD_MASK; + flags |= flagquals[i].stdflags; + flags2 = flagquals[i].flags2; + if (objfmt_coff->win32) { + flags &= ~COFF_STYP_WIN32_MASK; + flags |= flagquals[i].win32flags; + } + if (flagquals[i].win32flags & COFF_STYP_EXECUTE) + iscode = 1; + break; + } + flags_override = 1; + match = 1; + } + } + + if (match) + ; + else if (yasm__strncasecmp(vp->val, "gas_", 4) == 0) { + /* GAS-style flags */ + int alloc = 0, load = 0, readonly = 0, code = 0, data = 0; + int shared = 0; + iscode = 0; + for (i=4; ival); i++) { + switch (vp->val[i]) { + case 'a': + break; + case 'b': + alloc = 1; + load = 0; + break; + case 'n': + load = 0; + break; + case 's': + shared = 1; + /*@fallthrough@*/ + case 'd': + data = 1; + load = 1; + readonly = 0; + case 'x': + code = 1; + load = 1; + break; + case 'r': + data = 1; + load = 1; + readonly = 1; + break; + case 'w': + readonly = 0; + break; + default: + yasm_warn_set(YASM_WARN_GENERAL, + N_("unrecognized section attribute: `%c'"), + vp->val[i]); + } + } + if (code) { + flags = COFF_STYP_TEXT; + if (objfmt_coff->win32) + flags |= COFF_STYP_EXECUTE | COFF_STYP_READ; + iscode = 1; + } else if (data) { + flags = COFF_STYP_DATA; + if (objfmt_coff->win32) + flags |= COFF_STYP_READ | COFF_STYP_WRITE; + } else if (readonly) { + flags = COFF_STYP_DATA; + if (objfmt_coff->win32) + flags |= COFF_STYP_READ; + } else if (load) + flags = COFF_STYP_TEXT; + else if (alloc) + flags = COFF_STYP_BSS; + if (shared && objfmt_coff->win32) + flags |= COFF_STYP_SHARED; + } else if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { + if (objfmt_coff->win32) { + /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; + align_expr = yasm_expr_get_intnum(&vp->param, 0); + if (!align_expr) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not an integer"), + vp->val); + return NULL; + } + align = yasm_intnum_get_uint(align_expr); + + /* Alignments must be a power of two. */ + if (!is_exp2(align)) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not a power of two"), + vp->val); + return NULL; + } + + /* Check to see if alignment is supported size */ + if (align > 8192) { + yasm_error_set(YASM_ERROR_VALUE, + N_("Win32 does not support alignments > 8192")); + return NULL; + } + + } else + win32warn = 1; + } else + yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), + vp->val); + + if (win32warn) + yasm_warn_set(YASM_WARN_GENERAL, + N_("Standard COFF does not support qualifier `%s'"), vp->val); } retval = yasm_object_get_general(object, sectname, 0, align, iscode, - resonly, &isnew, line); + resonly, &isnew, line); if (isnew) - csd = coff_objfmt_init_new_section(object, retval, sectname, line); + csd = coff_objfmt_init_new_section(object, retval, sectname, line); else - csd = yasm_section_get_data(retval, &coff_section_data_cb); + csd = yasm_section_get_data(retval, &coff_section_data_cb); if (isnew || yasm_section_is_default(retval)) { - yasm_section_set_default(retval, 0); - csd->flags = flags; - csd->flags2 = flags2; - yasm_section_set_align(retval, align, line); + yasm_section_set_default(retval, 0); + csd->flags = flags; + csd->flags2 = flags2; + yasm_section_set_align(retval, align, line); } else if (flags_override) - yasm_warn_set(YASM_WARN_GENERAL, - N_("section flags ignored on section redeclaration")); + yasm_warn_set(YASM_WARN_GENERAL, + N_("section flags ignored on section redeclaration")); return retval; } @@ -1618,18 +1618,18 @@ coff_section_data_print(void *data, FILE *f, int indent_level) fprintf(f, "%*sscnum=%d\n", indent_level, "", csd->scnum); fprintf(f, "%*sflags=", indent_level, ""); switch (csd->flags & COFF_STYP_STD_MASK) { - case COFF_STYP_TEXT: - fprintf(f, "TEXT"); - break; - case COFF_STYP_DATA: - fprintf(f, "DATA"); - break; - case COFF_STYP_BSS: - fprintf(f, "BSS"); - break; - default: - fprintf(f, "UNKNOWN"); - break; + case COFF_STYP_TEXT: + fprintf(f, "TEXT"); + break; + case COFF_STYP_DATA: + fprintf(f, "DATA"); + break; + case COFF_STYP_BSS: + fprintf(f, "BSS"); + break; + default: + fprintf(f, "UNKNOWN"); + break; } fprintf(f, "\n%*saddr=0x%lx\n", indent_level, "", csd->addr); fprintf(f, "%*sscnptr=0x%lx\n", indent_level, "", csd->scnptr); @@ -1656,7 +1656,7 @@ coff_symrec_data_print(void *data, FILE *f, int indent_level) static void dir_export(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp; int isnew; @@ -1666,38 +1666,38 @@ dir_export(yasm_object *object, yasm_valparamhead *valparams, /* Reference exported symbol (to generate error if not declared) */ vp = yasm_vps_first(valparams); if (vp->val) - yasm_symtab_use(object->symtab, vp->val, line); + yasm_symtab_use(object->symtab, vp->val, line); else { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("argument to EXPORT must be symbol name")); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("argument to EXPORT must be symbol name")); + return; } /* Add to end of linker directives */ sect = yasm_object_get_general(object, ".drectve", 0, 0, 0, 0, &isnew, - line); + line); /* Initialize directive section if needed */ if (isnew) { - coff_section_data *csd; - csd = coff_objfmt_init_new_section(object, sect, - yasm_section_get_name(sect), line); - csd->flags = COFF_STYP_INFO | COFF_STYP_DISCARD | COFF_STYP_READ; + coff_section_data *csd; + csd = coff_objfmt_init_new_section(object, sect, + yasm_section_get_name(sect), line); + csd->flags = COFF_STYP_INFO | COFF_STYP_DISCARD | COFF_STYP_READ; } /* Add text as data bytecode */ yasm_dvs_initialize(&dvs); yasm_dvs_append(&dvs, yasm_dv_create_string(yasm__xstrdup("-export:"), - strlen("-export:"))); + strlen("-export:"))); yasm_dvs_append(&dvs, yasm_dv_create_string(yasm__xstrdup(vp->val), - strlen(vp->val))); + strlen(vp->val))); yasm_dvs_append(&dvs, yasm_dv_create_string(yasm__xstrdup(" "), 1)); yasm_section_bcs_append(sect, yasm_bc_create_data(&dvs, 1, 0, NULL, line)); } static void dir_ident(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparamhead sect_vps; @@ -1708,20 +1708,20 @@ dir_ident(yasm_object *object, yasm_valparamhead *valparams, /* Accept, but do nothing with empty ident */ if (!valparams) - return; + return; vp = yasm_vps_first(valparams); if (!vp) - return; + return; if (objfmt_coff->win32) { - /* Put ident data into .comment section for COFF, or .rdata$zzz - * to be compatible with the GNU linker, which doesn't ignore - * .comment (see binutils/gas/config/obj-coff.c:476-502). - */ - sectname = ".rdata$zzz"; + /* Put ident data into .comment section for COFF, or .rdata$zzz + * to be compatible with the GNU linker, which doesn't ignore + * .comment (see binutils/gas/config/obj-coff.c:476-502). + */ + sectname = ".rdata$zzz"; } else { - sectname = ".comment"; + sectname = ".comment"; } yasm_vps_initialize(§_vps); vp2 = yasm_vp_create(yasm__xstrdup(sectname), NULL); @@ -1733,42 +1733,42 @@ dir_ident(yasm_object *object, yasm_valparamhead *valparams, * initial 0 byte in the section. */ if (yasm_section_bcs_first(comment) == yasm_section_bcs_last(comment)) { - yasm_dvs_initialize(&dvs); - yasm_dvs_append(&dvs, yasm_dv_create_expr( - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), - line))); - yasm_section_bcs_append(comment, - yasm_bc_create_data(&dvs, 1, 0, object->arch, line)); + yasm_dvs_initialize(&dvs); + yasm_dvs_append(&dvs, yasm_dv_create_expr( + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), + line))); + yasm_section_bcs_append(comment, + yasm_bc_create_data(&dvs, 1, 0, object->arch, line)); } yasm_dvs_initialize(&dvs); do { - yasm_dvs_append(&dvs, yasm_dv_create_string(vp->val, strlen(vp->val))); - vp->val = NULL; + yasm_dvs_append(&dvs, yasm_dv_create_string(vp->val, strlen(vp->val))); + vp->val = NULL; } while ((vp = yasm_vps_next(vp))); yasm_section_bcs_append(comment, - yasm_bc_create_data(&dvs, 1, 1, object->arch, line)); + yasm_bc_create_data(&dvs, 1, 1, object->arch, line)); } static void dir_proc_frame(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparam *vp = yasm_vps_first(valparams); if (objfmt_coff->proc_frame) { - yasm_error_set_xref(objfmt_coff->proc_frame, - N_("previous procedure started here")); - yasm_error_set(YASM_ERROR_SYNTAX, - N_("nested procedures not supported (didn't use [ENDPROC_FRAME]?)")); - return; + yasm_error_set_xref(objfmt_coff->proc_frame, + N_("previous procedure started here")); + yasm_error_set(YASM_ERROR_SYNTAX, + N_("nested procedures not supported (didn't use [ENDPROC_FRAME]?)")); + return; } if (!vp || !vp->val) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("[%s] requires a procedure symbol name"), - "PROC_FRAME"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("[%s] requires a procedure symbol name"), + "PROC_FRAME"); + return; } objfmt_coff->proc_frame = line; objfmt_coff->done_prolog = 0; @@ -1778,28 +1778,28 @@ dir_proc_frame(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, /* Optional error handler */ vp = yasm_vps_next(vp); if (!vp || !vp->val) - return; + return; objfmt_coff->unwind->ehandler = - yasm_symtab_use(object->symtab, vp->val, line); + yasm_symtab_use(object->symtab, vp->val, line); } static int procframe_checkstate(yasm_objfmt_coff *objfmt_coff, const char *dirname) { if (!objfmt_coff->proc_frame) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("[%s] without preceding [PROC_FRAME]"), dirname); - return 0; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("[%s] without preceding [PROC_FRAME]"), dirname); + return 0; } if (objfmt_coff->done_prolog) { - yasm_error_set_xref(objfmt_coff->done_prolog, - N_("prologue ended here")); - yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] after end of prologue"), - dirname); - return 0; + yasm_error_set_xref(objfmt_coff->done_prolog, + N_("prologue ended here")); + yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] after end of prologue"), + dirname); + return 0; } if (!objfmt_coff->unwind) - yasm_internal_error(N_("unwind info not present")); + yasm_internal_error(N_("unwind info not present")); return 1; } @@ -1810,12 +1810,12 @@ static yasm_symrec * get_curpos(yasm_object *object, unsigned long line) { return yasm_symtab_define_curpos(object->symtab, "$", - yasm_section_bcs_last(object->cur_section), line); + yasm_section_bcs_last(object->cur_section), line); } static void dir_pushreg(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparam *vp = yasm_vps_first(valparams); @@ -1823,13 +1823,13 @@ dir_pushreg(yasm_object *object, yasm_valparamhead *valparams, const uintptr_t *reg; if (!procframe_checkstate(objfmt_coff, "PUSHREG")) - return; + return; if (!vp || !vp->param || !(reg = yasm_expr_get_reg(&vp->param, 0))) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("[%s] requires a register as the first parameter"), - "PUSHREG"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("[%s] requires a register as the first parameter"), + "PUSHREG"); + return; } /* Generate a PUSH_NONVOL unwind code. */ @@ -1844,7 +1844,7 @@ dir_pushreg(yasm_object *object, yasm_valparamhead *valparams, static void dir_setframe(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparam *vp = yasm_vps_first(valparams); @@ -1853,19 +1853,19 @@ dir_setframe(yasm_object *object, yasm_valparamhead *valparams, yasm_expr *off = NULL; if (!procframe_checkstate(objfmt_coff, "SETFRAME")) - return; + return; if (!vp || !vp->param || !(reg = yasm_expr_get_reg(&vp->param, 0))) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("[%s] requires a register as the first parameter"), - "SETFRAME"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("[%s] requires a register as the first parameter"), + "SETFRAME"); + return; } vp = yasm_vps_next(vp); if (vp && vp->param) { - off = vp->param; - vp->param = NULL; + off = vp->param; + vp->param = NULL; } /* Set the frame fields in the unwind info */ @@ -1884,25 +1884,25 @@ dir_setframe(yasm_object *object, yasm_valparamhead *valparams, static void dir_allocstack(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparam *vp = yasm_vps_first(valparams); coff_unwind_code *code; if (!procframe_checkstate(objfmt_coff, "ALLOCSTACK")) - return; + return; /* Transform ID to expression if needed */ if (vp && vp->val && !vp->param) { - vp->param = yasm_expr_create_ident(yasm_expr_sym( - yasm_symtab_use(object->symtab, vp->val, line)), line); + vp->param = yasm_expr_create_ident(yasm_expr_sym( + yasm_symtab_use(object->symtab, vp->val, line)), line); } if (!vp || !vp->param) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] requires a size"), - "ALLOCSTACK"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, N_("[%s] requires a size"), + "ALLOCSTACK"); + return; } /* Generate an ALLOC_SMALL unwind code; this will get enlarged to an @@ -1920,7 +1920,7 @@ dir_allocstack(yasm_object *object, yasm_valparamhead *valparams, static void dir_save_common(yasm_object *object, yasm_valparamhead *valparams, - unsigned long line, const char *name, int op) + unsigned long line, const char *name, int op) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparam *vp = yasm_vps_first(valparams); @@ -1928,28 +1928,28 @@ dir_save_common(yasm_object *object, yasm_valparamhead *valparams, const uintptr_t *reg; if (!procframe_checkstate(objfmt_coff, name)) - return; + return; if (!vp || !vp->param || !(reg = yasm_expr_get_reg(&vp->param, 0))) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("[%s] requires a register as the first parameter"), - name); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("[%s] requires a register as the first parameter"), + name); + return; } vp = yasm_vps_next(vp); /* Transform ID to expression if needed */ if (vp && vp->val && !vp->param) { - vp->param = yasm_expr_create_ident(yasm_expr_sym( - yasm_symtab_use(object->symtab, vp->val, line)), line); + vp->param = yasm_expr_create_ident(yasm_expr_sym( + yasm_symtab_use(object->symtab, vp->val, line)), line); } if (!vp || !vp->param) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("[%s] requires an offset as the second parameter"), - name); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("[%s] requires an offset as the second parameter"), + name); + return; } /* Generate a SAVE_XXX unwind code; this will get enlarged to a @@ -1967,28 +1967,28 @@ dir_save_common(yasm_object *object, yasm_valparamhead *valparams, static void dir_savereg(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { dir_save_common(object, valparams, line, "SAVEREG", UWOP_SAVE_NONVOL); } static void dir_savexmm128(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { dir_save_common(object, valparams, line, "SAVEXMM128", UWOP_SAVE_XMM128); } static void dir_pushframe(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_valparam *vp = yasm_vps_first(valparams); coff_unwind_code *code; if (!procframe_checkstate(objfmt_coff, "PUSHFRAME")) - return; + return; /* Generate a PUSH_MACHFRAME unwind code. If there's any parameter, * we set info to 1. Otherwise we set info to 0. @@ -2004,11 +2004,11 @@ dir_pushframe(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, static void dir_endprolog(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; if (!procframe_checkstate(objfmt_coff, "ENDPROLOG")) - return; + return; objfmt_coff->done_prolog = line; objfmt_coff->unwind->prolog = get_curpos(object, line); @@ -2016,7 +2016,7 @@ dir_endprolog(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, static void dir_endproc_frame(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_objfmt_coff *objfmt_coff = (yasm_objfmt_coff *)object->objfmt; yasm_section *sect; @@ -2026,24 +2026,24 @@ dir_endproc_frame(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, /*@dependent@*/ yasm_symrec *curpos, *unwindpos, *proc_sym, *xdata_sym; if (!objfmt_coff->proc_frame) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("[%s] without preceding [PROC_FRAME]"), - "ENDPROC_FRAME"); - return; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("[%s] without preceding [PROC_FRAME]"), + "ENDPROC_FRAME"); + return; } if (!objfmt_coff->done_prolog) { - yasm_error_set_xref(objfmt_coff->proc_frame, - N_("procedure started here")); - yasm_error_set(YASM_ERROR_SYNTAX, - N_("ended procedure without ending prologue"), - "ENDPROC_FRAME"); - objfmt_coff->proc_frame = 0; - yasm_win64__uwinfo_destroy(objfmt_coff->unwind); - objfmt_coff->unwind = NULL; - return; + yasm_error_set_xref(objfmt_coff->proc_frame, + N_("procedure started here")); + yasm_error_set(YASM_ERROR_SYNTAX, + N_("ended procedure without ending prologue"), + "ENDPROC_FRAME"); + objfmt_coff->proc_frame = 0; + yasm_win64__uwinfo_destroy(objfmt_coff->unwind); + objfmt_coff->unwind = NULL; + return; } if (!objfmt_coff->unwind) - yasm_internal_error(N_("unwind info not present")); + yasm_internal_error(N_("unwind info not present")); proc_sym = objfmt_coff->unwind->proc; @@ -2057,22 +2057,22 @@ dir_endproc_frame(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, /* Initialize xdata section if needed */ if (isnew) { - csd = coff_objfmt_init_new_section(object, sect, ".xdata", line); - csd->flags = COFF_STYP_DATA | COFF_STYP_READ; - yasm_section_set_align(sect, 8, line); + csd = coff_objfmt_init_new_section(object, sect, ".xdata", line); + csd->flags = COFF_STYP_DATA | COFF_STYP_READ; + yasm_section_set_align(sect, 8, line); } /* Get current position in .xdata section */ unwindpos = yasm_symtab_define_curpos(object->symtab, "$", - yasm_section_bcs_last(sect), line); + yasm_section_bcs_last(sect), line); /* Get symbol for .xdata as we'll want to reference it with WRT */ csd = yasm_section_get_data(sect, &coff_section_data_cb); xdata_sym = csd->sym; /* Add unwind info. Use line number of start of procedure. */ yasm_win64__unwind_generate(sect, objfmt_coff->unwind, - objfmt_coff->proc_frame); - objfmt_coff->unwind = NULL; /* generate keeps the unwind pointer */ + objfmt_coff->proc_frame); + objfmt_coff->unwind = NULL; /* generate keeps the unwind pointer */ /* * Add function lookup to end of .pdata section. @@ -2082,22 +2082,22 @@ dir_endproc_frame(yasm_object *object, /*@null@*/ yasm_valparamhead *valparams, /* Initialize pdata section if needed */ if (isnew) { - csd = coff_objfmt_init_new_section(object, sect, ".pdata", line); - csd->flags = COFF_STYP_DATA | COFF_STYP_READ; - csd->flags2 = COFF_FLAG_NOBASE; - yasm_section_set_align(sect, 4, line); + csd = coff_objfmt_init_new_section(object, sect, ".pdata", line); + csd->flags = COFF_STYP_DATA | COFF_STYP_READ; + csd->flags2 = COFF_FLAG_NOBASE; + yasm_section_set_align(sect, 4, line); } /* Add function structure as data bytecode */ yasm_dvs_initialize(&dvs); yasm_dvs_append(&dvs, yasm_dv_create_expr( - yasm_expr_create_ident(yasm_expr_sym(proc_sym), line))); + yasm_expr_create_ident(yasm_expr_sym(proc_sym), line))); yasm_dvs_append(&dvs, yasm_dv_create_expr( - yasm_expr_create(YASM_EXPR_WRT, yasm_expr_sym(curpos), - yasm_expr_sym(proc_sym), line))); + yasm_expr_create(YASM_EXPR_WRT, yasm_expr_sym(curpos), + yasm_expr_sym(proc_sym), line))); yasm_dvs_append(&dvs, yasm_dv_create_expr( - yasm_expr_create(YASM_EXPR_WRT, yasm_expr_sym(unwindpos), - yasm_expr_sym(xdata_sym), line))); + yasm_expr_create(YASM_EXPR_WRT, yasm_expr_sym(unwindpos), + yasm_expr_sym(xdata_sym), line))); yasm_section_bcs_append(sect, yasm_bc_create_data(&dvs, 4, 0, NULL, line)); objfmt_coff->proc_frame = 0; @@ -2112,8 +2112,8 @@ static const char *coff_objfmt_dbgfmt_keywords[] = { }; static const yasm_directive coff_objfmt_directives[] = { - { ".ident", "gas", dir_ident, YASM_DIR_ANY }, - { "ident", "nasm", dir_ident, YASM_DIR_ANY }, + { ".ident", "gas", dir_ident, YASM_DIR_ANY }, + { "ident", "nasm", dir_ident, YASM_DIR_ANY }, { NULL, NULL, NULL, 0 } }; @@ -2142,10 +2142,10 @@ static const char *winXX_objfmt_dbgfmt_keywords[] = { }; static const yasm_directive win32_objfmt_directives[] = { - { ".ident", "gas", dir_ident, YASM_DIR_ANY }, - { ".export", "gas", dir_export, YASM_DIR_ID_REQUIRED }, - { "ident", "nasm", dir_ident, YASM_DIR_ANY }, - { "export", "nasm", dir_export, YASM_DIR_ID_REQUIRED }, + { ".ident", "gas", dir_ident, YASM_DIR_ANY }, + { ".export", "gas", dir_export, YASM_DIR_ID_REQUIRED }, + { "ident", "nasm", dir_ident, YASM_DIR_ANY }, + { "export", "nasm", dir_export, YASM_DIR_ID_REQUIRED }, { NULL, NULL, NULL, 0 } }; @@ -2166,19 +2166,19 @@ yasm_objfmt_module yasm_win32_LTX_objfmt = { }; static const yasm_directive win64_objfmt_directives[] = { - { ".ident", "gas", dir_ident, YASM_DIR_ANY }, - { "ident", "nasm", dir_ident, YASM_DIR_ANY }, - { ".export", "gas", dir_export, YASM_DIR_ID_REQUIRED }, - { "export", "nasm", dir_export, YASM_DIR_ID_REQUIRED }, - { "proc_frame", "nasm", dir_proc_frame, YASM_DIR_ANY }, - { "pushreg", "nasm", dir_pushreg, YASM_DIR_ARG_REQUIRED }, - { "setframe", "nasm", dir_setframe, YASM_DIR_ARG_REQUIRED }, - { "allocstack", "nasm", dir_allocstack, YASM_DIR_ARG_REQUIRED }, - { "savereg", "nasm", dir_savereg, YASM_DIR_ARG_REQUIRED }, - { "savexmm128", "nasm", dir_savexmm128, YASM_DIR_ARG_REQUIRED }, - { "pushframe", "nasm", dir_pushframe, YASM_DIR_ANY }, - { "endprolog", "nasm", dir_endprolog, YASM_DIR_ANY }, - { "endproc_frame", "nasm", dir_endproc_frame, YASM_DIR_ANY }, + { ".ident", "gas", dir_ident, YASM_DIR_ANY }, + { "ident", "nasm", dir_ident, YASM_DIR_ANY }, + { ".export", "gas", dir_export, YASM_DIR_ID_REQUIRED }, + { "export", "nasm", dir_export, YASM_DIR_ID_REQUIRED }, + { "proc_frame", "nasm", dir_proc_frame, YASM_DIR_ANY }, + { "pushreg", "nasm", dir_pushreg, YASM_DIR_ARG_REQUIRED }, + { "setframe", "nasm", dir_setframe, YASM_DIR_ARG_REQUIRED }, + { "allocstack", "nasm", dir_allocstack, YASM_DIR_ARG_REQUIRED }, + { "savereg", "nasm", dir_savereg, YASM_DIR_ARG_REQUIRED }, + { "savexmm128", "nasm", dir_savexmm128, YASM_DIR_ARG_REQUIRED }, + { "pushframe", "nasm", dir_pushframe, YASM_DIR_ANY }, + { "endprolog", "nasm", dir_endprolog, YASM_DIR_ANY }, + { "endproc_frame", "nasm", dir_endproc_frame, YASM_DIR_ANY }, { NULL, NULL, NULL, 0 } }; diff --git a/modules/objfmts/coff/coff-objfmt.h b/modules/objfmts/coff/coff-objfmt.h index f30c1192..1360eade 100644 --- a/modules/objfmts/coff/coff-objfmt.h +++ b/modules/objfmts/coff/coff-objfmt.h @@ -30,32 +30,32 @@ typedef struct coff_unwind_code { SLIST_ENTRY(coff_unwind_code) link; - /*@dependent@*/ yasm_symrec *proc; /* Start of procedure */ - /*@dependent@*/ yasm_symrec *loc; /* Location of operation */ + /*@dependent@*/ yasm_symrec *proc; /* Start of procedure */ + /*@dependent@*/ yasm_symrec *loc; /* Location of operation */ /* Unwind operation code */ enum { - UWOP_PUSH_NONVOL = 0, - UWOP_ALLOC_LARGE = 1, - UWOP_ALLOC_SMALL = 2, - UWOP_SET_FPREG = 3, - UWOP_SAVE_NONVOL = 4, - UWOP_SAVE_NONVOL_FAR = 5, - UWOP_SAVE_XMM128 = 8, - UWOP_SAVE_XMM128_FAR = 9, - UWOP_PUSH_MACHFRAME = 10 + UWOP_PUSH_NONVOL = 0, + UWOP_ALLOC_LARGE = 1, + UWOP_ALLOC_SMALL = 2, + UWOP_SET_FPREG = 3, + UWOP_SAVE_NONVOL = 4, + UWOP_SAVE_NONVOL_FAR = 5, + UWOP_SAVE_XMM128 = 8, + UWOP_SAVE_XMM128_FAR = 9, + UWOP_PUSH_MACHFRAME = 10 } opcode; - unsigned int info; /* Operation info */ - yasm_value off; /* Offset expression (used for some codes) */ + unsigned int info; /* Operation info */ + yasm_value off; /* Offset expression (used for some codes) */ } coff_unwind_code; typedef struct coff_unwind_info { - /*@dependent@*/ yasm_symrec *proc; /* Start of procedure */ + /*@dependent@*/ yasm_symrec *proc; /* Start of procedure */ /*@dependent@*/ yasm_symrec *prolog; /* End of prologue */ - /*@null@*/ /*@dependent@*/ yasm_symrec *ehandler; /* Error handler */ + /*@null@*/ /*@dependent@*/ yasm_symrec *ehandler; /* Error handler */ - unsigned long framereg; /* Frame register */ - yasm_value frameoff; /* Frame offset */ + unsigned long framereg; /* Frame register */ + yasm_value frameoff; /* Frame offset */ /* Linked list of codes, in decreasing location offset order. * Inserting at the head of this list during assembly naturally results @@ -71,7 +71,7 @@ typedef struct coff_unwind_info { coff_unwind_info *yasm_win64__uwinfo_create(void); void yasm_win64__uwinfo_destroy(coff_unwind_info *info); void yasm_win64__unwind_generate(yasm_section *xdata, - /*@only@*/ coff_unwind_info *info, - unsigned long line); + /*@only@*/ coff_unwind_info *info, + unsigned long line); #endif diff --git a/modules/objfmts/coff/tests/cofftest.c b/modules/objfmts/coff/tests/cofftest.c index f7446b8a..fa6a552d 100644 --- a/modules/objfmts/coff/tests/cofftest.c +++ b/modules/objfmts/coff/tests/cofftest.c @@ -28,7 +28,7 @@ int main(void) { greet(); printf("These pointers should be equal: %p and %p\n", - &greet, textptr); + &greet, textptr); printf("So should these: %p and %p\n", selfptr, &selfptr); } diff --git a/modules/objfmts/coff/win64-except.c b/modules/objfmts/coff/win64-except.c index f50c271d..99c14376 100644 --- a/modules/objfmts/coff/win64-except.c +++ b/modules/objfmts/coff/win64-except.c @@ -42,14 +42,14 @@ /* Bytecode callback function prototypes */ static void win64_uwinfo_bc_destroy(void *contents); static void win64_uwinfo_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static void win64_uwinfo_bc_finalize(yasm_bytecode *bc, - yasm_bytecode *prev_bc); + yasm_bytecode *prev_bc); static int win64_uwinfo_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int win64_uwinfo_bc_expand(yasm_bytecode *bc, int span, long old_val, - long new_val, /*@out@*/ long *neg_thres, - /*@out@*/ long *pos_thres); + long new_val, /*@out@*/ long *neg_thres, + /*@out@*/ long *pos_thres); static int win64_uwinfo_bc_tobytes (yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_output_value_func output_value, @@ -57,14 +57,14 @@ static int win64_uwinfo_bc_tobytes static void win64_uwcode_bc_destroy(void *contents); static void win64_uwcode_bc_print(const void *contents, FILE *f, - int indent_level); + int indent_level); static void win64_uwcode_bc_finalize(yasm_bytecode *bc, - yasm_bytecode *prev_bc); + yasm_bytecode *prev_bc); static int win64_uwcode_bc_calc_len (yasm_bytecode *bc, yasm_bc_add_span_func add_span, void *add_span_data); static int win64_uwcode_bc_expand(yasm_bytecode *bc, int span, long old_val, - long new_val, /*@out@*/ long *neg_thres, - /*@out@*/ long *pos_thres); + long new_val, /*@out@*/ long *neg_thres, + /*@out@*/ long *pos_thres); static int win64_uwcode_bc_tobytes (yasm_bytecode *bc, unsigned char **bufp, void *d, yasm_output_value_func output_value, @@ -118,32 +118,32 @@ yasm_win64__uwinfo_destroy(coff_unwind_info *info) yasm_value_delete(&info->codes_count); while (!SLIST_EMPTY(&info->codes)) { - code = SLIST_FIRST(&info->codes); - SLIST_REMOVE_HEAD(&info->codes, link); - yasm_value_delete(&code->off); - yasm_xfree(code); + code = SLIST_FIRST(&info->codes); + SLIST_REMOVE_HEAD(&info->codes, link); + yasm_value_delete(&code->off); + yasm_xfree(code); } yasm_xfree(info); } void yasm_win64__unwind_generate(yasm_section *xdata, coff_unwind_info *info, - unsigned long line) + unsigned long line) { yasm_bytecode *infobc, *codebc = NULL; coff_unwind_code *code; /* 4-byte align the start of unwind info */ yasm_section_bcs_append(xdata, yasm_bc_create_align( - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), - line), - NULL, NULL, NULL, line)); + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), + line), + NULL, NULL, NULL, line)); /* Prolog size = end of prolog - start of procedure */ yasm_value_initialize(&info->prolog_size, - yasm_expr_create(YASM_EXPR_SUB, yasm_expr_sym(info->prolog), - yasm_expr_sym(info->proc), line), - 8); + yasm_expr_create(YASM_EXPR_SUB, yasm_expr_sym(info->prolog), + yasm_expr_sym(info->proc), line), + 8); /* Unwind info */ infobc = yasm_bc_create_common(&win64_uwinfo_bc_callback, info, line); @@ -151,9 +151,9 @@ yasm_win64__unwind_generate(yasm_section *xdata, coff_unwind_info *info, /* Code array */ SLIST_FOREACH(code, &info->codes, link) { - codebc = yasm_bc_create_common(&win64_uwcode_bc_callback, code, - yasm_symrec_get_def_line(code->loc)); - yasm_section_bcs_append(xdata, codebc); + codebc = yasm_bc_create_common(&win64_uwcode_bc_callback, code, + yasm_symrec_get_def_line(code->loc)); + yasm_section_bcs_append(xdata, codebc); } /* Avoid double-free (by code destroy and uwinfo destroy). */ @@ -161,34 +161,34 @@ yasm_win64__unwind_generate(yasm_section *xdata, coff_unwind_info *info, /* Number of codes = (Last code - end of info) >> 1 */ if (!codebc) { - yasm_value_initialize(&info->codes_count, - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), - line), - 8); + yasm_value_initialize(&info->codes_count, + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), + line), + 8); } else { - yasm_value_initialize(&info->codes_count, - yasm_expr_create(YASM_EXPR_SHR, yasm_expr_expr( - yasm_expr_create(YASM_EXPR_SUB, yasm_expr_precbc(codebc), - yasm_expr_precbc(infobc), line)), - yasm_expr_int(yasm_intnum_create_uint(1)), line), - 8); + yasm_value_initialize(&info->codes_count, + yasm_expr_create(YASM_EXPR_SHR, yasm_expr_expr( + yasm_expr_create(YASM_EXPR_SUB, yasm_expr_precbc(codebc), + yasm_expr_precbc(infobc), line)), + yasm_expr_int(yasm_intnum_create_uint(1)), line), + 8); } /* 4-byte align */ yasm_section_bcs_append(xdata, yasm_bc_create_align( - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), - line), - NULL, NULL, NULL, line)); + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(4)), + line), + NULL, NULL, NULL, line)); /* Exception handler, if present. Use data bytecode. */ if (info->ehandler) { - yasm_datavalhead dvs; + yasm_datavalhead dvs; - yasm_dvs_initialize(&dvs); - yasm_dvs_append(&dvs, yasm_dv_create_expr( - yasm_expr_create_ident(yasm_expr_sym(info->ehandler), line))); - yasm_section_bcs_append(xdata, - yasm_bc_create_data(&dvs, 4, 0, NULL, line)); + yasm_dvs_initialize(&dvs); + yasm_dvs_append(&dvs, yasm_dv_create_expr( + yasm_expr_create_ident(yasm_expr_sym(info->ehandler), line))); + yasm_section_bcs_append(xdata, + yasm_bc_create_data(&dvs, 4, 0, NULL, line)); } } @@ -210,19 +210,19 @@ win64_uwinfo_bc_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) coff_unwind_info *info = (coff_unwind_info *)bc->contents; if (yasm_value_finalize(&info->prolog_size, prev_bc)) - yasm_internal_error(N_("prolog size expression too complex")); + yasm_internal_error(N_("prolog size expression too complex")); if (yasm_value_finalize(&info->codes_count, prev_bc)) - yasm_internal_error(N_("codes count expression too complex")); + yasm_internal_error(N_("codes count expression too complex")); if (yasm_value_finalize(&info->frameoff, prev_bc)) - yasm_error_set(YASM_ERROR_VALUE, - N_("frame offset expression too complex")); + yasm_error_set(YASM_ERROR_VALUE, + N_("frame offset expression too complex")); } static int win64_uwinfo_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { coff_unwind_info *info = (coff_unwind_info *)bc->contents; /*@only@*/ /*@null@*/ yasm_intnum *intn; @@ -236,17 +236,17 @@ win64_uwinfo_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, intn = yasm_value_get_intnum(&info->frameoff, bc, 0); if (intn) { - intv = yasm_intnum_get_int(intn); - if (intv < 0 || intv > 240) - yasm_error_set(YASM_ERROR_VALUE, - N_("frame offset of %ld bytes, must be between 0 and 240"), - intv); - else if ((intv & 0xF) != 0) - yasm_error_set(YASM_ERROR_VALUE, - N_("frame offset of %ld is not a multiple of 16"), intv); - yasm_intnum_destroy(intn); + intv = yasm_intnum_get_int(intn); + if (intv < 0 || intv > 240) + yasm_error_set(YASM_ERROR_VALUE, + N_("frame offset of %ld bytes, must be between 0 and 240"), + intv); + else if ((intv & 0xF) != 0) + yasm_error_set(YASM_ERROR_VALUE, + N_("frame offset of %ld is not a multiple of 16"), intv); + yasm_intnum_destroy(intn); } else - add_span(add_span_data, bc, 3, &info->frameoff, 0, 240); + add_span(add_span_data, bc, 3, &info->frameoff, 0, 240); bc->len += 4; return 0; @@ -254,35 +254,35 @@ win64_uwinfo_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, static int win64_uwinfo_bc_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { coff_unwind_info *info = (coff_unwind_info *)bc->contents; switch (span) { - case 1: - yasm_error_set_xref(yasm_symrec_get_def_line(info->prolog), - N_("prologue ended here")); - yasm_error_set(YASM_ERROR_VALUE, - N_("prologue %ld bytes, must be <256"), new_val); - return -1; - case 2: - yasm_error_set(YASM_ERROR_VALUE, - N_("%ld unwind codes, maximum of 255"), new_val); - return -1; - case 3: - yasm_error_set(YASM_ERROR_VALUE, - N_("frame offset of %ld bytes, must be between 0 and 240"), - new_val); - return -1; - default: - yasm_internal_error(N_("unrecognized span id")); + case 1: + yasm_error_set_xref(yasm_symrec_get_def_line(info->prolog), + N_("prologue ended here")); + yasm_error_set(YASM_ERROR_VALUE, + N_("prologue %ld bytes, must be <256"), new_val); + return -1; + case 2: + yasm_error_set(YASM_ERROR_VALUE, + N_("%ld unwind codes, maximum of 255"), new_val); + return -1; + case 3: + yasm_error_set(YASM_ERROR_VALUE, + N_("frame offset of %ld bytes, must be between 0 and 240"), + new_val); + return -1; + default: + yasm_internal_error(N_("unrecognized span id")); } return 0; } static int win64_uwinfo_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { coff_unwind_info *info = (coff_unwind_info *)bc->contents; unsigned char *buf = *bufp; @@ -291,34 +291,34 @@ win64_uwinfo_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Version and flags */ if (info->ehandler) - YASM_WRITE_8(buf, 1 | (UNW_FLAG_EHANDLER << 3)); + YASM_WRITE_8(buf, 1 | (UNW_FLAG_EHANDLER << 3)); else - YASM_WRITE_8(buf, 1); + YASM_WRITE_8(buf, 1); /* Size of prolog */ output_value(&info->prolog_size, buf, 1, (unsigned long)(buf-*bufp), - bc, 1, d); + bc, 1, d); buf += 1; /* Count of codes */ output_value(&info->codes_count, buf, 1, (unsigned long)(buf-*bufp), - bc, 1, d); + bc, 1, d); buf += 1; /* Frame register and offset */ frameoff = yasm_value_get_intnum(&info->frameoff, bc, 1); if (!frameoff) { - yasm_error_set(YASM_ERROR_VALUE, - N_("frame offset expression too complex")); - return 1; + yasm_error_set(YASM_ERROR_VALUE, + N_("frame offset expression too complex")); + return 1; } intv = yasm_intnum_get_int(frameoff); if (intv < 0 || intv > 240) - yasm_error_set(YASM_ERROR_VALUE, - N_("frame offset of %ld bytes, must be between 0 and 240"), intv); + yasm_error_set(YASM_ERROR_VALUE, + N_("frame offset of %ld bytes, must be between 0 and 240"), intv); else if ((intv & 0xF) != 0) - yasm_error_set(YASM_ERROR_VALUE, - N_("frame offset of %ld is not a multiple of 16"), intv); + yasm_error_set(YASM_ERROR_VALUE, + N_("frame offset of %ld is not a multiple of 16"), intv); YASM_WRITE_8(buf, ((unsigned long)intv & 0xF0) | (info->framereg & 0x0F)); yasm_intnum_destroy(frameoff); @@ -346,12 +346,12 @@ win64_uwcode_bc_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) { coff_unwind_code *code = (coff_unwind_code *)bc->contents; if (yasm_value_finalize(&code->off, prev_bc)) - yasm_error_set(YASM_ERROR_VALUE, N_("offset expression too complex")); + yasm_error_set(YASM_ERROR_VALUE, N_("offset expression too complex")); } static int win64_uwcode_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, - void *add_span_data) + void *add_span_data) { coff_unwind_code *code = (coff_unwind_code *)bc->contents; int span = 0; @@ -362,107 +362,107 @@ win64_uwcode_bc_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, bc->len += 2; /* Prolog offset, code, and info */ switch (code->opcode) { - case UWOP_PUSH_NONVOL: - case UWOP_SET_FPREG: - case UWOP_PUSH_MACHFRAME: - /* always 1 node */ - return 0; - case UWOP_ALLOC_SMALL: - case UWOP_ALLOC_LARGE: - /* Start with smallest, then work our way up as necessary */ - code->opcode = UWOP_ALLOC_SMALL; - code->info = 0; - span = 1; low = 8; high = 128; mask = 0x7; - break; - case UWOP_SAVE_NONVOL: - case UWOP_SAVE_NONVOL_FAR: - /* Start with smallest, then work our way up as necessary */ - code->opcode = UWOP_SAVE_NONVOL; - bc->len += 2; /* Scaled offset */ - span = 2; - low = 0; - high = 8*64*1024-8; /* 16-bit field, *8 scaling */ - mask = 0x7; - break; - case UWOP_SAVE_XMM128: - case UWOP_SAVE_XMM128_FAR: - /* Start with smallest, then work our way up as necessary */ - code->opcode = UWOP_SAVE_XMM128; - bc->len += 2; /* Scaled offset */ - span = 3; - low = 0; - high = 16*64*1024-16; /* 16-bit field, *16 scaling */ - mask = 0xF; - break; - default: - yasm_internal_error(N_("unrecognied unwind opcode")); - /*@unreached@*/ - return 0; + case UWOP_PUSH_NONVOL: + case UWOP_SET_FPREG: + case UWOP_PUSH_MACHFRAME: + /* always 1 node */ + return 0; + case UWOP_ALLOC_SMALL: + case UWOP_ALLOC_LARGE: + /* Start with smallest, then work our way up as necessary */ + code->opcode = UWOP_ALLOC_SMALL; + code->info = 0; + span = 1; low = 8; high = 128; mask = 0x7; + break; + case UWOP_SAVE_NONVOL: + case UWOP_SAVE_NONVOL_FAR: + /* Start with smallest, then work our way up as necessary */ + code->opcode = UWOP_SAVE_NONVOL; + bc->len += 2; /* Scaled offset */ + span = 2; + low = 0; + high = 8*64*1024-8; /* 16-bit field, *8 scaling */ + mask = 0x7; + break; + case UWOP_SAVE_XMM128: + case UWOP_SAVE_XMM128_FAR: + /* Start with smallest, then work our way up as necessary */ + code->opcode = UWOP_SAVE_XMM128; + bc->len += 2; /* Scaled offset */ + span = 3; + low = 0; + high = 16*64*1024-16; /* 16-bit field, *16 scaling */ + mask = 0xF; + break; + default: + yasm_internal_error(N_("unrecognied unwind opcode")); + /*@unreached@*/ + return 0; } intn = yasm_value_get_intnum(&code->off, bc, 0); if (intn) { - intv = yasm_intnum_get_int(intn); - if (intv > high) { - /* Expand it ourselves here if we can and we're already larger */ - if (win64_uwcode_bc_expand(bc, span, intv, intv, &low, &high) > 0) - add_span(add_span_data, bc, span, &code->off, low, high); - } - if (intv < low) - yasm_error_set(YASM_ERROR_VALUE, - N_("negative offset not allowed")); - if ((intv & mask) != 0) - yasm_error_set(YASM_ERROR_VALUE, - N_("offset of %ld is not a multiple of %ld"), intv, mask+1); - yasm_intnum_destroy(intn); + intv = yasm_intnum_get_int(intn); + if (intv > high) { + /* Expand it ourselves here if we can and we're already larger */ + if (win64_uwcode_bc_expand(bc, span, intv, intv, &low, &high) > 0) + add_span(add_span_data, bc, span, &code->off, low, high); + } + if (intv < low) + yasm_error_set(YASM_ERROR_VALUE, + N_("negative offset not allowed")); + if ((intv & mask) != 0) + yasm_error_set(YASM_ERROR_VALUE, + N_("offset of %ld is not a multiple of %ld"), intv, mask+1); + yasm_intnum_destroy(intn); } else - add_span(add_span_data, bc, span, &code->off, low, high); + add_span(add_span_data, bc, span, &code->off, low, high); return 0; } static int win64_uwcode_bc_expand(yasm_bytecode *bc, int span, long old_val, long new_val, - /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) + /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) { coff_unwind_code *code = (coff_unwind_code *)bc->contents; if (new_val < 0) { - yasm_error_set(YASM_ERROR_VALUE, N_("negative offset not allowed")); - return -1; + yasm_error_set(YASM_ERROR_VALUE, N_("negative offset not allowed")); + return -1; } if (span == 1) { - /* 3 stages: SMALL, LARGE and info=0, LARGE and info=1 */ - if (code->opcode == UWOP_ALLOC_LARGE && code->info == 1) - yasm_internal_error(N_("expansion on already largest alloc")); - - if (code->opcode == UWOP_ALLOC_SMALL && new_val > 128) { - /* Overflowed small size */ - code->opcode = UWOP_ALLOC_LARGE; - bc->len += 2; - } - if (new_val <= 8*64*1024-8) { - /* Still can grow one more size */ - *pos_thres = 8*64*1024-8; - return 1; - } - /* We're into the largest size */ - code->info = 1; - bc->len += 2; + /* 3 stages: SMALL, LARGE and info=0, LARGE and info=1 */ + if (code->opcode == UWOP_ALLOC_LARGE && code->info == 1) + yasm_internal_error(N_("expansion on already largest alloc")); + + if (code->opcode == UWOP_ALLOC_SMALL && new_val > 128) { + /* Overflowed small size */ + code->opcode = UWOP_ALLOC_LARGE; + bc->len += 2; + } + if (new_val <= 8*64*1024-8) { + /* Still can grow one more size */ + *pos_thres = 8*64*1024-8; + return 1; + } + /* We're into the largest size */ + code->info = 1; + bc->len += 2; } else if (code->opcode == UWOP_SAVE_NONVOL && span == 2) { - code->opcode = UWOP_SAVE_NONVOL_FAR; - bc->len += 2; + code->opcode = UWOP_SAVE_NONVOL_FAR; + bc->len += 2; } else if (code->opcode == UWOP_SAVE_XMM128 && span == 3) { - code->opcode = UWOP_SAVE_XMM128_FAR; - bc->len += 2; + code->opcode = UWOP_SAVE_XMM128_FAR; + bc->len += 2; } return 0; } static int win64_uwcode_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, - yasm_output_value_func output_value, - yasm_output_reloc_func output_reloc) + yasm_output_value_func output_value, + yasm_output_reloc_func output_reloc) { coff_unwind_code *code = (coff_unwind_code *)bc->contents; unsigned char *buf = *bufp; @@ -474,82 +474,82 @@ win64_uwcode_bc_tobytes(yasm_bytecode *bc, unsigned char **bufp, void *d, /* Offset in prolog */ yasm_value_initialize(&val, - yasm_expr_create(YASM_EXPR_SUB, yasm_expr_sym(code->loc), - yasm_expr_sym(code->proc), bc->line), - 8); + yasm_expr_create(YASM_EXPR_SUB, yasm_expr_sym(code->loc), + yasm_expr_sym(code->proc), bc->line), + 8); output_value(&val, buf, 1, (unsigned long)(buf-*bufp), bc, 1, d); buf += 1; yasm_value_delete(&val); /* Offset value */ switch (code->opcode) { - case UWOP_PUSH_NONVOL: - case UWOP_SET_FPREG: - case UWOP_PUSH_MACHFRAME: - /* just 1 node, no offset; write opcode and info and we're done */ - YASM_WRITE_8(buf, (code->info << 4) | (code->opcode & 0xF)); - *bufp = buf; - return 0; - case UWOP_ALLOC_SMALL: - /* 1 node, but offset stored in info */ - size = 0; low = 8; high = 128; shift = 3; mask = 0x7; - break; - case UWOP_ALLOC_LARGE: - if (code->info == 0) { - size = 2; low = 136; high = 8*64*1024-8; shift = 3; - } else { - size = 4; low = high = 0; shift = 0; - } - mask = 0x7; - break; - case UWOP_SAVE_NONVOL: - size = 2; low = 0; high = 8*64*1024-8; shift = 3; mask = 0x7; - break; - case UWOP_SAVE_XMM128: - size = 2; low = 0; high = 16*64*1024-16; shift = 4; mask = 0xF; - break; - case UWOP_SAVE_NONVOL_FAR: - size = 4; low = high = 0; shift = 0; mask = 0x7; - break; - case UWOP_SAVE_XMM128_FAR: - size = 4; low = high = 0; shift = 0; mask = 0xF; - break; - default: - yasm_internal_error(N_("unrecognied unwind opcode")); - /*@unreached@*/ - return 1; + case UWOP_PUSH_NONVOL: + case UWOP_SET_FPREG: + case UWOP_PUSH_MACHFRAME: + /* just 1 node, no offset; write opcode and info and we're done */ + YASM_WRITE_8(buf, (code->info << 4) | (code->opcode & 0xF)); + *bufp = buf; + return 0; + case UWOP_ALLOC_SMALL: + /* 1 node, but offset stored in info */ + size = 0; low = 8; high = 128; shift = 3; mask = 0x7; + break; + case UWOP_ALLOC_LARGE: + if (code->info == 0) { + size = 2; low = 136; high = 8*64*1024-8; shift = 3; + } else { + size = 4; low = high = 0; shift = 0; + } + mask = 0x7; + break; + case UWOP_SAVE_NONVOL: + size = 2; low = 0; high = 8*64*1024-8; shift = 3; mask = 0x7; + break; + case UWOP_SAVE_XMM128: + size = 2; low = 0; high = 16*64*1024-16; shift = 4; mask = 0xF; + break; + case UWOP_SAVE_NONVOL_FAR: + size = 4; low = high = 0; shift = 0; mask = 0x7; + break; + case UWOP_SAVE_XMM128_FAR: + size = 4; low = high = 0; shift = 0; mask = 0xF; + break; + default: + yasm_internal_error(N_("unrecognied unwind opcode")); + /*@unreached@*/ + return 1; } /* Check for overflow */ intn = yasm_value_get_intnum(&code->off, bc, 1); if (!intn) { - yasm_error_set(YASM_ERROR_VALUE, N_("offset expression too complex")); - return 1; + yasm_error_set(YASM_ERROR_VALUE, N_("offset expression too complex")); + return 1; } intv = yasm_intnum_get_int(intn); if (size != 4 && (intv < low || intv > high)) { - yasm_error_set(YASM_ERROR_VALUE, - N_("offset of %ld bytes, must be between %ld and %ld"), - intv, low, high); - return 1; + yasm_error_set(YASM_ERROR_VALUE, + N_("offset of %ld bytes, must be between %ld and %ld"), + intv, low, high); + return 1; } if ((intv & mask) != 0) { - yasm_error_set(YASM_ERROR_VALUE, - N_("offset of %ld is not a multiple of %ld"), - intv, mask+1); - return 1; + yasm_error_set(YASM_ERROR_VALUE, + N_("offset of %ld is not a multiple of %ld"), + intv, mask+1); + return 1; } /* Stored value in info instead of extra code space */ if (size == 0) - code->info = (yasm_intnum_get_uint(intn) >> shift)-1; + code->info = (yasm_intnum_get_uint(intn) >> shift)-1; /* Opcode and info */ YASM_WRITE_8(buf, (code->info << 4) | (code->opcode & 0xF)); if (size != 0) { - yasm_intnum_get_sized(intn, buf, size, size*8, -shift, 0, 1); - buf += size; + yasm_intnum_get_sized(intn, buf, size, size*8, -shift, 0, 1); + buf += size; } yasm_intnum_destroy(intn); diff --git a/modules/objfmts/dbg/dbg-objfmt.c b/modules/objfmts/dbg/dbg-objfmt.c index 5017cb85..73a0dd35 100644 --- a/modules/objfmts/dbg/dbg-objfmt.c +++ b/modules/objfmts/dbg/dbg-objfmt.c @@ -32,7 +32,7 @@ typedef struct yasm_objfmt_dbg { - yasm_objfmt_base objfmt; /* base structure */ + yasm_objfmt_base objfmt; /* base structure */ FILE *dbgfile; } yasm_objfmt_dbg; @@ -49,8 +49,8 @@ dbg_objfmt_create(yasm_object *object) objfmt_dbg->dbgfile = tmpfile(); if (!objfmt_dbg->dbgfile) { - fprintf(stderr, N_("could not open temporary file")); - return 0; + fprintf(stderr, N_("could not open temporary file")); + return 0; } fprintf(objfmt_dbg->dbgfile, "create()\n"); return (yasm_objfmt *)objfmt_dbg; @@ -58,7 +58,7 @@ dbg_objfmt_create(yasm_object *object) static void dbg_objfmt_output(yasm_object *object, FILE *f, int all_syms, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)object->objfmt; char buf[1024]; @@ -67,8 +67,8 @@ dbg_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Copy temp file to real output file */ rewind(objfmt_dbg->dbgfile); while ((i = fread(buf, 1, 1024, objfmt_dbg->dbgfile))) { - if (fwrite(buf, 1, i, f) != i) - break; + if (fwrite(buf, 1, i, f) != i) + break; } /* Reassign objfmt debug file to output file */ @@ -98,22 +98,22 @@ dbg_objfmt_add_default_section(yasm_object *object) int isnew; retval = yasm_object_get_general(object, ".text", - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(200)), 0), - 0, 0, 0, &isnew, 0); + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(200)), 0), + 0, 0, 0, &isnew, 0); if (isnew) { - fprintf(objfmt_dbg->dbgfile, "(new) "); - yasm_symtab_define_label(object->symtab, ".text", - yasm_section_bcs_first(retval), 1, 0); - yasm_section_set_default(retval, 1); + fprintf(objfmt_dbg->dbgfile, "(new) "); + yasm_symtab_define_label(object->symtab, ".text", + yasm_section_bcs_first(retval), 1, 0); + yasm_section_set_default(retval, 1); } return retval; } static /*@observer@*/ /*@null@*/ yasm_section * dbg_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, - unsigned long line) + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + unsigned long line) { yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)object->objfmt; yasm_valparam *vp; @@ -127,20 +127,20 @@ dbg_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, fprintf(objfmt_dbg->dbgfile, ", %lu), returning ", line); if ((vp = yasm_vps_first(valparams)) && !vp->param && vp->val != NULL) { - retval = yasm_object_get_general(object, vp->val, - yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(200)), - line), 0, 0, 0, &isnew, line); - if (isnew) { - fprintf(objfmt_dbg->dbgfile, "(new) "); - yasm_symtab_define_label(object->symtab, vp->val, - yasm_section_bcs_first(retval), 1, line); - } - yasm_section_set_default(retval, 0); - fprintf(objfmt_dbg->dbgfile, "\"%s\" section\n", vp->val); - return retval; + retval = yasm_object_get_general(object, vp->val, + yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(200)), + line), 0, 0, 0, &isnew, line); + if (isnew) { + fprintf(objfmt_dbg->dbgfile, "(new) "); + yasm_symtab_define_label(object->symtab, vp->val, + yasm_section_bcs_first(retval), 1, line); + } + yasm_section_set_default(retval, 0); + fprintf(objfmt_dbg->dbgfile, "\"%s\" section\n", vp->val); + return retval; } else { - fprintf(objfmt_dbg->dbgfile, "NULL\n"); - return NULL; + fprintf(objfmt_dbg->dbgfile, "NULL\n"); + return NULL; } } @@ -158,7 +158,7 @@ yasm_objfmt_module yasm_dbg_LTX_objfmt = { 32, dbg_objfmt_dbgfmt_keywords, "null", - NULL, /* no directives */ + NULL, /* no directives */ dbg_objfmt_create, dbg_objfmt_output, dbg_objfmt_destroy, diff --git a/modules/objfmts/elf/elf-machine.h b/modules/objfmts/elf/elf-machine.h index bec98dbf..325c34d5 100644 --- a/modules/objfmts/elf/elf-machine.h +++ b/modules/objfmts/elf/elf-machine.h @@ -42,10 +42,10 @@ YASM_WRITE_32_L(p, lo); \ YASM_WRITE_32_L(p, hi); } while (0) -#define YASM_WRITE_64Z_L(p, i) YASM_WRITE_64C_L(p, 0, i) +#define YASM_WRITE_64Z_L(p, i) YASM_WRITE_64C_L(p, 0, i) typedef int(*func_accepts_reloc)(size_t val, yasm_symrec *wrt, - yasm_symrec **ssyms); + yasm_symrec **ssyms); typedef void(*func_write_symtab_entry)(unsigned char *bufp, elf_symtab_entry *entry, yasm_intnum *value_intn, @@ -59,7 +59,7 @@ typedef void(*func_write_secthead_rel)(unsigned char *bufp, typedef void(*func_handle_reloc_addend)(yasm_intnum *intn, elf_reloc_entry *reloc); typedef unsigned int(*func_map_reloc_info_to_type)(elf_reloc_entry *reloc, - yasm_symrec **ssyms); + yasm_symrec **ssyms); typedef void(*func_write_reloc)(unsigned char *bufp, elf_reloc_entry *reloc, unsigned int r_type, @@ -70,8 +70,8 @@ typedef void (*func_write_proghead)(unsigned char **bufpp, elf_section_index shstrtab_index); typedef struct { - const char *name; /* should be something like ..name */ - const int sym_rel; /* symbol or section-relative? */ + const char *name; /* should be something like ..name */ + const int sym_rel; /* symbol or section-relative? */ } elf_machine_ssym; struct elf_machine_handler { @@ -92,10 +92,10 @@ struct elf_machine_handler { func_write_reloc write_reloc; func_write_proghead write_proghead; - const elf_machine_ssym *ssyms; /* array of "special" syms */ - const size_t num_ssyms; /* size of array */ + const elf_machine_ssym *ssyms; /* array of "special" syms */ + const size_t num_ssyms; /* size of array */ - const int bits; /* usually 32 or 64 */ + const int bits; /* usually 32 or 64 */ }; #endif /* ELF_MACHINE_H_INCLUDED */ diff --git a/modules/objfmts/elf/elf-objfmt.c b/modules/objfmts/elf/elf-objfmt.c index 70b0f835..90258f22 100644 --- a/modules/objfmts/elf/elf-objfmt.c +++ b/modules/objfmts/elf/elf-objfmt.c @@ -53,14 +53,14 @@ #include "elf-machine.h" typedef struct yasm_objfmt_elf { - yasm_objfmt_base objfmt; /* base structure */ + yasm_objfmt_base objfmt; /* base structure */ - elf_symtab_head* elf_symtab; /* symbol table of indexed syms */ - elf_strtab_head* shstrtab; /* section name strtab */ - elf_strtab_head* strtab; /* strtab entries */ + elf_symtab_head* elf_symtab; /* symbol table of indexed syms */ + elf_strtab_head* shstrtab; /* section name strtab */ + elf_strtab_head* strtab; /* strtab entries */ elf_strtab_entry *file_strtab_entry;/* .file symbol associated string */ - yasm_symrec *dotdotsym; /* ..sym symbol */ + yasm_symrec *dotdotsym; /* ..sym symbol */ } yasm_objfmt_elf; typedef struct { @@ -86,23 +86,23 @@ yasm_objfmt_module yasm_elf64_LTX_objfmt; static elf_symtab_entry * elf_objfmt_symtab_append(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym, - elf_section_index sectidx, elf_symbol_binding bind, - elf_symbol_type type, elf_symbol_vis vis, + elf_section_index sectidx, elf_symbol_binding bind, + elf_symbol_type type, elf_symbol_vis vis, yasm_expr *size, elf_address *value) { elf_symtab_entry *entry = yasm_symrec_get_data(sym, &elf_symrec_data); if (!entry) { - elf_strtab_entry *name = - elf_strtab_append_str(objfmt_elf->strtab, - yasm_symrec_get_name(sym)); - entry = elf_symtab_entry_create(name, sym); - yasm_symrec_add_data(sym, &elf_symrec_data, entry); + elf_strtab_entry *name = + elf_strtab_append_str(objfmt_elf->strtab, + yasm_symrec_get_name(sym)); + entry = elf_symtab_entry_create(name, sym); + yasm_symrec_add_data(sym, &elf_symrec_data, entry); } /* Only append to table if not already appended */ if (!elf_sym_in_table(entry)) - elf_symtab_append_entry(objfmt_elf->elf_symtab, entry); + elf_symtab_append_entry(objfmt_elf->elf_symtab, entry); elf_symtab_set_nonzero(entry, NULL, sectidx, bind, type, size, value); elf_sym_set_visibility(entry, vis); @@ -114,34 +114,34 @@ static elf_symtab_entry * build_extern(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym) { yasm_valparamhead *objext_valparams = - yasm_symrec_get_objext_valparams(sym); + yasm_symrec_get_objext_valparams(sym); if (objext_valparams) { - yasm_valparam *vp = yasm_vps_first(objext_valparams); - for (; vp; vp = yasm_vps_next(vp)) { - if (vp->val) - yasm_error_set(YASM_ERROR_TYPE, - N_("unrecognized symbol type `%s'"), vp->val); - } + yasm_valparam *vp = yasm_vps_first(objext_valparams); + for (; vp; vp = yasm_vps_next(vp)) { + if (vp->val) + yasm_error_set(YASM_ERROR_TYPE, + N_("unrecognized symbol type `%s'"), vp->val); + } } return elf_objfmt_symtab_append(objfmt_elf, sym, SHN_UNDEF, STB_GLOBAL, 0, - STV_DEFAULT, NULL, NULL); + STV_DEFAULT, NULL, NULL); } static elf_symtab_entry * build_global(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym) { yasm_valparamhead *objext_valparams = - yasm_symrec_get_objext_valparams(sym); + yasm_symrec_get_objext_valparams(sym); elf_symbol_type type = 0; yasm_expr *size = NULL; elf_symbol_vis vis = STV_DEFAULT; unsigned int vis_overrides = 0; if (objext_valparams) { - yasm_valparam *vp = yasm_vps_first(objext_valparams); - for (; vp; vp = yasm_vps_next(vp)) + yasm_valparam *vp = yasm_vps_first(objext_valparams); + for (; vp; vp = yasm_vps_next(vp)) { if (vp->val) { if (yasm__strcasecmp(vp->val, "function") == 0) @@ -163,14 +163,14 @@ build_global(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym) } else yasm_error_set(YASM_ERROR_TYPE, - N_("unrecognized symbol type `%s'"), - vp->val); + N_("unrecognized symbol type `%s'"), + vp->val); } else if (vp->param && !size) { size = vp->param; - vp->param = NULL; /* to avoid double-free of expr */ + vp->param = NULL; /* to avoid double-free of expr */ } - } + } if (vis_overrides > 1) { yasm_warn_set(YASM_WARN_GENERAL, N_("More than one symbol visibility provided; using last")); @@ -178,7 +178,7 @@ build_global(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym) } return elf_objfmt_symtab_append(objfmt_elf, sym, SHN_UNDEF, STB_GLOBAL, - type, vis, size, NULL); + type, vis, size, NULL); } static /*@null@*/ elf_symtab_entry * @@ -186,11 +186,11 @@ build_common(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym) { yasm_expr **size = yasm_symrec_get_common_size(sym); yasm_valparamhead *objext_valparams = - yasm_symrec_get_objext_valparams(sym); + yasm_symrec_get_objext_valparams(sym); unsigned long addralign = 0; if (objext_valparams) { - yasm_valparam *vp = yasm_vps_first(objext_valparams); + yasm_valparam *vp = yasm_vps_first(objext_valparams); for (; vp; vp = yasm_vps_next(vp)) { if (!vp->val && vp->param) { /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; @@ -198,7 +198,7 @@ build_common(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym) align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("alignment constraint is not an integer")); + N_("alignment constraint is not an integer")); return NULL; } addralign = yasm_intnum_get_uint(align_expr); @@ -216,7 +216,7 @@ build_common(yasm_objfmt_elf *objfmt_elf, yasm_symrec *sym) } return elf_objfmt_symtab_append(objfmt_elf, sym, SHN_COMMON, STB_GLOBAL, - 0, STV_DEFAULT, *size, &addralign); + 0, STV_DEFAULT, *size, &addralign); } static int @@ -233,79 +233,79 @@ elf_objfmt_build_symtab(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (vis & YASM_SYM_EXTERN) { - entry = build_extern(info->objfmt_elf, sym); - yasm_errwarn_propagate(info->errwarns, - yasm_symrec_get_decl_line(sym)); - return 0; + entry = build_extern(info->objfmt_elf, sym); + yasm_errwarn_propagate(info->errwarns, + yasm_symrec_get_decl_line(sym)); + return 0; } if (vis & YASM_SYM_COMMON) { - entry = build_common(info->objfmt_elf, sym); - yasm_errwarn_propagate(info->errwarns, - yasm_symrec_get_decl_line(sym)); - /* If the COMMON variable was actually defined, fall through. */ - if (!(status & YASM_SYM_DEFINED)) - return 0; + entry = build_common(info->objfmt_elf, sym); + yasm_errwarn_propagate(info->errwarns, + yasm_symrec_get_decl_line(sym)); + /* If the COMMON variable was actually defined, fall through. */ + if (!(status & YASM_SYM_DEFINED)) + return 0; } /* Ignore any undefined at this point. */ if (!(status & YASM_SYM_DEFINED)) - return 0; + return 0; if (!yasm_symrec_get_label(sym, &precbc)) { - if (!yasm_symrec_get_equ(sym) && !yasm_symrec_is_abs(sym)) - return 0; - precbc = NULL; + if (!yasm_symrec_get_equ(sym) && !yasm_symrec_is_abs(sym)) + return 0; + precbc = NULL; } if (precbc) - sect = yasm_bc_get_section(precbc); + sect = yasm_bc_get_section(precbc); if (entry && elf_sym_in_table(entry)) - ; + ; else if (vis & YASM_SYM_GLOBAL) { - entry = build_global(info->objfmt_elf, sym); - yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); + entry = build_global(info->objfmt_elf, sym); + yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); } else { - int is_sect = 0; - - /* Locals (except when debugging) do not need to be - * in the symbol table, unless they're a section. - */ - if (sect && !yasm_section_is_absolute(sect) && - strcmp(yasm_symrec_get_name(sym), yasm_section_get_name(sect))==0) - is_sect = 1; + int is_sect = 0; + + /* Locals (except when debugging) do not need to be + * in the symbol table, unless they're a section. + */ + if (sect && !yasm_section_is_absolute(sect) && + strcmp(yasm_symrec_get_name(sym), yasm_section_get_name(sect))==0) + is_sect = 1; #if 0 - /* FIXME: to enable this we must have handling in place for special - * symbols. - */ - if (!info->local_names && !is_sect) - return 0; + /* FIXME: to enable this we must have handling in place for special + * symbols. + */ + if (!info->local_names && !is_sect) + return 0; #else - if (yasm_symrec_get_equ(sym) && !yasm_symrec_is_abs(sym)) - return 0; + if (yasm_symrec_get_equ(sym) && !yasm_symrec_is_abs(sym)) + return 0; #endif - entry = yasm_symrec_get_data(sym, &elf_symrec_data); - if (!entry) { - elf_strtab_entry *name = !info->local_names || is_sect ? NULL : - elf_strtab_append_str(info->objfmt_elf->strtab, - yasm_symrec_get_name(sym)); - entry = elf_symtab_entry_create(name, sym); - yasm_symrec_add_data(sym, &elf_symrec_data, entry); - } - - if (!elf_sym_in_table(entry)) - elf_symtab_insert_local_sym(info->objfmt_elf->elf_symtab, entry); - - elf_symtab_set_nonzero(entry, sect, 0, STB_LOCAL, - is_sect ? STT_SECTION : 0, NULL, 0); - - if (is_sect) - return 0; + entry = yasm_symrec_get_data(sym, &elf_symrec_data); + if (!entry) { + elf_strtab_entry *name = !info->local_names || is_sect ? NULL : + elf_strtab_append_str(info->objfmt_elf->strtab, + yasm_symrec_get_name(sym)); + entry = elf_symtab_entry_create(name, sym); + yasm_symrec_add_data(sym, &elf_symrec_data, entry); + } + + if (!elf_sym_in_table(entry)) + elf_symtab_insert_local_sym(info->objfmt_elf->elf_symtab, entry); + + elf_symtab_set_nonzero(entry, sect, 0, STB_LOCAL, + is_sect ? STT_SECTION : 0, NULL, 0); + + if (is_sect) + return 0; } if (precbc) - value = yasm_bc_next_offset(precbc); + value = yasm_bc_next_offset(precbc); elf_symtab_set_nonzero(entry, sect, 0, 0, 0, NULL, &value); return 0; @@ -313,8 +313,8 @@ elf_objfmt_build_symtab(yasm_symrec *sym, /*@null@*/ void *d) static yasm_objfmt * elf_objfmt_create_common(yasm_object *object, yasm_objfmt_module *module, - int bits_pref, - const elf_machine_handler **elf_march_out) + int bits_pref, + const elf_machine_handler **elf_march_out) { yasm_objfmt_elf *objfmt_elf = yasm_xmalloc(sizeof(yasm_objfmt_elf)); yasm_symrec *filesym; @@ -324,11 +324,11 @@ elf_objfmt_create_common(yasm_object *object, yasm_objfmt_module *module, objfmt_elf->objfmt.module = module; elf_march = elf_set_arch(object->arch, object->symtab, bits_pref); if (!elf_march) { - yasm_xfree(objfmt_elf); - return NULL; + yasm_xfree(objfmt_elf); + return NULL; } if (elf_march_out) - *elf_march_out = elf_march; + *elf_march_out = elf_march; objfmt_elf->shstrtab = elf_strtab_create(); objfmt_elf->strtab = elf_strtab_create(); @@ -338,16 +338,16 @@ elf_objfmt_create_common(yasm_object *object, yasm_objfmt_module *module, filesym = yasm_symtab_define_label(object->symtab, ".file", NULL, 0, 0); /* Put in current input filename; we'll replace it in output() */ objfmt_elf->file_strtab_entry = - elf_strtab_append_str(objfmt_elf->strtab, object->src_filename); + elf_strtab_append_str(objfmt_elf->strtab, object->src_filename); entry = elf_symtab_entry_create(objfmt_elf->file_strtab_entry, filesym); yasm_symrec_add_data(filesym, &elf_symrec_data, entry); elf_symtab_set_nonzero(entry, NULL, SHN_ABS, STB_LOCAL, STT_FILE, NULL, - NULL); + NULL); elf_symtab_append_entry(objfmt_elf->elf_symtab, entry); /* FIXME: misuse of NULL bytecode */ objfmt_elf->dotdotsym = - yasm_symtab_define_label(object->symtab, "..sym", NULL, 1, 0); + yasm_symtab_define_label(object->symtab, "..sym", NULL, 1, 0); return (yasm_objfmt *)objfmt_elf; } @@ -360,14 +360,14 @@ elf_objfmt_create(yasm_object *object) yasm_objfmt_elf *objfmt_elf; objfmt = elf_objfmt_create_common(object, &yasm_elf_LTX_objfmt, 0, - &elf_march); + &elf_march); if (objfmt) { - objfmt_elf = (yasm_objfmt_elf *)objfmt; - /* Figure out which bitness of object format to use */ - if (elf_march->bits == 32) - objfmt_elf->objfmt.module = &yasm_elf32_LTX_objfmt; - else if (elf_march->bits == 64) - objfmt_elf->objfmt.module = &yasm_elf64_LTX_objfmt; + objfmt_elf = (yasm_objfmt_elf *)objfmt; + /* Figure out which bitness of object format to use */ + if (elf_march->bits == 32) + objfmt_elf->objfmt.module = &yasm_elf32_LTX_objfmt; + else if (elf_march->bits == 64) + objfmt_elf->objfmt.module = &yasm_elf64_LTX_objfmt; } return objfmt; } @@ -390,30 +390,30 @@ elf_objfmt_output_align(FILE *f, unsigned int align) long pos; unsigned long delta; if (!is_exp2(align)) - yasm_internal_error("requested alignment not a power of two"); + yasm_internal_error("requested alignment not a power of two"); pos = ftell(f); if (pos == -1) { - yasm_error_set(YASM_ERROR_IO, - N_("could not get file position on output file")); - return -1; + yasm_error_set(YASM_ERROR_IO, + N_("could not get file position on output file")); + return -1; } delta = align - (pos & (align-1)); if (delta != align) { - pos += delta; - if (fseek(f, pos, SEEK_SET) < 0) { - yasm_error_set(YASM_ERROR_IO, - N_("could not set file position on output file")); - return -1; - } + pos += delta; + if (fseek(f, pos, SEEK_SET) < 0) { + yasm_error_set(YASM_ERROR_IO, + N_("could not set file position on output file")); + return -1; + } } return pos; } static int elf_objfmt_output_reloc(yasm_symrec *sym, yasm_bytecode *bc, - unsigned char *buf, unsigned int destsize, - unsigned int valsize, int warn, void *d) + unsigned char *buf, unsigned int destsize, + unsigned int valsize, int warn, void *d) { elf_reloc_entry *reloc; elf_objfmt_output_info *info = d; @@ -421,10 +421,10 @@ elf_objfmt_output_reloc(yasm_symrec *sym, yasm_bytecode *bc, int retval; reloc = elf_reloc_entry_create(sym, NULL, - yasm_intnum_create_uint(bc->offset), 0, valsize); + yasm_intnum_create_uint(bc->offset), 0, valsize); if (reloc == NULL) { - yasm_error_set(YASM_ERROR_TYPE, N_("elf: invalid relocation size")); - return 1; + yasm_error_set(YASM_ERROR_TYPE, N_("elf: invalid relocation size")); + return 1; } /* allocate .rel[a] sections on a need-basis */ elf_secthead_append_reloc(info->sect, info->shead, reloc); @@ -432,15 +432,15 @@ elf_objfmt_output_reloc(yasm_symrec *sym, yasm_bytecode *bc, zero = yasm_intnum_create_uint(0); elf_handle_reloc_addend(zero, reloc); retval = yasm_arch_intnum_tobytes(info->object->arch, zero, buf, destsize, - valsize, 0, bc, warn); + valsize, 0, bc, warn); yasm_intnum_destroy(zero); return retval; } static int elf_objfmt_output_value(yasm_value *value, unsigned char *buf, - unsigned int destsize, unsigned long offset, - yasm_bytecode *bc, int warn, /*@null@*/ void *d) + unsigned int destsize, unsigned long offset, + yasm_bytecode *bc, int warn, /*@null@*/ void *d) { /*@null@*/ elf_objfmt_output_info *info = (elf_objfmt_output_info *)d; /*@dependent@*/ /*@null@*/ yasm_intnum *intn; @@ -450,96 +450,96 @@ elf_objfmt_output_value(yasm_value *value, unsigned char *buf, unsigned int valsize = value->size; if (info == NULL) - yasm_internal_error("null info struct"); + yasm_internal_error("null info struct"); if (value->abs) - value->abs = yasm_expr_simplify(value->abs, 1); + value->abs = yasm_expr_simplify(value->abs, 1); /* Try to output constant and PC-relative section-local first. * Note this does NOT output any value with a SEG, WRT, external, * cross-section, or non-PC-relative reference (those are handled below). */ switch (yasm_value_output_basic(value, buf, destsize, bc, warn, - info->object->arch)) { - case -1: - return 1; - case 0: - break; - default: - return 0; + info->object->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; } /* Handle other expressions, with relocation if necessary */ if (value->seg_of || value->section_rel || value->rshift > 0) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("elf: relocation too complex")); - return 1; + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("elf: relocation too complex")); + return 1; } intn_val = 0; if (value->rel) { - yasm_sym_vis vis = yasm_symrec_get_visibility(value->rel); - /*@dependent@*/ /*@null@*/ yasm_symrec *sym = value->rel; - /*@dependent@*/ /*@null@*/ yasm_symrec *wrt = value->wrt; - - if (wrt == info->objfmt_elf->dotdotsym) - wrt = NULL; - else if (wrt && elf_is_wrt_sym_relative(wrt)) - ; - else if (vis == YASM_SYM_LOCAL) { - yasm_bytecode *sym_precbc; - /* Local symbols need relocation to their section's start, and - * add in the offset of the bytecode (within the target section) - * into the abs portion. - * - * This is only done if the symbol is relocated against the - * section instead of the symbol itself. - */ - if (yasm_symrec_get_label(sym, &sym_precbc)) { - /* Relocate to section start */ - yasm_section *sym_sect = yasm_bc_get_section(sym_precbc); - /*@null@*/ elf_secthead *sym_shead; - sym_shead = yasm_section_get_data(sym_sect, &elf_section_data); - assert(sym_shead != NULL); - sym = elf_secthead_get_sym(sym_shead); - - intn_val = yasm_bc_next_offset(sym_precbc); - } - } - - /* For PC-relative, need to add offset of expression within bc. */ - if (value->curpos_rel) - intn_val += offset; - - reloc = elf_reloc_entry_create(sym, wrt, - yasm_intnum_create_uint(bc->offset + offset), value->curpos_rel, - valsize); - if (reloc == NULL) { - yasm_error_set(YASM_ERROR_TYPE, - N_("elf: invalid relocation (WRT or size)")); - return 1; - } - /* allocate .rel[a] sections on a need-basis */ - elf_secthead_append_reloc(info->sect, info->shead, reloc); + yasm_sym_vis vis = yasm_symrec_get_visibility(value->rel); + /*@dependent@*/ /*@null@*/ yasm_symrec *sym = value->rel; + /*@dependent@*/ /*@null@*/ yasm_symrec *wrt = value->wrt; + + if (wrt == info->objfmt_elf->dotdotsym) + wrt = NULL; + else if (wrt && elf_is_wrt_sym_relative(wrt)) + ; + else if (vis == YASM_SYM_LOCAL) { + yasm_bytecode *sym_precbc; + /* Local symbols need relocation to their section's start, and + * add in the offset of the bytecode (within the target section) + * into the abs portion. + * + * This is only done if the symbol is relocated against the + * section instead of the symbol itself. + */ + if (yasm_symrec_get_label(sym, &sym_precbc)) { + /* Relocate to section start */ + yasm_section *sym_sect = yasm_bc_get_section(sym_precbc); + /*@null@*/ elf_secthead *sym_shead; + sym_shead = yasm_section_get_data(sym_sect, &elf_section_data); + assert(sym_shead != NULL); + sym = elf_secthead_get_sym(sym_shead); + + intn_val = yasm_bc_next_offset(sym_precbc); + } + } + + /* For PC-relative, need to add offset of expression within bc. */ + if (value->curpos_rel) + intn_val += offset; + + reloc = elf_reloc_entry_create(sym, wrt, + yasm_intnum_create_uint(bc->offset + offset), value->curpos_rel, + valsize); + if (reloc == NULL) { + yasm_error_set(YASM_ERROR_TYPE, + N_("elf: invalid relocation (WRT or size)")); + return 1; + } + /* allocate .rel[a] sections on a need-basis */ + elf_secthead_append_reloc(info->sect, info->shead, reloc); } intn = yasm_intnum_create_uint(intn_val); if (value->abs) { - yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); - if (!intn2) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("elf: relocation too complex")); - yasm_intnum_destroy(intn); - return 1; - } - yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); + yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); + if (!intn2) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("elf: relocation too complex")); + yasm_intnum_destroy(intn); + return 1; + } + yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); } if (reloc) - elf_handle_reloc_addend(intn, reloc); + elf_handle_reloc_addend(intn, reloc); retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize, - valsize, 0, bc, warn); + valsize, 0, bc, warn); yasm_intnum_destroy(intn); return retval; } @@ -554,44 +554,44 @@ elf_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) int gap; if (info == NULL) - yasm_internal_error("null info struct"); + yasm_internal_error("null info struct"); bigbuf = yasm_bc_tobytes(bc, buf, &size, &gap, info, - elf_objfmt_output_value, elf_objfmt_output_reloc); + elf_objfmt_output_value, elf_objfmt_output_reloc); /* Don't bother doing anything else if size ended up being 0. */ if (size == 0) { - if (bigbuf) - yasm_xfree(bigbuf); - return 0; + if (bigbuf) + yasm_xfree(bigbuf); + return 0; } else { - yasm_intnum *bcsize = yasm_intnum_create_uint(size); - elf_secthead_add_size(info->shead, bcsize); - yasm_intnum_destroy(bcsize); + yasm_intnum *bcsize = yasm_intnum_create_uint(size); + elf_secthead_add_size(info->shead, bcsize); + yasm_intnum_destroy(bcsize); } /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { - unsigned long left; - yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, - N_("uninitialized space declared in code/data section: zeroing")); - /* Write out in chunks */ - memset(buf, 0, 256); - left = size; - while (left > 256) { - fwrite(buf, 256, 1, info->f); - left -= 256; - } - fwrite(buf, left, 1, info->f); + unsigned long left; + yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, + N_("uninitialized space declared in code/data section: zeroing")); + /* Write out in chunks */ + memset(buf, 0, 256); + left = size; + while (left > 256) { + fwrite(buf, 256, 1, info->f); + left -= 256; + } + fwrite(buf, left, 1, info->f); } else { - /* Output buf (or bigbuf if non-NULL) to file */ - fwrite(bigbuf ? bigbuf : buf, (size_t)size, 1, info->f); + /* Output buf (or bigbuf if non-NULL) to file */ + fwrite(bigbuf ? bigbuf : buf, (size_t)size, 1, info->f); } /* If bigbuf was allocated, free it */ if (bigbuf) - yasm_xfree(bigbuf); + yasm_xfree(bigbuf); return 0; } @@ -609,25 +609,25 @@ elf_objfmt_create_dbg_secthead(yasm_section *sect, /*@null@*/ void *d) shead = yasm_section_get_data(sect, &elf_section_data); if (yasm_section_is_absolute(sect) || shead) - return 0; /* only create new secthead if missing and non-absolute */ + return 0; /* only create new secthead if missing and non-absolute */ sectname = yasm_section_get_name(sect); name = elf_strtab_append_str(info->objfmt_elf->shstrtab, sectname); if (yasm__strcasecmp(sectname, ".stab")==0) { - entsize = 12; + entsize = 12; } else if (yasm__strcasecmp(sectname, ".stabstr")==0) { - type = SHT_STRTAB; + type = SHT_STRTAB; } else if (yasm__strncasecmp(sectname, ".debug_", 7)==0) { - ; + ; } else - yasm_internal_error(N_("Unrecognized section without data")); + yasm_internal_error(N_("Unrecognized section without data")); shead = elf_secthead_create(name, type, 0, 0, 0); elf_secthead_set_entsize(shead, entsize); sym = yasm_symtab_define_label(info->object->symtab, sectname, - yasm_section_bcs_first(sect), 1, 0); + yasm_section_bcs_first(sect), 1, 0); elf_secthead_set_sym(shead, sym); yasm_section_add_data(sect, &elf_section_data, shead); @@ -646,53 +646,53 @@ elf_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; if (info == NULL) - yasm_internal_error("null info struct"); + yasm_internal_error("null info struct"); shead = yasm_section_get_data(sect, &elf_section_data); if (shead == NULL) - yasm_internal_error("no associated data"); + yasm_internal_error("no associated data"); if (elf_secthead_get_align(shead) == 0) - elf_secthead_set_align(shead, yasm_section_get_align(sect)); + elf_secthead_set_align(shead, yasm_section_get_align(sect)); /* don't output header-only sections */ if ((elf_secthead_get_type(shead) & SHT_NOBITS) == SHT_NOBITS) { - yasm_bytecode *last = yasm_section_bcs_last(sect); - if (last) { - yasm_intnum *sectsize; - sectsize = yasm_intnum_create_uint(yasm_bc_next_offset(last)); - elf_secthead_add_size(shead, sectsize); - yasm_intnum_destroy(sectsize); - } - elf_secthead_set_index(shead, ++info->sindex); - return 0; + yasm_bytecode *last = yasm_section_bcs_last(sect); + if (last) { + yasm_intnum *sectsize; + sectsize = yasm_intnum_create_uint(yasm_bc_next_offset(last)); + elf_secthead_add_size(shead, sectsize); + yasm_intnum_destroy(sectsize); + } + elf_secthead_set_index(shead, ++info->sindex); + return 0; } if ((pos = ftell(info->f)) == -1) { - yasm_error_set(YASM_ERROR_IO, - N_("couldn't read position on output stream")); - yasm_errwarn_propagate(info->errwarns, 0); + yasm_error_set(YASM_ERROR_IO, + N_("couldn't read position on output stream")); + yasm_errwarn_propagate(info->errwarns, 0); } pos = elf_secthead_set_file_offset(shead, pos); if (fseek(info->f, pos, SEEK_SET) < 0) { - yasm_error_set(YASM_ERROR_IO, N_("couldn't seek on output stream")); - yasm_errwarn_propagate(info->errwarns, 0); + yasm_error_set(YASM_ERROR_IO, N_("couldn't seek on output stream")); + yasm_errwarn_propagate(info->errwarns, 0); } info->sect = sect; info->shead = shead; yasm_section_bcs_traverse(sect, info->errwarns, info, - elf_objfmt_output_bytecode); + elf_objfmt_output_bytecode); elf_secthead_set_index(shead, ++info->sindex); /* No relocations to output? Go on to next section */ if (elf_secthead_write_relocs_to_file(info->f, sect, shead, - info->errwarns) == 0) - return 0; + info->errwarns) == 0) + return 0; elf_secthead_set_rel_index(shead, ++info->sindex); /* name the relocation section .rel[a].foo */ @@ -713,30 +713,30 @@ elf_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; if (info == NULL) - yasm_internal_error("null info struct"); + yasm_internal_error("null info struct"); shead = yasm_section_get_data(sect, &elf_section_data); if (shead == NULL) - yasm_internal_error("no section header attached to section"); + yasm_internal_error("no section header attached to section"); if(elf_secthead_write_to_file(info->f, shead, info->sindex+1)) - info->sindex++; + info->sindex++; /* output strtab headers here? */ /* relocation entries for .foo are stored in section .rel[a].foo */ if(elf_secthead_write_rel_to_file(info->f, 3, sect, shead, - info->sindex+1)) - info->sindex++; + info->sindex+1)) + info->sindex++; return 0; } static void elf_objfmt_output(yasm_object *object, FILE *f, int all_syms, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_objfmt_elf *objfmt_elf = (yasm_objfmt_elf *)object->objfmt; elf_objfmt_output_info info; @@ -756,19 +756,19 @@ elf_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Update filename strtab */ elf_strtab_entry_set_str(objfmt_elf->file_strtab_entry, - object->src_filename); + object->src_filename); /* Allocate space for Ehdr by seeking forward */ if (fseek(f, (long)(elf_proghead_get_size()), SEEK_SET) < 0) { - yasm_error_set(YASM_ERROR_IO, N_("could not seek on output file")); - yasm_errwarn_propagate(errwarns, 0); - return; + yasm_error_set(YASM_ERROR_IO, N_("could not seek on output file")); + yasm_errwarn_propagate(errwarns, 0); + return; } /* Create missing section headers */ if (yasm_object_sections_traverse(object, &info, - elf_objfmt_create_dbg_secthead)) - return; + elf_objfmt_create_dbg_secthead)) + return; /* add all (local) syms to symtab because relocation needs a symtab index * if all_syms, register them by name. if not, use strtab entry 0 */ @@ -776,70 +776,70 @@ elf_objfmt_output(yasm_object *object, FILE *f, int all_syms, buildsym_info.errwarns = errwarns; buildsym_info.local_names = all_syms; yasm_symtab_traverse(object->symtab, &buildsym_info, - elf_objfmt_build_symtab); + elf_objfmt_build_symtab); elf_symtab_nlocal = elf_symtab_assign_indices(objfmt_elf->elf_symtab); /* output known sections - includes reloc sections which aren't in yasm's * list. Assign indices as we go. */ info.sindex = 3; if (yasm_object_sections_traverse(object, &info, - elf_objfmt_output_section)) - return; + elf_objfmt_output_section)) + return; /* add final sections to the shstrtab */ elf_strtab_name = elf_strtab_append_str(objfmt_elf->shstrtab, ".strtab"); elf_symtab_name = elf_strtab_append_str(objfmt_elf->shstrtab, ".symtab"); elf_shstrtab_name = elf_strtab_append_str(objfmt_elf->shstrtab, - ".shstrtab"); + ".shstrtab"); /* output .shstrtab */ if ((pos = elf_objfmt_output_align(f, 4)) == -1) { - yasm_errwarn_propagate(errwarns, 0); - return; + yasm_errwarn_propagate(errwarns, 0); + return; } elf_shstrtab_offset = (unsigned long) pos; elf_shstrtab_size = elf_strtab_output_to_file(f, objfmt_elf->shstrtab); /* output .strtab */ if ((pos = elf_objfmt_output_align(f, 4)) == -1) { - yasm_errwarn_propagate(errwarns, 0); - return; + yasm_errwarn_propagate(errwarns, 0); + return; } elf_strtab_offset = (unsigned long) pos; elf_strtab_size = elf_strtab_output_to_file(f, objfmt_elf->strtab); /* output .symtab - last section so all others have indexes */ if ((pos = elf_objfmt_output_align(f, 4)) == -1) { - yasm_errwarn_propagate(errwarns, 0); - return; + yasm_errwarn_propagate(errwarns, 0); + return; } elf_symtab_offset = (unsigned long) pos; elf_symtab_size = elf_symtab_write_to_file(f, objfmt_elf->elf_symtab, - errwarns); + errwarns); /* output section header table */ if ((pos = elf_objfmt_output_align(f, 16)) == -1) { - yasm_errwarn_propagate(errwarns, 0); - return; + yasm_errwarn_propagate(errwarns, 0); + return; } elf_shead_addr = (unsigned long) pos; /* stabs debugging support */ if (strcmp(yasm_dbgfmt_keyword(object->dbgfmt), "stabs")==0) { - yasm_section *stabsect = yasm_object_find_general(object, ".stab"); - yasm_section *stabstrsect = - yasm_object_find_general(object, ".stabstr"); - if (stabsect && stabstrsect) { - elf_secthead *stab = - yasm_section_get_data(stabsect, &elf_section_data); - elf_secthead *stabstr = - yasm_section_get_data(stabstrsect, &elf_section_data); - if (stab && stabstr) { - elf_secthead_set_link(stab, elf_secthead_get_index(stabstr)); - } - else - yasm_internal_error(N_("missing .stab or .stabstr section/data")); - } + yasm_section *stabsect = yasm_object_find_general(object, ".stab"); + yasm_section *stabstrsect = + yasm_object_find_general(object, ".stabstr"); + if (stabsect && stabstrsect) { + elf_secthead *stab = + yasm_section_get_data(stabsect, &elf_section_data); + elf_secthead *stabstr = + yasm_section_get_data(stabstrsect, &elf_section_data); + if (stab && stabstr) { + elf_secthead_set_link(stab, elf_secthead_get_index(stabstr)); + } + else + yasm_internal_error(N_("missing .stab or .stabstr section/data")); + } } /* output dummy section header - 0 */ @@ -851,22 +851,22 @@ elf_objfmt_output(yasm_object *object, FILE *f, int all_syms, elf_secthead_destroy(esdn); esdn = elf_secthead_create(elf_shstrtab_name, SHT_STRTAB, 0, - elf_shstrtab_offset, elf_shstrtab_size); + elf_shstrtab_offset, elf_shstrtab_size); elf_secthead_set_index(esdn, 1); elf_secthead_write_to_file(f, esdn, 1); elf_secthead_destroy(esdn); esdn = elf_secthead_create(elf_strtab_name, SHT_STRTAB, 0, - elf_strtab_offset, elf_strtab_size); + elf_strtab_offset, elf_strtab_size); elf_secthead_set_index(esdn, 2); elf_secthead_write_to_file(f, esdn, 2); elf_secthead_destroy(esdn); esdn = elf_secthead_create(elf_symtab_name, SHT_SYMTAB, 0, - elf_symtab_offset, elf_symtab_size); + elf_symtab_offset, elf_symtab_size); elf_secthead_set_index(esdn, 3); elf_secthead_set_info(esdn, elf_symtab_nlocal); - elf_secthead_set_link(esdn, 2); /* for .strtab, which is index 2 */ + elf_secthead_set_link(esdn, 2); /* for .strtab, which is index 2 */ elf_secthead_write_to_file(f, esdn, 3); elf_secthead_destroy(esdn); @@ -876,9 +876,9 @@ elf_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* output Ehdr */ if (fseek(f, 0, SEEK_SET) < 0) { - yasm_error_set(YASM_ERROR_IO, N_("could not seek on output file")); - yasm_errwarn_propagate(errwarns, 0); - return; + yasm_error_set(YASM_ERROR_IO, N_("could not seek on output file")); + yasm_errwarn_propagate(errwarns, 0); + return; } elf_proghead_write_to_file(f, elf_shead_addr, info.sindex+1, 1); @@ -896,19 +896,19 @@ elf_objfmt_destroy(yasm_objfmt *objfmt) static elf_secthead * elf_objfmt_init_new_section(yasm_object *object, yasm_section *sect, - const char *sectname, unsigned long type, - unsigned long flags, unsigned long line) + const char *sectname, unsigned long type, + unsigned long flags, unsigned long line) { yasm_objfmt_elf *objfmt_elf = (yasm_objfmt_elf *)object->objfmt; elf_secthead *esd; yasm_symrec *sym; elf_strtab_entry *name = elf_strtab_append_str(objfmt_elf->shstrtab, - sectname); + sectname); esd = elf_secthead_create(name, type, flags, 0, 0); yasm_section_add_data(sect, &elf_section_data, esd); sym = yasm_symtab_define_label(object->symtab, sectname, - yasm_section_bcs_first(sect), 1, line); + yasm_section_bcs_first(sect), 1, line); elf_secthead_set_sym(esd, sym); @@ -924,15 +924,15 @@ elf_objfmt_add_default_section(yasm_object *object) retval = yasm_object_get_general(object, ".text", 0, 16, 1, 0, &isnew, 0); esd = elf_objfmt_init_new_section(object, retval, ".text", SHT_PROGBITS, - SHF_ALLOC + SHF_EXECINSTR, 0); + SHF_ALLOC + SHF_EXECINSTR, 0); yasm_section_set_default(retval, 1); return retval; } static /*@observer@*/ /*@null@*/ yasm_section * elf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, - /*@null@*/ yasm_valparamhead *objext_valparams, - unsigned long line) + /*@null@*/ yasm_valparamhead *objext_valparams, + unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_section *retval; @@ -944,121 +944,121 @@ elf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, char *sectname; int resonly = 0; static const struct { - const char *name; - unsigned long flags; + const char *name; + unsigned long flags; } flagquals[] = { - { "alloc", SHF_ALLOC }, - { "exec", SHF_EXECINSTR }, - { "write", SHF_WRITE }, - /*{ "progbits", SHT_PROGBITS },*/ - /*{ "align", 0 } */ + { "alloc", SHF_ALLOC }, + { "exec", SHF_EXECINSTR }, + { "write", SHF_WRITE }, + /*{ "progbits", SHT_PROGBITS },*/ + /*{ "align", 0 } */ }; /*@dependent@*/ /*@null@*/ const yasm_intnum *merge_intn = NULL; elf_secthead *esd; if (!vp || vp->param || !vp->val) - return NULL; + return NULL; sectname = vp->val; if (strcmp(sectname, ".bss") == 0) { - type = SHT_NOBITS; - flags = SHF_ALLOC + SHF_WRITE; - resonly = 1; + type = SHT_NOBITS; + flags = SHF_ALLOC + SHF_WRITE; + resonly = 1; } else if (strcmp(sectname, ".data") == 0) { - type = SHT_PROGBITS; - flags = SHF_ALLOC + SHF_WRITE; + type = SHT_PROGBITS; + flags = SHF_ALLOC + SHF_WRITE; } else if (strcmp(sectname, ".rodata") == 0) { - type = SHT_PROGBITS; - flags = SHF_ALLOC; + type = SHT_PROGBITS; + flags = SHF_ALLOC; } else if (strcmp(sectname, ".text") == 0) { - align = 16; - type = SHT_PROGBITS; - flags = SHF_ALLOC + SHF_EXECINSTR; + align = 16; + type = SHT_PROGBITS; + flags = SHF_ALLOC + SHF_EXECINSTR; } else if (strcmp(sectname, ".comment") == 0) { - align = 0; - type = SHT_PROGBITS; - flags = 0; + align = 0; + type = SHT_PROGBITS; + flags = 0; } else { - /* Default to code */ - align = 1; + /* Default to code */ + align = 1; } while ((vp = yasm_vps_next(vp))) { - size_t i; - int match; - - if (!vp->val) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized numeric qualifier")); - continue; - } - - match = 0; - for (i=0; ival, flagquals[i].name) == 0) { - flags_override = 1; - match = 1; - flags |= flagquals[i].flags; - } - else if (yasm__strcasecmp(vp->val+2, flagquals[i].name) == 0 - && yasm__strncasecmp(vp->val, "no", 2) == 0) { - flags &= ~flagquals[i].flags; - flags_override = 1; - match = 1; - } - } - - if (match) - ; - else if (yasm__strncasecmp(vp->val, "gas_", 4) == 0) { - /* GAS-style flags */ - flags = 0; - for (i=4; ival); i++) { - switch (vp->val[i]) { - case 'a': - flags |= SHF_ALLOC; - break; - case 'w': - flags |= SHF_WRITE; - break; - case 'x': - flags |= SHF_EXECINSTR; - break; - case 'M': - flags |= SHF_MERGE; - break; - case 'S': - flags |= SHF_STRINGS; - break; - case 'G': - flags |= SHF_GROUP; - break; - case 'T': - flags |= SHF_TLS; - break; - default: - yasm_warn_set(YASM_WARN_GENERAL, - N_("unrecognized section attribute: `%c'"), - vp->val[i]); - } - } - } else if (yasm__strcasecmp(vp->val, "progbits") == 0) { - type |= SHT_PROGBITS; - } - else if (yasm__strcasecmp(vp->val, "noprogbits") == 0 || - yasm__strcasecmp(vp->val, "nobits") == 0) { - type &= ~SHT_PROGBITS; - type |= SHT_NOBITS; - } - else if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { + size_t i; + int match; + + if (!vp->val) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized numeric qualifier")); + continue; + } + + match = 0; + for (i=0; ival, flagquals[i].name) == 0) { + flags_override = 1; + match = 1; + flags |= flagquals[i].flags; + } + else if (yasm__strcasecmp(vp->val+2, flagquals[i].name) == 0 + && yasm__strncasecmp(vp->val, "no", 2) == 0) { + flags &= ~flagquals[i].flags; + flags_override = 1; + match = 1; + } + } + + if (match) + ; + else if (yasm__strncasecmp(vp->val, "gas_", 4) == 0) { + /* GAS-style flags */ + flags = 0; + for (i=4; ival); i++) { + switch (vp->val[i]) { + case 'a': + flags |= SHF_ALLOC; + break; + case 'w': + flags |= SHF_WRITE; + break; + case 'x': + flags |= SHF_EXECINSTR; + break; + case 'M': + flags |= SHF_MERGE; + break; + case 'S': + flags |= SHF_STRINGS; + break; + case 'G': + flags |= SHF_GROUP; + break; + case 'T': + flags |= SHF_TLS; + break; + default: + yasm_warn_set(YASM_WARN_GENERAL, + N_("unrecognized section attribute: `%c'"), + vp->val[i]); + } + } + } else if (yasm__strcasecmp(vp->val, "progbits") == 0) { + type |= SHT_PROGBITS; + } + else if (yasm__strcasecmp(vp->val, "noprogbits") == 0 || + yasm__strcasecmp(vp->val, "nobits") == 0) { + type &= ~SHT_PROGBITS; + type |= SHT_NOBITS; + } + else if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; align_expr = yasm_expr_get_intnum(&vp->param, 0); if (!align_expr) { yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not an integer"), - vp->val); + N_("argument to `%s' is not an integer"), + vp->val); return NULL; } align = yasm_intnum_get_uint(align_expr); @@ -1066,55 +1066,55 @@ elf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, /* Alignments must be a power of two. */ if (!is_exp2(align)) { yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), + N_("argument to `%s' is not a power of two"), vp->val); return NULL; } - } else - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized qualifier `%s'"), vp->val); + } else + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized qualifier `%s'"), vp->val); } - /* Handle merge entity size */ - if (flags & SHF_MERGE) { - if (objext_valparams && (vp = yasm_vps_first(objext_valparams)) - && vp->param) { - - merge_intn = yasm_expr_get_intnum(&vp->param, 0); - if (!merge_intn) - yasm_warn_set(YASM_WARN_GENERAL, - N_("invalid merge entity size")); - } else { - yasm_warn_set(YASM_WARN_GENERAL, - N_("entity size for SHF_MERGE not specified")); - flags &= ~SHF_MERGE; - } - } + /* Handle merge entity size */ + if (flags & SHF_MERGE) { + if (objext_valparams && (vp = yasm_vps_first(objext_valparams)) + && vp->param) { + + merge_intn = yasm_expr_get_intnum(&vp->param, 0); + if (!merge_intn) + yasm_warn_set(YASM_WARN_GENERAL, + N_("invalid merge entity size")); + } else { + yasm_warn_set(YASM_WARN_GENERAL, + N_("entity size for SHF_MERGE not specified")); + flags &= ~SHF_MERGE; + } + } retval = yasm_object_get_general(object, sectname, 0, align, - (flags & SHF_EXECINSTR) != 0, resonly, - &isnew, line); + (flags & SHF_EXECINSTR) != 0, resonly, + &isnew, line); if (isnew) - esd = elf_objfmt_init_new_section(object, retval, sectname, type, - flags, line); + esd = elf_objfmt_init_new_section(object, retval, sectname, type, + flags, line); else - esd = yasm_section_get_data(retval, &elf_section_data); + esd = yasm_section_get_data(retval, &elf_section_data); if (isnew || yasm_section_is_default(retval)) { - yasm_section_set_default(retval, 0); - elf_secthead_set_typeflags(esd, type, flags); - if (merge_intn) - elf_secthead_set_entsize(esd, yasm_intnum_get_uint(merge_intn)); - yasm_section_set_align(retval, align, line); + yasm_section_set_default(retval, 0); + elf_secthead_set_typeflags(esd, type, flags); + if (merge_intn) + elf_secthead_set_entsize(esd, yasm_intnum_get_uint(merge_intn)); + yasm_section_set_align(retval, align, line); } else if (flags_override) - yasm_warn_set(YASM_WARN_GENERAL, - N_("section flags ignored on section redeclaration")); + yasm_warn_set(YASM_WARN_GENERAL, + N_("section flags ignored on section redeclaration")); return retval; } static void dir_type(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_objfmt_elf *objfmt_elf = (yasm_objfmt_elf *)object->objfmt; @@ -1125,28 +1125,28 @@ dir_type(yasm_object *object, yasm_valparamhead *valparams, /* Create entry if necessary */ if (!entry) { - entry = elf_symtab_entry_create( - elf_strtab_append_str(objfmt_elf->strtab, symname), sym); - yasm_symrec_add_data(sym, &elf_symrec_data, entry); + entry = elf_symtab_entry_create( + elf_strtab_append_str(objfmt_elf->strtab, symname), sym); + yasm_symrec_add_data(sym, &elf_symrec_data, entry); } /* Pull new type from val */ vp = yasm_vps_next(vp); if (vp && vp->val) { - if (yasm__strcasecmp(vp->val, "function") == 0) - elf_sym_set_type(entry, STT_FUNC); - else if (yasm__strcasecmp(vp->val, "object") == 0) - elf_sym_set_type(entry, STT_OBJECT); - else - yasm_warn_set(YASM_WARN_GENERAL, - N_("unrecognized symbol type `%s'"), vp->val); + if (yasm__strcasecmp(vp->val, "function") == 0) + elf_sym_set_type(entry, STT_FUNC); + else if (yasm__strcasecmp(vp->val, "object") == 0) + elf_sym_set_type(entry, STT_OBJECT); + else + yasm_warn_set(YASM_WARN_GENERAL, + N_("unrecognized symbol type `%s'"), vp->val); } else - yasm_error_set(YASM_ERROR_SYNTAX, N_("no type specified")); + yasm_error_set(YASM_ERROR_SYNTAX, N_("no type specified")); } static void dir_size(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_objfmt_elf *objfmt_elf = (yasm_objfmt_elf *)object->objfmt; @@ -1157,39 +1157,39 @@ dir_size(yasm_object *object, yasm_valparamhead *valparams, /* Create entry if necessary */ if (!entry) { - entry = elf_symtab_entry_create( - elf_strtab_append_str(objfmt_elf->strtab, symname), sym); - yasm_symrec_add_data(sym, &elf_symrec_data, entry); + entry = elf_symtab_entry_create( + elf_strtab_append_str(objfmt_elf->strtab, symname), sym); + yasm_symrec_add_data(sym, &elf_symrec_data, entry); } /* Pull new size from either param (expr) or val */ vp = yasm_vps_next(vp); if (vp && vp->param) { - elf_sym_set_size(entry, vp->param); - vp->param = NULL; + elf_sym_set_size(entry, vp->param); + vp->param = NULL; } else if (vp && vp->val) - elf_sym_set_size(entry, yasm_expr_create_ident(yasm_expr_sym( - yasm_symtab_use(object->symtab, vp->val, line)), line)); + elf_sym_set_size(entry, yasm_expr_create_ident(yasm_expr_sym( + yasm_symtab_use(object->symtab, vp->val, line)), line)); else - yasm_error_set(YASM_ERROR_SYNTAX, N_("no size specified")); + yasm_error_set(YASM_ERROR_SYNTAX, N_("no size specified")); } static void dir_weak(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_objfmt_elf *objfmt_elf = (yasm_objfmt_elf *)object->objfmt; char *symname = vp->val; yasm_symrec *sym = yasm_symtab_declare(object->symtab, symname, - YASM_SYM_GLOBAL, line); + YASM_SYM_GLOBAL, line); elf_objfmt_symtab_append(objfmt_elf, sym, SHN_UNDEF, STB_WEAK, 0, - STV_DEFAULT, NULL, NULL); + STV_DEFAULT, NULL, NULL); } static void dir_ident(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparamhead sect_vps; yasm_datavalhead dvs; @@ -1208,22 +1208,22 @@ dir_ident(yasm_object *object, yasm_valparamhead *valparams, * initial 0 byte in the section. */ if (yasm_section_bcs_first(comment) == yasm_section_bcs_last(comment)) { - yasm_dvs_initialize(&dvs); - yasm_dvs_append(&dvs, yasm_dv_create_expr( - yasm_expr_create_ident( - yasm_expr_int(yasm_intnum_create_uint(0)), line))); - yasm_section_bcs_append(comment, - yasm_bc_create_data(&dvs, 1, 0, object->arch, line)); + yasm_dvs_initialize(&dvs); + yasm_dvs_append(&dvs, yasm_dv_create_expr( + yasm_expr_create_ident( + yasm_expr_int(yasm_intnum_create_uint(0)), line))); + yasm_section_bcs_append(comment, + yasm_bc_create_data(&dvs, 1, 0, object->arch, line)); } yasm_dvs_initialize(&dvs); do { - yasm_dvs_append(&dvs, yasm_dv_create_string(vp->val, strlen(vp->val))); - vp->val = NULL; + yasm_dvs_append(&dvs, yasm_dv_create_string(vp->val, strlen(vp->val))); + vp->val = NULL; } while ((vp = yasm_vps_next(vp))); yasm_section_bcs_append(comment, - yasm_bc_create_data(&dvs, 1, 1, object->arch, line)); + yasm_bc_create_data(&dvs, 1, 1, object->arch, line)); } /* Define valid debug formats to use with this object format */ @@ -1235,14 +1235,14 @@ static const char *elf_objfmt_dbgfmt_keywords[] = { }; static const yasm_directive elf_objfmt_directives[] = { - { ".type", "gas", dir_type, YASM_DIR_ID_REQUIRED }, - { ".size", "gas", dir_size, YASM_DIR_ID_REQUIRED }, - { ".weak", "gas", dir_weak, YASM_DIR_ID_REQUIRED }, - { ".ident", "gas", dir_ident, YASM_DIR_ARG_REQUIRED }, - { "type", "nasm", dir_type, YASM_DIR_ID_REQUIRED }, - { "size", "nasm", dir_size, YASM_DIR_ID_REQUIRED }, - { "weak", "nasm", dir_weak, YASM_DIR_ID_REQUIRED }, - { "ident", "nasm", dir_ident, YASM_DIR_ARG_REQUIRED }, + { ".type", "gas", dir_type, YASM_DIR_ID_REQUIRED }, + { ".size", "gas", dir_size, YASM_DIR_ID_REQUIRED }, + { ".weak", "gas", dir_weak, YASM_DIR_ID_REQUIRED }, + { ".ident", "gas", dir_ident, YASM_DIR_ARG_REQUIRED }, + { "type", "nasm", dir_type, YASM_DIR_ID_REQUIRED }, + { "size", "nasm", dir_size, YASM_DIR_ID_REQUIRED }, + { "weak", "nasm", dir_weak, YASM_DIR_ID_REQUIRED }, + { "ident", "nasm", dir_ident, YASM_DIR_ARG_REQUIRED }, { NULL, NULL, NULL, 0 } }; diff --git a/modules/objfmts/elf/elf-x86-amd64.c b/modules/objfmts/elf/elf-x86-amd64.c index ac504ed8..66ba3af1 100644 --- a/modules/objfmts/elf/elf-x86-amd64.c +++ b/modules/objfmts/elf/elf-x86-amd64.c @@ -45,12 +45,12 @@ static int elf_x86_amd64_accepts_reloc(size_t val, yasm_symrec *wrt, yasm_symrec **ssyms) { if (wrt) { - if ((wrt == ssyms[SSYM_GOTPCREL] && val == 32) - || (wrt == ssyms[SSYM_GOT] && val == 32) - || (wrt == ssyms[SSYM_PLT] && val == 32)) - return 1; - else - return 0; + if ((wrt == ssyms[SSYM_GOTPCREL] && val == 32) + || (wrt == ssyms[SSYM_GOT] && val == 32) + || (wrt == ssyms[SSYM_PLT] && val == 32)) + return 1; + else + return 0; } return (val&(val-1)) ? 0 : ((val & (8|16|32|64)) != 0); } @@ -88,7 +88,7 @@ elf_x86_amd64_write_secthead(unsigned char *bufp, elf_secthead *shead) YASM_WRITE_32_L(bufp, shead->name ? shead->name->index : 0); YASM_WRITE_32_L(bufp, shead->type); YASM_WRITE_64Z_L(bufp, shead->flags); - YASM_WRITE_64Z_L(bufp, 0); /* vmem address */ + YASM_WRITE_64Z_L(bufp, 0); /* vmem address */ YASM_WRITE_64Z_L(bufp, shead->offset); YASM_WRITE_64I_L(bufp, shead->size); @@ -117,14 +117,14 @@ elf_x86_amd64_write_secthead_rel(unsigned char *bufp, nreloc = yasm_intnum_create_uint(shead->nreloc); relocsize = yasm_intnum_create_uint(RELOC64A_SIZE); yasm_intnum_calc(relocsize, YASM_EXPR_MUL, nreloc); - YASM_WRITE_64I_L(bufp, relocsize); /* size */ + YASM_WRITE_64I_L(bufp, relocsize); /* size */ yasm_intnum_destroy(nreloc); yasm_intnum_destroy(relocsize); - YASM_WRITE_32_L(bufp, symtab_idx); /* link: symtab index */ - YASM_WRITE_32_L(bufp, shead->index); /* info: relocated's index */ - YASM_WRITE_64Z_L(bufp, RELOC64_ALIGN); /* align */ - YASM_WRITE_64Z_L(bufp, RELOC64A_SIZE); /* entity size */ + YASM_WRITE_32_L(bufp, symtab_idx); /* link: symtab index */ + YASM_WRITE_32_L(bufp, shead->index); /* info: relocated's index */ + YASM_WRITE_64Z_L(bufp, RELOC64_ALIGN); /* align */ + YASM_WRITE_64Z_L(bufp, RELOC64A_SIZE); /* entity size */ } static void @@ -137,17 +137,17 @@ elf_x86_amd64_handle_reloc_addend(yasm_intnum *intn, elf_reloc_entry *reloc) static unsigned int elf_x86_amd64_map_reloc_info_to_type(elf_reloc_entry *reloc, - yasm_symrec **ssyms) + yasm_symrec **ssyms) { if (reloc->wrt) { - if (reloc->wrt == ssyms[SSYM_GOTPCREL] && reloc->valsize == 32) - return (unsigned char) R_X86_64_GOTPCREL; - else if (reloc->wrt == ssyms[SSYM_GOT] && reloc->valsize == 32) - return (unsigned char) R_X86_64_GOT32; - else if (reloc->wrt == ssyms[SSYM_PLT] && reloc->valsize == 32) - return (unsigned char) R_X86_64_PLT32; - else - yasm_internal_error(N_("Unsupported WRT")); + if (reloc->wrt == ssyms[SSYM_GOTPCREL] && reloc->valsize == 32) + return (unsigned char) R_X86_64_GOTPCREL; + else if (reloc->wrt == ssyms[SSYM_GOT] && reloc->valsize == 32) + return (unsigned char) R_X86_64_GOT32; + else if (reloc->wrt == ssyms[SSYM_PLT] && reloc->valsize == 32) + return (unsigned char) R_X86_64_PLT32; + else + yasm_internal_error(N_("Unsupported WRT")); } else if (reloc->rtype_rel) { switch (reloc->valsize) { case 8: return (unsigned char) R_X86_64_PC8; @@ -175,10 +175,10 @@ elf_x86_amd64_write_reloc(unsigned char *bufp, elf_reloc_entry *reloc, /*YASM_WRITE_64_L(bufp, ELF64_R_INFO(r_sym, r_type));*/ YASM_WRITE_64C_L(bufp, r_sym, r_type); if (reloc->addend) - YASM_WRITE_64I_L(bufp, reloc->addend); + YASM_WRITE_64I_L(bufp, reloc->addend); else { - YASM_WRITE_32_L(bufp, 0); - YASM_WRITE_32_L(bufp, 0); + YASM_WRITE_32_L(bufp, 0); + YASM_WRITE_32_L(bufp, 0); } } @@ -190,26 +190,26 @@ elf_x86_amd64_write_proghead(unsigned char **bufpp, { unsigned char *bufp = *bufpp; unsigned char *buf = bufp-4; - YASM_WRITE_8(bufp, ELFCLASS64); /* elf class */ - YASM_WRITE_8(bufp, ELFDATA2LSB); /* data encoding :: MSB? */ - YASM_WRITE_8(bufp, EV_CURRENT); /* elf version */ - YASM_WRITE_8(bufp, ELFOSABI_SYSV); /* os/abi */ - YASM_WRITE_8(bufp, 0); /* SYSV v3 ABI=0 */ - while (bufp-buf < EI_NIDENT) /* e_ident padding */ + YASM_WRITE_8(bufp, ELFCLASS64); /* elf class */ + YASM_WRITE_8(bufp, ELFDATA2LSB); /* data encoding :: MSB? */ + YASM_WRITE_8(bufp, EV_CURRENT); /* elf version */ + YASM_WRITE_8(bufp, ELFOSABI_SYSV); /* os/abi */ + YASM_WRITE_8(bufp, 0); /* SYSV v3 ABI=0 */ + while (bufp-buf < EI_NIDENT) /* e_ident padding */ YASM_WRITE_8(bufp, 0); - YASM_WRITE_16_L(bufp, ET_REL); /* e_type - object file */ - YASM_WRITE_16_L(bufp, EM_X86_64); /* e_machine - or others */ - YASM_WRITE_32_L(bufp, EV_CURRENT); /* elf version */ - YASM_WRITE_64Z_L(bufp, 0); /* e_entry */ - YASM_WRITE_64Z_L(bufp, 0); /* e_phoff */ + YASM_WRITE_16_L(bufp, ET_REL); /* e_type - object file */ + YASM_WRITE_16_L(bufp, EM_X86_64); /* e_machine - or others */ + YASM_WRITE_32_L(bufp, EV_CURRENT); /* elf version */ + YASM_WRITE_64Z_L(bufp, 0); /* e_entry */ + YASM_WRITE_64Z_L(bufp, 0); /* e_phoff */ YASM_WRITE_64Z_L(bufp, secthead_addr); /* e_shoff secthead off */ - YASM_WRITE_32_L(bufp, 0); /* e_flags */ - YASM_WRITE_16_L(bufp, EHDR64_SIZE); /* e_ehsize */ - YASM_WRITE_16_L(bufp, 0); /* e_phentsize */ - YASM_WRITE_16_L(bufp, 0); /* e_phnum */ - YASM_WRITE_16_L(bufp, SHDR64_SIZE); /* e_shentsize */ + YASM_WRITE_32_L(bufp, 0); /* e_flags */ + YASM_WRITE_16_L(bufp, EHDR64_SIZE); /* e_ehsize */ + YASM_WRITE_16_L(bufp, 0); /* e_phentsize */ + YASM_WRITE_16_L(bufp, 0); /* e_phnum */ + YASM_WRITE_16_L(bufp, SHDR64_SIZE); /* e_shentsize */ YASM_WRITE_16_L(bufp, secthead_count); /* e_shnum */ YASM_WRITE_16_L(bufp, shstrtab_index); /* e_shstrndx */ *bufpp = bufp; diff --git a/modules/objfmts/elf/elf-x86-x86.c b/modules/objfmts/elf/elf-x86-x86.c index 03882385..0286c22e 100644 --- a/modules/objfmts/elf/elf-x86-x86.c +++ b/modules/objfmts/elf/elf-x86-x86.c @@ -46,13 +46,13 @@ static int elf_x86_x86_accepts_reloc(size_t val, yasm_symrec *wrt, yasm_symrec **ssyms) { if (wrt) { - if ((wrt == ssyms[SSYM_GOTPC] && val == 32) - || (wrt == ssyms[SSYM_GOTOFF] && val == 32) - || (wrt == ssyms[SSYM_GOT] && val == 32) - || (wrt == ssyms[SSYM_PLT] && val == 32)) - return 1; - else - return 0; + if ((wrt == ssyms[SSYM_GOTPC] && val == 32) + || (wrt == ssyms[SSYM_GOTOFF] && val == 32) + || (wrt == ssyms[SSYM_GOT] && val == 32) + || (wrt == ssyms[SSYM_PLT] && val == 32)) + return 1; + else + return 0; } return (val&(val-1)) ? 0 : ((val & (8|16|32)) != 0); } @@ -116,11 +116,11 @@ elf_x86_x86_write_secthead_rel(unsigned char *bufp, YASM_WRITE_32_L(bufp, shead->rel_offset); YASM_WRITE_32_L(bufp, RELOC32_SIZE * shead->nreloc);/* size */ - YASM_WRITE_32_L(bufp, symtab_idx); /* link: symtab index */ - YASM_WRITE_32_L(bufp, shead->index); /* info: relocated's index */ + YASM_WRITE_32_L(bufp, symtab_idx); /* link: symtab index */ + YASM_WRITE_32_L(bufp, shead->index); /* info: relocated's index */ - YASM_WRITE_32_L(bufp, RELOC32_ALIGN); /* align */ - YASM_WRITE_32_L(bufp, RELOC32_SIZE); /* entity size */ + YASM_WRITE_32_L(bufp, RELOC32_ALIGN); /* align */ + YASM_WRITE_32_L(bufp, RELOC32_SIZE); /* entity size */ } static void @@ -131,19 +131,19 @@ elf_x86_x86_handle_reloc_addend(yasm_intnum *intn, elf_reloc_entry *reloc) static unsigned int elf_x86_x86_map_reloc_info_to_type(elf_reloc_entry *reloc, - yasm_symrec **ssyms) + yasm_symrec **ssyms) { if (reloc->wrt) { - if (reloc->wrt == ssyms[SSYM_GOTPC] && reloc->valsize == 32) - return (unsigned char) R_386_GOTPC; - else if (reloc->wrt == ssyms[SSYM_GOTOFF] && reloc->valsize == 32) - return (unsigned char) R_386_GOTOFF; - else if (reloc->wrt == ssyms[SSYM_GOT] && reloc->valsize == 32) - return (unsigned char) R_386_GOT32; - else if (reloc->wrt == ssyms[SSYM_PLT] && reloc->valsize == 32) - return (unsigned char) R_386_PLT32; - else - yasm_internal_error(N_("Unsupported WRT")); + if (reloc->wrt == ssyms[SSYM_GOTPC] && reloc->valsize == 32) + return (unsigned char) R_386_GOTPC; + else if (reloc->wrt == ssyms[SSYM_GOTOFF] && reloc->valsize == 32) + return (unsigned char) R_386_GOTOFF; + else if (reloc->wrt == ssyms[SSYM_GOT] && reloc->valsize == 32) + return (unsigned char) R_386_GOT32; + else if (reloc->wrt == ssyms[SSYM_PLT] && reloc->valsize == 32) + return (unsigned char) R_386_PLT32; + else + yasm_internal_error(N_("Unsupported WRT")); } else if (reloc->rtype_rel) { switch (reloc->valsize) { case 8: return (unsigned char) R_386_PC8; @@ -178,23 +178,23 @@ elf_x86_x86_write_proghead(unsigned char **bufpp, { unsigned char *bufp = *bufpp; unsigned char *buf = bufp-4; - YASM_WRITE_8(bufp, ELFCLASS32); /* elf class */ - YASM_WRITE_8(bufp, ELFDATA2LSB); /* data encoding :: MSB? */ - YASM_WRITE_8(bufp, EV_CURRENT); /* elf version */ - while (bufp-buf < EI_NIDENT) /* e_ident padding */ + YASM_WRITE_8(bufp, ELFCLASS32); /* elf class */ + YASM_WRITE_8(bufp, ELFDATA2LSB); /* data encoding :: MSB? */ + YASM_WRITE_8(bufp, EV_CURRENT); /* elf version */ + while (bufp-buf < EI_NIDENT) /* e_ident padding */ YASM_WRITE_8(bufp, 0); - YASM_WRITE_16_L(bufp, ET_REL); /* e_type - object file */ - YASM_WRITE_16_L(bufp, EM_386); /* e_machine - or others */ - YASM_WRITE_32_L(bufp, EV_CURRENT); /* elf version */ - YASM_WRITE_32_L(bufp, 0); /* e_entry exection startaddr */ - YASM_WRITE_32_L(bufp, 0); /* e_phoff program header off */ + YASM_WRITE_16_L(bufp, ET_REL); /* e_type - object file */ + YASM_WRITE_16_L(bufp, EM_386); /* e_machine - or others */ + YASM_WRITE_32_L(bufp, EV_CURRENT); /* elf version */ + YASM_WRITE_32_L(bufp, 0); /* e_entry exection startaddr */ + YASM_WRITE_32_L(bufp, 0); /* e_phoff program header off */ YASM_WRITE_32_L(bufp, secthead_addr); /* e_shoff section header off */ - YASM_WRITE_32_L(bufp, 0); /* e_flags also by arch */ - YASM_WRITE_16_L(bufp, EHDR32_SIZE); /* e_ehsize */ - YASM_WRITE_16_L(bufp, 0); /* e_phentsize */ - YASM_WRITE_16_L(bufp, 0); /* e_phnum */ - YASM_WRITE_16_L(bufp, SHDR32_SIZE); /* e_shentsize */ + YASM_WRITE_32_L(bufp, 0); /* e_flags also by arch */ + YASM_WRITE_16_L(bufp, EHDR32_SIZE); /* e_ehsize */ + YASM_WRITE_16_L(bufp, 0); /* e_phentsize */ + YASM_WRITE_16_L(bufp, 0); /* e_phnum */ + YASM_WRITE_16_L(bufp, SHDR32_SIZE); /* e_shentsize */ YASM_WRITE_16_L(bufp, secthead_count); /* e_shnum */ YASM_WRITE_16_L(bufp, shstrtab_index); /* e_shstrndx */ *bufpp = bufp; diff --git a/modules/objfmts/elf/elf.c b/modules/objfmts/elf/elf.c index 52b69ec8..024ab652 100644 --- a/modules/objfmts/elf/elf.c +++ b/modules/objfmts/elf/elf.c @@ -62,8 +62,8 @@ static const elf_machine_handler *elf_machine_handlers[] = NULL }; static const elf_machine_handler elf_null_machine = {0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0}; + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0}; static elf_machine_handler const *elf_march = &elf_null_machine; static yasm_symrec **elf_ssyms; @@ -79,22 +79,22 @@ elf_set_arch(yasm_arch *arch, yasm_symtab *symtab, int bits_pref) { if (yasm__strcasecmp(yasm_arch_keyword(arch), elf_march->arch)==0) if (yasm__strcasecmp(machine, elf_march->machine)==0) - if (bits_pref == 0 || bits_pref == elf_march->bits) - break; + if (bits_pref == 0 || bits_pref == elf_march->bits) + break; } if (elf_march && elf_march->num_ssyms > 0) { - /* Allocate "special" syms */ - elf_ssyms = - yasm_xmalloc(elf_march->num_ssyms * sizeof(yasm_symrec *)); - for (i=0; (unsigned int)inum_ssyms; i++) - { - /* FIXME: misuse of NULL bytecode */ - elf_ssyms[i] = yasm_symtab_define_label(symtab, - elf_march->ssyms[i].name, - NULL, 1, 0); - } + /* Allocate "special" syms */ + elf_ssyms = + yasm_xmalloc(elf_march->num_ssyms * sizeof(yasm_symrec *)); + for (i=0; (unsigned int)inum_ssyms; i++) + { + /* FIXME: misuse of NULL bytecode */ + elf_ssyms[i] = yasm_symtab_define_label(symtab, + elf_march->ssyms[i].name, + NULL, 1, 0); + } } return elf_march; @@ -106,8 +106,8 @@ elf_is_wrt_sym_relative(yasm_symrec *wrt) { int i; for (i=0; (unsigned int)inum_ssyms; i++) { - if (elf_ssyms[i] == wrt) - return elf_march->ssyms[i].sym_rel; + if (elf_ssyms[i] == wrt) + return elf_march->ssyms[i].sym_rel; } return 0; } @@ -115,15 +115,15 @@ elf_is_wrt_sym_relative(yasm_symrec *wrt) /* takes ownership of addr */ elf_reloc_entry * elf_reloc_entry_create(yasm_symrec *sym, - yasm_symrec *wrt, - yasm_intnum *addr, - int rel, - size_t valsize) + yasm_symrec *wrt, + yasm_intnum *addr, + int rel, + size_t valsize) { elf_reloc_entry *entry; if (!elf_march->accepts_reloc) - yasm_internal_error(N_("Unsupported machine for ELF output")); + yasm_internal_error(N_("Unsupported machine for ELF output")); if (!elf_march->accepts_reloc(valsize, wrt, elf_ssyms)) { @@ -133,7 +133,7 @@ elf_reloc_entry_create(yasm_symrec *sym, } if (sym == NULL) - yasm_internal_error("sym is null"); + yasm_internal_error("sym is null"); entry = yasm_xmalloc(sizeof(elf_reloc_entry)); entry->reloc.sym = sym; @@ -169,16 +169,16 @@ elf_strtab_entry_set_str(elf_strtab_entry *entry, const char *str) { elf_strtab_entry *last; if (entry->str) - yasm_xfree(entry->str); + yasm_xfree(entry->str); entry->str = yasm__xstrdup(str); /* Update all following indices since string length probably changes */ last = entry; entry = STAILQ_NEXT(last, qlink); while (entry) { - entry->index = last->index + (unsigned long)strlen(last->str) + 1; - last = entry; - entry = STAILQ_NEXT(last, qlink); + entry->index = last->index + (unsigned long)strlen(last->str) + 1; + last = entry; + entry = STAILQ_NEXT(last, qlink); } } @@ -202,9 +202,9 @@ elf_strtab_append_str(elf_strtab_head *strtab, const char *str) elf_strtab_entry *last, *entry; if (strtab == NULL) - yasm_internal_error("strtab is null"); + yasm_internal_error("strtab is null"); if (STAILQ_EMPTY(strtab)) - yasm_internal_error("strtab is missing initial dummy entry"); + yasm_internal_error("strtab is missing initial dummy entry"); last = STAILQ_LAST(strtab, elf_strtab_entry, qlink); @@ -221,16 +221,16 @@ elf_strtab_destroy(elf_strtab_head *strtab) elf_strtab_entry *s1, *s2; if (strtab == NULL) - yasm_internal_error("strtab is null"); + yasm_internal_error("strtab is null"); if (STAILQ_EMPTY(strtab)) - yasm_internal_error("strtab is missing initial dummy entry"); + yasm_internal_error("strtab is missing initial dummy entry"); s1 = STAILQ_FIRST(strtab); while (s1 != NULL) { - s2 = STAILQ_NEXT(s1, qlink); - yasm_xfree(s1->str); - yasm_xfree(s1); - s1 = s2; + s2 = STAILQ_NEXT(s1, qlink); + yasm_xfree(s1->str); + yasm_xfree(s1); + s1 = s2; } yasm_xfree(strtab); } @@ -242,13 +242,13 @@ elf_strtab_output_to_file(FILE *f, elf_strtab_head *strtab) elf_strtab_entry *entry; if (strtab == NULL) - yasm_internal_error("strtab is null"); + yasm_internal_error("strtab is null"); /* consider optimizing tables here */ STAILQ_FOREACH(entry, strtab, qlink) { - size_t len = 1 + strlen(entry->str); - fwrite(entry->str, len, 1, f); - size += (unsigned long)len; + size_t len = 1 + strlen(entry->str); + fwrite(entry->str, len, 1, f); + size += (unsigned long)len; } return size; } @@ -258,7 +258,7 @@ elf_strtab_output_to_file(FILE *f, elf_strtab_head *strtab) /* symtab functions */ elf_symtab_entry * elf_symtab_entry_create(elf_strtab_entry *name, - yasm_symrec *sym) + yasm_symrec *sym) { elf_symtab_entry *entry = yasm_xmalloc(sizeof(elf_symtab_entry)); entry->in_table = 0; @@ -281,7 +281,7 @@ static void elf_symtab_entry_destroy(elf_symtab_entry *entry) { if (entry == NULL) - yasm_internal_error("symtab entry is null"); + yasm_internal_error("symtab entry is null"); yasm_xfree(entry); } @@ -301,29 +301,29 @@ elf_symtab_entry_print(void *data, FILE *f, int indent_level) { elf_symtab_entry *entry = data; if (entry == NULL) - yasm_internal_error("symtab entry is null"); + yasm_internal_error("symtab entry is null"); fprintf(f, "%*sbind=", indent_level, ""); switch (entry->bind) { - case STB_LOCAL: fprintf(f, "local\n"); break; - case STB_GLOBAL: fprintf(f, "global\n"); break; - case STB_WEAK: fprintf(f, "weak\n"); break; - default: fprintf(f, "undef\n"); break; + case STB_LOCAL: fprintf(f, "local\n"); break; + case STB_GLOBAL: fprintf(f, "global\n"); break; + case STB_WEAK: fprintf(f, "weak\n"); break; + default: fprintf(f, "undef\n"); break; } fprintf(f, "%*stype=", indent_level, ""); switch (entry->type) { - case STT_NOTYPE: fprintf(f, "notype\n"); break; - case STT_OBJECT: fprintf(f, "object\n"); break; - case STT_FUNC: fprintf(f, "func\n"); break; - case STT_SECTION: fprintf(f, "section\n");break; - case STT_FILE: fprintf(f, "file\n"); break; - default: fprintf(f, "undef\n"); break; + case STT_NOTYPE: fprintf(f, "notype\n"); break; + case STT_OBJECT: fprintf(f, "object\n"); break; + case STT_FUNC: fprintf(f, "func\n"); break; + case STT_SECTION: fprintf(f, "section\n");break; + case STT_FILE: fprintf(f, "file\n"); break; + default: fprintf(f, "undef\n"); break; } fprintf(f, "%*ssize=", indent_level, ""); if (entry->xsize) - yasm_expr_print(entry->xsize, f); + yasm_expr_print(entry->xsize, f); else - fprintf(f, "%ld", entry->size); + fprintf(f, "%ld", entry->size); fprintf(f, "\n"); } @@ -354,11 +354,11 @@ void elf_symtab_append_entry(elf_symtab_head *symtab, elf_symtab_entry *entry) { if (symtab == NULL) - yasm_internal_error("symtab is null"); + yasm_internal_error("symtab is null"); if (entry == NULL) - yasm_internal_error("symtab entry is null"); + yasm_internal_error("symtab entry is null"); if (STAILQ_EMPTY(symtab)) - yasm_internal_error(N_("symtab is missing initial dummy entry")); + yasm_internal_error(N_("symtab is missing initial dummy entry")); STAILQ_INSERT_TAIL(symtab, entry, qlink); entry->in_table = 1; @@ -371,9 +371,9 @@ elf_symtab_insert_local_sym(elf_symtab_head *symtab, elf_symtab_entry *entry) elf_symtab_entry *before = NULL; while (after && (after->bind == STB_LOCAL)) { - before = after; - if (before->type == STT_FILE) break; - after = STAILQ_NEXT(after, qlink); + before = after; + if (before->type == STT_FILE) break; + after = STAILQ_NEXT(after, qlink); } STAILQ_INSERT_AFTER(symtab, before, entry, qlink); entry->in_table = 1; @@ -385,15 +385,15 @@ elf_symtab_destroy(elf_symtab_head *symtab) elf_symtab_entry *s1, *s2; if (symtab == NULL) - yasm_internal_error("symtab is null"); + yasm_internal_error("symtab is null"); if (STAILQ_EMPTY(symtab)) - yasm_internal_error(N_("symtab is missing initial dummy entry")); + yasm_internal_error(N_("symtab is missing initial dummy entry")); s1 = STAILQ_FIRST(symtab); while (s1 != NULL) { - s2 = STAILQ_NEXT(s1, qlink); - elf_symtab_entry_destroy(s1); - s1 = s2; + s2 = STAILQ_NEXT(s1, qlink); + elf_symtab_entry_destroy(s1); + s1 = s2; } yasm_xfree(symtab); } @@ -405,72 +405,72 @@ elf_symtab_assign_indices(elf_symtab_head *symtab) unsigned long last_local=0; if (symtab == NULL) - yasm_internal_error("symtab is null"); + yasm_internal_error("symtab is null"); if (STAILQ_EMPTY(symtab)) - yasm_internal_error(N_("symtab is missing initial dummy entry")); + yasm_internal_error(N_("symtab is missing initial dummy entry")); STAILQ_FOREACH(entry, symtab, qlink) { - if (prev) - entry->symindex = prev->symindex + 1; - if (entry->bind == STB_LOCAL) - last_local = entry->symindex; - prev = entry; + if (prev) + entry->symindex = prev->symindex + 1; + if (entry->bind == STB_LOCAL) + last_local = entry->symindex; + prev = entry; } return last_local + 1; } unsigned long elf_symtab_write_to_file(FILE *f, elf_symtab_head *symtab, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { unsigned char buf[SYMTAB_MAXSIZE], *bufp; elf_symtab_entry *entry, *prev; unsigned long size = 0; if (!symtab) - yasm_internal_error(N_("symtab is null")); + yasm_internal_error(N_("symtab is null")); prev = NULL; STAILQ_FOREACH(entry, symtab, qlink) { - yasm_intnum *size_intn=NULL, *value_intn=NULL; - bufp = buf; - - /* get size (if specified); expr overrides stored integer */ - if (entry->xsize) { - size_intn = yasm_intnum_copy( - yasm_expr_get_intnum(&entry->xsize, 1)); - if (!size_intn) { - yasm_error_set(YASM_ERROR_VALUE, - N_("size specifier not an integer expression")); - yasm_errwarn_propagate(errwarns, entry->xsize->line); - } - } - else - size_intn = yasm_intnum_create_uint(entry->size); - - /* get EQU value for constants */ - if (entry->sym) { - const yasm_expr *equ_expr_c; - equ_expr_c = yasm_symrec_get_equ(entry->sym); - - if (equ_expr_c != NULL) { - const yasm_intnum *equ_intn; - yasm_expr *equ_expr = yasm_expr_copy(equ_expr_c); - equ_intn = yasm_expr_get_intnum(&equ_expr, 1); - - if (equ_intn == NULL) { - yasm_error_set(YASM_ERROR_VALUE, - N_("EQU value not an integer expression")); - yasm_errwarn_propagate(errwarns, equ_expr->line); - } else - value_intn = yasm_intnum_copy(equ_intn); - entry->index = SHN_ABS; - yasm_expr_destroy(equ_expr); - } - } - if (value_intn == NULL) - value_intn = yasm_intnum_create_uint(entry->value); + yasm_intnum *size_intn=NULL, *value_intn=NULL; + bufp = buf; + + /* get size (if specified); expr overrides stored integer */ + if (entry->xsize) { + size_intn = yasm_intnum_copy( + yasm_expr_get_intnum(&entry->xsize, 1)); + if (!size_intn) { + yasm_error_set(YASM_ERROR_VALUE, + N_("size specifier not an integer expression")); + yasm_errwarn_propagate(errwarns, entry->xsize->line); + } + } + else + size_intn = yasm_intnum_create_uint(entry->size); + + /* get EQU value for constants */ + if (entry->sym) { + const yasm_expr *equ_expr_c; + equ_expr_c = yasm_symrec_get_equ(entry->sym); + + if (equ_expr_c != NULL) { + const yasm_intnum *equ_intn; + yasm_expr *equ_expr = yasm_expr_copy(equ_expr_c); + equ_intn = yasm_expr_get_intnum(&equ_expr, 1); + + if (equ_intn == NULL) { + yasm_error_set(YASM_ERROR_VALUE, + N_("EQU value not an integer expression")); + yasm_errwarn_propagate(errwarns, equ_expr->line); + } else + value_intn = yasm_intnum_copy(equ_intn); + entry->index = SHN_ABS; + yasm_expr_destroy(equ_expr); + } + } + if (value_intn == NULL) + value_intn = yasm_intnum_create_uint(entry->value); if (!elf_march->write_symtab_entry || !elf_march->symtab_entry_size) @@ -479,24 +479,24 @@ elf_symtab_write_to_file(FILE *f, elf_symtab_head *symtab, fwrite(buf, elf_march->symtab_entry_size, 1, f); size += elf_march->symtab_entry_size; - yasm_intnum_destroy(size_intn); - yasm_intnum_destroy(value_intn); + yasm_intnum_destroy(size_intn); + yasm_intnum_destroy(value_intn); - prev = entry; + prev = entry; } return size; } void elf_symtab_set_nonzero(elf_symtab_entry *entry, - yasm_section *sect, - elf_section_index sectidx, - elf_symbol_binding bind, - elf_symbol_type type, - yasm_expr *xsize, - elf_address *value) + yasm_section *sect, + elf_section_index sectidx, + elf_symbol_binding bind, + elf_symbol_type type, + yasm_expr *xsize, + elf_address *value) { if (!entry) - yasm_internal_error("NULL entry"); + yasm_internal_error("NULL entry"); if (sect) entry->sect = sect; if (sectidx) entry->index = sectidx; if (bind) entry->bind = bind; @@ -524,7 +524,7 @@ elf_sym_set_size(elf_symtab_entry *entry, struct yasm_expr *size) { if (entry->xsize) - yasm_expr_destroy(entry->xsize); + yasm_expr_destroy(entry->xsize); entry->xsize = size; } @@ -535,11 +535,11 @@ elf_sym_in_table(elf_symtab_entry *entry) } elf_secthead * -elf_secthead_create(elf_strtab_entry *name, - elf_section_type type, - elf_section_flags flags, - elf_address offset, - elf_size size) +elf_secthead_create(elf_strtab_entry *name, + elf_section_type type, + elf_section_flags flags, + elf_address offset, + elf_size size) { elf_secthead *esd = yasm_xmalloc(sizeof(elf_secthead)); @@ -563,7 +563,7 @@ elf_secthead_create(elf_strtab_entry *name, if (name && (strcmp(name->str, ".symtab") == 0)) { if (!elf_march->symtab_entry_size || !elf_march->symtab_entry_align) - yasm_internal_error(N_("unsupported ELF format")); + yasm_internal_error(N_("unsupported ELF format")); esd->entsize = elf_march->symtab_entry_size; esd->align = elf_march->symtab_entry_align; } @@ -575,7 +575,7 @@ void elf_secthead_destroy(elf_secthead *shead) { if (shead == NULL) - yasm_internal_error(N_("shead is null")); + yasm_internal_error(N_("shead is null")); yasm_intnum_destroy(shead->size); @@ -593,22 +593,22 @@ elf_secthead_print(void *data, FILE *f, int indent_level) { elf_secthead *sect = data; fprintf(f, "%*sname=%s\n", indent_level, "", - sect->name ? sect->name->str : ""); + sect->name ? sect->name->str : ""); fprintf(f, "%*ssym=\n", indent_level, ""); yasm_symrec_print(sect->sym, f, indent_level+1); fprintf(f, "%*sindex=0x%x\n", indent_level, "", sect->index); fprintf(f, "%*sflags=", indent_level, ""); if (sect->flags & SHF_WRITE) - fprintf(f, "WRITE "); + fprintf(f, "WRITE "); if (sect->flags & SHF_ALLOC) - fprintf(f, "ALLOC "); + fprintf(f, "ALLOC "); if (sect->flags & SHF_EXECINSTR) - fprintf(f, "EXEC "); + fprintf(f, "EXEC "); /*if (sect->flags & SHF_MASKPROC) - fprintf(f, "PROC-SPECIFIC"); */ + fprintf(f, "PROC-SPECIFIC"); */ fprintf(f, "%*soffset=0x%lx\n", indent_level, "", sect->offset); fprintf(f, "%*ssize=0x%lx\n", indent_level, "", - yasm_intnum_get_uint(sect->size)); + yasm_intnum_get_uint(sect->size)); fprintf(f, "%*slink=0x%x\n", indent_level, "", sect->link); fprintf(f, "%*salign=%lu\n", indent_level, "", sect->align); fprintf(f, "%*snreloc=%ld\n", indent_level, "", sect->nreloc); @@ -616,13 +616,13 @@ elf_secthead_print(void *data, FILE *f, int indent_level) unsigned long elf_secthead_write_to_file(FILE *f, elf_secthead *shead, - elf_section_index sindex) + elf_section_index sindex) { unsigned char buf[SHDR_MAXSIZE], *bufp = buf; shead->index = sindex; if (shead == NULL) - yasm_internal_error("shead is null"); + yasm_internal_error("shead is null"); if (!elf_march->write_secthead || !elf_march->secthead_size) yasm_internal_error(N_("Unsupported machine for ELF output")); @@ -635,14 +635,14 @@ elf_secthead_write_to_file(FILE *f, elf_secthead *shead, void elf_secthead_append_reloc(yasm_section *sect, elf_secthead *shead, - elf_reloc_entry *reloc) + elf_reloc_entry *reloc) { if (sect == NULL) - yasm_internal_error("sect is null"); + yasm_internal_error("sect is null"); if (shead == NULL) - yasm_internal_error("shead is null"); + yasm_internal_error("shead is null"); if (reloc == NULL) - yasm_internal_error("reloc is null"); + yasm_internal_error("reloc is null"); shead->nreloc++; yasm_section_add_reloc(sect, (yasm_reloc *)reloc, elf_reloc_entry_destroy); @@ -676,16 +676,16 @@ elf_handle_reloc_addend(yasm_intnum *intn, elf_reloc_entry *reloc) unsigned long elf_secthead_write_rel_to_file(FILE *f, elf_section_index symtab_idx, - yasm_section *sect, elf_secthead *shead, - elf_section_index sindex) + yasm_section *sect, elf_secthead *shead, + elf_section_index sindex) { unsigned char buf[SHDR_MAXSIZE], *bufp = buf; if (shead == NULL) - yasm_internal_error("shead is null"); + yasm_internal_error("shead is null"); if (!yasm_section_relocs_first(sect)) - return 0; /* no relocations, no .rel.* section header */ + return 0; /* no relocations, no .rel.* section header */ shead->rel_index = sindex; @@ -700,7 +700,7 @@ elf_secthead_write_rel_to_file(FILE *f, elf_section_index symtab_idx, unsigned long elf_secthead_write_relocs_to_file(FILE *f, yasm_section *sect, - elf_secthead *shead, yasm_errwarns *errwarns) + elf_secthead *shead, yasm_errwarns *errwarns) { elf_reloc_entry *reloc; unsigned char buf[RELOC_MAXSIZE], *bufp; @@ -708,52 +708,52 @@ elf_secthead_write_relocs_to_file(FILE *f, yasm_section *sect, long pos; if (shead == NULL) - yasm_internal_error("shead is null"); + yasm_internal_error("shead is null"); reloc = (elf_reloc_entry *)yasm_section_relocs_first(sect); if (!reloc) - return 0; + return 0; /* first align section to multiple of 4 */ pos = ftell(f); if (pos == -1) { - yasm_error_set(YASM_ERROR_IO, - N_("couldn't read position on output stream")); - yasm_errwarn_propagate(errwarns, 0); + yasm_error_set(YASM_ERROR_IO, + N_("couldn't read position on output stream")); + yasm_errwarn_propagate(errwarns, 0); } pos = (pos + 3) & ~3; if (fseek(f, pos, SEEK_SET) < 0) { - yasm_error_set(YASM_ERROR_IO, N_("couldn't seek on output stream")); - yasm_errwarn_propagate(errwarns, 0); + yasm_error_set(YASM_ERROR_IO, N_("couldn't seek on output stream")); + yasm_errwarn_propagate(errwarns, 0); } shead->rel_offset = (unsigned long)pos; while (reloc) { - yasm_sym_vis vis; - unsigned int r_type=0, r_sym; - elf_symtab_entry *esym; + yasm_sym_vis vis; + unsigned int r_type=0, r_sym; + elf_symtab_entry *esym; - esym = yasm_symrec_get_data(reloc->reloc.sym, &elf_symrec_data); - if (esym) - r_sym = esym->symindex; - else - r_sym = STN_UNDEF; + esym = yasm_symrec_get_data(reloc->reloc.sym, &elf_symrec_data); + if (esym) + r_sym = esym->symindex; + else + r_sym = STN_UNDEF; - vis = yasm_symrec_get_visibility(reloc->reloc.sym); + vis = yasm_symrec_get_visibility(reloc->reloc.sym); if (!elf_march->map_reloc_info_to_type) yasm_internal_error(N_("Unsupported arch/machine for elf output")); r_type = elf_march->map_reloc_info_to_type(reloc, elf_ssyms); - bufp = buf; + bufp = buf; if (!elf_march->write_reloc || !elf_march->reloc_entry_size) yasm_internal_error(N_("Unsupported arch/machine for elf output")); elf_march->write_reloc(bufp, reloc, r_type, r_sym); fwrite(buf, elf_march->reloc_entry_size, 1, f); size += elf_march->reloc_entry_size; - reloc = (elf_reloc_entry *) - yasm_section_reloc_next((yasm_reloc *)reloc); + reloc = (elf_reloc_entry *) + yasm_section_reloc_next((yasm_reloc *)reloc); } return size; } @@ -766,7 +766,7 @@ elf_secthead_get_type(elf_secthead *shead) void elf_secthead_set_typeflags(elf_secthead *shead, elf_section_type type, - elf_section_flags flags) + elf_section_flags flags) { shead->type = type; shead->flags = flags; @@ -848,7 +848,7 @@ void elf_secthead_add_size(elf_secthead *shead, yasm_intnum *size) { if (size) { - yasm_intnum_calc(shead->size, YASM_EXPR_ADD, size); + yasm_intnum_calc(shead->size, YASM_EXPR_ADD, size); } } @@ -858,13 +858,13 @@ elf_secthead_set_file_offset(elf_secthead *shead, long pos) unsigned long align = shead->align; if (align == 0 || align == 1) { - shead->offset = (unsigned long)pos; - return pos; + shead->offset = (unsigned long)pos; + return pos; } else if (align & (align - 1)) - yasm_internal_error( - N_("alignment %d for section `%s' is not a power of 2")); - /*, align, sect->name->str);*/ + yasm_internal_error( + N_("alignment %d for section `%s' is not a power of 2")); + /*, align, sect->name->str);*/ shead->offset = (unsigned long)((pos + align - 1) & ~(align - 1)); return (long)shead->offset; @@ -874,25 +874,25 @@ unsigned long elf_proghead_get_size(void) { if (!elf_march->proghead_size) - yasm_internal_error(N_("Unsupported ELF format for output")); + yasm_internal_error(N_("Unsupported ELF format for output")); return elf_march->proghead_size; } unsigned long elf_proghead_write_to_file(FILE *f, - elf_offset secthead_addr, - unsigned long secthead_count, - elf_section_index shstrtab_index) + elf_offset secthead_addr, + unsigned long secthead_count, + elf_section_index shstrtab_index) { unsigned char buf[EHDR_MAXSIZE], *bufp = buf; - YASM_WRITE_8(bufp, ELFMAG0); /* ELF magic number */ + YASM_WRITE_8(bufp, ELFMAG0); /* ELF magic number */ YASM_WRITE_8(bufp, ELFMAG1); YASM_WRITE_8(bufp, ELFMAG2); YASM_WRITE_8(bufp, ELFMAG3); if (!elf_march->write_proghead || !elf_march->proghead_size) - yasm_internal_error(N_("Unsupported ELF format for output")); + yasm_internal_error(N_("Unsupported ELF format for output")); elf_march->write_proghead(&bufp, secthead_addr, secthead_count, shstrtab_index); if (((unsigned)(bufp - buf)) != elf_march->proghead_size) diff --git a/modules/objfmts/elf/elf.h b/modules/objfmts/elf/elf.h index c78c5f9b..a7393c72 100644 --- a/modules/objfmts/elf/elf.h +++ b/modules/objfmts/elf/elf.h @@ -38,44 +38,44 @@ typedef struct elf_symtab_head elf_symtab_head; typedef struct elf_machine_handler elf_machine_handler; -typedef unsigned long elf_address; -typedef unsigned long elf_offset; -typedef unsigned long elf_size; -typedef unsigned long elf_section_info; +typedef unsigned long elf_address; +typedef unsigned long elf_offset; +typedef unsigned long elf_size; +typedef unsigned long elf_section_info; typedef enum { ET_NONE = 0, - ET_REL = 1, /* Relocatable */ - ET_EXEC = 2, /* Executable */ - ET_DYN = 3, /* Shared object */ - ET_CORE = 4, /* Core */ - ET_LOOS = 0xfe00, /* Environment specific */ + ET_REL = 1, /* Relocatable */ + ET_EXEC = 2, /* Executable */ + ET_DYN = 3, /* Shared object */ + ET_CORE = 4, /* Core */ + ET_LOOS = 0xfe00, /* Environment specific */ ET_HIOS = 0xfeff, - ET_LOPROC = 0xff00, /* Processor specific */ + ET_LOPROC = 0xff00, /* Processor specific */ ET_HIPROC = 0xffff } elf_file_type; typedef enum { EM_NONE = 0, - EM_M32 = 1, /* AT&T WE 32100 */ - EM_SPARC = 2, /* SPARC */ - EM_386 = 3, /* Intel 80386 */ - EM_68K = 4, /* Motorola 68000 */ - EM_88K = 5, /* Motorola 88000 */ - EM_860 = 7, /* Intel 80860 */ - EM_MIPS = 8, /* MIPS RS3000 */ - - EM_S370 = 9, /* IBM System/370 */ - EM_MIPS_RS4_BE = 10, /* MIPS R4000 Big-Endian (dep)*/ - EM_PARISC = 15, /* HPPA */ - EM_SPARC32PLUS = 18, /* SPARC v8plus */ - EM_PPC = 20, /* PowerPC 32-bit */ - EM_PPC64 = 21, /* PowerPC 64-bit */ - EM_ARM = 40, /* ARM */ - EM_SPARCV9 = 43, /* SPARC v9 64-bit */ - EM_IA_64 = 50, /* Intel IA-64 */ - EM_X86_64 = 62, /* AMD x86-64 */ - EM_ALPHA = 0x9026 /* Alpha (no ABI) */ + EM_M32 = 1, /* AT&T WE 32100 */ + EM_SPARC = 2, /* SPARC */ + EM_386 = 3, /* Intel 80386 */ + EM_68K = 4, /* Motorola 68000 */ + EM_88K = 5, /* Motorola 88000 */ + EM_860 = 7, /* Intel 80860 */ + EM_MIPS = 8, /* MIPS RS3000 */ + + EM_S370 = 9, /* IBM System/370 */ + EM_MIPS_RS4_BE = 10, /* MIPS R4000 Big-Endian (dep)*/ + EM_PARISC = 15, /* HPPA */ + EM_SPARC32PLUS = 18, /* SPARC v8plus */ + EM_PPC = 20, /* PowerPC 32-bit */ + EM_PPC64 = 21, /* PowerPC 64-bit */ + EM_ARM = 40, /* ARM */ + EM_SPARCV9 = 43, /* SPARC v9 64-bit */ + EM_IA_64 = 50, /* Intel IA-64 */ + EM_X86_64 = 62, /* AMD x86-64 */ + EM_ALPHA = 0x9026 /* Alpha (no ABI) */ } elf_machine; typedef enum { @@ -86,35 +86,35 @@ typedef enum { } elf_magic; typedef enum { - EV_NONE = 0, /* invalid */ - EV_CURRENT = 1 /* current */ + EV_NONE = 0, /* invalid */ + EV_CURRENT = 1 /* current */ } elf_version; typedef enum { - EI_MAG0 = 0, /* File id */ + EI_MAG0 = 0, /* File id */ EI_MAG1 = 1, EI_MAG2 = 2, EI_MAG3 = 3, - EI_CLASS = 4, /* File class */ - EI_DATA = 5, /* Data encoding */ - EI_VERSION = 6, /* File version */ - EI_OSABI = 7, /* OS and ABI */ - EI_ABIVERSION = 8, /* version of ABI */ - - EI_PAD = 9, /* Pad to end; start here */ - EI_NIDENT = 16 /* Sizeof e_ident[] */ + EI_CLASS = 4, /* File class */ + EI_DATA = 5, /* Data encoding */ + EI_VERSION = 6, /* File version */ + EI_OSABI = 7, /* OS and ABI */ + EI_ABIVERSION = 8, /* version of ABI */ + + EI_PAD = 9, /* Pad to end; start here */ + EI_NIDENT = 16 /* Sizeof e_ident[] */ } elf_identification_index; typedef enum { - ELFOSABI_SYSV = 0, /* System V ABI */ - ELFOSABI_HPUX = 1, /* HP-UX os */ - ELFOSABI_STANDALONE = 255 /* Standalone / embedded app */ + ELFOSABI_SYSV = 0, /* System V ABI */ + ELFOSABI_HPUX = 1, /* HP-UX os */ + ELFOSABI_STANDALONE = 255 /* Standalone / embedded app */ } elf_osabi_index; typedef enum { - ELFCLASSNONE = 0, /* invalid */ - ELFCLASS32 = 1, /* 32-bit */ - ELFCLASS64 = 2 /* 64-bit */ + ELFCLASSNONE = 0, /* invalid */ + ELFCLASS32 = 1, /* 32-bit */ + ELFCLASS64 = 2 /* 64-bit */ } elf_class; typedef enum { @@ -125,76 +125,76 @@ typedef enum { /* elf section types - index of semantics */ typedef enum { - SHT_NULL = 0, /* inactive section - no associated data */ - SHT_PROGBITS = 1, /* defined by program for its own meaning */ - SHT_SYMTAB = 2, /* symbol table (primarily) for linking */ - SHT_STRTAB = 3, /* string table - symbols need names */ - SHT_RELA = 4, /* relocation entries w/ explicit addends */ - SHT_HASH = 5, /* symbol hash table - for dynamic linking */ - SHT_DYNAMIC = 6, /* information for dynamic linking */ - SHT_NOTE = 7, /* extra data marking the file somehow */ - SHT_NOBITS = 8, /* no stored data, but occupies runtime space */ - SHT_REL = 9, /* relocations entries w/o explicit addends */ - SHT_SHLIB = 10, /* reserved; unspecified semantics */ - SHT_DYNSYM = 11, /* like symtab, but more for dynamic linking */ - - SHT_LOOS = 0x60000000, /* reserved for environment specific use */ + SHT_NULL = 0, /* inactive section - no associated data */ + SHT_PROGBITS = 1, /* defined by program for its own meaning */ + SHT_SYMTAB = 2, /* symbol table (primarily) for linking */ + SHT_STRTAB = 3, /* string table - symbols need names */ + SHT_RELA = 4, /* relocation entries w/ explicit addends */ + SHT_HASH = 5, /* symbol hash table - for dynamic linking */ + SHT_DYNAMIC = 6, /* information for dynamic linking */ + SHT_NOTE = 7, /* extra data marking the file somehow */ + SHT_NOBITS = 8, /* no stored data, but occupies runtime space */ + SHT_REL = 9, /* relocations entries w/o explicit addends */ + SHT_SHLIB = 10, /* reserved; unspecified semantics */ + SHT_DYNSYM = 11, /* like symtab, but more for dynamic linking */ + + SHT_LOOS = 0x60000000, /* reserved for environment specific use */ SHT_HIOS = 0x6fffffff, - SHT_LOPROC = 0x70000000, /* reserved for processor specific semantics */ + SHT_LOPROC = 0x70000000, /* reserved for processor specific semantics */ SHT_HIPROC = 0x7fffffff/*, - SHT_LOUSER = 0x80000000,*/ /* reserved for applications; safe */ + SHT_LOUSER = 0x80000000,*/ /* reserved for applications; safe */ /*SHT_HIUSER = 0xffffffff*/ } elf_section_type; /* elf section flags - bitfield of attributes */ typedef enum { - SHF_WRITE = 0x1, /* data should be writable at runtime */ - SHF_ALLOC = 0x2, /* occupies memory at runtime */ - SHF_EXECINSTR = 0x4, /* contains machine instructions */ - SHF_MERGE = 0x10, /* data can be merged */ - SHF_STRINGS = 0x20, /* contains 0-terminated strings */ - SHF_GROUP = 0x200, /* member of a section group */ - SHF_TLS = 0x400, /* thread local storage */ + SHF_WRITE = 0x1, /* data should be writable at runtime */ + SHF_ALLOC = 0x2, /* occupies memory at runtime */ + SHF_EXECINSTR = 0x4, /* contains machine instructions */ + SHF_MERGE = 0x10, /* data can be merged */ + SHF_STRINGS = 0x20, /* contains 0-terminated strings */ + SHF_GROUP = 0x200, /* member of a section group */ + SHF_TLS = 0x400, /* thread local storage */ SHF_MASKOS = 0x0f000000/*,*//* environment specific use */ - /*SHF_MASKPROC = 0xf0000000*/ /* bits reserved for processor specific needs */ + /*SHF_MASKPROC = 0xf0000000*/ /* bits reserved for processor specific needs */ } elf_section_flags; /* elf section index - just the special ones */ typedef enum { - SHN_UNDEF = 0, /* undefined symbol; requires other global */ - SHN_LORESERVE = 0xff00, /* reserved for various semantics */ - SHN_LOPROC = 0xff00, /* reserved for processor specific semantics */ + SHN_UNDEF = 0, /* undefined symbol; requires other global */ + SHN_LORESERVE = 0xff00, /* reserved for various semantics */ + SHN_LOPROC = 0xff00, /* reserved for processor specific semantics */ SHN_HIPROC = 0xff1f, - SHN_LOOS = 0xff20, /* reserved for environment specific use */ + SHN_LOOS = 0xff20, /* reserved for environment specific use */ SHN_HIOS = 0xff3f, - SHN_ABS = 0xfff1, /* associated symbols don't change on reloc */ - SHN_COMMON = 0xfff2, /* associated symbols refer to unallocated */ + SHN_ABS = 0xfff1, /* associated symbols don't change on reloc */ + SHN_COMMON = 0xfff2, /* associated symbols refer to unallocated */ SHN_HIRESERVE = 0xffff } elf_section_index; /* elf symbol binding - index of visibility/behavior */ typedef enum { - STB_LOCAL = 0, /* invisible outside defining file */ - STB_GLOBAL = 1, /* visible to all combined object files */ - STB_WEAK = 2, /* global but lower precedence */ + STB_LOCAL = 0, /* invisible outside defining file */ + STB_GLOBAL = 1, /* visible to all combined object files */ + STB_WEAK = 2, /* global but lower precedence */ - STB_LOOS = 10, /* Environment specific use */ + STB_LOOS = 10, /* Environment specific use */ STB_HIOS = 12, - STB_LOPROC = 13, /* reserved for processor specific semantics */ + STB_LOPROC = 13, /* reserved for processor specific semantics */ STB_HIPROC = 15 } elf_symbol_binding; /* elf symbol type - index of classifications */ typedef enum { - STT_NOTYPE = 0, /* type not specified */ - STT_OBJECT = 1, /* data object such as a variable, array, etc */ - STT_FUNC = 2, /* a function or executable code */ - STT_SECTION = 3, /* a section: often for relocation, STB_LOCAL */ - STT_FILE = 4, /* often source filename: STB_LOCAL, SHN_ABS */ + STT_NOTYPE = 0, /* type not specified */ + STT_OBJECT = 1, /* data object such as a variable, array, etc */ + STT_FUNC = 2, /* a function or executable code */ + STT_SECTION = 3, /* a section: often for relocation, STB_LOCAL */ + STT_FILE = 4, /* often source filename: STB_LOCAL, SHN_ABS */ - STT_LOOS = 10, /* Environment specific use */ + STT_LOOS = 10, /* Environment specific use */ STT_HIOS = 12, - STT_LOPROC = 13, /* reserved for processor specific semantics */ + STT_LOPROC = 13, /* reserved for processor specific semantics */ STT_HIPROC = 15 } elf_symbol_type; @@ -204,25 +204,25 @@ typedef enum { /* elf symbol visibility - lower two bits of OTHER field */ typedef enum { - STV_DEFAULT = 0, /* Default symbol visibility rules */ - STV_INTERNAL = 1, /* Processor specific hidden class */ - STV_HIDDEN = 2, /* Sym unavailable in other modules */ - STV_PROTECTED = 3 /* Not preemptable, not exported */ + STV_DEFAULT = 0, /* Default symbol visibility rules */ + STV_INTERNAL = 1, /* Processor specific hidden class */ + STV_HIDDEN = 2, /* Sym unavailable in other modules */ + STV_PROTECTED = 3 /* Not preemptable, not exported */ } elf_symbol_vis; /* internal only object definitions */ #ifdef YASM_OBJFMT_ELF_INTERNAL -#define ELF_VISIBILITY_MASK 0x03 +#define ELF_VISIBILITY_MASK 0x03 #define ELF_ST_VISIBILITY(v) ((v) & ELF_VISIBILITY_MASK) -#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) -#define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t)) +#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) +#define ELF32_R_INFO(s,t) (((s)<<8)+(unsigned char)(t)) #define ELF32_ST_OTHER(vis) ELF_ST_VISIBILITY(vis) -#define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) -#define ELF64_R_INFO(s,t) (((s)<<32) + ((t) & 0xffffffffL)) +#define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) +#define ELF64_R_INFO(s,t) (((s)<<32) + ((t) & 0xffffffffL)) #define ELF64_ST_OTHER(vis) ELF_ST_VISIBILITY(vis) #define EHDR32_SIZE 52 @@ -261,93 +261,93 @@ typedef enum { * S = Value of symbol */ typedef enum { - R_386_NONE = 0, /* none */ - R_386_32 = 1, /* word32, S + A */ - R_386_PC32 = 2, /* word32, S + A - P */ - R_386_GOT32 = 3, /* word32, G + A - P */ - R_386_PLT32 = 4, /* word32, L + A - P */ - R_386_COPY = 5, /* none */ - R_386_GLOB_DAT = 6, /* word32, S */ - R_386_JMP_SLOT = 7, /* word32, S */ - R_386_RELATIVE = 8, /* word32, B + A */ - R_386_GOTOFF = 9, /* word32, S + A - GOT */ - R_386_GOTPC = 10, /* word32, GOT + A - P */ - R_386_TLS_TPOFF = 14, /* Negative offset in static TLS block (GNU - version) */ - R_386_TLS_IE = 15, /* Absolute address of GOT entry for negative - static TLS block offset */ - R_386_TLS_GOTIE = 16, /* GOT entry for negative static TLS block - offset */ - R_386_TLS_LE = 17, /* Negative offset relative to static TLS - (GNU version) */ - R_386_TLS_GD = 18, /* Direct 32 bit for GNU version of GD TLS */ - R_386_TLS_LDM = 19, /* Direct 32 bit for GNU version of LD TLS - in LE code */ - R_386_16 = 20, /* word16, S + A (GNU extension) */ - R_386_PC16 = 21, /* word16, S + A - P (GNU extension) */ - R_386_8 = 22, /* word8, S + A (GNU extension) */ - R_386_PC8 = 23, /* word8, S + A - P (GNU extension) */ - R_386_TLS_GD_32 = 24, /* Direct 32 bit for GD TLS */ - R_386_TLS_GD_PUSH = 25, /* Tag for pushl in GD TLS code */ - R_386_TLS_GD_CALL = 26, /* Relocation for call to */ - R_386_TLS_GD_POP = 27, /* Tag for popl in GD TLS code */ - R_386_TLS_LDM_32 = 28, /* Direct 32 bit for local dynamic code */ - R_386_TLS_LDM_PUSH = 29, /* Tag for pushl in LDM TLS code */ - R_386_TLS_LDM_CALL = 30, /* Relocation for call to */ - R_386_TLS_LDM_POP = 31, /* Tag for popl in LDM TLS code */ - R_386_TLS_LDO_32 = 32, /* Offset relative to TLS block */ - R_386_TLS_IE_32 = 33, /* GOT entry for static TLS block */ - R_386_TLS_LE_32 = 34, /* Offset relative to static TLS block */ - R_386_TLS_DTPMOD32 = 35, /* ID of module containing symbol */ - R_386_TLS_DTPOFF32 = 36, /* Offset in TLS block */ - R_386_TLS_TPOFF32 = 37 /* Offset in static TLS block */ + R_386_NONE = 0, /* none */ + R_386_32 = 1, /* word32, S + A */ + R_386_PC32 = 2, /* word32, S + A - P */ + R_386_GOT32 = 3, /* word32, G + A - P */ + R_386_PLT32 = 4, /* word32, L + A - P */ + R_386_COPY = 5, /* none */ + R_386_GLOB_DAT = 6, /* word32, S */ + R_386_JMP_SLOT = 7, /* word32, S */ + R_386_RELATIVE = 8, /* word32, B + A */ + R_386_GOTOFF = 9, /* word32, S + A - GOT */ + R_386_GOTPC = 10, /* word32, GOT + A - P */ + R_386_TLS_TPOFF = 14, /* Negative offset in static TLS block (GNU + version) */ + R_386_TLS_IE = 15, /* Absolute address of GOT entry for negative + static TLS block offset */ + R_386_TLS_GOTIE = 16, /* GOT entry for negative static TLS block + offset */ + R_386_TLS_LE = 17, /* Negative offset relative to static TLS + (GNU version) */ + R_386_TLS_GD = 18, /* Direct 32 bit for GNU version of GD TLS */ + R_386_TLS_LDM = 19, /* Direct 32 bit for GNU version of LD TLS + in LE code */ + R_386_16 = 20, /* word16, S + A (GNU extension) */ + R_386_PC16 = 21, /* word16, S + A - P (GNU extension) */ + R_386_8 = 22, /* word8, S + A (GNU extension) */ + R_386_PC8 = 23, /* word8, S + A - P (GNU extension) */ + R_386_TLS_GD_32 = 24, /* Direct 32 bit for GD TLS */ + R_386_TLS_GD_PUSH = 25, /* Tag for pushl in GD TLS code */ + R_386_TLS_GD_CALL = 26, /* Relocation for call to */ + R_386_TLS_GD_POP = 27, /* Tag for popl in GD TLS code */ + R_386_TLS_LDM_32 = 28, /* Direct 32 bit for local dynamic code */ + R_386_TLS_LDM_PUSH = 29, /* Tag for pushl in LDM TLS code */ + R_386_TLS_LDM_CALL = 30, /* Relocation for call to */ + R_386_TLS_LDM_POP = 31, /* Tag for popl in LDM TLS code */ + R_386_TLS_LDO_32 = 32, /* Offset relative to TLS block */ + R_386_TLS_IE_32 = 33, /* GOT entry for static TLS block */ + R_386_TLS_LE_32 = 34, /* Offset relative to static TLS block */ + R_386_TLS_DTPMOD32 = 35, /* ID of module containing symbol */ + R_386_TLS_DTPOFF32 = 36, /* Offset in TLS block */ + R_386_TLS_TPOFF32 = 37 /* Offset in static TLS block */ } elf_386_relocation_type; typedef enum { - R_X86_64_NONE = 0, /* none */ - R_X86_64_64 = 1, /* word64, S + A */ - R_X86_64_PC32 = 2, /* word32, S + A - P */ - R_X86_64_GOT32 = 3, /* word32, G + A */ - R_X86_64_PLT32 = 4, /* word32, L + A - P */ - R_X86_64_COPY = 5, /* none */ - R_X86_64_GLOB_DAT = 6, /* word64, S, set GOT entry to data address */ - R_X86_64_JMP_SLOT = 7, /* word64, S, set GOT entry to code address */ - R_X86_64_RELATIVE = 8, /* word64, B + A */ - R_X86_64_GOTPCREL = 9, /* word32, G + GOT + A - P */ - R_X86_64_32 = 10, /* word32 (zero extend), S + A */ - R_X86_64_32S = 11, /* word32 (sign extend), S + A */ - R_X86_64_16 = 12, /* word16, S + A */ - R_X86_64_PC16 = 13, /* word16, S + A - P */ - R_X86_64_8 = 14, /* word8, S + A */ - R_X86_64_PC8 = 15, /* word8, S + A - P */ - R_X86_64_DPTMOD64 = 16, /* word64, ID of module containing symbol */ - R_X86_64_DTPOFF64 = 17, /* word64, offset in TLS block */ - R_X86_64_TPOFF64 = 18, /* word64, offset in initial TLS block */ - R_X86_64_TLSGD = 19, /* word32, PC-rel offset to GD GOT block */ - R_X86_64_TLSLD = 20, /* word32, PC-rel offset to LD GOT block */ - R_X86_64_DTPOFF32 = 21, /* word32, offset to TLS block */ - R_X86_64_GOTTPOFF = 22, /* word32, PC-rel offset to IE GOT entry */ - R_X86_64_TPOFF32 = 23 /* word32, offset in initial TLS block */ + R_X86_64_NONE = 0, /* none */ + R_X86_64_64 = 1, /* word64, S + A */ + R_X86_64_PC32 = 2, /* word32, S + A - P */ + R_X86_64_GOT32 = 3, /* word32, G + A */ + R_X86_64_PLT32 = 4, /* word32, L + A - P */ + R_X86_64_COPY = 5, /* none */ + R_X86_64_GLOB_DAT = 6, /* word64, S, set GOT entry to data address */ + R_X86_64_JMP_SLOT = 7, /* word64, S, set GOT entry to code address */ + R_X86_64_RELATIVE = 8, /* word64, B + A */ + R_X86_64_GOTPCREL = 9, /* word32, G + GOT + A - P */ + R_X86_64_32 = 10, /* word32 (zero extend), S + A */ + R_X86_64_32S = 11, /* word32 (sign extend), S + A */ + R_X86_64_16 = 12, /* word16, S + A */ + R_X86_64_PC16 = 13, /* word16, S + A - P */ + R_X86_64_8 = 14, /* word8, S + A */ + R_X86_64_PC8 = 15, /* word8, S + A - P */ + R_X86_64_DPTMOD64 = 16, /* word64, ID of module containing symbol */ + R_X86_64_DTPOFF64 = 17, /* word64, offset in TLS block */ + R_X86_64_TPOFF64 = 18, /* word64, offset in initial TLS block */ + R_X86_64_TLSGD = 19, /* word32, PC-rel offset to GD GOT block */ + R_X86_64_TLSLD = 20, /* word32, PC-rel offset to LD GOT block */ + R_X86_64_DTPOFF32 = 21, /* word32, offset to TLS block */ + R_X86_64_GOTTPOFF = 22, /* word32, PC-rel offset to IE GOT entry */ + R_X86_64_TPOFF32 = 23 /* word32, offset in initial TLS block */ } elf_x86_64_relocation_type; struct elf_secthead { - elf_section_type type; - elf_section_flags flags; - elf_address offset; - yasm_intnum *size; - elf_section_index link; - elf_section_info info; /* see note ESD1 */ - unsigned long align; - elf_size entsize; - - yasm_symrec *sym; - elf_strtab_entry *name; - elf_section_index index; - - elf_strtab_entry *rel_name; - elf_section_index rel_index; - elf_address rel_offset; - unsigned long nreloc; + elf_section_type type; + elf_section_flags flags; + elf_address offset; + yasm_intnum *size; + elf_section_index link; + elf_section_info info; /* see note ESD1 */ + unsigned long align; + elf_size entsize; + + yasm_symrec *sym; + elf_strtab_entry *name; + elf_section_index index; + + elf_strtab_entry *rel_name; + elf_section_index rel_index; + elf_address rel_offset; + unsigned long nreloc; }; /* Note ESD1: @@ -369,9 +369,9 @@ struct elf_secthead { */ struct elf_reloc_entry { - yasm_reloc reloc; - int rtype_rel; - size_t valsize; + yasm_reloc reloc; + int rtype_rel; + size_t valsize; yasm_intnum *addend; /*@null@*/ yasm_symrec *wrt; }; @@ -379,25 +379,25 @@ struct elf_reloc_entry { STAILQ_HEAD(elf_strtab_head, elf_strtab_entry); struct elf_strtab_entry { STAILQ_ENTRY(elf_strtab_entry) qlink; - unsigned long index; - char *str; + unsigned long index; + char *str; }; STAILQ_HEAD(elf_symtab_head, elf_symtab_entry); struct elf_symtab_entry { STAILQ_ENTRY(elf_symtab_entry) qlink; - int in_table; - yasm_symrec *sym; - yasm_section *sect; - elf_strtab_entry *name; - elf_address value; + int in_table; + yasm_symrec *sym; + yasm_section *sect; + elf_strtab_entry *name; + elf_address value; /*@dependent@*/ yasm_expr *xsize; - elf_size size; - elf_section_index index; - elf_symbol_binding bind; - elf_symbol_type type; + elf_size size; + elf_section_index index; + elf_symbol_binding bind; + elf_symbol_type type; elf_symbol_vis vis; - elf_symbol_index symindex; + elf_symbol_index symindex; }; #endif /* defined(YASM_OBJFMT_ELF_INTERNAL) */ @@ -407,16 +407,16 @@ extern const yasm_assoc_data_callback elf_symrec_data; const elf_machine_handler *elf_set_arch(struct yasm_arch *arch, - yasm_symtab *symtab, - int bits_pref); + yasm_symtab *symtab, + int bits_pref); /* reloc functions */ int elf_is_wrt_sym_relative(yasm_symrec *wrt); elf_reloc_entry *elf_reloc_entry_create(yasm_symrec *sym, - /*@null@*/ yasm_symrec *wrt, - yasm_intnum *addr, - int rel, - size_t valsize); + /*@null@*/ yasm_symrec *wrt, + yasm_intnum *addr, + int rel, + size_t valsize); void elf_reloc_entry_destroy(void *entry); /* strtab functions */ @@ -429,22 +429,22 @@ unsigned long elf_strtab_output_to_file(FILE *f, elf_strtab_head *head); /* symtab functions */ elf_symtab_entry *elf_symtab_entry_create(elf_strtab_entry *name, - struct yasm_symrec *sym); + struct yasm_symrec *sym); elf_symtab_head *elf_symtab_create(void); void elf_symtab_append_entry(elf_symtab_head *symtab, elf_symtab_entry *entry); void elf_symtab_insert_local_sym(elf_symtab_head *symtab, - elf_symtab_entry *entry); + elf_symtab_entry *entry); void elf_symtab_destroy(elf_symtab_head *head); unsigned long elf_symtab_assign_indices(elf_symtab_head *symtab); unsigned long elf_symtab_write_to_file(FILE *f, elf_symtab_head *symtab, - yasm_errwarns *errwarns); -void elf_symtab_set_nonzero(elf_symtab_entry *entry, - struct yasm_section *sect, - elf_section_index sectidx, - elf_symbol_binding bind, - elf_symbol_type type, - struct yasm_expr *size, - elf_address *value); + yasm_errwarns *errwarns); +void elf_symtab_set_nonzero(elf_symtab_entry *entry, + struct yasm_section *sect, + elf_section_index sectidx, + elf_symbol_binding bind, + elf_symbol_type type, + struct yasm_expr *size, + elf_address *value); void elf_sym_set_visibility(elf_symtab_entry *entry, elf_symbol_vis vis); void elf_sym_set_type(elf_symtab_entry *entry, elf_symbol_type type); @@ -452,47 +452,47 @@ void elf_sym_set_size(elf_symtab_entry *entry, struct yasm_expr *size); int elf_sym_in_table(elf_symtab_entry *entry); /* section header functions */ -elf_secthead *elf_secthead_create(elf_strtab_entry *name, - elf_section_type type, - elf_section_flags flags, - elf_address offset, - elf_size size); +elf_secthead *elf_secthead_create(elf_strtab_entry *name, + elf_section_type type, + elf_section_flags flags, + elf_address offset, + elf_size size); void elf_secthead_destroy(elf_secthead *esd); unsigned long elf_secthead_write_to_file(FILE *f, elf_secthead *esd, - elf_section_index sindex); + elf_section_index sindex); void elf_secthead_append_reloc(yasm_section *sect, elf_secthead *shead, - elf_reloc_entry *reloc); + elf_reloc_entry *reloc); elf_section_type elf_secthead_get_type(elf_secthead *shead); void elf_secthead_set_typeflags(elf_secthead *shead, elf_section_type type, - elf_section_flags flags); + elf_section_flags flags); int elf_secthead_is_empty(elf_secthead *shead); struct yasm_symrec *elf_secthead_get_sym(elf_secthead *shead); unsigned long elf_secthead_get_align(const elf_secthead *shead); unsigned long elf_secthead_set_align(elf_secthead *shead, unsigned long align); elf_section_index elf_secthead_get_index(elf_secthead *shead); elf_section_info elf_secthead_set_info(elf_secthead *shead, - elf_section_info info); + elf_section_info info); elf_section_index elf_secthead_set_index(elf_secthead *shead, - elf_section_index sectidx); + elf_section_index sectidx); elf_section_index elf_secthead_set_link(elf_secthead *shead, - elf_section_index link); + elf_section_index link); elf_section_index elf_secthead_set_rel_index(elf_secthead *shead, - elf_section_index sectidx); + elf_section_index sectidx); elf_strtab_entry *elf_secthead_set_rel_name(elf_secthead *shead, - elf_strtab_entry *entry); + elf_strtab_entry *entry); elf_size elf_secthead_set_entsize(elf_secthead *shead, elf_size size); struct yasm_symrec *elf_secthead_set_sym(elf_secthead *shead, - struct yasm_symrec *sym); + struct yasm_symrec *sym); void elf_secthead_add_size(elf_secthead *shead, yasm_intnum *size); char *elf_secthead_name_reloc_section(const char *basesect); void elf_handle_reloc_addend(yasm_intnum *intn, elf_reloc_entry *reloc); unsigned long elf_secthead_write_rel_to_file(FILE *f, elf_section_index symtab, - yasm_section *sect, - elf_secthead *esd, - elf_section_index sindex); + yasm_section *sect, + elf_secthead *esd, + elf_section_index sindex); unsigned long elf_secthead_write_relocs_to_file(FILE *f, yasm_section *sect, - elf_secthead *shead, - yasm_errwarns *errwarns); + elf_secthead *shead, + yasm_errwarns *errwarns); long elf_secthead_set_file_offset(elf_secthead *shead, long pos); /* program header function */ @@ -500,8 +500,8 @@ unsigned long elf_proghead_get_size(void); unsigned long elf_proghead_write_to_file(FILE *f, - elf_offset secthead_addr, - unsigned long secthead_count, - elf_section_index shstrtab_index); + elf_offset secthead_addr, + unsigned long secthead_count, + elf_section_index shstrtab_index); #endif /* ELF_H_INCLUDED */ diff --git a/modules/objfmts/elf/tests/elftest.c b/modules/objfmts/elf/tests/elftest.c index 73a15408..69610718 100644 --- a/modules/objfmts/elf/tests/elftest.c +++ b/modules/objfmts/elf/tests/elftest.c @@ -29,7 +29,7 @@ int main(void) { greet(); printf("These pointers should be equal: %p and %p\n", - &greet, textptr); + &greet, textptr); printf("So should these: %p and %p\n", selfptr, &selfptr); } diff --git a/modules/objfmts/macho/macho-objfmt.c b/modules/objfmts/macho/macho-objfmt.c index b07bbe0b..ad60d89d 100644 --- a/modules/objfmts/macho/macho-objfmt.c +++ b/modules/objfmts/macho/macho-objfmt.c @@ -28,7 +28,7 @@ /* notes: This implementation is rather basic. There are several implementation issues to be sorted out for full compliance and error resilience. - Some examples are given below (nasm syntax). + Some examples are given below (nasm syntax). 1) section placement Mach-O requires BSS sections to be placed last in object files. This @@ -50,8 +50,8 @@ 2.2) data referencing in 64 bit mode While ELF allows 32 bit absolute relocations in 64 bit mode, Mach-O does not. Therefore code like - lea rbx,[_foo] ;48 8d 1c 25 00 00 00 00 - mov rcx,[_bar] ;48 8b 0c 25 00 00 00 00 + lea rbx,[_foo] ;48 8d 1c 25 00 00 00 00 + mov rcx,[_bar] ;48 8b 0c 25 00 00 00 00 with a 32 bit address field cannot be relocated into an address >= 0x100000000 (OSX actually uses that). @@ -62,7 +62,7 @@ not implemented in this object format. The recommended was is PC relative code (called RIP-relative in x86-64). So instead of the lines above, just write: lea rbx,[_foo wrt rip] - mov rcx,[_bar wrt rip] + mov rcx,[_bar wrt rip] 2.3) section/data alignment Normally, you specify sections with a specific alignment @@ -115,108 +115,108 @@ /* MACH-O DEFINES */ /* Mach-O in-file header structure sizes (32 BIT, see below for 64 bit defs) */ -#define MACHO_HEADER_SIZE 28 -#define MACHO_SEGCMD_SIZE 56 -#define MACHO_SECTCMD_SIZE 68 -#define MACHO_SYMCMD_SIZE 24 -#define MACHO_NLIST_SIZE 12 -#define MACHO_RELINFO_SIZE 8 +#define MACHO_HEADER_SIZE 28 +#define MACHO_SEGCMD_SIZE 56 +#define MACHO_SECTCMD_SIZE 68 +#define MACHO_SYMCMD_SIZE 24 +#define MACHO_NLIST_SIZE 12 +#define MACHO_RELINFO_SIZE 8 /* 64 bit sizes */ -#define MACHO_HEADER64_SIZE 32 -#define MACHO_SEGCMD64_SIZE 72 -#define MACHO_SECTCMD64_SIZE 80 -#define MACHO_NLIST64_SIZE 16 -#define MACHO_RELINFO64_SIZE 8 +#define MACHO_HEADER64_SIZE 32 +#define MACHO_SEGCMD64_SIZE 72 +#define MACHO_SECTCMD64_SIZE 80 +#define MACHO_NLIST64_SIZE 16 +#define MACHO_RELINFO64_SIZE 8 /* Mach-O file header values */ -#define MH_MAGIC 0xfeedface -#define MH_MAGIC_64 0xfeedfacf +#define MH_MAGIC 0xfeedface +#define MH_MAGIC_64 0xfeedfacf /* CPU machine type */ -#define CPU_TYPE_I386 7 /* x86 platform */ -#define CPU_TYPE_X86_64 (CPU_TYPE_I386|CPU_ARCH_ABI64) -#define CPU_ARCH_ABI64 0x01000000 /* 64 bit ABI */ +#define CPU_TYPE_I386 7 /* x86 platform */ +#define CPU_TYPE_X86_64 (CPU_TYPE_I386|CPU_ARCH_ABI64) +#define CPU_ARCH_ABI64 0x01000000 /* 64 bit ABI */ /* CPU machine subtype, e.g. processor */ -#define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */ -#define CPU_SUBTYPE_X86_64_ALL CPU_SUBTYPE_I386_ALL -#define CPU_SUBTYPE_386 3 -#define CPU_SUBTYPE_486 4 -#define CPU_SUBTYPE_486SX (4 + 128) -#define CPU_SUBTYPE_586 5 -#define CPU_SUBTYPE_INTEL(f, m) ((f) + ((m) << 4)) -#define CPU_SUBTYPE_PENT CPU_SUBTYPE_INTEL(5, 0) -#define CPU_SUBTYPE_PENTPRO CPU_SUBTYPE_INTEL(6, 1) -#define CPU_SUBTYPE_PENTII_M3 CPU_SUBTYPE_INTEL(6, 3) -#define CPU_SUBTYPE_PENTII_M5 CPU_SUBTYPE_INTEL(6, 5) -#define CPU_SUBTYPE_PENTIUM_4 CPU_SUBTYPE_INTEL(10, 0) +#define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */ +#define CPU_SUBTYPE_X86_64_ALL CPU_SUBTYPE_I386_ALL +#define CPU_SUBTYPE_386 3 +#define CPU_SUBTYPE_486 4 +#define CPU_SUBTYPE_486SX (4 + 128) +#define CPU_SUBTYPE_586 5 +#define CPU_SUBTYPE_INTEL(f, m) ((f) + ((m) << 4)) +#define CPU_SUBTYPE_PENT CPU_SUBTYPE_INTEL(5, 0) +#define CPU_SUBTYPE_PENTPRO CPU_SUBTYPE_INTEL(6, 1) +#define CPU_SUBTYPE_PENTII_M3 CPU_SUBTYPE_INTEL(6, 3) +#define CPU_SUBTYPE_PENTII_M5 CPU_SUBTYPE_INTEL(6, 5) +#define CPU_SUBTYPE_PENTIUM_4 CPU_SUBTYPE_INTEL(10, 0) -#define CPU_SUBTYPE_INTEL_FAMILY(x) ((x) & 15) -#define CPU_SUBTYPE_INTEL_FAMILY_MAX 15 +#define CPU_SUBTYPE_INTEL_FAMILY(x) ((x) & 15) +#define CPU_SUBTYPE_INTEL_FAMILY_MAX 15 -#define CPU_SUBTYPE_INTEL_MODEL(x) ((x) >> 4) -#define CPU_SUBTYPE_INTEL_MODEL_ALL 0 +#define CPU_SUBTYPE_INTEL_MODEL(x) ((x) >> 4) +#define CPU_SUBTYPE_INTEL_MODEL_ALL 0 -#define MH_OBJECT 0x1 /* object file */ +#define MH_OBJECT 0x1 /* object file */ -#define LC_SEGMENT 0x1 /* segment load command */ -#define LC_SYMTAB 0x2 /* symbol table load command */ -#define LC_SEGMENT_64 0x19 /* segment load command */ +#define LC_SEGMENT 0x1 /* segment load command */ +#define LC_SYMTAB 0x2 /* symbol table load command */ +#define LC_SEGMENT_64 0x19 /* segment load command */ -#define VM_PROT_NONE 0x00 -#define VM_PROT_READ 0x01 -#define VM_PROT_WRITE 0x02 -#define VM_PROT_EXECUTE 0x04 +#define VM_PROT_NONE 0x00 +#define VM_PROT_READ 0x01 +#define VM_PROT_WRITE 0x02 +#define VM_PROT_EXECUTE 0x04 -#define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) -#define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) +#define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) +#define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) -#define SECTION_TYPE 0x000000ff /* section type mask */ +#define SECTION_TYPE 0x000000ff /* section type mask */ #define SECTION_ATTRIBUTES 0xffffff00UL/* section attributes mask */ -#define S_REGULAR 0x0 /* standard section */ -#define S_ZEROFILL 0x1 /* zerofill, in-memory only */ -#define S_CSTRING_LITERALS 0x2 /* literal C strings */ -#define S_4BYTE_LITERALS 0x3 /* only 4-byte literals */ -#define S_8BYTE_LITERALS 0x4 /* only 8-byte literals */ -#define S_LITERAL_POINTERS 0x5 /* only pointers to literals */ -#define S_NON_LAZY_SYMBOL_POINTERS 0x6 /* only non-lazy symbol pointers */ -#define S_LAZY_SYMBOL_POINTERS 0x7 /* only lazy symbol pointers */ -#define S_SYMBOL_STUBS 0x8 /* only symbol stubs; byte size of - * stub in the reserved2 field */ -#define S_MOD_INIT_FUNC_POINTERS 0x9 /* only function pointers for init */ -#define S_MOD_TERM_FUNC_POINTERS 0xa /* only function pointers for term */ -#define S_COALESCED 0xb /* symbols that are to be coalesced */ -#define S_GB_ZEROFILL 0xc /* >4GB zero fill on demand section */ -#define S_INTERPOSING 0xd /* only pairs of function pointers for - * interposing */ -#define S_16BYTE_LITERALS 0xe /* only 16 byte literals */ - -#define S_ATTR_DEBUG 0x02000000 /* a debug section */ -#define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */ -#define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some - * machine instructions */ -#define S_ATTR_EXT_RELOC 0x00000200 /* section has external - * relocation entries */ -#define S_ATTR_LOC_RELOC 0x00000100 /* section has local - * relocation entries */ - -#define SECTION_ATTRIBUTES_USR 0xff000000UL /* User setable attributes */ -#define S_ATTR_PURE_INSTRUCTIONS 0x80000000UL /* only true machine insns */ -#define S_ATTR_NO_TOC 0x40000000UL /* coalesced symbols that are - * not to be in a ranlib table - * of contents */ -#define S_ATTR_STRIP_STATIC_SYMS 0x20000000UL /* ok to strip static symbols - * in this section in files - * with the MH_DYLDLINK flag */ -#define S_ATTR_NO_DEAD_STRIP 0x10000000UL /* no dead stripping */ -#define S_ATTR_LIVE_SUPPORT 0x08000000UL /* blocks are live if they - * reference live blocks */ -#define S_ATTR_SELF_MODIFYING_CODE 0x04000000UL /* Used with i386 code stubs - * written on by dyld */ +#define S_REGULAR 0x0 /* standard section */ +#define S_ZEROFILL 0x1 /* zerofill, in-memory only */ +#define S_CSTRING_LITERALS 0x2 /* literal C strings */ +#define S_4BYTE_LITERALS 0x3 /* only 4-byte literals */ +#define S_8BYTE_LITERALS 0x4 /* only 8-byte literals */ +#define S_LITERAL_POINTERS 0x5 /* only pointers to literals */ +#define S_NON_LAZY_SYMBOL_POINTERS 0x6 /* only non-lazy symbol pointers */ +#define S_LAZY_SYMBOL_POINTERS 0x7 /* only lazy symbol pointers */ +#define S_SYMBOL_STUBS 0x8 /* only symbol stubs; byte size of + * stub in the reserved2 field */ +#define S_MOD_INIT_FUNC_POINTERS 0x9 /* only function pointers for init */ +#define S_MOD_TERM_FUNC_POINTERS 0xa /* only function pointers for term */ +#define S_COALESCED 0xb /* symbols that are to be coalesced */ +#define S_GB_ZEROFILL 0xc /* >4GB zero fill on demand section */ +#define S_INTERPOSING 0xd /* only pairs of function pointers for + * interposing */ +#define S_16BYTE_LITERALS 0xe /* only 16 byte literals */ + +#define S_ATTR_DEBUG 0x02000000 /* a debug section */ +#define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */ +#define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some + * machine instructions */ +#define S_ATTR_EXT_RELOC 0x00000200 /* section has external + * relocation entries */ +#define S_ATTR_LOC_RELOC 0x00000100 /* section has local + * relocation entries */ + +#define SECTION_ATTRIBUTES_USR 0xff000000UL /* User setable attributes */ +#define S_ATTR_PURE_INSTRUCTIONS 0x80000000UL /* only true machine insns */ +#define S_ATTR_NO_TOC 0x40000000UL /* coalesced symbols that are + * not to be in a ranlib table + * of contents */ +#define S_ATTR_STRIP_STATIC_SYMS 0x20000000UL /* ok to strip static symbols + * in this section in files + * with the MH_DYLDLINK flag */ +#define S_ATTR_NO_DEAD_STRIP 0x10000000UL /* no dead stripping */ +#define S_ATTR_LIVE_SUPPORT 0x08000000UL /* blocks are live if they + * reference live blocks */ +#define S_ATTR_SELF_MODIFYING_CODE 0x04000000UL /* Used with i386 code stubs + * written on by dyld */ /* macho references symbols in different ways whether they are linked at * runtime (LAZY, read library functions) or at link time (NON_LAZY, mostly @@ -229,27 +229,27 @@ #define REFERENCE_FLAG_UNDEFINED_LAZY 0x1 #define align(x, y) \ - (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */ + (((x) + (y) - 1) & ~((y) - 1)) /* align x to multiple of y */ #define align32(x) \ - align(x, 4) /* align x to 32 bit boundary */ + align(x, 4) /* align x to 32 bit boundary */ -#define macho_MAGIC 0x87654322 +#define macho_MAGIC 0x87654322 /* Symbol table type field bit masks */ -#define N_STAB 0xe0 /* mask indicating stab entry */ -#define N_PEXT 0x10 /* private external bit */ -#define N_TYPE 0x0e /* mask for all the type bits */ -#define N_EXT 0x01 /* external (global) bit */ +#define N_STAB 0xe0 /* mask indicating stab entry */ +#define N_PEXT 0x10 /* private external bit */ +#define N_TYPE 0x0e /* mask for all the type bits */ +#define N_EXT 0x01 /* external (global) bit */ /* Symbol table type field values */ -#define N_UNDF 0x00 /* undefined */ -#define N_ABS 0x02 /* absolute address */ -#define N_SECT 0x0e /* symbol is defined in a section */ +#define N_UNDF 0x00 /* undefined */ +#define N_ABS 0x02 /* absolute address */ +#define N_SECT 0x0e /* symbol is defined in a section */ -#define NO_SECT 0 /* no section for symbol in nlist */ +#define NO_SECT 0 /* no section for symbol in nlist */ -#define REGULAR_OUTBUF_SIZE 1024 +#define REGULAR_OUTBUF_SIZE 1024 typedef struct macho_reloc { @@ -258,53 +258,53 @@ typedef struct macho_reloc { int length; int ext; enum reloc_type_x86_64 { - /* x86 relocations */ - GENERIC_RELOC_VANILLA = 0, /* generic relocation */ - GENERIC_RELOC_PAIR = 1, /* Only follows a GENERIC_RELOC_SECTDIFF */ - GENERIC_RELOC_SECTDIFF = 2, - GENERIC_RELOC_PB_LA_PTR = 3, /* prebound lazy pointer */ - GENERIC_RELOC_LOCAL_SECTDIFF = 4, - - /* x86-64 relocations */ - X86_64_RELOC_UNSIGNED = 0, /* for absolute addresses */ - X86_64_RELOC_SIGNED = 1, /* for signed 32-bit displacement */ - X86_64_RELOC_BRANCH = 2, /* a CALL/JMP insn with 32-bit disp */ - X86_64_RELOC_GOT_LOAD = 3, /* a MOVQ load of a GOT entry */ - X86_64_RELOC_GOT = 4, /* other GOT references */ - X86_64_RELOC_SUBTRACTOR = 5, /* must be followed by a X86_64_RELOC_UNSIGNED */ - X86_64_RELOC_SIGNED_1 = 6, /* signed 32-bit disp, -1 addend */ - X86_64_RELOC_SIGNED_2 = 7, /* signed 32-bit disp, -2 addend */ - X86_64_RELOC_SIGNED_4 = 8 /* signed 32-bit disp, -4 addend */ + /* x86 relocations */ + GENERIC_RELOC_VANILLA = 0, /* generic relocation */ + GENERIC_RELOC_PAIR = 1, /* Only follows a GENERIC_RELOC_SECTDIFF */ + GENERIC_RELOC_SECTDIFF = 2, + GENERIC_RELOC_PB_LA_PTR = 3, /* prebound lazy pointer */ + GENERIC_RELOC_LOCAL_SECTDIFF = 4, + + /* x86-64 relocations */ + X86_64_RELOC_UNSIGNED = 0, /* for absolute addresses */ + X86_64_RELOC_SIGNED = 1, /* for signed 32-bit displacement */ + X86_64_RELOC_BRANCH = 2, /* a CALL/JMP insn with 32-bit disp */ + X86_64_RELOC_GOT_LOAD = 3, /* a MOVQ load of a GOT entry */ + X86_64_RELOC_GOT = 4, /* other GOT references */ + X86_64_RELOC_SUBTRACTOR = 5, /* must be followed by a X86_64_RELOC_UNSIGNED */ + X86_64_RELOC_SIGNED_1 = 6, /* signed 32-bit disp, -1 addend */ + X86_64_RELOC_SIGNED_2 = 7, /* signed 32-bit disp, -2 addend */ + X86_64_RELOC_SIGNED_4 = 8 /* signed 32-bit disp, -4 addend */ } type; } macho_reloc; typedef struct macho_section_data { /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ - long scnum; /* section number (0=first section) */ - const char *segname; /* segment name in file */ - const char *sectname; /* section name in file */ - unsigned long flags; /* S_* flags */ - unsigned long size; /* size of raw data (section data) in bytes */ - unsigned long offset; /* offset in raw data within file in bytes */ - unsigned long vmoff; /* memory offset */ - unsigned long nreloc; /* number of relocation entries */ - unsigned int extreloc; /* external relocations present (0/1) */ + long scnum; /* section number (0=first section) */ + const char *segname; /* segment name in file */ + const char *sectname; /* section name in file */ + unsigned long flags; /* S_* flags */ + unsigned long size; /* size of raw data (section data) in bytes */ + unsigned long offset; /* offset in raw data within file in bytes */ + unsigned long vmoff; /* memory offset */ + unsigned long nreloc; /* number of relocation entries */ + unsigned int extreloc; /* external relocations present (0/1) */ } macho_section_data; typedef struct macho_symrec_data { - unsigned long index; /* index in output order */ - yasm_intnum *value; /* valid after writing symtable to file */ - unsigned long length; /* length + 1 (plus auto underscore) */ - int add_uscore; /* add underscore (0/1) */ + unsigned long index; /* index in output order */ + yasm_intnum *value; /* valid after writing symtable to file */ + unsigned long length; /* length + 1 (plus auto underscore) */ + int add_uscore; /* add underscore (0/1) */ } macho_symrec_data; typedef struct yasm_objfmt_macho { - yasm_objfmt_base objfmt; /* base structure */ + yasm_objfmt_base objfmt; /* base structure */ - long parse_scnum; /* sect numbering in parser */ - int bits; /* 32 / 64 */ + long parse_scnum; /* sect numbering in parser */ + int bits; /* 32 / 64 */ } yasm_objfmt_macho; @@ -317,21 +317,21 @@ typedef struct macho_objfmt_output_info { yasm_section *sect; /*@dependent@ */ macho_section_data *msd; - unsigned int is_64; /* write object in 64 bit mode */ + unsigned int is_64; /* write object in 64 bit mode */ /* vmsize and filesize available after traversing section count routine */ - unsigned long vmsize; /* raw size of all sections (including BSS) */ - unsigned long filesize; /* size of sections in file (excluding BSS) */ - unsigned long offset; /* offset within file */ + unsigned long vmsize; /* raw size of all sections (including BSS) */ + unsigned long filesize; /* size of sections in file (excluding BSS) */ + unsigned long offset; /* offset within file */ /* forward offset tracking */ - unsigned long rel_base; /* first relocation in file */ - unsigned long s_reloff; /* in-file offset to relocations */ + unsigned long rel_base; /* first relocation in file */ + unsigned long s_reloff; /* in-file offset to relocations */ - unsigned long indx; /* current symbol size in bytes (name length+1) */ - unsigned long symindex; /* current symbol index in output order */ - int all_syms; /* outputting all symbols? */ - unsigned long strlength; /* length of all strings */ + unsigned long indx; /* current symbol size in bytes (name length+1) */ + unsigned long symindex; /* current symbol index in output order */ + int all_syms; /* outputting all symbols? */ + unsigned long strlength; /* length of all strings */ } macho_objfmt_output_info; @@ -357,7 +357,7 @@ yasm_objfmt_module yasm_macho64_LTX_objfmt; static yasm_objfmt * macho_objfmt_create_common(yasm_object *object, yasm_objfmt_module *module, - int bits_pref) + int bits_pref) { yasm_objfmt_macho *objfmt_macho = yasm_xmalloc(sizeof(yasm_objfmt_macho)); @@ -365,24 +365,24 @@ macho_objfmt_create_common(yasm_object *object, yasm_objfmt_module *module, /* Only support x86 arch for now */ if (yasm__strcasecmp(yasm_arch_keyword(object->arch), "x86") != 0) { - yasm_xfree(objfmt_macho); - return NULL; + yasm_xfree(objfmt_macho); + return NULL; } /* Support x86 and amd64 machines of x86 arch */ if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), "x86") == 0 && - (bits_pref == 0 || bits_pref == 32)) - objfmt_macho->bits = 32; + (bits_pref == 0 || bits_pref == 32)) + objfmt_macho->bits = 32; else if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), - "amd64") == 0 && - (bits_pref == 0 || bits_pref == 64)) - objfmt_macho->bits = 64; + "amd64") == 0 && + (bits_pref == 0 || bits_pref == 64)) + objfmt_macho->bits = 64; else { - yasm_xfree(objfmt_macho); - return NULL; + yasm_xfree(objfmt_macho); + return NULL; } - objfmt_macho->parse_scnum = 0; /* section numbering starts at 0 */ + objfmt_macho->parse_scnum = 0; /* section numbering starts at 0 */ return (yasm_objfmt *)objfmt_macho; } @@ -394,12 +394,12 @@ macho_objfmt_create(yasm_object *object) objfmt = macho_objfmt_create_common(object, &yasm_macho_LTX_objfmt, 0); if (objfmt) { - objfmt_macho = (yasm_objfmt_macho *)objfmt; - /* Figure out which bitness of object format to use */ - if (objfmt_macho->bits == 32) - objfmt_macho->objfmt.module = &yasm_macho32_LTX_objfmt; - else if (objfmt_macho->bits == 64) - objfmt_macho->objfmt.module = &yasm_macho64_LTX_objfmt; + objfmt_macho = (yasm_objfmt_macho *)objfmt; + /* Figure out which bitness of object format to use */ + if (objfmt_macho->bits == 32) + objfmt_macho->objfmt.module = &yasm_macho32_LTX_objfmt; + else if (objfmt_macho->bits == 64) + objfmt_macho->objfmt.module = &yasm_macho64_LTX_objfmt; } return objfmt; } @@ -418,8 +418,8 @@ macho64_objfmt_create(yasm_object *object) static int macho_objfmt_output_value(yasm_value *value, unsigned char *buf, - unsigned int destsize, unsigned long offset, - yasm_bytecode *bc, int warn, /*@null@*/ void *d) + unsigned int destsize, unsigned long offset, + yasm_bytecode *bc, int warn, /*@null@*/ void *d) { /*@null@*/ macho_objfmt_output_info *info = (macho_objfmt_output_info *)d; yasm_objfmt_macho *objfmt_macho; @@ -433,149 +433,149 @@ macho_objfmt_output_value(yasm_value *value, unsigned char *buf, objfmt_macho = info->objfmt_macho; if (value->abs) - value->abs = yasm_expr_simplify(value->abs, 1); + value->abs = yasm_expr_simplify(value->abs, 1); /* Try to output constant and PC-relative section-local first. * Note this does NOT output any value with a SEG, WRT, external, * cross-section, or non-PC-relative reference (those are handled below). */ switch (yasm_value_output_basic(value, buf, destsize, bc, warn, - info->object->arch)) { - case -1: - return 1; - case 0: - break; - default: - return 0; + info->object->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; } if (value->section_rel) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("macho: relocation too complex for current implementation")); - return 1; + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: relocation too complex for current implementation")); + return 1; } if (value->rel) { - yasm_sym_vis vis = yasm_symrec_get_visibility(value->rel); - - reloc = yasm_xcalloc(sizeof(macho_reloc), 1); - reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset); - reloc->reloc.sym = value->rel; - switch (valsize) { - case 64: - reloc->length = 3; - break; - case 32: - reloc->length = 2; - break; - case 16: - reloc->length = 1; - break; - case 8: - reloc->length = 0; - break; - default: - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("macho: relocation size unsupported")); - yasm_xfree(reloc); - return 1; - } - reloc->pcrel = 0; - reloc->ext = 0; - reloc->type = GENERIC_RELOC_VANILLA; - /* R_ABS */ - - if (value->rshift > 0) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("macho: shifted relocations not supported")); - yasm_xfree(reloc); - return 1; - } - - if (value->seg_of) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("macho: SEG not supported")); - yasm_xfree(reloc); - return 1; - } - - if (value->wrt) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("macho: WRT not supported")); - yasm_xfree(reloc); - return 1; - } - - if (value->curpos_rel) { - reloc->pcrel = 1; - if (!info->is_64) { - /* Adjust to start of section, so subtract out the bytecode - * offset. - */ - intn_minus = bc->offset; - } else { - /* Add in the offset plus value size to end up with 0. */ - intn_plus = offset+destsize; - if (value->jump_target) - reloc->type = X86_64_RELOC_BRANCH; - else - reloc->type = X86_64_RELOC_SIGNED; - } - } else if (info->is_64) { - if (valsize == 32) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider \"[_symbol wrt rip]\" for mem access, \"qword\" and \"dq _foo\" for pointers.")); - return 1; - } - reloc->type = X86_64_RELOC_UNSIGNED; - } - - /* It seems that x86-64 objects need to have all extern relocs? */ - if (info->is_64) - reloc->ext = 1; - - if ((vis & YASM_SYM_EXTERN) || (vis & YASM_SYM_COMMON)) { - reloc->ext = 1; - info->msd->extreloc = 1; /* section has external relocations */ - } else if (!value->curpos_rel && !info->is_64) { - /*@dependent@*/ /*@null@*/ yasm_bytecode *sym_precbc; - - /* Local symbols need valued to their actual address */ - if (yasm_symrec_get_label(value->rel, &sym_precbc)) { - yasm_section *sym_sect = yasm_bc_get_section(sym_precbc); - /*@null@*/ macho_section_data *msd; - msd = yasm_section_get_data(sym_sect, &macho_section_data_cb); - assert(msd != NULL); - intn_plus += msd->vmoff + yasm_bc_next_offset(sym_precbc); - } - } - - info->msd->nreloc++; - /*printf("reloc %s type %d ",yasm_symrec_get_name(reloc->reloc.sym),reloc->type);*/ - yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); + yasm_sym_vis vis = yasm_symrec_get_visibility(value->rel); + + reloc = yasm_xcalloc(sizeof(macho_reloc), 1); + reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset); + reloc->reloc.sym = value->rel; + switch (valsize) { + case 64: + reloc->length = 3; + break; + case 32: + reloc->length = 2; + break; + case 16: + reloc->length = 1; + break; + case 8: + reloc->length = 0; + break; + default: + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: relocation size unsupported")); + yasm_xfree(reloc); + return 1; + } + reloc->pcrel = 0; + reloc->ext = 0; + reloc->type = GENERIC_RELOC_VANILLA; + /* R_ABS */ + + if (value->rshift > 0) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: shifted relocations not supported")); + yasm_xfree(reloc); + return 1; + } + + if (value->seg_of) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: SEG not supported")); + yasm_xfree(reloc); + return 1; + } + + if (value->wrt) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: WRT not supported")); + yasm_xfree(reloc); + return 1; + } + + if (value->curpos_rel) { + reloc->pcrel = 1; + if (!info->is_64) { + /* Adjust to start of section, so subtract out the bytecode + * offset. + */ + intn_minus = bc->offset; + } else { + /* Add in the offset plus value size to end up with 0. */ + intn_plus = offset+destsize; + if (value->jump_target) + reloc->type = X86_64_RELOC_BRANCH; + else + reloc->type = X86_64_RELOC_SIGNED; + } + } else if (info->is_64) { + if (valsize == 32) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider \"[_symbol wrt rip]\" for mem access, \"qword\" and \"dq _foo\" for pointers.")); + return 1; + } + reloc->type = X86_64_RELOC_UNSIGNED; + } + + /* It seems that x86-64 objects need to have all extern relocs? */ + if (info->is_64) + reloc->ext = 1; + + if ((vis & YASM_SYM_EXTERN) || (vis & YASM_SYM_COMMON)) { + reloc->ext = 1; + info->msd->extreloc = 1; /* section has external relocations */ + } else if (!value->curpos_rel && !info->is_64) { + /*@dependent@*/ /*@null@*/ yasm_bytecode *sym_precbc; + + /* Local symbols need valued to their actual address */ + if (yasm_symrec_get_label(value->rel, &sym_precbc)) { + yasm_section *sym_sect = yasm_bc_get_section(sym_precbc); + /*@null@*/ macho_section_data *msd; + msd = yasm_section_get_data(sym_sect, &macho_section_data_cb); + assert(msd != NULL); + intn_plus += msd->vmoff + yasm_bc_next_offset(sym_precbc); + } + } + + info->msd->nreloc++; + /*printf("reloc %s type %d ",yasm_symrec_get_name(reloc->reloc.sym),reloc->type);*/ + yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); } if (intn_minus <= intn_plus) - intn = yasm_intnum_create_uint(intn_plus-intn_minus); + intn = yasm_intnum_create_uint(intn_plus-intn_minus); else { - intn = yasm_intnum_create_uint(intn_minus-intn_plus); - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + intn = yasm_intnum_create_uint(intn_minus-intn_plus); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); } if (value->abs) { - yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); - - if (!intn2) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("macho: relocation too complex")); - yasm_intnum_destroy(intn); - return 1; - } - yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); + yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); + + if (!intn2) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("macho: relocation too complex")); + yasm_intnum_destroy(intn); + return 1; + } + yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); } retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize, - valsize, 0, bc, warn); + valsize, 0, bc, warn); /*printf("val %ld\n",yasm_intnum_get_int(intn));*/ yasm_intnum_destroy(intn); return retval; @@ -592,37 +592,37 @@ macho_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) assert(info != NULL); bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info, - macho_objfmt_output_value, NULL); + macho_objfmt_output_value, NULL); /* Don't bother doing anything else if size ended up being 0. */ if (size == 0) { - if (bigbuf) - yasm_xfree(bigbuf); - return 0; + if (bigbuf) + yasm_xfree(bigbuf); + return 0; } /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { - unsigned long left; - - yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, - N_("uninitialized space: zeroing")); - /* Write out in chunks */ - memset(info->buf, 0, REGULAR_OUTBUF_SIZE); - left = size; - while (left > REGULAR_OUTBUF_SIZE) { - fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); - left -= REGULAR_OUTBUF_SIZE; - } - fwrite(info->buf, left, 1, info->f); + unsigned long left; + + yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, + N_("uninitialized space: zeroing")); + /* Write out in chunks */ + memset(info->buf, 0, REGULAR_OUTBUF_SIZE); + left = size; + while (left > REGULAR_OUTBUF_SIZE) { + fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); + left -= REGULAR_OUTBUF_SIZE; + } + fwrite(info->buf, left, 1, info->f); } else { - /* Output buf (or bigbuf if non-NULL) to file */ - fwrite(bigbuf ? bigbuf : info->buf, (size_t) size, 1, info->f); + /* Output buf (or bigbuf if non-NULL) to file */ + fwrite(bigbuf ? bigbuf : info->buf, (size_t) size, 1, info->f); } /* If bigbuf was allocated, free it */ if (bigbuf) - yasm_xfree(bigbuf); + yasm_xfree(bigbuf); return 0; } @@ -631,23 +631,23 @@ static int macho_objfmt_output_section(yasm_section *sect, /*@null@ */ void *d) { /*@null@ */ macho_objfmt_output_info *info = - (macho_objfmt_output_info *) d; + (macho_objfmt_output_info *) d; /*@dependent@ *//*@null@ */ macho_section_data *msd; /* FIXME: Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); msd = yasm_section_get_data(sect, &macho_section_data_cb); assert(msd != NULL); if (!(msd->flags & S_ZEROFILL)) { - /* Output non-BSS sections */ - info->sect = sect; - info->msd = msd; - yasm_section_bcs_traverse(sect, info->errwarns, info, - macho_objfmt_output_bytecode); + /* Output non-BSS sections */ + info->sect = sect; + info->msd = msd; + yasm_section_bcs_traverse(sect, info->errwarns, info, + macho_objfmt_output_bytecode); } return 0; } @@ -661,38 +661,38 @@ macho_objfmt_output_relocs(yasm_section *sect, /*@null@*/ void *d) /* FIXME: Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; reloc = (macho_reloc *)yasm_section_relocs_first(sect); while (reloc) { - unsigned char *localbuf = info->buf; - /*@null@*/ macho_symrec_data *xsymd; - unsigned long symnum; - - xsymd = yasm_symrec_get_data(reloc->reloc.sym, &macho_symrec_data_cb); - yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); - localbuf += 4; /* address of relocation */ - - if (reloc->ext) - symnum = xsymd->index; - else { - /* find section where the symbol relates to */ - /*@dependent@*/ /*@null@*/ yasm_section *dsect; - /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; - symnum = 0; /* default to absolute */ - if (yasm_symrec_get_label(reloc->reloc.sym, &precbc) && - (dsect = yasm_bc_get_section(precbc)) && - (msd = yasm_section_get_data(dsect, &macho_section_data_cb))) - symnum = msd->scnum+1; - } - YASM_WRITE_32_L(localbuf, - (symnum & 0x00ffffff) | - (((unsigned long)reloc->pcrel & 1) << 24) | - (((unsigned long)reloc->length & 3) << 25) | - (((unsigned long)reloc->ext & 1) << 27) | - (((unsigned long)reloc->type & 0xf) << 28)); - fwrite(info->buf, 8, 1, info->f); - reloc = (macho_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); + unsigned char *localbuf = info->buf; + /*@null@*/ macho_symrec_data *xsymd; + unsigned long symnum; + + xsymd = yasm_symrec_get_data(reloc->reloc.sym, &macho_symrec_data_cb); + yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); + localbuf += 4; /* address of relocation */ + + if (reloc->ext) + symnum = xsymd->index; + else { + /* find section where the symbol relates to */ + /*@dependent@*/ /*@null@*/ yasm_section *dsect; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; + symnum = 0; /* default to absolute */ + if (yasm_symrec_get_label(reloc->reloc.sym, &precbc) && + (dsect = yasm_bc_get_section(precbc)) && + (msd = yasm_section_get_data(dsect, &macho_section_data_cb))) + symnum = msd->scnum+1; + } + YASM_WRITE_32_L(localbuf, + (symnum & 0x00ffffff) | + (((unsigned long)reloc->pcrel & 1) << 24) | + (((unsigned long)reloc->length & 3) << 25) | + (((unsigned long)reloc->ext & 1) << 27) | + (((unsigned long)reloc->type & 0xf) << 28)); + fwrite(info->buf, 8, 1, info->f); + reloc = (macho_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); } return 0; @@ -704,8 +704,8 @@ exp2_to_bits(unsigned long val) int ret = 0; while (val) { - val >>= 1; - ret++; + val >>= 1; + ret++; } ret = (ret > 0) ? ret - 1 : 0; @@ -720,18 +720,18 @@ macho_objfmt_is_section_label(yasm_symrec *sym) /* Look at symrec for value/scnum/etc. */ if (yasm_symrec_get_label(sym, &precbc)) { - if (precbc) - sect = yasm_bc_get_section(precbc); - else - sect = NULL; - /* it's a label: get value and offset. - * If there is not a section, leave as debugging symbol. - */ - if (sect) { - if (strcmp(yasm_symrec_get_name(sym), - yasm_section_get_name(sect)) == 0) - return 1; /* don't store section names */ - } + if (precbc) + sect = yasm_bc_get_section(precbc); + else + sect = NULL; + /* it's a label: get value and offset. + * If there is not a section, leave as debugging symbol. + */ + if (sect) { + if (strcmp(yasm_symrec_get_name(sym), + yasm_section_get_name(sect)) == 0) + return 1; /* don't store section names */ + } } return 0; } @@ -746,7 +746,7 @@ macho_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); objfmt_macho = info->objfmt_macho; @@ -762,45 +762,45 @@ macho_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) strncpy((char *)localbuf, msd->segname, 16); localbuf += 16; /* section address, size depend on 32/64 bit mode */ - YASM_WRITE_32_L(localbuf, msd->vmoff); /* address in memory */ + YASM_WRITE_32_L(localbuf, msd->vmoff); /* address in memory */ if (info->is_64) - YASM_WRITE_32_L(localbuf, 0); /* 64-bit mode: upper 32 bits = 0 */ - YASM_WRITE_32_L(localbuf, msd->size); /* size in memory */ + YASM_WRITE_32_L(localbuf, 0); /* 64-bit mode: upper 32 bits = 0 */ + YASM_WRITE_32_L(localbuf, msd->size); /* size in memory */ if (info->is_64) - YASM_WRITE_32_L(localbuf, 0); /* 64-bit mode: upper 32 bits = 0 */ + YASM_WRITE_32_L(localbuf, 0); /* 64-bit mode: upper 32 bits = 0 */ /* offset,align,reloff,nreloc,flags,reserved1,reserved2 are 32 bit */ if ((msd->flags & SECTION_TYPE) != S_ZEROFILL) { - YASM_WRITE_32_L(localbuf, msd->offset); - YASM_WRITE_32_L(localbuf, exp2_to_bits(yasm_section_get_align(sect))); - if (msd->nreloc) { - msd->flags |= S_ATTR_LOC_RELOC; - if (msd->extreloc) - msd->flags |= S_ATTR_EXT_RELOC; - YASM_WRITE_32_L(localbuf, - align32((long)(info->rel_base + info->s_reloff))); - YASM_WRITE_32_L(localbuf, msd->nreloc); /* nreloc */ - } else { - YASM_WRITE_32_L(localbuf, 0); - YASM_WRITE_32_L(localbuf, 0); - } - - info->s_reloff += msd->nreloc * MACHO_RELINFO_SIZE; /* nreloc */ + YASM_WRITE_32_L(localbuf, msd->offset); + YASM_WRITE_32_L(localbuf, exp2_to_bits(yasm_section_get_align(sect))); + if (msd->nreloc) { + msd->flags |= S_ATTR_LOC_RELOC; + if (msd->extreloc) + msd->flags |= S_ATTR_EXT_RELOC; + YASM_WRITE_32_L(localbuf, + align32((long)(info->rel_base + info->s_reloff))); + YASM_WRITE_32_L(localbuf, msd->nreloc); /* nreloc */ + } else { + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + } + + info->s_reloff += msd->nreloc * MACHO_RELINFO_SIZE; /* nreloc */ } else { - YASM_WRITE_32_L(localbuf, 0); /* these are zero in BSS */ - YASM_WRITE_32_L(localbuf, 0); - YASM_WRITE_32_L(localbuf, 0); - YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); /* these are zero in BSS */ + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); } - YASM_WRITE_32_L(localbuf, msd->flags); /* flags */ - YASM_WRITE_32_L(localbuf, 0); /* reserved 1 */ - YASM_WRITE_32_L(localbuf, 0); /* reserved 2 */ + YASM_WRITE_32_L(localbuf, msd->flags); /* flags */ + YASM_WRITE_32_L(localbuf, 0); /* reserved 1 */ + YASM_WRITE_32_L(localbuf, 0); /* reserved 2 */ if (info->is_64) - fwrite(info->buf, MACHO_SECTCMD64_SIZE, 1, info->f); + fwrite(info->buf, MACHO_SECTCMD64_SIZE, 1, info->f); else - fwrite(info->buf, MACHO_SECTCMD_SIZE, 1, info->f); + fwrite(info->buf, MACHO_SECTCMD_SIZE, 1, info->f); return 0; } @@ -815,32 +815,32 @@ macho_objfmt_count_sym(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (info->all_syms || - vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { - if (0 == macho_objfmt_is_section_label(sym)) { - /* Save index in symrec data */ - macho_symrec_data *sym_data = - yasm_symrec_get_data(sym, &macho_symrec_data_cb); - if (!sym_data) { - sym_data = yasm_xcalloc(sizeof(macho_symrec_data), 1); - yasm_symrec_add_data(sym, &macho_symrec_data_cb, sym_data); - } - sym_data->index = info->symindex; - info->symindex++; - - name = yasm_symrec_get_name(sym); /*printf("%s\n",name); */ - sym_data->add_uscore = 0; + vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { + if (0 == macho_objfmt_is_section_label(sym)) { + /* Save index in symrec data */ + macho_symrec_data *sym_data = + yasm_symrec_get_data(sym, &macho_symrec_data_cb); + if (!sym_data) { + sym_data = yasm_xcalloc(sizeof(macho_symrec_data), 1); + yasm_symrec_add_data(sym, &macho_symrec_data_cb, sym_data); + } + sym_data->index = info->symindex; + info->symindex++; + + name = yasm_symrec_get_name(sym); /*printf("%s\n",name); */ + sym_data->add_uscore = 0; #ifdef AUTO_UNDERSCORE - if (vis & (YASM_SYM_EXTERN | YASM_SYM_COMMON | YASM_SYM_GLOBAL)) { - if (name[0] != '_') - sym_data->add_uscore = 1; - } + if (vis & (YASM_SYM_EXTERN | YASM_SYM_COMMON | YASM_SYM_GLOBAL)) { + if (name[0] != '_') + sym_data->add_uscore = 1; + } #endif - /* name length + delimiter */ - sym_data->length = - (unsigned long)strlen(name) + sym_data->add_uscore + 1; - info->strlength += sym_data->length; - info->indx++; - } + /* name length + delimiter */ + sym_data->length = + (unsigned long)strlen(name) + sym_data->add_uscore + 1; + info->strlength += sym_data->length; + info->indx++; + } } return 0; } @@ -855,130 +855,130 @@ macho_objfmt_output_symtable(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (info->all_syms || - vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { - const yasm_expr *equ_val; - const yasm_intnum *intn; - unsigned long value = 0; - long scnum = -3; /* -3 = debugging symbol */ - /*@dependent@*/ /*@null@*/ yasm_section *sect; - /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; - unsigned char *localbuf; - yasm_intnum *val; - unsigned int long_int_bytes = (info->is_64) ? 8 : 4; - unsigned int n_type = 0, n_sect = 0, n_desc = 0; - macho_symrec_data *symd; - - val = yasm_intnum_create_uint(0); - - symd = yasm_symrec_get_data(sym, &macho_symrec_data_cb); - - /* Look at symrec for value/scnum/etc. */ - if (yasm_symrec_get_label(sym, &precbc)) { - if (precbc) - sect = yasm_bc_get_section(precbc); - else - sect = NULL; - /* it's a label: get value and offset. - * If there is not a section, leave as debugging symbol. - */ - if (sect) { - /*@dependent@*/ /*@null@*/ macho_section_data *msd; - - if (strcmp(yasm_symrec_get_name(sym), - yasm_section_get_name(sect)) == 0) { - /* don't store section names */ - yasm_intnum_destroy(val); - return 0; - } - msd = yasm_section_get_data(sect, &macho_section_data_cb); - if (msd) { - scnum = msd->scnum; - n_type = N_SECT; - } else { - if (yasm_section_is_absolute(sect)) { - yasm_expr *abs_start; - - abs_start = - yasm_expr_copy(yasm_section_get_start(sect)); - intn = yasm_expr_get_intnum(&abs_start, 1); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("absolute section start not an integer expression")); - yasm_errwarn_propagate(info->errwarns, - abs_start->line); - } else - value = yasm_intnum_get_uint(intn); - yasm_expr_destroy(abs_start); - - scnum = -2; /* -2 = absolute symbol */ - } else - yasm_internal_error(N_("didn't understand section")); - } - if (precbc) - value += yasm_bc_next_offset(precbc); - /* all values are subject to correction: base offset is first - * raw section, therefore add section offset - */ - if (msd) - value += msd->vmoff; - yasm_intnum_set_uint(val, value); - /*printf("%s offset %lx\n",name,value);*/ - } - } else if ((equ_val = yasm_symrec_get_equ(sym))) { - yasm_expr *equ_val_copy = yasm_expr_copy(equ_val); - - intn = yasm_expr_get_intnum(&equ_val_copy, 1); - if (!intn) { - if (vis & YASM_SYM_GLOBAL) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("global EQU value not an integer expression")); - yasm_errwarn_propagate(info->errwarns, equ_val->line); - } - } else - value = yasm_intnum_get_uint(intn); - yasm_expr_destroy(equ_val_copy); - yasm_intnum_set_uint(val, value); - n_type = N_ABS; - scnum = -2; /* -2 = absolute symbol */ - } - - if (vis & YASM_SYM_EXTERN) { - n_type = N_EXT; - scnum = -1; - /*n_desc = REFERENCE_FLAG_UNDEFINED_LAZY; * FIXME: see definition of REFERENCE_FLAG_* above */ - } else if (vis & YASM_SYM_COMMON) { - yasm_expr **csize = yasm_symrec_get_common_size(sym); - n_type = N_UNDF | N_EXT; - if (csize) { - intn = yasm_expr_get_intnum(csize, 1); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("COMMON data size not an integer expression")); - yasm_errwarn_propagate(info->errwarns, (*csize)->line); - } else - yasm_intnum_set_uint(val, yasm_intnum_get_uint(intn)); - } - /*printf("common symbol %s val %lu\n", name, yasm_intnum_get_uint(val));*/ - } else if (vis & YASM_SYM_GLOBAL) { - n_type |= N_EXT; - } - - localbuf = info->buf; - YASM_WRITE_32_L(localbuf, info->indx); /* offset in string table */ - YASM_WRITE_8(localbuf, n_type); /* type of symbol entry */ - n_sect = (scnum >= 0) ? scnum + 1 : NO_SECT; - YASM_WRITE_8(localbuf, n_sect); /* referring section where symbol is found */ - YASM_WRITE_16_L(localbuf, n_desc); /* extra description */ - yasm_intnum_get_sized(val, localbuf, long_int_bytes, ((long_int_bytes) << 3), 0, 0, 0); /* value/argument */ - localbuf += long_int_bytes; - if (symd) - symd->value = val; - else - yasm_intnum_destroy(val); - - info->indx += symd->length; - - fwrite(info->buf, 8 + long_int_bytes, 1, info->f); + vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { + const yasm_expr *equ_val; + const yasm_intnum *intn; + unsigned long value = 0; + long scnum = -3; /* -3 = debugging symbol */ + /*@dependent@*/ /*@null@*/ yasm_section *sect; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; + unsigned char *localbuf; + yasm_intnum *val; + unsigned int long_int_bytes = (info->is_64) ? 8 : 4; + unsigned int n_type = 0, n_sect = 0, n_desc = 0; + macho_symrec_data *symd; + + val = yasm_intnum_create_uint(0); + + symd = yasm_symrec_get_data(sym, &macho_symrec_data_cb); + + /* Look at symrec for value/scnum/etc. */ + if (yasm_symrec_get_label(sym, &precbc)) { + if (precbc) + sect = yasm_bc_get_section(precbc); + else + sect = NULL; + /* it's a label: get value and offset. + * If there is not a section, leave as debugging symbol. + */ + if (sect) { + /*@dependent@*/ /*@null@*/ macho_section_data *msd; + + if (strcmp(yasm_symrec_get_name(sym), + yasm_section_get_name(sect)) == 0) { + /* don't store section names */ + yasm_intnum_destroy(val); + return 0; + } + msd = yasm_section_get_data(sect, &macho_section_data_cb); + if (msd) { + scnum = msd->scnum; + n_type = N_SECT; + } else { + if (yasm_section_is_absolute(sect)) { + yasm_expr *abs_start; + + abs_start = + yasm_expr_copy(yasm_section_get_start(sect)); + intn = yasm_expr_get_intnum(&abs_start, 1); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("absolute section start not an integer expression")); + yasm_errwarn_propagate(info->errwarns, + abs_start->line); + } else + value = yasm_intnum_get_uint(intn); + yasm_expr_destroy(abs_start); + + scnum = -2; /* -2 = absolute symbol */ + } else + yasm_internal_error(N_("didn't understand section")); + } + if (precbc) + value += yasm_bc_next_offset(precbc); + /* all values are subject to correction: base offset is first + * raw section, therefore add section offset + */ + if (msd) + value += msd->vmoff; + yasm_intnum_set_uint(val, value); + /*printf("%s offset %lx\n",name,value);*/ + } + } else if ((equ_val = yasm_symrec_get_equ(sym))) { + yasm_expr *equ_val_copy = yasm_expr_copy(equ_val); + + intn = yasm_expr_get_intnum(&equ_val_copy, 1); + if (!intn) { + if (vis & YASM_SYM_GLOBAL) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("global EQU value not an integer expression")); + yasm_errwarn_propagate(info->errwarns, equ_val->line); + } + } else + value = yasm_intnum_get_uint(intn); + yasm_expr_destroy(equ_val_copy); + yasm_intnum_set_uint(val, value); + n_type = N_ABS; + scnum = -2; /* -2 = absolute symbol */ + } + + if (vis & YASM_SYM_EXTERN) { + n_type = N_EXT; + scnum = -1; + /*n_desc = REFERENCE_FLAG_UNDEFINED_LAZY; * FIXME: see definition of REFERENCE_FLAG_* above */ + } else if (vis & YASM_SYM_COMMON) { + yasm_expr **csize = yasm_symrec_get_common_size(sym); + n_type = N_UNDF | N_EXT; + if (csize) { + intn = yasm_expr_get_intnum(csize, 1); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("COMMON data size not an integer expression")); + yasm_errwarn_propagate(info->errwarns, (*csize)->line); + } else + yasm_intnum_set_uint(val, yasm_intnum_get_uint(intn)); + } + /*printf("common symbol %s val %lu\n", name, yasm_intnum_get_uint(val));*/ + } else if (vis & YASM_SYM_GLOBAL) { + n_type |= N_EXT; + } + + localbuf = info->buf; + YASM_WRITE_32_L(localbuf, info->indx); /* offset in string table */ + YASM_WRITE_8(localbuf, n_type); /* type of symbol entry */ + n_sect = (scnum >= 0) ? scnum + 1 : NO_SECT; + YASM_WRITE_8(localbuf, n_sect); /* referring section where symbol is found */ + YASM_WRITE_16_L(localbuf, n_desc); /* extra description */ + yasm_intnum_get_sized(val, localbuf, long_int_bytes, ((long_int_bytes) << 3), 0, 0, 0); /* value/argument */ + localbuf += long_int_bytes; + if (symd) + symd->value = val; + else + yasm_intnum_destroy(val); + + info->indx += symd->length; + + fwrite(info->buf, 8 + long_int_bytes, 1, info->f); } return 0; @@ -996,16 +996,16 @@ macho_objfmt_output_str(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (info->all_syms || - vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { - if (0 == macho_objfmt_is_section_label(sym)) { - const char *name = yasm_symrec_get_name(sym); - size_t len = strlen(name); - - xsymd = yasm_symrec_get_data(sym, &macho_symrec_data_cb); - if (xsymd->add_uscore) - fputc('_', info->f); - fwrite(name, len + 1, 1, info->f); - } + vis & (YASM_SYM_GLOBAL | YASM_SYM_COMMON | YASM_SYM_EXTERN)) { + if (0 == macho_objfmt_is_section_label(sym)) { + const char *name = yasm_symrec_get_name(sym); + size_t len = strlen(name); + + xsymd = yasm_symrec_get_data(sym, &macho_symrec_data_cb); + if (xsymd->add_uscore) + fputc('_', info->f); + fwrite(name, len + 1, 1, info->f); + } } return 0; } @@ -1014,12 +1014,12 @@ static int macho_objfmt_calc_sectsize(yasm_section *sect, /*@null@ */ void *d) { /*@null@ */ macho_objfmt_output_info *info = - (macho_objfmt_output_info *) d; + (macho_objfmt_output_info *) d; /*@dependent@ *//*@null@ */ macho_section_data *msd; /* FIXME: Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); msd = yasm_section_get_data(sect, &macho_section_data_cb); @@ -1027,9 +1027,9 @@ macho_objfmt_calc_sectsize(yasm_section *sect, /*@null@ */ void *d) msd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); if (!(msd->flags & S_ZEROFILL)) { - msd->offset = info->offset; - info->offset += msd->size; - info->filesize += msd->size; + msd->offset = info->offset; + info->offset += msd->size; + info->filesize += msd->size; } /* accumulate size in memory */ @@ -1042,7 +1042,7 @@ macho_objfmt_calc_sectsize(yasm_section *sect, /*@null@ */ void *d) /* write object */ static void macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_objfmt_macho *objfmt_macho = (yasm_objfmt_macho *)object->objfmt; macho_objfmt_output_info info; @@ -1064,9 +1064,9 @@ macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, info.buf = yasm_xmalloc(REGULAR_OUTBUF_SIZE); if (objfmt_macho->parse_scnum == 0) { - yasm_internal_error(N_("no sections defined")); - /*@notreached@*/ - return; + yasm_internal_error(N_("no sections defined")); + /*@notreached@*/ + return; } val = yasm_intnum_create_uint(0); @@ -1076,44 +1076,44 @@ macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, */ info.is_64 = (objfmt_macho->bits == 32) ? 0 : 1; if (info.is_64) { - /* this works only when SYMBOLS and SECTIONS present */ - headsize = - MACHO_HEADER64_SIZE + MACHO_SEGCMD64_SIZE + - (MACHO_SECTCMD64_SIZE * (objfmt_macho->parse_scnum)) + - MACHO_SYMCMD_SIZE; - macho_segcmd = LC_SEGMENT_64; - macho_segcmdsize = MACHO_SEGCMD64_SIZE; - macho_sectcmdsize = MACHO_SECTCMD64_SIZE; - macho_nlistsize = MACHO_NLIST64_SIZE; - macho_relinfosize = MACHO_RELINFO64_SIZE; - long_int_bytes = 8; + /* this works only when SYMBOLS and SECTIONS present */ + headsize = + MACHO_HEADER64_SIZE + MACHO_SEGCMD64_SIZE + + (MACHO_SECTCMD64_SIZE * (objfmt_macho->parse_scnum)) + + MACHO_SYMCMD_SIZE; + macho_segcmd = LC_SEGMENT_64; + macho_segcmdsize = MACHO_SEGCMD64_SIZE; + macho_sectcmdsize = MACHO_SECTCMD64_SIZE; + macho_nlistsize = MACHO_NLIST64_SIZE; + macho_relinfosize = MACHO_RELINFO64_SIZE; + long_int_bytes = 8; } else { - headsize = - MACHO_HEADER_SIZE + MACHO_SEGCMD_SIZE + - (MACHO_SECTCMD_SIZE * (objfmt_macho->parse_scnum)) + - MACHO_SYMCMD_SIZE; - macho_segcmd = LC_SEGMENT; - macho_segcmdsize = MACHO_SEGCMD_SIZE; - macho_sectcmdsize = MACHO_SECTCMD_SIZE; - macho_nlistsize = MACHO_NLIST_SIZE; - macho_relinfosize = MACHO_RELINFO_SIZE; - long_int_bytes = 4; + headsize = + MACHO_HEADER_SIZE + MACHO_SEGCMD_SIZE + + (MACHO_SECTCMD_SIZE * (objfmt_macho->parse_scnum)) + + MACHO_SYMCMD_SIZE; + macho_segcmd = LC_SEGMENT; + macho_segcmdsize = MACHO_SEGCMD_SIZE; + macho_sectcmdsize = MACHO_SECTCMD_SIZE; + macho_nlistsize = MACHO_NLIST_SIZE; + macho_relinfosize = MACHO_RELINFO_SIZE; + long_int_bytes = 4; } /* Get number of symbols */ info.symindex = 0; info.indx = 0; - info.strlength = 1; /* string table starts with a zero byte */ + info.strlength = 1; /* string table starts with a zero byte */ info.all_syms = all_syms || info.is_64; - /*info.all_syms = 1; * force all syms into symbol table */ + /*info.all_syms = 1; * force all syms into symbol table */ yasm_symtab_traverse(object->symtab, &info, macho_objfmt_count_sym); symtab_count = info.indx; /* write raw section data first */ if (fseek(f, (long)headsize, SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@ */ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@ */ + return; } /* get size of sections in memory (including BSS) and size of sections @@ -1131,56 +1131,56 @@ macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Write headers */ if (fseek(f, 0, SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } localbuf = info.buf; /* header size is common to 32 bit and 64 bit variants */ if (info.is_64) { - YASM_WRITE_32_L(localbuf, MH_MAGIC_64); /* magic number */ - /* i386 64-bit ABI */ - YASM_WRITE_32_L(localbuf, CPU_ARCH_ABI64 | CPU_TYPE_I386); + YASM_WRITE_32_L(localbuf, MH_MAGIC_64); /* magic number */ + /* i386 64-bit ABI */ + YASM_WRITE_32_L(localbuf, CPU_ARCH_ABI64 | CPU_TYPE_I386); } else { - YASM_WRITE_32_L(localbuf, MH_MAGIC); /* magic number */ - YASM_WRITE_32_L(localbuf, CPU_TYPE_I386); /* i386 32-bit ABI */ + YASM_WRITE_32_L(localbuf, MH_MAGIC); /* magic number */ + YASM_WRITE_32_L(localbuf, CPU_TYPE_I386); /* i386 32-bit ABI */ } /* i386 all cpu subtype compatible */ YASM_WRITE_32_L(localbuf, CPU_SUBTYPE_I386_ALL); - YASM_WRITE_32_L(localbuf, MH_OBJECT); /* MACH file type */ + YASM_WRITE_32_L(localbuf, MH_OBJECT); /* MACH file type */ /* calculate number of commands and their size, put to stream */ head_ncmds = 0; head_sizeofcmds = 0; if (objfmt_macho->parse_scnum > 0) { - head_ncmds++; - head_sizeofcmds += - macho_segcmdsize + macho_sectcmdsize * objfmt_macho->parse_scnum; + head_ncmds++; + head_sizeofcmds += + macho_segcmdsize + macho_sectcmdsize * objfmt_macho->parse_scnum; } if (symtab_count > 0) { - head_ncmds++; - head_sizeofcmds += MACHO_SYMCMD_SIZE; + head_ncmds++; + head_sizeofcmds += MACHO_SYMCMD_SIZE; } YASM_WRITE_32_L(localbuf, head_ncmds); YASM_WRITE_32_L(localbuf, head_sizeofcmds); - YASM_WRITE_32_L(localbuf, 0); /* no flags (yet) */ + YASM_WRITE_32_L(localbuf, 0); /* no flags (yet) */ if (info.is_64) { - YASM_WRITE_32_L(localbuf, 0); /* reserved in 64 bit */ - fileoffset = MACHO_HEADER64_SIZE + head_sizeofcmds; + YASM_WRITE_32_L(localbuf, 0); /* reserved in 64 bit */ + fileoffset = MACHO_HEADER64_SIZE + head_sizeofcmds; } else { - /* initial offset to first section */ - fileoffset = MACHO_HEADER_SIZE + head_sizeofcmds; + /* initial offset to first section */ + fileoffset = MACHO_HEADER_SIZE + head_sizeofcmds; } /* --------------- write segment header command ---------------- */ - YASM_WRITE_32_L(localbuf, macho_segcmd); /* command LC_SEGMENT */ + YASM_WRITE_32_L(localbuf, macho_segcmd); /* command LC_SEGMENT */ /* size of load command including section load commands */ YASM_WRITE_32_L(localbuf, - macho_segcmdsize + - macho_sectcmdsize * objfmt_macho->parse_scnum); + macho_segcmdsize + + macho_sectcmdsize * objfmt_macho->parse_scnum); /* in an MH_OBJECT file all sections are in one unnamed (name all zeros) * segment (16x0) */ @@ -1190,29 +1190,29 @@ macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, YASM_WRITE_32_L(localbuf, 0); /* in-memory offset, in-memory size */ - yasm_intnum_set_uint(val, 0); /* offset in memory (vmaddr) */ + yasm_intnum_set_uint(val, 0); /* offset in memory (vmaddr) */ yasm_intnum_get_sized(val, localbuf, long_int_bytes, - ((long_int_bytes) << 3), 0, 0, 0); + ((long_int_bytes) << 3), 0, 0, 0); localbuf += long_int_bytes; - yasm_intnum_set_uint(val, info.vmsize); /* size in memory (vmsize) */ + yasm_intnum_set_uint(val, info.vmsize); /* size in memory (vmsize) */ yasm_intnum_get_sized(val, localbuf, long_int_bytes, - ((long_int_bytes) << 3), 0, 0, 0); + ((long_int_bytes) << 3), 0, 0, 0); localbuf += long_int_bytes; /* offset in file to first section */ yasm_intnum_set_uint(val, fileoffset); yasm_intnum_get_sized(val, localbuf, long_int_bytes, - ((long_int_bytes) << 3), 0, 0, 0); + ((long_int_bytes) << 3), 0, 0, 0); localbuf += long_int_bytes; - yasm_intnum_set_uint(val, info.filesize); /* overall size in file */ + yasm_intnum_set_uint(val, info.filesize); /* overall size in file */ yasm_intnum_get_sized(val, localbuf, long_int_bytes, - ((long_int_bytes) << 3), 0, 0, 0); + ((long_int_bytes) << 3), 0, 0, 0); localbuf += long_int_bytes; - YASM_WRITE_32_L(localbuf, VM_PROT_DEFAULT); /* VM protection, maximum */ - YASM_WRITE_32_L(localbuf, VM_PROT_DEFAULT); /* VM protection, initial */ + YASM_WRITE_32_L(localbuf, VM_PROT_DEFAULT); /* VM protection, maximum */ + YASM_WRITE_32_L(localbuf, VM_PROT_DEFAULT); /* VM protection, initial */ /* number of sections */ YASM_WRITE_32_L(localbuf, objfmt_macho->parse_scnum); - YASM_WRITE_32_L(localbuf, 0); /* no flags */ + YASM_WRITE_32_L(localbuf, 0); /* no flags */ /* write MACH-O header and segment command to outfile */ fwrite(info.buf, (size_t) (localbuf - info.buf), 1, f); @@ -1220,20 +1220,20 @@ macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* next: section headers */ /* offset to relocs for first section */ info.rel_base = align32((long)fileoffset + (long)info.filesize); - info.s_reloff = 0; /* offset for relocs of following sections */ + info.s_reloff = 0; /* offset for relocs of following sections */ yasm_object_sections_traverse(object, &info, macho_objfmt_output_secthead); localbuf = info.buf; /* write out symbol command */ - YASM_WRITE_32_L(localbuf, LC_SYMTAB); /* cmd == LC_SYMTAB */ + YASM_WRITE_32_L(localbuf, LC_SYMTAB); /* cmd == LC_SYMTAB */ YASM_WRITE_32_L(localbuf, MACHO_SYMCMD_SIZE); /* symbol table offset */ YASM_WRITE_32_L(localbuf, info.rel_base + info.s_reloff); - YASM_WRITE_32_L(localbuf, symtab_count); /* number of symbols */ + YASM_WRITE_32_L(localbuf, symtab_count); /* number of symbols */ YASM_WRITE_32_L(localbuf, macho_nlistsize * symtab_count + info.rel_base + - info.s_reloff); /* string table offset */ - YASM_WRITE_32_L(localbuf, info.strlength); /* string table size */ + info.s_reloff); /* string table offset */ + YASM_WRITE_32_L(localbuf, info.strlength); /* string table size */ /* write symbol command */ fwrite(info.buf, (size_t)(localbuf - info.buf), 1, f); @@ -1242,21 +1242,21 @@ macho_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* get back to end of raw section data */ if (fseek(f, (long)fileoff_sections, SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } /* padding to long boundary */ if (info.rel_base - (fileoffset + info.filesize)) { - fwrite(pad_data, info.rel_base - (fileoffset + info.filesize), 1, f); + fwrite(pad_data, info.rel_base - (fileoffset + info.filesize), 1, f); } /* relocation data */ yasm_object_sections_traverse(object, &info, macho_objfmt_output_relocs); /* symbol table (NLIST) */ - info.indx = 1; /* restart symbol table indices */ + info.indx = 1; /* restart symbol table indices */ yasm_symtab_traverse(object->symtab, &info, macho_objfmt_output_symtable); /* symbol strings */ @@ -1275,7 +1275,7 @@ macho_objfmt_destroy(yasm_objfmt *objfmt) static macho_section_data * macho_objfmt_init_new_section(yasm_object *object, yasm_section *sect, - const char *sectname, unsigned long line) + const char *sectname, unsigned long line) { yasm_objfmt_macho *objfmt_macho = (yasm_objfmt_macho *)object->objfmt; macho_section_data *data; @@ -1292,7 +1292,7 @@ macho_objfmt_init_new_section(yasm_object *object, yasm_section *sect, yasm_section_add_data(sect, &macho_section_data_cb, data); sym = yasm_symtab_define_label(object->symtab, sectname, - yasm_section_bcs_first(sect), 1, line); + yasm_section_bcs_first(sect), 1, line); data->sym = sym; return data; } @@ -1306,21 +1306,21 @@ macho_objfmt_add_default_section(yasm_object *object) retval = yasm_object_get_general(object, ".text", 0, 0, 1, 0, &isnew, 0); if (isnew) { - msd = macho_objfmt_init_new_section(object, retval, ".text", 0); - msd->segname = "__TEXT"; - msd->sectname = "__text"; - msd->flags = S_ATTR_PURE_INSTRUCTIONS; - yasm_section_set_align(retval, 0, 0); - yasm_section_set_default(retval, 1); + msd = macho_objfmt_init_new_section(object, retval, ".text", 0); + msd->segname = "__TEXT"; + msd->sectname = "__text"; + msd->flags = S_ATTR_PURE_INSTRUCTIONS; + yasm_section_set_align(retval, 0, 0); + yasm_section_set_default(retval, 1); } return retval; } static /*@observer@*/ /*@null@*/ yasm_section * macho_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, - unsigned long line) + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_section *retval; @@ -1335,85 +1335,85 @@ macho_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, size_t i; static const struct { - const char *in; - const char *seg; - const char *sect; - unsigned long flags; - unsigned long align; + const char *in; + const char *seg; + const char *sect; + unsigned long flags; + unsigned long align; } section_name_translation[] = { - {".text", "__TEXT", "__text", S_ATTR_PURE_INSTRUCTIONS, 0}, - {".const", "__TEXT", "__const", S_REGULAR, 0}, - {".static_const", "__TEXT", "__static_const", S_REGULAR, 0}, - {".cstring", "__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, - {".literal4", "__TEXT", "__literal4", S_4BYTE_LITERALS, 4}, - {".literal8", "__TEXT", "__literal8", S_8BYTE_LITERALS, 8}, - {".literal16", "__TEXT", "__literal16", S_16BYTE_LITERALS, 16}, - {".constructor", "__TEXT", "__constructor", S_REGULAR, 0}, - {".destructor", "__TEXT", "__destructor", S_REGULAR, 0}, - {".fvmlib_init0", "__TEXT", "__fvmlib_init0", S_REGULAR, 0}, - {".fvmlib_init1", "__TEXT", "__fvmlib_init1", S_REGULAR, 0}, - {".mod_init_func", "__DATA", "__mod_init_func", - S_MOD_INIT_FUNC_POINTERS, 4}, - {".mod_term_func", "__DATA", "__mod_term_func", - S_MOD_TERM_FUNC_POINTERS, 4}, - {".dyld", "__DATA", "__dyld", S_REGULAR, 0}, - {".data", "__DATA", "__data", S_REGULAR, 0}, - {".static_data", "__DATA", "__static_data", S_REGULAR, 0}, - {".const_data", "__DATA", "__const", S_REGULAR, 0}, - {".rodata", "__DATA", "__const", S_REGULAR, 0}, - {".bss", "__DATA", "__bss", S_ZEROFILL, 0}, - {".objc_class_names", "__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, - {".objc_meth_var_types","__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, - {".objc_meth_var_names","__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, - {".objc_selector_strs", "__OBJC", "__selector_strs", - S_CSTRING_LITERALS, 0}, - {".objc_class", "__OBJC", "__class", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_meta_class", "__OBJC", "__meta_class", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_string_object", "__OBJC", "__string_object", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_protocol", "__OBJC", "__protocol", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_cat_cls_meth", "__OBJC", "__cat_cls_meth", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_cat_inst_meth", "__OBJC", "__cat_inst_meth", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_cls_meth", "__OBJC", "__cls_meth", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_inst_meth", "__OBJC", "__inst_meth", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_message_refs", "__OBJC", "__message_refs", - S_LITERAL_POINTERS|S_ATTR_NO_DEAD_STRIP, 4}, - {".objc_cls_refs", "__OBJC", "__cls_refs", - S_LITERAL_POINTERS|S_ATTR_NO_DEAD_STRIP, 4}, - {".objc_module_info", "__OBJC", "__module_info", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_symbols", "__OBJC", "__symbols", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_category", "__OBJC", "__category", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_class_vars", "__OBJC", "__class_vars", - S_ATTR_NO_DEAD_STRIP, 0}, - {".objc_instance_vars", "__OBJC", "__instance_vars", - S_ATTR_NO_DEAD_STRIP, 0} + {".text", "__TEXT", "__text", S_ATTR_PURE_INSTRUCTIONS, 0}, + {".const", "__TEXT", "__const", S_REGULAR, 0}, + {".static_const", "__TEXT", "__static_const", S_REGULAR, 0}, + {".cstring", "__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".literal4", "__TEXT", "__literal4", S_4BYTE_LITERALS, 4}, + {".literal8", "__TEXT", "__literal8", S_8BYTE_LITERALS, 8}, + {".literal16", "__TEXT", "__literal16", S_16BYTE_LITERALS, 16}, + {".constructor", "__TEXT", "__constructor", S_REGULAR, 0}, + {".destructor", "__TEXT", "__destructor", S_REGULAR, 0}, + {".fvmlib_init0", "__TEXT", "__fvmlib_init0", S_REGULAR, 0}, + {".fvmlib_init1", "__TEXT", "__fvmlib_init1", S_REGULAR, 0}, + {".mod_init_func", "__DATA", "__mod_init_func", + S_MOD_INIT_FUNC_POINTERS, 4}, + {".mod_term_func", "__DATA", "__mod_term_func", + S_MOD_TERM_FUNC_POINTERS, 4}, + {".dyld", "__DATA", "__dyld", S_REGULAR, 0}, + {".data", "__DATA", "__data", S_REGULAR, 0}, + {".static_data", "__DATA", "__static_data", S_REGULAR, 0}, + {".const_data", "__DATA", "__const", S_REGULAR, 0}, + {".rodata", "__DATA", "__const", S_REGULAR, 0}, + {".bss", "__DATA", "__bss", S_ZEROFILL, 0}, + {".objc_class_names", "__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".objc_meth_var_types","__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".objc_meth_var_names","__TEXT", "__cstring", S_CSTRING_LITERALS, 0}, + {".objc_selector_strs", "__OBJC", "__selector_strs", + S_CSTRING_LITERALS, 0}, + {".objc_class", "__OBJC", "__class", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_meta_class", "__OBJC", "__meta_class", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_string_object", "__OBJC", "__string_object", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_protocol", "__OBJC", "__protocol", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_cat_cls_meth", "__OBJC", "__cat_cls_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_cat_inst_meth", "__OBJC", "__cat_inst_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_cls_meth", "__OBJC", "__cls_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_inst_meth", "__OBJC", "__inst_meth", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_message_refs", "__OBJC", "__message_refs", + S_LITERAL_POINTERS|S_ATTR_NO_DEAD_STRIP, 4}, + {".objc_cls_refs", "__OBJC", "__cls_refs", + S_LITERAL_POINTERS|S_ATTR_NO_DEAD_STRIP, 4}, + {".objc_module_info", "__OBJC", "__module_info", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_symbols", "__OBJC", "__symbols", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_category", "__OBJC", "__category", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_class_vars", "__OBJC", "__class_vars", + S_ATTR_NO_DEAD_STRIP, 0}, + {".objc_instance_vars", "__OBJC", "__instance_vars", + S_ATTR_NO_DEAD_STRIP, 0} }; if (!vp || vp->param || !vp->val) - return NULL; + return NULL; sectname = vp->val; /* translate .text,.data,.bss to __text,__data,__bss... */ for (i=0; ival) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized numeric qualifier")); - continue; - } - - flags_override = 1; - if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { - /*@dependent@ *//*@null@ */ const yasm_intnum *align_expr; - - align_expr = yasm_expr_get_intnum(&vp->param, 0); - if (!align_expr) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not an integer"), - vp->val); - return NULL; - } - align = yasm_intnum_get_uint(align_expr); - - /* Alignments must be a power of two. */ - if (!is_exp2(align)) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), - vp->val); - return NULL; - } - - /* Check to see if alignment is supported size */ - if (align > 16384) { - yasm_error_set(YASM_ERROR_VALUE, - N_("macho implementation does not support alignments > 16384")); - return NULL; - } - } else - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized qualifier `%s'"), vp->val); + if (!vp->val) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized numeric qualifier")); + continue; + } + + flags_override = 1; + if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { + /*@dependent@ *//*@null@ */ const yasm_intnum *align_expr; + + align_expr = yasm_expr_get_intnum(&vp->param, 0); + if (!align_expr) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not an integer"), + vp->val); + return NULL; + } + align = yasm_intnum_get_uint(align_expr); + + /* Alignments must be a power of two. */ + if (!is_exp2(align)) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not a power of two"), + vp->val); + return NULL; + } + + /* Check to see if alignment is supported size */ + if (align > 16384) { + yasm_error_set(YASM_ERROR_VALUE, + N_("macho implementation does not support alignments > 16384")); + return NULL; + } + } else + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized qualifier `%s'"), vp->val); } retval = yasm_object_get_general(object, sectname, 0, align, 1, resonly, - &isnew, line); + &isnew, line); if (isnew) - msd = macho_objfmt_init_new_section(object, retval, sectname, line); + msd = macho_objfmt_init_new_section(object, retval, sectname, line); else - msd = yasm_section_get_data(retval, &macho_section_data_cb); + msd = yasm_section_get_data(retval, &macho_section_data_cb); if (isnew || yasm_section_is_default(retval)) { - yasm_section_set_default(retval, 0); - msd->segname = f_segname; - msd->sectname = f_sectname; - msd->flags = flags; - yasm_section_set_align(retval, align, line); + yasm_section_set_default(retval, 0); + msd->segname = f_segname; + msd->sectname = f_sectname; + msd->flags = flags; + yasm_section_set_align(retval, align, line); } else if (flags_override) - yasm_warn_set(YASM_WARN_GENERAL, - N_("section flags ignored on section redeclaration")); + yasm_warn_set(YASM_WARN_GENERAL, + N_("section flags ignored on section redeclaration")); return retval; } @@ -1514,9 +1514,9 @@ macho_symrec_data_print(void *data, FILE *f, int indent_level) fprintf(f, "%*sindex=%ld\n", indent_level, "", msd->index); fprintf(f, "%*svalue=", indent_level, ""); if (msd->value) - fprintf(f, "%ld\n", yasm_intnum_get_int(msd->value)); + fprintf(f, "%ld\n", yasm_intnum_get_int(msd->value)); else - fprintf(f, "nil\n"); + fprintf(f, "nil\n"); } diff --git a/modules/objfmts/macho/tests/nasm32/machotest.c b/modules/objfmts/macho/tests/nasm32/machotest.c index 84badeed..42fc92c0 100644 --- a/modules/objfmts/macho/tests/nasm32/machotest.c +++ b/modules/objfmts/macho/tests/nasm32/machotest.c @@ -29,7 +29,7 @@ int main(void) { greet(); printf("These pointers should be equal: %p and %p\n", - &greet, textptr); + &greet, textptr); printf("So should these: %p and %p\n", selfptr, &selfptr); } diff --git a/modules/objfmts/macho/tests/nasm64/machotest64.c b/modules/objfmts/macho/tests/nasm64/machotest64.c index a77e5e5d..c0f1e3c4 100644 --- a/modules/objfmts/macho/tests/nasm64/machotest64.c +++ b/modules/objfmts/macho/tests/nasm64/machotest64.c @@ -26,10 +26,10 @@ int main(void) { printf("This string should read `hello, world': `%s'\n", asmstr); { - long a,b; - a = (long)asmstr; - b = (long)getstr(); - printf("The pointers %lx and %lx should be equal\n",a,b); + long a,b; + a = (long)asmstr; + b = (long)getstr(); + printf("The pointers %lx and %lx should be equal\n",a,b); } printf("This string should read `hello, world': `%s'\n", getstr()); @@ -40,7 +40,7 @@ int main(void) { printf("The absolute addressing to the asm-local integer should yield in 1235:\n%ld\n",readgreet()); printf("These pointers should be equal: %p and %p\n", - &greet, textptr); + &greet, textptr); printf("So should these: %p and %p\n", selfptr, &selfptr); } diff --git a/modules/objfmts/rdf/rdf-objfmt.c b/modules/objfmts/rdf/rdf-objfmt.c index 541935de..aa8b9b88 100644 --- a/modules/objfmts/rdf/rdf-objfmt.c +++ b/modules/objfmts/rdf/rdf-objfmt.c @@ -33,74 +33,74 @@ #include -#define REGULAR_OUTBUF_SIZE 1024 +#define REGULAR_OUTBUF_SIZE 1024 -#define RDF_MAGIC "RDOFF2" +#define RDF_MAGIC "RDOFF2" /* Maximum size of an import/export label (including trailing zero) */ -#define EXIM_LABEL_MAX 64 +#define EXIM_LABEL_MAX 64 /* Maximum size of library or module name (including trailing zero) */ -#define MODLIB_NAME_MAX 128 +#define MODLIB_NAME_MAX 128 /* Maximum number of segments that we can handle in one file */ -#define RDF_MAXSEGS 64 +#define RDF_MAXSEGS 64 /* Record types that may present the RDOFF header */ -#define RDFREC_GENERIC 0 -#define RDFREC_RELOC 1 -#define RDFREC_IMPORT 2 -#define RDFREC_GLOBAL 3 -#define RDFREC_DLL 4 -#define RDFREC_BSS 5 -#define RDFREC_SEGRELOC 6 -#define RDFREC_FARIMPORT 7 -#define RDFREC_MODNAME 8 -#define RDFREC_COMMON 10 +#define RDFREC_GENERIC 0 +#define RDFREC_RELOC 1 +#define RDFREC_IMPORT 2 +#define RDFREC_GLOBAL 3 +#define RDFREC_DLL 4 +#define RDFREC_BSS 5 +#define RDFREC_SEGRELOC 6 +#define RDFREC_FARIMPORT 7 +#define RDFREC_MODNAME 8 +#define RDFREC_COMMON 10 /* Flags for ExportRec/ImportRec */ -#define SYM_DATA 1 -#define SYM_FUNCTION 2 +#define SYM_DATA 1 +#define SYM_FUNCTION 2 /* Flags for ExportRec */ -#define SYM_GLOBAL 4 +#define SYM_GLOBAL 4 /* Flags for ImportRec */ -#define SYM_IMPORT 8 -#define SYM_FAR 16 +#define SYM_IMPORT 8 +#define SYM_FAR 16 typedef struct rdf_reloc { yasm_reloc reloc; enum { - RDF_RELOC_NORM, /* normal */ - RDF_RELOC_REL, /* relative to current position */ - RDF_RELOC_SEG /* segment containing symbol */ - } type; /* type of relocation */ + RDF_RELOC_NORM, /* normal */ + RDF_RELOC_REL, /* relative to current position */ + RDF_RELOC_SEG /* segment containing symbol */ + } type; /* type of relocation */ unsigned int size; unsigned int refseg; } rdf_reloc; typedef struct rdf_section_data { - /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ - long scnum; /* section number (0=first section) */ + /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ + long scnum; /* section number (0=first section) */ enum { - RDF_SECT_BSS = 0, - RDF_SECT_CODE = 1, - RDF_SECT_DATA = 2, - RDF_SECT_COMMENT = 3, - RDF_SECT_LCOMMENT = 4, - RDF_SECT_PCOMMENT = 5, - RDF_SECT_SYMDEBUG = 6, - RDF_SECT_LINEDEBUG = 7 - } type; /* section type */ + RDF_SECT_BSS = 0, + RDF_SECT_CODE = 1, + RDF_SECT_DATA = 2, + RDF_SECT_COMMENT = 3, + RDF_SECT_LCOMMENT = 4, + RDF_SECT_PCOMMENT = 5, + RDF_SECT_SYMDEBUG = 6, + RDF_SECT_LINEDEBUG = 7 + } type; /* section type */ unsigned int reserved; /* reserved data */ - unsigned long size; /* size of raw data (section data) in bytes */ + unsigned long size; /* size of raw data (section data) in bytes */ - unsigned char *raw_data; /* raw section data, only used during output */ + unsigned char *raw_data; /* raw section data, only used during output */ } rdf_section_data; typedef struct rdf_symrec_data { - unsigned int segment; /* assigned RDF "segment" index */ + unsigned int segment; /* assigned RDF "segment" index */ } rdf_symrec_data; typedef STAILQ_HEAD(xdf_str_head, xdf_str) xdf_str_head; @@ -110,9 +110,9 @@ typedef struct xdf_str { } xdf_str; typedef struct yasm_objfmt_rdf { - yasm_objfmt_base objfmt; /* base structure */ + yasm_objfmt_base objfmt; /* base structure */ - long parse_scnum; /* sect numbering in parser */ + long parse_scnum; /* sect numbering in parser */ /*@owned@*/ xdf_str_head module_names; /*@owned@*/ xdf_str_head library_names; @@ -127,9 +127,9 @@ typedef struct rdf_objfmt_output_info { yasm_section *sect; /*@dependent@*/ rdf_section_data *rsd; - unsigned long indx; /* symbol "segment" (extern/common only) */ + unsigned long indx; /* symbol "segment" (extern/common only) */ - unsigned long bss_size; /* total BSS size */ + unsigned long bss_size; /* total BSS size */ } rdf_objfmt_output_info; static void rdf_section_data_destroy(/*@only@*/ void *d); @@ -183,8 +183,8 @@ rdf_objfmt_create(yasm_object *object) static int rdf_objfmt_output_value(yasm_value *value, unsigned char *buf, - unsigned int destsize, unsigned long offset, - yasm_bytecode *bc, int warn, /*@null@*/ void *d) + unsigned int destsize, unsigned long offset, + yasm_bytecode *bc, int warn, /*@null@*/ void *d) { /*@null@*/ rdf_objfmt_output_info *info = (rdf_objfmt_output_info *)d; yasm_objfmt_rdf *objfmt_rdf; @@ -198,102 +198,102 @@ rdf_objfmt_output_value(yasm_value *value, unsigned char *buf, objfmt_rdf = info->objfmt_rdf; if (value->abs) - value->abs = yasm_expr_simplify(value->abs, 1); + value->abs = yasm_expr_simplify(value->abs, 1); /* Try to output constant and PC-relative section-local first. * Note this does NOT output any value with a SEG, WRT, external, * cross-section, or non-PC-relative reference (those are handled below). */ switch (yasm_value_output_basic(value, buf, destsize, bc, warn, - info->object->arch)) { - case -1: - return 1; - case 0: - break; - default: - return 0; + info->object->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; } if (value->section_rel) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("rdf: relocation too complex")); - return 1; + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("rdf: relocation too complex")); + return 1; } if (value->rel && value->wrt) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("rdf: WRT not supported")); - return 1; + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("rdf: WRT not supported")); + return 1; } intn_minus = 0; intn_plus = 0; if (value->rel) { - rdf_reloc *reloc; - /*@null@*/ rdf_symrec_data *rsymd; - /*@dependent@*/ yasm_bytecode *precbc; - - reloc = yasm_xmalloc(sizeof(rdf_reloc)); - reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset); - reloc->reloc.sym = value->rel; - reloc->size = valsize/8; - - if (value->seg_of) - reloc->type = RDF_RELOC_SEG; - else if (value->curpos_rel) { - reloc->type = RDF_RELOC_REL; - /* Adjust to start of section, so subtract out the bytecode - * offset. - */ - intn_minus = bc->offset; - } else - reloc->type = RDF_RELOC_NORM; - - if (yasm_symrec_get_label(value->rel, &precbc)) { - /* local, set the value to be the offset, and the refseg to the - * segment number. - */ - /*@dependent@*/ /*@null@*/ rdf_section_data *csectd; - /*@dependent@*/ yasm_section *sect; - - sect = yasm_bc_get_section(precbc); - csectd = yasm_section_get_data(sect, &rdf_section_data_cb); - if (!csectd) - yasm_internal_error(N_("didn't understand section")); - reloc->refseg = csectd->scnum; - intn_plus = yasm_bc_next_offset(precbc); - } else { - /* must be common/external */ - rsymd = yasm_symrec_get_data(reloc->reloc.sym, - &rdf_symrec_data_cb); - if (!rsymd) - yasm_internal_error( - N_("rdf: no symbol data for relocated symbol")); - reloc->refseg = rsymd->segment; - } - - yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); + rdf_reloc *reloc; + /*@null@*/ rdf_symrec_data *rsymd; + /*@dependent@*/ yasm_bytecode *precbc; + + reloc = yasm_xmalloc(sizeof(rdf_reloc)); + reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset); + reloc->reloc.sym = value->rel; + reloc->size = valsize/8; + + if (value->seg_of) + reloc->type = RDF_RELOC_SEG; + else if (value->curpos_rel) { + reloc->type = RDF_RELOC_REL; + /* Adjust to start of section, so subtract out the bytecode + * offset. + */ + intn_minus = bc->offset; + } else + reloc->type = RDF_RELOC_NORM; + + if (yasm_symrec_get_label(value->rel, &precbc)) { + /* local, set the value to be the offset, and the refseg to the + * segment number. + */ + /*@dependent@*/ /*@null@*/ rdf_section_data *csectd; + /*@dependent@*/ yasm_section *sect; + + sect = yasm_bc_get_section(precbc); + csectd = yasm_section_get_data(sect, &rdf_section_data_cb); + if (!csectd) + yasm_internal_error(N_("didn't understand section")); + reloc->refseg = csectd->scnum; + intn_plus = yasm_bc_next_offset(precbc); + } else { + /* must be common/external */ + rsymd = yasm_symrec_get_data(reloc->reloc.sym, + &rdf_symrec_data_cb); + if (!rsymd) + yasm_internal_error( + N_("rdf: no symbol data for relocated symbol")); + reloc->refseg = rsymd->segment; + } + + yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); } if (intn_minus > 0) { - intn = yasm_intnum_create_uint(intn_minus); - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + intn = yasm_intnum_create_uint(intn_minus); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); } else - intn = yasm_intnum_create_uint(intn_plus); + intn = yasm_intnum_create_uint(intn_plus); if (value->abs) { - yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); - if (!intn2) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("rdf: relocation too complex")); - yasm_intnum_destroy(intn); - return 1; - } - yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); + yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); + if (!intn2) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("rdf: relocation too complex")); + yasm_intnum_destroy(intn); + return 1; + } + yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); } retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize, - valsize, 0, bc, warn); + valsize, 0, bc, warn); yasm_intnum_destroy(intn); return retval; } @@ -309,32 +309,32 @@ rdf_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) assert(info != NULL); bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info, - rdf_objfmt_output_value, NULL); + rdf_objfmt_output_value, NULL); /* Don't bother doing anything else if size ended up being 0. */ if (size == 0) { - if (bigbuf) - yasm_xfree(bigbuf); - return 0; + if (bigbuf) + yasm_xfree(bigbuf); + return 0; } /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { - yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, - N_("uninitialized space: zeroing")); - /* Write out in chunks */ - memset(&info->rsd->raw_data[info->rsd->size], 0, size); + yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, + N_("uninitialized space: zeroing")); + /* Write out in chunks */ + memset(&info->rsd->raw_data[info->rsd->size], 0, size); } else { - /* Output buf (or bigbuf if non-NULL) to file */ - memcpy(&info->rsd->raw_data[info->rsd->size], - bigbuf ? bigbuf : info->buf, (size_t)size); + /* Output buf (or bigbuf if non-NULL) to file */ + memcpy(&info->rsd->raw_data[info->rsd->size], + bigbuf ? bigbuf : info->buf, (size_t)size); } info->rsd->size += size; /* If bigbuf was allocated, free it */ if (bigbuf) - yasm_xfree(bigbuf); + yasm_xfree(bigbuf); return 0; } @@ -348,7 +348,7 @@ rdf_objfmt_output_section_mem(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); rsd = yasm_section_get_data(sect, &rdf_section_data_cb); @@ -357,16 +357,16 @@ rdf_objfmt_output_section_mem(yasm_section *sect, /*@null@*/ void *d) size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); if (rsd->type == RDF_SECT_BSS) { - /* Don't output BSS sections, but remember length - * TODO: Check for non-reserve bytecodes? - */ - info->bss_size += size; - return 0; + /* Don't output BSS sections, but remember length + * TODO: Check for non-reserve bytecodes? + */ + info->bss_size += size; + return 0; } /* Empty? Go on to next section */ if (size == 0) - return 0; + return 0; /* See UGH comment in output() for why we're doing this */ rsd->raw_data = yasm_xmalloc(size); @@ -375,12 +375,12 @@ rdf_objfmt_output_section_mem(yasm_section *sect, /*@null@*/ void *d) info->sect = sect; info->rsd = rsd; yasm_section_bcs_traverse(sect, info->errwarns, info, - rdf_objfmt_output_bytecode); + rdf_objfmt_output_bytecode); /* Sanity check final section size */ if (rsd->size != size) - yasm_internal_error( - N_("rdf: section computed size did not match actual size")); + yasm_internal_error( + N_("rdf: section computed size did not match actual size")); return 0; } @@ -394,40 +394,40 @@ rdf_objfmt_output_section_reloc(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); rsd = yasm_section_get_data(sect, &rdf_section_data_cb); assert(rsd != NULL); if (rsd->type == RDF_SECT_BSS) { - /* Don't output BSS sections. */ - return 0; + /* Don't output BSS sections. */ + return 0; } /* Empty? Go on to next section */ if (rsd->size == 0) - return 0; + return 0; reloc = (rdf_reloc *)yasm_section_relocs_first(sect); while (reloc) { - unsigned char *localbuf = info->buf; - - if (reloc->type == RDF_RELOC_SEG) - YASM_WRITE_8(localbuf, RDFREC_SEGRELOC); - else - YASM_WRITE_8(localbuf, RDFREC_RELOC); - YASM_WRITE_8(localbuf, 8); /* record length */ - /* Section number, +0x40 if relative reloc */ - YASM_WRITE_8(localbuf, rsd->scnum + - (reloc->type == RDF_RELOC_REL ? 0x40 : 0)); - yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); - localbuf += 4; /* offset of relocation */ - YASM_WRITE_8(localbuf, reloc->size); /* size of relocation */ - YASM_WRITE_16_L(localbuf, reloc->refseg); /* relocated symbol */ - fwrite(info->buf, 10, 1, info->f); - - reloc = (rdf_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); + unsigned char *localbuf = info->buf; + + if (reloc->type == RDF_RELOC_SEG) + YASM_WRITE_8(localbuf, RDFREC_SEGRELOC); + else + YASM_WRITE_8(localbuf, RDFREC_RELOC); + YASM_WRITE_8(localbuf, 8); /* record length */ + /* Section number, +0x40 if relative reloc */ + YASM_WRITE_8(localbuf, rsd->scnum + + (reloc->type == RDF_RELOC_REL ? 0x40 : 0)); + yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); + localbuf += 4; /* offset of relocation */ + YASM_WRITE_8(localbuf, reloc->size); /* size of relocation */ + YASM_WRITE_16_L(localbuf, reloc->refseg); /* relocated symbol */ + fwrite(info->buf, 10, 1, info->f); + + reloc = (rdf_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); } return 0; @@ -442,27 +442,27 @@ rdf_objfmt_output_section_file(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); rsd = yasm_section_get_data(sect, &rdf_section_data_cb); assert(rsd != NULL); if (rsd->type == RDF_SECT_BSS) { - /* Don't output BSS sections. */ - return 0; + /* Don't output BSS sections. */ + return 0; } /* Empty? Go on to next section */ if (rsd->size == 0) - return 0; + return 0; /* Section header */ localbuf = info->buf; - YASM_WRITE_16_L(localbuf, rsd->type); /* type */ - YASM_WRITE_16_L(localbuf, rsd->scnum); /* number */ - YASM_WRITE_16_L(localbuf, rsd->reserved); /* reserved */ - YASM_WRITE_32_L(localbuf, rsd->size); /* length */ + YASM_WRITE_16_L(localbuf, rsd->type); /* type */ + YASM_WRITE_16_L(localbuf, rsd->scnum); /* number */ + YASM_WRITE_16_L(localbuf, rsd->reserved); /* reserved */ + YASM_WRITE_32_L(localbuf, rsd->size); /* length */ fwrite(info->buf, 10, 1, info->f); /* Section data */ @@ -480,62 +480,62 @@ rdf_parse_flags(yasm_symrec *sym) { yasm_sym_vis vis = yasm_symrec_get_visibility(sym); /*@dependent@*/ /*@null@*/ yasm_valparamhead *objext_valparams = - yasm_symrec_get_objext_valparams(sym); + yasm_symrec_get_objext_valparams(sym); yasm_valparam *vp; unsigned int flags = 0; static const struct { - enum { - FLAG_EXT = 1, - FLAG_GLOB = 2 - } type; - enum { - FLAG_SET = 1, - FLAG_CLR = 2 - } action; - const char *name; - unsigned int flags; + enum { + FLAG_EXT = 1, + FLAG_GLOB = 2 + } type; + enum { + FLAG_SET = 1, + FLAG_CLR = 2 + } action; + const char *name; + unsigned int flags; } flagtbl[] = { - { FLAG_EXT|FLAG_GLOB, FLAG_SET, "data", SYM_DATA }, - { FLAG_EXT|FLAG_GLOB, FLAG_SET, "object", SYM_DATA }, - { FLAG_EXT|FLAG_GLOB, FLAG_SET, "proc", SYM_FUNCTION }, - { FLAG_EXT|FLAG_GLOB, FLAG_SET, "function", SYM_FUNCTION }, - { FLAG_EXT, FLAG_SET, "import", SYM_IMPORT }, - { FLAG_GLOB, FLAG_SET, "export", SYM_GLOBAL }, - { FLAG_EXT, FLAG_SET, "far", SYM_FAR }, - { FLAG_EXT, FLAG_CLR, "near", SYM_FAR }, + { FLAG_EXT|FLAG_GLOB, FLAG_SET, "data", SYM_DATA }, + { FLAG_EXT|FLAG_GLOB, FLAG_SET, "object", SYM_DATA }, + { FLAG_EXT|FLAG_GLOB, FLAG_SET, "proc", SYM_FUNCTION }, + { FLAG_EXT|FLAG_GLOB, FLAG_SET, "function", SYM_FUNCTION }, + { FLAG_EXT, FLAG_SET, "import", SYM_IMPORT }, + { FLAG_GLOB, FLAG_SET, "export", SYM_GLOBAL }, + { FLAG_EXT, FLAG_SET, "far", SYM_FAR }, + { FLAG_EXT, FLAG_CLR, "near", SYM_FAR }, }; if (!objext_valparams) - return 0; + return 0; vp = yasm_vps_first(objext_valparams); for (; vp; vp = yasm_vps_next(vp)) { - size_t i; - int match; - - if (!vp->val) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized numeric qualifier")); - continue; - } - - match = 0; - for (i=0; ival, flagtbl[i].name) == 0) { - if (flagtbl[i].action == FLAG_SET) - flags |= flagtbl[i].flags; - else if (flagtbl[i].action == FLAG_CLR) - flags &= ~flagtbl[i].flags; - match = 1; - } - } - - if (!match) - yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), - vp->val); + size_t i; + int match; + + if (!vp->val) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized numeric qualifier")); + continue; + } + + match = 0; + for (i=0; ival, flagtbl[i].name) == 0) { + if (flagtbl[i].action == FLAG_SET) + flags |= flagtbl[i].flags; + else if (flagtbl[i].action == FLAG_CLR) + flags &= ~flagtbl[i].flags; + match = 1; + } + } + + if (!match) + yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), + vp->val); } return flags; @@ -557,127 +557,127 @@ rdf_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (vis == YASM_SYM_LOCAL || vis == YASM_SYM_DLOCAL) - return 0; /* skip local syms */ + return 0; /* skip local syms */ /* Look at symrec for value/scnum/etc. */ if (yasm_symrec_get_label(sym, &precbc)) { - /*@dependent@*/ /*@null@*/ rdf_section_data *csectd; - - if (precbc) - sect = yasm_bc_get_section(precbc); - else - sect = NULL; - if (!sect) - return 0; - - /* it's a label: get value and offset. */ - csectd = yasm_section_get_data(sect, &rdf_section_data_cb); - if (csectd) { - scnum = csectd->scnum; - } else if (yasm_section_is_absolute(sect)) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("rdf does not support exporting absolutes")); - yasm_errwarn_propagate(info->errwarns, - yasm_symrec_get_decl_line(sym)); - return 0; - } else - yasm_internal_error(N_("didn't understand section")); - value = yasm_bc_next_offset(precbc); + /*@dependent@*/ /*@null@*/ rdf_section_data *csectd; + + if (precbc) + sect = yasm_bc_get_section(precbc); + else + sect = NULL; + if (!sect) + return 0; + + /* it's a label: get value and offset. */ + csectd = yasm_section_get_data(sect, &rdf_section_data_cb); + if (csectd) { + scnum = csectd->scnum; + } else if (yasm_section_is_absolute(sect)) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("rdf does not support exporting absolutes")); + yasm_errwarn_propagate(info->errwarns, + yasm_symrec_get_decl_line(sym)); + return 0; + } else + yasm_internal_error(N_("didn't understand section")); + value = yasm_bc_next_offset(precbc); } else if (yasm_symrec_get_equ(sym)) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("rdf does not support exporting EQU/absolute values")); - yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); - return 0; + yasm_warn_set(YASM_WARN_GENERAL, + N_("rdf does not support exporting EQU/absolute values")); + yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); + return 0; } name = yasm_symrec_get_name(sym); len = strlen(name); if (len > EXIM_LABEL_MAX-1) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("label name too long, truncating to %d bytes"), - EXIM_LABEL_MAX); - len = EXIM_LABEL_MAX-1; + yasm_warn_set(YASM_WARN_GENERAL, + N_("label name too long, truncating to %d bytes"), + EXIM_LABEL_MAX); + len = EXIM_LABEL_MAX-1; } localbuf = info->buf; if (vis & YASM_SYM_GLOBAL) { - YASM_WRITE_8(localbuf, RDFREC_GLOBAL); - YASM_WRITE_8(localbuf, 6+len+1); /* record length */ - YASM_WRITE_8(localbuf, rdf_parse_flags(sym)); /* flags */ - YASM_WRITE_8(localbuf, scnum); /* segment referred to */ - YASM_WRITE_32_L(localbuf, value); /* offset */ + YASM_WRITE_8(localbuf, RDFREC_GLOBAL); + YASM_WRITE_8(localbuf, 6+len+1); /* record length */ + YASM_WRITE_8(localbuf, rdf_parse_flags(sym)); /* flags */ + YASM_WRITE_8(localbuf, scnum); /* segment referred to */ + YASM_WRITE_32_L(localbuf, value); /* offset */ } else { - /* Save symbol segment in symrec data (for later reloc gen) */ - scnum = info->indx++; - rdf_objfmt_sym_set_data(sym, scnum); - - if (vis & YASM_SYM_COMMON) { - /*@dependent@*/ /*@null@*/ yasm_expr **csize_expr; - const yasm_intnum *intn; - /*@dependent@*/ /*@null@*/ yasm_valparamhead *objext_valparams = - yasm_symrec_get_objext_valparams(sym); - unsigned long addralign = 0; - - YASM_WRITE_8(localbuf, RDFREC_COMMON); - YASM_WRITE_8(localbuf, 8+len+1); /* record length */ - YASM_WRITE_16_L(localbuf, scnum); /* segment allocated */ - - /* size */ - csize_expr = yasm_symrec_get_common_size(sym); - assert(csize_expr != NULL); - intn = yasm_expr_get_intnum(csize_expr, 1); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("COMMON data size not an integer expression")); - } else - value = yasm_intnum_get_uint(intn); - YASM_WRITE_32_L(localbuf, value); - - /* alignment */ - if (objext_valparams) { - yasm_valparam *vp = yasm_vps_first(objext_valparams); - for (; vp; vp = yasm_vps_next(vp)) { - if (!vp->val && vp->param) { - /*@null@*/ const yasm_intnum *align_expr; - - align_expr = yasm_expr_get_intnum(&vp->param, 0); - if (!align_expr) { - yasm_error_set(YASM_ERROR_VALUE, - N_("alignment constraint is not an integer")); - continue; - } - addralign = yasm_intnum_get_uint(align_expr); - - /* Alignments must be a power of two. */ - if (!is_exp2(addralign)) { - yasm_error_set(YASM_ERROR_VALUE, - N_("alignment constraint is not a power of two")); - continue; - } - } else if (vp->val) - yasm_warn_set(YASM_WARN_GENERAL, + /* Save symbol segment in symrec data (for later reloc gen) */ + scnum = info->indx++; + rdf_objfmt_sym_set_data(sym, scnum); + + if (vis & YASM_SYM_COMMON) { + /*@dependent@*/ /*@null@*/ yasm_expr **csize_expr; + const yasm_intnum *intn; + /*@dependent@*/ /*@null@*/ yasm_valparamhead *objext_valparams = + yasm_symrec_get_objext_valparams(sym); + unsigned long addralign = 0; + + YASM_WRITE_8(localbuf, RDFREC_COMMON); + YASM_WRITE_8(localbuf, 8+len+1); /* record length */ + YASM_WRITE_16_L(localbuf, scnum); /* segment allocated */ + + /* size */ + csize_expr = yasm_symrec_get_common_size(sym); + assert(csize_expr != NULL); + intn = yasm_expr_get_intnum(csize_expr, 1); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("COMMON data size not an integer expression")); + } else + value = yasm_intnum_get_uint(intn); + YASM_WRITE_32_L(localbuf, value); + + /* alignment */ + if (objext_valparams) { + yasm_valparam *vp = yasm_vps_first(objext_valparams); + for (; vp; vp = yasm_vps_next(vp)) { + if (!vp->val && vp->param) { + /*@null@*/ const yasm_intnum *align_expr; + + align_expr = yasm_expr_get_intnum(&vp->param, 0); + if (!align_expr) { + yasm_error_set(YASM_ERROR_VALUE, + N_("alignment constraint is not an integer")); + continue; + } + addralign = yasm_intnum_get_uint(align_expr); + + /* Alignments must be a power of two. */ + if (!is_exp2(addralign)) { + yasm_error_set(YASM_ERROR_VALUE, + N_("alignment constraint is not a power of two")); + continue; + } + } else if (vp->val) + yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), vp->val); - } - } - YASM_WRITE_16_L(localbuf, addralign); - } else if (vis & YASM_SYM_EXTERN) { - unsigned int flags = rdf_parse_flags(sym); - if (flags & SYM_FAR) { - YASM_WRITE_8(localbuf, RDFREC_FARIMPORT); - flags &= ~SYM_FAR; - } else - YASM_WRITE_8(localbuf, RDFREC_IMPORT); - YASM_WRITE_8(localbuf, 3+len+1); /* record length */ - YASM_WRITE_8(localbuf, flags); /* flags */ - YASM_WRITE_16_L(localbuf, scnum); /* segment allocated */ - } + } + } + YASM_WRITE_16_L(localbuf, addralign); + } else if (vis & YASM_SYM_EXTERN) { + unsigned int flags = rdf_parse_flags(sym); + if (flags & SYM_FAR) { + YASM_WRITE_8(localbuf, RDFREC_FARIMPORT); + flags &= ~SYM_FAR; + } else + YASM_WRITE_8(localbuf, RDFREC_IMPORT); + YASM_WRITE_8(localbuf, 3+len+1); /* record length */ + YASM_WRITE_8(localbuf, flags); /* flags */ + YASM_WRITE_16_L(localbuf, scnum); /* segment allocated */ + } } /* Symbol name */ memcpy(localbuf, name, len); localbuf += len; - YASM_WRITE_8(localbuf, 0); /* 0-terminated name */ + YASM_WRITE_8(localbuf, 0); /* 0-terminated name */ fwrite(info->buf, (unsigned long)(localbuf-info->buf), 1, info->f); @@ -687,7 +687,7 @@ rdf_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d) static void rdf_objfmt_output(yasm_object *object, FILE *f, int all_syms, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_objfmt_rdf *objfmt_rdf = (yasm_objfmt_rdf *)object->objfmt; rdf_objfmt_output_info info; @@ -705,32 +705,32 @@ rdf_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Allocate space for file header by seeking forward */ if (fseek(f, (long)strlen(RDF_MAGIC)+8, SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } /* Output custom header records (library and module, etc) */ cur = STAILQ_FIRST(&objfmt_rdf->module_names); while (cur) { - len = strlen(cur->str)+1; - localbuf = info.buf; - YASM_WRITE_8(localbuf, RDFREC_MODNAME); /* record type */ - YASM_WRITE_8(localbuf, len); /* record length */ - fwrite(info.buf, 2, 1, f); - fwrite(cur->str, len, 1, f); - cur = STAILQ_NEXT(cur, link); + len = strlen(cur->str)+1; + localbuf = info.buf; + YASM_WRITE_8(localbuf, RDFREC_MODNAME); /* record type */ + YASM_WRITE_8(localbuf, len); /* record length */ + fwrite(info.buf, 2, 1, f); + fwrite(cur->str, len, 1, f); + cur = STAILQ_NEXT(cur, link); } cur = STAILQ_FIRST(&objfmt_rdf->library_names); while (cur) { - len = strlen(cur->str)+1; - localbuf = info.buf; - YASM_WRITE_8(localbuf, RDFREC_DLL); /* record type */ - YASM_WRITE_8(localbuf, len); /* record length */ - fwrite(info.buf, 2, 1, f); - fwrite(cur->str, len, 1, f); - cur = STAILQ_NEXT(cur, link); + len = strlen(cur->str)+1; + localbuf = info.buf; + YASM_WRITE_8(localbuf, RDFREC_DLL); /* record type */ + YASM_WRITE_8(localbuf, len); /* record length */ + fwrite(info.buf, 2, 1, f); + fwrite(cur->str, len, 1, f); + cur = STAILQ_NEXT(cur, link); } /* Output symbol table */ @@ -750,35 +750,35 @@ rdf_objfmt_output(yasm_object *object, FILE *f, int all_syms, * We also calculate the total size of all BSS sections here. */ if (yasm_object_sections_traverse(object, &info, - rdf_objfmt_output_section_mem)) - return; + rdf_objfmt_output_section_mem)) + return; /* Output all relocs */ if (yasm_object_sections_traverse(object, &info, - rdf_objfmt_output_section_reloc)) - return; + rdf_objfmt_output_section_reloc)) + return; /* Output BSS record */ if (info.bss_size > 0) { - localbuf = info.buf; - YASM_WRITE_8(localbuf, RDFREC_BSS); /* record type */ - YASM_WRITE_8(localbuf, 4); /* record length */ - YASM_WRITE_32_L(localbuf, info.bss_size); /* total BSS size */ - fwrite(info.buf, 6, 1, f); + localbuf = info.buf; + YASM_WRITE_8(localbuf, RDFREC_BSS); /* record type */ + YASM_WRITE_8(localbuf, 4); /* record length */ + YASM_WRITE_32_L(localbuf, info.bss_size); /* total BSS size */ + fwrite(info.buf, 6, 1, f); } /* Determine header length */ headerlen = ftell(f); if (headerlen == -1) { - yasm__fatal(N_("could not get file position on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not get file position on output file")); + /*@notreached@*/ + return; } /* Section data (to file) */ if (yasm_object_sections_traverse(object, &info, - rdf_objfmt_output_section_file)) - return; + rdf_objfmt_output_section_file)) + return; /* NULL section to end file */ memset(info.buf, 0, 10); @@ -787,22 +787,22 @@ rdf_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Determine object length */ filelen = ftell(f); if (filelen == -1) { - yasm__fatal(N_("could not get file position on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not get file position on output file")); + /*@notreached@*/ + return; } /* Write file header */ if (fseek(f, 0, SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } fwrite(RDF_MAGIC, strlen(RDF_MAGIC), 1, f); localbuf = info.buf; - YASM_WRITE_32_L(localbuf, filelen-10); /* object size */ - YASM_WRITE_32_L(localbuf, headerlen-14); /* header size */ + YASM_WRITE_32_L(localbuf, filelen-10); /* object size */ + YASM_WRITE_32_L(localbuf, headerlen-14); /* header size */ fwrite(info.buf, 8, 1, f); yasm_xfree(info.buf); @@ -816,18 +816,18 @@ rdf_objfmt_destroy(yasm_objfmt *objfmt) cur = STAILQ_FIRST(&objfmt_rdf->module_names); while (cur) { - next = STAILQ_NEXT(cur, link); - yasm_xfree(cur->str); - yasm_xfree(cur); - cur = next; + next = STAILQ_NEXT(cur, link); + yasm_xfree(cur->str); + yasm_xfree(cur); + cur = next; } cur = STAILQ_FIRST(&objfmt_rdf->library_names); while (cur) { - next = STAILQ_NEXT(cur, link); - yasm_xfree(cur->str); - yasm_xfree(cur); - cur = next; + next = STAILQ_NEXT(cur, link); + yasm_xfree(cur->str); + yasm_xfree(cur); + cur = next; } yasm_xfree(objfmt); @@ -835,7 +835,7 @@ rdf_objfmt_destroy(yasm_objfmt *objfmt) static rdf_section_data * rdf_objfmt_init_new_section(yasm_object *object, yasm_section *sect, - const char *sectname, unsigned long line) + const char *sectname, unsigned long line) { yasm_objfmt_rdf *objfmt_rdf = (yasm_objfmt_rdf *)object->objfmt; rdf_section_data *data; @@ -850,7 +850,7 @@ rdf_objfmt_init_new_section(yasm_object *object, yasm_section *sect, yasm_section_add_data(sect, &rdf_section_data_cb, data); sym = yasm_symtab_define_label(object->symtab, sectname, - yasm_section_bcs_first(sect), 1, line); + yasm_section_bcs_first(sect), 1, line); data->sym = sym; return data; } @@ -864,19 +864,19 @@ rdf_objfmt_add_default_section(yasm_object *object) retval = yasm_object_get_general(object, ".text", 0, 0, 1, 0, &isnew, 0); if (isnew) { - rsd = rdf_objfmt_init_new_section(object, retval, ".text", 0); - rsd->type = RDF_SECT_CODE; - rsd->reserved = 0; - yasm_section_set_default(retval, 1); + rsd = rdf_objfmt_init_new_section(object, retval, ".text", 0); + rsd->type = RDF_SECT_CODE; + rsd->reserved = 0; + yasm_section_set_default(retval, 1); } return retval; } static /*@observer@*/ /*@null@*/ yasm_section * rdf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, - unsigned long line) + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_section *retval; @@ -888,58 +888,58 @@ rdf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, rdf_section_data *rsd; static const struct { - const char *name; - unsigned int type; + const char *name; + unsigned int type; } typenames[] = { - { "bss", RDF_SECT_BSS }, - { "code", RDF_SECT_CODE }, - { "text", RDF_SECT_CODE }, - { "data", RDF_SECT_DATA }, - { "comment", RDF_SECT_COMMENT }, - { "lcomment", RDF_SECT_LCOMMENT }, - { "pcomment", RDF_SECT_PCOMMENT }, - { "symdebug", RDF_SECT_SYMDEBUG }, - { "linedebug", RDF_SECT_LINEDEBUG }, + { "bss", RDF_SECT_BSS }, + { "code", RDF_SECT_CODE }, + { "text", RDF_SECT_CODE }, + { "data", RDF_SECT_DATA }, + { "comment", RDF_SECT_COMMENT }, + { "lcomment", RDF_SECT_LCOMMENT }, + { "pcomment", RDF_SECT_PCOMMENT }, + { "symdebug", RDF_SECT_SYMDEBUG }, + { "linedebug", RDF_SECT_LINEDEBUG }, }; if (!vp || vp->param || !vp->val) - return NULL; + return NULL; sectname = vp->val; if (strcmp(sectname, ".text") == 0) - type = RDF_SECT_CODE; + type = RDF_SECT_CODE; else if (strcmp(sectname, ".data") == 0) - type = RDF_SECT_DATA; + type = RDF_SECT_DATA; else if (strcmp(sectname, ".bss") == 0) - type = RDF_SECT_BSS; + type = RDF_SECT_BSS; /* Look for section type */ if ((vp = yasm_vps_next(vp))) { - size_t i; - int match; - if (vp->val) { - match = 0; - for (i=0; ival, typenames[i].name) == 0) { - type = typenames[i].type; - flags_override = 1; - match = 1; - } - } - if (!match) - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized RDF segment type `%s'"), - vp->val); - } else - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized numeric qualifier")); + size_t i; + int match; + if (vp->val) { + match = 0; + for (i=0; ival, typenames[i].name) == 0) { + type = typenames[i].type; + flags_override = 1; + match = 1; + } + } + if (!match) + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized RDF segment type `%s'"), + vp->val); + } else + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized numeric qualifier")); } if (type == 0xffff) { - yasm_error_set(YASM_ERROR_VALUE, - N_("new segment declared without type code")); - type = RDF_SECT_DATA; + yasm_error_set(YASM_ERROR_VALUE, + N_("new segment declared without type code")); + type = RDF_SECT_DATA; } /* Look for reserved value */ @@ -950,29 +950,29 @@ rdf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, reserved_expr = yasm_expr_get_intnum(&vp->param, 0); if (!reserved_expr) yasm_error_set(YASM_ERROR_VALUE, - N_("reserved value must be numeric")); + N_("reserved value must be numeric")); else reserved = yasm_intnum_get_uint(reserved_expr); - } else if (vp->val) + } else if (vp->val) yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), - vp->val); + vp->val); } retval = yasm_object_get_general(object, sectname, 0, 0, 1, - type == RDF_SECT_BSS, &isnew, line); + type == RDF_SECT_BSS, &isnew, line); if (isnew) - rsd = rdf_objfmt_init_new_section(object, retval, sectname, line); + rsd = rdf_objfmt_init_new_section(object, retval, sectname, line); else - rsd = yasm_section_get_data(retval, &rdf_section_data_cb); + rsd = yasm_section_get_data(retval, &rdf_section_data_cb); if (isnew || yasm_section_is_default(retval)) { - yasm_section_set_default(retval, 0); - rsd->type = type; - rsd->reserved = reserved; + yasm_section_set_default(retval, 0); + rsd->type = type; + rsd->reserved = reserved; } else if (flags_override) - yasm_warn_set(YASM_WARN_GENERAL, - N_("section flags ignored on section redeclaration")); + yasm_warn_set(YASM_WARN_GENERAL, + N_("section flags ignored on section redeclaration")); return retval; } @@ -981,7 +981,7 @@ rdf_section_data_destroy(void *data) { rdf_section_data *rsd = (rdf_section_data *)data; if (rsd->raw_data) - yasm_xfree(rsd->raw_data); + yasm_xfree(rsd->raw_data); yasm_xfree(data); } @@ -1022,34 +1022,34 @@ rdf_objfmt_add_libmodule(yasm_object *object, char *name, int lib) str = yasm_xmalloc(sizeof(xdf_str)); str->str = name; if (lib) - STAILQ_INSERT_TAIL(&objfmt_rdf->library_names, str, link); + STAILQ_INSERT_TAIL(&objfmt_rdf->library_names, str, link); else - STAILQ_INSERT_TAIL(&objfmt_rdf->module_names, str, link); + STAILQ_INSERT_TAIL(&objfmt_rdf->module_names, str, link); if (strlen(str->str) > MODLIB_NAME_MAX-1) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("name too long, truncating to %d bytes"), - MODLIB_NAME_MAX); - str->str[MODLIB_NAME_MAX-1] = '\0'; + yasm_warn_set(YASM_WARN_GENERAL, + N_("name too long, truncating to %d bytes"), + MODLIB_NAME_MAX); + str->str[MODLIB_NAME_MAX-1] = '\0'; } } static void dir_library(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); rdf_objfmt_add_libmodule(object, vp->val, 1); - vp->val = NULL; /* don't free it */ + vp->val = NULL; /* don't free it */ } static void dir_module(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); rdf_objfmt_add_libmodule(object, vp->val, 0); - vp->val = NULL; /* don't free it */ + vp->val = NULL; /* don't free it */ } /* Define valid debug formats to use with this object format */ @@ -1059,8 +1059,8 @@ static const char *rdf_objfmt_dbgfmt_keywords[] = { }; static const yasm_directive rdf_objfmt_directives[] = { - { "library", "nasm", dir_library, YASM_DIR_ID_REQUIRED }, - { "module", "nasm", dir_module, YASM_DIR_ID_REQUIRED }, + { "library", "nasm", dir_library, YASM_DIR_ID_REQUIRED }, + { "module", "nasm", dir_module, YASM_DIR_ID_REQUIRED }, { NULL, NULL, NULL, 0 } }; diff --git a/modules/objfmts/win32/tests/win32test.c b/modules/objfmts/win32/tests/win32test.c index f7446b8a..fa6a552d 100644 --- a/modules/objfmts/win32/tests/win32test.c +++ b/modules/objfmts/win32/tests/win32test.c @@ -28,7 +28,7 @@ int main(void) { greet(); printf("These pointers should be equal: %p and %p\n", - &greet, textptr); + &greet, textptr); printf("So should these: %p and %p\n", selfptr, &selfptr); } diff --git a/modules/objfmts/xdf/xdf-objfmt.c b/modules/objfmts/xdf/xdf-objfmt.c index 48bcae38..bd07077d 100644 --- a/modules/objfmts/xdf/xdf-objfmt.c +++ b/modules/objfmts/xdf/xdf-objfmt.c @@ -33,60 +33,60 @@ #include -#define REGULAR_OUTBUF_SIZE 1024 +#define REGULAR_OUTBUF_SIZE 1024 -#define XDF_MAGIC 0x87654322 +#define XDF_MAGIC 0x87654322 -#define XDF_SYM_EXTERN 1 -#define XDF_SYM_GLOBAL 2 -#define XDF_SYM_EQU 4 +#define XDF_SYM_EXTERN 1 +#define XDF_SYM_GLOBAL 2 +#define XDF_SYM_EQU 4 typedef struct xdf_reloc { yasm_reloc reloc; /*@null@*/ yasm_symrec *base; /* base symbol (for WRT) */ enum { - XDF_RELOC_REL = 1, /* relative to segment */ - XDF_RELOC_WRT = 2, /* relative to symbol */ - XDF_RELOC_RIP = 4, /* RIP-relative */ - XDF_RELOC_SEG = 8 /* segment containing symbol */ - } type; /* type of relocation */ + XDF_RELOC_REL = 1, /* relative to segment */ + XDF_RELOC_WRT = 2, /* relative to symbol */ + XDF_RELOC_RIP = 4, /* RIP-relative */ + XDF_RELOC_SEG = 8 /* segment containing symbol */ + } type; /* type of relocation */ enum { - XDF_RELOC_8 = 1, - XDF_RELOC_16 = 2, - XDF_RELOC_32 = 4, - XDF_RELOC_64 = 8 - } size; /* size of relocation */ - unsigned int shift; /* relocation shift (0,4,8,16,24,32) */ + XDF_RELOC_8 = 1, + XDF_RELOC_16 = 2, + XDF_RELOC_32 = 4, + XDF_RELOC_64 = 8 + } size; /* size of relocation */ + unsigned int shift; /* relocation shift (0,4,8,16,24,32) */ } xdf_reloc; typedef struct xdf_section_data { - /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ - yasm_intnum *addr; /* starting memory address */ - yasm_intnum *vaddr; /* starting virtual address */ - long scnum; /* section number (0=first section) */ + /*@dependent@*/ yasm_symrec *sym; /* symbol created for this section */ + yasm_intnum *addr; /* starting memory address */ + yasm_intnum *vaddr; /* starting virtual address */ + long scnum; /* section number (0=first section) */ enum { - XDF_SECT_ABSOLUTE = 0x01, - XDF_SECT_FLAT = 0x02, - XDF_SECT_BSS = 0x04, - XDF_SECT_EQU = 0x08, - XDF_SECT_USE_16 = 0x10, - XDF_SECT_USE_32 = 0x20, - XDF_SECT_USE_64 = 0x40 - } flags; /* section flags */ + XDF_SECT_ABSOLUTE = 0x01, + XDF_SECT_FLAT = 0x02, + XDF_SECT_BSS = 0x04, + XDF_SECT_EQU = 0x08, + XDF_SECT_USE_16 = 0x10, + XDF_SECT_USE_32 = 0x20, + XDF_SECT_USE_64 = 0x40 + } flags; /* section flags */ unsigned long scnptr; /* file ptr to raw data */ - unsigned long size; /* size of raw data (section data) in bytes */ + unsigned long size; /* size of raw data (section data) in bytes */ unsigned long relptr; /* file ptr to relocation */ unsigned long nreloc; /* number of relocation entries >64k -> error */ } xdf_section_data; typedef struct xdf_symrec_data { - unsigned long index; /* assigned XDF symbol table index */ + unsigned long index; /* assigned XDF symbol table index */ } xdf_symrec_data; typedef struct yasm_objfmt_xdf { - yasm_objfmt_base objfmt; /* base structure */ + yasm_objfmt_base objfmt; /* base structure */ - long parse_scnum; /* sect numbering in parser */ + long parse_scnum; /* sect numbering in parser */ } yasm_objfmt_xdf; typedef struct xdf_objfmt_output_info { @@ -98,8 +98,8 @@ typedef struct xdf_objfmt_output_info { yasm_section *sect; /*@dependent@*/ xdf_section_data *xsd; - unsigned long indx; /* current symbol index */ - int all_syms; /* outputting all symbols? */ + unsigned long indx; /* current symbol index */ + int all_syms; /* outputting all symbols? */ unsigned long strtab_offset; /* current string table offset */ } xdf_objfmt_output_info; @@ -129,15 +129,15 @@ xdf_objfmt_create(yasm_object *object) /* Only support x86 arch */ if (yasm__strcasecmp(yasm_arch_keyword(object->arch), "x86") != 0) { - yasm_xfree(objfmt_xdf); - return NULL; + yasm_xfree(objfmt_xdf); + return NULL; } /* Support x86 and amd64 machines of x86 arch */ if (yasm__strcasecmp(yasm_arch_get_machine(object->arch), "x86") && - yasm__strcasecmp(yasm_arch_get_machine(object->arch), "amd64")) { - yasm_xfree(objfmt_xdf); - return NULL; + yasm__strcasecmp(yasm_arch_get_machine(object->arch), "amd64")) { + yasm_xfree(objfmt_xdf); + return NULL; } objfmt_xdf->parse_scnum = 0; /* section numbering starts at 0 */ @@ -149,8 +149,8 @@ xdf_objfmt_create(yasm_object *object) static int xdf_objfmt_output_value(yasm_value *value, unsigned char *buf, - unsigned int destsize, unsigned long offset, - yasm_bytecode *bc, int warn, /*@null@*/ void *d) + unsigned int destsize, unsigned long offset, + yasm_bytecode *bc, int warn, /*@null@*/ void *d) { /*@null@*/ xdf_objfmt_output_info *info = (xdf_objfmt_output_info *)d; yasm_objfmt_xdf *objfmt_xdf; @@ -163,75 +163,75 @@ xdf_objfmt_output_value(yasm_value *value, unsigned char *buf, objfmt_xdf = info->objfmt_xdf; if (value->abs) - value->abs = yasm_expr_simplify(value->abs, 1); + value->abs = yasm_expr_simplify(value->abs, 1); /* Try to output constant and PC-relative section-local first. * Note this does NOT output any value with a SEG, WRT, external, * cross-section, or non-PC-relative reference (those are handled below). */ switch (yasm_value_output_basic(value, buf, destsize, bc, warn, - info->object->arch)) { - case -1: - return 1; - case 0: - break; - default: - return 0; + info->object->arch)) { + case -1: + return 1; + case 0: + break; + default: + return 0; } if (value->section_rel) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("xdf: relocation too complex")); - return 1; + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("xdf: relocation too complex")); + return 1; } intn_minus = 0; if (value->rel) { - xdf_reloc *reloc; - - reloc = yasm_xmalloc(sizeof(xdf_reloc)); - reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset); - reloc->reloc.sym = value->rel; - reloc->base = NULL; - reloc->size = valsize/8; - reloc->shift = value->rshift; - - if (value->seg_of) - reloc->type = XDF_RELOC_SEG; - else if (value->wrt) { - reloc->base = value->wrt; - reloc->type = XDF_RELOC_WRT; - } else if (value->curpos_rel) { - reloc->type = XDF_RELOC_RIP; - /* Adjust to start of section, so subtract out the bytecode - * offset. - */ - intn_minus = bc->offset; - } else - reloc->type = XDF_RELOC_REL; - info->xsd->nreloc++; - yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); + xdf_reloc *reloc; + + reloc = yasm_xmalloc(sizeof(xdf_reloc)); + reloc->reloc.addr = yasm_intnum_create_uint(bc->offset + offset); + reloc->reloc.sym = value->rel; + reloc->base = NULL; + reloc->size = valsize/8; + reloc->shift = value->rshift; + + if (value->seg_of) + reloc->type = XDF_RELOC_SEG; + else if (value->wrt) { + reloc->base = value->wrt; + reloc->type = XDF_RELOC_WRT; + } else if (value->curpos_rel) { + reloc->type = XDF_RELOC_RIP; + /* Adjust to start of section, so subtract out the bytecode + * offset. + */ + intn_minus = bc->offset; + } else + reloc->type = XDF_RELOC_REL; + info->xsd->nreloc++; + yasm_section_add_reloc(info->sect, (yasm_reloc *)reloc, yasm_xfree); } if (intn_minus > 0) { - intn = yasm_intnum_create_uint(intn_minus); - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + intn = yasm_intnum_create_uint(intn_minus); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); } else - intn = yasm_intnum_create_uint(0); + intn = yasm_intnum_create_uint(0); if (value->abs) { - yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); - if (!intn2) { - yasm_error_set(YASM_ERROR_TOO_COMPLEX, - N_("xdf: relocation too complex")); - yasm_intnum_destroy(intn); - return 1; - } - yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); + yasm_intnum *intn2 = yasm_expr_get_intnum(&value->abs, 0); + if (!intn2) { + yasm_error_set(YASM_ERROR_TOO_COMPLEX, + N_("xdf: relocation too complex")); + yasm_intnum_destroy(intn); + return 1; + } + yasm_intnum_calc(intn, YASM_EXPR_ADD, intn2); } retval = yasm_arch_intnum_tobytes(info->object->arch, intn, buf, destsize, - valsize, 0, bc, warn); + valsize, 0, bc, warn); yasm_intnum_destroy(intn); return retval; } @@ -247,38 +247,38 @@ xdf_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d) assert(info != NULL); bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info, - xdf_objfmt_output_value, NULL); + xdf_objfmt_output_value, NULL); /* Don't bother doing anything else if size ended up being 0. */ if (size == 0) { - if (bigbuf) - yasm_xfree(bigbuf); - return 0; + if (bigbuf) + yasm_xfree(bigbuf); + return 0; } info->xsd->size += size; /* Warn that gaps are converted to 0 and write out the 0's. */ if (gap) { - unsigned long left; - yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, - N_("uninitialized space: zeroing")); - /* Write out in chunks */ - memset(info->buf, 0, REGULAR_OUTBUF_SIZE); - left = size; - while (left > REGULAR_OUTBUF_SIZE) { - fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); - left -= REGULAR_OUTBUF_SIZE; - } - fwrite(info->buf, left, 1, info->f); + unsigned long left; + yasm_warn_set(YASM_WARN_UNINIT_CONTENTS, + N_("uninitialized space: zeroing")); + /* Write out in chunks */ + memset(info->buf, 0, REGULAR_OUTBUF_SIZE); + left = size; + while (left > REGULAR_OUTBUF_SIZE) { + fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f); + left -= REGULAR_OUTBUF_SIZE; + } + fwrite(info->buf, left, 1, info->f); } else { - /* Output buf (or bigbuf if non-NULL) to file */ - fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f); + /* Output buf (or bigbuf if non-NULL) to file */ + fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f); } /* If bigbuf was allocated, free it */ if (bigbuf) - yasm_xfree(bigbuf); + yasm_xfree(bigbuf); return 0; } @@ -293,87 +293,87 @@ xdf_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d) /* FIXME: Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); xsd = yasm_section_get_data(sect, &xdf_section_data_cb); assert(xsd != NULL); if (xsd->flags & XDF_SECT_BSS) { - /* Don't output BSS sections. - * TODO: Check for non-reserve bytecodes? - */ - pos = 0; /* position = 0 because it's not in the file */ - xsd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); + /* Don't output BSS sections. + * TODO: Check for non-reserve bytecodes? + */ + pos = 0; /* position = 0 because it's not in the file */ + xsd->size = yasm_bc_next_offset(yasm_section_bcs_last(sect)); } else { - pos = ftell(info->f); - if (pos == -1) { - yasm__fatal(N_("could not get file position on output file")); - /*@notreached@*/ - return 1; - } - - info->sect = sect; - info->xsd = xsd; - yasm_section_bcs_traverse(sect, info->errwarns, info, - xdf_objfmt_output_bytecode); - - /* Sanity check final section size */ - if (xsd->size != yasm_bc_next_offset(yasm_section_bcs_last(sect))) - yasm_internal_error( - N_("xdf: section computed size did not match actual size")); + pos = ftell(info->f); + if (pos == -1) { + yasm__fatal(N_("could not get file position on output file")); + /*@notreached@*/ + return 1; + } + + info->sect = sect; + info->xsd = xsd; + yasm_section_bcs_traverse(sect, info->errwarns, info, + xdf_objfmt_output_bytecode); + + /* Sanity check final section size */ + if (xsd->size != yasm_bc_next_offset(yasm_section_bcs_last(sect))) + yasm_internal_error( + N_("xdf: section computed size did not match actual size")); } /* Empty? Go on to next section */ if (xsd->size == 0) - return 0; + return 0; xsd->scnptr = (unsigned long)pos; /* No relocations to output? Go on to next section */ if (xsd->nreloc == 0) - return 0; + return 0; pos = ftell(info->f); if (pos == -1) { - yasm__fatal(N_("could not get file position on output file")); - /*@notreached@*/ - return 1; + yasm__fatal(N_("could not get file position on output file")); + /*@notreached@*/ + return 1; } xsd->relptr = (unsigned long)pos; reloc = (xdf_reloc *)yasm_section_relocs_first(sect); while (reloc) { - unsigned char *localbuf = info->buf; - /*@null@*/ xdf_symrec_data *xsymd; - - xsymd = yasm_symrec_get_data(reloc->reloc.sym, &xdf_symrec_data_cb); - if (!xsymd) - yasm_internal_error( - N_("xdf: no symbol data for relocated symbol")); - - yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); - localbuf += 4; /* address of relocation */ - YASM_WRITE_32_L(localbuf, xsymd->index); /* relocated symbol */ - if (reloc->base) { - xsymd = yasm_symrec_get_data(reloc->base, &xdf_symrec_data_cb); - if (!xsymd) - yasm_internal_error( - N_("xdf: no symbol data for relocated base symbol")); - YASM_WRITE_32_L(localbuf, xsymd->index); /* base symbol */ - } else { - if (reloc->type == XDF_RELOC_WRT) - yasm_internal_error( - N_("xdf: no base symbol for WRT relocation")); - YASM_WRITE_32_L(localbuf, 0); /* no base symbol */ - } - YASM_WRITE_8(localbuf, reloc->type); /* type of relocation */ - YASM_WRITE_8(localbuf, reloc->size); /* size of relocation */ - YASM_WRITE_8(localbuf, reloc->shift); /* relocation shift */ - YASM_WRITE_8(localbuf, 0); /* flags */ - fwrite(info->buf, 16, 1, info->f); - - reloc = (xdf_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); + unsigned char *localbuf = info->buf; + /*@null@*/ xdf_symrec_data *xsymd; + + xsymd = yasm_symrec_get_data(reloc->reloc.sym, &xdf_symrec_data_cb); + if (!xsymd) + yasm_internal_error( + N_("xdf: no symbol data for relocated symbol")); + + yasm_intnum_get_sized(reloc->reloc.addr, localbuf, 4, 32, 0, 0, 0); + localbuf += 4; /* address of relocation */ + YASM_WRITE_32_L(localbuf, xsymd->index); /* relocated symbol */ + if (reloc->base) { + xsymd = yasm_symrec_get_data(reloc->base, &xdf_symrec_data_cb); + if (!xsymd) + yasm_internal_error( + N_("xdf: no symbol data for relocated base symbol")); + YASM_WRITE_32_L(localbuf, xsymd->index); /* base symbol */ + } else { + if (reloc->type == XDF_RELOC_WRT) + yasm_internal_error( + N_("xdf: no base symbol for WRT relocation")); + YASM_WRITE_32_L(localbuf, 0); /* no base symbol */ + } + YASM_WRITE_8(localbuf, reloc->type); /* type of relocation */ + YASM_WRITE_8(localbuf, reloc->size); /* size of relocation */ + YASM_WRITE_8(localbuf, reloc->shift); /* relocation shift */ + YASM_WRITE_8(localbuf, 0); /* flags */ + fwrite(info->buf, 16, 1, info->f); + + reloc = (xdf_reloc *)yasm_section_reloc_next((yasm_reloc *)reloc); } return 0; @@ -390,7 +390,7 @@ xdf_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) /* Don't output absolute sections into the section table */ if (yasm_section_is_absolute(sect)) - return 0; + return 0; assert(info != NULL); objfmt_xdf = info->objfmt_xdf; @@ -401,29 +401,29 @@ xdf_objfmt_output_secthead(yasm_section *sect, /*@null@*/ void *d) xsymd = yasm_symrec_get_data(xsd->sym, &xdf_symrec_data_cb); assert(xsymd != NULL); - YASM_WRITE_32_L(localbuf, xsymd->index); /* section name symbol */ + YASM_WRITE_32_L(localbuf, xsymd->index); /* section name symbol */ if (xsd->addr) { - yasm_intnum_get_sized(xsd->addr, localbuf, 8, 64, 0, 0, 0); - localbuf += 8; /* physical address */ + yasm_intnum_get_sized(xsd->addr, localbuf, 8, 64, 0, 0, 0); + localbuf += 8; /* physical address */ } else { - YASM_WRITE_32_L(localbuf, 0); - YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); } if (xsd->vaddr) { - yasm_intnum_get_sized(xsd->vaddr, localbuf, 8, 64, 0, 0, 0); - localbuf += 8; /* virtual address */ + yasm_intnum_get_sized(xsd->vaddr, localbuf, 8, 64, 0, 0, 0); + localbuf += 8; /* virtual address */ } else if (xsd->addr) { - yasm_intnum_get_sized(xsd->addr, localbuf, 8, 64, 0, 0, 0); - localbuf += 8; /* VA=PA */ + yasm_intnum_get_sized(xsd->addr, localbuf, 8, 64, 0, 0, 0); + localbuf += 8; /* VA=PA */ } else { - YASM_WRITE_32_L(localbuf, 0); - YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); + YASM_WRITE_32_L(localbuf, 0); } YASM_WRITE_16_L(localbuf, yasm_section_get_align(sect)); /* alignment */ - YASM_WRITE_16_L(localbuf, xsd->flags); /* flags */ - YASM_WRITE_32_L(localbuf, xsd->scnptr); /* file ptr to data */ - YASM_WRITE_32_L(localbuf, xsd->size); /* section size */ - YASM_WRITE_32_L(localbuf, xsd->relptr); /* file ptr to relocs */ + YASM_WRITE_16_L(localbuf, xsd->flags); /* flags */ + YASM_WRITE_32_L(localbuf, xsd->scnptr); /* file ptr to data */ + YASM_WRITE_32_L(localbuf, xsd->size); /* section size */ + YASM_WRITE_32_L(localbuf, xsd->relptr); /* file ptr to relocs */ YASM_WRITE_32_L(localbuf, xsd->nreloc); /* num of relocation entries */ fwrite(info->buf, 40, 1, info->f); @@ -437,19 +437,19 @@ xdf_objfmt_count_sym(yasm_symrec *sym, /*@null@*/ void *d) yasm_sym_vis vis = yasm_symrec_get_visibility(sym); assert(info != NULL); if (vis & YASM_SYM_COMMON) { - yasm_error_set(YASM_ERROR_GENERAL, - N_("XDF object format does not support common variables")); - yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); - return 0; + yasm_error_set(YASM_ERROR_GENERAL, + N_("XDF object format does not support common variables")); + yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym)); + return 0; } if (info->all_syms || - (vis != YASM_SYM_LOCAL && !(vis & YASM_SYM_DLOCAL))) { - /* Save index in symrec data */ - xdf_symrec_data *sym_data = yasm_xmalloc(sizeof(xdf_symrec_data)); - sym_data->index = info->indx; - yasm_symrec_add_data(sym, &xdf_symrec_data_cb, sym_data); + (vis != YASM_SYM_LOCAL && !(vis & YASM_SYM_DLOCAL))) { + /* Save index in symrec data */ + xdf_symrec_data *sym_data = yasm_xmalloc(sizeof(xdf_symrec_data)); + sym_data->index = info->indx; + yasm_symrec_add_data(sym, &xdf_symrec_data_cb, sym_data); - info->indx++; + info->indx++; } return 0; } @@ -463,83 +463,83 @@ xdf_objfmt_output_sym(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (info->all_syms || vis != YASM_SYM_LOCAL) { - const char *name = yasm_symrec_get_name(sym); - const yasm_expr *equ_val; - const yasm_intnum *intn; - size_t len = strlen(name); - unsigned long value = 0; - long scnum = -3; /* -3 = debugging symbol */ - /*@dependent@*/ /*@null@*/ yasm_section *sect; - /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; - unsigned long flags = 0; - unsigned char *localbuf; - - if (vis & YASM_SYM_GLOBAL) - flags = XDF_SYM_GLOBAL; - - /* Look at symrec for value/scnum/etc. */ - if (yasm_symrec_get_label(sym, &precbc)) { - if (precbc) - sect = yasm_bc_get_section(precbc); - else - sect = NULL; - /* it's a label: get value and offset. - * If there is not a section, leave as debugging symbol. - */ - if (sect) { - /*@dependent@*/ /*@null@*/ xdf_section_data *csectd; - csectd = yasm_section_get_data(sect, &xdf_section_data_cb); - if (csectd) { - scnum = csectd->scnum; - } else if (yasm_section_is_absolute(sect)) { - yasm_expr *abs_start; - - abs_start = yasm_expr_copy(yasm_section_get_start(sect)); - intn = yasm_expr_get_intnum(&abs_start, 1); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("absolute section start not an integer expression")); - yasm_errwarn_propagate(info->errwarns, abs_start->line); - } else - value = yasm_intnum_get_uint(intn); - yasm_expr_destroy(abs_start); - - flags |= XDF_SYM_EQU; - scnum = -2; /* -2 = absolute symbol */ - } else - yasm_internal_error(N_("didn't understand section")); - if (precbc) - value += yasm_bc_next_offset(precbc); - } - } else if ((equ_val = yasm_symrec_get_equ(sym))) { - yasm_expr *equ_val_copy = yasm_expr_copy(equ_val); - intn = yasm_expr_get_intnum(&equ_val_copy, 1); - if (!intn) { - if (vis & YASM_SYM_GLOBAL) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("global EQU value not an integer expression")); - yasm_errwarn_propagate(info->errwarns, equ_val->line); - } - } else - value = yasm_intnum_get_uint(intn); - yasm_expr_destroy(equ_val_copy); - - flags |= XDF_SYM_EQU; - scnum = -2; /* -2 = absolute symbol */ - } else { - if (vis & YASM_SYM_EXTERN) { - flags = XDF_SYM_EXTERN; - scnum = -1; - } - } - - localbuf = info->buf; - YASM_WRITE_32_L(localbuf, scnum); /* section number */ - YASM_WRITE_32_L(localbuf, value); /* value */ - YASM_WRITE_32_L(localbuf, info->strtab_offset); - info->strtab_offset += (unsigned long)(len+1); - YASM_WRITE_32_L(localbuf, flags); /* flags */ - fwrite(info->buf, 16, 1, info->f); + const char *name = yasm_symrec_get_name(sym); + const yasm_expr *equ_val; + const yasm_intnum *intn; + size_t len = strlen(name); + unsigned long value = 0; + long scnum = -3; /* -3 = debugging symbol */ + /*@dependent@*/ /*@null@*/ yasm_section *sect; + /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc; + unsigned long flags = 0; + unsigned char *localbuf; + + if (vis & YASM_SYM_GLOBAL) + flags = XDF_SYM_GLOBAL; + + /* Look at symrec for value/scnum/etc. */ + if (yasm_symrec_get_label(sym, &precbc)) { + if (precbc) + sect = yasm_bc_get_section(precbc); + else + sect = NULL; + /* it's a label: get value and offset. + * If there is not a section, leave as debugging symbol. + */ + if (sect) { + /*@dependent@*/ /*@null@*/ xdf_section_data *csectd; + csectd = yasm_section_get_data(sect, &xdf_section_data_cb); + if (csectd) { + scnum = csectd->scnum; + } else if (yasm_section_is_absolute(sect)) { + yasm_expr *abs_start; + + abs_start = yasm_expr_copy(yasm_section_get_start(sect)); + intn = yasm_expr_get_intnum(&abs_start, 1); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("absolute section start not an integer expression")); + yasm_errwarn_propagate(info->errwarns, abs_start->line); + } else + value = yasm_intnum_get_uint(intn); + yasm_expr_destroy(abs_start); + + flags |= XDF_SYM_EQU; + scnum = -2; /* -2 = absolute symbol */ + } else + yasm_internal_error(N_("didn't understand section")); + if (precbc) + value += yasm_bc_next_offset(precbc); + } + } else if ((equ_val = yasm_symrec_get_equ(sym))) { + yasm_expr *equ_val_copy = yasm_expr_copy(equ_val); + intn = yasm_expr_get_intnum(&equ_val_copy, 1); + if (!intn) { + if (vis & YASM_SYM_GLOBAL) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("global EQU value not an integer expression")); + yasm_errwarn_propagate(info->errwarns, equ_val->line); + } + } else + value = yasm_intnum_get_uint(intn); + yasm_expr_destroy(equ_val_copy); + + flags |= XDF_SYM_EQU; + scnum = -2; /* -2 = absolute symbol */ + } else { + if (vis & YASM_SYM_EXTERN) { + flags = XDF_SYM_EXTERN; + scnum = -1; + } + } + + localbuf = info->buf; + YASM_WRITE_32_L(localbuf, scnum); /* section number */ + YASM_WRITE_32_L(localbuf, value); /* value */ + YASM_WRITE_32_L(localbuf, info->strtab_offset); + info->strtab_offset += (unsigned long)(len+1); + YASM_WRITE_32_L(localbuf, flags); /* flags */ + fwrite(info->buf, 16, 1, info->f); } return 0; } @@ -553,16 +553,16 @@ xdf_objfmt_output_str(yasm_symrec *sym, /*@null@*/ void *d) assert(info != NULL); if (info->all_syms || vis != YASM_SYM_LOCAL) { - const char *name = yasm_symrec_get_name(sym); - size_t len = strlen(name); - fwrite(name, len+1, 1, info->f); + const char *name = yasm_symrec_get_name(sym); + size_t len = strlen(name); + fwrite(name, len+1, 1, info->f); } return 0; } static void xdf_objfmt_output(yasm_object *object, FILE *f, int all_syms, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_objfmt_xdf *objfmt_xdf = (yasm_objfmt_xdf *)object->objfmt; xdf_objfmt_output_info info; @@ -577,14 +577,14 @@ xdf_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Allocate space for headers by seeking forward */ if (fseek(f, (long)(16+40*(objfmt_xdf->parse_scnum)), SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } /* Get number of symbols */ info.indx = 0; - info.all_syms = 1; /* force all syms into symbol table */ + info.all_syms = 1; /* force all syms into symbol table */ yasm_symtab_traverse(object->symtab, &info, xdf_objfmt_count_sym); symtab_count = info.indx; @@ -599,20 +599,20 @@ xdf_objfmt_output(yasm_object *object, FILE *f, int all_syms, /* Section data/relocs */ if (yasm_object_sections_traverse(object, &info, - xdf_objfmt_output_section)) - return; + xdf_objfmt_output_section)) + return; /* Write headers */ if (fseek(f, 0, SEEK_SET) < 0) { - yasm__fatal(N_("could not seek on output file")); - /*@notreached@*/ - return; + yasm__fatal(N_("could not seek on output file")); + /*@notreached@*/ + return; } localbuf = info.buf; - YASM_WRITE_32_L(localbuf, XDF_MAGIC); /* magic number */ + YASM_WRITE_32_L(localbuf, XDF_MAGIC); /* magic number */ YASM_WRITE_32_L(localbuf, objfmt_xdf->parse_scnum); /* number of sects */ - YASM_WRITE_32_L(localbuf, symtab_count); /* number of symtabs */ + YASM_WRITE_32_L(localbuf, symtab_count); /* number of symtabs */ /* size of sect headers + symbol table + strings */ YASM_WRITE_32_L(localbuf, info.strtab_offset-16); fwrite(info.buf, 16, 1, f); @@ -630,7 +630,7 @@ xdf_objfmt_destroy(yasm_objfmt *objfmt) static xdf_section_data * xdf_objfmt_init_new_section(yasm_object *object, yasm_section *sect, - const char *sectname, unsigned long line) + const char *sectname, unsigned long line) { yasm_objfmt_xdf *objfmt_xdf = (yasm_objfmt_xdf *)object->objfmt; xdf_section_data *data; @@ -648,7 +648,7 @@ xdf_objfmt_init_new_section(yasm_object *object, yasm_section *sect, yasm_section_add_data(sect, &xdf_section_data_cb, data); sym = yasm_symtab_define_label(object->symtab, sectname, - yasm_section_bcs_first(sect), 1, line); + yasm_section_bcs_first(sect), 1, line); data->sym = sym; return data; } @@ -662,17 +662,17 @@ xdf_objfmt_add_default_section(yasm_object *object) retval = yasm_object_get_general(object, ".text", 0, 0, 1, 0, &isnew, 0); if (isnew) { - xsd = xdf_objfmt_init_new_section(object, retval, ".text", 0); - yasm_section_set_default(retval, 1); + xsd = xdf_objfmt_init_new_section(object, retval, ".text", 0); + yasm_section_set_default(retval, 1); } return retval; } static /*@observer@*/ /*@null@*/ yasm_section * xdf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, - /*@unused@*/ /*@null@*/ - yasm_valparamhead *objext_valparams, - unsigned long line) + /*@unused@*/ /*@null@*/ + yasm_valparamhead *objext_valparams, + unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_section *retval; @@ -687,107 +687,107 @@ xdf_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams, xdf_section_data *xsd; if (!vp || vp->param || !vp->val) - return NULL; + return NULL; sectname = vp->val; while ((vp = yasm_vps_next(vp))) { - if (!vp->val) { - yasm_warn_set(YASM_WARN_GENERAL, - N_("Unrecognized numeric qualifier")); - continue; - } - - flags_override = 1; - if (yasm__strcasecmp(vp->val, "use16") == 0) { - flags &= ~(XDF_SECT_USE_32|XDF_SECT_USE_64); - flags |= XDF_SECT_USE_16; - yasm_arch_set_var(object->arch, "mode_bits", 16); - } else if (yasm__strcasecmp(vp->val, "use32") == 0) { - flags &= ~(XDF_SECT_USE_16|XDF_SECT_USE_64); - flags |= XDF_SECT_USE_32; - yasm_arch_set_var(object->arch, "mode_bits", 32); - } else if (yasm__strcasecmp(vp->val, "use64") == 0) { - flags &= ~(XDF_SECT_USE_16|XDF_SECT_USE_32); - flags |= XDF_SECT_USE_64; - yasm_arch_set_var(object->arch, "mode_bits", 64); - } else if (yasm__strcasecmp(vp->val, "bss") == 0) { - flags |= XDF_SECT_BSS; - } else if (yasm__strcasecmp(vp->val, "flat") == 0) { - flags |= XDF_SECT_FLAT; - } else if (yasm__strcasecmp(vp->val, "absolute") == 0 && vp->param) { - flags |= XDF_SECT_ABSOLUTE; - absaddr = yasm_expr_get_intnum(&vp->param, 0); - if (!absaddr) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("argument to `%s' is not an integer"), - vp->val); - return NULL; - } - } else if (yasm__strcasecmp(vp->val, "virtual") == 0 && vp->param) { - vaddr = yasm_expr_get_intnum(&vp->param, 0); - if (!vaddr) { - yasm_error_set(YASM_ERROR_NOT_CONSTANT, - N_("argument to `%s' is not an integer"), - vp->val); - return NULL; - } - } else if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { - /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; - - align_expr = yasm_expr_get_intnum(&vp->param, 0); - if (!align_expr) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not an integer"), - vp->val); - return NULL; - } - align = yasm_intnum_get_uint(align_expr); + if (!vp->val) { + yasm_warn_set(YASM_WARN_GENERAL, + N_("Unrecognized numeric qualifier")); + continue; + } + + flags_override = 1; + if (yasm__strcasecmp(vp->val, "use16") == 0) { + flags &= ~(XDF_SECT_USE_32|XDF_SECT_USE_64); + flags |= XDF_SECT_USE_16; + yasm_arch_set_var(object->arch, "mode_bits", 16); + } else if (yasm__strcasecmp(vp->val, "use32") == 0) { + flags &= ~(XDF_SECT_USE_16|XDF_SECT_USE_64); + flags |= XDF_SECT_USE_32; + yasm_arch_set_var(object->arch, "mode_bits", 32); + } else if (yasm__strcasecmp(vp->val, "use64") == 0) { + flags &= ~(XDF_SECT_USE_16|XDF_SECT_USE_32); + flags |= XDF_SECT_USE_64; + yasm_arch_set_var(object->arch, "mode_bits", 64); + } else if (yasm__strcasecmp(vp->val, "bss") == 0) { + flags |= XDF_SECT_BSS; + } else if (yasm__strcasecmp(vp->val, "flat") == 0) { + flags |= XDF_SECT_FLAT; + } else if (yasm__strcasecmp(vp->val, "absolute") == 0 && vp->param) { + flags |= XDF_SECT_ABSOLUTE; + absaddr = yasm_expr_get_intnum(&vp->param, 0); + if (!absaddr) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("argument to `%s' is not an integer"), + vp->val); + return NULL; + } + } else if (yasm__strcasecmp(vp->val, "virtual") == 0 && vp->param) { + vaddr = yasm_expr_get_intnum(&vp->param, 0); + if (!vaddr) { + yasm_error_set(YASM_ERROR_NOT_CONSTANT, + N_("argument to `%s' is not an integer"), + vp->val); + return NULL; + } + } else if (yasm__strcasecmp(vp->val, "align") == 0 && vp->param) { + /*@dependent@*/ /*@null@*/ const yasm_intnum *align_expr; + + align_expr = yasm_expr_get_intnum(&vp->param, 0); + if (!align_expr) { + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not an integer"), + vp->val); + return NULL; + } + align = yasm_intnum_get_uint(align_expr); /* Alignments must be a power of two. */ if (!is_exp2(align)) { - yasm_error_set(YASM_ERROR_VALUE, - N_("argument to `%s' is not a power of two"), - vp->val); - return NULL; - } - - /* Check to see if alignment is supported size */ - if (align > 4096) { - yasm_error_set(YASM_ERROR_VALUE, - N_("XDF does not support alignments > 4096")); - return NULL; - } - } else - yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), - vp->val); + yasm_error_set(YASM_ERROR_VALUE, + N_("argument to `%s' is not a power of two"), + vp->val); + return NULL; + } + + /* Check to see if alignment is supported size */ + if (align > 4096) { + yasm_error_set(YASM_ERROR_VALUE, + N_("XDF does not support alignments > 4096")); + return NULL; + } + } else + yasm_warn_set(YASM_WARN_GENERAL, N_("Unrecognized qualifier `%s'"), + vp->val); } retval = yasm_object_get_general(object, sectname, 0, align, 1, resonly, - &isnew, line); + &isnew, line); if (isnew) - xsd = xdf_objfmt_init_new_section(object, retval, sectname, line); + xsd = xdf_objfmt_init_new_section(object, retval, sectname, line); else - xsd = yasm_section_get_data(retval, &xdf_section_data_cb); + xsd = yasm_section_get_data(retval, &xdf_section_data_cb); if (isnew || yasm_section_is_default(retval)) { - yasm_section_set_default(retval, 0); - xsd->flags = flags; - if (absaddr) { - if (xsd->addr) - yasm_intnum_destroy(xsd->addr); - xsd->addr = yasm_intnum_copy(absaddr); - } - if (vaddr) { - if (xsd->vaddr) - yasm_intnum_destroy(xsd->vaddr); - xsd->vaddr = yasm_intnum_copy(vaddr); - } - yasm_section_set_align(retval, align, line); + yasm_section_set_default(retval, 0); + xsd->flags = flags; + if (absaddr) { + if (xsd->addr) + yasm_intnum_destroy(xsd->addr); + xsd->addr = yasm_intnum_copy(absaddr); + } + if (vaddr) { + if (xsd->vaddr) + yasm_intnum_destroy(xsd->vaddr); + xsd->vaddr = yasm_intnum_copy(vaddr); + } + yasm_section_set_align(retval, align, line); } else if (flags_override) - yasm_warn_set(YASM_WARN_GENERAL, - N_("section flags ignored on section redeclaration")); + yasm_warn_set(YASM_WARN_GENERAL, + N_("section flags ignored on section redeclaration")); return retval; } @@ -796,9 +796,9 @@ xdf_section_data_destroy(void *data) { xdf_section_data *xsd = (xdf_section_data *)data; if (xsd->addr) - yasm_intnum_destroy(xsd->addr); + yasm_intnum_destroy(xsd->addr); if (xsd->vaddr) - yasm_intnum_destroy(xsd->vaddr); + yasm_intnum_destroy(xsd->vaddr); yasm_xfree(data); } @@ -849,7 +849,7 @@ yasm_objfmt_module yasm_xdf_LTX_objfmt = { 32, xdf_objfmt_dbgfmt_keywords, "null", - NULL, /* no directives */ + NULL, /* no directives */ xdf_objfmt_create, xdf_objfmt_output, xdf_objfmt_destroy, diff --git a/modules/parsers/gas/gas-parse.c b/modules/parsers/gas/gas-parse.c index 67a572e3..00458146 100644 --- a/modules/parsers/gas/gas-parse.c +++ b/modules/parsers/gas/gas-parse.c @@ -53,7 +53,7 @@ static yasm_expr *parse_expr2(yasm_parser_gas *parser_gas); static void define_label(yasm_parser_gas *parser_gas, char *name, int local); static void define_lcomm(yasm_parser_gas *parser_gas, /*@only@*/ char *name, - yasm_expr *size, /*@null@*/ yasm_expr *align); + yasm_expr *size, /*@null@*/ yasm_expr *align); static yasm_section *gas_get_section (yasm_parser_gas *parser_gas, /*@only@*/ char *name, /*@null@*/ char *flags, /*@null@*/ char *type, /*@null@*/ yasm_valparamhead *objext_valparams, @@ -77,8 +77,8 @@ static void gas_parser_directive /*@null@*/ yasm_valparamhead *objext_valparams); #endif -#define is_eol_tok(tok) ((tok) == '\n' || (tok) == ';' || (tok) == 0) -#define is_eol() is_eol_tok(curtok) +#define is_eol_tok(tok) ((tok) == '\n' || (tok) == ';' || (tok) == 0) +#define is_eol() is_eol_tok(curtok) #define get_next_token() (curtok = gas_parser_lex(&curval, parser_gas)) @@ -87,9 +87,9 @@ get_peek_token(yasm_parser_gas *parser_gas) { char savech = parser_gas->tokch; if (parser_gas->peek_token != NONE) - yasm_internal_error(N_("only can have one token of lookahead")); + yasm_internal_error(N_("only can have one token of lookahead")); parser_gas->peek_token = - gas_parser_lex(&parser_gas->peek_tokval, parser_gas); + gas_parser_lex(&parser_gas->peek_tokval, parser_gas); parser_gas->peek_tokch = parser_gas->tokch; parser_gas->tokch = savech; } @@ -98,25 +98,25 @@ static void destroy_curtok_(yasm_parser_gas *parser_gas) { if (curtok < 256) - ; + ; else switch ((enum tokentype)curtok) { - case INTNUM: - yasm_intnum_destroy(curval.intn); - break; - case FLTNUM: - yasm_floatnum_destroy(curval.flt); - break; - case ID: - case LABEL: - yasm_xfree(curval.str_val); - break; - case STRING: - yasm_xfree(curval.str.contents); - break; - default: - break; + case INTNUM: + yasm_intnum_destroy(curval.intn); + break; + case FLTNUM: + yasm_floatnum_destroy(curval.flt); + break; + case ID: + case LABEL: + yasm_xfree(curval.str_val); + break; + case STRING: + yasm_xfree(curval.str.contents); + break; + default: + break; } - curtok = NONE; /* sanity */ + curtok = NONE; /* sanity */ } #define destroy_curtok() destroy_curtok_(parser_gas) @@ -127,15 +127,15 @@ static void demand_eol_(yasm_parser_gas *parser_gas) { if (is_eol()) - return; + return; yasm_error_set(YASM_ERROR_SYNTAX, - N_("junk at end of line, first unrecognized character is `%c'"), - parser_gas->tokch); + N_("junk at end of line, first unrecognized character is `%c'"), + parser_gas->tokch); do { - destroy_curtok(); - get_next_token(); + destroy_curtok(); + get_next_token(); } while (!is_eol()); } #define demand_eol() demand_eol_(parser_gas) @@ -147,47 +147,47 @@ expect_(yasm_parser_gas *parser_gas, int token) const char *str; if (curtok == token) - return 1; + return 1; switch (token) { - case INTNUM: str = "expected integer"; break; - case FLTNUM: str = "expected floating point value"; break; - case STRING: str = "expected string"; break; - case INSN: str = "expected instruction"; break; - case PREFIX: str = "expected instruction prefix"; break; - case REG: str = "expected register"; break; - case REGGROUP: str = "expected register group"; break; - case SEGREG: str = "expected segment register"; break; - case TARGETMOD: str = "expected target modifier"; break; - case LEFT_OP: str = "expected <<"; break; - case RIGHT_OP: str = "expected >>"; break; - case ID: str = "expected identifier"; break; - case LABEL: str = "expected label"; break; - case LINE: - case DIR_ALIGN: - case DIR_ASCII: - case DIR_COMM: - case DIR_DATA: - case DIR_ENDR: - case DIR_EQU: - case DIR_FILE: - case DIR_FILL: - case DIR_LEB128: - case DIR_LINE: - case DIR_LOCAL: - case DIR_LCOMM: - case DIR_ORG: - case DIR_REPT: - case DIR_SECTION: - case DIR_SECTNAME: - case DIR_SKIP: - case DIR_ZERO: - str = "expected directive"; - break; - default: - strch[10] = token; - str = strch; - break; + case INTNUM: str = "expected integer"; break; + case FLTNUM: str = "expected floating point value"; break; + case STRING: str = "expected string"; break; + case INSN: str = "expected instruction"; break; + case PREFIX: str = "expected instruction prefix"; break; + case REG: str = "expected register"; break; + case REGGROUP: str = "expected register group"; break; + case SEGREG: str = "expected segment register"; break; + case TARGETMOD: str = "expected target modifier"; break; + case LEFT_OP: str = "expected <<"; break; + case RIGHT_OP: str = "expected >>"; break; + case ID: str = "expected identifier"; break; + case LABEL: str = "expected label"; break; + case LINE: + case DIR_ALIGN: + case DIR_ASCII: + case DIR_COMM: + case DIR_DATA: + case DIR_ENDR: + case DIR_EQU: + case DIR_FILE: + case DIR_FILL: + case DIR_LEB128: + case DIR_LINE: + case DIR_LOCAL: + case DIR_LCOMM: + case DIR_ORG: + case DIR_REPT: + case DIR_SECTION: + case DIR_SECTNAME: + case DIR_SKIP: + case DIR_ZERO: + str = "expected directive"; + break; + default: + strch[10] = token; + str = strch; + break; } yasm_error_set(YASM_ERROR_PARSE, str); destroy_curtok(); @@ -199,26 +199,26 @@ void gas_parser_parse(yasm_parser_gas *parser_gas) { while (get_next_token() != 0) { - yasm_bytecode *bc = NULL, *temp_bc; - - if (!is_eol()) { - bc = parse_line(parser_gas); - demand_eol(); - } - - yasm_errwarn_propagate(parser_gas->errwarns, cur_line); - - temp_bc = yasm_section_bcs_append(cursect, bc); - if (temp_bc) - parser_gas->prev_bc = temp_bc; - if (curtok == ';') - continue; /* don't advance line number until \n */ - if (parser_gas->save_input) - yasm_linemap_add_source(parser_gas->linemap, - temp_bc, - (char *)parser_gas->save_line[parser_gas->save_last ^ 1]); - yasm_linemap_goto_next(parser_gas->linemap); - parser_gas->dir_line++; /* keep track for .line followed by .file */ + yasm_bytecode *bc = NULL, *temp_bc; + + if (!is_eol()) { + bc = parse_line(parser_gas); + demand_eol(); + } + + yasm_errwarn_propagate(parser_gas->errwarns, cur_line); + + temp_bc = yasm_section_bcs_append(cursect, bc); + if (temp_bc) + parser_gas->prev_bc = temp_bc; + if (curtok == ';') + continue; /* don't advance line number until \n */ + if (parser_gas->save_input) + yasm_linemap_add_source(parser_gas->linemap, + temp_bc, + (char *)parser_gas->save_line[parser_gas->save_last ^ 1]); + yasm_linemap_goto_next(parser_gas->linemap); + parser_gas->dir_line++; /* keep track for .line followed by .file */ } } @@ -235,439 +235,439 @@ parse_line(yasm_parser_gas *parser_gas) char *id; if (is_eol()) - return NULL; + return NULL; bc = parse_instr(parser_gas); if (bc) - return bc; + return bc; switch (curtok) { - case ID: - id = ID_val; - parser_gas->state = INSTDIR; - get_next_token(); /* ID */ - if (curtok == ':') { - /* Label */ - parser_gas->state = INITIAL; - get_next_token(); /* : */ - define_label(parser_gas, id, 0); - return parse_line(parser_gas); - } else if (curtok == '=') { - /* EQU */ - /* TODO: allow redefinition, assigning to . (same as .org) */ - parser_gas->state = INITIAL; - get_next_token(); /* = */ - e = parse_expr(parser_gas); - if (e) - yasm_symtab_define_equ(p_symtab, id, e, cur_line); - else - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected after `%s'"), "="); - yasm_xfree(id); - return NULL; - } - - /* possibly a directive; try to parse it */ - parse_dirvals(parser_gas, &vps); - if (!yasm_object_directive(p_object, id, "gas", &vps, NULL, - cur_line)) { - yasm_vps_delete(&vps); - yasm_xfree(id); - return NULL; - } - yasm_vps_delete(&vps); - if (id[0] == '.') - yasm_warn_set(YASM_WARN_GENERAL, - N_("directive `%s' not recognized"), id); - else - yasm_error_set(YASM_ERROR_SYNTAX, - N_("instruction not recognized: `%s'"), id); - yasm_xfree(id); - return NULL; - case LABEL: - define_label(parser_gas, LABEL_val, 0); - get_next_token(); /* LABEL */ - return parse_line(parser_gas); - - /* Line directive */ - case DIR_LINE: - get_next_token(); /* DIR_LINE */ - - if (!expect(INTNUM)) return NULL; - if (yasm_intnum_sign(INTNUM_val) < 0) { - get_next_token(); /* INTNUM */ - yasm_error_set(YASM_ERROR_SYNTAX, - N_("line number is negative")); - return NULL; - } - - parser_gas->dir_line = yasm_intnum_get_uint(INTNUM_val); - yasm_intnum_destroy(INTNUM_val); - get_next_token(); /* INTNUM */ + case ID: + id = ID_val; + parser_gas->state = INSTDIR; + get_next_token(); /* ID */ + if (curtok == ':') { + /* Label */ + parser_gas->state = INITIAL; + get_next_token(); /* : */ + define_label(parser_gas, id, 0); + return parse_line(parser_gas); + } else if (curtok == '=') { + /* EQU */ + /* TODO: allow redefinition, assigning to . (same as .org) */ + parser_gas->state = INITIAL; + get_next_token(); /* = */ + e = parse_expr(parser_gas); + if (e) + yasm_symtab_define_equ(p_symtab, id, e, cur_line); + else + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected after `%s'"), "="); + yasm_xfree(id); + return NULL; + } + + /* possibly a directive; try to parse it */ + parse_dirvals(parser_gas, &vps); + if (!yasm_object_directive(p_object, id, "gas", &vps, NULL, + cur_line)) { + yasm_vps_delete(&vps); + yasm_xfree(id); + return NULL; + } + yasm_vps_delete(&vps); + if (id[0] == '.') + yasm_warn_set(YASM_WARN_GENERAL, + N_("directive `%s' not recognized"), id); + else + yasm_error_set(YASM_ERROR_SYNTAX, + N_("instruction not recognized: `%s'"), id); + yasm_xfree(id); + return NULL; + case LABEL: + define_label(parser_gas, LABEL_val, 0); + get_next_token(); /* LABEL */ + return parse_line(parser_gas); + + /* Line directive */ + case DIR_LINE: + get_next_token(); /* DIR_LINE */ + + if (!expect(INTNUM)) return NULL; + if (yasm_intnum_sign(INTNUM_val) < 0) { + get_next_token(); /* INTNUM */ + yasm_error_set(YASM_ERROR_SYNTAX, + N_("line number is negative")); + return NULL; + } + + parser_gas->dir_line = yasm_intnum_get_uint(INTNUM_val); + yasm_intnum_destroy(INTNUM_val); + get_next_token(); /* INTNUM */ - if (parser_gas->dir_fileline == 3) { - /* Have both file and line */ - yasm_linemap_set(parser_gas->linemap, NULL, - parser_gas->dir_line, 1); - } else if (parser_gas->dir_fileline == 1) { - /* Had previous file directive only */ - parser_gas->dir_fileline = 3; - yasm_linemap_set(parser_gas->linemap, parser_gas->dir_file, - parser_gas->dir_line, 1); - } else { - /* Didn't see file yet */ - parser_gas->dir_fileline = 2; - } - return NULL; - - /* Macro directives */ - case DIR_REPT: - get_next_token(); /* DIR_REPT */ - e = parse_expr(parser_gas); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected after `%s'"), - ".rept"); - return NULL; - } - intn = yasm_expr_get_intnum(&e, 0); - - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, - N_("rept expression not absolute")); - } else if (yasm_intnum_sign(intn) < 0) { - yasm_error_set(YASM_ERROR_VALUE, - N_("rept expression is negative")); - } else { - gas_rept *rept = yasm_xmalloc(sizeof(gas_rept)); - STAILQ_INIT(&rept->lines); - rept->startline = cur_line; - rept->numrept = yasm_intnum_get_uint(intn); - rept->numdone = 0; - rept->line = NULL; - rept->linepos = 0; - rept->ended = 0; - rept->oldbuf = NULL; - rept->oldbuflen = 0; - rept->oldbufpos = 0; - parser_gas->rept = rept; - } - return NULL; - case DIR_ENDR: - get_next_token(); /* DIR_ENDR */ - /* Shouldn't ever get here unless we didn't get a DIR_REPT first */ - yasm_error_set(YASM_ERROR_SYNTAX, N_("endr without matching rept")); - return NULL; - - /* Alignment directives */ - case DIR_ALIGN: - { - yasm_expr *bound, *fill=NULL, *maxskip=NULL; - - ival = DIR_ALIGN_val; - get_next_token(); /* DIR_ALIGN */ - - bound = parse_expr(parser_gas); - if (!bound) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_(".align directive must specify alignment")); - return NULL; - } - - if (curtok == ',') { - get_next_token(); /* ',' */ - fill = parse_expr(parser_gas); - if (curtok == ',') { - get_next_token(); /* ',' */ - maxskip = parse_expr(parser_gas); - } - } - - return gas_parser_align(parser_gas, cursect, bound, fill, maxskip, - (int)ival); - } - case DIR_ORG: - get_next_token(); /* DIR_ORG */ - if (!expect(INTNUM)) return NULL; - /* TODO: support expr instead of intnum */ - bc = yasm_bc_create_org(yasm_intnum_get_uint(INTNUM_val), cur_line); - yasm_intnum_destroy(INTNUM_val); - get_next_token(); /* INTNUM */ - return bc; + if (parser_gas->dir_fileline == 3) { + /* Have both file and line */ + yasm_linemap_set(parser_gas->linemap, NULL, + parser_gas->dir_line, 1); + } else if (parser_gas->dir_fileline == 1) { + /* Had previous file directive only */ + parser_gas->dir_fileline = 3; + yasm_linemap_set(parser_gas->linemap, parser_gas->dir_file, + parser_gas->dir_line, 1); + } else { + /* Didn't see file yet */ + parser_gas->dir_fileline = 2; + } + return NULL; + + /* Macro directives */ + case DIR_REPT: + get_next_token(); /* DIR_REPT */ + e = parse_expr(parser_gas); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected after `%s'"), + ".rept"); + return NULL; + } + intn = yasm_expr_get_intnum(&e, 0); + + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, + N_("rept expression not absolute")); + } else if (yasm_intnum_sign(intn) < 0) { + yasm_error_set(YASM_ERROR_VALUE, + N_("rept expression is negative")); + } else { + gas_rept *rept = yasm_xmalloc(sizeof(gas_rept)); + STAILQ_INIT(&rept->lines); + rept->startline = cur_line; + rept->numrept = yasm_intnum_get_uint(intn); + rept->numdone = 0; + rept->line = NULL; + rept->linepos = 0; + rept->ended = 0; + rept->oldbuf = NULL; + rept->oldbuflen = 0; + rept->oldbufpos = 0; + parser_gas->rept = rept; + } + return NULL; + case DIR_ENDR: + get_next_token(); /* DIR_ENDR */ + /* Shouldn't ever get here unless we didn't get a DIR_REPT first */ + yasm_error_set(YASM_ERROR_SYNTAX, N_("endr without matching rept")); + return NULL; + + /* Alignment directives */ + case DIR_ALIGN: + { + yasm_expr *bound, *fill=NULL, *maxskip=NULL; + + ival = DIR_ALIGN_val; + get_next_token(); /* DIR_ALIGN */ + + bound = parse_expr(parser_gas); + if (!bound) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_(".align directive must specify alignment")); + return NULL; + } + + if (curtok == ',') { + get_next_token(); /* ',' */ + fill = parse_expr(parser_gas); + if (curtok == ',') { + get_next_token(); /* ',' */ + maxskip = parse_expr(parser_gas); + } + } + + return gas_parser_align(parser_gas, cursect, bound, fill, maxskip, + (int)ival); + } + case DIR_ORG: + get_next_token(); /* DIR_ORG */ + if (!expect(INTNUM)) return NULL; + /* TODO: support expr instead of intnum */ + bc = yasm_bc_create_org(yasm_intnum_get_uint(INTNUM_val), cur_line); + yasm_intnum_destroy(INTNUM_val); + get_next_token(); /* INTNUM */ + return bc; - /* Data visibility directives */ - case DIR_LOCAL: - get_next_token(); /* DIR_LOCAL */ - if (!expect(ID)) return NULL; - yasm_symtab_declare(p_symtab, ID_val, YASM_SYM_DLOCAL, cur_line); - yasm_xfree(ID_val); - get_next_token(); /* ID */ - return NULL; - case DIR_COMM: - case DIR_LCOMM: - { - yasm_expr *align = NULL; - /*@null@*/ /*@dependent@*/ yasm_symrec *sym; - int is_lcomm = curtok == DIR_LCOMM; - - get_next_token(); /* DIR_LOCAL */ - - if (!expect(ID)) return NULL; - id = ID_val; - get_next_token(); /* ID */ - if (!expect(',')) { - yasm_xfree(id); - return NULL; - } - get_next_token(); /* , */ - e = parse_expr(parser_gas); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("size expected for `%s'"), - ".COMM"); - return NULL; - } - if (curtok == ',') { - /* Optional alignment expression */ - get_next_token(); /* ',' */ - align = parse_expr(parser_gas); - } - /* If already explicitly declared local, treat like LCOMM */ - if (is_lcomm - || ((sym = yasm_symtab_get(p_symtab, id)) - && yasm_symrec_get_visibility(sym) == YASM_SYM_DLOCAL)) { - define_lcomm(parser_gas, id, e, align); - } else if (align) { - /* Give third parameter as objext valparam */ - yasm_valparamhead *extvps = yasm_vps_create(); - vp = yasm_vp_create(NULL, align); - yasm_vps_append(extvps, vp); - - sym = yasm_symtab_declare(p_symtab, id, YASM_SYM_COMMON, - cur_line); - yasm_symrec_set_common_size(sym, e); - yasm_symrec_set_objext_valparams(sym, extvps); - - yasm_xfree(id); - } else { - sym = yasm_symtab_declare(p_symtab, id, YASM_SYM_COMMON, - cur_line); - yasm_symrec_set_common_size(sym, e); - yasm_xfree(id); - } - return NULL; - } - - /* Integer data definition directives */ - case DIR_ASCII: - ival = DIR_ASCII_val; - get_next_token(); /* DIR_ASCII */ - if (!parse_strvals(parser_gas, &dvs)) - return NULL; - return yasm_bc_create_data(&dvs, 1, (int)ival, p_object->arch, - cur_line); - case DIR_DATA: - ival = DIR_DATA_val; - get_next_token(); /* DIR_DATA */ - if (!parse_datavals(parser_gas, &dvs)) - return NULL; - return yasm_bc_create_data(&dvs, ival, 0, p_object->arch, - cur_line); - case DIR_LEB128: - ival = DIR_LEB128_val; - get_next_token(); /* DIR_LEB128 */ - if (!parse_datavals(parser_gas, &dvs)) - return NULL; - return yasm_bc_create_leb128(&dvs, (int)ival, cur_line); - - /* Empty space / fill data definition directives */ - case DIR_ZERO: - get_next_token(); /* DIR_ZERO */ - e = parse_expr(parser_gas); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected after `%s'"), ".ZERO"); - return NULL; - } - - yasm_dvs_initialize(&dvs); - yasm_dvs_append(&dvs, yasm_dv_create_expr( - p_expr_new_ident(yasm_expr_int(yasm_intnum_create_uint(0))))); - bc = yasm_bc_create_data(&dvs, 1, 0, p_object->arch, cur_line); - yasm_bc_set_multiple(bc, e); - return bc; - case DIR_SKIP: - { - yasm_expr *e_val; - - get_next_token(); /* DIR_SKIP */ - e = parse_expr(parser_gas); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected after `%s'"), ".SKIP"); - return NULL; - } - if (curtok != ',') - return yasm_bc_create_reserve(e, 1, cur_line); - get_next_token(); /* ',' */ - e_val = parse_expr(parser_gas); - yasm_dvs_initialize(&dvs); - yasm_dvs_append(&dvs, yasm_dv_create_expr(e_val)); - bc = yasm_bc_create_data(&dvs, 1, 0, p_object->arch, cur_line); - - yasm_bc_set_multiple(bc, e); - return bc; - } - - /* fill data definition directive */ - case DIR_FILL: - { - yasm_expr *sz=NULL, *val=NULL; - get_next_token(); /* DIR_FILL */ - e = parse_expr(parser_gas); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected after `%s'"), ".FILL"); - return NULL; - } - if (curtok == ',') { - get_next_token(); /* ',' */ - sz = parse_expr(parser_gas); - if (curtok == ',') { - get_next_token(); /* ',' */ - val = parse_expr(parser_gas); - } - } - return gas_parser_dir_fill(parser_gas, e, sz, val); - } - - /* Section directives */ - case DIR_SECTNAME: - gas_switch_section(parser_gas, DIR_SECTNAME_val, NULL, NULL, NULL, - 1); - get_next_token(); /* DIR_SECTNAME */ - return NULL; - case DIR_SECTION: - { - /* DIR_SECTION ID ',' STRING ',' '@' ID ',' dirvals */ - char *sectname, *flags = NULL, *type = NULL; - int have_vps = 0; - - get_next_token(); /* DIR_SECTION */ - - if (!expect(ID)) return NULL; - sectname = ID_val; - get_next_token(); /* ID */ - - if (curtok == ',') { - get_next_token(); /* ',' */ - if (!expect(STRING)) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("flag string expected")); - yasm_xfree(sectname); - return NULL; - } - flags = STRING_val.contents; - get_next_token(); /* STRING */ - } - - if (curtok == ',') { - get_next_token(); /* ',' */ - if (!expect('@')) { - yasm_xfree(sectname); - yasm_xfree(flags); - return NULL; - } - get_next_token(); /* '@' */ - if (!expect(ID)) { - yasm_xfree(sectname); - yasm_xfree(flags); - return NULL; - } - type = ID_val; - get_next_token(); /* ID */ - } - - if (curtok == ',') { - get_next_token(); /* ',' */ - if (parse_dirvals(parser_gas, &vps)) - have_vps = 1; - } - - gas_switch_section(parser_gas, sectname, flags, type, - have_vps ? &vps : NULL, 0); - yasm_xfree(flags); - return NULL; - } - - /* Other directives */ - case DIR_FILE: - get_next_token(); /* DIR_FILE */ - if (curtok == STRING) { - /* No file number; this form also sets the assembler's - * internal line number. - */ - char *filename = STRING_val.contents; - get_next_token(); /* STRING */ - if (parser_gas->dir_fileline == 3) { - /* Have both file and line */ - const char *old_fn; - unsigned long old_line; - - yasm_linemap_lookup(parser_gas->linemap, cur_line, &old_fn, - &old_line); - yasm_linemap_set(parser_gas->linemap, filename, old_line, - 1); - } else if (parser_gas->dir_fileline == 2) { - /* Had previous line directive only */ - parser_gas->dir_fileline = 3; - yasm_linemap_set(parser_gas->linemap, filename, - parser_gas->dir_line, 1); - } else { - /* Didn't see line yet, save file */ - parser_gas->dir_fileline = 1; - if (parser_gas->dir_file) - yasm_xfree(parser_gas->dir_file); - parser_gas->dir_file = yasm__xstrdup(filename); - } - - /* Pass change along to debug format */ - yasm_vps_initialize(&vps); - vp = yasm_vp_create(filename, NULL); - yasm_vps_append(&vps, vp); - - yasm_object_directive(p_object, ".file", "gas", &vps, NULL, - cur_line); - - yasm_vps_delete(&vps); - return NULL; - } - - /* fileno filename form */ - yasm_vps_initialize(&vps); - - if (!expect(INTNUM)) return NULL; - vp = yasm_vp_create(NULL, - p_expr_new_ident(yasm_expr_int(INTNUM_val))); - yasm_vps_append(&vps, vp); - get_next_token(); /* INTNUM */ - - if (!expect(STRING)) { - yasm_vps_delete(&vps); - return NULL; - } - vp = yasm_vp_create(STRING_val.contents, NULL); - yasm_vps_append(&vps, vp); - get_next_token(); /* STRING */ - - yasm_object_directive(p_object, ".file", "gas", &vps, NULL, - cur_line); - - yasm_vps_delete(&vps); - return NULL; - default: - yasm_error_set(YASM_ERROR_SYNTAX, - N_("label or instruction expected at start of line")); - return NULL; + /* Data visibility directives */ + case DIR_LOCAL: + get_next_token(); /* DIR_LOCAL */ + if (!expect(ID)) return NULL; + yasm_symtab_declare(p_symtab, ID_val, YASM_SYM_DLOCAL, cur_line); + yasm_xfree(ID_val); + get_next_token(); /* ID */ + return NULL; + case DIR_COMM: + case DIR_LCOMM: + { + yasm_expr *align = NULL; + /*@null@*/ /*@dependent@*/ yasm_symrec *sym; + int is_lcomm = curtok == DIR_LCOMM; + + get_next_token(); /* DIR_LOCAL */ + + if (!expect(ID)) return NULL; + id = ID_val; + get_next_token(); /* ID */ + if (!expect(',')) { + yasm_xfree(id); + return NULL; + } + get_next_token(); /* , */ + e = parse_expr(parser_gas); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("size expected for `%s'"), + ".COMM"); + return NULL; + } + if (curtok == ',') { + /* Optional alignment expression */ + get_next_token(); /* ',' */ + align = parse_expr(parser_gas); + } + /* If already explicitly declared local, treat like LCOMM */ + if (is_lcomm + || ((sym = yasm_symtab_get(p_symtab, id)) + && yasm_symrec_get_visibility(sym) == YASM_SYM_DLOCAL)) { + define_lcomm(parser_gas, id, e, align); + } else if (align) { + /* Give third parameter as objext valparam */ + yasm_valparamhead *extvps = yasm_vps_create(); + vp = yasm_vp_create(NULL, align); + yasm_vps_append(extvps, vp); + + sym = yasm_symtab_declare(p_symtab, id, YASM_SYM_COMMON, + cur_line); + yasm_symrec_set_common_size(sym, e); + yasm_symrec_set_objext_valparams(sym, extvps); + + yasm_xfree(id); + } else { + sym = yasm_symtab_declare(p_symtab, id, YASM_SYM_COMMON, + cur_line); + yasm_symrec_set_common_size(sym, e); + yasm_xfree(id); + } + return NULL; + } + + /* Integer data definition directives */ + case DIR_ASCII: + ival = DIR_ASCII_val; + get_next_token(); /* DIR_ASCII */ + if (!parse_strvals(parser_gas, &dvs)) + return NULL; + return yasm_bc_create_data(&dvs, 1, (int)ival, p_object->arch, + cur_line); + case DIR_DATA: + ival = DIR_DATA_val; + get_next_token(); /* DIR_DATA */ + if (!parse_datavals(parser_gas, &dvs)) + return NULL; + return yasm_bc_create_data(&dvs, ival, 0, p_object->arch, + cur_line); + case DIR_LEB128: + ival = DIR_LEB128_val; + get_next_token(); /* DIR_LEB128 */ + if (!parse_datavals(parser_gas, &dvs)) + return NULL; + return yasm_bc_create_leb128(&dvs, (int)ival, cur_line); + + /* Empty space / fill data definition directives */ + case DIR_ZERO: + get_next_token(); /* DIR_ZERO */ + e = parse_expr(parser_gas); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected after `%s'"), ".ZERO"); + return NULL; + } + + yasm_dvs_initialize(&dvs); + yasm_dvs_append(&dvs, yasm_dv_create_expr( + p_expr_new_ident(yasm_expr_int(yasm_intnum_create_uint(0))))); + bc = yasm_bc_create_data(&dvs, 1, 0, p_object->arch, cur_line); + yasm_bc_set_multiple(bc, e); + return bc; + case DIR_SKIP: + { + yasm_expr *e_val; + + get_next_token(); /* DIR_SKIP */ + e = parse_expr(parser_gas); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected after `%s'"), ".SKIP"); + return NULL; + } + if (curtok != ',') + return yasm_bc_create_reserve(e, 1, cur_line); + get_next_token(); /* ',' */ + e_val = parse_expr(parser_gas); + yasm_dvs_initialize(&dvs); + yasm_dvs_append(&dvs, yasm_dv_create_expr(e_val)); + bc = yasm_bc_create_data(&dvs, 1, 0, p_object->arch, cur_line); + + yasm_bc_set_multiple(bc, e); + return bc; + } + + /* fill data definition directive */ + case DIR_FILL: + { + yasm_expr *sz=NULL, *val=NULL; + get_next_token(); /* DIR_FILL */ + e = parse_expr(parser_gas); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected after `%s'"), ".FILL"); + return NULL; + } + if (curtok == ',') { + get_next_token(); /* ',' */ + sz = parse_expr(parser_gas); + if (curtok == ',') { + get_next_token(); /* ',' */ + val = parse_expr(parser_gas); + } + } + return gas_parser_dir_fill(parser_gas, e, sz, val); + } + + /* Section directives */ + case DIR_SECTNAME: + gas_switch_section(parser_gas, DIR_SECTNAME_val, NULL, NULL, NULL, + 1); + get_next_token(); /* DIR_SECTNAME */ + return NULL; + case DIR_SECTION: + { + /* DIR_SECTION ID ',' STRING ',' '@' ID ',' dirvals */ + char *sectname, *flags = NULL, *type = NULL; + int have_vps = 0; + + get_next_token(); /* DIR_SECTION */ + + if (!expect(ID)) return NULL; + sectname = ID_val; + get_next_token(); /* ID */ + + if (curtok == ',') { + get_next_token(); /* ',' */ + if (!expect(STRING)) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("flag string expected")); + yasm_xfree(sectname); + return NULL; + } + flags = STRING_val.contents; + get_next_token(); /* STRING */ + } + + if (curtok == ',') { + get_next_token(); /* ',' */ + if (!expect('@')) { + yasm_xfree(sectname); + yasm_xfree(flags); + return NULL; + } + get_next_token(); /* '@' */ + if (!expect(ID)) { + yasm_xfree(sectname); + yasm_xfree(flags); + return NULL; + } + type = ID_val; + get_next_token(); /* ID */ + } + + if (curtok == ',') { + get_next_token(); /* ',' */ + if (parse_dirvals(parser_gas, &vps)) + have_vps = 1; + } + + gas_switch_section(parser_gas, sectname, flags, type, + have_vps ? &vps : NULL, 0); + yasm_xfree(flags); + return NULL; + } + + /* Other directives */ + case DIR_FILE: + get_next_token(); /* DIR_FILE */ + if (curtok == STRING) { + /* No file number; this form also sets the assembler's + * internal line number. + */ + char *filename = STRING_val.contents; + get_next_token(); /* STRING */ + if (parser_gas->dir_fileline == 3) { + /* Have both file and line */ + const char *old_fn; + unsigned long old_line; + + yasm_linemap_lookup(parser_gas->linemap, cur_line, &old_fn, + &old_line); + yasm_linemap_set(parser_gas->linemap, filename, old_line, + 1); + } else if (parser_gas->dir_fileline == 2) { + /* Had previous line directive only */ + parser_gas->dir_fileline = 3; + yasm_linemap_set(parser_gas->linemap, filename, + parser_gas->dir_line, 1); + } else { + /* Didn't see line yet, save file */ + parser_gas->dir_fileline = 1; + if (parser_gas->dir_file) + yasm_xfree(parser_gas->dir_file); + parser_gas->dir_file = yasm__xstrdup(filename); + } + + /* Pass change along to debug format */ + yasm_vps_initialize(&vps); + vp = yasm_vp_create(filename, NULL); + yasm_vps_append(&vps, vp); + + yasm_object_directive(p_object, ".file", "gas", &vps, NULL, + cur_line); + + yasm_vps_delete(&vps); + return NULL; + } + + /* fileno filename form */ + yasm_vps_initialize(&vps); + + if (!expect(INTNUM)) return NULL; + vp = yasm_vp_create(NULL, + p_expr_new_ident(yasm_expr_int(INTNUM_val))); + yasm_vps_append(&vps, vp); + get_next_token(); /* INTNUM */ + + if (!expect(STRING)) { + yasm_vps_delete(&vps); + return NULL; + } + vp = yasm_vp_create(STRING_val.contents, NULL); + yasm_vps_append(&vps, vp); + get_next_token(); /* STRING */ + + yasm_object_directive(p_object, ".file", "gas", &vps, NULL, + cur_line); + + yasm_vps_delete(&vps); + return NULL; + default: + yasm_error_set(YASM_ERROR_SYNTAX, + N_("label or instruction expected at start of line")); + return NULL; } } @@ -677,64 +677,64 @@ parse_instr(yasm_parser_gas *parser_gas) yasm_bytecode *bc; switch (curtok) { - case INSN: - { - yystype insn = curval; /* structure copy */ - yasm_insn_operands operands; - int num_operands = 0; - - get_next_token(); - if (is_eol()) { - /* no operands */ - return yasm_bc_create_insn(p_object->arch, insn.arch_data, - 0, NULL, cur_line); - } - - /* parse operands */ - yasm_ops_initialize(&operands); - for (;;) { - yasm_insn_operand *op = parse_operand(parser_gas); - if (!op) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression syntax error")); - yasm_ops_delete(&operands, 1); - return NULL; - } - yasm_ops_append(&operands, op); - num_operands++; - - if (is_eol()) - break; - if (!expect(',')) { - yasm_ops_delete(&operands, 1); - return NULL; - } - get_next_token(); - } - return yasm_bc_create_insn(p_object->arch, insn.arch_data, - num_operands, &operands, cur_line); - } - case PREFIX: - { - yystype prefix = curval; /* structure copy */ - get_next_token(); /* PREFIX */ - bc = parse_instr(parser_gas); - if (!bc) - bc = yasm_bc_create_empty_insn(p_object->arch, cur_line); - yasm_bc_insn_add_prefix(bc, prefix.arch_data); - return bc; - } - case SEGREG: - { - uintptr_t segreg = SEGREG_val[0]; - get_next_token(); /* SEGREG */ - bc = parse_instr(parser_gas); - if (!bc) - bc = yasm_bc_create_empty_insn(p_object->arch, cur_line); - yasm_bc_insn_add_seg_prefix(bc, segreg); - } - default: - return NULL; + case INSN: + { + yystype insn = curval; /* structure copy */ + yasm_insn_operands operands; + int num_operands = 0; + + get_next_token(); + if (is_eol()) { + /* no operands */ + return yasm_bc_create_insn(p_object->arch, insn.arch_data, + 0, NULL, cur_line); + } + + /* parse operands */ + yasm_ops_initialize(&operands); + for (;;) { + yasm_insn_operand *op = parse_operand(parser_gas); + if (!op) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression syntax error")); + yasm_ops_delete(&operands, 1); + return NULL; + } + yasm_ops_append(&operands, op); + num_operands++; + + if (is_eol()) + break; + if (!expect(',')) { + yasm_ops_delete(&operands, 1); + return NULL; + } + get_next_token(); + } + return yasm_bc_create_insn(p_object->arch, insn.arch_data, + num_operands, &operands, cur_line); + } + case PREFIX: + { + yystype prefix = curval; /* structure copy */ + get_next_token(); /* PREFIX */ + bc = parse_instr(parser_gas); + if (!bc) + bc = yasm_bc_create_empty_insn(p_object->arch, cur_line); + yasm_bc_insn_add_prefix(bc, prefix.arch_data); + return bc; + } + case SEGREG: + { + uintptr_t segreg = SEGREG_val[0]; + get_next_token(); /* SEGREG */ + bc = parse_instr(parser_gas); + if (!bc) + bc = yasm_bc_create_empty_insn(p_object->arch, cur_line); + yasm_bc_insn_add_seg_prefix(bc, segreg); + } + default: + return NULL; } } @@ -748,38 +748,38 @@ parse_dirvals(yasm_parser_gas *parser_gas, yasm_valparamhead *vps) yasm_vps_initialize(vps); for (;;) { - switch (curtok) { - case ID: - get_peek_token(parser_gas); - if (parser_gas->peek_token == ',' || - is_eol_tok(parser_gas->peek_token)) { - /* Just an ID */ - vp = yasm_vp_create(ID_val, NULL); - get_next_token(); /* ID */ - } else { - e = parse_expr(parser_gas); - vp = yasm_vp_create(NULL, e); - } - break; - case STRING: - vp = yasm_vp_create(STRING_val.contents, NULL); - get_next_token(); /* STRING */ - break; - case '@': - /* XXX: is throwing it away *really* the right thing? */ - get_next_token(); /* @ */ - continue; - default: - e = parse_expr(parser_gas); - if (!e) - return num; - vp = yasm_vp_create(NULL, e); - break; - } - yasm_vps_append(vps, vp); - num++; - if (curtok == ',') - get_next_token(); /* ',' */ + switch (curtok) { + case ID: + get_peek_token(parser_gas); + if (parser_gas->peek_token == ',' || + is_eol_tok(parser_gas->peek_token)) { + /* Just an ID */ + vp = yasm_vp_create(ID_val, NULL); + get_next_token(); /* ID */ + } else { + e = parse_expr(parser_gas); + vp = yasm_vp_create(NULL, e); + } + break; + case STRING: + vp = yasm_vp_create(STRING_val.contents, NULL); + get_next_token(); /* STRING */ + break; + case '@': + /* XXX: is throwing it away *really* the right thing? */ + get_next_token(); /* @ */ + continue; + default: + e = parse_expr(parser_gas); + if (!e) + return num; + vp = yasm_vp_create(NULL, e); + break; + } + yasm_vps_append(vps, vp); + num++; + if (curtok == ',') + get_next_token(); /* ',' */ } return num; } @@ -794,18 +794,18 @@ parse_datavals(yasm_parser_gas *parser_gas, yasm_datavalhead *dvs) yasm_dvs_initialize(dvs); for (;;) { - e = parse_expr(parser_gas); - if (!e) { - yasm_dvs_delete(dvs); - yasm_dvs_initialize(dvs); - return 0; - } - dv = yasm_dv_create_expr(e); - yasm_dvs_append(dvs, dv); - num++; - if (curtok != ',') - break; - get_next_token(); /* ',' */ + e = parse_expr(parser_gas); + if (!e) { + yasm_dvs_delete(dvs); + yasm_dvs_initialize(dvs); + return 0; + } + dv = yasm_dv_create_expr(e); + yasm_dvs_append(dvs, dv); + num++; + if (curtok != ',') + break; + get_next_token(); /* ',' */ } return num; } @@ -819,18 +819,18 @@ parse_strvals(yasm_parser_gas *parser_gas, yasm_datavalhead *dvs) yasm_dvs_initialize(dvs); for (;;) { - if (!expect(STRING)) { - yasm_dvs_delete(dvs); - yasm_dvs_initialize(dvs); - return 0; - } - dv = yasm_dv_create_string(STRING_val.contents, STRING_val.len); - yasm_dvs_append(dvs, dv); - get_next_token(); /* STRING */ - num++; - if (curtok != ',') - break; - get_next_token(); /* ',' */ + if (!expect(STRING)) { + yasm_dvs_delete(dvs); + yasm_dvs_initialize(dvs); + return 0; + } + dv = yasm_dv_create_string(STRING_val.contents, STRING_val.len); + yasm_dvs_append(dvs, dv); + get_next_token(); /* STRING */ + num++; + if (curtok != ',') + break; + get_next_token(); /* ',' */ } return num; } @@ -845,15 +845,15 @@ parse_memaddr(yasm_parser_gas *parser_gas) int strong = 0; if (curtok == SEGREG) { - uintptr_t segreg = SEGREG_val[0]; - get_next_token(); /* SEGREG */ - if (!expect(':')) return NULL; - get_next_token(); /* ':' */ - ea = parse_memaddr(parser_gas); - if (!ea) - return NULL; - yasm_ea_set_segreg(ea, segreg); - return ea; + uintptr_t segreg = SEGREG_val[0]; + get_next_token(); /* SEGREG */ + if (!expect(':')) return NULL; + get_next_token(); /* ':' */ + ea = parse_memaddr(parser_gas); + if (!ea) + return NULL; + yasm_ea_set_segreg(ea, segreg); + return ea; } /* We want to parse a leading expression, except when it's actually @@ -862,99 +862,99 @@ parse_memaddr(yasm_parser_gas *parser_gas) */ get_peek_token(parser_gas); if (curtok != '(' || (parser_gas->peek_token != REG - && parser_gas->peek_token != ',')) - e1 = parse_expr(parser_gas); + && parser_gas->peek_token != ',')) + e1 = parse_expr(parser_gas); else - e1 = NULL; + e1 = NULL; if (curtok == '(') { - int havereg = 0; - uintptr_t reg = 0; - yasm_intnum *scale = NULL; - - get_next_token(); /* '(' */ - - /* base register */ - if (curtok == REG) { - e2 = p_expr_new_ident(yasm_expr_reg(REG_val[0])); - get_next_token(); /* REG */ - } else - e2 = p_expr_new_ident(yasm_expr_int(yasm_intnum_create_uint(0))); - - if (curtok == ')') - goto done; - - if (!expect(',')) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("invalid memory expression")); - if (e1) yasm_expr_destroy(e1); - yasm_expr_destroy(e2); - return NULL; - } - get_next_token(); /* ',' */ - - if (curtok == ')') - goto done; - - /* index register */ - if (curtok == REG) { - reg = REG_val[0]; - havereg = 1; - get_next_token(); /* REG */ - if (curtok != ',') { - scale = yasm_intnum_create_uint(1); - goto done; - } - get_next_token(); /* ',' */ - } - - /* scale */ - if (!expect(INTNUM)) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("non-integer scale")); - if (e1) yasm_expr_destroy(e1); - yasm_expr_destroy(e2); - return NULL; - } - scale = INTNUM_val; - get_next_token(); /* INTNUM */ + int havereg = 0; + uintptr_t reg = 0; + yasm_intnum *scale = NULL; + + get_next_token(); /* '(' */ + + /* base register */ + if (curtok == REG) { + e2 = p_expr_new_ident(yasm_expr_reg(REG_val[0])); + get_next_token(); /* REG */ + } else + e2 = p_expr_new_ident(yasm_expr_int(yasm_intnum_create_uint(0))); + + if (curtok == ')') + goto done; + + if (!expect(',')) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("invalid memory expression")); + if (e1) yasm_expr_destroy(e1); + yasm_expr_destroy(e2); + return NULL; + } + get_next_token(); /* ',' */ + + if (curtok == ')') + goto done; + + /* index register */ + if (curtok == REG) { + reg = REG_val[0]; + havereg = 1; + get_next_token(); /* REG */ + if (curtok != ',') { + scale = yasm_intnum_create_uint(1); + goto done; + } + get_next_token(); /* ',' */ + } + + /* scale */ + if (!expect(INTNUM)) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("non-integer scale")); + if (e1) yasm_expr_destroy(e1); + yasm_expr_destroy(e2); + return NULL; + } + scale = INTNUM_val; + get_next_token(); /* INTNUM */ done: - if (!expect(')')) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("invalid memory expression")); - if (scale) yasm_intnum_destroy(scale); - if (e1) yasm_expr_destroy(e1); - yasm_expr_destroy(e2); - return NULL; - } - get_next_token(); /* ')' */ - - if (scale) { - if (!havereg) { - if (yasm_intnum_get_uint(scale) != 1) - yasm_warn_set(YASM_WARN_GENERAL, - N_("scale factor of %u without an index register"), - yasm_intnum_get_uint(scale)); - yasm_intnum_destroy(scale); - } else - e2 = p_expr_new(yasm_expr_expr(e2), YASM_EXPR_ADD, - yasm_expr_expr(p_expr_new(yasm_expr_reg(reg), YASM_EXPR_MUL, - yasm_expr_int(scale)))); - } - - if (e1) { - /* Ordering is critical here to correctly detecting presence of - * RIP in RIP-relative expressions. - */ - e1 = p_expr_new_tree(e2, YASM_EXPR_ADD, e1); - } else - e1 = e2; - strong = 1; + if (!expect(')')) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("invalid memory expression")); + if (scale) yasm_intnum_destroy(scale); + if (e1) yasm_expr_destroy(e1); + yasm_expr_destroy(e2); + return NULL; + } + get_next_token(); /* ')' */ + + if (scale) { + if (!havereg) { + if (yasm_intnum_get_uint(scale) != 1) + yasm_warn_set(YASM_WARN_GENERAL, + N_("scale factor of %u without an index register"), + yasm_intnum_get_uint(scale)); + yasm_intnum_destroy(scale); + } else + e2 = p_expr_new(yasm_expr_expr(e2), YASM_EXPR_ADD, + yasm_expr_expr(p_expr_new(yasm_expr_reg(reg), YASM_EXPR_MUL, + yasm_expr_int(scale)))); + } + + if (e1) { + /* Ordering is critical here to correctly detecting presence of + * RIP in RIP-relative expressions. + */ + e1 = p_expr_new_tree(e2, YASM_EXPR_ADD, e1); + } else + e1 = e2; + strong = 1; } if (!e1) - return NULL; + return NULL; ea = yasm_arch_ea_create(p_object->arch, e1); if (strong) - yasm_ea_set_strong(ea, 1); + yasm_ea_set_strong(ea, 1); return ea; } @@ -966,84 +966,84 @@ parse_operand(yasm_parser_gas *parser_gas) uintptr_t reg; switch (curtok) { - case REG: - reg = REG_val[0]; - get_next_token(); /* REG */ - return yasm_operand_create_reg(reg); - case SEGREG: - /* need to see if it's really a memory address */ - get_peek_token(parser_gas); - if (parser_gas->peek_token == ':') { - ea = parse_memaddr(parser_gas); - if (!ea) - return NULL; - return yasm_operand_create_mem(ea); - } - reg = SEGREG_val[0]; - get_next_token(); /* SEGREG */ - return yasm_operand_create_segreg(reg); - case REGGROUP: - { - unsigned long regindex; - reg = REGGROUP_val[0]; - get_next_token(); /* REGGROUP */ - if (curtok != '(') - return yasm_operand_create_reg(reg); - get_next_token(); /* '(' */ - if (!expect(INTNUM)) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("integer register index expected")); - return NULL; - } - regindex = yasm_intnum_get_uint(INTNUM_val); - get_next_token(); /* INTNUM */ - if (!expect(')')) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("missing closing parenthesis for register index")); - return NULL; - } - get_next_token(); /* ')' */ - reg = yasm_arch_reggroup_get_reg(p_object->arch, reg, regindex); - if (reg == 0) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("bad register index `%u'"), - regindex); - return NULL; - } - return yasm_operand_create_reg(reg); - } - case '$': - { - yasm_expr *e; - get_next_token(); /* '$' */ - e = parse_expr(parser_gas); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression missing after `%s'"), "$"); - return NULL; - } - return yasm_operand_create_imm(e); - } - case '*': - get_next_token(); /* '*' */ - if (curtok == REG) { - op = yasm_operand_create_reg(REG_val[0]); - get_next_token(); /* REG */ - } else { - ea = parse_memaddr(parser_gas); - if (!ea) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression missing after `%s'"), "*"); - return NULL; - } - op = yasm_operand_create_mem(ea); - } - op->deref = 1; - return op; - default: - ea = parse_memaddr(parser_gas); - if (!ea) - return NULL; - return yasm_operand_create_mem(ea); + case REG: + reg = REG_val[0]; + get_next_token(); /* REG */ + return yasm_operand_create_reg(reg); + case SEGREG: + /* need to see if it's really a memory address */ + get_peek_token(parser_gas); + if (parser_gas->peek_token == ':') { + ea = parse_memaddr(parser_gas); + if (!ea) + return NULL; + return yasm_operand_create_mem(ea); + } + reg = SEGREG_val[0]; + get_next_token(); /* SEGREG */ + return yasm_operand_create_segreg(reg); + case REGGROUP: + { + unsigned long regindex; + reg = REGGROUP_val[0]; + get_next_token(); /* REGGROUP */ + if (curtok != '(') + return yasm_operand_create_reg(reg); + get_next_token(); /* '(' */ + if (!expect(INTNUM)) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("integer register index expected")); + return NULL; + } + regindex = yasm_intnum_get_uint(INTNUM_val); + get_next_token(); /* INTNUM */ + if (!expect(')')) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("missing closing parenthesis for register index")); + return NULL; + } + get_next_token(); /* ')' */ + reg = yasm_arch_reggroup_get_reg(p_object->arch, reg, regindex); + if (reg == 0) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("bad register index `%u'"), + regindex); + return NULL; + } + return yasm_operand_create_reg(reg); + } + case '$': + { + yasm_expr *e; + get_next_token(); /* '$' */ + e = parse_expr(parser_gas); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression missing after `%s'"), "$"); + return NULL; + } + return yasm_operand_create_imm(e); + } + case '*': + get_next_token(); /* '*' */ + if (curtok == REG) { + op = yasm_operand_create_reg(REG_val[0]); + get_next_token(); /* REG */ + } else { + ea = parse_memaddr(parser_gas); + if (!ea) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression missing after `%s'"), "*"); + return NULL; + } + op = yasm_operand_create_mem(ea); + } + op->deref = 1; + return op; + default: + ea = parse_memaddr(parser_gas); + if (!ea) + return NULL; + return yasm_operand_create_mem(ea); } } @@ -1064,21 +1064,21 @@ parse_expr(yasm_parser_gas *parser_gas) yasm_expr *e, *f; e = parse_expr0(parser_gas); if (!e) - return NULL; + return NULL; while (curtok == '+' || curtok == '-') { - int op = curtok; - get_next_token(); - f = parse_expr0(parser_gas); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (op) { - case '+': e = p_expr_new_tree(e, YASM_EXPR_ADD, f); break; - case '-': e = p_expr_new_tree(e, YASM_EXPR_SUB, f); break; - } + int op = curtok; + get_next_token(); + f = parse_expr0(parser_gas); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (op) { + case '+': e = p_expr_new_tree(e, YASM_EXPR_ADD, f); break; + case '-': e = p_expr_new_tree(e, YASM_EXPR_SUB, f); break; + } } return e; } @@ -1089,23 +1089,23 @@ parse_expr0(yasm_parser_gas *parser_gas) yasm_expr *e, *f; e = parse_expr1(parser_gas); if (!e) - return NULL; + return NULL; while (curtok == '|' || curtok == '^' || curtok == '&' || curtok == '!') { - int op = curtok; - get_next_token(); - f = parse_expr1(parser_gas); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (op) { - case '|': e = p_expr_new_tree(e, YASM_EXPR_OR, f); break; - case '^': e = p_expr_new_tree(e, YASM_EXPR_XOR, f); break; - case '&': e = p_expr_new_tree(e, YASM_EXPR_AND, f); break; - case '!': e = p_expr_new_tree(e, YASM_EXPR_NOR, f); break; - } + int op = curtok; + get_next_token(); + f = parse_expr1(parser_gas); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (op) { + case '|': e = p_expr_new_tree(e, YASM_EXPR_OR, f); break; + case '^': e = p_expr_new_tree(e, YASM_EXPR_XOR, f); break; + case '&': e = p_expr_new_tree(e, YASM_EXPR_AND, f); break; + case '!': e = p_expr_new_tree(e, YASM_EXPR_NOR, f); break; + } } return e; } @@ -1116,25 +1116,25 @@ parse_expr1(yasm_parser_gas *parser_gas) yasm_expr *e, *f; e = parse_expr2(parser_gas); if (!e) - return NULL; + return NULL; while (curtok == '*' || curtok == '/' || curtok == '%' || curtok == LEFT_OP - || curtok == RIGHT_OP) { - int op = curtok; - get_next_token(); - f = parse_expr2(parser_gas); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (op) { - case '*': e = p_expr_new_tree(e, YASM_EXPR_MUL, f); break; - case '/': e = p_expr_new_tree(e, YASM_EXPR_DIV, f); break; - case '%': e = p_expr_new_tree(e, YASM_EXPR_MOD, f); break; - case LEFT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHL, f); break; - case RIGHT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHR, f); break; - } + || curtok == RIGHT_OP) { + int op = curtok; + get_next_token(); + f = parse_expr2(parser_gas); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (op) { + case '*': e = p_expr_new_tree(e, YASM_EXPR_MUL, f); break; + case '/': e = p_expr_new_tree(e, YASM_EXPR_DIV, f); break; + case '%': e = p_expr_new_tree(e, YASM_EXPR_MOD, f); break; + case LEFT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHL, f); break; + case RIGHT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHR, f); break; + } } return e; } @@ -1146,72 +1146,72 @@ parse_expr2(yasm_parser_gas *parser_gas) yasm_symrec *sym; switch (curtok) { - case '+': - get_next_token(); - return parse_expr2(parser_gas); - case '-': - get_next_token(); - e = parse_expr2(parser_gas); - if (!e) - return NULL; - return p_expr_new_branch(YASM_EXPR_NEG, e); - case '~': - get_next_token(); - e = parse_expr2(parser_gas); - if (!e) - return NULL; - return p_expr_new_branch(YASM_EXPR_NOT, e); - case '(': - get_next_token(); - e = parse_expr(parser_gas); - if (!e) - return NULL; - if (!expect(')')) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("missing parenthesis")); - return NULL; - } - get_next_token(); - return e; - case INTNUM: - e = p_expr_new_ident(yasm_expr_int(INTNUM_val)); - get_next_token(); - return e; - case FLTNUM: - e = p_expr_new_ident(yasm_expr_float(FLTNUM_val)); - get_next_token(); - return e; - case ID: - case DIR_SECTNAME: - { - char *name = ID_val; - get_next_token(); /* ID */ - if (curtok == '@') { - /* TODO: this is needed for shared objects, e.g. sym@PLT */ - get_next_token(); /* '@' */ - if (!expect(ID)) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expected identifier after `@'")); - yasm_xfree(name); - return NULL; - } - yasm_xfree(ID_val); - get_next_token(); /* ID */ - sym = yasm_symtab_use(p_symtab, name, cur_line); - yasm_xfree(name); - return p_expr_new_ident(yasm_expr_sym(sym)); - } - - /* "." references the current assembly position */ - if (name[1] == '\0' && name[0] == '.') - sym = yasm_symtab_define_curpos(p_symtab, ".", - parser_gas->prev_bc, cur_line); - else - sym = yasm_symtab_use(p_symtab, name, cur_line); - yasm_xfree(name); - return p_expr_new_ident(yasm_expr_sym(sym)); - } - default: - return NULL; + case '+': + get_next_token(); + return parse_expr2(parser_gas); + case '-': + get_next_token(); + e = parse_expr2(parser_gas); + if (!e) + return NULL; + return p_expr_new_branch(YASM_EXPR_NEG, e); + case '~': + get_next_token(); + e = parse_expr2(parser_gas); + if (!e) + return NULL; + return p_expr_new_branch(YASM_EXPR_NOT, e); + case '(': + get_next_token(); + e = parse_expr(parser_gas); + if (!e) + return NULL; + if (!expect(')')) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("missing parenthesis")); + return NULL; + } + get_next_token(); + return e; + case INTNUM: + e = p_expr_new_ident(yasm_expr_int(INTNUM_val)); + get_next_token(); + return e; + case FLTNUM: + e = p_expr_new_ident(yasm_expr_float(FLTNUM_val)); + get_next_token(); + return e; + case ID: + case DIR_SECTNAME: + { + char *name = ID_val; + get_next_token(); /* ID */ + if (curtok == '@') { + /* TODO: this is needed for shared objects, e.g. sym@PLT */ + get_next_token(); /* '@' */ + if (!expect(ID)) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expected identifier after `@'")); + yasm_xfree(name); + return NULL; + } + yasm_xfree(ID_val); + get_next_token(); /* ID */ + sym = yasm_symtab_use(p_symtab, name, cur_line); + yasm_xfree(name); + return p_expr_new_ident(yasm_expr_sym(sym)); + } + + /* "." references the current assembly position */ + if (name[1] == '\0' && name[0] == '.') + sym = yasm_symtab_define_curpos(p_symtab, ".", + parser_gas->prev_bc, cur_line); + else + sym = yasm_symtab_use(p_symtab, name, cur_line); + yasm_xfree(name); + return p_expr_new_ident(yasm_expr_sym(sym)); + } + default: + return NULL; } } @@ -1219,44 +1219,44 @@ static void define_label(yasm_parser_gas *parser_gas, char *name, int local) { if (!local) { - if (parser_gas->locallabel_base) - yasm_xfree(parser_gas->locallabel_base); - parser_gas->locallabel_base_len = strlen(name); - parser_gas->locallabel_base = - yasm_xmalloc(parser_gas->locallabel_base_len+1); - strcpy(parser_gas->locallabel_base, name); + if (parser_gas->locallabel_base) + yasm_xfree(parser_gas->locallabel_base); + parser_gas->locallabel_base_len = strlen(name); + parser_gas->locallabel_base = + yasm_xmalloc(parser_gas->locallabel_base_len+1); + strcpy(parser_gas->locallabel_base, name); } yasm_symtab_define_label(p_symtab, name, parser_gas->prev_bc, 1, - cur_line); + cur_line); yasm_xfree(name); } static void define_lcomm(yasm_parser_gas *parser_gas, /*@only@*/ char *name, - yasm_expr *size, /*@null@*/ yasm_expr *align) + yasm_expr *size, /*@null@*/ yasm_expr *align) { /* Put into .bss section. */ /*@dependent@*/ yasm_section *bss = - gas_get_section(parser_gas, yasm__xstrdup(".bss"), NULL, NULL, NULL, 1); + gas_get_section(parser_gas, yasm__xstrdup(".bss"), NULL, NULL, NULL, 1); if (align) { - /* XXX: assume alignment is in bytes, not power-of-two */ - yasm_section_bcs_append(bss, gas_parser_align(parser_gas, bss, align, - NULL, NULL, 0)); + /* XXX: assume alignment is in bytes, not power-of-two */ + yasm_section_bcs_append(bss, gas_parser_align(parser_gas, bss, align, + NULL, NULL, 0)); } yasm_symtab_define_label(p_symtab, name, yasm_section_bcs_last(bss), 1, - cur_line); + cur_line); yasm_section_bcs_append(bss, yasm_bc_create_reserve(size, 1, cur_line)); yasm_xfree(name); } static yasm_section * gas_get_section(yasm_parser_gas *parser_gas, char *name, - /*@null@*/ char *flags, /*@null@*/ char *type, - /*@null@*/ yasm_valparamhead *objext_valparams, - int builtin) + /*@null@*/ char *flags, /*@null@*/ char *type, + /*@null@*/ yasm_valparamhead *objext_valparams, + int builtin) { yasm_valparamhead vps; yasm_valparam *vp; @@ -1268,22 +1268,22 @@ gas_get_section(yasm_parser_gas *parser_gas, char *name, yasm_vps_append(&vps, vp); if (!builtin) { - if (flags) { - gasflags = yasm_xmalloc(5+strlen(flags)); - strcpy(gasflags, "gas_"); - strcat(gasflags, flags); - } else - gasflags = yasm__xstrdup("gas_"); - vp = yasm_vp_create(gasflags, NULL); - yasm_vps_append(&vps, vp); - if (type) { - vp = yasm_vp_create(type, NULL); - yasm_vps_append(&vps, vp); - } + if (flags) { + gasflags = yasm_xmalloc(5+strlen(flags)); + strcpy(gasflags, "gas_"); + strcat(gasflags, flags); + } else + gasflags = yasm__xstrdup("gas_"); + vp = yasm_vp_create(gasflags, NULL); + yasm_vps_append(&vps, vp); + if (type) { + vp = yasm_vp_create(type, NULL); + yasm_vps_append(&vps, vp); + } } new_section = yasm_objfmt_section_switch(p_object, &vps, objext_valparams, - cur_line); + cur_line); yasm_vps_delete(&vps); return new_section; @@ -1291,86 +1291,86 @@ gas_get_section(yasm_parser_gas *parser_gas, char *name, static void gas_switch_section(yasm_parser_gas *parser_gas, char *name, - /*@null@*/ char *flags, /*@null@*/ char *type, - /*@null@*/ yasm_valparamhead *objext_valparams, - int builtin) + /*@null@*/ char *flags, /*@null@*/ char *type, + /*@null@*/ yasm_valparamhead *objext_valparams, + int builtin) { yasm_section *new_section; new_section = gas_get_section(parser_gas, yasm__xstrdup(name), flags, type, - objext_valparams, builtin); + objext_valparams, builtin); if (new_section) { - cursect = new_section; - parser_gas->prev_bc = yasm_section_bcs_last(new_section); + cursect = new_section; + parser_gas->prev_bc = yasm_section_bcs_last(new_section); } else - yasm_error_set(YASM_ERROR_GENERAL, N_("invalid section name `%s'"), - name); + yasm_error_set(YASM_ERROR_GENERAL, N_("invalid section name `%s'"), + name); yasm_xfree(name); if (objext_valparams) - yasm_vps_delete(objext_valparams); + yasm_vps_delete(objext_valparams); } static yasm_bytecode * gas_parser_align(yasm_parser_gas *parser_gas, yasm_section *sect, - yasm_expr *boundval, /*@null@*/ yasm_expr *fillval, - /*@null@*/ yasm_expr *maxskipval, int power2) + yasm_expr *boundval, /*@null@*/ yasm_expr *fillval, + /*@null@*/ yasm_expr *maxskipval, int power2) { yasm_intnum *boundintn; /* Convert power of two to number of bytes if necessary */ if (power2) - boundval = yasm_expr_create(YASM_EXPR_SHL, - yasm_expr_int(yasm_intnum_create_uint(1)), - yasm_expr_expr(boundval), cur_line); + boundval = yasm_expr_create(YASM_EXPR_SHL, + yasm_expr_int(yasm_intnum_create_uint(1)), + yasm_expr_expr(boundval), cur_line); /* Largest .align in the section specifies section alignment. */ boundintn = yasm_expr_get_intnum(&boundval, 0); if (boundintn) { - unsigned long boundint = yasm_intnum_get_uint(boundintn); + unsigned long boundint = yasm_intnum_get_uint(boundintn); - /* Alignments must be a power of two. */ - if (is_exp2(boundint)) { - if (boundint > yasm_section_get_align(sect)) - yasm_section_set_align(sect, boundint, cur_line); - } + /* Alignments must be a power of two. */ + if (is_exp2(boundint)) { + if (boundint > yasm_section_get_align(sect)) + yasm_section_set_align(sect, boundint, cur_line); + } } return yasm_bc_create_align(boundval, fillval, maxskipval, - yasm_section_is_code(sect) ? - yasm_arch_get_fill(p_object->arch) : NULL, - cur_line); + yasm_section_is_code(sect) ? + yasm_arch_get_fill(p_object->arch) : NULL, + cur_line); } static yasm_bytecode * gas_parser_dir_fill(yasm_parser_gas *parser_gas, /*@only@*/ yasm_expr *repeat, - /*@only@*/ /*@null@*/ yasm_expr *size, - /*@only@*/ /*@null@*/ yasm_expr *value) + /*@only@*/ /*@null@*/ yasm_expr *size, + /*@only@*/ /*@null@*/ yasm_expr *value) { yasm_datavalhead dvs; yasm_bytecode *bc; unsigned int ssize; if (size) { - /*@dependent@*/ /*@null@*/ yasm_intnum *intn; - intn = yasm_expr_get_intnum(&size, 0); - if (!intn) { - yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, - N_("size must be an absolute expression")); - yasm_expr_destroy(repeat); - yasm_expr_destroy(size); - if (value) - yasm_expr_destroy(value); - return NULL; - } - ssize = yasm_intnum_get_uint(intn); + /*@dependent@*/ /*@null@*/ yasm_intnum *intn; + intn = yasm_expr_get_intnum(&size, 0); + if (!intn) { + yasm_error_set(YASM_ERROR_NOT_ABSOLUTE, + N_("size must be an absolute expression")); + yasm_expr_destroy(repeat); + yasm_expr_destroy(size); + if (value) + yasm_expr_destroy(value); + return NULL; + } + ssize = yasm_intnum_get_uint(intn); } else - ssize = 1; + ssize = 1; if (!value) - value = yasm_expr_create_ident( - yasm_expr_int(yasm_intnum_create_uint(0)), cur_line); + value = yasm_expr_create_ident( + yasm_expr_int(yasm_intnum_create_uint(0)), cur_line); yasm_dvs_initialize(&dvs); yasm_dvs_append(&dvs, yasm_dv_create_expr(value)); @@ -1383,23 +1383,23 @@ gas_parser_dir_fill(yasm_parser_gas *parser_gas, /*@only@*/ yasm_expr *repeat, #if 0 static void gas_parser_directive(yasm_parser_gas *parser_gas, const char *name, - yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams) + yasm_valparamhead *valparams, + yasm_valparamhead *objext_valparams) { unsigned long line = cur_line; /* Handle (mostly) output-format independent directives here */ if (!yasm_arch_parse_directive(p_object->arch, name, valparams, - objext_valparams, parser_gas->object, line)) { - ; + objext_valparams, parser_gas->object, line)) { + ; } else if (yasm_objfmt_directive(p_object, name, valparams, - objext_valparams, line)) { - yasm_error_set(YASM_ERROR_GENERAL, N_("unrecognized directive [%s]"), - name); + objext_valparams, line)) { + yasm_error_set(YASM_ERROR_GENERAL, N_("unrecognized directive [%s]"), + name); } yasm_vps_delete(valparams); if (objext_valparams) - yasm_vps_delete(objext_valparams); + yasm_vps_delete(objext_valparams); } #endif diff --git a/modules/parsers/gas/gas-parser.c b/modules/parsers/gas/gas-parser.c index 2b79db82..0a0bd442 100644 --- a/modules/parsers/gas/gas-parser.c +++ b/modules/parsers/gas/gas-parser.c @@ -38,8 +38,8 @@ static void gas_parser_do_parse(yasm_object *object, yasm_preproc *pp, FILE *f, - int save_input, yasm_linemap *linemap, - yasm_errwarns *errwarns) + int save_input, yasm_linemap *linemap, + yasm_errwarns *errwarns) { yasm_parser_gas parser_gas; @@ -79,28 +79,28 @@ gas_parser_do_parse(yasm_object *object, yasm_preproc *pp, FILE *f, /* Check for ending inside a rept */ if (parser_gas.rept) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("rept without matching endr")); - yasm_errwarn_propagate(errwarns, parser_gas.rept->startline); + yasm_error_set(YASM_ERROR_SYNTAX, N_("rept without matching endr")); + yasm_errwarn_propagate(errwarns, parser_gas.rept->startline); } /* Check for ending inside a comment */ if (parser_gas.state == COMMENT) { - yasm_warn_set(YASM_WARN_GENERAL, N_("end of file in comment")); - /* XXX: Minus two to compensate for already having moved past the EOF - * in the linemap. - */ - yasm_errwarn_propagate(errwarns, - yasm_linemap_get_current(parser_gas.linemap)-2); + yasm_warn_set(YASM_WARN_GENERAL, N_("end of file in comment")); + /* XXX: Minus two to compensate for already having moved past the EOF + * in the linemap. + */ + yasm_errwarn_propagate(errwarns, + yasm_linemap_get_current(parser_gas.linemap)-2); } yasm_scanner_delete(&parser_gas.s); /* Free locallabel base if necessary */ if (parser_gas.locallabel_base) - yasm_xfree(parser_gas.locallabel_base); + yasm_xfree(parser_gas.locallabel_base); if (parser_gas.dir_file) - yasm_xfree(parser_gas.dir_file); + yasm_xfree(parser_gas.dir_file); } /* Define valid preprocessors to use with this parser */ diff --git a/modules/parsers/gas/gas-parser.h b/modules/parsers/gas/gas-parser.h index b9661689..5992b43e 100644 --- a/modules/parsers/gas/gas-parser.h +++ b/modules/parsers/gas/gas-parser.h @@ -30,7 +30,7 @@ #ifndef YASM_GAS_PARSER_H #define YASM_GAS_PARSER_H -#define YYCTYPE unsigned char +#define YYCTYPE unsigned char #define MAX_SAVED_LINE_LEN 80 @@ -77,30 +77,30 @@ typedef union { yasm_floatnum *flt; uintptr_t arch_data[4]; struct { - char *contents; - size_t len; + char *contents; + size_t len; } str; } yystype; #define YYSTYPE yystype typedef struct gas_rept_line { STAILQ_ENTRY(gas_rept_line) link; - YYCTYPE *data; /* line characters */ - size_t len; /* length of data */ + YYCTYPE *data; /* line characters */ + size_t len; /* length of data */ } gas_rept_line; typedef struct gas_rept { - STAILQ_HEAD(reptlinelist, gas_rept_line) lines; /* repeated lines */ - unsigned long startline; /* line number of rept directive */ - unsigned long numrept; /* number of repititions to generate */ - unsigned long numdone; /* number of repititions executed so far */ - /*@null@*/ gas_rept_line *line; /* next line to repeat */ - size_t linepos; /* position to start pulling chars from line */ - int ended; /* seen endr directive yet? */ - - YYCTYPE *oldbuf; /* saved previous fill buffer */ - size_t oldbuflen; /* previous fill buffer length */ - size_t oldbufpos; /* position in previous fill buffer */ + STAILQ_HEAD(reptlinelist, gas_rept_line) lines; /* repeated lines */ + unsigned long startline; /* line number of rept directive */ + unsigned long numrept; /* number of repititions to generate */ + unsigned long numdone; /* number of repititions executed so far */ + /*@null@*/ gas_rept_line *line; /* next line to repeat */ + size_t linepos; /* position to start pulling chars from line */ + int ended; /* seen endr directive yet? */ + + YYCTYPE *oldbuf; /* saved previous fill buffer */ + size_t oldbuflen; /* previous fill buffer length */ + size_t oldbufpos; /* position in previous fill buffer */ } gas_rept; typedef struct yasm_parser_gas { @@ -132,18 +132,18 @@ typedef struct yasm_parser_gas { yasm_scanner s; enum { - INITIAL, - COMMENT, - SECTION_DIRECTIVE, - INSTDIR + INITIAL, + COMMENT, + SECTION_DIRECTIVE, + INSTDIR } state; - int token; /* enum tokentype or any character */ + int token; /* enum tokentype or any character */ yystype tokval; - char tokch; /* first character of token */ + char tokch; /* first character of token */ /* one token of lookahead; used sparingly */ - int peek_token; /* NONE if none */ + int peek_token; /* NONE if none */ yystype peek_tokval; char peek_tokch; @@ -151,35 +151,35 @@ typedef struct yasm_parser_gas { } yasm_parser_gas; /* shorter access names to commonly used parser_gas fields */ -#define p_object (parser_gas->object) -#define p_symtab (parser_gas->object->symtab) -#define cursect (parser_gas->object->cur_section) -#define curtok (parser_gas->token) -#define curval (parser_gas->tokval) - -#define INTNUM_val (curval.intn) -#define FLTNUM_val (curval.flt) -#define STRING_val (curval.str) -#define INSN_val (curval.arch_data) -#define PREFIX_val (curval.arch_data) -#define REG_val (curval.arch_data) -#define REGGROUP_val (curval.arch_data) -#define SEGREG_val (curval.arch_data) -#define TARGETMOD_val (curval.arch_data) -#define ID_val (curval.str_val) -#define LABEL_val (curval.str_val) -#define DIR_ALIGN_val (curval.int_info) -#define DIR_ASCII_val (curval.int_info) -#define DIR_DATA_val (curval.int_info) -#define DIR_LEB128_val (curval.int_info) -#define DIR_SECTNAME_val (curval.str_val) - -#define cur_line (yasm_linemap_get_current(parser_gas->linemap)) - -#define p_expr_new(l,o,r) yasm_expr_create(o,l,r,cur_line) -#define p_expr_new_tree(l,o,r) yasm_expr_create_tree(l,o,r,cur_line) -#define p_expr_new_branch(o,r) yasm_expr_create_branch(o,r,cur_line) -#define p_expr_new_ident(r) yasm_expr_create_ident(r,cur_line) +#define p_object (parser_gas->object) +#define p_symtab (parser_gas->object->symtab) +#define cursect (parser_gas->object->cur_section) +#define curtok (parser_gas->token) +#define curval (parser_gas->tokval) + +#define INTNUM_val (curval.intn) +#define FLTNUM_val (curval.flt) +#define STRING_val (curval.str) +#define INSN_val (curval.arch_data) +#define PREFIX_val (curval.arch_data) +#define REG_val (curval.arch_data) +#define REGGROUP_val (curval.arch_data) +#define SEGREG_val (curval.arch_data) +#define TARGETMOD_val (curval.arch_data) +#define ID_val (curval.str_val) +#define LABEL_val (curval.str_val) +#define DIR_ALIGN_val (curval.int_info) +#define DIR_ASCII_val (curval.int_info) +#define DIR_DATA_val (curval.int_info) +#define DIR_LEB128_val (curval.int_info) +#define DIR_SECTNAME_val (curval.str_val) + +#define cur_line (yasm_linemap_get_current(parser_gas->linemap)) + +#define p_expr_new(l,o,r) yasm_expr_create(o,l,r,cur_line) +#define p_expr_new_tree(l,o,r) yasm_expr_create_tree(l,o,r,cur_line) +#define p_expr_new_branch(o,r) yasm_expr_create_branch(o,r,cur_line) +#define p_expr_new_ident(r) yasm_expr_create_ident(r,cur_line) void gas_parser_parse(yasm_parser_gas *parser_gas); void gas_parser_cleanup(yasm_parser_gas *parser_gas); diff --git a/modules/parsers/gas/gas-token.re b/modules/parsers/gas/gas-token.re index 52b331c3..df3886a7 100644 --- a/modules/parsers/gas/gas-token.re +++ b/modules/parsers/gas/gas-token.re @@ -36,24 +36,24 @@ RCSID("$Id$"); #include "modules/parsers/gas/gas-parser.h" -#define BSIZE 8192 +#define BSIZE 8192 -#define YYCURSOR cursor -#define YYLIMIT (s->lim) -#define YYMARKER (s->ptr) -#define YYFILL(n) {cursor = fill(parser_gas, cursor);} +#define YYCURSOR cursor +#define YYLIMIT (s->lim) +#define YYMARKER (s->ptr) +#define YYFILL(n) {cursor = fill(parser_gas, cursor);} -#define RETURN(i) {s->cur = cursor; parser_gas->tokch = s->tok[0]; \ - return i;} +#define RETURN(i) {s->cur = cursor; parser_gas->tokch = s->tok[0]; \ + return i;} -#define SCANINIT() {s->tok = cursor;} +#define SCANINIT() {s->tok = cursor;} -#define TOK ((char *)s->tok) -#define TOKLEN (size_t)(cursor-s->tok) +#define TOK ((char *)s->tok) +#define TOKLEN (size_t)(cursor-s->tok) static size_t rept_input(yasm_parser_gas *parser_gas, /*@out@*/ YYCTYPE *buf, - size_t max_size) + size_t max_size) { gas_rept *rept = parser_gas->rept; size_t numleft = max_size; @@ -61,67 +61,67 @@ rept_input(yasm_parser_gas *parser_gas, /*@out@*/ YYCTYPE *buf, /* If numrept is 0, copy out just the line end characters */ if (rept->numrept == 0) { - /* Skip first line, which contains .line */ - rept->line = STAILQ_NEXT(rept->line, link); - if (!rept->line) { - rept->numrept = 1; - rept->numdone = 1; - } - while (rept->numrept == 0 && numleft > 0) { - *bufp++ = rept->line->data[rept->line->len-1]; - rept->line = STAILQ_NEXT(rept->line, link); - if (!rept->line) { - rept->numrept = 1; - rept->numdone = 1; - } - } + /* Skip first line, which contains .line */ + rept->line = STAILQ_NEXT(rept->line, link); + if (!rept->line) { + rept->numrept = 1; + rept->numdone = 1; + } + while (rept->numrept == 0 && numleft > 0) { + *bufp++ = rept->line->data[rept->line->len-1]; + rept->line = STAILQ_NEXT(rept->line, link); + if (!rept->line) { + rept->numrept = 1; + rept->numdone = 1; + } + } } /* Copy out the previous fill buffer until we're *really* done */ if (rept->numdone == rept->numrept) { - size_t numcopy = rept->oldbuflen - rept->oldbufpos; - if (numcopy > numleft) - numcopy = numleft; - memcpy(bufp, &rept->oldbuf[rept->oldbufpos], numcopy); - numleft -= numcopy; - bufp += numcopy; - rept->oldbufpos += numcopy; - - if (rept->oldbufpos == rept->oldbuflen) { - /* Delete lines, then delete rept and clear rept state */ - gas_rept_line *cur, *next; - cur = STAILQ_FIRST(&rept->lines); - while (cur) { - next = STAILQ_NEXT(cur, link); - yasm_xfree(cur->data); - yasm_xfree(cur); - cur = next; - } - yasm_xfree(rept->oldbuf); - yasm_xfree(rept); - parser_gas->rept = NULL; - } + size_t numcopy = rept->oldbuflen - rept->oldbufpos; + if (numcopy > numleft) + numcopy = numleft; + memcpy(bufp, &rept->oldbuf[rept->oldbufpos], numcopy); + numleft -= numcopy; + bufp += numcopy; + rept->oldbufpos += numcopy; + + if (rept->oldbufpos == rept->oldbuflen) { + /* Delete lines, then delete rept and clear rept state */ + gas_rept_line *cur, *next; + cur = STAILQ_FIRST(&rept->lines); + while (cur) { + next = STAILQ_NEXT(cur, link); + yasm_xfree(cur->data); + yasm_xfree(cur); + cur = next; + } + yasm_xfree(rept->oldbuf); + yasm_xfree(rept); + parser_gas->rept = NULL; + } } while (numleft > 0 && rept->numdone < rept->numrept) { - /* Copy from line data to buf */ - size_t numcopy = rept->line->len - rept->linepos; - if (numcopy > numleft) - numcopy = numleft; - memcpy(bufp, &rept->line->data[rept->linepos], numcopy); - numleft -= numcopy; - bufp += numcopy; - rept->linepos += numcopy; - - /* Update locations if needed */ - if (rept->linepos == rept->line->len) { - rept->line = STAILQ_NEXT(rept->line, link); - rept->linepos = 0; - } - if (rept->line == NULL) { - rept->numdone++; - rept->line = STAILQ_FIRST(&rept->lines); - } + /* Copy from line data to buf */ + size_t numcopy = rept->line->len - rept->linepos; + if (numcopy > numleft) + numcopy = numleft; + memcpy(bufp, &rept->line->data[rept->linepos], numcopy); + numleft -= numcopy; + bufp += numcopy; + rept->linepos += numcopy; + + /* Update locations if needed */ + if (rept->linepos == rept->line->len) { + rept->line = STAILQ_NEXT(rept->line, link); + rept->linepos = 0; + } + if (rept->line == NULL) { + rept->numdone++; + rept->line = STAILQ_FIRST(&rept->lines); + } } return (max_size-numleft); @@ -139,46 +139,46 @@ fill(yasm_parser_gas *parser_gas, YYCTYPE *cursor) yasm_scanner *s = &parser_gas->s; int first = 0; if(!s->eof){ - size_t cnt = s->tok - s->bot; - if(cnt){ - memmove(s->bot, s->tok, (size_t)(s->lim - s->tok)); - s->tok = s->bot; - s->ptr -= cnt; - cursor -= cnt; - s->lim -= cnt; - } - if (!s->bot) - first = 1; - if((s->top - s->lim) < BSIZE){ - YYCTYPE *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE); - memcpy(buf, s->tok, (size_t)(s->lim - s->tok)); - s->tok = buf; - s->ptr = &buf[s->ptr - s->bot]; - cursor = &buf[cursor - s->bot]; - s->lim = &buf[s->lim - s->bot]; - s->top = &s->lim[BSIZE]; - if (s->bot) - yasm_xfree(s->bot); - s->bot = buf; - } - if (parser_gas->rept && parser_gas->rept->ended) { - /* Pull from rept lines instead of preproc */ - cnt = rept_input(parser_gas, s->lim, BSIZE); - } else if((cnt = yasm_preproc_input(parser_gas->preproc, - (char *)s->lim, BSIZE)) == 0) { - s->eof = &s->lim[cnt]; *s->eof++ = '\n'; - } - s->lim += cnt; - if (first && parser_gas->save_input) { - int i; - YYCTYPE *saveline; - parser_gas->save_last ^= 1; - saveline = parser_gas->save_line[parser_gas->save_last]; - /* save next line into cur_line */ - for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++) - saveline[i] = s->tok[i]; - saveline[i] = '\0'; - } + size_t cnt = s->tok - s->bot; + if(cnt){ + memmove(s->bot, s->tok, (size_t)(s->lim - s->tok)); + s->tok = s->bot; + s->ptr -= cnt; + cursor -= cnt; + s->lim -= cnt; + } + if (!s->bot) + first = 1; + if((s->top - s->lim) < BSIZE){ + YYCTYPE *buf = yasm_xmalloc((size_t)(s->lim - s->bot) + BSIZE); + memcpy(buf, s->tok, (size_t)(s->lim - s->tok)); + s->tok = buf; + s->ptr = &buf[s->ptr - s->bot]; + cursor = &buf[cursor - s->bot]; + s->lim = &buf[s->lim - s->bot]; + s->top = &s->lim[BSIZE]; + if (s->bot) + yasm_xfree(s->bot); + s->bot = buf; + } + if (parser_gas->rept && parser_gas->rept->ended) { + /* Pull from rept lines instead of preproc */ + cnt = rept_input(parser_gas, s->lim, BSIZE); + } else if((cnt = yasm_preproc_input(parser_gas->preproc, + (char *)s->lim, BSIZE)) == 0) { + s->eof = &s->lim[cnt]; *s->eof++ = '\n'; + } + s->lim += cnt; + if (first && parser_gas->save_input) { + int i; + YYCTYPE *saveline; + parser_gas->save_last ^= 1; + saveline = parser_gas->save_line[parser_gas->save_last]; + /* save next line into cur_line */ + for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++) + saveline[i] = s->tok[i]; + saveline[i] = '\0'; + } } return cursor; } @@ -195,15 +195,15 @@ save_line(yasm_parser_gas *parser_gas, YYCTYPE *cursor) /* save next line into cur_line */ if ((YYLIMIT - YYCURSOR) < 80) - YYFILL(80); + YYFILL(80); for (i=0; i<79 && &cursor[i] < s->lim && cursor[i] != '\n'; i++) - saveline[i] = cursor[i]; + saveline[i] = cursor[i]; saveline[i] = '\0'; return cursor; } /* starting size of string buffer */ -#define STRBUF_ALLOC_SIZE 128 +#define STRBUF_ALLOC_SIZE 128 /* string buffer used when parsing strings/character constants */ static YYCTYPE *strbuf = NULL; @@ -215,8 +215,8 @@ static void strbuf_append(size_t count, YYCTYPE *cursor, yasm_scanner *s, int ch) { if (count >= strbuf_size) { - strbuf = yasm_xrealloc(strbuf, strbuf_size + STRBUF_ALLOC_SIZE); - strbuf_size += STRBUF_ALLOC_SIZE; + strbuf = yasm_xrealloc(strbuf, strbuf_size + STRBUF_ALLOC_SIZE); + strbuf_size += STRBUF_ALLOC_SIZE; } strbuf[count] = ch; } @@ -246,339 +246,339 @@ gas_parser_lex(YYSTYPE *lvalp, yasm_parser_gas *parser_gas) /* Handle one token of lookahead */ if (parser_gas->peek_token != NONE) { - int tok = parser_gas->peek_token; - *lvalp = parser_gas->peek_tokval; /* structure copy */ - parser_gas->tokch = parser_gas->peek_tokch; - parser_gas->peek_token = NONE; - return tok; + int tok = parser_gas->peek_token; + *lvalp = parser_gas->peek_tokval; /* structure copy */ + parser_gas->tokch = parser_gas->peek_tokch; + parser_gas->peek_token = NONE; + return tok; } /* Catch EOF */ if (s->eof && cursor == s->eof) - return 0; + return 0; /* Handle rept */ if (rept && !rept->ended) - goto rept_directive; + goto rept_directive; /* Jump to proper "exclusive" states */ switch (parser_gas->state) { - case COMMENT: - goto comment; - case SECTION_DIRECTIVE: - goto section_directive; - default: - break; + case COMMENT: + goto comment; + case SECTION_DIRECTIVE: + goto section_directive; + default: + break; } scan: SCANINIT(); /*!re2c - /* standard decimal integer */ - ([1-9] digit*) | "0" { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->intn = yasm_intnum_create_dec(TOK); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - - /* 0b10010011 - binary number */ - '0b' bindigit+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->intn = yasm_intnum_create_bin(TOK+2); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - - /* 0777 - octal number */ - "0" octdigit+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->intn = yasm_intnum_create_oct(TOK); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - - /* 0xAA - hexidecimal number */ - '0x' hexdigit+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - /* skip 0 and x */ - lvalp->intn = yasm_intnum_create_hex(TOK+2); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - - /* floating point value */ - "0" [DdEeFfTt] [-+]? (digit+)? ("." digit*)? ('e' [-+]? digit+)? { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->flt = yasm_floatnum_create(TOK+2); - s->tok[TOKLEN] = savech; - RETURN(FLTNUM); - } - - /* character constant values */ - ['] { - goto charconst; - } - - /* string constant values */ - dquot { - goto stringconst; - } - - /* operators */ - "<<" { RETURN(LEFT_OP); } - ">>" { RETURN(RIGHT_OP); } - "<" { RETURN(LEFT_OP); } - ">" { RETURN(RIGHT_OP); } - [-+|^!*&/~$():@=,] { RETURN(s->tok[0]); } - ";" { - parser_gas->state = INITIAL; - RETURN(s->tok[0]); - } - - /* arch-independent directives */ - /* alignment directives */ - '.align' { - /* FIXME: Whether this is power-of-two or not depends on arch and - * objfmt. - */ - lvalp->int_info = 0; - parser_gas->state = INSTDIR; RETURN(DIR_ALIGN); - } - '.p2align' { - lvalp->int_info = 1; - parser_gas->state = INSTDIR; RETURN(DIR_ALIGN); - } - '.balign' { - lvalp->int_info = 0; - parser_gas->state = INSTDIR; RETURN(DIR_ALIGN); - } - '.org' { parser_gas->state = INSTDIR; RETURN(DIR_ORG); } - /* data visibility directives */ - '.local' { parser_gas->state = INSTDIR; RETURN(DIR_LOCAL); } - '.comm' { parser_gas->state = INSTDIR; RETURN(DIR_COMM); } - '.lcomm' { parser_gas->state = INSTDIR; RETURN(DIR_LCOMM); } - /* integer data declaration directives */ - '.byte' { - lvalp->int_info = 1; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.2byte' { - lvalp->int_info = 2; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.4byte' { - lvalp->int_info = 4; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.8byte' { - lvalp->int_info = 8; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.16byte' { - lvalp->int_info = 16; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.short' { - lvalp->int_info = 2; /* TODO: This should depend on arch */ - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.int' { - lvalp->int_info = 4; /* TODO: This should depend on arch */ - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.long' { - lvalp->int_info = 4; /* TODO: This should depend on arch */ - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.hword' { - lvalp->int_info = 2; /* TODO: This should depend on arch */ - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.word' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)/8; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.quad' { - lvalp->int_info = 8; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.octa' { - lvalp->int_info = 16; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.value' { - lvalp->int_info = 2; /* XXX: At least on x86, this is 2 bytes */ - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - /* ASCII data declaration directives */ - '.ascii' { - lvalp->int_info = 0; /* do not add terminating zero */ - parser_gas->state = INSTDIR; RETURN(DIR_ASCII); - } - '.asciz' { - lvalp->int_info = 1; /* add terminating zero */ - parser_gas->state = INSTDIR; RETURN(DIR_ASCII); - } - '.string' { - lvalp->int_info = 1; /* add terminating zero */ - parser_gas->state = INSTDIR; RETURN(DIR_ASCII); - } - /* LEB128 integer data declaration directives */ - '.sleb128' { - lvalp->int_info = 1; /* signed */ - parser_gas->state = INSTDIR; RETURN(DIR_LEB128); - } - '.uleb128' { - lvalp->int_info = 0; /* unsigned */ - parser_gas->state = INSTDIR; RETURN(DIR_LEB128); - } - /* floating point data declaration directives */ - '.float' { - lvalp->int_info = 4; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.single' { - lvalp->int_info = 4; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.double' { - lvalp->int_info = 8; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - '.tfloat' { - lvalp->int_info = 10; - parser_gas->state = INSTDIR; RETURN(DIR_DATA); - } - /* section directives */ - '.bss' { - lvalp->str_val = yasm__xstrdup(".bss"); - RETURN(DIR_SECTNAME); - } - '.data' { - lvalp->str_val = yasm__xstrdup(".data"); - RETURN(DIR_SECTNAME); - } - '.text' { - lvalp->str_val = yasm__xstrdup(".text"); - RETURN(DIR_SECTNAME); - } - '.section' { - parser_gas->state = SECTION_DIRECTIVE; - RETURN(DIR_SECTION); - } - /* macro directives */ - '.rept' { parser_gas->state = INSTDIR; RETURN(DIR_REPT); } - '.endr' { parser_gas->state = INSTDIR; RETURN(DIR_ENDR); } - /* empty space/fill directives */ - '.skip' { parser_gas->state = INSTDIR; RETURN(DIR_SKIP); } - '.space' { parser_gas->state = INSTDIR; RETURN(DIR_SKIP); } - '.fill' { parser_gas->state = INSTDIR; RETURN(DIR_FILL); } - '.zero' { parser_gas->state = INSTDIR; RETURN(DIR_ZERO); } - /* other directives */ - '.equ' { parser_gas->state = INSTDIR; RETURN(DIR_EQU); } - '.file' { parser_gas->state = INSTDIR; RETURN(DIR_FILE); } - '.line' { parser_gas->state = INSTDIR; RETURN(DIR_LINE); } - '.set' { parser_gas->state = INSTDIR; RETURN(DIR_EQU); } - - /* label or maybe directive */ - [_.][a-zA-Z0-9_$.]* { - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - RETURN(ID); - } - - /* register or segment register */ - [%][a-zA-Z0-9]+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - switch (yasm_arch_parse_check_regtmod - (p_object->arch, lvalp->arch_data, TOK+1, TOKLEN-1)) { - case YASM_ARCH_REG: - s->tok[TOKLEN] = savech; - RETURN(REG); - case YASM_ARCH_REGGROUP: - s->tok[TOKLEN] = savech; - RETURN(REGGROUP); - case YASM_ARCH_SEGREG: - s->tok[TOKLEN] = savech; - RETURN(SEGREG); - default: - break; - } - yasm_error_set(YASM_ERROR_GENERAL, - N_("Unrecognized register name `%s'"), s->tok); - s->tok[TOKLEN] = savech; - lvalp->arch_data[0] = 0; - lvalp->arch_data[1] = 0; - lvalp->arch_data[2] = 0; - lvalp->arch_data[3] = 0; - RETURN(REG); - } - - /* label */ - [a-zA-Z][a-zA-Z0-9_$.]* ws* ':' { - /* strip off colon and any whitespace */ - count = TOKLEN-1; - while (s->tok[count] == ' ' || s->tok[count] == '\t' - || s->tok[count] == '\r') - count--; - /* Just an identifier, return as such. */ - lvalp->str_val = yasm__xstrndup(TOK, count); - RETURN(LABEL); - } - - /* identifier that may be an instruction, etc. */ - [a-zA-Z][a-zA-Z0-9_$.]* { - /* Can only be an instruction/prefix when not inside an - * instruction or directive. - */ - if (parser_gas->state != INSTDIR) { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - switch (yasm_arch_parse_check_insnprefix - (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { - case YASM_ARCH_INSN: - s->tok[TOKLEN] = savech; - parser_gas->state = INSTDIR; - RETURN(INSN); - case YASM_ARCH_PREFIX: - s->tok[TOKLEN] = savech; - RETURN(PREFIX); - default: - s->tok[TOKLEN] = savech; - } - } - /* Propagate errors in case we got a warning from the arch */ - yasm_errwarn_propagate(parser_gas->errwarns, cur_line); - /* Just an identifier, return as such. */ - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - RETURN(ID); - } - - "/*" { parser_gas->state = COMMENT; goto comment; } - "#" (any \ [\n])* { goto scan; } - - ws+ { goto scan; } - - "\n" { - if (parser_gas->save_input) - cursor = save_line(parser_gas, cursor); - parser_gas->state = INITIAL; - RETURN(s->tok[0]); - } - - any { - yasm_warn_set(YASM_WARN_UNREC_CHAR, - N_("ignoring unrecognized character `%s'"), - yasm__conv_unprint(s->tok[0])); - goto scan; - } + /* standard decimal integer */ + ([1-9] digit*) | "0" { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->intn = yasm_intnum_create_dec(TOK); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + + /* 0b10010011 - binary number */ + '0b' bindigit+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->intn = yasm_intnum_create_bin(TOK+2); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + + /* 0777 - octal number */ + "0" octdigit+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->intn = yasm_intnum_create_oct(TOK); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + + /* 0xAA - hexidecimal number */ + '0x' hexdigit+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + /* skip 0 and x */ + lvalp->intn = yasm_intnum_create_hex(TOK+2); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + + /* floating point value */ + "0" [DdEeFfTt] [-+]? (digit+)? ("." digit*)? ('e' [-+]? digit+)? { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->flt = yasm_floatnum_create(TOK+2); + s->tok[TOKLEN] = savech; + RETURN(FLTNUM); + } + + /* character constant values */ + ['] { + goto charconst; + } + + /* string constant values */ + dquot { + goto stringconst; + } + + /* operators */ + "<<" { RETURN(LEFT_OP); } + ">>" { RETURN(RIGHT_OP); } + "<" { RETURN(LEFT_OP); } + ">" { RETURN(RIGHT_OP); } + [-+|^!*&/~$():@=,] { RETURN(s->tok[0]); } + ";" { + parser_gas->state = INITIAL; + RETURN(s->tok[0]); + } + + /* arch-independent directives */ + /* alignment directives */ + '.align' { + /* FIXME: Whether this is power-of-two or not depends on arch and + * objfmt. + */ + lvalp->int_info = 0; + parser_gas->state = INSTDIR; RETURN(DIR_ALIGN); + } + '.p2align' { + lvalp->int_info = 1; + parser_gas->state = INSTDIR; RETURN(DIR_ALIGN); + } + '.balign' { + lvalp->int_info = 0; + parser_gas->state = INSTDIR; RETURN(DIR_ALIGN); + } + '.org' { parser_gas->state = INSTDIR; RETURN(DIR_ORG); } + /* data visibility directives */ + '.local' { parser_gas->state = INSTDIR; RETURN(DIR_LOCAL); } + '.comm' { parser_gas->state = INSTDIR; RETURN(DIR_COMM); } + '.lcomm' { parser_gas->state = INSTDIR; RETURN(DIR_LCOMM); } + /* integer data declaration directives */ + '.byte' { + lvalp->int_info = 1; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.2byte' { + lvalp->int_info = 2; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.4byte' { + lvalp->int_info = 4; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.8byte' { + lvalp->int_info = 8; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.16byte' { + lvalp->int_info = 16; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.short' { + lvalp->int_info = 2; /* TODO: This should depend on arch */ + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.int' { + lvalp->int_info = 4; /* TODO: This should depend on arch */ + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.long' { + lvalp->int_info = 4; /* TODO: This should depend on arch */ + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.hword' { + lvalp->int_info = 2; /* TODO: This should depend on arch */ + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.word' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)/8; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.quad' { + lvalp->int_info = 8; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.octa' { + lvalp->int_info = 16; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.value' { + lvalp->int_info = 2; /* XXX: At least on x86, this is 2 bytes */ + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + /* ASCII data declaration directives */ + '.ascii' { + lvalp->int_info = 0; /* do not add terminating zero */ + parser_gas->state = INSTDIR; RETURN(DIR_ASCII); + } + '.asciz' { + lvalp->int_info = 1; /* add terminating zero */ + parser_gas->state = INSTDIR; RETURN(DIR_ASCII); + } + '.string' { + lvalp->int_info = 1; /* add terminating zero */ + parser_gas->state = INSTDIR; RETURN(DIR_ASCII); + } + /* LEB128 integer data declaration directives */ + '.sleb128' { + lvalp->int_info = 1; /* signed */ + parser_gas->state = INSTDIR; RETURN(DIR_LEB128); + } + '.uleb128' { + lvalp->int_info = 0; /* unsigned */ + parser_gas->state = INSTDIR; RETURN(DIR_LEB128); + } + /* floating point data declaration directives */ + '.float' { + lvalp->int_info = 4; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.single' { + lvalp->int_info = 4; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.double' { + lvalp->int_info = 8; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + '.tfloat' { + lvalp->int_info = 10; + parser_gas->state = INSTDIR; RETURN(DIR_DATA); + } + /* section directives */ + '.bss' { + lvalp->str_val = yasm__xstrdup(".bss"); + RETURN(DIR_SECTNAME); + } + '.data' { + lvalp->str_val = yasm__xstrdup(".data"); + RETURN(DIR_SECTNAME); + } + '.text' { + lvalp->str_val = yasm__xstrdup(".text"); + RETURN(DIR_SECTNAME); + } + '.section' { + parser_gas->state = SECTION_DIRECTIVE; + RETURN(DIR_SECTION); + } + /* macro directives */ + '.rept' { parser_gas->state = INSTDIR; RETURN(DIR_REPT); } + '.endr' { parser_gas->state = INSTDIR; RETURN(DIR_ENDR); } + /* empty space/fill directives */ + '.skip' { parser_gas->state = INSTDIR; RETURN(DIR_SKIP); } + '.space' { parser_gas->state = INSTDIR; RETURN(DIR_SKIP); } + '.fill' { parser_gas->state = INSTDIR; RETURN(DIR_FILL); } + '.zero' { parser_gas->state = INSTDIR; RETURN(DIR_ZERO); } + /* other directives */ + '.equ' { parser_gas->state = INSTDIR; RETURN(DIR_EQU); } + '.file' { parser_gas->state = INSTDIR; RETURN(DIR_FILE); } + '.line' { parser_gas->state = INSTDIR; RETURN(DIR_LINE); } + '.set' { parser_gas->state = INSTDIR; RETURN(DIR_EQU); } + + /* label or maybe directive */ + [_.][a-zA-Z0-9_$.]* { + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + RETURN(ID); + } + + /* register or segment register */ + [%][a-zA-Z0-9]+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + switch (yasm_arch_parse_check_regtmod + (p_object->arch, lvalp->arch_data, TOK+1, TOKLEN-1)) { + case YASM_ARCH_REG: + s->tok[TOKLEN] = savech; + RETURN(REG); + case YASM_ARCH_REGGROUP: + s->tok[TOKLEN] = savech; + RETURN(REGGROUP); + case YASM_ARCH_SEGREG: + s->tok[TOKLEN] = savech; + RETURN(SEGREG); + default: + break; + } + yasm_error_set(YASM_ERROR_GENERAL, + N_("Unrecognized register name `%s'"), s->tok); + s->tok[TOKLEN] = savech; + lvalp->arch_data[0] = 0; + lvalp->arch_data[1] = 0; + lvalp->arch_data[2] = 0; + lvalp->arch_data[3] = 0; + RETURN(REG); + } + + /* label */ + [a-zA-Z][a-zA-Z0-9_$.]* ws* ':' { + /* strip off colon and any whitespace */ + count = TOKLEN-1; + while (s->tok[count] == ' ' || s->tok[count] == '\t' + || s->tok[count] == '\r') + count--; + /* Just an identifier, return as such. */ + lvalp->str_val = yasm__xstrndup(TOK, count); + RETURN(LABEL); + } + + /* identifier that may be an instruction, etc. */ + [a-zA-Z][a-zA-Z0-9_$.]* { + /* Can only be an instruction/prefix when not inside an + * instruction or directive. + */ + if (parser_gas->state != INSTDIR) { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + switch (yasm_arch_parse_check_insnprefix + (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { + case YASM_ARCH_INSN: + s->tok[TOKLEN] = savech; + parser_gas->state = INSTDIR; + RETURN(INSN); + case YASM_ARCH_PREFIX: + s->tok[TOKLEN] = savech; + RETURN(PREFIX); + default: + s->tok[TOKLEN] = savech; + } + } + /* Propagate errors in case we got a warning from the arch */ + yasm_errwarn_propagate(parser_gas->errwarns, cur_line); + /* Just an identifier, return as such. */ + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + RETURN(ID); + } + + "/*" { parser_gas->state = COMMENT; goto comment; } + "#" (any \ [\n])* { goto scan; } + + ws+ { goto scan; } + + "\n" { + if (parser_gas->save_input) + cursor = save_line(parser_gas, cursor); + parser_gas->state = INITIAL; + RETURN(s->tok[0]); + } + + any { + yasm_warn_set(YASM_WARN_UNREC_CHAR, + N_("ignoring unrecognized character `%s'"), + yasm__conv_unprint(s->tok[0])); + goto scan; + } */ /* C-style comment; nesting not supported */ @@ -586,20 +586,20 @@ comment: SCANINIT(); /*!re2c - /* End of comment */ - "*/" { parser_gas->state = INITIAL; goto scan; } - - "\n" { - if (parser_gas->save_input) - cursor = save_line(parser_gas, cursor); - RETURN(s->tok[0]); - } - - any { - if (cursor == s->eof) - return 0; - goto comment; - } + /* End of comment */ + "*/" { parser_gas->state = INITIAL; goto scan; } + + "\n" { + if (parser_gas->save_input) + cursor = save_line(parser_gas, cursor); + RETURN(s->tok[0]); + } + + any { + if (cursor == s->eof) + return 0; + goto comment; + } */ /* .section directive (the section name portion thereof) */ @@ -607,34 +607,34 @@ section_directive: SCANINIT(); /*!re2c - [a-zA-Z0-9_$.-]+ { - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - parser_gas->state = INITIAL; - RETURN(ID); - } - - dquot { goto stringconst; } - - ws+ { goto section_directive; } - - "," { - parser_gas->state = INITIAL; - RETURN(s->tok[0]); - } - - "\n" { - if (parser_gas->save_input) - cursor = save_line(parser_gas, cursor); - parser_gas->state = INITIAL; - RETURN(s->tok[0]); - } - - any { - yasm_warn_set(YASM_WARN_UNREC_CHAR, - N_("ignoring unrecognized character `%s'"), - yasm__conv_unprint(s->tok[0])); - goto section_directive; - } + [a-zA-Z0-9_$.-]+ { + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + parser_gas->state = INITIAL; + RETURN(ID); + } + + dquot { goto stringconst; } + + ws+ { goto section_directive; } + + "," { + parser_gas->state = INITIAL; + RETURN(s->tok[0]); + } + + "\n" { + if (parser_gas->save_input) + cursor = save_line(parser_gas, cursor); + parser_gas->state = INITIAL; + RETURN(s->tok[0]); + } + + any { + yasm_warn_set(YASM_WARN_UNREC_CHAR, + N_("ignoring unrecognized character `%s'"), + yasm__conv_unprint(s->tok[0])); + goto section_directive; + } */ /* character constant values */ @@ -651,38 +651,38 @@ stringconst_scan: SCANINIT(); /*!re2c - /* Handle escaped double-quote by copying and continuing */ - "\\\"" { - if (cursor == s->eof) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("unexpected end of file in string")); - lvalp->str.contents = (char *)strbuf; - lvalp->str.len = count; - RETURN(STRING); - } - strbuf_append(count++, cursor, s, '"'); - goto stringconst_scan; - } - - dquot { - strbuf_append(count, cursor, s, '\0'); - yasm_unescape_cstring(strbuf, &count); - lvalp->str.contents = (char *)strbuf; - lvalp->str.len = count; - RETURN(STRING); - } - - any { - if (cursor == s->eof) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("unexpected end of file in string")); - lvalp->str.contents = (char *)strbuf; - lvalp->str.len = count; - RETURN(STRING); - } - strbuf_append(count++, cursor, s, s->tok[0]); - goto stringconst_scan; - } + /* Handle escaped double-quote by copying and continuing */ + "\\\"" { + if (cursor == s->eof) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("unexpected end of file in string")); + lvalp->str.contents = (char *)strbuf; + lvalp->str.len = count; + RETURN(STRING); + } + strbuf_append(count++, cursor, s, '"'); + goto stringconst_scan; + } + + dquot { + strbuf_append(count, cursor, s, '\0'); + yasm_unescape_cstring(strbuf, &count); + lvalp->str.contents = (char *)strbuf; + lvalp->str.len = count; + RETURN(STRING); + } + + any { + if (cursor == s->eof) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("unexpected end of file in string")); + lvalp->str.contents = (char *)strbuf; + lvalp->str.len = count; + RETURN(STRING); + } + strbuf_append(count++, cursor, s, s->tok[0]); + goto stringconst_scan; + } */ rept_directive: @@ -696,94 +696,94 @@ rept_scan: SCANINIT(); /*!re2c - [\n;] { - /* Line ending, save in lines */ - new_line = yasm_xmalloc(sizeof(gas_rept_line)); - if (cursor == s->eof) { - yasm_xfree(strbuf); - return 0; - } - strbuf_append(count++, cursor, s, s->tok[0]); - new_line->data = strbuf; - new_line->len = count; - STAILQ_INSERT_TAIL(&rept->lines, new_line, link); - /* Allocate new strbuf */ - strbuf = yasm_xmalloc(STRBUF_ALLOC_SIZE); - strbuf_size = STRBUF_ALLOC_SIZE; - count = 0; - /* Mark start of line */ - linestart = 1; - goto rept_scan; - } - '.rept' { - int i; - if (linestart) { - /* We don't support nested right now, error */ - yasm_error_set(YASM_ERROR_GENERAL, - N_("nested rept not supported")); - yasm_errwarn_propagate(parser_gas->errwarns, cur_line); - } - for (i=0; i<6; i++) - strbuf_append(count++, cursor, s, s->tok[i]); - goto rept_scan; - } - '.endr' { - if (linestart) { - /* We're done, kick off the main lexer */ - rept->line = STAILQ_FIRST(&rept->lines); - if (!rept->line) { - /* Didn't get any intervening data? Empty repeat, so - * don't even bother. - */ - yasm_xfree(strbuf); - yasm_xfree(rept); - parser_gas->rept = NULL; - } else { - rept->ended = 1; - - /* Add .line as first line to get line numbers correct */ - new_line = yasm_xmalloc(sizeof(gas_rept_line)); - new_line->data = yasm_xmalloc(40); - sprintf((char *)new_line->data, ".line %lu;", - rept->startline+1); - new_line->len = strlen((char *)new_line->data); - STAILQ_INSERT_HEAD(&rept->lines, new_line, link); - - /* Save previous fill buffer */ - rept->oldbuf = parser_gas->s.bot; - rept->oldbuflen = s->lim - s->bot; - rept->oldbufpos = cursor - s->bot; - - /* Reset fill */ - s->bot = NULL; - s->tok = NULL; - s->ptr = NULL; - s->cur = NULL; - s->lim = NULL; - s->top = NULL; - s->eof = NULL; - cursor = NULL; - YYFILL(1); - } - - goto scan; - } else { - int i; - for (i=0; i<6; i++) - strbuf_append(count++, cursor, s, s->tok[i]); - goto rept_scan; - } - } - - any { - if (cursor == s->eof) { - yasm_xfree(strbuf); - return 0; - } - strbuf_append(count++, cursor, s, s->tok[0]); - linestart = 0; - goto rept_scan; - } + [\n;] { + /* Line ending, save in lines */ + new_line = yasm_xmalloc(sizeof(gas_rept_line)); + if (cursor == s->eof) { + yasm_xfree(strbuf); + return 0; + } + strbuf_append(count++, cursor, s, s->tok[0]); + new_line->data = strbuf; + new_line->len = count; + STAILQ_INSERT_TAIL(&rept->lines, new_line, link); + /* Allocate new strbuf */ + strbuf = yasm_xmalloc(STRBUF_ALLOC_SIZE); + strbuf_size = STRBUF_ALLOC_SIZE; + count = 0; + /* Mark start of line */ + linestart = 1; + goto rept_scan; + } + '.rept' { + int i; + if (linestart) { + /* We don't support nested right now, error */ + yasm_error_set(YASM_ERROR_GENERAL, + N_("nested rept not supported")); + yasm_errwarn_propagate(parser_gas->errwarns, cur_line); + } + for (i=0; i<6; i++) + strbuf_append(count++, cursor, s, s->tok[i]); + goto rept_scan; + } + '.endr' { + if (linestart) { + /* We're done, kick off the main lexer */ + rept->line = STAILQ_FIRST(&rept->lines); + if (!rept->line) { + /* Didn't get any intervening data? Empty repeat, so + * don't even bother. + */ + yasm_xfree(strbuf); + yasm_xfree(rept); + parser_gas->rept = NULL; + } else { + rept->ended = 1; + + /* Add .line as first line to get line numbers correct */ + new_line = yasm_xmalloc(sizeof(gas_rept_line)); + new_line->data = yasm_xmalloc(40); + sprintf((char *)new_line->data, ".line %lu;", + rept->startline+1); + new_line->len = strlen((char *)new_line->data); + STAILQ_INSERT_HEAD(&rept->lines, new_line, link); + + /* Save previous fill buffer */ + rept->oldbuf = parser_gas->s.bot; + rept->oldbuflen = s->lim - s->bot; + rept->oldbufpos = cursor - s->bot; + + /* Reset fill */ + s->bot = NULL; + s->tok = NULL; + s->ptr = NULL; + s->cur = NULL; + s->lim = NULL; + s->top = NULL; + s->eof = NULL; + cursor = NULL; + YYFILL(1); + } + + goto scan; + } else { + int i; + for (i=0; i<6; i++) + strbuf_append(count++, cursor, s, s->tok[i]); + goto rept_scan; + } + } + + any { + if (cursor == s->eof) { + yasm_xfree(strbuf); + return 0; + } + strbuf_append(count++, cursor, s, s->tok[0]); + linestart = 0; + goto rept_scan; + } */ } diff --git a/modules/parsers/nasm/nasm-parse.c b/modules/parsers/nasm/nasm-parse.c index f1e41934..486a5b9a 100644 --- a/modules/parsers/nasm/nasm-parse.c +++ b/modules/parsers/nasm/nasm-parse.c @@ -45,7 +45,7 @@ typedef enum { static yasm_bytecode *parse_line(yasm_parser_nasm *parser_nasm); static int parse_directive_valparams(yasm_parser_nasm *parser_nasm, - /*@out@*/ yasm_valparamhead *vps); + /*@out@*/ yasm_valparamhead *vps); static yasm_bytecode *parse_times(yasm_parser_nasm *parser_nasm); static yasm_bytecode *parse_exp(yasm_parser_nasm *parser_nasm); static yasm_bytecode *parse_instr(yasm_parser_nasm *parser_nasm); @@ -66,12 +66,12 @@ static void nasm_parser_directive /*@null@*/ yasm_valparamhead *valparams, /*@null@*/ yasm_valparamhead *objext_valparams); static int fix_directive_symrec(/*@null@*/ yasm_expr__item *ei, - /*@null@*/ void *d); + /*@null@*/ void *d); static void define_label(yasm_parser_nasm *parser_nasm, /*@only@*/ char *name, - int local); + int local); -#define is_eol_tok(tok) ((tok) == '\n' || (tok) == 0) -#define is_eol() is_eol_tok(curtok) +#define is_eol_tok(tok) ((tok) == '\n' || (tok) == 0) +#define is_eol() is_eol_tok(curtok) #define get_next_token() (curtok = nasm_parser_lex(&curval, parser_nasm)) @@ -80,9 +80,9 @@ get_peek_token(yasm_parser_nasm *parser_nasm) { char savech = parser_nasm->tokch; if (parser_nasm->peek_token != NONE) - yasm_internal_error(N_("only can have one token of lookahead")); + yasm_internal_error(N_("only can have one token of lookahead")); parser_nasm->peek_token = - nasm_parser_lex(&parser_nasm->peek_tokval, parser_nasm); + nasm_parser_lex(&parser_nasm->peek_tokval, parser_nasm); parser_nasm->peek_tokch = parser_nasm->tokch; parser_nasm->tokch = savech; } @@ -91,28 +91,28 @@ static void destroy_curtok_(yasm_parser_nasm *parser_nasm) { if (curtok < 256) - ; + ; else switch ((enum tokentype)curtok) { - case INTNUM: - yasm_intnum_destroy(curval.intn); - break; - case FLTNUM: - yasm_floatnum_destroy(curval.flt); - break; - case DIRECTIVE_NAME: - case FILENAME: - case ID: - case LOCAL_ID: - case SPECIAL_ID: - yasm_xfree(curval.str_val); - break; - case STRING: - yasm_xfree(curval.str.contents); - break; - default: - break; + case INTNUM: + yasm_intnum_destroy(curval.intn); + break; + case FLTNUM: + yasm_floatnum_destroy(curval.flt); + break; + case DIRECTIVE_NAME: + case FILENAME: + case ID: + case LOCAL_ID: + case SPECIAL_ID: + yasm_xfree(curval.str_val); + break; + case STRING: + yasm_xfree(curval.str.contents); + break; + default: + break; } - curtok = NONE; /* sanity */ + curtok = NONE; /* sanity */ } #define destroy_curtok() destroy_curtok_(parser_nasm) @@ -123,15 +123,15 @@ static void demand_eol_(yasm_parser_nasm *parser_nasm) { if (is_eol()) - return; + return; yasm_error_set(YASM_ERROR_SYNTAX, - N_("junk at end of line, first unrecognized character is `%c'"), - parser_nasm->tokch); + N_("junk at end of line, first unrecognized character is `%c'"), + parser_nasm->tokch); do { - destroy_curtok(); - get_next_token(); + destroy_curtok(); + get_next_token(); } while (!is_eol()); } #define demand_eol() demand_eol_(parser_nasm) @@ -143,42 +143,42 @@ expect_(yasm_parser_nasm *parser_nasm, int token) const char *str; if (curtok == token) - return 1; + return 1; switch (token) { - case INTNUM: str = "expected integer"; break; - case FLTNUM: str = "expected floating point value"; break; - case DIRECTIVE_NAME: str = "expected directive name"; break; - case FILENAME: str = "expected filename"; break; - case STRING: str = "expected string"; break; - case SIZE_OVERRIDE: str = "expected size override"; break; - case DECLARE_DATA: str = "expected DB/DW/etc."; break; - case RESERVE_SPACE: str = "expected RESB/RESW/etc."; break; - case INCBIN: str = "expected INCBIN"; break; - case EQU: str = "expected EQU"; break; - case TIMES: str = "expected TIMES"; break; - case SEG: str = "expected SEG"; break; - case WRT: str = "expected WRT"; break; - case NOSPLIT: str = "expected NOSPLIT"; break; - case STRICT: str = "expected STRICT"; break; - case INSN: str = "expected instruction"; break; - case PREFIX: str = "expected instruction prefix"; break; - case REG: str = "expected register"; break; - case SEGREG: str = "expected segment register"; break; - case TARGETMOD: str = "expected target modifier"; break; - case LEFT_OP: str = "expected <<"; break; - case RIGHT_OP: str = "expected >>"; break; - case SIGNDIV: str = "expected //"; break; - case SIGNMOD: str = "expected %%"; break; - case START_SECTION_ID: str = "expected $$"; break; - case ID: str = "expected identifier"; break; - case LOCAL_ID: str = "expected .identifier"; break; - case SPECIAL_ID: str = "expected ..identifier"; break; - case LINE: str = "expected %line"; break; - default: - strch[10] = token; - str = strch; - break; + case INTNUM: str = "expected integer"; break; + case FLTNUM: str = "expected floating point value"; break; + case DIRECTIVE_NAME: str = "expected directive name"; break; + case FILENAME: str = "expected filename"; break; + case STRING: str = "expected string"; break; + case SIZE_OVERRIDE: str = "expected size override"; break; + case DECLARE_DATA: str = "expected DB/DW/etc."; break; + case RESERVE_SPACE: str = "expected RESB/RESW/etc."; break; + case INCBIN: str = "expected INCBIN"; break; + case EQU: str = "expected EQU"; break; + case TIMES: str = "expected TIMES"; break; + case SEG: str = "expected SEG"; break; + case WRT: str = "expected WRT"; break; + case NOSPLIT: str = "expected NOSPLIT"; break; + case STRICT: str = "expected STRICT"; break; + case INSN: str = "expected instruction"; break; + case PREFIX: str = "expected instruction prefix"; break; + case REG: str = "expected register"; break; + case SEGREG: str = "expected segment register"; break; + case TARGETMOD: str = "expected target modifier"; break; + case LEFT_OP: str = "expected <<"; break; + case RIGHT_OP: str = "expected >>"; break; + case SIGNDIV: str = "expected //"; break; + case SIGNMOD: str = "expected %%"; break; + case START_SECTION_ID: str = "expected $$"; break; + case ID: str = "expected identifier"; break; + case LOCAL_ID: str = "expected .identifier"; break; + case SPECIAL_ID: str = "expected ..identifier"; break; + case LINE: str = "expected %line"; break; + default: + strch[10] = token; + str = strch; + break; } yasm_error_set(YASM_ERROR_PARSE, str); destroy_curtok(); @@ -190,23 +190,23 @@ void nasm_parser_parse(yasm_parser_nasm *parser_nasm) { while (get_next_token() != 0) { - yasm_bytecode *bc = NULL, *temp_bc; - - if (!is_eol()) { - bc = parse_line(parser_nasm); - demand_eol(); - } - - yasm_errwarn_propagate(parser_nasm->errwarns, cur_line); - - temp_bc = yasm_section_bcs_append(cursect, bc); - if (temp_bc) - parser_nasm->prev_bc = temp_bc; - if (parser_nasm->save_input) - yasm_linemap_add_source(parser_nasm->linemap, - temp_bc, - (char *)parser_nasm->save_line[parser_nasm->save_last ^ 1]); - yasm_linemap_goto_next(parser_nasm->linemap); + yasm_bytecode *bc = NULL, *temp_bc; + + if (!is_eol()) { + bc = parse_line(parser_nasm); + demand_eol(); + } + + yasm_errwarn_propagate(parser_nasm->errwarns, cur_line); + + temp_bc = yasm_section_bcs_append(cursect, bc); + if (temp_bc) + parser_nasm->prev_bc = temp_bc; + if (parser_nasm->save_input) + yasm_linemap_add_source(parser_nasm->linemap, + temp_bc, + (char *)parser_nasm->save_line[parser_nasm->save_last ^ 1]); + yasm_linemap_goto_next(parser_nasm->linemap); } } @@ -222,210 +222,210 @@ parse_line(yasm_parser_nasm *parser_nasm) bc = parse_exp(parser_nasm); if (bc) - return bc; + return bc; switch (curtok) { - case LINE: /* LINE INTNUM '+' INTNUM FILENAME */ - { - yasm_intnum *line, *incr; - char *filename; - - get_next_token(); - - if (!expect(INTNUM)) return NULL; - line = INTNUM_val; - get_next_token(); - - if (!expect('+')) return NULL; - get_next_token(); - - if (!expect(INTNUM)) return NULL; - incr = INTNUM_val; - get_next_token(); - - if (!expect(FILENAME)) return NULL; - filename = FILENAME_val; - get_next_token(); - - /* %line indicates the line number of the *next* line, so subtract - * out the increment when setting the line number. - */ - yasm_linemap_set(parser_nasm->linemap, filename, - yasm_intnum_get_uint(line) - yasm_intnum_get_uint(incr), - yasm_intnum_get_uint(incr)); - yasm_intnum_destroy(line); - yasm_intnum_destroy(incr); - yasm_xfree(filename); - return NULL; - } - case '[': /* [ directive ] */ - { - char *dirname; - yasm_valparamhead dir_vps; - int have_vps = 1; - - parser_nasm->state = DIRECTIVE; - get_next_token(); - - if (!expect(DIRECTIVE_NAME)) - return NULL; - dirname = DIRECTIVE_NAME_val; - get_next_token(); - - if (curtok == ']' || curtok == ':') - have_vps = 0; - else if (!parse_directive_valparams(parser_nasm, &dir_vps)) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("invalid arguments to [%s]"), dirname); - yasm_xfree(dirname); - return NULL; - } - if (curtok == ':') { - yasm_valparamhead ext_vps; - get_next_token(); - if (!parse_directive_valparams(parser_nasm, &ext_vps)) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("invalid arguments to [%s]"), dirname); - yasm_xfree(dirname); - return NULL; - } - nasm_parser_directive(parser_nasm, dirname, - have_vps ? &dir_vps : NULL, &ext_vps); - } else - nasm_parser_directive(parser_nasm, dirname, - have_vps ? &dir_vps : NULL, NULL); - yasm_xfree(dirname); - expect(']'); - get_next_token(); - return NULL; - } - case TIMES: /* TIMES expr exp */ - get_next_token(); - return parse_times(parser_nasm); - case ID: - case SPECIAL_ID: - case LOCAL_ID: - { - char *name = ID_val; - int local = (curtok != ID); - - get_next_token(); - if (is_eol()) { - /* label alone on the line */ - yasm_warn_set(YASM_WARN_ORPHAN_LABEL, - N_("label alone on a line without a colon might be in error")); - define_label(parser_nasm, name, local); - return NULL; - } - if (curtok == ':') - get_next_token(); - - if (curtok == EQU) { - /* label EQU expr */ - yasm_expr *e; - get_next_token(); - e = parse_expr(parser_nasm, NORM_EXPR); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected after %s"), "EQU"); - yasm_xfree(name); - return NULL; - } - yasm_symtab_define_equ(p_symtab, name, e, cur_line); - yasm_xfree(name); - return NULL; - } - - define_label(parser_nasm, name, local); - if (is_eol()) - return NULL; - if (curtok == TIMES) { - get_next_token(); - return parse_times(parser_nasm); - } - bc = parse_exp(parser_nasm); - if (!bc) - yasm_error_set(YASM_ERROR_SYNTAX, - N_("instruction expected after label")); - return bc; - } - default: - yasm_error_set(YASM_ERROR_SYNTAX, - N_("label or instruction expected at start of line")); - return NULL; + case LINE: /* LINE INTNUM '+' INTNUM FILENAME */ + { + yasm_intnum *line, *incr; + char *filename; + + get_next_token(); + + if (!expect(INTNUM)) return NULL; + line = INTNUM_val; + get_next_token(); + + if (!expect('+')) return NULL; + get_next_token(); + + if (!expect(INTNUM)) return NULL; + incr = INTNUM_val; + get_next_token(); + + if (!expect(FILENAME)) return NULL; + filename = FILENAME_val; + get_next_token(); + + /* %line indicates the line number of the *next* line, so subtract + * out the increment when setting the line number. + */ + yasm_linemap_set(parser_nasm->linemap, filename, + yasm_intnum_get_uint(line) - yasm_intnum_get_uint(incr), + yasm_intnum_get_uint(incr)); + yasm_intnum_destroy(line); + yasm_intnum_destroy(incr); + yasm_xfree(filename); + return NULL; + } + case '[': /* [ directive ] */ + { + char *dirname; + yasm_valparamhead dir_vps; + int have_vps = 1; + + parser_nasm->state = DIRECTIVE; + get_next_token(); + + if (!expect(DIRECTIVE_NAME)) + return NULL; + dirname = DIRECTIVE_NAME_val; + get_next_token(); + + if (curtok == ']' || curtok == ':') + have_vps = 0; + else if (!parse_directive_valparams(parser_nasm, &dir_vps)) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("invalid arguments to [%s]"), dirname); + yasm_xfree(dirname); + return NULL; + } + if (curtok == ':') { + yasm_valparamhead ext_vps; + get_next_token(); + if (!parse_directive_valparams(parser_nasm, &ext_vps)) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("invalid arguments to [%s]"), dirname); + yasm_xfree(dirname); + return NULL; + } + nasm_parser_directive(parser_nasm, dirname, + have_vps ? &dir_vps : NULL, &ext_vps); + } else + nasm_parser_directive(parser_nasm, dirname, + have_vps ? &dir_vps : NULL, NULL); + yasm_xfree(dirname); + expect(']'); + get_next_token(); + return NULL; + } + case TIMES: /* TIMES expr exp */ + get_next_token(); + return parse_times(parser_nasm); + case ID: + case SPECIAL_ID: + case LOCAL_ID: + { + char *name = ID_val; + int local = (curtok != ID); + + get_next_token(); + if (is_eol()) { + /* label alone on the line */ + yasm_warn_set(YASM_WARN_ORPHAN_LABEL, + N_("label alone on a line without a colon might be in error")); + define_label(parser_nasm, name, local); + return NULL; + } + if (curtok == ':') + get_next_token(); + + if (curtok == EQU) { + /* label EQU expr */ + yasm_expr *e; + get_next_token(); + e = parse_expr(parser_nasm, NORM_EXPR); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected after %s"), "EQU"); + yasm_xfree(name); + return NULL; + } + yasm_symtab_define_equ(p_symtab, name, e, cur_line); + yasm_xfree(name); + return NULL; + } + + define_label(parser_nasm, name, local); + if (is_eol()) + return NULL; + if (curtok == TIMES) { + get_next_token(); + return parse_times(parser_nasm); + } + bc = parse_exp(parser_nasm); + if (!bc) + yasm_error_set(YASM_ERROR_SYNTAX, + N_("instruction expected after label")); + return bc; + } + default: + yasm_error_set(YASM_ERROR_SYNTAX, + N_("label or instruction expected at start of line")); + return NULL; } } static int parse_directive_valparams(yasm_parser_nasm *parser_nasm, - /*@out@*/ yasm_valparamhead *vps) + /*@out@*/ yasm_valparamhead *vps) { yasm_vps_initialize(vps); for (;;) { - yasm_valparam *vp; - yasm_expr *e; - - switch (curtok) { - case STRING: - vp = yasm_vp_create(STRING_val.contents, NULL); - get_next_token(); - break; - case ID: - { - char *id = ID_val; - get_peek_token(parser_nasm); - if (parser_nasm->peek_token == '=') { - get_next_token(); /* id */ - get_next_token(); /* = */ - e = parse_expr(parser_nasm, DIR_EXPR); - if (!e) { - yasm_vps_delete(vps); - return 0; - } - yasm_expr__traverse_leaves_in(e, parser_nasm, - fix_directive_symrec); - vp = yasm_vp_create(id, e); - break; - } - if (parser_nasm->peek_token == ',' - || parser_nasm->peek_token == ']' - || parser_nasm->peek_token == ':') { - /* Try to catch just IDs here first */ - get_next_token(); /* id */ - vp = yasm_vp_create(id, NULL); - break; - } - /*@fallthrough@*/ - } - default: - { - /* If direxpr is just an ID, put it in val and delete the expr. - * Otherwise, we need to go through the expr and replace the - * current (local) symrecs with the use of global ones. - */ - const /*@null@*/ yasm_symrec *vp_symrec; - e = parse_expr(parser_nasm, DIR_EXPR); - if (!e) { - yasm_vps_delete(vps); - return 0; - } - if ((vp_symrec = yasm_expr_get_symrec(&e, 0))) { - vp = yasm_vp_create( - yasm__xstrdup(yasm_symrec_get_name(vp_symrec)), NULL); - yasm_expr_destroy(e); - } else { - yasm_expr__traverse_leaves_in(e, parser_nasm, - fix_directive_symrec); - vp = yasm_vp_create(NULL, e); - } - } - } - - yasm_vps_append(vps, vp); - if (curtok == ',') - get_next_token(); - if (curtok == ']' || curtok == ':' || is_eol()) - return 1; + yasm_valparam *vp; + yasm_expr *e; + + switch (curtok) { + case STRING: + vp = yasm_vp_create(STRING_val.contents, NULL); + get_next_token(); + break; + case ID: + { + char *id = ID_val; + get_peek_token(parser_nasm); + if (parser_nasm->peek_token == '=') { + get_next_token(); /* id */ + get_next_token(); /* = */ + e = parse_expr(parser_nasm, DIR_EXPR); + if (!e) { + yasm_vps_delete(vps); + return 0; + } + yasm_expr__traverse_leaves_in(e, parser_nasm, + fix_directive_symrec); + vp = yasm_vp_create(id, e); + break; + } + if (parser_nasm->peek_token == ',' + || parser_nasm->peek_token == ']' + || parser_nasm->peek_token == ':') { + /* Try to catch just IDs here first */ + get_next_token(); /* id */ + vp = yasm_vp_create(id, NULL); + break; + } + /*@fallthrough@*/ + } + default: + { + /* If direxpr is just an ID, put it in val and delete the expr. + * Otherwise, we need to go through the expr and replace the + * current (local) symrecs with the use of global ones. + */ + const /*@null@*/ yasm_symrec *vp_symrec; + e = parse_expr(parser_nasm, DIR_EXPR); + if (!e) { + yasm_vps_delete(vps); + return 0; + } + if ((vp_symrec = yasm_expr_get_symrec(&e, 0))) { + vp = yasm_vp_create( + yasm__xstrdup(yasm_symrec_get_name(vp_symrec)), NULL); + yasm_expr_destroy(e); + } else { + yasm_expr__traverse_leaves_in(e, parser_nasm, + fix_directive_symrec); + vp = yasm_vp_create(NULL, e); + } + } + } + + yasm_vps_append(vps, vp); + if (curtok == ',') + get_next_token(); + if (curtok == ']' || curtok == ':' || is_eol()) + return 1; } } @@ -437,16 +437,16 @@ parse_times(yasm_parser_nasm *parser_nasm) multiple = parse_expr(parser_nasm, DV_EXPR); if (!multiple) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("expression expected after %s"), - "TIMES"); - return NULL; + yasm_error_set(YASM_ERROR_SYNTAX, N_("expression expected after %s"), + "TIMES"); + return NULL; } bc = parse_exp(parser_nasm); if (!bc) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("instruction expected after TIMES expression")); - yasm_expr_destroy(multiple); - return NULL; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("instruction expected after TIMES expression")); + yasm_expr_destroy(multiple); + return NULL; } yasm_bc_set_multiple(bc, multiple); return bc; @@ -459,114 +459,114 @@ parse_exp(yasm_parser_nasm *parser_nasm) bc = parse_instr(parser_nasm); if (bc) - return bc; + return bc; switch (curtok) { - case DECLARE_DATA: - { - unsigned int size = DECLARE_DATA_val/8; - yasm_datavalhead dvs; - yasm_dataval *dv; - yasm_expr *e; - - get_next_token(); - - yasm_dvs_initialize(&dvs); - for (;;) { - if (curtok == STRING) { - /* Peek ahead to see if we're in an expr. If we're not, - * then generate a real string dataval. - */ - get_peek_token(parser_nasm); - if (parser_nasm->peek_token == ',' - || is_eol_tok(parser_nasm->peek_token)) { - dv = yasm_dv_create_string(STRING_val.contents, - STRING_val.len); - get_next_token(); - goto dv_done; - } - } - if ((e = parse_expr(parser_nasm, DV_EXPR))) - dv = yasm_dv_create_expr(e); - else { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression or string expected")); - yasm_dvs_delete(&dvs); - return NULL; - } + case DECLARE_DATA: + { + unsigned int size = DECLARE_DATA_val/8; + yasm_datavalhead dvs; + yasm_dataval *dv; + yasm_expr *e; + + get_next_token(); + + yasm_dvs_initialize(&dvs); + for (;;) { + if (curtok == STRING) { + /* Peek ahead to see if we're in an expr. If we're not, + * then generate a real string dataval. + */ + get_peek_token(parser_nasm); + if (parser_nasm->peek_token == ',' + || is_eol_tok(parser_nasm->peek_token)) { + dv = yasm_dv_create_string(STRING_val.contents, + STRING_val.len); + get_next_token(); + goto dv_done; + } + } + if ((e = parse_expr(parser_nasm, DV_EXPR))) + dv = yasm_dv_create_expr(e); + else { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression or string expected")); + yasm_dvs_delete(&dvs); + return NULL; + } dv_done: - yasm_dvs_append(&dvs, dv); - if (is_eol()) - break; - if (!expect(',')) { - yasm_dvs_delete(&dvs); - return NULL; - } - get_next_token(); - if (is_eol()) /* allow trailing , on list */ - break; - } - return yasm_bc_create_data(&dvs, size, 0, p_object->arch, - cur_line); - } - case RESERVE_SPACE: - { - unsigned int size = RESERVE_SPACE_val/8; - yasm_expr *e; - get_next_token(); - e = parse_expr(parser_nasm, DV_EXPR); - if (!e) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected after %s"), "RESx"); - return NULL; - } - return yasm_bc_create_reserve(e, size, cur_line); - } - case INCBIN: - { - char *filename; - yasm_expr *start = NULL, *maxlen = NULL; - - get_next_token(); - - if (!expect(STRING)) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("filename string expected after INCBIN")); - return NULL; - } - filename = STRING_val.contents; - get_next_token(); - - /* optional start expression */ - if (curtok == ',') - get_next_token(); - if (is_eol()) - goto incbin_done; - start = parse_expr(parser_nasm, DV_EXPR); - if (!start) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected for INCBIN start")); - return NULL; - } - - /* optional maxlen expression */ - if (curtok == ',') - get_next_token(); - if (is_eol()) - goto incbin_done; - maxlen = parse_expr(parser_nasm, DV_EXPR); - if (!start) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression expected for INCBIN maximum length")); - return NULL; - } + yasm_dvs_append(&dvs, dv); + if (is_eol()) + break; + if (!expect(',')) { + yasm_dvs_delete(&dvs); + return NULL; + } + get_next_token(); + if (is_eol()) /* allow trailing , on list */ + break; + } + return yasm_bc_create_data(&dvs, size, 0, p_object->arch, + cur_line); + } + case RESERVE_SPACE: + { + unsigned int size = RESERVE_SPACE_val/8; + yasm_expr *e; + get_next_token(); + e = parse_expr(parser_nasm, DV_EXPR); + if (!e) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected after %s"), "RESx"); + return NULL; + } + return yasm_bc_create_reserve(e, size, cur_line); + } + case INCBIN: + { + char *filename; + yasm_expr *start = NULL, *maxlen = NULL; + + get_next_token(); + + if (!expect(STRING)) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("filename string expected after INCBIN")); + return NULL; + } + filename = STRING_val.contents; + get_next_token(); + + /* optional start expression */ + if (curtok == ',') + get_next_token(); + if (is_eol()) + goto incbin_done; + start = parse_expr(parser_nasm, DV_EXPR); + if (!start) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected for INCBIN start")); + return NULL; + } + + /* optional maxlen expression */ + if (curtok == ',') + get_next_token(); + if (is_eol()) + goto incbin_done; + maxlen = parse_expr(parser_nasm, DV_EXPR); + if (!start) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression expected for INCBIN maximum length")); + return NULL; + } incbin_done: - return yasm_bc_create_incbin(filename, start, maxlen, - parser_nasm->linemap, cur_line); - } - default: - return NULL; + return yasm_bc_create_incbin(filename, start, maxlen, + parser_nasm->linemap, cur_line); + } + default: + return NULL; } } @@ -574,63 +574,63 @@ static yasm_bytecode * parse_instr(yasm_parser_nasm *parser_nasm) { switch (curtok) { - case INSN: - { - yystype insn = curval; /* structure copy */ - yasm_insn_operands operands; - int num_operands = 0; - - get_next_token(); - if (is_eol()) { - /* no operands */ - return yasm_bc_create_insn(p_object->arch, insn.arch_data, 0, - NULL, cur_line); - } - - /* parse operands */ - yasm_ops_initialize(&operands); - for (;;) { - yasm_insn_operand *op = parse_operand(parser_nasm); - if (!op) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("expression syntax error")); - yasm_ops_delete(&operands, 1); - return NULL; - } - yasm_ops_append(&operands, op); - num_operands++; - - if (is_eol()) - break; - if (!expect(',')) { - yasm_ops_delete(&operands, 1); - return NULL; - } - get_next_token(); - } - return yasm_bc_create_insn(p_object->arch, insn.arch_data, - num_operands, &operands, cur_line); - } - case PREFIX: { - yystype prefix = curval; /* structure copy */ - yasm_bytecode *bc; - get_next_token(); - bc = parse_instr(parser_nasm); - if (bc) - yasm_bc_insn_add_prefix(bc, prefix.arch_data); - return bc; - } - case SEGREG: { - uintptr_t segreg = SEGREG_val[0]; - yasm_bytecode *bc; - get_next_token(); - bc = parse_instr(parser_nasm); - if (bc) - yasm_bc_insn_add_seg_prefix(bc, segreg); - return bc; - } - default: - return NULL; + case INSN: + { + yystype insn = curval; /* structure copy */ + yasm_insn_operands operands; + int num_operands = 0; + + get_next_token(); + if (is_eol()) { + /* no operands */ + return yasm_bc_create_insn(p_object->arch, insn.arch_data, 0, + NULL, cur_line); + } + + /* parse operands */ + yasm_ops_initialize(&operands); + for (;;) { + yasm_insn_operand *op = parse_operand(parser_nasm); + if (!op) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("expression syntax error")); + yasm_ops_delete(&operands, 1); + return NULL; + } + yasm_ops_append(&operands, op); + num_operands++; + + if (is_eol()) + break; + if (!expect(',')) { + yasm_ops_delete(&operands, 1); + return NULL; + } + get_next_token(); + } + return yasm_bc_create_insn(p_object->arch, insn.arch_data, + num_operands, &operands, cur_line); + } + case PREFIX: { + yystype prefix = curval; /* structure copy */ + yasm_bytecode *bc; + get_next_token(); + bc = parse_instr(parser_nasm); + if (bc) + yasm_bc_insn_add_prefix(bc, prefix.arch_data); + return bc; + } + case SEGREG: { + uintptr_t segreg = SEGREG_val[0]; + yasm_bytecode *bc; + get_next_token(); + bc = parse_instr(parser_nasm); + if (bc) + yasm_bc_insn_add_seg_prefix(bc, segreg); + return bc; + } + default: + return NULL; } } @@ -639,84 +639,84 @@ parse_operand(yasm_parser_nasm *parser_nasm) { yasm_insn_operand *op; switch (curtok) { - case '[': - { - yasm_effaddr *ea; - get_next_token(); - ea = parse_memaddr(parser_nasm); - - expect(']'); - get_next_token(); - - if (!ea) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("memory address expected")); - return NULL; - } - return yasm_operand_create_mem(ea); - } - case SEGREG: - op = yasm_operand_create_segreg(SEGREG_val[0]); - get_next_token(); - return op; - case REG: - op = yasm_operand_create_reg(REG_val[0]); - get_next_token(); - return op; - case STRICT: - get_next_token(); - op = parse_operand(parser_nasm); - if (op) - op->strict = 1; - return op; - case SIZE_OVERRIDE: - { - unsigned int size = SIZE_OVERRIDE_val; - get_next_token(); - op = parse_operand(parser_nasm); - if (!op) - return NULL; - if (op->type == YASM_INSN__OPERAND_REG && - yasm_arch_get_reg_size(p_object->arch, op->data.reg) != size) - yasm_error_set(YASM_ERROR_TYPE, - N_("cannot override register size")); - else { - /* Silently override others unless a warning is turned on. - * This is to allow overrides such as: - * %define arg1 dword [bp+4] - * cmp word arg1, 2 - * Which expands to: - * cmp word dword [bp+4], 2 - */ - if (op->size != 0) { - if (op->size != size) - yasm_warn_set(YASM_WARN_SIZE_OVERRIDE, - N_("overriding operand size from %u-bit to %u-bit"), - op->size, size); - else - yasm_warn_set(YASM_WARN_SIZE_OVERRIDE, - N_("double operand size override")); - } - op->size = size; - } - return op; - } - case TARGETMOD: - { - uintptr_t tmod = TARGETMOD_val[0]; - get_next_token(); - op = parse_operand(parser_nasm); - if (op) - op->targetmod = tmod; - return op; - } - default: - { - yasm_expr *e = parse_expr(parser_nasm, NORM_EXPR); - if (!e) - return NULL; - return yasm_operand_create_imm(e); - } + case '[': + { + yasm_effaddr *ea; + get_next_token(); + ea = parse_memaddr(parser_nasm); + + expect(']'); + get_next_token(); + + if (!ea) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("memory address expected")); + return NULL; + } + return yasm_operand_create_mem(ea); + } + case SEGREG: + op = yasm_operand_create_segreg(SEGREG_val[0]); + get_next_token(); + return op; + case REG: + op = yasm_operand_create_reg(REG_val[0]); + get_next_token(); + return op; + case STRICT: + get_next_token(); + op = parse_operand(parser_nasm); + if (op) + op->strict = 1; + return op; + case SIZE_OVERRIDE: + { + unsigned int size = SIZE_OVERRIDE_val; + get_next_token(); + op = parse_operand(parser_nasm); + if (!op) + return NULL; + if (op->type == YASM_INSN__OPERAND_REG && + yasm_arch_get_reg_size(p_object->arch, op->data.reg) != size) + yasm_error_set(YASM_ERROR_TYPE, + N_("cannot override register size")); + else { + /* Silently override others unless a warning is turned on. + * This is to allow overrides such as: + * %define arg1 dword [bp+4] + * cmp word arg1, 2 + * Which expands to: + * cmp word dword [bp+4], 2 + */ + if (op->size != 0) { + if (op->size != size) + yasm_warn_set(YASM_WARN_SIZE_OVERRIDE, + N_("overriding operand size from %u-bit to %u-bit"), + op->size, size); + else + yasm_warn_set(YASM_WARN_SIZE_OVERRIDE, + N_("double operand size override")); + } + op->size = size; + } + return op; + } + case TARGETMOD: + { + uintptr_t tmod = TARGETMOD_val[0]; + get_next_token(); + op = parse_operand(parser_nasm); + if (op) + op->targetmod = tmod; + return op; + } + default: + { + yasm_expr *e = parse_expr(parser_nasm, NORM_EXPR); + if (!e) + return NULL; + return yasm_operand_create_imm(e); + } } } @@ -726,43 +726,43 @@ parse_memaddr(yasm_parser_nasm *parser_nasm) { yasm_effaddr *ea; switch (curtok) { - case SEGREG: - { - uintptr_t segreg = SEGREG_val[0]; - get_next_token(); - if (!expect(':')) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("`:' required after segment register")); - return NULL; - } - get_next_token(); - ea = parse_memaddr(parser_nasm); - if (ea) - yasm_ea_set_segreg(ea, segreg); - return ea; - } - case SIZE_OVERRIDE: - { - unsigned int size = SIZE_OVERRIDE_val; - get_next_token(); - ea = parse_memaddr(parser_nasm); - if (ea) - yasm_ea_set_len(ea, size); - return ea; - } - case NOSPLIT: - get_next_token(); - ea = parse_memaddr(parser_nasm); - if (ea) - yasm_ea_set_nosplit(ea, 1); - return ea; - default: - { - yasm_expr *e = parse_expr(parser_nasm, NORM_EXPR); - if (!e) - return NULL; - return yasm_arch_ea_create(p_object->arch, e); - } + case SEGREG: + { + uintptr_t segreg = SEGREG_val[0]; + get_next_token(); + if (!expect(':')) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("`:' required after segment register")); + return NULL; + } + get_next_token(); + ea = parse_memaddr(parser_nasm); + if (ea) + yasm_ea_set_segreg(ea, segreg); + return ea; + } + case SIZE_OVERRIDE: + { + unsigned int size = SIZE_OVERRIDE_val; + get_next_token(); + ea = parse_memaddr(parser_nasm); + if (ea) + yasm_ea_set_len(ea, size); + return ea; + } + case NOSPLIT: + get_next_token(); + ea = parse_memaddr(parser_nasm); + if (ea) + yasm_ea_set_nosplit(ea, 1); + return ea; + default: + { + yasm_expr *e = parse_expr(parser_nasm, NORM_EXPR); + if (!e) + return NULL; + return yasm_arch_ea_create(p_object->arch, e); + } } } @@ -784,36 +784,36 @@ parse_memaddr(yasm_parser_nasm *parser_nasm) */ #define parse_expr_common(leftfunc, tok, rightfunc, op) \ - do { \ - yasm_expr *e, *f; \ - e = leftfunc(parser_nasm, type); \ - if (!e) \ - return NULL; \ - \ - while (curtok == tok) { \ - get_next_token(); \ - f = rightfunc(parser_nasm, type); \ - if (!f) { \ - yasm_expr_destroy(e); \ - return NULL; \ - } \ - e = p_expr_new_tree(e, op, f); \ - } \ - return e; \ + do { \ + yasm_expr *e, *f; \ + e = leftfunc(parser_nasm, type); \ + if (!e) \ + return NULL; \ + \ + while (curtok == tok) { \ + get_next_token(); \ + f = rightfunc(parser_nasm, type); \ + if (!f) { \ + yasm_expr_destroy(e); \ + return NULL; \ + } \ + e = p_expr_new_tree(e, op, f); \ + } \ + return e; \ } while(0) static yasm_expr * parse_expr(yasm_parser_nasm *parser_nasm, expr_type type) { switch (type) { - case NORM_EXPR: - parse_expr_common(parse_bexpr, ':', parse_bexpr, YASM_EXPR_SEGOFF); - case DV_EXPR: - /* dataval expressions can't handle seg:off */ - return parse_bexpr(parser_nasm, type); - case DIR_EXPR: - /* directive expressions can't handle seg:off or WRT */ - return parse_expr0(parser_nasm, type); + case NORM_EXPR: + parse_expr_common(parse_bexpr, ':', parse_bexpr, YASM_EXPR_SEGOFF); + case DV_EXPR: + /* dataval expressions can't handle seg:off */ + return parse_bexpr(parser_nasm, type); + case DIR_EXPR: + /* directive expressions can't handle seg:off or WRT */ + return parse_expr0(parser_nasm, type); } /*@notreached@*/ return NULL; @@ -849,21 +849,21 @@ parse_expr3(yasm_parser_nasm *parser_nasm, expr_type type) yasm_expr *e, *f; e = parse_expr4(parser_nasm, type); if (!e) - return NULL; + return NULL; while (curtok == LEFT_OP || curtok == RIGHT_OP) { - int op = curtok; - get_next_token(); - f = parse_expr4(parser_nasm, type); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (op) { - case LEFT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHL, f); break; - case RIGHT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHR, f); break; - } + int op = curtok; + get_next_token(); + f = parse_expr4(parser_nasm, type); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (op) { + case LEFT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHL, f); break; + case RIGHT_OP: e = p_expr_new_tree(e, YASM_EXPR_SHR, f); break; + } } return e; } @@ -874,21 +874,21 @@ parse_expr4(yasm_parser_nasm *parser_nasm, expr_type type) yasm_expr *e, *f; e = parse_expr5(parser_nasm, type); if (!e) - return NULL; + return NULL; while (curtok == '+' || curtok == '-') { - int op = curtok; - get_next_token(); - f = parse_expr5(parser_nasm, type); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (op) { - case '+': e = p_expr_new_tree(e, YASM_EXPR_ADD, f); break; - case '-': e = p_expr_new_tree(e, YASM_EXPR_SUB, f); break; - } + int op = curtok; + get_next_token(); + f = parse_expr5(parser_nasm, type); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (op) { + case '+': e = p_expr_new_tree(e, YASM_EXPR_ADD, f); break; + case '-': e = p_expr_new_tree(e, YASM_EXPR_SUB, f); break; + } } return e; } @@ -899,25 +899,25 @@ parse_expr5(yasm_parser_nasm *parser_nasm, expr_type type) yasm_expr *e, *f; e = parse_expr6(parser_nasm, type); if (!e) - return NULL; + return NULL; while (curtok == '*' || curtok == '/' || curtok == '%' - || curtok == SIGNDIV || curtok == SIGNMOD) { - int op = curtok; - get_next_token(); - f = parse_expr6(parser_nasm, type); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (op) { - case '*': e = p_expr_new_tree(e, YASM_EXPR_MUL, f); break; - case '/': e = p_expr_new_tree(e, YASM_EXPR_DIV, f); break; - case '%': e = p_expr_new_tree(e, YASM_EXPR_MOD, f); break; - case SIGNDIV: e = p_expr_new_tree(e, YASM_EXPR_SIGNDIV, f); break; - case SIGNMOD: e = p_expr_new_tree(e, YASM_EXPR_SIGNMOD, f); break; - } + || curtok == SIGNDIV || curtok == SIGNMOD) { + int op = curtok; + get_next_token(); + f = parse_expr6(parser_nasm, type); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (op) { + case '*': e = p_expr_new_tree(e, YASM_EXPR_MUL, f); break; + case '/': e = p_expr_new_tree(e, YASM_EXPR_DIV, f); break; + case '%': e = p_expr_new_tree(e, YASM_EXPR_MOD, f); break; + case SIGNDIV: e = p_expr_new_tree(e, YASM_EXPR_SIGNDIV, f); break; + case SIGNMOD: e = p_expr_new_tree(e, YASM_EXPR_SIGNMOD, f); break; + } } return e; } @@ -930,112 +930,112 @@ parse_expr6(yasm_parser_nasm *parser_nasm, expr_type type) /* directives allow very little and handle IDs specially */ if (type == DIR_EXPR) { - switch (curtok) { - case '~': - get_next_token(); - e = parse_expr6(parser_nasm, type); - if (!e) - return NULL; - return p_expr_new_branch(YASM_EXPR_NOT, e); - case '(': - get_next_token(); - e = parse_expr(parser_nasm, type); - if (!e) - return NULL; - if (!expect(')')) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("missing parenthesis")); - return NULL; - } - get_next_token(); - return e; - case INTNUM: - e = p_expr_new_ident(yasm_expr_int(INTNUM_val)); - break; - case REG: - e = p_expr_new_ident(yasm_expr_reg(REG_val[0])); - break; - case ID: - e = p_expr_new_ident(yasm_expr_sym( - yasm_symtab_define_label(p_symtab, ID_val, - yasm_section_bcs_first(cursect), 0, cur_line))); - yasm_xfree(ID_val); - break; - default: - return NULL; - } + switch (curtok) { + case '~': + get_next_token(); + e = parse_expr6(parser_nasm, type); + if (!e) + return NULL; + return p_expr_new_branch(YASM_EXPR_NOT, e); + case '(': + get_next_token(); + e = parse_expr(parser_nasm, type); + if (!e) + return NULL; + if (!expect(')')) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("missing parenthesis")); + return NULL; + } + get_next_token(); + return e; + case INTNUM: + e = p_expr_new_ident(yasm_expr_int(INTNUM_val)); + break; + case REG: + e = p_expr_new_ident(yasm_expr_reg(REG_val[0])); + break; + case ID: + e = p_expr_new_ident(yasm_expr_sym( + yasm_symtab_define_label(p_symtab, ID_val, + yasm_section_bcs_first(cursect), 0, cur_line))); + yasm_xfree(ID_val); + break; + default: + return NULL; + } } else switch (curtok) { - case '+': - get_next_token(); - return parse_expr6(parser_nasm, type); - case '-': - get_next_token(); - e = parse_expr6(parser_nasm, type); - if (!e) - return NULL; - return p_expr_new_branch(YASM_EXPR_NEG, e); - case '~': - get_next_token(); - e = parse_expr6(parser_nasm, type); - if (!e) - return NULL; - return p_expr_new_branch(YASM_EXPR_NOT, e); - case SEG: - get_next_token(); - e = parse_expr6(parser_nasm, type); - if (!e) - return NULL; - return p_expr_new_branch(YASM_EXPR_SEG, e); - case '(': - get_next_token(); - e = parse_expr(parser_nasm, type); - if (!e) - return NULL; - if (!expect(')')) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("missing parenthesis")); - return NULL; - } - get_next_token(); - return e; - case INTNUM: - e = p_expr_new_ident(yasm_expr_int(INTNUM_val)); - break; - case FLTNUM: - e = p_expr_new_ident(yasm_expr_float(FLTNUM_val)); - break; - case REG: - if (type == DV_EXPR) { - yasm_error_set(YASM_ERROR_SYNTAX, - N_("data values can't have registers")); - return NULL; - } - e = p_expr_new_ident(yasm_expr_reg(REG_val[0])); - break; - case STRING: - e = p_expr_new_ident(yasm_expr_int( - yasm_intnum_create_charconst_nasm(STRING_val.contents))); - yasm_xfree(STRING_val.contents); - break; - case ID: - case SPECIAL_ID: - case LOCAL_ID: - sym = yasm_symtab_use(p_symtab, ID_val, cur_line); - e = p_expr_new_ident(yasm_expr_sym(sym)); - yasm_xfree(ID_val); - break; - case '$': - /* "$" references the current assembly position */ - sym = yasm_symtab_define_curpos(p_symtab, "$", - parser_nasm->prev_bc, cur_line); - e = p_expr_new_ident(yasm_expr_sym(sym)); - break; - case START_SECTION_ID: - /* "$$" references the start of the current section */ - sym = yasm_symtab_define_label(p_symtab, "$$", - yasm_section_bcs_first(cursect), 0, cur_line); - e = p_expr_new_ident(yasm_expr_sym(sym)); - break; - default: - return NULL; + case '+': + get_next_token(); + return parse_expr6(parser_nasm, type); + case '-': + get_next_token(); + e = parse_expr6(parser_nasm, type); + if (!e) + return NULL; + return p_expr_new_branch(YASM_EXPR_NEG, e); + case '~': + get_next_token(); + e = parse_expr6(parser_nasm, type); + if (!e) + return NULL; + return p_expr_new_branch(YASM_EXPR_NOT, e); + case SEG: + get_next_token(); + e = parse_expr6(parser_nasm, type); + if (!e) + return NULL; + return p_expr_new_branch(YASM_EXPR_SEG, e); + case '(': + get_next_token(); + e = parse_expr(parser_nasm, type); + if (!e) + return NULL; + if (!expect(')')) { + yasm_error_set(YASM_ERROR_SYNTAX, N_("missing parenthesis")); + return NULL; + } + get_next_token(); + return e; + case INTNUM: + e = p_expr_new_ident(yasm_expr_int(INTNUM_val)); + break; + case FLTNUM: + e = p_expr_new_ident(yasm_expr_float(FLTNUM_val)); + break; + case REG: + if (type == DV_EXPR) { + yasm_error_set(YASM_ERROR_SYNTAX, + N_("data values can't have registers")); + return NULL; + } + e = p_expr_new_ident(yasm_expr_reg(REG_val[0])); + break; + case STRING: + e = p_expr_new_ident(yasm_expr_int( + yasm_intnum_create_charconst_nasm(STRING_val.contents))); + yasm_xfree(STRING_val.contents); + break; + case ID: + case SPECIAL_ID: + case LOCAL_ID: + sym = yasm_symtab_use(p_symtab, ID_val, cur_line); + e = p_expr_new_ident(yasm_expr_sym(sym)); + yasm_xfree(ID_val); + break; + case '$': + /* "$" references the current assembly position */ + sym = yasm_symtab_define_curpos(p_symtab, "$", + parser_nasm->prev_bc, cur_line); + e = p_expr_new_ident(yasm_expr_sym(sym)); + break; + case START_SECTION_ID: + /* "$$" references the start of the current section */ + sym = yasm_symtab_define_label(p_symtab, "$$", + yasm_section_bcs_first(cursect), 0, cur_line); + e = p_expr_new_ident(yasm_expr_sym(sym)); + break; + default: + return NULL; } get_next_token(); return e; @@ -1045,16 +1045,16 @@ static void define_label(yasm_parser_nasm *parser_nasm, char *name, int local) { if (!local) { - if (parser_nasm->locallabel_base) - yasm_xfree(parser_nasm->locallabel_base); - parser_nasm->locallabel_base_len = strlen(name); - parser_nasm->locallabel_base = - yasm_xmalloc(parser_nasm->locallabel_base_len+1); - strcpy(parser_nasm->locallabel_base, name); + if (parser_nasm->locallabel_base) + yasm_xfree(parser_nasm->locallabel_base); + parser_nasm->locallabel_base_len = strlen(name); + parser_nasm->locallabel_base = + yasm_xmalloc(parser_nasm->locallabel_base_len+1); + strcpy(parser_nasm->locallabel_base, name); } yasm_symtab_define_label(p_symtab, name, parser_nasm->prev_bc, 1, - cur_line); + cur_line); yasm_xfree(name); } @@ -1063,19 +1063,19 @@ fix_directive_symrec(yasm_expr__item *ei, void *d) { yasm_parser_nasm *parser_nasm = (yasm_parser_nasm *)d; if (!ei || ei->type != YASM_EXPR_SYM) - return 0; + return 0; /* FIXME: Delete current symrec */ ei->data.sym = - yasm_symtab_use(p_symtab, yasm_symrec_get_name(ei->data.sym), - cur_line); + yasm_symtab_use(p_symtab, yasm_symrec_get_name(ei->data.sym), + cur_line); return 0; } static void dir_absolute(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_expr *start = yasm_vp_expr(vp, object->symtab, line); @@ -1085,7 +1085,7 @@ dir_absolute(yasm_object *object, yasm_valparamhead *valparams, static void dir_align(yasm_object *object, yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams, unsigned long line) + yasm_valparamhead *objext_valparams, unsigned long line) { yasm_valparam *vp = yasm_vps_first(valparams); yasm_expr *boundval = yasm_vp_expr(vp, object->symtab, line); @@ -1097,48 +1097,48 @@ dir_align(yasm_object *object, yasm_valparamhead *valparams, */ boundintn = yasm_expr_get_intnum(&boundval, 0); if (boundintn) { - unsigned long boundint = yasm_intnum_get_uint(boundintn); + unsigned long boundint = yasm_intnum_get_uint(boundintn); - /* Alignments must be a power of two. */ - if (is_exp2(boundint)) { - if (boundint > yasm_section_get_align(object->cur_section)) - yasm_section_set_align(object->cur_section, boundint, line); - } + /* Alignments must be a power of two. */ + if (is_exp2(boundint)) { + if (boundint > yasm_section_get_align(object->cur_section)) + yasm_section_set_align(object->cur_section, boundint, line); + } } /* As this directive is called only when nop is used as fill, always * use arch (nop) fill. */ yasm_section_bcs_append(object->cur_section, - yasm_bc_create_align(boundval, NULL, NULL, - /*yasm_section_is_code(object->cur_section) ?*/ - yasm_arch_get_fill(object->arch)/* : NULL*/, - line)); + yasm_bc_create_align(boundval, NULL, NULL, + /*yasm_section_is_code(object->cur_section) ?*/ + yasm_arch_get_fill(object->arch)/* : NULL*/, + line)); } static void nasm_parser_directive(yasm_parser_nasm *parser_nasm, const char *name, - yasm_valparamhead *valparams, - yasm_valparamhead *objext_valparams) + yasm_valparamhead *valparams, + yasm_valparamhead *objext_valparams) { unsigned long line = cur_line; if (!yasm_object_directive(p_object, name, "nasm", valparams, - objext_valparams, line)) - ; + objext_valparams, line)) + ; else if (yasm__strcasecmp(name, "absolute") == 0) - dir_absolute(p_object, valparams, objext_valparams, line); + dir_absolute(p_object, valparams, objext_valparams, line); else if (yasm__strcasecmp(name, "align") == 0) - dir_align(p_object, valparams, objext_valparams, line); + dir_align(p_object, valparams, objext_valparams, line); else - yasm_error_set(YASM_ERROR_SYNTAX, N_("unrecognized directive `%s'"), - name); + yasm_error_set(YASM_ERROR_SYNTAX, N_("unrecognized directive `%s'"), + name); /* In case cursect changed or a bytecode was added, update prev_bc. */ parser_nasm->prev_bc = yasm_section_bcs_last(cursect); if (valparams) - yasm_vps_delete(valparams); + yasm_vps_delete(valparams); if (objext_valparams) - yasm_vps_delete(objext_valparams); + yasm_vps_delete(objext_valparams); } diff --git a/modules/parsers/nasm/nasm-parser.c b/modules/parsers/nasm/nasm-parser.c index 9599e4a4..6ff75a4a 100644 --- a/modules/parsers/nasm/nasm-parser.c +++ b/modules/parsers/nasm/nasm-parser.c @@ -35,8 +35,8 @@ static void nasm_parser_do_parse(yasm_object *object, yasm_preproc *pp, FILE *f, - int save_input, yasm_linemap *linemap, - yasm_errwarns *errwarns) + int save_input, yasm_linemap *linemap, + yasm_errwarns *errwarns) { yasm_parser_nasm parser_nasm; @@ -72,7 +72,7 @@ nasm_parser_do_parse(yasm_object *object, yasm_preproc *pp, FILE *f, /* Free locallabel base if necessary */ if (parser_nasm.locallabel_base) - yasm_xfree(parser_nasm.locallabel_base); + yasm_xfree(parser_nasm.locallabel_base); } /* Define valid preprocessors to use with this parser */ diff --git a/modules/parsers/nasm/nasm-parser.h b/modules/parsers/nasm/nasm-parser.h index 949b7c00..099ab5d9 100644 --- a/modules/parsers/nasm/nasm-parser.h +++ b/modules/parsers/nasm/nasm-parser.h @@ -27,7 +27,7 @@ #ifndef YASM_NASM_PARSER_H #define YASM_NASM_PARSER_H -#define YYCTYPE unsigned char +#define YYCTYPE unsigned char #define MAX_SAVED_LINE_LEN 80 @@ -61,7 +61,7 @@ enum tokentype { LOCAL_ID, SPECIAL_ID, LINE, - NONE /* special token for lookahead */ + NONE /* special token for lookahead */ }; typedef union { @@ -71,8 +71,8 @@ typedef union { yasm_floatnum *flt; uintptr_t arch_data[4]; struct { - char *contents; - size_t len; + char *contents; + size_t len; } str; } yystype; #define YYSTYPE yystype @@ -100,52 +100,52 @@ typedef struct yasm_parser_nasm { yasm_scanner s; enum { - INITIAL, - DIRECTIVE, - SECTION_DIRECTIVE, - DIRECTIVE2, - LINECHG, - LINECHG2, - INSTRUCTION + INITIAL, + DIRECTIVE, + SECTION_DIRECTIVE, + DIRECTIVE2, + LINECHG, + LINECHG2, + INSTRUCTION } state; - int token; /* enum tokentype or any character */ + int token; /* enum tokentype or any character */ yystype tokval; - char tokch; /* first character of token */ + char tokch; /* first character of token */ /* one token of lookahead; used sparingly */ - int peek_token; /* NONE if none */ + int peek_token; /* NONE if none */ yystype peek_tokval; char peek_tokch; } yasm_parser_nasm; /* shorter access names to commonly used parser_nasm fields */ -#define p_object (parser_nasm->object) -#define p_symtab (parser_nasm->object->symtab) -#define cursect (parser_nasm->object->cur_section) -#define curtok (parser_nasm->token) -#define curval (parser_nasm->tokval) - -#define INTNUM_val (curval.intn) -#define FLTNUM_val (curval.flt) -#define DIRECTIVE_NAME_val (curval.str_val) -#define FILENAME_val (curval.str_val) -#define STRING_val (curval.str) -#define SIZE_OVERRIDE_val (curval.int_info) -#define DECLARE_DATA_val (curval.int_info) -#define RESERVE_SPACE_val (curval.int_info) -#define INSN_val (curval.arch_data) -#define PREFIX_val (curval.arch_data) -#define REG_val (curval.arch_data) -#define SEGREG_val (curval.arch_data) -#define TARGETMOD_val (curval.arch_data) -#define ID_val (curval.str_val) - -#define cur_line (yasm_linemap_get_current(parser_nasm->linemap)) - -#define p_expr_new_tree(l,o,r) yasm_expr_create_tree(l,o,r,cur_line) -#define p_expr_new_branch(o,r) yasm_expr_create_branch(o,r,cur_line) -#define p_expr_new_ident(r) yasm_expr_create_ident(r,cur_line) +#define p_object (parser_nasm->object) +#define p_symtab (parser_nasm->object->symtab) +#define cursect (parser_nasm->object->cur_section) +#define curtok (parser_nasm->token) +#define curval (parser_nasm->tokval) + +#define INTNUM_val (curval.intn) +#define FLTNUM_val (curval.flt) +#define DIRECTIVE_NAME_val (curval.str_val) +#define FILENAME_val (curval.str_val) +#define STRING_val (curval.str) +#define SIZE_OVERRIDE_val (curval.int_info) +#define DECLARE_DATA_val (curval.int_info) +#define RESERVE_SPACE_val (curval.int_info) +#define INSN_val (curval.arch_data) +#define PREFIX_val (curval.arch_data) +#define REG_val (curval.arch_data) +#define SEGREG_val (curval.arch_data) +#define TARGETMOD_val (curval.arch_data) +#define ID_val (curval.str_val) + +#define cur_line (yasm_linemap_get_current(parser_nasm->linemap)) + +#define p_expr_new_tree(l,o,r) yasm_expr_create_tree(l,o,r,cur_line) +#define p_expr_new_branch(o,r) yasm_expr_create_branch(o,r,cur_line) +#define p_expr_new_ident(r) yasm_expr_create_ident(r,cur_line) void nasm_parser_parse(yasm_parser_nasm *parser_nasm); void nasm_parser_cleanup(yasm_parser_nasm *parser_nasm); diff --git a/modules/parsers/nasm/nasm-token.re b/modules/parsers/nasm/nasm-token.re index b72883d9..144f9e4f 100644 --- a/modules/parsers/nasm/nasm-token.re +++ b/modules/parsers/nasm/nasm-token.re @@ -35,20 +35,20 @@ RCSID("$Id$"); #include "modules/parsers/nasm/nasm-parser.h" -#define BSIZE 8192 +#define BSIZE 8192 -#define YYCURSOR cursor -#define YYLIMIT (s->lim) -#define YYMARKER (s->ptr) -#define YYFILL(n) {fill(parser_nasm, &cursor);} +#define YYCURSOR cursor +#define YYLIMIT (s->lim) +#define YYMARKER (s->ptr) +#define YYFILL(n) {fill(parser_nasm, &cursor);} -#define RETURN(i) {s->cur = cursor; parser_nasm->tokch = s->tok[0]; \ - return i;} +#define RETURN(i) {s->cur = cursor; parser_nasm->tokch = s->tok[0]; \ + return i;} -#define SCANINIT() {s->tok = cursor;} +#define SCANINIT() {s->tok = cursor;} -#define TOK ((char *)s->tok) -#define TOKLEN (size_t)(cursor-s->tok) +#define TOK ((char *)s->tok) +#define TOKLEN (size_t)(cursor-s->tok) static size_t @@ -62,15 +62,15 @@ fill(yasm_parser_nasm *parser_nasm, YYCTYPE **cursor) { yasm_scanner *s = &parser_nasm->s; if (yasm_fill_helper(s, cursor, fill_input, parser_nasm->preproc) - && parser_nasm->save_input) { - int i; - YYCTYPE *saveline; - parser_nasm->save_last ^= 1; - saveline = parser_nasm->save_line[parser_nasm->save_last]; - /* save next line into cur_line */ - for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++) - saveline[i] = s->tok[i]; - saveline[i] = '\0'; + && parser_nasm->save_input) { + int i; + YYCTYPE *saveline; + parser_nasm->save_last ^= 1; + saveline = parser_nasm->save_line[parser_nasm->save_last]; + /* save next line into cur_line */ + for (i=0; i<79 && &s->tok[i] < s->lim && s->tok[i] != '\n'; i++) + saveline[i] = s->tok[i]; + saveline[i] = '\0'; } } @@ -86,15 +86,15 @@ save_line(yasm_parser_nasm *parser_nasm, YYCTYPE *cursor) /* save next line into cur_line */ if ((YYLIMIT - YYCURSOR) < 80) - YYFILL(80); + YYFILL(80); for (i=0; i<79 && &cursor[i] < s->lim && cursor[i] != '\n'; i++) - saveline[i] = cursor[i]; + saveline[i] = cursor[i]; saveline[i] = '\0'; return cursor; } /* starting size of string buffer */ -#define STRBUF_ALLOC_SIZE 128 +#define STRBUF_ALLOC_SIZE 128 /* string buffer used when parsing strings/character constants */ static YYCTYPE *strbuf = NULL; @@ -127,280 +127,280 @@ nasm_parser_lex(YYSTYPE *lvalp, yasm_parser_nasm *parser_nasm) /* Handle one token of lookahead */ if (parser_nasm->peek_token != NONE) { - int tok = parser_nasm->peek_token; - *lvalp = parser_nasm->peek_tokval; /* structure copy */ - parser_nasm->tokch = parser_nasm->peek_tokch; - parser_nasm->peek_token = NONE; - return tok; + int tok = parser_nasm->peek_token; + *lvalp = parser_nasm->peek_tokval; /* structure copy */ + parser_nasm->tokch = parser_nasm->peek_tokch; + parser_nasm->peek_token = NONE; + return tok; } /* Catch EOF */ if (s->eof && cursor == s->eof) - return 0; + return 0; /* Jump to proper "exclusive" states */ switch (parser_nasm->state) { - case DIRECTIVE: - goto directive; - case SECTION_DIRECTIVE: - goto section_directive; - case DIRECTIVE2: - goto directive2; - case LINECHG: - goto linechg; - case LINECHG2: - goto linechg2; - default: - break; + case DIRECTIVE: + goto directive; + case SECTION_DIRECTIVE: + goto section_directive; + case DIRECTIVE2: + goto directive2; + case LINECHG: + goto linechg; + case LINECHG2: + goto linechg2; + default: + break; } scan: SCANINIT(); /*!re2c - /* standard decimal integer */ - digit+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->intn = yasm_intnum_create_dec(TOK); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - /* 10010011b - binary number */ - - bindigit+ 'b' { - s->tok[TOKLEN-1] = '\0'; /* strip off 'b' */ - lvalp->intn = yasm_intnum_create_bin(TOK); - RETURN(INTNUM); - } - - /* 777q or 777o - octal number */ - octdigit+ [qQoO] { - s->tok[TOKLEN-1] = '\0'; /* strip off 'q' or 'o' */ - lvalp->intn = yasm_intnum_create_oct(TOK); - RETURN(INTNUM); - } - - /* 0AAh form of hexidecimal number */ - digit hexdigit* 'h' { - s->tok[TOKLEN-1] = '\0'; /* strip off 'h' */ - lvalp->intn = yasm_intnum_create_hex(TOK); - RETURN(INTNUM); - } - - /* $0AA and 0xAA forms of hexidecimal number */ - (("$" digit) | "0x") hexdigit+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - if (s->tok[1] == 'x') - /* skip 0 and x */ - lvalp->intn = yasm_intnum_create_hex(TOK+2); - else - /* don't skip 0 */ - lvalp->intn = yasm_intnum_create_hex(TOK+1); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - - /* floating point value */ - digit+ "." digit* ('e' [-+]? digit+)? { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->flt = yasm_floatnum_create(TOK); - s->tok[TOKLEN] = savech; - RETURN(FLTNUM); - } - - /* string/character constant values */ - quot { - endch = s->tok[0]; - goto stringconst; - } - - /* %line linenum+lineinc filename */ - "%line" { - parser_nasm->state = LINECHG; - linechg_numcount = 0; - RETURN(LINE); - } - - /* size specifiers */ - 'byte' { lvalp->int_info = 8; RETURN(SIZE_OVERRIDE); } - 'hword' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)/2; - RETURN(SIZE_OVERRIDE); - } - 'word' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch); - RETURN(SIZE_OVERRIDE); - } - 'dword' | 'long' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*2; - RETURN(SIZE_OVERRIDE); - } - 'qword' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*4; - RETURN(SIZE_OVERRIDE); - } - 'tword' { lvalp->int_info = 80; RETURN(SIZE_OVERRIDE); } - 'dqword' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*8; - RETURN(SIZE_OVERRIDE); - } - - /* pseudo-instructions */ - 'db' { lvalp->int_info = 8; RETURN(DECLARE_DATA); } - 'dhw' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)/2; - RETURN(DECLARE_DATA); - } - 'dw' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch); - RETURN(DECLARE_DATA); - } - 'dd' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*2; - RETURN(DECLARE_DATA); - } - 'dq' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*4; - RETURN(DECLARE_DATA); - } - 'dt' { lvalp->int_info = 80; RETURN(DECLARE_DATA); } - 'ddq' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*8; - RETURN(DECLARE_DATA); - } - - 'resb' { lvalp->int_info = 8; RETURN(RESERVE_SPACE); } - 'reshw' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)/2; - RETURN(RESERVE_SPACE); - } - 'resw' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch); - RETURN(RESERVE_SPACE); - } - 'resd' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*2; - RETURN(RESERVE_SPACE); - } - 'resq' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*4; - RETURN(RESERVE_SPACE); - } - 'rest' { lvalp->int_info = 80; RETURN(RESERVE_SPACE); } - 'resdq' { - lvalp->int_info = yasm_arch_wordsize(p_object->arch)*8; - RETURN(RESERVE_SPACE); - } - - 'incbin' { RETURN(INCBIN); } - - 'equ' { RETURN(EQU); } - - 'times' { RETURN(TIMES); } - - 'seg' { RETURN(SEG); } - 'wrt' { RETURN(WRT); } - - 'nosplit' { RETURN(NOSPLIT); } - 'strict' { RETURN(STRICT); } - - /* operators */ - "<<" { RETURN(LEFT_OP); } - ">>" { RETURN(RIGHT_OP); } - "//" { RETURN(SIGNDIV); } - "%%" { RETURN(SIGNMOD); } - "$$" { RETURN(START_SECTION_ID); } - [-+|^*&/%~$():=,\[] { RETURN(s->tok[0]); } - "]" { RETURN(s->tok[0]); } - - /* special non-local ..@label and labels like ..start */ - ".." [a-zA-Z0-9_$#@~.?]+ { - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - RETURN(SPECIAL_ID); - } - - /* local label (.label) */ - "." [a-zA-Z0-9_$#@~?][a-zA-Z0-9_$#@~.?]* { - if (!parser_nasm->locallabel_base) { - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - yasm_warn_set(YASM_WARN_GENERAL, - N_("no non-local label before `%s'"), - lvalp->str_val); - } else { - len = TOKLEN + parser_nasm->locallabel_base_len; - lvalp->str_val = yasm_xmalloc(len + 1); - strcpy(lvalp->str_val, parser_nasm->locallabel_base); - strncat(lvalp->str_val, TOK, TOKLEN); - lvalp->str_val[len] = '\0'; - } - - RETURN(LOCAL_ID); - } - - /* forced identifier */ - "$" [a-zA-Z0-9_$#@~.?]+ { - lvalp->str_val = yasm__xstrndup(TOK+1, TOKLEN-1); - RETURN(ID); - } - - /* identifier that may be a register, instruction, etc. */ - [a-zA-Z_?][a-zA-Z0-9_$#@~.?]* { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - if (parser_nasm->state != INSTRUCTION) - switch (yasm_arch_parse_check_insnprefix - (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { - case YASM_ARCH_INSN: - parser_nasm->state = INSTRUCTION; - s->tok[TOKLEN] = savech; - RETURN(INSN); - case YASM_ARCH_PREFIX: - s->tok[TOKLEN] = savech; - RETURN(PREFIX); - default: - break; - } - switch (yasm_arch_parse_check_regtmod - (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { - case YASM_ARCH_REG: - s->tok[TOKLEN] = savech; - RETURN(REG); - case YASM_ARCH_SEGREG: - s->tok[TOKLEN] = savech; - RETURN(SEGREG); - case YASM_ARCH_TARGETMOD: - s->tok[TOKLEN] = savech; - RETURN(TARGETMOD); - default: - s->tok[TOKLEN] = savech; - } - /* Propagate errors in case we got a warning from the arch */ - yasm_errwarn_propagate(parser_nasm->errwarns, cur_line); - /* Just an identifier, return as such. */ - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - RETURN(ID); - } - - ";" (any \ [\n])* { goto scan; } - - ws+ { goto scan; } - - "\n" { - if (parser_nasm->save_input) - cursor = save_line(parser_nasm, cursor); - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - any { - yasm_warn_set(YASM_WARN_UNREC_CHAR, - N_("ignoring unrecognized character `%s'"), - yasm__conv_unprint(s->tok[0])); - goto scan; - } + /* standard decimal integer */ + digit+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->intn = yasm_intnum_create_dec(TOK); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + /* 10010011b - binary number */ + + bindigit+ 'b' { + s->tok[TOKLEN-1] = '\0'; /* strip off 'b' */ + lvalp->intn = yasm_intnum_create_bin(TOK); + RETURN(INTNUM); + } + + /* 777q or 777o - octal number */ + octdigit+ [qQoO] { + s->tok[TOKLEN-1] = '\0'; /* strip off 'q' or 'o' */ + lvalp->intn = yasm_intnum_create_oct(TOK); + RETURN(INTNUM); + } + + /* 0AAh form of hexidecimal number */ + digit hexdigit* 'h' { + s->tok[TOKLEN-1] = '\0'; /* strip off 'h' */ + lvalp->intn = yasm_intnum_create_hex(TOK); + RETURN(INTNUM); + } + + /* $0AA and 0xAA forms of hexidecimal number */ + (("$" digit) | "0x") hexdigit+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + if (s->tok[1] == 'x') + /* skip 0 and x */ + lvalp->intn = yasm_intnum_create_hex(TOK+2); + else + /* don't skip 0 */ + lvalp->intn = yasm_intnum_create_hex(TOK+1); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + + /* floating point value */ + digit+ "." digit* ('e' [-+]? digit+)? { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->flt = yasm_floatnum_create(TOK); + s->tok[TOKLEN] = savech; + RETURN(FLTNUM); + } + + /* string/character constant values */ + quot { + endch = s->tok[0]; + goto stringconst; + } + + /* %line linenum+lineinc filename */ + "%line" { + parser_nasm->state = LINECHG; + linechg_numcount = 0; + RETURN(LINE); + } + + /* size specifiers */ + 'byte' { lvalp->int_info = 8; RETURN(SIZE_OVERRIDE); } + 'hword' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)/2; + RETURN(SIZE_OVERRIDE); + } + 'word' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch); + RETURN(SIZE_OVERRIDE); + } + 'dword' | 'long' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*2; + RETURN(SIZE_OVERRIDE); + } + 'qword' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*4; + RETURN(SIZE_OVERRIDE); + } + 'tword' { lvalp->int_info = 80; RETURN(SIZE_OVERRIDE); } + 'dqword' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*8; + RETURN(SIZE_OVERRIDE); + } + + /* pseudo-instructions */ + 'db' { lvalp->int_info = 8; RETURN(DECLARE_DATA); } + 'dhw' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)/2; + RETURN(DECLARE_DATA); + } + 'dw' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch); + RETURN(DECLARE_DATA); + } + 'dd' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*2; + RETURN(DECLARE_DATA); + } + 'dq' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*4; + RETURN(DECLARE_DATA); + } + 'dt' { lvalp->int_info = 80; RETURN(DECLARE_DATA); } + 'ddq' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*8; + RETURN(DECLARE_DATA); + } + + 'resb' { lvalp->int_info = 8; RETURN(RESERVE_SPACE); } + 'reshw' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)/2; + RETURN(RESERVE_SPACE); + } + 'resw' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch); + RETURN(RESERVE_SPACE); + } + 'resd' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*2; + RETURN(RESERVE_SPACE); + } + 'resq' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*4; + RETURN(RESERVE_SPACE); + } + 'rest' { lvalp->int_info = 80; RETURN(RESERVE_SPACE); } + 'resdq' { + lvalp->int_info = yasm_arch_wordsize(p_object->arch)*8; + RETURN(RESERVE_SPACE); + } + + 'incbin' { RETURN(INCBIN); } + + 'equ' { RETURN(EQU); } + + 'times' { RETURN(TIMES); } + + 'seg' { RETURN(SEG); } + 'wrt' { RETURN(WRT); } + + 'nosplit' { RETURN(NOSPLIT); } + 'strict' { RETURN(STRICT); } + + /* operators */ + "<<" { RETURN(LEFT_OP); } + ">>" { RETURN(RIGHT_OP); } + "//" { RETURN(SIGNDIV); } + "%%" { RETURN(SIGNMOD); } + "$$" { RETURN(START_SECTION_ID); } + [-+|^*&/%~$():=,\[] { RETURN(s->tok[0]); } + "]" { RETURN(s->tok[0]); } + + /* special non-local ..@label and labels like ..start */ + ".." [a-zA-Z0-9_$#@~.?]+ { + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + RETURN(SPECIAL_ID); + } + + /* local label (.label) */ + "." [a-zA-Z0-9_$#@~?][a-zA-Z0-9_$#@~.?]* { + if (!parser_nasm->locallabel_base) { + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + yasm_warn_set(YASM_WARN_GENERAL, + N_("no non-local label before `%s'"), + lvalp->str_val); + } else { + len = TOKLEN + parser_nasm->locallabel_base_len; + lvalp->str_val = yasm_xmalloc(len + 1); + strcpy(lvalp->str_val, parser_nasm->locallabel_base); + strncat(lvalp->str_val, TOK, TOKLEN); + lvalp->str_val[len] = '\0'; + } + + RETURN(LOCAL_ID); + } + + /* forced identifier */ + "$" [a-zA-Z0-9_$#@~.?]+ { + lvalp->str_val = yasm__xstrndup(TOK+1, TOKLEN-1); + RETURN(ID); + } + + /* identifier that may be a register, instruction, etc. */ + [a-zA-Z_?][a-zA-Z0-9_$#@~.?]* { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + if (parser_nasm->state != INSTRUCTION) + switch (yasm_arch_parse_check_insnprefix + (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { + case YASM_ARCH_INSN: + parser_nasm->state = INSTRUCTION; + s->tok[TOKLEN] = savech; + RETURN(INSN); + case YASM_ARCH_PREFIX: + s->tok[TOKLEN] = savech; + RETURN(PREFIX); + default: + break; + } + switch (yasm_arch_parse_check_regtmod + (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { + case YASM_ARCH_REG: + s->tok[TOKLEN] = savech; + RETURN(REG); + case YASM_ARCH_SEGREG: + s->tok[TOKLEN] = savech; + RETURN(SEGREG); + case YASM_ARCH_TARGETMOD: + s->tok[TOKLEN] = savech; + RETURN(TARGETMOD); + default: + s->tok[TOKLEN] = savech; + } + /* Propagate errors in case we got a warning from the arch */ + yasm_errwarn_propagate(parser_nasm->errwarns, cur_line); + /* Just an identifier, return as such. */ + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + RETURN(ID); + } + + ";" (any \ [\n])* { goto scan; } + + ws+ { goto scan; } + + "\n" { + if (parser_nasm->save_input) + cursor = save_line(parser_nasm, cursor); + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + any { + yasm_warn_set(YASM_WARN_UNREC_CHAR, + N_("ignoring unrecognized character `%s'"), + yasm__conv_unprint(s->tok[0])); + goto scan; + } */ /* %line linenum+lineinc filename */ @@ -408,60 +408,60 @@ linechg: SCANINIT(); /*!re2c - digit+ { - linechg_numcount++; - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->intn = yasm_intnum_create_dec(TOK); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - - "\n" { - if (parser_nasm->save_input) - cursor = save_line(parser_nasm, cursor); - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - "+" { - RETURN(s->tok[0]); - } - - ws+ { - if (linechg_numcount == 2) { - parser_nasm->state = LINECHG2; - goto linechg2; - } - goto linechg; - } - - any { - yasm_warn_set(YASM_WARN_UNREC_CHAR, - N_("ignoring unrecognized character `%s'"), - yasm__conv_unprint(s->tok[0])); - goto linechg; - } + digit+ { + linechg_numcount++; + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->intn = yasm_intnum_create_dec(TOK); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + + "\n" { + if (parser_nasm->save_input) + cursor = save_line(parser_nasm, cursor); + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + "+" { + RETURN(s->tok[0]); + } + + ws+ { + if (linechg_numcount == 2) { + parser_nasm->state = LINECHG2; + goto linechg2; + } + goto linechg; + } + + any { + yasm_warn_set(YASM_WARN_UNREC_CHAR, + N_("ignoring unrecognized character `%s'"), + yasm__conv_unprint(s->tok[0])); + goto linechg; + } */ linechg2: SCANINIT(); /*!re2c - "\n" { - if (parser_nasm->save_input) - cursor = save_line(parser_nasm, cursor); - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - "\r" { } - - (any \ [\r\n])+ { - parser_nasm->state = LINECHG; - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - RETURN(FILENAME); - } + "\n" { + if (parser_nasm->save_input) + cursor = save_line(parser_nasm, cursor); + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + "\r" { } + + (any \ [\r\n])+ { + parser_nasm->state = LINECHG; + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + RETURN(FILENAME); + } */ /* directive: [name value] */ @@ -469,29 +469,29 @@ directive: SCANINIT(); /*!re2c - [\]\n] { - if (parser_nasm->save_input) - cursor = save_line(parser_nasm, cursor); - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - [a-zA-Z_][a-zA-Z_0-9]* { - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - if (yasm__strcasecmp(lvalp->str_val, "section") == 0 || - yasm__strcasecmp(lvalp->str_val, "segment") == 0) - parser_nasm->state = SECTION_DIRECTIVE; - else - parser_nasm->state = DIRECTIVE2; - RETURN(DIRECTIVE_NAME); - } - - any { - yasm_warn_set(YASM_WARN_UNREC_CHAR, - N_("ignoring unrecognized character `%s'"), - yasm__conv_unprint(s->tok[0])); - goto directive; - } + [\]\n] { + if (parser_nasm->save_input) + cursor = save_line(parser_nasm, cursor); + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + [a-zA-Z_][a-zA-Z_0-9]* { + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + if (yasm__strcasecmp(lvalp->str_val, "section") == 0 || + yasm__strcasecmp(lvalp->str_val, "segment") == 0) + parser_nasm->state = SECTION_DIRECTIVE; + else + parser_nasm->state = DIRECTIVE2; + RETURN(DIRECTIVE_NAME); + } + + any { + yasm_warn_set(YASM_WARN_UNREC_CHAR, + N_("ignoring unrecognized character `%s'"), + yasm__conv_unprint(s->tok[0])); + goto directive; + } */ /* section directive (the section name portion thereof) */ @@ -499,42 +499,42 @@ section_directive: SCANINIT(); /*!re2c - [a-zA-Z0-9_$#@~.?-]+ { - lvalp->str.contents = yasm__xstrndup(TOK, TOKLEN); - lvalp->str.len = TOKLEN; - parser_nasm->state = DIRECTIVE2; - RETURN(STRING); - } - - quot { - parser_nasm->state = DIRECTIVE2; - endch = s->tok[0]; - goto stringconst; - } - - ws+ { - parser_nasm->state = DIRECTIVE2; - goto section_directive; - } - - "]" { - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - "\n" { - if (parser_nasm->save_input) - cursor = save_line(parser_nasm, cursor); - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - any { - yasm_warn_set(YASM_WARN_UNREC_CHAR, - N_("ignoring unrecognized character `%s'"), - yasm__conv_unprint(s->tok[0])); - goto section_directive; - } + [a-zA-Z0-9_$#@~.?-]+ { + lvalp->str.contents = yasm__xstrndup(TOK, TOKLEN); + lvalp->str.len = TOKLEN; + parser_nasm->state = DIRECTIVE2; + RETURN(STRING); + } + + quot { + parser_nasm->state = DIRECTIVE2; + endch = s->tok[0]; + goto stringconst; + } + + ws+ { + parser_nasm->state = DIRECTIVE2; + goto section_directive; + } + + "]" { + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + "\n" { + if (parser_nasm->save_input) + cursor = save_line(parser_nasm, cursor); + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + any { + yasm_warn_set(YASM_WARN_UNREC_CHAR, + N_("ignoring unrecognized character `%s'"), + yasm__conv_unprint(s->tok[0])); + goto section_directive; + } */ /* inner part of directive */ @@ -542,111 +542,111 @@ directive2: SCANINIT(); /*!re2c - /* standard decimal integer */ - digit+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - lvalp->intn = yasm_intnum_create_dec(TOK); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - /* 10010011b - binary number */ - - bindigit+ 'b' { - s->tok[TOKLEN-1] = '\0'; /* strip off 'b' */ - lvalp->intn = yasm_intnum_create_bin(TOK); - RETURN(INTNUM); - } - - /* 777q or 777o - octal number */ - octdigit+ [qQoO] { - s->tok[TOKLEN-1] = '\0'; /* strip off 'q' or 'o' */ - lvalp->intn = yasm_intnum_create_oct(TOK); - RETURN(INTNUM); - } - - /* 0AAh form of hexidecimal number */ - digit hexdigit* 'h' { - s->tok[TOKLEN-1] = '\0'; /* strip off 'h' */ - lvalp->intn = yasm_intnum_create_hex(TOK); - RETURN(INTNUM); - } - - /* $0AA and 0xAA forms of hexidecimal number */ - (("$" digit) | "0x") hexdigit+ { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - if (s->tok[1] == 'x') - /* skip 0 and x */ - lvalp->intn = yasm_intnum_create_hex(TOK+2); - else - /* don't skip 0 */ - lvalp->intn = yasm_intnum_create_hex(TOK+1); - s->tok[TOKLEN] = savech; - RETURN(INTNUM); - } - - /* string/character constant values */ - quot { - endch = s->tok[0]; - goto stringconst; - } - - /* operators */ - "<<" { RETURN(LEFT_OP); } - ">>" { RETURN(RIGHT_OP); } - "//" { RETURN(SIGNDIV); } - "%%" { RETURN(SIGNMOD); } - [-+|^*&/%~$():=,\[] { RETURN(s->tok[0]); } - - /* handle ] for directives */ - "]" { - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - /* forced identifier */ - "$" [a-zA-Z0-9_$#@~.?]+ { - lvalp->str_val = yasm__xstrndup(TOK+1, TOKLEN-1); - RETURN(ID); - } - - /* identifier; within directive, no local label mechanism */ - [a-zA-Z_.?][a-zA-Z0-9_$#@~.?]* { - savech = s->tok[TOKLEN]; - s->tok[TOKLEN] = '\0'; - switch (yasm_arch_parse_check_regtmod - (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { - case YASM_ARCH_REG: - s->tok[TOKLEN] = savech; - RETURN(REG); - default: - s->tok[TOKLEN] = savech; - } - /* Propagate errors in case we got a warning from the arch */ - yasm_errwarn_propagate(parser_nasm->errwarns, cur_line); - /* Just an identifier, return as such. */ - lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); - RETURN(ID); - } - - ";" (any \ [\n])* { goto directive2; } - - ws+ { goto directive2; } - - "\n" { - if (parser_nasm->save_input) - cursor = save_line(parser_nasm, cursor); - parser_nasm->state = INITIAL; - RETURN(s->tok[0]); - } - - any { - yasm_warn_set(YASM_WARN_UNREC_CHAR, - N_("ignoring unrecognized character `%s'"), - yasm__conv_unprint(s->tok[0])); - goto scan; - } + /* standard decimal integer */ + digit+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + lvalp->intn = yasm_intnum_create_dec(TOK); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + /* 10010011b - binary number */ + + bindigit+ 'b' { + s->tok[TOKLEN-1] = '\0'; /* strip off 'b' */ + lvalp->intn = yasm_intnum_create_bin(TOK); + RETURN(INTNUM); + } + + /* 777q or 777o - octal number */ + octdigit+ [qQoO] { + s->tok[TOKLEN-1] = '\0'; /* strip off 'q' or 'o' */ + lvalp->intn = yasm_intnum_create_oct(TOK); + RETURN(INTNUM); + } + + /* 0AAh form of hexidecimal number */ + digit hexdigit* 'h' { + s->tok[TOKLEN-1] = '\0'; /* strip off 'h' */ + lvalp->intn = yasm_intnum_create_hex(TOK); + RETURN(INTNUM); + } + + /* $0AA and 0xAA forms of hexidecimal number */ + (("$" digit) | "0x") hexdigit+ { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + if (s->tok[1] == 'x') + /* skip 0 and x */ + lvalp->intn = yasm_intnum_create_hex(TOK+2); + else + /* don't skip 0 */ + lvalp->intn = yasm_intnum_create_hex(TOK+1); + s->tok[TOKLEN] = savech; + RETURN(INTNUM); + } + + /* string/character constant values */ + quot { + endch = s->tok[0]; + goto stringconst; + } + + /* operators */ + "<<" { RETURN(LEFT_OP); } + ">>" { RETURN(RIGHT_OP); } + "//" { RETURN(SIGNDIV); } + "%%" { RETURN(SIGNMOD); } + [-+|^*&/%~$():=,\[] { RETURN(s->tok[0]); } + + /* handle ] for directives */ + "]" { + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + /* forced identifier */ + "$" [a-zA-Z0-9_$#@~.?]+ { + lvalp->str_val = yasm__xstrndup(TOK+1, TOKLEN-1); + RETURN(ID); + } + + /* identifier; within directive, no local label mechanism */ + [a-zA-Z_.?][a-zA-Z0-9_$#@~.?]* { + savech = s->tok[TOKLEN]; + s->tok[TOKLEN] = '\0'; + switch (yasm_arch_parse_check_regtmod + (p_object->arch, lvalp->arch_data, TOK, TOKLEN)) { + case YASM_ARCH_REG: + s->tok[TOKLEN] = savech; + RETURN(REG); + default: + s->tok[TOKLEN] = savech; + } + /* Propagate errors in case we got a warning from the arch */ + yasm_errwarn_propagate(parser_nasm->errwarns, cur_line); + /* Just an identifier, return as such. */ + lvalp->str_val = yasm__xstrndup(TOK, TOKLEN); + RETURN(ID); + } + + ";" (any \ [\n])* { goto directive2; } + + ws+ { goto directive2; } + + "\n" { + if (parser_nasm->save_input) + cursor = save_line(parser_nasm, cursor); + parser_nasm->state = INITIAL; + RETURN(s->tok[0]); + } + + any { + yasm_warn_set(YASM_WARN_UNREC_CHAR, + N_("ignoring unrecognized character `%s'"), + yasm__conv_unprint(s->tok[0])); + goto scan; + } */ /* string/character constant values */ @@ -659,35 +659,35 @@ stringconst_scan: SCANINIT(); /*!re2c - "\n" { - if (cursor == s->eof) - yasm_error_set(YASM_ERROR_SYNTAX, - N_("unexpected end of file in string")); - else - yasm_error_set(YASM_ERROR_SYNTAX, N_("unterminated string")); - strbuf[count] = '\0'; - lvalp->str.contents = (char *)strbuf; - lvalp->str.len = count; - if (parser_nasm->save_input) - cursor = save_line(parser_nasm, cursor); - RETURN(STRING); - } - - any { - if (s->tok[0] == endch) { - strbuf[count] = '\0'; - lvalp->str.contents = (char *)strbuf; - lvalp->str.len = count; - RETURN(STRING); - } - - strbuf[count++] = s->tok[0]; - if (count >= strbuf_size) { - strbuf = yasm_xrealloc(strbuf, strbuf_size + STRBUF_ALLOC_SIZE); - strbuf_size += STRBUF_ALLOC_SIZE; - } - - goto stringconst_scan; - } + "\n" { + if (cursor == s->eof) + yasm_error_set(YASM_ERROR_SYNTAX, + N_("unexpected end of file in string")); + else + yasm_error_set(YASM_ERROR_SYNTAX, N_("unterminated string")); + strbuf[count] = '\0'; + lvalp->str.contents = (char *)strbuf; + lvalp->str.len = count; + if (parser_nasm->save_input) + cursor = save_line(parser_nasm, cursor); + RETURN(STRING); + } + + any { + if (s->tok[0] == endch) { + strbuf[count] = '\0'; + lvalp->str.contents = (char *)strbuf; + lvalp->str.len = count; + RETURN(STRING); + } + + strbuf[count++] = s->tok[0]; + if (count >= strbuf_size) { + strbuf = yasm_xrealloc(strbuf, strbuf_size + STRBUF_ALLOC_SIZE); + strbuf_size += STRBUF_ALLOC_SIZE; + } + + goto stringconst_scan; + } */ } diff --git a/modules/preprocs/nasm/genmacro.c b/modules/preprocs/nasm/genmacro.c index 47d3ff37..3e7259a8 100644 --- a/modules/preprocs/nasm/genmacro.c +++ b/modules/preprocs/nasm/genmacro.c @@ -29,8 +29,8 @@ #include #include -#define OUTPUT "nasm-macros.c" -#define MAXLINE 1024 +#define OUTPUT "nasm-macros.c" +#define MAXLINE 1024 int main(int argc, char *argv[]) @@ -47,95 +47,95 @@ main(int argc, char *argv[]) size_t len; if (argc < 2) { - fprintf(stderr, "Usage: %s [ ...]\n", argv[0]); - return EXIT_FAILURE; + fprintf(stderr, "Usage: %s [ ...]\n", argv[0]); + return EXIT_FAILURE; } out = fopen(OUTPUT, "wt"); if (!out) { - fprintf(stderr, "Could not open `%s'.\n", OUTPUT); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s'.\n", OUTPUT); + return EXIT_FAILURE; } str = malloc(MAXLINE); fprintf(out, "/* This file auto-generated from standard.mac by genmacro.c" - " - don't edit it */\n\n#include \n\n" - "static const char *stdmac[] = {\n"); + " - don't edit it */\n\n#include \n\n" + "static const char *stdmac[] = {\n"); for (i=1; i 0 && (strp[len-1] == ' ' || strp[len-1] == '\t' || - strp[len-1] == '\n')) { - strp[len-1] = '\0'; - len--; - } - - /* skip blank lines */ - if (len == 0) - continue; - - /* check for special TASM ending token */ - if (strcmp(strp, "*END*TASM*MACROS*") == 0) { - tasm_count = lindex; - continue; - } - - /* output as string to output file */ - fprintf(out, " \""); - while (*strp != '\0') { - if (*strp == '\\' || *strp == '"') - fputc('\\', out); - fputc(*strp, out); - strp++; - } - fprintf(out, "\",\n"); - lindex++; - } - - fclose(in); + in = fopen(argv[i], "rt"); + if (!in) { + fprintf(stderr, "Could not open `%s'.\n", argv[i]); + fclose(out); + remove(OUTPUT); + return EXIT_FAILURE; + } + + fline = 0; + + while (fgets(str, MAXLINE, in)) { + line++; + fline++; + + strp = str; + + /* check for unterminated quotes and delete comments */ + charp = strp; + while ((charp = strpbrk(charp, "'\";"))) { + if (charp[0] == ';') { + *charp = '\0'; + break; + } + if ((charp = strchr(charp+1, charp[0])) == NULL) { + fprintf(stderr, "%s:%d: error: unterminated quote\n", + argv[i], fline); + fclose(out); + remove(OUTPUT); + return EXIT_FAILURE; + } + charp++; + } + + /* strip off leading and trailing whitespace */ + while (*strp == ' ' || *strp == '\t') + strp++; + len = strlen(strp); + while (len > 0 && (strp[len-1] == ' ' || strp[len-1] == '\t' || + strp[len-1] == '\n')) { + strp[len-1] = '\0'; + len--; + } + + /* skip blank lines */ + if (len == 0) + continue; + + /* check for special TASM ending token */ + if (strcmp(strp, "*END*TASM*MACROS*") == 0) { + tasm_count = lindex; + continue; + } + + /* output as string to output file */ + fprintf(out, " \""); + while (*strp != '\0') { + if (*strp == '\\' || *strp == '"') + fputc('\\', out); + fputc(*strp, out); + strp++; + } + fprintf(out, "\",\n"); + lindex++; + } + + fclose(in); } fprintf(out, " NULL\n};\n"); if (tasm_count == -1) - tasm_count = lindex; + tasm_count = lindex; fprintf(out, "#define TASM_MACRO_COUNT %d\n", tasm_count); fclose(out); diff --git a/modules/preprocs/nasm/genversion.c b/modules/preprocs/nasm/genversion.c index 30b7fccb..fdd65f30 100644 --- a/modules/preprocs/nasm/genversion.c +++ b/modules/preprocs/nasm/genversion.c @@ -25,7 +25,7 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -#include "config.h" /* for PACKAGE_VERSION */ +#include "config.h" /* for PACKAGE_VERSION */ #include #include @@ -39,38 +39,38 @@ main(int argc, char *argv[]) int major, minor, subminor; if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return EXIT_FAILURE; + fprintf(stderr, "Usage: %s \n", argv[0]); + return EXIT_FAILURE; } out = fopen(argv[1], "wt"); if (!out) { - fprintf(stderr, "Could not open `%s'.\n", argv[1]); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s'.\n", argv[1]); + return EXIT_FAILURE; } fprintf(out, "; This file auto-generated by genversion.c" - " - don't edit it\n"); + " - don't edit it\n"); if (sscanf(PACKAGE_INTVER, "%d.%d.%d", &major, &minor, &subminor) != 3) { - fprintf(stderr, "Version tokenizing error\n"); - fclose(out); - remove(argv[1]); - return EXIT_FAILURE; + fprintf(stderr, "Version tokenizing error\n"); + fclose(out); + remove(argv[1]); + return EXIT_FAILURE; } fprintf(out, "%%define __YASM_MAJOR__ %d\n", major); fprintf(out, "%%define __YASM_MINOR__ %d\n", minor); fprintf(out, "%%define __YASM_SUBMINOR__ %d\n", subminor); if (!isdigit(PACKAGE_BUILD[0])) - fprintf(out, "%%define __YASM_BUILD__ 0\n"); + fprintf(out, "%%define __YASM_BUILD__ 0\n"); else - fprintf(out, "%%define __YASM_BUILD__ %d\n", atoi(PACKAGE_BUILD)); + fprintf(out, "%%define __YASM_BUILD__ %d\n", atoi(PACKAGE_BUILD)); /* Version id (hex number) */ fprintf(out, "%%define __YASM_VERSION_ID__ 0%02x%02x%02x00h\n", major, - minor, subminor); + minor, subminor); /* Version string - version sans build */ fprintf(out, "%%define __YASM_VER__ \"%s\"\n", PACKAGE_INTVER); diff --git a/modules/preprocs/nasm/nasm-eval.c b/modules/preprocs/nasm/nasm-eval.c index 2b10d7e7..c896a1cc 100644 --- a/modules/preprocs/nasm/nasm-eval.c +++ b/modules/preprocs/nasm/nasm-eval.c @@ -18,11 +18,11 @@ #include "nasmlib.h" #include "nasm-eval.h" -static scanner scan; /* Address of scanner routine */ -static efunc error; /* Address of error reporting routine */ +static scanner scan; /* Address of scanner routine */ +static efunc error; /* Address of error reporting routine */ -static struct tokenval *tokval; /* The current token */ -static int i; /* The t_type of tokval */ +static struct tokenval *tokval; /* The current token */ +static int i; /* The t_type of tokval */ static void *scpriv; @@ -72,18 +72,18 @@ static yasm_expr *rexp0(void) e = rexp1(); if (!e) - return NULL; + return NULL; while (i == TOKEN_DBL_OR) - { - i = scan(scpriv, tokval); - f = rexp1(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - e = yasm_expr_create_tree(e, YASM_EXPR_LOR, f, 0); + { + i = scan(scpriv, tokval); + f = rexp1(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + e = yasm_expr_create_tree(e, YASM_EXPR_LOR, f, 0); } return e; } @@ -94,18 +94,18 @@ static yasm_expr *rexp1(void) e = rexp2(); if (!e) - return NULL; + return NULL; while (i == TOKEN_DBL_XOR) { - i = scan(scpriv, tokval); - f = rexp2(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - e = yasm_expr_create_tree(e, YASM_EXPR_LXOR, f, 0); + i = scan(scpriv, tokval); + f = rexp2(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + e = yasm_expr_create_tree(e, YASM_EXPR_LXOR, f, 0); } return e; } @@ -116,17 +116,17 @@ static yasm_expr *rexp2(void) e = rexp3(); if (!e) - return NULL; + return NULL; while (i == TOKEN_DBL_AND) { - i = scan(scpriv, tokval); - f = rexp3(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - e = yasm_expr_create_tree(e, YASM_EXPR_LAND, f, 0); + i = scan(scpriv, tokval); + f = rexp3(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + e = yasm_expr_create_tree(e, YASM_EXPR_LAND, f, 0); } return e; } @@ -137,40 +137,40 @@ static yasm_expr *rexp3(void) e = expr0(); if (!e) - return NULL; + return NULL; while (i == TOKEN_EQ || i == TOKEN_LT || i == TOKEN_GT || - i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) + i == TOKEN_NE || i == TOKEN_LE || i == TOKEN_GE) { - int j = i; - i = scan(scpriv, tokval); - f = expr0(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (j) - { - case TOKEN_EQ: - e = yasm_expr_create_tree(e, YASM_EXPR_EQ, f, 0); - break; - case TOKEN_LT: - e = yasm_expr_create_tree(e, YASM_EXPR_LT, f, 0); - break; - case TOKEN_GT: - e = yasm_expr_create_tree(e, YASM_EXPR_GT, f, 0); - break; - case TOKEN_NE: - e = yasm_expr_create_tree(e, YASM_EXPR_NE, f, 0); - break; - case TOKEN_LE: - e = yasm_expr_create_tree(e, YASM_EXPR_LE, f, 0); - break; - case TOKEN_GE: - e = yasm_expr_create_tree(e, YASM_EXPR_GE, f, 0); - break; - } + int j = i; + i = scan(scpriv, tokval); + f = expr0(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (j) + { + case TOKEN_EQ: + e = yasm_expr_create_tree(e, YASM_EXPR_EQ, f, 0); + break; + case TOKEN_LT: + e = yasm_expr_create_tree(e, YASM_EXPR_LT, f, 0); + break; + case TOKEN_GT: + e = yasm_expr_create_tree(e, YASM_EXPR_GT, f, 0); + break; + case TOKEN_NE: + e = yasm_expr_create_tree(e, YASM_EXPR_NE, f, 0); + break; + case TOKEN_LE: + e = yasm_expr_create_tree(e, YASM_EXPR_LE, f, 0); + break; + case TOKEN_GE: + e = yasm_expr_create_tree(e, YASM_EXPR_GE, f, 0); + break; + } } return e; } @@ -181,18 +181,18 @@ static yasm_expr *expr0(void) e = expr1(); if (!e) - return NULL; + return NULL; while (i == '|') { - i = scan(scpriv, tokval); - f = expr1(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - e = yasm_expr_create_tree(e, YASM_EXPR_OR, f, 0); + i = scan(scpriv, tokval); + f = expr1(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + e = yasm_expr_create_tree(e, YASM_EXPR_OR, f, 0); } return e; } @@ -203,17 +203,17 @@ static yasm_expr *expr1(void) e = expr2(); if (!e) - return NULL; + return NULL; while (i == '^') { - i = scan(scpriv, tokval); - f = expr2(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - e = yasm_expr_create_tree(e, YASM_EXPR_XOR, f, 0); + i = scan(scpriv, tokval); + f = expr2(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + e = yasm_expr_create_tree(e, YASM_EXPR_XOR, f, 0); } return e; } @@ -224,17 +224,17 @@ static yasm_expr *expr2(void) e = expr3(); if (!e) - return NULL; + return NULL; while (i == '&') { - i = scan(scpriv, tokval); - f = expr3(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - e = yasm_expr_create_tree(e, YASM_EXPR_AND, f, 0); + i = scan(scpriv, tokval); + f = expr3(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + e = yasm_expr_create_tree(e, YASM_EXPR_AND, f, 0); } return e; } @@ -245,26 +245,26 @@ static yasm_expr *expr3(void) e = expr4(); if (!e) - return NULL; + return NULL; while (i == TOKEN_SHL || i == TOKEN_SHR) { - int j = i; - i = scan(scpriv, tokval); - f = expr4(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - - switch (j) { - case TOKEN_SHL: - e = yasm_expr_create_tree(e, YASM_EXPR_SHL, f, 0); - break; - case TOKEN_SHR: - e = yasm_expr_create_tree(e, YASM_EXPR_SHR, f, 0); - break; - } + int j = i; + i = scan(scpriv, tokval); + f = expr4(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + + switch (j) { + case TOKEN_SHL: + e = yasm_expr_create_tree(e, YASM_EXPR_SHL, f, 0); + break; + case TOKEN_SHR: + e = yasm_expr_create_tree(e, YASM_EXPR_SHR, f, 0); + break; + } } return e; } @@ -275,24 +275,24 @@ static yasm_expr *expr4(void) e = expr5(); if (!e) - return NULL; + return NULL; while (i == '+' || i == '-') { - int j = i; - i = scan(scpriv, tokval); - f = expr5(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - switch (j) { - case '+': - e = yasm_expr_create_tree(e, YASM_EXPR_ADD, f, 0); - break; - case '-': - e = yasm_expr_create_tree(e, YASM_EXPR_SUB, f, 0); - break; - } + int j = i; + i = scan(scpriv, tokval); + f = expr5(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + switch (j) { + case '+': + e = yasm_expr_create_tree(e, YASM_EXPR_ADD, f, 0); + break; + case '-': + e = yasm_expr_create_tree(e, YASM_EXPR_SUB, f, 0); + break; + } } return e; } @@ -303,34 +303,34 @@ static yasm_expr *expr5(void) e = expr6(); if (!e) - return NULL; + return NULL; while (i == '*' || i == '/' || i == '%' || - i == TOKEN_SDIV || i == TOKEN_SMOD) + i == TOKEN_SDIV || i == TOKEN_SMOD) { - int j = i; - i = scan(scpriv, tokval); - f = expr6(); - if (!f) { - yasm_expr_destroy(e); - return NULL; - } - switch (j) { - case '*': - e = yasm_expr_create_tree(e, YASM_EXPR_MUL, f, 0); - break; - case '/': - e = yasm_expr_create_tree(e, YASM_EXPR_DIV, f, 0); - break; - case '%': - e = yasm_expr_create_tree(e, YASM_EXPR_MOD, f, 0); - break; - case TOKEN_SDIV: - e = yasm_expr_create_tree(e, YASM_EXPR_SIGNDIV, f, 0); - break; - case TOKEN_SMOD: - e = yasm_expr_create_tree(e, YASM_EXPR_SIGNMOD, f, 0); - break; - } + int j = i; + i = scan(scpriv, tokval); + f = expr6(); + if (!f) { + yasm_expr_destroy(e); + return NULL; + } + switch (j) { + case '*': + e = yasm_expr_create_tree(e, YASM_EXPR_MUL, f, 0); + break; + case '/': + e = yasm_expr_create_tree(e, YASM_EXPR_DIV, f, 0); + break; + case '%': + e = yasm_expr_create_tree(e, YASM_EXPR_MOD, f, 0); + break; + case TOKEN_SDIV: + e = yasm_expr_create_tree(e, YASM_EXPR_SIGNDIV, f, 0); + break; + case TOKEN_SMOD: + e = yasm_expr_create_tree(e, YASM_EXPR_SIGNMOD, f, 0); + break; + } } return e; } @@ -340,73 +340,73 @@ static yasm_expr *expr6(void) yasm_expr *e = NULL; if (i == '-') { - i = scan(scpriv, tokval); - e = expr6(); - if (!e) - return NULL; - return yasm_expr_create_branch(YASM_EXPR_NEG, e, 0); + i = scan(scpriv, tokval); + e = expr6(); + if (!e) + return NULL; + return yasm_expr_create_branch(YASM_EXPR_NEG, e, 0); } else if (i == '+') { - i = scan(scpriv, tokval); - return expr6(); + i = scan(scpriv, tokval); + return expr6(); } else if (i == '~') { - i = scan(scpriv, tokval); - e = expr6(); - if (!e) - return NULL; - return yasm_expr_create_branch(YASM_EXPR_NOT, e, 0); + i = scan(scpriv, tokval); + e = expr6(); + if (!e) + return NULL; + return yasm_expr_create_branch(YASM_EXPR_NOT, e, 0); } else if (i == TOKEN_SEG) { - i = scan(scpriv, tokval); - e = expr6(); - if (!e) - return NULL; - error(ERR_NONFATAL, "%s not supported", "SEG"); - return e; + i = scan(scpriv, tokval); + e = expr6(); + if (!e) + return NULL; + error(ERR_NONFATAL, "%s not supported", "SEG"); + return e; } else if (i == '(') { - i = scan(scpriv, tokval); - e = bexpr(); - if (!e) - return NULL; - if (i != ')') { - error(ERR_NONFATAL, "expecting `)'"); - return NULL; - } - i = scan(scpriv, tokval); - return e; + i = scan(scpriv, tokval); + e = bexpr(); + if (!e) + return NULL; + if (i != ')') { + error(ERR_NONFATAL, "expecting `)'"); + return NULL; + } + i = scan(scpriv, tokval); + return e; } else if (i == TOKEN_NUM || i == TOKEN_ID || - i == TOKEN_HERE || i == TOKEN_BASE) + i == TOKEN_HERE || i == TOKEN_BASE) { - switch (i) { - case TOKEN_NUM: - e = yasm_expr_create_ident(yasm_expr_int(tokval->t_integer), 0); - break; - case TOKEN_ID: - case TOKEN_HERE: - case TOKEN_BASE: - error(ERR_NONFATAL, - "cannot reference symbol `%s' in preprocessor", - (i == TOKEN_ID ? tokval->t_charptr : - i == TOKEN_HERE ? "$" : "$$")); - e = yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_int(1)), - 0); - break; - } - i = scan(scpriv, tokval); - return e; + switch (i) { + case TOKEN_NUM: + e = yasm_expr_create_ident(yasm_expr_int(tokval->t_integer), 0); + break; + case TOKEN_ID: + case TOKEN_HERE: + case TOKEN_BASE: + error(ERR_NONFATAL, + "cannot reference symbol `%s' in preprocessor", + (i == TOKEN_ID ? tokval->t_charptr : + i == TOKEN_HERE ? "$" : "$$")); + e = yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_int(1)), + 0); + break; + } + i = scan(scpriv, tokval); + return e; } else { - error(ERR_NONFATAL, "expression syntax error"); - return NULL; + error(ERR_NONFATAL, "expression syntax error"); + return NULL; } } yasm_expr *nasm_evaluate (scanner sc, void *scprivate, struct tokenval *tv, - int critical, efunc report_error) + int critical, efunc report_error) { if (critical & CRITICAL) { - critical &= ~CRITICAL; - bexpr = rexp0; + critical &= ~CRITICAL; + bexpr = rexp0; } else - bexpr = expr0; + bexpr = expr0; scan = sc; scpriv = scprivate; @@ -414,9 +414,9 @@ yasm_expr *nasm_evaluate (scanner sc, void *scprivate, struct tokenval *tv, error = report_error; if (tokval->t_type == TOKEN_INVALID) - i = scan(scpriv, tokval); + i = scan(scpriv, tokval); else - i = tokval->t_type; + i = tokval->t_type; return bexpr (); } diff --git a/modules/preprocs/nasm/nasm-eval.h b/modules/preprocs/nasm/nasm-eval.h index 32e3da8b..f06937c5 100644 --- a/modules/preprocs/nasm/nasm-eval.h +++ b/modules/preprocs/nasm/nasm-eval.h @@ -13,6 +13,6 @@ * The evaluator itself. */ yasm_expr *nasm_evaluate (scanner sc, void *scprivate, struct tokenval *tv, - int critical, efunc report_error); + int critical, efunc report_error); #endif diff --git a/modules/preprocs/nasm/nasm-pp.c b/modules/preprocs/nasm/nasm-pp.c index 2b65330b..604bb2bc 100644 --- a/modules/preprocs/nasm/nasm-pp.c +++ b/modules/preprocs/nasm/nasm-pp.c @@ -92,21 +92,21 @@ struct MMacro char *name; int casesense; long nparam_min, nparam_max; - int plus; /* is the last parameter greedy? */ - int nolist; /* is this macro listing-inhibited? */ + int plus; /* is the last parameter greedy? */ + int nolist; /* is this macro listing-inhibited? */ int in_progress; - Token *dlist; /* All defaults as one list */ - Token **defaults; /* Parameter default pointers */ - int ndefs; /* number of default parameters */ + Token *dlist; /* All defaults as one list */ + Token **defaults; /* Parameter default pointers */ + int ndefs; /* number of default parameters */ Line *expansion; MMacro *next_active; - MMacro *rep_nest; /* used for nesting %rep */ - Token **params; /* actual parameters */ - Token *iline; /* invocation line */ + MMacro *rep_nest; /* used for nesting %rep */ + Token **params; /* actual parameters */ + Token *iline; /* invocation line */ long nparam, rotate, *paramlen; unsigned long unique; - int lineno; /* Current line number on expansion */ + int lineno; /* Current line number on expansion */ }; /* @@ -143,7 +143,7 @@ struct Token { Token *next; char *text; - SMacro *mac; /* associated macro for TOK_SMAC_END */ + SMacro *mac; /* associated macro for TOK_SMAC_END */ int type; }; enum @@ -194,7 +194,7 @@ struct Include Line *expansion; char *fname; int lineno, lineinc; - MMacro *mstk; /* stack of active macros/reps */ + MMacro *mstk; /* stack of active macros/reps */ }; /* @@ -242,7 +242,7 @@ enum * These defines are used as the possible return values for do_directive */ #define NO_DIRECTIVE_FOUND 0 -#define DIRECTIVE_FOUND 1 +#define DIRECTIVE_FOUND 1 /* * Condition codes. Note that we use c_ prefix not C_ because C_ is @@ -308,7 +308,7 @@ enum static int is_condition(int arg) { return ((arg >= PP_ELIF) && (arg <= PP_ENDIF)) || - ((arg >= PP_IF) && (arg <= PP_IFSTR)); + ((arg >= PP_IF) && (arg <= PP_IFSTR)); } /* For TASM compatibility we need to be able to recognise TASM compatible @@ -343,12 +343,12 @@ static Include *istk; static FILE *first_fp = NULL; -static efunc _error; /* Pointer to client-provided error reporting function */ +static efunc _error; /* Pointer to client-provided error reporting function */ static evalfunc evaluate; -static int pass; /* HACK: pass 0 = generate dependencies only */ +static int pass; /* HACK: pass 0 = generate dependencies only */ -static unsigned long unique; /* unique identifier numbers */ +static unsigned long unique; /* unique identifier numbers */ static Line *builtindef = NULL; static Line *predef = NULL; @@ -405,8 +405,8 @@ static int nested_mac_count, nested_rep_count; #define TOKEN_BLOCKSIZE 4096 static Token *freeTokens = NULL; struct Blocks { - Blocks *next; - void *chunk; + Blocks *next; + void *chunk; }; static Blocks blocks = { NULL, NULL }; @@ -423,7 +423,7 @@ static void error(int severity, const char *fmt, ...); static void *new_Block(size_t size); static void delete_Blocks(void); static Token *new_Token(Token * next, int type, const char *text, - size_t txtlen); + size_t txtlen); static Token *delete_Token(Token * t); /* @@ -448,56 +448,56 @@ check_tasm_directive(char *line) /* Skip whitespace */ while (isspace(*p) && *p != 0) - p++; + p++; /* Binary search for the directive name */ i = -1; j = elements(tasm_directives); len = 0; while (!isspace(p[len]) && p[len] != 0) - len++; + len++; if (len) { - oldchar = p[len]; - p[len] = 0; - while (j - i > 1) - { - k = (j + i) / 2; - m = nasm_stricmp(p, tasm_directives[k]); - if (m == 0) - { - /* We have found a directive, so jam a % in front of it - * so that NASM will then recognise it as one if it's own. - */ - p[len] = oldchar; - len = strlen(p); - oldline = line; - line = nasm_malloc(len + 2); - line[0] = '%'; - if (k == TM_IFDIFI) - { - /* NASM does not recognise IFDIFI, so we convert it to - * %ifdef BOGUS. This is not used in NASM comaptible - * code, but does need to parse for the TASM macro - * package. - */ - strcpy(line + 1, "ifdef BOGUS"); - } - else - { - memcpy(line + 1, p, len + 1); - } - nasm_free(oldline); - return line; - } - else if (m < 0) - { - j = k; - } - else - i = k; - } - p[len] = oldchar; + oldchar = p[len]; + p[len] = 0; + while (j - i > 1) + { + k = (j + i) / 2; + m = nasm_stricmp(p, tasm_directives[k]); + if (m == 0) + { + /* We have found a directive, so jam a % in front of it + * so that NASM will then recognise it as one if it's own. + */ + p[len] = oldchar; + len = strlen(p); + oldline = line; + line = nasm_malloc(len + 2); + line[0] = '%'; + if (k == TM_IFDIFI) + { + /* NASM does not recognise IFDIFI, so we convert it to + * %ifdef BOGUS. This is not used in NASM comaptible + * code, but does need to parse for the TASM macro + * package. + */ + strcpy(line + 1, "ifdef BOGUS"); + } + else + { + memcpy(line + 1, p, len + 1); + } + nasm_free(oldline); + return line; + } + else if (m < 0) + { + j = k; + } + else + i = k; + } + p[len] = oldchar; } return line; } @@ -517,19 +517,19 @@ prepreproc(char *line) if (line[0] == '#' && line[1] == ' ') { - oldline = line; - fname = oldline + 2; - lineno = atoi(fname); - fname += strspn(fname, "0123456789 "); - if (*fname == '"') - fname++; - fnlen = strcspn(fname, "\""); - line = nasm_malloc(20 + fnlen); - sprintf(line, "%%line %d %.*s", lineno, (int)fnlen, fname); - nasm_free(oldline); + oldline = line; + fname = oldline + 2; + lineno = atoi(fname); + fname += strspn(fname, "0123456789 "); + if (*fname == '"') + fname++; + fnlen = strcspn(fname, "\""); + line = nasm_malloc(20 + fnlen); + sprintf(line, "%%line %d %.*s", lineno, (int)fnlen, fname); + nasm_free(oldline); } if (tasm_compatible_mode) - return check_tasm_directive(line); + return check_tasm_directive(line); return line; } @@ -548,17 +548,17 @@ hash(char *s) * Powers of three, mod 31. */ static const int multipliers[] = { - 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, - 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21 + 1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, + 30, 28, 22, 4, 12, 5, 15, 14, 11, 2, 6, 18, 23, 7, 21 }; while (*s) { - h += multipliers[i] * (unsigned char) (toupper(*s)); - s++; - if (++i >= elements(multipliers)) - i = 0; + h += multipliers[i] * (unsigned char) (toupper(*s)); + s++; + if (++i >= elements(multipliers)) + i = 0; } h %= NHASH; return h; @@ -572,7 +572,7 @@ free_tlist(Token * list_) { while (list_) { - list_ = delete_Token(list_); + list_ = delete_Token(list_); } } @@ -585,10 +585,10 @@ free_llist(Line * list_) Line *l; while (list_) { - l = list_; - list_ = list_->next; - free_tlist(l->first); - nasm_free(l); + l = list_; + list_ = list_->next; + free_tlist(l->first); + nasm_free(l); } } @@ -618,11 +618,11 @@ ctx_pop(void) smac = c->localmac; while (smac) { - s = smac; - smac = smac->next; - nasm_free(s->name); - free_tlist(s->expansion); - nasm_free(s); + s = smac; + smac = smac->next; + nasm_free(s->name); + free_tlist(s->expansion); + nasm_free(s); } nasm_free(c->name); nasm_free(c); @@ -647,61 +647,61 @@ read_line(void) /* Nasty hack for builtin defines */ for (pd = builtindef; pd; pd = pd->next) { - head = NULL; - tail = &head; - for (t = pd->first; t; t = t->next) - { - *tail = new_Token(NULL, t->type, t->text, 0); - tail = &(*tail)->next; - } - l = nasm_malloc(sizeof(Line)); - l->next = istk->expansion; - l->first = head; - l->finishes = FALSE; - istk->expansion = l; + head = NULL; + tail = &head; + for (t = pd->first; t; t = t->next) + { + *tail = new_Token(NULL, t->type, t->text, 0); + tail = &(*tail)->next; + } + l = nasm_malloc(sizeof(Line)); + l->next = istk->expansion; + l->first = head; + l->finishes = FALSE; + istk->expansion = l; } if (stdmacpos) { - if (*stdmacpos) - { - char *ret = nasm_strdup(*stdmacpos++); - if (!*stdmacpos && any_extrastdmac) - { - stdmacpos = extrastdmac; - any_extrastdmac = FALSE; - return ret; - } - /* - * Nasty hack: here we push the contents of `predef' on - * to the top-level expansion stack, since this is the - * most convenient way to implement the pre-include and - * pre-define features. - */ - if (!*stdmacpos) - { - for (pd = predef; pd; pd = pd->next) - { - head = NULL; - tail = &head; - for (t = pd->first; t; t = t->next) - { - *tail = new_Token(NULL, t->type, t->text, 0); - tail = &(*tail)->next; - } - l = nasm_malloc(sizeof(Line)); - l->next = istk->expansion; - l->first = head; - l->finishes = FALSE; - istk->expansion = l; - } - } - return ret; - } - else - { - stdmacpos = NULL; - } + if (*stdmacpos) + { + char *ret = nasm_strdup(*stdmacpos++); + if (!*stdmacpos && any_extrastdmac) + { + stdmacpos = extrastdmac; + any_extrastdmac = FALSE; + return ret; + } + /* + * Nasty hack: here we push the contents of `predef' on + * to the top-level expansion stack, since this is the + * most convenient way to implement the pre-include and + * pre-define features. + */ + if (!*stdmacpos) + { + for (pd = predef; pd; pd = pd->next) + { + head = NULL; + tail = &head; + for (t = pd->first; t; t = t->next) + { + *tail = new_Token(NULL, t->type, t->text, 0); + tail = &(*tail)->next; + } + l = nasm_malloc(sizeof(Line)); + l->next = istk->expansion; + l->first = head; + l->finishes = FALSE; + istk->expansion = l; + } + } + return ret; + } + else + { + stdmacpos = NULL; + } } bufsize = BUF_DELTA; @@ -710,12 +710,12 @@ read_line(void) continued_count = 0; while (1) { - q = fgets(p, bufsize - (int)(p - buffer), istk->fp); - if (!q) - break; - p += strlen(p); - if (p > buffer && p[-1] == '\n') - { + q = fgets(p, bufsize - (int)(p - buffer), istk->fp); + if (!q) + break; + p += strlen(p); + if (p > buffer && p[-1] == '\n') + { /* Convert backslash-CRLF line continuation sequences into nothing at all (for DOS and Windows) */ if (((p - 2) > buffer) && (p[-3] == '\\') && (p[-2] == '\r')) { @@ -733,20 +733,20 @@ read_line(void) else { break; } - } - if (p - buffer > bufsize - 10) - { - long offset = (long)(p - buffer); - bufsize += BUF_DELTA; - buffer = nasm_realloc(buffer, (size_t)bufsize); - p = buffer + offset; /* prevent stale-pointer problems */ - } + } + if (p - buffer > bufsize - 10) + { + long offset = (long)(p - buffer); + bufsize += BUF_DELTA; + buffer = nasm_realloc(buffer, (size_t)bufsize); + p = buffer + offset; /* prevent stale-pointer problems */ + } } if (!q && p == buffer) { - nasm_free(buffer); - return NULL; + nasm_free(buffer); + return NULL; } nasm_src_set_linnum(nasm_src_get_linnum() + istk->lineinc + (continued_count * istk->lineinc)); @@ -756,7 +756,7 @@ read_line(void) * present at the end of the line. */ while (--p >= buffer && (*p == '\n' || *p == '\r')) - *p = '\0'; + *p = '\0'; /* * Handle spurious ^Z, which may be inserted into source files @@ -784,153 +784,153 @@ tokenise(char *line) while (*line) { - p = line; - if (*p == '%') - { - p++; - if ( isdigit(*p) || - ((*p == '-' || *p == '+') && isdigit(p[1])) || - ((*p == '+') && (isspace(p[1]) || !p[1]))) - { - do - { - p++; - } - while (isdigit(*p)); - type = TOK_PREPROC_ID; - } - else if (*p == '{') - { - p++; - while (*p && *p != '}') - { - p[-1] = *p; - p++; - } - p[-1] = '\0'; - if (*p) - p++; - type = TOK_PREPROC_ID; - } - else if (isidchar(*p) || - ((*p == '!' || *p == '%' || *p == '$') && - isidchar(p[1]))) - { - do - { - p++; - } - while (isidchar(*p)); - type = TOK_PREPROC_ID; - } - else - { - type = TOK_OTHER; - if (*p == '%') - p++; - } - } - else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) - { - type = TOK_ID; - p++; - while (*p && isidchar(*p)) - p++; - } - else if (*p == '\'' || *p == '"') - { - /* - * A string token. - */ - char c = *p; - p++; - type = TOK_STRING; - while (*p && *p != c) - p++; - - if (*p) - { - p++; - } - else - { - error(ERR_WARNING, "unterminated string"); - type = -1; - } - } - else if (isnumstart(*p)) - { - /* - * A number token. - */ - type = TOK_NUMBER; - p++; - while (*p && isnumchar(*p)) - p++; - } - else if (isspace(*p)) - { - type = TOK_WHITESPACE; - p++; - while (*p && isspace(*p)) - p++; - /* - * Whitespace just before end-of-line is discarded by - * pretending it's a comment; whitespace just before a - * comment gets lumped into the comment. - */ - if (!*p || *p == ';') - { - type = TOK_COMMENT; - while (*p) - p++; - } - } - else if (*p == ';') - { - type = TOK_COMMENT; - while (*p) - p++; - } - else - { - /* - * Anything else is an operator of some kind. We check - * for all the double-character operators (>>, <<, //, - * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything - * else is a single-character operator. - */ - type = TOK_OTHER; - if ((p[0] == '>' && p[1] == '>') || - (p[0] == '<' && p[1] == '<') || - (p[0] == '/' && p[1] == '/') || - (p[0] == '<' && p[1] == '=') || - (p[0] == '>' && p[1] == '=') || - (p[0] == '=' && p[1] == '=') || - (p[0] == '!' && p[1] == '=') || - (p[0] == '<' && p[1] == '>') || - (p[0] == '&' && p[1] == '&') || - (p[0] == '|' && p[1] == '|') || - (p[0] == '^' && p[1] == '^')) - { - p++; - } - p++; - } - - /* Handle unterminated string */ - if (type == -1) - { - *tail = t = new_Token(NULL, TOK_STRING, line, (size_t)(p-line)+1); - t->text[p-line] = *line; - tail = &t->next; - } - else if (type != TOK_COMMENT) - { - *tail = t = new_Token(NULL, type, line, (size_t)(p - line)); - tail = &t->next; - } - line = p; + p = line; + if (*p == '%') + { + p++; + if ( isdigit(*p) || + ((*p == '-' || *p == '+') && isdigit(p[1])) || + ((*p == '+') && (isspace(p[1]) || !p[1]))) + { + do + { + p++; + } + while (isdigit(*p)); + type = TOK_PREPROC_ID; + } + else if (*p == '{') + { + p++; + while (*p && *p != '}') + { + p[-1] = *p; + p++; + } + p[-1] = '\0'; + if (*p) + p++; + type = TOK_PREPROC_ID; + } + else if (isidchar(*p) || + ((*p == '!' || *p == '%' || *p == '$') && + isidchar(p[1]))) + { + do + { + p++; + } + while (isidchar(*p)); + type = TOK_PREPROC_ID; + } + else + { + type = TOK_OTHER; + if (*p == '%') + p++; + } + } + else if (isidstart(*p) || (*p == '$' && isidstart(p[1]))) + { + type = TOK_ID; + p++; + while (*p && isidchar(*p)) + p++; + } + else if (*p == '\'' || *p == '"') + { + /* + * A string token. + */ + char c = *p; + p++; + type = TOK_STRING; + while (*p && *p != c) + p++; + + if (*p) + { + p++; + } + else + { + error(ERR_WARNING, "unterminated string"); + type = -1; + } + } + else if (isnumstart(*p)) + { + /* + * A number token. + */ + type = TOK_NUMBER; + p++; + while (*p && isnumchar(*p)) + p++; + } + else if (isspace(*p)) + { + type = TOK_WHITESPACE; + p++; + while (*p && isspace(*p)) + p++; + /* + * Whitespace just before end-of-line is discarded by + * pretending it's a comment; whitespace just before a + * comment gets lumped into the comment. + */ + if (!*p || *p == ';') + { + type = TOK_COMMENT; + while (*p) + p++; + } + } + else if (*p == ';') + { + type = TOK_COMMENT; + while (*p) + p++; + } + else + { + /* + * Anything else is an operator of some kind. We check + * for all the double-character operators (>>, <<, //, + * %%, <=, >=, ==, !=, <>, &&, ||, ^^), but anything + * else is a single-character operator. + */ + type = TOK_OTHER; + if ((p[0] == '>' && p[1] == '>') || + (p[0] == '<' && p[1] == '<') || + (p[0] == '/' && p[1] == '/') || + (p[0] == '<' && p[1] == '=') || + (p[0] == '>' && p[1] == '=') || + (p[0] == '=' && p[1] == '=') || + (p[0] == '!' && p[1] == '=') || + (p[0] == '<' && p[1] == '>') || + (p[0] == '&' && p[1] == '&') || + (p[0] == '|' && p[1] == '|') || + (p[0] == '^' && p[1] == '^')) + { + p++; + } + p++; + } + + /* Handle unterminated string */ + if (type == -1) + { + *tail = t = new_Token(NULL, TOK_STRING, line, (size_t)(p-line)+1); + t->text[p-line] = *line; + tail = &t->next; + } + else if (type != TOK_COMMENT) + { + *tail = t = new_Token(NULL, type, line, (size_t)(p - line)); + tail = &t->next; + } + line = p; } return list_; } @@ -943,20 +943,20 @@ tokenise(char *line) static void * new_Block(size_t size) { - Blocks *b = &blocks; - - /* first, get to the end of the linked list */ - while (b->next) - b = b->next; - /* now allocate the requested chunk */ - b->chunk = nasm_malloc(size); - - /* now allocate a new block for the next request */ - b->next = nasm_malloc(sizeof(Blocks)); - /* and initialize the contents of the new block */ - b->next->next = NULL; - b->next->chunk = NULL; - return b->chunk; + Blocks *b = &blocks; + + /* first, get to the end of the linked list */ + while (b->next) + b = b->next; + /* now allocate the requested chunk */ + b->chunk = nasm_malloc(size); + + /* now allocate a new block for the next request */ + b->next = nasm_malloc(sizeof(Blocks)); + /* and initialize the contents of the new block */ + b->next->next = NULL; + b->next->chunk = NULL; + return b->chunk; } /* @@ -965,23 +965,23 @@ new_Block(size_t size) static void delete_Blocks(void) { - Blocks *a,*b = &blocks; - - /* - * keep in mind that the first block, pointed to by blocks - * is a static and not dynamically allocated, so we don't - * free it. - */ - while (b) - { - if (b->chunk) - nasm_free(b->chunk); - a = b; - b = b->next; + Blocks *a,*b = &blocks; + + /* + * keep in mind that the first block, pointed to by blocks + * is a static and not dynamically allocated, so we don't + * free it. + */ + while (b) + { + if (b->chunk) + nasm_free(b->chunk); + a = b; + b = b->next; if (a != &blocks) - nasm_free(a); - } -} + nasm_free(a); + } +} /* * this function creates a new Token and passes a pointer to it @@ -996,10 +996,10 @@ new_Token(Token * next, int type, const char *text, size_t txtlen) if (freeTokens == NULL) { - freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); - for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) - freeTokens[i].next = &freeTokens[i + 1]; - freeTokens[i].next = NULL; + freeTokens = (Token *)new_Block(TOKEN_BLOCKSIZE * sizeof(Token)); + for (i = 0; i < TOKEN_BLOCKSIZE - 1; i++) + freeTokens[i].next = &freeTokens[i + 1]; + freeTokens[i].next = NULL; } t = freeTokens; freeTokens = t->next; @@ -1008,15 +1008,15 @@ new_Token(Token * next, int type, const char *text, size_t txtlen) t->type = type; if (type == TOK_WHITESPACE || text == NULL) { - t->text = NULL; + t->text = NULL; } else { - if (txtlen == 0) - txtlen = strlen(text); - t->text = nasm_malloc(1 + txtlen); - strncpy(t->text, text, txtlen); - t->text[txtlen] = '\0'; + if (txtlen == 0) + txtlen = strlen(text); + t->text = nasm_malloc(1 + txtlen); + strncpy(t->text, text, txtlen); + t->text[txtlen] = '\0'; } return t; } @@ -1046,56 +1046,56 @@ detoken(Token * tlist, int expand_locals) len = 0; for (t = tlist; t; t = t->next) { - if (t->type == TOK_PREPROC_ID && t->text[1] == '!') - { - char *p2 = getenv(t->text + 2); - nasm_free(t->text); - if (p2) - t->text = nasm_strdup(p2); - else - t->text = NULL; - } - /* Expand local macros here and not during preprocessing */ - if (expand_locals && - t->type == TOK_PREPROC_ID && t->text && - t->text[0] == '%' && t->text[1] == '$') - { - Context *ctx = get_ctx(t->text, FALSE); - if (ctx) - { - char buffer[40]; - char *p2, *q = t->text + 2; - - q += strspn(q, "$"); - sprintf(buffer, "..@%lu.", ctx->number); - p2 = nasm_strcat(buffer, q); - nasm_free(t->text); - t->text = p2; - } - } - if (t->type == TOK_WHITESPACE) - { - len++; - } - else if (t->text) - { - len += strlen(t->text); - } + if (t->type == TOK_PREPROC_ID && t->text[1] == '!') + { + char *p2 = getenv(t->text + 2); + nasm_free(t->text); + if (p2) + t->text = nasm_strdup(p2); + else + t->text = NULL; + } + /* Expand local macros here and not during preprocessing */ + if (expand_locals && + t->type == TOK_PREPROC_ID && t->text && + t->text[0] == '%' && t->text[1] == '$') + { + Context *ctx = get_ctx(t->text, FALSE); + if (ctx) + { + char buffer[40]; + char *p2, *q = t->text + 2; + + q += strspn(q, "$"); + sprintf(buffer, "..@%lu.", ctx->number); + p2 = nasm_strcat(buffer, q); + nasm_free(t->text); + t->text = p2; + } + } + if (t->type == TOK_WHITESPACE) + { + len++; + } + else if (t->text) + { + len += strlen(t->text); + } } p = line = nasm_malloc(len + 1); for (t = tlist; t; t = t->next) { - if (t->type == TOK_WHITESPACE) - { - *p = ' '; - p++; - *p = '\0'; - } - else if (t->text) - { - strcpy(p, t->text); - p += strlen(p); - } + if (t->type == TOK_WHITESPACE) + { + *p = ' '; + p++; + *p = '\0'; + } + else if (t->text) + { + strcpy(p, t->text); + p += strlen(p); + } } *p = '\0'; return line; @@ -1115,95 +1115,95 @@ ppscan(void *private_data, struct tokenval *tokval) do { - tline = *tlineptr; - *tlineptr = tline ? tline->next : NULL; + tline = *tlineptr; + *tlineptr = tline ? tline->next : NULL; } while (tline && (tline->type == TOK_WHITESPACE || - tline->type == TOK_COMMENT)); + tline->type == TOK_COMMENT)); if (!tline) - return tokval->t_type = TOKEN_EOS; + return tokval->t_type = TOKEN_EOS; if (tline->text[0] == '$' && !tline->text[1]) - return tokval->t_type = TOKEN_HERE; + return tokval->t_type = TOKEN_HERE; if (tline->text[0] == '$' && tline->text[1] == '$' && !tline->text[2]) - return tokval->t_type = TOKEN_BASE; + return tokval->t_type = TOKEN_BASE; if (tline->type == TOK_ID) { - tokval->t_charptr = tline->text; - if (tline->text[0] == '$') - { - tokval->t_charptr++; - return tokval->t_type = TOKEN_ID; - } - - /* - * This is the only special case we actually need to worry - * about in this restricted context. - */ - if (!nasm_stricmp(tline->text, "seg")) - return tokval->t_type = TOKEN_SEG; - - return tokval->t_type = TOKEN_ID; + tokval->t_charptr = tline->text; + if (tline->text[0] == '$') + { + tokval->t_charptr++; + return tokval->t_type = TOKEN_ID; + } + + /* + * This is the only special case we actually need to worry + * about in this restricted context. + */ + if (!nasm_stricmp(tline->text, "seg")) + return tokval->t_type = TOKEN_SEG; + + return tokval->t_type = TOKEN_ID; } if (tline->type == TOK_NUMBER) { - int rn_error; + int rn_error; - tokval->t_integer = nasm_readnum(tline->text, &rn_error); - if (rn_error) - return tokval->t_type = TOKEN_ERRNUM; - tokval->t_charptr = NULL; - return tokval->t_type = TOKEN_NUM; + tokval->t_integer = nasm_readnum(tline->text, &rn_error); + if (rn_error) + return tokval->t_type = TOKEN_ERRNUM; + tokval->t_charptr = NULL; + return tokval->t_type = TOKEN_NUM; } if (tline->type == TOK_STRING) { - int rn_warn; - char q, *r; - size_t l; - - r = tline->text; - q = *r++; - l = strlen(r); - - if (l == 0 || r[l - 1] != q) - return tokval->t_type = TOKEN_ERRNUM; - tokval->t_integer = nasm_readstrnum(r, l - 1, &rn_warn); - if (rn_warn) - error(ERR_WARNING | ERR_PASS1, "character constant too long"); - tokval->t_charptr = NULL; - return tokval->t_type = TOKEN_NUM; + int rn_warn; + char q, *r; + size_t l; + + r = tline->text; + q = *r++; + l = strlen(r); + + if (l == 0 || r[l - 1] != q) + return tokval->t_type = TOKEN_ERRNUM; + tokval->t_integer = nasm_readstrnum(r, l - 1, &rn_warn); + if (rn_warn) + error(ERR_WARNING | ERR_PASS1, "character constant too long"); + tokval->t_charptr = NULL; + return tokval->t_type = TOKEN_NUM; } if (tline->type == TOK_OTHER) { - if (!strcmp(tline->text, "<<")) - return tokval->t_type = TOKEN_SHL; - if (!strcmp(tline->text, ">>")) - return tokval->t_type = TOKEN_SHR; - if (!strcmp(tline->text, "//")) - return tokval->t_type = TOKEN_SDIV; - if (!strcmp(tline->text, "%%")) - return tokval->t_type = TOKEN_SMOD; - if (!strcmp(tline->text, "==")) - return tokval->t_type = TOKEN_EQ; - if (!strcmp(tline->text, "<>")) - return tokval->t_type = TOKEN_NE; - if (!strcmp(tline->text, "!=")) - return tokval->t_type = TOKEN_NE; - if (!strcmp(tline->text, "<=")) - return tokval->t_type = TOKEN_LE; - if (!strcmp(tline->text, ">=")) - return tokval->t_type = TOKEN_GE; - if (!strcmp(tline->text, "&&")) - return tokval->t_type = TOKEN_DBL_AND; - if (!strcmp(tline->text, "^^")) - return tokval->t_type = TOKEN_DBL_XOR; - if (!strcmp(tline->text, "||")) - return tokval->t_type = TOKEN_DBL_OR; + if (!strcmp(tline->text, "<<")) + return tokval->t_type = TOKEN_SHL; + if (!strcmp(tline->text, ">>")) + return tokval->t_type = TOKEN_SHR; + if (!strcmp(tline->text, "//")) + return tokval->t_type = TOKEN_SDIV; + if (!strcmp(tline->text, "%%")) + return tokval->t_type = TOKEN_SMOD; + if (!strcmp(tline->text, "==")) + return tokval->t_type = TOKEN_EQ; + if (!strcmp(tline->text, "<>")) + return tokval->t_type = TOKEN_NE; + if (!strcmp(tline->text, "!=")) + return tokval->t_type = TOKEN_NE; + if (!strcmp(tline->text, "<=")) + return tokval->t_type = TOKEN_LE; + if (!strcmp(tline->text, ">=")) + return tokval->t_type = TOKEN_GE; + if (!strcmp(tline->text, "&&")) + return tokval->t_type = TOKEN_DBL_AND; + if (!strcmp(tline->text, "^^")) + return tokval->t_type = TOKEN_DBL_XOR; + if (!strcmp(tline->text, "||")) + return tokval->t_type = TOKEN_DBL_OR; } /* @@ -1242,39 +1242,39 @@ get_ctx(char *name, int all_contexts) size_t i; if (!name || name[0] != '%' || name[1] != '$') - return NULL; + return NULL; if (!cstk) { - error(ERR_NONFATAL, "`%s': context stack is empty", name); - return NULL; + error(ERR_NONFATAL, "`%s': context stack is empty", name); + return NULL; } for (i = strspn(name + 2, "$"), ctx = cstk; (i > 0) && ctx; i--) { - ctx = ctx->next; + ctx = ctx->next; /* i--; Lino - 02/25/02 */ } if (!ctx) { - error(ERR_NONFATAL, "`%s': context stack is only" - " %d level%s deep", name, i - 1, (i == 2 ? "" : "s")); - return NULL; + error(ERR_NONFATAL, "`%s': context stack is only" + " %d level%s deep", name, i - 1, (i == 2 ? "" : "s")); + return NULL; } if (!all_contexts) - return ctx; + return ctx; do { - /* Search for this smacro in found context */ - m = ctx->localmac; - while (m) - { - if (!mstrcmp(m->name, name, m->casesense)) - return ctx; - m = m->next; - } - ctx = ctx->next; + /* Search for this smacro in found context */ + m = ctx->localmac; + while (m) + { + if (!mstrcmp(m->name, name, m->casesense)) + return ctx; + m = m->next; + } + ctx = ctx->next; } while (ctx); return NULL; @@ -1300,54 +1300,54 @@ inc_fopen(char *file, char **newname) pb = file; p1 = pb; for (;;) { - char *env; - while (*p1 != '\0' && *p1 != '%') - p1++; - if (*p1 == '\0') - break; - p2 = p1+1; - while (*p2 != '\0' && *p2 != '%') - p2++; - if (*p2 == '\0') - break; - /* Okay, we have a %...%, with p1 pointing to the first %, and p2 - * pointing to the second %. - */ - *p2 = '\0'; - env = getenv(p1+1); - if (!env) { - /* warn, restore %, and continue looking */ - error(ERR_WARNING, "environment variable `%s' does not exist", - p1+1); - *p2 = '%'; - p1 = p2+1; - continue; - } - /* need to expand */ - if (!file2) { - file2 = nasm_malloc(strlen(file)+strlen(env)+1); - file2[0] = '\0'; - } else - file2 = nasm_realloc(file2, strlen(file2)+strlen(env)+1); - *p1 = '\0'; - strcat(file2, pb); - strcat(file2, env); - pb = p2+1; - p1 = pb; + char *env; + while (*p1 != '\0' && *p1 != '%') + p1++; + if (*p1 == '\0') + break; + p2 = p1+1; + while (*p2 != '\0' && *p2 != '%') + p2++; + if (*p2 == '\0') + break; + /* Okay, we have a %...%, with p1 pointing to the first %, and p2 + * pointing to the second %. + */ + *p2 = '\0'; + env = getenv(p1+1); + if (!env) { + /* warn, restore %, and continue looking */ + error(ERR_WARNING, "environment variable `%s' does not exist", + p1+1); + *p2 = '%'; + p1 = p2+1; + continue; + } + /* need to expand */ + if (!file2) { + file2 = nasm_malloc(strlen(file)+strlen(env)+1); + file2[0] = '\0'; + } else + file2 = nasm_realloc(file2, strlen(file2)+strlen(env)+1); + *p1 = '\0'; + strcat(file2, pb); + strcat(file2, env); + pb = p2+1; + p1 = pb; } /* add tail end; string is long enough that we don't need to realloc */ if (file2) - strcat(file2, pb); + strcat(file2, pb); fp = yasm_fopen_include(file2 ? file2 : file, nasm_src_get_fname(), "r", - &combine); + &combine); if (!fp) - error(ERR_FATAL, "unable to open include file `%s'", - file2 ? file2 : file); + error(ERR_FATAL, "unable to open include file `%s'", + file2 ? file2 : file); nasm_preproc_add_dep(combine); if (file2) - nasm_free(file2); + nasm_free(file2); *newname = combine; return fp; @@ -1376,38 +1376,38 @@ inc_fopen(char *file, char **newname) */ static int smacro_defined(Context * ctx, char *name, int nparam, SMacro ** defn, - int nocase) + int nocase) { SMacro *m; if (ctx) - m = ctx->localmac; + m = ctx->localmac; else if (name[0] == '%' && name[1] == '$') { - if (cstk) - ctx = get_ctx(name, FALSE); - if (!ctx) - return FALSE; /* got to return _something_ */ - m = ctx->localmac; + if (cstk) + ctx = get_ctx(name, FALSE); + if (!ctx) + return FALSE; /* got to return _something_ */ + m = ctx->localmac; } else - m = smacros[hash(name)]; + m = smacros[hash(name)]; while (m) { - if (!mstrcmp(m->name, name, m->casesense && nocase) && - (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) - { - if (defn) - { - if (nparam == m->nparam || nparam == -1) - *defn = m; - else - *defn = NULL; - } - return TRUE; - } - m = m->next; + if (!mstrcmp(m->name, name, m->casesense && nocase) && + (nparam <= 0 || m->nparam == 0 || nparam == m->nparam)) + { + if (defn) + { + if (nparam == m->nparam || nparam == -1) + *defn = m; + else + *defn = NULL; + } + return TRUE; + } + m = m->next; } return FALSE; @@ -1428,39 +1428,39 @@ count_mmac_params(Token * t, int *nparam, Token *** params) *params = NULL; while (t) { - if (*nparam+1 >= paramsize) - { - paramsize += PARAM_DELTA; - *params = nasm_realloc(*params, sizeof(**params) * paramsize); - } - skip_white_(t); - brace = FALSE; - if (tok_is_(t, "{")) - brace = TRUE; - (*params)[(*nparam)++] = t; - while (tok_isnt_(t, brace ? "}" : ",")) - t = t->next; - if (t) - { /* got a comma/brace */ - t = t->next; - if (brace) - { - /* - * Now we've found the closing brace, look further - * for the comma. - */ - skip_white_(t); - if (tok_isnt_(t, ",")) - { - error(ERR_NONFATAL, - "braces do not enclose all of macro parameter"); - while (tok_isnt_(t, ",")) - t = t->next; - } - if (t) - t = t->next; /* eat the comma */ - } - } + if (*nparam+1 >= paramsize) + { + paramsize += PARAM_DELTA; + *params = nasm_realloc(*params, sizeof(**params) * paramsize); + } + skip_white_(t); + brace = FALSE; + if (tok_is_(t, "{")) + brace = TRUE; + (*params)[(*nparam)++] = t; + while (tok_isnt_(t, brace ? "}" : ",")) + t = t->next; + if (t) + { /* got a comma/brace */ + t = t->next; + if (brace) + { + /* + * Now we've found the closing brace, look further + * for the comma. + */ + skip_white_(t); + if (tok_isnt_(t, ",")) + { + error(ERR_NONFATAL, + "braces do not enclose all of macro parameter"); + while (tok_isnt_(t, ",")) + t = t->next; + } + if (t) + t = t->next; /* eat the comma */ + } + } } } @@ -1483,302 +1483,302 @@ if_condition(Token * tline, int i) switch (i) { - case PP_IFCTX: - case PP_ELIFCTX: - case PP_IFNCTX: - case PP_ELIFNCTX: - j = FALSE; /* have we matched yet? */ - while (cstk && tline) - { - skip_white_(tline); - if (!tline || tline->type != TOK_ID) - { - error(ERR_NONFATAL, - "`%s' expects context identifiers", - directives[i]); - free_tlist(origline); - return -1; - } - if (!nasm_stricmp(tline->text, cstk->name)) - j = TRUE; - tline = tline->next; - } - if (i == PP_IFNCTX || i == PP_ELIFNCTX) - j = !j; - free_tlist(origline); - return j; - - case PP_IFDEF: - case PP_ELIFDEF: - case PP_IFNDEF: - case PP_ELIFNDEF: - j = FALSE; /* have we matched yet? */ - while (tline) - { - skip_white_(tline); - if (!tline || (tline->type != TOK_ID && - (tline->type != TOK_PREPROC_ID || - tline->text[1] != '$'))) - { - error(ERR_NONFATAL, - "`%s' expects macro identifiers", - directives[i]); - free_tlist(origline); - return -1; - } - if (smacro_defined(NULL, tline->text, 0, NULL, 1)) - j = TRUE; - tline = tline->next; - } - if (i == PP_IFNDEF || i == PP_ELIFNDEF) - j = !j; - free_tlist(origline); - return j; - - case PP_IFIDN: - case PP_ELIFIDN: - case PP_IFNIDN: - case PP_ELIFNIDN: - case PP_IFIDNI: - case PP_ELIFIDNI: - case PP_IFNIDNI: - case PP_ELIFNIDNI: - tline = expand_smacro(tline); - t = tt = tline; - while (tok_isnt_(tt, ",")) - tt = tt->next; - if (!tt) - { - error(ERR_NONFATAL, - "`%s' expects two comma-separated arguments", - directives[i]); - free_tlist(tline); - return -1; - } - tt = tt->next; - casesense = (i == PP_IFIDN || i == PP_ELIFIDN || - i == PP_IFNIDN || i == PP_ELIFNIDN); - j = TRUE; /* assume equality unless proved not */ - while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) - { - if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) - { - error(ERR_NONFATAL, "`%s': more than one comma on line", - directives[i]); - free_tlist(tline); - return -1; - } - if (t->type == TOK_WHITESPACE) - { - t = t->next; - continue; - } - if (tt->type == TOK_WHITESPACE) - { - tt = tt->next; - continue; - } - if (tt->type != t->type) - { - j = FALSE; /* found mismatching tokens */ - break; - } - /* Unify surrounding quotes for strings */ - if (t->type == TOK_STRING) - { - tt->text[0] = t->text[0]; - tt->text[strlen(tt->text) - 1] = t->text[0]; - } - if (mstrcmp(tt->text, t->text, casesense) != 0) - { - j = FALSE; /* found mismatching tokens */ - break; - } - - t = t->next; - tt = tt->next; - } - if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) - j = FALSE; /* trailing gunk on one end or other */ - if (i == PP_IFNIDN || i == PP_ELIFNIDN || - i == PP_IFNIDNI || i == PP_ELIFNIDNI) - j = !j; - free_tlist(tline); - return j; + case PP_IFCTX: + case PP_ELIFCTX: + case PP_IFNCTX: + case PP_ELIFNCTX: + j = FALSE; /* have we matched yet? */ + while (cstk && tline) + { + skip_white_(tline); + if (!tline || tline->type != TOK_ID) + { + error(ERR_NONFATAL, + "`%s' expects context identifiers", + directives[i]); + free_tlist(origline); + return -1; + } + if (!nasm_stricmp(tline->text, cstk->name)) + j = TRUE; + tline = tline->next; + } + if (i == PP_IFNCTX || i == PP_ELIFNCTX) + j = !j; + free_tlist(origline); + return j; + + case PP_IFDEF: + case PP_ELIFDEF: + case PP_IFNDEF: + case PP_ELIFNDEF: + j = FALSE; /* have we matched yet? */ + while (tline) + { + skip_white_(tline); + if (!tline || (tline->type != TOK_ID && + (tline->type != TOK_PREPROC_ID || + tline->text[1] != '$'))) + { + error(ERR_NONFATAL, + "`%s' expects macro identifiers", + directives[i]); + free_tlist(origline); + return -1; + } + if (smacro_defined(NULL, tline->text, 0, NULL, 1)) + j = TRUE; + tline = tline->next; + } + if (i == PP_IFNDEF || i == PP_ELIFNDEF) + j = !j; + free_tlist(origline); + return j; + + case PP_IFIDN: + case PP_ELIFIDN: + case PP_IFNIDN: + case PP_ELIFNIDN: + case PP_IFIDNI: + case PP_ELIFIDNI: + case PP_IFNIDNI: + case PP_ELIFNIDNI: + tline = expand_smacro(tline); + t = tt = tline; + while (tok_isnt_(tt, ",")) + tt = tt->next; + if (!tt) + { + error(ERR_NONFATAL, + "`%s' expects two comma-separated arguments", + directives[i]); + free_tlist(tline); + return -1; + } + tt = tt->next; + casesense = (i == PP_IFIDN || i == PP_ELIFIDN || + i == PP_IFNIDN || i == PP_ELIFNIDN); + j = TRUE; /* assume equality unless proved not */ + while ((t->type != TOK_OTHER || strcmp(t->text, ",")) && tt) + { + if (tt->type == TOK_OTHER && !strcmp(tt->text, ",")) + { + error(ERR_NONFATAL, "`%s': more than one comma on line", + directives[i]); + free_tlist(tline); + return -1; + } + if (t->type == TOK_WHITESPACE) + { + t = t->next; + continue; + } + if (tt->type == TOK_WHITESPACE) + { + tt = tt->next; + continue; + } + if (tt->type != t->type) + { + j = FALSE; /* found mismatching tokens */ + break; + } + /* Unify surrounding quotes for strings */ + if (t->type == TOK_STRING) + { + tt->text[0] = t->text[0]; + tt->text[strlen(tt->text) - 1] = t->text[0]; + } + if (mstrcmp(tt->text, t->text, casesense) != 0) + { + j = FALSE; /* found mismatching tokens */ + break; + } + + t = t->next; + tt = tt->next; + } + if ((t->type != TOK_OTHER || strcmp(t->text, ",")) || tt) + j = FALSE; /* trailing gunk on one end or other */ + if (i == PP_IFNIDN || i == PP_ELIFNIDN || + i == PP_IFNIDNI || i == PP_ELIFNIDNI) + j = !j; + free_tlist(tline); + return j; case PP_IFMACRO: case PP_ELIFMACRO: case PP_IFNMACRO: case PP_ELIFNMACRO: - { - int found = 0; - MMacro searching, *mmac; - - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tok_type_(tline, TOK_ID)) - { - error(ERR_NONFATAL, - "`%s' expects a macro name", - directives[i]); - return -1; - } - searching.name = nasm_strdup(tline->text); - searching.casesense = (i == PP_MACRO); - searching.plus = FALSE; - searching.nolist = FALSE; - searching.in_progress = FALSE; - searching.rep_nest = NULL; - searching.nparam_min = 0; - searching.nparam_max = INT_MAX; - tline = expand_smacro(tline->next); - skip_white_(tline); - if (!tline) - { - } else if (!tok_type_(tline, TOK_NUMBER)) - { - error(ERR_NONFATAL, - "`%s' expects a parameter count or nothing", - directives[i]); - } - else - { - intn = nasm_readnum(tline->text, &j); - searching.nparam_min = searching.nparam_max = - yasm_intnum_get_int(intn); - yasm_intnum_destroy(intn); - if (j) - error(ERR_NONFATAL, - "unable to parse parameter count `%s'", - tline->text); - } - if (tline && tok_is_(tline->next, "-")) - { - tline = tline->next->next; - if (tok_is_(tline, "*")) - searching.nparam_max = INT_MAX; - else if (!tok_type_(tline, TOK_NUMBER)) - error(ERR_NONFATAL, - "`%s' expects a parameter count after `-'", - directives[i]); - else - { - intn = nasm_readnum(tline->text, &j); - searching.nparam_max = yasm_intnum_get_int(intn); - yasm_intnum_destroy(intn); - if (j) - error(ERR_NONFATAL, - "unable to parse parameter count `%s'", - tline->text); - if (searching.nparam_min > searching.nparam_max) - error(ERR_NONFATAL, - "minimum parameter count exceeds maximum"); - } - } - if (tline && tok_is_(tline->next, "+")) - { - tline = tline->next; - searching.plus = TRUE; - } - mmac = mmacros[hash(searching.name)]; - while (mmac) - { - if (!strcmp(mmac->name, searching.name) && - (mmac->nparam_min <= searching.nparam_max - || searching.plus) - && (searching.nparam_min <= mmac->nparam_max - || mmac->plus)) - { - found = TRUE; - break; - } - mmac = mmac->next; - } - nasm_free(searching.name); - free_tlist(origline); - if (i == PP_IFNMACRO || i == PP_ELIFNMACRO) - found = !found; - return found; - } - - case PP_IFID: - case PP_ELIFID: - case PP_IFNID: - case PP_ELIFNID: - case PP_IFNUM: - case PP_ELIFNUM: - case PP_IFNNUM: - case PP_ELIFNNUM: - case PP_IFSTR: - case PP_ELIFSTR: - case PP_IFNSTR: - case PP_ELIFNSTR: - tline = expand_smacro(tline); - t = tline; - while (tok_type_(t, TOK_WHITESPACE)) - t = t->next; - j = FALSE; /* placate optimiser */ - if (t) - switch (i) - { - case PP_IFID: - case PP_ELIFID: - case PP_IFNID: - case PP_ELIFNID: - j = (t->type == TOK_ID); - break; - case PP_IFNUM: - case PP_ELIFNUM: - case PP_IFNNUM: - case PP_ELIFNNUM: - j = (t->type == TOK_NUMBER); - break; - case PP_IFSTR: - case PP_ELIFSTR: - case PP_IFNSTR: - case PP_ELIFNSTR: - j = (t->type == TOK_STRING); - break; - } - if (i == PP_IFNID || i == PP_ELIFNID || - i == PP_IFNNUM || i == PP_ELIFNNUM || - i == PP_IFNSTR || i == PP_ELIFNSTR) - j = !j; - free_tlist(tline); - return j; - - case PP_IF: - case PP_ELIF: - t = tline = expand_smacro(tline); - tptr = &t; - tokval.t_type = TOKEN_INVALID; - evalresult = evaluate(ppscan, tptr, &tokval, pass | CRITICAL, - error); - free_tlist(tline); - if (!evalresult) - return -1; - if (tokval.t_type) - error(ERR_WARNING, - "trailing garbage after expression ignored"); - intn = yasm_expr_get_intnum(&evalresult, 0); - if (!intn) - { - error(ERR_NONFATAL, - "non-constant value given to `%s'", directives[i]); - yasm_expr_destroy(evalresult); - return -1; - } - j = !yasm_intnum_is_zero(intn); - yasm_expr_destroy(evalresult); - return j; - - default: - error(ERR_FATAL, - "preprocessor directive `%s' not yet implemented", - directives[i]); - free_tlist(origline); - return -1; /* yeah, right */ + { + int found = 0; + MMacro searching, *mmac; + + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tok_type_(tline, TOK_ID)) + { + error(ERR_NONFATAL, + "`%s' expects a macro name", + directives[i]); + return -1; + } + searching.name = nasm_strdup(tline->text); + searching.casesense = (i == PP_MACRO); + searching.plus = FALSE; + searching.nolist = FALSE; + searching.in_progress = FALSE; + searching.rep_nest = NULL; + searching.nparam_min = 0; + searching.nparam_max = INT_MAX; + tline = expand_smacro(tline->next); + skip_white_(tline); + if (!tline) + { + } else if (!tok_type_(tline, TOK_NUMBER)) + { + error(ERR_NONFATAL, + "`%s' expects a parameter count or nothing", + directives[i]); + } + else + { + intn = nasm_readnum(tline->text, &j); + searching.nparam_min = searching.nparam_max = + yasm_intnum_get_int(intn); + yasm_intnum_destroy(intn); + if (j) + error(ERR_NONFATAL, + "unable to parse parameter count `%s'", + tline->text); + } + if (tline && tok_is_(tline->next, "-")) + { + tline = tline->next->next; + if (tok_is_(tline, "*")) + searching.nparam_max = INT_MAX; + else if (!tok_type_(tline, TOK_NUMBER)) + error(ERR_NONFATAL, + "`%s' expects a parameter count after `-'", + directives[i]); + else + { + intn = nasm_readnum(tline->text, &j); + searching.nparam_max = yasm_intnum_get_int(intn); + yasm_intnum_destroy(intn); + if (j) + error(ERR_NONFATAL, + "unable to parse parameter count `%s'", + tline->text); + if (searching.nparam_min > searching.nparam_max) + error(ERR_NONFATAL, + "minimum parameter count exceeds maximum"); + } + } + if (tline && tok_is_(tline->next, "+")) + { + tline = tline->next; + searching.plus = TRUE; + } + mmac = mmacros[hash(searching.name)]; + while (mmac) + { + if (!strcmp(mmac->name, searching.name) && + (mmac->nparam_min <= searching.nparam_max + || searching.plus) + && (searching.nparam_min <= mmac->nparam_max + || mmac->plus)) + { + found = TRUE; + break; + } + mmac = mmac->next; + } + nasm_free(searching.name); + free_tlist(origline); + if (i == PP_IFNMACRO || i == PP_ELIFNMACRO) + found = !found; + return found; + } + + case PP_IFID: + case PP_ELIFID: + case PP_IFNID: + case PP_ELIFNID: + case PP_IFNUM: + case PP_ELIFNUM: + case PP_IFNNUM: + case PP_ELIFNNUM: + case PP_IFSTR: + case PP_ELIFSTR: + case PP_IFNSTR: + case PP_ELIFNSTR: + tline = expand_smacro(tline); + t = tline; + while (tok_type_(t, TOK_WHITESPACE)) + t = t->next; + j = FALSE; /* placate optimiser */ + if (t) + switch (i) + { + case PP_IFID: + case PP_ELIFID: + case PP_IFNID: + case PP_ELIFNID: + j = (t->type == TOK_ID); + break; + case PP_IFNUM: + case PP_ELIFNUM: + case PP_IFNNUM: + case PP_ELIFNNUM: + j = (t->type == TOK_NUMBER); + break; + case PP_IFSTR: + case PP_ELIFSTR: + case PP_IFNSTR: + case PP_ELIFNSTR: + j = (t->type == TOK_STRING); + break; + } + if (i == PP_IFNID || i == PP_ELIFNID || + i == PP_IFNNUM || i == PP_ELIFNNUM || + i == PP_IFNSTR || i == PP_ELIFNSTR) + j = !j; + free_tlist(tline); + return j; + + case PP_IF: + case PP_ELIF: + t = tline = expand_smacro(tline); + tptr = &t; + tokval.t_type = TOKEN_INVALID; + evalresult = evaluate(ppscan, tptr, &tokval, pass | CRITICAL, + error); + free_tlist(tline); + if (!evalresult) + return -1; + if (tokval.t_type) + error(ERR_WARNING, + "trailing garbage after expression ignored"); + intn = yasm_expr_get_intnum(&evalresult, 0); + if (!intn) + { + error(ERR_NONFATAL, + "non-constant value given to `%s'", directives[i]); + yasm_expr_destroy(evalresult); + return -1; + } + j = !yasm_intnum_is_zero(intn); + yasm_expr_destroy(evalresult); + return j; + + default: + error(ERR_FATAL, + "preprocessor directive `%s' not yet implemented", + directives[i]); + free_tlist(origline); + return -1; /* yeah, right */ } } @@ -1822,38 +1822,38 @@ do_directive(Token * tline) Line *l; struct tokenval tokval; yasm_expr *evalresult; - MMacro *tmp_defining; /* Used when manipulating rep_nest */ + MMacro *tmp_defining; /* Used when manipulating rep_nest */ yasm_intnum *intn; origline = tline; skip_white_(tline); if (!tok_type_(tline, TOK_PREPROC_ID) || - (tline->text[1] == '%' || tline->text[1] == '$' - || tline->text[1] == '!')) - return NO_DIRECTIVE_FOUND; + (tline->text[1] == '%' || tline->text[1] == '$' + || tline->text[1] == '!')) + return NO_DIRECTIVE_FOUND; i = -1; j = elements(directives); while (j - i > 1) { - k = (j + i) / 2; - m = nasm_stricmp(tline->text, directives[k]); - if (m == 0) { - if (tasm_compatible_mode) { - i = k; - j = -2; - } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) { - i = k; - j = -2; - } - break; - } - else if (m < 0) { - j = k; - } - else - i = k; + k = (j + i) / 2; + m = nasm_stricmp(tline->text, directives[k]); + if (m == 0) { + if (tasm_compatible_mode) { + i = k; + j = -2; + } else if (k != PP_ARG && k != PP_LOCAL && k != PP_STACKSIZE) { + i = k; + j = -2; + } + break; + } + else if (m < 0) { + j = k; + } + else + i = k; } /* @@ -1863,10 +1863,10 @@ do_directive(Token * tline) * directives. */ if (((istk->conds && !emitting(istk->conds->state)) || - (istk->mstk && !istk->mstk->in_progress)) && - !is_condition(i)) + (istk->mstk && !istk->mstk->in_progress)) && + !is_condition(i)) { - return NO_DIRECTIVE_FOUND; + return NO_DIRECTIVE_FOUND; } /* @@ -1877,1329 +1877,1329 @@ do_directive(Token * tline) * causes an error, so should be let through. */ if (defining && i != PP_MACRO && i != PP_IMACRO && - i != PP_ENDMACRO && i != PP_ENDM && - (defining->name || (i != PP_ENDREP && i != PP_REP))) + i != PP_ENDMACRO && i != PP_ENDM && + (defining->name || (i != PP_ENDREP && i != PP_REP))) { - return NO_DIRECTIVE_FOUND; + return NO_DIRECTIVE_FOUND; } if (defining) { - if (i == PP_MACRO || i == PP_IMACRO) { - nested_mac_count++; - return NO_DIRECTIVE_FOUND; - } else if (nested_mac_count > 0) { - if (i == PP_ENDMACRO) { - nested_mac_count--; - return NO_DIRECTIVE_FOUND; - } - } - if (!defining->name) { - if (i == PP_REP) { - nested_rep_count++; - return NO_DIRECTIVE_FOUND; - } else if (nested_rep_count > 0) { - if (i == PP_ENDREP) { - nested_rep_count--; - return NO_DIRECTIVE_FOUND; - } - } - } + if (i == PP_MACRO || i == PP_IMACRO) { + nested_mac_count++; + return NO_DIRECTIVE_FOUND; + } else if (nested_mac_count > 0) { + if (i == PP_ENDMACRO) { + nested_mac_count--; + return NO_DIRECTIVE_FOUND; + } + } + if (!defining->name) { + if (i == PP_REP) { + nested_rep_count++; + return NO_DIRECTIVE_FOUND; + } else if (nested_rep_count > 0) { + if (i == PP_ENDREP) { + nested_rep_count--; + return NO_DIRECTIVE_FOUND; + } + } + } } if (j != -2) { - error(ERR_NONFATAL, "unknown preprocessor directive `%s'", - tline->text); - return NO_DIRECTIVE_FOUND; /* didn't get it */ + error(ERR_NONFATAL, "unknown preprocessor directive `%s'", + tline->text); + return NO_DIRECTIVE_FOUND; /* didn't get it */ } switch (i) { - case PP_STACKSIZE: - /* Directive to tell NASM what the default stack size is. The - * default is for a 16-bit stack, and this can be overriden with - * %stacksize large. - * the following form: - * - * ARG arg1:WORD, arg2:DWORD, arg4:QWORD - */ - tline = tline->next; - if (tline && tline->type == TOK_WHITESPACE) - tline = tline->next; - if (!tline || tline->type != TOK_ID) - { - error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - if (nasm_stricmp(tline->text, "flat") == 0) - { - /* All subsequent ARG directives are for a 32-bit stack */ - StackSize = 4; - StackPointer = "ebp"; - ArgOffset = 8; - LocalOffset = 4; - } - else if (nasm_stricmp(tline->text, "large") == 0) - { - /* All subsequent ARG directives are for a 16-bit stack, - * far function call. - */ - StackSize = 2; - StackPointer = "bp"; - ArgOffset = 4; - LocalOffset = 2; - } - else if (nasm_stricmp(tline->text, "small") == 0) - { - /* All subsequent ARG directives are for a 16-bit stack, - * far function call. We don't support near functions. - */ - StackSize = 2; - StackPointer = "bp"; - ArgOffset = 6; - LocalOffset = 2; - } - else - { - error(ERR_NONFATAL, "`%%stacksize' invalid size type"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_ARG: - /* TASM like ARG directive to define arguments to functions, in - * the following form: - * - * ARG arg1:WORD, arg2:DWORD, arg4:QWORD - */ - offset = ArgOffset; - do - { - char *arg, directive[256]; - int size = StackSize; - - /* Find the argument name */ - tline = tline->next; - if (tline && tline->type == TOK_WHITESPACE) - tline = tline->next; - if (!tline || tline->type != TOK_ID) - { - error(ERR_NONFATAL, "`%%arg' missing argument parameter"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - arg = tline->text; - - /* Find the argument size type */ - tline = tline->next; - if (!tline || tline->type != TOK_OTHER - || tline->text[0] != ':') - { - error(ERR_NONFATAL, - "Syntax error processing `%%arg' directive"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - tline = tline->next; - if (!tline || tline->type != TOK_ID) - { - error(ERR_NONFATAL, - "`%%arg' missing size type parameter"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - - /* Allow macro expansion of type parameter */ - tt = tokenise(tline->text); - tt = expand_smacro(tt); - if (nasm_stricmp(tt->text, "byte") == 0) - { - size = MAX(StackSize, 1); - } - else if (nasm_stricmp(tt->text, "word") == 0) - { - size = MAX(StackSize, 2); - } - else if (nasm_stricmp(tt->text, "dword") == 0) - { - size = MAX(StackSize, 4); - } - else if (nasm_stricmp(tt->text, "qword") == 0) - { - size = MAX(StackSize, 8); - } - else if (nasm_stricmp(tt->text, "tword") == 0) - { - size = MAX(StackSize, 10); - } - else - { - error(ERR_NONFATAL, - "Invalid size type for `%%arg' missing directive"); - free_tlist(tt); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - free_tlist(tt); - - /* Now define the macro for the argument */ - sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer, - offset); - do_directive(tokenise(directive)); - offset += size; - - /* Move to the next argument in the list */ - tline = tline->next; - if (tline && tline->type == TOK_WHITESPACE) - tline = tline->next; - } - while (tline && tline->type == TOK_OTHER - && tline->text[0] == ','); - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_LOCAL: - /* TASM like LOCAL directive to define local variables for a - * function, in the following form: - * - * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize - * - * The '= LocalSize' at the end is ignored by NASM, but is - * required by TASM to define the local parameter size (and used - * by the TASM macro package). - */ - offset = LocalOffset; - do - { - char *local, directive[256]; - int size = StackSize; - - /* Find the argument name */ - tline = tline->next; - if (tline && tline->type == TOK_WHITESPACE) - tline = tline->next; - if (!tline || tline->type != TOK_ID) - { - error(ERR_NONFATAL, - "`%%local' missing argument parameter"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - local = tline->text; - - /* Find the argument size type */ - tline = tline->next; - if (!tline || tline->type != TOK_OTHER - || tline->text[0] != ':') - { - error(ERR_NONFATAL, - "Syntax error processing `%%local' directive"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - tline = tline->next; - if (!tline || tline->type != TOK_ID) - { - error(ERR_NONFATAL, - "`%%local' missing size type parameter"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - - /* Allow macro expansion of type parameter */ - tt = tokenise(tline->text); - tt = expand_smacro(tt); - if (nasm_stricmp(tt->text, "byte") == 0) - { - size = MAX(StackSize, 1); - } - else if (nasm_stricmp(tt->text, "word") == 0) - { - size = MAX(StackSize, 2); - } - else if (nasm_stricmp(tt->text, "dword") == 0) - { - size = MAX(StackSize, 4); - } - else if (nasm_stricmp(tt->text, "qword") == 0) - { - size = MAX(StackSize, 8); - } - else if (nasm_stricmp(tt->text, "tword") == 0) - { - size = MAX(StackSize, 10); - } - else - { - error(ERR_NONFATAL, - "Invalid size type for `%%local' missing directive"); - free_tlist(tt); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - free_tlist(tt); - - /* Now define the macro for the argument */ - sprintf(directive, "%%define %s (%s-%d)", local, StackPointer, - offset); - do_directive(tokenise(directive)); - offset += size; - - /* Now define the assign to setup the enter_c macro correctly */ - sprintf(directive, "%%assign %%$localsize %%$localsize+%d", - size); - do_directive(tokenise(directive)); - - /* Move to the next argument in the list */ - tline = tline->next; - if (tline && tline->type == TOK_WHITESPACE) - tline = tline->next; - } - while (tline && tline->type == TOK_OTHER - && tline->text[0] == ','); - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_CLEAR: - if (tline->next) - error(ERR_WARNING, - "trailing garbage after `%%clear' ignored"); - for (j = 0; j < NHASH; j++) - { - while (mmacros[j]) - { - MMacro *m2 = mmacros[j]; - mmacros[j] = m2->next; - free_mmacro(m2); - } - while (smacros[j]) - { - SMacro *s = smacros[j]; - smacros[j] = smacros[j]->next; - nasm_free(s->name); - free_tlist(s->expansion); - nasm_free(s); - } - } - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_INCLUDE: - tline = tline->next; - skip_white_(tline); - if (!tline || (tline->type != TOK_STRING && - tline->type != TOK_INTERNAL_STRING)) - { - error(ERR_NONFATAL, "`%%include' expects a file name"); - free_tlist(origline); - return DIRECTIVE_FOUND; /* but we did _something_ */ - } - if (tline->next) - error(ERR_WARNING, - "trailing garbage after `%%include' ignored"); - if (tline->type != TOK_INTERNAL_STRING) - { - p = tline->text + 1; /* point past the quote to the name */ - p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ - } - else - p = tline->text; /* internal_string is easier */ - expand_macros_in_string(&p); - inc = nasm_malloc(sizeof(Include)); - inc->next = istk; - inc->conds = NULL; - inc->fp = inc_fopen(p, &newname); - inc->fname = nasm_src_set_fname(newname); - inc->lineno = nasm_src_set_linnum(0); - inc->lineinc = 1; - inc->expansion = NULL; - inc->mstk = NULL; - istk = inc; - list->uplevel(LIST_INCLUDE); - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_PUSH: - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tok_type_(tline, TOK_ID)) - { - error(ERR_NONFATAL, "`%%push' expects a context identifier"); - free_tlist(origline); - return DIRECTIVE_FOUND; /* but we did _something_ */ - } - if (tline->next) - error(ERR_WARNING, "trailing garbage after `%%push' ignored"); - ctx = nasm_malloc(sizeof(Context)); - ctx->next = cstk; - ctx->localmac = NULL; - ctx->name = nasm_strdup(tline->text); - ctx->number = unique++; - cstk = ctx; - free_tlist(origline); - break; - - case PP_REPL: - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tok_type_(tline, TOK_ID)) - { - error(ERR_NONFATAL, "`%%repl' expects a context identifier"); - free_tlist(origline); - return DIRECTIVE_FOUND; /* but we did _something_ */ - } - if (tline->next) - error(ERR_WARNING, "trailing garbage after `%%repl' ignored"); - if (!cstk) - error(ERR_NONFATAL, "`%%repl': context stack is empty"); - else - { - nasm_free(cstk->name); - cstk->name = nasm_strdup(tline->text); - } - free_tlist(origline); - break; - - case PP_POP: - if (tline->next) - error(ERR_WARNING, "trailing garbage after `%%pop' ignored"); - if (!cstk) - error(ERR_NONFATAL, - "`%%pop': context stack is already empty"); - else - ctx_pop(); - free_tlist(origline); - break; - - case PP_ERROR: - tline->next = expand_smacro(tline->next); - tline = tline->next; - skip_white_(tline); - if (tok_type_(tline, TOK_STRING)) - { - p = tline->text + 1; /* point past the quote to the name */ - p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ - expand_macros_in_string(&p); - error(ERR_NONFATAL, "%s", p); - nasm_free(p); - } - else - { - p = detoken(tline, FALSE); - error(ERR_WARNING, "%s", p); - nasm_free(p); - } - free_tlist(origline); - break; - - case PP_IF: - case PP_IFCTX: - case PP_IFDEF: - case PP_IFID: - case PP_IFIDN: - case PP_IFIDNI: + case PP_STACKSIZE: + /* Directive to tell NASM what the default stack size is. The + * default is for a 16-bit stack, and this can be overriden with + * %stacksize large. + * the following form: + * + * ARG arg1:WORD, arg2:DWORD, arg4:QWORD + */ + tline = tline->next; + if (tline && tline->type == TOK_WHITESPACE) + tline = tline->next; + if (!tline || tline->type != TOK_ID) + { + error(ERR_NONFATAL, "`%%stacksize' missing size parameter"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + if (nasm_stricmp(tline->text, "flat") == 0) + { + /* All subsequent ARG directives are for a 32-bit stack */ + StackSize = 4; + StackPointer = "ebp"; + ArgOffset = 8; + LocalOffset = 4; + } + else if (nasm_stricmp(tline->text, "large") == 0) + { + /* All subsequent ARG directives are for a 16-bit stack, + * far function call. + */ + StackSize = 2; + StackPointer = "bp"; + ArgOffset = 4; + LocalOffset = 2; + } + else if (nasm_stricmp(tline->text, "small") == 0) + { + /* All subsequent ARG directives are for a 16-bit stack, + * far function call. We don't support near functions. + */ + StackSize = 2; + StackPointer = "bp"; + ArgOffset = 6; + LocalOffset = 2; + } + else + { + error(ERR_NONFATAL, "`%%stacksize' invalid size type"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_ARG: + /* TASM like ARG directive to define arguments to functions, in + * the following form: + * + * ARG arg1:WORD, arg2:DWORD, arg4:QWORD + */ + offset = ArgOffset; + do + { + char *arg, directive[256]; + int size = StackSize; + + /* Find the argument name */ + tline = tline->next; + if (tline && tline->type == TOK_WHITESPACE) + tline = tline->next; + if (!tline || tline->type != TOK_ID) + { + error(ERR_NONFATAL, "`%%arg' missing argument parameter"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + arg = tline->text; + + /* Find the argument size type */ + tline = tline->next; + if (!tline || tline->type != TOK_OTHER + || tline->text[0] != ':') + { + error(ERR_NONFATAL, + "Syntax error processing `%%arg' directive"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + tline = tline->next; + if (!tline || tline->type != TOK_ID) + { + error(ERR_NONFATAL, + "`%%arg' missing size type parameter"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + + /* Allow macro expansion of type parameter */ + tt = tokenise(tline->text); + tt = expand_smacro(tt); + if (nasm_stricmp(tt->text, "byte") == 0) + { + size = MAX(StackSize, 1); + } + else if (nasm_stricmp(tt->text, "word") == 0) + { + size = MAX(StackSize, 2); + } + else if (nasm_stricmp(tt->text, "dword") == 0) + { + size = MAX(StackSize, 4); + } + else if (nasm_stricmp(tt->text, "qword") == 0) + { + size = MAX(StackSize, 8); + } + else if (nasm_stricmp(tt->text, "tword") == 0) + { + size = MAX(StackSize, 10); + } + else + { + error(ERR_NONFATAL, + "Invalid size type for `%%arg' missing directive"); + free_tlist(tt); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + free_tlist(tt); + + /* Now define the macro for the argument */ + sprintf(directive, "%%define %s (%s+%d)", arg, StackPointer, + offset); + do_directive(tokenise(directive)); + offset += size; + + /* Move to the next argument in the list */ + tline = tline->next; + if (tline && tline->type == TOK_WHITESPACE) + tline = tline->next; + } + while (tline && tline->type == TOK_OTHER + && tline->text[0] == ','); + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_LOCAL: + /* TASM like LOCAL directive to define local variables for a + * function, in the following form: + * + * LOCAL local1:WORD, local2:DWORD, local4:QWORD = LocalSize + * + * The '= LocalSize' at the end is ignored by NASM, but is + * required by TASM to define the local parameter size (and used + * by the TASM macro package). + */ + offset = LocalOffset; + do + { + char *local, directive[256]; + int size = StackSize; + + /* Find the argument name */ + tline = tline->next; + if (tline && tline->type == TOK_WHITESPACE) + tline = tline->next; + if (!tline || tline->type != TOK_ID) + { + error(ERR_NONFATAL, + "`%%local' missing argument parameter"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + local = tline->text; + + /* Find the argument size type */ + tline = tline->next; + if (!tline || tline->type != TOK_OTHER + || tline->text[0] != ':') + { + error(ERR_NONFATAL, + "Syntax error processing `%%local' directive"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + tline = tline->next; + if (!tline || tline->type != TOK_ID) + { + error(ERR_NONFATAL, + "`%%local' missing size type parameter"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + + /* Allow macro expansion of type parameter */ + tt = tokenise(tline->text); + tt = expand_smacro(tt); + if (nasm_stricmp(tt->text, "byte") == 0) + { + size = MAX(StackSize, 1); + } + else if (nasm_stricmp(tt->text, "word") == 0) + { + size = MAX(StackSize, 2); + } + else if (nasm_stricmp(tt->text, "dword") == 0) + { + size = MAX(StackSize, 4); + } + else if (nasm_stricmp(tt->text, "qword") == 0) + { + size = MAX(StackSize, 8); + } + else if (nasm_stricmp(tt->text, "tword") == 0) + { + size = MAX(StackSize, 10); + } + else + { + error(ERR_NONFATAL, + "Invalid size type for `%%local' missing directive"); + free_tlist(tt); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + free_tlist(tt); + + /* Now define the macro for the argument */ + sprintf(directive, "%%define %s (%s-%d)", local, StackPointer, + offset); + do_directive(tokenise(directive)); + offset += size; + + /* Now define the assign to setup the enter_c macro correctly */ + sprintf(directive, "%%assign %%$localsize %%$localsize+%d", + size); + do_directive(tokenise(directive)); + + /* Move to the next argument in the list */ + tline = tline->next; + if (tline && tline->type == TOK_WHITESPACE) + tline = tline->next; + } + while (tline && tline->type == TOK_OTHER + && tline->text[0] == ','); + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_CLEAR: + if (tline->next) + error(ERR_WARNING, + "trailing garbage after `%%clear' ignored"); + for (j = 0; j < NHASH; j++) + { + while (mmacros[j]) + { + MMacro *m2 = mmacros[j]; + mmacros[j] = m2->next; + free_mmacro(m2); + } + while (smacros[j]) + { + SMacro *s = smacros[j]; + smacros[j] = smacros[j]->next; + nasm_free(s->name); + free_tlist(s->expansion); + nasm_free(s); + } + } + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_INCLUDE: + tline = tline->next; + skip_white_(tline); + if (!tline || (tline->type != TOK_STRING && + tline->type != TOK_INTERNAL_STRING)) + { + error(ERR_NONFATAL, "`%%include' expects a file name"); + free_tlist(origline); + return DIRECTIVE_FOUND; /* but we did _something_ */ + } + if (tline->next) + error(ERR_WARNING, + "trailing garbage after `%%include' ignored"); + if (tline->type != TOK_INTERNAL_STRING) + { + p = tline->text + 1; /* point past the quote to the name */ + p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ + } + else + p = tline->text; /* internal_string is easier */ + expand_macros_in_string(&p); + inc = nasm_malloc(sizeof(Include)); + inc->next = istk; + inc->conds = NULL; + inc->fp = inc_fopen(p, &newname); + inc->fname = nasm_src_set_fname(newname); + inc->lineno = nasm_src_set_linnum(0); + inc->lineinc = 1; + inc->expansion = NULL; + inc->mstk = NULL; + istk = inc; + list->uplevel(LIST_INCLUDE); + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_PUSH: + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tok_type_(tline, TOK_ID)) + { + error(ERR_NONFATAL, "`%%push' expects a context identifier"); + free_tlist(origline); + return DIRECTIVE_FOUND; /* but we did _something_ */ + } + if (tline->next) + error(ERR_WARNING, "trailing garbage after `%%push' ignored"); + ctx = nasm_malloc(sizeof(Context)); + ctx->next = cstk; + ctx->localmac = NULL; + ctx->name = nasm_strdup(tline->text); + ctx->number = unique++; + cstk = ctx; + free_tlist(origline); + break; + + case PP_REPL: + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tok_type_(tline, TOK_ID)) + { + error(ERR_NONFATAL, "`%%repl' expects a context identifier"); + free_tlist(origline); + return DIRECTIVE_FOUND; /* but we did _something_ */ + } + if (tline->next) + error(ERR_WARNING, "trailing garbage after `%%repl' ignored"); + if (!cstk) + error(ERR_NONFATAL, "`%%repl': context stack is empty"); + else + { + nasm_free(cstk->name); + cstk->name = nasm_strdup(tline->text); + } + free_tlist(origline); + break; + + case PP_POP: + if (tline->next) + error(ERR_WARNING, "trailing garbage after `%%pop' ignored"); + if (!cstk) + error(ERR_NONFATAL, + "`%%pop': context stack is already empty"); + else + ctx_pop(); + free_tlist(origline); + break; + + case PP_ERROR: + tline->next = expand_smacro(tline->next); + tline = tline->next; + skip_white_(tline); + if (tok_type_(tline, TOK_STRING)) + { + p = tline->text + 1; /* point past the quote to the name */ + p[strlen(p) - 1] = '\0'; /* remove the trailing quote */ + expand_macros_in_string(&p); + error(ERR_NONFATAL, "%s", p); + nasm_free(p); + } + else + { + p = detoken(tline, FALSE); + error(ERR_WARNING, "%s", p); + nasm_free(p); + } + free_tlist(origline); + break; + + case PP_IF: + case PP_IFCTX: + case PP_IFDEF: + case PP_IFID: + case PP_IFIDN: + case PP_IFIDNI: case PP_IFMACRO: - case PP_IFNCTX: - case PP_IFNDEF: - case PP_IFNID: - case PP_IFNIDN: - case PP_IFNIDNI: + case PP_IFNCTX: + case PP_IFNDEF: + case PP_IFNID: + case PP_IFNIDN: + case PP_IFNIDNI: case PP_IFNMACRO: - case PP_IFNNUM: - case PP_IFNSTR: - case PP_IFNUM: - case PP_IFSTR: - if (istk->conds && !emitting(istk->conds->state)) - j = COND_NEVER; - else - { - j = if_condition(tline->next, i); - tline->next = NULL; /* it got freed */ - j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; - } - free_tlist(origline); - cond = nasm_malloc(sizeof(Cond)); - cond->next = istk->conds; - cond->state = j; - istk->conds = cond; - return DIRECTIVE_FOUND; - - case PP_ELIF: - case PP_ELIFCTX: - case PP_ELIFDEF: - case PP_ELIFID: - case PP_ELIFIDN: - case PP_ELIFIDNI: + case PP_IFNNUM: + case PP_IFNSTR: + case PP_IFNUM: + case PP_IFSTR: + if (istk->conds && !emitting(istk->conds->state)) + j = COND_NEVER; + else + { + j = if_condition(tline->next, i); + tline->next = NULL; /* it got freed */ + j = j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; + } + free_tlist(origline); + cond = nasm_malloc(sizeof(Cond)); + cond->next = istk->conds; + cond->state = j; + istk->conds = cond; + return DIRECTIVE_FOUND; + + case PP_ELIF: + case PP_ELIFCTX: + case PP_ELIFDEF: + case PP_ELIFID: + case PP_ELIFIDN: + case PP_ELIFIDNI: case PP_ELIFMACRO: - case PP_ELIFNCTX: - case PP_ELIFNDEF: - case PP_ELIFNID: - case PP_ELIFNIDN: - case PP_ELIFNIDNI: + case PP_ELIFNCTX: + case PP_ELIFNDEF: + case PP_ELIFNID: + case PP_ELIFNIDN: + case PP_ELIFNIDNI: case PP_ELIFNMACRO: - case PP_ELIFNNUM: - case PP_ELIFNSTR: - case PP_ELIFNUM: - case PP_ELIFSTR: - if (!istk->conds) - error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]); - if (emitting(istk->conds->state) - || istk->conds->state == COND_NEVER) - istk->conds->state = COND_NEVER; - else - { - /* - * IMPORTANT: In the case of %if, we will already have - * called expand_mmac_params(); however, if we're - * processing an %elif we must have been in a - * non-emitting mode, which would have inhibited - * the normal invocation of expand_mmac_params(). Therefore, - * we have to do it explicitly here. - */ - j = if_condition(expand_mmac_params(tline->next), i); - tline->next = NULL; /* it got freed */ - istk->conds->state = - j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; - } - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_ELSE: - if (tline->next) - error(ERR_WARNING, "trailing garbage after `%%else' ignored"); - if (!istk->conds) - error(ERR_FATAL, "`%%else': no matching `%%if'"); - if (emitting(istk->conds->state) - || istk->conds->state == COND_NEVER) - istk->conds->state = COND_ELSE_FALSE; - else - istk->conds->state = COND_ELSE_TRUE; - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_ENDIF: - if (tline->next) - error(ERR_WARNING, - "trailing garbage after `%%endif' ignored"); - if (!istk->conds) - error(ERR_FATAL, "`%%endif': no matching `%%if'"); - cond = istk->conds; - istk->conds = cond->next; - nasm_free(cond); - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_MACRO: - case PP_IMACRO: - if (defining) - error(ERR_FATAL, - "`%%%smacro': already defining a macro", - (i == PP_IMACRO ? "i" : "")); - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tok_type_(tline, TOK_ID)) - { - error(ERR_NONFATAL, - "`%%%smacro' expects a macro name", - (i == PP_IMACRO ? "i" : "")); - return DIRECTIVE_FOUND; - } - defining = nasm_malloc(sizeof(MMacro)); - defining->name = nasm_strdup(tline->text); - defining->casesense = (i == PP_MACRO); - defining->plus = FALSE; - defining->nolist = FALSE; - defining->in_progress = FALSE; - defining->rep_nest = NULL; - tline = expand_smacro(tline->next); - skip_white_(tline); - if (!tok_type_(tline, TOK_NUMBER)) - { - error(ERR_NONFATAL, - "`%%%smacro' expects a parameter count", - (i == PP_IMACRO ? "i" : "")); - defining->nparam_min = defining->nparam_max = 0; - } - else - { - intn = nasm_readnum(tline->text, &j); - defining->nparam_min = defining->nparam_max = - yasm_intnum_get_int(intn); - yasm_intnum_destroy(intn); - if (j) - error(ERR_NONFATAL, - "unable to parse parameter count `%s'", - tline->text); - } - if (tline && tok_is_(tline->next, "-")) - { - tline = tline->next->next; - if (tok_is_(tline, "*")) - defining->nparam_max = INT_MAX; - else if (!tok_type_(tline, TOK_NUMBER)) - error(ERR_NONFATAL, - "`%%%smacro' expects a parameter count after `-'", - (i == PP_IMACRO ? "i" : "")); - else - { - intn = nasm_readnum(tline->text, &j); - defining->nparam_max = yasm_intnum_get_int(intn); - yasm_intnum_destroy(intn); - if (j) - error(ERR_NONFATAL, - "unable to parse parameter count `%s'", - tline->text); - if (defining->nparam_min > defining->nparam_max) - error(ERR_NONFATAL, - "minimum parameter count exceeds maximum"); - } - } - if (tline && tok_is_(tline->next, "+")) - { - tline = tline->next; - defining->plus = TRUE; - } - if (tline && tok_type_(tline->next, TOK_ID) && - !nasm_stricmp(tline->next->text, ".nolist")) - { - tline = tline->next; - defining->nolist = TRUE; - } - mmac = mmacros[hash(defining->name)]; - while (mmac) - { - if (!strcmp(mmac->name, defining->name) && - (mmac->nparam_min <= defining->nparam_max - || defining->plus) - && (defining->nparam_min <= mmac->nparam_max - || mmac->plus)) - { - error(ERR_WARNING, - "redefining multi-line macro `%s'", - defining->name); - break; - } - mmac = mmac->next; - } - /* - * Handle default parameters. - */ - if (tline && tline->next) - { - defining->dlist = tline->next; - tline->next = NULL; - count_mmac_params(defining->dlist, &defining->ndefs, - &defining->defaults); - } - else - { - defining->dlist = NULL; - defining->defaults = NULL; - } - defining->expansion = NULL; - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_ENDM: - case PP_ENDMACRO: - if (!defining) - { - error(ERR_NONFATAL, "`%s': not defining a macro", - tline->text); - return DIRECTIVE_FOUND; - } - k = hash(defining->name); - defining->next = mmacros[k]; - mmacros[k] = defining; - defining = NULL; - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_ROTATE: - if (tline->next && tline->next->type == TOK_WHITESPACE) - tline = tline->next; - if (tline->next == NULL) - { - free_tlist(origline); - error(ERR_NONFATAL, "`%%rotate' missing rotate count"); - return DIRECTIVE_FOUND; - } - t = expand_smacro(tline->next); - tline->next = NULL; - free_tlist(origline); - tline = t; - tptr = &t; - tokval.t_type = TOKEN_INVALID; - evalresult = evaluate(ppscan, tptr, &tokval, pass, error); - free_tlist(tline); - if (!evalresult) - return DIRECTIVE_FOUND; - if (tokval.t_type) - error(ERR_WARNING, - "trailing garbage after expression ignored"); - intn = yasm_expr_get_intnum(&evalresult, 0); - if (!intn) - { - error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); - yasm_expr_destroy(evalresult); - return DIRECTIVE_FOUND; - } - mmac = istk->mstk; - while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ - mmac = mmac->next_active; - if (!mmac) - { - error(ERR_NONFATAL, - "`%%rotate' invoked outside a macro call"); - } - else if (mmac->nparam == 0) - { - error(ERR_NONFATAL, - "`%%rotate' invoked within macro without parameters"); - } - else - { - mmac->rotate = mmac->rotate + yasm_intnum_get_int(intn); - - if (mmac->rotate < 0) - mmac->rotate = - mmac->nparam - (-mmac->rotate) % mmac->nparam; - mmac->rotate %= mmac->nparam; - } - yasm_expr_destroy(evalresult); - return DIRECTIVE_FOUND; - - case PP_REP: - nolist = FALSE; - do { - tline = tline->next; - } while (tok_type_(tline, TOK_WHITESPACE)); - - if (tok_type_(tline, TOK_ID) && - nasm_stricmp(tline->text, ".nolist") == 0) - { - nolist = TRUE; - do { - tline = tline->next; - } while (tok_type_(tline, TOK_WHITESPACE)); - } - - if (tline) - { - t = expand_smacro(tline); - tptr = &t; - tokval.t_type = TOKEN_INVALID; - evalresult = evaluate(ppscan, tptr, &tokval, pass, error); - if (!evalresult) - { - free_tlist(origline); - return DIRECTIVE_FOUND; - } - if (tokval.t_type) - error(ERR_WARNING, - "trailing garbage after expression ignored"); - intn = yasm_expr_get_intnum(&evalresult, 0); - if (!intn) - { - error(ERR_NONFATAL, "non-constant value given to `%%rep'"); - yasm_expr_destroy(evalresult); - return DIRECTIVE_FOUND; - } - i = (int)yasm_intnum_get_int(intn) + 1; - yasm_expr_destroy(evalresult); - } - else - { - error(ERR_NONFATAL, "`%%rep' expects a repeat count"); - i = 0; - } - free_tlist(origline); - - tmp_defining = defining; - defining = nasm_malloc(sizeof(MMacro)); - defining->name = NULL; /* flags this macro as a %rep block */ - defining->casesense = 0; - defining->plus = FALSE; - defining->nolist = nolist; - defining->in_progress = i; - defining->nparam_min = defining->nparam_max = 0; - defining->defaults = NULL; - defining->dlist = NULL; - defining->expansion = NULL; - defining->next_active = istk->mstk; - defining->rep_nest = tmp_defining; - return DIRECTIVE_FOUND; - - case PP_ENDREP: - if (!defining || defining->name) - { - error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); - return DIRECTIVE_FOUND; - } - - /* - * Now we have a "macro" defined - although it has no name - * and we won't be entering it in the hash tables - we must - * push a macro-end marker for it on to istk->expansion. - * After that, it will take care of propagating itself (a - * macro-end marker line for a macro which is really a %rep - * block will cause the macro to be re-expanded, complete - * with another macro-end marker to ensure the process - * continues) until the whole expansion is forcibly removed - * from istk->expansion by a %exitrep. - */ - l = nasm_malloc(sizeof(Line)); - l->next = istk->expansion; - l->finishes = defining; - l->first = NULL; - istk->expansion = l; - - istk->mstk = defining; - - list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); - tmp_defining = defining; - defining = defining->rep_nest; - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_EXITREP: - /* - * We must search along istk->expansion until we hit a - * macro-end marker for a macro with no name. Then we set - * its `in_progress' flag to 0. - */ - for (l = istk->expansion; l; l = l->next) - if (l->finishes && !l->finishes->name) - break; - - if (l) - l->finishes->in_progress = 0; - else - error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_XDEFINE: - case PP_IXDEFINE: - case PP_DEFINE: - case PP_IDEFINE: - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tline || (tline->type != TOK_ID && - (tline->type != TOK_PREPROC_ID || - tline->text[1] != '$'))) - { - error(ERR_NONFATAL, - "`%%%s%sdefine' expects a macro identifier", - ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""), - ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : "")); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - - ctx = get_ctx(tline->text, FALSE); - if (!ctx) - smhead = &smacros[hash(tline->text)]; - else - smhead = &ctx->localmac; - mname = tline->text; - last = tline; - param_start = tline = tline->next; - nparam = 0; - - /* Expand the macro definition now for %xdefine and %ixdefine */ - if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) - tline = expand_smacro(tline); - - if (tok_is_(tline, "(")) - { - /* - * This macro has parameters. - */ - - tline = tline->next; - while (1) - { - skip_white_(tline); - if (!tline) - { - error(ERR_NONFATAL, "parameter identifier expected"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - if (tline->type != TOK_ID) - { - error(ERR_NONFATAL, - "`%s': parameter identifier expected", - tline->text); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - tline->type = TOK_SMAC_PARAM + nparam++; - tline = tline->next; - skip_white_(tline); - if (tok_is_(tline, ",")) - { - tline = tline->next; - continue; - } - if (!tok_is_(tline, ")")) - { - error(ERR_NONFATAL, - "`)' expected to terminate macro template"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - break; - } - last = tline; - tline = tline->next; - } - if (tok_type_(tline, TOK_WHITESPACE)) - last = tline, tline = tline->next; - macro_start = NULL; - last->next = NULL; - t = tline; - while (t) - { - if (t->type == TOK_ID) - { - for (tt = param_start; tt; tt = tt->next) - if (tt->type >= TOK_SMAC_PARAM && - !strcmp(tt->text, t->text)) - t->type = tt->type; - } - tt = t->next; - t->next = macro_start; - macro_start = t; - t = tt; - } - /* - * Good. We now have a macro name, a parameter count, and a - * token list (in reverse order) for an expansion. We ought - * to be OK just to create an SMacro, store it, and let - * free_tlist have the rest of the line (which we have - * carefully re-terminated after chopping off the expansion - * from the end). - */ - if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) - { - if (!smac) - { - error(ERR_WARNING, - "single-line macro `%s' defined both with and" - " without parameters", mname); - free_tlist(origline); - free_tlist(macro_start); - return DIRECTIVE_FOUND; - } - else - { - /* - * We're redefining, so we have to take over an - * existing SMacro structure. This means freeing - * what was already in it. - */ - nasm_free(smac->name); - free_tlist(smac->expansion); - } - } - else - { - smac = nasm_malloc(sizeof(SMacro)); - smac->next = *smhead; - *smhead = smac; - } - smac->name = nasm_strdup(mname); - smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE)); - smac->nparam = nparam; - smac->expansion = macro_start; - smac->in_progress = FALSE; - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_UNDEF: - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tline || (tline->type != TOK_ID && - (tline->type != TOK_PREPROC_ID || - tline->text[1] != '$'))) - { - error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - if (tline->next) - { - error(ERR_WARNING, - "trailing garbage after macro name ignored"); - } - - /* Find the context that symbol belongs to */ - ctx = get_ctx(tline->text, FALSE); - if (!ctx) - smhead = &smacros[hash(tline->text)]; - else - smhead = &ctx->localmac; - - mname = tline->text; - - /* - * We now have a macro name... go hunt for it. - */ - while (smacro_defined(ctx, mname, -1, &smac, 1)) - { - /* Defined, so we need to find its predecessor and nuke it */ - SMacro **s; - for (s = smhead; *s && *s != smac; s = &(*s)->next); - if (*s) - { - *s = smac->next; - nasm_free(smac->name); - free_tlist(smac->expansion); - nasm_free(smac); - } - } - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_STRLEN: - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tline || (tline->type != TOK_ID && - (tline->type != TOK_PREPROC_ID || - tline->text[1] != '$'))) - { - error(ERR_NONFATAL, - "`%%strlen' expects a macro identifier as first parameter"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - ctx = get_ctx(tline->text, FALSE); - if (!ctx) - smhead = &smacros[hash(tline->text)]; - else - smhead = &ctx->localmac; - mname = tline->text; - last = tline; - tline = expand_smacro(tline->next); - last->next = NULL; - - t = tline; - while (tok_type_(t, TOK_WHITESPACE)) - t = t->next; - /* t should now point to the string */ - if (t->type != TOK_STRING) - { - error(ERR_NONFATAL, - "`%%strlen` requires string as second parameter"); - free_tlist(tline); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - - macro_start = nasm_malloc(sizeof(*macro_start)); - macro_start->next = NULL; - make_tok_num(macro_start, - yasm_intnum_create_uint((unsigned long)(strlen(t->text) - 2))); - macro_start->mac = NULL; - - /* - * We now have a macro name, an implicit parameter count of - * zero, and a numeric token to use as an expansion. Create - * and store an SMacro. - */ - if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) - { - if (!smac) - error(ERR_WARNING, - "single-line macro `%s' defined both with and" - " without parameters", mname); - else - { - /* - * We're redefining, so we have to take over an - * existing SMacro structure. This means freeing - * what was already in it. - */ - nasm_free(smac->name); - free_tlist(smac->expansion); - } - } - else - { - smac = nasm_malloc(sizeof(SMacro)); - smac->next = *smhead; - *smhead = smac; - } - smac->name = nasm_strdup(mname); - smac->casesense = (i == PP_STRLEN); - smac->nparam = 0; - smac->expansion = macro_start; - smac->in_progress = FALSE; - free_tlist(tline); - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_SUBSTR: - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tline || (tline->type != TOK_ID && - (tline->type != TOK_PREPROC_ID || - tline->text[1] != '$'))) - { - error(ERR_NONFATAL, - "`%%substr' expects a macro identifier as first parameter"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - ctx = get_ctx(tline->text, FALSE); - if (!ctx) - smhead = &smacros[hash(tline->text)]; - else - smhead = &ctx->localmac; - mname = tline->text; - last = tline; - tline = expand_smacro(tline->next); - last->next = NULL; - - t = tline->next; - while (tok_type_(t, TOK_WHITESPACE)) - t = t->next; - - /* t should now point to the string */ - if (t->type != TOK_STRING) - { - error(ERR_NONFATAL, - "`%%substr` requires string as second parameter"); - free_tlist(tline); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - - tt = t->next; - tptr = &tt; - tokval.t_type = TOKEN_INVALID; - evalresult = evaluate(ppscan, tptr, &tokval, pass, error); - if (!evalresult) - { - free_tlist(tline); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - intn = yasm_expr_get_intnum(&evalresult, 0); - if (!intn) - { - error(ERR_NONFATAL, "non-constant value given to `%%substr`"); - free_tlist(tline); - free_tlist(origline); - yasm_expr_destroy(evalresult); - return DIRECTIVE_FOUND; - } - - macro_start = nasm_malloc(sizeof(*macro_start)); - macro_start->next = NULL; - macro_start->text = nasm_strdup("'''"); - if (yasm_intnum_sign(intn) == 1 - && yasm_intnum_get_uint(intn) < strlen(t->text) - 1) - { - macro_start->text[1] = t->text[yasm_intnum_get_uint(intn)]; - } - else - { - macro_start->text[2] = '\0'; - } - yasm_expr_destroy(evalresult); - macro_start->type = TOK_STRING; - macro_start->mac = NULL; - - /* - * We now have a macro name, an implicit parameter count of - * zero, and a numeric token to use as an expansion. Create - * and store an SMacro. - */ - if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) - { - if (!smac) - error(ERR_WARNING, - "single-line macro `%s' defined both with and" - " without parameters", mname); - else - { - /* - * We're redefining, so we have to take over an - * existing SMacro structure. This means freeing - * what was already in it. - */ - nasm_free(smac->name); - free_tlist(smac->expansion); - } - } - else - { - smac = nasm_malloc(sizeof(SMacro)); - smac->next = *smhead; - *smhead = smac; - } - smac->name = nasm_strdup(mname); - smac->casesense = (i == PP_SUBSTR); - smac->nparam = 0; - smac->expansion = macro_start; - smac->in_progress = FALSE; - free_tlist(tline); - free_tlist(origline); - return DIRECTIVE_FOUND; - - - case PP_ASSIGN: - case PP_IASSIGN: - tline = tline->next; - skip_white_(tline); - tline = expand_id(tline); - if (!tline || (tline->type != TOK_ID && - (tline->type != TOK_PREPROC_ID || - tline->text[1] != '$'))) - { - error(ERR_NONFATAL, - "`%%%sassign' expects a macro identifier", - (i == PP_IASSIGN ? "i" : "")); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - ctx = get_ctx(tline->text, FALSE); - if (!ctx) - smhead = &smacros[hash(tline->text)]; - else - smhead = &ctx->localmac; - mname = tline->text; - last = tline; - tline = expand_smacro(tline->next); - last->next = NULL; - - t = tline; - tptr = &t; - tokval.t_type = TOKEN_INVALID; - evalresult = evaluate(ppscan, tptr, &tokval, pass, error); - free_tlist(tline); - if (!evalresult) - { - free_tlist(origline); - return DIRECTIVE_FOUND; - } - - if (tokval.t_type) - error(ERR_WARNING, - "trailing garbage after expression ignored"); - - intn = yasm_expr_get_intnum(&evalresult, 0); - if (!intn) - { - error(ERR_NONFATAL, - "non-constant value given to `%%%sassign'", - (i == PP_IASSIGN ? "i" : "")); - free_tlist(origline); - yasm_expr_destroy(evalresult); - return DIRECTIVE_FOUND; - } - - macro_start = nasm_malloc(sizeof(*macro_start)); - macro_start->next = NULL; - make_tok_num(macro_start, yasm_intnum_copy(intn)); - yasm_expr_destroy(evalresult); - macro_start->mac = NULL; - - /* - * We now have a macro name, an implicit parameter count of - * zero, and a numeric token to use as an expansion. Create - * and store an SMacro. - */ - if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) - { - if (!smac) - error(ERR_WARNING, - "single-line macro `%s' defined both with and" - " without parameters", mname); - else - { - /* - * We're redefining, so we have to take over an - * existing SMacro structure. This means freeing - * what was already in it. - */ - nasm_free(smac->name); - free_tlist(smac->expansion); - } - } - else - { - smac = nasm_malloc(sizeof(SMacro)); - smac->next = *smhead; - *smhead = smac; - } - smac->name = nasm_strdup(mname); - smac->casesense = (i == PP_ASSIGN); - smac->nparam = 0; - smac->expansion = macro_start; - smac->in_progress = FALSE; - free_tlist(origline); - return DIRECTIVE_FOUND; - - case PP_LINE: - /* - * Syntax is `%line nnn[+mmm] [filename]' - */ - tline = tline->next; - skip_white_(tline); - if (!tok_type_(tline, TOK_NUMBER)) - { - error(ERR_NONFATAL, "`%%line' expects line number"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - intn = nasm_readnum(tline->text, &j); - k = yasm_intnum_get_int(intn); - yasm_intnum_destroy(intn); - m = 1; - tline = tline->next; - if (tok_is_(tline, "+")) - { - tline = tline->next; - if (!tok_type_(tline, TOK_NUMBER)) - { - error(ERR_NONFATAL, "`%%line' expects line increment"); - free_tlist(origline); - return DIRECTIVE_FOUND; - } - intn = nasm_readnum(tline->text, &j); - m = yasm_intnum_get_int(intn); - yasm_intnum_destroy(intn); - tline = tline->next; - } - skip_white_(tline); - nasm_src_set_linnum(k); - istk->lineinc = m; - if (tline) - { - nasm_free(nasm_src_set_fname(detoken(tline, FALSE))); - } - free_tlist(origline); - return DIRECTIVE_FOUND; - - default: - error(ERR_FATAL, - "preprocessor directive `%s' not yet implemented", - directives[i]); - break; + case PP_ELIFNNUM: + case PP_ELIFNSTR: + case PP_ELIFNUM: + case PP_ELIFSTR: + if (!istk->conds) + error(ERR_FATAL, "`%s': no matching `%%if'", directives[i]); + if (emitting(istk->conds->state) + || istk->conds->state == COND_NEVER) + istk->conds->state = COND_NEVER; + else + { + /* + * IMPORTANT: In the case of %if, we will already have + * called expand_mmac_params(); however, if we're + * processing an %elif we must have been in a + * non-emitting mode, which would have inhibited + * the normal invocation of expand_mmac_params(). Therefore, + * we have to do it explicitly here. + */ + j = if_condition(expand_mmac_params(tline->next), i); + tline->next = NULL; /* it got freed */ + istk->conds->state = + j < 0 ? COND_NEVER : j ? COND_IF_TRUE : COND_IF_FALSE; + } + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_ELSE: + if (tline->next) + error(ERR_WARNING, "trailing garbage after `%%else' ignored"); + if (!istk->conds) + error(ERR_FATAL, "`%%else': no matching `%%if'"); + if (emitting(istk->conds->state) + || istk->conds->state == COND_NEVER) + istk->conds->state = COND_ELSE_FALSE; + else + istk->conds->state = COND_ELSE_TRUE; + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_ENDIF: + if (tline->next) + error(ERR_WARNING, + "trailing garbage after `%%endif' ignored"); + if (!istk->conds) + error(ERR_FATAL, "`%%endif': no matching `%%if'"); + cond = istk->conds; + istk->conds = cond->next; + nasm_free(cond); + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_MACRO: + case PP_IMACRO: + if (defining) + error(ERR_FATAL, + "`%%%smacro': already defining a macro", + (i == PP_IMACRO ? "i" : "")); + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tok_type_(tline, TOK_ID)) + { + error(ERR_NONFATAL, + "`%%%smacro' expects a macro name", + (i == PP_IMACRO ? "i" : "")); + return DIRECTIVE_FOUND; + } + defining = nasm_malloc(sizeof(MMacro)); + defining->name = nasm_strdup(tline->text); + defining->casesense = (i == PP_MACRO); + defining->plus = FALSE; + defining->nolist = FALSE; + defining->in_progress = FALSE; + defining->rep_nest = NULL; + tline = expand_smacro(tline->next); + skip_white_(tline); + if (!tok_type_(tline, TOK_NUMBER)) + { + error(ERR_NONFATAL, + "`%%%smacro' expects a parameter count", + (i == PP_IMACRO ? "i" : "")); + defining->nparam_min = defining->nparam_max = 0; + } + else + { + intn = nasm_readnum(tline->text, &j); + defining->nparam_min = defining->nparam_max = + yasm_intnum_get_int(intn); + yasm_intnum_destroy(intn); + if (j) + error(ERR_NONFATAL, + "unable to parse parameter count `%s'", + tline->text); + } + if (tline && tok_is_(tline->next, "-")) + { + tline = tline->next->next; + if (tok_is_(tline, "*")) + defining->nparam_max = INT_MAX; + else if (!tok_type_(tline, TOK_NUMBER)) + error(ERR_NONFATAL, + "`%%%smacro' expects a parameter count after `-'", + (i == PP_IMACRO ? "i" : "")); + else + { + intn = nasm_readnum(tline->text, &j); + defining->nparam_max = yasm_intnum_get_int(intn); + yasm_intnum_destroy(intn); + if (j) + error(ERR_NONFATAL, + "unable to parse parameter count `%s'", + tline->text); + if (defining->nparam_min > defining->nparam_max) + error(ERR_NONFATAL, + "minimum parameter count exceeds maximum"); + } + } + if (tline && tok_is_(tline->next, "+")) + { + tline = tline->next; + defining->plus = TRUE; + } + if (tline && tok_type_(tline->next, TOK_ID) && + !nasm_stricmp(tline->next->text, ".nolist")) + { + tline = tline->next; + defining->nolist = TRUE; + } + mmac = mmacros[hash(defining->name)]; + while (mmac) + { + if (!strcmp(mmac->name, defining->name) && + (mmac->nparam_min <= defining->nparam_max + || defining->plus) + && (defining->nparam_min <= mmac->nparam_max + || mmac->plus)) + { + error(ERR_WARNING, + "redefining multi-line macro `%s'", + defining->name); + break; + } + mmac = mmac->next; + } + /* + * Handle default parameters. + */ + if (tline && tline->next) + { + defining->dlist = tline->next; + tline->next = NULL; + count_mmac_params(defining->dlist, &defining->ndefs, + &defining->defaults); + } + else + { + defining->dlist = NULL; + defining->defaults = NULL; + } + defining->expansion = NULL; + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_ENDM: + case PP_ENDMACRO: + if (!defining) + { + error(ERR_NONFATAL, "`%s': not defining a macro", + tline->text); + return DIRECTIVE_FOUND; + } + k = hash(defining->name); + defining->next = mmacros[k]; + mmacros[k] = defining; + defining = NULL; + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_ROTATE: + if (tline->next && tline->next->type == TOK_WHITESPACE) + tline = tline->next; + if (tline->next == NULL) + { + free_tlist(origline); + error(ERR_NONFATAL, "`%%rotate' missing rotate count"); + return DIRECTIVE_FOUND; + } + t = expand_smacro(tline->next); + tline->next = NULL; + free_tlist(origline); + tline = t; + tptr = &t; + tokval.t_type = TOKEN_INVALID; + evalresult = evaluate(ppscan, tptr, &tokval, pass, error); + free_tlist(tline); + if (!evalresult) + return DIRECTIVE_FOUND; + if (tokval.t_type) + error(ERR_WARNING, + "trailing garbage after expression ignored"); + intn = yasm_expr_get_intnum(&evalresult, 0); + if (!intn) + { + error(ERR_NONFATAL, "non-constant value given to `%%rotate'"); + yasm_expr_destroy(evalresult); + return DIRECTIVE_FOUND; + } + mmac = istk->mstk; + while (mmac && !mmac->name) /* avoid mistaking %reps for macros */ + mmac = mmac->next_active; + if (!mmac) + { + error(ERR_NONFATAL, + "`%%rotate' invoked outside a macro call"); + } + else if (mmac->nparam == 0) + { + error(ERR_NONFATAL, + "`%%rotate' invoked within macro without parameters"); + } + else + { + mmac->rotate = mmac->rotate + yasm_intnum_get_int(intn); + + if (mmac->rotate < 0) + mmac->rotate = + mmac->nparam - (-mmac->rotate) % mmac->nparam; + mmac->rotate %= mmac->nparam; + } + yasm_expr_destroy(evalresult); + return DIRECTIVE_FOUND; + + case PP_REP: + nolist = FALSE; + do { + tline = tline->next; + } while (tok_type_(tline, TOK_WHITESPACE)); + + if (tok_type_(tline, TOK_ID) && + nasm_stricmp(tline->text, ".nolist") == 0) + { + nolist = TRUE; + do { + tline = tline->next; + } while (tok_type_(tline, TOK_WHITESPACE)); + } + + if (tline) + { + t = expand_smacro(tline); + tptr = &t; + tokval.t_type = TOKEN_INVALID; + evalresult = evaluate(ppscan, tptr, &tokval, pass, error); + if (!evalresult) + { + free_tlist(origline); + return DIRECTIVE_FOUND; + } + if (tokval.t_type) + error(ERR_WARNING, + "trailing garbage after expression ignored"); + intn = yasm_expr_get_intnum(&evalresult, 0); + if (!intn) + { + error(ERR_NONFATAL, "non-constant value given to `%%rep'"); + yasm_expr_destroy(evalresult); + return DIRECTIVE_FOUND; + } + i = (int)yasm_intnum_get_int(intn) + 1; + yasm_expr_destroy(evalresult); + } + else + { + error(ERR_NONFATAL, "`%%rep' expects a repeat count"); + i = 0; + } + free_tlist(origline); + + tmp_defining = defining; + defining = nasm_malloc(sizeof(MMacro)); + defining->name = NULL; /* flags this macro as a %rep block */ + defining->casesense = 0; + defining->plus = FALSE; + defining->nolist = nolist; + defining->in_progress = i; + defining->nparam_min = defining->nparam_max = 0; + defining->defaults = NULL; + defining->dlist = NULL; + defining->expansion = NULL; + defining->next_active = istk->mstk; + defining->rep_nest = tmp_defining; + return DIRECTIVE_FOUND; + + case PP_ENDREP: + if (!defining || defining->name) + { + error(ERR_NONFATAL, "`%%endrep': no matching `%%rep'"); + return DIRECTIVE_FOUND; + } + + /* + * Now we have a "macro" defined - although it has no name + * and we won't be entering it in the hash tables - we must + * push a macro-end marker for it on to istk->expansion. + * After that, it will take care of propagating itself (a + * macro-end marker line for a macro which is really a %rep + * block will cause the macro to be re-expanded, complete + * with another macro-end marker to ensure the process + * continues) until the whole expansion is forcibly removed + * from istk->expansion by a %exitrep. + */ + l = nasm_malloc(sizeof(Line)); + l->next = istk->expansion; + l->finishes = defining; + l->first = NULL; + istk->expansion = l; + + istk->mstk = defining; + + list->uplevel(defining->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); + tmp_defining = defining; + defining = defining->rep_nest; + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_EXITREP: + /* + * We must search along istk->expansion until we hit a + * macro-end marker for a macro with no name. Then we set + * its `in_progress' flag to 0. + */ + for (l = istk->expansion; l; l = l->next) + if (l->finishes && !l->finishes->name) + break; + + if (l) + l->finishes->in_progress = 0; + else + error(ERR_NONFATAL, "`%%exitrep' not within `%%rep' block"); + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_XDEFINE: + case PP_IXDEFINE: + case PP_DEFINE: + case PP_IDEFINE: + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tline || (tline->type != TOK_ID && + (tline->type != TOK_PREPROC_ID || + tline->text[1] != '$'))) + { + error(ERR_NONFATAL, + "`%%%s%sdefine' expects a macro identifier", + ((i == PP_IDEFINE || i == PP_IXDEFINE) ? "i" : ""), + ((i == PP_XDEFINE || i == PP_IXDEFINE) ? "x" : "")); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + + ctx = get_ctx(tline->text, FALSE); + if (!ctx) + smhead = &smacros[hash(tline->text)]; + else + smhead = &ctx->localmac; + mname = tline->text; + last = tline; + param_start = tline = tline->next; + nparam = 0; + + /* Expand the macro definition now for %xdefine and %ixdefine */ + if ((i == PP_XDEFINE) || (i == PP_IXDEFINE)) + tline = expand_smacro(tline); + + if (tok_is_(tline, "(")) + { + /* + * This macro has parameters. + */ + + tline = tline->next; + while (1) + { + skip_white_(tline); + if (!tline) + { + error(ERR_NONFATAL, "parameter identifier expected"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + if (tline->type != TOK_ID) + { + error(ERR_NONFATAL, + "`%s': parameter identifier expected", + tline->text); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + tline->type = TOK_SMAC_PARAM + nparam++; + tline = tline->next; + skip_white_(tline); + if (tok_is_(tline, ",")) + { + tline = tline->next; + continue; + } + if (!tok_is_(tline, ")")) + { + error(ERR_NONFATAL, + "`)' expected to terminate macro template"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + break; + } + last = tline; + tline = tline->next; + } + if (tok_type_(tline, TOK_WHITESPACE)) + last = tline, tline = tline->next; + macro_start = NULL; + last->next = NULL; + t = tline; + while (t) + { + if (t->type == TOK_ID) + { + for (tt = param_start; tt; tt = tt->next) + if (tt->type >= TOK_SMAC_PARAM && + !strcmp(tt->text, t->text)) + t->type = tt->type; + } + tt = t->next; + t->next = macro_start; + macro_start = t; + t = tt; + } + /* + * Good. We now have a macro name, a parameter count, and a + * token list (in reverse order) for an expansion. We ought + * to be OK just to create an SMacro, store it, and let + * free_tlist have the rest of the line (which we have + * carefully re-terminated after chopping off the expansion + * from the end). + */ + if (smacro_defined(ctx, mname, nparam, &smac, i == PP_DEFINE)) + { + if (!smac) + { + error(ERR_WARNING, + "single-line macro `%s' defined both with and" + " without parameters", mname); + free_tlist(origline); + free_tlist(macro_start); + return DIRECTIVE_FOUND; + } + else + { + /* + * We're redefining, so we have to take over an + * existing SMacro structure. This means freeing + * what was already in it. + */ + nasm_free(smac->name); + free_tlist(smac->expansion); + } + } + else + { + smac = nasm_malloc(sizeof(SMacro)); + smac->next = *smhead; + *smhead = smac; + } + smac->name = nasm_strdup(mname); + smac->casesense = ((i == PP_DEFINE) || (i == PP_XDEFINE)); + smac->nparam = nparam; + smac->expansion = macro_start; + smac->in_progress = FALSE; + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_UNDEF: + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tline || (tline->type != TOK_ID && + (tline->type != TOK_PREPROC_ID || + tline->text[1] != '$'))) + { + error(ERR_NONFATAL, "`%%undef' expects a macro identifier"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + if (tline->next) + { + error(ERR_WARNING, + "trailing garbage after macro name ignored"); + } + + /* Find the context that symbol belongs to */ + ctx = get_ctx(tline->text, FALSE); + if (!ctx) + smhead = &smacros[hash(tline->text)]; + else + smhead = &ctx->localmac; + + mname = tline->text; + + /* + * We now have a macro name... go hunt for it. + */ + while (smacro_defined(ctx, mname, -1, &smac, 1)) + { + /* Defined, so we need to find its predecessor and nuke it */ + SMacro **s; + for (s = smhead; *s && *s != smac; s = &(*s)->next); + if (*s) + { + *s = smac->next; + nasm_free(smac->name); + free_tlist(smac->expansion); + nasm_free(smac); + } + } + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_STRLEN: + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tline || (tline->type != TOK_ID && + (tline->type != TOK_PREPROC_ID || + tline->text[1] != '$'))) + { + error(ERR_NONFATAL, + "`%%strlen' expects a macro identifier as first parameter"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + ctx = get_ctx(tline->text, FALSE); + if (!ctx) + smhead = &smacros[hash(tline->text)]; + else + smhead = &ctx->localmac; + mname = tline->text; + last = tline; + tline = expand_smacro(tline->next); + last->next = NULL; + + t = tline; + while (tok_type_(t, TOK_WHITESPACE)) + t = t->next; + /* t should now point to the string */ + if (t->type != TOK_STRING) + { + error(ERR_NONFATAL, + "`%%strlen` requires string as second parameter"); + free_tlist(tline); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + + macro_start = nasm_malloc(sizeof(*macro_start)); + macro_start->next = NULL; + make_tok_num(macro_start, + yasm_intnum_create_uint((unsigned long)(strlen(t->text) - 2))); + macro_start->mac = NULL; + + /* + * We now have a macro name, an implicit parameter count of + * zero, and a numeric token to use as an expansion. Create + * and store an SMacro. + */ + if (smacro_defined(ctx, mname, 0, &smac, i == PP_STRLEN)) + { + if (!smac) + error(ERR_WARNING, + "single-line macro `%s' defined both with and" + " without parameters", mname); + else + { + /* + * We're redefining, so we have to take over an + * existing SMacro structure. This means freeing + * what was already in it. + */ + nasm_free(smac->name); + free_tlist(smac->expansion); + } + } + else + { + smac = nasm_malloc(sizeof(SMacro)); + smac->next = *smhead; + *smhead = smac; + } + smac->name = nasm_strdup(mname); + smac->casesense = (i == PP_STRLEN); + smac->nparam = 0; + smac->expansion = macro_start; + smac->in_progress = FALSE; + free_tlist(tline); + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_SUBSTR: + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tline || (tline->type != TOK_ID && + (tline->type != TOK_PREPROC_ID || + tline->text[1] != '$'))) + { + error(ERR_NONFATAL, + "`%%substr' expects a macro identifier as first parameter"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + ctx = get_ctx(tline->text, FALSE); + if (!ctx) + smhead = &smacros[hash(tline->text)]; + else + smhead = &ctx->localmac; + mname = tline->text; + last = tline; + tline = expand_smacro(tline->next); + last->next = NULL; + + t = tline->next; + while (tok_type_(t, TOK_WHITESPACE)) + t = t->next; + + /* t should now point to the string */ + if (t->type != TOK_STRING) + { + error(ERR_NONFATAL, + "`%%substr` requires string as second parameter"); + free_tlist(tline); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + + tt = t->next; + tptr = &tt; + tokval.t_type = TOKEN_INVALID; + evalresult = evaluate(ppscan, tptr, &tokval, pass, error); + if (!evalresult) + { + free_tlist(tline); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + intn = yasm_expr_get_intnum(&evalresult, 0); + if (!intn) + { + error(ERR_NONFATAL, "non-constant value given to `%%substr`"); + free_tlist(tline); + free_tlist(origline); + yasm_expr_destroy(evalresult); + return DIRECTIVE_FOUND; + } + + macro_start = nasm_malloc(sizeof(*macro_start)); + macro_start->next = NULL; + macro_start->text = nasm_strdup("'''"); + if (yasm_intnum_sign(intn) == 1 + && yasm_intnum_get_uint(intn) < strlen(t->text) - 1) + { + macro_start->text[1] = t->text[yasm_intnum_get_uint(intn)]; + } + else + { + macro_start->text[2] = '\0'; + } + yasm_expr_destroy(evalresult); + macro_start->type = TOK_STRING; + macro_start->mac = NULL; + + /* + * We now have a macro name, an implicit parameter count of + * zero, and a numeric token to use as an expansion. Create + * and store an SMacro. + */ + if (smacro_defined(ctx, mname, 0, &smac, i == PP_SUBSTR)) + { + if (!smac) + error(ERR_WARNING, + "single-line macro `%s' defined both with and" + " without parameters", mname); + else + { + /* + * We're redefining, so we have to take over an + * existing SMacro structure. This means freeing + * what was already in it. + */ + nasm_free(smac->name); + free_tlist(smac->expansion); + } + } + else + { + smac = nasm_malloc(sizeof(SMacro)); + smac->next = *smhead; + *smhead = smac; + } + smac->name = nasm_strdup(mname); + smac->casesense = (i == PP_SUBSTR); + smac->nparam = 0; + smac->expansion = macro_start; + smac->in_progress = FALSE; + free_tlist(tline); + free_tlist(origline); + return DIRECTIVE_FOUND; + + + case PP_ASSIGN: + case PP_IASSIGN: + tline = tline->next; + skip_white_(tline); + tline = expand_id(tline); + if (!tline || (tline->type != TOK_ID && + (tline->type != TOK_PREPROC_ID || + tline->text[1] != '$'))) + { + error(ERR_NONFATAL, + "`%%%sassign' expects a macro identifier", + (i == PP_IASSIGN ? "i" : "")); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + ctx = get_ctx(tline->text, FALSE); + if (!ctx) + smhead = &smacros[hash(tline->text)]; + else + smhead = &ctx->localmac; + mname = tline->text; + last = tline; + tline = expand_smacro(tline->next); + last->next = NULL; + + t = tline; + tptr = &t; + tokval.t_type = TOKEN_INVALID; + evalresult = evaluate(ppscan, tptr, &tokval, pass, error); + free_tlist(tline); + if (!evalresult) + { + free_tlist(origline); + return DIRECTIVE_FOUND; + } + + if (tokval.t_type) + error(ERR_WARNING, + "trailing garbage after expression ignored"); + + intn = yasm_expr_get_intnum(&evalresult, 0); + if (!intn) + { + error(ERR_NONFATAL, + "non-constant value given to `%%%sassign'", + (i == PP_IASSIGN ? "i" : "")); + free_tlist(origline); + yasm_expr_destroy(evalresult); + return DIRECTIVE_FOUND; + } + + macro_start = nasm_malloc(sizeof(*macro_start)); + macro_start->next = NULL; + make_tok_num(macro_start, yasm_intnum_copy(intn)); + yasm_expr_destroy(evalresult); + macro_start->mac = NULL; + + /* + * We now have a macro name, an implicit parameter count of + * zero, and a numeric token to use as an expansion. Create + * and store an SMacro. + */ + if (smacro_defined(ctx, mname, 0, &smac, i == PP_ASSIGN)) + { + if (!smac) + error(ERR_WARNING, + "single-line macro `%s' defined both with and" + " without parameters", mname); + else + { + /* + * We're redefining, so we have to take over an + * existing SMacro structure. This means freeing + * what was already in it. + */ + nasm_free(smac->name); + free_tlist(smac->expansion); + } + } + else + { + smac = nasm_malloc(sizeof(SMacro)); + smac->next = *smhead; + *smhead = smac; + } + smac->name = nasm_strdup(mname); + smac->casesense = (i == PP_ASSIGN); + smac->nparam = 0; + smac->expansion = macro_start; + smac->in_progress = FALSE; + free_tlist(origline); + return DIRECTIVE_FOUND; + + case PP_LINE: + /* + * Syntax is `%line nnn[+mmm] [filename]' + */ + tline = tline->next; + skip_white_(tline); + if (!tok_type_(tline, TOK_NUMBER)) + { + error(ERR_NONFATAL, "`%%line' expects line number"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + intn = nasm_readnum(tline->text, &j); + k = yasm_intnum_get_int(intn); + yasm_intnum_destroy(intn); + m = 1; + tline = tline->next; + if (tok_is_(tline, "+")) + { + tline = tline->next; + if (!tok_type_(tline, TOK_NUMBER)) + { + error(ERR_NONFATAL, "`%%line' expects line increment"); + free_tlist(origline); + return DIRECTIVE_FOUND; + } + intn = nasm_readnum(tline->text, &j); + m = yasm_intnum_get_int(intn); + yasm_intnum_destroy(intn); + tline = tline->next; + } + skip_white_(tline); + nasm_src_set_linnum(k); + istk->lineinc = m; + if (tline) + { + nasm_free(nasm_src_set_fname(detoken(tline, FALSE))); + } + free_tlist(origline); + return DIRECTIVE_FOUND; + + default: + error(ERR_FATAL, + "preprocessor directive `%s' not yet implemented", + directives[i]); + break; } return DIRECTIVE_FOUND; } @@ -3217,33 +3217,33 @@ find_cc(Token * t) skip_white_(t); if (t->type != TOK_ID) - return -1; + return -1; tt = t->next; skip_white_(tt); if (tt && (tt->type != TOK_OTHER || strcmp(tt->text, ","))) - return -1; + return -1; i = -1; j = elements(conditions); while (j - i > 1) { - k = (j + i) / 2; - m = nasm_stricmp(t->text, conditions[k]); - if (m == 0) - { - i = k; - j = -2; - break; - } - else if (m < 0) - { - j = k; - } - else - i = k; + k = (j + i) / 2; + m = nasm_stricmp(t->text, conditions[k]); + if (m == 0) + { + i = k; + j = -2; + break; + } + else if (m < 0) + { + j = k; + } + else + i = k; } if (j != -2) - return -1; + return -1; return i; } @@ -3261,177 +3261,177 @@ expand_mmac_params(Token * tline) while (tline) { - if (tline->type == TOK_PREPROC_ID && - (((tline->text[1] == '+' || tline->text[1] == '-') - && tline->text[2]) || tline->text[1] == '%' - || (tline->text[1] >= '0' && tline->text[1] <= '9'))) - { - char *text = NULL; - int type = 0, cc; /* type = 0 to placate optimisers */ - char tmpbuf[30]; - int n, i; - MMacro *mac; - - t = tline; - tline = tline->next; - - mac = istk->mstk; - while (mac && !mac->name) /* avoid mistaking %reps for macros */ - mac = mac->next_active; - if (!mac) - error(ERR_NONFATAL, "`%s': not in a macro call", t->text); - else - switch (t->text[1]) - { - /* - * We have to make a substitution of one of the - * forms %1, %-1, %+1, %%foo, %0. - */ - case '0': - type = TOK_NUMBER; - sprintf(tmpbuf, "%ld", mac->nparam); - text = nasm_strdup(tmpbuf); - break; - case '%': - type = TOK_ID; - sprintf(tmpbuf, "..@%lu.", mac->unique); - text = nasm_strcat(tmpbuf, t->text + 2); - break; - case '-': - n = atoi(t->text + 2) - 1; - if (n >= mac->nparam) - tt = NULL; - else - { - if (mac->nparam > 1) - n = (n + mac->rotate) % mac->nparam; - tt = mac->params[n]; - } - cc = find_cc(tt); - if (cc == -1) - { - error(ERR_NONFATAL, - "macro parameter %d is not a condition code", - n + 1); - text = NULL; - } - else - { - type = TOK_ID; - if (inverse_ccs[cc] == -1) - { - error(ERR_NONFATAL, - "condition code `%s' is not invertible", - conditions[cc]); - text = NULL; - } - else - text = - nasm_strdup(conditions[inverse_ccs - [cc]]); - } - break; - case '+': - n = atoi(t->text + 2) - 1; - if (n >= mac->nparam) - tt = NULL; - else - { - if (mac->nparam > 1) - n = (n + mac->rotate) % mac->nparam; - tt = mac->params[n]; - } - cc = find_cc(tt); - if (cc == -1) - { - error(ERR_NONFATAL, - "macro parameter %d is not a condition code", - n + 1); - text = NULL; - } - else - { - type = TOK_ID; - text = nasm_strdup(conditions[cc]); - } - break; - default: - n = atoi(t->text + 1) - 1; - if (n >= mac->nparam) - tt = NULL; - else - { - if (mac->nparam > 1) - n = (n + mac->rotate) % mac->nparam; - tt = mac->params[n]; - } - if (tt) - { - for (i = 0; i < mac->paramlen[n]; i++) - { - *tail = - new_Token(NULL, tt->type, tt->text, - 0); - tail = &(*tail)->next; - tt = tt->next; - } - } - text = NULL; /* we've done it here */ - break; - } - if (!text) - { - delete_Token(t); - } - else - { - *tail = t; - tail = &t->next; - t->type = type; - nasm_free(t->text); - t->text = text; - t->mac = NULL; - } - continue; - } - else - { - t = *tail = tline; - tline = tline->next; - t->mac = NULL; - tail = &t->next; - } + if (tline->type == TOK_PREPROC_ID && + (((tline->text[1] == '+' || tline->text[1] == '-') + && tline->text[2]) || tline->text[1] == '%' + || (tline->text[1] >= '0' && tline->text[1] <= '9'))) + { + char *text = NULL; + int type = 0, cc; /* type = 0 to placate optimisers */ + char tmpbuf[30]; + int n, i; + MMacro *mac; + + t = tline; + tline = tline->next; + + mac = istk->mstk; + while (mac && !mac->name) /* avoid mistaking %reps for macros */ + mac = mac->next_active; + if (!mac) + error(ERR_NONFATAL, "`%s': not in a macro call", t->text); + else + switch (t->text[1]) + { + /* + * We have to make a substitution of one of the + * forms %1, %-1, %+1, %%foo, %0. + */ + case '0': + type = TOK_NUMBER; + sprintf(tmpbuf, "%ld", mac->nparam); + text = nasm_strdup(tmpbuf); + break; + case '%': + type = TOK_ID; + sprintf(tmpbuf, "..@%lu.", mac->unique); + text = nasm_strcat(tmpbuf, t->text + 2); + break; + case '-': + n = atoi(t->text + 2) - 1; + if (n >= mac->nparam) + tt = NULL; + else + { + if (mac->nparam > 1) + n = (n + mac->rotate) % mac->nparam; + tt = mac->params[n]; + } + cc = find_cc(tt); + if (cc == -1) + { + error(ERR_NONFATAL, + "macro parameter %d is not a condition code", + n + 1); + text = NULL; + } + else + { + type = TOK_ID; + if (inverse_ccs[cc] == -1) + { + error(ERR_NONFATAL, + "condition code `%s' is not invertible", + conditions[cc]); + text = NULL; + } + else + text = + nasm_strdup(conditions[inverse_ccs + [cc]]); + } + break; + case '+': + n = atoi(t->text + 2) - 1; + if (n >= mac->nparam) + tt = NULL; + else + { + if (mac->nparam > 1) + n = (n + mac->rotate) % mac->nparam; + tt = mac->params[n]; + } + cc = find_cc(tt); + if (cc == -1) + { + error(ERR_NONFATAL, + "macro parameter %d is not a condition code", + n + 1); + text = NULL; + } + else + { + type = TOK_ID; + text = nasm_strdup(conditions[cc]); + } + break; + default: + n = atoi(t->text + 1) - 1; + if (n >= mac->nparam) + tt = NULL; + else + { + if (mac->nparam > 1) + n = (n + mac->rotate) % mac->nparam; + tt = mac->params[n]; + } + if (tt) + { + for (i = 0; i < mac->paramlen[n]; i++) + { + *tail = + new_Token(NULL, tt->type, tt->text, + 0); + tail = &(*tail)->next; + tt = tt->next; + } + } + text = NULL; /* we've done it here */ + break; + } + if (!text) + { + delete_Token(t); + } + else + { + *tail = t; + tail = &t->next; + t->type = type; + nasm_free(t->text); + t->text = text; + t->mac = NULL; + } + continue; + } + else + { + t = *tail = tline; + tline = tline->next; + t->mac = NULL; + tail = &t->next; + } } *tail = NULL; t = thead; for (; t && (tt = t->next) != NULL; t = t->next) - switch (t->type) - { - case TOK_WHITESPACE: - if (tt->type == TOK_WHITESPACE) - { - t->next = delete_Token(tt); - } - break; - case TOK_ID: - if (tt->type == TOK_ID || tt->type == TOK_NUMBER) - { - char *tmp = nasm_strcat(t->text, tt->text); - nasm_free(t->text); - t->text = tmp; - t->next = delete_Token(tt); - } - break; - case TOK_NUMBER: - if (tt->type == TOK_NUMBER) - { - char *tmp = nasm_strcat(t->text, tt->text); - nasm_free(t->text); - t->text = tmp; - t->next = delete_Token(tt); - } - break; - } + switch (t->type) + { + case TOK_WHITESPACE: + if (tt->type == TOK_WHITESPACE) + { + t->next = delete_Token(tt); + } + break; + case TOK_ID: + if (tt->type == TOK_ID || tt->type == TOK_NUMBER) + { + char *tmp = nasm_strcat(t->text, tt->text); + nasm_free(t->text); + t->text = tmp; + t->next = delete_Token(tt); + } + break; + case TOK_NUMBER: + if (tt->type == TOK_NUMBER) + { + char *tmp = nasm_strcat(t->text, tt->text); + nasm_free(t->text); + t->text = tmp; + t->next = delete_Token(tt); + } + break; + } return thead; } @@ -3463,12 +3463,12 @@ expand_smacro(Token * tline) */ if (org_tline) { - tline = - new_Token(org_tline->next, org_tline->type, org_tline->text, - 0); - tline->mac = org_tline->mac; - nasm_free(org_tline->text); - org_tline->text = NULL; + tline = + new_Token(org_tline->next, org_tline->type, org_tline->text, + 0); + tline->mac = org_tline->mac; + nasm_free(org_tline->text); + org_tline->text = NULL; } again: @@ -3476,277 +3476,277 @@ expand_smacro(Token * tline) thead = NULL; while (tline) - { /* main token loop */ - if ((mname = tline->text)) - { - /* if this token is a local macro, look in local context */ - if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) - ctx = get_ctx(mname, TRUE); - else - ctx = NULL; - if (!ctx) - head = smacros[hash(mname)]; - else - head = ctx->localmac; - /* - * We've hit an identifier. As in is_mmacro below, we first - * check whether the identifier is a single-line macro at - * all, then think about checking for parameters if - * necessary. - */ - for (m = head; m; m = m->next) - if (!mstrcmp(m->name, mname, m->casesense)) - break; - if (m) - { - mstart = tline; - params = NULL; - paramsize = NULL; - if (m->nparam == 0) - { - /* - * Simple case: the macro is parameterless. Discard the - * one token that the macro call took, and push the - * expansion back on the to-do stack. - */ - if (!m->expansion) - { - if (!strcmp("__FILE__", m->name)) - { - long num = 0; - nasm_src_get(&num, &(tline->text)); - nasm_quote(&(tline->text)); - tline->type = TOK_STRING; - continue; - } - if (!strcmp("__LINE__", m->name)) - { - nasm_free(tline->text); - make_tok_num(tline, yasm_intnum_create_int(nasm_src_get_linnum())); - continue; - } - tline = delete_Token(tline); - continue; - } - } - else - { - /* - * Complicated case: at least one macro with this name - * exists and takes parameters. We must find the - * parameters in the call, count them, find the SMacro - * that corresponds to that form of the macro call, and - * substitute for the parameters when we expand. What a - * pain. - */ - /*tline = tline->next; - skip_white_(tline);*/ - do { - t = tline->next; - while (tok_type_(t, TOK_SMAC_END)) - { - t->mac->in_progress = FALSE; - t->text = NULL; - t = tline->next = delete_Token(t); - } - tline = t; - } while (tok_type_(tline, TOK_WHITESPACE)); - if (!tok_is_(tline, "(")) - { - /* - * This macro wasn't called with parameters: ignore - * the call. (Behaviour borrowed from gnu cpp.) - */ - tline = mstart; - m = NULL; - } - else - { - int paren = 0; - int white = 0; - brackets = 0; - nparam = 0; - sparam = PARAM_DELTA; - params = nasm_malloc(sparam * sizeof(Token *)); - params[0] = tline->next; - paramsize = nasm_malloc(sparam * sizeof(int)); - paramsize[0] = 0; - while (TRUE) - { /* parameter loop */ - /* - * For some unusual expansions - * which concatenates function call - */ - t = tline->next; - while (tok_type_(t, TOK_SMAC_END)) - { - t->mac->in_progress = FALSE; - t->text = NULL; - t = tline->next = delete_Token(t); - } - tline = t; - - if (!tline) - { - error(ERR_NONFATAL, - "macro call expects terminating `)'"); - break; - } - if (tline->type == TOK_WHITESPACE - && brackets <= 0) - { - if (paramsize[nparam]) - white++; - else - params[nparam] = tline->next; - continue; /* parameter loop */ - } - if (tline->type == TOK_OTHER - && tline->text[1] == 0) - { - char ch = tline->text[0]; - if (ch == ',' && !paren && brackets <= 0) - { - if (++nparam >= sparam) - { - sparam += PARAM_DELTA; - params = nasm_realloc(params, - sparam * sizeof(Token *)); - paramsize = nasm_realloc(paramsize, - sparam * sizeof(int)); - } - params[nparam] = tline->next; - paramsize[nparam] = 0; - white = 0; - continue; /* parameter loop */ - } - if (ch == '{' && - (brackets > 0 || (brackets == 0 && - !paramsize[nparam]))) - { - if (!(brackets++)) - { - params[nparam] = tline->next; - continue; /* parameter loop */ - } - } - if (ch == '}' && brackets > 0) - if (--brackets == 0) - { - brackets = -1; - continue; /* parameter loop */ - } - if (ch == '(' && !brackets) - paren++; - if (ch == ')' && brackets <= 0) - if (--paren < 0) - break; - } - if (brackets < 0) - { - brackets = 0; - error(ERR_NONFATAL, "braces do not " - "enclose all of macro parameter"); - } - paramsize[nparam] += white + 1; - white = 0; - } /* parameter loop */ - nparam++; - while (m && (m->nparam != nparam || - mstrcmp(m->name, mname, - m->casesense))) - m = m->next; - if (!m) - error(ERR_WARNING | ERR_WARN_MNP, - "macro `%s' exists, " - "but not taking %d parameters", - mstart->text, nparam); - } - } - if (m && m->in_progress) - m = NULL; - if (!m) /* in progess or didn't find '(' or wrong nparam */ - { - /* - * Design question: should we handle !tline, which - * indicates missing ')' here, or expand those - * macros anyway, which requires the (t) test a few - * lines down? - */ - nasm_free(params); - nasm_free(paramsize); - tline = mstart; - } - else - { - /* - * Expand the macro: we are placed on the last token of the - * call, so that we can easily split the call from the - * following tokens. We also start by pushing an SMAC_END - * token for the cycle removal. - */ - t = tline; - if (t) - { - tline = t->next; - t->next = NULL; - } - tt = new_Token(tline, TOK_SMAC_END, NULL, 0); - tt->mac = m; - m->in_progress = TRUE; - tline = tt; - for (t = m->expansion; t; t = t->next) - { - if (t->type >= TOK_SMAC_PARAM) - { - Token *pcopy = tline, **ptail = &pcopy; - Token *ttt, *pt; - int i; - - ttt = params[t->type - TOK_SMAC_PARAM]; - for (i = paramsize[t->type - TOK_SMAC_PARAM]; - --i >= 0;) - { - pt = *ptail = - new_Token(tline, ttt->type, ttt->text, - 0); - ptail = &pt->next; - ttt = ttt->next; - } - tline = pcopy; - } - else - { - tt = new_Token(tline, t->type, t->text, 0); - tline = tt; - } - } - - /* - * Having done that, get rid of the macro call, and clean - * up the parameters. - */ - nasm_free(params); - nasm_free(paramsize); - free_tlist(mstart); - continue; /* main token loop */ - } - } - } - - if (tline->type == TOK_SMAC_END) - { - tline->mac->in_progress = FALSE; - tline = delete_Token(tline); - } - else - { - t = *tail = tline; - tline = tline->next; - t->mac = NULL; - t->next = NULL; - tail = &t->next; - } + { /* main token loop */ + if ((mname = tline->text)) + { + /* if this token is a local macro, look in local context */ + if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) + ctx = get_ctx(mname, TRUE); + else + ctx = NULL; + if (!ctx) + head = smacros[hash(mname)]; + else + head = ctx->localmac; + /* + * We've hit an identifier. As in is_mmacro below, we first + * check whether the identifier is a single-line macro at + * all, then think about checking for parameters if + * necessary. + */ + for (m = head; m; m = m->next) + if (!mstrcmp(m->name, mname, m->casesense)) + break; + if (m) + { + mstart = tline; + params = NULL; + paramsize = NULL; + if (m->nparam == 0) + { + /* + * Simple case: the macro is parameterless. Discard the + * one token that the macro call took, and push the + * expansion back on the to-do stack. + */ + if (!m->expansion) + { + if (!strcmp("__FILE__", m->name)) + { + long num = 0; + nasm_src_get(&num, &(tline->text)); + nasm_quote(&(tline->text)); + tline->type = TOK_STRING; + continue; + } + if (!strcmp("__LINE__", m->name)) + { + nasm_free(tline->text); + make_tok_num(tline, yasm_intnum_create_int(nasm_src_get_linnum())); + continue; + } + tline = delete_Token(tline); + continue; + } + } + else + { + /* + * Complicated case: at least one macro with this name + * exists and takes parameters. We must find the + * parameters in the call, count them, find the SMacro + * that corresponds to that form of the macro call, and + * substitute for the parameters when we expand. What a + * pain. + */ + /*tline = tline->next; + skip_white_(tline);*/ + do { + t = tline->next; + while (tok_type_(t, TOK_SMAC_END)) + { + t->mac->in_progress = FALSE; + t->text = NULL; + t = tline->next = delete_Token(t); + } + tline = t; + } while (tok_type_(tline, TOK_WHITESPACE)); + if (!tok_is_(tline, "(")) + { + /* + * This macro wasn't called with parameters: ignore + * the call. (Behaviour borrowed from gnu cpp.) + */ + tline = mstart; + m = NULL; + } + else + { + int paren = 0; + int white = 0; + brackets = 0; + nparam = 0; + sparam = PARAM_DELTA; + params = nasm_malloc(sparam * sizeof(Token *)); + params[0] = tline->next; + paramsize = nasm_malloc(sparam * sizeof(int)); + paramsize[0] = 0; + while (TRUE) + { /* parameter loop */ + /* + * For some unusual expansions + * which concatenates function call + */ + t = tline->next; + while (tok_type_(t, TOK_SMAC_END)) + { + t->mac->in_progress = FALSE; + t->text = NULL; + t = tline->next = delete_Token(t); + } + tline = t; + + if (!tline) + { + error(ERR_NONFATAL, + "macro call expects terminating `)'"); + break; + } + if (tline->type == TOK_WHITESPACE + && brackets <= 0) + { + if (paramsize[nparam]) + white++; + else + params[nparam] = tline->next; + continue; /* parameter loop */ + } + if (tline->type == TOK_OTHER + && tline->text[1] == 0) + { + char ch = tline->text[0]; + if (ch == ',' && !paren && brackets <= 0) + { + if (++nparam >= sparam) + { + sparam += PARAM_DELTA; + params = nasm_realloc(params, + sparam * sizeof(Token *)); + paramsize = nasm_realloc(paramsize, + sparam * sizeof(int)); + } + params[nparam] = tline->next; + paramsize[nparam] = 0; + white = 0; + continue; /* parameter loop */ + } + if (ch == '{' && + (brackets > 0 || (brackets == 0 && + !paramsize[nparam]))) + { + if (!(brackets++)) + { + params[nparam] = tline->next; + continue; /* parameter loop */ + } + } + if (ch == '}' && brackets > 0) + if (--brackets == 0) + { + brackets = -1; + continue; /* parameter loop */ + } + if (ch == '(' && !brackets) + paren++; + if (ch == ')' && brackets <= 0) + if (--paren < 0) + break; + } + if (brackets < 0) + { + brackets = 0; + error(ERR_NONFATAL, "braces do not " + "enclose all of macro parameter"); + } + paramsize[nparam] += white + 1; + white = 0; + } /* parameter loop */ + nparam++; + while (m && (m->nparam != nparam || + mstrcmp(m->name, mname, + m->casesense))) + m = m->next; + if (!m) + error(ERR_WARNING | ERR_WARN_MNP, + "macro `%s' exists, " + "but not taking %d parameters", + mstart->text, nparam); + } + } + if (m && m->in_progress) + m = NULL; + if (!m) /* in progess or didn't find '(' or wrong nparam */ + { + /* + * Design question: should we handle !tline, which + * indicates missing ')' here, or expand those + * macros anyway, which requires the (t) test a few + * lines down? + */ + nasm_free(params); + nasm_free(paramsize); + tline = mstart; + } + else + { + /* + * Expand the macro: we are placed on the last token of the + * call, so that we can easily split the call from the + * following tokens. We also start by pushing an SMAC_END + * token for the cycle removal. + */ + t = tline; + if (t) + { + tline = t->next; + t->next = NULL; + } + tt = new_Token(tline, TOK_SMAC_END, NULL, 0); + tt->mac = m; + m->in_progress = TRUE; + tline = tt; + for (t = m->expansion; t; t = t->next) + { + if (t->type >= TOK_SMAC_PARAM) + { + Token *pcopy = tline, **ptail = &pcopy; + Token *ttt, *pt; + int i; + + ttt = params[t->type - TOK_SMAC_PARAM]; + for (i = paramsize[t->type - TOK_SMAC_PARAM]; + --i >= 0;) + { + pt = *ptail = + new_Token(tline, ttt->type, ttt->text, + 0); + ptail = &pt->next; + ttt = ttt->next; + } + tline = pcopy; + } + else + { + tt = new_Token(tline, t->type, t->text, 0); + tline = tt; + } + } + + /* + * Having done that, get rid of the macro call, and clean + * up the parameters. + */ + nasm_free(params); + nasm_free(paramsize); + free_tlist(mstart); + continue; /* main token loop */ + } + } + } + + if (tline->type == TOK_SMAC_END) + { + tline->mac->in_progress = FALSE; + tline = delete_Token(tline); + } + else + { + t = *tail = tline; + tline = tline->next; + t->mac = NULL; + t->next = NULL; + tail = &t->next; + } } /* @@ -3760,62 +3760,62 @@ expand_smacro(Token * tline) rescan = 0; while (t) { - while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID) - t = t->next; - if (!t || !t->next) - break; - if (t->next->type == TOK_ID || - t->next->type == TOK_PREPROC_ID || - t->next->type == TOK_NUMBER) - { - char *p = nasm_strcat(t->text, t->next->text); - nasm_free(t->text); - t->next = delete_Token(t->next); - t->text = p; - rescan = 1; - } - else if (t->next->type == TOK_WHITESPACE && t->next->next && - t->next->next->type == TOK_PREPROC_ID && - strcmp(t->next->next->text, "%+") == 0) - { - /* free the next whitespace, the %+ token and next whitespace */ - int i; - for (i = 1; i <= 3; i++) - { - if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE)) - break; - t->next = delete_Token(t->next); - } /* endfor */ - } - else - t = t->next; + while (t && t->type != TOK_ID && t->type != TOK_PREPROC_ID) + t = t->next; + if (!t || !t->next) + break; + if (t->next->type == TOK_ID || + t->next->type == TOK_PREPROC_ID || + t->next->type == TOK_NUMBER) + { + char *p = nasm_strcat(t->text, t->next->text); + nasm_free(t->text); + t->next = delete_Token(t->next); + t->text = p; + rescan = 1; + } + else if (t->next->type == TOK_WHITESPACE && t->next->next && + t->next->next->type == TOK_PREPROC_ID && + strcmp(t->next->next->text, "%+") == 0) + { + /* free the next whitespace, the %+ token and next whitespace */ + int i; + for (i = 1; i <= 3; i++) + { + if (!t->next || (i != 2 && t->next->type != TOK_WHITESPACE)) + break; + t->next = delete_Token(t->next); + } /* endfor */ + } + else + t = t->next; } /* If we concatenaded something, re-scan the line for macros */ if (rescan) { - tline = thead; - goto again; + tline = thead; + goto again; } if (org_tline) { - if (thead) - { - *org_tline = *thead; - /* since we just gave text to org_line, don't free it */ - thead->text = NULL; - delete_Token(thead); - } - else - { - /* the expression expanded to empty line; - we can't return NULL for some reasons - we just set the line to a single WHITESPACE token. */ - memset(org_tline, 0, sizeof(*org_tline)); - org_tline->text = NULL; - org_tline->type = TOK_WHITESPACE; - } - thead = org_tline; + if (thead) + { + *org_tline = *thead; + /* since we just gave text to org_line, don't free it */ + thead->text = NULL; + delete_Token(thead); + } + else + { + /* the expression expanded to empty line; + we can't return NULL for some reasons + we just set the line to a single WHITESPACE token. */ + memset(org_tline, 0, sizeof(*org_tline)); + org_tline->text = NULL; + org_tline->type = TOK_WHITESPACE; + } + thead = org_tline; } return thead; @@ -3828,12 +3828,12 @@ expand_smacro(Token * tline) * are more than one element forming the name, user wants a expansion, * otherwise it will be left as-is. Example: * - * %define %$abc cde + * %define %$abc cde * * the identifier %$abc will be left as-is so that the handler for %define * will suck it and define the corresponding value. Other case: * - * %define _%$abc cde + * %define _%$abc cde * * In this case user wants name to be expanded *before* %define starts * working, so we'll expand %$abc into something (if it has a value; @@ -3846,34 +3846,34 @@ expand_id(Token * tline) Token *cur, *oldnext = NULL; if (!tline || !tline->next) - return tline; + return tline; cur = tline; while (cur->next && - (cur->next->type == TOK_ID || - cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER)) - cur = cur->next; + (cur->next->type == TOK_ID || + cur->next->type == TOK_PREPROC_ID || cur->next->type == TOK_NUMBER)) + cur = cur->next; /* If identifier consists of just one token, don't expand */ if (cur == tline) - return tline; + return tline; if (cur) { - oldnext = cur->next; /* Detach the tail past identifier */ - cur->next = NULL; /* so that expand_smacro stops here */ + oldnext = cur->next; /* Detach the tail past identifier */ + cur->next = NULL; /* so that expand_smacro stops here */ } tline = expand_smacro(tline); if (cur) { - /* expand_smacro possibly changhed tline; re-scan for EOL */ - cur = tline; - while (cur && cur->next) - cur = cur->next; - if (cur) - cur->next = oldnext; + /* expand_smacro possibly changhed tline; re-scan for EOL */ + cur = tline; + while (cur && cur->next) + cur = cur->next; + if (cur) + cur->next = oldnext; } return tline; @@ -3903,10 +3903,10 @@ is_mmacro(Token * tline, Token *** params_array) * list if necessary to find the proper MMacro. */ for (m = head; m; m = m->next) - if (!mstrcmp(m->name, tline->text, m->casesense)) - break; + if (!mstrcmp(m->name, tline->text, m->casesense)) + break; if (!m) - return NULL; + return NULL; /* * OK, we have a potential macro. Count and demarcate the @@ -3920,62 +3920,62 @@ is_mmacro(Token * tline, Token *** params_array) */ while (m) { - if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) - { - /* - * This one is right. Just check if cycle removal - * prohibits us using it before we actually celebrate... - */ - if (m->in_progress) - { + if (m->nparam_min <= nparam && (m->plus || nparam <= m->nparam_max)) + { + /* + * This one is right. Just check if cycle removal + * prohibits us using it before we actually celebrate... + */ + if (m->in_progress) + { #if 0 - error(ERR_NONFATAL, - "self-reference in multi-line macro `%s'", m->name); + error(ERR_NONFATAL, + "self-reference in multi-line macro `%s'", m->name); #endif - nasm_free(params); - return NULL; - } - /* - * It's right, and we can use it. Add its default - * parameters to the end of our list if necessary. - */ - if (m->defaults && nparam < m->nparam_min + m->ndefs) - { - params = - nasm_realloc(params, - ((m->nparam_min + m->ndefs + 1) * sizeof(*params))); - while (nparam < m->nparam_min + m->ndefs) - { - params[nparam] = m->defaults[nparam - m->nparam_min]; - nparam++; - } - } - /* - * If we've gone over the maximum parameter count (and - * we're in Plus mode), ignore parameters beyond - * nparam_max. - */ - if (m->plus && nparam > m->nparam_max) - nparam = m->nparam_max; - /* - * Then terminate the parameter list, and leave. - */ - if (!params) - { /* need this special case */ - params = nasm_malloc(sizeof(*params)); - nparam = 0; - } - params[nparam] = NULL; - *params_array = params; - return m; - } - /* - * This one wasn't right: look for the next one with the - * same name. - */ - for (m = m->next; m; m = m->next) - if (!mstrcmp(m->name, tline->text, m->casesense)) - break; + nasm_free(params); + return NULL; + } + /* + * It's right, and we can use it. Add its default + * parameters to the end of our list if necessary. + */ + if (m->defaults && nparam < m->nparam_min + m->ndefs) + { + params = + nasm_realloc(params, + ((m->nparam_min + m->ndefs + 1) * sizeof(*params))); + while (nparam < m->nparam_min + m->ndefs) + { + params[nparam] = m->defaults[nparam - m->nparam_min]; + nparam++; + } + } + /* + * If we've gone over the maximum parameter count (and + * we're in Plus mode), ignore parameters beyond + * nparam_max. + */ + if (m->plus && nparam > m->nparam_max) + nparam = m->nparam_max; + /* + * Then terminate the parameter list, and leave. + */ + if (!params) + { /* need this special case */ + params = nasm_malloc(sizeof(*params)); + nparam = 0; + } + params[nparam] = NULL; + *params_array = params; + return m; + } + /* + * This one wasn't right: look for the next one with the + * same name. + */ + for (m = m->next; m; m = m->next) + if (!mstrcmp(m->name, tline->text, m->casesense)) + break; } /* @@ -3983,8 +3983,8 @@ is_mmacro(Token * tline, Token *** params_array) * parameters. Issue a warning, and fail to expand the macro. */ error(ERR_WARNING | ERR_WARN_MNP, - "macro `%s' exists, but not taking %d parameters", - tline->text, nparam); + "macro `%s' exists, but not taking %d parameters", + tline->text, nparam); nasm_free(params); return NULL; } @@ -4010,32 +4010,32 @@ expand_mmacro(Token * tline) skip_white_(t); /* if (!tok_type_(t, TOK_ID)) Lino 02/25/02 */ if (!tok_type_(t, TOK_ID) && !tok_type_(t, TOK_PREPROC_ID)) - return 0; + return 0; m = is_mmacro(t, ¶ms); if (!m) { - Token *last; - /* - * We have an id which isn't a macro call. We'll assume - * it might be a label; we'll also check to see if a - * colon follows it. Then, if there's another id after - * that lot, we'll check it again for macro-hood. - */ - label = last = t; - t = t->next; - if (tok_type_(t, TOK_WHITESPACE)) - last = t, t = t->next; - if (tok_is_(t, ":")) - { - dont_prepend = 1; - last = t, t = t->next; - if (tok_type_(t, TOK_WHITESPACE)) - last = t, t = t->next; - } - if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, ¶ms)) == NULL) - return 0; - last->next = NULL; - tline = t; + Token *last; + /* + * We have an id which isn't a macro call. We'll assume + * it might be a label; we'll also check to see if a + * colon follows it. Then, if there's another id after + * that lot, we'll check it again for macro-hood. + */ + label = last = t; + t = t->next; + if (tok_type_(t, TOK_WHITESPACE)) + last = t, t = t->next; + if (tok_is_(t, ":")) + { + dont_prepend = 1; + last = t, t = t->next; + if (tok_type_(t, TOK_WHITESPACE)) + last = t, t = t->next; + } + if (!tok_type_(t, TOK_ID) || (m = is_mmacro(t, ¶ms)) == NULL) + return 0; + last->next = NULL; + tline = t; } /* @@ -4044,31 +4044,31 @@ expand_mmacro(Token * tline) * present. */ for (nparam = 0; params[nparam]; nparam++) - ; + ; paramlen = nparam ? nasm_malloc(nparam * sizeof(*paramlen)) : NULL; for (i = 0; params[i]; i++) { - int brace = FALSE; - int comma = (!m->plus || i < nparam - 1); - - t = params[i]; - skip_white_(t); - if (tok_is_(t, "{")) - t = t->next, brace = TRUE, comma = FALSE; - params[i] = t; - paramlen[i] = 0; - while (t) - { - if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) - break; /* ... because we have hit a comma */ - if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ",")) - break; /* ... or a space then a comma */ - if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) - break; /* ... or a brace */ - t = t->next; - paramlen[i]++; - } + int brace = FALSE; + int comma = (!m->plus || i < nparam - 1); + + t = params[i]; + skip_white_(t); + if (tok_is_(t, "{")) + t = t->next, brace = TRUE, comma = FALSE; + params[i] = t; + paramlen[i] = 0; + while (t) + { + if (comma && t->type == TOK_OTHER && !strcmp(t->text, ",")) + break; /* ... because we have hit a comma */ + if (comma && t->type == TOK_WHITESPACE && tok_is_(t->next, ",")) + break; /* ... or a space then a comma */ + if (brace && t->type == TOK_OTHER && !strcmp(t->text, "}")) + break; /* ... or a brace */ + t = t->next; + paramlen[i]++; + } } /* @@ -4104,29 +4104,29 @@ expand_mmacro(Token * tline) for (l = m->expansion; l; l = l->next) { - Token **tail; - - ll = nasm_malloc(sizeof(Line)); - ll->finishes = NULL; - ll->next = istk->expansion; - istk->expansion = ll; - tail = &ll->first; - - for (t = l->first; t; t = t->next) - { - Token *x = t; - if (t->type == TOK_PREPROC_ID && - t->text[1] == '0' && t->text[2] == '0') - { - dont_prepend = -1; - x = label; - if (!x) - continue; - } - tt = *tail = new_Token(NULL, x->type, x->text, 0); - tail = &tt->next; - } - *tail = NULL; + Token **tail; + + ll = nasm_malloc(sizeof(Line)); + ll->finishes = NULL; + ll->next = istk->expansion; + istk->expansion = ll; + tail = &ll->first; + + for (t = l->first; t; t = t->next) + { + Token *x = t; + if (t->type == TOK_PREPROC_ID && + t->text[1] == '0' && t->text[2] == '0') + { + dont_prepend = -1; + x = label; + if (!x) + continue; + } + tt = *tail = new_Token(NULL, x->type, x->text, 0); + tail = &tt->next; + } + *tail = NULL; } /* @@ -4135,22 +4135,22 @@ expand_mmacro(Token * tline) */ if (label) { - if (dont_prepend < 0) - free_tlist(startline); - else - { - ll = nasm_malloc(sizeof(Line)); - ll->finishes = NULL; - ll->next = istk->expansion; - istk->expansion = ll; - ll->first = startline; - if (!dont_prepend) - { - while (label->next) - label = label->next; - label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); - } - } + if (dont_prepend < 0) + free_tlist(startline); + else + { + ll = nasm_malloc(sizeof(Line)); + ll->finishes = NULL; + ll->next = istk->expansion; + istk->expansion = ll; + ll->first = startline; + if (!dont_prepend) + { + while (label->next) + label = label->next; + label->next = tt = new_Token(NULL, TOK_OTHER, ":", 0); + } + } } list->uplevel(m->nolist ? LIST_MACRO_NOLIST : LIST_MACRO); @@ -4172,22 +4172,22 @@ error(int severity, const char *fmt, ...) /* If we're in a dead branch of IF or something like it, ignore the error */ if (istk && istk->conds && !emitting(istk->conds->state)) - return; + return; va_start(arg, fmt); vsprintf(buff, fmt, arg); va_end(arg); if (istk && istk->mstk && istk->mstk->name) - _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name, - istk->mstk->lineno, buff); + _error(severity | ERR_PASS1, "(%s:%d) %s", istk->mstk->name, + istk->mstk->lineno, buff); else - _error(severity | ERR_PASS1, "%s", buff); + _error(severity | ERR_PASS1, "%s", buff); } static void pp_reset(FILE *f, const char *file, int apass, efunc errfunc, evalfunc eval, - ListGen * listgen) + ListGen * listgen) { int h; @@ -4209,15 +4209,15 @@ pp_reset(FILE *f, const char *file, int apass, efunc errfunc, evalfunc eval, nested_rep_count = 0; for (h = 0; h < NHASH; h++) { - mmacros[h] = NULL; - smacros[h] = NULL; + mmacros[h] = NULL; + smacros[h] = NULL; } unique = 0; - if (tasm_compatible_mode) { - stdmacpos = stdmac; - } else { - stdmacpos = &stdmac[TASM_MACRO_COUNT]; - } + if (tasm_compatible_mode) { + stdmacpos = stdmac; + } else { + stdmacpos = &stdmac[TASM_MACRO_COUNT]; + } any_extrastdmac = (extrastdmac != NULL); list = listgen; evaluate = eval; @@ -4232,224 +4232,224 @@ pp_getline(void) while (1) { - /* - * Fetch a tokenised line, either from the macro-expansion - * buffer or from the input file. - */ - tline = NULL; - while (istk->expansion && istk->expansion->finishes) - { - Line *l = istk->expansion; - if (!l->finishes->name && l->finishes->in_progress > 1) - { - Line *ll; - - /* - * This is a macro-end marker for a macro with no - * name, which means it's not really a macro at all - * but a %rep block, and the `in_progress' field is - * more than 1, meaning that we still need to - * repeat. (1 means the natural last repetition; 0 - * means termination by %exitrep.) We have - * therefore expanded up to the %endrep, and must - * push the whole block on to the expansion buffer - * again. We don't bother to remove the macro-end - * marker: we'd only have to generate another one - * if we did. - */ - l->finishes->in_progress--; - for (l = l->finishes->expansion; l; l = l->next) - { - Token *t, *tt, **tail; - - ll = nasm_malloc(sizeof(Line)); - ll->next = istk->expansion; - ll->finishes = NULL; - ll->first = NULL; - tail = &ll->first; - - for (t = l->first; t; t = t->next) - { - if (t->text || t->type == TOK_WHITESPACE) - { - tt = *tail = new_Token(NULL, t->type, t->text, 0); - tail = &tt->next; - } - } - - istk->expansion = ll; - } - } - else - { - /* - * Check whether a `%rep' was started and not ended - * within this macro expansion. This can happen and - * should be detected. It's a fatal error because - * I'm too confused to work out how to recover - * sensibly from it. - */ - if (defining) - { - if (defining->name) - error(ERR_PANIC, "defining with name in expansion"); - else if (istk->mstk->name) - error(ERR_FATAL, "`%%rep' without `%%endrep' within" - " expansion of macro `%s'", istk->mstk->name); - } - - /* - * FIXME: investigate the relationship at this point between - * istk->mstk and l->finishes - */ - { - MMacro *m = istk->mstk; - istk->mstk = m->next_active; - if (m->name) - { - /* - * This was a real macro call, not a %rep, and - * therefore the parameter information needs to - * be freed. - */ - nasm_free(m->params); - free_tlist(m->iline); - nasm_free(m->paramlen); - l->finishes->in_progress = FALSE; - } - else - free_mmacro(m); - } - istk->expansion = l->next; - nasm_free(l); - list->downlevel(LIST_MACRO); - } - } - while (1) - { /* until we get a line we can use */ - - if (istk->expansion) - { /* from a macro expansion */ - char *p; - Line *l = istk->expansion; - if (istk->mstk) - istk->mstk->lineno++; - tline = l->first; - istk->expansion = l->next; - nasm_free(l); - p = detoken(tline, FALSE); - list->line(LIST_MACRO, p); - nasm_free(p); - break; - } - line = read_line(); - if (line) - { /* from the current input file */ - line = prepreproc(line); - tline = tokenise(line); - nasm_free(line); - break; - } - /* - * The current file has ended; work down the istk - */ - { - Include *i = istk; - if (i->fp != first_fp) - fclose(i->fp); - if (i->conds) - error(ERR_FATAL, "expected `%%endif' before end of file"); - /* only set line and file name if there's a next node */ - if (i->next) - { - nasm_src_set_linnum(i->lineno); - nasm_free(nasm_src_set_fname(nasm_strdup(i->fname))); - } - istk = i->next; - list->downlevel(LIST_INCLUDE); - nasm_free(i); - if (!istk) - return NULL; - if (istk->expansion && istk->expansion->finishes) - break; - } - } - - /* - * We must expand MMacro parameters and MMacro-local labels - * _before_ we plunge into directive processing, to cope - * with things like `%define something %1' such as STRUC - * uses. Unless we're _defining_ a MMacro, in which case - * those tokens should be left alone to go into the - * definition; and unless we're in a non-emitting - * condition, in which case we don't want to meddle with - * anything. - */ - if (!defining && !(istk->conds && !emitting(istk->conds->state))) - tline = expand_mmac_params(tline); - - /* - * Check the line to see if it's a preprocessor directive. - */ - if (do_directive(tline) == DIRECTIVE_FOUND) - { - continue; - } - else if (defining) - { - /* - * We're defining a multi-line macro. We emit nothing - * at all, and just - * shove the tokenised line on to the macro definition. - */ - Line *l = nasm_malloc(sizeof(Line)); - l->next = defining->expansion; - l->first = tline; - l->finishes = FALSE; - defining->expansion = l; - continue; - } - else if (istk->conds && !emitting(istk->conds->state)) - { - /* - * We're in a non-emitting branch of a condition block. - * Emit nothing at all, not even a blank line: when we - * emerge from the condition we'll give a line-number - * directive so we keep our place correctly. - */ - free_tlist(tline); - continue; - } - else if (istk->mstk && !istk->mstk->in_progress) - { - /* - * We're in a %rep block which has been terminated, so - * we're walking through to the %endrep without - * emitting anything. Emit nothing at all, not even a - * blank line: when we emerge from the %rep block we'll - * give a line-number directive so we keep our place - * correctly. - */ - free_tlist(tline); - continue; - } - else - { - tline = expand_smacro(tline); - if (!expand_mmacro(tline)) - { - /* - * De-tokenise the line again, and emit it. - */ - line = detoken(tline, TRUE); - free_tlist(tline); - break; - } - else - { - continue; /* expand_mmacro calls free_tlist */ - } - } + /* + * Fetch a tokenised line, either from the macro-expansion + * buffer or from the input file. + */ + tline = NULL; + while (istk->expansion && istk->expansion->finishes) + { + Line *l = istk->expansion; + if (!l->finishes->name && l->finishes->in_progress > 1) + { + Line *ll; + + /* + * This is a macro-end marker for a macro with no + * name, which means it's not really a macro at all + * but a %rep block, and the `in_progress' field is + * more than 1, meaning that we still need to + * repeat. (1 means the natural last repetition; 0 + * means termination by %exitrep.) We have + * therefore expanded up to the %endrep, and must + * push the whole block on to the expansion buffer + * again. We don't bother to remove the macro-end + * marker: we'd only have to generate another one + * if we did. + */ + l->finishes->in_progress--; + for (l = l->finishes->expansion; l; l = l->next) + { + Token *t, *tt, **tail; + + ll = nasm_malloc(sizeof(Line)); + ll->next = istk->expansion; + ll->finishes = NULL; + ll->first = NULL; + tail = &ll->first; + + for (t = l->first; t; t = t->next) + { + if (t->text || t->type == TOK_WHITESPACE) + { + tt = *tail = new_Token(NULL, t->type, t->text, 0); + tail = &tt->next; + } + } + + istk->expansion = ll; + } + } + else + { + /* + * Check whether a `%rep' was started and not ended + * within this macro expansion. This can happen and + * should be detected. It's a fatal error because + * I'm too confused to work out how to recover + * sensibly from it. + */ + if (defining) + { + if (defining->name) + error(ERR_PANIC, "defining with name in expansion"); + else if (istk->mstk->name) + error(ERR_FATAL, "`%%rep' without `%%endrep' within" + " expansion of macro `%s'", istk->mstk->name); + } + + /* + * FIXME: investigate the relationship at this point between + * istk->mstk and l->finishes + */ + { + MMacro *m = istk->mstk; + istk->mstk = m->next_active; + if (m->name) + { + /* + * This was a real macro call, not a %rep, and + * therefore the parameter information needs to + * be freed. + */ + nasm_free(m->params); + free_tlist(m->iline); + nasm_free(m->paramlen); + l->finishes->in_progress = FALSE; + } + else + free_mmacro(m); + } + istk->expansion = l->next; + nasm_free(l); + list->downlevel(LIST_MACRO); + } + } + while (1) + { /* until we get a line we can use */ + + if (istk->expansion) + { /* from a macro expansion */ + char *p; + Line *l = istk->expansion; + if (istk->mstk) + istk->mstk->lineno++; + tline = l->first; + istk->expansion = l->next; + nasm_free(l); + p = detoken(tline, FALSE); + list->line(LIST_MACRO, p); + nasm_free(p); + break; + } + line = read_line(); + if (line) + { /* from the current input file */ + line = prepreproc(line); + tline = tokenise(line); + nasm_free(line); + break; + } + /* + * The current file has ended; work down the istk + */ + { + Include *i = istk; + if (i->fp != first_fp) + fclose(i->fp); + if (i->conds) + error(ERR_FATAL, "expected `%%endif' before end of file"); + /* only set line and file name if there's a next node */ + if (i->next) + { + nasm_src_set_linnum(i->lineno); + nasm_free(nasm_src_set_fname(nasm_strdup(i->fname))); + } + istk = i->next; + list->downlevel(LIST_INCLUDE); + nasm_free(i); + if (!istk) + return NULL; + if (istk->expansion && istk->expansion->finishes) + break; + } + } + + /* + * We must expand MMacro parameters and MMacro-local labels + * _before_ we plunge into directive processing, to cope + * with things like `%define something %1' such as STRUC + * uses. Unless we're _defining_ a MMacro, in which case + * those tokens should be left alone to go into the + * definition; and unless we're in a non-emitting + * condition, in which case we don't want to meddle with + * anything. + */ + if (!defining && !(istk->conds && !emitting(istk->conds->state))) + tline = expand_mmac_params(tline); + + /* + * Check the line to see if it's a preprocessor directive. + */ + if (do_directive(tline) == DIRECTIVE_FOUND) + { + continue; + } + else if (defining) + { + /* + * We're defining a multi-line macro. We emit nothing + * at all, and just + * shove the tokenised line on to the macro definition. + */ + Line *l = nasm_malloc(sizeof(Line)); + l->next = defining->expansion; + l->first = tline; + l->finishes = FALSE; + defining->expansion = l; + continue; + } + else if (istk->conds && !emitting(istk->conds->state)) + { + /* + * We're in a non-emitting branch of a condition block. + * Emit nothing at all, not even a blank line: when we + * emerge from the condition we'll give a line-number + * directive so we keep our place correctly. + */ + free_tlist(tline); + continue; + } + else if (istk->mstk && !istk->mstk->in_progress) + { + /* + * We're in a %rep block which has been terminated, so + * we're walking through to the %endrep without + * emitting anything. Emit nothing at all, not even a + * blank line: when we emerge from the %rep block we'll + * give a line-number directive so we keep our place + * correctly. + */ + free_tlist(tline); + continue; + } + else + { + tline = expand_smacro(tline); + if (!expand_mmacro(tline)) + { + /* + * De-tokenise the line again, and emit it. + */ + line = detoken(tline, TRUE); + free_tlist(tline); + break; + } + else + { + continue; /* expand_mmacro calls free_tlist */ + } + } } return line; @@ -4462,46 +4462,46 @@ pp_cleanup(int pass_) if (defining) { - error(ERR_NONFATAL, "end of file while still defining macro `%s'", - defining->name); - free_mmacro(defining); + error(ERR_NONFATAL, "end of file while still defining macro `%s'", + defining->name); + free_mmacro(defining); } while (cstk) - ctx_pop(); + ctx_pop(); for (h = 0; h < NHASH; h++) { - while (mmacros[h]) - { - MMacro *m = mmacros[h]; - mmacros[h] = mmacros[h]->next; - free_mmacro(m); - } - while (smacros[h]) - { - SMacro *s = smacros[h]; - smacros[h] = smacros[h]->next; - nasm_free(s->name); - free_tlist(s->expansion); - nasm_free(s); - } + while (mmacros[h]) + { + MMacro *m = mmacros[h]; + mmacros[h] = mmacros[h]->next; + free_mmacro(m); + } + while (smacros[h]) + { + SMacro *s = smacros[h]; + smacros[h] = smacros[h]->next; + nasm_free(s->name); + free_tlist(s->expansion); + nasm_free(s); + } } while (istk) { - Include *i = istk; - istk = istk->next; - if (i->fp != first_fp) - fclose(i->fp); - nasm_free(i->fname); - nasm_free(i); + Include *i = istk; + istk = istk->next; + if (i->fp != first_fp) + fclose(i->fp); + nasm_free(i->fname); + nasm_free(i); } while (cstk) - ctx_pop(); + ctx_pop(); if (pass_ == 0) - { - free_llist(builtindef); - free_llist(predef); - delete_Blocks(); - } + { + free_llist(builtindef); + free_llist(predef); + delete_Blocks(); + } } void @@ -4532,10 +4532,10 @@ pp_pre_define(char *definition) space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); def = new_Token(space, TOK_PREPROC_ID, "%define", 0); if (equals) - *equals = ' '; + *equals = ' '; space->next = tokenise(definition); if (equals) - *equals = '='; + *equals = '='; l = nasm_malloc(sizeof(Line)); l->next = predef; @@ -4572,10 +4572,10 @@ pp_builtin_define(char *definition) space = new_Token(NULL, TOK_WHITESPACE, NULL, 0); def = new_Token(space, TOK_PREPROC_ID, "%define", 0); if (equals) - *equals = ' '; + *equals = ' '; space->next = tokenise(definition); if (equals) - *equals = '='; + *equals = '='; l = nasm_malloc(sizeof(Line)); l->next = builtindef; diff --git a/modules/preprocs/nasm/nasm-preproc.c b/modules/preprocs/nasm/nasm-preproc.c index 5702a1d1..d3319156 100644 --- a/modules/preprocs/nasm/nasm-preproc.c +++ b/modules/preprocs/nasm/nasm-preproc.c @@ -106,31 +106,31 @@ nasm_efunc(int severity, const char *fmt, ...) va_start(va, fmt); switch (severity & ERR_MASK) { - case ERR_WARNING: - yasm_warn_set_va(YASM_WARN_PREPROC, fmt, va); - break; - case ERR_NONFATAL: - yasm_error_set_va(YASM_ERROR_GENERAL, fmt, va); - break; - case ERR_FATAL: - yasm_fatal(fmt, va); - /*@notreached@*/ - break; - case ERR_PANIC: - yasm_internal_error(fmt); /* FIXME */ - break; - case ERR_DEBUG: - break; + case ERR_WARNING: + yasm_warn_set_va(YASM_WARN_PREPROC, fmt, va); + break; + case ERR_NONFATAL: + yasm_error_set_va(YASM_ERROR_GENERAL, fmt, va); + break; + case ERR_FATAL: + yasm_fatal(fmt, va); + /*@notreached@*/ + break; + case ERR_PANIC: + yasm_internal_error(fmt); /* FIXME */ + break; + case ERR_DEBUG: + break; } va_end(va); yasm_errwarn_propagate(cur_errwarns, - yasm_linemap_poke(cur_lm, nasm_src_get_fname(), - (unsigned long)nasm_src_get_linnum())); + yasm_linemap_poke(cur_lm, nasm_src_get_fname(), + (unsigned long)nasm_src_get_linnum())); } static yasm_preproc * nasm_preproc_create(FILE *f, const char *in_filename, yasm_linemap *lm, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_preproc_nasm *preproc_nasm = yasm_xmalloc(sizeof(yasm_preproc_nasm)); @@ -156,10 +156,10 @@ nasm_preproc_destroy(yasm_preproc *preproc) yasm_preproc_nasm *preproc_nasm = (yasm_preproc_nasm *)preproc; nasmpp.cleanup(0); if (preproc_nasm->file_name) - yasm_xfree(preproc_nasm->file_name); + yasm_xfree(preproc_nasm->file_name); yasm_xfree(preproc); if (preproc_deps) - yasm_xfree(preproc_deps); + yasm_xfree(preproc_deps); } static size_t @@ -171,30 +171,30 @@ nasm_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size) int altline; if (!preproc_nasm->line) { - preproc_nasm->line = nasmpp.getline(); - if (!preproc_nasm->line) - return 0; - preproc_nasm->linepos = preproc_nasm->line; - preproc_nasm->lineleft = strlen(preproc_nasm->line) + 1; - preproc_nasm->line[preproc_nasm->lineleft-1] = '\n'; - - altline = nasm_src_get(&linnum, &preproc_nasm->file_name); - if (altline) { - if (altline == 1 && preproc_nasm->lineinc == 1) { - *buf++ = '\n'; - max_size--; - tot++; - } else { - preproc_nasm->lineinc = - (altline != -1 || preproc_nasm->lineinc != 1); - n = sprintf(buf, "%%line %ld+%d %s\n", linnum, - preproc_nasm->lineinc, preproc_nasm->file_name); - buf += n; - max_size -= n; - tot += n; - } - preproc_nasm->prior_linnum = linnum; - } + preproc_nasm->line = nasmpp.getline(); + if (!preproc_nasm->line) + return 0; + preproc_nasm->linepos = preproc_nasm->line; + preproc_nasm->lineleft = strlen(preproc_nasm->line) + 1; + preproc_nasm->line[preproc_nasm->lineleft-1] = '\n'; + + altline = nasm_src_get(&linnum, &preproc_nasm->file_name); + if (altline) { + if (altline == 1 && preproc_nasm->lineinc == 1) { + *buf++ = '\n'; + max_size--; + tot++; + } else { + preproc_nasm->lineinc = + (altline != -1 || preproc_nasm->lineinc != 1); + n = sprintf(buf, "%%line %ld+%d %s\n", linnum, + preproc_nasm->lineinc, preproc_nasm->file_name); + buf += n; + max_size -= n; + tot += n; + } + preproc_nasm->prior_linnum = linnum; + } } n = preproc_nasm->lineleftlineleft:max_size; @@ -202,11 +202,11 @@ nasm_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size) tot += n; if (n == preproc_nasm->lineleft) { - yasm_xfree(preproc_nasm->line); - preproc_nasm->line = NULL; + yasm_xfree(preproc_nasm->line); + preproc_nasm->line = NULL; } else { - preproc_nasm->lineleft -= n; - preproc_nasm->linepos += n; + preproc_nasm->lineleft -= n; + preproc_nasm->linepos += n; } return tot; @@ -219,7 +219,7 @@ nasm_preproc_add_dep(char *name) /* If not processing dependencies, simply return */ if (!preproc_deps) - return; + return; /* Save in preproc_deps */ dep = yasm_xmalloc(sizeof(preproc_dep)); @@ -229,40 +229,40 @@ nasm_preproc_add_dep(char *name) static size_t nasm_preproc_get_included_file(yasm_preproc *preproc, /*@out@*/ char *buf, - size_t max_size) + size_t max_size) { if (!preproc_deps) { - preproc_deps = yasm_xmalloc(sizeof(struct preproc_dep_head)); - STAILQ_INIT(preproc_deps); + preproc_deps = yasm_xmalloc(sizeof(struct preproc_dep_head)); + STAILQ_INIT(preproc_deps); } for (;;) { - char *line; - - /* Pull first dep out of preproc_deps and return it if there is one */ - if (!STAILQ_EMPTY(preproc_deps)) { - char *name; - preproc_dep *dep = STAILQ_FIRST(preproc_deps); - STAILQ_REMOVE_HEAD(preproc_deps, link); - name = dep->name; - yasm_xfree(dep); - strncpy(buf, name, max_size); - buf[max_size-1] = '\0'; - yasm_xfree(name); - return strlen(buf); - } - - /* No more preprocessing to do */ - if (done_dep_preproc) { - return 0; - } - - /* Preprocess some more, throwing away the result */ - line = nasmpp.getline(); - if (line) - yasm_xfree(line); - else - done_dep_preproc = 1; + char *line; + + /* Pull first dep out of preproc_deps and return it if there is one */ + if (!STAILQ_EMPTY(preproc_deps)) { + char *name; + preproc_dep *dep = STAILQ_FIRST(preproc_deps); + STAILQ_REMOVE_HEAD(preproc_deps, link); + name = dep->name; + yasm_xfree(dep); + strncpy(buf, name, max_size); + buf[max_size-1] = '\0'; + yasm_xfree(name); + return strlen(buf); + } + + /* No more preprocessing to do */ + if (done_dep_preproc) { + return 0; + } + + /* Preprocess some more, throwing away the result */ + line = nasmpp.getline(); + if (line) + yasm_xfree(line); + else + done_dep_preproc = 1; } } diff --git a/modules/preprocs/nasm/nasm.h b/modules/preprocs/nasm/nasm.h index 9a83543a..792877b0 100644 --- a/modules/preprocs/nasm/nasm.h +++ b/modules/preprocs/nasm/nasm.h @@ -15,7 +15,7 @@ #endif #ifndef FALSE -#define FALSE 0 /* comes in handy */ +#define FALSE 0 /* comes in handy */ #endif #ifndef TRUE #define TRUE 1 @@ -51,31 +51,31 @@ typedef void (*efunc) (int severity, const char *fmt, ...); * argument to an efunc. */ -#define ERR_DEBUG 0x00000008 /* put out debugging message */ -#define ERR_WARNING 0x00000000 /* warn only: no further action */ -#define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */ -#define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */ -#define ERR_PANIC 0x00000003 /* internal error: panic instantly - * and dump core for reference */ -#define ERR_MASK 0x0000000F /* mask off the above codes */ -#define ERR_NOFILE 0x00000010 /* don't give source file name/line */ -#define ERR_USAGE 0x00000020 /* print a usage message */ -#define ERR_PASS1 0x00000040 /* only print this error on pass one */ +#define ERR_DEBUG 0x00000008 /* put out debugging message */ +#define ERR_WARNING 0x00000000 /* warn only: no further action */ +#define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */ +#define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */ +#define ERR_PANIC 0x00000003 /* internal error: panic instantly + * and dump core for reference */ +#define ERR_MASK 0x0000000F /* mask off the above codes */ +#define ERR_NOFILE 0x00000010 /* don't give source file name/line */ +#define ERR_USAGE 0x00000020 /* print a usage message */ +#define ERR_PASS1 0x00000040 /* only print this error on pass one */ /* * These codes define specific types of suppressible warning. */ -#define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */ -#define ERR_WARN_SHR 8 /* how far to shift right */ +#define ERR_WARN_MASK 0x0000FF00 /* the mask for this feature */ +#define ERR_WARN_SHR 8 /* how far to shift right */ -#define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */ -#define ERR_WARN_MSR 0x00000200 /* macro self-reference */ -#define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and - * alone on line) */ -#define ERR_WARN_NOV 0x00000400 /* numeric overflow */ -#define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */ -#define ERR_WARN_MAX 5 /* the highest numbered one */ +#define ERR_WARN_MNP 0x00000100 /* macro-num-parameters warning */ +#define ERR_WARN_MSR 0x00000200 /* macro self-reference */ +#define ERR_WARN_OL 0x00000300 /* orphan label (no colon, and + * alone on line) */ +#define ERR_WARN_NOV 0x00000400 /* numeric overflow */ +#define ERR_WARN_GNUELF 0x00000500 /* using GNU ELF extensions */ +#define ERR_WARN_MAX 5 /* the highest numbered one */ /* * ----------------------- @@ -162,21 +162,21 @@ typedef int (*scanner) (void *private_data, struct tokenval *tv); * Token types returned by the scanner, in addition to ordinary * ASCII character values, and zero for end-of-string. */ -enum { /* token types, other than chars */ - TOKEN_INVALID = -1, /* a placeholder value */ - TOKEN_EOS = 0, /* end of string */ +enum { /* token types, other than chars */ + TOKEN_INVALID = -1, /* a placeholder value */ + TOKEN_EOS = 0, /* end of string */ TOKEN_EQ = '=', TOKEN_GT = '>', TOKEN_LT = '<', /* aliases */ TOKEN_ID = 256, TOKEN_NUM, TOKEN_REG, TOKEN_INSN, /* major token types */ - TOKEN_ERRNUM, /* numeric constant with error in */ - TOKEN_HERE, TOKEN_BASE, /* $ and $$ */ - TOKEN_SPECIAL, /* BYTE, WORD, DWORD, FAR, NEAR, etc */ - TOKEN_PREFIX, /* A32, O16, LOCK, REPNZ, TIMES, etc */ - TOKEN_SHL, TOKEN_SHR, /* << and >> */ - TOKEN_SDIV, TOKEN_SMOD, /* // and %% */ + TOKEN_ERRNUM, /* numeric constant with error in */ + TOKEN_HERE, TOKEN_BASE, /* $ and $$ */ + TOKEN_SPECIAL, /* BYTE, WORD, DWORD, FAR, NEAR, etc */ + TOKEN_PREFIX, /* A32, O16, LOCK, REPNZ, TIMES, etc */ + TOKEN_SHL, TOKEN_SHR, /* << and >> */ + TOKEN_SDIV, TOKEN_SMOD, /* // and %% */ TOKEN_GE, TOKEN_LE, TOKEN_NE, /* >=, <= and <> (!= is same as <>) */ TOKEN_DBL_AND, TOKEN_DBL_OR, TOKEN_DBL_XOR, /* &&, || and ^^ */ - TOKEN_SEG, TOKEN_WRT, /* SEG and WRT */ - TOKEN_FLOAT /* floating-point constant */ + TOKEN_SEG, TOKEN_WRT, /* SEG and WRT */ + TOKEN_FLOAT /* floating-point constant */ }; /* @@ -199,7 +199,7 @@ enum { /* token types, other than chars */ */ #define CRITICAL 0x100 typedef yasm_expr *(*evalfunc) (scanner sc, void *scprivate, struct tokenval *tv, - int critical, efunc error); + int critical, efunc error); /* * Preprocessors ought to look like this: diff --git a/modules/preprocs/nasm/nasmlib.c b/modules/preprocs/nasm/nasmlib.c index 268aa287..2ced9117 100644 --- a/modules/preprocs/nasm/nasmlib.c +++ b/modules/preprocs/nasm/nasmlib.c @@ -1,4 +1,4 @@ -/* nasmlib.c library routines for the Netwide Assembler +/* nasmlib.c library routines for the Netwide Assembler * * The Netwide Assembler is copyright (C) 1996 Simon Tatham and * Julian Hall. All rights reserved. The software is @@ -12,7 +12,7 @@ #include "nasm.h" #include "nasmlib.h" -/*#include "insns.h"*/ /* For MAX_KEYWORD */ +/*#include "insns.h"*/ /* For MAX_KEYWORD */ #define lib_isnumchar(c) ( isalnum(c) || (c) == '$') #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0') @@ -28,7 +28,7 @@ yasm_intnum *nasm_readnum (char *str, int *error) *error = FALSE; - while (isspace(*r)) r++; /* find start of number */ + while (isspace(*r)) r++; /* find start of number */ /* * If the number came from make_tok_num (as a result of an %assign), it @@ -36,8 +36,8 @@ yasm_intnum *nasm_readnum (char *str, int *error) */ if (*r == '-') { - r++; - sign = 1; + r++; + sign = 1; } q = r; @@ -50,17 +50,17 @@ yasm_intnum *nasm_readnum (char *str, int *error) * Otherwise, it's ordinary decimal. */ if (*r=='0' && (r[1]=='x' || r[1]=='X')) - radix = 16, r += 2; + radix = 16, r += 2; else if (*r=='$') - radix = 16, r++; + radix = 16, r++; else if (q[-1]=='H' || q[-1]=='h') - radix = 16 , q--; + radix = 16 , q--; else if (q[-1]=='Q' || q[-1]=='q' || q[-1]=='O' || q[-1]=='o') - radix = 8 , q--; + radix = 8 , q--; else if (q[-1]=='B' || q[-1]=='b') - radix = 2 , q--; + radix = 2 , q--; else - radix = 10; + radix = 10; /* * If this number has been found for us by something other than @@ -69,46 +69,46 @@ yasm_intnum *nasm_readnum (char *str, int *error) * now. */ if (r >= q) { - *error = TRUE; - return yasm_intnum_create_uint(0); + *error = TRUE; + return yasm_intnum_create_uint(0); } /* Check for valid number of that radix */ p = r; while (*p && p < q) { - if (*p<'0' || (*p>'9' && *p<'A') || (digit = numvalue(*p)) >= radix) - { - *error = TRUE; - return yasm_intnum_create_uint(0); - } - p++; + if (*p<'0' || (*p>'9' && *p<'A') || (digit = numvalue(*p)) >= radix) + { + *error = TRUE; + return yasm_intnum_create_uint(0); + } + p++; } /* Use intnum to actually do the conversion */ save = *q; *q = '\0'; switch (radix) { - case 2: - intn = yasm_intnum_create_bin(r); - break; - case 8: - intn = yasm_intnum_create_oct(r); - break; - case 10: - intn = yasm_intnum_create_dec(r); - break; - case 16: - intn = yasm_intnum_create_hex(r); - break; - default: - *error = TRUE; - intn = yasm_intnum_create_uint(0); - break; + case 2: + intn = yasm_intnum_create_bin(r); + break; + case 8: + intn = yasm_intnum_create_oct(r); + break; + case 10: + intn = yasm_intnum_create_dec(r); + break; + case 16: + intn = yasm_intnum_create_hex(r); + break; + default: + *error = TRUE; + intn = yasm_intnum_create_uint(0); + break; } *q = save; if (sign) - yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); + yasm_intnum_calc(intn, YASM_EXPR_NEG, NULL); return intn; } @@ -158,16 +158,16 @@ int nasm_src_get(long *xline, char **xname) { if (!file_name || !*xname || strcmp(*xname, file_name)) { - nasm_free(*xname); - *xname = file_name ? nasm_strdup(file_name) : NULL; - *xline = line_number; - return -2; + nasm_free(*xname); + *xname = file_name ? nasm_strdup(file_name) : NULL; + *xline = line_number; + return -2; } if (*xline != line_number) { - long tmp = line_number - *xline; - *xline = line_number; - return tmp; + long tmp = line_number - *xline; + *xline = line_number; + return tmp; } return 0; } @@ -178,10 +178,10 @@ void nasm_quote(char **str) char q=(*str)[0]; char *p; if (ln>1 && (*str)[ln-1]==q && (q=='"' || q=='\'')) - return; + return; q = '"'; if (strchr(*str,q)) - q = '\''; + q = '\''; p = nasm_malloc(ln+3); strcpy(p+1, *str); nasm_free(*str); diff --git a/modules/preprocs/nasm/nasmlib.h b/modules/preprocs/nasm/nasmlib.h index a14f64b4..c26c0668 100644 --- a/modules/preprocs/nasm/nasmlib.h +++ b/modules/preprocs/nasm/nasmlib.h @@ -1,4 +1,4 @@ -/* nasmlib.h header file for nasmlib.c +/* nasmlib.h header file for nasmlib.c * * The Netwide Assembler is copyright (C) 1996 Simon Tatham and * Julian Hall. All rights reserved. The software is diff --git a/modules/preprocs/raw/raw-preproc.c b/modules/preprocs/raw/raw-preproc.c index fa318caa..b87673c8 100644 --- a/modules/preprocs/raw/raw-preproc.c +++ b/modules/preprocs/raw/raw-preproc.c @@ -46,7 +46,7 @@ int isatty(int); static yasm_preproc * raw_preproc_create(FILE *f, const char *in_filename, yasm_linemap *lm, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_preproc_raw *preproc_raw = yasm_xmalloc(sizeof(yasm_preproc_raw)); @@ -75,21 +75,21 @@ raw_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size) size_t n; if (preproc_raw->is_interactive) { - for (n = 0; n < max_size && (c = getc(preproc_raw->in)) != EOF && - c != '\n'; n++) - buf[n] = (char)c; - if (c == '\n') - buf[n++] = (char)c; - if (c == EOF && ferror(preproc_raw->in)) { - yasm_error_set(YASM_ERROR_IO, N_("error when reading from file")); - yasm_errwarn_propagate(preproc_raw->errwarns, - yasm_linemap_get_current(preproc_raw->cur_lm)); - } + for (n = 0; n < max_size && (c = getc(preproc_raw->in)) != EOF && + c != '\n'; n++) + buf[n] = (char)c; + if (c == '\n') + buf[n++] = (char)c; + if (c == EOF && ferror(preproc_raw->in)) { + yasm_error_set(YASM_ERROR_IO, N_("error when reading from file")); + yasm_errwarn_propagate(preproc_raw->errwarns, + yasm_linemap_get_current(preproc_raw->cur_lm)); + } } else if (((n = fread(buf, 1, max_size, preproc_raw->in)) == 0) && - ferror(preproc_raw->in)) { - yasm_error_set(YASM_ERROR_IO, N_("error when reading from file")); - yasm_errwarn_propagate(preproc_raw->errwarns, - yasm_linemap_get_current(preproc_raw->cur_lm)); + ferror(preproc_raw->in)) { + yasm_error_set(YASM_ERROR_IO, N_("error when reading from file")); + yasm_errwarn_propagate(preproc_raw->errwarns, + yasm_linemap_get_current(preproc_raw->cur_lm)); } return n; @@ -97,7 +97,7 @@ raw_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size) static size_t raw_preproc_get_included_file(yasm_preproc *preproc, char *buf, - size_t max_size) + size_t max_size) { /* no included files */ return 0; diff --git a/modules/preprocs/yapp/yapp-preproc.c b/modules/preprocs/yapp/yapp-preproc.c index 257de213..4f7767ac 100644 --- a/modules/preprocs/yapp/yapp-preproc.c +++ b/modules/preprocs/yapp/yapp-preproc.c @@ -36,7 +36,7 @@ #define ydebug(x) /* printf x */ typedef struct yasm_preproc_yapp { - yasm_preproc_base preproc; /* base structure */ + yasm_preproc_base preproc; /* base structure */ } yasm_preproc_yapp; yasm_preproc_module yasm_yapp_LTX_preproc; @@ -72,8 +72,8 @@ typedef struct YAPP_Macro_s { struct source_head macro_head; struct source_head param_head; enum { - YAPP_MACRO = 0, - YAPP_DEFINE + YAPP_MACRO = 0, + YAPP_DEFINE } type; int args; int fillargs; @@ -103,9 +103,9 @@ yapp_define_param_get (HAMT *hamt, const char *key); static void replay_saved_tokens(char *ident, - struct source_head *from_head, - struct source_head *to_head, - source **to_tail); + struct source_head *from_head, + struct source_head *to_head, + source **to_tail); YAPP_Macro * yapp_macro_insert (char *name, int argc, int fillargs) @@ -143,22 +143,22 @@ yapp_define_insert (char *name, int argc, int fillargs) ym = yapp_macro_get(name); if (ym) { - if ((argc >= 0 && ym->args < 0) - || (argc < 0 && ym->args >= 0)) - { - yasm_warn_set(YASM_WARN_PREPROC, N_("Attempted %%define both with and without parameters")); - return NULL; - } + if ((argc >= 0 && ym->args < 0) + || (argc < 0 && ym->args >= 0)) + { + yasm_warn_set(YASM_WARN_PREPROC, N_("Attempted %%define both with and without parameters")); + return NULL; + } } else if (argc >= 0) { - /* insert placeholder for paramlisted defines */ - ym = yasm_xmalloc(sizeof(YAPP_Macro)); - ym->type = YAPP_DEFINE; - ym->args = argc; - ym->fillargs = fillargs; - ym->expanding = 0; - HAMT_insert(macro_table, name, (void *)ym, &zero, (void (*)(void *))yapp_macro_error_exists); + /* insert placeholder for paramlisted defines */ + ym = yasm_xmalloc(sizeof(YAPP_Macro)); + ym->type = YAPP_DEFINE; + ym->args = argc; + ym->fillargs = fillargs; + ym->expanding = 0; + HAMT_insert(macro_table, name, (void *)ym, &zero, (void (*)(void *))yapp_macro_error_exists); } /* now for the real one */ @@ -169,8 +169,8 @@ yapp_define_insert (char *name, int argc, int fillargs) ym->expanding = 0; if (argc>=0) { - mungename = yasm_xmalloc(strlen(name)+8); - sprintf(mungename, "%s(%d)", name, argc); + mungename = yasm_xmalloc(strlen(name)+8); + sprintf(mungename, "%s(%d)", name, argc); } ydebug(("YAPP: +Inserting macro %s\n", mungename)); @@ -193,9 +193,9 @@ void yapp_macro_delete (YAPP_Macro *ym) { while (!SLIST_EMPTY(&ym->macro_head)) { - source *s = SLIST_FIRST(&ym->macro_head); - yasm_xfree(s); - SLIST_REMOVE_HEAD(&ym->macro_head, next); + source *s = SLIST_FIRST(&ym->macro_head); + yasm_xfree(s); + SLIST_REMOVE_HEAD(&ym->macro_head, next); } yasm_xfree(ym); } @@ -234,16 +234,16 @@ copy_token(YAPP_Token *tok, struct source_head *to_head, source **to_tail); void expand_macro(char *name, - struct source_head *from_head, - struct source_head *to_head, - source **to_tail); + struct source_head *from_head, + struct source_head *to_head, + source **to_tail); void expand_token_list(struct source_head *paramexp, struct source_head *to_head, source **to_tail); static yasm_preproc * yapp_preproc_create(FILE *f, const char *in_filename, yasm_linemap *lm, - yasm_errwarns *errwarns) + yasm_errwarns *errwarns) { yasm_preproc_yapp *preproc_yapp = yasm_xmalloc(sizeof(yasm_preproc_yapp)); @@ -292,15 +292,15 @@ push_if(int val) switch (current_output) { - case YAPP_OUTPUT: - current_output = val ? YAPP_OUTPUT : YAPP_NO_OUTPUT; - break; - - case YAPP_NO_OUTPUT: - case YAPP_OLD_OUTPUT: - case YAPP_BLOCKED_OUTPUT: - current_output = YAPP_BLOCKED_OUTPUT; - break; + case YAPP_OUTPUT: + current_output = val ? YAPP_OUTPUT : YAPP_NO_OUTPUT; + break; + + case YAPP_NO_OUTPUT: + case YAPP_OLD_OUTPUT: + case YAPP_BLOCKED_OUTPUT: + current_output = YAPP_BLOCKED_OUTPUT; + break; } if (current_output != YAPP_OUTPUT) set_inhibit(); @@ -315,20 +315,20 @@ push_else(int val) { switch (current_output) { - /* if it was NO, turn to output IFF val is true */ - case YAPP_NO_OUTPUT: - current_output = val ? YAPP_OUTPUT : YAPP_NO_OUTPUT; - break; - - /* if it was yes, make it OLD */ - case YAPP_OUTPUT: - current_output = YAPP_OLD_OUTPUT; - break; - - /* leave OLD as OLD, BLOCKED as BLOCKED */ - case YAPP_OLD_OUTPUT: - case YAPP_BLOCKED_OUTPUT: - break; + /* if it was NO, turn to output IFF val is true */ + case YAPP_NO_OUTPUT: + current_output = val ? YAPP_OUTPUT : YAPP_NO_OUTPUT; + break; + + /* if it was yes, make it OLD */ + case YAPP_OUTPUT: + current_output = YAPP_OLD_OUTPUT; + break; + + /* leave OLD as OLD, BLOCKED as BLOCKED */ + case YAPP_OLD_OUTPUT: + case YAPP_BLOCKED_OUTPUT: + break; } if (current_output != YAPP_OUTPUT) set_inhibit(); } @@ -364,82 +364,82 @@ append_processed_token(source *tok, struct source_head *to_head, source **to_tai { ydebug(("YAPP: appended token \"%s\"\n", tok->token.str)); if (*to_tail) { - SLIST_INSERT_AFTER(*to_tail, tok, next); + SLIST_INSERT_AFTER(*to_tail, tok, next); } else { - SLIST_INSERT_HEAD(to_head, tok, next); + SLIST_INSERT_HEAD(to_head, tok, next); } *to_tail = tok; if (to_head == &source_head) - saved_length += strlen(tok->token.str); + saved_length += strlen(tok->token.str); } void append_token(int token, struct source_head *to_head, source **to_tail) { if (current_output != YAPP_OUTPUT) { - ydebug(("YAPP: append_token while not YAPP_OUTPUT\n")); - return; + ydebug(("YAPP: append_token while not YAPP_OUTPUT\n")); + return; } /* attempt to condense LINES together or newlines onto LINES */ if ((*to_tail) && (*to_tail)->token.type == LINE - && (token == '\n' || token == LINE)) + && (token == '\n' || token == LINE)) { - yasm_xfree ((*to_tail)->token.str); - (*to_tail)->token.str = yasm_xmalloc(23+strlen(yapp_preproc_current_file)); - sprintf((*to_tail)->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); + yasm_xfree ((*to_tail)->token.str); + (*to_tail)->token.str = yasm_xmalloc(23+strlen(yapp_preproc_current_file)); + sprintf((*to_tail)->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); } else { - src = yasm_xmalloc(sizeof(source)); - src->token.type = token; - switch (token) - { - case INTNUM: - src->token.str = yasm__xstrdup(yapp_preproc_lval.int_str_val.str); - src->token.val.int_val = yapp_preproc_lval.int_str_val.val; - break; - - case FLTNUM: - src->token.str = yasm__xstrdup(yapp_preproc_lval.double_str_val.str); - src->token.val.double_val = yapp_preproc_lval.double_str_val.val; - break; - - case STRING: - case WHITESPACE: - src->token.str = yasm__xstrdup(yapp_preproc_lval.str_val); - break; - - case IDENT: - src->token.str = yasm__xstrdup(yapp_preproc_lval.str_val); - break; - - case '+': case '-': case '*': case '/': case '%': case ',': case '\n': - case '[': case ']': case '(': case ')': - src->token.str = yasm_xmalloc(2); - src->token.str[0] = (char)token; - src->token.str[1] = '\0'; - break; - - case LINE: - /* TODO: consider removing any trailing newline or LINE tokens */ - src->token.str = yasm_xmalloc(23+strlen(yapp_preproc_current_file)); - sprintf(src->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); - break; - - default: - yasm_xfree(src); - return; - } - append_processed_token(src, to_head, to_tail); + src = yasm_xmalloc(sizeof(source)); + src->token.type = token; + switch (token) + { + case INTNUM: + src->token.str = yasm__xstrdup(yapp_preproc_lval.int_str_val.str); + src->token.val.int_val = yapp_preproc_lval.int_str_val.val; + break; + + case FLTNUM: + src->token.str = yasm__xstrdup(yapp_preproc_lval.double_str_val.str); + src->token.val.double_val = yapp_preproc_lval.double_str_val.val; + break; + + case STRING: + case WHITESPACE: + src->token.str = yasm__xstrdup(yapp_preproc_lval.str_val); + break; + + case IDENT: + src->token.str = yasm__xstrdup(yapp_preproc_lval.str_val); + break; + + case '+': case '-': case '*': case '/': case '%': case ',': case '\n': + case '[': case ']': case '(': case ')': + src->token.str = yasm_xmalloc(2); + src->token.str[0] = (char)token; + src->token.str[1] = '\0'; + break; + + case LINE: + /* TODO: consider removing any trailing newline or LINE tokens */ + src->token.str = yasm_xmalloc(23+strlen(yapp_preproc_current_file)); + sprintf(src->token.str, "%%line %d+1 %s\n", yapp_preproc_line_number, yapp_preproc_current_file); + break; + + default: + yasm_xfree(src); + return; + } + append_processed_token(src, to_head, to_tail); } } static void replay_saved_tokens(char *ident, - struct source_head *from_head, - struct source_head *to_head, - source **to_tail) + struct source_head *from_head, + struct source_head *to_head, + source **to_tail) { source *item, *after; ydebug(("-No, %s didn't match any macro we have\n", ident)); @@ -450,11 +450,11 @@ replay_saved_tokens(char *ident, item = SLIST_FIRST(from_head); while (item) { - after = SLIST_NEXT(item, next); - SLIST_INSERT_AFTER(*to_tail, item, next); - *to_tail = item; - saved_length += strlen(item->token.str); - item = after; + after = SLIST_NEXT(item, next); + SLIST_INSERT_AFTER(*to_tail, item, next); + *to_tail = item; + saved_length += strlen(item->token.str); + item = after; } SLIST_INIT(from_head); } @@ -464,11 +464,11 @@ append_to_return(struct source_head *to_head, source **to_tail) { int token = yapp_preproc_lex(); while (token != '\n') { - ydebug(("YAPP: ATR: '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); - if (token == 0) - return 0; - append_token(token, to_head, to_tail); - token = yapp_preproc_lex(); + ydebug(("YAPP: ATR: '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); + if (token == 0) + return 0; + append_token(token, to_head, to_tail); + token = yapp_preproc_lex(); } return '\n'; } @@ -478,10 +478,10 @@ eat_through_return(struct source_head *to_head, source **to_tail) { int token; while ((token = yapp_preproc_lex()) != '\n') { - if (token == 0) - return 0; - yasm_error_set(YASM_ERROR_SYNTAX, - N_("Skipping possibly valid %%define stuff")); + if (token == 0) + return 0; + yasm_error_set(YASM_ERROR_SYNTAX, + N_("Skipping possibly valid %%define stuff")); } append_token('\n', to_head, to_tail); return '\n'; @@ -492,10 +492,10 @@ yapp_get_ident(const char *synlvl) { int token = yapp_preproc_lex(); if (token == WHITESPACE) - token = yapp_preproc_lex(); + token = yapp_preproc_lex(); if (token != IDENT) { - yasm_error_set(YASM_ERROR_SYNTAX, N_("Identifier expected after %%%s"), - synlvl); + yasm_error_set(YASM_ERROR_SYNTAX, N_("Identifier expected after %%%s"), + synlvl); } return token; } @@ -512,9 +512,9 @@ copy_token(YAPP_Token *tok, struct source_head *to_head, source **to_tail) void expand_macro(char *name, - struct source_head *from_head, - struct source_head *to_head, - source **to_tail) + struct source_head *from_head, + struct source_head *to_head, + source **to_tail) { struct source_head replay_head, arg_head; source *replay_tail, *arg_tail; @@ -526,219 +526,219 @@ expand_macro(char *name, if (ym->expanding) yasm_internal_error(N_("Recursively expanding a macro!")); if (ym->type == YAPP_DEFINE) { - if (ym->args == -1) { - /* no parens to deal with */ - ym->expanding = 1; - expand_token_list(&ym->macro_head, to_head, to_tail); - ym->expanding = 0; - } - else - { - char *mungename; - int token; - int argc=0; - int parennest=0; - HAMT *param_table; - source *replay, *param; - - ydebug(("YAPP: +Expanding multi-arg macro %s...\n", name)); - - /* Build up both a parameter reference list and a token buffer, - * because we won't know until we've reached the closing paren if - * we can actuall expand the macro or not. bleah. */ - /* worse, we can't build the parameter list until we know the - * parameter names, which we can't look up until we know the - * number of arguments. sigh */ - /* HMM */ - SLIST_INIT(&replay_head); - replay_tail = SLIST_FIRST(&replay_head); - - /* find out what we got */ - if (from_head) { - yasm_internal_error(N_("Expanding macro with non-null from_head ugh\n")); - } - token = yapp_preproc_lex(); - append_token(token, &replay_head, &replay_tail); - /* allow one whitespace */ - if (token == WHITESPACE) { - ydebug(("Ignoring WS between macro and paren\n")); - token = yapp_preproc_lex(); - append_token(token, &replay_head, &replay_tail); - } - if (token != '(') { - ydebug(("YAPP: -Didn't get to left paren; instead got '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); - replay_saved_tokens(name, &replay_head, to_head, to_tail); - return; - } - - /* at this point, we've got the left paren. time to get annoyed */ - while (token != ')') { - token = yapp_preproc_lex(); - append_token(token, &replay_head, &replay_tail); - /* TODO: handle { } for commas? or is that just macros? */ - switch (token) { - case '(': - parennest++; - break; - - case ')': - if (parennest) { - parennest--; - token = ','; - } - else { - argc++; - } - break; - - case ',': - if (!parennest) - argc++; - break; - - case WHITESPACE: case IDENT: case INTNUM: case FLTNUM: case STRING: - case '+': case '-': case '*': case '/': - break; - - default: - if (token < 256) - yasm_internal_error(N_("Unexpected character token in parameter expansion")); - else - yasm_error_set(YASM_ERROR_SYNTAX, N_("Cannot handle preprocessor items inside possible macro invocation")); - } - } - - /* Now we have the argument count; let's see if it exists */ - mungename = yasm_xmalloc(strlen(name)+8); - sprintf(mungename, "%s(%d)", name, argc); - ym = yapp_macro_get(mungename); - if (!ym) - { - ydebug(("YAPP: -Didn't find macro %s\n", mungename)); - replay_saved_tokens(name, &replay_head, to_head, to_tail); - yasm_xfree(mungename); - return; - } - ydebug(("YAPP: +Found macro %s\n", mungename)); - - ym->expanding = 1; - - /* so the macro exists. build a HAMT parameter table */ - param_table = HAMT_create(yasm_internal_error_); - /* fill the entries by walking the replay buffer and create - * "macros". coincidentally, clear the replay buffer. */ - - /* get to the opening paren */ - replay = SLIST_FIRST(&replay_head); - while (replay->token.type != '(') { - ydebug(("YAPP: Ignoring replay token '%c' \"%s\"\n", replay->token.type, replay->token.str)); - SLIST_REMOVE_HEAD(&replay_head, next); - yasm_xfree(replay->token.str); - yasm_xfree(replay); - replay = SLIST_FIRST(&replay_head); - } - ydebug(("YAPP: Ignoring replay token '%c' \"%s\"\n", replay->token.type, replay->token.str)); - - /* free the open paren */ - SLIST_REMOVE_HEAD(&replay_head, next); - yasm_xfree(replay->token.str); - yasm_xfree(replay); - - param = SLIST_FIRST(&ym->param_head); - - SLIST_INIT(&arg_head); - arg_tail = SLIST_FIRST(&arg_head); - - replay = SLIST_FIRST(&replay_head); - SLIST_REMOVE_HEAD(&replay_head, next); - while (parennest || replay) { - if (replay->token.type == '(') { - parennest++; - append_processed_token(replay, &arg_head, &arg_tail); - ydebug(("YAPP: +add arg token '%c'\n", replay->token.type)); - } - else if (parennest && replay->token.type == ')') { - parennest--; - append_processed_token(replay, &arg_head, &arg_tail); - ydebug(("YAPP: +add arg token '%c'\n", replay->token.type)); - } - else if ((!parennest) && (replay->token.type == ',' - || replay->token.type == ')')) - { - int zero=0; - struct source_head *argmacro = yasm_xmalloc(sizeof(struct source_head)); - memcpy(argmacro, &arg_head, sizeof(struct source_head)); - SLIST_INIT(&arg_head); - arg_tail = SLIST_FIRST(&arg_head); - - /* don't save the comma */ - yasm_xfree(replay->token.str); - yasm_xfree(replay); - - HAMT_insert(param_table, - param->token.str, - (void *)argmacro, - &zero, - (void (*)(void *))yapp_macro_error_sameargname); - param = SLIST_NEXT(param, next); - } - else if (replay->token.type == IDENT - && yapp_defined(replay->token.str)) - { - ydebug(("YAPP: +add arg macro '%c' \"%s\"\n", replay->token.type, replay->token.str)); - expand_macro(replay->token.str, &replay_head, &arg_head, &arg_tail); - } - else if (arg_tail || replay->token.type != WHITESPACE) { - ydebug(("YAPP: +else add arg token '%c' \"%s\"\n", replay->token.type, replay->token.str)); - append_processed_token(replay, &arg_head, &arg_tail); - } - replay = SLIST_FIRST(&replay_head); - if (replay) SLIST_REMOVE_HEAD(&replay_head, next); - } - if (replay) { - yasm_xfree(replay->token.str); - yasm_xfree(replay); - } - else if (param) { - yasm_internal_error(N_("Got to end of arglist before end of replay!")); - } - replay = SLIST_FIRST(&replay_head); - if (replay || param) - yasm_internal_error(N_("Count and distribution of define args mismatched!")); - - /* the param_table is set up without errors, so expansion is ready - * to go */ - SLIST_FOREACH (replay, &ym->macro_head, next) { - if (replay->token.type == IDENT) { - - /* check local args first */ - struct source_head *paramexp = - yapp_define_param_get(param_table, replay->token.str); - - if (paramexp) { - expand_token_list(paramexp, to_head, to_tail); - } - else { - /* otherwise, check macros */ - YAPP_Macro *imacro = yapp_macro_get(replay->token.str); - if (imacro != NULL && !imacro->expanding) { - expand_macro(replay->token.str, NULL, to_head, to_tail); - } - else { - /* otherwise it's just a vanilla ident */ - copy_token(&replay->token, to_head, to_tail); - } - } - } - else { - copy_token(&replay->token, to_head, to_tail); - } - } - ym->expanding = 0; - } + if (ym->args == -1) { + /* no parens to deal with */ + ym->expanding = 1; + expand_token_list(&ym->macro_head, to_head, to_tail); + ym->expanding = 0; + } + else + { + char *mungename; + int token; + int argc=0; + int parennest=0; + HAMT *param_table; + source *replay, *param; + + ydebug(("YAPP: +Expanding multi-arg macro %s...\n", name)); + + /* Build up both a parameter reference list and a token buffer, + * because we won't know until we've reached the closing paren if + * we can actuall expand the macro or not. bleah. */ + /* worse, we can't build the parameter list until we know the + * parameter names, which we can't look up until we know the + * number of arguments. sigh */ + /* HMM */ + SLIST_INIT(&replay_head); + replay_tail = SLIST_FIRST(&replay_head); + + /* find out what we got */ + if (from_head) { + yasm_internal_error(N_("Expanding macro with non-null from_head ugh\n")); + } + token = yapp_preproc_lex(); + append_token(token, &replay_head, &replay_tail); + /* allow one whitespace */ + if (token == WHITESPACE) { + ydebug(("Ignoring WS between macro and paren\n")); + token = yapp_preproc_lex(); + append_token(token, &replay_head, &replay_tail); + } + if (token != '(') { + ydebug(("YAPP: -Didn't get to left paren; instead got '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); + replay_saved_tokens(name, &replay_head, to_head, to_tail); + return; + } + + /* at this point, we've got the left paren. time to get annoyed */ + while (token != ')') { + token = yapp_preproc_lex(); + append_token(token, &replay_head, &replay_tail); + /* TODO: handle { } for commas? or is that just macros? */ + switch (token) { + case '(': + parennest++; + break; + + case ')': + if (parennest) { + parennest--; + token = ','; + } + else { + argc++; + } + break; + + case ',': + if (!parennest) + argc++; + break; + + case WHITESPACE: case IDENT: case INTNUM: case FLTNUM: case STRING: + case '+': case '-': case '*': case '/': + break; + + default: + if (token < 256) + yasm_internal_error(N_("Unexpected character token in parameter expansion")); + else + yasm_error_set(YASM_ERROR_SYNTAX, N_("Cannot handle preprocessor items inside possible macro invocation")); + } + } + + /* Now we have the argument count; let's see if it exists */ + mungename = yasm_xmalloc(strlen(name)+8); + sprintf(mungename, "%s(%d)", name, argc); + ym = yapp_macro_get(mungename); + if (!ym) + { + ydebug(("YAPP: -Didn't find macro %s\n", mungename)); + replay_saved_tokens(name, &replay_head, to_head, to_tail); + yasm_xfree(mungename); + return; + } + ydebug(("YAPP: +Found macro %s\n", mungename)); + + ym->expanding = 1; + + /* so the macro exists. build a HAMT parameter table */ + param_table = HAMT_create(yasm_internal_error_); + /* fill the entries by walking the replay buffer and create + * "macros". coincidentally, clear the replay buffer. */ + + /* get to the opening paren */ + replay = SLIST_FIRST(&replay_head); + while (replay->token.type != '(') { + ydebug(("YAPP: Ignoring replay token '%c' \"%s\"\n", replay->token.type, replay->token.str)); + SLIST_REMOVE_HEAD(&replay_head, next); + yasm_xfree(replay->token.str); + yasm_xfree(replay); + replay = SLIST_FIRST(&replay_head); + } + ydebug(("YAPP: Ignoring replay token '%c' \"%s\"\n", replay->token.type, replay->token.str)); + + /* free the open paren */ + SLIST_REMOVE_HEAD(&replay_head, next); + yasm_xfree(replay->token.str); + yasm_xfree(replay); + + param = SLIST_FIRST(&ym->param_head); + + SLIST_INIT(&arg_head); + arg_tail = SLIST_FIRST(&arg_head); + + replay = SLIST_FIRST(&replay_head); + SLIST_REMOVE_HEAD(&replay_head, next); + while (parennest || replay) { + if (replay->token.type == '(') { + parennest++; + append_processed_token(replay, &arg_head, &arg_tail); + ydebug(("YAPP: +add arg token '%c'\n", replay->token.type)); + } + else if (parennest && replay->token.type == ')') { + parennest--; + append_processed_token(replay, &arg_head, &arg_tail); + ydebug(("YAPP: +add arg token '%c'\n", replay->token.type)); + } + else if ((!parennest) && (replay->token.type == ',' + || replay->token.type == ')')) + { + int zero=0; + struct source_head *argmacro = yasm_xmalloc(sizeof(struct source_head)); + memcpy(argmacro, &arg_head, sizeof(struct source_head)); + SLIST_INIT(&arg_head); + arg_tail = SLIST_FIRST(&arg_head); + + /* don't save the comma */ + yasm_xfree(replay->token.str); + yasm_xfree(replay); + + HAMT_insert(param_table, + param->token.str, + (void *)argmacro, + &zero, + (void (*)(void *))yapp_macro_error_sameargname); + param = SLIST_NEXT(param, next); + } + else if (replay->token.type == IDENT + && yapp_defined(replay->token.str)) + { + ydebug(("YAPP: +add arg macro '%c' \"%s\"\n", replay->token.type, replay->token.str)); + expand_macro(replay->token.str, &replay_head, &arg_head, &arg_tail); + } + else if (arg_tail || replay->token.type != WHITESPACE) { + ydebug(("YAPP: +else add arg token '%c' \"%s\"\n", replay->token.type, replay->token.str)); + append_processed_token(replay, &arg_head, &arg_tail); + } + replay = SLIST_FIRST(&replay_head); + if (replay) SLIST_REMOVE_HEAD(&replay_head, next); + } + if (replay) { + yasm_xfree(replay->token.str); + yasm_xfree(replay); + } + else if (param) { + yasm_internal_error(N_("Got to end of arglist before end of replay!")); + } + replay = SLIST_FIRST(&replay_head); + if (replay || param) + yasm_internal_error(N_("Count and distribution of define args mismatched!")); + + /* the param_table is set up without errors, so expansion is ready + * to go */ + SLIST_FOREACH (replay, &ym->macro_head, next) { + if (replay->token.type == IDENT) { + + /* check local args first */ + struct source_head *paramexp = + yapp_define_param_get(param_table, replay->token.str); + + if (paramexp) { + expand_token_list(paramexp, to_head, to_tail); + } + else { + /* otherwise, check macros */ + YAPP_Macro *imacro = yapp_macro_get(replay->token.str); + if (imacro != NULL && !imacro->expanding) { + expand_macro(replay->token.str, NULL, to_head, to_tail); + } + else { + /* otherwise it's just a vanilla ident */ + copy_token(&replay->token, to_head, to_tail); + } + } + } + else { + copy_token(&replay->token, to_head, to_tail); + } + } + ym->expanding = 0; + } } else - yasm_internal_error(N_("Invoking Macros not yet supported")); + yasm_internal_error(N_("Invoking Macros not yet supported")); ym->expanding = 0; } @@ -748,18 +748,18 @@ expand_token_list(struct source_head *paramexp, struct source_head *to_head, sou { source *item; SLIST_FOREACH (item, paramexp, next) { - if (item->token.type == IDENT) { - YAPP_Macro *imacro = yapp_macro_get(item->token.str); - if (imacro != NULL && !imacro->expanding) { - expand_macro(item->token.str, NULL, to_head, to_tail); - } - else { - copy_token(&item->token, to_head, to_tail); - } - } - else { - copy_token(&item->token, to_head, to_tail); - } + if (item->token.type == IDENT) { + YAPP_Macro *imacro = yapp_macro_get(item->token.str); + if (imacro != NULL && !imacro->expanding) { + expand_macro(item->token.str, NULL, to_head, to_tail); + } + else { + copy_token(&item->token, to_head, to_tail); + } + } + else { + copy_token(&item->token, to_head, to_tail); + } } } @@ -773,187 +773,187 @@ yapp_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size) while ((size_t)saved_length < max_size && state != YAPP_STATE_EOF) { - token = yapp_preproc_lex(); - - switch (state) { - case YAPP_STATE_INITIAL: - switch (token) - { - char *s; - default: - append_token(token, &source_head, &source_tail); - /*if (append_to_return()==0) state=YAPP_STATE_EOF;*/ - ydebug(("YAPP: default: '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); - /*yasm_error_set(YASM_ERROR_SYNTAX, N_("YAPP got an unhandled token."));*/ - break; - - case IDENT: - ydebug(("YAPP: ident: \"%s\"\n", yapp_preproc_lval.str_val)); - if (yapp_defined(yapp_preproc_lval.str_val)) { - expand_macro(yapp_preproc_lval.str_val, NULL, &source_head, &source_tail); - } - else { - append_token(token, &source_head, &source_tail); - } - break; - - case 0: - state = YAPP_STATE_EOF; - break; - - case '\n': - append_token(token, &source_head, &source_tail); - break; - - case CLEAR: - HAMT_destroy(macro_table, (void (*)(void *))yapp_macro_delete); - macro_table = HAMT_create(yasm_internal_error_); - break; - - case DEFINE: - ydebug(("YAPP: define: ")); - token = yapp_get_ident("define"); - ydebug((" \"%s\"\n", yapp_preproc_lval.str_val)); - s = yasm__xstrdup(yapp_preproc_lval.str_val); - - /* three cases: newline or stuff or left paren */ - token = yapp_preproc_lex(); - if (token == '\n') { - /* no args or content - just insert it */ - yapp_define_insert(s, -1, 0); - append_token('\n', &source_head, &source_tail); - } - else if (token == WHITESPACE) { - /* no parens */ - if(append_to_return(¯o_head, ¯o_tail)==0) state=YAPP_STATE_EOF; - else { - yapp_define_insert(s, -1, 0); - } - append_token('\n', &source_head, &source_tail); - } - else if (token == '(') { - /* get all params of the parameter list */ - /* ensure they alternate IDENT and ',' */ - int param_count = 0; - int last_token = ','; - - ydebug((" *Getting arglist for define %s\n", s)); - - while ((token = yapp_preproc_lex())!=')') { - ydebug(("YAPP: +read token '%c' \"%s\" for macro %s\n", token, yapp_preproc_lval.str_val, s)); - if (token == WHITESPACE) { - token = last_token; - } - else if (last_token == ',' && token == IDENT) { - append_token(token, ¶m_head, ¶m_tail); - param_count++; - } - else if (token == 0) { - state = YAPP_STATE_EOF; - break; - } - else if (last_token == ',' || token != ',') - yasm_error_set(YASM_ERROR_SYNTAX, N_("Unexpected token in %%define parameters")); - last_token = token; - } - if (token == ')') { - /* after paramlist and ')' */ - /* everything is what it's defined to be */ - token = yapp_preproc_lex(); - if (token != WHITESPACE) append_token(token, ¯o_head, ¯o_tail); - if (append_to_return(¯o_head, ¯o_tail)==0) state=YAPP_STATE_EOF; - else { - ydebug(("YAPP: Inserting define macro %s (%d)\n", s, param_count)); - yapp_define_insert(s, param_count, 0); - } - } - append_token('\n', &source_head, &source_tail); - } - else { - yasm_internal_error(N_("%%define ... failed miserably - neither \\n, WS, or ( followed ident")); - } - break; - - case UNDEF: - token = yapp_get_ident("undef"); - yapp_undef(yapp_preproc_lval.str_val); - state = YAPP_STATE_NEED_EOL; - break; - - case IFDEF: - token = yapp_get_ident("ifdef"); - push_if(yapp_defined(yapp_preproc_lval.str_val)); - state = YAPP_STATE_NEED_EOL; - break; - - case IFNDEF: - token = yapp_get_ident("ifndef"); - push_if(!yapp_defined(yapp_preproc_lval.str_val)); - state = YAPP_STATE_NEED_EOL; - break; - - case ELSE: - push_else(1); - if (current_output == YAPP_OUTPUT) need_line_directive = 1; - state = YAPP_STATE_NEED_EOL; - break; - - case ELIFDEF: - token = yapp_get_ident("elifdef"); - push_else(yapp_defined(yapp_preproc_lval.str_val)); - if (current_output == YAPP_OUTPUT) need_line_directive = 1; - state = YAPP_STATE_NEED_EOL; - break; - - case ELIFNDEF: - token = yapp_get_ident("elifndef"); - push_else(!yapp_defined(yapp_preproc_lval.str_val)); - if (current_output == YAPP_OUTPUT) need_line_directive = 1; - state = YAPP_STATE_NEED_EOL; - break; - - case ENDIF: - /* there's got to be another way to do this: */ - /* only set if going from non-output to output */ - if (current_output != YAPP_OUTPUT) need_line_directive = 1; - pop_if(); - if (current_output != YAPP_OUTPUT) need_line_directive = 0; - state = YAPP_STATE_NEED_EOL; - break; - - case INCLUDE: - case LINE: - need_line_directive = 1; - break; - } - if (state == YAPP_STATE_NEED_EOL) - { - if (eat_through_return(&source_head, &source_tail)==0) state=YAPP_STATE_EOF; - else state=YAPP_STATE_INITIAL; - } - break; - default: - yasm_error_set(YASM_ERROR_PARSE, N_("YAPP got into a bad state")); - } - if (need_line_directive) { - append_token(LINE, &source_head, &source_tail); - need_line_directive = 0; - } + token = yapp_preproc_lex(); + + switch (state) { + case YAPP_STATE_INITIAL: + switch (token) + { + char *s; + default: + append_token(token, &source_head, &source_tail); + /*if (append_to_return()==0) state=YAPP_STATE_EOF;*/ + ydebug(("YAPP: default: '%c' \"%s\"\n", token, yapp_preproc_lval.str_val)); + /*yasm_error_set(YASM_ERROR_SYNTAX, N_("YAPP got an unhandled token."));*/ + break; + + case IDENT: + ydebug(("YAPP: ident: \"%s\"\n", yapp_preproc_lval.str_val)); + if (yapp_defined(yapp_preproc_lval.str_val)) { + expand_macro(yapp_preproc_lval.str_val, NULL, &source_head, &source_tail); + } + else { + append_token(token, &source_head, &source_tail); + } + break; + + case 0: + state = YAPP_STATE_EOF; + break; + + case '\n': + append_token(token, &source_head, &source_tail); + break; + + case CLEAR: + HAMT_destroy(macro_table, (void (*)(void *))yapp_macro_delete); + macro_table = HAMT_create(yasm_internal_error_); + break; + + case DEFINE: + ydebug(("YAPP: define: ")); + token = yapp_get_ident("define"); + ydebug((" \"%s\"\n", yapp_preproc_lval.str_val)); + s = yasm__xstrdup(yapp_preproc_lval.str_val); + + /* three cases: newline or stuff or left paren */ + token = yapp_preproc_lex(); + if (token == '\n') { + /* no args or content - just insert it */ + yapp_define_insert(s, -1, 0); + append_token('\n', &source_head, &source_tail); + } + else if (token == WHITESPACE) { + /* no parens */ + if(append_to_return(¯o_head, ¯o_tail)==0) state=YAPP_STATE_EOF; + else { + yapp_define_insert(s, -1, 0); + } + append_token('\n', &source_head, &source_tail); + } + else if (token == '(') { + /* get all params of the parameter list */ + /* ensure they alternate IDENT and ',' */ + int param_count = 0; + int last_token = ','; + + ydebug((" *Getting arglist for define %s\n", s)); + + while ((token = yapp_preproc_lex())!=')') { + ydebug(("YAPP: +read token '%c' \"%s\" for macro %s\n", token, yapp_preproc_lval.str_val, s)); + if (token == WHITESPACE) { + token = last_token; + } + else if (last_token == ',' && token == IDENT) { + append_token(token, ¶m_head, ¶m_tail); + param_count++; + } + else if (token == 0) { + state = YAPP_STATE_EOF; + break; + } + else if (last_token == ',' || token != ',') + yasm_error_set(YASM_ERROR_SYNTAX, N_("Unexpected token in %%define parameters")); + last_token = token; + } + if (token == ')') { + /* after paramlist and ')' */ + /* everything is what it's defined to be */ + token = yapp_preproc_lex(); + if (token != WHITESPACE) append_token(token, ¯o_head, ¯o_tail); + if (append_to_return(¯o_head, ¯o_tail)==0) state=YAPP_STATE_EOF; + else { + ydebug(("YAPP: Inserting define macro %s (%d)\n", s, param_count)); + yapp_define_insert(s, param_count, 0); + } + } + append_token('\n', &source_head, &source_tail); + } + else { + yasm_internal_error(N_("%%define ... failed miserably - neither \\n, WS, or ( followed ident")); + } + break; + + case UNDEF: + token = yapp_get_ident("undef"); + yapp_undef(yapp_preproc_lval.str_val); + state = YAPP_STATE_NEED_EOL; + break; + + case IFDEF: + token = yapp_get_ident("ifdef"); + push_if(yapp_defined(yapp_preproc_lval.str_val)); + state = YAPP_STATE_NEED_EOL; + break; + + case IFNDEF: + token = yapp_get_ident("ifndef"); + push_if(!yapp_defined(yapp_preproc_lval.str_val)); + state = YAPP_STATE_NEED_EOL; + break; + + case ELSE: + push_else(1); + if (current_output == YAPP_OUTPUT) need_line_directive = 1; + state = YAPP_STATE_NEED_EOL; + break; + + case ELIFDEF: + token = yapp_get_ident("elifdef"); + push_else(yapp_defined(yapp_preproc_lval.str_val)); + if (current_output == YAPP_OUTPUT) need_line_directive = 1; + state = YAPP_STATE_NEED_EOL; + break; + + case ELIFNDEF: + token = yapp_get_ident("elifndef"); + push_else(!yapp_defined(yapp_preproc_lval.str_val)); + if (current_output == YAPP_OUTPUT) need_line_directive = 1; + state = YAPP_STATE_NEED_EOL; + break; + + case ENDIF: + /* there's got to be another way to do this: */ + /* only set if going from non-output to output */ + if (current_output != YAPP_OUTPUT) need_line_directive = 1; + pop_if(); + if (current_output != YAPP_OUTPUT) need_line_directive = 0; + state = YAPP_STATE_NEED_EOL; + break; + + case INCLUDE: + case LINE: + need_line_directive = 1; + break; + } + if (state == YAPP_STATE_NEED_EOL) + { + if (eat_through_return(&source_head, &source_tail)==0) state=YAPP_STATE_EOF; + else state=YAPP_STATE_INITIAL; + } + break; + default: + yasm_error_set(YASM_ERROR_PARSE, N_("YAPP got into a bad state")); + } + if (need_line_directive) { + append_token(LINE, &source_head, &source_tail); + need_line_directive = 0; + } } /* convert saved stuff into output. we either have enough, or are EOF */ while (n < max_size && saved_length) { - src = SLIST_FIRST(&source_head); - if (max_size - n >= strlen(src->token.str)) { - strcpy(buf+n, src->token.str); - n += strlen(src->token.str); - - saved_length -= strlen(src->token.str); - SLIST_REMOVE_HEAD(&source_head, next); - yasm_xfree(src->token.str); - yasm_xfree(src); - } + src = SLIST_FIRST(&source_head); + if (max_size - n >= strlen(src->token.str)) { + strcpy(buf+n, src->token.str); + n += strlen(src->token.str); + + saved_length -= strlen(src->token.str); + SLIST_REMOVE_HEAD(&source_head, next); + yasm_xfree(src->token.str); + yasm_xfree(src); + } } return n; @@ -961,7 +961,7 @@ yapp_preproc_input(yasm_preproc *preproc, char *buf, size_t max_size) static size_t yapp_preproc_get_included_file(yasm_preproc *preproc, /*@out@*/ char *buf, - size_t max_size) + size_t max_size) { /* TODO */ return 0; diff --git a/modules/preprocs/yapp/yapp-preproc.h b/modules/preprocs/yapp/yapp-preproc.h index 9e5366ae..52f44b23 100644 --- a/modules/preprocs/yapp/yapp-preproc.h +++ b/modules/preprocs/yapp/yapp-preproc.h @@ -30,9 +30,9 @@ typedef struct YAPP_Token_s { unsigned int type; char *str; union { - unsigned int int_val; - double double_val; - char *str_val; + unsigned int int_val; + double double_val; + char *str_val; } val; } YAPP_Token; @@ -48,9 +48,9 @@ typedef enum { /* tracks nested %if* %elif* %else %endif structures */ typedef enum { - YAPP_OUTPUT, /* this level+module outputs */ - YAPP_NO_OUTPUT, /* this would never output */ - YAPP_OLD_OUTPUT, /* this level has already output */ + YAPP_OUTPUT, /* this level+module outputs */ + YAPP_NO_OUTPUT, /* this would never output */ + YAPP_OLD_OUTPUT, /* this level has already output */ YAPP_BLOCKED_OUTPUT /* the surrounding level is not outputting */ } YAPP_Output; @@ -58,5 +58,5 @@ void yapp_lex_initialize(FILE *f); void set_inhibit(void); extern /*@dependent@*/ yasm_linemap *yapp_preproc_linemap; -#define cur_lindex yasm_linemap_get_current(yapp_preproc_linemap) +#define cur_lindex yasm_linemap_get_current(yapp_preproc_linemap) diff --git a/modules/preprocs/yapp/yapp-token.h b/modules/preprocs/yapp/yapp-token.h index 43012495..e63c4c83 100644 --- a/modules/preprocs/yapp/yapp-token.h +++ b/modules/preprocs/yapp/yapp-token.h @@ -1,60 +1,60 @@ typedef union { char *str_val; struct { - char *str; - unsigned long val; + char *str; + unsigned long val; } int_str_val; struct { - char *str; - double val; + char *str; + double val; } double_str_val; } YYSTYPE; -#define INTNUM 257 -#define FLTNUM 258 -#define STRING 259 -#define IDENT 260 -#define CLEAR 261 -#define INCLUDE 262 -#define LINE 263 -#define DEFINE 264 -#define UNDEF 265 -#define ASSIGN 266 -#define MACRO 267 -#define ENDMACRO 268 -#define ROTATE 269 -#define REP 270 -#define EXITREP 271 -#define ENDREP 272 -#define IF 273 -#define ELIF 274 -#define ELSE 275 -#define ENDIF 276 -#define IFDEF 277 -#define ELIFDEF 278 -#define IFNDEF 279 -#define ELIFNDEF 280 -#define IFCTX 281 -#define ELIFCTX 282 -#define IFIDN 283 -#define ELIFIDN 284 -#define IFIDNI 285 -#define ELIFIDNI 286 -#define IFID 287 -#define ELIFID 288 -#define IFNUM 289 -#define ELIFNUM 290 -#define IFSTR 291 -#define ELIFSTR 292 -#define ERROR 293 -#define PUSH 294 -#define POP 295 -#define REPL 296 -#define LEFT_OP 297 -#define RIGHT_OP 298 -#define SIGNDIV 299 -#define SIGNMOD 300 -#define UNARYOP 301 -#define WHITESPACE 302 +#define INTNUM 257 +#define FLTNUM 258 +#define STRING 259 +#define IDENT 260 +#define CLEAR 261 +#define INCLUDE 262 +#define LINE 263 +#define DEFINE 264 +#define UNDEF 265 +#define ASSIGN 266 +#define MACRO 267 +#define ENDMACRO 268 +#define ROTATE 269 +#define REP 270 +#define EXITREP 271 +#define ENDREP 272 +#define IF 273 +#define ELIF 274 +#define ELSE 275 +#define ENDIF 276 +#define IFDEF 277 +#define ELIFDEF 278 +#define IFNDEF 279 +#define ELIFNDEF 280 +#define IFCTX 281 +#define ELIFCTX 282 +#define IFIDN 283 +#define ELIFIDN 284 +#define IFIDNI 285 +#define ELIFIDNI 286 +#define IFID 287 +#define ELIFID 288 +#define IFNUM 289 +#define ELIFNUM 290 +#define IFSTR 291 +#define ELIFSTR 292 +#define ERROR 293 +#define PUSH 294 +#define POP 295 +#define REPL 296 +#define LEFT_OP 297 +#define RIGHT_OP 298 +#define SIGNDIV 299 +#define SIGNMOD 300 +#define UNARYOP 301 +#define WHITESPACE 302 extern YYSTYPE yapp_preproc_lval; diff --git a/out_test.sh b/out_test.sh index 2f9bb079..a0eb8be4 100755 --- a/out_test.sh +++ b/out_test.sh @@ -30,64 +30,64 @@ do e=${a}.ew eg=`echo ${asm} | sed 's,.asm$,.errwarn,'` if test \! -e ${eg}; then - eg=/dev/null + eg=/dev/null fi # Run within a subshell to prevent signal messages from displaying. sh -c "cat ${asm} | ./yasm $4 -o results/${o} - 2>results/${e}" >/dev/null 2>/dev/null status=$? if test $status -gt 128; then - # We should never get a coredump! - echo $ECHO_N "C$ECHO_C" + # We should never get a coredump! + echo $ECHO_N "C$ECHO_C" eval "failed$failedct='C: ${a} crashed!'" - failedct=`expr $failedct + 1` + failedct=`expr $failedct + 1` elif test $status -gt 0; then - echo ${asm} | grep err >/dev/null - if test $? -gt 0; then - # YASM detected errors but shouldn't have! - echo $ECHO_N "E$ECHO_C" + echo ${asm} | grep err >/dev/null + if test $? -gt 0; then + # YASM detected errors but shouldn't have! + echo $ECHO_N "E$ECHO_C" eval "failed$failedct='E: ${a} returned an error code!'" - failedct=`expr $failedct + 1` - else - # We got errors, check to see if they match: - if diff -w ${eg} results/${e} >/dev/null; then - # Error/warnings match, it passes! - echo $ECHO_N ".$ECHO_C" - passedct=`expr $passedct + 1` - else - # Error/warnings don't match. - echo $ECHO_N "W$ECHO_C" + failedct=`expr $failedct + 1` + else + # We got errors, check to see if they match: + if diff -w ${eg} results/${e} >/dev/null; then + # Error/warnings match, it passes! + echo $ECHO_N ".$ECHO_C" + passedct=`expr $passedct + 1` + else + # Error/warnings don't match. + echo $ECHO_N "W$ECHO_C" eval "failed$failedct='W: ${a} did not match errors and warnings!'" - failedct=`expr $failedct + 1` - fi - fi + failedct=`expr $failedct + 1` + fi + fi else - echo ${asm} | grep -v err >/dev/null - if test $? -gt 0; then - # YASM didn't detect errors but should have! - echo $ECHO_N "E$ECHO_C" + echo ${asm} | grep -v err >/dev/null + if test $? -gt 0; then + # YASM didn't detect errors but should have! + echo $ECHO_N "E$ECHO_C" eval "failed$failedct='E: ${a} did not return an error code!'" - failedct=`expr $failedct + 1` - else - ./test_hd results/${o} > results/${oh} - if diff ${og} results/${oh} >/dev/null; then - if diff -w ${eg} results/${e} >/dev/null; then - # Both object file and error/warnings match, it passes! - echo $ECHO_N ".$ECHO_C" - passedct=`expr $passedct + 1` - else - # Error/warnings don't match. - echo $ECHO_N "W$ECHO_C" + failedct=`expr $failedct + 1` + else + ./test_hd results/${o} > results/${oh} + if diff ${og} results/${oh} >/dev/null; then + if diff -w ${eg} results/${e} >/dev/null; then + # Both object file and error/warnings match, it passes! + echo $ECHO_N ".$ECHO_C" + passedct=`expr $passedct + 1` + else + # Error/warnings don't match. + echo $ECHO_N "W$ECHO_C" eval "failed$failedct='W: ${a} did not match errors and warnings!'" - failedct=`expr $failedct + 1` - fi - else - # Object file doesn't match. - echo $ECHO_N "O$ECHO_C" + failedct=`expr $failedct + 1` + fi + else + # Object file doesn't match. + echo $ECHO_N "O$ECHO_C" eval "failed$failedct='O: ${a} did not match object file!'" - failedct=`expr $failedct + 1` - fi - fi + failedct=`expr $failedct + 1` + fi + fi fi done diff --git a/test_hd.c b/test_hd.c index d3c5e9ea..d917adf5 100644 --- a/test_hd.c +++ b/test_hd.c @@ -35,23 +35,23 @@ main(int argc, char *argv[]) int ch; if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return EXIT_FAILURE; + fprintf(stderr, "Usage: %s \n", argv[0]); + return EXIT_FAILURE; } bfile = fopen(argv[1], "rb"); if (!bfile) { - fprintf(stderr, "Could not open `%s'.\n", argv[1]); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s'.\n", argv[1]); + return EXIT_FAILURE; } while ((ch = fgetc(bfile)) != EOF) - printf("%02x \n", ch); + printf("%02x \n", ch); if (ferror(bfile)) { - fprintf(stderr, "Error reading from `%s'.\n", argv[1]); - return EXIT_FAILURE; + fprintf(stderr, "Error reading from `%s'.\n", argv[1]); + return EXIT_FAILURE; } fclose(bfile); diff --git a/tools/gap/gap.c b/tools/gap/gap.c index b9069f20..9aa80fcd 100644 --- a/tools/gap/gap.c +++ b/tools/gap/gap.c @@ -71,7 +71,7 @@ typedef enum { } dir_type; typedef struct { - void (*parse_insn) (void); /* arch-specific parse_insn */ + void (*parse_insn) (void); /* arch-specific parse_insn */ int multi_parser[NUM_DIRS]; /* whether it has an initial parser field */ } arch_handler; @@ -124,9 +124,9 @@ dup_slist(slist *out, slist *in) STAILQ_INIT(out); STAILQ_FOREACH(sv, in, link) { - sval *nsv = yasm_xmalloc(sizeof(sval)); - nsv->str = yasm__xstrdup(sv->str); - STAILQ_INSERT_TAIL(out, nsv, link); + sval *nsv = yasm_xmalloc(sizeof(sval)); + nsv->str = yasm__xstrdup(sv->str); + STAILQ_INSERT_TAIL(out, nsv, link); } } @@ -147,16 +147,16 @@ get_dirs(dir_byp_list *byp, /*@null@*/ const char *parser) dir_byp *db; if (STAILQ_EMPTY(byp)) { - report_error("PARSERS not yet specified"); - return NULL; + report_error("PARSERS not yet specified"); + return NULL; } STAILQ_FOREACH(db, byp, link) { - if ((!parser && !db->parser) || - (parser && db->parser && strcmp(parser, db->parser) == 0)) { - found = &db->dirs; - break; - } + if ((!parser && !db->parser) || + (parser && db->parser && strcmp(parser, db->parser) == 0)) { + found = &db->dirs; + break; + } } return found; @@ -171,22 +171,22 @@ add_dir(dir_byp_list *byp, /*@null@*/ const char *parser, dir *d) dir_list *found = get_dirs(byp, parser); if (found) { - STAILQ_INSERT_TAIL(found, d, link); - return 0; + STAILQ_INSERT_TAIL(found, d, link); + return 0; } else if (!parser) { - /* Add separately to all */ - dir_byp *db; - int first = 1; - STAILQ_FOREACH(db, byp, link) { - if (!first) - d = dup_dir(d); - first = 0; - STAILQ_INSERT_TAIL(&db->dirs, d, link); - } - return 0; + /* Add separately to all */ + dir_byp *db; + int first = 1; + STAILQ_FOREACH(db, byp, link) { + if (!first) + d = dup_dir(d); + first = 0; + STAILQ_INSERT_TAIL(&db->dirs, d, link); + } + return 0; } else { - report_error("parser not found"); - return 1; + report_error("parser not found"); + return 1; } } @@ -196,9 +196,9 @@ check_parser(dir_type type) char *parser = NULL; if (arch->multi_parser[type]) { - parser = strtok(NULL, " \t\n"); - if (strcmp(parser, "-") == 0) - parser = NULL; + parser = strtok(NULL, " \t\n"); + if (strcmp(parser, "-") == 0) + parser = NULL; } return parser; @@ -214,15 +214,15 @@ parse_args(slist *args) tok = strtok(NULL, " \t\n"); if (!tok) { - report_error("no args"); - return; + report_error("no args"); + return; } while (tok) { - sv = yasm_xmalloc(sizeof(sval)); - sv->str = yasm__xstrdup(tok); - STAILQ_INSERT_TAIL(args, sv, link); - tok = strtok(NULL, " \t\n"); + sv = yasm_xmalloc(sizeof(sval)); + sv->str = yasm__xstrdup(tok); + STAILQ_INSERT_TAIL(args, sv, link); + tok = strtok(NULL, " \t\n"); } } @@ -249,18 +249,18 @@ parse_arch(void) char *tok = strtok(NULL, " \t\n"); if (!tok) { - report_error("ARCH requires an operand"); - return; + report_error("ARCH requires an operand"); + return; } for (i=0; imulti_parser[INSN] || arch->multi_parser[PREFIX]) { - db = yasm_xmalloc(sizeof(dir_byp)); - db->parser = yasm__xstrdup(tok); - STAILQ_INIT(&db->dirs); - - STAILQ_INSERT_TAIL(&insnprefix_byp, db, link); - } - if (arch->multi_parser[CPU] || arch->multi_parser[CPU_ALIAS] || - arch->multi_parser[CPU_FEATURE]) { - db = yasm_xmalloc(sizeof(dir_byp)); - db->parser = yasm__xstrdup(tok); - STAILQ_INIT(&db->dirs); - - STAILQ_INSERT_TAIL(&cpu_byp, db, link); - } - if (arch->multi_parser[TARGETMOD] || arch->multi_parser[REG] || - arch->multi_parser[REGGROUP] || arch->multi_parser[SEGREG]) { - db = yasm_xmalloc(sizeof(dir_byp)); - db->parser = yasm__xstrdup(tok); - STAILQ_INIT(&db->dirs); - - STAILQ_INSERT_TAIL(®tmod_byp, db, link); - } - tok = strtok(NULL, " \t\n"); + /* Insert into each slist of slist if broken out by parser */ + if (arch->multi_parser[INSN] || arch->multi_parser[PREFIX]) { + db = yasm_xmalloc(sizeof(dir_byp)); + db->parser = yasm__xstrdup(tok); + STAILQ_INIT(&db->dirs); + + STAILQ_INSERT_TAIL(&insnprefix_byp, db, link); + } + if (arch->multi_parser[CPU] || arch->multi_parser[CPU_ALIAS] || + arch->multi_parser[CPU_FEATURE]) { + db = yasm_xmalloc(sizeof(dir_byp)); + db->parser = yasm__xstrdup(tok); + STAILQ_INIT(&db->dirs); + + STAILQ_INSERT_TAIL(&cpu_byp, db, link); + } + if (arch->multi_parser[TARGETMOD] || arch->multi_parser[REG] || + arch->multi_parser[REGGROUP] || arch->multi_parser[SEGREG]) { + db = yasm_xmalloc(sizeof(dir_byp)); + db->parser = yasm__xstrdup(tok); + STAILQ_INIT(&db->dirs); + + STAILQ_INSERT_TAIL(®tmod_byp, db, link); + } + tok = strtok(NULL, " \t\n"); } /* Add NULL (global) versions if not already created */ if (STAILQ_EMPTY(&insnprefix_byp)) { - db = yasm_xmalloc(sizeof(dir_byp)); - db->parser = NULL; - STAILQ_INIT(&db->dirs); + db = yasm_xmalloc(sizeof(dir_byp)); + db->parser = NULL; + STAILQ_INIT(&db->dirs); - STAILQ_INSERT_TAIL(&insnprefix_byp, db, link); + STAILQ_INSERT_TAIL(&insnprefix_byp, db, link); } if (STAILQ_EMPTY(&cpu_byp)) { - db = yasm_xmalloc(sizeof(dir_byp)); - db->parser = NULL; - STAILQ_INIT(&db->dirs); + db = yasm_xmalloc(sizeof(dir_byp)); + db->parser = NULL; + STAILQ_INIT(&db->dirs); - STAILQ_INSERT_TAIL(&cpu_byp, db, link); + STAILQ_INSERT_TAIL(&cpu_byp, db, link); } if (STAILQ_EMPTY(®tmod_byp)) { - db = yasm_xmalloc(sizeof(dir_byp)); - db->parser = NULL; - STAILQ_INIT(&db->dirs); + db = yasm_xmalloc(sizeof(dir_byp)); + db->parser = NULL; + STAILQ_INIT(&db->dirs); - STAILQ_INSERT_TAIL(®tmod_byp, db, link); + STAILQ_INSERT_TAIL(®tmod_byp, db, link); } } @@ -346,62 +346,62 @@ x86_parse_insn(void) sval *sv; if (!suffix) { - report_error("INSN requires suffix"); - return; + report_error("INSN requires suffix"); + return; } /* save the remainder of args */ parse_args(&args); if (suffix[0] != '"') { - /* Just one instruction to generate */ - sv = yasm_xmalloc(sizeof(sval)); - sv->str = yasm__xstrdup(suffix); - STAILQ_INSERT_HEAD(&args, sv, link); - - d = yasm_xmalloc(sizeof(dir)); - d->name = yasm__xstrdup(bname); - d->func = "INSN"; - d->args = args; - add_dir(&insnprefix_byp, parser, d); + /* Just one instruction to generate */ + sv = yasm_xmalloc(sizeof(sval)); + sv->str = yasm__xstrdup(suffix); + STAILQ_INSERT_HEAD(&args, sv, link); + + d = yasm_xmalloc(sizeof(dir)); + d->name = yasm__xstrdup(bname); + d->func = "INSN"; + d->args = args; + add_dir(&insnprefix_byp, parser, d); } else { - /* Need to generate with suffixes for gas */ - char *p; - char sufstr[6]; - size_t bnamelen = strlen(bname); + /* Need to generate with suffixes for gas */ + char *p; + char sufstr[6]; + size_t bnamelen = strlen(bname); - strcpy(sufstr, "SUF_X"); + strcpy(sufstr, "SUF_X"); - for (p = &suffix[1]; *p != '"'; p++) { - sufstr[4] = toupper(*p); + for (p = &suffix[1]; *p != '"'; p++) { + sufstr[4] = toupper(*p); - d = yasm_xmalloc(sizeof(dir)); + d = yasm_xmalloc(sizeof(dir)); - d->name = yasm_xmalloc(bnamelen+2); - strcpy(d->name, bname); - d->name[bnamelen] = tolower(*p); - d->name[bnamelen+1] = '\0'; + d->name = yasm_xmalloc(bnamelen+2); + strcpy(d->name, bname); + d->name[bnamelen] = tolower(*p); + d->name[bnamelen+1] = '\0'; - d->func = "INSN"; - dup_slist(&d->args, &args); + d->func = "INSN"; + dup_slist(&d->args, &args); - sv = yasm_xmalloc(sizeof(sval)); - sv->str = yasm__xstrdup(sufstr); - STAILQ_INSERT_HEAD(&d->args, sv, link); + sv = yasm_xmalloc(sizeof(sval)); + sv->str = yasm__xstrdup(sufstr); + STAILQ_INSERT_HEAD(&d->args, sv, link); - add_dir(&insnprefix_byp, "gas", d); - } + add_dir(&insnprefix_byp, "gas", d); + } - /* And finally the version sans suffix */ - sv = yasm_xmalloc(sizeof(sval)); - sv->str = yasm__xstrdup("NONE"); - STAILQ_INSERT_HEAD(&args, sv, link); + /* And finally the version sans suffix */ + sv = yasm_xmalloc(sizeof(sval)); + sv->str = yasm__xstrdup("NONE"); + STAILQ_INSERT_HEAD(&args, sv, link); - d = yasm_xmalloc(sizeof(dir)); - d->name = yasm__xstrdup(bname); - d->func = "INSN"; - d->args = args; - add_dir(&insnprefix_byp, parser, d); + d = yasm_xmalloc(sizeof(dir)); + d->name = yasm__xstrdup(bname); + d->func = "INSN"; + d->args = args; + add_dir(&insnprefix_byp, parser, d); } } @@ -409,8 +409,8 @@ static void parse_insn(void) { if (!arch) { - report_error("ARCH not defined prior to INSN"); - return; + report_error("ARCH not defined prior to INSN"); + return; } arch->parse_insn(); } @@ -434,17 +434,17 @@ parse_cpu_alias(void) dir *aliasd, *d; if (!alias) { - report_error("CPU_ALIAS requires an operand"); - return; + report_error("CPU_ALIAS requires an operand"); + return; } STAILQ_FOREACH(aliasd, dirs, link) { - if (strcmp(aliasd->name, alias) == 0) - break; + if (strcmp(aliasd->name, alias) == 0) + break; } if (!aliasd) { - report_error("could not find `%s'", alias); - return; + report_error("could not find `%s'", alias); + return; } d = yasm_xmalloc(sizeof(dir)); @@ -522,83 +522,83 @@ make_c_tab( FILE *f, const char *which, const char *parser, - bstuff *tab, /* table indexed by b */ - ub4 smax, /* range of scramble[] */ - ub4 blen, /* b in 0..blen-1, power of 2 */ - ub4 *scramble) /* used in final hash */ + bstuff *tab, /* table indexed by b */ + ub4 smax, /* range of scramble[] */ + ub4 blen, /* b in 0..blen-1, power of 2 */ + ub4 *scramble) /* used in final hash */ { ub4 i; /* table for the mapping for the perfect hash */ if (blen >= USE_SCRAMBLE) { - /* A way to make the 1-byte values in tab bigger */ - if (smax > UB2MAXVAL+1) { - fprintf(f, "static const unsigned long %s_", which); - if (parser) - fprintf(f, "%s_", parser); - fprintf(f, "scramble[] = {\n"); - for (i=0; i<=UB1MAXVAL; i+=4) - fprintf(f, "0x%.8lx, 0x%.8lx, 0x%.8lx, 0x%.8lx,\n", - scramble[i+0], scramble[i+1], scramble[i+2], scramble[i+3]); - } else { - fprintf(f, "static const unsigned short %s_", which); - if (parser) - fprintf(f, "%s_", parser); - fprintf(f, "scramble[] = {\n"); - for (i=0; i<=UB1MAXVAL; i+=8) - fprintf(f, + /* A way to make the 1-byte values in tab bigger */ + if (smax > UB2MAXVAL+1) { + fprintf(f, "static const unsigned long %s_", which); + if (parser) + fprintf(f, "%s_", parser); + fprintf(f, "scramble[] = {\n"); + for (i=0; i<=UB1MAXVAL; i+=4) + fprintf(f, "0x%.8lx, 0x%.8lx, 0x%.8lx, 0x%.8lx,\n", + scramble[i+0], scramble[i+1], scramble[i+2], scramble[i+3]); + } else { + fprintf(f, "static const unsigned short %s_", which); + if (parser) + fprintf(f, "%s_", parser); + fprintf(f, "scramble[] = {\n"); + for (i=0; i<=UB1MAXVAL; i+=8) + fprintf(f, "0x%.4lx, 0x%.4lx, 0x%.4lx, 0x%.4lx, 0x%.4lx, 0x%.4lx, 0x%.4lx, 0x%.4lx,\n", - scramble[i+0], scramble[i+1], scramble[i+2], scramble[i+3], - scramble[i+4], scramble[i+5], scramble[i+6], scramble[i+7]); - } - fprintf(f, "};\n"); - fprintf(f, "\n"); + scramble[i+0], scramble[i+1], scramble[i+2], scramble[i+3], + scramble[i+4], scramble[i+5], scramble[i+6], scramble[i+7]); + } + fprintf(f, "};\n"); + fprintf(f, "\n"); } if (blen > 0) { - /* small adjustments to _a_ to make values distinct */ - if (smax <= UB1MAXVAL+1 || blen >= USE_SCRAMBLE) - fprintf(f, "static const unsigned char %s_", which); - else - fprintf(f, "static const unsigned short %s_", which); - if (parser) - fprintf(f, "%s_", parser); - fprintf(f, "tab[] = {\n"); - - if (blen < 16) { - for (i=0; i= USE_SCRAMBLE) + fprintf(f, "static const unsigned char %s_", which); + else + fprintf(f, "static const unsigned short %s_", which); + if (parser) + fprintf(f, "%s_", parser); + fprintf(f, "tab[] = {\n"); + + if (blen < 16) { + for (i=0; iname_k = yasm__xstrdup(d->name); - k->len_k = (ub4)strlen(d->name); - k->next_k = keys; - keys = k; - nkeys++; + k->name_k = yasm__xstrdup(d->name); + k->len_k = (ub4)strlen(d->name); + k->next_k = keys; + keys = k; + nkeys++; } /* find the hash */ findhash(&tab, &tabh, &alen, &blen, &salt, &final, - scramble, &smax, keys, nkeys, &form); + scramble, &smax, keys, nkeys, &form); /* output the dir table: this should loop up to smax for NORMAL_HP, * or up to pakd.nkeys for MINIMAL_HP. */ fprintf(out, "static const %s_parse_data %s_", which, which); if (parser) - fprintf(out, "%s_", parser); + fprintf(out, "%s_", parser); fprintf(out, "pd[%lu] = {\n", nkeys); for (i=0; iname, tabh[i].key_h->name_k) == 0) - break; - } - if (!d) { - report_error("internal error: could not find `%s'", - tabh[i].key_h->name_k); - break; - } - if (cpumode) - fprintf(out, "{\"%s\",", d->name); - else - fprintf(out, "%s(\"%s\",", d->func, d->name); - STAILQ_FOREACH(sv, &d->args, link) { - fprintf(out, " %s", sv->str); - if (STAILQ_NEXT(sv, link)) - fprintf(out, ","); - } - fprintf(out, cpumode ? "}" : ")"); - } else - fprintf(out, " { NULL }"); - - if (i < nkeys-1) - fprintf(out, ","); - fprintf(out, "\n"); + if (tabh[i].key_h) { + sval *sv; + STAILQ_FOREACH(d, dirs, link) { + if (strcmp(d->name, tabh[i].key_h->name_k) == 0) + break; + } + if (!d) { + report_error("internal error: could not find `%s'", + tabh[i].key_h->name_k); + break; + } + if (cpumode) + fprintf(out, "{\"%s\",", d->name); + else + fprintf(out, "%s(\"%s\",", d->func, d->name); + STAILQ_FOREACH(sv, &d->args, link) { + fprintf(out, " %s", sv->str); + if (STAILQ_NEXT(sv, link)) + fprintf(out, ","); + } + fprintf(out, cpumode ? "}" : ")"); + } else + fprintf(out, " { NULL }"); + + if (i < nkeys-1) + fprintf(out, ","); + fprintf(out, "\n"); } fprintf(out, "};\n"); @@ -696,20 +696,20 @@ perfect_dir(FILE *out, const char *which, const char *parser, dir_list *dirs) /* The hash function */ fprintf(out, "#define tab %s_", which); if (parser) - fprintf(out, "%s_", parser); + fprintf(out, "%s_", parser); fprintf(out, "tab\n"); fprintf(out, "static const %s_parse_data *\n%s_", which, which); if (parser) - fprintf(out, "%s_", parser); + fprintf(out, "%s_", parser); fprintf(out, "find(const char *key, size_t len)\n"); fprintf(out, "{\n"); fprintf(out, " const %s_parse_data *ret;\n", which); for (i=0; i= %lu) return NULL;\n", nkeys); fprintf(out, " ret = &%s_", which); if (parser) - fprintf(out, "%s_", parser); + fprintf(out, "%s_", parser); fprintf(out, "pd[rsl];\n"); fprintf(out, " if (strcmp(key, ret->name) != 0) return NULL;\n"); fprintf(out, " return ret;\n"); @@ -729,26 +729,26 @@ static int get_line(FILE *in) cur_line = next_line; if (feof(in)) - return 0; + return 0; while (p < &line[1023-128]) { - if (!fgets(p, 128, in)) - return 1; - next_line++; - /* if continuation, strip out leading whitespace */ - if (p > line) { - char *p2 = p; - while (isspace(*p2)) p2++; - if (p2 > p) - memmove(p, p2, strlen(p2)+1); - } - while (*p) p++; - if (p[-2] != '\\' || p[-1] != '\n') { - if (p[-1] == '\n') - p[-1] = '\0'; - return 1; - } - p -= 2; + if (!fgets(p, 128, in)) + return 1; + next_line++; + /* if continuation, strip out leading whitespace */ + if (p > line) { + char *p2 = p; + while (isspace(*p2)) p2++; + if (p2 > p) + memmove(p, p2, strlen(p2)+1); + } + while (*p) p++; + if (p[-2] != '\\' || p[-1] != '\n') { + if (p[-1] == '\n') + p[-1] = '\0'; + return 1; + } + p -= 2; } return 0; } @@ -781,17 +781,17 @@ main(int argc, char *argv[]) dir_byp *db; for (i=0; i \n"); - return EXIT_FAILURE; + fprintf(stderr, "Usage: gap \n"); + return EXIT_FAILURE; } in = fopen(argv[1], "rt"); if (!in) { - fprintf(stderr, "Could not open `%s' for reading\n", argv[1]); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s' for reading\n", argv[1]); + return EXIT_FAILURE; } STAILQ_INIT(&insnprefix_byp); @@ -800,54 +800,54 @@ main(int argc, char *argv[]) /* Parse input file */ while (get_line(in)) { - int found; - /*printf("%s\n", line);*/ - tok = strtok(line, " \t\n"); - if (!tok) - continue; - - /* Comments start with # as the first thing on a line */ - if (tok[0] == '#') - continue; - - /* Look for directive */ - found = 0; - for (i=0; i 0) - return EXIT_FAILURE; + return EXIT_FAILURE; out = fopen(argv[2], "wt"); if (!out) { - fprintf(stderr, "Could not open `%s' for writing\n", argv[2]); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s' for writing\n", argv[2]); + return EXIT_FAILURE; } /* Get perfect hashes for the three lists of directives */ STAILQ_FOREACH(db, &insnprefix_byp, link) - perfect_dir(out, "insnprefix", db->parser, &db->dirs); + perfect_dir(out, "insnprefix", db->parser, &db->dirs); STAILQ_FOREACH(db, &cpu_byp, link) - perfect_dir(out, "cpu", db->parser, &db->dirs); + perfect_dir(out, "cpu", db->parser, &db->dirs); STAILQ_FOREACH(db, ®tmod_byp, link) - perfect_dir(out, "regtmod", db->parser, &db->dirs); + perfect_dir(out, "regtmod", db->parser, &db->dirs); if (errors > 0) - return EXIT_FAILURE; + return EXIT_FAILURE; return EXIT_SUCCESS; } diff --git a/tools/gap/perfect.c b/tools/gap/perfect.c index c56304f6..d1218040 100644 --- a/tools/gap/perfect.c +++ b/tools/gap/perfect.c @@ -122,10 +122,10 @@ static void checkdup( { case STRING_HT: if ((key1->len_k == key2->len_k) && - !memcmp(key1->name_k, key2->name_k, (size_t)key1->len_k)) + !memcmp(key1->name_k, key2->name_k, (size_t)key1->len_k)) { fprintf(stderr, "perfect.c: Duplicates keys! %.*s\n", - (int)key1->len_k, key1->name_k); + (int)key1->len_k, key1->name_k); exit(EXIT_FAILURE); } break; @@ -138,7 +138,7 @@ static void checkdup( break; case AB_HT: fprintf(stderr, "perfect.c: Duplicate keys! %.8lx %.8lx\n", - key1->a_k, key1->b_k); + key1->a_k, key1->b_k); exit(EXIT_FAILURE); break; default: @@ -171,15 +171,15 @@ static int inittab( key *otherkey; for (otherkey=tabb[mykey->b_k].list_b; - otherkey; - otherkey=otherkey->nextb_k) + otherkey; + otherkey=otherkey->nextb_k) { if (mykey->a_k == otherkey->a_k) { nocollision = FALSE; - checkdup(mykey, otherkey, form); - if (!complete) - return FALSE; + checkdup(mykey, otherkey, form); + if (!complete) + return FALSE; } } ++tabb[mykey->b_k].listlen_b; @@ -216,14 +216,14 @@ static void initnorm( } final->used = 4; sprintf(final->line[0], - " unsigned long i,state[CHECKSTATE],rsl;\n"); + " unsigned long i,state[CHECKSTATE],rsl;\n"); sprintf(final->line[1], - " for (i=0; iline[2], - " phash_checksum(key, len, state);\n"); + " phash_checksum(key, len, state);\n"); sprintf(final->line[3], - " rsl = ((state[0]&0x%lx)^scramble[tab[state[1]&0x%lx]]);\n", - alen-1, blen-1); + " rsl = ((state[0]&0x%lx)^scramble[tab[state[1]&0x%lx]]);\n", + alen-1, blen-1); } else { @@ -238,7 +238,7 @@ static void initnorm( } final->used = 2; sprintf(final->line[0], - " unsigned long rsl, val = phash_lookup(key, len, 0x%lxUL);\n", initlev); + " unsigned long rsl, val = phash_lookup(key, len, 0x%lxUL);\n", initlev); if (smax <= 1) { sprintf(final->line[1], " rsl = 0;\n"); @@ -246,12 +246,12 @@ static void initnorm( else if (blen < USE_SCRAMBLE) { sprintf(final->line[1], " rsl = ((val>>%ld)^tab[val&0x%lx]);\n", - UB4BITS-phash_log2(alen), blen-1); + UB4BITS-phash_log2(alen), blen-1); } else { sprintf(final->line[1], " rsl = ((val>>%ld)^scramble[tab[val&0x%lx]]);\n", - UB4BITS-phash_log2(alen), blen-1); + UB4BITS-phash_log2(alen), blen-1); } } } @@ -293,12 +293,12 @@ static void initinl( else if (blen < USE_SCRAMBLE) { sprintf(final->line[0], " unsigned long rsl = ((val & 0x%lx) ^ tab[val >> %ld]);\n", - amask, UB4BITS-blog); + amask, UB4BITS-blog); } else { sprintf(final->line[0], " unsigned long rsl = ((val & 0x%lx) ^ scramble[tab[val >> %ld]]);\n", - amask, UB4BITS-blog); + amask, UB4BITS-blog); } } @@ -369,7 +369,7 @@ static void duplicates( for (i=0; inextb_k) for (key2=key1->nextb_k; key2; key2=key2->nextb_k) - checkdup(key1, key2, form); + checkdup(key1, key2, form); } @@ -403,7 +403,7 @@ static int apply( hash = mykey->a_k^stabb; if (mykey == tabh[hash].key_h) { /* erase hash for all of child's siblings */ - tabh[hash].key_h = (key *)0; + tabh[hash].key_h = (key *)0; } } @@ -417,13 +417,13 @@ static int apply( hash = mykey->a_k^stabb; if (rollback) { - if (parent == 0) continue; /* root never had a hash */ + if (parent == 0) continue; /* root never had a hash */ } else if (tabh[hash].key_h) { - /* very rare: roll back any changes */ - apply(tabb, tabh, tabq, blen, scramble, tail, TRUE); - return FALSE; /* failure, collision */ + /* very rare: roll back any changes */ + apply(tabb, tabh, tabq, blen, scramble, tail, TRUE); + return FALSE; /* failure, collision */ } tabh[hash].key_h = mykey; } @@ -489,26 +489,26 @@ static int augment( for (mykey = myb->list_b; mykey; mykey=mykey->nextb_k) { - key *childkey; - ub4 hash = mykey->a_k^scramble[i]; - - if (hash >= highhash) break; /* out of bounds */ - childkey = tabh[hash].key_h; - - if (childkey) - { - bstuff *hitb = &tabb[childkey->b_k]; - - if (childb) - { - if (childb != hitb) break; /* hit at most one child b */ - } - else - { - childb = hitb; /* remember this as childb */ - if (childb->water_b == highwater) break; /* already explored */ - } - } + key *childkey; + ub4 hash = mykey->a_k^scramble[i]; + + if (hash >= highhash) break; /* out of bounds */ + childkey = tabh[hash].key_h; + + if (childkey) + { + bstuff *hitb = &tabb[childkey->b_k]; + + if (childb) + { + if (childb != hitb) break; /* hit at most one child b */ + } + else + { + childb = hitb; /* remember this as childb */ + if (childb->water_b == highwater) break; /* already explored */ + } + } } if (mykey) continue; /* myb with i has multiple collisions */ @@ -522,11 +522,11 @@ static int augment( if (!childb) { /* found an *i* with no collisions? */ - /* try to apply the augmenting path */ - if (apply(tabb, tabh, tabq, blen, scramble, tail, FALSE)) - return TRUE; /* success, item was added to the perfect hash */ + /* try to apply the augmenting path */ + if (apply(tabb, tabh, tabq, blen, scramble, tail, FALSE)) + return TRUE; /* success, item was added to the perfect hash */ - --tail; /* don't know how to handle such a child! */ + --tail; /* don't know how to handle such a child! */ } } } @@ -550,8 +550,8 @@ static int perfect( /* clear any state from previous attempts */ memset((void *)tabh, 0, - (size_t)(sizeof(hstuff)* - ((form->perfect == MINIMAL_HP) ? nkeys : smax))); + (size_t)(sizeof(hstuff)* + ((form->perfect == MINIMAL_HP) ? nkeys : smax))); memset((void *)tabq, 0, (size_t)(sizeof(qstuff)*(blen+1))); for (maxkeys=0,i=0; i0; --j) for (i=0; iperfect == MINIMAL_HP ? - nkeys : *smax)); + nkeys : *smax)); /* check that (a,b) are distinct and put them in tabb indexed by b */ (void)inittab(*tabb, *blen, keys, form, FALSE); @@ -641,11 +641,11 @@ static void hash_ab( *smax = *smax * 2; scrambleinit(scramble, *smax); tabh = (hstuff *)yasm_xmalloc(sizeof(hstuff)*(form->perfect == MINIMAL_HP ? - nkeys : *smax)); + nkeys : *smax)); if (!perfect(*tabb, tabh, tabq, *blen, *smax, scramble, nkeys, form)) { - printf("fatal error: Cannot find perfect hash for user (A,B) pairs\n"); - exit(EXIT_FAILURE); + printf("fatal error: Cannot find perfect hash for user (A,B) pairs\n"); + exit(EXIT_FAILURE); } } } @@ -736,10 +736,10 @@ static void initalen( *blen = *smax; /* go for function speed not space */ else if (*smax/4 <= (1<<14)) *blen = ((nkeys <= *smax*0.56) ? *smax/32 : - (nkeys <= *smax*0.74) ? *smax/16 : *smax/8); + (nkeys <= *smax*0.74) ? *smax/16 : *smax/8); else *blen = ((nkeys <= *smax*0.6) ? *smax/16 : - (nkeys <= *smax*0.8) ? *smax/8 : *smax/4); + (nkeys <= *smax*0.8) ? *smax/8 : *smax/4); if ((form->speed == FAST_HS) && (*blen < *smax/8)) *blen = *smax/8; @@ -769,31 +769,31 @@ static void initalen( case 17: if (form->speed == FAST_HS) { - *alen = *smax/2; - *blen = *smax/4; + *alen = *smax/2; + *blen = *smax/4; } else if (*smax/4 < USE_SCRAMBLE) { - *alen = ((nkeys <= *smax*0.52) ? *smax/8 : *smax/4); - *blen = ((nkeys <= *smax*0.52) ? *smax/8 : *smax/4); + *alen = ((nkeys <= *smax*0.52) ? *smax/8 : *smax/4); + *blen = ((nkeys <= *smax*0.52) ? *smax/8 : *smax/4); } else { - *alen = ((nkeys <= *smax*(5.0/8.0)) ? *smax/8 : - (nkeys <= *smax*(3.0/4.0)) ? *smax/4 : *smax/2); - *blen = *smax/4; /* always give the small size a shot */ + *alen = ((nkeys <= *smax*(5.0/8.0)) ? *smax/8 : + (nkeys <= *smax*(3.0/4.0)) ? *smax/4 : *smax/2); + *blen = *smax/4; /* always give the small size a shot */ } break; case 18: if (form->speed == FAST_HS) { - *alen = *smax/2; - *blen = *smax/2; + *alen = *smax/2; + *blen = *smax/2; } else { - *alen = *smax/8; /* never require the multiword hash */ - *blen = (nkeys <= *smax*(5.0/8.0)) ? *smax/4 : *smax/2; + *alen = *smax/8; /* never require the multiword hash */ + *blen = (nkeys <= *smax*(5.0/8.0)) ? *smax/4 : *smax/2; } break; case 19: @@ -816,7 +816,7 @@ static void initalen( */ void findhash( bstuff **tabb, /* output, tab[] of the perfect hash, length *blen */ - hstuff **tabh, /* output, table of keys indexed by hash value */ + hstuff **tabh, /* output, table of keys indexed by hash value */ ub4 *alen, /* output, 0..alen-1 is range for a of (a,b) */ ub4 *blen, /* output, 0..blen-1 is range for b of (a,b) */ ub4 *salt, /* output, initializes initial hash */ @@ -837,7 +837,7 @@ void findhash( if (form->hashtype == AB_HT) { hash_ab(tabb, alen, blen, salt, final, - scramble, smax, keys, nkeys, form); + scramble, smax, keys, nkeys, form); return; } @@ -853,7 +853,7 @@ void findhash( *tabb = (bstuff *)yasm_xmalloc((size_t)(sizeof(bstuff)*(*blen))); tabq = (qstuff *)yasm_xmalloc(sizeof(qstuff)*(*blen+1)); *tabh = (hstuff *)yasm_xmalloc(sizeof(hstuff)*(form->perfect == MINIMAL_HP ? - nkeys : *smax)); + nkeys : *smax)); /* Actually find the perfect hash */ *salt = 0; @@ -865,7 +865,7 @@ void findhash( /* Try to find distinct (A,B) for all keys */ rslinit = initkey(keys, nkeys, *tabb, *alen, *blen, *smax, trysalt, - form, final); + form, final); if (rslinit == 2) { /* initkey actually found a perfect hash, not just distinct (a,b) */ @@ -878,27 +878,27 @@ void findhash( /* didn't find distinct (a,b) */ if (++bad_initkey >= RETRY_INITKEY) { - /* Try to put more bits in (A,B) to make distinct (A,B) more likely */ - if (*alen < maxalen) - { - *alen *= 2; - } - else if (*blen < *smax) - { - *blen *= 2; - free(tabq); - free(*tabb); - *tabb = (bstuff *)yasm_xmalloc((size_t)(sizeof(bstuff)*(*blen))); - tabq = (qstuff *)yasm_xmalloc((size_t)(sizeof(qstuff)*(*blen+1))); - } - else - { - duplicates(*tabb, *blen, keys, form); /* check for duplicates */ - printf("fatal error: Cannot perfect hash: cannot find distinct (A,B)\n"); - exit(EXIT_FAILURE); - } - bad_initkey = 0; - bad_perfect = 0; + /* Try to put more bits in (A,B) to make distinct (A,B) more likely */ + if (*alen < maxalen) + { + *alen *= 2; + } + else if (*blen < *smax) + { + *blen *= 2; + free(tabq); + free(*tabb); + *tabb = (bstuff *)yasm_xmalloc((size_t)(sizeof(bstuff)*(*blen))); + tabq = (qstuff *)yasm_xmalloc((size_t)(sizeof(qstuff)*(*blen+1))); + } + else + { + duplicates(*tabb, *blen, keys, form); /* check for duplicates */ + printf("fatal error: Cannot perfect hash: cannot find distinct (A,B)\n"); + exit(EXIT_FAILURE); + } + bad_initkey = 0; + bad_perfect = 0; } continue; /* two keys have same (a,b) pair */ } @@ -909,23 +909,23 @@ void findhash( if (!perfect(*tabb, *tabh, tabq, *blen, *smax, scramble, nkeys, form)) { if ((form->hashtype != INT_HT && ++bad_perfect >= RETRY_PERFECT) || - (form->hashtype == INT_HT && ++bad_perfect >= RETRY_HEX)) + (form->hashtype == INT_HT && ++bad_perfect >= RETRY_HEX)) { - if (*blen < *smax) - { - *blen *= 2; - free(*tabb); - free(tabq); - *tabb = (bstuff *)yasm_xmalloc((size_t)(sizeof(bstuff)*(*blen))); - tabq = (qstuff *)yasm_xmalloc((size_t)(sizeof(qstuff)*(*blen+1))); - --trysalt; /* we know this salt got distinct (A,B) */ - } - else - { - printf("fatal error: Cannot perfect hash: cannot build tab[]\n"); - exit(EXIT_FAILURE); - } - bad_perfect = 0; + if (*blen < *smax) + { + *blen *= 2; + free(*tabb); + free(tabq); + *tabb = (bstuff *)yasm_xmalloc((size_t)(sizeof(bstuff)*(*blen))); + tabq = (qstuff *)yasm_xmalloc((size_t)(sizeof(qstuff)*(*blen+1))); + --trysalt; /* we know this salt got distinct (A,B) */ + } + else + { + printf("fatal error: Cannot perfect hash: cannot build tab[]\n"); + exit(EXIT_FAILURE); + } + bad_perfect = 0; } continue; } @@ -1045,37 +1045,37 @@ hashform *form; /* user directives */ else if (blen <= 1024) { for (i=0; i0?"":"|"); + printf("%sEXTERN", first-->0?"":"|"); if (syment->e_flags & XDF_SYM_GLOBAL) - printf("%sGLOBAL", first-->0?"":"|"); + printf("%sGLOBAL", first-->0?"":"|"); if (syment->e_flags & XDF_SYM_EQU) - printf("%sEQU", first-->0?"":"|"); + printf("%sEQU", first-->0?"":"|"); if (first>0) - printf("None"); + printf("None"); printf(" Name=`%s' Section=", get_syment_name(syment, f)); if ((long)syment->e_sect_idx < 0) - printf("%d\n", syment->e_sect_idx); + printf("%d\n", syment->e_sect_idx); else - printf("`%s' (%d)\n", - get_sect_name(syment->e_sect_idx, f, symtab_off, secttab_off), - syment->e_sect_idx); + printf("`%s' (%d)\n", + get_sect_name(syment->e_sect_idx, f, symtab_off, secttab_off), + syment->e_sect_idx); } void @@ -101,27 +101,27 @@ print_reloc(const RELOCATION_ENTRY *relocent, FILE *f, size_t symtab_off) { const char *type = "UNK"; switch (relocent->r_type) { - case XDF_RELOC_REL: - type = "REL"; - break; - case XDF_RELOC_WRT: - type = "WRT"; - break; - case XDF_RELOC_RIP: - type = "RIP"; - break; - case XDF_RELOC_SEG: - type = "SEG"; - break; + case XDF_RELOC_REL: + type = "REL"; + break; + case XDF_RELOC_WRT: + type = "WRT"; + break; + case XDF_RELOC_RIP: + type = "RIP"; + break; + case XDF_RELOC_SEG: + type = "SEG"; + break; } printf("\t Offset=0x%08X Type=%s Size=%d Shift=%d Target=`%s' (%d)", - relocent->r_off, type, relocent->r_size, relocent->r_shift, - get_sym_name(relocent->r_targ_idx, f, symtab_off), - relocent->r_targ_idx); + relocent->r_off, type, relocent->r_size, relocent->r_shift, + get_sym_name(relocent->r_targ_idx, f, symtab_off), + relocent->r_targ_idx); if (relocent->r_type == XDF_RELOC_WRT) - printf(" Base=`%s' (%d)", - get_sym_name(relocent->r_base_idx, f, symtab_off), - relocent->r_base_idx); + printf(" Base=`%s' (%d)", + get_sym_name(relocent->r_base_idx, f, symtab_off), + relocent->r_base_idx); printf("\n"); } @@ -131,54 +131,54 @@ print_section(const SECTION_HEADER *secthead, FILE *f, size_t symtab_off) int first = 1; u32 i; printf("Section `%s' (section name symtab %d):\n", - get_sym_name(secthead->s_name_idx, f, symtab_off), - secthead->s_name_idx); + get_sym_name(secthead->s_name_idx, f, symtab_off), + secthead->s_name_idx); printf("\tPhysical Address=0x%016llX\n", secthead->s_addr); printf("\tVirtual Address=0x%016llX\n", secthead->s_vaddr); printf("\tAlign=%d\n", secthead->s_align); printf("\tFlags="); if (secthead->s_flags & XDF_SECT_ABSOLUTE) - printf("%sABSOLUTE", first-->0?"":"|"); + printf("%sABSOLUTE", first-->0?"":"|"); if (secthead->s_flags & XDF_SECT_FLAT) - printf("%sFLAT", first-->0?"":"|"); + printf("%sFLAT", first-->0?"":"|"); if (secthead->s_flags & XDF_SECT_BSS) - printf("%sBSS", first-->0?"":"|"); + printf("%sBSS", first-->0?"":"|"); if (secthead->s_flags & XDF_SECT_USE_16) - printf("%sUSE16", first-->0?"":"|"); + printf("%sUSE16", first-->0?"":"|"); if (secthead->s_flags & XDF_SECT_USE_32) - printf("%sUSE32", first-->0?"":"|"); + printf("%sUSE32", first-->0?"":"|"); if (secthead->s_flags & XDF_SECT_USE_64) - printf("%sUSE64", first-->0?"":"|"); + printf("%sUSE64", first-->0?"":"|"); if (first>0) - printf("None"); + printf("None"); printf("\n\tData Offset=0x%08X\n", secthead->s_data_off); printf("\tData Size=%d\n", secthead->s_data_size); if (!(secthead->s_flags & XDF_SECT_BSS) && secthead->s_data_size > 0) { - printf("\tSection Data:"); - long oldpos = ftell(f); - fseek(f, secthead->s_data_off, SEEK_SET); - for (i=0; is_data_size; i++) { - if (i % 16 == 0) - printf("\n\t\t%08X:", i); - if (i % 2 == 0) - printf(" "); - printf("%02X", fgetc(f)); - } - printf("\n"); - fseek(f, oldpos, SEEK_SET); + printf("\tSection Data:"); + long oldpos = ftell(f); + fseek(f, secthead->s_data_off, SEEK_SET); + for (i=0; is_data_size; i++) { + if (i % 16 == 0) + printf("\n\t\t%08X:", i); + if (i % 2 == 0) + printf(" "); + printf("%02X", fgetc(f)); + } + printf("\n"); + fseek(f, oldpos, SEEK_SET); } printf("\tReloc Table Offset=0x%08X\n", secthead->s_reltab_off); printf("\tNum Relocs=%d\n", secthead->s_num_reloc); if (secthead->s_num_reloc > 0) { - printf("\tRelocations:\n"); - long oldpos = ftell(f); - fseek(f, secthead->s_reltab_off, SEEK_SET); - for (i=0; is_num_reloc; i++) { - RELOCATION_ENTRY relocent; - fread(&relocent, sizeof(relocent), 1, f); - print_reloc(&relocent, f, symtab_off); - } - fseek(f, oldpos, SEEK_SET); + printf("\tRelocations:\n"); + long oldpos = ftell(f); + fseek(f, secthead->s_reltab_off, SEEK_SET); + for (i=0; is_num_reloc; i++) { + RELOCATION_ENTRY relocent; + fread(&relocent, sizeof(relocent), 1, f); + print_reloc(&relocent, f, symtab_off); + } + fseek(f, oldpos, SEEK_SET); } } @@ -191,37 +191,37 @@ main(int argc, char **argv) u32 i; if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - return EXIT_FAILURE; + fprintf(stderr, "Usage: %s \n", argv[0]); + return EXIT_FAILURE; } f = fopen(argv[1], "rb"); if (!f) { - fprintf(stderr, "Could not open `%s'\n", argv[1]); - return EXIT_FAILURE; + fprintf(stderr, "Could not open `%s'\n", argv[1]); + return EXIT_FAILURE; } fread(&filehead, sizeof(filehead), 1, f); if (filehead.f_magic != XDF_MAGIC) { - fprintf(stderr, "Magic number mismatch (expected %08X, got %08X\n", - XDF_MAGIC, filehead.f_magic); - return EXIT_FAILURE; + fprintf(stderr, "Magic number mismatch (expected %08X, got %08X\n", + XDF_MAGIC, filehead.f_magic); + return EXIT_FAILURE; } print_file_header(&filehead); symtab_off = sizeof(FILE_HEADER)+filehead.f_nsect*sizeof(SECTION_HEADER); for (i=0; i #ifdef lint -# define _(String) String +# define _(String) String #else # ifdef HAVE_LOCALE_H # include @@ -71,43 +71,43 @@ # ifdef ENABLE_NLS # include -# define _(String) gettext(String) +# define _(String) gettext(String) # else -# define gettext(Msgid) (Msgid) -# define dgettext(Domainname, Msgid) (Msgid) +# define gettext(Msgid) (Msgid) +# define dgettext(Domainname, Msgid) (Msgid) # define dcgettext(Domainname, Msgid, Category) (Msgid) -# define textdomain(Domainname) while (0) /* nothing */ -# define bindtextdomain(Domainname, Dirname) while (0) /* nothing */ -# define _(String) (String) +# define textdomain(Domainname) while (0) /* nothing */ +# define bindtextdomain(Domainname, Dirname) while (0) /* nothing */ +# define _(String) (String) # endif #endif #ifdef gettext_noop -# define N_(String) gettext_noop(String) +# define N_(String) gettext_noop(String) #else -# define N_(String) (String) +# define N_(String) (String) #endif #ifdef HAVE_MERGESORT -#define yasm__mergesort(a, b, c, d) mergesort(a, b, c, d) +#define yasm__mergesort(a, b, c, d) mergesort(a, b, c, d) #endif #ifdef HAVE_STRSEP -#define yasm__strsep(a, b) strsep(a, b) +#define yasm__strsep(a, b) strsep(a, b) #endif #ifdef HAVE_STRCASECMP -# define yasm__strcasecmp(x, y) strcasecmp(x, y) -# define yasm__strncasecmp(x, y, n) strncasecmp(x, y, n) +# define yasm__strcasecmp(x, y) strcasecmp(x, y) +# define yasm__strncasecmp(x, y, n) strncasecmp(x, y, n) #elif HAVE_STRICMP -# define yasm__strcasecmp(x, y) stricmp(x, y) -# define yasm__strncasecmp(x, y, n) strnicmp(x, y, n) +# define yasm__strcasecmp(x, y) stricmp(x, y) +# define yasm__strncasecmp(x, y, n) strnicmp(x, y, n) #elif HAVE__STRICMP -# define yasm__strcasecmp(x, y) _stricmp(x, y) -# define yasm__strncasecmp(x, y, n) _strnicmp(x, y, n) +# define yasm__strcasecmp(x, y) _stricmp(x, y) +# define yasm__strncasecmp(x, y, n) _strnicmp(x, y, n) #elif HAVE_STRCMPI -# define yasm__strcasecmp(x, y) strcmpi(x, y) -# define yasm__strncasecmp(x, y, n) strncmpi(x, y, n) +# define yasm__strcasecmp(x, y) strcmpi(x, y) +# define yasm__strncasecmp(x, y, n) strncmpi(x, y, n) #else # define USE_OUR_OWN_STRCASECMP #endif @@ -123,37 +123,37 @@ #endif #ifdef __RCSID -# define RCSID(s) __RCSID(s) +# define RCSID(s) __RCSID(s) #elif defined(__GNUC__) && defined(__ELF__) -# define RCSID(s) __asm__(".ident\t\"" s "\"") +# define RCSID(s) __asm__(".ident\t\"" s "\"") #else -# define RCSID(s) static const char rcsid[] = s +# define RCSID(s) static const char rcsid[] = s #endif #ifdef WITH_DMALLOC # include -# define yasm__xstrdup(str) xstrdup(str) -# define yasm_xmalloc(size) xmalloc(size) -# define yasm_xcalloc(count, size) xcalloc(count, size) -# define yasm_xrealloc(ptr, size) xrealloc(ptr, size) -# define yasm_xfree(ptr) xfree(ptr) +# define yasm__xstrdup(str) xstrdup(str) +# define yasm_xmalloc(size) xmalloc(size) +# define yasm_xcalloc(count, size) xcalloc(count, size) +# define yasm_xrealloc(ptr, size) xrealloc(ptr, size) +# define yasm_xfree(ptr) xfree(ptr) #endif /* Bit-counting: used primarily by HAMT but also in a few other places. */ -#define BC_TWO(c) (0x1ul << (c)) -#define BC_MSK(c) (((unsigned long)(-1)) / (BC_TWO(BC_TWO(c)) + 1ul)) -#define BC_COUNT(x,c) ((x) & BC_MSK(c)) + (((x) >> (BC_TWO(c))) & BC_MSK(c)) -#define BitCount(d, s) do { \ - d = BC_COUNT(s, 0); \ - d = BC_COUNT(d, 1); \ - d = BC_COUNT(d, 2); \ - d = BC_COUNT(d, 3); \ - d = BC_COUNT(d, 4); \ +#define BC_TWO(c) (0x1ul << (c)) +#define BC_MSK(c) (((unsigned long)(-1)) / (BC_TWO(BC_TWO(c)) + 1ul)) +#define BC_COUNT(x,c) ((x) & BC_MSK(c)) + (((x) >> (BC_TWO(c))) & BC_MSK(c)) +#define BitCount(d, s) do { \ + d = BC_COUNT(s, 0); \ + d = BC_COUNT(d, 1); \ + d = BC_COUNT(d, 2); \ + d = BC_COUNT(d, 3); \ + d = BC_COUNT(d, 4); \ } while (0) /** Determine if a value is exactly a power of 2. Zero is treated as a power * of two. - * \param x value + * \param x value * \return Nonzero if x is a power of 2. */ #define is_exp2(x) ((x & (x - 1)) == 0) @@ -161,10 +161,10 @@ #ifndef NELEMS /** Get the number of elements in an array. * \internal - * \param array array + * \param array array * \return Number of elements. */ -#define NELEMS(array) (sizeof(array) / sizeof(array[0])) +#define NELEMS(array) (sizeof(array) / sizeof(array[0])) #endif #endif