Compiler/virtual machine interpreter: Difference between revisions
Compiler/virtual machine interpreter (view source)
Revision as of 15:44, 18 February 2022
, 2 years ago→{{header|ATS}}
m (→{{header|ATS}}) |
|||
Line 1,342:
(* *)
datavtype instruction_vt =
|
|
#define OPCODE_COUNT 24
Line 1,478:
(arr : &(@[String0][OPCODE_COUNT]),
line : string n) :
let
val bad_instruction = "Bad VM instruction."
Line 1,495:
fn
finish_push () :
let
val i1 = skip_whitespace (line, n, start_of_argument)
Line 1,502:
in
(* Little-endian storage. *)
vmint_byte2 arg, vmint_byte3 arg)
end
Line 1,508:
fn
finish_fetch_or_store () :
let
val i1 = skip_whitespace (line, n, start_of_argument)
Line 1,522:
in
(* Little-endian storage. *)
vmint_byte2 arg, vmint_byte3 arg)
end
Line 1,529:
fn
finish_jmp_or_jz () :
let
val i1 = skip_whitespace (line, n, start_of_argument)
Line 1,543:
in
(* Little-endian storage. *)
vmint_byte2 arg, vmint_byte3 arg)
end
Line 1,554:
| OP_JMP => finish_jmp_or_jz ()
| OP_JZ => finish_jmp_or_jz ()
| _ =>
end
Line 1,560:
read_instructions (f : FILEref,
arr : &(@[String0][OPCODE_COUNT])) :
(List_vt (
(* Read the instructions from the input, producing a list of
number of bytes in the instructions. *)
let
fun
loop (arr : &(@[String0][OPCODE_COUNT]),
lst : List_vt (
bytes_needed : Size_t) :
@(List_vt (
if fileref_is_eof f then
@(list_vt_reverse lst, bytes_needed)
Line 1,589:
in
case+ instruction of
|
loop (arr, instruction :: lst, bytes_needed + i2sz 1)
|
loop (arr, instruction :: lst, bytes_needed + i2sz 5)
end
Line 1,601:
fn
list_of_instructions_to_code {bytes_needed : int}
(lst : List_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 (
i : Size_t) : void =
case+ lst of
| ~ NIL => ()
| ~
| ~ instruction_vt_1 (byte1) =>
in▼
code[i] := byte1;▼
val _ = assertloc (i + i2sz 4 < bytes_needed)
▲ code[i] := byte1;
code[i + i2sz
code[i + i2sz
code[i + i2sz 4] := byte5;
loop (code, tail, i + i2sz 5)
end
|