[codegen] more locations: binary, return, call (#820)

This commit is contained in:
Jan Kassens committed 2022-11-28 16:54:46 -05:00
1 parent b932ce19a7
commit 0bbb36e1a4
6 files changed
+24 -6

No files matched your search

+1
View File
@@ -136,6 +136,7 @@ function lowerStatement(
const fallthrough = builder.reserve();
const terminal: ReturnTerminal = {
kind: "return",
loc: stmt.node.loc ?? GeneratedSource,
value,
id: makeInstructionId(0),
};
+12 -4
View File
@@ -43,6 +43,8 @@ function withLoc<TNode extends t.Node, T extends (...args: any[]) => TNode>(
};
}
const createBinaryExpression = withLoc(t.binaryExpression);
const createCallExpression = withLoc(t.callExpression);
const createExpressionStatement = withLoc(t.expressionStatement);
const createFunctionDeclaration = withLoc(t.functionDeclaration);
const createLabelledStatement = withLoc(t.labeledStatement);
@@ -192,15 +194,16 @@ class CodegenVisitor
return createWhileStatement(terminal.loc, terminal.test, terminal.loop);
}
case "return": {
const createReturnStatement = withLoc(t.returnStatement);
if (terminal.value !== null) {
return t.returnStatement(terminal.value);
return createReturnStatement(terminal.loc, terminal.value);
} else if (this.depth === 1) {
// A return at the top-level of a function must be the last instruction,
// and functions implicitly return after the last instruction of the top-level.
// Elide the return.
return t.emptyStatement();
} else {
return t.returnStatement();
return createReturnStatement(terminal.loc);
}
}
case "throw": {
@@ -253,7 +256,12 @@ function codegenInstructionValue(
case "BinaryExpression": {
const left = codegenPlace(temp, instrValue.left);
const right = codegenPlace(temp, instrValue.right);
value = t.binaryExpression(instrValue.operator, left, right);
value = createBinaryExpression(
instrValue.loc,
instrValue.operator,
left,
right
);
break;
}
case "UnaryExpression": {
@@ -270,7 +278,7 @@ function codegenInstructionValue(
case "CallExpression": {
const callee = codegenPlace(temp, instrValue.callee);
const args = instrValue.args.map((arg) => codegenPlace(temp, arg));
value = t.callExpression(callee, args);
value = createCallExpression(instrValue.loc, callee, args);
break;
}
case "NewExpression": {
+1
View File
@@ -114,6 +114,7 @@ export type ThrowTerminal = { kind: "throw"; value: Place; id: InstructionId };
export type ReturnTerminal = {
kind: "return";
loc: SourceLocation;
value: Place | null;
id: InstructionId;
};
+7 -1
View File
@@ -11,6 +11,7 @@ import { invariant } from "../CompilerError";
import {
BasicBlock,
BlockId,
GeneratedSource,
GotoVariant,
HIR,
Identifier,
@@ -145,7 +146,12 @@ export default class HIRBuilder {
this.#completed.set(blockId, {
id: blockId,
instructions,
terminal: { kind: "return", value: null, id: makeInstructionId(0) },
terminal: {
kind: "return",
loc: GeneratedSource,
value: null,
id: makeInstructionId(0),
},
preds: new Set(),
phis: new Set(),
});
+2 -1
View File
@@ -74,6 +74,7 @@ class Driver<TBlock, TValue, TItem, TCase> {
blockValue,
this.visitor.visitTerminal({
kind: "return",
loc: terminal.loc,
value,
})
);
@@ -697,7 +698,7 @@ export interface Visitor<TBlock, TValue, TItem, TCase> {
}
export type BlockTerminal<TBlock, TValue, TItem, TCase> =
| { kind: "return"; value: TValue | null }
| { kind: "return"; loc: SourceLocation; value: TValue | null }
| { kind: "throw"; value: TValue }
| {
kind: "if";
+1
View File
@@ -193,6 +193,7 @@ export function mapTerminalSuccessors(
case "return": {
return {
kind: "return",
loc: terminal.loc,
value: terminal.value,
id: makeInstructionId(0),
};