From 3e4590e7c9f355ee7d287961f9a741c3cc3d0c33 Mon Sep 17 00:00:00 2001
From: cshapiro <cshapiro>
Date: Tue, 7 Nov 2006 11:24:12 +0000
Subject: [PATCH] Make SC_REG expand out to an lvalue on the x86 and AMD64. 
 Define a reg_NARGS macro on these targets and use SC_REG to replace
 open-coded assignments to platform specific sigcontext members in interrupt.c
 and os-common.c.  This code could be simplified further by merging the x86
 and AMD64 cases into the general case.

---
 lisp/FreeBSD-os.c     | 22 ++++++++--------
 lisp/FreeBSD-os.h     |  4 +--
 lisp/Linux-os.c       | 59 +++++++++++++++++++++----------------------
 lisp/Linux-os.h       |  6 ++---
 lisp/NetBSD-os.c      | 38 ++++++++++++++--------------
 lisp/NetBSD-os.h      |  4 +--
 lisp/OpenBSD-os.c     | 22 ++++++++--------
 lisp/OpenBSD-os.h     |  4 +--
 lisp/amd64-lispregs.h |  5 ++--
 lisp/interrupt.c      | 23 +++--------------
 lisp/os-common.c      | 22 +++-------------
 lisp/x86-lispregs.h   |  5 ++--
 12 files changed, 93 insertions(+), 121 deletions(-)

diff --git a/lisp/FreeBSD-os.c b/lisp/FreeBSD-os.c
index ed3b63698..9ac66bf2b 100644
--- a/lisp/FreeBSD-os.c
+++ b/lisp/FreeBSD-os.c
@@ -12,7 +12,7 @@
  * Much hacked by Paul Werkowski
  * GENCGC support by Douglas Crosher, 1996, 1997.
  *
- * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/FreeBSD-os.c,v 1.12 2006/05/30 22:42:05 fgilham Exp $
+ * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/FreeBSD-os.c,v 1.13 2006/11/07 11:24:12 cshapiro Exp $
  *
  */
 
@@ -52,29 +52,29 @@ os_init(void)
     os_vm_page_size = getpagesize();
 }
 
-int
+int *
 sc_reg(struct sigcontext *c, int offset)
 {
     switch (offset) {
       case 0:
-	  return c->sc_eax;
+	  return &c->sc_eax;
       case 2:
-	  return c->sc_ecx;
+	  return &c->sc_ecx;
       case 4:
-	  return c->sc_edx;
+	  return &c->sc_edx;
       case 6:
-	  return c->sc_ebx;
+	  return &c->sc_ebx;
       case 8:
-	  return c->sc_esp;
+	  return &c->sc_esp;
       case 10:
-	  return c->sc_ebp;
+	  return &c->sc_ebp;
       case 12:
-	  return c->sc_esi;
+	  return &c->sc_esi;
       case 14:
-	  return c->sc_edi;
+	  return &c->sc_edi;
     }
 
-    return 0;
+    return (int *) 0;
 }
 
 void
diff --git a/lisp/FreeBSD-os.h b/lisp/FreeBSD-os.h
index 49e8861c0..ffa93bf4f 100644
--- a/lisp/FreeBSD-os.h
+++ b/lisp/FreeBSD-os.h
@@ -1,6 +1,6 @@
 /*
 
- $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/FreeBSD-os.h,v 1.9 2006/05/30 22:42:14 fgilham Exp $
+ $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/FreeBSD-os.h,v 1.10 2006/11/07 11:24:12 cshapiro Exp $
 
  This code was written as part of the CMU Common Lisp project at
  Carnegie Mellon University, and has been placed in the public domain.
@@ -35,7 +35,7 @@ typedef int os_vm_prot_t;
 
 #define OS_VM_DEFAULT_PAGESIZE	4096
 
-int sc_reg(struct sigcontext *, int);
+int *sc_reg(struct sigcontext *, int);
 void os_save_context(void);
 
 /* I *think* this is when things became incompatible with old
diff --git a/lisp/Linux-os.c b/lisp/Linux-os.c
index 9e5baa38f..afdf79127 100644
--- a/lisp/Linux-os.c
+++ b/lisp/Linux-os.c
@@ -15,7 +15,7 @@
  * GENCGC support by Douglas Crosher, 1996, 1997.
  * Alpha support by Julian Dolby, 1999.
  *
- * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/Linux-os.c,v 1.26 2006/03/17 02:56:45 rtoy Exp $
+ * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/Linux-os.c,v 1.27 2006/11/07 11:24:12 cshapiro Exp $
  *
  */
 
