home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 221_01 / cc81.c < prev    next >
Text File  |  1979-12-31  |  9KB  |  448 lines

  1. /* >>>>>> start of cc8 <<<<<<< */
  2. /*
  3. ** declare entry point GLOBAL
  4. ** it is for the linker
  5. */
  6. entry(sname, class) char *sname; int class; {
  7.   if(class!=statik) {
  8.     ot("GLOBAL ");outstr(sname);nl();
  9.   }
  10.   outstr(sname);nl();
  11.   }
  12.  
  13. /*
  14. *  SWITCH 
  15. */
  16. sw()
  17.   {ol("JSR CCSWITCH");}
  18.  
  19. /*
  20. **  declare external reference
  21. */
  22. declexternal(n) char *n; {
  23.   ot("EXT ");outstr(n);nl();
  24.   }
  25.  
  26. /* Begin a comment line for the assembler */
  27. comment()
  28. { outbyte(';');
  29. }
  30. /* print NAME of the module */
  31. defname(n) char *n; {
  32.   ot("NAME ");
  33.   outstr(n);
  34.   nl();
  35. }
  36. /* Print all assembler info before any code is generated */
  37. header()
  38. { comment();
  39.  outstr(VERSION);
  40.  nl();
  41. }
  42. /* Print any assembler stuff needed after all code */
  43. trailer()
  44. {
  45.  ot("END"); 
  46. }
  47. /* Fetch a static memory cell into the primary register */
  48. getmem(sym)
  49.  char *sym;
  50. {int nameoffset;
  51.  if(sym[storage] == statik) nameoffset=symsiz;
  52.  else nameoffset=name;
  53.  if((sym[ident]!=pointer)&&(sym[type]==cchar)) {
  54.   ot("LDB  ");
  55.   outstr(sym+nameoffset);
  56.   if(sym[storage] == statik) outbyte('B'); /* dhf 29-oct-86 */
  57.   nl();
  58.   ol("SEX");
  59.  }
  60.  else {
  61.   ot("LDD  ");
  62.   outstr(sym+nameoffset);
  63.   if(sym[storage] == statik) outbyte('B'); /* dhf 29-oct-86 */
  64.   nl();
  65.  }
  66. }
  67.  
  68. /*
  69. **  load secondary register with value which address
  70. **  is on top of the stack
  71. **  Dieter H. Flunkert  13-jul-86
  72. */
  73. loadsec() {
  74.   ol("LDX  [,S]");
  75. }
  76.  
  77. /* Fetch the address of the specified symbol */
  78. /* into the primary register */
  79. /* changed by Dieter H. Flunkert 16-Jan-1986 */
  80. /* Calculates location depending of ident and type */
  81. getloc(sym)
  82.  char *sym;
  83. { int dec;
  84.  
  85.  ot("LEAY ");
  86. /* next three lines changed by Dieter Flunkert */
  87.  dec=((sym[offset] & 255) | (sym[offset+1]<<8)) - stkp;
  88.  if((sym[ident]==variable)&(sym[type]==cchar)) ++dec;
  89.  outdec(dec);
  90. /* end change */
  91.  outstr(",S");
  92.  nl();
  93.  ol("TFR  Y,D");
  94. }
  95. /* Store the primary register into the specified */
  96. /* static memory cell */
  97. /* changed for static variables dhf 29-oct-86 */
  98. putmem(sym)
  99.  char *sym;
  100. {int nameoffset;
  101. if(sym[storage] == statik) nameoffset=symsiz;
  102.  else nameoffset=name;
  103.  if((sym[ident]!=pointer)&(sym[type]==cchar))
  104.    ot("STB  ");
  105.  else
  106.    ot("STD  ");
  107.  outstr(sym+nameoffset);
  108.  if(sym[storage] == statik) outbyte('B');
  109.  nl();
  110. }
  111. /* Store the specified object type in the primary register */
  112. /* at the address on the top of the stack */
  113. putstk(typeobj)
  114.  char typeobj;
  115. {
  116.  if(typeobj==cchar)ol("STB  [,S++]");
  117.   else ol("STD  [,S++]");
  118.  stkp = stkp + 2;
  119. }
  120. /* Fetch the specified object type indirect through the */
  121. /* primary register into the primary register */
  122. indirect(typeobj)
  123.  char typeobj;
  124. {
  125.  ol("PSHS D");
  126.  if(typeobj==cchar){ ol("LDB  [,S++]");ol("SEX"); }
  127.   else ol("LDD  [,S++]");
  128. }
  129. /* Swap the primary and secondary registers */
  130. swap()
  131. { ol("EXG  D,X");
  132. }
  133. /* Print partial instruction to get an immediate value */
  134. /* into the primary register */
  135. immed()
  136. { ot("LDD  #");
  137. }
  138. /* Push the primary register onto the stack */
  139. push()
  140. { ol("PSHS D");
  141.  stkp=stkp-2;
  142. }
  143.  
  144. /*
  145. **  push secondary register on stack
  146. **  Dieter H. Flunkert  13-jul-86
  147. */
  148. pushsec() {
  149.   ol("PSHS X");
  150.   stkp=stkp-2;
  151. }
  152.  
  153. /* Pop the top of the stack into the secondary register */
  154. pop()
  155. { ol("PULS X");
  156.  stkp=stkp+2;
  157. }
  158. /* Swap the primary register and the top of the stack */
  159. swapstk()
  160. { ol("PULS X");
  161.  ol("PSHS D");
  162.  ol("TFR  X,D");
  163. }
  164. /* Call the specified subroutine name */
  165. call(sname)
  166.  char *sname;
  167. { ot("JSR ");
  168.  outstr(sname);
  169.  nl();
  170. }
  171. /* Return from subroutine */
  172. ret()
  173. { ol("RTS");
  174. }
  175. /* Perform subroutine call to value on top of stack */
  176. callstk()
  177. { pop();
  178.  ol("JSR  ,X");
  179.  }
  180. /* Jump to specified internal label number */
  181. jump(label)
  182.  int label;
  183. { ot("JMP ");
  184.  postlabel(label);
  185.  }
  186. /*
  187. **  like testjump but flags the instruction for no optimize
  188. */
  189. testnoopt(label) int label; {
  190.   ol("CMPD #0");
  191.   ot("LBEQ ");
  192.   printlabel(label);
  193.   outstr(" _");
  194.   nl();
  195. }
  196. /* Test the primary register and jump if false to label */
  197. testjump(label)
  198.  int label;
  199. {
  200.   ol("CMPD #0");
  201.   ot("LBEQ ");
  202.   postlabel(label);
  203.  }
  204.  
  205. /*
  206. **  Test the primary register and jump if true to label
  207. **  Dieter H. Flunkert    18-jul-86
  208. */
  209. testtruejump(label) int label; {
  210.    ol("CMPD #0");
  211.    ot("LBNE ");
  212.    postlabel(label);
  213. }
  214.  
  215. /*
  216. ** Debug feature 
  217. */
  218. debug(str) char *str; {
  219.   ol("JSR DEBUG");
  220.   ot("FCC ");
  221.   outbyte('"');
  222.   outstr(str);
  223.   outbyte('"');
  224.   outstr(",0");
  225.   nl();
  226. }
  227.  
  228. /* Print pseudo-op to define storage */
  229. defstorage(siz) int siz; {
  230.   if(siz==1) ot("FCB ");
  231.   else       ot("FDB ");
  232. }
  233.  
  234. /*
  235. **  reserve memory and init with zero
  236. */
  237. dumpzero(siz) int siz; {
  238.   if(siz<=0) return;
  239.   ot("RZB ");
  240.   outdec(siz);
  241.   nl();
  242. }
  243.  
  244. /*
  245. **  point to following object(s)
  246. */
  247. point() {
  248.   ol("FDB *+2");
  249.   }
  250.  
  251. /* Modify the stack pointer to the new value indicated */
  252. modstk(newsp)
  253.  int newsp;
  254.  { int k;
  255.  k=newsp-stkp;
  256.  if(k==0)return (newsp);
  257.  ot("LEAS ");
  258.  outdec(k);
  259.  outstr(",S");
  260.  nl();
  261.  return (newsp);
  262. }
  263. /* Double the primary register */
  264. doublereg()
  265. { ol("PSHS D");
  266.  ol("ADDD ,S++");
  267. }
  268. /* Add the primary and secondary registers (which is on stack) */
  269. /* (results in primary) */
  270. add()
  271. { ol("ADDD ,S++");
  272.  stkp=stkp+2;
  273. }
  274. /* Subtract the primary register from the secondary (which is on stack) */
  275. /* (results in primary) */
  276. sub()
  277. { swapstk();
  278.  ol("SUBD ,S++");
  279.  stkp=stkp+2;
  280. }
  281. /* Multiply the primary and secondary registers */
  282. /* (results in primary */
  283. mult()
  284. { call("ccmult");
  285. }
  286. /* Divide the secondary register by the primary */
  287. /* (quotient in primary, remainder in secondary) */
  288. div()
  289. { call("ccdiv");
  290. }
  291. /* Compute remainder (mod) of secondary register divided */
  292. /* by the primary */
  293. /* (remainder in primary, quotient in secondary) */
  294. mod()
  295. { div();
  296.  swap();
  297.  }
  298. /* Inclusive 'or' the primary and the secondary registers */
  299. /* (results in primary) */
  300. or()
  301. { ol("ORA  ,S+");
  302.  ol("ORB  ,S+");
  303.  stkp=stkp+2;
  304. }
  305. /* Exclusive 'or' the primary and seconday registers */
  306. /* (results in primary) */
  307. xor()
  308. { ol("EORA ,S+");
  309.  ol("EORB ,S+");
  310.  stkp=stkp+2;
  311. }
  312. /* 'And' the primary and secondary registers */
  313. /* (results in primary) */
  314. and()
  315. { ol("ANDA ,S+");
  316.  ol("ANDB ,S+");
  317.  stkp=stkp+2;
  318. }
  319. /* Arithmetic shift right the secondary register number of */
  320. /*  times in primary (results in primary) */
  321. asr()
  322. {ol("DECB");
  323.  ol("BLT  *+8");
  324.  ol("LSR  ,S");
  325.  ol("ROR  1,S");
  326.  ol("BRA  *-7");
  327.  ol("PULS D");
  328.  stkp=stkp+2;
  329. }
  330. /* Arithmetic left shift the secondary register number of */
  331. /* times in primary (results in primary) */
  332. asl()
  333. {ol("DECB");
  334.  ol("BLT  *+8");
  335.  ol("ASL  1,S");
  336.  ol("ROL  ,S");
  337.  ol("BRA  *-7");
  338.  ol("PULS D");
  339.  stkp=stkp+2;
  340. }
  341. /*
  342. ** lognot forms logical not
  343. **  13-jul-86  Dieter H. Flunkert
  344. */
  345. lognot() {
  346.  ol("BEQ  *+7");
  347.  restcom();
  348. }
  349. /* Form two's complement of primary register */
  350. neg()
  351. { com();
  352.  ol("ADDD #1");
  353. }
  354. /* Form one's complement of primary register */
  355. com()
  356. { ol("COMA");
  357.  ol("COMB");
  358. }
  359. /* Increment the primary register by one */
  360. inc()
  361.  {ol("ADDD #1");}
  362. /* Decrement the primary register by one */
  363. dec()
  364.  {ol("SUBD #1");}
  365. /* Following are the conditional operators */
  366. /* They compare the secondary register against the primary */
  367. /* and put a literal 1 in the primary if the condition is */
  368. /* true, otherwise they clear the primary register */
  369.  
  370. /* This is the rest of every compare operation */
  371. restcom()
  372. { ol("LDD  #0");
  373.  ol("BRA  *+5");
  374.  ol("LDD  #1");
  375.  stkp=stkp+2; /* adjust stack */
  376. }
  377.  
  378. /*
  379. **  compare primary - contense of stack
  380. */
  381. cmpd() {
  382.   ol("CMPD ,S++");
  383. }
  384.  
  385. /* Test for equal */
  386. eq()
  387. { cmpd();
  388.  ol("BEQ  *+7");
  389.  restcom();
  390. }
  391. /* Test for not equal */
  392. ne()
  393. { cmpd();
  394.  ol("BNE  *+7");
  395.  restcom();
  396. }
  397. /* Test for less than (signed) */
  398. lt()
  399. { cmpd();
  400.  ol("BGT  *+7");
  401.  restcom();
  402. }
  403. /* Test for less than or equal to (signed) */
  404. le()
  405. { cmpd();
  406.  ol("BGE  *+7");
  407.  restcom();
  408. }
  409. /* Test for greater than (signed) */
  410. gt()
  411. { cmpd();
  412.  ol("BLT  *+7");
  413.  restcom();
  414. }
  415. /* Test for greater than or equal to (signed) */
  416. ge()
  417. { cmpd();
  418.  ol("BLE  *+7");
  419.  restcom();
  420. }
  421. /* Test for less than (unsigned) */
  422. ult() {
  423.   cmpd();
  424.   ol("BHI  *+7");
  425.   restcom();
  426. }
  427.  
  428. /* Test for less than or equal to (unsigned) */
  429. ule() {
  430.   cmpd();
  431.   ol("BHS  *+7");
  432.   restcom();
  433. }
  434.  
  435. /* Test for greater than (unsigned) */
  436. ugt() {
  437.   cmpd();
  438.   ol("BLO  *+7");
  439.   restcom();
  440. }
  441.  
  442. /* Test for greater than or equal to (unsigned) */
  443. uge() {
  444.   cmpd();
  445.   ol("BLS  *+7");
  446.   restcom();
  447. }
  448.