Skip to content

Commit

Permalink
more streamlining and kotlinisms
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Nov 2, 2019
1 parent 7974111 commit 5b961cb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 63 deletions.
54 changes: 26 additions & 28 deletions src/cadenza/jit/code.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private inline fun isSuperCombinator(callTarget: RootCallTarget) =
@GenerateWrapper
@NodeInfo(language = "core", description = "core nodes")
@TypeSystemReference(DataTypes::class)
abstract class Code(val section: Section = Section.default) : Node(), InstrumentableNode {
abstract class Code(val loc: Loc? = null) : Node(), InstrumentableNode {

@Throws(NeutralException::class)
abstract fun execute(frame: VirtualFrame): Any?
Expand All @@ -45,20 +45,18 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument
@Throws(NeutralException::class)
open fun executeUnit(frame: VirtualFrame) { execute(frame) }

override fun getSourceSection(): SourceSection? =
(if (section.sourceCharIndex == NO_SOURCE) rootNode else null)?.sourceSection?.source?.section(section)

override fun isInstrumentable() = section.sourceCharIndex != NO_SOURCE
override fun getSourceSection(): SourceSection? = loc?.let { rootNode?.sourceSection?.source?.section(it) }
override fun isInstrumentable() = loc !== null
override fun hasTag(tag: Class<out Tag>?) = tag == StandardTags.ExpressionTag::class.java
override fun createWrapper(probe: ProbeNode): InstrumentableNode.WrapperNode = CodeWrapper(this, probe)

@TypeSystemReference(DataTypes::class)
@NodeInfo(shortName = "App")
class App(
@field:Child var rator: Code,
@field:Children val rands: Array<Code>
, section: Section = Section.default
) : Code(section) {
@field:Children val rands: Array<Code>,
loc: Loc? = null
) : Code(loc) {
@Child private var indirectCallNode: IndirectCallNode = Truffle.getRuntime().createIndirectCallNode()

@ExplodeLoop
Expand Down Expand Up @@ -102,8 +100,8 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument
@field:Child private var condNode: Code,
@field:Child private var thenNode: Code,
@field:Child private var elseNode: Code,
section: Section = Section.default
) : Code(section) {
loc: Loc? = null
) : Code(loc) {
private val conditionProfile = ConditionProfile.createBinaryProfile()

@Throws(NeutralException::class)
Expand Down Expand Up @@ -142,8 +140,8 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument
private val arity: Int,
@field:Child internal var callTarget: RootCallTarget,
internal val type: Type,
section: Section = Section.default
) : Code(section) {
loc: Loc? = null
) : Code(loc) {

// do we need to capture an environment?
private inline fun isSuperCombinator() = closureFrameDescriptor != null
Expand All @@ -165,7 +163,7 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument


@NodeInfo(shortName = "Read")
abstract class Var protected constructor(private val slot: FrameSlot, section: Section = Section.default) : Code(section) {
abstract class Var protected constructor(private val slot: FrameSlot, loc: Loc? = null) : Code(loc) {

@Specialization(rewriteOn = [FrameSlotTypeException::class])
@Throws(FrameSlotTypeException::class)
Expand All @@ -182,7 +180,7 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument
}

@Suppress("unused")
class Ann(@field:Child private var body: Code, val type: Type, section: Section = Section.default) : Code(section) {
class Ann(@field:Child private var body: Code, val type: Type, loc: Loc? = null) : Code(loc) {
@Throws(NeutralException::class)
override fun execute(frame: VirtualFrame): Any? = body.execute(frame)
override fun executeAny(frame: VirtualFrame): Any? = body.executeAny(frame)
Expand All @@ -201,8 +199,8 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument
val type: Type,
private val builtin: Builtin,
@field:Child internal var arg: Code,
section: Section = Section.default
) : Code(section) {
loc: Loc? = null
) : Code(loc) {
override fun executeAny(frame: VirtualFrame): Any? =
try {
builtin.execute(frame, arg)
Expand Down Expand Up @@ -246,7 +244,7 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument
// instrumentation
@Suppress("NOTHING_TO_INLINE","unused")
@NodeInfo(shortName = "LitBool")
class LitBool(val value: Boolean, section: Section = Section.default): Code(section) {
class LitBool(val value: Boolean, loc: Loc? = null): Code(loc) {
@Suppress("UNUSED_PARAMETER")
override fun execute(frame: VirtualFrame) = value
@Suppress("UNUSED_PARAMETER")
Expand All @@ -255,7 +253,7 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument

@Suppress("NOTHING_TO_INLINE")
@NodeInfo(shortName = "LitInt")
class LitInt(val value: Int, section: Section = Section.default): Code(section) {
class LitInt(val value: Int, loc: Loc? = null): Code(loc) {
@Suppress("UNUSED_PARAMETER")
override fun execute(frame: VirtualFrame) = value
@Suppress("UNUSED_PARAMETER")
Expand All @@ -264,7 +262,7 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument

@Suppress("NOTHING_TO_INLINE","unused")
@NodeInfo(shortName = "LitBigInt")
class LitBigInt(val value: BigInt, section: Section = Section.default): Code(section) {
class LitBigInt(val value: BigInt, loc: Loc? = null): Code(loc) {
@Suppress("UNUSED_PARAMETER")
override fun execute(frame: VirtualFrame) = value
@Throws(UnexpectedResultException::class)
Expand All @@ -280,31 +278,31 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument

companion object {
@Suppress("NOTHING_TO_INLINE")
inline fun `var`(slot: FrameSlot, section: Section = Section.default): Var = CodeFactory.VarNodeGen.create(slot, section)
inline 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")
fun lam(callTarget: RootCallTarget, type: Type, section: Section = Section.default): Lam {
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, section)
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, section: Section = Section.default): Lam {
return lam(null, noFrameBuilders, arity, callTarget, type, section)
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")
fun lam(closureFrameDescriptor: FrameDescriptor, captureSteps: Array<FrameBuilder>, callTarget: RootCallTarget, type: Type, section: Section = Section.default): Lam {
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, section)
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, section: Section = Section.default): Lam {
fun lam(closureFrameDescriptor: FrameDescriptor?, captureSteps: Array<FrameBuilder>, arity: Int, callTarget: RootCallTarget, type: Type, loc: Loc? = null): Lam {
assert(arity > 0)
val hasCaptureSteps = captureSteps.isNotEmpty()
assert(hasCaptureSteps == isSuperCombinator(callTarget)) { "mismatched calling convention" }
Expand All @@ -314,7 +312,7 @@ abstract class Code(val section: Section = Section.default) : Node(), Instrument
arity,
callTarget,
type,
section
loc
)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/cadenza/jit/frame_builder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ abstract class FrameBuilder(
}

protected fun allowsBooleanSlot(frame: VirtualFrame) = allowsSlotKind(frame, FrameSlotKind.Boolean)

protected fun allowsIntegerSlot(frame: VirtualFrame) = allowsSlotKind(frame, FrameSlotKind.Int)

// UnexpectedResultException lets us "accept" an answer on the slow path, but it forces me to give back an Object. small price to pay
Expand Down
19 changes: 19 additions & 0 deletions src/cadenza/loc.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cadenza

import com.oracle.truffle.api.source.Source
import com.oracle.truffle.api.source.SourceSection

sealed class Loc() {
abstract fun section(source: Source): SourceSection
object Unavailable : Loc() {
override fun section(source: Source): SourceSection = source.createUnavailableSection()
}
data class Range(val start: Int, val length: Int) : Loc() {
override fun section(source: Source): SourceSection = source.createSection(start, length)
}
data class Line(val line: Int) : Loc() {
override fun section(source: Source): SourceSection = source.createSection(line)
}
}

fun Source.section(loc: Loc): SourceSection = loc.section(this)
21 changes: 0 additions & 21 deletions src/cadenza/section.kt

This file was deleted.

17 changes: 8 additions & 9 deletions src/cadenza/semantics/terms.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cadenza.semantics

import cadenza.jit.Code
import cadenza.Section
import cadenza.todo
import cadenza.*
import com.oracle.truffle.api.frame.FrameDescriptor

typealias Ctx = Env<Type>
Expand All @@ -23,15 +22,15 @@ abstract class Term {

companion object {
@Suppress("unused")
fun tvar(name: String, section: Section = Section.default): Term = object : Term() {
fun tvar(name: String, loc: Loc? = null): Term = object : Term() {
@Throws(TypeError::class)
override fun infer(ctx: Ctx): Witness = object : Witness(ctx.lookup(name)) {
override fun compile(fd: FrameDescriptor): Code = Code.`var`(fd.findOrAddFrameSlot(name), section)
override fun compile(fd: FrameDescriptor): Code = Code.`var`(fd.findOrAddFrameSlot(name), loc)
}
}

@Suppress("unused")
fun tif(cond: Term, thenTerm: Term, elseTerm: Term, section: Section = Section.default): Term = object : Term() {
fun tif(cond: Term, thenTerm: Term, elseTerm: Term, loc: Loc? = null): Term = object : Term() {
@Throws(TypeError::class)
override fun infer(ctx: Ctx): Witness {
val condWitness = cond.check(ctx, Type.Bool)
Expand All @@ -40,14 +39,14 @@ abstract class Term {
val elseWitness = elseTerm.check(ctx, actualType)
return object : Witness(actualType) {
override fun compile(fd: FrameDescriptor): Code {
return Code.If(actualType, condWitness.compile(fd), thenWitness.compile(fd), elseWitness.compile(fd), section)
return Code.If(actualType, condWitness.compile(fd), thenWitness.compile(fd), elseWitness.compile(fd), loc)
}
}
}
}

@Suppress("unused")
fun tapp(trator: Term, vararg trands: Term, section: Section = Section.default): Term = object : Term() {
fun tapp(trator: Term, vararg trands: Term, loc: Loc? = null): Term = object : Term() {
@Throws(TypeError::class)
override fun infer(ctx: Ctx): Witness {
val wrator = trator.infer(ctx)
Expand All @@ -63,14 +62,14 @@ abstract class Term {
return Code.App(
wrator.compile(fd),
wrands.map { it.compile(fd) }.toTypedArray(),
section
loc
)
}
}
}
}

@Suppress("UNUSED_PARAMETER","unused")
fun tlam(names: Array<Name>, body: Term, section: Section = Section.default): Term = todo
fun tlam(names: Array<Name>, body: Term, loc: Loc? = null): Term = todo
}
}
4 changes: 0 additions & 4 deletions src/parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import java.util.regex.Matcher
import java.util.regex.Pattern

class ParseError(var pos: Int, message: String? = null) : Exception(message) {
constructor(pos: Int, message: String? = null, cause: Throwable): this(pos,message) {
initCause(cause)
}
override fun fillInStackTrace() = this // don't record
companion object { const val serialVersionUID : Long = 1L }
override fun toString(): String = message ?: super.toString()
}

data class Expected(val what: Any, val next: Expected?)
Expand Down

0 comments on commit 5b961cb

Please sign in to comment.