int vm::start() { if(!mem) return -1; //some temporary addresses some instructions might need ADDR_TYPE addr1; for(;;) { //load next instruction char *inst = MEM(pc); //prints the current instruction //just for debugging purposes //std::cout << (int)*inst << std::endl; switch(*inst) { case NOP: break; case ADD: //adds the two integers on top of the stack _ADD(); case DUP: //duplicates the integer on top of the stack _DUP(); case GETBASIC: //replaces a pointer to a data object with the object _GETBASIC(); case HALT: //Stops the vm return 0; case JUMP: //Jumps to an address pc = *(ADDR_TYPE*)(inst + sizeof(INST_TYPE)); continue; case JUMPZ: //Jumps if the long long on top of the stack is 0 _JUMPZ(); case LOADC: //loads a INTEGER constant on the stack _LOADC(); case STORE: //pushes the value on top of the stack to the adress lying at pos 2 of the stack //there is no check, if the destination address is allocated or of the right type _STORE(); case MKBASIC: //Takes the INTEGER from the stack and stores it on the heap _MKBASIC(); default: std::cerr << "Unrecognized Opcode " << (int)*inst << std::endl; } pc += sizeof(INST_TYPE); } return 0; }
int fcntl(int fd, int cmd, ...) { int arg, i, ans, err; Fdinfo *fi, *fans; va_list va; unsigned long oflags; err = 0; ans = 0; va_start(va, cmd); arg = va_arg(va, int); va_end(va); fi = &_fdinfo[fd]; if(fd<0 || fd>=OPEN_MAX || !(fi->flags&FD_ISOPEN)) err = EBADF; else switch(cmd){ case F_DUPFD: if(fi->flags&(FD_BUFFERED|FD_BUFFEREDX)){ err = EGREG; /* dup of buffered fd not implemented */ break; } oflags = fi->oflags; for(i = (arg>0)? arg : 0; i<OPEN_MAX; i++) if(!(_fdinfo[i].flags&FD_ISOPEN)) break; if(i == OPEN_MAX) err = EMFILE; else { ans = _DUP(fd, i); if(ans != i){ if(ans < 0){ _syserrno(); err = errno; }else err = EBADF; }else{ fans = &_fdinfo[ans]; fans->flags = fi->flags&~FD_CLOEXEC; fans->oflags = oflags; fans->uid = fi->uid; fans->gid = fi->gid; } } break; case F_GETFD: ans = fi->flags&FD_CLOEXEC; break; case F_SETFD: fi->flags = (fi->flags&~FD_CLOEXEC)|(arg&FD_CLOEXEC); break; case F_GETFL: ans = fi->oflags&OFL; break; case F_SETFL: fi->oflags = (fi->oflags&~OFL)|(arg&OFL); break; case F_GETLK: case F_SETLK: case F_SETLKW: err = EINVAL; break; } if(err){ errno = err; ans = -1; } return ans; }