From f85c640f9f5ab69fb13d28f26e11ab95c3301b18 Mon Sep 17 00:00:00 2001 From: Curtis F Kaylor Date: Thu, 14 Nov 2019 22:01:13 -0500 Subject: [PATCH] Tested and Debugged Pointer Dereferencing --- src/c02.c | 13 +++++++++++- src/common.h | 1 + src/expr.c | 56 +++++++++++++++++++++++++++++++++++----------------- src/parse.c | 15 +++++++------- src/stmnt.c | 15 ++++++++------ src/vars.c | 38 +++++++++++++++++------------------ 6 files changed, 87 insertions(+), 51 deletions(-) diff --git a/src/c02.c b/src/c02.c index fd633d6..a73658f 100644 --- a/src/c02.c +++ b/src/c02.c @@ -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 diff --git a/src/common.h b/src/common.h index 1ac2d8b..7ebee84 100644 --- a/src/common.h +++ b/src/common.h @@ -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 diff --git a/src/expr.c b/src/expr.c index 5cfc17c..807cee4 100644 --- a/src/expr.c +++ b/src/expr.c @@ -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); diff --git a/src/parse.c b/src/parse.c index 9dce446..83562b4 100644 --- a/src/parse.c +++ b/src/parse.c @@ -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); diff --git a/src/stmnt.c b/src/stmnt.c index cf28365..08336c3 100644 --- a/src/stmnt.c +++ b/src/stmnt.c @@ -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); } diff --git a/src/vars.c b/src/vars.c index 38ac9f1..b72812f 100644 --- a/src/vars.c +++ b/src/vars.c @@ -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 -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