Skip to content

Commit 2ddbbef

Browse files
authored
Added support for Tuples (hkust-taco#374)
1 parent 1df59ac commit 2ddbbef

7 files changed

Lines changed: 402 additions & 23 deletions

File tree

hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/Ctx.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ class TypeInfo(
128128
doc"(type${idDoc.surroundUnlessEmpty(doc" ")} ${compType.toWat})"
129129
end TypeInfo
130130

131+
enum WasmIntrinsicType:
132+
case TupleArray(mutable: Bool)
133+
131134
object Ctx:
132135
val binaryOps: Map[Str, (Expr, Expr) => Expr] = Map(
133136
"plus_impl" -> i32.add,
@@ -192,6 +195,7 @@ class Ctx(
192195
import Ctx.prettyString
193196

194197
private val wasmIntrinsicFuncs: MutMap[Str, FuncIdx] = MutMap.empty
198+
private val wasmIntrinsicTypes: MutMap[WasmIntrinsicType, TypeIdx] = MutMap.empty
195199

196200
/** Adds a type into this context. */
197201
def addType(sym: Opt[BlockMemberSymbol], typeInfo: TypeInfo): TypeIdx =
@@ -324,6 +328,13 @@ class Ctx(
324328
def getOrCreateWasmIntrinsic(name: Str, createIntrinsic: => FuncIdx): FuncIdx =
325329
wasmIntrinsicFuncs.getOrElseUpdate(name, createIntrinsic)
326330

331+
/**
332+
* Returns the cached [[TypeIdx]] for the intrinsic type `key`, creating it with `createType` if
333+
* it does not yet exist in this context.
334+
*/
335+
def getOrCreateWasmIntrinsicType(key: WasmIntrinsicType, createType: => TypeIdx): TypeIdx =
336+
wasmIntrinsicTypes.getOrElseUpdate(key, createType)
337+
327338
def toWat: Document =
328339
doc"(module #{ # ${(types.toSeq ++ funcs.toSeq).map(_.toWat).mkDocument(doc" # ")}) #} "
329340

hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/Instructions.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,30 @@ object Instructions:
243243
stackargs = Seq(arrayRef),
244244
resultType = S(I32Type)
245245
)
246+
247+
/** Creates an `array.new_fixed` instruction. */
248+
def new_fixed(arrayType: TypeIdx, items: Seq[Expr]): FoldedInstr = FoldedInstr(
249+
mnemonic = "array.new_fixed",
250+
instrargs = Seq(arrayType.toWat, doc"${items.length}"),
251+
stackargs = items,
252+
resultType = S(RefType(arrayType, nullable = false))
253+
)
254+
255+
/** Creates an `array.get` instruction. */
256+
def get(arrayType: TypeIdx, arrayRef: Expr, index: Expr, elemType: Type): FoldedInstr = FoldedInstr(
257+
mnemonic = "array.get",
258+
instrargs = Seq(arrayType.toWat),
259+
stackargs = Seq(arrayRef, index),
260+
resultType = S(elemType)
261+
)
262+
263+
/** Creates an `array.set` instruction. */
264+
def set(arrayType: TypeIdx, arrayRef: Expr, index: Expr, value: Expr): FoldedInstr = FoldedInstr(
265+
mnemonic = "array.set",
266+
instrargs = Seq(arrayType.toWat),
267+
stackargs = Seq(arrayRef, index, value),
268+
resultType = N
269+
)
246270
end array
247271

248272
object ref:
@@ -300,6 +324,14 @@ object Instructions:
300324
resultType = S(ty)
301325
)
302326

327+
/** Creates a `local.tee` instruction. */
328+
def tee(index: LocalIdx, value: Expr): FoldedInstr = FoldedInstr(
329+
mnemonic = "local.tee",
330+
instrargs = Seq(index),
331+
stackargs = Seq(value),
332+
resultTypes = value.resultTypes
333+
)
334+
303335
/** Creates a `local.set` instruction. */
304336
def set(index: LocalIdx, value: Expr): FoldedInstr = FoldedInstr(
305337
mnemonic = "local.set",

hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/Wasm.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,19 @@ case class StructType(
135135
def toWat: Document =
136136
doc"(struct${fieldSeq.map(_.toWat).mkDocument(doc" ").surroundUnlessEmpty(doc" ")})"
137137

138+
/** A type representing an array type. */
139+
case class ArrayType(
140+
elemType: Type,
141+
mutable: Bool,
142+
) extends ToWat:
143+
private def elemDoc: Document =
144+
if mutable then doc"(mut ${elemType.toWat})" else elemType.toWat
145+
146+
def toWat: Document =
147+
doc"(array ${elemDoc})"
148+
138149
/** A composite type. */
139-
type CompType = StructType | FunctionType
150+
type CompType = StructType | FunctionType | ArrayType
140151

141152
type AbsHeapType =
142153
HeapType.Func.type

0 commit comments

Comments
 (0)