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 3 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 @@ -560,6 +560,7 @@ typedef struct WasmCFunctionWriter {
bool pretty;
bool debug;
bool linkImports;
bool writeAlignment;
WasmDebugLines* debugLines;
} WasmCFunctionWriter;

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

Expand Down Expand Up @@ -3490,7 +3502,8 @@ wasmCWriteFunctionImplementations(
debugLines,
pretty,
debug,
linkImports
linkImports,
writeAlignment
))
fputs("\n", file);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -4773,7 +4787,8 @@ wasmCWriteImplementationFile(
endFunctionIndex,
pretty,
debug,
linkImports
linkImports,
writeAlignment
))
}

Expand Down Expand Up @@ -4804,6 +4819,7 @@ typedef struct WasmCImplementationWriterTask {
bool pretty;
bool debug;
bool linkImports;
bool writeAlignment;
bool result;
} WasmCImplementationWriterTask;

Expand Down Expand Up @@ -4874,6 +4890,7 @@ wasmCImplementationWriterThread(
bool pretty = task->pretty;
bool debug = task->debug;
bool linkImports = task->linkImports;
bool writeAlignment = task->writeAlignment;

writer->task = NULL;

Expand All @@ -4891,7 +4908,8 @@ wasmCImplementationWriterThread(
startFunctionIndex,
pretty,
debug,
linkImports
linkImports,
writeAlignment
);
if (!result) {
fprintf(
Expand Down Expand Up @@ -4942,7 +4960,8 @@ wasmCWriteModuleImplementationFiles(
0,
options.pretty,
options.debug,
options.linkImports
options.linkImports,
options.writeAlignment
))

{
Expand All @@ -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(
Expand Down Expand Up @@ -5012,7 +5032,8 @@ wasmCWriteModuleImplementationFiles(
startFunctionIndex,
options.pretty,
options.debug,
options.linkImports
options.linkImports,
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 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
Expand Down
3 changes: 2 additions & 1 deletion w2c2/instruction.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "instruction.h"
#include <stdio.h>
Heath123 marked this conversation as resolved.
Show resolved Hide resolved

/* WasmLocalInstruction */

Expand Down Expand Up @@ -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;
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: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
Expand Down Expand Up @@ -94,6 +94,7 @@ main(
bool pretty = false;
bool debug = false;
bool linkImports = false;
bool writeAlignment = false;
WasmDataSegmentMode dataSegmentMode = wasmDataSegmentModeArrays;
char moduleName[PATH_MAX];

Expand Down Expand Up @@ -126,6 +127,10 @@ main(
linkImports = 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"
" -l Link against imported functions rather than resolving them at runtime\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.linkImports = linkImports;
writeOptions.writeAlignment = writeAlignment;
writeOptions.dataSegmentMode = dataSegmentMode;

if (!wasmCWriteModule(reader.module, moduleName, writeOptions)) {
Expand Down