2017-04-22 18:39:52 +00:00
|
|
|
/*************************************
|
|
|
|
* C02 Variable Management Routines *
|
|
|
|
*************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "files.h"
|
|
|
|
#include "asm.h"
|
|
|
|
#include "parse.h"
|
2017-05-16 00:25:11 +00:00
|
|
|
#include "label.h"
|
2017-04-22 18:39:52 +00:00
|
|
|
#include "vars.h"
|
|
|
|
|
2018-02-09 03:06:25 +00:00
|
|
|
/* Lookup variable name in variable table *
|
|
|
|
* Sets: varidx = index into varnam array *
|
|
|
|
* varcnt if not found *
|
|
|
|
* Returns: TRUE if found, otherwise FALSE */
|
2017-06-27 00:16:23 +00:00
|
|
|
int fndvar(char *name)
|
2017-04-22 18:39:52 +00:00
|
|
|
{
|
|
|
|
DEBUG("Looking up variable '%s'\n", word);
|
2018-02-09 03:06:25 +00:00
|
|
|
for (varidx=0; varidx<varcnt; varidx++) {
|
|
|
|
if (strcmp(varnam[varidx], name) == 0)
|
|
|
|
return TRUE;
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
2018-02-09 03:06:25 +00:00
|
|
|
return FALSE;
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for variable *
|
2017-06-27 00:16:23 +00:00
|
|
|
* Generates error if variable is undefined *
|
|
|
|
* Args: alwreg - allow register name *
|
|
|
|
* name - variable name */
|
|
|
|
void chksym(int alwreg, char *name)
|
2017-04-22 18:39:52 +00:00
|
|
|
{
|
2017-06-27 00:16:23 +00:00
|
|
|
if (strlen(name) == 1 && strchr("AXY", name[0])) {
|
|
|
|
if (alwreg) return;
|
|
|
|
else ERROR("Illegal reference to register %s\n", name, EXIT_FAILURE);
|
|
|
|
}
|
2018-02-09 03:06:25 +00:00
|
|
|
if (!fndvar(name))
|
2017-06-27 00:16:23 +00:00
|
|
|
ERROR("Undeclared variable '%s' encountered\n", name, EXIT_FAILURE);
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 00:25:11 +00:00
|
|
|
|
2017-06-27 00:16:23 +00:00
|
|
|
/* Parse Variable Name *
|
|
|
|
* Parameters: alwary - Allow Array Reference *
|
|
|
|
* Sets: vrname - operand for LDA/STA/LDY/STY */
|
|
|
|
void reqvar(int alwary)
|
2017-05-16 00:25:11 +00:00
|
|
|
{
|
2017-06-27 00:16:23 +00:00
|
|
|
prsvar(FALSE);
|
|
|
|
if (!alwary)
|
|
|
|
if (valtyp != VARIABLE)
|
|
|
|
expctd("Variable");
|
2017-05-16 00:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 18:39:52 +00:00
|
|
|
/* Check for Array specifier and get size *
|
|
|
|
* Sets: value - array size (as string) *
|
|
|
|
* "" if not an array */
|
|
|
|
void pvarsz()
|
|
|
|
{
|
|
|
|
DEBUG("Checking for array definition\n", 0);
|
|
|
|
value[0] = 0;
|
|
|
|
if (match('[')) {
|
|
|
|
skpchr();
|
|
|
|
if (alcvar) {
|
|
|
|
DEBUG("Parsing array size\n", 0);
|
2017-06-27 00:16:23 +00:00
|
|
|
sprintf(value, "%d", prsnum(0xFF) + 1);
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
expect(']');
|
|
|
|
}
|
|
|
|
if (!alcvar)
|
|
|
|
strcpy(value, "*");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse Data Constant */
|
|
|
|
void prsdtc()
|
|
|
|
{
|
|
|
|
dtype = DTBYTE;
|
2017-06-27 00:16:23 +00:00
|
|
|
prscon();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse Data Array */
|
|
|
|
void prsdta()
|
|
|
|
{
|
|
|
|
dtype = DTARRY;
|
|
|
|
expect('{');
|
|
|
|
dlen = 0;
|
|
|
|
while (TRUE) {
|
|
|
|
prscon();
|
|
|
|
dattmp[dlen++] = cnstnt;
|
|
|
|
if (!look(','))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
expect('}');
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse Data String */
|
|
|
|
void prsdts()
|
|
|
|
{
|
|
|
|
dtype = DTSTR;
|
2017-05-01 01:17:50 +00:00
|
|
|
getstr();
|
2017-04-22 18:39:52 +00:00
|
|
|
strcpy(value, word);
|
|
|
|
DEBUG("Parsed Data String '%s'\n", value);
|
|
|
|
}
|
|
|
|
|
2017-05-01 01:17:50 +00:00
|
|
|
/* Store variable data *
|
|
|
|
* Uses: value - Data to store *
|
2017-04-22 18:39:52 +00:00
|
|
|
* Sets: datvar[] - Variable Data *
|
|
|
|
* datlen[] - Data Length */
|
2017-05-01 01:17:50 +00:00
|
|
|
void setdat()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (dtype == DTBYTE) {
|
|
|
|
DEBUG("Setting variable data to '%d'\n", cnstnt);
|
|
|
|
dlen = 1;
|
|
|
|
datvar[dsize++] = cnstnt;
|
|
|
|
}
|
2017-06-27 00:16:23 +00:00
|
|
|
else if (dtype == DTARRY) {
|
|
|
|
DEBUG("Setting variable data to array of length %d\n", dlen);
|
|
|
|
for (i=0; i<dlen; i++)
|
|
|
|
datvar[dsize++] = dattmp[i];
|
|
|
|
}
|
2017-05-01 01:17:50 +00:00
|
|
|
else {
|
|
|
|
DEBUG("Setting variable data to '%s'\n", value);
|
|
|
|
dlen = strlen(value);
|
|
|
|
for (i=0; i<dlen; i++)
|
|
|
|
datvar[dsize++] = value[i];
|
|
|
|
}
|
|
|
|
datlen[varcnt] = dlen;
|
2018-01-28 18:27:33 +00:00
|
|
|
dattyp[varcnt] = dtype;
|
|
|
|
DEBUG("Total data alllocated: %d bytes\n", dsize);
|
2017-05-01 01:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse and store variable data */
|
2017-04-22 18:39:52 +00:00
|
|
|
void prsdat()
|
|
|
|
{
|
|
|
|
DEBUG("Checking for variable data\n", 0);
|
|
|
|
if (!look('=')) {
|
|
|
|
datlen[varcnt] = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
skpspc();
|
2017-06-27 00:16:23 +00:00
|
|
|
if (iscpre())
|
|
|
|
prsdtc(); //Parse Data Constant
|
2017-04-22 18:39:52 +00:00
|
|
|
else if (match('"'))
|
2017-05-01 01:17:50 +00:00
|
|
|
prsdts(); //Parse Data String
|
2017-06-27 00:16:23 +00:00
|
|
|
else if (match('{'))
|
|
|
|
prsdta(); //Parse Data Array
|
2017-05-01 01:17:50 +00:00
|
|
|
else
|
|
|
|
expctd("numeric or string constant");
|
|
|
|
setdat(); //Store Data Value
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add Variable to Variable table *
|
2017-05-01 01:17:50 +00:00
|
|
|
* Uses: word - variable name *
|
|
|
|
* value - variable size */
|
2018-01-28 18:27:33 +00:00
|
|
|
void setvar(int m, int t)
|
2017-05-01 01:17:50 +00:00
|
|
|
{
|
2018-01-28 18:27:33 +00:00
|
|
|
DEBUG("Added variable '%s' ", word);
|
2017-05-05 03:15:53 +00:00
|
|
|
strncpy(varnam[varcnt], vrname, VARLEN);
|
2018-01-28 18:27:33 +00:00
|
|
|
varmod[varcnt] = m;
|
2017-05-01 01:17:50 +00:00
|
|
|
vartyp[varcnt] = t;
|
|
|
|
strncpy(varsiz[varcnt], value, 3);
|
2018-01-28 18:27:33 +00:00
|
|
|
DETAIL("at index %d\n", varcnt);
|
2017-05-01 01:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse and Compile Variable Declaration *
|
2017-04-22 18:39:52 +00:00
|
|
|
* Uses: word - variable name */
|
2017-06-27 00:16:23 +00:00
|
|
|
void addvar(int m, int t)
|
2017-04-22 18:39:52 +00:00
|
|
|
{
|
2017-05-05 03:15:53 +00:00
|
|
|
strcpy(vrname, word); //Save Variable Name
|
2018-02-09 03:06:25 +00:00
|
|
|
if (fndvar(vrname))
|
2017-04-22 18:39:52 +00:00
|
|
|
ERROR("Duplicate declaration of variable '%s\n", word,EXIT_FAILURE);
|
|
|
|
if (t == VTVOID)
|
|
|
|
ERROR("Illegal Variable Type\n", 0, EXIT_FAILURE);
|
2017-06-27 00:16:23 +00:00
|
|
|
if (m == MTZP) {
|
|
|
|
setlbl(vrname);
|
|
|
|
sprintf(word, "$%hhX", zpaddr++);
|
|
|
|
asmlin(EQUOP, word);
|
|
|
|
strcpy(value, "*"); //Set Variable Type to Zero Page
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pvarsz(); //Check for Array Declaration and Get Size
|
2018-01-28 18:27:33 +00:00
|
|
|
setvar(m, t); //Add to Variable Table
|
2017-06-27 00:16:23 +00:00
|
|
|
if (m != MTZP)
|
|
|
|
prsdat(); //Parse Variable Data
|
2017-05-01 01:17:50 +00:00
|
|
|
varcnt++; //Increment Variable Counter
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 00:25:11 +00:00
|
|
|
/* Add Function Definition */
|
2017-04-22 18:39:52 +00:00
|
|
|
void addfnc()
|
|
|
|
{
|
2017-06-27 00:16:23 +00:00
|
|
|
ACMNT(word);
|
|
|
|
expect('(');
|
2017-05-16 00:25:11 +00:00
|
|
|
strcpy(fncnam, word); //Save Function Name
|
|
|
|
prmcnt = 0; //Set Number of Parameters
|
|
|
|
skpspc(); //Skip Spaces
|
|
|
|
if (isalph()) { //Parse Parameters
|
2017-06-27 00:16:23 +00:00
|
|
|
reqvar(FALSE); //Get First Parameter
|
2017-05-16 00:25:11 +00:00
|
|
|
strcpy(prmtra, value);
|
|
|
|
prmcnt++;
|
|
|
|
if (look(',')) {
|
2017-06-27 00:16:23 +00:00
|
|
|
reqvar(FALSE); //Get Second Parameter
|
2017-05-16 00:25:11 +00:00
|
|
|
strcpy(prmtry, value);
|
|
|
|
prmcnt++;
|
|
|
|
if (look(',')) {
|
2017-06-27 00:16:23 +00:00
|
|
|
reqvar(FALSE); //Third Parameter
|
2017-05-16 00:25:11 +00:00
|
|
|
strcpy(prmtrx, value);
|
|
|
|
prmcnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-22 18:39:52 +00:00
|
|
|
expect(')');
|
2017-05-16 00:25:11 +00:00
|
|
|
if (look(';')) //Forward Definition
|
|
|
|
return;
|
|
|
|
setlbl(fncnam); //Set Function Entry Point
|
|
|
|
if (prmcnt > 0)
|
|
|
|
asmlin("STA", prmtra); //Store First Parameter
|
|
|
|
if (prmcnt > 1)
|
|
|
|
asmlin("STY", prmtry); //Store Second Parameter
|
|
|
|
if (prmcnt > 2)
|
|
|
|
asmlin("STX", prmtrx); //Store Third Parameter
|
|
|
|
endlbl[0] = 0; //Create Dummy End Label
|
|
|
|
pshlbl(LTFUNC, endlbl); //and Push onto Stack
|
2018-02-07 03:50:50 +00:00
|
|
|
bgnblk('{'); //Start Program Block
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (Check For and) Parse Variable Declaration*/
|
2017-06-27 00:16:23 +00:00
|
|
|
void pdecl(int m, int t)
|
2017-04-22 18:39:52 +00:00
|
|
|
{
|
|
|
|
DEBUG("Processing variable declarations(s) of type %d\n", t);
|
|
|
|
while(TRUE) {
|
|
|
|
getwrd();
|
2017-06-27 00:16:23 +00:00
|
|
|
if (match('(')) {
|
|
|
|
if (m != MTNONE) {
|
|
|
|
ERROR("Illegal Modifier %d in Function Definion", m, EXIT_FAILURE);
|
|
|
|
}
|
2017-05-16 00:25:11 +00:00
|
|
|
addfnc(); //Add Function Call
|
|
|
|
return;
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
2017-06-27 00:16:23 +00:00
|
|
|
addvar(m, t);
|
2017-04-22 18:39:52 +00:00
|
|
|
if (!look(','))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
expect(';');
|
2018-01-28 18:27:33 +00:00
|
|
|
DEBUG("Variable Declaration Completed\n", 0);
|
|
|
|
SCMNT(""); //Clear Assembler Comment
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 00:16:23 +00:00
|
|
|
/* Check for and Parse Type Keyword */
|
|
|
|
int ptype(int m)
|
|
|
|
{
|
|
|
|
int result = TRUE;
|
|
|
|
if (wordis("VOID"))
|
|
|
|
pdecl(m, VTVOID); //Parse 'void' declaration
|
|
|
|
else if (wordis("CHAR"))
|
|
|
|
pdecl(m, VTCHAR); //Parse 'char' declaration
|
|
|
|
else
|
|
|
|
result = FALSE;
|
2018-01-28 18:27:33 +00:00
|
|
|
//DEBUG("Returning %d from function ptype\n", result)'
|
2017-06-27 00:16:23 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for and Parse Modifier */
|
|
|
|
int pmodfr()
|
|
|
|
{
|
|
|
|
DEBUG("Parsing modifier '%s'\n", word);
|
|
|
|
int result = TRUE;
|
2018-01-28 18:27:33 +00:00
|
|
|
if (wordis("ALIGNED")) {
|
|
|
|
getwrd();
|
|
|
|
ptype(MTALGN);
|
|
|
|
}
|
|
|
|
else if (wordis("ZEROPAGE")) {
|
2017-06-27 00:16:23 +00:00
|
|
|
getwrd();
|
|
|
|
ptype(MTZP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = FALSE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-22 18:39:52 +00:00
|
|
|
/* Write Variable Data */
|
|
|
|
void vardat(int i)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
DEBUG("Building Data for Variable '%s'\n", varnam[i]);
|
|
|
|
value[0] = 0;
|
|
|
|
for (j=0; j<datlen[i]; j++) {
|
|
|
|
if (j) strcat(value,",");
|
2017-05-01 01:17:50 +00:00
|
|
|
sprintf(word, "$%hhX", datvar[dlen++]);
|
2017-04-22 18:39:52 +00:00
|
|
|
strcat(value, word);
|
|
|
|
}
|
|
|
|
if (dattyp[i] == DTSTR) strcat(value, ",$00");
|
|
|
|
DEBUG("Allocating Data for Variable '%s'\n", varnam[i]);
|
|
|
|
asmlin(BYTEOP, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write Variable Table */
|
|
|
|
void vartbl()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
DEBUG("Writing Variable Table", 0);
|
|
|
|
dlen = 0;
|
|
|
|
for (i=0; i<varcnt; i++) {
|
|
|
|
strcpy(lblasm, varnam[i]);
|
|
|
|
DEBUG("Set Label to '%s'\n", lblasm);
|
|
|
|
if (strcmp(varsiz[i], "*") == 0)
|
|
|
|
continue;
|
2018-01-28 18:27:33 +00:00
|
|
|
if (varmod[i] == MTALGN) {
|
|
|
|
DEBUG("Alligning variable '%s'\n", varnam[i]);
|
|
|
|
asmlin(ALNOP, "256");
|
|
|
|
}
|
2017-06-27 00:16:23 +00:00
|
|
|
if (datlen[i])
|
2017-04-22 18:39:52 +00:00
|
|
|
vardat(i); //Write Variable Data
|
|
|
|
else if (strlen(varsiz[i]) > 0) {
|
|
|
|
DEBUG("Allocating array '%s'\n", varnam[i]);
|
|
|
|
asmlin(STROP, varsiz[i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG("Allocating variable '%s'\n", varnam[i]);
|
|
|
|
asmlin(BYTEOP, "0");
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 00:16:23 +00:00
|
|
|
vrwrtn = TRUE;
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print Variable Table to Log File */
|
|
|
|
void logvar()
|
|
|
|
{
|
|
|
|
int i;
|
2017-05-01 01:17:50 +00:00
|
|
|
fprintf(logfil, "\n%-31s %s %s %s\n", "Variable", "Type", "Size", "Data");
|
2017-04-22 18:39:52 +00:00
|
|
|
for (i=0; i<varcnt; i++)
|
|
|
|
{
|
2017-05-01 01:17:50 +00:00
|
|
|
fprintf(logfil, "%-31s %4d %4s %1d-%d\n", varnam[i], vartyp[i], varsiz[i], dattyp[i], datlen[i]);
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|