Skip to content

Commit

Permalink
update vampir backend
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Feb 22, 2024
1 parent 17ded18 commit 972d9a9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
13 changes: 13 additions & 0 deletions runtime/src/vampir/stdlib.pir
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ def mul (x, e1) (y, e2) = {
(x * y, e1 * e2 * range_check (x * y))
};

def fadd (x, e1) (y, e2) = {
(x + y, e1 * e2)
};
def fsub (x, e1) (y, e2) = {
(x - y, e1 * e2)
};
def fmul (x, e1) (y, e2) = {
(x * y, e1 * e2)
};
def fdiv (x, e1) (y, e2) = {
(x / y, e1 * e2)
};

def equal (x, e1) (y, e2) = (isZero (x - y), e1 * e2);

def if (b, e) (x, e1) (y, e2) = (b * x + (1 - b) * y, e * (b * e1 + (1 - b) * e2));
Expand Down
5 changes: 5 additions & 0 deletions runtime/src/vampir/stdlib_unsafe.pir
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def add x y = x + y;
def sub x y = x - y;
def mul x y = x * y;

def fadd x y = x + y;
def fsub x y = x - y;
def fmul x y = x * y;
def fdiv x y = x / y;

def equal x y = isZero (x - y);

def if b x y = b * x + (1 - b) * y;
Expand Down
4 changes: 4 additions & 0 deletions src/Juvix/Compiler/Backend/VampIR/Language.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ data OpCode
| OpMul
| OpDiv
| OpMod
| OpFieldAdd
| OpFieldSub
| OpFieldMul
| OpFieldDiv
| OpEq
| OpLt
| OpLe
Expand Down
4 changes: 4 additions & 0 deletions src/Juvix/Compiler/Backend/VampIR/Pretty/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ instance PrettyCode OpCode where
OpMul -> return kwMul
OpDiv -> return kwDiv
OpMod -> return kwMod
OpFieldAdd -> return kwFieldAdd
OpFieldSub -> return kwFieldSub
OpFieldMul -> return kwFieldMul
OpFieldDiv -> return kwFieldDiv
OpEq -> return kwEqual
OpLt -> return kwLessThan
OpLe -> return kwLessOrEqual
Expand Down
12 changes: 12 additions & 0 deletions src/Juvix/Compiler/Backend/VampIR/Pretty/Keywords.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ kwDiv = keyword Str.vampirDiv
kwMod :: Doc Ann
kwMod = keyword Str.vampirMod

kwFieldAdd :: Doc Ann
kwFieldAdd = keyword Str.fadd

kwFieldSub :: Doc Ann
kwFieldSub = keyword Str.fsub

kwFieldMul :: Doc Ann
kwFieldMul = keyword Str.fmul

kwFieldDiv :: Doc Ann
kwFieldDiv = keyword Str.fdiv

kwEqual :: Doc Ann
kwEqual = keyword Str.vampirEqual

Expand Down
9 changes: 8 additions & 1 deletion src/Juvix/Compiler/Backend/VampIR/Translation/FromCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,18 @@ fromCoreNode ii node =
OpIntMod -> OpMod
OpIntLt -> OpLt
OpIntLe -> OpLe
Core.OpFieldAdd -> VampIR.OpFieldAdd
Core.OpFieldSub -> VampIR.OpFieldSub
Core.OpFieldMul -> VampIR.OpFieldMul
Core.OpFieldDiv -> VampIR.OpFieldDiv
Core.OpEq -> VampIR.OpEq
_ -> impossible
_ -> case _builtinAppOp of
[x] -> case _builtinAppOp of
OpFail -> ExpressionFail
OpFieldToInt -> convertExpr x
OpFieldFromInt -> convertExpr x
_ -> impossible
_ -> impossible

goConstructor :: Constr -> Expression
goConstructor Constr {..} = case _constrTag of
Expand Down

0 comments on commit 972d9a9

Please sign in to comment.