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
68 changes: 66 additions & 2 deletions src/mlir/cxx/mlir/CxxOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ def Cxx_IntegralCastOp : Cxx_Op<"integral_cast"> {
}

def Cxx_NotOp : Cxx_Op<"not"> {
let arguments = (ins Cxx_BoolType:$value);
let arguments = (ins AnyType:$value);

let results = (outs Cxx_BoolType:$result);
let results = (outs AnyType:$result);
}

def Cxx_AddIOp : Cxx_Op<"addi"> {
Expand All @@ -212,6 +212,70 @@ def Cxx_MulIOp : Cxx_Op<"muli"> {
let results = (outs Cxx_IntegerType:$result);
}

def Cxx_DivIOp : Cxx_Op<"divi"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_IntegerType:$result);
}

def Cxx_ModIOp : Cxx_Op<"mod"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_IntegerType:$result);
}

def Cxx_ShiftLeftOp : Cxx_Op<"shl"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_IntegerType:$result);
}

def Cxx_ShiftRightOp : Cxx_Op<"shr"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_IntegerType:$result);
}

def Cxx_EqualOp : Cxx_Op<"eq"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_BoolType:$result);
}

def Cxx_NotEqualOp : Cxx_Op<"ne"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_BoolType:$result);
}

def Cxx_LessThanOp : Cxx_Op<"lt"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_BoolType:$result);
}

def Cxx_LessEqualOp : Cxx_Op<"le"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_BoolType:$result);
}

def Cxx_GreaterThanOp : Cxx_Op<"gt"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_BoolType:$result);
}

def Cxx_GreaterEqualOp : Cxx_Op<"ge"> {
let arguments = (ins Cxx_IntegerType:$lhs, Cxx_IntegerType:$rhs);

let results = (outs Cxx_BoolType:$result);
}

//
// control flow ops
//

def Cxx_LabelOp : Cxx_Op<"label"> {
let arguments = (ins StringProp:$name);
}
Expand Down
8 changes: 4 additions & 4 deletions src/mlir/cxx/mlir/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ void Codegen::branch(mlir::Location loc, mlir::Block* block,
}

auto Codegen::findOrCreateLocal(Symbol* symbol) -> std::optional<mlir::Value> {
auto var = symbol_cast<VariableSymbol>(symbol);
if (!var) return std::nullopt;

if (auto local = locals_.find(var); local != locals_.end()) {
if (auto local = locals_.find(symbol); local != locals_.end()) {
return local->second;
}

auto var = symbol_cast<VariableSymbol>(symbol);
if (!var) return std::nullopt;

auto type = convertType(var->type());
auto ptrType = builder_.getType<mlir::cxx::PointerType>(type);

Expand Down
26 changes: 26 additions & 0 deletions src/mlir/cxx/mlir/codegen_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cxx/ast.h>
#include <cxx/control.h>
#include <cxx/external_name_encoder.h>
#include <cxx/scope.h>
#include <cxx/symbols.h>
#include <cxx/translation_unit.h>
#include <cxx/types.h>
Expand Down Expand Up @@ -375,6 +376,31 @@ auto Codegen::DeclarationVisitor::operator()(FunctionDefinitionAST* ast)
std::swap(gen.exitValue_, exitValue);
std::swap(gen.locals_, locals);

FunctionParametersSymbol* params = nullptr;
for (auto member : ast->symbol->scope()->symbols()) {
params = symbol_cast<FunctionParametersSymbol>(member);
if (!params) continue;

int argc = 0;
auto args = entryBlock->getArguments();
for (auto param : params->scope()->symbols()) {
auto arg = symbol_cast<ParameterSymbol>(param);
if (!arg) continue;

auto type = gen.convertType(arg->type());
auto ptrType = gen.builder_.getType<mlir::cxx::PointerType>(type);

auto loc = gen.getLocation(arg->location());
auto allocaOp = gen.builder_.create<mlir::cxx::AllocaOp>(loc, ptrType);

auto value = args[argc];
++argc;
gen.builder_.create<mlir::cxx::StoreOp>(loc, value, allocaOp);

gen.locals_.emplace(arg, allocaOp);
}
}

// generate code for the function body
auto functionBodyResult = gen.functionBody(ast->functionBody);

Expand Down
144 changes: 143 additions & 1 deletion src/mlir/cxx/mlir/codegen_expressions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ auto Codegen::ExpressionVisitor::operator()(LabelAddressExpressionAST* ast)
auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast)
-> ExpressionResult {
switch (ast->op) {
case cxx::TokenKind::T_EXCLAIM: {
case TokenKind::T_EXCLAIM: {
if (type_cast<BoolType>(control()->remove_cv(ast->type))) {
auto loc = gen.getLocation(ast->opLoc);
auto expressionResult = gen.expression(ast->expression);
Expand All @@ -690,6 +690,38 @@ auto Codegen::ExpressionVisitor::operator()(UnaryExpressionAST* ast)
break;
}

case TokenKind::T_PLUS: {
// unary plus, no-op
auto expressionResult = gen.expression(ast->expression);
return expressionResult;
}

case TokenKind::T_MINUS: {
// unary minus
auto expressionResult = gen.expression(ast->expression);
auto resultType = gen.convertType(ast->type);

auto loc = gen.getLocation(ast->opLoc);
auto zero =
gen.builder_.create<mlir::cxx::IntConstantOp>(loc, resultType, 0);
auto op = gen.builder_.create<mlir::cxx::SubIOp>(loc, resultType, zero,
expressionResult.value);

return {op};
}

case TokenKind::T_TILDE: {
// unary bitwise not
auto expressionResult = gen.expression(ast->expression);
auto resultType = gen.convertType(ast->type);

auto loc = gen.getLocation(ast->opLoc);
auto op = gen.builder_.create<mlir::cxx::NotOp>(loc, resultType,
expressionResult.value);

return {op};
}

default:
break;
} // switch
Expand Down Expand Up @@ -928,6 +960,116 @@ auto Codegen::ExpressionVisitor::operator()(BinaryExpressionAST* ast)
break;
}

case TokenKind::T_SLASH: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::DivIOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_PERCENT: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::ModIOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_LESS_LESS: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::ShiftLeftOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_GREATER_GREATER: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::ShiftRightOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_EQUAL_EQUAL: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::EqualOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_EXCLAIM_EQUAL: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::NotEqualOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_LESS: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::LessThanOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_LESS_EQUAL: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::LessEqualOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_GREATER: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::GreaterThanOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

case TokenKind::T_GREATER_EQUAL: {
if (control()->is_integral(ast->type)) {
auto op = gen.builder_.create<mlir::cxx::GreaterEqualOp>(
loc, resultType, leftExpressionResult.value,
rightExpressionResult.value);
return {op};
}

break;
}

default:
break;
} // switch
Expand Down
Loading
Loading