home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 309_01 / cc8.c < prev    next >
Text File  |  1990-03-20  |  11KB  |  559 lines

  1. /*
  2. HEADER:        Small-C source part 8;
  3. FILENAME:    CC8.C;
  4. AUTHORS:    Dieter H. Flunkert;
  5. COMPILERS:Turbo C V2.0 Medium Memory Model;
  6. SYSTEM: MSDOS (Brian Brown, Nov 1989)
  7. */
  8.  
  9. #include <stdio.h>
  10. #include "ccdef.c"
  11.  
  12. /* external function declarations */
  13. extern int nl(), ol(), ot(), outbyte(), outdec(), outstr(),
  14.   printlabel(), postlabel(), prelabel();
  15.  
  16. /* external variable declaration */
  17. extern int stkp, asmtype;
  18.  
  19. /* declare entry point GLOBAL, it is for the linker */
  20. entry(sname, class) char *sname; int class; {
  21.   if(class!=statik) {
  22.     nl();
  23.     if( asmtype == AS6809 ) ot(".globl ");
  24.     else if( asmtype == AS9 ) outstr("* .global ");
  25.     outstr(sname);nl();
  26.   }
  27.   else {
  28.     nl();
  29.   }
  30.   outstr(sname); outstr(": "); nl();
  31. }
  32.  
  33. /* SWITCH */
  34. sw()
  35. {   ol("jsr ccswitch");
  36. }
  37.  
  38. /*  declare external reference */
  39. declexternal(n) char *n; {
  40.   nl();
  41.   if( asmtype == AS9 ) outstr("* external reference");  else outstr("; external reference");
  42.   nl();
  43.   if( asmtype == AS6809) ot(".globl ");
  44.   else if( asmtype == AS9) outstr("* .global");
  45.   outstr(n);nl();
  46. }
  47.  
  48. /* Begin a comment line for the assembler */
  49. comment()
  50. {
  51.   if( asmtype == AS9 ){ nl(); outbyte('*'); } else outbyte(';');
  52.   outbyte(' ');
  53. }
  54.  
  55. /* print NAME of the module */
  56. defname(n)
  57. char *n;
  58. {
  59.   nl();
  60.   if(asmtype==AS6809) ot(".module ");
  61.   else if(asmtype==AS9) outstr("* .module ");
  62.   outstr(n);
  63.   nl();
  64. }
  65.  
  66. /* Print all assembler info before any code is generated */
  67. header()
  68. {
  69.   comment();
  70.   outstr(VERSION);
  71.   nl();
  72. }
  73.  
  74. /* Print any assembler stuff needed after all code */
  75. trailer()
  76. {
  77.  if( asmtype == AS6809) outstr(";    .end"); else outstr("  END");
  78.  nl();
  79. }
  80.  
  81. /* Fetch a static memory cell into the primary register */
  82. getmem(sym)
  83. char *sym;
  84. {
  85.  int nameoffset;
  86.  if(sym[storage] == statik) nameoffset=symsiz;
  87.  else nameoffset=name;
  88.  if((sym[ident]!=pointer)&&(sym[type]==cchar)) {
  89.   ot("ldb  ");
  90.   outstr(sym+nameoffset);
  91.   nl();
  92.   ol("sex");
  93.  }
  94.  else {
  95.   ot("ldd  ");
  96.   outstr(sym+nameoffset);
  97.   nl();
  98.  }
  99. }
  100.  
  101. /*  load secondary register with value which address is on top of the stack
  102.     Dieter H. Flunkert  13-jul-86 */
  103. loadsec()
  104. {
  105.   ol("ldx  [,s]");
  106. }
  107.  
  108. /* Fetch the address of the specified symbol */
  109. /* into the primary register */
  110. /* changed by Dieter H. Flunkert 16-Jan-1986 */
  111. /* Calculates location depending of ident and type */
  112. getloc(sym)
  113.  char *sym;
  114. { int dec;
  115.  
  116.  ot("leay ");
  117. /* next three lines changed by Dieter Flunkert */
  118.  dec=((sym[offset] & 255) | (sym[offset+1]<<8)) - stkp;
  119.  if((sym[ident]==variable)&(sym[type]==cchar)) ++dec;
  120.  outdec(dec);
  121. /* end change */
  122.  outstr(",s");
  123.  nl();
  124.  ol("tfr  y,d");
  125. }
  126.  
  127. /* Store the primary register into the specified static memory cell */
  128. /* changed for static variables dhf 29-oct-86 */
  129. putmem(sym)
  130. char *sym;
  131. {
  132.   int nameoffset;
  133.   if(sym[storage] == statik) nameoffset=symsiz;
  134.   else nameoffset=name;
  135.   if((sym[ident]!=pointer) & (sym[type]==cchar))
  136.     ot("stb  ");
  137.   else
  138.     ot("std  ");
  139.   outstr(sym+nameoffset);
  140.   nl();
  141. }
  142.  
  143. /* Store the specified object type in the primary register */
  144. /* at the address on the top of the stack */
  145. putstk(typeobj)
  146.  char typeobj;
  147. {
  148.  if(typeobj==cchar)ol("stb  [,s++]");
  149.   else ol("std  [,s++]");
  150.  stkp = stkp + 2;
  151. }
  152.  
  153. /* Fetch the specified object type indirect through the */
  154. /* primary register into the primary register */
  155. indirect(typeobj)
  156.  char typeobj;
  157. {
  158.  ol("pshs d");
  159.  if(typeobj==cchar){ ol("ldb  [,s++]");ol("sex"); }
  160.   else ol("ldd  [,s++]");
  161. }
  162.  
  163. /* Swap the primary and secondary registers */
  164. swap()
  165. { ol("exg  d,x");
  166. }
  167.  
  168. /* Print partial instruction to get an immediate value */
  169. /* into the primary register */
  170. immed()
  171. { ot("ldd  #");
  172. }
  173.  
  174. /* Push the primary register onto the stack */
  175. push()
  176. { ol("pshs d");
  177.  stkp=stkp-2;
  178. }
  179.  
  180. /* push secondary register on stack Dieter H. Flunkert  13-jul-86 */
  181. pushsec() {
  182.   ol("pshs x");
  183.   stkp=stkp-2;
  184. }
  185.  
  186. /* Pop the top of the stack into the secondary register */
  187. pop()
  188. { ol("puls x");
  189.  stkp=stkp+2;
  190. }
  191.  
  192. /* Swap the primary register and the top of the stack */
  193. swapstk()
  194. { ol("puls x");
  195.  ol("pshs d");
  196.  ol("tfr  x,d");
  197. }
  198.  
  199. /* Call the specified subroutine name */
  200. call(sname)
  201.  char *sname;
  202. { ot("jsr ");
  203.  outstr(sname);
  204.  nl();
  205. }
  206.  
  207. /* Return from subroutine */
  208. ret()
  209. { ol("rts");
  210. }
  211.  
  212. /* Perform subroutine call to value on top of stack */
  213. callstk()
  214. { pop();
  215.  ol("jsr  ,x");
  216.  }
  217.  
  218. /* Jump to specified internal label number */
  219. jump(label)
  220. int label;
  221. {
  222.   ot("jmp ");
  223.   postlabel(label);
  224. }
  225.  
  226. /* like testjump but flags the instruction for no optimize */
  227. testnoopt(label)
  228. int label;
  229. {
  230.   ol("cmpd #0");
  231.   ot("lbeq ");
  232.   printlabel(label);
  233.   outstr(" ;_  instruction flagged for non optimize");
  234.   nl();
  235. }
  236.  
  237. /* Test the primary register and jump if false to label */
  238. testjump(label)
  239. int label;
  240. {
  241.   ol("cmpd #0");
  242.   ot("lbeq ");
  243.   postlabel(label);
  244. }
  245.  
  246. /* Test the primary register and jump if true to label */
  247. testtruejump(label)
  248. int label;
  249. {
  250.    ol("cmpd #0");
  251.    ot("lbne ");
  252.    postlabel(label);
  253. }
  254.  
  255. /* Debug feature */
  256. debug(str)
  257. char *str;
  258. {
  259.   ol("jsr debug");
  260.   ot(".fcc ");
  261.   outbyte('"');
  262.   outstr(str);
  263.   outbyte('"');
  264.   outstr(",0");
  265.   nl();
  266. }
  267.  
  268. /* Print pseudo-op to define storage */
  269. defstorage(siz)
  270. int siz;
  271. {
  272.     if( asmtype == AS6809 ) {
  273.         if(siz == 1) ot(".db ");  else ot(".dw ");
  274.     }
  275.     else if( asmtype == AS9 ) {
  276.         if(siz == 1) ot("FCB ");  else ot("FDB ");
  277.     }
  278. }
  279.  
  280. /* reserve memory and init with zero */
  281. dumpzero(siz)
  282. int siz;
  283. {
  284.   if(siz <= 0) return;
  285.   if( asmtype==AS6809) ot(".ds "); else if( asmtype == AS9) ot("RMB ");
  286.   outdec(siz);
  287.   nl();
  288. }
  289.  
  290. /* point to following object(s) */
  291. point()
  292. {
  293.   if( asmtype == AS6809) ol(".dw .+2"); else if( asmtype == AS9 ) ol("FDB  *+2");
  294. }
  295.  
  296. /* Modify the stack pointer to the new value indicated */
  297. modstk(newsp)
  298. int newsp;
  299. {
  300.   int k;
  301.   k=newsp-stkp;
  302.   if(k==0)return (newsp);
  303.   ot("leas ");
  304.   outdec(k);
  305.   outstr(",s");
  306.   nl();
  307.   return (newsp);
  308. }
  309.  
  310.  
  311. /* Double the primary register */
  312. doublereg()
  313. {
  314.   ol("pshs d");
  315.   ol("addd ,s++");
  316. }
  317.  
  318. /* Add the primary and secondary registers (which is on stack) */
  319. /* (results in primary) */
  320. add()
  321. {
  322.   ol("addd ,s++");
  323.   stkp=stkp+2;
  324. }
  325.  
  326. /* Subtract the primary register from the secondary (which is on stack) */
  327. /* (results in primary) */
  328. sub()
  329. {
  330.   swapstk();
  331.   ol("subd ,s++");
  332.   stkp=stkp+2;
  333. }
  334.  
  335. /* Multiply the primary and secondary registers */
  336. /* (results in primary */
  337. mult()
  338. {
  339.   call("ccmult");
  340. }
  341.  
  342. /* Divide the secondary register by the primary */
  343. /* (quotient in primary, remainder in secondary) */
  344. div()
  345. {
  346.   call("ccdiv");
  347. }
  348.  
  349. /* Compute remainder (mod) of secondary register divided */
  350. /* by the primary */
  351. /* (remainder in primary, quotient in secondary) */
  352. mod()
  353. {
  354.   div();
  355.   swap();
  356. }
  357.  
  358. /* Inclusive 'or' the primary and the secondary registers */
  359. /* (results in primary) */
  360. or()
  361. {
  362.   ol("ora  ,s+");
  363.   ol("orb  ,s+");
  364.   stkp=stkp+2;
  365. }
  366.  
  367. /* Exclusive 'or' the primary and seconday registers */
  368. /* (results in primary) */
  369. xor()
  370. {
  371.   ol("eora ,s+");
  372.   ol("eorb ,s+");
  373.   stkp=stkp+2;
  374. }
  375.  
  376. /* 'And' the primary and secondary registers */
  377. /* (results in primary) */
  378. and()
  379. {
  380.   ol("anda ,s+");
  381.   ol("andb ,s+");
  382.   stkp=stkp+2;
  383. }
  384.  
  385. /* Arithmetic shift right the secondary register number of */
  386. /*  times in primary (results in primary) */
  387. asr()
  388. {
  389.   ol("decb");
  390.   if( asmtype == AS6809) ol("blt  .+8");
  391.   else if( asmtype == AS9 ) ol("blt  *+8");
  392.   ol("lsr  ,s");
  393.   ol("ror  1,s");
  394.   if( asmtype == AS6809) ol("bra  .-7");
  395.   else if( asmtype == AS9 ) ol("bra *-7");
  396.   ol("puls d");
  397.   stkp=stkp+2;
  398. }
  399.  
  400. /* Arithmetic left shift the secondary register number of */
  401. /* times in primary (results in primary) */
  402. asl()
  403. {
  404.   ol("decb");
  405.   if( asmtype == AS6809) ol("blt  .+8");
  406.   else if( asmtype == AS9 ) ol("blt  *+8");
  407.   ol("asl  1,s");
  408.   ol("rol  ,s");
  409.   if( asmtype == AS6809) ol("bra  .-7");
  410.   else if( asmtype == AS9 ) ol("bra  *-7");
  411.   ol("puls d");
  412.   stkp=stkp+2;
  413. }
  414.  
  415. /* logical not forms logical not  13-jul-86  Dieter H. Flunkert */
  416. lognot()
  417. {
  418.   if( asmtype == AS6809) ol("beq  .+7");
  419.   else if( asmtype == AS9 ) ol("beq  *+7");
  420.   restcom();
  421. }
  422.  
  423. /* Form two's complement of primary register */
  424. neg()
  425. {
  426.   com();
  427.   ol("addd #1");
  428. }
  429.  
  430. /* Form one's complement of primary register */
  431. com()
  432. {
  433.   ol("coma");
  434.   ol("comb");
  435. }
  436.  
  437. /* Increment the primary register by one */
  438. inc()
  439. {
  440.   ol("addd #1");
  441. }
  442.  
  443. /* Decrement the primary register by one */
  444. dec()
  445. {
  446.   ol("subd #1");
  447. }
  448.  
  449. /* Following are the conditional operators */
  450. /* They compare the secondary register against the primary */
  451. /* and put a literal 1 in the primary if the condition is */
  452. /* true, otherwise they clear the primary register */
  453.  
  454. /* This is the rest of every compare operation */
  455. restcom()
  456. {
  457.   ol("ldd  #0");
  458.   if( asmtype == AS6809) ol("bra  .+5");
  459.   else if( asmtype == AS9 ) ol("bra  *+5");
  460.   ol("ldd  #1");
  461.   stkp=stkp+2; /* adjust stack */
  462. }
  463.  
  464. /* compare primary - contense of stack */
  465. cmpd()
  466. {
  467.   ol("cmpd ,s++");
  468. }
  469.  
  470. /* Test for equal */
  471. eq()
  472. {
  473.   cmpd();
  474.   if( asmtype == AS6809) ol("beq  .+7");
  475.   else if( asmtype == AS9) ol("beq  *+7");
  476.   restcom();
  477. }
  478.  
  479. /* Test for not equal */
  480. ne()
  481. {
  482.   cmpd();
  483.   if( asmtype == AS6809) ol("bne  .+7");
  484.   else if( asmtype == AS9 ) ol("bne   *+7");
  485.   restcom();
  486. }
  487.  
  488. /* Test for less than (signed) */
  489. lt()
  490. {
  491.   cmpd();
  492.   if( asmtype == AS6809) ol("bgt  .+7");
  493.   else if( asmtype == AS9 ) ol("bgt  *+7");
  494.   restcom();
  495. }
  496.  
  497. /* Test for less than or equal to (signed) */
  498. le()
  499. {
  500.   cmpd();
  501.   if( asmtype == AS6809) ol("bge  .+7");
  502.   else if( asmtype == AS9 ) ol("bge  *+7");
  503.   restcom();
  504. }
  505.  
  506. /* Test for greater than (signed) */
  507. gt()
  508. {
  509.   cmpd();
  510.   if( asmtype == AS6809) ol("blt  .+7");
  511.   else if( asmtype == AS9 ) ol("blt  *+7");
  512.   restcom();
  513. }
  514.  
  515. /* Test for greater than or equal to (signed) */
  516. ge()
  517. {
  518.   cmpd();
  519.   if( asmtype == AS6809) ol("ble  .+7");
  520.   else if( asmtype == AS9 ) ol("ble  *+7");
  521.   restcom();
  522. }
  523.  
  524. /* Test for less than (unsigned) */
  525. ult()
  526. {
  527.   cmpd();
  528.   if( asmtype == AS6809) ol("bhi  .+7");
  529.   else if( asmtype == AS9 ) ol("bhi  *+7");
  530.   restcom();
  531. }
  532.  
  533. /* Test for less than or equal to (unsigned) */
  534. ule()
  535. {
  536.   cmpd();
  537.   if( asmtype == AS6809) ol("bhs  .+7");
  538.   else if( asmtype == AS9 ) ol("bhs  *+7");
  539.   restcom();
  540. }
  541.  
  542. /* Test for greater than (unsigned) */
  543. ugt()
  544. {
  545.   cmpd();
  546.   if( asmtype == AS6809) ol("blo  .+7");
  547.   else if( asmtype == AS9 ) ol("blo  *+7");
  548.   restcom();
  549. }
  550.  
  551. /* Test for greater than or equal to (unsigned) */
  552. uge()
  553. {
  554.   cmpd();
  555.   if( asmtype == AS6809) ol("bls  .+7");
  556.   else if( asmtype == AS9 ) ol("bls  *+7");
  557.   restcom();
  558. }
  559.