summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2021-08-20 08:48:14 +0000
committerAndreas Baumann <mail@andreasbaumann.cc>2021-08-20 08:48:14 +0000
commit1a6b63ad270d09ece8a520e64235ba8d8017ed22 (patch)
treef6c30564de52f1e9c5268944c242ab655b825c5f
parentb74b9a8ef45137611e53e779d932d9d222e8eb35 (diff)
downloadcompilertests-1a6b63ad270d09ece8a520e64235ba8d8017ed22.tar.gz
compilertests-1a6b63ad270d09ece8a520e64235ba8d8017ed22.tar.bz2
c4: fixed compiler warnings and using old style block comments
-rw-r--r--miniany/README3
-rw-r--r--miniany/REQUIREMENTS4
-rw-r--r--miniany/c4.c137
-rw-r--r--miniany/cc.c6
-rw-r--r--miniany/libc-hosted.c1
-rwxr-xr-xminiany/torture.sh12
6 files changed, 88 insertions, 75 deletions
diff --git a/miniany/README b/miniany/README
index f22c5f7..fef7bac 100644
--- a/miniany/README
+++ b/miniany/README
@@ -23,6 +23,7 @@ complex things into our own compiler.
* currently integrated:
- switch statement from switch-and-structs, adapted c4 to use switch
statements instead of ifs (as in switch-and-structs)
+ - structures from switch-and-structs
* my own changes:
- constants like EOF, EXIT_SUCCESS, NULL
- standard C block comments along to c++ end of line ones
@@ -30,6 +31,8 @@ complex things into our own compiler.
- do/while loops
- more C functions like isspace, getc, strcmp
- some simplified functions for printing like putstring, putint, putnl
+ - strict C89 conformance, mainly use standard comment blocks, also
+ removed some warnings
# Acknoledgments and references
diff --git a/miniany/REQUIREMENTS b/miniany/REQUIREMENTS
index 9cf810d..b8281d1 100644
--- a/miniany/REQUIREMENTS
+++ b/miniany/REQUIREMENTS
@@ -47,4 +47,6 @@ not implementing:
- void * allowing to omit (char *) from and to for instance structs
in dynamic memory management
- typedefs are just syntactic sugar, I use them mostly for 'struct T' -> 'T'
-
+- initializers of global and locals, not that important as we use C89 anyway,
+ forcing us to separate declaration and usage of variables per scope
+
diff --git a/miniany/c4.c b/miniany/c4.c
index f9f3f98..70e6b63 100644
--- a/miniany/c4.c
+++ b/miniany/c4.c
@@ -1,10 +1,10 @@
-// c4.c - C in four functions
+/* c4.c - C in four functions */
-// char, int, structs, and pointer types
-// if, while, do, return, switch and expression statements
-// just enough features to allow self-compilation and a bit more
+/* char, int, structs, and pointer types */
+/* if, while, do, return, switch and expression statements */
+/* just enough features to allow self-compilation and a bit more */
-// Written by Robert Swierczek
+/* Written by Robert Swierczek */
#include <stdio.h>
#include <stdlib.h>
@@ -12,26 +12,26 @@
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
-//#define int long long
+/* #define int long long */
-char *p, *lp, // current position in source code
- *data; // data/bss pointer
+char *p, *lp, /* current position in source code */
+ *data; /* data/bss pointer */
-int *e, *le, // current position in emitted code
- *cas, // case statement patch-up pointer
- *brak, // break statement patch-up pointer
- *def, // default statement patch-up pointer
- *tsize, // array (indexed by type) of type sizes
- tnew, // next available type
- tk, // current token
- ival, // current token value
- ty, // current expression type
- loc, // local variable offset
- line, // current line number
- src, // print source and assembly flag
- debug; // print executed instructions
+int *e, *le, /* current position in emitted code */
+ *cas, /* case statement patch-up pointer */
+ *brak, /* break statement patch-up pointer */
+ *def, /* default statement patch-up pointer */
+ *tsize, /* array (indexed by type) of type sizes */
+ tnew, /* next available type */
+ tk, /* current token */
+ ival, /* current token value */
+ ty, /* current expression type */
+ loc, /* local variable offset */
+ line, /* current line number */
+ src, /* print source and assembly flag */
+ debug; /* print executed instructions */
-// identifier
+/* identifier */
struct ident_s {
int tk;
int hash;
@@ -43,36 +43,36 @@ struct ident_s {
int hclass;
int htype;
int hval;
-} *id, // currently parsed identifier
- *sym; // symbol table (simple list of identifiers)
+} *id, /* currently parsed identifier */
+ *sym; /* symbol table (simple list of identifiers) */
struct member_s {
struct ident_s *id;
int offset;
int type;
struct member_s *next;
-} **members; // array (indexed by type) of struct member lists
+} **members; /* array (indexed by type) of struct member lists */
-// tokens and classes (operators last and in precedence order)
+/* tokens and classes (operators last and in precedence order) */
enum {
Num = 128, Fun, Sys, Glo, Loc, Id,
Break, Case, Char, Default, Else, Enum, If, Int, Return, Sizeof, Do, Struct, Switch, While,
Assign, Cond, Lor, Lan, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge, Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Dot, Arrow, Brak
};
-// opcodes
+/* 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,MALC,FREE,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,ISPC,IDGT,IANU,IALP,SCMP,SDUP,EXIT };
-// types
+/* types */
enum { CHAR, INT, PTR = 256, PTR2 = 512 };
void next()
{
char *pp;
- while (tk = *p) {
+ while ((tk = *p) != 0) {
++p;
switch (tk) {
case '\n':
@@ -119,7 +119,7 @@ void next()
return;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
- if (ival = tk - '0') { while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0'; }
+ if ((ival = tk - '0') != 0) { while (*p >= '0' && *p <= '9') ival = ival * 10 + *p++ - '0'; }
else if (*p == 'x' || *p == 'X') {
while ((tk = *++p) && ((tk >= '0' && tk <= '9') || (tk >= 'a' && tk <= 'f') || (tk >= 'A' && tk <= 'F')))
ival = ival * 16 + (tk & 15) + (tk >= 'A' ? 9 : 0);
@@ -135,12 +135,12 @@ void next()
while (*p != 0 && *p != '\n') ++p;
}
else if (*p != 0 && *p == '*') {
- ++p;
- while (*p != 0 && *p != '/') {
- if (*p == '\n') line++;
- if (*p == '*');
+ do {
+ while (*p != 0 && *p != '*') {
+ ++p;
+ }
++p;
- }
+ } while (*p != '/');
++p;
}
else {
@@ -196,7 +196,7 @@ void expr(int lev)
case '"':
*++e = IMM; *++e = ival; next();
while (tk == '"') next();
- data = (char *)((int)data + sizeof(int) & -sizeof(int)); ty = PTR;
+ data = (char *)(((int)data + sizeof(int)) & -sizeof(int)); ty = PTR;
break;
case Sizeof:
next(); if (tk == '(') next(); else { printf("%d: open paren expected in sizeof\n", line); exit(-1); }
@@ -275,7 +275,7 @@ void expr(int lev)
default: printf("%d: bad expression\n", line); exit(-1);
}
- while (tk >= lev) { // "precedence climbing" or "Top Down Operator Precedence" method
+ while (tk >= lev) { /* "precedence climbing" or "Top Down Operator Precedence" method */
t = ty;
switch (tk) {
case Assign:
@@ -460,10 +460,10 @@ void stmt()
int main(int argc, char **argv)
{
int fd, bt, mbt, ty, poolsz;
- struct ident_s *idmain, *d;
+ struct ident_s *idmain;
struct member_s *m;
- int *pc, *sp, *bp, a, cycle; // vm registers
- int i, *t, neg; // temps
+ int *pc, *sp, *bp, a, cycle; /* vm registers */
+ int i, *t, neg; /* temps */
--argc; ++argv;
if (argc > 0 && **argv == '-' && (*argv)[1] == 's') { src = 1; --argc; ++argv; }
@@ -472,7 +472,7 @@ int main(int argc, char **argv)
if ((fd = open(*argv, 0)) < 0) { printf("could not open(%s)\n", *argv); return -1; }
- poolsz = 256*1024; // arbitrary size
+ poolsz = 256*1024; /* arbitrary size */
if (!(sym = malloc(poolsz))) { printf("could not malloc(%d) symbol area\n", poolsz); return -1; }
if (!(le = e = malloc(poolsz))) { printf("could not malloc(%d) text area\n", poolsz); return -1; }
if (!(data = malloc(poolsz))) { printf("could not malloc(%d) data area\n", poolsz); return -1; }
@@ -489,30 +489,30 @@ int main(int argc, char **argv)
p = "break case char default else enum if int return sizeof do struct switch while "
"EOF EXIT_SUCCESS EXIT_FAILURE NULL "
"open read close printf malloc free memset memcmp getchar putstring putnl putchar putint isspace isdigit isalnum isalpha strcmp strdup exit void main";
- i = Break; while (i <= While) { next(); id->tk = i++; } // add keywords to symbol table
- // add library constants
+ i = Break; 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;
next(); id->class = Num; id->type = INT; id->val = 0;
next(); id->class = Num; id->type = INT; id->val = 1;
next(); id->class = Num; id->type = INT; id->val = (int)NULL;
- i = OPEN; while (i <= EXIT) { next(); id->class = Sys; id->type = INT; id->val = i++; } // add library to symbol table
- next(); id->tk = Char; // handle void type
- next(); idmain = id; // keep track of main
+ i = OPEN; while (i <= EXIT) { next(); id->class = Sys; id->type = INT; id->val = i++; } /* add library to symbol table */
+ next(); id->tk = Char; /* handle void type */
+ next(); idmain = id; /* keep track of main */
if (!(lp = p = malloc(poolsz))) { printf("could not malloc(%d) source area\n", poolsz); return -1; }
if ((i = read(fd, p, poolsz-1)) <= 0) { printf("read() returned %d\n", i); return -1; }
p[i] = 0;
close(fd);
- // add primitive types
+ /* add primitive types */
tsize[tnew++] = sizeof(char);
tsize[tnew++] = sizeof(int);
- // parse declarations
+ /* parse declarations */
line = 1;
next();
while (tk) {
- bt = INT; // basetype
+ bt = INT; /* basetype */
if (tk == Int) next();
else if (tk == Char) { next(); bt = CHAR; }
else if (tk == Enum) {
@@ -592,7 +592,7 @@ int main(int argc, char **argv)
if (id->class) { printf("%d: duplicate global definition\n", line); return -1; }
next();
id->type = ty;
- if (tk == '(') { // function
+ if (tk == '(') { /* function */
id->class = Fun;
id->val = (int)(e + 1);
next(); i = 0;
@@ -642,7 +642,7 @@ int main(int argc, char **argv)
*++e = ENT; *++e = i - loc;
while (tk != '}') stmt();
*++e = LEV;
- id = sym; // unwind symbol table locals
+ id = sym; /* unwind symbol table locals */
while (id->tk) {
if (id->class == Loc) {
id->class = id->hclass;
@@ -665,16 +665,17 @@ int main(int argc, char **argv)
if (!(pc = (int *)idmain->val)) { printf("main() not defined\n"); return -1; }
if (src) return 0;
- // setup stack
+ /* setup stack */
bp = sp = (int *)((int)sp + poolsz);
- *--sp = EXIT; // call exit if main returns
+ *--sp = EXIT; /* call exit if main returns */
*--sp = PSH; t = sp;
*--sp = argc;
*--sp = (int)argv;
*--sp = (int)t;
- // run...
+ /* run... */
cycle = 0;
+ a = 0;
while (1) {
i = *pc++; ++cycle;
if (debug) {
@@ -685,20 +686,20 @@ int main(int argc, char **argv)
if (i <= ADJ) printf(" %d\n", *pc); else printf("\n");
}
switch (i) {
- case LEA: a = (int)(bp + *pc++); break; // load local address
- case IMM: a = *pc++; break; // load global address or immediate
- case JMP: pc = (int *)*pc; break; // jump
- case JSR: *--sp = (int)(pc + 1); pc = (int *)*pc; break; // jump to subroutine
- case BZ: pc = a ? pc + 1 : (int *)*pc; break; // branch if zero
- case BNZ: pc = a ? (int *)*pc : pc + 1; break; // branch if not zero
- case ENT: *--sp = (int)bp; bp = sp; sp = sp - *pc++; break; // enter subroutine
- case ADJ: sp = sp + *pc++; break; // stack adjust
- case LEV: sp = bp; bp = (int *)*sp++; pc = (int *)*sp++; break; // leave subroutine
- case LI: a = *(int *)a; break; // load int
- case LC: a = *(char *)a; break; // load char
- case SI: *(int *)*sp++ = a; break; // store int
- case SC: a = *(char *)*sp++ = a; break; // store char
- case PSH: *--sp = a; break; // push
+ case LEA: a = (int)(bp + *pc++); break; /* load local address */
+ case IMM: a = *pc++; break; /* load global address or immediate */
+ case JMP: pc = (int *)*pc; break; /* jump */
+ case JSR: *--sp = (int)(pc + 1); pc = (int *)*pc; break; /* jump to subroutine */
+ case BZ: pc = a ? pc + 1 : (int *)*pc; break; /* branch if zero */
+ case BNZ: pc = a ? (int *)*pc : pc + 1; break; /* branch if not zero */
+ case ENT: *--sp = (int)bp; bp = sp; sp = sp - *pc++; break; /* enter subroutine */
+ case ADJ: sp = sp + *pc++; break; /* stack adjust */
+ case LEV: sp = bp; bp = (int *)*sp++; pc = (int *)*sp++; break; /* leave subroutine */
+ case LI: a = *(int *)a; break; /* load int */
+ case LC: a = *(char *)a; break; /* load char */
+ case SI: *(int *)*sp++ = a; break; /* store int */
+ case SC: a = *(char *)*sp++ = a; break; /* store char */
+ case PSH: *--sp = a; break; /* push */
case OR: a = *sp++ | a; break;
case XOR: a = *sp++ ^ a; break;
diff --git a/miniany/cc.c b/miniany/cc.c
index d77def0..fc253b1 100644
--- a/miniany/cc.c
+++ b/miniany/cc.c
@@ -290,7 +290,7 @@ struct Symbol *createSymbol( char *s )
struct Symbol *sym;
sym = (struct Symbol *)malloc( sizeof ( struct Symbol ) );
- sym->name = strdup( s );
+ sym->name = (char *)strdup( s );
sym->next = NULL;
return sym;
@@ -340,13 +340,15 @@ int main( int argc, char **argv )
pushback = 0;
DEBUG_SCANNER = 1;
symbol = NULL;
- ident = "12345678901234567890";
+ ident = (char *)malloc( MAX_IDENT_LEN+1 );
token = getToken( );
while( token != S_EOI ) {
parseStatement( );
}
+ free( (char *)ident );
+
exit( EXIT_SUCCESS );
return EXIT_SUCCESS;
diff --git a/miniany/libc-hosted.c b/miniany/libc-hosted.c
index 4154aa1..c6c1e69 100644
--- a/miniany/libc-hosted.c
+++ b/miniany/libc-hosted.c
@@ -3,6 +3,7 @@
* with glibc 2.31.
*/
+#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
diff --git a/miniany/torture.sh b/miniany/torture.sh
index 06c41c4..530cd7c 100755
--- a/miniany/torture.sh
+++ b/miniany/torture.sh
@@ -1,12 +1,16 @@
#!/bin/bash
-
for COMPILER in gcc clang pcc tcc; do
- $COMPILER -m32 -o c4 c4.c
for MODE in freestanding hosted; do
- for LEVEL in 0; do
+ for LEVEL in d 0 1 2 3; do
+# freestanding c4 needs much more work
+# ./build.sh c4 $COMPILER $MODE $LEVEL
+ $COMPILER -m32 -o c4 c4.c
./build.sh cc $COMPILER $MODE $LEVEL
- ./cc < cc.c
+ ./cc < test1.c
+ ./c4 cc.c < test1.c
+ ./c4 c4.c cc.c < test1.c
done
done
done
+