Class: Udb::ParameterTerm

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Comparable
Defined in:
lib/udb/logic.rb

Overview

a terminal for a Parameter test (e.g., MXLEN == 32)

Defined Under Namespace

Classes: ParameterComparisonType

Constant Summary collapse

ValueType =
T.type_alias { T.any(Integer, String, T::Boolean, T::Array[T.any(Integer, String, T::Boolean)]) }

Instance Method Summary collapse

Constructor Details

#initialize(yaml) ⇒ ParameterTerm

Returns a new instance of ParameterTerm.



325
326
327
328
# File 'lib/udb/logic.rb', line 325

def initialize(yaml)
  @yaml = yaml
  @yaml_no_reason = yaml.key?("reason") ? yaml.reject { |k, _| k == "reason" }.freeze : yaml
end

Instance Method Details

#<=>(other) ⇒ Object



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
# File 'lib/udb/logic.rb', line 1025

def <=>(other)
  return nil unless other.is_a?(ParameterTerm)

  other_param = other
  if name != other_param.name
    name <=> other_param.name
  elsif !index.nil? && !other_param.index.nil? && index != other_param.index
    T.must(index) <=> T.must(other_param.index)
  elsif size != other.size
    # one is a size operator and one isn't.
    size.nil? ? 1 : -1
  elsif @yaml.key?("includes") || other_param.to_h.key?("includes")
    if @yaml.key?("includes") && other_param.to_h.key?("includes")
      @yaml.fetch("includes") <=> other_param.to_h.fetch("includes")
    elsif @yaml.key?("includes") && !other_param.to_h.key?("includes")
      1
    elsif !@yaml.key?("includes") && other_param.to_h.key?("includes")
      -1
    end
  elsif @yaml.key?("oneOf") || other_param.comparison_type == ParameterComparisonType::OneOf
    if @yaml.key?("oneOf") && other_param.comparison_type == ParameterComparisonType::OneOf
      one_of = @yaml.fetch("oneOf")
      other_one_of = T.cast(other_param.comparison_value, T::Array[T.untyped])
      one_of.map { |e| [e.class.name, e.to_s] } <=> other_one_of.map { |e| [e.class.name, e.to_s] }
    elsif @yaml.key?("oneOf")
      1
    else
      -1
    end
  elsif @yaml.key?("range") || other_param.to_h.key?("range")
    if @yaml["range"] == other_param.to_h["range"]
      comparison_value <=> T.cast(other_param.comparison_value, Integer)
    elsif @yaml.key?("range")
      1
    else
      -1
    end
  elsif comparison_type != other_param.comparison_type
    comparison_type <=> other_param.comparison_type
  elsif comparison_value != other_param.comparison_value
    cv = comparison_value
    ocv = other_param.comparison_value
    if cv.class != ocv.class
      cv.class.name <=> ocv.class.name
    elsif cv.is_a?(String)
      cv <=> T.cast(ocv, String)
    elsif cv.is_a?(Array)
      ocv_arr = T.cast(ocv, T::Array[T.any(String, T::Boolean, Integer)])
      cv.map { |e| [e.class.name, e.to_s] } <=> ocv_arr.map { |e| [e.class.name, e.to_s] }
    else
      # cv and ocv have the same class (not String, not Array).
      # Given the type constraints (String, Boolean, Integer), this is Integer.
      # Two booleans with the same value are equal (cv != ocv is false), so
      # TrueClass or FalseClass values never reach here.
      T.cast(cv, Integer) <=> T.cast(ocv, Integer)
    end
  else
    # these are the same (ignoring reason)
    return 0
  end
end

#_eval(param_values) ⇒ Object



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/udb/logic.rb', line 486

