Skip to content

Commit 0062994

Browse files
waterlensLPTK
authored andcommitted
Llir improvements (hkust-taco#292)
Co-authored-by: Lionel Parreaux <lionel.parreaux@gmail.com>
1 parent 1108976 commit 0062994

39 files changed

+3684
-1181
lines changed

compiler/shared/main/scala/mlscript/compiler/codegen/CppAst.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ case class CompilationUnit(includes: Ls[Str], decls: Ls[Decl], defs: Ls[Def]):
161161
"HiddenTheseEntities", "True", "False", "Callable", "List", "Cons", "Nil", "Option", "Some", "None", "Pair", "Tuple2", "Tuple3", "Nat", "S", "O"
162162
)
163163
stack_list(defs.filterNot {
164-
case Def.StructDef(name, _, _, _) => hiddenNames.contains(name.stripPrefix("_mls_"))
164+
case d: Def.StructDef => hiddenNames.contains(d.name.stripPrefix("_mls_"))
165165
case _ => false
166166
}.map(_.toDocument))
167167

compiler/shared/main/scala/mlscript/compiler/codegen/CppCompilerHost.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ final class CppCompilerHost(val auxPath: Str):
4141
return
4242

4343
output("Execution succeeded: ")
44-
for line <- stdout do output(line)
44+
for line <- stdout do output(line)

hkmc2/shared/src/main/scala/hkmc2/codegen/Block.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,24 @@ sealed abstract class Block extends Product with AutoLocated:
120120
(bod.freeVars - lhs) ++ rst.freeVars ++ hdr.flatMap(_.freeVars)
121121
case End(msg) => Set.empty
122122

123-
// TODO: freeVarsLLIR skips `fun` and `cls` in `Call` and `Instantiate` respectively, which is needed in some
124-
// other places. However, adding them breaks some LLIR tests. Supposedly, once the IR uses the new symbol system,
125-
// this should no longer happen. This version should be removed once that is resolved.
126123
lazy val freeVarsLLIR: Set[Local] = this match
127124
case Match(scrut, arms, dflt, rest) =>
128125
scrut.freeVarsLLIR ++ dflt.toList.flatMap(_.freeVarsLLIR) ++ rest.freeVarsLLIR
129126
++ arms.flatMap:
130-
(pat, arm) => arm.freeVarsLLIR -- pat.freeVars
127+
(pat, arm) => arm.freeVarsLLIR -- pat.freeVarsLLIR
131128
case Return(res, implct) => res.freeVarsLLIR
132129
case Throw(exc) => exc.freeVarsLLIR
133130
case Label(label, body, rest) => (body.freeVarsLLIR - label) ++ rest.freeVarsLLIR
134-
case Break(label) => Set(label)
135-
case Continue(label) => Set(label)
131+
case Break(label) => Set.empty
132+
case Continue(label) => Set.empty
136133
case Begin(sub, rest) => sub.freeVarsLLIR ++ rest.freeVarsLLIR
137134
case TryBlock(sub, finallyDo, rest) => sub.freeVarsLLIR ++ finallyDo.freeVarsLLIR ++ rest.freeVarsLLIR
138-
case Assign(lhs, rhs, rest) => Set(lhs) ++ rhs.freeVarsLLIR ++ rest.freeVarsLLIR
135+
case Assign(lhs, rhs, rest) => rhs.freeVarsLLIR ++ (rest.freeVarsLLIR - lhs)
139136
case AssignField(lhs, nme, rhs, rest) => lhs.freeVarsLLIR ++ rhs.freeVarsLLIR ++ rest.freeVarsLLIR
140137
case AssignDynField(lhs, fld, arrayIdx, rhs, rest) => lhs.freeVarsLLIR ++ fld.freeVarsLLIR ++ rhs.freeVarsLLIR ++ rest.freeVarsLLIR
141-
case Define(defn, rest) => defn.freeVarsLLIR ++ rest.freeVarsLLIR
138+
case Define(defn, rest) => defn.freeVarsLLIR ++ (rest.freeVarsLLIR - defn.sym)
142139
case HandleBlock(lhs, res, par, args, cls, hdr, bod, rst) =>
143-
(bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVars)
140+
(bod.freeVarsLLIR - lhs) ++ rst.freeVarsLLIR ++ hdr.flatMap(_.freeVarsLLIR)
144141
case End(msg) => Set.empty
145142

146143
lazy val subBlocks: Ls[Block] = this match
@@ -385,8 +382,8 @@ final case class Handler(
385382
params: Ls[ParamList],
386383
body: Block,
387384
):
388-
lazy val freeVarsLLIR: Set[Local] = body.freeVarsLLIR -- params.flatMap(_.paramSyms) - sym - resumeSym
389385
lazy val freeVars: Set[Local] = body.freeVars -- params.flatMap(_.paramSyms) - sym - resumeSym
386+
lazy val freeVarsLLIR: Set[Local] = body.freeVarsLLIR -- params.flatMap(_.paramSyms) - sym - resumeSym
390387

