From bb595a8f028c5dc1e65f625e8cc947793dcb400d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 11 Jul 2001 23:16:50 +0000 Subject: [PATCH] Formatting changes and starting to comment more heavily. svn path=/trunk/yasm/; revision=128 --- frontends/yasm/yasm.c | 5 +- libyasm/bytecode.c | 124 ++++++++++++++++++------------------- libyasm/bytecode.h | 3 +- libyasm/errwarn.c | 101 +++++++++++++++++++++++++----- libyasm/errwarn.h | 21 ++++--- libyasm/expr.c | 57 +++++++++++++---- libyasm/expr.h | 8 +-- libyasm/symrec.c | 29 ++++++--- modules/arch/x86/expr.c | 57 +++++++++++++---- modules/arch/x86/x86expr.c | 57 +++++++++++++---- src/arch/x86/expr.c | 57 +++++++++++++---- src/arch/x86/x86expr.c | 57 +++++++++++++---- src/bytecode.c | 124 ++++++++++++++++++------------------- src/bytecode.h | 3 +- src/errwarn.c | 101 +++++++++++++++++++++++++----- src/errwarn.h | 21 ++++--- src/expr.c | 57 +++++++++++++---- src/expr.h | 8 +-- src/main.c | 5 +- src/symrec.c | 29 ++++++--- 20 files changed, 634 insertions(+), 290 deletions(-) diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c index d409bbb8..ffb095cc 100644 --- a/frontends/yasm/yasm.c +++ b/frontends/yasm/yasm.c @@ -1,4 +1,4 @@ -/* $Id: yasm.c,v 1.3 2001/05/21 02:15:53 peter Exp $ +/* $Id: yasm.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Program entry point, command line parsing * * Copyright (C) 2001 Peter Johnson @@ -29,7 +29,8 @@ extern int yyparse(void); unsigned int line_number = 1; unsigned int mode_bits = 32; -int main(void) +int +main (void) { yydebug = 1; yyparse(); diff --git a/libyasm/bytecode.c b/libyasm/bytecode.c index a0c15a08..4f3e631e 100644 --- a/libyasm/bytecode.c +++ b/libyasm/bytecode.c @@ -1,4 +1,4 @@ -/* $Id: bytecode.c,v 1.12 2001/07/11 04:07:10 peter Exp $ +/* $Id: bytecode.c,v 1.13 2001/07/11 23:16:50 peter Exp $ * Bytecode utility functions * * Copyright (C) 2001 Peter Johnson @@ -25,36 +25,18 @@ #include "errwarn.h" #include "expr.h" +/* Static structures for when NULL is passed to conversion functions. */ +/* for Convert*ToEA() */ static effaddr eff_static; +/* for Convert*ToImm() */ static immval im_static; +/* for Convert*ToBytes() */ unsigned char bytes_static[16]; -effaddr *ConvertIntToEA(effaddr *ptr, unsigned long int_val) -{ - if(!ptr) - ptr = &eff_static; - - ptr->segment = 0; - - ptr->valid_modrm = 0; - ptr->need_modrm = 1; - ptr->valid_sib = 0; - ptr->need_sib = 0; - - /* FIXME: this will leak expr's if static is used */ - ptr->disp = expr_new_ident(EXPR_NUM, ExprNum(int_val)); +static void BuildBC_Common(bytecode *bc); - if((int_val & 0xFF) == int_val) - ptr->len = 1; - else if((int_val & 0xFFFF) == int_val) - ptr->len = 2; - else - ptr->len = 4; - - return ptr; -} - -effaddr *ConvertRegToEA(effaddr *ptr, unsigned long reg) +effaddr * +ConvertRegToEA (effaddr *ptr, unsigned long reg) { if(!ptr) ptr = &eff_static; @@ -70,7 +52,8 @@ effaddr *ConvertRegToEA(effaddr *ptr, unsigned long reg) return ptr; } -effaddr *ConvertExprToEA(effaddr *ptr, expr *expr_ptr) +effaddr * +ConvertExprToEA (effaddr *ptr, expr *expr_ptr) { if(!ptr) ptr = &eff_static; @@ -87,7 +70,8 @@ effaddr *ConvertExprToEA(effaddr *ptr, expr *expr_ptr) return ptr; } -effaddr *ConvertImmToEA(effaddr *ptr, immval *im_ptr, unsigned char im_len) +effaddr * +ConvertImmToEA (effaddr *ptr, immval *im_ptr, unsigned char im_len) { if(!ptr) ptr = &eff_static; @@ -105,7 +89,8 @@ effaddr *ConvertImmToEA(effaddr *ptr, immval *im_ptr, unsigned char im_len) return ptr; } -immval *ConvertIntToImm(immval *ptr, unsigned long int_val) +immval * +ConvertIntToImm (immval *ptr, unsigned long int_val) { if(!ptr) ptr = &im_static; @@ -125,7 +110,8 @@ immval *ConvertIntToImm(immval *ptr, unsigned long int_val) return ptr; } -immval *ConvertExprToImm(immval *ptr, expr *expr_ptr) +immval * +ConvertExprToImm (immval *ptr, expr *expr_ptr) { if(!ptr) ptr = &im_static; @@ -137,7 +123,8 @@ immval *ConvertExprToImm(immval *ptr, expr *expr_ptr) return ptr; } -void SetEASegment(effaddr *ptr, unsigned char segment) +void +SetEASegment (effaddr *ptr, unsigned char segment) { if(!ptr) return; @@ -148,7 +135,8 @@ void SetEASegment(effaddr *ptr, unsigned char segment) ptr->segment = segment; } -void SetEALen(effaddr *ptr, unsigned char len) +void +SetEALen (effaddr *ptr, unsigned char len) { if(!ptr) return; @@ -160,7 +148,8 @@ void SetEALen(effaddr *ptr, unsigned char len) ptr->len = len; } -void SetInsnOperSizeOverride(bytecode *bc, unsigned char opersize) +void +SetInsnOperSizeOverride (bytecode *bc, unsigned char opersize) { if(!bc) return; @@ -179,7 +168,8 @@ void SetInsnOperSizeOverride(bytecode *bc, unsigned char opersize) } } -void SetInsnAddrSizeOverride(bytecode *bc, unsigned char addrsize) +void +SetInsnAddrSizeOverride (bytecode *bc, unsigned char addrsize) { if(!bc) return; @@ -198,7 +188,8 @@ void SetInsnAddrSizeOverride(bytecode *bc, unsigned char addrsize) } } -void SetInsnLockRepPrefix(bytecode *bc, unsigned char prefix) +void +SetInsnLockRepPrefix (bytecode *bc, unsigned char prefix) { unsigned char *lockrep_pre = (unsigned char *)NULL; @@ -224,7 +215,8 @@ void SetInsnLockRepPrefix(bytecode *bc, unsigned char prefix) *lockrep_pre = prefix; } -void SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) +void +SetOpcodeSel (jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) { if(!old_sel) return; @@ -234,7 +226,8 @@ void SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) *old_sel = new_sel; } -static void BuildBC_Common(bytecode *bc) +static void +BuildBC_Common (bytecode *bc) { bc->len = 0; @@ -245,17 +238,18 @@ static void BuildBC_Common(bytecode *bc) bc->mode_bits = mode_bits; } -void BuildBC_Insn(bytecode *bc, - unsigned char opersize, - unsigned char opcode_len, - unsigned char op0, - unsigned char op1, - unsigned char op2, - effaddr *ea_ptr, - unsigned char spare, - immval *im_ptr, - unsigned char im_len, - unsigned char im_sign) +void +BuildBC_Insn (bytecode *bc, + unsigned char opersize, + unsigned char opcode_len, + unsigned char op0, + unsigned char op1, + unsigned char op2, + effaddr *ea_ptr, + unsigned char spare, + immval *im_ptr, + unsigned char im_len, + unsigned char im_sign) { bc->next = (bytecode *)NULL; bc->type = BC_INSN; @@ -293,19 +287,20 @@ void BuildBC_Insn(bytecode *bc, BuildBC_Common(bc); } -void BuildBC_JmpRel(bytecode *bc, - targetval *target, - unsigned char short_valid, - unsigned char short_opcode_len, - unsigned char short_op0, - unsigned char short_op1, - unsigned char short_op2, - unsigned char near_valid, - unsigned char near_opcode_len, - unsigned char near_op0, - unsigned char near_op1, - unsigned char near_op2, - unsigned char addrsize) +void +BuildBC_JmpRel (bytecode *bc, + targetval *target, + unsigned char short_valid, + unsigned char short_opcode_len, + unsigned char short_op0, + unsigned char short_op1, + unsigned char short_op2, + unsigned char near_valid, + unsigned char near_opcode_len, + unsigned char near_op0, + unsigned char near_op1, + unsigned char near_op2, + unsigned char addrsize) { bc->next = (bytecode *)NULL; bc->type = BC_JMPREL; @@ -342,14 +337,16 @@ void BuildBC_JmpRel(bytecode *bc, } /* TODO: implement. Shouldn't be difficult. */ -unsigned char *ConvertBCInsnToBytes(unsigned char *ptr, bytecode *bc, int *len) +unsigned char * +ConvertBCInsnToBytes (unsigned char *ptr, bytecode *bc, int *len) { if(bc->type != BC_INSN) return (unsigned char *)NULL; return (unsigned char *)NULL; } -void DebugPrintBC(bytecode *bc) +void +DebugPrintBC (bytecode *bc) { unsigned long i; @@ -451,4 +448,3 @@ void DebugPrintBC(bytecode *bc) bc->filename ? bc->filename : "", bc->lineno); printf("Offset=%lx BITS=%u\n", bc->offset, bc->mode_bits); } - diff --git a/libyasm/bytecode.h b/libyasm/bytecode.h index 21592850..6b971c33 100644 --- a/libyasm/bytecode.h +++ b/libyasm/bytecode.h @@ -1,4 +1,4 @@ -/* $Id: bytecode.h,v 1.13 2001/07/11 04:07:10 peter Exp $ +/* $Id: bytecode.h,v 1.14 2001/07/11 23:16:50 peter Exp $ * Bytecode utility functions header file * * Copyright (C) 2001 Peter Johnson @@ -112,7 +112,6 @@ typedef struct bytecode_s { unsigned int mode_bits; } bytecode; -effaddr *ConvertIntToEA(effaddr *ptr, unsigned long int_val); effaddr *ConvertRegToEA(effaddr *ptr, unsigned long reg); effaddr *ConvertImmToEA(effaddr *ptr, immval *im_ptr, unsigned char im_len); effaddr *ConvertExprToEA(effaddr *ptr, struct expr_s *expr_ptr); diff --git a/libyasm/errwarn.c b/libyasm/errwarn.c index c0f3c058..cd296f64 100644 --- a/libyasm/errwarn.c +++ b/libyasm/errwarn.c @@ -1,4 +1,4 @@ -/* $Id: errwarn.c,v 1.15 2001/07/11 04:07:10 peter Exp $ +/* $Id: errwarn.c,v 1.16 2001/07/11 23:16:50 peter Exp $ * Error and warning reporting and related functions. * * Copyright (C) 2001 Peter Johnson @@ -38,14 +38,24 @@ #include "errwarn.h" #include "globals.h" +/* Total error count for entire assembler run. + * Assembler should exit with EXIT_FAILURE if this is >= 0 on finish. */ unsigned int error_count = 0; + +/* Total warning count for entire assembler run. + * Should not affect exit value of assembler. */ unsigned int warning_count = 0; +/* See errwarn.h for constants that match up to these strings. + * When adding a string here, keep errwarn.h in sync! */ + +/* Fatal error messages. Match up with fatal_num enum in errwarn.h. */ static char *fatal_msgs[] = { "unknown", "out of memory" }; +/* Error messages. Match up with err_num enum in errwarn.h. */ static char *err_msgs[] = { "", "parser error: %s", @@ -60,6 +70,7 @@ static char *err_msgs[] = { "no %s form of that jump instruction exists" }; +/* Warning messages. Match up with warn_num enum in errwarn.h. */ static char *warn_msgs[] = { "", "ignoring unrecognized character '%s'", @@ -70,17 +81,30 @@ static char *warn_msgs[] = { "multiple SHORT or NEAR specifiers, using leftmost" }; -/* hate to define these as static buffers; better solution would be to use - * vasprintf() to dynamically allocate, but that's not ANSI C */ +/* I hate to define these strings as static buffers; a better solution would be + * to use vasprintf() to dynamically allocate, but that's not ANSI C. + * FIXME! */ + +/* Last error message string. Set by Error(), read by OutputError(). */ static char last_err[1024]; + +/* Last warning message string. Set by Warning(), read by OutputWarning(). */ static char last_warn[1024]; + +/* Last error number. Set by Error(), read and reset by OutputError(). */ static err_num last_err_num = ERR_NONE; + +/* Last warning number. Set by Warning(), read and reset by + * OutputWarning(). */ static warn_num last_warn_num = WARN_NONE; -/* conv_unprint: convert a possibly unprintable character into a printable - * string, using standard cat(1) convention for unprintable characters. */ +/* Static buffer for use by conv_unprint(). */ static char unprint[5]; -char *conv_unprint(char ch) + +/* Convert a possibly unprintable character into a printable string, using + * standard cat(1) convention for unprintable characters. */ +char * +conv_unprint (char ch) { int pos=0; @@ -99,28 +123,59 @@ char *conv_unprint(char ch) return unprint; } -/* yyerror: parser error handler */ -void yyerror(char *s) +/* Parser error handler. Moves error into our error handling system. */ +void +yyerror (char *s) { Error(ERR_PARSER, (char *)NULL, s); } -void InternalError(unsigned int line, char *file, char *message) +/* Report an internal error. Essentially a fatal error with trace info. + * Exit immediately because it's essentially an assert() trap. */ +void +InternalError (unsigned int line, char *file, char *message) { fprintf(stderr, "INTERNAL ERROR at %s, line %d: %s\n", file, line, message); exit(EXIT_FAILURE); } -void Fatal(fatal_num num) +/* Report a fatal error. These are unrecoverable (such as running out of + * memory), so just exit immediately. */ +void +Fatal (fatal_num num) { fprintf(stderr, "FATAL: %s\n", fatal_msgs[num]); exit(EXIT_FAILURE); } -/* replace %1, %2, etc in src with %c, %s, etc. in argtypes. */ -/* currently limits maximum number of args to 9 (%1-%9). */ -static char *process_argtypes(char *src, char *argtypes) +/* Argument replacement function for use in error messages. + * Replaces %1, %2, etc in src with %c, %s, etc. in argtypes. + * Currently limits maximum number of args to 9 (%1-%9). + * + * We need this because messages that take multiple arguments become dependent + * on the order and type of the arguments passed to Error()/Warning(). + * + * i.e. an error string "'%d' is not a valid specifier for '%s'" would require + * that the arguments passed to Error() always be an int and a char *, in that + * order. If the string was changed to be "'%s': invalid specifier '%d'", all + * the times Error() was called for that string would need to be changed to + * reorder the arguments. Or if the %d was not right in some circumstances, + * we'd have to add another string for that type. + * + * This function fixes this problem by allowing the string to be specified as + * "'%1' is not a valid specifier for '%2'" and then specifying at the time of + * the Error() call what the types of %1 and %2 are by passing a argtype string + * "%d%s" (to emulate the first behavior). If the string was changed to be + * "'%2': invalid specifier '%1'", no change would need to be made to the + * Error calls using that string. And as the type is specified with the + * argument list, mismatches are far less likely. + * + * For strings that only have one argument of a fixed type, it can be directly + * specified and NULL passed for the argtypes parameter when Error() is + * called. */ +static char * +process_argtypes (char *src, char *argtypes) { char *dest; char *argtype[9]; @@ -159,7 +214,11 @@ static char *process_argtypes(char *src, char *argtypes) return dest; } -void Error(err_num num, char *argtypes, ...) +/* Register an error. Uses argtypes as described above to specify the + * argument types. Does not print the error, only stores it for + * OutputError() to print. */ +void +Error (err_num num, char *argtypes, ...) { va_list ap; char *printf_str; @@ -180,7 +239,11 @@ void Error(err_num num, char *argtypes, ...) error_count++; } -void Warning(warn_num num, char *argtypes, ...) +/* Register a warning. Uses argtypes as described above to specify the + * argument types. Does not print the warning, only stores it for + * OutputWarning() to print. */ +void +Warning (warn_num num, char *argtypes, ...) { va_list ap; char *printf_str; @@ -201,14 +264,18 @@ void Warning(warn_num num, char *argtypes, ...) warning_count++; } -void OutputError(void) +/* Output a previously stored error (if any) to stderr. */ +void +OutputError (void) { if(last_err_num != ERR_NONE) fprintf(stderr, "filename:%u: %s\n", line_number, last_err); last_err_num = ERR_NONE; } -void OutputWarning(void) +/* Output a previously stored warning (if any) to stderr. */ +void +OutputWarning (void) { if(last_warn_num != WARN_NONE) fprintf(stderr, "filename:%u: warning: %s\n", line_number, last_warn); diff --git a/libyasm/errwarn.h b/libyasm/errwarn.h index 66098a9d..a1e2044b 100644 --- a/libyasm/errwarn.h +++ b/libyasm/errwarn.h @@ -1,4 +1,4 @@ -/* $Id: errwarn.h,v 1.9 2001/07/11 04:07:10 peter Exp $ +/* $Id: errwarn.h,v 1.10 2001/07/11 23:16:50 peter Exp $ * Error and warning reporting and related functions header file. * * Copyright (C) 2001 Peter Johnson @@ -22,16 +22,18 @@ #ifndef YASM_ERRWARN_H #define YASM_ERRWARN_H -char *conv_unprint(char ch); +/* See errwarn.c for strings that match up to these constants. + * When adding a constant here, keep errwarn.c in sync! */ +/* Fatal error constants. Match up with fatal_msgs in errwarn.c. */ typedef enum { FATAL_UNKNOWN = 0, FATAL_NOMEM } fatal_num; -void InternalError(unsigned int line, char *file, char *message); -void Fatal(fatal_num); - +/* Error constants. Match up with err_msgs in errwarn.c. */ +/* FIXME: These shouldn't be ERR_* because they'll violate namespace + * constraints if errno.h is included. */ typedef enum { ERR_NONE = 0, ERR_PARSER, @@ -46,8 +48,7 @@ typedef enum { ERR_NO_JMPREL_FORM } err_num; -void Error(err_num, char *, ...); - +/* Warning constants. Match up with warn_msgs in errwarn.c. */ typedef enum { WARN_NONE = 0, WARN_UNREC_CHAR, @@ -58,6 +59,12 @@ typedef enum { WARN_MULT_SHORTNEAR } warn_num; +char *conv_unprint(char ch); + +void InternalError(unsigned int line, char *file, char *message); + +void Fatal(fatal_num); +void Error(err_num, char *, ...); void Warning(warn_num, char *, ...); void OutputError(void); diff --git a/libyasm/expr.c b/libyasm/expr.c index ceece7a7..627b2a4e 100644 --- a/libyasm/expr.c +++ b/libyasm/expr.c @@ -1,4 +1,4 @@ -/* $Id: expr.c,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: expr.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling * * Copyright (C) 2001 Michael Urman @@ -29,11 +29,12 @@ /* allocate a new expression node, with children as defined. * If it's a unary operator, put the element on the right */ -expr *expr_new (ExprType ltype, - ExprItem left, - ExprOp op, - ExprType rtype, - ExprItem right) +expr * +expr_new (ExprType ltype, + ExprItem left, + ExprOp op, + ExprType rtype, + ExprItem right) { expr *ptr; ptr = malloc (sizeof (expr)); @@ -65,13 +66,41 @@ expr *expr_new (ExprType ltype, } /* helpers */ -ExprItem ExprSym (struct symrec_s *s) { ExprItem e; e.sym = s; return e; } -ExprItem ExprExpr (expr *x) { ExprItem e; e.expr = x; return e; } -ExprItem ExprNum (unsigned long n) { ExprItem e; e.num = n; return e; } -ExprItem ExprNone () { ExprItem e; e.num = 0; return e; } +ExprItem +ExprSym (struct symrec_s *s) +{ + ExprItem e; + e.sym = s; + return e; +} + +ExprItem +ExprExpr (expr *x) +{ + ExprItem e; + e.expr = x; + return e; +} + +ExprItem +ExprNum (unsigned long n) +{ + ExprItem e; + e.num = n; + return e; +} + +ExprItem +ExprNone (void) +{ + ExprItem e; + e.num = 0; + return e; +} /* get rid of unnecessary branches if possible. report. */ -int expr_simplify (expr *e) +int +expr_simplify (expr *e) { int simplified = 0; @@ -197,7 +226,8 @@ int expr_simplify (expr *e) return simplified; } -int expr_get_value (expr *e, unsigned long *retval) +int +expr_get_value (expr *e, unsigned long *retval) { while (!(e->op == EXPR_IDENT && e->rtype == EXPR_NUM) && expr_simplify (e)); @@ -211,7 +241,8 @@ int expr_get_value (expr *e, unsigned long *retval) return 0; } -void expr_print (expr *e) +void +expr_print (expr *e) { if (e->op != EXPR_IDENT) { switch (e->ltype) { diff --git a/libyasm/expr.h b/libyasm/expr.h index 8878d71d..2f0188b2 100644 --- a/libyasm/expr.h +++ b/libyasm/expr.h @@ -1,4 +1,4 @@ -/* $Id: expr.h,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: expr.h,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling header file * * Copyright (C) 2001 Michael Urman @@ -19,8 +19,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef _EXPR_H_ -#define _EXPR_H_ +#ifndef YASM_EXPR_H +#define YASM_EXPR_H typedef enum { EXPR_ADD, @@ -71,7 +71,7 @@ expr *expr_new (ExprType, ExprItem, ExprOp, ExprType, ExprItem); ExprItem ExprSym (struct symrec_s *); ExprItem ExprExpr (expr *); ExprItem ExprNum (unsigned long); -ExprItem ExprNone (); +ExprItem ExprNone (void); #define expr_new_tree(l,o,r) \ expr_new (EXPR_EXPR, ExprExpr(l), (o), EXPR_EXPR, ExprExpr(r)) diff --git a/libyasm/symrec.c b/libyasm/symrec.c index b9a16db7..a7114323 100644 --- a/libyasm/symrec.c +++ b/libyasm/symrec.c @@ -1,4 +1,4 @@ -/* $Id: symrec.c,v 1.4 2001/07/04 20:57:53 peter Exp $ +/* $Id: symrec.c,v 1.5 2001/07/11 23:16:50 peter Exp $ * Symbol table handling * * Copyright (C) 2001 Michael Urman @@ -35,14 +35,16 @@ static void symtab_insert (symtab *); symtab *sym_table = (symtab *)NULL; /* insert a symtab into the global sym_table */ -void symtab_insert (symtab *tab) +void +symtab_insert (symtab *tab) { tab->next = (symtab *)sym_table; sym_table = tab; } /* find a symtab in the global sym_table */ -symtab *symtab_get (char *name) +symtab * +symtab_get (char *name) { symtab *tab; for (tab = sym_table; tab != NULL; tab = tab->next) @@ -56,7 +58,8 @@ symtab *symtab_get (char *name) } /* call a function with each symrec. stop early if 0 returned */ -void sym_foreach (int(*mapfunc)(symrec *)) +void +sym_foreach (int(*mapfunc)(symrec *)) { symtab *tab; for (tab = sym_table; tab != NULL; tab = tab->next) @@ -69,7 +72,8 @@ void sym_foreach (int(*mapfunc)(symrec *)) } /* create a new symtab */ -symtab *symtab_new (char *name, SymType type) +symtab * +symtab_new (char *name, SymType type) { symtab *tab; tab = malloc(sizeof(symtab)); @@ -89,7 +93,8 @@ symtab *symtab_new (char *name, SymType type) return tab; } -symtab *symtab_get_or_new (char *name, SymType type) +symtab * +symtab_get_or_new (char *name, SymType type) { symtab *tab; tab = symtab_get (name); @@ -104,7 +109,8 @@ symtab *symtab_get_or_new (char *name, SymType type) return tab; } -symrec *sym_use_get (char *name, SymType type) +symrec * +sym_use_get (char *name, SymType type) { symtab *tab; tab = symtab_get_or_new (name, type); @@ -112,7 +118,8 @@ symrec *sym_use_get (char *name, SymType type) return &(tab->rec); } -symrec *sym_def_get (char *name, SymType type) +symrec * +sym_def_get (char *name, SymType type) { symtab *tab; tab = symtab_get_or_new (name, type); @@ -123,7 +130,8 @@ symrec *sym_def_get (char *name, SymType type) } #if 0 -symrec *putsym(char *sym_name, int sym_type) +symrec * +putsym (char *sym_name, int sym_type) { symrec *ptr; ptr = malloc(sizeof(symrec)); @@ -136,7 +144,8 @@ symrec *putsym(char *sym_name, int sym_type) return ptr; } -symrec *getsym(char *sym_name) +symrec * +getsym (char *sym_name) { symrec *ptr; for(ptr=sym_table; ptr != (symrec *)NULL; ptr=(symrec *)ptr->next) diff --git a/modules/arch/x86/expr.c b/modules/arch/x86/expr.c index ceece7a7..627b2a4e 100644 --- a/modules/arch/x86/expr.c +++ b/modules/arch/x86/expr.c @@ -1,4 +1,4 @@ -/* $Id: expr.c,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: expr.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling * * Copyright (C) 2001 Michael Urman @@ -29,11 +29,12 @@ /* allocate a new expression node, with children as defined. * If it's a unary operator, put the element on the right */ -expr *expr_new (ExprType ltype, - ExprItem left, - ExprOp op, - ExprType rtype, - ExprItem right) +expr * +expr_new (ExprType ltype, + ExprItem left, + ExprOp op, + ExprType rtype, + ExprItem right) { expr *ptr; ptr = malloc (sizeof (expr)); @@ -65,13 +66,41 @@ expr *expr_new (ExprType ltype, } /* helpers */ -ExprItem ExprSym (struct symrec_s *s) { ExprItem e; e.sym = s; return e; } -ExprItem ExprExpr (expr *x) { ExprItem e; e.expr = x; return e; } -ExprItem ExprNum (unsigned long n) { ExprItem e; e.num = n; return e; } -ExprItem ExprNone () { ExprItem e; e.num = 0; return e; } +ExprItem +ExprSym (struct symrec_s *s) +{ + ExprItem e; + e.sym = s; + return e; +} + +ExprItem +ExprExpr (expr *x) +{ + ExprItem e; + e.expr = x; + return e; +} + +ExprItem +ExprNum (unsigned long n) +{ + ExprItem e; + e.num = n; + return e; +} + +ExprItem +ExprNone (void) +{ + ExprItem e; + e.num = 0; + return e; +} /* get rid of unnecessary branches if possible. report. */ -int expr_simplify (expr *e) +int +expr_simplify (expr *e) { int simplified = 0; @@ -197,7 +226,8 @@ int expr_simplify (expr *e) return simplified; } -int expr_get_value (expr *e, unsigned long *retval) +int +expr_get_value (expr *e, unsigned long *retval) { while (!(e->op == EXPR_IDENT && e->rtype == EXPR_NUM) && expr_simplify (e)); @@ -211,7 +241,8 @@ int expr_get_value (expr *e, unsigned long *retval) return 0; } -void expr_print (expr *e) +void +expr_print (expr *e) { if (e->op != EXPR_IDENT) { switch (e->ltype) { diff --git a/modules/arch/x86/x86expr.c b/modules/arch/x86/x86expr.c index abb8ef2c..2d891512 100644 --- a/modules/arch/x86/x86expr.c +++ b/modules/arch/x86/x86expr.c @@ -1,4 +1,4 @@ -/* $Id: x86expr.c,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: x86expr.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling * * Copyright (C) 2001 Michael Urman @@ -29,11 +29,12 @@ /* allocate a new expression node, with children as defined. * If it's a unary operator, put the element on the right */ -expr *expr_new (ExprType ltype, - ExprItem left, - ExprOp op, - ExprType rtype, - ExprItem right) +expr * +expr_new (ExprType ltype, + ExprItem left, + ExprOp op, + ExprType rtype, + ExprItem right) { expr *ptr; ptr = malloc (sizeof (expr)); @@ -65,13 +66,41 @@ expr *expr_new (ExprType ltype, } /* helpers */ -ExprItem ExprSym (struct symrec_s *s) { ExprItem e; e.sym = s; return e; } -ExprItem ExprExpr (expr *x) { ExprItem e; e.expr = x; return e; } -ExprItem ExprNum (unsigned long n) { ExprItem e; e.num = n; return e; } -ExprItem ExprNone () { ExprItem e; e.num = 0; return e; } +ExprItem +ExprSym (struct symrec_s *s) +{ + ExprItem e; + e.sym = s; + return e; +} + +ExprItem +ExprExpr (expr *x) +{ + ExprItem e; + e.expr = x; + return e; +} + +ExprItem +ExprNum (unsigned long n) +{ + ExprItem e; + e.num = n; + return e; +} + +ExprItem +ExprNone (void) +{ + ExprItem e; + e.num = 0; + return e; +} /* get rid of unnecessary branches if possible. report. */ -int expr_simplify (expr *e) +int +expr_simplify (expr *e) { int simplified = 0; @@ -197,7 +226,8 @@ int expr_simplify (expr *e) return simplified; } -int expr_get_value (expr *e, unsigned long *retval) +int +expr_get_value (expr *e, unsigned long *retval) { while (!(e->op == EXPR_IDENT && e->rtype == EXPR_NUM) && expr_simplify (e)); @@ -211,7 +241,8 @@ int expr_get_value (expr *e, unsigned long *retval) return 0; } -void expr_print (expr *e) +void +expr_print (expr *e) { if (e->op != EXPR_IDENT) { switch (e->ltype) { diff --git a/src/arch/x86/expr.c b/src/arch/x86/expr.c index ceece7a7..627b2a4e 100644 --- a/src/arch/x86/expr.c +++ b/src/arch/x86/expr.c @@ -1,4 +1,4 @@ -/* $Id: expr.c,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: expr.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling * * Copyright (C) 2001 Michael Urman @@ -29,11 +29,12 @@ /* allocate a new expression node, with children as defined. * If it's a unary operator, put the element on the right */ -expr *expr_new (ExprType ltype, - ExprItem left, - ExprOp op, - ExprType rtype, - ExprItem right) +expr * +expr_new (ExprType ltype, + ExprItem left, + ExprOp op, + ExprType rtype, + ExprItem right) { expr *ptr; ptr = malloc (sizeof (expr)); @@ -65,13 +66,41 @@ expr *expr_new (ExprType ltype, } /* helpers */ -ExprItem ExprSym (struct symrec_s *s) { ExprItem e; e.sym = s; return e; } -ExprItem ExprExpr (expr *x) { ExprItem e; e.expr = x; return e; } -ExprItem ExprNum (unsigned long n) { ExprItem e; e.num = n; return e; } -ExprItem ExprNone () { ExprItem e; e.num = 0; return e; } +ExprItem +ExprSym (struct symrec_s *s) +{ + ExprItem e; + e.sym = s; + return e; +} + +ExprItem +ExprExpr (expr *x) +{ + ExprItem e; + e.expr = x; + return e; +} + +ExprItem +ExprNum (unsigned long n) +{ + ExprItem e; + e.num = n; + return e; +} + +ExprItem +ExprNone (void) +{ + ExprItem e; + e.num = 0; + return e; +} /* get rid of unnecessary branches if possible. report. */ -int expr_simplify (expr *e) +int +expr_simplify (expr *e) { int simplified = 0; @@ -197,7 +226,8 @@ int expr_simplify (expr *e) return simplified; } -int expr_get_value (expr *e, unsigned long *retval) +int +expr_get_value (expr *e, unsigned long *retval) { while (!(e->op == EXPR_IDENT && e->rtype == EXPR_NUM) && expr_simplify (e)); @@ -211,7 +241,8 @@ int expr_get_value (expr *e, unsigned long *retval) return 0; } -void expr_print (expr *e) +void +expr_print (expr *e) { if (e->op != EXPR_IDENT) { switch (e->ltype) { diff --git a/src/arch/x86/x86expr.c b/src/arch/x86/x86expr.c index abb8ef2c..2d891512 100644 --- a/src/arch/x86/x86expr.c +++ b/src/arch/x86/x86expr.c @@ -1,4 +1,4 @@ -/* $Id: x86expr.c,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: x86expr.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling * * Copyright (C) 2001 Michael Urman @@ -29,11 +29,12 @@ /* allocate a new expression node, with children as defined. * If it's a unary operator, put the element on the right */ -expr *expr_new (ExprType ltype, - ExprItem left, - ExprOp op, - ExprType rtype, - ExprItem right) +expr * +expr_new (ExprType ltype, + ExprItem left, + ExprOp op, + ExprType rtype, + ExprItem right) { expr *ptr; ptr = malloc (sizeof (expr)); @@ -65,13 +66,41 @@ expr *expr_new (ExprType ltype, } /* helpers */ -ExprItem ExprSym (struct symrec_s *s) { ExprItem e; e.sym = s; return e; } -ExprItem ExprExpr (expr *x) { ExprItem e; e.expr = x; return e; } -ExprItem ExprNum (unsigned long n) { ExprItem e; e.num = n; return e; } -ExprItem ExprNone () { ExprItem e; e.num = 0; return e; } +ExprItem +ExprSym (struct symrec_s *s) +{ + ExprItem e; + e.sym = s; + return e; +} + +ExprItem +ExprExpr (expr *x) +{ + ExprItem e; + e.expr = x; + return e; +} + +ExprItem +ExprNum (unsigned long n) +{ + ExprItem e; + e.num = n; + return e; +} + +ExprItem +ExprNone (void) +{ + ExprItem e; + e.num = 0; + return e; +} /* get rid of unnecessary branches if possible. report. */ -int expr_simplify (expr *e) +int +expr_simplify (expr *e) { int simplified = 0; @@ -197,7 +226,8 @@ int expr_simplify (expr *e) return simplified; } -int expr_get_value (expr *e, unsigned long *retval) +int +expr_get_value (expr *e, unsigned long *retval) { while (!(e->op == EXPR_IDENT && e->rtype == EXPR_NUM) && expr_simplify (e)); @@ -211,7 +241,8 @@ int expr_get_value (expr *e, unsigned long *retval) return 0; } -void expr_print (expr *e) +void +expr_print (expr *e) { if (e->op != EXPR_IDENT) { switch (e->ltype) { diff --git a/src/bytecode.c b/src/bytecode.c index a0c15a08..4f3e631e 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -1,4 +1,4 @@ -/* $Id: bytecode.c,v 1.12 2001/07/11 04:07:10 peter Exp $ +/* $Id: bytecode.c,v 1.13 2001/07/11 23:16:50 peter Exp $ * Bytecode utility functions * * Copyright (C) 2001 Peter Johnson @@ -25,36 +25,18 @@ #include "errwarn.h" #include "expr.h" +/* Static structures for when NULL is passed to conversion functions. */ +/* for Convert*ToEA() */ static effaddr eff_static; +/* for Convert*ToImm() */ static immval im_static; +/* for Convert*ToBytes() */ unsigned char bytes_static[16]; -effaddr *ConvertIntToEA(effaddr *ptr, unsigned long int_val) -{ - if(!ptr) - ptr = &eff_static; - - ptr->segment = 0; - - ptr->valid_modrm = 0; - ptr->need_modrm = 1; - ptr->valid_sib = 0; - ptr->need_sib = 0; - - /* FIXME: this will leak expr's if static is used */ - ptr->disp = expr_new_ident(EXPR_NUM, ExprNum(int_val)); +static void BuildBC_Common(bytecode *bc); - if((int_val & 0xFF) == int_val) - ptr->len = 1; - else if((int_val & 0xFFFF) == int_val) - ptr->len = 2; - else - ptr->len = 4; - - return ptr; -} - -effaddr *ConvertRegToEA(effaddr *ptr, unsigned long reg) +effaddr * +ConvertRegToEA (effaddr *ptr, unsigned long reg) { if(!ptr) ptr = &eff_static; @@ -70,7 +52,8 @@ effaddr *ConvertRegToEA(effaddr *ptr, unsigned long reg) return ptr; } -effaddr *ConvertExprToEA(effaddr *ptr, expr *expr_ptr) +effaddr * +ConvertExprToEA (effaddr *ptr, expr *expr_ptr) { if(!ptr) ptr = &eff_static; @@ -87,7 +70,8 @@ effaddr *ConvertExprToEA(effaddr *ptr, expr *expr_ptr) return ptr; } -effaddr *ConvertImmToEA(effaddr *ptr, immval *im_ptr, unsigned char im_len) +effaddr * +ConvertImmToEA (effaddr *ptr, immval *im_ptr, unsigned char im_len) { if(!ptr) ptr = &eff_static; @@ -105,7 +89,8 @@ effaddr *ConvertImmToEA(effaddr *ptr, immval *im_ptr, unsigned char im_len) return ptr; } -immval *ConvertIntToImm(immval *ptr, unsigned long int_val) +immval * +ConvertIntToImm (immval *ptr, unsigned long int_val) { if(!ptr) ptr = &im_static; @@ -125,7 +110,8 @@ immval *ConvertIntToImm(immval *ptr, unsigned long int_val) return ptr; } -immval *ConvertExprToImm(immval *ptr, expr *expr_ptr) +immval * +ConvertExprToImm (immval *ptr, expr *expr_ptr) { if(!ptr) ptr = &im_static; @@ -137,7 +123,8 @@ immval *ConvertExprToImm(immval *ptr, expr *expr_ptr) return ptr; } -void SetEASegment(effaddr *ptr, unsigned char segment) +void +SetEASegment (effaddr *ptr, unsigned char segment) { if(!ptr) return; @@ -148,7 +135,8 @@ void SetEASegment(effaddr *ptr, unsigned char segment) ptr->segment = segment; } -void SetEALen(effaddr *ptr, unsigned char len) +void +SetEALen (effaddr *ptr, unsigned char len) { if(!ptr) return; @@ -160,7 +148,8 @@ void SetEALen(effaddr *ptr, unsigned char len) ptr->len = len; } -void SetInsnOperSizeOverride(bytecode *bc, unsigned char opersize) +void +SetInsnOperSizeOverride (bytecode *bc, unsigned char opersize) { if(!bc) return; @@ -179,7 +168,8 @@ void SetInsnOperSizeOverride(bytecode *bc, unsigned char opersize) } } -void SetInsnAddrSizeOverride(bytecode *bc, unsigned char addrsize) +void +SetInsnAddrSizeOverride (bytecode *bc, unsigned char addrsize) { if(!bc) return; @@ -198,7 +188,8 @@ void SetInsnAddrSizeOverride(bytecode *bc, unsigned char addrsize) } } -void SetInsnLockRepPrefix(bytecode *bc, unsigned char prefix) +void +SetInsnLockRepPrefix (bytecode *bc, unsigned char prefix) { unsigned char *lockrep_pre = (unsigned char *)NULL; @@ -224,7 +215,8 @@ void SetInsnLockRepPrefix(bytecode *bc, unsigned char prefix) *lockrep_pre = prefix; } -void SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) +void +SetOpcodeSel (jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) { if(!old_sel) return; @@ -234,7 +226,8 @@ void SetOpcodeSel(jmprel_opcode_sel *old_sel, jmprel_opcode_sel new_sel) *old_sel = new_sel; } -static void BuildBC_Common(bytecode *bc) +static void +BuildBC_Common (bytecode *bc) { bc->len = 0; @@ -245,17 +238,18 @@ static void BuildBC_Common(bytecode *bc) bc->mode_bits = mode_bits; } -void BuildBC_Insn(bytecode *bc, - unsigned char opersize, - unsigned char opcode_len, - unsigned char op0, - unsigned char op1, - unsigned char op2, - effaddr *ea_ptr, - unsigned char spare, - immval *im_ptr, - unsigned char im_len, - unsigned char im_sign) +void +BuildBC_Insn (bytecode *bc, + unsigned char opersize, + unsigned char opcode_len, + unsigned char op0, + unsigned char op1, + unsigned char op2, + effaddr *ea_ptr, + unsigned char spare, + immval *im_ptr, + unsigned char im_len, + unsigned char im_sign) { bc->next = (bytecode *)NULL; bc->type = BC_INSN; @@ -293,19 +287,20 @@ void BuildBC_Insn(bytecode *bc, BuildBC_Common(bc); } -void BuildBC_JmpRel(bytecode *bc, - targetval *target, - unsigned char short_valid, - unsigned char short_opcode_len, - unsigned char short_op0, - unsigned char short_op1, - unsigned char short_op2, - unsigned char near_valid, - unsigned char near_opcode_len, - unsigned char near_op0, - unsigned char near_op1, - unsigned char near_op2, - unsigned char addrsize) +void +BuildBC_JmpRel (bytecode *bc, + targetval *target, + unsigned char short_valid, + unsigned char short_opcode_len, + unsigned char short_op0, + unsigned char short_op1, + unsigned char short_op2, + unsigned char near_valid, + unsigned char near_opcode_len, + unsigned char near_op0, + unsigned char near_op1, + unsigned char near_op2, + unsigned char addrsize) { bc->next = (bytecode *)NULL; bc->type = BC_JMPREL; @@ -342,14 +337,16 @@ void BuildBC_JmpRel(bytecode *bc, } /* TODO: implement. Shouldn't be difficult. */ -unsigned char *ConvertBCInsnToBytes(unsigned char *ptr, bytecode *bc, int *len) +unsigned char * +ConvertBCInsnToBytes (unsigned char *ptr, bytecode *bc, int *len) { if(bc->type != BC_INSN) return (unsigned char *)NULL; return (unsigned char *)NULL; } -void DebugPrintBC(bytecode *bc) +void +DebugPrintBC (bytecode *bc) { unsigned long i; @@ -451,4 +448,3 @@ void DebugPrintBC(bytecode *bc) bc->filename ? bc->filename : "", bc->lineno); printf("Offset=%lx BITS=%u\n", bc->offset, bc->mode_bits); } - diff --git a/src/bytecode.h b/src/bytecode.h index 21592850..6b971c33 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -1,4 +1,4 @@ -/* $Id: bytecode.h,v 1.13 2001/07/11 04:07:10 peter Exp $ +/* $Id: bytecode.h,v 1.14 2001/07/11 23:16:50 peter Exp $ * Bytecode utility functions header file * * Copyright (C) 2001 Peter Johnson @@ -112,7 +112,6 @@ typedef struct bytecode_s { unsigned int mode_bits; } bytecode; -effaddr *ConvertIntToEA(effaddr *ptr, unsigned long int_val); effaddr *ConvertRegToEA(effaddr *ptr, unsigned long reg); effaddr *ConvertImmToEA(effaddr *ptr, immval *im_ptr, unsigned char im_len); effaddr *ConvertExprToEA(effaddr *ptr, struct expr_s *expr_ptr); diff --git a/src/errwarn.c b/src/errwarn.c index c0f3c058..cd296f64 100644 --- a/src/errwarn.c +++ b/src/errwarn.c @@ -1,4 +1,4 @@ -/* $Id: errwarn.c,v 1.15 2001/07/11 04:07:10 peter Exp $ +/* $Id: errwarn.c,v 1.16 2001/07/11 23:16:50 peter Exp $ * Error and warning reporting and related functions. * * Copyright (C) 2001 Peter Johnson @@ -38,14 +38,24 @@ #include "errwarn.h" #include "globals.h" +/* Total error count for entire assembler run. + * Assembler should exit with EXIT_FAILURE if this is >= 0 on finish. */ unsigned int error_count = 0; + +/* Total warning count for entire assembler run. + * Should not affect exit value of assembler. */ unsigned int warning_count = 0; +/* See errwarn.h for constants that match up to these strings. + * When adding a string here, keep errwarn.h in sync! */ + +/* Fatal error messages. Match up with fatal_num enum in errwarn.h. */ static char *fatal_msgs[] = { "unknown", "out of memory" }; +/* Error messages. Match up with err_num enum in errwarn.h. */ static char *err_msgs[] = { "", "parser error: %s", @@ -60,6 +70,7 @@ static char *err_msgs[] = { "no %s form of that jump instruction exists" }; +/* Warning messages. Match up with warn_num enum in errwarn.h. */ static char *warn_msgs[] = { "", "ignoring unrecognized character '%s'", @@ -70,17 +81,30 @@ static char *warn_msgs[] = { "multiple SHORT or NEAR specifiers, using leftmost" }; -/* hate to define these as static buffers; better solution would be to use - * vasprintf() to dynamically allocate, but that's not ANSI C */ +/* I hate to define these strings as static buffers; a better solution would be + * to use vasprintf() to dynamically allocate, but that's not ANSI C. + * FIXME! */ + +/* Last error message string. Set by Error(), read by OutputError(). */ static char last_err[1024]; + +/* Last warning message string. Set by Warning(), read by OutputWarning(). */ static char last_warn[1024]; + +/* Last error number. Set by Error(), read and reset by OutputError(). */ static err_num last_err_num = ERR_NONE; + +/* Last warning number. Set by Warning(), read and reset by + * OutputWarning(). */ static warn_num last_warn_num = WARN_NONE; -/* conv_unprint: convert a possibly unprintable character into a printable - * string, using standard cat(1) convention for unprintable characters. */ +/* Static buffer for use by conv_unprint(). */ static char unprint[5]; -char *conv_unprint(char ch) + +/* Convert a possibly unprintable character into a printable string, using + * standard cat(1) convention for unprintable characters. */ +char * +conv_unprint (char ch) { int pos=0; @@ -99,28 +123,59 @@ char *conv_unprint(char ch) return unprint; } -/* yyerror: parser error handler */ -void yyerror(char *s) +/* Parser error handler. Moves error into our error handling system. */ +void +yyerror (char *s) { Error(ERR_PARSER, (char *)NULL, s); } -void InternalError(unsigned int line, char *file, char *message) +/* Report an internal error. Essentially a fatal error with trace info. + * Exit immediately because it's essentially an assert() trap. */ +void +InternalError (unsigned int line, char *file, char *message) { fprintf(stderr, "INTERNAL ERROR at %s, line %d: %s\n", file, line, message); exit(EXIT_FAILURE); } -void Fatal(fatal_num num) +/* Report a fatal error. These are unrecoverable (such as running out of + * memory), so just exit immediately. */ +void +Fatal (fatal_num num) { fprintf(stderr, "FATAL: %s\n", fatal_msgs[num]); exit(EXIT_FAILURE); } -/* replace %1, %2, etc in src with %c, %s, etc. in argtypes. */ -/* currently limits maximum number of args to 9 (%1-%9). */ -static char *process_argtypes(char *src, char *argtypes) +/* Argument replacement function for use in error messages. + * Replaces %1, %2, etc in src with %c, %s, etc. in argtypes. + * Currently limits maximum number of args to 9 (%1-%9). + * + * We need this because messages that take multiple arguments become dependent + * on the order and type of the arguments passed to Error()/Warning(). + * + * i.e. an error string "'%d' is not a valid specifier for '%s'" would require + * that the arguments passed to Error() always be an int and a char *, in that + * order. If the string was changed to be "'%s': invalid specifier '%d'", all + * the times Error() was called for that string would need to be changed to + * reorder the arguments. Or if the %d was not right in some circumstances, + * we'd have to add another string for that type. + * + * This function fixes this problem by allowing the string to be specified as + * "'%1' is not a valid specifier for '%2'" and then specifying at the time of + * the Error() call what the types of %1 and %2 are by passing a argtype string + * "%d%s" (to emulate the first behavior). If the string was changed to be + * "'%2': invalid specifier '%1'", no change would need to be made to the + * Error calls using that string. And as the type is specified with the + * argument list, mismatches are far less likely. + * + * For strings that only have one argument of a fixed type, it can be directly + * specified and NULL passed for the argtypes parameter when Error() is + * called. */ +static char * +process_argtypes (char *src, char *argtypes) { char *dest; char *argtype[9]; @@ -159,7 +214,11 @@ static char *process_argtypes(char *src, char *argtypes) return dest; } -void Error(err_num num, char *argtypes, ...) +/* Register an error. Uses argtypes as described above to specify the + * argument types. Does not print the error, only stores it for + * OutputError() to print. */ +void +Error (err_num num, char *argtypes, ...) { va_list ap; char *printf_str; @@ -180,7 +239,11 @@ void Error(err_num num, char *argtypes, ...) error_count++; } -void Warning(warn_num num, char *argtypes, ...) +/* Register a warning. Uses argtypes as described above to specify the + * argument types. Does not print the warning, only stores it for + * OutputWarning() to print. */ +void +Warning (warn_num num, char *argtypes, ...) { va_list ap; char *printf_str; @@ -201,14 +264,18 @@ void Warning(warn_num num, char *argtypes, ...) warning_count++; } -void OutputError(void) +/* Output a previously stored error (if any) to stderr. */ +void +OutputError (void) { if(last_err_num != ERR_NONE) fprintf(stderr, "filename:%u: %s\n", line_number, last_err); last_err_num = ERR_NONE; } -void OutputWarning(void) +/* Output a previously stored warning (if any) to stderr. */ +void +OutputWarning (void) { if(last_warn_num != WARN_NONE) fprintf(stderr, "filename:%u: warning: %s\n", line_number, last_warn); diff --git a/src/errwarn.h b/src/errwarn.h index 66098a9d..a1e2044b 100644 --- a/src/errwarn.h +++ b/src/errwarn.h @@ -1,4 +1,4 @@ -/* $Id: errwarn.h,v 1.9 2001/07/11 04:07:10 peter Exp $ +/* $Id: errwarn.h,v 1.10 2001/07/11 23:16:50 peter Exp $ * Error and warning reporting and related functions header file. * * Copyright (C) 2001 Peter Johnson @@ -22,16 +22,18 @@ #ifndef YASM_ERRWARN_H #define YASM_ERRWARN_H -char *conv_unprint(char ch); +/* See errwarn.c for strings that match up to these constants. + * When adding a constant here, keep errwarn.c in sync! */ +/* Fatal error constants. Match up with fatal_msgs in errwarn.c. */ typedef enum { FATAL_UNKNOWN = 0, FATAL_NOMEM } fatal_num; -void InternalError(unsigned int line, char *file, char *message); -void Fatal(fatal_num); - +/* Error constants. Match up with err_msgs in errwarn.c. */ +/* FIXME: These shouldn't be ERR_* because they'll violate namespace + * constraints if errno.h is included. */ typedef enum { ERR_NONE = 0, ERR_PARSER, @@ -46,8 +48,7 @@ typedef enum { ERR_NO_JMPREL_FORM } err_num; -void Error(err_num, char *, ...); - +/* Warning constants. Match up with warn_msgs in errwarn.c. */ typedef enum { WARN_NONE = 0, WARN_UNREC_CHAR, @@ -58,6 +59,12 @@ typedef enum { WARN_MULT_SHORTNEAR } warn_num; +char *conv_unprint(char ch); + +void InternalError(unsigned int line, char *file, char *message); + +void Fatal(fatal_num); +void Error(err_num, char *, ...); void Warning(warn_num, char *, ...); void OutputError(void); diff --git a/src/expr.c b/src/expr.c index ceece7a7..627b2a4e 100644 --- a/src/expr.c +++ b/src/expr.c @@ -1,4 +1,4 @@ -/* $Id: expr.c,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: expr.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling * * Copyright (C) 2001 Michael Urman @@ -29,11 +29,12 @@ /* allocate a new expression node, with children as defined. * If it's a unary operator, put the element on the right */ -expr *expr_new (ExprType ltype, - ExprItem left, - ExprOp op, - ExprType rtype, - ExprItem right) +expr * +expr_new (ExprType ltype, + ExprItem left, + ExprOp op, + ExprType rtype, + ExprItem right) { expr *ptr; ptr = malloc (sizeof (expr)); @@ -65,13 +66,41 @@ expr *expr_new (ExprType ltype, } /* helpers */ -ExprItem ExprSym (struct symrec_s *s) { ExprItem e; e.sym = s; return e; } -ExprItem ExprExpr (expr *x) { ExprItem e; e.expr = x; return e; } -ExprItem ExprNum (unsigned long n) { ExprItem e; e.num = n; return e; } -ExprItem ExprNone () { ExprItem e; e.num = 0; return e; } +ExprItem +ExprSym (struct symrec_s *s) +{ + ExprItem e; + e.sym = s; + return e; +} + +ExprItem +ExprExpr (expr *x) +{ + ExprItem e; + e.expr = x; + return e; +} + +ExprItem +ExprNum (unsigned long n) +{ + ExprItem e; + e.num = n; + return e; +} + +ExprItem +ExprNone (void) +{ + ExprItem e; + e.num = 0; + return e; +} /* get rid of unnecessary branches if possible. report. */ -int expr_simplify (expr *e) +int +expr_simplify (expr *e) { int simplified = 0; @@ -197,7 +226,8 @@ int expr_simplify (expr *e) return simplified; } -int expr_get_value (expr *e, unsigned long *retval) +int +expr_get_value (expr *e, unsigned long *retval) { while (!(e->op == EXPR_IDENT && e->rtype == EXPR_NUM) && expr_simplify (e)); @@ -211,7 +241,8 @@ int expr_get_value (expr *e, unsigned long *retval) return 0; } -void expr_print (expr *e) +void +expr_print (expr *e) { if (e->op != EXPR_IDENT) { switch (e->ltype) { diff --git a/src/expr.h b/src/expr.h index 8878d71d..2f0188b2 100644 --- a/src/expr.h +++ b/src/expr.h @@ -1,4 +1,4 @@ -/* $Id: expr.h,v 1.3 2001/07/05 09:32:58 mu Exp $ +/* $Id: expr.h,v 1.4 2001/07/11 23:16:50 peter Exp $ * Expression handling header file * * Copyright (C) 2001 Michael Urman @@ -19,8 +19,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#ifndef _EXPR_H_ -#define _EXPR_H_ +#ifndef YASM_EXPR_H +#define YASM_EXPR_H typedef enum { EXPR_ADD, @@ -71,7 +71,7 @@ expr *expr_new (ExprType, ExprItem, ExprOp, ExprType, ExprItem); ExprItem ExprSym (struct symrec_s *); ExprItem ExprExpr (expr *); ExprItem ExprNum (unsigned long); -ExprItem ExprNone (); +ExprItem ExprNone (void); #define expr_new_tree(l,o,r) \ expr_new (EXPR_EXPR, ExprExpr(l), (o), EXPR_EXPR, ExprExpr(r)) diff --git a/src/main.c b/src/main.c index aeee7b14..28bee46f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.3 2001/05/21 02:15:53 peter Exp $ +/* $Id: main.c,v 1.4 2001/07/11 23:16:50 peter Exp $ * Program entry point, command line parsing * * Copyright (C) 2001 Peter Johnson @@ -29,7 +29,8 @@ extern int yyparse(void); unsigned int line_number = 1; unsigned int mode_bits = 32; -int main(void) +int +main (void) { yydebug = 1; yyparse(); diff --git a/src/symrec.c b/src/symrec.c index b9a16db7..a7114323 100644 --- a/src/symrec.c +++ b/src/symrec.c @@ -1,4 +1,4 @@ -/* $Id: symrec.c,v 1.4 2001/07/04 20:57:53 peter Exp $ +/* $Id: symrec.c,v 1.5 2001/07/11 23:16:50 peter Exp $ * Symbol table handling * * Copyright (C) 2001 Michael Urman @@ -35,14 +35,16 @@ static void symtab_insert (symtab *); symtab *sym_table = (symtab *)NULL; /* insert a symtab into the global sym_table */ -void symtab_insert (symtab *tab) +void +symtab_insert (symtab *tab) { tab->next = (symtab *)sym_table; sym_table = tab; } /* find a symtab in the global sym_table */ -symtab *symtab_get (char *name) +symtab * +symtab_get (char *name) { symtab *tab; for (tab = sym_table; tab != NULL; tab = tab->next) @@ -56,7 +58,8 @@ symtab *symtab_get (char *name) } /* call a function with each symrec. stop early if 0 returned */ -void sym_foreach (int(*mapfunc)(symrec *)) +void +sym_foreach (int(*mapfunc)(symrec *)) { symtab *tab; for (tab = sym_table; tab != NULL; tab = tab->next) @@ -69,7 +72,8 @@ void sym_foreach (int(*mapfunc)(symrec *)) } /* create a new symtab */ -symtab *symtab_new (char *name, SymType type) +symtab * +symtab_new (char *name, SymType type) { symtab *tab; tab = malloc(sizeof(symtab)); @@ -89,7 +93,8 @@ symtab *symtab_new (char *name, SymType type) return tab; } -symtab *symtab_get_or_new (char *name, SymType type) +symtab * +symtab_get_or_new (char *name, SymType type) { symtab *tab; tab = symtab_get (name); @@ -104,7 +109,8 @@ symtab *symtab_get_or_new (char *name, SymType type) return tab; } -symrec *sym_use_get (char *name, SymType type) +symrec * +sym_use_get (char *name, SymType type) { symtab *tab; tab = symtab_get_or_new (name, type); @@ -112,7 +118,8 @@ symrec *sym_use_get (char *name, SymType type) return &(tab->rec); } -symrec *sym_def_get (char *name, SymType type) +symrec * +sym_def_get (char *name, SymType type) { symtab *tab; tab = symtab_get_or_new (name, type); @@ -123,7 +130,8 @@ symrec *sym_def_get (char *name, SymType type) } #if 0 -symrec *putsym(char *sym_name, int sym_type) +symrec * +putsym (char *sym_name, int sym_type) { symrec *ptr; ptr = malloc(sizeof(symrec)); @@ -136,7 +144,8 @@ symrec *putsym(char *sym_name, int sym_type) return ptr; } -symrec *getsym(char *sym_name) +symrec * +getsym (char *sym_name) { symrec *ptr; for(ptr=sym_table; ptr != (symrec *)NULL; ptr=(symrec *)ptr->next) -- 2.50.1