def _eval(param_values)
  val = param_values[name]

  # don't have the value, so can't say either way
  return SatisfiedResult::Maybe if val.nil?

  if val.is_a?(Array)
    raise "Missing index, includes, or size key in #{@yaml}" unless array_comparison?

    if @yaml.key?("index")
      raise "Index out of range" if T.cast(@yaml.fetch("index"), Integer) >= T.cast(val, Array).size

      value = val.fetch(@yaml.fetch("index"))
      eval_value(value)
    elsif @yaml.key?("includes")
      eval_value(val)
    elsif @yaml.key?("size")
      value = val.size
      eval_value(value)
    else
      raise "unreachable"
    end
  elsif val.is_a?(Integer)
    if @yaml.key?("range")
      msb, lsb = @yaml.fetch("range").split("-").map(&:to_i)
      eval_value((val >> lsb) & (1 << (msb - lsb)))
    else
      eval_value(val)
    end
  else
    eval_value(val)
  end
end

#array_comparison?Boolean

Returns:

  • (Boolean)


346
# File 'lib/udb/logic.rb', line 346

def array_comparison? = @yaml.key?("index") || @yaml.key?("size") || @yaml.key?("includes")

#comparison_typeObject



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/udb/logic.rb', line 396

def comparison_type
  if @yaml.key?("equal")
    ParameterComparisonType::Equal
  elsif @yaml.key?("notEqual")
    ParameterComparisonType::NotEqual
  elsif @yaml.key?("lessThan")
    ParameterComparisonType::LessThan
  elsif @yaml.key?("greaterThan")
    ParameterComparisonType::GreaterThan
  elsif @yaml.key?("lessThanOrEqual")
    ParameterComparisonType::LessThanOrEqual
  elsif @yaml.key?("greaterThanOrEqual")
    ParameterComparisonType::GreaterThanOrEqual
  elsif @yaml.key?("includes")
    ParameterComparisonType::Includes
  elsif @yaml.key?("oneOf")
    ParameterComparisonType::OneOf
  else
    raise "No comparison found in [#{@yaml.keys}]"
  end
end

#comparison_valueObject



349
350
351
# File 'lib/udb/logic.rb', line 349

def comparison_value
  @yaml.fetch(comparison_type.serialize)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


1101
1102
1103
1104
1105
1106
# File 'lib/udb/logic.rb', line 1101

def eql?(other)
  return false unless other.is_a?(ParameterTerm)
  return false unless hash == other.hash

  @yaml_no_reason == other.yaml_no_reason
end

#eval(cfg_arch) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/udb/logic.rb', line 521

def eval(cfg_arch)
  p = cfg_arch.param(name)

  # we know nothing at all about this param. it might not even be valid
  return SatisfiedResult::No if p.nil?

  # since conditions are involved in ConfiguredArchitecture creation
  # (to, for example, determine the list of implemented extensions)
  # we use the parameter values directly from the config instead of the symtab
  # (which may not be constructed yet)
  _eval(cfg_arch.config.param_values)
end

#eval_value(value) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/udb/logic.rb', line 461

def eval_value(value)
  t = comparison_type
  case t
  when ParameterComparisonType::Equal
    (value == @yaml["equal"]) ? SatisfiedResult::Yes : SatisfiedResult::No
  when ParameterComparisonType::NotEqual
    (value != @yaml["notEqual"]) ? SatisfiedResult::Yes : SatisfiedResult::No
  when ParameterComparisonType::LessThan
    (value < @yaml["lessThan"]) ? SatisfiedResult::Yes : SatisfiedResult::No
  when ParameterComparisonType::GreaterThan
    (value > @yaml["greaterThan"]) ? SatisfiedResult::Yes : SatisfiedResult::No
  when ParameterComparisonType::LessThanOrEqual
    (value <= @yaml["lessThanOrEqual"]) ? SatisfiedResult::Yes : SatisfiedResult::No
  when ParameterComparisonType::GreaterThanOrEqual
    (value >= @yaml["greaterThanOrEqual"]) ? SatisfiedResult::Yes : SatisfiedResult::No
  when ParameterComparisonType::Includes
    (value.include?(@yaml["includes"])) ? SatisfiedResult::Yes : SatisfiedResult::No
  when ParameterComparisonType::OneOf
    (@yaml["oneOf"].include?(value)) ? SatisfiedResult::Yes : SatisfiedResult::No
  else
    T.absurd(t)
  end
end

#hashObject



