vector<string> CodeGenerator::block(vector<Node>::iterator& it) { vector<string> blockCode; ++it; ++it; while (it->symbol == "VarDeclList") { ++it; if (it->symbol == "VarDecl") { appendVectors(blockCode, varDecl(it)); ++it; } } ++it; while (it->symbol == "StmtList") { ++it; appendVectors(blockCode, statement(it)); it += 2; } ++it; // cout << "block " << it->symbol << endl; return blockCode; }
void CodeGenerator::codeGeneration(vector<Node> parseTree) { // set up print function llvmCode.push_back("@istr = private constant[4 x i8] c\"%d\\0A\\00\"\n"); llvmCode.push_back("@fstr = private constant[4 x i8] c\"%f\\0A\\00\"\n"); llvmCode.push_back("declare i32 @printf(i8*, ...)\n\n"); // scan the tree to generate the code for (vector<Node>::iterator it = parseTree.begin(); it != parseTree.end(); it++) { // Start analyzing the tree if (it->symbol == "DeclList'") // declaration of variable or function appendVectors(llvmCode, declaration(it)); else if (it->symbol == "VarDecl") // declaration of variable (could be array) appendVectors(llvmCode,varDecl(it)); else if (it->symbol == "Stmt") // statement is appeared appendVectors(llvmCode, statement(it)); else if (it->symbol == "{") { llvmCode.push_back("{\n"); instruction = 0; } else if (it->symbol == "}") llvmCode.push_back("}\n"); } exportLlvmCode(); }
vector<string> CodeGenerator::statement(vector<Node>::iterator& it) { vector<string> stmtCode; vector<Node>::iterator temp = ++it; if (temp->symbol == "print") { // print id; temp = temp + 2; appendVectors(stmtCode, printID(temp)); it += 3; } else if (temp->symbol == "Expr") { // Expr; appendVectors(stmtCode, expr(it)); ++it; } else if (temp->symbol == "if") { // if ( Expr ) Stmt else Stmt appendVectors(stmtCode, ifElse(it)); } else if (temp->symbol == "while") { // while ( Expr ) Stmt appendVectors(stmtCode, whileStatement(it)); } else if (temp->symbol == "return") { // TODO: return string ret = "ret i32 0\n"; stmtCode.push_back(ret); } else if (temp->symbol == "Block") { appendVectors(stmtCode, block(it)); } return stmtCode; }
vector<string> CodeGenerator::ifElse(vector<Node>::iterator& it) { vector<string> code; stringstream line; vector<string> exprCode, stmtCode1, stmtCode2; string cmp; int trueLabel, falseLabel, originLabel; it += 2; exprCode = expr(it); cmp = exprCode.back().substr(0, exprCode.back().find(" ")); trueLabel = ++instruction; it += 2; stmtCode1 = statement(it); falseLabel = ++instruction; it += 2; stmtCode2 = statement(it); originLabel = ++instruction; appendVectors(code, exprCode); line << "br i1 " << cmp << ", label %" << trueLabel << ", label %" << falseLabel <<"\n"; code.push_back(line.str()); line.str(""); line << "\n; <label>:" << trueLabel << "\n"; code.push_back(line.str()); line.str(""); appendVectors(code, stmtCode1); line << "br label %" << originLabel << "\n"; code.push_back(line.str()); line.str(""); line << "\n; <label>:" << falseLabel << "\n"; code.push_back(line.str()); line.str(""); appendVectors(code, stmtCode2); line << "br label %" << originLabel << "\n"; code.push_back(line.str()); line.str(""); line << "\n; <label>:" << originLabel << "\n"; code.push_back(line.str()); line.str(""); return code; }
void hDebugDrawRenderer::append(hDebugDraw* debugdraw) { hcAssert(debugdraw); hMutexAutoScope mutscope(&critSection_); hDebugPrimsSet* ddprims=debugdraw->debugPrims_; for (hUint32 i=0; i<eDebugSet_Max; ++i) { appendVectors(debugPrims_[i].lines_, ddprims[i].lines_); appendVectors(debugPrims_[i].tris_, ddprims[i].tris_); appendVectors(debugPrims_[i].texQuads_, ddprims[i].texQuads_); hUint newtxtstart=(hUint)debugPrims_[i].txtBuffer_.size(); appendVectors(debugPrims_[i].txtBuffer_, ddprims[i].txtBuffer_); hUint stringcount=(hUint)debugPrims_[i].strings_.size(); debugPrims_[i].strings_.reserve(debugPrims_[i].strings_.size()+stringcount); for (hUint t=0, n=(hUint)ddprims[i].strings_.size(); t<n; ++t) { debugPrims_[i].strings_.push_back(ddprims[i].strings_[t]); debugPrims_[i].strings_[stringcount++].txtBufOffset+=newtxtstart; } } }
vector<string> CodeGenerator::expr(vector<Node>::iterator& it) // start calculation { // TODO : add value to exprCode vector<string> exprCode; int exprLayer = it->layer; // save this expr's level it++; vector<string> expression; for (; it->layer > exprLayer; it++) { string thisSymbol = it->symbol; if (thisSymbol == "+" || thisSymbol == "-" || thisSymbol == "*" || thisSymbol == "/" || thisSymbol == "==" || thisSymbol == "!=" || thisSymbol == "<" || thisSymbol == "<=" || thisSymbol == ">" || thisSymbol == ">=" || thisSymbol == "&&" || thisSymbol == "||" || thisSymbol == "=") // BinOp expression.push_back(thisSymbol); else if (thisSymbol == "id") { it++; string var = it->symbol; // it += 2; expression.push_back(it->symbol); /* if (it->symbol != "(" && it->symbol != "[") //function expression.push_back(var); */ } else if (thisSymbol == "num") { it++; expression.push_back(it->symbol); } // else if (thisSymbol == "(") { // expression.push_back(it->symbol); // } // else if (thisSymbol == ")") { // expression.push_back(it->symbol); // } } it--; // for (vector<string>::iterator x = expression.begin(); x != expression.end(); // x++) // cout << *x << " "; // cout << endl; appendVectors(exprCode, handleExpr(infixExprToPostfix(expression))); return exprCode; }