From 70055964fc24699c07c515dd9a603196ba06a654 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Thu, 11 Oct 2007 00:05:31 -0700 Subject: [PATCH] Additional uses of bool and enum Proper use of bool and enum makes code easier to debug. Do more of it. In particular, we really should stomp out any residual uses of magic constants that aren't enums or, in some cases, even #defines. --- labels.c | 25 +++++++++++-------------- labels.h | 13 +++++++++---- nasm.c | 18 ++++++++++-------- nasm.h | 8 ++++---- nasmlib.c | 6 +++--- nasmlib.h | 4 ++-- ndisasm.c | 2 +- output/outbin.c | 1 - output/outelf32.c | 10 +++++----- output/outelf64.c | 8 ++++---- output/outobj.c | 9 +++++---- output/outrdf2.c | 11 ++++++----- preproc.c | 30 ++++++++++++++++-------------- stdscan.c | 2 +- 14 files changed, 77 insertions(+), 70 deletions(-) diff --git a/labels.c b/labels.c index 365330a3..d907b352 100644 --- a/labels.c +++ b/labels.c @@ -75,7 +75,7 @@ struct permts { /* permanent text storage */ char data[PERMTS_SIZE]; /* ... the data block itself */ }; -extern int global_offset_changed; /* defined in nasm.c */ +extern bool global_offset_changed; /* defined in nasm.c */ static struct hash_table *ltab; /* labels hash table */ static union label *ldata; /* all label data blocks */ @@ -88,7 +88,7 @@ static char *perm_copy(const char *string); static char *prevlabel; -static int initialized = false; +static bool initialized = false; char lprefix[PREFIX_MAX] = { 0 }; char lpostfix[PREFIX_MAX] = { 0 }; @@ -146,38 +146,35 @@ static union label *find_label(char *label, int create) return lfree++; } -int lookup_label(char *label, int32_t *segment, int32_t *offset) +bool lookup_label(char *label, int32_t *segment, int32_t *offset) { union label *lptr; if (!initialized) - return 0; + return false; lptr = find_label(label, 0); if (lptr && (lptr->defn.is_global & DEFINED_BIT)) { *segment = lptr->defn.segment; *offset = lptr->defn.offset; - return 1; + return true; } else - return 0; + return false; } -int is_extern(char *label) +bool is_extern(char *label) { union label *lptr; if (!initialized) - return 0; + return false; lptr = find_label(label, 0); - if (lptr && (lptr->defn.is_global & EXTERN_BIT)) - return 1; - else - return 0; + return (lptr && (lptr->defn.is_global & EXTERN_BIT)); } void redefine_label(char *label, int32_t segment, int32_t offset, char *special, - int is_norm, int isextrn, struct ofmt *ofmt, + bool is_norm, bool isextrn, struct ofmt *ofmt, efunc error) { union label *lptr; @@ -250,7 +247,7 @@ void redefine_label(char *label, int32_t segment, int32_t offset, char *special, } void define_label(char *label, int32_t segment, int32_t offset, char *special, - int is_norm, int isextrn, struct ofmt *ofmt, efunc error) + bool is_norm, bool isextrn, struct ofmt *ofmt, efunc error) { union label *lptr; int exi; diff --git a/labels.h b/labels.h index 91232dd8..327fb0f1 100644 --- a/labels.h +++ b/labels.h @@ -6,19 +6,24 @@ * distributed in the NASM archive. */ +#ifndef LABELS_H +#define LABELS_H + extern char lprefix[PREFIX_MAX]; extern char lpostfix[PREFIX_MAX]; -int lookup_label(char *label, int32_t *segment, int32_t *offset); -int is_extern(char *label); +bool lookup_label(char *label, int32_t *segment, int32_t *offset); +bool is_extern(char *label); void define_label(char *label, int32_t segment, int32_t offset, char *special, - int is_norm, int isextrn, struct ofmt *ofmt, + bool is_norm, bool isextrn, struct ofmt *ofmt, efunc error); void redefine_label(char *label, int32_t segment, int32_t offset, char *special, - int is_norm, int isextrn, struct ofmt *ofmt, + bool is_norm, bool isextrn, struct ofmt *ofmt, efunc error); void define_common(char *label, int32_t segment, int32_t size, char *special, struct ofmt *ofmt, efunc error); void declare_as_global(char *label, char *special, efunc error); int init_labels(void); void cleanup_labels(void); + +#endif /* LABELS_H */ diff --git a/nasm.c b/nasm.c index 3b7dfd17..fa13460a 100644 --- a/nasm.c +++ b/nasm.c @@ -37,7 +37,6 @@ static int get_bits(char *value); static uint32_t get_cpu(char *cpu_str); static void parse_cmdline(int, char **); static void assemble_file(char *); -static int getkw(char **directive, char **value); static void register_output_formats(void); static void report_error_gnu(int severity, const char *fmt, ...); static void report_error_vc(int severity, const char *fmt, ...); @@ -68,7 +67,7 @@ int optimizing = -1; /* number of optimization passes to take */ static int sb, cmd_sb = 16; /* by default */ static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */ static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */ -int global_offset_changed; /* referenced in labels.c */ +bool global_offset_changed; /* referenced in labels.c */ static struct location location; int in_abs_seg; /* Flag we are in ABSOLUTE seg */ @@ -817,7 +816,7 @@ static void parse_cmdline(int argc, char **argv) } /* List of directives */ -enum { +enum directives { D_NONE, D_ABSOLUTE, D_BITS, D_COMMON, D_CPU, D_DEBUG, D_DEFAULT, D_EXTERN, D_GLOBAL, D_LIST, D_SECTION, D_SEGMENT, D_WARNING }; @@ -825,12 +824,14 @@ static const char *directives[] = { "", "absolute", "bits", "common", "cpu", "debug", "default", "extern", "global", "list", "section", "segment", "warning" }; +static enum directives getkw(char **directive, char **value); static void assemble_file(char *fname) { char *directive, *value, *p, *q, *special, *line, debugid[80]; insn output_ins; - int i, rn_error, validid; + int i, validid; + bool rn_error; int32_t seg, offs; struct tokenval tokval; expr *e; @@ -876,16 +877,17 @@ static void assemble_file(char *fname) location.offset = offs = GET_CURR_OFFS; while ((line = preproc->getline())) { + enum directives d; globallineno++; /* here we parse our directives; this is not handled by the 'real' * parser. */ directive = line; - i = getkw(&directive, &value); - if (i) { + d = getkw(&directive, &value); + if (d) { int err = 0; - switch (i) { + switch (d) { case D_SEGMENT: /* [SEGMENT n] */ case D_SECTION: seg = ofmt->section(value, pass2, &sb); @@ -1419,7 +1421,7 @@ static void assemble_file(char *fname) #endif } /* exit from assemble_file (...) */ -static int getkw(char **directive, char **value) +static enum directives getkw(char **directive, char **value) { char *p, *q, *buf; diff --git a/nasm.h b/nasm.h index c9ec461a..5749b388 100644 --- a/nasm.h +++ b/nasm.h @@ -60,7 +60,7 @@ struct ofmt; /* * A label-lookup function should look like this. */ -typedef int (*lfunc) (char *label, int32_t *segment, int32_t *offset); +typedef bool (*lfunc) (char *label, int32_t *segment, int32_t *offset); /* * And a label-definition function like this. The boolean parameter @@ -69,7 +69,7 @@ typedef int (*lfunc) (char *label, int32_t *segment, int32_t *offset); * an EQU or a segment-base symbol, which shouldn't. */ typedef void (*ldfunc) (char *label, int32_t segment, int32_t offset, - char *special, int is_norm, int isextrn, + char *special, bool is_norm, bool isextrn, struct ofmt * ofmt, efunc error); /* @@ -721,8 +721,8 @@ struct ofmt { * been an EXTERN, a COMMON or a GLOBAL. The distinction should * be obvious to the output format from the other parameters. */ - void (*symdef) (char *name, int32_t segment, int32_t offset, int is_global, - char *special); + void (*symdef) (char *name, int32_t segment, int32_t offset, + int is_global, char *special); /* * This procedure is called when the source code requests a diff --git a/nasmlib.c b/nasmlib.c index 7e7441b4..8c81d6c0 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -196,13 +196,13 @@ char *nasm_strsep(char **stringp, const char *delim) #define lib_isnumchar(c) ( isalnum(c) || (c) == '$') #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0') -int64_t readnum(char *str, int *error) +int64_t readnum(char *str, bool *error) { char *r = str, *q; int32_t radix; uint64_t result, checklimit; int digit, last; - int warn = false; + bool warn = false; int sign = 1; *error = false; @@ -293,7 +293,7 @@ int64_t readnum(char *str, int *error) return result * sign; } -int64_t readstrnum(char *str, int length, int *warn) +int64_t readstrnum(char *str, int length, bool *warn) { int64_t charconst = 0; int i; diff --git a/nasmlib.h b/nasmlib.h index 3f839cf2..14b28677 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -130,7 +130,7 @@ char *nasm_strsep(char **stringp, const char *delim); * Convert a string into a number, using NASM number rules. Sets * `*error' to true if an error occurs, and false otherwise. */ -int64_t readnum(char *str, int *error); +int64_t readnum(char *str, bool *error); /* * Convert a character constant into a number. Sets @@ -138,7 +138,7 @@ int64_t readnum(char *str, int *error); * str points to and length covers the middle of the string, * without the quotes. */ -int64_t readstrnum(char *str, int length, int *warn); +int64_t readstrnum(char *str, int length, bool *warn); /* * seg_init: Initialise the segment-number allocator. diff --git a/ndisasm.c b/ndisasm.c index db9bf5a9..b0d63c15 100644 --- a/ndisasm.c +++ b/ndisasm.c @@ -63,7 +63,7 @@ int main(int argc, char **argv) int bits = 16, b; bool eof = false; uint32_t prefer = 0; - int rn_error; + bool rn_error; int32_t offset; FILE *fp; diff --git a/output/outbin.c b/output/outbin.c index b5a7524c..32bd88a2 100644 --- a/output/outbin.c +++ b/output/outbin.c @@ -124,7 +124,6 @@ static struct Reloc { } *relocs, **reloctail; extern char *stdscan_bufptr; -extern int lookup_label(char *label, int32_t *segment, int32_t *offset); static uint8_t format_mode; /* 0 = original bin, 1 = extended bin */ static int32_t current_section; /* only really needed if format_mode = 0 */ diff --git a/output/outelf32.c b/output/outelf32.c index 344546a6..6466b1eb 100644 --- a/output/outelf32.c +++ b/output/outelf32.c @@ -139,7 +139,7 @@ static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */ static struct ELF_SECTDATA { void *data; int32_t len; - int is_saa; + bool is_saa; } *elf_sects; static int elf_nsect; static int32_t elf_foffs; @@ -537,7 +537,7 @@ static void elf_deflabel(char *name, int32_t segment, int32_t offset, * as the alignment for the common variable. */ if (special) { - int err; + bool err; sym->value = readnum(special, &err); if (err) error(ERR_NONFATAL, "alignment constraint `%s' is not a" @@ -833,7 +833,7 @@ static void elf_out(int32_t segto, const void *data, uint32_t type, error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG"); elf_sect_write(s, data, realbytes); } else if (type == OUT_ADDRESS) { - int gnu16 = 0; + bool gnu16 = false; addr = *(int32_t *)data; if (segment != NO_SEG) { if (segment % 2) { @@ -842,7 +842,7 @@ static void elf_out(int32_t segto, const void *data, uint32_t type, } else { if (wrt == NO_SEG) { if (realbytes == 2) { - gnu16 = 1; + gnu16 = true; elf_add_reloc(s, segment, R_386_16); } else { elf_add_reloc(s, segment, R_386_32); @@ -862,7 +862,7 @@ static void elf_out(int32_t segto, const void *data, uint32_t type, R_386_GOT32, true); } else if (wrt == elf_sym_sect + 1) { if (realbytes == 2) { - gnu16 = 1; + gnu16 = true; addr = elf_add_gsym_reloc(s, segment, addr, R_386_16, false); } else { diff --git a/output/outelf64.c b/output/outelf64.c index 392a7d47..a72a6bf4 100644 --- a/output/outelf64.c +++ b/output/outelf64.c @@ -151,7 +151,7 @@ static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */ static struct ELF_SECTDATA { void *data; int32_t len; - int is_saa; + bool is_saa; } *elf_sects; static int elf_nsect; static int32_t elf_foffs; @@ -550,7 +550,7 @@ static void elf_deflabel(char *name, int32_t segment, int32_t offset, * as the alignment for the common variable. */ if (special) { - int err; + bool err; sym->value = readnum(special, &err); if (err) error(ERR_NONFATAL, "alignment constraint `%s' is not a" @@ -852,7 +852,7 @@ static void elf_out(int32_t segto, const void *data, uint32_t type, error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG"); elf_sect_write(s, data, realbytes); } else if (type == OUT_ADDRESS) { - int gnu16 = 0; + bool gnu16 = false; addr = *(int64_t *)data; if (segment != NO_SEG) { if (segment % 2) { @@ -890,7 +890,7 @@ static void elf_out(int32_t segto, const void *data, uint32_t type, } else if (wrt == elf_sym_sect + 1) { switch (realbytes) { case 2: - gnu16 = 1; + gnu16 = true; addr = elf_add_gsym_reloc(s, segment, addr, R_X86_64_16, false); break; diff --git a/output/outobj.c b/output/outobj.c index 04e4bbac..4c9949cb 100644 --- a/output/outobj.c +++ b/output/outobj.c @@ -1279,7 +1279,8 @@ static int32_t obj_segment(char *name, int pass, int *bits) struct Segment *seg; struct Group *grp; struct External **extp; - int obj_idx, i, attrs, rn_error; + int obj_idx, i, attrs; + bool rn_error; char *p; /* @@ -1655,7 +1656,7 @@ static int obj_directive(char *directive, char *value, int pass) " and library name"); else { struct ImpDef *imp; - int err = false; + bool err = false; imp = *imptail = nasm_malloc(sizeof(struct ImpDef)); imptail = &imp->next; @@ -1719,7 +1720,7 @@ static int obj_directive(char *directive, char *value, int pass) else if (!nasm_stricmp(v, "nodata")) flags |= EXPDEF_FLAG_NODATA; else if (!nasm_strnicmp(v, "parm=", 5)) { - int err = false; + bool err = false; flags |= EXPDEF_MASK_PARMCNT & readnum(v + 5, &err); if (err) { error(ERR_NONFATAL, @@ -1727,7 +1728,7 @@ static int obj_directive(char *directive, char *value, int pass) return 1; } } else { - int err = false; + bool err = false; ordinal = readnum(v, &err); if (err) { error(ERR_NONFATAL, diff --git a/output/outrdf2.c b/output/outrdf2.c index 82ac0ecb..ba488b42 100644 --- a/output/outrdf2.c +++ b/output/outrdf2.c @@ -137,6 +137,7 @@ static void rdf2_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval) static int32_t rdf2_section_names(char *name, int pass, int *bits) { int i; + bool err; char *p, *q; int code = -1; int reserved = 0; @@ -167,8 +168,8 @@ static int32_t rdf2_section_names(char *name, int pass, int *bits) if ((q = strchr(p, ','))) { *q++ = '\0'; - reserved = readnum(q, &i); - if (i) { + reserved = readnum(q, &err); + if (err) { error(ERR_NONFATAL, "value following comma must be numeric"); reserved = 0; @@ -184,8 +185,8 @@ static int32_t rdf2_section_names(char *name, int pass, int *bits) break; } if (code == -1) { /* didn't find anything */ - code = readnum(p, &i); - if (i) { + code = readnum(p, &err); + if (err) { error(ERR_NONFATAL, "unrecognised RDF segment type (%s)", p); code = 3; @@ -387,7 +388,7 @@ static void rdf2_deflabel(char *name, int32_t segment, int32_t offset, * of two; if so, store it as the alignment for the common variable. */ if (special) { - int err; + bool err; ci.align = readnum(special, &err); if (err) error(ERR_NONFATAL, "alignment constraint `%s' is not a" diff --git a/preproc.c b/preproc.c index a19db0a0..0c059ee3 100644 --- a/preproc.c +++ b/preproc.c @@ -75,9 +75,9 @@ typedef struct IncPath IncPath; struct SMacro { SMacro *next; char *name; - int casesense; - unsigned int nparam; + bool casesense; int in_progress; + unsigned int nparam; Token *expansion; }; @@ -1025,7 +1025,7 @@ static int ppscan(void *private_data, struct tokenval *tokval) } if (tline->type == TOK_NUMBER) { - int rn_error; + bool rn_error; tokval->t_integer = readnum(tline->text, &rn_error); if (rn_error) @@ -1091,7 +1091,7 @@ static int ppscan(void *private_data, struct tokenval *tokval) * simple wrapper which calls either strcmp or nasm_stricmp * depending on the value of the `casesense' parameter. */ -static int mstrcmp(char *p, char *q, int casesense) +static int mstrcmp(char *p, char *q, bool casesense) { return casesense ? strcmp(p, q) : nasm_stricmp(p, q); } @@ -1340,10 +1340,10 @@ static void count_mmac_params(Token * t, int *nparam, Token *** params) * * We must free the tline we get passed. */ -static int if_condition(Token * tline, enum preproc_token ct) +static bool if_condition(Token * tline, enum preproc_token ct) { enum pp_conditional i = PP_COND(ct); - int j; + bool j; Token *t, *tt, **tptr, *origline; struct tokenval tokval; expr *evalresult; @@ -1592,7 +1592,9 @@ static int do_directive(Token * tline) { enum preproc_token i; int j; - int nparam, nolist; + bool err; + int nparam; + bool nolist; int k, m; int offset; char *p, *mname; @@ -2060,8 +2062,8 @@ static int do_directive(Token * tline) defining->nparam_min = defining->nparam_max = 0; } else { defining->nparam_min = defining->nparam_max = - readnum(tline->text, &j); - if (j) + readnum(tline->text, &err); + if (err) error(ERR_NONFATAL, "unable to parse parameter count `%s'", tline->text); } @@ -2074,8 +2076,8 @@ static int do_directive(Token * tline) "`%%%smacro' expects a parameter count after `-'", (i == PP_IMACRO ? "i" : "")); else { - defining->nparam_max = readnum(tline->text, &j); - if (j) + defining->nparam_max = readnum(tline->text, &err); + if (err) error(ERR_NONFATAL, "unable to parse parameter count `%s'", tline->text); @@ -2221,7 +2223,7 @@ static int do_directive(Token * tline) tmp_defining = defining; defining = nasm_malloc(sizeof(MMacro)); defining->name = NULL; /* flags this macro as a %rep block */ - defining->casesense = 0; + defining->casesense = false; defining->plus = false; defining->nolist = nolist; defining->in_progress = i; @@ -2726,7 +2728,7 @@ static int do_directive(Token * tline) free_tlist(origline); return DIRECTIVE_FOUND; } - k = readnum(tline->text, &j); + k = readnum(tline->text, &err); m = 1; tline = tline->next; if (tok_is_(tline, "+")) { @@ -2736,7 +2738,7 @@ static int do_directive(Token * tline) free_tlist(origline); return DIRECTIVE_FOUND; } - m = readnum(tline->text, &j); + m = readnum(tline->text, &err); tline = tline->next; } skip_white_(tline); diff --git a/stdscan.c b/stdscan.c index 7305b313..64e77987 100644 --- a/stdscan.c +++ b/stdscan.c @@ -115,7 +115,7 @@ int stdscan(void *private_data, struct tokenval *tv) } return tv->t_type = TOKEN_HERE; } else if (isnumstart(*stdscan_bufptr)) { /* now we've got a number */ - int rn_error; + bool rn_error; r = stdscan_bufptr++; while (isnumchar(*stdscan_bufptr))