Skip to content

Commit

Permalink
Feature(compiler): BinaryExpression - support pow (a ** 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jan 11, 2019
1 parent 236ad40 commit 13a1011
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/backend/llvm/code-generation/binary-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class BinaryExpressionCodeGenerator implements NodeGenerateInterface<ts.B
case ts.SyntaxKind.LessThanLessThanEqualsToken:
case ts.SyntaxKind.GreaterThanGreaterThanEqualsToken:
case ts.SyntaxKind.CaretEqualsToken:
case ts.SyntaxKind.AsteriskAsteriskEqualsToken:
case ts.SyntaxKind.AsteriskEqualsToken:
case ts.SyntaxKind.PlusEqualsToken:
case ts.SyntaxKind.MinusEqualsToken: {
Expand Down Expand Up @@ -101,6 +102,22 @@ export class BinaryExpressionCodeGenerator implements NodeGenerateInterface<ts.B
)
);
}
// a ** b
case ts.SyntaxKind.AsteriskAsteriskEqualsToken:
case ts.SyntaxKind.AsteriskAsteriskToken: {
const left = buildFromExpression(node.left, ctx, builder);
const right = buildFromExpression(node.right, ctx, builder);

return new Primitive(
builder.createCall(
ctx.getIntrinsic('llvm.pow.f64'),
[
loadIfNeeded(left, builder),
loadIfNeeded(right, builder)
]
)
);
}
// a ^ b
case ts.SyntaxKind.CaretEqualsToken:
case ts.SyntaxKind.CaretToken: {
Expand Down
28 changes: 28 additions & 0 deletions src/backend/llvm/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,32 @@ export class Context {
'array<float64>'
));
}

getIntrinsic(functionName: string): llvm.Function {
const moduleFn = this.llvmModule.getFunction(functionName);
if (moduleFn) {
return moduleFn;
}

switch (functionName) {
case 'llvm.pow.f64':
const intrinsicType = llvm.FunctionType.get(
llvm.Type.getDoubleTy(this.llvmContext),
[
llvm.Type.getDoubleTy(this.llvmContext),
llvm.Type.getDoubleTy(this.llvmContext),
],
false
);

return llvm.Function.create(
intrinsicType,
llvm.LinkageTypes.ExternalLinkage,
functionName,
this.llvmModule
);
default:
throw new Error(`Unknown intrinsic: "${functionName}"`);
}
}
}

0 comments on commit 13a1011

Please sign in to comment.