News for hackers. Stuff that matters.
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 :)