This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[patch] Altera Nios II support
- From: Sandra Loosemore <sandra at codesourcery dot com>
- To: <newlib at sourceware dot org>
- Date: Fri, 19 Apr 2013 13:28:44 -0600
- Subject: [patch] Altera Nios II support
The attached patch adds minimal support for building newlib for Altera
Nios II ELF (nios2-elf). Altera did the initial toolchain port for this
processor some years ago and now Mentor Graphics is helping to update
the patches and get them in the upstream repositories. The binutils
patches were committed a couple of months ago and the gcc port has just
been posted. gdb will be posted soon and glibc (for nios2-linux-gnu
target) will follow in due course.
For this target, we are not contributing a libgloss port, only newlib
proper. Most people developing code for bare-metal applications on Nios
II will probably use a HAL BSP, which Altera's tools generate directly
from the hardware description. If the hardware description includes a
serial interface, the HAL BSP implements the low-level system calls to
make it work for console I/O, as well as providing all the necessary
startup and initialization code. In addition to testing with a HAL BSP,
at Mentor we also have a BSP built using our own proprietary framework
that supports semihosted I/O when used with an appropriate debug probe.
We re-used the same semihosting API as is implemented by the m68k
libgloss port and we would recommend borrowing that code if there is
future demand for a semihosted libgloss implementation on this target.
Besides running the newlib testsuite, we've also run the complete
gcc/g++/libstdc++ and gdb testsuites using newlib on nios2-elf, so the
newlib port has been well-tested overall.
I've elided the automatically-generated files from the attached patch to
make it easier to review. I also didn't include the aclocal.m4 file in
the diff because it is an exact copy of the one from the mips directory.
Of course these files will be checked in with the rest of the patch
per the ChangeLog, but I saw no point in asking maintainers to wade
through the diffs when reviewing this patch.
OK to commit?
-Sandra
2013-04-19 Sandra Loosemore <sandra@codesourcery.com>
Based on patches from Altera Corporation.
* configure.ac (noconfigdirs [nios2-*-*]): Add target-libgloss.
* configure: Regenerated.
newlib/
* configure.host (machine_dir, sys_dir, syscall_dir, newlib_cflags):
Add settings for nios2*.
* libc/include/machine/ieeefp.h (IEEE_BIG_ENDIAN, IEEE_LITTLE_ENDIAN):
Likewise.
* libc/include/machine/setjmp.h (JBLEN, JBTYPE): Likewise.
* libc/machine/configure.in: Add nios2 subdir.
* libc/machine/configure: Regenerated.
* libc/machine/nios2/Makefile.am: New.
* libc/machine/nios2/Makefile.in: New (autogenerated).
* libc/machine/nios2/aclocal.m4: New (copied from mips version).
* libc/machine/nios2/configure.in: New.
* libc/machine/nios2/configure: New (autogenerated).
* libc/machine/nios2/setjmp.s: New.
Index: configure.ac
===================================================================
RCS file: /cvs/src/src/configure.ac,v
retrieving revision 1.187
diff -u -p -r1.187 configure.ac
--- configure.ac 15 Feb 2013 17:55:25 -0000 1.187
+++ configure.ac 19 Apr 2013 15:34:08 -0000
@@ -1055,6 +1055,9 @@ case "${target}" in
mt-*-*)
noconfigdirs="$noconfigdirs sim"
;;
+ nios2-*-*)
+ noconfigdirs="$noconfigdirs target-libgloss"
+ ;;
powerpc-*-aix*)
# copied from rs6000-*-* entry
noconfigdirs="$noconfigdirs gprof"
Index: newlib/configure.host
===================================================================
RCS file: /cvs/src/src/newlib/configure.host,v
retrieving revision 1.127
diff -u -p -r1.127 configure.host
--- newlib/configure.host 4 Dec 2012 21:43:20 -0000 1.127
+++ newlib/configure.host 19 Apr 2013 15:34:08 -0000
@@ -227,6 +227,9 @@ case "${host_cpu}" in
mt*)
machine_dir=mt
;;
+ nios2*)
+ machine_dir=nios2
+ ;;
or16)
;;
or32)
@@ -476,6 +479,9 @@ case "${host}" in
moxie*)
sys_dir=
;;
+ nios2*)
+ sys_dir=
+ ;;
powerpcle-*-pe)
posix_dir=posix
;;
@@ -703,6 +709,10 @@ case "${host}" in
syscall_dir=syscalls
default_newlib_io_long_long="yes"
;;
+ nios2*)
+ syscall_dir=
+ newlib_cflags="${newlib_cflags} -DHAVE_RENAME -DHAVE_SYSTEM -DMISSING_SYSCALL_NAMES"
+ ;;
powerpc*-*-eabialtivec*)
default_newlib_io_long_long="yes"
newlib_cflags="${newlib_cflags} -DMISSING_SYSCALL_NAMES"
Index: newlib/libc/include/machine/ieeefp.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/machine/ieeefp.h,v
retrieving revision 1.52
diff -u -p -r1.52 ieeefp.h
--- newlib/libc/include/machine/ieeefp.h 26 Sep 2012 20:06:47 -0000 1.52
+++ newlib/libc/include/machine/ieeefp.h 19 Apr 2013 15:34:08 -0000
@@ -388,6 +388,14 @@
#define __SMALL_BITFIELDS /* 16 Bit INT */
#endif
+#ifdef __NIOS2__
+# ifdef __nios2_big_endian__
+# define __IEEE_BIG_ENDIAN
+# else
+# define __IEEE_LITTLE_ENDIAN
+# endif
+#endif
+
#ifndef __IEEE_BIG_ENDIAN
#ifndef __IEEE_LITTLE_ENDIAN
#error Endianess not declared!!
Index: newlib/libc/include/machine/setjmp.h
===================================================================
RCS file: /cvs/src/src/newlib/libc/include/machine/setjmp.h,v
retrieving revision 1.48
diff -u -p -r1.48 setjmp.h
--- newlib/libc/include/machine/setjmp.h 28 Feb 2013 16:42:21 -0000 1.48
+++ newlib/libc/include/machine/setjmp.h 19 Apr 2013 15:34:08 -0000
@@ -282,6 +282,24 @@ _BEGIN_STD_C
#define _JBTYPE unsigned char
#endif
+/*
+ * There are two versions of setjmp()/longjmp():
+ * 1) Compiler (gcc) built-in versions.
+ * 2) Function-call versions.
+ *
+ * The built-in versions are used most of the time. When used, gcc replaces
+ * calls to setjmp()/longjmp() with inline assembly code. The built-in
+ * versions save/restore a variable number of registers.
+
+ * _JBLEN is set to 40 to be ultra-safe with the built-in versions.
+ * It only needs to be 12 for the function-call versions
+ * but this data structure is used by both versions.
+ */
+#ifdef __NIOS2__
+#define _JBLEN 40
+#define _JBTYPE unsigned long
+#endif
+
#ifdef __RX__
#define _JBLEN 0x44
#endif
Index: newlib/libc/machine/configure.in
===================================================================
RCS file: /cvs/src/src/newlib/libc/machine/configure.in,v
retrieving revision 1.21
diff -u -p -r1.21 configure.in
--- newlib/libc/machine/configure.in 26 Sep 2012 20:06:47 -0000 1.21
+++ newlib/libc/machine/configure.in 19 Apr 2013 15:34:08 -0000
@@ -55,6 +55,7 @@ if test -n "${machine_dir}"; then
moxie) AC_CONFIG_SUBDIRS(moxie) ;;
mt) AC_CONFIG_SUBDIRS(mt) ;;
necv70) AC_CONFIG_SUBDIRS(necv70) ;;
+ nios2) AC_CONFIG_SUBDIRS(nios2) ;;
powerpc) AC_CONFIG_SUBDIRS(powerpc) ;;
rl78) AC_CONFIG_SUBDIRS(rl78) ;;
rx) AC_CONFIG_SUBDIRS(rx) ;;
Index: newlib/libc/machine/nios2/Makefile.am
===================================================================
RCS file: newlib/libc/machine/nios2/Makefile.am
diff -N newlib/libc/machine/nios2/Makefile.am
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ newlib/libc/machine/nios2/Makefile.am 19 Apr 2013 15:34:08 -0000
@@ -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
Index: newlib/libc/machine/nios2/configure.in
===================================================================
RCS file: newlib/libc/machine/nios2/configure.in
diff -N newlib/libc/machine/nios2/configure.in
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ newlib/libc/machine/nios2/configure.in 19 Apr 2013 15:34:08 -0000
@@ -0,0 +1,14 @@
+dnl This is the newlib/libc/machine/nios2 configure.in file.
+dnl Process this file with autoconf to produce a configure script.
+
+AC_PREREQ(2.59)
+AC_INIT([newlib],[NEWLIB_VERSION])
+AC_CONFIG_SRCDIR([setjmp.s])
+
+dnl Can't be done in NEWLIB_CONFIGURE because that confuses automake.
+AC_CONFIG_AUX_DIR(../../../..)
+
+NEWLIB_CONFIGURE(../../..)
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
Index: newlib/libc/machine/nios2/setjmp.s
===================================================================
RCS file: newlib/libc/machine/nios2/setjmp.s
diff -N newlib/libc/machine/nios2/setjmp.s
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ newlib/libc/machine/nios2/setjmp.s 19 Apr 2013 15:34:08 -0000
@@ -0,0 +1,54 @@
+;/*
+; * C library -- _setjmp, _longjmp
+; *
+; * _longjmp(a,v)
+; * will generate a "return(v?v:1)" from
+; * the last call to
+; * _setjmp(a)
+; * by unwinding the call stack.
+; * The previous signal state is NOT restored.
+; */
+;
+;
+ .section .text
+ .align 3
+ .globl setjmp
+ .type setjmp,@function
+ .globl longjmp
+ .type longjmp,@function
+
+
+setjmp:
+ stw r16, 0(r4)
+ stw r17, 4(r4)
+ stw r18, 8(r4)
+ stw r19, 12(r4)
+ stw r20, 16(r4)
+ stw r21, 20(r4)
+ stw r22, 24(r4)
+ stw r23, 28(r4)
+ stw gp, 32(r4)
+ stw sp, 36(r4)
+ stw fp, 40(r4)
+ stw ra, 44(r4)
+ mov r2, zero
+ ret
+
+longjmp:
+ ldw r16, 0(r4)
+ ldw r17, 4(r4)
+ ldw r18, 8(r4)
+ ldw r19, 12(r4)
+ ldw r20, 16(r4)
+ ldw r21, 20(r4)
+ ldw r22, 24(r4)
+ ldw r23, 28(r4)
+ ldw gp, 32(r4)
+ ldw sp, 36(r4)
+ ldw fp, 40(r4)
+ ldw ra, 44(r4)
+ mov r2, r5
+ bne r2, zero, 1f
+ movi r2, 1
+1:
+ ret