Verified Commit 3cae4956 authored by Camil Staps's avatar Camil Staps 🚀

Add graph{To,From}File, test in Compiler example

parent 8cb3e113
Pipeline #15431 failed with stage
in 1 minute and 46 seconds
......@@ -2,19 +2,12 @@
*.a
*.abc
*.tcl
a.out
*.opt.abc
*.obc
*.bc
a.out
*.aux
*.bbl
*.bcf
*.blg
*.fdb_latexmk
*.fls
*.log
*.out
*.pdf
*.run.xml
_minted-*/
*.graph
examples/compiler/compiler
implementation module Compiler
import StdFile
import StdList
import Data.Either
import Data.Maybe
import ABC.Interpreter
import Compiler.Optimise.Add
compile :: Expr -> [Instr]
......@@ -26,4 +30,24 @@ where
[a:b:rest] -> exec` is [a*b:rest]
_ -> Nothing
Start = compile (Add (Lit 1) (Mul (Lit 2) (Add (Lit 3) (Lit 4))))
Start w
# (opt_add,w) = testThroughFile optimise_addition "opt_add.graph" w
# asm = compile program
# asm = opt_add asm
= (asm,w)
where
program = Add (Lit 1) (Mul (Lit 2) (Add (Lit 3) (Lit 4)))
testThroughFile :: a !FilePath !*World -> *(a, !*World)
testThroughFile graph fp w
# (graph_s,w) = serialize_for_interpretation optimise_addition "./compiler" "./compiler.bc" w
# (ok,f,w) = fopen fp FWriteData w
# (graph_s,f) = graphToFile graph_s f
# (_,w) = fclose f w
# (ok,f,w) = fopen fp FReadData w
# (Right graph_s,f) = graphFromFile f
# (_,w) = fclose f w
= deserialize graph_s "./compiler" w
......@@ -11,5 +11,3 @@ optimise_addition = \is -> case is of
is -> [IPush a:is]
[i:is] -> [i:optimise_addition is]
[] -> []
Start = optimise_addition
CLM:=clm
CLMFLAGS:=\
-I ../../src\
-IL Platform\
-IL GraphCopy\
-nt\
-desc -exl\
-optabc -bytecode\
-no-opt-link -ns\
-h 10m
BIN:=compiler
MAIN:=Compiler
$(BIN): .FORCE
$(MAKE) -C ../../src Clean\ System\ Files/interpret.a
$(CLM) $(CLMFLAGS) $(MAIN) -o $@
clean:
$(RM) -r $(BIN) $(BIN).bc Clean\ System\ Files
.FORCE:
.PHONY: .FORCE clean
definition module ABC.Interpreter
from Data.Either import :: Either
from System.FilePath import :: FilePath
:: *SerializedGraph
......@@ -12,3 +13,6 @@ deserialize :: !SerializedGraph !FilePath !*World -> *(a, !*World)
:: *InterpretationEnvironment
get_start_rule_as_expression :: !FilePath !*World -> *(a, *World)
graphToFile :: !*SerializedGraph !*File -> *(!*SerializedGraph, !*File)
graphFromFile :: !*File -> *(!Either String *SerializedGraph, !*File)
......@@ -8,6 +8,7 @@ import StdMisc
import StdOrdList
import Data._Array
import Data.Either
import Data.Error
from Data.Func import `on`, on
import Data.Maybe
......@@ -175,3 +176,74 @@ make_finalizer ie_settings = code {
set_finalizers
pushI 0
}
graphToFile :: !*SerializedGraph !*File -> *(!*SerializedGraph, !*File)
graphToFile {graph,descinfo,modules,bytecode} f
# (graph_cpy,graph,graph_size) = copy graph
# f = f <<< graph_size
# f = f <<< {#c \\ c <- graph_cpy}
# (descinfo_cpy,descinfo,descinfo_size) = copy descinfo
# f = f <<< descinfo_size
# f = writeList (\di f -> f <<< di.di_prefix_arity_and_mod <<< size di.di_name <<< di.di_name) descinfo_cpy f
# (modules_cpy,modules,modules_size) = copy modules
# f = f <<< modules_size
# f = writeList (\m f -> f <<< size m <<< m) modules_cpy f
# f = f <<< size bytecode
# f = f <<< bytecode
= ({graph=graph,descinfo=descinfo,modules=modules,bytecode=bytecode},f)
where
copy :: !*(b a) -> *(![a], !*b a, !Int) | Array b a
copy arr
# (s,arr) = usize arr
# (cpy,arr) = copy (s-1) arr []
= (cpy,arr,s)
where
copy :: !Int !*(b a) ![a] -> *(![a], !*b a) | Array b a
copy -1 arr cpy = (cpy, arr)
copy i arr cpy
# (x,arr) = arr![i]
= copy (i-1) arr [x:cpy]
writeList :: !(a *File -> *File) ![a] !*File -> *File
writeList write [x:xs] f = writeList write xs (write x f)
writeList _ [] f = f
graphFromFile :: !*File -> *(!Either String *SerializedGraph, !*File)
graphFromFile f
# (graph,f) = readString f
# (_,descinfo_size,f) = freadi f
# (descinfo,f) = readArray readDescInfo 0 descinfo_size (createArrayUnsafe descinfo_size) f
# (_,modules_size,f) = freadi f
# (modules,f) = readArray readString 0 modules_size (createArrayUnsafe modules_size) f
# (bytecode,f) = readString f
# (end,f) = fend f
| not end = (Left "EOF not found after end of graph",f)
# (err,f) = ferror f
| err = (Left "I/O error while reading graph",f)
= (Right {graph=graph,descinfo=descinfo,modules=modules,bytecode=bytecode},f)
where
readArray :: !(*File -> *(a, *File)) !Int !Int !*(arr a) !*File -> *(!*arr a, !*File) | Array arr a
readArray _ i end xs f | i == end = (xs,f)
readArray read i end xs f
# (x,f) = read f
= readArray read (i+1) end {xs & [i]=x} f
readDescInfo :: !*File -> *(!DescInfo, !*File)
readDescInfo f
# (_,prefix_arity_and_mod,f) = freadi f
# (name,f) = readString f
= ({di_prefix_arity_and_mod=prefix_arity_and_mod, di_name=name}, f)
readString :: !*File -> *(!.String, !*File)
readString f
# (_,size,f) = freadi f
= freads f size
......@@ -8,7 +8,6 @@ override CFLAGS+=\
-Werror\
-DPOSIX\
-lm
DIR:="Clean System Files"
all: optimise bcgen link interpret debug
......
......@@ -1394,6 +1394,7 @@ void code_eqAC_a(char *string,int string_length) {
if (list_code)
printf("\t.data\n");
store_data_label_value("__STRING__",2);
store_string(string,string_length,0);
if (list_code)
printf("\t.text\n");
......
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