Class: YABFI::VM

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(input, output, eof) ⇒ Object

Initialize a new VM.

Parameters:

  • input (IO)

    the input from which the VM reads.

  • output (IO)

    the output to which the VM writes.

  • eof (Fixnum)

    the value to return when EOF is reached.



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/yabfi/vm.c', line 143

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.

Returns:

  • (nil)

    unconditionally.

Raises:



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
293
294
295
296
297
298
299
300
# File 'ext/yabfi/vm.c', line 221

static VALUE
vm_execute(VALUE self) {
  vm *ptr;
  int *tmp_memory;
  int delta;
  int iter;
  instruction curr;

  Data_Get_Struct(self, vm, ptr);

  while (ptr->program_counter < ptr->instructions_size) {
    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_size) {
          delta = ptr->memory_size;
          if (delta > MAX_REALLOCATION) {
            delta = MAX_REALLOCATION;
          }
          tmp_memory = ptr->memory;
          ptr->memory = malloc((ptr->memory_size + delta) * sizeof(int));
          memcpy(ptr->memory, tmp_memory, ptr->memory_size * sizeof(int));
          memset(ptr->memory + ptr->memory_size, 0, delta * sizeof(int));
          ptr->memory_size += 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 (ptr->buffer_size < curr.argument) {
          free(ptr->buffer);
          ptr->buffer_size = curr.argument;
          ptr->buffer = malloc(ptr->buffer_size * sizeof(char));
        }
        memset(ptr->buffer, ptr->memory[ptr->memory_cursor],
            curr.argument * sizeof(char));
        rb_funcall(ptr->output, rb_intern("write"), 1,
            rb_str_new(ptr->buffer, curr.argument));
        ptr->program_counter++;
        break;
      default:
        rb_raise(rb_cInvalidCommand, "Invalid command code: %i", curr.code);
    }
  }

  return Qnil;
}

#load!(ary) ⇒ nil

Load the VM with new instructions.

Parameters:

  • ary (Array<Object>)

    list of instructions to execute.

Returns:

  • (nil)

    unconditionally.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/yabfi/vm.c', line 169

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);

  vm_free(ptr);

  ptr->memory_cursor = 0;
  ptr->memory_size = INITIAL_MEMORY_SIZE;
  ptr->memory = calloc(INITIAL_MEMORY_SIZE, sizeof(int));

  ptr->program_counter = 0;
  ptr->instructions_size = RARRAY_LEN(ary);
  ptr->instructions = malloc(sizeof(instruction) * ptr->instructions_size);

  ptr->buffer_size = INITIAL_BUFFER_SIZE;
  ptr->buffer = malloc(INITIAL_BUFFER_SIZE * sizeof(char));

  for (iter = 0; iter < (int) ptr->instructions_size; 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;
}

#stateObject

Return the VM’s internal state – used in testing and debugging.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'ext/yabfi/vm.c', line 305

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_size")),
      INT2FIX(ptr->memory_size));
  rb_hash_aset(hash, ID2SYM(rb_intern("program_counter")),
      INT2FIX(ptr->program_counter));
  if (ptr->memory_cursor < ptr->memory_size) {
    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;
}