Class: Udb::Condition

Inherits:
AbstractCondition show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/udb/condition.rb,
lib/udb/condition.rb

Overview

represents a condition in the UDB data, which could include conditions involving extensions and/or parameters

Constant Summary collapse

EvalCallbackType =
T.type_alias { T.proc.params(term: TermType).returns(SatisfiedResult) }
Xlen32 =
XlenCondition.new(32)
Xlen64 =
XlenCondition.new(64)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractCondition

#always_implies?, #compatible?, #could_be_satisfied_by_cfg_arch?, #covered_by?, #equivalent?, #ext_req_terms, #implies, #mentions?, #mentions_xlen?, #param_terms, #rv32_only?, #rv64_only?, #to_yaml

Constructor Details

#initialize(yaml, cfg_arch, input_file: nil, input_line: nil) ⇒ Condition

Returns a new instance of Condition.



444
445
446
447
448
449
450
# File 'lib/udb/condition.rb', line 444

def initialize(yaml, cfg_arch, input_file: nil, input_line: nil)
  @yaml = yaml
  @cfg_arch = cfg_arch
  @input_file = input_file
  @input_line = input_line
  @satisfied_by_cfg_arch_memo = T.let({}, T::Hash[ConfiguredArchitecture, SatisfiedResult])
end

Class Method Details

.conjunction(conditions, cfg_arch) ⇒ Object



1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
# File 'lib/udb/condition.rb', line 1524

def self.conjunction(conditions, cfg_arch)
  if conditions.empty?
    AlwaysFalseCondition.new(cfg_arch)
  elsif conditions.size == 1
    conditions.fetch(0)
  else
    Condition.new(
      LogicNode.new(
        LogicNodeType::And,
        conditions.map { |c| c.to_logic_tree_internal }
      ).to_h,
      cfg_arch
    )
  end
end

.disjunction(conditions, cfg_arch) ⇒ Object



1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/udb/condition.rb', line 1548

def self.disjunction(conditions, cfg_arch)
  if conditions.empty?
    AlwaysFalseCondition.new(cfg_arch)
  elsif conditions.size == 1
    conditions.fetch(0)
  else
    Condition.new(
      LogicNode.new(
        LogicNodeType::Or,
        conditions.map { |c| c.to_logic_tree_internal }
      ).to_h,
      cfg_arch
    )
  end
end

.join(cfg_arch, conds) ⇒ Object



425
426
427
428
429
430
431
432
433
# File 'lib/udb/condition.rb', line 425

def self.join(cfg_arch, conds)
  if conds.size == 0
    (AlwaysTrueCondition.new(cfg_arch))
  elsif conds.size == 1
    conds.fetch(0)
  else
    Condition.new({ "allOf" => conds.map(&:to_h) }, cfg_arch)
  end
end

.not(condition, cfg_arch) ⇒ Object



1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
# File 'lib/udb/condition.rb', line 1595

def self.not(condition, cfg_arch)
  if condition.is_a?(AlwaysFalseCondition)
    AlwaysTrueCondition.new(cfg_arch)
  elsif condition.is_a?(AlwaysTrueCondition)
    AlwaysFalseCondition.new(cfg_arch)
  else
    Condition.new(
      LogicNode.new(
        LogicNodeType::Not,
        [condition.to_logic_tree_internal]
      ).to_h,
      cfg_arch
    )
  end
end

.one_of(conditions, cfg_arch) ⇒ Object



1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
# File 'lib/udb/condition.rb', line 1572

def self.one_of(conditions, cfg_arch)
  if conditions.empty?
    AlwaysFalseCondition.new(cfg_arch)
  elsif conditions.size == 1
    conditions.fetch(0)
  else
    Condition.new(
      LogicNode.new(
        LogicNodeType::Xor,
        conditions.map { |c| c.to_logic_tree_internal }
      ).to_h,
      cfg_arch
    )
  end
end

.solverObject



715
716
717
# File 'lib/udb/condition.rb', line 715

def self.solver
  @solver ||= Z3Solver.new
end

.solver_for_arch(cfg_arch) ⇒ Object



896
897
898
899
900
901
902
903
904
905
# File 'lib/udb/condition.rb', line 896

