CIRCT  19.0.0git
LLHDTypes.cpp
Go to the documentation of this file.
1 //===- LLHDTypes.cpp - LLHD types and attributes code defs ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation for LLHD data types.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "mlir/IR/Builders.h"
17 #include "mlir/IR/DialectImplementation.h"
18 #include "llvm/ADT/TypeSwitch.h"
19 
20 using namespace circt::llhd;
21 using namespace mlir;
22 
23 #define GET_TYPEDEF_CLASSES
24 #include "circt/Dialect/LLHD/IR/LLHDTypes.cpp.inc"
25 
26 #define GET_ATTRDEF_CLASSES
27 #include "circt/Dialect/LLHD/IR/LLHDAttributes.cpp.inc"
28 
29 //===----------------------------------------------------------------------===//
30 // Time Attribute
31 //===----------------------------------------------------------------------===//
32 
33 /// Parse a time attribute.
34 /// Syntax: timeattr ::= #llhd.time<[time][timeUnit], [delta]d, [epsilon]e>
35 Attribute TimeAttr::parse(AsmParser &p, Type type) {
36  llvm::StringRef timeUnit;
37  unsigned time = 0;
38  unsigned delta = 0;
39  unsigned eps = 0;
40 
41  // parse the time value
42  if (p.parseLess() || p.parseInteger(time) || p.parseKeyword(&timeUnit))
43  return {};
44 
45  // parse the delta step value
46  if (p.parseComma() || p.parseInteger(delta) || p.parseKeyword("d"))
47  return {};
48 
49  // parse the epsilon value
50  if (p.parseComma() || p.parseInteger(eps) || p.parseKeyword("e") ||
51  p.parseGreater())
52  return {};
53 
54  // return a new instance of time attribute
55  auto loc = p.getEncodedSourceLoc(p.getCurrentLocation());
56  return getChecked(mlir::detail::getDefaultDiagnosticEmitFn(loc),
57  p.getContext(), time, timeUnit, delta, eps);
58 }
59 
60 void TimeAttr::print(AsmPrinter &p) const {
61  p << "<" << getTime() << getTimeUnit() << ", " << getDelta() << "d, "
62  << getEpsilon() << "e>";
63 }
64 
65 LogicalResult TimeAttr::verify(function_ref<InFlightDiagnostic()> emitError,
66  TimeType type, unsigned time,
67  llvm::StringRef timeUnit, unsigned delta,
68  unsigned epsilon) {
69  // Check the time unit is a legal SI unit
70  std::vector<std::string> legalUnits{"ys", "zs", "as", "fs", "ps",
71  "ns", "us", "ms", "s"};
72  if (std::find(legalUnits.begin(), legalUnits.end(), timeUnit) ==
73  legalUnits.end())
74  return emitError() << "Illegal time unit.";
75 
76  return success();
77 }
78 
79 //===----------------------------------------------------------------------===//
80 // Register attributes and types to the LLHD Dialect
81 //===----------------------------------------------------------------------===//
82 
83 void LLHDDialect::registerTypes() {
84  addTypes<
85 #define GET_TYPEDEF_LIST
86 #include "circt/Dialect/LLHD/IR/LLHDTypes.cpp.inc"
87  >();
88 }
89 
90 void LLHDDialect::registerAttributes() {
91  addAttributes<
92 #define GET_ATTRDEF_LIST
93 #include "circt/Dialect/LLHD/IR/LLHDAttributes.cpp.inc"
94  >();
95 }
static LogicalResult verify(Value clock, bool eventExists, mlir::Location loc)
Definition: SVOps.cpp:2443