Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/cxx-frontend/src/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7974,6 +7974,58 @@ export class AssignmentExpressionAST extends ExpressionAST {
}
}

/**
* CompoundAssignmentExpressionAST node.
*/
export class CompoundAssignmentExpressionAST extends ExpressionAST {
/**
* Traverse this node using the given visitor.
* @param visitor the visitor.
* @param context the context.
* @returns the result of the visit.
*/
accept<Context, Result>(
visitor: ASTVisitor<Context, Result>,
context: Context,
): Result {
return visitor.visitCompoundAssignmentExpression(this, context);
}

/**
* Returns the leftExpression of this node
*/
getLeftExpression(): ExpressionAST | undefined {
return AST.from<ExpressionAST>(
cxx.getASTSlot(this.getHandle(), 0),
this.parser,
);
}

/**
* Returns the location of the op token in this node
*/
getOpToken(): Token | undefined {
return Token.from(cxx.getASTSlot(this.getHandle(), 1), this.parser);
}

/**
* Returns the rightExpression of this node
*/
getRightExpression(): ExpressionAST | undefined {
return AST.from<ExpressionAST>(
cxx.getASTSlot(this.getHandle(), 2),
this.parser,
);
}

/**
* Returns the op attribute of this node
*/
getOp(): TokenKind {
return cxx.getASTSlot(this.getHandle(), 3);
}
}

