Skip to content

Commit f784749

Browse files
committed
Emitting a class
1 parent 19aa606 commit f784749

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,72 @@ static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {
997997
return success();
998998
}
999999

1000+
static LogicalResult printOperation(CppEmitter &emitter, ClassOp classOp) {
1001+
CppEmitter::Scope classScope(emitter);
1002+
raw_indented_ostream &os = emitter.ostream();
1003+
os << "class " << classOp.getSymName() << " final {\n";
1004+
os << "public:\n";
1005+
1006+
os.indent();
1007+
for (Operation &op : classOp) {
1008+
if (isa<FieldOp>(op)) {
1009+
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/true)))
1010+
return failure();
1011+
}
1012+
}
1013+
os << "\nconst std::map<std::string, char*> _buffer_map {\n";
1014+
for (Operation &op : classOp) {
1015+
if (auto fieldOp = dyn_cast<FieldOp>(op)) {
1016+
os << " { \"" << fieldOp.getSymName() << "\", reinterpret_cast<char*>(&"
1017+
<< fieldOp.getSymName() << ") },\n";
1018+
}
1019+
}
1020+
os << "};\n";
1021+
1022+
os << "char* getBufferForName(const std::string& name) const {\n";
1023+
os << " auto it = _buffer_map.find(name);\n";
1024+
os << " return (it == _buffer_map.end()) ? nullptr : it->second;\n";
1025+
os << "}\n\n";
1026+
for (Operation &op : classOp) {
1027+
if (!isa<FieldOp>(op)) {
1028+
if (failed(emitter.emitOperation(op, /*trailingSemicolon=*/false)))
1029+
return failure();
1030+
}
1031+
}
1032+
1033+
os.unindent();
1034+
os << "};";
1035+
return success();
1036+
}
1037+
1038+
static LogicalResult printOperation(CppEmitter &emitter, FieldOp fieldOp) {
1039+
raw_ostream &os = emitter.ostream();
1040+
Location loc = fieldOp->getLoc();
1041+
Type type = fieldOp.getType();
1042+
if (failed(emitter.emitType(loc, type)))
1043+
return failure();
1044+
os << " " << fieldOp.getSymName();
1045+
return success();
1046+
}
1047+
1048+
static LogicalResult printOperation(CppEmitter &emitter,
1049+
GetFieldOp getFieldOp) {
1050+
raw_indented_ostream &os = emitter.ostream();
1051+
Location loc = getFieldOp->getLoc();
1052+
1053+
if (getFieldOp->getNumResults() > 0) {
1054+
Value result = getFieldOp->getResult(0);
1055+
if (failed(emitter.emitType(loc, result.getType())))
1056+
return failure();
1057+
os << " ";
1058+
if (failed(emitter.emitOperand(result)))
1059+
return failure();
1060+
os << " = ";
1061+
}
1062+
os << getFieldOp.getFieldName().str();
1063+
return success();
1064+
}
1065+
10001066
static LogicalResult printOperation(CppEmitter &emitter, FileOp file) {
10011067
if (!emitter.shouldEmitFile(file))
10021068
return success();
@@ -1612,7 +1678,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
16121678
emitc::LoadOp, emitc::LogicalAndOp, emitc::LogicalNotOp,
16131679
emitc::LogicalOrOp, emitc::MulOp, emitc::RemOp, emitc::ReturnOp,
16141680
emitc::SubOp, emitc::SwitchOp, emitc::UnaryMinusOp,
1615-
emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp>(
1681+
emitc::UnaryPlusOp, emitc::VariableOp, emitc::VerbatimOp,
1682+
emitc::ClassOp, emitc::FieldOp, emitc::GetFieldOp>(
16161683

16171684
[&](auto op) { return printOperation(*this, op); })
16181685
// Func ops.

0 commit comments

Comments
 (0)