Skip to content

Commit

Permalink
Mass commit in case my laptop bricks again...
Browse files Browse the repository at this point in the history
  • Loading branch information
igstan committed Oct 26, 2024
1 parent 12e9d93 commit 06b2f04
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 33 deletions.
8 changes: 8 additions & 0 deletions lib/core/lib.cm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library (0.1.0)
signature LIST
structure List
is
$/basis.cm

src/list.sig
src/list.sml
8 changes: 8 additions & 0 deletions lib/core/src/list.sig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
signature LIST =
sig
include LIST

val countWhere : ('a -> bool) -> 'a list -> int

val foldl : 'a list -> { seed : 'b, step : 'a * 'b -> 'b } -> 'b
end
31 changes: 31 additions & 0 deletions lib/core/src/list.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
structure List : LIST =
struct
open LIST

fun foldl list { seed, step } = List.foldl step seed list
fun foldr list { seed, step } = List.foldr step seed list

fun takeWhile _ [] = []
| takeWhile p (x :: xs) = if p x then x :: takeWhile p xs else []

fun takeUntil p = takeWhile (not o p)

fun countWhere predicate list =
raise Fail "not implemented"

fun bound xs ys a b =
case (xs, ys) of
(x, []) => a
| ([], y) => b
| (_ :: xs, _ :: ys) => recur xs ys a b

(**
* Returns the list with more elements.
*)
fun max a b = bound a b a b

(**
* Returns the list with fewer elements.
*)
fun min a b = bound a b b a
end
11 changes: 11 additions & 0 deletions lib/core/src/other.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
signature FOLDABLE =
sig
type 'a t
type ('a, 'b) arrow = 'a -> 'b
type 'a monoid = {
zero : 'a,
plus : 'a * 'a -> 'a
}

val foldMap : 'm monoid -> ('a -> 'm) -> 'a t -> 'm
end
2 changes: 1 addition & 1 deletion src/class-name.sml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
structure ClassName =
struct
type t = Text.t
fun fromParts parts = Text.concatWith "/" parts
fun fromParts parts = String.concatWith "/" parts
fun fromString s = s
end
33 changes: 33 additions & 0 deletions src/compilable.sig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(* Reader Monad *)
signature CONFIGURABLE =
sig
type 'a t
type config

val from : (config -> 'a) -> 'a t

val get : key ->

val run : config -> 'a t -> 'a
end

structure Configurable =
struct
type 'computation t = int

fun from f =
raise Fail "not implemented"

fun run config =
end

Configurable.from (fn config =>

)

signature COMPILABLE =
sig
type t

val compile : ConstPool.t -> t -> (Word8Vector.vector, ConstPool.t) Configurable.t
end
4 changes: 4 additions & 0 deletions src/index.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
structure Index =
struct
type t = int
end
18 changes: 18 additions & 0 deletions src/indexed-instr.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
signature INDEXED_INSTR =
sig
type t

val index : t -> Index.t
val instr : t -> Instr.t
end


structure IndexedInstr : INDEXED_INSTR =
struct
type t = (Index.t, Instr.t)

val fromPair = Fn.id

fun index (i, _) => i
fun instr (_, i) => i
end
46 changes: 25 additions & 21 deletions src/main.sml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
structure Main =
struct
open Fn.Syntax infix |>

structure Instr = LabeledInstr

fun symbol class name descriptor = {
Expand Down Expand Up @@ -272,9 +274,9 @@ structure Main =
]
}