/**
* PackExpansionExpressionAST node.
*/
Expand Down Expand Up @@ -13376,6 +13428,7 @@ const AST_CONSTRUCTORS: Array<
YieldExpressionAST,
ThrowExpressionAST,
AssignmentExpressionAST,
CompoundAssignmentExpressionAST,
PackExpansionExpressionAST,
DesignatedInitializerClauseAST,
TypeTraitExpressionAST,
Expand Down
1 change: 1 addition & 0 deletions packages/cxx-frontend/src/ASTKind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export enum ASTKind {
YieldExpression,
ThrowExpression,
AssignmentExpression,
CompoundAssignmentExpression,
PackExpansionExpression,
DesignatedInitializerClause,
TypeTraitExpression,
Expand Down
12 changes: 12 additions & 0 deletions packages/cxx-frontend/src/ASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,18 @@ export abstract class ASTVisitor<Context, Result> {
context: Context,
): Result;

/**
* Visit CompoundAssignmentExpression node.
*
* @param node The node to visit.
* @param context The context.
* @returns The result of the visit.
*/
abstract visitCompoundAssignmentExpression(
node: ast.CompoundAssignmentExpressionAST,
context: Context,
): Result;

/**
* Visit PackExpansionExpression node.
*
Expand Down
14 changes: 14 additions & 0 deletions packages/cxx-frontend/src/RecursiveASTVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,20 @@ export class RecursiveASTVisitor<Context> extends ASTVisitor<Context, void> {
this.accept(node.getRightExpression(), context);
}

/**
* Visit a CompoundAssignmentExpression node.
*
* @param node The node to visit.
* @param context The context.
*/
visitCompoundAssignmentExpression(
node: ast.CompoundAssignmentExpressionAST,
context: Context,
): void {
this.accept(node.getLeftExpression(), context);
this.accept(node.getRightExpression(), context);
}

/**
* Visit a PackExpansionExpression node.
*
Expand Down
28 changes: 28 additions & 0 deletions packages/cxx-gen-ast/src/gen_token_fwd_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ${cpy_header}
#pragma once

#include <cxx/cxx_fwd.h>
#include <optional>
#include <cstdint>

namespace cxx {
Expand Down Expand Up @@ -93,6 +94,33 @@ enum class BuiltinTypeTraitKind {
#undef TOKEN_ALIAS_ENUM
// clang-format on

[[nodiscard]] inline auto get_underlying_binary_op(TokenKind op) -> TokenKind {
switch (op) {
case TokenKind::T_STAR_EQUAL:
return TokenKind::T_STAR;
case TokenKind::T_SLASH_EQUAL:
return TokenKind::T_SLASH;
case TokenKind::T_PERCENT_EQUAL:
return TokenKind::T_PERCENT;
case TokenKind::T_PLUS_EQUAL:
return TokenKind::T_PLUS;
case TokenKind::T_MINUS_EQUAL:
return TokenKind::T_MINUS;
case TokenKind::T_LESS_LESS_EQUAL:
return TokenKind::T_LESS_LESS;
case TokenKind::T_AMP_EQUAL:
return TokenKind::T_AMP;
case TokenKind::T_CARET_EQUAL:
return TokenKind::T_CARET;
case TokenKind::T_BAR_EQUAL:
return TokenKind::T_BAR;
case TokenKind::T_GREATER_GREATER_EQUAL:
return TokenKind::T_GREATER_GREATER;
default:
return TokenKind::T_EOF_SYMBOL;
} // switch
}

} // namespace cxx
`;

Expand Down
2 changes: 0 additions & 2 deletions src/mlir/cxx/mlir/codegen_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
auto exitValueType = gen.convertType(returnType);
auto ptrType = gen.builder_.getType<mlir::cxx::PointerType>(exitValueType);
exitValue = gen.builder_.create<mlir::cxx::AllocaOp>(exitValueLoc, ptrType);

exitBlock->addArgument(ptrType, exitValueLoc);
}

// restore state
Expand Down
38 changes: 35 additions & 3 deletions src/mlir/cxx/mlir/codegen_expressions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <cxx/ast_interpreter.h>
#include <cxx/literals.h>
#include <cxx/symbols.h>
#include <cxx/translation_unit.h>
#include <cxx/types.h>

namespace cxx {
Expand Down Expand Up @@ -89,6 +90,7 @@ struct Codegen::ExpressionVisitor {
auto operator()(YieldExpressionAST* ast) -> ExpressionResult;
auto operator()(ThrowExpressionAST* ast) -> ExpressionResult;
auto operator()(AssignmentExpressionAST* ast) -> ExpressionResult;
auto operator()(CompoundAssignmentExpressionAST* ast) -> ExpressionResult;
auto operator()(PackExpansionExpressionAST* ast) -> ExpressionResult;
auto operator()(DesignatedInitializerClauseAST* ast) -> ExpressionResult;
auto operator()(TypeTraitExpressionAST* ast) -> ExpressionResult;
Expand Down Expand Up @@ -809,6 +811,13 @@ auto Codegen::ExpressionVisitor::operator()(ImplicitCastExpressionAST* ast)

auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast)
-> ExpressionResult {
if (ast->op == TokenKind::T_COMMA) {
// For the comma operator, we evaluate the left expression for its side
// effects and then return the right expression as the result.
(void)gen.expression(ast->leftExpression, ExpressionFormat::kSideEffect);
return gen.expression(ast->rightExpression, format);
}

auto op =
gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind()));

Expand Down Expand Up @@ -872,9 +881,7 @@ auto Codegen::ExpressionVisitor::operator()(AssignmentExpressionAST* ast)
auto rightExpressionResult = gen.expression(ast->rightExpression);

// Generate a store operation
auto loc = gen.getLocation(ast->opLoc);

auto resultType = gen.convertType(ast->leftExpression->type);
const auto loc = gen.getLocation(ast->opLoc);

gen.builder_.create<mlir::cxx::StoreOp>(loc, rightExpressionResult.value,
leftExpressionResult.value);
Expand All @@ -883,6 +890,18 @@ auto Codegen::ExpressionVisitor::operator()(AssignmentExpressionAST* ast)
return {};
}

if (gen.unit_->language() == LanguageKind::kC) {
// in C mode the result of the assignment is an rvalue
auto resultLoc = gen.getLocation(ast->firstSourceLocation());
auto resultType = gen.convertType(ast->leftExpression->type);

// generate a load
auto op = gen.builder_.create<mlir::cxx::LoadOp>(
resultLoc, resultType, leftExpressionResult.value);

return {op};
}

return leftExpressionResult;
}

Expand All @@ -897,6 +916,19 @@ auto Codegen::ExpressionVisitor::operator()(AssignmentExpressionAST* ast)
return {op};
}

auto Codegen::ExpressionVisitor::operator()(
CompoundAssignmentExpressionAST* ast) -> ExpressionResult {
auto op =
gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind()));

#if false
auto leftExpressionResult = gen.expression(ast->leftExpression);
auto rightExpressionResult = gen.expression(ast->rightExpression);
#endif

return {op};
}

auto Codegen::ExpressionVisitor::operator()(PackExpansionExpressionAST* ast)
-> ExpressionResult {
auto op =
Expand Down
12 changes: 1 addition & 11 deletions src/mlir/cxx/mlir/codegen_statements.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,16 @@ void Codegen::StatementVisitor::operator()(ContinueStatementAST* ast) {
}

void Codegen::StatementVisitor::operator()(ReturnStatementAST* ast) {
// (void)gen.emitTodoStmt(ast->firstSourceLocation(), to_string(ast->kind()));

auto value = gen.expression(ast->expression);

#if false
auto expressionResult = gen.expression(ast->expression);
#endif

auto loc = gen.getLocation(ast->firstSourceLocation());

mlir::SmallVector<mlir::Value> results;

if (gen.exitValue_) {
gen.builder_.create<mlir::cxx::StoreOp>(loc, value.value,
gen.exitValue_.getResult());

results.push_back(gen.exitValue_);
}

gen.builder_.create<mlir::cf::BranchOp>(loc, results, gen.exitBlock_);
gen.builder_.create<mlir::cf::BranchOp>(loc, gen.exitBlock_);
}

void Codegen::StatementVisitor::operator()(CoroutineReturnStatementAST* ast) {
Expand Down
Loading