Skip to content

Commit

Permalink
Refactor architecture specific configurations (#52)
Browse files Browse the repository at this point in the history
Close #47
  • Loading branch information
eecheng87 committed Feb 7, 2021
1 parent e837e34 commit 4de273b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
26 changes: 15 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ OBJS := $(SRCS:%.c=$(OUT)/%.o)
deps := $(OBJS:%.o=%.o.d)
TESTS := $(wildcard tests/*.c)
TESTBINS := $(TESTS:%.c=$(OUT)/%.elf)
TARGET_EXEC := `cat $(OUT)/target`

all: config bootstrap

config:
ifeq (riscv,$(ARCH))
@$(VECHO) "$(RISCV_EXEC)" > $(OUT)/target
@$(VECHO) "#define TARGET_RISCV 1" > $@
@ln -s $(PWD)/$(SRCDIR)/riscv-codegen.c $(SRCDIR)/codegen.c
# set ARM by default
ifeq ($(strip $(ARCH)),riscv)
ARCH = riscv
else
@$(VECHO) "$(ARM_EXEC)" > $(OUT)/target
@$(VECHO) "#define TARGET_ARM 1" > $@
@ln -s $(PWD)/$(SRCDIR)/arm-codegen.c $(SRCDIR)/codegen.c
ARCH = arm
endif

ifneq ("$(wildcard $(PWD)/config)","")
TARGET_EXEC := $($(shell head -1 config | sed 's/.*: \([^ ]*\).*/\1/')_EXEC)
endif
@$(VECHO) "Target machine code switch to %s\n" "$$(cat out/target | sed 's/.*qemu-\([^ ]*\).*/\1/')"
export TARGET_EXEC

config:
$(Q)ln -s $(PWD)/$(SRCDIR)/$(ARCH)-codegen.c $(SRCDIR)/codegen.c
$(call $(ARCH)-specific-defs) > $@
$(VECHO) "Target machine code switch to %s\n" $(ARCH)

$(OUT)/tests/%.elf: tests/%.c $(OUT)/$(STAGE0)
$(VECHO) " SHECC\t$@\n"
Expand Down Expand Up @@ -85,4 +89,4 @@ clean:
-$(RM) $(OUT)/shecc*.log
-$(RM) $(OUT)/inliner $(OUT)/libc.inc $(OUT)/target config $(SRCDIR)/codegen.c

-include $(deps)
-include $(deps)
8 changes: 8 additions & 0 deletions mk/arm.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ ARM_EXEC = echo WARN: unable to run
endif

export ARM_EXEC

arm-specific-defs = \
$(Q)$(PRINTF) \
" /* target: ARM */\n\
\#define ARCH_PREDEFINED \"__arm__\" /* defined by GNU C and RealView */\n\
\#define ELF_MACHINE 0x28 /* up to ARMv7/Aarch32 */\n\
\#define ELF_FLAGS 0x5000200\n\
"
10 changes: 9 additions & 1 deletion mk/riscv.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ $(warning "no qemu-riscv32 found. Please check package installation")
RISCV_EXEC = echo WARN: unable to run
endif

export RISCV_EXEC
export RISCV_EXEC

riscv-specific-defs = \
$(Q)$(PRINTF) \
" /* target: RISCV */\n\
\#define ARCH_PREDEFINED \"__riscv\" /* Older versions of the GCC toolchain defined __riscv__ */\n\
\#define ELF_MACHINE 0xf3\n\
\#define ELF_FLAGS 0\n\
"
11 changes: 2 additions & 9 deletions src/cfront.c
Original file line number Diff line number Diff line change
Expand Up @@ -2168,15 +2168,8 @@ void parse_internal()
add_block(NULL, NULL); /* global block */
elf_add_symbol("", 0, 0); /* undef symbol */

/* architecture defines */
/* FIXME: use #ifdef ... #else ... #endif */
#ifdef TARGET_ARM
add_alias("__arm__", "1"); /* defined by GNU C and RealView */
#endif
#ifdef TARGET_RISCV
/* Older versions of the GCC toolchain defined __riscv__ */
add_alias("__riscv", "1");
#endif
/* architecture defines */
add_alias(ARCH_PREDEFINED, "1");

/* binary entry point: read params, call main, exit */
ii = add_instr(OP_label);
Expand Down
18 changes: 3 additions & 15 deletions src/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,16 @@ void elf_generate_header()
elf_write_header_int(0); /* EI_PAD: unused */
elf_write_header_byte(2); /* ET_EXEC */
elf_write_header_byte(0);
/* FIXME: use #ifdef ... #else ... #endif */
#ifdef TARGET_ARM
elf_write_header_byte(0x28); /* ARM (up to ARMv7/Aarch32) */
#endif
#ifdef TARGET_RISCV
elf_write_header_byte(0xf3); /* RISC-V */
#endif
elf_write_header_byte(ELF_MACHINE);
elf_write_header_byte(0);
elf_write_header_int(1); /* ELF version */
elf_write_header_int(ELF_START + elf_header_len); /* entry point */
elf_write_header_int(0x34); /* program header offset */
elf_write_header_int(elf_header_len + elf_code_idx + elf_data_idx + 39 +
elf_symtab_index +
elf_strtab_index); /* section header offset */
/* flags */
/* FIXME: use #ifdef ... #else ... #endif */
#ifdef TARGET_ARM
elf_write_header_int(0x5000200); /* ARM */
#endif
#ifdef TARGET_RISCV
elf_write_header_int(0);
#endif
/* flags */
elf_write_header_int(ELF_FLAGS);
elf_write_header_byte(0x34); /* header size */
elf_write_header_byte(0);
elf_write_header_byte(0x20); /* program header size */
Expand Down
3 changes: 1 addition & 2 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ function try() {

local tmp_in="$(mktemp --suffix .c)"
local tmp_exe="$(mktemp)"
local target_exec=$(<"$PWD/out/target")
echo "$input" > "$tmp_in"
"$SHECC" -o "$tmp_exe" "$tmp_in"
chmod +x $tmp_exe
$target_exec "$tmp_exe"
$TARGET_EXEC "$tmp_exe"
local actual="$?"

if [ "$actual" = "$expected" ]; then
Expand Down

0 comments on commit 4de273b

Please sign in to comment.