1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-17 15:06:29 +00:00

Tested and Debugged Pointer Dereferencing

This commit is contained in:
Curtis F Kaylor 2019-11-14 22:01:13 -05:00
parent 77b0f1017f
commit f85c640f9f
6 changed files with 87 additions and 51 deletions

View File

@ -186,6 +186,16 @@ void pargs(int argc, char *argv[]) {
if (outnam[0]) DEBUG("outnam set to '%s'\n", outnam)
}
/* Validate CPU Type *
* Uses: cputype *
* Sets: cmos */
void chkcpu(void) {
if (strcmp(cputyp, "6502") == 0) cmos = FALSE;
else if (strcmp(cputyp, "65C02") == 0) cmos = TRUE;
else ERROR("Invalid CPU Type %s\n", cputyp, EXIT_FAILURE)
}
int main(int argc, char *argv[]) {
debug = TRUE; //Output Debug Info
gencmt = TRUE; //Generate Assembly Language Comments
@ -195,7 +205,8 @@ int main(int argc, char *argv[]) {
init(); //Initialize Global Variables
pargs(argc, argv); //Parse Command Line Arguments
chkcpu(); //Validate CPU Type
opnsrc(); //Open Source File
opnout(); //Open Output File
opnlog(); //Open Log File

View File

@ -49,6 +49,7 @@ void prttim(); //Print elapsed time
#define ERROR(fmt, val, err) {fprintf(stderr, fmt, val);exterr(err);}
int debug; //Print Debug Info (TRUE or FALSE)
int cmos; //Flag: Use 65C02 Instruction Set
int gencmt; //Generate Assembly Language Comments
char asmcmt[LINELEN]; //Processed Assembly Language Comment

View File

@ -20,6 +20,8 @@ void pshtrm(void) {
oprstk[trmidx] = oper; //Put Current Operator on Stack
strcpy(trmstk[trmidx], term); //Put Current Term on Stack
trmidx++; //Increment Stack Pointer
DEBUG("expr.pshtrm: Pushed term %s ", term)
DETAIL("and operator '%onto stack'\n", oper)
}
/* Pop Term and Operator off Stack */
@ -27,6 +29,8 @@ void poptrm(void) {
trmidx--; //Decrement Stack Pointer
strcpy(term, trmstk[trmidx]); //Restore Current Term from Stack
oper = oprstk[trmidx]; //Restore Current Operator from Stack
DEBUG("expr.pshtrm: Popped term %s ", term)
DETAIL("and operator '%c' off stack\n", oper)
}
/* Parse value (literal or identifier) *
@ -35,13 +39,14 @@ void poptrm(void) {
* Sets: value - the value (as a string) *
* valtyp - value type */
void prsval(int alwreg, int alwcon) {
DEBUG("Parsing value\n", 0)
DEBUG("expr.prsval: Parsing value\n", 0)
skpspc();
if (islpre()) prslit(); //Parse Literal
else if (isalph()) prsvar(alwreg, alwcon); //Parse Variable
else if (isbtop()) prsbop(); //Parse Byte Operator
else expctd("literal or variable");
DEBUG("Parsed value of type %d\n", valtyp)
DEBUG("expr.prsval: Parsed value %s ", value)
DETAIL("of type %d\n", valtyp)
skpspc();
}
@ -58,7 +63,7 @@ void prcmns(void) {
void prsidx(int clbrkt) {
expect('[');
prsval(TRUE, TRUE); //Parse Value, Allow Registers & Constants
DEBUG("Parsed array index '%s'\n", value)
DEBUG("expr.prsidx: Parsed array index '%s'\n", value)
if (clbrkt) expect(']');
}
@ -69,21 +74,24 @@ void prsidx(int clbrkt) {
* word - array index raw string *
* Sets: term - modified variable name */
void prcsix(void) {
if (valtyp == LITERAL) {
strcat(term, "+");
strcat(term, word);
}
else if (strcmp(value, "Y")==0)
strcat(term, ",Y");
else {
if (strcmp(value, "A")==0) asmlin("TAX", "");
else if (strcmp(value, "X")!=0) asmlin("LDX", value);
strcat(term, ",X");
}
DEBUG("expr.prcsix: Processing simple array index %s\n", word);
if (valtyp == LITERAL) {
strcat(term, "+");
strcat(term, word);
}
else if (strcmp(value, "Y")==0)
strcat(term, ",Y");
else {
if (strcmp(value, "A")==0) asmlin("TAX", "");
else if (strcmp(value, "X")!=0) asmlin("LDX", value);
strcat(term, ",X");
}
DEBUG("expr.prcsix: Set term to %s\n", term);
}
/* Process Expression Array Index */
void prcxix(void) {
DEBUG("expr.prcxix: Processing Expression Array Index", 0)
pshtrm(); //Push Array Variable onto Term Stack
if (trmcnt) asmlin("PHA", ""); //Save Accumulator if not first term
prcftm(FALSE); //Process First Term of Expression
@ -92,6 +100,7 @@ void prcxix(void) {
if (trmcnt) asmlin("PLA", ""); //Restore Accumator if not first term
poptrm(); //Pop Array Variable off Term Stack
strcat(term, ",X");
DEBUG("expr.prcxix: Set term to %s\n", term);
}
/* Check for, Parse, and Process Index */
@ -117,12 +126,16 @@ void prsptr(void) {
DEBUG("Parsing pointer\n", 0)
expect('*'); //Pointer Dereference Operator
prsvar(FALSE,FALSE); //Parse Variable to Dereference
strcpy(term, value);
if (varble.modifr != MTZP) ERROR("Illegal dereference of non-pointer variable %s.\n", value, EXIT_FAILURE)
DEBUG("expr.prsptr: Set term to %s\n", term);
}
/* Process Pointer Index *
* Sets: term - Compiled Pointer */
void prcptx(char *index) {
DEBUG("expr.prcptx: Processing Dereferenced Pointer %s ", term)
DETAIL("index [%s]\n", index)
if (strcmp(index,"X")==0) ERROR("Illegal use of register X\n", 0, EXIT_FAILURE);
if (strcmp(index,"A")==0) asmlin("TAY", "");
else if (strcmp(index,"Y") != 0) asmlin("LDY", index);
@ -132,13 +145,19 @@ void prcptx(char *index) {
* Sets: term - Compiled Pointer */
int prcptr(void) {
prsptr();
DEBUG("Dereferencing pointer %s\n", value);
sprintf(term, "(%s),Y", value);
DEBUG("expr.prcptr: Dereferencing Pointer %s\n", value);
if (valtyp == ARRAY) {
prsidx(TRUE);
prcptx(value);
sprintf(word, "(%s),Y", term);
} else if (cmos) {
sprintf(word, "(%s)", term);
} else {
asmlin("LDY","0");
sprintf(word, "(%s),Y", term);
}
else asmlin("LDY","0");
strcpy(term, word);
DEBUG("expr.prcptr: Set term to %s\n", term);
return FALSE; //Return Value Not an Integer
}
@ -152,7 +171,7 @@ int prstrm(int alwint) {
if (valtyp == FUNCTION) ERROR("Function call only allowed in first term\n", 0, EXIT_FAILURE)
strcpy(term, value);
if (valtyp == VARIABLE && prcivr(alwint)) return TRUE;
DEBUG("Parsed term %s\n", term)
DEBUG("expr.prstrm: Parsed term %s\n", term)
chkidx(); //Check for Array Index
skpspc();
return FALSE;
@ -417,6 +436,7 @@ void prsxpi(char trmntr, int asmxpr) {
prsvar(FALSE, TRUE);
if (valtyp == FUNCTION) {
strcpy(term, value);
DEBUG("expr.prsxpi: Set term to %s\n", term)
prsfnc(0); //Parse Expression Function
} else if (valtyp == STRUCTURE) {
prsmbr(value);

View File

@ -83,7 +83,7 @@ void skpspc(void) {
* otherwise FALSE */
int look(char c) {
int found;
DEBUG("Looking for '%c', ", c);
DEBUG("parse.look: Looking for '%c', ", c);
skpspc();
found = match(c);
if (found) {
@ -141,8 +141,8 @@ void getwrd(void) {
while (isanum()) word[wrdlen++] = toupper(getnxt());
word[wrdlen] = 0;
ACMNT(word);
DEBUG("Read word '%s'", word)
DETAIL("Delimited by '%c'\n", nxtchr)
DEBUG("parse.getwrd: Read word '%s' ", word)
DETAIL("delimited by '%c'\n", nxtchr)
}
/* Escape Character */
@ -375,8 +375,9 @@ void poperr(char* name) {
/* Process Post Operator */
void prcpst(int isint, char* name, char *index, char indtyp, char ispntr) {
DEBUG("Processing post operation '%c'\n", oper)
if (ispntr) {sprintf(word,"(%s),Y", name); strcpy(name, word); }
DEBUG("parse.prcpst: Processing post operation '%c'\n", oper)
if (ispntr) ERROR("Post Operation on dereferenced pointer %s not supported\n", name, EXIT_FAILURE)
//sprintf(word,"(%s),Y", name); strcpy(name, word); }
char name1[VARLEN+3];
strcpy(name1, name); strcat(name1, "+1");
if (strlen(index)) {
@ -388,7 +389,7 @@ void prcpst(int isint, char* name, char *index, char indtyp, char ispntr) {
case '+':
if (strcmp(name, "X")==0) asmlin("INX", "");
else if (strcmp(name, "Y")==0) asmlin("INY", "");
else if (strcmp(name, "A")==0) poperr(name); //65C02 supports implicit INC, 6502 does not
else if (strcmp(name, "A")==0 && !cmos) poperr(name); //65C02 supports implicit INC, 6502 does not
else {
asmlin("INC", word);
if (isint) {
@ -402,7 +403,7 @@ void prcpst(int isint, char* name, char *index, char indtyp, char ispntr) {
case '-':
if (strcmp(name, "X")==0) asmlin("DEX", "");
else if (strcmp(name, "Y")==0) asmlin("DEY", "");
else if (strcmp(name, "A")==0) poperr(name); //65C02 supports implicit DEC, 6502 does not
else if (strcmp(name, "A")==0 && !cmos) poperr(name); //65C02 supports implicit DEC, 6502 does not
else {
if (isint) {
newlbl(skplbl);

View File

@ -78,7 +78,7 @@ void setasn(char *name, char ispntr) {
else word[0] = 0;
strcat(word, name);
if (wrtofs[0]) strcat(word, wrtofs);
if (ispntr) strcat(word,"),Y");
if (ispntr) strcat(word,")");
}
void prcixy(char rgstr, char* idx, int ivt) {
@ -133,7 +133,10 @@ void prcasn(char trmntr, char ispntr) {
if (ispntr) prcptx(asnidx); //Process Pointer Index
else prcidx(asnivt, word, asnidx); //Process Index
}
else if (ispntr) asmlin("LDY","0");
else if (ispntr && !cmos) {
strcat(word, ",Y");
asmlin("LDY","0");
}
asmlin("STA", word); //Store Return Value
}
@ -153,7 +156,7 @@ int getidx(char* idx) {
prsidx(TRUE); //Parse Array Index
if (valtyp == LITERAL) strncpy(idx, word, VARLEN);
else strncpy(idx, value, VARLEN);
DEBUG("Parsed index %s\n", idx)
DEBUG("stmnt.getidx: Parsed index %s\n", idx)
return valtyp;
}
@ -161,7 +164,7 @@ int getidx(char* idx) {
int prcava(char *name, char trmntr, char ispntr) {
strcpy(asnvar, name);
asntyp = valtyp; //Set Assigned Variable Type
DEBUG("Set STA variable to %s\n", asnvar)
DEBUG("stmnt.prcava: Set STA variable to %s\n", asnvar)
if (asntyp == VARIABLE && look(';')) {
asmlin("STA", asnvar);
return TRUE;
@ -169,7 +172,7 @@ int prcava(char *name, char trmntr, char ispntr) {
if (asntyp == ARRAY) asnivt = getidx(asnidx); //Get Array Index and Type
else asnidx[0] = 0;
if (ispntr && strcmp(asnidx, "X") == 0) ERROR("Illegal use of register X\n", 0, EXIT_FAILURE)
DEBUG("Set STA index to '%s'", asnidx) DETAIL(" and type to %d\n", asnivt)
DEBUG("stmnt.prcava: Set STA index to '%s'", asnidx) DETAIL(" and type to %d\n", asnivt)
if (ispopr()) {
if (prspst(trmntr, FALSE, asnvar, asnidx, asnivt, ispntr)) expctd("post operator");
return TRUE;
@ -234,7 +237,7 @@ void pasm(void) {
/* Parse and Compile Assignment of Pointer */
void prcasp(char trmntr) {
prsptr(); //Parse Pointer Dereference
DEBUG("Processing assignment to dereferenced pointer %s\n", value)
DEBUG("stmnt.prcasp: Processing assignment to dereferenced pointer %s\n", value)
if (prcava(value, trmntr, TRUE)) return; //Process Accumulator Assignment Variable
prcasn(trmntr, TRUE);
}

View File

@ -21,7 +21,7 @@
* varcnt if not found *
* Returns: TRUE if found, otherwise FALSE */
int fndvar(char *name) {
DEBUG("Looking up variable '%s'\n", name)
DEBUG("vars,fndvar: Looking up variable '%s'\n", name)
for (varidx=0; varidx<varcnt; varidx++) {
if (strcmp(vartbl[varidx].name, name) == 0) {
memcpy(&varble, &vartbl[varidx], sizeof(varble));
@ -37,7 +37,7 @@ int fndvar(char *name) {
* sctcnt if not found *
* Returns: TRUE if found, otherwise FALSE */
int fndstc(char *name) {
DEBUG("Looking up struct '%s'\n", name)
DEBUG("vars.fndstc: Looking up struct '%s'\n", name)
for (stcidx=0; stcidx<stccnt; stcidx++)
if (strcmp(strcts[stcidx].name, name) == 0) return TRUE;
return FALSE;
@ -48,7 +48,7 @@ int fndstc(char *name) {
* stmcnt if not found *
* Returns: TRUE if found, otherwise FALSE */
int fndmbr(int idx, char *name) {
DEBUG("Looking up member '%s'\n", word)
DEBUG("vars.fndmbr: Looking up member '%s'\n", word)
for (mbridx=0; mbridx<mbrcnt; mbridx++) {
if (membrs[mbridx].strcti != idx) continue;
if (strcmp(membrs[mbridx].name, name) == 0) {
@ -88,7 +88,7 @@ void prcmbr(char* name) {
getwrd(); //Get Member Name
valtyp = gettyp(); //Determine Variable Type
if (valtyp == FUNCTION) ERROR("Illegal Function Reference\n", 0, EXIT_FAILURE)
DEBUG("Checking for member %s", word) DETAIL(" with struct index %d\n", stcidx)
DEBUG("vars.prcmbr: Checking for member %s", word) DETAIL(" with struct index %d\n", stcidx)
if (!fndmbr(stcidx, word)) ERROR("Struct does Not Contain Member %s\n", word, EXIT_FAILURE)
mbrofs += membr.offset; //Get Member Offet in Struct
}
@ -117,7 +117,7 @@ void prsvrw(int alwreg, int alwcon) {
valtyp = gettyp(); //Determine Variable Type
if (valtyp != FUNCTION) chksym(alwreg, alwcon, word);
strcpy(value, word);
DEBUG("Parsed variable '%s'\n", value)
DEBUG("vars.prsvrw: Parsed variable '%s'\n", value)
if (valtyp == STRUCTURE) prsmbr(value);
}
@ -143,7 +143,7 @@ void reqvar(int alwary) {
* Returns: variable size (as integer */
int pidxof(void) {
expect('?'); //Check for and Skip SizeOf Operator
DEBUG("Parsing IndexOf operator", 0);
DEBUG("vars.pidxof: Parsing IndexOf operator", 0);
mbridx = -1; //Set Member Index to None
reqvar(FALSE); //Parse Variable Name to get Size Of
if (mbridx > -1) {
@ -159,7 +159,7 @@ int pidxof(void) {
* Returns: variable size (as integer */
int psizof(void) {
expect('@'); //Check for and Skip SizeOf Operator
DEBUG("Parsing SizeOf operator", 0);
DEBUG("vars.pdizof: Parsing SizeOf operator", 0);
mbridx = -1; //Set Member Index to None
reqvar(FALSE); //Parse Variable Name to get Size Of
if (mbridx > -1) {
@ -181,7 +181,7 @@ int psizof(void) {
/* Parse Data Array */
void prsdta(void) {
DEBUG("Parsing Array Data\n", 0)
DEBUG("vars.prsdta: Parsing Array Data\n", 0)
int i;
dtype = DTARRY;
expect('{');
@ -202,7 +202,7 @@ void prsdts(void) {
dtype = DTSTR;
getstr();
strcpy(value, word);
DEBUG("Parsed Data String '%s'\n", value)
DEBUG("vars.prsdts: Parsed Data String '%s'\n", value)
}
/* Store variable data *
@ -212,34 +212,34 @@ void prsdts(void) {
void setdat(void) {
int i;
if (dtype == DTBYTE) {
DEBUG("Setting variable data to '%d'\n", litval)
DEBUG("vars.setdat: Setting variable data to '%d'\n", litval)
dlen = 1;
datvar[dsize++] = litval;
}
else if (dtype == DTINT) {
DEBUG("Setting variable data to '%d'\n", litval)
DEBUG("vars.setdat: Setting variable data to '%d'\n", litval)
dlen = 2;
datvar[dsize++] = litval & 0xFF;
datvar[dsize++] = litval >> 8;
}
else if (dtype == DTARRY) {
DEBUG("Setting variable data to array of length %d\n", dlen)
DEBUG("vars.setdat: Setting variable data to array of length %d\n", dlen)
for (i=0; i<dlen; i++) datvar[dsize++] = dattmp[i];
}
else {
DEBUG("Setting variable data to '%s'\n", value)
DEBUG("vars.setdat: Setting variable data to '%s'\n", value)
dlen = strlen(value);
for (i=0; i<dlen; i++) datvar[dsize++] = value[i];
}
datlen[varcnt] = dlen;
dattyp[varcnt] = dtype;
DEBUG("Total data allocated: %d bytes\n", dsize)
DEBUG("vars.setdat: Total data allocated: %d bytes\n", dsize)
}
/* Parse and store variable data */
void prsdat(int m, int t) {
if ((m & MTCONST) == 0) ERROR("Initialization allowed only on variables declared CONST\n", 0, EXIT_FAILURE);
DEBUG("Parsing variable data\n", 0)
DEBUG("vars.prsdat: Parsing variable data\n", 0)
skpspc();
if (t == VTINT) {dtype = DTINT; litval = prsnum(0xFFFF); } //Parse Integer
else if (islpre()) {dtype = DTBYTE; prslit(); } //Parse Data Literal
@ -253,7 +253,7 @@ void prsdat(int m, int t) {
* Uses: vrname - variable name *
* value - variable size */
void setvar(int m, int t) {
DEBUG("Added variable '%s' ", vrname);
DEBUG("vars.setvar: Added variable '%s' ", vrname);
strncpy(vartbl[varcnt].name, vrname, VARLEN);
vartbl[varcnt].modifr = m;
vartbl[varcnt].type = t;
@ -294,17 +294,17 @@ void addvar(int m, int t) {
}
else {
if (t == VTSTRUCT) {
DEBUG("Setting variable size to %d\n", strct.size)
DEBUG("vars.addvar: Setting variable size to %d\n", strct.size)
sprintf(value, "%d", strct.size);
} else if (t == VTINT) {
DEBUG("Setting variable size to %d\n", 2)
DEBUG("vars.addvar: Setting variable size to %d\n", 2)
sprintf(value, "%d", 2);
} else if (match('[')) {
t = VTARRAY; //Set Type to Array
CCMNT('[')
skpchr();
if (alcvar) {
DEBUG("Parsing array size\n", 0)
DEBUG("vars.addvar: Parsing array size\n", 0)
sprintf(value, "%d", prsnum(0xFF) + 1);
}
expect(']');