Compare commits
8 commits
switch-sta
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f098f3ea8 | ||
|
|
6884cb4de9 | ||
|
|
567db57ef2 | ||
|
|
461f117cda | ||
|
|
80db11debf | ||
|
|
32b33ff4f6 | ||
|
|
e78a343e1d | ||
|
|
021abbc0b4 |
1 changed files with 7 additions and 5 deletions
12
c4.c
12
c4.c
|
|
@ -10,6 +10,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
char *p, *lp, // current position in source code
|
char *p, *lp, // current position in source code
|
||||||
*data; // data/bss pointer
|
*data; // data/bss pointer
|
||||||
|
|
@ -35,7 +36,7 @@ enum {
|
||||||
// opcodes
|
// opcodes
|
||||||
enum { LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,
|
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 ,
|
OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,
|
||||||
OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,EXIT };
|
OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT };
|
||||||
|
|
||||||
// types
|
// types
|
||||||
enum { CHAR, INT, PTR };
|
enum { CHAR, INT, PTR };
|
||||||
|
|
@ -56,7 +57,7 @@ void next()
|
||||||
while (le < e) {
|
while (le < e) {
|
||||||
printf("%8.4s", &"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
|
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 ,"
|
"OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
|
||||||
"OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,EXIT,"[*++le * 5]);
|
"OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT,"[*++le * 5]);
|
||||||
if (*le <= ADJ) printf(" %d\n", *++le); else printf("\n");
|
if (*le <= ADJ) printf(" %d\n", *++le); else printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -352,7 +353,7 @@ int main(int argc, char **argv)
|
||||||
memset(data, 0, poolsz);
|
memset(data, 0, poolsz);
|
||||||
|
|
||||||
p = "char else enum if int return sizeof while "
|
p = "char else enum if int return sizeof while "
|
||||||
"open read close printf malloc memset memcmp exit void main";
|
"open read close printf malloc free memset memcmp exit void main";
|
||||||
i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table
|
i = Char; while (i <= While) { next(); id[Tk] = i++; } // add keywords to symbol table
|
||||||
i = OPEN; while (i <= EXIT) { next(); id[Class] = Sys; id[Type] = INT; id[Val] = i++; } // add library to symbol table
|
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(); id[Tk] = Char; // handle void type
|
||||||
|
|
@ -462,7 +463,7 @@ int main(int argc, char **argv)
|
||||||
if (src) return 0;
|
if (src) return 0;
|
||||||
|
|
||||||
// setup stack
|
// setup stack
|
||||||
sp = (int *)((int)sp + poolsz);
|
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 = PSH; t = sp;
|
||||||
*--sp = argc;
|
*--sp = argc;
|
||||||
|
|
@ -477,7 +478,7 @@ int main(int argc, char **argv)
|
||||||
printf("%d> %.4s", cycle,
|
printf("%d> %.4s", cycle,
|
||||||
&"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
|
&"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 ,"
|
"OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
|
||||||
"OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,EXIT,"[i * 5]);
|
"OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT,"[i * 5]);
|
||||||
if (i <= ADJ) printf(" %d\n", *pc); else printf("\n");
|
if (i <= ADJ) printf(" %d\n", *pc); else printf("\n");
|
||||||
}
|
}
|
||||||
if (i == LEA) a = (int)(bp + *pc++); // load local address
|
if (i == LEA) a = (int)(bp + *pc++); // load local address
|
||||||
|
|
@ -517,6 +518,7 @@ int main(int argc, char **argv)
|
||||||
else if (i == CLOS) a = close(*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 == PRTF) { t = sp + pc[1]; a = printf((char *)t[-1], t[-2], t[-3], t[-4], t[-5], t[-6]); }
|
||||||
else if (i == MALC) a = (int)malloc(*sp);
|
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 == 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 == MCMP) a = memcmp((char *)sp[2], (char *)sp[1], *sp);
|
||||||
else if (i == EXIT) { printf("exit(%d) cycle = %d\n", *sp, cycle); return *sp; }
|
else if (i == EXIT) { printf("exit(%d) cycle = %d\n", *sp, cycle); return *sp; }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue