HDLRuby 2.11.5 → 2.11.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -156,6 +156,7 @@ extern Type get_type_vector(Type base, unsigned long long number);
156
156
  typedef struct ValueS_ {
157
157
  #ifdef RCSIM
158
158
  Kind kind; /* The kind of object. */
159
+ Object owner; /* The owner of the object if any. */
159
160
  #endif
160
161
  Type type; /* The type of the value. */
161
162
  int numeric; /* Tell if the value is numeric or a bitstring. */
@@ -191,6 +192,12 @@ extern void set_value(Value value, int numeric, void* data);
191
192
  * @param data the source data */
192
193
  extern Value make_set_value(Type type, int numeric, void* data);
193
194
 
195
+ /** Make the size of a value able to store size ints.
196
+ * @note The content of the value is lost!
197
+ * @note do not change the type of the value, only its capacity.
198
+ * @praam value the value to change
199
+ * @param size the size to match */
200
+ extern void resize_value(Value value, unsigned long long size);
194
201
 
195
202
  /** Computes the neg of a value.
196
203
  * @param src the source value of the neg
@@ -335,6 +342,7 @@ extern Value lesser_equal_value(Value src0, Value src1, Value dst);
335
342
  * @param num the number of values for the selection
336
343
  * @return the selected value */
337
344
  extern Value select_value(Value cond, Value dst, unsigned int num, ...);
345
+ extern Value select_valueP(Value cond, Value dst, unsigned int num, Value* values);
338
346
 
339
347
  /** Concat multiple values to a single one.
340
348
  * @param num the number of values to concat
@@ -365,6 +373,7 @@ extern Value copy_value(Value src, Value dst);
365
373
  * @return dst */
366
374
  extern Value copy_value_no_z(Value src, Value dst);
367
375
 
376
+
368
377
  /** Testing if a value is 0.
369
378
  * @param value the value to check
370
379
  * @return 1 if 0 and 0 otherwize */
@@ -375,6 +384,11 @@ extern int zero_value(Value value);
375
384
  * @return 1 if defined and 0 otherwize */
376
385
  extern int is_defined_value(Value value);
377
386
 
387
+ /** Sets a value to undefined.
388
+ * @param dst the destination value
389
+ * @return the destination value */
390
+ extern Value set_undefined_bitstring(Value dst);
391
+
378
392
  /** Testing if two values have the same content (the type is not checked).
379
393
  * @param value0 the first value to compare
380
394
  * @param value1 the second value to compare
@@ -510,9 +524,9 @@ typedef struct SystemTS_ {
510
524
  typedef struct SignalIS_ {
511
525
  Kind kind; /* The kind of object. */
512
526
  Object owner; /* The owner if any. */
527
+ Type type; /* The type of the signal. */
513
528
 
514
529
  char* name; /* The name of the signal. */
515
- Type type; /* The type of the signal. */
516
530
  Value c_value; /* The current value of the signal. */
517
531
  Value f_value; /* The future (next) value of the signal. */
518
532
 
@@ -631,11 +645,14 @@ typedef struct EventS_ {
631
645
  /** The C model of a Statement. */
632
646
  typedef struct StatementS_ {
633
647
  Kind kind; /* The kind of object. */
648
+ Object owner; /* The owner of the object if any. */
634
649
  } StatementS;
635
650
 
636
651
  /** The C model of a transmit statement. */
637
652
  typedef struct TransmitS_ {
638
653
  Kind kind; /* The kind of object. */
654
+ Object owner; /* The owner of the object if any. */
655
+
639
656
  Reference left; /* The left value. */
640
657
  Expression right; /* The right value. */
641
658
  } TransmitS;
@@ -643,6 +660,8 @@ typedef struct TransmitS_ {
643
660
  /** The C model of a print statement. */
644
661
  typedef struct PrintS_ {
645
662
  Kind kind; /* The kind of object. */
663
+ Object owner; /* The owner of the object if any. */
664
+
646
665
  int num_args; /* The number of arguments of the print. */
647
666
  Expression* args; /* The arguments of the print. */
648
667
  } PrintS;
@@ -650,6 +669,8 @@ typedef struct PrintS_ {
650
669
  /** The C model of a hardware if statement. */
651
670
  typedef struct HIfS_ {
652
671
  Kind kind; /* The kind of object. */
672
+ Object owner; /* The owner of the object if any. */
673
+
653
674
  Expression condition; /* The condition. */
654
675
  Statement yes; /* The statement executed if the condition is met. */
655
676
  int num_noifs; /* The number of alternate conditions. */
@@ -661,6 +682,8 @@ typedef struct HIfS_ {
661
682
  /** The C model of a hardware case statement. */
662
683
  typedef struct HCaseS_ {
663
684
  Kind kind; /* The kind of object. */
685
+ Object owner; /* The owner of the object if any. */
686
+
664
687
  Expression value; /* The value to match. */
665
688
  int num_whens; /* The number of possible cases. */
666
689
  Expression* matches;/* The cases matching values. */
@@ -671,6 +694,8 @@ typedef struct HCaseS_ {
671
694
  /** The C model of a time wait statement. */
672
695
  typedef struct TimeWaitS_ {
673
696
  Kind kind; /* The kind of object. */
697
+ Object owner; /* The owner of the object if any. */
698
+
674
699
  // Expression delay; /* The delay to wait in pico seconds. */
675
700
  unsigned long long delay; /* The delay to wait in pico seconds. */
676
701
  } TimeWaitS;
@@ -678,6 +703,8 @@ typedef struct TimeWaitS_ {
678
703
  /** The C model of a time repeat statement. */
679
704
  typedef struct TimeRepeatS_ {
680
705
  Kind kind; /* The kind of object. */
706
+ Object owner; /* The owner of the object if any. */
707
+
681
708
  long long number; /* The number of interations, negative means infinity. */
682
709
  Statement statement;/* The statement to execute in loop. */
683
710
  } TimeRepeatS;
@@ -685,19 +712,23 @@ typedef struct TimeRepeatS_ {
685
712
  /** The C model of a time terminate statement. */
686
713
  typedef struct TimeTerminateS_ {
687
714
  Kind kind; /* The kind of object. */
715
+ Object owner; /* The owner of the object if any. */
688
716
  } TimeTerminateS;
689
717
 
690
718
 
691
719
  /** The C model of an expression. */
692
720
  typedef struct ExpressionS_ {
693
721
  Kind kind; /* The kind of object. */
722
+ Object owner; /* The owner of the object if any. */
694
723
  Type type; /* The type of the expression. */
695
724
  } ExpressionS;
696
725
 
697
726
  /** The C model of a unary expression. */
698
727
  typedef struct UnaryS_ {
699
728
  Kind kind; /* The kind of object. */
729
+ Object owner; /* The owner of the object if any. */
700
730
  Type type; /* The type of the expression. */
731
+
701
732
  Value (*oper)(Value,Value); /* The unary operator. */
702
733
  Expression child; /* The child. */
703
734
  } UnaryS;
@@ -705,7 +736,9 @@ typedef struct UnaryS_ {
705
736
  /** The C model of a binary expression. */
706
737
  typedef struct BinaryS_ {
707
738
  Kind kind; /* The kind of object. */
739
+ Object owner; /* The owner of the object if any. */
708
740
  Type type; /* The type of the expression. */
741
+
709
742
  Value (*oper)(Value,Value,Value); /* The binary operator. */
710
743
  Expression left; /* The left value. */
711
744
  Expression right; /* The right value. */
@@ -714,7 +747,9 @@ typedef struct BinaryS_ {
714
747
  /** The C model of a select expression. */
715
748
  typedef struct SelectS_ {
716
749
  Kind kind; /* The kind of object. */
750
+ Object owner; /* The owner of the object if any. */
717
751
  Type type; /* The type of the expression. */
752
+
718
753
  Expression select; /* The selection value. */
719
754
  int num_choices; /* The number of choices. */
720
755
  Expression* choices;/* The choices. */
@@ -723,7 +758,9 @@ typedef struct SelectS_ {
723
758
  /** The C model of a concat expression. */
724
759
  typedef struct ConcatS_ {
725
760
  Kind kind; /* The kind of object. */
761
+ Object owner; /* The owner of the object if any. */
726
762
  Type type; /* The type of the expression. */
763
+
727
764
  int dir; /* The direction of the concat. */
728
765
  int num_exprs; /* The number of sub expressions. */
729
766
  Expression* exprs; /* The sub expressions. */
@@ -732,7 +769,9 @@ typedef struct ConcatS_ {
732
769
  /** The C model of a cast expression. */
733
770
  typedef struct CastS_ {
734
771
  Kind kind; /* The kind of object. */
772
+ Object owner; /* The owner of the object if any. */
735
773
  Type type; /* The type of the expression. */
774
+
736
775
  Expression child; /* The child expression. */
737
776
  } CastS;
738
777
 
@@ -740,20 +779,25 @@ typedef struct CastS_ {
740
779
  /** The C model of a reference. */
741
780
  typedef struct ReferenceS_ {
742
781
  Kind kind; /* The kind of object. */
782
+ Object owner; /* The owner of the object if any. */
743
783
  Type type; /* The type of the reference. */
744
784
  } ReferenceS;
745
785
 
746
786
  /** The C model of a reference to an object. */
747
787
  typedef struct RefObjectS_ {
748
788
  Kind kind; /* The kind of object. */
789
+ Object owner; /* The owner of the object if any. */
749
790
  Type type; /* The type of the reference. */
791
+
750
792
  Object object; /* The refered object. */
751
793
  } RefObjectS;
752
794
 
753
795
  /** The C model of an index reference. */
754
796
  typedef struct RefIndexS_ {
755
797
  Kind kind; /* The kind of object. */
798
+ Object owner; /* The owner of the object if any. */
756
799
  Type type; /* The type of the reference. */
800
+
757
801
  Expression index; /* The index. */
758
802
  Reference ref; /* The reference to access. */
759
803
  } RefIndexS;
@@ -761,7 +805,9 @@ typedef struct RefIndexS_ {
761
805
  /** The C model of a range reference (expression version!). */
762
806
  typedef struct RefRangeES_ {
763
807
  Kind kind; /* The kind of object. */
808
+ Object owner; /* The owner of the object if any. */
764
809
  Type type; /* The type of the reference. */
810
+
765
811
  Expression first; /* The first of the range. */
766
812
  Expression last; /* The last of the range. */
767
813
  Reference ref; /* The reference to access. */
@@ -770,7 +816,9 @@ typedef struct RefRangeES_ {
770
816
  /** The C model of a concat reference. */
771
817
  typedef struct RefConcatS_ {
772
818
  Kind kind; /* The kind of object. */
819
+ Object owner; /* The owner of the object if any. */
773
820
  Type type; /* The type of the reference. */
821
+
774
822
  int dir; /* The direction of the concat. */
775
823
  int num_refs; /* The number of sub references. */
776
824
  Reference* refs; /* The sub references. */
@@ -779,6 +827,8 @@ typedef struct RefConcatS_ {
779
827
  /** The C model of a charcter string. */
780
828
  typedef struct StringES_ {
781
829
  Kind kind; /* The kind of object. */
830
+ Object owner; /* The owner of the object if any. */
831
+
782
832
  char* str; /* A pointer the to C character string. */
783
833
  } StringES;
784
834
 
@@ -797,6 +847,10 @@ extern SystemT top_system;
797
847
  * @param behavior the timed behavior to register */
798
848
  extern void register_timed_behavior(Behavior behavior);
799
849
 
850
+ /** Adds a behavior for initialization (not timed!).
851
+ * @param beh the behavior to register. */
852
+ extern void register_init_behavior(Behavior beh);
853
+
800
854
  /** Adds a signal for global processing.
801
855
  * @param signal the signal to register */
802
856
  extern void register_signal(SignalI signal);
@@ -947,6 +1001,10 @@ extern void default_print_string(const char* str);
947
1001
  * @param name the name of the vizualization. */
948
1002
  extern void init_default_visualizer(char* name);
949
1003
 
1004
+ /** Sets up the mute vizualization engine.
1005
+ * @param name the name of the vizualization. */
1006
+ extern void init_mute_visualizer(char* name);
1007
+
950
1008
  /** Sets up the vcd vizualization engine.
951
1009
  * @param name the name of the vizualization. */
952
1010
  extern void init_vcd_visualizer(char* name);
@@ -1049,6 +1107,12 @@ extern Value unary(Value (*oper)(Value,Value));
1049
1107
  **/
1050
1108
  extern Value binary(Value (*oper)(Value,Value,Value));
1051
1109
 
1110
+ /** Select calculation.
1111
+ * @param num the number of values to select from.
1112
+ * @return the destination.
1113
+ **/
1114
+ extern Value hselect(unsigned int num);
1115
+
1052
1116
  /** Cast calculation.
1053
1117
  * @param typ the type to cast to.
1054
1118
  * @return the destination.
@@ -315,7 +315,7 @@ Value copy_value(Value src, Value dst) {
315
315
  /* Numeric copy. */
316
316
  dst->data_int = fix_numeric_type(dst->type,src->data_int);
317
317
  } else {
318
- // printf("copy_value with bit string: %s\n",src->data_str);
318
+ // printf("copy_value with bit string: %.*s\n",src->capacity,src->data_str);
319
319
  /* Resize the destination if required. */
320
320
  resize_value(dst,type_width(dst->type));
321
321
  /* Bitstring copy up to the end of dst or src. */
@@ -336,7 +336,7 @@ static Value set_numeric_value(Value src, Value dst);
336
336
  * @param src the source value
337
337
  * @param dst the destination value
338
338
  * @return dst */
339
- extern Value copy_value_no_z(Value src, Value dst) {
339
+ Value copy_value_no_z(Value src, Value dst) {
340
340
  /* set the status of the destination from the source. */
341
341
  // dst->type = src->type;
342
342
  /* Copy the data. */
@@ -432,7 +432,7 @@ static Value set_numeric_value(Value src, Value dst) {
432
432
  /** Sets a value to undefined.
433
433
  * @param dst the destination value
434
434
  * @return the destination value */
435
- static Value set_undefined_bitstring(Value dst) {
435
+ Value set_undefined_bitstring(Value dst) {
436
436
  /* Compute the width of the result in bits. */
437
437
  unsigned long long width = type_width(dst->type);
438
438
 
@@ -481,13 +481,14 @@ static Value neg_value_bitstring(Value src, Value dst) {
481
481
  char d = src_data[count] - '0'; /* Get and convert to bit. */
482
482
  char res;
483
483
  if (d == (d&1)) { /* d is defined. */
484
+ d = 1-d;
484
485
  res = d ^ carry;
485
486
  carry = d & carry;
486
487
  } else {
487
488
  /* Undefined, end here. */
488
489
  break;
489
490
  }
490
- dst_data[count] = res;
491
+ dst_data[count] = res + '0';
491
492
  }
492
493
  /* The remaining bits are undefined. */
493
494
  for(;count < width; ++count) {
@@ -508,6 +509,8 @@ static Value add_value_bitstring(Value src0, Value src1, Value dst) {
508
509
  unsigned long long width0 = type_width(src0->type);
509
510
  unsigned long long width1 = type_width(src1->type);
510
511
 
512
+ // printf("add_value_bitstring with width0=%llu width1=%llu\n",width0,width1);
513
+
511
514
  /* Update the destination capacity if required. */
512
515
  resize_value(dst,width0);
513
516
  /* Set the type and size of the destination from the type of the source.*/
@@ -645,13 +648,78 @@ static Value sub_value_bitstring(Value src0, Value src1, Value dst) {
645
648
  * @return dst */
646
649
  static Value mul_value_defined_bitstring(Value src0, Value src1, Value dst) {
647
650
  // printf("mul_value_defined_bitstring with src0=%llx src1=%llx\n",value2integer(src0),value2integer(src1));
648
- /* Sets state of the destination using the first source. */
649
- dst->type = src0->type;
650
- dst->numeric = 1;
651
+ /* Get the width of the sources. */
652
+ unsigned long long width0 = type_width(src0->type);
653
+ unsigned long long width1 = type_width(src1->type);
654
+ if (width0 <= 64 && width1 <= 64) {
655
+ /* Numeric computation is possible. */
656
+ /* Sets state of the destination using the first source. */
657
+ dst->type = src0->type;
658
+ dst->numeric = 1;
651
659
 
652
- /* Perform the multiplication. */
653
- dst->data_int = value2integer(src0) * value2integer(src1);
654
- // printf("dst->data_int=%llx\n",dst->data_int);
660
+ /* Perform the multiplication. */
661
+ dst->data_int = value2integer(src0) * value2integer(src1);
662
+ // printf("dst->data_int=%llx\n",dst->data_int);
663
+ } else {
664
+ // printf("mul with width0=%llu width1=%llu\n",width0,width1);
665
+ // printf("src0->data_str=%s\n",src0->data_str);
666
+ // printf("src1->data_str=%s\n",src1->data_str);
667
+ /* Bit string computation. */
668
+ unsigned int pos = get_value_pos();
669
+ /* Process the sign. */
670
+ int sgn1 = (src1->type->flags.sign) && (src1->data_str[width1-1] == '1');
671
+ // printf("sgn0=%d sgn1=%d\n",(src0->type->flags.sign) && (src0->data_str[width0-1] == '1'),sgn1);
672
+ Value psrc1;
673
+ // printf("first scr1->data_str=%s\n",src1->data_str);fflush(stdout);
674
+ if (sgn1) {
675
+ psrc1 = neg_value_bitstring(src1,get_value());
676
+ } else {
677
+ psrc1 = src1;
678
+ }
679
+ // printf("now pscr1->data_str=%s\n",psrc1->data_str);fflush(stdout);
680
+ /* Perform the multiplying with sucessive shifts and additions. */
681
+ /* First the result is zero. */
682
+ const char* str = psrc1->data_str;
683
+ Value acc = get_value();
684
+ ValueS sh;
685
+ memcpy(&sh,src0,sizeof(ValueS));
686
+ // printf("sh width=%llu\n",type_width(sh.type));
687
+ // printf("sh.data_str=%s\n",sh.data_str);
688
+ resize_value(acc,width0);
689
+ acc->numeric = 0;
690
+ acc->type = src0->type;
691
+ memset(acc->data_str,'0',width0);
692
+ // printf("first acc->data_str=%s\n",acc->data_str);fflush(stdout);
693
+ /* The multiply loop. */
694
+ unsigned long long i;
695
+ for(i=0; i<width1-1; ++i) {
696
+ unsigned long long j;
697
+ /* Add is bit is 1. */
698
+ if (str[i] == '1')
699
+ acc = add_value_bitstring(acc,&sh,get_value());
700
+ /* Shift. */
701
+ for(j=0; j<(width0-i-1); ++j) {
702
+ sh.data_str[width0-j-1] = sh.data_str[width0-j-2];
703
+ }
704
+ sh.data_str[width0-j-1] = '0';
705
+ }
706
+ /* Last addition. */
707
+ if (str[i] == '1')
708
+ acc = add_value_bitstring(acc,&sh,get_value());
709
+ /* Mange the sign of the result. */
710
+ // printf("before acc->data_str=%s\n",acc->data_str);fflush(stdout);
711
+ // printf("acc last bit=%d\n",acc->data_str[width0-1]);
712
+ if (sgn1) acc = neg_value_bitstring(acc,get_value());
713
+ // printf("finally acc->data_str=%s\n",acc->data_str);fflush(stdout);
714
+ // printf("acc last bit=%d\n",acc->data_str[width0-1]);
715
+ /* Save the result. */
716
+ dst = copy_value(acc,dst);
717
+ // printf("acc width=%llu acc->data_str=%p\n",type_width(acc->type),acc->data_str);
718
+ // printf("dst width=%llu dst->data_str=%p\n",type_width(dst->type),dst->data_str);
719
+ // printf("finally dst->data_str=%s\n",dst->data_str);fflush(stdout);
720
+ /* Restores the pool of values. */
721
+ set_value_pos(pos);
722
+ }
655
723
  return dst;
656
724
  }
657
725
 
@@ -1165,8 +1233,8 @@ static Value xor_value_bitstring(Value src0, Value src1, Value dst) {
1165
1233
 
1166
1234
 
1167
1235
  /** Computes the left shift of a bitstring value by a numeric value.
1168
- * @param src0 the first source value of the addition
1169
- * @param src1 the second source value of the addition
1236
+ * @param src0 the first source value of the shift
1237
+ * @param src1 the second source value of the shift
1170
1238
  * @param dst the destination
1171
1239
  * @return dst */
1172
1240
  static Value shift_left_value_bitstring_numeric(Value src0, Value src1, Value dst) {
@@ -1407,6 +1475,38 @@ static Value select_value_bitstring(Value cond, Value dst, unsigned int num,
1407
1475
  return dst;
1408
1476
  }
1409
1477
 
1478
+ /** Selects a value depending on a bitstring condition (pointer version).
1479
+ * @param cond the condition to use for selecting a value
1480
+ * @param dst the destination value (used only if new value is created).
1481
+ * @param num the number of values for the selection
1482
+ * @param values the values to select from
1483
+ * @return the selected value */
1484
+ static Value select_value_bitstring_array(Value cond, Value dst,
1485
+ unsigned int num, Value* values)
1486
+ {
1487
+ // printf("select_value_bitstring_array with cond=%s\n",cond->data_str);
1488
+ /* Get the first alternative for sizing the result. */
1489
+ Value src = values[0];
1490
+ /* Compute the width of the result in bits. */
1491
+ unsigned long long width = type_width(src->type);
1492
+ // printf("select width=%llu\n",width);
1493
+
1494
+ /* Update the destination capacity if required. */
1495
+ resize_value(dst,width);
1496
+ /* Set the type and size of the destination from the type of the source.*/
1497
+ dst->type = src->type;
1498
+ dst->numeric = 0;
1499
+ char *dst_data = dst->data_str;
1500
+
1501
+ /* Sets the destination as undefined. */
1502
+ unsigned long long count;
1503
+ for(count = 0; count < width; ++count) {
1504
+ dst_data[count] = 'x';
1505
+ }
1506
+ /* Return the destination. */
1507
+ return dst;
1508
+ }
1509
+
1410
1510
 
1411
1511
  /** Concat multiple bitstring values to a single one.
1412
1512
  * @param num the number of values to concat
@@ -1744,6 +1844,7 @@ static Value add_value_numeric(Value src0, Value src1, Value dst) {
1744
1844
  /* Sets state of the destination using the first source. */
1745
1845
  dst->type = src0->type;
1746
1846
  dst->numeric = 1;
1847
+ // printf("add_value_numeric with width0=%llu width1=%llu\n",type_width(src0->type),type_width(src1->type));
1747
1848
 
1748
1849
  /* Perform the addition. */
1749
1850
  dst->data_int = fix_numeric_type(dst->type,src0->data_int + src1->data_int);
@@ -1773,7 +1874,7 @@ static Value sub_value_numeric(Value src0, Value src1, Value dst) {
1773
1874
  * @param dst the destination value
1774
1875
  * @return dst */
1775
1876
  static Value mul_value_numeric(Value src0, Value src1, Value dst) {
1776
- // printf("mul_value_numeric with src0->data_int=%llx src1->data_int=%llx\n",src0->data_int, src1->data_int);
1877
+ printf("mul_value_numeric with src0->data_int=%llx src1->data_int=%llx\n",src0->data_int, src1->data_int);
1777
1878
  /* Sets state of the destination using the first source. */
1778
1879
  dst->type = src0->type;
1779
1880
  dst->numeric = 1;
@@ -1939,8 +2040,8 @@ static Value equal_value_numeric(Value src0, Value src1, Value dst) {
1939
2040
 
1940
2041
  /* Perform the comparison. */
1941
2042
  dst->data_int = (src0->data_int == src1->data_int) ? 1 : 0;
1942
- // printf("scr0->data_int=%lld\n",src0->data_int);
1943
- // printf("scr1->data_int=%lld\n",src1->data_int);
2043
+ // printf("src0->data_int=%lld\n",src0->data_int);
2044
+ // printf("src1->data_int=%lld\n",src1->data_int);
1944
2045
  // printf("dst->data_int=%lld\n",dst->data_int);
1945
2046
  return dst;
1946
2047
  }
@@ -2165,6 +2266,30 @@ static Value select_value_numeric(Value cond, Value dst, unsigned int num,
2165
2266
  return NULL;
2166
2267
  }
2167
2268
 
2269
+ /** Selects a value depending on a numeric condition (pointer version).
2270
+ * @param cond the condition to use for selecting a value
2271
+ * @param dst the destination value (used only if new value is created).
2272
+ * @param num the number of values for the selection
2273
+ * @param values the values to select from.
2274
+ * @return the selected value */
2275
+ static Value select_value_numeric_array(Value cond, Value dst, unsigned int num,
2276
+ Value* values) {
2277
+ unsigned int i;
2278
+ /* Select the value corresponding to the condition and copy it to
2279
+ * the destination. */
2280
+ for(i = 0; i<num; ++i) {
2281
+ Value value = values[i];
2282
+ if (i == value2integer(cond)) {
2283
+ /* The right value is reached, copy it. */
2284
+ copy_value(value,dst);
2285
+ return dst;
2286
+ }
2287
+ }
2288
+ /* Should never be here. */
2289
+ return NULL;
2290
+ }
2291
+
2292
+
2168
2293
  /** Concat multiple numeric values to a single one.
2169
2294
  * @param num the number of values to concat
2170
2295
  * @param dir the direction of the concatenation.
@@ -2228,6 +2353,28 @@ static int zero_value_numeric(Value value) {
2228
2353
  return value->data_int == 0;
2229
2354
  }
2230
2355
 
2356
+ /** Testing if a bitstring value is 0.
2357
+ * @param value the value to check.
2358
+ * @return 1 if 0 and 0 othewize. */
2359
+ static int zero_value_bitstring(Value value) {
2360
+ unsigned long long width = type_width(value->type);
2361
+ for(unsigned long long i=0;i<width; ++i) {
2362
+ if (value->data_str[i] != '0') return 0;
2363
+ }
2364
+ return 1;
2365
+ }
2366
+
2367
+ /** Testing if a numeric value is positive (or 0). */
2368
+ static int positive_value_numeric(Value value) {
2369
+ return (value->type->flags.sign == 0) || ((long long)(value->data_int) >= 0);
2370
+ }
2371
+
2372
+ /** Testing if a bitstring value is positive (or 0). */
2373
+ static int positive_value_bitstring(Value value) {
2374
+ return (value->type->flags.sign == 0) ||
2375
+ (value->data_str[type_width(value->type)-1] == '1');
2376
+ }
2377
+
2231
2378
 
2232
2379
 
2233
2380
  /** Testing if two numeric values have the same content (the type is not checked).
@@ -2450,8 +2597,7 @@ Value sub_value(Value src0, Value src1, Value dst) {
2450
2597
  * @param dst the destination value
2451
2598
  * @return dst */
2452
2599
  Value mul_value(Value src0, Value src1, Value dst) {
2453
- // printf("mul_value with src0=%llx src1=%llx\n",value2integer(src0),value2integer(src1));
2454
- // printf("src0->numeric=%d src1->numeric=%d\n",src0->numeric,src1->numeric);
2600
+ // printf("mul_value with src0->numeric=%d src1->numeric=%d\n",src0->numeric,src1->numeric);
2455
2601
  // printf("is_defined_value(src0)=%d is_defined_value(src1)=%d\n",is_defined_value(src0),is_defined_value(src1));
2456
2602
  /* Might allocate a new value so save the current pool state. */
2457
2603
  // unsigned int pos = get_value_pos();
@@ -2472,15 +2618,19 @@ Value mul_value(Value src0, Value src1, Value dst) {
2472
2618
  return mul_value_numeric(src0,src1,dst);
2473
2619
  }
2474
2620
  } else if (src1->numeric && is_defined_value(src0)) {
2475
- // /* src1 is numeric but src0 is a defined bitstring, convert src1 to
2476
- // * bitstring. */
2477
- // src1 = set_bit_string_value(src1,get_value());
2478
- // /* And do a bitstring multiplying. */
2479
- // return mul_value_defined_bitstring(src0,src1,dst);
2480
- /* src0 is a defined bitstring, convert it to a numeric. */
2621
+ if (type_width(src0->type) <= 64) {
2622
+ /* src0 is aanot too big defined bitstring,
2623
+ * convert it to a numeric. */
2481
2624
  src0 = set_numeric_value(src0,get_value());
2482
2625
  /* And do a numeri multiplying. */
2483
2626
  return mul_value_numeric(src0,src1,dst);
2627
+ } else {
2628
+ /* src1 is numeric but src0 is a defined bitstring, convert src1 to
2629
+ * bitstring. */
2630
+ src1 = set_bitstring_value(src1,get_value());
2631
+ /* And do a bitstring multiplying. */
2632
+ return mul_value_defined_bitstring(src0,src1,dst);
2633
+ }
2484
2634
  } else if (is_defined_value(src0) && is_defined_value(src1)) {
2485
2635
  /* Both sources are defined bitstrings. */
2486
2636
  return mul_value_defined_bitstring(src0,src1,dst);
@@ -2933,7 +3083,24 @@ Value select_value(Value cond, Value dst, unsigned int num, ...) {
2933
3083
  return dst;
2934
3084
  }
2935
3085
 
2936
- /** Concat multiple general values to a single one.
3086
+ /** Selects a value depending on a general condition (pointer version).
3087
+ * @param cond the condition to use for selecting a value
3088
+ * @param dst the destination value (used only if new value is created).
3089
+ * @param num the number of values for the selection
3090
+ * @param values the values to select from.
3091
+ * @return the selected value */
3092
+ Value select_valueP(Value cond, Value dst, unsigned int num, Value* values) {
3093
+ if (is_defined_value(cond)) {
3094
+ /* The condition can be made numeric. */
3095
+ dst = select_value_numeric_array(cond,dst,num,values);
3096
+ } else {
3097
+ /* The sources cannot be numeric, compute bitsitrings. */
3098
+ dst = select_value_bitstring_array(cond,dst,num,values);
3099
+ }
3100
+ return dst;
3101
+ }
3102
+
3103
+ /** Concat multiple general values to a single one (pointer version).
2937
3104
  * @param dir the direction of the concatenation.
2938
3105
  * @param num the number of values to concat
2939
3106
  * @param dst the destination value
@@ -3001,9 +3168,22 @@ Value concat_value(unsigned int num, int dir, Value dst, ...) {
3001
3168
  * @param dst the destination value
3002
3169
  * @return dst */
3003
3170
  Value cast_value(Value src, Type type, Value dst) {
3171
+ // printf("cast value from width=%llu to width=%llu\n",type_width(src->type),type_width(type));
3004
3172
  if (src->numeric) {
3005
3173
  /* The source is numeric. */
3006
- return cast_value_numeric(src,type,dst);
3174
+ /* Is the destination type small enough? */
3175
+ if (type_width(type) <= 64) {
3176
+ /* Yes, keep numeric. */
3177
+ return cast_value_numeric(src,type,dst);
3178
+ } else {
3179
+ /* No, do it in bit string. */
3180
+ Value res = set_bitstring_value(src,get_value());
3181
+ // printf("res=%.*s\n",res->capacity,res->data_str);
3182
+ dst = cast_value_bitstring(res,type,dst);
3183
+ // printf("dst=%.*s\n",dst->capacity,dst->data_str);
3184
+ free_value();
3185
+ return dst;
3186
+ }
3007
3187
  } else {
3008
3188
  /* The source cannot be numeric, compute bitsitrings. */
3009
3189
  return cast_value_bitstring(src,type,dst);
@@ -3019,8 +3199,7 @@ int zero_value(Value value) {
3019
3199
  /* The value is numeric. */
3020
3200
  return zero_value_numeric(value);
3021
3201
  } else {
3022
- /* The value cannot be reduced to numeric: cannot be 0. */
3023
- return 0;
3202
+ return zero_value_bitstring(value);
3024
3203
  }
3025
3204
  }
3026
3205
 
@@ -3161,7 +3340,11 @@ unsigned long long value2integer(Value value) {
3161
3340
  unsigned long long width = type_width(value->type);
3162
3341
  /* If the value is numeric, just return its data as is. */
3163
3342
  if (value->numeric) {
3164
- return value->data_int & ~((unsigned long long)(-1LL) << width);
3343
+ // printf("width=%llu\n",width);
3344
+ if (width == 64)
3345
+ return value->data_int;
3346
+ else
3347
+ return value->data_int & ~(0xFFFFFFFFFFFFFFFFULL << width);
3165
3348
  }
3166
3349
  /* Otherwise convert the bitstring to an integer if possible,
3167
3350
  * but return 0 in case of failure. */