@@ -100,75 +100,74 @@ os_init(void)
 }
 
 #ifdef i386
+int *
 #if (LINUX_VERSION_CODE >= linuxversion(2,1,0)) || (__GNU_LIBRARY__ >= 6)
-int
 sc_reg(struct sigcontext *c, int offset)
 #else
-int
 sc_reg(struct sigcontext_struct *c, int offset)
 #endif
 {
     switch (offset) {
       case 0:
-	  return c->eax;
+	  return &c->eax;
       case 2:
-	  return c->ecx;
+	  return &c->ecx;
       case 4:
-	  return c->edx;
+	  return &c->edx;
       case 6:
-	  return c->ebx;
+	  return &c->ebx;
       case 8:
-	  return c->esp;
+	  return &c->esp;
       case 10:
-	  return c->ebp;
+	  return &c->ebp;
       case 12:
-	  return c->esi;
+	  return &c->esi;
       case 14:
-	  return c->edi;
+	  return &c->edi;
     }
-    return 0;
+    return (int *) 0;
 }
 #endif
 
 #ifdef __x86_64
-int
+int *
 sc_reg(struct sigcontext *c, int offset)
 {
     switch (offset) {
       case 0:
-	  return c->rax;
+	  return &c->rax;
       case 2:
-	  return c->rcx;
+	  return &c->rcx;
       case 4:
-	  return c->rdx;
+	  return &c->rdx;
       case 6:
-	  return c->rbx;
+	  return &c->rbx;
       case 8:
-	  return c->rsp;
+	  return &c->rsp;
       case 10:
-	  return c->rbp;
+	  return &c->rbp;
       case 12:
-	  return c->rsi;
+	  return &c->rsi;
       case 14:
-	  return c->rdi;
+	  return &c->rdi;
       case 16:
-	  return c->r8;
+	  return &c->r8;
       case 18:
-	  return c->r9;
+	  return &c->r9;
       case 20:
-	  return c->r10;
+	  return &c->r10;
       case 22:
-	  return c->r11;
+	  return &c->r11;
       case 24:
-	  return c->r12;
+	  return &c->r12;
       case 26:
-	  return c->r13;
+	  return &c->r13;
       case 28:
-	  return c->r14;
+	  return &c->r14;
       case 30:
-	  return c->r15;
+	  return &c->r15;
     }
-    return 0;
+    return (int *) 0;
 }
 #endif
 
diff --git a/lisp/Linux-os.h b/lisp/Linux-os.h
index 4cce24206..de69cc214 100644
--- a/lisp/Linux-os.h
+++ b/lisp/Linux-os.h
@@ -1,4 +1,4 @@
-/* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/Linux-os.h,v 1.18 2005/10/05 17:23:56 rtoy Exp $
+/* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/Linux-os.h,v 1.19 2006/11/07 11:24:12 cshapiro Exp $
 
  This code was written as part of the CMU Common Lisp project at
  Carnegie Mellon University, and has been placed in the public domain.
@@ -45,9 +45,9 @@ typedef int os_vm_prot_t;	/* like hpux */
 #endif
 
 #if (LINUX_VERSION_CODE >= linuxversion(2,1,0)) || (__GNU_LIBRARY__ >= 6)
-int sc_reg(struct sigcontext *, int);
+int *sc_reg(struct sigcontext *, int);
 #else
-int sc_reg(struct sigcontext_struct *, int);
+int *sc_reg(struct sigcontext_struct *, int);
 #endif
 void os_save_context(void);
 
diff --git a/lisp/NetBSD-os.c b/lisp/NetBSD-os.c
index 77048695d..2976120c6 100644
--- a/lisp/NetBSD-os.c
+++ b/lisp/NetBSD-os.c
@@ -15,7 +15,7 @@
  * Frobbed for OpenBSD by Pierre R. Mai, 2001.
  * Frobbed for NetBSD by Pierre R. Mai, 2002.
  *
- * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/NetBSD-os.c,v 1.3 2005/09/15 18:26:50 rtoy Exp $
+ * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/NetBSD-os.c,v 1.4 2006/11/07 11:24:12 cshapiro Exp $
  *
  */
 
@@ -53,51 +53,51 @@ os_init(void)
     os_vm_page_size = OS_VM_DEFAULT_PAGESIZE;
 }
 
-int
+int *
 sc_reg(os_context_t * c, int offset)
 {
 #ifdef i386
 #if USE_SA_SIGINFO
     switch (offset) {
       case 0:
-	  return c->uc_mcontext.__gregs[_REG_EAX];
+	  return &c->uc_mcontext.__gregs[_REG_EAX];
       case 2:
-	  return c->uc_mcontext.__gregs[_REG_ECX];
+	  return &c->uc_mcontext.__gregs[_REG_ECX];
       case 4:
-	  return c->uc_mcontext.__gregs[_REG_EDX];
+	  return &c->uc_mcontext.__gregs[_REG_EDX];
       case 6:
-	  return c->uc_mcontext.__gregs[_REG_EBX];
+	  return &c->uc_mcontext.__gregs[_REG_EBX];
       case 8:
-	  return c->uc_mcontext.__gregs[_REG_ESP];
+	  return &c->uc_mcontext.__gregs[_REG_ESP];
       case 10:
-	  return c->uc_mcontext.__gregs[_REG_EBP];
+	  return &c->uc_mcontext.__gregs[_REG_EBP];
       case 12:
-	  return c->uc_mcontext.__gregs[_REG_ESI];
+	  return &c->uc_mcontext.__gregs[_REG_ESI];
       case 14:
-	  return c->uc_mcontext.__gregs[_REG_EDI];
+	  return &c->uc_mcontext.__gregs[_REG_EDI];
     }
 #else
     switch (offset) {
       case 0:
-	  return c->sc_eax;
+	  return &c->sc_eax;
       case 2:
-	  return c->sc_ecx;
+	  return &c->sc_ecx;
       case 4:
-	  return c->sc_edx;
+	  return &c->sc_edx;
       case 6:
-	  return c->sc_ebx;
+	  return &c->sc_ebx;
       case 8:
-	  return c->sc_esp;
+	  return &c->sc_esp;
       case 10:
-	  return c->sc_ebp;
+	  return &c->sc_ebp;
       case 12:
-	  return c->sc_esi;
+	  return &c->sc_esi;
       case 14:
-	  return c->sc_edi;
+	  return &c->sc_edi;
     }
 #endif
 #endif
-    return 0;
+    return (int *) 0;
 }
 
 void
diff --git a/lisp/NetBSD-os.h b/lisp/NetBSD-os.h
index ce4fc36ac..ec1cddf1d 100644
--- a/lisp/NetBSD-os.h
+++ b/lisp/NetBSD-os.h
@@ -1,6 +1,6 @@
 /*
 
- $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/NetBSD-os.h,v 1.4 2005/09/15 18:26:50 rtoy Exp $
+ $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/NetBSD-os.h,v 1.5 2006/11/07 11:24:12 cshapiro Exp $
 
  This code was written as part of the CMU Common Lisp project at
  Carnegie Mellon University, and has been placed in the public domain.
@@ -34,7 +34,7 @@ typedef int os_vm_prot_t;
 #define CODE(code)  ((code) ? code->si_code : 0)
 #define os_context_t ucontext_t
 
-int sc_reg(ucontext_t *, int);
+int *sc_reg(ucontext_t *, int);
 
 #define PROTECTION_VIOLATION_SIGNAL SIGSEGV
 
diff --git a/lisp/OpenBSD-os.c b/lisp/OpenBSD-os.c
index 67b3d2eee..c1c9bc41e 100644
--- a/lisp/OpenBSD-os.c
+++ b/lisp/OpenBSD-os.c
@@ -13,7 +13,7 @@
  * GENCGC support by Douglas Crosher, 1996, 1997.
  * Frobbed for OpenBSD by Pierre R. Mai, 2001.
  *
- * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/OpenBSD-os.c,v 1.2 2005/09/15 18:26:50 rtoy Exp $
+ * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/OpenBSD-os.c,v 1.3 2006/11/07 11:24:12 cshapiro Exp $
  *
  */
 
