Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @02:11AM
(#8130570)
Hi there! Sorry for this. I actually have a help message but for some reason I forgot to commit it. It's coming soon!
You run the examples like so: carp -f examples/carp/call.carp
You can compile C files that use the Carp API, but I have not written any documentation yet.
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @02:12AM
(#8130574)
Oh that's weird. the carp folder from examples disappeared. Gimme a sec.
Re:Show HN: A small virtual machine written in C
by proxyswapi on Monday August 04, 2014 @03:55AM
(#8130748)
A year (or two) ago, I wrote a simple virtual machine [1] of my own. Fun time. Interesting exercise. Wish I had courage to post it here and get feedback from the HN. Anyways, yours is much cleaner and concise code. Kudos.
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @03:58AM
(#8130757)
Definitely post it! I've written muuuuch worse and it's still public :D
Check out http://github.com/tekknolagi/gecho — shudder
Re:Show HN: A small virtual machine written in C
by proxyswapi on Monday August 04, 2014 @05:12AM
(#8130905)
Posted my reply on wrong comment (Got confused between akkartik and tekknolagi) :). Anyways, thanks for sharing.
Are you planning to add memory management opcodes (Allocate memory, free memory)? I see DBS, DBG instruction, but it is like key-value store (since it uses carp_ht internally).
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @12:02PM
(#8132388)
Umm perhaps. Could you give a use case? Not sure how/why.
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @02:14AM
(#8130582)
Alright, try now. Also — thanks for the pull request!
Re:Show HN: A small virtual machine written in C
by akkartik on Monday August 04, 2014 @02:21AM
(#8130600)
You're welcome. I pulled the new commits, but I'm still having trouble running the examples:
$ ./carp -f examples/carp/call.carp
Unknown label <add>
(I am very interested in building easy-to-understand codebases: http://akkartik.name/post/wart)
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @02:24AM
(#8130608)
That's strange: http://i.imgur.com/TbF7Izc.png
Try rebuilding carp? o.O
That was some very nice documentation!
Re:Show HN: A small virtual machine written in C
by akkartik on Monday August 04, 2014 @02:28AM
(#8130618)
Nope, rebuilding didn't help.. never mind, I'll look into it later tonight.
Edit: I just noticed you changed the target (why?) and are also immediately deleting carp.out. So I was indeed using the stale version in spite of using 'make clean' :)
Anyways, so I did this, but still no luck.
$ git diff
diff --git a/Makefile b/Makefile
index f921d96..7c55b4e 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,6 @@ all:
$(CC) $(CFLAGS) $(SRCS)
ar cr libcarp.a $(OBJS)
$(CC) src/carp.c libcarp.a -o $(PROG)
- make clean_objs
#.PHONY: tests
$ make
gcc -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
ar cr libcarp.a *.o
gcc src/carp.c libcarp.a -o carp.out
$ ./carp.out -f reg.carp
Unknown label <add>
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @02:39AM
(#8130638)
Yeah I realized that I had changed PROG to carp.out but not changed clean_objs. Already pushed a fix!
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @02:29AM
(#8130620)
So strange.
Re:Show HN: A small virtual machine written in C
by akkartik on Monday August 04, 2014 @04:29AM
(#8130808)
In the process of debugging it I noticed that you're compiling but not linking in c99 mode. When I fix that the problem seems to go away:
$ git diff
diff --git a/Makefile b/Makefile
index 1263dd8..6592c59 100644
--- a/Makefile
+++ b/Makefile
@@ -1,16 +1,16 @@
CC = gcc #/usr/local/bin/gcc-4.2
PREFIX = /usr/local
NDEBUG ?=
-CFLAGS = -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static $(NDEBUG)
+CFLAGS = -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static $(NDEBUG)
SRCS = src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
#$(wildcard src/*.c src/lib/*.c)
OBJS = *.o
PROG = carp.out
all:
- $(CC) $(CFLAGS) $(SRCS)
+ $(CC) -c $(CFLAGS) $(SRCS)
ar cr libcarp.a $(OBJS)
- $(CC) src/carp.c libcarp.a -o $(PROG)
+ $(CC) $(CFLAGS) src/carp.c libcarp.a -o $(PROG)
#.PHONY: tests
Still, I'm concerned that you might be triggering some sort of undefined behavior, which means it might work for you but not on a slightly different machine or compiler version. I tried printing out token lexemes in carp_run_program, right after the call to tokenize(), and some of the tokens printed binary garbage, suggesting that they might be missing a terminating null, or worse. Does this look right? $ git diff
...
diff --git a/src/carp.c b/src/carp.c
index 9268d98..19e16d9 100644
--- a/src/carp.c
+++ b/src/carp.c
@@ -88,6 +88,8 @@ void carp_print_conditions () {
void carp_run_program (char *fn) {
carp_tok *tokens = carp_lex_tokenize(fn);
+ for (carp_tok* tt = tokens; tt; tt=tt->next)
+ printf("%s %d\n", tt->lexeme, tt->type);
if (tokens == NULL) {
fprintf(stderr, "Something went wrong with tokenization.\n");
exit(1);
$ make && ./carp.out -f ./examples/carp/call.carp
gcc -c -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static src/carp_instructions.c src/carp_lexer.c src/carp_machine.c src/carp_tokenizer.c src/lib/carp_stack.c src/lib/carp_ht.c
ar cr libcarp.a *.o
gcc -std=c99 -Wall -Werror -Wno-unused-variable -Wno-format-security -O3 -static src/carp.c libcarp.a -o carp.out
add 3
gload 6
-5l 1
gload 6
-4l 1
add 6
ret 6
main 3
push 6
7�l 1
push 6
9�l 1
call 6
add 4
2 1
ptop 6
halt 6
0 1
16
The trouble with C/C++ is that it's so easy to end up with undefined behavior :/
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @04:36AM
(#8130837)
Actually I can't link with the same CFLAGS; I get an error about missing lcrt0.o or something of the sort.
Also, yeah your code looks fine; I added it in and it worked fine. That's unfortunate. I'll debug soon.
Re:Show HN: A small virtual machine written in C
by akkartik on Monday August 04, 2014 @04:46AM
(#8130854)
What machine/os are you on?
(Perhaps we should take this offline. My email is in my profile.)
Re:Show HN: A small virtual machine written in C
by tekknolagi on Monday August 04, 2014 @12:50PM
(#8132680)
Sent an email :)
There's some examples, but no makefile to build them. How do you run them?
I see you used to have some .carp files but you just deleted them: https://github.com/tekknolagi/carp/commit/fa16eeb443
I tried restoring one, but it doesn't work:
So at this point I'm ready to give up..