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 / Beta / main.h < prev    next >
C/C++ Source or Header  |  2007-09-04  |  46KB  |  1,225 lines

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