File: ydata.c

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (814 lines) | stat: -rw-r--r-- 22,728 bytes parent folder | download | duplicates (3)
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
/*
    YDATA.C
    Implement functions for Yorick-specific types of data.

    $Id: ydata.c,v 1.1 1993/08/27 18:32:09 munro Exp $
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

#include "bcast.h"
#include "defmem.h"
#include "defstr.h"

/* Intended for use by the print() and grow() functions -- dangerous
   because it zeroes the contents of the source array to avoid
   having to deal with pointers.  */
extern Array *GrowArray(Array *array, long extra);

extern BuiltIn Y_yorick_stats, Y_symbol_def, Y_symbol_set;

/* Required for FetchLValue, StoreLValue */
extern void ReadGather(void *dst, void *srcM, long srcD, StructDef *base,
		       long number, const Strider *strider);
extern void WriteScatter(void *src, void *dstM, long dstD, StructDef *base,
			 long number, const Strider *strider);

/* function defined in yrdwr.c required for StoreLValue */
extern void SetSequentialWrite(IOStream *file, long last);

/*--------------------------------------------------------------------------*/

Array *GrowArray(Array *array, long extra)
{
  Array *result;
  StructDef *base= array->type.base;
  long number= array->type.number;
  Dimension *tmp= tmpDims;
  if (extra<=0) return array;
  if (!array->type.dims)
    array->type.dims= NewDimension(1L, 1L, (Dimension *)0);
  tmpDims= 0;
  FreeDimension(tmp);
  tmpDims= CopyDims(array->type.dims, (Dimension *)0, 1);
  tmpDims->number+= extra;
  result= NewArray(base, tmpDims);

  /* do direct copy of array to result, then ZERO array -- this avoids
     potential cost of pointer copies */
  memcpy(result->value.c, array->value.c, number*base->size);
  if (base->Copy!=&CopyX) memset(array->value.c, 0, number*base->size);
  return result;
}

Function *NewFunction(Symbol *consts, long nConsts, int nPos, int nKey,
		      int nLocal, long hasPL, int maxStackDepth,
		      Instruction *code, long codeSize)
{
  Function *func= Ymalloc(sizeof(Function)+codeSize*sizeof(Instruction));
  long frameSize= 1+nPos+(hasPL&1)+nKey+nLocal;
  Instruction *fcode= &func->code[frameSize];
  long i;
  func->references= 0;
  func->ops= &functionOps;
  func->constantTable= consts;
  func->nConstants= nConsts;
  func->nReq= frameSize+maxStackDepth+2;
  func->nPos= nPos;
  func->nKey= nKey;
  func->nLocal= nLocal;
  func->hasPosList= hasPL;
  codeSize-= frameSize-1;
  /* YpFunc puts the frame variables (parameters and locals) at the end
     of the code, switch them to the beginning now.  */
  for (i=0 ; i<codeSize ; i++) fcode[i]= code[i];
  code+= codeSize;
  fcode= func->code;
  for (i=0 ; i<frameSize ; i++) fcode[i]= code[i];
  return func;
}

void FreeFunction(void *v)  /* ******* Use Unref(func) ******* */
{
  Function *func= v;
  Symbol *cnst= func->constantTable;
  if (cnst) {  /* must free any string constants */
    long n= func->nConstants;
    while (n--) {
      if (cnst->ops==&dataBlockSym) Unref(cnst->value.db);
      cnst++;
    }
    Yfree(func->constantTable);
  }
  Yfree(func);
}

/* Set up a block allocator which grabs space for 64 range objects
   at a time.  Since Range contains an ops pointer, the alignment
   of a Range must be at least as strict as a void*.  */
static MemoryBlock rangeBlock= {0, 0, sizeof(Range),
				   64*sizeof(Range)};

Range *NewRange(long min, long max, long inc, int nilFlags)
{
  Range *range= NextUnit(&rangeBlock);
  range->references= 0;
  range->ops= &rangeOps;
  range->min= min;
  range->max= max;
  range->inc= inc;
  range->nilFlags= nilFlags;
  range->rf= 0;
  return range;
}