def self.solver_for_arch(cfg_arch)
  return @arch_solvers.fetch(cfg_arch) if @arch_solvers&.key?(cfg_arch)

  @arch_solvers ||= {}

  tree = cfg_arch.arch_condition.to_logic_tree(expand: false)
  s = solver_for(tree, cfg_arch, "arch")

  @arch_solvers[cfg_arch] = s
end

.solver_for_cfg_arch(cfg_arch) ⇒ Object



864
865
866
867
868
869
870
871
872
873
# File 'lib/udb/condition.rb', line 864

def self.solver_for_cfg_arch(cfg_arch)
  return @cfg_arch_solvers[cfg_arch] if @cfg_arch_solvers&.key?(cfg_arch)

  @cfg_arch_solvers ||= {}

  tree = cfg_arch.to_condition.to_logic_tree(expand: false)
  s = solver_for(tree, cfg_arch, "cfg_arch")

  @cfg_arch_solvers[cfg_arch] = s
end

Instance Method Details

#&(other) ⇒ Object



1613
1614
1615
# File 'lib/udb/condition.rb', line 1613

def &(other)
  Condition.conjunction([self, other], @cfg_arch)
end

#-@Object



1623
1624
1625
# File 'lib/udb/condition.rb', line 1623

def -@
  Condition.not(self, @cfg_arch)
end

#empty?Boolean

Returns:

  • (Boolean)


453
# File 'lib/udb/condition.rb', line 453

def empty? = @yaml == true || @yaml == false || @yaml.empty?

#expand_term_requirements(tree, expansion_clauses = [], touched_terms = T.let(Set.new, T::Set[TermType])) ⇒ Object



646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/udb/condition.rb', line 646

def expand_term_requirements(tree, expansion_clauses = [], touched_terms = T.let(Set.new, T::Set[TermType]))
  terms = tree.terms

  terms.each do |term|
    case term
    when ExtensionTerm
      expand_extension_term_requirements(term, expansion_clauses, touched_terms)
    when ParameterTerm
      expand_parameter_term_requirements(term, expansion_clauses, touched_terms)
    else
      #pass
    end
  end

  expansion_clauses
end

#expand_to_enforce_single_ext_ver(tree, expansion_clauses) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/udb/condition.rb', line 545

def expand_to_enforce_single_ext_ver(tree, expansion_clauses)
  # for every mentioned extension, enforce that either zero or one version is ever implemented
  mentioned_ext_terms = (tree.terms.grep(ExtensionTerm) + expansion_clauses.map { |c| c.terms.grep(ExtensionTerm) }.flatten).uniq

  grouped_ext_terms = mentioned_ext_terms.group_by(&:name)

  grouped_ext_terms.each do |ext_name, ext_terms|
    # assuming this comes after expand_extension_version_ranges, so we can ignore ranges
    mentioned_versions = ext_terms.select { |e| e.comparison == ExtensionTerm::ComparisonOp::Equal }
    if mentioned_versions.size > 1
      # add NONE(ext_terms) || XOR(ext_terms)
      expansion_clauses <<
        LogicNode.new(
          LogicNodeType::Or,
          [
            LogicNode.new(
              LogicNodeType::None,
              mentioned_versions.map { |t| LogicNode.new(LogicNodeType::Term, [t]) }
            ),
            LogicNode.new(
              LogicNodeType::Xor,
              mentioned_versions.map { |t| LogicNode.new(LogicNodeType::Term, [t]) }
            )
          ]
        )
    end
  end
end

#failing_conjuncts(cfg_arch, expand: false) ⇒ Object



1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
# File 'lib/udb/condition.rb', line 1297

