translator: reorginize code, fix double expression, add assigment llvm
This commit is contained in:
@@ -29,17 +29,21 @@ 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 llvmExpression = when (operation) {
|
||||||
KtTokens.PLUS -> firstOp.type?.operatorPlus(newVar, firstOp, secondOp)?.generateExpression(this)
|
KtTokens.PLUS -> firstOp.type!!.operatorPlus(newVar, firstOp, secondOp)
|
||||||
KtTokens.MINUS -> firstOp.type?.operatorMinus(newVar, firstOp, secondOp)?.generateExpression(this)
|
KtTokens.MINUS -> firstOp.type!!.operatorMinus(newVar, firstOp, secondOp)
|
||||||
KtTokens.MUL -> firstOp.type?.operatorTimes(newVar, firstOp, secondOp)?.generateExpression(this)
|
KtTokens.MUL -> firstOp.type!!.operatorTimes(newVar, firstOp, secondOp)
|
||||||
else -> throw UnsupportedOperationException("Unkbown binary operator")
|
else -> throw UnsupportedOperationException("Unkbown binary operator")
|
||||||
}
|
}
|
||||||
|
|
||||||
llvmCode.appendln("$newVar = $llvmOperator $firstOp, $secondOp")
|
addAssignment(newVar, llvmExpression)
|
||||||
|
|
||||||
return newVar
|
return newVar
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addAssignment(llvmVariable: LLVMNode, assignExpression: LLVMNode) {
|
||||||
|
llvmCode.appendln("$llvmVariable = $assignExpression")
|
||||||
|
}
|
||||||
|
|
||||||
fun clean() {
|
fun clean() {
|
||||||
llvmCode = StringBuilder()
|
llvmCode = StringBuilder()
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package org.kotlinnative.translator.llvm
|
package org.kotlinnative.translator.llvm
|
||||||
|
|
||||||
import org.kotlinnative.translator.llvm.types.LLVMType
|
import org.kotlinnative.translator.llvm.types.LLVMType
|
||||||
import kotlin.reflect.KFunction0
|
|
||||||
|
|
||||||
class LLVMExpression(val variableType: KFunction0<LLVMType>, val llvmCode: String) : LLVMNode() {
|
class LLVMExpression(val variableType: LLVMType, val llvmCode: String) : LLVMNode() {
|
||||||
fun generateExpression(builder: LLVMBuilder): LLVMVariable {
|
|
||||||
val newVar = builder.getNewVariable(variableType);
|
override fun toString(): String {
|
||||||
builder.addLLVMCode("$newVar = $llvmCode");
|
return llvmCode
|
||||||
return newVar
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,17 +8,17 @@ class LLVMDoubleType() : LLVMType() {
|
|||||||
|
|
||||||
override fun operatorMinus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
override fun operatorMinus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
||||||
//TODO switch by types: int + double = int
|
//TODO switch by types: int + double = int
|
||||||
return LLVMExpression(::LLVMIntType, "fsub double i32 $firstOp, $secondOp")
|
return LLVMExpression(LLVMDoubleType(), "fsub double i32 $firstOp, $secondOp")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun operatorTimes(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
override fun operatorTimes(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
||||||
//TODO switch by types: int + double = int
|
//TODO switch by types: int + double = int
|
||||||
return LLVMExpression(::LLVMIntType, "fmul double i32 $firstOp, $secondOp")
|
return LLVMExpression(LLVMDoubleType(), "fmul double 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, "fadd double $firstOp, $secondOp")
|
return LLVMExpression(LLVMDoubleType(), "fadd double $firstOp, $secondOp")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,17 +8,17 @@ class LLVMIntType() : LLVMType() {
|
|||||||
|
|
||||||
override fun operatorMinus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
override fun operatorMinus(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
||||||
//TODO switch by types: int + double = int
|
//TODO switch by types: int + double = int
|
||||||
return LLVMExpression(::LLVMIntType, "sub nsw i32 $firstOp, $secondOp")
|
return LLVMExpression(LLVMIntType(), "sub nsw i32 $firstOp, $secondOp")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun operatorTimes(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
override fun operatorTimes(result: LLVMVariable, firstOp: LLVMVariable, secondOp: LLVMVariable): LLVMExpression {
|
||||||
//TODO switch by types: int + double = int
|
//TODO switch by types: int + double = int
|
||||||
return LLVMExpression(::LLVMIntType, "mul nsw i32 $firstOp, $secondOp")
|
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")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user