void FreeRange(void *range)  /* ******* Use Unref(range) ******* */
{
  FreeUnit(&rangeBlock , range);
}

/* Set up a block allocator which grabs space for 64 lvalue objects
   at a time.  Since LValue contains several pointers, the alignment
   of an LValue must be at least as strict as a void*.  */
static MemoryBlock lvalueBlock= {0, 0, sizeof(LValue),
				    64*sizeof(LValue)};

LValue *NewLValueD(long address, StructDef *base, Dimension *dims)
{
  long number= TotalNumber(dims);
  LValue *lvalue= NextUnit(&lvalueBlock);
  lvalue->references= 0;
  lvalue->ops= &lvalueOps;
  lvalue->owner= 0;
  lvalue->type.base= Ref(base);
  lvalue->type.dims= Ref(dims);
  lvalue->type.number= number;
  lvalue->address.d= address;
  lvalue->strider= 0;
  return lvalue;
}

LValue *NewLValueM(Array *owner, void *address,
		   StructDef *base, Dimension *dims)
{
  long number= TotalNumber(dims);
  LValue *lvalue= NextUnit(&lvalueBlock);
  lvalue->references= 0;
  lvalue->ops= &lvalueOps;
  lvalue->owner= Ref(owner);
  lvalue->type.base= Ref(base);
  lvalue->type.dims= Ref(dims);
  lvalue->type.number= number;
  lvalue->address.m= address;
  lvalue->strider= 0;
  return lvalue;
}

void FreeLValue(void *v)  /* ******* Use Unref(lvalue) ******* */
{
  LValue *lvalue= v;
  Unref(lvalue->owner);
  Unref(lvalue->type.base);
  FreeDimension(lvalue->type.dims);
  FreeStrider(lvalue->strider);
  FreeUnit(&lvalueBlock, lvalue);
}

/* Set up a block allocator which grabs space for 16 BIFunction
   objeccts at a time. */
static MemoryBlock bifBlock= {0, 0, sizeof(BIFunction),
		                 16*sizeof(BIFunction)};

BIFunction *NewBIFunction(BuiltIn *bi, long index)
{
  BIFunction *func= NextUnit(&bifBlock);
  func->references= 0;
  func->ops= &builtinOps;
  func->function= bi;
  func->index= index;
  return func;
}

static char warning[72];

void FreeBIFunction(void *bif)
{
  BIFunction *func= bif;
  strcpy(warning, "freeing builtin function ");
  strncat(warning,
	  func->index>=0? globalTable.names[func->index] : "<nameless>", 40L);
  YWarning(warning);
  FreeUnit(&bifBlock, func);
}

/*--------------------------------------------------------------------------*/

/* The basic idea of FetchLValue is to convert an LValue into an Array.
   This Array is created on the stack, but then popped into the given
   destination Symbol (usually the one holding the input LValue).  */
Array *FetchLValue(void *db, Symbol *dsts)
{
  LValue *lvalue= db;
  StructDef *base= lvalue->type.base;
  StructDef *model= base;
  IOStream *file= base->file;
  char *memory= file? 0 : lvalue->address.m;
  Array *darray;
  void *data;

  while (model->model) model= model->model;

  darray= PushDataBlock(NewArray(model, lvalue->type.dims));
  data= darray->value.c;

  ReadGather(data, memory, lvalue->address.d,
	     base, lvalue->type.number, lvalue->strider);
  if (file && file->pointeeList.table.nItems) ClearPointees(file, 0);

  PopTo(dsts);
  return darray;
}

/* StoreLValue stores the data in data to the specified LValue.
   This may require data conversion operations.  The input data
   MUST be of the type given by base->model->...->model, NOT base.  */
void StoreLValue(void *db, void *data)
{
  LValue *lvalue= db;
  StructDef *base= lvalue->type.base;
  IOStream *file= base->file;
  char *memory;
  long disk, number= lvalue->type.number;

  if (file) {
    disk= lvalue->address.d;
    memory= 0;  /* signal for WriteScatter to call WritePointees */
    if (base->addressType==2)
      SetSequentialWrite(file, disk+base->size*number);
  } else {
    memory= lvalue->address.m;
    disk= 0;
  }

  WriteScatter(data, memory, disk, base, number, lvalue->strider);
  if (file && file->pointeeList.table.nItems) ClearPointees(file, 1);
}

