+\subsection{Instruction Set}\label{sec:instruction}
+The instruction set is given in Listing~\ref{bc:instr}. The instruction set is
+kept large, but under $255$, to get the highest expressivity while keeping all
+instruction within one byte.
+
+The interpreter running in the client is a stack machine. The virtual
+instruction \CI{BCLab} is added to allow for an easy implementation of jumping.
+However, this is not a real instruction and the labels are resolved to actual
+program memory addresses in the final step of compilation to save instructions
+and avoid label lookups at runtime.
+
+\begin{lstlisting}[label={bc:instr},%
+ caption={Bytecode instruction set}]
+:: BC = BCNop
+ | BCLab Int | BCPush BCValue | BCPop
+ //SDS functions
+ | BCSdsStore BCShare | BCSdsFetch BCShare | BCSdsPublish BCShare
+ //Unary ops
+ | BCNot
+ //Binary Int ops
+ | BCAdd | BCSub | BCMul
+ | BCDiv
+ //Binary Bool ops
+ | BCAnd | BCOr
+ //Binary ops
+ | BCEq | BCNeq | BCLes | BCGre
+ | BCLeq | BCGeq
+ //Conditionals and jumping
+ | BCJmp Int | BCJmpT Int | BCJmpF Int
+ //UserLED
+ | BCLedOn | BCLedOff
+ //Pins
+ | BCAnalogRead Pin | BCAnalogWrite Pin | BCDigitalRead Pin | BCDigitalWrite Pin
+ //Return
+ | BCReturn
+\end{lstlisting}
+
+All single byte instructions are converted automatically using the generic
+function \CI{consIndex} which returns the index of the constructor. The index
+of the constructor is the byte value for all instructions. The last step of the
+compilation is transforming the list of bytecode instructions to actual bytes.
+
+\subsection{Helper functions}
+The \CI{ByteCode} type is just a boxed \gls{RWST} and that gives access to
+the whole range of \gls{RWST} functions. However, to apply a function the type
+must be unboxed. After application the type must be boxed again. To achieve
+this, several helper functions have been created. They are listed in
+Listing~\ref{lst:helpers}. The \CI{op} and \CI{op2} functions is hand-crafted
+to make operators that pop one or two values off the stack respectively. The
+\CI{tell`} is a wrapper around the \gls{RWST} function \CI{tell} that appends
+the argument to the \emph{Writer} value.
+
+\begin{lstlisting}[label={lst:helpers},caption={Some helper functions}]
+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 b c
+op (BC x) bc = BC (x >>| tell [bc])
+
+tell` :: [BC] -> (ByteCode a p)
+tell` x = BC (tell x)
+
+unBC :: (ByteCode a p) -> RWS () [BC] BCState ()
+unBC (BC x) = x
+\end{lstlisting}
+
+\subsection{Arithmetics \& Peripherals}
+Almost all of the code from the simple classes exclusively use helper
+functions. Listing~\ref{lst:arithview} shows some implementations. The classes
+\CI{boolExpr} and the classes for the peripherals are implemented using the
+same strategy.
+
+\begin{lstlisting}[label={lst:arithview},caption={%
+ Bytecode view implementation for arithmetic and peripheral classes}]
+instance arith ByteCode where
+ lit x = tell` [BCPush (BCValue x)]
+ (+.) x y = op2 x y BCDiv
+ ...
+
+instance userLed ByteCode where
+ ledOn l = op l BCLedOn
+ ledOff l = op l BCLedOff
+\end{lstlisting}
+
+\subsection{Control Flow}
+Implementing the sequence operator 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 into play. The
+implementation speaks for itself in Listing~\ref{lst:controlflow}. First, all
+the labels are gathered after which 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 anyway.
+
+\begin{lstlisting}[label={lst:controlflow},%
+ caption={Bytecode view for \texttt{arith} class}]
+freshlabel = get >>= \st=:{freshl}->put {st & freshl=freshl+1} >>| pure freshl
+
+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}
+
+The semantics for the \glspl{mTask} bytecode view are different from the
+semantics for the \gls{C} view. \glspl{Task} in the \gls{C} view can start new
+\gls{Task} or themselves to continue, while in the bytecode view, \gls{Task}
+run idefinitly, one-shot or on interrupt. To allow interval and interrupt
+\glspl{Task} to terminate, a return instruction is added. This class was not
+available in the original system and is thus added. It just writes a single
+instruction so that the interpreter knows to stop execution.
+Listing~\ref{lst:return} shows the classes and implementation for the return
+expression.
+
+\begin{lstlisting}[label={lst:return},%
+ caption={Bytecode view for the return instruction}]
+class retrn v where
+ retrn :: v () Expr
+
+instance retrn ByteCode where
+ retrn = tell` [BCReturn]
+\end{lstlisting}
+
+\subsection{Shared Data Sources \& Assignment}
+Fresh \gls{SDS} are generated using the state and constructing one involves
+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 \gls{SDS}
+to 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=:{freshs}->put {st & freshs=freshs+1} >>| pure freshs
+
+instance sds ByteCode where
+ 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))
+ }
+instance sdspub ByteCode where
+ pub (BC x) = BC (censor (\[BCSdsFetch s]->[BCSdsPublish s]) x)
+
+addSDS sds v s = {s & sdss=[{sds & sdsval=BCValue v}:s.sdss]}
+\end{lstlisting}
+
+All assignable types compile to a \gls{RWST} that writes one fetch instruction.
+For example, using a \gls{SDS} always results in an expression of the form
+\CI{sds \x=4 In ...}. The actual \CI{x} is the \gls{RWST} that always writes
+one \CI{BCSdsFetch} instruction with the correctly embedded \gls{SDS}.
+Assigning to an analog pin will result in the \gls{RWST} containing the
+\CI{BCAnalogRead} instruction. When the operation on the assignable is not a
+read operation from but an assign operation, the instruction(s) will be
+rewritten accordingly. This results in an \CI{BCSdsStore} or \CI{BCAnalogWrite}
+instruction respectively. The implementation for this is given in
+Listing~\ref{lst:assignmentview}.
+
+\begin{lstlisting}[label={lst:assignmentview},%
+ caption={Bytecode view implementation for assignment.}]
+instance assign ByteCode where
+ (=.) (BC v) (BC e) = BC (e >>| censor makeStore v)
+
+makeStore [BCSdsFetch i] = [BCSdsStore i]
+makeStore [BCDigitalRead i] = [BCDigitalWrite i]
+makeStore [...] = [...]
+\end{lstlisting}
+
+\subsection{Actual Compilation}
+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 added as
+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. Then, the \emph{Jump} statements that
+jump to labels are transformed to jump to program memory 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
+anyway 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