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


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