From 4b0b8f3d4fffcb09c38004ba0016973b3230a7b1 Mon Sep 17 00:00:00 2001 From: Akos Kiss Date: Thu, 14 Apr 2016 14:06:09 +0200 Subject: [PATCH] Merge darwin and linux jerry-libc targets into a single posix target The two target implementations are very close clones of each other. The only known differences are the following: * The asm component of the linux target has `.type` and `.size` declarations for the functions defined therein, while the darwin target doesn't support those. * Linux uses `__NR_xxx` mnemonics for syscall numbers, while darwin uses `SYS_xxx` format. * Darwin does not have `exit_group` syscall but `exit` only. * The linux target has a commented out `jrt_set_mem_limits` fuction declaration at the end of the C source. (Based on its name, this function does not really belong here.) Simple preprocessor macros can unify the first three differences. While for the sake of legacy, we can keep the fourth commented-out code in the code base; it might turn out to be useful elsewhere in the future. Since it remains commented out it wont cause any problems on any OSs. So, this patch gets rid of a lot of duplication. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu --- jerry-libc/CMakeLists.txt | 4 +- jerry-libc/target/darwin/jerry-asm.S | 56 --- jerry-libc/target/darwin/jerry-libc-target.c | 324 ------------------ jerry-libc/target/linux/jerry-asm.S | 70 ---- jerry-libc/target/posix/jerry-asm.S | 76 ++++ .../{linux => posix}/jerry-libc-target.c | 53 +-- 6 files changed, 109 insertions(+), 474 deletions(-) delete mode 100644 jerry-libc/target/darwin/jerry-asm.S delete mode 100644 jerry-libc/target/darwin/jerry-libc-target.c delete mode 100644 jerry-libc/target/linux/jerry-asm.S create mode 100644 jerry-libc/target/posix/jerry-asm.S rename jerry-libc/target/{linux => posix}/jerry-libc-target.c (80%) diff --git a/jerry-libc/CMakeLists.txt b/jerry-libc/CMakeLists.txt index 535b3499b..1bee12175 100644 --- a/jerry-libc/CMakeLists.txt +++ b/jerry-libc/CMakeLists.txt @@ -77,10 +77,10 @@ set(COMPILE_FLAGS_LIBC "${COMPILE_FLAGS_JERRY} ${C_FLAGS_JERRY}") # Platform-specific # Linux - file(GLOB SOURCE_LIBC_LINUX target/linux/*.c target/linux/*.S) + file(GLOB SOURCE_LIBC_LINUX target/posix/*.c target/posix/*.S) # Darwin - file(GLOB SOURCE_LIBC_DARWIN target/darwin/*.c target/darwin/*.S) + file(GLOB SOURCE_LIBC_DARWIN target/posix/*.c target/posix/*.S) # MCU # stm32f3 diff --git a/jerry-libc/target/darwin/jerry-asm.S b/jerry-libc/target/darwin/jerry-asm.S deleted file mode 100644 index cbd2b84ae..000000000 --- a/jerry-libc/target/darwin/jerry-asm.S +++ /dev/null @@ -1,56 +0,0 @@ -#ifdef __TARGET_HOST_x64 -# include "arch/x86-64.h" -#elif defined (__TARGET_HOST_x86) -# include "arch/x86-32.h" -#elif defined (__TARGET_HOST_ARMv7) -# include "arch/arm-v7.h" -#else /* !__TARGET_HOST_x64 && !__TARGET_HOST_x86 && !__TARGET_HOST_ARMv7 */ -# error "!__TARGET_HOST_x64 && !__TARGET_HOST_x86 && !__TARGET_HOST_ARMv7" -#endif /* !__TARGET_HOST_x64 && !__TARGET_HOST_x86 && !__TARGET_HOST_ARMv7 */ - -.global _start -_start: - _START - -.global syscall_0 -syscall_0: - SYSCALL_0 - -.global syscall_1 -syscall_1: - SYSCALL_1 - -.global syscall_2 -syscall_2: - SYSCALL_2 - -.global syscall_3 -syscall_3: - SYSCALL_3 - -/** - * setjmp (jmp_buf env) - * - * See also: - * longjmp - * - * @return 0 - if returns from direct call, - * nonzero - if returns after longjmp. - */ -.global setjmp -setjmp: - _SETJMP - -/** - * longjmp (jmp_buf env, int val) - * - * Note: - * if val is not 0, then it would be returned from setjmp, - * otherwise - 0 would be returned. - * - * See also: - * setjmp - */ -.global longjmp -longjmp: - _LONGJMP diff --git a/jerry-libc/target/darwin/jerry-libc-target.c b/jerry-libc/target/darwin/jerry-libc-target.c deleted file mode 100644 index 0b6aaeaa5..000000000 --- a/jerry-libc/target/darwin/jerry-libc-target.c +++ /dev/null @@ -1,324 +0,0 @@ -/* Copyright 2014-2016 Samsung Electronics Co., Ltd. - * Copyright 2016 University of Szeged. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Jerry libc platform-specific functions darwin implementation - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "jerry-libc-defs.h" - -extern long int syscall_0 (long int syscall_no); -extern long int syscall_1 (long int syscall_no, long int arg1); -extern long int syscall_2 (long int syscall_no, long int arg1, long int arg2); -extern long int syscall_3 (long int syscall_no, long int arg1, long int arg2, long int arg3); - -/** Output of character. Writes the character c, cast to an unsigned char, to stdout. */ -int -putchar (int c) -{ - fwrite (&c, 1, sizeof (char), stdout); - - return c; -} /* putchar */ - -/** - * Output specified string - */ -int -puts (const char *s) /**< string to print */ -{ - while (*s) - { - putchar (*s); - - s++; - } - - return 0; -} /* puts */ - -/** - * Exit - cause normal process termination with specified status code - */ -void __attr_noreturn___ __attr_used___ -exit (int status) /**< status code */ -{ - syscall_1 (SYS_close, (long int) stdin); - syscall_1 (SYS_close, (long int) stdout); - syscall_1 (SYS_close, (long int) stderr); - - syscall_1 (SYS_exit, status); - - while (true) - { - /* unreachable */ - } -} /* exit */ - -/** - * Abort current process, producing an abnormal program termination. - * The function raises the SIGABRT signal. - */ -void __attr_noreturn___ __attr_used___ -abort (void) -{ - syscall_1 (SYS_close, (long int) stdin); - syscall_1 (SYS_close, (long int) stdout); - syscall_1 (SYS_close, (long int) stderr); - - raise (SIGABRT); - - while (true) - { - /* unreachable */ - } -} /* abort */ - -/** - * Send a signal to the current process. - */ -int __attr_used___ -raise (int sig) -{ - return (int) syscall_2 (SYS_kill, syscall_0 (SYS_getpid), sig); -} /* raise */ - -/** - * fopen - * - * @return FILE pointer - upon successful completion, - * NULL - otherwise - */ -FILE * -fopen (const char *path, /**< file path */ - const char *mode) /**< file open mode */ -{ - bool may_read = false; - bool may_write = false; - bool truncate = false; - bool create_if_not_exist = false; - bool position_at_end = false; - - assert (path != NULL && mode != NULL); - assert (mode[1] == '+' || mode[1] == '\0'); - - switch (mode[0]) - { - case 'r': - { - may_read = true; - may_write = (mode[1] == '+'); - break; - } - case 'w': - { - may_write = true; - truncate = true; - create_if_not_exist = true; - may_read = (mode[1] == '+'); - break; - } - case 'a': - { - may_write = true; - position_at_end = true; - create_if_not_exist = true; - if (mode[1] == '+') - { - assert (!"unsupported mode a+"); - } - break; - } - default: - { - assert (!"unsupported mode"); - } - } - - int flags = 0; - int access = S_IRUSR | S_IWUSR; - if (may_read && !may_write) - { - flags = O_RDONLY; - } - else if (!may_read && may_write) - { - flags = O_WRONLY; - } - else - { - assert (may_read && may_write); - - flags = O_RDWR; - } - - if (truncate) - { - flags |= O_TRUNC; - } - - if (create_if_not_exist) - { - flags |= O_CREAT; - } - - if (position_at_end) - { - flags |= O_APPEND; - } - - long int ret = syscall_3 (SYS_open, (long int) path, flags, access); - - return (void *) (uintptr_t) (ret); -} /* fopen */ - -/** - * The rewind () function sets the file position indicator - * for the stream pointed to by STREAM to the beginning of the file. - */ -void -rewind (FILE *stream) /**< stream pointer */ -{ - syscall_3 (SYS_lseek, (long int) stream, 0, SEEK_SET); -} /* rewind */ - -/** - * fclose - * - * @return 0 - upon successful completion, - * non-zero value - otherwise. - */ -int -fclose (FILE *fp) /**< stream pointer */ -{ - syscall_2 (SYS_close, (long int) fp, 0); - - return 0; -} /* fclose */ - -/** - * fseek - */ -int -fseek (FILE * fp, /**< stream pointer */ - long offset, /**< offset */ - int whence) /**< specifies position type - * to add offset to */ -{ - syscall_3 (SYS_lseek, (long int) fp, offset, whence); - - return 0; -} /* fseek */ - -/** - * ftell - */ -long -ftell (FILE * fp) /**< stream pointer */ -{ - long int ret = syscall_3 (SYS_lseek, (long int) fp, 0, SEEK_CUR); - - return ret; -} /* ftell */ - -/** - * fread - * - * @return number of elements read - */ -size_t -fread (void *ptr, /**< address of buffer to read to */ - size_t size, /**< size of elements to read */ - size_t nmemb, /**< number of elements to read */ - FILE *stream) /**< stream pointer */ -{ - long int ret; - size_t bytes_read = 0; - - if (size == 0) - { - return 0; - } - - do - { - ret = syscall_3 (SYS_read, - (long int) stream, - (long int) ((uint8_t *) ptr + bytes_read), - (long int) (size * nmemb - bytes_read)); - - bytes_read += (size_t) ret; - } - while (bytes_read != size * nmemb && ret != 0); - - return bytes_read / size; -} /* fread */ - -/** - * fwrite - * - * @return number of elements written - */ -size_t -fwrite (const void *ptr, /**< data to write */ - size_t size, /**< size of elements to write */ - size_t nmemb, /**< number of elements */ - FILE *stream) /**< stream pointer */ -{ - size_t bytes_written = 0; - - if (size == 0) - { - return 0; - } - - do - { - long int ret = syscall_3 (SYS_write, - (long int) stream, - (long int) ((uint8_t *) ptr + bytes_written), - (long int) (size * nmemb - bytes_written)); - - bytes_written += (size_t) ret; - } - while (bytes_written != size * nmemb); - - return bytes_written / size; -} /* fwrite */ - -/** - * This function can get the time as well as a timezone. - * - * @return 0 if success, -1 otherwise - */ -int -gettimeofday (void *tp, /**< struct timeval */ - void *tzp) /**< struct timezone */ -{ - return (int) syscall_2 (gettimeofday, (long int) tp, (long int) tzp); -} /* gettimeofday */ diff --git a/jerry-libc/target/linux/jerry-asm.S b/jerry-libc/target/linux/jerry-asm.S deleted file mode 100644 index 42b541b73..000000000 --- a/jerry-libc/target/linux/jerry-asm.S +++ /dev/null @@ -1,70 +0,0 @@ -#ifdef __TARGET_HOST_x64 -# include "arch/x86-64.h" -#elif defined (__TARGET_HOST_x86) -# include "arch/x86-32.h" -#elif defined (__TARGET_HOST_ARMv7) -# include "arch/arm-v7.h" -#else /* !__TARGET_HOST_x64 && !__TARGET_HOST_x86 && !__TARGET_HOST_ARMv7 */ -# error "!__TARGET_HOST_x64 && !__TARGET_HOST_x86 && !__TARGET_HOST_ARMv7" -#endif /* !__TARGET_HOST_x64 && !__TARGET_HOST_x86 && !__TARGET_HOST_ARMv7 */ - -.global _start -.type _start, %function -_start: - _START -.size _start, . - _start - -.global syscall_0 -.type syscall_0, %function -syscall_0: - SYSCALL_0 -.size syscall_0, . - syscall_0 - -.global syscall_1 -.type syscall_1, %function -syscall_1: - SYSCALL_1 -.size syscall_1, . - syscall_1 - -.global syscall_2 -.type syscall_2, %function -syscall_2: - SYSCALL_2 -.size syscall_2, . - syscall_2 - -.global syscall_3 -.type syscall_3, %function -syscall_3: - SYSCALL_3 -.size syscall_3, . - syscall_3 - -/** - * setjmp (jmp_buf env) - * - * See also: - * longjmp - * - * @return 0 - if returns from direct call, - * nonzero - if returns after longjmp. - */ -.global setjmp -.type setjmp, %function -setjmp: - _SETJMP -.size setjmp, . - setjmp - -/** - * longjmp (jmp_buf env, int val) - * - * Note: - * if val is not 0, then it would be returned from setjmp, - * otherwise - 0 would be returned. - * - * See also: - * setjmp - */ -.global longjmp -.type longjmp, %function -longjmp: - _LONGJMP -.size longjmp, . - setjmp diff --git a/jerry-libc/target/posix/jerry-asm.S b/jerry-libc/target/posix/jerry-asm.S new file mode 100644 index 000000000..2a33c6faa --- /dev/null +++ b/jerry-libc/target/posix/jerry-asm.S @@ -0,0 +1,76 @@ +#if defined (__TARGET_HOST_x64) +#include "arch/x86-64.h" +#elif defined (__TARGET_HOST_x86) +#include "arch/x86-32.h" +#elif defined (__TARGET_HOST_ARMv7) +#include "arch/arm-v7.h" +#else +#error "Unsupported architecture" +#endif /* !__TARGET_HOST_x64 && !__TARGET_HOST_x86 && !__TARGET_HOST_ARMv7 */ + +#if defined (__linux__) +.macro func _name +.global \_name +.type \_name, %function +\_name: +.endm +.macro endfunc _name +.size \_name, .-\_name +.endm +#elif defined (__APPLE__) && defined (__MACH__) +.macro func _name +.global \_name +\_name: +.endm +.macro endfunc _name +.endm +#else +#error "Unsupported OS" +#endif /* !__linux && !(__APPLE__ && __MACH__) */ + +func _start + _START +endfunc _start + +func syscall_0 + SYSCALL_0 +endfunc syscall_0 + +func syscall_1 + SYSCALL_1 +endfunc syscall_1 + +func syscall_2 + SYSCALL_2 +endfunc syscall_2 + +func syscall_3 + SYSCALL_3 +endfunc syscall_3 + +/** + * setjmp (jmp_buf env) + * + * See also: + * longjmp + * + * @return 0 - if returns from direct call, + * nonzero - if returns after longjmp. + */ +func setjmp + _SETJMP +endfunc setjmp + +/** + * longjmp (jmp_buf env, int val) + * + * Note: + * if val is not 0, then it would be returned from setjmp, + * otherwise - 0 would be returned. + * + * See also: + * setjmp + */ +func longjmp + _LONGJMP +endfunc longjmp diff --git a/jerry-libc/target/linux/jerry-libc-target.c b/jerry-libc/target/posix/jerry-libc-target.c similarity index 80% rename from jerry-libc/target/linux/jerry-libc-target.c rename to jerry-libc/target/posix/jerry-libc-target.c index fb3179dc9..c169280df 100644 --- a/jerry-libc/target/linux/jerry-libc-target.c +++ b/jerry-libc/target/posix/jerry-libc-target.c @@ -15,7 +15,7 @@ */ /** - * Jerry libc platform-specific functions linux implementation + * Jerry libc platform-specific functions posix implementation */ #include @@ -25,11 +25,20 @@ #include #include #include -#include +#include #include #include #include +#if defined (__linux__) +#define SYSCALL_NO(NAME) __NR_ ## NAME +#elif defined (__APPLE__) && defined (__MACH__) +#define SYS_exit_group SYS_exit +#define SYSCALL_NO(NAME) SYS_ ## NAME +#else +#error "Unsupported OS" +#endif /* !__linux && !(__APPLE__ && __MACH__) */ + #include "jerry-libc-defs.h" extern long int syscall_0 (long int syscall_no); @@ -68,11 +77,11 @@ puts (const char *s) /**< string to print */ void __attr_noreturn___ __attr_used___ exit (int status) /**< status code */ { - syscall_1 (__NR_close, (long int) stdin); - syscall_1 (__NR_close, (long int) stdout); - syscall_1 (__NR_close, (long int) stderr); + syscall_1 (SYSCALL_NO (close), (long int) stdin); + syscall_1 (SYSCALL_NO (close), (long int) stdout); + syscall_1 (SYSCALL_NO (close), (long int) stderr); - syscall_1 (__NR_exit_group, status); + syscall_1 (SYSCALL_NO (exit_group), status); while (true) { @@ -87,9 +96,9 @@ exit (int status) /**< status code */ void __attr_noreturn___ __attr_used___ abort (void) { - syscall_1 (__NR_close, (long int) stdin); - syscall_1 (__NR_close, (long int) stdout); - syscall_1 (__NR_close, (long int) stderr); + syscall_1 (SYSCALL_NO (close), (long int) stdin); + syscall_1 (SYSCALL_NO (close), (long int) stdout); + syscall_1 (SYSCALL_NO (close), (long int) stderr); raise (SIGABRT); @@ -105,7 +114,7 @@ abort (void) int __attr_used___ raise (int sig) { - return (int) syscall_2 (__NR_kill, syscall_0 (__NR_getpid), sig); + return (int) syscall_2 (SYSCALL_NO (kill), syscall_0 (SYSCALL_NO (getpid)), sig); } /* raise */ /** @@ -192,7 +201,7 @@ fopen (const char *path, /**< file path */ flags |= O_APPEND; } - long int ret = syscall_3 (__NR_open, (long int) path, flags, access); + long int ret = syscall_3 (SYSCALL_NO (open), (long int) path, flags, access); return (void *) (uintptr_t) (ret); } /* fopen */ @@ -204,7 +213,7 @@ fopen (const char *path, /**< file path */ void rewind (FILE *stream) /**< stream pointer */ { - syscall_3 (__NR_lseek, (long int) stream, 0, SEEK_SET); + syscall_3 (SYSCALL_NO (lseek), (long int) stream, 0, SEEK_SET); } /* rewind */ /** @@ -216,7 +225,7 @@ rewind (FILE *stream) /**< stream pointer */ int fclose (FILE *fp) /**< stream pointer */ { - syscall_2 (__NR_close, (long int) fp, 0); + syscall_2 (SYSCALL_NO (close), (long int) fp, 0); return 0; } /* fclose */ @@ -230,7 +239,7 @@ fseek (FILE * fp, /**< stream pointer */ int whence) /**< specifies position type * to add offset to */ { - syscall_3 (__NR_lseek, (long int) fp, offset, whence); + syscall_3 (SYSCALL_NO (lseek), (long int) fp, offset, whence); return 0; } /* fseek */ @@ -241,7 +250,7 @@ fseek (FILE * fp, /**< stream pointer */ long ftell (FILE * fp) /**< stream pointer */ { - long int ret = syscall_3 (__NR_lseek, (long int) fp, 0, SEEK_CUR); + long int ret = syscall_3 (SYSCALL_NO (lseek), (long int) fp, 0, SEEK_CUR); return ret; } /* ftell */ @@ -267,7 +276,7 @@ fread (void *ptr, /**< address of buffer to read to */ do { - ret = syscall_3 (__NR_read, + ret = syscall_3 (SYSCALL_NO (read), (long int) stream, (long int) ((uint8_t *) ptr + bytes_read), (long int) (size * nmemb - bytes_read)); @@ -299,7 +308,7 @@ fwrite (const void *ptr, /**< data to write */ do { - long int ret = syscall_3 (__NR_write, + long int ret = syscall_3 (SYSCALL_NO (write), (long int) stream, (long int) ((uint8_t *) ptr + bytes_written), (long int) (size * nmemb - bytes_written)); @@ -320,7 +329,7 @@ int gettimeofday (void *tp, /**< struct timeval */ void *tzp) /**< struct timezone */ { - return (int) syscall_2 (__NR_gettimeofday, (long int) tp, (long int) tzp); + return (int) syscall_2 (SYSCALL_NO (gettimeofday), (long int) tp, (long int) tzp); } /* gettimeofday */ // FIXME @@ -347,16 +356,16 @@ jrt_set_mem_limits (size_t data_size, /**< limit for data + bss + brk heap */ long int ret; #ifdef __TARGET_HOST_x64 - ret = syscall_2 (__NR_setrlimit, RLIMIT_DATA, (intptr_t) &data_limit); + ret = syscall_2 (SYSCALL_NO (setrlimit), RLIMIT_DATA, (intptr_t) &data_limit); assert (ret == 0); - ret = syscall_2 (__NR_setrlimit, RLIMIT_STACK, (intptr_t) &stack_limit); + ret = syscall_2 (SYSCALL_NO (setrlimit), RLIMIT_STACK, (intptr_t) &stack_limit); assert (ret == 0); #elif defined (__TARGET_HOST_ARMv7) - ret = syscall_3 (__NR_prlimit64, 0, RLIMIT_DATA, (intptr_t) &data_limit); + ret = syscall_3 (SYSCALL_NO (prlimit64), 0, RLIMIT_DATA, (intptr_t) &data_limit); assert (ret == 0); - ret = syscall_3 (__NR_prlimit64, 0, RLIMIT_STACK, (intptr_t) &stack_limit); + ret = syscall_3 (SYSCALL_NO (prlimit64), 0, RLIMIT_STACK, (intptr_t) &stack_limit); assert (ret == 0); #elif defined (__TARGET_HOST_x86) # error "__TARGET_HOST_x86 case is not implemented"