Compare commits

...

8 commits

Author SHA1 Message Date
Robert Swierczek
3f098f3ea8 Merge pull request #32 from nubok/patch-2
Avoid undefined behavior - bp is initialized when setting up the stack
2017-08-21 17:39:47 -04:00
Wolfgang Keller
6884cb4de9 Avoid undefined behavior - bp is initialized when setting up the stack 2017-08-21 23:30:09 +02:00
Robert Swierczek
567db57ef2 Merge pull request #27 from chainhelen/master
add a FREE opcode
2017-02-24 06:21:56 -05:00
chainhelen
461f117cda add a FREE opcode 2017-02-24 17:28:35 +08:00
rswier
80db11debf Revert "Added structures"
This reverts commit 32b33ff4f6.
2016-02-26 01:17:04 -05:00
rswier
32b33ff4f6 Added structures
This change adds structures (struct) along with the dot (.) and arrow (->) operators.  It is very silly to add this level of complexity while keeping it just four functions.  But it had to be done :-)
2016-02-25 20:59:44 -05:00
rswier
e78a343e1d Merge pull request #22 from neosilky/fix-compile
Fix a compile error from missing include
2016-01-28 19:52:59 -05:00
Daniel Lockyer
021abbc0b4 Fix a compile error from missing include
open() needs the <fcntl.h> include so this has been added.
2016-01-29 00:28:40 +00:00

12
c4.c
View file

@ -10,6 +10,7 @@
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <fcntl.h>
char *p, *lp, // current position in source code
*data; // data/bss pointer
@ -35,7 +36,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 ,
OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,EXIT };
OPEN,READ,CLOS,PRTF,MALC,FREE,MSET,MCMP,EXIT };
// types
enum { CHAR, INT, PTR };
@ -56,7 +57,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,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");
}
}
@ -352,7 +353,7 @@ int main(int argc, char **argv)
memset(data, 0, poolsz);
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 = 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
@ -462,7 +463,7 @@ int main(int argc, char **argv)
if (src) return 0;
// setup stack
sp = (int *)((int)sp + poolsz);
bp = sp = (int *)((int)sp + poolsz);
*--sp = EXIT; // call exit if main returns
*--sp = PSH; t = sp;
*--sp = argc;
@ -477,7 +478,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,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 == 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 == 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 == 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 == EXIT) { printf("exit(%d) cycle = %d\n", *sp, cycle); return *sp; }