391388
/* Represents either unreachable code (for functions that must return a result)
392389
* or the end of a non-returning function or a REPL block */
@@ -446,9 +443,13 @@ sealed abstract class Result extends AutoLocated:
446443
case Value.Rcd(args) => args.flatMap(arg => arg.idx.fold(Set.empty)(_.freeVars) ++ arg.value.freeVars).toSet
447444

448445
lazy val freeVarsLLIR: Set[Local] = this match
449-
case Call(fun, args) => args.flatMap(_.value.freeVarsLLIR).toSet
450-
case Instantiate(cls, args) => args.flatMap(_.freeVarsLLIR).toSet
446+
case Call(fun, args) => fun.freeVarsLLIR ++ args.flatMap(_.value.freeVarsLLIR).toSet
447+
case Instantiate(cls, args) => cls.freeVarsLLIR ++ args.flatMap(_.freeVarsLLIR).toSet
451448
case Select(qual, name) => qual.freeVarsLLIR
449+
case Value.Ref(l: (BuiltinSymbol | TopLevelSymbol | ClassSymbol | TermSymbol)) => Set.empty
450+
case Value.Ref(l: MemberSymbol[?]) => l.defn match
451+
case Some(d: ClassLikeDef) => Set.empty
452+
case _ => Set(l)
452453
case Value.Ref(l) => Set(l)
453454
case Value.This(sym) => Set.empty
454455
case Value.Lit(lit) => Set.empty

hkmc2/shared/src/main/scala/hkmc2/codegen/Printer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ object Printer:
3131
.map{ case (c, b) => doc"${case_doc(c)} => #{ # ${mkDocument(b)} #} " }
3232
.mkDocument(sep = doc" # ")
3333
val docDefault = dflt.map(mkDocument).getOrElse(doc"")
34-
doc"match ${mkDocument(scrut)} #{ # ${docCases} # else #{ # ${docDefault} #} #} # in # ${mkDocument(rest)} "
34+
doc"match ${mkDocument(scrut)} #{ # ${docCases} # else #{ # ${docDefault} #} #} # in # ${mkDocument(rest)}"
3535
case Return(res, implct) => doc"return ${mkDocument(res)}"
3636
case Throw(exc) => doc"throw ${mkDocument(exc)}"
3737
case Label(label, body, rest) =>

hkmc2/shared/src/main/scala/hkmc2/codegen/cpp/Ast.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
package hkmc2.codegen.cpp
1+
package hkmc2
2+
package codegen.cpp
23

34
import mlscript._
45
import mlscript.utils._
56
import mlscript.utils.shorthands._
6-
7-
import hkmc2.Message.MessageContext
8-
import hkmc2.document._
9-
107
import scala.language.implicitConversions
118

9+
import document._
10+
1211
private def raw(x: String): Document = doc"$x"
1312
given Conversion[String, Document] = x => doc"$x"
1413

@@ -130,14 +129,13 @@ enum Expr:
130129
case Unary(op: Str, expr: Expr)
131130
case Binary(op: Str, lhs: Expr, rhs: Expr)
132131
case Initializer(exprs: Ls[Expr])
133-
case Constructor(name: Str, init: Expr)
134132

135133
def toDocument: Document =
136134
def aux(x: Expr): Document = x match
137135
case Var(name) => name
138136
case IntLit(value) => value.toString
139137
case DoubleLit(value) => value.toString
140-
case StrLit(value) => s"\"$value\"" // need more reliable escape utils
138+
case StrLit(value) => value.escaped
141139
case CharLit(value) => value.toInt.toString
142140
case Call(func, args) =>
143141
doc"${func.toDocument}(${Expr.toDocuments(args, sep = ", ")})"
@@ -151,8 +149,6 @@ enum Expr:
151149
doc"(${lhs.toDocument} $op ${rhs.toDocument})"
152150
case Initializer(exprs) =>
153151
doc"{${Expr.toDocuments(exprs, sep = ", ")}}"
154-
case Constructor(name, init) =>
155-
doc"$name(${init.toDocument})"
156152
aux(this)
157153

158154
case class CompilationUnit(includes: Ls[Str], decls: Ls[Decl], defs: Ls[Def]):
@@ -161,57 +157,63 @@ case class CompilationUnit(includes: Ls[Str], decls: Ls[Decl], defs: Ls[Def]):
161157
def toDocumentWithoutHidden: Document =
162158
val hiddenNames: Set[Str] = Set()
163159
defs.filterNot {
164-
case Def.StructDef(name, _, _, _) => hiddenNames.contains(name.stripPrefix("_mls_"))
160+
case Def.StructDef(name, _, _, _, _) => hiddenNames.contains(name.stripPrefix("_mls_"))
165161
case _ => false
166162
}.map(_.toDocument).mkDocument(doc" # ")
167163

