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 (3408B)


      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>=010)
    145 		return(2);
    146 	switch(t) {
    147 
    148 	case 0:
    149 		return(2);
    150 
    151 	case 1:
    152 		return(1);
    153 
    154 	case 2:
    155 		return(4);
    156 
    157 	case 3:
    158 		return(8);
    159 
    160 	case 4:
    161 		return(4);
    162 
    163 	}
    164 	return(1024);
    165 }
    166 
    167 rlength(c) {
    168 	extern length;
    169 	auto l;
    170 
    171 	return((l=length(c))==1? 2: l);
    172 }
    173 
    174 printd(n) {
    175 	int a;
    176 
    177 	if(a=n/10)
    178 		printd(a);
    179 	putchar(n%10 + '0');
    180 }
    181 
    182 printo(n)
    183 {
    184 	int a;
    185 	if (a = (n>>3) & 017777)
    186 		printo(a);
    187 	putchar((n&07) + '0');
    188 }
    189 
    190 printf(fmt,x1,x2,x3,x4,x5,x6,x7,x8,x9)
    191 char fmt[]; {
    192 	extern namsiz, ncpw;
    193 	char s[];
    194 	auto adx[], x, c, i[];
    195 
    196 	adx = &x1; /* argument pointer */
    197 loop:
    198 	while((c = *fmt++) != '%') {
    199 		if(c == '\0')
    200 			return;
    201 		putchar(c);
    202 	}
    203 	x = *adx++;
    204 	switch (c = *fmt++) {
    205 
    206 	case 'o':
    207 		printo(x);
    208 		goto loop;
    209 
    210 	case 'd': /* decimal */
    211 		if(x < 0) {
    212 			x = -x;
    213 			if(x<0)  {	/* - infinity */
    214 				printf("-32768");
    215 				goto loop;
    216 			}
    217 			putchar('-');
    218 		}
    219 		printd(x);
    220 		goto loop;
    221 
    222 	case 'c':
    223 		if (x)
    224 			putchar(x);
    225 		goto loop;
    226 
    227 	case 's': /* string */
    228 		s = x;
    229 		while(c = *s++)
    230 			putchar(c);
    231 		goto loop;
    232 
    233 	case 'p':
    234 		s = x;
    235 		putchar('_');
    236 		c = namsiz;
    237 		while(c--)
    238 			if(*s)
    239 				putchar((*s++)&0177);
    240 		goto loop;
    241 	}
    242 	putchar('%');
    243 	fmt--;
    244 	adx--;
    245 	goto loop;
    246 }
    247 
    248 error(s, p1, p2) {
    249 	extern printf, line, fout, flush, putchar, nerror;
    250 	int f;
    251 
    252 	nerror++;
    253 	flush();
    254 	f = fout;
    255 	fout = 1;
    256 	printf("%d: ", line);
    257 	printf(s, p1, p2);
    258 	putchar('\n');
    259 	fout = f;
    260 }
    261