[PATCH] Introduce idata_bytes helper

View: New views
1 Messages — Rating Filter:   Alert me  

[PATCH] Introduce idata_bytes helper

by Cyrill Gorcunov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Please review. Not sure that idata_bytes is a good
name :)

        -- Cyrill
---
From: Cyrill Gorcunov <gorcunov@...>
Date: Sun, 25 Oct 2009 00:06:45 +0400
Subject: [PATCH] Introduce idata_bytes helper

There are a few places with code duplication
which retrieve size of "initialized data" opcodes
like I_DB, I_DW and friends.

Make a general function for the purpose.

Signed-off-by: Cyrill Gorcunov <gorcunov@...>
---
 assemble.c |   58 +++++-----------------------------------------------------
 nasmlib.c  |   37 +++++++++++++++++++++++++++++++++++++
 nasmlib.h  |    2 ++
 parser.c   |   55 ++++++++++++++++++-------------------------------------
 4 files changed, 62 insertions(+), 90 deletions(-)

diff --git a/assemble.c b/assemble.c
index ca2b909..16a7376 100644
--- a/assemble.c
+++ b/assemble.c
@@ -344,40 +344,16 @@ int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
     int64_t insn_end;
     int32_t itimes;
     int64_t start = offset;
-    int64_t wsize = 0;             /* size for DB etc. */
+    int64_t wsize;              /* size for DB etc. */
 
     errfunc = error;            /* to pass to other functions */
     cpu = cp;
     outfmt = output;            /* likewise */
     list = listgen;             /* and again */
 
-    switch (instruction->opcode) {
-    case -1:
+    wsize = idata_bytes(instruction->opcode);
+    if (!wsize)
         return 0;
-    case I_DB:
-        wsize = 1;
-        break;
-    case I_DW:
-        wsize = 2;
-        break;
-    case I_DD:
-        wsize = 4;
-        break;
-    case I_DQ:
-        wsize = 8;
-        break;
-    case I_DT:
-        wsize = 10;
-        break;
-    case I_DO:
- wsize = 16;
- break;
-    case I_DY:
- wsize = 32;
- break;
-    default:
- break;
-    }
 
     if (wsize) {
         extop *e;
@@ -684,34 +660,10 @@ int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
  instruction->opcode == I_DT || instruction->opcode == I_DO ||
  instruction->opcode == I_DY) {
         extop *e;
-        int32_t isize, osize, wsize = 0;   /* placate gcc */
+        int32_t isize, osize, wsize;
 
         isize = 0;
-        switch (instruction->opcode) {
-        case I_DB:
-            wsize = 1;
-            break;
-        case I_DW:
-            wsize = 2;
-            break;
-        case I_DD:
-            wsize = 4;
-            break;
-        case I_DQ:
-            wsize = 8;
-            break;
-        case I_DT:
-            wsize = 10;
-            break;
- case I_DO:
-    wsize = 16;
-    break;
- case I_DY:
-    wsize = 32;
-    break;
- default:
-    break;
-        }
+        wsize = idata_bytes(instruction->opcode);
 
         list_for_each(e, instruction->eops) {
             int32_t align;
diff --git a/nasmlib.c b/nasmlib.c
index 0dea39e..b1e8e2f 100644
--- a/nasmlib.c
+++ b/nasmlib.c
@@ -689,3 +689,40 @@ char *nasm_zap_spaces_rev(char *p)
             *p-- = 0x0;
     return p;
 }
+
+/*
+ * initialized data bytes from opcode
+ */
+unsigned int idata_bytes(unsigned long opcode)
+{
+    unsigned int ret;
+
+    switch (opcode) {
+    case I_DB:
+        ret = 1;
+        break;
+    case I_DW:
+        ret = 2;
+        break;
+    case I_DD:
+        ret = 4;
+        break;
+    case I_DQ:
+        ret = 8;
+        break;
+    case I_DT:
+        ret = 10;
+        break;
+    case I_DO:
+        ret = 16;
+        break;
+    case I_DY:
+        ret = 32;
+        break;
+    case I_none:
+    default:
+        ret = 0;
+        break;
+    }
+    return ret;
+}
diff --git a/nasmlib.h b/nasmlib.h
index 2b30ef4..15857fe 100644
--- a/nasmlib.h
+++ b/nasmlib.h
@@ -415,4 +415,6 @@ static inline bool overflow_unsigned(int64_t value, int bytes)
     return value < vmin || value > vmax;
 }
 
+unsigned int idata_bytes(unsigned long opcode);
+
 #endif
diff --git a/parser.c b/parser.c
index d0d660b..c721bfb 100644
--- a/parser.c
+++ b/parser.c
@@ -429,44 +429,25 @@ restart_parse:
     goto is_float;
  }
             } else if (i == TOKEN_FLOAT) {
-    is_float:
+is_float:
  eop->type = EOT_DB_STRING;
  result->eops_float = true;
- switch (result->opcode) {
- case I_DB:
-    eop->stringlen = 1;
-    break;
- case I_DW:
-    eop->stringlen = 2;
-    break;
- case I_DD:
-    eop->stringlen = 4;
-    break;
- case I_DQ:
-    eop->stringlen = 8;
-    break;
- case I_DT:
-    eop->stringlen = 10;
-    break;
- case I_DO:
-    eop->stringlen = 16;
-    break;
- case I_DY:
-    nasm_error(ERR_NONFATAL, "floating-point constant"
-  " encountered in DY instruction");
-    eop->stringlen = 0;
-    break;
- default:
-    nasm_error(ERR_NONFATAL, "floating-point constant"
-  " encountered in unknown instruction");
-    /*
-     * fix suggested by Pedro Gimeno... original line
-     * was:
-     * eop->type = EOT_NOTHING;
-     */
-    eop->stringlen = 0;
-    break;
- }
+
+                eop->stringlen = idata_bytes(result->opcode);
+                if (eop->stringlen > 16) {
+                    nasm_error(ERR_NONFATAL, "floating-point constant"
+                               " encountered in DY instruction");
+                    eop->stringlen = 0;
+                } else if (eop->stringlen < 1) {
+                    nasm_error(ERR_NONFATAL, "floating-point constant"
+                               " encountered in unknown instruction");
+                    /*
+                     * fix suggested by Pedro Gimeno... original line was:
+                     * eop->type = EOT_NOTHING;
+                     */
+                    eop->stringlen = 0;
+                }
+
  eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
  tail = &eop->next;
  *fixptr = eop;
@@ -481,7 +462,7 @@ restart_parse:
  /* anything else, assume it is an expression */
                 expr *value;
 
-    is_expression:
+is_expression:
                 value = evaluate(stdscan, NULL, &tokval, NULL,
                                  critical, nasm_error, NULL);
                 i = tokval.t_type;
--
1.6.5


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Nasm-devel mailing list
Nasm-devel@...
https://lists.sourceforge.net/lists/listinfo/nasm-devel