[llvm-commits] CVS: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/Makefile emitter.c error.c global.h init.c lexer.c main.c parser.c symbol.c
Chris Lattner
lattner at cs.uiuc.edu
Tue Oct 5 12:24:17 PDT 2004
Changes in directory llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler:
Makefile added (r1.1)
emitter.c added (r1.1)
error.c added (r1.1)
global.h added (r1.1)
init.c added (r1.1)
lexer.c added (r1.1)
main.c added (r1.1)
parser.c added (r1.1)
symbol.c added (r1.1)
---
Log message:
New benchmark, also missing an input
---
Diffs of the changes: (+2374 -0)
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/Makefile
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/Makefile:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/Makefile Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,6 ----
+ LEVEL = ../../../..
+
+ PROG = compiler
+ #STDIN_FILENAME = $(SourceDir)/testset
+ include $(LEVEL)/MultiSource/Makefile.multisrc
+
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/emitter.c
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/emitter.c:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/emitter.c Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,171 ----
+ /**** emitter.c *******************************************************/
+
+ #include "global.h"
+
+ void emit(int t, int tval, float rval) /* generates output */
+ {
+ if (ErrorFlag)
+ return;
+
+ ++NumberC;
+ if (NumberC >= 8)
+ {
+ printf("\n");
+ NumberC = 0;
+ } /* end if NumberC */
+
+ switch(t) {
+ case RADD:
+ printf("RADD, ");
+ break;
+ case ADD:
+ printf("ADD, ");
+ break;
+ case RSUB:
+ printf("RSUB, ");
+ break;
+ case SUB:
+ printf("SUB, ");
+ break;
+ case RMUL:
+ printf("RMUL, ");
+ break;
+ case MUL:
+ printf("MUL, ");
+ break;
+ case RDIV:
+ printf("RDIV, ");
+ break;
+ case DIV:
+ printf("DIV, ");
+ break;
+ case PUSH_LOC_VAR_ADDR:
+ printf("'%s#%d, ", LocalTable[tval].lexptr, Scope);
+ break;
+ case PUSH_GLO_VAR_ADDR:
+ printf("'%s#0, ", GlobalTable[tval].lexptr);
+ break;
+ case WRITEI:
+ printf("WRITEI, ");
+ break;
+ case WRITEF:
+ printf("WRITER, ");
+ break;
+ case WRITELN:
+ printf("NEWLINE, ");
+ break;
+ case STORE:
+ printf("ST, ");
+ break;
+ case IST:
+ printf("IST, ");
+ break;
+ case STH:
+ printf("STH, ");
+ break;
+ case VAL:
+ printf("VAL, ");
+ break;
+ case VALB:
+ printf("VALB, ");
+ break;
+ case PUSH_GLO_VAR_VALUE:
+ printf("@%s#0, ",GlobalTable[tval].lexptr);
+ break;
+ case PUSH_LOC_VAR_VALUE:
+ printf(">%s#%d, ", LocalTable[tval].lexptr, Scope);
+ break;
+ case READ:
+ printf("READ, ");
+ break;
+ case NUM:
+ printf("%d, ", tval);
+ break;
+ case RNUM:
+ printf("%16.4e, ", rval);
+ break;
+ case GLO_DECL: /* here tval is a subscript to the symbol table */
+ printf("%s#0 = %d, ", GlobalTable[tval].lexptr, DecCount);
+ break;
+ case LOC_DECL:
+ printf("%s#%d = %d, ", LocalTable[tval].lexptr, Scope, offset);
+ break;
+ case NEG:
+ printf("NEG, ");
+ break;
+ case RNEG:
+ printf("RNEG, ");
+ break;
+ case INT:
+ printf("FIX, ");
+ break;
+ case FLOAT:
+ printf("FLOAT, ");
+ break;
+ case POP:
+ printf("POP, ");
+ break;
+ case LABEL_CODE:
+ printf("$%d: ", tval);
+ break;
+ case PUSH_CODE_LABEL:
+ printf("$%d, ", tval);
+ break;
+ case COMP:
+ printf("COMP, ");
+ break;
+ case SWAP:
+ printf("SWAP, ");
+ break;
+ case BEQ:
+ printf("BEQ, ");
+ break;
+ case BNE:
+ printf("BNE, ");
+ break;
+ case B:
+ printf("B, ");
+ break;
+ case STHB:
+ printf("STHB, ");
+ break;
+ case LABEL_FUNC:
+ printf("%s: ", GlobalTable[tval].lexptr);
+ break;
+ case ISTB:
+ printf("ISTB, ");
+ break;
+ case STORE_RA:
+ printf("RA%s = 0, ", GlobalTable[tval].lexptr);
+ printf("'RA%s, ", GlobalTable[tval].lexptr);
+ printf("istb, ");
+ break;
+ case SAVE_FRAMESIZE:
+ printf("frame%s = %d, ", GlobalTable[tval].lexptr, offset);
+ break;
+ case PUSH_FRAMESIZE:
+ printf("'frame%s, ", GlobalTable[tval].lexptr);
+ break;
+ case IB:
+ printf("IB, ");
+ break;
+ case DB:
+ printf("DB, ");
+ break;
+ case PUSH_RA:
+ printf(">RA%s, ", GlobalTable[tval].lexptr);
+ break;
+ case BEGIN:
+ printf("\n%%%d, START: $3, sb, $2, $1, b,\n", DecCount);
+ NumberC = 0;
+ break;
+ case END:
+ printf("\n$2: STOP, $3: ->START.\n");
+ break;
+ case NOTDEC:
+ printf("\n Function %s called but not parsed.\n",GlobalTable[tval].lexptr);
+ break;
+ default:
+ printf("token %d, tval %d, rval %f\n", t, tval, rval);
+ }
+ }
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/error.c
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/error.c:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/error.c Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,39 ----
+ /**** error.c **********************************************************/
+
+ #include "global.h"
+ extern void match(int t);
+
+ void error(char *m) /* generates all error messages */
+ {
+ ErrorFlag = 1;
+ printf("\nERROR: line %d: %s \n", lineno, m);
+
+ if (lookahead == DONE)
+ return;
+
+ printf("Skipping: ");
+ while ((lookahead != ';') && (lookahead != EOF))
+ {
+ lookahead = getc(stdin);
+ if (lookahead == '\n')
+ ++lineno;
+ if (lookahead != EOF)
+ printf ("%c", lookahead);
+ }
+
+ if (lookahead == EOF)
+ {
+ lookahead = DONE;
+ return;
+ }
+ else
+ match(';');
+
+ printf("\n continuing parsing...\n");
+ return;
+ }
+
+
+
+
+
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/global.h
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/global.h:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/global.h Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,133 ----
+
+ /****** global.h ********************************************************/
+
+ #include <stdio.h>
+ #include <ctype.h>
+ /*
+ extern char *strcpy();
+ extern int strcmp();
+ */
+
+ #define STRMAX 999 /* size of lexemes array */
+ #define SYMMAX 100 /* size of symtable */
+
+ #define BSIZE 128 /* buffer size */
+ #define NONE -1
+ #define EOS '\0' /* END OF STRING */
+
+ #define NUM 256 /* token for integers */
+ #define ID 259 /* token for identifiers */
+ #define DONE 260 /* token for end of file */
+
+ #define RNUM 2001 /* token for real numbers */
+ #define INT 2002 /* token for the string "int" */
+ #define FLOAT 2003 /* token for the string "float" */
+ #define POP 2004 /* token for poping to a labeled location */
+ #define PUSH_LABEL 2005 /* token for outputing push from labeled loc. */
+ #define ARRAY 2006 /* token for an array referance */
+ #define WHILE 2007 /* token for the keyword while */
+ #define IF 2008 /* token for the keyword if */
+ #define READ 2009 /* token for the keyword read */
+ #define WRITE 2010 /* token for the keyword write */
+ #define WRITELN 2011 /* token for the keyword writeln */
+ #define EQUAL 2012 /* token for == */
+ #define NEQUAL 2013 /* token for != */
+ #define LABEL_CODE 2014 /* emitter code to label an assemble stmt */
+ #define PUSH_CODE_LABEL 2015 /* emitter code to push code labels */
+ #define COMP 2016 /* emitter code for comp opcode */
+ #define BEQ 2017 /* emitter code for BEQ */
+ #define B 2018 /* emitter code for B */
+ #define STORE 2020 /* emitter code to output ST */
+ #define STH 2022 /* emitter code for STH command */
+ #define VAL 2023 /* emitter code for VAL command */
+ #define ELSE 2024 /* token for keyword else */
+ #define BNE 2025 /* emitter code for BNE command */
+ #define BEGIN 2026 /* emitter code: print special char at start prg*/
+ #define END 2027 /* emitter code: print special char at end prog */
+ #define IST 2028 /* emitter code for IST instruction */
+ #define ADD 2029 /* emitter code for ADD */
+ #define RADD 2030 /* emitter code for RADD */
+ #define SUB 2031 /* emitter code for SUB */
+ #define RSUB 2032 /* emitter code for RSUB */
+ #define MUL 2033 /* emitter code for MUL */
+ #define RMUL 2034 /* emitter code for RMUL */
+ #define DIV 2035 /* emitter code for DIV */
+ #define RDIV 2036 /* emitter code for RDIV */
+ #define SWAP 2037 /* emitter code for SWAP */
+ #define NEG 2038 /* emitter code for NEG */
+ #define RNEG 2039 /* emitter code for RNEG */
+ #define FIX 2040 /* emitter code for FIX */
+ #define PARAM 2041 /* code for parameters in symbol tables */
+ #define RETURN 2042 /* token for return keyword */
+ #define READF 2043 /* token for readf() */
+ #define READI 2044 /* token for readi() */
+ #define WRITEF 2045 /* token for writef() */
+ #define WRITEI 2046 /* token for writei() */
+ #define LOC_DECL 2047 /* emitter code for a local declaration */
+ #define GLO_DECL 2048 /* emitter code for a global declaration */
+ #define PUSH_LOC_VAR_ADDR 2049 /* emitter code to push the address of a
+ local variable */
+ #define PUSH_GLO_VAR_ADDR 2050 /* emitter code to push the address of a
+ global varialbe */
+ #define PUSH_LOC_VAR_VALUE 2051 /* emitter code to push value stored
+ in a local variable */
+ #define PUSH_GLO_VAR_VALUE 2052 /* emitter code to push value stored
+ in a global variable */
+ #define STHB 2053 /* emitter code for STHB */
+ #define LABEL_FUNC 2054 /* emitter code to insert function name in code */
+ #define ISTB 2055 /* emitter code for ISTB instruction */
+ #define STORE_RA 2056 /* emitter code to store return address */
+ #define SAVE_FRAMESIZE 2057 /* emitter code to save frame size */
+ #define PUSH_RA 2058 /* emitter code to push the return address */
+ #define PUSH_FRAMESIZE 2059 /* emitter code for pushing the frame size */
+ #define IB 2060 /* emitter code for ib */
+ #define DB 2061 /* emitter code for db */
+ #define NOTDEC 2062 /* emitter code for funtion not parsed */
+ #define VALB 2063 /* emitter code for VALB */
+
+ int lookahead; /* token code number */
+ int tokenval; /* value of token attribute */
+ float ftokenval; /* global for float RNUM attribute */
+ int FloatFlag; /* used as boolean, 1 if we doing float calcs. */
+ int ErrorFlag; /* 1 if an error has occured, 0 otherwise */
+ int DecCount; /* keeps track of memeory locations */
+ int offset; /* offset within an activation record, local var*/
+ int lineno; /* the current line number */
+ int LabelCounter; /* used to create new labels for the mach. code */
+ int NumberC; /* keeps track of number of commands emitted */
+ char lexbuf[BSIZE]; /* stores lexeme to be inserted into symtable */
+ int LocalIndex; /* value returned from lookup in loctable */
+ int GlobalIndex; /* value returned from lookup to glotable */
+ int NextLookahead; /* the next token of the input */
+ int NextTokenval; /* the tokenval associated with the next token */
+ float NextFtokenval; /* the ftokenval associated with the next token */
+ int PreviousLookahead; /* the token that was last matched */
+ int PreviousTokenval; /* tokenval associated with the previous token*/
+ float PreviousFtokenval; /* ftokenval associated with the previous token*/
+ int Scope; /* the label assoc. with the current function
+ to be used in the creation of unique labels */
+ int ReturnLabel; /* stores the label to jump out of a function
+ if a return is encountered */
+ int CallReturnAddr; /* return address of a function call */
+ int FuncNameIndex; /* stores the index of the current function
+ name*/
+ int ArrayParsed; /* flag set to 1 if PushArrayCellAddr() called
+ while lookahead is ARRAY */
+
+ struct entry { /* form of symbol table entry */
+ char *lexptr; /* ptr to lexeme */
+ int token; /* token: ID or ARRAY */
+ int type; /* type of entry: INT or FLOAT */
+ int size; /* size of entry: 1 for INT or FLOAT,
+ larger for arrays */
+ int function; /* non-zero if is a function.
+ field is > 0 if function called but not parsed,
+ field is < 0 if function body has been parsed*/
+ int functionlabel; /* stores the machine code label associated
+ with the function */
+ };
+
+
+ struct entry GlobalTable[SYMMAX]; /* Global symbol table */
+ struct entry LocalTable[SYMMAX]; /* Local symbol table */
+
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/init.c
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/init.c:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/init.c Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,42 ----
+ /**** init.c ********************************************************/
+
+ #include "global.h"
+ extern int GlobalInsert(char s[], int tok, int type, int size, int function,
+ int functionlabel);
+
+ /* contains all keywords to be added to the symbol table */
+ struct entry keywords[] = {
+ { "int", INT, 0, 0, 0, 0 },
+ { "float", FLOAT, 0, 0, 0, 0 },
+ { "while", WHILE, 0, 0, 0, 0 },
+ { "if", IF, 0, 0, 0, 0 },
+ { "else", ELSE, 0, 0, 0, 0 },
+ { "read", READ, 0, 0, 0, 0 },
+ { "write", WRITE, 0, 0, 0, 0 },
+ { "writeln", WRITELN, 0, 0, 0, 0 },
+ { "return", RETURN, 0, 0, 0, 0 },
+ { "readf", READF, 0, 0, 0, 0 },
+ { "readi", READI, 0, 0, 0, 0 },
+ { "writef", WRITEF, 0, 0, 0, 0 },
+ { "writei", WRITEI, 0, 0, 0, 0 },
+ { 0, 0, 0, 0, 0, 0 }
+ };
+
+ void init(void)
+ {
+ /* loads keywords into symtable */
+ struct entry *p;
+ for (p = keywords; p->token; p++)
+ (void) GlobalInsert(p->lexptr, p->token, p->type, p->size, 0, 0);
+
+ lineno = 1;
+ LabelCounter = 1;
+ DecCount = 2;
+ ErrorFlag = 0;
+ NumberC = 0;
+ }
+
+
+
+
+
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/lexer.c
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/lexer.c:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/lexer.c Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,231 ----
+ /**** lexer.c ***************************************************************/
+
+
+ # include "global.h"
+ extern int Number(int t);
+ extern int Indentifier(int t);
+ extern int Equal(int t);
+ extern int Nequal(int t);
+ extern void error(char *m);
+ extern int LocalLookup(char s[]);
+ extern int GlobalLookup(char s[]);
+
+ int lexan(void)
+ {
+ int t; /* temp to check next char in input */
+
+ while (1)
+ {
+ t = getc(stdin);
+ if (t == ' ' || t == '\t')
+ ; /* strip out white space */
+
+ else if (t == '\n')
+ ++lineno;
+
+ else if (isdigit(t) || t == '.' )
+ return (Number(t));
+
+ else if ( isalpha(t) || t == '_')
+ return(Indentifier(t));
+
+ else if (t == '=')
+ return (Equal(t) );
+
+ else if (t == '!')
+ return (Nequal(t) );
+
+ else if (t == EOF)
+ return(DONE);
+
+ else
+ {
+ NextTokenval = NONE;
+ return(t);
+ } /* end else */
+ } /* end while (1) */
+ } /* end lexan */
+
+ /* ======================================================================= */
+
+ int Indentifier(int t)
+ {
+ int b = 0; /* used as a index to lexbuf */
+
+ if (t == '_')
+ {
+ lexbuf[b] = t;
+ ++b;
+ t = getc(stdin);
+
+ if (t == '_')
+ {
+ error("Indentifier cannot begin with a double underscore");
+ return(lookahead);
+ } /* end if (t == '_') */
+ } /* end if (t == '_') */
+
+ if (isalpha(t))
+ {
+ lexbuf[b] = t;
+ ++b;
+ t = getc(stdin);
+ } /* end if (isalpha(t)) */
+ else
+ {
+ error("improperly formed indentifier");
+ return(lookahead);
+ } /* end else */
+
+ while (isalpha(t) || isdigit(t) || t == '_')
+ {
+ lexbuf[b] = t;
+ ++b;
+ t = getc(stdin);
+ } /* end while */
+
+ (void) ungetc (t, stdin);
+ lexbuf[b] = EOS;
+
+ LocalIndex = LocalLookup(lexbuf);
+ GlobalIndex = GlobalLookup(lexbuf);
+
+ if (LocalIndex)
+ return(LocalTable[LocalIndex].token);
+ else if (GlobalIndex)
+ return (GlobalTable[GlobalIndex].token);
+ else
+ return(ID);
+ }
+
+ /* ======================================================================== */
+
+ int Equal(int t)
+ {
+ t = getc(stdin);
+ if (t == '=')
+ return(EQUAL);
+ else
+ {
+ (void) ungetc(t,stdin);
+ NextTokenval = NONE;
+ return('=');
+ } /* end else */
+ } /* end Equal function */
+
+ /* ======================================================================= */
+
+ int Nequal(int t)
+ {
+ t = getc(stdin);
+ if (t == '=')
+ return(NEQUAL);
+ else
+ {
+ (void) ungetc(t,stdin);
+ NextTokenval = NONE;
+ return('!');
+ } /* end else */
+ } /* end function Nequal */
+
+ /* ====================================================================== */
+
+ int Number(int t)
+ {
+ int DecimalAsInt; /* temp holder of the number */
+ int Exponent;
+ int NumOfZeros;
+
+ NextFtokenval = 0.0;
+ NextTokenval = 0;
+
+ /* get interger portion of float or the whole integer */
+ if (isdigit (t))
+ {
+ (void) ungetc(t, stdin);
+ scanf("%d", &NextTokenval);
+ t = getc(stdin);
+ if ((t != '.') && (t != 'e') && (t != 'E'))
+ {
+ (void) ungetc(t, stdin);
+ return (NUM);
+ }
+ } /* end if t is digit */
+
+ /* NextTokenval set at this point or NUM has been returned */
+ if (t == '.')
+ {
+ t = getc(stdin);
+ NumOfZeros = 0;
+
+ /*
+ counts the number of zeros left of decimal and right of the
+ first non-zero digit.
+ */
+ while (t == '0')
+ {
+ ++NumOfZeros;
+ t = getc(stdin);
+ } /* end while t == 0 */
+
+ /* reads the number after any leading zeros */
+ if (isdigit (t))
+ {
+ (void) ungetc(t, stdin);
+ scanf("%d", &DecimalAsInt);
+ NextFtokenval = (float) DecimalAsInt;
+
+ /* converts the decimal characters into a decimal of the number */
+ while (NextFtokenval >= 1.0)
+ {
+ NextFtokenval /= 10;
+ } /* end NextFtokenval > 1 */
+ if (NumOfZeros > 0)
+ {
+ while (NumOfZeros > 0)
+ {
+ NextFtokenval /= 10;
+ --NumOfZeros;
+ } /* end num of zeros > 0 */
+ } /* end if num of zeros > 0 */
+ t = getc(stdin); /* not needed here, but used later */
+ } /* end if t is digit */
+ } /* end if t == . */
+
+ /* tokenval and ftokenval set if needed, otherwise still zero */
+ NextFtokenval += NextTokenval;
+ if ((t == 'e') || (t == 'E'))
+ {
+ t = getc(stdin);
+ if ((t == '+') || (t == '-') || (isdigit (t)))
+ {
+ (void) ungetc(t, stdin);
+ scanf("%d", &Exponent);
+ t = getc(stdin); /* used for error checking, but also used later */
+ if (t == '.')
+ {
+ error("Exponents must be integer values");
+ return(lookahead);
+ } /* end if exponent value is a float */
+ if (Exponent > 0)
+ {
+ while (Exponent > 0)
+ {
+ NextFtokenval *= 10;
+ --Exponent;
+ } /* end while */
+ } /* end if Exponent > 0 */
+ else
+ {
+ while (Exponent < 0)
+ {
+ NextFtokenval /= 10;
+ ++Exponent;
+ } /* end while */
+ } /* end else Exponent > 0 */
+ } /* end if t is +, -, or a digit */
+ } /* if t == e or t == E */
+ (void) ungetc(t, stdin);
+ return(RNUM);
+ } /* end isdigit or . */
+
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/main.c
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/main.c:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/main.c Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,13 ----
+ /**** main.c *********************************************************/
+
+ #include "global.h"
+
+ extern void init(void);
+ extern void parse(void);
+
+ int main(void)
+ {
+ init();
+ parse();
+ return 0; /* successful termination */
+ }
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/parser.c
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/parser.c:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/parser.c Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,1543 ----
+ /**** parser.c ***************************************************/
+
+ #include "global.h"
+
+ extern void emit(int t, int tval, float rval);
+ extern void error(char *m);
+ extern int lexan(void);
+ extern int GlobalInsert(char s[], int tok, int type, int size, int function,
+ int functionlabel);
+ extern void CheckMain(void);
+ extern void AllBodsParsed(void);
+ extern void LocalReset(void);
+ extern int LocalInsert(char s[], int tok, int type, int size,int function,
+ int functionlabel);
+ extern void ParamInt(void);
+
+ int term(void);
+ void match(int t);
+ int expr(void);
+ void PushArrayCellAddr(void);
+ void DeclOrF(void);
+ void GlobalScopeAndInsert(int type);
+ void GlobalVarList(int type);
+ void exprTest(void);
+ void exprAssg(void);
+ void parseAStmt(void);
+ void parse(void);
+ int factor(void);
+ void ExprList(void);
+ void FunctionList(void);
+ void FunctionDef(int type);
+ void MoreParams(void);
+ void ParamList(void);
+ void DecList(void);
+ void VarList(int type);
+ void FstmtList(void);
+
+ int term(void)
+ {
+ int t; /* temporary holder of tokens */
+ int leftExpr, rightExpr;
+
+ leftExpr = factor();
+ if (ErrorFlag)
+ return -99;
+ while(lookahead == '*' || lookahead == '/')
+ {
+ t = lookahead;
+ match(lookahead);
+ if (ErrorFlag)
+ return -99;
+ rightExpr = factor();
+ if (ErrorFlag)
+ return -99;
+ if (leftExpr == rightExpr)
+ {
+ if (leftExpr)
+ {
+ if (t == '*')
+ emit(RMUL, 0, 0.0);
+ else
+ emit(RDIV, 0, 0.0);
+ leftExpr = 1;
+ }
+ else
+ {
+ if (t == '*')
+ emit(MUL, 0, 0.0);
+ else
+ emit(DIV, 0, 0.0);
+ leftExpr = 0;
+ }
+ }
+ else
+ {
+ if (leftExpr)
+ {
+ emit(FLOAT, 0, 0.0);
+ if (t == '*')
+ emit(RMUL, 0, 0.0);
+ else
+ emit(RDIV, 0, 0.0);
+ }
+ else
+ {
+ emit(NUM, 1, 0.0);
+ emit(IST, 0, 0.0);
+ emit(FLOAT, 0, 0.0);
+ emit(NUM, 1, 0.0);
+ emit(VAL, 0, 0.0);
+ if (t == '*')
+ emit(RMUL, 0, 0.0);
+ else
+ emit(RDIV, 0, 0.0);
+ }
+ leftExpr = 1;
+ } /* end else leftExpr == rightExpr */
+ }
+ if (FloatFlag)
+ return(1);
+ else
+ return (leftExpr);
+ }
+ /* ========================================================================= */
+
+ void match(int t) /* verify that t was the last token, then get next token */
+ /* token to verify as last found */
+ {
+ if (lookahead == t)
+ {
+ PreviousLookahead = lookahead;
+ PreviousTokenval = tokenval;
+ PreviousFtokenval = ftokenval;
+
+ lookahead = NextLookahead;
+ tokenval = NextTokenval;
+ ftokenval = NextFtokenval;
+
+ if (NextLookahead != DONE)
+ NextLookahead = lexan();
+ }
+ else
+ {
+ switch (lookahead)
+ {
+ case WHILE:
+ case IF:
+ error("Missing ; at end of previous statement");
+ break;
+ }
+ if (ErrorFlag)
+ return;
+ switch (t)
+ {
+ case ')':
+ error("Missing right parenthesis or illegal operator");
+ break;
+ default:
+ printf("\n In default of match lookahead = %d",lookahead);
+ error("syntax error");
+ break;
+ } /* end switch */
+ } /* else */
+ }
+
+ /* ------------------------------------------------------------------- */
+
+ int expr(void)
+ {
+ int t; /* temporary holder of tokens */
+ int leftExpr, rightExpr;
+
+ leftExpr = term();
+ if (ErrorFlag)
+ return -99;
+ while(lookahead == '+' || lookahead == '-')
+ {
+ t = lookahead;
+ match(lookahead);
+ if (ErrorFlag)
+ return -99;
+ rightExpr = term();
+ if (ErrorFlag)
+ return -99;
+ if (leftExpr == rightExpr)
+ {
+ if (leftExpr)
+ {
+ if (t == '+')
+ emit(RADD, 0, 0.0);
+ else
+ emit(RSUB, 0, 0.0);
+ leftExpr = 1;
+ }
+ else
+ {
+ if (t == '+')
+ emit(ADD, 0, 0.0);
+ else
+ emit(SUB, 0, 0.0);
+ leftExpr = 0;
+ }
+ }
+ else
+ {
+ if (leftExpr)
+ {
+ emit(FLOAT, 0, 0.0);
+ if (t == '+')
+ emit(RADD, 0, 0.0);
+ else
+ emit(RSUB, 0, 0.0);
+ }
+ else
+ {
+ emit(NUM, 1, 0.0);
+ emit(IST, 0, 0.0);
+ emit(FLOAT, 0, 0.0);
+ emit(NUM, 1, 0.0);
+ emit(VAL, 0, 0.0);
+ if (t == '+')
+ emit(RADD, 0, 0.0);
+ else
+ emit(RSUB, 0, 0.0);
+ }
+ leftExpr = 1;
+ } /* end else leftExpr == rightExpr */
+ }
+ if (FloatFlag)
+ return(1);
+ else
+ return(leftExpr);
+
+ }
+ /* ========================================================================= */
+
+
+
+ void PushArrayCellAddr(void) /* calculate address of array cell on stack */
+ {
+ int tempTokenval;
+
+ if ( !(ArrayParsed))
+ {
+ ArrayParsed = 1;
+ tempTokenval = tokenval;
+ if (LocalIndex)
+ emit(PUSH_LOC_VAR_ADDR, LocalIndex, 0.0);
+ else if (GlobalIndex)
+ emit(PUSH_GLO_VAR_ADDR, GlobalIndex, 0.0);
+ else
+ {
+ error("Compiler error, array lexeme not found in any table");
+ return;
+ }
+ match(ARRAY);
+ if (ErrorFlag)
+ return;
+ match('[');
+ if (ErrorFlag)
+ return;
+ FloatFlag = 0;
+ FloatFlag = expr();
+ if (ErrorFlag)
+ return;
+ if (lookahead != ']')
+ {
+ error("Missing ']'");
+ return;
+ }
+ if (FloatFlag)
+ {
+ error("Array index must be an integer value");
+ return;
+ }
+ emit(ADD, 0, 0.0);
+ lookahead = ARRAY;
+ tokenval = tempTokenval;
+ } /* end if !ArrayParsed */
+ else
+ ArrayParsed = 0;
+ } /* end PushArrayCellAddr */
+
+ /* ============================================================= */
+
+ void DeclOrF(void)
+ {
+ int type; /* stores type to call other functions with */
+ int temp;
+
+ Scope = 0; /* set scope to zero to mean global variables */
+
+ while(1)
+ {
+ if (lookahead == ID)
+ {
+ emit(BEGIN, 0, 0.0);
+ LabelCounter = 4; /* first 3 label used by emit BEGIN */
+ /* insert an implicit call to main into the symbol tabel */
+ temp = GlobalInsert ("main",ID,INT,0,1,1);
+ if (ErrorFlag)
+ return;
+ if (strcmp("main", lexbuf) == 0)
+ GlobalIndex = temp;
+ FunctionDef (INT);
+ if (ErrorFlag)
+ return;
+ return;
+ }
+ else if (lookahead == INT)
+ {
+ match (INT);
+ if (ErrorFlag)
+ return;
+ }
+ else
+ {
+ match (FLOAT);
+ if (ErrorFlag)
+ return;
+ }
+
+ if (NextLookahead == '(')
+ {
+ emit(BEGIN, 0, 0.0);
+ LabelCounter = 4; /* first 3 labels used by emit BAGIN */
+ /* insert an implicit call to main into the symbol tabel */
+ temp = GlobalInsert ("main",ID,INT,0,1,1);
+ if (ErrorFlag)
+ return;
+ if (strcmp("main", lexbuf) == 0)
+ GlobalIndex = temp;
+ FunctionDef(PreviousLookahead);
+ if (ErrorFlag)
+ return;
+ return;
+ }
+ else
+ {
+ type = PreviousLookahead;
+ GlobalScopeAndInsert (type);
+ if (ErrorFlag)
+ return;
+ GlobalVarList (type);
+ if (ErrorFlag)
+ return;
+ match(';');
+ if (ErrorFlag)
+ return;
+ } /* end if != '(' */
+ } /* end while(1) */
+ } /* end DeclOrF */
+
+ /* ============================================================= */
+
+ void GlobalScopeAndInsert(int type)
+ {
+ int tempGlobalIndex;
+
+ if (LocalIndex)
+ {
+ error("Compiler error! global lexeme found in local symbol table");
+ return;
+ } /* end LocalIndex */
+ else if (GlobalIndex)
+ {
+ if (GlobalTable[GlobalIndex].function)
+ {
+ error("Attempting to define a function name as an identifier");
+ return;
+ } /* end if */
+
+ else
+ {
+ error("Attempting to define a global variable twice");
+ return;
+ } /* end else */
+ } /* end else ifGlobalIndex */
+ else
+ {
+ if (NextLookahead == '[')
+ {
+ tempGlobalIndex = GlobalIndex = GlobalInsert(lexbuf, ARRAY, type,0,0,0);
+ if (ErrorFlag)
+ return;
+ match(ID);
+ if (ErrorFlag)
+ return;
+ match('[');
+ if (ErrorFlag)
+ return;
+ if (lookahead == NUM)
+ {
+ GlobalTable[tempGlobalIndex].size = tokenval;
+ match(NUM);
+ } /* end if (lookahead == num) */
+ else
+ {
+ error("Improperly formed array definition");
+ return;
+ } /* end else */
+ match(']');
+ if (ErrorFlag)
+ return;
+ emit(GLO_DECL, tempGlobalIndex, 0.0);
+ DecCount += GlobalTable[tempGlobalIndex].size;
+ } /* end if next == '[' */
+ else /* regular ID */
+ {
+ GlobalIndex = GlobalInsert(lexbuf, ID, type, 1, 0, 0);
+ if (ErrorFlag)
+ return;
+ emit(GLO_DECL, GlobalIndex, 0.0);
+ ++DecCount;
+ match(ID);
+ if (ErrorFlag)
+ return;
+ } /* end else */
+ } /* end else */
+ } /* end GlobalScopeAndInsert */
+
+ /* ============================================================= */
+
+ void GlobalVarList(int type)
+ {
+ while (lookahead == ',')
+ {
+ match(',');
+ if (ErrorFlag)
+ return;
+ if (lookahead == ID || lookahead == ARRAY)
+ {
+ GlobalScopeAndInsert (type);
+ if (ErrorFlag)
+ return;
+ } /* end if ID or ARRAY */
+ else
+ {
+ error("Expected an identifier or array");
+ return;
+ } /* end else */
+ } /* end while loop */
+ } /* end GlobalVarList */
+
+ /* ============================================================= */
+
+ void exprTest(void)
+ {
+ int tempTrue, tempDone;
+ int tempOperation;
+ int TempFloatFlag;
+
+ FloatFlag = 0;
+ FloatFlag = expr();
+ if (ErrorFlag)
+ return;
+
+ while ((lookahead == EQUAL) || (lookahead == NEQUAL))
+ {
+ tempOperation = lookahead;
+ if (lookahead == EQUAL)
+ match(EQUAL);
+ else
+ match(NEQUAL);
+ if (ErrorFlag)
+ return;
+ TempFloatFlag = FloatFlag;
+ if (TempFloatFlag)
+ {
+ FloatFlag = 0;
+ FloatFlag = expr();
+ if (!(FloatFlag))
+ { emit(FLOAT, 0, 0.0);
+ FloatFlag = 1;
+ }
+ }
+ else
+ {
+ FloatFlag = expr();
+ if (FloatFlag)
+ {
+ emit(NUM, 1, 0.0);
+ emit(IST, 0, 0.0);
+ emit(FLOAT, 0, 0.0);
+ emit(NUM, 1, 0.0);
+ emit(VAL, 0, 0.0);
+ }
+ }
+ if (ErrorFlag)
+ return;
+ emit(COMP, 0, 0.0);
+ tempTrue = LabelCounter;
+ ++LabelCounter;
+ tempDone = LabelCounter;
+ ++LabelCounter;
+ emit(PUSH_CODE_LABEL, tempTrue, 0.0);
+ if (tempOperation == EQUAL)
+ emit(BEQ, 0, 0.0);
+ else
+ emit(BNE, 0, 0.0);
+ emit(NUM, 0, 0.0);
+ emit(PUSH_CODE_LABEL, tempDone, 0.0);
+ emit(B, 0, 0.0);
+ emit(LABEL_CODE, tempTrue, 0.0);
+ emit(NUM, 1, 0.0);
+ emit(LABEL_CODE, tempDone, 0.0);
+ } /* end if */
+ } /* end exprTest */
+
+ /* ========================================================================= */
+
+ void exprAssg(void)
+ {
+ int tempLocalIndex, tempGlobalIndex; /* temp indices during recursion */
+
+ switch (lookahead)
+ {
+ case ARRAY:
+ tempLocalIndex = LocalIndex;
+ tempGlobalIndex = GlobalIndex;
+ PushArrayCellAddr();
+ if (ErrorFlag)
+ return;
+ if (NextLookahead == '=')
+ {
+ ArrayParsed = 0;
+ match(ARRAY);
+ if (ErrorFlag)
+ return;
+ match('=');
+ if (ErrorFlag)
+ return;
+ FloatFlag = 0;
+ exprAssg();
+ if (ErrorFlag)
+ return;
+ if (tempLocalIndex)
+ {
+ if ((LocalTable[tempLocalIndex].type == INT) && (FloatFlag))
+ {
+ emit(INT, 0, 0.0);
+ FloatFlag = 0;
+ }
+ else if ((LocalTable[tempLocalIndex].type == FLOAT) && ( !FloatFlag))
+ {
+ emit(FLOAT, 0, 0.0);
+ FloatFlag = 1;
+ }
+ emit(STHB ,0 ,0.0);
+ }
+ else if (tempGlobalIndex)
+ {
+ if ((GlobalTable[tempGlobalIndex].type == INT) && (FloatFlag))
+ {
+ emit(INT, 0, 0.0);
+ FloatFlag = 0;
+ }
+ else if((GlobalTable[tempGlobalIndex].type == FLOAT) && (!FloatFlag))
+ {
+ emit(FLOAT, 0, 0.0);
+ FloatFlag = 1;
+ }
+ emit(STH, 0, 0.0);
+ }
+ emit(SWAP, 0, 0.0);
+ emit(POP, 0, 0.0);
+ } /* end if NextLookahead == '=' */
+ else
+ {
+ exprTest();
+ if (ErrorFlag)
+ return;
+ } /* end else of if lookahead == '=' */
+ break;
+ case ID:
+ tempLocalIndex = LocalIndex;
+ tempGlobalIndex = GlobalIndex;
+
+ if (NextLookahead == '=')
+ {
+ if (LocalIndex)
+ emit(PUSH_LOC_VAR_ADDR, LocalIndex, 0.0);
+ else if (GlobalIndex)
+ emit(PUSH_GLO_VAR_ADDR, GlobalIndex, 0.0);
+ else
+ {
+ error("Compiler error, variable lexeme not found in any table");
+ return;
+ }
+ match(ID);
+ if (ErrorFlag)
+ return;
+ match('=');
+ if (ErrorFlag)
+ return;
+ FloatFlag = 0;
+ exprAssg();
+ if (ErrorFlag)
+ return;
+ if (tempLocalIndex)
+ {
+ if ((LocalTable[tempLocalIndex].type == INT) && (FloatFlag))
+ {
+ emit(INT, 0, 0.0);
+ FloatFlag = 0;
+ }
+ else if ((LocalTable[tempLocalIndex].type == FLOAT) && ( !FloatFlag))
+ {
+ emit(FLOAT, 0, 0.0);
+ FloatFlag = 1;
+ }
+ emit(STHB, 0, 0.0);
+ }
+ else if (tempGlobalIndex)
+ {
+ if ((GlobalTable[tempGlobalIndex].type == INT) && (FloatFlag))
+ {
+ emit(INT, 0, 0.0);
+ FloatFlag = 0;
+ }
+ else if((GlobalTable[tempGlobalIndex].type == FLOAT) && (!FloatFlag))
+ {
+ emit(FLOAT, 0, 0.0);
+ FloatFlag = 1;
+ }
+ emit(STH, 0, 0.0);
+ }
+ emit(SWAP, 0, 0.0);
+ emit(POP, 0, 0.0);
+ }
+ else
+ {
+ exprTest();
+ if (ErrorFlag)
+ return;
+ }
+ break;
+ default:
+ exprTest();
+ if (ErrorFlag)
+ return;
+ } /* end switch */
+ return;
+ } /* end exprAssg */
+
+ /* ========================================================================= */
+
+ void parseAStmt(void) /* parse a single statement */
+ {
+ int tempLabel1, tempLabel2;
+
+ switch (lookahead)
+ {
+ case WHILE:
+ match(WHILE);
+ if (ErrorFlag)
+ return;
+ tempLabel1 = LabelCounter;
+ ++LabelCounter;
+ tempLabel2 = LabelCounter;
+ ++LabelCounter;
+ emit(LABEL_CODE, tempLabel1, 0.0);
+ emit(PUSH_CODE_LABEL, tempLabel2, 0.0);
+ match('(');
+ if (ErrorFlag)
+ return;
+ FloatFlag = 0;
+ exprAssg();
+ if (ErrorFlag)
+ return;
+ match(')');
+ if (ErrorFlag)
+ return;
+ if (FloatFlag)
+ emit(RNUM, 0, 0.0); /* push 0.0 */
+ else
+ emit(NUM, 0 ,0.0); /* push 0 */
+ emit(COMP, 0, 0.0);
+ emit(BEQ, 0, 0.0);
+ parseAStmt();
+ if (ErrorFlag)
+ return;
+ emit(PUSH_CODE_LABEL, tempLabel1, 0.0);
+ emit(B, 0, 0.0);
+ emit(LABEL_CODE, tempLabel2, 0.0);
+ break;
+ case IF:
+ match(IF);
+ if (ErrorFlag)
+ return;
+ tempLabel1 = LabelCounter;
+ ++LabelCounter;
+ emit(PUSH_CODE_LABEL, tempLabel1, 0.0);
+ match('(');
+ if (ErrorFlag)
+ return;
+ FloatFlag = 0;
+ exprAssg();
+ if (ErrorFlag)
+ return;
+ match(')');
+ if (ErrorFlag)
+ return;
+ if (FloatFlag)
+ emit(RNUM, 0, 0.0); /* push 0.0 */
+ else
+ emit(NUM, 0, 0.0); /* push 0 */
+ emit(COMP, 0, 0.0);
+ emit(BEQ, 0, 0.0);
+ parseAStmt();
+ if (ErrorFlag)
+ return;
+ if (lookahead == ELSE)
+ {
+ match (ELSE);
+ if (ErrorFlag)
+ return;
+ tempLabel2 = LabelCounter;
+ ++LabelCounter;
+ emit(PUSH_CODE_LABEL, tempLabel2, 0.0);
+ emit(B, 0, 0.0);
+ emit(LABEL_CODE, tempLabel1, 0.0);
+ parseAStmt();
+ if (ErrorFlag)
+ return;
+ emit(LABEL_CODE, tempLabel2, 0.0);
+ } /* end if lookahead == ELSE */
+ else
+ {
+ emit(LABEL_CODE, tempLabel1, 0.0);
+ } /* end else lookahead == ELSE */
+ break;
+ case '{':
+ match('{');
+ do
+ {
+ parseAStmt();
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ } while ((lookahead != '}') && (lookahead != DONE));
+ match('}');
+ if (ErrorFlag)
+ return;
+ break;
+ case RETURN:
+ if (NextLookahead == ';')
+ {
+ match(RETURN);
+ if (ErrorFlag)
+ return;
+ match(';');
+ if (ErrorFlag)
+ return;
+ if (GlobalTable[FuncNameIndex].type == INT)
+ emit(NUM, 1, 0.0);
+ else
+ emit(RNUM, 0, 1.0);
+ } /* end if */
+ else
+ {
+ match(RETURN);
+ if (ErrorFlag)
+ return;
+ exprAssg();
+ if (ErrorFlag)
+ return;
+ match(';');
+ if (ErrorFlag)
+ return;
+ } /* end else */
+ emit(PUSH_CODE_LABEL, ReturnLabel, 0.0);
+ emit(B, 0, 0.0);
+ break;
+ default: /* assignment statement */
+ exprAssg();
+ if (ErrorFlag)
+ return;
+ emit(POP, 0, 0.0);
+ match(';');
+ return;
+ } /* end switch */
+ return;
+ } /* end parseAStmt */
+
+ /* ========================================================================= */
+
+ void parse(void) /* parse a list of functions */
+ {
+ /* prime NextLookahead */
+ lookahead = 1;
+ match (lookahead);
+ if (ErrorFlag)
+ ErrorFlag = 0;
+
+ /* prime lookahead */
+ lookahead = 1;
+ match (lookahead);
+ if (ErrorFlag)
+ ErrorFlag = 0;
+
+ DeclOrF(); /* parses the global var. declarations and the first function */
+ if (ErrorFlag)
+ return;
+ FunctionList();
+ if (ErrorFlag)
+ return;
+ CheckMain();
+ AllBodsParsed();
+ if (ErrorFlag)
+ return;
+ emit(END, 0, 0.0);
+ return;
+ } /* end parse */
+ /* ========================================================================= */
+
+
+ int factor(void)
+ {
+ int temp;
+ int tempLocalIndex, tempGlobalIndex;
+ char templexbuf[30];
+
+ switch(lookahead) {
+ case '(':
+ match('(');
+ if (ErrorFlag)
+ return -99;
+ switch (lookahead)
+ {
+ case INT:
+ match (INT);
+ if (ErrorFlag)
+ return -99;
+ match (')');
+ if (ErrorFlag)
+ return -99;
+
+ temp = factor();
+ if (ErrorFlag)
+ return -99;
+ if (temp)
+ emit(INT, 0, 0.0);
+ return(0);
+ break;
+
+ case FLOAT:
+ match (FLOAT);
+ if (ErrorFlag)
+ return -99;
+ match (')');
+ if (ErrorFlag)
+ return -99;
+
+ temp = factor();
+ if (ErrorFlag)
+ return -99;
+ if ( !temp)
+ emit(FLOAT, 0, 0.0);
+ return(1);
+ break;
+
+ default:
+ FloatFlag = 0;
+ exprAssg();
+ if (ErrorFlag)
+ return -99;
+ match(')');
+ if (ErrorFlag)
+ return -99;
+ if (FloatFlag)
+ return(1);
+ else
+ return(0);
+ break;
+ } /* end switch */
+ break;
+
+ case NUM:
+ emit(NUM, tokenval, 0.0);
+ match(NUM);
+ if (ErrorFlag)
+ return -99;
+ return(0); /* meaning false float flag */
+ break;
+
+ case RNUM:
+ emit(RNUM, tokenval, ftokenval);
+ match(RNUM);
+ if (ErrorFlag)
+ return -99;
+ return(1); /* meaning true float flag */
+ break;
+
+ case ID:
+ if (NextLookahead == '(')
+ {
+ /* function call */
+ tempLocalIndex = LocalIndex;
+ tempGlobalIndex = GlobalIndex;
+ strcpy (templexbuf, lexbuf);
+ match(ID);
+ if (ErrorFlag)
+ return -99;
+ match('(');
+ if (ErrorFlag)
+ return -99;
+ ExprList();
+ if (ErrorFlag)
+ return -99;
+ match(')');
+ if (ErrorFlag)
+ return -99;
+
+ /* prepare to branch to function */
+ CallReturnAddr = LabelCounter;
+ ++LabelCounter;
+ emit(PUSH_CODE_LABEL, CallReturnAddr, 0.0);
+ emit(PUSH_FRAMESIZE, FuncNameIndex, 0.0);
+ emit(IB, 0, 0.0);
+
+ /* branch to function */
+ if (tempLocalIndex)
+ {
+ error("Function called is to a local variable");
+ return -99;
+ } /* end LocalIndex */
+ else if (tempGlobalIndex)
+ {
+ if (!(GlobalTable[tempGlobalIndex].function))
+ {
+ error("Function called is to a global variable");
+ return -99;
+ } /* end if !function */
+ else
+ {
+ emit(PUSH_CODE_LABEL,GlobalTable[tempGlobalIndex].functionlabel,0.0);
+ } /* end else !function */
+ } /* end GlobalIndex */
+ else /* lexeme not in any symbol table */
+ {
+ tempGlobalIndex = GlobalInsert (templexbuf,ID,INT,0,1,LabelCounter);
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ ++LabelCounter;
+ emit(PUSH_CODE_LABEL, GlobalTable[tempGlobalIndex].functionlabel, 0.0);
+ } /* end insert function name */
+ emit(B, 0, 0.0);
+
+ /* return sequence */
+ emit(LABEL_CODE, CallReturnAddr, 0.0);
+ emit(PUSH_FRAMESIZE, FuncNameIndex, 0.0);
+ emit(DB, 0, 0.0);
+ if (GlobalTable[tempGlobalIndex].function < 0) /* function defined */
+ {
+ if (GlobalTable[tempGlobalIndex].type == INT)
+ return(0);
+ else
+ return(1);
+ }
+ else /* default return type of undefined function is INT */
+ return(0);
+ } /* end if NextLookahead == ')' */
+ else if ( !(LocalIndex || GlobalIndex))
+ {
+ error("attempting to use an undefined variable");
+ return -99;
+ }
+
+ if (LocalIndex)
+ emit(PUSH_LOC_VAR_VALUE, LocalIndex, 0.0);
+ else if (GlobalIndex)
+ emit(PUSH_GLO_VAR_VALUE, GlobalIndex, 0.0);
+ else
+ {
+ error("Compiler error, lexeme was not found in any table");
+ break;
+ }
+ tempLocalIndex = LocalIndex;
+ tempGlobalIndex = GlobalIndex;
+ match(ID);
+ if (ErrorFlag)
+ return -99;
+ if (tempLocalIndex)
+ {
+ if (LocalTable[tempLocalIndex].type == FLOAT)
+ return(1);
+ else
+ return(0);
+ }
+ else
+ {
+ if (GlobalTable[tempGlobalIndex].type == FLOAT)
+ return(1);
+ else
+ return(0);
+ }
+ break;
+ case ARRAY:
+ tempLocalIndex = LocalIndex;
+ tempGlobalIndex = GlobalIndex;
+ PushArrayCellAddr();
+ if (ErrorFlag)
+ return -99;
+ ArrayParsed = 0;
+ match(ARRAY);
+ if (ErrorFlag)
+ return -99;
+ if (tempLocalIndex)
+ emit(VALB, 0, 0.0);
+ else if (tempGlobalIndex)
+ emit(VAL, 0, 0.0);
+ else
+ {
+ error("using an undefined array referance");
+ return -99;
+ }
+
+ if (tempLocalIndex)
+ {
+ if (LocalTable[tempLocalIndex].type == FLOAT)
+ return(1);
+ else
+ return(0);
+ }
+ else
+ {
+ if (GlobalTable[tempGlobalIndex].type == FLOAT)
+ return(1);
+ else
+ return(0);
+ }
+ break;
+ case '-':
+ match('-');
+ if (ErrorFlag)
+ return -99;
+ temp=factor();
+ if (ErrorFlag)
+ return -99;
+ if (temp)
+ emit(RNEG, 0, 0.0);
+ else
+ emit(NEG, 0, 0.0);
+ return(temp);
+ break;
+ case READF:
+ match(READF);
+ if (ErrorFlag)
+ return -99;
+ match ('(');
+ if (ErrorFlag)
+ return -99;
+ match (')');
+ if (ErrorFlag)
+ return -99;
+ emit(READ, 0, 0.0);
+ emit(FLOAT, 0, 0.0);
+ return(1);
+ break;
+ case READI:
+ match(READI);
+ if (ErrorFlag)
+ return -99;
+ match('(');
+ if (ErrorFlag)
+ return -99;
+ match(')');
+ if (ErrorFlag)
+ return -99;
+ emit(READ, 0, 0.0);
+ emit(INT, 0, 0.0);
+ return(0);
+ break;
+ case WRITEF:
+ match (WRITEF);
+ if (ErrorFlag)
+ return -99;
+ match('(');
+ if (ErrorFlag)
+ return -99;
+ FloatFlag = 0;
+ exprAssg();
+ if (ErrorFlag)
+ return -99;
+ if ( !(FloatFlag))
+ {
+ emit(FLOAT, 0, 0.0);
+ FloatFlag = 1;
+ }
+ match(')');
+ if (ErrorFlag)
+ return -99;
+ emit(WRITEF, 0, 0.0);
+ emit(RNUM, 1, 1.0);
+ return(1);
+ break;
+ case WRITEI:
+ match(WRITEI);
+ if (ErrorFlag)
+ return -99;
+ match('(');
+ if (ErrorFlag)
+ return -99;
+ FloatFlag = 0;
+ exprAssg();
+ if (ErrorFlag)
+ return -99;
+ if (FloatFlag)
+ {
+ emit(INT, 0, 0.0);
+ FloatFlag = 0;
+ }
+ match(')');
+ if (ErrorFlag)
+ return -99;
+ emit(WRITEI, 0, 0.0);
+ emit(NUM, 1, 0.0);
+ return(0);
+ break;
+ case WRITELN:
+ match(WRITELN);
+ if (ErrorFlag)
+ return -99;
+ match('(');
+ if (ErrorFlag)
+ return -99;
+ match(')');
+ if (ErrorFlag)
+ return -99;
+ emit(WRITELN, 0, 0.0);
+ emit(NUM, 1, 0.0);
+ return(0);
+ break;
+ default:
+ /* error used to be called from here */
+ error("Illegal operator");
+ return(0);
+ }
+ return -99;
+ }
+
+ /* ------------------------------------------------------------------ */
+
+ void ExprList(void)
+ {
+ if (lookahead != ')')
+ {
+ exprAssg();
+ if (ErrorFlag)
+ return;
+
+ while (lookahead == ',')
+ {
+ match(',');
+ if (ErrorFlag)
+ return;
+ exprAssg();
+ if (ErrorFlag)
+ return;
+ } /* end while */
+ } /* end if ) */
+ } /* end ExprList */
+
+ /* ========================================================================= */
+
+
+ void FunctionList(void)
+ {
+ do
+ {
+ ErrorFlag = 0;
+ FloatFlag = 0;
+ LocalReset();
+ switch(lookahead)
+ {
+ case ID:
+ FunctionDef(INT);
+ if (ErrorFlag)
+ return;
+ break;
+ case INT:
+ match(INT);
+ if (ErrorFlag)
+ return;
+ FunctionDef(INT);
+ if (ErrorFlag)
+ return;
+ break;
+ case FLOAT:
+ match(FLOAT);
+ if (ErrorFlag)
+ return;
+ FunctionDef(FLOAT);
+ if (ErrorFlag)
+ return;
+ case DONE:
+ break;
+ default:
+ error("Unexpected token found");
+ return;
+ break;
+ } /* end for switch statement */
+ }
+ while (lookahead != DONE);
+ } /* end FunctionList */
+
+ /* ----------------------------------------------------------------------- */
+
+ void FunctionDef(int type)
+ {
+ offset = 1;
+ if (lookahead != ID)
+ {
+ error("Invalid function defintion structure. Expected ID");
+ return;
+ } /* end if lookahead */
+
+ if (LocalIndex)
+ {
+ error("Compiler error, function name in local symbol table");
+ return;
+ } /* end if LocalIndex */
+ else if (GlobalIndex)
+ {
+ if (!(GlobalTable[GlobalIndex].function))
+ {
+ error("Attempting to redefine a global variable as a function name");
+ return;
+ } /* end if GlobalTable */
+ else if (GlobalTable[GlobalIndex].function < 0)
+ {
+ error("Redefining a function.");
+ return;
+ } /* end else if GlobalTable */
+ else if (GlobalTable[GlobalIndex].function > 0)
+ /* function is a postive value, already called not defined */
+ {
+ GlobalTable[GlobalIndex].type = type;
+ FuncNameIndex = GlobalIndex;
+ GlobalTable[GlobalIndex].function = -1;
+ } /* end else if function > 0 */
+ else
+ {
+ error("Compiler error, functiondef is confused");
+ return;
+ }
+ } /* end else if (GlobalIndex) */
+ else /* ID not in any tabel */
+ {
+ GlobalIndex = GlobalInsert (lexbuf,ID,type,0,-1,LabelCounter);
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ FuncNameIndex = GlobalIndex;
+ ++LabelCounter;
+ } /* end else */
+
+ Scope = GlobalTable[GlobalIndex].functionlabel;
+
+ /* label function */
+ emit(LABEL_FUNC, GlobalIndex, 0.0);
+ emit(LABEL_CODE, Scope, 0.0);
+
+ /* store return address */
+ emit(STORE_RA, FuncNameIndex, 0.0);
+
+ ReturnLabel = LabelCounter;
+ ++LabelCounter;
+
+ match(ID);
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ match('(');
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ if ( strcmp(GlobalTable[FuncNameIndex].lexptr, "main") == 0)
+ {
+ if (lookahead == ')')
+ {
+ match(')');
+ if (ErrorFlag)
+ return;
+ } /* end if lookahead == ')' */
+ else
+ {
+ error("Function main cannot have parameters.");
+ return;
+ } /* end else */
+ } /* end if function is main */
+ else
+ {
+ ParamList();
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ match(')');
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ } /* end else (function is something other than main) */
+ DecList();
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ emit(SAVE_FRAMESIZE, FuncNameIndex, 0.0);
+ FstmtList();
+ if (ErrorFlag)
+ return;
+ if (GlobalTable[FuncNameIndex].type == INT)
+ emit(NUM, 1, 0.0);
+ else
+ emit(RNUM, 0, 1.0);
+ emit(LABEL_CODE, ReturnLabel, 0.0);
+ emit(PUSH_RA, FuncNameIndex, 0.0);
+ emit(B, 0, 0.0);
+ } /* end function Def */
+
+ /* ---------------------------------------------------------------------- */
+
+ void MoreParams(void)
+ {
+ int tempLocalIndex;
+
+ if (lookahead == ',')
+ {
+ match(',');
+ if (ErrorFlag)
+ return;
+ if (lookahead != ID)
+ {
+ error("Improperly formed parameter list");
+ return;
+ }
+ if (GlobalIndex)
+ {
+
+ if (GlobalTable[GlobalIndex].function)
+ {
+ error("Cannot use a function name as parameter");
+ return;
+ } /* end if GlobalTable */
+ } /* GlobalIndex */
+
+ if (LocalIndex)
+ {
+ error("Trying to use a prameter twice");
+ return;
+ } /* end else if (LocalIndex) */
+
+ else
+ {
+ tempLocalIndex = LocalIndex = LocalInsert (lexbuf,ID,PARAM,1,0,0);
+ if (ErrorFlag)
+ return;
+ } /* end else */
+ match (ID);
+ if (ErrorFlag)
+ return;
+ MoreParams();
+ if (ErrorFlag)
+ return;
+ emit(LOC_DECL, tempLocalIndex, 0.0);
+ emit(PUSH_LOC_VAR_ADDR, tempLocalIndex, 0.0);
+ emit(ISTB, 0, 0.0);
+ ++offset;
+ } /* end if lookahead == ',' */
+ } /* end MoreParams() */
+
+
+ /* ---------------------------------------------------------------------- */
+
+ void ParamList(void)
+ {
+ int tempLocalIndex;
+
+ if (lookahead == ID)
+ {
+ if (GlobalIndex)
+ {
+
+ if (GlobalTable[GlobalIndex].function)
+ {
+ error("Cannot use a function name as parameter");
+ return;
+ } /* end if GlobalTable */
+ } /* GlobalIndex */
+
+ if (LocalIndex)
+ {
+ error("Trying to use a parameter twice");
+ return;
+ } /* end else if (LocalIndex) */
+
+ else
+ {
+ tempLocalIndex = LocalIndex = LocalInsert (lexbuf,ID,PARAM,1,0,0);
+ if (ErrorFlag)
+ return;
+ } /* end else */
+ match (ID);
+ if (ErrorFlag)
+ return;
+ MoreParams();
+ if (ErrorFlag)
+ return;
+ emit(LOC_DECL, tempLocalIndex, 0.0);
+ emit(PUSH_LOC_VAR_ADDR, tempLocalIndex, 0.0);
+ emit(ISTB, 0, 0.0);
+ ++offset;
+ } /* end if lookahead == ID */
+ } /* end paramlist() */
+
+ /* ------------------------------------------------------------------ */
+
+ void DecList(void)
+ {
+ while ((lookahead == INT) || (lookahead == FLOAT))
+ {
+ match (lookahead); /* obviously must be INT or FLOAT */
+ if (ErrorFlag)
+ return;
+ VarList (PreviousLookahead);
+ if (ErrorFlag)
+ return;
+ } /* end while ((lookahead == INT) || (loookahead == FLOAT)) */
+ ParamInt();
+ } /* end function DecList */
+
+ /* --------------------------------------------------------------------- */
+
+ void VarList(int type)
+ {
+ int tempLocalIndex;
+
+ do
+ {
+ if (GlobalIndex)
+ {
+
+ if (GlobalTable[GlobalIndex].function)
+ {
+ error ("Cannot use a function name as a parameter");
+ return;
+ } /* end if GlogalTable */
+ } /* end if (GlobalIndex) */
+
+ if (LocalIndex)
+ {
+
+ if (LocalTable[LocalIndex].type == PARAM)
+ {
+ if (NextLookahead == '[')
+ error ("Cannot pass an array as a parameter");
+ else
+ {
+ LocalTable[LocalIndex].type = type;
+ match(ID);
+ if (ErrorFlag)
+ return;
+ } /* end else */
+ } /* end if LocalTable[LocalIndex] */
+
+ else
+ {
+ error ("Trying to redefine a variable");
+ return;
+ } /* end else */
+ } /* end if LocalIndex */
+
+ else /* variable is not entered as a local so insert */
+ {
+ if (NextLookahead == '[')
+ {
+ tempLocalIndex = LocalIndex = LocalInsert (lexbuf,ARRAY,type,0,0,0);
+ if (ErrorFlag)
+ return;
+ lookahead = ARRAY;
+ match (ARRAY);
+ if (ErrorFlag)
+ return;
+ match ('[');
+ if (ErrorFlag)
+ return;
+ if (lookahead == NUM)
+ {
+ LocalTable[tempLocalIndex].size = tokenval;
+ if (ErrorFlag)
+ return;
+ }
+ else
+ {
+ error("Array declarations requires an integer within []");
+ return;
+ }
+ match (NUM);
+ if (ErrorFlag)
+ return;
+ match (']');
+ if (ErrorFlag)
+ return;
+ emit(LOC_DECL, tempLocalIndex, 0.0);
+ offset += LocalTable[tempLocalIndex].size;
+ } /* end if nextlook */
+
+ else
+ {
+ LocalIndex = LocalInsert(lexbuf,ID,type,1,0,0);
+ if (ErrorFlag)
+ return;
+ emit(LOC_DECL, LocalIndex, 0.0);
+ ++offset;
+ match (ID);
+ if (ErrorFlag)
+ return;
+ } /* end else */
+ } /* end else */
+
+ switch (lookahead)
+ {
+ case ',':
+ match (',');
+ if (ErrorFlag)
+ return;
+ break;
+ case ';':
+ match (';');
+ if (ErrorFlag)
+ return;
+ return;
+ break;
+ default:
+ error ("badly formed declarations");
+ return;
+ } /* end switch (lookahead) */
+ } while ((lookahead == ID) || (lookahead == ARRAY));
+ error("can only use ID or ARRAAY");
+ return;
+ } /* end function VarList */
+
+ /* ------------------------------------------------------------------- */
+
+ void FstmtList(void)
+ {
+ match ('{');
+ if (ErrorFlag)
+ ErrorFlag = 0;
+
+ do
+ {
+ parseAStmt();
+ if (ErrorFlag)
+ ErrorFlag = 0;
+ }
+ while ((lookahead != '}') && (lookahead != DONE));
+
+ if (lookahead == DONE)
+ {
+ error("Unexpected end of file");
+ return;
+ } /* end if */
+ else
+ {
+ match ('}');
+ if (ErrorFlag)
+ return;
+ } /* end else */
+ } /* end function FstmtList */
Index: llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/symbol.c
diff -c /dev/null llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/symbol.c:1.1
*** /dev/null Tue Oct 5 14:24:17 2004
--- llvm-test/MultiSource/Benchmarks/Prolangs-C/compiler/symbol.c Tue Oct 5 14:24:06 2004
***************
*** 0 ****
--- 1,196 ----
+ /**** symbol.c *****************************************************/
+
+ #include "global.h"
+
+ extern void error(char *m);
+ extern void emit(int t, int tval, float rval);
+
+ char Glexemes[STRMAX]; /* char array to hold global lexemes */
+ char Llexemes[STRMAX]; /* char array to hold local lexemes */
+ /* struct entry LocalTable[SYMMAX]; define the size of the local table */
+ /* struct entry GlobalTable[SYMMAX]; define the size of the global table */
+ int Glastchar = - 1; /* last used position in Glexemes */
+ int Llastchar = - 1; /* last used position in Llexemes */
+ int Glastentry = 0; /* last used position in global table */
+ int Llastentry = 0; /* last usedentry in local table */
+
+ /* ---------------------------------------------------------------- */
+
+ void OutputLocal(void) /* function used for testing only */
+ {
+ int i; /* temporary index */
+
+ printf("\n\nLOCAL SYMBOL TABLE\n------------------\n");
+ for (i = 0; i <= Llastentry; i++)
+ {
+ printf("%s\t%d\t%d\t%d\t%d\t%d\n", LocalTable[i].lexptr,
+ LocalTable[i].token,
+ LocalTable[i].type,
+ LocalTable[i].size,
+ LocalTable[i].function,
+ LocalTable[i].functionlabel);
+ } /* end for */
+ printf("\n\n");
+ } /* end OutputLocal */
+
+ /* ----------------------------------------------------------------- */
+
+ void OutputGlobal(void) /* function used for testing only */
+ {
+ int i; /* temporary index */
+
+ printf("\n\nGLOBAL SYMBOL TABLE\n-------------------\n");
+ for (i = 0; i <= Glastentry; i++)
+ {
+ printf("%s\t%d\t%d\t%d\t%d\t%d\n",
+ GlobalTable[i].lexptr,
+ GlobalTable[i].token,
+ GlobalTable[i].type,
+ GlobalTable[i].size,
+ GlobalTable[i].function,
+ GlobalTable[i].functionlabel);
+ } /* end for */
+ printf("\n\n");
+ } /* end OutputGlobal */
+
+ /* ----------------------------------------------------------------- */
+
+
+ int GlobalLookup(char s[]) /* returns position of entry for s */
+ {
+ int p;
+ for (p = Glastentry; p > 0; p = p - 1)
+ if (strcmp(GlobalTable[p].lexptr, s) == 0)
+ return p;
+ return 0;
+ }
+
+ /* ----------------------------------------------------------------- */
+
+ int LocalLookup(char s[]) /* returns position of entry for s */
+ {
+ int p;
+ for (p = Llastentry; p > 0; p = p - 1)
+ if (strcmp(LocalTable[p].lexptr, s) == 0)
+ return p;
+ return 0;
+ }
+
+ /* ----------------------------------------------------------------- */
+
+ int GlobalInsert(char s[], int tok, int type, int size, int function, int functionlabel)
+
+ /* returns position of entry for s */
+ {
+ int len;
+
+ len = strlen(s); /* strlen computes length of s */
+ if (Glastentry + 1 >= SYMMAX)
+ {
+ error("symbol table full");
+ return (0);
+ }
+ if (Glastchar + len + 1 >= STRMAX)
+ {
+ error("lexemes array full");
+ return (0);
+ }
+ Glastentry = Glastentry + 1;
+ GlobalTable[Glastentry].token = tok;
+ GlobalTable[Glastentry].lexptr = &Glexemes[Glastchar + 1];
+ Glastchar = Glastchar + len + 1;
+ (void) strcpy(GlobalTable[Glastentry].lexptr, s);
+ GlobalTable[Glastentry].type = type;
+ GlobalTable[Glastentry].size = size;
+ GlobalTable[Glastentry].function = function;
+ GlobalTable[Glastentry].functionlabel = functionlabel;
+ return Glastentry;
+ }
+
+ /* ----------------------------------------------------------------- */
+
+ int LocalInsert(char s[], int tok, int type, int size,int function,int functionlabel)
+
+ /* returns position of entry for s */
+
+ {
+ int len;
+
+ len = strlen(s); /* strlen computes length of s */
+ if (Llastentry + 1 >= SYMMAX)
+ {
+ error("symbol table full");
+ return (0);
+ }
+ if (Llastchar + len + 1 >= STRMAX)
+ {
+ error("lexemes array full");
+ return (0);
+ }
+ Llastentry = Llastentry + 1;
+ LocalTable[Llastentry].token = tok;
+ LocalTable[Llastentry].lexptr = &Llexemes[Llastchar + 1];
+ Llastchar = Llastchar + len + 1;
+ (void) strcpy(LocalTable[Llastentry].lexptr, s);
+ LocalTable[Llastentry].type = type;
+ LocalTable[Llastentry].size = size;
+ LocalTable[Llastentry].function = function;
+ LocalTable[Llastentry].functionlabel = functionlabel;
+ return Llastentry;
+ }
+
+ /* ------------------------------------------------------------------ */
+
+ void ParamInt(void)
+ /* This function changes all PARAM fields to INT in local table */
+
+ {
+ int p;
+
+ for (p = Llastentry; p > 0; p = p - 1)
+ if (LocalTable[p].type == PARAM)
+ LocalTable[p].type = INT;
+ } /* end ParamInt */
+
+
+ /* ------------------------------------------------------------------ */
+
+ void LocalReset(void) /* this function effectively erases the local symbol table */
+ {
+ Llastchar = - 1;
+ Llastentry = 0;
+ } /* end LocalReset */
+
+ /* ------------------------------------------------------------------ */
+
+ void AllBodsParsed(void)
+ {
+ int p;
+ int tmpflag = 0;
+
+ for (p = Glastentry; p > 0; --p)
+ {
+ if (GlobalTable[p].function > 0)
+ {
+ emit(NOTDEC, p, 0.0);
+ tmpflag = 1;
+ } /* end if statement */
+ } /* end for loop */
+ ErrorFlag = tmpflag;
+ } /* end function */
+
+ /* ----------------------------------------------------------- */
+
+ void CheckMain(void)
+ {
+ int temp;
+
+ temp = GlobalLookup("main");
+
+ if (GlobalTable[temp].function > 0)
+ {
+ error("Main never declared");
+ GlobalTable[temp].function = -1;
+ }
+ ErrorFlag = 0; /* will get set again in parse's call to AllBodsParsed */
+ } /* end check main */
More information about the llvm-commits
mailing list