summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2021-07-31 10:51:02 +0000
committerAndreas Baumann <mail@andreasbaumann.cc>2021-07-31 10:51:02 +0000
commitb1164d69ea2fc694c4cabdbdee6f4155684ce322 (patch)
tree4b4c1bd06d48f0d18aa7652c3429e9c92e34ce5a
parent24194fbe5ea73f7df8b7afaea327e726ec51bfb4 (diff)
downloadcompilertests-b1164d69ea2fc694c4cabdbdee6f4155684ce322.tar.gz
compilertests-b1164d69ea2fc694c4cabdbdee6f4155684ce322.tar.bz2
fixed read->-1 in c4/c4/cc chain
-rw-r--r--miniany/README3
-rw-r--r--miniany/c4.c47
2 files changed, 16 insertions, 34 deletions
diff --git a/miniany/README b/miniany/README
index 77c7726..deb195b 100644
--- a/miniany/README
+++ b/miniany/README
@@ -11,6 +11,9 @@ c4 - C in four functions
minimalistic C compiler running on an emulator, inspiration for this
project
https://github.com/rswier/c4.git
+adaptions to provide switch and structs:
+https://github.com/rswier/c4/blob/switch-and-structs/c4.c: structs
+https://github.com/EarlGray/c4, a X86 JIT version of c4
selfie
C* self-hosting C compiler (also emulator, hypervisor) for RISCV,
diff --git a/miniany/c4.c b/miniany/c4.c
index 4f00e30..d8a588a 100644
--- a/miniany/c4.c
+++ b/miniany/c4.c
@@ -30,14 +30,14 @@ int *e, *le, // current position in emitted code
// tokens and classes (operators last and in precedence order)
enum {
Num = 128, Fun, Sys, Glo, Loc, Id,
- Char, Else, Enum, If, Int, Void, Return, Sizeof, While,
+ Char, Else, Enum, If, Int, Return, Sizeof, While,
Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak
};
// opcodes
enum { LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,
OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,
- OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,PUTI,PUTN,MALC,FREE,MSET,MCMP,EXIT };
+ OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,EXIT };
// types
enum { CHAR, INT, PTR };
@@ -58,7 +58,7 @@ void next()
while (le < e) {
printf("%8.4s", &"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
"OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
- "OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,PUTI,PUTN,MALC,FREE,MSET,MCMP,EXIT,"[*++le * 5]);
+ "OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,EXIT,"[*++le * 5]);
if (*le <= ADJ) printf(" %d\n", *++le); else printf("\n");
}
}
@@ -97,15 +97,6 @@ void next()
++p;
while (*p != 0 && *p != '\n') ++p;
}
- else if (*p != 0 && *p == '*') {
- ++p;
- while (*p != 0 && *p != '/') {
- if (*p == '\n') line++;
- if (*p == '*');
- ++p;
- }
- ++p;
- }
else {
tk = Div;
return;
@@ -125,18 +116,7 @@ void next()
}
else if (tk == '=') { if (*p == '=') { ++p; tk = Eq; } else tk = Assign; return; }
else if (tk == '+') { if (*p == '+') { ++p; tk = Inc; } else tk = Add; return; }
- else if (tk == '-') {
- if (*p == '-') {
- ++p; tk = Dec;
- }
- else if (*p >= '0' && *p <= '9') {
- if (ival = *p - '0') { p++; while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0'; }
- ival = -ival;
- tk = Num;
- return;
- }
- else tk = Sub; return;
- }
+ else if (tk == '-') { if (*p == '-') { ++p; tk = Dec; } else tk = Sub; return; }
else if (tk == '!') { if (*p == '=') { ++p; tk = Ne; } return; }
else if (tk == '<') { if (*p == '=') { ++p; tk = Le; } else if (*p == '<') { ++p; tk = Shl; } else tk = Lt; return; }
else if (tk == '>') { if (*p == '=') { ++p; tk = Ge; } else if (*p == '>') { ++p; tk = Shr; } else tk = Gt; return; }
@@ -373,9 +353,9 @@ int main(int argc, char **argv)
memset(e, 0, poolsz);
memset(data, 0, poolsz);
- p = "char else enum if int void return sizeof while "
+ p = "char else enum if int return sizeof while "
"EOF EXIT_SUCCESS "
- "open read close printf putstring getchar putchar putint putnl malloc free memset memcmp exit void main";
+ "open read close printf malloc free memset memcmp getchar putstring putnl putchar putint exit void main";
i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table
// add library constants
next(); id[Class] = Num; id[Type] = INT; id[Val] = -1;
@@ -388,7 +368,7 @@ int main(int argc, char **argv)
if ((i = read(fd, p, poolsz-1)) <= 0) { printf("read() returned %d\n", i); return -1; }
p[i] = 0;
close(fd);
-
+
// parse declarations
line = 1;
next();
@@ -432,7 +412,6 @@ int main(int argc, char **argv)
ty = INT;
if (tk == Int) next();
else if (tk == Char) { next(); ty = CHAR; }
- else if (tk == Void) { printf("here\n"); }
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk != Id) { printf("%d: bad parameter declaration\n", line); return -1; }
if (id[Class] == Loc) { printf("%d: duplicate parameter definition\n", line); return -1; }
@@ -504,7 +483,7 @@ int main(int argc, char **argv)
printf("%d> %.4s", cycle,
&"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
"OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
- "OPEN,READ,CLOS,PRTF,PUTS,GETC,PUTC,MALC,FREE,MSET,MCMP,EXIT,"[i * 5]);
+ "OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,EXIT,"[i * 5]);
if (i <= ADJ) printf(" %d\n", *pc); else printf("\n");
}
if (i == LEA) a = (int)(bp + *pc++); // load local address
@@ -543,15 +522,15 @@ int main(int argc, char **argv)
else if (i == READ) a = read(sp[2], (char *)sp[1], *sp);
else if (i == CLOS) a = close(*sp);
else if (i == PRTF) { t = sp + pc[1]; a = printf((char *)t[-1], t[-2], t[-3], t[-4], t[-5], t[-6]); }
- else if (i == PUTS) { t = sp + pc[1]; a = printf("%s", (char *)t[-1]); }
- else if (i == GETC) a = getchar();
- else if (i == PUTC) a = putchar(*sp);
- else if (i == PUTI) a = printf("%d", (int)t[-1]);
- else if (i == PUTN) a = puts("");
else if (i == MALC) a = (int)malloc(*sp);
else if (i == FREE) free((void *)*sp);
else if (i == MSET) a = (int)memset((char *)sp[2], sp[1], *sp);
else if (i == MCMP) a = memcmp((char *)sp[2], (char *)sp[1], *sp);
+ else if (i == GETC) a = getchar();
+ else if (i == PUTS) { t = sp + pc[1]; a = printf("%s", (char *)t[-1]); }
+ else if (i == PUTN) a = printf("\n");
+ else if (i == PUTC) a = putchar(*sp);
+ else if (i == PUTI) { t = sp + pc[1]; a = printf("%d", (int)t[-1]); }
else if (i == EXIT) { printf("exit(%d) cycle = %d\n", *sp, cycle); return *sp; }
else { printf("unknown instruction = %d! cycle = %d\n", i, cycle); return -1; }
}