From 94e40691ba69d94cbfafc66bcee847ec0e963719 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Tue, 19 May 2020 08:55:41 +0200 Subject: [PATCH] core: Remove CBYTES, CSTRING, NCBYTES macros. * src/gc.c (cell_bytes, news_bytes): New function. * include/mes/macros.h (CBYTES, CSTRING, NCBYTES): Remove. Update users. * include/mes/m2.h: Likewise. --- include/mes/m2.h | 3 --- include/mes/macros.h | 5 ----- include/mes/mes.h | 4 +++- src/display.c | 10 +++++----- src/gc.c | 16 ++++++++++++++-- src/hash.c | 4 ++-- src/posix.c | 28 ++++++++++++++-------------- src/string.c | 8 ++++---- 8 files changed, 42 insertions(+), 36 deletions(-) diff --git a/include/mes/m2.h b/include/mes/m2.h index db5b22fd..e645c8e9 100644 --- a/include/mes/m2.h +++ b/include/mes/m2.h @@ -83,13 +83,10 @@ struct timeval #define NLENGTH(x) ((x*struct_size)+g_news)->car -#define NCBYTES(x) (((x*struct_size)+g_news) + 8) #define NVALUE(x) ((x*struct_size)+g_news)->cdr #define NSTRING(x) ((x*struct_size)+g_news)->cdr #define NVECTOR(x) ((x*struct_size)+g_news)->cdr -#define CSTRING(x) CBYTES (STRING (x)) - #define CAAR(x) CAR (CAR (x)) #define CADR(x) CAR (CDR (x)) #define CDAR(x) CDR (CAR (x)) diff --git a/include/mes/macros.h b/include/mes/macros.h index 4218521b..94b63f7f 100644 --- a/include/mes/macros.h +++ b/include/mes/macros.h @@ -37,8 +37,6 @@ #define CLOSURE(x) g_cells[x].cdr #define CONTINUATION(x) g_cells[x].cdr -#define CBYTES(x) (char*)&g_cells[x].cdr - #define MACRO(x) g_cells[x].car #define NAME(x) g_cells[x].cdr #define PORT(x) g_cells[x].car @@ -48,13 +46,10 @@ #define VECTOR(x) g_cells[x].cdr #define NLENGTH(x) g_news[x].car -#define NCBYTES(x) (char*)&g_news[x].cdr #define NVALUE(x) g_news[x].cdr #define NSTRING(x) g_news[x].cdr #define NVECTOR(x) g_news[x].cdr -#define CSTRING(x) CBYTES (STRING (x)) - #define CAAR(x) CAR (CAR (x)) #define CADR(x) CAR (CDR (x)) #define CDAR(x) CDR (CAR (x)) diff --git a/include/mes/mes.h b/include/mes/mes.h index 5f3e4841..a3d601e5 100644 --- a/include/mes/mes.h +++ b/include/mes/mes.h @@ -78,7 +78,6 @@ SCM alloc (long n); SCM apply (SCM f, SCM x, SCM a); SCM apply_builtin (SCM fn, SCM x); SCM builtin_name (SCM builtin); -FUNCTION builtin_function (SCM builtin); SCM cstring_to_list (char const *s); SCM cstring_to_symbol (char const *s); SCM fdisplay_ (SCM, int, int); @@ -109,6 +108,9 @@ SCM struct_ref_ (SCM x, long i); SCM struct_set_x_ (SCM x, long i, SCM e); SCM vector_ref_ (SCM x, long i); SCM vector_set_x_ (SCM x, long i, SCM e); +FUNCTION builtin_function (SCM builtin); +char *cell_bytes (SCM x); +char *news_bytes (SCM x); int peekchar (); int readchar (); int unreadchar (); diff --git a/src/display.c b/src/display.c index ca2b05cf..c2597902 100644 --- a/src/display.c +++ b/src/display.c @@ -189,28 +189,28 @@ display_helper (SCM x, int cont, char *sep, int fd, int write_p) fdputs (" ", fd); x = STRING (x); fdputc ('"', fd); - fdwrite_string (CSTRING (x), LENGTH (x), fd); + fdwrite_string (cell_bytes (STRING (x)), LENGTH (x), fd); fdputc ('"', fd); fdputs (">", fd); } else if (t == TKEYWORD) { fdputs ("#:", fd); - fdwrite_string (CSTRING (x), LENGTH (x), fd); + fdwrite_string (cell_bytes (STRING (x)), LENGTH (x), fd); } else if (t == TSTRING) { if (write_p == 1) { fdputc ('"', fd); - fdwrite_string (CSTRING (x), LENGTH (x), fd); + fdwrite_string (cell_bytes (STRING (x)), LENGTH (x), fd); fdputc ('"', fd); } else - fdputs (CSTRING (x), fd); + fdputs (cell_bytes (STRING (x)), fd); } else if (t == TSPECIAL || t == TSYMBOL) - fdwrite_string (CSTRING (x), LENGTH (x), fd); + fdwrite_string (cell_bytes (STRING (x)), LENGTH (x), fd); else if (t == TREF) fdisplay_ (REF (x), fd, write_p); else if (t == TSTRUCT) diff --git a/src/gc.c b/src/gc.c index 1a78905c..da7fc219 100644 --- a/src/gc.c +++ b/src/gc.c @@ -25,6 +25,18 @@ #include #include +char * +cell_bytes (SCM x) +{ + return &CDR (x); +} + +char * +news_bytes (SCM x) +{ + return &NCDR (x); +} + SCM gc_init () /*:((internal)) */ { @@ -261,8 +273,8 @@ gc_copy (SCM old) /*:((internal)) */ } else if (NTYPE (new) == TBYTES) { - char const *src = CBYTES (old); - char *dest = NCBYTES (new); + char const *src = cell_bytes (old); + char *dest = news_bytes (new); size_t length = NLENGTH (new); memcpy (dest, src, length + 1); g_free = g_free + bytes_cells (length) - 1; diff --git a/src/hash.c b/src/hash.c index 73f0f111..9b036530 100644 --- a/src/hash.c +++ b/src/hash.c @@ -38,7 +38,7 @@ int hashq_ (SCM x, long size) { if (TYPE (x) == TSPECIAL || TYPE (x) == TSYMBOL) - return hash_cstring (CSTRING (x), size); /* FIXME: hash x directly. */ + return hash_cstring (cell_bytes (STRING (x)), size); /* FIXME: hash x directly. */ error (cell_symbol_system_error, cons (make_string0 ("hashq_: not a symbol"), x)); } @@ -46,7 +46,7 @@ int hash_ (SCM x, long size) { if (TYPE (x) == TSTRING) - return hash_cstring (CSTRING (x), size); + return hash_cstring (cell_bytes (STRING (x)), size); assert_msg (0, "0"); return hashq_ (x, size); } diff --git a/src/posix.c b/src/posix.c index 1dee281a..f91416e5 100644 --- a/src/posix.c +++ b/src/posix.c @@ -47,7 +47,7 @@ peekchar () size_t length = LENGTH (string); if (length == 0) return -1; - char const *p = CSTRING (string); + char const *p = cell_bytes (STRING (string)); return p[0]; } @@ -61,7 +61,7 @@ readchar () size_t length = LENGTH (string); if (length == 0) return -1; - char const *p = CSTRING (string); + char const *p = cell_bytes (STRING (string)); int c = p[0]; p = p + 1; STRING (port) = make_string (p, length - 1); @@ -78,10 +78,10 @@ unreadchar (int c) SCM port = current_input_port (); SCM string = STRING (port); size_t length = LENGTH (string); - char *p = CSTRING (string); + char *p = cell_bytes (STRING (string)); p = p - 1; string = make_string (p, length + 1); - p = CSTRING (string); + p = cell_bytes (STRING (string)); p[0] = c; STRING (port) = string; return c; @@ -159,7 +159,7 @@ SCM getenv_ (SCM s) /*:((name . "getenv")) */ { char *p; - p = getenv (CSTRING (s)); + p = getenv (cell_bytes (STRING (s))); if (p != 0) return make_string0 (p); return cell_f; @@ -169,15 +169,15 @@ SCM setenv_ (SCM s, SCM v) /*:((name . "setenv")) */ { char *buf = __setenv_buf; - strcpy (buf, CSTRING (s)); - setenv (buf, CSTRING (v), 1); + strcpy (buf, cell_bytes (STRING (s))); + setenv (buf, cell_bytes (STRING (v)), 1); return cell_unspecified; } SCM access_p (SCM file_name, SCM mode) { - if (access (CSTRING (file_name), VALUE (mode)) == 0) + if (access (cell_bytes (STRING (file_name)), VALUE (mode)) == 0) return cell_t; return cell_f; } @@ -196,7 +196,7 @@ current_input_port () SCM open_input_file (SCM file_name) { - int filedes = mes_open (CSTRING (file_name), O_RDONLY, 0); + int filedes = mes_open (cell_bytes (STRING (file_name)), O_RDONLY, 0); if (filedes == -1) error (cell_symbol_system_error, cons (make_string0 ("No such file or directory"), file_name)); return make_number (filedes); @@ -247,7 +247,7 @@ open_output_file (SCM x) /*:((arity . n)) */ int mode = S_IRUSR | S_IWUSR; if (TYPE (x) == TPAIR && TYPE (car (x)) == TNUMBER) mode = VALUE (car (x)); - return make_number (mes_open (CSTRING (file_name), O_WRONLY | O_CREAT | O_TRUNC, mode)); + return make_number (mes_open (cell_bytes (STRING (file_name)), O_WRONLY | O_CREAT | O_TRUNC, mode)); } SCM @@ -273,7 +273,7 @@ set_current_error_port (SCM port) SCM chmod_ (SCM file_name, SCM mode) /*:((name . "chmod")) */ { - chmod (CSTRING (file_name), VALUE (mode)); + chmod (cell_bytes (STRING (file_name)), VALUE (mode)); return cell_unspecified; } @@ -300,13 +300,13 @@ execl_ (SCM file_name, SCM args) /*:((name . "execl")) */ if (length__ (args) > 1000) error (cell_symbol_system_error, cons (file_name, cons (make_string0 ("too many arguments"), cons (file_name, args)))); - c_argv[i] = CSTRING (file_name); + c_argv[i] = cell_bytes (STRING (file_name)); i = i + 1; while (args != cell_nil) { assert_msg (TYPE (CAR (args)) == TSTRING, "TYPE (CAR (args)) == TSTRING"); SCM arg = CAR (args); - c_argv[i] = CSTRING (arg); + c_argv[i] = cell_bytes (STRING (arg)); i = i + 1; args = CDR (args); if (g_debug > 2) @@ -420,6 +420,6 @@ dup2_ (SCM old, SCM new) /*:((name . "dup2")) */ SCM delete_file (SCM file_name) { - unlink (CSTRING (file_name)); + unlink (cell_bytes (STRING (file_name))); return cell_unspecified; } diff --git a/src/string.c b/src/string.c index de5a630e..1f7c53ab 100644 --- a/src/string.c +++ b/src/string.c @@ -80,7 +80,7 @@ string_equal_p (SCM a, SCM b) /*:((name . "string=?")) */ if (a == b || STRING (a) == STRING (b) || (LENGTH (a) == 0 && LENGTH (b) == 0) - || (LENGTH (a) == LENGTH (b) && !memcmp (CSTRING (a), CSTRING (b), LENGTH (a)))) + || (LENGTH (a) == LENGTH (b) && !memcmp (cell_bytes (STRING (a)), cell_bytes (STRING (b)), LENGTH (a)))) return cell_t; return cell_f; @@ -150,7 +150,7 @@ cstring_to_symbol (char const *s) SCM string_to_list (SCM string) { - return bytes_to_list (CSTRING (string), LENGTH (string)); + return bytes_to_list (cell_bytes (STRING (string)), LENGTH (string)); } SCM @@ -192,7 +192,7 @@ string_append (SCM x) /*:((arity . n)) */ { SCM string = CAR (x); assert_msg (TYPE (string) == TSTRING, "TYPE (string) == TSTRING"); - memcpy (p, CSTRING (string), LENGTH (string) + 1); + memcpy (p, cell_bytes (STRING (string)), LENGTH (string) + 1); p = p + LENGTH (string); size = size + LENGTH (string); if (size > MAX_STRING) @@ -218,6 +218,6 @@ string_ref (SCM str, SCM k) size_t i = VALUE (k); if (i > size) error (cell_symbol_system_error, cons (make_string0 ("value out of range"), k)); - char const *p = CSTRING (str); + char const *p = cell_bytes (STRING (str)); return make_char (p[i]); }