3030#include < cxx/names.h>
3131#include < cxx/symbols.h>
3232#include < cxx/translation_unit.h>
33+ #include < cxx/type_printer.h>
3334#include < cxx/types.h>
35+ #include < mlir/Dialect/ControlFlow/IR/ControlFlowOps.h>
3436#include < mlir/Dialect/Func/IR/FuncOps.h>
37+ #include < mlir/Dialect/SCF/IR/SCF.h>
3538
36- #if __has_include(<cxxabi.h>)
37- #include < cxxabi.h>
38- #endif
39+ #include < format>
3940
4041namespace cxx {
4142
@@ -638,13 +639,19 @@ Codegen::~Codegen() {}
638639
639640auto Codegen::control () const -> Control* { return unit_->control (); }
640641
641- auto Codegen::emitTodoStmt (SourceLocation location, std::string_view message)
642- -> mlir::cxx::TodoStmtOp {
643- #if true
642+ auto Codegen::getLocation (SourceLocation location) -> mlir::Location {
644643 auto [filename, line, column] = unit_->tokenStartPosition (location);
645644
646645 auto loc =
647646 mlir::FileLineColLoc::get (builder_.getContext (), filename, line, column);
647+
648+ return loc;
649+ }
650+
651+ auto Codegen::emitTodoStmt (SourceLocation location, std::string_view message)
652+ -> mlir::cxx::TodoStmtOp {
653+ #if true
654+ auto loc = getLocation (location);
648655#else
649656 auto loc = builder_.getUnknownLoc ();
650657#endif
@@ -656,10 +663,7 @@ auto Codegen::emitTodoStmt(SourceLocation location, std::string_view message)
656663auto Codegen::emitTodoExpr (SourceLocation location, std::string_view message)
657664 -> mlir::cxx::TodoExprOp {
658665#if true
659- auto [filename, line, column] = unit_->tokenStartPosition (location);
660-
661- auto loc =
662- mlir::FileLineColLoc::get (builder_.getContext (), filename, line, column);
666+ auto loc = getLocation (location);
663667#else
664668 auto loc = builder_.getUnknownLoc ();
665669#endif
@@ -1218,7 +1222,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
12181222 // generate unique names until we have proper name mangling
12191223 name += std::format (" _{}" , ++gen.count_ );
12201224
1221- auto savedInsertionPoint = gen.builder_ .getInsertionBlock ();
1225+ auto savedInsertionPoint = gen.builder_ .saveInsertionPoint ();
12221226
12231227 auto func = gen.builder_ .create <mlir::func::FuncOp>(loc, name, funcType);
12241228
@@ -1247,7 +1251,7 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
12471251
12481252 gen.builder_ .create <mlir::func::ReturnOp>(gen.builder_ .getUnknownLoc ());
12491253
1250- gen.builder_ .setInsertionPointToEnd (savedInsertionPoint);
1254+ gen.builder_ .restoreInsertionPoint (savedInsertionPoint);
12511255
12521256 return {};
12531257}
@@ -1455,9 +1459,6 @@ auto Codegen::StatementVisitor::operator()(ExpressionStatementAST* ast)
14551459
14561460auto Codegen::StatementVisitor::operator ()(CompoundStatementAST* ast)
14571461 -> StatementResult {
1458- auto op =
1459- gen.emitTodoStmt (ast->firstSourceLocation (), " CompoundStatementAST" );
1460-
14611462 for (auto node : ListView{ast->statementList }) {
14621463 auto value = gen (node);
14631464 }
@@ -1467,12 +1468,31 @@ auto Codegen::StatementVisitor::operator()(CompoundStatementAST* ast)
14671468
14681469auto Codegen::StatementVisitor::operator ()(IfStatementAST* ast)
14691470 -> StatementResult {
1470- auto op = gen.emitTodoStmt (ast->firstSourceLocation (), " IfStatementAST" );
1471-
14721471 auto initializerResult = gen (ast->initializer );
1472+
14731473 auto conditionResult = gen (ast->condition );
1474- auto statementResult = gen (ast->statement );
1475- auto elseStatementResult = gen (ast->elseStatement );
1474+
1475+ auto condition = gen.builder_ .create <mlir::cxx::ToBoolOp>(
1476+ gen.getLocation (ast->ifLoc ), gen.builder_ .getI1Type (),
1477+ conditionResult.value );
1478+
1479+ auto ifOp = gen.builder_ .create <mlir::scf::IfOp>(gen.getLocation (ast->ifLoc ),
1480+ condition,
1481+ /* withElse=*/ true );
1482+
1483+ auto ip = gen.builder_ .saveInsertionPoint ();
1484+
1485+ if (ast->statement ) {
1486+ gen.builder_ .setInsertionPointToStart (&ifOp.getThenRegion ().front ());
1487+ auto statementResult = gen (ast->statement );
1488+ }
1489+
1490+ if (ast->elseStatement ) {
1491+ gen.builder_ .setInsertionPointToStart (&ifOp.getElseRegion ().front ());
1492+ auto elseStatementResult = gen (ast->elseStatement );
1493+ }
1494+
1495+ gen.builder_ .restoreInsertionPoint (ip);
14761496
14771497 return {};
14781498}
@@ -1560,9 +1580,13 @@ auto Codegen::StatementVisitor::operator()(ContinueStatementAST* ast)
15601580
15611581auto Codegen::StatementVisitor::operator ()(ReturnStatementAST* ast)
15621582 -> StatementResult {
1563- auto op = gen.emitTodoStmt (ast->firstSourceLocation (), " ReturnStatementAST" );
1564-
15651583 auto expressionResult = gen (ast->expression );
1584+
1585+ auto loc = gen.getLocation (ast->firstSourceLocation ());
1586+
1587+ auto op =
1588+ gen.builder_ .create <mlir::cxx::ReturnOp>(loc, expressionResult.value );
1589+
15661590 return {};
15671591}
15681592
@@ -1629,8 +1653,11 @@ auto Codegen::ExpressionVisitor::operator()(BoolLiteralExpressionAST* ast)
16291653
16301654auto Codegen::ExpressionVisitor::operator ()(IntLiteralExpressionAST* ast)
16311655 -> ExpressionResult {
1632- auto op =
1633- gen.emitTodoExpr (ast->firstSourceLocation (), " IntLiteralExpressionAST" );
1656+ auto loc = gen.getLocation (ast->literalLoc );
1657+
1658+ auto op = gen.builder_ .create <mlir::cxx::IntLiteralOp>(
1659+ loc, ast->literal ->integerValue ());
1660+
16341661 return {op};
16351662}
16361663
@@ -1685,9 +1712,18 @@ auto Codegen::ExpressionVisitor::operator()(NestedExpressionAST* ast)
16851712
16861713auto Codegen::ExpressionVisitor::operator ()(IdExpressionAST* ast)
16871714 -> ExpressionResult {
1688- auto op = gen.emitTodoExpr (ast->firstSourceLocation (), " IdExpressionAST" );
16891715 auto nestedNameSpecifierResult = gen (ast->nestedNameSpecifier );
16901716 auto unqualifiedIdResult = gen (ast->unqualifiedId );
1717+
1718+ if (auto id = ast_cast<NameIdAST>(ast->unqualifiedId );
1719+ id && !ast->nestedNameSpecifier ) {
1720+ auto loc = gen.getLocation (ast->firstSourceLocation ());
1721+ auto name = id->identifier ->name ();
1722+ auto op = gen.builder_ .create <mlir::cxx::IdOp>(loc, name);
1723+ return {op};
1724+ }
1725+
1726+ auto op = gen.emitTodoExpr (ast->firstSourceLocation (), " IdExpressionAST" );
16911727 return {op};
16921728}
16931729
@@ -1788,14 +1824,20 @@ auto Codegen::ExpressionVisitor::operator()(SubscriptExpressionAST* ast)
17881824
17891825auto Codegen::ExpressionVisitor::operator ()(CallExpressionAST* ast)
17901826 -> ExpressionResult {
1791- auto op = gen.emitTodoExpr (ast->firstSourceLocation (), " CallExpressionAST" );
1792-
17931827 auto baseExpressionResult = gen (ast->baseExpression );
17941828
1829+ std::vector<mlir::Value> arguments;
1830+
17951831 for (auto node : ListView{ast->expressionList }) {
17961832 auto value = gen (node);
1833+ arguments.push_back (value.value );
17971834 }
17981835
1836+ auto loc = gen.getLocation (ast->lparenLoc );
1837+
1838+ auto op = gen.builder_ .create <mlir::cxx::CallOp>(
1839+ loc, baseExpressionResult.value , arguments);
1840+
17991841 return {op};
18001842}
18011843
@@ -2064,21 +2106,28 @@ auto Codegen::ExpressionVisitor::operator()(CastExpressionAST* ast)
20642106
20652107auto Codegen::ExpressionVisitor::operator ()(ImplicitCastExpressionAST* ast)
20662108 -> ExpressionResult {
2067- auto op =
2068- gen.emitTodoExpr (ast->firstSourceLocation (), " ImplicitCastExpressionAST" );
2069-
20702109 auto expressionResult = gen (ast->expression );
20712110
2111+ auto loc = gen.getLocation (ast->firstSourceLocation ());
2112+
2113+ auto op = gen.builder_ .create <mlir::cxx::ImplicitCastOp>(
2114+ loc, to_string (ast->castKind ), expressionResult.value );
2115+
20722116 return {op};
20732117}
20742118
20752119auto Codegen::ExpressionVisitor::operator ()(BinaryExpressionAST* ast)
20762120 -> ExpressionResult {
2077- auto op = gen.emitTodoExpr (ast->firstSourceLocation (), " BinaryExpressionAST" );
2078-
20792121 auto leftExpressionResult = gen (ast->leftExpression );
20802122 auto rightExpressionResult = gen (ast->rightExpression );
20812123
2124+ auto loc = gen.getLocation (ast->opLoc );
2125+
2126+ auto operation = Token::spell (ast->op );
2127+
2128+ auto op = gen.builder_ .create <mlir::cxx::BinOp>(
2129+ loc, operation, leftExpressionResult.value , rightExpressionResult.value );
2130+
20822131 return {op};
20832132}
20842133
0 commit comments