[contrib] Add freestanding translator prototype

This is the idea, some of the functionality isn't yet implemented.
This commit is contained in:
Nick Terrell
2020-08-26 12:26:05 -07:00
parent c465f24457
commit ae455dde08
4 changed files with 496 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
# ################################################################
# Copyright (c) 2015-2020, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under both the BSD-style license (found in the
# LICENSE file in the root directory of this source tree) and the GPLv2 (found
# in the COPYING file in the root directory of this source tree).
# You may select, at your option, one of the above-listed licenses.
# ################################################################
all: run-test
.PHONY: libzstd
libzstd: clean
../freestanding.py --source-lib ../../../lib --output-lib ./libzstd
test: libzstd
$(CC) $(CFLAGS) $(CPPFLAGS) -ffreestanding $(wildcard libzstd/*/*.c) -c
$(CC) $(CFLAGS) $(CPPFLAGS) -ffreestanding -c test.c
$(CC) $(LDFLAGS) -ffreestanding -nostdlib *.o -o test
run-test: test
./test
clean:
rm -rf ./libzstd/ *.o test
+99
View File
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2016-2020, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree).
* You may select, at your option, one of the above-listed licenses.
*/
#ifndef __x86_64__
# error This test only works on x86-64
#endif
#include "./libzstd/zstd.h"
void* ZSTD_malloc(size_t size) {
(void)size;
return NULL;
}
void ZSTD_free(void* ptr) {
(void)ptr;
}
void* ZSTD_calloc(size_t num, size_t size) {
(void)num;
(void)size;
return NULL;
}
/* The standard requires these three functions be present.
* The compiler is free to insert calls to these functions.
*/
void* memmove(void* destination, const void* source, size_t num) {
char* const d = (char*)destination;
char const* const s = (char const*)source;
if (s < d) {
for (size_t i = num; i-- > 0;) {
d[i] = s[i];
}
} else {
for (size_t i = 0; i < num; ++i) {
d[i] = s[i];
}
}
return destination;
}
void* memcpy(void* destination, const void* source, size_t num) {
return memmove(destination, source, num);
}
void* memset(void* destination, int value, size_t num) {
char* const d = (char*)destination;
for (size_t i = 0; i < num; ++i) {
d[i] = value;
}
return destination;
}
void* ZSTD_memmove(void* destination, const void* source, size_t num) {
return memmove(destination, source, num);
}
void* ZSTD_memcpy(void* destination, const void* source, size_t num) {
return memmove(destination, source, num);
}
void* ZSTD_memset(void* destination, int value, size_t num) {
return memset(destination, value, num);
}
int main(void) {
char dst[100];
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
ZSTD_freeCCtx(cctx);
if (cctx != NULL) {
return 1;
}
if (!ZSTD_isError(ZSTD_compress(dst, sizeof(dst), NULL, 0, 1))) {
return 2;
}
return 0;
}
static inline long syscall1(long syscall, long arg1) {
long ret;
__asm__ volatile
(
"syscall"
: "=a" (ret)
: "a" (syscall), "D" (arg1)
: "rcx", "r11", "memory"
);
return ret;
}
static inline void exit(int status) {
syscall1(60, status);
}
void _start(void) {
int const ret = main();
exit(ret);
}
+106
View File
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2016-2020, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree).
* You may select, at your option, one of the above-listed licenses.
*/
/* Need:
* NULL
* ZSTD_memcpy()
* ZSTD_memset()
* ZSTD_memmove()
* BYTE
* S16
* U16
* U32
* U64
* size_t
* ptrdiff_t
* INT_MAX
* ...
*/
#ifndef ZSTD_DEPS_COMMON
#define ZSTD_DEPS_COMMON
#include <stddef.h>
typedef unsigned char BYTE;
typedef unsigned short U16;
typedef signed short S16;
typedef unsigned int U32;
typedef signed int S32;
typedef unsigned long long U64;
typedef signed long long S64;
#ifndef INT_MAX
# define INT_MAX ((int)((1u << 31) - 1))
#endif
#if defined(__GNUC__) && __GNUC__ >= 4
#define ZSTD_memcpy(d,s,n) __builtin_memcpy((d),(s),(n))
#define ZSTD_memmove(d,s,n) __builtin_memmove((d),(s),(n))
#define ZSTD_memset(d,s,n) __builtin_memset((d),(s),(n))
#else
void* ZSTD_memcpy(void* destination, const void* source, size_t num);
void* ZSTD_memmove(void* destination, const void* source, size_t num);
void* ZSTD_memset(void* destination, int value, size_t num);
#endif
#endif /* ZSTD_DEPS_COMMON */
/* Need:
* ZSTD_malloc()
* ZSTD_free()
* ZSTD_calloc()
*/
#ifdef ZSTD_DEPS_NEED_MALLOC
#ifndef ZSTD_DEPS_MALLOC
#define ZSTD_DEPS_MALLOC
void* ZSTD_malloc(size_t size);
void ZSTD_free(void* ptr);
void* ZSTD_calloc(size_t num, size_t size);
#endif /* ZSTD_DEPS_MALLOC */
#endif /* ZSTD_DEPS_NEED_MALLOC */
/* Need:
* assert()
*/
#ifdef ZSTD_DEPS_NEED_ASSERT
#ifndef ZSTD_DEPS_ASSERT
#define ZSTD_DEPS_ASSERT
#define assert(x) ((void)0)
#endif /* ZSTD_DEPS_ASSERT */
#endif /* ZSTD_DEPS_NEED_ASSERT */
/* Need:
* ZSTD_DEBUG_PRINT()
*/
#ifdef ZSTD_DEPS_NEED_IO
#ifndef ZSTD_DEPS_IO
#define ZSTD_DEPS_IO
#define ZSTD_DEBUG_PRINT(...)
#endif /* ZSTD_DEPS_IO */
#endif /* ZSTD_DEPS_NEED_IO */
/* Only requested when <stdint.h> is known to be present.
* Need:
* intptr_t
*/
#ifdef ZSTD_DEPS_NEED_STDINT
#ifndef ZSTD_DEPS_STDINT
#define ZSTD_DEPS_STDINT
#define intptr_t size_t
#endif /* ZSTD_DEPS_STDINT */
#endif /* ZSTD_DEPS_NEED_STDINT */