translator: add double and fix variable bugs

This commit is contained in:
Alexey Stepanov
2016-07-11 15:25:13 +03:00
parent 968fc9ec03
commit 55c3401f56
5 changed files with 42 additions and 4 deletions
@@ -30,9 +30,9 @@ class LLVMBuilder {
fun addPrimitiveBinaryOperation(operation: IElementType, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMVariable { fun addPrimitiveBinaryOperation(operation: IElementType, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMVariable {
val newVar = getNewVariable(::LLVMIntType) val newVar = getNewVariable(::LLVMIntType)
val llvmOperator = when (operation) { val llvmOperator = when (operation) {
KtTokens.PLUS -> "add nsw i32" KtTokens.PLUS -> firstOp.type?.operatorPlus(newVar, firstOp, secondOp)?.generateExpression(this)
KtTokens.MINUS -> "sub nsw i32" KtTokens.MINUS -> firstOp.type?.operatorMinus(newVar, firstOp, secondOp)?.generateExpression(this)
KtTokens.MUL -> "mul nsw i32" KtTokens.MUL -> firstOp.type?.operatorTimes(newVar, firstOp, secondOp)?.generateExpression(this)
else -> throw UnsupportedOperationException("Unkbown binary operator") else -> throw UnsupportedOperationException("Unkbown binary operator")
} }
@@ -6,7 +6,7 @@ import kotlin.reflect.KFunction0
class LLVMExpression(val variableType: KFunction0<LLVMType>, val llvmCode: String) : LLVMNode() { class LLVMExpression(val variableType: KFunction0<LLVMType>, val llvmCode: String) : LLVMNode() {
fun generateExpression(builder: LLVMBuilder): LLVMVariable { fun generateExpression(builder: LLVMBuilder): LLVMVariable {
val newVar = builder.getNewVariable(variableType); val newVar = builder.getNewVariable(variableType);
builder.addLLVMCode("%$newVar = $llvmCode"); builder.addLLVMCode("$newVar = $llvmCode");
return newVar return newVar
} }
} }
@@ -0,0 +1,24 @@
package org.kotlinnative.translator.llvm.types
import org.kotlinnative.translator.llvm.LLVMExpression
import org.kotlinnative.translator.llvm.LLVMVariable
class LLVMDoubleType() : LLVMType() {
override fun operatorMinus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
//TODO switch by types: int + double = int
return LLVMExpression(::LLVMIntType, "fsub double i32 $firstOp, $secondOp")
}
override fun operatorTimes(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
//TODO switch by types: int + double = int
return LLVMExpression(::LLVMIntType, "fmul double i32 $firstOp, $secondOp")
}
override fun operatorPlus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
//TODO switch by types: int + double = int
return LLVMExpression(::LLVMIntType, "fadd double $firstOp, $secondOp")
}
}
@@ -5,6 +5,17 @@ import org.kotlinnative.translator.llvm.LLVMVariable
class LLVMIntType() : LLVMType() { class LLVMIntType() : LLVMType() {
override fun operatorMinus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
//TODO switch by types: int + double = int
return LLVMExpression(::LLVMIntType, "sub nsw i32 $firstOp, $secondOp")
}
override fun operatorTimes(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
//TODO switch by types: int + double = int
return LLVMExpression(::LLVMIntType, "mul nsw i32 $firstOp, $secondOp")
}
override fun operatorPlus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression { override fun operatorPlus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
//TODO switch by types: int + double = int //TODO switch by types: int + double = int
return LLVMExpression(::LLVMIntType, "add nsw i32 $firstOp, $secondOp") return LLVMExpression(::LLVMIntType, "add nsw i32 $firstOp, $secondOp")
@@ -4,5 +4,8 @@ import org.kotlinnative.translator.llvm.LLVMExpression
import org.kotlinnative.translator.llvm.LLVMVariable import org.kotlinnative.translator.llvm.LLVMVariable
abstract class LLVMType() { abstract class LLVMType() {
abstract fun operatorPlus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression; abstract fun operatorPlus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression;
abstract fun operatorTimes(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression;
abstract fun operatorMinus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression;
} }