Conversation
|
The latest commit will most likely break tests because of the addition of a tracing globals map similar to those used for expressions and functions. I also figured that some form of |
kripken
left a comment
There was a problem hiding this comment.
I didn't review carefully yet, but yes, this looks like overall like a good direction.
|
|
||
| // Print an expression to stdout. Useful for debugging. | ||
| void BinaryenExpressionPrint(BinaryenExpressionRef expr); | ||
| BinaryenIndex BinaryenExpressionGetId(BinaryenExpressionRef expr); |
There was a problem hiding this comment.
should return BinaryenExpressionId?
| @@ -356,6 +390,12 @@ void BinaryenRemoveExport(BinaryenModuleRef module, const char* externalName); | |||
| // Globals | |||
|
|
|||
| BinaryenImportRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name, BinaryenType type, int8_t mutable_, BinaryenExpressionRef init); | |||
There was a problem hiding this comment.
hmm, this was wrong before, AddGlobal should return a global, not an import. Let's fix that here and in the new places it is used in this PR.
|
Not sure about setting names, though. Is it necessary to update the respective lookup maps (i.e. |
|
The latest commit also changes In general, a clean solution would also require renaming all the methods that take a |
cd5c6c5 to
ed1e52e
Compare
| BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const char* name, BinaryenType result, BinaryenType* paramTypes, BinaryenIndex numParams); | ||
| // Gets the function type at the specified index. Returns `NULL` when there are no more function types. | ||
| BinaryenFunctionTypeRef BinaryenGetFunctionTypeAt(BinaryenModuleRef module, BinaryenIndex index); | ||
| // Gets the name of the specified function. |
There was a problem hiding this comment.
comment should say function type
| BinaryenFunctionTypeRef BinaryenGetFunctionTypeAt(BinaryenModuleRef module, BinaryenIndex index); | ||
| // Gets the name of the specified function. | ||
| const char* BinaryenFunctionTypeGetName(BinaryenFunctionTypeRef ftype); | ||
| // Sets the name of the specified function. |
| // Add a new function type. This is thread-safe. | ||
| // Note: name can be NULL, in which case we auto-generate a name | ||
| BinaryenFunctionTypeRef BinaryenAddFunctionType(BinaryenModuleRef module, const char* name, BinaryenType result, BinaryenType* paramTypes, BinaryenIndex numParams); | ||
| // Gets the function type at the specified index. Returns `NULL` when there are no more function types. |
There was a problem hiding this comment.
why do you want to get function types by their index? can't you use their names?
There was a problem hiding this comment.
Idea here is to have a simple way to iterate over all function types (by calling this function with 0, 1, etc. until it returns NULL) and then being able to use the getters and setters below to evaluate and maybe modify them. Could be used, for example, when loading an existing module with the intention to unify function type names to combinations of "i", "I", "f", "F" and "v" in their debug info.
There was a problem hiding this comment.
I see. For iteration, perhaps another option is something like a JS API that returns an array of the names, and a C API underneath that does something similar (mallocs a list that must later be freed, and returns it)? Just a suggestion, I'm not sure what is best, but overall I'd like to avoid using indexes (as we have names intentionally, and also, the indexes are internal and do not necessarily match the wasm binary indexes, which could be confusing).
If you want, you can split this into smaller PRs, as we can land the obvious stuff already, but the areas where the API design isn't yet clear may take longer.
There was a problem hiding this comment.
Sure, going to split it up then - and taking the array idea into account.
| // Gets the import at the specified index from the specified module. Returns `NULL` when there are no more imports. | ||
| BinaryenImportRef BinaryenGetImportAt(BinaryenModuleRef module, BinaryenIndex index); | ||
| // Gets the internal name of the specified import. This is the name referenced by other internal elements. | ||
| const char* BinaryenImportGetInternalName(BinaryenImportRef import); |
There was a problem hiding this comment.
can use just Name, as that's the field name in the code. But the comment might mention it is the internal name.
| // Sets the internal name of the specified import. This is the name referenced by other internal elements. | ||
| void BinaryenImportSetInternalName(BinaryenImportRef import, const char* newName); | ||
| // Gets the external module name of the specified import. This is the name of the module being imported from. | ||
| const char* BinaryenImportGetExternalModuleName(BinaryenImportRef import); |
There was a problem hiding this comment.
can also remove the External from these, I think.
fddc0df to
0b39ea7
Compare
|
Closing in favor of splitting this up into multiple PRs. |
This adds some utility to the C-API to check ids (kinds) and types of expressions, here especially globals.
My use case is that I'd like to reuse the "precompute" pass so that I don't have to reimplement constant evaluation in the compiler. Hence, I add globals with complex initializers (i.e. binary expressions that are not supported by the MVP, for example a global / enum value
1 << 2) and rely on Binaryen to make these constants through precompute. But this might fail, so I need a way to determine if it worked or not (BinaryenGlobalGetInit->BinaryenExpressionGetId) to generate proper error messages.In general, there are a few places where getters and setters like these might make sense (one more example is to rename function types, i.e. if reading a binary that does not have names, here:
BinaryenGetFunctionTypeBySignature->BinaryenSetFunctionTypeNameor something like that). I am open to adding more.I didn't put too much effort into this yet and would like to have your opinion on how to name or structure these first.
For example, at some places functions are namedFigured that these are most likely named after the first argument. If it is an expression, it isBinaryenSubjectOperation(i.e.BinaryenExpressionPrint) while at others it'sBinaryenOperationSubject(i.e.BinaryenAddFunction).BinaryenExpressionXYetc., correct?