translator: add mod operator for all types

This commit is contained in:
Alexey Stepanov
2016-08-18 09:40:01 +03:00
parent da104370cd
commit 40463477cb
8 changed files with 27 additions and 1 deletions
@@ -716,6 +716,7 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
"or" -> firstNativeOp.type!!.operatorOr(firstNativeOp, secondNativeOp)
"xor" -> firstNativeOp.type!!.operatorXor(firstNativeOp, secondNativeOp)
"and" -> firstNativeOp.type!!.operatorAnd(firstNativeOp, secondNativeOp)
"%" -> firstNativeOp.type!!.operatorMod(firstNativeOp, secondNativeOp)
"shl" -> {
var secondNativeOpWithRequiredType = secondNativeOp
if (firstNativeOp.type != secondNativeOp.type) {
@@ -765,6 +766,13 @@ abstract class BlockCodegen(val state: TranslationState, val variableManager: Va
codeBuilder.storeVariable(firstOp, resultOp)
return LLVMExpression(resultOp.type, "load ${firstOp.getType()} $firstOp, align ${firstOp.type!!.align}")
}
"%=" -> {
val llvmExpression = firstNativeOp.type!!.operatorMod(firstNativeOp, secondNativeOp)
val resultOp = codeBuilder.getNewVariable(llvmExpression.variableType)
codeBuilder.addAssignment(resultOp, llvmExpression)
codeBuilder.storeVariable(firstOp, resultOp)
return LLVMExpression(resultOp.type, "load ${firstOp.getType()} $firstOp, align ${firstOp.type!!.align}")
}
else -> throw UnsupportedOperationException("Unknown binary operator")
}
}
@@ -22,6 +22,9 @@ class LLVMByteType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp ne i8 $firstOp, $secondOp")
override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMByteType(), "srem i8 $firstOp, $secondOp")
override fun equals(other: Any?): Boolean {
return other is LLVMByteType
}
@@ -42,6 +42,9 @@ class LLVMDoubleType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "fcmp one double $firstOp, $secondOp")
override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMDoubleType(), "frem double $firstOp, $secondOp")
override fun equals(other: Any?): Boolean {
return other is LLVMDoubleType
}
@@ -42,6 +42,9 @@ class LLVMFloatType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "fcmp one float $firstOp, $secondOp")
override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMFloatType(), "frem float $firstOp, $secondOp")
override fun equals(other: Any?): Boolean {
return other is LLVMFloatType
}
@@ -61,6 +61,9 @@ class LLVMIntType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp ne i32 $firstOp, $secondOp")
override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMIntType(), "srem i32 $firstOp, $secondOp")
override fun equals(other: Any?): Boolean {
return other is LLVMIntType
}
@@ -43,7 +43,6 @@ class LLVMLongType() : LLVMType() {
override fun operatorGt(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp sgt i64 $firstOp, $secondOp")
override fun operatorLeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp sle i64 $firstOp, $secondOp")
@@ -56,6 +55,9 @@ class LLVMLongType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp ne i64 $firstOp, $secondOp")
override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMLongType(), "srem i64 $firstOp, $secondOp")
override fun convertFrom(source: LLVMSingleValue): LLVMExpression = when (source.type!!) {
is LLVMBooleanType,
is LLVMByteType,
@@ -22,6 +22,9 @@ class LLVMShortType() : LLVMType() {
override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMBooleanType(), "icmp ne i16 $firstOp, $secondOp")
override fun operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression =
LLVMExpression(LLVMShortType(), "srem i16 $firstOp, $secondOp")
override fun equals(other: Any?): Boolean {
return other is LLVMShortType
}
@@ -21,6 +21,7 @@ abstract class LLVMType() : Cloneable {
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 operatorMod(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
open fun operatorInc(firstOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
open fun operatorDec(firstOp: LLVMSingleValue): LLVMExpression = throw UnimplementedException()
open fun parseArg(inputArg: String) = inputArg