-
Notifications
You must be signed in to change notification settings - Fork 2
/
variable.c
1133 lines (944 loc) · 24.2 KB
/
variable.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
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
/***************************************************************************
* Copyright (C) 2002, 2003, 2004, 2005, 2006 by Christoph Thelen *
* DocBacardi@the-dreams.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "globals.h"
/*-----------------------------------*/
/* Locals */
typedef struct
{
localdepth_t start;
localdepth_t length;
} PIDXLEN;
typedef struct
{
localdepth_t idx;
bool f_exists;
VARIABLE var;
} VARIDX;
typedef struct
{
varcnt_t left;
varcnt_t right;
varcnt_t lweight;
varcnt_t rweight;
stringsize_t *vname;
localdepth_t varidx_count, varidx_buflen;
VARIDX *varidx;
} VARNAME;
typedef struct
{
bool err;
varcnt_t pi;
localdepth_t lb_best;
localdepth_t lb_idx;
localdepth_t levelidx;
varcnt_t *connector;
} VARFIND;
/*-----------------------------------*/
uint8_t makeHash(const char* name, const stringsize_t namelen);
void r_dump(FILE *dfh, varcnt_t pi, localdepth_t level, bool useHtml);
/* void tdump(FILE *dfh, varcnt_t pi, varcnt_t phere); */
/* void tdump_r(FILE *dfh, varcnt_t pi, varcnt_t phere); */
VARFIND find_var(const stringsize_t *name);
VARADR assumeVariable(const stringsize_t *name);
varcnt_t treeSearch(const char* name, const stringsize_t namelen, const bool addweight, varcnt_t **connector);
VARIABLE *getVarByAdr(const VARADR adr);
/*-----------------------------------*/
varcnt_t *hashtbl;
localdepth_t blockidx;
localdepth_t *parentidx, parentidx_count, parentidx_buflen;
PIDXLEN *pliststart;
localdepth_t pliststart_count, pliststart_buflen;
VARNAME *vars;
varcnt_t vars_count, vars_buflen;
varcnt_t cnt_undefine;
varcnt_t cnt_resolves;
extern FILE *debugLog;
/*-----------------------------------*/
/* uninitialize variable list */
void variable_zero(void)
{
vars_count = vars_buflen = 0;
hashtbl = NULL;
vars = NULL;
parentidx = NULL;
pliststart = NULL;
}
/* allocate new variable list */
bool new_variable(varcnt_t nmemb)
{
uint8_t cnt = 0;
varcnt_t *hashp;
if( (hashp=hashtbl=(varcnt_t*)(malloc(256*sizeof(varcnt_t))))!=NULL &&
(vars=(VARNAME*)(malloc((vars_buflen=nmemb)*sizeof(VARNAME))))!=NULL &&
(parentidx=(localdepth_t*)(malloc((parentidx_buflen=32)*sizeof(localdepth_t))))!=NULL &&
(pliststart=(PIDXLEN*)(malloc((pliststart_buflen=16)*sizeof(PIDXLEN))))!=NULL )
{
/* Clear the Hashtable */
do
*hashp++ = no_var;
while( ++cnt!=0 );
vars_count = 0;
/* generate index to searchlist for this block */
pliststart->start = 0;
pliststart->length = 1;
pliststart_count = 1;
/* first block is the global one. it has no parent, just search itself */
parentidx[0] = 0;
parentidx_count = 1;
cnt_undefine = cnt_resolves = 0;
blockidx = 0;
return true;
}
else
{
if( hashtbl!=NULL )
free( hashtbl );
if( vars!=NULL )
free( vars );
if( parentidx!=NULL )
free( parentidx );
if( pliststart!=NULL )
free( pliststart );
systemError(EM_OutOfMemory);
return false;
}
}
/* delete variable list */
void del_variable(void)
{
VARNAME *vnc, *vne;
VARIDX *vic, *vie;
/* Free Hashtable */
if( hashtbl!=NULL )
free( hashtbl );
/* Free Tree Table */
vnc = vars;
if( vnc!=NULL )
{
vne = vnc + vars_count;
/* Free Name for every entry */
while( vnc<vne )
{
/* free all variables */
vic = vnc->varidx;
vie = vic + vnc->varidx_count;
while( vic<vie )
{
deleteVariable(&vic->var);
++vic;
}
free( vnc->varidx );
free( vnc->vname );
++vnc;
}
/* Free the complete Table */
free( vars );
}
if( parentidx!=NULL )
free( parentidx );
if( pliststart!=NULL )
free( pliststart );
}
/*
* Look for variable and create it if it does
* not exist yet
*/
VARADR assumeVariable(const stringsize_t *name)
{
VARIDX *vi_cnt, *newvibuf;
localdepth_t newvilen;
VARADR vret = { no_var, 0 };
VARNAME *newbuf;
varcnt_t newlen;
VARNAME *pp;
VARFIND vf;
const char *cname;
stringsize_t namelen;
if( (vf=find_var(name)).err ) {
return vret;
}
if( vf.pi!=no_var )
{
if( vf.lb_best==(localdepth_t)-1 )
{
/* No definition found in this block or it's parents -> insert a new one at the end of the array */
/* is enough space in the array left? */
if( (pp=vars+vf.pi)->varidx_count>=pp->varidx_buflen )
{
/* can the array be expanded or is it's maximal size already reached? */
if( pp->varidx_buflen==((localdepth_t)-1) )
{
error(EM_TooManyLocalBlocks);
return vret;
}
/* double the array's size or use the maximal length */
if( (newvilen=pp->varidx_buflen<<1)<pp->varidx_buflen )
newvilen=((localdepth_t)-1);
/* resize the array */
if( (newvibuf=(VARIDX*)(realloc(pp->varidx, newvilen*sizeof(VARIDX))))==NULL )
{
systemError(EM_OutOfMemory);
return vret;
}
pp->varidx = newvibuf;
pp->varidx_buflen = newvilen;
}
/* insert new item */
vi_cnt = pp->varidx+pp->varidx_count;
vi_cnt->idx = vf.levelidx;
vi_cnt->f_exists = false;
vi_cnt->var.defined = vi_cnt->var.used = vi_cnt->var.final = vi_cnt->var.readwrite = false;
vi_cnt->var.segidx = (seglistsize_t)-1;
vi_cnt->var.valt.typ = VALTYP_NUM;
vi_cnt->var.valt.byteSize = 0;
vi_cnt->var.valt.value.num = 0;
/* a new variable was requested which is not defined yet -> we have another undefined var */
++cnt_undefine;
vret.varidx = vf.pi;
vret.localidx = pp->varidx_count++;
return vret;
}
else
{
vret.varidx = vf.pi;
vret.localidx = vf.lb_idx;
return vret;
}
}
else
{
/* The variable was not found. Create it now. */
/* Erase the '@...' */
namelen = *name;
cname = (const char*)(name+1);
if( namelen>1 && *cname=='@' )
{
do
++cname;
while( --namelen>1 && isdigit(*cname) );
}
/* set the connector from the upper element (or the hashtable) to this idx */
/* do it before the realloc as the pointer might become invalid */
*vf.connector = vars_count;
/* is the array full? */
if( vars_count >= vars_buflen )
{
/* can the array be expanded or is it's maximal size already reached? */
if( vars_buflen==((varcnt_t)-1) )
{
error(EM_TooManyVariables);
return vret;
}
/* double the array's size or use the maximal length */
if( (newlen=vars_buflen<<1)<vars_buflen )
newlen=((varcnt_t)-1);
/* resize the array */
if( (newbuf=(VARNAME*)(realloc(vars, newlen*sizeof(VARNAME))))==NULL )
{
systemError(EM_OutOfMemory);
return vret;
}
vars = newbuf;
vars_buflen = newlen;
}
/* get a pointer to the next free element in the "vars" array */
pp=vars+vars_count;
/* The new element has no left and right children */
pp->left = pp->right = no_var;
/* no children means a weight of '0' */
pp->lweight = pp->rweight = 0;
/* allocate space for the name string */
if( (pp->vname=cstr2string(cname,namelen))==NULL )
return vret;
/* Create a new VARIDX list */
if( (pp->varidx=(VARIDX*)malloc(4*sizeof(VARIDX)))==NULL )
{
systemError(EM_OutOfMemory);
return vret;
}
pp->varidx_buflen=4;
/* Fill the first entry of the new array with the index of this local block and an empty variable */
pp->varidx[0].idx = vf.levelidx;
pp->varidx[0].f_exists = false;
pp->varidx[0].var.defined = pp->varidx[0].var.used = pp->varidx[0].var.final = pp->varidx[0].var.readwrite = false;
pp->varidx[0].var.segidx = (seglistsize_t)-1;
pp->varidx[0].var.valt.typ = VALTYP_NUM;
pp->varidx[0].var.valt.byteSize = 0;
pp->varidx[0].var.valt.value.num = 0;
pp->varidx_count = 1;
/* a new variable was requested which is not defined yet -> we have another undefined var */
++cnt_undefine;
/* return the adress of the variable and increase the array's length counter "vars_count" */
vret.varidx = vars_count++;
vret.localidx = 0;
/* increase the weight on the whole path. vf.connector is just a dummy here */
treeSearch(cname,namelen, true, &vf.connector);
return vret;
}
}
/*
* Find variable in Tree and local block
*/
VARFIND find_var(const stringsize_t *name)
{
stringsize_t namelen;
localdepth_t level;
const char *cname;
bool digread;
bool fixedlevel;
localdepth_t *pi_start, *pi_cnt, *pi_end;
VARIDX *vi_cnt, *vi_end;
VARNAME *pp;
VARFIND vret = { true, 0, 0, 0, 0, NULL };
/* start and endpointer to the list of parent indices */
pi_end=(pi_start=parentidx+pliststart[blockidx].start)+pliststart[blockidx].length;
cname=(const char*)(name+1);
if( (namelen=*name)>1 && *cname=='@' )
{
level=0; /* the level */
digread=false; /* no digits read for now */
while( --namelen>0 && isdigit(*++cname) )
{
level *= 10;
level += *cname -'0';
digread = true; /* now we have read a digit */
}
if( namelen==0 ) /* no text after '@' / number -> error! */
{
error(EM_AtWithoutLabelname);
return vret;
}
if(digread)
{
if( (pi_cnt=pi_start+level)>=pi_end ) /* level exceeds local block depth */
{
error(EM_LevelExceedsDepth);
return vret;
}
vret.levelidx = *pi_cnt; /* get index of this level */
}
else
vret.levelidx = blockidx; /* no number after the '@' means local, which is the actual blockidx */
fixedlevel = true;
}
else
{
vret.levelidx = blockidx;
fixedlevel = false;
}
if( (vret.pi=treeSearch(cname,namelen, false, &vret.connector))!=no_var )
{
/* The variable was found. Search the VARIDX list for definition in the deepest local block */
pp = vars+vret.pi;
vret.lb_best = (localdepth_t)-1;
vi_cnt = pp->varidx;
vi_end = vi_cnt + pp->varidx_count;
/* search the list */
if( fixedlevel )
{
/* Only search for exact match */
while( vi_cnt<vi_end )
{
if( vi_cnt->idx==vret.levelidx )
{
vret.lb_best= 0;
vret.lb_idx = vi_cnt-pp->varidx;
break;
}
++vi_cnt;
};
}
else
{
/* somehow I don't like this... But I can't sort the list either. Nah, maybe nobody sees it ;) */
while( vi_cnt<vi_end )
{
for( pi_cnt=pi_start; pi_cnt<pi_end; ++pi_cnt)
{
if( *pi_cnt==vi_cnt->idx && (vret.lb_best<*pi_cnt || vret.lb_best==(localdepth_t)-1) )
{
vret.lb_best = *pi_cnt;
vret.lb_idx = vi_cnt-pp->varidx;
}
}
++vi_cnt;
};
}
}
vret.err = false;
return vret;
}
/*
* Tree search
* looks for the variable in the nametree
* In Parameter:
* char* name = pointer to the name
* stringsize_t namelen = length of the name
* bool addweight = change weights in the path
* Out Parameter:
* Index of found Variable or 'no_var'
*/
varcnt_t treeSearch(const char* name, const stringsize_t namelen, const bool addweight, varcnt_t **connector)
{
varcnt_t *up;
size_t minlen;
stringsize_t pl;
VARNAME *lp, *pp=NULL, *rp;
varcnt_t pi;
int cmpres;
bool checkwheight;
checkwheight=addweight;
pi=*(up=hashtbl+makeHash(name,namelen));
while( pi!=no_var )
{
pp = vars+pi;
/* Compare */
minlen=( namelen<(pl=*pp->vname) ) ? namelen : pl;
if( (cmpres=strncmp( name, (char*)(pp->vname+1), minlen ))==0 && namelen!=pl )
cmpres = (namelen<pl)?-1:1;
if( cmpres==0 )
break;
else if( cmpres<0 )
{
if( checkwheight && (++pp->lweight>pp->rweight+8) )
{
/* swap pp and pp->left */
lp = vars+(*up=pp->left);
pp->left = lp->right;
lp->right = pi;
pp->lweight = lp->rweight;
lp->rweight += pp->rweight + 1;
pi = *up;
checkwheight=false;
}
else
{
up = &pp->left;
pi=pp->left;
checkwheight=addweight;
}
}
else
{
if( checkwheight && (++pp->rweight>pp->lweight+8) )
{
/* swap pp and pp->right */
rp = vars+(*up=pp->right);
pp->right = rp->left;
rp->left = pi;
pp->rweight = rp->lweight;
rp->lweight += pp->lweight + 1;
pi = *up;
checkwheight=false;
}
else
{
up = &pp->right;
pi=pp->right;
checkwheight=addweight;
}
}
};
*connector=up;
return pi;
}
VARADR writeVariable(const stringsize_t *name, const VARIABLE invar, const bool allowRedefine)
{
VARIABLE *var;
char *sname;
VARADR vget, vret = { no_var, 0 };
/* try to find variable, if it doesn't exist yet, create it */
if( (vget=assumeVariable(name)).varidx==no_var )
return vret;
/* get the variable's address for direct modification */
var = &vars[vget.varidx].varidx[vget.localidx].var;
/* only allow modification of an already defined variable if */
/* redefinition is requested or the variable is non-final */
if( var->defined )
{
if( !allowRedefine && var->final )
{
/* redefinition not allowed, show error */
if( (sname=string2cstr(name))==NULL )
{
error(EM_VarRedefinition_s, "");
return vret;
}
error(EM_VarRedefinition_s, sname);
free(sname);
return vret;
}
/* free old value of variable */
switch( invar.valt.typ ) {
case VALTYP_NUM:
/* nothing to do for a number... */
break;
case VALTYP_STR:
/* free the old string */
free(var->valt.value.str);
break;
}
}
/* resolved another undefined variable? */
if( !var->defined && invar.defined )
{
--cnt_undefine;
++cnt_resolves;
}
/* set the variable to the new value */
/* first do a simple copy of all elements */
*var = invar;
copyVariable(var);
return vget;
}
void resetResolveCounter(void)
{
cnt_resolves = 0;
}
bool resolvedSomething(void)
{
return cnt_resolves>0;
}
uint8_t makeHash(const char* name, const stringsize_t namelen)
{
/* no good hash function, but keeps all labels in sorted order -> no need to sort at label dump time */
return ((uint8_t)(*name));
}
/*
* Dump all labels up to a level
* Output should be reuseable.
*/
bool dump(FILE *dfh, localdepth_t level, bool useHtml)
{
uint8_t hash;
if( useHtml )
{
fprintf(dfh, "<table><tbody>\n");
}
hash = 0;
do
r_dump( dfh, *(hashtbl+hash), level, useHtml );
while( ++hash!=0 );
if( useHtml )
{
fprintf(dfh, "</tbody></table>\n");
}
return !ferror(dfh);
}
void r_dump(FILE *dfh, varcnt_t pi, localdepth_t level, bool useHtml)
{
VARNAME *pp;
VARIABLE var;
localdepth_t cnt;
if( pi!=no_var )
{
pp = vars + pi;
r_dump( dfh, pp->left, level, useHtml );
for( cnt=0; cnt<pp->varidx_count; cnt++ )
{
if( pp->varidx[cnt].idx<=level )
{
if( useHtml )
{
fprintf(dfh, "<tr><td>");
}
printString(dfh, pp->vname);
if( useHtml )
{
fprintf(dfh, "</td><td>");
}
else
{
fprintf(dfh," = ");
}
var = pp->varidx[cnt].var;
switch( var.valt.typ )
{
case VALTYP_NUM:
switch( var.valt.byteSize )
{
case 1:
fprintf(dfh, "$%02x", var.valt.value.num);
break;
case 2:
fprintf(dfh, "$%04x", var.valt.value.num);
break;
case 3:
fprintf(dfh, "$%06x", var.valt.value.num);
break;
default:
fprintf(dfh, "$%08x", var.valt.value.num);
break;
}
break;
case VALTYP_STR:
fprintf(dfh, "\"");
printString(dfh, var.valt.value.str);
fputc('\"', dfh);
break;
};
if( useHtml )
{
fprintf(dfh, "</td><td>");
}
if( !var.used )
{
if( useHtml )
{
fprintf(dfh, "unused");
}
else
{
fprintf(dfh, "\t\t;unused" );
}
}
else if( useHtml )
{
fprintf(dfh, " ");
}
if( useHtml )
{
fprintf(dfh, "</td></tr>");
}
fputc('\n', dfh);
}
}
r_dump( dfh, pp->right, level, useHtml );
}
}
/*
void tdump(FILE *dfh, varcnt_t pi, varcnt_t phere)
{
//Write a VCG file to filehandle dfh
fprintf(dfh, "\n---------------------------------------\n");
fprintf(dfh, "Variable Tree dump\n\n");
fprintf(dfh, "graph:\n{\n");
fprintf(dfh, "\tdisplay_edge_labels: yes\n");
fprintf(dfh, "\tstraight_phase: yes\n");
fprintf(dfh, "\tpriority_phase: yes\n");
fprintf(dfh, "\tport_sharing: no\n");
fprintf(dfh, "\tcrossing_weight: median\n");
fprintf(dfh, "\tsplines: yes\n");
tdump_r(dfh, pi, phere);
fprintf(dfh, "}\n");
fprintf(dfh, "\n---------------------------------------\n\n");
}
void tdump_r(FILE *dfh, varcnt_t pi, varcnt_t phere)
{
const char *edgecolor = "blue";
const char *labelcolor= "black";
VARNAME *pp;
stringsize_t nlen;
char *nc;
pp = vars + pi;
fprintf(dfh, "\tnode:\n\t{\n\t\ttitle: \"%d\"\n\t\tlabel: %c\"", pi, (pi==phere)?'*':' ');
//Dirty hack to dump name
nlen = *(pp->vname);
nc = (char*)(pp->vname+1);
while( nlen-->0 )
fprintf(dfh, "%c", *(nc++) );
fprintf(dfh, "\"\n\t}\n");
if( pp->left!=no_var )
{
fprintf(dfh, "\tedge:\n");
fprintf(dfh, "\t{\n");
fprintf(dfh, "\t\tsourcename: \"%d\"\n", pi);
fprintf(dfh, "\t\ttargetname: \"%d\"\n", pp->left);
fprintf(dfh, "\t\tlabel: \"left\"\n");
fprintf(dfh, "\t\tcolor: %s\n", edgecolor);
fprintf(dfh, "\t\ttextcolor: %s\n", labelcolor);
fprintf(dfh, "\t}\n");
}
if( pp->right!=no_var )
{
fprintf(dfh, "\tedge:\n");
fprintf(dfh, "\t{\n");
fprintf(dfh, "\t\tsourcename: \"%d\"\n", pi);
fprintf(dfh, "\t\ttargetname: \"%d\"\n", pp->right);
fprintf(dfh, "\t\tlabel: \"right\"\n");
fprintf(dfh, "\t\tcolor: %s\n", edgecolor);
fprintf(dfh, "\t\ttextcolor: %s\n", labelcolor);
fprintf(dfh, "\t}\n");
}
if( pp->left!=no_var )
tdump_r(dfh, pp->left, phere);
if( pp->right!=no_var )
tdump_r(dfh, pp->right, phere);
}
*/
localdepth_t localDive(void)
{
localdepth_t depth, newpilen, *newpibuf;
localdepth_t newpslen;
localdepth_t *pcnt, *pend;
PIDXLEN* newpsbuf;
depth = pliststart[blockidx].length;
/* need to resize the parentidx array? */
if( parentidx_count+depth+1 > parentidx_buflen )
{
/* can the array be expanded or is it's maximal size already reached? */
if( parentidx_buflen==((localdepth_t)-1) )
{
error(EM_TooManyVariables);
return (localdepth_t)-1;
}
/* double the array's size or use the maximal length */
if( (newpilen=parentidx_buflen<<1)<parentidx_buflen )
newpilen=((localdepth_t)-1);
/* resize the array */
if( (newpibuf=(localdepth_t*)(realloc(parentidx, newpilen*sizeof(localdepth_t))))==NULL )
{
systemError(EM_OutOfMemory);
return (localdepth_t)-1;
}
parentidx = newpibuf;
parentidx_buflen = newpilen;
}
/* need to resize the pliststart array? */
if( pliststart_count >= pliststart_buflen )
{
/* can the array be expanded or is it's maximal size already reached? */
if( pliststart_buflen==((localdepth_t)-1) )
{
error(EM_TooManyVariables);
return (localdepth_t)-1;
}
/* double the array's size or use the maximal length */
if( (newpslen=pliststart_buflen<<1)<pliststart_buflen )
newpilen=((localdepth_t)-1);
/* resize the array */
if( (newpsbuf=(PIDXLEN*)(realloc(pliststart, newpslen*sizeof(PIDXLEN))))==NULL )
{
systemError(EM_OutOfMemory);
return (localdepth_t)-1;
}
pliststart = newpsbuf;
pliststart_buflen = newpslen;
}
/* get start and end of the parent list of this local block */
/* this will be the base of the list for the new block */
/* the depth of the new local block is the depth of the current +1 */
pend = (pcnt=parentidx+pliststart[blockidx].start) + depth;
/* the new index of the local block is the number of used local blocks which happens to be the same as pliststart_count */
blockidx = pliststart_count;
/* generate index to searchlist for this block */
pliststart[blockidx].start = parentidx_count;
pliststart[blockidx].length = depth + 1;
++pliststart_count;
/* copy the parent's parents to the list */
while( pcnt<pend )
parentidx[parentidx_count++] = *(pcnt++);
/* and this block too */
parentidx[parentidx_count++] = blockidx;
return blockidx;
}
localdepth_t localUp(void)
{
if( blockidx!=0 )
{
return ( blockidx = *(parentidx + pliststart[blockidx].start + (pliststart[blockidx].length-2) ) );
} else {
return (localdepth_t)-1;
}
}
localdepth_t getLocalBlock(void)
{
return blockidx;
}
void setLocalBlock(localdepth_t bidx)
{
blockidx = bidx;
}
VARIABLE *getVarByAdr(const VARADR adr)
{
return &vars[adr.varidx].varidx[adr.localidx].var;
}
bool existVariable(const stringsize_t *name)
{
VARFIND vf;
return ( !(vf=find_var(name)).err && vf.pi!=no_var && vf.lb_best!=(localdepth_t)-1 );
}
bool existAndDefinedVariable(const stringsize_t *name)
{
VARFIND vf;
VARADR vadr;
VARIABLE *pVar;
vf = find_var(name);
if( !vf.err && vf.pi!=no_var && vf.lb_best!=(localdepth_t)-1 ) {
vadr.varidx = vf.pi;
vadr.localidx = vf.lb_idx;
pVar = getVarByAdr(vadr);
return pVar->defined;
} else {
return false;
}
}
void writeVarAdr(const VARADR adr, const VARIABLE invar)
{
VARIABLE *var;
(var=getVarByAdr(adr))->defined = true;
/* delete old data structure */
deleteVariable(var);
/* copy new data */
*var = invar;
copyVariable(var);
}
VARERR readVariable(const stringsize_t *name)
{
VARERR vret;
VARADR vadr;
VARFIND vf;
VARIABLE *vp;
vret.err = true;
vret.var.defined = vret.var.used = vret.var.final = vret.var.readwrite = false;
vret.var.segidx = (seglistsize_t)-1;
vret.var.valt.typ = VALTYP_NUM;
vret.var.valt.byteSize = 0;
vret.var.valt.value.num = 0;
if( !(vf=find_var(name)).err )
{
vret.err = false;
if( vf.pi!=no_var && vf.lb_best!=(localdepth_t)-1 )
{
vadr.varidx = vf.pi;
vadr.localidx = vf.lb_idx;
(vp=getVarByAdr(vadr))->used = true;
vret.var = *vp;
}
}
return vret;
}
bool getRMWPointer(const stringsize_t *name, VARADR *adr, VARIABLE **var)
{