- /*
- * Ace: A Compiler for the Ace language.
- */
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdarg.h>
- /*
- * Types:
- */
- typedef struct Token * Token;
- struct Token { // one parser token
- Token * next; // next in hash table
- long line; // line number, 0 for keywords
- int kind; // EOF, ERROR, INT, INTVAL, EQUALS, etc
- long num; // integer value associated with this token
- double real; // floating-point value
- char * name; // string value (malloc'd)
- };
- typedef struct Register * Reg;
- struct Register { // register usage
- int pos; // 0 to 63
- int refcount; // 0 if currently unused, else > 0
- int used; // set to true if ever used
- int kind; // what kind of object is referred to
- int size; // size of the object referrred to
- };
- typedef struct CodeLine * CodeLine;
- struct CodeLine { // one line of generated code
- CodeLine next; // for linked list
- CodeLine prev; // for linked list
- char * line; // line of generated code
- };
- typedef struct CodeList * CodeList;
- struct CodeList {
- int size; // number of lines of code
- CodeLine head; // first line of code
- };
- typedef struct Var * Var;
- struct Var { // a variable or constant
- Token type; // INT, REAL, STRING, etc
- char * name; // name of local or global variable
- Token value; // initial value or ERROR if none
- int used; // has it been used in the current func?
- int constant; // is it a constant?
- Reg reg; // register to use for this variable
- };
- typedef struct LValue * LValue;
- typedef struct RValue * RValue;
- typedef struct Expr * Expr;
- struct Expr { // an expression
- int kind; // what is the kind of this expr?
- Reg reg; // where is the result of this expr?
- RValue rvalue; // leaf variable or value
- int op; // PLUS, MINUS, LT, EQUALITY, LSHIFT etc
- Expr left; // left-hand sub-expression
- Expr right; // right-hand sub-expression
- };
- typedef struct Stmt * Stmt;
- struct Stmt { // a code statement
- int kind; // 0 for compound stmt, or WHILE, IF, EQUALS etc
- LValue lvalue; // left-hand side in assignment
- Expr expr; // right-hand side in assignment
- RValue rvalue; // function-call statement
- int stmt_count; // number of sub-statements
- Stmt * stmts; // sub-statements list
- };
- typedef struct Func * Func;
- struct Func { // a code function
- Var return_var; // stores return type and register use
- char * name; // name of function
- int arg_count; // argument variables list
- Var * args;
- int local_count; // local variables list
- Var * locals;
- int global_count; // global imports list
- Var * globals;
- Stmt stmt; // top statement contains all sub-statements
- CodeList prolog; // list of prolog code (save registers)
- CodeList code; // list of generated code (function body)
- CodeList epilog; // list of epilog code (restore registers)
- int used; // is this function ever called?
- int caller; // does this function ever call another?
- };
- struct LValue { // a left-hand operand in assignment
- Reg reg; // register for result
- char * name; // name of var
- Var var; // variable used
- Expr index; // array index or NULL
- };
- struct RValue { // a right-hand operand in assignment
- Reg reg; // register for result
- char * name; // name of var or func
- Var var; // variable used
- Expr index; // array index or NULL
- Func fn; // function called
- int arg_count; // number of arguments to function call
- Expr * args; // arguments to function call
- };
- typedef struct Program * Program;
- struct Program { // a code program
- FILE * file;
- int global_count; // global variable list
- Var * globals;
- int func_count; // function list
- Func * funcs;
- CodeList code; // list of global code
- };
- /*
- * Data for the compiler:
- */
- #define MAXTOKEN 256 // makimum token length
- enum TokenKind {
- ERROR=0, NEWLINE, IDENT,
- INT, REAL, STRING, INTVAL, REALVAL, STRINGVAL,
- LENGTH, RETURN, EXIT, READ, PRINT,
- WHILE, IF, ELSE,
- AND, OR, NOT, COMMENT,
- LSHIFT, RSHIFT, LPARENTH, RPARENTH,
- LBRACE, RBRACE, LBRACKET, RBRACKET,
- EQUALITY, INEQUALITY, LTEQ, GTEQ, LT, GT, EQUALS,
- COMMA, SEMICOLON, STAR, SLASH, MOD, PLUS, MINUS,
- ABS, BITWISE_AND, BITWISE_OR, BITWISE_XOR, BITWISE_NOT,
- CAT, IND, INS,
- BACKSLASH, QUOTE, DOT,
- };
- extern char * TokenNames[];
- extern char * InstructNames[];
- Token getToken(Program p);
- /*
- * Global variables:
- */
- extern long line_number; // current input line number
- extern long label_number; // for generated labels
- extern int reg_number; // register allocations
- /*
- * Register variables and constants:
- */
- #define NREGS 64 /* number of registers */
- #define SP 63 /* stack pointer for saving registers */
- #define FP 62 /* frame pointer for pointing to locals */
- #define MAXSTACK 1000 /* maximum stack size */
- extern struct Register registers[NREGS]; // register-in-use table
Raw Paste