translator: add binary operations
This commit is contained in:
@@ -321,7 +321,7 @@ class FunctionCodegen(val state: TranslationState, val variableManager: Variable
|
||||
val left = evaluateExpression(expr.firstChild, scopeDepth) ?: throw UnsupportedOperationException("Wrong binary exception")
|
||||
val right = evaluateExpression(expr.lastChild, scopeDepth) ?: throw UnsupportedOperationException("Wrong binary exception")
|
||||
val operator = expr.operationToken
|
||||
return codeBuilder.addPrimitiveBinaryOperation(operator, left, right)
|
||||
return codeBuilder.addPrimitiveBinaryOperation(operator, expr.operationReference, left, right)
|
||||
}
|
||||
|
||||
private fun evaluateConstantExpression(expr: KtConstantExpression): LLVMConstant {
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.kotlinnative.translator.llvm
|
||||
|
||||
import com.intellij.psi.tree.IElementType
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtSimpleNameExpression
|
||||
import org.kotlinnative.translator.llvm.types.LLVMCharType
|
||||
import org.kotlinnative.translator.llvm.types.LLVMIntType
|
||||
import org.kotlinnative.translator.llvm.types.LLVMStringType
|
||||
@@ -60,7 +61,19 @@ class LLVMBuilder(val arm: Boolean) {
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
fun addPrimitiveBinaryOperation(operation: IElementType, firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMVariable {
|
||||
fun addPrimitiveReferenceOperation(referenceName: KtSimpleNameExpression, firstNativeOp: LLVMSingleValue, secondNativeOp: LLVMSingleValue): LLVMExpression {
|
||||
return when (referenceName.getReferencedName()) {
|
||||
"or" -> firstNativeOp.type!!.operatorOr(firstNativeOp, secondNativeOp)
|
||||
"xor" -> firstNativeOp.type!!.operatorXor(firstNativeOp, secondNativeOp)
|
||||
"and" -> firstNativeOp.type!!.operatorAnd(firstNativeOp, secondNativeOp)
|
||||
"lhr" -> firstNativeOp.type!!.operatorShl(firstNativeOp, secondNativeOp)
|
||||
"shr" -> firstNativeOp.type!!.operatorShr(firstNativeOp, secondNativeOp)
|
||||
"ushr" -> firstNativeOp.type!!.operatorUshr(firstNativeOp, secondNativeOp)
|
||||
else -> throw UnsupportedOperationException("Unknown binary operator")
|
||||
}
|
||||
}
|
||||
|
||||
fun addPrimitiveBinaryOperation(operation: IElementType, referenceName: KtSimpleNameExpression, firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMVariable {
|
||||
val firstNativeOp = receiveNativeValue(firstOp)
|
||||
val secondNativeOp = receiveNativeValue(secondOp)
|
||||
val llvmExpression = when (operation) {
|
||||
@@ -78,7 +91,7 @@ class LLVMBuilder(val arm: Boolean) {
|
||||
storeVariable(result, secondNativeOp)
|
||||
return result
|
||||
}
|
||||
else -> throw UnsupportedOperationException("Unknown binary operator")
|
||||
else -> addPrimitiveReferenceOperation(referenceName, firstNativeOp, secondNativeOp);
|
||||
}
|
||||
val resultOp = getNewVariable(llvmExpression.variableType)
|
||||
addAssignment(resultOp, llvmExpression)
|
||||
|
||||
@@ -6,6 +6,24 @@ import org.kotlinnative.translator.llvm.LLVMSingleValue
|
||||
|
||||
class LLVMIntType() : LLVMType() {
|
||||
|
||||
override fun operatorOr(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
|
||||
LLVMExpression(LLVMIntType(), "or i32 $firstOp, $secondOp")
|
||||
|
||||
override fun operatorAnd(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
|
||||
LLVMExpression(LLVMIntType(), "and i32 $firstOp, $secondOp")
|
||||
|
||||
override fun operatorXor(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
|
||||
LLVMExpression(LLVMIntType(), "xor i32 $firstOp, $secondOp")
|
||||
|
||||
override fun operatorShl(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
|
||||
LLVMExpression(LLVMIntType(), "shl i32 $firstOp, $secondOp")
|
||||
|
||||
override fun operatorShr(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
|
||||
LLVMExpression(LLVMIntType(), "ashr i32 $firstOp, $secondOp")
|
||||
|
||||
override fun operatorUshr(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
|
||||
LLVMExpression(LLVMIntType(), "lshr i32 $firstOp, $secondOp")
|
||||
|
||||
//TODO switch by types: int + double = int
|
||||
override fun operatorMinus(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
|
||||
LLVMExpression(LLVMIntType(), "sub nsw i32 $firstOp, $secondOp")
|
||||
|
||||
@@ -15,6 +15,12 @@ abstract class LLVMType() : Cloneable {
|
||||
open fun operatorGeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorOr(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorAnd(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorXor(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorShl(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorShr(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun operatorUshr(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
|
||||
open fun parseArg(inputArg: String) = inputArg
|
||||
|
||||
fun makeClone() = clone()
|
||||
|
||||
Reference in New Issue
Block a user