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 / ir.cc < prev    next >
C/C++ Source or Header  |  2006-09-25  |  104KB  |  3,688 lines

  1. // ir.cc  --  Methods for IR classes
  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. //    05/24/03 - Harry H. Porter III
  16. //
  17. // Modifcations by:
  18. //   03/15/06 - Harry H. Porter III
  19. //
  20. //
  21.  
  22. #include "main.h"
  23.  
  24.  
  25.  
  26. // within16Bits (int) --> bool
  27. //
  28. // This routine returns true if the argument can be represented as a 2's
  29. // complement signed, 16-bit integer, and can therefore be put into the
  30. // immediate field of instructions like:
  31. //        mul     r1,xxxxx,r2
  32. //        mov     xxxxx, r3
  33. //        load    [r14+xxxxx],r5
  34. //
  35. int within16Bits (int i) {
  36.   if ((i <= 32767) && (i >= -32768)) {
  37.     return 1;
  38.   } else {
  39.     return 0;
  40.   }
  41. }
  42.  
  43.  
  44.  
  45. // getIntoReg4 (src, reg)
  46. //
  47. // This routine generates the code necessary to get the "src" into "reg".
  48. // It moves the 4 bytes of data into "reg".
  49. //
  50. // "src" should be:
  51. //      LOCAL
  52. //      PARAMETER
  53. //      GLOBAL
  54. //      CLASS_FIELD
  55. //      INT_CONST
  56. //
  57. void getIntoReg4 (AstNode * src, char * reg) {
  58.   int i;
  59.   if ((src->op == LOCAL) || (src->op == PARAMETER)) {
  60.     i = ((VarDecl *) src)->offset;
  61.     if (within16Bits (i)) {
  62.       fprintf (outputFile, "\tload\t[r14+%d],%s\n", i, reg);
  63.     } else {
  64.       // printf ("16-BIT OVERFLOW IN VARIABLE1: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  65.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg);
  66.       fprintf (outputFile, "\tload\t[r14+%s],%s\n", reg, reg);
  67.     }
  68.   } else if (src->op == CLASS_FIELD) {
  69.     i = ((VarDecl *) src)->offset;
  70.     if (within16Bits (i)) {
  71.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg);
  72.       fprintf (outputFile, "\tload\t[%s+%d],%s\n", reg, i, reg);
  73.     } else {
  74.       // printf ("16-BIT OVERFLOW IN VARIABLE2: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  75.       fprintf (outputFile, "\tload\t[r14+8],r11\n");
  76.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg);
  77.       fprintf (outputFile, "\tload\t[r11+%s],%s\n", reg, reg);
  78.     }
  79.   } else if (src->op == GLOBAL) {
  80.     fprintf (outputFile, "\tset\t%s,%s\n", ((VarDecl *) src)->id->chars, reg);
  81.     fprintf (outputFile, "\tload\t[%s],%s\n", reg, reg);
  82.   } else if (src->op == INT_CONST) {
  83.     i = ((IntConst *) src)->ivalue;
  84.     if (within16Bits (i)) {
  85.       fprintf (outputFile, "\tmov\t%d,%s\n", i, reg);
  86.     } else if (i == 0x80000000) {
  87.       fprintf (outputFile, "\tset\t0x80000000,%s\n", reg);
  88.     } else {
  89.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg);
  90.     }
  91.   } else {
  92.     printf ("\nsrc->op = %s\n", symbolName (src->op));
  93.     programLogicError ("Unknown node in getIntoReg4");
  94.   }
  95. }
  96.  
  97.  
  98.  
  99. // getIntoReg1 (src, reg)
  100. //
  101. // This routine generates the code necessary to get the "src" into "reg".
  102. // It moves the 1 byte of data into the LSB of "reg1" and clears the hi-order
  103. // 24 bits.
  104. //
  105. // "src" should be:
  106. //      LOCAL
  107. //      PARAMETER
  108. //      GLOBAL
  109. //      CLASS_FIELD
  110. //      CHAR_CONST
  111. //      BOOL_CONST
  112. //
  113. void getIntoReg1 (AstNode * src, char * reg) {
  114.   int i;
  115.   if ((src->op == LOCAL) || (src->op == PARAMETER)) {
  116.     i = ((VarDecl *) src)->offset;
  117.     if (within16Bits (i)) {
  118.       fprintf (outputFile, "\tloadb\t[r14+%d],%s\n", i, reg);
  119.     } else {
  120.       // printf ("16-BIT OVERFLOW IN VARIABLE3: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  121.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg);
  122.       fprintf (outputFile, "\tloadb\t[r14+%s],%s\n", reg, reg);
  123.     }
  124.   } else if (src->op == CLASS_FIELD) {
  125.     i = ((VarDecl *) src)->offset;
  126.     if (within16Bits (i)) {
  127.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg);
  128.       fprintf (outputFile, "\tloadb\t[%s+%d],%s\n", reg, i, reg);
  129.     } else {
  130.       // printf ("16-BIT OVERFLOW IN VARIABLE4: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  131.       fprintf (outputFile, "\tload\t[r14+8],r11\n");
  132.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg);
  133.       fprintf (outputFile, "\tloadb\t[r11+%s],%s\n", reg, reg);
  134.     }
  135.   } else if (src->op == GLOBAL) {
  136.     fprintf (outputFile, "\tset\t%s,%s\n", ((VarDecl *) src)->id->chars, reg);
  137.     fprintf (outputFile, "\tloadb\t[%s],%s\n", reg, reg);
  138.   } else if (src->op == CHAR_CONST) {
  139.     i = ((IntConst *) src)->ivalue;
  140.     fprintf (outputFile, "\tmov\t%d,%s\n", i, reg);
  141.   } else if (src->op == BOOL_CONST) {
  142.     i = ((BoolConst *) src)->ivalue;
  143.     fprintf (outputFile, "\tmov\t%d,%s\n", i, reg);
  144.   } else {
  145.     printf ("\nsrc->op = %s\n", symbolName (src->op));
  146.     programLogicError ("Unknown node in getIntoReg1");
  147.   }
  148. }
  149.  
  150.  
  151.  
  152. // getIntoReg8 (src, freg1, reg2)
  153. //
  154. // This routine generates the code necessary to get the "src" into "freg1", using
  155. // "reg2" if necessary.  It moves the 8 byte of data into the floating reg "freg1".
  156. //
  157. // "src" should be:
  158. //      LOCAL
  159. //      PARAMETER
  160. //      GLOBAL
  161. //      CLASS_FIELD
  162. //      DOUBLE_CONST
  163. //
  164. void getIntoReg8 (AstNode * src, char * freg1, char * reg2) {
  165.   int i;
  166.   DoubleConst * doubleConst;
  167.   if ((src->op == LOCAL) || (src->op == PARAMETER)) {
  168.     i = ((VarDecl *) src)->offset;
  169.     if (within16Bits (i)) {
  170.       fprintf (outputFile, "\tfload\t[r14+%d],%s\n", i, freg1);
  171.     } else {
  172.       // printf ("16-BIT OVERFLOW IN VARIABLE5: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  173.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  174.       fprintf (outputFile, "\tfload\t[r14+%s],%s\n", reg2, freg1);
  175.     }
  176.   } else if (src->op == CLASS_FIELD) {
  177.     i = ((VarDecl *) src)->offset;
  178.     if (within16Bits (i)) {
  179.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg2);
  180.       fprintf (outputFile, "\tfload\t[%s+%d],%s\n", reg2, i, freg1);
  181.     } else {
  182.       // printf ("16-BIT OVERFLOW IN VARIABLE6: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  183.       fprintf (outputFile, "\tload\t[r14+8],r11\n");
  184.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  185.       fprintf (outputFile, "\tfload\t[r11+%s],%s\n", reg2, freg1);
  186.     }
  187.   } else if (src->op == GLOBAL) {
  188.     fprintf (outputFile, "\tset\t%s,%s\n", ((VarDecl *) src)->id->chars, reg2);
  189.     fprintf (outputFile, "\tfload\t[%s],%s\n", reg2, freg1);
  190.   } else if (src->op == DOUBLE_CONST) {
  191.     doubleConst = (DoubleConst *) src;
  192.     fprintf (outputFile, "\tset\t%s,%s\n", doubleConst->nameOfConstant, reg2);
  193.     fprintf (outputFile, "\tfload\t[%s],%s\n", reg2, freg1);
  194.   } else {
  195.     printf ("\nsrc->op = %s\n", symbolName (src->op));
  196.     programLogicError ("Unknown node in getIntoReg8");
  197.   }
  198. }
  199.  
  200.  
  201.  
  202. // getAddrOfVarIntoReg (src, reg1)
  203. //
  204. // This routine generates the code necessary to get the address of "src" into
  205. // "reg1".  The "src" may be:
  206. //      LOCAL
  207. //      PARAMETER
  208. //      GLOBAL
  209. //      CLASS_FIELD
  210. //
  211. void getAddrOfVarIntoReg (AstNode * src, char * reg1) {
  212.   int i;
  213.   if ((src->op == LOCAL) || (src->op == PARAMETER)) {
  214.     i = ((VarDecl *) src)->offset;
  215.     if (within16Bits (i)) {
  216.       fprintf (outputFile, "\tadd\tr14,%d,%s\n", i, reg1);
  217.     } else {
  218.       // printf ("16-BIT OVERFLOW IN VARIABLE7: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  219.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg1);
  220.       fprintf (outputFile, "\tadd\tr14,%s,%s\n", reg1, reg1);
  221.     }
  222.   } else if (src->op == GLOBAL) {
  223.     fprintf (outputFile, "\tset\t%s,%s\n", ((VarDecl *) src)->id->chars, reg1);
  224.   } else if (src->op == CLASS_FIELD) {
  225.     i = ((VarDecl *) src)->offset;
  226.     // printf ("ClassField = %s   offset = %d\n", ((VarDecl *) src)->id->chars, i);
  227.     if (within16Bits (i)) {
  228.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg1);
  229.       fprintf (outputFile, "\tadd\t%s,%d,%s\n", reg1, i, reg1);
  230.     } else {
  231.       // printf ("16-BIT OVERFLOW IN VARIABLE8: %s, offset = %d\n", ((VarDecl *) src)->id->chars, i);
  232.       fprintf (outputFile, "\tset\t%d,r11\n", i);
  233.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg1);
  234.       fprintf (outputFile, "\tadd\t%s,r11,%s\n", reg1, reg1);
  235.     }
  236.   } else {
  237.     printf ("\nsrc->op = %s\n", symbolName (src->op));
  238.     programLogicError ("Unknown node in getAddrOfVarIntoReg");
  239.   }
  240. }
  241.  
  242.  
  243.  
  244. // storeFromReg4 (dest, reg1, reg2)
  245. //
  246. // This routine generates the code necessary to move a word from "reg1" to
  247. // the "dest" location, using "reg2" if necessary.
  248. //
  249. // "dest" should be:
  250. //      LOCAL
  251. //      PARAMETER
  252. //      GLOBAL
  253. //      CLASS_FIELD
  254. //
  255. void storeFromReg4 (VarDecl * dest, char * reg1, char * reg2) {
  256.   int i;
  257.   if ((dest->op == LOCAL) || (dest->op == PARAMETER)) {
  258.     i = dest->offset;
  259.     if (within16Bits (i)) {
  260.       fprintf (outputFile, "\tstore\t%s,[r14+%d]\n", reg1, i);
  261.     } else {
  262.       // printf ("16-BIT OVERFLOW IN VARIABLE9: %s, offset = %d\n", ((VarDecl *) dest)->id->chars, i);
  263.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  264.       fprintf (outputFile, "\tstore\t%s,[r14+%s]\n", reg1, reg2);
  265.     }
  266.   } else if (dest->op == CLASS_FIELD) {
  267.     i = dest->offset;
  268.     if (within16Bits (i)) {
  269.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg2);
  270.       fprintf (outputFile, "\tstore\t%s,[%s+%d]\n", reg1, reg2, i);
  271.     } else {
  272.       // printf ("16-BIT OVERFLOW IN VARIABLE10: %s, offset = %d\n", ((VarDecl *) dest)->id->chars, i);
  273.       fprintf (outputFile, "\tload\t[r14+8],r11\n");
  274.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  275.       fprintf (outputFile, "\tstore\t%s,[r11+%s]\n", reg1, reg2);
  276.     }
  277.   } else if (dest->op == GLOBAL) {
  278.     fprintf (outputFile, "\tset\t%s,%s\n", dest->id->chars, reg2);
  279.     fprintf (outputFile, "\tstore\t%s,[%s]\n", reg1, reg2);
  280.   } else {
  281.     printf ("\ndest->op = %s\n", symbolName (dest->op));
  282.     programLogicError ("Unknown node in storeFromReg4");
  283.   }
  284. }
  285.  
  286.  
  287.  
  288. // storeFromReg1 (dest, reg1, reg2)
  289. //
  290. // This routine generates the code necessary to move 1 byte from "reg1" to
  291. // the "dest" location, using "reg2" if necessary.
  292. //
  293. // "dest" should be:
  294. //      LOCAL
  295. //      PARAMETER
  296. //      GLOBAL
  297. //      CLASS_FIELD
  298. //
  299. void storeFromReg1 (VarDecl * dest, char * reg1, char * reg2) {
  300.   int i;
  301.   if ((dest->op == LOCAL) || (dest->op == PARAMETER)) {
  302.     i = dest->offset;
  303.     if (within16Bits (i)) {
  304.       fprintf (outputFile, "\tstoreb\t%s,[r14+%d]\n", reg1, i);
  305.     } else {
  306.       // printf ("16-BIT OVERFLOW IN VARIABLE11: %s, offset = %d\n", ((VarDecl *) dest)->id->chars, i);
  307.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  308.       fprintf (outputFile, "\tstoreb\t%s,[r14+%s]\n", reg1, reg2);
  309.     }
  310.   } else if (dest->op == CLASS_FIELD) {
  311.     i = dest->offset;
  312.     if (within16Bits (i)) {
  313.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg2);
  314.       fprintf (outputFile, "\tstoreb\t%s,[%s+%d]\n", reg1, reg2, i);
  315.     } else {
  316.       // printf ("16-BIT OVERFLOW IN VARIABLE12: %s, offset = %d\n", ((VarDecl *) dest)->id->chars, i);
  317.       fprintf (outputFile, "\tload\t[r14+8],r11\n");
  318.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  319.       fprintf (outputFile, "\tstoreb\t%s,[r11+%s]\n", reg1, reg2);
  320.     }
  321.   } else if (dest->op == GLOBAL) {
  322.     fprintf (outputFile, "\tset\t%s,%s\n", dest->id->chars, reg2);
  323.     fprintf (outputFile, "\tstoreb\t%s,[%s]\n", reg1, reg2);
  324.   } else {
  325.     printf ("\ndest->op = %s\n", symbolName (dest->op));
  326.     programLogicError ("Unknown node in storeFromReg1");
  327.   }
  328. }
  329.  
  330.  
  331.  
  332. // storeFromReg8 (dest, reg1, reg2)
  333. //
  334. // This routine generates the code necessary to move 8 bytes from "freg1" to
  335. // the "dest" location, using "reg2" if necessary.
  336. //
  337. // "dest" should be:
  338. //      LOCAL
  339. //      PARAMETER
  340. //      GLOBAL
  341. //      CLASS_FIELD
  342. //
  343. void storeFromReg8 (VarDecl * dest, char * freg1, char * reg2) {
  344.   int i;
  345.   if ((dest->op == LOCAL) || (dest->op == PARAMETER)) {
  346.     i = dest->offset;
  347.     if (within16Bits (i)) {
  348.       fprintf (outputFile, "\tfstore\t%s,[r14+%d]\n", freg1, i);
  349.     } else {
  350.       // printf ("16-BIT OVERFLOW IN VARIABLE13: %s, offset = %d\n", ((VarDecl *) dest)->id->chars, i);
  351.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  352.       fprintf (outputFile, "\tfstore\t%s,[r14+%s]\n", freg1, reg2);
  353.     }
  354.   } else if (dest->op == CLASS_FIELD) {
  355.     i = dest->offset;
  356.     if (within16Bits (i)) {
  357.       fprintf (outputFile, "\tload\t[r14+8],%s\n", reg2);
  358.       fprintf (outputFile, "\tfstore\t%s,[%s+%d]\n", freg1, reg2, i);
  359.     } else {
  360.       // printf ("16-BIT OVERFLOW IN VARIABLE14: %s, offset = %d\n", ((VarDecl *) dest)->id->chars, i);
  361.       fprintf (outputFile, "\tload\t[r14+8],r11\n");
  362.       fprintf (outputFile, "\tset\t%d,%s\n", i, reg2);
  363.       fprintf (outputFile, "\tfstore\t%s,[r11+%s]\n", freg1, reg2);
  364.     }
  365.   } else if (dest->op == GLOBAL) {
  366.     fprintf (outputFile, "\tset\t%s,%s\n", dest->id->chars, reg2);
  367.     fprintf (outputFile, "\tfstore\t%s,[%s]\n", freg1, reg2);
  368.   } else {
  369.     printf ("\ndest->op = %s\n", symbolName (dest->op));
  370.     programLogicError ("Unknown node in storeFromReg8");
  371.   }
  372. }
  373.  
  374.  
  375.  
  376. // overflowTest ()
  377. //
  378. // This routine generates:
  379. //       bvs   _runtimeErrorOverflow
  380. //
  381. void overflowTest () {
  382.     fprintf (outputFile, "\tbvs\t_runtimeErrorOverflow\n");
  383. }
  384.  
  385.  
  386.  
  387. //
  388. // printIR
  389. //
  390. // This routine runs through the IR statements and prints them.
  391. //
  392. void printIR () {
  393.     IR * inst;
  394.     for (inst = firstInstruction; inst; inst = inst->next) {
  395.       inst->print ();
  396.       // fflush (outputFile);      // Useful during debugging...
  397.     }
  398. }
  399.  
  400.  
  401.  
  402. //
  403. // printANode (node)
  404. //
  405. // This routine is passed a pointer to:
  406. //    Local
  407. //    Global
  408. //    Parameter
  409. //    ClassField
  410. //    IntConst
  411. //    DoubleConst
  412. // It prints the node into the output file.
  413. //
  414. void printANode (AstNode * node) {
  415.     double r, rval;
  416.     r = 0.0;
  417.  
  418.   if (node==NULL) {
  419.     fprintf (outputFile, "*****  NULL  *****");
  420.     // For debugging, keep going.  Later, perhaps we should just abort...
  421.     //     programLogicError ("printANode called with node == NULL");
  422.   }
  423.  
  424.   switch (node->op) {
  425.  
  426.     case INT_CONST:
  427.       fprintf (outputFile, "%d", ((IntConst *) node)->ivalue);
  428.       return;
  429.  
  430.     case DOUBLE_CONST:
  431.       rval = ((DoubleConst *) node)->rvalue;
  432.       if (rval == (-1.0) / r) {
  433.         fprintf (outputFile, "<NegativeInfinity>");
  434.       } else if (rval == (+1.0) / r) {
  435.         fprintf (outputFile, "<PositiveInfinity>");
  436.       } else if (isnan (rval)) {
  437.         fprintf (outputFile, "<Not-A-Number>");
  438.       } else if (rval == 0.0 && 1.0/rval < 0.0) {
  439.         fprintf (outputFile, "<NegativeZero>");
  440.       } else {
  441.         fprintf (outputFile, "%.16gD", rval);
  442.       }
  443.       return;
  444.  
  445.     case CHAR_CONST:
  446.       fprintf (outputFile, "%d", ((CharConst *) node)->ivalue);
  447.       return;
  448.  
  449.     case BOOL_CONST:
  450.       fprintf (outputFile, "%d", ((BoolConst *) node)->ivalue);
  451.       return;
  452.  
  453.     case GLOBAL:
  454.       printString (outputFile, ((Global *) node)->id);
  455.       return;
  456.  
  457.     case LOCAL:
  458.       printString (outputFile, ((Local *) node)->id);
  459.       return;
  460.  
  461.     case PARAMETER:
  462.       printString (outputFile, ((Parameter *) node)->id);
  463.       return;
  464.  
  465.     case CLASS_FIELD:
  466.       printString (outputFile, ((ClassField *) node)->id);
  467.       return;
  468.  
  469.     default:
  470.       printf ("\nnode->op = %s\n", symbolName (node->op));
  471.       programLogicError ("Unkown op in printANode");
  472.  
  473.   }
  474.  
  475.   programLogicError ("All cases should have returned in printANode");
  476.   return;
  477.  
  478. }
  479.  
  480.  
  481.  
  482. //----------  IR  ----------
  483.  
  484. void IR::print () {
  485.   programLogicError ("Method 'print' should have been overridden (IR)");
  486. }
  487.  
  488. void linkIR (IR * inst) {
  489.   // if (inst->next != NULL) {
  490.   //   programLogicError ("Problems in linkIR (1)");
  491.   // }
  492.   if (firstInstruction == NULL) {
  493.     firstInstruction = inst;
  494.     // if (lastInstruction != NULL) {
  495.     //   programLogicError ("Problems in linkIR (2)");
  496.     // }
  497.   }
  498.   if (lastInstruction != NULL) {
  499.     lastInstruction->next = inst;
  500.   }
  501.   lastInstruction = inst;
  502. }
  503.  
  504.  
  505.  
  506. //----------  Comment  ----------
  507.  
  508. void Comment::print () {
  509.   fprintf (outputFile, "! %s\n", str);
  510. }
  511.  
  512. void IRComment (char * str) {
  513.   linkIR (new Comment (str));
  514. }
  515.  
  516.  
  517.  
  518. //----------  Comment3  ----------
  519.  
  520. void Comment3::print () {
  521.   fprintf (outputFile, "! %s%d...\n", str, ivalue);
  522. }
  523.  
  524. void IRComment3 (char * str, int i) {
  525.   linkIR (new Comment3 (str, i));
  526. }
  527.  
  528.  
  529.  
  530. //----------  Goto  ----------
  531. void Goto::print () {
  532.   Label * lab;
  533.   // If we happen to be jumping to the very next IR instruction...
  534.   if (next && next->op == OPLabel) {
  535.     lab = (Label *) this->next;
  536.     if (lab->label == label) {
  537.       fprintf (outputFile, "!", label);   // Comment the jump out
  538.     }
  539.   }
  540.   // Else generate a "jmp" instruction...
  541.   fprintf (outputFile, "\tjmp\t%s\n", label);
  542. }
  543.  
  544. void IRGoto (char * label) {
  545.   linkIR (new Goto (label));
  546. }
  547.  
  548.  
  549.  
  550. //----------  Goto2  ----------
  551. void Goto2::print () {
  552.   fprintf (outputFile, "\tjmp\t%s\t! %d:\t%s\n", label, offset, selector);
  553. }
  554.  
  555. void IRGoto2 (char * label, int offset, char * selector) {
  556.   linkIR (new Goto2 (label, offset, selector));
  557. }
  558.  
  559.  
  560.  
  561. //----------  Label  ----------
  562.  
  563. void Label::print () {
  564.   fprintf (outputFile, "%s:\n", label);
  565. }
  566.  
  567. void IRLabel (char * label) {
  568.   linkIR (new Label (label));
  569. }
  570.  
  571.  
  572.  
  573. //----------  Import  ----------
  574.  
  575. void Import::print () {
  576.   fprintf (outputFile, "\t.import\t%s\n", name);
  577. }
  578.  
  579. void IRImport (char * nam) {
  580.   linkIR (new Import (nam));
  581. }
  582.  
  583.  
  584.  
  585. //----------  Export  ----------
  586.  
  587. void Export::print () {
  588.   fprintf (outputFile, "\t.export\t%s\n", name);
  589. }
  590.  
  591. void IRExport (char * nam) {
  592.   linkIR (new Export (nam));
  593. }
  594.  
  595.  
  596.  
  597. //----------  Data  ----------
  598.  
  599. void Data::print () {
  600.   fprintf (outputFile, "\t.data\n");
  601. }
  602.  
  603. void IRData () {
  604.   linkIR (new Data ());
  605. }
  606.  
  607.  
  608.  
  609. //----------  Text  ----------
  610.  
  611. void Text::print () {
  612.   fprintf (outputFile, "\t.text\n");
  613. }
  614.  
  615. void IRText () {
  616.   linkIR (new Text ());
  617. }
  618.  
  619.  
  620.  
  621. //----------  Align  ----------
  622.  
  623. void Align::print () {
  624.   fprintf (outputFile, "\t.align\n");
  625. }
  626.  
  627. void IRAlign () {
  628.   linkIR (new Align ());
  629. }
  630.  
  631.  
  632.  
  633. //----------  Skip  ----------
  634.  
  635. void Skip::print () {
  636.   fprintf (outputFile, "\t.skip\t%d\n", byteCount);
  637. }
  638.  
  639. void IRSkip (int i) {
  640.   linkIR (new Skip (i));
  641. }
  642.  
  643.  
  644.  
  645. //----------  Byte  ----------
  646.  
  647. void Byte::print () {
  648.   fprintf (outputFile, "\t.byte\t%d", byteValue);
  649.   if (byteValue >= 32 && byteValue <= 126) {
  650.     fprintf (outputFile, "\t\t\t! '");
  651.     printChar (outputFile, byteValue);
  652.     fprintf (outputFile, "'\n");
  653.   } else if (byteValue <= 9) {
  654.     fprintf (outputFile, "\n");
  655.   } else {
  656.     int i = intToHexChar ((byteValue >>4) & 0x0000000f);
  657.     int j = intToHexChar (byteValue & 0x0000000f);
  658.     fprintf (outputFile, "\t\t\t! (%c%c in hex)\n", i, j);
  659.   }
  660. }
  661.  
  662. void IRByte (int i) {
  663.   linkIR (new Byte (i));
  664. }
  665.  
  666.  
  667.  
  668. //----------  Word  ----------
  669.  
  670. void Word::print () {
  671.   if (wordValue == 0) {
  672.     fprintf (outputFile, "\t.word\t0\n");
  673.   } else {
  674.     fprintf (outputFile, "\t.word\t0x%08x\t\t! decimal value = %d\n",
  675.                          wordValue, wordValue);
  676.   }
  677. }
  678.  
  679. void IRWord (int i) {
  680.   linkIR (new Word (i));
  681. }
  682.  
  683.  
  684.  
  685. //----------  Word2  ----------
  686.  
  687. void Word2::print () {
  688.   fprintf (outputFile, "\t.word\t%s\n", symbol);
  689. }
  690.  
  691. void IRWord2 (char * s) {
  692.   linkIR (new Word2 (s));
  693. }
  694.  
  695.  
  696.  
  697. //----------  Word3  ----------
  698.  
  699. void Word3::print () {
  700.   fprintf (outputFile, "\t.word\t%d\t\t! %s\n", wordValue, comment);
  701. }
  702.  
  703. void IRWord3 (int i, char * s) {
  704.   linkIR (new Word3 (i, s));
  705. }
  706.  
  707.  
  708.  
  709. //----------  LoadAddr  ----------
  710.  
  711. void LoadAddr::print () {
  712.   fprintf (outputFile, "!   ");
  713.   printANode (dest);
  714.   fprintf (outputFile, " = %s\n", stringName);
  715.   fprintf (outputFile, "\tset\t%s,r1\n", stringName);
  716.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  717. }
  718.  
  719. void IRLoadAddr (AstNode * d, char * s) {
  720.   linkIR (new LoadAddr (d, s));
  721. }
  722.  
  723.  
  724.  
  725. //----------  LoadAddrIndirect  ----------
  726.  
  727. void LoadAddrIndirect::print () {
  728.   fprintf (outputFile, "!   *");
  729.   printANode (dest);
  730.   fprintf (outputFile, " = %s\n", stringName);
  731.   fprintf (outputFile, "\tset\t%s,r1\n", stringName);
  732.   getIntoReg4 (dest, "r2");
  733.   fprintf (outputFile, "\tstore\tr1,[r2]\n");
  734.  
  735.  
  736. }
  737.  
  738. void IRLoadAddrIndirect (AstNode * d, char * s) {
  739.   linkIR (new LoadAddrIndirect (d, s));
  740. }
  741.  
  742.  
  743.  
  744. //----------  LoadAddr2  ----------
  745.  
  746. void LoadAddr2::print () {
  747.   fprintf (outputFile, "!   ");
  748.   printANode (dest);
  749.   fprintf (outputFile, " = &");
  750.   printANode (src);
  751.   fprintf (outputFile, "\n");
  752.   getAddrOfVarIntoReg (src, "r1");
  753.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  754. }
  755.  
  756. void IRLoadAddr2 (VarDecl * d, AstNode * s) {
  757.   linkIR (new LoadAddr2 (d, s));
  758. }
  759.  
  760.  
  761.  
  762. //----------  LoadAddrWithIncr  ----------
  763.  
  764. void LoadAddrWithIncr::print () {
  765.   fprintf (outputFile, "!   ");
  766.   printANode (dest);
  767.   fprintf (outputFile, " = ");
  768.   printANode (src);
  769.   fprintf (outputFile, ".%s\n", fieldInit->id->chars);
  770.   getAddrOfVarIntoReg (src, "r1");
  771.   if (within16Bits (fieldInit->offset)) {
  772.     fprintf (outputFile, "\tadd\tr1,%d,r1\n", fieldInit->offset);
  773.   } else {
  774.     fprintf (outputFile, "\tset\t0x%08x,r1\n", fieldInit->offset);
  775.     fprintf (outputFile, "\tadd\tr1,r2,r1\n");
  776.   }
  777.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  778. }
  779.  
  780. void IRLoadAddrWithIncr (VarDecl * dest, AstNode * src, FieldInit * fieldInit) {
  781.   linkIR (new LoadAddrWithIncr (dest, src, fieldInit));
  782. }
  783.  
  784.  
  785.  
  786. //----------  LoadAddrWithIncr2  ----------
  787.  
  788. void LoadAddrWithIncr2::print () {
  789.   fprintf (outputFile, "!   ");
  790.   printANode (dest);
  791.   fprintf (outputFile, " = (*");
  792.   printANode (src);
  793.   fprintf (outputFile, ") . %s\n", fieldInit->id->chars);
  794.   getIntoReg4 (src, "r1");
  795.   if (within16Bits (fieldInit->offset)) {
  796.     fprintf (outputFile, "\tadd\tr1,%d,r1\n", fieldInit->offset);
  797.   } else {
  798.     fprintf (outputFile, "\tset\t0x%08x,r1\n", fieldInit->offset);
  799.     fprintf (outputFile, "\tadd\tr1,r2,r1\n");
  800.   }
  801.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  802. }
  803.  
  804. void IRLoadAddrWithIncr2 (VarDecl * d, AstNode * s, FieldInit * f) {
  805.   linkIR (new LoadAddrWithIncr2 (d, s, f));
  806. }
  807.  
  808.  
  809.  
  810. //----------  Double  ----------
  811.  
  812. void Double::print () {
  813.   int * p = (int *) &doubleValue;
  814.   double r = 0.0;
  815.   fprintf (outputFile, "\t.word\t0x%08x\t\t! double value = ", *p);
  816.   if (doubleValue == (-1.0) / r) {
  817.     fprintf (outputFile, "NegInf\n");
  818.   } else if (doubleValue == (+1.0) / r) {
  819.     fprintf (outputFile, "PosInf\n");
  820.   } else if (isnan (doubleValue)) {
  821.     fprintf (outputFile, "NaN\n");
  822.   } else if (doubleValue == 0.0 && 1.0/doubleValue < 0.0) {
  823.     fprintf (outputFile, "NegZero\n");
  824.   } else {
  825.     fprintf (outputFile, "%.15g\n", doubleValue);
  826.   }
  827.   p++;
  828.   fprintf (outputFile, "\t.word\t0x%08x\t\t! .\n", *p);
  829. }
  830.  
  831. void IRDouble (double r) {
  832.   linkIR (new Double (r));
  833. }
  834.  
  835.  
  836.  
  837. //----------  Call  ----------
  838.  
  839. void Call::print () {
  840.   fprintf (outputFile, "\tcall\t%s\n", name);
  841. }
  842.  
  843. void IRCall (char * n) {
  844.   linkIR (new Call (n));
  845. }
  846.  
  847.  
  848.  
  849. //----------  CallIndirect  ----------
  850.  
  851. void CallIndirect::print () {
  852.   getIntoReg4 (varDesc, "r1");
  853.   fprintf (outputFile, "\tcmp\tr1,0\n");
  854.   fprintf (outputFile, "\tbe\t_runtimeErrorNullPointerDuringCall\n");
  855.   fprintf (outputFile, "\tcall\tr1\n");
  856. }
  857.  
  858. void IRCallIndirect (VarDecl * vd) {
  859.   linkIR (new CallIndirect (vd));
  860. }
  861.  
  862.  
  863.  
  864. //----------  Debug  ----------
  865.  
  866. void Debug::print () {
  867.   fprintf (outputFile, "\tdebug\n");
  868. }
  869.  
  870. void IRDebug () {
  871.   linkIR (new Debug ());
  872. }
  873.  
  874.  
  875.  
  876. //----------  Halt  ----------
  877.  
  878. void Halt::print () {
  879.   fprintf (outputFile, "\thalt\n");
  880. }
  881.  
  882. void IRHalt () {
  883.   linkIR (new Halt ());
  884. }
  885.  
  886.  
  887.  
  888. //----------  SetLineNumber  ----------
  889.  
  890. void SetLineNumber::print () {
  891.   if (within16Bits (lineNumber)) {
  892.     fprintf (outputFile, "\tmov\t%d,r13\t\t! source line %d\n",
  893.              lineNumber, lineNumber);
  894.   } else {
  895.     fprintf (outputFile, "\tset\t%d,r13\t\t! source line %d\n",
  896.              lineNumber, lineNumber);
  897.   }
  898.   fprintf (outputFile, "\tmov\t\"\\0\\0%s\",r10\n", stmtCode);
  899.  
  900. }
  901.  
  902. void IRSetLineNumber (int i, char * s) {
  903.   linkIR (new SetLineNumber (i, s));
  904. }
  905.  
  906.  
  907.  
  908. //----------  FunctionEntry  ----------
  909.  
  910. void FunctionEntry::print () {
  911.   int i;
  912.   char * label;
  913.  
  914.   fprintf (outputFile, "\tpush\tr14\n");
  915.   fprintf (outputFile, "\tmov\tr15,r14\n");
  916.   fprintf (outputFile, "\tpush\tr13\n");
  917.   fprintf (outputFile, "\tset\t%s,r1\n",
  918.            appendStrings ("_RoutineDescriptor_",fun->newName,""));
  919.   fprintf (outputFile, "\tpush\tr1\n");
  920.   if (fun->frameSize % 4 != 0) {
  921.     programLogicError ("fun->frameSize should be a multiple of 4");
  922.   }
  923.  
  924.   // Generate code to zero-out the var & arg portion of this frame...
  925.   i = fun->frameSize / 4;
  926.   if (i > 0) {
  927.     label = newLabel ();
  928.     if (within16Bits (i)) {
  929.       fprintf (outputFile, "\tmov\t%d,r1\n", i);
  930.     } else {
  931.       // printf ("16-bit OVERFLOW IN FUNCTION: %s,  i = %d\n", fun->id->chars, i);
  932.       fprintf (outputFile, "\tset\t%d,r1\n", i);
  933.     }
  934.     fprintf (outputFile, "%s:\n", label);
  935.     fprintf (outputFile, "\tpush\tr0\n");
  936.     fprintf (outputFile, "\tsub\tr1,1,r1\n");
  937.     fprintf (outputFile, "\tbne\t%s\n", label);
  938.   }
  939.  
  940. }
  941.  
  942. void IRFunctionEntry (Function * fun) {
  943.   linkIR (new FunctionEntry (fun));
  944. }
  945.  
  946.  
  947.  
  948. //----------  FunctionReturn  ----------
  949.  
  950. void FunctionReturn::print () {
  951.   int i = fun->frameSize+4;
  952.   if (within16Bits (i)) {
  953.     fprintf (outputFile, "\tadd\tr15,%d,r15\n", i);
  954.   } else {
  955.     // printf ("16-bit OVERFLOW IN FUNCTION RETURN: %s   i=%d\n", fun->id->chars, i);
  956.     fprintf (outputFile, "\tset\t%d,r1\n", i);
  957.     fprintf (outputFile, "\tadd\tr15,r1,r15\n");
  958.   }
  959.   fprintf (outputFile, "\tpop\tr13\n");
  960.   fprintf (outputFile, "\tpop\tr14\n");
  961.   fprintf (outputFile, "\tret\n");
  962. }
  963.  
  964. void IRFunctionReturn (Function * fun) {
  965.   linkIR (new FunctionReturn (fun));
  966. }
  967.  
  968.  
  969.  
  970. //----------  MethodEntry  ----------
  971.  
  972. void MethodEntry::print () {
  973.   int i;
  974.   char * label;
  975.  
  976.   fprintf (outputFile, "\tpush\tr14\n");
  977.   fprintf (outputFile, "\tmov\tr15,r14\n");
  978.   fprintf (outputFile, "\tpush\tr13\n");
  979.   fprintf (outputFile, "\tset\t%s,r1\n",
  980.            appendStrings ("_RoutineDescriptor_",meth->newName,""));
  981.   fprintf (outputFile, "\tpush\tr1\n");
  982.   if (meth->frameSize % 4 != 0) {
  983.     programLogicError ("meth->frameSize should be a multiple of 4");
  984.   }
  985.  
  986.   // Generate code to zero-out the var & arg portion of this frame...
  987.   i = meth->frameSize / 4;
  988.   if (i > 0) {
  989.     label = newLabel ();
  990.     if (within16Bits (i)) {
  991.       fprintf (outputFile, "\tmov\t%d,r1\n", i);
  992.     } else {
  993.       // printf ("16-bit OVERFLOW IN METHOD: %s,  i = %d\n", meth->selector->chars, i);
  994.       fprintf (outputFile, "\tset\t%d,r1\n", i);
  995.     }
  996.     fprintf (outputFile, "%s:\n", label);
  997.     fprintf (outputFile, "\tpush\tr0\n");
  998.     fprintf (outputFile, "\tsub\tr1,1,r1\n");
  999.     fprintf (outputFile, "\tbne\t%s\n", label);
  1000.   }
  1001.  
  1002. }
  1003.  
  1004. void IRMethodEntry (Method * meth) {
  1005.   linkIR (new MethodEntry (meth));
  1006. }
  1007.  
  1008.  
  1009.  
  1010. //----------  MethodReturn  ----------
  1011.  
  1012. void MethodReturn::print () {
  1013.   int i = meth->frameSize+4;
  1014.   if (within16Bits (i)) {
  1015.     fprintf (outputFile, "\tadd\tr15,%d,r15\n", i);
  1016.   } else {
  1017.     // printf ("16-bit OVERFLOW IN METHOD RETURN: %s   i=%d\n", meth->selector->chars, i);
  1018.     fprintf (outputFile, "\tset\t%d,r1\n", i);
  1019.     fprintf (outputFile, "\tadd\tr15,r1,r15\n");
  1020.   }
  1021.   fprintf (outputFile, "\tpop\tr13\n");
  1022.   fprintf (outputFile, "\tpop\tr14\n");
  1023.   fprintf (outputFile, "\tret\n");
  1024. }
  1025.  
  1026. void IRMethodReturn (Method * meth) {
  1027.   linkIR (new MethodReturn (meth));
  1028. }
  1029.  
  1030.  
  1031.  
  1032. //----------  CheckVersion  ----------
  1033.  
  1034. void CheckVersion::print () {
  1035.   fprintf (outputFile, "!\n");
  1036.   fprintf (outputFile, "! CheckVersion\n");
  1037.   fprintf (outputFile, "!\n");
  1038.   fprintf (outputFile, "!     This routine is passed:\n");
  1039.   fprintf (outputFile, "!       r2 = ptr to the name of the 'using' package\n");
  1040.   fprintf (outputFile, "!       r3 = the expected hashVal for 'used' package (myPackage)\n");
  1041.   fprintf (outputFile, "!     It prints an error message if the expected hashVal is not correct\n");
  1042.   fprintf (outputFile, "!     It then checks all the packages that 'myPackage' uses.\n");
  1043.   fprintf (outputFile, "!\n");
  1044.   fprintf (outputFile, "!     This routine returns:\n");
  1045.   fprintf (outputFile, "!       r1:  0=No problems, 1=Problems\n");
  1046.   fprintf (outputFile, "!\n");
  1047.   fprintf (outputFile, "!     Registers modified: r1-r4\n");
  1048.   fprintf (outputFile, "!\n");
  1049.  
  1050.   fprintf (outputFile, "_CheckVersion%s:\n", mySaniName);
  1051.   fprintf (outputFile, "\t.export\t_CheckVersion%s\n", mySaniName);
  1052.  
  1053. //  fprintf (outputFile, "\tset\t_CVMess7,r1\n");
  1054. //  fprintf (outputFile, "\tcall\t_putString\n");
  1055.  
  1056. //  fprintf (outputFile, "\tset\t_packageName,r1\t\t! print 'Checking this package name'\n");
  1057. //  fprintf (outputFile, "\tcall\t_putString\n");
  1058.  
  1059. //  fprintf (outputFile, "\tset\t_CVMess8,r1\n");
  1060. //  fprintf (outputFile, "\tcall\t_putString\n");
  1061. //  fprintf (outputFile, "\tcall\t_flush\n");
  1062.  
  1063.   fprintf (outputFile, "\tset\t0x%08x,r4\t\t! myHashVal = %d\n", myHashVal, myHashVal);
  1064.   fprintf (outputFile, "\tcmp\tr3,r4\n");
  1065.   fprintf (outputFile, "\tbe\t%s\n", label1);
  1066.  
  1067.   fprintf (outputFile, "\tset\t_CVMess1,r1\n");
  1068.   fprintf (outputFile, "\tcall\t_putString\n");
  1069.  
  1070.   fprintf (outputFile, "\tmov\tr2,r1\t\t\t! print using package\n");
  1071.   fprintf (outputFile, "\tcall\t_putString\n");
  1072.  
  1073.   fprintf (outputFile, "\tset\t_CVMess2,r1\n");
  1074.   fprintf (outputFile, "\tcall\t_putString\n");
  1075.  
  1076.   fprintf (outputFile, "\tset\t_packageName,r1\t\t! print myPackage\n");
  1077.   fprintf (outputFile, "\tcall\t_putString\n");
  1078.  
  1079.   fprintf (outputFile, "\tset\t_CVMess3,r1\n");
  1080.   fprintf (outputFile, "\tcall\t_putString\n");
  1081.  
  1082.   fprintf (outputFile, "\tset\t_packageName,r1\t\t! print myPackage\n");
  1083.   fprintf (outputFile, "\tcall\t_putString\n");
  1084.  
  1085.   fprintf (outputFile, "\tset\t_CVMess4,r1\n");
  1086.   fprintf (outputFile, "\tcall\t_putString\n");
  1087.  
  1088.   fprintf (outputFile, "\tmov\tr2,r1\t\t\t! print using package\n");
  1089.   fprintf (outputFile, "\tcall\t_putString\n");
  1090.  
  1091.   fprintf (outputFile, "\tset\t_CVMess5,r1\n");
  1092.   fprintf (outputFile, "\tcall\t_putString\n");
  1093.  
  1094.   fprintf (outputFile, "\tset\t_packageName,r1\t\t! print myPackage\n");
  1095.   fprintf (outputFile, "\tcall\t_putString\n");
  1096.  
  1097.   fprintf (outputFile, "\tset\t_CVMess6,r1\n");
  1098.   fprintf (outputFile, "\tcall\t_putString\n");
  1099.   // fprintf (outputFile, "\tcall\t_flush\n");
  1100.  
  1101.   fprintf (outputFile, "\tmov\t1,r1\n");
  1102.   fprintf (outputFile, "\tret\t\n");
  1103.  
  1104.   fprintf (outputFile, "%s:\n", label1);
  1105.   fprintf (outputFile, "\tmov\t0,r1\n");
  1106. }
  1107.  
  1108. void IRCheckVersion (char * mySaniName, int myHashVal, char * label1) {
  1109.   linkIR (new CheckVersion (mySaniName, myHashVal, label1));
  1110. }
  1111.  
  1112.  
  1113.  
  1114. //----------  CallCheckVersion  ----------
  1115.  
  1116. void CallCheckVersion::print () {
  1117.  
  1118.   fprintf (outputFile, "! Make sure %s has hash value 0x%08x (decimal %d)\n",
  1119.                         theirSaniName, theirHashVal, theirHashVal);
  1120.   fprintf (outputFile, "\tset\t_packageName,r2\n");
  1121.   fprintf (outputFile, "\tset\t0x%08x,r3\n", theirHashVal);
  1122.   fprintf (outputFile, "\tcall\t_CheckVersion%s\n", theirSaniName);
  1123.   fprintf (outputFile, "\t.import\t_CheckVersion%s\n", theirSaniName);
  1124.   fprintf (outputFile, "\tcmp\tr1,0\n");
  1125.   fprintf (outputFile, "\tbne\t%s\n", label2);
  1126. }
  1127.  
  1128. void IRCallCheckVersion (char * theirSaniName, int theirHashVal, char * label2) {
  1129.   linkIR (new CallCheckVersion (theirSaniName, theirHashVal, label2));
  1130. }
  1131.  
  1132.  
  1133.  
  1134. //----------  EndCheckVersion  ----------
  1135.  
  1136. void EndCheckVersion::print () {
  1137.   fprintf (outputFile, "%s:\n", label2);
  1138.   fprintf (outputFile, "\tret\n");
  1139.   fprintf (outputFile, "_CVMess1:\t.ascii\t\"\\nVERSION CONSISTENCY ERROR: Package '\\0\"\n");
  1140.   fprintf (outputFile, "_CVMess2:\t.ascii\t\"' uses package '\\0\"\n");
  1141.   fprintf (outputFile, "_CVMess3:\t.ascii\t\"'.  Whenever a header file is modified, all packages that use that package (directly or indirectly) must be recompiled.  The header file for '\\0\"\n");
  1142.   fprintf (outputFile, "_CVMess4:\t.ascii\t\"' has been changed since '\\0\"\n");
  1143.   fprintf (outputFile, "_CVMess5:\t.ascii\t\"' was compiled last.  Please recompile all packages that depend on '\\0\"\n");
  1144.   fprintf (outputFile, "_CVMess6:\t.ascii\t\"'.\\n\\n\\0\"\n");
  1145. //  fprintf (outputFile, "_CVMess7:\t.ascii\t\"Checking \\0\"\n");
  1146. //  fprintf (outputFile, "_CVMess8:\t.ascii\t\"...\\n\\0\"\n");
  1147.   fprintf (outputFile, "\t.align\n");
  1148. }
  1149.  
  1150. void IREndCheckVersion (char * label2) {
  1151.   linkIR (new EndCheckVersion (label2));
  1152. }
  1153.  
  1154.  
  1155.  
  1156. //----------  StartCheckVersion  ----------
  1157.  
  1158. void StartCheckVersion::print () {
  1159.  
  1160.   fprintf (outputFile, "\tset\t_packageName,r2\t\t! Get CheckVersion started\n");
  1161.   fprintf (outputFile, "\tset\t0x%08x,r3\t\t! .  hashVal = %d\n",
  1162.                               myHashVal, myHashVal);
  1163.   fprintf (outputFile, "\tcall\t_CheckVersion%s\t! .\n", mySaniName);
  1164.   fprintf (outputFile, "\tcmp\tr1,0\t\t\t! .\n");
  1165.   fprintf (outputFile, "\tbe\t%s\t\t! .\n", continueLab);
  1166.   fprintf (outputFile, "\tret\t\t\t\t! .\n");
  1167.   fprintf (outputFile, "%s:\t\t\t\t! .\n", continueLab);
  1168. }
  1169.  
  1170. void IRStartCheckVersion (char * mySaniName, int myHashVal, char * continueLab) {
  1171.   linkIR (new StartCheckVersion (mySaniName, myHashVal, continueLab));
  1172. }
  1173.  
  1174.  
  1175.  
  1176. //----------  VarDesc1  ----------
  1177.  
  1178. void VarDesc1::print () {
  1179.   fprintf (outputFile, "\t.word\t%s\n", label);
  1180.   fprintf (outputFile, "\t.word\t%d\n", varDecl->offset);
  1181.   fprintf (outputFile, "\t.word\t%d\n", sizeInBytes);
  1182. }
  1183.  
  1184. void IRVarDesc1 (char * lab, VarDecl * vd, int sz) {
  1185.   linkIR (new VarDesc1 (lab, vd, sz));
  1186. }
  1187.  
  1188.  
  1189.  
  1190. //----------  VarDesc2  ----------
  1191.  
  1192. void VarDesc2::print () {
  1193.   fprintf (outputFile, "%s:\n", label);
  1194.   fprintf (outputFile, "\t.byte\t'%c'\n", kind);
  1195.   fprintf (outputFile, "\t.ascii\t\"%s\\0\"\n", name);
  1196.   fprintf (outputFile, "\t.align\n");
  1197. }
  1198.  
  1199. void IRVarDesc2 (char * lab, char k, char * n) {
  1200.   linkIR (new VarDesc2 (lab, k, n));
  1201. }
  1202.  
  1203.  
  1204.  
  1205. //----------  FrameSize  ----------
  1206.  
  1207. void FrameSize::print () {
  1208.   int i;
  1209.   if (funOrMeth->op == FUNCTION) {
  1210.     i = ((Function *) funOrMeth)->frameSize;
  1211.   } else if (funOrMeth->op == METHOD) {
  1212.     i = ((Method *) funOrMeth)->frameSize;
  1213.   } else {
  1214.     programLogicError ("Expecting fun or meth in IRFrameSize");
  1215.   }
  1216.   fprintf (outputFile, "\t.word\t%d\t\t! frame size = %d\n", i, i);
  1217. }
  1218.  
  1219. void IRFrameSize (AstNode * funOrMeth) {
  1220.   linkIR (new FrameSize (funOrMeth));
  1221. }
  1222.  
  1223.  
  1224.  
  1225. //----------  Assign1  ----------
  1226.  
  1227. void Assign1::print () {
  1228.   fprintf (outputFile, "!   ");
  1229.   printANode (dest);
  1230.   fprintf (outputFile, " = ");
  1231.   printANode (src);
  1232.   fprintf (outputFile, "\t\t(1 byte)\n");
  1233.   getIntoReg1 (src, "r1");
  1234.   storeFromReg1 ((VarDecl *) dest, "r1", "r2");
  1235. }
  1236.  
  1237. void IRAssign1 (AstNode * d, AstNode * s) {
  1238.   linkIR (new Assign1 (d, s));
  1239. }
  1240.  
  1241.  
  1242.  
  1243. //----------  Assign4  ----------
  1244.  
  1245. void Assign4::print () {
  1246.   fprintf (outputFile, "!   ");
  1247.   printANode (dest);
  1248.   fprintf (outputFile, " = ");
  1249.   printANode (src);
  1250.   fprintf (outputFile, "\t\t(4 bytes)\n");
  1251.   getIntoReg4 (src, "r1");
  1252.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1253. }
  1254.  
  1255. void IRAssign4 (AstNode * d, AstNode * s) {
  1256.   linkIR (new Assign4 (d, s));
  1257. }
  1258.  
  1259.  
  1260.  
  1261. //----------  Assign8  ----------
  1262.  
  1263. void Assign8::print () {
  1264.   fprintf (outputFile, "!   ");
  1265.   printANode (dest);
  1266.   fprintf (outputFile, " = ");
  1267.   printANode (src);
  1268.   fprintf (outputFile, "\t\t(8 bytes)\n");
  1269.  
  1270.   getIntoReg8 (src, "f0", "r1");
  1271.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1272. }
  1273.  
  1274. void IRAssign8 (AstNode * d, AstNode * s) {
  1275.   linkIR (new Assign8 (d, s));
  1276. }
  1277.  
  1278.  
  1279.  
  1280. //----------  Ascii  ----------
  1281.  
  1282. void Ascii::print () {
  1283.   fprintf (outputFile, "\t.ascii\t\"%s\"\n", str);
  1284. }
  1285.  
  1286. void IRAscii (char * s) {
  1287.   linkIR (new Ascii (s));
  1288. }
  1289.  
  1290.  
  1291.  
  1292. //----------  Ascii0  ----------
  1293.  
  1294. void Ascii0::print () {
  1295.   fprintf (outputFile, "\t.ascii\t\"%s\\0\"\n", str);
  1296. }
  1297.  
  1298. void IRAscii0 (char * s) {
  1299.   linkIR (new Ascii0 (s));
  1300. }
  1301.  
  1302.  
  1303.  
  1304. //----------  Ascii2  ----------
  1305.  
  1306. void Ascii2::print () {
  1307.   fprintf (outputFile, "\t.word\t%d\t\t\t! length\n", str->length);
  1308.   fprintf (outputFile, "\t.ascii\t\"");
  1309.   printString (outputFile, str);
  1310.   fprintf (outputFile, "\"\n");
  1311. }
  1312.  
  1313. void IRAscii2 (String * s) {
  1314.   linkIR (new Ascii2 (s));
  1315. }
  1316.  
  1317.  
  1318.  
  1319. //----------  IAdd  ----------
  1320.  
  1321. void IAdd::print () {
  1322.   fprintf (outputFile, "!   ");
  1323.   printANode (dest);
  1324.   fprintf (outputFile, " = ");
  1325.   printANode (arg1);
  1326.   fprintf (outputFile, " + ");
  1327.   printANode (arg2);
  1328.   fprintf (outputFile, "\t\t(int)\n");
  1329.   getIntoReg4 (arg1, "r1");
  1330.   getIntoReg4 (arg2, "r2");
  1331.   fprintf (outputFile, "\tadd\tr1,r2,r1\n");
  1332.   overflowTest ();
  1333.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1334. }
  1335.  
  1336. void IRIAdd (AstNode * d, AstNode * a1, AstNode * a2) {
  1337.   linkIR (new IAdd (d, a1, a2));
  1338. }
  1339.  
  1340.  
  1341.  
  1342. //----------  ISub  ----------
  1343.  
  1344. void ISub::print () {
  1345.   fprintf (outputFile, "!   ");
  1346.   printANode (dest);
  1347.   fprintf (outputFile, " = ");
  1348.   printANode (arg1);
  1349.   fprintf (outputFile, " - ");
  1350.   printANode (arg2);
  1351.   fprintf (outputFile, "\t\t(int)\n");
  1352.   getIntoReg4 (arg1, "r1");
  1353.   getIntoReg4 (arg2, "r2");
  1354.   fprintf (outputFile, "\tsub\tr1,r2,r1\n");
  1355.   overflowTest ();
  1356.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1357. }
  1358.  
  1359. void IRISub (AstNode * d, AstNode * a1, AstNode * a2) {
  1360.   linkIR (new ISub (d, a1, a2));
  1361. }
  1362.  
  1363.  
  1364.  
  1365. //----------  IMul  ----------
  1366.  
  1367. void IMul::print () {
  1368.   fprintf (outputFile, "!   ");
  1369.   printANode (dest);
  1370.   fprintf (outputFile, " = ");
  1371.   printANode (arg1);
  1372.   fprintf (outputFile, " * ");
  1373.   printANode (arg2);
  1374.   fprintf (outputFile, "\t\t(int)\n");
  1375.   getIntoReg4 (arg1, "r1");
  1376.   getIntoReg4 (arg2, "r2");
  1377.   fprintf (outputFile, "\tmul\tr1,r2,r1\n");
  1378.   overflowTest ();
  1379.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1380. }
  1381.  
  1382. void IRIMul (AstNode * d, AstNode * a1, AstNode * a2) {
  1383.   linkIR (new IMul (d, a1, a2));
  1384. }
  1385.  
  1386.  
  1387.  
  1388. //----------  IDiv  ----------
  1389.  
  1390. void IDiv::print () {
  1391.   fprintf (outputFile, "!   ");
  1392.   printANode (dest);
  1393.   fprintf (outputFile, " = ");
  1394.   printANode (arg1);
  1395.   fprintf (outputFile, " div ");
  1396.   printANode (arg2);
  1397.   fprintf (outputFile, "\t\t(int)\n");
  1398.   getIntoReg4 (arg1, "r1");
  1399.   getIntoReg4 (arg2, "r2");
  1400.   fprintf (outputFile, "\tcmp\tr2,0\n");
  1401.   fprintf (outputFile, "\tbe\t_runtimeErrorZeroDivide\n");
  1402.   fprintf (outputFile, "\tdiv\tr1,r2,r1\n");
  1403.   overflowTest ();
  1404.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1405. }
  1406.  
  1407. void IRIDiv (AstNode * d, AstNode * a1, AstNode * a2) {
  1408.   linkIR (new IDiv (d, a1, a2));
  1409. }
  1410.  
  1411.  
  1412.  
  1413. //----------  IRem  ----------
  1414.  
  1415. void IRem::print () {
  1416.   fprintf (outputFile, "!   ");
  1417.   printANode (dest);
  1418.   fprintf (outputFile, " = ");
  1419.   printANode (arg1);
  1420.   fprintf (outputFile, " rem ");
  1421.   printANode (arg2);
  1422.   fprintf (outputFile, "\t\t(int)\n");
  1423.   getIntoReg4 (arg1, "r1");
  1424.   getIntoReg4 (arg2, "r2");
  1425.   fprintf (outputFile, "\tcmp\tr2,0\n");
  1426.   fprintf (outputFile, "\tbe\t_runtimeErrorZeroDivide\n");
  1427.   fprintf (outputFile, "\trem\tr1,r2,r1\n");
  1428.   overflowTest ();
  1429.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1430. }
  1431.  
  1432. void IRIRem (AstNode * d, AstNode * a1, AstNode * a2) {
  1433.   linkIR (new IRem (d, a1, a2));
  1434. }
  1435.  
  1436.  
  1437.  
  1438. //----------  Sll  ----------
  1439.  
  1440. void Sll::print () {
  1441.   fprintf (outputFile, "!   ");
  1442.   printANode (dest);
  1443.   fprintf (outputFile, " = ");
  1444.   printANode (arg1);
  1445.   fprintf (outputFile, " sll ");
  1446.   printANode (arg2);
  1447.   fprintf (outputFile, "\t\t(int)\n");
  1448.   getIntoReg4 (arg1, "r1");
  1449.   getIntoReg4 (arg2, "r2");
  1450.   fprintf (outputFile, "\tsll\tr1,r2,r1\n");
  1451.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1452. }
  1453.  
  1454. void IRSll (AstNode * d, AstNode * a1, AstNode * a2) {
  1455.   linkIR (new Sll (d, a1, a2));
  1456. }
  1457.  
  1458.  
  1459.  
  1460. //----------  Sra  ----------
  1461.  
  1462. void Sra::print () {
  1463.   fprintf (outputFile, "!   ");
  1464.   printANode (dest);
  1465.   fprintf (outputFile, " = ");
  1466.   printANode (arg1);
  1467.   fprintf (outputFile, " sra ");
  1468.   printANode (arg2);
  1469.   fprintf (outputFile, "\t\t(int)\n");
  1470.   getIntoReg4 (arg1, "r1");
  1471.   getIntoReg4 (arg2, "r2");
  1472.   fprintf (outputFile, "\tsra\tr1,r2,r1\n");
  1473.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1474. }
  1475.  
  1476. void IRSra (AstNode * d, AstNode * a1, AstNode * a2) {
  1477.   linkIR (new Sra (d, a1, a2));
  1478. }
  1479.  
  1480.  
  1481.  
  1482. //----------  Srl  ----------
  1483.  
  1484. void Srl::print () {
  1485.   fprintf (outputFile, "!   ");
  1486.   printANode (dest);
  1487.   fprintf (outputFile, " = ");
  1488.   printANode (arg1);
  1489.   fprintf (outputFile, " srl ");
  1490.   printANode (arg2);
  1491.   fprintf (outputFile, "\t\t(int)\n");
  1492.   getIntoReg4 (arg1, "r1");
  1493.   getIntoReg4 (arg2, "r2");
  1494.   fprintf (outputFile, "\tsrl\tr1,r2,r1\n");
  1495.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1496. }
  1497.  
  1498. void IRSrl (AstNode * d, AstNode * a1, AstNode * a2) {
  1499.   linkIR (new Srl (d, a1, a2));
  1500. }
  1501.  
  1502.  
  1503.  
  1504. //----------  And  ----------
  1505.  
  1506. void And::print () {
  1507.   fprintf (outputFile, "!   ");
  1508.   printANode (dest);
  1509.   fprintf (outputFile, " = ");
  1510.   printANode (arg1);
  1511.   fprintf (outputFile, " AND ");
  1512.   printANode (arg2);
  1513.   fprintf (outputFile, "\t\t(int)\n");
  1514.   getIntoReg4 (arg1, "r1");
  1515.   getIntoReg4 (arg2, "r2");
  1516.   fprintf (outputFile, "\tand\tr1,r2,r1\n");
  1517.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1518. }
  1519.  
  1520. void IRAnd (AstNode * d, AstNode * a1, AstNode * a2) {
  1521.   linkIR (new And (d, a1, a2));
  1522. }
  1523.  
  1524.  
  1525.  
  1526. //----------  Or  ----------
  1527.  
  1528. void Or::print () {
  1529.   fprintf (outputFile, "!   ");
  1530.   printANode (dest);
  1531.   fprintf (outputFile, " = ");
  1532.   printANode (arg1);
  1533.   fprintf (outputFile, " OR ");
  1534.   printANode (arg2);
  1535.   fprintf (outputFile, "\t\t(int)\n");
  1536.   getIntoReg4 (arg1, "r1");
  1537.   getIntoReg4 (arg2, "r2");
  1538.   fprintf (outputFile, "\tor\tr1,r2,r1\n");
  1539.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1540. }
  1541.  
  1542. void IROr (AstNode * d, AstNode * a1, AstNode * a2) {
  1543.   linkIR (new Or (d, a1, a2));
  1544. }
  1545.  
  1546.  
  1547.  
  1548. //----------  Xor  ----------
  1549.  
  1550. void Xor::print () {
  1551.   fprintf (outputFile, "!   ");
  1552.   printANode (dest);
  1553.   fprintf (outputFile, " = ");
  1554.   printANode (arg1);
  1555.   fprintf (outputFile, " XOR ");
  1556.   printANode (arg2);
  1557.   fprintf (outputFile, "\t\t(int)\n");
  1558.   getIntoReg4 (arg1, "r1");
  1559.   getIntoReg4 (arg2, "r2");
  1560.   fprintf (outputFile, "\txor\tr1,r2,r1\n");
  1561.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1562. }
  1563.  
  1564. void IRXor (AstNode * d, AstNode * a1, AstNode * a2) {
  1565.   linkIR (new Xor (d, a1, a2));
  1566. }
  1567.  
  1568.  
  1569.  
  1570. //----------  INeg  ----------
  1571.  
  1572. void INeg::print () {
  1573.   fprintf (outputFile, "!   ");
  1574.   printANode (dest);
  1575.   fprintf (outputFile, " = -");
  1576.   printANode (arg);
  1577.   fprintf (outputFile, "\t\t(int)\n");
  1578.   getIntoReg4 (arg, "r1");
  1579.   fprintf (outputFile, "\tneg\tr1\n");
  1580.   overflowTest ();
  1581.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1582. }
  1583.  
  1584. void IRINeg (AstNode * d, AstNode * a) {
  1585.   linkIR (new INeg (d, a));
  1586. }
  1587.  
  1588.  
  1589.  
  1590. //----------  Not  ----------
  1591.  
  1592. void Not::print () {
  1593.   fprintf (outputFile, "!   ");
  1594.   printANode (dest);
  1595.   fprintf (outputFile, " = NOT ");
  1596.   printANode (arg);
  1597.   fprintf (outputFile, "\t\t(int)\n");
  1598.   getIntoReg4 (arg, "r1");
  1599.   fprintf (outputFile, "\tnot\tr1\n");
  1600.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1601. }
  1602.  
  1603. void IRNot (AstNode * d, AstNode * a) {
  1604.   linkIR (new Not (d, a));
  1605. }
  1606.  
  1607.  
  1608.  
  1609. //----------  FAdd  ----------
  1610.  
  1611. void FAdd::print () {
  1612.   fprintf (outputFile, "!   ");
  1613.   printANode (dest);
  1614.   fprintf (outputFile, " = ");
  1615.   printANode (arg1);
  1616.   fprintf (outputFile, " + ");
  1617.   printANode (arg2);
  1618.   fprintf (outputFile, "\t\t(float)\n");
  1619.   getIntoReg8 (arg1, "f0", "r1");
  1620.   getIntoReg8 (arg2, "f1", "r2");
  1621.   fprintf (outputFile, "\tfadd\tf0,f1,f0\n");
  1622.   // CCR is not affected: no overflow test possible
  1623.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1624. }
  1625.  
  1626. void IRFAdd (AstNode * d, AstNode * a1, AstNode * a2) {
  1627.   linkIR (new FAdd (d, a1, a2));
  1628. }
  1629.  
  1630.  
  1631.  
  1632. //----------  FSub  ----------
  1633.  
  1634. void FSub::print () {
  1635.   fprintf (outputFile, "!   ");
  1636.   printANode (dest);
  1637.   fprintf (outputFile, " = ");
  1638.   printANode (arg1);
  1639.   fprintf (outputFile, " + ");
  1640.   printANode (arg2);
  1641.   fprintf (outputFile, "\t\t(float)\n");
  1642.   getIntoReg8 (arg1, "f0", "r1");
  1643.   getIntoReg8 (arg2, "f1", "r2");
  1644.   fprintf (outputFile, "\tfsub\tf0,f1,f0\n");
  1645.   // CCR is not affected: no overflow test possible
  1646.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1647. }
  1648.  
  1649. void IRFSub (AstNode * d, AstNode * a1, AstNode * a2) {
  1650.   linkIR (new FSub (d, a1, a2));
  1651. }
  1652.  
  1653.  
  1654.  
  1655. //----------  FMul  ----------
  1656.  
  1657. void FMul::print () {
  1658.   fprintf (outputFile, "!   ");
  1659.   printANode (dest);
  1660.   fprintf (outputFile, " = ");
  1661.   printANode (arg1);
  1662.   fprintf (outputFile, " + ");
  1663.   printANode (arg2);
  1664.   fprintf (outputFile, "\t\t(float)\n");
  1665.   getIntoReg8 (arg1, "f0", "r1");
  1666.   getIntoReg8 (arg2, "f1", "r2");
  1667.   fprintf (outputFile, "\tfmul\tf0,f1,f0\n");
  1668.   // CCR is not affected: no overflow test possible
  1669.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1670. }
  1671.  
  1672. void IRFMul (AstNode * d, AstNode * a1, AstNode * a2) {
  1673.   linkIR (new FMul (d, a1, a2));
  1674. }
  1675.  
  1676.  
  1677.  
  1678. //----------  FDiv  ----------
  1679.  
  1680. void FDiv::print () {
  1681.   fprintf (outputFile, "!   ");
  1682.   printANode (dest);
  1683.   fprintf (outputFile, " = ");
  1684.   printANode (arg1);
  1685.   fprintf (outputFile, " + ");
  1686.   printANode (arg2);
  1687.   fprintf (outputFile, "\t\t(float)\n");
  1688.   getIntoReg8 (arg1, "f0", "r1");
  1689.   getIntoReg8 (arg2, "f1", "r2");
  1690.   fprintf (outputFile, "\tfdiv\tf0,f1,f0\n");
  1691.   // CCR is not affected: no overflow test possible
  1692.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1693. }
  1694.  
  1695. void IRFDiv (AstNode * d, AstNode * a1, AstNode * a2) {
  1696.   linkIR (new FDiv (d, a1, a2));
  1697. }
  1698.  
  1699.  
  1700.  
  1701. //----------  FNeg  ----------
  1702.  
  1703. void FNeg::print () {
  1704.   fprintf (outputFile, "!   ");
  1705.   printANode (dest);
  1706.   fprintf (outputFile, " = -");
  1707.   printANode (arg);
  1708.   fprintf (outputFile, "\t\t(float)\n");
  1709.   getIntoReg8 (arg, "f0", "r1");
  1710.   fprintf (outputFile, "\tfneg\tf0,f0\n");
  1711.   // CCR is not affected: no overflow test possible
  1712.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1713. }
  1714.  
  1715. void IRFNeg (AstNode * d, AstNode * a) {
  1716.   linkIR (new FNeg (d, a));
  1717. }
  1718.  
  1719.  
  1720.  
  1721. //----------  ItoF  ----------
  1722.  
  1723. void ItoF::print () {
  1724.   fprintf (outputFile, "!   ");
  1725.   printANode (dest);
  1726.   fprintf (outputFile, " = intToFloat (");
  1727.   printANode (arg);
  1728.   fprintf (outputFile, ")\n");
  1729.   getIntoReg4 (arg, "r1");
  1730.   fprintf (outputFile, "\titof\tr1,f0\n");
  1731.   // CCR is not affected: no overflow test possible
  1732.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1733. }
  1734.  
  1735. void IRItoF (AstNode * d, AstNode * a) {
  1736.   linkIR (new ItoF (d, a));
  1737. }
  1738.  
  1739.  
  1740.  
  1741. //----------  FtoI  ----------
  1742.  
  1743. void FtoI::print () {
  1744.   fprintf (outputFile, "!   ");
  1745.   printANode (dest);
  1746.   fprintf (outputFile, " = floatToInt (");
  1747.   printANode (arg);
  1748.   fprintf (outputFile, ")\n");
  1749.   getIntoReg8 (arg, "f0", "r1");
  1750.   fprintf (outputFile, "\tftoi\tf0,r1\n");
  1751.   // CCR is not affected: no overflow test possible
  1752.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1753. }
  1754.  
  1755. void IRFtoI (AstNode * d, AstNode * a) {
  1756.   linkIR (new FtoI (d, a));
  1757. }
  1758.  
  1759.  
  1760.  
  1761. //----------  ItoC  ----------
  1762.  
  1763. void ItoC::print () {
  1764.   fprintf (outputFile, "!   ");
  1765.   printANode (dest);
  1766.   fprintf (outputFile, " = intToChar (");
  1767.   printANode (arg);
  1768.   fprintf (outputFile, ")\n");
  1769.   getIntoReg4 (arg, "r1");
  1770.   // CCR is not affected: no overflow test possible
  1771.   storeFromReg1 ((VarDecl *) dest, "r1", "r2");
  1772. }
  1773.  
  1774. void IRItoC (AstNode * d, AstNode * a) {
  1775.   linkIR (new ItoC (d, a));
  1776. }
  1777.  
  1778.  
  1779.  
  1780. //----------  CtoI  ----------
  1781.  
  1782. void CtoI::print () {
  1783.   fprintf (outputFile, "!   ");
  1784.   printANode (dest);
  1785.   fprintf (outputFile, " = charToInt (");
  1786.   printANode (arg);
  1787.   fprintf (outputFile, ")\n");
  1788.   getIntoReg1 (arg, "r1");
  1789.   fprintf (outputFile, "\tsll\tr1,24,r1\n");
  1790.   fprintf (outputFile, "\tsra\tr1,24,r1\n");
  1791.   storeFromReg4 ((VarDecl *) dest, "r1", "r2");
  1792. }
  1793.  
  1794. void IRCtoI (AstNode * d, AstNode * a) {
  1795.   linkIR (new CtoI (d, a));
  1796. }
  1797.  
  1798.  
  1799.  
  1800. //----------  PosInf  ----------
  1801.  
  1802. void PosInf::print () {
  1803.   fprintf (outputFile, "!   ");
  1804.   printANode (dest);
  1805.   fprintf (outputFile, " = PosInf\n");
  1806.   fprintf (outputFile, "\tset\t_PosInf,r1\n");
  1807.   fprintf (outputFile, "\tfload\t[r1],f0\n");
  1808.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1809. }
  1810.  
  1811. void IRPosInf (AstNode * d) {
  1812.   linkIR (new PosInf (d));
  1813. }
  1814.  
  1815.  
  1816.  
  1817. //----------  NegInf  ----------
  1818.  
  1819. void NegInf::print () {
  1820.   fprintf (outputFile, "!   ");
  1821.   printANode (dest);
  1822.   fprintf (outputFile, " = NegInf\n");
  1823.   fprintf (outputFile, "\tset\t_NegInf,r1\n");
  1824.   fprintf (outputFile, "\tfload\t[r1],f0\n");
  1825.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1826. }
  1827.  
  1828. void IRNegInf (AstNode * d) {
  1829.   linkIR (new NegInf (d));
  1830. }
  1831.  
  1832.  
  1833.  
  1834. //----------  NegZero  ----------
  1835.  
  1836. void NegZero::print () {
  1837.   fprintf (outputFile, "!   ");
  1838.   printANode (dest);
  1839.   fprintf (outputFile, " = NegZero\n");
  1840.   fprintf (outputFile, "\tset\t_NegZero,r1\n");
  1841.   fprintf (outputFile, "\tfload\t[r1],f0\n");
  1842.   storeFromReg8 ((VarDecl *) dest, "f0", "r2");
  1843. }
  1844.  
  1845. void IRNegZero (AstNode * d) {
  1846.   linkIR (new NegZero (d));
  1847. }
  1848.  
  1849.  
  1850.  
  1851. //----------  IntLTGoto  ----------
  1852.  
  1853. void IntLTGoto::print () {
  1854.   fprintf (outputFile, "!   if ");
  1855.   printANode (arg1);
  1856.   fprintf (outputFile, " < ");
  1857.   printANode (arg2);
  1858.   fprintf (outputFile, " then goto %s\t\t(int)\n", label);
  1859.   getIntoReg4 (arg1, "r1");
  1860.   getIntoReg4 (arg2, "r2");
  1861.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  1862.   overflowTest ();
  1863.   fprintf (outputFile, "\tbl\t%s\n", label);
  1864. }
  1865.  
  1866. void IRIntLTGoto (AstNode * a1, AstNode * a2, char * lab) {
  1867.   linkIR (new IntLTGoto (a1, a2, lab));
  1868. }
  1869.  
  1870.  
  1871.  
  1872. //----------  IntLEGoto  ----------
  1873.  
  1874. void IntLEGoto::print () {
  1875.   fprintf (outputFile, "!   if ");
  1876.   printANode (arg1);
  1877.   fprintf (outputFile, " <= ");
  1878.   printANode (arg2);
  1879.   fprintf (outputFile, " then goto %s\t\t(int)\n", label);
  1880.   getIntoReg4 (arg1, "r1");
  1881.   getIntoReg4 (arg2, "r2");
  1882.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  1883.   overflowTest ();
  1884.   fprintf (outputFile, "\tble\t%s\n", label);
  1885. }
  1886.  
  1887. void IRIntLEGoto (AstNode * a1, AstNode * a2, char * lab) {
  1888.   linkIR (new IntLEGoto (a1, a2, lab));
  1889. }
  1890.  
  1891.  
  1892.  
  1893. //----------  IntGTGoto  ----------
  1894.  
  1895. void IntGTGoto::print () {
  1896.   fprintf (outputFile, "!   if ");
  1897.   printANode (arg1);
  1898.   fprintf (outputFile, " > ");
  1899.   printANode (arg2);
  1900.   fprintf (outputFile, " then goto %s\t\t(int)\n", label);
  1901.   getIntoReg4 (arg1, "r1");
  1902.   getIntoReg4 (arg2, "r2");
  1903.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  1904.   overflowTest ();
  1905.   fprintf (outputFile, "\tbg\t%s\n", label);
  1906. }
  1907.  
  1908. void IRIntGTGoto (AstNode * a1, AstNode * a2, char * lab) {
  1909.   linkIR (new IntGTGoto (a1, a2, lab));
  1910. }
  1911.  
  1912.  
  1913.  
  1914. //----------  IntGEGoto  ----------
  1915.  
  1916. void IntGEGoto::print () {
  1917.   fprintf (outputFile, "!   if ");
  1918.   printANode (arg1);
  1919.   fprintf (outputFile, " >= ");
  1920.   printANode (arg2);
  1921.   fprintf (outputFile, " then goto %s\t\t(int)\n", label);
  1922.   getIntoReg4 (arg1, "r1");
  1923.   getIntoReg4 (arg2, "r2");
  1924.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  1925.   overflowTest ();
  1926.   fprintf (outputFile, "\tbge\t%s\n", label);
  1927. }
  1928.  
  1929. void IRIntGEGoto (AstNode * a1, AstNode * a2, char * lab) {
  1930.   linkIR (new IntGEGoto (a1, a2, lab));
  1931. }
  1932.  
  1933.  
  1934.  
  1935. //----------  IntEQGoto  ----------
  1936.  
  1937. void IntEQGoto::print () {
  1938.   fprintf (outputFile, "!   if ");
  1939.   printANode (arg1);
  1940.   fprintf (outputFile, " == ");
  1941.   printANode (arg2);
  1942.   fprintf (outputFile, " then goto %s\t\t(int)\n", label);
  1943.   getIntoReg4 (arg1, "r1");
  1944.   getIntoReg4 (arg2, "r2");
  1945.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  1946.   // overflowTest ();             // The Z flag will be ok, even if overflow
  1947.   fprintf (outputFile, "\tbe\t%s\n", label);
  1948. }
  1949.  
  1950. void IRIntEQGoto (AstNode * a1, AstNode * a2, char * lab) {
  1951.   linkIR (new IntEQGoto (a1, a2, lab));
  1952. }
  1953.  
  1954.  
  1955.  
  1956. //----------  IntNEGoto  ----------
  1957.  
  1958. void IntNEGoto::print () {
  1959.   fprintf (outputFile, "!   if ");
  1960.   printANode (arg1);
  1961.   fprintf (outputFile, " != ");
  1962.   printANode (arg2);
  1963.   fprintf (outputFile, " then goto %s\t\t(int)\n", label);
  1964.   getIntoReg4 (arg1, "r1");
  1965.   getIntoReg4 (arg2, "r2");
  1966.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  1967.   // overflowTest ();             // The Z flag will be ok, even if overflow
  1968.   fprintf (outputFile, "\tbne\t%s\n", label);
  1969. }
  1970.  
  1971. void IRIntNEGoto (AstNode * a1, AstNode * a2, char * lab) {
  1972.   linkIR (new IntNEGoto (a1, a2, lab));
  1973. }
  1974.  
  1975.  
  1976.  
  1977. //----------  FloatLTGoto  ----------
  1978.  
  1979. void FloatLTGoto::print () {
  1980.   fprintf (outputFile, "!   if ");
  1981.   printANode (arg1);
  1982.   fprintf (outputFile, " < ");
  1983.   printANode (arg2);
  1984.   fprintf (outputFile, " then goto %s\t\t(float)\n", label);
  1985.   getIntoReg8 (arg1, "f0", "r1");
  1986.   getIntoReg8 (arg2, "f1", "r1");
  1987.   fprintf (outputFile, "\tfcmp\tf0,f1\n");
  1988.   overflowTest ();             // Can occur if either is Nan
  1989.   fprintf (outputFile, "\tbl\t%s\n", label);
  1990. }
  1991.  
  1992. void IRFloatLTGoto (AstNode * a1, AstNode * a2, char * lab) {
  1993.   linkIR (new FloatLTGoto (a1, a2, lab));
  1994. }
  1995.  
  1996.  
  1997.  
  1998. //----------  FloatLEGoto  ----------
  1999.  
  2000. void FloatLEGoto::print () {
  2001.   fprintf (outputFile, "!   if ");
  2002.   printANode (arg1);
  2003.   fprintf (outputFile, " <= ");
  2004.   printANode (arg2);
  2005.   fprintf (outputFile, " then goto %s\t\t(float)\n", label);
  2006.   getIntoReg8 (arg1, "f0", "r1");
  2007.   getIntoReg8 (arg2, "f1", "r1");
  2008.   fprintf (outputFile, "\tfcmp\tf0,f1\n");
  2009.   overflowTest ();             // Can occur if either is Nan
  2010.   fprintf (outputFile, "\tble\t%s\n", label);
  2011. }
  2012.  
  2013. void IRFloatLEGoto (AstNode * a1, AstNode * a2, char * lab) {
  2014.   linkIR (new FloatLEGoto (a1, a2, lab));
  2015. }
  2016.  
  2017.  
  2018.  
  2019. //----------  FloatGTGoto  ----------
  2020.  
  2021. void FloatGTGoto::print () {
  2022.   fprintf (outputFile, "!   if ");
  2023.   printANode (arg1);
  2024.   fprintf (outputFile, " > ");
  2025.   printANode (arg2);
  2026.   fprintf (outputFile, " then goto %s\t\t(float)\n", label);
  2027.   getIntoReg8 (arg1, "f0", "r1");
  2028.   getIntoReg8 (arg2, "f1", "r1");
  2029.   fprintf (outputFile, "\tfcmp\tf0,f1\n");
  2030.   overflowTest ();             // Can occur if either is Nan
  2031.   fprintf (outputFile, "\tbg\t%s\n", label);
  2032. }
  2033.  
  2034. void IRFloatGTGoto (AstNode * a1, AstNode * a2, char * lab) {
  2035.   linkIR (new FloatGTGoto (a1, a2, lab));
  2036. }
  2037.  
  2038.  
  2039.  
  2040. //----------  FloatGEGoto  ----------
  2041.  
  2042. void FloatGEGoto::print () {
  2043.   fprintf (outputFile, "!   if ");
  2044.   printANode (arg1);
  2045.   fprintf (outputFile, " >= ");
  2046.   printANode (arg2);
  2047.   fprintf (outputFile, " then goto %s\t\t(float)\n", label);
  2048.   getIntoReg8 (arg1, "f0", "r1");
  2049.   getIntoReg8 (arg2, "f1", "r1");
  2050.   fprintf (outputFile, "\tfcmp\tf0,f1\n");
  2051.   overflowTest ();             // Can occur if either is Nan
  2052.   fprintf (outputFile, "\tbge\t%s\n", label);
  2053. }
  2054.  
  2055. void IRFloatGEGoto (AstNode * a1, AstNode * a2, char * lab) {
  2056.   linkIR (new FloatGEGoto (a1, a2, lab));
  2057. }
  2058.  
  2059.  
  2060.  
  2061. //----------  FloatEQGoto  ----------
  2062.  
  2063. void FloatEQGoto::print () {
  2064.   fprintf (outputFile, "!   if ");
  2065.   printANode (arg1);
  2066.   fprintf (outputFile, " == ");
  2067.   printANode (arg2);
  2068.   fprintf (outputFile, " then goto %s\t\t(float)\n", label);
  2069.   getIntoReg8 (arg1, "f0", "r1");
  2070.   getIntoReg8 (arg2, "f1", "r1");
  2071.   fprintf (outputFile, "\tfcmp\tf0,f1\n");
  2072.   overflowTest ();             // Can occur if either is Nan
  2073.   fprintf (outputFile, "\tbe\t%s\n", label);
  2074. }
  2075.  
  2076. void IRFloatEQGoto (AstNode * a1, AstNode * a2, char * lab) {
  2077.   linkIR (new FloatEQGoto (a1, a2, lab));
  2078. }
  2079.  
  2080.  
  2081.  
  2082. //----------  FloatNEGoto  ----------
  2083.  
  2084. void FloatNEGoto::print () {
  2085.   fprintf (outputFile, "!   if ");
  2086.   printANode (arg1);
  2087.   fprintf (outputFile, " != ");
  2088.   printANode (arg2);
  2089.   fprintf (outputFile, " then goto %s\t\t(float)\n", label);
  2090.   getIntoReg8 (arg1, "f0", "r1");
  2091.   getIntoReg8 (arg2, "f1", "r1");
  2092.   fprintf (outputFile, "\tfcmp\tf0,f1\n");
  2093.   overflowTest ();             // Can occur if either is Nan
  2094.   fprintf (outputFile, "\tbne\t%s\n", label);
  2095. }
  2096.  
  2097. void IRFloatNEGoto (AstNode * a1, AstNode * a2, char * lab) {
  2098.   linkIR (new FloatNEGoto (a1, a2, lab));
  2099. }
  2100.  
  2101.  
  2102.  
  2103. //----------  BoolTest  ----------
  2104.  
  2105. void BoolTest::print () {
  2106.   fprintf (outputFile, "!   if ");
  2107.   printANode (arg);
  2108.   fprintf (outputFile, " then goto %s else goto %s\n", trueLabel, falseLabel);
  2109.   getIntoReg1 (arg, "r1");
  2110.   fprintf (outputFile, "\tcmp\tr1,0\n");
  2111.   fprintf (outputFile, "\tbe\t%s\n", falseLabel);
  2112.   fprintf (outputFile, "\tjmp\t%s\n", trueLabel);
  2113. }
  2114.  
  2115. void IRBoolTest (AstNode * a, char * lab1, char * lab2) {
  2116.   linkIR (new BoolTest (a, lab1, lab2));
  2117. }
  2118.  
  2119.  
  2120.  
  2121. //----------  BoolTest2  ----------
  2122.  
  2123. void BoolTest2::print () {
  2124.   fprintf (outputFile, "!   if result==true then goto %s else goto %s\n", trueLabel, falseLabel);
  2125.   fprintf (outputFile, "\tloadb\t[r15],r1\n");
  2126.   fprintf (outputFile, "\tcmp\tr1,0\n");
  2127.   fprintf (outputFile, "\tbe\t%s\n", falseLabel);
  2128.   fprintf (outputFile, "\tjmp\t%s\n", trueLabel);
  2129. }
  2130.  
  2131. void IRBoolTest2 (char * lab1, char * lab2) {
  2132.   linkIR (new BoolTest2 (lab1, lab2));
  2133. }
  2134.  
  2135.  
  2136.  
  2137. //----------  BXor  ----------
  2138.  
  2139. void BXor::print () {
  2140.   fprintf (outputFile, "!   ");
  2141.   printANode (dest);
  2142.   fprintf (outputFile, " = ");
  2143.   printANode (arg1);
  2144.   fprintf (outputFile, " XOR ");
  2145.   printANode (arg2);
  2146.   fprintf (outputFile, "\t\t(bool)\n");
  2147.   getIntoReg1 (arg1, "r1");
  2148.   getIntoReg1 (arg2, "r2");
  2149.   fprintf (outputFile, "\txor\tr1,r2,r1\n");
  2150.   storeFromReg1 ((VarDecl *) dest, "r1", "r2");
  2151. }
  2152.  
  2153. void IRBXor (AstNode * d, AstNode * a1, AstNode * a2) {
  2154.   linkIR (new BXor (d, a1, a2));
  2155. }
  2156.  
  2157.  
  2158.  
  2159. //----------  BEq  ----------
  2160.  
  2161. void BEq::print () {
  2162.   fprintf (outputFile, "!   ");
  2163.   printANode (dest);
  2164.   fprintf (outputFile, " = ");
  2165.   printANode (arg1);
  2166.   fprintf (outputFile, " XOR ");
  2167.   printANode (arg2);
  2168.   fprintf (outputFile, "\t\t(bool)\n");
  2169.   getIntoReg1 (arg1, "r1");
  2170.   getIntoReg1 (arg2, "r2");
  2171.   fprintf (outputFile, "\txor\tr1,r2,r1\n");
  2172.   fprintf (outputFile, "\tbtog\t1,r1\n");
  2173.   storeFromReg1 ((VarDecl *) dest, "r1", "r2");
  2174. }
  2175.  
  2176. void IRBEq (AstNode * d, AstNode * a1, AstNode * a2) {
  2177.   linkIR (new BEq (d, a1, a2));
  2178. }
  2179.  
  2180.  
  2181.  
  2182. //----------  BNot  ----------
  2183.  
  2184. void BNot::print () {
  2185.   fprintf (outputFile, "!   ");
  2186.   printANode (dest);
  2187.   fprintf (outputFile, " = ");
  2188.   fprintf (outputFile, " NOT ");
  2189.   printANode (arg);
  2190.   fprintf (outputFile, "\t\t(bool)\n");
  2191.   getIntoReg1 (arg, "r1");
  2192.   fprintf (outputFile, "\tbtog\t1,r1\n");
  2193.   storeFromReg1 ((VarDecl *) dest, "r1", "r2");
  2194. }
  2195.  
  2196. void IRBNot (AstNode * d, AstNode * a) {
  2197.   linkIR (new BNot (d, a));
  2198. }
  2199.  
  2200.  
  2201.  
  2202. //----------  IntEqZero  ----------
  2203.  
  2204. void IntEqZero::print () {
  2205.   fprintf (outputFile, "!   if intIsZero (");
  2206.   printANode (arg);
  2207.   fprintf (outputFile, ") then goto %s\n", label);
  2208.   getIntoReg4 (arg, "r1");
  2209.   fprintf (outputFile, "\tcmp\tr1,r0\n");
  2210.   fprintf (outputFile, "\tbe\t%s\n", label);
  2211. }
  2212.  
  2213. void IRIntEqZero (AstNode * a, char * lab) {
  2214.   linkIR (new IntEqZero (a, lab));
  2215. }
  2216.  
  2217.  
  2218.  
  2219. //----------  IntNeZero  ----------
  2220.  
  2221. void IntNeZero::print () {
  2222.   fprintf (outputFile, "!   if intNotZero (");
  2223.   printANode (arg);
  2224.   fprintf (outputFile, ") then goto %s\n", label);
  2225.   getIntoReg4 (arg, "r1");
  2226.   fprintf (outputFile, "\tcmp\tr1,r0\n");
  2227.   fprintf (outputFile, "\tbne\t%s\n", label);
  2228. }
  2229.  
  2230. void IRIntNeZero (AstNode * a, char * lab) {
  2231.   linkIR (new IntNeZero (a, lab));
  2232. }
  2233.  
  2234.  
  2235.  
  2236. //----------  IntLeZero  ----------
  2237.  
  2238. void IntLeZero::print () {
  2239.   fprintf (outputFile, "!   if ");
  2240.   printANode (arg);
  2241.   fprintf (outputFile, " <= 0 then goto %s\n", label);
  2242.   getIntoReg4 (arg, "r1");
  2243.   fprintf (outputFile, "\tcmp\tr1,r0\n");
  2244.   fprintf (outputFile, "\tble\t%s\n", label);
  2245. }
  2246.  
  2247. void IRIntLeZero (AstNode * a, char * lab) {
  2248.   linkIR (new IntLeZero (a, lab));
  2249. }
  2250.  
  2251.  
  2252.  
  2253. //----------  BoolEqZeroIndirect  ----------
  2254.  
  2255. void BoolEqZeroIndirect::print () {
  2256.   fprintf (outputFile, "!   if boolIsZero (");
  2257.   printANode (arg);
  2258.   fprintf (outputFile, " ) then goto %s\t\t(int)\n", label);
  2259.   getIntoReg4 (arg, "r1");
  2260.   fprintf (outputFile, "\tloadb\t[r1],r1\n");
  2261.   fprintf (outputFile, "\tcmp\tr1,r0\n");
  2262.   fprintf (outputFile, "\tbe\t%s\n", label);
  2263. }
  2264.  
  2265. void IRBoolEqZeroIndirect (VarDecl * a, char * lab) {
  2266.   linkIR (new BoolEqZeroIndirect (a, lab));
  2267. }
  2268.  
  2269.  
  2270.  
  2271. //----------  PrepareArg  ----------
  2272.  
  2273. void PrepareArg::print () {
  2274.   char * label;
  2275.   int i;
  2276.   fprintf (outputFile, "!   Prepare Argument: offset=%d  value=", offset);
  2277.   printANode (tempName);
  2278.   fprintf (outputFile, "  sizeInBytes=%d\n", sizeInBytes);
  2279.   if (sizeInBytes == 1) {
  2280.     getIntoReg1 (tempName, "r1");
  2281.     if (within16Bits (offset-8)) {
  2282.       fprintf (outputFile, "\tstoreb\tr1,[r15+%d]\n", offset-8);
  2283.     } else {
  2284.       fprintf (outputFile, "\tset\t%d,r2\n", offset-8);
  2285.       fprintf (outputFile, "\tstoreb\tr1,[r15+r2]\n");
  2286.     }
  2287.   } else if (sizeInBytes == 4) {
  2288.     getIntoReg4 (tempName, "r1");
  2289.     if (within16Bits (offset-8)) {
  2290.       fprintf (outputFile, "\tstore\tr1,[r15+%d]\n", offset-8);
  2291.     } else {
  2292.       fprintf (outputFile, "\tset\t%d,r2\n", offset-8);
  2293.       fprintf (outputFile, "\tstore\tr1,[r15+r2]\n");
  2294.     }
  2295.   } else if (sizeInBytes == 8) {
  2296.     getIntoReg8 (tempName, "f0", "r1");
  2297.     if (within16Bits (offset-8)) {
  2298.       fprintf (outputFile, "\tfstore\tf0,[r15+%d]\n", offset-8);
  2299.     } else {
  2300.       fprintf (outputFile, "\tset\t%d,r2\n", offset-8);
  2301.       fprintf (outputFile, "\tfstore\tf0,[r15+r2]\n");
  2302.     }
  2303.   } else if ((sizeInBytes < 0) || (sizeInBytes%4 != 0)) {
  2304.     printf ("sizeInBytes = %d\n", sizeInBytes);
  2305.     programLogicError ("In PrepareArg, problems with sizeInBytes");
  2306.   } else {
  2307.     // Generate code to move N bytes into the arg position...
  2308.     //     r3 = counter of words
  2309.     //     r4 = ptr to dest
  2310.     //     r5 = ptr to source
  2311.     //     r1 = word being moved
  2312.     label = newLabel ();
  2313.     if (within16Bits (sizeInBytes / 4)) {
  2314.       fprintf (outputFile, "\tmov\t%d,r3\n", sizeInBytes / 4);
  2315.     } else {
  2316.       fprintf (outputFile, "\tset\t%d,r3\n", sizeInBytes / 4);
  2317.     }
  2318.     if (within16Bits (offset-8)) {
  2319.       fprintf (outputFile, "\tadd\tr15,%d,r4\n", offset-8);
  2320.     } else {
  2321.       fprintf (outputFile, "\tset\t%d,r4\n", offset-8);
  2322.       fprintf (outputFile, "\tadd\tr15,r4,r4\n");
  2323.     }
  2324.     getAddrOfVarIntoReg (tempName, "r5");
  2325.     fprintf (outputFile, "%s:\n", label);
  2326.     fprintf (outputFile, "\tload\t[r5],r1\n");
  2327.     fprintf (outputFile, "\tadd\tr5,4,r5\n");
  2328.     fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2329.     fprintf (outputFile, "\tadd\tr4,4,r4\n");
  2330.     fprintf (outputFile, "\tsub\tr3,1,r3\n");
  2331.     fprintf (outputFile, "\tbne\t%s\n", label);
  2332.   }
  2333. }
  2334.  
  2335. void IRPrepareArg (int off, AstNode * tname, int size) {
  2336.   linkIR (new PrepareArg (off, tname, size));
  2337. }
  2338.  
  2339.  
  2340.  
  2341. //----------  RetrieveResult  ----------
  2342.  
  2343. void RetrieveResult::print () {
  2344.   char * label;
  2345.   int i;
  2346.  
  2347.   fprintf (outputFile, "!   Retrieve Result: targetName=");
  2348.   printANode (targetName);
  2349.   fprintf (outputFile, "  sizeInBytes=%d\n", sizeInBytes);
  2350.   if (sizeInBytes == 1) {
  2351.     fprintf (outputFile, "\tloadb\t[r15],r1\n");
  2352.     storeFromReg1 (targetName, "r1", "r2");
  2353.   } else if (sizeInBytes == 4) {
  2354.     fprintf (outputFile, "\tload\t[r15],r1\n");
  2355.     storeFromReg4 (targetName, "r1", "r2");
  2356.   } else if (sizeInBytes == 8) {
  2357.     fprintf (outputFile, "\tfload\t[r15],f0\n");
  2358.     storeFromReg8 (targetName, "f0", "r2");
  2359.   } else if ((sizeInBytes < 0) || (sizeInBytes%4 != 0)) {
  2360.     printf ("sizeInBytes = %d\n", sizeInBytes);
  2361.     programLogicError ("In RetrieveResult, problems with sizeInBytes");
  2362.   } else {
  2363.     // Generate code to move N bytes from result to target...
  2364.     //     r3 = counter of words
  2365.     //     r4 = ptr to dest
  2366.     //     r5 = ptr to source
  2367.     //     r1 = word being moved
  2368.     label = newLabel ();
  2369.     if (within16Bits (sizeInBytes / 4)) {
  2370.       fprintf (outputFile, "\tmov\t%d,r3\n", sizeInBytes / 4);
  2371.     } else {
  2372.       fprintf (outputFile, "\tset\t%d,r3\n", sizeInBytes / 4);
  2373.     }
  2374.     fprintf (outputFile, "\tmov\tr15,r5\n");
  2375.     getAddrOfVarIntoReg (targetName, "r4");
  2376.     fprintf (outputFile, "%s:\n", label);
  2377.     fprintf (outputFile, "\tload\t[r5],r1\n");
  2378.     fprintf (outputFile, "\tadd\tr5,4,r5\n");
  2379.     fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2380.     fprintf (outputFile, "\tadd\tr4,4,r4\n");
  2381.     fprintf (outputFile, "\tsub\tr3,1,r3\n");
  2382.     fprintf (outputFile, "\tbne\t%s\n", label);
  2383.   }
  2384. }
  2385.  
  2386. void IRRetrieveResult (VarDecl * tname, int size) {
  2387.   linkIR (new RetrieveResult (tname, size));
  2388. }
  2389.  
  2390.  
  2391.  
  2392. //----------  ReturnResult  ----------
  2393.  
  2394. void ReturnResult::print () {
  2395.   char * label;
  2396.   int i;
  2397.  
  2398.   fprintf (outputFile, "!   ReturnResult: ");
  2399.   printANode (tempName);
  2400.   fprintf (outputFile, "  (sizeInBytes=%d)\n", sizeInBytes);
  2401.   if (sizeInBytes == 1) {
  2402.     getIntoReg1 (tempName, "r1");
  2403.     fprintf (outputFile, "\tstoreb\tr1,[r14+8]\n");
  2404.   } else if (sizeInBytes == 4) {
  2405.     getIntoReg4 (tempName, "r1");
  2406.     fprintf (outputFile, "\tstore\tr1,[r14+8]\n");
  2407.   } else if (sizeInBytes == 8) {
  2408.     getIntoReg8 (tempName, "f0", "r1");
  2409.     fprintf (outputFile, "\tfstore\tf0,[r14+8]\n");
  2410.   } else if ((sizeInBytes < 0) || (sizeInBytes%4 != 0)) {
  2411.     printf ("sizeInBytes = %d\n", sizeInBytes);
  2412.     programLogicError ("In ReturnResult, problems with sizeInBytes");
  2413.   } else {
  2414.     // Generate code to move N bytes into the arg position...
  2415.     //     r3 = counter of words
  2416.     //     r4 = ptr to dest (r14+8)
  2417.     //     r5 = ptr to source (tempName)
  2418.     //     r1 = word being moved
  2419.     label = newLabel ();
  2420.     if (within16Bits (sizeInBytes / 4)) {
  2421.       fprintf (outputFile, "\tmov\t%d,r3\n", sizeInBytes / 4);
  2422.     } else {
  2423.       fprintf (outputFile, "\tset\t%d,r3\n", sizeInBytes / 4);
  2424.     }
  2425.     fprintf (outputFile, "\tadd\tr14,8,r4\n");
  2426.     getAddrOfVarIntoReg (tempName, "r5");
  2427.     fprintf (outputFile, "%s:\n", label);
  2428.     fprintf (outputFile, "\tload\t[r5],r1\n");
  2429.     fprintf (outputFile, "\tadd\tr5,4,r5\n");
  2430.     fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2431.     fprintf (outputFile, "\tadd\tr4,4,r4\n");
  2432.     fprintf (outputFile, "\tsub\tr3,1,r3\n");
  2433.     fprintf (outputFile, "\tbne\t%s\n", label);
  2434.   }
  2435. }
  2436.  
  2437. void IRReturnResult (AstNode * tname, int size) {
  2438.   linkIR (new ReturnResult (tname, size));
  2439. }
  2440.  
  2441.  
  2442.  
  2443. //----------  Comment2  ----------
  2444.  
  2445. void Comment2::print () {
  2446.   fprintf (outputFile, "! %s%s%s\n", str1, str2, str3);
  2447. }
  2448.  
  2449. void IRComment2 (char * str1, char * str2, char * str3) {
  2450.   linkIR (new Comment2 (str1, str2, str3));
  2451. }
  2452.  
  2453.  
  2454.  
  2455. //----------  Set  ----------
  2456.  
  2457. void Set::print () {
  2458.   if (within16Bits (initValue)) {
  2459.     fprintf (outputFile, "\tmov\t%d,r1\n", initValue);
  2460.   } else if (initValue == 0x80000000) {
  2461.     fprintf (outputFile, "\tset\t0x80000000,r1\n");
  2462.   } else {
  2463.     fprintf (outputFile, "\tset\t%d,r1\n", initValue);
  2464.   }
  2465.   storeFromReg4 (varDecl, "r1", "r2");
  2466. }
  2467.  
  2468. void IRSet (VarDecl * v, int i) {
  2469.   linkIR (new Set (v, i));
  2470. }
  2471.  
  2472.  
  2473.  
  2474. //----------  Send  ----------
  2475.  
  2476. void Send::print () {
  2477.   fprintf (outputFile, "!   Send message %s\n", methProto->selector->chars);
  2478.   getIntoReg4 (recvr, "r1");
  2479. //  The check for null pointer occurs elsewhere, when the ptr is first encountered.
  2480. //     fprintf (outputFile, "\tcmp\tr1,0\n");
  2481. //     fprintf (outputFile, "\tbe\t_runtimeErrorNullPointer\n");
  2482.   fprintf (outputFile, "\tload\t[r1],r2\n");
  2483.   fprintf (outputFile, "\tcmp\tr2,0\n");
  2484.   fprintf (outputFile, "\tbe\t _runtimeErrorUninitializedObject\n");
  2485.   fprintf (outputFile, "\tstore\tr1,[r15]\n");
  2486.   if (within16Bits (methProto->offset)) {
  2487.     fprintf (outputFile, "\tadd\tr2,%d,r2\n", methProto->offset);
  2488.   } else {
  2489.     fprintf (outputFile, "\tset\t%d,r11\n", methProto->offset);
  2490.     fprintf (outputFile, "\tadd\tr2,r11,r2\n");
  2491.     // printf ("16-bit overflow in IRSend   Method=%s  offset=%d\n",
  2492.     //         methProto->selector->chars, methProto->offset);
  2493.   }
  2494.   fprintf (outputFile, "\tcall\tr2\n");
  2495. }
  2496.  
  2497. void IRSend (VarDecl * r, MethodProto * m) {
  2498.   linkIR (new Send (r, m));
  2499. }
  2500.  
  2501.  
  2502.  
  2503. //----------  LoadSelfPtr  ----------
  2504.  
  2505. void LoadSelfPtr::print () {
  2506.     fprintf (outputFile, "\tload\t[r14+8],r1\n");
  2507.     storeFromReg4 (targetName, "r1", "r2");
  2508. }
  2509.  
  2510. void IRLoadSelfPtr (VarDecl * tname) {
  2511.   linkIR (new LoadSelfPtr (tname));
  2512. }
  2513.  
  2514.  
  2515.  
  2516. //----------  Move  ----------
  2517.  
  2518. void Move::print () {
  2519.   char * label;
  2520.   int i;
  2521.   fprintf (outputFile, "!   Data Move: ");
  2522.   if (targetVar) {
  2523.     printANode (targetVar);
  2524.   }
  2525.   if (targetPtr) {
  2526.     fprintf (outputFile, "*");
  2527.     printANode (targetPtr);
  2528.   }
  2529.   fprintf (outputFile, " = ");
  2530.   if (srcVar) {
  2531.     printANode (srcVar);
  2532.   }
  2533.   if (srcPtr) {
  2534.     fprintf (outputFile, "*");
  2535.     printANode (srcPtr);
  2536.   }
  2537.   fprintf (outputFile, "  (sizeInBytes=%d)\n", sizeInBytes);
  2538.   if ((targetVar==NULL) == (targetPtr==NULL)) {
  2539.     programLogicError ("In Move, targetVar and targetPtr are incorrect");
  2540.   }
  2541.   if ((srcVar==NULL) == (srcPtr==NULL)) {
  2542.     programLogicError ("In Move, srcVar and srcPtr are incorrect");
  2543.   }
  2544.   if (sizeInBytes == 1) {
  2545.     if (srcVar) {
  2546.       // untested...
  2547.       getIntoReg1 (srcVar, "r1");
  2548.     } else if (srcPtr) {
  2549.       getIntoReg4 (srcPtr, "r1");
  2550.       fprintf (outputFile, "\tloadb\t[r1],r1\n");
  2551.     }
  2552.     if (targetVar) {
  2553.       storeFromReg1 (targetVar, "r1", "r2");
  2554.     } else if (targetPtr) {
  2555.       // untested...
  2556.       getIntoReg4 (targetPtr, "r2");
  2557.       fprintf (outputFile, "\tstoreb\tr1,[r2]\n");
  2558.     }
  2559.   } else if (sizeInBytes == 4) {
  2560.     if (srcVar) {
  2561.       getIntoReg4 (srcVar, "r1");
  2562.     } else if (srcPtr) {
  2563.       getIntoReg4 (srcPtr, "r1");
  2564.       fprintf (outputFile, "\tload\t[r1],r1\n");
  2565.     }
  2566.     if (targetVar) {
  2567.       storeFromReg4 (targetVar, "r1", "r2");
  2568.     } else if (targetPtr) {
  2569.       // untested...
  2570.       getIntoReg4 (targetPtr, "r2");
  2571.       fprintf (outputFile, "\tstore\tr1,[r2]\n");
  2572.     }
  2573.   } else if (sizeInBytes == 8) {
  2574.     if (srcVar) {
  2575.       // untested...
  2576.       getIntoReg8 (srcVar, "f1", "r1");
  2577.     } else if (srcPtr) {
  2578.       getIntoReg4 (srcPtr, "r1");
  2579.       fprintf (outputFile, "\tfload\t[r1],f1\n");
  2580.     }
  2581.     if (targetVar) {
  2582.       storeFromReg8 (targetVar, "f1", "r2");
  2583.     } else if (targetPtr) {
  2584.       // untested...
  2585.       getIntoReg4 (targetPtr, "r2");
  2586.       fprintf (outputFile, "\tfstore\tf1,[r2]\n");
  2587.     }
  2588.   } else if ((sizeInBytes < 0) || (sizeInBytes%4 != 0)) {
  2589.     printf ("sizeInBytes = %d\n", sizeInBytes);
  2590.     programLogicError ("In Move, problems with sizeInBytes");
  2591.   } else {
  2592.  
  2593.     if (srcVar) {
  2594.       // untested...
  2595.       getAddrOfVarIntoReg (srcVar, "r5");
  2596.     } else if (srcPtr) {
  2597.       getIntoReg4 (srcPtr, "r5");
  2598.     }
  2599.     if (targetVar) {
  2600.       // untested...
  2601.       getAddrOfVarIntoReg (targetVar, "r4");
  2602.     } else if (targetPtr) {
  2603.       getIntoReg4 (targetPtr, "r4");
  2604.     }
  2605.  
  2606.     if (sizeInBytes == 12) {
  2607.       fprintf (outputFile, "\tload\t[r5],r1\n");
  2608.       fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2609.       fprintf (outputFile, "\tload\t[r5+4],r1\n");
  2610.       fprintf (outputFile, "\tstore\tr1,[r4+4]\n");
  2611.       fprintf (outputFile, "\tload\t[r5+8],r1\n");
  2612.       fprintf (outputFile, "\tstore\tr1,[r4+8]\n");
  2613.     } else if (sizeInBytes == 16) {
  2614.       fprintf (outputFile, "\tload\t[r5],r1\n");
  2615.       fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2616.       fprintf (outputFile, "\tload\t[r5+4],r1\n");
  2617.       fprintf (outputFile, "\tstore\tr1,[r4+4]\n");
  2618.       fprintf (outputFile, "\tload\t[r5+8],r1\n");
  2619.       fprintf (outputFile, "\tstore\tr1,[r4+8]\n");
  2620.       fprintf (outputFile, "\tload\t[r5+12],r1\n");
  2621.       fprintf (outputFile, "\tstore\tr1,[r4+12]\n");
  2622.     } else if (sizeInBytes == 20) {
  2623.       fprintf (outputFile, "\tload\t[r5],r1\n");
  2624.       fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2625.       fprintf (outputFile, "\tload\t[r5+4],r1\n");
  2626.       fprintf (outputFile, "\tstore\tr1,[r4+4]\n");
  2627.       fprintf (outputFile, "\tload\t[r5+8],r1\n");
  2628.       fprintf (outputFile, "\tstore\tr1,[r4+8]\n");
  2629.       fprintf (outputFile, "\tload\t[r5+12],r1\n");
  2630.       fprintf (outputFile, "\tstore\tr1,[r4+12]\n");
  2631.       fprintf (outputFile, "\tload\t[r5+16],r1\n");
  2632.       fprintf (outputFile, "\tstore\tr1,[r4+16]\n");
  2633.     } else if (sizeInBytes == 24) {
  2634.       fprintf (outputFile, "\tload\t[r5],r1\n");
  2635.       fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2636.       fprintf (outputFile, "\tload\t[r5+4],r1\n");
  2637.       fprintf (outputFile, "\tstore\tr1,[r4+4]\n");
  2638.       fprintf (outputFile, "\tload\t[r5+8],r1\n");
  2639.       fprintf (outputFile, "\tstore\tr1,[r4+8]\n");
  2640.       fprintf (outputFile, "\tload\t[r5+12],r1\n");
  2641.       fprintf (outputFile, "\tstore\tr1,[r4+12]\n");
  2642.       fprintf (outputFile, "\tload\t[r5+16],r1\n");
  2643.       fprintf (outputFile, "\tstore\tr1,[r4+16]\n");
  2644.       fprintf (outputFile, "\tload\t[r5+20],r1\n");
  2645.       fprintf (outputFile, "\tstore\tr1,[r4+20]\n");
  2646.     } else {
  2647.       // Generate code to move N bytes...
  2648.       //     r3 = counter of words
  2649.       //     r4 = ptr to dest
  2650.       //     r5 = ptr to source
  2651.       //     r1 = word being moved
  2652.       label = newLabel ();
  2653.       if (within16Bits (sizeInBytes / 4)) {
  2654.         fprintf (outputFile, "\tmov\t%d,r3\n", sizeInBytes / 4);
  2655.       } else {
  2656.         fprintf (outputFile, "\tset\t0x%08x,r3\t\t\t! decimal = %d\n",
  2657.                  sizeInBytes / 4, sizeInBytes / 4);
  2658.       }
  2659.       fprintf (outputFile, "%s:\n", label);
  2660.       fprintf (outputFile, "\tload\t[r5],r1\n");
  2661.       fprintf (outputFile, "\tadd\tr5,4,r5\n");
  2662.       fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2663.       fprintf (outputFile, "\tadd\tr4,4,r4\n");
  2664.       fprintf (outputFile, "\tsub\tr3,1,r3\n");
  2665.       fprintf (outputFile, "\tbne\t%s\n", label);
  2666.     }
  2667.   }
  2668. }
  2669.  
  2670. void IRMove (VarDecl * tarV, VarDecl * tarP, AstNode * srcV, VarDecl * srcP, int sz) {
  2671.   linkIR (new Move (tarV, tarP, srcV, srcP, sz));
  2672. }
  2673.  
  2674.  
  2675.  
  2676. //----------  DynamicObjectMove  ----------
  2677.  
  2678. void DynamicObjectMove::print () {
  2679.   char * label;
  2680.   int i;
  2681.   fprintf (outputFile, "!   Dynamic Object Move: *");
  2682.   printANode (targetPtr);
  2683.   fprintf (outputFile, " = *");
  2684.   printANode (srcPtr);
  2685.   fprintf (outputFile, "\n");
  2686.   // Generate code to get pointers to the objects into r4 and r5...
  2687.   // fprintf (outputFile, "! get ptr to target into r4 and ptr to src into r5...\n");
  2688.   getIntoReg4 (targetPtr, "r4");
  2689.   getIntoReg4 (srcPtr, "r5");
  2690.   // Generate code to make sure the dispatch table ptrs are the same...
  2691.   // fprintf (outputFile, "! make sure the dispatch table ptrs are the same...\n");
  2692.   fprintf (outputFile, "\tload\t[r4],r1\n");
  2693.   fprintf (outputFile, "\tload\t[r5],r3\n");
  2694.   fprintf (outputFile, "\tcmp\tr1,r3\n");
  2695.   fprintf (outputFile, "\tbne\t_runtimeErrorWrongObject3\n");
  2696.   // Generate code to get the size in words into r3...
  2697.   // fprintf (outputFile, "! get size in words into r3...\n");
  2698.   fprintf (outputFile, "\tload\t[r3],r3\n");
  2699.   fprintf (outputFile, "\tload\t[r3+16],r3\n");
  2700.   fprintf (outputFile, "\tcmp\tr3,4\n");
  2701.   fprintf (outputFile, "\tbl\t_runtimeErrorBadObjectSize\n");
  2702.   fprintf (outputFile, "\tsrl\tr3,2,r3\n");
  2703.   // Generate code to move N bytes...
  2704.   //     r3 = counter of words
  2705.   //     r4 = ptr to dest
  2706.   //     r5 = ptr to source
  2707.   //     r1 = word being moved
  2708.   // fprintf (outputFile, "! do the move...\n");
  2709.   label = newLabel ();
  2710.   fprintf (outputFile, "%s:\n", label);
  2711.   fprintf (outputFile, "\tload\t[r5],r1\n");
  2712.   fprintf (outputFile, "\tadd\tr5,4,r5\n");
  2713.   fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2714.   fprintf (outputFile, "\tadd\tr4,4,r4\n");
  2715.   fprintf (outputFile, "\tsub\tr3,1,r3\n");
  2716.   fprintf (outputFile, "\tbne\t%s\n", label);
  2717.   fprintf (outputFile, "! done with move\n");
  2718. }
  2719.  
  2720. void IRDynamicObjectMove (VarDecl * tar, VarDecl * src) {
  2721.   linkIR (new DynamicObjectMove (tar, src));
  2722. }
  2723.  
  2724.  
  2725.  
  2726. //   //----------  ZeroLocal  ----------
  2727. //   
  2728. //   void ZeroLocal::print () {
  2729. //     int i;
  2730. //     if (local->sizeInBytes == 1) {
  2731. //       fprintf (outputFile, "\tstoreb\tr0,[r14+%d]\n", local->offset);
  2732. //     } else {
  2733. //       if (local->sizeInBytes <= 20) {
  2734. //         for (i = 0; i < local->sizeInBytes; i = i + 4) {
  2735. //           fprintf (outputFile, "\tstore\tr0,[r14+%d]\n", local->offset + i);
  2736. //         }
  2737. //       } else {
  2738. //         printf ("UNIMPLEMENTED / UNTESTED: IRZeroLocal with large value\n");
  2739. //       }
  2740. //     }
  2741. //   }
  2742. //   
  2743. //   void IRZeroLocal (Local * loc) {
  2744. //     linkIR (new ZeroLocal (loc));
  2745. //   }
  2746.  
  2747.  
  2748.  
  2749. //----------  CheckDPT  ----------
  2750.  
  2751. void CheckDPT::print () {
  2752.   getIntoReg4 (var, "r1");
  2753.   fprintf (outputFile, "\tload\t[r1],r1\n");
  2754.   fprintf (outputFile, "\tset\t%s,r2\n", classDef->newName);
  2755.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  2756.   fprintf (outputFile, "\tbne\t_runtimeErrorWrongObject\n");
  2757. }
  2758.  
  2759. void IRCheckDPT (VarDecl * v, ClassDef * cl) {
  2760.   linkIR (new CheckDPT (v, cl));
  2761. }
  2762.  
  2763.  
  2764.  
  2765. //----------  CheckDPT2  ----------
  2766.  
  2767. void CheckDPT2::print () {
  2768.   getIntoReg4 (var, "r1");
  2769.   fprintf (outputFile, "\tload\t[r1],r1\n");
  2770.   fprintf (outputFile, "\tset\t%s,r2\n", classDef->newName);
  2771.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  2772.   fprintf (outputFile, "\tbne\t_runtimeErrorWrongObject2\n");
  2773. }
  2774.  
  2775. void IRCheckDPT2 (VarDecl * v, ClassDef * cl) {
  2776.   linkIR (new CheckDPT2 (v, cl));
  2777. }
  2778.  
  2779.  
  2780.  
  2781. //----------  CopyArrays  ----------
  2782.  
  2783. void CopyArrays::print () {
  2784.   char * label;
  2785.   int i;
  2786.   fprintf (outputFile, "!   Dynamic Array Copy: *");
  2787.   printANode (targetPtr);
  2788.   fprintf (outputFile, " = *");
  2789.   printANode (srcPtr);
  2790.   fprintf (outputFile, "\n");
  2791.   // Generate code to get pointers to the arrays into r4 and r5...
  2792.   fprintf (outputFile, "!     get ptr to target into r4 and ptr to src into r5...\n");
  2793.   getIntoReg4 (targetPtr, "r4");
  2794.   getIntoReg4 (srcPtr, "r5");
  2795.   // Generate code to make sure the sizes are the same...
  2796.   fprintf (outputFile, "!     make sure the sizes are the same...\n");
  2797.   fprintf (outputFile, "\tload\t[r4],r1\n");
  2798.   fprintf (outputFile, "\tload\t[r5],r3\n");
  2799.   fprintf (outputFile, "\tcmp\tr1,r3\n");
  2800.   fprintf (outputFile, "\tbne\t_runtimeErrorDifferentArraySizes\n");
  2801.   // Generate code to get the size in words into r3...
  2802.   fprintf (outputFile, "!     get size in words into r3...\n");
  2803.   if (within16Bits (elementSize)) {
  2804.     fprintf (outputFile, "\tmul\tr3,%d,r3\t\t! multiply by size of elements\n", elementSize);
  2805.   } else {
  2806.     // printf ("16-BIT OVERFLOW;  DEST = %s, SOURCE = %s, ELEMENT SIZE = %d\n",
  2807.     //         ((VarDecl *) targetPtr)->id->chars, ((VarDecl *) srcPtr)->id->chars,
  2808.     //         elementSize);
  2809.     fprintf (outputFile, "\tset\t%d,r11\t\t! multiply by size of elements\n", elementSize);
  2810.     fprintf (outputFile, "\tmul\tr3,r11,r3\n");
  2811.   }
  2812.   overflowTest ();
  2813.   fprintf (outputFile, "\tadd\tr3,7,r3\t\t! add 4 and divide by 4, rounding up\n");
  2814.   overflowTest ();
  2815.   fprintf (outputFile, "\tsrl\tr3,2,r3\n");
  2816.   // Generate code to move N bytes...
  2817.   //     r3 = counter of words
  2818.   //     r4 = ptr to dest
  2819.   //     r5 = ptr to source
  2820.   //     r1 = word being moved
  2821.   fprintf (outputFile, "!     do the move...\n");
  2822.   label = newLabel ();
  2823.   fprintf (outputFile, "%s:\n", label);
  2824.   fprintf (outputFile, "\tload\t[r5],r1\n");
  2825.   fprintf (outputFile, "\tadd\tr5,4,r5\n");
  2826.   fprintf (outputFile, "\tstore\tr1,[r4]\n");
  2827.   fprintf (outputFile, "\tadd\tr4,4,r4\n");
  2828.   fprintf (outputFile, "\tsub\tr3,1,r3\n");
  2829.   fprintf (outputFile, "\tbne\t%s\n", label);
  2830.   fprintf (outputFile, "! done with move\n");
  2831. }
  2832.  
  2833. void IRCopyArrays (VarDecl * tar, VarDecl * src, int i) {
  2834.   linkIR (new CopyArrays (tar, src, i));
  2835. }
  2836.  
  2837.  
  2838.  
  2839. //----------  CheckArraySizeInt  ----------
  2840.  
  2841. void CheckArraySizeInt::print () {
  2842.   // This instruction is passed a variable "ptr", which contains the address
  2843.   // of an array, and "numberOfElements", which is the expected size of the
  2844.   // the array.  It signals an error if the array does not have that size.
  2845.   fprintf (outputFile, "!   make sure array has size %d\n", numberOfElements);
  2846.   getIntoReg4 (ptr, "r1");
  2847.   fprintf (outputFile, "\tload\t[r1],r1\n");
  2848.   fprintf (outputFile, "\tset\t%d, r2\n", numberOfElements);
  2849.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  2850.   overflowTest ();
  2851.   fprintf (outputFile, "\tbne\t_runtimeErrorWrongArraySize\n");
  2852. }
  2853.  
  2854. void IRCheckArraySizeInt (VarDecl * p, int i) {
  2855.   linkIR (new CheckArraySizeInt (p, i));
  2856. }
  2857.  
  2858.  
  2859.  
  2860. //----------  CheckArraySizeInt2  ----------
  2861.  
  2862. void CheckArraySizeInt2::print () {
  2863.   // This instruction is passed a variable "ptr", which contains the address
  2864.   // of an array, and "numberOfElements", which is the expected size of the
  2865.   // the array.  It signals an error if the array does not have that size.
  2866.   // HOWEVER: If the array size if zero (i.e., the array is uninitialized), it
  2867.   // ignores the previous size.
  2868.   char * label;
  2869.   fprintf (outputFile, "!   make sure array has size %d\n", numberOfElements);
  2870.   getIntoReg4 (ptr, "r1");
  2871.   fprintf (outputFile, "\tload\t[r1],r1\n");
  2872.   fprintf (outputFile, "\tset\t%d, r2\n", numberOfElements);
  2873.   fprintf (outputFile, "\tcmp\tr1,0\n");
  2874.   label = newLabel ();
  2875.   fprintf (outputFile, "\tbe\t%s\n", label);
  2876.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  2877.   overflowTest ();
  2878.   fprintf (outputFile, "\tbne\t_runtimeErrorWrongArraySize\n");
  2879.   fprintf (outputFile, "%s:\n", label);
  2880. }
  2881.  
  2882. void IRCheckArraySizeInt2 (VarDecl * p, int i) {
  2883.   linkIR (new CheckArraySizeInt2 (p, i));
  2884. }
  2885.  
  2886.  
  2887.  
  2888. //----------  ArrayIndex  ----------
  2889.  
  2890. void ArrayIndex::print () {
  2891.   fprintf (outputFile, "!   Move address of ");
  2892.   printANode (baseAddr);
  2893.   fprintf (outputFile, " [");
  2894.   printANode (indexVal);
  2895.   fprintf (outputFile, " ] into ");
  2896.   printANode (result);
  2897.   fprintf (outputFile, "\n");
  2898.   fprintf (outputFile, "!     make sure index expr is >= 0\n");
  2899.   getIntoReg4 (indexVal, "r2");
  2900.   fprintf (outputFile, "\tcmp\tr2,0\n");
  2901.   fprintf (outputFile, "\tbl\t_runtimeErrorBadArrayIndex\n");
  2902.   fprintf (outputFile, "!     make sure index expr is < array size\n");
  2903.   getIntoReg4 (baseAddr, "r1");
  2904.   fprintf (outputFile, "\tload\t[r1],r3\n");
  2905.   fprintf (outputFile, "\tcmp\tr3,0\n");
  2906.   fprintf (outputFile, "\tble\t_runtimeErrorUninitializedArray\n");
  2907.   fprintf (outputFile, "\tcmp\tr2,r3\n");
  2908.   overflowTest ();
  2909.   fprintf (outputFile, "\tbge\t_runtimeErrorBadArrayIndex\n");
  2910.   fprintf (outputFile, "!     compute address of array element\n");
  2911.   fprintf (outputFile, "\tset\t%d,r3\n", elementSize);
  2912.   fprintf (outputFile, "\tmul\tr2,r3,r2\n");
  2913.   fprintf (outputFile, "\tadd\tr2,4,r2\n");
  2914.   fprintf (outputFile, "\tadd\tr2,r1,r2\n");
  2915.   storeFromReg4 (result, "r2", "r1");
  2916. }
  2917.  
  2918. void IRArrayIndex (VarDecl * b, AstNode * i, VarDecl * r, int sz) {
  2919.   linkIR (new ArrayIndex (b, i, r, sz));
  2920. }
  2921.  
  2922.  
  2923.  
  2924. //----------  TestObjEq  ----------
  2925.  
  2926. void TestObjEq::print () {
  2927.   char * label;
  2928.   fprintf (outputFile, "!   TEST OBJECT EQUALITY: if * ");
  2929.   printANode (ptr1);
  2930.   fprintf (outputFile, " == * ");
  2931.   printANode (ptr2);
  2932.   fprintf (outputFile, " goto %s else goto %s\n", trueLabel, falseLabel);
  2933.  
  2934.   // Generate code to get pointers to the objects into r4 and r5...
  2935.   fprintf (outputFile, "!    get ptr1 into r4 and ptr2 into r5...\n");
  2936.   getIntoReg4 (ptr1, "r4");
  2937.   getIntoReg4 (ptr2, "r5");
  2938.  
  2939.   // Generate code to make sure the dispatch table ptrs are the same...
  2940.   fprintf (outputFile, "!    make sure the dispatch table ptrs are the same...\n");
  2941.   fprintf (outputFile, "\tload\t[r4],r1\n");
  2942.   fprintf (outputFile, "\tcmp\tr1,0\n");
  2943.   fprintf (outputFile, "\tbe\t_runtimeErrorUninitializedObject\n");
  2944.   fprintf (outputFile, "\tload\t[r5],r3\n");
  2945.   fprintf (outputFile, "\tcmp\tr3,0\n");
  2946.   fprintf (outputFile, "\tbe\t_runtimeErrorUninitializedObject\n");
  2947.   fprintf (outputFile, "\tcmp\tr1,r3\n");
  2948.   fprintf (outputFile, "\tbne\t%s\n", falseLabel);
  2949.   // Generate code to get the size in words into r3...
  2950.   fprintf (outputFile, "!    get size in words into r3...\n");
  2951.   fprintf (outputFile, "\tload\t[r3],r3\n");
  2952.   fprintf (outputFile, "\tload\t[r3+16],r3\n");
  2953.   fprintf (outputFile, "\tcmp\tr3,4\n");
  2954.   fprintf (outputFile, "\tbl\t_runtimeErrorBadObjectSize\n");
  2955.   fprintf (outputFile, "\tsrl\tr3,2,r3\n");
  2956.   // Generate code to test N bytes...
  2957.   //     r3 = counter of words
  2958.   //     r4 = ptr to dest -- ptr1
  2959.   //     r5 = ptr to source -- ptr2
  2960.   //     r1 = word from *ptr1
  2961.   //     r2 = word from *ptr2
  2962.   fprintf (outputFile, "!    do the testing in a loop...\n");
  2963.   label = newLabel ();
  2964.   fprintf (outputFile, "%s:\n", label);
  2965.   fprintf (outputFile, "\tload\t[r4],r1\n");
  2966.   fprintf (outputFile, "\tadd\tr4,4,r4\n");
  2967.   fprintf (outputFile, "\tload\t[r5],r2\n");
  2968.   fprintf (outputFile, "\tadd\tr5,4,r5\n");
  2969.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  2970.   fprintf (outputFile, "\tbne\t%s\n", falseLabel);
  2971.   fprintf (outputFile, "\tsub\tr3,1,r3\n");
  2972.   fprintf (outputFile, "\tbne\t%s\n", label);
  2973.   fprintf (outputFile, "\tjmp\t%s\n", trueLabel);
  2974.   fprintf (outputFile, "!    done with test\n");
  2975. }
  2976.  
  2977. void IRTestObjEq (VarDecl * ptr1, VarDecl * ptr2, char * trueLabel, char * falseLabel) {
  2978.   linkIR (new TestObjEq (ptr1, ptr2, trueLabel, falseLabel));
  2979. }
  2980.  
  2981.  
  2982.  
  2983. //----------  ForTest  ----------
  2984.  
  2985. void ForTest::print () {
  2986.   fprintf (outputFile, "!   Perform the FOR-LOOP termination test\n");
  2987.   fprintf (outputFile, "!   if * ");
  2988.   printANode (ptr);
  2989.   fprintf (outputFile, " > ");
  2990.   printANode (stopVal);
  2991.   fprintf (outputFile, " then goto %s\t\t\n", exitLabel);
  2992.   getIntoReg4 (ptr, "r1");
  2993.   fprintf (outputFile, "\tload\t[r1],r1\n");
  2994.   getIntoReg4 (stopVal, "r2");
  2995.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  2996.   overflowTest ();
  2997.   fprintf (outputFile, "\tbg\t%s\n", exitLabel);
  2998. }
  2999.  
  3000. void IRForTest (VarDecl * pt, AstNode * st, char * lab) {
  3001.   linkIR (new ForTest (pt, st, lab));
  3002. }
  3003.  
  3004.  
  3005.  
  3006. //----------  ForTest2  ----------
  3007.  
  3008. void ForTest2::print () {
  3009.   fprintf (outputFile, "!   Perform the FOR-LOOP termination test\n");
  3010.   fprintf (outputFile, "!   if ");
  3011.   printANode (var);
  3012.   fprintf (outputFile, " > ");
  3013.   printANode (stopVal);
  3014.   fprintf (outputFile, " then goto %s\t\t\n", exitLabel);
  3015.   getIntoReg4 (var, "r1");
  3016.   getIntoReg4 (stopVal, "r2");
  3017.   fprintf (outputFile, "\tcmp\tr1,r2\n");
  3018.   overflowTest ();
  3019.   fprintf (outputFile, "\tbg\t%s\n", exitLabel);
  3020. }
  3021.  
  3022. void IRForTest2 (VarDecl * v, AstNode * st, char * lab) {
  3023.   linkIR (new ForTest2 (v, st, lab));
  3024. }
  3025.  
  3026.  
  3027.  
  3028. //----------  IncrVarDirect  ----------
  3029. //
  3030. // IRIncrVarDirect (VarDecl * dest, VarDecl * src, AstNode * incr, int incrInt, int wantOverflowTest)
  3031. //
  3032. // 
  3033. void IncrVarDirect::print () {
  3034.   fprintf (outputFile, "!   ");
  3035.   printANode (dest);
  3036.   fprintf (outputFile, " = ");
  3037.   printANode (src);
  3038.   fprintf (outputFile, " + ");
  3039.   if (incr == NULL) {
  3040.     fprintf (outputFile, "%d\n", incrInt);
  3041.   } else {
  3042.     printANode (incr);
  3043.     fprintf (outputFile, "\n");
  3044.   }
  3045.  
  3046.   getIntoReg4 (src, "r1");
  3047.   if (incr == NULL) {
  3048.     if (within16Bits (incrInt)) {
  3049.       fprintf (outputFile, "\tadd\tr1,%d,r1\n", incrInt);
  3050.     } else {
  3051.       fprintf (outputFile, "\tset\t0x%08x,r2\t\t\t! decimal = %d\n", incrInt, incrInt);
  3052.       fprintf (outputFile, "\tadd\tr1,r2,r1\n");
  3053.     }
  3054.   } else {
  3055.     getIntoReg4 (incr, "r2");
  3056.     fprintf (outputFile, "\tadd\tr1,r2,r1\n");
  3057.   }
  3058.   if (wantOverflowTest) {
  3059.     overflowTest ();
  3060.   }
  3061.   storeFromReg4 (dest, "r1", "r2");
  3062. }
  3063.  
  3064. void IRIncrVarDirect (VarDecl * dest, VarDecl * src,
  3065.                       AstNode * incr, int incrInt, int wantOverflowTest) {
  3066.   linkIR (new IncrVarDirect (dest, src, incr, incrInt, wantOverflowTest));
  3067. }
  3068.  
  3069.  
  3070.  
  3071. //----------  IncrVarIndirect  ----------
  3072.  
  3073. void IncrVarIndirect::print () {
  3074.   getIntoReg4 (ptr, "r1");
  3075.   fprintf (outputFile, "\tload\t[r1],r3\n");
  3076.   if (incr == NULL) {
  3077.     if (within16Bits (incrInt)) {
  3078.       fprintf (outputFile, "\tadd\tr3,%d,r3\n", incrInt);
  3079.     } else {
  3080.       fprintf (outputFile, "\tset\t0x%08x,r2\t\t\t! decimal = %d\n", incrInt, incrInt);
  3081.       fprintf (outputFile, "\tadd\tr3,r2,r3\n");
  3082.     }
  3083.     overflowTest ();
  3084.   } else {
  3085.     getIntoReg4 (incr, "r2");
  3086.     fprintf (outputFile, "\tadd\tr3,r2,r3\n");
  3087.     overflowTest ();
  3088.   }
  3089.   fprintf (outputFile, "\tstore\tr3,[r1]\n");
  3090. }
  3091.  
  3092. void IRIncrVarIndirect (VarDecl * p, VarDecl * i, int i2) {
  3093.   linkIR (new IncrVarIndirect (p, i, i2));
  3094. }
  3095.  
  3096.  
  3097.  
  3098. //----------  MultiplyVarImmed  ----------
  3099.  
  3100. void MultiplyVarImmed::print () {
  3101.   fprintf (outputFile, "!   ");
  3102.   printANode (dest);
  3103.   fprintf (outputFile, " = ");
  3104.   printANode (src);
  3105.   fprintf (outputFile, " * %d\n", ivalue);
  3106.  
  3107.   getIntoReg4 (src, "r1");
  3108.   if (within16Bits (ivalue)) {
  3109.     fprintf (outputFile, "\tmul\tr1,%d,r1\n", ivalue);
  3110.     overflowTest ();
  3111.   } else {
  3112.     fprintf (outputFile, "\tset\t0x%08x,r2\t\t\t! decimal = %d\n", ivalue, ivalue);
  3113.     fprintf (outputFile, "\tmul\tr1,r2,r1\n");
  3114.     overflowTest ();
  3115.   }
  3116.   storeFromReg4 (dest, "r1", "r2");
  3117. }
  3118.  
  3119. void IRMultiplyVarImmed (VarDecl * dest, VarDecl * src, int ivalue) {
  3120.   linkIR (new MultiplyVarImmed (dest, src, ivalue));
  3121. }
  3122.  
  3123.  
  3124.  
  3125. //----------  SwitchReg1  ----------
  3126.  
  3127. void SwitchReg1::print () {
  3128.   getIntoReg4 (expr, "r1");
  3129. }
  3130.  
  3131. void IRSwitchReg1 (AstNode * e) {
  3132.   linkIR (new SwitchReg1 (e));
  3133. }
  3134.  
  3135.  
  3136.  
  3137. //----------  SwitchTestReg1  ----------
  3138.  
  3139. void SwitchTestReg1::print () {
  3140.   if (within16Bits (ivalue)) {
  3141.     fprintf (outputFile, "\tcmp\tr1,%d\n", ivalue);
  3142.   } else {
  3143.     fprintf (outputFile, "\tset\t%d,r2\n", ivalue);
  3144.     fprintf (outputFile, "\tcmp\tr1,r2\n");
  3145.   }
  3146.   fprintf (outputFile, "\tbe\t%s\n", label);
  3147. }
  3148.  
  3149. void IRSwitchTestReg1 (int i, char * l) {
  3150.   linkIR (new SwitchTestReg1 (i, l));
  3151. }
  3152.  
  3153.  
  3154.  
  3155. //----------  SwitchDirect  ----------
  3156.  
  3157. void SwitchDirect::print () {
  3158.   char * okLabel = newLabel ();
  3159.   getIntoReg4 (expr, "r1");
  3160.   // Make sure the expr is not negative...
  3161.   fprintf (outputFile, "!   If ");
  3162.   printANode (expr);
  3163.   fprintf (outputFile, " is not within 16-bits goto default code\n");
  3164.   fprintf (outputFile, "\tsrl\tr1,15,r2\n");
  3165.   fprintf (outputFile, "\tcmp\tr2,0\n");
  3166.   fprintf (outputFile, "\tbe\t%s\n", okLabel);
  3167.   fprintf (outputFile, "\tset\t0x1ffff,r3\n");
  3168.   fprintf (outputFile, "\tcmp\tr2,r3\n");
  3169.   fprintf (outputFile, "\tbne\t%s\n", defaultLabel);
  3170.   fprintf (outputFile, "%s:\n", okLabel);
  3171.   // Make sure the expr is not < lowValue...
  3172.   fprintf (outputFile, "!   If ");
  3173.   printANode (expr);
  3174.   fprintf (outputFile, " is < %d (==smallestCaseValue) goto default code\n", lowValue, highValue);
  3175.   fprintf (outputFile, "\tcmp\tr1,%d\n", lowValue);
  3176.   // (Overflow cannot occur since r1 and lowValue are both within -32768..32767.)
  3177.   fprintf (outputFile, "\tbl\t%s\n", defaultLabel);
  3178.   // Make sure the expr is not > highValue...
  3179.   // (Overflow cannot occur since r1 and highValue are both within -32768..32767.)
  3180.   fprintf (outputFile, "!   If ");
  3181.   printANode (expr);
  3182.   fprintf (outputFile, " is > %d (==greatestCaseValue) goto default code\n", highValue);
  3183.   fprintf (outputFile, "\tcmp\tr1,%d\n", highValue);
  3184.   fprintf (outputFile, "\tbg\t%s\n", defaultLabel);
  3185.   fprintf (outputFile, "!   r1 = (r1-lowValue) * 4 + jumpTableAddr\n");
  3186.   fprintf (outputFile, "\tsub\tr1,%d,r1\n", lowValue);
  3187.   fprintf (outputFile, "\tsll\tr1,2,r1\n");
  3188.   fprintf (outputFile, "\tset\t%s,r2\n", directTable);
  3189.   fprintf (outputFile, "\tadd\tr1,r2,r1\n");
  3190.   fprintf (outputFile, "!   Jump indirect through the jump table\n");
  3191.   fprintf (outputFile, "\tjmp\tr1\n");
  3192. }
  3193.  
  3194. void IRSwitchDirect (AstNode * e, char * tab, char * def, int low, int high) {
  3195.   linkIR (new SwitchDirect (e, tab, def, low, high));
  3196. }
  3197.  
  3198.  
  3199.  
  3200. //----------  SwitchHashJump  ----------
  3201.  
  3202. void SwitchHashJump::print () {
  3203.   char * loopLabel = newLabel ();
  3204.   char * incrLabel = newLabel ();
  3205.   getIntoReg4 (expr, "r1");
  3206.   fprintf (outputFile, "!   r2 = hashValue (r1)\n");
  3207.   fprintf (outputFile, "\trem\tr1,%d,r2\n", tableSize);
  3208.   fprintf (outputFile, "!   LOOP: r5 = addr of hashTable [r2 * 8 + 4]\n");
  3209.   fprintf (outputFile, "%s:\n", loopLabel);
  3210.   fprintf (outputFile, "\tsll\tr2,3,r5\n");
  3211.   fprintf (outputFile, "\tadd\tr5,%s+4,r5\n", tableName);
  3212.   fprintf (outputFile, "!   r3 = the label for this table entry\n");
  3213.   fprintf (outputFile, "\tload\t[r5],r3\n");
  3214.   fprintf (outputFile, "!   If r3 == 0 goto default code\n");
  3215.   fprintf (outputFile, "\tcmp\tr3,0\n");
  3216.   fprintf (outputFile, "\tbe\t%s\n", defaultLabel);
  3217.   fprintf (outputFile, "!   r4 = the value for this table entry\n");
  3218.   fprintf (outputFile, "\tload\t[r5+-4],r4\n");
  3219.   fprintf (outputFile, "!   If r1 != r4 goto %s\n", incrLabel);
  3220.   fprintf (outputFile, "\tcmp\tr1,r4\n");
  3221.   fprintf (outputFile, "\tbne\t%s\n", incrLabel);
  3222.   fprintf (outputFile, "!   Jump indirect through r3\n");
  3223.   fprintf (outputFile, "\tjmp\tr3\n");
  3224.   fprintf (outputFile, "%s:\n", incrLabel);
  3225.   fprintf (outputFile, "!   r2 = r2+1\n");
  3226.   fprintf (outputFile, "\tadd\tr2,1,r2\n");
  3227.   fprintf (outputFile, "!   If r2 != tableSize goto LOOP\n");
  3228.   fprintf (outputFile, "\tcmp\tr2,%d\n", tableSize);
  3229.   fprintf (outputFile, "\tbne\t%s\n", loopLabel);
  3230.   fprintf (outputFile, "!   r2 = 0\n");
  3231.   fprintf (outputFile, "\tmov\t0,r2\n");
  3232.   fprintf (outputFile, "!   Goto LOOP\n");
  3233.   fprintf (outputFile, "\tjmp\t%s\n", loopLabel);
  3234. }
  3235.  
  3236. void IRSwitchHashJump (AstNode * e, char * tab, char * def, int sz) {
  3237.   linkIR (new SwitchHashJump (e, tab, def, sz));
  3238. }
  3239.  
  3240.  
  3241.  
  3242. //----------  Alloc  ----------
  3243.  
  3244. void Alloc::print () {
  3245.   fprintf (outputFile, "!   ");
  3246.   printANode (dest);
  3247.   fprintf (outputFile, " = alloc (");
  3248.   printANode (byteCount);
  3249.   fprintf (outputFile, ")\n");
  3250.  
  3251.   getIntoReg4 (byteCount, "r1");
  3252.   fprintf (outputFile, "\tcall\t_heapAlloc\n");  // Will not return NULL
  3253.   storeFromReg4 (dest, "r1", "r2");
  3254. }
  3255.  
  3256. void IRAlloc (VarDecl * dest, VarDecl * byteCount) {
  3257.   linkIR (new Alloc (dest, byteCount));
  3258. }
  3259.  
  3260.  
  3261.  
  3262. //----------  Free  ----------
  3263.  
  3264. void Free::print () {
  3265.   fprintf (outputFile, "!   Free (");
  3266.   printANode (ptr);
  3267.   fprintf (outputFile, ")\n");
  3268.  
  3269.   getIntoReg4 (ptr, "r1");
  3270.   fprintf (outputFile, "\tcall\t_heapFree\n");
  3271. }
  3272.  
  3273. void IRFree (AstNode * ptr) {
  3274.   linkIR (new Free (ptr));
  3275. }
  3276.  
  3277.  
  3278.  
  3279. //----------  SaveCatchStack  ----------
  3280.  
  3281. void SaveCatchStack::print () {
  3282.   fprintf (outputFile, "!   Save Catch Stack to ");
  3283.   printANode (temp);
  3284.   fprintf (outputFile, "\n");
  3285.   storeFromReg4 (temp, "r12", "r2");
  3286. }
  3287.  
  3288. void IRSaveCatchStack (VarDecl * temp) {
  3289.   linkIR (new SaveCatchStack (temp));
  3290. }
  3291.  
  3292.  
  3293.  
  3294. //----------  RestoreCatchStack  ----------
  3295.  
  3296. void RestoreCatchStack::print () {
  3297.   // This instruction does the following.  Since the code sequence is
  3298.   // is lengthy, most of the work is done in a routine in "runtime.s".
  3299.   //        load   <temp>, r4
  3300.   //        r1 := r12 (CatchStack top ptr)
  3301.   //        r12 := r4
  3302.   //    loop:
  3303.   //        if r1 == r4 goto done
  3304.   //        if r1 == NULL goto _runtimeErrorRestoreCatchStackError
  3305.   //        load   [r1], r2
  3306.   //        push   r2
  3307.   //        push   r4
  3308.   //        free   (r1)
  3309.   //        pop    r4
  3310.   //        pop    r1
  3311.   //        goto   loop
  3312.   //    done:
  3313.  
  3314.   fprintf (outputFile, "!   Restore Catch Stack from ");
  3315.   printANode (temp);
  3316.   fprintf (outputFile, "\n");
  3317.   getIntoReg4 (temp, "r4");
  3318.   fprintf (outputFile, "\tcall\t_RestoreCatchStack\n");    
  3319. }
  3320.  
  3321. void IRRestoreCatchStack (VarDecl * temp) {
  3322.   linkIR (new RestoreCatchStack (temp));
  3323. }
  3324.  
  3325.  
  3326.  
  3327. //----------  PushCatchRecord  ----------
  3328.  
  3329. void PushCatchRecord::print () {
  3330.   // Generate this code...
  3331.   //     r1 = alloc (28)
  3332.   //     r1->0 = r12 (catchStack top ptr)
  3333.   //     r1->4 = errorID
  3334.   //     r1->8 = catchCodeAddr
  3335.   //     r1->12 = r14
  3336.   //     r1->16 = r15
  3337.   //     r1->20 = ptr to source file name
  3338.   //     r1->24 = source line number
  3339.   //     r12 = r1
  3340.   //
  3341.   fprintf (outputFile, "!   Push Catch Record\n");
  3342.   fprintf (outputFile, "\tmov\t28,r1\n");
  3343.   fprintf (outputFile, "\tcall\t_heapAlloc\n");
  3344.   fprintf (outputFile, "\tstore\tr12,[r1]\n");
  3345.   fprintf (outputFile, "\tset\t%s,r2\n", cat->myDef->newName);
  3346.   fprintf (outputFile, "\tstore\tr2,[r1+4]\n");
  3347.   fprintf (outputFile, "\tset\t%s,r2\n", cat->label);
  3348.   fprintf (outputFile, "\tstore\tr2,[r1+8]\n");
  3349.   fprintf (outputFile, "\tstore\tr14,[r1+12]\n");
  3350.   fprintf (outputFile, "\tstore\tr15,[r1+16]\n");
  3351.   fprintf (outputFile, "\tset\t_sourceFileName,r2\n");
  3352.   fprintf (outputFile, "\tstore\tr2,[r1+20]\n");
  3353.   fprintf (outputFile, "\tset\t%d,r2\n", extractLineNumber (cat->tokn));
  3354.   fprintf (outputFile, "\tstore\tr2,[r1+24]\n");
  3355.   fprintf (outputFile, "\tmov\tr1,r12\n");
  3356.   // fprintf (outputFile, "\tdebug\n");
  3357. }
  3358.  
  3359. void IRPushCatchRecord (Catch * cat) {
  3360.   linkIR (new PushCatchRecord (cat));
  3361. }
  3362.  
  3363.  
  3364.  
  3365. //----------  Throw  ----------
  3366.  
  3367. void Throw::print () {
  3368.   fprintf (outputFile, "\tset\t%s,r4\n", errorDecl->newName);
  3369.   fprintf (outputFile, "\tjmp\t_PerformThrow\n");
  3370. }
  3371.  
  3372. void IRThrow (ErrorDecl * errorDecl) {
  3373.   linkIR (new Throw (errorDecl));
  3374. }
  3375.  
  3376.  
  3377.  
  3378. //----------  CopyCatchParm  ----------
  3379.  
  3380. void CopyCatchParm::print () {
  3381.   char * label;
  3382.   int sizeInBytes = parm->sizeInBytes;
  3383.  
  3384.   // fprintf (outputFile, "debug\n");
  3385.  
  3386.   fprintf (outputFile, "!   Copy catch arg to parm %s, offset=%d, throwSideOffset=%d, sizeInBytes=%d\n",
  3387.          parm->id->chars,
  3388.          parm->offset,
  3389.          parm->throwSideOffset,
  3390.          sizeInBytes);
  3391.   // printf ("Parm=%s     Offset=%d    ThrowSideOffset=%d    SizeInBytes=%d\n",
  3392.   //        parm->id->chars,
  3393.   //        parm->offset,
  3394.   //        parm->throwSideOffset,
  3395.   //        sizeInBytes);
  3396.  
  3397.   // At this point, r15 is the old SP, and will be used to acess the src args
  3398.  
  3399.   if (sizeInBytes == 1 || sizeInBytes == 4 || sizeInBytes == 8) {
  3400.     if (within16Bits (parm->throwSideOffset)) {
  3401.       if (sizeInBytes == 1) {
  3402.         fprintf (outputFile, "\tloadb\t[r15+%d], r1\n", parm->throwSideOffset);
  3403.       } else if (sizeInBytes == 4) {
  3404.         fprintf (outputFile, "\tload\t[r15+%d], r1\n", parm->throwSideOffset);
  3405.       } else if (sizeInBytes == 8) {
  3406.         fprintf (outputFile, "\tfload\t[r15+%d], f0\n", parm->throwSideOffset);
  3407.       }
  3408.     } else {
  3409.       // printf ("          throwSideOffset exceeds 16-bit size limitation: %s, %d\n",
  3410.       //         parm->id->chars, parm->throwSideOffset);
  3411.       if (sizeInBytes == 1) {
  3412.         fprintf (outputFile, "\tset\t%d, r11\n", parm->throwSideOffset);
  3413.         fprintf (outputFile, "\tloadb\t[r15+r11], r1\n");
  3414.       } else if (sizeInBytes == 4) {
  3415.         fprintf (outputFile, "\tset\t%d, r11\n", parm->throwSideOffset);
  3416.         fprintf (outputFile, "\tload\t[r15+r11], r1\n");
  3417.       } else if (sizeInBytes == 8) {
  3418.         fprintf (outputFile, "\tset\t%d, r11\n", parm->throwSideOffset);
  3419.         fprintf (outputFile, "\tfload\t[r15+r11], f0\n");
  3420.       }
  3421.     }
  3422.     if (within16Bits (parm->offset)) {
  3423.       if (sizeInBytes == 1) {
  3424.         fprintf (outputFile, "\tstoreb\tr1,[r14+%d]\n", parm->offset);
  3425.       } else if (sizeInBytes == 4) {
  3426.         fprintf (outputFile, "\tstore\tr1,[r14+%d]\n", parm->offset);
  3427.       } else if (sizeInBytes == 8) {
  3428.         fprintf (outputFile, "\tfstore\tf0,[r14+%d]\n", parm->offset);
  3429.       }
  3430.     } else {
  3431.       // printf ("          offset exceeds 16-bit size limitation: %s, %d\n",
  3432.       //         parm->id->chars, parm->offset);
  3433.       if (sizeInBytes == 1) {
  3434.         fprintf (outputFile, "\tset\t%d, r11\n", parm->offset);
  3435.         fprintf (outputFile, "\tstoreb\tr1,[r14+r11]\n");
  3436.       } else if (sizeInBytes == 4) {
  3437.         fprintf (outputFile, "\tset\t%d, r11\n", parm->offset);
  3438.         fprintf (outputFile, "\tstore\tr1,[r14+r11]\n");
  3439.       } else if (sizeInBytes == 8) {
  3440.         fprintf (outputFile, "\tset\t%d, r11\n", parm->offset);
  3441.         fprintf (outputFile, "\tfstore\tf0,[r14+r11]\n");
  3442.       }
  3443.     }
  3444.   } else if ((sizeInBytes < 0) || (sizeInBytes%4 != 0)) {
  3445.     printf ("sizeInBytes = %d\n", sizeInBytes);
  3446.     programLogicError ("In CopyCatchParm, problems with sizeInBytes");
  3447.   } else {
  3448.     // Generate code to move N bytes into the arg position...
  3449.     //     r3 = counter of words
  3450.     //     r4 = ptr to dest
  3451.     //     r5 = ptr to source
  3452.     //     r1 = word being moved
  3453.     label = newLabel ();
  3454.     if (within16Bits (sizeInBytes / 4)) {
  3455.       fprintf (outputFile, "\tmov\t%d,r3\n", sizeInBytes / 4);
  3456.     } else {
  3457.       fprintf (outputFile, "\tset\t%d,r3\n", sizeInBytes / 4);
  3458.       // printf ("In IRCopyCatchParm, sizeInBytes / 4 exceeds 16-bit size limitation: %s\n",
  3459.       //         parm->id->chars);
  3460.     }
  3461.     if (within16Bits (parm->offset)) {
  3462.       fprintf (outputFile, "\tadd\tr14,%d,r4\n", parm->offset);
  3463.     } else {
  3464.       fprintf (outputFile, "\tset\t%d,r11\n", parm->offset);
  3465.       fprintf (outputFile, "\tadd\tr14,r11,r4\n");
  3466.       // printf ("In IRCopyCatchParm, offset exceeds 16-bit size limitation: %s\n",
  3467.       //         parm->id->chars);
  3468.     }
  3469.     if (within16Bits (parm->throwSideOffset)) {
  3470.       fprintf (outputFile, "\tadd\tr15,%d,r5\n", parm->throwSideOffset);
  3471.     } else {
  3472.       fprintf (outputFile, "\tset\t%d,r11\n", parm->throwSideOffset);
  3473.       fprintf (outputFile, "\tadd\tr15,r11,r5\n");
  3474.       // printf ("In IRCopyCatchParm, throwSideOffset exceeds 16-bit size limitation: %s\n",
  3475.       //         parm->id->chars);
  3476.     }
  3477.     fprintf (outputFile, "%s:\n", label);
  3478.     fprintf (outputFile, "\tload\t[r5],r1\n");
  3479.     fprintf (outputFile, "\tadd\tr5,4,r5\n");
  3480.     fprintf (outputFile, "\tstore\tr1,[r4]\n");
  3481.     fprintf (outputFile, "\tadd\tr4,4,r4\n");
  3482.     fprintf (outputFile, "\tsub\tr3,1,r3\n");
  3483.     fprintf (outputFile, "\tbne\t%s\n", label);
  3484.   }
  3485.  
  3486.  
  3487.  
  3488. }
  3489.  
  3490. void IRCopyCatchParm (Parameter * parm) {
  3491.   linkIR (new CopyCatchParm (parm));
  3492. }
  3493.  
  3494.  
  3495.  
  3496. //----------  ResetStack  ----------
  3497.  
  3498. void ResetStack::print () {
  3499.   fprintf (outputFile, "\tmov\tr6,r15\t\t! Done copying args so reset stack\n");
  3500. }
  3501.  
  3502. void IRResetStack () {
  3503.   linkIR (new ResetStack ());
  3504. }
  3505.  
  3506.  
  3507.  
  3508. //----------  IsKindOf  ----------
  3509.  
  3510. void IsKindOf::print () {
  3511.   if (target) {
  3512.     fprintf (outputFile, "!   ");
  3513.     printANode (target);
  3514.     fprintf (outputFile, " = IsKindOf (");
  3515.     printANode (temp);
  3516.     fprintf (outputFile, ", %s)\n", descLab);
  3517.   } else {
  3518.     fprintf (outputFile, "!   if not isKindOf (");
  3519.     printANode (temp);
  3520.     fprintf (outputFile, ", %s) then goto %s\n", descLab, falseLabel);
  3521.   }
  3522.   getIntoReg4 (temp, "r1");
  3523.   fprintf (outputFile, "\tset\t%s,r2\n", descLab);
  3524.   fprintf (outputFile, "\tcall\t_IsKindOf\n");
  3525.   // If we have a target, move r1 into it...
  3526.   if (target) {
  3527.     storeFromReg1 (target, "r1", "r2");
  3528.   // Else, generate a jump if false, and fall thru otherwise...
  3529.   } else {
  3530.     fprintf (outputFile, "\tcmp\tr1,0\n");
  3531.     fprintf (outputFile, "\tbe\t%s\n", falseLabel);
  3532.   }
  3533. }
  3534.  
  3535. void IRIsKindOf (VarDecl * target, VarDecl * temp, char * descLab, char * falseLabel) {
  3536.   linkIR (new IsKindOf (target, temp, descLab, falseLabel));
  3537. }
  3538.  
  3539.  
  3540.  
  3541. //----------  IsInstanceOf  ----------
  3542.  
  3543. void IsInstanceOf::print () {
  3544.   char * retLabel, * return0;
  3545.   if (target) {
  3546.     fprintf (outputFile, "!   ");
  3547.     printANode (target);
  3548.     fprintf (outputFile, " = IsInstanceOf (");
  3549.     printANode (temp);
  3550.     fprintf (outputFile, ", %s)\n", descLab);
  3551.   } else {
  3552.     fprintf (outputFile, "!   if not IsInstanceOf (");
  3553.     printANode (temp);
  3554.     fprintf (outputFile, ", %s) then goto %s\n", descLab, falseLabel);
  3555.   }
  3556.   getIntoReg4 (temp, "r1");
  3557.   fprintf (outputFile, "\tcmp\tr1,0\n");
  3558.   fprintf (outputFile, "\tbe\t_runtimeErrorNullPointer\n");
  3559.   fprintf (outputFile, "\tset\t%s,r2\n", descLab);
  3560.   fprintf (outputFile, "\tload\t[r1],r1\n");
  3561.   fprintf (outputFile, "\tcmp\tr1,0\n");
  3562.   // If we have a target, generate code to move 0 or 1 into it...
  3563.   if (target) {
  3564.     return0 = newLabel ();
  3565.     retLabel = newLabel ();
  3566.     fprintf (outputFile, "\tbe\t%s\n", return0);
  3567.     fprintf (outputFile, "\tcmp\tr1,r2\n");
  3568.     fprintf (outputFile, "\tbne\t%s\n", return0);
  3569.     fprintf (outputFile, "\tmov\t1,r1\n");
  3570.     fprintf (outputFile, "\tjmp\t%s\n", retLabel);
  3571.     fprintf (outputFile, "%s:\n", return0);
  3572.     fprintf (outputFile, "\tmov\t0,r1\n");
  3573.     fprintf (outputFile, "%s:\n", retLabel);
  3574.     storeFromReg1 (target, "r1", "r2");
  3575.   // Else, generate a jump if false, and fall thru otherwise...
  3576.   } else {
  3577.     fprintf (outputFile, "\tbe\t%s\n", falseLabel);
  3578.     fprintf (outputFile, "\tcmp\tr1,r2\n");
  3579.     fprintf (outputFile, "\tbne\t%s\n", falseLabel);
  3580.   }
  3581.   
  3582. }
  3583.  
  3584. void IRIsInstanceOf (VarDecl * target, VarDecl * temp, char * descLab, char * falseLabel) {
  3585.   linkIR (new IsInstanceOf (target, temp, descLab, falseLabel));
  3586. }
  3587.  
  3588.  
  3589.  
  3590. //----------  ZeroMemory  ----------
  3591.  
  3592. void ZeroMemory::print () {
  3593.   char * label;
  3594.   int i;
  3595.   fprintf (outputFile, "!   ZeroMemory: ");
  3596.   if (targetVar) {
  3597.     printANode (targetVar);
  3598.   }
  3599.   if (targetPtr) {
  3600.     fprintf (outputFile, "*");
  3601.     printANode (targetPtr);
  3602.   }
  3603.   fprintf (outputFile, " = zeros  (sizeInBytes=%d)\n", sizeInBytes);
  3604.   if ((targetVar==NULL) == (targetPtr==NULL)) {
  3605.     programLogicError ("In ZeroMemory, targetVar and targetPtr are incorrect");
  3606.   }
  3607.   if (sizeInBytes == 1) {
  3608.     programLogicError ("In ZeroMemory, untested code - 1");
  3609.     if (targetVar) {
  3610.       storeFromReg1 (targetVar, "r0", "r1");
  3611.     } else if (targetPtr) {
  3612.       getIntoReg4 (targetPtr, "r1");
  3613.       fprintf (outputFile, "\tstoreb\tr0,[r1]\n");
  3614.     }
  3615.   } else if (sizeInBytes == 4) {
  3616.     programLogicError ("In ZeroMemory, untested code - 2");
  3617.     if (targetVar) {
  3618.       storeFromReg4 (targetVar, "r0", "r1");
  3619.     } else if (targetPtr) {
  3620.       getIntoReg4 (targetPtr, "r1");
  3621.       fprintf (outputFile, "\tstore\tr0,[r1]\n");
  3622.     }
  3623.   } else if ((sizeInBytes < 0) || (sizeInBytes%4 != 0)) {
  3624.     printf ("sizeInBytes = %d\n", sizeInBytes);
  3625.     programLogicError ("In ZeroMemory, problems with sizeInBytes");
  3626.   } else {
  3627.  
  3628.     if (targetVar) {
  3629.       getAddrOfVarIntoReg (targetVar, "r4");
  3630.     } else if (targetPtr) {
  3631.       getIntoReg4 (targetPtr, "r4");
  3632.     }
  3633.  
  3634.     if (sizeInBytes == 8) {
  3635.       // tested...
  3636.       fprintf (outputFile, "\tstore\tr0,[r4]\n");
  3637.       fprintf (outputFile, "\tstore\tr0,[r4+4]\n");
  3638.     } else if (sizeInBytes == 12) {
  3639.       // tested...
  3640.       fprintf (outputFile, "\tstore\tr0,[r4]\n");
  3641.       fprintf (outputFile, "\tstore\tr0,[r4+4]\n");
  3642.       fprintf (outputFile, "\tstore\tr0,[r4+8]\n");
  3643.     } else if (sizeInBytes == 16) {
  3644.       // tested...
  3645.       fprintf (outputFile, "\tstore\tr0,[r4]\n");
  3646.       fprintf (outputFile, "\tstore\tr0,[r4+4]\n");
  3647.       fprintf (outputFile, "\tstore\tr0,[r4+8]\n");
  3648.       fprintf (outputFile, "\tstore\tr0,[r4+12]\n");
  3649.     } else if (sizeInBytes == 20) {
  3650.       // tested...
  3651.       fprintf (outputFile, "\tstore\tr0,[r4]\n");
  3652.       fprintf (outputFile, "\tstore\tr0,[r4+4]\n");
  3653.       fprintf (outputFile, "\tstore\tr0,[r4+8]\n");
  3654.       fprintf (outputFile, "\tstore\tr0,[r4+12]\n");
  3655.       fprintf (outputFile, "\tstore\tr0,[r4+16]\n");
  3656.     } else if (sizeInBytes == 24) {
  3657.       // tested...
  3658.       fprintf (outputFile, "\tstore\tr0,[r4]\n");
  3659.       fprintf (outputFile, "\tstore\tr0,[r4+4]\n");
  3660.       fprintf (outputFile, "\tstore\tr0,[r4+8]\n");
  3661.       fprintf (outputFile, "\tstore\tr0,[r4+12]\n");
  3662.       fprintf (outputFile, "\tstore\tr0,[r4+16]\n");
  3663.       fprintf (outputFile, "\tstore\tr0,[r4+20]\n");
  3664.     } else {
  3665.       // tested...
  3666.       // Generate code to move N bytes...
  3667.       //     r3 = counter of words
  3668.       //     r4 = ptr to dest
  3669.       label = newLabel ();
  3670.       if (within16Bits (sizeInBytes / 4)) {
  3671.         fprintf (outputFile, "\tmov\t%d,r3\n", sizeInBytes / 4);
  3672.       } else {
  3673.         fprintf (outputFile, "\tset\t0x%08x,r3\t\t\t! decimal = %d\n",
  3674.                  sizeInBytes / 4, sizeInBytes / 4);
  3675.       }
  3676.       fprintf (outputFile, "%s:\n", label);
  3677.       fprintf (outputFile, "\tstore\tr0,[r4]\n");
  3678.       fprintf (outputFile, "\tadd\tr4,4,r4\n");
  3679.       fprintf (outputFile, "\tsub\tr3,1,r3\n");
  3680.       fprintf (outputFile, "\tbne\t%s\n", label);
  3681.     }
  3682.   }
  3683. }
  3684.  
  3685. void IRZeroMemory (VarDecl * tarV, VarDecl * tarP, int sz) {
  3686.   linkIR (new ZeroMemory (tarV, tarP, sz));
  3687. }
  3688.