forked from alastairreid/asl-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstdlib.isa
More file actions
529 lines (463 loc) · 23.6 KB
/
Copy pathstdlib.isa
File metadata and controls
529 lines (463 loc) · 23.6 KB
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
////////////////////////////////////////////////////////////////
// Standard library
//
// Copyright (C) 2022-2026 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
////////////////////////////////////////////////////////////////
__builtin type String;
__builtin type Std::RAM; // todo: should have a type parameter
__builtin type Slice; // todo: should have a type parameter
type Bit = Bits(1);
enumeration Boolean = { False, True };
__builtin function Std::Boolean::Eq(left : Boolean, right : Boolean) -> Boolean;
__builtin function Std::Boolean::Ne(left : Boolean, right : Boolean) -> Boolean;
__builtin function Std::Boolean::Not(x : Boolean) -> Boolean;
__builtin function Std::Boolean::Lazy_And(left : Boolean, right : Boolean) -> Boolean;
__builtin function Std::Boolean::Strict_And(left : Boolean, right : Boolean) -> Boolean;
__builtin function Std::Boolean::Lazy_Or(left : Boolean, right : Boolean) -> Boolean;
__builtin function Std::Boolean::Strict_Or(left : Boolean, right : Boolean) -> Boolean;
__builtin function Std::Integer::Eq(left : Integer, right : Integer) -> Boolean;
__builtin function Std::Integer::Ne(left : Integer, right : Integer) -> Boolean;
__builtin function Std::Integer::Gt(left : Integer, right : Integer) -> Boolean;
__builtin function Std::Integer::Ge(left : Integer, right : Integer) -> Boolean;
__builtin function Std::Integer::Le(left : Integer, right : Integer) -> Boolean;
__builtin function Std::Integer::Lt(left : Integer, right : Integer) -> Boolean;
__builtin function Std::Integer::Is_Power_Of_Two(x : Integer) -> Boolean;
__builtin function Std::Integer::Add(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Negate(left : Integer) -> Integer;
__builtin function Std::Integer::Subtract(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Shift_Left(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Shift_Right(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Multiply(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Positive_Divide(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Positive_Remainder(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Truncated_Divide(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Truncated_Remainder(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Floor_Divide(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Floor_Remainder(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Ceiling_Divide(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Ceiling_Remainder(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Mod_Power_Of_Two(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Align_Down(left : Integer, right : Integer) -> Integer;
__builtin function Std::Integer::Power2(x : Integer) -> Integer;
__builtin function Std::Bits::From_Integer(x : Integer, size : Integer) -> Bits(size);
__builtin function Std::Bits::In_Mask(implicit size : Integer, left : Bits(size), right : __mask(size)) -> Boolean;
__builtin function Std::Bits::Not_In_Mask(implicit size : Integer, left : Bits(size), right : __mask(size)) -> Boolean;
__builtin function Std::Bits::Eq(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Boolean;
__builtin function Std::Bits::Ne(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Boolean;
__builtin function Std::Bits::Add(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Bits(size);
__builtin function Std::Bits::Subtract(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Bits(size);
__builtin function Std::Bits::Multiply(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Bits(size);
__builtin function Std::Bits::And(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Bits(size);
__builtin function Std::Bits::Or(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Bits(size);
__builtin function Std::Bits::Xor(implicit size : Integer, left : Bits(size), right : Bits(size)) -> Bits(size);
__builtin function Std::Bits::Not(implicit size : Integer, x : Bits(size)) -> Bits(size);
__builtin function Std::Bits::Zero(size : Integer) -> Bits(size);
__builtin function Std::Bits::All_Ones(size : Integer) -> Bits(size);
__builtin function Std::Bits::Shift_Left_Logical_Restricted(implicit size : Integer, x : Bits(size), amount : Integer) -> Bits(size);
__builtin function Std::Bits::Shift_Right_Logical_Restricted(implicit size : Integer, x : Bits(size), amount : Integer) -> Bits(size);
__builtin function Std::Bits::Shift_Right_Arithmetic_Restricted(implicit size : Integer, x : Bits(size), amount : Integer) -> Bits(size);
__builtin function Std::Bits::Power2(x : Integer, size : Integer) -> Bits(size);
// Construct 'Zero_Extend(All_Ones(w), n)'
// e.g. Mk_Mask(3, 8) == '00000 111'
// This is used in the Bitmask lowering transformation
__builtin function Std::Bits::Mk_Mask(w : Integer, size : Integer) -> Bits(size);
__builtin function Std::Bits::Zero_Extend(implicit source_size : Integer, x : Bits(source_size), target_size : Integer) -> Bits(target_size);
__builtin function Std::Bits::Sign_Extend(implicit source_size : Integer, x : Bits(source_size), target_size : Integer) -> Bits(target_size);
// todo: since this is not part of the language, change Std:: to something else?
__builtin function Std::SInt::Eq(implicit size : Integer, left : __sint(size), right : __sint(size)) -> Boolean;
__builtin function Std::SInt::Ne(implicit size : Integer, left : __sint(size), right : __sint(size)) -> Boolean;
__builtin function Std::SInt::Gt(implicit size : Integer, left : __sint(size), right : __sint(size)) -> Boolean;
__builtin function Std::SInt::Ge(implicit size : Integer, left : __sint(size), right : __sint(size)) -> Boolean;
__builtin function Std::SInt::Le(implicit size : Integer, left : __sint(size), right : __sint(size)) -> Boolean;
__builtin function Std::SInt::Lt(implicit size : Integer, left : __sint(size), right : __sint(size)) -> Boolean;
__builtin function Std::SInt::Add(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Negate(implicit size : Integer, x : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Subtract(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Shift_Left(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Shift_Right(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Multiply(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Truncated_Divide(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Truncated_Remainder(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Floor_Divide(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Floor_Remainder(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Is_Power_Of_Two(implicit size : Integer, x : __sint(size)) -> Boolean;
__builtin function Std::SInt::Exp2(implicit size : Integer, x : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Align_Down(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::Mod_Power_Of_Two(implicit size : Integer, left : __sint(size), right : __sint(size)) -> __sint(size);
__builtin function Std::SInt::To_Bits(implicit source_size : Integer, x : __sint(source_size), target_size : Integer) -> Bits(target_size);
__builtin function Std::SInt::Unsigned(implicit size : Integer, x : Bits(size)) -> __sint(size);
__builtin function Std::SInt::Signed(implicit size : Integer, x : Bits(size)) -> __sint(size);
__builtin function Std::SInt::To_Integer(implicit size : Integer, x : __sint(size)) -> Integer;
__builtin function Std::SInt::From_Integer(x : Integer, size : Integer) -> __sint(size);
__builtin function Std::SInt::Resize(implicit source_size : Integer, x : __sint(source_size), target_size : Integer) -> __sint(target_size);
__builtin function Std::Print::Bits::Hex(implicit size : Integer, x : Bits(size)) -> ();
__builtin function Std::Print::Char(x : Integer) -> ();
__builtin function Std::Print::Integer::Dec(x : Integer) -> ();
__builtin function Std::Print::Integer::Hex(x : Integer) -> ();
__builtin function Std::Print::SInt::Dec(implicit size : Integer, x : __sint(size)) -> ();
__builtin function Std::Print::SInt::Hex(implicit size : Integer, x : __sint(size)) -> ();
__builtin function Std::Print::String(x : String) -> ();
// Note: although this is a builtin function, it is expanded into a sequence
// of calls to the Std::Print::* functions by the typechecker
__builtin function Std::Print(x : String) -> ();
__builtin function Std::Info(level : Integer, x : String) -> ();
__builtin function Std::End_Execution(success : Boolean) -> ();
__builtin type Builtin::Foreign::CInt; // C int
// todo: these should all have size parameters
__builtin type Builtin::Foreign::UInts; // uint64_t[size/64]
__builtin type Builtin::Foreign::UInt; // uint{size}_t
__builtin type Builtin::Foreign::SInt; // sint{size}_t
__builtin type Builtin::Foreign::Int; // int
__builtin function Builtin::Foreign::CInt::To_Integer(x : Builtin::Foreign::CInt) -> Integer;
__builtin function Builtin::Foreign::CInt::From_Integer(x : Integer) -> Builtin::Foreign::CInt;
__builtin function Builtin::Foreign::UInts::To_Bits(implicit size : {0..}, x : Builtin::Foreign::UInts(size)) -> Bits(size);
__builtin function Builtin::Foreign::UInts::From_Bits(implicit size : {0..}, x : Bits(size)) -> Builtin::Foreign::UInts(size);
__builtin function Builtin::Foreign::UInt::To_Bits(implicit size : {8, 16, 32, 64}, x : Builtin::Foreign::UInt(size)) -> Bits(size);
__builtin function Builtin::Foreign::UInt::From_Bits(implicit size : {8, 16, 32, 64}, x : Bits(size)) -> Builtin::Foreign::UInt(size);
__builtin function Builtin::Foreign::UInt::To_Integer(implicit size : {8, 16, 32, 64}, x : Builtin::Foreign::UInt(size)) -> Integer;
__builtin function Builtin::Foreign::UInt::From_Integer(size : {8, 16, 32, 64}, x : Integer) -> Builtin::Foreign::UInt(size);
__builtin function Builtin::Foreign::SInt::To_Integer(implicit size : {8, 16, 32, 64}, x : Builtin::Foreign::SInt(size)) -> Integer;
__builtin function Builtin::Foreign::SInt::From_Integer(size : {8, 16, 32, 64}, x : Integer) -> Builtin::Foreign::SInt(size);
__builtin function Builtin::Foreign::Int::To_Integer(x : Builtin::Foreign::Int) -> Integer;
__builtin function Builtin::Foreign::Int::From_Integer(x : Integer) -> Builtin::Foreign::Int;
// todo: remove these __Trace functions
// Advance trace to next instruction
__builtin function __TraceNext() -> ();
// Trace a read/write of data/instruction from a physical memory address
__builtin function __TracePhysicalMemory
( is_read : Boolean, is_data : Boolean, pa : Integer, n : Integer,
physical_address : Bits(pa), val : Bits(n)
) -> ();
// Trace a read/write of data/instruction from a virtual memory address
// providing both the context ID and the physical address that the access maps to.
__builtin function __TraceVirtualMemory
( implicit C : Integer, va : Integer, pa : Integer, n : Integer,
is_read : Boolean, is_data : Boolean,
context : Bits(C), virtual_address : Bits(va), physical_address : Bits(pa), val : Bits(n)
) -> ();
// Trace a read of a page table entry from a physical address
// providing both the context ID and the level of this entry in the page table tree.
__builtin function __TracePageTableWalk
(implicit C : Integer,
implicit n : Integer,
pa : Integer, w : Integer,
context : Bits(C), level : Integer,
physical_address : Bits(pa), val : Bits(n)
) -> ();
// Emit an error message to trace
__builtin function __TraceError(kind : String, event : String) -> ();
// Emit an informational message to trace
__builtin function __TraceEvent(kind : String, event : String) -> ();
function Std::Bits::Add_int(implicit n : Integer, left : Bits(n), right : Integer) -> Bits(n)
begin
return Std::Bits::Add(left, Std::Bits::From_Integer(right, n));
end
function Std::Bits::Subtract_int(implicit n : Integer, left : Bits(n), right : Integer) -> Bits(n)
begin
return Std::Bits::Subtract(left, Std::Bits::From_Integer(right, n));
end
function Std::Bits::Multiply_int(implicit n : Integer, left : Bits(n), right : Integer) -> Bits(n)
begin
return Std::Bits::Multiply(left, Std::Bits::From_Integer(right, n));
end
function Std::Bits::Shift_Left_Logical(implicit size : Integer, x : Bits(size), amount : {0..}) -> Bits(size)
begin
if amount < size then
return Std::Bits::Shift_Left_Logical_Restricted(x, amount);
else
return Std::Bits::Zero(size);
endif;
end
function Std::Bits::Shift_Right_Logical(implicit size : Integer, x : Bits(size), amount : Integer) -> Bits(size)
begin
if amount < size then
return Std::Bits::Shift_Right_Logical_Restricted(x, amount);
else
return Std::Bits::Zero(size);
endif;
end
function Std::Bits::Shift_Right_Arithmetic(implicit size : Integer, x : Bits(size), amount : Integer) -> Bits(size)
begin
if amount < size then
return Std::Bits::Shift_Right_Arithmetic_Restricted(x, amount);
elsif x[size-1] == 0b0 then
return Std::Bits::Zero(size);
else
return Std::Bits::All_Ones(size);
endif;
end
function Std::Boolean::To_Bit(x : Boolean) -> Bit
begin
return (if x then 0b1 else 0b0);
end
__operator2 == = Std::Integer::Eq, Std::Bits::Eq;
__operator2 != = Std::Integer::Ne, Std::Bits::Ne;
__operator2 <= = Std::Integer::Le;
__operator2 >= = Std::Integer::Ge;
__operator2 < = Std::Integer::Lt;
__operator2 > = Std::Integer::Gt;
__operator2 and then = Std::Boolean::Lazy_And;
__operator2 or else = Std::Boolean::Lazy_Or;
__operator2 and = Std::Bits::And, Std::Boolean::Strict_And;
__operator2 or = Std::Bits::Or, Std::Boolean::Strict_Or;
__operator2 xor = Std::Bits::Xor;
__operator1 not = Std::Bits::Not, Std::Boolean::Not;
__operator2 + = Std::Integer::Add, Std::Bits::Add, Std::Bits::Add_int;
__operator2 - = Std::Integer::Subtract, Std::Bits::Subtract, Std::Bits::Subtract_int;
__operator1 - = Std::Integer::Negate;
__operator1 - = Std::SInt::Negate;
__operator2 * = Std::Integer::Multiply, Std::Bits::Multiply, Std::Bits::Multiply_int;
__operator2 / = Std::Integer::Positive_Divide; // restricted to positive values
__operator2 % = Std::Integer::Positive_Remainder; // restricted to positive values
__operator2 << = Std::Integer::Shift_Left;
__operator2 >> = Std::Integer::Shift_Right;
// These must be declared after the * and + operators
__builtin function Std::Bits::Repeat(implicit m : Integer, x : Bits(m), n : Integer) -> Bits(m*n);
__builtin function Std::Bits::Append(implicit m : Integer, implicit n : Integer, x : Bits(m), y : Bits(n)) -> Bits(m+n);
function Std::Integer::Exp(left : Integer, right : Integer) -> Integer
begin
if left == 2 then
return Std::Integer::Power2(right); // optimized case
else
assert right >= 0;
var result : Integer := 1;
for i := 1 to right do
result := result * left;
end;
return result;
end;
end
__operator2 ++ = Std::Bits::Append;
__operator2 ** = Std::Integer::Exp;
// These must be declared after the ** operator
__builtin function Std::Bits::Signed(implicit size : Integer, x : Bits(size)) -> {-2**(size-1) .. 2**(size-1)-1};
__builtin function Std::Bits::Unsigned(implicit size : Integer, x : Bits(size)) -> {0..2**size-1};
// omit since they are auto-generated
// __operator2 == = eq_bool;
// __operator2 != = ne_bool;
function Std::Integer::Abs(x : Integer) -> Integer
begin
return if x >= 0 then x else -x;
end
function Std::Integer::Max(left : Integer, right : Integer) -> Integer
begin
return if left >= right then left else right;
end
function Std::Integer::Min(left : Integer, right : Integer) -> Integer
begin
return if left <= right then left else right;
end
function Std::Integer::Log2(a : {1..}) -> {0..}
begin
assert Std::Integer::Is_Power_Of_Two(a);
var b := a;
var r := 0;
while b > 1 do
b := b / 2;
r := r + 1;
end;
return r;
end
function Std::Integer::Floor_Log2(a : {0..}) -> {0..}
begin
var b := a;
var r := 0;
while b > 1 do
b := b / 2;
r := r + 1;
end;
return r;
end
function Std::Integer::Ceil_Log2(a : {0..}) -> {0..}
begin
var b := a;
var r := 0;
while b >= 1 do
b := b / 2;
r := r + 1;
end;
return r;
end
function Std::Integer::Align_Up(x : Integer, y : Integer) -> Integer
begin
return Std::Integer::Align_Down(x + y - 1, y);
end
function Std::Integer::Is_Even(x : Integer) -> Boolean
begin
return x[0] == 0b0;
end
function Std::Integer::Is_Odd(x : Integer) -> Boolean
begin
return x[0] == 0b1;
end
function Std::Bits::Is_Zero(implicit size : Integer, x : Bits(size)) -> Boolean
begin
return x == Std::Bits::Zero(size);
end
function Std::Bits::Is_All_Ones(implicit size : Integer, x : Bits(size)) -> Boolean
begin
return x == Std::Bits::All_Ones(size);
end
function Std::Bits::Extend(implicit source_size : Integer, x : Bits(source_size), target_size : Integer, is_unsigned : Boolean) -> Bits(target_size)
begin
assert target_size >= source_size;
return (if is_unsigned then Std::Bits::Zero_Extend(x, target_size) else Std::Bits::Sign_Extend(x, target_size));
end
function Std::Bits::Length(implicit size : Integer, x : Bits(size)) -> Integer
begin
return size;
end
function Std::Bits::Signed_Saturate(x : Integer, size : {1..}) -> Bits(size)
begin
let r := if x >= 2**(size-1) then 2**(size-1) - 1
elsif x < - 2**(size-1) then - 2**(size-1)
else x;
return r[0 +: size];
end
function Std::Bits::Unsigned_Saturate(x : Integer, size : Integer) -> Bits(size)
begin
let r := if x >= 2**size then 2**size - 1
elsif x < 0 then 0
else x;
return r[0 +: size];
end
function Std::Bits::Saturate(x : Integer, size : {1..}, is_unsigned : Boolean) -> Bits(size)
begin
return (if is_unsigned then Std::Bits::Unsigned_Saturate(x, size) else Std::Bits::Signed_Saturate(x, size));
end
function Std::Bits::Count_Set_Bits(implicit size : Integer, x : Bits(size)) -> {0 .. size}
begin
var result : {0 .. size} := 0;
for i := 0 to size-1 do
if x[i] == 0b1 then
result := result + 1;
end;
end;
return result;
end
function Std::Bits::Count_Leading_Zero_Bits(implicit size : Integer, x : Bits(size)) -> {0 .. size}
begin
for i := size-1 downto 0 do
if x[i] == 0b1 then
return size - i - 1;
end;
end;
return size;
end
function Std::Bits::Count_Trailing_Zero_Bits(implicit size : Integer, x : Bits(size)) -> {0 .. size}
begin
for i := 0 to size-1 do
if x[i] == 0b1 then
return i;
end;
end;
return size;
end
function Std::Bits::Count_Leading_Sign_Bits(implicit size : {1..}, x : Bits(size)) -> {1 .. size}
begin
return Std::Bits::Count_Leading_Zero_Bits(x[size-1:1] xor x[size-2:0]);
end
function Std::Bits::Rotate_Left(implicit size : Integer, x : Bits(size), amount : Integer) -> Bits(size)
begin
let distance := amount % size;
return (x ++ x)[(size - distance) +: size];
end
function Std::Bits::Rotate_Right(implicit size : Integer, x : Bits(size), amount : Integer) -> Bits(size)
begin
let distance := amount % size;
return (x ++ x)[distance +: size];
end
function Std::Bits::Align_Down(implicit size : Integer, x : Bits(size), y : Integer) -> Bits(size)
begin
assert Is_Power_Of_Two(y);
return x and not (y-1)[0 +: size];
end
function Std::Bits::Align_Up(implicit size : Integer, x : Bits(size), y : Integer) -> Bits(size)
begin
assert Is_Power_Of_Two(y);
return (x + y - 1) and not (y-1)[0 +: size];
end
function Std::Bits::Is_Parity_Even(implicit size : Integer, x : Bits(size)) -> Boolean
begin
var r : Bit := 0b0;
for i := 0 to size - 1 do
r := r xor x[i];
end;
return r == 0b0;
end
function Std::Bits::Is_Parity_Odd(implicit size : Integer, x : Bits(size)) -> Boolean
begin
var r : Bit := 0b0;
for i := 0 to size - 1 do
r := r xor x[i];
end;
return r == 0b1;
end
function Std::Print::Boolean(x : Boolean)
begin
if x then
Std::Print::String("True");
else
Std::Print::String("False");
end;
end
function Std::Unreachable()
begin
Std::Print("Unreachable() function called.\n");
Std::Print("This should be impossible - report a bug in the specification\n");
Std::End_Execution(False);
end
// todo: decide whether these are 'standard' or not
__builtin function Std::RAM::Init(ram : Std::RAM(a), val : Bits(64)) -> ();
__builtin function Std::RAM::Read(implicit address_size : Integer, ram : Std::RAM(address_size), address : Bits(address_size), data_size : Integer) -> Bits(8*data_size);
__builtin function Std::RAM::Write(implicit address_size : Integer, ram : Std::RAM(address_size), address : Bits(address_size), data_size : Integer, val : Bits(8*data_size)) -> ();
use Std::Integer::Abs as Abs;
use Std::Integer::Align_Down as Integer::Align_Down;
use Std::Integer::Align_Up as Integer::Align_Up;
use Std::Integer::Floor_Divide as Floor_Divide;
use Std::Integer::Floor_Remainder as Floor_Remainder;
use Std::Integer::Is_Even as Is_Even;
use Std::Integer::Is_Odd as Is_Odd;
use Std::Integer::Is_Power_Of_Two as Is_Power_Of_Two;
use Std::Integer::Log2 as Log2;
use Std::Integer::Floor_Log2 as Floor_Log2;
use Std::Integer::Ceil_Log2 as Ceil_Log2;
use Std::Integer::Max as Max;
use Std::Integer::Min as Min;
use Std::Integer::Power2 as Integer::Power2;
use Std::Integer::Truncated_Divide as Truncated_Divide;
use Std::Integer::Truncated_Remainder as Truncated_Remainder;
use Std::Bits::Align_Down as Bits::Align_Down;
use Std::Bits::Align_Up as Bits::Align_Up;
use Std::Bits::All_Ones as All_Ones;
use Std::Bits::Count_Leading_Sign_Bits as Count_Leading_Sign_Bits;
use Std::Bits::Count_Leading_Zero_Bits as Count_Leading_Zero_Bits;
use Std::Bits::Count_Set_Bits as Count_Set_Bits;
use Std::Bits::Count_Trailing_Zero_Bits as Count_Trailing_Zero_Bits;
use Std::Bits::Extend as Extend;
use Std::Bits::Is_All_Ones as Is_All_Ones;
use Std::Bits::Is_Parity_Even as Is_Parity_Even;
use Std::Bits::Is_Parity_Odd as Is_Parity_Odd;
use Std::Bits::Is_Zero as Is_Zero;
use Std::Bits::Length as Bits::Length;
use Std::Bits::Power2 as Bits::Power2;
use Std::Bits::Repeat as Repeat;
use Std::Bits::Rotate_Left as Rotate_Left;
use Std::Bits::Rotate_Right as Rotate_Right;
use Std::Bits::Saturate as Saturate;
use Std::Bits::Shift_Left_Logical as Shift_Left_Logical;
use Std::Bits::Shift_Right_Arithmetic as Shift_Right_Arithmetic;
use Std::Bits::Shift_Right_Logical as Shift_Right_Logical;
use Std::Bits::Sign_Extend as Sign_Extend;
use Std::Bits::Signed as Signed;
use Std::Bits::Signed_Saturate as Signed_Saturate;
use Std::Bits::Unsigned as Unsigned;
use Std::Bits::Unsigned_Saturate as Unsigned_Saturate;
use Std::Bits::Zero as Zero;
use Std::Bits::Zero_Extend as Zero_Extend;
use Std::Print as Print;
use Std::Unreachable as Unreachable;
////////////////////////////////////////////////////////////////
// End
////////////////////////////////////////////////////////////////