TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Monads to Machine Code (Part 1)

151 pointsby primodemusover 9 years ago

4 comments

pkaover 9 years ago
What&#x27;s cool about monads is that the computation (assembly code in this particular case) is just a plain old value.<p>So if you have a fragment like this:<p><pre><code> factorial :: Int64 -&gt; X86 () factorial n = do mov rcx (I n) mov rax (I 1) l1 &lt;- label mul rcx loop l1 ret </code></pre> you can just compose this into a bigger program:<p><pre><code> two_factorials :: Int64 -&gt; Int64 -&gt; X86 () two_factorials n1 n2 = do factorial n1 factorial n2 </code></pre> Notice how the labels won&#x27;t get mixed up. This concept (representing computation by values) is incredibly powerful: one can compose, combine, abstract values into bigger values. For example, compare the compositional properties of RethinkDB&#x27;s query language to SQL.
评论 #10853633 未加载
评论 #10857161 未加载
chover 9 years ago
Always fun to see this type of thing implemented in Haskell.<p>Here are some other examples.<p>This one uses indexed monads to keep the instruction stream correct:<p><a href="https:&#x2F;&#x2F;intoverflow.wordpress.com&#x2F;2010&#x2F;05&#x2F;21&#x2F;announcing-potential-x86-64-assembler-as-a-haskell-edsl&#x2F;" rel="nofollow">https:&#x2F;&#x2F;intoverflow.wordpress.com&#x2F;2010&#x2F;05&#x2F;21&#x2F;announcing-pote...</a><p>And this one is very similar to the OP but from 2013, and emits 6502 assembly: <a href="http:&#x2F;&#x2F;wall.org&#x2F;~lewis&#x2F;2013&#x2F;10&#x2F;15&#x2F;asm-monad.html" rel="nofollow">http:&#x2F;&#x2F;wall.org&#x2F;~lewis&#x2F;2013&#x2F;10&#x2F;15&#x2F;asm-monad.html</a> (of course without the JIT portion).
评论 #10849114 未加载
brandonbloomover 9 years ago
Awesome article!<p>Seems a reasonably complete assembler too with one notable exception: Forward jumps, conditionals specifically. These may be a tad tricky to fit in to a monad because they require either knowing the future (eg. how far away the jump target is) or rewriting the past (eg. leave a hole and fill it in later).<p>I&#x27;d really like to see both approaches in Haskell, since they&#x27;re kinda different. My guess is that in the former case, you&#x27;d have nested&#x2F;delimited monads with buffers. However, I don&#x27;t know Haskell enough to figure out how to accomplish the hole-filling approach.<p>EDIT: On second thought, I guess both approaches (traditionally &quot;two-pass&quot; vs &quot;one-pass&quot; assemblers) can be accomplished by changing labels to be opaque rather than addresses and storing a symbol table in either JITMem or the two-types that would replace it for a two-pass approach.
评论 #10854702 未加载
评论 #10849386 未加载
anyfooover 9 years ago
You can do even more with Arrows. This very short presentation goes from Monads to Arrows, and explains why: <a href="https:&#x2F;&#x2F;www.dropbox.com&#x2F;s&#x2F;e3la5wueg1zsid7&#x2F;Arrows%20for%20CPU%20Emulation.pdf?dl=0" rel="nofollow">https:&#x2F;&#x2F;www.dropbox.com&#x2F;s&#x2F;e3la5wueg1zsid7&#x2F;Arrows%20for%20CPU...</a>