All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.backend.wasm.ast.WasmInstructions.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.backend.wasm.ast


sealed class WasmImmediate {
    object None : WasmImmediate()
    class DeclarationReference(val name: String) : WasmImmediate()
    class LiteralValue(val value: T) : WasmImmediate()
}

sealed class WasmInstruction(
    val mnemonic: String,
    val immediate: WasmImmediate = WasmImmediate.None,
    val operands: List = emptyList()
)

class WasmSimpleInstruction(mnemonic: String, operands: List) :
    WasmInstruction(mnemonic, operands = operands)

class WasmNop : WasmInstruction("nop")

class WasmReturn(values: List) :
    WasmInstruction("return", operands = values)

class WasmDrop(instructions: List) :
    WasmInstruction("drop", operands = instructions)

class WasmCall(name: String, operands: List) :
    WasmInstruction("call", WasmImmediate.DeclarationReference(name), operands)

class WasmGetLocal(name: String) :
    WasmInstruction("get_local", WasmImmediate.DeclarationReference(name))

class WasmGetGlobal(name: String) :
    WasmInstruction("get_global", WasmImmediate.DeclarationReference(name))

class WasmSetGlobal(name: String, value: WasmInstruction) :
    WasmInstruction("set_global", WasmImmediate.DeclarationReference(name), listOf(value))

class WasmSetLocal(name: String, value: WasmInstruction) :
    WasmInstruction("set_local", WasmImmediate.DeclarationReference(name), listOf(value))

class WasmIf(condition: WasmInstruction, thenInstructions: WasmThen?, elseInstruction: WasmElse?) :
    WasmInstruction("if", operands = listOfNotNull(condition, thenInstructions, elseInstruction))

class WasmThen(inst: WasmInstruction) :
    WasmInstruction("then", operands = listOf(inst))

class WasmElse(inst: WasmInstruction) :
    WasmInstruction("else", operands = listOf(inst))

class WasmBlock(instructions: List) :
    WasmInstruction("block", operands = instructions)

sealed class WasmConst(value: KotlinType, type: WasmType) :
    WasmInstruction(type.mnemonic + ".const", WasmImmediate.LiteralValue(value))

class WasmI32Const(value: Int) : WasmConst(value, WasmI32)
class WasmI64Const(value: Long) : WasmConst(value, WasmI64)
class WasmF32Const(value: Float) : WasmConst(value, WasmF32)
class WasmF64Const(value: Double) : WasmConst(value, WasmF64)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy