home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / misc / smc203.ark / CC42.C < prev    next >
Encoding:
Text File  |  1983-07-28  |  8.8 KB  |  412 lines

  1.  
  2. /*
  3. ** arithmetic routines prefaced with "zz" to keep M80
  4. ** assembler from generating error msgs when this is compiled
  5. */
  6.  
  7. /*
  8. ** add primary and secondary registers (result in primary)
  9. */
  10. zzadd() {ol("DAD D");}
  11.  
  12. /*
  13. ** subtract primary from secondary register (result in primary)
  14. */
  15. zzsub() {zzcall("CCSUB");}
  16.  
  17. /*
  18. ** multiply primary and secondary registers (result in primary)
  19. */
  20. zzmult() {zzcall("CCMULT");}
  21.  
  22. /*
  23. ** divide secondary by primary register
  24. ** (quotient in primary, remainder in secondary)
  25. */
  26. zzdiv() {zzcall("CCDIV");}
  27.  
  28. /*
  29. ** remainder of secondary/primary
  30. ** (remainder in primary, quotient in secondary)
  31. */
  32. zzmod() {zzdiv();swap();}
  33.  
  34. /*
  35. ** inclusive "or" primary and secondary registers
  36. ** (result in primary)
  37. */
  38. zzor() {zzcall("CCOR");}
  39.  
  40. /*
  41. ** exclusive "or" the primary and secondary registers
  42. ** (result in primary)
  43. */
  44. zzxor() {zzcall("CCXOR");}
  45.  
  46. /*
  47. ** "and" primary and secondary registers
  48. ** (result in primary)
  49. */
  50. zzand() {zzcall("CCAND");}
  51.  
  52. /*
  53. ** logical negation of primary register
  54. */
  55. lneg() {zzcall("CCLNEG");}
  56.  
  57. /*
  58. ** arithmetic shift right secondary register
  59. ** number of bits given in primary register
  60. ** (result in primary)
  61. */
  62. zzasr() {zzcall("CCASR");}
  63.  
  64. /*
  65. ** arithmetic shift left secondary register
  66. ** number of bits given in primary register
  67. ** (result in primary)
  68. */
  69. zzasl() {zzcall("CCASL");}
  70.  
  71. /*
  72. ** two's complement primary register
  73. */
  74. neg() {zzcall("CCNEG");}
  75.  
  76. /*
  77. ** one's complement primary register
  78. */
  79. com() {zzcall("CCCOM");}
  80.  
  81. /*
  82. ** increment primary register by one object of whatever size
  83. */
  84. inc(n) int n; {
  85.   while(1) {
  86.     ol("INX H");
  87.     if(--n < 1) break;
  88.     }
  89.   }
  90.  
  91. /*
  92. ** decrement primary register by one object of whatever size
  93. */
  94. dec(n) int n; {
  95.   while(1) {
  96.     ol("DCX H");
  97.     if(--n < 1) break;
  98.     }
  99.   }
  100.  
  101. /*
  102. ** test for equal to
  103. */
  104. zzeq()  {zzcall("CCEQ");}
  105.  
  106. /*
  107. ** test for equal to zero
  108. */
  109. eq0(label) int label; {
  110.   ol("MOV A,H");
  111.   ol("ORA L");
  112.   ot("JNZ ");
  113.   printlabel(label);
  114.   nl();
  115.   }
  116.  
  117. /*
  118. ** test for not equal to
  119. */
  120. zzne()  {zzcall("CCNE");}
  121.  
  122. /*
  123. ** test for not equal to zero
  124. */
  125. ne0(label) int label; {
  126.   ol("MOV A,H");
  127.   ol("ORA L");
  128.   ot("JZ ");
  129.   printlabel(label);
  130.   nl();
  131.   }
  132.  
  133. /*
  134. ** test for less than (signed)
  135. */
  136. zzlt()  {zzcall("CCLT");}
  137.  
  138. /*
  139. ** test for less than to zero
  140. */
  141. lt0(label) int label; {
  142.   ol("XRA A");
  143.   ol("ORA H");
  144.   ot("JP ");
  145.   printlabel(label);
  146.   nl();
  147.   }
  148.  
  149. /*
  150. ** test for less than or equal to (signed)
  151. */
  152. zzle()  {zzcall("CCLE");}
  153.  
  154. /*
  155. ** test for less than or equal to zero
  156. */
  157. le0(label) int label; {
  158.   ol("MOV A,H");
  159.   ol("ORA L");
  160.   ol("JZ $+8");
  161.   ol("XRA A");
  162.   ol("ORA H");
  163.   ot("JP ");
  164.   printlabel(label);
  165.   nl();
  166.   }
  167.  
  168. /*
  169. ** test for greater than (signed)
  170. */
  171. zzgt()  {zzcall("CCGT");}
  172.  
  173. /*
  174. ** test for greater than to zero
  175. */
  176. gt0(label) int label; {
  177.   ol("XRA A");
  178.   ol("ORA H");
  179.   ot("JM ");
  180.   printlabel(label);
  181.   nl();
  182.   ol("ORA L");
  183.   ot("JZ ");
  184.   printlabel(label);
  185.   nl();
  186.   }
  187.  
  188. /*
  189. ** test for greater than or equal to (signed)
  190. */
  191. zzge()  {zzcall("CCGE");}
  192.  
  193. /*
  194. ** test for gteater than or equal to zero
  195. */
  196. ge0(label) int label; {
  197.   ol("XRA A");
  198.   ol("ORA H");
  199.   ot("JM ");
  200.   printlabel(label);
  201.   nl();
  202.   }
  203.  
  204. /*
  205. ** test for less than (unsigned)
  206. */
  207. ult()  {zzcall("CCULT");}
  208.  
  209. /*
  210. ** test for less than to zero (unsigned)
  211. */
  212. ult0(label) int label; {
  213.   ot("JMP ");
  214.   printlabel(label);
  215.   nl();
  216.   }
  217.  
  218. /*
  219. ** test for less than or equal to (unsigned)
  220. */
  221. ule()  {zzcall("CCULE");}
  222.  
  223. /*
  224. ** test for greater than (unsigned)
  225. */
  226. ugt()  {zzcall("CCUGT");}
  227.  
  228. /*
  229. ** test for greater than or equal to (unsigned)
  230. */
  231. uge()  {zzcall("CCUGE");}
  232.  
  233. #ifdef OPTIMIZE
  234. peephole(ptr) char *ptr; {
  235.   while(*ptr) {
  236. #ifdef TAB
  237.     if(streq(ptr, "\tLXI H,0\n\tDAD SP\n\tCALL CCGINT")) {
  238.       if(streq(ptr+30, "\tXCHG;;")) {pp2();ptr=ptr+38;}
  239.       else                        {pp1();ptr=ptr+30;}
  240.       }
  241.     else if(streq(ptr, "\tLXI H,2\n\tDAD SP\n\tCALL CCGINT")) {
  242.       if(streq(ptr+30, "\tXCHG;;")) {pp3(pp2);ptr=ptr+38;}
  243.       else                        {pp3(pp1);ptr=ptr+30;}
  244.       }
  245.     else if(optimize) {
  246.       if(streq(ptr, "\tDAD SP\n\tCALL CCGINT")) {
  247.         ol("CALL CCDSGI");
  248.         ptr=ptr+21;
  249.         }
  250.       else if(streq(ptr, "\tDAD D\n\tCALL CCGINT")) {
  251.         ol("CALL CCDDGI");
  252.         ptr=ptr+20;
  253.         }
  254.       else if(streq(ptr, "\tDAD SP\n\tCALL CCGCHAR")) {
  255.         ol("CALL CCDSGC");
  256.         ptr=ptr+22;
  257.         }
  258.       else if(streq(ptr, "\tDAD D\n\tCALL CCGCHAR")) {
  259.         ol("CALL CCDDGC");
  260.         ptr=ptr+21;
  261.         }
  262.       else if(streq(ptr,
  263. "\tDAD SP\n\tMOV D,H\n\tMOV E,L\n\tCALL CCGINT\n\tINX H\n\tCALL CCPINT")) {
  264.         ol("CALL CCINCI");
  265.         ptr=ptr+59;
  266.         }
  267.       else if(streq(ptr,
  268. "\tDAD SP\n\tMOV D,H\n\tMOV E,L\n\tCALL CCGINT\n\tDCX H\n\tCALL CCPINT")) {
  269.         ol("CALL CCDECI");
  270.         ptr=ptr+59;
  271.         }
  272.       else if(streq(ptr,
  273. "\tDAD SP\n\tMOV D,H\n\tMOV E,L\n\tCALL CCGCHAR\n\tINX H\n\tMOV A,L\n\tSTAX D")) {
  274.         ol("CALL CCINCC");
  275.         ptr=ptr+64;
  276.         }
  277.       else if(streq(ptr,
  278. "\tDAD SP\n\tMOV D,H\n\tMOV E,L\n\tCALL CCGCHAR\n\tDCX H\n\tMOV A,L\n\tSTAX D")) {
  279.         ol("CALL CCDECC");
  280.         ptr=ptr+64;
  281.         }
  282.       else if(streq(ptr, "\tDAD D\n\tPOP D\n\tCALL CCPINT")) {
  283. #ifdef M80
  284.         ol("CALL CCDDPI"); /* unique name in six characters */
  285. #else /* M80 */
  286.         ol("CALL CCDDPDPI");
  287. #endif /* M80 */
  288.         ptr=ptr+27;
  289.         }
  290.       else if(streq(ptr, "\tDAD D\n\tPOP D\n\tMOV A,L\n\tSTAX D")) {
  291. #ifdef M80
  292.         ol("CALL CCDDPC"); /* unique name in six characters */
  293. #else /* M80 */
  294.         ol("CALL CCDDPDPC");
  295. #endif /* M80 */
  296.         ptr=ptr+31;
  297.         }
  298.       else if(streq(ptr, "\tPOP D\n\tCALL CCPINT")) {
  299.         ol("CALL CCPDPI");
  300.         ptr=ptr+20;
  301.         }
  302.       else if(streq(ptr, "\tPOP D\n\tMOV A,L\n\tSTAX D")) {
  303.         ol("CALL CCPDPC");
  304.         ptr=ptr+24;
  305.         }
  306.       /* additional optimizing logic goes here */
  307. #else /* TAB */
  308.     if(streq(ptr," LXI H,0\n DAD SP\n CALL CCGINT")) {
  309.       if(streq(ptr+30, " XCHG;;")) {pp2();ptr=ptr+38;}
  310.       else                        {pp1();ptr=ptr+30;}
  311.       }
  312.     else if(streq(ptr," LXI H,2\n DAD SP\n CALL CCGINT")) {
  313.       if(streq(ptr+30, " XCHG;;")) {pp3(pp2);ptr=ptr+38;}
  314.       else                        {pp3(pp1);ptr=ptr+30;}
  315.       }
  316.     else if(optimize) {
  317.       if(streq(ptr, " DAD SP\n CALL CCGINT")) {
  318.         ol("CALL CCDSGI");
  319.         ptr=ptr+21;
  320.         }
  321.       else if(streq(ptr, " DAD D\n CALL CCGINT")) {
  322.         ol("CALL CCDDGI");
  323.         ptr=ptr+20;
  324.         }
  325.       else if(streq(ptr, " DAD SP\n CALL CCGCHAR")) {
  326.         ol("CALL CCDSGC");
  327.         ptr=ptr+22;
  328.           }
  329.       else if(streq(ptr, " DAD D\n CALL CCGCHAR")) {
  330.         ol("CALL CCDDGC");
  331.         ptr=ptr+21;
  332.         }
  333.       else if(streq(ptr,
  334. " DAD SP\n MOV D,H\n MOV E,L\n CALL CCGINT\n INX H\n CALL CCPINT")) {
  335.         ol("CALL CCINCI");
  336.         ptr=ptr+59;
  337.         }
  338.       else if(streq(ptr,
  339. " DAD SP\n MOV D,H\n MOV E,L\n CALL CCGINT\n DCX H\n CALL CCPINT")) {
  340.         ol("CALL CCDECI");
  341.         ptr=ptr+59;
  342.         }
  343.       else if(streq(ptr,
  344. " DAD SP\n MOV D,H\n MOV E,L\n CALL CCGCHAR\n INX H\n MOV A,L\n STAX D")) {
  345.         ol("CALL CCINCC");
  346.         ptr=ptr+64;
  347.         }
  348.       else if(streq(ptr,
  349. " DAD SP\n MOV D,H\n MOV E,L\n CALL CCGCHAR\n DCX H\n MOV A,L\n STAX D")) {
  350.         ol("CALL CCDECC");
  351.         ptr=ptr+64;
  352.         }
  353.       else if(streq(ptr, " DAD D\n POP D\n CALL CCPINT")) {
  354. #ifdef M80
  355.         ol("CALL CCDDPI"); /* unique name in six characters */
  356. #else /* M80 */
  357.         ol("CALL CCDDPDPI");
  358. #endif /* M80 */
  359.         ptr=ptr+27;
  360.         }
  361.       else if(streq(ptr, " DAD D\n POP D\n MOV A,L\n STAX D")) {
  362. #ifdef M80
  363.         ol("CALL CCDDPC"); /* unique name in six characters */
  364. #else /* M80 */
  365.         ol("CALL CCDDPDPC");
  366. #endif /* M80 */
  367.         ptr=ptr+31;
  368.         }
  369.       else if(streq(ptr, " POP D\n CALL CCPINT")) {
  370.         ol("CALL CCPDPI");
  371.         ptr=ptr+20;
  372.         }
  373.       else if(streq(ptr, " POP D\n MOV A,L\n STAX D")) {
  374.         ol("CALL CCPDPC");
  375.         ptr=ptr+24;
  376.         }
  377.       /* additional optimizing logic goes here */
  378. #endif /* TAB */
  379.       else cout(*ptr++, output);
  380.       }
  381.     else cout(*ptr++, output);
  382.     }
  383.   }
  384.  
  385. pp1() {
  386.   ol("POP H");
  387.   ol("PUSH H");
  388.   }
  389.  
  390. pp2() {
  391.   ol("POP D");
  392.   ol("PUSH D");
  393.   }
  394.  
  395. #ifdef FULLC
  396. pp3(pp) int (*pp)(); {
  397.   ol("POP B");
  398.   (*pp)();
  399.   ol("PUSH B");
  400.   }
  401.  
  402. #else  /* FULLC */
  403. pp3(pp) int pp; {
  404.   ol("POP B");
  405.   pp();
  406.   ol("PUSH B");
  407.   }
  408. #endif  /* FULLC */
  409.  
  410. #endif /* OPTIMIZE */
  411. 
  412. D?CC1     C   F⌠CC11    C   Im(