def failing_conjuncts(cfg_arch, expand: false)
  cb = LogicNode.make_eval_cb do |term|
    case term
    when ExtensionTerm
      if cfg_arch.fully_configured?
        ext_ver = cfg_arch.implemented_extension_version(term.name)
        if ext_ver.nil? || !term.to_ext_req(cfg_arch).satisfied_by?(ext_ver)
          SatisfiedResult::No
        else
          SatisfiedResult::Yes
        end
      elsif cfg_arch.partially_configured?
        ext_req = term.to_ext_req(cfg_arch)
        if cfg_arch.mandatory_extension_reqs.any? { |cfg_ext_req| ext_req.satisfied_by?(cfg_ext_req) }
          SatisfiedResult::Yes
        elsif (cfg_arch.possible_extension_versions_by_name[term.name] || []).any? { |cfg_ext_ver| ext_req.satisfied_by?(cfg_ext_ver) }
          SatisfiedResult::Maybe
        else
          SatisfiedResult::No
        end
      else
        SatisfiedResult::Maybe
      end
    when ParameterTerm
      if cfg_arch.fully_configured?
        if cfg_arch.param_values.key?(term.name)
          term.eval(cfg_arch)
        else
          SatisfiedResult::No
        end
      elsif cfg_arch.partially_configured?
        term.eval(cfg_arch)
      else
        SatisfiedResult::Maybe
      end
    when XlenTerm
      if cfg_arch.possible_xlens.include?(term.xlen)
        if cfg_arch.possible_xlens.size == 1
          SatisfiedResult::Yes
        else
          SatisfiedResult::Maybe
        end
      else
        SatisfiedResult::No
      end
    else
      raise "unexpected term type #{term.class.name}"
    end
  end
  to_logic_tree(expand:).failing_conjuncts(cb).map do |node|
    node.to_s_with_value(cb, format: LogicNode::LogicSymbolFormat::C)
  end
end

#has_extension_requirement?Boolean

Returns:

  • (Boolean)


1008
1009
1010
# File 'lib/udb/condition.rb', line 1008

def has_extension_requirement?
  to_logic_tree(expand: true).terms.any? { |t| t.is_a?(ExtensionVersion) }
end

#has_param?Boolean

Returns:

  • (Boolean)


1003
1004
1005
# File 'lib/udb/condition.rb', line 1003

def has_param?
  to_logic_tree(expand: true).terms.any? { |t| t.is_a?(ParameterTerm) }
end

#implied_extension_conflicts(expand: true) ⇒ Object



1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File 'lib/udb/condition.rb', line 1431

def implied_extension_conflicts(expand: true)
  # strategy:
  #   1. invert extension requiremnts (to get conflicts)
  #   1. convert to product-of-sums.
  #   2. for each product, find the positive terms. These are the conflicts
  #   3. for each product, find the negative terms. These are the "conditions" when the positive terms apply

  @conflicts ||= begin
    conflicts = T.let([], T::Array[ConditionalExtensionRequirement])
    pos = LogicNode.new(LogicNodeType::Not, [to_logic_tree(expand:)]).minimize(LogicNode::CanonicalizationType::ProductOfSums)
    if pos.type == LogicNodeType::Term
      # there are no negative terms, do nothing
    elsif pos.type == LogicNodeType::Not
      single_term = pos.node_children.fetch(0).children.fetch(0)
      if single_term.is_a?(ExtensionTerm)
        conflicts << \
          ConditionalExtensionRequirement.new(
            ext_req: single_term.to_ext_req(@cfg_arch),
            cond: AlwaysTrueCondition.new(@cfg_arch)
          )
      else
        # parameter, do nothing
      end
    elsif pos.type == LogicNodeType::And
      pos.children.each do |child|
        child = T.cast(child, LogicNode)
        if child.type == LogicNodeType::Term
          # not a negative term; do nothing
        elsif child.type == LogicNodeType::Not
          term = child.node_children.fetch(0).children.fetch(0)
          if term.is_a?(ExtensionTerm)
            conflicts << \
              ConditionalExtensionRequirement.new(
                ext_req: term.to_ext_req(@cfg_arch),
                cond: (AlwaysTrueCondition.new(@cfg_arch))
              )
          else
            puts "Not a term: #{term} #{term.class.name}"
          end
        elsif child.children.all? { |child| T.cast(child, LogicNode).type == LogicNodeType::Term }
          # there is no negative term, so do nothing
        else
          raise "? #{child.type}" unless child.type == LogicNodeType::Or

          negative_terms =
            child.node_children.select do |and_child|
              and_child.type == LogicNodeType::Not && and_child.node_children.fetch(0).children.fetch(0).is_a?(ExtensionTerm)
            end.map { |n| n.node_children.fetch(0) }
          cond_terms =
            child.node_children.select { |and_child| and_child.type == LogicNodeType::Term }
          negative_terms.each do |nterm|
            cond_node =
              if cond_terms.empty?
                LogicNode::True
              else
                cond_terms.size == 1 \
                    ? cond_terms.fetch(0)
                    : LogicNode.new(LogicNodeType::Or, cond_terms)
              end

            conflicts << \
              ConditionalExtensionRequirement.new(
                ext_req: T.cast(nterm.children.fetch(0), ExtensionTerm).to_ext_req(@cfg_arch),
                cond: Condition.new(cond_node.to_h, @cfg_arch)
              )
          end
          conflicts
        end
      end
    end
    conflicts
  end
