diff --git a/w2c2/c.c b/w2c2/c.c index 788c8609..af702912 100644 --- a/w2c2/c.c +++ b/w2c2/c.c @@ -553,6 +553,7 @@ typedef struct WasmCFunctionWriter { bool pretty; bool debug; bool multipleModules; + bool writeAlignment; WasmDebugLines* debugLines; } WasmCFunctionWriter; @@ -1225,6 +1226,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); @@ -1325,6 +1330,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); @@ -3271,7 +3280,8 @@ wasmCWriteFunctionBody( WasmDebugLines* debugLines, bool pretty, bool debug, - bool multipleModules + bool multipleModules, + bool writeAlignment ) { Buffer code = function.code; StringBuilder stringBuilder = emptyStringBuilder; @@ -3310,6 +3320,7 @@ wasmCWriteFunctionBody( writer.pretty = pretty; writer.debug = debug; writer.multipleModules = multipleModules; + 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 multipleModules + bool multipleModules, + bool writeAlignment ) { const size_t functionImportCount = module->functionImports.length; @@ -3491,7 +3503,8 @@ wasmCWriteFunctionImplementations( debugLines, pretty, debug, - multipleModules + multipleModules, + writeAlignment )) fputs("\n", file); } @@ -4712,7 +4725,8 @@ wasmCWriteImplementationFile( U32 startFunctionIndex, bool pretty, bool debug, - bool multipleModules + bool multipleModules, + bool writeAlignment ) { char filename[13]; U32 functionCount = module->functions.count; @@ -4753,7 +4767,8 @@ wasmCWriteImplementationFile( endFunctionIndex, pretty, debug, - multipleModules + multipleModules, + writeAlignment )) } @@ -4784,6 +4799,7 @@ typedef struct WasmCImplementationWriterTask { bool pretty; bool debug; bool multipleModules; + bool writeAlignment; bool result; } WasmCImplementationWriterTask; @@ -4854,6 +4870,7 @@ wasmCImplementationWriterThread( bool pretty = task->pretty; bool debug = task->debug; bool multipleModules = task->multipleModules; + bool writeAlignment = task->writeAlignment; writer->task = NULL; @@ -4871,7 +4888,8 @@ wasmCImplementationWriterThread( startFunctionIndex, pretty, debug, - multipleModules + multipleModules, + writeAlignment ); if (!result) { fprintf( @@ -4922,7 +4940,8 @@ wasmCWriteModuleImplementationFiles( 0, options.pretty, options.debug, - options.multipleModules + options.multipleModules, + options.writeAlignment )) { @@ -4941,6 +4960,7 @@ wasmCWriteModuleImplementationFiles( task.pretty = options.pretty; task.debug = options.debug; task.multipleModules = options.multipleModules; + task.writeAlignment = options.writeAlignment; for (; jobIndex < threadCount; jobIndex++) { int err = pthread_create( @@ -4992,7 +5012,8 @@ wasmCWriteModuleImplementationFiles( startFunctionIndex, options.pretty, options.debug, - options.multipleModules + options.multipleModules, + options.writeAlignment )) #endif /* HAS_PTHREAD */ } diff --git a/w2c2/c.h b/w2c2/c.h index 22f4e3b8..2ce70a00 100644 --- a/w2c2/c.h +++ b/w2c2/c.h @@ -11,11 +11,12 @@ typedef struct WasmCWriteModuleOptions { bool pretty; bool debug; bool multipleModules; + 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..85a8ba56 100644 --- a/w2c2/instruction.c +++ b/w2c2/instruction.c @@ -72,7 +72,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 c58377a4..8ee9cfcc 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:pgmh"; +static char* const optString = "t:f:d:pgmah"; #else -static char* const optString = "f:d:pgmh"; +static char* const optString = "f:d:pgmah"; #endif /* HAS_PTHREAD */ static @@ -94,6 +94,7 @@ main( bool pretty = false; bool debug = false; bool multipleModules = false; + bool writeAlignment = false; WasmDataSegmentMode dataSegmentMode = wasmDataSegmentModeArrays; char moduleName[PATH_MAX]; @@ -126,6 +127,10 @@ main( multipleModules = 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" " -m Support multiple modules (prefixes function names)\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; } @@ -259,6 +265,7 @@ main( writeOptions.pretty = pretty; writeOptions.debug = debug; writeOptions.multipleModules = multipleModules; + writeOptions.writeAlignment = writeAlignment; writeOptions.dataSegmentMode = dataSegmentMode; if (!wasmCWriteModule(reader.module, moduleName, writeOptions)) {