Skip to content

Commit

Permalink
Merge pull request #106 from turbolent/break-data-segment-arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Oct 14, 2024
2 parents 8c0e9dd + a03b8aa commit 3b37454
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Working towards [WebAssembly as the Elusive Universal Binary](https://kripken.gi
- [Sign-extension operators](https://github.com/WebAssembly/sign-extension-ops/blob/master/proposals/sign-extension-ops/Overview.md)
- [Non-trapping float-to-int conversions](https://github.com/WebAssembly/nontrapping-float-to-int-conversions/blob/main/proposals/nontrapping-float-to-int-conversion/Overview.md)
- Passes 99.9% of the WebAssembly core semantics test suite
- Written in C89 and generates C89
- Written in (mostly) C89 and generates (mostly) C89 (some common extensions are used and not all limits are followed, see e.g. #98)
- Support for many operating systems (e.g. Mac OS X, Mac OS 9, Haiku, Rhapsody, OPENSTEP, NeXTSTEP, DOS, Windows XP, etc.)
- Support for many architectures (e.g. x86, ARM, PowerPC, SPARC, PA-RISC, etc.)
- Support for big-endian systems (e.g. PowerPC, SPARC, PA-RISC, etc.)
Expand Down
42 changes: 32 additions & 10 deletions w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -5105,6 +5105,8 @@ wasmCWriteExports(
}
}

#define DATA_SEGMENT_CHUNK_LENGTH 18

/* TODO: add support for multiple modules */
static
void
Expand All @@ -5121,30 +5123,50 @@ wasmCWriteDataSegments(
U32 dataSegmentIndex = 0;
for (; dataSegmentIndex < dataSegmentCount; dataSegmentIndex++) {
const WasmDataSegment dataSegment = module->dataSegments.dataSegments[dataSegmentIndex];
const size_t byteCount = dataSegment.bytes.length;

fputs("const U8 ", file);
/* TODO: add support for multiple modules */
wasmCWriteFileDataSegmentName(file, dataSegmentIndex);
if (pretty) {
fputs("[] = {\n", file);
fputs("[] = {", file);
} else {
fputs("[]={\n", file);
fputs("[]={", file);
}
if (pretty) {
fputs(indentation, file);
if (byteCount > DATA_SEGMENT_CHUNK_LENGTH) {
fputc('\n', file);
if (pretty) {
fputs(indentation, file);
}
}
{
U32 byteIndex = 0;
for (; byteIndex < dataSegment.bytes.length; byteIndex++) {
fprintf(file, "0x%x", dataSegment.bytes.data[byteIndex]);
if (pretty) {
fputs(", ", file);
for (; byteIndex < byteCount; byteIndex++) {
U8 value = dataSegment.bytes.data[byteIndex];
if (byteIndex > 0) {
if (pretty) {
fputs(", ", file);
} else {
fputc(',', file);
}
if (byteIndex % DATA_SEGMENT_CHUNK_LENGTH == 0) {
fputs("\n", file);
if (pretty) {
fputs(indentation, file);
}
}
}
if (value < 10) {
fprintf(file, "%u", value);
} else {
fputc(',', file);
fprintf(file, "0x%x", value);
}
}
}
fputs("\n};\n\n", file);
if (byteCount > DATA_SEGMENT_CHUNK_LENGTH) {
fputc('\n', file);
}
fputs("};\n\n", file);
}
break;
}
Expand Down

0 comments on commit 3b37454

Please sign in to comment.