Skip to content

Commit

Permalink
Enable specifying target architecture and platform on Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Dec 21, 2023
1 parent b3ffd31 commit 5d13783
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 35 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ ifeq ("$(ARCHTYPE)", "")
ARCHTYPE:=aarch64
endif
endif
ARCHTYPE_UPPER:=$(shell echo "$(ARCHTYPE)" | tr \'[a-z]\' \'[A-Z]\')

CC1_ARCH_DIR:=$(CC1_DIR)/arch/$(ARCHTYPE)
CC1_FE_DIR:=$(CC1_DIR)/frontend
Expand Down Expand Up @@ -104,7 +105,7 @@ $(foreach D, $(EXES), $(eval $(call DEFINE_EXE_TARGET,$(D))))
define DEFINE_OBJ_TARGET
$(OBJ_DIR)/%.o: $(1)/%.c $(PARENT_DEPS)
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c -o $$@ $$<
$(CC) $(CFLAGS) -DXCC_TARGET_ARCH=XCC_ARCH_$(ARCHTYPE_UPPER) -c -o $$@ $$<
endef
XCC_SRC_DIRS:=$(XCC_DIR) $(CC1_FE_DIR) $(CC1_BE_DIR) $(CC1_DIR) $(CC1_ARCH_DIR) $(CPP_DIR) \
$(AS_DIR) $(LD_DIR) $(UTIL_DIR) $(DEBUG_DIR)
Expand Down Expand Up @@ -171,7 +172,7 @@ test-self-hosting: self-hosting
WCC_OBJ_DIR:=obj/wcc

WCC_DIR:=src/wcc
WCC_CFLAGS:=$(CFLAGS) -I$(CPP_DIR) -DTARGET_WASM
WCC_CFLAGS:=$(CFLAGS) -I$(CPP_DIR)

WCC_SRCS:=$(wildcard $(WCC_DIR)/*.c) \
$(wildcard $(CC1_FE_DIR)/*.c) \
Expand All @@ -186,7 +187,7 @@ wcc: $(PARENT_DEPS) $(WCC_OBJS) $(WCC_LIBS)
define DEFINE_WCCOBJ_TARGET
$(WCC_OBJ_DIR)/%.o: $(1)/%.c $(PARENT_DEPS)
@mkdir -p $(WCC_OBJ_DIR)
$(CC) $(WCC_CFLAGS) -c -o $$@ $$<
$(CC) $(WCC_CFLAGS) -DXCC_TARGET_ARCH=XCC_ARCH_WASM -c -o $$@ $$<
endef
WCC_SRC_DIRS:=$(WCC_DIR) $(CC1_FE_DIR) $(CC1_BE_DIR) $(CC1_DIR) $(CPP_DIR) $(UTIL_DIR)
$(foreach D, $(WCC_SRC_DIRS), $(eval $(call DEFINE_WCCOBJ_TARGET,$(D))))
Expand Down
1 change: 1 addition & 0 deletions include/elf.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct proghdr {
#define EM_386 (3) // Intel 80386
#define EM_X86_64 (62) // AMD x86-64 architecture
#define EM_AARCH64 (183) // ARM AARCH64
#define EM_RISCV (243) // RISC-V

#define ET_REL (1) // Relocatable file
#define ET_EXEC (2) // Executable file
Expand Down
2 changes: 1 addition & 1 deletion src/cc/arch/aarch64/aarch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@

#define EMIT_ALIGN(x) emit_align_p2(x)

#ifdef __APPLE__
#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
#define _RODATA() _SECTION("__DATA,__const")
#define _LOCAL(x) ((void)0)
#else
Expand Down
4 changes: 2 additions & 2 deletions src/cc/arch/aarch64/emit_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ char *reg_offset(const char *base, const char *reg, const char *shift) {
}

char *label_at_page(char *label, int flag) {
#ifdef __APPLE__
#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
static const char *s[] = {
"%s@PAGE", "%s@PAGEOFF",
"%s@GOTPAGE", "%s@GOTPAGEOFF",
Expand Down Expand Up @@ -446,7 +446,7 @@ static void move_params_to_assigned(Function *func) {
}
}

#ifdef VAARG_ON_STACK
#if VAARG_ON_STACK
bool vaargs = false;
#else
bool vaargs = func->type->func.vaargs;
Expand Down
2 changes: 1 addition & 1 deletion src/cc/arch/aarch64/ir_aarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void mov_immediate(const char *dst, int64_t value, bool b64, bool is_unsigned) {
}

static bool is_got(const Name *name) {
#ifdef __APPLE__
#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
// TODO: How to detect the label is GOT?
return name->bytes >= 5 && strncmp(name->chars, "__std", 5) == 0; // __stdinp, etc.
#else
Expand Down
2 changes: 1 addition & 1 deletion src/cc/arch/x64/ir_x64.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const RegAllocSettings kArchRegAllocSettings = {
//

static bool is_got(const Name *name) {
#ifdef __APPLE__
#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
// TODO: How to detect the label is GOT?
return name->bytes >= 5 && strncmp(name->chars, "__std", 5) == 0; // __stdinp, etc.
#else
Expand Down
2 changes: 1 addition & 1 deletion src/cc/arch/x64/x64.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@

#define EMIT_ALIGN(x) emit_align_p2(x)

#ifdef __APPLE__
#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
#define _RODATA() _SECTION("__DATA,__const")
#define _LOCAL(x) ((void)0)
#else
Expand Down
4 changes: 2 additions & 2 deletions src/cc/backend/codegen_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ static Expr *simplify_funarg(Expr *arg) {
case EX_MOD:
case EX_LSHIFT:
case EX_RSHIFT:
#if defined(__x86_64__)
#if XCC_TARGET_ARCH == XCC_ARCH_X64
// On x64, MUL, DIV and MOD instruction implicitly uses (breaks) %rdx
// and %rdx is used as 3rd argument.
// Similary, Shift instructions (SHL, SHR) uses %cl which is 4th argument.
Expand Down Expand Up @@ -476,7 +476,7 @@ static VReg *gen_funcall(Expr *expr) {
p->size = type_size(arg->type);
p->is_flo = is_flonum(arg->type);
p->stack_arg = is_stack_param(arg->type);
#if defined(VAARG_ON_STACK)
#if VAARG_ON_STACK
if (functype->func.vaargs && functype->func.params != NULL && i >= functype->func.params->len)
p->stack_arg = true;
#endif
Expand Down
6 changes: 1 addition & 5 deletions src/cc/backend/emit_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include "table.h"
#include "util.h"

#ifdef __APPLE__
#define MANGLE_PREFIX "_"
#endif

static FILE *emit_fp;

char *fmt(const char *fm, ...) {
Expand Down Expand Up @@ -134,7 +130,7 @@ void emit_align_p2(int align) {
}

void emit_bss(const char *label, size_t size, size_t align) {
#ifdef __APPLE__
#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
fprintf(emit_fp, "\t.zerofill __DATA,__bss,%s,%zu,%d\n", label, size,
most_significant_bit(align));
#else
Expand Down
4 changes: 2 additions & 2 deletions src/cc/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static Expr *proc_builtin_type_kind(const Token *ident) {
return new_expr_fixlit(&tySize, ident, type->kind);
}

#if defined(VAARG_ON_STACK)
#if VAARG_ON_STACK
static VReg *gen_builtin_va_start(Expr *expr) {
assert(expr->kind == EX_FUNCALL);
Vector *args = expr->funcall.args;
Expand Down Expand Up @@ -146,7 +146,7 @@ void install_builtins(void) {
add_builtin_expr_ident("__builtin_type_kind", &p_reg_class);

{
#if defined(VAARG_ON_STACK)
#if VAARG_ON_STACK
Type *tyVaList = ptrof(&tyVoidPtr);
#else
Type *tyVaElem = create_struct_type(NULL, alloc_name("__va_elem", NULL, false), 0);
Expand Down
2 changes: 1 addition & 1 deletion src/cc/frontend/fe_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ Expr *extract_bitfield_value(Expr *src, const MemberInfo *minfo) {
tmp = new_expr_bop(EX_BITAND, tmp->type, tmp->token, tmp,
new_expr_fixlit(tmp->type, tmp->token, mask));
} else {
#if defined(__aarch64__) || defined(__WASM) || defined(TARGET_WASM)
#if XCC_TARGET_ARCH == XCC_ARCH_AARCH64 || XCC_TARGET_ARCH == XCC_ARCH_WASM
const unsigned int MINREGSIZE = 4;
int w = MAX(type_size(type), MINREGSIZE) * TARGET_CHAR_BIT;
#else
Expand Down
57 changes: 50 additions & 7 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
#pragma once

#if defined(__aarch64__)
// Architecture
#define XCC_ARCH_X64 1
#define XCC_ARCH_AARCH64 2
#define XCC_ARCH_RISCV64 3
#define XCC_ARCH_WASM 4

#if !defined(XCC_TARGET_ARCH)
# if defined(__WASM)
# define XCC_TARGET_ARCH XCC_ARCH_WASM
# elif defined(__x86_64__)
# define XCC_TARGET_ARCH XCC_ARCH_X64
# elif defined(__aarch64__)
# define XCC_TARGET_ARCH XCC_ARCH_AARCH64
# elif defined(__riscv) && defined(__LP64__)
# define XCC_TARGET_ARCH XCC_ARCH_RISCV64
# else
# error "Target architecture unspecified"
# endif
#endif

// Apple, or not
#define XCC_PLATFORM_POSIX 1
#define XCC_PLATFORM_APPLE 2
#define XCC_PLATFORM_WASI 3

#if !XCC_TARGET_PLATFORM
# if defined(__WASM) || XCC_TARGET_ARCH == XCC_ARCH_WASM
# define XCC_TARGET_PLATFORM XCC_PLATFORM_WASI
# elif defined(__APPLE__)
# define XCC_TARGET_PLATFORM XCC_PLATFORM_APPLE
# else
# define XCC_TARGET_PLATFORM XCC_PLATFORM_POSIX
# endif
#endif

//

#if XCC_TARGET_ARCH == XCC_ARCH_AARCH64
# define AS_USE_CC
# if !defined(__APPLE__)
# if XCC_TARGET_PLATFORM != XCC_PLATFORM_APPLE
# define NO_STD_LIB
# endif

#elif !defined(__linux__)
#elif XCC_TARGET_ARCH != XCC_ARCH_X64 && XCC_TARGET_ARCH != XCC_ARCH_WASM
# define AS_USE_CC
#endif

Expand All @@ -19,9 +56,13 @@
#define ALLOCA(size) malloc(size)
#endif

#if defined(__APPLE__) && defined(__aarch64__)
#if !defined(VAARG_ON_STACK) && XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE && XCC_TARGET_ARCH == XCC_ARCH_AARCH64
// variadic arguments are passed through stack, not registers.
#define VAARG_ON_STACK
#define VAARG_ON_STACK 1
#endif

#if !defined(MANGLE_PREFIX) && XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
#define MANGLE_PREFIX "_"
#endif

#define TARGET_CHAR_BIT 8
Expand All @@ -35,8 +76,10 @@
#endif

// Elf
#if defined(__x86_64__)
#if XCC_TARGET_ARCH == XCC_ARCH_X64
#define MACHINE_TYPE EM_X86_64
#elif defined(__aarch64__)
#elif XCC_TARGET_ARCH == XCC_ARCH_AARCH64
#define MACHINE_TYPE EM_AARCH64
#elif XCC_TARGET_ARCH == XCC_ARCH_RISCV64
#define MACHINE_TYPE EM_RISCV
#endif
5 changes: 0 additions & 5 deletions src/cpp/cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ int main(int argc, char *argv[]) {

// Predefeined macros.
define_macro("__XCC");
#if defined(__linux__)
define_macro("__linux__");
#elif defined(__APPLE__)
define_macro("__APPLE__");
#endif
#if defined(__NO_FLONUM)
define_macro("__NO_FLONUM");
#endif
Expand Down
12 changes: 8 additions & 4 deletions src/xcc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,17 @@ int main(int argc, char *argv[]) {
Vector *cpp_cmd = new_vector();
vec_push(cpp_cmd, cpp_path);
vec_push(cpp_cmd, "-D__LP64__"); // Memory model.
#if defined(__aarch64__)
vec_push(cpp_cmd, "-D__aarch64__");
#elif defined(__x86_64__)
#if XCC_TARGET_ARCH == XCC_ARCH_X64
vec_push(cpp_cmd, "-D__x86_64__");
#elif XCC_TARGET_ARCH == XCC_ARCH_AARCH64
vec_push(cpp_cmd, "-D__aarch64__");
#elif XCC_TARGET_ARCH == XCC_ARCH_RISCV64
vec_push(cpp_cmd, "-D__riscv");
#endif
#if defined(__APPLE__)
#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
vec_push(cpp_cmd, "-D__APPLE__");
#elif XCC_TARGET_PLATFORM == XCC_PLATFORM_POSIX
vec_push(cpp_cmd, "-D__linux__");
#endif

Vector *cc1_cmd = new_vector();
Expand Down

0 comments on commit 5d13783

Please sign in to comment.