X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=results.mtask.tex;h=c32f7acf318e05ad9d6a03f3404b136458d6b089;hb=7694dfaa240c6dd9af2f1ac8880f0fb1d7f38324;hp=9580dec4917da66c9a8f29699ced1c31f8c60018;hpb=7b0fe8509016c0841d35239dc87150e945cfd960;p=msc-thesis1617.git diff --git a/results.mtask.tex b/results.mtask.tex index 9580dec..c32f7ac 100644 --- a/results.mtask.tex +++ b/results.mtask.tex @@ -6,9 +6,24 @@ does not have to implement all the available classes. Moreover, classes can be added at will without interfering with the existing views. \section{Semantics} -\todo{semantics} +\subsection{\glspl{mTask}} +The current \gls{mTask} engine for devices does not support \glspl{Task} in the +sense that the \gls{C}-view it does. \glspl{Task} in the new system are are +\CI{Main} objects with a program inside. A \gls{Task} runs periodically, on +interrupt or one-shot. +\todo{elaborate} -\section{Bytecode compilation} +\subsection{\glspl{SDS}} +\glspl{SDS} behave a little bit differently on an \gls{mTask} device than in +the \gls{iTasks} system. In an \gls{iTasks} system, when the \gls{SDS} is +updated, a broadcast to everyone in the system watching is made to notify them +of an update. \glspl{SDS} on the device can update very often and the update +might not be the final value it will get. Therefore a device must publish the +\gls{SDS} explicitly to save bandwidth. This means that an extra function is +added to the \CI{sds} class that adds the \CI{pub} function. +\todo{elaborate} + +\section{Bytecode compilation}\label{sec:compiler} The \glspl{mTask} are sent to the device in bytecode and are saved in the memory of the device. To compile the \gls{EDSL} code to bytecode, a view is added to the \gls{mTask}-system called \CI{ByteCode}. As shown in @@ -38,14 +53,14 @@ accordingly. \begin{lstlisting}[label={lst:bcview},caption={Bytecode view}] :: ByteCode a p = BC (RWS () [BC] BCState ()) :: BCValue = E.e: BCValue e & mTaskType, TC e -:: BCShare = { - sdsi :: Int, - sdsval :: BCValue +:: BCShare = + { sdsi :: Int + , sdsval :: BCValue } -:: BCState = { - freshl :: [Int], - freshs :: [Int], - sdss :: [BCShare] +:: BCState = + { freshl :: [Int] + , freshs :: [Int] + , sdss :: [BCShare] } class toByteCode a :: a -> String @@ -119,7 +134,7 @@ to the \emph{Writer} value. op2 :: (ByteCode a p1) (ByteCode a p2) BC -> ByteCode b Expr op2 (BC x) (BC y) bc = BC (x >>| y >>| tell [bc]) -op :: (ByteCode a p) BC -> ByteCode a Expr +op :: (ByteCode a p) BC -> ByteCode b c op (BC x) bc = BC (x >>| tell [bc]) tell` :: [BC] -> (ByteCode a p) @@ -148,8 +163,31 @@ instance userLed ByteCode where \end{lstlisting} \subsection{Control Flow} +Sequence is very straightforward in the bytecode view. The function just +sequences the two \glspl{RWST}. The \emph{If} statement requires some detailed +explanation since labels come in to play. The implementation speaks for itself +in Listing~\ref{lst:controlflow}. First all the labels are gathered and then +they are placed in the correct order in the bytecode sequence. It can happen +that multiple labels appear consecutively in the code. This is not a problem +since the labels are resolved to real addresses later on anyways. + \begin{lstlisting}[label={lst:controlflow},% caption={Bytecode view for \texttt{arith}}] +freshlabel = get >>= \st=:{freshl=[fr:frs]}->put {st & freshl=frs} >>| pure fr + +instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e +instance If ByteCode e Stmt Stmt where If b t e = BCIfStmt b t e +instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e +instance If ByteCode x y Stmt where If b t e = BCIfStmt b t e +instance IF ByteCode where + IF b t e = BCIfStmt b t e + (?) b t = BCIfStmt b t (tell` []) +BCIfStmt (BC b) (BC t) (BC e) = BC ( + freshlabel >>= \else->freshlabel >>= \endif-> + b >>| tell [BCJmpF else] >>| + t >>| tell [BCJmp endif, BCLab else] >>| + e >>| tell [BCLab endif] + ) instance noOp ByteCode where noOp = tell` [BCNop] \end{lstlisting} @@ -159,17 +197,20 @@ Shared data sources must be acquired from the state and constructing one happens via multiple steps. First a fresh identifier is grabbed from the state. Then a \CI{BCShare} record is created with that identifier. A \CI{BCSdsFetch} instruction is written and the body is generated to finally add the share to -the actual state. The exact implementation is shown in -Listing~\ref{lst:shareview}. +the actual state with the value obtained from the function. The exact +implementation is shown in Listing~\ref{lst:shareview}. \begin{lstlisting}[label={lst:shareview},% caption={Bytecode view for \texttt{arith}}] +freshshare = get >>= \st=:{freshl=[fr:frs]}->put {st & freshl=frs} >>| pure fr + instance sds ByteCode where - sds f = {main = BC (freshs - >>= \sdsi->pure {BCShare | sdsi=sdsi,sdsval=BCValue 0} - >>= \sds->pure (f (tell` [BCSdsFetch sds])) - >>= \(v In bdy)->modify (addSDS sds v) - >>| unBC (unMain bdy))} + sds f = {main = BC (freshshare + >>= \sdsi->pure {BCShare | sdsi=sdsi,sdsval=BCValue 0} + >>= \sds->pure (f (tell` [BCSdsFetch sds])) + >>= \(v In bdy)->modify (addSDS sds v) + >>| unBC (unMain bdy)) + } pub (BC x) = BC (censor (\[BCSdsFetch s]->[BCSdsPublish s]) x) addSDS sds v s = {s & sdss=[{sds & sdsval=BCValue v}:s.sdss]} @@ -194,4 +235,56 @@ makeStore [...] = [...] \end{lstlisting} \section{Actual Compilation} -\todo{hulp functies voor compileren} +All the previous functions are tied together with the \CI{toMessages} function. +This function compiles the bytecode and transforms the \gls{Task} in a message. +The \glspl{SDS} that were not already sent to the device are also placed in +messages to be sent to the device. This functionality is listed in +Listing~\ref{lst:compilation}. The compilation process consists of two steps. +First the \gls{RWST} is executed, secondly the \emph{Jump} statements that jump +to labels are transformed to jump to addresses. The translation of labels is +possible in one sweep because no labels are reused. Reusing labels would not +give a speed improvement since the labels are removed anyways in the end. + +\begin{lstlisting}[label={lst:compilation},% + caption={Actual compilation.}] +bclength :: BC -> Int +bclength (BCPush s) = 1 + size (toByteCode s) +bclength ... = ... +bclength x = 1 + consNum{|*|} x + +computeGotos :: [BC] Int -> ([BC], Map Int Int) +computeGotos [] _ = ([], newMap) +computeGotos [BCLab l:xs] i = appSnd ('DM'.put l i) (computeGotos xs i) +computeGotos [x:xs] i = appFst (\bc->[x:bc]) (computeGotos xs (i + bclength x)) + +toRealByteCode :: (ByteCode a b) BCState -> (String, BCState) +toRealByteCode x s +# (s, bc) = runBC x s +# (bc, gtmap) = computeGotos bc 1 += (concat (map (toString o toByteVal) (map (implGotos gtmap) bc)), s) + +toMessages :: MTaskInterval (Main (ByteCode a b)) BCState -> ([MTaskMSGSend], BCState) +toMessages interval x oldstate +# (bc, newstate) = toRealByteCode (unMain x) oldstate +# newsdss = difference newstate.sdss oldstate.sdss += ([MTSds sdsi e\\{sdsi,sdsval=e}<-newsdss] ++ [MTTask interval bc], newstate) +\end{lstlisting} + +\section{Example} +The heating example given previously in Listing~\ref{lst:exmtask} would be +compiled to the following code. The left column indicates the +position in the program memory. + +\begin{lstlisting}[caption={Thermostat bytecode},language=c] + 1-2 : BCAnalogRead (Analog A0) + 3-6 : BCPush (Int 50) + 7 : BCGre + 8-9 : BCJmpF 17 //Jump to else +10-12: BCPush (Bool 1) +13-14: BCDigitalWrite (Digital D0) +15-16: BCJmp 21 //Jump to end of if +17-19: BCPush (Bool 0) //Else label +20 : BCDigitalWrite (Digital D0) +\end{lstlisting} + +\todo{add more elaborate example?}