comparison gen/CodeGen.d @ 155:2149f4a7b48d

Codegen for statements
author Anders Halager <halager@gmail.com>
date Mon, 21 Jul 2008 22:12:33 +0200
parents d76cc5cad4fc
children 6cb2f4201e2a
comparison
equal deleted inserted replaced
154:0ea5d2f3e96b 155:2149f4a7b48d
508 508
509 b.positionAtEnd(mergeBB); 509 b.positionAtEnd(mergeBB);
510 break; 510 break;
511 case StmtType.While: 511 case StmtType.While:
512 auto wStmt = cast(WhileStmt)stmt; 512 auto wStmt = cast(WhileStmt)stmt;
513 auto fd = stmt.env.parentFunction(); 513 genLoop(stmt.env, wStmt.cond, false, wStmt.whileBody);
514 Function func = m.getNamedFunction(symbolName(fd)); 514 break;
515 515 /+
516 auto condBB = func.appendBasicBlock("cond"); 516 case StmtType.DoWhile:
517 auto bodyBB = func.appendBasicBlock("body"); 517 auto wStmt = cast(DoWhileStmt)stmt;
518 auto doneBB = func.appendBasicBlock("done"); 518 genLoop(stmt.env, wStmt.cond, true, wStmt.whileBody);
519 519 break;
520 b.buildBr(condBB); 520 +/
521 b.positionAtEnd(condBB); 521 case StmtType.For:
522 Value cond = genExpression(wStmt.cond).value; 522 auto fStmt = cast(ForStmt)stmt;
523 if (cond.type !is Type.Int1) 523 genStmt(fStmt.init);
524 { 524 scope inc = new ExpStmt(fStmt.incre);
525 Value False = ConstantInt.GetS(cond.type, 0); 525 genLoop(stmt.env, fStmt.cond, false, fStmt.forBody, inc);
526 cond = b.buildICmp(IntPredicate.NE, cond, False, ".cond");
527 }
528 b.buildCondBr(cond, bodyBB, doneBB);
529
530 b.positionAtEnd(bodyBB);
531 genStmt(wStmt.whileBody);
532 if (b.getInsertBlock().terminated() is false)
533 b.buildBr(condBB);
534
535 b.positionAtEnd(doneBB);
536 break; 526 break;
537 case StmtType.Switch: 527 case StmtType.Switch:
538 auto sw = cast(SwitchStmt)stmt; 528 auto sw = cast(SwitchStmt)stmt;
539 Value cond = genExpression(sw.cond).value; 529 Value cond = genExpression(sw.cond).value;
540 530
583 } 573 }
584 } 574 }
585 b.positionAtEnd(endBB); 575 b.positionAtEnd(endBB);
586 break; 576 break;
587 } 577 }
578 }
579
580 /**
581 Generate a loop.
582
583 Loops while cond is true, executing all statements in stmts every time.
584
585 If skipFirstCond is set, the condition is skipped the first time around,
586 like in a do-while loop.
587 **/
588 void genLoop(Scope env, Exp cond, bool skipFirstCond, Stmt[] stmts...)
589 {
590 auto fd = env.parentFunction();
591 Function func = m.getNamedFunction(symbolName(fd));
592
593 auto condBB = func.appendBasicBlock("cond");
594 auto bodyBB = func.appendBasicBlock("body");
595 auto doneBB = func.appendBasicBlock("done");
596
597 b.buildBr(skipFirstCond? bodyBB : condBB);
598 b.positionAtEnd(condBB);
599
600 Value cond_v = genExpression(cond).value;
601 if (cond_v.type !is Type.Int1)
602 {
603 Value False = ConstantInt.GetS(cond_v.type, 0);
604 cond_v = b.buildICmp(IntPredicate.NE, cond_v, False, ".cond");
605 }
606 b.buildCondBr(cond_v, bodyBB, doneBB);
607
608 b.positionAtEnd(bodyBB);
609 foreach (stmt; stmts)
610 genStmt(stmt);
611 if (b.getInsertBlock().terminated() is false)
612 b.buildBr(condBB);
613
614 b.positionAtEnd(doneBB);
588 } 615 }
589 616
590 /* 617 /*
591 Get the address of an expression - allowing us to modify something in 618 Get the address of an expression - allowing us to modify something in
592 memory or on the stack. 619 memory or on the stack.