Skip to content

Commit 39e8cb8

Browse files
committed
Feature(compiler): ForStatement - fixes + drop cast in LessThanToken/GreaterThanToken operators
1 parent 45eb940 commit 39e8cb8

1 file changed

Lines changed: 40 additions & 14 deletions

File tree

src/backend/llvm/index.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ export function passForStatement(parent: ts.ForStatement, ctx: Context, builder:
7373
const conditionBlock = llvm.BasicBlock.create(ctx.llvmContext, "for.condition");
7474
ctx.scope.currentFunction.addBasicBlock(conditionBlock);
7575

76-
builder.createBr(conditionBlock);
77-
7876
const positiveBlock = llvm.BasicBlock.create(ctx.llvmContext, "for.true");
7977
ctx.scope.currentFunction.addBasicBlock(positiveBlock);
8078

@@ -83,6 +81,7 @@ export function passForStatement(parent: ts.ForStatement, ctx: Context, builder:
8381

8482
if (parent.condition) {
8583
builder.createBr(conditionBlock);
84+
builder.setInsertionPoint(conditionBlock);
8685

8786
emitCondition(
8887
parent.condition,
@@ -91,11 +90,18 @@ export function passForStatement(parent: ts.ForStatement, ctx: Context, builder:
9190
positiveBlock,
9291
next
9392
);
93+
} else {
94+
builder.createBr(next);
9495
}
9596

9697
builder.setInsertionPoint(positiveBlock);
97-
passNode(parent.statement, ctx, builder);
98+
passStatement(parent.statement, ctx, builder);
9899

100+
if (parent.incrementor) {
101+
passStatement(<any>parent.incrementor, ctx, builder);
102+
}
103+
104+
// jump again to condition
99105
builder.createBr(conditionBlock);
100106

101107
builder.setInsertionPoint(next);
@@ -110,7 +116,9 @@ export function emitCondition(
110116
) {
111117
const left = buildFromExpression(condition, ctx, builder);
112118

113-
const conditionBoolValue = builder.createICmpNE(left, llvm.ConstantInt.get(ctx.llvmContext, 0));
119+
const leftInt = builder.createZExt(left, llvm.Type.getInt32Ty(ctx.llvmContext));
120+
121+
const conditionBoolValue = builder.createICmpNE(leftInt, llvm.ConstantInt.get(ctx.llvmContext, 0));
114122
builder.createCondBr(conditionBoolValue, positiveBlock, negativeBlock);
115123
}
116124

@@ -183,11 +191,10 @@ function buildFromNumericLiteral(
183191
return llvm.ConstantFP.get(ctx.llvmContext, parseFloat(value.text));
184192
}
185193

186-
const type = nativeType.getType();
187194
return llvm.ConstantInt.get(
188195
ctx.llvmContext,
189196
parseInt(value.text),
190-
(<llvm.IntegerType>type).getBitWidth(),
197+
(<llvm.IntegerType>nativeType.getType()).getBitWidth(),
191198
nativeType.isSigned()
192199
);
193200
}
@@ -238,25 +245,44 @@ function buildFromBinaryExpression(
238245
const left = buildFromExpression(expr.left, ctx, builder);
239246
const right = buildFromExpression(expr.right, ctx, builder);
240247

241-
const leftInt = builder.createZExt(left, llvm.Type.getInt32Ty(ctx.llvmContext));
242-
const rightInt = builder.createZExt(right, llvm.Type.getInt32Ty(ctx.llvmContext));
248+
// const leftInt = builder.createZExt(left, llvm.Type.getInt32Ty(ctx.llvmContext));
249+
// const rightInt = builder.createZExt(right, llvm.Type.getInt32Ty(ctx.llvmContext));
250+
251+
// const leftInt = builder.createFPToSI(
252+
// loadIfNeeded(left, builder, ctx),
253+
// llvm.Type.getInt32Ty(ctx.llvmContext)
254+
// );
255+
// const rightInt = builder.createFPToSI(
256+
// loadIfNeeded(right, builder, ctx),
257+
// llvm.Type.getInt32Ty(ctx.llvmContext)
258+
// );
243259

244260
return builder.createFCmpOGT(
245-
leftInt,
246-
rightInt,
261+
loadIfNeeded(left, builder, ctx),
262+
loadIfNeeded(right, builder, ctx),
247263
'cmpGT'
248264
);
249265
}
250266
case ts.SyntaxKind.LessThanToken: {
251267
const left = buildFromExpression(expr.left, ctx, builder);
252268
const right = buildFromExpression(expr.right, ctx, builder);
253269

254-
const leftInt = builder.createZExt(left, llvm.Type.getInt32Ty(ctx.llvmContext));
255-
const rightInt = builder.createZExt(right, llvm.Type.getInt32Ty(ctx.llvmContext));
270+
// const leftInt = builder.createZExt(left, llvm.Type.getInt32Ty(ctx.llvmContext));
271+
// const rightInt = builder.createZExt(right, llvm.Type.getInt32Ty(ctx.llvmContext));
272+
273+
// const leftInt = builder.createFPToSI(
274+
// loadIfNeeded(left, builder, ctx),
275+
// llvm.Type.getInt32Ty(ctx.llvmContext)
276+
// );
277+
//
278+
// const rightInt = builder.createFPToSI(
279+
// loadIfNeeded(right, builder, ctx),
280+
// llvm.Type.getInt32Ty(ctx.llvmContext)
281+
// );
256282

257283
return builder.createFCmpOLT(
258-
leftInt,
259-
rightInt,
284+
loadIfNeeded(left, builder, ctx),
285+
loadIfNeeded(right, builder, ctx),
260286
'cmpLT'
261287
);
262288
}

0 commit comments

Comments
 (0)