From 2f438dab6223b13de278a5e78a2ccc59b255cc56 Mon Sep 17 00:00:00 2001 From: wqz <1197460504@qq.com> Date: Wed, 5 Jul 2023 15:55:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E9=9D=9E=E8=BF=90=E7=AE=97?= =?UTF-8?q?=E5=9C=A8=E6=9D=A1=E4=BB=B6=E8=AF=AD=E5=8F=A5=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SysYIRGenerator.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/SysYIRGenerator.cpp b/src/SysYIRGenerator.cpp index c05f191..87c8342 100644 --- a/src/SysYIRGenerator.cpp +++ b/src/SysYIRGenerator.cpp @@ -515,8 +515,6 @@ namespace sysy any SysYIRGenerator::visitAndExp(SysYParser::AndExpContext *ctx) { - // get lhs value - auto lhs = any_cast(ctx->exp()[0]->accept(this)); // create rhs block builder.rhs_add(); auto current_block = builder.getBasicBlock(); @@ -526,16 +524,19 @@ namespace sysy auto rhs_block = func->addBasicBlock(rhs_name); rhs_block->getPredecessors().push_back(current_block); current_block->getSuccessors().push_back(rhs_block); + builder.push_truetarget(rhs_block); + builder.push_falsetarget(builder.get_falsetarget()); + // get lhs value + auto lhs = any_cast(ctx->exp()[0]->accept(this)); // create condbr instr - Value *condbr = builder.createCondBrInst(lhs, rhs_block, builder.get_falsetarget(), vector(), vector()); + Value *condbr = builder.createCondBrInst(lhs, builder.get_truetarget(), builder.get_falsetarget(), vector(), vector()); + builder.poptarget(); // generate code for rhs block builder.setPosition(rhs_block, rhs_block->begin()); return (ctx->exp()[1]->accept(this)); } any SysYIRGenerator::visitOrExp(SysYParser::OrExpContext *ctx) { - // get lhs value - auto lhs = any_cast(ctx->exp()[0]->accept(this)); // create rhs block builder.rhs_add(); auto current_block = builder.getBasicBlock(); @@ -545,8 +546,13 @@ namespace sysy auto rhs_block = func->addBasicBlock(rhs_name); rhs_block->getPredecessors().push_back(current_block); current_block->getSuccessors().push_back(rhs_block); + builder.push_truetarget(builder.get_truetarget()); + builder.push_falsetarget(rhs_block); + // get lhs value + auto lhs = any_cast(ctx->exp()[0]->accept(this)); // create condbr instr - Value *condbr = builder.createCondBrInst(lhs, builder.get_truetarget(), rhs_block, vector(), vector()); + Value *condbr = builder.createCondBrInst(lhs, builder.get_truetarget(), builder.get_falsetarget(), vector(), vector()); + builder.poptarget(); // generate code for rhs block builder.setPosition(rhs_block, rhs_block->begin()); return (ctx->exp()[1]->accept(this));