@@ -49,28 +49,28 @@ os_init(void)
     os_vm_page_size = OS_VM_DEFAULT_PAGESIZE;
 }
 
-int
+int *
 sc_reg(struct sigcontext *c, int offset)
 {
     switch (offset) {
       case 0:
-	  return c->sc_eax;
+	  return &c->sc_eax;
       case 2:
-	  return c->sc_ecx;
+	  return &c->sc_ecx;
       case 4:
-	  return c->sc_edx;
+	  return &c->sc_edx;
       case 6:
-	  return c->sc_ebx;
+	  return &c->sc_ebx;
       case 8:
-	  return c->sc_esp;
+	  return &c->sc_esp;
       case 10:
-	  return c->sc_ebp;
+	  return &c->sc_ebp;
       case 12:
-	  return c->sc_esi;
+	  return &c->sc_esi;
       case 14:
-	  return c->sc_edi;
+	  return &c->sc_edi;
     }
-    return 0;
+    return (int *) 0;
 }
 
 void
diff --git a/lisp/OpenBSD-os.h b/lisp/OpenBSD-os.h
index a0c1db894..86683e7e9 100644
--- a/lisp/OpenBSD-os.h
+++ b/lisp/OpenBSD-os.h
@@ -1,6 +1,6 @@
 /*
 
- $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/OpenBSD-os.h,v 1.3 2005/09/15 18:26:50 rtoy Exp $
+ $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/OpenBSD-os.h,v 1.4 2006/11/07 11:24:12 cshapiro Exp $
 
  This code was written as part of the CMU Common Lisp project at
  Carnegie Mellon University, and has been placed in the public domain.
@@ -34,6 +34,6 @@ typedef int os_vm_prot_t;
 #define CODE(code)  ((code) ? code->si_code : 0)
 
 #define uc_sigmask sc_mask
-int sc_reg(struct sigcontext *, int);
+int *sc_reg(struct sigcontext *, int);
 
 #endif /* _OPENBSD_OS_H_ */
diff --git a/lisp/amd64-lispregs.h b/lisp/amd64-lispregs.h
index b9fd3b38d..7cedf5bf3 100644
--- a/lisp/amd64-lispregs.h
+++ b/lisp/amd64-lispregs.h
@@ -1,5 +1,5 @@
 /* x86-lispregs.h -*- Mode: C; -*-
- * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/amd64-lispregs.h,v 1.3 2005/01/13 19:55:00 fgilham Exp $
+ * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/amd64-lispregs.h,v 1.4 2006/11/07 11:24:12 cshapiro Rel $
  */
 
 /* These register names and offsets correspond to definitions
@@ -38,6 +38,7 @@
 
 #define reg_SP reg_RSP
 #define reg_FP reg_RBP
+#define reg_NARGS reg_RCX
 
 #define REGNAMES "RAX", "RCX", "RDX", "RBX", "RSP", "RBP", "RSI", "RDI", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
 
@@ -51,7 +52,7 @@
  * xxx-os.c handle it.
  */
 
-#define SC_REG(sc, n) sc_reg(sc,n)
+#define SC_REG(sc, n) (*sc_reg(sc,n))
 #define SC_PC(sc) ((sc)->sc_pc)
 
 #endif /* _AMD64_LISPREGS_H_ */
diff --git a/lisp/interrupt.c b/lisp/interrupt.c
index ebaf5f094..4b228395e 100644
--- a/lisp/interrupt.c
+++ b/lisp/interrupt.c
@@ -1,4 +1,4 @@
-/* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/interrupt.c,v 1.43 2005/09/15 18:26:51 rtoy Exp $ */
+/* $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/interrupt.c,v 1.44 2006/11/07 11:24:12 cshapiro Exp $ */
 
 /* Interrupt handling magic. */
 
