From d54ef1d3c512eebb27e62ea02539e3769084e4a4 Mon Sep 17 00:00:00 2001 From: e5l Date: Wed, 13 Jul 2016 14:14:45 +0300 Subject: [PATCH] translator: plain function call fix; minor fixes --- car_llvmkot/Makefile.bak | 82 ------------------- car_llvmkot/src/kotlin_main.ll | 37 ++++++++- car_llvmkot/src/main.c | 8 ++ .../translator/FunctionCodegen.kt | 8 +- .../translator/llvm/generators.kt | 2 +- 5 files changed, 51 insertions(+), 86 deletions(-) delete mode 100644 car_llvmkot/Makefile.bak diff --git a/car_llvmkot/Makefile.bak b/car_llvmkot/Makefile.bak deleted file mode 100644 index 5383554d6d7..00000000000 --- a/car_llvmkot/Makefile.bak +++ /dev/null @@ -1,82 +0,0 @@ -BIN_DIR=$(PWD)/bin -SRC_DIR=$(PWD)/src -LIB_DIR=$(SRC_DIR)/lib -OS_ARCH=linux-arm - -LIB_CMSIS_DIR=$(LIB_DIR)/cmsis -LIB_STDPERIPH_DIR=$(LIB_DIR)/stdperiph -LIB_STM32F4D_DIR=$(LIB_DIR)/stm32f4d - -LIB_CMSIS_OBJ=$(BIN_DIR)/libcmsis.o -LIB_STDPERIPH_OBJ=$(BIN_DIR)/libstdperiph.o -LIB_STM32F4D_OBJ=$(BIN_DIR)/libstm32f4d.o -CAR_FMW_OBJ=$(BIN_DIR)/car_fmw.o -CAR_FMW_ELF=$(BIN_DIR)/car_fmw.elf -CAR_FMW_BIN=$(BIN_DIR)/car_fmw.bin -ST_DIR=$(SRC_DIR)/../../devloader/data/$(OS_ARCH) - -LIB_KOTLIN=$(PWD)/bin/kotlib.o - -CC=arm-none-eabi-gcc -AS=arm-none-eabi-as -LD=arm-none-eabi-ld -OBJ_COPY=arm-none-eabi-objcopy -GDB=arm-none-eabi-gdb - -INCLUDES=-I$(LIB_CMSIS_DIR) \ - -I$(LIB_STDPERIPH_DIR) \ - -I$(LIB_STM32F4D_DIR) \ - -I$(SRC_DIR) -DEFINES=-DUSE_STM32_DISCOVERY=1 -DUSE_STDPERIPH_DRIVER=1 -DSTM32F4XX=1 -DHSE_VALUE=8000000 -CFLAGS=-g -nostdlib -ffreestanding -O0 \ - -mcpu=cortex-m3 -mfloat-abi=soft -mthumb \ - $(DEFINES) -ASMFLAGS=-g -mthumb - -LD_ALL_DEPS=$(LD) -r $(filter %.o,$^) -o $@ -CC_ALL_DEPS=$(CC) $(CFLAGS) -c $(INCLUDES) $(filter %.c,$^) -o $@ -AS_ALL_DEPS=$(AS) $(ASMFLAGS) $(filter %.s,$^) -o $@ - -dirhs=$(wildcard $(1)/*.h) -dircs=$(wildcard $(1)/*.c) -dircs_to_prefxd_objs=\ - $(patsubst $(1)/%.c,$(BIN_DIR)/$(2)%.o,$(call dircs,$1)) - -$(CAR_FMW_BIN): $(CAR_FMW_ELF) $(BIN_DIR) - $(OBJ_COPY) -O binary $< $@ - -$(CAR_FMW_ELF): $(CAR_FMW_OBJ) $(LIB_STM32F4D_OBJ) \ - $(LIB_CMSIS_OBJ) $(LIB_STDPERIPH_OBJ) - make -C kotlib - $(CC) $(CFLAGS) $^ $(LIB_KOTLIN) -T $(SRC_DIR)/stm32_flash.ld -o $@ - -CAR_OBJ_PREFIX=car_ -$(CAR_FMW_OBJ): \ - $(call dircs_to_prefxd_objs,$(SRC_DIR),$(CAR_OBJ_PREFIX)) - $(LD_ALL_DEPS) - -$(BIN_DIR)/$(CAR_OBJ_PREFIX)%.o: $(SRC_DIR)/%.c \ - $(call dirhs,$(SRC_DIR)) - $(CC_ALL_DEPS) - -$(BIN_DIR): - mkdir -p $(BIN_DIR) - - -flash: $(CAR_FMW_BIN) - $(ST_DIR)/st-flash write $(CAR_FMW_BIN) 0x8000000 - -debug: - -killall st-util - (setsid $(ST_DIR)/st-util 2>/dev/null 1>&2)& - $(GDB) $(CAR_FMW_ELF) -x $(ST_DIR)/../gdb.cmds - -clean: - rm -rf bin/* - -tags: - ctags -R * - -include $(LIB_DIR)/**/Makefile - -.PHONY: clean debug flash tags diff --git a/car_llvmkot/src/kotlin_main.ll b/car_llvmkot/src/kotlin_main.ll index ff02c40a4b0..1e80a7e52de 100644 --- a/car_llvmkot/src/kotlin_main.ll +++ b/car_llvmkot/src/kotlin_main.ll @@ -1,6 +1,41 @@ declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1) attributes #0 = { nounwind "stack-protector-buffer-size"="8" "target-cpu"="cortex-m3" "target-features"="+hwdiv,+strict-align" } -define void @kotlin_main() #0 +declare void @wait(i32 %loops) #0 +define void @program_forward() #0 { +call void @engine_forward() +call void @wait(i32 10) ret void } +declare void @leds_init() #0 +declare void @engine_init() #0 +define void @program_backward() #0 +{ +call void @engine_backward() +call void @wait(i32 10) +ret void +} +declare void @engine_backward() #0 +define void @program_rotation_left() #0 +{ +call void @engine_turn_left() +call void @wait(i32 10) +ret void +} +define void @kotlin_main() #0 +{ +call void @leds_init() +call void @engine_init() +ret void +} +declare void @engine_stop() #0 +declare void @engine_forward() #0 +define void @program_rotation_right() #0 +{ +call void @engine_turn_right() +call void @wait(i32 10) +ret void +} +declare void @engine_turn_left() #0 +declare void @engine_turn_right() #0 + diff --git a/car_llvmkot/src/main.c b/car_llvmkot/src/main.c index c157546a563..33a96977b90 100644 --- a/car_llvmkot/src/main.c +++ b/car_llvmkot/src/main.c @@ -1,5 +1,13 @@ +#include +#include "car_leds.h" +#include "car_engine.h" +#include "car_user_btn.h" +#include "wait.h" + +extern void kotlin_main(); int main(void) { + kotlin_main(); } diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt b/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt index 257ebd667bf..17508ed651e 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/FunctionCodegen.kt @@ -37,7 +37,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction codeBuilder.addStartExpression() generateLoadArguments() - evaluateCodeBlock(function.bodyExpression, startLabel = null, finishLabel = null, scopeDepth = 0) + evaluateCodeBlock(function.bodyExpression) if (returnType is LLVMVoidType) { @@ -72,7 +72,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction } } - private fun evaluateCodeBlock(expr: PsiElement?, startLabel: LLVMLabel?, finishLabel: LLVMLabel?, scopeDepth: Int) { + private fun evaluateCodeBlock(expr: PsiElement?, startLabel: LLVMLabel? = null, finishLabel: LLVMLabel? = null, scopeDepth: Int = 0) { codeBuilder.markWithLabel(startLabel) expressionWalker(expr, scopeDepth) codeBuilder.addUnconditionJump(finishLabel ?: return) @@ -83,6 +83,10 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction is KtBlockExpression -> expressionWalker(expr.firstChild, scopeDepth + 1) is KtProperty -> evaluateLeafPsiElement(expr.firstChild as LeafPsiElement, scopeDepth) is KtBinaryExpression -> evaluateBinaryExpression(expr, scopeDepth) + is KtCallExpression -> { + val expression = evaluateCallExpression(expr) as LLVMCall + codeBuilder.addLLVMCode(expression.toString()) + } is PsiElement -> evaluateExpression(expr.firstChild, scopeDepth + 1) null -> { variableManager.pullUpwardsLevel(scopeDepth) diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt index 236e6cf2928..9083ecd47a5 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/generators.kt @@ -7,7 +7,7 @@ fun LLVMFunctionDescriptor(name: String, argTypes: List?, returnTy "${if (declare) "declare" else "define"} $returnType @$name(${ argTypes?.mapIndexed { i: Int, s: LLVMVariable -> "${s.getType()} %${s.label}" - }?.joinToString() }) ${ if (arm) "#0 " else ""}" + }?.joinToString() }) ${ if (arm) "#0" else ""}" fun LLVMMapStandardType(type: String): LLVMType = when (type) { "Int" -> LLVMIntType()