Skip to content

Commit

Permalink
Add optional name: Option[String] to Val.{Local, Var} allowing to p…
Browse files Browse the repository at this point in the history
…reserve original name of ValDef. It would be used to provide better debug informations in the future
  • Loading branch information
WojciechMazur committed Jul 12, 2023
1 parent fe23977 commit 0779259
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 167 deletions.
15 changes: 11 additions & 4 deletions nir/src/main/scala/scala/scalanative/nir/Buffer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ class Buffer(implicit fresh: Fresh) {
this += Inst.Throw(value, unwind)

// Compute ops
def let(name: Local, op: Op, unwind: Next)(implicit pos: Position): Val = {
this += Inst.Let(name, op, unwind)
Val.Local(name, op.resty)
def let(id: Local, op: Op, unwind: Next, name: Option[String] = None)(implicit
pos: Position
): Val = {
this += Inst.Let(id, op, unwind)
Val.Local(id, op.resty, name)
}
def let(op: Op, unwind: Next)(implicit pos: Position): Val =
let(fresh(), op, unwind)
def let(op: Op, name: Option[String], unwind: Next)(implicit
pos: Position
): Val =
let(fresh(), op, unwind, name)
def call(ty: Type, ptr: Val, args: Seq[Val], unwind: Next)(implicit
pos: Position
): Val =
Expand Down Expand Up @@ -131,8 +137,9 @@ class Buffer(implicit fresh: Fresh) {
let(Op.Box(ty, obj), unwind)
def unbox(ty: Type, obj: Val, unwind: Next)(implicit pos: Position): Val =
let(Op.Unbox(ty, obj), unwind)
// TODO: named var
def var_(ty: Type, unwind: Next)(implicit pos: Position): Val =
let(Op.Var(ty), unwind)
let(Op.Var(ty, name = None), unwind)
def varload(slot: Val, unwind: Next)(implicit pos: Position): Val =
let(Op.Varload(slot), unwind)
def varstore(slot: Val, value: Val, unwind: Next)(implicit
Expand Down
8 changes: 4 additions & 4 deletions nir/src/main/scala/scala/scalanative/nir/Fresh.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ object Fresh {
def apply(insts: Seq[Inst]): Fresh = {
var max = -1L
insts.foreach {
case Inst.Let(local, _, Next.Unwind(Val.Local(exc, _), _)) =>
case Inst.Let(local, _, Next.Unwind(Val.Local(exc, _, _), _)) =>
max = Math.max(max, local.id)
max = Math.max(max, exc.id)
case Inst.Let(local, _, _) =>
max = Math.max(max, local.id)
case Inst.Label(local, params) =>
max = Math.max(max, local.id)
params.foreach { param => max = Math.max(max, param.name.id) }
case Inst.Throw(_, Next.Unwind(Val.Local(exc, _), _)) =>
params.foreach { param => max = Math.max(max, param.id.id) }
case Inst.Throw(_, Next.Unwind(Val.Local(exc, _, _), _)) =>
max = Math.max(max, exc.id)
case Inst.Unreachable(Next.Unwind(Val.Local(exc, _), _)) =>
case Inst.Unreachable(Next.Unwind(Val.Local(exc, _, _), _)) =>
max = Math.max(max, exc.id)
case _ =>
()
Expand Down
4 changes: 2 additions & 2 deletions nir/src/main/scala/scala/scalanative/nir/Ops.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sealed abstract class Op {
val nullable = Type.isPtrBox(refty)
Type.Ref(refty.className, exact = true, nullable = nullable)
case Op.Unbox(ty, _) => Type.unbox(ty)
case Op.Var(ty) => Type.Var(ty)
case Op.Var(ty, _) => Type.Var(ty)
case Op.Varload(slot) => val Type.Var(ty) = slot.ty: @unchecked; ty
case Op.Varstore(slot, _) => Type.Unit
case Op.Arrayalloc(ty, _, _) =>
Expand Down Expand Up @@ -149,7 +149,7 @@ object Op {
final case class AlignmentOf(ty: Type) extends Op
final case class Box(ty: Type, obj: Val) extends Op
final case class Unbox(ty: Type, obj: Val) extends Op
final case class Var(ty: Type) extends Op
final case class Var(ty: Type, name: Option[String]) extends Op
final case class Varload(slot: Val) extends Op
final case class Varstore(slot: Val, value: Val) extends Op
final case class Arrayalloc(ty: Type, init: Val, zone: Option[Val]) extends Op
Expand Down
14 changes: 9 additions & 5 deletions nir/src/main/scala/scala/scalanative/nir/Show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ object Show {
} else {
str("(")
rep(params, sep = ", ") {
case Val.Local(n, ty) =>
case Val.Local(n, ty, _) =>
local_(n)
str(" : ")
type_(ty)
Expand Down Expand Up @@ -369,10 +369,13 @@ object Show {
type_(ty)
str("] ")
val_(v)
case Op.Var(ty) =>
str("var[")
case Op.Var(ty, name) =>
str("var")
name.foreach(v => " " + quoted(v))
str(" [")
type_(ty)
str("]")

case Op.Varload(slot) =>
str("varload ")
val_(slot)
Expand Down Expand Up @@ -527,8 +530,9 @@ object Show {
val stringValue = new String(v.bytes, StandardCharsets.ISO_8859_1)
str(escapeNewLine(escapeQuotes(stringValue)))
str("\"")
case Val.Local(name, ty) =>
local_(name)
case Val.Local(id, ty, name) =>
local_(id)
name.foreach(v => str(s" [$v]"))
str(" : ")
type_(ty)
case Val.Global(name, ty) =>
Expand Down
13 changes: 6 additions & 7 deletions nir/src/main/scala/scala/scalanative/nir/Transform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ trait Transform {
inst match {
case Inst.Label(n, params) =>
val newparams = params.map { param =>
Val.Local(param.name, onType(param.ty))
param.copy(valty = onType(param.ty))
}
Inst.Label(n, newparams)
case Inst.Let(n, op, unwind) =>
Expand Down Expand Up @@ -104,8 +104,7 @@ trait Transform {
Op.Box(code, onVal(obj))
case Op.Unbox(code, obj) =>
Op.Unbox(code, onVal(obj))
case Op.Var(ty) =>
Op.Var(onType(ty))
case o @ Op.Var(ty, _) => o.copy(ty = onType(ty))
case Op.Varload(elem) =>
Op.Varload(onVal(elem))
case Op.Varstore(elem, value) =>
Expand All @@ -125,10 +124,10 @@ trait Transform {
case Val.StructValue(values) => Val.StructValue(values.map(onVal))
case Val.ArrayValue(ty, values) =>
Val.ArrayValue(onType(ty), values.map(onVal))
case Val.Local(n, ty) => Val.Local(n, onType(ty))
case Val.Global(n, ty) => Val.Global(n, onType(ty))
case Val.Const(v) => Val.Const(onVal(v))
case _ => value
case v @ Val.Local(_, ty, _) => v.copy(valty = onType(ty))
case Val.Global(n, ty) => Val.Global(n, onType(ty))
case Val.Const(v) => Val.Const(onVal(v))
case _ => value
}

def onType(ty: Type): Type = ty match {
Expand Down
10 changes: 5 additions & 5 deletions nir/src/main/scala/scala/scalanative/nir/Traverse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ trait Traverse {
onVal(obj)
case Op.Unbox(code, obj) =>
onVal(obj)
case Op.Var(ty) =>
case Op.Var(ty, _) =>
onType(ty)
case Op.Varload(elem) =>
onVal(elem)
Expand Down Expand Up @@ -151,10 +151,10 @@ trait Traverse {
case Val.ArrayValue(ty, values) =>
onType(ty)
values.foreach(onVal)
case Val.Local(n, ty) => onType(ty)
case Val.Global(n, ty) => onType(ty)
case Val.Const(v) => onVal(v)
case _ => ()
case Val.Local(_, ty, _) => onType(ty)
case Val.Global(n, ty) => onType(ty)
case Val.Const(v) => onVal(v)
case _ => ()
}

def onType(ty: Type): Unit = ty match {
Expand Down
8 changes: 6 additions & 2 deletions nir/src/main/scala/scala/scalanative/nir/Vals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ sealed abstract class Val {
case Val.StructValue(vals) => Type.StructValue(vals.map(_.ty))
case Val.ArrayValue(ty, vals) => Type.ArrayValue(ty, vals.length)
case v: Val.ByteString => Type.ArrayValue(Type.Byte, v.byteCount)
case Val.Local(_, ty) => ty
case Val.Local(_, ty, _) => ty
case Val.Global(_, ty) => ty

case Val.Unit => Type.Unit
Expand Down Expand Up @@ -188,7 +188,11 @@ object Val {
final case class ByteString(bytes: Array[scala.Byte]) extends Val {
def byteCount: scala.Int = bytes.length + 1
}
final case class Local(name: nir.Local, valty: nir.Type) extends Val
final case class Local(
id: nir.Local,
valty: nir.Type,
name: Option[java.lang.String] = None // TODO: audit default usages
) extends Val
final case class Global(name: nir.Global, valty: nir.Type) extends Val

// high-level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ final class BinaryDeserializer(buffer: ByteBuffer, fileName: String) {
case T.CopyOp => Op.Copy(getVal())
case T.BoxOp => Op.Box(getType(), getVal())
case T.UnboxOp => Op.Unbox(getType(), getVal())
case T.VarOp => Op.Var(getType())
case T.VarOp => Op.Var(getType(), getOpt(getString()))
case T.VarloadOp => Op.Varload(getVal())
case T.VarstoreOp => Op.Varstore(getVal(), getVal())
case T.ArrayallocOp => Op.Arrayalloc(getType(), getVal(), None)
Expand All @@ -364,7 +364,7 @@ final class BinaryDeserializer(buffer: ByteBuffer, fileName: String) {
}

private def getParams(): Seq[Val.Local] = getSeq(getParam())
private def getParam(): Val.Local = Val.Local(getLocal(), getType())
private def getParam(): Val.Local = Val.Local(getLocal(), getType(), getOpt(getString()))

private def getTypes(): Seq[Type] = getSeq(getType())
private def getType(): Type = in(prelude.sections.types) {
Expand Down Expand Up @@ -411,8 +411,9 @@ final class BinaryDeserializer(buffer: ByteBuffer, fileName: String) {
case T.StructValueVal => Val.StructValue(getVals())
case T.ArrayValueVal => Val.ArrayValue(getType(), getVals())
case T.ByteStringVal => Val.ByteString(getBytes())
case T.LocalVal => Val.Local(getLocal(), getType())
case T.GlobalVal => Val.Global(getGlobal(), getType())
// TODO: dedicated unnamed local tag
case T.LocalVal => Val.Local(getLocal(), getType(), getOpt(getString()))
case T.GlobalVal => Val.Global(getGlobal(), getType())

case T.UnitVal => Val.Unit
case T.ConstVal => Val.Const(getVal())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ final class BinarySerializer(channel: WritableByteChannel) {
}

override def put(value: Val): Unit = value match {
case Val.Local(n, ty) => putTag(T.LocalVal); putLocal(n); putType(ty)
// TODO: decicated unnamed local tag
case Val.Local(n, ty, name) => putTag(T.LocalVal); putLocal(n); putType(ty); putOpt(name)(putString)
case Val.Global(n, ty) => putTag(T.GlobalVal); putGlobal(n); putType(ty)
case Val.Unit => putTag(T.UnitVal)
case Val.Null => putTag(T.NullVal)
Expand Down Expand Up @@ -514,9 +515,10 @@ final class BinarySerializer(channel: WritableByteChannel) {
putType(ty)
putVal(v)

case Op.Var(ty) =>
case Op.Var(ty, name) =>
putTag(T.VarOp)
putType(ty)
putOpt(name)(putString)

case Op.Varload(slot) =>
putTag(T.VarloadOp)
Expand All @@ -542,8 +544,9 @@ final class BinarySerializer(channel: WritableByteChannel) {

private def putParams(params: Seq[Val.Local]) = putSeq(params)(putParam)
private def putParam(param: Val.Local) = {
putLebUnsignedLong(param.name.id)
putLebUnsignedLong(param.id.id)
putType(param.ty)
putOpt(param.name)(putString)
}

private def putInst(cf: Inst) = {
Expand Down
4 changes: 2 additions & 2 deletions tools/src/main/scala/scala/scalanative/checker/Check.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ final class Check(implicit linked: linker.Result) extends NIRCheck {

def enterInst(inst: Inst): Unit = {
def enterParam(value: Val.Local) = {
val Val.Local(local, ty) = value
val Val.Local(local, ty, _) = value
env(local) = ty
}

Expand Down Expand Up @@ -318,7 +318,7 @@ final class Check(implicit linked: linker.Result) extends NIRCheck {
} { unboxedty => expect(unboxedty, value) }
case Op.Unbox(ty, obj) =>
expect(Rt.Object, obj)
case Op.Var(ty) =>
case Op.Var(ty, _) =>
ok
case Op.Varload(slot) =>
slot.ty match {
Expand Down
20 changes: 10 additions & 10 deletions tools/src/main/scala/scala/scalanative/codegen/Generate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ object Generate {
val and = Val.Local(fresh(), Type.Int)
val result = Val.Local(fresh(), Type.Bool)

def let(local: Val.Local, op: Op) = Inst.Let(local.name, op, Next.None)
def let(local: Val.Local, op: Op) = Inst.Let(local.id, op, Next.None)

buf += Defn.Define(
Attrs(inlineHint = Attr.AlwaysInline),
Expand Down Expand Up @@ -242,7 +242,7 @@ object Generate {
Seq(
// init __stack_bottom variable
Inst.Let(
stackBottom.name,
stackBottom.id,
Op.Stackalloc(Type.Ptr, Val.Long(0)),
unwind
),
Expand Down Expand Up @@ -293,9 +293,9 @@ object Generate {
genGcInit(unwindProvider) ++
genClassInitializersCalls(unwindProvider) ++
Seq(
Inst.Let(rt.name, Op.Module(Runtime.name), unwind),
Inst.Let(rt.id, Op.Module(Runtime.name), unwind),
Inst.Let(
arr.name,
arr.id,
Op.Call(RuntimeInitSig, RuntimeInit, Seq(rt, argc, argv)),
unwind
),
Expand Down Expand Up @@ -387,10 +387,10 @@ object Generate {
def loadSinglethreadImpl: Seq[Inst] = {
Seq(
Inst.Label(entry, Seq.empty),
Inst.Let(slot.name, selectSlot, Next.None),
Inst.Let(self.name, Op.Load(clsTy, slot), Next.None),
Inst.Let(slot.id, selectSlot, Next.None),
Inst.Let(self.id, Op.Load(clsTy, slot), Next.None),
Inst.Let(
cond.name,
cond.id,
Op.Comp(Comp.Ine, nir.Rt.Object, self, Val.Null),
Next.None
),
Expand All @@ -399,7 +399,7 @@ object Generate {
Inst.Ret(self),
Inst.Label(initialize, Seq.empty),
Inst.Let(
alloc.name,
alloc.id,
Op.Classalloc(name, zone = None),
Next.None
),
Expand All @@ -423,9 +423,9 @@ object Generate {

Seq(
Inst.Label(entry, Seq.empty),
Inst.Let(slot.name, selectSlot, Next.None),
Inst.Let(slot.id, selectSlot, Next.None),
Inst.Let(
self.name,
self.id,
Op.Call(
LoadModuleSig,
LoadModule,
Expand Down
12 changes: 6 additions & 6 deletions tools/src/main/scala/scala/scalanative/codegen/Lower.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ object Lower {
}

insts.foreach {
case inst @ Inst.Let(n, Op.Var(ty), unwind) =>
buf.let(n, Op.Stackalloc(ty, one), unwind)(inst.pos)
case inst @ Inst.Let(n, Op.Var(ty, name), unwind) =>
buf.let(n, Op.Stackalloc(ty, one), unwind, name = name)(inst.pos)
case _ =>
()
}
Expand Down Expand Up @@ -440,10 +440,10 @@ object Lower {
genUnboxOp(buf, n, op)
case op: Op.Module =>
genModuleOp(buf, n, op)
case Op.Var(_) => () // Already emmited
case Op.Varload(Val.Local(slot, Type.Var(ty))) =>
case Op.Var(_, _) => () // Already emmited
case Op.Varload(Val.Local(slot, Type.Var(ty), _)) =>
buf.let(n, Op.Load(ty, Val.Local(slot, Type.Ptr)), unwind)
case Op.Varstore(Val.Local(slot, Type.Var(ty)), value) =>
case Op.Varstore(Val.Local(slot, Type.Var(ty), _), value) =>
buf.let(
n,
Op.Store(ty, Val.Local(slot, Type.Ptr), genVal(buf, value)),
Expand Down Expand Up @@ -1038,7 +1038,7 @@ object Lower {
val safeZoneAllocImplMethod = Val.Local(fresh(), Type.Ptr)
genMethodOp(
buf,
safeZoneAllocImplMethod.name,
safeZoneAllocImplMethod.id,
Op.Method(zone, safeZoneAllocImpl.sig)
)
buf.let(
Expand Down

0 comments on commit 0779259

Please sign in to comment.