Skip to content

Commit

Permalink
partially parse a .names
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Lenharth committed Dec 31, 2024
1 parent 5a60636 commit 58f041c
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/circt/Dialect/BLIF/BLIFOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def LogicGateOp: BLIFOp<"logic_gate", []> {
means don't use the input.
}];

let arguments = (ins IntArrayProperty<I8Property>:$func,
let arguments = (ins /* IntArrayProperty<I8Property>:$func, */
Variadic<I1>:$inputs);
let results = (outs I1:$result);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Dialect/BLIF/Import/BLIFLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class BLIFToken {
bool isKeyword() const;

bool isModelHeaderKeyword() const {
return isAny(kw_inputs, kw_outputs, kw_clock);
return isAny(kw_inputs, kw_outputs, kw_clocks, kw_input, kw_output,
kw_clock);
}

/// Given a token containing a string literal, return its value, including
Expand Down
98 changes: 89 additions & 9 deletions lib/Dialect/BLIF/Import/BLIFParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,14 @@ struct BLIFModelParser : public BLIFParser {
explicit BLIFModelParser(Block &blockToInsertInto, BLIFLexer &lexer)
: BLIFParser(blockToInsertInto.getParentOp()->getContext(), lexer),
builder(UnknownLoc::get(getContext()), getContext()) {
builder.setInsertionPointToEnd(&blockToInsertInto);
builder.setInsertionPoint(&blockToInsertInto, blockToInsertInto.begin());
unresolved = builder
.create<mlir::UnrealizedConversionCastOp>(
builder.getIntegerType(1), ValueRange())
.getResult(0);
}

ParseResult parseSimpleStmt();
ParseResult parseSimpleStmtBlock();
ParseResult parseModelBody();

private:
ParseResult parseSimpleStmtImpl();
Expand All @@ -294,17 +297,54 @@ struct BLIFModelParser : public BLIFParser {
ParseResult parseModelReference();
ParseResult parseModelCmdImpl();
ParseResult parseModelCmd();
ParseResult parseModelBody();

// Helper to fetch a module referenced by an instance-like statement.
Value getReferencedModel(SMLoc loc, StringRef modelName);

/// Find the value corrosponding to the named identifier.
Value getNamedValue(StringRef name);

/// Set the value corrosponding to the named identifier.
void setNamedValue(StringRef name, Value value);

/// Remember to fix an op whose value could not be resolved
void addFixup(Operation *op, unsigned idx, StringRef name);

// The builder to build into.
ImplicitLocOpBuilder builder;

/// Keep track of names;
DenseMap<StringRef, Value> namedValues;

struct fixup {
Operation *op;
unsigned idx;
StringRef name;
};

DenseMap<StringRef, SmallVector<fixup>> fixups;

Value unresolved;
};

} // end anonymous namespace

Value BLIFModelParser::getNamedValue(StringRef name) {
auto &val = namedValues[name];
if (!val)
val = unresolved;
return val;
}

void BLIFModelParser::setNamedValue(StringRef name, Value value) {
assert(!namedValues[name] && "name already exists");
namedValues[name] = value;
}

void BLIFModelParser::addFixup(Operation *op, unsigned idx, StringRef name) {
fixups[name].push_back({op, idx, name});
}

Value BLIFModelParser::getReferencedModel(SMLoc loc, StringRef modelName) {
return {};
}
Expand All @@ -320,7 +360,19 @@ ParseResult BLIFModelParser::parseLogicGate() {
output = inputs.back();
inputs.pop_back();
// TODO: READ THE TRUTH TABLE
// builder.create<LogicGateOp>(loc, inputs, output);
// Resolve Inputs
SmallVector<Value> inputValues;
for (auto input : inputs)
inputValues.push_back(getNamedValue(input));
// BuildOp
auto op = builder.create<LogicGateOp>(builder.getIntegerType(1), inputValues);
// Record output name
setNamedValue(output, op.getResult());
// Record Fixups
for (auto [idx, name, val] : llvm::enumerate(inputs, inputValues)) {
if (val == unresolved)
addFixup(op, idx, name);
}
return success();
}

Expand Down Expand Up @@ -394,7 +446,6 @@ ParseResult BLIFModelParser::parseModelCmd() {

// Parse the body of this module.
ParseResult BLIFModelParser::parseModelBody() {
// auto &body = moduleOp->getRegion(0).front();

while (true) {
// The outer level parser can handle these tokens.
Expand Down Expand Up @@ -449,16 +500,40 @@ ParseResult BLIFFileParser::parseModel() {
if (parseIdList(inputs, "expected input list", 1))
return failure();
break;
case BLIFToken::kw_input: {
consumeToken(BLIFToken::kw_input);
StringRef tmp;
if (parseId(tmp, "expected input"))
return failure();
inputs.push_back(tmp);
break;
}
case BLIFToken::kw_outputs:
consumeToken(BLIFToken::kw_outputs);
if (parseIdList(outputs, "expected output list", 1))
return failure();
break;
case BLIFToken::kw_clock:
consumeToken(BLIFToken::kw_clock);
case BLIFToken::kw_output: {
consumeToken(BLIFToken::kw_output);
StringRef tmp;
if (parseId(tmp, "expected output"))
return failure();
outputs.push_back(tmp);
break;
}
case BLIFToken::kw_clocks:
consumeToken(BLIFToken::kw_clocks);
if (parseIdList(clocks, "expected clock list", 1))
return failure();
break;
case BLIFToken::kw_clock: {
consumeToken(BLIFToken::kw_clock);
StringRef tmp;
if (parseId(tmp, "expected clock"))
return failure();
clocks.push_back(tmp);
break;
}
default:
break;
}
Expand All @@ -469,9 +544,14 @@ ParseResult BLIFFileParser::parseModel() {
builder.setInsertionPointToEnd(&m.getBody().back());
builder.create<OutputOp>(ValueRange());

if (/*parseModelBody() ||*/ parseToken(BLIFToken::kw_end, "expected .end"))
BLIFModelParser bodyParser(m.getBody().back(), getLexer());

if (bodyParser.parseModelBody() ||
parseToken(BLIFToken::kw_end, "expected .end"))
return failure();

// Fix up outputs

return success();
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Dialect/BLIF/Import/BLIFTokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ TOK_PUNCTUATION(equal_greater, "=>")
TOK_KEYWORD_DOT(area)
TOK_KEYWORD_DOT(cycle)
TOK_KEYWORD_DOT(clock)
TOK_KEYWORD_DOT(clocks)
TOK_KEYWORD_DOT(clock_event)
TOK_KEYWORD_DOT(default_input_arrival)
TOK_KEYWORD_DOT(default_input_drive)
Expand All @@ -92,11 +93,13 @@ TOK_KEYWORD_DOT(default_output_required)
TOK_KEYWORD_DOT(delay)
TOK_KEYWORD_DOT(end)
TOK_KEYWORD_DOT(end_kiss)
TOK_KEYWORD_DOT(input)
TOK_KEYWORD_DOT(inputs)
TOK_KEYWORD_DOT(input_arrival)
TOK_KEYWORD_DOT(input_drive)
TOK_KEYWORD_DOT(max_input_load)
TOK_KEYWORD_DOT(model)
TOK_KEYWORD_DOT(output)
TOK_KEYWORD_DOT(outputs)
TOK_KEYWORD_DOT(output_load)
TOK_KEYWORD_DOT(output_required)
Expand Down
10 changes: 7 additions & 3 deletions test/Dialect/BLIF/parse-basic.blif
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# RUN: circt-translate -import-blif -verify-diagnostics -split-input-file %s | circt-opt | FileCheck %s

.model simple
.inputs a B
.outputs c
#.names a b c
.input a
.output b
.clock clk1
.inputs c d
.outputs e f
.clocks clk2 clk3
.names a b c
#11 1
.end

Expand Down

0 comments on commit 58f041c

Please sign in to comment.