Skip to content

Commit 7d00b99

Browse files
authored
Bugfix/arr assign (#6)
* WIP: 增加true、false,qsort测试[1/2] * fix[2/3] * 修复and无效的问题,数组赋值报错
1 parent 82de384 commit 7d00b99

File tree

8 files changed

+784
-1087
lines changed

8 files changed

+784
-1087
lines changed

ast/NodeAST.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

11701172
llvm::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+
}

ast/NodeAST.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ class StringExprAST : public ExpressionAST {
120120
static long id;
121121
};
122122

123+
class BoolExprAST : public ExpressionAST {
124+
public:
125+
bool is_true;
126+
127+
explicit BoolExprAST(bool is_true) : is_true(is_true) {}
128+
129+
llvm::Value *codegen() override;
130+
131+
string toString() override;
132+
};
133+
123134
// 标识符
124135
class IdentifierExprAST : public ExpressionAST {
125136
public:

module/sql/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ endif ()
2525
aux_source_directory(src SRC)
2626

2727
add_library(ksql STATIC ${SRC})
28-
add_executable(ksqldemo ${SRC})
28+
#add_executable(ksqldemo ${SRC})
2929

30-
target_link_libraries(ksqldemo mysqlcppconn kjson)
30+
#target_link_libraries(ksqldemo mysqlcppconn kjson)
3131

3232
install(TARGETS ksql
3333
ARCHIVE DESTINATION ${LOCATION}

parser/Lexer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ int Lexer::_getNextToken() {
5353
return T_NULL;
5454
} else if(identifierStr == "func_ptr"){
5555
return T_FUNC_SIGN;
56+
} else if(identifierStr == "true"){
57+
return T_TRUE;
58+
} else if(identifierStr == "false"){
59+
return T_FALSE;
5660
}
5761
// else if (identifierStr == "module"){
5862
// return T_MODULE;
@@ -209,7 +213,6 @@ int Lexer::_getNextToken() {
209213
// Check for end of file. Don't eat the END_OF_FILE.
210214
if (last_char == END_OF_FILE)
211215
return 0;
212-
213216
// Otherwise, just return the character as its ascii value.
214217
int ThisChar = last_char;
215218
NEXTCHAR;

0 commit comments

Comments
 (0)