Class: Udb::ParameterTerm
- Inherits:
-
Object
- Object
- Udb::ParameterTerm
- 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)]) }
Instance Method Summary collapse
- #<=>(other) ⇒ Integer?
- #_eval(param_values) ⇒ SatisfiedResult
- #array_comparison? ⇒ Boolean
- #comparison_type ⇒ ParameterComparisonType
- #comparison_value ⇒ ValueType
- #eql?(other) ⇒ Boolean
-
#equivalent?(other) ⇒ Boolean
test for logical equivalence.
- #eval(cfg_arch) ⇒ SatisfiedResult
- #eval_value(value) ⇒ SatisfiedResult
-
#hash ⇒ Integer
hash and eql? must be implemented to use ParameterTerm as a Hash key.
- #index ⇒ Integer?
- #initialize(yaml) constructor
- #name ⇒ String
-
#negate ⇒ ParameterTerm?
return a negated version of self, or nil if no simple negation exists.
- #param_is_array? ⇒ Boolean
- #param_to_s ⇒ String
- #partial_eval(param_values) ⇒ SatisfiedResult
- #reason ⇒ String
-
#relation_to(other_param) ⇒ LogicNode?
if self and other_param had a well-defined logical relationship, return it otherwise, return nil note: this is only one half of the relationship.
- #scalar_relation_to(other_param, self_implies_other, self_implies_not_other) ⇒ LogicNode? private
- #size ⇒ Boolean?
- #to_asciidoc ⇒ Object
- #to_h ⇒ Hash{String => T.untyped}
- #to_idl(cfg_arch) ⇒ String
- #to_logic_node ⇒ LogicNode
- #to_s ⇒ String
- #to_s_pretty ⇒ String
- #to_z3(solver, cfg_arch) ⇒ Z3::BoolExpr
Constructor Details
#initialize(yaml)
318 319 320 |
# File 'lib/udb/logic.rb', line 318 def initialize(yaml) @yaml = yaml end |
Instance Method Details
#<=>(other) ⇒ Integer?
1018 1019 1020 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 |
# File 'lib/udb/logic.rb', line 1018 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.to_h.key?("oneOf") if @yaml.key?("oneOf") && other_param.to_h.key?("oneOf") @yaml.fetch("oneOf") <=> other_param.to_h.fetch("oneOf") 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 if cv.is_a?(String) cv <=> T.cast(other_param.comparison_value, String) elsif cv.is_a?(Array) cv <=> T.cast(other_param.comparison_value, T::Array[T.any(String, T::Boolean, Integer)]) else T.cast(comparison_value, Integer) <=> T.cast(other_param.comparison_value, Integer) end else # these are the same (ignoring reason) return 0 end end |
#_eval(param_values) ⇒ SatisfiedResult
479 480 481 482 483 484 485 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 |
# File 'lib/udb/logic.rb', line 479 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
335 |
# File 'lib/udb/logic.rb', line 335 def array_comparison? = @yaml.key?("index") || @yaml.key?("size") || @yaml.key?("includes") |
#comparison_type ⇒ ParameterComparisonType
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/udb/logic.rb', line 385 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_value ⇒ ValueType
338 339 340 |
# File 'lib/udb/logic.rb', line 338 def comparison_value @yaml.fetch(comparison_type.serialize) end |
#eql?(other) ⇒ Boolean
1084 1085 1086 1087 1088 |
# File 'lib/udb/logic.rb', line 1084 def eql?(other) return false unless other.is_a?(ParameterTerm) (self <=> other) == 0 end |
#equivalent?(other) ⇒ Boolean
test for logical equivalence
1092 1093 1094 1095 1096 |
# File 'lib/udb/logic.rb', line 1092 def equivalent?(other) return false unless other.name == name LogicNode.new(LogicNodeType::Term, [self]).equivalent?(LogicNode.new(LogicNodeType::Term, [other])) end |
#eval(cfg_arch) ⇒ SatisfiedResult
514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/udb/logic.rb', line 514 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) ⇒ SatisfiedResult
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/udb/logic.rb', line 454 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 |
#hash ⇒ Integer
hash and eql? must be implemented to use ParameterTerm as a Hash key
1076 |
# File 'lib/udb/logic.rb', line 1076 def hash = @yaml.reject { |k, _| k == "reason" }.hash |
#index ⇒ Integer?
329 |
# File 'lib/udb/logic.rb', line 329 def index = @yaml["index"] |
#name ⇒ String
323 |
# File 'lib/udb/logic.rb', line 323 def name = @yaml.fetch("name") |
#negate ⇒ ParameterTerm?
return a negated version of self, or nil if no simple negation exists
344 345 346 347 348 349 350 351 352 353 354 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 |
# File 'lib/udb/logic.rb', line 344 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
696 697 698 |
# File 'lib/udb/logic.rb', line 696 def param_is_array? @yaml.keys.any? { |k| ["index", "includes", "size"].include?(k) } end |
#param_to_s ⇒ String
627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/udb/logic.rb', line 627 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) ⇒ SatisfiedResult
528 |
# File 'lib/udb/logic.rb', line 528 def partial_eval(param_values) = _eval(param_values) |
#reason ⇒ String
326 |
# File 'lib/udb/logic.rb', line 326 def reason = @yaml.fetch("reason") |
#relation_to(other_param) ⇒ LogicNode?
if self and other_param had a well-defined logical relationship, return it otherwise, return nil note: this is only one half of the relationship. to get the whole picture, need to use self.relation_to(other_param) && other_param.relation_to(self)
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 |
# File 'lib/udb/logic.rb', line 705 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) ⇒ LogicNode?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
737 738 739 740 741 742 743 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 |
# File 'lib/udb/logic.rb', line 737 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 |
#size ⇒ Boolean?
332 |
# File 'lib/udb/logic.rb', line 332 def size = @yaml["size"] |
#to_asciidoc ⇒ Object
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/udb/logic.rb', line 598 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_h ⇒ Hash{String => T.untyped}
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/udb/logic.rb', line 531 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) ⇒ String
556 557 558 559 560 561 562 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 |
# File 'lib/udb/logic.rb', line 556 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_node ⇒ LogicNode
551 552 553 |
# File 'lib/udb/logic.rb', line 551 def to_logic_node LogicNode.new(LogicNodeType::Term, [self]) end |
#to_s ⇒ String
641 642 643 644 |
# File 'lib/udb/logic.rb', line 641 def to_s # just return IDL to_idl(nil) end |
#to_s_pretty ⇒ String
647 648 649 650 651 652 653 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 |
# File 'lib/udb/logic.rb', line 647 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) ⇒ Z3::BoolExpr
408 409 410 411 412 413 414 415 416 417 418 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 |
# File 'lib/udb/logic.rb', line 408 def to_z3(solver, cfg_arch) t = comparison_type e = T.let( solver.param(name, T.must(cfg_arch.param(name)).maximal_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 expr = T.cast(e, Z3ParameterTerm)[0] == @yaml["includes"] T.cast(e, Z3ParameterTerm).max_items.times do |i| expr = expr | (T.cast(e, Z3ParameterTerm)[i] == @yaml["includes"]) end expr 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 |