1093
# File 'lib/udb/logic.rb', line 1093

def hash = @hash ||= @yaml_no_reason.hash

#indexObject



340
# File 'lib/udb/logic.rb', line 340

def index = @yaml["index"]

#nameObject



334
# File 'lib/udb/logic.rb', line 334

def name = @yaml.fetch("name")

#negateObject



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/udb/logic.rb', line 355

def negate
  if @yaml.key?("equal")
    new_yaml = @yaml.dup
    new_yaml["notEqual"] = @yaml["equal"]
    new_yaml.delete("equal")
    ParameterTerm.new(new_yaml)
  elsif @yaml.key?("notEqual")
    new_yaml = @yaml.dup
    new_yaml["equal"] = @yaml["notEqual"]
    new_yaml.delete("notEqual")
    ParameterTerm.new(new_yaml)
  elsif @yaml.key?("lessThan")
    new_yaml = @yaml.dup
    new_yaml["greaterThanOrEqual"] = @yaml["lessThan"]
    new_yaml.delete("lessThan")
    ParameterTerm.new(new_yaml)
  elsif @yaml.key?("greaterThan")
    new_yaml = @yaml.dup
    new_yaml["lessThanOrEqual"] = @yaml["greaterThan"]
    new_yaml.delete("greaterThan")
    ParameterTerm.new(new_yaml)
  elsif @yaml.key?("lessThanOrEqual")
    new_yaml = @yaml.dup
    new_yaml["greaterThan"] = @yaml["lessThanOrEqual"]
    new_yaml.delete("lessThanOrEqual")
    ParameterTerm.new(new_yaml)
  elsif @yaml.key?("greaterThanOrEqual")
    new_yaml = @yaml.dup
    new_yaml["lessThan"] = @yaml["greaterThanOrEqual"]
    new_yaml.delete("greaterThanOrEqual")
    ParameterTerm.new(new_yaml)
  elsif @yaml.key?("includes")
    nil
  elsif @yaml.key?("oneOf")
    nil
  else
    raise "No comparison found in [#{@yaml.keys}]"
  end
end

#param_is_array?Boolean

Returns:

  • (Boolean)


703
704
705
# File 'lib/udb/logic.rb', line 703

def param_is_array?
  @yaml.keys.any? { |k| ["index", "includes", "size"].include?(k) }
end

#param_to_sObject



634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/udb/logic.rb', line 634

def param_to_s
  if !index.nil?
    "#{name}[#{index}]"
  elsif !size.nil?
    "$array_size(#{name})"
  elsif @yaml.key?("range")
    msb, lsb = @yaml.fetch("range").split("-").map(&:to_i)
    "#{name}[#{msb}:#{lsb}]"
  else
    name
  end
end

#partial_eval(param_values) ⇒ Object



535
# File 'lib/udb/logic.rb', line 535

def partial_eval(param_values) = _eval(param_values)

#reasonObject



337
# File 'lib/udb/logic.rb', line 337

def reason = @yaml.fetch("reason")

#relation_to(other_param) ⇒ Object



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'lib/udb/logic.rb', line 712

def relation_to(other_param)
  return nil unless name == other_param.name

  self_implies_other =
    LogicNode.new(LogicNodeType::If, [LogicNode.new(LogicNodeType::Term, [self]), LogicNode.new(LogicNodeType::Term, [other_param])])

  self_implies_not_other =
    LogicNode.new(LogicNodeType::If, [LogicNode.new(LogicNodeType::Term, [self]), LogicNode.new(LogicNodeType::Not, [LogicNode.new(LogicNodeType::Term, [other_param])])])

  if param_is_array?
    if @yaml.key?("includes")
      if other_param.to_h.key?("index") && other_param.to_h.key?("equals") && (other_param.to_h.fetch("equals") == @yaml.fetch("includes"))
        self_implies_other
      elsif other_param.to_h.key?("size") && other_param.to_h.key?("equals") && (other_param.to_h.fetch("equals") == 0)
        self_implies_not_other
      elsif other_param.to_h.key?("size") && other_param.to_h.key?("notEqual") && (other_param.to_h.fetch("notEqual") == 0)
        self_implies_other
      elsif other_param.to_h.key?("size") && other_param.to_h.key?("greaterThan") && (other_param.to_h.fetch("greaterThan") == 0)
        self_implies_other
      end
    elsif @yaml.key?("size")
      scalar_relation_to(other_param, self_implies_other, self_implies_not_other)
    elsif @yaml.key?("index") && other_param.to_h.key?("index") && @yaml.fetch("index") == other_param.to_h.fetch("index")
      scalar_relation_to(other_param, self_implies_other, self_implies_not_other)
    end
  else
    scalar_relation_to(other_param, self_implies_other, self_implies_not_other)
  end
