Skip to content

Commit

Permalink
implements InfC, NegInfC, NanC for nifc (#104)
Browse files Browse the repository at this point in the history
* implements NifC, NegInfC, NanC for nifc

* clean up

* adds tests

---------

Co-authored-by: Andreas Rumpf <[email protected]>
  • Loading branch information
ringabout and Araq authored Oct 19, 2024
1 parent d4f19fd commit a32cd88
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/nifc-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Expr ::= Number | CharLiteral | StringLiteral |
(par Expr) | # wraps the expression in parentheses
(addr Lvalue) | # "address of" operation
(nil) | (false) | (true) |
(inf) | (neginf) | (nan) |
(and Expr Expr) | # "&&"
(or Expr Expr) | # "||"
(not Expr) | # "!"
Expand Down
3 changes: 3 additions & 0 deletions src/nifc/amd64/genasm_e.nim
Original file line number Diff line number Diff line change
Expand Up @@ -812,5 +812,8 @@ proc genx(c: var GeneratedCode; t: Tree; n: NodePos; dest: var Location) =
# genConv c, t, n
of SufC:
genSuffix(c, t, n, dest)
of InfC, NegInfC, NanC:
# TODO:
discard
else:
genLvalue c, t, n, dest
2 changes: 1 addition & 1 deletion src/nifc/amd64/register_allocator.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ proc allocRegsForProc(c: var GeneratedCode; t: Tree; n: NodePos; weights: Table[
let k = t[n].kind
case k
of Empty, Ident, SymDef, Sym, IntLit, UIntLit, FloatLit, CharLit, StrLit, Err,
NilC, FalseC, TrueC, SizeofC:
NilC, FalseC, TrueC, SizeofC, InfC, NegInfC, NanC:
discard
of StmtsC, ScopeC:
c.openScope()
Expand Down
22 changes: 22 additions & 0 deletions src/nifc/cprelude.nim
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,27 @@ typedef NU8 NU;
#define NIM_TRUE true
#define NIM_FALSE false
// NAN definition copied from math.h included in the Windows SDK version 10.0.14393.0
#ifndef NAN
# ifndef _HUGE_ENUF
# define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow
# endif
# define NAN_INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF))
# define NAN ((float)(NAN_INFINITY * 0.0F))
#endif
#ifndef INF
# ifdef INFINITY
# define INF INFINITY
# elif defined(HUGE_VAL)
# define INF HUGE_VAL
# elif defined(_MSC_VER)
# include <float.h>
# define INF (DBL_MAX+DBL_MAX)
# else
# define INF (1.0 / 0.0)
# endif
#endif
"""

6 changes: 6 additions & 0 deletions src/nifc/genexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ proc genx(c: var GeneratedCode; t: Tree; n: NodePos) =
c.add makeCString(c.m.lits.strings[t[n].litId])
of NilC:
c.add NullPtr
of InfC:
c.add "INF"
of NegInfC:
c.add "-INF"
of NanC:
c.add "NAN"
of AconstrC:
c.objConstrType(t, n.firstSon)
c.add CurlyLe
Expand Down
2 changes: 1 addition & 1 deletion src/nifc/native/analyser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ proc analyseVarUsages(c: var Context; t: Tree; n: NodePos) =
let k = t[n].kind
case k
of Empty, Ident, SymDef, IntLit, UIntLit, FloatLit, CharLit, StrLit, Err,
NilC, FalseC, TrueC, SizeofC:
NilC, FalseC, TrueC, SizeofC, InfC, NegInfC, NanC:
discard
of StmtsC, ScopeC:
c.openScope()
Expand Down
1 change: 1 addition & 0 deletions src/nifc/nifc_grammar.nif
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
(par Expr)
(addr Lvalue)
(nil) (false) (true)
(inf) (neginf) (nan)
(and Expr Expr)
(or Expr Expr)
(not Expr)
Expand Down
4 changes: 4 additions & 0 deletions src/nifc/nifc_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ type
NodeclC = "nodecl"
InclC = "incl"
SufC = "suf"
InfC = "inf"
NegInfC = "neginf"
NanC = "nan"


const
CallingConventions* = {CdeclC..MemberC}
Expand Down
2 changes: 1 addition & 1 deletion src/nifc/typenav.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ proc getType*(m: var Module; t: Tree; n: NodePos): TypeDesc =
result = TypeDesc(p: v.typ)
of IntLit, SizeofC: result = createIntegralType(m.lits, IntC, "-1")
of UIntLit: result = createIntegralType(m.lits, UIntC, "-1")
of FloatLit: result = createIntegralType(m.lits, FloatC, "64")
of FloatLit, InfC, NegInfC, NanC: result = createIntegralType(m.lits, FloatC, "64")
of CharLit: result = createIntegralType(m.lits, CharC, "8")
of StrLit: result = makePtrType(m, createIntegralType(m.lits, CharC, "8"))
of TrueC, FalseC, AndC, OrC, NotC, EqC, NeqC, LeC, LtC:
Expand Down
9 changes: 9 additions & 0 deletions tests/data/nifc_grammar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ proc matchExpr(c: var Context; it: var Item): bool =
if isTag(c, it, TrueT):
or2 = true
break or3
if isTag(c, it, InfT):
or2 = true
break or3
if isTag(c, it, NeginfT):
or2 = true
break or3
if isTag(c, it, NanT):
or2 = true
break or3
var kw6 = false
if isTag(c, it, AndT):
if not matchExpr(c, it):
Expand Down
7 changes: 7 additions & 0 deletions tests/nifc/issues.nif
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@
)
)

(proc :foo.floatspecial . . . (stmts
(var :x.m . (f +32) (inf))
(var :x2.m . (f +64) (neginf))
(var :x3.m . (f +64) (nan))
))

(proc :main.c . (i +32) . (stmts
1,1,hello.nim(var :x1.m . (i +32) +12)
1,2,hello.nim(var :z2.m . (ptr (c +8)) (cast (ptr (c +8)) (suf "hello" "r")))
Expand All @@ -145,6 +151,7 @@
(call foo.while)
(call foo.neg)
(call foo.cs2)
(call foo.floatspecial)

(var :x.c . (i +8) (suf +12 "i8"))
(var :x.m . (i +16) (suf -12 "i16"))
Expand Down

0 comments on commit a32cd88

Please sign in to comment.