From 6a9e8b06a7db36043b1c723d7e290722a97b7d79 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Sun, 28 May 2017 23:08:13 -0700 Subject: [PATCH] configure: add a proper configure script It's not very good, but it supports all of the options by the previous far-worse Makefile configure targets, and also now supports out-of-tree builds. Also include the relevent updates to the .buildbot script and .gitignore. --- .buildbot | 12 ++++++-- .gitignore | 4 ++- Makefile | 62 +++++++++---------------------------- configure | 74 +++++++++++++++++++++++++++++++++++++++++++++ src/gbasm/opcodes.c | 43 +++++++++++++++++++++----- 5 files changed, 137 insertions(+), 58 deletions(-) mode change 100755 => 100644 Makefile create mode 100755 configure diff --git a/.buildbot b/.buildbot index 8d6384b..d2a5793 100755 --- a/.buildbot +++ b/.buildbot @@ -4,19 +4,25 @@ set -e do_build() { - make config-default + mkdir build || true + cd build + ../configure make } do_test() { - make config-debug + mkdir build || true + cd build + ../configure --debug make check } do_coverage() { - make config-coverage + mkdir build || true + cd build + ../configure --coverage make coverage-report mkdir -p ./buildbot-upload/coverage/ cp -r ./build/coverage/html/* ./buildbot-upload/coverage/ diff --git a/.gitignore b/.gitignore index 72f6fbf..f4c86a9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,6 @@ build/* *.gcda *[#]*[#] doxygen/* -buildbot-upload/* \ No newline at end of file +buildbot-upload/* +*/config.mak +*\~ \ No newline at end of file diff --git a/Makefile b/Makefile old mode 100755 new mode 100644 index 748b086..6af8e51 --- a/Makefile +++ b/Makefile @@ -1,82 +1,50 @@ -build=OUT -COV_DIR=$(OUT)/coverage/ -SRC_DIR=src -APP_DIR=src/apps/ -OUT=build +CONFIG = config.mak +include $(CONFIG) + +CONFIGURE = $(SRC_DIR)/ + +COV_DIR=$(OUT)/coverage/ +APP_DIR=$(SRC_DIR)/apps/ CC=gcc CCFLAGS = -Wall -Werror CCFLAGS += -std=c99 -D_POSIX_C_SOURCE=200809L CCFLAGS += -I$(SRC_DIR) -.PHONY: release debug - -CONFIG = $(OUT)/.config.mk - --include $(CONFIG) - -ifeq ($(DEBUG), 1) -CCFLAGS += -DLOG_LEVEL=5 -DDEBUG -CCFLAGS += -O0 -ggdb -endif - -ifeq ($(COVERAGE), 1) -LDFLAGS += -lgcov --coverage -CCFLAGS += -O0 --coverage -CCFLAGS += -fprofile-arcs -ftest-coverage -endif - -ifeq ($(RELEASE), 1) -CCFLAGS += -O3 -endif - CCFLAGS += -I/usr/include/glib-2.0/ -I/usr/lib/x86_64-linux-gnu/glib-2.0/include LDFLAGS += -lglib-2.0 +CCFLAGS += $(CCFLAGS_EXTRA) +LDFLAGS += $(LDFLAGS_EXTRA) + C_SOURCES = $(shell find $(SRC_DIR) -name "*.c") ALL_OBJS = $(patsubst $(SRC_DIR)/%.c, $(OUT)/%.o, $(C_SOURCES)) APP_OBJS = $(filter $(OUT)/apps/%, $(ALL_OBJS)) TEST_OBJS = $(filter $(OUT)%test.o, $(ALL_OBJS)) OBJS = $(filter-out $(TEST_OBJS) $(APP_OBJS), $(ALL_OBJS)) APPS = $(patsubst %.o, %, $(APP_OBJS)) + .PHONY: all all: gbdb gbasm tests -.PHONY: config-debug config-release config-default config-coverage - -$(CONFIG): - @mkdir -p $(OUT) - -config-debug: $(CONFIG) - @echo "DEBUG=1" > $(CONFIG) - -config-release: $(CONFIG) - @echo "RELEASE=1" > $(CONFIG) - -config-coverage: $(CONFIG) - @echo "COVERAGE=1" > $(CONFIG) - -config-default: $(CONFIG) - @echo "" > $(CONFIG) - .PHONY: gbdb gbasm - gbdb: $(OUT)/apps/gbdb gbasm: $(OUT)/apps/gbasm -.PHONY: gbasm-test sample-test tests +.PHONY: gbasm-test tests TESTS = $(patsubst %.o, %, $(filter %/test.o, $(ALL_OBJS))) tests: $(TESTS) $(ALL_APPS) $(ALL_OBJS): $(CONFIG) -$(OUT)/%.o: $(SRC_DIR)/%.c +$(OUT)/%.o: $(SRC_DIR)/%.c $(CONFIG) @echo " CC $@" @$(shell mkdir -p $(@D)) @$(CC) -c $(CCFLAGS) -o $@ $< + .SECONDEXPANSION: $(APPS) $(TESTS): $(OBJS) $$@.o @echo " LD $@" @@ -89,7 +57,7 @@ $(APPS) $(TESTS): $(OBJS) $$@.o define run_tests = @for i in $(TESTS); do \ echo " TEST $$i"; \ - $$i; \ + $$i | sed 's/^/ /'; \ if [ $$? -ne 0 ]; then \ exit 1; \ fi; \ diff --git a/configure b/configure new file mode 100755 index 0000000..c5015c3 --- /dev/null +++ b/configure @@ -0,0 +1,74 @@ +#!/bin/bash + +CONFIG_MAK=config.mak +MAKEFILE=Makefile + +debug="no" +coverage="no" +release="no" +output_dir=$(pwd) +proj_dir=$(dirname "$BASH_SOURCE") + +for arg in $@; do + case $arg in + --debug) + debug="yes" + ;; + --release) + release="yes" + ;; + --coverage) + coverage="yes" + ;; + *) + echo "unknown option $arg" + exit 1 + esac +done + +add_define() +{ + var=$1 + shift + + if [[ $# -gt 0 ]]; then + echo "$var=$@" >> $CONFIG_MAK + else + echo "# $var is not defined" >> CONFIG_MAK + fi + +} + +quiet() +{ + $@ 2> /dev/null 1> /dev/null +} + +quiet rm $CONFIG_MAK + +if [[ ! -e $MAKEFILE ]]; then + quiet ln -s $proj_dir/$MAKEFILE $MAKEFILE +fi + +ccflags_extra="" +ldflags_extra="" + +if [[ "$debug" == "yes" ]]; then + ccflags_extra="$ccflags_extra -DLOG_LEVEL=5 -DDEBUG -O0 -ggdb" +fi + +if [[ "$coverage" == "yes" ]]; then + ccflags_extra="$ccflags_extra --coverage -fprofile-arcs -ftest-coverage" + ldflags_extra="$ldflags_extra -lgcov --coverage" +fi + +if [[ "$release" == "yes" ]]; then + ccflags_extra="$ccflags_extra -O3" +fi + +add_define CCFLAGS_EXTRA "$ccflags_extra" +add_define LDFLAGS_EXTRA "$ldflags_extra" +add_define OUT "$output_dir" +add_define SRC_DIR "$proj_dir/src" + +exit 0 diff --git a/src/gbasm/opcodes.c b/src/gbasm/opcodes.c index 44537ab..68dba8b 100644 --- a/src/gbasm/opcodes.c +++ b/src/gbasm/opcodes.c @@ -15,6 +15,11 @@ static bool gbasm_argtype_in_set(uint32_t argtype_set, uint32_t argtype) return !!(argtype & argtype_set); } +static bool imm_is_8_bit(uint16_t val) +{ + return (val <= 127) && (val >= -128); +} + static int check_no_args(const struct gbasm_parsed_inst *inst, const struct gbasm_op_info *op_info) { @@ -89,6 +94,11 @@ static int ld_check(const struct gbasm_parsed_inst *inst, gbasm_arg_wrong_type_error(inst, op_info); } + if (inst->operands[0].type == GBASM_OPERAND_REG_8 && + imm_is_8_bit(inst->operands[1].imm.value)) { + + } + return 0; } @@ -163,16 +173,33 @@ size_t dec_emit(struct emitter *emitter, } size_t ld_emit(struct emitter *emitter, - const struct gbasm_parsed_inst *inst) + const struct gbasm_parsed_inst *inst) { - uint64_t opcode = 0x40; + uint8_t opcode; + uint8_t imm8; - opcode += inst->operands[0].r8.type * 8; - opcode += inst->operands[1].r8.type; + if (inst->operands[0].type == GBASM_OPERAND_REG_8) { + switch (inst->operands[1].type) { + case GBASM_OPERAND_REG_8: + opcode = 0x40; + opcode += inst->operands[0].r8.type * 8; + opcode += inst->operands[1].r8.type; + emit(emitter, &opcode, 1); + return 1; + case GBASM_OPERAND_IMM: + imm8 = inst->operands[1].imm.value; + opcode = 0x06; + opcode += inst->operands[0].r8.type * 8; + emit(emitter, &opcode, 1); + emit(emitter, &imm8, 1); + return 2; + default: + break; + } - emit(emitter, &opcode, 1); + } - return 1; + return -1; } struct gbasm_op_info gbasm_op_infos[] = { @@ -293,7 +320,9 @@ struct gbasm_op_info gbasm_op_infos[] = { { .opcode = "ld", /* support all of the other operands */ - .operand_types = { GBASM_OPERAND_REG_8, GBASM_OPERAND_REG_8 }, + .operand_types = { GBASM_OPERAND_REG_8, + GBASM_OPERAND_REG_8 | + GBASM_OPERAND_IMM }, .check = ld_check, .length = length_one_byte, .emit = ld_emit,