From 8885fef4f3b5177d9bf782226d50c207c7b452d2 Mon Sep 17 00:00:00 2001
From: Sabrina Ni <sabrina@andestech.com>
Date: Thu, 6 Jun 2013 17:44:51 +0800
Subject: [PATCH 1/3] Add nds32 port in newlib. Provide minimal architecture
 specific code.

[newlib]
    * configure.host (machine_dir, syscall_dir, newlib_cflags):
    Add settings for nds32*.
    * libc/include/machine/ieeefp.h (IEEE_BIG_ENDIAN, IEEE_LITTLE_ENDIAN):
    Ditto.
    * libc/include/machine/setjmp.h (JBLEN): Ditto.
    * libc/machine/configure.in: Add nds32 subdir.
    * libc/machine/configure: Regenerated.
    * libc/machine/nds32/Makefile.am: New.
    * libc/machine/nds32/Makefile.in: New (autogenerated).
    * libc/machine/nds32/aclocal.m4: New (autogenerated).
    * libc/machine/nds32/configure.in: New.
    * libc/machine/nds32/configure: New (autogenerated).
    * libc/machine/nds32/setjmp.s: New.
---
 newlib/configure.host                  |   11 +-
 newlib/libc/include/machine/ieeefp.h   |    8 +
 newlib/libc/include/machine/setjmp.h   |    7 +
 newlib/libc/machine/configure          |    7 +-
 newlib/libc/machine/configure.in       |    1 +
 newlib/libc/machine/nds32/Makefile.am  |   16 +
 newlib/libc/machine/nds32/Makefile.in  |  440 +++
 newlib/libc/machine/nds32/aclocal.m4   | 1012 +++++++
 newlib/libc/machine/nds32/configure    | 4747 ++++++++++++++++++++++++++++++++
 newlib/libc/machine/nds32/configure.in |   14 +
 newlib/libc/machine/nds32/setjmp.S     |   87 +
 11 files changed, 6347 insertions(+), 3 deletions(-)
 create mode 100644 newlib/libc/machine/nds32/Makefile.am
 create mode 100644 newlib/libc/machine/nds32/Makefile.in
 create mode 100644 newlib/libc/machine/nds32/aclocal.m4
 create mode 100755 newlib/libc/machine/nds32/configure
 create mode 100644 newlib/libc/machine/nds32/configure.in
 create mode 100644 newlib/libc/machine/nds32/setjmp.S

diff --git a/newlib/configure.host b/newlib/configure.host
index f19fa15..d577ce9 100644
--- a/newlib/configure.host
+++ b/newlib/configure.host
@@ -79,7 +79,7 @@ case "${target_optspace}:${host}" in
   yes:*)
     newlib_cflags="${newlib_cflags} -Os"
     ;;
-  :m32r-* | :d10v-* | :d30v-* | :avr-* | :m32c-* | :msp430*-* )
+  :m32r-* | :d10v-* | :d30v-* | :avr-* | :m32c-* | :msp430*-* | :nds32* )
     newlib_cflags="${newlib_cflags} -Os"
     ;;
   no:* | :*)
@@ -233,6 +233,12 @@ case "${host_cpu}" in
   mt*)
 	machine_dir=mt
 	;;
+  nds32*)
+	machine_dir=nds32
+	newlib_cflags="${newlib_cflags} -DPREFER_SIZE_OVER_SPEED "
+	newlib_cflags="${newlib_cflags} -DSMALL_MEMORY "
+	newlib_cflags="${newlib_cflags} -ffunction-sections -fdata-sections"
+	;;
   nios2*)
 	machine_dir=nios2
 	;;
@@ -719,6 +725,9 @@ case "${host}" in
 	syscall_dir=
 	newlib_cflags="${newlib_cflags} -DHAVE_RENAME -DHAVE_SYSTEM -DMISSING_SYSCALL_NAMES"
 	;;
+  nds32*)
+	syscall_dir=syscalls
+	;;
   powerpc*-*-eabialtivec*)
 	default_newlib_io_long_long="yes"
 	newlib_cflags="${newlib_cflags} -DMISSING_SYSCALL_NAMES"
diff --git a/newlib/libc/include/machine/ieeefp.h b/newlib/libc/include/machine/ieeefp.h
index dd1fced..256a781 100644
--- a/newlib/libc/include/machine/ieeefp.h
+++ b/newlib/libc/include/machine/ieeefp.h
@@ -86,6 +86,14 @@
 #define __IEEE_BIG_ENDIAN
 #endif
 
+#ifdef __nds32__
+#ifdef __big_endian__
+#define __IEEE_BIG_ENDIAN
+#else
+#define __IEEE_LITTLE_ENDIAN
+#endif
+#endif
+
 #ifdef __SPU__
 #define __IEEE_BIG_ENDIAN
 
diff --git a/newlib/libc/include/machine/setjmp.h b/newlib/libc/include/machine/setjmp.h
index 78fbdd7..fee409c 100644
--- a/newlib/libc/include/machine/setjmp.h
+++ b/newlib/libc/include/machine/setjmp.h
@@ -55,6 +55,13 @@ _BEGIN_STD_C
 #define _JBLEN  32
 #endif
 
