X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=mtaskext.examples.tex;h=05c1d9bbfa8d68205bdb0fe7ef394a8d0a78e192;hb=4bb4ae51e1199aa618d8765b0bc429fba9055140;hp=66c24660dd46af92c731b21d7fa5533baf00b9bc;hpb=6548a5ec9ce8e0df67fc4019625ab5238eb1bf71;p=msc-thesis1617.git diff --git a/mtaskext.examples.tex b/mtaskext.examples.tex index 66c2466..05c1d9b 100644 --- a/mtaskext.examples.tex +++ b/mtaskext.examples.tex @@ -1,6 +1,6 @@ -The thermostat 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. +As an example for the bytecode compilation the following listing shows the +thermostat example given in Listing~\ref{lst:exmtask} compiled to bytecode. +The left column indicates the position in the program memory. \begin{lstlisting}[caption={Thermostat bytecode},language=c] 1-2 : BCAnalogRead (Analog A0) @@ -9,7 +9,53 @@ position in the program memory. 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 +15-16: BCJmp 21 //Jump to endif 17-19: BCPush (Bool 0) //Else label 20 : BCDigitalWrite (Digital D0) +21 : //Endif label +\end{lstlisting} + +The factorial function can be expressed as an \gls{mTask}-\gls{Task} and uses +the \CI{sds} and the \CI{return} functionality. Typically this \gls{Task} is +called with the \CI{OnInterval} scheduling strategy and will calculate the +factorial after which is will return. The following listings shows the actual +\gls{mTask} and the generated messages followed by the actual bytecode in a +readable form. + +\begin{lstlisting}[caption={Factorial as an \gls{mTask}-\gls{Task}}] +factorial :: Int -> Main (ByteCode () Stmt) +factorial i = sds \y=i In + namedsds \x=1 Named "result" In + {main = + IF (y <=. lit 1) ( + pub x :. retrn + ) ( + x =. x *. y :. y =. y -. lit 1 + )} + +//Generating the actual messages with: +Start = fst $ toMessages (OnInterval 500) (factorial 5) zero + +//The output will be +//[MTSds 2 (BCValue 5), MTSds 1 (BCValue 1), MTTask (OnInterval 500) ...] +\end{lstlisting} + +\begin{lstlisting}[label={lst:actualbc},% + caption={The resulting bytecode for the factorial function},language=C] + 0-2 : BCSdsFetch 1 + 3-6 : BCPush (Int 1) + 7 : BCLeq + 8-9 : BCJmpF 16 //Jump to else +10-12: BCSdsPublish 2 ("result") +13 : BCReturn +14-15: BCJmp 37 //Jump to endif +16-18: BCSdsFetch 2 ("result") //Else label +19-21: BCSdsFetch 1 +22 : BCMul +23-25: BCSdsStore 2 ("result") +26-28: BCSdsFetch 1 +29-32: BCPush (Int 1) +33 : BCSub +34-36: BCSdsStore 1 +37 : //Endif label \end{lstlisting}