summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2018-04-15 10:20:11 +0200
committerTom Smeding <tom.smeding@gmail.com>2018-04-15 10:20:11 +0200
commit32147b215e5066545e4f971384f6f9c2865354fd (patch)
treec4e095532dec58f6f112b0d7c08c6eef943697d9
parent001804756eefd24bedb2ac55170e9df4827f2964 (diff)
Better generated code
-rw-r--r--Compiler.hs19
1 files changed, 12 insertions, 7 deletions
diff --git a/Compiler.hs b/Compiler.hs
index f02a50a..23ce5d3 100644
--- a/Compiler.hs
+++ b/Compiler.hs
@@ -64,12 +64,18 @@ compileIns (ICopy _ _ 0) = return ()
compileIns (ICopy from to m) = do
emit $ "mov al, [" ++ cursorOffset from ++ "]"
case m of
- 1 -> return ()
- _ | Just p <- isTwoPower m -> emit $ "shl al, " ++ show p
+ 1 -> emit $ "add [" ++ cursorOffset to ++ "], al"
+ -1 -> emit $ "sub [" ++ cursorOffset to ++ "], al"
+ _ | Just p <- isTwoPower m -> do
+ emit $ "shl al, " ++ show p
+ emit $ "add [" ++ cursorOffset to ++ "], al"
+ | Just p <- isTwoPower (-m) -> do
+ emit $ "shl al, " ++ show p
+ emit $ "sub [" ++ cursorOffset to ++ "], al"
| otherwise -> do
- emit $ "mov cl, " ++ show m
- emit "mul byte cl"
- emit $ "add [" ++ cursorOffset to ++ "], al"
+ emit $ "mov cl, " ++ show m
+ emit "mul byte cl"
+ emit $ "add [" ++ cursorOffset to ++ "], al"
compileIns (ISlide off) = emit $ "add " ++ cursorReg ++ ", " ++ show off
compileIns (ILoop inss off) = do
loopid <- genId
@@ -84,8 +90,7 @@ compileIns (IInput off) = do
emit $ generateCall "getchar"
emit $ "mov [" ++ cursorOffset off ++ "], al"
compileIns (IOutput off) = do
- emit "xor edi, edi"
- emit $ "mov dil, [" ++ cursorOffset off ++ "]"
+ emit $ "movzx edi, byte [" ++ cursorOffset off ++ "]"
emit $ generateCall "putchar"
compileIns IStart = return ()