From d5c61681198527d70bc95f8ed10f19e881ac51b3 Mon Sep 17 00:00:00 2001 From: lemon Date: Wed, 24 Aug 2022 07:04:02 +0200 Subject: arch stuff --- .gitignore | 1 + bootstrap/bootstrap.sh | 2 ++ src/cffc.hff | 2 ++ src/libc.hff | 2 ++ src/main.cff | 8 +++++++- src/targ.cff | 14 ++++++++++++++ 6 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index eb3e33c..7f31903 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ compile_commands.json a.out .gdb_history .cache/ +src/host-target-triple.cff diff --git a/bootstrap/bootstrap.sh b/bootstrap/bootstrap.sh index f221ce2..1d682dc 100755 --- a/bootstrap/bootstrap.sh +++ b/bootstrap/bootstrap.sh @@ -16,6 +16,8 @@ export CFLAGS="-Wno-builtin-declaration-mismatch -Wno-discarded-qualifiers -g" set -euo pipefail +echo "extern static host_target_triple const *const u8 = \"`clang -print-target-triple`\";" > ../src/host-target-triple.cff + mkdir -p obj1/ for f in $(find ../src/ -name '*.cff'); do out=obj1/$(basename "$f").o diff --git a/src/cffc.hff b/src/cffc.hff index d327766..21418b9 100644 --- a/src/cffc.hff +++ b/src/cffc.hff @@ -382,7 +382,9 @@ extern fn fatal(*Parser, Loc, fmt *const u8, ...) void; // targ.cff extern static g_targ Targ; +extern static host_target_triple const *const u8; extern fn targ_ini(name *const u8) bool; +extern fn triple2targ(triple *const u8) *const u8; // type.cff extern static ty_void *const Type, diff --git a/src/libc.hff b/src/libc.hff index a6c326c..0744d35 100644 --- a/src/libc.hff +++ b/src/libc.hff @@ -29,11 +29,13 @@ def PATH_MAX = 4096; // string.h extern fn strlen(s *const u8) usize; extern fn strcmp(a *const u8, b *const u8) int; +extern fn strncmp(a *const u8, b *const u8, usize) int; extern fn memcmp(*const void, *const void, usize) int; extern fn strcasecmp(a *const u8, b *const u8) int; extern fn memcpy(*void, *const void, usize) *void; extern fn strcpy(*u8, *const u8) *u8; extern fn strerror(int) *const u8; +extern fn strstr(*const u8, *const u8) *const u8; // ctype.h extern fn tolower(int) int; diff --git a/src/main.cff b/src/main.cff index d2330ed..32664fd 100644 --- a/src/main.cff +++ b/src/main.cff @@ -4,7 +4,13 @@ import "common.hff"; extern fn main(argc int, argv **u8) int { assert(argc > 1, "args?"); - targ_ini("amd64-sysv"); + let triple = host_target_triple; + let targ = triple2targ(triple); + if targ == #null { + fprintf(stderr, "error: unsupported target triple `%s'\n", triple); + return 1; + } + targ_ini(targ); let p = Parser {}; parser_init(&p, argv[1]); diff --git a/src/targ.cff b/src/targ.cff index c5fc093..f0b500b 100644 --- a/src/targ.cff +++ b/src/targ.cff @@ -31,3 +31,17 @@ extern fn targ_ini(name *const u8) bool { } return #f; } + +extern fn triple2targ(triple *const u8) *const u8 { + let arch = (do + let delim = strstr(triple, "-"); + delim ? triple[0::delim - triple] : spanz(triple); + ); + let rest = arch.#ptr + arch.#len; + defmacro archp(s) [ (strncmp(s, arch.#ptr, arch.#len) == 0) ] + defmacro restp(s) [ (strstr(rest, s) != #null) ] + switch { + case archp("x86_64") and !restp("windows"); return "amd64-sysv"; + } + return #null; +} -- cgit v1.2.3