home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d141 / smallc.lha / SmallC / CC78.C < prev    next >
C/C++ Source or Header  |  1988-05-15  |  13KB  |  658 lines

  1. /* Start of G68KCC78.C */
  2.  
  3. /* 2/27/88 */
  4.  
  5. test(label)
  6.         int label;
  7.         {
  8.         needbrack("(");
  9.         expression();
  10.         needbrack(")");
  11.         testjump(label);
  12.         }
  13.  
  14. constant(val)
  15.         int val[];
  16.         {
  17.         if(number(val))
  18.                 immval(val[0]);
  19.         else if(pstr(val))
  20.                 immval(val[0]);
  21.         else if(qstr(val))
  22.                 {
  23.                 ot("MOVE.L #");
  24.                 printlabel(litlab);
  25.                 outbyte('+');
  26.                 outdec(val[0]);
  27.                 outasm(",BX");
  28.                 nl();
  29.                 }
  30.         else return 0;
  31.         return 1;
  32.         }
  33.  
  34. number(val)
  35.         int val[];
  36.         {
  37.         int k, minus;
  38.         char c;
  39.         k=minus=1;
  40.         while(k)
  41.                 {
  42.                 k=0;
  43.                 if(match("+"))
  44.                         k=1;
  45.                 if(match("-"))
  46.                         {
  47.                         minus=(-minus);
  48.                         k=1;
  49.                         }
  50.                 }
  51.         if(numeric(ch())==0)
  52.                 return 0;
  53.         while (numeric(ch()))
  54.                 {
  55.                 c=inbyte();
  56.                 k=k*10+(c-'0');
  57.                 }
  58.         if(minus<0)
  59.                 k=(-k);
  60.         val[0]=k;
  61.         return 1;
  62.         }
  63.  
  64. pstr(val)
  65.         int val[];
  66.         {
  67.         int k;
  68.         char c;
  69.         k=0;
  70.         if(match("'")==0)
  71.                 return 0;
  72.         while ((c=gch())!=39)
  73.                 k=(k&255)*256 + (c&127);
  74.         val[0]=k;
  75.         return 1;
  76.         }
  77.  
  78. qstr(val)
  79.         int val[];
  80.         {
  81.         char c;
  82.         if(match(quote)==0)
  83.                 return 0;
  84.         val[0]=litptr;
  85.         while (ch()!='"')
  86.                 {
  87.                 if(ch()==0) break;
  88.                 if(litptr>=litmax)
  89.                         {
  90.                         error("string space exhausted");
  91.                         while (match(quote)==0)
  92.                                 if(gch()==0) break;
  93.                         return 1;
  94.                         }
  95.                 litq[litptr++]=gch();
  96.                 }
  97.         gch();
  98.         litq[litptr++]=0;
  99.         return 1;
  100.         }
  101.  
  102. /*      >>>>>> start of cc8 <<<<<<<     */
  103.  
  104. /* Begin a comment line for the assembler */
  105. comment()
  106.         {
  107.         outbyte(';');
  108.         }
  109.  
  110. /* Put out assembler info before any code is generated */
  111. header()
  112.         {
  113.         comment();
  114.         outstr(BANNER);
  115.         nl();
  116.         comment();
  117.         outstr(VERSION);
  118.         nl();
  119.         comment();
  120.         outstr(AUTHOR);
  121.         nl();
  122.         comment();
  123.         nl();
  124.         outasm("BX EQUR D0"); nl();
  125.         outasm("DX EQUR D1"); nl();
  126.         ol("SECTION ONE");
  127.         if(mainmode)
  128.                 {
  129.                 ol("XREF CCINIT");
  130.                 ol("XREF CCWRAP");
  131.                 ol("JSR CCINIT");
  132.                 ol("BSR QZMAIN");
  133.                 ol("JSR CCWRAP");
  134.                 ol("RTS");
  135.                 }
  136.         }
  137.  
  138. /* Print any assembler stuff needed after all code */
  139. trailer()
  140.         {
  141.         nl();                   /* 6 May 80 rj errorsummary() now goes to console */
  142.         comment();
  143.         outstr(" --- End of Compilation ---");
  144.         nl();
  145.         ol("END");
  146.         }
  147.  
  148. /* Print out a name such that it won't annoy the assembler */
  149. /*      (by matching anything reserved, like opcodes.) */
  150. /*      gtf 4/7/80 */
  151. outname(sname)
  152.         char *sname;
  153.         {
  154.         int len, i, j;
  155.         outasm("QZ");
  156.         len = strlen(sname);
  157.         if(len>(asmpref+asmsuff))
  158.                 {
  159.                 i = asmpref;
  160.                 len = len-asmpref-asmsuff;
  161.                 while(i-- > 0)
  162.                         outbyte(raise(*sname++));
  163.                 while(len-- > 0)
  164.                         sname++;
  165.                 while(*sname)
  166.                         outbyte(raise(*sname++));
  167.                 }
  168.         else outasm(sname);
  169.         }
  170.  
  171. mem2pri(size,sym)
  172.         char size, *sym;
  173.         {
  174.         ot("MOVE.");
  175.         outbyte(size);
  176.         outbyte(' ');
  177.         outname(sym);
  178.         outasm(",BX");
  179.         nl();
  180.         }
  181.  
  182. extend()
  183.         {
  184.         ol("EXT.W BX");
  185.         ol("EXT.L BX");
  186.         }
  187.  
  188. /* Fetch a static memory cell into the primary register */
  189. getmem(sym)
  190.         char *sym;
  191.         {
  192.         if((sym[ident]!=pointer) & (sym[type]==cchar))
  193.                 {
  194.                 mem2pri('B',sym);
  195.                 extend();
  196.                 }
  197.         else mem2pri('L',sym);
  198.         }
  199.  
  200. /* Fetch the address of the specified symbol */
  201. /*      into the primary register */
  202. getloc(sym)
  203.         char *sym;
  204.         {
  205.         ol("MOVE.L SP,BX");
  206.         ot("ADDI.L #");
  207.         outdec(((sym[offset]&255) | ((sym[offset+1])<<8)) - Zsp);
  208.         outasm(",BX");
  209.         nl();
  210.         }
  211.  
  212. /* Store the primary register into the specified */
  213. /*      static memory cell */
  214. putmem(sym)
  215.         char *sym;
  216.         {
  217.         ot("MOVE.");
  218.         if((sym[ident]!=pointer) & (sym[type]==cchar))
  219.                 outbyte('B');
  220.         else outbyte('L');
  221.         outasm(" BX,");
  222.         outname(sym/*+name*/);   /* remove comment if optimize is working */
  223.         nl();
  224.         }
  225.  
  226. /* Store the specified object type in the primary register */
  227. /*      at the address on the top of the stack */
  228. putstk(typeobj)
  229.         char typeobj;
  230.         {
  231.         ol("MOVE.L (SP)+,A0");
  232.         Zsp=Zsp+4;
  233.         ot("MOVE.");
  234.         if((typeobj==cint) | (typeobj==cchararg))
  235.                 outbyte('L');
  236.         else outbyte('B');
  237.         outasm(" BX,(A0)");
  238.         nl();
  239.         }
  240.  
  241. ind2pri(size)
  242.         char size;
  243.         {
  244.         ot("MOVE.");
  245.         outbyte(size);
  246.         outasm(" (A0),BX");
  247.         nl();
  248.         }
  249.  
  250. /* Fetch the specified object type indirect through the */
  251. /*      primary register into the primary register */
  252. indirect(typeobj)
  253.         char typeobj;
  254.         {
  255.         ol("MOVE.L BX,A0");
  256.         if(typeobj==cchar)
  257.                 {
  258.                 ind2pri('B');
  259.                 extend();
  260.                 }
  261.         else ind2pri('L');
  262.         }
  263.  
  264. /* Swap the primary and secondary registers */
  265. swap()
  266.         {
  267.         ol("EXG BX,DX");
  268.         }
  269.  
  270. /* Load literal value into primary register */
  271. immval(val)
  272.         int val;
  273.         {
  274.         ot("MOVE.L #");
  275.         outdec(val);
  276.         outasm(",BX");
  277.         nl();
  278.         }
  279.  
  280. /* Load address of static cell into primary register */
  281. immlabel(ptr)
  282.         char *ptr;
  283.         {
  284.         ot("MOVE.L #");
  285.         outname(ptr);
  286.         outasm(",BX");
  287.         nl();
  288.         }
  289.  
  290. /* Push the primary register onto the stack */
  291. zpush()
  292.         {
  293.         ol("MOVE.L BX,-(SP)");
  294.         Zsp=Zsp-4;
  295.         }
  296.  
  297. /* Pop the top of the stack into the secondary register */
  298. zpop()
  299.         {
  300.         ol("MOVE.L (SP)+,DX");
  301.         Zsp=Zsp+4;
  302.         }
  303.  
  304. /* Swap the primary register and the top of the stack */
  305. swapstk()
  306.         {
  307.         ol("MOVE.L (SP),A0");
  308.         ol("EXG BX,A0");
  309.         ol("MOVE.L A0,(SP)");
  310.         }
  311.  
  312. /* Call the specified subroutine name */
  313. zcall(sname)
  314.         char *sname;
  315.         {
  316.         ot("JSR ");
  317.         outname(sname);
  318.         nl();
  319.         addext(sname,userfunc);
  320.         }
  321.  
  322. /* Call a run-time library routine */
  323. callrts(sname)
  324.         char *sname;
  325.         {
  326.         ot("JSR ");
  327.         outasm(sname);
  328.         nl();
  329.         addext(sname,rtsfunc);
  330.         }
  331.  
  332. /* Return from subroutine */
  333. zret()
  334.         {
  335.         ol("RTS");
  336.         }
  337.  
  338. /* Perform subroutine call to value on top of stack */
  339. callstk()
  340.         {
  341.         ol("MOVE.L #*+20,A0");
  342.         ol("MOVE.L (SP),*+10");
  343.         OL("MOVE.L A0,(SP)");
  344.         ol("JMP *");
  345.         Zsp=Zsp+4;
  346.         }
  347.  
  348. /* Jump to specified internal label number */
  349. jump(label)
  350.         int label;
  351.         {
  352.         ot("BRA ");
  353.         printlabel(label);
  354.         nl();
  355.         }
  356.  
  357. /* Test the primary register and jump if false to label */
  358. testjump(label)
  359.         int label;
  360.         {
  361.         ol("TST.L BX");
  362.         ot("BEQ ");
  363.         printlabel(label);
  364.         nl();
  365.         }
  366.  
  367. /* Print pseudo-op to define a literal byte */
  368. defbyte()
  369. {       ot("DC.B ");
  370. }
  371.  
  372. /* Print pseudo-op to define storage for a character */
  373. defstorchr()
  374.         {
  375.         ot("DS.B ");
  376.         }
  377.  
  378. /* Print pseudo-op to define storage for an integer */
  379. defstorint()
  380.         {
  381.         ot("DS.L ");
  382.         }
  383.  
  384. /* Print pseudo-op to define storage for a pointer */
  385. defstorptr()
  386.         {
  387.         ot("DS.L ");
  388.         }
  389.  
  390. /* Modify the stack pointer to the new value indicated */
  391. modstk(newsp)
  392.         int newsp;
  393.         {
  394.         int k;
  395.         k=newsp-Zsp;
  396.         if(k==0)return newsp;
  397.         if(k>0)
  398.                 {
  399.                 ot("ADD");
  400.                 if(k>8)
  401.                         outbyte('A');
  402.                 else outbyte('Q');
  403.                 outasm(" #");
  404.                 outdec(k);
  405.                 outasm(",SP");
  406.                 nl();
  407.                 }
  408.         else
  409.                 {
  410.                 ot("SUB");
  411.                 if(k>-9)
  412.                         outbyte('Q');
  413.                 else outbyte('A');
  414.                 outasm(" #");
  415.                 outdec(-k);
  416.                 outasm(",SP");
  417.                 nl();
  418.                 }
  419.         return newsp;
  420. }
  421.  
  422. /* Quadruple the primary register */
  423. doublereg()
  424.         {
  425.         ol("ASL.L #2,BX");
  426.         }
  427.  
  428. /* Add the primary and secondary registers */
  429. /*      (results in primary) */
  430. zadd()
  431.         {
  432.         ol("ADD.L DX,BX");
  433.         }
  434.  
  435. /* Subtract the primary register from the secondary */
  436. /*      (results in primary) */
  437. zsub()
  438.         {
  439.         ol("SUB.L DX,BX");
  440.         ol("NEG.L BX");
  441.         }
  442.  
  443. /* Multiply the primary and secondary registers */
  444. /*      (results in primary */
  445. mult()
  446.         {
  447.         ol("MULS DX,BX");
  448.         }
  449.  
  450. /* Divide the secondary register by the primary */
  451. /*      (quotient in primary, remainder in secondary) */
  452. div()
  453.         {
  454.         callrts("ccdiv");
  455.         }
  456.  
  457. /* Compute remainder (mod) of secondary register divided */
  458. /*      by the primary */
  459. /*      (remainder in primary, quotient in secondary) */
  460. zmod()
  461.         {
  462.         div();
  463.         swap();
  464.         }
  465.  
  466. /* Inclusive 'or' the primary and the secondary registers */
  467. /*      (results in primary) */
  468. zor()
  469.         {
  470.         ol("OR.L DX,BX");
  471.         }
  472.  
  473. /* Exclusive 'or' the primary and seconday registers */
  474. /*      (results in primary) */
  475. zxor()
  476.         {
  477.         ol("EOR.L DX,BX");
  478.         }
  479.  
  480. /* 'And' the primary and secondary registers */
  481. /*      (results in primary) */
  482. zand()
  483.         {
  484.         ol("AND.L DX,BX");
  485.         }
  486.  
  487. /* Arithmetic shift right the secondary register number of */
  488. /*      times in primary (results in primary) */
  489. asr()
  490.         {
  491.         ol("ASR.L BX,DX");
  492.         ol("MOVE.L DX,BX");
  493.         }
  494.  
  495. /* Arithmetic left shift the secondary register number of */
  496. /*      times in primary (results in primary) */
  497. asl()
  498.         {
  499.         ol("ASL.L BX,DX");
  500.         ol("MOVE.L DX,BX");
  501.         }
  502.  
  503. /* Form two's complement of primary register */
  504. neg()
  505.         {
  506.         ol("NEG.L BX");
  507.         }
  508.  
  509. /* Form one's complement of primary register */
  510. com()
  511.         {
  512.         ol("NOT.L BX");
  513.         }
  514.  
  515. /* Increment the primary register by one */
  516. inc()
  517.         {
  518.         ol("ADDQ #1,BX");
  519.         }
  520.  
  521. /* Increment the primary register by three */
  522. add3()
  523.         {
  524.         ol("ADDQ #3,BX");
  525.         }
  526.  
  527. /* Decrement the primary register by one */
  528. dec()
  529.         {
  530.         ol("SUBQ #1,BX");
  531.         }
  532.  
  533. /* Decrement the primary register by three */
  534. sub3()
  535.         {
  536.         ol("SUBQ #3,BX");
  537.         }
  538.  
  539. /* Following are the conditional operators */
  540. /* They compare the secondary register against the primary */
  541. /* and put a literal 1 in the primary if the condition is */
  542. /* true, otherwise they clear the primary register */
  543.  
  544. scomp()
  545.         {
  546.         ol("CMP.L BX,DX");
  547.         }
  548.  
  549. ucomp()
  550.         {
  551.         ol("MOVE.L DX,A0");
  552.         ol("CMPA.L BX,A0");
  553.         }
  554.  
  555. /*  This didn't work
  556. saftc(ptr)
  557.         char *ptr;
  558.         {
  559.         ot();
  560.         outbyte('S');
  561.         outasm(ptr);
  562.         outasm(" BX");
  563.         nl();
  564.         extend();
  565. */
  566.  
  567. /* Test for equal */
  568. zeq()
  569.         {
  570.         scomp();
  571. /*      saftc("EQ");*/
  572.         ol("SEQ BX");
  573.         extend();
  574.         }
  575.  
  576. /* Test for not equal */
  577. zne()
  578.         {
  579.         scomp();
  580. /*      saftc("NE");*/
  581.         ol("SNE BX");
  582.         extend();
  583.         }
  584.  
  585. /* Test for less than (signed) */
  586. zlt()
  587.         {
  588.         scomp();
  589. /*      saftc("LT");*/
  590.         ol("SLT BX");
  591.         extend();
  592.         }
  593.  
  594. /* Test for less than or equal to (signed) */
  595. zle()
  596.         {
  597.         scomp();
  598. /*      saftc("LE");*/
  599.         ol("SLE BX");
  600.         extend();
  601.         }
  602.  
  603. /* Test for greater than (signed) */
  604. zgt()
  605.         {
  606.         scomp();
  607. /*      saftc("GT");*/
  608.         ol("SGT BX");
  609.         extend();
  610.         }
  611.  
  612. /* Test for greater than or equal to (signed) */
  613. zge()
  614.         {
  615.         scomp();
  616. /*      saftc("GE");*/
  617.         ol("SGE BX");
  618.         extend();
  619.         }
  620.  
  621. /* Test for less than (unsigned) */
  622. ult()
  623.         {
  624.         ucomp();
  625. /*      saftc("LT");*/
  626.         ol("SLT BX");
  627.         extend();
  628.         }
  629.  
  630. /* Test for less than or equal to (unsigned) */
  631. ule()
  632.         {
  633.         ucomp();
  634. /*      saftc("LE");*/
  635.         ol("SLE BX");
  636.         extend();
  637.         }
  638.  
  639. /* Test for greater than (unsigned) */
  640. ugt()
  641.         {
  642.         ucomp();
  643. /*      saftc("GT");*/
  644.         ol("SGT BX");
  645.         extend();
  646.         }
  647.  
  648. /* Test for greater than or equal to (unsigned) */
  649. uge()
  650.         {
  651.         ucomp();
  652. /*      saftc("GE");*/
  653.         ol("SGE BX");
  654.         extend();
  655.         }
  656.  
  657. /*      <<<<<  End of Small-C compiler  >>>>>   */
  658.