From 1a6b63ad270d09ece8a520e64235ba8d8017ed22 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 20 Aug 2021 08:48:14 +0000 Subject: c4: fixed compiler warnings and using old style block comments --- miniany/c4.c | 137 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 69 insertions(+), 68 deletions(-) (limited to 'miniany/c4.c') 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 #include @@ -12,26 +12,26 @@ #include #include #include -//#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; -- cgit v1.2.3-54-g00ecf