@@ -160,13 +160,13 @@ Value *BinaryExprAST::codeGenAnd(NodeAST *l, NodeAST *r) {
160160 auto bbCond2 = BasicBlock::Create (*TheContext, " and_right" );
161161 auto bbCondEnd = BasicBlock::Create (*TheContext, " and_end" );
162162 auto cond1V = l->codegen ();
163- cond1V = Builder->CreateICmpSGT (cond1V, ConstantInt::get (cond1V->getType (), 0 ));
163+ cond1V = Builder->CreateICmpNE (cond1V, ConstantInt::get (cond1V->getType (), 0 ));
164164 bbCond1 = Builder->GetInsertBlock ();
165165 Builder->CreateCondBr (cond1V, bbCond2, bbCondEnd); // 如果为true的话接着判断cond2,
166166 func->getBasicBlockList ().push_back (bbCond2);
167167 Builder->SetInsertPoint (bbCond2);
168168 auto cond2V = r->codegen ();
169- cond2V = Builder->CreateICmpSGT (cond2V, ConstantInt::get (cond2V->getType (), 0 ));
169+ cond2V = Builder->CreateICmpNE (cond2V, ConstantInt::get (cond2V->getType (), 0 ));
170170 // 要考虑嵌套
171171 bbCond2 = Builder->GetInsertBlock ();
172172 Builder->CreateBr (bbCondEnd);
@@ -336,6 +336,7 @@ Value *ConditionAST::codegen() {
336336 ABORT_COMPILE;
337337 return LogErrorV (" if没在函数体内使用" );
338338 }
339+ TheCodeGenContext->push_block (Builder->GetInsertBlock ());
339340 // 创建三个basic block,先添加IfTrue至函数体里面
340341 BasicBlock *bbIfTrue = BasicBlock::Create (*TheContext, " neuq_jintao_if_true" , theFunction);
341342 BasicBlock *bbElse = else_stmt == NIL ? NIL : BasicBlock::Create (*TheContext, " neuq_jintao_else" );
@@ -392,6 +393,7 @@ Value *ConditionAST::codegen() {
392393// if (elseV){
393394// pn->addIncoming(elseV, bbElse);
394395// }
396+ TheCodeGenContext->pop_block ();
395397 return bbIfEnd;
396398}
397399
@@ -592,7 +594,7 @@ Value *BlockAST::codegen() {
592594 lastStatementValue = it->codegen ();
593595#ifdef DEBUG_FLAG
594596 if (lastStatementValue != NIL) {
595- lastStatementValue->print (outs ());
597+ // lastStatementValue->print(outs());
596598 }
597599 }
598600#endif
@@ -612,7 +614,7 @@ Value *BlockAST::codegen() {
612614 lastStatementValue = (*iterator)->codegen ();
613615#ifdef DEBUG_FLAG
614616 if (lastStatementValue != NIL) {
615- lastStatementValue->print (outs ());
617+ // lastStatementValue->print(outs());
616618 }
617619#endif
618620 if (typeid (*it) == typeid (OutStmtAST)) {
@@ -1169,15 +1171,24 @@ VariableArrAssignmentAST::VariableArrAssignmentAST(IdentifierArrExprAST *identif
11691171
11701172llvm::Value *VariableArrAssignmentAST::codegen () {
11711173 auto arr_addr = FINDLOCAL (identifier->identifier );
1174+ // 如果是i8*
1175+ if (arr_addr->getType ()->isPointerTy () && arr_addr->getType ()->getContainedType (0 )->isArrayTy ()) {
1176+ // ignore
1177+ } else {
1178+ arr_addr = Builder->CreateLoad (arr_addr);
1179+ }
11721180 if (arr_addr == nullptr ) {
11731181 ABORT_COMPILE;
11741182 return LogErrorV ((identifier->identifier + " is not defined" ).c_str ());
11751183 }
11761184 auto st = identifier->arrIndex ->begin ();
11771185 Value *ret = arr_addr;
11781186 vector<Value *> vec;
1179- // 0取地址
1180- vec.push_back (ConstantInt::get (getTypeFromStr (" int" ), 0 ));
1187+ // 0取地址,如果是i8*这种就不用,如果是[i8 x n]*就得加0
1188+ // FIXME: 不优雅的特判
1189+ if (arr_addr->getType ()->isPointerTy () && arr_addr->getType ()->getContainedType (0 )->isArrayTy ()) {
1190+ vec.push_back (ConstantInt::get (getTypeFromStr (" int" ), 0 ));
1191+ }
11811192 for (; st != identifier->arrIndex ->end (); st++) {
11821193 auto v = (*st)->codegen ();
11831194 vec.push_back (v);
@@ -1279,6 +1290,7 @@ llvm::Value *WhileStmtAST::codegen() {
12791290 return LogErrorV (" while 不能用在非函数体中" );
12801291 }
12811292 BasicBlock *bbCond = BasicBlock::Create (*TheContext, " while_cond" , function);
1293+ TheCodeGenContext->push_block (bbCond);
12821294 BasicBlock *bbBody = BasicBlock::Create (*TheContext, " while_body" );
12831295 BasicBlock *bbEndWhile = BasicBlock::Create (*TheContext, " while_end" );
12841296 LOCALS->setContextType (CodeGenBlockContextType::WHILE);
@@ -1305,6 +1317,7 @@ llvm::Value *WhileStmtAST::codegen() {
13051317 }
13061318 function->getBasicBlockList ().push_back (bbEndWhile);
13071319 Builder->SetInsertPoint (bbEndWhile);
1320+ TheCodeGenContext->pop_block ();
13081321 return bbEndWhile;
13091322}
13101323
@@ -1384,3 +1397,10 @@ llvm::Value *NullExprAST::codegen() {
13841397 return ConstantExpr::getNullValue (getTypeFromStr (" char" )->getPointerTo ());
13851398}
13861399
1400+ llvm::Value *BoolExprAST::codegen () {
1401+ return is_true ? ConstantInt::get (getTypeFromStr (" bool" ),1 ) : ConstantInt::get (getTypeFromStr (" bool" ),0 );
1402+ }
1403+
1404+ string BoolExprAST::toString () {
1405+ return std::to_string (is_true);
1406+ }
0 commit comments