Skip to content

Commit

Permalink
edk2-libc: Add LoongArch64 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarsDoge committed Dec 10, 2024
1 parent d25f970 commit f22e382
Show file tree
Hide file tree
Showing 32 changed files with 2,101 additions and 10 deletions.
2 changes: 1 addition & 1 deletion AppPkg/AppPkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
PLATFORM_VERSION = 0.01
DSC_SPECIFICATION = 0x00010006
OUTPUT_DIRECTORY = Build/AppPkg
SUPPORTED_ARCHITECTURES = IA32|X64|ARM|AARCH64
SUPPORTED_ARCHITECTURES = IA32|X64|ARM|AARCH64|LOONGARCH64
BUILD_TARGETS = DEBUG|RELEASE|NOOPT
SKUID_IDENTIFIER = DEFAULT

Expand Down
104 changes: 104 additions & 0 deletions StdLib/Include/LoongArch64/loongarch-gcc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/** @file
Copyright (c) 2014, ARM Limited. All rights reserved.
Modified by Loongson Technology Co., Ltd. on 2024.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/


/* $NetBSD: arm-gcc.h,v 1.4 2013/01/26 07:08:14 matt Exp $ */

/*
-------------------------------------------------------------------------------
One of the macros `BIGENDIAN' or `LITTLEENDIAN' must be defined.
-------------------------------------------------------------------------------
*/
#define LITTLEENDIAN

/*
-------------------------------------------------------------------------------
The macro `BITS64' can be defined to indicate that 64-bit integer types are
supported by the compiler.
-------------------------------------------------------------------------------
*/
#define BITS64

/*
-------------------------------------------------------------------------------
Each of the following `typedef's defines the most convenient type that holds
integers of at least as many bits as specified. For example, `uint8' should
be the most convenient type that can hold unsigned integers of as many as
8 bits. The `flag' type must be able to hold either a 0 or 1. For most
implementations of C, `flag', `uint8', and `int8' should all be `typedef'ed
to the same as `int'.
-------------------------------------------------------------------------------
*/
typedef int flag;
typedef int uint8;
typedef int int8;
typedef int uint16;
typedef int int16;
typedef unsigned int uint32;
typedef signed int int32;
#ifdef BITS64
typedef unsigned long long int uint64;
typedef signed long long int int64;
#endif

/*
-------------------------------------------------------------------------------
Each of the following `typedef's defines a type that holds integers
of _exactly_ the number of bits specified. For instance, for most
implementation of C, `bits16' and `sbits16' should be `typedef'ed to
`unsigned short int' and `signed short int' (or `short int'), respectively.
-------------------------------------------------------------------------------
*/
typedef unsigned char bits8;
typedef signed char sbits8;
typedef unsigned short int bits16;
typedef signed short int sbits16;
typedef unsigned int bits32;
typedef signed int sbits32;
#ifdef BITS64
typedef unsigned long long int bits64;
typedef signed long long int sbits64;
#endif

#ifdef BITS64
/*
-------------------------------------------------------------------------------
The `LIT64' macro takes as its argument a textual integer literal and
if necessary ``marks'' the literal as having a 64-bit integer type.
For example, the GNU C Compiler (`gcc') requires that 64-bit literals be
appended with the letters `LL' standing for `long long', which is `gcc's
name for the 64-bit integer type. Some compilers may allow `LIT64' to be
defined as the identity macro: `#define LIT64( a ) a'.
-------------------------------------------------------------------------------
*/
#define LIT64( a ) a##ULL
#endif

/*
-------------------------------------------------------------------------------
The macro `INLINE' can be used before functions that should be inlined. If
a compiler does not support explicit inlining, this macro should be defined
to be `static'.
-------------------------------------------------------------------------------
*/
#define INLINE static inline

/* Modified by Loongson Technology Co., Ltd. on [2024]:\
* Changed the endianness detection for new architecture */

#if defined(SOFTFLOAT_FOR_GCC)
#define FLOAT64_DEMANGLE(a) (a)
#define FLOAT64_MANGLE(a) (a)
#endif
106 changes: 106 additions & 0 deletions StdLib/Include/LoongArch64/machine/ansi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/** @file
Machine dependent ANSI type definitions.
Copyright (c) 2010-2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available
under the terms and conditions of the BSD License that accompanies this
distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* from: @(#)ansi.h 8.2 (Berkeley) 1/4/94
NetBSD: ansi.h,v 1.7 2006/10/04 13:51:59 tnozaki Exp
**/
#ifndef _ANSI_H_
#define _ANSI_H_

#include <sys/EfiCdefs.h>

#include <machine/int_types.h>

/*
* Types which are fundamental to the implementation and may appear in
* more than one standard header are defined here. Standard headers
* then use:
* #ifdef _BSD_SIZE_T_
* typedef _BSD_SIZE_T_ size_t;
* #undef _BSD_SIZE_T_
* #endif
*/
#define _BSD_CLOCK_T_ _EFI_CLOCK_T /* clock() */
#define _BSD_PTRDIFF_T_ _EFI_PTRDIFF_T_ /* ptr1 - ptr2 */
#define _BSD_SIZE_T_ _EFI_SIZE_T_ /* sizeof() */
#define _BSD_SSIZE_T_ INTN /* byte count or error */
#define _BSD_TIME_T_ _EFI_TIME_T /* time() */
#define _BSD_VA_LIST_ VA_LIST
#define _BSD_CLOCKID_T_ INT64 /* clockid_t */
#define _BSD_TIMER_T_ INT64 /* timer_t */
#define _BSD_SUSECONDS_T_ INT64 /* suseconds_t */
#define _BSD_USECONDS_T_ UINT64 /* useconds_t */

