translator: track pointer level

This commit is contained in:
e5l
2016-07-14 16:39:16 +03:00
parent d02e6ebd32
commit 0ec0154c81
9 changed files with 35 additions and 38 deletions
@@ -69,7 +69,7 @@ class ClassCodegen(val state: TranslationState, val clazz: KtClass, val codeBuil
refType.addParam("sret") refType.addParam("sret")
refType.isReturn = true refType.isReturn = true
val thisField = LLVMVariable("instance", refType, clazz.name, pointer = true) val thisField = LLVMVariable("instance", refType, clazz.name, pointer = 1)
argFields.add(thisField) argFields.add(thisField)
argFields.addAll(fields) argFields.addAll(fields)
@@ -86,7 +86,7 @@ class ClassCodegen(val state: TranslationState, val clazz: KtClass, val codeBuil
private fun generateLoadArguments(thisField: LLVMVariable) { private fun generateLoadArguments(thisField: LLVMVariable) {
val thisVariable = LLVMVariable(thisField.label, thisField.type, thisField.label, LLVMLocalScope(), pointer = true) val thisVariable = LLVMVariable(thisField.label, thisField.type, thisField.label, LLVMLocalScope(), pointer = 1)
codeBuilder.loadArgument(thisVariable, false) codeBuilder.loadArgument(thisVariable, false)
fields.forEach { fields.forEach {
@@ -98,16 +98,16 @@ class ClassCodegen(val state: TranslationState, val clazz: KtClass, val codeBuil
private fun generateAssignments() { private fun generateAssignments() {
fields.forEach { fields.forEach {
val argument = codeBuilder.getNewVariable(it.type) val argument = codeBuilder.getNewVariable(it.type)
codeBuilder.loadVariable(argument, LLVMVariable("${it.label}.addr", it.type, scope = LLVMLocalScope(), pointer = true)) codeBuilder.loadVariable(argument, LLVMVariable("${it.label}.addr", it.type, scope = LLVMLocalScope(), pointer = 1))
val classField = codeBuilder.getNewVariable(it.type, true) val classField = codeBuilder.getNewVariable(it.type, pointer = 1)
codeBuilder.loadClassField(classField, LLVMVariable("instance.addr", type, scope = LLVMLocalScope(), pointer = true), it.offset) codeBuilder.loadClassField(classField, LLVMVariable("instance.addr", type, scope = LLVMLocalScope(), pointer = 1), it.offset)
codeBuilder.storeVariable(classField, argument) codeBuilder.storeVariable(classField, argument)
} }
} }
private fun generateReturn() { private fun generateReturn() {
val dst = LLVMVariable("instance", type, scope = LLVMLocalScope(), pointer = true) val dst = LLVMVariable("instance", type, scope = LLVMLocalScope(), pointer = 1)
val src = LLVMVariable("instance.addr", type, scope = LLVMLocalScope(), pointer = true) val src = LLVMVariable("instance.addr", type, scope = LLVMLocalScope(), pointer = 1)
val castedDst = codeBuilder.bitcast(dst, LLVMCharType()) val castedDst = codeBuilder.bitcast(dst, LLVMCharType())
val castedSrc = codeBuilder.bitcast(src, LLVMCharType()) val castedSrc = codeBuilder.bitcast(src, LLVMCharType())
@@ -85,7 +85,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
var actualReturnType: LLVMType = returnType.type var actualReturnType: LLVMType = returnType.type
val actualArgs = ArrayList<LLVMVariable>() val actualArgs = ArrayList<LLVMVariable>()
if (returnType.pointer) { if (returnType.pointer > 0) {
actualReturnType = LLVMVoidType() actualReturnType = LLVMVoidType()
actualArgs.add(returnType) actualArgs.add(returnType)
} }
@@ -99,11 +99,11 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
private fun generateLoadArguments() { private fun generateLoadArguments() {
args.forEach { args.forEach {
if (it.type !is LLVMReferenceType || (it.type as LLVMReferenceType).isReturn) { if (it.type !is LLVMReferenceType || (it.type as LLVMReferenceType).isReturn) {
val loadVariable = LLVMVariable("${it.label}", it.type, it.label, LLVMLocalScope(), pointer = false) val loadVariable = LLVMVariable("${it.label}", it.type, it.label, LLVMLocalScope(), pointer = 0)
val allocVar = codeBuilder.loadArgument(loadVariable) val allocVar = codeBuilder.loadArgument(loadVariable)
variableManager.addVariable(it.label, allocVar, 2) variableManager.addVariable(it.label, allocVar, 2)
} else { } else {
variableManager.addVariable(it.label, LLVMVariable(it.label, it.type, it.label, LLVMLocalScope(), pointer = true), 2) variableManager.addVariable(it.label, LLVMVariable(it.label, it.type, it.label, LLVMLocalScope(), pointer = 0), 2)
} }
} }
} }
@@ -153,7 +153,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
val receiveValue = state.bindingContext.get(BindingContext.COMPILE_TIME_VALUE, expr) val receiveValue = state.bindingContext.get(BindingContext.COMPILE_TIME_VALUE, expr)
val type = (receiveValue as TypedCompileTimeConstant).type val type = (receiveValue as TypedCompileTimeConstant).type
val value = receiveValue.getValue(type) ?: return null val value = receiveValue.getValue(type) ?: return null
val variable = variableManager.receiveVariable(".str", LLVMStringType(value.toString().length), LLVMGlobalScope(), pointer = false) val variable = variableManager.receiveVariable(".str", LLVMStringType(value.toString().length), LLVMGlobalScope(), pointer = 0)
codeBuilder.addStringConstant(variable, value.toString()) codeBuilder.addStringConstant(variable, value.toString())
return variable return variable
@@ -173,7 +173,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
val clazz = state.classes[(receiver.type as LLVMReferenceType).type]!! val clazz = state.classes[(receiver.type as LLVMReferenceType).type]!!
val field = clazz.fieldsIndex[selectorName]!! val field = clazz.fieldsIndex[selectorName]!!
val result = codeBuilder.getNewVariable(field.type, pointer = true) val result = codeBuilder.getNewVariable(field.type, pointer = 1)
codeBuilder.loadClassField(result, receiver, field.offset) codeBuilder.loadClassField(result, receiver, field.offset)
return result return result
} }
@@ -183,7 +183,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
val arrayIndex = evaluateConstantExpression(expr.indexExpressions.first() as KtConstantExpression) val arrayIndex = evaluateConstantExpression(expr.indexExpressions.first() as KtConstantExpression)
val arrayReceivedVariable = codeBuilder.loadAndGetVariable(arrayNameVariable) val arrayReceivedVariable = codeBuilder.loadAndGetVariable(arrayNameVariable)
val arrayElementType = (arrayNameVariable.type as LLVMArray).basicType() val arrayElementType = (arrayNameVariable.type as LLVMArray).basicType()
val indexVariable = codeBuilder.getNewVariable(arrayElementType, pointer = true) val indexVariable = codeBuilder.getNewVariable(arrayElementType, pointer = 1)
codeBuilder.loadVariableOffset(indexVariable, arrayReceivedVariable, arrayIndex); codeBuilder.loadVariableOffset(indexVariable, arrayReceivedVariable, arrayIndex);
return indexVariable return indexVariable
} }
@@ -219,7 +219,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
val result = codeBuilder.getNewVariable(returnType.type) val result = codeBuilder.getNewVariable(returnType.type)
codeBuilder.allocVar(result) codeBuilder.allocVar(result)
result.pointer = true result.pointer = 1
val args = ArrayList<LLVMSingleValue>() val args = ArrayList<LLVMSingleValue>()
args.add(result) args.add(result)
@@ -241,7 +241,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
val names = parseArgList(expr, scopeDepth).mapIndexed(fun(i: Int, llvmSingleValue: LLVMSingleValue): LLVMSingleValue { val names = parseArgList(expr, scopeDepth).mapIndexed(fun(i: Int, llvmSingleValue: LLVMSingleValue): LLVMSingleValue {
var result = llvmSingleValue var result = llvmSingleValue
if (result.pointer && !descriptor.args[i].pointer) { if (result.pointer > 0 && descriptor.args[i].pointer == 0) {
result = codeBuilder.getNewVariable(descriptor.args[i].type) result = codeBuilder.getNewVariable(descriptor.args[i].type)
codeBuilder.loadVariable(result, llvmSingleValue as LLVMVariable) codeBuilder.loadVariable(result, llvmSingleValue as LLVMVariable)
} }
@@ -261,7 +261,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
is LLVMReferenceType -> { is LLVMReferenceType -> {
val result = codeBuilder.getNewVariable(returnType) val result = codeBuilder.getNewVariable(returnType)
codeBuilder.allocVar(result) codeBuilder.allocVar(result)
result.pointer = true result.pointer = 1
val args = ArrayList<LLVMSingleValue>() val args = ArrayList<LLVMSingleValue>()
args.add(result) args.add(result)
@@ -285,7 +285,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
val resultPtr = codeBuilder.getNewVariable(returnType) val resultPtr = codeBuilder.getNewVariable(returnType)
codeBuilder.allocVar(resultPtr) codeBuilder.allocVar(resultPtr)
resultPtr.pointer = true resultPtr.pointer = 1
codeBuilder.storeVariable(resultPtr, result) codeBuilder.storeVariable(resultPtr, result)
return resultPtr return resultPtr
} }
@@ -323,7 +323,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
KtNodeTypes.CHARACTER_CONSTANT -> LLVMCharType() KtNodeTypes.CHARACTER_CONSTANT -> LLVMCharType()
else -> throw IllegalArgumentException("Unknown type") else -> throw IllegalArgumentException("Unknown type")
} }
return LLVMConstant(node.firstChildNode.text, type, pointer = false) return LLVMConstant(node.firstChildNode.text, type, pointer = 0)
} }
private fun evaluatePsiElement(element: PsiElement, scopeDepth: Int): LLVMSingleValue? { private fun evaluatePsiElement(element: PsiElement, scopeDepth: Int): LLVMSingleValue? {
@@ -388,7 +388,7 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
private fun executeIfExpression(condition: KtBinaryExpression, thenExpression: PsiElement, elseExpression: PsiElement?, scopeDepth: Int): LLVMVariable? { private fun executeIfExpression(condition: KtBinaryExpression, thenExpression: PsiElement, elseExpression: PsiElement?, scopeDepth: Int): LLVMVariable? {
val conditionResult: LLVMVariable = evaluateBinaryExpression(condition, scopeDepth + 1) val conditionResult: LLVMVariable = evaluateBinaryExpression(condition, scopeDepth + 1)
val variable = codeBuilder.getNewVariable(LLVMIntType(), true) val variable = codeBuilder.getNewVariable(LLVMIntType(), pointer = 1)
codeBuilder.allocVar(variable) codeBuilder.allocVar(variable)
val thenLabel = codeBuilder.getNewLabel(prefix = "if") val thenLabel = codeBuilder.getNewLabel(prefix = "if")
val elseLabel = codeBuilder.getNewLabel(prefix = "if") val elseLabel = codeBuilder.getNewLabel(prefix = "if")
@@ -436,13 +436,13 @@ class FunctionCodegen(val state: TranslationState, val function: KtNamedFunction
when (assignExpression) { when (assignExpression) {
is LLVMVariable -> { is LLVMVariable -> {
val allocVar = variableManager.receiveVariable(identifier!!.text, assignExpression.type, LLVMLocalScope(), pointer = true) val allocVar = variableManager.receiveVariable(identifier!!.text, assignExpression.type, LLVMLocalScope(), pointer = 1)
codeBuilder.allocVar(allocVar) codeBuilder.allocVar(allocVar)
variableManager.addVariable(identifier.text, allocVar, scopeDepth) variableManager.addVariable(identifier.text, allocVar, scopeDepth)
copyVariable(assignExpression, allocVar) copyVariable(assignExpression, allocVar)
} }
is LLVMConstant -> { is LLVMConstant -> {
val newVar = variableManager.receiveVariable(identifier!!.text, LLVMIntType(), LLVMLocalScope(), pointer = true) val newVar = variableManager.receiveVariable(identifier!!.text, LLVMIntType(), LLVMLocalScope(), pointer = 1)
codeBuilder.addConstant(newVar, assignExpression) codeBuilder.addConstant(newVar, assignExpression)
variableManager.addVariable(identifier.text, newVar, scopeDepth) variableManager.addVariable(identifier.text, newVar, scopeDepth)
@@ -19,7 +19,7 @@ class PropertyCodegen(val state: TranslationState, val property: KtProperty, val
val value = varInfo.value val value = varInfo.value
if (kotlinType.nameIfStandardType != null) { if (kotlinType.nameIfStandardType != null) {
val variableType = LLVMMapStandardType(property.name ?: return, kotlinType).type val variableType = LLVMMapStandardType(property.name ?: return, kotlinType).type
val variable = LLVMVariable(property.name.toString(), variableType, property.name.toString(), LLVMGlobalScope(), pointer = true) val variable = LLVMVariable(property.name.toString(), variableType, property.name.toString(), LLVMGlobalScope(), pointer = 1)
variableManager.addGlobalVariable(property.name.toString(), variable) variableManager.addGlobalVariable(property.name.toString(), variable)
codeBuilder.declareGlovalVariable(variable, variableType.parseArg(value.toString())) codeBuilder.declareGlovalVariable(variable, variableType.parseArg(value.toString()))
} }
@@ -29,7 +29,7 @@ class VariableManager {
globalVariableCollection.put(name, variable) globalVariableCollection.put(name, variable)
} }
fun receiveVariable(name: String, type: LLVMType, scope: LLVMScope, pointer: Boolean): LLVMVariable { fun receiveVariable(name: String, type: LLVMType, scope: LLVMScope, pointer: Int): LLVMVariable {
val ourVersion = variableVersion.getOrDefault(name, 0) + 1 val ourVersion = variableVersion.getOrDefault(name, 0) + 1
variableVersion.put(name, ourVersion) variableVersion.put(name, ourVersion)
@@ -26,7 +26,7 @@ class LLVMBuilder(val arm: Boolean) {
} }
} }
fun getNewVariable(type: LLVMType, pointer: Boolean = false, kotlinName: String? = null): LLVMVariable { fun getNewVariable(type: LLVMType, pointer: Int = 0, kotlinName: String? = null): LLVMVariable {
variableCount++ variableCount++
return LLVMVariable("var$variableCount", type, kotlinName, LLVMLocalScope(), pointer) return LLVMVariable("var$variableCount", type, kotlinName, LLVMLocalScope(), pointer)
} }
@@ -50,10 +50,7 @@ class LLVMBuilder(val arm: Boolean) {
fun receiveNativeValue(firstOp: LLVMSingleValue): LLVMSingleValue = when (firstOp) { fun receiveNativeValue(firstOp: LLVMSingleValue): LLVMSingleValue = when (firstOp) {
is LLVMConstant -> firstOp is LLVMConstant -> firstOp
is LLVMVariable -> when (firstOp.pointer) { is LLVMVariable -> if (firstOp.pointer == 0) firstOp else loadAndGetVariable(firstOp)
false -> firstOp
else -> loadAndGetVariable(firstOp)
}
else -> throw UnsupportedOperationException() else -> throw UnsupportedOperationException()
} }
@@ -143,7 +140,7 @@ class LLVMBuilder(val arm: Boolean) {
} }
fun loadArgument(llvmVariable: LLVMVariable, store: Boolean = true): LLVMVariable { fun loadArgument(llvmVariable: LLVMVariable, store: Boolean = true): LLVMVariable {
val allocVar = LLVMVariable("${llvmVariable.label}.addr", llvmVariable.type, llvmVariable.kotlinName, LLVMLocalScope(), true) val allocVar = LLVMVariable("${llvmVariable.label}.addr", llvmVariable.type, llvmVariable.kotlinName, LLVMLocalScope(), pointer = 1)
addVariableByRef(allocVar, llvmVariable, store) addVariableByRef(allocVar, llvmVariable, store)
return allocVar return allocVar
} }
@@ -175,7 +172,7 @@ class LLVMBuilder(val arm: Boolean) {
} }
fun loadAndGetVariable(source: LLVMVariable): LLVMVariable { fun loadAndGetVariable(source: LLVMVariable): LLVMVariable {
assert(!source.pointer) assert(source.pointer > 0)
val target = getNewVariable(source.type, source.pointer, source.kotlinName) val target = getNewVariable(source.type, source.pointer, source.kotlinName)
val code = "$target = load ${target.type}, ${source.getType()} $source, align ${target.type.align}" val code = "$target = load ${target.type}, ${source.getType()} $source, align ${target.type.align}"
llvmLocalCode.appendln(code) llvmLocalCode.appendln(code)
@@ -196,7 +193,7 @@ class LLVMBuilder(val arm: Boolean) {
} }
fun bitcast(src: LLVMVariable, llvmType: LLVMType): LLVMVariable { fun bitcast(src: LLVMVariable, llvmType: LLVMType): LLVMVariable {
val empty = getNewVariable(llvmType, true) val empty = getNewVariable(llvmType, pointer = 1)
val code = "$empty = bitcast ${src.getType()} $src to $llvmType*" val code = "$empty = bitcast ${src.getType()} $src to $llvmType*"
llvmLocalCode.appendln(code) llvmLocalCode.appendln(code)
return empty return empty
@@ -2,14 +2,14 @@ package org.kotlinnative.translator.llvm
import org.kotlinnative.translator.llvm.types.LLVMType import org.kotlinnative.translator.llvm.types.LLVMType
open class LLVMConstant(value: String, override val type: LLVMType? = null, override var pointer: Boolean = false) : LLVMSingleValue(type, pointer) { open class LLVMConstant(value: String, override val type: LLVMType? = null, override var pointer: Int = 0) : LLVMSingleValue(type, pointer) {
val value: String val value: String
init { init {
this.value = type?.parseArg(value) ?: value this.value = type?.parseArg(value) ?: value
} }
override fun getType(): String = type.toString() + if (pointer) "*" else " " override fun getType(): String = type.toString() + "*".repeat(pointer)
override fun toString(): String = value override fun toString(): String = value
} }
@@ -3,7 +3,7 @@ package org.kotlinnative.translator.llvm
import org.kotlinnative.translator.exceptions.UnimplementedException import org.kotlinnative.translator.exceptions.UnimplementedException
import org.kotlinnative.translator.llvm.types.LLVMType import org.kotlinnative.translator.llvm.types.LLVMType
open class LLVMSingleValue(open val type: LLVMType? = null, open var pointer: Boolean = false) : LLVMNode() { open class LLVMSingleValue(open val type: LLVMType? = null, open var pointer: Int = 0) : LLVMNode() {
open fun getType(): String = throw UnimplementedException() open fun getType(): String = throw UnimplementedException()
} }
@@ -2,9 +2,9 @@ package org.kotlinnative.translator.llvm
import org.kotlinnative.translator.llvm.types.LLVMType import org.kotlinnative.translator.llvm.types.LLVMType
open class LLVMVariable(val label: String, override val type: LLVMType, var kotlinName: String? = null, val scope: LLVMScope = LLVMLocalScope(), override var pointer: Boolean = false) : LLVMSingleValue() { open class LLVMVariable(val label: String, override val type: LLVMType, var kotlinName: String? = null, val scope: LLVMScope = LLVMLocalScope(), override var pointer: Int = 0) : LLVMSingleValue() {
override fun getType(): String = type.toString() + if (pointer) "*" else "" override fun getType(): String = type.toString() + "*".repeat(pointer)
override fun toString(): String = "$scope$label" override fun toString(): String = "$scope$label"
} }
@@ -14,9 +14,9 @@ fun LLVMFunctionDescriptor(name: String, argTypes: List<LLVMVariable>?, returnTy
}?.joinToString()}) ${if (arm) "#0" else ""}" }?.joinToString()}) ${if (arm) "#0" else ""}"
fun LLVMMapStandardType(name: String, type: KotlinType, scope: LLVMScope = LLVMLocalScope()): LLVMVariable = when { fun LLVMMapStandardType(name: String, type: KotlinType, scope: LLVMScope = LLVMLocalScope()): LLVMVariable = when {
type.isFunctionTypeOrSubtype -> LLVMVariable(name, LLVMFunctionType(type), type.toString(), scope, pointer = true) type.isFunctionTypeOrSubtype -> LLVMVariable(name, LLVMFunctionType(type), type.toString(), scope, pointer = 1)
type.toString() == "Int" -> LLVMVariable(name, LLVMIntType(), type.toString(), scope) type.toString() == "Int" -> LLVMVariable(name, LLVMIntType(), type.toString(), scope)
type.toString() == "Double" -> LLVMVariable(name, LLVMDoubleType(), type.toString(), scope) type.toString() == "Double" -> LLVMVariable(name, LLVMDoubleType(), type.toString(), scope)
type.isUnit() -> LLVMVariable("", LLVMVoidType(), "", scope) type.isUnit() -> LLVMVariable("", LLVMVoidType(), "", scope)
else -> LLVMVariable(name, LLVMReferenceType("$type"), name, scope, pointer = true) else -> LLVMVariable(name, LLVMReferenceType("$type"), name, scope, pointer = 1)
} }