* Parameterise J2K

Allow specifying an external Julia module to pass to J2K and an output file to stream to.
Lift test0.jl to the separate directory.
Adjust how we deal with paths to allow calling J2K from other directories.
This commit is contained in:
Colin Gravill 2020-04-22 12:45:13 +01:00 коммит произвёл GitHub
Родитель f1dfb1aab1
Коммит 5b1ae75c3e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 114 добавлений и 8 удалений

Просмотреть файл

@ -6,6 +6,12 @@ git-tree-sha1 = "051c95d6836228d120f5f4b984dd5aba1624f716"
uuid = "621f4979-c628-5d54-868e-fcf4e3e8185c"
version = "0.5.0"
[[ArgParse]]
deps = ["Logging", "TextWrap"]
git-tree-sha1 = "a8fc2e149cd6db276c76faebe197ccd3a92fb9ff"
uuid = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
version = "1.1.0"
[[ArrayLayouts]]
deps = ["FillArrays", "LinearAlgebra"]
git-tree-sha1 = "f8904599065b57f51715faf6278126f853aef6fc"
@ -171,6 +177,11 @@ uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[[TextWrap]]
git-tree-sha1 = "9250ef9b01b66667380cf3275b3f7488d0e25faf"
uuid = "b718987f-49a8-5099-9789-dcd902bef87d"
version = "1.0.1"
[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

Просмотреть файл

@ -3,5 +3,6 @@ uuid = "9cfc71e7-a108-4579-8f35-c364d5fffa70"
version = "0.0.1"
[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

Просмотреть файл

@ -2,7 +2,7 @@
Julia already offers a rich set of tools (particularly Mike Innes's IRTools) for reflection over methods, so this is a short demo of how that allows translation of Julia code to Knossos IR.
To demo, just `julia --project j2k.jl`. This takes these three method definitions
To demo, just `julia --project j2k.jl --input foo1.jl --output test.jl`. This takes these three method definitions
```julia
f(x) = cos(x) * x
@ -120,3 +120,32 @@ Which converts to this IRTools IR:
(b6 %2 %3)
(b2 %2 %5 %3))))))
```
This can also be connected to the overall tools, from the project root
```
julia --project=./src/j2k/ ./src/j2k/j2k.jl --input ./test/j2k/test0.jl --output obj/test/j2k/j2k_test0.ks
```
then (in PowerShell style)
```powershell
./ksc --compile-and-run `
--ks-source-file src/runtime/prelude.ks `
--ks-source-file obj/test/j2k/j2k_test0.ks `
--ks-output-file obj/test/j2k/j2k_test0.kso `
--cpp-output-file obj/test/j2k/j2k_test0.cpp `
--c++ g++ `
--exe-output-file obj/test/j2k/j2k_test0.exe
```
which currently fails, but we can work on next
```
read decls
ksc.exe: Failed parse: (line 1, column 1):
unexpected '-'
expecting end of input or "("
CallStack (from HasCallStack):
error, called at src/ksc\Parse.hs:124:36 in main:Parse
```

17
src/j2k/foo1.jl Normal file
Просмотреть файл

@ -0,0 +1,17 @@
f(x) = cos(x) * x
sumsq(xs) = sum(xs.^2)
function foo1(as :: Vector{Float64}, b :: Float64)
p = length(as)
p = p - 1
if p > 0
if as[1] > 0
y = [sin(a) for a in as] .* f(b)
else
y = [1.1*p, foo1(as[2:end], 1.1)]
end
else
y = -as.*f(b)
end
f(sumsq(y)) + 5.5555
end

Просмотреть файл

@ -1,6 +1,7 @@
# Julia to Knossos
using Zygote
using IRTools
using ArgParse
import Base.show
import IRTools: isconditional
@ -9,6 +10,30 @@ import IRTools: isconditional
# (still some Anys in basic block defs)
# 2. Nice printing :)
function parse_commandline()
s = ArgParseSettings()
@add_arg_table! s begin
"--input"
help = "input .jl file, last expression used, absolute path"
required = true
"--output", "-o"
help = "output Knossos .ks extension recommended"
required = true
end
return parse_args(s)
end
parsed_args = parse_commandline()
inputfilename = parsed_args["input"]
outputfilename = parsed_args["output"]
mkpath(dirname(outputfilename))
io = open(outputfilename, "w")
# Newline constant for s-expr printing
nl = "\n"
tab = "\t"
@ -167,23 +192,26 @@ function make_sexps(ir :: IRTools.IR)
# Emit blocks n:2 in reverse
for b in reverse(blocks[2:end])
se = make_def(false, blockid(b.id), b)
println(se,"\n")
println(io, se,"\n")
end
# Emit block 1
f = IRTools.argtypes(blocks[1])[1].val
se = make_def(true, string(f), blocks[1])
println(se)
println(io, se)
end
###################
### Test make_sexp
print("---\n")
print(io, "---\n")
#=
f(x) = cos(x) * x
sumsq(xs) = sum(xs.^2)
function foo1(as :: Vector{Float64}, b :: Float64)
p = length(as)
p = p - 1
@ -198,18 +226,21 @@ function foo1(as :: Vector{Float64}, b :: Float64)
end
f(sumsq(y)) + 5.5555
end
=#
function jl2ks(f, argtypes)
meta = IRTools.typed_meta(Tuple{typeof(f),argtypes...})
ir = IRTools.IR(meta)
IRTools.expand!(ir)
println("-------- IR ----------")
println(ir)
println("--------")
println(io, "-------- IR ----------")
println(io, ir)
println(io, "--------")
make_sexps(ir)
end
jl2ks(foo1, (Vector{Float64},Float64))
sample = evalfile(joinpath(pwd(), inputfilename))
jl2ks(sample, (Vector{Float64},Float64))

17
test/j2k/test0.jl Normal file
Просмотреть файл

@ -0,0 +1,17 @@
f(x) = cos(x) * x
sumsq(xs) = sum(xs.^2)
function foo1(as :: Vector{Float64}, b :: Float64)
p = length(as)
p = p - 1
if p > 0
if as[1] > 0
y = [sin(a) for a in as] .* f(b)
else
y = [1.1*p, foo1(as[2:end], 1.1)]
end
else
y = -as.*f(b)
end
f(sumsq(y)) + 5.5555
end