Mercurial > repos > rhope
diff nworker.rhope @ 66:d4b44ae2e34a
New variant of C backend works now
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 06 Jun 2010 20:29:10 -0400 |
parents | 689fb73e7612 |
children | d0ce696786cc |
line wrap: on
line diff
--- a/nworker.rhope Tue Jun 01 01:13:54 2010 -0400 +++ b/nworker.rhope Sun Jun 06 20:29:10 2010 -0400 @@ -1,6 +1,7 @@ Import extendlib.rhope Import cbackend.rhope - +Import number.rhope +Import boolean.rhope Blueprint Condition Set { @@ -295,7 +296,7 @@ NodeResults Free Temps Name - Builtin? + Builtin? Library } @@ -346,8 +347,8 @@ new type <- Val[type] new count <- 1 } - new node <- [node]Input Types <<[ - [ [node]Input Types >> ]Set[ [dest]IO Num >>, [[()]Append[new type]]Append[new count] ] + new node <- [node]Input Types <<[ + [ [node]Input Types >> ]Set[ [dest]IO Num >>, [[()]Append[new type]]Append[new count] ] ] out <- Infer Types Node[[nodelist]Set[[dest]Index >>, new node], new node, [dest]Index >>, prog, worker] } @@ -365,21 +366,21 @@ If[[Type Of[[node]Data >>]] = ["Whole Number"]] { outtype <- Type Instance["Int32"] - }{ - If[[Type Of[[node]Data >>]] = ["Type Instance"]] - { - outtype <- Type Instance["Blueprint"] - }{ - If[[Type Of[[node]Data >>]] = ["Machine Integer"]] - { - If[[[node]Data >>]Signed? >>] - { base <- "Int" } - { base <- "UInt" } - - outtype <- Type Instance[[base]Append[ [[node]Data >>]Size >> ]] + }{ + If[[Type Of[[node]Data >>]] = ["Type Instance"]] + { + outtype <- Type Instance["Blueprint"] + }{ + If[[Type Of[[node]Data >>]] = ["Machine Integer"]] + { + If[[[node]Data >>]Signed? >>] + { base <- "Int" } + { base <- "UInt" } + + outtype <- Type Instance[[base]Append[ [[node]Data >>]Size >> ]] }{ - outtype <- Type Instance[Type Of[[node]Data >>]] - } + outtype <- Type Instance[Type Of[[node]Data >>]] + } } } nextnode <- [node]Output Types <<[ [()]Append[outtype] ] @@ -462,9 +463,9 @@ } Infer Types@NWorker[worker,prog:out] -{ +{ Print[["Start inference: "]Append[[worker]Name >>]] - out <- [worker]Nodes <<[Fold[[["Infer Types Node"]Set Input[3, prog]]Set Input[4, worker], [worker]Nodes >>, [worker]Nodes >>]] + out <- [worker]Nodes <<[Fold[[["Infer Types Node"]Set Input[3, prog]]Set Input[4, worker], [worker]Nodes >>, [worker]Nodes >>]] { Print[["End inference: "]Append[[worker]Name >>]] } } @@ -654,15 +655,15 @@ { //TODO: Support parametric types datstring <- [[node]Data >>]Name >> - }{ + }{ If[[Type Of[[node]Data >>]] = ["Machine Integer"]] - { - If[[[node]Data >>]Signed? >>] - { s <- "i" } - { s <- "u" } - datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] + { + If[[[node]Data >>]Signed? >>] + { s <- "i" } + { s <- "u" } + datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] }{ - datstring <- [node]Data >> + datstring <- [node]Data >> } } out <- Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring]] @@ -749,10 +750,10 @@ with call <- [func]Method Call[[[node]Data >>]Name >>, inputs] { Print["Method Call done"] } }{ - Print["Function!"] - { - Pretty Print[inputs, ""] - { with call <- [func]Call[[[node]Data >>]Name >>, inputs]} + Print["Function!"] + { + Pretty Print[inputs, ""] + { with call <- [func]Call[[[node]Data >>]Name >>, inputs]} } } @@ -784,15 +785,15 @@ nstream <- Compile Call Node[node, program, stream, inputs, node index] }{ If[[[node]Type >>] = ["output"]] - { + { Print["Compiling output node"] inputs <- [worker]Collect Inputs[node] [conditions]For Backend - { + { Print["has conditions"] stream <- [func]Instruction Stream nfunc <- [func]Do If[~, nstream] - }{ + }{ Print["no conditions"] stream <- Val[func] nfunc <- Val[nstream] @@ -808,13 +809,13 @@ datstring <- [[node]Data >>]Name >> }{ If[[Type Of[[node]Data >>]] = ["Machine Integer"]] - { - If[[[node]Data >>]Signed? >>] - { s <- "i" } - { s <- "u" } - datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] + { + If[[[node]Data >>]Signed? >>] + { s <- "i" } + { s <- "u" } + datstring <- [[[[node]Data >>]Value >>]Append[s]]Append[[[node]Data >>]Size >>] }{ - datstring <- [node]Data >> + datstring <- [node]Data >> } } nfunc <- [func]Register Constant[[[Type Of[[node]Data >>]]Append["_"]]Append[datstring], [node]Data >>] @@ -898,143 +899,143 @@ Result Vars@NWorker[worker:out] { out <- Fold["Node Result Vars", (), [worker]Nodes >>] -} - -Make Basic Type[type:out] -{ - out <- [Type Instance[[type]Name >>]]Params <<[ [type]Params >> ] -} - -FInputs[ifunc, input type, index, inputs:out] -{ - func <- [ifunc]Set Input Type[Make Basic Type[input type], index] - name <- [inputs]Index[index] - Print[["FInputs: "]Append[index]] - Pretty Print[input type, ""] - { - If[[[input type]Variant >>] = ["Naked"]] - { - - naked <- [" naked"]Append[name] - - out <- [[[func]Allocate Var[naked, input type] - ]Unbox[name, naked] - ]Release[name] - }{ - If[[input type]Mutable? >>] - { - name <- [inputs]Index[index] - copied <- [func]Copy[name, name] - - }{ - copied <- Val[func] - } - If[[[input type]Variant >>] = ["Raw Pointer"]] - { - raw <- [" raw"]Append[name] - If[[[input type]Name >>]=["Array"]] - { - - out <- [[copied]Allocate Var[raw, input type] - ]Array Raw Pointer[name, raw] - }{ - out <- [[copied]Allocate Var[raw, input type] - ]Get Raw Pointer[name, raw] - } - }{ - out <- Val[copied] - } - } - } -} - -FParams[input:out] -{ - iname <- [input]Index[0] - type <- [input]Index[1] - If[[[type]Variant >>] = ["Naked"]] - { out <- [" naked"]Append[iname] } - { - If[[[type]Variant >>] = ["Raw Pointer"]] - { out <- [" raw"]Append[iname] } - { out <- Val[iname] } - } -} -_Return Param[outputs, inputs, input types, index:out,none] -{ - Print[["_Return Param: "]Append[output]] - output <- [outputs]Index[index] - [inputs]Find[output] - { - If[[[input types]Index[~]]Mutable? >>] - { - ,none <- [outputs]Next[index] - { - out,none <- _Return Param[outputs, inputs, input types, ~] - } - } { - out <- index - } - }{ - out <- index - } -} - -Return Param[outputs, inputs, input types:out,none] -{ - Print["Return Param"] - ,none <- [outputs]First - { out,none <- _Return Param[outputs, inputs, input types, ~] } - -} - -Save Foreign Result[func, output, index, output types, inputs, input types:out] -{ - type <- [output types]Index[index] - If[[[type]Variant >>] = ["Naked"]] - { - out <- [func]Box[[" naked"]Append[output], output, type] - }{ - [inputs]Find[output] - { - If[[[input types]Index[~]]Mutable? >>] - { - out <- [func]Move[output, Output[output]] - }{ - out <- func - } - }{ - out <- func - } - } -} - -Compile Foreign Stub[worker,program,name:out] -{ - Print[["Compiling FFI stub for "]Append[name]] - ifunc <- [[program]Create Function[name, [worker]Inputs >>, [worker]Outputs >>, "rhope"] - ]Output Types <<[Map[[worker]Output Types >>, "Make Basic Type"]] - - rp num <- Return Param[[worker]Outputs >>, [worker]Inputs >>, [worker]Input Types >>] - { - rbase <- [[worker]Outputs >>]Index[rp num] - If[[[[[worker]Output Types >>]Index[rp num]]Variant >>] = ["Naked"]] - { - rparam <- [" naked"]Append[rbase] - rfunc <- [ifunc]Allocate Var[rparam, [[worker]Output Types >>]Index[rp num]] - }{ - rparam <- Val[rbase] - rfunc <- Val[ifunc] - } - }{ - rparam <- "" - rfunc <- Val[ifunc] - } - - Fold[["FInputs"]Set Input[3, [worker]Inputs >>], rfunc, [worker]Input Types >>] - { [~]Call Foreign[name, [worker]Convention >>, Map[Zip[[worker]Inputs >>, [worker]Input Types >>], "FParams"], rparam] - { Fold[[[["Save Foreign Result"]Set Input[3, [worker]Output Types >>]]Set Input[4, [worker]Inputs >>]]Set Input[5, [worker]Input Types >>], ~, [worker]Outputs >>] - { out <- [program]Store Function[~] }}} +} + +Make Basic Type[type:out] +{ + out <- [Type Instance[[type]Name >>]]Params <<[ [type]Params >> ] +} + +FInputs[ifunc, input type, index, inputs:out] +{ + func <- [ifunc]Set Input Type[Make Basic Type[input type], index] + name <- [inputs]Index[index] + Print[["FInputs: "]Append[index]] + Pretty Print[input type, ""] + { + If[[[input type]Variant >>] = ["Naked"]] + { + + naked <- [" naked"]Append[name] + + out <- [[[func]Allocate Var[naked, input type] + ]Unbox[name, naked] + ]Release[name] + }{ + If[[input type]Mutable? >>] + { + name <- [inputs]Index[index] + copied <- [func]Copy[name, name] + + }{ + copied <- Val[func] + } + If[[[input type]Variant >>] = ["Raw Pointer"]] + { + raw <- [" raw"]Append[name] + If[[[input type]Name >>]=["Array"]] + { + + out <- [[copied]Allocate Var[raw, input type] + ]Array Raw Pointer[name, raw] + }{ + out <- [[copied]Allocate Var[raw, input type] + ]Get Raw Pointer[name, raw] + } + }{ + out <- Val[copied] + } + } + } +} + +FParams[input:out] +{ + iname <- [input]Index[0] + type <- [input]Index[1] + If[[[type]Variant >>] = ["Naked"]] + { out <- [" naked"]Append[iname] } + { + If[[[type]Variant >>] = ["Raw Pointer"]] + { out <- [" raw"]Append[iname] } + { out <- Val[iname] } + } +} +_Return Param[outputs, inputs, input types, index:out,none] +{ + Print[["_Return Param: "]Append[output]] + output <- [outputs]Index[index] + [inputs]Find[output] + { + If[[[input types]Index[~]]Mutable? >>] + { + ,none <- [outputs]Next[index] + { + out,none <- _Return Param[outputs, inputs, input types, ~] + } + } { + out <- index + } + }{ + out <- index + } +} + +Return Param[outputs, inputs, input types:out,none] +{ + Print["Return Param"] + ,none <- [outputs]First + { out,none <- _Return Param[outputs, inputs, input types, ~] } + +} + +Save Foreign Result[func, output, index, output types, inputs, input types:out] +{ + type <- [output types]Index[index] + If[[[type]Variant >>] = ["Naked"]] + { + out <- [func]Box[[" naked"]Append[output], output, type] + }{ + [inputs]Find[output] + { + If[[[input types]Index[~]]Mutable? >>] + { + out <- [func]Move[output, Output[output]] + }{ + out <- func + } + }{ + out <- func + } + } +} + +Compile Foreign Stub[worker,program,name:out] +{ + Print[["Compiling FFI stub for "]Append[name]] + ifunc <- [[program]Create Function[name, [worker]Inputs >>, [worker]Outputs >>, "rhope"] + ]Output Types <<[Map[[worker]Output Types >>, "Make Basic Type"]] + + rp num <- Return Param[[worker]Outputs >>, [worker]Inputs >>, [worker]Input Types >>] + { + rbase <- [[worker]Outputs >>]Index[rp num] + If[[[[[worker]Output Types >>]Index[rp num]]Variant >>] = ["Naked"]] + { + rparam <- [" naked"]Append[rbase] + rfunc <- [ifunc]Allocate Var[rparam, [[worker]Output Types >>]Index[rp num]] + }{ + rparam <- Val[rbase] + rfunc <- Val[ifunc] + } + }{ + rparam <- "" + rfunc <- Val[ifunc] + } + + Fold[["FInputs"]Set Input[3, [worker]Inputs >>], rfunc, [worker]Input Types >>] + { [~]Call Foreign[name, [worker]Convention >>, Map[Zip[[worker]Inputs >>, [worker]Input Types >>], "FParams"], rparam] + { Fold[[[["Save Foreign Result"]Set Input[3, [worker]Output Types >>]]Set Input[4, [worker]Inputs >>]]Set Input[5, [worker]Input Types >>], ~, [worker]Outputs >>] + { out <- [program]Store Function[~] }}} } Compile Worker@NWorker[worker,program,name:out] @@ -1042,8 +1043,8 @@ If[[worker]Builtin? >>] { out <- program - }{ - If[[[worker]Library >>] = [""]] + }{ + If[[[worker]Library >>] = [""]] { Print[["Compiling: "]Append[name]] { @@ -1060,9 +1061,9 @@ final func <- Val[func] } out <- [program]Store Function[Fold["Release", Fold[["Release Var"]Set Input[0, worker], final func, res vars], [worker]Inputs >>]] - } - }{ - out <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name] + } + }{ + out <- Compile Foreign Stub[worker,[program]Link[[worker]Convention >>, [worker]Library >> ],name] } } } @@ -1208,7 +1209,7 @@ { getter <- [[start getter]Do AddRef[getref, "out"]]Release["obj"] }{ - getter <- [[start getter]Box[getref, "out", type]]Release["obj"] + getter <- [[start getter]Box[getref, "out", type]]Release["obj"] { Print["Got getter"] } } @@ -1236,7 +1237,7 @@ ,setref <- [begin setter]Write Field["obj", name] { setter <- [[~]Unbox["newval", setref] - ]Move["obj", "out"] + ]Move["obj", "out"] { Print ["got setter"] } } } @@ -1291,8 +1292,8 @@ after bind <- [prog]Workers << [ [[prog]Workers >>]Set[name, [worker]Name <<[name]] ] parts <- [name]Split["@"] [parts]Index[1] - { - orig bp <- [[after bind]Blueprints >>]Index[~] {} + { + orig bp <- [[after bind]Blueprints >>]Index[~] {} { orig bp <- NBlueprint[] } out <- [after bind]Blueprints <<[ [[after bind]Blueprints >>]Set[~, [orig bp]Add Method[[parts]Index[0]] ] ] }{ @@ -1317,16 +1318,16 @@ } _Compile Program[backend, worker, name:out] -{ +{ Print["_Compile Program"] out <- [worker]Compile Worker[backend, name] } Compile Program@NProgram[prog, backend:out] -{ - backend with bps <- Generate Number Methods[Fold["_Compile Program BP Special", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Blueprints >>]] - { Print["All blueprints added"] } - workers with infer <- SMap[[prog]Workers >>, ["Infer Types"]Set Input[1, prog]] +{ + backend with bps <- Generate Boolean Methods[Generate Number Methods[Fold["_Compile Program BP Special", Fold["_Compile Program BP", backend, [prog]Blueprints >>], [prog]Blueprints >>]]] + { Print["All blueprints added"] } + workers with infer <- SMap[[prog]Workers >>, ["Infer Types"]Set Input[1, prog]] { Print["Type inference complete"] } out <- Fold["_Compile Program", backend with bps, workers with infer] } @@ -1339,10 +1340,10 @@ Register Worker@NProgram[prog, name, convention, inputs, outputs: out] { Print[["Register Worker "]Append[name]] - after reg <- [prog]Worker Refs <<[ - [ [prog]Worker Refs >> ]Set[name, - Worker Ref[name, convention, inputs, outputs, No] - ] + after reg <- [prog]Worker Refs <<[ + [ [prog]Worker Refs >> ]Set[name, + Worker Ref[name, convention, inputs, outputs, No] + ] ] parts <- [name]Split["@"] [parts]Index[1] @@ -1357,8 +1358,8 @@ { registered <- [[[[[prog]Register Worker["Print", "rhope", 1, 1] ]Register Worker["If@Boolean", "rhope", 1, 2] - ]Register Worker["Build", "rhope", 1, 1] - ]Register Worker["Blueprint Of", "rhope", 1, 1] + ]Register Worker["Build", "rhope", 1, 1] + ]Register Worker["Blueprint Of", "rhope", 1, 1] ]Register Number Methods out <- [[[[registered]Bind Worker["If@Boolean", @@ -1381,13 +1382,13 @@ ]Input Types <<[ [()]Append[Type Instance["Blueprint"]] ] ]Outputs <<[("out")] ]Output Types <<[ [()]Append[Type Instance["Any Type"]] ] - ]Builtin? <<[Yes]] - ]Bind Worker["Blueprint Of", - [[[[[NWorker["rhope"] - ]Inputs <<[("object")] - ]Input Types <<[ [()]Append[Type Instance["Any Type"]]] - ]Outputs <<[("type")] - ]Output Types <<[ [()]Append[Type Instance["Blueprint"]]] + ]Builtin? <<[Yes]] + ]Bind Worker["Blueprint Of", + [[[[[NWorker["rhope"] + ]Inputs <<[("object")] + ]Input Types <<[ [()]Append[Type Instance["Any Type"]]] + ]Outputs <<[("type")] + ]Output Types <<[ [()]Append[Type Instance["Blueprint"]]] ]Builtin? <<[Yes]] }