-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.c
720 lines (605 loc) · 15.2 KB
/
monitor.c
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
/*****************************************************************************
NAME
monitor.c -- parse-tree execution
SYNOPSIS
void die(char *msg, ...)
void warn(char *msg, ...)
void make_scalar(value *v, scalar i)
void copy_value(value v);
value allocate_value(int rank, int i, int j)
void deallocate_value(value *v)
void cupl_reset_write()
void cupl_eol_write()
void cupl_scalar_write(char *name, scalar quant)
void cupl_string_write(char *s)
value cupl_add(value, value)
value cupl_multiply(value, value)
value cupl_subtract(value, value)
value cupl_divide(value, value)
value cupl_uminus(value, value)
value cupl_abs(value)
value cupl_atan(value)
value cupl_cos(value)
value cupl_exp(value)
value cupl_floor(value)
value cupl_ln(value)
value cupl_log(value)
value cupl_sqrt(value)
value cupl_max(value, value)
value cupl_min(value, value)
value cupl_rand(value)
bool cupl_eq(value, value)
bool cupl_lt(value, value)
bool cupl_gt(value, value)
bool cupl_le(value, value)
bool cupl_ge(value, value)
value cupl_det(value);
value cupl_dot(value, vlue);
value cupl_inv(value);
value cupl_posmax(value);
value cupl_posmin(value);
value cupl_sgm(value right)
value cupl_trc(value right)
value cupl_trn(value right)
DESCRIPTION
Runtime support. This is segregated from the execute() code in case anyone
ever wants to write a back end that is a compiler. For the same reason
we do an allocate each time an intrinsic returns a value. This is
relatively inefficient but means these functions could be used as a runtime
library.
LICENSE
SPDX-License-Identifier: BSD-2-clause
*****************************************************************************/
/*LINTLIBRARY*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include <string.h>
#include "cupl.h"
#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))
/****************************************************************************
*
* Error reporting
*
****************************************************************************/
void warn(char *msg, ...)
/* warn of an error and die */
{
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
}
noreturn void die(char *msg, ...)
/* complain of a fatal error and die */
{
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
exit(1);
}
/****************************************************************************
*
* Value allocation
*
****************************************************************************/
void make_scalar(value *v, scalar i)
/* initialize a scalar value element */
{
v->rank = 0;
v->width = v->depth = 1;
v->elements = (scalar *)malloc(sizeof(scalar));
v->elements[0] = i;
}
value copy_value(value v)
/* male a new copy of a value element */
{
value newvalue = v;
newvalue.elements = (scalar *)malloc(sizeof(scalar) * v.width * v.depth);
memcpy(newvalue.elements, v.elements,
sizeof(scalar) * v.width * v.depth);
return(newvalue);
}
value allocate_value(int rank, int i, int j)
/* allocate a value of given shape */
{
value v;
v.rank = rank;
v.width = j; v.depth = i;
v.elements = (scalar *)calloc(sizeof(scalar), i * j);
return(v);
}
void deallocate_value(value *v)
/* destroy a value copy, only if its reference count is 1 */
{
(void) free(v->elements);
v->elements = (scalar *)NULL;
}
/****************************************************************************
*
* Output support
*
****************************************************************************/
static int used;
void cupl_reset_write(void)
{
used = 0;
}
void cupl_eol_write(void)
{
(void) putchar('\n');
used = 0;
}
static void needspace(int w)
/* emit a LF if there are not more than w spaces left on the line */
{
used += w;
if (used >= linewidth)
{
cupl_eol_write();
used = w;
}
}
void cupl_scalar_write(char *name, scalar quant)
/* write a numeric or skip a field in CUPL style */
{
if (name)
{
needspace(2 * fieldwidth);
(void) printf("%*s = ", fieldwidth - 3, name);
}
else
needspace(fieldwidth);
if (0.001 < fabs(quant) && fabs(quant) < 100000)
(void) printf("%*.9f", fieldwidth, quant);
else
(void) printf("%*.9E", fieldwidth, quant);
}
void cupl_string_write(char *s)
/* write a string, or just skip the field */
{
needspace(fieldwidth);
(void) printf("%-*s", fieldwidth, s);
}
/****************************************************************************
*
* Functions for arithmetic intrinsics
*
****************************************************************************/
#define CONGRUENT(l, r) ((l.rank == r.rank) \
&& (l.width == r.width) \
&& (l.depth == r.depth))
value cupl_add(value left, value right)
/* add two CUPL values */
{
if (!CONGRUENT(left, right))
die("addition failed, operands of different sizes or ranks\n");
else
{
value result;
int n;
result = copy_value(right);
for (n = 0; n < left.width * left.depth; n++)
result.elements[n] = left.elements[n] + right.elements[n];
return(result);
}
}
value cupl_subtract(value left, value right)
/* subtract two CUPL values */
{
if (!CONGRUENT(left, right))
die("subtract failed, operands of different sizes or ranks\n");
else
{
value result;
int n;
result = copy_value(right);
for (n = 0; n < left.width * left.depth; n++)
result.elements[n] = left.elements[n] - right.elements[n];
return(result);
}
}
value cupl_multiply(value left, value right)
/* multiply two CUPL values */
{
if (left.rank == 0 && right.rank == 0)
{
value result;
make_scalar(&result, 0);
result.elements[0] = left.elements[0] * right.elements[0];
return(result);
}
else if (left.width == right.depth)
{
value result;
int i, j;
result = allocate_value(2, left.depth, right.width);
for (i = 0; i < left.depth; i++)
for (j = 0; j < right.width; j++)
{
int k;
scalar p = 0;
for (k = 0; k < left.width; k++)
p += SUB(left, i, k)[0] * SUB(right, k, j)[0];
SUB(result, i, j)[0] = p;
}
return(result);
}
else
die("multiplication attempt on non-conformable matrices\n");
}
value cupl_divide(value left, value right)
/* divide two CUPL values */
{
if (left.rank == 0 && right.rank == 0)
{
value result;
make_scalar(&result, 0);
result.elements[0] = left.elements[0] / right.elements[0];
return(result);
}
else if (right.rank == 0)
{
value result;
int n;
result = copy_value(left);
for (n = 0; n < left.width * left.depth; n++)
result.elements[n] = left.elements[n] / right.elements[0];
return(result);
}
else
die("division of rank %d by rank %d value is undefined\n",
left.rank, right.rank);
}
value cupl_power(value left, value right)
/* apply power operation with two CUPL values */
{
if (left.rank == 0 && right.rank == 0)
{
value result;
make_scalar(&result, 0);
result.elements[0] = pow(left.elements[0], right.elements[0]);
return(result);
}
else
die("power operation on non-scalars is not yet supported\n");
}
value cupl_uminus(value right)
/* apply unary minus */
{
int n;
value result;
result = copy_value(right);
for (n = 0; n < right.width * right.depth; n++)
result.elements[n] = -result.elements[n];
return(result);
}
/****************************************************************************
*
* Scalar functions
*
****************************************************************************/
value cupl_abs(value right)
/* apply absolute-value function */
{
value result;
int n;
result = copy_value(right);
for (n = 0; n < right.width * right.depth; n++)
result.elements[n] = fabs(right.elements[n]);
return(result);
}
value cupl_atan(value right)
/* apply arctangent */
{
value result;
if (right.rank != 0)
die("ATAN is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
result.elements[0] = atan(right.elements[0]);
return(result);
}
}
value cupl_cos(value right)
/* apply cosine */
{
value result;
if (right.rank != 0)
die("COS is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
result.elements[0] = cos(right.elements[0]);
return(result);
}
}
value cupl_exp(value right)
/* apply exponent function */
{
value result;
if (right.rank != 0)
die("EXP is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
result.elements[0] = exp(right.elements[0]);
return(result);
}
}
value cupl_floor(value right)
/* apply floor function */
{
value result;
if (right.rank != 0)
die("FLOOR is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
result.elements[0] = floor(right.elements[0]);
return(result);
}
}
value cupl_ln(value right)
/* apply ln function */
{
value result;
if (right.rank != 0)
die("LOG is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
result.elements[0] = log(right.elements[0]);
return(result);
}
}
value cupl_log(value right)
/* apply log10 function */
{
value result;
if (right.rank != 0)
die("LOG is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
result.elements[0] = log10(right.elements[0]);
return(result);
}
}
value cupl_sqrt(value right)
/* apply square root */
{
value result;
if (right.rank != 0)
die("SQRT is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
result.elements[0] = sqrt(right.elements[0]);
return(result);
}
}
/****************************************************************************
*
* Special functions
*
****************************************************************************/
value cupl_max(value left, value right)
/* apply max function */
{
value result;
int n;
make_scalar(&result, right.elements[0]);
for (n = 0; n < right.width * right.depth; n++)
if (result.elements[0] < right.elements[n])
result.elements[0] = right.elements[n];
return(result);
}
value cupl_min(value left, value right)
/* apply min functionnot yet supported for vectors and matrices */
{
value result;
int n;
make_scalar(&result, right.elements[0]);
for (n = 0; n < right.width * right.depth; n++)
if (result.elements[0] > right.elements[n])
result.elements[0] = right.elements[n];
return(result);
}
value cupl_rand(value right)
/* get a random number from a seed */
{
value result;
if (right.rank != 0)
die("RAND is only defined for scalar arguments\n");
else
{
make_scalar(&result, 0);
srand(right.elements[0]);
result.elements[0] = rand();
return(result);
}
}
/****************************************************************************
*
* Relations
*
****************************************************************************/
/*
* Original CUPL's roundoff rule for relations seems to have been designed
* to throw away all digits of precision more than 14,
*/
#define FUZZY_EQUAL(m, n) (fabs((m) - (n)) < 10e-15)
bool cupl_eq(value v1, value v2)
/* test any two CUPL values for pairwise equality */
{
if (!CONGRUENT(v1, v2))
die("comparison failed, operands of different sizes or ranks\n");
else
{
int n;
for (n = 0; n < v2.width * v2.depth; n++)
{
scalar e1 = v1.elements[n], e2 = v2.elements[n];
if (!FUZZY_EQUAL(e1, e2))
return(false);
}
return(true);
}
}
bool cupl_le(value v1, value v2)
/* are all elements of v1 less than or equal to their correspondents in v2? */
{
if (!CONGRUENT(v1, v2))
die("LE failed, operands of different sizes or ranks\n");
else
{
bool equal = true;
int n;
for (n = 0; n < v2.width * v2.depth; n++)
{
scalar e1 = v1.elements[n], e2 = v2.elements[n];
if (!FUZZY_EQUAL(e1, e2) && e1 > e2)
return(false);
}
return(true);
}
}
bool cupl_ge(value v1, value v2)
/* are all els of v1 greater than or equal to their correspondents in v2? */
{
if (!CONGRUENT(v1, v2))
die("GE failed, operands of different sizes or ranks\n");
else
{
int n;
for (n = 0; n < v2.width * v2.depth; n++)
{
scalar e1 = v1.elements[n], e2 = v2.elements[n];
if (!FUZZY_EQUAL(e1, e2) && e1 < e2)
return(false);
}
return(true);
}
}
bool cupl_lt(value v1, value v2)
/* strange CUPL definition #1, see appendix A */
{
return(cupl_le(v1, v2) && !cupl_eq(v1, v2));
}
bool cupl_gt(value v1, value v2)
/* strange CUPL definition #2, see appendix A */
{
return(cupl_ge(v1, v2) && !cupl_eq(v1, v2));
}
/****************************************************************************
*
* Matrix functions
*
****************************************************************************/
value cupl_det(value right)
{
/* FIXME: implement DET */
die("the determinant function is not yet implemented");
}
value cupl_dot(value left, value right)
/* compute inner or dot product of two vectors */
{
if (!CONGRUENT(left, right) || right.rank != 1)
die("DOT failed, operands of different sizes or ranks\n");
else
{
value result;
int n;
make_scalar(&result, 0);
for (n = 0; n < left.width * left.depth; n++)
result.elements[0] += left.elements[n] * right.elements[n];
return(result);
}
}
value cupl_inv(value right)
/* compute the inverse of a matrix */
{
/* FIXME: implement INV */
die("the inverse function is not yet implemented\n");
}
value cupl_posmax(value right)
/* row position of maximum element of argument */
{
if (right.rank == 0)
die("POSMAX argument is a scalar");
else
{
value result;
int n;
scalar maxel = right.elements[0];;
make_scalar(&result, 0);
for (n = 0; n < right.width * right.depth; n++)
if (maxel < right.elements[n])
{
result.elements[0] = SUBI(right, n);
maxel = right.elements[0];
}
return(result);
}
}
value cupl_posmin(value right)
/* row position of minimum element of argument */
{
if (right.rank == 0)
die("POSMIN argument is a scalar");
else
{
value result;
int n;
scalar minel = right.elements[0];;
make_scalar(&result, 0);
for (n = 0; n < right.width * right.depth; n++)
if (minel > right.elements[n])
{
result.elements[0] = SUBI(right, n);
minel = right.elements[n];
}
return(result);
}
}
value cupl_sgm(value right)
/* compute the sum of the elements of a matrix */
{
value result;
int n;
make_scalar(&result, 0);
for (n = 0; n < right.width * right.depth; n++)
result.elements[0] += fabs(right.elements[n]);
return(result);
}
value cupl_trc(value right)
/* compute sum of elements on main diagonals */
{
if (right.rank != 2 || right.width != right.depth)
die("TRACE failed, operands is not a square matrix\n");
else
{
value result;
int n;
make_scalar(&result, 0);
for (n = 0; n < right.width; n++)
result.elements[0] += SUB(right, n, n)[0];
return(result);
}
}
value cupl_trn(value right)
/* compute the transpose of a matrix */
{
value result = allocate_value(right.rank, right.depth, right.width);
int i, j;
for (i = 0; i < right.depth; i++)
for (j = 0; j < right.width; j++)
SUB(result, j, i)[0] = SUB(right, i, j)[0];
return(result);
}
/* monitor.c ends here */