{-# LANGUAGE OverloadedStrings #-}

module LLVM.Examples.Maths (maths) where

import qualified LLVM.General.AST as AST

import Control.Monad
import Control.Lens ((.=))
import LLVM.General.AST.Type (double)
import LLVM.Gen


maths :: AST.Module
maths = mapLLVM . runLLVM defaultModule $ do

  moduleName .= "Maths"

  defn double "adds" [(double, "a", 8), (double, "b", 8)] $ do
    a <- getvar' "a" >>= load double 8
    b <- getvar' "b" >>= load double 8
    fadd' double a b >>= ret
    
  defn double "subs" [(double, "a", 8), (double, "b", 8)] $ do
    a <- getvar' "a" >>= load double 8
    b <- getvar' "b" >>= load double 8
    fsub' double a b >>= ret

  defn double "muls" [(double, "a", 8), (double, "b", 8)] $ do
    a <- getvar' "a" >>= load double 8
    b <- getvar' "b" >>= load double 8
    fmul' double a b >>= ret

  defn double "divs" [(double, "a", 8), (double, "b", 8)] $ do
    a <- getvar' "a" >>= load double 8
    b <- getvar' "b" >>= load double 8
    fdiv' double a b >>= ret