val class = Class.from {
fun class name = Class.from {
accessFlags = [Class.Flag.PUBLIC],
thisClass = ClassName.fromString "Main",
thisClass = ClassName.fromString name,
superClass = ClassName.fromString "java/lang/Object",
interfaces = [],
attributes = [Attr.SourceFile "main.sml"],
Expand All @@ -297,9 +299,9 @@ structure Main =
string o dropl isSpace o dropr isSpace o full
end

fun java classPath className =
fun java { classpath } className =
let
val proc = Unix.execute ("/usr/bin/java", ["-cp", classPath, className])
val proc = Unix.execute ("/usr/bin/java", ["-cp", classpath, className])
val output = TextIO.inputAll (Unix.textInstreamOf proc)
in
Unix.reap proc
Expand All @@ -308,27 +310,29 @@ structure Main =

fun main () =
let
val className = "Main"
val workDir = OS.FileSys.getDir ()
val bytes = Class.compile class
val f = BinIO.openOut (OS.Path.joinDirFile { dir = workDir, file = "Main.class" })
val _ = BinIO.output (f, bytes)
val _ = BinIO.closeOut f
val output = java workDir "Main"
val binDir = OS.Path.joinDirFile { dir = workDir, file = "bin" }
val fileName = OS.Path.joinDirFile { dir = binDir, file = className ^ ".class" }
val classFile = BinIO.openOut fileName
val bytes = Class.compile (class className)
val _ = BinIO.output (classFile, bytes)
val _ = BinIO.closeOut classFile
val output = java { classpath = binDir } className
in
print (output ^ "\n")
end

fun stackMap () =
let
val { attributes = [Attr.Code { code, ... }], ... } = nestedLoops
val { offsetedInstrs, ... } = Instr.compileList ConstPool.empty code
in
StackLang.compileCompact
(
StackLang.interpret
(
Verifier.verify offsetedInstrs
)
)
end
case nestedLoops of
| { attributes = [Attr.Code { code, ... }], ... } =>
let
val { offsetedInstrs, ... } = Instr.compileList ConstPool.empty code
in
offsetedInstrs
|> Verifier.verify
|> StackLang.interpret
|> StackLang.compileCompact
end
| _ => raise Fail "not implemented"
end
23 changes: 16 additions & 7 deletions src/stack-map/stack-lang.sml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ structure StackLang =
struct
type local_index = int

type indexed_type = {
index : local_index,
vtype : VerificationType.t
}

datatype t =
| Push of VerificationType.t
| Pop of VerificationType.t
| Load of local_index * VerificationType.t
| Store of local_index * VerificationType.t
| Local of local_index * VerificationType.t
| Load of local_index * VerificationType.t (* indexed_type *)
| Store of local_index * VerificationType.t (* indexed_type *)
| Local of local_index * VerificationType.t (* indexed_type *)
| Branch of { targetOffset : int, fallsThrough : bool }

exception StackUnderflow
Expand All @@ -17,11 +22,15 @@ structure StackLang =
case t of
| Push vtype => "Push " ^ VerificationType.toString vtype
| Pop vtype => "Pop " ^ VerificationType.toString vtype
| Load (index, vtype) => "Load ("^ Int.toString index ^", "^ VerificationType.toString vtype ^")"
| Store (index, vtype) => "Store ("^ Int.toString index ^", "^ VerificationType.toString vtype ^")"
| Local (index, vtype) => "Local ("^ Int.toString index ^", "^ VerificationType.toString vtype ^")"
| Load (index, vtype) =>
"Load ("^ Int.toString index ^", "^ VerificationType.toString vtype ^")"
| Store (index, vtype) =>
"Store ("^ Int.toString index ^", "^ VerificationType.toString vtype ^")"
| Local (index, vtype) =>
"Local ("^ Int.toString index ^", "^ VerificationType.toString vtype ^")"
| Branch { targetOffset, fallsThrough } =>
"Branch { targetOffset = "^ Int.toString targetOffset ^", fallsThrough = "^ Bool.toString fallsThrough ^" }"
"Branch { targetOffset = "^ Int.toString targetOffset
^", fallsThrough = "^ Bool.toString fallsThrough ^" }"

fun interpret instrs =
let
Expand Down
10 changes: 10 additions & 0 deletions src/stack-map/stack-map.sml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ structure StackMap =
stack : VerificationType.t list
}

(* structure Frame =
struct
type t = frame
fun toString frame =
case frame of
| Same { offsetDelta } => "Same { "^ Int.toString offsetDelta ^" }"
|
end *)

open Util

fun compile constPool frame =
Expand Down
4 changes: 3 additions & 1 deletion src/stack-map/verification-type.sml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ structure VerificationType =

(* See: JVMS18 / $4.10.1.2 / Verification Type System *)
datatype t =
Top
| Top
(* | OneWord *) (* ??? *)
(* | TwoWord *) (* ??? *)
| Integer
| Float
| Long
Expand Down
2 changes: 1 addition & 1 deletion src/stack-map/verifier.sig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
signature VERIFIER =
sig
val verify : Instr.t list -> StackLang.t list list
val verify : ('offset * Instr.t) list -> { offset : 'offset, instrs : StackLang.t list } list
end
8 changes: 6 additions & 2 deletions src/stack-map/verifier.sml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
structure Verifier =
struct
structure Verifier : VERIFIER =
let
open Instr StackLang

fun verify instrs =
Expand Down Expand Up @@ -256,4 +256,8 @@ structure Verifier =
{ offset = offset, instrs = transition instr })
instrs
end
in
struct
val verify = verify
end
end
28 changes: 28 additions & 0 deletions src/stack-map/verifier2.sml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(*
* Write it using the final tagless approach? Is it worth it?
*)
(* structure StackLang =
struct
type t = int
end *)



structure Verifier2 :
sig
val verify : Instr.t list -> StackMap.frame list
end
=
struct
fun verify instrs =
let
fun fold (instr, state) =
raise Fail "not implemented"

val seed = []

val r = List.foldl fold seed instrs
in
raise Fail "not implemented"
end
end

0 comments on commit 06b2f04

Please sign in to comment.