3 * (C) Copyright Apr 15 1995, Edmond J. Breen.
5 * This code may be copied for personal, non-profit use only.
8 /* Modified by Intel OpenCV team. Added lines 528-531 in order to prevent
9 the parser exceptions throwing in specific cases. No guarantee that this
10 is the fix for all cases. */
30 int EiC_iskeyword(keyword_t *keywords,char*id,int n)
34 if(strcmp(keywords[i].id,id) == 0)
35 return keywords[i].token;
40 void EiC_eicpush(eicstack_t *s, val_t v)
44 s->val = (val_t*)xcalloc(sizeof(val_t),MoD);
46 s->val = (val_t*)xrealloc(s->val,(s->n+MoD)*sizeof(val_t));
52 int EiC_eicpop(eicstack_t *s, val_t *pop)
62 s->val = (val_t*)xrealloc(s->val,s->n*sizeof(val_t));
68 /* LOOK UP TABLE ROUTINES
69 --------------------------*/
71 static size_t _EnTrY_No = 0;
72 int hashsmc(char * s,int mod);
73 symentry_t *EiC_HTAB[HSIZE];
76 /* code for generating tempories */
77 unsigned int NumTemps=0, CurTemp =0;
79 symentry_t * EiC_nxtTemp(int obj, int level)
83 sprintf(tmpName,"%s%d","__TeMp",NumTemps);
85 /* There should be no need to watch out for change of level !!!!
86 * It will be assumed that the compound statement routine will
91 sym = EiC_insertLUT(EiC_work_tab,tmpName,obj);
96 if(sym->val.ival == -1) /* needs a home */
97 EiC_stackit(sym,level);
99 /* Setting up the token information is left
100 * to the caller of this routine.
109 size_t EiC_lut_NextEntryNum(void)
114 int hashsmc(char * s,int mod)
116 register unsigned int h, c;
118 while( (c = (int) *s++) > 0)
127 char *EiC_getClashedfname(char nspace,char *id)
130 for(sym = EiC_HTAB[hashsmc(id,HSIZE)]; sym != NULL; sym = sym->next)
131 if(sym->nspace == nspace && strcmp(id,sym->id) == 0)
132 if((sym->sclass & c_private) &&
133 sym->fname != CurrentFileName()) {
139 symentry_t * EiC_lookup(char nspace, char *id)
143 for(sym = EiC_HTAB[hashsmc(id,HSIZE)]; sym != NULL; sym = sym->next)
144 if(sym->nspace == nspace && strcmp(id,sym->id) == 0) {
145 if((sym->sclass & c_private) &&
146 sym->fname != CurrentFileName()) {
158 /* function added by Sergey Oblomov (Intel OpenCV team member) */
159 symentry_t * EiC_lookup_by_callback(char nspace, void* callback)
164 for( i = 0; i < HSIZE; i++ )
166 for(sym = EiC_HTAB[i]; sym != NULL; sym = sym->next)
167 if(sym->nspace == nspace)
168 if( sym->type->obj == t_func && sym->type->u.inf &&
169 ((func_t*)sym->type->u.inf)->callBack == callback )
176 symentry_t * EiC_insertLUT(char nspace,char *id,int type)
181 sym = (symentry_t *) xcalloc(1,sizeof(symentry_t));
184 if( (sym->id = EiC_strsave(id)) == NULL) {
188 sym->entry = _EnTrY_No++;
189 hashval = hashsmc(sym->id,HSIZE);
190 sym->next = EiC_HTAB[hashval];
191 EiC_HTAB[hashval] = sym;
192 sym->nspace = nspace;
193 sym->val.ival = -1; /* indicates unused */
194 sym->type = EiC_addtype(type,NULL);
195 sym->fname = CurrentFileName();
200 void delete(symentry_t *sym)
202 auto symentry_t * this;
205 idx = hashsmc(sym->id,HSIZE);
206 this = EiC_HTAB[idx];
208 EiC_HTAB[idx] = sym->next;
209 else { /* find and unlink */
210 while(this && this->next != sym)
212 this->next = sym->next;
216 void EiC_UpdateSymPos(symentry_t * sym)
219 if(sym->next && sym->next->level > sym->level) {
220 symentry_t * p = EiC_HTAB[hashsmc(sym->id,HSIZE)];
222 while(p->next != sym)
224 if(p->next->level > sym->level)
237 void EiC_remsym(symentry_t *sym)
247 void remBuiltin(symentry_t *sym)
249 /* removes the prototype of the builtin function only */
250 EiC_freetype(sym->type);
251 sym->type = EiC_addtype(t_builtin,NULL);
252 sym->fname = "::EiC::";
258 void EiC_remlevel(int level)
261 symentry_t * sym, *symh;
263 for(i=0;i<HSIZE;i++) {
265 while(sym && sym->level >= level) {
274 /*CUT lutRemTempories*/
275 void EiC_remTempories(void)
278 symentry_t * sym, *symh;
280 for(i=0;i<HSIZE;i++) {
282 while(sym && IsTemp(sym->type)) {
294 int EiC_lutClearFileEntries(char *FileName)
298 symentry_t * sym, *p, *t;
300 for(i=0;i<HSIZE;i++) {
301 t = sym = EiC_HTAB[i];
304 if(strcmp(sym->fname,FileName) == 0) {
326 void EiC_lut_CleanUp(size_t bot)
330 symentry_t * sym, *p, *t;
331 for(i=0;i<HSIZE;i++) {
332 t = sym = EiC_HTAB[i];
335 if(sym->entry >= bot) {
336 if(EiC_gettype(sym->type) == t_builtin) {
361 #define freeAg(X) do {\
363 if(!isconst(x->type)) \
364 xfree(EiC_ENV->AR[x->val.ival].v.p.p);\
365 else xfree(x->val.p.p);\
369 #define freeAg1(X) xfree(EiC_ENV->AR[X->val.ival].v.p.p)
372 static void free_sym(symentry_t *sym)
376 printf("Freeing [%s] %d %d [%d]\n",
377 sym->id, sym->entry, sym->val.ival,EiC_gettype(sym->type));
381 if(EiC_gettype(sym->type) == t_func) {
383 } else if(sym->level == 1) { /* global value */
385 if((t=EiC_gettype(sym->type)) == t_array && sym->val.ival >=0)
387 else if ((t== t_union || t == t_struct) && sym->val.ival >=0 )
394 * the conditions for pushing onto the ARgar stack
395 * must be the same as those for stacking as found in
396 * function establish_id
397 * Except for ParseError
399 if( sym->val.ival >=0
401 && EiC_gettype(sym->type) != t_builtin ) {
402 if(! isconst(sym->type)
403 && sym->nspace == stand_tab
404 && sym->sclass != c_typedef) {
405 EiC_eicpush(&EiC_ENV->ARgar,sym->val);
408 EiC_freetype(sym->type);
414 void EiC_UpdateEntry(symentry_t *sym)
416 int t = EiC_gettype(sym->type);
417 if(CurrentFileName() != sym->fname &&
422 sym->entry = _EnTrY_No++;
423 sym->pname = sym->fname;
424 sym->fname = CurrentFileName();
427 void EiC_addoffsettolevel(char nspace,int level,int off)
432 for(i=0;i<HSIZE;i++) {
434 while(sym && sym->level == level && sym->nspace == nspace) {
435 sym->val.ival = -(sym->val.ival + off);
441 void EiC_marktype(type_expr *type, char mark)
445 void EiC_markFunc(type_expr *t,int mark);
449 switch(EiC_gettype(type)) {
451 if(EiC_getInf(type) == NULL)
455 EiC_markFunc(type,mark);
461 s = EiC_getInf(type);
463 * REM must allow for incomplete
471 xmark(s->offset,mark);
474 for(i=0;i<s->n;i++) {
475 xmark(s->id[i],mark);
476 EiC_marktype(s->type[i],mark);
480 for(i=0;i<s->ntags;++i)
481 EiC_marktype(s->tag[i],mark);
485 type = nextType(type);
489 static void marklabel(Label_t *l,char mark)
498 static void markcode(symentry_t *sym,char mark)
503 code = EiC_ENV->AR[sym->val.ival].v.p.p;
505 xmark(code->inst,mark);
506 marklabel(code->labels,mark);
507 marklabel(code->gotos,mark);
510 for(i=0;i<code->nextinst;i++,inst++)
511 if(inst->opcode == jmptab) {
516 } else if(inst->opcode == assigntype)
517 EiC_marktype(inst->val.p.p,mark);
521 void EiC_marksyms(char mark)
523 void EiC_markmacros(char);
524 void EiC_markENV(char);
529 EiC_markmacros(mark);
532 xmark(EiC_ENV->AR,mark);
534 xmark(EiC_ENV->ARgar.val,mark);
536 xmark(EiC_ENV->LAR,mark);
537 if(EiC_ENV->CODE.nextinst)
538 xmark(EiC_ENV->CODE.inst,mark);
543 for(i=0;i<HSIZE;i++) {
546 /*printf("marking %s\n",sym->id);*/
547 if(strcmp(sym->id,"p") == 0)
551 EiC_marktype(sym->type,mark);
552 if(sym->nspace != tag_tab)
553 switch(EiC_gettype(sym->type)) {
554 case t_func: markcode(sym,mark); break;
558 if(isconst(sym->type))
559 xmark(sym->val.p.p,mark);
561 if(sym->sclass != c_typedef && sym->val.ival >= 0)
562 xmark(EiC_ENV->AR[sym->val.ival].v.p.p,mark);
571 char * EiC_strsave(char *s)
576 for(n = 0,p =s; *p != '\0';++p,++n)
579 p = xcalloc(n,sizeof(char));
581 while((*p++ = *s++));
585 void EiC_newsymtype(symentry_t *sym, type_expr *t)
588 if(sym->type && sym->type != t)
589 EiC_freetype(sym->type);
594 int nextstackitem(int level)
596 if(level == 1) { /* static variables */
598 if(!EiC_eicpop(&EiC_ENV->ARgar,&v)) { /* check for spare slots */
599 if(EiC_ENV->sp == EiC_ENV->ARsize) {
601 EiC_ENV->AR = (AR_t*)xcalloc(sizeof(AR_t),1);
603 EiC_ENV->AR = (AR_t*)xrealloc(EiC_ENV->AR,
604 (EiC_ENV->sp+1)*sizeof(AR_t));
607 v.ival = EiC_ENV->sp;
611 } else { /* automatic variables */
612 if(EiC_ENV->lsp == EiC_ENV->LARsize) {
613 if(!EiC_ENV->LARsize)
614 EiC_ENV->LAR = (AR_t*)xcalloc(sizeof(AR_t),1);
616 EiC_ENV->LAR = (AR_t*)xrealloc(EiC_ENV->LAR,(EiC_ENV->lsp+1)*
621 return EiC_ENV->lsp - 1;
625 int EiC_stackit(symentry_t * sym,int level)
630 i = nextstackitem(level);
631 if(level == 1) /* static variables */
633 else /* local variable */
638 ar[i].v.dval = 0; /* NULL it */
639 ar[i].type = sym->type;
644 /*------------------------------------------------*/
645 void EiC_inittoken(token_t * e1)
648 e1->Code.binst = e1->Code.nextinst = 0;
649 e1->Code.labels = e1->Code.gotos = NULL;
650 e1->Typequal = e1->Sclass = 0;
655 void EiC_freetoken(token_t * e1)
657 EiC_freetype(e1->Type);
660 void initcode(code_t * code)
662 code->binst = code->nextinst = 0;
666 void EiC_cleancode(code_t * code)
674 /* printf("Next instr: %d\n", code->nextinst); */
675 /* rem free up other info also */
676 for(i=0;i<code->nextinst;i++,inst++)
677 if(inst->opcode == jmptab) {
682 } else if(inst->opcode == assigntype)
683 EiC_freetype(inst->val.p.p);
687 static void freeCodeLabels(code_t *code)
689 void EiCp_freeLabels(Label_t *lab);
691 EiCp_freeLabels(code->labels);
695 EiCp_freeLabels(code->gotos);
700 void EiC_killcode(symentry_t *sym)
703 code = EiC_ENV->AR[sym->val.ival].v.p.p;
707 freeCodeLabels(code);
711 void EiC_freecode(code_t * code)
714 if(code && code->binst > 0) {
716 code->nextinst = code->binst = 0;
717 freeCodeLabels(code);
723 void EiC_generate(code_t * code, int opcode,val_t *val,int ext)
727 if(code->nextinst == code->binst)
728 if(!(code->binst%ModSize)) {
730 inst = (InsT_t*)xcalloc(1,sizeof(InsT_t)*ModSize);
733 xrealloc(inst,(code->binst+ModSize)*sizeof(InsT_t));
734 code->binst += ModSize;
737 inst[code->nextinst].opcode = opcode;
738 inst[code->nextinst].val = *val;
739 inst[code->nextinst].ext = ext;
740 inst[code->nextinst].line = CurrentLineNo();
745 void copycode(code_t * c1, code_t * c2)
747 /* this function needs to handle label and gotos */
751 for(i=0;i<c2->nextinst;++i,++inst) {
752 EiC_generate(c1,inst->opcode,&inst->val,inst->ext);
753 c1->inst[c1->nextinst-1].line = inst->line;
757 void copycode(code_t * c1, code_t * c2)
759 Label_t * EiCp_addLabel(Label_t *, char *, int, int);
766 /* concatenate labels*/
770 c1->labels = EiCp_addLabel(c1->labels,lab->name,lab->loc+c1->nextinst,1);
774 /* conatenate gotos */
778 c1->gotos = EiCp_addLabel(c1->gotos,lab->name,lab->loc+c1->nextinst,0);
783 Tsize = c1->nextinst + c2->nextinst;
784 /* make memory size a multiple of ModSize */
787 Tsize += ModSize - (Tsize%ModSize);
789 c1->inst = xrealloc(c1->inst,Tsize*sizeof(InsT_t));
791 c1->inst = xcalloc(Tsize,sizeof(InsT_t));
793 memcpy(&inst[c1->nextinst], c2->inst, c2->nextinst * sizeof(InsT_t));
800 void EiC_concode(code_t * c1, code_t * c2)
805 void EiC_contoken(token_t * e1, token_t * e2)
807 EiC_concode(&e1->Code,&e2->Code);
808 e1->Type = EiC_getcontype(e1->Type,e2->Type);
812 void EiC_swaptokens(token_t *e1, token_t * e2)
823 typedef struct list_t {
829 typdef struct llist_t {
834 static llist_t ** includes = NULL, ** included = NULL;
836 static int inllist(llist_t **list, char *fname)
838 /* returns -1 if not in list */
840 for(i=0;i<list->n,++i) {
841 if(fname == list->list[i]->fname == 0)
847 static list_t * add2list(list_t *list, char *fname)
849 list_t n = calloc(sizeof(list_t),1);
856 static void add2includes(char *file, char * includes)
858 int i = inlist(includes,file);
862 includes->list = realloc(includes->list,
863 (includes->n + 1) * sizeof(*includes->list));