From 57a73165f59c669688686d67463a94ec1ae38146 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 28 Jun 2023 00:29:06 +0100 Subject: Use createFunction() to simplify function creation --- .../org/shedlang/compiler/backends/wasm/WasmCodeGen.java | 12 +++++------- .../compiler/backends/wasm/WasmCodeGenNamespaces.java | 16 ++++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java index 844a2347..15bf531b 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGen.java @@ -1,9 +1,9 @@ package org.shedlang.compiler.backends.wasm; import org.shedlang.compiler.backends.wasm.structure.ast.instructions.*; -import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFunction; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocalIndex; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.stackir.instructions.*; import org.shedlang.compiler.typechecker.TypeInfo; import org.shedlang.compiler.types.NamespaceName; @@ -22,13 +22,11 @@ public class WasmCodeGen { public static WasmModule generateMain(NamespaceName main, TypeInfo typeInfo) { var context = WasmCodeGenContext.fromTypeInfo(typeInfo); - context = context.addFunction(new WasmFunction( + context = context.createFunction( "_start", - List.of(), - List.of(), - List.of(), - new WasmExpression(List.of()) - )); + new WasmFuncType(List.of(), List.of()), + initContext -> initContext + ); return context.toModule(); } diff --git a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java index a1fdb0fc..a2faaacd 100644 --- a/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java +++ b/src/main/java/org/shedlang/compiler/backends/wasm/WasmCodeGenNamespaces.java @@ -7,8 +7,10 @@ import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmMemory import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmAlignment; import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmLocalIndex; +import org.shedlang.compiler.backends.wasm.structure.ast.types.WasmFuncType; import org.shedlang.compiler.types.NamespaceName; +import java.util.List; import java.util.function.Function; public class WasmCodeGenNamespaces { @@ -22,20 +24,18 @@ public class WasmCodeGenNamespaces { ) { var namespaceTypeInfo = context.typeInfo().namespaceTypeInfo(namespaceName); - var dataName = globalNameNamespaceValue(namespaceName); - context = context.addData( - dataName, + globalNameNamespaceValue(namespaceName), // TODO: remove magic number new byte[4 * namespaceTypeInfo.fieldCount()], WasmAlignment.ALIGN_32_BIT ).context(); - context = generateInitBody.apply(context); - - var namespaceInitFunction = context.toFunction(globalNameNamespaceInit(namespaceName)); - - return context.addFunction(namespaceInitFunction); + return context.createFunction( + globalNameNamespaceInit(namespaceName), + new WasmFuncType(List.of(), List.of()), + generateInitBody + ); } public static WasmCodeGenContext fieldStore( -- cgit v1.2.3