Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to pass alignment hints to the load and store macros #67

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ typedef struct WasmCFunctionWriter {
bool pretty;
bool debug;
bool multipleModules;
bool writeAlignment;
WasmDebugLines* debugLines;
} WasmCFunctionWriter;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -3271,7 +3280,8 @@ wasmCWriteFunctionBody(
WasmDebugLines* debugLines,
bool pretty,
bool debug,
bool multipleModules
bool multipleModules,
bool writeAlignment
) {
Buffer code = function.code;
StringBuilder stringBuilder = emptyStringBuilder;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -3446,7 +3457,8 @@ wasmCWriteFunctionImplementations(
U32 endIndex,
bool pretty,
bool debug,
bool multipleModules
bool multipleModules,
bool writeAlignment
) {
const size_t functionImportCount = module->functionImports.length;

Expand Down Expand Up @@ -3491,7 +3503,8 @@ wasmCWriteFunctionImplementations(
debugLines,
pretty,
debug,
multipleModules
multipleModules,
writeAlignment
))
fputs("\n", file);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -4753,7 +4767,8 @@ wasmCWriteImplementationFile(
endFunctionIndex,
pretty,
debug,
multipleModules
multipleModules,
writeAlignment
))
}

Expand Down Expand Up @@ -4784,6 +4799,7 @@ typedef struct WasmCImplementationWriterTask {
bool pretty;
bool debug;
bool multipleModules;
bool writeAlignment;
bool result;
} WasmCImplementationWriterTask;

Expand Down Expand Up @@ -4854,6 +4870,7 @@ wasmCImplementationWriterThread(
bool pretty = task->pretty;
bool debug = task->debug;
bool multipleModules = task->multipleModules;
bool writeAlignment = task->writeAlignment;

writer->task = NULL;

Expand All @@ -4871,7 +4888,8 @@ wasmCImplementationWriterThread(
startFunctionIndex,
pretty,
debug,
multipleModules
multipleModules,
writeAlignment
);
if (!result) {
fprintf(
Expand Down Expand Up @@ -4922,7 +4940,8 @@ wasmCWriteModuleImplementationFiles(
0,
options.pretty,
options.debug,
options.multipleModules
options.multipleModules,
options.writeAlignment
))

{
Expand All @@ -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(
Expand Down Expand Up @@ -4992,7 +5012,8 @@ wasmCWriteModuleImplementationFiles(
startFunctionIndex,
options.pretty,
options.debug,
options.multipleModules
options.multipleModules,
options.writeAlignment
))
#endif /* HAS_PTHREAD */
}
Expand Down
3 changes: 2 additions & 1 deletion w2c2/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion w2c2/instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions w2c2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -94,6 +94,7 @@ main(
bool pretty = false;
bool debug = false;
bool multipleModules = false;
bool writeAlignment = false;
WasmDataSegmentMode dataSegmentMode = wasmDataSegmentModeArrays;
char moduleName[PATH_MAX];

Expand Down Expand Up @@ -126,6 +127,10 @@ main(
multipleModules = true;
break;
}
case 'a': {
writeAlignment = true;
break;
}
case 'd': {
if (strcmp(optarg, "arrays") == 0) {
dataSegmentMode = wasmDataSegmentModeArrays;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down