Files
CWasm/cwasm.h

92 lines
3.3 KiB
C

/*
File: cwasm.h
Author: Taylor Robbins
Date: 08\28\2025
Description:
** This header should be included before any other header and at the top of every compilation unit.
** It in turn includes any files that are needed for a CWasm-based application to function
*/
#ifndef _CWASM_H
#define _CWASM_H
// +--------------------------------------------------------------+
// | Check Compiler |
// +--------------------------------------------------------------+
#if !defined(__clang__)
#error CWasm is only tested to function with Clang as the compiler!
#endif
#if !defined(__clang_major__) || !defined(__clang_minor__) || !defined(__clang_patchlevel__)
#error Missing defines for __clang_major__, __clang_minor__, and __clang_patchlevel__!
#endif
#if !defined(__wasm32__)
#error CWasm only works when compiling for 32-bit WebAssembly!
#endif
#if defined(__EMSCRIPTEN__)
#error CWasm is not meant to be compiled with Emscripten. It functions as a standalone platform for C applications that only depend on a small subset of the C standard library
#endif
#ifndef __BYTE_ORDER__
#error Missing __BYTE_ORDER__ define!
#elif __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
#error This standard library implementation assumes little-endian byte order
#endif
#ifdef __cplusplus
#define LANGUAGE_IS_C 0
#define LANGUAGE_IS_CPP 1
#else
#define LANGUAGE_IS_C 1
#define LANGUAGE_IS_CPP 0
#endif
#if LANGUAGE_IS_C
#define nullptr ((void*)0)
#endif
#define START_EXTERN_C extern "C" {
#define END_EXTERN_C }
#if LANGUAGE_IS_CPP
#define MAYBE_EXTERN_C EXTERN_C
#define MAYBE_START_EXTERN_C START_EXTERN_C
#define MAYBE_END_EXTERN_C END_EXTERN_C
#else
#define MAYBE_EXTERN_C //nothing
#define MAYBE_START_EXTERN_C //nothing
#define MAYBE_END_EXTERN_C //nothing
#endif
#define WASM_MEMORY_PAGE_SIZE (64*1024ULL) //64kB or 65,536b
#define WASM_MEMORY_MAX_NUM_PAGES (64*1024ULL) //65,536 pages * 64 kB/page = 4GB
#define WASM_MEMORY_MAX_SIZE ((u64)WASM_MEMORY_MAX_NUM_PAGES * (u64)WASM_MEMORY_PAGE_SIZE)
#define WASM_PROTECTED_SIZE 1024 //1kB at start of wasm memory should be unused and should never be written to
#define WASM_EXPORT(functionName) MAYBE_EXTERN_C __attribute__((export_name(#functionName)))
// +--------------------------------------------------------------+
// | Standard Includes |
// +--------------------------------------------------------------+
// NOTE: These headers are all contained in CWasm's "std" folder, they are not "real" C standard library headers
#include <stdint.h>
#include <string.h>
// +--------------------------------------------------------------+
// | Basic Type Names |
// +--------------------------------------------------------------+
// We use an "i" to indicate it can hold integer numbers
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef long long i64;
// We use a "u" to distinguish these as only holding unsigned numbers (they are still integers, but "i" is already taken)
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef unsigned long long u64;
// We use an "r" to indicate it can hold real numbers
typedef float r32;
typedef double r64;
#endif // _CWASM_H