Skip to content

Commit

Permalink
Merge branch 'partial_eval' into scala-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
ailrst committed Jul 24, 2024
2 parents 40d2e07 + a19d5ab commit 7ee3e4b
Show file tree
Hide file tree
Showing 39 changed files with 2,311 additions and 20 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ scripts/output.txt
scripts/cntlm.bir
scripts/total.txt

<<<<<<< HEAD
.cache/
build/
||||||| 630277b
=======

offlineASL/aarch64_*
>>>>>>> partial_eval
offlineASL-scala/lifter/src/generated/*
offlineASL-cpp/subprojects/aslp-lifter/src/generated/*
offlineASL-cpp/subprojects/aslp-lifter/include/aslp/generated/*

16 changes: 9 additions & 7 deletions bin/asli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ let help_msg = [

(** supported backends for :gen and their default output directories *)
let gen_backends = [
("scala", (Cpu.Scala, "offlineASL-scala/lifter/src/generated"));
("ocaml", (Cpu.Ocaml, "offlineASL"));
("cpp", (Cpu.Cpp, "offlineASL-cpp"));
("cpp", (Cpu.Cpp, "offlineASL-cpp/subprojects/aslp-lifter"));
]

let () = Random.self_init ()
Expand Down Expand Up @@ -200,23 +201,24 @@ let rec process_command (tcenv: TC.Env.t) (cpu: Cpu.cpu) (fname: string) (input0
(Dis.dis_decode_entry cpu.env cpu.denv decoder op);
Option.iter close_out chan_opt
| ":gen" :: iset :: id :: rest when List.length rest <= 3 ->
let pc_option = Option.value List.(nth_opt rest 0) ~default:"false" in
let backend = Option.value List.(nth_opt rest 0) ~default:"ocaml" in
Printf.printf "Generating lifter for %s %s with pc option %s using %s backend\n" iset id pc_option backend;
let pc_option = Option.value List.(nth_opt rest 1) ~default:"false" in
let backend_str = Option.value List.(nth_opt rest 0) ~default:"ocaml" in
Printf.printf "Generating lifter for %s %s with pc option %s using %s backend\n" iset id pc_option backend_str;

let pc_option = match String.lowercase_ascii pc_option with
| "false" -> false
| "true" -> true
| _ -> invalid_arg @@ Printf.sprintf "unkown pc option %s (expected: true, false)" pc_option in

let (backend, default_dir) = match List.assoc_opt backend gen_backends with
let (backend, default_dir) = match List.assoc_opt backend_str gen_backends with
| Some x -> x
| None -> invalid_arg @@ Printf.sprintf "unknown backend %s (supported: %s)"
backend (String.concat ", " List.(map fst gen_backends)) in
backend_str (String.concat ", " List.(map fst gen_backends)) in

let dir = Option.value List.(nth_opt rest 2) ~default:default_dir in
let cpu' = Cpu.mkCPU cpu.env cpu.denv in
cpu'.gen iset id pc_option backend dir
cpu'.gen iset id pc_option backend dir;
Printf.printf "Done generating %s lifter into '%s'.\n" backend_str dir;
| ":dump" :: iset :: opcode :: rest ->
let fname =
(match rest with
Expand Down
43 changes: 37 additions & 6 deletions libASL/cpp_backend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ let write_explicit_instantiations cppfuns prefix dir =

write_epilogue () st;
close_out st.oc;
cppfuns
(path, cppfuns)
in
(* group by the .hpp file where each template is defined. *)
let files = Utils.nub @@ List.map (fun x -> x.file) cppfuns in
Expand All @@ -513,10 +513,38 @@ let write_explicit_instantiations cppfuns prefix dir =
write_instantiation file (List.filter (fun x -> x.file = file) cppfuns))
files

(* Installs non-generated support headers, e.g. interface definition *)
let install_headers prefix dir = ()

(* Write all of the above, expecting Utils.ml to already be present in dir *)
(* Finalises the generation by writing build-system files. *)
let write_build_files instdir funs dir =
let meson_template = [%blob "../offlineASL-cpp/subprojects/aslp-lifter/meson.build.in"] in
let interface_file = [%blob "../offlineASL-cpp/subprojects/aslp-lifter/include/aslp/interface.hpp"] in

close_out @@ open_out_bin @@ dir ^ "/dummy.cpp";

let headers_dir = dir ^ "/include/aslp" in
Utils.mkdir_p headers_dir;
let exported = ["aslp_lifter.hpp"; "aslp_lifter_impl.hpp"] in
List.iter (fun h ->
let f = open_out_bin @@ headers_dir ^ "/" ^ h in
Printf.fprintf f {|#include "generated/%s" // IWYU pragma: export%s|} h "\n";
close_out f
) exported;

let f = open_out_bin @@ headers_dir ^ "/interface.hpp" in
output_string f interface_file;
close_out f;

let re = Str.regexp_string {|["SRCFILES"]|} in
let instfiles =
List.map fst funs
|> Utils.nub
|> List.map (fun file -> Printf.sprintf {| '%s/%s',%s|} instdir file "\n") in
let srcfiles = "[\n" ^ String.concat "" instfiles ^ "]\n" in
let f = open_out_bin @@ dir ^ "/meson.build" in
output_string f @@ Str.global_replace re srcfiles meson_template;
close_out f

(* Write all of the above, generating all necessary files for a minimal meson project *)
let run dfn dfnsig tests fns root =

let genprefix = root ^ "/" ^ gen_dir in
Expand All @@ -530,10 +558,13 @@ let run dfn dfnsig tests fns root =
let allfns = dfn :: allfns in

let _header = write_header_file dfn dfnsig (dfn :: semfns) testfns genprefix export_prefix in
let _explicits = write_explicit_instantiations allfns instprefix "." in
let explicits = write_explicit_instantiations allfns instprefix "." in

let _impl = write_impl_file allfns genprefix export_prefix in

let _headers = install_headers genprefix (export_prefix ^ "/..") in
let () = write_build_files instantiate_dir explicits root in

if not (Sys.file_exists (root ^ "/meson.build")) then
Printf.eprintf "Warning: cpp gen directory '%s' is missing build system files. These might need to be copied manually.\n\n" root;

()
9 changes: 8 additions & 1 deletion libASL/dune
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,17 @@
scala_backend
arm_env pretransforms flags
)
(preprocessor_deps (alias ../asl_files))
(preprocessor_deps (alias ../asl_files) (alias cpp_backend_files))
(preprocess (pps ppx_blob))
(libraries libASL_stage0 libASL_support str mlbdd))


(alias
(name cpp_backend_files)
(deps
../offlineASL-cpp/subprojects/aslp-lifter/meson.build.in
../offlineASL-cpp/subprojects/aslp-lifter/include/aslp/interface.hpp))

(library
(name libASL_virtual)
(public_name asli.libASL-virtual)
Expand Down
7 changes: 5 additions & 2 deletions libASL/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ let rec mkdir_p p =
let open Filename in
if Sys.file_exists p then
()
else
else begin
(* make parents, then make final directory. *)
(mkdir_p (dirname p); Sys.mkdir p 0o755)
mkdir_p (dirname p);
if Sys.file_exists p then ()
else Sys.mkdir p 0o755
end


(****************************************************************
Expand Down
13 changes: 13 additions & 0 deletions offlineASL-cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
generated
!.gitkeep
build

.cache
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
54 changes: 54 additions & 0 deletions offlineASL-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.20)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(offlineasl)

set(ASLP_LIFTER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/subprojects/aslp-lifter")
set(ASLP_LIFTER_LLVM_DIR "${CMAKE_CURRENT_SOURCE_DIR}/subprojects/aslp-lifter-instantiate")

## dependencies ##

if (NOT LLVM_FOUND)
find_package(LLVM REQUIRED CONFIG)
else()
message(STATUS "... LLVM already found")
endif()
message(STATUS "Found LLVM: " ${LLVM_DIR} ", tools: " ${LLVM_TOOLS_BINARY_DIR})
get_target_property(LLVM_CONFIG llvm-config LOCATION)

message(STATUS "llvm-config: ${LLVM_CONFIG}")

### aslp-lifter ###
# provides templates to instantiate the lifter with generic targets

add_library(aslp-lifter INTERFACE)
target_include_directories(aslp-lifter INTERFACE "${ASLP_LIFTER_DIR}/include")

target_compile_options(aslp-lifter INTERFACE -Wno-unused-parameter -Wno-error=unused-variable -Wno-error=unused-but-set-variable)
target_compile_options(aslp-lifter INTERFACE -g -O0)

### aslp-lifter-llvm ###
# instantiates the aslp-lifter with a LLVM target

file(GLOB_RECURSE GEN_INSTANTIATE "${ASLP_LIFTER_DIR}/src/*.cpp")
add_library(aslp-lifter-llvm SHARED ${GEN_INSTANTIATE})

target_include_directories(aslp-lifter-llvm PUBLIC "${ASLP_LIFTER_LLVM_DIR}/include")

execute_process(COMMAND "${LLVM_CONFIG}" --libs support core irreader bitwriter
COMMAND_ERROR_IS_FATAL ANY
OUTPUT_VARIABLE llvm_libs OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "llvm_libs: ${llvm_libs}")
separate_arguments(llvm_libs UNIX_COMMAND "${llvm_libs}")
target_link_options(aslp-lifter-llvm PUBLIC "${llvm_libs}")
target_link_directories(aslp-lifter-llvm PUBLIC ${LLVM_LIBRARY_DIR})
target_include_directories(aslp-lifter-llvm PUBLIC ${LLVM_INCLUDE_DIRS})

target_link_libraries(aslp-lifter-llvm PUBLIC aslp-lifter)

# define macros for the explicit instantiation
target_compile_options(aslp-lifter-llvm PRIVATE "-includeaslp/llvm_lifter_traits.hpp")
target_compile_definitions(aslp-lifter-llvm PRIVATE "ASLP_LIFTER_INSTANTIATE=llvm_lifter_traits")

Loading

0 comments on commit 7ee3e4b

Please sign in to comment.