home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / version-1-0 / BlitzSrc / kpl / main.h < prev    next >
C/C++ Source or Header  |  2006-09-25  |  46KB  |  1,200 lines

  1. // main.h  --  Header File
  2. //
  3. // KPL Compiler
  4. //
  5. // Copyright 2002-2006, Harry H. Porter III
  6. //
  7. // This file may be freely copied, modified and compiled, on the sole
  8. // condition that if you modify it...
  9. //   (1) Your name and the date of modification is added to this comment
  10. //       under "Modifications by", and
  11. //   (2) Your name and the date of modification is added to the printHelp()
  12. //       routine in file "main.cc" under "Modifications by".
  13. //
  14. // Original Author:
  15. //   06/15/02 - Harry H. Porter III
  16. //
  17. // Modifcations by:
  18. //   03/15/06 - Harry H. Porter III
  19. //
  20.  
  21.  
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. #include <float.h>
  28. #include <math.h>
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31.  
  32. template <class Key, class Value> class Mapping;
  33. template <class Key, class Value> class Bucket;
  34.  
  35. union TokenValue;
  36. struct Token;
  37.  
  38. struct Offset;
  39. struct AbstractStack;
  40.  
  41. class AstNode;
  42. class Header;
  43. class MethOrFunction;
  44. class Abstract;
  45. class Code;
  46. class Interface;
  47. class ClassDef;
  48. class Type;
  49. class IntConst;
  50. class DoubleConst;
  51. class StringConst;
  52.  
  53. class IR;
  54.  
  55. //----------  Routines in main.cc  ----------
  56.  
  57. void initializeConstants ();
  58. void printAllData ();
  59. void dump (char * message);
  60. void testLexer ();
  61. void printToken (Token token);
  62. void programLogicError (char * msg);
  63. void terminateCompiler ();
  64. void fatalError (char * msg);
  65. void syntaxError (char * msg);
  66. void syntaxErrorWithToken (Token token, char * msg);
  67. void error (AstNode * node, char * msg);
  68. void error2 (AstNode * node, char * msg);
  69. void doMessage (Token token, char * prefix, char * msg);
  70. void errorWithType (char * msg, Type * type);
  71. void checkTokenSkipping (int count);
  72. void processCommandLine (int argc, char ** argv);
  73. void printHelp ();
  74. void checkHostCompatibility ();
  75. char * appendStrings (char * str1, char * str2, char * str3);
  76. void divide (int a, int b);
  77. int truncateToInt (double d);
  78.  
  79.  
  80.  
  81. enum {
  82.        // Misc token types
  83.        ID=257, INT_CONST, DOUBLE_CONST, CHAR_CONST, STRING_CONST, OPERATOR,
  84.  
  85.        // Keywords
  86.        ALLOC, ANY_TYPE, ARRAY, ARRAY_SIZE, AS_INTEGER, AS_PTR_TO, BEHAVIOR, BOOL,
  87.        BREAK, BY, CASE, CATCH, CHAR, CLASS, CODE,
  88.        CONST, CONTINUE, DEBUG, DEFAULT, DO, DOUBLE, ELSE, ELSE_IF,
  89.        END_BEHAVIOR, END_CLASS, END_CODE, END_FOR, END_FUNCTION, END_HEADER, END_IF,
  90.        END_INTERFACE, END_METHOD, END_RECORD, END_SWITCH, END_TRY, END_WHILE,
  91.        ENUM, ERRORS, EXTENDS,
  92.        EXTERNAL, FALSE, FIELDS, FOR, FREE, FUNCTION, FUNCTIONS,
  93.        HEADER, IF, IMPLEMENTS, INFIX, INT,
  94.        INTERFACE, IS_INSTANCE_OF, IS_KIND_OF,
  95.        MESSAGES, METHOD, METHODS, NEW, NULL_KEYWORD,
  96.        OF, PREFIX, PTR, RECORD, RENAMING, RETURN, RETURNS, SELF, SIZE_OF, SUPER, SUPER_CLASS,
  97.        SWITCH, THROW, TO, TRUE, TRY, TYPE, TYPE_OF_NULL, UNTIL, USES, VAR, VOID, WHILE,
  98.  
  99.        // Punctuation tokens
  100.        L_PAREN, R_PAREN, L_BRACE, R_BRACE, L_BRACK, R_BRACK,
  101.        COLON, SEMI_COLON, COMMA, PERIOD, EQUAL,
  102.  
  103.        // Symbols describing nodes in the Abstract Syntax Tree
  104.        ALLOC_EXPR, ARGUMENT, ARRAY_ACCESS,
  105.        ARRAY_SIZE_EXPR, ARRAY_TYPE, AS_INTEGER_EXPR,
  106.        AS_PTR_TO_EXPR, ASSIGN_STMT, BOOL_CONST,
  107.        BOOL_TYPE, BREAK_STMT, CALL_EXPR, CALL_STMT,
  108.        CHAR_TYPE, CLASS_DEF, CLASS_FIELD,
  109.        CLOSURE_EXPR, CONST_DECL, CONSTRUCTOR,
  110.        CONTINUE_STMT, COUNT_VALUE, DO_STMT, DOUBLE_TYPE, DYNAMIC_CHECK,
  111.        ERROR_DECL, FIELD_ACCESS, FIELD_INIT, FOR_STMT,
  112.        FUNCTION_PROTO, FUNCTION_TYPE, GLOBAL, IF_STMT,
  113.        INT_TYPE, IS_INSTANCE_OF_EXPR, IS_KIND_OF_EXPR, LOCAL,
  114.        METHOD_PROTO, NAMED_TYPE, NEW_EXPR, NULL_CONST, PARAMETER,
  115.        PTR_TYPE, RECORD_FIELD, RECORD_TYPE, RETURN_STMT,
  116.        SELF_EXPR, SEND_EXPR, SEND_STMT, SIZE_OF_EXPR,
  117.        SUPER_EXPR, SWITCH_STMT, THROW_STMT,
  118.        TRY_STMT, TYPE_ARG, TYPE_DEF, TYPE_OF_NULL_TYPE,
  119.        TYPE_PARM, VARIABLE_EXPR, VOID_TYPE, WHILE_STMT, FREE_STMT, DEBUG_STMT,
  120.  
  121.        // AstNode types, which are also keywords
  122.        // BEHAVIOR, CASE, CATCH, CHAR_CONST, CODE, DOUBLE_CONST,
  123.        // FUNCTION, HEADER, INT_CONST, INTERFACE, METHOD, STRING_CONST,
  124.        // USES, RENAMING,
  125.  
  126.        // Symbols used to identify build-in function ids, and message selectors
  127.        INT_TO_DOUBLE, DOUBLE_TO_INT, INT_TO_CHAR, CHAR_TO_INT,
  128.        PTR_TO_BOOL, POS_INF, NEG_INF, NEG_ZERO,
  129.        I_IS_ZERO, I_NOT_ZERO,
  130.  
  131.        PLUS, MINUS, STAR, SLASH, PERCENT,
  132.        BAR, CARET, AMP, BAR_BAR, AMP_AMP,
  133.        EQUAL_EQUAL, NOT_EQUAL, LESS, LESS_EQUAL, GREATER, GREATER_EQUAL,
  134.        LESS_LESS, GREATER_GREATER, GREATER_GREATER_GREATER,
  135.  
  136.        UNARY_BANG, UNARY_STAR, UNARY_AMP, UNARY_MINUS,
  137.  
  138.        // Symbols identifying primitive messages
  139.        PRIMITIVE_I_ADD,
  140.        PRIMITIVE_I_SUB,
  141.        PRIMITIVE_I_MUL,
  142.        PRIMITIVE_I_DIV,
  143.        PRIMITIVE_I_REM,
  144.        PRIMITIVE_I_SLL,
  145.        PRIMITIVE_I_SRA,
  146.        PRIMITIVE_I_SRL,
  147.        PRIMITIVE_I_LT,
  148.        PRIMITIVE_I_LE,
  149.        PRIMITIVE_I_GT,
  150.        PRIMITIVE_I_GE,
  151.        PRIMITIVE_I_EQ,
  152.        PRIMITIVE_I_NE,
  153.        PRIMITIVE_I_AND,
  154.        PRIMITIVE_I_OR,
  155.        PRIMITIVE_I_XOR,
  156.        PRIMITIVE_I_NOT,
  157.        PRIMITIVE_I_NEG,
  158.        PRIMITIVE_I_IS_ZERO,
  159.        PRIMITIVE_I_NOT_ZERO,
  160.  
  161.        PRIMITIVE_D_ADD,
  162.        PRIMITIVE_D_SUB,
  163.        PRIMITIVE_D_MUL,
  164.        PRIMITIVE_D_DIV,
  165.        PRIMITIVE_D_LT,
  166.        PRIMITIVE_D_LE,
  167.        PRIMITIVE_D_GT,
  168.        PRIMITIVE_D_GE,
  169.        PRIMITIVE_D_EQ,
  170.        PRIMITIVE_D_NE,
  171.        PRIMITIVE_D_NEG,
  172.  
  173.        PRIMITIVE_B_AND,
  174.        PRIMITIVE_B_OR,
  175.        PRIMITIVE_B_NOT,
  176.        PRIMITIVE_B_EQ,
  177.        PRIMITIVE_B_NE,
  178.  
  179.        PRIMITIVE_OBJECT_EQ,
  180.        PRIMITIVE_OBJECT_NE,
  181.        PRIMITIVE_DEREF,
  182.        PRIMITIVE_ADDRESS_OF,
  183.  
  184.        // Symbols identifying primitive functions
  185.        PRIMITIVE_INT_TO_DOUBLE,
  186.        PRIMITIVE_DOUBLE_TO_INT,
  187.        PRIMITIVE_INT_TO_CHAR,
  188.        PRIMITIVE_CHAR_TO_INT,
  189.        PRIMITIVE_PTR_TO_BOOL,
  190.        PRIMITIVE_POS_INF,
  191.        PRIMITIVE_NEG_INF,
  192.        PRIMITIVE_NEG_ZERO,
  193.  
  194.        // Misc symbols
  195.        KEYWORD, NORMAL,
  196.  
  197. };
  198.  
  199.  
  200.  
  201. //----------  String Table  ----------
  202.  
  203. class String {
  204.   public:
  205.     String       * next;           // Link list of Strings
  206.     int          type;             // E.g., ID or BOOL, BREAK, ...
  207.     int          primitiveSymbol;  // 0=not a primitive; e.g., PLUS, DOUBLE_TO_INT, ...
  208.     int          length;           // The length and characters
  209.     char         chars [];
  210. };
  211.  
  212. #define MAX_STR_LEN 2000           // Strings, IDs are limited to 2000 chars.
  213. #define STRING_TABLE_HASH_SIZE 211 // Size of hash table for string table.
  214. extern String * stringTableIndex [STRING_TABLE_HASH_SIZE];
  215.  
  216.  
  217.  
  218. //----------  Token  ----------
  219.  
  220. union TokenValue {
  221.   String * svalue;
  222.   int      ivalue;
  223.   double   rvalue;
  224. };
  225.  
  226. struct Token {
  227.   int        type;
  228.   TokenValue value;
  229.   int        tokenPos;
  230. //  char *     fileName;
  231. //  int        lineNumber;
  232. };
  233.  
  234.  
  235.  
  236. //----------  Global Variables  ----------
  237.  
  238. #define MAX_INPUT_FILES 255            /* Do not change; related to 8 bits */
  239.  
  240. extern TokenValue currentTokenValue;   // Used in lexer only
  241. extern Token tokenMinusOne, token, token2, token3, token4, token5;
  242. extern int currentInputFileIndex;      // These describe the current position in the file
  243. extern int currentLineOfToken;         // .
  244. extern int currentCharPosOfToken;      // .
  245. extern int posOfNextToken;             // Position of the next token
  246. extern int eofCount;                   // Used to check for looping on EOF
  247. extern char * inputFileNames [MAX_INPUT_FILES+1];    // Array of ptrs to file names
  248. extern int errorsDetected;             // Count of errors detected so far
  249. extern int tokenPosOfLastError;        // Used to suppress extraneous syntax errors
  250. extern int hashVal;                    // The running hash code for this file
  251. extern int hashCount;                  // Used in comuting the hashVal
  252. extern char * commandPackageName;      // The package name, NULL = missing
  253. extern char * commandDirectoryName;    // The search directory name, NULL = missing
  254. extern char * headerFileName;          // The header file name, NULL = missing
  255. extern char * codeFileName;            // The code file name, NULL = missing
  256. extern char * outputFileName;          // The .s filename, NULL = missing
  257. extern FILE * inputFile;               // The input file, e.g., stdin
  258. extern FILE * outputFile;              // The output file, e.g., stdout
  259. extern int commandOptionS;             // True: print the symbol table
  260. extern int commandOptionP;             // True: pretty-print the AST
  261. extern int commandOptionAST;           // True: print the full AST
  262. extern int commandOptionTestLexer;     // True: stop after lexer & print tokens
  263. extern int commandOptionTestParser;    // True: stop after parser & print AST
  264. extern int safe;                       // True: only allow safe constructs
  265. extern Header * headerList;            // List of all headers
  266. extern Header * headerListLast;        // .
  267. extern Mapping<String, Header> *       // Strings --> Headers
  268.   headerMapping;                       // .
  269. extern Code * code;                    // The thing being compiled
  270. extern Header * mainHeader;            // The corresponding header
  271. extern Interface * tempInterList;      // Used in topoProcessInterfaces
  272. extern ClassDef * tempClassList;       // Used in topoProcessClasses
  273. extern int changed;                    // Used in assignOffsets
  274. extern int quo;                        // Used in integer division
  275. extern int rem;                        // .
  276. extern ClassDef * currentClass;        // The class we are currently processing
  277. extern Header * currentHeader;         // The header we are currently processing
  278. extern int recursionCounter;           // Used to detect recursive types
  279. extern IntConst * memoryStart;         // Used to compute memory usage by compiler
  280. extern IR * firstInstruction;          // List of IR instructions
  281. extern IR * lastInstruction;           // .
  282. extern int maxArgBytesSoFar;           // Used in setting fun/meth->maxArgBytes
  283. extern DoubleConst * floatList;        // Used during code gen
  284. extern StringConst * stringList;       // Used during code gen
  285. extern MethOrFunction * currentFunOrMeth;  // Used during code gen
  286. extern Offset * firstDispatchOffset;   // Ptr to linked list: 4,8,12,16,...
  287.  
  288.  
  289.  
  290. #define BUFF_LEN 300
  291. extern char buffer [BUFF_LEN];     // Misc. use, e.g., "_Person__Constructor"
  292.  
  293. // During parsing errors, tokens may be skipped in an attempt to recover
  294. // and continue parsing.  Do we print a message saying that tokens were
  295. // skipped?  This constant determines the threshhold for printing that msg.
  296. #define TOKEN_SKIP_COUNT 1
  297.  
  298. // The compiler will automatically terminate after this many error messages
  299. #define MAX_NUMBER_OF_ERRORS 100
  300.  
  301.  
  302.  
  303. //----------  Lexical Routines  ----------
  304.  
  305. char * initScanner (char * filename);
  306. void scan ();
  307. int getToken (void);
  308. void lexError (char *msg);
  309. void initKeywords ();
  310. String * lookupAndAdd (char * givenStr, int newType);
  311. String * lookupAndAdd2 (char * givenStr, int length, int newType);
  312. int bytesEqual (char * p, char * q, int length);
  313. void printStringTable ();
  314. void printString (FILE * file, String *);
  315. void printChar (FILE * file, int c);
  316. char * symbolName (int i);
  317. void printSymbol (int sym);
  318. int hexCharToInt (char ch);
  319. char intToHexChar (int c);
  320. double raisedToThePower (int i);
  321. int isEscape (char c);
  322. int scanEscape ();
  323. int isOpChar (char);
  324. void addToHash (int i);
  325. void addTokenToHash (Token tok);
  326. int createTokenPos ();
  327. int extractLineNumber (Token token);
  328. int extractCharPos (Token token);
  329. char * extractFilename (Token token);
  330. void incrLineNumber ();
  331. char getNextChar ();
  332. void unGetChar (char ch);
  333. void addToInputFilenames (char * filename);
  334.  
  335.  
  336. #include "ast.h"
  337.  
  338.  
  339. //----------  Misc. routines in ast.cc  ----------
  340.  
  341. void pretty (AstNode *);
  342. void pretty2 (AstNode *);
  343. void printStmtList (int indent, Statement * p);
  344. void printVarDecls (int indent, VarDecl * p);
  345. void printConsts (int indent, ConstDecl * p);
  346. void printErrorDecls (int indent, ErrorDecl * p);
  347. void printTypeDefs (int indent, TypeDef * p);
  348. void printFunctionProtos (int indent, FunctionProto * p);
  349. void printFunctions (int indent, Function * p);
  350. void printParmList (int indent, Parameter * parmList);
  351. void printTypeParms (int indent, TypeParm * parmList);
  352. void ppIndent (int indent);
  353. void ppLine (int indent, char * str);
  354.  
  355. /*****
  356. void printDeclList (Decl * declList, int indent);
  357. void printTemplate (TypeParm * typeParms);
  358. void printBinaryOp (int op);
  359. void printModeAndLength (int mode, int length);
  360. *****/
  361.  
  362.  
  363. //---------- Global Data  ----------
  364.  
  365. extern String * stringUnaryBang;
  366. extern String * stringUnaryStar;
  367. extern String * stringUnaryAmp;
  368. extern String * stringUnaryMinus;
  369.  
  370. extern String * stringPlus;
  371. extern String * stringMinus;
  372. extern String * stringStar;
  373. extern String * stringSlash;
  374. extern String * stringPercent;
  375. extern String * stringBar;
  376. extern String * stringCaret;
  377. extern String * stringAmp;
  378. extern String * stringBarBar;
  379. extern String * stringAmpAmp;
  380. extern String * stringEqualEqual;
  381. extern String * stringNotEqual;
  382. extern String * stringLess;
  383. extern String * stringLessEqual;
  384. extern String * stringGreater;
  385. extern String * stringGreaterEqual;
  386. extern String * stringLessLess;
  387. extern String * stringGreaterGreater;
  388. extern String * stringGreaterGreaterGreater;
  389. extern String * stringIntToDouble;
  390. extern String * stringDoubleToInt;
  391. extern String * stringIntToChar;
  392. extern String * stringCharToInt;
  393. extern String * stringPtrToBool;
  394. extern String * stringPosInf;
  395. extern String * stringNegInf;
  396. extern String * stringNegZero;
  397. extern String * stringIIsZero;
  398. extern String * stringINotZero;
  399. extern String * stringObject;
  400. extern String * stringMain;
  401.  
  402. extern CharType * basicCharType;
  403. extern IntType  * basicIntType;
  404. extern DoubleType * basicDoubleType;
  405. extern BoolType * basicBoolType;
  406. extern VoidType * basicVoidType;
  407. extern TypeOfNullType * basicTypeOfNullType;
  408. extern AnyType * basicAnyType;
  409. extern PtrType * basicCharArrayPtrType;
  410. extern PtrType * basicVoidPtrType;
  411. extern PtrType * basicAnyPtrType;
  412.  
  413. extern IntConst * constantIntZero;
  414. extern IntConst * constantIntOne;
  415. extern IntConst * constantIntMinusOne;
  416. extern BoolConst * constantFalse;
  417. extern BoolConst * constantTrue;
  418.  
  419. /*****
  420. extern DoubleExpr * constantDoubleZero;
  421. extern DoubleExpr * constantDoubleOne;
  422. extern CharExpr * constantCharNull;
  423. extern NullExpr * constantNull;
  424. extern String * stringGenericDestructor;
  425. extern String * stringThis;
  426.  
  427.  
  428. *****/
  429.  
  430. //----------  Parsing Routines  ----------
  431.  
  432. int nextTokenIsID (char * msg);
  433. String * mustHaveID (char * msg);
  434. void mustHave (int tok, char * msg);
  435. int mustHaveOrScanUntilInScanSet (int tok, char * msg);
  436. void scanToFollowType ();
  437. int inScanSet ();
  438. int inFirstStmt ();
  439. int inFollowStmt ();
  440. int inFirstExpr (Token tok);
  441. int inFollowExpr ();
  442. int inFirstType (Token tok);
  443. int inFollowType ();
  444. int inHeaderSet ();
  445. int inCodeSet ();
  446. Statement * appendStmtLists (Statement * stmtList1,
  447.                            Statement * stmtList2);
  448. Statement * parseStmtList (Statement * enclosingStmtForBreak,
  449.                            Statement * enclosingStmtForContinue,
  450.                            MethOrFunction * enclosingMethOrFunction,
  451.                            int endsWithSemiColon);
  452. Statement * parseStmt (Statement * enclosingStmtForBreak,
  453.                        Statement * enclosingStmtForContinue,
  454.                        MethOrFunction * enclosingMethOrFunction);
  455. int gotVoidType (Type * type);
  456. Parameter * parseParmList ();
  457. Function * parseFunction (int expectingID);
  458. FunctionProto * parseFunctionProtos ();
  459. Method * parseMethod ();
  460. MethodProto * parseMethodProto ();
  461. Header * parseHeader ();
  462. Code * parseCode ();
  463. Behavior * parseBehavior ();
  464. Uses * parseUses ();
  465. Renaming * parseRenamings ();
  466. String * pickupKeywordSelector ();
  467. int colonCount (char * str);
  468. Interface * parseInterface ();
  469. TypeParm * parseTypeParms ();
  470. ClassDef * parseClass();
  471.  
  472. Type * parseType (char * errorMsg);
  473. RecordField * parseRecordFieldList ();
  474. FunctionType * parseFunctionType ();
  475. NamedType * parseNamedType (char * errorMsg);
  476. ArrayType * parseArrayType (Token tokenForPos);
  477. Expression * parseExpr (char * errorMsg);
  478. Expression * parseExpr0 (char * errorMsg);
  479. Expression * parseExpr1 (char * errorMsg);
  480. Expression * parseExpr2 (char * errorMsg);
  481. Expression * parseExpr3 (char * errorMsg);
  482. // Expression * parseExpr4 (char * errorMsg);
  483. Expression * parseExpr5 (char * errorMsg);
  484. Expression * parseExpr6 (char * errorMsg);
  485. Expression * parseExpr7 (char * errorMsg);
  486. Expression * parseExpr8 (char * errorMsg);
  487. Expression * parseExpr9 (char * errorMsg);
  488. Expression * parseExpr10 (char * errorMsg);
  489. Expression * parseExpr11 (char * errorMsg);
  490. Expression * parseExpr12 (char * errorMsg);
  491. Expression * parseExpr13 (char * errorMsg);
  492. Expression * parseExpr14 (char * errorMsg);
  493. Expression * parseExpr15 (char * errorMsg);
  494. Expression * parseExpr16 (char * errorMsg);
  495. Expression * parseExpr17 (char * errorMsg);
  496. ArrayAccess * parseArrayAccess (Token tokenForPos, Expression * soFar);
  497. Argument * parseArgList ();
  498. Constructor * parseConstructor ();
  499. Local * parseLocalVarDecls ();
  500. Global * parseGlobalVarDecls ();
  501. ClassField * parseClassFields ();
  502. ErrorDecl * parseErrorDecls ();
  503. ConstDecl * parseConstDecls ();
  504. ConstDecl * parseEnums ();
  505. TypeDef * parseTypeDefs ();
  506.  
  507.  
  508. //----------  Routines in printAst.cc  ----------
  509.  
  510. void printAst (int indent, AstNode *t);
  511. void printHeader (int indent, char * str, AstNode * p);
  512. void printFooter (int indent);
  513. void printIndent (int indent);
  514. void printLine (int indent, char * str);
  515. void printPtrField (int indent, char * str, AstNode * id);
  516. void printIntField (int indent, char * str, int i);
  517. void printBoolField (int indent, char * str, int i);
  518. void printStringField (int indent, char * str1, String * str2);
  519. void printSymbolField (int indent, char * str, int sym);
  520. void printCharPtrField (int indent, char * str, char * charPtr);
  521. void printId (int indent, String * id);
  522. void printFieldName (int indent, char * str);
  523. void printItem (int indent, char * s, AstNode * t);
  524. void printOperator (int indent, int op);
  525. int printPtr (AstNode * p);
  526. void fpretty (Type * p);
  527. void fpretty2 (Type * p, int wantRecursion);
  528.  
  529. //----------  Mapping Class Stuff  ----------
  530.  
  531. template <class Key, class Value> class Mapping {
  532.   public:
  533.     void    enter (Key * k, Value * v);
  534.     Value * find (Key * k);                       // Search all scopes
  535.     Value * findInTopScope (Key * k);
  536.     int     alreadyDefined (Key * k);             // Search top scope only
  537.     void    print (int indent);
  538.     Value * getFirst ();                          // Used to iterate through
  539.     Value * getNext ();                           //     all values in top scope
  540.     Key *   getItsKey ();                         //     Return key of last value
  541.     void    printOffsetToSelector (char * title); // Used for "offsetToSelector" maps
  542.     void    printSelectorToOffset ();             // Used for "selectorToOffset" maps
  543.     Mapping (int initSize, Mapping * superMap);   // Zero is OK; NULL is OK
  544.     ~Mapping ();
  545.  
  546.   private:
  547.     Mapping * superMap;
  548.     int numberOfElements;
  549.     int sizeOfArray;
  550.     Bucket < Key, Value> * * array;
  551. //    int iteratorLastIndex;
  552.     Bucket < Key, Value> * iteratorLastBucket;
  553.     Bucket < Key, Value> * firstInsertedBucket;
  554.     Bucket < Key, Value> * lastInsertedBucket;
  555.  
  556.     int nextLargerSize (int oldSize);
  557.     void rehashIfNecessary ();
  558.     int arrayIndex (Key *);
  559.     void printKeyValue (Key * key, Value * value, int indent);
  560. };
  561.  
  562. template <class Key, class Value> class Bucket {
  563.   public:
  564.     Bucket * next;
  565.     Bucket * nextForIterator;
  566.     Key * key;
  567.     Value * value;
  568.     Bucket () {
  569.       next = NULL;
  570.       nextForIterator = NULL;
  571.       key = NULL;
  572.       value = NULL;
  573.     }
  574.     ~ Bucket () { }
  575. };
  576.  
  577. void testMapping ();
  578.  
  579.  
  580.  
  581. //----------  Offset  ----------
  582. //
  583. // This data structure is used in assigning offsets to messages in
  584. // the dispatch tables.  There is a single linked list of these
  585. // structures, with values 4,8,12,16,20,...  We use these structures
  586. // rather than "int"s so that we may put them into Mappings.
  587.  
  588. struct Offset {
  589.   int        ivalue;
  590.   Offset *   nextOffset;
  591. };
  592.  
  593.  
  594.  
  595. //----------  AbstractStack  ----------
  596. //
  597. // The algorithm which computes the dispatch table offsets of messages
  598. // (in the presence of multiple inheritance among interfaces) need a stack
  599. // of Abstracts.  The following data structure supports this.  See routines
  600. // "stackPush", "stackPop", "stackEmpty", ...  A stack is represented by a
  601. // pointer to the top element.  Each element on the stack points to the
  602. // element below it.
  603.  
  604. struct AbstractStack {
  605.   Abstract *        element;
  606.   AbstractStack *   next;
  607. };
  608.  
  609.  
  610.  
  611. //----------  Routines in check.cc  ----------
  612.  
  613. void topoProcessAllPackages ();
  614. void topoProcessOnePackage (Header * hdr);
  615. void buildPackageMapping (Header * hdr);
  616. void addToPackage (Header * hdr, String * id, AstNode * item);
  617. void topoProcessInterfaces (Header * hdr);
  618. void topoProcessOneInterface (Interface * inter);
  619. void topoProcessClasses (Header * hdr);
  620. void topoProcessOneClass (ClassDef * cl);
  621. void bindTypeNames (AstNode * node,
  622.                     Mapping <String, AstNode> * typeParmMap);
  623. void checkTypeDefCircularity (Header * hdr);
  624. Type * findCoreType (Type * typ);
  625. Type * copyTypeWithSubst (Type * type, Mapping<TypeParm,Type> * subst);
  626. TypeArg * copyArgListWithSubst (TypeArg * argList,
  627.                                 Mapping<TypeParm,Type> * subst);
  628. RecordField * copyRecordFieldsWithSubst (RecordField * fieldList,
  629.                                          Mapping <TypeParm, Type> * subst);
  630. void inheritFields (Header * hdr);
  631. void inheritMethodProtos (Header * hdr);
  632. String * addSuperTo (String * str);
  633. void inheritMessages (Header * hdr);
  634. //----------
  635. void bindVarNames (AstNode * node,
  636.                    Mapping <String, AstNode> * varMap);
  637.  
  638. void setFourByteRestricted (Type * t);
  639. //----------
  640. void assignOffsetsAndEvalExprs (Header * hdr);
  641. void assignOffsets2 (AstNode * node, int wantPrinting);
  642. void assignOffsetsInClass (ClassDef * cl, int wantPrinting);
  643. void assignOffsetsInRecord (RecordType * recordType, int wantPrinting);
  644. int assignOffsetsInParmList (Parameter * parmList,
  645.                               int wantPrinting,
  646.                               int startingOffset);
  647. void setParmSizeInFunctionType (FunctionType * fType);
  648. int needsAlignment (Type * type);
  649. AstNode * getTypeDef (Type * t);
  650. int sizeInBytesOfWhole (Type * type, AstNode * errNode, int wantPrinting);
  651. AstNode * evalExprsIn (AstNode * node);
  652. int isIntConst (AstNode * node);
  653. int isCharConst (AstNode * node);
  654. int isBoolConst (AstNode * node);
  655. int isDoubleConst (AstNode * node);
  656. //----------
  657. void testSubType (Header * hdr);
  658. int typesEqual (Type * t1, Type * t2);
  659. int isSubType (Type * t1, Type * t2);
  660. int assignable (Type * t1, Type * t2);
  661. Mapping <TypeParm, Type> * buildSubstitution (TypeParm * typeParm,
  662.                                               TypeArg * typeArg);
  663. Parameter * copyParmListWithSubst (Parameter * parmList,
  664.                                    Mapping <TypeParm, Type> * subst);
  665. //----------
  666. void checkImplements (Header * hdr);
  667. void checkMethodProtos (Header * hdr);
  668. void checkProtos (MethodProto * protoSuper, MethodProto * protoSub);
  669. void checkProtos2 (MethodProto * protoSuper, MethodProto * protoSub);
  670. void checkProtos3 (MethodProto * proto1, MethodProto * proto2);
  671. void checkExtends (Header * hdr);
  672. //----------
  673. Type * checkTypes (AstNode * hdr);
  674. Type * checkConstructor (Constructor * constructor);
  675. void linkDouble (DoubleConst * doubleConst);
  676. void checkStaticData (Expression * expr, AstNode * node);
  677. AstNode * isStaticData (Expression * expr);
  678. void checkArgList (Argument * arg,
  679.                    Parameter * parm,
  680.                    AstNode * proto,
  681.                    AstNode * invocation);
  682. void checkArgList2 (Argument * arg,
  683.                    TypeArg * parm,
  684.                    AstNode * proto,
  685.                    AstNode * invocation);
  686. void checkTypeInstantiation (TypeArg * typeArg,
  687.                              TypeParm * typeParm,
  688.                              AstNode * proto,
  689.                              AstNode * invocation);
  690. Type * checkForPrimitive (SendExpr * sendExpr,
  691.                           Type * recvrType,
  692.                           int PRIMITIVE_I_OP,
  693.                           int PRIMITIVE_D_OP);
  694. Type * checkForPrimitive2 (SendExpr * sendExpr,
  695.                            Type * recvrType,
  696.                            int PRIMITIVE_I_OP);
  697. Type * checkForPrimitive3 (SendExpr * sendExpr,
  698.                            Type * recvrType,
  699.                            int PRIMITIVE_I_OP,
  700.                            int PRIMITIVE_D_OP);
  701. Type * checkForPrimitive4 (SendExpr * sendExpr,
  702.                            Type * recvrType,
  703.                            int PRIMITIVE_B_OP);
  704. Type * checkForPrimitive5 (SendExpr * sendExpr,
  705.                            Type * recvrType,
  706.                            int PRIMITIVE_I_OP,
  707.                            int PRIMITIVE_D_OP,
  708.                            int PRIMITIVE_B_OP,
  709.                            int PRIMITIVE_OBJ_OP);
  710. Type * checkMessageSend (MethodProto * methodProto,
  711.                          SendExpr * sendExpr,
  712.                          Type * recvrType,
  713.                          Mapping<TypeParm,Type> * subst);
  714. Expression * checkAssignment (Expression * expr,
  715.                               Type * expectedType,
  716.                               char * msg,
  717.                               AssignStmt * assignStmt);
  718. Expression * insertIntToDouble (Expression * expr);
  719. Expression * insertIntIsZero (Expression * expr);
  720. Expression * insertIntNotZero (Expression * expr);
  721. Expression * insertCharToInt (Expression * expr);
  722. Expression * insertPtrToBool (Expression * expr);
  723. Expression * insertDeref (Expression * expr);
  724. int isDeref (AstNode * p);
  725. int isAddressOf (AstNode * p);
  726. Expression * insertFalse (Expression * expr);
  727. Expression * insertTrue (Expression * expr);
  728. Expression * insertZero (Expression * expr);
  729. int argCount (Argument * argList);
  730. int isCharType (Type * type);
  731. int isIntType (Type * type);
  732. int isDoubleType (Type * type);
  733. int isBoolType (Type * type);
  734. int isVoidType (Type * type);
  735. int isTypeOfNullType (Type * type);
  736. int isAnyType (Type * type);
  737. int isPtrType (Type * type);
  738. int isPtrToVoidType (Type * type);
  739. PtrType * getPtrType (Type * type);
  740. int isRecordType (Type * type);
  741. int isArrayType (Type * type);
  742. ArrayType * getArrayType (Type * type);
  743. FunctionType * getFunctionType (Type * type);
  744. int isObjectType (Type * type);
  745. int isExactClass (Type * type);
  746. ClassDef * getClassDef (Type * type);
  747. Interface * getInterface (Type * type);
  748. Type * resolveNamedType (Type * type);
  749. Type * resolveNamedType2 (Type * type);
  750. void checkConcreteClass (Type * type,
  751.                          AstNode * errorNode,
  752.                          char * errorMsg);
  753. void checkConcreteClassOrInterface (Type * type,
  754.                                     AstNode * errorNode,
  755.                                     char * errorMsg);
  756. void checkConcreteType (Type * type,
  757.                         AstNode * errorNode,
  758.                         char * errorMsg);
  759. int isLValue (Expression * expr);
  760. void updateMaxArgBytes (int i);
  761. //----------
  762. int fallsThru (AstNode * node);
  763. int fallsThruStmtList (Statement * stmtList);
  764. //----------
  765. void assignLocalOffsets (Header * hdr);
  766. void assignOffsetsInMethOrFunction (MethOrFunction * methOrFunction);
  767. int assignOffsetToLocal (int lastOffset,
  768.                          VarDecl * varDecl,
  769.                          int commandOptionS,
  770.                          int doingBytes);
  771. void printParmOffsets (Parameter * parmList);
  772. //----------
  773. void assignDispatchTableOffsets (Header * hdr);
  774. int isThisOffsetOK (String * sel,
  775.                     Offset * offset,
  776.                     Mapping<Abstract,Abstract> * setToCheck);
  777. void assignOffsetToSelector (String * sel,
  778.                              Offset * offset,
  779.                              Mapping <Abstract, Abstract> * relatedSet,
  780.                              Mapping <Abstract, Abstract> * affectedSet);
  781. void addAllSuperAbstracts (Abstract * abs,
  782.                            Header * hdr,
  783.                            Mapping <Abstract, Abstract> * supersInThisPackage,
  784.                            Mapping <Abstract, Abstract> * supersInOtherPackages);
  785. Offset * nextDispatchOffset (Offset * off);
  786. void stackPush (AbstractStack * st, Abstract * elt);
  787. Abstract * stackPop (AbstractStack * st);
  788. int stackEmpty (AbstractStack * st);
  789. AbstractStack * newStack ();
  790.  
  791.  
  792.  
  793.  
  794. //----------  Routines in ir.cc  ----------
  795.  
  796. #include "ir.h"
  797.  
  798. int within16Bits (int);
  799. void getIntoReg4 (AstNode * src, char * reg);
  800. void getIntoReg1 (AstNode * src, char * reg1, char * reg2);
  801. void getIntoReg8 (AstNode * src, char * freg1, char * reg2);
  802. void getAddrOfVarIntoReg (AstNode * src, char * reg1);
  803. void storeFromReg4 (VarDecl * dest, char * reg1, char * reg2);
  804. void storeFromReg1 (VarDecl * dest, char * reg1, char * reg2);
  805. void storeFromReg8 (VarDecl * dest, char * freg1, char * reg2);
  806. void printIR ();
  807. void printANode (AstNode * node);
  808.  
  809.  
  810.  
  811. //----------  Routines in gen.cc  ----------
  812.  
  813. void generateIR ();
  814. void genInterface (Interface * inter);
  815. void genClass (ClassDef * cl);
  816. void addAllSupers (Abstract * abs, Mapping<Abstract, Abstract > * setOfSupers);
  817. void genMethOrFunction (MethOrFunction * methOrFunction);
  818. void genVarDescriptor (VarDecl * varDecl);
  819. void genStaticData (Expression * expr);
  820. FieldInit * sortFieldInits (FieldInit * f);
  821. void genStmts (Statement * stmtList);
  822. void genIfStmt (IfStmt * stmt);
  823. void genAssignStmt (AssignStmt * stmt);
  824. void genCallStmt (CallStmt * stmt);
  825. void genSendStmt (SendStmt * stmt);
  826. void genCallExpr (VarDecl * target,
  827.                   CallExpr * callExpr,
  828.                   char * trueLabel,
  829.                   char * falseLabel);
  830. void genSendExpr (VarDecl * target,
  831.                   SendExpr * sendExpr,
  832.                   char * trueLabel,
  833.                   char * falseLabel);
  834. void genWhileStmt (WhileStmt * stmt);
  835. void genDoStmt (DoStmt * stmt);
  836. void genBreakStmt (BreakStmt * stmt);
  837. void genContinueStmt (ContinueStmt * stmt);
  838. void genReturnStmt (ReturnStmt * stmt);
  839. void genForStmt (ForStmt * stmt);
  840. void genSwitchStmt (SwitchStmt * stmt);
  841. int roundUpToPrime (int i);
  842. int hashForSwitchStmt (int i, int hashMax);
  843. void genTryStmt (TryStmt * stmt);
  844. void genThrowStmt (ThrowStmt * stmt);
  845. void genFreeStmt (FreeStmt * stmt);
  846. void genDebugStmt (DebugStmt * stmt);
  847. AstNode * genExpr (Expression * p, int targetSize);
  848. void genExprInto (VarDecl * target,
  849.                   Expression * node,
  850.                   char * trueLabel,
  851.                   char * falseLabel);
  852. VarDecl * genAddressOf (Expression * node);
  853. VarDecl * genConstructor (Constructor * constructor, VarDecl * target);
  854. char * newLabel ();
  855. char * newName (char * str);
  856. char * sanitizedPackageName (String * packageName);
  857. char * newMethodName (char * sanitizedClassName, int i);
  858. void genLineNumber (AstNode * node, char * stmtCode);
  859. Local * newTemp (int size);
  860.  
  861. //qqqqq
  862.  
  863.  
  864.  
  865.  
  866.  
  867. /***************
  868.  
  869.  
  870. //  //----------  Routines in check.cc  ----------
  871. //  
  872. //  void addGlobalDecls ();
  873. //  void addFunctions ();
  874. //  void checkSuperclassCycles ();
  875. //  void bindAllTypeNames ();
  876. //  void bindTypeNamesIn (AstNode * node,
  877. //                        Mapping<String,TypeParm> * typeParmMap);
  878. //  void bindTypeNamesInStmtList (Statement * stmtList,
  879. //                                Mapping<String,TypeParm> * typeParmMap);
  880. //  void topoProcessAllClasses ();
  881. //  void topoProcess (Class * cl);
  882. //  void buildFieldMapping ();
  883. //  void buildMethodMapping ();
  884. //  Header * copyHeaderWithSubst (Header * subst,
  885. //                                Mapping<TypeParm,Type> * subst);
  886. //  void checkConstructor (Method * meth, Class * cl);
  887. //  void bindAllNames ();
  888. //  void bindNamesIn (AstNode * node,
  889. //                    Mapping<String,AstNode> * varMap,
  890. //                    Mapping<String,AstNode> * methMap,
  891. //                    Class * currentClass,
  892. //                    AstNode * enclosingMethodOrFunction);
  893. //  void bindNamesInStmtList (Statement * stmtList,
  894. //                            Mapping<String,AstNode> * varMap,
  895. //                            Mapping<String,AstNode> * methMap,
  896. //                            Class * currentClass,
  897. //                            AstNode * enclosingMethodOrFunction);
  898. //  
  899. //  int typesAreEqual (Type * type1, Type * type2);
  900. //  int subTypeOf (Type * type1,
  901. //                 Mapping<TypeParm,Type> * subst1,
  902. //                 Type * type2,
  903. //                 Mapping<TypeParm,Type> * subst2);
  904. //  int subClassOf (Type * type1,
  905. //                  Mapping<TypeParm,Type> * subst1,
  906. //                  Type * type2,
  907. //                  Mapping<TypeParm,Type> * subst2);
  908. //  int typeArgListsEqual (TypeArg * typeArgList1,
  909. //                         TypeArg * typeArgList2);
  910. //  Type * copyTypeWithSubst (Type * type,
  911. //                            Mapping<TypeParm,Type> * subst);
  912. //  TypeArg * copyArgListWithSubst (TypeArg * argList,
  913. //                                  Mapping<TypeParm,Type> * subst);
  914. //  Parameter * copyParmListWithSubst (Parameter * parmList,
  915. //                                     Mapping<TypeParm,Type> * subst);
  916. //  
  917. //  void evaluateExpressions ();
  918. //  Expression * evalExprsIn (AstNode * node);
  919. //  void evalExprsInStmtList (Statement * stmtList);
  920. //  Expression * evalExprsInUnaryExpr (AstNode * node);
  921. //  Expression * evalExprsInBinaryExpr (AstNode * node);
  922. //  BinaryExpr * newMultiplyExpr (Expression * expr1, Expression * expr2);
  923. //  IntExpr * newIntConst (int value, AstNode * otherNode);
  924. //  DoubleExpr * newDoubleConst (double value, AstNode * otherNode);
  925. //  BoolExpr * newBoolConst (int value, AstNode * otherNode);
  926. //  CharExpr * newCharConst (int value, AstNode * otherNode);
  927. //  
  928. //  void printDispatchTable (Class * cl);
  929. //  
  930. //  #define FIRST_DISPATCH_TABLE_OFFSET 0
  931. //  // The following constant must match a similar constant in the emulator.
  932. //  #define MAX_NUMBER_OF_DISPATCH_TABLE_ENTRIES 100
  933. //  Statement * createInitializers (Decl * declList);
  934. //  Statement * createDestructors (Decl * declList);
  935. //  void addConstructorDestructorCalls (Method * meth);
  936. //  void setFourByteRestriction ();
  937. //  void setRestrictionOnType (Type * type);
  938. //  void assignOffsetsAndEvalExprs ();
  939. //  int assignOffsets2 (int wantPrinting);
  940. //  int needsAlignment (Type * type);
  941. //  int sizeInBytesOfWhole (Type * type);
  942. //  void checkAllTypes ();
  943. //  Type* checkTypesIn (AstNode * node,
  944. //                     Class * currentClass,
  945. //                     AstNode * enclosingMethodOrFunction);
  946. //  void checkTypesInStmtList (Statement * stmtList,
  947. //                             Class * currentClass,
  948. //                             AstNode * enclosingMethodOrFunction);
  949. //  Type * checkTypesInUnaryExpr (AstNode * node,
  950. //                                Class * currentClass,
  951. //                                AstNode * enclosingMethodOrFunction);
  952. //  Type * checkTypesInBinaryExpr (AstNode * node,
  953. //                                 Class * currentClass,
  954. //                                 AstNode * enclosingMethodOrFunction);
  955. //  void rewriteAssignmentEquals (BinaryExpr * binExpr,
  956. //                                int mode,
  957. //                                AstNode * methodOrFunction);
  958. //  int isBool (Type * type);
  959. //  int isInt (Type * type);
  960. //  int isDouble (Type * type);
  961. //  int isChar (Type * type);
  962. //  int isVoid (Type * type);
  963. //  int isNull (Type * type);
  964. //  int isFunction (Type * type);
  965. //  int isArrayPtrType (Type * type);
  966. //  int isVoidPointer (Type * type);
  967. //  int isClassType (Type * type);
  968. //  int isVoidable (Expression * expr);
  969. //  int isLValue (Expression * expr);
  970. //  String * newName (char * str);
  971. //  Decl * newPtrTemp (AstNode * methodOrFunction);
  972. //  Decl * newIntTemp (AstNode * methodOrFunction);
  973. //  Decl * newCharTemp (AstNode * methodOrFunction);
  974. //  Decl * newDoubleTemp (AstNode * methodOrFunction);
  975. //  Decl * newTempWithType (AstNode * methodOrFunction, Type * type);
  976. //  int implicitCastToFrom (Type * toType, Type* fromType);
  977. //  Expression * insertImplicitCast (Type * toType,
  978. //                                   Type* fromType,
  979. //                                   Expression * expr);
  980. //  int explicitCastToFrom (Type * toType, Type* fromType);
  981. //  Expression * insertExplicitCast (Type * toType,
  982. //                                   Type* fromType,
  983. //                                   Expression * expr);
  984. //  UnaryExpr * insertUnary (Expression * expr, int op);
  985. //  void setModeAndLength (Expression * expr, Type * type);
  986. //  TypeArg * createTypeArgListFromTemplateParmList (TypeParm * typeParmList);
  987. //  Type * checkTypesInNew (AstNode * node,
  988. //                          Class * currentCl,
  989. //                          AstNode * enclosingMethOrFun);
  990. //  Type * checkTypesInCall (AstNode * node,
  991. //                           Class * currentCl,
  992. //                           AstNode * enclosingMethOrFun);
  993. //  void checkFallsThru ();
  994. //  int fallsThru (AstNode * node);
  995. //  int fallsThruStmtList (Statement * stmtList);
  996. //  
  997. //  
  998. //  
  999. //  //----------  Data related to IR code generation  ----------
  1000. //  
  1001. //  
  1002. //  
  1003. //  // These are the op-code for the IR instructions.
  1004. //  
  1005. //  typedef enum iropcode {
  1006. //    IRcomment,        // result is a string
  1007. //    IRlabel,          // result is a string such as "Label_43"
  1008. //    IRgoto,           // result is a string such as "Label_43"
  1009. //    IRbyte1,          // result is a string. Arg1 is an integer
  1010. //    IRbyte2,          // result is not used. Arg1 is an integer
  1011. //    IRword1,          // result is a string. Arg1 is an integer
  1012. //    IRword2,          // result is a string. Arg1 is a string
  1013. //    IRword3,          // result is not used. Arg1 is a string
  1014. //    IRdouble,         // result is a string. Arg1 points to a double
  1015. //    IRimport,         // result is a string
  1016. //    IRexport,         // result is a string
  1017. //    IRalign,          // no args
  1018. //    IRdata,           // no args
  1019. //    IRtext,           // no args
  1020. //    IRskip,           // result is a string, Arg1 is an integer
  1021. //    IRskip2,          // result is not used, Arg1 is an integer
  1022. //    IRascii,          // result is a string label; Arg1 points to a "String"
  1023. //    IRassignb,        // Move arg1 to result.  Arg2 is ignored.
  1024. //    IRassignw,        // Move arg1 to result.  Arg2 is ignored.
  1025. //    IRassignf,        // Move arg1 to result.  Arg2 is ignored.
  1026. //    IRmoveBytes,      // Move *arg1 to *result.  Arg2 is is int count.
  1027. //    IRloadAddr,       // result := &arg1
  1028. //    IRloadWordFromByteIndirect,  // result := *arg1, zero-filling high 3 bytes
  1029. //    IRloadByteIndirect,  // result := *arg1
  1030. //    IRloadWordIndirect,  // result := *arg1
  1031. //    IRloadFloatIndirect, // result := *arg1
  1032. //    IRloadStringAddr, // result := addr; arg1 is StringExpr
  1033. //    IRloadLabel,      // result := value; arg1 is a label, e.g., "foo"
  1034. //    IRloadFloat,      // result is 8 byte var, arg1 is DoubleExpr
  1035. //    IRstoreb,         // *result := arg1, byte length
  1036. //    IRstorew,         // *result := arg1, word length
  1037. //    IRstoref,         // *result := arg1, float length
  1038. //    IRgotoiEQ,        // if arg1 == arg2 then goto result (integer arith)
  1039. //    IRgotoiNE,        // if arg1 != arg2 then goto result (integer arith)
  1040. //    IRgotoiLT,        // if arg1 <  arg2 then goto result (integer arith)
  1041. //    IRgotoiLE,        // if arg1 <= arg2 then goto result (integer arith)
  1042. //    IRgotoiGT,        // if arg1 >  arg2 then goto result (integer arith)
  1043. //    IRgotoiGE,        // if arg1 >= arg2 then goto result (integer arith)
  1044. //    IRgotofEQ,        // if arg1 == arg2 then goto result (float arith)
  1045. //    IRgotofNE,        // if arg1 != arg2 then goto result (float arith)
  1046. //    IRgotofLT,        // if arg1 <  arg2 then goto result (float arith)
  1047. //    IRgotofLE,        // if arg1 <= arg2 then goto result (float arith)
  1048. //    IRgotofGT,        // if arg1 >  arg2 then goto result (float arith)
  1049. //    IRgotofGE,        // if arg1 >= arg2 then goto result (float arith)
  1050. //    IRintToDouble,    // result := intToDouble (arg1) 
  1051. //    IRintToBool,      // result := intToBool (arg1)   
  1052. //    IRintToChar,      // result := intToChar (arg1); truncate to 8 bits
  1053. //    IRdoubleToInt,    // result := doubleToInt (arg1) 
  1054. //    IRcharToInt,      // result := charToInt (arg1); sign-extend
  1055. //    IRbyteToWord,     // result := arg1; result is 32 bits; zero-fill
  1056. //    IRcharToDouble,   // result := charToDouble (arg1)
  1057. //    IRcharToBool,     // result := charToBool (arg1)  
  1058. //                      // (There is no ptrToBool; we use intToBool instead)
  1059. //                      // (There is no boolToInt; we use charToInt instead)
  1060. //    IRiadd,           // result := arg1 + arg2, using integer arithmetic.
  1061. //    IRisub,           // result := arg1 - arg2, using integer arithmetic.
  1062. //    IRimul,           // result := arg1 * arg2, using integer arithmetic.
  1063. //    IRidiv,           // result := arg1 DIV arg2, using integer arithmetic.
  1064. //    IRirem,           // result := arg1 REM arg2, using integer arithmetic.
  1065. //    IRineg,           // result := - arg1, using integer arithmetic.
  1066. //  
  1067. //    IRsll,            // result := arg1 << arg2, using integer arithmetic.
  1068. //    IRsra,            // result := arg1 >> arg2, using integer arithmetic.
  1069. //  
  1070. //    IRfadd,           // result := arg1 + arg2, using floating arithmetic.
  1071. //    IRfsub,           // result := arg1 - arg2, using floating arithmetic.
  1072. //    IRfmul,           // result := arg1 * arg2, using floating arithmetic.
  1073. //    IRfdiv,           // result := arg1 / arg2, using floating arithmetic.
  1074. //    IRfneg,           // result := - arg1, using floating arithmetic.
  1075. //  
  1076. //    IRand,            // result := arg1 AND arg2, bitwise, word length.
  1077. //    IRor,             // result := arg1 OR arg2, bitwise, word length.
  1078. //    IRxor,            // result := arg1 XOR arg2, bitwise, word length.
  1079. //  
  1080. //  
  1081. //  // temp...
  1082. //    IRreturnExpr,     // Return with result as the returned value, arg1=lexLev.
  1083. //    IRreturnVoid,     // Return with no value, arg1=lexLev.
  1084. //  
  1085. //  // The following are leftover from PCAT...
  1086. //    IRitof,           // result := intToFloat(arg1).
  1087. //    IRcall,           // Invokes a routine.  Result points to PROC_DECL. 
  1088. //    IRparam,          // arg2 contains a value, arg1 is the arg number.
  1089. //    IRresultTo,       // result :=  value returned from function.
  1090. //    IRmainEntry,      // Result points to a BODY
  1091. //    IRmainExit,       // No args
  1092. //    IRprocEntry,      // result points to the PROC_DECL, arg1 is lex. level.
  1093. //    IRformal,         // result points to a FORMAL; arg1 is the position.
  1094. //    IRallocate,       // result := allocate (arg1).
  1095. //    IRreadInt,        // readInt (result)
  1096. //    IRreadFloat,      // readFloat (result)
  1097. //    IRwriteInt,       // writeInt (result)
  1098. //    IRwriteFloat,     // writeFloat (result)
  1099. //    IRwriteString,    // writeString (result)
  1100. //    IRwriteBoolean,   // writeBoolean (result)
  1101. //    IRwriteNewline    // writeNewline ()
  1102. //  } IRopcode;
  1103. //  
  1104. //  
  1105. //  
  1106. //  // Each IR instruction is called a "Quad" and is represented by
  1107. //  // one of these structures.
  1108. //  
  1109. //  typedef struct irquad Quad;
  1110. //  
  1111. //  struct irquad {
  1112. //    IRopcode    op;
  1113. //    AstNode *    result;
  1114. //    AstNode *    arg1;
  1115. //    AstNode *    arg2;
  1116. //    Quad *    next;
  1117. //  };
  1118. //  
  1119. //  
  1120. //  
  1121. //  // There is a linked-list of all the IR quads.
  1122. //  
  1123. //  extern Quad * firstQuad;
  1124. //  extern Quad * lastQuad;
  1125. //  
  1126. //  
  1127. //  
  1128. //  extern AstNode * currentMethOrFun;
  1129. //  extern Class * currentClass;
  1130. //  extern StringExpr * stringList;
  1131. //  extern DoubleExpr * floatList;
  1132. //  
  1133. //  
  1134. //  
  1135. //  // Constants relating to assigning offsets.
  1136. //  
  1137. //  // #define INITIAL_VARIABLE_OFFSET  -4
  1138. //  // #define VARIABLE_OFFSET_INCR  -4
  1139. //  // #define INITIAL_FORMAL_OFFSET +68
  1140. //  // #define FORMAL_OFFSET_INCR +4
  1141. //  // #define REGISTER_SAVE_AREA_SIZE +64
  1142. //  // #define DISPLAY_REG_SAVE_AREA_OFFSET +64
  1143. //  
  1144. //  
  1145. //  
  1146. //  //----------  Routines in generate.cc  ----------
  1147. //  
  1148. //  char * newLabel (char * str);
  1149. //  Decl * newTemp ();
  1150. //  void genComment (char * commentString);
  1151. //  void gen (IRopcode op, AstNode * result, AstNode * arg1, AstNode * arg2);
  1152. //  void printIR ();
  1153. //  void printOffsets (AstNode * p);
  1154. //  void generateIR ();
  1155. //  void genFunction (Function * fun);
  1156. //  void genMethod (Method * meth);
  1157. //  void genStmts (Statement * stmtList);
  1158. //  void genIfStmt (IfStmt * t);
  1159. //  void genWhileStmt (WhileStmt * t);
  1160. //  void genDoStmt (DoStmt * t);
  1161. //  void genForStmt (ForStmt * t);
  1162. //  void genSwitchStmt (SwitchStmt * t);
  1163. //  void genBreakContinueStmt (BreakContinueStmt * t);
  1164. //  void genReturnStmt (ReturnStmt * t);
  1165. //  void genExprStmt (ExprStmt * t);
  1166. //  AstNode * genExpr (AstNode * node,
  1167. //                     char * trueLabel,
  1168. //                     char * falseLabel,
  1169. //                     AstNode * dest);
  1170. //  AstNode * genUnaryExpr (UnaryExpr * node,
  1171. //                          char * trueLabel,
  1172. //                          char * falseLabel,
  1173. //                          AstNode * dest);
  1174. //  AstNode * genBinaryExpr (BinaryExpr * node,
  1175. //                           char * trueLabel,
  1176. //                           char * falseLabel,
  1177. //                           AstNode * dest);
  1178. //  AstNode * genLValue (AstNode * node, AstNode * dest);
  1179. //  Type * typeOfVariable (AstNode * declOrParameter);
  1180. //  int modeOf (Type * type);
  1181. //  void printDoubleValue (double d);
  1182. //  void printIntInHex (int i);
  1183. //  void printByte (int c);
  1184. //  void genCompareBytes (AstNode * x,
  1185. //                        AstNode * y,
  1186. //                        int count,
  1187. //                        char * trueLabel,
  1188. //                        char * falseLabel);
  1189. //  int isType (AstNode * type);
  1190. //  
  1191. //  
  1192. //  
  1193. //  
  1194. //  //----------  Routines in misc .c files.  ----------
  1195. //  
  1196. //  // void peephole ();                        // peephole.c
  1197. //  // void emitAll ();                         // emit.c
  1198.  
  1199. ***************/
  1200.