Skip to content

Commit

Permalink
more code golf
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Nov 2, 2019
1 parent 5b961cb commit 04fcf4a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 37 deletions.
9 changes: 4 additions & 5 deletions src/cadenza/data/frame.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cadenza.data // cadenza.aot?

import cadenza.jit.Code
import org.intelligence.asm.*
import cadenza.panic
import cadenza.todo
import com.oracle.truffle.api.dsl.TypeSystemReference
Expand All @@ -10,10 +9,10 @@ import com.oracle.truffle.api.frame.VirtualFrame
import com.oracle.truffle.api.nodes.Node
import com.oracle.truffle.api.nodes.NodeCost
import com.oracle.truffle.api.nodes.NodeInfo
import org.objectweb.asm.Opcodes.ASM7
import org.objectweb.asm.tree.*
import org.intelligence.asm.*
import org.objectweb.asm.Type
import java.lang.IndexOutOfBoundsException
import org.objectweb.asm.tree.AnnotationNode
import org.objectweb.asm.tree.LabelNode

// manufacture cadenza.data.frame.IIO, OIO, etc.

Expand Down Expand Up @@ -47,7 +46,7 @@ abstract class DataFrame {
abstract fun isObject(slot: Slot): Boolean
}

final class SimpleDataFrame(vararg val data: Any?) {
class SimpleDataFrame(vararg val data: Any?) {
fun getValue(slot: Slot) = data[slot]
fun getSize() = data.size
@Throws(FrameSlotTypeException::class)
Expand Down
10 changes: 4 additions & 6 deletions src/cadenza/jit/code.kt
Original file line number Diff line number Diff line change
Expand Up @@ -277,31 +277,29 @@ abstract class Code(val loc: Loc? = null) : Node(), InstrumentableNode {
}

companion object {
@Suppress("NOTHING_TO_INLINE")
inline fun `var`(slot: FrameSlot, loc: Loc? = null): Var = CodeFactory.VarNodeGen.create(slot, loc)
fun `var`(slot: FrameSlot, loc: Loc? = null): Var = CodeFactory.VarNodeGen.create(slot, loc)

// invariant callTarget points to a native function body with known arity
@Suppress("NOTHING_TO_INLINE","UNUSED")
@Suppress("UNUSED")
fun lam(callTarget: RootCallTarget, type: Type, loc: Loc? = null): Lam {
val root = callTarget.rootNode
assert(root is ClosureRootNode)
return lam((root as ClosureRootNode).arity, callTarget, type, loc)
}

// package a foreign root call target with known arity
@Suppress("NOTHING_TO_INLINE")
fun lam(arity: Int, callTarget: RootCallTarget, type: Type, loc: Loc? = null): Lam {
return lam(null, noFrameBuilders, arity, callTarget, type, loc)
}

@Suppress("NOTHING_TO_INLINE","unused")
//@Suppress("unused")
fun lam(closureFrameDescriptor: FrameDescriptor, captureSteps: Array<FrameBuilder>, callTarget: RootCallTarget, type: Type, loc: Loc? = null): Lam {
val root = callTarget.rootNode
assert(root is ClosureRootNode)
return lam(closureFrameDescriptor, captureSteps, (root as ClosureRootNode).arity, callTarget, type, loc)
}

// ensures that all the invariants for the constructor are satisfied
@Suppress("NOTHING_TO_INLINE")
fun lam(closureFrameDescriptor: FrameDescriptor?, captureSteps: Array<FrameBuilder>, arity: Int, callTarget: RootCallTarget, type: Type, loc: Loc? = null): Lam {
assert(arity > 0)
val hasCaptureSteps = captureSteps.isNotEmpty()
Expand Down
6 changes: 2 additions & 4 deletions src/cadenza/jit/frame_builder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ abstract class FrameBuilder(
@field:Child protected var rhs: Code
) : Node() {

@Suppress("NOTHING_TO_INLINE")
inline fun build(frame: VirtualFrame, oldFrame: VirtualFrame) {
fun build(frame: VirtualFrame, oldFrame: VirtualFrame) {
execute(frame, 0, oldFrame)
}

Expand Down Expand Up @@ -89,5 +88,4 @@ abstract class FrameBuilder(
override fun isAdoptable() = false
}

@Suppress("NOTHING_TO_INLINE","unused")
inline fun put(slot: FrameSlot, value: Code): FrameBuilder = FrameBuilderNodeGen.create(slot, value)
fun put(slot: FrameSlot, value: Code): FrameBuilder = FrameBuilderNodeGen.create(slot, value)
2 changes: 1 addition & 1 deletion src/cadenza/language.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class Language : TruffleLanguage<Language.Context>() {

class Context(
@Suppress("unused") val language: Language,
var env: TruffleLanguage.Env
var env: Env
) {
val singleThreadedAssumption = Truffle.getRuntime().createAssumption("context is single threaded")!!
fun shutdown() {}
Expand Down
22 changes: 8 additions & 14 deletions src/cadenza/launcher.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package cadenza

import org.fusesource.jansi.Ansi
import org.fusesource.jansi.Ansi.*
import org.fusesource.jansi.Ansi.Attribute
import org.fusesource.jansi.Ansi.ansi
import org.fusesource.jansi.AnsiConsole
import org.graalvm.launcher.*
import org.graalvm.launcher.AbstractLanguageLauncher
import org.graalvm.options.OptionCategory
import org.graalvm.polyglot.Context
import org.graalvm.polyglot.PolyglotException
import org.graalvm.polyglot.Source

import java.io.File
import java.io.IOException
import java.nio.file.Paths
import java.util.*
import kotlin.io.*
import kotlin.system.exitProcess

fun <A> withAnsi(f: () -> A): A {
Expand Down Expand Up @@ -94,16 +93,11 @@ class Launcher : AbstractLanguageLauncher() {
"--show-version" -> versionAction = VersionAction.PrintAndContinue
"--version" -> versionAction = VersionAction.PrintAndExit
else -> {
//var optionName = option
val argument: String?
val equalsIndex = option.indexOf('=')
when {
equalsIndex > 0 -> {
argument = option.substring(equalsIndex + 1)
//optionName = option.substring(0, equalsIndex)
}
iterator.hasNext() -> argument = iterator.next()
else -> argument = null
val argument = when {
equalsIndex > 0 -> option.substring(equalsIndex + 1)
iterator.hasNext() -> iterator.next()
else -> null
}
unrecognizedOptions.add(option)
if (equalsIndex < 0 && argument != null) iterator.previous()
Expand Down Expand Up @@ -132,7 +126,7 @@ class Launcher : AbstractLanguageLauncher() {
}

override fun printHelp(_maxCategory: OptionCategory) {
Ansi.ansi().run {
ansi().run {
newline()
render("Usage: @|italic,blue cadenza|@ @|bold [OPTION]|@... @|bold [FILE]|@ @|bold [PROGRAM ARGS]|@\n\n")
a("Run cadenza programs on GraalVM\n\n")
Expand Down
2 changes: 1 addition & 1 deletion src/cadenza/loc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cadenza
import com.oracle.truffle.api.source.Source
import com.oracle.truffle.api.source.SourceSection

sealed class Loc() {
sealed class Loc {
abstract fun section(source: Source): SourceSection
object Unavailable : Loc() {
override fun section(source: Source): SourceSection = source.createUnavailableSection()
Expand Down
12 changes: 6 additions & 6 deletions src/pretty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data class Seq(val children: List<Out>) : Out() {
internal sealed class Atom : Out()

internal object Newline : Atom() {
override fun emit(s: Ansi) { s.a(System.getProperty("line.separator")) };
override fun emit(s: Ansi) { s.a(System.getProperty("line.separator")) }
}

internal sealed class Chunk : Atom() {
Expand Down Expand Up @@ -114,19 +114,19 @@ class Pretty(
const val DEFAULT_MAX_WIDTH: Int = 80
const val DEFAULT_MAX_RIBBON: Int = 60

fun prep(maxWidth: W = Pretty.DEFAULT_MAX_WIDTH, maxRibbon: W = Pretty.DEFAULT_MAX_RIBBON, doc: Doc): Out {
fun prep(maxWidth: W = DEFAULT_MAX_WIDTH, maxRibbon: W = DEFAULT_MAX_RIBBON, doc: Doc): Out {
val printer = Pretty(maxWidth,maxRibbon)
doc(printer)
return Seq(printer.output)
}

fun ppString(maxWidth: W = Pretty.DEFAULT_MAX_WIDTH, maxRibbon: W = Pretty.DEFAULT_MAX_RIBBON, doc: Doc): String {
fun ppString(maxWidth: W = DEFAULT_MAX_WIDTH, maxRibbon: W = DEFAULT_MAX_RIBBON, doc: Doc): String {
val builder = Ansi.ansi()
prep(maxWidth, maxRibbon, doc).emit(builder)
return builder.toString()
}

fun pp(maxWidth: W = Pretty.DEFAULT_MAX_WIDTH, maxRibbon: W = Pretty.DEFAULT_MAX_RIBBON, doc: Doc) = println(ppString(maxWidth, maxRibbon, doc))
fun pp(maxWidth: W = DEFAULT_MAX_WIDTH, maxRibbon: W = DEFAULT_MAX_RIBBON, doc: Doc) = println(ppString(maxWidth, maxRibbon, doc))
fun doc(x: Doc): Doc = x

fun Ansi.out(s: Out): Ansi = this.also { s.emit(this) }
Expand Down Expand Up @@ -332,7 +332,7 @@ inline fun <A> Pretty.expr(d: D<A>): A = align { grouped(d) }

fun <A> Pretty.fg(color: Ansi.Color, bright: Boolean = true, f: D<A>): A {
val old: Format = format
val new: Color = Color(color, bright)
val new = Color(color, bright)
val ann = object : Ann {
override val delta: Format.Delta get() = Format.Delta(fg = new)
override fun set(ansi: Ansi) = new.fg(ansi)
Expand All @@ -354,7 +354,7 @@ fun <A> Pretty.yellow(bright: Boolean = true, f: D<A>): A = fg(Ansi.Color.YELLOW

fun <A> Pretty.bg(color: Ansi.Color, bright: Boolean = false, f: D<A>): A {
val old: Format = format
val new: Color = Color(color, bright)
val new = Color(color, bright)
val ann = object : Ann {
override val delta: Format.Delta get() = Format.Delta(bg = new)
override fun set(ansi: Ansi) = new.bg(ansi)
Expand Down

0 comments on commit 04fcf4a

Please sign in to comment.