/*--------------------------------------------------------------------------*/

Symbol *globTab= 0;
HashTable globalTable;

long Globalize(const char *name, long n)
{
  if (!HashAdd(&globalTable, name, n)) {
    HASH_MANAGE(globalTable, Symbol, globTab);
    globTab[hashIndex].ops= &dataBlockSym;
    globTab[hashIndex].value.db= Ref(&nilDB);
  }
  return hashIndex;
}

long GlobalizeDB(const char *name, long n, void *db)
{
  long index= Globalize(name, n);
  if (globTab[index].ops==&dataBlockSym) {
    Unref(globTab[index].value.db);
    globTab[index].value.db= db;
  } else {
    globTab[index].value.db= db;
    globTab[index].ops= &dataBlockSym;
  }
  return index;
}

/*--------------------------------------------------------------------------*/

extern long yStackBlock, yStackSize;
long yStackBlock= 64;  /* number of stack elements to allocate at a time */
long yStackSize= 0;    /* current virtual machine stack size */

Symbol *spBottom= 0;   /* bottom of virtual machine stack */
Symbol *sp= 0;         /* current top of stack */
/* spBottom may change if the stack must be lengthened by CheckStack -
   spBottom is an extern so that the comination sp-spBottom may be
   computed and saved to refer to a given stack element */

int CheckStack(int n)
{
  long nNow= sp-spBottom;
  long nRequired= nNow + n;
  if (nRequired >= yStackSize) {
    nRequired= yStackBlock*(1 + nRequired/yStackBlock);
    sp= Yrealloc(spBottom, sizeof(Symbol)*nRequired);
    if (!sp) YError("memory manager failed in CheckStack");
    spBottom= sp;
    spBottom->ops= &intScalar;
    spBottom->value.i= 0;
    sp+= nNow;
    yStackSize= nRequired;
    return 1;
  } else {
    return 0;
  }
}

void PushIntValue(int i)
{
  register Symbol *stack= sp+1;
  stack->ops= &intScalar;
  stack->value.i= i;
  sp= stack;           /* sp updated AFTER new stack element intact */
}

void PushLongValue(long l)
{
  register Symbol *stack= sp+1;
  stack->ops= &longScalar;
  stack->value.l= l;
  sp= stack;           /* sp updated AFTER new stack element intact */
}

void PushDoubleValue(double d)
{
  register Symbol *stack= sp+1;
  stack->ops= &doubleScalar;
  stack->value.d= d;
  sp= stack;           /* sp updated AFTER new stack element intact */
}

int PushCopy(Symbol *s)
{
  register int isDB= (s->ops==&dataBlockSym);
  register Symbol *stack= sp+1;
  stack->ops= s->ops;
  if (isDB) stack->value.db= Ref(s->value.db);
  else stack->value= s->value;
  sp= stack;           /* sp updated AFTER new stack element intact */
  return isDB;
}

void *PushDataBlock(void *db)
{
  register Symbol *stack= sp+1;
  stack->ops= &dataBlockSym;
  stack->value.db= db;      /* does NOT increment reference counter */
  sp= stack;           /* sp updated AFTER new stack element intact */
  return db;
}

void Drop(int n)
{
  register Symbol *stack;
  while (n--) {
    stack= sp--;
    /* sp decremented BEFORE stack element is deleted */
    if (stack->ops==&dataBlockSym) Unref(stack->value.db);
  }
}

void PopTo(Symbol *s)
{
  DataBlock *old= s->ops==&dataBlockSym? s->value.db : 0;
  Symbol *stack= sp--;
  s->value= stack->value;
  s->ops= stack->ops;
  Unref(old);
}

/*--------------------------------------------------------------------------*/

void ReplaceRef(Symbol *stack)
{
  Symbol *ref;
  if (stack->ops!=&referenceSym) return;
  ref= &globTab[stack->index];
  if (ref->ops==&dataBlockSym) stack->value.db= Ref(ref->value.db);
  else stack->value= ref->value;
  stack->ops= ref->ops;     /* change ops only AFTER value updated */
}

/*--------------------------------------------------------------------------*/

static int EvenConform(Dimension *ldims, Dimension *rdims);
static void BdCast(Operand *op, Dimension *ddims);

/* Conform returns a bit mask indicating the status of the conformability
   test:
   1  - right operand must be broadcast
   2  - left operand must be broadcast
   4  - not conformable
   8  - right operand has at least as many dimensions as left
   16 - left operand has at least as many dimensions as right   */
int Conform(Dimension *ldims, Dimension *rdims)
{
  int nl= CountDims(ldims);
  int nr= CountDims(rdims);
  int n= nl-nr;
  int casts;

  Dimension *prev, *next= tmpDims;
  tmpDims= 0;
  FreeDimension(next);

  if (n==0) {
    /* same number of dimensions */
    if (nl==0) return 24;  /* both scalar */
    casts= 24 | EvenConform(ldims, rdims);

  } else if (n>0) {
    /* left array is higher rank */
    casts= 16;
    while (n--) {
      if (ldims->number>1) casts|= 1;  /* must broadcast right operand */
      tmpDims= NewDimension(ldims->number, ldims->origin, tmpDims);
      ldims= ldims->next;
    }
    casts|= EvenConform(ldims, rdims);

  } else {
    /* right array is higher rank */
    casts= 8;
    while (n++) {
      if (rdims->number>1) casts|= 2;  /* must broadcast left operand */
      tmpDims= NewDimension(rdims->number, rdims->origin, tmpDims);
      rdims= rdims->next;
    }
    casts|= EvenConform(ldims, rdims);
  }

  /* tmpDims is reversed, switch it back (already did tmpDims!=0) */
  prev= 0;
  for (;;) {
    next= tmpDims->next;
    tmpDims->next= prev;
    if (!next) break;
    prev= tmpDims;
    tmpDims= next;
  }

  return casts;
}

static int EvenConform(Dimension *ldims, Dimension *rdims)
{
  long lnum, rnum;
  int casts= 0;
  while (ldims) {
    lnum= ldims->number;
    ldims= ldims->next;
    rnum= rdims->number;
    rdims= rdims->next;
    if (lnum!=rnum) {
      if (lnum==1) {
	casts|= 2;  /* must broadcast left operand */
	lnum= rnum;
      } else if (rnum==1) {
	casts|= 1;  /* must broadcast right operand */
      } else {
	casts|= 4;  /* not conformable */
	lnum= -lnum-rnum;  /* error marker */
      }
    }
    tmpDims= NewDimension(lnum, 1L, tmpDims);
  }
  return casts;
}

static void BdCast(Operand *op, Dimension *ddims)
{
  StructDef *base= op->type.base;
  Array *dst= PushDataBlock(NewArray(base, ddims));
  Broadcast(dst->value.c, ddims, op->value, op->type.dims, base);
  PopTo(op->owner);
  op->references= 0;
  op->type.dims= ddims;
  op->type.number= dst->type.number;
  op->value= dst->value.c;
}

int BinaryConform(Operand *l, Operand *r)
{
  int casts= Conform(l->type.dims, r->type.dims);
  if (casts&4) return 4;
  if (casts&2) BdCast(l, tmpDims);
  else if ((casts&16) && !l->references) casts|= 2;
  if (casts&1) BdCast(r, tmpDims);
  else if ((casts&8) && !r->references) casts|= 1;
  return casts & 7;
}

int RightConform(Dimension *ldims, Operand *r)
{
  int casts= Conform(ldims, r->type.dims);
  if (casts&6) return 4;
  else if (casts&1) BdCast(r, tmpDims);
  return 0;
}

/*--------------------------------------------------------------------------*/

extern VMaction DropTop;
int CalledAsSubroutine(void)
{
  return pc->Action==&DropTop;
}