/*
* NOTE: rune_t is not covered by ANSI nor other standards, and should not
* be instantiated outside of lib/libc/locale. use wchar_t.
*
* Runes (wchar_t) is declared to be an ``int'' instead of the more natural
* ``unsigned long'' or ``long''. Two things are happening here. It is not
* unsigned so that EOF (-1) can be naturally assigned to it and used. Also,
* it looks like 10646 will be a 31 bit standard. This means that if your
* ints cannot hold 32 bits, you will be in trouble. The reason an int was
* chosen over a long is that the is*() and to*() routines take ints (says
* ANSI C), but they use _RUNE_T_ instead of int. By changing it here, you
* lose a bit of ANSI conformance, but your programs will still work.
*
* Note that _WCHAR_T_ and _RUNE_T_ must be of the same type. When wchar_t
* and rune_t are typedef'd, _WCHAR_T_ will be undef'd, but _RUNE_T remains
* defined for ctype.h.
*/
#define _BSD_WCHAR_T_ _EFI_WCHAR_T /* wchar_t */
#define _BSD_WINT_T_ _EFI_WINT_T /* wint_t */
#define _BSD_RUNE_T_ _EFI_WCHAR_T /* rune_t */
#define _BSD_WCTRANS_T_ void * /* wctrans_t */
#define _BSD_WCTYPE_T_ unsigned int /* wctype_t */

/*
* mbstate_t is an opaque object to keep conversion state, during multibyte
* stream conversions. The content must not be referenced by user programs.
*/
typedef struct {
UINT32 A; // Np;
UINT32 B; // U;
UINT32 E; // L
UINT8 C[4]; // n[4]
UINT16 D[2]; // w[2]
} __mbstate_t;
#define _BSD_MBSTATE_T_ __mbstate_t /* mbstate_t */

#endif /* _ANSI_H_ */
13 changes: 13 additions & 0 deletions StdLib/Include/LoongArch64/machine/bswap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* $NetBSD: bswap.h,v 1.4 2006/01/31 07:49:18 dsl Exp $ */

/* Written by Manuel Bouyer. Public domain */

#ifndef _MACHINE_BSWAP_H_
#define _MACHINE_BSWAP_H_

#include <machine/byte_swap.h>

#define __BSWAP_RENAME
#include <sys/bswap.h>

#endif /* !_MACHINE_BSWAP_H_ */
65 changes: 65 additions & 0 deletions StdLib/Include/LoongArch64/machine/byte_swap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*-
* Copyright (c) 1997, 1999, 2002 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum, Neil A. Carson, and Jason R. Thorpe.
*
* Modified by Loongson Technology Co., Ltd. on 2024/12/08.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/

#ifndef _LOONGARCH64_BYTE_SWAP_H_
#define _LOONGARCH64_BYTE_SWAP_H_

#ifdef __GNUC__
#include <sys/types.h>
__BEGIN_DECLS

#define __BYTE_SWAP_U32_VARIABLE __byte_swap_u32_variable
static __inline uint32_t
__byte_swap_u32_variable(uint32_t v)
{
uint32_t t1;

t1 = v ^ ((v << 16) | (v >> 16));
t1 &= 0xff00ffffU;
v = (v >> 8) | (v << 24);
v ^= (t1 >> 8);
return (v);
}

#define __BYTE_SWAP_U16_VARIABLE __byte_swap_u16_variable
static __inline uint16_t
__byte_swap_u16_variable(uint16_t v)
{

v &= 0xffff;
v = (v >> 8) | (v << 8);
return (v);
}

__END_DECLS
#endif

#endif /* _LOONGARCH64_BYTE_SWAP_H_ */
3 changes: 3 additions & 0 deletions StdLib/Include/LoongArch64/machine/endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* $NetBSD: endian.h,v 1.3 2001/06/23 12:20:27 bjh21 Exp $ */

#include <sys/endian.h>
3 changes: 3 additions & 0 deletions StdLib/Include/LoongArch64/machine/endian_machdep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* $NetBSD: endian_machdep.h,v 1.8 2006/01/30 21:52:38 dsl Exp $ */

#define _BYTE_ORDER _LITTLE_ENDIAN
29 changes: 29 additions & 0 deletions StdLib/Include/LoongArch64/machine/fenv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* $NetBSD: fenv.h,v 1.2 2014/01/29 00:22:09 matt Exp $ */

/*
* Based on ieeefp.h written by J.T. Conklin, Apr 28, 1995
* Public domain.
*
* Modified by Loongson Technology Co., Ltd. on 2024/12/08.
*
*/

#ifndef _LOONGARCH64_FENV_H_
#define _LOONGARCH64_FENV_H_

typedef int fexcept_t;

#define FE_INVALID 0x01 /* invalid operation exception */
#define FE_DIVBYZERO 0x02 /* divide-by-zero exception */
#define FE_OVERFLOW 0x04 /* overflow exception */
#define FE_UNDERFLOW 0x08 /* underflow exception */
#define FE_INEXACT 0x10 /* imprecise (loss of precision; "inexact") */

#define FE_ALL_EXCEPT 0x1f

#define FE_TONEAREST 0 /* round to nearest representable number */
#define FE_UPWARD 1 /* round toward positive infinity */
#define FE_DOWNWARD 2 /* round toward negative infinity */
#define FE_TOWARDZERO 3 /* round to zero (truncate) */

#endif /* _LOONGARCH64_FENV_H_ */
Loading

0 comments on commit f22e382

Please sign in to comment.