]> granicus.if.org Git - yasm/commitdiff
Formatting changes and starting to comment more heavily.
authorPeter Johnson <peter@tortall.net>
Wed, 11 Jul 2001 23:16:50 +0000 (23:16 -0000)
committerPeter Johnson <peter@tortall.net>
Wed, 11 Jul 2001 23:16:50 +0000 (23:16 -0000)
svn path=/trunk/yasm/; revision=128

20 files changed:
frontends/yasm/yasm.c
libyasm/bytecode.c
libyasm/bytecode.h
libyasm/errwarn.c
libyasm/errwarn.h
libyasm/expr.c
libyasm/expr.h
libyasm/symrec.c
modules/arch/x86/expr.c
modules/arch/x86/x86expr.c
src/arch/x86/expr.c
src/arch/x86/x86expr.c
src/bytecode.c
src/bytecode.h
src/errwarn.c
src/errwarn.h
src/expr.c
src/expr.h
src/main.c
src/symrec.c

index d409bbb84a590e500d38e0d5956bb75c38926f9d..ffb095ccee895411934c3be019f6f0904fc6c825 100644 (file)
@@ -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();
index a0c15a08e3a5162e0a970227cc95b1c988db7f5e..4f3e631e443f0e437e96cf362cbeebf613877a51 100644 (file)
@@ -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
 #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 : "<UNKNOWN>", bc->lineno);
     printf("Offset=%lx BITS=%u\n", bc->offset, bc->mode_bits);
 }
-
index 21592850f62364f48b34307509dcc61fb42d26ba..6b971c33c4e2b159bf401fb3f7a10eca2fd6642d 100644 (file)
@@ -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);
index c0f3c0588508562375edfe27864e3a6d8c1ada85..cd296f6436578c339a37a47daff6d28c82f69d13 100644 (file)
@@ -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
 #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);
index 66098a9db55ba7cd55c53a6f4c1cfe8bf320647a..a1e2044b4f7bf849ec76845cf0f6513d6466141e 100644 (file)
@@ -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
 #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);
index ceece7a7b94c4039bbc3fdf4d35d18efa29a7683..627b2a4e028f296c0e4c7e0973053aaa06d6dc6e 100644 (file)
@@ -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
 
 /* 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) {
index 8878d71d8de3190f1ea81d874645b0015078c756..2f0188b291da7f9cba048fe5e29cbb9da38f0d50 100644 (file)
@@ -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))
index b9a16db7c0fddae295a4f6f82aaaad9f48fe45c6..a7114323160012903b3e6ff97643a3f0fb60461e 100644 (file)
@@ -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)
index ceece7a7b94c4039bbc3fdf4d35d18efa29a7683..627b2a4e028f296c0e4c7e0973053aaa06d6dc6e 100644 (file)
@@ -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
 
 /* 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) {
index abb8ef2ce8484d55d99a575b2e758705e3f534e0..2d891512dc6862011cac725b45ae32bd1484e1ac 100644 (file)
@@ -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
 
 /* 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) {
index ceece7a7b94c4039bbc3fdf4d35d18efa29a7683..627b2a4e028f296c0e4c7e0973053aaa06d6dc6e 100644 (file)
@@ -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
 
 /* 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) {
index abb8ef2ce8484d55d99a575b2e758705e3f534e0..2d891512dc6862011cac725b45ae32bd1484e1ac 100644 (file)
@@ -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
 
 /* 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) {
index a0c15a08e3a5162e0a970227cc95b1c988db7f5e..4f3e631e443f0e437e96cf362cbeebf613877a51 100644 (file)
@@ -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
 #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 : "<UNKNOWN>", bc->lineno);
     printf("Offset=%lx BITS=%u\n", bc->offset, bc->mode_bits);
 }
-
index 21592850f62364f48b34307509dcc61fb42d26ba..6b971c33c4e2b159bf401fb3f7a10eca2fd6642d 100644 (file)
@@ -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);
index c0f3c0588508562375edfe27864e3a6d8c1ada85..cd296f6436578c339a37a47daff6d28c82f69d13 100644 (file)
@@ -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
 #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);
index 66098a9db55ba7cd55c53a6f4c1cfe8bf320647a..a1e2044b4f7bf849ec76845cf0f6513d6466141e 100644 (file)
@@ -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
 #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);
index ceece7a7b94c4039bbc3fdf4d35d18efa29a7683..627b2a4e028f296c0e4c7e0973053aaa06d6dc6e 100644 (file)
@@ -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
 
 /* 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) {
index 8878d71d8de3190f1ea81d874645b0015078c756..2f0188b291da7f9cba048fe5e29cbb9da38f0d50 100644 (file)
@@ -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))
index aeee7b14a6f1a8b49551520cb26220d7ba4d785f..28bee46fc90d82ada196d6739106b9d9c035519a 100644 (file)
@@ -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();
index b9a16db7c0fddae295a4f6f82aaaad9f48fe45c6..a7114323160012903b3e6ff97643a3f0fb60461e 100644 (file)
@@ -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)