2017-04-22 18:39:52 +00:00
|
|
|
/******************************************************
|
|
|
|
* C02 Label Parsing, Generation, and Lookup Routines *
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "asm.h"
|
|
|
|
#include "parse.h"
|
|
|
|
#include "label.h"
|
|
|
|
|
2017-05-01 01:17:50 +00:00
|
|
|
/* Find Last Label of Specified Type *
|
|
|
|
* Args: lbtype: Label type *
|
|
|
|
* Sets: tmplbl - Label name *
|
|
|
|
* Returns: Index into label table *
|
|
|
|
* (-1 if not found) */
|
|
|
|
int lstlbl(int lbtype)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
DEBUG("Searching for label type %d\n", lbtype);
|
|
|
|
for (i = lblcnt - 1; i>-1; i--)
|
|
|
|
if (lbltyp[i] == lbtype) break;
|
|
|
|
DEBUG("Search produced label index %d\n", i);
|
|
|
|
if (i>=0)
|
|
|
|
strcpy(tmplbl, lblnam[i]);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Block Flag for Last Label */
|
|
|
|
void setblk(int blkflg)
|
|
|
|
{
|
|
|
|
lblblk[lblcnt-1] = blkflg;
|
|
|
|
}
|
|
|
|
|
2017-04-22 18:39:52 +00:00
|
|
|
/* Set label for next line of *
|
|
|
|
* Assembly Language Code *
|
|
|
|
* to word */
|
|
|
|
void setlbl(char *lblset)
|
|
|
|
{
|
2017-05-05 03:15:53 +00:00
|
|
|
DEBUG("Setting Label '%s'\n", lblset);
|
2017-04-22 18:39:52 +00:00
|
|
|
if (strlen(lblasm) > 0)
|
|
|
|
asmlin("",""); //Emit Block End Label on it's own line
|
2017-05-01 01:17:50 +00:00
|
|
|
if (strlen(lblset) > LABLEN)
|
|
|
|
ERROR("Label '%s' exceeds maximum size\n", word, EXIT_FAILURE);
|
2017-04-22 18:39:52 +00:00
|
|
|
strcpy(lblasm, lblset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse label in code */
|
|
|
|
void prslbl()
|
|
|
|
{
|
|
|
|
DEBUG("Parsing Label '%s''\n", word);
|
|
|
|
skpchr(); //skip ':'
|
|
|
|
setlbl(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* generate new label */
|
2017-05-16 00:25:11 +00:00
|
|
|
void newlbl(char* lbname)
|
2017-04-22 18:39:52 +00:00
|
|
|
{
|
2017-05-16 00:25:11 +00:00
|
|
|
sprintf(lbname, LABFMT, lblnxt++);
|
|
|
|
DEBUG("Generated new label '%s'\n", lbname);
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 01:17:50 +00:00
|
|
|
/* Pop Label from Stack and Emit on Next Line */
|
|
|
|
int poplbl()
|
2017-04-22 18:39:52 +00:00
|
|
|
{
|
|
|
|
int lbtype = lbltyp[--lblcnt];
|
2017-05-01 01:17:50 +00:00
|
|
|
DEBUG("Popped label type %d\n", lbtype);
|
|
|
|
if (lbtype == LTLOOP)
|
|
|
|
asmlin("JMP", lblnam[lblcnt--]); //Jump to Beginning of Loop
|
2017-05-16 00:25:11 +00:00
|
|
|
if (lbtype == LTFUNC) {
|
|
|
|
if (!lsrtrn) asmlin("RTS", ""); //Return From Subroutine
|
|
|
|
}
|
|
|
|
else if (lbtype == LTDO)
|
2017-05-01 01:17:50 +00:00
|
|
|
strcpy(loplbl, lblnam[lblcnt]);
|
2017-04-22 18:39:52 +00:00
|
|
|
else
|
|
|
|
setlbl(lblnam[lblcnt]);
|
2017-05-01 01:17:50 +00:00
|
|
|
inblck = lblblk[lblcnt-1];
|
|
|
|
return lbtype;
|
2017-04-22 18:39:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 01:17:50 +00:00
|
|
|
/* Push Label onto Stack *
|
|
|
|
* Args: lbltyp - Label type *
|
|
|
|
* Uses: curlbl - Label to push */
|
2017-05-16 00:25:11 +00:00
|
|
|
void pshlbl(int lbtype, char* lbname)
|
2017-05-01 01:17:50 +00:00
|
|
|
{
|
2017-05-05 03:15:53 +00:00
|
|
|
DEBUG("Pushing label type %d\n", lbtype);
|
2017-05-16 00:25:11 +00:00
|
|
|
strcpy(lblnam[lblcnt], lbname);
|
2017-05-01 01:17:50 +00:00
|
|
|
lbltyp[lblcnt] = lbtype;
|
|
|
|
lblblk[lblcnt++] = FALSE;
|
2017-05-16 00:25:11 +00:00
|
|
|
DEBUG("Pushed label '%s' onto stack\n", lbname);
|
2017-05-01 01:17:50 +00:00
|
|
|
}
|
2017-04-22 18:39:52 +00:00
|
|
|
|