This repository has been archived on 2024-01-18. You can view files and clone it, but cannot push or open issues or pull requests.
compilation/flap/src/elf/elfInterpreter.ml

38 lines
1 KiB
OCaml
Raw Normal View History

2023-10-04 15:40:22 +02:00
(** This module implements the interpreter for X86-64 programs. *)
open ElfAST
type runtime = unit
type observable = {
exit_status : Unix.process_status;
stdout : string;
stderr : string;
}
let initial_runtime () = ()
let show_runtime _ = ()
let evaluate (_ : runtime) (buf : t) =
(* 1. Generate a temporary .s file.
2. Call gcc to generate an executable linked with runtime.o
3. Execute this program, capturing its stdout/stderr
*)
let fn = Filename.chop_extension (Options.get_input_filename ()) ^ ".elf" in
let oc = open_out fn in
Buffer.output_buffer oc buf;
close_out oc;
ExtStd.Unix.add_exec_bits fn;
let exit_status, stdout, stderr =
ExtStd.Unix.output_and_error_of_command ("./" ^ fn)
in
(), { exit_status; stdout; stderr; }
let print_observable (_ : runtime) (obs : observable) =
Printf.sprintf
"Process exited with status %s.\nSTDOUT:\n%s\nSTDERR:\n%s\n\n"
(ExtStd.Unix.string_of_process_status obs.exit_status)
obs.stdout
obs.stderr