summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2018-11-28 14:55:58 -0800
committerH. Peter Anvin <hpa@zytor.com>2018-11-28 14:55:58 -0800
commit1350620bf1dc474b39ca05eb9ba23813a90042b5 (patch)
tree3ba5001319b61d490d63f96bcd13c2efacd38fcf /include
parent099cc177398df447ee7153ab29337f00dbcc9d16 (diff)
downloadnasm-1350620bf1dc474b39ca05eb9ba23813a90042b5.tar.gz
ctype: create our own ctype table
Create our own ctype table where we can do the tests we want to do cheaply, instead of calling ctype functions and then adding additional tests all over the code. Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Diffstat (limited to 'include')
-rw-r--r--include/nasm.h45
-rw-r--r--include/nasmlib.h18
-rw-r--r--include/nctype.h123
3 files changed, 126 insertions, 60 deletions
diff --git a/include/nasm.h b/include/nasm.h
index 6fdd0c15..1d26df98 100644
--- a/include/nasm.h
+++ b/include/nasm.h
@@ -44,6 +44,7 @@
#include <time.h>
#include "nasmlib.h"
+#include "nctype.h"
#include "strlist.h"
#include "preproc.h"
#include "insnsi.h" /* For enum opcode */
@@ -379,46 +380,6 @@ extern struct strlist *depend_list;
extern bool tasm_compatible_mode;
/*
- * Some lexical properties of the NASM source language, included
- * here because they are shared between the parser and preprocessor.
- */
-
-/*
- * isidstart matches any character that may start an identifier, and isidchar
- * matches any character that may appear at places other than the start of an
- * identifier. E.g. a period may only appear at the start of an identifier
- * (for local labels), whereas a number may appear anywhere *but* at the
- * start.
- * isbrcchar matches any character that may placed inside curly braces as a
- * decorator. E.g. {rn-sae}, {1to8}, {k1}{z}
- */
-
-static inline bool isidstart(char c)
-{
- return nasm_isalpha(c) || c == '_' || c == '.' || c == '@' ||
- (tasm_compatible_mode && c == '?');
-}
-static inline bool isidchar(char c)
-{
- return isidstart(c) || nasm_isdigit(c) || c == '$' || c == '#' || c == '~';
-}
-
-static inline bool isbrcchar(char c)
-{
- return isidchar(c) || c == '-';
-}
-
-static inline bool isnumstart(char c)
-{
- return nasm_isdigit(c) || c == '$';
-}
-
-static inline bool isnumchar(char c)
-{
- return nasm_isalnum(c) || c == '_';
-}
-
-/*
* inline function to skip past an identifier; returns the first character past
* the identifier if valid, otherwise NULL.
*/
@@ -426,10 +387,10 @@ static inline char *nasm_skip_identifier(const char *str)
{
const char *p = str;
- if (!isidstart(*p++)) {
+ if (!nasm_isidstart(*p++)) {
p = NULL;
} else {
- while (isidchar(*p++))
+ while (nasm_isidchar(*p++))
;
}
return (char *)p;
diff --git a/include/nasmlib.h b/include/nasmlib.h
index e57d0e6d..ee8045e0 100644
--- a/include/nasmlib.h
+++ b/include/nasmlib.h
@@ -41,7 +41,6 @@
#include "compiler.h"
#include "bytesex.h"
-#include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
@@ -49,23 +48,6 @@
#endif
/*
- * tolower table -- avoids a function call on some platforms.
- * NOTE: unlike the tolower() function in ctype, EOF is *NOT*
- * a permitted value, for obvious reasons.
- */
-void tolower_init(void);
-extern unsigned char nasm_tolower_tab[256];
-#define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
-
-/* Wrappers around <ctype.h> functions */
-/* These are only valid for values that cannot include EOF */
-#define nasm_isspace(x) isspace((unsigned char)(x))
-#define nasm_isalpha(x) isalpha((unsigned char)(x))
-#define nasm_isdigit(x) isdigit((unsigned char)(x))
-#define nasm_isalnum(x) isalnum((unsigned char)(x))
-#define nasm_isxdigit(x) isxdigit((unsigned char)(x))
-
-/*
* Wrappers around malloc, realloc and free. nasm_malloc will
* fatal-error and die rather than return NULL; nasm_realloc will
* do likewise, and will also guarantee to work right on being
diff --git a/include/nctype.h b/include/nctype.h
new file mode 100644
index 00000000..6c9bec81
--- /dev/null
+++ b/include/nctype.h
@@ -0,0 +1,123 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2018 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * ctype-like functions specific to NASM
+ */
+#ifndef NASM_NCTYPE_H
+#define NASM_NCTYPE_H
+
+#include "compiler.h"
+
+void nasm_ctype_init(void);
+
+extern unsigned char nasm_tolower_tab[256];
+static inline char nasm_tolower(char x)
+{
+ return nasm_tolower_tab[(unsigned char)x];
+}
+
+/*
+ * NASM ctype table
+ */
+enum nasm_ctype {
+ NCT_CTRL = 0x001,
+ NCT_SPACE = 0x002,
+ NCT_ASCII = 0x004,
+ NCT_LOWER = 0x008, /* isalpha(x) && tolower(x) == x */
+ NCT_UPPER = 0x010, /* isalpha(x) && tolower(x) != x */
+ NCT_DIGIT = 0x020,
+ NCT_HEX = 0x040,
+ NCT_ID = 0x080,
+ NCT_IDSTART = 0x100,
+ NCT_MINUS = 0x200, /* - */
+ NCT_DOLLAR = 0x400, /* $ */
+ NCT_UNDER = 0x800 /* _ */
+};
+
+extern uint16_t nasm_ctype_tab[256];
+static inline bool nasm_ctype(unsigned char x, enum nasm_ctype mask)
+{
+ return (nasm_ctype_tab[x] & mask) != 0;
+}
+
+static inline bool nasm_isspace(char x)
+{
+ return nasm_ctype(x, NCT_SPACE);
+}
+
+static inline bool nasm_isalpha(char x)
+{
+ return nasm_ctype(x, NCT_LOWER|NCT_UPPER);
+}
+
+static inline bool nasm_isdigit(char x)
+{
+ return nasm_ctype(x, NCT_DIGIT);
+}
+static inline bool nasm_isalnum(char x)
+{
+ return nasm_ctype(x, NCT_LOWER|NCT_UPPER|NCT_DIGIT);
+}
+static inline bool nasm_isxdigit(char x)
+{
+ return nasm_ctype(x, NCT_HEX);
+}
+static inline bool nasm_isidstart(char x)
+{
+ return nasm_ctype(x, NCT_IDSTART);
+}
+static inline bool nasm_isidchar(char x)
+{
+ return nasm_ctype(x, NCT_ID);
+}
+static inline bool nasm_isbrcchar(char x)
+{
+ return nasm_ctype(x, NCT_ID|NCT_MINUS);
+}
+static inline bool nasm_isnumstart(char x)
+{
+ return nasm_ctype(x, NCT_DIGIT|NCT_DOLLAR);
+}
+static inline bool nasm_isnumchar(char x)
+{
+ return nasm_ctype(x, NCT_DIGIT|NCT_LOWER|NCT_UPPER|NCT_UNDER);
+}
+
+/* TASM-compatible mode requires ? to be an identifier character */
+static inline void nasm_ctype_tasm_mode(void)
+{
+ nasm_ctype_tab['?'] |= NCT_ID|NCT_IDSTART;
+}
+
+#endif /* NASM_NCTYPE_H */