Skip to content

Commit

Permalink
fix non-array data segment initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Mar 25, 2024
1 parent bd75c9b commit 2a3275b
Showing 1 changed file with 39 additions and 36 deletions.
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

0 comments on commit 2a3275b

Please sign in to comment.