static void channm(Lextok *n) { char lbuf[512]; if (n->sym->type == CHAN) strcat(Buf, n->sym->name); else if (n->sym->type == NAME) strcat(Buf, lookup(n->sym->name)->name); else if (n->sym->type == STRUCT) { Symbol *r = n->sym; if (r->context) { r = findloc(r); if (!r) { strcat(Buf, "*?*"); return; } } ini_struct(r); printf("%s", r->name); strcpy(lbuf, ""); struct_name(n->lft, r, 1, lbuf); strcat(Buf, lbuf); } else strcat(Buf, "-"); if (n->lft->lft) { sprintf(lbuf, "[%d]", eval(n->lft->lft)); strcat(Buf, lbuf); } }
/** * declare argument types * called from "newfunc", this routine adds an entry in the local * symbol table for each named argument * completely rewritten version. p.l. woods * @param t argument type (char, int) * @return */ getarg(int t) { int j, legalname, address, argptr; char n[NAMESIZE]; FOREVER { if (argstk == 0) return; if (match("*")) j = POINTER; else j = VARIABLE; if (!(legalname = symname(n))) illname(); if (match("[")) { while (inbyte() != ']') if (endst()) break; j = POINTER; } if (legalname) { if (argptr = findloc(n)) { symbol_table[argptr].identity = j; symbol_table[argptr].type = t; address = argtop - symbol_table[argptr].offset; symbol_table[argptr].offset = address; } else error("expecting argument name"); } argstk = argstk - INTSIZE; if (endst()) return; if (!match(",")) error("expected comma"); } }
doLocalAnsiArgument(int type) { char symbol_name[NAMESIZE]; int identity, address, argptr, ptr; if (match("*")) { identity = POINTER; } else { identity = VARIABLE; } if (symname(symbol_name)) { if (findloc(symbol_name)) { multidef(symbol_name); } else { argptr = add_local (symbol_name, identity, type, 0, AUTO); argstk = argstk + INTSIZE; ptr = local_table_index; while (ptr != NUMBER_OF_GLOBALS) { // modify stack offset as we push more params ptr = ptr - 1; address = symbol_table[ptr].offset; symbol_table[ptr].offset = address + INTSIZE; } } } else { error("illegal argument name"); junk(); } if (match("[")) { while (inbyte() != ']') { if (endst()) { break; } } identity = POINTER; symbol_table[argptr].identity = identity; } }
newfunc () { char n[NAMESIZE], *ptr; fexitlab = getlabel(); if (!symname (n) ) { error ("illegal function or declaration"); kill (); return; } if (ptr = findglb (n)) { if (ptr[IDENT] != FUNCTION) multidef (n); else if (ptr[OFFSET] == FUNCTION) multidef (n); else ptr[OFFSET] = FUNCTION; } else addglb (n, FUNCTION, CINT, FUNCTION, PUBLIC); prologue (); if (!match ("(")) error ("missing open paren"); prefix (); outstr (n); col (); nl (); locptr = STARTLOC; argstk = 0; while (!match (")")) { if (symname (n)) { if (findloc (n)) multidef (n); else { addloc (n, 0, 0, argstk, AUTO); argstk = argstk + intsize(); } } else { error ("illegal argument name"); junk (); } blanks (); if (!streq (line + lptr, ")")) { if (!match (",")) error ("expected comma"); } if (endst ()) break; } stkp = 0; argtop = argstk; while (argstk) { if (amatch ("register", 8)) { if (amatch("char", 4)) getarg(CCHAR); else if (amatch ("int", 3)) getarg(CINT); else getarg(CINT); ns(); } else if (amatch ("char", 4)) { getarg (CCHAR); ns (); } else if (amatch ("int", 3)) { getarg (CINT); ns (); } else { error ("wrong number args"); break; } } statement(YES); printlabel(fexitlab); col(); nl(); modstk (0); gret (); stkp = 0; locptr = STARTLOC; }
SYMBOL * #endif getarg( int typ , /* typ = CCHAR, CINT, DOUBLE or STRUCT */ TAG_SYMBOL *otag , /* structure tag for STRUCT type objects */ int deftype, /* YES=ANSI -> addloc NO=K&R -> findloc */ SYMBOL *prevarg, /* ptr to previous argument, only of use to ANSI */ char issigned, /* YES=unsigned NO=signed */ char zfar, /* FARPTR=far NO=near */ char proto) /* YES=prototyping -> names not needed */ { char n[NAMESIZE] ; SYMBOL *argptr ; int legalname, ident, more ; int brkflag; /* Needed to correctly break out for K&R*/ /* * This is of dubious need since prototyping came about, we could * inadvertently include fp packages if the user includes <math.h> but * didn't actually use them, we'll save the incfloat business for * static doubles and definitions of local doubles * * if (typ == DOUBLE) * incfloat=1; */ argptr = NULL; /* Only need while loop if K&R defs */ while ( undeclared) { ident = get_ident() ; more =0; if ( (legalname=symname(n)) == 0 ) { if (!proto) { illname(n); } else { /* * Obligatory silly fake name */ sprintf(n,"sg6p_%d",proto); legalname=1; } } if ( ident == FUNCTIONP ) { needtoken(")()"); /* function returning pointer needs dummy symbol */ more = dummy_idx(typ, otag) ; typ = (zfar ? CPTR : CINT ); } else if ( ident == PTR_TO_FN ) { needtoken(")()") ; ident = POINTER ; } if ( cmatch('[') ) { /* pointer ? */ ptrerror(ident) ; /* it is a pointer, so skip all */ /* stuff between "[]" */ while ( inbyte() != ']' ) if ( endst() ) break; /* add entry as pointer */ ident = (ident == POINTER) ? PTR_TO_PTR : POINTER ; } if ( legalname ) { /* * For ANSI we need to add symbol name to local table - this CINT is * temporary */ if (deftype) { argptr=addloc(n,0,CINT,0,0); argptr->offset.p = prevarg; } /* * If prototyping, then we can't find the name, but if we're prototyping * we have been defined as ANSI, therefore argptr already holds * the correct pointer - kinda neat! */ if ( proto || (argptr=findloc(n)) ) { argptr->flags=(zfar&FARPTR)|(issigned&UNSIGNED); /* add in details of the type of the name */ if ( ident == PTR_TO_PTR ) { /* djm mods will be here for long pointer */ // argptr->flags = UNSIGNED |FARPTR ; /*unsigned*/ argptr->ident = POINTER ; argptr->type = typ; // argptr->type = ( (zfar&FARPTR) ? CPTR : CINT ); argptr->more = dummy_idx(typ, otag) ; } else { argptr->more = more; argptr->ident = ident ; argptr->type = typ ; } } else warning(W_EXPARG); if ( otag ) { argptr->tag_idx = otag - tagtab ; argptr->ident = POINTER ; argptr->type = STRUCT ; } } brkflag=0; if (!deftype) { --undeclared; /* cnt down */ if ( endst() ) { brkflag=1; break; } if ( cmatch(',') == 0 ) warning(W_EXPCOM) ; } if (brkflag || deftype) break; } if (deftype) return(argptr); ns(); return(0); }
/** * begin a function * called from "parse", this routine tries to make a function out * of what follows * modified version. p.l. woods */ newfunc() { char n[NAMESIZE]; int idx, type; fexitlab = getlabel(); if (!symname(n)) { error("illegal function or declaration"); kill(); return; } if (idx = findglb(n)) { if (symbol_table[idx].identity != FUNCTION) multidef(n); else if (symbol_table[idx].offset == FUNCTION) multidef(n); else symbol_table[idx].offset = FUNCTION; } else add_global(n, FUNCTION, CINT, FUNCTION, PUBLIC); if (!match("(")) error("missing open paren"); output_string(n); output_label_terminator(); newline(); local_table_index = NUMBER_OF_GLOBALS; //locptr = STARTLOC; argstk = 0; // ANSI style argument declaration if (doAnsiArguments() == 0) { // K&R style argument declaration while (!match(")")) { if (symname(n)) { if (findloc(n)) multidef(n); else { add_local(n, 0, 0, argstk, AUTO); argstk = argstk + INTSIZE; } } else { error("illegal argument name"); junk(); } blanks(); if (!streq(line + lptr, ")")) { if (!match(",")) error("expected comma"); } if (endst()) break; } stkp = 0; argtop = argstk; while (argstk) { if (type = get_type()) { getarg(type); need_semicolon(); } else { error("wrong number args"); break; } } } statement(YES); print_label(fexitlab); output_label_terminator(); newline(); gen_modify_stack(0); gen_ret(); stkp = 0; local_table_index = NUMBER_OF_GLOBALS; //locptr = STARTLOC; }
primary (lvalue_t *lval) { char sname[NAMESIZE]; int num[1], k, symbol_table_idx, offset, reg; symbol_t *symbol; lval->ptr_type = 0; /* clear pointer/array type */ if (match ("(")) { k = hier1 (lval); needbrack (")"); return (k); } if (amatch("sizeof", 6)) { needbrack("("); gen_immediate_c(); if (amatch("int", 3)) output_number(INTSIZE); else if (amatch("char", 4)) output_number(1); else if (symname(sname)) { if ((symbol_table_idx = findloc(sname)) || (symbol_table_idx = findglb(sname))) { symbol = &symbol_table[symbol_table_idx]; if (symbol->storage == LSTATIC) error("sizeof local static"); offset = symbol->count; if ((symbol->type & CINT) || (symbol->identity == POINTER)) offset *= INTSIZE; output_number(offset); } else { error("sizeof undeclared variable"); output_number(0); } } else { error("sizeof only on simple type or variable"); } needbrack(")"); newline(); lval->symbol = 0; lval->indirect = 0; return(0); } if (symname (sname)) { if (symbol_table_idx = findloc (sname)) { symbol = &symbol_table[symbol_table_idx]; reg = gen_get_location (symbol); lval->symbol = symbol; lval->indirect = symbol->type; if (symbol->identity == ARRAY) { //lval->ptr_type = symbol->type; lval->ptr_type = 0; return 0; } if (symbol->identity == POINTER) { lval->indirect = UINT; lval->ptr_type = symbol->type; } return reg; } if (symbol_table_idx = findglb (sname)) { symbol = &symbol_table[symbol_table_idx]; lval->symbol = symbol; switch (symbol->identity) { case ARRAY: gen_immediate_a (); output_string (symbol->name); newline (); /* Fall through */ case FUNCTION: lval->indirect = lval->ptr_type = symbol_table[symbol_table_idx].type; lval->ptr_type = 0; return 0; default: lval->indirect = 0; if (symbol->identity == POINTER) { lval->ptr_type = symbol->type; } return 1; } } blanks (); if (ch() != '(') error("undeclared variable"); symbol_table_idx = add_global (sname, FUNCTION, CINT, 0, PUBLIC, 1); symbol = &symbol_table[symbol_table_idx]; lval->symbol = symbol; lval->indirect = 0; return 0; } if (constant (num)) { lval->symbol = 0; lval->indirect = 0; return 0; } error ("invalid expression"); gen_immediate_c (); output_number(0); newline (); junk (); return 0; }