home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 163_01 / cc51.c < prev    next >
Text File  |  1990-11-21  |  18KB  |  666 lines

  1. /*
  2. ** peephole optimizer
  3. **
  4. ** basically a simple text replacement processor
  5. */
  6.  
  7. char nooptimize = 0; /* this flag disables optimization if set (by debug) */
  8.  
  9. peephole(ptr) char *ptr; {
  10.   while(*ptr) {
  11.     char *ptr1;
  12.     ptr1 = ptr; /* save for later compare */
  13.     /*
  14.     ** if optimization is disabled, skip everything
  15.     ** all patterns start with tab, so look for one
  16.     */
  17.     while(*ptr && (*ptr != '\t'|| nooptimize)) cout(*ptr++,output);
  18.     if(streq(ptr, "\tLEA AX,[BP]")) ptr = optim1(ptr, 12);
  19.     if(streq(ptr, "\tMOV AX,1\n")) ptr = optim3(ptr, 10);
  20.     if(streq(ptr, "\tMOV AX,")) ptr = optim4(ptr, 8);
  21.     if(streq(ptr, "\tPUSH AX\n\tMOV AX,")) ptr = optim5(ptr, 17);
  22.     if(streq(ptr, "\tPUSH AX\n\tLEA AX,[BP]")) ptr = optim6(ptr, 21);
  23.     if(streq(ptr, "\tMOV BX,DX\n\tPOP BX\n")) ptr = optim7(ptr, 19);
  24.     if(streq(ptr, "\tMOV BX,")) ptr = optim8(ptr, 8);
  25.     if(streq(ptr, "\tJNZ $+5\n")) ptr = optim9(ptr+9, "JNZ $+");
  26.     if(streq(ptr, "\tJZ $+5\n")) ptr = optim9(ptr+8, "JZ $+");
  27.     /* additional optimizing logic goes here  */
  28.     if(ptr == ptr1) cout(*ptr++, output);
  29.     }
  30.   }
  31.  
  32. /*
  33. ** optimize strings beginning "\tLEA AX,[BP]"
  34. */
  35. optim1(ptr, bump) char *ptr; int bump; {
  36.   char offset[20], *ptr1, *ptr2;
  37.   ptr1 = ptr + bump; /* skip text already recognized */
  38.   ptr2 = offset;
  39.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save offset from LEA */
  40.   *--ptr2 = 0;
  41.   ++bump;
  42.   ptr2 = ptr; /* save for compare */
  43.   if(streq(ptr1, "\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"))
  44.     ptr = optim11(ptr, bump+35, offset);
  45.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n"))
  46.     ptr = optim12(ptr, bump+24, offset);
  47.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  48.     ot("MOV AL,[BP]");
  49.     outstr(offset);
  50.     outstr(" ;optim1 - 1");
  51.     nl();
  52.     ptr = ptr1 + 24;
  53.     }
  54.   else if(streq(ptr1, "\tPUSH AX\n\tLEA AX,[BP]"))
  55.     ptr = optim13(ptr, bump+21, offset);
  56.   else if(streq(ptr1, "\tPUSH AX\n\tMOV AX,"))
  57.     ptr = optim14(ptr, bump+17, offset);
  58.   else if(streq(ptr1, "\tPUSH AX\n\tMOV SI,AX\n"))
  59.     ptr = optim15(ptr, bump+20, offset);
  60.   return ptr;
  61.   }
  62.  
  63. /*
  64. ** optimize strings beginning
  65. **   "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  66. */
  67. optim11(ptr, bump, offset) char *ptr, offset[]; int bump; {
  68.   char *ptr1, *ptr2;
  69.   ptr1 = ptr + bump; /* skip text already recognized */
  70.   ptr2 = ptr; /* save for compare */
  71.   if(streq(ptr1, "\tINC AX\n\tMOV [BX],AX\n"))
  72.     ptr = optim111(ptr, bump+21, offset);
  73.   else if(streq(ptr1, "\tDEC AX\n\tMOV [BX],AX\n"))
  74.     ptr = optim112(ptr, bump+21, offset);
  75.   return ptr;
  76.   }
  77.  
  78. /*
  79. ** optimize strings beginning
  80. **   "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  81. **   "\tINC AX\n\tMOV [BX],AX\n"
  82. */
  83. optim111(ptr, bump, offset) char *ptr, offset[]; int bump; {
  84.   char *ptr1, *ptr2;
  85.   ptr1 = ptr + bump; /* skip text already recognized */
  86.   ptr2 = ptr; /* save for compare */
  87.   if(streq(ptr1, "\tDEC AX\n\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  88.     if(ptr1[32]) { /* if not end of expression */
  89.       ot("MOV BX,[BP]");
  90.       outstr(offset);
  91.       nl();
  92.       ot("INC WORD PTR [BP]");
  93.       outstr(offset);
  94.       nl();
  95.       ol("MOV AL,[BX] ;optim111 - 1");
  96.       ptr = ptr1 + 32;
  97.       }
  98.     else
  99.       ptr = opti1111(ptr, bump+32, offset);
  100.     }
  101.   else if(streq(ptr1, "\tDEC AX\n")) {
  102.     if(ptr1[8]) { /* if not end of expression */
  103.       ot("MOV AX,[BP]");
  104.       outstr(offset);
  105.       nl();
  106.       ot("INC WORD PTR [BP]");
  107.       outstr(offset);
  108.       outstr(" ;optim111 - 2");
  109.       nl();
  110.       ptr = ptr1 + 8;
  111.       }
  112.     else
  113.       ptr = opti1111(ptr, bump+8, offset);
  114.     }
  115.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  116.     if(ptr1[8]) { /* if not end of expression */
  117.       ot("INC WORD PTR [BP]");
  118.       outstr(offset);
  119.       nl();
  120.       ot("MOV BX,[BP]");
  121.       outstr(offset);
  122.       nl();
  123.       ol("MOV AL,[BX] ;optim111 - 3");
  124.       ptr = ptr1 + 24;
  125.       }
  126.     else
  127.       ptr = opti1111(ptr, bump+24, offset);
  128.     }
  129.   else {
  130.     if(ptr1[0]) { /* if not end of expression */
  131.       ot("INC WORD PTR [BP]");
  132.       outstr(offset);
  133.       nl();
  134.       ot("MOV AX,[BP]");
  135.       outstr(offset);
  136.       outstr(" ;optim111 - 4");
  137.       nl();
  138.       ptr = ptr1;
  139.       }
  140.     else
  141.       ptr = opti1111(ptr, bump, offset);
  142.     }
  143.   return ptr;
  144.   }
  145.  
  146. /*
  147. ** optimize "++" operators on simple auto variables with no destination
  148. */
  149. opti1111(ptr, bump, offset) char *ptr, offset[]; int bump; {
  150.   ot("INC WORD PTR [BP]");
  151.   outstr(offset);
  152.   outstr(" ;opti1111 - 1");
  153.   nl();
  154.   return ptr + bump;
  155.   }
  156.  
  157. /*
  158. ** optimize strings beginning
  159. **   "\tLEA AX,[BP]xxxx\n\tMOV BX,AX\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  160. **   "\tDEC AX\n\tMOV [BX],AX\n"
  161. */
  162. optim112(ptr, bump, offset) char *ptr, offset[]; int bump; {
  163.   char *ptr1, *ptr2;
  164.   ptr1 = ptr + bump; /* skip text already recognized */
  165.   ptr2 = ptr; /* save for compare */
  166.   if(streq(ptr1, "\tINC AX\n\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  167.     if(ptr1[32]) { /* if not end of expression */
  168.       ot("MOV BX,[BP]");
  169.       outstr(offset);
  170.       nl();
  171.       ot("DEC WORD PTR [BP]");
  172.       outstr(offset);
  173.       nl();
  174.       ol("MOV AL,[BX] ;optim112 - 1");
  175.       ptr = ptr1 + 32;
  176.       }
  177.     else
  178.       ptr = opti1121(ptr, bump+32, offset);
  179.     }
  180.   else if(streq(ptr1, "\tINC AX\n")) {
  181.     if(ptr1[8]) { /* if not end of expression */
  182.     ot("MOV AX,[BP]");
  183.     outstr(offset);
  184.     nl();
  185.     ot("DEC WORD PTR [BP]");
  186.     outstr(offset);
  187.     outstr(" ;optim112 - 2");
  188.     nl();
  189.     ptr = ptr1 + 8;
  190.       }
  191.     else
  192.       ptr = opti1121(ptr, bump+8, offset);
  193.     }
  194.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  195.     if(ptr1[24]) { /* if not end of expression */
  196.       ot("DEC WORD PTR [BP]");
  197.       outstr(offset);
  198.       nl();
  199.       ot("MOV BX,[BP]");
  200.       outstr(offset);
  201.       nl();
  202.       ol("MOV AL,[BX] ;optim112 - 3");
  203.       ptr = ptr1 + 24;
  204.       }
  205.     else
  206.       ptr = opti1121(ptr, bump+24, offset);
  207.     }
  208.   else {
  209.     if(ptr1[0]) { /* if not end of expression */
  210.       ot("DEC WORD PTR [BP]");
  211.       outstr(offset);
  212.       nl();
  213.       ot("MOV AX,[BP]");
  214.       outstr(offset);
  215.       outstr(" ;optim112 - 4");
  216.       nl();
  217.       ptr = ptr1;
  218.       }
  219.     else
  220.       ptr = opti1121(ptr, bump, offset);
  221.     }
  222.   return ptr;
  223.   }
  224.  
  225. /*
  226. ** optimize "--" operators on simple auto variables with no destination
  227. */
  228. opti1121(ptr, bump, offset) char *ptr, offset[]; int bump; {
  229.   ot("DEC WORD PTR [BP]");
  230.   outstr(offset);
  231.   outstr(" ;opti1121 - 1");
  232.   nl();
  233.   return ptr + bump;
  234.   }
  235.  
  236. /*
  237. ** optimize strings beginning
  238. **   "\tLEA AX,[BP]xxxx\n\tMOV SI,AX\n\tMOV AX,[SI]\n"
  239. */
  240. optim12(ptr, bump, offset) char *ptr, offset[]; int bump; {
  241.   char *ptr1, *ptr2;
  242.   ptr1 = ptr + bump; /* skip text already recognized */
  243.   ptr2 = ptr; /* save for compare */
  244.   if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n")) {
  245.     ot("MOV SI,[BP]");
  246.     outstr(offset);
  247.     nl();
  248.     ol("MOV AX,[SI] ;optim12 - 1");
  249.     ptr = ptr1 + 24;
  250.     }
  251.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n\tCBW\n\tAND AX,AX\n")) {
  252.     ot("MOV SI,[BP]");
  253.     outstr(offset);
  254.     nl();
  255.     ol("CMP BYTE PTR [SI],0 ;optim12 - 2");
  256.     ptr = ptr1 + 40;
  257.     }
  258.   else if(streq(ptr1, "\tMOV SI,AX\n\tMOV AL,[SI]\n")) {
  259.     ot("MOV SI,[BP]");
  260.     outstr(offset);
  261.     nl();
  262.     ol("MOV AL,[SI] ;optim12 - 3");
  263.     ptr = ptr1 + 24;
  264.     }
  265.   else {
  266.     ot("MOV AX,[BP]");
  267.     outstr(offset);
  268.     outstr(" ;optim12 - 4");
  269.     nl();
  270.     ptr = ptr1;
  271.     }
  272.   return ptr;
  273.   }
  274.  
  275. /*
  276. ** optimize strings beginning "\tLEA AX,[BP]xxxx\n\tPUSH AX\n\tLEA AX,[BP]"
  277. */
  278. optim13(ptr, bump, offset) char *ptr, offset[]; int bump; {
  279.   char offset2[20], *ptr1, *ptr2;
  280.   ptr1 = ptr + bump; /* skip text already recognized */
  281.   ptr2 = offset2;
  282.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save source from LEA */
  283.   *--ptr2 = 0;
  284.   ++bump;
  285.   ptr2 = ptr; /* save for compare */
  286.   if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n\tPOP BX\n\tMOV [BX],AX\n")) {
  287.     ot("MOV AX,[BP]");
  288.     outstr(offset2);
  289.     nl();
  290.     ot("MOV [BP]");
  291.     outstr(offset);
  292.     outstr(",AX ;optim13 - 1");
  293.     nl();
  294.     ptr = ptr1 + 45;
  295.     }
  296.   else if(streq(ptr1,
  297.       "\tMOV SI,AX\n\tMOV AL,[SI]\n\tCBW\n\tPOP BX\n\tMOV [BX],AL\n")) {
  298.     ot("MOV AL,[BP]");
  299.     outstr(offset2);
  300.     nl();
  301.     ol("CBW");
  302.     ot("MOV [BP]");
  303.     outstr(offset);
  304.     outstr(",AL ;optim13 - 2");
  305.     nl();
  306.     ptr = ptr1 + 50;
  307.     }
  308.   return ptr;
  309.   }
  310.  
  311. /*
  312. ** optimize strings beginning "\tLEA AX,[BP]xxxx\n\tPUSH AX\n\tMOV AX,"
  313. */
  314. optim14(ptr, bump, offset) char *ptr, offset[]; int bump; {
  315.   char offset2[20], *ptr1, *ptr2;
  316.   ptr1 = ptr + bump; /* skip text already recognized */
  317.   ptr2 = offset2;
  318.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save source from MOV */
  319.   *--ptr2 = 0;
  320.   ++bump;
  321.   ptr2 = ptr; /* save for compare */
  322.   if(streq(ptr1, "\tPOP BX\n\tMOV [BX],AX\n")) {
  323.     ot("MOV AX,");
  324.     outstr(offset2);
  325.     nl();
  326.     ot("MOV [BP]");
  327.     outstr(offset);
  328.     outstr(",AX ;optim14 - 1");
  329.     nl();
  330.     ptr = ptr1 + 21;
  331.     }
  332.   else if(streq(ptr1, "\tPOP BX\n\tMOV [BX],AL\n")) {
  333.     ot("MOV AX,");
  334.     outstr(offset2);
  335.     nl();
  336.     ot("MOV [BP]");
  337.     outstr(offset);
  338.     outstr(",AL ;optim14 - 2");
  339.     nl();
  340.     ptr = ptr1 + 21;
  341.     }
  342.   return ptr;
  343.   }
  344.  
  345. /*
  346. ** optimize strings beginning
  347. **   "\tLEA AX,[BP]xxxx\n\tPUSH AX\n\tMOV SI,AX\n"
  348. */
  349. optim15(ptr, bump, offset) char *ptr, offset[]; int bump; {
  350.   char *ptr1, *ptr2;
  351.   ptr1 = ptr + bump; /* skip text already recognized */
  352.   ptr2 = ptr; /* save for compare */
  353.   if(streq(ptr1, "\tMOV AX,[SI]\n")) {
  354.     ot("LEA SI,[BP]");
  355.     outstr(offset);
  356.     nl();
  357.     ol("PUSH SI");
  358.     ol("MOV AX,[SI] ;optim15 - 1");
  359.     ptr = ptr1 + 13;
  360.     }
  361.   else if(streq(ptr1, "\tMOV AL,[SI]\n")) {
  362.     ot("LEA SI,[BP]");
  363.     outstr(offset);
  364.     nl();
  365.     ol("PUSH SI");
  366.     ol("MOV AL,[SI] ;optim15 - 2");
  367.     ptr = ptr1 + 13;
  368.     }
  369.   return ptr;
  370.   }
  371.  
  372. /*
  373. ** optimize strings beginning "\tMOV AX,1\n"
  374. */
  375. optim3(ptr, bump) char *ptr; int bump; {
  376.   char *ptr1, *ptr2;
  377.   ptr1 = ptr + bump; /* skip text already recognized */
  378.   ptr2 = ptr; /* save for compare */
  379.   if(streq(ptr1, "\tJE $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n") |
  380.       streq(ptr1, "\tJZ $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n")) {
  381.     ol(";optim3 - 1");
  382.     ptr = optim9(ptr1+36, "JZ $+");
  383.     }
  384.   else if(streq(ptr1, "\tJNE $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n") |
  385.       streq(ptr1, "\tJNZ $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n")) {
  386.     ol(";optim3 - 1B");
  387.     ptr = optim9(ptr1+37, "JNZ $+");
  388.     }
  389.   else if(streq(ptr1, "\tJG $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n")) {
  390.     ol(";optim3 - 2");
  391.     ptr = optim9(ptr1+36, "JG $+");
  392.     }
  393.   else if(streq(ptr1, "\tJL $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n")) {
  394.     ol(";optim3 - 3");
  395.     ptr = optim9(ptr1+36, "JL $+");
  396.     }
  397.   else if(streq(ptr1, "\tJGE $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n")) {
  398.     ol(";optim3 - 4");
  399.     ptr = optim9(ptr1+37, "JGE $+");
  400.     }
  401.   else if(streq(ptr1, "\tJLE $+3\n\tDEC AX\n\tAND AX,AX\n\tJNZ $+5\n")) {
  402.     ol(";optim3 - 5");
  403.     ptr = optim9(ptr1+37, "JLE $+");
  404.     }
  405.   return ptr;
  406.   }
  407.  
  408. /*
  409. ** optimize strings beginning "\tMOV AX,"
  410. */
  411. optim4(ptr, bump) char *ptr; int bump; {
  412.   char offset[20], *ptr1, *ptr2;
  413.   ptr1 = ptr + bump; /* skip text already recognized */
  414.   ptr2 = offset;
  415.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save source from MOV */
  416.   *--ptr2 = 0;
  417.   ++bump;
  418.   ptr2 = ptr; /* save for compare */
  419.   if(streq(ptr1, "\tINC AX\n\tMOV ")) ptr = optim41(ptr, bump+13, offset);
  420.   else if(streq(ptr1, "\tDEC AX\n\tMOV ")) ptr = optim42(ptr, bump+13, offset);
  421.   else if(streq(ptr1, "\tMOV BX,")) ptr = optim43(ptr, bump+8, offset);
  422.   return ptr;
  423.   }
  424.  
  425. /*
  426. ** optimize strings beginning "\tMOV AX,xxxx\n\tINC AX\n\tMOV "
  427. */
  428. optim41(ptr, bump, offset) char *ptr, offset[]; int bump; {
  429.   char *ptr1, *ptr2, *ptr3;
  430.   ptr1 = ptr + bump; /* skip text already recognized */
  431.   ptr2 = ptr; /* save for compare */
  432.   if(streq(ptr1, offset)) {
  433.     ptr3 = offset;
  434.     while(*ptr3++) {++ptr1; ++bump;} /* skip over value */
  435.     if(streq(ptr1, ",AX\n\tDEC AX\n")) {
  436.       if(ptr1[12]) {
  437.         ot("MOV AX,");
  438.         outstr(offset);
  439.         nl();
  440.         ot("INC ");
  441.         outstr(offset);
  442.         outstr(" ;optim41 - 1");
  443.         nl();
  444.         ptr = ptr1 + 12;
  445.         }
  446.       else ptr = optim411(ptr, bump+12, offset);
  447.       }
  448.     else if(streq(ptr1, ",AX\n")) {
  449.       if(ptr1[4]) {
  450.         ot("INC ");
  451.         outstr(offset);
  452.         nl();
  453.         ot("MOV AX,");
  454.         outstr(offset);
  455.         outstr(" ;optim41 - 2");
  456.         nl();
  457.         ptr = ptr1 + 4;
  458.         }
  459.       else ptr = optim411(ptr, bump+4, offset);
  460.       }
  461.     }
  462.   return ptr;
  463.   }
  464.  
  465. /*
  466. ** optimize "++" operators on simple static variables with no destination
  467. */
  468. optim411(ptr, bump, offset) char *ptr, offset[]; int bump; {
  469.   ot("INC ");
  470.   outstr(offset);
  471.   outstr(" ;optim411 - 1");
  472.   nl();
  473.   return ptr + bump;
  474.   }
  475.  
  476. /*
  477. ** optimize strings beginning "\tMOV AX,xxxx\n\tDEC AX\n\tMOV "
  478. */
  479. optim42(ptr, bump, offset) char *ptr, offset[]; int bump; {
  480.   char *ptr1, *ptr2, *ptr3;
  481.   ptr1 = ptr + bump; /* skip text already recognized */
  482.   ptr2 = ptr; /* save for compare */
  483.   if(streq(ptr1, offset)) {
  484.     ptr3 = offset;
  485.     while(*ptr3++) {++ptr1; ++bump;} /* skip over value */
  486.     if(streq(ptr1, ",AX\n\tINC AX\n")) {
  487.       if(ptr1[12]) {
  488.         ot("MOV AX,");
  489.         outstr(offset);
  490.         nl();
  491.         ot("DEC ");
  492.         outstr(offset);
  493.         outstr(" ;optim42 - 1");
  494.         nl();
  495.         ptr = ptr1 + 12;
  496.         }
  497.       else ptr = optim421(ptr, bump+12, offset);
  498.       }
  499.     else if(streq(ptr1, ",AX\n")) {
  500.       if(ptr1[4]) {
  501.         ot("DEC ");
  502.         outstr(offset);
  503.         nl();
  504.         ot("MOV AX,");
  505.         outstr(offset);
  506.         outstr(" ;optim42 - 2");
  507.         nl();
  508.         ptr = ptr1 + 4;
  509.         }
  510.       else ptr = optim421(ptr, bump+4, offset);
  511.       }
  512.     }
  513.   return ptr;
  514.   }
  515.  
  516. /*
  517. ** optimize "--" operators on simple auto variables with no destination
  518. */
  519. optim421(ptr, bump, offset) char *ptr, offset[]; int bump; {
  520.   ot("DEC ");
  521.   outstr(offset);
  522.   outstr(" ;optim421 - 1");
  523.   nl();
  524.   return ptr + bump;
  525.   }
  526.  
  527. /*
  528. ** optimize strings beginning "\tMOV AX,xxxx\n\tMOV BX,"
  529. */
  530. optim43(ptr, bump, offset) char *ptr, offset[]; int bump; {
  531.   char offset2[20], *ptr1, *ptr2;
  532.   ptr1 = ptr + bump; /* skip text already recognized */
  533.   ptr2 = offset2;
  534.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save source from MOV */
  535.   *--ptr2 = 0;
  536.   ++bump;
  537.   ptr2 = ptr; /* save for compare */
  538.   if(streq(ptr1, "\n\tADD AX,BX\n\tMOV SI,AX\n\tMOV AX,[SI]\n")) {
  539.     ot("MOV BX,");
  540.     outstr(offset);
  541.     nl();
  542.     ot("ADD BX,");
  543.     outstr(offset2);
  544.     nl();
  545.     ol("MOV AX,[BX] ;optim43 - 1");
  546.     ptr = ptr1 + 36;
  547.     }
  548.   return ptr; /* "MOV BX," may be separately optimizable */
  549.   }
  550.  
  551. /*
  552. ** optimize strings beginning "\tPUSH AX\n\tMOV AX,"
  553. */
  554. optim5(ptr, bump) char *ptr; int bump; {
  555.   char offset[20], *ptr1, *ptr2;
  556.   ptr1 = ptr + bump; /* skip text already recognized */
  557.   ptr2 = offset;
  558.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save source from MOV */
  559.   *--ptr2 = 0;
  560.   ++bump;
  561.   ptr2 = ptr; /* save for compare */
  562.   if(streq(ptr1, "\tPOP BX\n\tADD AX,BX\n")) {
  563.     ot("ADD AX,");
  564.     outstr(offset);
  565.     outstr(" ;optim5 - 1");
  566.     nl();
  567.     ptr = ptr1 + 19;
  568.     }
  569.   else if(streq(ptr1, "\tPOP BX\n\tSUB AX,BX\n\tNEG AX\n")) {
  570.     ot("SUB AX,");
  571.     outstr(offset);
  572.     outstr(" ;optim5 - 2");
  573.     nl();
  574.     ptr = ptr1 + 27;
  575.     }
  576.   else if(streq(ptr1, "\tPOP BX\n\tSUB AX,BX\n")) {
  577.     ot("SUB AX,");
  578.     outstr(offset);
  579.     nl();
  580.     ol("NEG AX ;optim5 - 3");
  581.     ptr = ptr1 + 19;
  582.     }
  583.   else if(streq(ptr1, "\tPOP BX\n\tCMP BX,AX\n")) {
  584.     ot("CMP AX,");
  585.     outstr(offset);
  586.     outstr(" ;optim5 - 4");
  587.     nl();
  588.     ptr = ptr1 + 19;
  589.     }
  590.   else if(streq(ptr1, "\tPOP BX\n\tXCHG AX,BX\n")) {
  591.     ot("MOV BX,");
  592.     outstr(offset);
  593.     outstr(" ;optim5 - 5");
  594.     nl();
  595.     ptr = ptr1 + 20;
  596.     }
  597.   else if(streq(ptr1, "\tPOP BX\n")) {
  598.     ol("MOV BX,AX");
  599.     ot("MOV AX,");
  600.     outstr(offset);
  601.     outstr(" ;optim5 - 6");
  602.     nl();
  603.     ptr = ptr1 + 8;
  604.     }
  605.   return ptr;
  606.   }
  607.  
  608. /*
  609. ** optimize strings beginning "\tPUSH AX\n\tLEA AX,[BP]"
  610. */
  611. optim6(ptr, bump) char *ptr; int bump; {
  612.   char offset[20], *ptr1, *ptr2;
  613.   ptr1 = ptr + bump; /* skip text already recognized */
  614.   ptr2 = offset;
  615.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save offset from LEA */
  616.   *--ptr2 = 0;
  617.   ++bump;
  618.   ptr2 = ptr; /* save for compare */
  619.   if(streq(ptr1, "\tMOV SI,AX\n\tMOV AX,[SI]\n\tPOP BX\n")) {
  620.     ol("MOV BX,AX");
  621.     ot("MOV AX,[BP]");
  622.     outstr(offset);
  623.     outstr(" ;optim6 - 1");
  624.     nl();
  625.     ptr = ptr1 + 32;
  626.     }
  627.   return ptr;
  628.   }
  629.  
  630. /*
  631. ** optimize strings beginning "\tMOV BX,DX\n\tPOP BX\n"
  632. */
  633. optim7(ptr, bump) char *ptr; int bump; {
  634.   ol(";optim7 - 1");
  635.   return ptr + 11; /* just skip over the useless part */
  636.   }
  637.  
  638. /*
  639. ** optimize strings beginning "\tMOV BX,"
  640. */
  641. optim8(ptr, bump) char *ptr; int bump; {
  642.   char offset[20], *ptr1, *ptr2;
  643.   ptr1 = ptr + bump; /* skip text already recognized */
  644.   ptr2 = offset;
  645.   while((*ptr2++ = *ptr1++) > ' ') ++bump; /* save offset from LEA */
  646.   *--ptr2 = 0;
  647.   ++bump;
  648.   ptr2 = ptr; /* save for compare */
  649.   if(streq(ptr1, "\tADD AX,BX\n")) {
  650.     ot("ADD AX,");
  651.     outstr(offset);
  652.     outstr(" ;optim8 - 1");
  653.     nl();
  654.     ptr = ptr1 + 11;
  655.     }
  656.   else if(streq(ptr1, "\tSUB AX,BX\n")) {
  657.     ot("SUB AX,");
  658.     outstr(offset);
  659.     outstr(" ;optim8 - 2");
  660.     nl();
  661.     ptr = ptr1 + 11;
  662.     }
  663.   return ptr;
  664.   }
  665.  
  666.