Compiler/virtual machine interpreter: Difference between revisions

Line 1,342:
(* *)
 
datavtype instruction_vt =
datatype instruction_t =
| instruction_t_1instruction_vt_1 of (byte)
| instruction_t_5instruction_vt_5 of (byte, byte, byte, byte, byte)
 
#define OPCODE_COUNT 24
Line 1,478:
(arr : &(@[String0][OPCODE_COUNT]),
line : string n) :
instruction_tinstruction_vt =
let
val bad_instruction = "Bad VM instruction."
Line 1,495:
fn
finish_push () :
instruction_tinstruction_vt =
let
val i1 = skip_whitespace (line, n, start_of_argument)
Line 1,502:
in
(* Little-endian storage. *)
instruction_t_5instruction_vt_5 (opcode, vmint_byte0 arg, vmint_byte1 arg,
vmint_byte2 arg, vmint_byte3 arg)
end
Line 1,508:
fn
finish_fetch_or_store () :
instruction_tinstruction_vt =
let
val i1 = skip_whitespace (line, n, start_of_argument)
Line 1,522:
in
(* Little-endian storage. *)
instruction_t_5instruction_vt_5 (opcode, vmint_byte0 arg, vmint_byte1 arg,
vmint_byte2 arg, vmint_byte3 arg)
end
Line 1,529:
fn
finish_jmp_or_jz () :
instruction_tinstruction_vt =
let
val i1 = skip_whitespace (line, n, start_of_argument)
Line 1,543:
in
(* Little-endian storage. *)
instruction_t_5instruction_vt_5 (opcode, vmint_byte0 arg, vmint_byte1 arg,
vmint_byte2 arg, vmint_byte3 arg)
end
Line 1,554:
| OP_JMP => finish_jmp_or_jz ()
| OP_JZ => finish_jmp_or_jz ()
| _ => instruction_t_1instruction_vt_1 (opcode)
end
 
Line 1,560:
read_instructions (f : FILEref,
arr : &(@[String0][OPCODE_COUNT])) :
(List_vt (instruction_tinstruction_vt), Size_t) =
(* Read the instructions from the input, producing a list of
instruction_tinstruction_vt objects, and also calculating the total
number of bytes in the instructions. *)
let
fun
loop (arr : &(@[String0][OPCODE_COUNT]),
lst : List_vt (instruction_tinstruction_vt),
bytes_needed : Size_t) :
@(List_vt (instruction_tinstruction_vt), Size_t) =
if fileref_is_eof f then
@(list_vt_reverse lst, bytes_needed)
Line 1,589:
in
case+ instruction of
| instruction_t_1instruction_vt_1 _ =>
loop (arr, instruction :: lst, bytes_needed + i2sz 1)
| instruction_t_5instruction_vt_5 _ =>
loop (arr, instruction :: lst, bytes_needed + i2sz 5)
end
Line 1,601:
fn
list_of_instructions_to_code {bytes_needed : int}
(lst : List_vt (instruction_tinstruction_vt),
bytes_needed : size_t bytes_needed) :
[bytes_needed : int]
Line 1,610:
loop {n : int | 0 <= n} .<n>.
(code : &vmarray_vt (byte, bytes_needed),
lst : list_vt (instruction_tinstruction_vt, n),
i : Size_t) : void =
case+ lst of
| ~ NIL => ()
| ~ (instruction_t_1 (byte1)head :: tail) =>
letbegin
valcase _head = assertloc (i < bytes_needed)of
| ~ instruction_vt_1 (byte1) =>
in
code[i] := byte1;let
loop (code, tail, i +val _ = assertloc (i i2sz< 1bytes_needed)
end in
code[i] := byte1;
| ~ (instruction_t_5 (byte1, byte2, byte3, byte4, byte5)
::loop (code, tail), =>i + i2sz 1)
let end
val| _~ = assertlocinstruction_vt_5 (ibyte1, +byte2, i2szbyte3, 4byte4, <byte5) bytes_needed)=>
in let
val _ = assertloc (i + i2sz 4 < bytes_needed)
code[i] := byte1;
code[i + i2sz 1] := byte2;in
code[i + i2sz 2 code[i] := byte3byte1;
code[i + i2sz 31] := byte4byte2;
code[i + i2sz 42] := byte5byte3;
loop (code, tail, code[i + i2sz 5)3] := byte4;
code[i + i2sz 4] := byte5;
loop (code, tail, i + i2sz 5)
in end
end
 
1,448

edits