end

#scalar_relation_to(other_param, self_implies_other, self_implies_not_other) ⇒ Object



744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'lib/udb/logic.rb', line 744

def scalar_relation_to(other_param, self_implies_other, self_implies_not_other)
  op = comparison_type
  other_op = other_param.comparison_type

  case op
  when ParameterComparisonType::Equal
    case other_op
    when ParameterComparisonType::Equal
      if comparison_value != other_param.comparison_value
        self_implies_not_other
      else
        self_implies_other
      end
    when ParameterComparisonType::NotEqual
      if comparison_value != other_param.comparison_value
        self_implies_other
      else
        self_implies_not_other
      end
    when ParameterComparisonType::LessThan
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_other
      else
        self_implies_not_other
      end
    when ParameterComparisonType::LessThanOrEqual
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      else
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThan
      if T.cast(comparison_value, Integer) > T.cast(other_param.comparison_value, Integer)
        self_implies_other
      else
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThanOrEqual
      if T.cast(comparison_value, Integer) >= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      else
        self_implies_not_other
      end
    when ParameterComparisonType::OneOf
      if T.cast(other_param.comparison_value, T::Array[T.any(String, Integer)]).include?(comparison_value)
        self_implies_other
      else
        self_implies_not_other
      end
    when ParameterComparisonType::Includes
      # self must be a size comparison
      raise "impossible: comparing #{self} to #{other_param}" unless @yaml.key?("size")
      # if size is 0, we know it doesn't include something
      if comparison_value == 0
        self_implies_not_other
      end
    else
      T.absurd(other_op)
    end
  when ParameterComparisonType::NotEqual
    case other_op
    when ParameterComparisonType::Equal
      if comparison_value != other_param.comparison_value
        if comparison_value.is_a?(TrueClass) || comparison_value.is_a?(FalseClass)
          self_implies_other
        end
      else
        self_implies_not_other
      end
    when ParameterComparisonType::NotEqual
      if comparison_value != other_param.comparison_value # otherwise, this would be self-comparison
        if comparison_value.is_a?(TrueClass) || comparison_value.is_a?(FalseClass)
          self_implies_not_other
        end
      else
        self_implies_other
      end
    when ParameterComparisonType::LessThan,
          ParameterComparisonType::LessThanOrEqual,
          ParameterComparisonType::GreaterThan,
          ParameterComparisonType::GreaterThanOrEqual,
          ParameterComparisonType::OneOf
      # nothing to say here.
    when ParameterComparisonType::Includes
      raise "impossible"
    else
      T.absurd(other_op)
    end
  when ParameterComparisonType::LessThan
    case other_op
    when ParameterComparisonType::Equal
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::NotEqual
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::LessThan
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::LessThanOrEqual
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::GreaterThan
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThanOrEqual
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::OneOf
      if T.cast(other_param.comparison_value, T::Array[Integer]).all? { |v| v >= T.cast(comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::Includes
      raise "impossible"
    else
      T.absurd(other_op)
    end
  when ParameterComparisonType::LessThanOrEqual
    case other_op
    when ParameterComparisonType::Equal
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::NotEqual
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::LessThan
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::LessThanOrEqual
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::GreaterThan
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThanOrEqual
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::OneOf
      if T.cast(other_param.comparison_value, T::Array[Integer]).all? { |v| v > T.cast(comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::Includes
      raise "impossible"
    else
      T.absurd(other_op)
    end
  when ParameterComparisonType::GreaterThan
    case other_op
    when ParameterComparisonType::Equal
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::NotEqual
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::LessThan
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::LessThanOrEqual
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThan
      if T.cast(comparison_value, Integer) >= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::GreaterThanOrEqual
      if T.cast(comparison_value, Integer) > T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::OneOf
      if T.cast(other_param.comparison_value, T::Array[Integer]).all? { |v| v <= T.cast(comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::Includes
      # if self is a size operation and size is 0, we can know that other_param does not include
      # otherwise, we know nothing
      if @yaml.key?("size") && size == 0
        self_implies_not_other
      end
    else
      T.absurd(other_op)
    end
  when ParameterComparisonType::GreaterThanOrEqual
    case other_op
    when ParameterComparisonType::Equal
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::NotEqual
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::LessThan
      if T.cast(comparison_value, Integer) < T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::LessThanOrEqual
      if T.cast(comparison_value, Integer) <= T.cast(other_param.comparison_value, Integer)
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThan
      if T.cast(comparison_value, Integer) > T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::GreaterThanOrEqual
      if T.cast(comparison_value, Integer) >= T.cast(other_param.comparison_value, Integer)
        self_implies_other
      end
    when ParameterComparisonType::OneOf
      if T.cast(other_param.comparison_value, T::Array[Integer]).all? { |v| v < T.cast(comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::Includes
      raise "impossible"
    else
      T.absurd(other_op)
    end
  when ParameterComparisonType::OneOf
    case other_op
    when ParameterComparisonType::Equal
      if T.cast(comparison_value, T::Array[T.any(Integer, String)]).all? { |v| v != other_param.comparison_value }
        self_implies_not_other
      end
    when ParameterComparisonType::NotEqual
      if T.cast(comparison_value, T::Array[T.any(Integer, String)]).all? { |v| v != other_param.comparison_value }
        self_implies_other
      end
    when ParameterComparisonType::LessThan
      if T.cast(comparison_value, T::Array[Integer]).all? { |v| v >= T.cast(other_param.comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::LessThanOrEqual
      if T.cast(comparison_value, T::Array[Integer]).all? { |v| v > T.cast(other_param.comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThan
      if T.cast(comparison_value, T::Array[Integer]).all? { |v| v <= T.cast(other_param.comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::GreaterThanOrEqual
      if T.cast(comparison_value, T::Array[Integer]).all? { |v| v < T.cast(other_param.comparison_value, Integer) }
        self_implies_not_other
      end
    when ParameterComparisonType::OneOf
      # self implies other if all in set set are also in other set
      if T.cast(comparison_value, T::Array[T.any(Integer, String)]).all? { |v| T.cast(other_param.comparison_value, T::Array[T.any(Integer, String)]).include?(v) }
        self_implies_other
      end
    when ParameterComparisonType::Includes
      raise "impossible: comparing #{self} to #{other_param}"
    else
      T.absurd(other_op)
    end
  when ParameterComparisonType::Includes
    raise "impossible"
  else
    T.absurd(op)
  end
end

#sizeObject



343
# File 'lib/udb/logic.rb', line 343

def size = @yaml["size"]

#to_asciidocObject



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/udb/logic.rb', line 605

def to_asciidoc
  padoc =
    index.nil? \
      ? name
      : "#{name}[#{index}]"
  type = comparison_type
  case type
  when ParameterTerm::ParameterComparisonType::Equal
    "`#{padoc}` == #{comparison_value}"
  when ParameterTerm::ParameterComparisonType::NotEqual
    "`#{padoc}` != #{comparison_value}"
  when ParameterTerm::ParameterComparisonType::LessThan
    "`#{padoc}` < #{comparison_value}"
  when ParameterTerm::ParameterComparisonType::GreaterThan
    "`#{padoc}` > #{comparison_value}"
  when ParameterTerm::ParameterComparisonType::LessThanOrEqual
    "`#{padoc}` <= #{comparison_value}"
  when ParameterTerm::ParameterComparisonType::GreaterThanOrEqual
    "`#{padoc}` >= #{comparison_value}"
  when ParameterTerm::ParameterComparisonType::Includes
    "#{comparison_value} in `#{padoc}`"
  when ParameterTerm::ParameterComparisonType::OneOf
    "`#{padoc}` in [#{@yaml["oneOf"].join(", ")}]"
  else
    T.absurd(type)
  end
end

#to_hObject



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/udb/logic.rb', line 538

def to_h
  if @yaml.key?("oneOf")
    excl_terms = @yaml["oneOf"].size.times.map do |i|
      els = [
        { "param" => { "name" => @yaml["name"], "equal" => @yaml.fetch("oneOf").fetch(i) } }
      ]
      els += @yaml.fetch("oneOf").size.times.select { |j| j != i }.map do |j|
        { "param" => { "name" => @yaml["name"], "notEqual" => @yaml.fetch("oneOf").fetch(j) } }
      end
      { "allOf" => els }
    end
    {
      "anyOf" => excl_terms
    }
  else
    @yaml
  end
end

#to_idl(cfg_arch) ⇒ Object



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/udb/logic.rb', line 563

def to_idl(cfg_arch)
  t = comparison_type
  case t
  when ParameterComparisonType::Equal
    if comparison_value.is_a?(String)
      "(#{param_to_s}==\"#{comparison_value}\")"
    else
      "(#{param_to_s}==#{comparison_value})"
    end
  when ParameterComparisonType::NotEqual
    if comparison_value.is_a?(String)
      "(#{param_to_s}!=\"#{comparison_value}\")"
    else
      "(#{param_to_s}!=#{comparison_value})"
    end
  when ParameterComparisonType::LessThan
    "(#{param_to_s}<#{comparison_value})"
  when ParameterComparisonType::GreaterThan
    "(#{param_to_s}>#{comparison_value})"
  when ParameterComparisonType::LessThanOrEqual
    "(#{param_to_s}<=#{comparison_value})"
  when ParameterComparisonType::GreaterThanOrEqual
    "(#{param_to_s}>=#{comparison_value})"
  when ParameterComparisonType::Includes
    "$array_includes?(#{param_to_s}, #{comparison_value})"
  when ParameterComparisonType::OneOf
    # construct XOR out of AND and OR
    # e.g.,
    # a XOR B XOR C = (a AND !b AND !c) OR (!a AND b AND !c) OR (!a AND !b AND c)
    terms =
      T.cast(comparison_value, T::Array[T.any(Integer, String)]).map do |v|
        v.is_a?(String) ? "(#{param_to_s}==\"#{v}\")" : "(#{param_to_s}==#{v})"
      end
    excl_terms = terms.size.times.map do |i|
      "(#{terms[i]} && #{terms.size.times.select { |j| j != i }.map { |j| "!#{terms[j]}" }.join(" && ")})"
    end
    "(#{excl_terms.join(" || ")})"
  else
    T.absurd(t)
  end
end

#to_logic_nodeObject



558
559
560
# File 'lib/udb/logic.rb', line 558

def to_logic_node
  LogicNode.new(LogicNodeType::Term, [self])
end

#to_sObject



648
649
650
651
# File 'lib/udb/logic.rb', line 648

def to_s
  # just return IDL
  to_idl(nil)
end

#to_s_prettyObject



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# File 'lib/udb/logic.rb', line 654

def to_s_pretty
  t = comparison_type
  i = index
  if i.nil?
    case t
    when ParameterComparisonType::Equal
      "Paremeter #{@name} equals #{comparison_value}"
    when ParameterComparisonType::NotEqual
      "Paremeter #{@name} does not equal #{comparison_value}"
    when ParameterComparisonType::LessThan
      "Paremeter #{@name} is less than #{comparison_value}"
    when ParameterComparisonType::GreaterThan
      "Paremeter #{@name} is greater than #{comparison_value}"
    when ParameterComparisonType::LessThanOrEqual
      "Paremeter #{@name} is less than or equal to #{comparison_value}"
    when ParameterComparisonType::GreaterThanOrEqual
      "Paremeter #{@name} is greater than or equal to #{comparison_value}"
    when ParameterComparisonType::Includes
      "Paremeter #{@name} (an array) includes the value #{comparison_value}"
    when ParameterComparisonType::OneOf
      "Paremeter #{@name} is one of the following values: #{comparison_value}"
    else
      T.absurd(t)
    end
  else
    case t
    when ParameterComparisonType::Equal
      "The #{i.to_words(ordinal: true, remove_hyphen: true)} element of paremeter #{@name} equals #{comparison_value}"
    when ParameterComparisonType::NotEqual
      "The #{i.to_words(ordinal: true, remove_hyphen: true)} element of paremeter #{@name} does not equal #{comparison_value}"
    when ParameterComparisonType::LessThan
      "The #{i.to_words(ordinal: true, remove_hyphen: true)} element of paremeter #{@name} is less than #{comparison_value}"
    when ParameterComparisonType::GreaterThan
      "The #{i.to_words(ordinal: true, remove_hyphen: true)} element of paremeter #{@name} is greater than #{comparison_value}"
    when ParameterComparisonType::LessThanOrEqual
      "The #{i.to_words(ordinal: true, remove_hyphen: true)} element of paremeter #{@name} is less than or equal to #{comparison_value}"
    when ParameterComparisonType::GreaterThanOrEqual
      "The #{i.to_words(ordinal: true, remove_hyphen: true)} element of paremeter #{@name} is greater than or equal to #{comparison_value}"
    when ParameterComparisonType::Includes
      raise "Cannot occur"
    when ParameterComparisonType::OneOf
      "The #{i.to_words(ordinal: true, remove_hyphen: true)} element of paremeter #{@name} equals on of the following values: #{comparison_value}"
    else
      T.absurd(t)
    end
  end
end

#to_z3(solver, cfg_arch) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/udb/logic.rb', line 419

def to_z3(solver, cfg_arch)
  t = comparison_type
  e = T.let(
    solver.param(name, T.must(cfg_arch.param(name)).schema.to_h),
    T.any(Z3ParameterTerm, Z3::BitvecExpr, Z3::IntExpr, Z3::BoolExpr)
  )
  if @yaml.key?("size")
    e = T.cast(e, Z3ParameterTerm).size_term
  elsif @yaml.key?("index")
    e = T.cast(e, Z3ParameterTerm)[@yaml.fetch("index")]
  end
  if @yaml.key?("range")
    msb, lsb = @yaml.fetch("range").split("-").map(&:to_i)
    e = T.cast(e, Z3ParameterTerm).extract(msb, lsb)
  end
  case t
  when ParameterComparisonType::Equal
    e == @yaml["equal"]
  when ParameterComparisonType::NotEqual
    e != @yaml["notEqual"]
  when ParameterComparisonType::LessThan
    T.cast(e, T.any(Z3ParameterTerm, Z3::BitvecExpr, Z3::IntExpr)) < @yaml["lessThan"]
  when ParameterComparisonType::GreaterThan
    T.cast(e, T.any(Z3ParameterTerm, Z3::BitvecExpr, Z3::IntExpr)) > @yaml["greaterThan"]
  when ParameterComparisonType::LessThanOrEqual
    T.cast(e, T.any(Z3ParameterTerm, Z3::BitvecExpr, Z3::IntExpr)) <= @yaml["lessThanOrEqual"]
  when ParameterComparisonType::GreaterThanOrEqual
    T.cast(e, T.any(Z3ParameterTerm, Z3::BitvecExpr, Z3::IntExpr)) >= @yaml["greaterThanOrEqual"]
  when ParameterComparisonType::Includes
    T.cast(e, Z3ParameterTerm).has_value?(@yaml["includes"])
  when ParameterComparisonType::OneOf
    expr = e == @yaml["oneOf"][0]
    @yaml["oneOf"][1..].each do |v|
      expr = expr | (e == v)
    end
    expr
  else
    T.absurd(t)
  end
end

#yaml_no_reasonObject



331
# File 'lib/udb/logic.rb', line 331

def yaml_no_reason = @yaml_no_reason