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

c11.c (3428B)


      1 jumpc(tree, lbl, cond)
      2 int tree[]; {
      3 	extern jump, cctab[], rcexpr, isn, label, branch, cbranch;
      4 	int l1, l2;
      5 
      6 	if (tree==0)
      7 		return;
      8 	switch(*tree) {
      9 
     10 	/* & */
     11 	case 47:
     12 		if (cond) {
     13 			cbranch(tree[3], l1=isn++, 0, 0);
     14 			cbranch(tree[4], l1, 0, 0);
     15 			jump(lbl);
     16 			label(l1);
     17 		} else {
     18 			cbranch(tree[3], l1=isn++, 0, 0);
     19 			cbranch(tree[4], l2=isn++, 1, 0);
     20 			label(l1);
     21 			jump(lbl);
     22 			label(l2);
     23 		}
     24 		return;
     25 
     26 	/* | */
     27 	case 48:
     28 		if (cond) {
     29 			cbranch(tree[3], l1=isn++, 1, 0);
     30 			cbranch(tree[4], l2=isn++, 0, 0);
     31 			label(l1);
     32 			jump(lbl);
     33 			label(l2);
     34 		} else {
     35 			cbranch(tree[3], l1=isn++, 1, 0);
     36 			cbranch(tree[4], l1, 1, 0);
     37 			jump(lbl);
     38 			label(l1);
     39 		}
     40 		return;
     41 
     42 	/* ! */
     43 	case 34:
     44 		jumpc(tree[3], lbl, !cond);
     45 		return;
     46 	}
     47 	rcexpr(tree, cctab, 0);
     48 	branch(l1=isn++, *tree, cond);
     49 	jump(lbl);
     50 	label(l1);
     51 	return;
     52 }
     53 
     54 cbranch(tree, lbl, cond, reg)
     55 int tree[]; {
     56 	extern branch, cctab[], rcexpr, isn, label;
     57 	int l1;
     58 
     59 	if (tree==0)
     60 		return;
     61 	switch(*tree) {
     62 
     63 	/* & */
     64 	case 47:
     65 		if (cond) {
     66 			cbranch(tree[3], l1=isn++, 0, reg);
     67 			cbranch(tree[4], lbl, 1, reg);
     68 			label(l1);
     69 		} else {
     70 			cbranch(tree[3], lbl, 0, reg);
     71 			cbranch(tree[4], lbl, 0, reg);
     72 		}
     73 		return;
     74 
     75 	/* | */
     76 	case 48:
     77 		if (cond) {
     78 			cbranch(tree[3], lbl, 1, reg);
     79 			cbranch(tree[4], lbl, 1, reg);
     80 		} else {
     81 			cbranch(tree[3], l1=isn++, 1, reg);
     82 			cbranch(tree[4], lbl, 0, reg);
     83 			label(l1);
     84 		}
     85 		return;
     86 
     87 	/* ! */
     88 	case 34:
     89 		cbranch(tree[3], lbl, !cond, reg);
     90 		return;
     91 	}
     92 	rcexpr(tree, cctab, reg);
     93 	branch(lbl, *tree, !cond);
     94 	return;
     95 }
     96 
     97 
     98 branch(lbl, op, c) {
     99 	extern printf, prins, opdope[];
    100 
    101 	if(op) {
    102 		if((opdope[op]&04)==0)
    103 			op = 61;
    104 		prins(op,c);
    105 	} else
    106 		printf("br");
    107 	printf("\tl%d\n", lbl);
    108 }
    109 
    110 jump(lab) {
    111 	extern printf;
    112 
    113 	printf("jmp\tl%d\n", lab);
    114 }
    115 
    116 label(l) {
    117 	extern printf;
    118 
    119 	printf("l%d:", l);
    120 }
    121 
    122 
    123 popstk(a) {
    124 	extern printf;
    125 
    126 	switch(a) {
    127 
    128 	case 0:
    129 		return;
    130 
    131 	case 2:
    132 		printf("tst	(sp)+\n");
    133 		return;
    134 
    135 	case 4:
    136 		printf("cmp	(sp)+,(sp)+\n");
    137 		return;
    138 	}
    139 	printf("add	$%o,sp\n", a);
    140 }
    141 
    142 length(t) {
    143 
    144 	if (t<0)
    145 		t =+ 020;
    146 	if (t>=020)
    147 		return(2);
    148 	switch(t) {
    149 
    150 	case 0:
    151 		return(2);
    152 
    153 	case 1:
    154 		return(1);
    155 
    156 	case 2:
    157 		return(4);
    158 
    159 	case 3:
    160 		return(8);
    161 
    162 	case 4:
    163 		return(4);
    164 
    165 	}
    166 	return(1024);
    167 }
    168 
    169 rlength(c) {
    170 	extern length;
    171 	auto l;
    172 
    173 	return((l=length(c))==1? 2: l);
    174 }
    175 
    176 printn(n,b) {
    177 	extern putchar;
    178 	auto a;
    179 
    180 	if(a=n/b) /* assignment, not test for equality */
    181 		printn(a, b); /* recursive */
    182 	putchar(n%b + '0');
    183 }
    184 
    185 printf(fmt,x1,x2,x3,x4,x5,x6,x7,x8,x9)
    186 char fmt[]; {
    187 	extern printn, putchar, namsiz, ncpw;
    188 	char s[];
    189 	auto adx[], x, c, i[];
    190 
    191 	adx = &x1; /* argument pointer */
    192 loop:
    193 	while((c = *fmt++) != '%') {
    194 		if(c == '\0')
    195 			return;
    196 		putchar(c);
    197 	}
    198 	x = *adx++;
    199 	switch (c = *fmt++) {
    200 
    201 	case 'd': /* decimal */
    202 	case 'o': /* octal */
    203 		if(x < 0) {
    204 			x = -x;
    205 			if(x<0)  {	/* - infinity */
    206 				if(c=='o')
    207 					printf("100000");
    208 				else
    209 					printf("-32767");
    210 				goto loop;
    211 			}
    212 			putchar('-');
    213 		}
    214 		printn(x, c=='o'?8:10);
    215 		goto loop;
    216 
    217 	case 's': /* string */
    218 		s = x;
    219 		while(c = *s++)
    220 			putchar(c);
    221 		goto loop;
    222 
    223 	case 'p':
    224 		s = x;
    225 		putchar('_');
    226 		c = namsiz;
    227 		while(c--)
    228 			if(*s)
    229 				putchar(*s++);
    230 		goto loop;
    231 	}
    232 	putchar('%');
    233 	fmt--;
    234 	adx--;
    235 	goto loop;
    236 }
    237 
    238 error(s, p1, p2) {
    239 	extern printf, line, fout, flush, putchar, nerror;
    240 	int f;
    241 
    242 	nerror++;
    243 	flush();
    244 	f = fout;
    245 	fout = 1;
    246 	printf("%d: ", line);
    247 	printf(s, p1, p2);
    248 	putchar('\n');
    249 	fout = f;
    250 }
    251