Skip to content
Merged
Show file tree
Hide file tree
Changes from 57 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
1f127e2
Add `Term.Resolved` and `DefinitionSymbol`
FlandiaYingman Sep 20, 2025
113fbe6
Support lowering of `Term.Resolved`
FlandiaYingman Sep 27, 2025
cf8c1c7
Defer `.class` expansion until lower
FlandiaYingman Sep 27, 2025
0f16141
WIP: Test Cases
FlandiaYingman Oct 9, 2025
df23015
Use `Term.Resolved` for Sel resoultion
FlandiaYingman Oct 9, 2025
b1545c0
Merge branch 'hkmc2' into resolved-term
FlandiaYingman Oct 9, 2025
5501876
Merge branch 'hkmc2' into resolved-term
FlandiaYingman Oct 11, 2025
85efd07
Merge branch 'hkmc2' into resolved-term
FlandiaYingman Oct 11, 2025
8606815
Some refactor_ization_ WIP
FlandiaYingman Oct 14, 2025
019c3ae
wrapup 1
FlandiaYingman Oct 30, 2025
99bdb5f
Apply Mark's Patch
FlandiaYingman Oct 30, 2025
55ba010
Resolve symbols for inner SynthSel
FlandiaYingman Oct 31, 2025
cd3ac61
Fix Effect Handler
FlandiaYingman Oct 31, 2025
66a700b
Fix LLIR
FlandiaYingman Oct 31, 2025
b4235eb
Recover errors that were already there
FlandiaYingman Oct 31, 2025
18ff415
Fix WASM
FlandiaYingman Oct 31, 2025
da6fcd7
Merge branch 'hkmc2' into resolved-term
FlandiaYingman Oct 31, 2025
1e5db6b
Fix duplicate symbol error
FlandiaYingman Oct 31, 2025
a2fbf48
Enhance type resolution
FlandiaYingman Oct 31, 2025
b4c9cd1
Cleanup asDefnSym_TODO
FlandiaYingman Oct 31, 2025
37dc34a
Fix instanceof for objects
FlandiaYingman Oct 31, 2025
d1c43f1
Cleanup hasLiftedClass
FlandiaYingman Oct 31, 2025
ef12ca6
Fix lifter bugs
CAG2Mark Oct 31, 2025
5de57fe
Cleanup symbol_SelectSymbol
FlandiaYingman Oct 31, 2025
f58deae
Cleanup add docs
FlandiaYingman Oct 31, 2025
df75b33
Cleanup HandlerLowering
FlandiaYingman Oct 31, 2025
09c39ab
Merge remote-tracking branch 'mark/top-quality-fixes-2' into resolved…
FlandiaYingman Oct 31, 2025
46830c6
Cleanup lifter
FlandiaYingman Nov 1, 2025
04640c1
Cleanup Normalization
FlandiaYingman Nov 1, 2025
efc0587
Cleanup Pattern
FlandiaYingman Nov 1, 2025
0137185
Cleanup Term
FlandiaYingman Nov 1, 2025
9597814
Apply suggestion from @LPTK
FlandiaYingman Nov 1, 2025
0997535
Cleanup HkScratch
FlandiaYingman Nov 1, 2025
82c0879
Cleanup some tests 1
FlandiaYingman Nov 1, 2025
571d9dd
Cleanup some tests 2
FlandiaYingman Nov 1, 2025
79a7a88
Cleanup Resolver
FlandiaYingman Nov 1, 2025
6ff7a4f
Correct location of TermDefinition
FlandiaYingman Nov 1, 2025
383da7f
Cleanup Resolver
FlandiaYingman Nov 1, 2025
dd08cce
Cleanup Elaborator
FlandiaYingman Nov 1, 2025
ce4f4a6
Apply @LPTK's suggestion
FlandiaYingman Nov 8, 2025
7d92a23
Apply @LPTK's suggestion
FlandiaYingman Nov 8, 2025
146ee2b
Clarify test comment
FlandiaYingman Nov 8, 2025
63ec9ee
Refactor out `t.resolvedSym.flatMap(_.asBlkMember)`
FlandiaYingman Nov 8, 2025
c327e45
Deobfuscate Harry's top-quality code
FlandiaYingman Nov 8, 2025
ba1f714
Refactor out `t.resolvedSym.flatMap(_.asBlkMember)`
FlandiaYingman Nov 8, 2025
4fe9cf6
Preserve empty lines in Lifter
FlandiaYingman Nov 8, 2025
6c50dde
Refine symbol typing and clean redundancy
FlandiaYingman Nov 8, 2025
31fb16c
Apply @LPTK's suggestion
FlandiaYingman Nov 8, 2025
a8bb754
Apply @LPTK's suggestion
FlandiaYingman Nov 8, 2025
e026034
Fix @LPTK's suggestion
FlandiaYingman Nov 8, 2025
ae1a97b
Fix sanity check test cases
FlandiaYingman Nov 8, 2025
51a77b8
Remove unused definition
FlandiaYingman Nov 8, 2025
55ca3fd
Store `TermSymbol` directly in `BlockMemberSymbol` rather than `TermD…
FlandiaYingman Nov 8, 2025
d81d0e6
Fix "Worst part of the PR 👎“
FlandiaYingman Nov 8, 2025
f527f3d
Refine TODO comments in resolver
FlandiaYingman Nov 8, 2025
a37f2f5
Merge branch 'hkmc2' into resolved-term
FlandiaYingman Nov 11, 2025
7aa3feb
Merge branch 'hkmc2' into resolved-term
FlandiaYingman Nov 11, 2025
58de90d
Prelinminarily remove `defn` from BMS
FlandiaYingman Nov 11, 2025
2c45f41
Move `defn` definition to `DefinitionSymbol`
FlandiaYingman Nov 13, 2025
ebb3af6
Apply @LPTK's top-quality suggestion
FlandiaYingman Nov 13, 2025
4bd0874
Revert regression
FlandiaYingman Nov 13, 2025
859ff43
Remove something that I forgot to remove
FlandiaYingman Nov 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ object ValDefn:
// * a lone module is represented as an empty class with a `companion` module.
final case class ClsLikeDefn(
owner: Opt[InnerSymbol],
isym: MemberSymbol[? <: ClassLikeDef] & InnerSymbol,
isym: DefinitionSymbol[? <: ClassLikeDef] & InnerSymbol,
sym: BlockMemberSymbol,
k: syntax.ClsLikeKind,
paramsOpt: Opt[ParamList],
Expand All @@ -414,12 +414,12 @@ final case class ClsLikeDefn(
bufferable: Option[Bool],
) extends Defn:
require(k isnt syntax.Mod)
val innerSym = S(isym)
val innerSym = S(isym.asMemSym)


// * This is only supposed to be for companion module definitions (notably, not for `object`)
final case class ClsLikeBody(
isym: MemberSymbol[? <: ModuleOrObjectDef] & InnerSymbol,
isym: DefinitionSymbol[? <: ModuleOrObjectDef] & InnerSymbol,
methods: Ls[FunDefn],
privateFields: Ls[TermSymbol],
publicFields: Ls[BlockMemberSymbol -> TermSymbol],
Expand Down Expand Up @@ -497,7 +497,7 @@ sealed abstract class Result extends AutoLocated:
case Lambda(params, body) => params :: Nil
case Tuple(mut, elems) => elems.map(_.value)
case Record(mut, elems) => elems.map(_.value)
case Value.Ref(l) => Nil
case Value.Ref(l, disamb) => Nil
case Value.This(sym) => Nil
case Value.Lit(lit) => lit :: Nil

Expand All @@ -518,7 +518,7 @@ sealed abstract class Result extends AutoLocated:
case Tuple(mut, elems) => elems.flatMap(_.value.freeVars).toSet
case Record(mut, args) =>
args.flatMap(arg => arg.idx.fold(Set.empty)(_.freeVars) ++ arg.value.freeVars).toSet
case Value.Ref(l) => Set(l)
case Value.Ref(l, disamb) => Set(l)
case Value.This(sym) => Set.empty
case Value.Lit(lit) => Set.empty
case DynSelect(qual, fld, arrayIdx) => qual.freeVars ++ fld.freeVars
Expand All @@ -531,11 +531,11 @@ sealed abstract class Result extends AutoLocated:
case Tuple(mut, elems) => elems.flatMap(_.value.freeVarsLLIR).toSet
case Record(mut, args) =>
args.flatMap(arg => arg.idx.fold(Set.empty)(_.freeVarsLLIR) ++ arg.value.freeVarsLLIR).toSet
case Value.Ref(l: (BuiltinSymbol | TopLevelSymbol | ClassSymbol | TermSymbol)) => Set.empty
case Value.Ref(l: MemberSymbol[?]) => l.defn match
case Value.Ref(l: (BuiltinSymbol | TopLevelSymbol | ClassSymbol | TermSymbol), disamb) => Set.empty
case Value.Ref(l: MemberSymbol[?], disamb) => l.defn match
case Some(d: ClassLikeDef) => Set.empty
case _ => Set(l)
case Value.Ref(l) => Set(l)
case Value.Ref(l, disamb) => Set(l)
case Value.This(sym) => Set.empty
case Value.Lit(lit) => Set.empty
case DynSelect(qual, fld, arrayIdx) => qual.freeVarsLLIR ++ fld.freeVarsLLIR
Expand All @@ -560,19 +560,38 @@ case class Record(mut: Bool, elems: Ls[RcdArg]) extends Result

sealed abstract class Path extends TrivialResult:
def selN(id: Tree.Ident): Path = Select(this, id)(N)
def sel(id: Tree.Ident, sym: FieldSymbol): Path = Select(this, id)(S(sym))
def sel(id: Tree.Ident, sym: DefinitionSymbol[?]): Path = Select(this, id)(S(sym))
def selSN(id: Str): Path = selN(new Tree.Ident(id))
def asArg = Arg(spread = N, this)

case class Select(qual: Path, name: Tree.Ident)(val symbol: Opt[FieldSymbol]) extends Path with ProductWithExtraInfo:
def extraInfo: Str = symbol.mkString
/**
* @param symbol The symbol representing the definition that the selection refers to, if known.
*/
case class Select(qual: Path, name: Tree.Ident)(val symbol: Opt[DefinitionSymbol[?]]) extends Path with ProductWithExtraInfo:
def extraInfo: Str = symbol.map(s => s"sym=${s}").mkString

case class DynSelect(qual: Path, fld: Path, arrayIdx: Bool) extends Path

enum Value extends Path:
case Ref(l: Local)
enum Value extends Path with ProductWithExtraInfo:
/**
* @param disamb The symbol disambiguating the definition that the reference refers to. This
* exists if and only if l is a BlockMemberSymbol.
*/
case Ref(l: Local, disamb: Opt[DefinitionSymbol[?]])
case This(sym: InnerSymbol) // TODO rm – just use Ref
case Lit(lit: Literal)

override def extraInfo: Str = this match
case Ref(l, disamb) => disamb.map(s => s"disamb=${s}").mkString
case _ => ""

object Value:
object Ref:
// * Some helper constructors that allow omitting the disambiguation symbol.
// * If the ref itself is a DefinitionSymbol, then disambiguating it results in itself.
def apply(l: DefinitionSymbol[?]): Ref = Ref(l, S(l))
// * If the ref is a symbol that does not refer to a definition, then there is no disambiguation.
def apply(l: TempSymbol | VarSymbol | BuiltinSymbol): Ref = Ref(l, N)

case class Arg(spread: Opt[Bool], value: Path)

Expand Down Expand Up @@ -603,6 +622,6 @@ extension (k: Block => Block)
def blockBuilder: Block => Block = identity

extension (l: Local)
def asPath: Path = Value.Ref(l)
def asPath: Path = Value.Ref(l, N)


Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ class BlockTransformer(subst: SymbolSubst):
case v: Value => applyValue(v)(k)

def applyValue(v: Value)(k: Value => Block) = v match
case Value.Ref(l) =>
case Value.Ref(l, disamb) =>
val l2 = l.subst
k(if (l2 is l) then v else Value.Ref(l2))
k(if (l2 is l) then v else Value.Ref(l2, disamb))
case Value.This(sym) =>
val sym2 = sym.subst
k(if (sym2 is sym) then v else Value.This(sym2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class BlockTraverser:
case v: Value => applyValue(v)

def applyValue(v: Value): Unit = v match
case Value.Ref(l) => l.traverse
case Value.Ref(l, disamb) => l.traverse
case Value.This(sym) => sym.traverse
case Value.Lit(lit) => ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class BufferableTransform()(using Ctx, State, Raise):
fieldMap.get(sym).orElse(pubFieldMap.get(sym).flatMap(fieldMap.get(_))).fold(super.applyPath(p)(k)): off =>
getOffset(off): res =>
k(res)
case Value.Ref(l) =>
case Value.Ref(l, _) =>
fieldMap.get(l).fold(super.applyPath(p)(k)): off =>
getOffset(off): res =>
k(res)
Expand Down
25 changes: 13 additions & 12 deletions hkmc2/shared/src/main/scala/hkmc2/codegen/HandlerLowering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object HandlerLowering:

private case class LinkState(res: Local, cls: Path, uid: Path)

type FnOrCls = Either[BlockMemberSymbol, MemberSymbol[? <: ClassLikeDef] & InnerSymbol]
type FnOrCls = Either[BlockMemberSymbol, DefinitionSymbol[? <: ClassLikeDef] & InnerSymbol]

// isTopLevel:
// whether the current block is the top level block, as we do not emit code for continuation class on the top level
Expand Down Expand Up @@ -132,7 +132,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
def apply(res: Local, uid: StateId, rest: Block) =
Assign(res, PureCall(Value.Ref(resumptionSymbol), List(Value.Lit(Tree.IntLit(uid)))), rest)
def unapply(blk: Block) = blk match
case Assign(res, PureCall(Value.Ref(`resumptionSymbol`), List(Value.Lit(Tree.IntLit(uid)))), rest) =>
case Assign(res, PureCall(Value.Ref(`resumptionSymbol`, _), List(Value.Lit(Tree.IntLit(uid)))), rest) =>
Some(res, uid, rest)
case _ => None

Expand All @@ -141,7 +141,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
def apply(res: Local, uid: StateId) =
Assign(res, PureCall(Value.Ref(returnContSymbol), List(Value.Lit(Tree.IntLit(uid)))), End(""))
def unapply(blk: Block) = blk match
case Assign(res, PureCall(Value.Ref(`returnContSymbol`), List(Value.Lit(Tree.IntLit(uid)))), _) =>
case Assign(res, PureCall(Value.Ref(`returnContSymbol`, _), List(Value.Lit(Tree.IntLit(uid)))), _) =>
Some(res, uid)
case _ => None

Expand All @@ -157,7 +157,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
def unapply(blk: Block) = blk match
case Assign(
res,
PureCall(Value.Ref(`callSymbol`), List(Value.Lit(Tree.IntLit(uid)))),
PureCall(Value.Ref(`callSymbol`, _), List(Value.Lit(Tree.IntLit(uid)))),
Assign(_, c, rest)) =>
Some(res, uid, c, rest)
case _ => None
Expand All @@ -167,15 +167,15 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
def apply(uid: StateId) =
Return(PureCall(Value.Ref(transitionSymbol), List(Value.Lit(Tree.IntLit(uid)))), false)
def unapply(blk: Block) = blk match
case Return(PureCall(Value.Ref(`transitionSymbol`), List(Value.Lit(Tree.IntLit(uid)))), false) =>
case Return(PureCall(Value.Ref(`transitionSymbol`, _), List(Value.Lit(Tree.IntLit(uid)))), false) =>
S(uid)
case _ => N

object FnEnd:
private val fnEndSymbol = freshTmp("fnEnd")
def apply() = Return(PureCall(Value.Ref(fnEndSymbol), Nil), false)
def unapply(blk: Block) = blk match
case Return(PureCall(Value.Ref(`fnEndSymbol`), Nil), false) => true
case Return(PureCall(Value.Ref(`fnEndSymbol`, _), Nil), false) => true
case _ => false

private class FreshId:
Expand Down Expand Up @@ -522,7 +522,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
ResultPlaceholder(res, freshId(), c2, k(Value.Ref(res)))
case r => super.applyResult(r)(k)
override def applyPath(p: Path)(k: Path => Block): Block = p match
case Value.Ref(`getLocalsSym`) => k(handlerCtx.debugInfo.prevLocalsFn.get)
case Value.Ref(`getLocalsSym`, _) => k(handlerCtx.debugInfo.prevLocalsFn.get)
case _ => super.applyPath(p)(k)
override def applyLam(lam: Lambda): Lambda =
// This should normally be unreachable due to prior desugaring of lambda
Expand Down Expand Up @@ -591,7 +591,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
f.owner match
case None => S(Call(f.sym.asPath, params)(true, true))
case Some(owner) =>
S(Call(Select(owner.asPath, Tree.Ident(f.sym.nme))(S(f.sym)), params)(true, true))
S(Call(Select(owner.asPath, Tree.Ident(f.sym.nme))(N), params)(true, true))
case _ => None // TODO: more than one plist

FunDefn(f.owner, f.sym, f.params, translateBlock(f.body,
Expand Down Expand Up @@ -657,7 +657,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
handler.sym, handler.params,
Define(
fDef,
Return(PureCall(paths.mkEffectPath, h.cls.asPath :: Value.Ref(sym) :: Nil), false)))
Return(PureCall(paths.mkEffectPath, h.cls.asPath :: Value.Ref(sym, N) :: Nil), false)))

// Some limited handling of effects extending classes and having access to their fields.
// Currently does not support super() raising effects.
Expand Down Expand Up @@ -687,7 +687,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,

val body = blockBuilder
.define(clsDefn)
.assign(h.lhs, Instantiate(mut = true, Value.Ref(clsDefn.sym), Nil))
.assign(h.lhs, Instantiate(mut = true, Value.Ref(clsDefn.sym, S(h.cls)), Nil))
.rest(handlerBody)

val defn = FunDefn(
Expand Down Expand Up @@ -735,7 +735,7 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
val transform = new BlockTransformerShallow(SymbolSubst()):
override def applyResult(r: Result)(k: Result => Block): Block =
r match
case c @ Call(Value.Ref(s: BuiltinSymbol), _) => ()
case c @ Call(Value.Ref(s: BuiltinSymbol, _), _) => ()
case c: Call if !c.mayRaiseEffects => ()
case _: Call | _: Instantiate => containsCall = true
case _ => ()
Expand Down Expand Up @@ -792,7 +792,8 @@ class HandlerLowering(paths: HandlerPaths, opt: EffectHandlers)(using TL, Raise,
override def applyBlock(b: Block): Block = b match
case ReturnCont(res, uid) => Return(Call(
Select(clsSym.asPath, Tree.Ident("doUnwind"))(
N /* this refers to the method defined in Runtime.FunctionContFrame */ ),
N /* this refers to the method defined in Runtime.FunctionContFrame */
),
res.asPath.asArg :: Value.Lit(Tree.IntLit(uid)).asArg :: Nil)(true, false),
false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object LambdaRewriter:
case lam: Lambda =>
val sym = BlockMemberSymbol("lambda", Nil, nameIsMeaningful = false)
lambdasList ::= (sym -> super.applyLam(lam))
k(Value.Ref(sym))
k(Value.Ref(sym, N))
case _ => super.applyResult(r)(k)
val blk = lambdaRewriter.applyBlock(b)
(blk, lambdasList)
Expand Down
Loading
Loading