Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Merge WasmExpressionBuilder and WasmFunctionContext into FunctionEmitter. #125

Merged
merged 8 commits into from
May 14, 2024

Conversation

sjrd
Copy link
Collaborator

@sjrd sjrd commented May 10, 2024

The combined class has the same responsibilities as the FunctionEmitter in the JS backend.

Based on #124, but otherwise ready to review.

@sjrd sjrd requested a review from tanishiking May 10, 2024 14:52
@sjrd sjrd force-pushed the function-emitter branch from f4bc829 to 5f10787 Compare May 11, 2024 11:14
@sjrd sjrd mentioned this pull request May 11, 2024
sjrd added 3 commits May 14, 2024 09:56
`SWasmGen` has the same role as `SJSGen` in the JS backend:
producing Wasm fragments that are specific to the linker encoding,
but used across several other components (notably, `ClassEmitter`
and `WasmExpressionBuilder`).
This removes one more use of synthetic IR trees in `ClassEmitter`.
@sjrd sjrd force-pushed the function-emitter branch from 5f10787 to 606b8f4 Compare May 14, 2024 07:57
@sjrd
Copy link
Collaborator Author

sjrd commented May 14, 2024

Rebased.

Copy link
Owner

@tanishiking tanishiking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one question, but overall LGTM 🎉

@@ -210,22 +214,38 @@ const scalaJSHelpers = {

// Non-native JS class support
newSymbol: Symbol,
createJSClass: (data, superClass, preSuperStats, superArgs, postSuperStats) => {
createJSClass: (data, superClass, preSuperStats, superArgs, postSuperStats, fields) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note]
fields is the array that, even index (including 0) contains the field name, and odd index contains the initial value

instrs ++= ctx.getConstantStringInstr(value)

case IRTrees.VarRef(IRTrees.LocalIdent(localName))
if classCaptureParamsOfTypeAny.contains(localName) =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need this condition in this case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because in theory we could have a reference to a class capture of a type different than Any. In that case, we would need adapt to fix up the type, and that means we need a full expression builder/function emitter.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we would need adapt to fix up the type,

Ah, right, thanks!

sjrd added 5 commits May 14, 2024 11:06
Instead of including it in `postSuperStats`.

With this change, the `nameTree` of fields is evaluated in the same
context as all the other `nameTree`s. This will make it easier to
extract name evaluation.
In general, they can contain local variable definitions. These must
not be defined in the same scope as the `createJSClass` function
body, since they could clash with each other and/or with the class
captures.

Instead, in the general case, we extract them in separate functions,
which we immediately call. For the common cases of string literals
and direct reference to a class capture, we avoid the extra function.

This treatment is similar to what the JS backend does, except the
latter defines the "separate functions" inline, as immediatly
invoked function expressions (IIFEs).
Now that we got rid of all the uses of the expression builder
inside it, we can directly use a `FunctionBuilder`.

This was the last place where we mixed IR-derived local names and
codegen-chosen local names, as evidenced by the removal of the last
public method `addLocal` in `WasmFunctionContext`.
The previous entry points `generateIRBody` and `generateBlockStats`
are now private. Instead, `WasmExpressionBuilder` exposes
`emitFunction` (the main entry point) and `emitJSConstructionFunctions`
(a special alternative for the 3 functions that make up a JS
constructor def).

These entry points instantiate their own `WasmFunctionContext`,
which is never used anywhere else anymore.
…ter.

The combined class has the same responsibilities as the
`FunctionEmitter` in the JS backend.
@sjrd sjrd force-pushed the function-emitter branch from 606b8f4 to ac59475 Compare May 14, 2024 09:08
@sjrd sjrd merged commit d457fe6 into tanishiking:main May 14, 2024
1 check passed
@sjrd sjrd deleted the function-emitter branch May 14, 2024 11:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants