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

Simplify instructions #89

Merged
merged 1 commit into from
Nov 12, 2023
Merged
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
58 changes: 24 additions & 34 deletions w2c2/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,8 @@ WARN_UNUSED_RESULT
wasmCWriteLocalGetExpr(
const WasmCFunctionWriter* writer
) {
static const WasmOpcode opcode = wasmOpcodeLocalGet;

WasmLocalInstruction instruction;
if (!wasmLocalInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmLocalInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid local.get instruction encoding\n");
return false;
}
Expand Down Expand Up @@ -876,7 +874,7 @@ wasmCWriteLocalAssignmentExpr(
const WasmOpcode opcode
) {
WasmLocalInstruction instruction;
if (!wasmLocalInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmLocalInstructionRead(writer->code, &instruction)) {
fprintf(
stderr,
"w2c2: invalid %s instruction encoding\n",
Expand Down Expand Up @@ -930,7 +928,7 @@ wasmCWriteGlobalGetExpr(
static const WasmOpcode opcode = wasmOpcodeGlobalGet;

WasmGlobalInstruction instruction;
if (!wasmGlobalInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmGlobalInstructionRead(writer->code, &instruction)) {
fprintf(
stderr,
"w2c2: invalid %s instruction encoding\n",
Expand Down Expand Up @@ -980,7 +978,7 @@ wasmCWriteGlobalSetExpr(
static const WasmOpcode opcode = wasmOpcodeGlobalSet;

WasmGlobalInstruction instruction;
if (!wasmGlobalInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmGlobalInstructionRead(writer->code, &instruction)) {
fprintf(
stderr,
"w2c2: invalid %s instruction encoding\n",
Expand Down Expand Up @@ -1131,7 +1129,7 @@ wasmCWriteLoadExpr(
) {
WasmLoadStoreInstruction instruction;

if (!wasmLoadStoreInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmLoadStoreInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid load instruction encoding\n");
return false;
}
Expand Down Expand Up @@ -1260,7 +1258,7 @@ wasmCWriteStoreExpr(
const WasmOpcode opcode
) {
WasmLoadStoreInstruction instruction;
if (!wasmLoadStoreInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmLoadStoreInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid store instruction encoding\n");
return false;
}
Expand Down Expand Up @@ -1356,10 +1354,8 @@ WARN_UNUSED_RESULT
wasmCWriteMemorySize(
const WasmCFunctionWriter* writer
) {
static const WasmOpcode opcode = wasmOpcodeMemorySize;

WasmMemoryInstruction instruction;
if (!wasmMemoryInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmMemoryInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid memory.size instruction encoding\n");
return false;
}
Expand Down Expand Up @@ -1411,10 +1407,8 @@ WARN_UNUSED_RESULT
wasmCWriteMemoryGrow(
const WasmCFunctionWriter* writer
) {
static const WasmOpcode opcode = wasmOpcodeMemoryGrow;

WasmMemoryInstruction instruction;
if (!wasmMemoryInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmMemoryInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid memory.grow instruction encoding\n");
return false;
}
Expand Down Expand Up @@ -1551,8 +1545,8 @@ wasmCWriteMemoryFill(
const WasmCFunctionWriter* writer,
const WasmMiscOpcode miscOpcode
) {
WasmMiscMemoryInstruction instruction;
if (!wasmMiscMemoryInstructionRead(writer->code, miscOpcode, &instruction)) {
WasmMemoryInstruction instruction;
if (!wasmMemoryInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid memory.fill instruction encoding\n");
return false;
}
Expand Down Expand Up @@ -2286,10 +2280,8 @@ WARN_UNUSED_RESULT
wasmCWriteBranchExpr(
const WasmCFunctionWriter* writer
) {
static const WasmOpcode opcode = wasmOpcodeBr;

WasmBranchInstruction instruction;
if (!wasmBranchInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmBranchInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid br instruction encoding\n");
return false;
}
Expand All @@ -2308,10 +2300,8 @@ WARN_UNUSED_RESULT
wasmCWriteBranchIfExpr(
WasmCFunctionWriter* writer
) {
static const WasmOpcode opcode = wasmOpcodeBrIf;

WasmBranchInstruction instruction;
if (!wasmBranchInstructionRead(writer->code, opcode, &instruction)) {
if (!wasmBranchInstructionRead(writer->code, &instruction)) {
fprintf(stderr, "w2c2: invalid br.if instruction encoding\n");
return false;
}
Expand Down Expand Up @@ -3614,7 +3604,7 @@ wasmCWriteConstantExpr(
}
case wasmOpcodeGlobalGet: {
WasmGlobalInstruction instruction;
MUST (wasmGlobalInstructionRead(&code, opcode, &instruction))
MUST (wasmGlobalInstructionRead(&code, &instruction))
MUST (wasmCWriteStringGlobalUse(builder, module, instruction.globalIndex, false))
break;
}
Expand Down Expand Up @@ -4860,27 +4850,27 @@ wasmCImplementationWriterThread(
}

{
WasmCImplementationWriterTask* task = writer->task;
const WasmCImplementationWriterTask* task = writer->task;

const WasmModule* module = task->module;
const char* moduleName = task->moduleName;
const char* headerName = task->headerName;
char filePrefix = task->filePrefix;
U32 fileIndex = task->fileIndex;
U32 functionsPerFile = task->functionsPerFile;
U32 startFunctionIDIndex = task->startFunctionIDIndex;
WasmFunctionIDs functionIDs = task->functionIDs;
bool pretty = task->pretty;
bool debug = task->debug;
bool multipleModules = task->multipleModules;
const char filePrefix = task->filePrefix;
const U32 fileIndex = task->fileIndex;
const U32 functionsPerFile = task->functionsPerFile;
const U32 startFunctionIDIndex = task->startFunctionIDIndex;
const WasmFunctionIDs functionIDs = task->functionIDs;
const bool pretty = task->pretty;
const bool debug = task->debug;
const bool multipleModules = task->multipleModules;
WasmDebugLines* debugLines = task->debugLines;

writer->task = NULL;

pthread_mutex_unlock(&writer->mutex);

{
bool result = wasmCWriteImplementationFile(
const bool result = wasmCWriteImplementationFile(
module,
moduleName,
headerName,
Expand All @@ -4895,7 +4885,7 @@ wasmCImplementationWriterThread(
multipleModules
);
if (!result) {
WasmFunctionID startFunctionID = functionIDs.functionIDs[startFunctionIDIndex];
const WasmFunctionID startFunctionID = functionIDs.functionIDs[startFunctionIDIndex];
fprintf(
stderr,
"w2c2: failed to write implementation file %d. start function index: %d\n",
Expand Down
30 changes: 0 additions & 30 deletions w2c2/instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
bool
wasmLocalInstructionRead(
Buffer* buffer,
const WasmOpcode opcode,
WasmLocalInstruction* result
) {
U32 localIndex = 0;
MUST (leb128ReadU32(buffer, &localIndex) > 0)

result->opcode = opcode;
result->localIndex = localIndex;

return true;
Expand All @@ -22,13 +20,11 @@ wasmLocalInstructionRead(
bool
wasmGlobalInstructionRead(
Buffer* buffer,
const WasmOpcode opcode,
WasmGlobalInstruction* result
) {
U32 globalIndex = 0;
MUST (leb128ReadU32(buffer, &globalIndex) > 0)

result->opcode = opcode;
result->globalIndex = globalIndex;

return true;
Expand All @@ -42,7 +38,6 @@ wasmConstInstructionRead(
const WasmOpcode opcode,
WasmConstInstruction* result
) {
result->opcode = opcode;
switch (opcode) {
case wasmOpcodeI32Const:
return leb128ReadI32(buffer, &result->value.i32) > 0;
Expand All @@ -62,7 +57,6 @@ wasmConstInstructionRead(
bool
wasmLoadStoreInstructionRead(
Buffer* buffer,
const WasmOpcode opcode,
WasmLoadStoreInstruction* result
) {
U32 align = 0;
Expand All @@ -71,7 +65,6 @@ wasmLoadStoreInstructionRead(
MUST (leb128ReadU32(buffer, &align) > 0)
MUST (leb128ReadU32(buffer, &offset) > 0)

result->opcode = opcode;
result->align = align;
result->offset = offset;

Expand Down Expand Up @@ -117,13 +110,11 @@ wasmCallIndirectInstructionRead(
bool
wasmBranchInstructionRead(
Buffer* buffer,
const WasmOpcode opcode,
WasmBranchInstruction* result
) {
U32 labelIndex = 0;
MUST(leb128ReadU32(buffer, &labelIndex) > 0)

result->opcode = opcode;
result->labelIndex = labelIndex;

return true;
Expand Down Expand Up @@ -171,31 +162,11 @@ wasmBranchTableInstructionFree(
bool
wasmMemoryInstructionRead(
Buffer* buffer,
const WasmOpcode opcode,
WasmMemoryInstruction* result
) {
U32 memoryIndex = 0;
MUST(leb128ReadU32(buffer, &memoryIndex) > 0)

result->opcode = opcode;
result->memoryIndex = memoryIndex;

return true;
}

/* WasmMiscMemoryInstruction */

bool
WARN_UNUSED_RESULT
wasmMiscMemoryInstructionRead(
Buffer* buffer,
const WasmMiscOpcode opcode,
WasmMiscMemoryInstruction* result
) {
U32 memoryIndex = 0;
MUST(leb128ReadU32(buffer, &memoryIndex) > 0)

result->opcode = opcode;
result->memoryIndex = memoryIndex;

return true;
Expand All @@ -219,4 +190,3 @@ wasmMemoryCopyInstructionRead(

return true;
}

26 changes: 0 additions & 26 deletions w2c2/instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,26 @@
/* WasmLocalInstruction */

typedef struct WasmLocalInstruction {
WasmOpcode opcode;
U32 localIndex;
} WasmLocalInstruction;

bool
WARN_UNUSED_RESULT
wasmLocalInstructionRead(
Buffer* buffer,
WasmOpcode opcode,
WasmLocalInstruction* result
);

/* WasmGlobalInstruction */

typedef struct WasmGlobalInstruction {
WasmOpcode opcode;
U32 globalIndex;
} WasmGlobalInstruction;

bool
WARN_UNUSED_RESULT
wasmGlobalInstructionRead(
Buffer* buffer,
WasmOpcode opcode,
WasmGlobalInstruction* result
);

Expand All @@ -45,7 +41,6 @@ typedef union WasmValue {
} WasmValue;

typedef struct WasmConstInstruction {
WasmOpcode opcode;
WasmValue value;
} WasmConstInstruction;

Expand All @@ -60,7 +55,6 @@ wasmConstInstructionRead(
/* WasmLoadStoreInstruction */

typedef struct WasmLoadStoreInstruction {
WasmOpcode opcode;
U32 align;
U32 offset;
} WasmLoadStoreInstruction;
Expand All @@ -69,7 +63,6 @@ bool
WARN_UNUSED_RESULT
wasmLoadStoreInstructionRead(
Buffer* buffer,
WasmOpcode opcode,
WasmLoadStoreInstruction* result
);

Expand Down Expand Up @@ -103,15 +96,13 @@ wasmCallIndirectInstructionRead(
/* WasmBranchInstruction */

typedef struct WasmBranchInstruction {
WasmOpcode opcode;
U32 labelIndex;
} WasmBranchInstruction;

bool
WARN_UNUSED_RESULT
wasmBranchInstructionRead(
Buffer* buffer,
WasmOpcode opcode,
WasmBranchInstruction* result
);

Expand All @@ -138,33 +129,16 @@ wasmBranchTableInstructionRead(
/* WasmMemoryInstruction */

typedef struct WasmMemoryInstruction {
WasmOpcode opcode;
U32 memoryIndex;
} WasmMemoryInstruction;

bool
WARN_UNUSED_RESULT
wasmMemoryInstructionRead(
Buffer* buffer,
WasmOpcode opcode,
WasmMemoryInstruction* result
);

/* WasmMiscMemoryInstruction */

typedef struct WasmMiscMemoryInstruction {
WasmMiscOpcode opcode;
U32 memoryIndex;
} WasmMiscMemoryInstruction;

bool
WARN_UNUSED_RESULT
wasmMiscMemoryInstructionRead(
Buffer* buffer,
WasmMiscOpcode opcode,
WasmMiscMemoryInstruction* result
);

/* WasmMemoryCopyInstruction */

typedef struct WasmMemoryCopyInstruction {
Expand Down
2 changes: 1 addition & 1 deletion w2c2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ cleanImplementationFiles(void) {
#if HAS_GLOB
glob_t globbuf;
size_t pathIndex = 0;
int globResult = glob("*.c", GLOB_NOSORT, NULL, &globbuf);
const int globResult = glob("*.c", GLOB_NOSORT, NULL, &globbuf);
if (globResult != 0) {
if (globResult != GLOB_NOMATCH) {
fprintf(stderr, "w2c2: failed to glob files to clean\n");
Expand Down
2 changes: 1 addition & 1 deletion w2c2/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ wasmReadConstantExpr(
}
case wasmOpcodeGlobalGet: {
WasmGlobalInstruction instruction;
MUST (wasmGlobalInstructionRead(buffer, opcode, &instruction))
MUST (wasmGlobalInstructionRead(buffer, &instruction))
break;
}
case wasmOpcodeEnd:
Expand Down