end

#implied_extension_requirements(expand: true) ⇒ Object



1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
# File 'lib/udb/condition.rb', line 1357

def implied_extension_requirements(expand: true)
  # strategy:
  #   1. convert to sum-of-products.
  #   2. for each product, find the positive terms. These are the implications
  #   3. for each product, find the negative terms. These are the "conditions" when the positive terms apply

  @implications ||= begin
    reqs = T.let([], T::Array[ConditionalExtensionRequirement])
    pos = to_logic_tree(expand:).minimize(LogicNode::CanonicalizationType::ProductOfSums)

    if pos.type == LogicNodeType::Term
      single_term = pos.children.fetch(0)
      if single_term.is_a?(ExtensionTerm)
        reqs << ConditionalExtensionRequirement.new(ext_req: single_term.to_ext_req(@cfg_arch), cond: AlwaysTrueCondition.new(@cfg_arch))
      else
        # this is a single parameter, do nothing
      end
    elsif pos.type == LogicNodeType::Not
      # there are no positive terms, do nothing
    elsif pos.type == LogicNodeType::And
      pos.children.each do |child|
        child = T.cast(child, LogicNode)
        if child.type == LogicNodeType::Term
          term = child.children.fetch(0)
          if term.is_a?(ExtensionTerm)
            reqs << \
              ConditionalExtensionRequirement.new(
                ext_req: term.to_ext_req(@cfg_arch),
                cond: AlwaysTrueCondition.new(@cfg_arch)
              )
          end
        elsif child.type == LogicNodeType::Not
          # not a positive term; do nothing
        elsif child.children.all? { |child| T.cast(child, LogicNode).type == LogicNodeType::Not }
          # there is no positive term, so do nothing
        else
          raise "? #{child.type}" unless child.type == LogicNodeType::Or

          positive_terms =
            child.node_children.select do |and_child|
              and_child.type == LogicNodeType::Term && and_child.children.fetch(0).is_a?(ExtensionTerm)
            end
          cond_terms =
            child.node_children.select { |and_child| and_child.type == LogicNodeType::Not }
            .map { |neg_term| neg_term.node_children.fetch(0) }
          cond_terms +=
            child.node_children.select do |and_child|
              and_child.type == LogicNodeType::Term && and_child.children.fetch(0).is_a?(ParameterTerm)
            end.map { |c| LogicNode.new(LogicNodeType::Not, [c]) }
          positive_terms.each do |pterm|
            cond_node =
              if cond_terms.empty?
                LogicNode::True
              else
                cond_terms.size == 1 \
                    ? cond_terms.fetch(0)
                    : LogicNode.new(LogicNodeType::And, cond_terms)
              end

            reqs << \
              ConditionalExtensionRequirement.new(
                ext_req: T.cast(pterm.children.fetch(0), ExtensionTerm).to_ext_req(@cfg_arch),
                cond: Condition.new(cond_node.to_h, @cfg_arch)
              )
          end
          reqs
        end
      end
    end
    reqs
  end
end

#make_cb_proc(&blk) ⇒ Object



1015
1016
1017
# File 'lib/udb/condition.rb', line 1015

def make_cb_proc(&blk)
  blk
end

#minimize(expand: true) ⇒ Object



940
941
942
# File 'lib/udb/condition.rb', line 940

def minimize(expand: true)
  Condition.new(to_logic_tree(expand:).minimize(LogicNode::CanonicalizationType::ProductOfSums).to_h, @cfg_arch)
end

#partial_eval(ext_reqs: [], expand: true) ⇒ Object



1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
# File 'lib/udb/condition.rb', line 1205

def partial_eval(ext_reqs: [], expand: true)
  cb = LogicNode.make_replace_cb do |node|
    if node.type == LogicNodeType::Term
      term = node.children.fetch(0)
      if term.is_a?(ExtensionTerm)
        term_ext_req = term.to_ext_req(@cfg_arch)
        if ext_reqs.any? { |ext_req| term_ext_req.satisfied_by?(ext_req) }
          next LogicNode::True
        end
      end
    end
    node
  end
  LogicCondition.new(to_logic_tree(expand:).replace_terms(cb), @cfg_arch)