long YGetInteger(Symbol *s)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (op.ops->typeID<=T_LONG && !op.type.dims) {
    op.ops->ToLong(&op);
  } else {
    YError("expecting scalar integer argument");
  }
  return *(long *)op.value;
}

double YGetReal(Symbol *s)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (op.ops->typeID<=T_DOUBLE && !op.type.dims) {
    op.ops->ToDouble(&op);
  } else {
    YError("expecting scalar real argument");
  }
  return *(double *)op.value;
}

char *YGetString(Symbol *s)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (op.ops->typeID!=T_STRING || op.type.dims)
    YError("expecting scalar string argument");
  return *(char **)op.value;
}

int YNotNil(Symbol *s)
{
  if (!s) return 0;  /* for use with YGetKeywords */
  if (s->ops==&referenceSym) ReplaceRef(s);
  return !(s->ops==&dataBlockSym && s->value.db==&nilDB);
}

Symbol *YGetKeywords(Symbol *stack, int nArgs, char **keyNames,
		     Symbol **symbols)
{
  int i;
  char *globName;
  Symbol *s0= 0;
  for (i=0 ; keyNames[i] ; i++) symbols[i]= 0;
  for ( ; nArgs>0 ; stack++, nArgs--) {
    if (stack->ops) {
      if (!s0) s0= stack;
      continue;
    }
    globName= globalTable.names[stack->index];
    for (i=0 ; keyNames[i] ; i++)
      if (strcmp(globName, keyNames[i])==0) break;
    if (!keyNames[i])
      YError("unrecognized keyword in builtin function call");
    symbols[i]= ++stack;
    nArgs--;
  }
  return s0? s0 : stack+1;
}

IOStream *YGetFile(Symbol *stack)
{
  IOStream *file;
  Operand op;
  op.ops= 0;
  if (stack->ops) stack->ops->FormOperand(stack, &op);
  if (op.ops!=&streamOps)
    YError("expecting binary file as function argument");
  file= op.value;
  return file;
}

/*--------------------------------------------------------------------------*/

/* Retrieve array arguments for foreign code wrappers,
   applying type conversion (modifies s) if necessary.
   -- Just cast YGetInteger, YGetReal for scalar arguments, and
      use YGetString for scalar strings.  */

char *YGet_C(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  op.ops->ToChar(&op);
  if (dims) *dims= op.type.dims;
  return op.value;
}

short *YGet_S(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  op.ops->ToShort(&op);
  if (dims) *dims= op.type.dims;
  return op.value;
}

int *YGet_I(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  if (s->ops==&referenceSym && globTab[s->index].ops==&intScalar) {
    if (dims) *dims= 0;
    return &globTab[s->index].value.i;
  }
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  op.ops->ToInt(&op);
  if (dims) *dims= op.type.dims;
  return op.value;
}

long *YGet_L(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  if (s->ops==&referenceSym && globTab[s->index].ops==&longScalar) {
    if (dims) *dims= 0;
    return &globTab[s->index].value.l;
  }
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  op.ops->ToLong(&op);
  if (dims) *dims= op.type.dims;
  return op.value;
}

float *YGet_F(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  op.ops->ToFloat(&op);
  if (dims) *dims= op.type.dims;
  return op.value;
}

double *YGet_D(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  if (s->ops==&referenceSym && globTab[s->index].ops==&doubleScalar) {
    if (dims) *dims= 0;
    return &globTab[s->index].value.d;
  }
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  op.ops->ToDouble(&op);
  if (dims) *dims= op.type.dims;
  return op.value;
}

double *YGet_Z(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  op.ops->ToComplex(&op);
  if (dims) *dims= op.type.dims;
  return op.value;
}

char **YGet_Q(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  if (op.ops->typeID!=T_STRING) YError("expecting string argument");
  if (dims) *dims= op.type.dims;
  return op.value;
}

void **YGet_P(Symbol *s, int nilOK, Dimension **dims)
{
  Operand op;
  if (!s->ops) YError("unexpected keyword argument");
  s->ops->FormOperand(s, &op);
  if (nilOK && op.ops==&voidOps) { if (dims) *dims= 0;  return 0;}
  if (op.ops->typeID!=T_POINTER) YError("expecting pointer argument");
  if (dims) *dims= op.type.dims;
  return op.value;
}

