Commit: 2908510e6bf25dbad116105e3113fe6e490112b7
Parent: 68774a77b36018c502c243f545aa5808f9aa56f0
Author: Randy Palamar
Date: Tue, 22 Oct 2024 10:04:27 -0600
move basic typedefs into the platform code
this makes it so that the libc free version doesn't use any
headers and allows you to accurately count actual program lines of
code without white space and comments with `cpp -P platform_linux_amd64.c`
doing this is completely fine in the platform code since the size
of C types are well defined if you consider the architecture of the CPU.
Diffstat:
5 files changed, 38 insertions(+), 24 deletions(-)
diff --git a/build.sh b/build.sh
@@ -16,7 +16,7 @@ src=platform_posix.c
case $(uname -sm) in
"Linux x86_64")
src=platform_linux_amd64.c
- cflags="${cflags} -nostdlib -ffreestanding -fno-stack-protector -Wl,--gc-sections"
+ cflags="${cflags} -nostdinc -nostdlib -ffreestanding -fno-stack-protector -Wl,--gc-sections"
;;
esac
diff --git a/jdict.c b/jdict.c
@@ -1,16 +1,4 @@
/* See LICENSE for license details. */
-#include <stdint.h>
-#include <stddef.h>
-typedef uint8_t u8;
-typedef int64_t i64;
-typedef uint64_t u64;
-typedef int32_t i32;
-typedef uint32_t u32;
-typedef uint32_t b32;
-typedef uint16_t u16;
-typedef ptrdiff_t size;
-typedef ptrdiff_t iptr;
-
#ifdef _DEBUG
#define ASSERT(c) do { __asm("int3; nop"); } while (0)
#else
@@ -181,8 +169,8 @@ static void *
alloc_(Arena *a, size len, size align, size count, u32 flags)
{
size padding;
- if (flags & ARENA_ALLOC_END) padding = (uintptr_t)a->end & (align - 1);
- else padding = -(uintptr_t)a->beg & (align - 1);
+ if (flags & ARENA_ALLOC_END) padding = (usize)a->end & (align - 1);
+ else padding = -(usize)a->beg & (align - 1);
size available = a->end - a->beg - padding;
if (available <= 0 || available / len <= count)
@@ -355,7 +343,8 @@ parse_term_bank(Arena *a, struct ht *ht, s8 data)
continue;
YomiTok *tstr = NULL, *tdefs = NULL;
- for (size_t j = 1; j < base_tok->len; j++) {
+ for (usize j = 1; j < base_tok->len; j++) {
+
switch (base_tok[j].type) {
case YOMI_STR:
if (tstr == NULL)
@@ -394,7 +383,7 @@ parse_term_bank(Arena *a, struct ht *ht, s8 data)
}
}
- for (size_t i = 1; i <= tdefs->len; i++) {
+ for (usize i = 1; i <= tdefs->len; i++) {
DictDef *def = alloc(a, DictDef, 1, ARENA_NO_CLEAR);
def->text = s8_dup(a, (s8){.len = tdefs[i].end - tdefs[i].start,
.s = data.s + tdefs[i].start});
@@ -437,9 +426,9 @@ make_dict(Arena *a, Dict *d)
}
static void
-make_dicts(Arena *a, Dict *dicts, size_t ndicts)
+make_dicts(Arena *a, Dict *dicts, u32 ndicts)
{
- for (size_t i = 0; i < ndicts; i++) {
+ for (u32 i = 0; i < ndicts; i++) {
if (!make_dict(a, &dicts[i])) {
stream_append_s8(&error_stream, s8("make_dict failed for: "));
stream_append_s8(&error_stream, dicts[i].rom);
@@ -493,7 +482,7 @@ find_and_print(s8 term, Dict *d)
}
static void
-find_and_print_defs(Arena *a, Dict *dict, s8 *terms, size_t nterms)
+find_and_print_defs(Arena *a, Dict *dict, s8 *terms, u32 nterms)
{
if (!make_dict(a, dict)) {
stream_append_s8(&error_stream, s8("failed to allocate dict: "));
@@ -502,7 +491,7 @@ find_and_print_defs(Arena *a, Dict *dict, s8 *terms, size_t nterms)
return;
}
- for (size_t i = 0; i < nterms; i++)
+ for (u32 i = 0; i < nterms; i++)
find_and_print(terms[i], dict);
}
@@ -523,7 +512,7 @@ get_stdin_line(Stream *buf)
}
static void
-repl(Arena *a, Dict *dicts, size_t ndicts)
+repl(Arena *a, Dict *dicts, u32 ndicts)
{
Stream buf = {.cap = 4096};
buf.data = alloc(a, u8, buf.cap, ARENA_NO_CLEAR);
@@ -537,7 +526,7 @@ repl(Arena *a, Dict *dicts, size_t ndicts)
if (!get_stdin_line(&buf))
break;
s8 trimmed = s8trim((s8){.len = buf.widx, .s = buf.data});
- for (size_t i = 0; i < ndicts; i++)
+ for (u32 i = 0; i < ndicts; i++)
find_and_print(trimmed, &dicts[i]);
buf.widx = 0;
}
diff --git a/platform_linux.c b/platform_linux.c
@@ -1,6 +1,8 @@
/* See LICENSE for license details. */
#define os_path_sep s8("/")
+#define NULL ((void *)0)
+
#include "jdict.c"
#define PROT_READ 0x01
@@ -27,7 +29,7 @@ typedef struct {
/* NOTE: necessary garbage required by GCC/CLANG even when -nostdlib is used */
__attribute((section(".text.memset")))
-void *memset(void *d, int c, size_t n)
+void *memset(void *d, int c, usize n)
{
return mem_clear(d, c, n);
}
diff --git a/platform_linux_amd64.c b/platform_linux_amd64.c
@@ -1,4 +1,14 @@
/* See LICENSE for license details. */
+typedef unsigned char u8;
+typedef signed long i64;
+typedef unsigned long u64;
+typedef signed int i32;
+typedef unsigned int u32;
+typedef unsigned int b32;
+typedef unsigned short u16;
+typedef signed long size;
+typedef unsigned long usize;
+typedef signed long iptr;
#ifndef asm
#ifdef __asm
diff --git a/platform_posix.c b/platform_posix.c
@@ -5,6 +5,19 @@
#include <sys/stat.h>
#include <unistd.h>
+#include <stdint.h>
+#include <stddef.h>
+typedef uint8_t u8;
+typedef int64_t i64;
+typedef uint64_t u64;
+typedef int32_t i32;
+typedef uint32_t u32;
+typedef uint32_t b32;
+typedef uint16_t u16;
+typedef ptrdiff_t size;
+typedef size_t usize;
+typedef ptrdiff_t iptr;
+
#define os_path_sep s8("/")
#include "jdict.c"