Skip to content

Commit 1dc9e9d

Browse files
committed
Feature(compiler): ForStatement - initial
1 parent 4edbcf7 commit 1dc9e9d

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/backend/llvm/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ export function passIfStatement(parent: ts.IfStatement, ctx: Context, builder: l
6565
builder.setInsertionPoint(next);
6666
}
6767

68+
export function passForStatement(parent: ts.ForStatement, ctx: Context, builder: llvm.IRBuilder) {
69+
if (parent.initializer) {
70+
passStatement(<any>parent.initializer, ctx, builder);
71+
}
72+
73+
const conditionBlock = llvm.BasicBlock.create(ctx.llvmContext, "for.condition");
74+
ctx.scope.currentFunction.addBasicBlock(conditionBlock);
75+
76+
builder.createBr(conditionBlock);
77+
78+
const positiveBlock = llvm.BasicBlock.create(ctx.llvmContext, "for.true");
79+
ctx.scope.currentFunction.addBasicBlock(positiveBlock);
80+
81+
const next = llvm.BasicBlock.create(ctx.llvmContext, "for.end");
82+
ctx.scope.currentFunction.addBasicBlock(next);
83+
84+
if (parent.condition) {
85+
builder.createBr(conditionBlock);
86+
87+
emitCondition(
88+
parent.condition,
89+
ctx,
90+
builder,
91+
positiveBlock,
92+
next
93+
);
94+
}
95+
96+
builder.setInsertionPoint(positiveBlock);
97+
passNode(parent.statement, ctx, builder);
98+
99+
builder.createBr(conditionBlock);
100+
101+
builder.setInsertionPoint(next);
102+
}
103+
68104
export function emitCondition(
69105
condition: ts.Expression,
70106
ctx: Context,
@@ -443,6 +479,9 @@ export function passStatement(stmt: ts.Statement, ctx: Context, builder: llvm.IR
443479
case ts.SyntaxKind.IfStatement:
444480
passIfStatement(<any>stmt, ctx, builder);
445481
break;
482+
case ts.SyntaxKind.ForStatement:
483+
passForStatement(<any>stmt, ctx, builder);
484+
break;
446485
default:
447486
throw new UnsupportedError(
448487
stmt,

0 commit comments

Comments
 (0)