legacy-cc

The earliest versions of the very first c compiler known to exist in the wild written by the late legend himself dmr. https://github.com/mortdeus/legacy-cc
git clone http://git.hanabi.in/repos/legacy-cc.git
Log | Files | Refs | README

c03.c (2453B)


      1 jumpc(tree, lbl, cond)
      2 int tree[];
      3 {
      4 	extern cctab, block, rcexpr;
      5 
      6 	rcexpr(block(1,easystmt()+103,tree,lbl,cond),cctab);
      7 }
      8 
      9 rcexpr(tree, table)
     10 int tree[], table;
     11 {
     12 	extern space, ospace, putwrd, putchar, line;
     13 	int c, sp[];
     14 
     15 	putchar('#');
     16 	c = space-ospace;
     17 	c =/ 2;		/* # addresses per word */
     18 	sp = ospace;
     19 
     20 	putwrd(c);
     21 	putwrd(tree);
     22 	putwrd(table);
     23 	putwrd(line);
     24 	while(c--)
     25 		putwrd(*sp++);
     26 }
     27 
     28 jump(lab) {
     29 	extern printf;
     30 
     31 	printf("jmp\tl%d\n", lab);
     32 }
     33 
     34 label(l) {
     35 	extern printf;
     36 
     37 	printf("l%d:", l);
     38 }
     39 
     40 retseq() {
     41 	extern printf;
     42 
     43 	printf("jmp\tretrn\n");
     44 }
     45 
     46 slabel() {
     47 	extern csym[], printf;
     48 
     49 	printf(".data; l%d: 1f; .text; 1:\n", csym[2]);
     50 }
     51 
     52 setstk(a) {
     53 	extern printf, stack;
     54 	auto ts;
     55 
     56 	ts = a-stack;
     57 	stack = a;
     58 	switch(ts) {
     59 
     60 	case 0:
     61 		return;
     62 
     63 	case 0177776:	/* -2 */
     64 		printf("tst	-(sp)\n");
     65 		return;
     66 
     67 	case 0177774:	/* -4 */
     68 		printf("cmp	-(sp),-(sp)\n");
     69 		return;
     70 	}
     71 	printf("add	$%o,sp\n", ts);
     72 }
     73 
     74 defvec() {
     75 	extern printf, stack;
     76 
     77 	printf("mov\tsp,r0\nmov\tr0,-(sp)\n");
     78 	stack =- 2;
     79 }
     80 
     81 defstat(s)
     82 int s[]; {
     83 	extern printf, length;
     84 	int len;
     85 
     86 	len = length(s[1]);
     87 	if (s[3])
     88 		printf(".data; l%d:1f; .bss; 1:.=.+%o; .even; .text\n", s[2],
     89 			s[3]*len);
     90 	else
     91 		printf(".bss; l%d:.=.+%o; .even; .text\n", s[2], len);
     92 }
     93 
     94 length(t) {
     95 
     96 	if (t<0)
     97 		t =+ 020;
     98 	if (t>=020)
     99 		return(2);
    100 	switch(t) {
    101 
    102 	case 0:
    103 		return(2);
    104 
    105 	case 1:
    106 		return(1);
    107 
    108 	case 2:
    109 		return(4);
    110 
    111 	case 3:
    112 		return(8);
    113 
    114 	case 4:
    115 		return(4);
    116 
    117 	}
    118 	return(1024);
    119 }
    120 
    121 rlength(c) {
    122 	extern length;
    123 	auto l;
    124 
    125 	return((l=length(c))==1? 2: l);
    126 }
    127 
    128 printn(n,b) {
    129 	extern putchar;
    130 	auto a;
    131 
    132 	if(a=n/b) /* assignment, not test for equality */
    133 		printn(a, b); /* recursive */
    134 	putchar(n%b + '0');
    135 }
    136 
    137 printf(fmt,x1,x2,x3,x4,x5,x6,x7,x8,x9)
    138 char fmt[]; {
    139 	extern printn, putchar, namsiz, ncpw;
    140 	char s[];
    141 	auto adx[], x, c, i[];
    142 
    143 	adx = &x1; /* argument pointer */
    144 loop:
    145 	while((c = *fmt++) != '%') {
    146 		if(c == '\0')
    147 			return;
    148 		putchar(c);
    149 	}
    150 	x = *adx++;
    151 	switch (c = *fmt++) {
    152 
    153 	case 'd': /* decimal */
    154 	case 'o': /* octal */
    155 		if(x < 0) {
    156 			x = -x;
    157 			if(x<0)  {	/* - infinity */
    158 				if(c=='o')
    159 					printf("100000");
    160 				else
    161 					printf("-32767");
    162 				goto loop;
    163 			}
    164 			putchar('-');
    165 		}
    166 		printn(x, c=='o'?8:10);
    167 		goto loop;
    168 
    169 	case 's': /* string */
    170 		s = x;
    171 		while(c = *s++)
    172 			putchar(c);
    173 		goto loop;
    174 
    175 	case 'p':
    176 		s = x;
    177 		putchar('_');
    178 		c = namsiz;
    179 		while(c--)
    180 			if(*s)
    181 				putchar(*s++);
    182 		goto loop;
    183 	}
    184 	putchar('%');
    185 	fmt--;
    186 	adx--;
    187 	goto loop;
    188 }
    189