int YGet_dims(const Dimension *dims, long *dlist, int maxDims)
{
  int i, n= CountDims(dims);
  for (i=1 ; i<=n ; i++) {
    if (n-i < maxDims) dlist[n-i]= dims->number;
    dims= dims->next;
  }
  return n;
}

long YGet_Ref(Symbol *s)
{
  if (s->ops!=&referenceSym)
    YError("expecting simple variable reference as argument");
  return s->index;
}

void YPut_Result(Symbol *s, long index)
{
  Symbol *sout= &globTab[index];
  if (sout->ops==&dataBlockSym) {
    sout->ops= &intScalar;
    Unref(sout->value.db);
  }
  if (s->ops==&dataBlockSym) sout->value.db= Ref(s->value.db);
  else sout->value= s->value;
  sout->ops= s->ops;
}

/*--------------------------------------------------------------------------*/

extern long y_net_malloc, y_net_blocks;   /* in defmem.c */

static long ExtraNilRefs(void)
{
  long i, expectNil= 0;
  Symbol *s;
  for (i=0 ; i<globalTable.nItems ; i++)
    if (globTab[i].value.db==&nilDB && globTab[i].ops==&dataBlockSym)
      expectNil++;
  for (s=spBottom ; s<=sp ; s++)
    if (s->value.db==&nilDB && s->ops==&dataBlockSym) expectNil++;
  return nilDB.references - expectNil;
}

void Y_yorick_stats(int nArgs)
{
  Array *result;
  Dimension *dims= tmpDims;
  tmpDims= 0;
  FreeDimension(dims);
  tmpDims= NewDimension(14L, 1L, (Dimension *)0);
  result= PushDataBlock(NewArray(&longStruct, tmpDims));
  result->value.l[0]= globalTable.nItems;
  result->value.l[1]= yStackSize;
  result->value.l[2]= y_net_malloc;
  result->value.l[3]= y_net_blocks;
  result->value.l[4]= ExtraNilRefs();
  result->value.l[5]= charStruct.references;
  result->value.l[6]= shortStruct.references;
  result->value.l[7]= intStruct.references;
  result->value.l[8]= longStruct.references;
  result->value.l[9]= floatStruct.references;
  result->value.l[10]= doubleStruct.references;
  result->value.l[11]= complexStruct.references;
  result->value.l[12]= stringStruct.references;
  result->value.l[13]= pointerStruct.references;
}

void Y_symbol_def(int nArgs)
{
  long index;
  Symbol *spp;
  if (nArgs!=1) YError("symbol_def takes exactly one argument");
  if (!HashFind(&globalTable, YGetString(sp), 0L))
    YError("symbol_def name not in global symbol table");
  index= hashIndex;
  Drop(2);
  spp= sp+1;
  spp->ops= &referenceSym;
  spp->index= index;
  sp= spp;
  ReplaceRef(sp);
}

void Y_symbol_set(int nArgs)
{
  Symbol *glob, *spp= sp-1;
  if (nArgs!=2 ||
      !spp->ops) YError("symbol_set takes exactly two arguments");
  glob= &globTab[Globalize(YGetString(spp), 0L)];
  ReplaceRef(sp);
  /* following copied from Define function in ops3.c */
  if (sp->ops==&dataBlockSym) {
    Array *array= (Array *)sp->value.db;
    if (array->references && array->ops->isArray) {
      /* copy non-temporary arrays to avoid unexpected aliasing */
      Array *result= NewArray(array->type.base, array->type.dims);
      glob->value.db= (DataBlock *)result;
      array->type.base->Copy(array->type.base, result->value.c,
			     array->value.c, array->type.number);
    } else {
      if (array->ops==&lvalueOps) FetchLValue(array, sp);
      glob->value.db= Ref(sp->value.db);
    }
  } else {
    glob->value= sp->value;
  }
  glob->ops= sp->ops;
}

/*--------------------------------------------------------------------------*/