Verified Commit df071753 authored by Camil Staps's avatar Camil Staps 🚀

serialize_for_interpretation and deserialize seem to work on integers

parent 06262be3
......@@ -2,10 +2,12 @@ definition module ABC.Interpreter
from System.FilePath import :: FilePath
:: SerializedGraph
:: *SerializedGraph
serialize_for_interpretation :: a !FilePath !FilePath !*World -> *(!SerializedGraph, !*World)
deserialize :: !SerializedGraph !FilePath !*World -> *(a, !*World)
:: InterpretedExpression
:: *InterpretationEnvironment
......
implementation module ABC.Interpreter
import StdArray
import StdClass
import StdFile
import StdInt
import StdMisc
import StdOrdList
import Data._Array
import Data.Error
from Data.Func import `on`, on
import Data.Maybe
import System.CommandLine
import System.File
......@@ -20,8 +23,10 @@ import symbols_in_program
import ABC.Interpreter.Internal
import ABC.Interpreter.Util
:: SerializedGraph =
{ graph :: !String
:: *SerializedGraph =
{ graph :: !*String
, descinfo :: !*{#DescInfo}
, modules :: !*{#String}
, bytecode :: !String
}
......@@ -39,20 +44,86 @@ import ABC.Interpreter.Util
serialize_for_interpretation :: a !FilePath !FilePath !*World -> *(!SerializedGraph, !*World)
serialize_for_interpretation graph thisexe bcfile w
# (graph,descs,mods) = copy_to_string_with_names graph
# (host_syms,w) = accFiles (read_symbols thisexe) w
# (bytecode,w) = readFile bcfile w
| isError bytecode = abort "Failed to read the bytecode file\n"
# bytecode = fromOk bytecode
# rec =
{ graph = graph
, descinfo = descs
, modules = mods
, bytecode = bytecode
}
= (rec, w)
import Debug.Trace, StdDebug
deserialize :: !SerializedGraph !FilePath !*World -> *(a, !*World)
deserialize {graph,descinfo,modules,bytecode} thisexe w
# (host_syms,w) = accFiles (read_symbols thisexe) w
# pgm = parse host_syms bytecode
| isNothing pgm = abort "Failed to parse bytecode file\n"
| isNothing pgm = abort "Failed to parse bytecode\n"
# pgm = fromJust pgm
# int_syms = getInterpreterSymbols pgm
# int_syms = {s \\ s <- matchSymbols [s \\ s <-: host_syms] int_syms}
# (graph,descs,mods) = copy_to_string_with_names graph
# graph = replace_symbols_in_graph graph descs mods host_syms
#! graph = replace_symbols_in_graph graph descinfo modules int_syms
# stack = malloc (IF_INT_64_OR_32 8 4 * STACK_SIZE)
# asp = stack
# bsp = stack + IF_INT_64_OR_32 8 4 * (STACK_SIZE-1)
# csp = stack + IF_INT_64_OR_32 4 2 * STACK_SIZE
# heap = malloc (IF_INT_64_OR_32 8 4 * HEAP_SIZE)
# ie_settings = build_interpretation_environment
pgm
heap HEAP_SIZE stack STACK_SIZE
asp bsp csp heap
# graph_node = string_to_interpreter graph ie_settings
#! (ie,_) = make_finalizer ie_settings
#! (ie,graph_node,w) = trace_stdout (ie,graph_node,w)
# ie = {ie_finalizer=ie, ie_snode_ptr=0, ie_snodes=unsafeCreate 1}
= (interpret ie (Finalizer 0 0 graph_node), w)
where
matchSymbols :: ![Symbol] ![Symbol] -> [Symbol]
matchSymbols [] _ = []
matchSymbols [h:_] [] = abort ("Symbol " +++ h.symbol_name +++ " not present in bytecode\n")
matchSymbols orghs=:[h:hs] orgis=:[i:is]
| h.symbol_name > i.symbol_name = matchSymbols orghs is
| h.symbol_name == i.symbol_name = [i:matchSymbols hs is]
| otherwise = abort ("Symbol " +++ h.symbol_name +++ " not present in bytecode\n")
getInterpreterSymbols :: !Pointer -> [Symbol]
getInterpreterSymbols pgm =
sortBy ((<) `on` (\s->s.symbol_name))
[getSymbol i \\ i <- [0..get_host_symbols_n pgm-1]]
where
host_symbols = get_host_symbols pgm
getSymbol :: !Int -> Symbol
getSymbol i
#! offset = host_symbols + i * IF_INT_64_OR_32 24 12 /* size of struct host_symbol */
#! name = derefString (derefInt (offset + IF_INT_64_OR_32 8 4))
#! loc = derefInt (offset + IF_INT_64_OR_32 16 8)
= {symbol_name=name, symbol_value=loc}
get_host_symbols_n :: !Pointer -> Int
get_host_symbols_n pgm = code {
ccall get_host_symbols_n "p:I"
}
= ({graph=graph, bytecode=bytecode}, w)
get_host_symbols :: !Pointer -> Pointer
get_host_symbols pgm = code {
ccall get_host_symbols "p:p"
}
string_to_interpreter :: !String !Pointer -> Pointer
string_to_interpreter graph ie = code {
ccall string_to_interpreter "Sp:p"
}
STACK_SIZE :== (512 << 10) * 2
HEAP_SIZE :== 2 << 20
......@@ -84,25 +155,25 @@ get_start_rule_as_expression filename w
// called from the garbage collector. But that's okay, because we don't add
// it to the finalizer_list anyway. This is just to ensure that the first
// call to interpret gets the right argument.
where
build_interpretation_environment :: !Pointer !Pointer !Int !Pointer !Int !Pointer !Pointer !Pointer !Pointer -> Pointer
build_interpretation_environment pgm heap hsize stack ssize asp bsp csp hp = code {
ccall build_interpretation_environment "ppIpIpppp:p"
}
build_start_node :: !Pointer -> Pointer
build_start_node ie = code {
ccall build_start_node "p:p"
}
build_interpretation_environment :: !Pointer !Pointer !Int !Pointer !Int !Pointer !Pointer !Pointer !Pointer -> Pointer
build_interpretation_environment pgm heap hsize stack ssize asp bsp csp hp = code {
ccall build_interpretation_environment "ppIpIpppp:p"
}
make_finalizer :: !Int -> (!.Finalizer,!Int)
make_finalizer ie_settings = code {
push_finalizers
ccall get_interpretation_environment_finalizer ":p"
push_a_b 0
pop_a 1
build_r e__system_kFinalizer 0 3 0 0
pop_b 3
set_finalizers
pushI 0
}
build_start_node :: !Pointer -> Pointer
build_start_node ie = code {
ccall build_start_node "p:p"
}
make_finalizer :: !Int -> (!.Finalizer,!Int)
make_finalizer ie_settings = code {
push_finalizers
ccall get_interpretation_environment_finalizer ":p"
push_a_b 0
pop_a 1
build_r e__system_kFinalizer 0 3 0 0
pop_b 3
set_finalizers
pushI 0
}
......@@ -2,4 +2,9 @@ module GraphTest
import ABC.Interpreter
Start w = serialize_for_interpretation 37 "./GraphTest" "../test/hamming.bc" w
Start w
# (graph,w) = serialize_for_interpretation graph "./GraphTest" "../test/hamming.bc" w
# (val,w) = deserialize graph "./GraphTest" w
= val
graph = 37
......@@ -13,10 +13,10 @@ DIR:="Clean System Files"
all: optimise bcgen link interpret debug
CodeSharing: Clean\ System\ Files/interpret.a .FORCE
$(CLM) -IL Platform -IL GraphCopy -no-opt-link -ns $@ -o $@
$(CLM) -I . -IL Platform -IL GraphCopy -no-opt-link -ns $@ -o $@
GraphTest: Clean\ System\ Files/interpret.a .FORCE graph_copy_with_names.icl graph_copy_with_names.dcl
$(CLM) -I . -IL Platform -IL GraphCopy -no-opt-link -ns -exl -desc $@ -o $@
$(CLM) -I . -IL Platform -IL GraphCopy -exl -desc -ns -no-opt-link $@ -o $@
PATCHED:=graph_copy_with_names.icl graph_copy_with_names.dcl
$(PATCHED): %: $(CLEAN_HOME)/lib/GraphCopy/% %.patch
......
......@@ -26,6 +26,15 @@ void free_program(struct program *pgm) {
# ifdef LINK_CLEAN_RUNTIME
# include <string.h>
int get_host_symbols_n(struct program *pgm) {
return pgm->host_symbols_n;
}
struct host_symbol *get_host_symbols(struct program *pgm) {
return pgm->host_symbols;
}
struct host_symbol *find_host_symbol_by_name(struct program *pgm, char *name) {
int start = 0;
int end = pgm->host_symbols_n - 1;
......
......@@ -77,7 +77,11 @@ void free_program(struct program *pgm);
#ifdef INTERPRETER
# ifdef LINK_CLEAN_RUNTIME
int get_host_symbols_n(struct program *pgm);
struct host_symbol *get_host_symbols(struct program *pgm);
/* Assumes the symbols are sorted by name */
struct host_symbol *find_host_symbol_by_name(struct program *pgm, char *name);
/* Assumes the symbols are sorted by (host) address */
struct host_symbol *find_host_symbol_by_address(struct program *pgm, void *addr);
void sort_host_symbols_by_location(struct program *pgm);
# endif
......
#include <stdint.h>
#ifndef LINK_CLEAN_RUNTIME
# define LINK_CLEAN_RUNTIME
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "abc_instructions.h"
#include "copy_host_to_interpreter.h"
......@@ -77,6 +77,14 @@ BC_WORD *build_start_node(struct interpretation_environment *ie) {
return hp;
}
BC_WORD *string_to_interpreter(void **clean_string, struct interpretation_environment *ie) {
int len = *(int*)clean_string;
memcpy(ie->hp, &clean_string[1], len);
BC_WORD *node = ie->hp;
ie->hp += len;
return node;
}
void interpretation_environment_finalizer(struct interpretation_environment *ie) {
#if DEBUG_CLEAN_LINKS > 0
fprintf(stderr,"Freeing interpretation_environment %p\n",ie);
......@@ -154,7 +162,7 @@ BC_WORD copy_to_host(struct InterpretationEnvironment *clean_ie, BC_WORD *node)
size_t host_heap_free = ie->host->host_hp_free;
BC_WORD *org_host_heap = host_heap;
if (node[0] == (BC_WORD) &INT+2) {
if (node[0] == (BC_WORD) &INT+2 || node[0] == (BC_WORD) &dINT+2) {
if (host_heap_free < 2)
return -2;
host_heap[0] = (BC_WORD) &dINT+2;
......
......@@ -45,7 +45,7 @@ void init_parser(struct parser *state
char *symbol_strings = state->program->host_symbols_strings;
for (int i = 0; i < host_symbols_n; i++) {
state->program->host_symbols[i].interpreter_location = NULL;
state->program->host_symbols[i].interpreter_location = (void*) -1;
state->program->host_symbols[i].location = *(void**)host_symbols;
host_symbols += IF_INT_64_OR_32(8,4);
state->program->host_symbols[i].name = symbol_strings;
......@@ -54,6 +54,10 @@ void init_parser(struct parser *state
*symbol_strings++ = '\0';
host_symbols++;
}
/* TODO: pre-seed the symbol matching with more descriptors that are not in the bytecode */
find_host_symbol_by_name(state->program, "INT")->interpreter_location = (void*) &INT;
find_host_symbol_by_name(state->program, "dINT")->interpreter_location = (void*) &dINT;
#endif
#ifdef LINKER
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment