-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemoryLayout.java
1203 lines (1039 loc) · 50.6 KB
/
MemoryLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
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
604
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
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
694
695
696
697
698
699
700
701
702
703
704
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
734
735
736
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
//------------------------------------------------------------------------------
// Memory layout with base being global
// Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2024
//------------------------------------------------------------------------------
package com.AppaApps.Silicon; // Memory layout
import java.util.*;
class MemoryLayout extends Test // Memory layout
{Layout layout; // Layout of part of memory
Memory memory; // Memory containing layout
int base; // Base of layout in memory - like located in Pl1
boolean debug; // Debug if true
String name; // Name of the memory layout if supplied
//D1 Control // Testing, control and integrity
void memory(Memory Memory) {memory = Memory;} // Set the base of the layout in memory allowing such layouts to be relocated
void layout(Layout Layout) {layout = Layout;} // Set the base of the layout in memory allowing such layouts to be relocated
void base (int Base) {base = Base;} // Set the base of the layout in memory allowing such layouts to be relocated
void ok(String Lines) // Check that specified lines are present in the memory layout
{final String m = toString(); // Memory as string
final String[]L = Lines.split("\\n"); // Lines of expected
int p = 0;
for(int i = 0; i < L.length; ++i) // Each expected
{final String l = L[i];
final int q = m.indexOf(l, p); // Check specified lines are present
if (q == -1) // Line missing
{err("Layout does not contain line:", i+1, "\n"+l+"\n");
++Layout.testsFailed;
return;
}
p = q;
}
++Layout.testsPassed; // Lines found
}
//D1 Get and Set // Get and set values in memory but only during testing
int getInt(Layout.Field field, int...indices) // Get a value from memory occupied by the layout
{z(); return new At(field, indices).setOff().result;
}
void setInt(Layout.Field field, int value, int...indices) // Set a value in memory occupoied by the layout
{z();
final At a = new At(field, indices).setOff();
memory.set(a.at, a.width, value);
}
void zero() // Clear the memory associated with the layout to zeros
{z();
memory.set(base, layout.size(), 0);
}
//D1 Components // Locate a variable in memory via its indices
class At
{final Layout.Field field; // Field description in layout
final int width; // Width of element in memory
enum Type {constant, direct, indirect}; // Type of memory reference. Constant a known constant value. Direct: a field with indioces known at compile time. Indirect: a field whose indices are other fields whose indices are known at compile time.
final Type type; // Type of memory reference
final int[]indices; // Known indices to be applied directly to locate the field in memory
final At[] directs; // Fields whose location is known at the start so they can be used for indices into memory rather like registers on a chip
int delta; // Delta due to indices
int at; // Location in memory
int result; // The contents of memory at this location
At(int constant) // Constant value made to look like a memory reference
{z(); field = null; indices = null; width = delta = at = 0;
directs = null;
type = Type.constant; result = constant;
}
void locateDirectAddress() // Locate a direct address and its content
{delta = field.locator.at(indices);
at = base + delta;
result = memory.getInt(at, width);
}
void locateInDirectAddress() // Locate an indirect address and its content
{final int N = directs.length;
for (int i = 0; i < N; i++)
{indices[i] = directs[i].setOff().getInt();
}
locateDirectAddress(); // Locate the address directly now that its indices are known
}
void checkCompiled(Layout.Field Field) // Check the field has been compiled
{if (!Field.compiled)
{stop("Field:", Field.name, "has not been compiled yet");
}
if (layout == null)
{stop("No layout has been supplied for this memory layout");
}
if (memory == null)
{stop("No memory has been supplied for this memory layout");
}
if (layout != Field.container())
{final String n = Field.name, m = layout.layoutName, f = Field.container().layoutName;
if (f == null || m == null || !f.equals(m))
{stop("Field:", n, (f == null ? "" : "in layout: "+ f),
"is not part of", (m == null ? "this layout" : "layout: "+ f));
}
}
}
At(Layout.Field Field) // No indices or base
{z(); checkCompiled(Field);
field = Field; indices = new int[0]; width = field.width;
type = Type.direct; directs = null;
locateDirectAddress(); // The indices are constant so the address will not change over time
}
At(Layout.Field Field, int...Indices) // Constant indices used for setting initial values
{z(); checkCompiled(Field);
field = Field; indices = Indices; width = field.width;
type = Type.direct; directs = null;
locateDirectAddress(); // The indices are constant so the address will not change over time
}
At(Layout.Field Field, At...Directs) // Variable indices used for obtaining run time values
{z(); checkCompiled(Field);
for (int i = 0; i < Directs.length; i++)
{if (Directs[i].type != Type.direct) stop("Index:", i, "must not have a base or indices");
}
field = Field; width = field.width;
type = Type.indirect; directs = Directs;
indices = new int[Directs.length];
}
boolean sameSize(At b) // Check two fields are the same size
{if (field == null) return true; // Constants match any size
z(); field.sameSize(b.field);
z(); return true;
}
int width() // Width of the field in memory
{z(); if (type == Type.constant) stop("A constant does not have any specific width");
z(); return field.width();
}
At setOff() // Set the base address of the field
{z();
if (type == Type.indirect) {z(); locateInDirectAddress();} // Evaluate indirect indices
else {z(); locateDirectAddress();} // Evaluate direct indices
return this;
}
boolean getBit(int i) {return memory.getBit(at+i);} // Get a bit from memory assuming that setOff() has been called to fix the location of the field containing the bit
void setBit(int i, boolean b) { memory.set(at+i, b);} // Set a bit in memory assuming that setOff() has been called to fix the location of the field containing the bit
int getInt() {z(); return result;} // The value in memory, at the indicated location, treated as an integer or the value of the constant
void setInt(int value) {z(); memory.set(at, width, value);} // Set the value in memory at the indicated location, treated as an integer
public String toString() // Print field name(indices)=value or name=value if there are no indices
{final StringBuilder s = new StringBuilder();
s.append(field.name);
if (indices.length > 0)
{setOff();
s.append("[");
for (int i = 0, N = indices.length; i < N; i++)
{s.append(indices[i]);
if (i < N-1) s.append(",");
}
s.append("]");
}
s.append("("+base+"+"+delta+")"+at+"="+result);
return s.toString();
}
MemoryLayout ml() {return MemoryLayout.this;} // Containing memory layout
//D2 Move // Copy data between memory locations
void move(At source) // Copy the specified number of bits from source to target assuming no overlap. The source and target can be in the same or a different memory.
{z(); sameSize(source);
for(int i = 0; i < width; ++i)
{z();
final boolean b = source.getBit(i);
setBit(i, b);
}
}
void move(At source, At buffer) // Copy the specified number of bits from source to target via a buffer to allow the operation to proceed in bit parallel assuming the buffer does not overlap either the source or target, each of which can be in different memories.
{z(); sameSize(source); sameSize(buffer);
buffer.move(source);
move(buffer);
}
void moveUp(At Index, At buffer) // Move the elements of an array up one position deleting the last element. A buffer of the same size is used to permit copy in parallel.
{z(); sameSize(buffer);
if (!(field instanceof Layout.Array)) stop("Array required for moveUp");
z(); setOff(); // Set the offset of the source/target
z(); buffer.setOff().move(this); // Set the offset of the buffer and copies the source
final Layout.Array A = field.toArray(); // Address field to be moved as an array
final int w = A.element.width; // Width of array element
for (int i = Index.result+1; i < A.size; i++) // Each element
{for (int j = 0; j < w; j++) // Each bit in each element
{final boolean b = buffer.getBit((i-1)*w + j);
setBit(i*w+j, b);
}
}
}
void moveDown(At Index, At buffer) // Move the elements of an array down one position deleting the indexed element. A buffer of the same size is used to permit copy in parallel.
{z(); sameSize(buffer);
if (!(field instanceof Layout.Array)) stop("Array required for moveDown");
z(); setOff(); // Set the offset of the target
z(); buffer.setOff().move(this); // Set the offset of the buffer and copies the source
final Layout.Array A = field.toArray(); // Address field to be moved as an array
final int w = A.element.width; // Width of array element
for (int i = Index.result; i < A.size-1; i++) // Each element
{for (int j = 0; j < w; j++) // Each bit in each element
{final boolean b = buffer.getBit((i+1)*w + j);
setBit(i*w+j, b);
}
}
}
//D2 Bits // Bit operations in a memory.
void zero() // Zero some memory
{z(); memory.zero(at, width);
}
void ones() // Ones some memory
{z(); memory.ones(at, width);
}
void invert(At a) // Invert the specified bits
{z(); memory.invert(a.result, a.width());
}
boolean isAllZero() // Check that the specified memory is all zeros
{z(); return memory.isAllZero(at, width);
}
boolean isAllOnes() // Check that the specified memory is all ones
{z(); return memory.isAllOnes(at, width);
}
//D1 Boolean // Boolean operations on fields held in memories.
boolean isZero() // Whether the field is all zero
{z();
for(int i = 0; i < width; ++i)
{z(); if (getBit(i)) {z(); return false;}
}
z(); return true;
}
boolean isOnes() // Whether the field is all ones
{z();
for(int i = 0; i < width; ++i)
{z(); if (!getBit(i)) {z(); return false;}
}
z(); return true;
}
boolean equal(At b) // Whether a == b
{z();
if (field == null || b.field == null)
{z(); return result == b.result;
}
z(); sameSize(b);
for(int i = 0; i < width; ++i)
{z(); if (getBit(i) != b.getBit(i)) {z(); return false;}
}
z(); return true;
}
void equal(At b, At result) // Whether a == b
{z();
result.setInt(equal(b) ? 1 : 0);
}
boolean notEqual(At b) {return !equal(b);} // Whether a != b
void notEqual(At b, At result) // Whether a != b
{z();
result.setInt(equal(b) ? 1 : 0);
}
boolean lessThan(At b) // Whether a < b
{z();
if (field == null || b.field == null) {z(); return result < b.result;}
z(); sameSize(b);
for(int i = width; i > 0; --i)
{z();
if (!getBit(i-1) && b.getBit(i-1)) {z(); return true;}
if ( getBit(i-1) && !b.getBit(i-1)) {z(); return false;}
}
z(); return false;
}
void lessThan(At b, At result) // Whether a < b
{z();
result.setInt(lessThan(b) ? 1 : 0);
}
boolean lessThanOrEqual(At b) // Whether a <= b
{z();
if (field == null || b.field == null) {z(); return result <= b.result;}
z(); sameSize(b);
return lessThan(b) || equal(b);
}
void lessThanOrEqual(At b, At result) // Whether a <= b
{z();
result.setInt(lessThanOrEqual(b) ? 1 : 0);
}
boolean greaterThan(At b) // Whether a > b
{z();
if (field == null || b.field == null) {z(); return result > b.result;}
z(); sameSize(b);
return !lessThan(b) && !equal(b);
}
void greaterThan(At b, At result) // Whether a > b
{z();
result.setInt(greaterThan(b) ? 1 : 0);
}
boolean greaterThanOrEqual(At b) // Whether a >= b
{z();
return !lessThan(b);
}
void greaterThanOrEqual(At b, At result) // Whether a >= b
{z();
result.setInt(greaterThanOrEqual(b) ? 1 : 0);
}
//D1 Arithmetic // Arithmetic on integers
//D2 Binary // Arithmetic on binary integers
int inc() {z(); final int i = getInt()+1; setInt(i); return i;} // Increment a variable treated as an signed binary integer with wrap around on overflow. Return the result after the increment.
int dec() {z(); final int i = getInt()-1; setInt(i); return i;} // Decrement a variable treated as an signed binary integer with wrap around on underflow. Return the result after the decrement.
int incPost() {z(); final int i = getInt(); setInt(i+1); return i;} // Increment a variable treated as an signed binary integer with wrap around on overflow. Return the result before the increment.
int decPost() {z(); final int i = getInt(); setInt(i-1); return i;} // Decrement a variable treated as an signed binary integer with wrap around on underflow. Return the result before the decrement.
} // At
At at(Layout.Field Field) // A field without indices or base addressing
{return new At(Field);
}
At at(Layout.Field Field, int...Indices) // A field with constant indices
{return new At(Field, Indices);
}
At at(Layout.Field Field, At...Indices) // A field with variable indices. Each index being a field with no indices
{return new At(Field, Indices);
}
class Constant extends At // A constant integer
{Constant(int constant)
{super(constant);
}
}
Constant constant(int constant)
{return new Constant(constant);
}
//D1 Print // Print a memory layout
class PrintPosition // Position in print
{final StringBuilder s = new StringBuilder();
int line = 1;
int bits = base;
final Stack<Integer> indices = new Stack<>();
}
public String toString() // Print the values of the layout variable in memory
{final PrintPosition pp = new PrintPosition();
if (name != null) pp.s.append("MemoryLayout: "+name+"\n");
if (memory.name != null) pp.s.append("Memory: "+memory.name+"\n");
pp.s.append(String.format
("%4s %1s %8s %8s %8s %8s %8s %s\n",
"Line", "T", "At", "Wide", "Size", "Indices", "Value", "Name"));
print(layout.top, pp, 0);
return pp.s.toString();
}
void print(Layout.Field field, PrintPosition pp, int indent) // Print the values of each variable in memory
{final int I = pp.indices.size();
final int[]in = new int[I];
for (int i = 0; i < I; i++) in[i] = pp.indices.elementAt(i);
pp.s.append(String.format("%4d %s %8d %8d",
pp.line, field.fieldType(), pp.bits, field.width));
final int start = pp.bits; // Start bit for this field
pp.bits += switch(field) // Width
{case Layout.Variable v -> field.width;
default -> 0;
};
pp.s.append(switch(field) // Size
{case Layout.Variable v -> String.format("%11s", "");
case Layout.Array a -> String.format("%11d", a.size);
case Layout.Structure s -> String.format("%11s", "");
default -> {stop("Unknown field type:", field); yield "";}
});
pp.s.append(" ");
for (int i = 0; i < 4; i++) // Indices
{if (i < pp.indices.size())
{pp.s.append(String.format("%2d", pp.indices.elementAt(i)));
}
else
{pp.s.append(" ");
}
}
pp.s.append(switch(field) // Value
{case Layout.Variable v ->
{final int a = field.locator.at(in);
final int n = getInt(field, in);
yield String.format("%13d", n);
}
case Layout.Array a -> String.format("%13s", "");
case Layout.Structure s -> String.format("%13s", "");
default -> {stop("Unknown field type:", field); yield "";}
});
pp.s.append(" "+(" ".repeat(indent))+field.name+"\n"); // Name
switch(field) // Sub fields
{case Layout.Variable v -> {}
case Layout.Array a -> // Array
{for (int i = 0; i < a.size; i++)
{pp.indices.push(i);
pp.line++;
print(a.element, pp, indent+1);
pp.indices.pop();
}
}
case Layout.Union u -> // Union
{final int end = pp.bits; // Save end point of union
for (int i = 0; i < u.fields.length; i++)
{pp.bits = start; // Restore the start point for each field of the union
pp.line++;
print(u.fields[i], pp, indent+1);
pp.bits = max(pp.bits, end); // Restore end point of union
}
}
case Layout.Structure s -> // Structure
{for (int i = 0; i < s.fields.length; i++)
{pp.line++;
print(s.fields[i], pp, indent+1);
}
}
default -> stop("Unknown field type:", field);
};
}
//D0 Tests // Testing
static class TestMemoryLayout
{Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
Layout.Variable b = l.variable ("b", 4);
Layout.Variable c = l.variable ("c", 4);
Layout.Structure s = l.structure("s", a, b, c);
Layout.Array A = l.array ("A", s, 3);
Layout.Variable d = l.variable ("d", 4);
Layout.Variable e = l.variable ("e", 4);
Layout.Structure S = l.structure("S", d, A, e);
Layout.Array B = l.array ("B", S, 3);
Layout.Array C = l.array ("C", B, 3);
MemoryLayout M;
TestMemoryLayout()
{l.compile();
M = new MemoryLayout();
M.memory(new Memory("ML", l.size()));
M.layout(l);
M.base(0);
M.memory.alternating(4);
}
}
static void test_get_set()
{TestMemoryLayout t = new TestMemoryLayout();
MemoryLayout m = t.M;
Layout l = m.layout;
ok(m.at( t.c, 0, 0, 0), "c[0,0,0](0+12)12=15");
m.setInt(t.c, 11, 0, 0, 0);
ok(m.at( t.c, 0, 0, 0), "c[0,0,0](0+12)12=11");
ok(m.at( t.c, 0, 0, 1), "c[0,0,1](0+24)24=0");
m.setInt(t.c, 11, 0, 0, 1);
ok(m.at( t.c, 0, 0, 1), "c[0,0,1](0+24)24=11");
ok(m.at( t.a, 0, 2, 2), "a[0,2,2](0+116)116=15");
m.setInt(t.a, 5, 0, 2, 2);
ok(m.at( t.a, 0, 2, 2), "a[0,2,2](0+116)116=5");
ok(m.at( t.b, 1, 2, 2), "b[1,2,2](0+252)252=15");
m.setInt(t.b, 7, 1, 2, 2);
ok(m.at( t.b, 1, 2, 2), "b[1,2,2](0+252)252=7");
ok(m.at( t.e, 1, 2), "e[1,2](0+260)260=15");
m.setInt(t.e, 11, 1, 2);
ok(m.at( t.e, 1, 2), "e[1,2](0+260)260=11");
}
static void test_boolean()
{TestMemoryLayout t = new TestMemoryLayout();
MemoryLayout m = t.M;
m.setInt(t.a, 1, 0, 1, 1);
m.setInt(t.a, 2, 0, 1, 2);
m.setInt(t.a, 1, 0, 2, 1);
m.setInt(t.a, 2, 0, 2, 2);
ok( m.at(t.a, 0, 1, 1).equal (m.at(t.a, 0, 2, 1)));
ok(!m.at(t.a, 0, 1, 1).equal (m.at(t.a, 0, 1, 2)));
ok(!m.at(t.a, 0, 1, 1).notEqual(m.at(t.a, 0, 2, 1)));
ok( m.at(t.a, 0, 1, 1).notEqual(m.at(t.a, 0, 1, 2)));
ok( m.at(t.a, 0, 1, 1).lessThan (m.at(t.a, 0, 1, 2)));
ok(!m.at(t.a, 0, 1, 1).lessThan (m.at(t.a, 0, 2, 1)));
ok( m.at(t.a, 0, 1, 1).lessThanOrEqual(m.at(t.a, 0, 1, 2)));
ok( m.at(t.a, 0, 1, 1).lessThanOrEqual(m.at(t.a, 0, 2, 1)));
ok(!m.at(t.a, 0, 1, 2).lessThanOrEqual(m.at(t.a, 0, 2, 1)));
ok(!m.at(t.a, 0, 1, 1).greaterThan (m.at(t.a, 0, 1, 2)));
ok( m.at(t.a, 0, 1, 2).greaterThan (m.at(t.a, 0, 1, 1)));
ok( m.at(t.a, 0, 2, 1).greaterThanOrEqual(m.at(t.a, 0, 1, 1)));
ok( m.at(t.a, 0, 1, 1).greaterThanOrEqual(m.at(t.a, 0, 2, 1)));
ok(!m.at(t.a, 0, 2, 1).greaterThanOrEqual(m.at(t.a, 0, 1, 2)));
}
static void test_copy()
{TestMemoryLayout t = new TestMemoryLayout();
MemoryLayout m = t.M;
m.setInt(t.a, 1, 0, 0, 0);
m.setInt(t.a, 2, 0, 0, 1);
m.setInt(t.a, 3, 0, 0, 2);
ok(m.at(t.a, 0, 0, 0), "a[0,0,0](0+4)4=1");
ok(m.at(t.a, 0, 0, 1), "a[0,0,1](0+16)16=2");
ok(m.at(t.a, 0, 0, 2), "a[0,0,2](0+28)28=3");
m.at(t.a, 0, 0, 1).move(m.at(t.a, 0, 0, 0));
m.at(t.a, 0, 0, 1).move(m.at(t.a, 0, 0, 1));
ok(m.at(t.a, 0, 0, 0), "a[0,0,0](0+4)4=1");
ok(m.at(t.a, 0, 0, 1), "a[0,0,1](0+16)16=1");
ok(m.at(t.a, 0, 0, 2), "a[0,0,2](0+28)28=3");
}
static void test_base()
{Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
Layout.Variable b = l.variable ("b", 4);
Layout.Variable c = l.variable ("c", 4);
Layout.Variable d = l.variable ("d", 4);
Layout.Structure s = l.structure("s", a, b, c, d);
l.compile();
final int N = l.size();
MemoryLayout m = new MemoryLayout();
m.layout(l);
m.memory(new Memory("ML", 2*N));
m.base(N);
m.memory.alternating(4);
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 S 16 16 s
2 V 16 4 0 a
3 V 20 4 15 b
4 V 24 4 0 c
5 V 28 4 15 d
""");
ok(m.getInt(a), 0);
ok(m.getInt(b), 15);
m.setInt(a, 9);
m.setInt(b, 10);
m.setInt(c, 11);
m.setInt(d, 12);
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 S 16 16 s
2 V 16 4 9 a
3 V 20 4 10 b
4 V 24 4 11 c
5 V 28 4 12 d
""");
}
static void test_based_array()
{final int B = 8;
Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
Layout.Array A = l.array ("A", a, 4);
l.compile();
MemoryLayout m = new MemoryLayout();
m.layout(l);
m.memory(new Memory("ML", l.size()+B));
m.base(B);
m.memory.alternating(4);
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 A 8 16 4 A
2 V 8 4 0 0 a
3 V 12 4 1 15 a
4 V 16 4 2 0 a
5 V 20 4 3 15 a
""");
ok(m.getInt(a, 0), 0);
ok(m.getInt(a, 1), 15);
ok(m.getInt(a, 2), 0);
ok(m.getInt(a, 3), 15);
m.setInt(a, 9, 0);
m.setInt(a, 10, 1);
m.setInt(a, 11, 2);
m.setInt(a, 12, 3);
ok(m.getInt(a, 0), 9);
ok(m.getInt(a, 1), 10);
ok(m.getInt(a, 2), 11);
ok(m.getInt(a, 3), 12);
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 A 8 16 4 A
2 V 8 4 0 9 a
3 V 12 4 1 10 a
4 V 16 4 2 11 a
5 V 20 4 3 12 a
""");
}
static void test_move()
{Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
Layout.Variable b = l.variable ("b", 4);
Layout.Variable c = l.variable ("c", 4);
Layout.Variable d = l.variable ("d", 4);
Layout.Structure s = l.structure("s", a, b, c, d);
l.compile();
MemoryLayout m = new MemoryLayout();
m.layout(l);
m.memory(new Memory("ML", l.size()));
m.memory.alternating(4);
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 S 0 16 s
2 V 0 4 0 a
3 V 4 4 15 b
4 V 8 4 0 c
5 V 12 4 15 d
""");
m.at(d).move(m.at(a), m.at(b));
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 S 0 16 s
2 V 0 4 0 a
3 V 4 4 0 b
4 V 8 4 0 c
5 V 12 4 0 d
""");
}
static void test_move_across()
{Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
Layout.Variable b = l.variable ("b", 4);
Layout.Structure s = l.structure("s", a, b);
l.compile();
Layout l1 = Layout.layout();
Layout.Field s1 = l1.duplicate("s", l);
l1.compile();
Layout l2 = Layout.layout();
Layout.Field s2 = l2.duplicate("s", l);
l2.compile();
MemoryLayout m1 = new MemoryLayout();
m1.layout(l1);
m1.memory(new Memory("ML", l1.size()));
MemoryLayout m2 = new MemoryLayout();
m2.layout(l2);
m2.memory(new Memory("ML", l2.size()));
m1.at(l1.get("a")).setInt(1);
m2.at(l2.get("b")).move(m1.at(l1.get("a")));
//stop(m1);
m1.ok("""
Line T At Wide Size Indices Value Name
1 S 0 8 s
2 V 0 4 1 a
3 V 4 4 0 b
""");
//stop(m2);
m2.ok("""
Line T At Wide Size Indices Value Name
1 S 0 8 s
2 V 0 4 0 a
3 V 4 4 1 b
""");
}
static void test_set_inc_dec_get()
{Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
Layout.Variable b = l.variable ("b", 4);
Layout.Structure s = l.structure("s", a, b);
l.compile();
MemoryLayout m = new MemoryLayout();
m.layout(l);
m.memory(new Memory("ML", l.size()));
m.at(a).setInt(1);
m.at(b).setInt(3);
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 S 0 8 s
2 V 0 4 1 a
3 V 4 4 3 b
""");
ok(m.new At(a).inc(), 2);
ok(m.new At(b).dec(), 2);
//stop(m);
m.ok("""
Line T At Wide Size Indices Value Name
1 S 0 8 s
2 V 0 4 2 a
3 V 4 4 2 b
""");
ok(m.at(a).getInt(), 2);
ok(m.at(b).getInt(), 2);
ok(m.new At(a).decPost(), 2);
ok(m.new At(b).incPost(), 2);
ok(m.at(a).getInt(), 1);
ok(m.at(b).getInt(), 3);
}
static void test_boolean_constant()
{Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
l.compile();
MemoryLayout m = new MemoryLayout();
m.layout(l);
m.memory(new Memory("ML", l.size()));
m.at(a).setInt(1);
MemoryLayout.At A = m.at(a),
c0 = m.constant(0), c1 = m.constant(1), c2 = m.constant(2);
final boolean T = true, F = false;
ok(A.equal (c0), F); ok(A.equal (c1), T); ok(A.equal (c2), F);
ok(A.notEqual(c0), T); ok(A.notEqual(c1), F); ok(A.notEqual(c2), T);
ok(A.lessThan (c0), F); ok(A.lessThan (c1), F); ok(A.lessThan (c2), T);
ok(A.lessThanOrEqual(c0), F); ok(A.lessThanOrEqual(c1), T); ok(A.lessThanOrEqual(c2), T);
ok(A.greaterThan (c0), T); ok(A.greaterThan (c1), F); ok(A.greaterThan (c2), F);
ok(A.greaterThanOrEqual(c0), T); ok(A.greaterThanOrEqual(c1), T); ok(A.greaterThanOrEqual(c2), F);
}
static void test_addressing()
{final int N = 8;
Layout l = Layout.layout();
Layout.Variable z = l.variable ("z", N);
Layout.Variable i = l.variable ("i", N);
Layout.Variable j = l.variable ("j", N);
Layout.Variable a = l.variable ("a", N);
Layout.Array A = l.array ("A", a, N);
Layout.Structure S = l.structure("S", z, i, j, A);
l.compile();
MemoryLayout m = new MemoryLayout();
m.layout(l);
m.memory(new Memory("ML", l.size()));
MemoryLayout.At Z = m.at(z), I = m.at(i), J = m.at(j);
Z.setOff().setInt(0);
I.setOff().setInt(1);
J.setOff().setInt(2);
MemoryLayout.At az = m.at(a, Z);
MemoryLayout.At ai = m.at(a, I);
MemoryLayout.At aj = m.at(a, J);
az.setOff().setInt(10);
ai.setOff().setInt(11);
aj.setOff().setInt(12);
//stop(m.memory);
ok(""+m.memory, """
Memory: ML
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0c0b 0a02 0100
""");
//stop(m);
ok(""+m, """
Memory: ML
Line T At Wide Size Indices Value Name
1 S 0 88 S
2 V 0 8 0 z
3 V 8 8 1 i
4 V 16 8 2 j
5 A 24 64 8 A
6 V 24 8 0 10 a
7 V 32 8 1 11 a
8 V 40 8 2 12 a
9 V 48 8 3 0 a
10 V 56 8 4 0 a
11 V 64 8 5 0 a
12 V 72 8 6 0 a
13 V 80 8 7 0 a
""");
I.setInt(3);
J.setInt(4);
ai.setOff().setInt(13);
aj.setOff().setInt(14);
//stop(m);
ok(""+m, """
Memory: ML
Line T At Wide Size Indices Value Name
1 S 0 88 S
2 V 0 8 0 z
3 V 8 8 3 i
4 V 16 8 4 j
5 A 24 64 8 A
6 V 24 8 0 10 a
7 V 32 8 1 11 a
8 V 40 8 2 12 a
9 V 48 8 3 13 a
10 V 56 8 4 14 a
11 V 64 8 5 0 a
12 V 72 8 6 0 a
13 V 80 8 7 0 a
""");
}
static void test_move_up()
{final int N = 8;
Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", N);
Layout.Array A = l.array ("A", a, 6);
l.compile();
Layout w = Layout.layout();
Layout.Variable z = w.variable ("z", N);
Layout.Variable i = w.variable ("i", N);
Layout.Field B = w.duplicate("A", l);
Layout.Structure S = w.structure("S", z, i, B);
w.compile();
MemoryLayout L = new MemoryLayout();
L.layout(l);
L.memory(new Memory("ML", l.size()));
MemoryLayout W = new MemoryLayout();
W.layout(w);
W.memory(new Memory("ML", w.size()));
for (int j = 0; j < A.size; j++) L.at(a, j).setInt(10+j);
W.at(i).setInt(2);
L.at(A).moveUp(W.at(i), W.at(B));
//L.at(A, W.at(z)).moveUp(W.at(i), W.at(B, W.at(z)));
//stop(L);
ok(L, """
Line T At Wide Size Indices Value Name
1 A 0 48 6 A
2 V 0 8 0 10 a
3 V 8 8 1 11 a
4 V 16 8 2 12 a
5 V 24 8 3 12 a
6 V 32 8 4 13 a
7 V 40 8 5 14 a
""");
//stop(W);
ok(W, """
Line T At Wide Size Indices Value Name
1 S 0 64 S
2 V 0 8 0 z
3 V 8 8 2 i
4 A 16 48 6 A
5 V 16 8 0 10 a
6 V 24 8 1 11 a
7 V 32 8 2 12 a
8 V 40 8 3 13 a
9 V 48 8 4 14 a
10 V 56 8 5 15 a
""");
}
static void test_move_down()
{final int N = 8;
Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", N);
Layout.Array A = l.array ("A", a, 6);
l.compile();
Layout w = Layout.layout();
Layout.Variable z = w.variable ("z", N);
Layout.Variable i = w.variable ("i", N);
Layout.Field B = w.duplicate("A", l);
Layout.Structure S = w.structure("S", z, i, B);
w.compile();
MemoryLayout L = new MemoryLayout();
L.layout(l);
L.memory(new Memory("ML", l.size()));
MemoryLayout W = new MemoryLayout();
W.layout(w);
W.memory(new Memory("ML", w.size()));
for (int j = 0; j < A.size; j++) L.at(a, j).setInt(10+j);
W.at(i).setInt(2);
L.at(A).moveDown(W.at(i), W.at(B));
//L.at(A, W.at(z)).moveUp(W.at(i), W.at(B, W.at(z)));
//stop(L);
ok(L, """
Line T At Wide Size Indices Value Name
1 A 0 48 6 A
2 V 0 8 0 10 a
3 V 8 8 1 11 a
4 V 16 8 2 13 a
5 V 24 8 3 14 a
6 V 32 8 4 15 a
7 V 40 8 5 15 a
""");
//stop(W);
ok(W, """
Line T At Wide Size Indices Value Name
1 S 0 64 S
2 V 0 8 0 z
3 V 8 8 2 i
4 A 16 48 6 A
5 V 16 8 0 10 a
6 V 24 8 1 11 a
7 V 32 8 2 12 a
8 V 40 8 3 13 a
9 V 48 8 4 14 a
10 V 56 8 5 15 a
""");
}
static void test_zero()
{Layout l = Layout.layout();
Layout.Variable a = l.variable ("a", 4);
Layout.Array A = l.array ("A", a, 6);
l.compile();
MemoryLayout L = new MemoryLayout();
L.layout(l);
L.memory(new Memory("ML", l.size()+16));
L.base(16);
L.memory.alternating(4);
//stop(L.memory);
ok(L.memory, """
Memory: ML
4... 4... 4... 4... 3... 3... 3... 3... 2... 2... 2... 2... 1... 1... 1... 1...
Line FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210 FEDC BA98 7654 3210
0 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00f0 f0f0 f0f0
""");
//stop(L);
ok(L, """
Memory: ML
Line T At Wide Size Indices Value Name
1 A 16 24 6 A