end

#partially_evaluate_for_params(cfg_arch, expand:) ⇒ Object



1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'lib/udb/condition.rb', line 1021

def partially_evaluate_for_params(cfg_arch, expand:)
  cb = make_cb_proc do |term|
    if term.is_a?(ExtensionTerm)
      SatisfiedResult::Maybe
    elsif term.is_a?(ParameterTerm)
      term.partial_eval(cfg_arch.config.param_values)
    elsif term.is_a?(FreeTerm)
      raise "unreachable"
    elsif term.is_a?(XlenTerm)
      # can't use cfg_arch.possible_xlens because of an initialization circular dependency in figuring out
      # is S/U is implemented
      if term.xlen == 32
        if cfg_arch.mxlen.nil?
          SatisfiedResult::Maybe
        elsif cfg_arch.mxlen == 32
          SatisfiedResult::Yes
        else
          # mxlen == 64. can some other mode be 32?
          if !cfg_arch.config.param_values.key?("SXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("SXLEN"), T::Array[Integer]).include?(32)
            SatisfiedResult::Yes
          elsif !cfg_arch.config.param_values.key?("UXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("UXLEN"), T::Array[Integer]).include?(32)
            SatisfiedResult::Yes
          elsif !cfg_arch.config.param_values.key?("VSXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("VSXLEN"), T::Array[Integer]).include?(32)
            SatisfiedResult::Yes
          elsif !cfg_arch.config.param_values.key?("VUXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("VUXLEN"), T::Array[Integer]).include?(32)
            SatisfiedResult::Yes
          else
            SatisfiedResult::No
          end
        end
      elsif term.xlen == 64
        if cfg_arch.mxlen.nil?
          SatisfiedResult::Maybe
        elsif cfg_arch.mxlen == 32
          SatisfiedResult::No
        else
          # mxlen == 64. can some other mode be 32?
          if !cfg_arch.config.param_values.key?("SXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("SXLEN"), T::Array[Integer]) == [32]
            SatisfiedResult::Maybe
          elsif !cfg_arch.config.param_values.key?("UXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("UXLEN"), T::Array[Integer]) == [32]
            SatisfiedResult::Maybe
          elsif !cfg_arch.config.param_values.key?("VSXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("VSXLEN"), T::Array[Integer]) == [32]
            SatisfiedResult::Maybe
          elsif !cfg_arch.config.param_values.key?("VUXLEN")
            SatisfiedResult::Maybe
          elsif T.cast(cfg_arch.config.param_values.fetch("VUXLEN"), T::Array[Integer]) == [32]
            SatisfiedResult::Maybe
          else
            SatisfiedResult::Yes
          end
        end
      else
        raise "term.xlen is not 32 or 64"
      end
    else
      T.absurd(term)
    end
  end

  Condition.new(
    to_logic_tree(expand:).partial_evaluate(cb).to_h,
    cfg_arch
  )
end

#sat_arch_model(cfg_arch) ⇒ Object



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# File 'lib/udb/condition.rb', line 841

def sat_arch_model(cfg_arch)
  s = Condition.solver_for_arch(cfg_arch)
  s.push
  tree = to_logic_tree(expand: false)
  if tree.type == LogicNodeType::And
    tree.node_children.each do |child|
      s.assert_as child.to_z3(cfg_arch, s), child.to_s
    end
  else
    s.assert_as tree.to_z3(cfg_arch, s), tree.to_s
  end
  result = s.satisfiable?
  if result
    m = s.model
    s.pop
    m
  else
    s.pop
    nil
  end
end

#satisfiability_depends_on_ext_req?(ext_req, include_requirements: false) ⇒ Boolean

Returns:

  • (Boolean)


1196
1197
1198
1199
1200
1201
1202
# File 'lib/udb/condition.rb', line 1196

def satisfiability_depends_on_ext_req?(ext_req, include_requirements: false)
  if include_requirements
    (self & -ext_req.to_condition & -ext_req.requirements_condition).satisfiable? == false
  else
    (self & -ext_req.to_condition).satisfiable? == false
  end
end

#satisfiable?Boolean

Returns:

  • (Boolean)


929
930
931
# File 'lib/udb/condition.rb', line 929

def satisfiable?
  solver { |s| s.satisfiable? }
end

#satisfiable_by_arch?(cfg_arch) ⇒ Boolean

Returns:

  • (Boolean)


908
909
910
911
912
913
914
915
# File 'lib/udb/condition.rb', line 908

def satisfiable_by_arch?(cfg_arch)
  s = Condition.solver_for_arch(cfg_arch)
  s.push
  s.assert to_logic_tree(expand: false).to_z3(cfg_arch, s)
  result = s.satisfiable?
  s.pop
  result
end

#satisfiable_by_cfg_arch?(cfg_arch) ⇒ Boolean

Returns:

  • (Boolean)


876
877
878
879
880
881
882
883
# File 'lib/udb/condition.rb', line 876

def satisfiable_by_cfg_arch?(cfg_arch)
  s = Condition.solver_for_cfg_arch(cfg_arch)
  s.push
  s.assert to_logic_tree(expand: false).to_z3(@cfg_arch, s)
  result = s.satisfiable?
  s.pop
  result
end

#satisfied_by_cfg_arch?(cfg_arch) ⇒ Boolean

Returns:

  • (Boolean)


1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
# File 'lib/udb/condition.rb', line 1101

def satisfied_by_cfg_arch?(cfg_arch)
  @satisfied_by_cfg_arch_memo[cfg_arch] ||=
    if cfg_arch.fully_configured?
      implemented_ext_cb = make_cb_proc do |term|
        if term.is_a?(ExtensionTerm)
          ext_ver = cfg_arch.implemented_extension_version(term.name)
          next SatisfiedResult::No if ext_ver.nil?
          term.to_ext_req(cfg_arch).satisfied_by?(ext_ver) \
            ? SatisfiedResult::Yes
            : SatisfiedResult::No
        elsif term.is_a?(ParameterTerm)
          if cfg_arch.param_values.key?(term.name)
            result = term.eval(cfg_arch)
            result = resolve_param_term_from_maybe(term, cfg_arch) if result == SatisfiedResult::Maybe
            result
          else
            SatisfiedResult::No
          end
        elsif term.is_a?(FreeTerm)
          raise "unreachable"
        elsif term.is_a?(XlenTerm)
          if cfg_arch.possible_xlens.include?(term.xlen)
            if cfg_arch.possible_xlens.size == 1
              SatisfiedResult::Yes
            else
              SatisfiedResult::Maybe
            end
          else
            SatisfiedResult::No
          end
        else
          T.absurd(term)
        end
      end
      if to_logic_tree(expand: false).eval_cb(implemented_ext_cb) == SatisfiedResult::Yes
        SatisfiedResult::Yes
      else
        SatisfiedResult::No
      end
    elsif cfg_arch.partially_configured?
      cb = make_cb_proc do |term|
        if term.is_a?(ExtensionTerm)
          ext_req = term.to_ext_req(cfg_arch)
          if cfg_arch.mandatory_extension_reqs.any? { |cfg_ext_req| ext_req.satisfied_by?(cfg_ext_req) }
            SatisfiedResult::Yes
          elsif (cfg_arch.possible_extension_versions_by_name[term.name] || []).any? { |cfg_ext_ver| ext_req.satisfied_by?(cfg_ext_ver) }
            SatisfiedResult::Maybe
          else
            SatisfiedResult::No
          end
        elsif term.is_a?(ParameterTerm)
          result = term.eval(cfg_arch)
          result = resolve_param_term_from_maybe(term, cfg_arch) if result == SatisfiedResult::Maybe
          result
        elsif term.is_a?(FreeTerm)
          raise "unreachable"
        elsif term.is_a?(XlenTerm)
          if cfg_arch.possible_xlens.include?(term.xlen)
            if cfg_arch.possible_xlens.size == 1
              SatisfiedResult::Yes
            else
              SatisfiedResult::Maybe
            end
          else
            SatisfiedResult::No
          end
        else
          T.absurd(term)
        end
      end

      to_logic_tree(expand: false).eval_cb(cb)
    else
      # unconfig. Can't really say anthing
      SatisfiedResult::Maybe
    end
end

#satisfied_by_ext_req?(ext_req, include_requirements: false) ⇒ Boolean

Returns:

  • (Boolean)


1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
# File 'lib/udb/condition.rb', line 1180

def satisfied_by_ext_req?(ext_req, include_requirements: false)
  cb = make_cb_proc do |term|
    if term.is_a?(ExtensionTerm)
      if term.to_ext_req(@cfg_arch).satisfied_by?(ext_req)
        SatisfiedResult::Yes
      else
        SatisfiedResult::No
      end
    else
      SatisfiedResult::No
    end
  end
  ext_req.to_condition.to_logic_tree(expand: include_requirements).eval_cb(cb) == SatisfiedResult::Yes
end

#solver(&blk) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/udb/condition.rb', line 724

def solver(&blk)
  s = Condition.solver
  s.push
  s.assert to_logic_tree(expand: false).to_z3(@cfg_arch, s)
  expansion_clauses = expand_term_requirements(to_logic_tree(expand: false))
  expansion_clauses.each do |clause|
    s.assert clause.to_z3(@cfg_arch, s)
  end
  # puts "-----------------------"
  # puts s.assertions
  # puts "^^^^^^^^^^^^^^^^^^^^^^"

  ret = yield s

  s.pop

  ret
end

#to_asciidocObject



1352
1353
1354
# File 'lib/udb/condition.rb', line 1352

def to_asciidoc
  to_logic_tree(expand: false).to_asciidoc(include_versions: false)
end

#to_expanded_logic_tree_shallowObject



663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'lib/udb/condition.rb', line 663

def to_expanded_logic_tree_shallow
  @expanded_logic_tree_shallow ||=
    begin
      starting_tree = to_logic_tree_internal

      expansion_clauses = expand_term_requirements(starting_tree)

      if expansion_clauses.empty?
        starting_tree
      else
        LogicNode.new(LogicNodeType::And, [starting_tree] + expansion_clauses)
      end
    end
end

#to_hObject



1222
1223
1224
# File 'lib/udb/condition.rb', line 1222

def to_h
  to_logic_tree(expand: false).to_h
end

#to_idl(cfg_arch) ⇒ Object



1227
1228
1229
1230
# File 'lib/udb/condition.rb', line 1227

def to_idl(cfg_arch)
  idl = to_logic_tree(expand: false).to_idl(cfg_arch)
  "-> #{idl};"
end

#to_logic_tree(expand:) ⇒ Object



679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/udb/condition.rb', line 679

def to_logic_tree(expand:)
  if expand
    @logic_tree_expanded ||=
      begin
        # we do several things in expansion:
        #
        #  1. expand any requirements of extensions/parameters
        #  2. ensure XLEN is exclusive (can be 32 or 64, but not both)
        #  3. ensure zero or one version of an extension can be implemented
        starting_tree = to_logic_tree_internal

        expansion_clauses = expand_term_requirements(starting_tree)

        expand_extension_version_ranges(starting_tree, expansion_clauses)

        # enforce_single_ext_ver must come after expand_extension_version_ranges
        expand_to_enforce_single_ext_ver(starting_tree, expansion_clauses)

        expand_to_enforce_param_relations(starting_tree, expansion_clauses)

        expand_xlen(starting_tree, expansion_clauses)

        expanded_tree =
          if expansion_clauses.empty?
            starting_tree
          else
            LogicNode.new(LogicNodeType::And, [starting_tree] + expansion_clauses)
          end

        expanded_tree
      end
  else
    @logic_tree_unexpanded ||= to_logic_tree_helper(@yaml)
  end
end

#to_logic_tree_internalObject



949
950
951
# File 'lib/udb/condition.rb', line 949

def to_logic_tree_internal
  to_logic_tree_helper(@yaml)
end

#to_s(expand: false) ⇒ Object



1233
1234
1235
# File 'lib/udb/condition.rb', line 1233

def to_s(expand: false)
  to_logic_tree(expand:).to_s(format: LogicNode::LogicSymbolFormat::C)
end

#to_s_prettyObject



1239
1240
1241
# File 'lib/udb/condition.rb', line 1239

def to_s_pretty
  to_logic_tree(expand: false).to_s_pretty
end

#to_s_with_value(cfg_arch, expand: false) ⇒ Object



1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'lib/udb/condition.rb', line 1244

def to_s_with_value(cfg_arch, expand: false)
  cb = LogicNode.make_eval_cb do |term|
    case term
    when ExtensionTerm
      if cfg_arch.fully_configured?
        ext_ver = cfg_arch.implemented_extension_version(term.name)
        if ext_ver.nil? || !term.to_ext_req(cfg_arch).satisfied_by?(ext_ver)
          SatisfiedResult::No
        else
          SatisfiedResult::Yes
        end
      elsif cfg_arch.partially_configured?
        ext_req = term.to_ext_req(cfg_arch)
        if cfg_arch.mandatory_extension_reqs.any? { |cfg_ext_req| ext_req.satisfied_by?(cfg_ext_req) }
          SatisfiedResult::Yes
        elsif (cfg_arch.possible_extension_versions_by_name[term.name] || []).any? { |cfg_ext_ver| ext_req.satisfied_by?(cfg_ext_ver) }
          SatisfiedResult::Maybe
        else
          SatisfiedResult::No
        end
      else
        SatisfiedResult::Maybe
      end
    when ParameterTerm
      if cfg_arch.fully_configured?
        if cfg_arch.param_values.key?(term.name)
          term.eval(cfg_arch)
        else
          SatisfiedResult::No
        end
      elsif cfg_arch.partially_configured?
        term.eval(cfg_arch)
      else
        SatisfiedResult::Maybe
      end
    when XlenTerm
      if cfg_arch.possible_xlens.include?(term.xlen)
        if cfg_arch.possible_xlens.size == 1
          SatisfiedResult::Yes
        else
          SatisfiedResult::Maybe
        end
      else
        SatisfiedResult::No
      end
    else
      raise "unexpected term type #{term.class.name}"
    end
  end
  to_logic_tree(expand:).to_s_with_value(cb, format: LogicNode::LogicSymbolFormat::C)
end

#unsat_arch_core(cfg_arch) ⇒ Object



805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
# File 'lib/udb/condition.rb', line 805

def unsat_arch_core(cfg_arch)
  s = Condition.solver_for_arch(cfg_arch)
  s.push
  tree = to_logic_tree(expand: false)
  if tree.type == LogicNodeType::And
    tree.node_children.each do |child|
      s.assert_as child.to_z3(cfg_arch, s), child.to_s
    end
  else
    s.assert_as tree.to_z3(cfg_arch, s), tree.to_s
  end
  result = s.unsatisfiable?
  if result
    core = Z3::LowLevel.solver_get_unsat_core(s.solver)
    s.pop
    return Z3::LowLevel.unpack_ast_vector(core)
  end
  raise "Cond is not unsatisfiable"
end

#unsatisfiable?Boolean

Returns:

  • (Boolean)


935
936
937
# File 'lib/udb/condition.rb', line 935

def unsatisfiable?
  solver { |s| s.unsatisfiable? }
end

#unsatisfiable_by_arch?(cfg_arch) ⇒ Boolean

Returns:

  • (Boolean)


918
919
920
921
922
923
924
925
# File 'lib/udb/condition.rb', line 918

def unsatisfiable_by_arch?(cfg_arch)
  s = Condition.solver_for_arch(cfg_arch)
  s.push
  s.assert to_logic_tree(expand: false).to_z3(cfg_arch, s)
  result = s.unsatisfiable?
  s.pop
  result
end

#unsatisfiable_by_cfg_arch?(cfg_arch) ⇒ Boolean

Returns:

  • (Boolean)


886
887
888
889
890
891
892
893
# File 'lib/udb/condition.rb', line 886

def unsatisfiable_by_cfg_arch?(cfg_arch)
  s = Condition.solver_for_cfg_arch(cfg_arch)
  s.push
  s.assert to_logic_tree(expand: false).to_z3(@cfg_arch, s)
  result = s.unsatisfiable?
  s.pop
  result
end

#z3_assertions(cfg_arch) ⇒ Object



825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# File 'lib/udb/condition.rb', line 825

def z3_assertions(cfg_arch)
  s = Condition.solver_for_arch(cfg_arch)
  s.push
  tree = to_logic_tree(expand: false)
  if tree.type == LogicNodeType::And
    tree.node_children.each do |child|
      s.assert_as child.to_z3(cfg_arch, s), child.to_s
    end
  else
    s.assert_as tree.to_z3(cfg_arch, s), tree.to_s
  end
  assertions = s.assertions
  s.pop
  assertions
end

#|(other) ⇒ Object



1618
1619
1620
# File 'lib/udb/condition.rb', line 1618

def |(other)
  Condition.disjunction([self, other], @cfg_arch)
end