From b1c99512da3ac712f4e3688e77ed1eed7e84475b Mon Sep 17 00:00:00 2001 From: Alain Emilia Anna Zscheile Date: Thu, 22 Feb 2024 23:10:54 +0100 Subject: [PATCH] initial commit --- .gitignore | 6 ++++++ dune-project | 19 +++++++++++++++++++ lib/dune | 7 +++++++ lib/layout.ml | 45 +++++++++++++++++++++++++++++++++++++++++++++ lib/layout.mli | 17 +++++++++++++++++ lib/stackAction.ml | 16 ++++++++++++++++ yanaijepeux.opam | 27 +++++++++++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 dune-project create mode 100644 lib/dune create mode 100644 lib/layout.ml create mode 100644 lib/layout.mli create mode 100644 lib/stackAction.ml create mode 100644 yanaijepeux.opam diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc9f764 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/_build +/yanais.opam +*.cmi +*.cmx +*.exe +*.o diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..44a5eda --- /dev/null +++ b/dune-project @@ -0,0 +1,19 @@ +(lang dune 2.7) +(using menhir 2.0) + +(name yanaijepeux) +(version 0.0.1) +(authors "Alain Emilia Anna Zscheile ") +(license "Apache-2.0 OR ISC") + +(package + (name yanaijepeux) + (synopsis "Yanais-associated utilities") + (description "Yanais-associated utilities") + (depends + (gen (>= 0.5)) + (integers (>= 0.2)) + ) +) + +(generate_opam_files) diff --git a/lib/dune b/lib/dune new file mode 100644 index 0000000..7b8021f --- /dev/null +++ b/lib/dune @@ -0,0 +1,7 @@ +(library + (name yanaijepeux) + (public_name yanaijepeux) + (synopsis "Yanais-associated utilities") + (libraries)) + +(documentation) diff --git a/lib/layout.ml b/lib/layout.ml new file mode 100644 index 0000000..3596372 --- /dev/null +++ b/lib/layout.ml @@ -0,0 +1,45 @@ +(** a memory layout *) +type t = { + size : int; + + align_exp : char; + (** alignment = 1 << align_exp *) + } + +open Int + +let show x = "Layout { size = " ^ (string_of_int x.size) ^ "; align_exp = " ^ (Char.code x.align_exp |> string_of_int) ^ "}" + +let alignment_of_exp x = shift_left 1 (Char.code x) + +let alignment x = alignment_of_exp x.align_exp + +let max_size_for_align_exp x = + let y = alignment_of_exp x in + let isz_max_sc = shift_right max_int 1 |> succ in + isz_max_sc - y + +(** appends a layout to this one, returns new layout and the offset *) +let push orig next = + let align_exp = max (Char.code orig.align_exp) (Char.code next.align_exp) |> Char.chr in + let align_exp_cc = Char.code align_exp in + if (align_exp_cc >= 32) || (align_exp_cc < 0) then Option.none else + + (* fill up ourselves so that the other element is correctly aligned *) + let smask_sh = alignment next in + let smask = pred smask_sh in + let smasked = logand orig.size smask in + + (* align size *) + let size = if compare smasked zero == 0 then orig.size else ( + add (sub orig.size smasked) smask_sh + ) in + + let offset = size in + let size = size + next.size in + if size <= max_size_for_align_exp align_exp + then Option.some ({ size; align_exp; }, offset) + else Option.none + +(** finishes a layout by inserting padding at the end to ensure alignment *) +let finish orig = push orig { size = 0; align_exp = orig.align_exp } diff --git a/lib/layout.mli b/lib/layout.mli new file mode 100644 index 0000000..2c119fc --- /dev/null +++ b/lib/layout.mli @@ -0,0 +1,17 @@ +(** a memory layout *) +type t = { + size : int; + + align_exp : char; + (** alignment = 1 << align_exp *) + } + +val show : t -> string + +val alignment : t -> int + +(** appends a layout to this one, returns new layout and the offset *) +val push : t -> t -> (t * int) option + +(** finishes a layout by inserting padding at the end to ensure alignment (might fail when overflowing) *) +val finish : t -> (t * int) option diff --git a/lib/stackAction.ml b/lib/stackAction.ml new file mode 100644 index 0000000..d6e0e85 --- /dev/null +++ b/lib/stackAction.ml @@ -0,0 +1,16 @@ +(** A stack action (pop is run first, then push sequentially) *) +type 'a t = + { pop : int + ; push : 'a list + } + +let merge lhs rhs = + (* merge lhs.push and rhs.pop first *) + let rec compensate push pop = match (push, pop) with + | (_, 0) -> (push, pop) + | ([], _) -> (push, pop) + | (_::xs, _) -> compensate xs (pop - 1) + in let (push, pop) = compensate lhs.push rhs.pop in + { pop = lhs.pop + pop; push = List.append push rhs.push; } + +let empty = { pop = 0; push = []; } diff --git a/yanaijepeux.opam b/yanaijepeux.opam new file mode 100644 index 0000000..c604dab --- /dev/null +++ b/yanaijepeux.opam @@ -0,0 +1,27 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +version: "0.0.1" +synopsis: "Yanais-associated utilities" +description: "Yanais-associated utilities" +authors: ["Alain Emilia Anna Zscheile "] +license: "Apache-2.0 OR ISC" +depends: [ + "dune" {>= "2.7"} + "gen" {>= "0.5"} + "integers" {>= "0.2"} + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +]