/************************************* * C02 Variable Management Routines * *************************************/ #include #include #include #include #include #include "common.h" #include "files.h" #include "asm.h" #include "parse.h" #include "label.h" #include "vars.h" /* Lookup variable name in variable table * * Sets: varidx = index into varnam array * * varcnt if not found * * Returns: TRUE if found, otherwise FALSE */ int fndvar(char *name) { DEBUG("Looking up variable '%s'\n", name) for (varidx=0; varidx 0) { sprintf(value, "$%hhX", membrs[mbridx].size); return membrs[mbridx].size; } if (datlen[varidx]) { sprintf(value, "$%hhX", datlen[varidx]); return datlen[varidx]; } if (strlen(varsiz[varidx]) == 0) { strcpy(value,"1"); return 1; } strcpy(value, varsiz[varidx]); if (strcmp(value, "*") == 0) ERROR("Unable to Determine Size of Variable %s\n", vrname, EXIT_FAILURE); return atoi(value); } /* Parse Data Array */ void prsdta(void) { dtype = DTARRY; expect('{'); dlen = 0; do { prslit(); //Parse Literal dattmp[dlen++] = litval; } while (look(',')); expect('}'); } /* Parse Data String */ void prsdts(void) { dtype = DTSTR; getstr(); strcpy(value, word); DEBUG("Parsed Data String '%s'\n", value) } /* Store variable data * * Uses: value - Data to store * * Sets: datvar[] - Variable Data * * datlen[] - Data Length */ void setdat(void) { int i; if (dtype == DTBYTE) { DEBUG("Setting variable data to '%d'\n", litval) dlen = 1; datvar[dsize++] = litval; } else if (dtype == DTARRY) { DEBUG("Setting variable data to array of length %d\n", dlen) for (i=0; i 0) { DEBUG("Allocating array '%s'\n", varnam[i]) asmlin(STROP, varsiz[i]); } else { DEBUG("Allocating variable '%s'\n", varnam[i]) asmlin(BYTEOP, "0"); } } vrwrtn = TRUE; } /* Parse and Compile Struct Declaration */ void addstc(void) { if (!fndstc(word)) ERROR("Undefined Struct '%s\n", word,EXIT_FAILURE) strct = strcts[stcidx]; //Retrieve Structure getwrd(); //Get Variable Name addvar(MTNONE, VTSTRUCT); expect(';'); } /* Parse Struct Definition * * Uses: word - Struct Name */ void defstc(void) { DEBUG("Parsing struct definition\n", 0) if (fndstc(word)) ERROR("Duplicate Declaration of Struct '%s\n", word,EXIT_FAILURE) strncpy(strct.name, word, STCLEN); DEBUG("Set struct name to '%s'\n", word); strct.size = 0; //Initialize Struct Length do { getwrd(); //Get Member Name if (wordis("CHAR")) getwrd(); //Skip Optional Type Declaration if (fndmbr(stccnt, word)) ERROR("Duplicate Declaration of Struct Member '%s\n", word,EXIT_FAILURE) DEBUG("Parsing member %s", word) strncpy(membr.name, word, STMLEN); //Set Member Name membr.strcti = stcidx; //Set Parent Struct Index membr.offset = strct.size; //Set Offset into Struct DEBUG("Checking for array definition\n", 0) if (match('[')) { CCMNT('['); skpchr(); membr.stype = ARRAY; DEBUG("Parsing array size\n", 0) membr.size = prsnum(0xFF) + 1; expect(']'); } else { membr.stype = VARIABLE; membr.size = 1; } DEBUG("Set member type to %d", membr.stype) DETAIL(" and size to %d\n", membr.size); DEBUG("Adding member at index %d\n", mbrcnt); membrs[mbrcnt++] = membr; strct.size += membr.size; expect(';'); } while (!look('}')); expect(';'); if (strct.size > 256) ERROR("Structure Size %d Exceeds Limit of 256 bytes.\n", strct.size, EXIT_FAILURE); DEBUG("Adding struct with size %d", strct.size) DETAIL("at index %d\n", stccnt); strcts[stccnt++] = strct; } /* Print Struc Tables to Log File */ void logstc(void) { fprintf(logfil, "\n%-8s %5s\n", "Struct", "Size"); for (stcidx=0; stcidx