From 7f32ad51b0f08446d7f66e5403d69299d0d60021 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 1 Oct 2021 21:10:36 +0200 Subject: c4: added strl* functions cc: started peudo function parsing, currently for void main() only some philosophical thinking and testing around forward function declarations --- miniany/c4.c | 77 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 35 deletions(-) (limited to 'miniany/c4.c') diff --git a/miniany/c4.c b/miniany/c4.c index ca6d3d3..7c2b53b 100644 --- a/miniany/c4.c +++ b/miniany/c4.c @@ -68,7 +68,7 @@ enum { /* 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 , - MALC,FREE,MCPY,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,ISPC,IDGT,IANU,IALP,SCMP,SDUP,EXIT }; + MALC,FREE,MCPY,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,ISPC,IDGT,IANU,IALP,SCMP,SDUP,SLCP,SLCA,EXIT }; /* types */ enum { CHAR, INT, PTR = 256, PTR2 = 512 }; @@ -113,7 +113,7 @@ void next() le++; memcpy(buf, &"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 ," - "MALC,FREE,MCPY,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,ISPC,IDGT,IANU.IALP,SCMP,SDUP,EXIT,"[*le * 5], 4); + "MALC,FREE,MCPY,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,ISPC,IDGT,IANU.IALP,SCMP,SDUP,SLCP,SLCA,EXIT,"[*le * 5], 4); buf[4] = 0; putstring(" "); putstring(buf); @@ -528,7 +528,7 @@ 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 " - "malloc free memcpy memset memcmp getchar putstring putnl putchar putint isspace isdigit isalnum isalpha strcmp strdup exit void main"; + "malloc free memcpy memset memcmp getchar putstring putnl putchar putint isspace isdigit isalnum isalpha strcmp strdup strlcpy strlcat exit void main"; 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; @@ -652,7 +652,7 @@ int main(int argc, char **argv) } while (tk == Mul) { next(); ty = ty + PTR; } if (tk != Id) { error("bad parameter declaration"); } - if (id->class == Loc) { error("duplicate parameter definition"); } + if (id->class == Loc) { /* error("duplicate parameter definition"); */ } id->hclass = id->class; id->class = Loc; id->htype = id->type; id->type = ty; id->hval = id->val; id->val = i++; @@ -660,40 +660,45 @@ int main(int argc, char **argv) if (tk == ',') next(); } next(); - if (tk != '{') { error("bad function definition"); } - loc = ++i; - next(); - while (tk == Int || tk == Char || tk == Struct) { - if (tk == Int) bt = INT; else if (tk == Char) bt = CHAR; else { - next(); - if (tk != Id) { error("bad struct declaration"); } - bt = id->stype; - } + + if (tk == '{') { + /* function body declaration */ + loc = ++i; next(); - while (tk != ';') { - ty = bt; - while (tk == Mul) { next(); ty = ty + PTR; } - if (tk != Id) { error("bad local declaration"); } - if (id->class == Loc) { error("duplicate local definition"); } - id->hclass = id->class; id->class = Loc; - id->htype = id->type; id->type = ty; - id->hval = id->val; id->val = ++i; + while (tk == Int || tk == Char || tk == Struct) { + if (tk == Int) bt = INT; else if (tk == Char) bt = CHAR; else { + next(); + if (tk != Id) { error("bad struct declaration"); } + bt = id->stype; + } + next(); + while (tk != ';') { + ty = bt; + while (tk == Mul) { next(); ty = ty + PTR; } + if (tk != Id) { error("bad local declaration"); } + if (id->class == Loc) { error("duplicate local definition"); } + id->hclass = id->class; id->class = Loc; + id->htype = id->type; id->type = ty; + id->hval = id->val; id->val = ++i; + next(); + if (tk == ',') next(); + } next(); - if (tk == ',') next(); } - next(); - } - *++e = ENT; *++e = i - loc; - while (tk != '}') stmt(); - *++e = LEV; - id = sym; /* unwind symbol table locals */ - while (id->tk) { - if (id->class == Loc) { - id->class = id->hclass; - id->type = id->htype; - id->val = id->hval; + *++e = ENT; *++e = i - loc; + while (tk != '}') stmt(); + *++e = LEV; + id = sym; /* unwind symbol table locals */ + while (id->tk) { + if (id->class == Loc) { + id->class = id->hclass; + id->type = id->htype; + id->val = id->hval; + } + id = id + 1; } - id = id + 1; + } else { + /* forward function declaration */ } } else { @@ -730,7 +735,7 @@ int main(int argc, char **argv) buf = (char *)malloc( 5 ); memcpy(buf, &"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 ," - "MALC,FREE,MCPY,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,ISPC,IDGT,IANU.IALP,SCMP,SDUP,EXIT,"[i * 5],4); + "MALC,FREE,MCPY,MSET,MCMP,GETC,PUTS,PUTN,PUTC,PUTI,ISPC,IDGT,IANU.IALP,SCMP,SDUP,SLCP,SLCA,EXIT,"[i * 5],4); buf[4] = 0; putstring(buf); free(buf); @@ -789,6 +794,8 @@ int main(int argc, char **argv) case IALP: t = sp + pc[1]; a = isalpha( (int)t[-1]); break; case SCMP: t = sp + pc[1]; a = strcmp((char *)t[-1], (char *)t[-2]); break; case SDUP: t = sp + pc[1]; a = (int)strdup((char *)t[-1]); break; + case SLCP: a = (int)strlcpy((char *)sp[2], (char *)sp[1], *sp); break; + case SLCA: a = (int)strlcat((char *)sp[2], (char *)sp[1], *sp); break; case EXIT: /* putstring("exit("); putint(*sp); putstring(") cycle = "); putint(cycle); putnl(); */ rt = *sp; run = 0; break; default: putstring("unknown instruction = "); putint(i); putstring("! cycle = "); putint(cycle); putnl(); rt = -1; run = 0; break; } -- cgit v1.2.3-54-g00ecf