ReactiveFunction and codegen for optional calls

Implements HIR->ReactiveFunction conversion and Codegen for optional calls. We 
add a new OptionalCall variant of ReactiveValue, which is a SequenceExpression 
that describes the evaluation of the args and the call itself. This is then 
straightforward to codgen.
This commit is contained in:
Joe Savona committed 2023-03-24 14:22:14 -07:00
1 parent feb8e924e4
commit 77bb9ff765
11 files changed
+104 -21

No files matched your search

+1 -1
View File
@@ -941,7 +941,7 @@ function lowerExpression(
}
case "OptionalCallExpression": {
const expr = exprPath as NodePath<t.OptionalCallExpression>;
const optional = expr.get("optional");
const optional = expr.node.optional;
const calleePath = expr.get("callee");
if (
calleePath.isMemberExpression() ||
+10 -1
View File
@@ -90,7 +90,8 @@ export type ReactiveValue =
| InstructionValue
| ReactiveLogicalValue
| ReactiveSequenceValue
| ReactiveTernaryValue;
| ReactiveTernaryValue
| ReactiveOptionalCallValue;
export type ReactiveLogicalValue = {
kind: "LogicalExpression";
@@ -116,6 +117,14 @@ export type ReactiveSequenceValue = {
loc: SourceLocation;
};
export type ReactiveOptionalCallValue = {
kind: "OptionalCall";
id: InstructionId;
call: ReactiveValue;
optional: boolean;
loc: SourceLocation;
};
export type ReactiveTerminal =
| ReactiveBreakTerminal
| ReactiveContinueTerminal
+4
View File
@@ -440,6 +440,10 @@ export function printInstructionValue(instrValue: ReactiveValue): string {
value = `Global ${instrValue.name}`;
break;
}
case "OptionalCall": {
value = `OptionalCall ${printInstructionValue(instrValue.call)}`;
break;
}
default: {
assertExhaustive(
instrValue,
@@ -29,7 +29,6 @@ import {
ReactiveValue,
Terminal,
} from "../HIR/HIR";
import todo from "../Utils/todo";
import { assertExhaustive } from "../Utils/utils";
/**
@@ -457,6 +456,7 @@ class Driver {
break;
}
case "optional-call":
case "ternary":
case "logical": {
const fallthroughId = terminal.fallthrough;
@@ -482,9 +482,6 @@ class Driver {
this.visitBlock(this.cx.ir.blocks.get(fallthroughId)!, blockValue);
break;
}
case "optional-call": {
todo("BuildReactiveFunction: support optional-call terminal");
}
case "goto": {
switch (terminal.variant) {
case GotoVariant.Break: {
@@ -655,6 +652,31 @@ class Driver {
id: InstructionId;
} {
switch (terminal.kind) {
case "optional-call": {
const test = this.visitValueBlock(terminal.test, terminal.loc);
const testBlock = this.cx.ir.blocks.get(test.block)!;
invariant(
testBlock.terminal.kind === "branch",
"Unexpected terminal kind '%s' for optional call test block",
testBlock.terminal.kind
);
const consequent = this.visitValueBlock(
testBlock.terminal.consequent,
terminal.loc
);
return {
place: { ...consequent.place },
value: {
kind: "OptionalCall",
optional: terminal.optional,
call: consequent.value,
id: terminal.id,
loc: terminal.loc,
},
fallthrough: terminal.fallthrough,
id: terminal.id,
};
}
case "logical": {
const test = this.visitValueBlock(terminal.test, terminal.loc);
const testBlock = this.cx.ir.blocks.get(test.block)!;
@@ -589,6 +589,20 @@ function codegenInstructionValue(
value = createCallExpression(instrValue.loc, callee, args);
break;
}
case "OptionalCall": {
const call = codegenInstructionValue(cx, instrValue.call);
invariant(call.type === "CallExpression", "Expected a call expression");
invariant(
t.isExpression(call.callee),
"v8 intrinsics are validated during lowering"
);
value = t.optionalCallExpression(
call.callee,
call.arguments,
instrValue.optional
);
break;
}
case "MethodCall": {
const memberExpr = codegenPlace(cx, instrValue.property);
invariant(
@@ -124,7 +124,8 @@ class Visitor extends ReactiveFunctionVisitor<Context> {
): void {
if (
instruction.value.kind === "ConditionalExpression" ||
instruction.value.kind === "LogicalExpression"
instruction.value.kind === "LogicalExpression" ||
instruction.value.kind === "OptionalCall"
) {
state.enter(() => {
super.visitInstruction(instruction, state);
@@ -444,6 +444,12 @@ function visitReactiveValue(
value: ReactiveValue
): void {
switch (value.kind) {
case "OptionalCall": {
context.enterConditional(() => {
visitReactiveValue(context, id, value.call);
});
break;
}
case "LogicalExpression": {
visitReactiveValue(context, id, value.left);
context.enterConditional(() => {
@@ -508,6 +508,7 @@ function computeMemoizationInputs(
rvalues: [value.value],
};
}
case "OptionalCall":
case "FunctionExpression":
case "TaggedTemplateExpression":
case "CallExpression":
@@ -41,6 +41,10 @@ export class ReactiveFunctionVisitor<TState = void> {
}
traverseValue(id: InstructionId, value: ReactiveValue, state: TState): void {
switch (value.kind) {
case "OptionalCall": {
this.visitValue(id, value.call, state);
break;
}
case "LogicalExpression": {
this.visitValue(id, value.left, state);
this.visitValue(id, value.right, state);
@@ -277,6 +281,10 @@ export function* eachReactiveValueOperand(
instrValue: ReactiveValue
): Iterable<Place> {
switch (instrValue.kind) {
case "OptionalCall": {
yield* eachReactiveValueOperand(instrValue.call);
break;
}
case "LogicalExpression": {
yield* eachReactiveValueOperand(instrValue.left);
yield* eachReactiveValueOperand(instrValue.right);
+5 -1
View File
@@ -336,7 +336,11 @@ export function leaveSSA(fn: HIRFunction): void {
const update = fn.body.blocks.get(terminal.update)!;
pushPhis(update);
}
if (terminal.kind === "logical" || terminal.kind === "ternary") {
if (
terminal.kind === "logical" ||
terminal.kind === "ternary" ||
terminal.kind === "optional-call"
) {
const fallthrough = fn.body.blocks.get(terminal.fallthrough)!;
pushPhis(fallthrough);
}
+27 -13
View File
@@ -83,12 +83,12 @@
"@jridgewell/gen-mapping" "^0.3.2"
jsesc "^2.5.1"
"@babel/generator@^7.21.1":
version "7.21.1"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.21.1.tgz#951cc626057bc0af2c35cd23e9c64d384dea83dd"
integrity sha512-1lT45bAYlQhFn/BHivJs43AiW2rg3/UbLyShGfF3C0KmHvO5fSghWd5kBJy30kpRRucGzXStvnnCFniCR2kXAA==
"@babel/generator@^7.21.3":
version "7.21.3"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.21.3.tgz#232359d0874b392df04045d72ce2fd9bb5045fce"
integrity sha512-QS3iR1GYC/YGUnW7IdggFeN5c1poPUurnGttOV/bZgPGV+izC/D8HnD6DLwod0fsatNyVn1G3EVWMYIF0nHbeA==
dependencies:
"@babel/types" "^7.21.0"
"@babel/types" "^7.21.3"
"@jridgewell/gen-mapping" "^0.3.2"
"@jridgewell/trace-mapping" "^0.3.17"
jsesc "^2.5.1"
@@ -269,11 +269,16 @@
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.19.3.tgz#8dd36d17c53ff347f9e55c328710321b49479a9a"
integrity sha512-pJ9xOlNWHiy9+FuFP09DEAFbAn4JskgRsVcc169w2xRBC3FRGuQEwjeIMMND9L2zc0iEhO/tGv4Zq+km+hxNpQ==
"@babel/parser@^7.20.7", "@babel/parser@^7.21.2":
"@babel/parser@^7.20.7":
version "7.21.2"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.21.2.tgz#dacafadfc6d7654c3051a66d6fe55b6cb2f2a0b3"
integrity sha512-URpaIJQwEkEC2T9Kn+Ai6Xe/02iNaVCuT/PtoRz3GPVJVDpPd7mLo+VddTbhCRU9TXqW5mSrQfXZyi8kDKOVpQ==
"@babel/parser@^7.21.3":
version "7.21.3"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.21.3.tgz#1d285d67a19162ff9daa358d4cb41d50c06220b3"
integrity sha512-lobG0d7aOfQRXh8AyklEAgZGvA4FShxo6xQbUrrT/cNBPUdIDojlokwJsQyCC/eKia7ifqM0yP+2DRZ4WKw2RQ==
"@babel/plugin-syntax-async-generators@^7.8.4":
version "7.8.4"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-generators/-/plugin-syntax-async-generators-7.8.4.tgz#a983fb1aeb2ec3f6ed042a210f640e90e786fe0d"
@@ -502,18 +507,18 @@
lodash "^4.17.10"
"@babel/traverse@^7.19.1":
version "7.21.2"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.21.2.tgz#ac7e1f27658750892e815e60ae90f382a46d8e75"
integrity sha512-ts5FFU/dSUPS13tv8XiEObDu9K+iagEKME9kAbaP7r0Y9KtZJZ+NGndDvWoRAYNpeWafbpFeki3q9QoMD6gxyw==
version "7.21.3"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.21.3.tgz#4747c5e7903d224be71f90788b06798331896f67"
integrity sha512-XLyopNeaTancVitYZe2MlUEvgKb6YVVPXzofHgqHijCImG33b/uTurMS488ht/Hbsb2XK3U2BnSTxKVNGV3nGQ==
dependencies:
"@babel/code-frame" "^7.18.6"
"@babel/generator" "^7.21.1"
"@babel/generator" "^7.21.3"
"@babel/helper-environment-visitor" "^7.18.9"
"@babel/helper-function-name" "^7.21.0"
"@babel/helper-hoist-variables" "^7.18.6"
"@babel/helper-split-export-declaration" "^7.18.6"
"@babel/parser" "^7.21.2"
"@babel/types" "^7.21.2"
"@babel/parser" "^7.21.3"
"@babel/types" "^7.21.3"
debug "^4.1.0"
globals "^11.1.0"
@@ -561,7 +566,7 @@
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"
"@babel/types@^7.21.0", "@babel/types@^7.21.2":
"@babel/types@^7.21.0":
version "7.21.2"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.21.2.tgz#92246f6e00f91755893c2876ad653db70c8310d1"
integrity sha512-3wRZSs7jiFaB8AjxiiD+VqN5DTG2iRvJGQ+qYFrs/654lg6kGTQWIOFjlBo5RaXuAZjBmP3+OQH4dmhqiiyYxw==
@@ -570,6 +575,15 @@
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"
"@babel/types@^7.21.3":
version "7.21.3"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.21.3.tgz#4865a5357ce40f64e3400b0f3b737dc6d4f64d05"
integrity sha512-sBGdETxC+/M4o/zKC0sl6sjWv62WFR/uzxrJ6uYyMLZOUlPnwzw0tKgVHOXxaAd5l2g8pEDM5RZ495GPQI77kg==
dependencies:
"@babel/helper-string-parser" "^7.19.4"
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"
"@bcoe/v8-coverage@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"