Class: YABFI::VM
- Inherits:
-
Object
- Object
- YABFI::VM
- Defined in:
- ext/yabfi/vm.c,
ext/yabfi/vm.c,
ext/yabfi/vm.c
Overview
This class, which is implemented as a C extension, executes the instructions generated by the upstream ruby pipeline.
Defined Under Namespace
Classes: InvalidCommand, MemoryOutOfBounds
Instance Method Summary collapse
-
#execute! ⇒ nil
Execute the instructions loaded into the VM.
-
#initialize(input, output, eof) ⇒ Object
constructor
Initialize a new VM.
-
#load!(ary) ⇒ nil
Load the VM with new instructions.
-
#state ⇒ Object
Return the VM’s internal state – used in testing and debugging.
Constructor Details
#initialize(input, output, eof) ⇒ Object
Initialize a new VM.
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'ext/yabfi/vm.c', line 133 static VALUE vm_initialize(VALUE self, VALUE input, VALUE output, VALUE rb_eof) { vm *ptr; Check_Type(rb_eof, T_FIXNUM); Data_Get_Struct(self, vm, ptr); ptr->input = input; ptr->output = output; ptr->eof = NUM2INT(rb_eof); return self; }; |
Instance Method Details
#execute! ⇒ nil
Execute the instructions loaded into the VM.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'ext/yabfi/vm.c', line 206 static VALUE vm_execute(VALUE self) { vm *ptr; char *buffer; int *tmp_memory; int buffer_size; int delta; int iter; instruction curr; Data_Get_Struct(self, vm, ptr); buffer_size = INITIAL_BUFFER_SIZE; buffer = malloc(buffer_size * sizeof(char)); while (ptr->program_counter < ptr->instructions_length) { curr = ptr->instructions[ptr->program_counter]; switch (curr.code) { case INSTRUCTION_CHANGE_VALUE: ptr->memory[ptr->memory_cursor] += curr.argument; ptr->program_counter++; break; case INSTRUCTION_CHANGE_POINTER: if (((int) ptr->memory_cursor + curr.argument) < 0) { rb_raise(rb_cMemoryOutOfBounds, "The memory cursor went below zero"); } ptr->memory_cursor += curr.argument; while (ptr->memory_cursor >= ptr->memory_length) { delta = ptr->memory_length; if (delta > MAX_REALLOCATION) { delta = MAX_REALLOCATION; } tmp_memory = ptr->memory; ptr->memory = malloc((ptr->memory_length + delta) * sizeof(int)); memcpy(ptr->memory, tmp_memory, ptr->memory_length * sizeof(int)); memset(ptr->memory + ptr->memory_length, 0, delta * sizeof(int)); ptr->memory_length += delta; free(tmp_memory); } ptr->program_counter++; break; case INSTRUCTION_BRANCH_IF_ZERO: if (ptr->memory[ptr->memory_cursor] == 0) { ptr->program_counter += curr.argument; } else { ptr->program_counter++; } break; case INSTRUCTION_BRANCH_NOT_ZERO: if (ptr->memory[ptr->memory_cursor] != 0) { ptr->program_counter += curr.argument; } else { ptr->program_counter++; } break; case INSTRUCTION_GET: for (iter = 0; iter < curr.argument; iter++) { if (rb_funcall(ptr->input, rb_intern("eof?"), 0)) { ptr->memory[ptr->memory_cursor] = ptr->eof; } else { ptr->memory[ptr->memory_cursor] = FIX2INT(rb_funcall(ptr->input, rb_intern("getbyte"), 0)); } } ptr->program_counter++; break; case INSTRUCTION_PUT: if (buffer_size < curr.argument) { free(buffer); buffer_size = curr.argument; buffer = malloc(buffer_size * sizeof(char)); } memset(buffer, ptr->memory[ptr->memory_cursor], curr.argument * sizeof(char)); rb_funcall(ptr->output, rb_intern("write"), 1, rb_str_new(buffer, curr.argument)); ptr->program_counter++; break; default: free(buffer); rb_raise(rb_cInvalidCommand, "Invalid command code: %i", curr.code); } } free(buffer); return Qnil; } |
#load!(ary) ⇒ nil
Load the VM with new instructions.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/yabfi/vm.c', line 159 static VALUE vm_load(VALUE self, VALUE ary) { int iter; vm *ptr; VALUE entry, code, arg; Data_Get_Struct(self, vm, ptr); Check_Type(ary, T_ARRAY); ptr->memory_cursor = 0; ptr->memory_length = INITIAL_MEMORY_SIZE; ptr->memory = calloc(INITIAL_MEMORY_SIZE, sizeof(int)); ptr->program_counter = 0; ptr->instructions_length = RARRAY_LEN(ary); ptr->instructions = malloc(sizeof(instruction) * ptr->instructions_length); for (iter = 0; iter < (int) ptr->instructions_length; iter++) { entry = rb_ary_entry(ary, iter); Check_Type(entry, T_ARRAY); if (RARRAY_LEN(entry) != 2) { rb_raise(rb_cInvalidCommand, "Commands must be tuples"); } code = rb_ary_entry(entry, 0); arg = rb_ary_entry(entry, 1); Check_Type(code, T_FIXNUM); Check_Type(arg, T_FIXNUM); ptr->instructions[iter] = (instruction) { FIX2INT(code), FIX2INT(arg) }; } return Qnil; } |
#state ⇒ Object
Return the VM’s internal state – used in testing and debugging.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'ext/yabfi/vm.c', line 297 static VALUE vm_state(VALUE self) { vm *ptr; VALUE hash; Data_Get_Struct(self, vm, ptr); hash = rb_hash_new(); rb_hash_aset(hash, ID2SYM(rb_intern("memory_cursor")), INT2FIX(ptr->memory_cursor)); rb_hash_aset(hash, ID2SYM(rb_intern("memory_length")), INT2FIX(ptr->memory_length)); rb_hash_aset(hash, ID2SYM(rb_intern("program_counter")), INT2FIX(ptr->program_counter)); if (ptr->memory_cursor < ptr->memory_length) { rb_hash_aset(hash, ID2SYM(rb_intern("current_value")), INT2FIX(ptr->memory[ptr->memory_cursor])); } else { rb_hash_aset(hash, ID2SYM(rb_intern("current_value")), Qnil); } return hash; } |