168164
enum Decl:
169165
case StructDecl(name: Str)
170166
case EnumDecl(name: Str)
171-
case FuncDecl(ret: Type, name: Str, args: Ls[Type])
167+
case FuncDecl(ret: Type, name: Str, args: Ls[Type], isOverride: Bool, isVirtual: Bool)
172168
case VarDecl(name: Str, typ: Type)
173169

174170
def toDocument: Document =
175171
def aux(x: Decl): Document = x match
176172
case StructDecl(name) => doc"struct $name;"
177173
case EnumDecl(name) => doc"enum $name;"
178-
case FuncDecl(ret, name, args) =>
179-
doc"${ret.toDocument()} $name(${Type.toDocuments(args, sep = ", ")});"
174+
case FuncDecl(ret, name, args, or, virt) =>
175+
val docVirt = (if virt then doc"virtual " else doc"")
176+
val docSpecRet = ret.toDocument()
177+
val docArgs = Type.toDocuments(args, sep = ", ")
178+
val docOverride = if or then doc" override" else doc""
179+
doc"$docVirt$docSpecRet $name($docArgs)$docOverride;"
180180
case VarDecl(name, typ) =>
181181
doc"${typ.toDocument()} $name;"
182182
aux(this)
183183

184184
enum Def:
185-
case StructDef(name: Str, fields: Ls[(Str, Type)], inherit: Opt[Ls[Str]], methods: Ls[Def] = Ls.empty)
185+
case StructDef(name: Str, fields: Ls[(Str, Type)], inherit: Opt[Ls[Str]], methods: Ls[Def], methodsDecl: Ls[Decl])
186186
case EnumDef(name: Str, fields: Ls[(Str, Opt[Int])])
187-
case FuncDef(specret: Type, name: Str, args: Ls[(Str, Type)], body: Stmt.Block, or: Bool = false, virt: Bool = false)
187+
case FuncDef(specret: Type, name: Str, args: Ls[(Str, Type)], body: Stmt.Block, isOverride: Bool, isVirtual: Bool, in_scope: Opt[Str])
188188
case VarDef(typ: Type, name: Str, init: Opt[Expr])
189189
case RawDef(raw: Str)
190190

191191
def toDocument: Document =
192192
def aux(x: Def): Document = x match
193-
case StructDef(name, fields, inherit, defs) =>
193+
case StructDef(name, fields, inherit, defs, decls) =>
194194
val docFirst = doc"struct $name${inherit.fold(doc"")(x => doc": public ${x.mkDocument(doc", ")}")} {"
195195
val docFields = fields.map {
196196
case (name, typ) => doc"${typ.toDocument()} $name;"
197197
}.mkDocument(doc" # ")
198198
val docDefs = defs.map(_.toDocument).mkDocument(doc" # ")
199+
val docDecls = decls.map(_.toDocument).mkDocument(doc" # ")
199200
val docLast = "};"
200-
doc"$docFirst #{ # $docFields # $docDefs #} # $docLast"
201+
doc"$docFirst #{ # $docFields # $docDefs # $docDecls #} # $docLast"
201202
case EnumDef(name, fields) =>
202203
val docFirst = doc"enum $name {"
203204
val docFields = fields.map {
204205
case (name, value) => value.fold(s"$name")(x => s"$name = $x")
205206
}.mkDocument(doc" # ")
206207
val docLast = "};"
207208
doc"$docFirst #{ # $docFields #} # $docLast"
208-
case FuncDef(specret, name, args, body, or, virt) =>
209+
case FuncDef(specret, name, args, body, or, virt, scope) =>
209210
val docVirt = (if virt then doc"virtual " else doc"")
210211
val docSpecRet = specret.toDocument()
211212
val docArgs = Type.toDocuments(args, sep = ", ")
212213
val docOverride = if or then doc" override" else doc""
213214
val docBody = body.toDocument
214-
doc"$docVirt$docSpecRet $name($docArgs)$docOverride ${body.toDocument}"
215+
val docScope = scope.fold(doc"")(x => doc"$x::")
216+
doc"$docVirt$docSpecRet $docScope$name($docArgs)$docOverride ${body.toDocument}"
215217
case VarDef(typ, name, init) =>
216218
val docTyp = typ.toDocument()
217219
val docInit = init.fold(raw(""))(x => doc" = ${x.toDocument}")

0 commit comments

Comments
 (0)