3 * (C) Copyright Apr 15 1995, Edmond J. Breen.
5 * This code may be copied for personal, non-profit use only.
9 /* Modified by Intel OpenCV team. Added lines 528-531 in order to prevent
10 the parser exceptions throwing in specific cases. No guarantee that this
11 is the fix for all cases. */
32 static int LSP, RESET = 0;
35 static int ABSDECL = 0; /* constant for abstract declorator */
36 static int INPARMLIST = 0;
37 static token_t * INSTUN = 0;
38 static type_expr *PRAMHANDLE;
39 token_t *EiC_RETURNEXPR;
41 /* TODO: external functions that need to be declared in a header somewhere */
42 void EiC_SaveGlobalString(ptr_t *s);
43 void EiC_reset_env_pointers(token_t *, int bp);
45 /** PROTOTYPES from cdecl.c **/
46 static int isredec(token_t * e1);
47 static void establish_id(token_t * e1);
48 static void new_var(token_t * e1);
49 static void addreturn(token_t *,code_t * c);
50 static void f_ext_decl(token_t * e1);
51 static void ff_ext_decl(token_t * e1);
52 static void fff_ext_decl(token_t * e1);
53 static void semi_colon(token_t * e1);
54 static void decl_spec(token_t * e1);
55 static void specifier(token_t *e1, int t,int *sclass);
56 static void init_decl_list(token_t * e1);
57 static void init_decl(token_t * e1, int t);
58 static void initialiser(token_t * e1);
59 static void decl(token_t * e1, int t);
60 static type_expr * pointer(void);
61 static void dir_decl(token_t * e1, int t);
62 static void f_dir_decl(token_t * e1);
63 static void ff_dir_decl(token_t * e1);
64 static void parm_type_list(func_t * f);
65 static void f_parm_type_list(func_t * f);
66 static void parm_decl(token_t * e1);
67 static void enum_spec(token_t * e1);
68 static void f_enum_spec(token_t * e1);
69 static void enum_list(token_t * e1);
70 static void array_decl(token_t * e1);
71 static void st_un_spec(token_t * e1, int t);
72 static void f_st_un_spec(token_t * e1);
73 static void s_decl_list(token_t * e1);
74 static void st_decl(token_t * e1, int t);
75 static void spec_qual_list(token_t * e1, int t);
76 static void r_spec_qual_list(token_t * e1);
77 static void addst_un_tag(symentry_t *sym);
78 static void addst_un_mem(token_t * e1, token_t * e2);
79 static void spec_declor_list(token_t * e1, token_t * e2);
80 static void st_declor(token_t * e1, int t);
81 static void f_st_declor();
82 static void abs_decl(token_t * e1, int t);
83 static void f_abs_decl(token_t * e1);
84 static void dir_abs_decl(token_t * e1, int t);
85 static void f1_dir_abs(token_t * e1);
86 static void EiC_f2_dir_abs(token_t * e1);
89 /* RoundUp returns x rounded to the next multiple
90 * of n, which must be a power of two.
92 #define RoundUp(x,n) (((x)+((n)-1))&(~((n)-1)))
93 #define init_ident(e,t) (e)->Val = token->Val; (e)->Tab = (t)
95 static int isredec(token_t * e1)
97 /* test for re declaration. */
98 return EiC_sametypes(e1->Type, e1->Val.sym->type);
103 #define setBoundaryLimits(x)
106 static void setBoundaryLimits(token_t *e1)
108 int t = EiC_gettype(e1->Type);
109 if(e1->Sclass == c_typedef) /* watch out for typedefs */
111 if((!EiC_INFUNC || EiC_GI(e1) == 0) && t > t_pointer &&
112 !(t == t_func || t == t_funcdec || t == t_builtin)) {
114 int s = EiC_get_sizeof(e1->Type);
116 if(!isconst(e1->Type))
117 p = &EiC_ENV->AR[e1->Sym->val.ival].v.p;
122 p->ep = (char*)p->p + s;
125 /* automatic aggregate
126 * types are done on the fly.
132 static void handle_address_operator(token_t *e1)
138 EiC_exchtype(t_pointer,e1->Type);
140 EiC_exchtype(t_ref,e1->Type);
147 static void cast_t_enum(token_t *e1)
149 /* enumeration types into ints */
150 e1->Type = EiC_addtype(t_int, EiC_freetype(e1->Type));
151 EiC_setAsBaseType(e1->Type);
155 static int checklevel(token_t *e1,symentry_t * sym, int level)
158 * Checks to see if there is a true difference between
159 * the current scope and the declaration scope.
162 if(sym->level != level) {
163 int t = EiC_gettype(e1->Type);
164 if((t == t_func || t == t_funcdec) && !INPARMLIST) {
170 if(level == 1 && sym->id != CurrentFileName()) {
171 if (e1->Sclass & c_static)
178 static void setscope(symentry_t * sym,int level, int t)
180 void EiC_UpdateSymPos(symentry_t * sym);
181 if((t == t_func || t == t_funcdec) && !INPARMLIST) {
183 } else if(sym->sclass == c_extern)
187 if(sym->level < level ||
188 (sym->next && sym->next->level > sym->level))
189 EiC_UpdateSymPos(sym);
192 static void freeArray(token_t *e1)
194 if(EiC_get_sizeof(e1->Val.sym->type))
195 xfree(EiC_ENV->AR[e1->Val.sym->val.ival].v.p.p);
196 EiC_ENV->AR[e1->Val.sym->val.ival].v.p.p = NULL;
199 static void check_decl(token_t * e1)
202 type_expr *ty = e1->Type;
203 for(;ty;ty=nextType(ty))
204 if((t=EiC_gettype(ty)) == t_funcdec || t == t_func) {
205 t = EiC_gettype(nextType(ty));
206 if(t == t_array || t == t_funcdec)
207 EiC_error("Illegal return type for %s",
212 static void doBuiltin(token_t * e1)
214 void EiC_UpdateEntry(symentry_t * sym);
215 if(token->Tok == ';') { /* must be a prototype */
216 EiC_UpdateEntry(e1->Val.sym);
217 if(nextType(e1->Val.sym->type)) {
218 EiC_warningerror("2nd prototype for builtin -> %s",
221 EiC_freetype(e1->Val.sym->type);
222 EiC_exchtype(t_builtin,e1->Type);
223 e1->Val.sym->type = e1->Type;
224 e1->Sym = e1->Val.sym;
226 EiC_error("Illegal redefinition of builtin function %s",
230 static void showRedec(token_t *e1)
232 char * EiC_getClashedfname(char nspace,char *id);
237 fn = EiC_getClashedfname(EiC_work_tab,e1->Val.sym->id);
239 fn = e1->Val.sym->fname;
241 EiC_error(" Redeclaration of parameter `%s'\n"
242 "Previously declared in: %s",e1->Val.sym->id,fn);
245 /* generate a dummy entry */
246 e1->Val.sym = EiC_insertLUT(e1->Tab,
247 e1->Val.sym->id, ID);
250 static void establish_id(token_t * e1)
252 /* e1->Val.sym->type is the previous or
254 e1->Type is the new type
259 void EiC_UpdateEntry(symentry_t * sym);
261 if ((t=EiC_gettype(e1->Val.sym->type)) == ID) {
262 /* variable not declared previously,
263 * but check for possible clashes with
264 * previously declared static variables
267 if(Pclash && !(e1->Sclass & c_static) && !(EiC_INFUNC || INPARMLIST || INSTUN))
270 } else if(e1->Sclass != c_extern &&
271 ( checklevel(e1,e1->Val.sym,EiC_S_LEVEL)
272 || e1->Tab != e1->Val.sym->nspace))
273 e1->Val.sym = EiC_insertLUT(e1->Tab, e1->Val.sym->id, ID);
274 else if (isredec(e1)) {
275 if(EiC_INFUNC && EiC_S_LEVEL == 2 && e1->Val.sym->val.ival < 0)
277 /* catch declaration after definition */
279 /* Swap Parmameter Lists */
281 if(token->Tok == '{') { /* is definition */
282 if(e1->Val.sym->fname == CurrentFileName()) {
283 func_t *f2 = (func_t *)EiC_getInf(e1->Type);
284 if(EiC_hasPrototype(f2))
285 EiC_swapFPLists(EiC_getInf(e1->Val.sym->type),f2);
286 EiC_UpdateEntry(e1->Val.sym);
291 EiC_freetype(e1->Type);
292 e1->Type = e1->Val.sym->type ;
293 e1->Sym = e1->Val.sym;
296 } else if(t == t_builtin) {
299 } else if(CurrentFileName() != e1->Val.sym->fname &&
300 (e1->Sclass & c_static)) {
301 e1->Val.sym = EiC_insertLUT(e1->Tab, e1->Val.sym->id, ID);
302 } else if(CurrentFileName() != e1->Val.sym->fname &&
303 e1->Sclass != c_extern &&
304 e1->Val.sym->sclass != c_extern &&
308 EiC_UpdateEntry(e1->Val.sym);
309 if((t=EiC_gettype(e1->Type)) == t_array) {
310 if(e1->Sclass != c_extern)
312 } else if(t == t_struct || (t == t_union)) {
313 /* use original copy */
314 EiC_freetype(e1->Type);
315 e1->Type = e1->Val.sym->type ;
316 } if(t == t_funcdec) {
318 f1 = EiC_getInf(e1->Type);
319 f2 = EiC_getInf(e1->Val.sym->type);
320 setFcallBack(f1,getFcallBack(f2));
321 setFcallBack(f2,NULL);
325 e1->Sym = e1->Val.sym;
328 } else if(t == t_builtin) {
331 } else if(t == t_ref) {
332 if((e1->Sclass & c_extern) &&
333 EiC_sametypes(e1->Type,nextType(e1->Val.sym->type))) {
334 e1->Sym = e1->Val.sym;
335 EiC_freetype(e1->Type);
336 e1->Type = e1->Sym->type;
341 if(e1->Val.sym->level == EiC_S_LEVEL &&
342 !(e1->Sclass & c_static &&
343 e1->Val.sym->fname != CurrentFileName()))
346 /* generate space in lookup table */
347 e1->Val.sym = EiC_insertLUT(e1->Tab, e1->Val.sym->id, ID);
349 e1->Val.sym->sclass = e1->Sclass;
350 setscope(e1->Val.sym,EiC_S_LEVEL,EiC_gettype(e1->Type));
351 e1->Val.sym->nspace = e1->Tab;
352 e1->Sym = e1->Val.sym;
353 if (e1->Sclass == c_static) {
355 if(EiC_S_LEVEL == 1) {
356 /* mark as private */
357 e1->Sym->sclass |= c_private;
361 level = e1->Val.sym->level; /*EiC_S_LEVEL;*/
363 * N.B. if changes are made to the condition
364 * for stacking, make sure that the free_sym
365 * function remains consistent.
367 if (!isconst(e1->Type) &&
368 e1->Tab == stand_tab && e1->Sclass != c_typedef &&
369 e1->Val.sym->val.ival == -1)
370 EiC_stackit(e1->Val.sym, level);
376 static size_t TempSz = 0;
378 void EiC_clearTempories(void)
380 void EiC_updateLocals(void);
381 extern unsigned CurTemp,EiC_ASPOT;
384 EiC_ENV->lsp = EiC_ENV->lsp > CurTemp ? EiC_ENV->lsp - CurTemp: 0;
385 EiC_ASPOT = EiC_ASPOT > TempSz ? EiC_ASPOT - TempSz : 0;
386 TempSz = CurTemp = 0;
390 static void newSlot(token_t * E, size_t sz, val_t *v, int align)
392 extern unsigned EiC_ASPOT;
393 /* Non static locals */
397 TempSz += (sz + RoundUp(EiC_ASPOT,align) - EiC_ASPOT);
400 EiC_ASPOT = RoundUp(EiC_ASPOT,align);
404 /* the lda instruction relies on stoptr
405 * being the next instruction, so don't change
406 * unless made compatible with 'lda' usage
410 EiC_generate(&E->Code,lda,&v2,EiC_ASPOT);
411 EiC_generate(&E->Code, stoptr, v, 1);
417 static void new_var(token_t * e1)
419 int t = EiC_gettype(e1->Type);
421 e1->Type = EiC_revtype(e1->Type);
425 if(EiC_gettype(e1->Type) == t_enum && e1->Tab != tag_tab)
428 EiC_newsymtype(e1->Sym, e1->Type);
429 if (!(e1->Sym->sclass == c_typedef) && !INPARMLIST)
430 if (e1->Tab == stand_tab &&
431 ((t = EiC_gettype(e1->Type)) == t_array ||
432 t == t_struct || t == t_union)) {
435 v.ival = EiC_get_sizeof(e1->Sym->type);
437 * Here we must consider 3 types
438 * of aggregate data: (1) local,
439 * (2) local but static and
441 * Global and local static data
442 * get placed on the global stack.
443 * Local data goes on the local stack.
445 if (EiC_INFUNC && (e1->Sym->sclass & c_static)) {
447 EiC_add_func_static(EiC_getInf(EiC_RETURNEXPR->Type),
452 if (/*IsTemp(E->type) ||*/ (E->Sym->level > 1 && E != EiC_RETURNEXPR)) {
454 newSlot(E,v.ival,&e1->Sym->val,EiC_get_align(e1->Type));
458 * Globals and static local arrays/structs
459 * are made on the fly. However, if not
460 * NULL, assume memory has already been allocated.
463 sz = v.ival > 1? v.ival:1;
465 if(isconst(e1->Type)) {
466 if(e1->Sym->val.ival == -1) /* not very safe ! */
467 e1->Sym->val.p.p = (void*)
470 if(!EiC_ENV->AR[e1->Sym->val.ival].v.p.p)
471 EiC_ENV->AR[e1->Sym->val.ival].v.p.p
472 = (void*)xcalloc(1,sz);
479 static void addreturn(token_t * e1, code_t * c)
482 int i, n, lcode, rtn;
483 int err = 1; /* expect an error */
484 int EiC_analyseCode(code_t *c);
491 rtn = EiC_analyseCode(c);
493 if(lcode == eicreturn && rtn <= n)
495 /*printf("rtn = %d possible %s\n",rtn,e1->Sym->id);*/
497 if (lcode == fmem) { /* free memory */
498 /* The last instruction is fmem. Thus, force all
499 * return calls within the function
502 if(rtn <= n - 1 && opcode(c,n-1) == eicreturn)
503 err=0; /* no possible error */
504 for(i = 0; i < n; ++i)
505 if(c->inst[i].opcode == eicreturn) {
506 c->inst[i].opcode = jmpu;
507 c->inst[i].val.ival = n - i;
512 EiC_generate(c, eicreturn, &v, 0);
513 if(EiC_gettype(nextType(e1->Type)) != t_void && err)
514 EiC_warningerror("Flow reaches end "
515 "of non-void function `%s'",
520 int EiC_ext_decl(token_t * e1)
524 switch (EiC_lexan()) {
533 if(e1->Sym && e1->Sym->type)
534 e1->Sym->type->sym = e1->Sym;
535 /* Hawk end change */
536 EiC_clearTempories();
546 static void f_ext_decl(token_t * e1)
548 switch (EiC_lexan()) {
550 decl(e1, token->Tok);
556 EiC_error("Declaration error");
557 EiC_match(';', " ; ");
561 token_t *EiC_genTemporay(type_expr *type, int level)
563 token_t *e1 = xcalloc(sizeof(token_t),1);
564 extern symentry_t * EiC_nxtTemp(int obj, int level);
567 sym = EiC_nxtTemp(ID,level);
570 e1->Type = EiC_copytype(type);
572 e1->Tab = sym->nspace;
580 static void do_Gotos(code_t *c)
587 while(lab && strcmp(lab->name,go->name) != 0)
590 ivalcode(c,go->loc) = lab->loc - go->loc;
592 EiC_error("Missing label `%s' defined at line %d",go->name,instline(c,go->loc));
598 static void ff_ext_decl(token_t * e1)
601 extern void EiC_UpdateEntry(symentry_t * sym);
604 switch (EiC_lexan()) {
605 case '{': /* handle funtion definition */
607 if (EiC_S_LEVEL > 1) {
608 EiC_error("Illegal function definition in %s",EiC_RETURNEXPR->Sym->id);
613 if((t = EiC_gotMissingNames(EiC_getInf(e1->Type))))
614 EiC_error("Missing name for Paramater %d",t);
616 if(EiC_ErrorRecover) /* force recovery */
617 EiC_ErrorRecover = 0;
619 if((t = EiC_gettype(e1->Type)) == t_funcdec || t == t_func) {
625 if(EiC_HasHiddenParm(e1->Type))
626 EiC_addoffsettolevel(stand_tab, EiC_S_LEVEL+1,2);
628 EiC_addoffsettolevel(stand_tab, EiC_S_LEVEL+1,1);
632 EiC_warningerror("Function Re-definition of %s",e1->Sym->id);
633 EiC_freeFuncComments(EiC_getInf(e1->Type));
636 /* Update declaration to definition. */
637 EiC_exchtype(t_func, e1->Type);
644 EiC_reset_env_pointers(e1, LSP);
647 /* remove reducear, if present */
648 if(e2.Code.nextinst != 0) {
649 if (e2.Code.inst[e2.Code.nextinst - 1].opcode ==
652 addreturn(e1,&e2.Code);
654 EiC_warningerror("Empty function definition");
655 EiC_generate(&e2.Code, eicreturn, &e2.Val, 0);
657 if(!EiC_ParseError) {
659 EiC_killcode(e1->Sym);
660 code = (code_t *) xcalloc(1, sizeof(code_t));
663 EiC_ENV->AR[e1->Sym->val.ival].v.p.p = code;
664 codeName(code) = CurrentFileName();
666 void EiC_cleancode(code_t *);
667 EiC_cleancode(&e2.Code);
668 EiC_freecode(&e2.Code);
671 EiC_freetype(e2.Type);
672 e1->Type = NULL; /* hide new type */
682 if(token->Tok == '@') { /* watch for reference declaration */
683 e1->Type = EiC_revtype(e1->Type);
684 e1->Type = EiC_addtype(t_ref,e1->Type);
685 EiC_setAsBaseType(e1->Type);
690 EiC_remlevel(EiC_S_LEVEL + 1);
691 if(token->Tok == ASS) {
694 } else if(token->Tok == '@') {
695 handle_address_operator(e1);
702 static void fff_ext_decl(token_t * e1)
705 EiC_reset_env_pointers(e1, LSP);
708 setBoundaryLimits(e1);
710 if(EiC_gettype(e1->Type) == t_array &&
711 !EiC_get_sizeof(nextType(e1->Type)))
712 EiC_error("Ilegal size specified for %s", e1->Sym->id);
715 if (token->Tok == ',') {
718 e1->Type = NULL; /* hide new type */
719 EiC_match(';', " ;");
730 static void semi_colon(token_t * e1)
732 e1->Type = NULL; /* hide new type */
733 if(!EiC_match(';', ";"))
737 static void decl_spec(token_t * e1)
740 void specifier(token_t *e1, int,int *sclass);
742 switch (token->Tok) {
746 specifier(e1, token->Tok,&sclass);
747 if ((sclass == c_auto || sclass == c_register)
748 && !(EiC_INFUNC || EiC_S_LEVEL > 1))
749 EiC_error("Illegal storage class usage");
750 else if(INPARMLIST && sclass && sclass != c_register)
751 EiC_error("Illegal storage class for parameter %s",EiC_LEXEM);
754 EiC_setAsBaseType(e1->Type);
761 static void specifier(token_t *e1, int t,int *sclass)
763 /* This function was modelled from lcc32's
767 int cls, cons, sign, size, type, vol,ty;
768 cls = ty = vol = cons = sign = size = type=0;
776 case autosym: tt = c_auto; p = &cls; break;
777 case externsym: tt = c_extern; p = &cls; break;
778 case registersym: tt = c_register; p = &cls; break;
779 case staticsym: tt = c_static; p = &cls; break;
780 case typedefsym: tt = c_typedef; p = &cls; break;
781 case constsym: p = &cons; break;
782 case volatilesym:p = &vol; break;
784 case unsignedsym:p = &sign; break;
786 if(size == longsym) {
788 tt = longsym+longsym;
791 case shortsym: p = &size; break;
792 case voidsym: ty = t_void; p = &type; break;
793 case charsym: ty = t_char; p = &type; break;
794 case intsym: ty = t_int; p = &type; break;
795 case floatsym: ty = t_float; p = &type; break;
796 case doublesym: ty = t_double; p = &type; break;
811 e1->Type = EiC_copytype(e2.Type);
814 addst_un_tag(e2.Val.sym);
816 int sclass = e1->Sclass;
825 if(type == 0 && p != &sign && p != &size) {
826 e1->Type = EiC_copytype(token->Val.sym->type);
828 } else { /* allow for masking and redeclarations */
830 if(checklevel(e1,token->Val.sym,EiC_S_LEVEL) ||
835 EiC_error("Illegal use of typedef %s",token->Val.sym->id);
852 EiC_error("invalid specification");
862 if ((size == shortsym && type != intsym)
863 || (size == longsym && type != intsym && type != doublesym)
864 || (sign && type != intsym && type != charsym))
865 EiC_error("invalid type specification");
866 if (type == charsym && sign)
867 ty = sign == unsignedsym ? t_uchar : t_char;
868 else if (size == shortsym)
869 ty = sign == unsignedsym ? t_ushort : t_short;
870 else if (size == longsym && type == doublesym)
872 else if (size == longsym+longsym) {
877 ty = sign == unsignedsym ? t_ulong : t_long;
880 } else if (size == longsym)
881 ty = sign == unsignedsym ? t_ulong : t_long;
882 else if (sign == unsignedsym && type == intsym)
887 e1->Type = EiC_addtype(ty,e1->Type);
889 if (cons == constsym) {
890 if(INPARMLIST || EiC_INFUNC) /* */
895 if(EiC_INFUNC && !INPARMLIST && sclass)
901 /* ignore volatile for now;
903 ty = qual(VOLATILE, ty);
907 static void init_decl_list(token_t * e1)
913 switch (EiC_lexan()) {
915 e2.Type = EiC_copyBaseType(e1->Type);
916 e2.Sclass = e1->Sclass;
918 init_decl(&e2, token->Tok);
920 EiC_reset_env_pointers(&e2, LSP);
923 EiC_concode(&e1->Code, &e2.Code);
924 EiC_remlevel(EiC_S_LEVEL+1);
925 setBoundaryLimits(&e2);
929 EiC_error("Init decl list error");
932 if(EiC_gettype(e2.Type) == t_array &&
933 !EiC_get_sizeof(nextType(e2.Type)))
934 EiC_error("Ilegal size specified for %s", e2.Sym->id);
936 } while (EiC_lexan() == ',');
940 static void init_decl(token_t * e1, int ty)
948 if(t == '@') { /* watch for reference declaration */
949 e1->Type = EiC_addtype(t_ref,e1->Type);
950 EiC_setAsBaseType(e1->Type);
962 handle_address_operator(e1);
969 if (EiC_lexan() == ASS) {
977 static void initglobal(type_expr * type, void **addr,int lev);
978 static void initstruct(type_expr * type, void **addr,int lev);
979 static void do_struct(type_expr *type, void **addr,int lev);
980 static int initarray(type_expr * type, void ** addr, int size, int lev);
981 static void assign_var(type_expr *type, void *addr,int allow);
982 static int do_array(type_expr *type, void ** addr, int size, int lev, int inc);
984 static void structUnionCode(token_t * e1, token_t *e2)
988 e1->Val = e1->Sym->val;
992 EiC_generate(&e1->Code, bump, &v, 0);
993 v.ival = EiC_get_sizeof(e2->Type);
994 EiC_generate(&e2->Code,refmem,&v,0);
998 static void initialiser(token_t * e1)
1001 int peek = EiC_lexan();
1004 if(EiC_GI(e1) == 0) { /* global or static local variable */
1006 if ((t =EiC_gettype(e1->Type)) <= t_pointer) { /* get scalars */
1008 if(e1->Sclass & c_static) /* catch statics */
1010 if(!isconst(e1->Type))
1011 assign_var(e1->Type,&EiC_ENV->AR[e1->Sym->val.ival].v,v);
1013 assign_var(e1->Type,&e1->Sym->val,v);
1014 switch(EiC_gettype(e1->Type)) {
1015 case t_char: e1->Sym->val.ival = e1->Sym->val.cval;break;
1016 case t_uchar: e1->Sym->val.ival = e1->Sym->val.ucval;break;
1017 case t_short: e1->Sym->val.ival = e1->Sym->val.sval;break;
1018 case t_ushort: e1->Sym->val.ival = e1->Sym->val.usval;break;
1019 case t_float: e1->Sym->val.dval = e1->Sym->val.fval;break;
1023 if(!isconst(e1->Type))
1024 initglobal(e1->Type,&EiC_ENV->AR[e1->Sym->val.ival].v.p.p,0);
1026 initglobal(e1->Type,&e1->Sym->val.p.p,0);
1030 if ((t =EiC_gettype(e1->Type)) <= t_pointer /* local scalar types */
1031 || ((t == t_struct || t==t_union) && peek != '{')
1033 if(isconst(e1->Type))
1034 assign_var(e1->Type,&e1->Sym->val,0);
1039 EiC_assign_expr(&e2);
1040 if (isconst(e2.Type)) {
1041 EiC_castconst(&e2,e1,0);
1043 e1->Val = e1->Sym->val;
1044 EiC_generate(&e1->Code,pushval,&e2.Val,0);
1045 EiC_do_stooutput(e1);
1050 EiC_castvar(&e2,e1,0);
1051 if (t == t_struct || t == t_union)
1052 structUnionCode(e1,&e2);
1053 EiC_concode(&e1->Code,&e2.Code);
1055 e1->Val = e1->Sym->val;
1056 EiC_do_stooutput(e1);
1061 } else if(EiC_INFUNC) {
1062 /* code for initialising automatic aggregate types */
1063 extern unsigned EiC_ASPOT;
1064 int s2, s1 = EiC_get_sizeof(e1->Type);
1069 v.p.p = xcalloc(1,s1);
1072 initglobal(e1->Type,&v.p.p,0);
1073 if((s2 = EiC_get_sizeof(e1->Type)) > s1)
1074 EiC_ASPOT += s2 - s1;
1075 EiC_add_func_initialiser(EiC_getInf(EiC_RETURNEXPR->Type),v.p.p);
1076 EiC_generate(&e1->Code,minit,&v,s2);
1078 EiC_error("Initialisation not "
1079 "supported for non scalar local types");
1083 static void initglobal(type_expr * type, void **addr, int lev)
1085 switch(EiC_gettype(type)) {
1087 initarray(type,addr,0,lev); break;
1089 initstruct(type,addr,lev); break;
1094 S = EiC_getInf(type);
1096 S->n = 1; /* force initialisation of first member only */
1097 initstruct(type,addr,lev+1);
1102 assign_var(type, *addr,0); break;
1106 static void initstruct(type_expr * type, void **addr, int lev)
1108 if(EiC_lexan() == '{' ) {
1109 do_struct(type,addr,lev);
1110 if(lev > 1 && EiC_lexan() != ',')
1112 EiC_match('}', " }");
1116 do_struct(type,addr,lev);
1117 else /* else intializer must be an expression of the same type */
1118 assign_var(type,*addr,1);
1122 static void do_struct(type_expr *type, void **addr,int lev)
1126 S = EiC_getInf(type);
1128 if(EiC_lexan() != '}') {
1130 *addr = (char*)*addr + S->offset[i];
1131 initglobal(S->type[i],addr,lev+1);
1132 *addr = (char*)*addr - S->offset[i];
1133 if(EiC_lexan() != ',' && i != S->n-1)
1143 static int dostrlit(void ** addr,int ln, int sln, char * str)
1145 * parse eg. char a[] = "hello world";
1146 * or char a[5] = "hello world";
1148 xmark(str,eicgstring); /* mark for garbage collector */
1151 EiC_warningerror("Array of chars is too long");
1153 ln = sln+1; /* allow for null at end */
1154 *addr = xrealloc(*addr,ln);
1157 memcpy(*addr,str,sln+1);
1159 memcpy(*addr,str,ln);
1164 static int initarray(type_expr * type, void ** addr, int size, int lev)
1166 static int INCREMENT;
1168 if(EiC_gettype(type) == t_array) {
1170 if((tok =EiC_lexan()) == '{') {
1171 t = EiC_get_sizeof(type);
1173 INCREMENT = t == 0 ? EiC_get_sizeof(nextType(type)): 0;
1174 s = do_array(nextType(type),addr,t,lev,INCREMENT);
1176 int sz = EiC_get_sizeof(nextType(type));
1180 EiC_error("Ilegal array domain in initialisation");
1181 setNumElems(type,t);
1183 EiC_match('}'," }");
1184 } else if(tok == STR) { /* handle string literals */
1185 if(EiC_gettype(nextType(type)) == t_char) {
1187 t = EiC_get_sizeof(type);
1188 sln = (char*)token->Val.p.ep - (char*)token->Val.p.p - 1;
1189 s = dostrlit(addr,t,sln,token->Val.p.p);
1190 if(lev == 0 && t == 0)
1191 setNumElems(type,s);
1193 EiC_error("Illegal initialisation");
1196 if(lev > 0) /* lev indicates a recusive call */
1197 size = do_array(nextType(type),addr,size,lev,INCREMENT);
1199 EiC_error("missing { in initialisation of an array");
1202 initglobal(type,addr,lev+1);
1206 static int do_array(type_expr *type, void ** addr, int size, int lev, int inc)
1210 while(EiC_lexan() != '}') {
1215 *addr = xrealloc(*addr, size);
1216 memset((char*)*addr + size - inc, 0, inc);
1219 EiC_error("Too many initialisers");
1221 EiC_error("Illegal domain for initialisation");
1224 *addr = (char*)*addr + n;
1225 size = initarray(type,addr, size,lev+1);
1226 *addr = (char*)*addr - n;
1227 n += EiC_get_sizeof(type);
1228 if (EiC_lexan() != ',')
1236 static void assign_var(type_expr *type, void *addr,int allow)
1240 void *EiC_getaddress(token_t *);
1244 EiC_assign_expr(&e2);
1248 EiC_clearTempories();
1251 t = EiC_gettype(type);
1253 if (isconst(e2.Type)) {
1255 EiC_castconst(&e2,&e1,0);
1258 case t_uchar: *(char*)addr = e2.Val.ival; break;
1260 case t_ushort: *(short*)addr = e2.Val.ival; break;
1262 case t_uint: *(int *)addr = e2.Val.ival; break;
1264 case t_ulong: *(long*)addr = e2.Val.lval; break;
1265 case t_llong: *(eic_llong*)addr = e2.Val.llval; break;
1266 case t_float: *(float*)addr = e2.Val.dval; break;
1267 case t_double: *(double*)addr = e2.Val.dval; break;
1269 if(EiC_S_LEVEL == 1 && EiC_gettype(e2.Type) == t_pointer
1270 && EiC_gettype(nextType(e2.Type)) == t_char
1274 EiC_SaveGlobalString(&e2.Val.p);
1276 *(ptr_t*)addr = e2.Val.p;
1278 *(void**)addr = e2.Val.p.p;
1282 EiC_error("Unknown initialiserXXX");
1290 EiC_castvar(&e2,&e3,0);
1291 if (t == t_struct || t == t_union)
1292 structUnionCode(E1,&e2);
1294 E1->Val = E1->Sym->val;
1295 EiC_concode(&E1->Code,&e2.Code);
1296 EiC_do_stooutput(E1);
1299 if(EiC_GI(&e2) != 0) /* global or static addresses only are allowed */
1300 EiC_error("Illegal initialization: illegal address operation");
1303 /*EiC_output(&e2);*/
1304 /*EiC_castvar(&e2,&e1,1);*/
1305 if(EiC_gettype(type) == t_pointer ||
1306 (e2.Sym && EiC_gettype(e2.Sym->type) == t_ref)) {
1308 p.sp = p.p = EiC_getaddress(&e2);
1309 p.ep = (char*)p.p + EiC_get_sizeof(e2.Type);
1310 if(!EiC_sametypes(e2.Type,type))
1311 EiC_warningerror("Suspicious pointer conversion");
1315 *(void**)addr = p.p;
1316 EiC_freecode(&e2.Code);
1318 EiC_error("Expected constant expression as an"
1326 static void decl(token_t * e1, int t)
1328 type_expr *P = NULL;
1332 dir_decl(e1, EiC_lexan());
1334 e1->Type = EiC_catTypes(P,e1->Type);
1341 EiC_error("Declarator error");
1347 static type_expr * pointer(void)
1349 type_expr *t = NULL;
1351 t = EiC_addtype(t_pointer,t);
1352 if(EiC_lexan() == constsym || token->Tok == volatilesym) {
1353 if(token->Tok == constsym) {
1356 /*ignore volatilesym for now*/
1359 /* pointer qualifer */
1360 if(token->Tok == safesym) {
1365 } else if(token->Tok == unsafesym) {
1370 } while (token->Tok == '*');
1375 static void dir_decl(token_t * e1, int t)
1379 decl(e1, EiC_lexan());
1380 EiC_match(')', " )");
1386 init_ident(e1, EiC_work_tab);
1391 EiC_error("Direct declarator error");
1396 static void f_dir_decl(token_t * e1)
1399 switch (EiC_lexan()) {
1406 EiC_remlevel(EiC_S_LEVEL+1);
1414 static void ff_dir_decl(token_t * e1)
1418 switch ((h=EiC_lexan())) {
1429 * Use INPARMLIST to inform other
1430 * modules that the following declarations
1431 * are function parameters.
1434 PRAMHANDLE = e1->Type;
1436 parm_type_list(EiC_getInf(e1->Type));
1438 * now pseudo reverse the parameter
1441 EiC_reset_env_pointers(e1, LSP);
1451 * This should really be made illegal, it allows
1452 * for function declarations with empty
1453 * paramater list, such as:
1454 * int f(); Therefore, force an implied t_var argument.
1459 type = EiC_addtype(t_var,NULL);
1460 EiC_add_func_parm(EiC_getInf(e1->Type), &type, NULL);
1466 EiC_error("Unknown type '%s': possible "
1467 "old C type declaration", token->Val.sym->id);
1469 EiC_error("Syntax error");
1471 EiC_match(')', " )");
1475 static void UpDateParmSym(type_expr *ty, symentry_t *sym)
1477 if(EiC_gettype(ty) == t_pointer && EiC_gettype(nextType(ty)) ==
1479 if(EiC_gettype(sym->type) != t_pointer)
1480 sym->type = EiC_addtype(t_pointer,sym->type);
1483 static void parm_type_list(func_t * f)
1491 * Must watch out for void as a parameter.
1492 * The void paramater will have no sym entry.
1498 void EiC_adjustParam(type_expr **type);
1499 EiC_adjustParam(&e2.Sym->type);
1504 } else /* still must reverse type if needed */
1505 e2.Type = EiC_revtype(e2.Type);
1507 EiC_add_func_parm(f, &e2.Type,name);
1509 EiC_freetype(e2.Type);
1511 UpDateParmSym(getFPty(f,getFNp(f)-1),e2.Val.sym);
1516 if (EiC_lexan() == ',')
1517 f_parm_type_list(f);
1522 static void f_parm_type_list(func_t * f)
1524 if (EiC_lexan() == '.') {
1525 if (EiC_lexan() == '.')
1526 if (EiC_lexan() == '.') {
1528 type = EiC_addtype(t_var, NULL);
1529 EiC_add_func_parm(f, &type,NULL);
1534 EiC_error("Expected ...");
1541 static void fff_parm_decl(token_t *e1);
1542 static void ff_parm_decl(token_t *e1);
1543 static void f_parm_decl(token_t * e1);
1545 static void parm_decl(token_t * e1)
1547 switch (EiC_lexan()) {
1555 if(token->Tok == ID)
1556 EiC_error("Unknown type '%s'",token->Val.sym->id);
1558 EiC_error("Parameter declaration error");
1563 static void f_parm_decl(token_t * e1)
1565 if(EiC_lexan() == '*') {
1566 type_expr *P = NULL;
1570 e1->Type = EiC_catTypes(P,e1->Type);
1578 static void ff_parm_decl(token_t *e1)
1580 switch(EiC_lexan()) {
1581 case ID: init_ident(e1, EiC_work_tab);
1584 case '(': fff_parm_decl(e1);
1586 case '[': retractlexan();
1590 if(token->Tok != ',' && token->Tok != ')')
1591 EiC_error("Parameter declaration error");
1598 static void fff_parm_decl(token_t *e1)
1600 switch(EiC_lexan()) {
1606 EiC_remlevel(EiC_S_LEVEL+1);
1611 EiC_match(')', " ) ");
1618 static void enum_spec(token_t * e1)
1620 e1->Type = EiC_addtype(t_enum, NULL);
1624 static void f_enum_spec(token_t * e1)
1628 EiC_work_tab = tag_tab;
1629 switch (EiC_lexan()) {
1630 case ID: /* enumeration tag */
1631 t = EiC_gettype(token->Val.sym->type);
1635 if (nxtt == '{' || t == ID) {
1637 init_ident(e1, EiC_work_tab);
1643 if (EiC_lexan() == '{') {
1645 EiC_match('}', " }");
1646 e1->Type = e1->Val.sym->type;
1650 } else if (t == t_enum) {
1651 EiC_freetype(e1->Type);
1652 e1->Type = EiC_copytype(token->Val.sym->type);
1654 EiC_error("Enumeration declaration error");
1660 EiC_match('}', " }");
1664 EiC_error("Enumeration declaration error");
1670 static void enum_list(token_t * e1)
1678 * now do enumerator part
1680 if (EiC_lexan() == ID) {
1681 e2.Type = EiC_addtype(t_int, NULL);
1683 EiC_setAsBaseType(e2.Type);
1685 init_ident(&e2, EiC_work_tab);
1687 if (EiC_lexan() == ASS) {
1690 if (isconst(e3.Type) && EiC_gettype(e3.Type) == t_int)
1691 ENUMVAL = e3.Val.ival;
1694 EiC_error("Assignment must be constant");
1700 e2.Sym->val.ival = ENUMVAL++;
1702 EiC_error("Expected identifier in enumeration");
1705 } while (EiC_lexan() == ',');
1709 static void array_decl(token_t * e1)
1714 EiC_assign_expr(&e2);
1715 if (isconst(e2.Type)) {
1716 if ((t=EiC_gettype(e2.Type)) != t_int && t != t_uint) {
1717 if(sizeof(int) != sizeof(long) || t != t_ulong) {
1718 EiC_error("Constant int expression needed");
1723 if(!ABSDECL && /*((t == t_void && EiC_INFUNC) || */
1724 EiC_gettype(e2.Type) != t_void )
1725 EiC_error("Constant int expression needed");
1728 e1->Type = EiC_addtype(t_array, e1->Type);
1729 setNumElems(e1->Type,e2.Val.ival);
1731 EiC_match(']', " ] ");
1735 static void st_un_spec(token_t * e1, int t)
1739 e1->Type = EiC_addtype(t_struct, e1->Type);
1742 e1->Type = EiC_addtype(t_union, e1->Type);
1748 static eicstack_t stun;
1749 static void f_st_un_spec(token_t * e1)
1754 int h = EiC_work_tab;
1755 EiC_work_tab = tag_tab;
1759 EiC_eicpush(&stun,val);
1760 switch (EiC_lexan()) {
1761 case ID: /* struct or union tag */
1763 sym = token->Val.sym;
1764 t = EiC_gettype(sym->type);
1769 if(t==ID || (nxtt == '{' && token->Val.sym->level<EiC_S_LEVEL)) {
1770 init_ident(e1, tag_tab);
1772 setInf(e1->Type,xcalloc(1, sizeof(struct_t)));
1778 EiC_match('}', " }");
1780 if(EiC_lexan() == ';' && EiC_S_LEVEL == 1)
1787 if (t == t_struct || t == t_union) {
1788 EiC_freetype(e1->Type);
1789 e1->Type = EiC_copytype(sym->type);
1791 init_ident(e1, tag_tab);
1793 setInf(e1->Type,xcalloc(1, sizeof(struct_t)));
1799 EiC_match('}', " }");
1801 if(EiC_lexan() == ';' && EiC_S_LEVEL == 1) {
1802 EiC_freetype(e1->Type);
1814 EiC_match('}', " }");
1819 EiC_eicpop(&stun,&val);
1825 static void s_decl_list(token_t * e1)
1827 int f = 0, bp = EiC_ENV->lsp;
1831 if(!EiC_getInf(e1->Type))
1832 setInf(e1->Type,xcalloc(1, sizeof(struct_t)));
1834 if(((struct_t*)EiC_getInf(e1->Type))->cl) {
1835 EiC_error("Illegal use of struct/union");
1841 switch (EiC_lexan()) {
1845 st_decl(e1, token->Tok);
1849 EiC_error("Struct/Union list declaration error");
1850 S = EiC_getInf(e1->Type);
1853 * determine structure alignment.
1856 S->tsize = RoundUp(S->tsize,S->align);
1858 EiC_reset_env_pointers(e1, bp);
1864 static void st_decl(token_t * e1, int t)
1868 spec_qual_list(&e2, t);
1869 EiC_setAsBaseType(e2.Type);
1870 spec_declor_list(e1, &e2);
1871 EiC_freetype(e2.Type);
1873 EiC_match(';', " ; ");
1876 static void spec_qual_list(token_t * e1, int t)
1878 /*type_spec(e1, t);*/
1879 specifier(e1,t,NULL);
1880 r_spec_qual_list(e1);
1883 static void r_spec_qual_list(token_t * e1)
1885 switch (EiC_lexan()) {
1887 spec_qual_list(e1, token->Tok);
1894 void EiC_free_un_mem(type_expr * e)
1896 struct_t *S = EiC_getInf(e);
1900 for (i = 0; i < S->n; i++) {
1902 EiC_freetype(S->type[i]);
1909 for(i=0;i<S->ntags;++i)
1910 EiC_freetype(S->tag[i]);
1917 static void addst_un_tag(symentry_t *sym)
1920 S = EiC_getInf(INSTUN->Val.sym->type);
1923 S->tag = (type_expr**)xmalloc(sizeof(type_expr*));
1925 S->tag = (type_expr**)xrealloc(S->tag,
1926 sizeof(type_expr*)*S->ntags);
1927 S->tag[S->ntags - 1] = EiC_transtype(sym->type);
1928 EiC_setaliases(sym->type,1);
1931 static void addst_un_mem(token_t * e1, token_t * e2)
1936 S = EiC_getInf(e1->Type);
1938 S->id = (char **) xcalloc(1, sizeof(char *));
1939 S->offset = (int *) xcalloc(1, sizeof(int));
1940 S->type = (type_expr **) xcalloc(1, sizeof(type_expr *));
1943 * first check for duplication of names
1947 if(strcmp(S->id[i], e2->Val.sym->id) == 0)
1948 EiC_error("Duplication of identifier in struct/union");
1950 S->id = (char **) xrealloc(S->id,
1951 (S->n + 1) * sizeof(char *));
1952 S->offset = (int *) xrealloc(S->offset,
1953 (S->n + 1) * sizeof(int));
1954 S->type = (type_expr **) xrealloc(S->type,
1955 (S->n + 1) * sizeof(type_expr *));
1958 if(isconst(e2->Type)) {
1959 unsetConst(e2->Type);
1960 setConstp(e2->Type);
1963 if(EiC_gettype(e2->Type) == t_enum)
1966 S->id[S->n] = (char *) EiC_strsave(e2->Val.sym->id);
1967 S->type[S->n] = EiC_transtype(e2->Type);
1970 if(!(s = EiC_get_sizeof(S->type[S->n]))) {
1971 EiC_error("Incomplete data type for struct/union member %s",S->id[S->n]);
1973 off = EiC_get_align(e2->Type);
1975 if (EiC_gettype(e1->Type) == t_struct) {
1977 S->offset[S->n] = RoundUp(S->tsize,off);
1978 S->tsize += s + S->offset[S->n] - S->tsize;
1979 } else { /* handle union */
1980 S->offset[S->n] = 0;
1981 S->tsize = S->tsize > s ? S->tsize : s;
1984 * structure alignment is equal to the
1985 * maximum alignment of its members.
1992 static void spec_declor_list(token_t * e1, token_t * e2)
1996 cl = ((struct_t*)EiC_getInf(e1->Type))->cl;
1998 switch (EiC_lexan()) {
2004 e3.Type = EiC_transtype(e2->Type);
2005 st_declor(&e3, token->Tok);
2006 if (EiC_gettype(e3.Type) == t_struct ||
2007 EiC_gettype(e3.Type) == t_union) {
2009 S = EiC_getInf(e3.Type);
2010 if (!S->cl) /* check for closure */
2011 EiC_error("Illegal Struct/Union member");
2013 /* if structure/union is already closed,
2014 * then assume structure is being re-entered.
2016 if (e3.Val.sym && !cl) {
2017 e3.Type = EiC_revtype(e3.Type);
2019 EiC_setaliases(e2->Type, 1);
2020 addst_un_mem(e1, &e3);
2022 EiC_setaliases(e3.Type, 1);
2026 EiC_freetype(e3.Type);
2027 EiC_remsym(e3.Val.sym);
2031 EiC_error("Struct/Union member declaration error");
2033 } while (EiC_lexan() == ',');
2037 static void st_declor(token_t * e1, int t)
2044 /*setBoundaryLimits(e1);*/
2049 EiC_error("Bit fields not supported");
2054 static void f_st_declor()
2056 if (EiC_lexan() == ':')
2057 EiC_error("Bit fields not supported");
2062 int EiC_type_name(token_t * e1)
2064 switch (EiC_lexan()) {
2067 spec_qual_list(e1, token->Tok);
2068 EiC_setAsBaseType(e1->Type);
2069 switch (EiC_lexan()) {
2073 abs_decl(e1, token->Tok);
2074 e1->Type = EiC_revtype(e1->Type);
2087 static void abs_decl(token_t * e1, int t)
2089 type_expr *P = NULL;
2096 e1->Type = EiC_catTypes(P,e1->Type);
2100 dir_abs_decl(e1, t);
2103 EiC_error("extraneous identifier `%s'",token->Val.sym->id);
2109 static void f_abs_decl(token_t * e1)
2111 switch (EiC_lexan()) {
2114 dir_abs_decl(e1, token->Tok);
2117 EiC_error("extraneous identifier `%s'",token->Val.sym->id);
2124 static void dir_abs_decl(token_t * e1, int t)
2139 static void f1_dir_abs(token_t * e1)
2141 switch (EiC_lexan()) {
2148 EiC_remlevel(EiC_S_LEVEL+1);
2153 abs_decl(e1, token->Tok);
2154 EiC_match(')', " )");
2158 EiC_error("Abstract declaration error");
2162 static void EiC_f2_dir_abs(token_t * e1)
2164 switch (EiC_lexan()) {
2171 EiC_remlevel(EiC_S_LEVEL+1);