C   115
compiler.h
Guest on 26th July 2022 01:07:05 AM


  1. /*
  2.  *  Ace: A Compiler for the Ace language.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <stdarg.h>
  10.  
  11. /*
  12.  *  Types:
  13.  */
  14.  
  15.   typedef struct Token * Token;
  16.   struct Token {        // one parser token
  17.         Token * next;   // next in hash table
  18.         long    line;   // line number, 0 for keywords
  19.         int     kind;   // EOF, ERROR, INT, INTVAL, EQUALS, etc
  20.         long    num;    // integer value associated with this token
  21.         double  real;   // floating-point value
  22.         char *  name;   // string value (malloc'd)
  23.  };
  24.  
  25.   typedef struct Register * Reg;
  26.   struct Register {     // register usage
  27.         int     pos;            // 0 to 63
  28.         int     refcount;       // 0 if currently unused, else > 0
  29.         int     used;           // set to true if ever used
  30.         int     kind;           // what kind of object is referred to
  31.         int     size;           // size of the object referrred to
  32.   };
  33.  
  34.   typedef struct CodeLine * CodeLine;
  35.   struct CodeLine {     // one line of generated code
  36.         CodeLine next;  // for linked list
  37.         CodeLine prev;  // for linked list
  38.         char *  line;   // line of generated code
  39.   };
  40.  
  41.   typedef struct CodeList * CodeList;
  42.   struct CodeList {
  43.         int      size;  // number of lines of code
  44.         CodeLine head;  // first line of code
  45.   };
  46.  
  47.   typedef struct Var * Var;
  48.   struct Var {          // a variable or constant
  49.         Token   type;   // INT, REAL, STRING, etc
  50.         char *  name;   // name of local or global variable
  51.         Token   value;  // initial value or ERROR if none
  52.         int     used;   // has it been used in the current func?
  53.         int     constant; // is it a constant?
  54.         Reg     reg;    // register to use for this variable
  55.   };
  56.  
  57.   typedef struct LValue * LValue;
  58.   typedef struct RValue * RValue;
  59.  
  60.   typedef struct Expr * Expr;
  61.   struct Expr {         // an expression
  62.         int     kind;           // what is the kind of this expr?
  63.         Reg     reg;            // where is the result of this expr?
  64.         RValue  rvalue;         // leaf variable or value
  65.         int     op;             // PLUS, MINUS, LT, EQUALITY, LSHIFT etc
  66.         Expr    left;           // left-hand sub-expression
  67.         Expr    right;          // right-hand sub-expression
  68.   };
  69.  
  70.   typedef struct Stmt * Stmt;
  71.   struct Stmt {         // a code statement
  72.         int     kind;           // 0 for compound stmt, or WHILE, IF, EQUALS etc
  73.         LValue  lvalue;         // left-hand side in assignment
  74.         Expr    expr;           //   right-hand side in assignment
  75.         RValue  rvalue;         // function-call statement
  76.         int     stmt_count;     // number of sub-statements
  77.         Stmt *  stmts;          // sub-statements list
  78.   };
  79.  
  80.   typedef struct Func * Func;
  81.   struct Func {         // a code function
  82.         Var     return_var;     // stores return type and register use
  83.         char *  name;           // name of function
  84.         int     arg_count;      // argument variables list
  85.         Var *   args;
  86.         int     local_count;    // local variables list
  87.         Var *   locals;
  88.         int     global_count;   // global imports list
  89.         Var *   globals;
  90.         Stmt    stmt;           // top statement contains all sub-statements
  91.         CodeList prolog;        // list of prolog code (save registers)
  92.         CodeList code;          // list of generated code (function body)
  93.         CodeList epilog;        // list of epilog code (restore registers)
  94.         int     used;           // is this function ever called?
  95.         int     caller;         // does this function ever call another?
  96.   };
  97.  
  98.   struct LValue {       // a left-hand operand in assignment
  99.         Reg     reg;            // register for result
  100.         char *  name;           // name of var
  101.         Var     var;            // variable used
  102.         Expr    index;          //   array index or NULL
  103.   };
  104.  
  105.   struct RValue {       // a right-hand operand in assignment
  106.         Reg     reg;            // register for result
  107.         char *  name;           // name of var or func
  108.         Var     var;            // variable used
  109.         Expr    index;          // array index or NULL
  110.         Func    fn;             // function called
  111.         int     arg_count;      //   number of arguments to function call
  112.         Expr *  args;           //   arguments to function call
  113.   };
  114.  
  115.   typedef struct Program * Program;
  116.   struct Program {      // a code program
  117.         FILE *  file;
  118.         int     global_count;   // global variable list
  119.         Var *   globals;
  120.         int     func_count;     // function list
  121.         Func *  funcs;
  122.         CodeList code;          // list of global code
  123.   };
  124.  
  125. /*
  126.  *  Data for the compiler:
  127.  */
  128.   #define MAXTOKEN 256  // makimum token length
  129.  
  130.   enum TokenKind {
  131.         ERROR=0, NEWLINE, IDENT,
  132.         INT, REAL, STRING, INTVAL, REALVAL, STRINGVAL,
  133.         LENGTH, RETURN, EXIT, READ, PRINT,
  134.         WHILE, IF, ELSE,
  135.         AND, OR, NOT, COMMENT,
  136.         LSHIFT, RSHIFT, LPARENTH, RPARENTH,
  137.         LBRACE, RBRACE, LBRACKET, RBRACKET,
  138.         EQUALITY, INEQUALITY, LTEQ, GTEQ, LT, GT, EQUALS,
  139.         COMMA, SEMICOLON, STAR, SLASH, MOD, PLUS, MINUS,
  140.         ABS, BITWISE_AND, BITWISE_OR, BITWISE_XOR, BITWISE_NOT,
  141.         CAT, IND, INS,
  142.         BACKSLASH, QUOTE, DOT,
  143.   };
  144.  
  145.   extern char * TokenNames[];
  146.  
  147.   extern char * InstructNames[];
  148.  
  149.   Token getToken(Program p);
  150.  
  151. /*
  152.  *  Global variables:
  153.  */
  154.   extern long line_number;              // current input line number
  155.   extern long label_number;             // for generated labels
  156.   extern int reg_number;                // register allocations
  157.  
  158. /*
  159.  *  Register variables and constants:
  160.  */
  161.   #define NREGS    64   /* number of registers */
  162.   #define SP       63   /* stack pointer for saving registers */
  163.   #define FP       62   /* frame pointer for pointing to locals */
  164.   #define MAXSTACK 1000 /* maximum stack size */
  165.  
  166.   extern struct Register registers[NREGS];      // register-in-use table

Raw Paste

Login or Register to edit or fork this paste. It's free.