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

Fix non-array data segment initialization #97

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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
75 changes: 39 additions & 36 deletions w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -5117,14 +5117,9 @@ wasmCWriteDataSegments(

{
U32 dataSegmentIndex = 0;
U64 byteOffset = 0;
for (; dataSegmentIndex < dataSegmentCount; dataSegmentIndex++) {
const WasmDataSegment dataSegment = module->dataSegments.dataSegments[dataSegmentIndex];

if (!dataSegment.bytes.length) {
continue;
}

switch (mode) {
case wasmDataSegmentModeArrays: {
fputs("const U8 ", file);
Expand Down Expand Up @@ -5155,15 +5150,10 @@ wasmCWriteDataSegments(
case wasmDataSegmentModeGNULD:
case wasmDataSegmentModeSectcreate1:
case wasmDataSegmentModeSectcreate2: {
fputs("const U8* ", file);
fputs("U8* ", file);
/* TODO: add support for multiple modules */
wasmCWriteFileDataSegmentName(file, dataSegmentIndex);
if (pretty) {
fprintf(file, " = ds + %llu", byteOffset);
} else {
fprintf(file, "=ds+%llu", byteOffset);
}
byteOffset += dataSegment.bytes.length;
fputs(";\n", file);
break;
}
default: {
Expand Down Expand Up @@ -5386,39 +5376,52 @@ wasmCWriteInitMemories(
{
const U32 dataSegmentCount = module->dataSegments.count;
U32 dataSegmentIndex = 0;
U64 byteOffset = 0;
for (; dataSegmentIndex < dataSegmentCount; dataSegmentIndex++) {
const WasmDataSegment dataSegment = module->dataSegments.dataSegments[dataSegmentIndex];
const size_t dataSegmentLength = dataSegment.bytes.length;
const Buffer code = dataSegment.offset;

if (!dataSegmentLength) {
continue;
switch (dataSegmentMode) {
case wasmDataSegmentModeGNULD:
case wasmDataSegmentModeSectcreate1:
case wasmDataSegmentModeSectcreate2: {
/* TODO: add support for multiple modules */
wasmCWriteFileDataSegmentName(file, dataSegmentIndex);
if (pretty) {
fprintf(file, " = ds + %llu", byteOffset);
} else {
fprintf(file, "=ds+%llu", byteOffset);
}
fputs(";\n", file);
break;
}
}

/* Skip passive segments */
if (code.data == NULL) {
continue;
/* Load active segments */
if (code.data != NULL) {
if (pretty) {
fputs(indentation, file);
}
fputs("LOAD_DATA(", file);
wasmCWriteFileMemoryUse(
file,
module,
dataSegment.memoryIndex,
NULL,
false
);
fputs(", ", file);
MUST (stringBuilderReset(&stringBuilder))
MUST (wasmCWriteConstantExpr(&stringBuilder, module, code))
fputs(stringBuilder.string, file);
fputs(", ", file);
/* TODO: add support for multiple modules */
wasmCWriteFileDataSegmentName(file, dataSegmentIndex);
fprintf(file, ", %lu);\n", (unsigned long) dataSegmentLength);
}

if (pretty) {
fputs(indentation, file);
}
fputs("LOAD_DATA(", file);
wasmCWriteFileMemoryUse(
file,
module,
dataSegment.memoryIndex,
NULL,
false
);
fputs(", ", file);
MUST (stringBuilderReset(&stringBuilder))
MUST (wasmCWriteConstantExpr(&stringBuilder, module, code))
fputs(stringBuilder.string, file);
fputs(", ", file);
/* TODO: add support for multiple modules */
wasmCWriteFileDataSegmentName(file, dataSegmentIndex);
fprintf(file, ", %lu);\n", (unsigned long) dataSegmentLength);
byteOffset += dataSegment.bytes.length;
}
}

Expand Down
Loading