+#ifdef __nds32__
+/* Only 17 words are currently needed.
+   Preserve one word slot if we need to expand.
+   Check newlib/libc/machine/nds32/setjmp.S for more information.  */
+#define _JBLEN 18
+#endif
+
 #if defined(__Z8001__) || defined(__Z8002__)
 /* 16 regs + pc */
 #define _JBLEN 20
diff --git a/newlib/libc/machine/configure b/newlib/libc/machine/configure
** omitted **

diff --git a/newlib/libc/machine/configure.in b/newlib/libc/machine/configure.in
index 5314175..641811a 100644
--- a/newlib/libc/machine/configure.in
+++ b/newlib/libc/machine/configure.in
@@ -55,6 +55,7 @@ if test -n "${machine_dir}"; then
 	moxie) AC_CONFIG_SUBDIRS(moxie) ;;
 	msp430) AC_CONFIG_SUBDIRS(msp430) ;;
 	mt) AC_CONFIG_SUBDIRS(mt) ;;
+	nds32) AC_CONFIG_SUBDIRS(nds32) ;;
 	necv70) AC_CONFIG_SUBDIRS(necv70) ;;
 	nios2) AC_CONFIG_SUBDIRS(nios2) ;;
 	powerpc) AC_CONFIG_SUBDIRS(powerpc) ;;
diff --git a/newlib/libc/machine/nds32/Makefile.am b/newlib/libc/machine/nds32/Makefile.am
new file mode 100644
index 0000000..329e2ce
--- /dev/null
+++ b/newlib/libc/machine/nds32/Makefile.am
@@ -0,0 +1,16 @@
+## Process this file with automake to generate Makefile.in
+
+AUTOMAKE_OPTIONS = cygnus
+
+INCLUDES = $(NEWLIB_CFLAGS) $(CROSS_CFLAGS) $(TARGET_CFLAGS)
+
+AM_CCASFLAGS = $(INCLUDES)
+
+noinst_LIBRARIES = lib.a
+
+lib_a_SOURCES = setjmp.S
+lib_a_CCASFLAGS=$(AM_CCASFLAGS)
+lib_a_CFLAGS = $(AM_CFLAGS)
+
+ACLOCAL_AMFLAGS = -I ../../.. -I ../../../..
+CONFIG_STATUS_DEPENDENCIES = $(newlib_basedir)/configure.host
diff --git a/newlib/libc/machine/nds32/Makefile.in b/newlib/libc/machine/nds32/Makefile.in
** omitted **

diff --git a/newlib/libc/machine/nds32/configure b/newlib/libc/machine/nds32/configure
** omitted **

diff --git a/newlib/libc/machine/nds32/setjmp.S b/newlib/libc/machine/nds32/setjmp.S
new file mode 100644
index 0000000..90b162e
--- /dev/null
+++ b/newlib/libc/machine/nds32/setjmp.S
@@ -0,0 +1,87 @@
+/*
+Copyright (c) 2013 Andes Technology Corporation.
+All rights reserved.
+
+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.
+
+    The name of the company may not be used to endorse or promote
+    products derived from this software without specific prior written
+    permission.
+
+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 RED HAT INCORPORATED 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.
+
+
+The setjmp/longjmp for nds32.
+The usage of thirty-two 32-bit General Purpose Registers (GPR):
+  $r28 : $fp
+  $r29 : $gp
+  $r30 : $lp
+  $r31 : $sp
+
+  caller-save registers: $r0 ~ $r5, $r16 ~ $r23
+  callee-save registers: $r6 ~ $r10, $r11 ~ $r14
+  reserved for assembler : $r15
+  reserved for other use : $r24, $r25, $r26, $r27
+
+Save all callee-save registers and $fp, $gp, $lp and $sp.
+*/
+
+/* int setjmp(jmp_buf env);  */
+	.text
+	.align	2
+	.global	setjmp
+	.type	setjmp, @function
+setjmp:
+#ifdef __NDS32_REDUCED_REGS__
+	/* This case, save 10 words.  */
+	smw.bim   $r6, [$r0], $r10, 0x0
+	smw.bim  $r31, [$r0], $r31, 0xf
+#else
+	/* This case, save 17 words.  */
+	smw.bim   $r6, [$r0], $r14, 0x0
+	smw.bim  $r16, [$r0], $r19, 0xf
+#endif
+
+	/* Set return value to zero.  */
+	movi	$r0, 0
+	ret
+	.size	setjmp, .-setjmp
+
+
+/* void longjmp(jmp_buf env, int val);  */
+	.text
+	.align	2
+	.global	longjmp
+	.type	longjmp, @function
+longjmp:
+#ifdef __NDS32_REDUCED_REGS__
+	lmw.bim   $r6, [$r0], $r10, 0x0
+	lmw.bim  $r31, [$r0], $r31, 0xf
+#else
+	lmw.bim   $r6, [$r0], $r14, 0x0
+	lmw.bim  $r16, [$r0], $r19, 0xf
+#endif
+	/* If the value val is 0, 1 will be returned instead.  */
+	bnez	$r1, 1f
+	movi	$r1, 1
+1:
+	move	$r0, $r1
+	ret
+	.size	longjmp, .-longjmp
-- 
1.7.9.5

