commit 5cc5ee423034284c2d22f1286981a2e8b37c2783 Author: sam Date: Mon May 27 21:30:27 2024 +1200 first commit diff --git a/c-embedding/Makefile b/c-embedding/Makefile new file mode 100644 index 0000000..551f5b7 --- /dev/null +++ b/c-embedding/Makefile @@ -0,0 +1,5 @@ +binary: main.c + gcc main.c -Iinclude -Llib -ltcc -o main -g + +run: binary + ./main diff --git a/c-embedding/adder/adder.c b/c-embedding/adder/adder.c new file mode 100644 index 0000000..59aab49 --- /dev/null +++ b/c-embedding/adder/adder.c @@ -0,0 +1,3 @@ +int add(int a, int b) { + return a + b; +} diff --git a/c-embedding/adder/adder.o b/c-embedding/adder/adder.o new file mode 100644 index 0000000..b5dfb98 Binary files /dev/null and b/c-embedding/adder/adder.o differ diff --git a/c-embedding/adder/libadder.a b/c-embedding/adder/libadder.a new file mode 100644 index 0000000..095ea0b Binary files /dev/null and b/c-embedding/adder/libadder.a differ diff --git a/c-embedding/env/include/adder.h b/c-embedding/env/include/adder.h new file mode 100644 index 0000000..d339620 --- /dev/null +++ b/c-embedding/env/include/adder.h @@ -0,0 +1,8 @@ +#ifndef __ADDER_H__ +#define __ADDER_H__ + +#pragma comment(lib, "adder") + +extern int add(int a, int b); + +#endif diff --git a/c-embedding/env/lib/libadder.a b/c-embedding/env/lib/libadder.a new file mode 100644 index 0000000..095ea0b Binary files /dev/null and b/c-embedding/env/lib/libadder.a differ diff --git a/c-embedding/env/test.c b/c-embedding/env/test.c new file mode 100644 index 0000000..ee5b16d --- /dev/null +++ b/c-embedding/env/test.c @@ -0,0 +1,6 @@ +#include +#include + +int main() { + printf("%d\n", add(5, 7)); +} diff --git a/c-embedding/include/libtcc.h b/c-embedding/include/libtcc.h new file mode 100644 index 0000000..5949c80 --- /dev/null +++ b/c-embedding/include/libtcc.h @@ -0,0 +1,116 @@ +#ifndef LIBTCC_H +#define LIBTCC_H + +#ifndef LIBTCCAPI +# define LIBTCCAPI +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/*****************************/ +/* set custom allocator for all allocations (optional), NULL for default. */ +typedef void *TCCReallocFunc(void *ptr, unsigned long size); +LIBTCCAPI void tcc_set_realloc(TCCReallocFunc *my_realloc); + +/*****************************/ +typedef struct TCCState TCCState; + +/* create a new TCC compilation context */ +LIBTCCAPI TCCState *tcc_new(void); + +/* free a TCC compilation context */ +LIBTCCAPI void tcc_delete(TCCState *s); + +/* set CONFIG_TCCDIR at runtime */ +LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path); + +/* set error/warning callback (optional) */ +typedef void TCCErrorFunc(void *opaque, const char *msg); +LIBTCCAPI void tcc_set_error_func(TCCState *s, void *error_opaque, TCCErrorFunc *error_func); + +/* set options as from command line (multiple supported) */ +LIBTCCAPI int tcc_set_options(TCCState *s, const char *str); + +/*****************************/ +/* preprocessor */ + +/* add include path */ +LIBTCCAPI int tcc_add_include_path(TCCState *s, const char *pathname); + +/* add in system include path */ +LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname); + +/* define preprocessor symbol 'sym'. value can be NULL, sym can be "sym=val" */ +LIBTCCAPI void tcc_define_symbol(TCCState *s, const char *sym, const char *value); + +/* undefine preprocess symbol 'sym' */ +LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym); + +/*****************************/ +/* compiling */ + +/* add a file (C file, dll, object, library, ld script). Return -1 if error. */ +LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename); + +/* compile a string containing a C source. Return -1 if error. */ +LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf); + +/* Tip: to have more specific errors/warnings from tcc_compile_string(), + you can prefix the string with "#line \"\"\n" */ + +/*****************************/ +/* linking commands */ + +/* set output type. MUST BE CALLED before any compilation */ +LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type); +#define TCC_OUTPUT_MEMORY 1 /* output will be run in memory */ +#define TCC_OUTPUT_EXE 2 /* executable file */ +#define TCC_OUTPUT_DLL 4 /* dynamic library */ +#define TCC_OUTPUT_OBJ 3 /* object file */ +#define TCC_OUTPUT_PREPROCESS 5 /* only preprocess */ + +/* equivalent to -Lpath option */ +LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname); + +/* the library name is the same as the argument of the '-l' option */ +LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname); + +/* add a symbol to the compiled program */ +LIBTCCAPI int tcc_add_symbol(TCCState *s, const char *name, const void *val); + +/* output an executable, library or object file. DO NOT call + tcc_relocate() before. */ +LIBTCCAPI int tcc_output_file(TCCState *s, const char *filename); + +/* link and run main() function and return its value. DO NOT call + tcc_relocate() before. */ +LIBTCCAPI int tcc_run(TCCState *s, int argc, char **argv); + +/* do all relocations (needed before using tcc_get_symbol()) */ +LIBTCCAPI int tcc_relocate(TCCState *s1); + +/* return symbol value or NULL if not found */ +LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name); + +/* list all (global) symbols and their values via 'symbol_cb()' */ +LIBTCCAPI void tcc_list_symbols(TCCState *s, void *ctx, + void (*symbol_cb)(void *ctx, const char *name, const void *val)); + +/* experimental/advanced section (see libtcc_test_mt.c for an example) */ + +/* catch runtime exceptions (optionally limit backtraces at top_func), + when using tcc_set_options("-bt") and when not using tcc_run() */ +LIBTCCAPI void *_tcc_setjmp(TCCState *s1, void *jmp_buf, void *top_func, void *longjmp); +#define tcc_setjmp(s1,jb,f) setjmp(_tcc_setjmp(s1, jb, f, longjmp)) + +/* custom error printer for runtime exceptions. Returning 0 stops backtrace */ +typedef int TCCBtFunc(void *udata, void *pc, const char *file, int line, const char* func, const char *msg); +LIBTCCAPI void tcc_set_backtrace_func(TCCState *s1, void* userdata, TCCBtFunc*); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/c-embedding/lib/libtcc.a b/c-embedding/lib/libtcc.a new file mode 100644 index 0000000..8f23dc3 Binary files /dev/null and b/c-embedding/lib/libtcc.a differ diff --git a/c-embedding/main b/c-embedding/main new file mode 100755 index 0000000..4744050 Binary files /dev/null and b/c-embedding/main differ diff --git a/c-embedding/main.c b/c-embedding/main.c new file mode 100644 index 0000000..b5129e5 --- /dev/null +++ b/c-embedding/main.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +#define SL_IMPLEMENTATION +#include + +#define ASSERT(x) assert(x == 0) + +void symbol_cb(void* ctx, const char* name, const void* val) { + printf("%s\n", name); +} + +int main() { + TCCState* state = tcc_new(); + ASSERT(tcc_set_output_type(state, TCC_OUTPUT_MEMORY)); + + ASSERT(tcc_add_include_path(state, "runtime/include")); + ASSERT(tcc_add_include_path(state, "env/include")); + + sl_string file = { 0 }; + printf("file: "); + char path[512]; + scanf("%s", path); + sl_read_file(path, &file); + ASSERT(tcc_compile_string(state, sl_c_str(file))); + + ASSERT(tcc_add_library_path(state, "runtime/lib")); + ASSERT(tcc_add_library_path(state, "env/lib")); + + // dumb testing shit + ASSERT(tcc_add_library_path(state, "lib")); + ASSERT(tcc_add_library(state, "tcc")); + + ASSERT(tcc_relocate(state)); + + int (*entry)() = tcc_get_symbol(state, "main"); + //tcc_list_symbols(state, NULL, symbol_cb); + + entry(); + + tcc_delete(state); +} diff --git a/c-embedding/runtime/include/float.h b/c-embedding/runtime/include/float.h new file mode 100644 index 0000000..24b7410 --- /dev/null +++ b/c-embedding/runtime/include/float.h @@ -0,0 +1,75 @@ +#ifndef _FLOAT_H_ +#define _FLOAT_H_ + +#define FLT_RADIX 2 + +/* IEEE float */ +#define FLT_MANT_DIG 24 +#define FLT_DIG 6 +#define FLT_ROUNDS 1 +#define FLT_EPSILON 1.19209290e-07F +#define FLT_MIN_EXP (-125) +#define FLT_MIN 1.17549435e-38F +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_EXP 128 +#define FLT_MAX 3.40282347e+38F +#define FLT_MAX_10_EXP 38 + +/* IEEE double */ +#define DBL_MANT_DIG 53 +#define DBL_DIG 15 +#define DBL_EPSILON 2.2204460492503131e-16 +#define DBL_MIN_EXP (-1021) +#define DBL_MIN 2.2250738585072014e-308 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_EXP 1024 +#define DBL_MAX 1.7976931348623157e+308 +#define DBL_MAX_10_EXP 308 + +/* horrible intel long double */ +#if defined __i386__ || defined __x86_64__ + +#define LDBL_MANT_DIG 64 +#define LDBL_DIG 18 +#define LDBL_EPSILON 1.08420217248550443401e-19L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176502e+4932L +#define LDBL_MAX_10_EXP 4932 +#define DECIMAL_DIG 21 + +#elif defined __aarch64__ || defined __riscv +/* + * Use values from: + * gcc -dM -E -xc /dev/null | grep LDBL | sed -e "s/__//g" + */ +#define LDBL_MANT_DIG 113 +#define LDBL_DIG 33 +#define LDBL_EPSILON 1.92592994438723585305597794258492732e-34L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626267781732175260e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176508575932662800702e+4932L +#define LDBL_MAX_EXP 16384 +#define DECIMAL_DIG 36 + +#else + +/* same as IEEE double */ +#define LDBL_MANT_DIG 53 +#define LDBL_DIG 15 +#define LDBL_EPSILON 2.2204460492503131e-16L +#define LDBL_MIN_EXP (-1021) +#define LDBL_MIN 2.2250738585072014e-308L +#define LDBL_MIN_10_EXP (-307) +#define LDBL_MAX_EXP 1024 +#define LDBL_MAX 1.7976931348623157e+308L +#define LDBL_MAX_10_EXP 308 +#define DECIMAL_DIG 17 + +#endif + +#endif /* _FLOAT_H_ */ diff --git a/c-embedding/runtime/include/stdalign.h b/c-embedding/runtime/include/stdalign.h new file mode 100644 index 0000000..ae46c34 --- /dev/null +++ b/c-embedding/runtime/include/stdalign.h @@ -0,0 +1,16 @@ +#ifndef _STDALIGN_H +#define _STDALIGN_H + +#if __STDC_VERSION__ < 201112L && (defined(__GNUC__) || defined(__TINYC__)) +# define _Alignas(t) __attribute__((__aligned__(t))) +# define _Alignof(t) __alignof__(t) +#endif + +#define alignas _Alignas +#define alignof _Alignof + +#define __alignas_is_defined 1 +#define __alignof_is_defined 1 + +#endif /* _STDALIGN_H */ + diff --git a/c-embedding/runtime/include/stdarg.h b/c-embedding/runtime/include/stdarg.h new file mode 100644 index 0000000..aa784da --- /dev/null +++ b/c-embedding/runtime/include/stdarg.h @@ -0,0 +1,14 @@ +#ifndef _STDARG_H +#define _STDARG_H + +typedef __builtin_va_list va_list; +#define va_start __builtin_va_start +#define va_arg __builtin_va_arg +#define va_copy __builtin_va_copy +#define va_end __builtin_va_end + +/* fix a buggy dependency on GCC in libio.h */ +typedef va_list __gnuc_va_list; +#define _VA_LIST_DEFINED + +#endif /* _STDARG_H */ diff --git a/c-embedding/runtime/include/stdatomic.h b/c-embedding/runtime/include/stdatomic.h new file mode 100644 index 0000000..2d423d1 --- /dev/null +++ b/c-embedding/runtime/include/stdatomic.h @@ -0,0 +1,173 @@ +/* This file is derived from clang's stdatomic.h */ + +/*===---- stdatomic.h - Standard header for atomic types and operations -----=== + * + * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + * + *===-----------------------------------------------------------------------=== + */ + +#ifndef _STDATOMIC_H +#define _STDATOMIC_H + +#include +#include +#include + +#define __ATOMIC_RELAXED 0 +#define __ATOMIC_CONSUME 1 +#define __ATOMIC_ACQUIRE 2 +#define __ATOMIC_RELEASE 3 +#define __ATOMIC_ACQ_REL 4 +#define __ATOMIC_SEQ_CST 5 + +/* Memory ordering */ +typedef enum { + memory_order_relaxed = __ATOMIC_RELAXED, + memory_order_consume = __ATOMIC_CONSUME, + memory_order_acquire = __ATOMIC_ACQUIRE, + memory_order_release = __ATOMIC_RELEASE, + memory_order_acq_rel = __ATOMIC_ACQ_REL, + memory_order_seq_cst = __ATOMIC_SEQ_CST, +} memory_order; + +/* Atomic typedefs */ +typedef _Atomic(_Bool) atomic_bool; +typedef _Atomic(char) atomic_char; +typedef _Atomic(signed char) atomic_schar; +typedef _Atomic(unsigned char) atomic_uchar; +typedef _Atomic(short) atomic_short; +typedef _Atomic(unsigned short) atomic_ushort; +typedef _Atomic(int) atomic_int; +typedef _Atomic(unsigned int) atomic_uint; +typedef _Atomic(long) atomic_long; +typedef _Atomic(unsigned long) atomic_ulong; +typedef _Atomic(long long) atomic_llong; +typedef _Atomic(unsigned long long) atomic_ullong; +typedef _Atomic(uint_least16_t) atomic_char16_t; +typedef _Atomic(uint_least32_t) atomic_char32_t; +typedef _Atomic(wchar_t) atomic_wchar_t; +typedef _Atomic(int_least8_t) atomic_int_least8_t; +typedef _Atomic(uint_least8_t) atomic_uint_least8_t; +typedef _Atomic(int_least16_t) atomic_int_least16_t; +typedef _Atomic(uint_least16_t) atomic_uint_least16_t; +typedef _Atomic(int_least32_t) atomic_int_least32_t; +typedef _Atomic(uint_least32_t) atomic_uint_least32_t; +typedef _Atomic(int_least64_t) atomic_int_least64_t; +typedef _Atomic(uint_least64_t) atomic_uint_least64_t; +typedef _Atomic(int_fast8_t) atomic_int_fast8_t; +typedef _Atomic(uint_fast8_t) atomic_uint_fast8_t; +typedef _Atomic(int_fast16_t) atomic_int_fast16_t; +typedef _Atomic(uint_fast16_t) atomic_uint_fast16_t; +typedef _Atomic(int_fast32_t) atomic_int_fast32_t; +typedef _Atomic(uint_fast32_t) atomic_uint_fast32_t; +typedef _Atomic(int_fast64_t) atomic_int_fast64_t; +typedef _Atomic(uint_fast64_t) atomic_uint_fast64_t; +typedef _Atomic(intptr_t) atomic_intptr_t; +typedef _Atomic(uintptr_t) atomic_uintptr_t; +typedef _Atomic(size_t) atomic_size_t; +typedef _Atomic(ptrdiff_t) atomic_ptrdiff_t; +typedef _Atomic(intmax_t) atomic_intmax_t; +typedef _Atomic(uintmax_t) atomic_uintmax_t; + +/* Atomic flag */ +typedef struct { + atomic_bool value; +} atomic_flag; + +#define ATOMIC_FLAG_INIT {0} +#define ATOMIC_VAR_INIT(value) (value) + +#define atomic_flag_test_and_set_explicit(object, order) \ + __atomic_test_and_set((void *)(&((object)->value)), order) +#define atomic_flag_test_and_set(object) \ + atomic_flag_test_and_set_explicit(object, __ATOMIC_SEQ_CST) + +#define atomic_flag_clear_explicit(object, order) \ + __atomic_clear((bool *)(&((object)->value)), order) +#define atomic_flag_clear(object) \ + atomic_flag_clear_explicit(object, __ATOMIC_SEQ_CST) + +/* Generic routines */ +#define atomic_init(object, desired) \ + atomic_store_explicit(object, desired, __ATOMIC_RELAXED) + +#define atomic_store_explicit(object, desired, order) \ + ({ __typeof__ (object) ptr = (object); \ + __typeof__ (*ptr) tmp = (desired); \ + __atomic_store (ptr, &tmp, (order)); \ + }) +#define atomic_store(object, desired) \ + atomic_store_explicit (object, desired, __ATOMIC_SEQ_CST) + +#define atomic_load_explicit(object, order) \ + ({ __typeof__ (object) ptr = (object); \ + __typeof__ (*ptr) tmp; \ + __atomic_load (ptr, &tmp, (order)); \ + tmp; \ + }) +#define atomic_load(object) atomic_load_explicit (object, __ATOMIC_SEQ_CST) + +#define atomic_exchange_explicit(object, desired, order) \ + ({ __typeof__ (object) ptr = (object); \ + __typeof__ (*ptr) val = (desired); \ + __typeof__ (*ptr) tmp; \ + __atomic_exchange (ptr, &val, &tmp, (order)); \ + tmp; \ + }) +#define atomic_exchange(object, desired) \ + atomic_exchange_explicit (object, desired, __ATOMIC_SEQ_CST) + +#define atomic_compare_exchange_strong_explicit(object, expected, desired, success, failure) \ + ({ __typeof__ (object) ptr = (object); \ + __typeof__ (*ptr) tmp = desired; \ + __atomic_compare_exchange(ptr, expected, &tmp, 0, success, failure); \ + }) +#define atomic_compare_exchange_strong(object, expected, desired) \ + atomic_compare_exchange_strong_explicit (object, expected, desired, \ + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) + +#define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure) \ + ({ __typeof__ (object) ptr = (object); \ + __typeof__ (*ptr) tmp = desired; \ + __atomic_compare_exchange(ptr, expected, &tmp, 1, success, failure); \ + }) +#define atomic_compare_exchange_weak(object, expected, desired) \ + atomic_compare_exchange_weak_explicit (object, expected, desired, \ + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) + +#define atomic_fetch_add(object, operand) \ + __atomic_fetch_add(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_add_explicit __atomic_fetch_add + +#define atomic_fetch_sub(object, operand) \ + __atomic_fetch_sub(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_sub_explicit __atomic_fetch_sub + +#define atomic_fetch_or(object, operand) \ + __atomic_fetch_or(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_or_explicit __atomic_fetch_or + +#define atomic_fetch_xor(object, operand) \ + __atomic_fetch_xor(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_xor_explicit __atomic_fetch_xor + +#define atomic_fetch_and(object, operand) \ + __atomic_fetch_and(object, operand, __ATOMIC_SEQ_CST) +#define atomic_fetch_and_explicit __atomic_fetch_and + +extern void atomic_thread_fence (memory_order); +extern void __atomic_thread_fence (memory_order); +#define atomic_thread_fence(order) __atomic_thread_fence (order) +extern void atomic_signal_fence (memory_order); +extern void __atomic_signal_fence (memory_order); +#define atomic_signal_fence(order) __atomic_signal_fence (order) +extern bool __atomic_is_lock_free(size_t size, void *ptr); +#define atomic_is_lock_free(OBJ) __atomic_is_lock_free (sizeof (*(OBJ)), (OBJ)) + +extern bool __atomic_test_and_set (void *, memory_order); +extern void __atomic_clear (bool *, memory_order); + +#endif /* _STDATOMIC_H */ diff --git a/c-embedding/runtime/include/stdbool.h b/c-embedding/runtime/include/stdbool.h new file mode 100644 index 0000000..d2ee446 --- /dev/null +++ b/c-embedding/runtime/include/stdbool.h @@ -0,0 +1,11 @@ +#ifndef _STDBOOL_H +#define _STDBOOL_H + +/* ISOC99 boolean */ + +#define bool _Bool +#define true 1 +#define false 0 +#define __bool_true_false_are_defined 1 + +#endif /* _STDBOOL_H */ diff --git a/c-embedding/runtime/include/stddef.h b/c-embedding/runtime/include/stddef.h new file mode 100644 index 0000000..da9b9e0 --- /dev/null +++ b/c-embedding/runtime/include/stddef.h @@ -0,0 +1,41 @@ +#ifndef _STDDEF_H +#define _STDDEF_H + +typedef __SIZE_TYPE__ size_t; +typedef __PTRDIFF_TYPE__ ssize_t; +typedef __WCHAR_TYPE__ wchar_t; +typedef __PTRDIFF_TYPE__ ptrdiff_t; +typedef __PTRDIFF_TYPE__ intptr_t; +typedef __SIZE_TYPE__ uintptr_t; + +#if __STDC_VERSION__ >= 201112L +typedef union { long long __ll; long double __ld; } max_align_t; +#endif + +#ifndef NULL +#define NULL ((void*)0) +#endif + +#undef offsetof +#define offsetof(type, field) ((size_t)&((type *)0)->field) + +#if defined __i386__ || defined __x86_64__ +void *alloca(size_t size); +#endif + +#endif + +/* Older glibc require a wint_t from (when requested + by __need_wint_t, as otherwise stddef.h isn't allowed to + define this type). Note that this must be outside the normal + _STDDEF_H guard, so that it works even when we've included the file + already (without requiring wint_t). Some other libs define _WINT_T + if they've already provided that type, so we can use that as guard. + TCC defines __WINT_TYPE__ for us. */ +#if defined (__need_wint_t) +#ifndef _WINT_T +#define _WINT_T +typedef __WINT_TYPE__ wint_t; +#endif +#undef __need_wint_t +#endif diff --git a/c-embedding/runtime/include/stdnoreturn.h b/c-embedding/runtime/include/stdnoreturn.h new file mode 100644 index 0000000..4d580ea --- /dev/null +++ b/c-embedding/runtime/include/stdnoreturn.h @@ -0,0 +1,7 @@ +#ifndef _STDNORETURN_H +#define _STDNORETURN_H + +/* ISOC11 noreturn */ +#define noreturn _Noreturn + +#endif /* _STDNORETURN_H */ diff --git a/c-embedding/runtime/include/tccdefs.h b/c-embedding/runtime/include/tccdefs.h new file mode 100644 index 0000000..8ec6464 --- /dev/null +++ b/c-embedding/runtime/include/tccdefs.h @@ -0,0 +1,333 @@ +/* tccdefs.h + + Nothing is defined before this file except target machine, target os + and the few things related to option settings in tccpp.c:tcc_predefs(). + + This file is either included at runtime as is, or converted and + included as C-strings at compile-time (depending on CONFIG_TCC_PREDEFS). + + Note that line indent matters: + + - in lines starting at column 1, platform macros are replaced by + corresponding TCC target compile-time macros. See conftest.c for + the list of platform macros supported in lines starting at column 1. + + - only lines indented >= 4 are actually included into the executable, + check tccdefs_.h. +*/ + +#if __SIZEOF_POINTER__ == 4 + /* 32bit systems. */ +#if defined __OpenBSD__ + #define __SIZE_TYPE__ unsigned long + #define __PTRDIFF_TYPE__ long +#else + #define __SIZE_TYPE__ unsigned int + #define __PTRDIFF_TYPE__ int +#endif + #define __ILP32__ 1 + #define __INT64_TYPE__ long long +#elif __SIZEOF_LONG__ == 4 + /* 64bit Windows. */ + #define __SIZE_TYPE__ unsigned long long + #define __PTRDIFF_TYPE__ long long + #define __LLP64__ 1 + #define __INT64_TYPE__ long long +#else + /* Other 64bit systems. */ + #define __SIZE_TYPE__ unsigned long + #define __PTRDIFF_TYPE__ long + #define __LP64__ 1 +# if defined __linux__ + #define __INT64_TYPE__ long +# else /* APPLE, BSD */ + #define __INT64_TYPE__ long long +# endif +#endif + #define __SIZEOF_INT__ 4 + #define __INT_MAX__ 0x7fffffff +#if __SIZEOF_LONG__ == 4 + #define __LONG_MAX__ 0x7fffffffL +#else + #define __LONG_MAX__ 0x7fffffffffffffffL +#endif + #define __SIZEOF_LONG_LONG__ 8 + #define __LONG_LONG_MAX__ 0x7fffffffffffffffLL + #define __CHAR_BIT__ 8 + #define __ORDER_LITTLE_ENDIAN__ 1234 + #define __ORDER_BIG_ENDIAN__ 4321 + #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#if defined _WIN32 + #define __WCHAR_TYPE__ unsigned short + #define __WINT_TYPE__ unsigned short +#elif defined __linux__ + #define __WCHAR_TYPE__ int + #define __WINT_TYPE__ unsigned int +#else + #define __WCHAR_TYPE__ int + #define __WINT_TYPE__ int +#endif + + #if __STDC_VERSION__ >= 201112L + # define __STDC_NO_ATOMICS__ 1 + # define __STDC_NO_COMPLEX__ 1 + # define __STDC_NO_THREADS__ 1 +#if !defined _WIN32 + # define __STDC_UTF_16__ 1 + # define __STDC_UTF_32__ 1 +#endif + #endif + +#if defined _WIN32 + #define __declspec(x) __attribute__((x)) + #define __cdecl + +#elif defined __FreeBSD__ + #define __GNUC__ 9 + #define __GNUC_MINOR__ 3 + #define __GNUC_PATCHLEVEL__ 0 + #define __GNUC_STDC_INLINE__ 1 + #define __NO_TLS 1 + #define __RUNETYPE_INTERNAL 1 +# if __SIZEOF_POINTER__ == 8 + /* FIXME, __int128_t is used by setjump */ + #define __int128_t struct { unsigned char _dummy[16] __attribute((aligned(16))); } + #define __SIZEOF_SIZE_T__ 8 + #define __SIZEOF_PTRDIFF_T__ 8 +#else + #define __SIZEOF_SIZE_T__ 4 + #define __SIZEOF_PTRDIFF_T__ 4 +# endif + +#elif defined __FreeBSD_kernel__ + +#elif defined __NetBSD__ + #define __GNUC__ 4 + #define __GNUC_MINOR__ 1 + #define __GNUC_PATCHLEVEL__ 0 + #define _Pragma(x) + #define __ELF__ 1 +#if defined __aarch64__ + #define _LOCORE /* avoids usage of __asm */ +#endif + +#elif defined __OpenBSD__ + #define __GNUC__ 4 + #define _ANSI_LIBRARY 1 + +#elif defined __APPLE__ + /* emulate APPLE-GCC to make libc's headerfiles compile: */ + #define __GNUC__ 4 /* darwin emits warning on GCC<4 */ + #define __APPLE_CC__ 1 /* for */ + #define __LITTLE_ENDIAN__ 1 + #define _DONT_USE_CTYPE_INLINE_ 1 + /* avoids usage of GCC/clang specific builtins in libc-headerfiles: */ + #define __FINITE_MATH_ONLY__ 1 + #define _FORTIFY_SOURCE 0 + //#define __has_builtin(x) 0 + +#elif defined __ANDROID__ + #define BIONIC_IOCTL_NO_SIGNEDNESS_OVERLOAD + +#else + /* Linux */ + +#endif + + /* Some derived integer types needed to get stdint.h to compile correctly on some platforms */ +#ifndef __NetBSD__ + #define __UINTPTR_TYPE__ unsigned __PTRDIFF_TYPE__ + #define __INTPTR_TYPE__ __PTRDIFF_TYPE__ +#endif + #define __INT32_TYPE__ int + +#if !defined _WIN32 + /* glibc defines. We do not support __USER_NAME_PREFIX__ */ + #define __REDIRECT(name, proto, alias) name proto __asm__ (#alias) + #define __REDIRECT_NTH(name, proto, alias) name proto __asm__ (#alias) __THROW + #define __REDIRECT_NTHNL(name, proto, alias) name proto __asm__ (#alias) __THROWNL +#endif + + /* not implemented */ + #define __PRETTY_FUNCTION__ __FUNCTION__ + #define __has_builtin(x) 0 + #define __has_feature(x) 0 + /* C23 Keywords */ + #define _Nonnull + #define _Nullable + #define _Nullable_result + #define _Null_unspecified + + /* skip __builtin... with -E */ + #ifndef __TCC_PP__ + + #define __builtin_offsetof(type, field) ((__SIZE_TYPE__)&((type*)0)->field) + #define __builtin_extract_return_addr(x) x +#if !defined __linux__ && !defined _WIN32 + /* used by math.h */ + #define __builtin_huge_val() 1e500 + #define __builtin_huge_valf() 1e50f + #define __builtin_huge_vall() 1e5000L +# if defined __APPLE__ + #define __builtin_nanf(ignored_string) (0.0F/0.0F) + /* used by floats.h to implement FLT_ROUNDS C99 macro. 1 == to nearest */ + #define __builtin_flt_rounds() 1 + /* used by _fd_def.h */ + #define __builtin_bzero(p, ignored_size) bzero(p, sizeof(*(p))) +# else + #define __builtin_nanf(ignored_string) (0.0F/0.0F) +# endif +#endif + + /* __builtin_va_list */ +#if defined __x86_64__ +#if !defined _WIN32 + /* GCC compatible definition of va_list. */ + /* This should be in sync with the declaration in our lib/libtcc1.c */ + typedef struct { + unsigned gp_offset, fp_offset; + union { + unsigned overflow_offset; + char *overflow_arg_area; + }; + char *reg_save_area; + } __builtin_va_list[1]; + + void *__va_arg(__builtin_va_list ap, int arg_type, int size, int align); + #define __builtin_va_start(ap, last) \ + (*(ap) = *(__builtin_va_list)((char*)__builtin_frame_address(0) - 24)) + #define __builtin_va_arg(ap, t) \ + (*(t *)(__va_arg(ap, __builtin_va_arg_types(t), sizeof(t), __alignof__(t)))) + #define __builtin_va_copy(dest, src) (*(dest) = *(src)) + +#else /* _WIN64 */ + typedef char *__builtin_va_list; + #define __builtin_va_arg(ap, t) ((sizeof(t) > 8 || (sizeof(t) & (sizeof(t) - 1))) \ + ? **(t **)((ap += 8) - 8) : *(t *)((ap += 8) - 8)) +#endif + +#elif defined __arm__ + typedef char *__builtin_va_list; + #define _tcc_alignof(type) ((int)&((struct {char c;type x;} *)0)->x) + #define _tcc_align(addr,type) (((unsigned)addr + _tcc_alignof(type) - 1) \ + & ~(_tcc_alignof(type) - 1)) + #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)) + #define __builtin_va_arg(ap,type) (ap = (void *) ((_tcc_align(ap,type)+sizeof(type)+3) \ + &~3), *(type *)(ap - ((sizeof(type)+3)&~3))) + +#elif defined __aarch64__ +#if defined __APPLE__ + typedef struct { + void *__stack; + } __builtin_va_list; + +#else + typedef struct { + void *__stack, *__gr_top, *__vr_top; + int __gr_offs, __vr_offs; + } __builtin_va_list; + +#endif +#elif defined __riscv + typedef char *__builtin_va_list; + #define __va_reg_size (__riscv_xlen >> 3) + #define _tcc_align(addr,type) (((unsigned long)addr + __alignof__(type) - 1) \ + & -(__alignof__(type))) + #define __builtin_va_arg(ap,type) (*(sizeof(type) > (2*__va_reg_size) ? *(type **)((ap += __va_reg_size) - __va_reg_size) : (ap = (va_list)(_tcc_align(ap,type) + (sizeof(type)+__va_reg_size - 1)& -__va_reg_size), (type *)(ap - ((sizeof(type)+ __va_reg_size - 1)& -__va_reg_size))))) + +#else /* __i386__ */ + typedef char *__builtin_va_list; + #define __builtin_va_start(ap,last) (ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)) + #define __builtin_va_arg(ap,t) (*(t*)((ap+=(sizeof(t)+3)&~3)-((sizeof(t)+3)&~3))) + +#endif + #define __builtin_va_end(ap) (void)(ap) + #ifndef __builtin_va_copy + # define __builtin_va_copy(dest, src) (dest) = (src) + #endif + + /* TCC BBUILTIN AND BOUNDS ALIASES */ + #ifdef __leading_underscore + # define __RENAME(X) __asm__("_"X) + #else + # define __RENAME(X) __asm__(X) + #endif + + #ifdef __TCC_BCHECK__ + # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME("__bound_"#name); + # define __BOUND(ret,name,params) ret name params __RENAME("__bound_"#name); + #else + # define __BUILTINBC(ret,name,params) ret __builtin_##name params __RENAME(#name); + # define __BOUND(ret,name,params) + #endif +#ifdef _WIN32 + #define __BOTH __BOUND + #define __BUILTIN(ret,name,params) +#else + #define __BOTH(ret,name,params) __BUILTINBC(ret,name,params)__BOUND(ret,name,params) + #define __BUILTIN(ret,name,params) ret __builtin_##name params __RENAME(#name); +#endif + + __BOTH(void*, memcpy, (void *, const void*, __SIZE_TYPE__)) + __BOTH(void*, memmove, (void *, const void*, __SIZE_TYPE__)) + __BOTH(void*, memset, (void *, int, __SIZE_TYPE__)) + __BOTH(int, memcmp, (const void *, const void*, __SIZE_TYPE__)) + __BOTH(__SIZE_TYPE__, strlen, (const char *)) + __BOTH(char*, strcpy, (char *, const char *)) + __BOTH(char*, strncpy, (char *, const char*, __SIZE_TYPE__)) + __BOTH(int, strcmp, (const char*, const char*)) + __BOTH(int, strncmp, (const char*, const char*, __SIZE_TYPE__)) + __BOTH(char*, strcat, (char*, const char*)) + __BOTH(char*, strncat, (char*, const char*, __SIZE_TYPE__)) + __BOTH(char*, strchr, (const char*, int)) + __BOTH(char*, strrchr, (const char*, int)) + __BOTH(char*, strdup, (const char*)) +#if defined __ARM_EABI__ + __BOUND(void*,__aeabi_memcpy,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memmove,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memmove4,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memmove8,(void*,const void*,__SIZE_TYPE__)) + __BOUND(void*,__aeabi_memset,(void*,int,__SIZE_TYPE__)) +#endif + +#if defined __linux__ || defined __APPLE__ // HAVE MALLOC_REDIR + #define __MAYBE_REDIR __BUILTIN +#else + #define __MAYBE_REDIR __BOTH +#endif + __MAYBE_REDIR(void*, malloc, (__SIZE_TYPE__)) + __MAYBE_REDIR(void*, realloc, (void *, __SIZE_TYPE__)) + __MAYBE_REDIR(void*, calloc, (__SIZE_TYPE__, __SIZE_TYPE__)) + __MAYBE_REDIR(void*, memalign, (__SIZE_TYPE__, __SIZE_TYPE__)) + __MAYBE_REDIR(void, free, (void*)) +#if defined __i386__ || defined __x86_64__ + __BOTH(void*, alloca, (__SIZE_TYPE__)) +#else + __BUILTIN(void*, alloca, (__SIZE_TYPE__)) +#endif + __BUILTIN(void, abort, (void)) + __BOUND(void, longjmp, ()) +#if !defined _WIN32 + __BOUND(void*, mmap, ()) + __BOUND(int, munmap, ()) +#endif + #undef __BUILTINBC + #undef __BUILTIN + #undef __BOUND + #undef __BOTH + #undef __MAYBE_REDIR + #undef __RENAME + + #define __BUILTIN_EXTERN(name,u) \ + int __builtin_##name(u int); \ + int __builtin_##name##l(u long); \ + int __builtin_##name##ll(u long long); + __BUILTIN_EXTERN(ffs,) + __BUILTIN_EXTERN(clz, unsigned) + __BUILTIN_EXTERN(ctz, unsigned) + __BUILTIN_EXTERN(clrsb,) + __BUILTIN_EXTERN(popcount, unsigned) + __BUILTIN_EXTERN(parity, unsigned) + #undef __BUILTIN_EXTERN + + #endif /* ndef __TCC_PP__ */ diff --git a/c-embedding/runtime/include/tgmath.h b/c-embedding/runtime/include/tgmath.h new file mode 100644 index 0000000..5d3e357 --- /dev/null +++ b/c-embedding/runtime/include/tgmath.h @@ -0,0 +1,89 @@ +/* + * ISO C Standard: 7.22 Type-generic math + */ + +#ifndef _TGMATH_H +#define _TGMATH_H + +#include + +#ifndef __cplusplus +#define __tgmath_real(x, F) \ + _Generic ((x), float: F##f, long double: F##l, default: F)(x) +#define __tgmath_real_2_1(x, y, F) \ + _Generic ((x), float: F##f, long double: F##l, default: F)(x, y) +#define __tgmath_real_2(x, y, F) \ + _Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y) +#define __tgmath_real_3_2(x, y, z, F) \ + _Generic ((x)+(y), float: F##f, long double: F##l, default: F)(x, y, z) +#define __tgmath_real_3(x, y, z, F) \ + _Generic ((x)+(y)+(z), float: F##f, long double: F##l, default: F)(x, y, z) + +/* Functions defined in both and (7.22p4) */ +#define acos(z) __tgmath_real(z, acos) +#define asin(z) __tgmath_real(z, asin) +#define atan(z) __tgmath_real(z, atan) +#define acosh(z) __tgmath_real(z, acosh) +#define asinh(z) __tgmath_real(z, asinh) +#define atanh(z) __tgmath_real(z, atanh) +#define cos(z) __tgmath_real(z, cos) +#define sin(z) __tgmath_real(z, sin) +#define tan(z) __tgmath_real(z, tan) +#define cosh(z) __tgmath_real(z, cosh) +#define sinh(z) __tgmath_real(z, sinh) +#define tanh(z) __tgmath_real(z, tanh) +#define exp(z) __tgmath_real(z, exp) +#define log(z) __tgmath_real(z, log) +#define pow(z1,z2) __tgmath_real_2(z1, z2, pow) +#define sqrt(z) __tgmath_real(z, sqrt) +#define fabs(z) __tgmath_real(z, fabs) + +/* Functions defined in only (7.22p5) */ +#define atan2(x,y) __tgmath_real_2(x, y, atan2) +#define cbrt(x) __tgmath_real(x, cbrt) +#define ceil(x) __tgmath_real(x, ceil) +#define copysign(x,y) __tgmath_real_2(x, y, copysign) +#define erf(x) __tgmath_real(x, erf) +#define erfc(x) __tgmath_real(x, erfc) +#define exp2(x) __tgmath_real(x, exp2) +#define expm1(x) __tgmath_real(x, expm1) +#define fdim(x,y) __tgmath_real_2(x, y, fdim) +#define floor(x) __tgmath_real(x, floor) +#define fma(x,y,z) __tgmath_real_3(x, y, z, fma) +#define fmax(x,y) __tgmath_real_2(x, y, fmax) +#define fmin(x,y) __tgmath_real_2(x, y, fmin) +#define fmod(x,y) __tgmath_real_2(x, y, fmod) +#define frexp(x,y) __tgmath_real_2_1(x, y, frexp) +#define hypot(x,y) __tgmath_real_2(x, y, hypot) +#define ilogb(x) __tgmath_real(x, ilogb) +#define ldexp(x,y) __tgmath_real_2_1(x, y, ldexp) +#define lgamma(x) __tgmath_real(x, lgamma) +#define llrint(x) __tgmath_real(x, llrint) +#define llround(x) __tgmath_real(x, llround) +#define log10(x) __tgmath_real(x, log10) +#define log1p(x) __tgmath_real(x, log1p) +#define log2(x) __tgmath_real(x, log2) +#define logb(x) __tgmath_real(x, logb) +#define lrint(x) __tgmath_real(x, lrint) +#define lround(x) __tgmath_real(x, lround) +#define nearbyint(x) __tgmath_real(x, nearbyint) +#define nextafter(x,y) __tgmath_real_2(x, y, nextafter) +#define nexttoward(x,y) __tgmath_real_2(x, y, nexttoward) +#define remainder(x,y) __tgmath_real_2(x, y, remainder) +#define remquo(x,y,z) __tgmath_real_3_2(x, y, z, remquo) +#define rint(x) __tgmath_real(x, rint) +#define round(x) __tgmath_real(x, round) +#define scalbln(x,y) __tgmath_real_2_1(x, y, scalbln) +#define scalbn(x,y) __tgmath_real_2_1(x, y, scalbn) +#define tgamma(x) __tgmath_real(x, tgamma) +#define trunc(x) __tgmath_real(x, trunc) + +/* Functions defined in only (7.22p6) +#define carg(z) __tgmath_cplx_only(z, carg) +#define cimag(z) __tgmath_cplx_only(z, cimag) +#define conj(z) __tgmath_cplx_only(z, conj) +#define cproj(z) __tgmath_cplx_only(z, cproj) +#define creal(z) __tgmath_cplx_only(z, creal) +*/ +#endif /* __cplusplus */ +#endif /* _TGMATH_H */ diff --git a/c-embedding/runtime/include/varargs.h b/c-embedding/runtime/include/varargs.h new file mode 100644 index 0000000..d614366 --- /dev/null +++ b/c-embedding/runtime/include/varargs.h @@ -0,0 +1,12 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the w64 mingw-runtime package. + * No warranty is given; refer to the file DISCLAIMER within this package. + */ +#ifndef _VARARGS_H +#define _VARARGS_H + +#error "TinyCC no longer implements ." +#error "Revise your code to use ." + +#endif diff --git a/c-embedding/runtime/lib/libtcc1.a b/c-embedding/runtime/lib/libtcc1.a new file mode 100644 index 0000000..aeff838 Binary files /dev/null and b/c-embedding/runtime/lib/libtcc1.a differ diff --git a/c-embedding/runtime/lib/runmain.o b/c-embedding/runtime/lib/runmain.o new file mode 100644 index 0000000..37004f4 Binary files /dev/null and b/c-embedding/runtime/lib/runmain.o differ diff --git a/cs-test/main.cs b/cs-test/main.cs new file mode 100644 index 0000000..c69cfcf --- /dev/null +++ b/cs-test/main.cs @@ -0,0 +1,7 @@ +using System; + +class CSTesting { + public static void Main(string[] args) { + Console.WriteLine("balls"); + } +} diff --git a/cs-test/main.exe b/cs-test/main.exe new file mode 100755 index 0000000..05b645a Binary files /dev/null and b/cs-test/main.exe differ diff --git a/img-format/compress b/img-format/compress new file mode 100755 index 0000000..2714c8b Binary files /dev/null and b/img-format/compress differ diff --git a/img-format/compress.c b/img-format/compress.c new file mode 100644 index 0000000..3fb9538 --- /dev/null +++ b/img-format/compress.c @@ -0,0 +1,30 @@ +#include +#include +#include + +int main() { + SetRandomSeed(time(NULL)); + + int width = 10000; + int height = 10000; + int size = width * height; + + uint8_t* data = calloc(sizeof(uint8_t), size); + uint8_t col = GetRandomValue(0, 255); + int count = 0; + for(int x = 0; x < width; x++) { + for(int y = 0; y < height; y++) { + //if(GetRandomValue(0, 500) < 50) { + count++; + col = GetRandomValue(0, 255); + //} + data[y * height + x] = col; + } + } + + int comp_size = 0; + uint8_t* compressed = CompressData(data, size, &comp_size); + SaveFileData("img.plif", compressed, comp_size); + + printf("%d different color strips, %d -> %d\n", count, size, comp_size); +} diff --git a/img-format/decompress b/img-format/decompress new file mode 100755 index 0000000..b15be6a Binary files /dev/null and b/img-format/decompress differ diff --git a/img-format/decompress.c b/img-format/decompress.c new file mode 100644 index 0000000..a761a08 --- /dev/null +++ b/img-format/decompress.c @@ -0,0 +1,17 @@ +#include +#include +#include + +int main() { + int comp_size; + uint8_t* compressed = LoadFileData("img.plif", &comp_size); + + int size; + uint8_t* data = DecompressData(compressed, comp_size, &size); + + for(int i = 0; i < size; i++) { + printf("0b%08b ", data[i]); + } + + printf("size: %d\n", size); +} diff --git a/img-format/img.plif b/img-format/img.plif new file mode 100644 index 0000000..bfc800b Binary files /dev/null and b/img-format/img.plif differ diff --git a/img-format/img.plif.lzma b/img-format/img.plif.lzma new file mode 100644 index 0000000..893588f Binary files /dev/null and b/img-format/img.plif.lzma differ diff --git a/img-format/img.plif.xz b/img-format/img.plif.xz new file mode 100644 index 0000000..3ff0532 Binary files /dev/null and b/img-format/img.plif.xz differ diff --git a/luajit-test/a.out b/luajit-test/a.out new file mode 100755 index 0000000..e31b79c Binary files /dev/null and b/luajit-test/a.out differ diff --git a/luajit-test/main.c b/luajit-test/main.c new file mode 100644 index 0000000..ef8d443 --- /dev/null +++ b/luajit-test/main.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include + +#define lua_check(x) if(x != LUA_OK) printf("ERR: %s\n", lua_tostring(L, -1)) + +#define LUA_ARGS lua_State* L +#define arg(type) lua_to##type(L, 1) + +int logfn(LUA_ARGS) { + const char* value = arg(string); + + printf("[LOG] %s\n", value); + + return 0; +} + +int main() { + lua_State* L = luaL_newstate(); + assert(L != NULL); + luaL_openlibs(L); + + lua_pushcfunction(L, logfn); + lua_setglobal(L, "log"); + + lua_check(luaL_dofile(L, "test.lua")); + + void* data = lua_newuserdata(L); + +} + diff --git a/luajit-test/test.lua b/luajit-test/test.lua new file mode 100644 index 0000000..b7a9392 --- /dev/null +++ b/luajit-test/test.lua @@ -0,0 +1,5 @@ +function dolog(name, text) + log(name .. text) +end + +dolog("hello", "bob") diff --git a/mem-locating-poc/helpers.c b/mem-locating-poc/helpers.c new file mode 100644 index 0000000..fc889c0 --- /dev/null +++ b/mem-locating-poc/helpers.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include "helpers.h" + +uint64_t get_base_address(uint32_t pid) { + char maps_file[MAX_PATH_LEN]; + FILE* file; + char line[MAX_LINE_LEN]; + uint64_t base_address = 0; + + snprintf(maps_file, MAX_PATH_LEN, "/proc/%d/maps", pid); + + file = fopen(maps_file, "r"); + if (file == NULL) { + perror("fopen"); + exit(EXIT_FAILURE); + } + + while (fgets(line, sizeof(line), file)) { + char *address = strtok(line, "-"); + if (address != NULL) { + base_address = strtoul(address, NULL, 16); + break; + } + } + + fclose(file); + + return base_address; +} diff --git a/mem-locating-poc/helpers.h b/mem-locating-poc/helpers.h new file mode 100644 index 0000000..c5d6525 --- /dev/null +++ b/mem-locating-poc/helpers.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +#define MAX_PATH_LEN 256 +#define MAX_LINE_LEN 2048 + +uint64_t get_base_address(uint32_t pid); diff --git a/mem-locating-poc/main b/mem-locating-poc/main new file mode 100755 index 0000000..4517bf4 Binary files /dev/null and b/mem-locating-poc/main differ diff --git a/mem-locating-poc/main.c b/mem-locating-poc/main.c new file mode 100644 index 0000000..66a9eb0 --- /dev/null +++ b/mem-locating-poc/main.c @@ -0,0 +1,11 @@ +#include +#include +#include + +int num = 5; + +int main() { + void* handle = dlopen("./test.so", RTLD_LAZY); + void (*handle_func)() = dlsym(handle, "test"); + handle_func(); +} diff --git a/mem-locating-poc/test.c b/mem-locating-poc/test.c new file mode 100644 index 0000000..8d31a5e --- /dev/null +++ b/mem-locating-poc/test.c @@ -0,0 +1,14 @@ +#include +#include +#include "helpers.h" + +uint64_t num_addr = 0x4020; + +void test() { + uint32_t pid = getpid(); + uint64_t base = get_base_address(pid); + printf("pid: %u, base: %p\n", pid, base); + + int* num = (int*)(base + num_addr); + printf("%d\n", *num); +} diff --git a/mem-locating-poc/test.so b/mem-locating-poc/test.so new file mode 100755 index 0000000..f7435be Binary files /dev/null and b/mem-locating-poc/test.so differ diff --git a/minesweeper-asm/minesweeper.asm b/minesweeper-asm/minesweeper.asm new file mode 100644 index 0000000..80f065c --- /dev/null +++ b/minesweeper-asm/minesweeper.asm @@ -0,0 +1,30 @@ +global _start + +mmap equ 0x9 + +PROT_READ equ 0x1 +PROT_WRITE equ 0x2 + +section .text +_start: + +create_board: + mov [board + board_t.width], rax + mov [board + board_t.height], rdi + + mov rax, mmap + mov rdi, 0 + mov rsi, [board + board_t.width] + mul rsi, [board + board_t.height] + mov rdx, PROT_READ | PROT_WRITE + mov + syscall + +section .bss +struc board_t + .width resd 1 + .height resd 1 + .board resd 1 +endstruc + +board: resb board_t_size diff --git a/minesweeper-c/a.out b/minesweeper-c/a.out new file mode 100755 index 0000000..4c8a88d Binary files /dev/null and b/minesweeper-c/a.out differ diff --git a/minesweeper-c/minesweeper b/minesweeper-c/minesweeper new file mode 100755 index 0000000..f9bcbdf Binary files /dev/null and b/minesweeper-c/minesweeper differ diff --git a/minesweeper-c/minesweeper.c b/minesweeper-c/minesweeper.c new file mode 100644 index 0000000..dfeed7f --- /dev/null +++ b/minesweeper-c/minesweeper.c @@ -0,0 +1,68 @@ +#include +#include +#include + +typedef struct Board { + char* board; + int width, height, mine_rng; +} Board; + +typedef enum Difficulty { + Easy, + Medium, + Hard +} Difficulty; + +Board create_board(Difficulty difficulty) { + Board board = { 0 }; + + switch(difficulty) { + case Easy: + board.width = 25; + board.height = 25; + board.mine_rng = 10; + break; + + case Medium: + board.width = 35; + board.height = 35; + board.mine_rng = 7; + break; + + case Hard: + board.width = 50; + board.height = 50; + board.mine_rng = 5; + break; + } + + board.board = calloc(sizeof(char), board.width * board.height); + return board; +} + +void generate_board(Board board) { + for(int x = 0; x < board.width; x++) { + for(int y = 0; y < board.height; y++) { + int index = y * board.width + x; + if(rand() % board.mine_rng == 0) + board.board[index] = 'b'; + else + board.board[index] = ' '; + } + } +} + +void print_board(Board board) { + for(int x = 0; x < board.width; x++) { + for(int y = 0; y < board.height; y++) + printf("%c", board.board[y * board.width + x]); + printf("\n"); + } +} + +int main() { + srand(time(NULL)); + Board board = create_board(Easy); + generate_board(board); + print_board(board); +} diff --git a/minesweeper/main.cs b/minesweeper/main.cs new file mode 100644 index 0000000..619930b --- /dev/null +++ b/minesweeper/main.cs @@ -0,0 +1,8 @@ +using Minesweeper; + +class Application { + public static void Main(string[] args) { + Game game = new Game(args); + game.Run(); + } +} diff --git a/minesweeper/main.exe b/minesweeper/main.exe new file mode 100755 index 0000000..856b209 Binary files /dev/null and b/minesweeper/main.exe differ diff --git a/minesweeper/minesweeper.cs b/minesweeper/minesweeper.cs new file mode 100644 index 0000000..3c24133 --- /dev/null +++ b/minesweeper/minesweeper.cs @@ -0,0 +1,83 @@ +using System; + +namespace Minesweeper { + class Game { + Board board; + + public Game(string[] args) { + board = new Board(Board.Difficulty.Easy); + } + + public void Run() { + board.Generate(); + board.Print(); + } + } + + class Board { + public enum Difficulty { + Easy, + Medium, + Hard + } + + private char[,] board; + private int width; + private int height; + private int mineRNG; + + private Random rng = new Random(); + + // For Difficulty-scaled Boards + public Board(Difficulty difficulty) { + switch(difficulty) { + case Difficulty.Easy: + mineRNG = 10; + + width = 25; + height = 25; + break; + + case Difficulty.Medium: + mineRNG = 7; + + width = 35; + height = 35; + break; + case Difficulty.Hard: + mineRNG = 5; + + width = 50; + height = 50; + break; + } + + board = new char[width, height]; + } + // For Custom Boards + public Board(int width, int height) { + board = new char[width, height]; + this.width = width; + this.height = height; + } + + public void Generate() { + for (int y = 0; y < width; y++) { + for (int x = 0; x < height; x++) { + if (rng.Next(mineRNG) == 0) + board[x, y] = 'b'; + else + board[x, y] = ' '; + } + } + } + + public void Print() { + for (int y = 0; y < width; y++) { + for (int x = 0; x < height; x++) + Console.Write(board[x, y]); + Console.WriteLine(); + } + } + } +} diff --git a/nn/a.out b/nn/a.out new file mode 100755 index 0000000..a230b88 Binary files /dev/null and b/nn/a.out differ diff --git a/nn/main.c b/nn/main.c new file mode 100644 index 0000000..8df80de --- /dev/null +++ b/nn/main.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include + +typedef struct Layer { + int neuron_count; + float* neurons; + float* weights; +} Layer; + +typedef struct Network { + struct Layer* layers; + int layer_count; +} Network; + +Layer create_layer(int count, int next) { + Layer layer; + layer.neuron_count = count; + layer.neurons = calloc(sizeof(float), count); + layer.weights = calloc(sizeof(float), count * next); + return layer; +} + +Network* create_network(int input, int hidden, int output) { + Network* network = calloc(sizeof(Network), 1); + network->layer_count = 3; + network->layers = calloc(sizeof(Layer), 3); + network->layers[0] = create_layer(input, hidden); + network->layers[1] = create_layer(hidden, output); + network->layers[2] = create_layer(output, 0); + + return network; +} + +float activation_func(float in) { + return 1.0f / (1.0f + powf(2.71828f, -in)); +} + +float randomf() { + return (rand() % 1000) / 1000.0f; +} + +int main() { + srand(time(NULL)); + + Network* net = create_network(2, 3, 1); + + for(int i = 0; i < net->layer_count - 1; i++) { + for(int j = 0; j < net->layers[i].neuron_count; j++) { + for(int m = 0; m < net->layers[i + 1].neuron_count; m++) { + net->layers[i].weights[j * net->layers[i + 1].neuron_count + m] = (randomf() - 0.5f) * 2.0f; + } + } + } + + net->layers[0].neurons[0] = 1.0f; + net->layers[0].neurons[1] = 0.0f; + + for(int layer = 1; layer < net->layer_count; layer++) { + for(int n = 0; n < net->layers[layer].neuron_count; n++) { + float sum = 0.0f; + Layer l_back = net->layers[layer - 1]; + + for(int n_back = 0; n_back < l_back.neuron_count; n_back++) { + sum += l_back.neurons[n_back] * + l_back.weights[n_back * l_back.neuron_count + n]; + } + + net->layers[layer].neurons[n] = activation_func(sum); + printf("layer[%d].neurons[%d] = %f\n", layer, n, net->layers[layer].neurons[n]); + } + } +} diff --git a/nn/test.c b/nn/test.c new file mode 100644 index 0000000..21d3b7b --- /dev/null +++ b/nn/test.c @@ -0,0 +1,6 @@ +#include + +int main() { + srand(time(NULL)); + printf("%d\n", rand()); +} diff --git a/tinypaint/a.out b/tinypaint/a.out new file mode 100755 index 0000000..6d7ed05 Binary files /dev/null and b/tinypaint/a.out differ diff --git a/tinypaint/window.c b/tinypaint/window.c new file mode 100644 index 0000000..a7a245d --- /dev/null +++ b/tinypaint/window.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +int main() { + Display *display; + Window window; + int screen; + GC gc; + XEvent event; + + display = XOpenDisplay(NULL); + if (display == NULL) { + fprintf(stderr, "Unable to open X display\n"); + exit(1); + } + + screen = DefaultScreen(display); + window = XCreateSimpleWindow(display, RootWindow(display, screen), 10, 10, 800, 600, 1, + BlackPixel(display, screen), WhitePixel(display, screen)); + + XSelectInput(display, window, ExposureMask | KeyPressMask); + XMapWindow(display, window); + + gc = XCreateGC(display, window, 0, NULL); + XSetForeground(display, gc, BlackPixel(display, screen)); + + XGetWindowAttributes(display, window, &attributes); + *width = attributes.width; + *height = attributes.height; + + int width = 800; + int height = 600; + int depth = DefaultDepth(display, screen); + + // Create a pixel buffer + uint32_t* pixel_buffer = (uint32_t*)malloc(width * height * sizeof(uint32_t)); + if (pixel_buffer == NULL) { + fprintf(stderr, "Unable to allocate pixel buffer\n"); + exit(1); + } + + // Initialize pixel buffer (example: fill with blue color) + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + pixel_buffer[y * width + x] = 0x0000FF; // Blue color + } + } + + // Create an XImage + XImage *image = XCreateImage(display, DefaultVisual(display, screen), depth, ZPixmap, 0, + (char *)pixel_buffer, width, height, 32, 0); + + while(1) { + XNextEvent(display, &event); + + if (event.type == Expose) { + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + } + } + + XDestroyImage(image); + XFreeGC(display, gc); + XDestroyWindow(display, window); + XCloseDisplay(display); + + return 0; +}