From 0f04ecfe1a38c8c7277af7bcdbb0fc81ac7c97a1 Mon Sep 17 00:00:00 2001 From: Heath123 Date: Thu, 30 Mar 2023 12:52:20 +0100 Subject: [PATCH 1/4] Option to pass alignment hints to the load and store macros --- w2c2/c.c | 34 +++++++++++++++++++++++++++------- w2c2/c.h | 3 ++- w2c2/instruction.c | 3 ++- w2c2/main.c | 11 +++++++++-- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/w2c2/c.c b/w2c2/c.c index 13858bbb..b7c87438 100644 --- a/w2c2/c.c +++ b/w2c2/c.c @@ -560,6 +560,7 @@ typedef struct WasmCFunctionWriter { bool pretty; bool debug; bool linkImports; + bool writeAlignment; WasmDebugLines* debugLines; } WasmCFunctionWriter; @@ -1232,6 +1233,10 @@ wasmCWriteLoadExpr( MUST (stringBuilderAppendU32(writer->builder, instruction.offset)) MUST (wasmCWrite(writer, "U")) } + if (writer->writeAlignment) { + MUST (wasmCWriteComma(writer)) + MUST (stringBuilderAppendU32(writer->builder, instruction.align)) + } MUST (wasmCWrite(writer, ");\n")) wasmTypeStackDrop(writer->typeStack, 1); @@ -1332,6 +1337,10 @@ wasmCWriteStoreExpr( stackIndex0, writer->typeStack->valueTypes[stackIndex0] )) + if (writer->writeAlignment) { + MUST (wasmCWriteComma(writer)) + MUST (stringBuilderAppendU32(writer->builder, instruction.align)) + } MUST (wasmCWrite(writer, ");\n")) wasmTypeStackDrop(writer->typeStack, 2); @@ -3278,7 +3287,8 @@ wasmCWriteFunctionBody( WasmDebugLines* debugLines, bool pretty, bool debug, - bool linkImports + bool linkImports, + bool writeAlignment ) { Buffer code = function.code; StringBuilder stringBuilder = emptyStringBuilder; @@ -3317,6 +3327,7 @@ wasmCWriteFunctionBody( writer.pretty = pretty; writer.debug = debug; writer.linkImports = linkImports; + writer.writeAlignment = writeAlignment; writer.debugLines = debugLines; MUST (wasmLabelStackPush(writer.labelStack, 0, resultType, &label)) @@ -3446,7 +3457,8 @@ wasmCWriteFunctionImplementations( U32 endIndex, bool pretty, bool debug, - bool linkImports + bool linkImports, + bool writeAlignment ) { const size_t functionImportCount = module->functionImports.length; @@ -3490,7 +3502,8 @@ wasmCWriteFunctionImplementations( debugLines, pretty, debug, - linkImports + linkImports, + writeAlignment )) fputs("\n", file); } @@ -4732,7 +4745,8 @@ wasmCWriteImplementationFile( U32 startFunctionIndex, bool pretty, bool debug, - bool linkImports + bool linkImports, + bool writeAlignment ) { char filename[13]; U32 functionCount = module->functions.count; @@ -4773,7 +4787,8 @@ wasmCWriteImplementationFile( endFunctionIndex, pretty, debug, - linkImports + linkImports, + writeAlignment )) } @@ -4804,6 +4819,7 @@ typedef struct WasmCImplementationWriterTask { bool pretty; bool debug; bool linkImports; + bool writeAlignment; bool result; } WasmCImplementationWriterTask; @@ -4874,6 +4890,7 @@ wasmCImplementationWriterThread( bool pretty = task->pretty; bool debug = task->debug; bool linkImports = task->linkImports; + bool writeAlignment = task->writeAlignment; writer->task = NULL; @@ -4891,7 +4908,8 @@ wasmCImplementationWriterThread( startFunctionIndex, pretty, debug, - linkImports + linkImports, + writeAlignment ); if (!result) { fprintf( @@ -4942,7 +4960,8 @@ wasmCWriteModuleImplementationFiles( 0, options.pretty, options.debug, - options.linkImports + options.linkImports, + options.writeAlignment )) { @@ -4961,6 +4980,7 @@ wasmCWriteModuleImplementationFiles( task.pretty = options.pretty; task.debug = options.debug; task.linkImports = options.linkImports; + task.writeAlignment = options.writeAlignment; for (; jobIndex < threadCount; jobIndex++) { int err = pthread_create( diff --git a/w2c2/c.h b/w2c2/c.h index 1b700166..b57ab175 100644 --- a/w2c2/c.h +++ b/w2c2/c.h @@ -11,11 +11,12 @@ typedef struct WasmCWriteModuleOptions { bool pretty; bool debug; bool linkImports; + bool writeAlignment; WasmDataSegmentMode dataSegmentMode; } WasmCWriteModuleOptions; static const WasmCWriteModuleOptions emptyWasmCWriteModuleOptions ={ - NULL, 0, 0, false, false, false, wasmDataSegmentModeArrays + NULL, 0, 0, false, false, false, false, wasmDataSegmentModeArrays }; bool diff --git a/w2c2/instruction.c b/w2c2/instruction.c index 53af6aa0..7858a9a4 100644 --- a/w2c2/instruction.c +++ b/w2c2/instruction.c @@ -1,4 +1,5 @@ #include "instruction.h" +#include /* WasmLocalInstruction */ @@ -72,7 +73,7 @@ wasmLoadStoreInstructionRead( MUST (leb128ReadU32(buffer, &offset) > 0) result->opcode = opcode; - result->align = align; + result->align = 1 << align; result->offset = offset; return true; diff --git a/w2c2/main.c b/w2c2/main.c index af7078ab..1d84adb1 100644 --- a/w2c2/main.c +++ b/w2c2/main.c @@ -22,9 +22,9 @@ #include "stringbuilder.h" #if HAS_PTHREAD -static char* const optString = "t:f:d:pglh"; +static char* const optString = "t:f:d:pglah"; #else -static char* const optString = "f:d:pglh"; +static char* const optString = "f:d:pglah"; #endif /* HAS_PTHREAD */ static @@ -94,6 +94,7 @@ main( bool pretty = false; bool debug = false; bool linkImports = false; + bool writeAlignment = false; WasmDataSegmentMode dataSegmentMode = wasmDataSegmentModeArrays; char moduleName[PATH_MAX]; @@ -126,6 +127,10 @@ main( linkImports = true; break; } + case 'a': { + writeAlignment = true; + break; + } case 'd': { if (strcmp(optarg, "arrays") == 0) { dataSegmentMode = wasmDataSegmentModeArrays; @@ -180,6 +185,7 @@ main( " -g Generate debug information (function names using asm(); #line directives based on DWARF, if available)\n" " -p Generate pretty code\n" " -l Link against imported functions rather than resolving them at runtime\n" + " -a Pass alsignment hints to the load and store macros. You'll need a custom w2c2_base.h to use this\n" ); return 0; } @@ -259,6 +265,7 @@ main( writeOptions.pretty = pretty; writeOptions.debug = debug; writeOptions.linkImports = linkImports; + writeOptions.writeAlignment = writeAlignment; writeOptions.dataSegmentMode = dataSegmentMode; if (!wasmCWriteModule(reader.module, moduleName, writeOptions)) { From 5c062e9619549a5557838b698d7e3822fb097dee Mon Sep 17 00:00:00 2001 From: Heath123 Date: Thu, 30 Mar 2023 12:57:20 +0100 Subject: [PATCH 2/4] Typo fix --- w2c2/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/w2c2/main.c b/w2c2/main.c index 1d84adb1..4ab10251 100644 --- a/w2c2/main.c +++ b/w2c2/main.c @@ -185,7 +185,7 @@ main( " -g Generate debug information (function names using asm(); #line directives based on DWARF, if available)\n" " -p Generate pretty code\n" " -l Link against imported functions rather than resolving them at runtime\n" - " -a Pass alsignment hints to the load and store macros. You'll need a custom w2c2_base.h to use this\n" + " -a Pass alignment hints to the load and store macros. You'll need a custom w2c2_base.h to use this\n" ); return 0; } From 3830ecdc77799407288d90b5e992e2b4c296c92c Mon Sep 17 00:00:00 2001 From: Heath123 Date: Thu, 30 Mar 2023 13:00:32 +0100 Subject: [PATCH 3/4] Fix missing argument --- w2c2/c.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/w2c2/c.c b/w2c2/c.c index b7c87438..b50cd254 100644 --- a/w2c2/c.c +++ b/w2c2/c.c @@ -5032,7 +5032,8 @@ wasmCWriteModuleImplementationFiles( startFunctionIndex, options.pretty, options.debug, - options.linkImports + options.linkImports, + options.writeAlignment )) #endif /* HAS_PTHREAD */ } From 970aa9d39f4e7897ecc9f1e06498a0f6c01355b9 Mon Sep 17 00:00:00 2001 From: circuit10 Date: Thu, 6 Apr 2023 12:18:49 +0100 Subject: [PATCH 4/4] Remove unneeded include MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Müller --- w2c2/instruction.c | 1 - 1 file changed, 1 deletion(-) diff --git a/w2c2/instruction.c b/w2c2/instruction.c index 7858a9a4..85a8ba56 100644 --- a/w2c2/instruction.c +++ b/w2c2/instruction.c @@ -1,5 +1,4 @@ #include "instruction.h" -#include /* WasmLocalInstruction */