@@ -680,23 +680,10 @@ install_handler(int signal, void handler(HANDLER_ARGS))
 void
 interrupt_handle_space_overflow(lispobj error, os_context_t * context)
 {
-#ifdef i386
-    /* ECX is the argument count.  */
-#if USE_SA_SIGINFO
-    context->uc_mcontext.__gregs[_REG_EIP] ==
-	(int) ((struct function *) PTR(error))->code;
-    context->uc_mcontext.__gregs[_REG_ECX] == 0;
-#else
+#if defined(i386) || defined(__x86_64)
     SC_PC(context) = (int) ((struct function *) PTR(error))->code;
-    context->sc_ecx = 0;
-#endif
-#else
-#ifdef __x86_64
-    /* RCX is the argument count.  */
-    context->sc_rip = (unsigned long) ((struct function *) PTR(error))->code;
-    context->sc_rcx = 0;
-#else
-#ifdef sparc
+    SC_REG(context, reg_NARGS) = 0;
+#elif defined(sparc)
     build_fake_control_stack_frame(context);
     /* This part should be common to all non-x86 ports */
     SC_PC(context) = (long) ((struct function *) PTR(error))->code;
@@ -709,8 +696,6 @@ interrupt_handle_space_overflow(lispobj error, os_context_t * context)
 #else
 #error interrupt_handle_space_overflow not implemented for this system
 #endif
-#endif
-#endif
 }
 #endif /* FEATURE_HEAP_OVERFLOW_CHECK */
 
diff --git a/lisp/os-common.c b/lisp/os-common.c
index 37c897568..b0e8bdab5 100644
--- a/lisp/os-common.c
+++ b/lisp/os-common.c
@@ -1,6 +1,6 @@
 /*
 
- $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/os-common.c,v 1.20 2005/09/15 18:26:52 rtoy Exp $
+ $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/os-common.c,v 1.21 2006/11/07 11:24:12 cshapiro Exp $
 
  This code was written as part of the CMU Common Lisp project at
  Carnegie Mellon University, and has been placed in the public domain.
@@ -449,22 +449,10 @@ os_control_stack_overflow(void *fault_addr, os_context_t * context)
 	else
 	    error = SymbolFunction(YELLOW_ZONE_HIT);
 
-#ifdef i386
-	/* ECX is the argument count.  */
+#if defined(i386) || defined(__x86_64)
 	SC_PC(context) = (int) ((struct function *) PTR(error))->code;
-#if USE_SA_SIGINFO
-	context->uc_mcontext.__gregs[_REG_ECX] == 0;
-#else
-	context->sc_ecx = 0;
-#endif
-#else
-#ifdef __x86_64
-	/* RCX is the argument count.  */
-	context->sc_rip =
-	    (unsigned long) ((struct function *) PTR(error))->code;
-	context->sc_rcx = 0;
-#else
-#ifdef sparc
+	SC_REG(context, reg_NARGS) = 0;
+#elif defined(sparc)
 	/* This part should be common to all non-x86 ports */
 	SC_PC(context) = (long) ((struct function *) PTR(error))->code;
 	SC_NPC(context) = SC_PC(context) + 4;
@@ -476,8 +464,6 @@ os_control_stack_overflow(void *fault_addr, os_context_t * context)
 	SC_REG(context, reg_CODE) = ((long) PTR(error)) + type_FunctionPointer;
 #else
 #error os_control_stack_overflow not implemented for this system
-#endif
-#endif
 #endif
 	return 1;
     }
diff --git a/lisp/x86-lispregs.h b/lisp/x86-lispregs.h
index 398f9898c..d78a590d6 100644
--- a/lisp/x86-lispregs.h
+++ b/lisp/x86-lispregs.h
@@ -1,5 +1,5 @@
 /* x86-lispregs.h -*- Mode: C; -*-
- * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/x86-lispregs.h,v 1.4 2005/01/13 19:55:01 fgilham Exp $
+ * $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/lisp/x86-lispregs.h,v 1.5 2006/11/07 11:24:12 cshapiro Exp $
  */
 
 #ifndef _X86_LISPREGS_H_
@@ -30,6 +30,7 @@
 
 #define reg_SP reg_ESP
 #define reg_FP reg_EBP
+#define reg_NARGS reg_ECX
 
 #define REGNAMES "EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI"
 
@@ -43,7 +44,7 @@
  * xxx-os.c handle it.
  */
 
-#define SC_REG(sc, n) sc_reg(sc,n)
+#define SC_REG(sc, n) (*sc_reg(sc,n))
 
 #ifdef __NetBSD__
 #define SC_PC(uc) ((uc)->uc_mcontext.__gregs[_REG_EIP])
-- 
GitLab